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> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.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> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
ff002b30 | 77 | #include <linux/fs_struct.h> |
7d67af2c | 78 | #include <linux/splice.h> |
b41e9852 | 79 | #include <linux/task_work.h> |
bcf5a063 | 80 | #include <linux/pagemap.h> |
0f212204 | 81 | #include <linux/io_uring.h> |
91d8f519 | 82 | #include <linux/blk-cgroup.h> |
4ea33a97 | 83 | #include <linux/audit.h> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
95 | |
96 | /* | |
97 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
98 | */ | |
99 | #define IORING_FILE_TABLE_SHIFT 9 | |
100 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
101 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
102 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
103 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
104 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 105 | |
b16fed66 PB |
106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
108 | IOSQE_BUFFER_SELECT) | |
109 | ||
2b188cc1 JA |
110 | struct io_uring { |
111 | u32 head ____cacheline_aligned_in_smp; | |
112 | u32 tail ____cacheline_aligned_in_smp; | |
113 | }; | |
114 | ||
1e84b97b | 115 | /* |
75b28aff HV |
116 | * This data is shared with the application through the mmap at offsets |
117 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
118 | * |
119 | * The offsets to the member fields are published through struct | |
120 | * io_sqring_offsets when calling io_uring_setup. | |
121 | */ | |
75b28aff | 122 | struct io_rings { |
1e84b97b SB |
123 | /* |
124 | * Head and tail offsets into the ring; the offsets need to be | |
125 | * masked to get valid indices. | |
126 | * | |
75b28aff HV |
127 | * The kernel controls head of the sq ring and the tail of the cq ring, |
128 | * and the application controls tail of the sq ring and the head of the | |
129 | * cq ring. | |
1e84b97b | 130 | */ |
75b28aff | 131 | struct io_uring sq, cq; |
1e84b97b | 132 | /* |
75b28aff | 133 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
134 | * ring_entries - 1) |
135 | */ | |
75b28aff HV |
136 | u32 sq_ring_mask, cq_ring_mask; |
137 | /* Ring sizes (constant, power of 2) */ | |
138 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
139 | /* |
140 | * Number of invalid entries dropped by the kernel due to | |
141 | * invalid index stored in array | |
142 | * | |
143 | * Written by the kernel, shouldn't be modified by the | |
144 | * application (i.e. get number of "new events" by comparing to | |
145 | * cached value). | |
146 | * | |
147 | * After a new SQ head value was read by the application this | |
148 | * counter includes all submissions that were dropped reaching | |
149 | * the new SQ head (and possibly more). | |
150 | */ | |
75b28aff | 151 | u32 sq_dropped; |
1e84b97b | 152 | /* |
0d9b5b3a | 153 | * Runtime SQ flags |
1e84b97b SB |
154 | * |
155 | * Written by the kernel, shouldn't be modified by the | |
156 | * application. | |
157 | * | |
158 | * The application needs a full memory barrier before checking | |
159 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
160 | */ | |
75b28aff | 161 | u32 sq_flags; |
0d9b5b3a SG |
162 | /* |
163 | * Runtime CQ flags | |
164 | * | |
165 | * Written by the application, shouldn't be modified by the | |
166 | * kernel. | |
167 | */ | |
168 | u32 cq_flags; | |
1e84b97b SB |
169 | /* |
170 | * Number of completion events lost because the queue was full; | |
171 | * this should be avoided by the application by making sure | |
0b4295b5 | 172 | * there are not more requests pending than there is space in |
1e84b97b SB |
173 | * the completion queue. |
174 | * | |
175 | * Written by the kernel, shouldn't be modified by the | |
176 | * application (i.e. get number of "new events" by comparing to | |
177 | * cached value). | |
178 | * | |
179 | * As completion events come in out of order this counter is not | |
180 | * ordered with any other data. | |
181 | */ | |
75b28aff | 182 | u32 cq_overflow; |
1e84b97b SB |
183 | /* |
184 | * Ring buffer of completion events. | |
185 | * | |
186 | * The kernel writes completion events fresh every time they are | |
187 | * produced, so the application is allowed to modify pending | |
188 | * entries. | |
189 | */ | |
75b28aff | 190 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
191 | }; |
192 | ||
45d189c6 PB |
193 | enum io_uring_cmd_flags { |
194 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 195 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
196 | }; |
197 | ||
edafccee JA |
198 | struct io_mapped_ubuf { |
199 | u64 ubuf; | |
200 | size_t len; | |
201 | struct bio_vec *bvec; | |
202 | unsigned int nr_bvecs; | |
de293938 | 203 | unsigned long acct_pages; |
edafccee JA |
204 | }; |
205 | ||
50238531 BM |
206 | struct io_ring_ctx; |
207 | ||
269bbe5f BM |
208 | struct io_rsrc_put { |
209 | struct list_head list; | |
50238531 BM |
210 | union { |
211 | void *rsrc; | |
212 | struct file *file; | |
213 | }; | |
269bbe5f BM |
214 | }; |
215 | ||
216 | struct fixed_rsrc_table { | |
65e19f54 | 217 | struct file **files; |
31b51510 JA |
218 | }; |
219 | ||
269bbe5f | 220 | struct fixed_rsrc_ref_node { |
05589553 XW |
221 | struct percpu_ref refs; |
222 | struct list_head node; | |
269bbe5f BM |
223 | struct list_head rsrc_list; |
224 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
225 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
226 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 227 | struct llist_node llist; |
e297822b | 228 | bool done; |
05589553 XW |
229 | }; |
230 | ||
269bbe5f BM |
231 | struct fixed_rsrc_data { |
232 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
233 | struct io_ring_ctx *ctx; |
234 | ||
269bbe5f | 235 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 236 | struct percpu_ref refs; |
05f3fb3c | 237 | struct completion done; |
8bad28d8 | 238 | bool quiesce; |
05f3fb3c JA |
239 | }; |
240 | ||
5a2e745d JA |
241 | struct io_buffer { |
242 | struct list_head list; | |
243 | __u64 addr; | |
244 | __s32 len; | |
245 | __u16 bid; | |
246 | }; | |
247 | ||
21b55dbc SG |
248 | struct io_restriction { |
249 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
250 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
251 | u8 sqe_flags_allowed; | |
252 | u8 sqe_flags_required; | |
7e84e1c7 | 253 | bool registered; |
21b55dbc SG |
254 | }; |
255 | ||
37d1e2e3 JA |
256 | enum { |
257 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
258 | IO_SQ_THREAD_SHOULD_PARK, | |
259 | }; | |
260 | ||
534ca6d6 JA |
261 | struct io_sq_data { |
262 | refcount_t refs; | |
69fb2131 JA |
263 | struct mutex lock; |
264 | ||
265 | /* ctx's that are using this sqd */ | |
266 | struct list_head ctx_list; | |
267 | struct list_head ctx_new_list; | |
268 | struct mutex ctx_lock; | |
269 | ||
534ca6d6 JA |
270 | struct task_struct *thread; |
271 | struct wait_queue_head wait; | |
08369246 XW |
272 | |
273 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
274 | int sq_cpu; |
275 | pid_t task_pid; | |
276 | ||
277 | unsigned long state; | |
278 | struct completion startup; | |
279 | struct completion completion; | |
280 | struct completion exited; | |
534ca6d6 JA |
281 | }; |
282 | ||
258b29a9 | 283 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 284 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 285 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 286 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
287 | |
288 | struct io_comp_state { | |
6dd0be1e | 289 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 290 | unsigned int nr; |
c7dae4ba JA |
291 | unsigned int locked_free_nr; |
292 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 293 | struct list_head free_list; |
c7dae4ba JA |
294 | /* IRQ completion list, under ->completion_lock */ |
295 | struct list_head locked_free_list; | |
258b29a9 PB |
296 | }; |
297 | ||
a1ab7b35 PB |
298 | struct io_submit_link { |
299 | struct io_kiocb *head; | |
300 | struct io_kiocb *last; | |
301 | }; | |
302 | ||
258b29a9 PB |
303 | struct io_submit_state { |
304 | struct blk_plug plug; | |
a1ab7b35 | 305 | struct io_submit_link link; |
258b29a9 PB |
306 | |
307 | /* | |
308 | * io_kiocb alloc cache | |
309 | */ | |
bf019da7 | 310 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
311 | unsigned int free_reqs; |
312 | ||
313 | bool plug_started; | |
314 | ||
315 | /* | |
316 | * Batch completion logic | |
317 | */ | |
318 | struct io_comp_state comp; | |
319 | ||
320 | /* | |
321 | * File reference cache | |
322 | */ | |
323 | struct file *file; | |
324 | unsigned int fd; | |
325 | unsigned int file_refs; | |
326 | unsigned int ios_left; | |
327 | }; | |
328 | ||
2b188cc1 JA |
329 | struct io_ring_ctx { |
330 | struct { | |
331 | struct percpu_ref refs; | |
332 | } ____cacheline_aligned_in_smp; | |
333 | ||
334 | struct { | |
335 | unsigned int flags; | |
e1d85334 | 336 | unsigned int compat: 1; |
e1d85334 RD |
337 | unsigned int cq_overflow_flushed: 1; |
338 | unsigned int drain_next: 1; | |
339 | unsigned int eventfd_async: 1; | |
21b55dbc | 340 | unsigned int restricted: 1; |
5f3f26f9 | 341 | unsigned int sqo_exec: 1; |
2b188cc1 | 342 | |
75b28aff HV |
343 | /* |
344 | * Ring buffer of indices into array of io_uring_sqe, which is | |
345 | * mmapped by the application using the IORING_OFF_SQES offset. | |
346 | * | |
347 | * This indirection could e.g. be used to assign fixed | |
348 | * io_uring_sqe entries to operations and only submit them to | |
349 | * the queue when needed. | |
350 | * | |
351 | * The kernel modifies neither the indices array nor the entries | |
352 | * array. | |
353 | */ | |
354 | u32 *sq_array; | |
2b188cc1 JA |
355 | unsigned cached_sq_head; |
356 | unsigned sq_entries; | |
357 | unsigned sq_mask; | |
6c271ce2 | 358 | unsigned sq_thread_idle; |
498ccd9e | 359 | unsigned cached_sq_dropped; |
2c3bac6d | 360 | unsigned cached_cq_overflow; |
ad3eb2c8 | 361 | unsigned long sq_check_overflow; |
de0617e4 | 362 | |
e941894e JA |
363 | /* hashed buffered write serialization */ |
364 | struct io_wq_hash *hash_map; | |
365 | ||
de0617e4 | 366 | struct list_head defer_list; |
5262f567 | 367 | struct list_head timeout_list; |
1d7bb1d5 | 368 | struct list_head cq_overflow_list; |
fcb323cc | 369 | |
ad3eb2c8 | 370 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
371 | } ____cacheline_aligned_in_smp; |
372 | ||
3c1a2ead JA |
373 | struct { |
374 | struct mutex uring_lock; | |
375 | wait_queue_head_t wait; | |
376 | } ____cacheline_aligned_in_smp; | |
377 | ||
378 | struct io_submit_state submit_state; | |
379 | ||
206aefde JA |
380 | struct io_rings *rings; |
381 | ||
2aede0e4 JA |
382 | /* Only used for accounting purposes */ |
383 | struct mm_struct *mm_account; | |
384 | ||
534ca6d6 JA |
385 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
386 | ||
90554200 | 387 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 388 | struct list_head sqd_list; |
75b28aff | 389 | |
6b06314c JA |
390 | /* |
391 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
392 | * readers must ensure that ->refs is alive as long as the file* is | |
393 | * used. Only updated through io_uring_register(2). | |
394 | */ | |
269bbe5f | 395 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
396 | unsigned nr_user_files; |
397 | ||
edafccee JA |
398 | /* if used, fixed mapped user buffers */ |
399 | unsigned nr_user_bufs; | |
400 | struct io_mapped_ubuf *user_bufs; | |
401 | ||
2b188cc1 JA |
402 | struct user_struct *user; |
403 | ||
0f158b4c JA |
404 | struct completion ref_comp; |
405 | struct completion sq_thread_comp; | |
206aefde JA |
406 | |
407 | #if defined(CONFIG_UNIX) | |
408 | struct socket *ring_sock; | |
409 | #endif | |
410 | ||
5a2e745d JA |
411 | struct idr io_buffer_idr; |
412 | ||
071698e1 JA |
413 | struct idr personality_idr; |
414 | ||
206aefde JA |
415 | struct { |
416 | unsigned cached_cq_tail; | |
417 | unsigned cq_entries; | |
418 | unsigned cq_mask; | |
419 | atomic_t cq_timeouts; | |
f010505b | 420 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 421 | unsigned long cq_check_overflow; |
206aefde JA |
422 | struct wait_queue_head cq_wait; |
423 | struct fasync_struct *cq_fasync; | |
424 | struct eventfd_ctx *cq_ev_fd; | |
425 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 426 | |
2b188cc1 JA |
427 | struct { |
428 | spinlock_t completion_lock; | |
e94f141b | 429 | |
def596e9 | 430 | /* |
540e32a0 | 431 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
432 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
433 | * For SQPOLL, only the single threaded io_sq_thread() will | |
434 | * manipulate the list, hence no extra locking is needed there. | |
435 | */ | |
540e32a0 | 436 | struct list_head iopoll_list; |
78076bb6 JA |
437 | struct hlist_head *cancel_hash; |
438 | unsigned cancel_hash_bits; | |
e94f141b | 439 | bool poll_multi_file; |
31b51510 | 440 | |
fcb323cc JA |
441 | spinlock_t inflight_lock; |
442 | struct list_head inflight_list; | |
2b188cc1 | 443 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 444 | |
269bbe5f BM |
445 | struct delayed_work rsrc_put_work; |
446 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
447 | struct list_head rsrc_ref_list; |
448 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 449 | |
21b55dbc | 450 | struct io_restriction restrictions; |
3c1a2ead | 451 | |
7c25c0d1 JA |
452 | /* exit task_work */ |
453 | struct callback_head *exit_task_work; | |
454 | ||
e941894e JA |
455 | struct wait_queue_head hash_wait; |
456 | ||
3c1a2ead JA |
457 | /* Keep this last, we don't need it for the fast path */ |
458 | struct work_struct exit_work; | |
2b188cc1 JA |
459 | }; |
460 | ||
09bb8394 JA |
461 | /* |
462 | * First field must be the file pointer in all the | |
463 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
464 | */ | |
221c5eb2 JA |
465 | struct io_poll_iocb { |
466 | struct file *file; | |
018043be | 467 | struct wait_queue_head *head; |
221c5eb2 | 468 | __poll_t events; |
8c838788 | 469 | bool done; |
221c5eb2 | 470 | bool canceled; |
392edb45 | 471 | struct wait_queue_entry wait; |
221c5eb2 JA |
472 | }; |
473 | ||
018043be PB |
474 | struct io_poll_remove { |
475 | struct file *file; | |
476 | u64 addr; | |
477 | }; | |
478 | ||
b5dba59e JA |
479 | struct io_close { |
480 | struct file *file; | |
b5dba59e JA |
481 | int fd; |
482 | }; | |
483 | ||
ad8a48ac JA |
484 | struct io_timeout_data { |
485 | struct io_kiocb *req; | |
486 | struct hrtimer timer; | |
487 | struct timespec64 ts; | |
488 | enum hrtimer_mode mode; | |
489 | }; | |
490 | ||
8ed8d3c3 JA |
491 | struct io_accept { |
492 | struct file *file; | |
493 | struct sockaddr __user *addr; | |
494 | int __user *addr_len; | |
495 | int flags; | |
09952e3e | 496 | unsigned long nofile; |
8ed8d3c3 JA |
497 | }; |
498 | ||
499 | struct io_sync { | |
500 | struct file *file; | |
501 | loff_t len; | |
502 | loff_t off; | |
503 | int flags; | |
d63d1b5e | 504 | int mode; |
8ed8d3c3 JA |
505 | }; |
506 | ||
fbf23849 JA |
507 | struct io_cancel { |
508 | struct file *file; | |
509 | u64 addr; | |
510 | }; | |
511 | ||
b29472ee JA |
512 | struct io_timeout { |
513 | struct file *file; | |
bfe68a22 PB |
514 | u32 off; |
515 | u32 target_seq; | |
135fcde8 | 516 | struct list_head list; |
90cd7e42 PB |
517 | /* head of the link, used by linked timeouts only */ |
518 | struct io_kiocb *head; | |
b29472ee JA |
519 | }; |
520 | ||
0bdf7a2d PB |
521 | struct io_timeout_rem { |
522 | struct file *file; | |
523 | u64 addr; | |
9c8e11b3 PB |
524 | |
525 | /* timeout update */ | |
526 | struct timespec64 ts; | |
527 | u32 flags; | |
0bdf7a2d PB |
528 | }; |
529 | ||
9adbd45d JA |
530 | struct io_rw { |
531 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
532 | struct kiocb kiocb; | |
533 | u64 addr; | |
534 | u64 len; | |
535 | }; | |
536 | ||
3fbb51c1 JA |
537 | struct io_connect { |
538 | struct file *file; | |
539 | struct sockaddr __user *addr; | |
540 | int addr_len; | |
541 | }; | |
542 | ||
e47293fd JA |
543 | struct io_sr_msg { |
544 | struct file *file; | |
fddaface | 545 | union { |
270a5940 | 546 | struct user_msghdr __user *umsg; |
fddaface JA |
547 | void __user *buf; |
548 | }; | |
e47293fd | 549 | int msg_flags; |
bcda7baa | 550 | int bgid; |
fddaface | 551 | size_t len; |
bcda7baa | 552 | struct io_buffer *kbuf; |
e47293fd JA |
553 | }; |
554 | ||
15b71abe JA |
555 | struct io_open { |
556 | struct file *file; | |
557 | int dfd; | |
15b71abe | 558 | struct filename *filename; |
c12cedf2 | 559 | struct open_how how; |
4022e7af | 560 | unsigned long nofile; |
15b71abe JA |
561 | }; |
562 | ||
269bbe5f | 563 | struct io_rsrc_update { |
05f3fb3c JA |
564 | struct file *file; |
565 | u64 arg; | |
566 | u32 nr_args; | |
567 | u32 offset; | |
568 | }; | |
569 | ||
4840e418 JA |
570 | struct io_fadvise { |
571 | struct file *file; | |
572 | u64 offset; | |
573 | u32 len; | |
574 | u32 advice; | |
575 | }; | |
576 | ||
c1ca757b JA |
577 | struct io_madvise { |
578 | struct file *file; | |
579 | u64 addr; | |
580 | u32 len; | |
581 | u32 advice; | |
582 | }; | |
583 | ||
3e4827b0 JA |
584 | struct io_epoll { |
585 | struct file *file; | |
586 | int epfd; | |
587 | int op; | |
588 | int fd; | |
589 | struct epoll_event event; | |
e47293fd JA |
590 | }; |
591 | ||
7d67af2c PB |
592 | struct io_splice { |
593 | struct file *file_out; | |
594 | struct file *file_in; | |
595 | loff_t off_out; | |
596 | loff_t off_in; | |
597 | u64 len; | |
598 | unsigned int flags; | |
599 | }; | |
600 | ||
ddf0322d JA |
601 | struct io_provide_buf { |
602 | struct file *file; | |
603 | __u64 addr; | |
604 | __s32 len; | |
605 | __u32 bgid; | |
606 | __u16 nbufs; | |
607 | __u16 bid; | |
608 | }; | |
609 | ||
1d9e1288 BM |
610 | struct io_statx { |
611 | struct file *file; | |
612 | int dfd; | |
613 | unsigned int mask; | |
614 | unsigned int flags; | |
e62753e4 | 615 | const char __user *filename; |
1d9e1288 BM |
616 | struct statx __user *buffer; |
617 | }; | |
618 | ||
36f4fa68 JA |
619 | struct io_shutdown { |
620 | struct file *file; | |
621 | int how; | |
622 | }; | |
623 | ||
80a261fd JA |
624 | struct io_rename { |
625 | struct file *file; | |
626 | int old_dfd; | |
627 | int new_dfd; | |
628 | struct filename *oldpath; | |
629 | struct filename *newpath; | |
630 | int flags; | |
631 | }; | |
632 | ||
14a1143b JA |
633 | struct io_unlink { |
634 | struct file *file; | |
635 | int dfd; | |
636 | int flags; | |
637 | struct filename *filename; | |
638 | }; | |
639 | ||
3ca405eb PB |
640 | struct io_completion { |
641 | struct file *file; | |
642 | struct list_head list; | |
0f7e466b | 643 | int cflags; |
3ca405eb PB |
644 | }; |
645 | ||
f499a021 JA |
646 | struct io_async_connect { |
647 | struct sockaddr_storage address; | |
648 | }; | |
649 | ||
03b1230c JA |
650 | struct io_async_msghdr { |
651 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
652 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
653 | struct iovec *free_iov; | |
03b1230c JA |
654 | struct sockaddr __user *uaddr; |
655 | struct msghdr msg; | |
b537916c | 656 | struct sockaddr_storage addr; |
03b1230c JA |
657 | }; |
658 | ||
f67676d1 JA |
659 | struct io_async_rw { |
660 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
661 | const struct iovec *free_iovec; |
662 | struct iov_iter iter; | |
227c0c96 | 663 | size_t bytes_done; |
bcf5a063 | 664 | struct wait_page_queue wpq; |
f67676d1 JA |
665 | }; |
666 | ||
6b47ee6e PB |
667 | enum { |
668 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
669 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
670 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
671 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
672 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 673 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 674 | |
6b47ee6e PB |
675 | REQ_F_FAIL_LINK_BIT, |
676 | REQ_F_INFLIGHT_BIT, | |
677 | REQ_F_CUR_POS_BIT, | |
678 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 679 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 680 | REQ_F_ISREG_BIT, |
99bc4c38 | 681 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 682 | REQ_F_POLLED_BIT, |
bcda7baa | 683 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 684 | REQ_F_NO_FILE_TABLE_BIT, |
900fad45 | 685 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 686 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
687 | |
688 | /* not a real bit, just to check we're not overflowing the space */ | |
689 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
690 | }; |
691 | ||
692 | enum { | |
693 | /* ctx owns file */ | |
694 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
695 | /* drain existing IO first */ | |
696 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
697 | /* linked sqes */ | |
698 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
699 | /* doesn't sever on completion < 0 */ | |
700 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
701 | /* IOSQE_ASYNC */ | |
702 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
703 | /* IOSQE_BUFFER_SELECT */ |
704 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 705 | |
6b47ee6e PB |
706 | /* fail rest of links */ |
707 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
708 | /* on inflight list */ | |
709 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
710 | /* read/write uses file position */ | |
711 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
712 | /* must not punt to workers */ | |
713 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 714 | /* has or had linked timeout */ |
6b47ee6e | 715 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
716 | /* regular file */ |
717 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
718 | /* needs cleanup */ |
719 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
720 | /* already went through poll handler */ |
721 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
722 | /* buffer already selected */ |
723 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
724 | /* doesn't need file table for this request */ |
725 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
900fad45 PB |
726 | /* linked timeout is active, i.e. prepared by link's head */ |
727 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
728 | /* completion is deferred through io_comp_state */ |
729 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
730 | }; |
731 | ||
732 | struct async_poll { | |
733 | struct io_poll_iocb poll; | |
807abcb0 | 734 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
735 | }; |
736 | ||
7cbf1722 JA |
737 | struct io_task_work { |
738 | struct io_wq_work_node node; | |
739 | task_work_func_t func; | |
740 | }; | |
741 | ||
09bb8394 JA |
742 | /* |
743 | * NOTE! Each of the iocb union members has the file pointer | |
744 | * as the first entry in their struct definition. So you can | |
745 | * access the file pointer through any of the sub-structs, | |
746 | * or directly as just 'ki_filp' in this struct. | |
747 | */ | |
2b188cc1 | 748 | struct io_kiocb { |
221c5eb2 | 749 | union { |
09bb8394 | 750 | struct file *file; |
9adbd45d | 751 | struct io_rw rw; |
221c5eb2 | 752 | struct io_poll_iocb poll; |
018043be | 753 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
754 | struct io_accept accept; |
755 | struct io_sync sync; | |
fbf23849 | 756 | struct io_cancel cancel; |
b29472ee | 757 | struct io_timeout timeout; |
0bdf7a2d | 758 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 759 | struct io_connect connect; |
e47293fd | 760 | struct io_sr_msg sr_msg; |
15b71abe | 761 | struct io_open open; |
b5dba59e | 762 | struct io_close close; |
269bbe5f | 763 | struct io_rsrc_update rsrc_update; |
4840e418 | 764 | struct io_fadvise fadvise; |
c1ca757b | 765 | struct io_madvise madvise; |
3e4827b0 | 766 | struct io_epoll epoll; |
7d67af2c | 767 | struct io_splice splice; |
ddf0322d | 768 | struct io_provide_buf pbuf; |
1d9e1288 | 769 | struct io_statx statx; |
36f4fa68 | 770 | struct io_shutdown shutdown; |
80a261fd | 771 | struct io_rename rename; |
14a1143b | 772 | struct io_unlink unlink; |
3ca405eb PB |
773 | /* use only after cleaning per-op data, see io_clean_op() */ |
774 | struct io_completion compl; | |
221c5eb2 | 775 | }; |
2b188cc1 | 776 | |
e8c2bc1f JA |
777 | /* opcode allocated if it needs to store data for async defer */ |
778 | void *async_data; | |
d625c6ee | 779 | u8 opcode; |
65a6543d XW |
780 | /* polled IO has completed */ |
781 | u8 iopoll_completed; | |
2b188cc1 | 782 | |
4f4eeba8 | 783 | u16 buf_index; |
9cf7c104 | 784 | u32 result; |
4f4eeba8 | 785 | |
010e8e6b PB |
786 | struct io_ring_ctx *ctx; |
787 | unsigned int flags; | |
788 | refcount_t refs; | |
789 | struct task_struct *task; | |
790 | u64 user_data; | |
d7718a9d | 791 | |
f2f87370 | 792 | struct io_kiocb *link; |
269bbe5f | 793 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 794 | |
d21ffe7e PB |
795 | /* |
796 | * 1. used with ctx->iopoll_list with reads/writes | |
797 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
798 | */ | |
010e8e6b | 799 | struct list_head inflight_entry; |
7cbf1722 JA |
800 | union { |
801 | struct io_task_work io_task_work; | |
802 | struct callback_head task_work; | |
803 | }; | |
010e8e6b PB |
804 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
805 | struct hlist_node hash_node; | |
806 | struct async_poll *apoll; | |
807 | struct io_wq_work work; | |
2b188cc1 | 808 | }; |
05589553 | 809 | |
27dc8338 PB |
810 | struct io_defer_entry { |
811 | struct list_head list; | |
812 | struct io_kiocb *req; | |
9cf7c104 | 813 | u32 seq; |
2b188cc1 JA |
814 | }; |
815 | ||
d3656344 | 816 | struct io_op_def { |
d3656344 JA |
817 | /* needs req->file assigned */ |
818 | unsigned needs_file : 1; | |
d3656344 JA |
819 | /* hash wq insertion if file is a regular file */ |
820 | unsigned hash_reg_file : 1; | |
821 | /* unbound wq insertion if file is a non-regular file */ | |
822 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
823 | /* opcode is not supported by this kernel */ |
824 | unsigned not_supported : 1; | |
8a72758c JA |
825 | /* set if opcode supports polled "wait" */ |
826 | unsigned pollin : 1; | |
827 | unsigned pollout : 1; | |
bcda7baa JA |
828 | /* op supports buffer selection */ |
829 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
830 | /* must always have async data allocated */ |
831 | unsigned needs_async_data : 1; | |
27926b68 JA |
832 | /* should block plug */ |
833 | unsigned plug : 1; | |
e8c2bc1f JA |
834 | /* size of async data needed, if any */ |
835 | unsigned short async_size; | |
d3656344 JA |
836 | }; |
837 | ||
0918682b | 838 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
839 | [IORING_OP_NOP] = {}, |
840 | [IORING_OP_READV] = { | |
d3656344 JA |
841 | .needs_file = 1, |
842 | .unbound_nonreg_file = 1, | |
8a72758c | 843 | .pollin = 1, |
4d954c25 | 844 | .buffer_select = 1, |
e8c2bc1f | 845 | .needs_async_data = 1, |
27926b68 | 846 | .plug = 1, |
e8c2bc1f | 847 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 848 | }, |
0463b6c5 | 849 | [IORING_OP_WRITEV] = { |
d3656344 JA |
850 | .needs_file = 1, |
851 | .hash_reg_file = 1, | |
852 | .unbound_nonreg_file = 1, | |
8a72758c | 853 | .pollout = 1, |
e8c2bc1f | 854 | .needs_async_data = 1, |
27926b68 | 855 | .plug = 1, |
e8c2bc1f | 856 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 857 | }, |
0463b6c5 | 858 | [IORING_OP_FSYNC] = { |
d3656344 JA |
859 | .needs_file = 1, |
860 | }, | |
0463b6c5 | 861 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
862 | .needs_file = 1, |
863 | .unbound_nonreg_file = 1, | |
8a72758c | 864 | .pollin = 1, |
27926b68 | 865 | .plug = 1, |
e8c2bc1f | 866 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 867 | }, |
0463b6c5 | 868 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
869 | .needs_file = 1, |
870 | .hash_reg_file = 1, | |
871 | .unbound_nonreg_file = 1, | |
8a72758c | 872 | .pollout = 1, |
27926b68 | 873 | .plug = 1, |
e8c2bc1f | 874 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 875 | }, |
0463b6c5 | 876 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
877 | .needs_file = 1, |
878 | .unbound_nonreg_file = 1, | |
879 | }, | |
0463b6c5 PB |
880 | [IORING_OP_POLL_REMOVE] = {}, |
881 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
882 | .needs_file = 1, |
883 | }, | |
0463b6c5 | 884 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
885 | .needs_file = 1, |
886 | .unbound_nonreg_file = 1, | |
8a72758c | 887 | .pollout = 1, |
e8c2bc1f JA |
888 | .needs_async_data = 1, |
889 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 890 | }, |
0463b6c5 | 891 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
892 | .needs_file = 1, |
893 | .unbound_nonreg_file = 1, | |
8a72758c | 894 | .pollin = 1, |
52de1fe1 | 895 | .buffer_select = 1, |
e8c2bc1f JA |
896 | .needs_async_data = 1, |
897 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 898 | }, |
0463b6c5 | 899 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
900 | .needs_async_data = 1, |
901 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 902 | }, |
9c8e11b3 PB |
903 | [IORING_OP_TIMEOUT_REMOVE] = { |
904 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 905 | }, |
0463b6c5 | 906 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
907 | .needs_file = 1, |
908 | .unbound_nonreg_file = 1, | |
8a72758c | 909 | .pollin = 1, |
d3656344 | 910 | }, |
0463b6c5 PB |
911 | [IORING_OP_ASYNC_CANCEL] = {}, |
912 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
913 | .needs_async_data = 1, |
914 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 915 | }, |
0463b6c5 | 916 | [IORING_OP_CONNECT] = { |
d3656344 JA |
917 | .needs_file = 1, |
918 | .unbound_nonreg_file = 1, | |
8a72758c | 919 | .pollout = 1, |
e8c2bc1f JA |
920 | .needs_async_data = 1, |
921 | .async_size = sizeof(struct io_async_connect), | |
d3656344 | 922 | }, |
0463b6c5 | 923 | [IORING_OP_FALLOCATE] = { |
d3656344 | 924 | .needs_file = 1, |
d3656344 | 925 | }, |
44526bed JA |
926 | [IORING_OP_OPENAT] = {}, |
927 | [IORING_OP_CLOSE] = {}, | |
928 | [IORING_OP_FILES_UPDATE] = {}, | |
929 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 930 | [IORING_OP_READ] = { |
3a6820f2 JA |
931 | .needs_file = 1, |
932 | .unbound_nonreg_file = 1, | |
8a72758c | 933 | .pollin = 1, |
bcda7baa | 934 | .buffer_select = 1, |
27926b68 | 935 | .plug = 1, |
e8c2bc1f | 936 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 937 | }, |
0463b6c5 | 938 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
939 | .needs_file = 1, |
940 | .unbound_nonreg_file = 1, | |
8a72758c | 941 | .pollout = 1, |
27926b68 | 942 | .plug = 1, |
e8c2bc1f | 943 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 944 | }, |
0463b6c5 | 945 | [IORING_OP_FADVISE] = { |
4840e418 | 946 | .needs_file = 1, |
c1ca757b | 947 | }, |
44526bed | 948 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 949 | [IORING_OP_SEND] = { |
fddaface JA |
950 | .needs_file = 1, |
951 | .unbound_nonreg_file = 1, | |
8a72758c | 952 | .pollout = 1, |
fddaface | 953 | }, |
0463b6c5 | 954 | [IORING_OP_RECV] = { |
fddaface JA |
955 | .needs_file = 1, |
956 | .unbound_nonreg_file = 1, | |
8a72758c | 957 | .pollin = 1, |
bcda7baa | 958 | .buffer_select = 1, |
fddaface | 959 | }, |
0463b6c5 | 960 | [IORING_OP_OPENAT2] = { |
cebdb986 | 961 | }, |
3e4827b0 JA |
962 | [IORING_OP_EPOLL_CTL] = { |
963 | .unbound_nonreg_file = 1, | |
3e4827b0 | 964 | }, |
7d67af2c PB |
965 | [IORING_OP_SPLICE] = { |
966 | .needs_file = 1, | |
967 | .hash_reg_file = 1, | |
968 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
969 | }, |
970 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 971 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
972 | [IORING_OP_TEE] = { |
973 | .needs_file = 1, | |
974 | .hash_reg_file = 1, | |
975 | .unbound_nonreg_file = 1, | |
976 | }, | |
36f4fa68 JA |
977 | [IORING_OP_SHUTDOWN] = { |
978 | .needs_file = 1, | |
979 | }, | |
44526bed JA |
980 | [IORING_OP_RENAMEAT] = {}, |
981 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
982 | }; |
983 | ||
9936c7c2 PB |
984 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
985 | struct task_struct *task, | |
986 | struct files_struct *files); | |
37d1e2e3 | 987 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
269bbe5f | 988 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 989 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 990 | struct io_ring_ctx *ctx); |
f2303b1f | 991 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
1ffc5422 | 992 | |
23faba36 | 993 | static bool io_rw_reissue(struct io_kiocb *req); |
78e19bbe | 994 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 995 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 996 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 997 | static void io_double_put_req(struct io_kiocb *req); |
c7dae4ba JA |
998 | static void io_dismantle_req(struct io_kiocb *req); |
999 | static void io_put_task(struct task_struct *task, int nr); | |
1000 | static void io_queue_next(struct io_kiocb *req); | |
94ae5e77 | 1001 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1002 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1003 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1004 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1005 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1006 | unsigned nr_args); |
3ca405eb | 1007 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1008 | static struct file *io_file_get(struct io_submit_state *state, |
1009 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1010 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1011 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1012 | |
847595de PB |
1013 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1014 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1015 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1016 | const struct iovec *fast_iov, | |
227c0c96 | 1017 | struct iov_iter *iter, bool force); |
907d1df3 | 1018 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1019 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1020 | struct io_ring_ctx *ctx); | |
de0617e4 | 1021 | |
2b188cc1 JA |
1022 | static struct kmem_cache *req_cachep; |
1023 | ||
0918682b | 1024 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1025 | |
1026 | struct sock *io_uring_get_socket(struct file *file) | |
1027 | { | |
1028 | #if defined(CONFIG_UNIX) | |
1029 | if (file->f_op == &io_uring_fops) { | |
1030 | struct io_ring_ctx *ctx = file->private_data; | |
1031 | ||
1032 | return ctx->ring_sock->sk; | |
1033 | } | |
1034 | #endif | |
1035 | return NULL; | |
1036 | } | |
1037 | EXPORT_SYMBOL(io_uring_get_socket); | |
1038 | ||
f2f87370 PB |
1039 | #define io_for_each_link(pos, head) \ |
1040 | for (pos = (head); pos; pos = pos->link) | |
1041 | ||
3ca405eb PB |
1042 | static inline void io_clean_op(struct io_kiocb *req) |
1043 | { | |
9d5c8190 | 1044 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1045 | __io_clean_op(req); |
1046 | } | |
1047 | ||
36f72fe2 PB |
1048 | static inline void io_set_resource_node(struct io_kiocb *req) |
1049 | { | |
1050 | struct io_ring_ctx *ctx = req->ctx; | |
1051 | ||
269bbe5f BM |
1052 | if (!req->fixed_rsrc_refs) { |
1053 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1054 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1055 | } |
1056 | } | |
1057 | ||
08d23634 PB |
1058 | static bool io_match_task(struct io_kiocb *head, |
1059 | struct task_struct *task, | |
1060 | struct files_struct *files) | |
1061 | { | |
1062 | struct io_kiocb *req; | |
1063 | ||
84965ff8 JA |
1064 | if (task && head->task != task) { |
1065 | /* in terms of cancelation, always match if req task is dead */ | |
1066 | if (head->task->flags & PF_EXITING) | |
1067 | return true; | |
08d23634 | 1068 | return false; |
84965ff8 | 1069 | } |
08d23634 PB |
1070 | if (!files) |
1071 | return true; | |
1072 | ||
1073 | io_for_each_link(req, head) { | |
02a13674 JA |
1074 | if (req->file && req->file->f_op == &io_uring_fops) |
1075 | return true; | |
4379bf8b | 1076 | if (req->task->files == files) |
08d23634 PB |
1077 | return true; |
1078 | } | |
1079 | return false; | |
1080 | } | |
1081 | ||
c40f6379 JA |
1082 | static inline void req_set_fail_links(struct io_kiocb *req) |
1083 | { | |
1084 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1085 | req->flags |= REQ_F_FAIL_LINK; | |
1086 | } | |
4a38aed2 | 1087 | |
2b188cc1 JA |
1088 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1089 | { | |
1090 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1091 | ||
0f158b4c | 1092 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1093 | } |
1094 | ||
8eb7e2d0 PB |
1095 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1096 | { | |
1097 | return !req->timeout.off; | |
1098 | } | |
1099 | ||
2b188cc1 JA |
1100 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1101 | { | |
1102 | struct io_ring_ctx *ctx; | |
78076bb6 | 1103 | int hash_bits; |
2b188cc1 JA |
1104 | |
1105 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1106 | if (!ctx) | |
1107 | return NULL; | |
1108 | ||
78076bb6 JA |
1109 | /* |
1110 | * Use 5 bits less than the max cq entries, that should give us around | |
1111 | * 32 entries per hash list if totally full and uniformly spread. | |
1112 | */ | |
1113 | hash_bits = ilog2(p->cq_entries); | |
1114 | hash_bits -= 5; | |
1115 | if (hash_bits <= 0) | |
1116 | hash_bits = 1; | |
1117 | ctx->cancel_hash_bits = hash_bits; | |
1118 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1119 | GFP_KERNEL); | |
1120 | if (!ctx->cancel_hash) | |
1121 | goto err; | |
1122 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1123 | ||
21482896 | 1124 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1125 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1126 | goto err; | |
2b188cc1 JA |
1127 | |
1128 | ctx->flags = p->flags; | |
90554200 | 1129 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1130 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1131 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1132 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1133 | init_completion(&ctx->ref_comp); |
1134 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1135 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1136 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1137 | mutex_init(&ctx->uring_lock); |
1138 | init_waitqueue_head(&ctx->wait); | |
1139 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1140 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1141 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1142 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1143 | spin_lock_init(&ctx->inflight_lock); |
1144 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1145 | spin_lock_init(&ctx->rsrc_ref_lock); |
1146 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1147 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1148 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1149 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1150 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1151 | return ctx; |
206aefde | 1152 | err: |
78076bb6 | 1153 | kfree(ctx->cancel_hash); |
206aefde JA |
1154 | kfree(ctx); |
1155 | return NULL; | |
2b188cc1 JA |
1156 | } |
1157 | ||
9cf7c104 | 1158 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1159 | { |
2bc9930e JA |
1160 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1161 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1162 | |
9cf7c104 | 1163 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1164 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1165 | } |
de0617e4 | 1166 | |
9d858b21 | 1167 | return false; |
de0617e4 JA |
1168 | } |
1169 | ||
ce3d5aae PB |
1170 | static void io_req_track_inflight(struct io_kiocb *req) |
1171 | { | |
1172 | struct io_ring_ctx *ctx = req->ctx; | |
1173 | ||
1174 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
ce3d5aae PB |
1175 | req->flags |= REQ_F_INFLIGHT; |
1176 | ||
1177 | spin_lock_irq(&ctx->inflight_lock); | |
1178 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1179 | spin_unlock_irq(&ctx->inflight_lock); | |
1180 | } | |
1181 | } | |
1182 | ||
1e6fa521 JA |
1183 | static void io_prep_async_work(struct io_kiocb *req) |
1184 | { | |
1185 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1186 | struct io_ring_ctx *ctx = req->ctx; |
1187 | ||
feaadc4f PB |
1188 | if (req->flags & REQ_F_FORCE_ASYNC) |
1189 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1190 | ||
1e6fa521 JA |
1191 | if (req->flags & REQ_F_ISREG) { |
1192 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1193 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1194 | } else { | |
1195 | if (def->unbound_nonreg_file) | |
1196 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1197 | } | |
561fb04a | 1198 | } |
cccf0ee8 | 1199 | |
cbdcb435 | 1200 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1201 | { |
cbdcb435 | 1202 | struct io_kiocb *cur; |
54a91f3b | 1203 | |
f2f87370 PB |
1204 | io_for_each_link(cur, req) |
1205 | io_prep_async_work(cur); | |
561fb04a JA |
1206 | } |
1207 | ||
7271ef3a | 1208 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1209 | { |
a197f664 | 1210 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1211 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1212 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1213 | |
3bfe6106 JA |
1214 | BUG_ON(!tctx); |
1215 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1216 | |
8766dd51 PB |
1217 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1218 | &req->work, req->flags); | |
5aa75ed5 | 1219 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a | 1220 | return link; |
18d9be1a JA |
1221 | } |
1222 | ||
cbdcb435 PB |
1223 | static void io_queue_async_work(struct io_kiocb *req) |
1224 | { | |
7271ef3a JA |
1225 | struct io_kiocb *link; |
1226 | ||
cbdcb435 PB |
1227 | /* init ->work of the whole link before punting */ |
1228 | io_prep_async_link(req); | |
7271ef3a JA |
1229 | link = __io_queue_async_work(req); |
1230 | ||
1231 | if (link) | |
1232 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1233 | } |
1234 | ||
5262f567 JA |
1235 | static void io_kill_timeout(struct io_kiocb *req) |
1236 | { | |
e8c2bc1f | 1237 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1238 | int ret; |
1239 | ||
e8c2bc1f | 1240 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1241 | if (ret != -1) { |
01cec8c1 PB |
1242 | atomic_set(&req->ctx->cq_timeouts, |
1243 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1244 | list_del_init(&req->timeout.list); |
78e19bbe | 1245 | io_cqring_fill_event(req, 0); |
216578e5 | 1246 | io_put_req_deferred(req, 1); |
5262f567 JA |
1247 | } |
1248 | } | |
1249 | ||
76e1b642 JA |
1250 | /* |
1251 | * Returns true if we found and killed one or more timeouts | |
1252 | */ | |
6b81928d PB |
1253 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1254 | struct files_struct *files) | |
5262f567 JA |
1255 | { |
1256 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1257 | int canceled = 0; |
5262f567 JA |
1258 | |
1259 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1260 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1261 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1262 | io_kill_timeout(req); |
76e1b642 JA |
1263 | canceled++; |
1264 | } | |
f3606e3a | 1265 | } |
5262f567 | 1266 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1267 | return canceled != 0; |
5262f567 JA |
1268 | } |
1269 | ||
04518945 | 1270 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1271 | { |
04518945 | 1272 | do { |
27dc8338 PB |
1273 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1274 | struct io_defer_entry, list); | |
de0617e4 | 1275 | |
9cf7c104 | 1276 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1277 | break; |
27dc8338 | 1278 | list_del_init(&de->list); |
907d1df3 | 1279 | io_req_task_queue(de->req); |
27dc8338 | 1280 | kfree(de); |
04518945 PB |
1281 | } while (!list_empty(&ctx->defer_list)); |
1282 | } | |
1283 | ||
360428f8 | 1284 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1285 | { |
f010505b MDG |
1286 | u32 seq; |
1287 | ||
1288 | if (list_empty(&ctx->timeout_list)) | |
1289 | return; | |
1290 | ||
1291 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1292 | ||
1293 | do { | |
1294 | u32 events_needed, events_got; | |
360428f8 | 1295 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1296 | struct io_kiocb, timeout.list); |
de0617e4 | 1297 | |
8eb7e2d0 | 1298 | if (io_is_timeout_noseq(req)) |
360428f8 | 1299 | break; |
f010505b MDG |
1300 | |
1301 | /* | |
1302 | * Since seq can easily wrap around over time, subtract | |
1303 | * the last seq at which timeouts were flushed before comparing. | |
1304 | * Assuming not more than 2^31-1 events have happened since, | |
1305 | * these subtractions won't have wrapped, so we can check if | |
1306 | * target is in [last_seq, current_seq] by comparing the two. | |
1307 | */ | |
1308 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1309 | events_got = seq - ctx->cq_last_tm_flush; | |
1310 | if (events_got < events_needed) | |
360428f8 | 1311 | break; |
bfe68a22 | 1312 | |
135fcde8 | 1313 | list_del_init(&req->timeout.list); |
5262f567 | 1314 | io_kill_timeout(req); |
f010505b MDG |
1315 | } while (!list_empty(&ctx->timeout_list)); |
1316 | ||
1317 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1318 | } |
5262f567 | 1319 | |
360428f8 PB |
1320 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1321 | { | |
1322 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1323 | |
1324 | /* order cqe stores with ring update */ | |
1325 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1326 | |
04518945 PB |
1327 | if (unlikely(!list_empty(&ctx->defer_list))) |
1328 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1329 | } |
1330 | ||
90554200 JA |
1331 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1332 | { | |
1333 | struct io_rings *r = ctx->rings; | |
1334 | ||
1335 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1336 | } | |
1337 | ||
888aae2e PB |
1338 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1339 | { | |
1340 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1341 | } | |
1342 | ||
2b188cc1 JA |
1343 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1344 | { | |
75b28aff | 1345 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1346 | unsigned tail; |
1347 | ||
115e12e5 SB |
1348 | /* |
1349 | * writes to the cq entry need to come after reading head; the | |
1350 | * control dependency is enough as we're using WRITE_ONCE to | |
1351 | * fill the cq entry | |
1352 | */ | |
888aae2e | 1353 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1354 | return NULL; |
1355 | ||
888aae2e | 1356 | tail = ctx->cached_cq_tail++; |
75b28aff | 1357 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1358 | } |
1359 | ||
f2842ab5 JA |
1360 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1361 | { | |
f0b493e6 JA |
1362 | if (!ctx->cq_ev_fd) |
1363 | return false; | |
7e55a19c SG |
1364 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1365 | return false; | |
f2842ab5 JA |
1366 | if (!ctx->eventfd_async) |
1367 | return true; | |
b41e9852 | 1368 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1369 | } |
1370 | ||
b41e9852 | 1371 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1372 | { |
b1445e59 PB |
1373 | /* see waitqueue_active() comment */ |
1374 | smp_mb(); | |
1375 | ||
1d7bb1d5 JA |
1376 | if (waitqueue_active(&ctx->wait)) |
1377 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1378 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1379 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1380 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1381 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1382 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1383 | wake_up_interruptible(&ctx->cq_wait); |
1384 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1385 | } | |
1d7bb1d5 JA |
1386 | } |
1387 | ||
80c18e4a PB |
1388 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1389 | { | |
b1445e59 PB |
1390 | /* see waitqueue_active() comment */ |
1391 | smp_mb(); | |
1392 | ||
80c18e4a PB |
1393 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1394 | if (waitqueue_active(&ctx->wait)) | |
1395 | wake_up(&ctx->wait); | |
1396 | } | |
1397 | if (io_should_trigger_evfd(ctx)) | |
1398 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1399 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1400 | wake_up_interruptible(&ctx->cq_wait); |
1401 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1402 | } | |
80c18e4a PB |
1403 | } |
1404 | ||
c4a2ed72 | 1405 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1406 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1407 | struct task_struct *tsk, | |
1408 | struct files_struct *files) | |
1d7bb1d5 JA |
1409 | { |
1410 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1411 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1412 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1413 | unsigned long flags; |
b18032bb | 1414 | bool all_flushed, posted; |
1d7bb1d5 JA |
1415 | LIST_HEAD(list); |
1416 | ||
e23de15f PB |
1417 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1418 | return false; | |
1d7bb1d5 | 1419 | |
b18032bb | 1420 | posted = false; |
1d7bb1d5 | 1421 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1422 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1423 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1424 | continue; |
1425 | ||
1d7bb1d5 JA |
1426 | cqe = io_get_cqring(ctx); |
1427 | if (!cqe && !force) | |
1428 | break; | |
1429 | ||
40d8ddd4 | 1430 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1431 | if (cqe) { |
1432 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1433 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1434 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1435 | } else { |
2c3bac6d | 1436 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1437 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1438 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1439 | } |
b18032bb | 1440 | posted = true; |
1d7bb1d5 JA |
1441 | } |
1442 | ||
09e88404 PB |
1443 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1444 | if (all_flushed) { | |
1445 | clear_bit(0, &ctx->sq_check_overflow); | |
1446 | clear_bit(0, &ctx->cq_check_overflow); | |
1447 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1448 | } | |
46930143 | 1449 | |
b18032bb JA |
1450 | if (posted) |
1451 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1452 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1453 | if (posted) |
1454 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1455 | |
1456 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1457 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1458 | list_del(&req->compl.list); | |
ec9c02ad | 1459 | io_put_req(req); |
1d7bb1d5 | 1460 | } |
c4a2ed72 | 1461 | |
09e88404 | 1462 | return all_flushed; |
1d7bb1d5 JA |
1463 | } |
1464 | ||
6c503150 PB |
1465 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1466 | struct task_struct *tsk, | |
1467 | struct files_struct *files) | |
1468 | { | |
1469 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1470 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1471 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1472 | mutex_lock(&ctx->uring_lock); | |
1473 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1474 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1475 | mutex_unlock(&ctx->uring_lock); | |
1476 | } | |
1477 | } | |
1478 | ||
bcda7baa | 1479 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1480 | { |
78e19bbe | 1481 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1482 | struct io_uring_cqe *cqe; |
1483 | ||
78e19bbe | 1484 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1485 | |
2b188cc1 JA |
1486 | /* |
1487 | * If we can't get a cq entry, userspace overflowed the | |
1488 | * submission (by quite a lot). Increment the overflow count in | |
1489 | * the ring. | |
1490 | */ | |
1491 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1492 | if (likely(cqe)) { |
78e19bbe | 1493 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1494 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1495 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1496 | } else if (ctx->cq_overflow_flushed || |
1497 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1498 | /* |
1499 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1500 | * then we cannot store the request for later flushing, we need | |
1501 | * to drop it on the floor. | |
1502 | */ | |
2c3bac6d PB |
1503 | ctx->cached_cq_overflow++; |
1504 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1505 | } else { |
ad3eb2c8 JA |
1506 | if (list_empty(&ctx->cq_overflow_list)) { |
1507 | set_bit(0, &ctx->sq_check_overflow); | |
1508 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1509 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1510 | } |
40d8ddd4 | 1511 | io_clean_op(req); |
1d7bb1d5 | 1512 | req->result = res; |
0f7e466b | 1513 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1514 | refcount_inc(&req->refs); |
1515 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1516 | } |
1517 | } | |
1518 | ||
bcda7baa JA |
1519 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1520 | { | |
1521 | __io_cqring_fill_event(req, res, 0); | |
1522 | } | |
1523 | ||
c7dae4ba JA |
1524 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1525 | unsigned int cflags) | |
2b188cc1 | 1526 | { |
78e19bbe | 1527 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1528 | unsigned long flags; |
1529 | ||
1530 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1531 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1532 | io_commit_cqring(ctx); |
c7dae4ba JA |
1533 | /* |
1534 | * If we're the last reference to this request, add to our locked | |
1535 | * free_list cache. | |
1536 | */ | |
1537 | if (refcount_dec_and_test(&req->refs)) { | |
1538 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1539 | ||
1540 | io_dismantle_req(req); | |
1541 | io_put_task(req->task, 1); | |
1542 | list_add(&req->compl.list, &cs->locked_free_list); | |
1543 | cs->locked_free_nr++; | |
1544 | } else | |
1545 | req = NULL; | |
2b188cc1 JA |
1546 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1547 | ||
8c838788 | 1548 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1549 | if (req) { |
1550 | io_queue_next(req); | |
1551 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1552 | } |
229a7b63 JA |
1553 | } |
1554 | ||
a38d68db | 1555 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1556 | unsigned int cflags) |
229a7b63 | 1557 | { |
a38d68db PB |
1558 | io_clean_op(req); |
1559 | req->result = res; | |
1560 | req->compl.cflags = cflags; | |
e342c807 | 1561 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1562 | } |
1563 | ||
889fca73 PB |
1564 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1565 | long res, unsigned cflags) | |
bcda7baa | 1566 | { |
889fca73 PB |
1567 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1568 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1569 | else |
c7dae4ba | 1570 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1571 | } |
1572 | ||
a38d68db | 1573 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1574 | { |
889fca73 | 1575 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1576 | } |
1577 | ||
c7dae4ba | 1578 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1579 | { |
c7dae4ba JA |
1580 | struct io_submit_state *state = &ctx->submit_state; |
1581 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 1582 | struct io_kiocb *req = NULL; |
0ddf92e8 | 1583 | |
c7dae4ba JA |
1584 | /* |
1585 | * If we have more than a batch's worth of requests in our IRQ side | |
1586 | * locked cache, grab the lock and move them over to our submission | |
1587 | * side cache. | |
1588 | */ | |
1589 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
1590 | spin_lock_irq(&ctx->completion_lock); | |
1591 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1592 | cs->locked_free_nr = 0; | |
1593 | spin_unlock_irq(&ctx->completion_lock); | |
1594 | } | |
0ddf92e8 | 1595 | |
c7dae4ba JA |
1596 | while (!list_empty(&cs->free_list)) { |
1597 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
1598 | compl.list); |
1599 | list_del(&req->compl.list); | |
e5d1bc0a PB |
1600 | state->reqs[state->free_reqs++] = req; |
1601 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
1602 | break; | |
1b4c351f JA |
1603 | } |
1604 | ||
e5d1bc0a | 1605 | return req != NULL; |
0ddf92e8 JA |
1606 | } |
1607 | ||
e5d1bc0a | 1608 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1609 | { |
e5d1bc0a PB |
1610 | struct io_submit_state *state = &ctx->submit_state; |
1611 | ||
1612 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1613 | ||
f6b6c7d6 | 1614 | if (!state->free_reqs) { |
291b2821 | 1615 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1616 | int ret; |
1617 | ||
c7dae4ba | 1618 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1619 | goto got_req; |
1620 | ||
bf019da7 PB |
1621 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1622 | state->reqs); | |
fd6fab2c JA |
1623 | |
1624 | /* | |
1625 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1626 | * retry single alloc to be on the safe side. | |
1627 | */ | |
1628 | if (unlikely(ret <= 0)) { | |
1629 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1630 | if (!state->reqs[0]) | |
3893f39f | 1631 | return NULL; |
fd6fab2c JA |
1632 | ret = 1; |
1633 | } | |
291b2821 | 1634 | state->free_reqs = ret; |
2b188cc1 | 1635 | } |
e5d1bc0a | 1636 | got_req: |
291b2821 PB |
1637 | state->free_reqs--; |
1638 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1639 | } |
1640 | ||
8da11c19 PB |
1641 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1642 | bool fixed) | |
1643 | { | |
36f72fe2 | 1644 | if (!fixed) |
8da11c19 PB |
1645 | fput(file); |
1646 | } | |
1647 | ||
4edf20f9 | 1648 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1649 | { |
3ca405eb | 1650 | io_clean_op(req); |
929a3af9 | 1651 | |
e8c2bc1f JA |
1652 | if (req->async_data) |
1653 | kfree(req->async_data); | |
8da11c19 PB |
1654 | if (req->file) |
1655 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
1656 | if (req->fixed_rsrc_refs) |
1657 | percpu_ref_put(req->fixed_rsrc_refs); | |
f85c310a PB |
1658 | |
1659 | if (req->flags & REQ_F_INFLIGHT) { | |
1660 | struct io_ring_ctx *ctx = req->ctx; | |
1661 | struct io_uring_task *tctx = req->task->io_uring; | |
1662 | unsigned long flags; | |
1663 | ||
1664 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1665 | list_del(&req->inflight_entry); | |
1666 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1667 | req->flags &= ~REQ_F_INFLIGHT; | |
1668 | if (atomic_read(&tctx->in_idle)) | |
1669 | wake_up(&tctx->wait); | |
1670 | } | |
e65ef56d JA |
1671 | } |
1672 | ||
7c660731 PB |
1673 | static inline void io_put_task(struct task_struct *task, int nr) |
1674 | { | |
1675 | struct io_uring_task *tctx = task->io_uring; | |
1676 | ||
1677 | percpu_counter_sub(&tctx->inflight, nr); | |
1678 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1679 | wake_up(&tctx->wait); | |
1680 | put_task_struct_many(task, nr); | |
1681 | } | |
1682 | ||
216578e5 | 1683 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1684 | { |
51a4cc11 | 1685 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1686 | |
216578e5 | 1687 | io_dismantle_req(req); |
7c660731 | 1688 | io_put_task(req->task, 1); |
c6ca97b3 | 1689 | |
3893f39f | 1690 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1691 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1692 | } |
1693 | ||
f2f87370 PB |
1694 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1695 | { | |
1696 | struct io_kiocb *nxt = req->link; | |
1697 | ||
1698 | req->link = nxt->link; | |
1699 | nxt->link = NULL; | |
1700 | } | |
1701 | ||
c9abd7ad | 1702 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1703 | { |
a197f664 | 1704 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1705 | struct io_kiocb *link; |
c9abd7ad PB |
1706 | bool cancelled = false; |
1707 | unsigned long flags; | |
7c86ffee | 1708 | |
c9abd7ad | 1709 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1710 | link = req->link; |
1711 | ||
900fad45 PB |
1712 | /* |
1713 | * Can happen if a linked timeout fired and link had been like | |
1714 | * req -> link t-out -> link t-out [-> ...] | |
1715 | */ | |
c9abd7ad PB |
1716 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1717 | struct io_timeout_data *io = link->async_data; | |
1718 | int ret; | |
7c86ffee | 1719 | |
f2f87370 | 1720 | io_remove_next_linked(req); |
90cd7e42 | 1721 | link->timeout.head = NULL; |
c9abd7ad PB |
1722 | ret = hrtimer_try_to_cancel(&io->timer); |
1723 | if (ret != -1) { | |
1724 | io_cqring_fill_event(link, -ECANCELED); | |
1725 | io_commit_cqring(ctx); | |
1726 | cancelled = true; | |
1727 | } | |
1728 | } | |
7c86ffee | 1729 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 1730 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 1731 | |
c9abd7ad | 1732 | if (cancelled) { |
7c86ffee | 1733 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
1734 | io_put_req(link); |
1735 | } | |
7c86ffee PB |
1736 | } |
1737 | ||
9e645e11 | 1738 | |
d148ca4b | 1739 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1740 | { |
f2f87370 | 1741 | struct io_kiocb *link, *nxt; |
2665abfd | 1742 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 1743 | unsigned long flags; |
9e645e11 | 1744 | |
d148ca4b | 1745 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1746 | link = req->link; |
1747 | req->link = NULL; | |
9e645e11 | 1748 | |
f2f87370 PB |
1749 | while (link) { |
1750 | nxt = link->link; | |
1751 | link->link = NULL; | |
2665abfd | 1752 | |
f2f87370 | 1753 | trace_io_uring_fail_link(req, link); |
7c86ffee | 1754 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 | 1755 | |
1575f21a | 1756 | io_put_req_deferred(link, 2); |
f2f87370 | 1757 | link = nxt; |
9e645e11 | 1758 | } |
2665abfd | 1759 | io_commit_cqring(ctx); |
216578e5 | 1760 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 1761 | |
2665abfd | 1762 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1763 | } |
1764 | ||
3fa5e0f3 | 1765 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1766 | { |
7c86ffee PB |
1767 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1768 | io_kill_linked_timeout(req); | |
944e58bf | 1769 | |
9e645e11 JA |
1770 | /* |
1771 | * If LINK is set, we have dependent requests in this chain. If we | |
1772 | * didn't fail this request, queue the first one up, moving any other | |
1773 | * dependencies to the next request. In case of failure, fail the rest | |
1774 | * of the chain. | |
1775 | */ | |
f2f87370 PB |
1776 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
1777 | struct io_kiocb *nxt = req->link; | |
1778 | ||
1779 | req->link = NULL; | |
1780 | return nxt; | |
1781 | } | |
9b5f7bd9 PB |
1782 | io_fail_links(req); |
1783 | return NULL; | |
4d7dd462 | 1784 | } |
9e645e11 | 1785 | |
f2f87370 | 1786 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1787 | { |
cdbff982 | 1788 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1789 | return NULL; |
1790 | return __io_req_find_next(req); | |
1791 | } | |
1792 | ||
2c32395d PB |
1793 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
1794 | { | |
1795 | if (!ctx) | |
1796 | return; | |
1797 | if (ctx->submit_state.comp.nr) { | |
1798 | mutex_lock(&ctx->uring_lock); | |
1799 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1800 | mutex_unlock(&ctx->uring_lock); | |
1801 | } | |
1802 | percpu_ref_put(&ctx->refs); | |
1803 | } | |
1804 | ||
7cbf1722 | 1805 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1806 | { |
65453d1e | 1807 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1808 | struct io_wq_work_list list; |
1809 | struct io_wq_work_node *node; | |
c2c4c83c | 1810 | |
7cbf1722 JA |
1811 | if (wq_list_empty(&tctx->task_list)) |
1812 | return false; | |
6200b0ae | 1813 | |
0b81e80c | 1814 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1815 | list = tctx->task_list; |
1816 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1817 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1818 | |
7cbf1722 JA |
1819 | node = list.first; |
1820 | while (node) { | |
1821 | struct io_wq_work_node *next = node->next; | |
1822 | struct io_kiocb *req; | |
0ba9c9ed | 1823 | |
7cbf1722 | 1824 | req = container_of(node, struct io_kiocb, io_task_work.node); |
2c32395d PB |
1825 | if (req->ctx != ctx) { |
1826 | ctx_flush_and_put(ctx); | |
1827 | ctx = req->ctx; | |
1828 | percpu_ref_get(&ctx->refs); | |
65453d1e | 1829 | } |
65453d1e | 1830 | |
2c32395d PB |
1831 | req->task_work.func(&req->task_work); |
1832 | node = next; | |
7cbf1722 JA |
1833 | } |
1834 | ||
2c32395d | 1835 | ctx_flush_and_put(ctx); |
7cbf1722 | 1836 | return list.first != NULL; |
c2c4c83c JA |
1837 | } |
1838 | ||
7cbf1722 | 1839 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1840 | { |
7cbf1722 | 1841 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1842 | |
1d5f360d JA |
1843 | clear_bit(0, &tctx->task_state); |
1844 | ||
7cbf1722 JA |
1845 | while (__tctx_task_work(tctx)) |
1846 | cond_resched(); | |
7cbf1722 JA |
1847 | } |
1848 | ||
1849 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
1850 | enum task_work_notify_mode notify) | |
1851 | { | |
1852 | struct io_uring_task *tctx = tsk->io_uring; | |
1853 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 1854 | unsigned long flags; |
7cbf1722 JA |
1855 | int ret; |
1856 | ||
1857 | WARN_ON_ONCE(!tctx); | |
1858 | ||
0b81e80c | 1859 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1860 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1861 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1862 | |
1863 | /* task_work already pending, we're done */ | |
1864 | if (test_bit(0, &tctx->task_state) || | |
1865 | test_and_set_bit(0, &tctx->task_state)) | |
1866 | return 0; | |
1867 | ||
1868 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
1869 | return 0; | |
1870 | ||
1871 | /* | |
1872 | * Slow path - we failed, find and delete work. if the work is not | |
1873 | * in the list, it got run and we're fine. | |
1874 | */ | |
1875 | ret = 0; | |
0b81e80c | 1876 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1877 | wq_list_for_each(node, prev, &tctx->task_list) { |
1878 | if (&req->io_task_work.node == node) { | |
1879 | wq_list_del(&tctx->task_list, node, prev); | |
1880 | ret = 1; | |
1881 | break; | |
1882 | } | |
1883 | } | |
0b81e80c | 1884 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1885 | clear_bit(0, &tctx->task_state); |
1886 | return ret; | |
1887 | } | |
1888 | ||
355fb9e2 | 1889 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
1890 | { |
1891 | struct task_struct *tsk = req->task; | |
1892 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
1893 | enum task_work_notify_mode notify; |
1894 | int ret; | |
c2c4c83c | 1895 | |
6200b0ae JA |
1896 | if (tsk->flags & PF_EXITING) |
1897 | return -ESRCH; | |
1898 | ||
c2c4c83c | 1899 | /* |
0ba9c9ed JA |
1900 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1901 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1902 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1903 | * will do the job. | |
c2c4c83c | 1904 | */ |
91989c70 | 1905 | notify = TWA_NONE; |
355fb9e2 | 1906 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
1907 | notify = TWA_SIGNAL; |
1908 | ||
7cbf1722 | 1909 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
1910 | if (!ret) |
1911 | wake_up_process(tsk); | |
0ba9c9ed | 1912 | |
c2c4c83c JA |
1913 | return ret; |
1914 | } | |
1915 | ||
eab30c4d | 1916 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 1917 | task_work_func_t cb) |
eab30c4d | 1918 | { |
7c25c0d1 JA |
1919 | struct io_ring_ctx *ctx = req->ctx; |
1920 | struct callback_head *head; | |
eab30c4d PB |
1921 | |
1922 | init_task_work(&req->task_work, cb); | |
7c25c0d1 JA |
1923 | do { |
1924 | head = READ_ONCE(ctx->exit_task_work); | |
1925 | req->task_work.next = head; | |
1926 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); | |
eab30c4d PB |
1927 | } |
1928 | ||
c40f6379 JA |
1929 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1930 | { | |
1931 | struct io_ring_ctx *ctx = req->ctx; | |
1932 | ||
1933 | spin_lock_irq(&ctx->completion_lock); | |
1934 | io_cqring_fill_event(req, error); | |
1935 | io_commit_cqring(ctx); | |
1936 | spin_unlock_irq(&ctx->completion_lock); | |
1937 | ||
1938 | io_cqring_ev_posted(ctx); | |
1939 | req_set_fail_links(req); | |
1940 | io_double_put_req(req); | |
1941 | } | |
1942 | ||
1943 | static void io_req_task_cancel(struct callback_head *cb) | |
1944 | { | |
1945 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 1946 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 1947 | |
792bb6eb | 1948 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 1949 | __io_req_task_cancel(req, req->result); |
792bb6eb | 1950 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 1951 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
1952 | } |
1953 | ||
1954 | static void __io_req_task_submit(struct io_kiocb *req) | |
1955 | { | |
1956 | struct io_ring_ctx *ctx = req->ctx; | |
1957 | ||
04fc6c80 | 1958 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 1959 | mutex_lock(&ctx->uring_lock); |
70aacfe6 | 1960 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
c5eef2b9 | 1961 | __io_queue_sqe(req); |
81b6d05c | 1962 | else |
c40f6379 | 1963 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 1964 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
1965 | } |
1966 | ||
1967 | static void io_req_task_submit(struct callback_head *cb) | |
1968 | { | |
1969 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
1970 | ||
1971 | __io_req_task_submit(req); | |
1972 | } | |
1973 | ||
1974 | static void io_req_task_queue(struct io_kiocb *req) | |
1975 | { | |
c40f6379 JA |
1976 | int ret; |
1977 | ||
7cbf1722 | 1978 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 1979 | ret = io_req_task_work_add(req); |
c40f6379 | 1980 | if (unlikely(ret)) { |
a3df7698 | 1981 | req->result = -ECANCELED; |
04fc6c80 | 1982 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 1983 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 1984 | } |
c40f6379 JA |
1985 | } |
1986 | ||
a3df7698 PB |
1987 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
1988 | { | |
1989 | percpu_ref_get(&req->ctx->refs); | |
1990 | req->result = ret; | |
1991 | req->task_work.func = io_req_task_cancel; | |
1992 | ||
1993 | if (unlikely(io_req_task_work_add(req))) | |
1994 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
1995 | } | |
1996 | ||
f2f87370 | 1997 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 1998 | { |
9b5f7bd9 | 1999 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2000 | |
2001 | if (nxt) | |
906a8c3f | 2002 | io_req_task_queue(nxt); |
c69f8dbe JL |
2003 | } |
2004 | ||
c3524383 | 2005 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2006 | { |
c3524383 PB |
2007 | io_queue_next(req); |
2008 | __io_free_req(req); | |
2009 | } | |
8766dd51 | 2010 | |
2d6500d4 | 2011 | struct req_batch { |
5af1d13e PB |
2012 | struct task_struct *task; |
2013 | int task_refs; | |
1b4c351f | 2014 | int ctx_refs; |
2d6500d4 PB |
2015 | }; |
2016 | ||
5af1d13e PB |
2017 | static inline void io_init_req_batch(struct req_batch *rb) |
2018 | { | |
5af1d13e | 2019 | rb->task_refs = 0; |
9ae72463 | 2020 | rb->ctx_refs = 0; |
5af1d13e PB |
2021 | rb->task = NULL; |
2022 | } | |
2023 | ||
2d6500d4 PB |
2024 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2025 | struct req_batch *rb) | |
2026 | { | |
6e833d53 | 2027 | if (rb->task) |
7c660731 | 2028 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2029 | if (rb->ctx_refs) |
2030 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2031 | } |
2032 | ||
6ff119a6 PB |
2033 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2034 | struct io_submit_state *state) | |
2d6500d4 | 2035 | { |
f2f87370 | 2036 | io_queue_next(req); |
2d6500d4 | 2037 | |
e3bc8e9d | 2038 | if (req->task != rb->task) { |
7c660731 PB |
2039 | if (rb->task) |
2040 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2041 | rb->task = req->task; |
2042 | rb->task_refs = 0; | |
5af1d13e | 2043 | } |
e3bc8e9d | 2044 | rb->task_refs++; |
9ae72463 | 2045 | rb->ctx_refs++; |
5af1d13e | 2046 | |
4edf20f9 | 2047 | io_dismantle_req(req); |
bd759045 | 2048 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2049 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2050 | else |
2051 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2052 | } |
2053 | ||
905c172f PB |
2054 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2055 | struct io_ring_ctx *ctx) | |
2056 | { | |
2057 | int i, nr = cs->nr; | |
2058 | struct io_kiocb *req; | |
2059 | struct req_batch rb; | |
2060 | ||
2061 | io_init_req_batch(&rb); | |
2062 | spin_lock_irq(&ctx->completion_lock); | |
2063 | for (i = 0; i < nr; i++) { | |
2064 | req = cs->reqs[i]; | |
2065 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2066 | } | |
2067 | io_commit_cqring(ctx); | |
2068 | spin_unlock_irq(&ctx->completion_lock); | |
2069 | ||
2070 | io_cqring_ev_posted(ctx); | |
2071 | for (i = 0; i < nr; i++) { | |
2072 | req = cs->reqs[i]; | |
2073 | ||
2074 | /* submission and completion refs */ | |
2075 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2076 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2077 | } |
2078 | ||
2079 | io_req_free_batch_finish(ctx, &rb); | |
2080 | cs->nr = 0; | |
7a743e22 PB |
2081 | } |
2082 | ||
ba816ad6 JA |
2083 | /* |
2084 | * Drop reference to request, return next in chain (if there is one) if this | |
2085 | * was the last reference to this request. | |
2086 | */ | |
9b5f7bd9 | 2087 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2088 | { |
9b5f7bd9 PB |
2089 | struct io_kiocb *nxt = NULL; |
2090 | ||
2a44f467 | 2091 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2092 | nxt = io_req_find_next(req); |
4d7dd462 | 2093 | __io_free_req(req); |
2a44f467 | 2094 | } |
9b5f7bd9 | 2095 | return nxt; |
2b188cc1 JA |
2096 | } |
2097 | ||
e65ef56d JA |
2098 | static void io_put_req(struct io_kiocb *req) |
2099 | { | |
2100 | if (refcount_dec_and_test(&req->refs)) | |
2101 | io_free_req(req); | |
2b188cc1 JA |
2102 | } |
2103 | ||
216578e5 PB |
2104 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2105 | { | |
2106 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2107 | ||
2108 | io_free_req(req); | |
2109 | } | |
2110 | ||
2111 | static void io_free_req_deferred(struct io_kiocb *req) | |
2112 | { | |
2113 | int ret; | |
2114 | ||
7cbf1722 | 2115 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2116 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2117 | if (unlikely(ret)) |
2118 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2119 | } |
2120 | ||
2121 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2122 | { | |
2123 | if (refcount_sub_and_test(refs, &req->refs)) | |
2124 | io_free_req_deferred(req); | |
2125 | } | |
2126 | ||
978db57e JA |
2127 | static void io_double_put_req(struct io_kiocb *req) |
2128 | { | |
2129 | /* drop both submit and complete references */ | |
2130 | if (refcount_sub_and_test(2, &req->refs)) | |
2131 | io_free_req(req); | |
2132 | } | |
2133 | ||
6c503150 | 2134 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2135 | { |
2136 | /* See comment at the top of this file */ | |
2137 | smp_rmb(); | |
e23de15f | 2138 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2139 | } |
2140 | ||
fb5ccc98 PB |
2141 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2142 | { | |
2143 | struct io_rings *rings = ctx->rings; | |
2144 | ||
2145 | /* make sure SQ entry isn't read before tail */ | |
2146 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2147 | } | |
2148 | ||
8ff069bf | 2149 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2150 | { |
8ff069bf | 2151 | unsigned int cflags; |
e94f141b | 2152 | |
bcda7baa JA |
2153 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2154 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2155 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2156 | kfree(kbuf); |
2157 | return cflags; | |
e94f141b JA |
2158 | } |
2159 | ||
8ff069bf | 2160 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2161 | { |
4d954c25 | 2162 | struct io_buffer *kbuf; |
bcda7baa | 2163 | |
4d954c25 | 2164 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2165 | return io_put_kbuf(req, kbuf); |
2166 | } | |
2167 | ||
4c6e277c JA |
2168 | static inline bool io_run_task_work(void) |
2169 | { | |
6200b0ae JA |
2170 | /* |
2171 | * Not safe to run on exiting task, and the task_work handling will | |
2172 | * not add work to such a task. | |
2173 | */ | |
2174 | if (unlikely(current->flags & PF_EXITING)) | |
2175 | return false; | |
4c6e277c JA |
2176 | if (current->task_works) { |
2177 | __set_current_state(TASK_RUNNING); | |
2178 | task_work_run(); | |
2179 | return true; | |
2180 | } | |
2181 | ||
2182 | return false; | |
bcda7baa JA |
2183 | } |
2184 | ||
def596e9 JA |
2185 | /* |
2186 | * Find and free completed poll iocbs | |
2187 | */ | |
2188 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2189 | struct list_head *done) | |
2190 | { | |
8237e045 | 2191 | struct req_batch rb; |
def596e9 | 2192 | struct io_kiocb *req; |
bbde017a XW |
2193 | |
2194 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2195 | smp_rmb(); | |
def596e9 | 2196 | |
5af1d13e | 2197 | io_init_req_batch(&rb); |
def596e9 | 2198 | while (!list_empty(done)) { |
bcda7baa JA |
2199 | int cflags = 0; |
2200 | ||
d21ffe7e | 2201 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2202 | list_del(&req->inflight_entry); |
2203 | ||
bbde017a XW |
2204 | if (READ_ONCE(req->result) == -EAGAIN) { |
2205 | req->iopoll_completed = 0; | |
23faba36 | 2206 | if (io_rw_reissue(req)) |
f161340d | 2207 | continue; |
bbde017a | 2208 | } |
def596e9 | 2209 | |
bcda7baa | 2210 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2211 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2212 | |
2213 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2214 | (*nr_events)++; |
2215 | ||
c3524383 | 2216 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2217 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2218 | } |
def596e9 | 2219 | |
09bb8394 | 2220 | io_commit_cqring(ctx); |
80c18e4a | 2221 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2222 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2223 | } |
2224 | ||
def596e9 JA |
2225 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2226 | long min) | |
2227 | { | |
2228 | struct io_kiocb *req, *tmp; | |
2229 | LIST_HEAD(done); | |
2230 | bool spin; | |
2231 | int ret; | |
2232 | ||
2233 | /* | |
2234 | * Only spin for completions if we don't have multiple devices hanging | |
2235 | * off our complete list, and we're under the requested amount. | |
2236 | */ | |
2237 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2238 | ||
2239 | ret = 0; | |
d21ffe7e | 2240 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2241 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2242 | |
2243 | /* | |
581f9810 BM |
2244 | * Move completed and retryable entries to our local lists. |
2245 | * If we find a request that requires polling, break out | |
2246 | * and complete those lists first, if we have entries there. | |
def596e9 | 2247 | */ |
65a6543d | 2248 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2249 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2250 | continue; |
2251 | } | |
2252 | if (!list_empty(&done)) | |
2253 | break; | |
2254 | ||
2255 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2256 | if (ret < 0) | |
2257 | break; | |
2258 | ||
3aadc23e PB |
2259 | /* iopoll may have completed current req */ |
2260 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2261 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2262 | |
def596e9 JA |
2263 | if (ret && spin) |
2264 | spin = false; | |
2265 | ret = 0; | |
2266 | } | |
2267 | ||
2268 | if (!list_empty(&done)) | |
2269 | io_iopoll_complete(ctx, nr_events, &done); | |
2270 | ||
2271 | return ret; | |
2272 | } | |
2273 | ||
2274 | /* | |
d195a66e | 2275 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2276 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2277 | * as a non-spinning completion check. | |
2278 | */ | |
2279 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2280 | long min) | |
2281 | { | |
540e32a0 | 2282 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2283 | int ret; |
2284 | ||
2285 | ret = io_do_iopoll(ctx, nr_events, min); | |
2286 | if (ret < 0) | |
2287 | return ret; | |
eba0a4dd | 2288 | if (*nr_events >= min) |
def596e9 JA |
2289 | return 0; |
2290 | } | |
2291 | ||
2292 | return 1; | |
2293 | } | |
2294 | ||
2295 | /* | |
2296 | * We can't just wait for polled events to come to us, we have to actively | |
2297 | * find and complete them. | |
2298 | */ | |
b2edc0a7 | 2299 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2300 | { |
2301 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2302 | return; | |
2303 | ||
2304 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2305 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2306 | unsigned int nr_events = 0; |
2307 | ||
b2edc0a7 | 2308 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2309 | |
b2edc0a7 PB |
2310 | /* let it sleep and repeat later if can't complete a request */ |
2311 | if (nr_events == 0) | |
2312 | break; | |
08f5439f JA |
2313 | /* |
2314 | * Ensure we allow local-to-the-cpu processing to take place, | |
2315 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2316 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2317 | */ |
3fcee5a6 PB |
2318 | if (need_resched()) { |
2319 | mutex_unlock(&ctx->uring_lock); | |
2320 | cond_resched(); | |
2321 | mutex_lock(&ctx->uring_lock); | |
2322 | } | |
def596e9 JA |
2323 | } |
2324 | mutex_unlock(&ctx->uring_lock); | |
2325 | } | |
2326 | ||
7668b92a | 2327 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2328 | { |
7668b92a | 2329 | unsigned int nr_events = 0; |
2b2ed975 | 2330 | int iters = 0, ret = 0; |
500f9fba | 2331 | |
c7849be9 XW |
2332 | /* |
2333 | * We disallow the app entering submit/complete with polling, but we | |
2334 | * still need to lock the ring to prevent racing with polled issue | |
2335 | * that got punted to a workqueue. | |
2336 | */ | |
2337 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2338 | do { |
a3a0e43f JA |
2339 | /* |
2340 | * Don't enter poll loop if we already have events pending. | |
2341 | * If we do, we can potentially be spinning for commands that | |
2342 | * already triggered a CQE (eg in error). | |
2343 | */ | |
6c503150 PB |
2344 | if (test_bit(0, &ctx->cq_check_overflow)) |
2345 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2346 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2347 | break; |
2348 | ||
500f9fba JA |
2349 | /* |
2350 | * If a submit got punted to a workqueue, we can have the | |
2351 | * application entering polling for a command before it gets | |
2352 | * issued. That app will hold the uring_lock for the duration | |
2353 | * of the poll right here, so we need to take a breather every | |
2354 | * now and then to ensure that the issue has a chance to add | |
2355 | * the poll to the issued list. Otherwise we can spin here | |
2356 | * forever, while the workqueue is stuck trying to acquire the | |
2357 | * very same mutex. | |
2358 | */ | |
2359 | if (!(++iters & 7)) { | |
2360 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2361 | io_run_task_work(); |
500f9fba JA |
2362 | mutex_lock(&ctx->uring_lock); |
2363 | } | |
2364 | ||
7668b92a | 2365 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2366 | if (ret <= 0) |
2367 | break; | |
2368 | ret = 0; | |
7668b92a | 2369 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2370 | |
500f9fba | 2371 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2372 | return ret; |
2373 | } | |
2374 | ||
491381ce | 2375 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2376 | { |
491381ce JA |
2377 | /* |
2378 | * Tell lockdep we inherited freeze protection from submission | |
2379 | * thread. | |
2380 | */ | |
2381 | if (req->flags & REQ_F_ISREG) { | |
2382 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2383 | |
491381ce | 2384 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2385 | } |
491381ce | 2386 | file_end_write(req->file); |
2b188cc1 JA |
2387 | } |
2388 | ||
b63534c4 | 2389 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2390 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2391 | { |
2392 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2393 | int rw, ret; |
b63534c4 | 2394 | struct iov_iter iter; |
b63534c4 | 2395 | |
dc2a6e9a PB |
2396 | /* already prepared */ |
2397 | if (req->async_data) | |
2398 | return true; | |
b63534c4 JA |
2399 | |
2400 | switch (req->opcode) { | |
2401 | case IORING_OP_READV: | |
2402 | case IORING_OP_READ_FIXED: | |
2403 | case IORING_OP_READ: | |
2404 | rw = READ; | |
2405 | break; | |
2406 | case IORING_OP_WRITEV: | |
2407 | case IORING_OP_WRITE_FIXED: | |
2408 | case IORING_OP_WRITE: | |
2409 | rw = WRITE; | |
2410 | break; | |
2411 | default: | |
2412 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2413 | req->opcode); | |
dc2a6e9a | 2414 | return false; |
b63534c4 JA |
2415 | } |
2416 | ||
dc2a6e9a PB |
2417 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2418 | if (ret < 0) | |
2419 | return false; | |
6bf985dc | 2420 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2421 | } |
b63534c4 | 2422 | |
3e6a0d3c | 2423 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2424 | { |
355afaeb | 2425 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2426 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2427 | |
355afaeb JA |
2428 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2429 | return false; | |
3e6a0d3c JA |
2430 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2431 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2432 | return false; |
7c977a58 JA |
2433 | /* |
2434 | * If ref is dying, we might be running poll reap from the exit work. | |
2435 | * Don't attempt to reissue from that path, just let it fail with | |
2436 | * -EAGAIN. | |
2437 | */ | |
3e6a0d3c JA |
2438 | if (percpu_ref_is_dying(&ctx->refs)) |
2439 | return false; | |
2440 | return true; | |
2441 | } | |
2442 | #endif | |
2443 | ||
2444 | static bool io_rw_reissue(struct io_kiocb *req) | |
2445 | { | |
2446 | #ifdef CONFIG_BLOCK | |
2447 | if (!io_rw_should_reissue(req)) | |
7c977a58 | 2448 | return false; |
b63534c4 | 2449 | |
55e6ac1e PB |
2450 | lockdep_assert_held(&req->ctx->uring_lock); |
2451 | ||
37d1e2e3 | 2452 | if (io_resubmit_prep(req)) { |
fdee946d JA |
2453 | refcount_inc(&req->refs); |
2454 | io_queue_async_work(req); | |
b63534c4 | 2455 | return true; |
fdee946d | 2456 | } |
dc2a6e9a | 2457 | req_set_fail_links(req); |
b63534c4 JA |
2458 | #endif |
2459 | return false; | |
2460 | } | |
2461 | ||
a1d7c393 | 2462 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2463 | unsigned int issue_flags) |
a1d7c393 | 2464 | { |
2f8e45f1 PB |
2465 | int cflags = 0; |
2466 | ||
23faba36 PB |
2467 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2468 | return; | |
2f8e45f1 PB |
2469 | if (res != req->result) |
2470 | req_set_fail_links(req); | |
23faba36 | 2471 | |
2f8e45f1 PB |
2472 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2473 | kiocb_end_write(req); | |
2474 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2475 | cflags = io_put_rw_kbuf(req); | |
2476 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2477 | } |
2478 | ||
2479 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2480 | { | |
9adbd45d | 2481 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2482 | |
889fca73 | 2483 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2484 | } |
2485 | ||
def596e9 JA |
2486 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2487 | { | |
9adbd45d | 2488 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2489 | |
3e6a0d3c JA |
2490 | #ifdef CONFIG_BLOCK |
2491 | /* Rewind iter, if we have one. iopoll path resubmits as usual */ | |
2492 | if (res == -EAGAIN && io_rw_should_reissue(req)) { | |
2493 | struct io_async_rw *rw = req->async_data; | |
2494 | ||
2495 | if (rw) | |
2496 | iov_iter_revert(&rw->iter, | |
2497 | req->result - iov_iter_count(&rw->iter)); | |
2498 | else if (!io_resubmit_prep(req)) | |
2499 | res = -EIO; | |
2500 | } | |
2501 | #endif | |
2502 | ||
491381ce JA |
2503 | if (kiocb->ki_flags & IOCB_WRITE) |
2504 | kiocb_end_write(req); | |
def596e9 | 2505 | |
2d7d6792 | 2506 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2507 | req_set_fail_links(req); |
bbde017a XW |
2508 | |
2509 | WRITE_ONCE(req->result, res); | |
2510 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2511 | smp_wmb(); |
2512 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2513 | } |
2514 | ||
2515 | /* | |
2516 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2517 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2518 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2519 | * accessing the kiocb cookie. | |
2520 | */ | |
2e9dbe90 | 2521 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2522 | { |
2523 | struct io_ring_ctx *ctx = req->ctx; | |
2524 | ||
2525 | /* | |
2526 | * Track whether we have multiple files in our lists. This will impact | |
2527 | * how we do polling eventually, not spinning if we're on potentially | |
2528 | * different devices. | |
2529 | */ | |
540e32a0 | 2530 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2531 | ctx->poll_multi_file = false; |
2532 | } else if (!ctx->poll_multi_file) { | |
2533 | struct io_kiocb *list_req; | |
2534 | ||
540e32a0 | 2535 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2536 | inflight_entry); |
9adbd45d | 2537 | if (list_req->file != req->file) |
def596e9 JA |
2538 | ctx->poll_multi_file = true; |
2539 | } | |
2540 | ||
2541 | /* | |
2542 | * For fast devices, IO may have already completed. If it has, add | |
2543 | * it to the front so we find it first. | |
2544 | */ | |
65a6543d | 2545 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2546 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2547 | else |
d21ffe7e | 2548 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2549 | |
2e9dbe90 XW |
2550 | /* |
2551 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2552 | * task context or in io worker task context. If current task context is | |
2553 | * sq thread, we don't need to check whether should wake up sq thread. | |
2554 | */ | |
2555 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2556 | wq_has_sleeper(&ctx->sq_data->wait)) |
2557 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2558 | } |
2559 | ||
9f13c35b PB |
2560 | static inline void io_state_file_put(struct io_submit_state *state) |
2561 | { | |
02b23a9a PB |
2562 | if (state->file_refs) { |
2563 | fput_many(state->file, state->file_refs); | |
2564 | state->file_refs = 0; | |
2565 | } | |
9a56a232 JA |
2566 | } |
2567 | ||
2568 | /* | |
2569 | * Get as many references to a file as we have IOs left in this submission, | |
2570 | * assuming most submissions are for one file, or at least that each file | |
2571 | * has more than one submission. | |
2572 | */ | |
8da11c19 | 2573 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2574 | { |
2575 | if (!state) | |
2576 | return fget(fd); | |
2577 | ||
6e1271e6 | 2578 | if (state->file_refs) { |
9a56a232 | 2579 | if (state->fd == fd) { |
6e1271e6 | 2580 | state->file_refs--; |
9a56a232 JA |
2581 | return state->file; |
2582 | } | |
02b23a9a | 2583 | io_state_file_put(state); |
9a56a232 JA |
2584 | } |
2585 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2586 | if (unlikely(!state->file)) |
9a56a232 JA |
2587 | return NULL; |
2588 | ||
2589 | state->fd = fd; | |
6e1271e6 | 2590 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2591 | return state->file; |
2592 | } | |
2593 | ||
4503b767 JA |
2594 | static bool io_bdev_nowait(struct block_device *bdev) |
2595 | { | |
9ba0d0c8 | 2596 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2597 | } |
2598 | ||
2b188cc1 JA |
2599 | /* |
2600 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2601 | * any file. For now, just ensure that anything potentially problematic is done | |
2602 | * inline. | |
2603 | */ | |
af197f50 | 2604 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2605 | { |
2606 | umode_t mode = file_inode(file)->i_mode; | |
2607 | ||
4503b767 | 2608 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2609 | if (IS_ENABLED(CONFIG_BLOCK) && |
2610 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2611 | return true; |
2612 | return false; | |
2613 | } | |
2614 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2615 | return true; |
4503b767 | 2616 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2617 | if (IS_ENABLED(CONFIG_BLOCK) && |
2618 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2619 | file->f_op != &io_uring_fops) |
2620 | return true; | |
2621 | return false; | |
2622 | } | |
2b188cc1 | 2623 | |
c5b85625 JA |
2624 | /* any ->read/write should understand O_NONBLOCK */ |
2625 | if (file->f_flags & O_NONBLOCK) | |
2626 | return true; | |
2627 | ||
af197f50 JA |
2628 | if (!(file->f_mode & FMODE_NOWAIT)) |
2629 | return false; | |
2630 | ||
2631 | if (rw == READ) | |
2632 | return file->f_op->read_iter != NULL; | |
2633 | ||
2634 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2635 | } |
2636 | ||
a88fc400 | 2637 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2638 | { |
def596e9 | 2639 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2640 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2641 | struct file *file = req->file; |
09bb8394 JA |
2642 | unsigned ioprio; |
2643 | int ret; | |
2b188cc1 | 2644 | |
75c668cd | 2645 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2646 | req->flags |= REQ_F_ISREG; |
2647 | ||
2b188cc1 | 2648 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2649 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2650 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2651 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2652 | } |
2b188cc1 | 2653 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2654 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2655 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2656 | if (unlikely(ret)) | |
2657 | return ret; | |
2b188cc1 | 2658 | |
75c668cd PB |
2659 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2660 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2661 | req->flags |= REQ_F_NOWAIT; | |
2662 | ||
2b188cc1 JA |
2663 | ioprio = READ_ONCE(sqe->ioprio); |
2664 | if (ioprio) { | |
2665 | ret = ioprio_check_cap(ioprio); | |
2666 | if (ret) | |
09bb8394 | 2667 | return ret; |
2b188cc1 JA |
2668 | |
2669 | kiocb->ki_ioprio = ioprio; | |
2670 | } else | |
2671 | kiocb->ki_ioprio = get_current_ioprio(); | |
2672 | ||
def596e9 | 2673 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2674 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2675 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2676 | return -EOPNOTSUPP; |
2b188cc1 | 2677 | |
def596e9 JA |
2678 | kiocb->ki_flags |= IOCB_HIPRI; |
2679 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2680 | req->iopoll_completed = 0; |
def596e9 | 2681 | } else { |
09bb8394 JA |
2682 | if (kiocb->ki_flags & IOCB_HIPRI) |
2683 | return -EINVAL; | |
def596e9 JA |
2684 | kiocb->ki_complete = io_complete_rw; |
2685 | } | |
9adbd45d | 2686 | |
3529d8c2 JA |
2687 | req->rw.addr = READ_ONCE(sqe->addr); |
2688 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2689 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2690 | return 0; |
2b188cc1 JA |
2691 | } |
2692 | ||
2693 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2694 | { | |
2695 | switch (ret) { | |
2696 | case -EIOCBQUEUED: | |
2697 | break; | |
2698 | case -ERESTARTSYS: | |
2699 | case -ERESTARTNOINTR: | |
2700 | case -ERESTARTNOHAND: | |
2701 | case -ERESTART_RESTARTBLOCK: | |
2702 | /* | |
2703 | * We can't just restart the syscall, since previously | |
2704 | * submitted sqes may already be in progress. Just fail this | |
2705 | * IO with EINTR. | |
2706 | */ | |
2707 | ret = -EINTR; | |
df561f66 | 2708 | fallthrough; |
2b188cc1 JA |
2709 | default: |
2710 | kiocb->ki_complete(kiocb, ret, 0); | |
2711 | } | |
2712 | } | |
2713 | ||
a1d7c393 | 2714 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2715 | unsigned int issue_flags) |
ba816ad6 | 2716 | { |
ba04291e | 2717 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2718 | struct io_async_rw *io = req->async_data; |
ba04291e | 2719 | |
227c0c96 | 2720 | /* add previously done IO, if any */ |
e8c2bc1f | 2721 | if (io && io->bytes_done > 0) { |
227c0c96 | 2722 | if (ret < 0) |
e8c2bc1f | 2723 | ret = io->bytes_done; |
227c0c96 | 2724 | else |
e8c2bc1f | 2725 | ret += io->bytes_done; |
227c0c96 JA |
2726 | } |
2727 | ||
ba04291e JA |
2728 | if (req->flags & REQ_F_CUR_POS) |
2729 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2730 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2731 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2732 | else |
2733 | io_rw_done(kiocb, ret); | |
2734 | } | |
2735 | ||
847595de | 2736 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2737 | { |
9adbd45d JA |
2738 | struct io_ring_ctx *ctx = req->ctx; |
2739 | size_t len = req->rw.len; | |
edafccee | 2740 | struct io_mapped_ubuf *imu; |
4be1c615 | 2741 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2742 | size_t offset; |
2743 | u64 buf_addr; | |
2744 | ||
edafccee JA |
2745 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2746 | return -EFAULT; | |
edafccee JA |
2747 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2748 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2749 | buf_addr = req->rw.addr; |
edafccee JA |
2750 | |
2751 | /* overflow */ | |
2752 | if (buf_addr + len < buf_addr) | |
2753 | return -EFAULT; | |
2754 | /* not inside the mapped region */ | |
2755 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2756 | return -EFAULT; | |
2757 | ||
2758 | /* | |
2759 | * May not be a start of buffer, set size appropriately | |
2760 | * and advance us to the beginning. | |
2761 | */ | |
2762 | offset = buf_addr - imu->ubuf; | |
2763 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2764 | |
2765 | if (offset) { | |
2766 | /* | |
2767 | * Don't use iov_iter_advance() here, as it's really slow for | |
2768 | * using the latter parts of a big fixed buffer - it iterates | |
2769 | * over each segment manually. We can cheat a bit here, because | |
2770 | * we know that: | |
2771 | * | |
2772 | * 1) it's a BVEC iter, we set it up | |
2773 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2774 | * first and last bvec | |
2775 | * | |
2776 | * So just find our index, and adjust the iterator afterwards. | |
2777 | * If the offset is within the first bvec (or the whole first | |
2778 | * bvec, just use iov_iter_advance(). This makes it easier | |
2779 | * since we can just skip the first segment, which may not | |
2780 | * be PAGE_SIZE aligned. | |
2781 | */ | |
2782 | const struct bio_vec *bvec = imu->bvec; | |
2783 | ||
2784 | if (offset <= bvec->bv_len) { | |
2785 | iov_iter_advance(iter, offset); | |
2786 | } else { | |
2787 | unsigned long seg_skip; | |
2788 | ||
2789 | /* skip first vec */ | |
2790 | offset -= bvec->bv_len; | |
2791 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2792 | ||
2793 | iter->bvec = bvec + seg_skip; | |
2794 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2795 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2796 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2797 | } |
2798 | } | |
2799 | ||
847595de | 2800 | return 0; |
edafccee JA |
2801 | } |
2802 | ||
bcda7baa JA |
2803 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2804 | { | |
2805 | if (needs_lock) | |
2806 | mutex_unlock(&ctx->uring_lock); | |
2807 | } | |
2808 | ||
2809 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2810 | { | |
2811 | /* | |
2812 | * "Normal" inline submissions always hold the uring_lock, since we | |
2813 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2814 | * The only exception is when we've detached the request and issue it | |
2815 | * from an async worker thread, grab the lock for that case. | |
2816 | */ | |
2817 | if (needs_lock) | |
2818 | mutex_lock(&ctx->uring_lock); | |
2819 | } | |
2820 | ||
2821 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2822 | int bgid, struct io_buffer *kbuf, | |
2823 | bool needs_lock) | |
2824 | { | |
2825 | struct io_buffer *head; | |
2826 | ||
2827 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2828 | return kbuf; | |
2829 | ||
2830 | io_ring_submit_lock(req->ctx, needs_lock); | |
2831 | ||
2832 | lockdep_assert_held(&req->ctx->uring_lock); | |
2833 | ||
2834 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2835 | if (head) { | |
2836 | if (!list_empty(&head->list)) { | |
2837 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2838 | list); | |
2839 | list_del(&kbuf->list); | |
2840 | } else { | |
2841 | kbuf = head; | |
2842 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2843 | } | |
2844 | if (*len > kbuf->len) | |
2845 | *len = kbuf->len; | |
2846 | } else { | |
2847 | kbuf = ERR_PTR(-ENOBUFS); | |
2848 | } | |
2849 | ||
2850 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2851 | ||
2852 | return kbuf; | |
2853 | } | |
2854 | ||
4d954c25 JA |
2855 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2856 | bool needs_lock) | |
2857 | { | |
2858 | struct io_buffer *kbuf; | |
4f4eeba8 | 2859 | u16 bgid; |
4d954c25 JA |
2860 | |
2861 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2862 | bgid = req->buf_index; |
4d954c25 JA |
2863 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2864 | if (IS_ERR(kbuf)) | |
2865 | return kbuf; | |
2866 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2867 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2868 | return u64_to_user_ptr(kbuf->addr); | |
2869 | } | |
2870 | ||
2871 | #ifdef CONFIG_COMPAT | |
2872 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2873 | bool needs_lock) | |
2874 | { | |
2875 | struct compat_iovec __user *uiov; | |
2876 | compat_ssize_t clen; | |
2877 | void __user *buf; | |
2878 | ssize_t len; | |
2879 | ||
2880 | uiov = u64_to_user_ptr(req->rw.addr); | |
2881 | if (!access_ok(uiov, sizeof(*uiov))) | |
2882 | return -EFAULT; | |
2883 | if (__get_user(clen, &uiov->iov_len)) | |
2884 | return -EFAULT; | |
2885 | if (clen < 0) | |
2886 | return -EINVAL; | |
2887 | ||
2888 | len = clen; | |
2889 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2890 | if (IS_ERR(buf)) | |
2891 | return PTR_ERR(buf); | |
2892 | iov[0].iov_base = buf; | |
2893 | iov[0].iov_len = (compat_size_t) len; | |
2894 | return 0; | |
2895 | } | |
2896 | #endif | |
2897 | ||
2898 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2899 | bool needs_lock) | |
2900 | { | |
2901 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2902 | void __user *buf; | |
2903 | ssize_t len; | |
2904 | ||
2905 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2906 | return -EFAULT; | |
2907 | ||
2908 | len = iov[0].iov_len; | |
2909 | if (len < 0) | |
2910 | return -EINVAL; | |
2911 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2912 | if (IS_ERR(buf)) | |
2913 | return PTR_ERR(buf); | |
2914 | iov[0].iov_base = buf; | |
2915 | iov[0].iov_len = len; | |
2916 | return 0; | |
2917 | } | |
2918 | ||
2919 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2920 | bool needs_lock) | |
2921 | { | |
dddb3e26 JA |
2922 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2923 | struct io_buffer *kbuf; | |
2924 | ||
2925 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2926 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2927 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2928 | return 0; |
dddb3e26 | 2929 | } |
dd201662 | 2930 | if (req->rw.len != 1) |
4d954c25 JA |
2931 | return -EINVAL; |
2932 | ||
2933 | #ifdef CONFIG_COMPAT | |
2934 | if (req->ctx->compat) | |
2935 | return io_compat_import(req, iov, needs_lock); | |
2936 | #endif | |
2937 | ||
2938 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2939 | } | |
2940 | ||
847595de PB |
2941 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2942 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2943 | { |
9adbd45d JA |
2944 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2945 | size_t sqe_len = req->rw.len; | |
847595de | 2946 | u8 opcode = req->opcode; |
4d954c25 | 2947 | ssize_t ret; |
edafccee | 2948 | |
7d009165 | 2949 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2950 | *iovec = NULL; |
9adbd45d | 2951 | return io_import_fixed(req, rw, iter); |
edafccee | 2952 | } |
2b188cc1 | 2953 | |
bcda7baa | 2954 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2955 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2956 | return -EINVAL; |
2957 | ||
3a6820f2 | 2958 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2959 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2960 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2961 | if (IS_ERR(buf)) |
4d954c25 | 2962 | return PTR_ERR(buf); |
3f9d6441 | 2963 | req->rw.len = sqe_len; |
bcda7baa JA |
2964 | } |
2965 | ||
3a6820f2 JA |
2966 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
2967 | *iovec = NULL; | |
10fc72e4 | 2968 | return ret; |
3a6820f2 JA |
2969 | } |
2970 | ||
4d954c25 JA |
2971 | if (req->flags & REQ_F_BUFFER_SELECT) { |
2972 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
2973 | if (!ret) |
2974 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
2975 | *iovec = NULL; |
2976 | return ret; | |
2977 | } | |
2978 | ||
89cd35c5 CH |
2979 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
2980 | req->ctx->compat); | |
2b188cc1 JA |
2981 | } |
2982 | ||
0fef9483 JA |
2983 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
2984 | { | |
5b09e37e | 2985 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
2986 | } |
2987 | ||
31b51510 | 2988 | /* |
32960613 JA |
2989 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
2990 | * by looping over ->read() or ->write() manually. | |
31b51510 | 2991 | */ |
4017eb91 | 2992 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 2993 | { |
4017eb91 JA |
2994 | struct kiocb *kiocb = &req->rw.kiocb; |
2995 | struct file *file = req->file; | |
32960613 JA |
2996 | ssize_t ret = 0; |
2997 | ||
2998 | /* | |
2999 | * Don't support polled IO through this interface, and we can't | |
3000 | * support non-blocking either. For the latter, this just causes | |
3001 | * the kiocb to be handled from an async context. | |
3002 | */ | |
3003 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3004 | return -EOPNOTSUPP; | |
3005 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3006 | return -EAGAIN; | |
3007 | ||
3008 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3009 | struct iovec iovec; |
32960613 JA |
3010 | ssize_t nr; |
3011 | ||
311ae9e1 PB |
3012 | if (!iov_iter_is_bvec(iter)) { |
3013 | iovec = iov_iter_iovec(iter); | |
3014 | } else { | |
4017eb91 JA |
3015 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3016 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3017 | } |
3018 | ||
32960613 JA |
3019 | if (rw == READ) { |
3020 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3021 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3022 | } else { |
3023 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3024 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3025 | } |
3026 | ||
3027 | if (nr < 0) { | |
3028 | if (!ret) | |
3029 | ret = nr; | |
3030 | break; | |
3031 | } | |
3032 | ret += nr; | |
3033 | if (nr != iovec.iov_len) | |
3034 | break; | |
4017eb91 JA |
3035 | req->rw.len -= nr; |
3036 | req->rw.addr += nr; | |
32960613 JA |
3037 | iov_iter_advance(iter, nr); |
3038 | } | |
3039 | ||
3040 | return ret; | |
3041 | } | |
3042 | ||
ff6165b2 JA |
3043 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3044 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3045 | { |
e8c2bc1f | 3046 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3047 | |
ff6165b2 | 3048 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3049 | rw->free_iovec = iovec; |
227c0c96 | 3050 | rw->bytes_done = 0; |
ff6165b2 | 3051 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3052 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3053 | return; |
b64e3444 | 3054 | if (!iovec) { |
ff6165b2 JA |
3055 | unsigned iov_off = 0; |
3056 | ||
3057 | rw->iter.iov = rw->fast_iov; | |
3058 | if (iter->iov != fast_iov) { | |
3059 | iov_off = iter->iov - fast_iov; | |
3060 | rw->iter.iov += iov_off; | |
3061 | } | |
3062 | if (rw->fast_iov != fast_iov) | |
3063 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3064 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3065 | } else { |
3066 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3067 | } |
3068 | } | |
3069 | ||
e8c2bc1f | 3070 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3071 | { |
e8c2bc1f JA |
3072 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3073 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3074 | return req->async_data == NULL; | |
3d9932a8 XW |
3075 | } |
3076 | ||
e8c2bc1f | 3077 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3078 | { |
e8c2bc1f | 3079 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3080 | return 0; |
3d9932a8 | 3081 | |
e8c2bc1f | 3082 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3083 | } |
3084 | ||
ff6165b2 JA |
3085 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3086 | const struct iovec *fast_iov, | |
227c0c96 | 3087 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3088 | { |
e8c2bc1f | 3089 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3090 | return 0; |
e8c2bc1f | 3091 | if (!req->async_data) { |
6bf985dc PB |
3092 | if (__io_alloc_async_data(req)) { |
3093 | kfree(iovec); | |
5d204bcf | 3094 | return -ENOMEM; |
6bf985dc | 3095 | } |
b7bb4f7d | 3096 | |
ff6165b2 | 3097 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3098 | } |
b7bb4f7d | 3099 | return 0; |
f67676d1 JA |
3100 | } |
3101 | ||
73debe68 | 3102 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3103 | { |
e8c2bc1f | 3104 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3105 | struct iovec *iov = iorw->fast_iov; |
847595de | 3106 | int ret; |
c3e330a4 | 3107 | |
2846c481 | 3108 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3109 | if (unlikely(ret < 0)) |
3110 | return ret; | |
3111 | ||
ab0b196c PB |
3112 | iorw->bytes_done = 0; |
3113 | iorw->free_iovec = iov; | |
3114 | if (iov) | |
3115 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3116 | return 0; |
3117 | } | |
3118 | ||
73debe68 | 3119 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3120 | { |
3529d8c2 JA |
3121 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3122 | return -EBADF; | |
93642ef8 | 3123 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3124 | } |
3125 | ||
c1dd91d1 JA |
3126 | /* |
3127 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3128 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3129 | * This gets called when the page is unlocked, and we generally expect that to | |
3130 | * happen when the page IO is completed and the page is now uptodate. This will | |
3131 | * queue a task_work based retry of the operation, attempting to copy the data | |
3132 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3133 | * do a thread based blocking retry of the operation. That's the unexpected | |
3134 | * slow path. | |
3135 | */ | |
bcf5a063 JA |
3136 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3137 | int sync, void *arg) | |
3138 | { | |
3139 | struct wait_page_queue *wpq; | |
3140 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3141 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3142 | |
3143 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3144 | ||
cdc8fcb4 LT |
3145 | if (!wake_page_match(wpq, key)) |
3146 | return 0; | |
3147 | ||
c8d317aa | 3148 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3149 | list_del_init(&wait->entry); |
3150 | ||
bcf5a063 JA |
3151 | /* submit ref gets dropped, acquire a new one */ |
3152 | refcount_inc(&req->refs); | |
921b9054 | 3153 | io_req_task_queue(req); |
bcf5a063 JA |
3154 | return 1; |
3155 | } | |
3156 | ||
c1dd91d1 JA |
3157 | /* |
3158 | * This controls whether a given IO request should be armed for async page | |
3159 | * based retry. If we return false here, the request is handed to the async | |
3160 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3161 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3162 | * will either succeed because the page is now uptodate and unlocked, or it | |
3163 | * will register a callback when the page is unlocked at IO completion. Through | |
3164 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3165 | * That retry will attempt the buffered read again. The retry will generally | |
3166 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3167 | * async worker threads for a blocking retry. | |
3168 | */ | |
227c0c96 | 3169 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3170 | { |
e8c2bc1f JA |
3171 | struct io_async_rw *rw = req->async_data; |
3172 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3173 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3174 | |
bcf5a063 JA |
3175 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3176 | if (req->flags & REQ_F_NOWAIT) | |
3177 | return false; | |
f67676d1 | 3178 | |
227c0c96 | 3179 | /* Only for buffered IO */ |
3b2a4439 | 3180 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3181 | return false; |
3b2a4439 | 3182 | |
bcf5a063 JA |
3183 | /* |
3184 | * just use poll if we can, and don't attempt if the fs doesn't | |
3185 | * support callback based unlocks | |
3186 | */ | |
3187 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3188 | return false; | |
f67676d1 | 3189 | |
3b2a4439 JA |
3190 | wait->wait.func = io_async_buf_func; |
3191 | wait->wait.private = req; | |
3192 | wait->wait.flags = 0; | |
3193 | INIT_LIST_HEAD(&wait->wait.entry); | |
3194 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3195 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3196 | kiocb->ki_waitq = wait; |
3b2a4439 | 3197 | return true; |
bcf5a063 JA |
3198 | } |
3199 | ||
3200 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3201 | { | |
3202 | if (req->file->f_op->read_iter) | |
3203 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3204 | else if (req->file->f_op->read) |
4017eb91 | 3205 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3206 | else |
3207 | return -EINVAL; | |
f67676d1 JA |
3208 | } |
3209 | ||
889fca73 | 3210 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3211 | { |
3212 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3213 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3214 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3215 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3216 | ssize_t io_size, ret, ret2; |
45d189c6 | 3217 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3218 | |
2846c481 | 3219 | if (rw) { |
e8c2bc1f | 3220 | iter = &rw->iter; |
2846c481 PB |
3221 | iovec = NULL; |
3222 | } else { | |
3223 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3224 | if (ret < 0) | |
3225 | return ret; | |
3226 | } | |
632546c4 | 3227 | io_size = iov_iter_count(iter); |
fa15bafb | 3228 | req->result = io_size; |
2b188cc1 | 3229 | |
fd6c2e4c JA |
3230 | /* Ensure we clear previously set non-block flag */ |
3231 | if (!force_nonblock) | |
29de5f6a | 3232 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3233 | else |
3234 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3235 | ||
24c74678 | 3236 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3237 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3238 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3239 | return ret ?: -EAGAIN; |
6713e7a6 | 3240 | } |
9e645e11 | 3241 | |
632546c4 | 3242 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3243 | if (unlikely(ret)) { |
3244 | kfree(iovec); | |
3245 | return ret; | |
3246 | } | |
2b188cc1 | 3247 | |
227c0c96 | 3248 | ret = io_iter_do_read(req, iter); |
32960613 | 3249 | |
57cd657b | 3250 | if (ret == -EIOCBQUEUED) { |
3e6a0d3c JA |
3251 | if (req->async_data) |
3252 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); | |
fe1cdd55 | 3253 | goto out_free; |
227c0c96 | 3254 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3255 | /* IOPOLL retry should happen for io-wq threads */ |
3256 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3257 | goto done; |
75c668cd PB |
3258 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3259 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3260 | goto done; |
84216315 | 3261 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3262 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3263 | ret = 0; |
7335e3bf | 3264 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3265 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3266 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3267 | goto done; |
227c0c96 JA |
3268 | } |
3269 | ||
227c0c96 | 3270 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3271 | if (ret2) |
3272 | return ret2; | |
3273 | ||
fe1cdd55 | 3274 | iovec = NULL; |
e8c2bc1f | 3275 | rw = req->async_data; |
227c0c96 | 3276 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3277 | iter = &rw->iter; |
227c0c96 | 3278 | |
b23df91b PB |
3279 | do { |
3280 | io_size -= ret; | |
3281 | rw->bytes_done += ret; | |
3282 | /* if we can retry, do so with the callbacks armed */ | |
3283 | if (!io_rw_should_retry(req)) { | |
3284 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3285 | return -EAGAIN; | |
3286 | } | |
3287 | ||
3288 | /* | |
3289 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3290 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3291 | * desired page gets unlocked. We can also get a partial read | |
3292 | * here, and if we do, then just retry at the new offset. | |
3293 | */ | |
3294 | ret = io_iter_do_read(req, iter); | |
3295 | if (ret == -EIOCBQUEUED) | |
3296 | return 0; | |
227c0c96 | 3297 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3298 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3299 | done: |
889fca73 | 3300 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3301 | out_free: |
3302 | /* it's faster to check here then delegate to kfree */ | |
3303 | if (iovec) | |
3304 | kfree(iovec); | |
5ea5dd45 | 3305 | return 0; |
2b188cc1 JA |
3306 | } |
3307 | ||
73debe68 | 3308 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3309 | { |
3529d8c2 JA |
3310 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3311 | return -EBADF; | |
93642ef8 | 3312 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3313 | } |
3314 | ||
889fca73 | 3315 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3316 | { |
3317 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3318 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3319 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3320 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3321 | ssize_t ret, ret2, io_size; |
45d189c6 | 3322 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3323 | |
2846c481 | 3324 | if (rw) { |
e8c2bc1f | 3325 | iter = &rw->iter; |
2846c481 PB |
3326 | iovec = NULL; |
3327 | } else { | |
3328 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3329 | if (ret < 0) | |
3330 | return ret; | |
3331 | } | |
632546c4 | 3332 | io_size = iov_iter_count(iter); |
fa15bafb | 3333 | req->result = io_size; |
2b188cc1 | 3334 | |
fd6c2e4c JA |
3335 | /* Ensure we clear previously set non-block flag */ |
3336 | if (!force_nonblock) | |
a88fc400 PB |
3337 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3338 | else | |
3339 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3340 | |
24c74678 | 3341 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3342 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3343 | goto copy_iov; |
31b51510 | 3344 | |
10d59345 JA |
3345 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3346 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3347 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3348 | goto copy_iov; |
31b51510 | 3349 | |
632546c4 | 3350 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3351 | if (unlikely(ret)) |
3352 | goto out_free; | |
4ed734b0 | 3353 | |
fa15bafb PB |
3354 | /* |
3355 | * Open-code file_start_write here to grab freeze protection, | |
3356 | * which will be released by another thread in | |
3357 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3358 | * released so that it doesn't complain about the held lock when | |
3359 | * we return to userspace. | |
3360 | */ | |
3361 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3362 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3363 | __sb_writers_release(file_inode(req->file)->i_sb, |
3364 | SB_FREEZE_WRITE); | |
3365 | } | |
3366 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3367 | |
fa15bafb | 3368 | if (req->file->f_op->write_iter) |
ff6165b2 | 3369 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3370 | else if (req->file->f_op->write) |
4017eb91 | 3371 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3372 | else |
3373 | ret2 = -EINVAL; | |
4ed734b0 | 3374 | |
fa15bafb PB |
3375 | /* |
3376 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3377 | * retry them without IOCB_NOWAIT. | |
3378 | */ | |
3379 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3380 | ret2 = -EAGAIN; | |
75c668cd PB |
3381 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3382 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3383 | goto done; |
3e6a0d3c JA |
3384 | if (ret2 == -EIOCBQUEUED && req->async_data) |
3385 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); | |
fa15bafb | 3386 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3387 | /* IOPOLL retry should happen for io-wq threads */ |
3388 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3389 | goto copy_iov; | |
355afaeb | 3390 | done: |
889fca73 | 3391 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3392 | } else { |
f67676d1 | 3393 | copy_iov: |
84216315 | 3394 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3395 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3396 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3397 | return ret ?: -EAGAIN; |
2b188cc1 | 3398 | } |
31b51510 | 3399 | out_free: |
f261c168 | 3400 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3401 | if (iovec) |
6f2cc166 | 3402 | kfree(iovec); |
2b188cc1 JA |
3403 | return ret; |
3404 | } | |
3405 | ||
80a261fd JA |
3406 | static int io_renameat_prep(struct io_kiocb *req, |
3407 | const struct io_uring_sqe *sqe) | |
3408 | { | |
3409 | struct io_rename *ren = &req->rename; | |
3410 | const char __user *oldf, *newf; | |
3411 | ||
3412 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3413 | return -EBADF; | |
3414 | ||
3415 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3416 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3417 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3418 | ren->new_dfd = READ_ONCE(sqe->len); | |
3419 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3420 | ||
3421 | ren->oldpath = getname(oldf); | |
3422 | if (IS_ERR(ren->oldpath)) | |
3423 | return PTR_ERR(ren->oldpath); | |
3424 | ||
3425 | ren->newpath = getname(newf); | |
3426 | if (IS_ERR(ren->newpath)) { | |
3427 | putname(ren->oldpath); | |
3428 | return PTR_ERR(ren->newpath); | |
3429 | } | |
3430 | ||
3431 | req->flags |= REQ_F_NEED_CLEANUP; | |
3432 | return 0; | |
3433 | } | |
3434 | ||
45d189c6 | 3435 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3436 | { |
3437 | struct io_rename *ren = &req->rename; | |
3438 | int ret; | |
3439 | ||
45d189c6 | 3440 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3441 | return -EAGAIN; |
3442 | ||
3443 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3444 | ren->newpath, ren->flags); | |
3445 | ||
3446 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3447 | if (ret < 0) | |
3448 | req_set_fail_links(req); | |
3449 | io_req_complete(req, ret); | |
3450 | return 0; | |
3451 | } | |
3452 | ||
14a1143b JA |
3453 | static int io_unlinkat_prep(struct io_kiocb *req, |
3454 | const struct io_uring_sqe *sqe) | |
3455 | { | |
3456 | struct io_unlink *un = &req->unlink; | |
3457 | const char __user *fname; | |
3458 | ||
3459 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3460 | return -EBADF; | |
3461 | ||
3462 | un->dfd = READ_ONCE(sqe->fd); | |
3463 | ||
3464 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3465 | if (un->flags & ~AT_REMOVEDIR) | |
3466 | return -EINVAL; | |
3467 | ||
3468 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3469 | un->filename = getname(fname); | |
3470 | if (IS_ERR(un->filename)) | |
3471 | return PTR_ERR(un->filename); | |
3472 | ||
3473 | req->flags |= REQ_F_NEED_CLEANUP; | |
3474 | return 0; | |
3475 | } | |
3476 | ||
45d189c6 | 3477 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3478 | { |
3479 | struct io_unlink *un = &req->unlink; | |
3480 | int ret; | |
3481 | ||
45d189c6 | 3482 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3483 | return -EAGAIN; |
3484 | ||
3485 | if (un->flags & AT_REMOVEDIR) | |
3486 | ret = do_rmdir(un->dfd, un->filename); | |
3487 | else | |
3488 | ret = do_unlinkat(un->dfd, un->filename); | |
3489 | ||
3490 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3491 | if (ret < 0) | |
3492 | req_set_fail_links(req); | |
3493 | io_req_complete(req, ret); | |
3494 | return 0; | |
3495 | } | |
3496 | ||
36f4fa68 JA |
3497 | static int io_shutdown_prep(struct io_kiocb *req, |
3498 | const struct io_uring_sqe *sqe) | |
3499 | { | |
3500 | #if defined(CONFIG_NET) | |
3501 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3502 | return -EINVAL; | |
3503 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3504 | sqe->buf_index) | |
3505 | return -EINVAL; | |
3506 | ||
3507 | req->shutdown.how = READ_ONCE(sqe->len); | |
3508 | return 0; | |
3509 | #else | |
3510 | return -EOPNOTSUPP; | |
3511 | #endif | |
3512 | } | |
3513 | ||
45d189c6 | 3514 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3515 | { |
3516 | #if defined(CONFIG_NET) | |
3517 | struct socket *sock; | |
3518 | int ret; | |
3519 | ||
45d189c6 | 3520 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3521 | return -EAGAIN; |
3522 | ||
48aba79b | 3523 | sock = sock_from_file(req->file); |
36f4fa68 | 3524 | if (unlikely(!sock)) |
48aba79b | 3525 | return -ENOTSOCK; |
36f4fa68 JA |
3526 | |
3527 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3528 | if (ret < 0) |
3529 | req_set_fail_links(req); | |
36f4fa68 JA |
3530 | io_req_complete(req, ret); |
3531 | return 0; | |
3532 | #else | |
3533 | return -EOPNOTSUPP; | |
3534 | #endif | |
3535 | } | |
3536 | ||
f2a8d5c7 PB |
3537 | static int __io_splice_prep(struct io_kiocb *req, |
3538 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3539 | { |
3540 | struct io_splice* sp = &req->splice; | |
3541 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3542 | |
3232dd02 PB |
3543 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3544 | return -EINVAL; | |
7d67af2c PB |
3545 | |
3546 | sp->file_in = NULL; | |
7d67af2c PB |
3547 | sp->len = READ_ONCE(sqe->len); |
3548 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3549 | ||
3550 | if (unlikely(sp->flags & ~valid_flags)) | |
3551 | return -EINVAL; | |
3552 | ||
8371adf5 PB |
3553 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3554 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3555 | if (!sp->file_in) | |
3556 | return -EBADF; | |
7d67af2c PB |
3557 | req->flags |= REQ_F_NEED_CLEANUP; |
3558 | ||
7cdaf587 XW |
3559 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3560 | /* | |
3561 | * Splice operation will be punted aync, and here need to | |
3562 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3563 | */ | |
7d67af2c | 3564 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3565 | } |
7d67af2c PB |
3566 | |
3567 | return 0; | |
3568 | } | |
3569 | ||
f2a8d5c7 PB |
3570 | static int io_tee_prep(struct io_kiocb *req, |
3571 | const struct io_uring_sqe *sqe) | |
3572 | { | |
3573 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3574 | return -EINVAL; | |
3575 | return __io_splice_prep(req, sqe); | |
3576 | } | |
3577 | ||
45d189c6 | 3578 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3579 | { |
3580 | struct io_splice *sp = &req->splice; | |
3581 | struct file *in = sp->file_in; | |
3582 | struct file *out = sp->file_out; | |
3583 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3584 | long ret = 0; | |
3585 | ||
45d189c6 | 3586 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3587 | return -EAGAIN; |
3588 | if (sp->len) | |
3589 | ret = do_tee(in, out, sp->len, flags); | |
3590 | ||
3591 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3592 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3593 | ||
f2a8d5c7 PB |
3594 | if (ret != sp->len) |
3595 | req_set_fail_links(req); | |
e1e16097 | 3596 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3597 | return 0; |
3598 | } | |
3599 | ||
3600 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3601 | { | |
3602 | struct io_splice* sp = &req->splice; | |
3603 | ||
3604 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3605 | sp->off_out = READ_ONCE(sqe->off); | |
3606 | return __io_splice_prep(req, sqe); | |
3607 | } | |
3608 | ||
45d189c6 | 3609 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3610 | { |
3611 | struct io_splice *sp = &req->splice; | |
3612 | struct file *in = sp->file_in; | |
3613 | struct file *out = sp->file_out; | |
3614 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3615 | loff_t *poff_in, *poff_out; | |
c9687426 | 3616 | long ret = 0; |
7d67af2c | 3617 | |
45d189c6 | 3618 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3619 | return -EAGAIN; |
7d67af2c PB |
3620 | |
3621 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3622 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3623 | |
948a7749 | 3624 | if (sp->len) |
c9687426 | 3625 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3626 | |
3627 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3628 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3629 | ||
7d67af2c PB |
3630 | if (ret != sp->len) |
3631 | req_set_fail_links(req); | |
e1e16097 | 3632 | io_req_complete(req, ret); |
7d67af2c PB |
3633 | return 0; |
3634 | } | |
3635 | ||
2b188cc1 JA |
3636 | /* |
3637 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3638 | */ | |
889fca73 | 3639 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3640 | { |
3641 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3642 | |
def596e9 JA |
3643 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3644 | return -EINVAL; | |
3645 | ||
889fca73 | 3646 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3647 | return 0; |
3648 | } | |
3649 | ||
1155c76a | 3650 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3651 | { |
6b06314c | 3652 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3653 | |
09bb8394 JA |
3654 | if (!req->file) |
3655 | return -EBADF; | |
c992fe29 | 3656 | |
6b06314c | 3657 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3658 | return -EINVAL; |
edafccee | 3659 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3660 | return -EINVAL; |
3661 | ||
8ed8d3c3 JA |
3662 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3663 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3664 | return -EINVAL; | |
3665 | ||
3666 | req->sync.off = READ_ONCE(sqe->off); | |
3667 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3668 | return 0; |
3669 | } | |
3670 | ||
45d189c6 | 3671 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3672 | { |
8ed8d3c3 | 3673 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3674 | int ret; |
3675 | ||
ac45abc0 | 3676 | /* fsync always requires a blocking context */ |
45d189c6 | 3677 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3678 | return -EAGAIN; |
3679 | ||
9adbd45d | 3680 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3681 | end > 0 ? end : LLONG_MAX, |
3682 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3683 | if (ret < 0) | |
3684 | req_set_fail_links(req); | |
e1e16097 | 3685 | io_req_complete(req, ret); |
c992fe29 CH |
3686 | return 0; |
3687 | } | |
3688 | ||
d63d1b5e JA |
3689 | static int io_fallocate_prep(struct io_kiocb *req, |
3690 | const struct io_uring_sqe *sqe) | |
3691 | { | |
3692 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3693 | return -EINVAL; | |
3232dd02 PB |
3694 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3695 | return -EINVAL; | |
d63d1b5e JA |
3696 | |
3697 | req->sync.off = READ_ONCE(sqe->off); | |
3698 | req->sync.len = READ_ONCE(sqe->addr); | |
3699 | req->sync.mode = READ_ONCE(sqe->len); | |
3700 | return 0; | |
3701 | } | |
3702 | ||
45d189c6 | 3703 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3704 | { |
ac45abc0 PB |
3705 | int ret; |
3706 | ||
d63d1b5e | 3707 | /* fallocate always requiring blocking context */ |
45d189c6 | 3708 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3709 | return -EAGAIN; |
ac45abc0 PB |
3710 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3711 | req->sync.len); | |
ac45abc0 PB |
3712 | if (ret < 0) |
3713 | req_set_fail_links(req); | |
e1e16097 | 3714 | io_req_complete(req, ret); |
5d17b4a4 JA |
3715 | return 0; |
3716 | } | |
3717 | ||
ec65fea5 | 3718 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3719 | { |
f8748881 | 3720 | const char __user *fname; |
15b71abe | 3721 | int ret; |
b7bb4f7d | 3722 | |
ec65fea5 | 3723 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3724 | return -EINVAL; |
ec65fea5 | 3725 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3726 | return -EBADF; |
03b1230c | 3727 | |
ec65fea5 PB |
3728 | /* open.how should be already initialised */ |
3729 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3730 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3731 | |
25e72d10 PB |
3732 | req->open.dfd = READ_ONCE(sqe->fd); |
3733 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3734 | req->open.filename = getname(fname); |
15b71abe JA |
3735 | if (IS_ERR(req->open.filename)) { |
3736 | ret = PTR_ERR(req->open.filename); | |
3737 | req->open.filename = NULL; | |
3738 | return ret; | |
3739 | } | |
4022e7af | 3740 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3741 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3742 | return 0; |
03b1230c JA |
3743 | } |
3744 | ||
ec65fea5 PB |
3745 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3746 | { | |
3747 | u64 flags, mode; | |
3748 | ||
14587a46 | 3749 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3750 | return -EINVAL; |
ec65fea5 PB |
3751 | mode = READ_ONCE(sqe->len); |
3752 | flags = READ_ONCE(sqe->open_flags); | |
3753 | req->open.how = build_open_how(flags, mode); | |
3754 | return __io_openat_prep(req, sqe); | |
3755 | } | |
3756 | ||
cebdb986 | 3757 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3758 | { |
cebdb986 | 3759 | struct open_how __user *how; |
cebdb986 | 3760 | size_t len; |
0fa03c62 JA |
3761 | int ret; |
3762 | ||
14587a46 | 3763 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3764 | return -EINVAL; |
cebdb986 JA |
3765 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3766 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3767 | if (len < OPEN_HOW_SIZE_VER0) |
3768 | return -EINVAL; | |
3529d8c2 | 3769 | |
cebdb986 JA |
3770 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3771 | len); | |
3772 | if (ret) | |
3773 | return ret; | |
3529d8c2 | 3774 | |
ec65fea5 | 3775 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3776 | } |
3777 | ||
45d189c6 | 3778 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3779 | { |
3780 | struct open_flags op; | |
15b71abe | 3781 | struct file *file; |
3a81fd02 JA |
3782 | bool nonblock_set; |
3783 | bool resolve_nonblock; | |
15b71abe JA |
3784 | int ret; |
3785 | ||
cebdb986 | 3786 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3787 | if (ret) |
3788 | goto err; | |
3a81fd02 JA |
3789 | nonblock_set = op.open_flag & O_NONBLOCK; |
3790 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3791 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3792 | /* |
3793 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3794 | * it'll always -EAGAIN | |
3795 | */ | |
3796 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3797 | return -EAGAIN; | |
3798 | op.lookup_flags |= LOOKUP_CACHED; | |
3799 | op.open_flag |= O_NONBLOCK; | |
3800 | } | |
15b71abe | 3801 | |
4022e7af | 3802 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3803 | if (ret < 0) |
3804 | goto err; | |
3805 | ||
3806 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3807 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3808 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3809 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3810 | /* |
3a81fd02 JA |
3811 | * We could hang on to this 'fd', but seems like marginal |
3812 | * gain for something that is now known to be a slower path. | |
3813 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3814 | */ |
3a81fd02 JA |
3815 | put_unused_fd(ret); |
3816 | return -EAGAIN; | |
3817 | } | |
3818 | ||
15b71abe JA |
3819 | if (IS_ERR(file)) { |
3820 | put_unused_fd(ret); | |
3821 | ret = PTR_ERR(file); | |
3822 | } else { | |
45d189c6 | 3823 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3824 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3825 | fsnotify_open(file); |
3826 | fd_install(ret, file); | |
3827 | } | |
3828 | err: | |
3829 | putname(req->open.filename); | |
8fef80bf | 3830 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3831 | if (ret < 0) |
3832 | req_set_fail_links(req); | |
e1e16097 | 3833 | io_req_complete(req, ret); |
15b71abe JA |
3834 | return 0; |
3835 | } | |
3836 | ||
45d189c6 | 3837 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3838 | { |
45d189c6 | 3839 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
3840 | } |
3841 | ||
067524e9 JA |
3842 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3843 | const struct io_uring_sqe *sqe) | |
3844 | { | |
3845 | struct io_provide_buf *p = &req->pbuf; | |
3846 | u64 tmp; | |
3847 | ||
3848 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3849 | return -EINVAL; | |
3850 | ||
3851 | tmp = READ_ONCE(sqe->fd); | |
3852 | if (!tmp || tmp > USHRT_MAX) | |
3853 | return -EINVAL; | |
3854 | ||
3855 | memset(p, 0, sizeof(*p)); | |
3856 | p->nbufs = tmp; | |
3857 | p->bgid = READ_ONCE(sqe->buf_group); | |
3858 | return 0; | |
3859 | } | |
3860 | ||
3861 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3862 | int bgid, unsigned nbufs) | |
3863 | { | |
3864 | unsigned i = 0; | |
3865 | ||
3866 | /* shouldn't happen */ | |
3867 | if (!nbufs) | |
3868 | return 0; | |
3869 | ||
3870 | /* the head kbuf is the list itself */ | |
3871 | while (!list_empty(&buf->list)) { | |
3872 | struct io_buffer *nxt; | |
3873 | ||
3874 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3875 | list_del(&nxt->list); | |
3876 | kfree(nxt); | |
3877 | if (++i == nbufs) | |
3878 | return i; | |
3879 | } | |
3880 | i++; | |
3881 | kfree(buf); | |
3882 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3883 | ||
3884 | return i; | |
3885 | } | |
3886 | ||
889fca73 | 3887 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3888 | { |
3889 | struct io_provide_buf *p = &req->pbuf; | |
3890 | struct io_ring_ctx *ctx = req->ctx; | |
3891 | struct io_buffer *head; | |
3892 | int ret = 0; | |
45d189c6 | 3893 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3894 | |
3895 | io_ring_submit_lock(ctx, !force_nonblock); | |
3896 | ||
3897 | lockdep_assert_held(&ctx->uring_lock); | |
3898 | ||
3899 | ret = -ENOENT; | |
3900 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3901 | if (head) | |
3902 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3903 | if (ret < 0) |
3904 | req_set_fail_links(req); | |
067524e9 | 3905 | |
31bff9a5 PB |
3906 | /* need to hold the lock to complete IOPOLL requests */ |
3907 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 3908 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
3909 | io_ring_submit_unlock(ctx, !force_nonblock); |
3910 | } else { | |
3911 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 3912 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 3913 | } |
067524e9 JA |
3914 | return 0; |
3915 | } | |
3916 | ||
ddf0322d JA |
3917 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3918 | const struct io_uring_sqe *sqe) | |
3919 | { | |
3920 | struct io_provide_buf *p = &req->pbuf; | |
3921 | u64 tmp; | |
3922 | ||
3923 | if (sqe->ioprio || sqe->rw_flags) | |
3924 | return -EINVAL; | |
3925 | ||
3926 | tmp = READ_ONCE(sqe->fd); | |
3927 | if (!tmp || tmp > USHRT_MAX) | |
3928 | return -E2BIG; | |
3929 | p->nbufs = tmp; | |
3930 | p->addr = READ_ONCE(sqe->addr); | |
3931 | p->len = READ_ONCE(sqe->len); | |
3932 | ||
efe68c1c | 3933 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3934 | return -EFAULT; |
3935 | ||
3936 | p->bgid = READ_ONCE(sqe->buf_group); | |
3937 | tmp = READ_ONCE(sqe->off); | |
3938 | if (tmp > USHRT_MAX) | |
3939 | return -E2BIG; | |
3940 | p->bid = tmp; | |
3941 | return 0; | |
3942 | } | |
3943 | ||
3944 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3945 | { | |
3946 | struct io_buffer *buf; | |
3947 | u64 addr = pbuf->addr; | |
3948 | int i, bid = pbuf->bid; | |
3949 | ||
3950 | for (i = 0; i < pbuf->nbufs; i++) { | |
3951 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3952 | if (!buf) | |
3953 | break; | |
3954 | ||
3955 | buf->addr = addr; | |
3956 | buf->len = pbuf->len; | |
3957 | buf->bid = bid; | |
3958 | addr += pbuf->len; | |
3959 | bid++; | |
3960 | if (!*head) { | |
3961 | INIT_LIST_HEAD(&buf->list); | |
3962 | *head = buf; | |
3963 | } else { | |
3964 | list_add_tail(&buf->list, &(*head)->list); | |
3965 | } | |
3966 | } | |
3967 | ||
3968 | return i ? i : -ENOMEM; | |
3969 | } | |
3970 | ||
889fca73 | 3971 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
3972 | { |
3973 | struct io_provide_buf *p = &req->pbuf; | |
3974 | struct io_ring_ctx *ctx = req->ctx; | |
3975 | struct io_buffer *head, *list; | |
3976 | int ret = 0; | |
45d189c6 | 3977 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
3978 | |
3979 | io_ring_submit_lock(ctx, !force_nonblock); | |
3980 | ||
3981 | lockdep_assert_held(&ctx->uring_lock); | |
3982 | ||
3983 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3984 | ||
3985 | ret = io_add_buffers(p, &head); | |
3986 | if (ret < 0) | |
3987 | goto out; | |
3988 | ||
3989 | if (!list) { | |
3990 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
3991 | GFP_KERNEL); | |
3992 | if (ret < 0) { | |
067524e9 | 3993 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
3994 | goto out; |
3995 | } | |
3996 | } | |
3997 | out: | |
ddf0322d JA |
3998 | if (ret < 0) |
3999 | req_set_fail_links(req); | |
31bff9a5 PB |
4000 | |
4001 | /* need to hold the lock to complete IOPOLL requests */ | |
4002 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4003 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4004 | io_ring_submit_unlock(ctx, !force_nonblock); |
4005 | } else { | |
4006 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4007 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4008 | } |
ddf0322d | 4009 | return 0; |
cebdb986 JA |
4010 | } |
4011 | ||
3e4827b0 JA |
4012 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4013 | const struct io_uring_sqe *sqe) | |
4014 | { | |
4015 | #if defined(CONFIG_EPOLL) | |
4016 | if (sqe->ioprio || sqe->buf_index) | |
4017 | return -EINVAL; | |
6ca56f84 | 4018 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4019 | return -EINVAL; |
3e4827b0 JA |
4020 | |
4021 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4022 | req->epoll.op = READ_ONCE(sqe->len); | |
4023 | req->epoll.fd = READ_ONCE(sqe->off); | |
4024 | ||
4025 | if (ep_op_has_event(req->epoll.op)) { | |
4026 | struct epoll_event __user *ev; | |
4027 | ||
4028 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4029 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4030 | return -EFAULT; | |
4031 | } | |
4032 | ||
4033 | return 0; | |
4034 | #else | |
4035 | return -EOPNOTSUPP; | |
4036 | #endif | |
4037 | } | |
4038 | ||
889fca73 | 4039 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4040 | { |
4041 | #if defined(CONFIG_EPOLL) | |
4042 | struct io_epoll *ie = &req->epoll; | |
4043 | int ret; | |
45d189c6 | 4044 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4045 | |
4046 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4047 | if (force_nonblock && ret == -EAGAIN) | |
4048 | return -EAGAIN; | |
4049 | ||
4050 | if (ret < 0) | |
4051 | req_set_fail_links(req); | |
889fca73 | 4052 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4053 | return 0; |
4054 | #else | |
4055 | return -EOPNOTSUPP; | |
4056 | #endif | |
4057 | } | |
4058 | ||
c1ca757b JA |
4059 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4060 | { | |
4061 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4062 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4063 | return -EINVAL; | |
3232dd02 PB |
4064 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4065 | return -EINVAL; | |
c1ca757b JA |
4066 | |
4067 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4068 | req->madvise.len = READ_ONCE(sqe->len); | |
4069 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4070 | return 0; | |
4071 | #else | |
4072 | return -EOPNOTSUPP; | |
4073 | #endif | |
4074 | } | |
4075 | ||
45d189c6 | 4076 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4077 | { |
4078 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4079 | struct io_madvise *ma = &req->madvise; | |
4080 | int ret; | |
4081 | ||
45d189c6 | 4082 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4083 | return -EAGAIN; |
4084 | ||
0726b01e | 4085 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4086 | if (ret < 0) |
4087 | req_set_fail_links(req); | |
e1e16097 | 4088 | io_req_complete(req, ret); |
c1ca757b JA |
4089 | return 0; |
4090 | #else | |
4091 | return -EOPNOTSUPP; | |
4092 | #endif | |
4093 | } | |
4094 | ||
4840e418 JA |
4095 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4096 | { | |
4097 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4098 | return -EINVAL; | |
3232dd02 PB |
4099 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4100 | return -EINVAL; | |
4840e418 JA |
4101 | |
4102 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4103 | req->fadvise.len = READ_ONCE(sqe->len); | |
4104 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4105 | return 0; | |
4106 | } | |
4107 | ||
45d189c6 | 4108 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4109 | { |
4110 | struct io_fadvise *fa = &req->fadvise; | |
4111 | int ret; | |
4112 | ||
45d189c6 | 4113 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4114 | switch (fa->advice) { |
4115 | case POSIX_FADV_NORMAL: | |
4116 | case POSIX_FADV_RANDOM: | |
4117 | case POSIX_FADV_SEQUENTIAL: | |
4118 | break; | |
4119 | default: | |
4120 | return -EAGAIN; | |
4121 | } | |
4122 | } | |
4840e418 JA |
4123 | |
4124 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4125 | if (ret < 0) | |
4126 | req_set_fail_links(req); | |
e1e16097 | 4127 | io_req_complete(req, ret); |
4840e418 JA |
4128 | return 0; |
4129 | } | |
4130 | ||
eddc7ef5 JA |
4131 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4132 | { | |
6ca56f84 | 4133 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4134 | return -EINVAL; |
eddc7ef5 JA |
4135 | if (sqe->ioprio || sqe->buf_index) |
4136 | return -EINVAL; | |
9c280f90 | 4137 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4138 | return -EBADF; |
eddc7ef5 | 4139 | |
1d9e1288 BM |
4140 | req->statx.dfd = READ_ONCE(sqe->fd); |
4141 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4142 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4143 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4144 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4145 | |
4146 | return 0; | |
4147 | } | |
4148 | ||
45d189c6 | 4149 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4150 | { |
1d9e1288 | 4151 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4152 | int ret; |
4153 | ||
45d189c6 | 4154 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4155 | /* only need file table for an actual valid fd */ |
4156 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4157 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4158 | return -EAGAIN; |
5b0bbee4 | 4159 | } |
eddc7ef5 | 4160 | |
e62753e4 BM |
4161 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4162 | ctx->buffer); | |
eddc7ef5 | 4163 | |
eddc7ef5 JA |
4164 | if (ret < 0) |
4165 | req_set_fail_links(req); | |
e1e16097 | 4166 | io_req_complete(req, ret); |
eddc7ef5 JA |
4167 | return 0; |
4168 | } | |
4169 | ||
b5dba59e JA |
4170 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4171 | { | |
14587a46 | 4172 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4173 | return -EINVAL; |
b5dba59e JA |
4174 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4175 | sqe->rw_flags || sqe->buf_index) | |
4176 | return -EINVAL; | |
9c280f90 | 4177 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4178 | return -EBADF; |
b5dba59e JA |
4179 | |
4180 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4181 | return 0; |
b5dba59e JA |
4182 | } |
4183 | ||
889fca73 | 4184 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4185 | { |
9eac1904 | 4186 | struct files_struct *files = current->files; |
3af73b28 | 4187 | struct io_close *close = &req->close; |
9eac1904 JA |
4188 | struct fdtable *fdt; |
4189 | struct file *file; | |
b5dba59e JA |
4190 | int ret; |
4191 | ||
9eac1904 JA |
4192 | file = NULL; |
4193 | ret = -EBADF; | |
4194 | spin_lock(&files->file_lock); | |
4195 | fdt = files_fdtable(files); | |
4196 | if (close->fd >= fdt->max_fds) { | |
4197 | spin_unlock(&files->file_lock); | |
4198 | goto err; | |
4199 | } | |
4200 | file = fdt->fd[close->fd]; | |
4201 | if (!file) { | |
4202 | spin_unlock(&files->file_lock); | |
4203 | goto err; | |
4204 | } | |
4205 | ||
4206 | if (file->f_op == &io_uring_fops) { | |
4207 | spin_unlock(&files->file_lock); | |
4208 | file = NULL; | |
4209 | goto err; | |
3af73b28 | 4210 | } |
b5dba59e JA |
4211 | |
4212 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4213 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4214 | spin_unlock(&files->file_lock); |
0bf0eefd | 4215 | return -EAGAIN; |
a2100672 | 4216 | } |
b5dba59e | 4217 | |
9eac1904 JA |
4218 | ret = __close_fd_get_file(close->fd, &file); |
4219 | spin_unlock(&files->file_lock); | |
4220 | if (ret < 0) { | |
4221 | if (ret == -ENOENT) | |
4222 | ret = -EBADF; | |
4223 | goto err; | |
4224 | } | |
4225 | ||
3af73b28 | 4226 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4227 | ret = filp_close(file, current->files); |
4228 | err: | |
3af73b28 PB |
4229 | if (ret < 0) |
4230 | req_set_fail_links(req); | |
9eac1904 JA |
4231 | if (file) |
4232 | fput(file); | |
889fca73 | 4233 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4234 | return 0; |
b5dba59e JA |
4235 | } |
4236 | ||
1155c76a | 4237 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4238 | { |
4239 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4240 | |
5d17b4a4 JA |
4241 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4242 | return -EINVAL; | |
4243 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4244 | return -EINVAL; | |
4245 | ||
8ed8d3c3 JA |
4246 | req->sync.off = READ_ONCE(sqe->off); |
4247 | req->sync.len = READ_ONCE(sqe->len); | |
4248 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4249 | return 0; |
4250 | } | |
4251 | ||
45d189c6 | 4252 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4253 | { |
8ed8d3c3 JA |
4254 | int ret; |
4255 | ||
ac45abc0 | 4256 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4257 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4258 | return -EAGAIN; |
4259 | ||
9adbd45d | 4260 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4261 | req->sync.flags); |
4262 | if (ret < 0) | |
4263 | req_set_fail_links(req); | |
e1e16097 | 4264 | io_req_complete(req, ret); |
5d17b4a4 JA |
4265 | return 0; |
4266 | } | |
4267 | ||
469956e8 | 4268 | #if defined(CONFIG_NET) |
02d27d89 PB |
4269 | static int io_setup_async_msg(struct io_kiocb *req, |
4270 | struct io_async_msghdr *kmsg) | |
4271 | { | |
e8c2bc1f JA |
4272 | struct io_async_msghdr *async_msg = req->async_data; |
4273 | ||
4274 | if (async_msg) | |
02d27d89 | 4275 | return -EAGAIN; |
e8c2bc1f | 4276 | if (io_alloc_async_data(req)) { |
257e84a5 | 4277 | kfree(kmsg->free_iov); |
02d27d89 PB |
4278 | return -ENOMEM; |
4279 | } | |
e8c2bc1f | 4280 | async_msg = req->async_data; |
02d27d89 | 4281 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4282 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4283 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4284 | /* if were using fast_iov, set it to the new one */ |
4285 | if (!async_msg->free_iov) | |
4286 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4287 | ||
02d27d89 PB |
4288 | return -EAGAIN; |
4289 | } | |
4290 | ||
2ae523ed PB |
4291 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4292 | struct io_async_msghdr *iomsg) | |
4293 | { | |
2ae523ed | 4294 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4295 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4296 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4297 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4298 | } |
4299 | ||
93642ef8 PB |
4300 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4301 | { | |
4302 | int ret; | |
4303 | ||
4304 | if (!io_op_defs[req->opcode].needs_async_data) | |
4305 | return 0; | |
4306 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4307 | if (!ret) | |
4308 | req->flags |= REQ_F_NEED_CLEANUP; | |
4309 | return ret; | |
4310 | } | |
4311 | ||
3529d8c2 | 4312 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4313 | { |
e47293fd | 4314 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4315 | |
d2b6f48b PB |
4316 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4317 | return -EINVAL; | |
4318 | ||
e47293fd | 4319 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4320 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4321 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4322 | |
d8768362 JA |
4323 | #ifdef CONFIG_COMPAT |
4324 | if (req->ctx->compat) | |
4325 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4326 | #endif | |
93642ef8 | 4327 | return 0; |
03b1230c JA |
4328 | } |
4329 | ||
889fca73 | 4330 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4331 | { |
6b754c8b | 4332 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4333 | struct socket *sock; |
7a7cacba | 4334 | unsigned flags; |
0fa03c62 JA |
4335 | int ret; |
4336 | ||
dba4a925 | 4337 | sock = sock_from_file(req->file); |
7a7cacba | 4338 | if (unlikely(!sock)) |
dba4a925 | 4339 | return -ENOTSOCK; |
3529d8c2 | 4340 | |
257e84a5 PB |
4341 | kmsg = req->async_data; |
4342 | if (!kmsg) { | |
7a7cacba PB |
4343 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4344 | if (ret) | |
4345 | return ret; | |
4346 | kmsg = &iomsg; | |
0fa03c62 | 4347 | } |
0fa03c62 | 4348 | |
7a7cacba PB |
4349 | flags = req->sr_msg.msg_flags; |
4350 | if (flags & MSG_DONTWAIT) | |
4351 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4352 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4353 | flags |= MSG_DONTWAIT; |
e47293fd | 4354 | |
7a7cacba | 4355 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4356 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4357 | return io_setup_async_msg(req, kmsg); |
4358 | if (ret == -ERESTARTSYS) | |
4359 | ret = -EINTR; | |
0fa03c62 | 4360 | |
257e84a5 PB |
4361 | /* fast path, check for non-NULL to avoid function call */ |
4362 | if (kmsg->free_iov) | |
4363 | kfree(kmsg->free_iov); | |
99bc4c38 | 4364 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4365 | if (ret < 0) |
4366 | req_set_fail_links(req); | |
889fca73 | 4367 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4368 | return 0; |
03b1230c | 4369 | } |
aa1fa28f | 4370 | |
889fca73 | 4371 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4372 | { |
7a7cacba PB |
4373 | struct io_sr_msg *sr = &req->sr_msg; |
4374 | struct msghdr msg; | |
4375 | struct iovec iov; | |
fddaface | 4376 | struct socket *sock; |
7a7cacba | 4377 | unsigned flags; |
fddaface JA |
4378 | int ret; |
4379 | ||
dba4a925 | 4380 | sock = sock_from_file(req->file); |
7a7cacba | 4381 | if (unlikely(!sock)) |
dba4a925 | 4382 | return -ENOTSOCK; |
fddaface | 4383 | |
7a7cacba PB |
4384 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4385 | if (unlikely(ret)) | |
14db8411 | 4386 | return ret; |
fddaface | 4387 | |
7a7cacba PB |
4388 | msg.msg_name = NULL; |
4389 | msg.msg_control = NULL; | |
4390 | msg.msg_controllen = 0; | |
4391 | msg.msg_namelen = 0; | |
fddaface | 4392 | |
7a7cacba PB |
4393 | flags = req->sr_msg.msg_flags; |
4394 | if (flags & MSG_DONTWAIT) | |
4395 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4396 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4397 | flags |= MSG_DONTWAIT; |
fddaface | 4398 | |
7a7cacba PB |
4399 | msg.msg_flags = flags; |
4400 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4401 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4402 | return -EAGAIN; |
4403 | if (ret == -ERESTARTSYS) | |
4404 | ret = -EINTR; | |
fddaface | 4405 | |
fddaface JA |
4406 | if (ret < 0) |
4407 | req_set_fail_links(req); | |
889fca73 | 4408 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4409 | return 0; |
fddaface JA |
4410 | } |
4411 | ||
1400e697 PB |
4412 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4413 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4414 | { |
4415 | struct io_sr_msg *sr = &req->sr_msg; | |
4416 | struct iovec __user *uiov; | |
4417 | size_t iov_len; | |
4418 | int ret; | |
4419 | ||
1400e697 PB |
4420 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4421 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4422 | if (ret) |
4423 | return ret; | |
4424 | ||
4425 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4426 | if (iov_len > 1) | |
4427 | return -EINVAL; | |
5476dfed | 4428 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4429 | return -EFAULT; |
5476dfed | 4430 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4431 | iomsg->free_iov = NULL; |
52de1fe1 | 4432 | } else { |
257e84a5 | 4433 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4434 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4435 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4436 | false); |
52de1fe1 JA |
4437 | if (ret > 0) |
4438 | ret = 0; | |
4439 | } | |
4440 | ||
4441 | return ret; | |
4442 | } | |
4443 | ||
4444 | #ifdef CONFIG_COMPAT | |
4445 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4446 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4447 | { |
4448 | struct compat_msghdr __user *msg_compat; | |
4449 | struct io_sr_msg *sr = &req->sr_msg; | |
4450 | struct compat_iovec __user *uiov; | |
4451 | compat_uptr_t ptr; | |
4452 | compat_size_t len; | |
4453 | int ret; | |
4454 | ||
270a5940 | 4455 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4456 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4457 | &ptr, &len); |
4458 | if (ret) | |
4459 | return ret; | |
4460 | ||
4461 | uiov = compat_ptr(ptr); | |
4462 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4463 | compat_ssize_t clen; | |
4464 | ||
4465 | if (len > 1) | |
4466 | return -EINVAL; | |
4467 | if (!access_ok(uiov, sizeof(*uiov))) | |
4468 | return -EFAULT; | |
4469 | if (__get_user(clen, &uiov->iov_len)) | |
4470 | return -EFAULT; | |
4471 | if (clen < 0) | |
4472 | return -EINVAL; | |
2d280bc8 | 4473 | sr->len = clen; |
257e84a5 | 4474 | iomsg->free_iov = NULL; |
52de1fe1 | 4475 | } else { |
257e84a5 | 4476 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4477 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4478 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4479 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4480 | if (ret < 0) |
4481 | return ret; | |
4482 | } | |
4483 | ||
4484 | return 0; | |
4485 | } | |
4486 | #endif | |
4487 | ||
1400e697 PB |
4488 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4489 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4490 | { |
1400e697 | 4491 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4492 | |
4493 | #ifdef CONFIG_COMPAT | |
4494 | if (req->ctx->compat) | |
1400e697 | 4495 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4496 | #endif |
52de1fe1 | 4497 | |
1400e697 | 4498 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4499 | } |
4500 | ||
bcda7baa | 4501 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4502 | bool needs_lock) |
bcda7baa JA |
4503 | { |
4504 | struct io_sr_msg *sr = &req->sr_msg; | |
4505 | struct io_buffer *kbuf; | |
4506 | ||
bcda7baa JA |
4507 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4508 | if (IS_ERR(kbuf)) | |
4509 | return kbuf; | |
4510 | ||
4511 | sr->kbuf = kbuf; | |
4512 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4513 | return kbuf; |
fddaface JA |
4514 | } |
4515 | ||
7fbb1b54 PB |
4516 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4517 | { | |
4518 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4519 | } | |
4520 | ||
93642ef8 | 4521 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4522 | { |
99bc4c38 | 4523 | int ret; |
3529d8c2 | 4524 | |
93642ef8 PB |
4525 | if (!io_op_defs[req->opcode].needs_async_data) |
4526 | return 0; | |
4527 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4528 | if (!ret) | |
4529 | req->flags |= REQ_F_NEED_CLEANUP; | |
4530 | return ret; | |
4531 | } | |
4532 | ||
4533 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4534 | { | |
4535 | struct io_sr_msg *sr = &req->sr_msg; | |
4536 | ||
d2b6f48b PB |
4537 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4538 | return -EINVAL; | |
4539 | ||
3529d8c2 | 4540 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4541 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4542 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4543 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4544 | |
d8768362 JA |
4545 | #ifdef CONFIG_COMPAT |
4546 | if (req->ctx->compat) | |
4547 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4548 | #endif | |
93642ef8 | 4549 | return 0; |
aa1fa28f JA |
4550 | } |
4551 | ||
889fca73 | 4552 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4553 | { |
6b754c8b | 4554 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4555 | struct socket *sock; |
7fbb1b54 | 4556 | struct io_buffer *kbuf; |
7a7cacba | 4557 | unsigned flags; |
52de1fe1 | 4558 | int ret, cflags = 0; |
45d189c6 | 4559 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4560 | |
dba4a925 | 4561 | sock = sock_from_file(req->file); |
7a7cacba | 4562 | if (unlikely(!sock)) |
dba4a925 | 4563 | return -ENOTSOCK; |
3529d8c2 | 4564 | |
257e84a5 PB |
4565 | kmsg = req->async_data; |
4566 | if (!kmsg) { | |
7a7cacba PB |
4567 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4568 | if (ret) | |
681fda8d | 4569 | return ret; |
7a7cacba PB |
4570 | kmsg = &iomsg; |
4571 | } | |
03b1230c | 4572 | |
bc02ef33 | 4573 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4574 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4575 | if (IS_ERR(kbuf)) |
52de1fe1 | 4576 | return PTR_ERR(kbuf); |
7a7cacba | 4577 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4578 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4579 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4580 | 1, req->sr_msg.len); |
4581 | } | |
52de1fe1 | 4582 | |
7a7cacba PB |
4583 | flags = req->sr_msg.msg_flags; |
4584 | if (flags & MSG_DONTWAIT) | |
4585 | req->flags |= REQ_F_NOWAIT; | |
4586 | else if (force_nonblock) | |
4587 | flags |= MSG_DONTWAIT; | |
e47293fd | 4588 | |
7a7cacba PB |
4589 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4590 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4591 | if (force_nonblock && ret == -EAGAIN) |
4592 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4593 | if (ret == -ERESTARTSYS) |
4594 | ret = -EINTR; | |
03b1230c | 4595 | |
7fbb1b54 PB |
4596 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4597 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4598 | /* fast path, check for non-NULL to avoid function call */ |
4599 | if (kmsg->free_iov) | |
4600 | kfree(kmsg->free_iov); | |
99bc4c38 | 4601 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4602 | if (ret < 0) |
4603 | req_set_fail_links(req); | |
889fca73 | 4604 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4605 | return 0; |
0fa03c62 | 4606 | } |
5d17b4a4 | 4607 | |
889fca73 | 4608 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4609 | { |
6b754c8b | 4610 | struct io_buffer *kbuf; |
7a7cacba PB |
4611 | struct io_sr_msg *sr = &req->sr_msg; |
4612 | struct msghdr msg; | |
4613 | void __user *buf = sr->buf; | |
fddaface | 4614 | struct socket *sock; |
7a7cacba PB |
4615 | struct iovec iov; |
4616 | unsigned flags; | |
bcda7baa | 4617 | int ret, cflags = 0; |
45d189c6 | 4618 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4619 | |
dba4a925 | 4620 | sock = sock_from_file(req->file); |
7a7cacba | 4621 | if (unlikely(!sock)) |
dba4a925 | 4622 | return -ENOTSOCK; |
fddaface | 4623 | |
bc02ef33 | 4624 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4625 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4626 | if (IS_ERR(kbuf)) |
4627 | return PTR_ERR(kbuf); | |
7a7cacba | 4628 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4629 | } |
bcda7baa | 4630 | |
7a7cacba | 4631 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4632 | if (unlikely(ret)) |
4633 | goto out_free; | |
fddaface | 4634 | |
7a7cacba PB |
4635 | msg.msg_name = NULL; |
4636 | msg.msg_control = NULL; | |
4637 | msg.msg_controllen = 0; | |
4638 | msg.msg_namelen = 0; | |
4639 | msg.msg_iocb = NULL; | |
4640 | msg.msg_flags = 0; | |
fddaface | 4641 | |
7a7cacba PB |
4642 | flags = req->sr_msg.msg_flags; |
4643 | if (flags & MSG_DONTWAIT) | |
4644 | req->flags |= REQ_F_NOWAIT; | |
4645 | else if (force_nonblock) | |
4646 | flags |= MSG_DONTWAIT; | |
4647 | ||
4648 | ret = sock_recvmsg(sock, &msg, flags); | |
4649 | if (force_nonblock && ret == -EAGAIN) | |
4650 | return -EAGAIN; | |
4651 | if (ret == -ERESTARTSYS) | |
4652 | ret = -EINTR; | |
14c32eee | 4653 | out_free: |
7fbb1b54 PB |
4654 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4655 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4656 | if (ret < 0) |
4657 | req_set_fail_links(req); | |
889fca73 | 4658 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4659 | return 0; |
fddaface JA |
4660 | } |
4661 | ||
3529d8c2 | 4662 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4663 | { |
8ed8d3c3 JA |
4664 | struct io_accept *accept = &req->accept; |
4665 | ||
14587a46 | 4666 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4667 | return -EINVAL; |
8042d6ce | 4668 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4669 | return -EINVAL; |
4670 | ||
d55e5f5b JA |
4671 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4672 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4673 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4674 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4675 | return 0; |
8ed8d3c3 | 4676 | } |
17f2fe35 | 4677 | |
889fca73 | 4678 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4679 | { |
4680 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4681 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4682 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4683 | int ret; |
4684 | ||
e697deed JX |
4685 | if (req->file->f_flags & O_NONBLOCK) |
4686 | req->flags |= REQ_F_NOWAIT; | |
4687 | ||
8ed8d3c3 | 4688 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4689 | accept->addr_len, accept->flags, |
4690 | accept->nofile); | |
8ed8d3c3 | 4691 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4692 | return -EAGAIN; |
ac45abc0 PB |
4693 | if (ret < 0) { |
4694 | if (ret == -ERESTARTSYS) | |
4695 | ret = -EINTR; | |
4e88d6e7 | 4696 | req_set_fail_links(req); |
ac45abc0 | 4697 | } |
889fca73 | 4698 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4699 | return 0; |
8ed8d3c3 JA |
4700 | } |
4701 | ||
93642ef8 PB |
4702 | static int io_connect_prep_async(struct io_kiocb *req) |
4703 | { | |
4704 | struct io_async_connect *io = req->async_data; | |
4705 | struct io_connect *conn = &req->connect; | |
4706 | ||
4707 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4708 | } | |
4709 | ||
3529d8c2 | 4710 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4711 | { |
3529d8c2 | 4712 | struct io_connect *conn = &req->connect; |
f499a021 | 4713 | |
14587a46 | 4714 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4715 | return -EINVAL; |
4716 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4717 | return -EINVAL; | |
4718 | ||
3529d8c2 JA |
4719 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4720 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4721 | return 0; |
f499a021 JA |
4722 | } |
4723 | ||
889fca73 | 4724 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4725 | { |
e8c2bc1f | 4726 | struct io_async_connect __io, *io; |
f8e85cf2 | 4727 | unsigned file_flags; |
3fbb51c1 | 4728 | int ret; |
45d189c6 | 4729 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4730 | |
e8c2bc1f JA |
4731 | if (req->async_data) { |
4732 | io = req->async_data; | |
f499a021 | 4733 | } else { |
3529d8c2 JA |
4734 | ret = move_addr_to_kernel(req->connect.addr, |
4735 | req->connect.addr_len, | |
e8c2bc1f | 4736 | &__io.address); |
f499a021 JA |
4737 | if (ret) |
4738 | goto out; | |
4739 | io = &__io; | |
4740 | } | |
4741 | ||
3fbb51c1 JA |
4742 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4743 | ||
e8c2bc1f | 4744 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4745 | req->connect.addr_len, file_flags); |
87f80d62 | 4746 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4747 | if (req->async_data) |
b7bb4f7d | 4748 | return -EAGAIN; |
e8c2bc1f | 4749 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4750 | ret = -ENOMEM; |
4751 | goto out; | |
4752 | } | |
e8c2bc1f JA |
4753 | io = req->async_data; |
4754 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4755 | return -EAGAIN; |
f499a021 | 4756 | } |
f8e85cf2 JA |
4757 | if (ret == -ERESTARTSYS) |
4758 | ret = -EINTR; | |
f499a021 | 4759 | out: |
4e88d6e7 JA |
4760 | if (ret < 0) |
4761 | req_set_fail_links(req); | |
889fca73 | 4762 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4763 | return 0; |
469956e8 Y |
4764 | } |
4765 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4766 | #define IO_NETOP_FN(op) \ |
4767 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4768 | { \ | |
4769 | return -EOPNOTSUPP; \ | |
4770 | } | |
4771 | ||
4772 | #define IO_NETOP_PREP(op) \ | |
4773 | IO_NETOP_FN(op) \ | |
4774 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4775 | { \ | |
4776 | return -EOPNOTSUPP; \ | |
4777 | } \ | |
4778 | ||
4779 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4780 | IO_NETOP_PREP(op) \ | |
4781 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4782 | { \ | |
4783 | return -EOPNOTSUPP; \ | |
4784 | } | |
4785 | ||
4786 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4787 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4788 | IO_NETOP_PREP_ASYNC(connect); | |
4789 | IO_NETOP_PREP(accept); | |
4790 | IO_NETOP_FN(send); | |
4791 | IO_NETOP_FN(recv); | |
469956e8 | 4792 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4793 | |
d7718a9d JA |
4794 | struct io_poll_table { |
4795 | struct poll_table_struct pt; | |
4796 | struct io_kiocb *req; | |
4797 | int error; | |
4798 | }; | |
ce593a6c | 4799 | |
d7718a9d JA |
4800 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4801 | __poll_t mask, task_work_func_t func) | |
4802 | { | |
aa96bf8a | 4803 | int ret; |
d7718a9d JA |
4804 | |
4805 | /* for instances that support it check for an event match first: */ | |
4806 | if (mask && !(mask & poll->events)) | |
4807 | return 0; | |
4808 | ||
4809 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4810 | ||
4811 | list_del_init(&poll->wait.entry); | |
4812 | ||
d7718a9d | 4813 | req->result = mask; |
7cbf1722 | 4814 | req->task_work.func = func; |
6d816e08 JA |
4815 | percpu_ref_get(&req->ctx->refs); |
4816 | ||
d7718a9d | 4817 | /* |
e3aabf95 JA |
4818 | * If this fails, then the task is exiting. When a task exits, the |
4819 | * work gets canceled, so just cancel this request as well instead | |
4820 | * of executing it. We can't safely execute it anyway, as we may not | |
4821 | * have the needed state needed for it anyway. | |
d7718a9d | 4822 | */ |
355fb9e2 | 4823 | ret = io_req_task_work_add(req); |
aa96bf8a | 4824 | if (unlikely(ret)) { |
e3aabf95 | 4825 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4826 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4827 | } |
d7718a9d JA |
4828 | return 1; |
4829 | } | |
4830 | ||
74ce6ce4 JA |
4831 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4832 | __acquires(&req->ctx->completion_lock) | |
4833 | { | |
4834 | struct io_ring_ctx *ctx = req->ctx; | |
4835 | ||
4836 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4837 | struct poll_table_struct pt = { ._key = poll->events }; | |
4838 | ||
4839 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4840 | } | |
4841 | ||
4842 | spin_lock_irq(&ctx->completion_lock); | |
4843 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4844 | add_wait_queue(poll->head, &poll->wait); | |
4845 | return true; | |
4846 | } | |
4847 | ||
4848 | return false; | |
4849 | } | |
4850 | ||
d4e7cd36 | 4851 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4852 | { |
e8c2bc1f | 4853 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4854 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4855 | return req->async_data; |
d4e7cd36 JA |
4856 | return req->apoll->double_poll; |
4857 | } | |
4858 | ||
4859 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4860 | { | |
4861 | if (req->opcode == IORING_OP_POLL_ADD) | |
4862 | return &req->poll; | |
4863 | return &req->apoll->poll; | |
4864 | } | |
4865 | ||
4866 | static void io_poll_remove_double(struct io_kiocb *req) | |
4867 | { | |
4868 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4869 | |
4870 | lockdep_assert_held(&req->ctx->completion_lock); | |
4871 | ||
4872 | if (poll && poll->head) { | |
4873 | struct wait_queue_head *head = poll->head; | |
4874 | ||
4875 | spin_lock(&head->lock); | |
4876 | list_del_init(&poll->wait.entry); | |
4877 | if (poll->wait.private) | |
4878 | refcount_dec(&req->refs); | |
4879 | poll->head = NULL; | |
4880 | spin_unlock(&head->lock); | |
4881 | } | |
4882 | } | |
4883 | ||
4884 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4885 | { | |
4886 | struct io_ring_ctx *ctx = req->ctx; | |
4887 | ||
d4e7cd36 | 4888 | io_poll_remove_double(req); |
18bceab1 JA |
4889 | req->poll.done = true; |
4890 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4891 | io_commit_cqring(ctx); | |
4892 | } | |
4893 | ||
dd221f46 | 4894 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4895 | { |
dd221f46 | 4896 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4897 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4898 | struct io_kiocb *nxt; |
18bceab1 JA |
4899 | |
4900 | if (io_poll_rewait(req, &req->poll)) { | |
4901 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
4902 | } else { |
4903 | hash_del(&req->hash_node); | |
4904 | io_poll_complete(req, req->result, 0); | |
4905 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 4906 | |
dd221f46 PB |
4907 | nxt = io_put_req_find_next(req); |
4908 | io_cqring_ev_posted(ctx); | |
4909 | if (nxt) | |
4910 | __io_req_task_submit(nxt); | |
4911 | } | |
18bceab1 | 4912 | |
6d816e08 | 4913 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4914 | } |
4915 | ||
4916 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4917 | int sync, void *key) | |
4918 | { | |
4919 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4920 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4921 | __poll_t mask = key_to_poll(key); |
4922 | ||
4923 | /* for instances that support it check for an event match first: */ | |
4924 | if (mask && !(mask & poll->events)) | |
4925 | return 0; | |
4926 | ||
8706e04e JA |
4927 | list_del_init(&wait->entry); |
4928 | ||
807abcb0 | 4929 | if (poll && poll->head) { |
18bceab1 JA |
4930 | bool done; |
4931 | ||
807abcb0 JA |
4932 | spin_lock(&poll->head->lock); |
4933 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4934 | if (!done) |
807abcb0 | 4935 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4936 | /* make sure double remove sees this as being gone */ |
4937 | wait->private = NULL; | |
807abcb0 | 4938 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4939 | if (!done) { |
4940 | /* use wait func handler, so it matches the rq type */ | |
4941 | poll->wait.func(&poll->wait, mode, sync, key); | |
4942 | } | |
18bceab1 JA |
4943 | } |
4944 | refcount_dec(&req->refs); | |
4945 | return 1; | |
4946 | } | |
4947 | ||
4948 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4949 | wait_queue_func_t wake_func) | |
4950 | { | |
4951 | poll->head = NULL; | |
4952 | poll->done = false; | |
4953 | poll->canceled = false; | |
4954 | poll->events = events; | |
4955 | INIT_LIST_HEAD(&poll->wait.entry); | |
4956 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4957 | } | |
4958 | ||
4959 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4960 | struct wait_queue_head *head, |
4961 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4962 | { |
4963 | struct io_kiocb *req = pt->req; | |
4964 | ||
4965 | /* | |
4966 | * If poll->head is already set, it's because the file being polled | |
4967 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4968 | * for write). Setup a separate io_poll_iocb if this happens. | |
4969 | */ | |
4970 | if (unlikely(poll->head)) { | |
58852d4d PB |
4971 | struct io_poll_iocb *poll_one = poll; |
4972 | ||
18bceab1 | 4973 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 4974 | if (*poll_ptr) { |
18bceab1 JA |
4975 | pt->error = -EINVAL; |
4976 | return; | |
4977 | } | |
1c3b3e65 JA |
4978 | /* double add on the same waitqueue head, ignore */ |
4979 | if (poll->head == head) | |
4980 | return; | |
18bceab1 JA |
4981 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
4982 | if (!poll) { | |
4983 | pt->error = -ENOMEM; | |
4984 | return; | |
4985 | } | |
58852d4d | 4986 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
4987 | refcount_inc(&req->refs); |
4988 | poll->wait.private = req; | |
807abcb0 | 4989 | *poll_ptr = poll; |
18bceab1 JA |
4990 | } |
4991 | ||
4992 | pt->error = 0; | |
4993 | poll->head = head; | |
a31eb4a2 JX |
4994 | |
4995 | if (poll->events & EPOLLEXCLUSIVE) | |
4996 | add_wait_queue_exclusive(head, &poll->wait); | |
4997 | else | |
4998 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
4999 | } |
5000 | ||
5001 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5002 | struct poll_table_struct *p) | |
5003 | { | |
5004 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5005 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5006 | |
807abcb0 | 5007 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5008 | } |
5009 | ||
d7718a9d JA |
5010 | static void io_async_task_func(struct callback_head *cb) |
5011 | { | |
5012 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5013 | struct async_poll *apoll = req->apoll; | |
5014 | struct io_ring_ctx *ctx = req->ctx; | |
5015 | ||
5016 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5017 | ||
74ce6ce4 | 5018 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5019 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5020 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5021 | return; |
d7718a9d JA |
5022 | } |
5023 | ||
31067255 | 5024 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5025 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5026 | hash_del(&req->hash_node); |
2bae047e | 5027 | |
d4e7cd36 | 5028 | io_poll_remove_double(req); |
74ce6ce4 JA |
5029 | spin_unlock_irq(&ctx->completion_lock); |
5030 | ||
0be0b0e3 PB |
5031 | if (!READ_ONCE(apoll->poll.canceled)) |
5032 | __io_req_task_submit(req); | |
5033 | else | |
5034 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5035 | |
6d816e08 | 5036 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5037 | kfree(apoll->double_poll); |
31067255 | 5038 | kfree(apoll); |
d7718a9d JA |
5039 | } |
5040 | ||
5041 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5042 | void *key) | |
5043 | { | |
5044 | struct io_kiocb *req = wait->private; | |
5045 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5046 | ||
5047 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5048 | key_to_poll(key)); | |
5049 | ||
5050 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5051 | } | |
5052 | ||
5053 | static void io_poll_req_insert(struct io_kiocb *req) | |
5054 | { | |
5055 | struct io_ring_ctx *ctx = req->ctx; | |
5056 | struct hlist_head *list; | |
5057 | ||
5058 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5059 | hlist_add_head(&req->hash_node, list); | |
5060 | } | |
5061 | ||
5062 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5063 | struct io_poll_iocb *poll, | |
5064 | struct io_poll_table *ipt, __poll_t mask, | |
5065 | wait_queue_func_t wake_func) | |
5066 | __acquires(&ctx->completion_lock) | |
5067 | { | |
5068 | struct io_ring_ctx *ctx = req->ctx; | |
5069 | bool cancel = false; | |
5070 | ||
4d52f338 | 5071 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5072 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5073 | poll->file = req->file; |
18bceab1 | 5074 | poll->wait.private = req; |
d7718a9d JA |
5075 | |
5076 | ipt->pt._key = mask; | |
5077 | ipt->req = req; | |
5078 | ipt->error = -EINVAL; | |
5079 | ||
d7718a9d JA |
5080 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5081 | ||
5082 | spin_lock_irq(&ctx->completion_lock); | |
5083 | if (likely(poll->head)) { | |
5084 | spin_lock(&poll->head->lock); | |
5085 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5086 | if (ipt->error) | |
5087 | cancel = true; | |
5088 | ipt->error = 0; | |
5089 | mask = 0; | |
5090 | } | |
5091 | if (mask || ipt->error) | |
5092 | list_del_init(&poll->wait.entry); | |
5093 | else if (cancel) | |
5094 | WRITE_ONCE(poll->canceled, true); | |
5095 | else if (!poll->done) /* actually waiting for an event */ | |
5096 | io_poll_req_insert(req); | |
5097 | spin_unlock(&poll->head->lock); | |
5098 | } | |
5099 | ||
5100 | return mask; | |
5101 | } | |
5102 | ||
5103 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5104 | { | |
5105 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5106 | struct io_ring_ctx *ctx = req->ctx; | |
5107 | struct async_poll *apoll; | |
5108 | struct io_poll_table ipt; | |
5109 | __poll_t mask, ret; | |
9dab14b8 | 5110 | int rw; |
d7718a9d JA |
5111 | |
5112 | if (!req->file || !file_can_poll(req->file)) | |
5113 | return false; | |
24c74678 | 5114 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5115 | return false; |
9dab14b8 JA |
5116 | if (def->pollin) |
5117 | rw = READ; | |
5118 | else if (def->pollout) | |
5119 | rw = WRITE; | |
5120 | else | |
5121 | return false; | |
5122 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5123 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5124 | return false; |
5125 | ||
5126 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5127 | if (unlikely(!apoll)) | |
5128 | return false; | |
807abcb0 | 5129 | apoll->double_poll = NULL; |
d7718a9d JA |
5130 | |
5131 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5132 | req->apoll = apoll; |
d7718a9d | 5133 | |
8755d97a | 5134 | mask = 0; |
d7718a9d | 5135 | if (def->pollin) |
8755d97a | 5136 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5137 | if (def->pollout) |
5138 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5139 | |
5140 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5141 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5142 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5143 | mask &= ~POLLIN; | |
5144 | ||
d7718a9d JA |
5145 | mask |= POLLERR | POLLPRI; |
5146 | ||
5147 | ipt.pt._qproc = io_async_queue_proc; | |
5148 | ||
5149 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5150 | io_async_wake); | |
a36da65c | 5151 | if (ret || ipt.error) { |
d4e7cd36 | 5152 | io_poll_remove_double(req); |
d7718a9d | 5153 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5154 | kfree(apoll->double_poll); |
d7718a9d JA |
5155 | kfree(apoll); |
5156 | return false; | |
5157 | } | |
5158 | spin_unlock_irq(&ctx->completion_lock); | |
5159 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5160 | apoll->poll.events); | |
5161 | return true; | |
5162 | } | |
5163 | ||
5164 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5165 | struct io_poll_iocb *poll) | |
221c5eb2 | 5166 | { |
b41e9852 | 5167 | bool do_complete = false; |
221c5eb2 JA |
5168 | |
5169 | spin_lock(&poll->head->lock); | |
5170 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5171 | if (!list_empty(&poll->wait.entry)) { |
5172 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5173 | do_complete = true; |
221c5eb2 JA |
5174 | } |
5175 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5176 | hash_del(&req->hash_node); |
d7718a9d JA |
5177 | return do_complete; |
5178 | } | |
5179 | ||
5180 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5181 | { | |
5182 | bool do_complete; | |
5183 | ||
d4e7cd36 JA |
5184 | io_poll_remove_double(req); |
5185 | ||
d7718a9d JA |
5186 | if (req->opcode == IORING_OP_POLL_ADD) { |
5187 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5188 | } else { | |
3bfa5bcb JA |
5189 | struct async_poll *apoll = req->apoll; |
5190 | ||
d7718a9d | 5191 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5192 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5193 | if (do_complete) { | |
d7718a9d | 5194 | io_put_req(req); |
807abcb0 | 5195 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5196 | kfree(apoll); |
5197 | } | |
b1f573bd XW |
5198 | } |
5199 | ||
b41e9852 JA |
5200 | if (do_complete) { |
5201 | io_cqring_fill_event(req, -ECANCELED); | |
5202 | io_commit_cqring(req->ctx); | |
f254ac04 | 5203 | req_set_fail_links(req); |
216578e5 | 5204 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5205 | } |
5206 | ||
5207 | return do_complete; | |
221c5eb2 JA |
5208 | } |
5209 | ||
76e1b642 JA |
5210 | /* |
5211 | * Returns true if we found and killed one or more poll requests | |
5212 | */ | |
6b81928d PB |
5213 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5214 | struct files_struct *files) | |
221c5eb2 | 5215 | { |
78076bb6 | 5216 | struct hlist_node *tmp; |
221c5eb2 | 5217 | struct io_kiocb *req; |
8e2e1faf | 5218 | int posted = 0, i; |
221c5eb2 JA |
5219 | |
5220 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5221 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5222 | struct hlist_head *list; | |
5223 | ||
5224 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5225 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5226 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5227 | posted += io_poll_remove_one(req); |
5228 | } | |
221c5eb2 JA |
5229 | } |
5230 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5231 | |
8e2e1faf JA |
5232 | if (posted) |
5233 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5234 | |
5235 | return posted != 0; | |
221c5eb2 JA |
5236 | } |
5237 | ||
47f46768 JA |
5238 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5239 | { | |
78076bb6 | 5240 | struct hlist_head *list; |
47f46768 JA |
5241 | struct io_kiocb *req; |
5242 | ||
78076bb6 JA |
5243 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5244 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5245 | if (sqe_addr != req->user_data) |
5246 | continue; | |
5247 | if (io_poll_remove_one(req)) | |
eac406c6 | 5248 | return 0; |
b41e9852 | 5249 | return -EALREADY; |
47f46768 JA |
5250 | } |
5251 | ||
5252 | return -ENOENT; | |
5253 | } | |
5254 | ||
3529d8c2 JA |
5255 | static int io_poll_remove_prep(struct io_kiocb *req, |
5256 | const struct io_uring_sqe *sqe) | |
0969e783 | 5257 | { |
0969e783 JA |
5258 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5259 | return -EINVAL; | |
5260 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5261 | sqe->poll_events) | |
5262 | return -EINVAL; | |
5263 | ||
018043be | 5264 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5265 | return 0; |
5266 | } | |
5267 | ||
221c5eb2 JA |
5268 | /* |
5269 | * Find a running poll command that matches one specified in sqe->addr, | |
5270 | * and remove it if found. | |
5271 | */ | |
61e98203 | 5272 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5273 | { |
5274 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5275 | int ret; |
221c5eb2 | 5276 | |
221c5eb2 | 5277 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5278 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5279 | spin_unlock_irq(&ctx->completion_lock); |
5280 | ||
4e88d6e7 JA |
5281 | if (ret < 0) |
5282 | req_set_fail_links(req); | |
e1e16097 | 5283 | io_req_complete(req, ret); |
221c5eb2 JA |
5284 | return 0; |
5285 | } | |
5286 | ||
221c5eb2 JA |
5287 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5288 | void *key) | |
5289 | { | |
c2f2eb7d JA |
5290 | struct io_kiocb *req = wait->private; |
5291 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5292 | |
d7718a9d | 5293 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5294 | } |
5295 | ||
221c5eb2 JA |
5296 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5297 | struct poll_table_struct *p) | |
5298 | { | |
5299 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5300 | ||
e8c2bc1f | 5301 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5302 | } |
5303 | ||
3529d8c2 | 5304 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5305 | { |
5306 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5307 | u32 events; |
221c5eb2 JA |
5308 | |
5309 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5310 | return -EINVAL; | |
5311 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5312 | return -EINVAL; | |
5313 | ||
5769a351 JX |
5314 | events = READ_ONCE(sqe->poll32_events); |
5315 | #ifdef __BIG_ENDIAN | |
5316 | events = swahw32(events); | |
5317 | #endif | |
a31eb4a2 JX |
5318 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5319 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5320 | return 0; |
5321 | } | |
5322 | ||
61e98203 | 5323 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5324 | { |
5325 | struct io_poll_iocb *poll = &req->poll; | |
5326 | struct io_ring_ctx *ctx = req->ctx; | |
5327 | struct io_poll_table ipt; | |
0969e783 | 5328 | __poll_t mask; |
0969e783 | 5329 | |
d7718a9d | 5330 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5331 | |
d7718a9d JA |
5332 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5333 | io_poll_wake); | |
221c5eb2 | 5334 | |
8c838788 | 5335 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5336 | ipt.error = 0; |
b0dd8a41 | 5337 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5338 | } |
221c5eb2 JA |
5339 | spin_unlock_irq(&ctx->completion_lock); |
5340 | ||
8c838788 JA |
5341 | if (mask) { |
5342 | io_cqring_ev_posted(ctx); | |
014db007 | 5343 | io_put_req(req); |
221c5eb2 | 5344 | } |
8c838788 | 5345 | return ipt.error; |
221c5eb2 JA |
5346 | } |
5347 | ||
5262f567 JA |
5348 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5349 | { | |
ad8a48ac JA |
5350 | struct io_timeout_data *data = container_of(timer, |
5351 | struct io_timeout_data, timer); | |
5352 | struct io_kiocb *req = data->req; | |
5353 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5354 | unsigned long flags; |
5355 | ||
5262f567 | 5356 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5357 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5358 | atomic_set(&req->ctx->cq_timeouts, |
5359 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5360 | ||
78e19bbe | 5361 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5362 | io_commit_cqring(ctx); |
5363 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5364 | ||
5365 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5366 | req_set_fail_links(req); |
5262f567 JA |
5367 | io_put_req(req); |
5368 | return HRTIMER_NORESTART; | |
5369 | } | |
5370 | ||
fbd15848 PB |
5371 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5372 | __u64 user_data) | |
f254ac04 | 5373 | { |
fbd15848 | 5374 | struct io_timeout_data *io; |
47f46768 JA |
5375 | struct io_kiocb *req; |
5376 | int ret = -ENOENT; | |
f254ac04 | 5377 | |
135fcde8 | 5378 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5379 | if (user_data == req->user_data) { |
47f46768 JA |
5380 | ret = 0; |
5381 | break; | |
5382 | } | |
5383 | } | |
5384 | ||
5385 | if (ret == -ENOENT) | |
fbd15848 PB |
5386 | return ERR_PTR(ret); |
5387 | ||
5388 | io = req->async_data; | |
e8c2bc1f | 5389 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5390 | if (ret == -1) |
fbd15848 | 5391 | return ERR_PTR(-EALREADY); |
a71976f3 | 5392 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5393 | return req; |
5394 | } | |
47f46768 | 5395 | |
fbd15848 PB |
5396 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5397 | { | |
5398 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5399 | ||
5400 | if (IS_ERR(req)) | |
5401 | return PTR_ERR(req); | |
f254ac04 JA |
5402 | |
5403 | req_set_fail_links(req); | |
f254ac04 | 5404 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5405 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5406 | return 0; |
5407 | } | |
5408 | ||
9c8e11b3 PB |
5409 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5410 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5411 | { |
9c8e11b3 PB |
5412 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5413 | struct io_timeout_data *data; | |
47f46768 | 5414 | |
9c8e11b3 PB |
5415 | if (IS_ERR(req)) |
5416 | return PTR_ERR(req); | |
47f46768 | 5417 | |
9c8e11b3 PB |
5418 | req->timeout.off = 0; /* noseq */ |
5419 | data = req->async_data; | |
5420 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5421 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5422 | data->timer.function = io_timeout_fn; | |
5423 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5424 | return 0; | |
47f46768 JA |
5425 | } |
5426 | ||
3529d8c2 JA |
5427 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5428 | const struct io_uring_sqe *sqe) | |
b29472ee | 5429 | { |
9c8e11b3 PB |
5430 | struct io_timeout_rem *tr = &req->timeout_rem; |
5431 | ||
b29472ee JA |
5432 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5433 | return -EINVAL; | |
61710e43 DA |
5434 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5435 | return -EINVAL; | |
9c8e11b3 | 5436 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5437 | return -EINVAL; |
5438 | ||
9c8e11b3 PB |
5439 | tr->addr = READ_ONCE(sqe->addr); |
5440 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5441 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5442 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5443 | return -EINVAL; | |
5444 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5445 | return -EFAULT; | |
5446 | } else if (tr->flags) { | |
5447 | /* timeout removal doesn't support flags */ | |
b29472ee | 5448 | return -EINVAL; |
9c8e11b3 | 5449 | } |
b29472ee | 5450 | |
b29472ee JA |
5451 | return 0; |
5452 | } | |
5453 | ||
8662daec PB |
5454 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5455 | { | |
5456 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5457 | : HRTIMER_MODE_REL; | |
5458 | } | |
5459 | ||
11365043 JA |
5460 | /* |
5461 | * Remove or update an existing timeout command | |
5462 | */ | |
61e98203 | 5463 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5464 | { |
9c8e11b3 | 5465 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5466 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5467 | int ret; |
11365043 | 5468 | |
11365043 | 5469 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5470 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5471 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5472 | else |
5473 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5474 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5475 | |
47f46768 | 5476 | io_cqring_fill_event(req, ret); |
11365043 JA |
5477 | io_commit_cqring(ctx); |
5478 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5479 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5480 | if (ret < 0) |
5481 | req_set_fail_links(req); | |
ec9c02ad | 5482 | io_put_req(req); |
11365043 | 5483 | return 0; |
5262f567 JA |
5484 | } |
5485 | ||
3529d8c2 | 5486 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5487 | bool is_timeout_link) |
5262f567 | 5488 | { |
ad8a48ac | 5489 | struct io_timeout_data *data; |
a41525ab | 5490 | unsigned flags; |
56080b02 | 5491 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5492 | |
ad8a48ac | 5493 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5494 | return -EINVAL; |
ad8a48ac | 5495 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5496 | return -EINVAL; |
56080b02 | 5497 | if (off && is_timeout_link) |
2d28390a | 5498 | return -EINVAL; |
a41525ab JA |
5499 | flags = READ_ONCE(sqe->timeout_flags); |
5500 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5501 | return -EINVAL; |
bdf20073 | 5502 | |
bfe68a22 | 5503 | req->timeout.off = off; |
26a61679 | 5504 | |
e8c2bc1f | 5505 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5506 | return -ENOMEM; |
5507 | ||
e8c2bc1f | 5508 | data = req->async_data; |
ad8a48ac | 5509 | data->req = req; |
ad8a48ac JA |
5510 | |
5511 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5512 | return -EFAULT; |
5513 | ||
8662daec | 5514 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5515 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5516 | return 0; | |
5517 | } | |
5518 | ||
61e98203 | 5519 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5520 | { |
ad8a48ac | 5521 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5522 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5523 | struct list_head *entry; |
bfe68a22 | 5524 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5525 | |
733f5c95 | 5526 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5527 | |
5262f567 JA |
5528 | /* |
5529 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5530 | * timeout event to be satisfied. If it isn't set, then this is |
5531 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5532 | */ |
8eb7e2d0 | 5533 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5534 | entry = ctx->timeout_list.prev; |
5535 | goto add; | |
5536 | } | |
5262f567 | 5537 | |
bfe68a22 PB |
5538 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5539 | req->timeout.target_seq = tail + off; | |
5262f567 | 5540 | |
f010505b MDG |
5541 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5542 | * This is safe because ->completion_lock is held, and submissions | |
5543 | * and completions are never mixed in the same ->completion_lock section. | |
5544 | */ | |
5545 | ctx->cq_last_tm_flush = tail; | |
5546 | ||
5262f567 JA |
5547 | /* |
5548 | * Insertion sort, ensuring the first entry in the list is always | |
5549 | * the one we need first. | |
5550 | */ | |
5262f567 | 5551 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5552 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5553 | timeout.list); | |
5262f567 | 5554 | |
8eb7e2d0 | 5555 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5556 | continue; |
bfe68a22 PB |
5557 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5558 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5559 | break; |
5560 | } | |
93bd25bb | 5561 | add: |
135fcde8 | 5562 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5563 | data->timer.function = io_timeout_fn; |
5564 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5565 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5566 | return 0; |
5567 | } | |
5262f567 | 5568 | |
62755e35 JA |
5569 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5570 | { | |
5571 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5572 | ||
5573 | return req->user_data == (unsigned long) data; | |
5574 | } | |
5575 | ||
5aa75ed5 | 5576 | static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr) |
62755e35 | 5577 | { |
62755e35 | 5578 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5579 | int ret = 0; |
5580 | ||
5aa75ed5 JA |
5581 | if (!tctx->io_wq) |
5582 | return -ENOENT; | |
5583 | ||
5584 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false); | |
62755e35 JA |
5585 | switch (cancel_ret) { |
5586 | case IO_WQ_CANCEL_OK: | |
5587 | ret = 0; | |
5588 | break; | |
5589 | case IO_WQ_CANCEL_RUNNING: | |
5590 | ret = -EALREADY; | |
5591 | break; | |
5592 | case IO_WQ_CANCEL_NOTFOUND: | |
5593 | ret = -ENOENT; | |
5594 | break; | |
5595 | } | |
5596 | ||
e977d6d3 JA |
5597 | return ret; |
5598 | } | |
5599 | ||
47f46768 JA |
5600 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5601 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5602 | int success_ret) |
47f46768 JA |
5603 | { |
5604 | unsigned long flags; | |
5605 | int ret; | |
5606 | ||
5aa75ed5 JA |
5607 | ret = io_async_cancel_one(req->task->io_uring, |
5608 | (void *) (unsigned long) sqe_addr); | |
47f46768 JA |
5609 | if (ret != -ENOENT) { |
5610 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5611 | goto done; | |
5612 | } | |
5613 | ||
5614 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5615 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5616 | if (ret != -ENOENT) | |
5617 | goto done; | |
5618 | ret = io_poll_cancel(ctx, sqe_addr); | |
5619 | done: | |
b0dd8a41 JA |
5620 | if (!ret) |
5621 | ret = success_ret; | |
47f46768 JA |
5622 | io_cqring_fill_event(req, ret); |
5623 | io_commit_cqring(ctx); | |
5624 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5625 | io_cqring_ev_posted(ctx); | |
5626 | ||
4e88d6e7 JA |
5627 | if (ret < 0) |
5628 | req_set_fail_links(req); | |
014db007 | 5629 | io_put_req(req); |
47f46768 JA |
5630 | } |
5631 | ||
3529d8c2 JA |
5632 | static int io_async_cancel_prep(struct io_kiocb *req, |
5633 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5634 | { |
fbf23849 | 5635 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5636 | return -EINVAL; |
61710e43 DA |
5637 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5638 | return -EINVAL; | |
5639 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5640 | return -EINVAL; |
5641 | ||
fbf23849 JA |
5642 | req->cancel.addr = READ_ONCE(sqe->addr); |
5643 | return 0; | |
5644 | } | |
5645 | ||
61e98203 | 5646 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5647 | { |
5648 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5649 | |
014db007 | 5650 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5651 | return 0; |
5652 | } | |
5653 | ||
269bbe5f | 5654 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5655 | const struct io_uring_sqe *sqe) |
5656 | { | |
6ca56f84 JA |
5657 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5658 | return -EINVAL; | |
61710e43 DA |
5659 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5660 | return -EINVAL; | |
5661 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5662 | return -EINVAL; |
5663 | ||
269bbe5f BM |
5664 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5665 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5666 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5667 | return -EINVAL; |
269bbe5f | 5668 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5669 | return 0; |
5670 | } | |
5671 | ||
889fca73 | 5672 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5673 | { |
5674 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5675 | struct io_uring_rsrc_update up; |
05f3fb3c | 5676 | int ret; |
fbf23849 | 5677 | |
45d189c6 | 5678 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5679 | return -EAGAIN; |
05f3fb3c | 5680 | |
269bbe5f BM |
5681 | up.offset = req->rsrc_update.offset; |
5682 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5683 | |
5684 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5685 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5686 | mutex_unlock(&ctx->uring_lock); |
5687 | ||
5688 | if (ret < 0) | |
5689 | req_set_fail_links(req); | |
889fca73 | 5690 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5691 | return 0; |
5692 | } | |
5693 | ||
bfe76559 | 5694 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5695 | { |
d625c6ee | 5696 | switch (req->opcode) { |
e781573e | 5697 | case IORING_OP_NOP: |
bfe76559 | 5698 | return 0; |
f67676d1 JA |
5699 | case IORING_OP_READV: |
5700 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5701 | case IORING_OP_READ: |
bfe76559 | 5702 | return io_read_prep(req, sqe); |
f67676d1 JA |
5703 | case IORING_OP_WRITEV: |
5704 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5705 | case IORING_OP_WRITE: |
bfe76559 | 5706 | return io_write_prep(req, sqe); |
0969e783 | 5707 | case IORING_OP_POLL_ADD: |
bfe76559 | 5708 | return io_poll_add_prep(req, sqe); |
0969e783 | 5709 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5710 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5711 | case IORING_OP_FSYNC: |
1155c76a | 5712 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5713 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5714 | return io_sfr_prep(req, sqe); |
03b1230c | 5715 | case IORING_OP_SENDMSG: |
fddaface | 5716 | case IORING_OP_SEND: |
bfe76559 | 5717 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5718 | case IORING_OP_RECVMSG: |
fddaface | 5719 | case IORING_OP_RECV: |
bfe76559 | 5720 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5721 | case IORING_OP_CONNECT: |
bfe76559 | 5722 | return io_connect_prep(req, sqe); |
2d28390a | 5723 | case IORING_OP_TIMEOUT: |
bfe76559 | 5724 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5725 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5726 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5727 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5728 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5729 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5730 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5731 | case IORING_OP_ACCEPT: |
bfe76559 | 5732 | return io_accept_prep(req, sqe); |
d63d1b5e | 5733 | case IORING_OP_FALLOCATE: |
bfe76559 | 5734 | return io_fallocate_prep(req, sqe); |
15b71abe | 5735 | case IORING_OP_OPENAT: |
bfe76559 | 5736 | return io_openat_prep(req, sqe); |
b5dba59e | 5737 | case IORING_OP_CLOSE: |
bfe76559 | 5738 | return io_close_prep(req, sqe); |
05f3fb3c | 5739 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5740 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5741 | case IORING_OP_STATX: |
bfe76559 | 5742 | return io_statx_prep(req, sqe); |
4840e418 | 5743 | case IORING_OP_FADVISE: |
bfe76559 | 5744 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5745 | case IORING_OP_MADVISE: |
bfe76559 | 5746 | return io_madvise_prep(req, sqe); |
cebdb986 | 5747 | case IORING_OP_OPENAT2: |
bfe76559 | 5748 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5749 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5750 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5751 | case IORING_OP_SPLICE: |
bfe76559 | 5752 | return io_splice_prep(req, sqe); |
ddf0322d | 5753 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5754 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5755 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5756 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5757 | case IORING_OP_TEE: |
bfe76559 | 5758 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5759 | case IORING_OP_SHUTDOWN: |
5760 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5761 | case IORING_OP_RENAMEAT: |
5762 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5763 | case IORING_OP_UNLINKAT: |
5764 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5765 | } |
5766 | ||
bfe76559 PB |
5767 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5768 | req->opcode); | |
5769 | return-EINVAL; | |
5770 | } | |
5771 | ||
93642ef8 | 5772 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5773 | { |
93642ef8 PB |
5774 | switch (req->opcode) { |
5775 | case IORING_OP_READV: | |
5776 | case IORING_OP_READ_FIXED: | |
5777 | case IORING_OP_READ: | |
5778 | return io_rw_prep_async(req, READ); | |
5779 | case IORING_OP_WRITEV: | |
5780 | case IORING_OP_WRITE_FIXED: | |
5781 | case IORING_OP_WRITE: | |
5782 | return io_rw_prep_async(req, WRITE); | |
5783 | case IORING_OP_SENDMSG: | |
5784 | case IORING_OP_SEND: | |
5785 | return io_sendmsg_prep_async(req); | |
5786 | case IORING_OP_RECVMSG: | |
5787 | case IORING_OP_RECV: | |
5788 | return io_recvmsg_prep_async(req); | |
5789 | case IORING_OP_CONNECT: | |
5790 | return io_connect_prep_async(req); | |
5791 | } | |
5792 | return 0; | |
5793 | } | |
5794 | ||
be7053b7 | 5795 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 5796 | { |
be7053b7 | 5797 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 5798 | return 0; |
be7053b7 | 5799 | /* some opcodes init it during the inital prep */ |
93642ef8 | 5800 | if (req->async_data) |
be7053b7 PB |
5801 | return 0; |
5802 | if (__io_alloc_async_data(req)) | |
bfe76559 | 5803 | return -EAGAIN; |
be7053b7 | 5804 | return io_req_prep_async(req); |
f67676d1 JA |
5805 | } |
5806 | ||
9cf7c104 PB |
5807 | static u32 io_get_sequence(struct io_kiocb *req) |
5808 | { | |
5809 | struct io_kiocb *pos; | |
5810 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 5811 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 5812 | |
f2f87370 PB |
5813 | io_for_each_link(pos, req) |
5814 | nr_reqs++; | |
9cf7c104 PB |
5815 | |
5816 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5817 | return total_submitted - nr_reqs; | |
5818 | } | |
5819 | ||
be7053b7 | 5820 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 5821 | { |
a197f664 | 5822 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5823 | struct io_defer_entry *de; |
f67676d1 | 5824 | int ret; |
9cf7c104 | 5825 | u32 seq; |
de0617e4 | 5826 | |
9d858b21 | 5827 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5828 | if (likely(list_empty_careful(&ctx->defer_list) && |
5829 | !(req->flags & REQ_F_IO_DRAIN))) | |
5830 | return 0; | |
5831 | ||
5832 | seq = io_get_sequence(req); | |
5833 | /* Still a chance to pass the sequence check */ | |
5834 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5835 | return 0; |
5836 | ||
be7053b7 PB |
5837 | ret = io_req_defer_prep(req); |
5838 | if (ret) | |
5839 | return ret; | |
cbdcb435 | 5840 | io_prep_async_link(req); |
27dc8338 PB |
5841 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5842 | if (!de) | |
5843 | return -ENOMEM; | |
2d28390a | 5844 | |
de0617e4 | 5845 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5846 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5847 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5848 | kfree(de); |
ae34817b PB |
5849 | io_queue_async_work(req); |
5850 | return -EIOCBQUEUED; | |
de0617e4 JA |
5851 | } |
5852 | ||
915967f6 | 5853 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5854 | de->req = req; |
9cf7c104 | 5855 | de->seq = seq; |
27dc8338 | 5856 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5857 | spin_unlock_irq(&ctx->completion_lock); |
5858 | return -EIOCBQUEUED; | |
5859 | } | |
5860 | ||
3ca405eb | 5861 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5862 | { |
0e1b6fe3 PB |
5863 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5864 | switch (req->opcode) { | |
5865 | case IORING_OP_READV: | |
5866 | case IORING_OP_READ_FIXED: | |
5867 | case IORING_OP_READ: | |
bcda7baa | 5868 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5869 | break; |
5870 | case IORING_OP_RECVMSG: | |
5871 | case IORING_OP_RECV: | |
bcda7baa | 5872 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5873 | break; |
5874 | } | |
5875 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5876 | } |
5877 | ||
0e1b6fe3 PB |
5878 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5879 | switch (req->opcode) { | |
5880 | case IORING_OP_READV: | |
5881 | case IORING_OP_READ_FIXED: | |
5882 | case IORING_OP_READ: | |
5883 | case IORING_OP_WRITEV: | |
5884 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5885 | case IORING_OP_WRITE: { |
5886 | struct io_async_rw *io = req->async_data; | |
5887 | if (io->free_iovec) | |
5888 | kfree(io->free_iovec); | |
0e1b6fe3 | 5889 | break; |
e8c2bc1f | 5890 | } |
0e1b6fe3 | 5891 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5892 | case IORING_OP_SENDMSG: { |
5893 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
5894 | |
5895 | kfree(io->free_iov); | |
0e1b6fe3 | 5896 | break; |
e8c2bc1f | 5897 | } |
0e1b6fe3 PB |
5898 | case IORING_OP_SPLICE: |
5899 | case IORING_OP_TEE: | |
5900 | io_put_file(req, req->splice.file_in, | |
5901 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5902 | break; | |
f3cd4850 JA |
5903 | case IORING_OP_OPENAT: |
5904 | case IORING_OP_OPENAT2: | |
5905 | if (req->open.filename) | |
5906 | putname(req->open.filename); | |
5907 | break; | |
80a261fd JA |
5908 | case IORING_OP_RENAMEAT: |
5909 | putname(req->rename.oldpath); | |
5910 | putname(req->rename.newpath); | |
5911 | break; | |
14a1143b JA |
5912 | case IORING_OP_UNLINKAT: |
5913 | putname(req->unlink.filename); | |
5914 | break; | |
0e1b6fe3 PB |
5915 | } |
5916 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5917 | } |
99bc4c38 PB |
5918 | } |
5919 | ||
889fca73 | 5920 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 5921 | { |
a197f664 | 5922 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 5923 | const struct cred *creds = NULL; |
d625c6ee | 5924 | int ret; |
2b188cc1 | 5925 | |
5730b27e JA |
5926 | if (req->work.personality) { |
5927 | const struct cred *new_creds; | |
5928 | ||
5929 | if (!(issue_flags & IO_URING_F_NONBLOCK)) | |
5930 | mutex_lock(&ctx->uring_lock); | |
5931 | new_creds = idr_find(&ctx->personality_idr, req->work.personality); | |
5932 | if (!(issue_flags & IO_URING_F_NONBLOCK)) | |
5933 | mutex_unlock(&ctx->uring_lock); | |
5934 | if (!new_creds) | |
5935 | return -EINVAL; | |
5936 | creds = override_creds(new_creds); | |
5937 | } | |
5938 | ||
d625c6ee | 5939 | switch (req->opcode) { |
2b188cc1 | 5940 | case IORING_OP_NOP: |
889fca73 | 5941 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
5942 | break; |
5943 | case IORING_OP_READV: | |
edafccee | 5944 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5945 | case IORING_OP_READ: |
889fca73 | 5946 | ret = io_read(req, issue_flags); |
edafccee | 5947 | break; |
3529d8c2 | 5948 | case IORING_OP_WRITEV: |
edafccee | 5949 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5950 | case IORING_OP_WRITE: |
889fca73 | 5951 | ret = io_write(req, issue_flags); |
2b188cc1 | 5952 | break; |
c992fe29 | 5953 | case IORING_OP_FSYNC: |
45d189c6 | 5954 | ret = io_fsync(req, issue_flags); |
c992fe29 | 5955 | break; |
221c5eb2 | 5956 | case IORING_OP_POLL_ADD: |
61e98203 | 5957 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
5958 | break; |
5959 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 5960 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 5961 | break; |
5d17b4a4 | 5962 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 5963 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 5964 | break; |
0fa03c62 | 5965 | case IORING_OP_SENDMSG: |
889fca73 | 5966 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 5967 | break; |
fddaface | 5968 | case IORING_OP_SEND: |
889fca73 | 5969 | ret = io_send(req, issue_flags); |
0fa03c62 | 5970 | break; |
aa1fa28f | 5971 | case IORING_OP_RECVMSG: |
889fca73 | 5972 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 5973 | break; |
fddaface | 5974 | case IORING_OP_RECV: |
889fca73 | 5975 | ret = io_recv(req, issue_flags); |
aa1fa28f | 5976 | break; |
5262f567 | 5977 | case IORING_OP_TIMEOUT: |
61e98203 | 5978 | ret = io_timeout(req, issue_flags); |
5262f567 | 5979 | break; |
11365043 | 5980 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 5981 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 5982 | break; |
17f2fe35 | 5983 | case IORING_OP_ACCEPT: |
889fca73 | 5984 | ret = io_accept(req, issue_flags); |
17f2fe35 | 5985 | break; |
f8e85cf2 | 5986 | case IORING_OP_CONNECT: |
889fca73 | 5987 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 5988 | break; |
62755e35 | 5989 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 5990 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 5991 | break; |
d63d1b5e | 5992 | case IORING_OP_FALLOCATE: |
45d189c6 | 5993 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 5994 | break; |
15b71abe | 5995 | case IORING_OP_OPENAT: |
45d189c6 | 5996 | ret = io_openat(req, issue_flags); |
15b71abe | 5997 | break; |
b5dba59e | 5998 | case IORING_OP_CLOSE: |
889fca73 | 5999 | ret = io_close(req, issue_flags); |
b5dba59e | 6000 | break; |
05f3fb3c | 6001 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6002 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6003 | break; |
eddc7ef5 | 6004 | case IORING_OP_STATX: |
45d189c6 | 6005 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6006 | break; |
4840e418 | 6007 | case IORING_OP_FADVISE: |
45d189c6 | 6008 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6009 | break; |
c1ca757b | 6010 | case IORING_OP_MADVISE: |
45d189c6 | 6011 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6012 | break; |
cebdb986 | 6013 | case IORING_OP_OPENAT2: |
45d189c6 | 6014 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6015 | break; |
3e4827b0 | 6016 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6017 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6018 | break; |
7d67af2c | 6019 | case IORING_OP_SPLICE: |
45d189c6 | 6020 | ret = io_splice(req, issue_flags); |
7d67af2c | 6021 | break; |
ddf0322d | 6022 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6023 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6024 | break; |
067524e9 | 6025 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6026 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6027 | break; |
f2a8d5c7 | 6028 | case IORING_OP_TEE: |
45d189c6 | 6029 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6030 | break; |
36f4fa68 | 6031 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6032 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6033 | break; |
80a261fd | 6034 | case IORING_OP_RENAMEAT: |
45d189c6 | 6035 | ret = io_renameat(req, issue_flags); |
80a261fd | 6036 | break; |
14a1143b | 6037 | case IORING_OP_UNLINKAT: |
45d189c6 | 6038 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6039 | break; |
2b188cc1 JA |
6040 | default: |
6041 | ret = -EINVAL; | |
6042 | break; | |
6043 | } | |
6044 | ||
5730b27e JA |
6045 | if (creds) |
6046 | revert_creds(creds); | |
6047 | ||
def596e9 JA |
6048 | if (ret) |
6049 | return ret; | |
6050 | ||
b532576e JA |
6051 | /* If the op doesn't have a file, we're not polling for it */ |
6052 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6053 | const bool in_async = io_wq_current_is_worker(); |
6054 | ||
11ba820b JA |
6055 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6056 | if (in_async) | |
6057 | mutex_lock(&ctx->uring_lock); | |
6058 | ||
2e9dbe90 | 6059 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6060 | |
6061 | if (in_async) | |
6062 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6063 | } |
6064 | ||
6065 | return 0; | |
2b188cc1 JA |
6066 | } |
6067 | ||
5280f7e5 | 6068 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6069 | { |
6070 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6071 | struct io_kiocb *timeout; |
561fb04a | 6072 | int ret = 0; |
2b188cc1 | 6073 | |
6df1db6b PB |
6074 | timeout = io_prep_linked_timeout(req); |
6075 | if (timeout) | |
6076 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6077 | |
4014d943 | 6078 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6079 | ret = -ECANCELED; |
31b51510 | 6080 | |
561fb04a | 6081 | if (!ret) { |
561fb04a | 6082 | do { |
889fca73 | 6083 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6084 | /* |
6085 | * We can get EAGAIN for polled IO even though we're | |
6086 | * forcing a sync submission from here, since we can't | |
6087 | * wait for request slots on the block side. | |
6088 | */ | |
6089 | if (ret != -EAGAIN) | |
6090 | break; | |
6091 | cond_resched(); | |
6092 | } while (1); | |
6093 | } | |
31b51510 | 6094 | |
a3df7698 | 6095 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6096 | if (ret) { |
a3df7698 PB |
6097 | /* io-wq is going to take one down */ |
6098 | refcount_inc(&req->refs); | |
6099 | io_req_task_queue_fail(req, ret); | |
edafccee | 6100 | } |
2b188cc1 JA |
6101 | } |
6102 | ||
65e19f54 JA |
6103 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6104 | int index) | |
6105 | { | |
269bbe5f | 6106 | struct fixed_rsrc_table *table; |
65e19f54 | 6107 | |
05f3fb3c | 6108 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6109 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6110 | } |
6111 | ||
8371adf5 PB |
6112 | static struct file *io_file_get(struct io_submit_state *state, |
6113 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6114 | { |
a197f664 | 6115 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6116 | struct file *file; |
09bb8394 | 6117 | |
8da11c19 | 6118 | if (fixed) { |
479f517b | 6119 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6120 | return NULL; |
b7620121 | 6121 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6122 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6123 | io_set_resource_node(req); |
09bb8394 | 6124 | } else { |
c826bd7a | 6125 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6126 | file = __io_file_get(state, fd); |
09bb8394 JA |
6127 | } |
6128 | ||
ce3d5aae PB |
6129 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6130 | io_req_track_inflight(req); | |
8371adf5 | 6131 | return file; |
09bb8394 JA |
6132 | } |
6133 | ||
2665abfd | 6134 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6135 | { |
ad8a48ac JA |
6136 | struct io_timeout_data *data = container_of(timer, |
6137 | struct io_timeout_data, timer); | |
90cd7e42 | 6138 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6139 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6140 | unsigned long flags; |
2665abfd JA |
6141 | |
6142 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6143 | prev = req->timeout.head; |
6144 | req->timeout.head = NULL; | |
2665abfd JA |
6145 | |
6146 | /* | |
6147 | * We don't expect the list to be empty, that will only happen if we | |
6148 | * race with the completion of the linked work. | |
6149 | */ | |
90cd7e42 | 6150 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6151 | io_remove_next_linked(prev); |
90cd7e42 PB |
6152 | else |
6153 | prev = NULL; | |
2665abfd JA |
6154 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6155 | ||
6156 | if (prev) { | |
4e88d6e7 | 6157 | req_set_fail_links(prev); |
014db007 | 6158 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6159 | io_put_req_deferred(prev, 1); |
47f46768 | 6160 | } else { |
9ae1f8dd PB |
6161 | io_req_complete_post(req, -ETIME, 0); |
6162 | io_put_req_deferred(req, 1); | |
2665abfd | 6163 | } |
2665abfd JA |
6164 | return HRTIMER_NORESTART; |
6165 | } | |
6166 | ||
7271ef3a | 6167 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6168 | { |
76a46e06 | 6169 | /* |
f2f87370 PB |
6170 | * If the back reference is NULL, then our linked request finished |
6171 | * before we got a chance to setup the timer | |
76a46e06 | 6172 | */ |
90cd7e42 | 6173 | if (req->timeout.head) { |
e8c2bc1f | 6174 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6175 | |
ad8a48ac JA |
6176 | data->timer.function = io_link_timeout_fn; |
6177 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6178 | data->mode); | |
2665abfd | 6179 | } |
7271ef3a JA |
6180 | } |
6181 | ||
6182 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6183 | { | |
6184 | struct io_ring_ctx *ctx = req->ctx; | |
6185 | ||
6186 | spin_lock_irq(&ctx->completion_lock); | |
6187 | __io_queue_linked_timeout(req); | |
76a46e06 | 6188 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6189 | |
2665abfd | 6190 | /* drop submission reference */ |
76a46e06 JA |
6191 | io_put_req(req); |
6192 | } | |
2665abfd | 6193 | |
ad8a48ac | 6194 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6195 | { |
f2f87370 | 6196 | struct io_kiocb *nxt = req->link; |
2665abfd | 6197 | |
f2f87370 PB |
6198 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6199 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6200 | return NULL; |
2665abfd | 6201 | |
90cd7e42 | 6202 | nxt->timeout.head = req; |
900fad45 | 6203 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6204 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6205 | return nxt; |
2665abfd JA |
6206 | } |
6207 | ||
c5eef2b9 | 6208 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6209 | { |
d3d7298d | 6210 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
e0c5c576 | 6211 | int ret; |
2b188cc1 | 6212 | |
c5eef2b9 | 6213 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6214 | |
491381ce JA |
6215 | /* |
6216 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6217 | * doesn't support non-blocking read/write attempts | |
6218 | */ | |
24c74678 | 6219 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6220 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6221 | /* |
6222 | * Queued up for async execution, worker will release | |
6223 | * submit reference when the iocb is actually submitted. | |
6224 | */ | |
6225 | io_queue_async_work(req); | |
2b188cc1 | 6226 | } |
0d63c148 PB |
6227 | } else if (likely(!ret)) { |
6228 | /* drop submission reference */ | |
e342c807 | 6229 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6230 | struct io_ring_ctx *ctx = req->ctx; |
6231 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6232 | |
6dd0be1e | 6233 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6234 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6235 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6236 | } else { |
d3d7298d | 6237 | io_put_req(req); |
0d63c148 PB |
6238 | } |
6239 | } else { | |
4e88d6e7 | 6240 | req_set_fail_links(req); |
e65ef56d | 6241 | io_put_req(req); |
e1e16097 | 6242 | io_req_complete(req, ret); |
9e645e11 | 6243 | } |
d3d7298d PB |
6244 | if (linked_timeout) |
6245 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6246 | } |
6247 | ||
be7053b7 | 6248 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6249 | { |
6250 | int ret; | |
6251 | ||
be7053b7 | 6252 | ret = io_req_defer(req); |
4fe2c963 JL |
6253 | if (ret) { |
6254 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6255 | fail_req: |
4e88d6e7 | 6256 | req_set_fail_links(req); |
e1e16097 JA |
6257 | io_put_req(req); |
6258 | io_req_complete(req, ret); | |
4fe2c963 | 6259 | } |
2550878f | 6260 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6261 | ret = io_req_defer_prep(req); |
6262 | if (unlikely(ret)) | |
6263 | goto fail_req; | |
ce35a47a JA |
6264 | io_queue_async_work(req); |
6265 | } else { | |
c5eef2b9 | 6266 | __io_queue_sqe(req); |
ce35a47a | 6267 | } |
4fe2c963 JL |
6268 | } |
6269 | ||
b16fed66 PB |
6270 | /* |
6271 | * Check SQE restrictions (opcode and flags). | |
6272 | * | |
6273 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6274 | */ | |
6275 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6276 | struct io_kiocb *req, | |
6277 | unsigned int sqe_flags) | |
4fe2c963 | 6278 | { |
b16fed66 PB |
6279 | if (!ctx->restricted) |
6280 | return true; | |
6281 | ||
6282 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6283 | return false; | |
6284 | ||
6285 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6286 | ctx->restrictions.sqe_flags_required) | |
6287 | return false; | |
6288 | ||
6289 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6290 | ctx->restrictions.sqe_flags_required)) | |
6291 | return false; | |
6292 | ||
6293 | return true; | |
4fe2c963 JL |
6294 | } |
6295 | ||
b16fed66 PB |
6296 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6297 | const struct io_uring_sqe *sqe) | |
6298 | { | |
6299 | struct io_submit_state *state; | |
6300 | unsigned int sqe_flags; | |
5730b27e | 6301 | int ret = 0; |
b16fed66 PB |
6302 | |
6303 | req->opcode = READ_ONCE(sqe->opcode); | |
6304 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6305 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6306 | req->user_data = READ_ONCE(sqe->user_data); | |
6307 | req->async_data = NULL; | |
6308 | req->file = NULL; | |
6309 | req->ctx = ctx; | |
6310 | req->link = NULL; | |
6311 | req->fixed_rsrc_refs = NULL; | |
6312 | /* one is dropped after submission, the other at completion */ | |
6313 | refcount_set(&req->refs, 2); | |
6314 | req->task = current; | |
6315 | req->result = 0; | |
6316 | ||
6317 | /* enforce forwards compatibility on users */ | |
ebf4a5db PB |
6318 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6319 | req->flags = 0; | |
b16fed66 | 6320 | return -EINVAL; |
ebf4a5db | 6321 | } |
b16fed66 PB |
6322 | |
6323 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6324 | return -EINVAL; | |
6325 | ||
b16fed66 PB |
6326 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6327 | return -EACCES; | |
6328 | ||
6329 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6330 | !io_op_defs[req->opcode].buffer_select) | |
6331 | return -EOPNOTSUPP; | |
863e0560 | 6332 | |
5730b27e JA |
6333 | req->work.list.next = NULL; |
6334 | req->work.flags = 0; | |
6335 | req->work.personality = READ_ONCE(sqe->personality); | |
b16fed66 PB |
6336 | state = &ctx->submit_state; |
6337 | ||
6338 | /* | |
6339 | * Plug now if we have more than 1 IO left after this, and the target | |
6340 | * is potentially a read/write to block based storage. | |
6341 | */ | |
6342 | if (!state->plug_started && state->ios_left > 1 && | |
6343 | io_op_defs[req->opcode].plug) { | |
6344 | blk_start_plug(&state->plug); | |
6345 | state->plug_started = true; | |
6346 | } | |
6347 | ||
6348 | if (io_op_defs[req->opcode].needs_file) { | |
6349 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6350 | ||
6351 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6352 | if (unlikely(!req->file)) | |
6353 | ret = -EBADF; | |
6354 | } | |
6355 | ||
6356 | state->ios_left--; | |
6357 | return ret; | |
6358 | } | |
6359 | ||
a6b8cadc | 6360 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6361 | const struct io_uring_sqe *sqe) |
9e645e11 | 6362 | { |
a1ab7b35 | 6363 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6364 | int ret; |
9e645e11 | 6365 | |
a6b8cadc PB |
6366 | ret = io_init_req(ctx, req, sqe); |
6367 | if (unlikely(ret)) { | |
6368 | fail_req: | |
6369 | io_put_req(req); | |
6370 | io_req_complete(req, ret); | |
de59bc10 PB |
6371 | if (link->head) { |
6372 | /* fail even hard links since we don't submit */ | |
cf109604 | 6373 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6374 | io_put_req(link->head); |
6375 | io_req_complete(link->head, -ECANCELED); | |
6376 | link->head = NULL; | |
6377 | } | |
a6b8cadc PB |
6378 | return ret; |
6379 | } | |
be7053b7 PB |
6380 | ret = io_req_prep(req, sqe); |
6381 | if (unlikely(ret)) | |
6382 | goto fail_req; | |
a6b8cadc | 6383 | |
be7053b7 | 6384 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6385 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6386 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6387 | ||
9e645e11 JA |
6388 | /* |
6389 | * If we already have a head request, queue this one for async | |
6390 | * submittal once the head completes. If we don't have a head but | |
6391 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6392 | * submitted sync once the chain is complete. If none of those | |
6393 | * conditions are true (normal request), then just queue it. | |
6394 | */ | |
863e0560 PB |
6395 | if (link->head) { |
6396 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6397 | |
8cdf2193 PB |
6398 | /* |
6399 | * Taking sequential execution of a link, draining both sides | |
6400 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6401 | * requests in the link. So, it drains the head and the | |
6402 | * next after the link request. The last one is done via | |
6403 | * drain_next flag to persist the effect across calls. | |
6404 | */ | |
ef4ff581 | 6405 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6406 | head->flags |= REQ_F_IO_DRAIN; |
6407 | ctx->drain_next = 1; | |
6408 | } | |
be7053b7 | 6409 | ret = io_req_defer_prep(req); |
cf109604 | 6410 | if (unlikely(ret)) |
a6b8cadc | 6411 | goto fail_req; |
9d76377f | 6412 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6413 | link->last->link = req; |
863e0560 | 6414 | link->last = req; |
32fe525b PB |
6415 | |
6416 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6417 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6418 | io_queue_sqe(head); |
863e0560 | 6419 | link->head = NULL; |
32fe525b | 6420 | } |
9e645e11 | 6421 | } else { |
711be031 PB |
6422 | if (unlikely(ctx->drain_next)) { |
6423 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6424 | ctx->drain_next = 0; |
711be031 | 6425 | } |
ef4ff581 | 6426 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6427 | link->head = req; |
6428 | link->last = req; | |
711be031 | 6429 | } else { |
be7053b7 | 6430 | io_queue_sqe(req); |
711be031 | 6431 | } |
9e645e11 | 6432 | } |
2e6e1fde | 6433 | |
1d4240cc | 6434 | return 0; |
9e645e11 JA |
6435 | } |
6436 | ||
9a56a232 JA |
6437 | /* |
6438 | * Batched submission is done, ensure local IO is flushed out. | |
6439 | */ | |
ba88ff11 PB |
6440 | static void io_submit_state_end(struct io_submit_state *state, |
6441 | struct io_ring_ctx *ctx) | |
9a56a232 | 6442 | { |
a1ab7b35 | 6443 | if (state->link.head) |
de59bc10 | 6444 | io_queue_sqe(state->link.head); |
6dd0be1e | 6445 | if (state->comp.nr) |
ba88ff11 | 6446 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6447 | if (state->plug_started) |
6448 | blk_finish_plug(&state->plug); | |
9f13c35b | 6449 | io_state_file_put(state); |
9a56a232 JA |
6450 | } |
6451 | ||
6452 | /* | |
6453 | * Start submission side cache. | |
6454 | */ | |
6455 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6456 | unsigned int max_ios) |
9a56a232 | 6457 | { |
27926b68 | 6458 | state->plug_started = false; |
9a56a232 | 6459 | state->ios_left = max_ios; |
a1ab7b35 PB |
6460 | /* set only head, no need to init link_last in advance */ |
6461 | state->link.head = NULL; | |
9a56a232 JA |
6462 | } |
6463 | ||
2b188cc1 JA |
6464 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6465 | { | |
75b28aff | 6466 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6467 | |
caf582c6 PB |
6468 | /* |
6469 | * Ensure any loads from the SQEs are done at this point, | |
6470 | * since once we write the new head, the application could | |
6471 | * write new data to them. | |
6472 | */ | |
6473 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6474 | } |
6475 | ||
2b188cc1 | 6476 | /* |
3529d8c2 | 6477 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6478 | * that is mapped by userspace. This means that care needs to be taken to |
6479 | * ensure that reads are stable, as we cannot rely on userspace always | |
6480 | * being a good citizen. If members of the sqe are validated and then later | |
6481 | * used, it's important that those reads are done through READ_ONCE() to | |
6482 | * prevent a re-load down the line. | |
6483 | */ | |
709b302f | 6484 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6485 | { |
75b28aff | 6486 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6487 | unsigned head; |
6488 | ||
6489 | /* | |
6490 | * The cached sq head (or cq tail) serves two purposes: | |
6491 | * | |
6492 | * 1) allows us to batch the cost of updating the user visible | |
6493 | * head updates. | |
6494 | * 2) allows the kernel side to track the head on its own, even | |
6495 | * though the application is the one updating it. | |
6496 | */ | |
4fccfcbb | 6497 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6498 | if (likely(head < ctx->sq_entries)) |
6499 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6500 | |
6501 | /* drop invalid entries */ | |
498ccd9e | 6502 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6503 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6504 | return NULL; |
6505 | } | |
6506 | ||
0f212204 | 6507 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6508 | { |
46c4e16a | 6509 | int submitted = 0; |
6c271ce2 | 6510 | |
c4a2ed72 | 6511 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6512 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6513 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6514 | return -EBUSY; |
6515 | } | |
6c271ce2 | 6516 | |
ee7d46d9 PB |
6517 | /* make sure SQ entry isn't read before tail */ |
6518 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6519 | |
2b85edfc PB |
6520 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6521 | return -EAGAIN; | |
6c271ce2 | 6522 | |
d8a6df10 | 6523 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6524 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6525 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6526 | |
46c4e16a | 6527 | while (submitted < nr) { |
3529d8c2 | 6528 | const struct io_uring_sqe *sqe; |
196be95c | 6529 | struct io_kiocb *req; |
fb5ccc98 | 6530 | |
258b29a9 | 6531 | req = io_alloc_req(ctx); |
196be95c PB |
6532 | if (unlikely(!req)) { |
6533 | if (!submitted) | |
6534 | submitted = -EAGAIN; | |
fb5ccc98 | 6535 | break; |
196be95c | 6536 | } |
4fccfcbb PB |
6537 | sqe = io_get_sqe(ctx); |
6538 | if (unlikely(!sqe)) { | |
6539 | kmem_cache_free(req_cachep, req); | |
6540 | break; | |
6541 | } | |
d3656344 JA |
6542 | /* will complete beyond this point, count as submitted */ |
6543 | submitted++; | |
a1ab7b35 | 6544 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6545 | break; |
6c271ce2 JA |
6546 | } |
6547 | ||
9466f437 PB |
6548 | if (unlikely(submitted != nr)) { |
6549 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6550 | struct io_uring_task *tctx = current->io_uring; |
6551 | int unused = nr - ref_used; | |
9466f437 | 6552 | |
d8a6df10 JA |
6553 | percpu_ref_put_many(&ctx->refs, unused); |
6554 | percpu_counter_sub(&tctx->inflight, unused); | |
6555 | put_task_struct_many(current, unused); | |
9466f437 | 6556 | } |
6c271ce2 | 6557 | |
a1ab7b35 | 6558 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6559 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6560 | io_commit_sqring(ctx); | |
6561 | ||
6c271ce2 JA |
6562 | return submitted; |
6563 | } | |
6564 | ||
23b3628e XW |
6565 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6566 | { | |
6567 | /* Tell userspace we may need a wakeup call */ | |
6568 | spin_lock_irq(&ctx->completion_lock); | |
6569 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6570 | spin_unlock_irq(&ctx->completion_lock); | |
6571 | } | |
6572 | ||
6573 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6574 | { | |
6575 | spin_lock_irq(&ctx->completion_lock); | |
6576 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6577 | spin_unlock_irq(&ctx->completion_lock); | |
6578 | } | |
6579 | ||
08369246 | 6580 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6581 | { |
c8d1ba58 | 6582 | unsigned int to_submit; |
bdcd3eab | 6583 | int ret = 0; |
6c271ce2 | 6584 | |
c8d1ba58 | 6585 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6586 | /* if we're handling multiple rings, cap submit size for fairness */ |
6587 | if (cap_entries && to_submit > 8) | |
6588 | to_submit = 8; | |
6589 | ||
906a3c6f | 6590 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6591 | unsigned nr_events = 0; |
a4c0b3de | 6592 | |
c8d1ba58 | 6593 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6594 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6595 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6596 | |
70aacfe6 | 6597 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs))) |
08369246 | 6598 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6599 | mutex_unlock(&ctx->uring_lock); |
6600 | } | |
6c271ce2 | 6601 | |
90554200 JA |
6602 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6603 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6604 | |
08369246 XW |
6605 | return ret; |
6606 | } | |
6c271ce2 | 6607 | |
08369246 XW |
6608 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6609 | { | |
6610 | struct io_ring_ctx *ctx; | |
6611 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6612 | |
08369246 XW |
6613 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6614 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6615 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6616 | } |
c1edbf5f | 6617 | |
08369246 | 6618 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6619 | } |
6c271ce2 | 6620 | |
69fb2131 JA |
6621 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6622 | { | |
6623 | struct io_ring_ctx *ctx; | |
6624 | ||
6625 | while (!list_empty(&sqd->ctx_new_list)) { | |
6626 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6627 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6628 | complete(&ctx->sq_thread_comp); | |
6629 | } | |
08369246 XW |
6630 | |
6631 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6632 | } |
6633 | ||
37d1e2e3 JA |
6634 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
6635 | { | |
6636 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
6637 | } | |
6638 | ||
6639 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) | |
6640 | { | |
6641 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
6642 | } | |
6643 | ||
6644 | static void io_sq_thread_parkme(struct io_sq_data *sqd) | |
6645 | { | |
6646 | for (;;) { | |
6647 | /* | |
6648 | * TASK_PARKED is a special state; we must serialize against | |
6649 | * possible pending wakeups to avoid store-store collisions on | |
6650 | * task->state. | |
6651 | * | |
6652 | * Such a collision might possibly result in the task state | |
6653 | * changin from TASK_PARKED and us failing the | |
6654 | * wait_task_inactive() in kthread_park(). | |
6655 | */ | |
6656 | set_special_state(TASK_PARKED); | |
6657 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) | |
6658 | break; | |
6659 | ||
6660 | /* | |
6661 | * Thread is going to call schedule(), do not preempt it, | |
6662 | * or the caller of kthread_park() may spend more time in | |
6663 | * wait_task_inactive(). | |
6664 | */ | |
6665 | preempt_disable(); | |
6666 | complete(&sqd->completion); | |
6667 | schedule_preempt_disabled(); | |
6668 | preempt_enable(); | |
6669 | } | |
6670 | __set_current_state(TASK_RUNNING); | |
6671 | } | |
6672 | ||
c8d1ba58 JA |
6673 | static int io_sq_thread(void *data) |
6674 | { | |
69fb2131 JA |
6675 | struct io_sq_data *sqd = data; |
6676 | struct io_ring_ctx *ctx; | |
a0d9205f | 6677 | unsigned long timeout = 0; |
37d1e2e3 | 6678 | char buf[TASK_COMM_LEN]; |
08369246 | 6679 | DEFINE_WAIT(wait); |
6c271ce2 | 6680 | |
37d1e2e3 JA |
6681 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
6682 | set_task_comm(current, buf); | |
6683 | sqd->thread = current; | |
6684 | current->pf_io_worker = NULL; | |
6685 | ||
6686 | if (sqd->sq_cpu != -1) | |
6687 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6688 | else | |
6689 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6690 | current->flags |= PF_NO_SETAFFINITY; | |
6691 | ||
6692 | complete(&sqd->completion); | |
6c271ce2 | 6693 | |
37d1e2e3 JA |
6694 | wait_for_completion(&sqd->startup); |
6695 | ||
6696 | while (!io_sq_thread_should_stop(sqd)) { | |
08369246 XW |
6697 | int ret; |
6698 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
6699 | |
6700 | /* | |
69fb2131 | 6701 | * Any changes to the sqd lists are synchronized through the |
37d1e2e3 | 6702 | * thread parking. This synchronizes the thread vs users, |
69fb2131 | 6703 | * the users are synchronized on the sqd->ctx_lock. |
c1edbf5f | 6704 | */ |
37d1e2e3 JA |
6705 | if (io_sq_thread_should_park(sqd)) { |
6706 | io_sq_thread_parkme(sqd); | |
6707 | continue; | |
65b2b213 | 6708 | } |
08369246 | 6709 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 6710 | io_sqd_init_new(sqd); |
08369246 XW |
6711 | timeout = jiffies + sqd->sq_thread_idle; |
6712 | } | |
37d1e2e3 JA |
6713 | if (fatal_signal_pending(current)) |
6714 | break; | |
08369246 | 6715 | sqt_spin = false; |
e95eee2d | 6716 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6717 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
08369246 XW |
6718 | ret = __io_sq_thread(ctx, cap_entries); |
6719 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
6720 | sqt_spin = true; | |
69fb2131 | 6721 | } |
6c271ce2 | 6722 | |
08369246 | 6723 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6724 | io_run_task_work(); |
6725 | cond_resched(); | |
08369246 XW |
6726 | if (sqt_spin) |
6727 | timeout = jiffies + sqd->sq_thread_idle; | |
6728 | continue; | |
6729 | } | |
6730 | ||
08369246 XW |
6731 | needs_sched = true; |
6732 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6733 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6734 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6735 | !list_empty_careful(&ctx->iopoll_list)) { | |
6736 | needs_sched = false; | |
6737 | break; | |
6738 | } | |
6739 | if (io_sqring_entries(ctx)) { | |
6740 | needs_sched = false; | |
6741 | break; | |
6742 | } | |
6743 | } | |
6744 | ||
37d1e2e3 | 6745 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
69fb2131 JA |
6746 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6747 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6748 | |
69fb2131 | 6749 | schedule(); |
69fb2131 JA |
6750 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6751 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6752 | } |
08369246 XW |
6753 | |
6754 | finish_wait(&sqd->wait, &wait); | |
6755 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
6756 | } |
6757 | ||
37d1e2e3 JA |
6758 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6759 | io_uring_cancel_sqpoll(ctx); | |
06058632 | 6760 | |
37d1e2e3 | 6761 | io_run_task_work(); |
28cea78a | 6762 | |
8629397e JA |
6763 | if (io_sq_thread_should_park(sqd)) |
6764 | io_sq_thread_parkme(sqd); | |
6765 | ||
37d1e2e3 JA |
6766 | /* |
6767 | * Clear thread under lock so that concurrent parks work correctly | |
6768 | */ | |
8629397e | 6769 | complete(&sqd->completion); |
37d1e2e3 JA |
6770 | mutex_lock(&sqd->lock); |
6771 | sqd->thread = NULL; | |
5f3f26f9 JA |
6772 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6773 | ctx->sqo_exec = 1; | |
6774 | io_ring_set_wakeup_flag(ctx); | |
6775 | } | |
06058632 | 6776 | |
37d1e2e3 | 6777 | complete(&sqd->exited); |
e54945ae | 6778 | mutex_unlock(&sqd->lock); |
37d1e2e3 | 6779 | do_exit(0); |
6c271ce2 JA |
6780 | } |
6781 | ||
bda52162 JA |
6782 | struct io_wait_queue { |
6783 | struct wait_queue_entry wq; | |
6784 | struct io_ring_ctx *ctx; | |
6785 | unsigned to_wait; | |
6786 | unsigned nr_timeouts; | |
6787 | }; | |
6788 | ||
6c503150 | 6789 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6790 | { |
6791 | struct io_ring_ctx *ctx = iowq->ctx; | |
6792 | ||
6793 | /* | |
d195a66e | 6794 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6795 | * started waiting. For timeouts, we always want to return to userspace, |
6796 | * regardless of event count. | |
6797 | */ | |
6c503150 | 6798 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6799 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6800 | } | |
6801 | ||
6802 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6803 | int wake_flags, void *key) | |
6804 | { | |
6805 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6806 | wq); | |
6807 | ||
6c503150 PB |
6808 | /* |
6809 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6810 | * the task, and the next invocation will do it. | |
6811 | */ | |
6812 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6813 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6814 | return -1; | |
bda52162 JA |
6815 | } |
6816 | ||
af9c1a44 JA |
6817 | static int io_run_task_work_sig(void) |
6818 | { | |
6819 | if (io_run_task_work()) | |
6820 | return 1; | |
6821 | if (!signal_pending(current)) | |
6822 | return 0; | |
792ee0f6 JA |
6823 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
6824 | return -ERESTARTSYS; | |
af9c1a44 JA |
6825 | return -EINTR; |
6826 | } | |
6827 | ||
eeb60b9a PB |
6828 | /* when returns >0, the caller should retry */ |
6829 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6830 | struct io_wait_queue *iowq, | |
6831 | signed long *timeout) | |
6832 | { | |
6833 | int ret; | |
6834 | ||
6835 | /* make sure we run task_work before checking for signals */ | |
6836 | ret = io_run_task_work_sig(); | |
6837 | if (ret || io_should_wake(iowq)) | |
6838 | return ret; | |
6839 | /* let the caller flush overflows, retry */ | |
6840 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6841 | return 1; | |
6842 | ||
6843 | *timeout = schedule_timeout(*timeout); | |
6844 | return !*timeout ? -ETIME : 1; | |
6845 | } | |
6846 | ||
2b188cc1 JA |
6847 | /* |
6848 | * Wait until events become available, if we don't already have some. The | |
6849 | * application must reap them itself, as they reside on the shared cq ring. | |
6850 | */ | |
6851 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
6852 | const sigset_t __user *sig, size_t sigsz, |
6853 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 6854 | { |
bda52162 JA |
6855 | struct io_wait_queue iowq = { |
6856 | .wq = { | |
6857 | .private = current, | |
6858 | .func = io_wake_function, | |
6859 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6860 | }, | |
6861 | .ctx = ctx, | |
6862 | .to_wait = min_events, | |
6863 | }; | |
75b28aff | 6864 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
6865 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
6866 | int ret; | |
2b188cc1 | 6867 | |
b41e9852 | 6868 | do { |
6c503150 PB |
6869 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6870 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 6871 | return 0; |
4c6e277c | 6872 | if (!io_run_task_work()) |
b41e9852 | 6873 | break; |
b41e9852 | 6874 | } while (1); |
2b188cc1 JA |
6875 | |
6876 | if (sig) { | |
9e75ad5d AB |
6877 | #ifdef CONFIG_COMPAT |
6878 | if (in_compat_syscall()) | |
6879 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6880 | sigsz); |
9e75ad5d AB |
6881 | else |
6882 | #endif | |
b772434b | 6883 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6884 | |
2b188cc1 JA |
6885 | if (ret) |
6886 | return ret; | |
6887 | } | |
6888 | ||
c73ebb68 | 6889 | if (uts) { |
c1d5a224 PB |
6890 | struct timespec64 ts; |
6891 | ||
c73ebb68 HX |
6892 | if (get_timespec64(&ts, uts)) |
6893 | return -EFAULT; | |
6894 | timeout = timespec64_to_jiffies(&ts); | |
6895 | } | |
6896 | ||
bda52162 | 6897 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6898 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 6899 | do { |
6c503150 | 6900 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
6901 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
6902 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
6903 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
6904 | finish_wait(&ctx->wait, &iowq.wq); | |
6905 | } while (ret > 0); | |
bda52162 | 6906 | |
b7db41c9 | 6907 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6908 | |
75b28aff | 6909 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6910 | } |
6911 | ||
6b06314c JA |
6912 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6913 | { | |
6914 | #if defined(CONFIG_UNIX) | |
6915 | if (ctx->ring_sock) { | |
6916 | struct sock *sock = ctx->ring_sock->sk; | |
6917 | struct sk_buff *skb; | |
6918 | ||
6919 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6920 | kfree_skb(skb); | |
6921 | } | |
6922 | #else | |
6923 | int i; | |
6924 | ||
65e19f54 JA |
6925 | for (i = 0; i < ctx->nr_user_files; i++) { |
6926 | struct file *file; | |
6927 | ||
6928 | file = io_file_from_index(ctx, i); | |
6929 | if (file) | |
6930 | fput(file); | |
6931 | } | |
6b06314c JA |
6932 | #endif |
6933 | } | |
6934 | ||
00835dce | 6935 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 6936 | { |
269bbe5f | 6937 | struct fixed_rsrc_data *data; |
05f3fb3c | 6938 | |
269bbe5f | 6939 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
6940 | complete(&data->done); |
6941 | } | |
6942 | ||
2a63b2d9 | 6943 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 6944 | { |
2a63b2d9 | 6945 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
6946 | } |
6947 | ||
2a63b2d9 | 6948 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 6949 | { |
2a63b2d9 BM |
6950 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
6951 | } | |
65e19f54 | 6952 | |
d67d2263 BM |
6953 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
6954 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 6955 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 6956 | { |
2a63b2d9 | 6957 | io_rsrc_ref_lock(ctx); |
269bbe5f | 6958 | rsrc_data->node = ref_node; |
d67d2263 | 6959 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 6960 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 6961 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
6962 | } |
6963 | ||
8bad28d8 | 6964 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 6965 | { |
8bad28d8 | 6966 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 6967 | |
2a63b2d9 | 6968 | io_rsrc_ref_lock(ctx); |
1e5d770b | 6969 | ref_node = data->node; |
e6cb007c | 6970 | data->node = NULL; |
2a63b2d9 | 6971 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
6972 | if (ref_node) |
6973 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 HX |
6974 | } |
6975 | ||
6976 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, | |
6977 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
6978 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
6979 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 6980 | { |
f2303b1f | 6981 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 6982 | int ret; |
05589553 | 6983 | |
8bad28d8 HX |
6984 | if (data->quiesce) |
6985 | return -ENXIO; | |
05589553 | 6986 | |
8bad28d8 | 6987 | data->quiesce = true; |
1ffc5422 | 6988 | do { |
f2303b1f PB |
6989 | ret = -ENOMEM; |
6990 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
6991 | if (!backup_node) | |
6992 | break; | |
6993 | backup_node->rsrc_data = data; | |
6994 | backup_node->rsrc_put = rsrc_put; | |
6995 | ||
8bad28d8 HX |
6996 | io_sqe_rsrc_kill_node(ctx, data); |
6997 | percpu_ref_kill(&data->refs); | |
6998 | flush_delayed_work(&ctx->rsrc_put_work); | |
6999 | ||
1ffc5422 PB |
7000 | ret = wait_for_completion_interruptible(&data->done); |
7001 | if (!ret) | |
7002 | break; | |
8bad28d8 | 7003 | |
cb5e1b81 | 7004 | percpu_ref_resurrect(&data->refs); |
8bad28d8 HX |
7005 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7006 | backup_node = NULL; | |
cb5e1b81 | 7007 | reinit_completion(&data->done); |
8bad28d8 | 7008 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7009 | ret = io_run_task_work_sig(); |
8bad28d8 | 7010 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7011 | } while (ret >= 0); |
8bad28d8 | 7012 | data->quiesce = false; |
05f3fb3c | 7013 | |
8bad28d8 HX |
7014 | if (backup_node) |
7015 | destroy_fixed_rsrc_ref_node(backup_node); | |
7016 | return ret; | |
d7954b2b BM |
7017 | } |
7018 | ||
1ad555c6 BM |
7019 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7020 | { | |
7021 | struct fixed_rsrc_data *data; | |
7022 | ||
7023 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7024 | if (!data) | |
7025 | return NULL; | |
7026 | ||
00835dce | 7027 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7028 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7029 | kfree(data); | |
7030 | return NULL; | |
7031 | } | |
7032 | data->ctx = ctx; | |
7033 | init_completion(&data->done); | |
7034 | return data; | |
7035 | } | |
7036 | ||
7037 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7038 | { | |
7039 | percpu_ref_exit(&data->refs); | |
7040 | kfree(data->table); | |
7041 | kfree(data); | |
7042 | } | |
7043 | ||
d7954b2b BM |
7044 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7045 | { | |
7046 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7047 | unsigned nr_tables, i; |
7048 | int ret; | |
7049 | ||
8bad28d8 HX |
7050 | /* |
7051 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7052 | * Since we possibly drop uring lock later in this function to | |
7053 | * run task work. | |
7054 | */ | |
7055 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7056 | return -ENXIO; |
f2303b1f | 7057 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7058 | if (ret) |
7059 | return ret; | |
7060 | ||
6b06314c | 7061 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7062 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7063 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7064 | kfree(data->table[i].files); |
1ad555c6 | 7065 | free_fixed_rsrc_data(data); |
05f3fb3c | 7066 | ctx->file_data = NULL; |
6b06314c JA |
7067 | ctx->nr_user_files = 0; |
7068 | return 0; | |
7069 | } | |
7070 | ||
37d1e2e3 JA |
7071 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7072 | __releases(&sqd->lock) | |
7073 | { | |
7074 | if (!sqd->thread) | |
7075 | return; | |
7076 | if (sqd->thread == current) | |
7077 | return; | |
7078 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7079 | wake_up_state(sqd->thread, TASK_PARKED); | |
7080 | mutex_unlock(&sqd->lock); | |
7081 | } | |
7082 | ||
7083 | static bool io_sq_thread_park(struct io_sq_data *sqd) | |
7084 | __acquires(&sqd->lock) | |
7085 | { | |
7086 | if (sqd->thread == current) | |
7087 | return true; | |
7088 | mutex_lock(&sqd->lock); | |
7089 | if (!sqd->thread) { | |
7090 | mutex_unlock(&sqd->lock); | |
7091 | return false; | |
7092 | } | |
7093 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7094 | wake_up_process(sqd->thread); | |
7095 | wait_for_completion(&sqd->completion); | |
7096 | return true; | |
7097 | } | |
7098 | ||
7099 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7100 | { | |
e54945ae | 7101 | if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) |
37d1e2e3 | 7102 | return; |
e54945ae JA |
7103 | mutex_lock(&sqd->lock); |
7104 | if (sqd->thread) { | |
7105 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
7106 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); | |
7107 | wake_up_process(sqd->thread); | |
7108 | mutex_unlock(&sqd->lock); | |
7109 | wait_for_completion(&sqd->exited); | |
7110 | WARN_ON_ONCE(sqd->thread); | |
7111 | } else { | |
7112 | mutex_unlock(&sqd->lock); | |
7113 | } | |
37d1e2e3 JA |
7114 | } |
7115 | ||
534ca6d6 | 7116 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7117 | { |
534ca6d6 | 7118 | if (refcount_dec_and_test(&sqd->refs)) { |
37d1e2e3 JA |
7119 | io_sq_thread_stop(sqd); |
7120 | kfree(sqd); | |
7121 | } | |
7122 | } | |
7123 | ||
7124 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7125 | { | |
7126 | struct io_sq_data *sqd = ctx->sq_data; | |
7127 | ||
7128 | if (sqd) { | |
eb85890b | 7129 | complete(&sqd->startup); |
534ca6d6 | 7130 | if (sqd->thread) { |
37d1e2e3 JA |
7131 | wait_for_completion(&ctx->sq_thread_comp); |
7132 | io_sq_thread_park(sqd); | |
534ca6d6 JA |
7133 | } |
7134 | ||
37d1e2e3 JA |
7135 | mutex_lock(&sqd->ctx_lock); |
7136 | list_del(&ctx->sqd_list); | |
7137 | io_sqd_update_thread_idle(sqd); | |
7138 | mutex_unlock(&sqd->ctx_lock); | |
7139 | ||
7140 | if (sqd->thread) | |
7141 | io_sq_thread_unpark(sqd); | |
7142 | ||
7143 | io_put_sq_data(sqd); | |
7144 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7145 | } |
7146 | } | |
7147 | ||
aa06165d JA |
7148 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7149 | { | |
7150 | struct io_ring_ctx *ctx_attach; | |
7151 | struct io_sq_data *sqd; | |
7152 | struct fd f; | |
7153 | ||
7154 | f = fdget(p->wq_fd); | |
7155 | if (!f.file) | |
7156 | return ERR_PTR(-ENXIO); | |
7157 | if (f.file->f_op != &io_uring_fops) { | |
7158 | fdput(f); | |
7159 | return ERR_PTR(-EINVAL); | |
7160 | } | |
7161 | ||
7162 | ctx_attach = f.file->private_data; | |
7163 | sqd = ctx_attach->sq_data; | |
7164 | if (!sqd) { | |
7165 | fdput(f); | |
7166 | return ERR_PTR(-EINVAL); | |
7167 | } | |
7168 | ||
7169 | refcount_inc(&sqd->refs); | |
7170 | fdput(f); | |
7171 | return sqd; | |
7172 | } | |
7173 | ||
534ca6d6 JA |
7174 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7175 | { | |
7176 | struct io_sq_data *sqd; | |
7177 | ||
aa06165d JA |
7178 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7179 | return io_attach_sq_data(p); | |
7180 | ||
534ca6d6 JA |
7181 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7182 | if (!sqd) | |
7183 | return ERR_PTR(-ENOMEM); | |
7184 | ||
7185 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7186 | INIT_LIST_HEAD(&sqd->ctx_list); |
7187 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7188 | mutex_init(&sqd->ctx_lock); | |
7189 | mutex_init(&sqd->lock); | |
534ca6d6 | 7190 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 JA |
7191 | init_completion(&sqd->startup); |
7192 | init_completion(&sqd->completion); | |
7193 | init_completion(&sqd->exited); | |
534ca6d6 JA |
7194 | return sqd; |
7195 | } | |
7196 | ||
6b06314c | 7197 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7198 | /* |
7199 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7200 | * the io_uring can be safely unregistered on process exit, even if we have | |
7201 | * loops in the file referencing. | |
7202 | */ | |
7203 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7204 | { | |
7205 | struct sock *sk = ctx->ring_sock->sk; | |
7206 | struct scm_fp_list *fpl; | |
7207 | struct sk_buff *skb; | |
08a45173 | 7208 | int i, nr_files; |
6b06314c | 7209 | |
6b06314c JA |
7210 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7211 | if (!fpl) | |
7212 | return -ENOMEM; | |
7213 | ||
7214 | skb = alloc_skb(0, GFP_KERNEL); | |
7215 | if (!skb) { | |
7216 | kfree(fpl); | |
7217 | return -ENOMEM; | |
7218 | } | |
7219 | ||
7220 | skb->sk = sk; | |
6b06314c | 7221 | |
08a45173 | 7222 | nr_files = 0; |
62e398be | 7223 | fpl->user = get_uid(current_user()); |
6b06314c | 7224 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7225 | struct file *file = io_file_from_index(ctx, i + offset); |
7226 | ||
7227 | if (!file) | |
08a45173 | 7228 | continue; |
65e19f54 | 7229 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7230 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7231 | nr_files++; | |
6b06314c JA |
7232 | } |
7233 | ||
08a45173 JA |
7234 | if (nr_files) { |
7235 | fpl->max = SCM_MAX_FD; | |
7236 | fpl->count = nr_files; | |
7237 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7238 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7239 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7240 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7241 | |
08a45173 JA |
7242 | for (i = 0; i < nr_files; i++) |
7243 | fput(fpl->fp[i]); | |
7244 | } else { | |
7245 | kfree_skb(skb); | |
7246 | kfree(fpl); | |
7247 | } | |
6b06314c JA |
7248 | |
7249 | return 0; | |
7250 | } | |
7251 | ||
7252 | /* | |
7253 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7254 | * causes regular reference counting to break down. We rely on the UNIX | |
7255 | * garbage collection to take care of this problem for us. | |
7256 | */ | |
7257 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7258 | { | |
7259 | unsigned left, total; | |
7260 | int ret = 0; | |
7261 | ||
7262 | total = 0; | |
7263 | left = ctx->nr_user_files; | |
7264 | while (left) { | |
7265 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7266 | |
7267 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7268 | if (ret) | |
7269 | break; | |
7270 | left -= this_files; | |
7271 | total += this_files; | |
7272 | } | |
7273 | ||
7274 | if (!ret) | |
7275 | return 0; | |
7276 | ||
7277 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7278 | struct file *file = io_file_from_index(ctx, total); |
7279 | ||
7280 | if (file) | |
7281 | fput(file); | |
6b06314c JA |
7282 | total++; |
7283 | } | |
7284 | ||
7285 | return ret; | |
7286 | } | |
7287 | #else | |
7288 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7289 | { | |
7290 | return 0; | |
7291 | } | |
7292 | #endif | |
7293 | ||
269bbe5f | 7294 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7295 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7296 | { |
7297 | int i; | |
7298 | ||
7299 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7300 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7301 | unsigned this_files; |
7302 | ||
7303 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7304 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7305 | GFP_KERNEL); | |
7306 | if (!table->files) | |
7307 | break; | |
7308 | nr_files -= this_files; | |
7309 | } | |
7310 | ||
7311 | if (i == nr_tables) | |
7312 | return 0; | |
7313 | ||
7314 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7315 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7316 | kfree(table->files); |
7317 | } | |
7318 | return 1; | |
7319 | } | |
7320 | ||
50238531 | 7321 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7322 | { |
50238531 | 7323 | struct file *file = prsrc->file; |
05f3fb3c JA |
7324 | #if defined(CONFIG_UNIX) |
7325 | struct sock *sock = ctx->ring_sock->sk; | |
7326 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7327 | struct sk_buff *skb; | |
7328 | int i; | |
7329 | ||
7330 | __skb_queue_head_init(&list); | |
7331 | ||
7332 | /* | |
7333 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7334 | * remove this entry and rearrange the file array. | |
7335 | */ | |
7336 | skb = skb_dequeue(head); | |
7337 | while (skb) { | |
7338 | struct scm_fp_list *fp; | |
7339 | ||
7340 | fp = UNIXCB(skb).fp; | |
7341 | for (i = 0; i < fp->count; i++) { | |
7342 | int left; | |
7343 | ||
7344 | if (fp->fp[i] != file) | |
7345 | continue; | |
7346 | ||
7347 | unix_notinflight(fp->user, fp->fp[i]); | |
7348 | left = fp->count - 1 - i; | |
7349 | if (left) { | |
7350 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7351 | left * sizeof(struct file *)); | |
7352 | } | |
7353 | fp->count--; | |
7354 | if (!fp->count) { | |
7355 | kfree_skb(skb); | |
7356 | skb = NULL; | |
7357 | } else { | |
7358 | __skb_queue_tail(&list, skb); | |
7359 | } | |
7360 | fput(file); | |
7361 | file = NULL; | |
7362 | break; | |
7363 | } | |
7364 | ||
7365 | if (!file) | |
7366 | break; | |
7367 | ||
7368 | __skb_queue_tail(&list, skb); | |
7369 | ||
7370 | skb = skb_dequeue(head); | |
7371 | } | |
7372 | ||
7373 | if (skb_peek(&list)) { | |
7374 | spin_lock_irq(&head->lock); | |
7375 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7376 | __skb_queue_tail(head, skb); | |
7377 | spin_unlock_irq(&head->lock); | |
7378 | } | |
7379 | #else | |
7380 | fput(file); | |
7381 | #endif | |
7382 | } | |
7383 | ||
269bbe5f | 7384 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7385 | { |
269bbe5f BM |
7386 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7387 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7388 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7389 | |
269bbe5f BM |
7390 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7391 | list_del(&prsrc->list); | |
50238531 | 7392 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7393 | kfree(prsrc); |
65e19f54 | 7394 | } |
05589553 | 7395 | |
05589553 XW |
7396 | percpu_ref_exit(&ref_node->refs); |
7397 | kfree(ref_node); | |
269bbe5f | 7398 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7399 | } |
65e19f54 | 7400 | |
269bbe5f | 7401 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7402 | { |
7403 | struct io_ring_ctx *ctx; | |
7404 | struct llist_node *node; | |
7405 | ||
269bbe5f BM |
7406 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7407 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7408 | |
7409 | while (node) { | |
269bbe5f | 7410 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7411 | struct llist_node *next = node->next; |
7412 | ||
269bbe5f BM |
7413 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7414 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7415 | node = next; |
7416 | } | |
7417 | } | |
7418 | ||
ea64ec02 PB |
7419 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7420 | unsigned i) | |
2faf852d | 7421 | { |
ea64ec02 PB |
7422 | struct fixed_rsrc_table *table; |
7423 | ||
7424 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7425 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7426 | } | |
7427 | ||
00835dce | 7428 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7429 | { |
269bbe5f BM |
7430 | struct fixed_rsrc_ref_node *ref_node; |
7431 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7432 | struct io_ring_ctx *ctx; |
e297822b | 7433 | bool first_add = false; |
4a38aed2 | 7434 | int delay = HZ; |
65e19f54 | 7435 | |
269bbe5f BM |
7436 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7437 | data = ref_node->rsrc_data; | |
e297822b PB |
7438 | ctx = data->ctx; |
7439 | ||
2a63b2d9 | 7440 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7441 | ref_node->done = true; |
7442 | ||
d67d2263 BM |
7443 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7444 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7445 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7446 | /* recycle ref nodes in order */ |
7447 | if (!ref_node->done) | |
7448 | break; | |
7449 | list_del(&ref_node->node); | |
269bbe5f | 7450 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7451 | } |
2a63b2d9 | 7452 | io_rsrc_ref_unlock(ctx); |
05589553 | 7453 | |
e297822b | 7454 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7455 | delay = 0; |
05589553 | 7456 | |
4a38aed2 | 7457 | if (!delay) |
269bbe5f | 7458 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7459 | else if (first_add) |
269bbe5f | 7460 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7461 | } |
65e19f54 | 7462 | |
6802535d | 7463 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7464 | struct io_ring_ctx *ctx) |
05f3fb3c | 7465 | { |
269bbe5f | 7466 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7467 | |
05589553 XW |
7468 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7469 | if (!ref_node) | |
3e2224c5 | 7470 | return NULL; |
05f3fb3c | 7471 | |
00835dce | 7472 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7473 | 0, GFP_KERNEL)) { |
7474 | kfree(ref_node); | |
3e2224c5 | 7475 | return NULL; |
05589553 XW |
7476 | } |
7477 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7478 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7479 | ref_node->done = false; |
05589553 | 7480 | return ref_node; |
05589553 XW |
7481 | } |
7482 | ||
bc9744cd PB |
7483 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7484 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7485 | { |
269bbe5f | 7486 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7487 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7488 | } |
7489 | ||
269bbe5f | 7490 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7491 | { |
7492 | percpu_ref_exit(&ref_node->refs); | |
7493 | kfree(ref_node); | |
65e19f54 JA |
7494 | } |
7495 | ||
ea64ec02 | 7496 | |
6b06314c JA |
7497 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7498 | unsigned nr_args) | |
7499 | { | |
7500 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7501 | unsigned nr_tables, i; |
05f3fb3c | 7502 | struct file *file; |
600cf3f8 | 7503 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7504 | struct fixed_rsrc_ref_node *ref_node; |
7505 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7506 | |
05f3fb3c | 7507 | if (ctx->file_data) |
6b06314c JA |
7508 | return -EBUSY; |
7509 | if (!nr_args) | |
7510 | return -EINVAL; | |
7511 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7512 | return -EMFILE; | |
7513 | ||
1ad555c6 | 7514 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7515 | if (!file_data) |
05f3fb3c | 7516 | return -ENOMEM; |
13770a71 | 7517 | ctx->file_data = file_data; |
05f3fb3c | 7518 | |
65e19f54 | 7519 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7520 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7521 | GFP_KERNEL); |
600cf3f8 PB |
7522 | if (!file_data->table) |
7523 | goto out_free; | |
05f3fb3c | 7524 | |
600cf3f8 | 7525 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7526 | goto out_free; |
65e19f54 | 7527 | |
08a45173 | 7528 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7529 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7530 | ret = -EFAULT; | |
7531 | goto out_fput; | |
7532 | } | |
08a45173 | 7533 | /* allow sparse sets */ |
600cf3f8 | 7534 | if (fd == -1) |
08a45173 | 7535 | continue; |
6b06314c | 7536 | |
05f3fb3c | 7537 | file = fget(fd); |
6b06314c | 7538 | ret = -EBADF; |
05f3fb3c | 7539 | if (!file) |
600cf3f8 | 7540 | goto out_fput; |
05f3fb3c | 7541 | |
6b06314c JA |
7542 | /* |
7543 | * Don't allow io_uring instances to be registered. If UNIX | |
7544 | * isn't enabled, then this causes a reference cycle and this | |
7545 | * instance can never get freed. If UNIX is enabled we'll | |
7546 | * handle it just fine, but there's still no point in allowing | |
7547 | * a ring fd as it doesn't support regular read/write anyway. | |
7548 | */ | |
05f3fb3c JA |
7549 | if (file->f_op == &io_uring_fops) { |
7550 | fput(file); | |
600cf3f8 | 7551 | goto out_fput; |
6b06314c | 7552 | } |
ea64ec02 | 7553 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7554 | } |
7555 | ||
6b06314c | 7556 | ret = io_sqe_files_scm(ctx); |
05589553 | 7557 | if (ret) { |
6b06314c | 7558 | io_sqe_files_unregister(ctx); |
05589553 XW |
7559 | return ret; |
7560 | } | |
6b06314c | 7561 | |
bc9744cd | 7562 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7563 | if (!ref_node) { |
05589553 | 7564 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7565 | return -ENOMEM; |
05589553 | 7566 | } |
bc9744cd | 7567 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7568 | |
d67d2263 | 7569 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7570 | return ret; |
600cf3f8 PB |
7571 | out_fput: |
7572 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7573 | file = io_file_from_index(ctx, i); | |
7574 | if (file) | |
7575 | fput(file); | |
7576 | } | |
7577 | for (i = 0; i < nr_tables; i++) | |
7578 | kfree(file_data->table[i].files); | |
7579 | ctx->nr_user_files = 0; | |
600cf3f8 | 7580 | out_free: |
1ad555c6 | 7581 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7582 | ctx->file_data = NULL; |
6b06314c JA |
7583 | return ret; |
7584 | } | |
7585 | ||
c3a31e60 JA |
7586 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7587 | int index) | |
7588 | { | |
7589 | #if defined(CONFIG_UNIX) | |
7590 | struct sock *sock = ctx->ring_sock->sk; | |
7591 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7592 | struct sk_buff *skb; | |
7593 | ||
7594 | /* | |
7595 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7596 | * file set. If there's no room, fall back to allocating a new skb | |
7597 | * and filling it in. | |
7598 | */ | |
7599 | spin_lock_irq(&head->lock); | |
7600 | skb = skb_peek(head); | |
7601 | if (skb) { | |
7602 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7603 | ||
7604 | if (fpl->count < SCM_MAX_FD) { | |
7605 | __skb_unlink(skb, head); | |
7606 | spin_unlock_irq(&head->lock); | |
7607 | fpl->fp[fpl->count] = get_file(file); | |
7608 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7609 | fpl->count++; | |
7610 | spin_lock_irq(&head->lock); | |
7611 | __skb_queue_head(head, skb); | |
7612 | } else { | |
7613 | skb = NULL; | |
7614 | } | |
7615 | } | |
7616 | spin_unlock_irq(&head->lock); | |
7617 | ||
7618 | if (skb) { | |
7619 | fput(file); | |
7620 | return 0; | |
7621 | } | |
7622 | ||
7623 | return __io_sqe_files_scm(ctx, 1, index); | |
7624 | #else | |
7625 | return 0; | |
7626 | #endif | |
7627 | } | |
7628 | ||
50238531 | 7629 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7630 | { |
269bbe5f BM |
7631 | struct io_rsrc_put *prsrc; |
7632 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7633 | |
269bbe5f BM |
7634 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7635 | if (!prsrc) | |
a5318d3c | 7636 | return -ENOMEM; |
05f3fb3c | 7637 | |
50238531 | 7638 | prsrc->rsrc = rsrc; |
269bbe5f | 7639 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7640 | |
a5318d3c | 7641 | return 0; |
05f3fb3c JA |
7642 | } |
7643 | ||
269bbe5f BM |
7644 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7645 | struct file *file) | |
7646 | { | |
50238531 | 7647 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7648 | } |
7649 | ||
05f3fb3c | 7650 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7651 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7652 | unsigned nr_args) |
7653 | { | |
269bbe5f BM |
7654 | struct fixed_rsrc_data *data = ctx->file_data; |
7655 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7656 | struct file *file, **file_slot; |
c3a31e60 JA |
7657 | __s32 __user *fds; |
7658 | int fd, i, err; | |
7659 | __u32 done; | |
05589553 | 7660 | bool needs_switch = false; |
c3a31e60 | 7661 | |
05f3fb3c | 7662 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7663 | return -EOVERFLOW; |
7664 | if (done > ctx->nr_user_files) | |
7665 | return -EINVAL; | |
7666 | ||
bc9744cd | 7667 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
7668 | if (!ref_node) |
7669 | return -ENOMEM; | |
bc9744cd | 7670 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7671 | |
269bbe5f | 7672 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7673 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7674 | err = 0; |
7675 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7676 | err = -EFAULT; | |
7677 | break; | |
7678 | } | |
4e0377a1 | 7679 | if (fd == IORING_REGISTER_FILES_SKIP) |
7680 | continue; | |
7681 | ||
67973b93 | 7682 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7683 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7684 | ||
7685 | if (*file_slot) { | |
7686 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
7687 | if (err) |
7688 | break; | |
ea64ec02 | 7689 | *file_slot = NULL; |
05589553 | 7690 | needs_switch = true; |
c3a31e60 JA |
7691 | } |
7692 | if (fd != -1) { | |
c3a31e60 JA |
7693 | file = fget(fd); |
7694 | if (!file) { | |
7695 | err = -EBADF; | |
7696 | break; | |
7697 | } | |
7698 | /* | |
7699 | * Don't allow io_uring instances to be registered. If | |
7700 | * UNIX isn't enabled, then this causes a reference | |
7701 | * cycle and this instance can never get freed. If UNIX | |
7702 | * is enabled we'll handle it just fine, but there's | |
7703 | * still no point in allowing a ring fd as it doesn't | |
7704 | * support regular read/write anyway. | |
7705 | */ | |
7706 | if (file->f_op == &io_uring_fops) { | |
7707 | fput(file); | |
7708 | err = -EBADF; | |
7709 | break; | |
7710 | } | |
e68a3ff8 | 7711 | *file_slot = file; |
c3a31e60 | 7712 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7713 | if (err) { |
e68a3ff8 | 7714 | *file_slot = NULL; |
f3bd9dae | 7715 | fput(file); |
c3a31e60 | 7716 | break; |
f3bd9dae | 7717 | } |
c3a31e60 | 7718 | } |
05f3fb3c JA |
7719 | } |
7720 | ||
05589553 | 7721 | if (needs_switch) { |
b2e96852 | 7722 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 7723 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 7724 | } else |
269bbe5f | 7725 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
7726 | |
7727 | return done ? done : err; | |
7728 | } | |
05589553 | 7729 | |
05f3fb3c JA |
7730 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7731 | unsigned nr_args) | |
7732 | { | |
269bbe5f | 7733 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7734 | |
7735 | if (!ctx->file_data) | |
7736 | return -ENXIO; | |
7737 | if (!nr_args) | |
7738 | return -EINVAL; | |
7739 | if (copy_from_user(&up, arg, sizeof(up))) | |
7740 | return -EFAULT; | |
7741 | if (up.resv) | |
7742 | return -EINVAL; | |
7743 | ||
7744 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7745 | } | |
c3a31e60 | 7746 | |
5280f7e5 | 7747 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7748 | { |
7749 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7750 | ||
5280f7e5 PB |
7751 | req = io_put_req_find_next(req); |
7752 | return req ? &req->work : NULL; | |
7d723065 JA |
7753 | } |
7754 | ||
5aa75ed5 | 7755 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
24369c2e | 7756 | { |
e941894e | 7757 | struct io_wq_hash *hash; |
24369c2e | 7758 | struct io_wq_data data; |
24369c2e | 7759 | unsigned int concurrency; |
24369c2e | 7760 | |
e941894e JA |
7761 | hash = ctx->hash_map; |
7762 | if (!hash) { | |
7763 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
7764 | if (!hash) | |
7765 | return ERR_PTR(-ENOMEM); | |
7766 | refcount_set(&hash->refs, 1); | |
7767 | init_waitqueue_head(&hash->wait); | |
7768 | ctx->hash_map = hash; | |
24369c2e PB |
7769 | } |
7770 | ||
e941894e | 7771 | data.hash = hash; |
e9fd9396 | 7772 | data.free_work = io_free_work; |
f5fa38c5 | 7773 | data.do_work = io_wq_submit_work; |
24369c2e | 7774 | |
d25e3a3d JA |
7775 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7776 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7777 | |
5aa75ed5 | 7778 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7779 | } |
7780 | ||
5aa75ed5 JA |
7781 | static int io_uring_alloc_task_context(struct task_struct *task, |
7782 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7783 | { |
7784 | struct io_uring_task *tctx; | |
d8a6df10 | 7785 | int ret; |
0f212204 JA |
7786 | |
7787 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7788 | if (unlikely(!tctx)) | |
7789 | return -ENOMEM; | |
7790 | ||
d8a6df10 JA |
7791 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7792 | if (unlikely(ret)) { | |
7793 | kfree(tctx); | |
7794 | return ret; | |
7795 | } | |
7796 | ||
5aa75ed5 JA |
7797 | tctx->io_wq = io_init_wq_offload(ctx); |
7798 | if (IS_ERR(tctx->io_wq)) { | |
7799 | ret = PTR_ERR(tctx->io_wq); | |
7800 | percpu_counter_destroy(&tctx->inflight); | |
7801 | kfree(tctx); | |
7802 | return ret; | |
7803 | } | |
7804 | ||
0f212204 JA |
7805 | xa_init(&tctx->xa); |
7806 | init_waitqueue_head(&tctx->wait); | |
7807 | tctx->last = NULL; | |
fdaf083c JA |
7808 | atomic_set(&tctx->in_idle, 0); |
7809 | tctx->sqpoll = false; | |
0f212204 | 7810 | task->io_uring = tctx; |
7cbf1722 JA |
7811 | spin_lock_init(&tctx->task_lock); |
7812 | INIT_WQ_LIST(&tctx->task_list); | |
7813 | tctx->task_state = 0; | |
7814 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7815 | return 0; |
7816 | } | |
7817 | ||
7818 | void __io_uring_free(struct task_struct *tsk) | |
7819 | { | |
7820 | struct io_uring_task *tctx = tsk->io_uring; | |
7821 | ||
7822 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e PB |
7823 | WARN_ON_ONCE(tctx->io_wq); |
7824 | ||
d8a6df10 | 7825 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7826 | kfree(tctx); |
7827 | tsk->io_uring = NULL; | |
7828 | } | |
7829 | ||
5f3f26f9 JA |
7830 | static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx) |
7831 | { | |
7832 | int ret; | |
7833 | ||
7834 | clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
7835 | reinit_completion(&sqd->completion); | |
70aacfe6 | 7836 | ctx->sqo_exec = 0; |
5f3f26f9 JA |
7837 | sqd->task_pid = current->pid; |
7838 | current->flags |= PF_IO_WORKER; | |
7839 | ret = io_wq_fork_thread(io_sq_thread, sqd); | |
7840 | current->flags &= ~PF_IO_WORKER; | |
7841 | if (ret < 0) { | |
7842 | sqd->thread = NULL; | |
7843 | return ret; | |
7844 | } | |
7845 | wait_for_completion(&sqd->completion); | |
7846 | return io_uring_alloc_task_context(sqd->thread, ctx); | |
7847 | } | |
7848 | ||
7e84e1c7 SG |
7849 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7850 | struct io_uring_params *p) | |
2b188cc1 JA |
7851 | { |
7852 | int ret; | |
7853 | ||
d25e3a3d JA |
7854 | /* Retain compatibility with failing for an invalid attach attempt */ |
7855 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7856 | IORING_SETUP_ATTACH_WQ) { | |
7857 | struct fd f; | |
7858 | ||
7859 | f = fdget(p->wq_fd); | |
7860 | if (!f.file) | |
7861 | return -ENXIO; | |
7862 | if (f.file->f_op != &io_uring_fops) { | |
7863 | fdput(f); | |
7864 | return -EINVAL; | |
7865 | } | |
7866 | fdput(f); | |
7867 | } | |
6c271ce2 | 7868 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
7869 | struct io_sq_data *sqd; |
7870 | ||
3ec482d1 | 7871 | ret = -EPERM; |
ce59fc69 | 7872 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
7873 | goto err; |
7874 | ||
534ca6d6 JA |
7875 | sqd = io_get_sq_data(p); |
7876 | if (IS_ERR(sqd)) { | |
7877 | ret = PTR_ERR(sqd); | |
7878 | goto err; | |
7879 | } | |
69fb2131 | 7880 | |
534ca6d6 | 7881 | ctx->sq_data = sqd; |
69fb2131 JA |
7882 | io_sq_thread_park(sqd); |
7883 | mutex_lock(&sqd->ctx_lock); | |
7884 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
7885 | mutex_unlock(&sqd->ctx_lock); | |
7886 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 7887 | |
917257da JA |
7888 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7889 | if (!ctx->sq_thread_idle) | |
7890 | ctx->sq_thread_idle = HZ; | |
7891 | ||
aa06165d | 7892 | if (sqd->thread) |
5aa75ed5 | 7893 | return 0; |
aa06165d | 7894 | |
6c271ce2 | 7895 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7896 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7897 | |
917257da | 7898 | ret = -EINVAL; |
44a9bd18 JA |
7899 | if (cpu >= nr_cpu_ids) |
7900 | goto err; | |
7889f44d | 7901 | if (!cpu_online(cpu)) |
917257da JA |
7902 | goto err; |
7903 | ||
37d1e2e3 | 7904 | sqd->sq_cpu = cpu; |
6c271ce2 | 7905 | } else { |
37d1e2e3 | 7906 | sqd->sq_cpu = -1; |
6c271ce2 | 7907 | } |
37d1e2e3 JA |
7908 | |
7909 | sqd->task_pid = current->pid; | |
7910 | current->flags |= PF_IO_WORKER; | |
7911 | ret = io_wq_fork_thread(io_sq_thread, sqd); | |
7912 | current->flags &= ~PF_IO_WORKER; | |
7913 | if (ret < 0) { | |
534ca6d6 | 7914 | sqd->thread = NULL; |
6c271ce2 JA |
7915 | goto err; |
7916 | } | |
37d1e2e3 | 7917 | wait_for_completion(&sqd->completion); |
5aa75ed5 | 7918 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
0f212204 JA |
7919 | if (ret) |
7920 | goto err; | |
6c271ce2 JA |
7921 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7922 | /* Can't have SQ_AFF without SQPOLL */ | |
7923 | ret = -EINVAL; | |
7924 | goto err; | |
7925 | } | |
7926 | ||
2b188cc1 JA |
7927 | return 0; |
7928 | err: | |
37d1e2e3 | 7929 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
7930 | return ret; |
7931 | } | |
7932 | ||
7e84e1c7 SG |
7933 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7934 | { | |
534ca6d6 JA |
7935 | struct io_sq_data *sqd = ctx->sq_data; |
7936 | ||
3ebba796 | 7937 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
eb85890b | 7938 | if (ctx->flags & IORING_SETUP_SQPOLL) |
37d1e2e3 | 7939 | complete(&sqd->startup); |
7e84e1c7 SG |
7940 | } |
7941 | ||
a087e2b5 BM |
7942 | static inline void __io_unaccount_mem(struct user_struct *user, |
7943 | unsigned long nr_pages) | |
2b188cc1 JA |
7944 | { |
7945 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7946 | } | |
7947 | ||
a087e2b5 BM |
7948 | static inline int __io_account_mem(struct user_struct *user, |
7949 | unsigned long nr_pages) | |
2b188cc1 JA |
7950 | { |
7951 | unsigned long page_limit, cur_pages, new_pages; | |
7952 | ||
7953 | /* Don't allow more pages than we can safely lock */ | |
7954 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7955 | ||
7956 | do { | |
7957 | cur_pages = atomic_long_read(&user->locked_vm); | |
7958 | new_pages = cur_pages + nr_pages; | |
7959 | if (new_pages > page_limit) | |
7960 | return -ENOMEM; | |
7961 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7962 | new_pages) != cur_pages); | |
7963 | ||
7964 | return 0; | |
7965 | } | |
7966 | ||
26bfa89e | 7967 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7968 | { |
62e398be | 7969 | if (ctx->user) |
a087e2b5 | 7970 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7971 | |
26bfa89e JA |
7972 | if (ctx->mm_account) |
7973 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7974 | } |
7975 | ||
26bfa89e | 7976 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7977 | { |
30975825 BM |
7978 | int ret; |
7979 | ||
62e398be | 7980 | if (ctx->user) { |
30975825 BM |
7981 | ret = __io_account_mem(ctx->user, nr_pages); |
7982 | if (ret) | |
7983 | return ret; | |
7984 | } | |
7985 | ||
26bfa89e JA |
7986 | if (ctx->mm_account) |
7987 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7988 | |
7989 | return 0; | |
7990 | } | |
7991 | ||
2b188cc1 JA |
7992 | static void io_mem_free(void *ptr) |
7993 | { | |
52e04ef4 MR |
7994 | struct page *page; |
7995 | ||
7996 | if (!ptr) | |
7997 | return; | |
2b188cc1 | 7998 | |
52e04ef4 | 7999 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8000 | if (put_page_testzero(page)) |
8001 | free_compound_page(page); | |
8002 | } | |
8003 | ||
8004 | static void *io_mem_alloc(size_t size) | |
8005 | { | |
8006 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8007 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8008 | |
8009 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8010 | } | |
8011 | ||
75b28aff HV |
8012 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8013 | size_t *sq_offset) | |
8014 | { | |
8015 | struct io_rings *rings; | |
8016 | size_t off, sq_array_size; | |
8017 | ||
8018 | off = struct_size(rings, cqes, cq_entries); | |
8019 | if (off == SIZE_MAX) | |
8020 | return SIZE_MAX; | |
8021 | ||
8022 | #ifdef CONFIG_SMP | |
8023 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8024 | if (off == 0) | |
8025 | return SIZE_MAX; | |
8026 | #endif | |
8027 | ||
b36200f5 DV |
8028 | if (sq_offset) |
8029 | *sq_offset = off; | |
8030 | ||
75b28aff HV |
8031 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8032 | if (sq_array_size == SIZE_MAX) | |
8033 | return SIZE_MAX; | |
8034 | ||
8035 | if (check_add_overflow(off, sq_array_size, &off)) | |
8036 | return SIZE_MAX; | |
8037 | ||
75b28aff HV |
8038 | return off; |
8039 | } | |
8040 | ||
0a96bbe4 | 8041 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8042 | { |
8043 | int i, j; | |
8044 | ||
8045 | if (!ctx->user_bufs) | |
8046 | return -ENXIO; | |
8047 | ||
8048 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8049 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8050 | ||
8051 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8052 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8053 | |
de293938 | 8054 | if (imu->acct_pages) |
26bfa89e | 8055 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8056 | kvfree(imu->bvec); |
edafccee JA |
8057 | imu->nr_bvecs = 0; |
8058 | } | |
8059 | ||
8060 | kfree(ctx->user_bufs); | |
8061 | ctx->user_bufs = NULL; | |
8062 | ctx->nr_user_bufs = 0; | |
8063 | return 0; | |
8064 | } | |
8065 | ||
8066 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8067 | void __user *arg, unsigned index) | |
8068 | { | |
8069 | struct iovec __user *src; | |
8070 | ||
8071 | #ifdef CONFIG_COMPAT | |
8072 | if (ctx->compat) { | |
8073 | struct compat_iovec __user *ciovs; | |
8074 | struct compat_iovec ciov; | |
8075 | ||
8076 | ciovs = (struct compat_iovec __user *) arg; | |
8077 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8078 | return -EFAULT; | |
8079 | ||
d55e5f5b | 8080 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8081 | dst->iov_len = ciov.iov_len; |
8082 | return 0; | |
8083 | } | |
8084 | #endif | |
8085 | src = (struct iovec __user *) arg; | |
8086 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8087 | return -EFAULT; | |
8088 | return 0; | |
8089 | } | |
8090 | ||
de293938 JA |
8091 | /* |
8092 | * Not super efficient, but this is just a registration time. And we do cache | |
8093 | * the last compound head, so generally we'll only do a full search if we don't | |
8094 | * match that one. | |
8095 | * | |
8096 | * We check if the given compound head page has already been accounted, to | |
8097 | * avoid double accounting it. This allows us to account the full size of the | |
8098 | * page, not just the constituent pages of a huge page. | |
8099 | */ | |
8100 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8101 | int nr_pages, struct page *hpage) | |
8102 | { | |
8103 | int i, j; | |
8104 | ||
8105 | /* check current page array */ | |
8106 | for (i = 0; i < nr_pages; i++) { | |
8107 | if (!PageCompound(pages[i])) | |
8108 | continue; | |
8109 | if (compound_head(pages[i]) == hpage) | |
8110 | return true; | |
8111 | } | |
8112 | ||
8113 | /* check previously registered pages */ | |
8114 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8115 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8116 | ||
8117 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8118 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8119 | continue; | |
8120 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8121 | return true; | |
8122 | } | |
8123 | } | |
8124 | ||
8125 | return false; | |
8126 | } | |
8127 | ||
8128 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8129 | int nr_pages, struct io_mapped_ubuf *imu, | |
8130 | struct page **last_hpage) | |
8131 | { | |
8132 | int i, ret; | |
8133 | ||
8134 | for (i = 0; i < nr_pages; i++) { | |
8135 | if (!PageCompound(pages[i])) { | |
8136 | imu->acct_pages++; | |
8137 | } else { | |
8138 | struct page *hpage; | |
8139 | ||
8140 | hpage = compound_head(pages[i]); | |
8141 | if (hpage == *last_hpage) | |
8142 | continue; | |
8143 | *last_hpage = hpage; | |
8144 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8145 | continue; | |
8146 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8147 | } | |
8148 | } | |
8149 | ||
8150 | if (!imu->acct_pages) | |
8151 | return 0; | |
8152 | ||
26bfa89e | 8153 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8154 | if (ret) |
8155 | imu->acct_pages = 0; | |
8156 | return ret; | |
8157 | } | |
8158 | ||
0a96bbe4 BM |
8159 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8160 | struct io_mapped_ubuf *imu, | |
8161 | struct page **last_hpage) | |
edafccee JA |
8162 | { |
8163 | struct vm_area_struct **vmas = NULL; | |
8164 | struct page **pages = NULL; | |
0a96bbe4 BM |
8165 | unsigned long off, start, end, ubuf; |
8166 | size_t size; | |
8167 | int ret, pret, nr_pages, i; | |
8168 | ||
8169 | ubuf = (unsigned long) iov->iov_base; | |
8170 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8171 | start = ubuf >> PAGE_SHIFT; | |
8172 | nr_pages = end - start; | |
8173 | ||
8174 | ret = -ENOMEM; | |
8175 | ||
8176 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8177 | if (!pages) | |
8178 | goto done; | |
8179 | ||
8180 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8181 | GFP_KERNEL); | |
8182 | if (!vmas) | |
8183 | goto done; | |
edafccee | 8184 | |
0a96bbe4 BM |
8185 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8186 | GFP_KERNEL); | |
8187 | if (!imu->bvec) | |
8188 | goto done; | |
8189 | ||
8190 | ret = 0; | |
8191 | mmap_read_lock(current->mm); | |
8192 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8193 | pages, vmas); | |
8194 | if (pret == nr_pages) { | |
8195 | /* don't support file backed memory */ | |
8196 | for (i = 0; i < nr_pages; i++) { | |
8197 | struct vm_area_struct *vma = vmas[i]; | |
8198 | ||
8199 | if (vma->vm_file && | |
8200 | !is_file_hugepages(vma->vm_file)) { | |
8201 | ret = -EOPNOTSUPP; | |
8202 | break; | |
8203 | } | |
8204 | } | |
8205 | } else { | |
8206 | ret = pret < 0 ? pret : -EFAULT; | |
8207 | } | |
8208 | mmap_read_unlock(current->mm); | |
8209 | if (ret) { | |
8210 | /* | |
8211 | * if we did partial map, or found file backed vmas, | |
8212 | * release any pages we did get | |
8213 | */ | |
8214 | if (pret > 0) | |
8215 | unpin_user_pages(pages, pret); | |
8216 | kvfree(imu->bvec); | |
8217 | goto done; | |
8218 | } | |
8219 | ||
8220 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8221 | if (ret) { | |
8222 | unpin_user_pages(pages, pret); | |
8223 | kvfree(imu->bvec); | |
8224 | goto done; | |
8225 | } | |
8226 | ||
8227 | off = ubuf & ~PAGE_MASK; | |
8228 | size = iov->iov_len; | |
8229 | for (i = 0; i < nr_pages; i++) { | |
8230 | size_t vec_len; | |
8231 | ||
8232 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8233 | imu->bvec[i].bv_page = pages[i]; | |
8234 | imu->bvec[i].bv_len = vec_len; | |
8235 | imu->bvec[i].bv_offset = off; | |
8236 | off = 0; | |
8237 | size -= vec_len; | |
8238 | } | |
8239 | /* store original address for later verification */ | |
8240 | imu->ubuf = ubuf; | |
8241 | imu->len = iov->iov_len; | |
8242 | imu->nr_bvecs = nr_pages; | |
8243 | ret = 0; | |
8244 | done: | |
8245 | kvfree(pages); | |
8246 | kvfree(vmas); | |
8247 | return ret; | |
8248 | } | |
8249 | ||
2b358604 | 8250 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8251 | { |
edafccee JA |
8252 | if (ctx->user_bufs) |
8253 | return -EBUSY; | |
8254 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8255 | return -EINVAL; | |
8256 | ||
8257 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8258 | GFP_KERNEL); | |
8259 | if (!ctx->user_bufs) | |
8260 | return -ENOMEM; | |
8261 | ||
2b358604 BM |
8262 | return 0; |
8263 | } | |
edafccee | 8264 | |
2b358604 BM |
8265 | static int io_buffer_validate(struct iovec *iov) |
8266 | { | |
8267 | /* | |
8268 | * Don't impose further limits on the size and buffer | |
8269 | * constraints here, we'll -EINVAL later when IO is | |
8270 | * submitted if they are wrong. | |
8271 | */ | |
8272 | if (!iov->iov_base || !iov->iov_len) | |
8273 | return -EFAULT; | |
edafccee | 8274 | |
2b358604 BM |
8275 | /* arbitrary limit, but we need something */ |
8276 | if (iov->iov_len > SZ_1G) | |
8277 | return -EFAULT; | |
edafccee | 8278 | |
2b358604 BM |
8279 | return 0; |
8280 | } | |
edafccee | 8281 | |
2b358604 BM |
8282 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8283 | unsigned int nr_args) | |
8284 | { | |
8285 | int i, ret; | |
8286 | struct iovec iov; | |
8287 | struct page *last_hpage = NULL; | |
edafccee | 8288 | |
2b358604 BM |
8289 | ret = io_buffers_map_alloc(ctx, nr_args); |
8290 | if (ret) | |
8291 | return ret; | |
edafccee | 8292 | |
edafccee JA |
8293 | for (i = 0; i < nr_args; i++) { |
8294 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8295 | |
edafccee JA |
8296 | ret = io_copy_iov(ctx, &iov, arg, i); |
8297 | if (ret) | |
0a96bbe4 | 8298 | break; |
de293938 | 8299 | |
2b358604 BM |
8300 | ret = io_buffer_validate(&iov); |
8301 | if (ret) | |
0a96bbe4 | 8302 | break; |
edafccee | 8303 | |
0a96bbe4 BM |
8304 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8305 | if (ret) | |
8306 | break; | |
edafccee JA |
8307 | |
8308 | ctx->nr_user_bufs++; | |
8309 | } | |
0a96bbe4 BM |
8310 | |
8311 | if (ret) | |
8312 | io_sqe_buffers_unregister(ctx); | |
8313 | ||
edafccee JA |
8314 | return ret; |
8315 | } | |
8316 | ||
9b402849 JA |
8317 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8318 | { | |
8319 | __s32 __user *fds = arg; | |
8320 | int fd; | |
8321 | ||
8322 | if (ctx->cq_ev_fd) | |
8323 | return -EBUSY; | |
8324 | ||
8325 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8326 | return -EFAULT; | |
8327 | ||
8328 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8329 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8330 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8331 | ctx->cq_ev_fd = NULL; | |
8332 | return ret; | |
8333 | } | |
8334 | ||
8335 | return 0; | |
8336 | } | |
8337 | ||
8338 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8339 | { | |
8340 | if (ctx->cq_ev_fd) { | |
8341 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8342 | ctx->cq_ev_fd = NULL; | |
8343 | return 0; | |
8344 | } | |
8345 | ||
8346 | return -ENXIO; | |
8347 | } | |
8348 | ||
5a2e745d JA |
8349 | static int __io_destroy_buffers(int id, void *p, void *data) |
8350 | { | |
8351 | struct io_ring_ctx *ctx = data; | |
8352 | struct io_buffer *buf = p; | |
8353 | ||
067524e9 | 8354 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8355 | return 0; |
8356 | } | |
8357 | ||
8358 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8359 | { | |
8360 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8361 | idr_destroy(&ctx->io_buffer_idr); | |
8362 | } | |
8363 | ||
68e68ee6 | 8364 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8365 | { |
68e68ee6 | 8366 | struct io_kiocb *req, *nxt; |
1b4c351f | 8367 | |
68e68ee6 JA |
8368 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8369 | if (tsk && req->task != tsk) | |
8370 | continue; | |
1b4c351f JA |
8371 | list_del(&req->compl.list); |
8372 | kmem_cache_free(req_cachep, req); | |
8373 | } | |
8374 | } | |
8375 | ||
4010fec4 | 8376 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8377 | { |
bf019da7 | 8378 | struct io_submit_state *submit_state = &ctx->submit_state; |
e5547d2c | 8379 | struct io_comp_state *cs = &ctx->submit_state.comp; |
bf019da7 | 8380 | |
9a4fdbd8 JA |
8381 | mutex_lock(&ctx->uring_lock); |
8382 | ||
8e5c66c4 | 8383 | if (submit_state->free_reqs) { |
9a4fdbd8 JA |
8384 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
8385 | submit_state->reqs); | |
8e5c66c4 PB |
8386 | submit_state->free_reqs = 0; |
8387 | } | |
9a4fdbd8 JA |
8388 | |
8389 | spin_lock_irq(&ctx->completion_lock); | |
e5547d2c PB |
8390 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
8391 | cs->locked_free_nr = 0; | |
9a4fdbd8 JA |
8392 | spin_unlock_irq(&ctx->completion_lock); |
8393 | ||
e5547d2c PB |
8394 | io_req_cache_free(&cs->free_list, NULL); |
8395 | ||
9a4fdbd8 JA |
8396 | mutex_unlock(&ctx->uring_lock); |
8397 | } | |
8398 | ||
2b188cc1 JA |
8399 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8400 | { | |
04fc6c80 PB |
8401 | /* |
8402 | * Some may use context even when all refs and requests have been put, | |
8403 | * and they are free to do so while still holding uring_lock, see | |
8404 | * __io_req_task_submit(). Wait for them to finish. | |
8405 | */ | |
8406 | mutex_lock(&ctx->uring_lock); | |
8407 | mutex_unlock(&ctx->uring_lock); | |
8408 | ||
37d1e2e3 | 8409 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8410 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8411 | |
37d1e2e3 | 8412 | if (ctx->mm_account) { |
2aede0e4 JA |
8413 | mmdrop(ctx->mm_account); |
8414 | ctx->mm_account = NULL; | |
30975825 | 8415 | } |
def596e9 | 8416 | |
8bad28d8 | 8417 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8418 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8419 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8420 | io_eventfd_unregister(ctx); |
5a2e745d | 8421 | io_destroy_buffers(ctx); |
41726c9a | 8422 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8423 | |
2b188cc1 | 8424 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8425 | if (ctx->ring_sock) { |
8426 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8427 | sock_release(ctx->ring_sock); |
355e8d26 | 8428 | } |
2b188cc1 JA |
8429 | #endif |
8430 | ||
75b28aff | 8431 | io_mem_free(ctx->rings); |
2b188cc1 | 8432 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8433 | |
8434 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8435 | free_uid(ctx->user); |
4010fec4 | 8436 | io_req_caches_free(ctx); |
e941894e JA |
8437 | if (ctx->hash_map) |
8438 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8439 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8440 | kfree(ctx); |
8441 | } | |
8442 | ||
8443 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8444 | { | |
8445 | struct io_ring_ctx *ctx = file->private_data; | |
8446 | __poll_t mask = 0; | |
8447 | ||
8448 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8449 | /* |
8450 | * synchronizes with barrier from wq_has_sleeper call in | |
8451 | * io_commit_cqring | |
8452 | */ | |
2b188cc1 | 8453 | smp_rmb(); |
90554200 | 8454 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8455 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8456 | |
8457 | /* | |
8458 | * Don't flush cqring overflow list here, just do a simple check. | |
8459 | * Otherwise there could possible be ABBA deadlock: | |
8460 | * CPU0 CPU1 | |
8461 | * ---- ---- | |
8462 | * lock(&ctx->uring_lock); | |
8463 | * lock(&ep->mtx); | |
8464 | * lock(&ctx->uring_lock); | |
8465 | * lock(&ep->mtx); | |
8466 | * | |
8467 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8468 | * pushs them to do the flush. | |
8469 | */ | |
8470 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8471 | mask |= EPOLLIN | EPOLLRDNORM; |
8472 | ||
8473 | return mask; | |
8474 | } | |
8475 | ||
8476 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8477 | { | |
8478 | struct io_ring_ctx *ctx = file->private_data; | |
8479 | ||
8480 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8481 | } | |
8482 | ||
0bead8cd | 8483 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8484 | { |
4379bf8b | 8485 | const struct cred *creds; |
071698e1 | 8486 | |
4379bf8b JA |
8487 | creds = idr_remove(&ctx->personality_idr, id); |
8488 | if (creds) { | |
8489 | put_cred(creds); | |
0bead8cd | 8490 | return 0; |
1e6fa521 | 8491 | } |
0bead8cd YD |
8492 | |
8493 | return -EINVAL; | |
8494 | } | |
8495 | ||
8496 | static int io_remove_personalities(int id, void *p, void *data) | |
8497 | { | |
8498 | struct io_ring_ctx *ctx = data; | |
8499 | ||
8500 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8501 | return 0; |
8502 | } | |
8503 | ||
ba50a036 | 8504 | static bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
7c25c0d1 | 8505 | { |
28c4721b | 8506 | struct callback_head *work, *next; |
ba50a036 | 8507 | bool executed = false; |
7c25c0d1 JA |
8508 | |
8509 | do { | |
28c4721b | 8510 | work = xchg(&ctx->exit_task_work, NULL); |
7c25c0d1 JA |
8511 | if (!work) |
8512 | break; | |
8513 | ||
8514 | do { | |
8515 | next = work->next; | |
8516 | work->func(work); | |
8517 | work = next; | |
8518 | cond_resched(); | |
8519 | } while (work); | |
ba50a036 | 8520 | executed = true; |
7c25c0d1 | 8521 | } while (1); |
ba50a036 PB |
8522 | |
8523 | return executed; | |
7c25c0d1 JA |
8524 | } |
8525 | ||
85faa7b8 JA |
8526 | static void io_ring_exit_work(struct work_struct *work) |
8527 | { | |
b2edc0a7 PB |
8528 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8529 | exit_work); | |
85faa7b8 | 8530 | |
56952e91 JA |
8531 | /* |
8532 | * If we're doing polled IO and end up having requests being | |
8533 | * submitted async (out-of-line), then completions can come in while | |
8534 | * we're waiting for refs to drop. We need to reap these manually, | |
8535 | * as nobody else will be looking for them. | |
8536 | */ | |
b2edc0a7 | 8537 | do { |
9936c7c2 | 8538 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b2edc0a7 | 8539 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8540 | io_ring_ctx_free(ctx); |
8541 | } | |
8542 | ||
2b188cc1 JA |
8543 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8544 | { | |
8545 | mutex_lock(&ctx->uring_lock); | |
8546 | percpu_ref_kill(&ctx->refs); | |
cda286f0 PB |
8547 | /* if force is set, the ring is going away. always drop after that */ |
8548 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8549 | if (ctx->rings) |
6c503150 | 8550 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8551 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8552 | mutex_unlock(&ctx->uring_lock); |
8553 | ||
6b81928d PB |
8554 | io_kill_timeouts(ctx, NULL, NULL); |
8555 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8556 | |
15dff286 | 8557 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8558 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8559 | |
85faa7b8 | 8560 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8561 | /* |
8562 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8563 | * if we're exiting a ton of rings at the same time. It just adds | |
8564 | * noise and overhead, there's no discernable change in runtime | |
8565 | * over using system_wq. | |
8566 | */ | |
8567 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8568 | } |
8569 | ||
8570 | static int io_uring_release(struct inode *inode, struct file *file) | |
8571 | { | |
8572 | struct io_ring_ctx *ctx = file->private_data; | |
8573 | ||
8574 | file->private_data = NULL; | |
8575 | io_ring_ctx_wait_and_kill(ctx); | |
8576 | return 0; | |
8577 | } | |
8578 | ||
f6edbabb PB |
8579 | struct io_task_cancel { |
8580 | struct task_struct *task; | |
8581 | struct files_struct *files; | |
8582 | }; | |
f254ac04 | 8583 | |
f6edbabb | 8584 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8585 | { |
9a472ef7 | 8586 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8587 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8588 | bool ret; |
8589 | ||
f6edbabb | 8590 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8591 | unsigned long flags; |
8592 | struct io_ring_ctx *ctx = req->ctx; | |
8593 | ||
8594 | /* protect against races with linked timeouts */ | |
8595 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8596 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8597 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8598 | } else { | |
f6edbabb | 8599 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8600 | } |
8601 | return ret; | |
b711d4ea JA |
8602 | } |
8603 | ||
b7ddce3c | 8604 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8605 | struct task_struct *task, |
b7ddce3c PB |
8606 | struct files_struct *files) |
8607 | { | |
8608 | struct io_defer_entry *de = NULL; | |
8609 | LIST_HEAD(list); | |
8610 | ||
8611 | spin_lock_irq(&ctx->completion_lock); | |
8612 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8613 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8614 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8615 | break; | |
8616 | } | |
8617 | } | |
8618 | spin_unlock_irq(&ctx->completion_lock); | |
8619 | ||
8620 | while (!list_empty(&list)) { | |
8621 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8622 | list_del_init(&de->list); | |
8623 | req_set_fail_links(de->req); | |
8624 | io_put_req(de->req); | |
8625 | io_req_complete(de->req, -ECANCELED); | |
8626 | kfree(de); | |
8627 | } | |
8628 | } | |
8629 | ||
9936c7c2 PB |
8630 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8631 | struct task_struct *task, | |
8632 | struct files_struct *files) | |
8633 | { | |
8634 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
64c72123 PB |
8635 | struct task_struct *tctx_task = task ?: current; |
8636 | struct io_uring_task *tctx = tctx_task->io_uring; | |
9936c7c2 PB |
8637 | |
8638 | while (1) { | |
8639 | enum io_wq_cancel cret; | |
8640 | bool ret = false; | |
8641 | ||
5aa75ed5 JA |
8642 | if (tctx && tctx->io_wq) { |
8643 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, | |
9936c7c2 PB |
8644 | &cancel, true); |
8645 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8646 | } | |
8647 | ||
8648 | /* SQPOLL thread does its own polling */ | |
8649 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8650 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8651 | io_iopoll_try_reap_events(ctx); | |
8652 | ret = true; | |
8653 | } | |
8654 | } | |
8655 | ||
8656 | ret |= io_poll_remove_all(ctx, task, files); | |
8657 | ret |= io_kill_timeouts(ctx, task, files); | |
8658 | ret |= io_run_task_work(); | |
ba50a036 | 8659 | ret |= io_run_ctx_fallback(ctx); |
9936c7c2 PB |
8660 | io_cqring_overflow_flush(ctx, true, task, files); |
8661 | if (!ret) | |
8662 | break; | |
8663 | cond_resched(); | |
8664 | } | |
8665 | } | |
8666 | ||
ca70f00b PB |
8667 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8668 | struct task_struct *task, | |
8669 | struct files_struct *files) | |
8670 | { | |
8671 | struct io_kiocb *req; | |
8672 | int cnt = 0; | |
8673 | ||
8674 | spin_lock_irq(&ctx->inflight_lock); | |
8675 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8676 | cnt += io_match_task(req, task, files); | |
8677 | spin_unlock_irq(&ctx->inflight_lock); | |
8678 | return cnt; | |
8679 | } | |
8680 | ||
b52fda00 | 8681 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8682 | struct task_struct *task, |
fcb323cc JA |
8683 | struct files_struct *files) |
8684 | { | |
fcb323cc | 8685 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 8686 | DEFINE_WAIT(wait); |
ca70f00b | 8687 | int inflight; |
fcb323cc | 8688 | |
ca70f00b PB |
8689 | inflight = io_uring_count_inflight(ctx, task, files); |
8690 | if (!inflight) | |
fcb323cc | 8691 | break; |
f6edbabb | 8692 | |
9936c7c2 | 8693 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 8694 | |
34343786 PB |
8695 | if (ctx->sq_data) |
8696 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
8697 | prepare_to_wait(&task->io_uring->wait, &wait, |
8698 | TASK_UNINTERRUPTIBLE); | |
8699 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8700 | schedule(); | |
c98de08c | 8701 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
8702 | if (ctx->sq_data) |
8703 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 8704 | } |
0f212204 JA |
8705 | } |
8706 | ||
8707 | /* | |
8708 | * We need to iteratively cancel requests, in case a request has dependent | |
8709 | * hard links. These persist even for failure of cancelations, hence keep | |
8710 | * looping until none are found. | |
8711 | */ | |
8712 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8713 | struct files_struct *files) | |
8714 | { | |
8715 | struct task_struct *task = current; | |
37d1e2e3 | 8716 | bool did_park = false; |
0f212204 | 8717 | |
fdaf083c | 8718 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
70aacfe6 PB |
8719 | /* never started, nothing to cancel */ |
8720 | if (ctx->flags & IORING_SETUP_R_DISABLED) { | |
8721 | io_sq_offload_start(ctx); | |
8722 | return; | |
8723 | } | |
37d1e2e3 JA |
8724 | did_park = io_sq_thread_park(ctx->sq_data); |
8725 | if (did_park) { | |
8726 | task = ctx->sq_data->thread; | |
8727 | atomic_inc(&task->io_uring->in_idle); | |
8728 | } | |
fdaf083c | 8729 | } |
0f212204 | 8730 | |
df9923f9 | 8731 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 8732 | |
3a7efd1a | 8733 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 8734 | if (!files) |
9936c7c2 | 8735 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c | 8736 | |
37d1e2e3 | 8737 | if (did_park) { |
fdaf083c | 8738 | atomic_dec(&task->io_uring->in_idle); |
fdaf083c JA |
8739 | io_sq_thread_unpark(ctx->sq_data); |
8740 | } | |
0f212204 JA |
8741 | } |
8742 | ||
8743 | /* | |
8744 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8745 | */ | |
fdaf083c | 8746 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8747 | { |
236434c3 | 8748 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 8749 | int ret; |
236434c3 MWO |
8750 | |
8751 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8752 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8753 | if (unlikely(ret)) |
8754 | return ret; | |
236434c3 | 8755 | tctx = current->io_uring; |
0f212204 | 8756 | } |
236434c3 MWO |
8757 | if (tctx->last != file) { |
8758 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8759 | |
236434c3 | 8760 | if (!old) { |
0f212204 | 8761 | get_file(file); |
a528b04e PB |
8762 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
8763 | file, GFP_KERNEL)); | |
8764 | if (ret) { | |
8765 | fput(file); | |
8766 | return ret; | |
8767 | } | |
0f212204 | 8768 | } |
236434c3 | 8769 | tctx->last = file; |
0f212204 JA |
8770 | } |
8771 | ||
fdaf083c JA |
8772 | /* |
8773 | * This is race safe in that the task itself is doing this, hence it | |
8774 | * cannot be going through the exit/cancel paths at the same time. | |
8775 | * This cannot be modified while exit/cancel is running. | |
8776 | */ | |
8777 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8778 | tctx->sqpoll = true; | |
8779 | ||
0f212204 JA |
8780 | return 0; |
8781 | } | |
8782 | ||
8783 | /* | |
8784 | * Remove this io_uring_file -> task mapping. | |
8785 | */ | |
8786 | static void io_uring_del_task_file(struct file *file) | |
8787 | { | |
8788 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
8789 | |
8790 | if (tctx->last == file) | |
8791 | tctx->last = NULL; | |
5e2ed8c4 | 8792 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
8793 | if (file) |
8794 | fput(file); | |
8795 | } | |
8796 | ||
8452d4a6 | 8797 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e PB |
8798 | { |
8799 | struct file *file; | |
8800 | unsigned long index; | |
8801 | ||
8802 | xa_for_each(&tctx->xa, index, file) | |
8803 | io_uring_del_task_file(file); | |
8452d4a6 PB |
8804 | if (tctx->io_wq) { |
8805 | io_wq_put_and_exit(tctx->io_wq); | |
8806 | tctx->io_wq = NULL; | |
8807 | } | |
de7f1d9e PB |
8808 | } |
8809 | ||
0f212204 JA |
8810 | void __io_uring_files_cancel(struct files_struct *files) |
8811 | { | |
8812 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
8813 | struct file *file; |
8814 | unsigned long index; | |
0f212204 JA |
8815 | |
8816 | /* make sure overflow events are dropped */ | |
fdaf083c | 8817 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
8818 | xa_for_each(&tctx->xa, index, file) |
8819 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 8820 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8821 | |
8452d4a6 PB |
8822 | if (files) |
8823 | io_uring_clean_tctx(tctx); | |
fdaf083c JA |
8824 | } |
8825 | ||
8826 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8827 | { | |
0e9ddb39 PB |
8828 | return percpu_counter_sum(&tctx->inflight); |
8829 | } | |
fdaf083c | 8830 | |
0e9ddb39 PB |
8831 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8832 | { | |
37d1e2e3 | 8833 | struct io_sq_data *sqd = ctx->sq_data; |
0e9ddb39 PB |
8834 | struct io_uring_task *tctx; |
8835 | s64 inflight; | |
8836 | DEFINE_WAIT(wait); | |
fdaf083c | 8837 | |
37d1e2e3 | 8838 | if (!sqd) |
0e9ddb39 | 8839 | return; |
37d1e2e3 JA |
8840 | if (!io_sq_thread_park(sqd)) |
8841 | return; | |
8842 | tctx = ctx->sq_data->thread->io_uring; | |
e54945ae JA |
8843 | /* can happen on fork/alloc failure, just ignore that state */ |
8844 | if (!tctx) { | |
8845 | io_sq_thread_unpark(sqd); | |
8846 | return; | |
8847 | } | |
fdaf083c | 8848 | |
0e9ddb39 PB |
8849 | atomic_inc(&tctx->in_idle); |
8850 | do { | |
8851 | /* read completions before cancelations */ | |
8852 | inflight = tctx_inflight(tctx); | |
8853 | if (!inflight) | |
8854 | break; | |
8855 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 8856 | |
0e9ddb39 PB |
8857 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
8858 | /* | |
8859 | * If we've seen completions, retry without waiting. This | |
8860 | * avoids a race where a completion comes in before we did | |
8861 | * prepare_to_wait(). | |
8862 | */ | |
8863 | if (inflight == tctx_inflight(tctx)) | |
8864 | schedule(); | |
8865 | finish_wait(&tctx->wait, &wait); | |
8866 | } while (1); | |
8867 | atomic_dec(&tctx->in_idle); | |
37d1e2e3 | 8868 | io_sq_thread_unpark(sqd); |
0f212204 JA |
8869 | } |
8870 | ||
0f212204 JA |
8871 | /* |
8872 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8873 | * requests. | |
8874 | */ | |
8875 | void __io_uring_task_cancel(void) | |
8876 | { | |
8877 | struct io_uring_task *tctx = current->io_uring; | |
8878 | DEFINE_WAIT(wait); | |
d8a6df10 | 8879 | s64 inflight; |
0f212204 JA |
8880 | |
8881 | /* make sure overflow events are dropped */ | |
fdaf083c | 8882 | atomic_inc(&tctx->in_idle); |
0f212204 | 8883 | |
0e9ddb39 PB |
8884 | if (tctx->sqpoll) { |
8885 | struct file *file; | |
8886 | unsigned long index; | |
8887 | ||
8888 | xa_for_each(&tctx->xa, index, file) | |
8889 | io_uring_cancel_sqpoll(file->private_data); | |
8890 | } | |
0b5cd6c3 | 8891 | |
d8a6df10 | 8892 | do { |
0f212204 | 8893 | /* read completions before cancelations */ |
fdaf083c | 8894 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
8895 | if (!inflight) |
8896 | break; | |
0f212204 JA |
8897 | __io_uring_files_cancel(NULL); |
8898 | ||
8899 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8900 | ||
8901 | /* | |
a1bb3cd5 PB |
8902 | * If we've seen completions, retry without waiting. This |
8903 | * avoids a race where a completion comes in before we did | |
8904 | * prepare_to_wait(). | |
0f212204 | 8905 | */ |
a1bb3cd5 PB |
8906 | if (inflight == tctx_inflight(tctx)) |
8907 | schedule(); | |
f57555ed | 8908 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 8909 | } while (1); |
0f212204 | 8910 | |
fdaf083c | 8911 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8912 | |
8452d4a6 PB |
8913 | io_uring_clean_tctx(tctx); |
8914 | /* all current's requests should be gone, we can kill tctx */ | |
8915 | __io_uring_free(current); | |
44e728b8 PB |
8916 | } |
8917 | ||
6c5c240e RP |
8918 | static void *io_uring_validate_mmap_request(struct file *file, |
8919 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8920 | { |
2b188cc1 | 8921 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8922 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8923 | struct page *page; |
8924 | void *ptr; | |
8925 | ||
8926 | switch (offset) { | |
8927 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8928 | case IORING_OFF_CQ_RING: |
8929 | ptr = ctx->rings; | |
2b188cc1 JA |
8930 | break; |
8931 | case IORING_OFF_SQES: | |
8932 | ptr = ctx->sq_sqes; | |
8933 | break; | |
2b188cc1 | 8934 | default: |
6c5c240e | 8935 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8936 | } |
8937 | ||
8938 | page = virt_to_head_page(ptr); | |
a50b854e | 8939 | if (sz > page_size(page)) |
6c5c240e RP |
8940 | return ERR_PTR(-EINVAL); |
8941 | ||
8942 | return ptr; | |
8943 | } | |
8944 | ||
8945 | #ifdef CONFIG_MMU | |
8946 | ||
8947 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8948 | { | |
8949 | size_t sz = vma->vm_end - vma->vm_start; | |
8950 | unsigned long pfn; | |
8951 | void *ptr; | |
8952 | ||
8953 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8954 | if (IS_ERR(ptr)) | |
8955 | return PTR_ERR(ptr); | |
2b188cc1 JA |
8956 | |
8957 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
8958 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
8959 | } | |
8960 | ||
6c5c240e RP |
8961 | #else /* !CONFIG_MMU */ |
8962 | ||
8963 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8964 | { | |
8965 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
8966 | } | |
8967 | ||
8968 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
8969 | { | |
8970 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
8971 | } | |
8972 | ||
8973 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
8974 | unsigned long addr, unsigned long len, | |
8975 | unsigned long pgoff, unsigned long flags) | |
8976 | { | |
8977 | void *ptr; | |
8978 | ||
8979 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
8980 | if (IS_ERR(ptr)) | |
8981 | return PTR_ERR(ptr); | |
8982 | ||
8983 | return (unsigned long) ptr; | |
8984 | } | |
8985 | ||
8986 | #endif /* !CONFIG_MMU */ | |
8987 | ||
d9d05217 | 8988 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 8989 | { |
d9d05217 | 8990 | int ret = 0; |
90554200 JA |
8991 | DEFINE_WAIT(wait); |
8992 | ||
8993 | do { | |
8994 | if (!io_sqring_full(ctx)) | |
8995 | break; | |
90554200 JA |
8996 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
8997 | ||
8998 | if (!io_sqring_full(ctx)) | |
8999 | break; | |
90554200 JA |
9000 | schedule(); |
9001 | } while (!signal_pending(current)); | |
9002 | ||
9003 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 | 9004 | return ret; |
90554200 JA |
9005 | } |
9006 | ||
c73ebb68 HX |
9007 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9008 | struct __kernel_timespec __user **ts, | |
9009 | const sigset_t __user **sig) | |
9010 | { | |
9011 | struct io_uring_getevents_arg arg; | |
9012 | ||
9013 | /* | |
9014 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9015 | * is just a pointer to the sigset_t. | |
9016 | */ | |
9017 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9018 | *sig = (const sigset_t __user *) argp; | |
9019 | *ts = NULL; | |
9020 | return 0; | |
9021 | } | |
9022 | ||
9023 | /* | |
9024 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9025 | * timespec and sigset_t pointers if good. | |
9026 | */ | |
9027 | if (*argsz != sizeof(arg)) | |
9028 | return -EINVAL; | |
9029 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9030 | return -EFAULT; | |
9031 | *sig = u64_to_user_ptr(arg.sigmask); | |
9032 | *argsz = arg.sigmask_sz; | |
9033 | *ts = u64_to_user_ptr(arg.ts); | |
9034 | return 0; | |
9035 | } | |
9036 | ||
2b188cc1 | 9037 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9038 | u32, min_complete, u32, flags, const void __user *, argp, |
9039 | size_t, argsz) | |
2b188cc1 JA |
9040 | { |
9041 | struct io_ring_ctx *ctx; | |
9042 | long ret = -EBADF; | |
9043 | int submitted = 0; | |
9044 | struct fd f; | |
9045 | ||
4c6e277c | 9046 | io_run_task_work(); |
b41e9852 | 9047 | |
90554200 | 9048 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9049 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9050 | return -EINVAL; |
9051 | ||
9052 | f = fdget(fd); | |
9053 | if (!f.file) | |
9054 | return -EBADF; | |
9055 | ||
9056 | ret = -EOPNOTSUPP; | |
9057 | if (f.file->f_op != &io_uring_fops) | |
9058 | goto out_fput; | |
9059 | ||
9060 | ret = -ENXIO; | |
9061 | ctx = f.file->private_data; | |
9062 | if (!percpu_ref_tryget(&ctx->refs)) | |
9063 | goto out_fput; | |
9064 | ||
7e84e1c7 SG |
9065 | ret = -EBADFD; |
9066 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9067 | goto out; | |
9068 | ||
6c271ce2 JA |
9069 | /* |
9070 | * For SQ polling, the thread will do all submissions and completions. | |
9071 | * Just return the requested submit count, and wake the thread if | |
9072 | * we were asked to. | |
9073 | */ | |
b2a9eada | 9074 | ret = 0; |
6c271ce2 | 9075 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9076 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9077 | |
5f3f26f9 JA |
9078 | if (unlikely(ctx->sqo_exec)) { |
9079 | ret = io_sq_thread_fork(ctx->sq_data, ctx); | |
9080 | if (ret) | |
9081 | goto out; | |
9082 | ctx->sqo_exec = 0; | |
9083 | } | |
d9d05217 | 9084 | ret = -EOWNERDEAD; |
6c271ce2 | 9085 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9086 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9087 | if (flags & IORING_ENTER_SQ_WAIT) { |
9088 | ret = io_sqpoll_wait_sq(ctx); | |
9089 | if (ret) | |
9090 | goto out; | |
9091 | } | |
6c271ce2 | 9092 | submitted = to_submit; |
b2a9eada | 9093 | } else if (to_submit) { |
fdaf083c | 9094 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9095 | if (unlikely(ret)) |
9096 | goto out; | |
2b188cc1 | 9097 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9098 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9099 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9100 | |
9101 | if (submitted != to_submit) | |
9102 | goto out; | |
2b188cc1 JA |
9103 | } |
9104 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9105 | const sigset_t __user *sig; |
9106 | struct __kernel_timespec __user *ts; | |
9107 | ||
9108 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9109 | if (unlikely(ret)) | |
9110 | goto out; | |
9111 | ||
2b188cc1 JA |
9112 | min_complete = min(min_complete, ctx->cq_entries); |
9113 | ||
32b2244a XW |
9114 | /* |
9115 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9116 | * space applications don't need to do io completion events | |
9117 | * polling again, they can rely on io_sq_thread to do polling | |
9118 | * work, which can reduce cpu usage and uring_lock contention. | |
9119 | */ | |
9120 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9121 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9122 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9123 | } else { |
c73ebb68 | 9124 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9125 | } |
2b188cc1 JA |
9126 | } |
9127 | ||
7c504e65 | 9128 | out: |
6805b32e | 9129 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9130 | out_fput: |
9131 | fdput(f); | |
9132 | return submitted ? submitted : ret; | |
9133 | } | |
9134 | ||
bebdb65e | 9135 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9136 | static int io_uring_show_cred(int id, void *p, void *data) |
9137 | { | |
4379bf8b | 9138 | const struct cred *cred = p; |
87ce955b JA |
9139 | struct seq_file *m = data; |
9140 | struct user_namespace *uns = seq_user_ns(m); | |
9141 | struct group_info *gi; | |
9142 | kernel_cap_t cap; | |
9143 | unsigned __capi; | |
9144 | int g; | |
9145 | ||
9146 | seq_printf(m, "%5d\n", id); | |
9147 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9148 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9149 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9150 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9151 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9152 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9153 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9154 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9155 | seq_puts(m, "\n\tGroups:\t"); | |
9156 | gi = cred->group_info; | |
9157 | for (g = 0; g < gi->ngroups; g++) { | |
9158 | seq_put_decimal_ull(m, g ? " " : "", | |
9159 | from_kgid_munged(uns, gi->gid[g])); | |
9160 | } | |
9161 | seq_puts(m, "\n\tCapEff:\t"); | |
9162 | cap = cred->cap_effective; | |
9163 | CAP_FOR_EACH_U32(__capi) | |
9164 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9165 | seq_putc(m, '\n'); | |
9166 | return 0; | |
9167 | } | |
9168 | ||
9169 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9170 | { | |
dbbe9c64 | 9171 | struct io_sq_data *sq = NULL; |
fad8e0de | 9172 | bool has_lock; |
87ce955b JA |
9173 | int i; |
9174 | ||
fad8e0de JA |
9175 | /* |
9176 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9177 | * since fdinfo case grabs it in the opposite direction of normal use | |
9178 | * cases. If we fail to get the lock, we just don't iterate any | |
9179 | * structures that could be going away outside the io_uring mutex. | |
9180 | */ | |
9181 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9182 | ||
5f3f26f9 | 9183 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9184 | sq = ctx->sq_data; |
5f3f26f9 JA |
9185 | if (!sq->thread) |
9186 | sq = NULL; | |
9187 | } | |
dbbe9c64 JQ |
9188 | |
9189 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9190 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9191 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9192 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9193 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9194 | |
87ce955b JA |
9195 | if (f) |
9196 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9197 | else | |
9198 | seq_printf(m, "%5u: <none>\n", i); | |
9199 | } | |
9200 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9201 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9202 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9203 | ||
9204 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9205 | (unsigned int) buf->len); | |
9206 | } | |
fad8e0de | 9207 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9208 | seq_printf(m, "Personalities:\n"); |
9209 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9210 | } | |
d7718a9d JA |
9211 | seq_printf(m, "PollList:\n"); |
9212 | spin_lock_irq(&ctx->completion_lock); | |
9213 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9214 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9215 | struct io_kiocb *req; | |
9216 | ||
9217 | hlist_for_each_entry(req, list, hash_node) | |
9218 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9219 | req->task->task_works != NULL); | |
9220 | } | |
9221 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9222 | if (has_lock) |
9223 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9224 | } |
9225 | ||
9226 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9227 | { | |
9228 | struct io_ring_ctx *ctx = f->private_data; | |
9229 | ||
9230 | if (percpu_ref_tryget(&ctx->refs)) { | |
9231 | __io_uring_show_fdinfo(ctx, m); | |
9232 | percpu_ref_put(&ctx->refs); | |
9233 | } | |
9234 | } | |
bebdb65e | 9235 | #endif |
87ce955b | 9236 | |
2b188cc1 JA |
9237 | static const struct file_operations io_uring_fops = { |
9238 | .release = io_uring_release, | |
9239 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9240 | #ifndef CONFIG_MMU |
9241 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9242 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9243 | #endif | |
2b188cc1 JA |
9244 | .poll = io_uring_poll, |
9245 | .fasync = io_uring_fasync, | |
bebdb65e | 9246 | #ifdef CONFIG_PROC_FS |
87ce955b | 9247 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9248 | #endif |
2b188cc1 JA |
9249 | }; |
9250 | ||
9251 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9252 | struct io_uring_params *p) | |
9253 | { | |
75b28aff HV |
9254 | struct io_rings *rings; |
9255 | size_t size, sq_array_offset; | |
2b188cc1 | 9256 | |
bd740481 JA |
9257 | /* make sure these are sane, as we already accounted them */ |
9258 | ctx->sq_entries = p->sq_entries; | |
9259 | ctx->cq_entries = p->cq_entries; | |
9260 | ||
75b28aff HV |
9261 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9262 | if (size == SIZE_MAX) | |
9263 | return -EOVERFLOW; | |
9264 | ||
9265 | rings = io_mem_alloc(size); | |
9266 | if (!rings) | |
2b188cc1 JA |
9267 | return -ENOMEM; |
9268 | ||
75b28aff HV |
9269 | ctx->rings = rings; |
9270 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9271 | rings->sq_ring_mask = p->sq_entries - 1; | |
9272 | rings->cq_ring_mask = p->cq_entries - 1; | |
9273 | rings->sq_ring_entries = p->sq_entries; | |
9274 | rings->cq_ring_entries = p->cq_entries; | |
9275 | ctx->sq_mask = rings->sq_ring_mask; | |
9276 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9277 | |
9278 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9279 | if (size == SIZE_MAX) { |
9280 | io_mem_free(ctx->rings); | |
9281 | ctx->rings = NULL; | |
2b188cc1 | 9282 | return -EOVERFLOW; |
eb065d30 | 9283 | } |
2b188cc1 JA |
9284 | |
9285 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9286 | if (!ctx->sq_sqes) { |
9287 | io_mem_free(ctx->rings); | |
9288 | ctx->rings = NULL; | |
2b188cc1 | 9289 | return -ENOMEM; |
eb065d30 | 9290 | } |
2b188cc1 | 9291 | |
2b188cc1 JA |
9292 | return 0; |
9293 | } | |
9294 | ||
9faadcc8 PB |
9295 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9296 | { | |
9297 | int ret, fd; | |
9298 | ||
9299 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9300 | if (fd < 0) | |
9301 | return fd; | |
9302 | ||
9303 | ret = io_uring_add_task_file(ctx, file); | |
9304 | if (ret) { | |
9305 | put_unused_fd(fd); | |
9306 | return ret; | |
9307 | } | |
9308 | fd_install(fd, file); | |
9309 | return fd; | |
9310 | } | |
9311 | ||
2b188cc1 JA |
9312 | /* |
9313 | * Allocate an anonymous fd, this is what constitutes the application | |
9314 | * visible backing of an io_uring instance. The application mmaps this | |
9315 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9316 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9317 | */ | |
9faadcc8 | 9318 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9319 | { |
9320 | struct file *file; | |
9faadcc8 | 9321 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9322 | int ret; |
9323 | ||
2b188cc1 JA |
9324 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9325 | &ctx->ring_sock); | |
9326 | if (ret) | |
9faadcc8 | 9327 | return ERR_PTR(ret); |
2b188cc1 JA |
9328 | #endif |
9329 | ||
2b188cc1 JA |
9330 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9331 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9332 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9333 | if (IS_ERR(file)) { |
9334 | sock_release(ctx->ring_sock); | |
9335 | ctx->ring_sock = NULL; | |
9336 | } else { | |
9337 | ctx->ring_sock->file = file; | |
0f212204 | 9338 | } |
2b188cc1 | 9339 | #endif |
9faadcc8 | 9340 | return file; |
2b188cc1 JA |
9341 | } |
9342 | ||
7f13657d XW |
9343 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9344 | struct io_uring_params __user *params) | |
2b188cc1 | 9345 | { |
2b188cc1 | 9346 | struct io_ring_ctx *ctx; |
9faadcc8 | 9347 | struct file *file; |
2b188cc1 JA |
9348 | int ret; |
9349 | ||
8110c1a6 | 9350 | if (!entries) |
2b188cc1 | 9351 | return -EINVAL; |
8110c1a6 JA |
9352 | if (entries > IORING_MAX_ENTRIES) { |
9353 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9354 | return -EINVAL; | |
9355 | entries = IORING_MAX_ENTRIES; | |
9356 | } | |
2b188cc1 JA |
9357 | |
9358 | /* | |
9359 | * Use twice as many entries for the CQ ring. It's possible for the | |
9360 | * application to drive a higher depth than the size of the SQ ring, | |
9361 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9362 | * some flexibility in overcommitting a bit. If the application has |
9363 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9364 | * of CQ ring entries manually. | |
2b188cc1 JA |
9365 | */ |
9366 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9367 | if (p->flags & IORING_SETUP_CQSIZE) { |
9368 | /* | |
9369 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9370 | * to a power-of-two, if it isn't already. We do NOT impose | |
9371 | * any cq vs sq ring sizing. | |
9372 | */ | |
eb2667b3 | 9373 | if (!p->cq_entries) |
33a107f0 | 9374 | return -EINVAL; |
8110c1a6 JA |
9375 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9376 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9377 | return -EINVAL; | |
9378 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9379 | } | |
eb2667b3 JQ |
9380 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9381 | if (p->cq_entries < p->sq_entries) | |
9382 | return -EINVAL; | |
33a107f0 JA |
9383 | } else { |
9384 | p->cq_entries = 2 * p->sq_entries; | |
9385 | } | |
2b188cc1 | 9386 | |
2b188cc1 | 9387 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9388 | if (!ctx) |
2b188cc1 | 9389 | return -ENOMEM; |
2b188cc1 | 9390 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9391 | if (!capable(CAP_IPC_LOCK)) |
9392 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9393 | |
9394 | /* | |
9395 | * This is just grabbed for accounting purposes. When a process exits, | |
9396 | * the mm is exited and dropped before the files, hence we need to hang | |
9397 | * on to this mm purely for the purposes of being able to unaccount | |
9398 | * memory (locked/pinned vm). It's not used for anything else. | |
9399 | */ | |
6b7898eb | 9400 | mmgrab(current->mm); |
2aede0e4 | 9401 | ctx->mm_account = current->mm; |
6b7898eb | 9402 | |
2b188cc1 JA |
9403 | ret = io_allocate_scq_urings(ctx, p); |
9404 | if (ret) | |
9405 | goto err; | |
9406 | ||
7e84e1c7 | 9407 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9408 | if (ret) |
9409 | goto err; | |
9410 | ||
7e84e1c7 SG |
9411 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9412 | io_sq_offload_start(ctx); | |
9413 | ||
2b188cc1 | 9414 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9415 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9416 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9417 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9418 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9419 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9420 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9421 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9422 | |
9423 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9424 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9425 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9426 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9427 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9428 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9429 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9430 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9431 | |
7f13657d XW |
9432 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9433 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9434 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9435 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9436 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9437 | |
9438 | if (copy_to_user(params, p, sizeof(*p))) { | |
9439 | ret = -EFAULT; | |
9440 | goto err; | |
9441 | } | |
d1719f70 | 9442 | |
9faadcc8 PB |
9443 | file = io_uring_get_file(ctx); |
9444 | if (IS_ERR(file)) { | |
9445 | ret = PTR_ERR(file); | |
9446 | goto err; | |
9447 | } | |
9448 | ||
044c1ab3 JA |
9449 | /* |
9450 | * Install ring fd as the very last thing, so we don't risk someone | |
9451 | * having closed it before we finish setup | |
9452 | */ | |
9faadcc8 PB |
9453 | ret = io_uring_install_fd(ctx, file); |
9454 | if (ret < 0) { | |
9455 | /* fput will clean it up */ | |
9456 | fput(file); | |
9457 | return ret; | |
9458 | } | |
044c1ab3 | 9459 | |
c826bd7a | 9460 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9461 | return ret; |
9462 | err: | |
9463 | io_ring_ctx_wait_and_kill(ctx); | |
9464 | return ret; | |
9465 | } | |
9466 | ||
9467 | /* | |
9468 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9469 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9470 | * params structure passed in. | |
9471 | */ | |
9472 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9473 | { | |
9474 | struct io_uring_params p; | |
2b188cc1 JA |
9475 | int i; |
9476 | ||
9477 | if (copy_from_user(&p, params, sizeof(p))) | |
9478 | return -EFAULT; | |
9479 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9480 | if (p.resv[i]) | |
9481 | return -EINVAL; | |
9482 | } | |
9483 | ||
6c271ce2 | 9484 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9485 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9486 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9487 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9488 | return -EINVAL; |
9489 | ||
7f13657d | 9490 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9491 | } |
9492 | ||
9493 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9494 | struct io_uring_params __user *, params) | |
9495 | { | |
9496 | return io_uring_setup(entries, params); | |
9497 | } | |
9498 | ||
66f4af93 JA |
9499 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9500 | { | |
9501 | struct io_uring_probe *p; | |
9502 | size_t size; | |
9503 | int i, ret; | |
9504 | ||
9505 | size = struct_size(p, ops, nr_args); | |
9506 | if (size == SIZE_MAX) | |
9507 | return -EOVERFLOW; | |
9508 | p = kzalloc(size, GFP_KERNEL); | |
9509 | if (!p) | |
9510 | return -ENOMEM; | |
9511 | ||
9512 | ret = -EFAULT; | |
9513 | if (copy_from_user(p, arg, size)) | |
9514 | goto out; | |
9515 | ret = -EINVAL; | |
9516 | if (memchr_inv(p, 0, size)) | |
9517 | goto out; | |
9518 | ||
9519 | p->last_op = IORING_OP_LAST - 1; | |
9520 | if (nr_args > IORING_OP_LAST) | |
9521 | nr_args = IORING_OP_LAST; | |
9522 | ||
9523 | for (i = 0; i < nr_args; i++) { | |
9524 | p->ops[i].op = i; | |
9525 | if (!io_op_defs[i].not_supported) | |
9526 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9527 | } | |
9528 | p->ops_len = i; | |
9529 | ||
9530 | ret = 0; | |
9531 | if (copy_to_user(arg, p, size)) | |
9532 | ret = -EFAULT; | |
9533 | out: | |
9534 | kfree(p); | |
9535 | return ret; | |
9536 | } | |
9537 | ||
071698e1 JA |
9538 | static int io_register_personality(struct io_ring_ctx *ctx) |
9539 | { | |
4379bf8b | 9540 | const struct cred *creds; |
1e6fa521 | 9541 | int ret; |
071698e1 | 9542 | |
4379bf8b | 9543 | creds = get_current_cred(); |
1e6fa521 | 9544 | |
4379bf8b JA |
9545 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
9546 | USHRT_MAX, GFP_KERNEL); | |
9547 | if (ret < 0) | |
9548 | put_cred(creds); | |
1e6fa521 | 9549 | return ret; |
071698e1 JA |
9550 | } |
9551 | ||
21b55dbc SG |
9552 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9553 | unsigned int nr_args) | |
9554 | { | |
9555 | struct io_uring_restriction *res; | |
9556 | size_t size; | |
9557 | int i, ret; | |
9558 | ||
7e84e1c7 SG |
9559 | /* Restrictions allowed only if rings started disabled */ |
9560 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9561 | return -EBADFD; | |
9562 | ||
21b55dbc | 9563 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9564 | if (ctx->restrictions.registered) |
21b55dbc SG |
9565 | return -EBUSY; |
9566 | ||
9567 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9568 | return -EINVAL; | |
9569 | ||
9570 | size = array_size(nr_args, sizeof(*res)); | |
9571 | if (size == SIZE_MAX) | |
9572 | return -EOVERFLOW; | |
9573 | ||
9574 | res = memdup_user(arg, size); | |
9575 | if (IS_ERR(res)) | |
9576 | return PTR_ERR(res); | |
9577 | ||
9578 | ret = 0; | |
9579 | ||
9580 | for (i = 0; i < nr_args; i++) { | |
9581 | switch (res[i].opcode) { | |
9582 | case IORING_RESTRICTION_REGISTER_OP: | |
9583 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9584 | ret = -EINVAL; | |
9585 | goto out; | |
9586 | } | |
9587 | ||
9588 | __set_bit(res[i].register_op, | |
9589 | ctx->restrictions.register_op); | |
9590 | break; | |
9591 | case IORING_RESTRICTION_SQE_OP: | |
9592 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9593 | ret = -EINVAL; | |
9594 | goto out; | |
9595 | } | |
9596 | ||
9597 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9598 | break; | |
9599 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9600 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9601 | break; | |
9602 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9603 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9604 | break; | |
9605 | default: | |
9606 | ret = -EINVAL; | |
9607 | goto out; | |
9608 | } | |
9609 | } | |
9610 | ||
9611 | out: | |
9612 | /* Reset all restrictions if an error happened */ | |
9613 | if (ret != 0) | |
9614 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9615 | else | |
7e84e1c7 | 9616 | ctx->restrictions.registered = true; |
21b55dbc SG |
9617 | |
9618 | kfree(res); | |
9619 | return ret; | |
9620 | } | |
9621 | ||
7e84e1c7 SG |
9622 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9623 | { | |
9624 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9625 | return -EBADFD; | |
9626 | ||
9627 | if (ctx->restrictions.registered) | |
9628 | ctx->restricted = 1; | |
9629 | ||
7e84e1c7 | 9630 | io_sq_offload_start(ctx); |
7e84e1c7 SG |
9631 | return 0; |
9632 | } | |
9633 | ||
071698e1 JA |
9634 | static bool io_register_op_must_quiesce(int op) |
9635 | { | |
9636 | switch (op) { | |
9637 | case IORING_UNREGISTER_FILES: | |
9638 | case IORING_REGISTER_FILES_UPDATE: | |
9639 | case IORING_REGISTER_PROBE: | |
9640 | case IORING_REGISTER_PERSONALITY: | |
9641 | case IORING_UNREGISTER_PERSONALITY: | |
9642 | return false; | |
9643 | default: | |
9644 | return true; | |
9645 | } | |
9646 | } | |
9647 | ||
edafccee JA |
9648 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9649 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9650 | __releases(ctx->uring_lock) |
9651 | __acquires(ctx->uring_lock) | |
edafccee JA |
9652 | { |
9653 | int ret; | |
9654 | ||
35fa71a0 JA |
9655 | /* |
9656 | * We're inside the ring mutex, if the ref is already dying, then | |
9657 | * someone else killed the ctx or is already going through | |
9658 | * io_uring_register(). | |
9659 | */ | |
9660 | if (percpu_ref_is_dying(&ctx->refs)) | |
9661 | return -ENXIO; | |
9662 | ||
071698e1 | 9663 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9664 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9665 | |
05f3fb3c JA |
9666 | /* |
9667 | * Drop uring mutex before waiting for references to exit. If | |
9668 | * another thread is currently inside io_uring_enter() it might | |
9669 | * need to grab the uring_lock to make progress. If we hold it | |
9670 | * here across the drain wait, then we can deadlock. It's safe | |
9671 | * to drop the mutex here, since no new references will come in | |
9672 | * after we've killed the percpu ref. | |
9673 | */ | |
9674 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9675 | do { |
9676 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9677 | if (!ret) | |
9678 | break; | |
ed6930c9 JA |
9679 | ret = io_run_task_work_sig(); |
9680 | if (ret < 0) | |
9681 | break; | |
af9c1a44 JA |
9682 | } while (1); |
9683 | ||
05f3fb3c | 9684 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9685 | |
c150368b JA |
9686 | if (ret) { |
9687 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
9688 | goto out_quiesce; |
9689 | } | |
9690 | } | |
9691 | ||
9692 | if (ctx->restricted) { | |
9693 | if (opcode >= IORING_REGISTER_LAST) { | |
9694 | ret = -EINVAL; | |
9695 | goto out; | |
9696 | } | |
9697 | ||
9698 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9699 | ret = -EACCES; | |
c150368b JA |
9700 | goto out; |
9701 | } | |
05f3fb3c | 9702 | } |
edafccee JA |
9703 | |
9704 | switch (opcode) { | |
9705 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9706 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9707 | break; |
9708 | case IORING_UNREGISTER_BUFFERS: | |
9709 | ret = -EINVAL; | |
9710 | if (arg || nr_args) | |
9711 | break; | |
0a96bbe4 | 9712 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9713 | break; |
6b06314c JA |
9714 | case IORING_REGISTER_FILES: |
9715 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9716 | break; | |
9717 | case IORING_UNREGISTER_FILES: | |
9718 | ret = -EINVAL; | |
9719 | if (arg || nr_args) | |
9720 | break; | |
9721 | ret = io_sqe_files_unregister(ctx); | |
9722 | break; | |
c3a31e60 JA |
9723 | case IORING_REGISTER_FILES_UPDATE: |
9724 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9725 | break; | |
9b402849 | 9726 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9727 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9728 | ret = -EINVAL; |
9729 | if (nr_args != 1) | |
9730 | break; | |
9731 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9732 | if (ret) |
9733 | break; | |
9734 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9735 | ctx->eventfd_async = 1; | |
9736 | else | |
9737 | ctx->eventfd_async = 0; | |
9b402849 JA |
9738 | break; |
9739 | case IORING_UNREGISTER_EVENTFD: | |
9740 | ret = -EINVAL; | |
9741 | if (arg || nr_args) | |
9742 | break; | |
9743 | ret = io_eventfd_unregister(ctx); | |
9744 | break; | |
66f4af93 JA |
9745 | case IORING_REGISTER_PROBE: |
9746 | ret = -EINVAL; | |
9747 | if (!arg || nr_args > 256) | |
9748 | break; | |
9749 | ret = io_probe(ctx, arg, nr_args); | |
9750 | break; | |
071698e1 JA |
9751 | case IORING_REGISTER_PERSONALITY: |
9752 | ret = -EINVAL; | |
9753 | if (arg || nr_args) | |
9754 | break; | |
9755 | ret = io_register_personality(ctx); | |
9756 | break; | |
9757 | case IORING_UNREGISTER_PERSONALITY: | |
9758 | ret = -EINVAL; | |
9759 | if (arg) | |
9760 | break; | |
9761 | ret = io_unregister_personality(ctx, nr_args); | |
9762 | break; | |
7e84e1c7 SG |
9763 | case IORING_REGISTER_ENABLE_RINGS: |
9764 | ret = -EINVAL; | |
9765 | if (arg || nr_args) | |
9766 | break; | |
9767 | ret = io_register_enable_rings(ctx); | |
9768 | break; | |
21b55dbc SG |
9769 | case IORING_REGISTER_RESTRICTIONS: |
9770 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9771 | break; | |
edafccee JA |
9772 | default: |
9773 | ret = -EINVAL; | |
9774 | break; | |
9775 | } | |
9776 | ||
21b55dbc | 9777 | out: |
071698e1 | 9778 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9779 | /* bring the ctx back to life */ |
05f3fb3c | 9780 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 9781 | out_quiesce: |
0f158b4c | 9782 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9783 | } |
edafccee JA |
9784 | return ret; |
9785 | } | |
9786 | ||
9787 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9788 | void __user *, arg, unsigned int, nr_args) | |
9789 | { | |
9790 | struct io_ring_ctx *ctx; | |
9791 | long ret = -EBADF; | |
9792 | struct fd f; | |
9793 | ||
9794 | f = fdget(fd); | |
9795 | if (!f.file) | |
9796 | return -EBADF; | |
9797 | ||
9798 | ret = -EOPNOTSUPP; | |
9799 | if (f.file->f_op != &io_uring_fops) | |
9800 | goto out_fput; | |
9801 | ||
9802 | ctx = f.file->private_data; | |
9803 | ||
b6c23dd5 PB |
9804 | io_run_task_work(); |
9805 | ||
edafccee JA |
9806 | mutex_lock(&ctx->uring_lock); |
9807 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9808 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9809 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9810 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9811 | out_fput: |
9812 | fdput(f); | |
9813 | return ret; | |
9814 | } | |
9815 | ||
2b188cc1 JA |
9816 | static int __init io_uring_init(void) |
9817 | { | |
d7f62e82 SM |
9818 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9819 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9820 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9821 | } while (0) | |
9822 | ||
9823 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9824 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9825 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9826 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9827 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9828 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9829 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9830 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9831 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9832 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9833 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9834 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9835 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9836 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9837 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9838 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9839 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9840 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9841 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9842 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9843 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9844 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9845 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9846 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9847 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9848 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9849 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9850 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9851 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9852 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9853 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9854 | |
d3656344 | 9855 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9856 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
9857 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
9858 | SLAB_ACCOUNT); | |
2b188cc1 JA |
9859 | return 0; |
9860 | }; | |
9861 | __initcall(io_uring_init); |