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