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