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