blob: 6d8c2dcbfa0811394abc056acea7f9e100f44836 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020084#define CREATE_TRACE_POINTS
85#include <trace/events/io_uring.h>
86
Jens Axboe2b188cc2019-01-07 10:46:33 -070087#include <uapi/linux/io_uring.h>
88
89#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060090#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
Daniel Xu5277dea2019-09-14 14:23:45 -070092#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060093#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060094
95/*
96 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
97 */
98#define IORING_FILE_TABLE_SHIFT 9
99#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
100#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
101#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200102#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
103 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700104
105struct io_uring {
106 u32 head ____cacheline_aligned_in_smp;
107 u32 tail ____cacheline_aligned_in_smp;
108};
109
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 * This data is shared with the application through the mmap at offsets
112 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 *
114 * The offsets to the member fields are published through struct
115 * io_sqring_offsets when calling io_uring_setup.
116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
119 * Head and tail offsets into the ring; the offsets need to be
120 * masked to get valid indices.
121 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * The kernel controls head of the sq ring and the tail of the cq ring,
123 * and the application controls tail of the sq ring and the head of the
124 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 * ring_entries - 1)
130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 u32 sq_ring_mask, cq_ring_mask;
132 /* Ring sizes (constant, power of 2) */
133 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Number of invalid entries dropped by the kernel due to
136 * invalid index stored in array
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application (i.e. get number of "new events" by comparing to
140 * cached value).
141 *
142 * After a new SQ head value was read by the application this
143 * counter includes all submissions that were dropped reaching
144 * the new SQ head (and possibly more).
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200148 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application.
152 *
153 * The application needs a full memory barrier before checking
154 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
155 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000156 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200158 * Runtime CQ flags
159 *
160 * Written by the application, shouldn't be modified by the
161 * kernel.
162 */
163 u32 cq_flags;
164 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * Number of completion events lost because the queue was full;
166 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800167 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 * the completion queue.
169 *
170 * Written by the kernel, shouldn't be modified by the
171 * application (i.e. get number of "new events" by comparing to
172 * cached value).
173 *
174 * As completion events come in out of order this counter is not
175 * ordered with any other data.
176 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000177 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200178 /*
179 * Ring buffer of completion events.
180 *
181 * The kernel writes completion events fresh every time they are
182 * produced, so the application is allowed to modify pending
183 * entries.
184 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000185 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700186};
187
Jens Axboeedafcce2019-01-09 09:16:05 -0700188struct io_mapped_ubuf {
189 u64 ubuf;
190 size_t len;
191 struct bio_vec *bvec;
192 unsigned int nr_bvecs;
193};
194
Jens Axboe65e19f52019-10-26 07:20:21 -0600195struct fixed_file_table {
196 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700197};
198
Xiaoguang Wang05589552020-03-31 14:05:18 +0800199struct fixed_file_ref_node {
200 struct percpu_ref refs;
201 struct list_head node;
202 struct list_head file_list;
203 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600204 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800205};
206
Jens Axboe05f3fb32019-12-09 11:22:50 -0700207struct fixed_file_data {
208 struct fixed_file_table *table;
209 struct io_ring_ctx *ctx;
210
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700212 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800214 struct list_head ref_list;
215 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700216};
217
Jens Axboe5a2e7452020-02-23 16:23:11 -0700218struct io_buffer {
219 struct list_head list;
220 __u64 addr;
221 __s32 len;
222 __u16 bid;
223};
224
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200225struct io_restriction {
226 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
227 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
228 u8 sqe_flags_allowed;
229 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200230 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200231};
232
Jens Axboe534ca6d2020-09-02 13:52:19 -0600233struct io_sq_data {
234 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600235 struct mutex lock;
236
237 /* ctx's that are using this sqd */
238 struct list_head ctx_list;
239 struct list_head ctx_new_list;
240 struct mutex ctx_lock;
241
Jens Axboe534ca6d2020-09-02 13:52:19 -0600242 struct task_struct *thread;
243 struct wait_queue_head wait;
244};
245
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246struct io_ring_ctx {
247 struct {
248 struct percpu_ref refs;
249 } ____cacheline_aligned_in_smp;
250
251 struct {
252 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800253 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700254 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800255 unsigned int cq_overflow_flushed: 1;
256 unsigned int drain_next: 1;
257 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200258 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259
Hristo Venev75b28af2019-08-26 17:23:46 +0000260 /*
261 * Ring buffer of indices into array of io_uring_sqe, which is
262 * mmapped by the application using the IORING_OFF_SQES offset.
263 *
264 * This indirection could e.g. be used to assign fixed
265 * io_uring_sqe entries to operations and only submit them to
266 * the queue when needed.
267 *
268 * The kernel modifies neither the indices array nor the entries
269 * array.
270 */
271 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700272 unsigned cached_sq_head;
273 unsigned sq_entries;
274 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700275 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600276 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600279
280 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600281 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700282 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
Jens Axboefcb323c2019-10-24 12:39:47 -0600284 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286 } ____cacheline_aligned_in_smp;
287
Hristo Venev75b28af2019-08-26 17:23:46 +0000288 struct io_rings *rings;
289
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600291 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600292
293 /*
294 * For SQPOLL usage - we hold a reference to the parent task, so we
295 * have access to the ->files
296 */
297 struct task_struct *sqo_task;
298
299 /* Only used for accounting purposes */
300 struct mm_struct *mm_account;
301
Jens Axboe534ca6d2020-09-02 13:52:19 -0600302 struct io_sq_data *sq_data; /* if using sq thread polling */
303
Jens Axboe90554202020-09-03 12:12:41 -0600304 struct wait_queue_head sqo_sq_wait;
Jens Axboe6a779382020-09-02 12:21:41 -0600305 struct wait_queue_entry sqo_wait_entry;
Jens Axboe69fb2132020-09-14 11:16:23 -0600306 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307
Jens Axboe6b063142019-01-10 22:13:58 -0700308 /*
309 * If used, fixed file set. Writers must ensure that ->refs is dead,
310 * readers must ensure that ->refs is alive as long as the file* is
311 * used. Only updated through io_uring_register(2).
312 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700313 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700314 unsigned nr_user_files;
315
Jens Axboeedafcce2019-01-09 09:16:05 -0700316 /* if used, fixed mapped user buffers */
317 unsigned nr_user_bufs;
318 struct io_mapped_ubuf *user_bufs;
319
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320 struct user_struct *user;
321
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700322 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700323
Jens Axboe0f158b42020-05-14 17:18:39 -0600324 struct completion ref_comp;
325 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700326
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700327 /* if all else fails... */
328 struct io_kiocb *fallback_req;
329
Jens Axboe206aefd2019-11-07 18:27:42 -0700330#if defined(CONFIG_UNIX)
331 struct socket *ring_sock;
332#endif
333
Jens Axboe5a2e7452020-02-23 16:23:11 -0700334 struct idr io_buffer_idr;
335
Jens Axboe071698e2020-01-28 10:04:42 -0700336 struct idr personality_idr;
337
Jens Axboe206aefd2019-11-07 18:27:42 -0700338 struct {
339 unsigned cached_cq_tail;
340 unsigned cq_entries;
341 unsigned cq_mask;
342 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700343 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700344 struct wait_queue_head cq_wait;
345 struct fasync_struct *cq_fasync;
346 struct eventfd_ctx *cq_ev_fd;
347 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348
349 struct {
350 struct mutex uring_lock;
351 wait_queue_head_t wait;
352 } ____cacheline_aligned_in_smp;
353
354 struct {
355 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700356
Jens Axboedef596e2019-01-09 08:59:42 -0700357 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300358 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700359 * io_uring instances that don't use IORING_SETUP_SQPOLL.
360 * For SQPOLL, only the single threaded io_sq_thread() will
361 * manipulate the list, hence no extra locking is needed there.
362 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300363 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700364 struct hlist_head *cancel_hash;
365 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700366 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600367
368 spinlock_t inflight_lock;
369 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700370 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600371
Jens Axboe4a38aed22020-05-14 17:21:15 -0600372 struct delayed_work file_put_work;
373 struct llist_head file_put_llist;
374
Jens Axboe85faa7b2020-04-09 18:14:00 -0600375 struct work_struct exit_work;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200376 struct io_restriction restrictions;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377};
378
Jens Axboe09bb8392019-03-13 12:39:28 -0600379/*
380 * First field must be the file pointer in all the
381 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
382 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700383struct io_poll_iocb {
384 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700385 union {
386 struct wait_queue_head *head;
387 u64 addr;
388 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700389 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600390 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700391 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700392 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700393};
394
Jens Axboeb5dba592019-12-11 14:02:38 -0700395struct io_close {
396 struct file *file;
397 struct file *put_file;
398 int fd;
399};
400
Jens Axboead8a48a2019-11-15 08:49:11 -0700401struct io_timeout_data {
402 struct io_kiocb *req;
403 struct hrtimer timer;
404 struct timespec64 ts;
405 enum hrtimer_mode mode;
406};
407
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700408struct io_accept {
409 struct file *file;
410 struct sockaddr __user *addr;
411 int __user *addr_len;
412 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600413 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700414};
415
416struct io_sync {
417 struct file *file;
418 loff_t len;
419 loff_t off;
420 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700421 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700422};
423
Jens Axboefbf23842019-12-17 18:45:56 -0700424struct io_cancel {
425 struct file *file;
426 u64 addr;
427};
428
Jens Axboeb29472e2019-12-17 18:50:29 -0700429struct io_timeout {
430 struct file *file;
431 u64 addr;
432 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300433 u32 off;
434 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300435 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700436};
437
Jens Axboe9adbd452019-12-20 08:45:55 -0700438struct io_rw {
439 /* NOTE: kiocb has the file as the first member, so don't do it here */
440 struct kiocb kiocb;
441 u64 addr;
442 u64 len;
443};
444
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700445struct io_connect {
446 struct file *file;
447 struct sockaddr __user *addr;
448 int addr_len;
449};
450
Jens Axboee47293f2019-12-20 08:58:21 -0700451struct io_sr_msg {
452 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700453 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300454 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700455 void __user *buf;
456 };
Jens Axboee47293f2019-12-20 08:58:21 -0700457 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700458 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700459 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700460 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700461};
462
Jens Axboe15b71ab2019-12-11 11:20:36 -0700463struct io_open {
464 struct file *file;
465 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700466 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700467 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600468 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700469};
470
Jens Axboe05f3fb32019-12-09 11:22:50 -0700471struct io_files_update {
472 struct file *file;
473 u64 arg;
474 u32 nr_args;
475 u32 offset;
476};
477
Jens Axboe4840e412019-12-25 22:03:45 -0700478struct io_fadvise {
479 struct file *file;
480 u64 offset;
481 u32 len;
482 u32 advice;
483};
484
Jens Axboec1ca7572019-12-25 22:18:28 -0700485struct io_madvise {
486 struct file *file;
487 u64 addr;
488 u32 len;
489 u32 advice;
490};
491
Jens Axboe3e4827b2020-01-08 15:18:09 -0700492struct io_epoll {
493 struct file *file;
494 int epfd;
495 int op;
496 int fd;
497 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498};
499
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300500struct io_splice {
501 struct file *file_out;
502 struct file *file_in;
503 loff_t off_out;
504 loff_t off_in;
505 u64 len;
506 unsigned int flags;
507};
508
Jens Axboeddf0322d2020-02-23 16:41:33 -0700509struct io_provide_buf {
510 struct file *file;
511 __u64 addr;
512 __s32 len;
513 __u32 bgid;
514 __u16 nbufs;
515 __u16 bid;
516};
517
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700518struct io_statx {
519 struct file *file;
520 int dfd;
521 unsigned int mask;
522 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700523 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700524 struct statx __user *buffer;
525};
526
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300527struct io_completion {
528 struct file *file;
529 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300530 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300531};
532
Jens Axboef499a022019-12-02 16:28:46 -0700533struct io_async_connect {
534 struct sockaddr_storage address;
535};
536
Jens Axboe03b12302019-12-02 18:50:25 -0700537struct io_async_msghdr {
538 struct iovec fast_iov[UIO_FASTIOV];
539 struct iovec *iov;
540 struct sockaddr __user *uaddr;
541 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700542 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700543};
544
Jens Axboef67676d2019-12-02 11:03:47 -0700545struct io_async_rw {
546 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600547 const struct iovec *free_iovec;
548 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600549 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600550 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700551};
552
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700553struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700554 union {
555 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700556 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700557 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700558 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700559 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700560};
561
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300562enum {
563 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
564 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
565 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
566 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
567 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700568 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300569
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300570 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300571 REQ_F_FAIL_LINK_BIT,
572 REQ_F_INFLIGHT_BIT,
573 REQ_F_CUR_POS_BIT,
574 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300578 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700579 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600581 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800582 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700583
584 /* not a real bit, just to check we're not overflowing the space */
585 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300586};
587
588enum {
589 /* ctx owns file */
590 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
591 /* drain existing IO first */
592 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
593 /* linked sqes */
594 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
595 /* doesn't sever on completion < 0 */
596 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
597 /* IOSQE_ASYNC */
598 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700599 /* IOSQE_BUFFER_SELECT */
600 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300601
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300602 /* head of a link */
603 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300604 /* fail rest of links */
605 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
606 /* on inflight list */
607 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
608 /* read/write uses file position */
609 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
610 /* must not punt to workers */
611 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300612 /* has linked timeout */
613 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300614 /* regular file */
615 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300616 /* completion under lock */
617 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300618 /* needs cleanup */
619 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700620 /* already went through poll handler */
621 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700622 /* buffer already selected */
623 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600624 /* doesn't need file table for this request */
625 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800626 /* io_wq_work is initialized */
627 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700628};
629
630struct async_poll {
631 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600632 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300633};
634
Jens Axboe09bb8392019-03-13 12:39:28 -0600635/*
636 * NOTE! Each of the iocb union members has the file pointer
637 * as the first entry in their struct definition. So you can
638 * access the file pointer through any of the sub-structs,
639 * or directly as just 'ki_filp' in this struct.
640 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700642 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600643 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700644 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700645 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700646 struct io_accept accept;
647 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700648 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700649 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700650 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700651 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700652 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700653 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700654 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700655 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700656 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700657 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300658 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700659 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700660 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300661 /* use only after cleaning per-op data, see io_clean_op() */
662 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700663 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700665 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700666 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800667 /* polled IO has completed */
668 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700670 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300671 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700672
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300673 struct io_ring_ctx *ctx;
674 unsigned int flags;
675 refcount_t refs;
676 struct task_struct *task;
677 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700678
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300679 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700680
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300681 /*
682 * 1. used with ctx->iopoll_list with reads/writes
683 * 2. to track reqs with ->files (see io_op_def::file_table)
684 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300685 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600686
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300687 struct percpu_ref *fixed_file_refs;
688 struct callback_head task_work;
689 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
690 struct hlist_node hash_node;
691 struct async_poll *apoll;
692 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700693};
694
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300695struct io_defer_entry {
696 struct list_head list;
697 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300698 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300699};
700
Jens Axboedef596e2019-01-09 08:59:42 -0700701#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700702
Jens Axboe013538b2020-06-22 09:29:15 -0600703struct io_comp_state {
704 unsigned int nr;
705 struct list_head list;
706 struct io_ring_ctx *ctx;
707};
708
Jens Axboe9a56a232019-01-09 09:06:50 -0700709struct io_submit_state {
710 struct blk_plug plug;
711
712 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700713 * io_kiocb alloc cache
714 */
715 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300716 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700717
718 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600719 * Batch completion logic
720 */
721 struct io_comp_state comp;
722
723 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700724 * File reference cache
725 */
726 struct file *file;
727 unsigned int fd;
728 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700729 unsigned int ios_left;
730};
731
Jens Axboed3656342019-12-18 09:50:26 -0700732struct io_op_def {
733 /* needs req->io allocated for deferral/async */
734 unsigned async_ctx : 1;
735 /* needs current->mm setup, does mm access */
736 unsigned needs_mm : 1;
737 /* needs req->file assigned */
738 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600739 /* don't fail if file grab fails */
740 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700741 /* hash wq insertion if file is a regular file */
742 unsigned hash_reg_file : 1;
743 /* unbound wq insertion if file is a non-regular file */
744 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700745 /* opcode is not supported by this kernel */
746 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700747 /* needs file table */
748 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700749 /* needs ->fs */
750 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700751 /* set if opcode supports polled "wait" */
752 unsigned pollin : 1;
753 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700754 /* op supports buffer selection */
755 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300756 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700757};
758
Jens Axboe738277a2020-09-03 05:54:56 -0600759static const struct io_op_def io_op_defs[] __read_mostly = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_NOP] = {},
761 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700762 .async_ctx = 1,
763 .needs_mm = 1,
764 .needs_file = 1,
765 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700766 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700767 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .async_ctx = 1,
771 .needs_mm = 1,
772 .needs_file = 1,
773 .hash_reg_file = 1,
774 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700775 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300776 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_file = 1,
780 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300781 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700782 .needs_file = 1,
783 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700784 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700787 .needs_file = 1,
788 .hash_reg_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700790 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300791 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700792 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300793 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700794 .needs_file = 1,
795 .unbound_nonreg_file = 1,
796 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300797 [IORING_OP_POLL_REMOVE] = {},
798 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .needs_file = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .async_ctx = 1,
803 .needs_mm = 1,
804 .needs_file = 1,
805 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700806 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700807 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700808 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300809 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700810 .async_ctx = 1,
811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700814 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700815 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700816 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .async_ctx = 1,
820 .needs_mm = 1,
821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_TIMEOUT_REMOVE] = {},
823 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_mm = 1,
825 .needs_file = 1,
826 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700827 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700828 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_ASYNC_CANCEL] = {},
831 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700832 .async_ctx = 1,
833 .needs_mm = 1,
834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .async_ctx = 1,
837 .needs_mm = 1,
838 .needs_file = 1,
839 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700840 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700843 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300844 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700845 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300846 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700847 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700848 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700849 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300850 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600851 .needs_file = 1,
852 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700853 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700856 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700857 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700860 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700861 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600862 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700865 .needs_mm = 1,
866 .needs_file = 1,
867 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700868 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700869 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700872 .needs_mm = 1,
873 .needs_file = 1,
874 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700875 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300876 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700879 .needs_file = 1,
880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700882 .needs_mm = 1,
883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700885 .needs_mm = 1,
886 .needs_file = 1,
887 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700888 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700891 .needs_mm = 1,
892 .needs_file = 1,
893 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700894 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700895 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700898 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700899 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700900 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700901 [IORING_OP_EPOLL_CTL] = {
902 .unbound_nonreg_file = 1,
903 .file_table = 1,
904 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300905 [IORING_OP_SPLICE] = {
906 .needs_file = 1,
907 .hash_reg_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700909 },
910 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700911 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300912 [IORING_OP_TEE] = {
913 .needs_file = 1,
914 .hash_reg_file = 1,
915 .unbound_nonreg_file = 1,
916 },
Jens Axboed3656342019-12-18 09:50:26 -0700917};
918
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700919enum io_mem_account {
920 ACCT_LOCKED,
921 ACCT_PINNED,
922};
923
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300924static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
925 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700926static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800927static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600928static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700929static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700930static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600931static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700932static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700933static int __io_sqe_files_update(struct io_ring_ctx *ctx,
934 struct io_uring_files_update *ip,
935 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300936static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300937static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700938static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
939 int fd, struct file **out_file, bool fixed);
940static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600941 const struct io_uring_sqe *sqe,
942 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600943static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600944
Jens Axboeb63534c2020-06-04 11:28:00 -0600945static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
946 struct iovec **iovec, struct iov_iter *iter,
947 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600948static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
949 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600950 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951
952static struct kmem_cache *req_cachep;
953
Jens Axboe738277a2020-09-03 05:54:56 -0600954static const struct file_operations io_uring_fops __read_mostly;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955
956struct sock *io_uring_get_socket(struct file *file)
957{
958#if defined(CONFIG_UNIX)
959 if (file->f_op == &io_uring_fops) {
960 struct io_ring_ctx *ctx = file->private_data;
961
962 return ctx->ring_sock->sk;
963 }
964#endif
965 return NULL;
966}
967EXPORT_SYMBOL(io_uring_get_socket);
968
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300969static inline void io_clean_op(struct io_kiocb *req)
970{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300971 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
972 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300973 __io_clean_op(req);
974}
975
Jens Axboe4349f302020-07-09 15:07:01 -0600976static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600977{
978 struct mm_struct *mm = current->mm;
979
980 if (mm) {
981 kthread_unuse_mm(mm);
982 mmput(mm);
983 }
984}
985
986static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
987{
988 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300989 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600990 !ctx->sqo_task->mm ||
991 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600992 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600993 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600994 }
995
996 return 0;
997}
998
999static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1000 struct io_kiocb *req)
1001{
1002 if (!io_op_defs[req->opcode].needs_mm)
1003 return 0;
1004 return __io_sq_thread_acquire_mm(ctx);
1005}
1006
1007static inline void req_set_fail_links(struct io_kiocb *req)
1008{
1009 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1010 req->flags |= REQ_F_FAIL_LINK;
1011}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001012
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001013/*
1014 * Note: must call io_req_init_async() for the first time you
1015 * touch any members of io_wq_work.
1016 */
1017static inline void io_req_init_async(struct io_kiocb *req)
1018{
1019 if (req->flags & REQ_F_WORK_INITIALIZED)
1020 return;
1021
1022 memset(&req->work, 0, sizeof(req->work));
1023 req->flags |= REQ_F_WORK_INITIALIZED;
1024}
1025
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001026static inline bool io_async_submit(struct io_ring_ctx *ctx)
1027{
1028 return ctx->flags & IORING_SETUP_SQPOLL;
1029}
1030
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1032{
1033 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1034
Jens Axboe0f158b42020-05-14 17:18:39 -06001035 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036}
1037
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001038static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1039{
1040 return !req->timeout.off;
1041}
1042
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1044{
1045 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001046 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1049 if (!ctx)
1050 return NULL;
1051
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001052 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1053 if (!ctx->fallback_req)
1054 goto err;
1055
Jens Axboe78076bb2019-12-04 19:56:40 -07001056 /*
1057 * Use 5 bits less than the max cq entries, that should give us around
1058 * 32 entries per hash list if totally full and uniformly spread.
1059 */
1060 hash_bits = ilog2(p->cq_entries);
1061 hash_bits -= 5;
1062 if (hash_bits <= 0)
1063 hash_bits = 1;
1064 ctx->cancel_hash_bits = hash_bits;
1065 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1066 GFP_KERNEL);
1067 if (!ctx->cancel_hash)
1068 goto err;
1069 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1070
Roman Gushchin21482892019-05-07 10:01:48 -07001071 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001072 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1073 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074
1075 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001076 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001077 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001079 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001080 init_completion(&ctx->ref_comp);
1081 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001082 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001083 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 mutex_init(&ctx->uring_lock);
1085 init_waitqueue_head(&ctx->wait);
1086 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001087 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001088 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001089 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001090 init_waitqueue_head(&ctx->inflight_wait);
1091 spin_lock_init(&ctx->inflight_lock);
1092 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001093 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1094 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001096err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001097 if (ctx->fallback_req)
1098 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001099 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001100 kfree(ctx);
1101 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102}
1103
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001104static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001105{
Jens Axboe2bc99302020-07-09 09:43:27 -06001106 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1107 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001108
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001109 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001110 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001111 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001112
Bob Liu9d858b22019-11-13 18:06:25 +08001113 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001114}
1115
Jens Axboede0617e2019-04-06 21:51:27 -06001116static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117{
Hristo Venev75b28af2019-08-26 17:23:46 +00001118 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119
Pavel Begunkov07910152020-01-17 03:52:46 +03001120 /* order cqe stores with ring update */
1121 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122
Pavel Begunkov07910152020-01-17 03:52:46 +03001123 if (wq_has_sleeper(&ctx->cq_wait)) {
1124 wake_up_interruptible(&ctx->cq_wait);
1125 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126 }
1127}
1128
Jens Axboe51a4cc12020-08-10 10:55:56 -06001129/*
1130 * Returns true if we need to defer file table putting. This can only happen
1131 * from the error path with REQ_F_COMP_LOCKED set.
1132 */
1133static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001134{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001135 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001136 return false;
1137
1138 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001139
Jens Axboecccf0ee2020-01-27 16:34:48 -07001140 if (req->work.mm) {
1141 mmdrop(req->work.mm);
1142 req->work.mm = NULL;
1143 }
1144 if (req->work.creds) {
1145 put_cred(req->work.creds);
1146 req->work.creds = NULL;
1147 }
Jens Axboeff002b32020-02-07 16:05:21 -07001148 if (req->work.fs) {
1149 struct fs_struct *fs = req->work.fs;
1150
Jens Axboe51a4cc12020-08-10 10:55:56 -06001151 if (req->flags & REQ_F_COMP_LOCKED)
1152 return true;
1153
Jens Axboeff002b32020-02-07 16:05:21 -07001154 spin_lock(&req->work.fs->lock);
1155 if (--fs->users)
1156 fs = NULL;
1157 spin_unlock(&req->work.fs->lock);
1158 if (fs)
1159 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001160 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001161 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001162
1163 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001164}
1165
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001166static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001167{
Jens Axboed3656342019-12-18 09:50:26 -07001168 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001169
Pavel Begunkov16d59802020-07-12 16:16:47 +03001170 io_req_init_async(req);
1171
Jens Axboed3656342019-12-18 09:50:26 -07001172 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001173 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001174 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001175 } else {
1176 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001177 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001178 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001179 if (!req->work.mm && def->needs_mm) {
1180 mmgrab(current->mm);
1181 req->work.mm = current->mm;
1182 }
1183 if (!req->work.creds)
1184 req->work.creds = get_current_cred();
1185 if (!req->work.fs && def->needs_fs) {
1186 spin_lock(&current->fs->lock);
1187 if (!current->fs->in_exec) {
1188 req->work.fs = current->fs;
1189 req->work.fs->users++;
1190 } else {
1191 req->work.flags |= IO_WQ_WORK_CANCEL;
1192 }
1193 spin_unlock(&current->fs->lock);
1194 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001195 if (def->needs_fsize)
1196 req->work.fsize = rlimit(RLIMIT_FSIZE);
1197 else
1198 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001199}
1200
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001201static void io_prep_async_link(struct io_kiocb *req)
1202{
1203 struct io_kiocb *cur;
1204
1205 io_prep_async_work(req);
1206 if (req->flags & REQ_F_LINK_HEAD)
1207 list_for_each_entry(cur, &req->link_list, link_list)
1208 io_prep_async_work(cur);
1209}
1210
Jens Axboe7271ef32020-08-10 09:55:22 -06001211static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001212{
Jackie Liua197f662019-11-08 08:09:12 -07001213 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001214 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001215
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001216 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1217 &req->work, req->flags);
1218 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001219 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001220}
1221
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001222static void io_queue_async_work(struct io_kiocb *req)
1223{
Jens Axboe7271ef32020-08-10 09:55:22 -06001224 struct io_kiocb *link;
1225
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001226 /* init ->work of the whole link before punting */
1227 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001228 link = __io_queue_async_work(req);
1229
1230 if (link)
1231 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001232}
1233
Jens Axboe5262f562019-09-17 12:26:57 -06001234static void io_kill_timeout(struct io_kiocb *req)
1235{
1236 int ret;
1237
Jens Axboe2d283902019-12-04 11:08:05 -07001238 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001239 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001240 atomic_set(&req->ctx->cq_timeouts,
1241 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001242 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001243 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001244 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001245 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001246 }
1247}
1248
Jens Axboef3606e32020-09-22 08:18:24 -06001249static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1250{
1251 struct io_ring_ctx *ctx = req->ctx;
1252
1253 if (!tsk || req->task == tsk)
1254 return true;
Jens Axboe534ca6d2020-09-02 13:52:19 -06001255 if (ctx->flags & IORING_SETUP_SQPOLL) {
1256 if (ctx->sq_data && req->task == ctx->sq_data->thread)
1257 return true;
1258 }
Jens Axboef3606e32020-09-22 08:18:24 -06001259 return false;
1260}
1261
Jens Axboe76e1b642020-09-26 15:05:03 -06001262/*
1263 * Returns true if we found and killed one or more timeouts
1264 */
1265static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001266{
1267 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001268 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001269
1270 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001271 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Jens Axboe76e1b642020-09-26 15:05:03 -06001272 if (io_task_match(req, tsk)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001273 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001274 canceled++;
1275 }
Jens Axboef3606e32020-09-22 08:18:24 -06001276 }
Jens Axboe5262f562019-09-17 12:26:57 -06001277 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001278 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001279}
1280
Pavel Begunkov04518942020-05-26 20:34:05 +03001281static void __io_queue_deferred(struct io_ring_ctx *ctx)
1282{
1283 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001284 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1285 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001286 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001287
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001288 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001289 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001290 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001291 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001292 link = __io_queue_async_work(de->req);
1293 if (link) {
1294 __io_queue_linked_timeout(link);
1295 /* drop submission reference */
1296 link->flags |= REQ_F_COMP_LOCKED;
1297 io_put_req(link);
1298 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001299 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001300 } while (!list_empty(&ctx->defer_list));
1301}
1302
Pavel Begunkov360428f2020-05-30 14:54:17 +03001303static void io_flush_timeouts(struct io_ring_ctx *ctx)
1304{
1305 while (!list_empty(&ctx->timeout_list)) {
1306 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001307 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001308
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001309 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001310 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001311 if (req->timeout.target_seq != ctx->cached_cq_tail
1312 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001313 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001314
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001315 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001316 io_kill_timeout(req);
1317 }
1318}
1319
Jens Axboede0617e2019-04-06 21:51:27 -06001320static void io_commit_cqring(struct io_ring_ctx *ctx)
1321{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001322 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001323 __io_commit_cqring(ctx);
1324
Pavel Begunkov04518942020-05-26 20:34:05 +03001325 if (unlikely(!list_empty(&ctx->defer_list)))
1326 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001327}
1328
Jens Axboe90554202020-09-03 12:12:41 -06001329static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1330{
1331 struct io_rings *r = ctx->rings;
1332
1333 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1334}
1335
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1337{
Hristo Venev75b28af2019-08-26 17:23:46 +00001338 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 unsigned tail;
1340
1341 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001342 /*
1343 * writes to the cq entry need to come after reading head; the
1344 * control dependency is enough as we're using WRITE_ONCE to
1345 * fill the cq entry
1346 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001347 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 return NULL;
1349
1350 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001351 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352}
1353
Jens Axboef2842ab2020-01-08 11:04:00 -07001354static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1355{
Jens Axboef0b493e2020-02-01 21:30:11 -07001356 if (!ctx->cq_ev_fd)
1357 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001358 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1359 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001360 if (!ctx->eventfd_async)
1361 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001362 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001363}
1364
Jens Axboeb41e9852020-02-17 09:52:41 -07001365static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001366{
1367 if (waitqueue_active(&ctx->wait))
1368 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001369 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1370 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001371 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001372 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001373}
1374
Pavel Begunkov46930142020-07-30 18:43:49 +03001375static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1376{
1377 if (list_empty(&ctx->cq_overflow_list)) {
1378 clear_bit(0, &ctx->sq_check_overflow);
1379 clear_bit(0, &ctx->cq_check_overflow);
1380 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1381 }
1382}
1383
Jens Axboee6c8aa92020-09-28 13:10:13 -06001384static inline bool io_match_files(struct io_kiocb *req,
1385 struct files_struct *files)
1386{
1387 if (!files)
1388 return true;
1389 if (req->flags & REQ_F_WORK_INITIALIZED)
1390 return req->work.files == files;
1391 return false;
1392}
1393
Jens Axboec4a2ed72019-11-21 21:01:26 -07001394/* Returns true if there are no backlogged entries after the flush */
Jens Axboee6c8aa92020-09-28 13:10:13 -06001395static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1396 struct task_struct *tsk,
1397 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001400 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001401 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001402 unsigned long flags;
1403 LIST_HEAD(list);
1404
1405 if (!force) {
1406 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001407 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1409 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001410 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001411 }
1412
1413 spin_lock_irqsave(&ctx->completion_lock, flags);
1414
1415 /* if force is set, the ring is going away. always drop after that */
1416 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001417 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001418
Jens Axboec4a2ed72019-11-21 21:01:26 -07001419 cqe = NULL;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001420 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1421 if (tsk && req->task != tsk)
1422 continue;
1423 if (!io_match_files(req, files))
1424 continue;
1425
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426 cqe = io_get_cqring(ctx);
1427 if (!cqe && !force)
1428 break;
1429
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001430 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 if (cqe) {
1432 WRITE_ONCE(cqe->user_data, req->user_data);
1433 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001434 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 } else {
1436 WRITE_ONCE(ctx->rings->cq_overflow,
1437 atomic_inc_return(&ctx->cached_cq_overflow));
1438 }
1439 }
1440
1441 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001442 io_cqring_mark_overflow(ctx);
1443
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1445 io_cqring_ev_posted(ctx);
1446
1447 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001448 req = list_first_entry(&list, struct io_kiocb, compl.list);
1449 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001450 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001451 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001452
1453 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001454}
1455
Jens Axboebcda7ba2020-02-23 16:42:51 -07001456static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001457{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459 struct io_uring_cqe *cqe;
1460
Jens Axboe78e19bb2019-11-06 15:21:34 -07001461 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001462
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 /*
1464 * If we can't get a cq entry, userspace overflowed the
1465 * submission (by quite a lot). Increment the overflow count in
1466 * the ring.
1467 */
1468 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001469 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001470 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001471 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001472 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe0f212202020-09-13 13:09:39 -06001473 } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
1474 /*
1475 * If we're in ring overflow flush mode, or in task cancel mode,
1476 * then we cannot store the request for later flushing, we need
1477 * to drop it on the floor.
1478 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479 WRITE_ONCE(ctx->rings->cq_overflow,
1480 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001481 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001482 if (list_empty(&ctx->cq_overflow_list)) {
1483 set_bit(0, &ctx->sq_check_overflow);
1484 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001485 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001486 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001487 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001489 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001490 refcount_inc(&req->refs);
1491 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492 }
1493}
1494
Jens Axboebcda7ba2020-02-23 16:42:51 -07001495static void io_cqring_fill_event(struct io_kiocb *req, long res)
1496{
1497 __io_cqring_fill_event(req, res, 0);
1498}
1499
Jens Axboee1e16092020-06-22 09:17:17 -06001500static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503 unsigned long flags;
1504
1505 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001506 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001507 io_commit_cqring(ctx);
1508 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1509
Jens Axboe8c838782019-03-12 15:48:16 -06001510 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001511}
1512
Jens Axboe229a7b62020-06-22 10:13:11 -06001513static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001514{
Jens Axboe229a7b62020-06-22 10:13:11 -06001515 struct io_ring_ctx *ctx = cs->ctx;
1516
1517 spin_lock_irq(&ctx->completion_lock);
1518 while (!list_empty(&cs->list)) {
1519 struct io_kiocb *req;
1520
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001521 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1522 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001523 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001524 if (!(req->flags & REQ_F_LINK_HEAD)) {
1525 req->flags |= REQ_F_COMP_LOCKED;
1526 io_put_req(req);
1527 } else {
1528 spin_unlock_irq(&ctx->completion_lock);
1529 io_put_req(req);
1530 spin_lock_irq(&ctx->completion_lock);
1531 }
1532 }
1533 io_commit_cqring(ctx);
1534 spin_unlock_irq(&ctx->completion_lock);
1535
1536 io_cqring_ev_posted(ctx);
1537 cs->nr = 0;
1538}
1539
1540static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1541 struct io_comp_state *cs)
1542{
1543 if (!cs) {
1544 io_cqring_add_event(req, res, cflags);
1545 io_put_req(req);
1546 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001547 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001548 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001549 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001550 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001551 if (++cs->nr >= 32)
1552 io_submit_flush_completions(cs);
1553 }
Jens Axboee1e16092020-06-22 09:17:17 -06001554}
1555
1556static void io_req_complete(struct io_kiocb *req, long res)
1557{
Jens Axboe229a7b62020-06-22 10:13:11 -06001558 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001559}
1560
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001561static inline bool io_is_fallback_req(struct io_kiocb *req)
1562{
1563 return req == (struct io_kiocb *)
1564 ((unsigned long) req->ctx->fallback_req & ~1UL);
1565}
1566
1567static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1568{
1569 struct io_kiocb *req;
1570
1571 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001572 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001573 return req;
1574
1575 return NULL;
1576}
1577
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001578static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1579 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580{
Jens Axboefd6fab22019-03-14 16:30:06 -06001581 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582 struct io_kiocb *req;
1583
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001584 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001585 size_t sz;
1586 int ret;
1587
1588 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001589 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1590
1591 /*
1592 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1593 * retry single alloc to be on the safe side.
1594 */
1595 if (unlikely(ret <= 0)) {
1596 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1597 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001598 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001599 ret = 1;
1600 }
Jens Axboe2579f912019-01-09 09:10:43 -07001601 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001602 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001603 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001604 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001605 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 }
1607
Jens Axboe2579f912019-01-09 09:10:43 -07001608 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001609fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001610 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611}
1612
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001613static inline void io_put_file(struct io_kiocb *req, struct file *file,
1614 bool fixed)
1615{
1616 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001617 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001618 else
1619 fput(file);
1620}
1621
Jens Axboe51a4cc12020-08-10 10:55:56 -06001622static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001623{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001624 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001625
Jens Axboe5acbbc82020-07-08 15:15:26 -06001626 if (req->io)
1627 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001628 if (req->file)
1629 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001630
Jens Axboe51a4cc12020-08-10 10:55:56 -06001631 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001632}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001633
Jens Axboe51a4cc12020-08-10 10:55:56 -06001634static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001635{
Jens Axboe0f212202020-09-13 13:09:39 -06001636 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001637 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001638
Jens Axboe0f212202020-09-13 13:09:39 -06001639 atomic_long_inc(&tctx->req_complete);
1640 if (tctx->in_idle)
1641 wake_up(&tctx->wait);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001642 put_task_struct(req->task);
1643
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001644 if (likely(!io_is_fallback_req(req)))
1645 kmem_cache_free(req_cachep, req);
1646 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001647 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1648 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001649}
1650
Jens Axboe51a4cc12020-08-10 10:55:56 -06001651static void io_req_task_file_table_put(struct callback_head *cb)
1652{
1653 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1654 struct fs_struct *fs = req->work.fs;
1655
1656 spin_lock(&req->work.fs->lock);
1657 if (--fs->users)
1658 fs = NULL;
1659 spin_unlock(&req->work.fs->lock);
1660 if (fs)
1661 free_fs_struct(fs);
1662 req->work.fs = NULL;
1663 __io_free_req_finish(req);
1664}
1665
1666static void __io_free_req(struct io_kiocb *req)
1667{
1668 if (!io_dismantle_req(req)) {
1669 __io_free_req_finish(req);
1670 } else {
1671 int ret;
1672
1673 init_task_work(&req->task_work, io_req_task_file_table_put);
1674 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1675 if (unlikely(ret)) {
1676 struct task_struct *tsk;
1677
1678 tsk = io_wq_get_task(req->ctx->io_wq);
1679 task_work_add(tsk, &req->task_work, 0);
1680 }
1681 }
1682}
1683
Jackie Liua197f662019-11-08 08:09:12 -07001684static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001685{
Jackie Liua197f662019-11-08 08:09:12 -07001686 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001687 int ret;
1688
Jens Axboe2d283902019-12-04 11:08:05 -07001689 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001690 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001691 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001692 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001693 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001694 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001695 return true;
1696 }
1697
1698 return false;
1699}
1700
Jens Axboeab0b6452020-06-30 08:43:15 -06001701static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001702{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001703 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001704 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001705
1706 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001707 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001708 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1709 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001710 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001711
1712 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001713 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001714 wake_ev = io_link_cancel_timeout(link);
1715 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001716 return wake_ev;
1717}
1718
1719static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001720{
Jens Axboe2665abf2019-11-05 12:40:47 -07001721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001722 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001723
Jens Axboeab0b6452020-06-30 08:43:15 -06001724 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1725 unsigned long flags;
1726
1727 spin_lock_irqsave(&ctx->completion_lock, flags);
1728 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001729 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001730 } else {
1731 wake_ev = __io_kill_linked_timeout(req);
1732 }
1733
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001734 if (wake_ev)
1735 io_cqring_ev_posted(ctx);
1736}
1737
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001738static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001739{
1740 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001741
Jens Axboe9e645e112019-05-10 16:07:28 -06001742 /*
1743 * The list should never be empty when we are called here. But could
1744 * potentially happen if the chain is messed up, check to be on the
1745 * safe side.
1746 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001747 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001748 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001749
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001750 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1751 list_del_init(&req->link_list);
1752 if (!list_empty(&nxt->link_list))
1753 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001754 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001755}
1756
1757/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001758 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001759 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001760static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001761{
Jens Axboe2665abf2019-11-05 12:40:47 -07001762 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001763
1764 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001765 struct io_kiocb *link = list_first_entry(&req->link_list,
1766 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001767
Pavel Begunkov44932332019-12-05 16:16:35 +03001768 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001769 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001770
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001771 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001772 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001773 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001774 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001775 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001776
1777 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001778 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001779}
1780
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001781static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001782{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001783 struct io_ring_ctx *ctx = req->ctx;
1784
1785 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1786 unsigned long flags;
1787
1788 spin_lock_irqsave(&ctx->completion_lock, flags);
1789 __io_fail_links(req);
1790 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1791 } else {
1792 __io_fail_links(req);
1793 }
1794
Jens Axboe9e645e112019-05-10 16:07:28 -06001795 io_cqring_ev_posted(ctx);
1796}
1797
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001798static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001799{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001800 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001801 if (req->flags & REQ_F_LINK_TIMEOUT)
1802 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001803
Jens Axboe9e645e112019-05-10 16:07:28 -06001804 /*
1805 * If LINK is set, we have dependent requests in this chain. If we
1806 * didn't fail this request, queue the first one up, moving any other
1807 * dependencies to the next request. In case of failure, fail the rest
1808 * of the chain.
1809 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001810 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1811 return io_req_link_next(req);
1812 io_fail_links(req);
1813 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001814}
Jens Axboe2665abf2019-11-05 12:40:47 -07001815
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001816static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1817{
1818 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1819 return NULL;
1820 return __io_req_find_next(req);
1821}
1822
Jens Axboefd7d6de2020-08-23 11:00:37 -06001823static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1824 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001825{
1826 struct task_struct *tsk = req->task;
1827 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001828 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001829
Jens Axboe6200b0a2020-09-13 14:38:30 -06001830 if (tsk->flags & PF_EXITING)
1831 return -ESRCH;
1832
Jens Axboec2c4c832020-07-01 15:37:11 -06001833 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001834 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1835 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1836 * processing task_work. There's no reliable way to tell if TWA_RESUME
1837 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001838 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001839 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001840 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001841 notify = TWA_SIGNAL;
1842
1843 ret = task_work_add(tsk, cb, notify);
1844 if (!ret)
1845 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001846
Jens Axboec2c4c832020-07-01 15:37:11 -06001847 return ret;
1848}
1849
Jens Axboec40f6372020-06-25 15:39:59 -06001850static void __io_req_task_cancel(struct io_kiocb *req, int error)
1851{
1852 struct io_ring_ctx *ctx = req->ctx;
1853
1854 spin_lock_irq(&ctx->completion_lock);
1855 io_cqring_fill_event(req, error);
1856 io_commit_cqring(ctx);
1857 spin_unlock_irq(&ctx->completion_lock);
1858
1859 io_cqring_ev_posted(ctx);
1860 req_set_fail_links(req);
1861 io_double_put_req(req);
1862}
1863
1864static void io_req_task_cancel(struct callback_head *cb)
1865{
1866 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001867 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001868
1869 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001870 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001871}
1872
1873static void __io_req_task_submit(struct io_kiocb *req)
1874{
1875 struct io_ring_ctx *ctx = req->ctx;
1876
Jens Axboec40f6372020-06-25 15:39:59 -06001877 if (!__io_sq_thread_acquire_mm(ctx)) {
1878 mutex_lock(&ctx->uring_lock);
1879 __io_queue_sqe(req, NULL, NULL);
1880 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001881 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001882 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001883 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001884}
1885
Jens Axboec40f6372020-06-25 15:39:59 -06001886static void io_req_task_submit(struct callback_head *cb)
1887{
1888 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001889 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001890
1891 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001892 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001893}
1894
1895static void io_req_task_queue(struct io_kiocb *req)
1896{
Jens Axboec40f6372020-06-25 15:39:59 -06001897 int ret;
1898
1899 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001900 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001901
Jens Axboefd7d6de2020-08-23 11:00:37 -06001902 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001903 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001904 struct task_struct *tsk;
1905
Jens Axboec40f6372020-06-25 15:39:59 -06001906 init_task_work(&req->task_work, io_req_task_cancel);
1907 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001908 task_work_add(tsk, &req->task_work, 0);
1909 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001910 }
Jens Axboec40f6372020-06-25 15:39:59 -06001911}
1912
Pavel Begunkovc3524382020-06-28 12:52:32 +03001913static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001914{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001915 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001916
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001917 if (nxt)
1918 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001919}
1920
Jens Axboe9e645e112019-05-10 16:07:28 -06001921static void io_free_req(struct io_kiocb *req)
1922{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001923 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001924 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001925}
1926
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001927struct req_batch {
1928 void *reqs[IO_IOPOLL_BATCH];
1929 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001930
1931 struct task_struct *task;
1932 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001933};
1934
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001935static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001936{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001937 rb->to_free = 0;
1938 rb->task_refs = 0;
1939 rb->task = NULL;
1940}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001941
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001942static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1943 struct req_batch *rb)
1944{
1945 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1946 percpu_ref_put_many(&ctx->refs, rb->to_free);
1947 rb->to_free = 0;
1948}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001949
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001950static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1951 struct req_batch *rb)
1952{
1953 if (rb->to_free)
1954 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001955 if (rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001956 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001957 put_task_struct_many(rb->task, rb->task_refs);
1958 rb->task = NULL;
1959 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001960}
1961
1962static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1963{
1964 if (unlikely(io_is_fallback_req(req))) {
1965 io_free_req(req);
1966 return;
1967 }
1968 if (req->flags & REQ_F_LINK_HEAD)
1969 io_queue_next(req);
1970
Jens Axboee3bc8e92020-09-24 08:45:57 -06001971 if (req->task != rb->task) {
Jens Axboe0f212202020-09-13 13:09:39 -06001972 if (rb->task) {
1973 atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
Jens Axboee3bc8e92020-09-24 08:45:57 -06001974 put_task_struct_many(rb->task, rb->task_refs);
Jens Axboe0f212202020-09-13 13:09:39 -06001975 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001976 rb->task = req->task;
1977 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001978 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001979 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001980
Jens Axboe51a4cc12020-08-10 10:55:56 -06001981 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001982 rb->reqs[rb->to_free++] = req;
1983 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1984 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001985}
1986
Jens Axboeba816ad2019-09-28 11:36:45 -06001987/*
1988 * Drop reference to request, return next in chain (if there is one) if this
1989 * was the last reference to this request.
1990 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001991static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001992{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001993 struct io_kiocb *nxt = NULL;
1994
Jens Axboe2a44f462020-02-25 13:25:41 -07001995 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001996 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001997 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001998 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001999 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000}
2001
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002static void io_put_req(struct io_kiocb *req)
2003{
Jens Axboedef596e2019-01-09 08:59:42 -07002004 if (refcount_dec_and_test(&req->refs))
2005 io_free_req(req);
2006}
2007
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002008static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002009{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002010 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002011
Pavel Begunkovf4db7182020-06-25 18:20:54 +03002012 /*
2013 * A ref is owned by io-wq in which context we're. So, if that's the
2014 * last one, it's safe to steal next work. False negatives are Ok,
2015 * it just will be re-punted async in io_put_work()
2016 */
2017 if (refcount_read(&req->refs) != 1)
2018 return NULL;
2019
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002020 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03002021 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002022}
2023
Jens Axboe978db572019-11-14 22:39:04 -07002024/*
2025 * Must only be used if we don't need to care about links, usually from
2026 * within the completion handling itself.
2027 */
2028static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06002029{
Jens Axboe78e19bb2019-11-06 15:21:34 -07002030 /* drop both submit and complete references */
2031 if (refcount_sub_and_test(2, &req->refs))
2032 __io_free_req(req);
2033}
2034
Jens Axboe978db572019-11-14 22:39:04 -07002035static void io_double_put_req(struct io_kiocb *req)
2036{
2037 /* drop both submit and complete references */
2038 if (refcount_sub_and_test(2, &req->refs))
2039 io_free_req(req);
2040}
2041
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002042static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06002043{
Jens Axboe84f97dc2019-11-06 11:27:53 -07002044 struct io_rings *rings = ctx->rings;
2045
Jens Axboead3eb2c2019-12-18 17:12:20 -07002046 if (test_bit(0, &ctx->cq_check_overflow)) {
2047 /*
2048 * noflush == true is from the waitqueue handler, just ensure
2049 * we wake up the task, and the next invocation will flush the
2050 * entries. We cannot safely to it from here.
2051 */
2052 if (noflush && !list_empty(&ctx->cq_overflow_list))
2053 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002054
Jens Axboee6c8aa92020-09-28 13:10:13 -06002055 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboead3eb2c2019-12-18 17:12:20 -07002056 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002057
Jens Axboea3a0e432019-08-20 11:03:11 -06002058 /* See comment at the top of this file */
2059 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002060 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002061}
2062
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002063static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2064{
2065 struct io_rings *rings = ctx->rings;
2066
2067 /* make sure SQ entry isn't read before tail */
2068 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2069}
2070
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002071static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002072{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002073 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002074
Jens Axboebcda7ba2020-02-23 16:42:51 -07002075 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2076 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002077 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002078 kfree(kbuf);
2079 return cflags;
2080}
2081
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002082static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2083{
2084 struct io_buffer *kbuf;
2085
2086 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2087 return io_put_kbuf(req, kbuf);
2088}
2089
Jens Axboe4c6e2772020-07-01 11:29:10 -06002090static inline bool io_run_task_work(void)
2091{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002092 /*
2093 * Not safe to run on exiting task, and the task_work handling will
2094 * not add work to such a task.
2095 */
2096 if (unlikely(current->flags & PF_EXITING))
2097 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002098 if (current->task_works) {
2099 __set_current_state(TASK_RUNNING);
2100 task_work_run();
2101 return true;
2102 }
2103
2104 return false;
2105}
2106
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002107static void io_iopoll_queue(struct list_head *again)
2108{
2109 struct io_kiocb *req;
2110
2111 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002112 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2113 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002114 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002115 } while (!list_empty(again));
2116}
2117
Jens Axboedef596e2019-01-09 08:59:42 -07002118/*
2119 * Find and free completed poll iocbs
2120 */
2121static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2122 struct list_head *done)
2123{
Jens Axboe8237e042019-12-28 10:48:22 -07002124 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002125 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002126 LIST_HEAD(again);
2127
2128 /* order with ->result store in io_complete_rw_iopoll() */
2129 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002130
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002131 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002132 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002133 int cflags = 0;
2134
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002135 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002136 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002137 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002138 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002139 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002140 continue;
2141 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002142 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002143
Jens Axboebcda7ba2020-02-23 16:42:51 -07002144 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002145 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002146
2147 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002148 (*nr_events)++;
2149
Pavel Begunkovc3524382020-06-28 12:52:32 +03002150 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002151 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002152 }
Jens Axboedef596e2019-01-09 08:59:42 -07002153
Jens Axboe09bb8392019-03-13 12:39:28 -06002154 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002155 if (ctx->flags & IORING_SETUP_SQPOLL)
2156 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002157 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002158
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002159 if (!list_empty(&again))
2160 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002161}
2162
Jens Axboedef596e2019-01-09 08:59:42 -07002163static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2164 long min)
2165{
2166 struct io_kiocb *req, *tmp;
2167 LIST_HEAD(done);
2168 bool spin;
2169 int ret;
2170
2171 /*
2172 * Only spin for completions if we don't have multiple devices hanging
2173 * off our complete list, and we're under the requested amount.
2174 */
2175 spin = !ctx->poll_multi_file && *nr_events < min;
2176
2177 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002178 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002179 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002180
2181 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002182 * Move completed and retryable entries to our local lists.
2183 * If we find a request that requires polling, break out
2184 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002185 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002186 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002187 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002188 continue;
2189 }
2190 if (!list_empty(&done))
2191 break;
2192
2193 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2194 if (ret < 0)
2195 break;
2196
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002197 /* iopoll may have completed current req */
2198 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002199 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002200
Jens Axboedef596e2019-01-09 08:59:42 -07002201 if (ret && spin)
2202 spin = false;
2203 ret = 0;
2204 }
2205
2206 if (!list_empty(&done))
2207 io_iopoll_complete(ctx, nr_events, &done);
2208
2209 return ret;
2210}
2211
2212/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002213 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002214 * non-spinning poll check - we'll still enter the driver poll loop, but only
2215 * as a non-spinning completion check.
2216 */
2217static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2218 long min)
2219{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002220 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002221 int ret;
2222
2223 ret = io_do_iopoll(ctx, nr_events, min);
2224 if (ret < 0)
2225 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002226 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002227 return 0;
2228 }
2229
2230 return 1;
2231}
2232
2233/*
2234 * We can't just wait for polled events to come to us, we have to actively
2235 * find and complete them.
2236 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002237static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002238{
2239 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2240 return;
2241
2242 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002243 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002244 unsigned int nr_events = 0;
2245
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002246 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002247
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002248 /* let it sleep and repeat later if can't complete a request */
2249 if (nr_events == 0)
2250 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002251 /*
2252 * Ensure we allow local-to-the-cpu processing to take place,
2253 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002254 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002255 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002256 if (need_resched()) {
2257 mutex_unlock(&ctx->uring_lock);
2258 cond_resched();
2259 mutex_lock(&ctx->uring_lock);
2260 }
Jens Axboedef596e2019-01-09 08:59:42 -07002261 }
2262 mutex_unlock(&ctx->uring_lock);
2263}
2264
Pavel Begunkov7668b922020-07-07 16:36:21 +03002265static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002266{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002267 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002268 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002269
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002270 /*
2271 * We disallow the app entering submit/complete with polling, but we
2272 * still need to lock the ring to prevent racing with polled issue
2273 * that got punted to a workqueue.
2274 */
2275 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002276 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002277 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002278 * Don't enter poll loop if we already have events pending.
2279 * If we do, we can potentially be spinning for commands that
2280 * already triggered a CQE (eg in error).
2281 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002282 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002283 break;
2284
2285 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002286 * If a submit got punted to a workqueue, we can have the
2287 * application entering polling for a command before it gets
2288 * issued. That app will hold the uring_lock for the duration
2289 * of the poll right here, so we need to take a breather every
2290 * now and then to ensure that the issue has a chance to add
2291 * the poll to the issued list. Otherwise we can spin here
2292 * forever, while the workqueue is stuck trying to acquire the
2293 * very same mutex.
2294 */
2295 if (!(++iters & 7)) {
2296 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002297 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002298 mutex_lock(&ctx->uring_lock);
2299 }
2300
Pavel Begunkov7668b922020-07-07 16:36:21 +03002301 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002302 if (ret <= 0)
2303 break;
2304 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002305 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002306
Jens Axboe500f9fb2019-08-19 12:15:59 -06002307 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002308 return ret;
2309}
2310
Jens Axboe491381ce2019-10-17 09:20:46 -06002311static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312{
Jens Axboe491381ce2019-10-17 09:20:46 -06002313 /*
2314 * Tell lockdep we inherited freeze protection from submission
2315 * thread.
2316 */
2317 if (req->flags & REQ_F_ISREG) {
2318 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319
Jens Axboe491381ce2019-10-17 09:20:46 -06002320 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002322 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323}
2324
Jens Axboea1d7c392020-06-22 11:09:46 -06002325static void io_complete_rw_common(struct kiocb *kiocb, long res,
2326 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327{
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002329 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330
Jens Axboe491381ce2019-10-17 09:20:46 -06002331 if (kiocb->ki_flags & IOCB_WRITE)
2332 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002334 if (res != req->result)
2335 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002336 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002337 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002338 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002339}
2340
Jens Axboeb63534c2020-06-04 11:28:00 -06002341#ifdef CONFIG_BLOCK
2342static bool io_resubmit_prep(struct io_kiocb *req, int error)
2343{
2344 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2345 ssize_t ret = -ECANCELED;
2346 struct iov_iter iter;
2347 int rw;
2348
2349 if (error) {
2350 ret = error;
2351 goto end_req;
2352 }
2353
2354 switch (req->opcode) {
2355 case IORING_OP_READV:
2356 case IORING_OP_READ_FIXED:
2357 case IORING_OP_READ:
2358 rw = READ;
2359 break;
2360 case IORING_OP_WRITEV:
2361 case IORING_OP_WRITE_FIXED:
2362 case IORING_OP_WRITE:
2363 rw = WRITE;
2364 break;
2365 default:
2366 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2367 req->opcode);
2368 goto end_req;
2369 }
2370
Jens Axboe8f3d7492020-09-14 09:28:14 -06002371 if (!req->io) {
2372 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2373 if (ret < 0)
2374 goto end_req;
2375 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2376 if (!ret)
2377 return true;
2378 kfree(iovec);
2379 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002380 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002381 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002382end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002383 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002384 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002385 return false;
2386}
Jens Axboeb63534c2020-06-04 11:28:00 -06002387#endif
2388
2389static bool io_rw_reissue(struct io_kiocb *req, long res)
2390{
2391#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002392 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002393 int ret;
2394
Jens Axboe355afae2020-09-02 09:30:31 -06002395 if (!S_ISBLK(mode) && !S_ISREG(mode))
2396 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002397 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2398 return false;
2399
Jens Axboefdee9462020-08-27 16:46:24 -06002400 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002401
Jens Axboefdee9462020-08-27 16:46:24 -06002402 if (io_resubmit_prep(req, ret)) {
2403 refcount_inc(&req->refs);
2404 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002405 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002406 }
2407
Jens Axboeb63534c2020-06-04 11:28:00 -06002408#endif
2409 return false;
2410}
2411
Jens Axboea1d7c392020-06-22 11:09:46 -06002412static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2413 struct io_comp_state *cs)
2414{
2415 if (!io_rw_reissue(req, res))
2416 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002417}
2418
2419static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2420{
Jens Axboe9adbd452019-12-20 08:45:55 -07002421 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002422
Jens Axboea1d7c392020-06-22 11:09:46 -06002423 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002424}
2425
Jens Axboedef596e2019-01-09 08:59:42 -07002426static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2427{
Jens Axboe9adbd452019-12-20 08:45:55 -07002428 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002429
Jens Axboe491381ce2019-10-17 09:20:46 -06002430 if (kiocb->ki_flags & IOCB_WRITE)
2431 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002432
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002433 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002434 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002435
2436 WRITE_ONCE(req->result, res);
2437 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002438 smp_wmb();
2439 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002440}
2441
2442/*
2443 * After the iocb has been issued, it's safe to be found on the poll list.
2444 * Adding the kiocb to the list AFTER submission ensures that we don't
2445 * find it from a io_iopoll_getevents() thread before the issuer is done
2446 * accessing the kiocb cookie.
2447 */
2448static void io_iopoll_req_issued(struct io_kiocb *req)
2449{
2450 struct io_ring_ctx *ctx = req->ctx;
2451
2452 /*
2453 * Track whether we have multiple files in our lists. This will impact
2454 * how we do polling eventually, not spinning if we're on potentially
2455 * different devices.
2456 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002457 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002458 ctx->poll_multi_file = false;
2459 } else if (!ctx->poll_multi_file) {
2460 struct io_kiocb *list_req;
2461
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002462 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002463 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002464 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002465 ctx->poll_multi_file = true;
2466 }
2467
2468 /*
2469 * For fast devices, IO may have already completed. If it has, add
2470 * it to the front so we find it first.
2471 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002472 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002473 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002474 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002475 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002476
Jens Axboe534ca6d2020-09-02 13:52:19 -06002477 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2478 wq_has_sleeper(&ctx->sq_data->wait))
2479 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002480}
2481
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002482static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002483{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002484 if (state->has_refs)
2485 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002486 state->file = NULL;
2487}
2488
2489static inline void io_state_file_put(struct io_submit_state *state)
2490{
2491 if (state->file)
2492 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002493}
2494
2495/*
2496 * Get as many references to a file as we have IOs left in this submission,
2497 * assuming most submissions are for one file, or at least that each file
2498 * has more than one submission.
2499 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002500static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002501{
2502 if (!state)
2503 return fget(fd);
2504
2505 if (state->file) {
2506 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002507 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002508 state->ios_left--;
2509 return state->file;
2510 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002511 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002512 }
2513 state->file = fget_many(fd, state->ios_left);
2514 if (!state->file)
2515 return NULL;
2516
2517 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002518 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002519 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002520 return state->file;
2521}
2522
Jens Axboe4503b762020-06-01 10:00:27 -06002523static bool io_bdev_nowait(struct block_device *bdev)
2524{
2525#ifdef CONFIG_BLOCK
2526 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2527#else
2528 return true;
2529#endif
2530}
2531
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532/*
2533 * If we tracked the file through the SCM inflight mechanism, we could support
2534 * any file. For now, just ensure that anything potentially problematic is done
2535 * inline.
2536 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002537static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538{
2539 umode_t mode = file_inode(file)->i_mode;
2540
Jens Axboe4503b762020-06-01 10:00:27 -06002541 if (S_ISBLK(mode)) {
2542 if (io_bdev_nowait(file->f_inode->i_bdev))
2543 return true;
2544 return false;
2545 }
2546 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002548 if (S_ISREG(mode)) {
2549 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2550 file->f_op != &io_uring_fops)
2551 return true;
2552 return false;
2553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554
Jens Axboec5b85622020-06-09 19:23:05 -06002555 /* any ->read/write should understand O_NONBLOCK */
2556 if (file->f_flags & O_NONBLOCK)
2557 return true;
2558
Jens Axboeaf197f52020-04-28 13:15:06 -06002559 if (!(file->f_mode & FMODE_NOWAIT))
2560 return false;
2561
2562 if (rw == READ)
2563 return file->f_op->read_iter != NULL;
2564
2565 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566}
2567
Jens Axboe3529d8c2019-12-19 18:24:38 -07002568static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2569 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002570{
Jens Axboedef596e2019-01-09 08:59:42 -07002571 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002572 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002573 unsigned ioprio;
2574 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575
Jens Axboe491381ce2019-10-17 09:20:46 -06002576 if (S_ISREG(file_inode(req->file)->i_mode))
2577 req->flags |= REQ_F_ISREG;
2578
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002580 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2581 req->flags |= REQ_F_CUR_POS;
2582 kiocb->ki_pos = req->file->f_pos;
2583 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002585 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2586 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2587 if (unlikely(ret))
2588 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002589
2590 ioprio = READ_ONCE(sqe->ioprio);
2591 if (ioprio) {
2592 ret = ioprio_check_cap(ioprio);
2593 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002594 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002595
2596 kiocb->ki_ioprio = ioprio;
2597 } else
2598 kiocb->ki_ioprio = get_current_ioprio();
2599
Stefan Bühler8449eed2019-04-27 20:34:19 +02002600 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002601 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002602 req->flags |= REQ_F_NOWAIT;
2603
2604 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002606
Jens Axboedef596e2019-01-09 08:59:42 -07002607 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002608 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2609 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002610 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611
Jens Axboedef596e2019-01-09 08:59:42 -07002612 kiocb->ki_flags |= IOCB_HIPRI;
2613 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002614 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002615 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002616 if (kiocb->ki_flags & IOCB_HIPRI)
2617 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002618 kiocb->ki_complete = io_complete_rw;
2619 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002620
Jens Axboe3529d8c2019-12-19 18:24:38 -07002621 req->rw.addr = READ_ONCE(sqe->addr);
2622 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002623 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625}
2626
2627static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2628{
2629 switch (ret) {
2630 case -EIOCBQUEUED:
2631 break;
2632 case -ERESTARTSYS:
2633 case -ERESTARTNOINTR:
2634 case -ERESTARTNOHAND:
2635 case -ERESTART_RESTARTBLOCK:
2636 /*
2637 * We can't just restart the syscall, since previously
2638 * submitted sqes may already be in progress. Just fail this
2639 * IO with EINTR.
2640 */
2641 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002642 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 default:
2644 kiocb->ki_complete(kiocb, ret, 0);
2645 }
2646}
2647
Jens Axboea1d7c392020-06-22 11:09:46 -06002648static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2649 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002650{
Jens Axboeba042912019-12-25 16:33:42 -07002651 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2652
Jens Axboe227c0c92020-08-13 11:51:40 -06002653 /* add previously done IO, if any */
2654 if (req->io && req->io->rw.bytes_done > 0) {
2655 if (ret < 0)
2656 ret = req->io->rw.bytes_done;
2657 else
2658 ret += req->io->rw.bytes_done;
2659 }
2660
Jens Axboeba042912019-12-25 16:33:42 -07002661 if (req->flags & REQ_F_CUR_POS)
2662 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002663 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002664 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002665 else
2666 io_rw_done(kiocb, ret);
2667}
2668
Jens Axboe9adbd452019-12-20 08:45:55 -07002669static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002670 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002671{
Jens Axboe9adbd452019-12-20 08:45:55 -07002672 struct io_ring_ctx *ctx = req->ctx;
2673 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002674 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002675 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002676 size_t offset;
2677 u64 buf_addr;
2678
2679 /* attempt to use fixed buffers without having provided iovecs */
2680 if (unlikely(!ctx->user_bufs))
2681 return -EFAULT;
2682
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002683 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002684 if (unlikely(buf_index >= ctx->nr_user_bufs))
2685 return -EFAULT;
2686
2687 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2688 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002689 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002690
2691 /* overflow */
2692 if (buf_addr + len < buf_addr)
2693 return -EFAULT;
2694 /* not inside the mapped region */
2695 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2696 return -EFAULT;
2697
2698 /*
2699 * May not be a start of buffer, set size appropriately
2700 * and advance us to the beginning.
2701 */
2702 offset = buf_addr - imu->ubuf;
2703 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002704
2705 if (offset) {
2706 /*
2707 * Don't use iov_iter_advance() here, as it's really slow for
2708 * using the latter parts of a big fixed buffer - it iterates
2709 * over each segment manually. We can cheat a bit here, because
2710 * we know that:
2711 *
2712 * 1) it's a BVEC iter, we set it up
2713 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2714 * first and last bvec
2715 *
2716 * So just find our index, and adjust the iterator afterwards.
2717 * If the offset is within the first bvec (or the whole first
2718 * bvec, just use iov_iter_advance(). This makes it easier
2719 * since we can just skip the first segment, which may not
2720 * be PAGE_SIZE aligned.
2721 */
2722 const struct bio_vec *bvec = imu->bvec;
2723
2724 if (offset <= bvec->bv_len) {
2725 iov_iter_advance(iter, offset);
2726 } else {
2727 unsigned long seg_skip;
2728
2729 /* skip first vec */
2730 offset -= bvec->bv_len;
2731 seg_skip = 1 + (offset >> PAGE_SHIFT);
2732
2733 iter->bvec = bvec + seg_skip;
2734 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002735 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002736 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002737 }
2738 }
2739
Jens Axboe5e559562019-11-13 16:12:46 -07002740 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002741}
2742
Jens Axboebcda7ba2020-02-23 16:42:51 -07002743static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2744{
2745 if (needs_lock)
2746 mutex_unlock(&ctx->uring_lock);
2747}
2748
2749static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2750{
2751 /*
2752 * "Normal" inline submissions always hold the uring_lock, since we
2753 * grab it from the system call. Same is true for the SQPOLL offload.
2754 * The only exception is when we've detached the request and issue it
2755 * from an async worker thread, grab the lock for that case.
2756 */
2757 if (needs_lock)
2758 mutex_lock(&ctx->uring_lock);
2759}
2760
2761static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2762 int bgid, struct io_buffer *kbuf,
2763 bool needs_lock)
2764{
2765 struct io_buffer *head;
2766
2767 if (req->flags & REQ_F_BUFFER_SELECTED)
2768 return kbuf;
2769
2770 io_ring_submit_lock(req->ctx, needs_lock);
2771
2772 lockdep_assert_held(&req->ctx->uring_lock);
2773
2774 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2775 if (head) {
2776 if (!list_empty(&head->list)) {
2777 kbuf = list_last_entry(&head->list, struct io_buffer,
2778 list);
2779 list_del(&kbuf->list);
2780 } else {
2781 kbuf = head;
2782 idr_remove(&req->ctx->io_buffer_idr, bgid);
2783 }
2784 if (*len > kbuf->len)
2785 *len = kbuf->len;
2786 } else {
2787 kbuf = ERR_PTR(-ENOBUFS);
2788 }
2789
2790 io_ring_submit_unlock(req->ctx, needs_lock);
2791
2792 return kbuf;
2793}
2794
Jens Axboe4d954c22020-02-27 07:31:19 -07002795static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2796 bool needs_lock)
2797{
2798 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002799 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002800
2801 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002802 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002803 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2804 if (IS_ERR(kbuf))
2805 return kbuf;
2806 req->rw.addr = (u64) (unsigned long) kbuf;
2807 req->flags |= REQ_F_BUFFER_SELECTED;
2808 return u64_to_user_ptr(kbuf->addr);
2809}
2810
2811#ifdef CONFIG_COMPAT
2812static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2813 bool needs_lock)
2814{
2815 struct compat_iovec __user *uiov;
2816 compat_ssize_t clen;
2817 void __user *buf;
2818 ssize_t len;
2819
2820 uiov = u64_to_user_ptr(req->rw.addr);
2821 if (!access_ok(uiov, sizeof(*uiov)))
2822 return -EFAULT;
2823 if (__get_user(clen, &uiov->iov_len))
2824 return -EFAULT;
2825 if (clen < 0)
2826 return -EINVAL;
2827
2828 len = clen;
2829 buf = io_rw_buffer_select(req, &len, needs_lock);
2830 if (IS_ERR(buf))
2831 return PTR_ERR(buf);
2832 iov[0].iov_base = buf;
2833 iov[0].iov_len = (compat_size_t) len;
2834 return 0;
2835}
2836#endif
2837
2838static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2839 bool needs_lock)
2840{
2841 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2842 void __user *buf;
2843 ssize_t len;
2844
2845 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2846 return -EFAULT;
2847
2848 len = iov[0].iov_len;
2849 if (len < 0)
2850 return -EINVAL;
2851 buf = io_rw_buffer_select(req, &len, needs_lock);
2852 if (IS_ERR(buf))
2853 return PTR_ERR(buf);
2854 iov[0].iov_base = buf;
2855 iov[0].iov_len = len;
2856 return 0;
2857}
2858
2859static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2860 bool needs_lock)
2861{
Jens Axboedddb3e22020-06-04 11:27:01 -06002862 if (req->flags & REQ_F_BUFFER_SELECTED) {
2863 struct io_buffer *kbuf;
2864
2865 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2866 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2867 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002868 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002869 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002870 if (!req->rw.len)
2871 return 0;
2872 else if (req->rw.len > 1)
2873 return -EINVAL;
2874
2875#ifdef CONFIG_COMPAT
2876 if (req->ctx->compat)
2877 return io_compat_import(req, iov, needs_lock);
2878#endif
2879
2880 return __io_iov_buffer_select(req, iov, needs_lock);
2881}
2882
Jens Axboe8452fd02020-08-18 13:58:33 -07002883static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2884 struct iovec **iovec, struct iov_iter *iter,
2885 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886{
Jens Axboe9adbd452019-12-20 08:45:55 -07002887 void __user *buf = u64_to_user_ptr(req->rw.addr);
2888 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002889 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002890 u8 opcode;
2891
Jens Axboed625c6e2019-12-17 19:53:05 -07002892 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002893 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002894 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002895 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002896 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002897
Jens Axboebcda7ba2020-02-23 16:42:51 -07002898 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002899 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002900 return -EINVAL;
2901
Jens Axboe3a6820f2019-12-22 15:19:35 -07002902 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002903 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002904 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002905 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002906 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002907 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002908 }
2909
Jens Axboe3a6820f2019-12-22 15:19:35 -07002910 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2911 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002912 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002913 }
2914
Jens Axboe4d954c22020-02-27 07:31:19 -07002915 if (req->flags & REQ_F_BUFFER_SELECT) {
2916 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002917 if (!ret) {
2918 ret = (*iovec)->iov_len;
2919 iov_iter_init(iter, rw, *iovec, 1, ret);
2920 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002921 *iovec = NULL;
2922 return ret;
2923 }
2924
Jens Axboe2b188cc2019-01-07 10:46:33 -07002925#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002926 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002927 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2928 iovec, iter);
2929#endif
2930
2931 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2932}
2933
Jens Axboe8452fd02020-08-18 13:58:33 -07002934static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2935 struct iovec **iovec, struct iov_iter *iter,
2936 bool needs_lock)
2937{
2938 if (!req->io)
2939 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2940 *iovec = NULL;
2941 return iov_iter_count(&req->io->rw.iter);
2942}
2943
Jens Axboe0fef9482020-08-26 10:36:20 -06002944static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2945{
2946 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2947}
2948
Jens Axboe32960612019-09-23 11:05:34 -06002949/*
2950 * For files that don't have ->read_iter() and ->write_iter(), handle them
2951 * by looping over ->read() or ->write() manually.
2952 */
2953static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2954 struct iov_iter *iter)
2955{
2956 ssize_t ret = 0;
2957
2958 /*
2959 * Don't support polled IO through this interface, and we can't
2960 * support non-blocking either. For the latter, this just causes
2961 * the kiocb to be handled from an async context.
2962 */
2963 if (kiocb->ki_flags & IOCB_HIPRI)
2964 return -EOPNOTSUPP;
2965 if (kiocb->ki_flags & IOCB_NOWAIT)
2966 return -EAGAIN;
2967
2968 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002969 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002970 ssize_t nr;
2971
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002972 if (!iov_iter_is_bvec(iter)) {
2973 iovec = iov_iter_iovec(iter);
2974 } else {
2975 /* fixed buffers import bvec */
2976 iovec.iov_base = kmap(iter->bvec->bv_page)
2977 + iter->iov_offset;
2978 iovec.iov_len = min(iter->count,
2979 iter->bvec->bv_len - iter->iov_offset);
2980 }
2981
Jens Axboe32960612019-09-23 11:05:34 -06002982 if (rw == READ) {
2983 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002984 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002985 } else {
2986 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002987 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002988 }
2989
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002990 if (iov_iter_is_bvec(iter))
2991 kunmap(iter->bvec->bv_page);
2992
Jens Axboe32960612019-09-23 11:05:34 -06002993 if (nr < 0) {
2994 if (!ret)
2995 ret = nr;
2996 break;
2997 }
2998 ret += nr;
2999 if (nr != iovec.iov_len)
3000 break;
3001 iov_iter_advance(iter, nr);
3002 }
3003
3004 return ret;
3005}
3006
Jens Axboeff6165b2020-08-13 09:47:43 -06003007static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3008 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003009{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003010 struct io_async_rw *rw = &req->io->rw;
3011
Jens Axboeff6165b2020-08-13 09:47:43 -06003012 memcpy(&rw->iter, iter, sizeof(*iter));
3013 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06003014 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003015 /* can only be fixed buffers, no need to do anything */
3016 if (iter->type == ITER_BVEC)
3017 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003018 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003019 unsigned iov_off = 0;
3020
3021 rw->iter.iov = rw->fast_iov;
3022 if (iter->iov != fast_iov) {
3023 iov_off = iter->iov - fast_iov;
3024 rw->iter.iov += iov_off;
3025 }
3026 if (rw->fast_iov != fast_iov)
3027 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003028 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003029 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06003030 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003031 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003032 }
3033}
3034
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003035static inline int __io_alloc_async_ctx(struct io_kiocb *req)
3036{
3037 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
3038 return req->io == NULL;
3039}
3040
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003041static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003042{
Jens Axboed3656342019-12-18 09:50:26 -07003043 if (!io_op_defs[req->opcode].async_ctx)
3044 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003045
3046 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003047}
3048
Jens Axboeff6165b2020-08-13 09:47:43 -06003049static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3050 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003051 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003052{
Jens Axboe227c0c92020-08-13 11:51:40 -06003053 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003054 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003055 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003056 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003057 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003058
Jens Axboeff6165b2020-08-13 09:47:43 -06003059 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003060 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003061 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003062}
3063
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003064static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3065 bool force_nonblock)
3066{
Jens Axboeff6165b2020-08-13 09:47:43 -06003067 struct io_async_rw *iorw = &req->io->rw;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003068 struct iovec *iov = iorw->fast_iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003069 ssize_t ret;
3070
Jens Axboec183edf2020-09-04 22:36:52 -06003071 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003072 if (unlikely(ret < 0))
3073 return ret;
3074
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003075 io_req_map_rw(req, iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003076 return 0;
3077}
3078
Jens Axboe3529d8c2019-12-19 18:24:38 -07003079static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3080 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003081{
3082 ssize_t ret;
3083
Jens Axboe3529d8c2019-12-19 18:24:38 -07003084 ret = io_prep_rw(req, sqe, force_nonblock);
3085 if (ret)
3086 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003087
Jens Axboe3529d8c2019-12-19 18:24:38 -07003088 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3089 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003090
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003091 /* either don't need iovec imported or already have it */
3092 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003093 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003094 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003095}
3096
Jens Axboec1dd91d2020-08-03 16:43:59 -06003097/*
3098 * This is our waitqueue callback handler, registered through lock_page_async()
3099 * when we initially tried to do the IO with the iocb armed our waitqueue.
3100 * This gets called when the page is unlocked, and we generally expect that to
3101 * happen when the page IO is completed and the page is now uptodate. This will
3102 * queue a task_work based retry of the operation, attempting to copy the data
3103 * again. If the latter fails because the page was NOT uptodate, then we will
3104 * do a thread based blocking retry of the operation. That's the unexpected
3105 * slow path.
3106 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003107static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3108 int sync, void *arg)
3109{
3110 struct wait_page_queue *wpq;
3111 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003112 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003113 int ret;
3114
3115 wpq = container_of(wait, struct wait_page_queue, wait);
3116
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003117 if (!wake_page_match(wpq, key))
3118 return 0;
3119
Hao Xuc8d317a2020-09-29 20:00:45 +08003120 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003121 list_del_init(&wait->entry);
3122
Pavel Begunkove7375122020-07-12 20:42:04 +03003123 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003124 percpu_ref_get(&req->ctx->refs);
3125
Jens Axboebcf5a062020-05-22 09:24:42 -06003126 /* submit ref gets dropped, acquire a new one */
3127 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003128 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003129 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003130 struct task_struct *tsk;
3131
Jens Axboebcf5a062020-05-22 09:24:42 -06003132 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003133 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003134 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003135 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003136 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003137 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003138 return 1;
3139}
3140
Jens Axboec1dd91d2020-08-03 16:43:59 -06003141/*
3142 * This controls whether a given IO request should be armed for async page
3143 * based retry. If we return false here, the request is handed to the async
3144 * worker threads for retry. If we're doing buffered reads on a regular file,
3145 * we prepare a private wait_page_queue entry and retry the operation. This
3146 * will either succeed because the page is now uptodate and unlocked, or it
3147 * will register a callback when the page is unlocked at IO completion. Through
3148 * that callback, io_uring uses task_work to setup a retry of the operation.
3149 * That retry will attempt the buffered read again. The retry will generally
3150 * succeed, or in rare cases where it fails, we then fall back to using the
3151 * async worker threads for a blocking retry.
3152 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003153static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003154{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003155 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003156 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003157
3158 /* never retry for NOWAIT, we just complete with -EAGAIN */
3159 if (req->flags & REQ_F_NOWAIT)
3160 return false;
3161
Jens Axboe227c0c92020-08-13 11:51:40 -06003162 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003163 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003164 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003165
Jens Axboebcf5a062020-05-22 09:24:42 -06003166 /*
3167 * just use poll if we can, and don't attempt if the fs doesn't
3168 * support callback based unlocks
3169 */
3170 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3171 return false;
3172
Jens Axboe3b2a4432020-08-16 10:58:43 -07003173 wait->wait.func = io_async_buf_func;
3174 wait->wait.private = req;
3175 wait->wait.flags = 0;
3176 INIT_LIST_HEAD(&wait->wait.entry);
3177 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003178 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003179 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003180 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003181}
3182
3183static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3184{
3185 if (req->file->f_op->read_iter)
3186 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003187 else if (req->file->f_op->read)
3188 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3189 else
3190 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003191}
3192
Jens Axboea1d7c392020-06-22 11:09:46 -06003193static int io_read(struct io_kiocb *req, bool force_nonblock,
3194 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003195{
3196 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003197 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003198 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003199 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003200 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003201 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003202
Jens Axboeff6165b2020-08-13 09:47:43 -06003203 if (req->io)
3204 iter = &req->io->rw.iter;
3205
3206 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003207 if (ret < 0)
3208 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003209 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003210 io_size = ret;
3211 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003212 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003213
Jens Axboefd6c2e42019-12-18 12:19:41 -07003214 /* Ensure we clear previously set non-block flag */
3215 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003216 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003217
Pavel Begunkov24c74672020-06-21 13:09:51 +03003218 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003219 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3220 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003221 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003222
Jens Axboe0fef9482020-08-26 10:36:20 -06003223 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003224 if (unlikely(ret))
3225 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003226
Jens Axboe227c0c92020-08-13 11:51:40 -06003227 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003228
Jens Axboe227c0c92020-08-13 11:51:40 -06003229 if (!ret) {
3230 goto done;
3231 } else if (ret == -EIOCBQUEUED) {
3232 ret = 0;
3233 goto out_free;
3234 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003235 /* IOPOLL retry should happen for io-wq threads */
3236 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003237 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003238 /* no retry on NONBLOCK marked file */
3239 if (req->file->f_flags & O_NONBLOCK)
3240 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003241 /* some cases will consume bytes even on error returns */
3242 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003243 ret = 0;
3244 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003245 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003246 /* make sure -ERESTARTSYS -> -EINTR is done */
3247 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003248 }
3249
3250 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003251 if (!iov_iter_count(iter) || !force_nonblock ||
3252 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003253 goto done;
3254
3255 io_size -= ret;
3256copy_iov:
3257 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3258 if (ret2) {
3259 ret = ret2;
3260 goto out_free;
3261 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003262 if (no_async)
3263 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003264 /* it's copied and will be cleaned with ->io */
3265 iovec = NULL;
3266 /* now use our persistent iterator, if we aren't already */
3267 iter = &req->io->rw.iter;
3268retry:
3269 req->io->rw.bytes_done += ret;
3270 /* if we can retry, do so with the callbacks armed */
3271 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003272 kiocb->ki_flags &= ~IOCB_WAITQ;
3273 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003274 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003275
3276 /*
3277 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3278 * get -EIOCBQUEUED, then we'll get a notification when the desired
3279 * page gets unlocked. We can also get a partial read here, and if we
3280 * do, then just retry at the new offset.
3281 */
3282 ret = io_iter_do_read(req, iter);
3283 if (ret == -EIOCBQUEUED) {
3284 ret = 0;
3285 goto out_free;
3286 } else if (ret > 0 && ret < io_size) {
3287 /* we got some bytes, but not all. retry. */
3288 goto retry;
3289 }
3290done:
3291 kiocb_done(kiocb, ret, cs);
3292 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003293out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003294 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003295 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003296 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003297 return ret;
3298}
3299
Jens Axboe3529d8c2019-12-19 18:24:38 -07003300static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3301 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003302{
3303 ssize_t ret;
3304
Jens Axboe3529d8c2019-12-19 18:24:38 -07003305 ret = io_prep_rw(req, sqe, force_nonblock);
3306 if (ret)
3307 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003308
Jens Axboe3529d8c2019-12-19 18:24:38 -07003309 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3310 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003311
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003312 /* either don't need iovec imported or already have it */
3313 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003314 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003315 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003316}
3317
Jens Axboea1d7c392020-06-22 11:09:46 -06003318static int io_write(struct io_kiocb *req, bool force_nonblock,
3319 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003320{
3321 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003322 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003323 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003324 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003325 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003326
Jens Axboeff6165b2020-08-13 09:47:43 -06003327 if (req->io)
3328 iter = &req->io->rw.iter;
3329
3330 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003331 if (ret < 0)
3332 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003333 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003334 io_size = ret;
3335 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003336
Jens Axboefd6c2e42019-12-18 12:19:41 -07003337 /* Ensure we clear previously set non-block flag */
3338 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003339 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003340
Pavel Begunkov24c74672020-06-21 13:09:51 +03003341 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003342 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003343 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003344
Jens Axboe10d59342019-12-09 20:16:22 -07003345 /* file path doesn't support NOWAIT for non-direct_IO */
3346 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3347 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003348 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003349
Jens Axboe0fef9482020-08-26 10:36:20 -06003350 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003351 if (unlikely(ret))
3352 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003353
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003354 /*
3355 * Open-code file_start_write here to grab freeze protection,
3356 * which will be released by another thread in
3357 * io_complete_rw(). Fool lockdep by telling it the lock got
3358 * released so that it doesn't complain about the held lock when
3359 * we return to userspace.
3360 */
3361 if (req->flags & REQ_F_ISREG) {
3362 __sb_start_write(file_inode(req->file)->i_sb,
3363 SB_FREEZE_WRITE, true);
3364 __sb_writers_release(file_inode(req->file)->i_sb,
3365 SB_FREEZE_WRITE);
3366 }
3367 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003368
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003369 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003370 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003371 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003372 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003373 else
3374 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003375
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003376 /*
3377 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3378 * retry them without IOCB_NOWAIT.
3379 */
3380 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3381 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003382 /* no retry on NONBLOCK marked file */
3383 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3384 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003385 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003386 /* IOPOLL retry should happen for io-wq threads */
3387 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3388 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003389done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003390 kiocb_done(kiocb, ret2, cs);
3391 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003392copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003393 /* some cases will consume bytes even on error returns */
3394 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003395 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003396 if (!ret)
3397 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398 }
Jens Axboe31b51512019-01-18 22:56:34 -07003399out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003400 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003401 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003402 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003403 return ret;
3404}
3405
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003406static int __io_splice_prep(struct io_kiocb *req,
3407 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003408{
3409 struct io_splice* sp = &req->splice;
3410 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3411 int ret;
3412
3413 if (req->flags & REQ_F_NEED_CLEANUP)
3414 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003415 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3416 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003417
3418 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003419 sp->len = READ_ONCE(sqe->len);
3420 sp->flags = READ_ONCE(sqe->splice_flags);
3421
3422 if (unlikely(sp->flags & ~valid_flags))
3423 return -EINVAL;
3424
3425 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3426 (sp->flags & SPLICE_F_FD_IN_FIXED));
3427 if (ret)
3428 return ret;
3429 req->flags |= REQ_F_NEED_CLEANUP;
3430
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003431 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3432 /*
3433 * Splice operation will be punted aync, and here need to
3434 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3435 */
3436 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003437 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003438 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003439
3440 return 0;
3441}
3442
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003443static int io_tee_prep(struct io_kiocb *req,
3444 const struct io_uring_sqe *sqe)
3445{
3446 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3447 return -EINVAL;
3448 return __io_splice_prep(req, sqe);
3449}
3450
3451static int io_tee(struct io_kiocb *req, bool force_nonblock)
3452{
3453 struct io_splice *sp = &req->splice;
3454 struct file *in = sp->file_in;
3455 struct file *out = sp->file_out;
3456 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3457 long ret = 0;
3458
3459 if (force_nonblock)
3460 return -EAGAIN;
3461 if (sp->len)
3462 ret = do_tee(in, out, sp->len, flags);
3463
3464 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3465 req->flags &= ~REQ_F_NEED_CLEANUP;
3466
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003467 if (ret != sp->len)
3468 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003469 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003470 return 0;
3471}
3472
3473static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3474{
3475 struct io_splice* sp = &req->splice;
3476
3477 sp->off_in = READ_ONCE(sqe->splice_off_in);
3478 sp->off_out = READ_ONCE(sqe->off);
3479 return __io_splice_prep(req, sqe);
3480}
3481
Pavel Begunkov014db002020-03-03 21:33:12 +03003482static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003483{
3484 struct io_splice *sp = &req->splice;
3485 struct file *in = sp->file_in;
3486 struct file *out = sp->file_out;
3487 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3488 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003489 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003490
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003491 if (force_nonblock)
3492 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003493
3494 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3495 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003496
Jens Axboe948a7742020-05-17 14:21:38 -06003497 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003498 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003499
3500 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3501 req->flags &= ~REQ_F_NEED_CLEANUP;
3502
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003503 if (ret != sp->len)
3504 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003505 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003506 return 0;
3507}
3508
Jens Axboe2b188cc2019-01-07 10:46:33 -07003509/*
3510 * IORING_OP_NOP just posts a completion event, nothing else.
3511 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003512static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513{
3514 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003515
Jens Axboedef596e2019-01-09 08:59:42 -07003516 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3517 return -EINVAL;
3518
Jens Axboe229a7b62020-06-22 10:13:11 -06003519 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003520 return 0;
3521}
3522
Jens Axboe3529d8c2019-12-19 18:24:38 -07003523static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003524{
Jens Axboe6b063142019-01-10 22:13:58 -07003525 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003526
Jens Axboe09bb8392019-03-13 12:39:28 -06003527 if (!req->file)
3528 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003529
Jens Axboe6b063142019-01-10 22:13:58 -07003530 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003531 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003532 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003533 return -EINVAL;
3534
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003535 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3536 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3537 return -EINVAL;
3538
3539 req->sync.off = READ_ONCE(sqe->off);
3540 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003541 return 0;
3542}
3543
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003544static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003545{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003546 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003547 int ret;
3548
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003549 /* fsync always requires a blocking context */
3550 if (force_nonblock)
3551 return -EAGAIN;
3552
Jens Axboe9adbd452019-12-20 08:45:55 -07003553 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003554 end > 0 ? end : LLONG_MAX,
3555 req->sync.flags & IORING_FSYNC_DATASYNC);
3556 if (ret < 0)
3557 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003558 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003559 return 0;
3560}
3561
Jens Axboed63d1b52019-12-10 10:38:56 -07003562static int io_fallocate_prep(struct io_kiocb *req,
3563 const struct io_uring_sqe *sqe)
3564{
3565 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3566 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003567 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3568 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003569
3570 req->sync.off = READ_ONCE(sqe->off);
3571 req->sync.len = READ_ONCE(sqe->addr);
3572 req->sync.mode = READ_ONCE(sqe->len);
3573 return 0;
3574}
3575
Pavel Begunkov014db002020-03-03 21:33:12 +03003576static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003577{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003578 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003579
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003580 /* fallocate always requiring blocking context */
3581 if (force_nonblock)
3582 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003583 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3584 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003585 if (ret < 0)
3586 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003587 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003588 return 0;
3589}
3590
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003591static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003592{
Jens Axboef8748882020-01-08 17:47:02 -07003593 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003594 int ret;
3595
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003596 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003597 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003598 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003599 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003600
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003601 /* open.how should be already initialised */
3602 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003603 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003604
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003605 req->open.dfd = READ_ONCE(sqe->fd);
3606 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003607 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003608 if (IS_ERR(req->open.filename)) {
3609 ret = PTR_ERR(req->open.filename);
3610 req->open.filename = NULL;
3611 return ret;
3612 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003613 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003614 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003615 return 0;
3616}
3617
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003618static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3619{
3620 u64 flags, mode;
3621
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003622 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3623 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003624 if (req->flags & REQ_F_NEED_CLEANUP)
3625 return 0;
3626 mode = READ_ONCE(sqe->len);
3627 flags = READ_ONCE(sqe->open_flags);
3628 req->open.how = build_open_how(flags, mode);
3629 return __io_openat_prep(req, sqe);
3630}
3631
Jens Axboecebdb982020-01-08 17:59:24 -07003632static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3633{
3634 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003635 size_t len;
3636 int ret;
3637
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003638 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3639 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003640 if (req->flags & REQ_F_NEED_CLEANUP)
3641 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003642 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3643 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003644 if (len < OPEN_HOW_SIZE_VER0)
3645 return -EINVAL;
3646
3647 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3648 len);
3649 if (ret)
3650 return ret;
3651
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003652 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003653}
3654
Pavel Begunkov014db002020-03-03 21:33:12 +03003655static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003656{
3657 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003658 struct file *file;
3659 int ret;
3660
Jens Axboef86cd202020-01-29 13:46:44 -07003661 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003662 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003663
Jens Axboecebdb982020-01-08 17:59:24 -07003664 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003665 if (ret)
3666 goto err;
3667
Jens Axboe4022e7a2020-03-19 19:23:18 -06003668 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003669 if (ret < 0)
3670 goto err;
3671
3672 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3673 if (IS_ERR(file)) {
3674 put_unused_fd(ret);
3675 ret = PTR_ERR(file);
3676 } else {
3677 fsnotify_open(file);
3678 fd_install(ret, file);
3679 }
3680err:
3681 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003682 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003683 if (ret < 0)
3684 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003685 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003686 return 0;
3687}
3688
Pavel Begunkov014db002020-03-03 21:33:12 +03003689static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003690{
Pavel Begunkov014db002020-03-03 21:33:12 +03003691 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003692}
3693
Jens Axboe067524e2020-03-02 16:32:28 -07003694static int io_remove_buffers_prep(struct io_kiocb *req,
3695 const struct io_uring_sqe *sqe)
3696{
3697 struct io_provide_buf *p = &req->pbuf;
3698 u64 tmp;
3699
3700 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3701 return -EINVAL;
3702
3703 tmp = READ_ONCE(sqe->fd);
3704 if (!tmp || tmp > USHRT_MAX)
3705 return -EINVAL;
3706
3707 memset(p, 0, sizeof(*p));
3708 p->nbufs = tmp;
3709 p->bgid = READ_ONCE(sqe->buf_group);
3710 return 0;
3711}
3712
3713static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3714 int bgid, unsigned nbufs)
3715{
3716 unsigned i = 0;
3717
3718 /* shouldn't happen */
3719 if (!nbufs)
3720 return 0;
3721
3722 /* the head kbuf is the list itself */
3723 while (!list_empty(&buf->list)) {
3724 struct io_buffer *nxt;
3725
3726 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3727 list_del(&nxt->list);
3728 kfree(nxt);
3729 if (++i == nbufs)
3730 return i;
3731 }
3732 i++;
3733 kfree(buf);
3734 idr_remove(&ctx->io_buffer_idr, bgid);
3735
3736 return i;
3737}
3738
Jens Axboe229a7b62020-06-22 10:13:11 -06003739static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3740 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003741{
3742 struct io_provide_buf *p = &req->pbuf;
3743 struct io_ring_ctx *ctx = req->ctx;
3744 struct io_buffer *head;
3745 int ret = 0;
3746
3747 io_ring_submit_lock(ctx, !force_nonblock);
3748
3749 lockdep_assert_held(&ctx->uring_lock);
3750
3751 ret = -ENOENT;
3752 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3753 if (head)
3754 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3755
3756 io_ring_submit_lock(ctx, !force_nonblock);
3757 if (ret < 0)
3758 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003759 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003760 return 0;
3761}
3762
Jens Axboeddf0322d2020-02-23 16:41:33 -07003763static int io_provide_buffers_prep(struct io_kiocb *req,
3764 const struct io_uring_sqe *sqe)
3765{
3766 struct io_provide_buf *p = &req->pbuf;
3767 u64 tmp;
3768
3769 if (sqe->ioprio || sqe->rw_flags)
3770 return -EINVAL;
3771
3772 tmp = READ_ONCE(sqe->fd);
3773 if (!tmp || tmp > USHRT_MAX)
3774 return -E2BIG;
3775 p->nbufs = tmp;
3776 p->addr = READ_ONCE(sqe->addr);
3777 p->len = READ_ONCE(sqe->len);
3778
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003779 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003780 return -EFAULT;
3781
3782 p->bgid = READ_ONCE(sqe->buf_group);
3783 tmp = READ_ONCE(sqe->off);
3784 if (tmp > USHRT_MAX)
3785 return -E2BIG;
3786 p->bid = tmp;
3787 return 0;
3788}
3789
3790static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3791{
3792 struct io_buffer *buf;
3793 u64 addr = pbuf->addr;
3794 int i, bid = pbuf->bid;
3795
3796 for (i = 0; i < pbuf->nbufs; i++) {
3797 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3798 if (!buf)
3799 break;
3800
3801 buf->addr = addr;
3802 buf->len = pbuf->len;
3803 buf->bid = bid;
3804 addr += pbuf->len;
3805 bid++;
3806 if (!*head) {
3807 INIT_LIST_HEAD(&buf->list);
3808 *head = buf;
3809 } else {
3810 list_add_tail(&buf->list, &(*head)->list);
3811 }
3812 }
3813
3814 return i ? i : -ENOMEM;
3815}
3816
Jens Axboe229a7b62020-06-22 10:13:11 -06003817static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3818 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003819{
3820 struct io_provide_buf *p = &req->pbuf;
3821 struct io_ring_ctx *ctx = req->ctx;
3822 struct io_buffer *head, *list;
3823 int ret = 0;
3824
3825 io_ring_submit_lock(ctx, !force_nonblock);
3826
3827 lockdep_assert_held(&ctx->uring_lock);
3828
3829 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3830
3831 ret = io_add_buffers(p, &head);
3832 if (ret < 0)
3833 goto out;
3834
3835 if (!list) {
3836 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3837 GFP_KERNEL);
3838 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003839 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003840 goto out;
3841 }
3842 }
3843out:
3844 io_ring_submit_unlock(ctx, !force_nonblock);
3845 if (ret < 0)
3846 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003847 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003848 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003849}
3850
Jens Axboe3e4827b2020-01-08 15:18:09 -07003851static int io_epoll_ctl_prep(struct io_kiocb *req,
3852 const struct io_uring_sqe *sqe)
3853{
3854#if defined(CONFIG_EPOLL)
3855 if (sqe->ioprio || sqe->buf_index)
3856 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003857 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003858 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003859
3860 req->epoll.epfd = READ_ONCE(sqe->fd);
3861 req->epoll.op = READ_ONCE(sqe->len);
3862 req->epoll.fd = READ_ONCE(sqe->off);
3863
3864 if (ep_op_has_event(req->epoll.op)) {
3865 struct epoll_event __user *ev;
3866
3867 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3868 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3869 return -EFAULT;
3870 }
3871
3872 return 0;
3873#else
3874 return -EOPNOTSUPP;
3875#endif
3876}
3877
Jens Axboe229a7b62020-06-22 10:13:11 -06003878static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3879 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003880{
3881#if defined(CONFIG_EPOLL)
3882 struct io_epoll *ie = &req->epoll;
3883 int ret;
3884
3885 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3886 if (force_nonblock && ret == -EAGAIN)
3887 return -EAGAIN;
3888
3889 if (ret < 0)
3890 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003891 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003892 return 0;
3893#else
3894 return -EOPNOTSUPP;
3895#endif
3896}
3897
Jens Axboec1ca7572019-12-25 22:18:28 -07003898static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3899{
3900#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3901 if (sqe->ioprio || sqe->buf_index || sqe->off)
3902 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003903 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3904 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003905
3906 req->madvise.addr = READ_ONCE(sqe->addr);
3907 req->madvise.len = READ_ONCE(sqe->len);
3908 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3909 return 0;
3910#else
3911 return -EOPNOTSUPP;
3912#endif
3913}
3914
Pavel Begunkov014db002020-03-03 21:33:12 +03003915static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003916{
3917#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3918 struct io_madvise *ma = &req->madvise;
3919 int ret;
3920
3921 if (force_nonblock)
3922 return -EAGAIN;
3923
3924 ret = do_madvise(ma->addr, ma->len, ma->advice);
3925 if (ret < 0)
3926 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003927 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003928 return 0;
3929#else
3930 return -EOPNOTSUPP;
3931#endif
3932}
3933
Jens Axboe4840e412019-12-25 22:03:45 -07003934static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3935{
3936 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3937 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003938 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3939 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003940
3941 req->fadvise.offset = READ_ONCE(sqe->off);
3942 req->fadvise.len = READ_ONCE(sqe->len);
3943 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3944 return 0;
3945}
3946
Pavel Begunkov014db002020-03-03 21:33:12 +03003947static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003948{
3949 struct io_fadvise *fa = &req->fadvise;
3950 int ret;
3951
Jens Axboe3e694262020-02-01 09:22:49 -07003952 if (force_nonblock) {
3953 switch (fa->advice) {
3954 case POSIX_FADV_NORMAL:
3955 case POSIX_FADV_RANDOM:
3956 case POSIX_FADV_SEQUENTIAL:
3957 break;
3958 default:
3959 return -EAGAIN;
3960 }
3961 }
Jens Axboe4840e412019-12-25 22:03:45 -07003962
3963 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3964 if (ret < 0)
3965 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003966 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003967 return 0;
3968}
3969
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003970static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3971{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003972 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003973 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003974 if (sqe->ioprio || sqe->buf_index)
3975 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003976 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003977 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003978
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003979 req->statx.dfd = READ_ONCE(sqe->fd);
3980 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003981 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003982 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3983 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003984
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003985 return 0;
3986}
3987
Pavel Begunkov014db002020-03-03 21:33:12 +03003988static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003989{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003990 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003991 int ret;
3992
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003993 if (force_nonblock) {
3994 /* only need file table for an actual valid fd */
3995 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3996 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003997 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003998 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003999
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004000 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4001 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004002
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004003 if (ret < 0)
4004 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004005 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004006 return 0;
4007}
4008
Jens Axboeb5dba592019-12-11 14:02:38 -07004009static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4010{
4011 /*
4012 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004013 * leave the 'file' in an undeterminate state, and here need to modify
4014 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07004015 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004016 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07004017 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
4018
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004019 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4020 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004021 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4022 sqe->rw_flags || sqe->buf_index)
4023 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004024 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004025 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004026
4027 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboe0f212202020-09-13 13:09:39 -06004028 if ((req->file && req->file->f_op == &io_uring_fops))
Jens Axboefd2206e2020-06-02 16:40:47 -06004029 return -EBADF;
4030
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004031 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004032 return 0;
4033}
4034
Jens Axboe229a7b62020-06-22 10:13:11 -06004035static int io_close(struct io_kiocb *req, bool force_nonblock,
4036 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07004037{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004038 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07004039 int ret;
4040
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004041 /* might be already done during nonblock submission */
4042 if (!close->put_file) {
4043 ret = __close_fd_get_file(close->fd, &close->put_file);
4044 if (ret < 0)
4045 return (ret == -ENOENT) ? -EBADF : ret;
4046 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004047
4048 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004049 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004050 /* was never set, but play safe */
4051 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004052 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004053 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004054 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004055 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004056
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004057 /* No ->flush() or already async, safely close from here */
4058 ret = filp_close(close->put_file, req->work.files);
4059 if (ret < 0)
4060 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004061 fput(close->put_file);
4062 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004063 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004064 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004065}
4066
Jens Axboe3529d8c2019-12-19 18:24:38 -07004067static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004068{
4069 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004070
4071 if (!req->file)
4072 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004073
4074 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4075 return -EINVAL;
4076 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4077 return -EINVAL;
4078
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004079 req->sync.off = READ_ONCE(sqe->off);
4080 req->sync.len = READ_ONCE(sqe->len);
4081 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004082 return 0;
4083}
4084
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004085static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004086{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004087 int ret;
4088
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004089 /* sync_file_range always requires a blocking context */
4090 if (force_nonblock)
4091 return -EAGAIN;
4092
Jens Axboe9adbd452019-12-20 08:45:55 -07004093 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004094 req->sync.flags);
4095 if (ret < 0)
4096 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004097 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004098 return 0;
4099}
4100
YueHaibing469956e2020-03-04 15:53:52 +08004101#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004102static int io_setup_async_msg(struct io_kiocb *req,
4103 struct io_async_msghdr *kmsg)
4104{
4105 if (req->io)
4106 return -EAGAIN;
4107 if (io_alloc_async_ctx(req)) {
4108 if (kmsg->iov != kmsg->fast_iov)
4109 kfree(kmsg->iov);
4110 return -ENOMEM;
4111 }
4112 req->flags |= REQ_F_NEED_CLEANUP;
4113 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4114 return -EAGAIN;
4115}
4116
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004117static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4118 struct io_async_msghdr *iomsg)
4119{
4120 iomsg->iov = iomsg->fast_iov;
4121 iomsg->msg.msg_name = &iomsg->addr;
4122 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4123 req->sr_msg.msg_flags, &iomsg->iov);
4124}
4125
Jens Axboe3529d8c2019-12-19 18:24:38 -07004126static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004127{
Jens Axboee47293f2019-12-20 08:58:21 -07004128 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004130 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004131
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004132 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4133 return -EINVAL;
4134
Jens Axboee47293f2019-12-20 08:58:21 -07004135 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004136 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004137 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004138
Jens Axboed8768362020-02-27 14:17:49 -07004139#ifdef CONFIG_COMPAT
4140 if (req->ctx->compat)
4141 sr->msg_flags |= MSG_CMSG_COMPAT;
4142#endif
4143
Jens Axboefddafac2020-01-04 20:19:44 -07004144 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004145 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004146 /* iovec is already imported */
4147 if (req->flags & REQ_F_NEED_CLEANUP)
4148 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004149
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004150 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004151 if (!ret)
4152 req->flags |= REQ_F_NEED_CLEANUP;
4153 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004154}
4155
Jens Axboe229a7b62020-06-22 10:13:11 -06004156static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4157 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004158{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004159 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004160 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004161 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004162 int ret;
4163
Jens Axboe03b12302019-12-02 18:50:25 -07004164 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004165 if (unlikely(!sock))
4166 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004167
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004168 if (req->io) {
4169 kmsg = &req->io->msg;
4170 kmsg->msg.msg_name = &req->io->msg.addr;
4171 /* if iov is set, it's allocated already */
4172 if (!kmsg->iov)
4173 kmsg->iov = kmsg->fast_iov;
4174 kmsg->msg.msg_iter.iov = kmsg->iov;
4175 } else {
4176 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004177 if (ret)
4178 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004179 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004180 }
4181
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004182 flags = req->sr_msg.msg_flags;
4183 if (flags & MSG_DONTWAIT)
4184 req->flags |= REQ_F_NOWAIT;
4185 else if (force_nonblock)
4186 flags |= MSG_DONTWAIT;
4187
4188 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4189 if (force_nonblock && ret == -EAGAIN)
4190 return io_setup_async_msg(req, kmsg);
4191 if (ret == -ERESTARTSYS)
4192 ret = -EINTR;
4193
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004194 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004195 kfree(kmsg->iov);
4196 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004197 if (ret < 0)
4198 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004199 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004200 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004201}
4202
Jens Axboe229a7b62020-06-22 10:13:11 -06004203static int io_send(struct io_kiocb *req, bool force_nonblock,
4204 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004205{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004206 struct io_sr_msg *sr = &req->sr_msg;
4207 struct msghdr msg;
4208 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004209 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004210 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004211 int ret;
4212
4213 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004214 if (unlikely(!sock))
4215 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004216
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004217 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4218 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004219 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004220
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004221 msg.msg_name = NULL;
4222 msg.msg_control = NULL;
4223 msg.msg_controllen = 0;
4224 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004225
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004226 flags = req->sr_msg.msg_flags;
4227 if (flags & MSG_DONTWAIT)
4228 req->flags |= REQ_F_NOWAIT;
4229 else if (force_nonblock)
4230 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004231
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004232 msg.msg_flags = flags;
4233 ret = sock_sendmsg(sock, &msg);
4234 if (force_nonblock && ret == -EAGAIN)
4235 return -EAGAIN;
4236 if (ret == -ERESTARTSYS)
4237 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004238
Jens Axboe03b12302019-12-02 18:50:25 -07004239 if (ret < 0)
4240 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004241 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004242 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004243}
4244
Pavel Begunkov1400e692020-07-12 20:41:05 +03004245static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4246 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004247{
4248 struct io_sr_msg *sr = &req->sr_msg;
4249 struct iovec __user *uiov;
4250 size_t iov_len;
4251 int ret;
4252
Pavel Begunkov1400e692020-07-12 20:41:05 +03004253 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4254 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004255 if (ret)
4256 return ret;
4257
4258 if (req->flags & REQ_F_BUFFER_SELECT) {
4259 if (iov_len > 1)
4260 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004261 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004262 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004263 sr->len = iomsg->iov[0].iov_len;
4264 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004265 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004266 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004267 } else {
4268 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004269 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004270 if (ret > 0)
4271 ret = 0;
4272 }
4273
4274 return ret;
4275}
4276
4277#ifdef CONFIG_COMPAT
4278static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004279 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004280{
4281 struct compat_msghdr __user *msg_compat;
4282 struct io_sr_msg *sr = &req->sr_msg;
4283 struct compat_iovec __user *uiov;
4284 compat_uptr_t ptr;
4285 compat_size_t len;
4286 int ret;
4287
Pavel Begunkov270a5942020-07-12 20:41:04 +03004288 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004289 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004290 &ptr, &len);
4291 if (ret)
4292 return ret;
4293
4294 uiov = compat_ptr(ptr);
4295 if (req->flags & REQ_F_BUFFER_SELECT) {
4296 compat_ssize_t clen;
4297
4298 if (len > 1)
4299 return -EINVAL;
4300 if (!access_ok(uiov, sizeof(*uiov)))
4301 return -EFAULT;
4302 if (__get_user(clen, &uiov->iov_len))
4303 return -EFAULT;
4304 if (clen < 0)
4305 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004306 sr->len = iomsg->iov[0].iov_len;
4307 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004308 } else {
4309 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004310 &iomsg->iov,
4311 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004312 if (ret < 0)
4313 return ret;
4314 }
4315
4316 return 0;
4317}
Jens Axboe03b12302019-12-02 18:50:25 -07004318#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004319
Pavel Begunkov1400e692020-07-12 20:41:05 +03004320static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4321 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004322{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004323 iomsg->msg.msg_name = &iomsg->addr;
4324 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004325
4326#ifdef CONFIG_COMPAT
4327 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004328 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004329#endif
4330
Pavel Begunkov1400e692020-07-12 20:41:05 +03004331 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004332}
4333
Jens Axboebcda7ba2020-02-23 16:42:51 -07004334static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004335 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004336{
4337 struct io_sr_msg *sr = &req->sr_msg;
4338 struct io_buffer *kbuf;
4339
Jens Axboebcda7ba2020-02-23 16:42:51 -07004340 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4341 if (IS_ERR(kbuf))
4342 return kbuf;
4343
4344 sr->kbuf = kbuf;
4345 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004346 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004347}
4348
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004349static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4350{
4351 return io_put_kbuf(req, req->sr_msg.kbuf);
4352}
4353
Jens Axboe3529d8c2019-12-19 18:24:38 -07004354static int io_recvmsg_prep(struct io_kiocb *req,
4355 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004356{
Jens Axboee47293f2019-12-20 08:58:21 -07004357 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004358 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004359 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004360
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004361 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4362 return -EINVAL;
4363
Jens Axboe3529d8c2019-12-19 18:24:38 -07004364 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004365 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004366 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004367 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004368
Jens Axboed8768362020-02-27 14:17:49 -07004369#ifdef CONFIG_COMPAT
4370 if (req->ctx->compat)
4371 sr->msg_flags |= MSG_CMSG_COMPAT;
4372#endif
4373
Jens Axboefddafac2020-01-04 20:19:44 -07004374 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004375 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004376 /* iovec is already imported */
4377 if (req->flags & REQ_F_NEED_CLEANUP)
4378 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004379
Pavel Begunkov1400e692020-07-12 20:41:05 +03004380 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004381 if (!ret)
4382 req->flags |= REQ_F_NEED_CLEANUP;
4383 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004384}
4385
Jens Axboe229a7b62020-06-22 10:13:11 -06004386static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4387 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004388{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004389 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004390 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004391 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004392 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004393 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004394
Jens Axboe0fa03c62019-04-19 13:34:07 -06004395 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004396 if (unlikely(!sock))
4397 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004398
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004399 if (req->io) {
4400 kmsg = &req->io->msg;
4401 kmsg->msg.msg_name = &req->io->msg.addr;
4402 /* if iov is set, it's allocated already */
4403 if (!kmsg->iov)
4404 kmsg->iov = kmsg->fast_iov;
4405 kmsg->msg.msg_iter.iov = kmsg->iov;
4406 } else {
4407 ret = io_recvmsg_copy_hdr(req, &iomsg);
4408 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004409 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004410 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004411 }
4412
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004413 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004414 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004415 if (IS_ERR(kbuf))
4416 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004417 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4418 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4419 1, req->sr_msg.len);
4420 }
4421
4422 flags = req->sr_msg.msg_flags;
4423 if (flags & MSG_DONTWAIT)
4424 req->flags |= REQ_F_NOWAIT;
4425 else if (force_nonblock)
4426 flags |= MSG_DONTWAIT;
4427
4428 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4429 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004430 if (force_nonblock && ret == -EAGAIN)
4431 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 if (ret == -ERESTARTSYS)
4433 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004434
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004435 if (req->flags & REQ_F_BUFFER_SELECTED)
4436 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004437 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004438 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004439 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004440 if (ret < 0)
4441 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004442 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004443 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004444}
4445
Jens Axboe229a7b62020-06-22 10:13:11 -06004446static int io_recv(struct io_kiocb *req, bool force_nonblock,
4447 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004448{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004449 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004450 struct io_sr_msg *sr = &req->sr_msg;
4451 struct msghdr msg;
4452 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004453 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004454 struct iovec iov;
4455 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004456 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004457
Jens Axboefddafac2020-01-04 20:19:44 -07004458 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004459 if (unlikely(!sock))
4460 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004461
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004462 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004463 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004464 if (IS_ERR(kbuf))
4465 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004466 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004467 }
4468
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004469 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004470 if (unlikely(ret))
4471 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004472
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004473 msg.msg_name = NULL;
4474 msg.msg_control = NULL;
4475 msg.msg_controllen = 0;
4476 msg.msg_namelen = 0;
4477 msg.msg_iocb = NULL;
4478 msg.msg_flags = 0;
4479
4480 flags = req->sr_msg.msg_flags;
4481 if (flags & MSG_DONTWAIT)
4482 req->flags |= REQ_F_NOWAIT;
4483 else if (force_nonblock)
4484 flags |= MSG_DONTWAIT;
4485
4486 ret = sock_recvmsg(sock, &msg, flags);
4487 if (force_nonblock && ret == -EAGAIN)
4488 return -EAGAIN;
4489 if (ret == -ERESTARTSYS)
4490 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004491out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004492 if (req->flags & REQ_F_BUFFER_SELECTED)
4493 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004494 if (ret < 0)
4495 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004496 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004497 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004498}
4499
Jens Axboe3529d8c2019-12-19 18:24:38 -07004500static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004501{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004502 struct io_accept *accept = &req->accept;
4503
Jens Axboe17f2fe32019-10-17 14:42:58 -06004504 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4505 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004506 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004507 return -EINVAL;
4508
Jens Axboed55e5f52019-12-11 16:12:15 -07004509 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4510 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004511 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004512 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004513 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004514}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004515
Jens Axboe229a7b62020-06-22 10:13:11 -06004516static int io_accept(struct io_kiocb *req, bool force_nonblock,
4517 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004518{
4519 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004520 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004521 int ret;
4522
Jiufei Xuee697dee2020-06-10 13:41:59 +08004523 if (req->file->f_flags & O_NONBLOCK)
4524 req->flags |= REQ_F_NOWAIT;
4525
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004526 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004527 accept->addr_len, accept->flags,
4528 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004529 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004530 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004531 if (ret < 0) {
4532 if (ret == -ERESTARTSYS)
4533 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004534 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004535 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004536 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004537 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004538}
4539
Jens Axboe3529d8c2019-12-19 18:24:38 -07004540static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004541{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004542 struct io_connect *conn = &req->connect;
4543 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004544
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004545 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4546 return -EINVAL;
4547 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4548 return -EINVAL;
4549
Jens Axboe3529d8c2019-12-19 18:24:38 -07004550 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4551 conn->addr_len = READ_ONCE(sqe->addr2);
4552
4553 if (!io)
4554 return 0;
4555
4556 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004557 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004558}
4559
Jens Axboe229a7b62020-06-22 10:13:11 -06004560static int io_connect(struct io_kiocb *req, bool force_nonblock,
4561 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004562{
Jens Axboef499a022019-12-02 16:28:46 -07004563 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004564 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004565 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004566
Jens Axboef499a022019-12-02 16:28:46 -07004567 if (req->io) {
4568 io = req->io;
4569 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004570 ret = move_addr_to_kernel(req->connect.addr,
4571 req->connect.addr_len,
4572 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004573 if (ret)
4574 goto out;
4575 io = &__io;
4576 }
4577
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004578 file_flags = force_nonblock ? O_NONBLOCK : 0;
4579
4580 ret = __sys_connect_file(req->file, &io->connect.address,
4581 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004582 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004583 if (req->io)
4584 return -EAGAIN;
4585 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004586 ret = -ENOMEM;
4587 goto out;
4588 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004589 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004590 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004591 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004592 if (ret == -ERESTARTSYS)
4593 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004594out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004595 if (ret < 0)
4596 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004597 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004598 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004599}
YueHaibing469956e2020-03-04 15:53:52 +08004600#else /* !CONFIG_NET */
4601static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4602{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004603 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004604}
4605
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004606static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4607 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004608{
YueHaibing469956e2020-03-04 15:53:52 +08004609 return -EOPNOTSUPP;
4610}
4611
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004612static int io_send(struct io_kiocb *req, bool force_nonblock,
4613 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004614{
4615 return -EOPNOTSUPP;
4616}
4617
4618static int io_recvmsg_prep(struct io_kiocb *req,
4619 const struct io_uring_sqe *sqe)
4620{
4621 return -EOPNOTSUPP;
4622}
4623
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004624static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4625 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004626{
4627 return -EOPNOTSUPP;
4628}
4629
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004630static int io_recv(struct io_kiocb *req, bool force_nonblock,
4631 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004632{
4633 return -EOPNOTSUPP;
4634}
4635
4636static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4637{
4638 return -EOPNOTSUPP;
4639}
4640
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004641static int io_accept(struct io_kiocb *req, bool force_nonblock,
4642 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004643{
4644 return -EOPNOTSUPP;
4645}
4646
4647static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4648{
4649 return -EOPNOTSUPP;
4650}
4651
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004652static int io_connect(struct io_kiocb *req, bool force_nonblock,
4653 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004654{
4655 return -EOPNOTSUPP;
4656}
4657#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004658
Jens Axboed7718a92020-02-14 22:23:12 -07004659struct io_poll_table {
4660 struct poll_table_struct pt;
4661 struct io_kiocb *req;
4662 int error;
4663};
4664
Jens Axboed7718a92020-02-14 22:23:12 -07004665static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4666 __poll_t mask, task_work_func_t func)
4667{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004668 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004669 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004670
4671 /* for instances that support it check for an event match first: */
4672 if (mask && !(mask & poll->events))
4673 return 0;
4674
4675 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4676
4677 list_del_init(&poll->wait.entry);
4678
Jens Axboed7718a92020-02-14 22:23:12 -07004679 req->result = mask;
4680 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004681 percpu_ref_get(&req->ctx->refs);
4682
Jens Axboed7718a92020-02-14 22:23:12 -07004683 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004684 * If we using the signalfd wait_queue_head for this wakeup, then
4685 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4686 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4687 * either, as the normal wakeup will suffice.
4688 */
4689 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4690
4691 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004692 * If this fails, then the task is exiting. When a task exits, the
4693 * work gets canceled, so just cancel this request as well instead
4694 * of executing it. We can't safely execute it anyway, as we may not
4695 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004696 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004697 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004698 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004699 struct task_struct *tsk;
4700
Jens Axboee3aabf92020-05-18 11:04:17 -06004701 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004702 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004703 task_work_add(tsk, &req->task_work, 0);
4704 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004705 }
Jens Axboed7718a92020-02-14 22:23:12 -07004706 return 1;
4707}
4708
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004709static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4710 __acquires(&req->ctx->completion_lock)
4711{
4712 struct io_ring_ctx *ctx = req->ctx;
4713
4714 if (!req->result && !READ_ONCE(poll->canceled)) {
4715 struct poll_table_struct pt = { ._key = poll->events };
4716
4717 req->result = vfs_poll(req->file, &pt) & poll->events;
4718 }
4719
4720 spin_lock_irq(&ctx->completion_lock);
4721 if (!req->result && !READ_ONCE(poll->canceled)) {
4722 add_wait_queue(poll->head, &poll->wait);
4723 return true;
4724 }
4725
4726 return false;
4727}
4728
Jens Axboed4e7cd32020-08-15 11:44:50 -07004729static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004730{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004731 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4732 if (req->opcode == IORING_OP_POLL_ADD)
4733 return (struct io_poll_iocb *) req->io;
4734 return req->apoll->double_poll;
4735}
4736
4737static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4738{
4739 if (req->opcode == IORING_OP_POLL_ADD)
4740 return &req->poll;
4741 return &req->apoll->poll;
4742}
4743
4744static void io_poll_remove_double(struct io_kiocb *req)
4745{
4746 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004747
4748 lockdep_assert_held(&req->ctx->completion_lock);
4749
4750 if (poll && poll->head) {
4751 struct wait_queue_head *head = poll->head;
4752
4753 spin_lock(&head->lock);
4754 list_del_init(&poll->wait.entry);
4755 if (poll->wait.private)
4756 refcount_dec(&req->refs);
4757 poll->head = NULL;
4758 spin_unlock(&head->lock);
4759 }
4760}
4761
4762static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4763{
4764 struct io_ring_ctx *ctx = req->ctx;
4765
Jens Axboed4e7cd32020-08-15 11:44:50 -07004766 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004767 req->poll.done = true;
4768 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4769 io_commit_cqring(ctx);
4770}
4771
4772static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4773{
4774 struct io_ring_ctx *ctx = req->ctx;
4775
4776 if (io_poll_rewait(req, &req->poll)) {
4777 spin_unlock_irq(&ctx->completion_lock);
4778 return;
4779 }
4780
4781 hash_del(&req->hash_node);
4782 io_poll_complete(req, req->result, 0);
4783 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004784 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004785 spin_unlock_irq(&ctx->completion_lock);
4786
4787 io_cqring_ev_posted(ctx);
4788}
4789
4790static void io_poll_task_func(struct callback_head *cb)
4791{
4792 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004793 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004794 struct io_kiocb *nxt = NULL;
4795
4796 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004797 if (nxt)
4798 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004799 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004800}
4801
4802static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4803 int sync, void *key)
4804{
4805 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004806 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004807 __poll_t mask = key_to_poll(key);
4808
4809 /* for instances that support it check for an event match first: */
4810 if (mask && !(mask & poll->events))
4811 return 0;
4812
Jens Axboe8706e042020-09-28 08:38:54 -06004813 list_del_init(&wait->entry);
4814
Jens Axboe807abcb2020-07-17 17:09:27 -06004815 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004816 bool done;
4817
Jens Axboe807abcb2020-07-17 17:09:27 -06004818 spin_lock(&poll->head->lock);
4819 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004820 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004821 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004822 /* make sure double remove sees this as being gone */
4823 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004824 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004825 if (!done)
4826 __io_async_wake(req, poll, mask, io_poll_task_func);
4827 }
4828 refcount_dec(&req->refs);
4829 return 1;
4830}
4831
4832static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4833 wait_queue_func_t wake_func)
4834{
4835 poll->head = NULL;
4836 poll->done = false;
4837 poll->canceled = false;
4838 poll->events = events;
4839 INIT_LIST_HEAD(&poll->wait.entry);
4840 init_waitqueue_func_entry(&poll->wait, wake_func);
4841}
4842
4843static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004844 struct wait_queue_head *head,
4845 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004846{
4847 struct io_kiocb *req = pt->req;
4848
4849 /*
4850 * If poll->head is already set, it's because the file being polled
4851 * uses multiple waitqueues for poll handling (eg one for read, one
4852 * for write). Setup a separate io_poll_iocb if this happens.
4853 */
4854 if (unlikely(poll->head)) {
4855 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004856 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004857 pt->error = -EINVAL;
4858 return;
4859 }
4860 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4861 if (!poll) {
4862 pt->error = -ENOMEM;
4863 return;
4864 }
4865 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4866 refcount_inc(&req->refs);
4867 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004868 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004869 }
4870
4871 pt->error = 0;
4872 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004873
4874 if (poll->events & EPOLLEXCLUSIVE)
4875 add_wait_queue_exclusive(head, &poll->wait);
4876 else
4877 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004878}
4879
4880static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4881 struct poll_table_struct *p)
4882{
4883 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004884 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004885
Jens Axboe807abcb2020-07-17 17:09:27 -06004886 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004887}
4888
Jens Axboed7718a92020-02-14 22:23:12 -07004889static void io_async_task_func(struct callback_head *cb)
4890{
4891 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4892 struct async_poll *apoll = req->apoll;
4893 struct io_ring_ctx *ctx = req->ctx;
4894
4895 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4896
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004897 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004898 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004899 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004900 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004901 }
4902
Jens Axboe31067252020-05-17 17:43:31 -06004903 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004904 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004905 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004906
Jens Axboed4e7cd32020-08-15 11:44:50 -07004907 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004908 spin_unlock_irq(&ctx->completion_lock);
4909
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004910 if (!READ_ONCE(apoll->poll.canceled))
4911 __io_req_task_submit(req);
4912 else
4913 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004914
Jens Axboe6d816e02020-08-11 08:04:14 -06004915 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004916 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004917 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004918}
4919
4920static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4921 void *key)
4922{
4923 struct io_kiocb *req = wait->private;
4924 struct io_poll_iocb *poll = &req->apoll->poll;
4925
4926 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4927 key_to_poll(key));
4928
4929 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4930}
4931
4932static void io_poll_req_insert(struct io_kiocb *req)
4933{
4934 struct io_ring_ctx *ctx = req->ctx;
4935 struct hlist_head *list;
4936
4937 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4938 hlist_add_head(&req->hash_node, list);
4939}
4940
4941static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4942 struct io_poll_iocb *poll,
4943 struct io_poll_table *ipt, __poll_t mask,
4944 wait_queue_func_t wake_func)
4945 __acquires(&ctx->completion_lock)
4946{
4947 struct io_ring_ctx *ctx = req->ctx;
4948 bool cancel = false;
4949
Jens Axboe18bceab2020-05-15 11:56:54 -06004950 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004951 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004952 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004953
4954 ipt->pt._key = mask;
4955 ipt->req = req;
4956 ipt->error = -EINVAL;
4957
Jens Axboed7718a92020-02-14 22:23:12 -07004958 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4959
4960 spin_lock_irq(&ctx->completion_lock);
4961 if (likely(poll->head)) {
4962 spin_lock(&poll->head->lock);
4963 if (unlikely(list_empty(&poll->wait.entry))) {
4964 if (ipt->error)
4965 cancel = true;
4966 ipt->error = 0;
4967 mask = 0;
4968 }
4969 if (mask || ipt->error)
4970 list_del_init(&poll->wait.entry);
4971 else if (cancel)
4972 WRITE_ONCE(poll->canceled, true);
4973 else if (!poll->done) /* actually waiting for an event */
4974 io_poll_req_insert(req);
4975 spin_unlock(&poll->head->lock);
4976 }
4977
4978 return mask;
4979}
4980
4981static bool io_arm_poll_handler(struct io_kiocb *req)
4982{
4983 const struct io_op_def *def = &io_op_defs[req->opcode];
4984 struct io_ring_ctx *ctx = req->ctx;
4985 struct async_poll *apoll;
4986 struct io_poll_table ipt;
4987 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004988 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004989
4990 if (!req->file || !file_can_poll(req->file))
4991 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004992 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004993 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004994 if (def->pollin)
4995 rw = READ;
4996 else if (def->pollout)
4997 rw = WRITE;
4998 else
4999 return false;
5000 /* if we can't nonblock try, then no point in arming a poll handler */
5001 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005002 return false;
5003
5004 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5005 if (unlikely(!apoll))
5006 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005007 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005008
5009 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005010 req->apoll = apoll;
5011 INIT_HLIST_NODE(&req->hash_node);
5012
Nathan Chancellor8755d972020-03-02 16:01:19 -07005013 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005014 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005015 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005016 if (def->pollout)
5017 mask |= POLLOUT | POLLWRNORM;
5018 mask |= POLLERR | POLLPRI;
5019
5020 ipt.pt._qproc = io_async_queue_proc;
5021
5022 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5023 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005024 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005025 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005026 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005027 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005028 kfree(apoll);
5029 return false;
5030 }
5031 spin_unlock_irq(&ctx->completion_lock);
5032 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5033 apoll->poll.events);
5034 return true;
5035}
5036
5037static bool __io_poll_remove_one(struct io_kiocb *req,
5038 struct io_poll_iocb *poll)
5039{
Jens Axboeb41e9852020-02-17 09:52:41 -07005040 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005041
5042 spin_lock(&poll->head->lock);
5043 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005044 if (!list_empty(&poll->wait.entry)) {
5045 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005046 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005047 }
5048 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005049 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005050 return do_complete;
5051}
5052
5053static bool io_poll_remove_one(struct io_kiocb *req)
5054{
5055 bool do_complete;
5056
Jens Axboed4e7cd32020-08-15 11:44:50 -07005057 io_poll_remove_double(req);
5058
Jens Axboed7718a92020-02-14 22:23:12 -07005059 if (req->opcode == IORING_OP_POLL_ADD) {
5060 do_complete = __io_poll_remove_one(req, &req->poll);
5061 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005062 struct async_poll *apoll = req->apoll;
5063
Jens Axboed7718a92020-02-14 22:23:12 -07005064 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005065 do_complete = __io_poll_remove_one(req, &apoll->poll);
5066 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005067 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005068 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005069 kfree(apoll);
5070 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005071 }
5072
Jens Axboeb41e9852020-02-17 09:52:41 -07005073 if (do_complete) {
5074 io_cqring_fill_event(req, -ECANCELED);
5075 io_commit_cqring(req->ctx);
5076 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005077 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005078 io_put_req(req);
5079 }
5080
5081 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005082}
5083
Jens Axboe76e1b642020-09-26 15:05:03 -06005084/*
5085 * Returns true if we found and killed one or more poll requests
5086 */
5087static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005088{
Jens Axboe78076bb2019-12-04 19:56:40 -07005089 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005090 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005091 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005092
5093 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005094 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5095 struct hlist_head *list;
5096
5097 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005098 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5099 if (io_task_match(req, tsk))
5100 posted += io_poll_remove_one(req);
5101 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005102 }
5103 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005104
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005105 if (posted)
5106 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005107
5108 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005109}
5110
Jens Axboe47f46762019-11-09 17:43:02 -07005111static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5112{
Jens Axboe78076bb2019-12-04 19:56:40 -07005113 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005114 struct io_kiocb *req;
5115
Jens Axboe78076bb2019-12-04 19:56:40 -07005116 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5117 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005118 if (sqe_addr != req->user_data)
5119 continue;
5120 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005121 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005122 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005123 }
5124
5125 return -ENOENT;
5126}
5127
Jens Axboe3529d8c2019-12-19 18:24:38 -07005128static int io_poll_remove_prep(struct io_kiocb *req,
5129 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005130{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005131 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5132 return -EINVAL;
5133 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5134 sqe->poll_events)
5135 return -EINVAL;
5136
Jens Axboe0969e782019-12-17 18:40:57 -07005137 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005138 return 0;
5139}
5140
5141/*
5142 * Find a running poll command that matches one specified in sqe->addr,
5143 * and remove it if found.
5144 */
5145static int io_poll_remove(struct io_kiocb *req)
5146{
5147 struct io_ring_ctx *ctx = req->ctx;
5148 u64 addr;
5149 int ret;
5150
Jens Axboe0969e782019-12-17 18:40:57 -07005151 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005152 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005153 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005154 spin_unlock_irq(&ctx->completion_lock);
5155
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005156 if (ret < 0)
5157 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005158 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005159 return 0;
5160}
5161
Jens Axboe221c5eb2019-01-17 09:41:58 -07005162static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5163 void *key)
5164{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005165 struct io_kiocb *req = wait->private;
5166 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005167
Jens Axboed7718a92020-02-14 22:23:12 -07005168 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005169}
5170
Jens Axboe221c5eb2019-01-17 09:41:58 -07005171static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5172 struct poll_table_struct *p)
5173{
5174 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5175
Jens Axboe807abcb2020-07-17 17:09:27 -06005176 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005177}
5178
Jens Axboe3529d8c2019-12-19 18:24:38 -07005179static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005180{
5181 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005182 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005183
5184 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5185 return -EINVAL;
5186 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5187 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005188 if (!poll->file)
5189 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005190
Jiufei Xue5769a352020-06-17 17:53:55 +08005191 events = READ_ONCE(sqe->poll32_events);
5192#ifdef __BIG_ENDIAN
5193 events = swahw32(events);
5194#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005195 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5196 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005197 return 0;
5198}
5199
Pavel Begunkov014db002020-03-03 21:33:12 +03005200static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005201{
5202 struct io_poll_iocb *poll = &req->poll;
5203 struct io_ring_ctx *ctx = req->ctx;
5204 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005205 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005206
Jens Axboe78076bb2019-12-04 19:56:40 -07005207 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005208 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005209
Jens Axboed7718a92020-02-14 22:23:12 -07005210 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5211 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005212
Jens Axboe8c838782019-03-12 15:48:16 -06005213 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005214 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005215 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005216 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005217 spin_unlock_irq(&ctx->completion_lock);
5218
Jens Axboe8c838782019-03-12 15:48:16 -06005219 if (mask) {
5220 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005221 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005222 }
Jens Axboe8c838782019-03-12 15:48:16 -06005223 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005224}
5225
Jens Axboe5262f562019-09-17 12:26:57 -06005226static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5227{
Jens Axboead8a48a2019-11-15 08:49:11 -07005228 struct io_timeout_data *data = container_of(timer,
5229 struct io_timeout_data, timer);
5230 struct io_kiocb *req = data->req;
5231 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005232 unsigned long flags;
5233
Jens Axboe5262f562019-09-17 12:26:57 -06005234 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005235 atomic_set(&req->ctx->cq_timeouts,
5236 atomic_read(&req->ctx->cq_timeouts) + 1);
5237
zhangyi (F)ef036812019-10-23 15:10:08 +08005238 /*
Jens Axboe11365042019-10-16 09:08:32 -06005239 * We could be racing with timeout deletion. If the list is empty,
5240 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005241 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005242 if (!list_empty(&req->timeout.list))
5243 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005244
Jens Axboe78e19bb2019-11-06 15:21:34 -07005245 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005246 io_commit_cqring(ctx);
5247 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5248
5249 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005250 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005251 io_put_req(req);
5252 return HRTIMER_NORESTART;
5253}
5254
Jens Axboef254ac02020-08-12 17:33:30 -06005255static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005256{
Jens Axboef254ac02020-08-12 17:33:30 -06005257 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005258
Jens Axboef254ac02020-08-12 17:33:30 -06005259 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005260
Jens Axboe2d283902019-12-04 11:08:05 -07005261 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005262 if (ret == -1)
5263 return -EALREADY;
5264
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005265 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005266 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005267 io_cqring_fill_event(req, -ECANCELED);
5268 io_put_req(req);
5269 return 0;
5270}
5271
Jens Axboef254ac02020-08-12 17:33:30 -06005272static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5273{
5274 struct io_kiocb *req;
5275 int ret = -ENOENT;
5276
5277 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5278 if (user_data == req->user_data) {
5279 ret = 0;
5280 break;
5281 }
5282 }
5283
5284 if (ret == -ENOENT)
5285 return ret;
5286
5287 return __io_timeout_cancel(req);
5288}
5289
Jens Axboe3529d8c2019-12-19 18:24:38 -07005290static int io_timeout_remove_prep(struct io_kiocb *req,
5291 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005292{
Jens Axboeb29472e2019-12-17 18:50:29 -07005293 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5294 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005295 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5296 return -EINVAL;
5297 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005298 return -EINVAL;
5299
5300 req->timeout.addr = READ_ONCE(sqe->addr);
5301 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5302 if (req->timeout.flags)
5303 return -EINVAL;
5304
Jens Axboeb29472e2019-12-17 18:50:29 -07005305 return 0;
5306}
5307
Jens Axboe11365042019-10-16 09:08:32 -06005308/*
5309 * Remove or update an existing timeout command
5310 */
Jens Axboefc4df992019-12-10 14:38:45 -07005311static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005312{
5313 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005314 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005315
Jens Axboe11365042019-10-16 09:08:32 -06005316 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005317 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005318
Jens Axboe47f46762019-11-09 17:43:02 -07005319 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005320 io_commit_cqring(ctx);
5321 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005322 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005323 if (ret < 0)
5324 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005325 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005326 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005327}
5328
Jens Axboe3529d8c2019-12-19 18:24:38 -07005329static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005330 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005331{
Jens Axboead8a48a2019-11-15 08:49:11 -07005332 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005333 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005334 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005335
Jens Axboead8a48a2019-11-15 08:49:11 -07005336 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005337 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005338 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005339 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005340 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005341 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005342 flags = READ_ONCE(sqe->timeout_flags);
5343 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005344 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005345
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005346 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005347
Jens Axboe3529d8c2019-12-19 18:24:38 -07005348 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005349 return -ENOMEM;
5350
5351 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005352 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005353
5354 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005355 return -EFAULT;
5356
Jens Axboe11365042019-10-16 09:08:32 -06005357 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005358 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005359 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005360 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005361
Jens Axboead8a48a2019-11-15 08:49:11 -07005362 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5363 return 0;
5364}
5365
Jens Axboefc4df992019-12-10 14:38:45 -07005366static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005367{
Jens Axboead8a48a2019-11-15 08:49:11 -07005368 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005369 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005370 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005371 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005372
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005373 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005374
Jens Axboe5262f562019-09-17 12:26:57 -06005375 /*
5376 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005377 * timeout event to be satisfied. If it isn't set, then this is
5378 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005379 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005380 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005381 entry = ctx->timeout_list.prev;
5382 goto add;
5383 }
Jens Axboe5262f562019-09-17 12:26:57 -06005384
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005385 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5386 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005387
5388 /*
5389 * Insertion sort, ensuring the first entry in the list is always
5390 * the one we need first.
5391 */
Jens Axboe5262f562019-09-17 12:26:57 -06005392 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005393 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5394 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005395
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005396 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005397 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005398 /* nxt.seq is behind @tail, otherwise would've been completed */
5399 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005400 break;
5401 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005402add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005403 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005404 data->timer.function = io_timeout_fn;
5405 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005406 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005407 return 0;
5408}
5409
Jens Axboe62755e32019-10-28 21:49:21 -06005410static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005411{
Jens Axboe62755e32019-10-28 21:49:21 -06005412 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005413
Jens Axboe62755e32019-10-28 21:49:21 -06005414 return req->user_data == (unsigned long) data;
5415}
5416
Jens Axboee977d6d2019-11-05 12:39:45 -07005417static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005418{
Jens Axboe62755e32019-10-28 21:49:21 -06005419 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005420 int ret = 0;
5421
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005422 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005423 switch (cancel_ret) {
5424 case IO_WQ_CANCEL_OK:
5425 ret = 0;
5426 break;
5427 case IO_WQ_CANCEL_RUNNING:
5428 ret = -EALREADY;
5429 break;
5430 case IO_WQ_CANCEL_NOTFOUND:
5431 ret = -ENOENT;
5432 break;
5433 }
5434
Jens Axboee977d6d2019-11-05 12:39:45 -07005435 return ret;
5436}
5437
Jens Axboe47f46762019-11-09 17:43:02 -07005438static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5439 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005440 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005441{
5442 unsigned long flags;
5443 int ret;
5444
5445 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5446 if (ret != -ENOENT) {
5447 spin_lock_irqsave(&ctx->completion_lock, flags);
5448 goto done;
5449 }
5450
5451 spin_lock_irqsave(&ctx->completion_lock, flags);
5452 ret = io_timeout_cancel(ctx, sqe_addr);
5453 if (ret != -ENOENT)
5454 goto done;
5455 ret = io_poll_cancel(ctx, sqe_addr);
5456done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005457 if (!ret)
5458 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005459 io_cqring_fill_event(req, ret);
5460 io_commit_cqring(ctx);
5461 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5462 io_cqring_ev_posted(ctx);
5463
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005464 if (ret < 0)
5465 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005466 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005467}
5468
Jens Axboe3529d8c2019-12-19 18:24:38 -07005469static int io_async_cancel_prep(struct io_kiocb *req,
5470 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005471{
Jens Axboefbf23842019-12-17 18:45:56 -07005472 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005473 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005474 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5475 return -EINVAL;
5476 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005477 return -EINVAL;
5478
Jens Axboefbf23842019-12-17 18:45:56 -07005479 req->cancel.addr = READ_ONCE(sqe->addr);
5480 return 0;
5481}
5482
Pavel Begunkov014db002020-03-03 21:33:12 +03005483static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005484{
5485 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005486
Pavel Begunkov014db002020-03-03 21:33:12 +03005487 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005488 return 0;
5489}
5490
Jens Axboe05f3fb32019-12-09 11:22:50 -07005491static int io_files_update_prep(struct io_kiocb *req,
5492 const struct io_uring_sqe *sqe)
5493{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005494 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5495 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005496 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5497 return -EINVAL;
5498 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005499 return -EINVAL;
5500
5501 req->files_update.offset = READ_ONCE(sqe->off);
5502 req->files_update.nr_args = READ_ONCE(sqe->len);
5503 if (!req->files_update.nr_args)
5504 return -EINVAL;
5505 req->files_update.arg = READ_ONCE(sqe->addr);
5506 return 0;
5507}
5508
Jens Axboe229a7b62020-06-22 10:13:11 -06005509static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5510 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005511{
5512 struct io_ring_ctx *ctx = req->ctx;
5513 struct io_uring_files_update up;
5514 int ret;
5515
Jens Axboef86cd202020-01-29 13:46:44 -07005516 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005517 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005518
5519 up.offset = req->files_update.offset;
5520 up.fds = req->files_update.arg;
5521
5522 mutex_lock(&ctx->uring_lock);
5523 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5524 mutex_unlock(&ctx->uring_lock);
5525
5526 if (ret < 0)
5527 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005528 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005529 return 0;
5530}
5531
Jens Axboe3529d8c2019-12-19 18:24:38 -07005532static int io_req_defer_prep(struct io_kiocb *req,
5533 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005534{
Jens Axboee7815732019-12-17 19:45:06 -07005535 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005536
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005537 if (!sqe)
5538 return 0;
5539
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005540 if (io_alloc_async_ctx(req))
5541 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005542 ret = io_prep_work_files(req);
5543 if (unlikely(ret))
5544 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005545
Jens Axboe202700e12020-09-12 13:18:10 -06005546 io_prep_async_work(req);
5547
Jens Axboed625c6e2019-12-17 19:53:05 -07005548 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005549 case IORING_OP_NOP:
5550 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005551 case IORING_OP_READV:
5552 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005553 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005554 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005555 break;
5556 case IORING_OP_WRITEV:
5557 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005558 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005559 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005560 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005561 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005562 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005563 break;
5564 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005565 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005566 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005567 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005568 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005569 break;
5570 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005571 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005572 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005573 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005574 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005575 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005576 break;
5577 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005578 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005579 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005580 break;
Jens Axboef499a022019-12-02 16:28:46 -07005581 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005582 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005583 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005584 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005585 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005586 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005587 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005588 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005589 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005590 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005591 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005592 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005593 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005594 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005595 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005596 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005597 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005598 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005599 case IORING_OP_FALLOCATE:
5600 ret = io_fallocate_prep(req, sqe);
5601 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005602 case IORING_OP_OPENAT:
5603 ret = io_openat_prep(req, sqe);
5604 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005605 case IORING_OP_CLOSE:
5606 ret = io_close_prep(req, sqe);
5607 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005608 case IORING_OP_FILES_UPDATE:
5609 ret = io_files_update_prep(req, sqe);
5610 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005611 case IORING_OP_STATX:
5612 ret = io_statx_prep(req, sqe);
5613 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005614 case IORING_OP_FADVISE:
5615 ret = io_fadvise_prep(req, sqe);
5616 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005617 case IORING_OP_MADVISE:
5618 ret = io_madvise_prep(req, sqe);
5619 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005620 case IORING_OP_OPENAT2:
5621 ret = io_openat2_prep(req, sqe);
5622 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005623 case IORING_OP_EPOLL_CTL:
5624 ret = io_epoll_ctl_prep(req, sqe);
5625 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005626 case IORING_OP_SPLICE:
5627 ret = io_splice_prep(req, sqe);
5628 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005629 case IORING_OP_PROVIDE_BUFFERS:
5630 ret = io_provide_buffers_prep(req, sqe);
5631 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005632 case IORING_OP_REMOVE_BUFFERS:
5633 ret = io_remove_buffers_prep(req, sqe);
5634 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005635 case IORING_OP_TEE:
5636 ret = io_tee_prep(req, sqe);
5637 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005638 default:
Jens Axboee7815732019-12-17 19:45:06 -07005639 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5640 req->opcode);
5641 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005642 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005643 }
5644
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005645 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005646}
5647
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005648static u32 io_get_sequence(struct io_kiocb *req)
5649{
5650 struct io_kiocb *pos;
5651 struct io_ring_ctx *ctx = req->ctx;
5652 u32 total_submitted, nr_reqs = 1;
5653
5654 if (req->flags & REQ_F_LINK_HEAD)
5655 list_for_each_entry(pos, &req->link_list, link_list)
5656 nr_reqs++;
5657
5658 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5659 return total_submitted - nr_reqs;
5660}
5661
Jens Axboe3529d8c2019-12-19 18:24:38 -07005662static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005663{
Jackie Liua197f662019-11-08 08:09:12 -07005664 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005665 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005666 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005667 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005668
Bob Liu9d858b22019-11-13 18:06:25 +08005669 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005670 if (likely(list_empty_careful(&ctx->defer_list) &&
5671 !(req->flags & REQ_F_IO_DRAIN)))
5672 return 0;
5673
5674 seq = io_get_sequence(req);
5675 /* Still a chance to pass the sequence check */
5676 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005677 return 0;
5678
Pavel Begunkov650b5482020-05-17 14:02:11 +03005679 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005680 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005681 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005682 return ret;
5683 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005684 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005685 de = kmalloc(sizeof(*de), GFP_KERNEL);
5686 if (!de)
5687 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005688
Jens Axboede0617e2019-04-06 21:51:27 -06005689 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005690 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005691 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005692 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005693 io_queue_async_work(req);
5694 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005695 }
5696
Jens Axboe915967f2019-11-21 09:01:20 -07005697 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005698 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005699 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005700 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005701 spin_unlock_irq(&ctx->completion_lock);
5702 return -EIOCBQUEUED;
5703}
5704
Jens Axboef573d382020-09-22 10:19:24 -06005705static void io_req_drop_files(struct io_kiocb *req)
5706{
5707 struct io_ring_ctx *ctx = req->ctx;
5708 unsigned long flags;
5709
5710 spin_lock_irqsave(&ctx->inflight_lock, flags);
5711 list_del(&req->inflight_entry);
5712 if (waitqueue_active(&ctx->inflight_wait))
5713 wake_up(&ctx->inflight_wait);
5714 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5715 req->flags &= ~REQ_F_INFLIGHT;
Jens Axboe0f212202020-09-13 13:09:39 -06005716 put_files_struct(req->work.files);
Jens Axboe9b828492020-09-18 20:13:06 -06005717 put_nsproxy(req->work.nsproxy);
Jens Axboef573d382020-09-22 10:19:24 -06005718 req->work.files = NULL;
5719}
5720
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005721static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005722{
5723 struct io_async_ctx *io = req->io;
5724
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005725 if (req->flags & REQ_F_BUFFER_SELECTED) {
5726 switch (req->opcode) {
5727 case IORING_OP_READV:
5728 case IORING_OP_READ_FIXED:
5729 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005730 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005731 break;
5732 case IORING_OP_RECVMSG:
5733 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005734 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005735 break;
5736 }
5737 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005738 }
5739
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005740 if (req->flags & REQ_F_NEED_CLEANUP) {
5741 switch (req->opcode) {
5742 case IORING_OP_READV:
5743 case IORING_OP_READ_FIXED:
5744 case IORING_OP_READ:
5745 case IORING_OP_WRITEV:
5746 case IORING_OP_WRITE_FIXED:
5747 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005748 if (io->rw.free_iovec)
5749 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005750 break;
5751 case IORING_OP_RECVMSG:
5752 case IORING_OP_SENDMSG:
5753 if (io->msg.iov != io->msg.fast_iov)
5754 kfree(io->msg.iov);
5755 break;
5756 case IORING_OP_SPLICE:
5757 case IORING_OP_TEE:
5758 io_put_file(req, req->splice.file_in,
5759 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5760 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005761 case IORING_OP_OPENAT:
5762 case IORING_OP_OPENAT2:
5763 if (req->open.filename)
5764 putname(req->open.filename);
5765 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005766 }
5767 req->flags &= ~REQ_F_NEED_CLEANUP;
5768 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005769
Jens Axboef573d382020-09-22 10:19:24 -06005770 if (req->flags & REQ_F_INFLIGHT)
5771 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005772}
5773
Jens Axboe3529d8c2019-12-19 18:24:38 -07005774static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005775 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005776{
Jackie Liua197f662019-11-08 08:09:12 -07005777 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005778 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005779
Jens Axboed625c6e2019-12-17 19:53:05 -07005780 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005781 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005782 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005783 break;
5784 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005785 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005786 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005787 if (sqe) {
5788 ret = io_read_prep(req, sqe, force_nonblock);
5789 if (ret < 0)
5790 break;
5791 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005792 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005793 break;
5794 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005795 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005796 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005797 if (sqe) {
5798 ret = io_write_prep(req, sqe, force_nonblock);
5799 if (ret < 0)
5800 break;
5801 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005802 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005803 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005804 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005805 if (sqe) {
5806 ret = io_prep_fsync(req, sqe);
5807 if (ret < 0)
5808 break;
5809 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005810 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005811 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005812 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005813 if (sqe) {
5814 ret = io_poll_add_prep(req, sqe);
5815 if (ret)
5816 break;
5817 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005818 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005819 break;
5820 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005821 if (sqe) {
5822 ret = io_poll_remove_prep(req, sqe);
5823 if (ret < 0)
5824 break;
5825 }
Jens Axboefc4df992019-12-10 14:38:45 -07005826 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005827 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005828 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005829 if (sqe) {
5830 ret = io_prep_sfr(req, sqe);
5831 if (ret < 0)
5832 break;
5833 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005834 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005835 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005836 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005837 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005838 if (sqe) {
5839 ret = io_sendmsg_prep(req, sqe);
5840 if (ret < 0)
5841 break;
5842 }
Jens Axboefddafac2020-01-04 20:19:44 -07005843 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005844 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005845 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005846 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005847 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005848 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005849 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005850 if (sqe) {
5851 ret = io_recvmsg_prep(req, sqe);
5852 if (ret)
5853 break;
5854 }
Jens Axboefddafac2020-01-04 20:19:44 -07005855 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005856 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005857 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005858 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005859 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005860 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005861 if (sqe) {
5862 ret = io_timeout_prep(req, sqe, false);
5863 if (ret)
5864 break;
5865 }
Jens Axboefc4df992019-12-10 14:38:45 -07005866 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005867 break;
Jens Axboe11365042019-10-16 09:08:32 -06005868 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005869 if (sqe) {
5870 ret = io_timeout_remove_prep(req, sqe);
5871 if (ret)
5872 break;
5873 }
Jens Axboefc4df992019-12-10 14:38:45 -07005874 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005875 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005876 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005877 if (sqe) {
5878 ret = io_accept_prep(req, sqe);
5879 if (ret)
5880 break;
5881 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005882 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005883 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005884 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005885 if (sqe) {
5886 ret = io_connect_prep(req, sqe);
5887 if (ret)
5888 break;
5889 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005890 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005891 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005892 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005893 if (sqe) {
5894 ret = io_async_cancel_prep(req, sqe);
5895 if (ret)
5896 break;
5897 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005898 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005899 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005900 case IORING_OP_FALLOCATE:
5901 if (sqe) {
5902 ret = io_fallocate_prep(req, sqe);
5903 if (ret)
5904 break;
5905 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005906 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005907 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005908 case IORING_OP_OPENAT:
5909 if (sqe) {
5910 ret = io_openat_prep(req, sqe);
5911 if (ret)
5912 break;
5913 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005914 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005915 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005916 case IORING_OP_CLOSE:
5917 if (sqe) {
5918 ret = io_close_prep(req, sqe);
5919 if (ret)
5920 break;
5921 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005922 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005923 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005924 case IORING_OP_FILES_UPDATE:
5925 if (sqe) {
5926 ret = io_files_update_prep(req, sqe);
5927 if (ret)
5928 break;
5929 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005930 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005931 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005932 case IORING_OP_STATX:
5933 if (sqe) {
5934 ret = io_statx_prep(req, sqe);
5935 if (ret)
5936 break;
5937 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005938 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005939 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005940 case IORING_OP_FADVISE:
5941 if (sqe) {
5942 ret = io_fadvise_prep(req, sqe);
5943 if (ret)
5944 break;
5945 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005946 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005947 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005948 case IORING_OP_MADVISE:
5949 if (sqe) {
5950 ret = io_madvise_prep(req, sqe);
5951 if (ret)
5952 break;
5953 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005954 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005955 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005956 case IORING_OP_OPENAT2:
5957 if (sqe) {
5958 ret = io_openat2_prep(req, sqe);
5959 if (ret)
5960 break;
5961 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005962 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005963 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005964 case IORING_OP_EPOLL_CTL:
5965 if (sqe) {
5966 ret = io_epoll_ctl_prep(req, sqe);
5967 if (ret)
5968 break;
5969 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005970 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005971 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005972 case IORING_OP_SPLICE:
5973 if (sqe) {
5974 ret = io_splice_prep(req, sqe);
5975 if (ret < 0)
5976 break;
5977 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005978 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005979 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005980 case IORING_OP_PROVIDE_BUFFERS:
5981 if (sqe) {
5982 ret = io_provide_buffers_prep(req, sqe);
5983 if (ret)
5984 break;
5985 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005986 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005987 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005988 case IORING_OP_REMOVE_BUFFERS:
5989 if (sqe) {
5990 ret = io_remove_buffers_prep(req, sqe);
5991 if (ret)
5992 break;
5993 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005994 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005995 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005996 case IORING_OP_TEE:
5997 if (sqe) {
5998 ret = io_tee_prep(req, sqe);
5999 if (ret < 0)
6000 break;
6001 }
6002 ret = io_tee(req, force_nonblock);
6003 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006004 default:
6005 ret = -EINVAL;
6006 break;
6007 }
6008
6009 if (ret)
6010 return ret;
6011
Jens Axboeb5325762020-05-19 21:20:27 -06006012 /* If the op doesn't have a file, we're not polling for it */
6013 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006014 const bool in_async = io_wq_current_is_worker();
6015
Jens Axboe11ba8202020-01-15 21:51:17 -07006016 /* workqueue context doesn't hold uring_lock, grab it now */
6017 if (in_async)
6018 mutex_lock(&ctx->uring_lock);
6019
Jens Axboe2b188cc2019-01-07 10:46:33 -07006020 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07006021
6022 if (in_async)
6023 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07006024 }
6025
6026 return 0;
6027}
6028
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006029static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006030{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006032 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006033 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006035 timeout = io_prep_linked_timeout(req);
6036 if (timeout)
6037 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006038
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006039 /* if NO_CANCEL is set, we must still run the work */
6040 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
6041 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06006042 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07006043 }
Jens Axboe31b51512019-01-18 22:56:34 -07006044
Jens Axboe561fb042019-10-24 07:25:42 -06006045 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006046 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006047 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006048 /*
6049 * We can get EAGAIN for polled IO even though we're
6050 * forcing a sync submission from here, since we can't
6051 * wait for request slots on the block side.
6052 */
6053 if (ret != -EAGAIN)
6054 break;
6055 cond_resched();
6056 } while (1);
6057 }
Jens Axboe31b51512019-01-18 22:56:34 -07006058
Jens Axboe561fb042019-10-24 07:25:42 -06006059 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006060 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006061 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006062 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006064 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006065}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066
Jens Axboe65e19f52019-10-26 07:20:21 -06006067static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6068 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006069{
Jens Axboe65e19f52019-10-26 07:20:21 -06006070 struct fixed_file_table *table;
6071
Jens Axboe05f3fb32019-12-09 11:22:50 -07006072 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006073 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006074}
6075
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006076static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6077 int fd, struct file **out_file, bool fixed)
6078{
6079 struct io_ring_ctx *ctx = req->ctx;
6080 struct file *file;
6081
6082 if (fixed) {
6083 if (unlikely(!ctx->file_data ||
6084 (unsigned) fd >= ctx->nr_user_files))
6085 return -EBADF;
6086 fd = array_index_nospec(fd, ctx->nr_user_files);
6087 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006088 if (file) {
6089 req->fixed_file_refs = ctx->file_data->cur_refs;
6090 percpu_ref_get(req->fixed_file_refs);
6091 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006092 } else {
6093 trace_io_uring_file_get(ctx, fd);
6094 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006095 }
6096
Jens Axboefd2206e2020-06-02 16:40:47 -06006097 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6098 *out_file = file;
6099 return 0;
6100 }
6101 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006102}
6103
Jens Axboe3529d8c2019-12-19 18:24:38 -07006104static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006105 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006106{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006107 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006108
Jens Axboe63ff8222020-05-07 14:56:15 -06006109 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006110 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006111 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006112
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006113 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006114}
6115
Jackie Liua197f662019-11-08 08:09:12 -07006116static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117{
Jackie Liua197f662019-11-08 08:09:12 -07006118 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006119
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006120 io_req_init_async(req);
6121
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006122 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006123 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07006124
Jens Axboe0f212202020-09-13 13:09:39 -06006125 req->work.files = get_files_struct(current);
Jens Axboe9b828492020-09-18 20:13:06 -06006126 get_nsproxy(current->nsproxy);
6127 req->work.nsproxy = current->nsproxy;
Jens Axboe0f212202020-09-13 13:09:39 -06006128 req->flags |= REQ_F_INFLIGHT;
6129
Jens Axboefcb323c2019-10-24 12:39:47 -06006130 spin_lock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006131 list_add(&req->inflight_entry, &ctx->inflight_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06006132 spin_unlock_irq(&ctx->inflight_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06006133 return 0;
Jens Axboefcb323c2019-10-24 12:39:47 -06006134}
6135
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006136static inline int io_prep_work_files(struct io_kiocb *req)
6137{
6138 if (!io_op_defs[req->opcode].file_table)
6139 return 0;
6140 return io_grab_files(req);
6141}
6142
Jens Axboe2665abf2019-11-05 12:40:47 -07006143static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6144{
Jens Axboead8a48a2019-11-15 08:49:11 -07006145 struct io_timeout_data *data = container_of(timer,
6146 struct io_timeout_data, timer);
6147 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006148 struct io_ring_ctx *ctx = req->ctx;
6149 struct io_kiocb *prev = NULL;
6150 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006151
6152 spin_lock_irqsave(&ctx->completion_lock, flags);
6153
6154 /*
6155 * We don't expect the list to be empty, that will only happen if we
6156 * race with the completion of the linked work.
6157 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006158 if (!list_empty(&req->link_list)) {
6159 prev = list_entry(req->link_list.prev, struct io_kiocb,
6160 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006161 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006162 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006163 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6164 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006165 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006166 }
6167
6168 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6169
6170 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006171 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006172 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006173 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006174 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006175 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006176 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006177 return HRTIMER_NORESTART;
6178}
6179
Jens Axboe7271ef32020-08-10 09:55:22 -06006180static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006181{
Jens Axboe76a46e02019-11-10 23:34:16 -07006182 /*
6183 * If the list is now empty, then our linked request finished before
6184 * we got a chance to setup the timer
6185 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006186 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006187 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006188
Jens Axboead8a48a2019-11-15 08:49:11 -07006189 data->timer.function = io_link_timeout_fn;
6190 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6191 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006192 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006193}
6194
6195static void io_queue_linked_timeout(struct io_kiocb *req)
6196{
6197 struct io_ring_ctx *ctx = req->ctx;
6198
6199 spin_lock_irq(&ctx->completion_lock);
6200 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006201 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006202
Jens Axboe2665abf2019-11-05 12:40:47 -07006203 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006204 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006205}
6206
Jens Axboead8a48a2019-11-15 08:49:11 -07006207static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006208{
6209 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006210
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006211 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006212 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006213 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006214 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006215
Pavel Begunkov44932332019-12-05 16:16:35 +03006216 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6217 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006218 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006219 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006220
Jens Axboe76a46e02019-11-10 23:34:16 -07006221 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006222 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006223}
6224
Jens Axboef13fad72020-06-22 09:34:30 -06006225static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6226 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006227{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006228 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006229 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006230 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006231 int ret;
6232
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006233again:
6234 linked_timeout = io_prep_linked_timeout(req);
6235
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006236 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6237 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006238 if (old_creds)
6239 revert_creds(old_creds);
6240 if (old_creds == req->work.creds)
6241 old_creds = NULL; /* restored original creds */
6242 else
6243 old_creds = override_creds(req->work.creds);
6244 }
6245
Jens Axboef13fad72020-06-22 09:34:30 -06006246 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006247
6248 /*
6249 * We async punt it if the file wasn't marked NOWAIT, or if the file
6250 * doesn't support non-blocking read/write attempts
6251 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006252 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006253 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006254punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006255 ret = io_prep_work_files(req);
6256 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006257 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006258 /*
6259 * Queued up for async execution, worker will release
6260 * submit reference when the iocb is actually submitted.
6261 */
6262 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006263 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006264
Pavel Begunkovf063c542020-07-25 14:41:59 +03006265 if (linked_timeout)
6266 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006267 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006268 }
Jens Axboee65ef562019-03-12 10:16:44 -06006269
Pavel Begunkov652532a2020-07-03 22:15:07 +03006270 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006271err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006272 /* un-prep timeout, so it'll be killed as any other linked */
6273 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006274 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006275 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006276 io_req_complete(req, ret);
6277 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006278 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006279
Jens Axboe6c271ce2019-01-10 11:22:30 -07006280 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006281 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006282 if (linked_timeout)
6283 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006284
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006285 if (nxt) {
6286 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006287
6288 if (req->flags & REQ_F_FORCE_ASYNC)
6289 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006290 goto again;
6291 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006292exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006293 if (old_creds)
6294 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295}
6296
Jens Axboef13fad72020-06-22 09:34:30 -06006297static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6298 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006299{
6300 int ret;
6301
Jens Axboe3529d8c2019-12-19 18:24:38 -07006302 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006303 if (ret) {
6304 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006305fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006306 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006307 io_put_req(req);
6308 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006309 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006310 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006311 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006312 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006313 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006314 goto fail_req;
6315 }
6316
Jens Axboece35a472019-12-17 08:04:44 -07006317 /*
6318 * Never try inline submit of IOSQE_ASYNC is set, go straight
6319 * to async execution.
6320 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006321 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006322 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6323 io_queue_async_work(req);
6324 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006325 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006326 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006327}
6328
Jens Axboef13fad72020-06-22 09:34:30 -06006329static inline void io_queue_link_head(struct io_kiocb *req,
6330 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006331{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006332 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006333 io_put_req(req);
6334 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006335 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006336 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006337}
6338
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006339static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006340 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006341{
Jackie Liua197f662019-11-08 08:09:12 -07006342 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006343 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006344
Jens Axboe9e645e112019-05-10 16:07:28 -06006345 /*
6346 * If we already have a head request, queue this one for async
6347 * submittal once the head completes. If we don't have a head but
6348 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6349 * submitted sync once the chain is complete. If none of those
6350 * conditions are true (normal request), then just queue it.
6351 */
6352 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006353 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006354
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006355 /*
6356 * Taking sequential execution of a link, draining both sides
6357 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6358 * requests in the link. So, it drains the head and the
6359 * next after the link request. The last one is done via
6360 * drain_next flag to persist the effect across calls.
6361 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006362 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006363 head->flags |= REQ_F_IO_DRAIN;
6364 ctx->drain_next = 1;
6365 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006366 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006367 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006368 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006369 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006370 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006371 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006372 trace_io_uring_link(ctx, req, head);
6373 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006374
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006375 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006376 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006377 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006378 *link = NULL;
6379 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006380 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006381 if (unlikely(ctx->drain_next)) {
6382 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006383 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006384 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006385 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006386 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006387 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006388
Pavel Begunkov711be032020-01-17 03:57:59 +03006389 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006390 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006391 req->flags |= REQ_F_FAIL_LINK;
6392 *link = req;
6393 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006394 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006395 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006396 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006397
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006398 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006399}
6400
Jens Axboe9a56a232019-01-09 09:06:50 -07006401/*
6402 * Batched submission is done, ensure local IO is flushed out.
6403 */
6404static void io_submit_state_end(struct io_submit_state *state)
6405{
Jens Axboef13fad72020-06-22 09:34:30 -06006406 if (!list_empty(&state->comp.list))
6407 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006408 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006409 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006410 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006411 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006412}
6413
6414/*
6415 * Start submission side cache.
6416 */
6417static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006418 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006419{
6420 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006421 state->comp.nr = 0;
6422 INIT_LIST_HEAD(&state->comp.list);
6423 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006424 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006425 state->file = NULL;
6426 state->ios_left = max_ios;
6427}
6428
Jens Axboe2b188cc2019-01-07 10:46:33 -07006429static void io_commit_sqring(struct io_ring_ctx *ctx)
6430{
Hristo Venev75b28af2019-08-26 17:23:46 +00006431 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006433 /*
6434 * Ensure any loads from the SQEs are done at this point,
6435 * since once we write the new head, the application could
6436 * write new data to them.
6437 */
6438 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439}
6440
6441/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006442 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443 * that is mapped by userspace. This means that care needs to be taken to
6444 * ensure that reads are stable, as we cannot rely on userspace always
6445 * being a good citizen. If members of the sqe are validated and then later
6446 * used, it's important that those reads are done through READ_ONCE() to
6447 * prevent a re-load down the line.
6448 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006449static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450{
Hristo Venev75b28af2019-08-26 17:23:46 +00006451 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006452 unsigned head;
6453
6454 /*
6455 * The cached sq head (or cq tail) serves two purposes:
6456 *
6457 * 1) allows us to batch the cost of updating the user visible
6458 * head updates.
6459 * 2) allows the kernel side to track the head on its own, even
6460 * though the application is the one updating it.
6461 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006462 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006463 if (likely(head < ctx->sq_entries))
6464 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006465
6466 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006467 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006468 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006469 return NULL;
6470}
6471
6472static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6473{
6474 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006475}
6476
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006477/*
6478 * Check SQE restrictions (opcode and flags).
6479 *
6480 * Returns 'true' if SQE is allowed, 'false' otherwise.
6481 */
6482static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6483 struct io_kiocb *req,
6484 unsigned int sqe_flags)
6485{
6486 if (!ctx->restricted)
6487 return true;
6488
6489 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6490 return false;
6491
6492 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6493 ctx->restrictions.sqe_flags_required)
6494 return false;
6495
6496 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6497 ctx->restrictions.sqe_flags_required))
6498 return false;
6499
6500 return true;
6501}
6502
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006503#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6504 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6505 IOSQE_BUFFER_SELECT)
6506
6507static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6508 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006509 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006510{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006511 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006512 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006513
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006514 req->opcode = READ_ONCE(sqe->opcode);
6515 req->user_data = READ_ONCE(sqe->user_data);
6516 req->io = NULL;
6517 req->file = NULL;
6518 req->ctx = ctx;
6519 req->flags = 0;
6520 /* one is dropped after submission, the other at completion */
6521 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006522 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006523 get_task_struct(req->task);
Jens Axboe0f212202020-09-13 13:09:39 -06006524 atomic_long_inc(&req->task->io_uring->req_issue);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006525 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006526
6527 if (unlikely(req->opcode >= IORING_OP_LAST))
6528 return -EINVAL;
6529
Jens Axboe9d8426a2020-06-16 18:42:49 -06006530 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6531 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006532
6533 sqe_flags = READ_ONCE(sqe->flags);
6534 /* enforce forwards compatibility on users */
6535 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6536 return -EINVAL;
6537
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006538 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6539 return -EACCES;
6540
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006541 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6542 !io_op_defs[req->opcode].buffer_select)
6543 return -EOPNOTSUPP;
6544
6545 id = READ_ONCE(sqe->personality);
6546 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006547 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006548 req->work.creds = idr_find(&ctx->personality_idr, id);
6549 if (unlikely(!req->work.creds))
6550 return -EINVAL;
6551 get_cred(req->work.creds);
6552 }
6553
6554 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006555 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006556
Jens Axboe63ff8222020-05-07 14:56:15 -06006557 if (!io_op_defs[req->opcode].needs_file)
6558 return 0;
6559
6560 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006561}
6562
Jens Axboe0f212202020-09-13 13:09:39 -06006563static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006564{
Jens Axboeac8691c2020-06-01 08:30:41 -06006565 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006566 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006567 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006568
Jens Axboec4a2ed72019-11-21 21:01:26 -07006569 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006570 if (test_bit(0, &ctx->sq_check_overflow)) {
6571 if (!list_empty(&ctx->cq_overflow_list) &&
Jens Axboee6c8aa92020-09-28 13:10:13 -06006572 !io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006573 return -EBUSY;
6574 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006575
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006576 /* make sure SQ entry isn't read before tail */
6577 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006578
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006579 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6580 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006581
Jens Axboe013538b2020-06-22 09:29:15 -06006582 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006583
6584 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006585 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006586 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006587 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006588
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006589 sqe = io_get_sqe(ctx);
6590 if (unlikely(!sqe)) {
6591 io_consume_sqe(ctx);
6592 break;
6593 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006594 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006595 if (unlikely(!req)) {
6596 if (!submitted)
6597 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006598 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006599 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006600
Jens Axboeac8691c2020-06-01 08:30:41 -06006601 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006602 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006603 /* will complete beyond this point, count as submitted */
6604 submitted++;
6605
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006606 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006607fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006608 io_put_req(req);
6609 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006610 break;
6611 }
6612
Jens Axboe354420f2020-01-08 18:55:15 -07006613 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006614 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006615 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006616 if (err)
6617 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006618 }
6619
Pavel Begunkov9466f432020-01-25 22:34:01 +03006620 if (unlikely(submitted != nr)) {
6621 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6622
6623 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6624 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006625 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006626 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006627 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006628
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006629 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6630 io_commit_sqring(ctx);
6631
Jens Axboe6c271ce2019-01-10 11:22:30 -07006632 return submitted;
6633}
6634
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006635static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6636{
6637 /* Tell userspace we may need a wakeup call */
6638 spin_lock_irq(&ctx->completion_lock);
6639 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6640 spin_unlock_irq(&ctx->completion_lock);
6641}
6642
6643static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6644{
6645 spin_lock_irq(&ctx->completion_lock);
6646 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6647 spin_unlock_irq(&ctx->completion_lock);
6648}
6649
Jens Axboe3f0e64d2020-09-02 12:42:47 -06006650static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
6651 int sync, void *key)
6652{
6653 struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
6654 int ret;
6655
6656 ret = autoremove_wake_function(wqe, mode, sync, key);
6657 if (ret) {
6658 unsigned long flags;
6659
6660 spin_lock_irqsave(&ctx->completion_lock, flags);
6661 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6662 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6663 }
6664 return ret;
6665}
6666
Jens Axboec8d1ba52020-09-14 11:07:26 -06006667enum sq_ret {
6668 SQT_IDLE = 1,
6669 SQT_SPIN = 2,
6670 SQT_DID_WORK = 4,
6671};
6672
6673static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
6674 unsigned long start_jiffies)
6675{
6676 unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -06006677 struct io_sq_data *sqd = ctx->sq_data;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006678 unsigned int to_submit;
6679 int ret = 0;
6680
6681again:
6682 if (!list_empty(&ctx->iopoll_list)) {
6683 unsigned nr_events = 0;
6684
6685 mutex_lock(&ctx->uring_lock);
6686 if (!list_empty(&ctx->iopoll_list) && !need_resched())
6687 io_do_iopoll(ctx, &nr_events, 0);
6688 mutex_unlock(&ctx->uring_lock);
6689 }
6690
6691 to_submit = io_sqring_entries(ctx);
6692
6693 /*
6694 * If submit got -EBUSY, flag us as needing the application
6695 * to enter the kernel to reap and flush events.
6696 */
6697 if (!to_submit || ret == -EBUSY || need_resched()) {
6698 /*
6699 * Drop cur_mm before scheduling, we can't hold it for
6700 * long periods (or over schedule()). Do this before
6701 * adding ourselves to the waitqueue, as the unuse/drop
6702 * may sleep.
6703 */
6704 io_sq_thread_drop_mm();
6705
6706 /*
6707 * We're polling. If we're within the defined idle
6708 * period, then let us spin without work before going
6709 * to sleep. The exception is if we got EBUSY doing
6710 * more IO, we should wait for the application to
6711 * reap events and wake us up.
6712 */
6713 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
6714 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6715 !percpu_ref_is_dying(&ctx->refs)))
6716 return SQT_SPIN;
6717
Jens Axboe534ca6d2020-09-02 13:52:19 -06006718 prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
Jens Axboec8d1ba52020-09-14 11:07:26 -06006719 TASK_INTERRUPTIBLE);
6720
6721 /*
6722 * While doing polled IO, before going to sleep, we need
6723 * to check if there are new reqs added to iopoll_list,
6724 * it is because reqs may have been punted to io worker
6725 * and will be added to iopoll_list later, hence check
6726 * the iopoll_list again.
6727 */
6728 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6729 !list_empty_careful(&ctx->iopoll_list)) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06006730 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006731 goto again;
6732 }
6733
Jens Axboec8d1ba52020-09-14 11:07:26 -06006734 to_submit = io_sqring_entries(ctx);
6735 if (!to_submit || ret == -EBUSY)
6736 return SQT_IDLE;
6737 }
6738
Jens Axboe534ca6d2020-09-02 13:52:19 -06006739 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
Jens Axboec8d1ba52020-09-14 11:07:26 -06006740 io_ring_clear_wakeup_flag(ctx);
6741
6742 mutex_lock(&ctx->uring_lock);
6743 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6744 ret = io_submit_sqes(ctx, to_submit);
6745 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006746
6747 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6748 wake_up(&ctx->sqo_sq_wait);
6749
Jens Axboec8d1ba52020-09-14 11:07:26 -06006750 return SQT_DID_WORK;
6751}
6752
Jens Axboe69fb2132020-09-14 11:16:23 -06006753static void io_sqd_init_new(struct io_sq_data *sqd)
6754{
6755 struct io_ring_ctx *ctx;
6756
6757 while (!list_empty(&sqd->ctx_new_list)) {
6758 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
6759 init_wait(&ctx->sqo_wait_entry);
6760 ctx->sqo_wait_entry.func = io_sq_wake_function;
6761 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6762 complete(&ctx->sq_thread_comp);
6763 }
6764}
6765
Jens Axboe6c271ce2019-01-10 11:22:30 -07006766static int io_sq_thread(void *data)
6767{
Jens Axboe69fb2132020-09-14 11:16:23 -06006768 const struct cred *old_cred = NULL;
6769 struct io_sq_data *sqd = data;
6770 struct io_ring_ctx *ctx;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006771 unsigned long start_jiffies;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006772
Jens Axboec8d1ba52020-09-14 11:07:26 -06006773 start_jiffies = jiffies;
Jens Axboe69fb2132020-09-14 11:16:23 -06006774 while (!kthread_should_stop()) {
6775 enum sq_ret ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006776
Jens Axboe69fb2132020-09-14 11:16:23 -06006777 /*
6778 * Any changes to the sqd lists are synchronized through the
6779 * kthread parking. This synchronizes the thread vs users,
6780 * the users are synchronized on the sqd->ctx_lock.
6781 */
6782 if (kthread_should_park())
6783 kthread_parkme();
6784
6785 if (unlikely(!list_empty(&sqd->ctx_new_list)))
6786 io_sqd_init_new(sqd);
6787
6788 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6789 if (current->cred != ctx->creds) {
6790 if (old_cred)
6791 revert_creds(old_cred);
6792 old_cred = override_creds(ctx->creds);
6793 }
6794
6795 ret |= __io_sq_thread(ctx, start_jiffies);
6796
6797 io_sq_thread_drop_mm();
6798 }
6799
6800 if (ret & SQT_SPIN) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006801 io_run_task_work();
6802 cond_resched();
Jens Axboe69fb2132020-09-14 11:16:23 -06006803 } else if (ret == SQT_IDLE) {
6804 if (kthread_should_park())
6805 continue;
6806 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6807 io_ring_set_wakeup_flag(ctx);
6808 schedule();
6809 start_jiffies = jiffies;
6810 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6811 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006812 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006813 }
6814
Jens Axboe4c6e2772020-07-01 11:29:10 -06006815 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006816
Jens Axboe69fb2132020-09-14 11:16:23 -06006817 if (old_cred)
6818 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006819
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006820 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006821
Jens Axboe6c271ce2019-01-10 11:22:30 -07006822 return 0;
6823}
6824
Jens Axboebda52162019-09-24 13:47:15 -06006825struct io_wait_queue {
6826 struct wait_queue_entry wq;
6827 struct io_ring_ctx *ctx;
6828 unsigned to_wait;
6829 unsigned nr_timeouts;
6830};
6831
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006832static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006833{
6834 struct io_ring_ctx *ctx = iowq->ctx;
6835
6836 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006837 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006838 * started waiting. For timeouts, we always want to return to userspace,
6839 * regardless of event count.
6840 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006841 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006842 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6843}
6844
6845static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6846 int wake_flags, void *key)
6847{
6848 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6849 wq);
6850
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006851 /* use noflush == true, as we can't safely rely on locking context */
6852 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006853 return -1;
6854
6855 return autoremove_wake_function(curr, mode, wake_flags, key);
6856}
6857
Jens Axboe2b188cc2019-01-07 10:46:33 -07006858/*
6859 * Wait until events become available, if we don't already have some. The
6860 * application must reap them itself, as they reside on the shared cq ring.
6861 */
6862static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6863 const sigset_t __user *sig, size_t sigsz)
6864{
Jens Axboebda52162019-09-24 13:47:15 -06006865 struct io_wait_queue iowq = {
6866 .wq = {
6867 .private = current,
6868 .func = io_wake_function,
6869 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6870 },
6871 .ctx = ctx,
6872 .to_wait = min_events,
6873 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006874 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006875 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006876
Jens Axboeb41e9852020-02-17 09:52:41 -07006877 do {
6878 if (io_cqring_events(ctx, false) >= min_events)
6879 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006880 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006881 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006882 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006883
6884 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006885#ifdef CONFIG_COMPAT
6886 if (in_compat_syscall())
6887 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006888 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006889 else
6890#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006891 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006892
Jens Axboe2b188cc2019-01-07 10:46:33 -07006893 if (ret)
6894 return ret;
6895 }
6896
Jens Axboebda52162019-09-24 13:47:15 -06006897 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006898 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006899 do {
6900 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6901 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006902 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006903 if (io_run_task_work())
6904 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006905 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006906 if (current->jobctl & JOBCTL_TASK_WORK) {
6907 spin_lock_irq(&current->sighand->siglock);
6908 current->jobctl &= ~JOBCTL_TASK_WORK;
6909 recalc_sigpending();
6910 spin_unlock_irq(&current->sighand->siglock);
6911 continue;
6912 }
6913 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006914 break;
6915 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006916 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006917 break;
6918 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006919 } while (1);
6920 finish_wait(&ctx->wait, &iowq.wq);
6921
Jens Axboeb7db41c2020-07-04 08:55:50 -06006922 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006923
Hristo Venev75b28af2019-08-26 17:23:46 +00006924 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006925}
6926
Jens Axboe6b063142019-01-10 22:13:58 -07006927static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6928{
6929#if defined(CONFIG_UNIX)
6930 if (ctx->ring_sock) {
6931 struct sock *sock = ctx->ring_sock->sk;
6932 struct sk_buff *skb;
6933
6934 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6935 kfree_skb(skb);
6936 }
6937#else
6938 int i;
6939
Jens Axboe65e19f52019-10-26 07:20:21 -06006940 for (i = 0; i < ctx->nr_user_files; i++) {
6941 struct file *file;
6942
6943 file = io_file_from_index(ctx, i);
6944 if (file)
6945 fput(file);
6946 }
Jens Axboe6b063142019-01-10 22:13:58 -07006947#endif
6948}
6949
Jens Axboe05f3fb32019-12-09 11:22:50 -07006950static void io_file_ref_kill(struct percpu_ref *ref)
6951{
6952 struct fixed_file_data *data;
6953
6954 data = container_of(ref, struct fixed_file_data, refs);
6955 complete(&data->done);
6956}
6957
Jens Axboe6b063142019-01-10 22:13:58 -07006958static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6959{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006960 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006961 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006962 unsigned nr_tables, i;
6963
Jens Axboe05f3fb32019-12-09 11:22:50 -07006964 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006965 return -ENXIO;
6966
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006967 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006968 if (!list_empty(&data->ref_list))
6969 ref_node = list_first_entry(&data->ref_list,
6970 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006971 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006972 if (ref_node)
6973 percpu_ref_kill(&ref_node->refs);
6974
6975 percpu_ref_kill(&data->refs);
6976
6977 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006978 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006979 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006980
Jens Axboe6b063142019-01-10 22:13:58 -07006981 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006982 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6983 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006984 kfree(data->table[i].files);
6985 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006986 percpu_ref_exit(&data->refs);
6987 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006988 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006989 ctx->nr_user_files = 0;
6990 return 0;
6991}
6992
Jens Axboe534ca6d2020-09-02 13:52:19 -06006993static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006994{
Jens Axboe534ca6d2020-09-02 13:52:19 -06006995 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006996 /*
6997 * The park is a bit of a work-around, without it we get
6998 * warning spews on shutdown with SQPOLL set and affinity
6999 * set to a single CPU.
7000 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007001 if (sqd->thread) {
7002 kthread_park(sqd->thread);
7003 kthread_stop(sqd->thread);
7004 }
7005
7006 kfree(sqd);
7007 }
7008}
7009
Jens Axboeaa061652020-09-02 14:50:27 -06007010static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7011{
7012 struct io_ring_ctx *ctx_attach;
7013 struct io_sq_data *sqd;
7014 struct fd f;
7015
7016 f = fdget(p->wq_fd);
7017 if (!f.file)
7018 return ERR_PTR(-ENXIO);
7019 if (f.file->f_op != &io_uring_fops) {
7020 fdput(f);
7021 return ERR_PTR(-EINVAL);
7022 }
7023
7024 ctx_attach = f.file->private_data;
7025 sqd = ctx_attach->sq_data;
7026 if (!sqd) {
7027 fdput(f);
7028 return ERR_PTR(-EINVAL);
7029 }
7030
7031 refcount_inc(&sqd->refs);
7032 fdput(f);
7033 return sqd;
7034}
7035
Jens Axboe534ca6d2020-09-02 13:52:19 -06007036static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7037{
7038 struct io_sq_data *sqd;
7039
Jens Axboeaa061652020-09-02 14:50:27 -06007040 if (p->flags & IORING_SETUP_ATTACH_WQ)
7041 return io_attach_sq_data(p);
7042
Jens Axboe534ca6d2020-09-02 13:52:19 -06007043 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7044 if (!sqd)
7045 return ERR_PTR(-ENOMEM);
7046
7047 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007048 INIT_LIST_HEAD(&sqd->ctx_list);
7049 INIT_LIST_HEAD(&sqd->ctx_new_list);
7050 mutex_init(&sqd->ctx_lock);
7051 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007052 init_waitqueue_head(&sqd->wait);
7053 return sqd;
7054}
7055
Jens Axboe69fb2132020-09-14 11:16:23 -06007056static void io_sq_thread_unpark(struct io_sq_data *sqd)
7057 __releases(&sqd->lock)
7058{
7059 if (!sqd->thread)
7060 return;
7061 kthread_unpark(sqd->thread);
7062 mutex_unlock(&sqd->lock);
7063}
7064
7065static void io_sq_thread_park(struct io_sq_data *sqd)
7066 __acquires(&sqd->lock)
7067{
7068 if (!sqd->thread)
7069 return;
7070 mutex_lock(&sqd->lock);
7071 kthread_park(sqd->thread);
7072}
7073
Jens Axboe534ca6d2020-09-02 13:52:19 -06007074static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7075{
7076 struct io_sq_data *sqd = ctx->sq_data;
7077
7078 if (sqd) {
7079 if (sqd->thread) {
7080 /*
7081 * We may arrive here from the error branch in
7082 * io_sq_offload_create() where the kthread is created
7083 * without being waked up, thus wake it up now to make
7084 * sure the wait will complete.
7085 */
7086 wake_up_process(sqd->thread);
7087 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007088
7089 io_sq_thread_park(sqd);
7090 }
7091
7092 mutex_lock(&sqd->ctx_lock);
7093 list_del(&ctx->sqd_list);
7094 mutex_unlock(&sqd->ctx_lock);
7095
7096 if (sqd->thread) {
7097 finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
7098 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007099 }
7100
7101 io_put_sq_data(sqd);
7102 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007103 }
7104}
7105
Jens Axboe6b063142019-01-10 22:13:58 -07007106static void io_finish_async(struct io_ring_ctx *ctx)
7107{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007108 io_sq_thread_stop(ctx);
7109
Jens Axboe561fb042019-10-24 07:25:42 -06007110 if (ctx->io_wq) {
7111 io_wq_destroy(ctx->io_wq);
7112 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007113 }
7114}
7115
7116#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007117/*
7118 * Ensure the UNIX gc is aware of our file set, so we are certain that
7119 * the io_uring can be safely unregistered on process exit, even if we have
7120 * loops in the file referencing.
7121 */
7122static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7123{
7124 struct sock *sk = ctx->ring_sock->sk;
7125 struct scm_fp_list *fpl;
7126 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007127 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007128
Jens Axboe6b063142019-01-10 22:13:58 -07007129 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7130 if (!fpl)
7131 return -ENOMEM;
7132
7133 skb = alloc_skb(0, GFP_KERNEL);
7134 if (!skb) {
7135 kfree(fpl);
7136 return -ENOMEM;
7137 }
7138
7139 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007140
Jens Axboe08a45172019-10-03 08:11:03 -06007141 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007142 fpl->user = get_uid(ctx->user);
7143 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007144 struct file *file = io_file_from_index(ctx, i + offset);
7145
7146 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007147 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007148 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007149 unix_inflight(fpl->user, fpl->fp[nr_files]);
7150 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007151 }
7152
Jens Axboe08a45172019-10-03 08:11:03 -06007153 if (nr_files) {
7154 fpl->max = SCM_MAX_FD;
7155 fpl->count = nr_files;
7156 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007157 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007158 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7159 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007160
Jens Axboe08a45172019-10-03 08:11:03 -06007161 for (i = 0; i < nr_files; i++)
7162 fput(fpl->fp[i]);
7163 } else {
7164 kfree_skb(skb);
7165 kfree(fpl);
7166 }
Jens Axboe6b063142019-01-10 22:13:58 -07007167
7168 return 0;
7169}
7170
7171/*
7172 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7173 * causes regular reference counting to break down. We rely on the UNIX
7174 * garbage collection to take care of this problem for us.
7175 */
7176static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7177{
7178 unsigned left, total;
7179 int ret = 0;
7180
7181 total = 0;
7182 left = ctx->nr_user_files;
7183 while (left) {
7184 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007185
7186 ret = __io_sqe_files_scm(ctx, this_files, total);
7187 if (ret)
7188 break;
7189 left -= this_files;
7190 total += this_files;
7191 }
7192
7193 if (!ret)
7194 return 0;
7195
7196 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007197 struct file *file = io_file_from_index(ctx, total);
7198
7199 if (file)
7200 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007201 total++;
7202 }
7203
7204 return ret;
7205}
7206#else
7207static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7208{
7209 return 0;
7210}
7211#endif
7212
Jens Axboe65e19f52019-10-26 07:20:21 -06007213static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
7214 unsigned nr_files)
7215{
7216 int i;
7217
7218 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007219 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007220 unsigned this_files;
7221
7222 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7223 table->files = kcalloc(this_files, sizeof(struct file *),
7224 GFP_KERNEL);
7225 if (!table->files)
7226 break;
7227 nr_files -= this_files;
7228 }
7229
7230 if (i == nr_tables)
7231 return 0;
7232
7233 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007234 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007235 kfree(table->files);
7236 }
7237 return 1;
7238}
7239
Jens Axboe05f3fb32019-12-09 11:22:50 -07007240static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007241{
7242#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007243 struct sock *sock = ctx->ring_sock->sk;
7244 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7245 struct sk_buff *skb;
7246 int i;
7247
7248 __skb_queue_head_init(&list);
7249
7250 /*
7251 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7252 * remove this entry and rearrange the file array.
7253 */
7254 skb = skb_dequeue(head);
7255 while (skb) {
7256 struct scm_fp_list *fp;
7257
7258 fp = UNIXCB(skb).fp;
7259 for (i = 0; i < fp->count; i++) {
7260 int left;
7261
7262 if (fp->fp[i] != file)
7263 continue;
7264
7265 unix_notinflight(fp->user, fp->fp[i]);
7266 left = fp->count - 1 - i;
7267 if (left) {
7268 memmove(&fp->fp[i], &fp->fp[i + 1],
7269 left * sizeof(struct file *));
7270 }
7271 fp->count--;
7272 if (!fp->count) {
7273 kfree_skb(skb);
7274 skb = NULL;
7275 } else {
7276 __skb_queue_tail(&list, skb);
7277 }
7278 fput(file);
7279 file = NULL;
7280 break;
7281 }
7282
7283 if (!file)
7284 break;
7285
7286 __skb_queue_tail(&list, skb);
7287
7288 skb = skb_dequeue(head);
7289 }
7290
7291 if (skb_peek(&list)) {
7292 spin_lock_irq(&head->lock);
7293 while ((skb = __skb_dequeue(&list)) != NULL)
7294 __skb_queue_tail(head, skb);
7295 spin_unlock_irq(&head->lock);
7296 }
7297#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007298 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007299#endif
7300}
7301
Jens Axboe05f3fb32019-12-09 11:22:50 -07007302struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007303 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007304 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007305};
7306
Jens Axboe4a38aed22020-05-14 17:21:15 -06007307static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007308{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007309 struct fixed_file_data *file_data = ref_node->file_data;
7310 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007311 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007312
7313 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007314 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007315 io_ring_file_put(ctx, pfile->file);
7316 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007317 }
7318
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007319 spin_lock(&file_data->lock);
7320 list_del(&ref_node->node);
7321 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007322
Xiaoguang Wang05589552020-03-31 14:05:18 +08007323 percpu_ref_exit(&ref_node->refs);
7324 kfree(ref_node);
7325 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007326}
7327
Jens Axboe4a38aed22020-05-14 17:21:15 -06007328static void io_file_put_work(struct work_struct *work)
7329{
7330 struct io_ring_ctx *ctx;
7331 struct llist_node *node;
7332
7333 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7334 node = llist_del_all(&ctx->file_put_llist);
7335
7336 while (node) {
7337 struct fixed_file_ref_node *ref_node;
7338 struct llist_node *next = node->next;
7339
7340 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7341 __io_file_put_work(ref_node);
7342 node = next;
7343 }
7344}
7345
Jens Axboe05f3fb32019-12-09 11:22:50 -07007346static void io_file_data_ref_zero(struct percpu_ref *ref)
7347{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007348 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007349 struct io_ring_ctx *ctx;
7350 bool first_add;
7351 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007352
Xiaoguang Wang05589552020-03-31 14:05:18 +08007353 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007354 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007355
Jens Axboe4a38aed22020-05-14 17:21:15 -06007356 if (percpu_ref_is_dying(&ctx->file_data->refs))
7357 delay = 0;
7358
7359 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7360 if (!delay)
7361 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7362 else if (first_add)
7363 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007364}
7365
7366static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7367 struct io_ring_ctx *ctx)
7368{
7369 struct fixed_file_ref_node *ref_node;
7370
7371 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7372 if (!ref_node)
7373 return ERR_PTR(-ENOMEM);
7374
7375 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7376 0, GFP_KERNEL)) {
7377 kfree(ref_node);
7378 return ERR_PTR(-ENOMEM);
7379 }
7380 INIT_LIST_HEAD(&ref_node->node);
7381 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007382 ref_node->file_data = ctx->file_data;
7383 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007384}
7385
7386static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7387{
7388 percpu_ref_exit(&ref_node->refs);
7389 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007390}
7391
7392static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7393 unsigned nr_args)
7394{
7395 __s32 __user *fds = (__s32 __user *) arg;
7396 unsigned nr_tables;
7397 struct file *file;
7398 int fd, ret = 0;
7399 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007400 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007401
7402 if (ctx->file_data)
7403 return -EBUSY;
7404 if (!nr_args)
7405 return -EINVAL;
7406 if (nr_args > IORING_MAX_FIXED_FILES)
7407 return -EMFILE;
7408
7409 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7410 if (!ctx->file_data)
7411 return -ENOMEM;
7412 ctx->file_data->ctx = ctx;
7413 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007414 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007415 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007416
7417 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7418 ctx->file_data->table = kcalloc(nr_tables,
7419 sizeof(struct fixed_file_table),
7420 GFP_KERNEL);
7421 if (!ctx->file_data->table) {
7422 kfree(ctx->file_data);
7423 ctx->file_data = NULL;
7424 return -ENOMEM;
7425 }
7426
Xiaoguang Wang05589552020-03-31 14:05:18 +08007427 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007428 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7429 kfree(ctx->file_data->table);
7430 kfree(ctx->file_data);
7431 ctx->file_data = NULL;
7432 return -ENOMEM;
7433 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007434
7435 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7436 percpu_ref_exit(&ctx->file_data->refs);
7437 kfree(ctx->file_data->table);
7438 kfree(ctx->file_data);
7439 ctx->file_data = NULL;
7440 return -ENOMEM;
7441 }
7442
7443 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7444 struct fixed_file_table *table;
7445 unsigned index;
7446
7447 ret = -EFAULT;
7448 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7449 break;
7450 /* allow sparse sets */
7451 if (fd == -1) {
7452 ret = 0;
7453 continue;
7454 }
7455
7456 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7457 index = i & IORING_FILE_TABLE_MASK;
7458 file = fget(fd);
7459
7460 ret = -EBADF;
7461 if (!file)
7462 break;
7463
7464 /*
7465 * Don't allow io_uring instances to be registered. If UNIX
7466 * isn't enabled, then this causes a reference cycle and this
7467 * instance can never get freed. If UNIX is enabled we'll
7468 * handle it just fine, but there's still no point in allowing
7469 * a ring fd as it doesn't support regular read/write anyway.
7470 */
7471 if (file->f_op == &io_uring_fops) {
7472 fput(file);
7473 break;
7474 }
7475 ret = 0;
7476 table->files[index] = file;
7477 }
7478
7479 if (ret) {
7480 for (i = 0; i < ctx->nr_user_files; i++) {
7481 file = io_file_from_index(ctx, i);
7482 if (file)
7483 fput(file);
7484 }
7485 for (i = 0; i < nr_tables; i++)
7486 kfree(ctx->file_data->table[i].files);
7487
Yang Yingliang667e57d2020-07-10 14:14:20 +00007488 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007489 kfree(ctx->file_data->table);
7490 kfree(ctx->file_data);
7491 ctx->file_data = NULL;
7492 ctx->nr_user_files = 0;
7493 return ret;
7494 }
7495
7496 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007497 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007498 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007499 return ret;
7500 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007501
Xiaoguang Wang05589552020-03-31 14:05:18 +08007502 ref_node = alloc_fixed_file_ref_node(ctx);
7503 if (IS_ERR(ref_node)) {
7504 io_sqe_files_unregister(ctx);
7505 return PTR_ERR(ref_node);
7506 }
7507
7508 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007509 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007510 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007511 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007512 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007513 return ret;
7514}
7515
Jens Axboec3a31e62019-10-03 13:59:56 -06007516static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7517 int index)
7518{
7519#if defined(CONFIG_UNIX)
7520 struct sock *sock = ctx->ring_sock->sk;
7521 struct sk_buff_head *head = &sock->sk_receive_queue;
7522 struct sk_buff *skb;
7523
7524 /*
7525 * See if we can merge this file into an existing skb SCM_RIGHTS
7526 * file set. If there's no room, fall back to allocating a new skb
7527 * and filling it in.
7528 */
7529 spin_lock_irq(&head->lock);
7530 skb = skb_peek(head);
7531 if (skb) {
7532 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7533
7534 if (fpl->count < SCM_MAX_FD) {
7535 __skb_unlink(skb, head);
7536 spin_unlock_irq(&head->lock);
7537 fpl->fp[fpl->count] = get_file(file);
7538 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7539 fpl->count++;
7540 spin_lock_irq(&head->lock);
7541 __skb_queue_head(head, skb);
7542 } else {
7543 skb = NULL;
7544 }
7545 }
7546 spin_unlock_irq(&head->lock);
7547
7548 if (skb) {
7549 fput(file);
7550 return 0;
7551 }
7552
7553 return __io_sqe_files_scm(ctx, 1, index);
7554#else
7555 return 0;
7556#endif
7557}
7558
Hillf Dantona5318d32020-03-23 17:47:15 +08007559static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007560 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007561{
Hillf Dantona5318d32020-03-23 17:47:15 +08007562 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007563 struct percpu_ref *refs = data->cur_refs;
7564 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007565
Jens Axboe05f3fb32019-12-09 11:22:50 -07007566 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007567 if (!pfile)
7568 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007569
Xiaoguang Wang05589552020-03-31 14:05:18 +08007570 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007571 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007572 list_add(&pfile->list, &ref_node->file_list);
7573
Hillf Dantona5318d32020-03-23 17:47:15 +08007574 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007575}
7576
7577static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7578 struct io_uring_files_update *up,
7579 unsigned nr_args)
7580{
7581 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007582 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007583 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007584 __s32 __user *fds;
7585 int fd, i, err;
7586 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007587 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007588
Jens Axboe05f3fb32019-12-09 11:22:50 -07007589 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007590 return -EOVERFLOW;
7591 if (done > ctx->nr_user_files)
7592 return -EINVAL;
7593
Xiaoguang Wang05589552020-03-31 14:05:18 +08007594 ref_node = alloc_fixed_file_ref_node(ctx);
7595 if (IS_ERR(ref_node))
7596 return PTR_ERR(ref_node);
7597
Jens Axboec3a31e62019-10-03 13:59:56 -06007598 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007599 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007600 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007601 struct fixed_file_table *table;
7602 unsigned index;
7603
Jens Axboec3a31e62019-10-03 13:59:56 -06007604 err = 0;
7605 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7606 err = -EFAULT;
7607 break;
7608 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007609 i = array_index_nospec(up->offset, ctx->nr_user_files);
7610 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007611 index = i & IORING_FILE_TABLE_MASK;
7612 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007613 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007614 err = io_queue_file_removal(data, file);
7615 if (err)
7616 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007617 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007618 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007619 }
7620 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007621 file = fget(fd);
7622 if (!file) {
7623 err = -EBADF;
7624 break;
7625 }
7626 /*
7627 * Don't allow io_uring instances to be registered. If
7628 * UNIX isn't enabled, then this causes a reference
7629 * cycle and this instance can never get freed. If UNIX
7630 * is enabled we'll handle it just fine, but there's
7631 * still no point in allowing a ring fd as it doesn't
7632 * support regular read/write anyway.
7633 */
7634 if (file->f_op == &io_uring_fops) {
7635 fput(file);
7636 err = -EBADF;
7637 break;
7638 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007639 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007640 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007641 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007642 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007643 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007644 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007645 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007646 }
7647 nr_args--;
7648 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007649 up->offset++;
7650 }
7651
Xiaoguang Wang05589552020-03-31 14:05:18 +08007652 if (needs_switch) {
7653 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007654 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007655 list_add(&ref_node->node, &data->ref_list);
7656 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007657 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007658 percpu_ref_get(&ctx->file_data->refs);
7659 } else
7660 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007661
7662 return done ? done : err;
7663}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007664
Jens Axboe05f3fb32019-12-09 11:22:50 -07007665static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7666 unsigned nr_args)
7667{
7668 struct io_uring_files_update up;
7669
7670 if (!ctx->file_data)
7671 return -ENXIO;
7672 if (!nr_args)
7673 return -EINVAL;
7674 if (copy_from_user(&up, arg, sizeof(up)))
7675 return -EFAULT;
7676 if (up.resv)
7677 return -EINVAL;
7678
7679 return __io_sqe_files_update(ctx, &up, nr_args);
7680}
Jens Axboec3a31e62019-10-03 13:59:56 -06007681
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007682static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007683{
7684 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7685
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007686 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007687 io_put_req(req);
7688}
7689
Pavel Begunkov24369c22020-01-28 03:15:48 +03007690static int io_init_wq_offload(struct io_ring_ctx *ctx,
7691 struct io_uring_params *p)
7692{
7693 struct io_wq_data data;
7694 struct fd f;
7695 struct io_ring_ctx *ctx_attach;
7696 unsigned int concurrency;
7697 int ret = 0;
7698
7699 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007700 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007701 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007702
7703 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7704 /* Do QD, or 4 * CPUS, whatever is smallest */
7705 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7706
7707 ctx->io_wq = io_wq_create(concurrency, &data);
7708 if (IS_ERR(ctx->io_wq)) {
7709 ret = PTR_ERR(ctx->io_wq);
7710 ctx->io_wq = NULL;
7711 }
7712 return ret;
7713 }
7714
7715 f = fdget(p->wq_fd);
7716 if (!f.file)
7717 return -EBADF;
7718
7719 if (f.file->f_op != &io_uring_fops) {
7720 ret = -EINVAL;
7721 goto out_fput;
7722 }
7723
7724 ctx_attach = f.file->private_data;
7725 /* @io_wq is protected by holding the fd */
7726 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7727 ret = -EINVAL;
7728 goto out_fput;
7729 }
7730
7731 ctx->io_wq = ctx_attach->io_wq;
7732out_fput:
7733 fdput(f);
7734 return ret;
7735}
7736
Jens Axboe0f212202020-09-13 13:09:39 -06007737static int io_uring_alloc_task_context(struct task_struct *task)
7738{
7739 struct io_uring_task *tctx;
7740
7741 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7742 if (unlikely(!tctx))
7743 return -ENOMEM;
7744
7745 xa_init(&tctx->xa);
7746 init_waitqueue_head(&tctx->wait);
7747 tctx->last = NULL;
7748 tctx->in_idle = 0;
7749 atomic_long_set(&tctx->req_issue, 0);
7750 atomic_long_set(&tctx->req_complete, 0);
7751 task->io_uring = tctx;
7752 return 0;
7753}
7754
7755void __io_uring_free(struct task_struct *tsk)
7756{
7757 struct io_uring_task *tctx = tsk->io_uring;
7758
7759 WARN_ON_ONCE(!xa_empty(&tctx->xa));
7760 xa_destroy(&tctx->xa);
7761 kfree(tctx);
7762 tsk->io_uring = NULL;
7763}
7764
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007765static int io_sq_offload_create(struct io_ring_ctx *ctx,
7766 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007767{
7768 int ret;
7769
Jens Axboe6c271ce2019-01-10 11:22:30 -07007770 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007771 struct io_sq_data *sqd;
7772
Jens Axboe3ec482d2019-04-08 10:51:01 -06007773 ret = -EPERM;
7774 if (!capable(CAP_SYS_ADMIN))
7775 goto err;
7776
Jens Axboe534ca6d2020-09-02 13:52:19 -06007777 sqd = io_get_sq_data(p);
7778 if (IS_ERR(sqd)) {
7779 ret = PTR_ERR(sqd);
7780 goto err;
7781 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007782
Jens Axboe534ca6d2020-09-02 13:52:19 -06007783 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007784 io_sq_thread_park(sqd);
7785 mutex_lock(&sqd->ctx_lock);
7786 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7787 mutex_unlock(&sqd->ctx_lock);
7788 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007789
Jens Axboe917257d2019-04-13 09:28:55 -06007790 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7791 if (!ctx->sq_thread_idle)
7792 ctx->sq_thread_idle = HZ;
7793
Jens Axboeaa061652020-09-02 14:50:27 -06007794 if (sqd->thread)
7795 goto done;
7796
Jens Axboe6c271ce2019-01-10 11:22:30 -07007797 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007798 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007799
Jens Axboe917257d2019-04-13 09:28:55 -06007800 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007801 if (cpu >= nr_cpu_ids)
7802 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007803 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007804 goto err;
7805
Jens Axboe69fb2132020-09-14 11:16:23 -06007806 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007807 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007808 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007809 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007810 "io_uring-sq");
7811 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007812 if (IS_ERR(sqd->thread)) {
7813 ret = PTR_ERR(sqd->thread);
7814 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007815 goto err;
7816 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007817 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06007818 if (ret)
7819 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007820 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7821 /* Can't have SQ_AFF without SQPOLL */
7822 ret = -EINVAL;
7823 goto err;
7824 }
7825
Jens Axboeaa061652020-09-02 14:50:27 -06007826done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03007827 ret = io_init_wq_offload(ctx, p);
7828 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007829 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007830
7831 return 0;
7832err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007833 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007834 return ret;
7835}
7836
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007837static void io_sq_offload_start(struct io_ring_ctx *ctx)
7838{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007839 struct io_sq_data *sqd = ctx->sq_data;
7840
7841 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
7842 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007843}
7844
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007845static inline void __io_unaccount_mem(struct user_struct *user,
7846 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007847{
7848 atomic_long_sub(nr_pages, &user->locked_vm);
7849}
7850
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007851static inline int __io_account_mem(struct user_struct *user,
7852 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007853{
7854 unsigned long page_limit, cur_pages, new_pages;
7855
7856 /* Don't allow more pages than we can safely lock */
7857 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7858
7859 do {
7860 cur_pages = atomic_long_read(&user->locked_vm);
7861 new_pages = cur_pages + nr_pages;
7862 if (new_pages > page_limit)
7863 return -ENOMEM;
7864 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7865 new_pages) != cur_pages);
7866
7867 return 0;
7868}
7869
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007870static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7871 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007872{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007873 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007874 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007875
Jens Axboe2aede0e2020-09-14 10:45:53 -06007876 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007877 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007878 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007879 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007880 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007881 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007882}
7883
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007884static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7885 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007886{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007887 int ret;
7888
7889 if (ctx->limit_mem) {
7890 ret = __io_account_mem(ctx->user, nr_pages);
7891 if (ret)
7892 return ret;
7893 }
7894
Jens Axboe2aede0e2020-09-14 10:45:53 -06007895 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007896 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007897 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007898 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007899 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007900 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007901
7902 return 0;
7903}
7904
Jens Axboe2b188cc2019-01-07 10:46:33 -07007905static void io_mem_free(void *ptr)
7906{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007907 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007908
Mark Rutland52e04ef2019-04-30 17:30:21 +01007909 if (!ptr)
7910 return;
7911
7912 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007913 if (put_page_testzero(page))
7914 free_compound_page(page);
7915}
7916
7917static void *io_mem_alloc(size_t size)
7918{
7919 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7920 __GFP_NORETRY;
7921
7922 return (void *) __get_free_pages(gfp_flags, get_order(size));
7923}
7924
Hristo Venev75b28af2019-08-26 17:23:46 +00007925static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7926 size_t *sq_offset)
7927{
7928 struct io_rings *rings;
7929 size_t off, sq_array_size;
7930
7931 off = struct_size(rings, cqes, cq_entries);
7932 if (off == SIZE_MAX)
7933 return SIZE_MAX;
7934
7935#ifdef CONFIG_SMP
7936 off = ALIGN(off, SMP_CACHE_BYTES);
7937 if (off == 0)
7938 return SIZE_MAX;
7939#endif
7940
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007941 if (sq_offset)
7942 *sq_offset = off;
7943
Hristo Venev75b28af2019-08-26 17:23:46 +00007944 sq_array_size = array_size(sizeof(u32), sq_entries);
7945 if (sq_array_size == SIZE_MAX)
7946 return SIZE_MAX;
7947
7948 if (check_add_overflow(off, sq_array_size, &off))
7949 return SIZE_MAX;
7950
Hristo Venev75b28af2019-08-26 17:23:46 +00007951 return off;
7952}
7953
Jens Axboe2b188cc2019-01-07 10:46:33 -07007954static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7955{
Hristo Venev75b28af2019-08-26 17:23:46 +00007956 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007957
Hristo Venev75b28af2019-08-26 17:23:46 +00007958 pages = (size_t)1 << get_order(
7959 rings_size(sq_entries, cq_entries, NULL));
7960 pages += (size_t)1 << get_order(
7961 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007962
Hristo Venev75b28af2019-08-26 17:23:46 +00007963 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007964}
7965
Jens Axboeedafcce2019-01-09 09:16:05 -07007966static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7967{
7968 int i, j;
7969
7970 if (!ctx->user_bufs)
7971 return -ENXIO;
7972
7973 for (i = 0; i < ctx->nr_user_bufs; i++) {
7974 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7975
7976 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007977 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007978
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007979 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007980 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007981 imu->nr_bvecs = 0;
7982 }
7983
7984 kfree(ctx->user_bufs);
7985 ctx->user_bufs = NULL;
7986 ctx->nr_user_bufs = 0;
7987 return 0;
7988}
7989
7990static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7991 void __user *arg, unsigned index)
7992{
7993 struct iovec __user *src;
7994
7995#ifdef CONFIG_COMPAT
7996 if (ctx->compat) {
7997 struct compat_iovec __user *ciovs;
7998 struct compat_iovec ciov;
7999
8000 ciovs = (struct compat_iovec __user *) arg;
8001 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8002 return -EFAULT;
8003
Jens Axboed55e5f52019-12-11 16:12:15 -07008004 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008005 dst->iov_len = ciov.iov_len;
8006 return 0;
8007 }
8008#endif
8009 src = (struct iovec __user *) arg;
8010 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8011 return -EFAULT;
8012 return 0;
8013}
8014
8015static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
8016 unsigned nr_args)
8017{
8018 struct vm_area_struct **vmas = NULL;
8019 struct page **pages = NULL;
8020 int i, j, got_pages = 0;
8021 int ret = -EINVAL;
8022
8023 if (ctx->user_bufs)
8024 return -EBUSY;
8025 if (!nr_args || nr_args > UIO_MAXIOV)
8026 return -EINVAL;
8027
8028 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8029 GFP_KERNEL);
8030 if (!ctx->user_bufs)
8031 return -ENOMEM;
8032
8033 for (i = 0; i < nr_args; i++) {
8034 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8035 unsigned long off, start, end, ubuf;
8036 int pret, nr_pages;
8037 struct iovec iov;
8038 size_t size;
8039
8040 ret = io_copy_iov(ctx, &iov, arg, i);
8041 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03008042 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008043
8044 /*
8045 * Don't impose further limits on the size and buffer
8046 * constraints here, we'll -EINVAL later when IO is
8047 * submitted if they are wrong.
8048 */
8049 ret = -EFAULT;
8050 if (!iov.iov_base || !iov.iov_len)
8051 goto err;
8052
8053 /* arbitrary limit, but we need something */
8054 if (iov.iov_len > SZ_1G)
8055 goto err;
8056
8057 ubuf = (unsigned long) iov.iov_base;
8058 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8059 start = ubuf >> PAGE_SHIFT;
8060 nr_pages = end - start;
8061
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008062 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008063 if (ret)
8064 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07008065
8066 ret = 0;
8067 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03008068 kvfree(vmas);
8069 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008070 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07008071 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008072 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07008073 sizeof(struct vm_area_struct *),
8074 GFP_KERNEL);
8075 if (!pages || !vmas) {
8076 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008077 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008078 goto err;
8079 }
8080 got_pages = nr_pages;
8081 }
8082
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008083 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07008084 GFP_KERNEL);
8085 ret = -ENOMEM;
8086 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008087 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07008088 goto err;
8089 }
8090
8091 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008092 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08008093 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07008094 FOLL_WRITE | FOLL_LONGTERM,
8095 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008096 if (pret == nr_pages) {
8097 /* don't support file backed memory */
8098 for (j = 0; j < nr_pages; j++) {
8099 struct vm_area_struct *vma = vmas[j];
8100
8101 if (vma->vm_file &&
8102 !is_file_hugepages(vma->vm_file)) {
8103 ret = -EOPNOTSUPP;
8104 break;
8105 }
8106 }
8107 } else {
8108 ret = pret < 0 ? pret : -EFAULT;
8109 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07008110 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07008111 if (ret) {
8112 /*
8113 * if we did partial map, or found file backed vmas,
8114 * release any pages we did get
8115 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07008116 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008117 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008118 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008119 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008120 goto err;
8121 }
8122
8123 off = ubuf & ~PAGE_MASK;
8124 size = iov.iov_len;
8125 for (j = 0; j < nr_pages; j++) {
8126 size_t vec_len;
8127
8128 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8129 imu->bvec[j].bv_page = pages[j];
8130 imu->bvec[j].bv_len = vec_len;
8131 imu->bvec[j].bv_offset = off;
8132 off = 0;
8133 size -= vec_len;
8134 }
8135 /* store original address for later verification */
8136 imu->ubuf = ubuf;
8137 imu->len = iov.iov_len;
8138 imu->nr_bvecs = nr_pages;
8139
8140 ctx->nr_user_bufs++;
8141 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008142 kvfree(pages);
8143 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008144 return 0;
8145err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008146 kvfree(pages);
8147 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07008148 io_sqe_buffer_unregister(ctx);
8149 return ret;
8150}
8151
Jens Axboe9b402842019-04-11 11:45:41 -06008152static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8153{
8154 __s32 __user *fds = arg;
8155 int fd;
8156
8157 if (ctx->cq_ev_fd)
8158 return -EBUSY;
8159
8160 if (copy_from_user(&fd, fds, sizeof(*fds)))
8161 return -EFAULT;
8162
8163 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8164 if (IS_ERR(ctx->cq_ev_fd)) {
8165 int ret = PTR_ERR(ctx->cq_ev_fd);
8166 ctx->cq_ev_fd = NULL;
8167 return ret;
8168 }
8169
8170 return 0;
8171}
8172
8173static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8174{
8175 if (ctx->cq_ev_fd) {
8176 eventfd_ctx_put(ctx->cq_ev_fd);
8177 ctx->cq_ev_fd = NULL;
8178 return 0;
8179 }
8180
8181 return -ENXIO;
8182}
8183
Jens Axboe5a2e7452020-02-23 16:23:11 -07008184static int __io_destroy_buffers(int id, void *p, void *data)
8185{
8186 struct io_ring_ctx *ctx = data;
8187 struct io_buffer *buf = p;
8188
Jens Axboe067524e2020-03-02 16:32:28 -07008189 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008190 return 0;
8191}
8192
8193static void io_destroy_buffers(struct io_ring_ctx *ctx)
8194{
8195 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8196 idr_destroy(&ctx->io_buffer_idr);
8197}
8198
Jens Axboe2b188cc2019-01-07 10:46:33 -07008199static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8200{
Jens Axboe6b063142019-01-10 22:13:58 -07008201 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07008202 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008203
8204 if (ctx->sqo_task) {
8205 put_task_struct(ctx->sqo_task);
8206 ctx->sqo_task = NULL;
8207 mmdrop(ctx->mm_account);
8208 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008209 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008210
Jens Axboe6b063142019-01-10 22:13:58 -07008211 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06008212 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008213 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008214 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008215
Jens Axboe2b188cc2019-01-07 10:46:33 -07008216#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008217 if (ctx->ring_sock) {
8218 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008219 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008220 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008221#endif
8222
Hristo Venev75b28af2019-08-26 17:23:46 +00008223 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008224 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008225
8226 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008227 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008228 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07008229 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07008230 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008231 kfree(ctx);
8232}
8233
8234static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8235{
8236 struct io_ring_ctx *ctx = file->private_data;
8237 __poll_t mask = 0;
8238
8239 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008240 /*
8241 * synchronizes with barrier from wq_has_sleeper call in
8242 * io_commit_cqring
8243 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008244 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008245 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008246 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01008247 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008248 mask |= EPOLLIN | EPOLLRDNORM;
8249
8250 return mask;
8251}
8252
8253static int io_uring_fasync(int fd, struct file *file, int on)
8254{
8255 struct io_ring_ctx *ctx = file->private_data;
8256
8257 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8258}
8259
Jens Axboe071698e2020-01-28 10:04:42 -07008260static int io_remove_personalities(int id, void *p, void *data)
8261{
8262 struct io_ring_ctx *ctx = data;
8263 const struct cred *cred;
8264
8265 cred = idr_remove(&ctx->personality_idr, id);
8266 if (cred)
8267 put_cred(cred);
8268 return 0;
8269}
8270
Jens Axboe85faa7b2020-04-09 18:14:00 -06008271static void io_ring_exit_work(struct work_struct *work)
8272{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008273 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8274 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008275
Jens Axboe56952e92020-06-17 15:00:04 -06008276 /*
8277 * If we're doing polled IO and end up having requests being
8278 * submitted async (out-of-line), then completions can come in while
8279 * we're waiting for refs to drop. We need to reap these manually,
8280 * as nobody else will be looking for them.
8281 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008282 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008283 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008284 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008285 io_iopoll_try_reap_events(ctx);
8286 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008287 io_ring_ctx_free(ctx);
8288}
8289
Jens Axboe2b188cc2019-01-07 10:46:33 -07008290static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8291{
8292 mutex_lock(&ctx->uring_lock);
8293 percpu_ref_kill(&ctx->refs);
8294 mutex_unlock(&ctx->uring_lock);
8295
Jens Axboef3606e32020-09-22 08:18:24 -06008296 io_kill_timeouts(ctx, NULL);
8297 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008298
8299 if (ctx->io_wq)
8300 io_wq_cancel_all(ctx->io_wq);
8301
Jens Axboe15dff282019-11-13 09:09:23 -07008302 /* if we failed setting up the ctx, we might not have any rings */
8303 if (ctx->rings)
Jens Axboee6c8aa92020-09-28 13:10:13 -06008304 io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008305 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008306 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008307
8308 /*
8309 * Do this upfront, so we won't have a grace period where the ring
8310 * is closed but resources aren't reaped yet. This can cause
8311 * spurious failure in setting up a new ring.
8312 */
Jens Axboe760618f2020-07-24 12:53:31 -06008313 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8314 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008315
Jens Axboe85faa7b2020-04-09 18:14:00 -06008316 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008317 /*
8318 * Use system_unbound_wq to avoid spawning tons of event kworkers
8319 * if we're exiting a ton of rings at the same time. It just adds
8320 * noise and overhead, there's no discernable change in runtime
8321 * over using system_wq.
8322 */
8323 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008324}
8325
8326static int io_uring_release(struct inode *inode, struct file *file)
8327{
8328 struct io_ring_ctx *ctx = file->private_data;
8329
8330 file->private_data = NULL;
8331 io_ring_ctx_wait_and_kill(ctx);
8332 return 0;
8333}
8334
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008335static bool io_wq_files_match(struct io_wq_work *work, void *data)
8336{
8337 struct files_struct *files = data;
8338
Jens Axboe0f212202020-09-13 13:09:39 -06008339 return !files || work->files == files;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008340}
8341
Jens Axboef254ac02020-08-12 17:33:30 -06008342/*
8343 * Returns true if 'preq' is the link parent of 'req'
8344 */
8345static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8346{
8347 struct io_kiocb *link;
8348
8349 if (!(preq->flags & REQ_F_LINK_HEAD))
8350 return false;
8351
8352 list_for_each_entry(link, &preq->link_list, link_list) {
8353 if (link == req)
8354 return true;
8355 }
8356
8357 return false;
8358}
8359
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008360static bool io_match_link_files(struct io_kiocb *req,
8361 struct files_struct *files)
8362{
8363 struct io_kiocb *link;
8364
8365 if (io_match_files(req, files))
8366 return true;
8367 if (req->flags & REQ_F_LINK_HEAD) {
8368 list_for_each_entry(link, &req->link_list, link_list) {
8369 if (io_match_files(link, files))
8370 return true;
8371 }
8372 }
8373 return false;
8374}
8375
Jens Axboef254ac02020-08-12 17:33:30 -06008376/*
8377 * We're looking to cancel 'req' because it's holding on to our files, but
8378 * 'req' could be a link to another request. See if it is, and cancel that
8379 * parent request if so.
8380 */
8381static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8382{
8383 struct hlist_node *tmp;
8384 struct io_kiocb *preq;
8385 bool found = false;
8386 int i;
8387
8388 spin_lock_irq(&ctx->completion_lock);
8389 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8390 struct hlist_head *list;
8391
8392 list = &ctx->cancel_hash[i];
8393 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8394 found = io_match_link(preq, req);
8395 if (found) {
8396 io_poll_remove_one(preq);
8397 break;
8398 }
8399 }
8400 }
8401 spin_unlock_irq(&ctx->completion_lock);
8402 return found;
8403}
8404
8405static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8406 struct io_kiocb *req)
8407{
8408 struct io_kiocb *preq;
8409 bool found = false;
8410
8411 spin_lock_irq(&ctx->completion_lock);
8412 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8413 found = io_match_link(preq, req);
8414 if (found) {
8415 __io_timeout_cancel(preq);
8416 break;
8417 }
8418 }
8419 spin_unlock_irq(&ctx->completion_lock);
8420 return found;
8421}
8422
Jens Axboeb711d4e2020-08-16 08:23:05 -07008423static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8424{
8425 return io_match_link(container_of(work, struct io_kiocb, work), data);
8426}
8427
8428static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8429{
8430 enum io_wq_cancel cret;
8431
8432 /* cancel this particular work, if it's running */
8433 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8434 if (cret != IO_WQ_CANCEL_NOTFOUND)
8435 return;
8436
8437 /* find links that hold this pending, cancel those */
8438 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8439 if (cret != IO_WQ_CANCEL_NOTFOUND)
8440 return;
8441
8442 /* if we have a poll link holding this pending, cancel that */
8443 if (io_poll_remove_link(ctx, req))
8444 return;
8445
8446 /* final option, timeout link is holding this req pending */
8447 io_timeout_remove_link(ctx, req);
8448}
8449
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008450static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8451 struct files_struct *files)
8452{
8453 struct io_defer_entry *de = NULL;
8454 LIST_HEAD(list);
8455
8456 spin_lock_irq(&ctx->completion_lock);
8457 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008458 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008459 list_cut_position(&list, &ctx->defer_list, &de->list);
8460 break;
8461 }
8462 }
8463 spin_unlock_irq(&ctx->completion_lock);
8464
8465 while (!list_empty(&list)) {
8466 de = list_first_entry(&list, struct io_defer_entry, list);
8467 list_del_init(&de->list);
8468 req_set_fail_links(de->req);
8469 io_put_req(de->req);
8470 io_req_complete(de->req, -ECANCELED);
8471 kfree(de);
8472 }
8473}
8474
Jens Axboe76e1b642020-09-26 15:05:03 -06008475/*
8476 * Returns true if we found and killed one or more files pinning requests
8477 */
8478static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
Jens Axboefcb323c2019-10-24 12:39:47 -06008479 struct files_struct *files)
8480{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008481 if (list_empty_careful(&ctx->inflight_list))
Jens Axboe76e1b642020-09-26 15:05:03 -06008482 return false;
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008483
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008484 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008485 /* cancel all at once, should be faster than doing it one by one*/
8486 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8487
Jens Axboefcb323c2019-10-24 12:39:47 -06008488 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008489 struct io_kiocb *cancel_req = NULL, *req;
8490 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008491
8492 spin_lock_irq(&ctx->inflight_lock);
8493 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe0f212202020-09-13 13:09:39 -06008494 if (files && req->work.files != files)
Jens Axboe768134d2019-11-10 20:30:53 -07008495 continue;
8496 /* req is being completed, ignore */
8497 if (!refcount_inc_not_zero(&req->refs))
8498 continue;
8499 cancel_req = req;
8500 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008501 }
Jens Axboe768134d2019-11-10 20:30:53 -07008502 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008503 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008504 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008505 spin_unlock_irq(&ctx->inflight_lock);
8506
Jens Axboe768134d2019-11-10 20:30:53 -07008507 /* We need to keep going until we don't find a matching req */
8508 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008509 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008510 /* cancel this request, or head link requests */
8511 io_attempt_cancel(ctx, cancel_req);
8512 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008513 /* cancellations _may_ trigger task work */
8514 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008515 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008516 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008517 }
Jens Axboe76e1b642020-09-26 15:05:03 -06008518
8519 return true;
Jens Axboefcb323c2019-10-24 12:39:47 -06008520}
8521
Pavel Begunkov801dd572020-06-15 10:33:14 +03008522static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008523{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008524 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8525 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008526
Jens Axboef3606e32020-09-22 08:18:24 -06008527 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008528}
8529
Jens Axboe0f212202020-09-13 13:09:39 -06008530static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8531 struct task_struct *task,
8532 struct files_struct *files)
8533{
8534 bool ret;
8535
8536 ret = io_uring_cancel_files(ctx, files);
8537 if (!files) {
8538 enum io_wq_cancel cret;
8539
8540 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
8541 if (cret != IO_WQ_CANCEL_NOTFOUND)
8542 ret = true;
8543
8544 /* SQPOLL thread does its own polling */
8545 if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
8546 while (!list_empty_careful(&ctx->iopoll_list)) {
8547 io_iopoll_try_reap_events(ctx);
8548 ret = true;
8549 }
8550 }
8551
8552 ret |= io_poll_remove_all(ctx, task);
8553 ret |= io_kill_timeouts(ctx, task);
8554 }
8555
8556 return ret;
8557}
8558
8559/*
8560 * We need to iteratively cancel requests, in case a request has dependent
8561 * hard links. These persist even for failure of cancelations, hence keep
8562 * looping until none are found.
8563 */
8564static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8565 struct files_struct *files)
8566{
8567 struct task_struct *task = current;
8568
Jens Axboe534ca6d2020-09-02 13:52:19 -06008569 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
8570 task = ctx->sq_data->thread;
Jens Axboe0f212202020-09-13 13:09:39 -06008571
8572 io_cqring_overflow_flush(ctx, true, task, files);
8573
8574 while (__io_uring_cancel_task_requests(ctx, task, files)) {
8575 io_run_task_work();
8576 cond_resched();
8577 }
8578}
8579
8580/*
8581 * Note that this task has used io_uring. We use it for cancelation purposes.
8582 */
8583static int io_uring_add_task_file(struct file *file)
8584{
8585 if (unlikely(!current->io_uring)) {
8586 int ret;
8587
8588 ret = io_uring_alloc_task_context(current);
8589 if (unlikely(ret))
8590 return ret;
8591 }
8592 if (current->io_uring->last != file) {
8593 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8594 void *old;
8595
8596 rcu_read_lock();
8597 old = xas_load(&xas);
8598 if (old != file) {
8599 get_file(file);
8600 xas_lock(&xas);
8601 xas_store(&xas, file);
8602 xas_unlock(&xas);
8603 }
8604 rcu_read_unlock();
8605 current->io_uring->last = file;
8606 }
8607
8608 return 0;
8609}
8610
8611/*
8612 * Remove this io_uring_file -> task mapping.
8613 */
8614static void io_uring_del_task_file(struct file *file)
8615{
8616 struct io_uring_task *tctx = current->io_uring;
8617 XA_STATE(xas, &tctx->xa, (unsigned long) file);
8618
8619 if (tctx->last == file)
8620 tctx->last = NULL;
8621
8622 xas_lock(&xas);
8623 file = xas_store(&xas, NULL);
8624 xas_unlock(&xas);
8625
8626 if (file)
8627 fput(file);
8628}
8629
8630static void __io_uring_attempt_task_drop(struct file *file)
8631{
8632 XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
8633 struct file *old;
8634
8635 rcu_read_lock();
8636 old = xas_load(&xas);
8637 rcu_read_unlock();
8638
8639 if (old == file)
8640 io_uring_del_task_file(file);
8641}
8642
8643/*
8644 * Drop task note for this file if we're the only ones that hold it after
8645 * pending fput()
8646 */
8647static void io_uring_attempt_task_drop(struct file *file, bool exiting)
8648{
8649 if (!current->io_uring)
8650 return;
8651 /*
8652 * fput() is pending, will be 2 if the only other ref is our potential
8653 * task file note. If the task is exiting, drop regardless of count.
8654 */
8655 if (!exiting && atomic_long_read(&file->f_count) != 2)
8656 return;
8657
8658 __io_uring_attempt_task_drop(file);
8659}
8660
8661void __io_uring_files_cancel(struct files_struct *files)
8662{
8663 struct io_uring_task *tctx = current->io_uring;
8664 XA_STATE(xas, &tctx->xa, 0);
8665
8666 /* make sure overflow events are dropped */
8667 tctx->in_idle = true;
8668
8669 do {
8670 struct io_ring_ctx *ctx;
8671 struct file *file;
8672
8673 xas_lock(&xas);
8674 file = xas_next_entry(&xas, ULONG_MAX);
8675 xas_unlock(&xas);
8676
8677 if (!file)
8678 break;
8679
8680 ctx = file->private_data;
8681
8682 io_uring_cancel_task_requests(ctx, files);
8683 if (files)
8684 io_uring_del_task_file(file);
8685 } while (1);
8686}
8687
8688static inline bool io_uring_task_idle(struct io_uring_task *tctx)
8689{
8690 return atomic_long_read(&tctx->req_issue) ==
8691 atomic_long_read(&tctx->req_complete);
8692}
8693
8694/*
8695 * Find any io_uring fd that this task has registered or done IO on, and cancel
8696 * requests.
8697 */
8698void __io_uring_task_cancel(void)
8699{
8700 struct io_uring_task *tctx = current->io_uring;
8701 DEFINE_WAIT(wait);
8702 long completions;
8703
8704 /* make sure overflow events are dropped */
8705 tctx->in_idle = true;
8706
8707 while (!io_uring_task_idle(tctx)) {
8708 /* read completions before cancelations */
8709 completions = atomic_long_read(&tctx->req_complete);
8710 __io_uring_files_cancel(NULL);
8711
8712 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8713
8714 /*
8715 * If we've seen completions, retry. This avoids a race where
8716 * a completion comes in before we did prepare_to_wait().
8717 */
8718 if (completions != atomic_long_read(&tctx->req_complete))
8719 continue;
8720 if (io_uring_task_idle(tctx))
8721 break;
8722 schedule();
8723 }
8724
8725 finish_wait(&tctx->wait, &wait);
8726 tctx->in_idle = false;
8727}
8728
Jens Axboefcb323c2019-10-24 12:39:47 -06008729static int io_uring_flush(struct file *file, void *data)
8730{
8731 struct io_ring_ctx *ctx = file->private_data;
8732
Jens Axboe6ab23142020-02-08 20:23:59 -07008733 /*
8734 * If the task is going away, cancel work it may have pending
8735 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008736 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
Jens Axboe0f212202020-09-13 13:09:39 -06008737 data = NULL;
Jens Axboe6ab23142020-02-08 20:23:59 -07008738
Jens Axboe0f212202020-09-13 13:09:39 -06008739 io_uring_cancel_task_requests(ctx, data);
8740 io_uring_attempt_task_drop(file, !data);
Jens Axboefcb323c2019-10-24 12:39:47 -06008741 return 0;
8742}
8743
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008744static void *io_uring_validate_mmap_request(struct file *file,
8745 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008746{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008747 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008748 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008749 struct page *page;
8750 void *ptr;
8751
8752 switch (offset) {
8753 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008754 case IORING_OFF_CQ_RING:
8755 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008756 break;
8757 case IORING_OFF_SQES:
8758 ptr = ctx->sq_sqes;
8759 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008760 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008761 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008762 }
8763
8764 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008765 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008766 return ERR_PTR(-EINVAL);
8767
8768 return ptr;
8769}
8770
8771#ifdef CONFIG_MMU
8772
8773static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8774{
8775 size_t sz = vma->vm_end - vma->vm_start;
8776 unsigned long pfn;
8777 void *ptr;
8778
8779 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8780 if (IS_ERR(ptr))
8781 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782
8783 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8784 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8785}
8786
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008787#else /* !CONFIG_MMU */
8788
8789static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8790{
8791 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8792}
8793
8794static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8795{
8796 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8797}
8798
8799static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8800 unsigned long addr, unsigned long len,
8801 unsigned long pgoff, unsigned long flags)
8802{
8803 void *ptr;
8804
8805 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8806 if (IS_ERR(ptr))
8807 return PTR_ERR(ptr);
8808
8809 return (unsigned long) ptr;
8810}
8811
8812#endif /* !CONFIG_MMU */
8813
Jens Axboe90554202020-09-03 12:12:41 -06008814static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
8815{
8816 DEFINE_WAIT(wait);
8817
8818 do {
8819 if (!io_sqring_full(ctx))
8820 break;
8821
8822 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
8823
8824 if (!io_sqring_full(ctx))
8825 break;
8826
8827 schedule();
8828 } while (!signal_pending(current));
8829
8830 finish_wait(&ctx->sqo_sq_wait, &wait);
8831}
8832
Jens Axboe2b188cc2019-01-07 10:46:33 -07008833SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8834 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8835 size_t, sigsz)
8836{
8837 struct io_ring_ctx *ctx;
8838 long ret = -EBADF;
8839 int submitted = 0;
8840 struct fd f;
8841
Jens Axboe4c6e2772020-07-01 11:29:10 -06008842 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008843
Jens Axboe90554202020-09-03 12:12:41 -06008844 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
8845 IORING_ENTER_SQ_WAIT))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008846 return -EINVAL;
8847
8848 f = fdget(fd);
8849 if (!f.file)
8850 return -EBADF;
8851
8852 ret = -EOPNOTSUPP;
8853 if (f.file->f_op != &io_uring_fops)
8854 goto out_fput;
8855
8856 ret = -ENXIO;
8857 ctx = f.file->private_data;
8858 if (!percpu_ref_tryget(&ctx->refs))
8859 goto out_fput;
8860
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008861 ret = -EBADFD;
8862 if (ctx->flags & IORING_SETUP_R_DISABLED)
8863 goto out;
8864
Jens Axboe6c271ce2019-01-10 11:22:30 -07008865 /*
8866 * For SQ polling, the thread will do all submissions and completions.
8867 * Just return the requested submit count, and wake the thread if
8868 * we were asked to.
8869 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008870 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008871 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008872 if (!list_empty_careful(&ctx->cq_overflow_list))
Jens Axboee6c8aa92020-09-28 13:10:13 -06008873 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008874 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008875 wake_up(&ctx->sq_data->wait);
Jens Axboe90554202020-09-03 12:12:41 -06008876 if (flags & IORING_ENTER_SQ_WAIT)
8877 io_sqpoll_wait_sq(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008878 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008879 } else if (to_submit) {
Jens Axboe0f212202020-09-13 13:09:39 -06008880 ret = io_uring_add_task_file(f.file);
8881 if (unlikely(ret))
8882 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008883 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008884 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008885 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008886
8887 if (submitted != to_submit)
8888 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008889 }
8890 if (flags & IORING_ENTER_GETEVENTS) {
8891 min_complete = min(min_complete, ctx->cq_entries);
8892
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008893 /*
8894 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8895 * space applications don't need to do io completion events
8896 * polling again, they can rely on io_sq_thread to do polling
8897 * work, which can reduce cpu usage and uring_lock contention.
8898 */
8899 if (ctx->flags & IORING_SETUP_IOPOLL &&
8900 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008901 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008902 } else {
8903 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8904 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008905 }
8906
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008907out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008908 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008909out_fput:
8910 fdput(f);
8911 return submitted ? submitted : ret;
8912}
8913
Tobias Klauserbebdb652020-02-26 18:38:32 +01008914#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008915static int io_uring_show_cred(int id, void *p, void *data)
8916{
8917 const struct cred *cred = p;
8918 struct seq_file *m = data;
8919 struct user_namespace *uns = seq_user_ns(m);
8920 struct group_info *gi;
8921 kernel_cap_t cap;
8922 unsigned __capi;
8923 int g;
8924
8925 seq_printf(m, "%5d\n", id);
8926 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8927 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8928 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8929 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8930 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8931 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8932 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8933 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8934 seq_puts(m, "\n\tGroups:\t");
8935 gi = cred->group_info;
8936 for (g = 0; g < gi->ngroups; g++) {
8937 seq_put_decimal_ull(m, g ? " " : "",
8938 from_kgid_munged(uns, gi->gid[g]));
8939 }
8940 seq_puts(m, "\n\tCapEff:\t");
8941 cap = cred->cap_effective;
8942 CAP_FOR_EACH_U32(__capi)
8943 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8944 seq_putc(m, '\n');
8945 return 0;
8946}
8947
8948static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8949{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008950 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008951 int i;
8952
Jens Axboefad8e0d2020-09-28 08:57:48 -06008953 /*
8954 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8955 * since fdinfo case grabs it in the opposite direction of normal use
8956 * cases. If we fail to get the lock, we just don't iterate any
8957 * structures that could be going away outside the io_uring mutex.
8958 */
8959 has_lock = mutex_trylock(&ctx->uring_lock);
8960
Jens Axboe87ce9552020-01-30 08:25:34 -07008961 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008962 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008963 struct fixed_file_table *table;
8964 struct file *f;
8965
8966 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8967 f = table->files[i & IORING_FILE_TABLE_MASK];
8968 if (f)
8969 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8970 else
8971 seq_printf(m, "%5u: <none>\n", i);
8972 }
8973 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008974 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008975 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8976
8977 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8978 (unsigned int) buf->len);
8979 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008980 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008981 seq_printf(m, "Personalities:\n");
8982 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8983 }
Jens Axboed7718a92020-02-14 22:23:12 -07008984 seq_printf(m, "PollList:\n");
8985 spin_lock_irq(&ctx->completion_lock);
8986 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8987 struct hlist_head *list = &ctx->cancel_hash[i];
8988 struct io_kiocb *req;
8989
8990 hlist_for_each_entry(req, list, hash_node)
8991 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8992 req->task->task_works != NULL);
8993 }
8994 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008995 if (has_lock)
8996 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008997}
8998
8999static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9000{
9001 struct io_ring_ctx *ctx = f->private_data;
9002
9003 if (percpu_ref_tryget(&ctx->refs)) {
9004 __io_uring_show_fdinfo(ctx, m);
9005 percpu_ref_put(&ctx->refs);
9006 }
9007}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009008#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009009
Jens Axboe2b188cc2019-01-07 10:46:33 -07009010static const struct file_operations io_uring_fops = {
9011 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009012 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009013 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009014#ifndef CONFIG_MMU
9015 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9016 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9017#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009018 .poll = io_uring_poll,
9019 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009020#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009021 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009022#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009023};
9024
9025static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9026 struct io_uring_params *p)
9027{
Hristo Venev75b28af2019-08-26 17:23:46 +00009028 struct io_rings *rings;
9029 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009030
Jens Axboebd740482020-08-05 12:58:23 -06009031 /* make sure these are sane, as we already accounted them */
9032 ctx->sq_entries = p->sq_entries;
9033 ctx->cq_entries = p->cq_entries;
9034
Hristo Venev75b28af2019-08-26 17:23:46 +00009035 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9036 if (size == SIZE_MAX)
9037 return -EOVERFLOW;
9038
9039 rings = io_mem_alloc(size);
9040 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009041 return -ENOMEM;
9042
Hristo Venev75b28af2019-08-26 17:23:46 +00009043 ctx->rings = rings;
9044 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9045 rings->sq_ring_mask = p->sq_entries - 1;
9046 rings->cq_ring_mask = p->cq_entries - 1;
9047 rings->sq_ring_entries = p->sq_entries;
9048 rings->cq_ring_entries = p->cq_entries;
9049 ctx->sq_mask = rings->sq_ring_mask;
9050 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009051
9052 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009053 if (size == SIZE_MAX) {
9054 io_mem_free(ctx->rings);
9055 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009056 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009057 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009058
9059 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009060 if (!ctx->sq_sqes) {
9061 io_mem_free(ctx->rings);
9062 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009063 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009064 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009065
Jens Axboe2b188cc2019-01-07 10:46:33 -07009066 return 0;
9067}
9068
9069/*
9070 * Allocate an anonymous fd, this is what constitutes the application
9071 * visible backing of an io_uring instance. The application mmaps this
9072 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9073 * we have to tie this fd to a socket for file garbage collection purposes.
9074 */
9075static int io_uring_get_fd(struct io_ring_ctx *ctx)
9076{
9077 struct file *file;
9078 int ret;
9079
9080#if defined(CONFIG_UNIX)
9081 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9082 &ctx->ring_sock);
9083 if (ret)
9084 return ret;
9085#endif
9086
9087 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9088 if (ret < 0)
9089 goto err;
9090
9091 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9092 O_RDWR | O_CLOEXEC);
9093 if (IS_ERR(file)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009094err_fd:
Jens Axboe2b188cc2019-01-07 10:46:33 -07009095 put_unused_fd(ret);
9096 ret = PTR_ERR(file);
9097 goto err;
9098 }
9099
9100#if defined(CONFIG_UNIX)
9101 ctx->ring_sock->file = file;
9102#endif
Jens Axboe0f212202020-09-13 13:09:39 -06009103 if (unlikely(io_uring_add_task_file(file))) {
9104 file = ERR_PTR(-ENOMEM);
9105 goto err_fd;
9106 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009107 fd_install(ret, file);
9108 return ret;
9109err:
9110#if defined(CONFIG_UNIX)
9111 sock_release(ctx->ring_sock);
9112 ctx->ring_sock = NULL;
9113#endif
9114 return ret;
9115}
9116
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009117static int io_uring_create(unsigned entries, struct io_uring_params *p,
9118 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009119{
9120 struct user_struct *user = NULL;
9121 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009122 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009123 int ret;
9124
Jens Axboe8110c1a2019-12-28 15:39:54 -07009125 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009126 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009127 if (entries > IORING_MAX_ENTRIES) {
9128 if (!(p->flags & IORING_SETUP_CLAMP))
9129 return -EINVAL;
9130 entries = IORING_MAX_ENTRIES;
9131 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009132
9133 /*
9134 * Use twice as many entries for the CQ ring. It's possible for the
9135 * application to drive a higher depth than the size of the SQ ring,
9136 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009137 * some flexibility in overcommitting a bit. If the application has
9138 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9139 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009140 */
9141 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009142 if (p->flags & IORING_SETUP_CQSIZE) {
9143 /*
9144 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9145 * to a power-of-two, if it isn't already. We do NOT impose
9146 * any cq vs sq ring sizing.
9147 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07009148 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009149 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009150 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9151 if (!(p->flags & IORING_SETUP_CLAMP))
9152 return -EINVAL;
9153 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9154 }
Jens Axboe33a107f2019-10-04 12:10:03 -06009155 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9156 } else {
9157 p->cq_entries = 2 * p->sq_entries;
9158 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009159
9160 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009161 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009162
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009163 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009164 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009165 ring_pages(p->sq_entries, p->cq_entries));
9166 if (ret) {
9167 free_uid(user);
9168 return ret;
9169 }
9170 }
9171
9172 ctx = io_ring_ctx_alloc(p);
9173 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07009174 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07009175 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009176 p->cq_entries));
9177 free_uid(user);
9178 return -ENOMEM;
9179 }
9180 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009181 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009182 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07009183
Jens Axboe2aede0e2020-09-14 10:45:53 -06009184 ctx->sqo_task = get_task_struct(current);
9185
9186 /*
9187 * This is just grabbed for accounting purposes. When a process exits,
9188 * the mm is exited and dropped before the files, hence we need to hang
9189 * on to this mm purely for the purposes of being able to unaccount
9190 * memory (locked/pinned vm). It's not used for anything else.
9191 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009192 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009193 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009194
Jens Axboef74441e2020-08-05 13:00:44 -06009195 /*
9196 * Account memory _before_ installing the file descriptor. Once
9197 * the descriptor is installed, it can get closed at any time. Also
9198 * do this before hitting the general error path, as ring freeing
9199 * will un-account as well.
9200 */
9201 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
9202 ACCT_LOCKED);
9203 ctx->limit_mem = limit_mem;
9204
Jens Axboe2b188cc2019-01-07 10:46:33 -07009205 ret = io_allocate_scq_urings(ctx, p);
9206 if (ret)
9207 goto err;
9208
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009209 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009210 if (ret)
9211 goto err;
9212
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009213 if (!(p->flags & IORING_SETUP_R_DISABLED))
9214 io_sq_offload_start(ctx);
9215
Jens Axboe2b188cc2019-01-07 10:46:33 -07009216 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009217 p->sq_off.head = offsetof(struct io_rings, sq.head);
9218 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9219 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9220 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9221 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9222 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9223 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009224
9225 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009226 p->cq_off.head = offsetof(struct io_rings, cq.head);
9227 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9228 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9229 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9230 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9231 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009232 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009233
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009234 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9235 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009236 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
9237 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009238
9239 if (copy_to_user(params, p, sizeof(*p))) {
9240 ret = -EFAULT;
9241 goto err;
9242 }
Jens Axboed1719f72020-07-30 13:43:53 -06009243
9244 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009245 * Install ring fd as the very last thing, so we don't risk someone
9246 * having closed it before we finish setup
9247 */
9248 ret = io_uring_get_fd(ctx);
9249 if (ret < 0)
9250 goto err;
9251
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009252 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009253 return ret;
9254err:
9255 io_ring_ctx_wait_and_kill(ctx);
9256 return ret;
9257}
9258
9259/*
9260 * Sets up an aio uring context, and returns the fd. Applications asks for a
9261 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9262 * params structure passed in.
9263 */
9264static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9265{
9266 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009267 int i;
9268
9269 if (copy_from_user(&p, params, sizeof(p)))
9270 return -EFAULT;
9271 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9272 if (p.resv[i])
9273 return -EINVAL;
9274 }
9275
Jens Axboe6c271ce2019-01-10 11:22:30 -07009276 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009277 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009278 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9279 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009280 return -EINVAL;
9281
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009282 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009283}
9284
9285SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9286 struct io_uring_params __user *, params)
9287{
9288 return io_uring_setup(entries, params);
9289}
9290
Jens Axboe66f4af92020-01-16 15:36:52 -07009291static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9292{
9293 struct io_uring_probe *p;
9294 size_t size;
9295 int i, ret;
9296
9297 size = struct_size(p, ops, nr_args);
9298 if (size == SIZE_MAX)
9299 return -EOVERFLOW;
9300 p = kzalloc(size, GFP_KERNEL);
9301 if (!p)
9302 return -ENOMEM;
9303
9304 ret = -EFAULT;
9305 if (copy_from_user(p, arg, size))
9306 goto out;
9307 ret = -EINVAL;
9308 if (memchr_inv(p, 0, size))
9309 goto out;
9310
9311 p->last_op = IORING_OP_LAST - 1;
9312 if (nr_args > IORING_OP_LAST)
9313 nr_args = IORING_OP_LAST;
9314
9315 for (i = 0; i < nr_args; i++) {
9316 p->ops[i].op = i;
9317 if (!io_op_defs[i].not_supported)
9318 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9319 }
9320 p->ops_len = i;
9321
9322 ret = 0;
9323 if (copy_to_user(arg, p, size))
9324 ret = -EFAULT;
9325out:
9326 kfree(p);
9327 return ret;
9328}
9329
Jens Axboe071698e2020-01-28 10:04:42 -07009330static int io_register_personality(struct io_ring_ctx *ctx)
9331{
9332 const struct cred *creds = get_current_cred();
9333 int id;
9334
9335 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9336 USHRT_MAX, GFP_KERNEL);
9337 if (id < 0)
9338 put_cred(creds);
9339 return id;
9340}
9341
9342static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
9343{
9344 const struct cred *old_creds;
9345
9346 old_creds = idr_remove(&ctx->personality_idr, id);
9347 if (old_creds) {
9348 put_cred(old_creds);
9349 return 0;
9350 }
9351
9352 return -EINVAL;
9353}
9354
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009355static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9356 unsigned int nr_args)
9357{
9358 struct io_uring_restriction *res;
9359 size_t size;
9360 int i, ret;
9361
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009362 /* Restrictions allowed only if rings started disabled */
9363 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9364 return -EBADFD;
9365
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009366 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009367 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009368 return -EBUSY;
9369
9370 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9371 return -EINVAL;
9372
9373 size = array_size(nr_args, sizeof(*res));
9374 if (size == SIZE_MAX)
9375 return -EOVERFLOW;
9376
9377 res = memdup_user(arg, size);
9378 if (IS_ERR(res))
9379 return PTR_ERR(res);
9380
9381 ret = 0;
9382
9383 for (i = 0; i < nr_args; i++) {
9384 switch (res[i].opcode) {
9385 case IORING_RESTRICTION_REGISTER_OP:
9386 if (res[i].register_op >= IORING_REGISTER_LAST) {
9387 ret = -EINVAL;
9388 goto out;
9389 }
9390
9391 __set_bit(res[i].register_op,
9392 ctx->restrictions.register_op);
9393 break;
9394 case IORING_RESTRICTION_SQE_OP:
9395 if (res[i].sqe_op >= IORING_OP_LAST) {
9396 ret = -EINVAL;
9397 goto out;
9398 }
9399
9400 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9401 break;
9402 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9403 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9404 break;
9405 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9406 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9407 break;
9408 default:
9409 ret = -EINVAL;
9410 goto out;
9411 }
9412 }
9413
9414out:
9415 /* Reset all restrictions if an error happened */
9416 if (ret != 0)
9417 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9418 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009419 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009420
9421 kfree(res);
9422 return ret;
9423}
9424
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009425static int io_register_enable_rings(struct io_ring_ctx *ctx)
9426{
9427 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9428 return -EBADFD;
9429
9430 if (ctx->restrictions.registered)
9431 ctx->restricted = 1;
9432
9433 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9434
9435 io_sq_offload_start(ctx);
9436
9437 return 0;
9438}
9439
Jens Axboe071698e2020-01-28 10:04:42 -07009440static bool io_register_op_must_quiesce(int op)
9441{
9442 switch (op) {
9443 case IORING_UNREGISTER_FILES:
9444 case IORING_REGISTER_FILES_UPDATE:
9445 case IORING_REGISTER_PROBE:
9446 case IORING_REGISTER_PERSONALITY:
9447 case IORING_UNREGISTER_PERSONALITY:
9448 return false;
9449 default:
9450 return true;
9451 }
9452}
9453
Jens Axboeedafcce2019-01-09 09:16:05 -07009454static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9455 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009456 __releases(ctx->uring_lock)
9457 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009458{
9459 int ret;
9460
Jens Axboe35fa71a2019-04-22 10:23:23 -06009461 /*
9462 * We're inside the ring mutex, if the ref is already dying, then
9463 * someone else killed the ctx or is already going through
9464 * io_uring_register().
9465 */
9466 if (percpu_ref_is_dying(&ctx->refs))
9467 return -ENXIO;
9468
Jens Axboe071698e2020-01-28 10:04:42 -07009469 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009470 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009471
Jens Axboe05f3fb32019-12-09 11:22:50 -07009472 /*
9473 * Drop uring mutex before waiting for references to exit. If
9474 * another thread is currently inside io_uring_enter() it might
9475 * need to grab the uring_lock to make progress. If we hold it
9476 * here across the drain wait, then we can deadlock. It's safe
9477 * to drop the mutex here, since no new references will come in
9478 * after we've killed the percpu ref.
9479 */
9480 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06009481 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009482 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07009483 if (ret) {
9484 percpu_ref_resurrect(&ctx->refs);
9485 ret = -EINTR;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009486 goto out_quiesce;
9487 }
9488 }
9489
9490 if (ctx->restricted) {
9491 if (opcode >= IORING_REGISTER_LAST) {
9492 ret = -EINVAL;
9493 goto out;
9494 }
9495
9496 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9497 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009498 goto out;
9499 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009500 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009501
9502 switch (opcode) {
9503 case IORING_REGISTER_BUFFERS:
9504 ret = io_sqe_buffer_register(ctx, arg, nr_args);
9505 break;
9506 case IORING_UNREGISTER_BUFFERS:
9507 ret = -EINVAL;
9508 if (arg || nr_args)
9509 break;
9510 ret = io_sqe_buffer_unregister(ctx);
9511 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009512 case IORING_REGISTER_FILES:
9513 ret = io_sqe_files_register(ctx, arg, nr_args);
9514 break;
9515 case IORING_UNREGISTER_FILES:
9516 ret = -EINVAL;
9517 if (arg || nr_args)
9518 break;
9519 ret = io_sqe_files_unregister(ctx);
9520 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009521 case IORING_REGISTER_FILES_UPDATE:
9522 ret = io_sqe_files_update(ctx, arg, nr_args);
9523 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009524 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009525 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009526 ret = -EINVAL;
9527 if (nr_args != 1)
9528 break;
9529 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009530 if (ret)
9531 break;
9532 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9533 ctx->eventfd_async = 1;
9534 else
9535 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009536 break;
9537 case IORING_UNREGISTER_EVENTFD:
9538 ret = -EINVAL;
9539 if (arg || nr_args)
9540 break;
9541 ret = io_eventfd_unregister(ctx);
9542 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009543 case IORING_REGISTER_PROBE:
9544 ret = -EINVAL;
9545 if (!arg || nr_args > 256)
9546 break;
9547 ret = io_probe(ctx, arg, nr_args);
9548 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009549 case IORING_REGISTER_PERSONALITY:
9550 ret = -EINVAL;
9551 if (arg || nr_args)
9552 break;
9553 ret = io_register_personality(ctx);
9554 break;
9555 case IORING_UNREGISTER_PERSONALITY:
9556 ret = -EINVAL;
9557 if (arg)
9558 break;
9559 ret = io_unregister_personality(ctx, nr_args);
9560 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009561 case IORING_REGISTER_ENABLE_RINGS:
9562 ret = -EINVAL;
9563 if (arg || nr_args)
9564 break;
9565 ret = io_register_enable_rings(ctx);
9566 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009567 case IORING_REGISTER_RESTRICTIONS:
9568 ret = io_register_restrictions(ctx, arg, nr_args);
9569 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009570 default:
9571 ret = -EINVAL;
9572 break;
9573 }
9574
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009575out:
Jens Axboe071698e2020-01-28 10:04:42 -07009576 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009577 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009578 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009579out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009580 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009581 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009582 return ret;
9583}
9584
9585SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9586 void __user *, arg, unsigned int, nr_args)
9587{
9588 struct io_ring_ctx *ctx;
9589 long ret = -EBADF;
9590 struct fd f;
9591
9592 f = fdget(fd);
9593 if (!f.file)
9594 return -EBADF;
9595
9596 ret = -EOPNOTSUPP;
9597 if (f.file->f_op != &io_uring_fops)
9598 goto out_fput;
9599
9600 ctx = f.file->private_data;
9601
9602 mutex_lock(&ctx->uring_lock);
9603 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9604 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009605 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9606 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009607out_fput:
9608 fdput(f);
9609 return ret;
9610}
9611
Jens Axboe2b188cc2019-01-07 10:46:33 -07009612static int __init io_uring_init(void)
9613{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009614#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9615 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9616 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9617} while (0)
9618
9619#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9620 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9621 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9622 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9623 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9624 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9625 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9626 BUILD_BUG_SQE_ELEM(8, __u64, off);
9627 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9628 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009629 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009630 BUILD_BUG_SQE_ELEM(24, __u32, len);
9631 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9632 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9633 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9634 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009635 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9636 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009637 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9638 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9639 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9640 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9641 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9642 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9643 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9644 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009645 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009646 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9647 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9648 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009649 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009650
Jens Axboed3656342019-12-18 09:50:26 -07009651 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009652 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009653 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9654 return 0;
9655};
9656__initcall(io_uring_init);