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