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