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