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