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