Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // 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 | |
1e84b97b SB |
7 | * 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. | |
2b188cc1 JA |
29 | * |
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 | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
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> | |
47 | #include <linux/refcount.h> | |
48 | #include <linux/uio.h> | |
49 | ||
50 | #include <linux/sched/signal.h> | |
51 | #include <linux/fs.h> | |
52 | #include <linux/file.h> | |
53 | #include <linux/fdtable.h> | |
54 | #include <linux/mm.h> | |
55 | #include <linux/mman.h> | |
56 | #include <linux/mmu_context.h> | |
57 | #include <linux/percpu.h> | |
58 | #include <linux/slab.h> | |
6c271ce2 | 59 | #include <linux/kthread.h> |
2b188cc1 | 60 | #include <linux/blkdev.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
2b188cc1 | 72 | |
c826bd7a DD |
73 | #define CREATE_TRACE_POINTS |
74 | #include <trace/events/io_uring.h> | |
75 | ||
2b188cc1 JA |
76 | #include <uapi/linux/io_uring.h> |
77 | ||
78 | #include "internal.h" | |
561fb04a | 79 | #include "io-wq.h" |
2b188cc1 | 80 | |
5277deaa | 81 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 82 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
83 | |
84 | /* | |
85 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
86 | */ | |
87 | #define IORING_FILE_TABLE_SHIFT 9 | |
88 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
89 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
90 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
2b188cc1 JA |
91 | |
92 | struct io_uring { | |
93 | u32 head ____cacheline_aligned_in_smp; | |
94 | u32 tail ____cacheline_aligned_in_smp; | |
95 | }; | |
96 | ||
1e84b97b | 97 | /* |
75b28aff HV |
98 | * This data is shared with the application through the mmap at offsets |
99 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
100 | * |
101 | * The offsets to the member fields are published through struct | |
102 | * io_sqring_offsets when calling io_uring_setup. | |
103 | */ | |
75b28aff | 104 | struct io_rings { |
1e84b97b SB |
105 | /* |
106 | * Head and tail offsets into the ring; the offsets need to be | |
107 | * masked to get valid indices. | |
108 | * | |
75b28aff HV |
109 | * The kernel controls head of the sq ring and the tail of the cq ring, |
110 | * and the application controls tail of the sq ring and the head of the | |
111 | * cq ring. | |
1e84b97b | 112 | */ |
75b28aff | 113 | struct io_uring sq, cq; |
1e84b97b | 114 | /* |
75b28aff | 115 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
116 | * ring_entries - 1) |
117 | */ | |
75b28aff HV |
118 | u32 sq_ring_mask, cq_ring_mask; |
119 | /* Ring sizes (constant, power of 2) */ | |
120 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
121 | /* |
122 | * Number of invalid entries dropped by the kernel due to | |
123 | * invalid index stored in array | |
124 | * | |
125 | * Written by the kernel, shouldn't be modified by the | |
126 | * application (i.e. get number of "new events" by comparing to | |
127 | * cached value). | |
128 | * | |
129 | * After a new SQ head value was read by the application this | |
130 | * counter includes all submissions that were dropped reaching | |
131 | * the new SQ head (and possibly more). | |
132 | */ | |
75b28aff | 133 | u32 sq_dropped; |
1e84b97b SB |
134 | /* |
135 | * Runtime flags | |
136 | * | |
137 | * Written by the kernel, shouldn't be modified by the | |
138 | * application. | |
139 | * | |
140 | * The application needs a full memory barrier before checking | |
141 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
142 | */ | |
75b28aff | 143 | u32 sq_flags; |
1e84b97b SB |
144 | /* |
145 | * Number of completion events lost because the queue was full; | |
146 | * this should be avoided by the application by making sure | |
147 | * there are not more requests pending thatn there is space in | |
148 | * the completion queue. | |
149 | * | |
150 | * Written by the kernel, shouldn't be modified by the | |
151 | * application (i.e. get number of "new events" by comparing to | |
152 | * cached value). | |
153 | * | |
154 | * As completion events come in out of order this counter is not | |
155 | * ordered with any other data. | |
156 | */ | |
75b28aff | 157 | u32 cq_overflow; |
1e84b97b SB |
158 | /* |
159 | * Ring buffer of completion events. | |
160 | * | |
161 | * The kernel writes completion events fresh every time they are | |
162 | * produced, so the application is allowed to modify pending | |
163 | * entries. | |
164 | */ | |
75b28aff | 165 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
166 | }; |
167 | ||
edafccee JA |
168 | struct io_mapped_ubuf { |
169 | u64 ubuf; | |
170 | size_t len; | |
171 | struct bio_vec *bvec; | |
172 | unsigned int nr_bvecs; | |
173 | }; | |
174 | ||
65e19f54 JA |
175 | struct fixed_file_table { |
176 | struct file **files; | |
31b51510 JA |
177 | }; |
178 | ||
2b188cc1 JA |
179 | struct io_ring_ctx { |
180 | struct { | |
181 | struct percpu_ref refs; | |
182 | } ____cacheline_aligned_in_smp; | |
183 | ||
184 | struct { | |
185 | unsigned int flags; | |
186 | bool compat; | |
187 | bool account_mem; | |
1d7bb1d5 | 188 | bool cq_overflow_flushed; |
1b4a51b6 | 189 | bool drain_next; |
2b188cc1 | 190 | |
75b28aff HV |
191 | /* |
192 | * Ring buffer of indices into array of io_uring_sqe, which is | |
193 | * mmapped by the application using the IORING_OFF_SQES offset. | |
194 | * | |
195 | * This indirection could e.g. be used to assign fixed | |
196 | * io_uring_sqe entries to operations and only submit them to | |
197 | * the queue when needed. | |
198 | * | |
199 | * The kernel modifies neither the indices array nor the entries | |
200 | * array. | |
201 | */ | |
202 | u32 *sq_array; | |
2b188cc1 JA |
203 | unsigned cached_sq_head; |
204 | unsigned sq_entries; | |
205 | unsigned sq_mask; | |
6c271ce2 | 206 | unsigned sq_thread_idle; |
498ccd9e | 207 | unsigned cached_sq_dropped; |
206aefde | 208 | atomic_t cached_cq_overflow; |
2b188cc1 | 209 | struct io_uring_sqe *sq_sqes; |
de0617e4 JA |
210 | |
211 | struct list_head defer_list; | |
5262f567 | 212 | struct list_head timeout_list; |
1d7bb1d5 | 213 | struct list_head cq_overflow_list; |
fcb323cc JA |
214 | |
215 | wait_queue_head_t inflight_wait; | |
2b188cc1 JA |
216 | } ____cacheline_aligned_in_smp; |
217 | ||
206aefde JA |
218 | struct io_rings *rings; |
219 | ||
2b188cc1 | 220 | /* IO offload */ |
561fb04a | 221 | struct io_wq *io_wq; |
6c271ce2 | 222 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
2b188cc1 | 223 | struct mm_struct *sqo_mm; |
6c271ce2 | 224 | wait_queue_head_t sqo_wait; |
75b28aff | 225 | |
6b06314c JA |
226 | /* |
227 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
228 | * readers must ensure that ->refs is alive as long as the file* is | |
229 | * used. Only updated through io_uring_register(2). | |
230 | */ | |
65e19f54 | 231 | struct fixed_file_table *file_table; |
6b06314c JA |
232 | unsigned nr_user_files; |
233 | ||
edafccee JA |
234 | /* if used, fixed mapped user buffers */ |
235 | unsigned nr_user_bufs; | |
236 | struct io_mapped_ubuf *user_bufs; | |
237 | ||
2b188cc1 JA |
238 | struct user_struct *user; |
239 | ||
206aefde JA |
240 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
241 | struct completion *completions; | |
242 | ||
0ddf92e8 JA |
243 | /* if all else fails... */ |
244 | struct io_kiocb *fallback_req; | |
245 | ||
206aefde JA |
246 | #if defined(CONFIG_UNIX) |
247 | struct socket *ring_sock; | |
248 | #endif | |
249 | ||
250 | struct { | |
251 | unsigned cached_cq_tail; | |
252 | unsigned cq_entries; | |
253 | unsigned cq_mask; | |
254 | atomic_t cq_timeouts; | |
255 | struct wait_queue_head cq_wait; | |
256 | struct fasync_struct *cq_fasync; | |
257 | struct eventfd_ctx *cq_ev_fd; | |
258 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
259 | |
260 | struct { | |
261 | struct mutex uring_lock; | |
262 | wait_queue_head_t wait; | |
263 | } ____cacheline_aligned_in_smp; | |
264 | ||
265 | struct { | |
266 | spinlock_t completion_lock; | |
def596e9 JA |
267 | bool poll_multi_file; |
268 | /* | |
269 | * ->poll_list is protected by the ctx->uring_lock for | |
270 | * io_uring instances that don't use IORING_SETUP_SQPOLL. | |
271 | * For SQPOLL, only the single threaded io_sq_thread() will | |
272 | * manipulate the list, hence no extra locking is needed there. | |
273 | */ | |
274 | struct list_head poll_list; | |
eac406c6 | 275 | struct rb_root cancel_tree; |
31b51510 | 276 | |
fcb323cc JA |
277 | spinlock_t inflight_lock; |
278 | struct list_head inflight_list; | |
2b188cc1 | 279 | } ____cacheline_aligned_in_smp; |
2b188cc1 JA |
280 | }; |
281 | ||
282 | struct sqe_submit { | |
283 | const struct io_uring_sqe *sqe; | |
fcb323cc JA |
284 | struct file *ring_file; |
285 | int ring_fd; | |
8776f3fa | 286 | u32 sequence; |
2b188cc1 | 287 | bool has_user; |
ba5290cc | 288 | bool in_async; |
6c271ce2 | 289 | bool needs_fixed_file; |
2b188cc1 JA |
290 | }; |
291 | ||
09bb8394 JA |
292 | /* |
293 | * First field must be the file pointer in all the | |
294 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
295 | */ | |
221c5eb2 JA |
296 | struct io_poll_iocb { |
297 | struct file *file; | |
298 | struct wait_queue_head *head; | |
299 | __poll_t events; | |
8c838788 | 300 | bool done; |
221c5eb2 JA |
301 | bool canceled; |
302 | struct wait_queue_entry wait; | |
303 | }; | |
304 | ||
ad8a48ac JA |
305 | struct io_timeout_data { |
306 | struct io_kiocb *req; | |
307 | struct hrtimer timer; | |
308 | struct timespec64 ts; | |
309 | enum hrtimer_mode mode; | |
310 | }; | |
311 | ||
5262f567 JA |
312 | struct io_timeout { |
313 | struct file *file; | |
ad8a48ac | 314 | struct io_timeout_data *data; |
5262f567 JA |
315 | }; |
316 | ||
09bb8394 JA |
317 | /* |
318 | * NOTE! Each of the iocb union members has the file pointer | |
319 | * as the first entry in their struct definition. So you can | |
320 | * access the file pointer through any of the sub-structs, | |
321 | * or directly as just 'ki_filp' in this struct. | |
322 | */ | |
2b188cc1 | 323 | struct io_kiocb { |
221c5eb2 | 324 | union { |
09bb8394 | 325 | struct file *file; |
221c5eb2 JA |
326 | struct kiocb rw; |
327 | struct io_poll_iocb poll; | |
5262f567 | 328 | struct io_timeout timeout; |
221c5eb2 | 329 | }; |
2b188cc1 JA |
330 | |
331 | struct sqe_submit submit; | |
332 | ||
333 | struct io_ring_ctx *ctx; | |
eac406c6 JA |
334 | union { |
335 | struct list_head list; | |
336 | struct rb_node rb_node; | |
337 | }; | |
9e645e11 | 338 | struct list_head link_list; |
2b188cc1 | 339 | unsigned int flags; |
c16361c1 | 340 | refcount_t refs; |
8449eeda | 341 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
def596e9 | 342 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
6b06314c | 343 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
4d7dd462 | 344 | #define REQ_F_LINK_NEXT 8 /* already grabbed next link */ |
e2033e33 SB |
345 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
346 | #define REQ_F_IO_DRAINED 32 /* drain done */ | |
9e645e11 | 347 | #define REQ_F_LINK 64 /* linked sqes */ |
2665abfd | 348 | #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */ |
f7b76ac9 | 349 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
1b4a51b6 | 350 | #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */ |
5262f567 | 351 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
491381ce JA |
352 | #define REQ_F_ISREG 2048 /* regular file */ |
353 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ | |
93bd25bb | 354 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
fb4b3d3f LT |
355 | #define REQ_F_INFLIGHT 16384 /* on inflight list */ |
356 | #define REQ_F_COMP_LOCKED 32768 /* completion under lock */ | |
94ae5e77 | 357 | #define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */ |
2b188cc1 | 358 | u64 user_data; |
9e645e11 | 359 | u32 result; |
de0617e4 | 360 | u32 sequence; |
2b188cc1 | 361 | |
fcb323cc JA |
362 | struct list_head inflight_entry; |
363 | ||
561fb04a | 364 | struct io_wq_work work; |
2b188cc1 JA |
365 | }; |
366 | ||
367 | #define IO_PLUG_THRESHOLD 2 | |
def596e9 | 368 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 369 | |
9a56a232 JA |
370 | struct io_submit_state { |
371 | struct blk_plug plug; | |
372 | ||
2579f913 JA |
373 | /* |
374 | * io_kiocb alloc cache | |
375 | */ | |
376 | void *reqs[IO_IOPOLL_BATCH]; | |
377 | unsigned int free_reqs; | |
378 | unsigned int cur_req; | |
379 | ||
9a56a232 JA |
380 | /* |
381 | * File reference cache | |
382 | */ | |
383 | struct file *file; | |
384 | unsigned int fd; | |
385 | unsigned int has_refs; | |
386 | unsigned int used_refs; | |
387 | unsigned int ios_left; | |
388 | }; | |
389 | ||
561fb04a | 390 | static void io_wq_submit_work(struct io_wq_work **workptr); |
78e19bbe | 391 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
4fe2c963 | 392 | static void __io_free_req(struct io_kiocb *req); |
ec9c02ad | 393 | static void io_put_req(struct io_kiocb *req); |
78e19bbe | 394 | static void io_double_put_req(struct io_kiocb *req); |
978db57e | 395 | static void __io_double_put_req(struct io_kiocb *req); |
94ae5e77 JA |
396 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
397 | static void io_queue_linked_timeout(struct io_kiocb *req); | |
de0617e4 | 398 | |
2b188cc1 JA |
399 | static struct kmem_cache *req_cachep; |
400 | ||
401 | static const struct file_operations io_uring_fops; | |
402 | ||
403 | struct sock *io_uring_get_socket(struct file *file) | |
404 | { | |
405 | #if defined(CONFIG_UNIX) | |
406 | if (file->f_op == &io_uring_fops) { | |
407 | struct io_ring_ctx *ctx = file->private_data; | |
408 | ||
409 | return ctx->ring_sock->sk; | |
410 | } | |
411 | #endif | |
412 | return NULL; | |
413 | } | |
414 | EXPORT_SYMBOL(io_uring_get_socket); | |
415 | ||
416 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) | |
417 | { | |
418 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
419 | ||
206aefde | 420 | complete(&ctx->completions[0]); |
2b188cc1 JA |
421 | } |
422 | ||
423 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) | |
424 | { | |
425 | struct io_ring_ctx *ctx; | |
426 | ||
427 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
428 | if (!ctx) | |
429 | return NULL; | |
430 | ||
0ddf92e8 JA |
431 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
432 | if (!ctx->fallback_req) | |
433 | goto err; | |
434 | ||
206aefde JA |
435 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
436 | if (!ctx->completions) | |
437 | goto err; | |
438 | ||
21482896 | 439 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
440 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
441 | goto err; | |
2b188cc1 JA |
442 | |
443 | ctx->flags = p->flags; | |
444 | init_waitqueue_head(&ctx->cq_wait); | |
1d7bb1d5 | 445 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
206aefde JA |
446 | init_completion(&ctx->completions[0]); |
447 | init_completion(&ctx->completions[1]); | |
2b188cc1 JA |
448 | mutex_init(&ctx->uring_lock); |
449 | init_waitqueue_head(&ctx->wait); | |
450 | spin_lock_init(&ctx->completion_lock); | |
def596e9 | 451 | INIT_LIST_HEAD(&ctx->poll_list); |
eac406c6 | 452 | ctx->cancel_tree = RB_ROOT; |
de0617e4 | 453 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 454 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
455 | init_waitqueue_head(&ctx->inflight_wait); |
456 | spin_lock_init(&ctx->inflight_lock); | |
457 | INIT_LIST_HEAD(&ctx->inflight_list); | |
2b188cc1 | 458 | return ctx; |
206aefde | 459 | err: |
0ddf92e8 JA |
460 | if (ctx->fallback_req) |
461 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
206aefde JA |
462 | kfree(ctx->completions); |
463 | kfree(ctx); | |
464 | return NULL; | |
2b188cc1 JA |
465 | } |
466 | ||
9d858b21 | 467 | static inline bool __req_need_defer(struct io_kiocb *req) |
7adf4eaf | 468 | { |
a197f664 JL |
469 | struct io_ring_ctx *ctx = req->ctx; |
470 | ||
498ccd9e JA |
471 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
472 | + atomic_read(&ctx->cached_cq_overflow); | |
7adf4eaf JA |
473 | } |
474 | ||
9d858b21 | 475 | static inline bool req_need_defer(struct io_kiocb *req) |
de0617e4 | 476 | { |
9d858b21 BL |
477 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
478 | return __req_need_defer(req); | |
de0617e4 | 479 | |
9d858b21 | 480 | return false; |
de0617e4 JA |
481 | } |
482 | ||
7adf4eaf | 483 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
de0617e4 JA |
484 | { |
485 | struct io_kiocb *req; | |
486 | ||
7adf4eaf | 487 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
9d858b21 | 488 | if (req && !req_need_defer(req)) { |
de0617e4 JA |
489 | list_del_init(&req->list); |
490 | return req; | |
491 | } | |
492 | ||
493 | return NULL; | |
494 | } | |
495 | ||
5262f567 JA |
496 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
497 | { | |
7adf4eaf JA |
498 | struct io_kiocb *req; |
499 | ||
500 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); | |
93bd25bb JA |
501 | if (req) { |
502 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) | |
503 | return NULL; | |
fb4b3d3f | 504 | if (!__req_need_defer(req)) { |
93bd25bb JA |
505 | list_del_init(&req->list); |
506 | return req; | |
507 | } | |
7adf4eaf JA |
508 | } |
509 | ||
510 | return NULL; | |
5262f567 JA |
511 | } |
512 | ||
de0617e4 | 513 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 514 | { |
75b28aff | 515 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 516 | |
75b28aff | 517 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
2b188cc1 | 518 | /* order cqe stores with ring update */ |
75b28aff | 519 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
2b188cc1 | 520 | |
2b188cc1 JA |
521 | if (wq_has_sleeper(&ctx->cq_wait)) { |
522 | wake_up_interruptible(&ctx->cq_wait); | |
523 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
524 | } | |
525 | } | |
526 | } | |
527 | ||
561fb04a | 528 | static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) |
18d9be1a | 529 | { |
561fb04a JA |
530 | u8 opcode = READ_ONCE(sqe->opcode); |
531 | ||
532 | return !(opcode == IORING_OP_READ_FIXED || | |
533 | opcode == IORING_OP_WRITE_FIXED); | |
534 | } | |
535 | ||
94ae5e77 JA |
536 | static inline bool io_prep_async_work(struct io_kiocb *req, |
537 | struct io_kiocb **link) | |
18d9be1a | 538 | { |
561fb04a | 539 | bool do_hashed = false; |
54a91f3b | 540 | |
6cc47d1d JA |
541 | if (req->submit.sqe) { |
542 | switch (req->submit.sqe->opcode) { | |
543 | case IORING_OP_WRITEV: | |
544 | case IORING_OP_WRITE_FIXED: | |
561fb04a | 545 | do_hashed = true; |
5f8fd2d3 JA |
546 | /* fall-through */ |
547 | case IORING_OP_READV: | |
548 | case IORING_OP_READ_FIXED: | |
549 | case IORING_OP_SENDMSG: | |
550 | case IORING_OP_RECVMSG: | |
551 | case IORING_OP_ACCEPT: | |
552 | case IORING_OP_POLL_ADD: | |
553 | /* | |
554 | * We know REQ_F_ISREG is not set on some of these | |
555 | * opcodes, but this enables us to keep the check in | |
556 | * just one place. | |
557 | */ | |
558 | if (!(req->flags & REQ_F_ISREG)) | |
559 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
6cc47d1d JA |
560 | break; |
561 | } | |
561fb04a JA |
562 | if (io_sqe_needs_user(req->submit.sqe)) |
563 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; | |
54a91f3b JA |
564 | } |
565 | ||
94ae5e77 | 566 | *link = io_prep_linked_timeout(req); |
561fb04a JA |
567 | return do_hashed; |
568 | } | |
569 | ||
a197f664 | 570 | static inline void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 571 | { |
a197f664 | 572 | struct io_ring_ctx *ctx = req->ctx; |
94ae5e77 JA |
573 | struct io_kiocb *link; |
574 | bool do_hashed; | |
575 | ||
576 | do_hashed = io_prep_async_work(req, &link); | |
561fb04a JA |
577 | |
578 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, | |
579 | req->flags); | |
580 | if (!do_hashed) { | |
581 | io_wq_enqueue(ctx->io_wq, &req->work); | |
582 | } else { | |
583 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, | |
584 | file_inode(req->file)); | |
585 | } | |
94ae5e77 JA |
586 | |
587 | if (link) | |
588 | io_queue_linked_timeout(link); | |
18d9be1a JA |
589 | } |
590 | ||
5262f567 JA |
591 | static void io_kill_timeout(struct io_kiocb *req) |
592 | { | |
593 | int ret; | |
594 | ||
ad8a48ac | 595 | ret = hrtimer_try_to_cancel(&req->timeout.data->timer); |
5262f567 JA |
596 | if (ret != -1) { |
597 | atomic_inc(&req->ctx->cq_timeouts); | |
842f9612 | 598 | list_del_init(&req->list); |
78e19bbe | 599 | io_cqring_fill_event(req, 0); |
ec9c02ad | 600 | io_put_req(req); |
5262f567 JA |
601 | } |
602 | } | |
603 | ||
604 | static void io_kill_timeouts(struct io_ring_ctx *ctx) | |
605 | { | |
606 | struct io_kiocb *req, *tmp; | |
607 | ||
608 | spin_lock_irq(&ctx->completion_lock); | |
609 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) | |
610 | io_kill_timeout(req); | |
611 | spin_unlock_irq(&ctx->completion_lock); | |
612 | } | |
613 | ||
de0617e4 JA |
614 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
615 | { | |
616 | struct io_kiocb *req; | |
617 | ||
5262f567 JA |
618 | while ((req = io_get_timeout_req(ctx)) != NULL) |
619 | io_kill_timeout(req); | |
620 | ||
de0617e4 JA |
621 | __io_commit_cqring(ctx); |
622 | ||
623 | while ((req = io_get_deferred_req(ctx)) != NULL) { | |
624 | req->flags |= REQ_F_IO_DRAINED; | |
a197f664 | 625 | io_queue_async_work(req); |
de0617e4 JA |
626 | } |
627 | } | |
628 | ||
2b188cc1 JA |
629 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
630 | { | |
75b28aff | 631 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
632 | unsigned tail; |
633 | ||
634 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
635 | /* |
636 | * writes to the cq entry need to come after reading head; the | |
637 | * control dependency is enough as we're using WRITE_ONCE to | |
638 | * fill the cq entry | |
639 | */ | |
75b28aff | 640 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
641 | return NULL; |
642 | ||
643 | ctx->cached_cq_tail++; | |
75b28aff | 644 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
645 | } |
646 | ||
1d7bb1d5 JA |
647 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
648 | { | |
649 | if (waitqueue_active(&ctx->wait)) | |
650 | wake_up(&ctx->wait); | |
651 | if (waitqueue_active(&ctx->sqo_wait)) | |
652 | wake_up(&ctx->sqo_wait); | |
653 | if (ctx->cq_ev_fd) | |
654 | eventfd_signal(ctx->cq_ev_fd, 1); | |
655 | } | |
656 | ||
657 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) | |
658 | { | |
659 | struct io_rings *rings = ctx->rings; | |
660 | struct io_uring_cqe *cqe; | |
661 | struct io_kiocb *req; | |
662 | unsigned long flags; | |
663 | LIST_HEAD(list); | |
664 | ||
665 | if (!force) { | |
666 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
667 | return; | |
668 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == | |
669 | rings->cq_ring_entries)) | |
670 | return; | |
671 | } | |
672 | ||
673 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
674 | ||
675 | /* if force is set, the ring is going away. always drop after that */ | |
676 | if (force) | |
677 | ctx->cq_overflow_flushed = true; | |
678 | ||
679 | while (!list_empty(&ctx->cq_overflow_list)) { | |
680 | cqe = io_get_cqring(ctx); | |
681 | if (!cqe && !force) | |
682 | break; | |
683 | ||
684 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, | |
685 | list); | |
686 | list_move(&req->list, &list); | |
687 | if (cqe) { | |
688 | WRITE_ONCE(cqe->user_data, req->user_data); | |
689 | WRITE_ONCE(cqe->res, req->result); | |
690 | WRITE_ONCE(cqe->flags, 0); | |
691 | } else { | |
692 | WRITE_ONCE(ctx->rings->cq_overflow, | |
693 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
694 | } | |
695 | } | |
696 | ||
697 | io_commit_cqring(ctx); | |
698 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
699 | io_cqring_ev_posted(ctx); | |
700 | ||
701 | while (!list_empty(&list)) { | |
702 | req = list_first_entry(&list, struct io_kiocb, list); | |
703 | list_del(&req->list); | |
ec9c02ad | 704 | io_put_req(req); |
1d7bb1d5 JA |
705 | } |
706 | } | |
707 | ||
78e19bbe | 708 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
2b188cc1 | 709 | { |
78e19bbe | 710 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
711 | struct io_uring_cqe *cqe; |
712 | ||
78e19bbe | 713 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 714 | |
2b188cc1 JA |
715 | /* |
716 | * If we can't get a cq entry, userspace overflowed the | |
717 | * submission (by quite a lot). Increment the overflow count in | |
718 | * the ring. | |
719 | */ | |
720 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 721 | if (likely(cqe)) { |
78e19bbe | 722 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 723 | WRITE_ONCE(cqe->res, res); |
c71ffb67 | 724 | WRITE_ONCE(cqe->flags, 0); |
1d7bb1d5 | 725 | } else if (ctx->cq_overflow_flushed) { |
498ccd9e JA |
726 | WRITE_ONCE(ctx->rings->cq_overflow, |
727 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1d7bb1d5 JA |
728 | } else { |
729 | refcount_inc(&req->refs); | |
730 | req->result = res; | |
731 | list_add_tail(&req->list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
732 | } |
733 | } | |
734 | ||
78e19bbe | 735 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
2b188cc1 | 736 | { |
78e19bbe | 737 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
738 | unsigned long flags; |
739 | ||
740 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
78e19bbe | 741 | io_cqring_fill_event(req, res); |
2b188cc1 JA |
742 | io_commit_cqring(ctx); |
743 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
744 | ||
8c838788 | 745 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
746 | } |
747 | ||
0ddf92e8 JA |
748 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
749 | { | |
750 | return req == (struct io_kiocb *) | |
751 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
752 | } | |
753 | ||
754 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
755 | { | |
756 | struct io_kiocb *req; | |
757 | ||
758 | req = ctx->fallback_req; | |
759 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) | |
760 | return req; | |
761 | ||
762 | return NULL; | |
763 | } | |
764 | ||
2579f913 JA |
765 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
766 | struct io_submit_state *state) | |
2b188cc1 | 767 | { |
fd6fab2c | 768 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2b188cc1 JA |
769 | struct io_kiocb *req; |
770 | ||
771 | if (!percpu_ref_tryget(&ctx->refs)) | |
772 | return NULL; | |
773 | ||
2579f913 | 774 | if (!state) { |
fd6fab2c | 775 | req = kmem_cache_alloc(req_cachep, gfp); |
2579f913 | 776 | if (unlikely(!req)) |
0ddf92e8 | 777 | goto fallback; |
2579f913 JA |
778 | } else if (!state->free_reqs) { |
779 | size_t sz; | |
780 | int ret; | |
781 | ||
782 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
783 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
784 | ||
785 | /* | |
786 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
787 | * retry single alloc to be on the safe side. | |
788 | */ | |
789 | if (unlikely(ret <= 0)) { | |
790 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
791 | if (!state->reqs[0]) | |
0ddf92e8 | 792 | goto fallback; |
fd6fab2c JA |
793 | ret = 1; |
794 | } | |
2579f913 JA |
795 | state->free_reqs = ret - 1; |
796 | state->cur_req = 1; | |
797 | req = state->reqs[0]; | |
798 | } else { | |
799 | req = state->reqs[state->cur_req]; | |
800 | state->free_reqs--; | |
801 | state->cur_req++; | |
2b188cc1 JA |
802 | } |
803 | ||
0ddf92e8 | 804 | got_it: |
60c112b0 | 805 | req->file = NULL; |
2579f913 JA |
806 | req->ctx = ctx; |
807 | req->flags = 0; | |
e65ef56d JA |
808 | /* one is dropped after submission, the other at completion */ |
809 | refcount_set(&req->refs, 2); | |
9e645e11 | 810 | req->result = 0; |
561fb04a | 811 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
2579f913 | 812 | return req; |
0ddf92e8 JA |
813 | fallback: |
814 | req = io_get_fallback_req(ctx); | |
815 | if (req) | |
816 | goto got_it; | |
6805b32e | 817 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
818 | return NULL; |
819 | } | |
820 | ||
def596e9 JA |
821 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
822 | { | |
823 | if (*nr) { | |
824 | kmem_cache_free_bulk(req_cachep, *nr, reqs); | |
6805b32e | 825 | percpu_ref_put_many(&ctx->refs, *nr); |
def596e9 JA |
826 | *nr = 0; |
827 | } | |
828 | } | |
829 | ||
9e645e11 | 830 | static void __io_free_req(struct io_kiocb *req) |
2b188cc1 | 831 | { |
fcb323cc JA |
832 | struct io_ring_ctx *ctx = req->ctx; |
833 | ||
bbad27b2 PB |
834 | if (req->flags & REQ_F_FREE_SQE) |
835 | kfree(req->submit.sqe); | |
09bb8394 JA |
836 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
837 | fput(req->file); | |
fcb323cc JA |
838 | if (req->flags & REQ_F_INFLIGHT) { |
839 | unsigned long flags; | |
840 | ||
841 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
842 | list_del(&req->inflight_entry); | |
843 | if (waitqueue_active(&ctx->inflight_wait)) | |
844 | wake_up(&ctx->inflight_wait); | |
845 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
846 | } | |
ad8a48ac JA |
847 | if (req->flags & REQ_F_TIMEOUT) |
848 | kfree(req->timeout.data); | |
fcb323cc | 849 | percpu_ref_put(&ctx->refs); |
0ddf92e8 JA |
850 | if (likely(!io_is_fallback_req(req))) |
851 | kmem_cache_free(req_cachep, req); | |
852 | else | |
853 | clear_bit_unlock(0, (unsigned long *) ctx->fallback_req); | |
e65ef56d JA |
854 | } |
855 | ||
a197f664 | 856 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
2665abfd | 857 | { |
a197f664 | 858 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
859 | int ret; |
860 | ||
ad8a48ac | 861 | ret = hrtimer_try_to_cancel(&req->timeout.data->timer); |
2665abfd | 862 | if (ret != -1) { |
78e19bbe | 863 | io_cqring_fill_event(req, -ECANCELED); |
2665abfd JA |
864 | io_commit_cqring(ctx); |
865 | req->flags &= ~REQ_F_LINK; | |
ec9c02ad | 866 | io_put_req(req); |
2665abfd JA |
867 | return true; |
868 | } | |
869 | ||
870 | return false; | |
e65ef56d JA |
871 | } |
872 | ||
ba816ad6 | 873 | static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
9e645e11 | 874 | { |
2665abfd | 875 | struct io_ring_ctx *ctx = req->ctx; |
9e645e11 | 876 | struct io_kiocb *nxt; |
2665abfd | 877 | bool wake_ev = false; |
9e645e11 | 878 | |
4d7dd462 JA |
879 | /* Already got next link */ |
880 | if (req->flags & REQ_F_LINK_NEXT) | |
881 | return; | |
882 | ||
9e645e11 JA |
883 | /* |
884 | * The list should never be empty when we are called here. But could | |
885 | * potentially happen if the chain is messed up, check to be on the | |
886 | * safe side. | |
887 | */ | |
888 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); | |
2665abfd | 889 | while (nxt) { |
76a46e06 | 890 | list_del_init(&nxt->list); |
94ae5e77 JA |
891 | |
892 | if ((req->flags & REQ_F_LINK_TIMEOUT) && | |
893 | (nxt->flags & REQ_F_TIMEOUT)) { | |
894 | wake_ev |= io_link_cancel_timeout(nxt); | |
895 | nxt = list_first_entry_or_null(&req->link_list, | |
896 | struct io_kiocb, list); | |
897 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
898 | continue; | |
899 | } | |
9e645e11 JA |
900 | if (!list_empty(&req->link_list)) { |
901 | INIT_LIST_HEAD(&nxt->link_list); | |
902 | list_splice(&req->link_list, &nxt->link_list); | |
903 | nxt->flags |= REQ_F_LINK; | |
904 | } | |
905 | ||
ba816ad6 JA |
906 | /* |
907 | * If we're in async work, we can continue processing the chain | |
908 | * in this context instead of having to queue up new async work. | |
909 | */ | |
94ae5e77 JA |
910 | if (nxt) { |
911 | if (nxtptr && io_wq_current_is_worker()) | |
912 | *nxtptr = nxt; | |
913 | else | |
914 | io_queue_async_work(nxt); | |
2665abfd | 915 | } |
94ae5e77 | 916 | break; |
9e645e11 | 917 | } |
2665abfd | 918 | |
4d7dd462 | 919 | req->flags |= REQ_F_LINK_NEXT; |
2665abfd JA |
920 | if (wake_ev) |
921 | io_cqring_ev_posted(ctx); | |
9e645e11 JA |
922 | } |
923 | ||
924 | /* | |
925 | * Called if REQ_F_LINK is set, and we fail the head request | |
926 | */ | |
927 | static void io_fail_links(struct io_kiocb *req) | |
928 | { | |
2665abfd | 929 | struct io_ring_ctx *ctx = req->ctx; |
9e645e11 | 930 | struct io_kiocb *link; |
2665abfd JA |
931 | unsigned long flags; |
932 | ||
933 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
9e645e11 JA |
934 | |
935 | while (!list_empty(&req->link_list)) { | |
936 | link = list_first_entry(&req->link_list, struct io_kiocb, list); | |
2665abfd | 937 | list_del_init(&link->list); |
9e645e11 | 938 | |
c826bd7a | 939 | trace_io_uring_fail_link(req, link); |
2665abfd JA |
940 | |
941 | if ((req->flags & REQ_F_LINK_TIMEOUT) && | |
942 | link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) { | |
a197f664 | 943 | io_link_cancel_timeout(link); |
2665abfd | 944 | } else { |
78e19bbe | 945 | io_cqring_fill_event(link, -ECANCELED); |
978db57e | 946 | __io_double_put_req(link); |
2665abfd | 947 | } |
5d960724 | 948 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
9e645e11 | 949 | } |
2665abfd JA |
950 | |
951 | io_commit_cqring(ctx); | |
952 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
953 | io_cqring_ev_posted(ctx); | |
9e645e11 JA |
954 | } |
955 | ||
4d7dd462 | 956 | static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt) |
9e645e11 | 957 | { |
4d7dd462 | 958 | if (likely(!(req->flags & REQ_F_LINK))) |
2665abfd | 959 | return; |
2665abfd | 960 | |
9e645e11 JA |
961 | /* |
962 | * If LINK is set, we have dependent requests in this chain. If we | |
963 | * didn't fail this request, queue the first one up, moving any other | |
964 | * dependencies to the next request. In case of failure, fail the rest | |
965 | * of the chain. | |
966 | */ | |
2665abfd JA |
967 | if (req->flags & REQ_F_FAIL_LINK) { |
968 | io_fail_links(req); | |
7c9e7f0f JA |
969 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
970 | REQ_F_LINK_TIMEOUT) { | |
2665abfd JA |
971 | struct io_ring_ctx *ctx = req->ctx; |
972 | unsigned long flags; | |
973 | ||
974 | /* | |
975 | * If this is a timeout link, we could be racing with the | |
976 | * timeout timer. Grab the completion lock for this case to | |
7c9e7f0f | 977 | * protect against that. |
2665abfd JA |
978 | */ |
979 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
980 | io_req_link_next(req, nxt); | |
981 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
982 | } else { | |
983 | io_req_link_next(req, nxt); | |
9e645e11 | 984 | } |
4d7dd462 | 985 | } |
9e645e11 | 986 | |
4d7dd462 JA |
987 | static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt) |
988 | { | |
989 | io_req_find_next(req, nxt); | |
9e645e11 JA |
990 | __io_free_req(req); |
991 | } | |
992 | ||
c69f8dbe JL |
993 | static void io_free_req(struct io_kiocb *req) |
994 | { | |
995 | io_free_req_find_next(req, NULL); | |
996 | } | |
997 | ||
ba816ad6 JA |
998 | /* |
999 | * Drop reference to request, return next in chain (if there is one) if this | |
1000 | * was the last reference to this request. | |
1001 | */ | |
ec9c02ad | 1002 | static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
e65ef56d | 1003 | { |
ba816ad6 JA |
1004 | struct io_kiocb *nxt = NULL; |
1005 | ||
4d7dd462 JA |
1006 | io_req_find_next(req, &nxt); |
1007 | ||
e65ef56d | 1008 | if (refcount_dec_and_test(&req->refs)) |
4d7dd462 | 1009 | __io_free_req(req); |
ba816ad6 | 1010 | |
ba816ad6 | 1011 | if (nxt) { |
561fb04a | 1012 | if (nxtptr) |
ba816ad6 | 1013 | *nxtptr = nxt; |
561fb04a | 1014 | else |
a197f664 | 1015 | io_queue_async_work(nxt); |
ba816ad6 | 1016 | } |
2b188cc1 JA |
1017 | } |
1018 | ||
e65ef56d JA |
1019 | static void io_put_req(struct io_kiocb *req) |
1020 | { | |
1021 | if (refcount_dec_and_test(&req->refs)) | |
1022 | io_free_req(req); | |
2b188cc1 JA |
1023 | } |
1024 | ||
978db57e JA |
1025 | /* |
1026 | * Must only be used if we don't need to care about links, usually from | |
1027 | * within the completion handling itself. | |
1028 | */ | |
1029 | static void __io_double_put_req(struct io_kiocb *req) | |
78e19bbe JA |
1030 | { |
1031 | /* drop both submit and complete references */ | |
1032 | if (refcount_sub_and_test(2, &req->refs)) | |
1033 | __io_free_req(req); | |
1034 | } | |
1035 | ||
978db57e JA |
1036 | static void io_double_put_req(struct io_kiocb *req) |
1037 | { | |
1038 | /* drop both submit and complete references */ | |
1039 | if (refcount_sub_and_test(2, &req->refs)) | |
1040 | io_free_req(req); | |
1041 | } | |
1042 | ||
1d7bb1d5 | 1043 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 1044 | { |
84f97dc2 JA |
1045 | struct io_rings *rings = ctx->rings; |
1046 | ||
1d7bb1d5 JA |
1047 | /* |
1048 | * noflush == true is from the waitqueue handler, just ensure we wake | |
1049 | * up the task, and the next invocation will flush the entries. We | |
1050 | * cannot safely to it from here. | |
1051 | */ | |
1052 | if (noflush && !list_empty(&ctx->cq_overflow_list)) | |
1053 | return -1U; | |
1054 | ||
1055 | io_cqring_overflow_flush(ctx, false); | |
1056 | ||
a3a0e43f JA |
1057 | /* See comment at the top of this file */ |
1058 | smp_rmb(); | |
75b28aff | 1059 | return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
1060 | } |
1061 | ||
fb5ccc98 PB |
1062 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
1063 | { | |
1064 | struct io_rings *rings = ctx->rings; | |
1065 | ||
1066 | /* make sure SQ entry isn't read before tail */ | |
1067 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
1068 | } | |
1069 | ||
def596e9 JA |
1070 | /* |
1071 | * Find and free completed poll iocbs | |
1072 | */ | |
1073 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1074 | struct list_head *done) | |
1075 | { | |
1076 | void *reqs[IO_IOPOLL_BATCH]; | |
1077 | struct io_kiocb *req; | |
09bb8394 | 1078 | int to_free; |
def596e9 | 1079 | |
09bb8394 | 1080 | to_free = 0; |
def596e9 JA |
1081 | while (!list_empty(done)) { |
1082 | req = list_first_entry(done, struct io_kiocb, list); | |
1083 | list_del(&req->list); | |
1084 | ||
78e19bbe | 1085 | io_cqring_fill_event(req, req->result); |
def596e9 JA |
1086 | (*nr_events)++; |
1087 | ||
09bb8394 JA |
1088 | if (refcount_dec_and_test(&req->refs)) { |
1089 | /* If we're not using fixed files, we have to pair the | |
1090 | * completion part with the file put. Use regular | |
1091 | * completions for those, only batch free for fixed | |
9e645e11 | 1092 | * file and non-linked commands. |
09bb8394 | 1093 | */ |
bbad27b2 PB |
1094 | if (((req->flags & |
1095 | (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) == | |
0ddf92e8 | 1096 | REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) { |
09bb8394 JA |
1097 | reqs[to_free++] = req; |
1098 | if (to_free == ARRAY_SIZE(reqs)) | |
1099 | io_free_req_many(ctx, reqs, &to_free); | |
6b06314c | 1100 | } else { |
09bb8394 | 1101 | io_free_req(req); |
6b06314c | 1102 | } |
9a56a232 | 1103 | } |
def596e9 | 1104 | } |
def596e9 | 1105 | |
09bb8394 | 1106 | io_commit_cqring(ctx); |
def596e9 JA |
1107 | io_free_req_many(ctx, reqs, &to_free); |
1108 | } | |
1109 | ||
1110 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1111 | long min) | |
1112 | { | |
1113 | struct io_kiocb *req, *tmp; | |
1114 | LIST_HEAD(done); | |
1115 | bool spin; | |
1116 | int ret; | |
1117 | ||
1118 | /* | |
1119 | * Only spin for completions if we don't have multiple devices hanging | |
1120 | * off our complete list, and we're under the requested amount. | |
1121 | */ | |
1122 | spin = !ctx->poll_multi_file && *nr_events < min; | |
1123 | ||
1124 | ret = 0; | |
1125 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { | |
1126 | struct kiocb *kiocb = &req->rw; | |
1127 | ||
1128 | /* | |
1129 | * Move completed entries to our local list. If we find a | |
1130 | * request that requires polling, break out and complete | |
1131 | * the done list first, if we have entries there. | |
1132 | */ | |
1133 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { | |
1134 | list_move_tail(&req->list, &done); | |
1135 | continue; | |
1136 | } | |
1137 | if (!list_empty(&done)) | |
1138 | break; | |
1139 | ||
1140 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
1141 | if (ret < 0) | |
1142 | break; | |
1143 | ||
1144 | if (ret && spin) | |
1145 | spin = false; | |
1146 | ret = 0; | |
1147 | } | |
1148 | ||
1149 | if (!list_empty(&done)) | |
1150 | io_iopoll_complete(ctx, nr_events, &done); | |
1151 | ||
1152 | return ret; | |
1153 | } | |
1154 | ||
1155 | /* | |
1156 | * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a | |
1157 | * non-spinning poll check - we'll still enter the driver poll loop, but only | |
1158 | * as a non-spinning completion check. | |
1159 | */ | |
1160 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1161 | long min) | |
1162 | { | |
08f5439f | 1163 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
def596e9 JA |
1164 | int ret; |
1165 | ||
1166 | ret = io_do_iopoll(ctx, nr_events, min); | |
1167 | if (ret < 0) | |
1168 | return ret; | |
1169 | if (!min || *nr_events >= min) | |
1170 | return 0; | |
1171 | } | |
1172 | ||
1173 | return 1; | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * We can't just wait for polled events to come to us, we have to actively | |
1178 | * find and complete them. | |
1179 | */ | |
1180 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) | |
1181 | { | |
1182 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
1183 | return; | |
1184 | ||
1185 | mutex_lock(&ctx->uring_lock); | |
1186 | while (!list_empty(&ctx->poll_list)) { | |
1187 | unsigned int nr_events = 0; | |
1188 | ||
1189 | io_iopoll_getevents(ctx, &nr_events, 1); | |
08f5439f JA |
1190 | |
1191 | /* | |
1192 | * Ensure we allow local-to-the-cpu processing to take place, | |
1193 | * in this case we need to ensure that we reap all events. | |
1194 | */ | |
1195 | cond_resched(); | |
def596e9 JA |
1196 | } |
1197 | mutex_unlock(&ctx->uring_lock); | |
1198 | } | |
1199 | ||
2b2ed975 JA |
1200 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
1201 | long min) | |
def596e9 | 1202 | { |
2b2ed975 | 1203 | int iters = 0, ret = 0; |
500f9fba | 1204 | |
def596e9 JA |
1205 | do { |
1206 | int tmin = 0; | |
1207 | ||
a3a0e43f JA |
1208 | /* |
1209 | * Don't enter poll loop if we already have events pending. | |
1210 | * If we do, we can potentially be spinning for commands that | |
1211 | * already triggered a CQE (eg in error). | |
1212 | */ | |
1d7bb1d5 | 1213 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
1214 | break; |
1215 | ||
500f9fba JA |
1216 | /* |
1217 | * If a submit got punted to a workqueue, we can have the | |
1218 | * application entering polling for a command before it gets | |
1219 | * issued. That app will hold the uring_lock for the duration | |
1220 | * of the poll right here, so we need to take a breather every | |
1221 | * now and then to ensure that the issue has a chance to add | |
1222 | * the poll to the issued list. Otherwise we can spin here | |
1223 | * forever, while the workqueue is stuck trying to acquire the | |
1224 | * very same mutex. | |
1225 | */ | |
1226 | if (!(++iters & 7)) { | |
1227 | mutex_unlock(&ctx->uring_lock); | |
1228 | mutex_lock(&ctx->uring_lock); | |
1229 | } | |
1230 | ||
def596e9 JA |
1231 | if (*nr_events < min) |
1232 | tmin = min - *nr_events; | |
1233 | ||
1234 | ret = io_iopoll_getevents(ctx, nr_events, tmin); | |
1235 | if (ret <= 0) | |
1236 | break; | |
1237 | ret = 0; | |
1238 | } while (min && !*nr_events && !need_resched()); | |
1239 | ||
2b2ed975 JA |
1240 | return ret; |
1241 | } | |
1242 | ||
1243 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, | |
1244 | long min) | |
1245 | { | |
1246 | int ret; | |
1247 | ||
1248 | /* | |
1249 | * We disallow the app entering submit/complete with polling, but we | |
1250 | * still need to lock the ring to prevent racing with polled issue | |
1251 | * that got punted to a workqueue. | |
1252 | */ | |
1253 | mutex_lock(&ctx->uring_lock); | |
1254 | ret = __io_iopoll_check(ctx, nr_events, min); | |
500f9fba | 1255 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
1256 | return ret; |
1257 | } | |
1258 | ||
491381ce | 1259 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 1260 | { |
491381ce JA |
1261 | /* |
1262 | * Tell lockdep we inherited freeze protection from submission | |
1263 | * thread. | |
1264 | */ | |
1265 | if (req->flags & REQ_F_ISREG) { | |
1266 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 1267 | |
491381ce | 1268 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 1269 | } |
491381ce | 1270 | file_end_write(req->file); |
2b188cc1 JA |
1271 | } |
1272 | ||
ba816ad6 | 1273 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
2b188cc1 JA |
1274 | { |
1275 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
1276 | ||
491381ce JA |
1277 | if (kiocb->ki_flags & IOCB_WRITE) |
1278 | kiocb_end_write(req); | |
2b188cc1 | 1279 | |
9e645e11 JA |
1280 | if ((req->flags & REQ_F_LINK) && res != req->result) |
1281 | req->flags |= REQ_F_FAIL_LINK; | |
78e19bbe | 1282 | io_cqring_add_event(req, res); |
ba816ad6 JA |
1283 | } |
1284 | ||
1285 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
1286 | { | |
1287 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
1288 | ||
1289 | io_complete_rw_common(kiocb, res); | |
e65ef56d | 1290 | io_put_req(req); |
2b188cc1 JA |
1291 | } |
1292 | ||
ba816ad6 JA |
1293 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
1294 | { | |
1295 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
ec9c02ad | 1296 | struct io_kiocb *nxt = NULL; |
ba816ad6 JA |
1297 | |
1298 | io_complete_rw_common(kiocb, res); | |
ec9c02ad JL |
1299 | io_put_req_find_next(req, &nxt); |
1300 | ||
1301 | return nxt; | |
2b188cc1 JA |
1302 | } |
1303 | ||
def596e9 JA |
1304 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
1305 | { | |
1306 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); | |
1307 | ||
491381ce JA |
1308 | if (kiocb->ki_flags & IOCB_WRITE) |
1309 | kiocb_end_write(req); | |
def596e9 | 1310 | |
9e645e11 JA |
1311 | if ((req->flags & REQ_F_LINK) && res != req->result) |
1312 | req->flags |= REQ_F_FAIL_LINK; | |
1313 | req->result = res; | |
def596e9 JA |
1314 | if (res != -EAGAIN) |
1315 | req->flags |= REQ_F_IOPOLL_COMPLETED; | |
1316 | } | |
1317 | ||
1318 | /* | |
1319 | * After the iocb has been issued, it's safe to be found on the poll list. | |
1320 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
1321 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
1322 | * accessing the kiocb cookie. | |
1323 | */ | |
1324 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
1325 | { | |
1326 | struct io_ring_ctx *ctx = req->ctx; | |
1327 | ||
1328 | /* | |
1329 | * Track whether we have multiple files in our lists. This will impact | |
1330 | * how we do polling eventually, not spinning if we're on potentially | |
1331 | * different devices. | |
1332 | */ | |
1333 | if (list_empty(&ctx->poll_list)) { | |
1334 | ctx->poll_multi_file = false; | |
1335 | } else if (!ctx->poll_multi_file) { | |
1336 | struct io_kiocb *list_req; | |
1337 | ||
1338 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, | |
1339 | list); | |
1340 | if (list_req->rw.ki_filp != req->rw.ki_filp) | |
1341 | ctx->poll_multi_file = true; | |
1342 | } | |
1343 | ||
1344 | /* | |
1345 | * For fast devices, IO may have already completed. If it has, add | |
1346 | * it to the front so we find it first. | |
1347 | */ | |
1348 | if (req->flags & REQ_F_IOPOLL_COMPLETED) | |
1349 | list_add(&req->list, &ctx->poll_list); | |
1350 | else | |
1351 | list_add_tail(&req->list, &ctx->poll_list); | |
1352 | } | |
1353 | ||
3d6770fb | 1354 | static void io_file_put(struct io_submit_state *state) |
9a56a232 | 1355 | { |
3d6770fb | 1356 | if (state->file) { |
9a56a232 JA |
1357 | int diff = state->has_refs - state->used_refs; |
1358 | ||
1359 | if (diff) | |
1360 | fput_many(state->file, diff); | |
1361 | state->file = NULL; | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | /* | |
1366 | * Get as many references to a file as we have IOs left in this submission, | |
1367 | * assuming most submissions are for one file, or at least that each file | |
1368 | * has more than one submission. | |
1369 | */ | |
1370 | static struct file *io_file_get(struct io_submit_state *state, int fd) | |
1371 | { | |
1372 | if (!state) | |
1373 | return fget(fd); | |
1374 | ||
1375 | if (state->file) { | |
1376 | if (state->fd == fd) { | |
1377 | state->used_refs++; | |
1378 | state->ios_left--; | |
1379 | return state->file; | |
1380 | } | |
3d6770fb | 1381 | io_file_put(state); |
9a56a232 JA |
1382 | } |
1383 | state->file = fget_many(fd, state->ios_left); | |
1384 | if (!state->file) | |
1385 | return NULL; | |
1386 | ||
1387 | state->fd = fd; | |
1388 | state->has_refs = state->ios_left; | |
1389 | state->used_refs = 1; | |
1390 | state->ios_left--; | |
1391 | return state->file; | |
1392 | } | |
1393 | ||
2b188cc1 JA |
1394 | /* |
1395 | * If we tracked the file through the SCM inflight mechanism, we could support | |
1396 | * any file. For now, just ensure that anything potentially problematic is done | |
1397 | * inline. | |
1398 | */ | |
1399 | static bool io_file_supports_async(struct file *file) | |
1400 | { | |
1401 | umode_t mode = file_inode(file)->i_mode; | |
1402 | ||
1403 | if (S_ISBLK(mode) || S_ISCHR(mode)) | |
1404 | return true; | |
1405 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) | |
1406 | return true; | |
1407 | ||
1408 | return false; | |
1409 | } | |
1410 | ||
267bc904 | 1411 | static int io_prep_rw(struct io_kiocb *req, bool force_nonblock) |
2b188cc1 | 1412 | { |
267bc904 | 1413 | const struct io_uring_sqe *sqe = req->submit.sqe; |
def596e9 | 1414 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1415 | struct kiocb *kiocb = &req->rw; |
09bb8394 JA |
1416 | unsigned ioprio; |
1417 | int ret; | |
2b188cc1 | 1418 | |
09bb8394 JA |
1419 | if (!req->file) |
1420 | return -EBADF; | |
2b188cc1 | 1421 | |
491381ce JA |
1422 | if (S_ISREG(file_inode(req->file)->i_mode)) |
1423 | req->flags |= REQ_F_ISREG; | |
1424 | ||
1425 | /* | |
1426 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so | |
1427 | * we know to async punt it even if it was opened O_NONBLOCK | |
1428 | */ | |
1429 | if (force_nonblock && !io_file_supports_async(req->file)) { | |
1430 | req->flags |= REQ_F_MUST_PUNT; | |
1431 | return -EAGAIN; | |
1432 | } | |
6b06314c | 1433 | |
2b188cc1 JA |
1434 | kiocb->ki_pos = READ_ONCE(sqe->off); |
1435 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); | |
1436 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); | |
1437 | ||
1438 | ioprio = READ_ONCE(sqe->ioprio); | |
1439 | if (ioprio) { | |
1440 | ret = ioprio_check_cap(ioprio); | |
1441 | if (ret) | |
09bb8394 | 1442 | return ret; |
2b188cc1 JA |
1443 | |
1444 | kiocb->ki_ioprio = ioprio; | |
1445 | } else | |
1446 | kiocb->ki_ioprio = get_current_ioprio(); | |
1447 | ||
1448 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
1449 | if (unlikely(ret)) | |
09bb8394 | 1450 | return ret; |
8449eeda SB |
1451 | |
1452 | /* don't allow async punt if RWF_NOWAIT was requested */ | |
491381ce JA |
1453 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
1454 | (req->file->f_flags & O_NONBLOCK)) | |
8449eeda SB |
1455 | req->flags |= REQ_F_NOWAIT; |
1456 | ||
1457 | if (force_nonblock) | |
2b188cc1 | 1458 | kiocb->ki_flags |= IOCB_NOWAIT; |
8449eeda | 1459 | |
def596e9 | 1460 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
1461 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
1462 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 1463 | return -EOPNOTSUPP; |
2b188cc1 | 1464 | |
def596e9 JA |
1465 | kiocb->ki_flags |= IOCB_HIPRI; |
1466 | kiocb->ki_complete = io_complete_rw_iopoll; | |
6873e0bd | 1467 | req->result = 0; |
def596e9 | 1468 | } else { |
09bb8394 JA |
1469 | if (kiocb->ki_flags & IOCB_HIPRI) |
1470 | return -EINVAL; | |
def596e9 JA |
1471 | kiocb->ki_complete = io_complete_rw; |
1472 | } | |
2b188cc1 | 1473 | return 0; |
2b188cc1 JA |
1474 | } |
1475 | ||
1476 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
1477 | { | |
1478 | switch (ret) { | |
1479 | case -EIOCBQUEUED: | |
1480 | break; | |
1481 | case -ERESTARTSYS: | |
1482 | case -ERESTARTNOINTR: | |
1483 | case -ERESTARTNOHAND: | |
1484 | case -ERESTART_RESTARTBLOCK: | |
1485 | /* | |
1486 | * We can't just restart the syscall, since previously | |
1487 | * submitted sqes may already be in progress. Just fail this | |
1488 | * IO with EINTR. | |
1489 | */ | |
1490 | ret = -EINTR; | |
1491 | /* fall through */ | |
1492 | default: | |
1493 | kiocb->ki_complete(kiocb, ret, 0); | |
1494 | } | |
1495 | } | |
1496 | ||
ba816ad6 JA |
1497 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
1498 | bool in_async) | |
1499 | { | |
1500 | if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw) | |
1501 | *nxt = __io_complete_rw(kiocb, ret); | |
1502 | else | |
1503 | io_rw_done(kiocb, ret); | |
1504 | } | |
1505 | ||
edafccee JA |
1506 | static int io_import_fixed(struct io_ring_ctx *ctx, int rw, |
1507 | const struct io_uring_sqe *sqe, | |
1508 | struct iov_iter *iter) | |
1509 | { | |
1510 | size_t len = READ_ONCE(sqe->len); | |
1511 | struct io_mapped_ubuf *imu; | |
1512 | unsigned index, buf_index; | |
1513 | size_t offset; | |
1514 | u64 buf_addr; | |
1515 | ||
1516 | /* attempt to use fixed buffers without having provided iovecs */ | |
1517 | if (unlikely(!ctx->user_bufs)) | |
1518 | return -EFAULT; | |
1519 | ||
1520 | buf_index = READ_ONCE(sqe->buf_index); | |
1521 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
1522 | return -EFAULT; | |
1523 | ||
1524 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
1525 | imu = &ctx->user_bufs[index]; | |
1526 | buf_addr = READ_ONCE(sqe->addr); | |
1527 | ||
1528 | /* overflow */ | |
1529 | if (buf_addr + len < buf_addr) | |
1530 | return -EFAULT; | |
1531 | /* not inside the mapped region */ | |
1532 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
1533 | return -EFAULT; | |
1534 | ||
1535 | /* | |
1536 | * May not be a start of buffer, set size appropriately | |
1537 | * and advance us to the beginning. | |
1538 | */ | |
1539 | offset = buf_addr - imu->ubuf; | |
1540 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
1541 | |
1542 | if (offset) { | |
1543 | /* | |
1544 | * Don't use iov_iter_advance() here, as it's really slow for | |
1545 | * using the latter parts of a big fixed buffer - it iterates | |
1546 | * over each segment manually. We can cheat a bit here, because | |
1547 | * we know that: | |
1548 | * | |
1549 | * 1) it's a BVEC iter, we set it up | |
1550 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
1551 | * first and last bvec | |
1552 | * | |
1553 | * So just find our index, and adjust the iterator afterwards. | |
1554 | * If the offset is within the first bvec (or the whole first | |
1555 | * bvec, just use iov_iter_advance(). This makes it easier | |
1556 | * since we can just skip the first segment, which may not | |
1557 | * be PAGE_SIZE aligned. | |
1558 | */ | |
1559 | const struct bio_vec *bvec = imu->bvec; | |
1560 | ||
1561 | if (offset <= bvec->bv_len) { | |
1562 | iov_iter_advance(iter, offset); | |
1563 | } else { | |
1564 | unsigned long seg_skip; | |
1565 | ||
1566 | /* skip first vec */ | |
1567 | offset -= bvec->bv_len; | |
1568 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
1569 | ||
1570 | iter->bvec = bvec + seg_skip; | |
1571 | iter->nr_segs -= seg_skip; | |
99c79f66 | 1572 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 1573 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
1574 | } |
1575 | } | |
1576 | ||
5e559561 | 1577 | return len; |
edafccee JA |
1578 | } |
1579 | ||
87e5e6da JA |
1580 | static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw, |
1581 | const struct sqe_submit *s, struct iovec **iovec, | |
1582 | struct iov_iter *iter) | |
2b188cc1 JA |
1583 | { |
1584 | const struct io_uring_sqe *sqe = s->sqe; | |
1585 | void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
1586 | size_t sqe_len = READ_ONCE(sqe->len); | |
edafccee JA |
1587 | u8 opcode; |
1588 | ||
1589 | /* | |
1590 | * We're reading ->opcode for the second time, but the first read | |
1591 | * doesn't care whether it's _FIXED or not, so it doesn't matter | |
1592 | * whether ->opcode changes concurrently. The first read does care | |
1593 | * about whether it is a READ or a WRITE, so we don't trust this read | |
1594 | * for that purpose and instead let the caller pass in the read/write | |
1595 | * flag. | |
1596 | */ | |
1597 | opcode = READ_ONCE(sqe->opcode); | |
1598 | if (opcode == IORING_OP_READ_FIXED || | |
1599 | opcode == IORING_OP_WRITE_FIXED) { | |
87e5e6da | 1600 | ssize_t ret = io_import_fixed(ctx, rw, sqe, iter); |
edafccee JA |
1601 | *iovec = NULL; |
1602 | return ret; | |
1603 | } | |
2b188cc1 JA |
1604 | |
1605 | if (!s->has_user) | |
1606 | return -EFAULT; | |
1607 | ||
1608 | #ifdef CONFIG_COMPAT | |
1609 | if (ctx->compat) | |
1610 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, | |
1611 | iovec, iter); | |
1612 | #endif | |
1613 | ||
1614 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
1615 | } | |
1616 | ||
31b51510 | 1617 | /* |
32960613 JA |
1618 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
1619 | * by looping over ->read() or ->write() manually. | |
31b51510 | 1620 | */ |
32960613 JA |
1621 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
1622 | struct iov_iter *iter) | |
1623 | { | |
1624 | ssize_t ret = 0; | |
1625 | ||
1626 | /* | |
1627 | * Don't support polled IO through this interface, and we can't | |
1628 | * support non-blocking either. For the latter, this just causes | |
1629 | * the kiocb to be handled from an async context. | |
1630 | */ | |
1631 | if (kiocb->ki_flags & IOCB_HIPRI) | |
1632 | return -EOPNOTSUPP; | |
1633 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
1634 | return -EAGAIN; | |
1635 | ||
1636 | while (iov_iter_count(iter)) { | |
1637 | struct iovec iovec = iov_iter_iovec(iter); | |
1638 | ssize_t nr; | |
1639 | ||
1640 | if (rw == READ) { | |
1641 | nr = file->f_op->read(file, iovec.iov_base, | |
1642 | iovec.iov_len, &kiocb->ki_pos); | |
1643 | } else { | |
1644 | nr = file->f_op->write(file, iovec.iov_base, | |
1645 | iovec.iov_len, &kiocb->ki_pos); | |
1646 | } | |
1647 | ||
1648 | if (nr < 0) { | |
1649 | if (!ret) | |
1650 | ret = nr; | |
1651 | break; | |
1652 | } | |
1653 | ret += nr; | |
1654 | if (nr != iovec.iov_len) | |
1655 | break; | |
1656 | iov_iter_advance(iter, nr); | |
1657 | } | |
1658 | ||
1659 | return ret; | |
1660 | } | |
1661 | ||
267bc904 | 1662 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
8358e3a8 | 1663 | bool force_nonblock) |
2b188cc1 JA |
1664 | { |
1665 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
1666 | struct kiocb *kiocb = &req->rw; | |
1667 | struct iov_iter iter; | |
1668 | struct file *file; | |
31b51510 | 1669 | size_t iov_count; |
9d93a3f5 | 1670 | ssize_t read_size, ret; |
2b188cc1 | 1671 | |
267bc904 | 1672 | ret = io_prep_rw(req, force_nonblock); |
2b188cc1 JA |
1673 | if (ret) |
1674 | return ret; | |
1675 | file = kiocb->ki_filp; | |
1676 | ||
2b188cc1 | 1677 | if (unlikely(!(file->f_mode & FMODE_READ))) |
09bb8394 | 1678 | return -EBADF; |
2b188cc1 | 1679 | |
267bc904 | 1680 | ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter); |
87e5e6da | 1681 | if (ret < 0) |
09bb8394 | 1682 | return ret; |
2b188cc1 | 1683 | |
9d93a3f5 | 1684 | read_size = ret; |
9e645e11 JA |
1685 | if (req->flags & REQ_F_LINK) |
1686 | req->result = read_size; | |
1687 | ||
31b51510 JA |
1688 | iov_count = iov_iter_count(&iter); |
1689 | ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count); | |
2b188cc1 JA |
1690 | if (!ret) { |
1691 | ssize_t ret2; | |
1692 | ||
32960613 JA |
1693 | if (file->f_op->read_iter) |
1694 | ret2 = call_read_iter(file, kiocb, &iter); | |
1695 | else | |
1696 | ret2 = loop_rw_iter(READ, file, kiocb, &iter); | |
1697 | ||
9d93a3f5 JA |
1698 | /* |
1699 | * In case of a short read, punt to async. This can happen | |
1700 | * if we have data partially cached. Alternatively we can | |
1701 | * return the short read, in which case the application will | |
1702 | * need to issue another SQE and wait for it. That SQE will | |
1703 | * need async punt anyway, so it's more efficient to do it | |
1704 | * here. | |
1705 | */ | |
491381ce JA |
1706 | if (force_nonblock && !(req->flags & REQ_F_NOWAIT) && |
1707 | (req->flags & REQ_F_ISREG) && | |
1708 | ret2 > 0 && ret2 < read_size) | |
9d93a3f5 JA |
1709 | ret2 = -EAGAIN; |
1710 | /* Catch -EAGAIN return for forced non-blocking submission */ | |
561fb04a | 1711 | if (!force_nonblock || ret2 != -EAGAIN) |
267bc904 | 1712 | kiocb_done(kiocb, ret2, nxt, req->submit.in_async); |
561fb04a | 1713 | else |
2b188cc1 JA |
1714 | ret = -EAGAIN; |
1715 | } | |
1716 | kfree(iovec); | |
2b188cc1 JA |
1717 | return ret; |
1718 | } | |
1719 | ||
267bc904 | 1720 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
8358e3a8 | 1721 | bool force_nonblock) |
2b188cc1 JA |
1722 | { |
1723 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
1724 | struct kiocb *kiocb = &req->rw; | |
1725 | struct iov_iter iter; | |
1726 | struct file *file; | |
31b51510 | 1727 | size_t iov_count; |
87e5e6da | 1728 | ssize_t ret; |
2b188cc1 | 1729 | |
267bc904 | 1730 | ret = io_prep_rw(req, force_nonblock); |
2b188cc1 JA |
1731 | if (ret) |
1732 | return ret; | |
2b188cc1 | 1733 | |
2b188cc1 JA |
1734 | file = kiocb->ki_filp; |
1735 | if (unlikely(!(file->f_mode & FMODE_WRITE))) | |
09bb8394 | 1736 | return -EBADF; |
2b188cc1 | 1737 | |
267bc904 | 1738 | ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter); |
87e5e6da | 1739 | if (ret < 0) |
09bb8394 | 1740 | return ret; |
2b188cc1 | 1741 | |
9e645e11 JA |
1742 | if (req->flags & REQ_F_LINK) |
1743 | req->result = ret; | |
1744 | ||
31b51510 JA |
1745 | iov_count = iov_iter_count(&iter); |
1746 | ||
1747 | ret = -EAGAIN; | |
561fb04a | 1748 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) |
31b51510 | 1749 | goto out_free; |
31b51510 JA |
1750 | |
1751 | ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count); | |
2b188cc1 | 1752 | if (!ret) { |
9bf7933f RP |
1753 | ssize_t ret2; |
1754 | ||
2b188cc1 JA |
1755 | /* |
1756 | * Open-code file_start_write here to grab freeze protection, | |
1757 | * which will be released by another thread in | |
1758 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
1759 | * released so that it doesn't complain about the held lock when | |
1760 | * we return to userspace. | |
1761 | */ | |
491381ce | 1762 | if (req->flags & REQ_F_ISREG) { |
2b188cc1 JA |
1763 | __sb_start_write(file_inode(file)->i_sb, |
1764 | SB_FREEZE_WRITE, true); | |
1765 | __sb_writers_release(file_inode(file)->i_sb, | |
1766 | SB_FREEZE_WRITE); | |
1767 | } | |
1768 | kiocb->ki_flags |= IOCB_WRITE; | |
9bf7933f | 1769 | |
32960613 JA |
1770 | if (file->f_op->write_iter) |
1771 | ret2 = call_write_iter(file, kiocb, &iter); | |
1772 | else | |
1773 | ret2 = loop_rw_iter(WRITE, file, kiocb, &iter); | |
561fb04a | 1774 | if (!force_nonblock || ret2 != -EAGAIN) |
267bc904 | 1775 | kiocb_done(kiocb, ret2, nxt, req->submit.in_async); |
561fb04a | 1776 | else |
9bf7933f | 1777 | ret = -EAGAIN; |
2b188cc1 | 1778 | } |
31b51510 | 1779 | out_free: |
2b188cc1 | 1780 | kfree(iovec); |
2b188cc1 JA |
1781 | return ret; |
1782 | } | |
1783 | ||
1784 | /* | |
1785 | * IORING_OP_NOP just posts a completion event, nothing else. | |
1786 | */ | |
78e19bbe | 1787 | static int io_nop(struct io_kiocb *req) |
2b188cc1 JA |
1788 | { |
1789 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 1790 | |
def596e9 JA |
1791 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
1792 | return -EINVAL; | |
1793 | ||
78e19bbe | 1794 | io_cqring_add_event(req, 0); |
e65ef56d | 1795 | io_put_req(req); |
2b188cc1 JA |
1796 | return 0; |
1797 | } | |
1798 | ||
c992fe29 CH |
1799 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
1800 | { | |
6b06314c | 1801 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 1802 | |
09bb8394 JA |
1803 | if (!req->file) |
1804 | return -EBADF; | |
c992fe29 | 1805 | |
6b06314c | 1806 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 1807 | return -EINVAL; |
edafccee | 1808 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
1809 | return -EINVAL; |
1810 | ||
c992fe29 CH |
1811 | return 0; |
1812 | } | |
1813 | ||
1814 | static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
ba816ad6 | 1815 | struct io_kiocb **nxt, bool force_nonblock) |
c992fe29 CH |
1816 | { |
1817 | loff_t sqe_off = READ_ONCE(sqe->off); | |
1818 | loff_t sqe_len = READ_ONCE(sqe->len); | |
1819 | loff_t end = sqe_off + sqe_len; | |
1820 | unsigned fsync_flags; | |
1821 | int ret; | |
1822 | ||
1823 | fsync_flags = READ_ONCE(sqe->fsync_flags); | |
1824 | if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC)) | |
1825 | return -EINVAL; | |
1826 | ||
1827 | ret = io_prep_fsync(req, sqe); | |
1828 | if (ret) | |
1829 | return ret; | |
1830 | ||
1831 | /* fsync always requires a blocking context */ | |
1832 | if (force_nonblock) | |
1833 | return -EAGAIN; | |
1834 | ||
1835 | ret = vfs_fsync_range(req->rw.ki_filp, sqe_off, | |
1836 | end > 0 ? end : LLONG_MAX, | |
1837 | fsync_flags & IORING_FSYNC_DATASYNC); | |
1838 | ||
9e645e11 JA |
1839 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
1840 | req->flags |= REQ_F_FAIL_LINK; | |
78e19bbe | 1841 | io_cqring_add_event(req, ret); |
ec9c02ad | 1842 | io_put_req_find_next(req, nxt); |
c992fe29 CH |
1843 | return 0; |
1844 | } | |
1845 | ||
5d17b4a4 JA |
1846 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
1847 | { | |
1848 | struct io_ring_ctx *ctx = req->ctx; | |
1849 | int ret = 0; | |
1850 | ||
1851 | if (!req->file) | |
1852 | return -EBADF; | |
5d17b4a4 JA |
1853 | |
1854 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
1855 | return -EINVAL; | |
1856 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
1857 | return -EINVAL; | |
1858 | ||
5d17b4a4 JA |
1859 | return ret; |
1860 | } | |
1861 | ||
1862 | static int io_sync_file_range(struct io_kiocb *req, | |
1863 | const struct io_uring_sqe *sqe, | |
ba816ad6 | 1864 | struct io_kiocb **nxt, |
5d17b4a4 JA |
1865 | bool force_nonblock) |
1866 | { | |
1867 | loff_t sqe_off; | |
1868 | loff_t sqe_len; | |
1869 | unsigned flags; | |
1870 | int ret; | |
1871 | ||
1872 | ret = io_prep_sfr(req, sqe); | |
1873 | if (ret) | |
1874 | return ret; | |
1875 | ||
1876 | /* sync_file_range always requires a blocking context */ | |
1877 | if (force_nonblock) | |
1878 | return -EAGAIN; | |
1879 | ||
1880 | sqe_off = READ_ONCE(sqe->off); | |
1881 | sqe_len = READ_ONCE(sqe->len); | |
1882 | flags = READ_ONCE(sqe->sync_range_flags); | |
1883 | ||
1884 | ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags); | |
1885 | ||
9e645e11 JA |
1886 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
1887 | req->flags |= REQ_F_FAIL_LINK; | |
78e19bbe | 1888 | io_cqring_add_event(req, ret); |
ec9c02ad | 1889 | io_put_req_find_next(req, nxt); |
5d17b4a4 JA |
1890 | return 0; |
1891 | } | |
1892 | ||
0fa03c62 | 1893 | #if defined(CONFIG_NET) |
aa1fa28f | 1894 | static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
ba816ad6 | 1895 | struct io_kiocb **nxt, bool force_nonblock, |
aa1fa28f JA |
1896 | long (*fn)(struct socket *, struct user_msghdr __user *, |
1897 | unsigned int)) | |
1898 | { | |
0fa03c62 JA |
1899 | struct socket *sock; |
1900 | int ret; | |
1901 | ||
1902 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1903 | return -EINVAL; | |
1904 | ||
1905 | sock = sock_from_file(req->file, &ret); | |
1906 | if (sock) { | |
1907 | struct user_msghdr __user *msg; | |
1908 | unsigned flags; | |
1909 | ||
1910 | flags = READ_ONCE(sqe->msg_flags); | |
1911 | if (flags & MSG_DONTWAIT) | |
1912 | req->flags |= REQ_F_NOWAIT; | |
1913 | else if (force_nonblock) | |
1914 | flags |= MSG_DONTWAIT; | |
1915 | ||
1916 | msg = (struct user_msghdr __user *) (unsigned long) | |
1917 | READ_ONCE(sqe->addr); | |
1918 | ||
aa1fa28f | 1919 | ret = fn(sock, msg, flags); |
0fa03c62 JA |
1920 | if (force_nonblock && ret == -EAGAIN) |
1921 | return ret; | |
1922 | } | |
1923 | ||
78e19bbe | 1924 | io_cqring_add_event(req, ret); |
f1f40853 JA |
1925 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
1926 | req->flags |= REQ_F_FAIL_LINK; | |
ec9c02ad | 1927 | io_put_req_find_next(req, nxt); |
5d17b4a4 JA |
1928 | return 0; |
1929 | } | |
aa1fa28f JA |
1930 | #endif |
1931 | ||
1932 | static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
ba816ad6 | 1933 | struct io_kiocb **nxt, bool force_nonblock) |
aa1fa28f JA |
1934 | { |
1935 | #if defined(CONFIG_NET) | |
ba816ad6 JA |
1936 | return io_send_recvmsg(req, sqe, nxt, force_nonblock, |
1937 | __sys_sendmsg_sock); | |
aa1fa28f JA |
1938 | #else |
1939 | return -EOPNOTSUPP; | |
1940 | #endif | |
1941 | } | |
1942 | ||
1943 | static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, | |
ba816ad6 | 1944 | struct io_kiocb **nxt, bool force_nonblock) |
aa1fa28f JA |
1945 | { |
1946 | #if defined(CONFIG_NET) | |
ba816ad6 JA |
1947 | return io_send_recvmsg(req, sqe, nxt, force_nonblock, |
1948 | __sys_recvmsg_sock); | |
0fa03c62 JA |
1949 | #else |
1950 | return -EOPNOTSUPP; | |
1951 | #endif | |
1952 | } | |
5d17b4a4 | 1953 | |
17f2fe35 JA |
1954 | static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
1955 | struct io_kiocb **nxt, bool force_nonblock) | |
1956 | { | |
1957 | #if defined(CONFIG_NET) | |
1958 | struct sockaddr __user *addr; | |
1959 | int __user *addr_len; | |
1960 | unsigned file_flags; | |
1961 | int flags, ret; | |
1962 | ||
1963 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) | |
1964 | return -EINVAL; | |
1965 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
1966 | return -EINVAL; | |
1967 | ||
1968 | addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr); | |
1969 | addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2); | |
1970 | flags = READ_ONCE(sqe->accept_flags); | |
1971 | file_flags = force_nonblock ? O_NONBLOCK : 0; | |
1972 | ||
1973 | ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags); | |
1974 | if (ret == -EAGAIN && force_nonblock) { | |
1975 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; | |
1976 | return -EAGAIN; | |
1977 | } | |
8e3cca12 JA |
1978 | if (ret == -ERESTARTSYS) |
1979 | ret = -EINTR; | |
17f2fe35 JA |
1980 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
1981 | req->flags |= REQ_F_FAIL_LINK; | |
78e19bbe | 1982 | io_cqring_add_event(req, ret); |
ec9c02ad | 1983 | io_put_req_find_next(req, nxt); |
17f2fe35 | 1984 | return 0; |
0fa03c62 JA |
1985 | #else |
1986 | return -EOPNOTSUPP; | |
1987 | #endif | |
1988 | } | |
5d17b4a4 | 1989 | |
eac406c6 JA |
1990 | static inline void io_poll_remove_req(struct io_kiocb *req) |
1991 | { | |
1992 | if (!RB_EMPTY_NODE(&req->rb_node)) { | |
1993 | rb_erase(&req->rb_node, &req->ctx->cancel_tree); | |
1994 | RB_CLEAR_NODE(&req->rb_node); | |
1995 | } | |
1996 | } | |
1997 | ||
221c5eb2 JA |
1998 | static void io_poll_remove_one(struct io_kiocb *req) |
1999 | { | |
2000 | struct io_poll_iocb *poll = &req->poll; | |
2001 | ||
2002 | spin_lock(&poll->head->lock); | |
2003 | WRITE_ONCE(poll->canceled, true); | |
2004 | if (!list_empty(&poll->wait.entry)) { | |
2005 | list_del_init(&poll->wait.entry); | |
a197f664 | 2006 | io_queue_async_work(req); |
221c5eb2 JA |
2007 | } |
2008 | spin_unlock(&poll->head->lock); | |
eac406c6 | 2009 | io_poll_remove_req(req); |
221c5eb2 JA |
2010 | } |
2011 | ||
2012 | static void io_poll_remove_all(struct io_ring_ctx *ctx) | |
2013 | { | |
eac406c6 | 2014 | struct rb_node *node; |
221c5eb2 JA |
2015 | struct io_kiocb *req; |
2016 | ||
2017 | spin_lock_irq(&ctx->completion_lock); | |
eac406c6 JA |
2018 | while ((node = rb_first(&ctx->cancel_tree)) != NULL) { |
2019 | req = rb_entry(node, struct io_kiocb, rb_node); | |
221c5eb2 JA |
2020 | io_poll_remove_one(req); |
2021 | } | |
2022 | spin_unlock_irq(&ctx->completion_lock); | |
2023 | } | |
2024 | ||
47f46768 JA |
2025 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
2026 | { | |
eac406c6 | 2027 | struct rb_node *p, *parent = NULL; |
47f46768 JA |
2028 | struct io_kiocb *req; |
2029 | ||
eac406c6 JA |
2030 | p = ctx->cancel_tree.rb_node; |
2031 | while (p) { | |
2032 | parent = p; | |
2033 | req = rb_entry(parent, struct io_kiocb, rb_node); | |
2034 | if (sqe_addr < req->user_data) { | |
2035 | p = p->rb_left; | |
2036 | } else if (sqe_addr > req->user_data) { | |
2037 | p = p->rb_right; | |
2038 | } else { | |
2039 | io_poll_remove_one(req); | |
2040 | return 0; | |
2041 | } | |
47f46768 JA |
2042 | } |
2043 | ||
2044 | return -ENOENT; | |
2045 | } | |
2046 | ||
221c5eb2 JA |
2047 | /* |
2048 | * Find a running poll command that matches one specified in sqe->addr, | |
2049 | * and remove it if found. | |
2050 | */ | |
2051 | static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
2052 | { | |
2053 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 2054 | int ret; |
221c5eb2 JA |
2055 | |
2056 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
2057 | return -EINVAL; | |
2058 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
2059 | sqe->poll_events) | |
2060 | return -EINVAL; | |
2061 | ||
2062 | spin_lock_irq(&ctx->completion_lock); | |
47f46768 | 2063 | ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr)); |
221c5eb2 JA |
2064 | spin_unlock_irq(&ctx->completion_lock); |
2065 | ||
78e19bbe | 2066 | io_cqring_add_event(req, ret); |
f1f40853 JA |
2067 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
2068 | req->flags |= REQ_F_FAIL_LINK; | |
e65ef56d | 2069 | io_put_req(req); |
221c5eb2 JA |
2070 | return 0; |
2071 | } | |
2072 | ||
b0dd8a41 | 2073 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
221c5eb2 | 2074 | { |
a197f664 JL |
2075 | struct io_ring_ctx *ctx = req->ctx; |
2076 | ||
8c838788 | 2077 | req->poll.done = true; |
b0dd8a41 JA |
2078 | if (error) |
2079 | io_cqring_fill_event(req, error); | |
2080 | else | |
2081 | io_cqring_fill_event(req, mangle_poll(mask)); | |
8c838788 | 2082 | io_commit_cqring(ctx); |
221c5eb2 JA |
2083 | } |
2084 | ||
561fb04a | 2085 | static void io_poll_complete_work(struct io_wq_work **workptr) |
221c5eb2 | 2086 | { |
561fb04a | 2087 | struct io_wq_work *work = *workptr; |
221c5eb2 JA |
2088 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
2089 | struct io_poll_iocb *poll = &req->poll; | |
2090 | struct poll_table_struct pt = { ._key = poll->events }; | |
2091 | struct io_ring_ctx *ctx = req->ctx; | |
89723d0b | 2092 | struct io_kiocb *nxt = NULL; |
221c5eb2 | 2093 | __poll_t mask = 0; |
b0dd8a41 | 2094 | int ret = 0; |
221c5eb2 | 2095 | |
b0dd8a41 | 2096 | if (work->flags & IO_WQ_WORK_CANCEL) { |
561fb04a | 2097 | WRITE_ONCE(poll->canceled, true); |
b0dd8a41 JA |
2098 | ret = -ECANCELED; |
2099 | } else if (READ_ONCE(poll->canceled)) { | |
2100 | ret = -ECANCELED; | |
2101 | } | |
561fb04a | 2102 | |
b0dd8a41 | 2103 | if (ret != -ECANCELED) |
221c5eb2 JA |
2104 | mask = vfs_poll(poll->file, &pt) & poll->events; |
2105 | ||
2106 | /* | |
2107 | * Note that ->ki_cancel callers also delete iocb from active_reqs after | |
2108 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to | |
2109 | * synchronize with them. In the cancellation case the list_del_init | |
2110 | * itself is not actually needed, but harmless so we keep it in to | |
2111 | * avoid further branches in the fast path. | |
2112 | */ | |
2113 | spin_lock_irq(&ctx->completion_lock); | |
b0dd8a41 | 2114 | if (!mask && ret != -ECANCELED) { |
221c5eb2 JA |
2115 | add_wait_queue(poll->head, &poll->wait); |
2116 | spin_unlock_irq(&ctx->completion_lock); | |
2117 | return; | |
2118 | } | |
eac406c6 | 2119 | io_poll_remove_req(req); |
b0dd8a41 | 2120 | io_poll_complete(req, mask, ret); |
221c5eb2 JA |
2121 | spin_unlock_irq(&ctx->completion_lock); |
2122 | ||
8c838788 | 2123 | io_cqring_ev_posted(ctx); |
89723d0b | 2124 | |
fba38c27 JA |
2125 | if (ret < 0 && req->flags & REQ_F_LINK) |
2126 | req->flags |= REQ_F_FAIL_LINK; | |
ec9c02ad | 2127 | io_put_req_find_next(req, &nxt); |
89723d0b JA |
2128 | if (nxt) |
2129 | *workptr = &nxt->work; | |
221c5eb2 JA |
2130 | } |
2131 | ||
2132 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
2133 | void *key) | |
2134 | { | |
2135 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, | |
2136 | wait); | |
2137 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); | |
2138 | struct io_ring_ctx *ctx = req->ctx; | |
2139 | __poll_t mask = key_to_poll(key); | |
8c838788 | 2140 | unsigned long flags; |
221c5eb2 JA |
2141 | |
2142 | /* for instances that support it check for an event match first: */ | |
8c838788 JA |
2143 | if (mask && !(mask & poll->events)) |
2144 | return 0; | |
221c5eb2 | 2145 | |
8c838788 | 2146 | list_del_init(&poll->wait.entry); |
221c5eb2 | 2147 | |
7c9e7f0f JA |
2148 | /* |
2149 | * Run completion inline if we can. We're using trylock here because | |
2150 | * we are violating the completion_lock -> poll wq lock ordering. | |
2151 | * If we have a link timeout we're going to need the completion_lock | |
2152 | * for finalizing the request, mark us as having grabbed that already. | |
2153 | */ | |
8c838788 | 2154 | if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
eac406c6 | 2155 | io_poll_remove_req(req); |
b0dd8a41 | 2156 | io_poll_complete(req, mask, 0); |
7c9e7f0f JA |
2157 | req->flags |= REQ_F_COMP_LOCKED; |
2158 | io_put_req(req); | |
8c838788 | 2159 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
221c5eb2 | 2160 | |
8c838788 | 2161 | io_cqring_ev_posted(ctx); |
8c838788 | 2162 | } else { |
a197f664 | 2163 | io_queue_async_work(req); |
221c5eb2 JA |
2164 | } |
2165 | ||
221c5eb2 JA |
2166 | return 1; |
2167 | } | |
2168 | ||
2169 | struct io_poll_table { | |
2170 | struct poll_table_struct pt; | |
2171 | struct io_kiocb *req; | |
2172 | int error; | |
2173 | }; | |
2174 | ||
2175 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, | |
2176 | struct poll_table_struct *p) | |
2177 | { | |
2178 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
2179 | ||
2180 | if (unlikely(pt->req->poll.head)) { | |
2181 | pt->error = -EINVAL; | |
2182 | return; | |
2183 | } | |
2184 | ||
2185 | pt->error = 0; | |
2186 | pt->req->poll.head = head; | |
2187 | add_wait_queue(head, &pt->req->poll.wait); | |
2188 | } | |
2189 | ||
eac406c6 JA |
2190 | static void io_poll_req_insert(struct io_kiocb *req) |
2191 | { | |
2192 | struct io_ring_ctx *ctx = req->ctx; | |
2193 | struct rb_node **p = &ctx->cancel_tree.rb_node; | |
2194 | struct rb_node *parent = NULL; | |
2195 | struct io_kiocb *tmp; | |
2196 | ||
2197 | while (*p) { | |
2198 | parent = *p; | |
2199 | tmp = rb_entry(parent, struct io_kiocb, rb_node); | |
2200 | if (req->user_data < tmp->user_data) | |
2201 | p = &(*p)->rb_left; | |
2202 | else | |
2203 | p = &(*p)->rb_right; | |
2204 | } | |
2205 | rb_link_node(&req->rb_node, parent, p); | |
2206 | rb_insert_color(&req->rb_node, &ctx->cancel_tree); | |
2207 | } | |
2208 | ||
89723d0b JA |
2209 | static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2210 | struct io_kiocb **nxt) | |
221c5eb2 JA |
2211 | { |
2212 | struct io_poll_iocb *poll = &req->poll; | |
2213 | struct io_ring_ctx *ctx = req->ctx; | |
2214 | struct io_poll_table ipt; | |
8c838788 | 2215 | bool cancel = false; |
221c5eb2 JA |
2216 | __poll_t mask; |
2217 | u16 events; | |
221c5eb2 JA |
2218 | |
2219 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
2220 | return -EINVAL; | |
2221 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
2222 | return -EINVAL; | |
09bb8394 JA |
2223 | if (!poll->file) |
2224 | return -EBADF; | |
221c5eb2 | 2225 | |
6cc47d1d | 2226 | req->submit.sqe = NULL; |
561fb04a | 2227 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
221c5eb2 JA |
2228 | events = READ_ONCE(sqe->poll_events); |
2229 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; | |
eac406c6 | 2230 | RB_CLEAR_NODE(&req->rb_node); |
221c5eb2 | 2231 | |
221c5eb2 | 2232 | poll->head = NULL; |
8c838788 | 2233 | poll->done = false; |
221c5eb2 JA |
2234 | poll->canceled = false; |
2235 | ||
2236 | ipt.pt._qproc = io_poll_queue_proc; | |
2237 | ipt.pt._key = poll->events; | |
2238 | ipt.req = req; | |
2239 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ | |
2240 | ||
2241 | /* initialized the list so that we can do list_empty checks */ | |
2242 | INIT_LIST_HEAD(&poll->wait.entry); | |
2243 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); | |
2244 | ||
36703247 JA |
2245 | INIT_LIST_HEAD(&req->list); |
2246 | ||
221c5eb2 | 2247 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
221c5eb2 JA |
2248 | |
2249 | spin_lock_irq(&ctx->completion_lock); | |
8c838788 JA |
2250 | if (likely(poll->head)) { |
2251 | spin_lock(&poll->head->lock); | |
2252 | if (unlikely(list_empty(&poll->wait.entry))) { | |
2253 | if (ipt.error) | |
2254 | cancel = true; | |
2255 | ipt.error = 0; | |
2256 | mask = 0; | |
2257 | } | |
2258 | if (mask || ipt.error) | |
2259 | list_del_init(&poll->wait.entry); | |
2260 | else if (cancel) | |
2261 | WRITE_ONCE(poll->canceled, true); | |
2262 | else if (!poll->done) /* actually waiting for an event */ | |
eac406c6 | 2263 | io_poll_req_insert(req); |
8c838788 JA |
2264 | spin_unlock(&poll->head->lock); |
2265 | } | |
2266 | if (mask) { /* no async, we'd stolen it */ | |
221c5eb2 | 2267 | ipt.error = 0; |
b0dd8a41 | 2268 | io_poll_complete(req, mask, 0); |
221c5eb2 | 2269 | } |
221c5eb2 JA |
2270 | spin_unlock_irq(&ctx->completion_lock); |
2271 | ||
8c838788 JA |
2272 | if (mask) { |
2273 | io_cqring_ev_posted(ctx); | |
ec9c02ad | 2274 | io_put_req_find_next(req, nxt); |
221c5eb2 | 2275 | } |
8c838788 | 2276 | return ipt.error; |
221c5eb2 JA |
2277 | } |
2278 | ||
5262f567 JA |
2279 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
2280 | { | |
ad8a48ac JA |
2281 | struct io_timeout_data *data = container_of(timer, |
2282 | struct io_timeout_data, timer); | |
2283 | struct io_kiocb *req = data->req; | |
2284 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
2285 | unsigned long flags; |
2286 | ||
5262f567 JA |
2287 | atomic_inc(&ctx->cq_timeouts); |
2288 | ||
2289 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
ef03681a | 2290 | /* |
11365043 JA |
2291 | * We could be racing with timeout deletion. If the list is empty, |
2292 | * then timeout lookup already found it and will be handling it. | |
ef03681a | 2293 | */ |
842f9612 | 2294 | if (!list_empty(&req->list)) { |
11365043 | 2295 | struct io_kiocb *prev; |
5262f567 | 2296 | |
11365043 JA |
2297 | /* |
2298 | * Adjust the reqs sequence before the current one because it | |
2299 | * will consume a slot in the cq_ring and the the cq_tail | |
2300 | * pointer will be increased, otherwise other timeout reqs may | |
2301 | * return in advance without waiting for enough wait_nr. | |
2302 | */ | |
2303 | prev = req; | |
2304 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) | |
2305 | prev->sequence++; | |
11365043 | 2306 | list_del_init(&req->list); |
11365043 | 2307 | } |
5262f567 | 2308 | |
78e19bbe | 2309 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
2310 | io_commit_cqring(ctx); |
2311 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
2312 | ||
2313 | io_cqring_ev_posted(ctx); | |
f1f40853 JA |
2314 | if (req->flags & REQ_F_LINK) |
2315 | req->flags |= REQ_F_FAIL_LINK; | |
5262f567 JA |
2316 | io_put_req(req); |
2317 | return HRTIMER_NORESTART; | |
2318 | } | |
2319 | ||
47f46768 JA |
2320 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
2321 | { | |
2322 | struct io_kiocb *req; | |
2323 | int ret = -ENOENT; | |
2324 | ||
2325 | list_for_each_entry(req, &ctx->timeout_list, list) { | |
2326 | if (user_data == req->user_data) { | |
2327 | list_del_init(&req->list); | |
2328 | ret = 0; | |
2329 | break; | |
2330 | } | |
2331 | } | |
2332 | ||
2333 | if (ret == -ENOENT) | |
2334 | return ret; | |
2335 | ||
ad8a48ac | 2336 | ret = hrtimer_try_to_cancel(&req->timeout.data->timer); |
47f46768 JA |
2337 | if (ret == -1) |
2338 | return -EALREADY; | |
2339 | ||
fba38c27 JA |
2340 | if (req->flags & REQ_F_LINK) |
2341 | req->flags |= REQ_F_FAIL_LINK; | |
47f46768 JA |
2342 | io_cqring_fill_event(req, -ECANCELED); |
2343 | io_put_req(req); | |
2344 | return 0; | |
2345 | } | |
2346 | ||
11365043 JA |
2347 | /* |
2348 | * Remove or update an existing timeout command | |
2349 | */ | |
2350 | static int io_timeout_remove(struct io_kiocb *req, | |
2351 | const struct io_uring_sqe *sqe) | |
2352 | { | |
2353 | struct io_ring_ctx *ctx = req->ctx; | |
11365043 | 2354 | unsigned flags; |
47f46768 | 2355 | int ret; |
11365043 JA |
2356 | |
2357 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
2358 | return -EINVAL; | |
2359 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) | |
2360 | return -EINVAL; | |
2361 | flags = READ_ONCE(sqe->timeout_flags); | |
2362 | if (flags) | |
2363 | return -EINVAL; | |
2364 | ||
11365043 | 2365 | spin_lock_irq(&ctx->completion_lock); |
47f46768 | 2366 | ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr)); |
11365043 | 2367 | |
47f46768 | 2368 | io_cqring_fill_event(req, ret); |
11365043 JA |
2369 | io_commit_cqring(ctx); |
2370 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 2371 | io_cqring_ev_posted(ctx); |
47f46768 JA |
2372 | if (ret < 0 && req->flags & REQ_F_LINK) |
2373 | req->flags |= REQ_F_FAIL_LINK; | |
ec9c02ad | 2374 | io_put_req(req); |
11365043 | 2375 | return 0; |
5262f567 JA |
2376 | } |
2377 | ||
ad8a48ac | 2378 | static int io_timeout_setup(struct io_kiocb *req) |
5262f567 | 2379 | { |
ad8a48ac JA |
2380 | const struct io_uring_sqe *sqe = req->submit.sqe; |
2381 | struct io_timeout_data *data; | |
a41525ab | 2382 | unsigned flags; |
5262f567 | 2383 | |
ad8a48ac | 2384 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 2385 | return -EINVAL; |
ad8a48ac | 2386 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab JA |
2387 | return -EINVAL; |
2388 | flags = READ_ONCE(sqe->timeout_flags); | |
2389 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 2390 | return -EINVAL; |
bdf20073 | 2391 | |
ad8a48ac JA |
2392 | data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL); |
2393 | if (!data) | |
2394 | return -ENOMEM; | |
2395 | data->req = req; | |
2396 | req->timeout.data = data; | |
2397 | req->flags |= REQ_F_TIMEOUT; | |
2398 | ||
2399 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
2400 | return -EFAULT; |
2401 | ||
11365043 | 2402 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 2403 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 2404 | else |
ad8a48ac | 2405 | data->mode = HRTIMER_MODE_REL; |
11365043 | 2406 | |
ad8a48ac JA |
2407 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
2408 | return 0; | |
2409 | } | |
2410 | ||
2411 | static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
2412 | { | |
2413 | unsigned count; | |
2414 | struct io_ring_ctx *ctx = req->ctx; | |
2415 | struct io_timeout_data *data; | |
2416 | struct list_head *entry; | |
2417 | unsigned span = 0; | |
2418 | int ret; | |
2419 | ||
2420 | ret = io_timeout_setup(req); | |
2421 | /* common setup allows flags (like links) set, we don't */ | |
2422 | if (!ret && sqe->flags) | |
2423 | ret = -EINVAL; | |
2424 | if (ret) | |
2425 | return ret; | |
93bd25bb | 2426 | |
5262f567 JA |
2427 | /* |
2428 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
2429 | * timeout event to be satisfied. If it isn't set, then this is |
2430 | * a pure timeout request, sequence isn't used. | |
5262f567 JA |
2431 | */ |
2432 | count = READ_ONCE(sqe->off); | |
93bd25bb JA |
2433 | if (!count) { |
2434 | req->flags |= REQ_F_TIMEOUT_NOSEQ; | |
2435 | spin_lock_irq(&ctx->completion_lock); | |
2436 | entry = ctx->timeout_list.prev; | |
2437 | goto add; | |
2438 | } | |
5262f567 JA |
2439 | |
2440 | req->sequence = ctx->cached_sq_head + count - 1; | |
5da0fb1a | 2441 | /* reuse it to store the count */ |
2442 | req->submit.sequence = count; | |
5262f567 JA |
2443 | |
2444 | /* | |
2445 | * Insertion sort, ensuring the first entry in the list is always | |
2446 | * the one we need first. | |
2447 | */ | |
5262f567 JA |
2448 | spin_lock_irq(&ctx->completion_lock); |
2449 | list_for_each_prev(entry, &ctx->timeout_list) { | |
2450 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); | |
5da0fb1a | 2451 | unsigned nxt_sq_head; |
2452 | long long tmp, tmp_nxt; | |
5262f567 | 2453 | |
93bd25bb JA |
2454 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
2455 | continue; | |
2456 | ||
5da0fb1a | 2457 | /* |
2458 | * Since cached_sq_head + count - 1 can overflow, use type long | |
2459 | * long to store it. | |
2460 | */ | |
2461 | tmp = (long long)ctx->cached_sq_head + count - 1; | |
2462 | nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1; | |
2463 | tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1; | |
2464 | ||
2465 | /* | |
2466 | * cached_sq_head may overflow, and it will never overflow twice | |
2467 | * once there is some timeout req still be valid. | |
2468 | */ | |
2469 | if (ctx->cached_sq_head < nxt_sq_head) | |
8b07a65a | 2470 | tmp += UINT_MAX; |
5da0fb1a | 2471 | |
a1f58ba4 | 2472 | if (tmp > tmp_nxt) |
5262f567 | 2473 | break; |
a1f58ba4 | 2474 | |
2475 | /* | |
2476 | * Sequence of reqs after the insert one and itself should | |
2477 | * be adjusted because each timeout req consumes a slot. | |
2478 | */ | |
2479 | span++; | |
2480 | nxt->sequence++; | |
5262f567 | 2481 | } |
a1f58ba4 | 2482 | req->sequence -= span; |
93bd25bb | 2483 | add: |
5262f567 | 2484 | list_add(&req->list, entry); |
ad8a48ac JA |
2485 | data = req->timeout.data; |
2486 | data->timer.function = io_timeout_fn; | |
2487 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 2488 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
2489 | return 0; |
2490 | } | |
5262f567 | 2491 | |
62755e35 JA |
2492 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
2493 | { | |
2494 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
2495 | ||
2496 | return req->user_data == (unsigned long) data; | |
2497 | } | |
2498 | ||
e977d6d3 | 2499 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 2500 | { |
62755e35 | 2501 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
2502 | int ret = 0; |
2503 | ||
62755e35 JA |
2504 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
2505 | switch (cancel_ret) { | |
2506 | case IO_WQ_CANCEL_OK: | |
2507 | ret = 0; | |
2508 | break; | |
2509 | case IO_WQ_CANCEL_RUNNING: | |
2510 | ret = -EALREADY; | |
2511 | break; | |
2512 | case IO_WQ_CANCEL_NOTFOUND: | |
2513 | ret = -ENOENT; | |
2514 | break; | |
2515 | } | |
2516 | ||
e977d6d3 JA |
2517 | return ret; |
2518 | } | |
2519 | ||
47f46768 JA |
2520 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
2521 | struct io_kiocb *req, __u64 sqe_addr, | |
b0dd8a41 | 2522 | struct io_kiocb **nxt, int success_ret) |
47f46768 JA |
2523 | { |
2524 | unsigned long flags; | |
2525 | int ret; | |
2526 | ||
2527 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
2528 | if (ret != -ENOENT) { | |
2529 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
2530 | goto done; | |
2531 | } | |
2532 | ||
2533 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
2534 | ret = io_timeout_cancel(ctx, sqe_addr); | |
2535 | if (ret != -ENOENT) | |
2536 | goto done; | |
2537 | ret = io_poll_cancel(ctx, sqe_addr); | |
2538 | done: | |
b0dd8a41 JA |
2539 | if (!ret) |
2540 | ret = success_ret; | |
47f46768 JA |
2541 | io_cqring_fill_event(req, ret); |
2542 | io_commit_cqring(ctx); | |
2543 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
2544 | io_cqring_ev_posted(ctx); | |
2545 | ||
2546 | if (ret < 0 && (req->flags & REQ_F_LINK)) | |
2547 | req->flags |= REQ_F_FAIL_LINK; | |
2548 | io_put_req_find_next(req, nxt); | |
2549 | } | |
2550 | ||
e977d6d3 JA |
2551 | static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2552 | struct io_kiocb **nxt) | |
2553 | { | |
2554 | struct io_ring_ctx *ctx = req->ctx; | |
e977d6d3 JA |
2555 | |
2556 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
2557 | return -EINVAL; | |
2558 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || | |
2559 | sqe->cancel_flags) | |
2560 | return -EINVAL; | |
2561 | ||
b0dd8a41 | 2562 | io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0); |
5262f567 JA |
2563 | return 0; |
2564 | } | |
2565 | ||
a197f664 | 2566 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 2567 | { |
267bc904 | 2568 | const struct io_uring_sqe *sqe = req->submit.sqe; |
de0617e4 | 2569 | struct io_uring_sqe *sqe_copy; |
a197f664 | 2570 | struct io_ring_ctx *ctx = req->ctx; |
de0617e4 | 2571 | |
9d858b21 BL |
2572 | /* Still need defer if there is pending req in defer list. */ |
2573 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) | |
de0617e4 JA |
2574 | return 0; |
2575 | ||
2576 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); | |
2577 | if (!sqe_copy) | |
2578 | return -EAGAIN; | |
2579 | ||
2580 | spin_lock_irq(&ctx->completion_lock); | |
9d858b21 | 2581 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
de0617e4 JA |
2582 | spin_unlock_irq(&ctx->completion_lock); |
2583 | kfree(sqe_copy); | |
2584 | return 0; | |
2585 | } | |
2586 | ||
2587 | memcpy(sqe_copy, sqe, sizeof(*sqe_copy)); | |
bbad27b2 | 2588 | req->flags |= REQ_F_FREE_SQE; |
de0617e4 JA |
2589 | req->submit.sqe = sqe_copy; |
2590 | ||
915967f6 | 2591 | trace_io_uring_defer(ctx, req, req->user_data); |
de0617e4 JA |
2592 | list_add_tail(&req->list, &ctx->defer_list); |
2593 | spin_unlock_irq(&ctx->completion_lock); | |
2594 | return -EIOCBQUEUED; | |
2595 | } | |
2596 | ||
d732447f PB |
2597 | static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt, |
2598 | bool force_nonblock) | |
2b188cc1 | 2599 | { |
e0c5c576 | 2600 | int ret, opcode; |
267bc904 | 2601 | struct sqe_submit *s = &req->submit; |
a197f664 | 2602 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
2603 | |
2604 | opcode = READ_ONCE(s->sqe->opcode); | |
2605 | switch (opcode) { | |
2606 | case IORING_OP_NOP: | |
78e19bbe | 2607 | ret = io_nop(req); |
2b188cc1 JA |
2608 | break; |
2609 | case IORING_OP_READV: | |
edafccee JA |
2610 | if (unlikely(s->sqe->buf_index)) |
2611 | return -EINVAL; | |
267bc904 | 2612 | ret = io_read(req, nxt, force_nonblock); |
2b188cc1 JA |
2613 | break; |
2614 | case IORING_OP_WRITEV: | |
edafccee JA |
2615 | if (unlikely(s->sqe->buf_index)) |
2616 | return -EINVAL; | |
267bc904 | 2617 | ret = io_write(req, nxt, force_nonblock); |
edafccee JA |
2618 | break; |
2619 | case IORING_OP_READ_FIXED: | |
267bc904 | 2620 | ret = io_read(req, nxt, force_nonblock); |
edafccee JA |
2621 | break; |
2622 | case IORING_OP_WRITE_FIXED: | |
267bc904 | 2623 | ret = io_write(req, nxt, force_nonblock); |
2b188cc1 | 2624 | break; |
c992fe29 | 2625 | case IORING_OP_FSYNC: |
ba816ad6 | 2626 | ret = io_fsync(req, s->sqe, nxt, force_nonblock); |
c992fe29 | 2627 | break; |
221c5eb2 | 2628 | case IORING_OP_POLL_ADD: |
89723d0b | 2629 | ret = io_poll_add(req, s->sqe, nxt); |
221c5eb2 JA |
2630 | break; |
2631 | case IORING_OP_POLL_REMOVE: | |
2632 | ret = io_poll_remove(req, s->sqe); | |
2633 | break; | |
5d17b4a4 | 2634 | case IORING_OP_SYNC_FILE_RANGE: |
ba816ad6 | 2635 | ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock); |
5d17b4a4 | 2636 | break; |
0fa03c62 | 2637 | case IORING_OP_SENDMSG: |
ba816ad6 | 2638 | ret = io_sendmsg(req, s->sqe, nxt, force_nonblock); |
0fa03c62 | 2639 | break; |
aa1fa28f | 2640 | case IORING_OP_RECVMSG: |
ba816ad6 | 2641 | ret = io_recvmsg(req, s->sqe, nxt, force_nonblock); |
aa1fa28f | 2642 | break; |
5262f567 JA |
2643 | case IORING_OP_TIMEOUT: |
2644 | ret = io_timeout(req, s->sqe); | |
2645 | break; | |
11365043 JA |
2646 | case IORING_OP_TIMEOUT_REMOVE: |
2647 | ret = io_timeout_remove(req, s->sqe); | |
2648 | break; | |
17f2fe35 JA |
2649 | case IORING_OP_ACCEPT: |
2650 | ret = io_accept(req, s->sqe, nxt, force_nonblock); | |
2651 | break; | |
62755e35 JA |
2652 | case IORING_OP_ASYNC_CANCEL: |
2653 | ret = io_async_cancel(req, s->sqe, nxt); | |
2654 | break; | |
2b188cc1 JA |
2655 | default: |
2656 | ret = -EINVAL; | |
2657 | break; | |
2658 | } | |
2659 | ||
def596e9 JA |
2660 | if (ret) |
2661 | return ret; | |
2662 | ||
2663 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
9e645e11 | 2664 | if (req->result == -EAGAIN) |
def596e9 JA |
2665 | return -EAGAIN; |
2666 | ||
2667 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
ba5290cc | 2668 | if (s->in_async) |
def596e9 JA |
2669 | mutex_lock(&ctx->uring_lock); |
2670 | io_iopoll_req_issued(req); | |
ba5290cc | 2671 | if (s->in_async) |
def596e9 JA |
2672 | mutex_unlock(&ctx->uring_lock); |
2673 | } | |
2674 | ||
2675 | return 0; | |
2b188cc1 JA |
2676 | } |
2677 | ||
b76da70f JA |
2678 | static void io_link_work_cb(struct io_wq_work **workptr) |
2679 | { | |
2680 | struct io_wq_work *work = *workptr; | |
2681 | struct io_kiocb *link = work->data; | |
2682 | ||
2683 | io_queue_linked_timeout(link); | |
2684 | work->func = io_wq_submit_work; | |
2685 | } | |
2686 | ||
561fb04a | 2687 | static void io_wq_submit_work(struct io_wq_work **workptr) |
2b188cc1 | 2688 | { |
561fb04a | 2689 | struct io_wq_work *work = *workptr; |
2b188cc1 | 2690 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
561fb04a | 2691 | struct sqe_submit *s = &req->submit; |
561fb04a JA |
2692 | struct io_kiocb *nxt = NULL; |
2693 | int ret = 0; | |
2b188cc1 | 2694 | |
561fb04a JA |
2695 | /* Ensure we clear previously set non-block flag */ |
2696 | req->rw.ki_flags &= ~IOCB_NOWAIT; | |
2b188cc1 | 2697 | |
561fb04a JA |
2698 | if (work->flags & IO_WQ_WORK_CANCEL) |
2699 | ret = -ECANCELED; | |
31b51510 | 2700 | |
561fb04a JA |
2701 | if (!ret) { |
2702 | s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; | |
2703 | s->in_async = true; | |
2704 | do { | |
d732447f | 2705 | ret = io_issue_sqe(req, &nxt, false); |
561fb04a JA |
2706 | /* |
2707 | * We can get EAGAIN for polled IO even though we're | |
2708 | * forcing a sync submission from here, since we can't | |
2709 | * wait for request slots on the block side. | |
2710 | */ | |
2711 | if (ret != -EAGAIN) | |
2712 | break; | |
2713 | cond_resched(); | |
2714 | } while (1); | |
2715 | } | |
31b51510 | 2716 | |
561fb04a | 2717 | /* drop submission reference */ |
ec9c02ad | 2718 | io_put_req(req); |
817869d2 | 2719 | |
561fb04a | 2720 | if (ret) { |
f1f40853 JA |
2721 | if (req->flags & REQ_F_LINK) |
2722 | req->flags |= REQ_F_FAIL_LINK; | |
78e19bbe | 2723 | io_cqring_add_event(req, ret); |
817869d2 | 2724 | io_put_req(req); |
edafccee | 2725 | } |
2b188cc1 | 2726 | |
561fb04a JA |
2727 | /* if a dependent link is ready, pass it back */ |
2728 | if (!ret && nxt) { | |
94ae5e77 JA |
2729 | struct io_kiocb *link; |
2730 | ||
2731 | io_prep_async_work(nxt, &link); | |
561fb04a | 2732 | *workptr = &nxt->work; |
b76da70f JA |
2733 | if (link) { |
2734 | nxt->work.flags |= IO_WQ_WORK_CB; | |
2735 | nxt->work.func = io_link_work_cb; | |
2736 | nxt->work.data = link; | |
2737 | } | |
31b51510 | 2738 | } |
2b188cc1 JA |
2739 | } |
2740 | ||
09bb8394 JA |
2741 | static bool io_op_needs_file(const struct io_uring_sqe *sqe) |
2742 | { | |
2743 | int op = READ_ONCE(sqe->opcode); | |
2744 | ||
2745 | switch (op) { | |
2746 | case IORING_OP_NOP: | |
2747 | case IORING_OP_POLL_REMOVE: | |
5683e540 | 2748 | case IORING_OP_TIMEOUT: |
a320e9fa PB |
2749 | case IORING_OP_TIMEOUT_REMOVE: |
2750 | case IORING_OP_ASYNC_CANCEL: | |
2751 | case IORING_OP_LINK_TIMEOUT: | |
09bb8394 JA |
2752 | return false; |
2753 | default: | |
2754 | return true; | |
2755 | } | |
2756 | } | |
2757 | ||
65e19f54 JA |
2758 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
2759 | int index) | |
2760 | { | |
2761 | struct fixed_file_table *table; | |
2762 | ||
2763 | table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT]; | |
2764 | return table->files[index & IORING_FILE_TABLE_MASK]; | |
2765 | } | |
2766 | ||
a197f664 | 2767 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req) |
09bb8394 | 2768 | { |
267bc904 | 2769 | struct sqe_submit *s = &req->submit; |
a197f664 | 2770 | struct io_ring_ctx *ctx = req->ctx; |
09bb8394 JA |
2771 | unsigned flags; |
2772 | int fd; | |
2773 | ||
2774 | flags = READ_ONCE(s->sqe->flags); | |
2775 | fd = READ_ONCE(s->sqe->fd); | |
2776 | ||
4fe2c963 | 2777 | if (flags & IOSQE_IO_DRAIN) |
de0617e4 | 2778 | req->flags |= REQ_F_IO_DRAIN; |
4fe2c963 JL |
2779 | /* |
2780 | * All io need record the previous position, if LINK vs DARIN, | |
2781 | * it can be used to mark the position of the first IO in the | |
2782 | * link list. | |
2783 | */ | |
2784 | req->sequence = s->sequence; | |
de0617e4 | 2785 | |
60c112b0 | 2786 | if (!io_op_needs_file(s->sqe)) |
09bb8394 | 2787 | return 0; |
09bb8394 JA |
2788 | |
2789 | if (flags & IOSQE_FIXED_FILE) { | |
65e19f54 | 2790 | if (unlikely(!ctx->file_table || |
09bb8394 JA |
2791 | (unsigned) fd >= ctx->nr_user_files)) |
2792 | return -EBADF; | |
b7620121 | 2793 | fd = array_index_nospec(fd, ctx->nr_user_files); |
65e19f54 JA |
2794 | req->file = io_file_from_index(ctx, fd); |
2795 | if (!req->file) | |
08a45173 | 2796 | return -EBADF; |
09bb8394 JA |
2797 | req->flags |= REQ_F_FIXED_FILE; |
2798 | } else { | |
2799 | if (s->needs_fixed_file) | |
2800 | return -EBADF; | |
c826bd7a | 2801 | trace_io_uring_file_get(ctx, fd); |
09bb8394 JA |
2802 | req->file = io_file_get(state, fd); |
2803 | if (unlikely(!req->file)) | |
2804 | return -EBADF; | |
2805 | } | |
2806 | ||
2807 | return 0; | |
2808 | } | |
2809 | ||
a197f664 | 2810 | static int io_grab_files(struct io_kiocb *req) |
fcb323cc JA |
2811 | { |
2812 | int ret = -EBADF; | |
a197f664 | 2813 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc JA |
2814 | |
2815 | rcu_read_lock(); | |
2816 | spin_lock_irq(&ctx->inflight_lock); | |
2817 | /* | |
2818 | * We use the f_ops->flush() handler to ensure that we can flush | |
2819 | * out work accessing these files if the fd is closed. Check if | |
2820 | * the fd has changed since we started down this path, and disallow | |
2821 | * this operation if it has. | |
2822 | */ | |
2823 | if (fcheck(req->submit.ring_fd) == req->submit.ring_file) { | |
2824 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
2825 | req->flags |= REQ_F_INFLIGHT; | |
2826 | req->work.files = current->files; | |
2827 | ret = 0; | |
2828 | } | |
2829 | spin_unlock_irq(&ctx->inflight_lock); | |
2830 | rcu_read_unlock(); | |
2831 | ||
2832 | return ret; | |
2833 | } | |
2834 | ||
2665abfd | 2835 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 2836 | { |
ad8a48ac JA |
2837 | struct io_timeout_data *data = container_of(timer, |
2838 | struct io_timeout_data, timer); | |
2839 | struct io_kiocb *req = data->req; | |
2665abfd JA |
2840 | struct io_ring_ctx *ctx = req->ctx; |
2841 | struct io_kiocb *prev = NULL; | |
2842 | unsigned long flags; | |
2665abfd JA |
2843 | |
2844 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
2845 | ||
2846 | /* | |
2847 | * We don't expect the list to be empty, that will only happen if we | |
2848 | * race with the completion of the linked work. | |
2849 | */ | |
2850 | if (!list_empty(&req->list)) { | |
2851 | prev = list_entry(req->list.prev, struct io_kiocb, link_list); | |
5d960724 | 2852 | if (refcount_inc_not_zero(&prev->refs)) { |
76a46e06 | 2853 | list_del_init(&req->list); |
5d960724 JA |
2854 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
2855 | } else | |
76a46e06 | 2856 | prev = NULL; |
2665abfd JA |
2857 | } |
2858 | ||
2859 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
2860 | ||
2861 | if (prev) { | |
fba38c27 JA |
2862 | if (prev->flags & REQ_F_LINK) |
2863 | prev->flags |= REQ_F_FAIL_LINK; | |
b0dd8a41 JA |
2864 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
2865 | -ETIME); | |
76a46e06 | 2866 | io_put_req(prev); |
47f46768 JA |
2867 | } else { |
2868 | io_cqring_add_event(req, -ETIME); | |
2869 | io_put_req(req); | |
2665abfd | 2870 | } |
2665abfd JA |
2871 | return HRTIMER_NORESTART; |
2872 | } | |
2873 | ||
ad8a48ac | 2874 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 2875 | { |
76a46e06 | 2876 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 2877 | |
76a46e06 JA |
2878 | /* |
2879 | * If the list is now empty, then our linked request finished before | |
2880 | * we got a chance to setup the timer | |
2881 | */ | |
2882 | spin_lock_irq(&ctx->completion_lock); | |
2883 | if (!list_empty(&req->list)) { | |
94ae5e77 JA |
2884 | struct io_timeout_data *data = req->timeout.data; |
2885 | ||
ad8a48ac JA |
2886 | data->timer.function = io_link_timeout_fn; |
2887 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
2888 | data->mode); | |
2665abfd | 2889 | } |
76a46e06 | 2890 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 2891 | |
2665abfd | 2892 | /* drop submission reference */ |
76a46e06 JA |
2893 | io_put_req(req); |
2894 | } | |
2665abfd | 2895 | |
ad8a48ac | 2896 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd JA |
2897 | { |
2898 | struct io_kiocb *nxt; | |
2899 | ||
2900 | if (!(req->flags & REQ_F_LINK)) | |
2901 | return NULL; | |
2902 | ||
2903 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); | |
76a46e06 JA |
2904 | if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT) |
2905 | return NULL; | |
2665abfd | 2906 | |
76a46e06 | 2907 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 2908 | return nxt; |
2665abfd JA |
2909 | } |
2910 | ||
0e0702da | 2911 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 2912 | { |
94ae5e77 | 2913 | struct io_kiocb *nxt = io_prep_linked_timeout(req); |
e0c5c576 | 2914 | int ret; |
2b188cc1 | 2915 | |
d732447f | 2916 | ret = io_issue_sqe(req, NULL, true); |
491381ce JA |
2917 | |
2918 | /* | |
2919 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
2920 | * doesn't support non-blocking read/write attempts | |
2921 | */ | |
2922 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || | |
2923 | (req->flags & REQ_F_MUST_PUNT))) { | |
267bc904 | 2924 | struct sqe_submit *s = &req->submit; |
2b188cc1 JA |
2925 | struct io_uring_sqe *sqe_copy; |
2926 | ||
954dab19 | 2927 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
bbad27b2 PB |
2928 | if (!sqe_copy) |
2929 | goto err; | |
e65ef56d | 2930 | |
bbad27b2 PB |
2931 | s->sqe = sqe_copy; |
2932 | req->flags |= REQ_F_FREE_SQE; | |
2933 | ||
2934 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { | |
2935 | ret = io_grab_files(req); | |
2936 | if (ret) | |
2937 | goto err; | |
2b188cc1 | 2938 | } |
bbad27b2 PB |
2939 | |
2940 | /* | |
2941 | * Queued up for async execution, worker will release | |
2942 | * submit reference when the iocb is actually submitted. | |
2943 | */ | |
2944 | io_queue_async_work(req); | |
2945 | return; | |
2b188cc1 | 2946 | } |
e65ef56d | 2947 | |
fcb323cc | 2948 | err: |
76a46e06 | 2949 | /* drop submission reference */ |
ec9c02ad | 2950 | io_put_req(req); |
e65ef56d | 2951 | |
76a46e06 JA |
2952 | if (nxt) { |
2953 | if (!ret) | |
ad8a48ac | 2954 | io_queue_linked_timeout(nxt); |
76a46e06 JA |
2955 | else |
2956 | io_put_req(nxt); | |
2957 | } | |
2958 | ||
e65ef56d | 2959 | /* and drop final reference, if we failed */ |
9e645e11 | 2960 | if (ret) { |
78e19bbe | 2961 | io_cqring_add_event(req, ret); |
9e645e11 JA |
2962 | if (req->flags & REQ_F_LINK) |
2963 | req->flags |= REQ_F_FAIL_LINK; | |
e65ef56d | 2964 | io_put_req(req); |
9e645e11 | 2965 | } |
2b188cc1 JA |
2966 | } |
2967 | ||
0e0702da | 2968 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
2969 | { |
2970 | int ret; | |
2971 | ||
1b4a51b6 PB |
2972 | if (unlikely(req->ctx->drain_next)) { |
2973 | req->flags |= REQ_F_IO_DRAIN; | |
2974 | req->ctx->drain_next = false; | |
2975 | } | |
2976 | req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK); | |
2977 | ||
a197f664 | 2978 | ret = io_req_defer(req); |
4fe2c963 JL |
2979 | if (ret) { |
2980 | if (ret != -EIOCBQUEUED) { | |
78e19bbe | 2981 | io_cqring_add_event(req, ret); |
d3b35796 PB |
2982 | if (req->flags & REQ_F_LINK) |
2983 | req->flags |= REQ_F_FAIL_LINK; | |
78e19bbe | 2984 | io_double_put_req(req); |
4fe2c963 | 2985 | } |
0e0702da JA |
2986 | } else |
2987 | __io_queue_sqe(req); | |
4fe2c963 JL |
2988 | } |
2989 | ||
1b4a51b6 | 2990 | static inline void io_queue_link_head(struct io_kiocb *req) |
4fe2c963 | 2991 | { |
94ae5e77 | 2992 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
1b4a51b6 PB |
2993 | io_cqring_add_event(req, -ECANCELED); |
2994 | io_double_put_req(req); | |
2995 | } else | |
0e0702da | 2996 | io_queue_sqe(req); |
4fe2c963 JL |
2997 | } |
2998 | ||
1b4a51b6 | 2999 | |
9e645e11 JA |
3000 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK) |
3001 | ||
a197f664 JL |
3002 | static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state, |
3003 | struct io_kiocb **link) | |
9e645e11 | 3004 | { |
267bc904 | 3005 | struct sqe_submit *s = &req->submit; |
a197f664 | 3006 | struct io_ring_ctx *ctx = req->ctx; |
9e645e11 JA |
3007 | int ret; |
3008 | ||
78e19bbe JA |
3009 | req->user_data = s->sqe->user_data; |
3010 | ||
9e645e11 JA |
3011 | /* enforce forwards compatibility on users */ |
3012 | if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) { | |
3013 | ret = -EINVAL; | |
196be95c | 3014 | goto err_req; |
9e645e11 JA |
3015 | } |
3016 | ||
a197f664 | 3017 | ret = io_req_set_file(state, req); |
9e645e11 JA |
3018 | if (unlikely(ret)) { |
3019 | err_req: | |
78e19bbe JA |
3020 | io_cqring_add_event(req, ret); |
3021 | io_double_put_req(req); | |
9e645e11 JA |
3022 | return; |
3023 | } | |
3024 | ||
9e645e11 JA |
3025 | /* |
3026 | * If we already have a head request, queue this one for async | |
3027 | * submittal once the head completes. If we don't have a head but | |
3028 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
3029 | * submitted sync once the chain is complete. If none of those | |
3030 | * conditions are true (normal request), then just queue it. | |
3031 | */ | |
3032 | if (*link) { | |
3033 | struct io_kiocb *prev = *link; | |
bbad27b2 | 3034 | struct io_uring_sqe *sqe_copy; |
9e645e11 | 3035 | |
1b4a51b6 PB |
3036 | if (s->sqe->flags & IOSQE_IO_DRAIN) |
3037 | (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN; | |
3038 | ||
94ae5e77 JA |
3039 | if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) { |
3040 | ret = io_timeout_setup(req); | |
3041 | /* common setup allows offset being set, we don't */ | |
3042 | if (!ret && s->sqe->off) | |
3043 | ret = -EINVAL; | |
3044 | if (ret) { | |
3045 | prev->flags |= REQ_F_FAIL_LINK; | |
3046 | goto err_req; | |
3047 | } | |
3048 | } | |
3049 | ||
9e645e11 JA |
3050 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
3051 | if (!sqe_copy) { | |
3052 | ret = -EAGAIN; | |
3053 | goto err_req; | |
3054 | } | |
3055 | ||
3056 | s->sqe = sqe_copy; | |
94ae5e77 | 3057 | req->flags |= REQ_F_FREE_SQE; |
c826bd7a | 3058 | trace_io_uring_link(ctx, req, prev); |
9e645e11 JA |
3059 | list_add_tail(&req->list, &prev->link_list); |
3060 | } else if (s->sqe->flags & IOSQE_IO_LINK) { | |
3061 | req->flags |= REQ_F_LINK; | |
3062 | ||
9e645e11 JA |
3063 | INIT_LIST_HEAD(&req->link_list); |
3064 | *link = req; | |
3065 | } else { | |
a197f664 | 3066 | io_queue_sqe(req); |
9e645e11 JA |
3067 | } |
3068 | } | |
3069 | ||
9a56a232 JA |
3070 | /* |
3071 | * Batched submission is done, ensure local IO is flushed out. | |
3072 | */ | |
3073 | static void io_submit_state_end(struct io_submit_state *state) | |
3074 | { | |
3075 | blk_finish_plug(&state->plug); | |
3d6770fb | 3076 | io_file_put(state); |
2579f913 JA |
3077 | if (state->free_reqs) |
3078 | kmem_cache_free_bulk(req_cachep, state->free_reqs, | |
3079 | &state->reqs[state->cur_req]); | |
9a56a232 JA |
3080 | } |
3081 | ||
3082 | /* | |
3083 | * Start submission side cache. | |
3084 | */ | |
3085 | static void io_submit_state_start(struct io_submit_state *state, | |
3086 | struct io_ring_ctx *ctx, unsigned max_ios) | |
3087 | { | |
3088 | blk_start_plug(&state->plug); | |
2579f913 | 3089 | state->free_reqs = 0; |
9a56a232 JA |
3090 | state->file = NULL; |
3091 | state->ios_left = max_ios; | |
3092 | } | |
3093 | ||
2b188cc1 JA |
3094 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
3095 | { | |
75b28aff | 3096 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 3097 | |
75b28aff | 3098 | if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) { |
2b188cc1 JA |
3099 | /* |
3100 | * Ensure any loads from the SQEs are done at this point, | |
3101 | * since once we write the new head, the application could | |
3102 | * write new data to them. | |
3103 | */ | |
75b28aff | 3104 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
2b188cc1 JA |
3105 | } |
3106 | } | |
3107 | ||
2b188cc1 JA |
3108 | /* |
3109 | * Fetch an sqe, if one is available. Note that s->sqe will point to memory | |
3110 | * that is mapped by userspace. This means that care needs to be taken to | |
3111 | * ensure that reads are stable, as we cannot rely on userspace always | |
3112 | * being a good citizen. If members of the sqe are validated and then later | |
3113 | * used, it's important that those reads are done through READ_ONCE() to | |
3114 | * prevent a re-load down the line. | |
3115 | */ | |
3116 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) | |
3117 | { | |
75b28aff HV |
3118 | struct io_rings *rings = ctx->rings; |
3119 | u32 *sq_array = ctx->sq_array; | |
2b188cc1 JA |
3120 | unsigned head; |
3121 | ||
3122 | /* | |
3123 | * The cached sq head (or cq tail) serves two purposes: | |
3124 | * | |
3125 | * 1) allows us to batch the cost of updating the user visible | |
3126 | * head updates. | |
3127 | * 2) allows the kernel side to track the head on its own, even | |
3128 | * though the application is the one updating it. | |
3129 | */ | |
3130 | head = ctx->cached_sq_head; | |
e523a29c | 3131 | /* make sure SQ entry isn't read before tail */ |
9835d6fa | 3132 | if (unlikely(head == smp_load_acquire(&rings->sq.tail))) |
2b188cc1 JA |
3133 | return false; |
3134 | ||
75b28aff | 3135 | head = READ_ONCE(sq_array[head & ctx->sq_mask]); |
9835d6fa | 3136 | if (likely(head < ctx->sq_entries)) { |
fcb323cc | 3137 | s->ring_file = NULL; |
2b188cc1 | 3138 | s->sqe = &ctx->sq_sqes[head]; |
8776f3fa | 3139 | s->sequence = ctx->cached_sq_head; |
2b188cc1 JA |
3140 | ctx->cached_sq_head++; |
3141 | return true; | |
3142 | } | |
3143 | ||
3144 | /* drop invalid entries */ | |
3145 | ctx->cached_sq_head++; | |
498ccd9e JA |
3146 | ctx->cached_sq_dropped++; |
3147 | WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped); | |
2b188cc1 JA |
3148 | return false; |
3149 | } | |
3150 | ||
fb5ccc98 | 3151 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
ae9428ca PB |
3152 | struct file *ring_file, int ring_fd, |
3153 | struct mm_struct **mm, bool async) | |
6c271ce2 JA |
3154 | { |
3155 | struct io_submit_state state, *statep = NULL; | |
9e645e11 | 3156 | struct io_kiocb *link = NULL; |
9e645e11 | 3157 | int i, submitted = 0; |
95a1b3ff | 3158 | bool mm_fault = false; |
6c271ce2 | 3159 | |
1d7bb1d5 JA |
3160 | if (!list_empty(&ctx->cq_overflow_list)) { |
3161 | io_cqring_overflow_flush(ctx, false); | |
3162 | return -EBUSY; | |
3163 | } | |
6c271ce2 JA |
3164 | |
3165 | if (nr > IO_PLUG_THRESHOLD) { | |
3166 | io_submit_state_start(&state, ctx, nr); | |
3167 | statep = &state; | |
3168 | } | |
3169 | ||
3170 | for (i = 0; i < nr; i++) { | |
196be95c | 3171 | struct io_kiocb *req; |
50585b9a | 3172 | unsigned int sqe_flags; |
fb5ccc98 | 3173 | |
196be95c PB |
3174 | req = io_get_req(ctx, statep); |
3175 | if (unlikely(!req)) { | |
3176 | if (!submitted) | |
3177 | submitted = -EAGAIN; | |
fb5ccc98 | 3178 | break; |
196be95c | 3179 | } |
50585b9a | 3180 | if (!io_get_sqring(ctx, &req->submit)) { |
196be95c PB |
3181 | __io_free_req(req); |
3182 | break; | |
3183 | } | |
fb5ccc98 | 3184 | |
50585b9a | 3185 | if (io_sqe_needs_user(req->submit.sqe) && !*mm) { |
95a1b3ff PB |
3186 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
3187 | if (!mm_fault) { | |
3188 | use_mm(ctx->sqo_mm); | |
3189 | *mm = ctx->sqo_mm; | |
3190 | } | |
9e645e11 | 3191 | } |
9e645e11 | 3192 | |
50585b9a PB |
3193 | sqe_flags = req->submit.sqe->flags; |
3194 | ||
50585b9a PB |
3195 | req->submit.ring_file = ring_file; |
3196 | req->submit.ring_fd = ring_fd; | |
3197 | req->submit.has_user = *mm != NULL; | |
3198 | req->submit.in_async = async; | |
3199 | req->submit.needs_fixed_file = async; | |
3200 | trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data, | |
3201 | true, async); | |
a197f664 | 3202 | io_submit_sqe(req, statep, &link); |
95a1b3ff | 3203 | submitted++; |
e5eb6366 PB |
3204 | |
3205 | /* | |
3206 | * If previous wasn't linked and we have a linked command, | |
3207 | * that's the end of the chain. Submit the previous link. | |
3208 | */ | |
50585b9a | 3209 | if (!(sqe_flags & IOSQE_IO_LINK) && link) { |
1b4a51b6 | 3210 | io_queue_link_head(link); |
e5eb6366 | 3211 | link = NULL; |
6c271ce2 | 3212 | } |
6c271ce2 JA |
3213 | } |
3214 | ||
9e645e11 | 3215 | if (link) |
1b4a51b6 | 3216 | io_queue_link_head(link); |
6c271ce2 JA |
3217 | if (statep) |
3218 | io_submit_state_end(&state); | |
3219 | ||
ae9428ca PB |
3220 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
3221 | io_commit_sqring(ctx); | |
3222 | ||
6c271ce2 JA |
3223 | return submitted; |
3224 | } | |
3225 | ||
3226 | static int io_sq_thread(void *data) | |
3227 | { | |
6c271ce2 JA |
3228 | struct io_ring_ctx *ctx = data; |
3229 | struct mm_struct *cur_mm = NULL; | |
3230 | mm_segment_t old_fs; | |
3231 | DEFINE_WAIT(wait); | |
3232 | unsigned inflight; | |
3233 | unsigned long timeout; | |
c1edbf5f | 3234 | int ret; |
6c271ce2 | 3235 | |
206aefde | 3236 | complete(&ctx->completions[1]); |
a4c0b3de | 3237 | |
6c271ce2 JA |
3238 | old_fs = get_fs(); |
3239 | set_fs(USER_DS); | |
3240 | ||
c1edbf5f | 3241 | ret = timeout = inflight = 0; |
2bbcd6d3 | 3242 | while (!kthread_should_park()) { |
fb5ccc98 | 3243 | unsigned int to_submit; |
6c271ce2 JA |
3244 | |
3245 | if (inflight) { | |
3246 | unsigned nr_events = 0; | |
3247 | ||
3248 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
2b2ed975 JA |
3249 | /* |
3250 | * inflight is the count of the maximum possible | |
3251 | * entries we submitted, but it can be smaller | |
3252 | * if we dropped some of them. If we don't have | |
3253 | * poll entries available, then we know that we | |
3254 | * have nothing left to poll for. Reset the | |
3255 | * inflight count to zero in that case. | |
3256 | */ | |
3257 | mutex_lock(&ctx->uring_lock); | |
3258 | if (!list_empty(&ctx->poll_list)) | |
3259 | __io_iopoll_check(ctx, &nr_events, 0); | |
3260 | else | |
3261 | inflight = 0; | |
3262 | mutex_unlock(&ctx->uring_lock); | |
6c271ce2 JA |
3263 | } else { |
3264 | /* | |
3265 | * Normal IO, just pretend everything completed. | |
3266 | * We don't have to poll completions for that. | |
3267 | */ | |
3268 | nr_events = inflight; | |
3269 | } | |
3270 | ||
3271 | inflight -= nr_events; | |
3272 | if (!inflight) | |
3273 | timeout = jiffies + ctx->sq_thread_idle; | |
3274 | } | |
3275 | ||
fb5ccc98 | 3276 | to_submit = io_sqring_entries(ctx); |
c1edbf5f JA |
3277 | |
3278 | /* | |
3279 | * If submit got -EBUSY, flag us as needing the application | |
3280 | * to enter the kernel to reap and flush events. | |
3281 | */ | |
3282 | if (!to_submit || ret == -EBUSY) { | |
6c271ce2 JA |
3283 | /* |
3284 | * We're polling. If we're within the defined idle | |
3285 | * period, then let us spin without work before going | |
c1edbf5f JA |
3286 | * to sleep. The exception is if we got EBUSY doing |
3287 | * more IO, we should wait for the application to | |
3288 | * reap events and wake us up. | |
6c271ce2 | 3289 | */ |
c1edbf5f JA |
3290 | if (inflight || |
3291 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { | |
9831a90c | 3292 | cond_resched(); |
6c271ce2 JA |
3293 | continue; |
3294 | } | |
3295 | ||
3296 | /* | |
3297 | * Drop cur_mm before scheduling, we can't hold it for | |
3298 | * long periods (or over schedule()). Do this before | |
3299 | * adding ourselves to the waitqueue, as the unuse/drop | |
3300 | * may sleep. | |
3301 | */ | |
3302 | if (cur_mm) { | |
3303 | unuse_mm(cur_mm); | |
3304 | mmput(cur_mm); | |
3305 | cur_mm = NULL; | |
3306 | } | |
3307 | ||
3308 | prepare_to_wait(&ctx->sqo_wait, &wait, | |
3309 | TASK_INTERRUPTIBLE); | |
3310 | ||
3311 | /* Tell userspace we may need a wakeup call */ | |
75b28aff | 3312 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
0d7bae69 SB |
3313 | /* make sure to read SQ tail after writing flags */ |
3314 | smp_mb(); | |
6c271ce2 | 3315 | |
fb5ccc98 | 3316 | to_submit = io_sqring_entries(ctx); |
c1edbf5f | 3317 | if (!to_submit || ret == -EBUSY) { |
2bbcd6d3 | 3318 | if (kthread_should_park()) { |
6c271ce2 JA |
3319 | finish_wait(&ctx->sqo_wait, &wait); |
3320 | break; | |
3321 | } | |
3322 | if (signal_pending(current)) | |
3323 | flush_signals(current); | |
3324 | schedule(); | |
3325 | finish_wait(&ctx->sqo_wait, &wait); | |
3326 | ||
75b28aff | 3327 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
3328 | continue; |
3329 | } | |
3330 | finish_wait(&ctx->sqo_wait, &wait); | |
3331 | ||
75b28aff | 3332 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
3333 | } |
3334 | ||
fb5ccc98 | 3335 | to_submit = min(to_submit, ctx->sq_entries); |
1d7bb1d5 JA |
3336 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
3337 | if (ret > 0) | |
3338 | inflight += ret; | |
6c271ce2 JA |
3339 | } |
3340 | ||
3341 | set_fs(old_fs); | |
3342 | if (cur_mm) { | |
3343 | unuse_mm(cur_mm); | |
3344 | mmput(cur_mm); | |
3345 | } | |
06058632 | 3346 | |
2bbcd6d3 | 3347 | kthread_parkme(); |
06058632 | 3348 | |
6c271ce2 JA |
3349 | return 0; |
3350 | } | |
3351 | ||
bda52162 JA |
3352 | struct io_wait_queue { |
3353 | struct wait_queue_entry wq; | |
3354 | struct io_ring_ctx *ctx; | |
3355 | unsigned to_wait; | |
3356 | unsigned nr_timeouts; | |
3357 | }; | |
3358 | ||
1d7bb1d5 | 3359 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
3360 | { |
3361 | struct io_ring_ctx *ctx = iowq->ctx; | |
3362 | ||
3363 | /* | |
3364 | * Wake up if we have enough events, or if a timeout occured since we | |
3365 | * started waiting. For timeouts, we always want to return to userspace, | |
3366 | * regardless of event count. | |
3367 | */ | |
1d7bb1d5 | 3368 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
3369 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
3370 | } | |
3371 | ||
3372 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
3373 | int wake_flags, void *key) | |
3374 | { | |
3375 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
3376 | wq); | |
3377 | ||
1d7bb1d5 JA |
3378 | /* use noflush == true, as we can't safely rely on locking context */ |
3379 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
3380 | return -1; |
3381 | ||
3382 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
3383 | } | |
3384 | ||
2b188cc1 JA |
3385 | /* |
3386 | * Wait until events become available, if we don't already have some. The | |
3387 | * application must reap them itself, as they reside on the shared cq ring. | |
3388 | */ | |
3389 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
3390 | const sigset_t __user *sig, size_t sigsz) | |
3391 | { | |
bda52162 JA |
3392 | struct io_wait_queue iowq = { |
3393 | .wq = { | |
3394 | .private = current, | |
3395 | .func = io_wake_function, | |
3396 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
3397 | }, | |
3398 | .ctx = ctx, | |
3399 | .to_wait = min_events, | |
3400 | }; | |
75b28aff | 3401 | struct io_rings *rings = ctx->rings; |
e9ffa5c2 | 3402 | int ret = 0; |
2b188cc1 | 3403 | |
1d7bb1d5 | 3404 | if (io_cqring_events(ctx, false) >= min_events) |
2b188cc1 JA |
3405 | return 0; |
3406 | ||
3407 | if (sig) { | |
9e75ad5d AB |
3408 | #ifdef CONFIG_COMPAT |
3409 | if (in_compat_syscall()) | |
3410 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 3411 | sigsz); |
9e75ad5d AB |
3412 | else |
3413 | #endif | |
b772434b | 3414 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 3415 | |
2b188cc1 JA |
3416 | if (ret) |
3417 | return ret; | |
3418 | } | |
3419 | ||
bda52162 | 3420 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 3421 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
3422 | do { |
3423 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
3424 | TASK_INTERRUPTIBLE); | |
1d7bb1d5 | 3425 | if (io_should_wake(&iowq, false)) |
bda52162 JA |
3426 | break; |
3427 | schedule(); | |
3428 | if (signal_pending(current)) { | |
e9ffa5c2 | 3429 | ret = -EINTR; |
bda52162 JA |
3430 | break; |
3431 | } | |
3432 | } while (1); | |
3433 | finish_wait(&ctx->wait, &iowq.wq); | |
3434 | ||
e9ffa5c2 | 3435 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 3436 | |
75b28aff | 3437 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
3438 | } |
3439 | ||
6b06314c JA |
3440 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
3441 | { | |
3442 | #if defined(CONFIG_UNIX) | |
3443 | if (ctx->ring_sock) { | |
3444 | struct sock *sock = ctx->ring_sock->sk; | |
3445 | struct sk_buff *skb; | |
3446 | ||
3447 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
3448 | kfree_skb(skb); | |
3449 | } | |
3450 | #else | |
3451 | int i; | |
3452 | ||
65e19f54 JA |
3453 | for (i = 0; i < ctx->nr_user_files; i++) { |
3454 | struct file *file; | |
3455 | ||
3456 | file = io_file_from_index(ctx, i); | |
3457 | if (file) | |
3458 | fput(file); | |
3459 | } | |
6b06314c JA |
3460 | #endif |
3461 | } | |
3462 | ||
3463 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) | |
3464 | { | |
65e19f54 JA |
3465 | unsigned nr_tables, i; |
3466 | ||
3467 | if (!ctx->file_table) | |
6b06314c JA |
3468 | return -ENXIO; |
3469 | ||
3470 | __io_sqe_files_unregister(ctx); | |
65e19f54 JA |
3471 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
3472 | for (i = 0; i < nr_tables; i++) | |
3473 | kfree(ctx->file_table[i].files); | |
3474 | kfree(ctx->file_table); | |
3475 | ctx->file_table = NULL; | |
6b06314c JA |
3476 | ctx->nr_user_files = 0; |
3477 | return 0; | |
3478 | } | |
3479 | ||
6c271ce2 JA |
3480 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
3481 | { | |
3482 | if (ctx->sqo_thread) { | |
206aefde | 3483 | wait_for_completion(&ctx->completions[1]); |
2bbcd6d3 RP |
3484 | /* |
3485 | * The park is a bit of a work-around, without it we get | |
3486 | * warning spews on shutdown with SQPOLL set and affinity | |
3487 | * set to a single CPU. | |
3488 | */ | |
06058632 | 3489 | kthread_park(ctx->sqo_thread); |
6c271ce2 JA |
3490 | kthread_stop(ctx->sqo_thread); |
3491 | ctx->sqo_thread = NULL; | |
3492 | } | |
3493 | } | |
3494 | ||
6b06314c JA |
3495 | static void io_finish_async(struct io_ring_ctx *ctx) |
3496 | { | |
6c271ce2 JA |
3497 | io_sq_thread_stop(ctx); |
3498 | ||
561fb04a JA |
3499 | if (ctx->io_wq) { |
3500 | io_wq_destroy(ctx->io_wq); | |
3501 | ctx->io_wq = NULL; | |
6b06314c JA |
3502 | } |
3503 | } | |
3504 | ||
3505 | #if defined(CONFIG_UNIX) | |
3506 | static void io_destruct_skb(struct sk_buff *skb) | |
3507 | { | |
3508 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; | |
8a997340 | 3509 | |
561fb04a JA |
3510 | if (ctx->io_wq) |
3511 | io_wq_flush(ctx->io_wq); | |
6b06314c | 3512 | |
6b06314c JA |
3513 | unix_destruct_scm(skb); |
3514 | } | |
3515 | ||
3516 | /* | |
3517 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
3518 | * the io_uring can be safely unregistered on process exit, even if we have | |
3519 | * loops in the file referencing. | |
3520 | */ | |
3521 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
3522 | { | |
3523 | struct sock *sk = ctx->ring_sock->sk; | |
3524 | struct scm_fp_list *fpl; | |
3525 | struct sk_buff *skb; | |
08a45173 | 3526 | int i, nr_files; |
6b06314c JA |
3527 | |
3528 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { | |
3529 | unsigned long inflight = ctx->user->unix_inflight + nr; | |
3530 | ||
3531 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) | |
3532 | return -EMFILE; | |
3533 | } | |
3534 | ||
3535 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); | |
3536 | if (!fpl) | |
3537 | return -ENOMEM; | |
3538 | ||
3539 | skb = alloc_skb(0, GFP_KERNEL); | |
3540 | if (!skb) { | |
3541 | kfree(fpl); | |
3542 | return -ENOMEM; | |
3543 | } | |
3544 | ||
3545 | skb->sk = sk; | |
6b06314c | 3546 | |
08a45173 | 3547 | nr_files = 0; |
6b06314c JA |
3548 | fpl->user = get_uid(ctx->user); |
3549 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
3550 | struct file *file = io_file_from_index(ctx, i + offset); |
3551 | ||
3552 | if (!file) | |
08a45173 | 3553 | continue; |
65e19f54 | 3554 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
3555 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
3556 | nr_files++; | |
6b06314c JA |
3557 | } |
3558 | ||
08a45173 JA |
3559 | if (nr_files) { |
3560 | fpl->max = SCM_MAX_FD; | |
3561 | fpl->count = nr_files; | |
3562 | UNIXCB(skb).fp = fpl; | |
3563 | skb->destructor = io_destruct_skb; | |
3564 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); | |
3565 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 3566 | |
08a45173 JA |
3567 | for (i = 0; i < nr_files; i++) |
3568 | fput(fpl->fp[i]); | |
3569 | } else { | |
3570 | kfree_skb(skb); | |
3571 | kfree(fpl); | |
3572 | } | |
6b06314c JA |
3573 | |
3574 | return 0; | |
3575 | } | |
3576 | ||
3577 | /* | |
3578 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
3579 | * causes regular reference counting to break down. We rely on the UNIX | |
3580 | * garbage collection to take care of this problem for us. | |
3581 | */ | |
3582 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
3583 | { | |
3584 | unsigned left, total; | |
3585 | int ret = 0; | |
3586 | ||
3587 | total = 0; | |
3588 | left = ctx->nr_user_files; | |
3589 | while (left) { | |
3590 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
3591 | |
3592 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
3593 | if (ret) | |
3594 | break; | |
3595 | left -= this_files; | |
3596 | total += this_files; | |
3597 | } | |
3598 | ||
3599 | if (!ret) | |
3600 | return 0; | |
3601 | ||
3602 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
3603 | struct file *file = io_file_from_index(ctx, total); |
3604 | ||
3605 | if (file) | |
3606 | fput(file); | |
6b06314c JA |
3607 | total++; |
3608 | } | |
3609 | ||
3610 | return ret; | |
3611 | } | |
3612 | #else | |
3613 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
3614 | { | |
3615 | return 0; | |
3616 | } | |
3617 | #endif | |
3618 | ||
65e19f54 JA |
3619 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
3620 | unsigned nr_files) | |
3621 | { | |
3622 | int i; | |
3623 | ||
3624 | for (i = 0; i < nr_tables; i++) { | |
3625 | struct fixed_file_table *table = &ctx->file_table[i]; | |
3626 | unsigned this_files; | |
3627 | ||
3628 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
3629 | table->files = kcalloc(this_files, sizeof(struct file *), | |
3630 | GFP_KERNEL); | |
3631 | if (!table->files) | |
3632 | break; | |
3633 | nr_files -= this_files; | |
3634 | } | |
3635 | ||
3636 | if (i == nr_tables) | |
3637 | return 0; | |
3638 | ||
3639 | for (i = 0; i < nr_tables; i++) { | |
3640 | struct fixed_file_table *table = &ctx->file_table[i]; | |
3641 | kfree(table->files); | |
3642 | } | |
3643 | return 1; | |
3644 | } | |
3645 | ||
6b06314c JA |
3646 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
3647 | unsigned nr_args) | |
3648 | { | |
3649 | __s32 __user *fds = (__s32 __user *) arg; | |
65e19f54 | 3650 | unsigned nr_tables; |
6b06314c JA |
3651 | int fd, ret = 0; |
3652 | unsigned i; | |
3653 | ||
65e19f54 | 3654 | if (ctx->file_table) |
6b06314c JA |
3655 | return -EBUSY; |
3656 | if (!nr_args) | |
3657 | return -EINVAL; | |
3658 | if (nr_args > IORING_MAX_FIXED_FILES) | |
3659 | return -EMFILE; | |
3660 | ||
65e19f54 JA |
3661 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
3662 | ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table), | |
3663 | GFP_KERNEL); | |
3664 | if (!ctx->file_table) | |
6b06314c JA |
3665 | return -ENOMEM; |
3666 | ||
65e19f54 JA |
3667 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
3668 | kfree(ctx->file_table); | |
46568e9b | 3669 | ctx->file_table = NULL; |
65e19f54 JA |
3670 | return -ENOMEM; |
3671 | } | |
3672 | ||
08a45173 | 3673 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
3674 | struct fixed_file_table *table; |
3675 | unsigned index; | |
3676 | ||
6b06314c JA |
3677 | ret = -EFAULT; |
3678 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
3679 | break; | |
08a45173 JA |
3680 | /* allow sparse sets */ |
3681 | if (fd == -1) { | |
3682 | ret = 0; | |
3683 | continue; | |
3684 | } | |
6b06314c | 3685 | |
65e19f54 JA |
3686 | table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT]; |
3687 | index = i & IORING_FILE_TABLE_MASK; | |
3688 | table->files[index] = fget(fd); | |
6b06314c JA |
3689 | |
3690 | ret = -EBADF; | |
65e19f54 | 3691 | if (!table->files[index]) |
6b06314c JA |
3692 | break; |
3693 | /* | |
3694 | * Don't allow io_uring instances to be registered. If UNIX | |
3695 | * isn't enabled, then this causes a reference cycle and this | |
3696 | * instance can never get freed. If UNIX is enabled we'll | |
3697 | * handle it just fine, but there's still no point in allowing | |
3698 | * a ring fd as it doesn't support regular read/write anyway. | |
3699 | */ | |
65e19f54 JA |
3700 | if (table->files[index]->f_op == &io_uring_fops) { |
3701 | fput(table->files[index]); | |
6b06314c JA |
3702 | break; |
3703 | } | |
6b06314c JA |
3704 | ret = 0; |
3705 | } | |
3706 | ||
3707 | if (ret) { | |
65e19f54 JA |
3708 | for (i = 0; i < ctx->nr_user_files; i++) { |
3709 | struct file *file; | |
6b06314c | 3710 | |
65e19f54 JA |
3711 | file = io_file_from_index(ctx, i); |
3712 | if (file) | |
3713 | fput(file); | |
3714 | } | |
3715 | for (i = 0; i < nr_tables; i++) | |
3716 | kfree(ctx->file_table[i].files); | |
6b06314c | 3717 | |
65e19f54 JA |
3718 | kfree(ctx->file_table); |
3719 | ctx->file_table = NULL; | |
6b06314c JA |
3720 | ctx->nr_user_files = 0; |
3721 | return ret; | |
3722 | } | |
3723 | ||
3724 | ret = io_sqe_files_scm(ctx); | |
3725 | if (ret) | |
3726 | io_sqe_files_unregister(ctx); | |
3727 | ||
3728 | return ret; | |
3729 | } | |
3730 | ||
c3a31e60 JA |
3731 | static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index) |
3732 | { | |
3733 | #if defined(CONFIG_UNIX) | |
65e19f54 | 3734 | struct file *file = io_file_from_index(ctx, index); |
c3a31e60 JA |
3735 | struct sock *sock = ctx->ring_sock->sk; |
3736 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
3737 | struct sk_buff *skb; | |
3738 | int i; | |
3739 | ||
3740 | __skb_queue_head_init(&list); | |
3741 | ||
3742 | /* | |
3743 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
3744 | * remove this entry and rearrange the file array. | |
3745 | */ | |
3746 | skb = skb_dequeue(head); | |
3747 | while (skb) { | |
3748 | struct scm_fp_list *fp; | |
3749 | ||
3750 | fp = UNIXCB(skb).fp; | |
3751 | for (i = 0; i < fp->count; i++) { | |
3752 | int left; | |
3753 | ||
3754 | if (fp->fp[i] != file) | |
3755 | continue; | |
3756 | ||
3757 | unix_notinflight(fp->user, fp->fp[i]); | |
3758 | left = fp->count - 1 - i; | |
3759 | if (left) { | |
3760 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
3761 | left * sizeof(struct file *)); | |
3762 | } | |
3763 | fp->count--; | |
3764 | if (!fp->count) { | |
3765 | kfree_skb(skb); | |
3766 | skb = NULL; | |
3767 | } else { | |
3768 | __skb_queue_tail(&list, skb); | |
3769 | } | |
3770 | fput(file); | |
3771 | file = NULL; | |
3772 | break; | |
3773 | } | |
3774 | ||
3775 | if (!file) | |
3776 | break; | |
3777 | ||
3778 | __skb_queue_tail(&list, skb); | |
3779 | ||
3780 | skb = skb_dequeue(head); | |
3781 | } | |
3782 | ||
3783 | if (skb_peek(&list)) { | |
3784 | spin_lock_irq(&head->lock); | |
3785 | while ((skb = __skb_dequeue(&list)) != NULL) | |
3786 | __skb_queue_tail(head, skb); | |
3787 | spin_unlock_irq(&head->lock); | |
3788 | } | |
3789 | #else | |
65e19f54 | 3790 | fput(io_file_from_index(ctx, index)); |
c3a31e60 JA |
3791 | #endif |
3792 | } | |
3793 | ||
3794 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, | |
3795 | int index) | |
3796 | { | |
3797 | #if defined(CONFIG_UNIX) | |
3798 | struct sock *sock = ctx->ring_sock->sk; | |
3799 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
3800 | struct sk_buff *skb; | |
3801 | ||
3802 | /* | |
3803 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
3804 | * file set. If there's no room, fall back to allocating a new skb | |
3805 | * and filling it in. | |
3806 | */ | |
3807 | spin_lock_irq(&head->lock); | |
3808 | skb = skb_peek(head); | |
3809 | if (skb) { | |
3810 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
3811 | ||
3812 | if (fpl->count < SCM_MAX_FD) { | |
3813 | __skb_unlink(skb, head); | |
3814 | spin_unlock_irq(&head->lock); | |
3815 | fpl->fp[fpl->count] = get_file(file); | |
3816 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
3817 | fpl->count++; | |
3818 | spin_lock_irq(&head->lock); | |
3819 | __skb_queue_head(head, skb); | |
3820 | } else { | |
3821 | skb = NULL; | |
3822 | } | |
3823 | } | |
3824 | spin_unlock_irq(&head->lock); | |
3825 | ||
3826 | if (skb) { | |
3827 | fput(file); | |
3828 | return 0; | |
3829 | } | |
3830 | ||
3831 | return __io_sqe_files_scm(ctx, 1, index); | |
3832 | #else | |
3833 | return 0; | |
3834 | #endif | |
3835 | } | |
3836 | ||
3837 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, | |
3838 | unsigned nr_args) | |
3839 | { | |
3840 | struct io_uring_files_update up; | |
3841 | __s32 __user *fds; | |
3842 | int fd, i, err; | |
3843 | __u32 done; | |
3844 | ||
65e19f54 | 3845 | if (!ctx->file_table) |
c3a31e60 JA |
3846 | return -ENXIO; |
3847 | if (!nr_args) | |
3848 | return -EINVAL; | |
3849 | if (copy_from_user(&up, arg, sizeof(up))) | |
3850 | return -EFAULT; | |
3851 | if (check_add_overflow(up.offset, nr_args, &done)) | |
3852 | return -EOVERFLOW; | |
3853 | if (done > ctx->nr_user_files) | |
3854 | return -EINVAL; | |
3855 | ||
3856 | done = 0; | |
3857 | fds = (__s32 __user *) up.fds; | |
3858 | while (nr_args) { | |
65e19f54 JA |
3859 | struct fixed_file_table *table; |
3860 | unsigned index; | |
3861 | ||
c3a31e60 JA |
3862 | err = 0; |
3863 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
3864 | err = -EFAULT; | |
3865 | break; | |
3866 | } | |
3867 | i = array_index_nospec(up.offset, ctx->nr_user_files); | |
65e19f54 JA |
3868 | table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT]; |
3869 | index = i & IORING_FILE_TABLE_MASK; | |
3870 | if (table->files[index]) { | |
c3a31e60 | 3871 | io_sqe_file_unregister(ctx, i); |
65e19f54 | 3872 | table->files[index] = NULL; |
c3a31e60 JA |
3873 | } |
3874 | if (fd != -1) { | |
3875 | struct file *file; | |
3876 | ||
3877 | file = fget(fd); | |
3878 | if (!file) { | |
3879 | err = -EBADF; | |
3880 | break; | |
3881 | } | |
3882 | /* | |
3883 | * Don't allow io_uring instances to be registered. If | |
3884 | * UNIX isn't enabled, then this causes a reference | |
3885 | * cycle and this instance can never get freed. If UNIX | |
3886 | * is enabled we'll handle it just fine, but there's | |
3887 | * still no point in allowing a ring fd as it doesn't | |
3888 | * support regular read/write anyway. | |
3889 | */ | |
3890 | if (file->f_op == &io_uring_fops) { | |
3891 | fput(file); | |
3892 | err = -EBADF; | |
3893 | break; | |
3894 | } | |
65e19f54 | 3895 | table->files[index] = file; |
c3a31e60 JA |
3896 | err = io_sqe_file_register(ctx, file, i); |
3897 | if (err) | |
3898 | break; | |
3899 | } | |
3900 | nr_args--; | |
3901 | done++; | |
3902 | up.offset++; | |
3903 | } | |
3904 | ||
3905 | return done ? done : err; | |
3906 | } | |
3907 | ||
7d723065 JA |
3908 | static void io_put_work(struct io_wq_work *work) |
3909 | { | |
3910 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
3911 | ||
3912 | io_put_req(req); | |
3913 | } | |
3914 | ||
3915 | static void io_get_work(struct io_wq_work *work) | |
3916 | { | |
3917 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
3918 | ||
3919 | refcount_inc(&req->refs); | |
3920 | } | |
3921 | ||
6c271ce2 JA |
3922 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
3923 | struct io_uring_params *p) | |
2b188cc1 | 3924 | { |
561fb04a | 3925 | unsigned concurrency; |
2b188cc1 JA |
3926 | int ret; |
3927 | ||
6c271ce2 | 3928 | init_waitqueue_head(&ctx->sqo_wait); |
2b188cc1 JA |
3929 | mmgrab(current->mm); |
3930 | ctx->sqo_mm = current->mm; | |
3931 | ||
6c271ce2 | 3932 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
3ec482d1 JA |
3933 | ret = -EPERM; |
3934 | if (!capable(CAP_SYS_ADMIN)) | |
3935 | goto err; | |
3936 | ||
917257da JA |
3937 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
3938 | if (!ctx->sq_thread_idle) | |
3939 | ctx->sq_thread_idle = HZ; | |
3940 | ||
6c271ce2 | 3941 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 3942 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 3943 | |
917257da | 3944 | ret = -EINVAL; |
44a9bd18 JA |
3945 | if (cpu >= nr_cpu_ids) |
3946 | goto err; | |
7889f44d | 3947 | if (!cpu_online(cpu)) |
917257da JA |
3948 | goto err; |
3949 | ||
6c271ce2 JA |
3950 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
3951 | ctx, cpu, | |
3952 | "io_uring-sq"); | |
3953 | } else { | |
3954 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, | |
3955 | "io_uring-sq"); | |
3956 | } | |
3957 | if (IS_ERR(ctx->sqo_thread)) { | |
3958 | ret = PTR_ERR(ctx->sqo_thread); | |
3959 | ctx->sqo_thread = NULL; | |
3960 | goto err; | |
3961 | } | |
3962 | wake_up_process(ctx->sqo_thread); | |
3963 | } else if (p->flags & IORING_SETUP_SQ_AFF) { | |
3964 | /* Can't have SQ_AFF without SQPOLL */ | |
3965 | ret = -EINVAL; | |
3966 | goto err; | |
3967 | } | |
3968 | ||
561fb04a JA |
3969 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
3970 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
7d723065 JA |
3971 | ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user, |
3972 | io_get_work, io_put_work); | |
975c99a5 JA |
3973 | if (IS_ERR(ctx->io_wq)) { |
3974 | ret = PTR_ERR(ctx->io_wq); | |
3975 | ctx->io_wq = NULL; | |
2b188cc1 JA |
3976 | goto err; |
3977 | } | |
3978 | ||
3979 | return 0; | |
3980 | err: | |
54a91f3b | 3981 | io_finish_async(ctx); |
2b188cc1 JA |
3982 | mmdrop(ctx->sqo_mm); |
3983 | ctx->sqo_mm = NULL; | |
3984 | return ret; | |
3985 | } | |
3986 | ||
3987 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) | |
3988 | { | |
3989 | atomic_long_sub(nr_pages, &user->locked_vm); | |
3990 | } | |
3991 | ||
3992 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) | |
3993 | { | |
3994 | unsigned long page_limit, cur_pages, new_pages; | |
3995 | ||
3996 | /* Don't allow more pages than we can safely lock */ | |
3997 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
3998 | ||
3999 | do { | |
4000 | cur_pages = atomic_long_read(&user->locked_vm); | |
4001 | new_pages = cur_pages + nr_pages; | |
4002 | if (new_pages > page_limit) | |
4003 | return -ENOMEM; | |
4004 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
4005 | new_pages) != cur_pages); | |
4006 | ||
4007 | return 0; | |
4008 | } | |
4009 | ||
4010 | static void io_mem_free(void *ptr) | |
4011 | { | |
52e04ef4 MR |
4012 | struct page *page; |
4013 | ||
4014 | if (!ptr) | |
4015 | return; | |
2b188cc1 | 4016 | |
52e04ef4 | 4017 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
4018 | if (put_page_testzero(page)) |
4019 | free_compound_page(page); | |
4020 | } | |
4021 | ||
4022 | static void *io_mem_alloc(size_t size) | |
4023 | { | |
4024 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
4025 | __GFP_NORETRY; | |
4026 | ||
4027 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
4028 | } | |
4029 | ||
75b28aff HV |
4030 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
4031 | size_t *sq_offset) | |
4032 | { | |
4033 | struct io_rings *rings; | |
4034 | size_t off, sq_array_size; | |
4035 | ||
4036 | off = struct_size(rings, cqes, cq_entries); | |
4037 | if (off == SIZE_MAX) | |
4038 | return SIZE_MAX; | |
4039 | ||
4040 | #ifdef CONFIG_SMP | |
4041 | off = ALIGN(off, SMP_CACHE_BYTES); | |
4042 | if (off == 0) | |
4043 | return SIZE_MAX; | |
4044 | #endif | |
4045 | ||
4046 | sq_array_size = array_size(sizeof(u32), sq_entries); | |
4047 | if (sq_array_size == SIZE_MAX) | |
4048 | return SIZE_MAX; | |
4049 | ||
4050 | if (check_add_overflow(off, sq_array_size, &off)) | |
4051 | return SIZE_MAX; | |
4052 | ||
4053 | if (sq_offset) | |
4054 | *sq_offset = off; | |
4055 | ||
4056 | return off; | |
4057 | } | |
4058 | ||
2b188cc1 JA |
4059 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
4060 | { | |
75b28aff | 4061 | size_t pages; |
2b188cc1 | 4062 | |
75b28aff HV |
4063 | pages = (size_t)1 << get_order( |
4064 | rings_size(sq_entries, cq_entries, NULL)); | |
4065 | pages += (size_t)1 << get_order( | |
4066 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 4067 | |
75b28aff | 4068 | return pages; |
2b188cc1 JA |
4069 | } |
4070 | ||
edafccee JA |
4071 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
4072 | { | |
4073 | int i, j; | |
4074 | ||
4075 | if (!ctx->user_bufs) | |
4076 | return -ENXIO; | |
4077 | ||
4078 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
4079 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
4080 | ||
4081 | for (j = 0; j < imu->nr_bvecs; j++) | |
27c4d3a3 | 4082 | put_user_page(imu->bvec[j].bv_page); |
edafccee JA |
4083 | |
4084 | if (ctx->account_mem) | |
4085 | io_unaccount_mem(ctx->user, imu->nr_bvecs); | |
d4ef6475 | 4086 | kvfree(imu->bvec); |
edafccee JA |
4087 | imu->nr_bvecs = 0; |
4088 | } | |
4089 | ||
4090 | kfree(ctx->user_bufs); | |
4091 | ctx->user_bufs = NULL; | |
4092 | ctx->nr_user_bufs = 0; | |
4093 | return 0; | |
4094 | } | |
4095 | ||
4096 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
4097 | void __user *arg, unsigned index) | |
4098 | { | |
4099 | struct iovec __user *src; | |
4100 | ||
4101 | #ifdef CONFIG_COMPAT | |
4102 | if (ctx->compat) { | |
4103 | struct compat_iovec __user *ciovs; | |
4104 | struct compat_iovec ciov; | |
4105 | ||
4106 | ciovs = (struct compat_iovec __user *) arg; | |
4107 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
4108 | return -EFAULT; | |
4109 | ||
4110 | dst->iov_base = (void __user *) (unsigned long) ciov.iov_base; | |
4111 | dst->iov_len = ciov.iov_len; | |
4112 | return 0; | |
4113 | } | |
4114 | #endif | |
4115 | src = (struct iovec __user *) arg; | |
4116 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
4117 | return -EFAULT; | |
4118 | return 0; | |
4119 | } | |
4120 | ||
4121 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, | |
4122 | unsigned nr_args) | |
4123 | { | |
4124 | struct vm_area_struct **vmas = NULL; | |
4125 | struct page **pages = NULL; | |
4126 | int i, j, got_pages = 0; | |
4127 | int ret = -EINVAL; | |
4128 | ||
4129 | if (ctx->user_bufs) | |
4130 | return -EBUSY; | |
4131 | if (!nr_args || nr_args > UIO_MAXIOV) | |
4132 | return -EINVAL; | |
4133 | ||
4134 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
4135 | GFP_KERNEL); | |
4136 | if (!ctx->user_bufs) | |
4137 | return -ENOMEM; | |
4138 | ||
4139 | for (i = 0; i < nr_args; i++) { | |
4140 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
4141 | unsigned long off, start, end, ubuf; | |
4142 | int pret, nr_pages; | |
4143 | struct iovec iov; | |
4144 | size_t size; | |
4145 | ||
4146 | ret = io_copy_iov(ctx, &iov, arg, i); | |
4147 | if (ret) | |
a278682d | 4148 | goto err; |
edafccee JA |
4149 | |
4150 | /* | |
4151 | * Don't impose further limits on the size and buffer | |
4152 | * constraints here, we'll -EINVAL later when IO is | |
4153 | * submitted if they are wrong. | |
4154 | */ | |
4155 | ret = -EFAULT; | |
4156 | if (!iov.iov_base || !iov.iov_len) | |
4157 | goto err; | |
4158 | ||
4159 | /* arbitrary limit, but we need something */ | |
4160 | if (iov.iov_len > SZ_1G) | |
4161 | goto err; | |
4162 | ||
4163 | ubuf = (unsigned long) iov.iov_base; | |
4164 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
4165 | start = ubuf >> PAGE_SHIFT; | |
4166 | nr_pages = end - start; | |
4167 | ||
4168 | if (ctx->account_mem) { | |
4169 | ret = io_account_mem(ctx->user, nr_pages); | |
4170 | if (ret) | |
4171 | goto err; | |
4172 | } | |
4173 | ||
4174 | ret = 0; | |
4175 | if (!pages || nr_pages > got_pages) { | |
4176 | kfree(vmas); | |
4177 | kfree(pages); | |
d4ef6475 | 4178 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 4179 | GFP_KERNEL); |
d4ef6475 | 4180 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
4181 | sizeof(struct vm_area_struct *), |
4182 | GFP_KERNEL); | |
4183 | if (!pages || !vmas) { | |
4184 | ret = -ENOMEM; | |
4185 | if (ctx->account_mem) | |
4186 | io_unaccount_mem(ctx->user, nr_pages); | |
4187 | goto err; | |
4188 | } | |
4189 | got_pages = nr_pages; | |
4190 | } | |
4191 | ||
d4ef6475 | 4192 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
4193 | GFP_KERNEL); |
4194 | ret = -ENOMEM; | |
4195 | if (!imu->bvec) { | |
4196 | if (ctx->account_mem) | |
4197 | io_unaccount_mem(ctx->user, nr_pages); | |
4198 | goto err; | |
4199 | } | |
4200 | ||
4201 | ret = 0; | |
4202 | down_read(¤t->mm->mmap_sem); | |
932f4a63 IW |
4203 | pret = get_user_pages(ubuf, nr_pages, |
4204 | FOLL_WRITE | FOLL_LONGTERM, | |
4205 | pages, vmas); | |
edafccee JA |
4206 | if (pret == nr_pages) { |
4207 | /* don't support file backed memory */ | |
4208 | for (j = 0; j < nr_pages; j++) { | |
4209 | struct vm_area_struct *vma = vmas[j]; | |
4210 | ||
4211 | if (vma->vm_file && | |
4212 | !is_file_hugepages(vma->vm_file)) { | |
4213 | ret = -EOPNOTSUPP; | |
4214 | break; | |
4215 | } | |
4216 | } | |
4217 | } else { | |
4218 | ret = pret < 0 ? pret : -EFAULT; | |
4219 | } | |
4220 | up_read(¤t->mm->mmap_sem); | |
4221 | if (ret) { | |
4222 | /* | |
4223 | * if we did partial map, or found file backed vmas, | |
4224 | * release any pages we did get | |
4225 | */ | |
27c4d3a3 JH |
4226 | if (pret > 0) |
4227 | put_user_pages(pages, pret); | |
edafccee JA |
4228 | if (ctx->account_mem) |
4229 | io_unaccount_mem(ctx->user, nr_pages); | |
d4ef6475 | 4230 | kvfree(imu->bvec); |
edafccee JA |
4231 | goto err; |
4232 | } | |
4233 | ||
4234 | off = ubuf & ~PAGE_MASK; | |
4235 | size = iov.iov_len; | |
4236 | for (j = 0; j < nr_pages; j++) { | |
4237 | size_t vec_len; | |
4238 | ||
4239 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
4240 | imu->bvec[j].bv_page = pages[j]; | |
4241 | imu->bvec[j].bv_len = vec_len; | |
4242 | imu->bvec[j].bv_offset = off; | |
4243 | off = 0; | |
4244 | size -= vec_len; | |
4245 | } | |
4246 | /* store original address for later verification */ | |
4247 | imu->ubuf = ubuf; | |
4248 | imu->len = iov.iov_len; | |
4249 | imu->nr_bvecs = nr_pages; | |
4250 | ||
4251 | ctx->nr_user_bufs++; | |
4252 | } | |
d4ef6475 MR |
4253 | kvfree(pages); |
4254 | kvfree(vmas); | |
edafccee JA |
4255 | return 0; |
4256 | err: | |
d4ef6475 MR |
4257 | kvfree(pages); |
4258 | kvfree(vmas); | |
edafccee JA |
4259 | io_sqe_buffer_unregister(ctx); |
4260 | return ret; | |
4261 | } | |
4262 | ||
9b402849 JA |
4263 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
4264 | { | |
4265 | __s32 __user *fds = arg; | |
4266 | int fd; | |
4267 | ||
4268 | if (ctx->cq_ev_fd) | |
4269 | return -EBUSY; | |
4270 | ||
4271 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
4272 | return -EFAULT; | |
4273 | ||
4274 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
4275 | if (IS_ERR(ctx->cq_ev_fd)) { | |
4276 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
4277 | ctx->cq_ev_fd = NULL; | |
4278 | return ret; | |
4279 | } | |
4280 | ||
4281 | return 0; | |
4282 | } | |
4283 | ||
4284 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
4285 | { | |
4286 | if (ctx->cq_ev_fd) { | |
4287 | eventfd_ctx_put(ctx->cq_ev_fd); | |
4288 | ctx->cq_ev_fd = NULL; | |
4289 | return 0; | |
4290 | } | |
4291 | ||
4292 | return -ENXIO; | |
4293 | } | |
4294 | ||
2b188cc1 JA |
4295 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
4296 | { | |
6b06314c | 4297 | io_finish_async(ctx); |
2b188cc1 JA |
4298 | if (ctx->sqo_mm) |
4299 | mmdrop(ctx->sqo_mm); | |
def596e9 JA |
4300 | |
4301 | io_iopoll_reap_events(ctx); | |
edafccee | 4302 | io_sqe_buffer_unregister(ctx); |
6b06314c | 4303 | io_sqe_files_unregister(ctx); |
9b402849 | 4304 | io_eventfd_unregister(ctx); |
def596e9 | 4305 | |
2b188cc1 | 4306 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
4307 | if (ctx->ring_sock) { |
4308 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 4309 | sock_release(ctx->ring_sock); |
355e8d26 | 4310 | } |
2b188cc1 JA |
4311 | #endif |
4312 | ||
75b28aff | 4313 | io_mem_free(ctx->rings); |
2b188cc1 | 4314 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
4315 | |
4316 | percpu_ref_exit(&ctx->refs); | |
4317 | if (ctx->account_mem) | |
4318 | io_unaccount_mem(ctx->user, | |
4319 | ring_pages(ctx->sq_entries, ctx->cq_entries)); | |
4320 | free_uid(ctx->user); | |
206aefde | 4321 | kfree(ctx->completions); |
0ddf92e8 | 4322 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
4323 | kfree(ctx); |
4324 | } | |
4325 | ||
4326 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
4327 | { | |
4328 | struct io_ring_ctx *ctx = file->private_data; | |
4329 | __poll_t mask = 0; | |
4330 | ||
4331 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
4332 | /* |
4333 | * synchronizes with barrier from wq_has_sleeper call in | |
4334 | * io_commit_cqring | |
4335 | */ | |
2b188cc1 | 4336 | smp_rmb(); |
75b28aff HV |
4337 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
4338 | ctx->rings->sq_ring_entries) | |
2b188cc1 | 4339 | mask |= EPOLLOUT | EPOLLWRNORM; |
daa5de54 | 4340 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
2b188cc1 JA |
4341 | mask |= EPOLLIN | EPOLLRDNORM; |
4342 | ||
4343 | return mask; | |
4344 | } | |
4345 | ||
4346 | static int io_uring_fasync(int fd, struct file *file, int on) | |
4347 | { | |
4348 | struct io_ring_ctx *ctx = file->private_data; | |
4349 | ||
4350 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
4351 | } | |
4352 | ||
4353 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) | |
4354 | { | |
4355 | mutex_lock(&ctx->uring_lock); | |
4356 | percpu_ref_kill(&ctx->refs); | |
4357 | mutex_unlock(&ctx->uring_lock); | |
4358 | ||
5262f567 | 4359 | io_kill_timeouts(ctx); |
221c5eb2 | 4360 | io_poll_remove_all(ctx); |
561fb04a JA |
4361 | |
4362 | if (ctx->io_wq) | |
4363 | io_wq_cancel_all(ctx->io_wq); | |
4364 | ||
def596e9 | 4365 | io_iopoll_reap_events(ctx); |
15dff286 JA |
4366 | /* if we failed setting up the ctx, we might not have any rings */ |
4367 | if (ctx->rings) | |
4368 | io_cqring_overflow_flush(ctx, true); | |
206aefde | 4369 | wait_for_completion(&ctx->completions[0]); |
2b188cc1 JA |
4370 | io_ring_ctx_free(ctx); |
4371 | } | |
4372 | ||
4373 | static int io_uring_release(struct inode *inode, struct file *file) | |
4374 | { | |
4375 | struct io_ring_ctx *ctx = file->private_data; | |
4376 | ||
4377 | file->private_data = NULL; | |
4378 | io_ring_ctx_wait_and_kill(ctx); | |
4379 | return 0; | |
4380 | } | |
4381 | ||
fcb323cc JA |
4382 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
4383 | struct files_struct *files) | |
4384 | { | |
4385 | struct io_kiocb *req; | |
4386 | DEFINE_WAIT(wait); | |
4387 | ||
4388 | while (!list_empty_careful(&ctx->inflight_list)) { | |
768134d4 | 4389 | struct io_kiocb *cancel_req = NULL; |
fcb323cc JA |
4390 | |
4391 | spin_lock_irq(&ctx->inflight_lock); | |
4392 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
768134d4 JA |
4393 | if (req->work.files != files) |
4394 | continue; | |
4395 | /* req is being completed, ignore */ | |
4396 | if (!refcount_inc_not_zero(&req->refs)) | |
4397 | continue; | |
4398 | cancel_req = req; | |
4399 | break; | |
fcb323cc | 4400 | } |
768134d4 | 4401 | if (cancel_req) |
fcb323cc | 4402 | prepare_to_wait(&ctx->inflight_wait, &wait, |
768134d4 | 4403 | TASK_UNINTERRUPTIBLE); |
fcb323cc JA |
4404 | spin_unlock_irq(&ctx->inflight_lock); |
4405 | ||
768134d4 JA |
4406 | /* We need to keep going until we don't find a matching req */ |
4407 | if (!cancel_req) | |
fcb323cc | 4408 | break; |
2f6d9b9d BL |
4409 | |
4410 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); | |
4411 | io_put_req(cancel_req); | |
fcb323cc JA |
4412 | schedule(); |
4413 | } | |
768134d4 | 4414 | finish_wait(&ctx->inflight_wait, &wait); |
fcb323cc JA |
4415 | } |
4416 | ||
4417 | static int io_uring_flush(struct file *file, void *data) | |
4418 | { | |
4419 | struct io_ring_ctx *ctx = file->private_data; | |
4420 | ||
4421 | io_uring_cancel_files(ctx, data); | |
1d7bb1d5 JA |
4422 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
4423 | io_cqring_overflow_flush(ctx, true); | |
fcb323cc | 4424 | io_wq_cancel_all(ctx->io_wq); |
1d7bb1d5 | 4425 | } |
fcb323cc JA |
4426 | return 0; |
4427 | } | |
4428 | ||
2b188cc1 JA |
4429 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
4430 | { | |
4431 | loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT; | |
4432 | unsigned long sz = vma->vm_end - vma->vm_start; | |
4433 | struct io_ring_ctx *ctx = file->private_data; | |
4434 | unsigned long pfn; | |
4435 | struct page *page; | |
4436 | void *ptr; | |
4437 | ||
4438 | switch (offset) { | |
4439 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
4440 | case IORING_OFF_CQ_RING: |
4441 | ptr = ctx->rings; | |
2b188cc1 JA |
4442 | break; |
4443 | case IORING_OFF_SQES: | |
4444 | ptr = ctx->sq_sqes; | |
4445 | break; | |
2b188cc1 JA |
4446 | default: |
4447 | return -EINVAL; | |
4448 | } | |
4449 | ||
4450 | page = virt_to_head_page(ptr); | |
a50b854e | 4451 | if (sz > page_size(page)) |
2b188cc1 JA |
4452 | return -EINVAL; |
4453 | ||
4454 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
4455 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
4456 | } | |
4457 | ||
4458 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, | |
4459 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
4460 | size_t, sigsz) | |
4461 | { | |
4462 | struct io_ring_ctx *ctx; | |
4463 | long ret = -EBADF; | |
4464 | int submitted = 0; | |
4465 | struct fd f; | |
4466 | ||
6c271ce2 | 4467 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
2b188cc1 JA |
4468 | return -EINVAL; |
4469 | ||
4470 | f = fdget(fd); | |
4471 | if (!f.file) | |
4472 | return -EBADF; | |
4473 | ||
4474 | ret = -EOPNOTSUPP; | |
4475 | if (f.file->f_op != &io_uring_fops) | |
4476 | goto out_fput; | |
4477 | ||
4478 | ret = -ENXIO; | |
4479 | ctx = f.file->private_data; | |
4480 | if (!percpu_ref_tryget(&ctx->refs)) | |
4481 | goto out_fput; | |
4482 | ||
6c271ce2 JA |
4483 | /* |
4484 | * For SQ polling, the thread will do all submissions and completions. | |
4485 | * Just return the requested submit count, and wake the thread if | |
4486 | * we were asked to. | |
4487 | */ | |
b2a9eada | 4488 | ret = 0; |
6c271ce2 | 4489 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c1edbf5f JA |
4490 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
4491 | io_cqring_overflow_flush(ctx, false); | |
6c271ce2 JA |
4492 | if (flags & IORING_ENTER_SQ_WAKEUP) |
4493 | wake_up(&ctx->sqo_wait); | |
4494 | submitted = to_submit; | |
b2a9eada | 4495 | } else if (to_submit) { |
ae9428ca | 4496 | struct mm_struct *cur_mm; |
2b188cc1 | 4497 | |
ae9428ca | 4498 | to_submit = min(to_submit, ctx->sq_entries); |
2b188cc1 | 4499 | mutex_lock(&ctx->uring_lock); |
ae9428ca PB |
4500 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
4501 | cur_mm = ctx->sqo_mm; | |
4502 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, | |
4503 | &cur_mm, false); | |
2b188cc1 | 4504 | mutex_unlock(&ctx->uring_lock); |
2b188cc1 JA |
4505 | } |
4506 | if (flags & IORING_ENTER_GETEVENTS) { | |
def596e9 JA |
4507 | unsigned nr_events = 0; |
4508 | ||
2b188cc1 JA |
4509 | min_complete = min(min_complete, ctx->cq_entries); |
4510 | ||
def596e9 | 4511 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 | 4512 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
def596e9 JA |
4513 | } else { |
4514 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
4515 | } | |
2b188cc1 JA |
4516 | } |
4517 | ||
6805b32e | 4518 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
4519 | out_fput: |
4520 | fdput(f); | |
4521 | return submitted ? submitted : ret; | |
4522 | } | |
4523 | ||
4524 | static const struct file_operations io_uring_fops = { | |
4525 | .release = io_uring_release, | |
fcb323cc | 4526 | .flush = io_uring_flush, |
2b188cc1 JA |
4527 | .mmap = io_uring_mmap, |
4528 | .poll = io_uring_poll, | |
4529 | .fasync = io_uring_fasync, | |
4530 | }; | |
4531 | ||
4532 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
4533 | struct io_uring_params *p) | |
4534 | { | |
75b28aff HV |
4535 | struct io_rings *rings; |
4536 | size_t size, sq_array_offset; | |
2b188cc1 | 4537 | |
75b28aff HV |
4538 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
4539 | if (size == SIZE_MAX) | |
4540 | return -EOVERFLOW; | |
4541 | ||
4542 | rings = io_mem_alloc(size); | |
4543 | if (!rings) | |
2b188cc1 JA |
4544 | return -ENOMEM; |
4545 | ||
75b28aff HV |
4546 | ctx->rings = rings; |
4547 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
4548 | rings->sq_ring_mask = p->sq_entries - 1; | |
4549 | rings->cq_ring_mask = p->cq_entries - 1; | |
4550 | rings->sq_ring_entries = p->sq_entries; | |
4551 | rings->cq_ring_entries = p->cq_entries; | |
4552 | ctx->sq_mask = rings->sq_ring_mask; | |
4553 | ctx->cq_mask = rings->cq_ring_mask; | |
4554 | ctx->sq_entries = rings->sq_ring_entries; | |
4555 | ctx->cq_entries = rings->cq_ring_entries; | |
2b188cc1 JA |
4556 | |
4557 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
4558 | if (size == SIZE_MAX) { |
4559 | io_mem_free(ctx->rings); | |
4560 | ctx->rings = NULL; | |
2b188cc1 | 4561 | return -EOVERFLOW; |
eb065d30 | 4562 | } |
2b188cc1 JA |
4563 | |
4564 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
4565 | if (!ctx->sq_sqes) { |
4566 | io_mem_free(ctx->rings); | |
4567 | ctx->rings = NULL; | |
2b188cc1 | 4568 | return -ENOMEM; |
eb065d30 | 4569 | } |
2b188cc1 | 4570 | |
2b188cc1 JA |
4571 | return 0; |
4572 | } | |
4573 | ||
4574 | /* | |
4575 | * Allocate an anonymous fd, this is what constitutes the application | |
4576 | * visible backing of an io_uring instance. The application mmaps this | |
4577 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
4578 | * we have to tie this fd to a socket for file garbage collection purposes. | |
4579 | */ | |
4580 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
4581 | { | |
4582 | struct file *file; | |
4583 | int ret; | |
4584 | ||
4585 | #if defined(CONFIG_UNIX) | |
4586 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
4587 | &ctx->ring_sock); | |
4588 | if (ret) | |
4589 | return ret; | |
4590 | #endif | |
4591 | ||
4592 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
4593 | if (ret < 0) | |
4594 | goto err; | |
4595 | ||
4596 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
4597 | O_RDWR | O_CLOEXEC); | |
4598 | if (IS_ERR(file)) { | |
4599 | put_unused_fd(ret); | |
4600 | ret = PTR_ERR(file); | |
4601 | goto err; | |
4602 | } | |
4603 | ||
4604 | #if defined(CONFIG_UNIX) | |
4605 | ctx->ring_sock->file = file; | |
6b06314c | 4606 | ctx->ring_sock->sk->sk_user_data = ctx; |
2b188cc1 JA |
4607 | #endif |
4608 | fd_install(ret, file); | |
4609 | return ret; | |
4610 | err: | |
4611 | #if defined(CONFIG_UNIX) | |
4612 | sock_release(ctx->ring_sock); | |
4613 | ctx->ring_sock = NULL; | |
4614 | #endif | |
4615 | return ret; | |
4616 | } | |
4617 | ||
4618 | static int io_uring_create(unsigned entries, struct io_uring_params *p) | |
4619 | { | |
4620 | struct user_struct *user = NULL; | |
4621 | struct io_ring_ctx *ctx; | |
4622 | bool account_mem; | |
4623 | int ret; | |
4624 | ||
4625 | if (!entries || entries > IORING_MAX_ENTRIES) | |
4626 | return -EINVAL; | |
4627 | ||
4628 | /* | |
4629 | * Use twice as many entries for the CQ ring. It's possible for the | |
4630 | * application to drive a higher depth than the size of the SQ ring, | |
4631 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
4632 | * some flexibility in overcommitting a bit. If the application has |
4633 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
4634 | * of CQ ring entries manually. | |
2b188cc1 JA |
4635 | */ |
4636 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
4637 | if (p->flags & IORING_SETUP_CQSIZE) { |
4638 | /* | |
4639 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
4640 | * to a power-of-two, if it isn't already. We do NOT impose | |
4641 | * any cq vs sq ring sizing. | |
4642 | */ | |
4643 | if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES) | |
4644 | return -EINVAL; | |
4645 | p->cq_entries = roundup_pow_of_two(p->cq_entries); | |
4646 | } else { | |
4647 | p->cq_entries = 2 * p->sq_entries; | |
4648 | } | |
2b188cc1 JA |
4649 | |
4650 | user = get_uid(current_user()); | |
4651 | account_mem = !capable(CAP_IPC_LOCK); | |
4652 | ||
4653 | if (account_mem) { | |
4654 | ret = io_account_mem(user, | |
4655 | ring_pages(p->sq_entries, p->cq_entries)); | |
4656 | if (ret) { | |
4657 | free_uid(user); | |
4658 | return ret; | |
4659 | } | |
4660 | } | |
4661 | ||
4662 | ctx = io_ring_ctx_alloc(p); | |
4663 | if (!ctx) { | |
4664 | if (account_mem) | |
4665 | io_unaccount_mem(user, ring_pages(p->sq_entries, | |
4666 | p->cq_entries)); | |
4667 | free_uid(user); | |
4668 | return -ENOMEM; | |
4669 | } | |
4670 | ctx->compat = in_compat_syscall(); | |
4671 | ctx->account_mem = account_mem; | |
4672 | ctx->user = user; | |
4673 | ||
4674 | ret = io_allocate_scq_urings(ctx, p); | |
4675 | if (ret) | |
4676 | goto err; | |
4677 | ||
6c271ce2 | 4678 | ret = io_sq_offload_start(ctx, p); |
2b188cc1 JA |
4679 | if (ret) |
4680 | goto err; | |
4681 | ||
2b188cc1 | 4682 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
4683 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
4684 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
4685 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
4686 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
4687 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
4688 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
4689 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
4690 | |
4691 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
4692 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
4693 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
4694 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
4695 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
4696 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
4697 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
ac90f249 | 4698 | |
044c1ab3 JA |
4699 | /* |
4700 | * Install ring fd as the very last thing, so we don't risk someone | |
4701 | * having closed it before we finish setup | |
4702 | */ | |
4703 | ret = io_uring_get_fd(ctx); | |
4704 | if (ret < 0) | |
4705 | goto err; | |
4706 | ||
1d7bb1d5 | 4707 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP; |
c826bd7a | 4708 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
4709 | return ret; |
4710 | err: | |
4711 | io_ring_ctx_wait_and_kill(ctx); | |
4712 | return ret; | |
4713 | } | |
4714 | ||
4715 | /* | |
4716 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
4717 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
4718 | * params structure passed in. | |
4719 | */ | |
4720 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
4721 | { | |
4722 | struct io_uring_params p; | |
4723 | long ret; | |
4724 | int i; | |
4725 | ||
4726 | if (copy_from_user(&p, params, sizeof(p))) | |
4727 | return -EFAULT; | |
4728 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
4729 | if (p.resv[i]) | |
4730 | return -EINVAL; | |
4731 | } | |
4732 | ||
6c271ce2 | 4733 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
33a107f0 | 4734 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE)) |
2b188cc1 JA |
4735 | return -EINVAL; |
4736 | ||
4737 | ret = io_uring_create(entries, &p); | |
4738 | if (ret < 0) | |
4739 | return ret; | |
4740 | ||
4741 | if (copy_to_user(params, &p, sizeof(p))) | |
4742 | return -EFAULT; | |
4743 | ||
4744 | return ret; | |
4745 | } | |
4746 | ||
4747 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
4748 | struct io_uring_params __user *, params) | |
4749 | { | |
4750 | return io_uring_setup(entries, params); | |
4751 | } | |
4752 | ||
edafccee JA |
4753 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
4754 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
4755 | __releases(ctx->uring_lock) |
4756 | __acquires(ctx->uring_lock) | |
edafccee JA |
4757 | { |
4758 | int ret; | |
4759 | ||
35fa71a0 JA |
4760 | /* |
4761 | * We're inside the ring mutex, if the ref is already dying, then | |
4762 | * someone else killed the ctx or is already going through | |
4763 | * io_uring_register(). | |
4764 | */ | |
4765 | if (percpu_ref_is_dying(&ctx->refs)) | |
4766 | return -ENXIO; | |
4767 | ||
edafccee | 4768 | percpu_ref_kill(&ctx->refs); |
b19062a5 JA |
4769 | |
4770 | /* | |
4771 | * Drop uring mutex before waiting for references to exit. If another | |
4772 | * thread is currently inside io_uring_enter() it might need to grab | |
4773 | * the uring_lock to make progress. If we hold it here across the drain | |
4774 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
4775 | * no new references will come in after we've killed the percpu ref. | |
4776 | */ | |
4777 | mutex_unlock(&ctx->uring_lock); | |
206aefde | 4778 | wait_for_completion(&ctx->completions[0]); |
b19062a5 | 4779 | mutex_lock(&ctx->uring_lock); |
edafccee JA |
4780 | |
4781 | switch (opcode) { | |
4782 | case IORING_REGISTER_BUFFERS: | |
4783 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
4784 | break; | |
4785 | case IORING_UNREGISTER_BUFFERS: | |
4786 | ret = -EINVAL; | |
4787 | if (arg || nr_args) | |
4788 | break; | |
4789 | ret = io_sqe_buffer_unregister(ctx); | |
4790 | break; | |
6b06314c JA |
4791 | case IORING_REGISTER_FILES: |
4792 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
4793 | break; | |
4794 | case IORING_UNREGISTER_FILES: | |
4795 | ret = -EINVAL; | |
4796 | if (arg || nr_args) | |
4797 | break; | |
4798 | ret = io_sqe_files_unregister(ctx); | |
4799 | break; | |
c3a31e60 JA |
4800 | case IORING_REGISTER_FILES_UPDATE: |
4801 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
4802 | break; | |
9b402849 JA |
4803 | case IORING_REGISTER_EVENTFD: |
4804 | ret = -EINVAL; | |
4805 | if (nr_args != 1) | |
4806 | break; | |
4807 | ret = io_eventfd_register(ctx, arg); | |
4808 | break; | |
4809 | case IORING_UNREGISTER_EVENTFD: | |
4810 | ret = -EINVAL; | |
4811 | if (arg || nr_args) | |
4812 | break; | |
4813 | ret = io_eventfd_unregister(ctx); | |
4814 | break; | |
edafccee JA |
4815 | default: |
4816 | ret = -EINVAL; | |
4817 | break; | |
4818 | } | |
4819 | ||
4820 | /* bring the ctx back to life */ | |
206aefde | 4821 | reinit_completion(&ctx->completions[0]); |
edafccee JA |
4822 | percpu_ref_reinit(&ctx->refs); |
4823 | return ret; | |
4824 | } | |
4825 | ||
4826 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
4827 | void __user *, arg, unsigned int, nr_args) | |
4828 | { | |
4829 | struct io_ring_ctx *ctx; | |
4830 | long ret = -EBADF; | |
4831 | struct fd f; | |
4832 | ||
4833 | f = fdget(fd); | |
4834 | if (!f.file) | |
4835 | return -EBADF; | |
4836 | ||
4837 | ret = -EOPNOTSUPP; | |
4838 | if (f.file->f_op != &io_uring_fops) | |
4839 | goto out_fput; | |
4840 | ||
4841 | ctx = f.file->private_data; | |
4842 | ||
4843 | mutex_lock(&ctx->uring_lock); | |
4844 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
4845 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
4846 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
4847 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
4848 | out_fput: |
4849 | fdput(f); | |
4850 | return ret; | |
4851 | } | |
4852 | ||
2b188cc1 JA |
4853 | static int __init io_uring_init(void) |
4854 | { | |
4855 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); | |
4856 | return 0; | |
4857 | }; | |
4858 | __initcall(io_uring_init); |