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