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; |
d9d05217 | 341 | unsigned int sqo_dead: 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 | 382 | /* |
37d1e2e3 | 383 | * For SQPOLL usage |
2aede0e4 JA |
384 | */ |
385 | struct task_struct *sqo_task; | |
386 | ||
387 | /* Only used for accounting purposes */ | |
388 | struct mm_struct *mm_account; | |
389 | ||
534ca6d6 JA |
390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
391 | ||
90554200 | 392 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 393 | struct list_head sqd_list; |
75b28aff | 394 | |
6b06314c JA |
395 | /* |
396 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
397 | * readers must ensure that ->refs is alive as long as the file* is | |
398 | * used. Only updated through io_uring_register(2). | |
399 | */ | |
269bbe5f | 400 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
401 | unsigned nr_user_files; |
402 | ||
edafccee JA |
403 | /* if used, fixed mapped user buffers */ |
404 | unsigned nr_user_bufs; | |
405 | struct io_mapped_ubuf *user_bufs; | |
406 | ||
2b188cc1 JA |
407 | struct user_struct *user; |
408 | ||
0f158b4c JA |
409 | struct completion ref_comp; |
410 | struct completion sq_thread_comp; | |
206aefde JA |
411 | |
412 | #if defined(CONFIG_UNIX) | |
413 | struct socket *ring_sock; | |
414 | #endif | |
415 | ||
5a2e745d JA |
416 | struct idr io_buffer_idr; |
417 | ||
071698e1 JA |
418 | struct idr personality_idr; |
419 | ||
206aefde JA |
420 | struct { |
421 | unsigned cached_cq_tail; | |
422 | unsigned cq_entries; | |
423 | unsigned cq_mask; | |
424 | atomic_t cq_timeouts; | |
f010505b | 425 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 426 | unsigned long cq_check_overflow; |
206aefde JA |
427 | struct wait_queue_head cq_wait; |
428 | struct fasync_struct *cq_fasync; | |
429 | struct eventfd_ctx *cq_ev_fd; | |
430 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 431 | |
2b188cc1 JA |
432 | struct { |
433 | spinlock_t completion_lock; | |
e94f141b | 434 | |
def596e9 | 435 | /* |
540e32a0 | 436 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
437 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
438 | * For SQPOLL, only the single threaded io_sq_thread() will | |
439 | * manipulate the list, hence no extra locking is needed there. | |
440 | */ | |
540e32a0 | 441 | struct list_head iopoll_list; |
78076bb6 JA |
442 | struct hlist_head *cancel_hash; |
443 | unsigned cancel_hash_bits; | |
e94f141b | 444 | bool poll_multi_file; |
31b51510 | 445 | |
fcb323cc JA |
446 | spinlock_t inflight_lock; |
447 | struct list_head inflight_list; | |
2b188cc1 | 448 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 449 | |
269bbe5f BM |
450 | struct delayed_work rsrc_put_work; |
451 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
452 | struct list_head rsrc_ref_list; |
453 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 454 | |
21b55dbc | 455 | struct io_restriction restrictions; |
3c1a2ead | 456 | |
7c25c0d1 JA |
457 | /* exit task_work */ |
458 | struct callback_head *exit_task_work; | |
459 | ||
e941894e JA |
460 | struct wait_queue_head hash_wait; |
461 | ||
3c1a2ead JA |
462 | /* Keep this last, we don't need it for the fast path */ |
463 | struct work_struct exit_work; | |
2b188cc1 JA |
464 | }; |
465 | ||
09bb8394 JA |
466 | /* |
467 | * First field must be the file pointer in all the | |
468 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
469 | */ | |
221c5eb2 JA |
470 | struct io_poll_iocb { |
471 | struct file *file; | |
018043be | 472 | struct wait_queue_head *head; |
221c5eb2 | 473 | __poll_t events; |
8c838788 | 474 | bool done; |
221c5eb2 | 475 | bool canceled; |
392edb45 | 476 | struct wait_queue_entry wait; |
221c5eb2 JA |
477 | }; |
478 | ||
018043be PB |
479 | struct io_poll_remove { |
480 | struct file *file; | |
481 | u64 addr; | |
482 | }; | |
483 | ||
b5dba59e JA |
484 | struct io_close { |
485 | struct file *file; | |
b5dba59e JA |
486 | int fd; |
487 | }; | |
488 | ||
ad8a48ac JA |
489 | struct io_timeout_data { |
490 | struct io_kiocb *req; | |
491 | struct hrtimer timer; | |
492 | struct timespec64 ts; | |
493 | enum hrtimer_mode mode; | |
494 | }; | |
495 | ||
8ed8d3c3 JA |
496 | struct io_accept { |
497 | struct file *file; | |
498 | struct sockaddr __user *addr; | |
499 | int __user *addr_len; | |
500 | int flags; | |
09952e3e | 501 | unsigned long nofile; |
8ed8d3c3 JA |
502 | }; |
503 | ||
504 | struct io_sync { | |
505 | struct file *file; | |
506 | loff_t len; | |
507 | loff_t off; | |
508 | int flags; | |
d63d1b5e | 509 | int mode; |
8ed8d3c3 JA |
510 | }; |
511 | ||
fbf23849 JA |
512 | struct io_cancel { |
513 | struct file *file; | |
514 | u64 addr; | |
515 | }; | |
516 | ||
b29472ee JA |
517 | struct io_timeout { |
518 | struct file *file; | |
bfe68a22 PB |
519 | u32 off; |
520 | u32 target_seq; | |
135fcde8 | 521 | struct list_head list; |
90cd7e42 PB |
522 | /* head of the link, used by linked timeouts only */ |
523 | struct io_kiocb *head; | |
b29472ee JA |
524 | }; |
525 | ||
0bdf7a2d PB |
526 | struct io_timeout_rem { |
527 | struct file *file; | |
528 | u64 addr; | |
9c8e11b3 PB |
529 | |
530 | /* timeout update */ | |
531 | struct timespec64 ts; | |
532 | u32 flags; | |
0bdf7a2d PB |
533 | }; |
534 | ||
9adbd45d JA |
535 | struct io_rw { |
536 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
537 | struct kiocb kiocb; | |
538 | u64 addr; | |
539 | u64 len; | |
540 | }; | |
541 | ||
3fbb51c1 JA |
542 | struct io_connect { |
543 | struct file *file; | |
544 | struct sockaddr __user *addr; | |
545 | int addr_len; | |
546 | }; | |
547 | ||
e47293fd JA |
548 | struct io_sr_msg { |
549 | struct file *file; | |
fddaface | 550 | union { |
270a5940 | 551 | struct user_msghdr __user *umsg; |
fddaface JA |
552 | void __user *buf; |
553 | }; | |
e47293fd | 554 | int msg_flags; |
bcda7baa | 555 | int bgid; |
fddaface | 556 | size_t len; |
bcda7baa | 557 | struct io_buffer *kbuf; |
e47293fd JA |
558 | }; |
559 | ||
15b71abe JA |
560 | struct io_open { |
561 | struct file *file; | |
562 | int dfd; | |
15b71abe | 563 | struct filename *filename; |
c12cedf2 | 564 | struct open_how how; |
4022e7af | 565 | unsigned long nofile; |
15b71abe JA |
566 | }; |
567 | ||
269bbe5f | 568 | struct io_rsrc_update { |
05f3fb3c JA |
569 | struct file *file; |
570 | u64 arg; | |
571 | u32 nr_args; | |
572 | u32 offset; | |
573 | }; | |
574 | ||
4840e418 JA |
575 | struct io_fadvise { |
576 | struct file *file; | |
577 | u64 offset; | |
578 | u32 len; | |
579 | u32 advice; | |
580 | }; | |
581 | ||
c1ca757b JA |
582 | struct io_madvise { |
583 | struct file *file; | |
584 | u64 addr; | |
585 | u32 len; | |
586 | u32 advice; | |
587 | }; | |
588 | ||
3e4827b0 JA |
589 | struct io_epoll { |
590 | struct file *file; | |
591 | int epfd; | |
592 | int op; | |
593 | int fd; | |
594 | struct epoll_event event; | |
e47293fd JA |
595 | }; |
596 | ||
7d67af2c PB |
597 | struct io_splice { |
598 | struct file *file_out; | |
599 | struct file *file_in; | |
600 | loff_t off_out; | |
601 | loff_t off_in; | |
602 | u64 len; | |
603 | unsigned int flags; | |
604 | }; | |
605 | ||
ddf0322d JA |
606 | struct io_provide_buf { |
607 | struct file *file; | |
608 | __u64 addr; | |
609 | __s32 len; | |
610 | __u32 bgid; | |
611 | __u16 nbufs; | |
612 | __u16 bid; | |
613 | }; | |
614 | ||
1d9e1288 BM |
615 | struct io_statx { |
616 | struct file *file; | |
617 | int dfd; | |
618 | unsigned int mask; | |
619 | unsigned int flags; | |
e62753e4 | 620 | const char __user *filename; |
1d9e1288 BM |
621 | struct statx __user *buffer; |
622 | }; | |
623 | ||
36f4fa68 JA |
624 | struct io_shutdown { |
625 | struct file *file; | |
626 | int how; | |
627 | }; | |
628 | ||
80a261fd JA |
629 | struct io_rename { |
630 | struct file *file; | |
631 | int old_dfd; | |
632 | int new_dfd; | |
633 | struct filename *oldpath; | |
634 | struct filename *newpath; | |
635 | int flags; | |
636 | }; | |
637 | ||
14a1143b JA |
638 | struct io_unlink { |
639 | struct file *file; | |
640 | int dfd; | |
641 | int flags; | |
642 | struct filename *filename; | |
643 | }; | |
644 | ||
3ca405eb PB |
645 | struct io_completion { |
646 | struct file *file; | |
647 | struct list_head list; | |
0f7e466b | 648 | int cflags; |
3ca405eb PB |
649 | }; |
650 | ||
f499a021 JA |
651 | struct io_async_connect { |
652 | struct sockaddr_storage address; | |
653 | }; | |
654 | ||
03b1230c JA |
655 | struct io_async_msghdr { |
656 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
657 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
658 | struct iovec *free_iov; | |
03b1230c JA |
659 | struct sockaddr __user *uaddr; |
660 | struct msghdr msg; | |
b537916c | 661 | struct sockaddr_storage addr; |
03b1230c JA |
662 | }; |
663 | ||
f67676d1 JA |
664 | struct io_async_rw { |
665 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
666 | const struct iovec *free_iovec; |
667 | struct iov_iter iter; | |
227c0c96 | 668 | size_t bytes_done; |
bcf5a063 | 669 | struct wait_page_queue wpq; |
f67676d1 JA |
670 | }; |
671 | ||
6b47ee6e PB |
672 | enum { |
673 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
674 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
675 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
676 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
677 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 678 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 679 | |
6b47ee6e PB |
680 | REQ_F_FAIL_LINK_BIT, |
681 | REQ_F_INFLIGHT_BIT, | |
682 | REQ_F_CUR_POS_BIT, | |
683 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 684 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 685 | REQ_F_ISREG_BIT, |
99bc4c38 | 686 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 687 | REQ_F_POLLED_BIT, |
bcda7baa | 688 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 689 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 690 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 691 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 692 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
693 | |
694 | /* not a real bit, just to check we're not overflowing the space */ | |
695 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
696 | }; |
697 | ||
698 | enum { | |
699 | /* ctx owns file */ | |
700 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
701 | /* drain existing IO first */ | |
702 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
703 | /* linked sqes */ | |
704 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
705 | /* doesn't sever on completion < 0 */ | |
706 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
707 | /* IOSQE_ASYNC */ | |
708 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
709 | /* IOSQE_BUFFER_SELECT */ |
710 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 711 | |
6b47ee6e PB |
712 | /* fail rest of links */ |
713 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
714 | /* on inflight list */ | |
715 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
716 | /* read/write uses file position */ | |
717 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
718 | /* must not punt to workers */ | |
719 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 720 | /* has or had linked timeout */ |
6b47ee6e | 721 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
722 | /* regular file */ |
723 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
724 | /* needs cleanup */ |
725 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
726 | /* already went through poll handler */ |
727 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
728 | /* buffer already selected */ |
729 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
730 | /* doesn't need file table for this request */ |
731 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
732 | /* io_wq_work is initialized */ |
733 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
734 | /* linked timeout is active, i.e. prepared by link's head */ |
735 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
736 | /* completion is deferred through io_comp_state */ |
737 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
738 | }; |
739 | ||
740 | struct async_poll { | |
741 | struct io_poll_iocb poll; | |
807abcb0 | 742 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
743 | }; |
744 | ||
7cbf1722 JA |
745 | struct io_task_work { |
746 | struct io_wq_work_node node; | |
747 | task_work_func_t func; | |
748 | }; | |
749 | ||
09bb8394 JA |
750 | /* |
751 | * NOTE! Each of the iocb union members has the file pointer | |
752 | * as the first entry in their struct definition. So you can | |
753 | * access the file pointer through any of the sub-structs, | |
754 | * or directly as just 'ki_filp' in this struct. | |
755 | */ | |
2b188cc1 | 756 | struct io_kiocb { |
221c5eb2 | 757 | union { |
09bb8394 | 758 | struct file *file; |
9adbd45d | 759 | struct io_rw rw; |
221c5eb2 | 760 | struct io_poll_iocb poll; |
018043be | 761 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
762 | struct io_accept accept; |
763 | struct io_sync sync; | |
fbf23849 | 764 | struct io_cancel cancel; |
b29472ee | 765 | struct io_timeout timeout; |
0bdf7a2d | 766 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 767 | struct io_connect connect; |
e47293fd | 768 | struct io_sr_msg sr_msg; |
15b71abe | 769 | struct io_open open; |
b5dba59e | 770 | struct io_close close; |
269bbe5f | 771 | struct io_rsrc_update rsrc_update; |
4840e418 | 772 | struct io_fadvise fadvise; |
c1ca757b | 773 | struct io_madvise madvise; |
3e4827b0 | 774 | struct io_epoll epoll; |
7d67af2c | 775 | struct io_splice splice; |
ddf0322d | 776 | struct io_provide_buf pbuf; |
1d9e1288 | 777 | struct io_statx statx; |
36f4fa68 | 778 | struct io_shutdown shutdown; |
80a261fd | 779 | struct io_rename rename; |
14a1143b | 780 | struct io_unlink unlink; |
3ca405eb PB |
781 | /* use only after cleaning per-op data, see io_clean_op() */ |
782 | struct io_completion compl; | |
221c5eb2 | 783 | }; |
2b188cc1 | 784 | |
e8c2bc1f JA |
785 | /* opcode allocated if it needs to store data for async defer */ |
786 | void *async_data; | |
d625c6ee | 787 | u8 opcode; |
65a6543d XW |
788 | /* polled IO has completed */ |
789 | u8 iopoll_completed; | |
2b188cc1 | 790 | |
4f4eeba8 | 791 | u16 buf_index; |
9cf7c104 | 792 | u32 result; |
4f4eeba8 | 793 | |
010e8e6b PB |
794 | struct io_ring_ctx *ctx; |
795 | unsigned int flags; | |
796 | refcount_t refs; | |
797 | struct task_struct *task; | |
798 | u64 user_data; | |
d7718a9d | 799 | |
f2f87370 | 800 | struct io_kiocb *link; |
269bbe5f | 801 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 802 | |
d21ffe7e PB |
803 | /* |
804 | * 1. used with ctx->iopoll_list with reads/writes | |
805 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
806 | */ | |
010e8e6b | 807 | struct list_head inflight_entry; |
7cbf1722 JA |
808 | union { |
809 | struct io_task_work io_task_work; | |
810 | struct callback_head task_work; | |
811 | }; | |
010e8e6b PB |
812 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
813 | struct hlist_node hash_node; | |
814 | struct async_poll *apoll; | |
815 | struct io_wq_work work; | |
2b188cc1 | 816 | }; |
05589553 | 817 | |
27dc8338 PB |
818 | struct io_defer_entry { |
819 | struct list_head list; | |
820 | struct io_kiocb *req; | |
9cf7c104 | 821 | u32 seq; |
2b188cc1 JA |
822 | }; |
823 | ||
d3656344 | 824 | struct io_op_def { |
d3656344 JA |
825 | /* needs req->file assigned */ |
826 | unsigned needs_file : 1; | |
d3656344 JA |
827 | /* hash wq insertion if file is a regular file */ |
828 | unsigned hash_reg_file : 1; | |
829 | /* unbound wq insertion if file is a non-regular file */ | |
830 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
831 | /* opcode is not supported by this kernel */ |
832 | unsigned not_supported : 1; | |
8a72758c JA |
833 | /* set if opcode supports polled "wait" */ |
834 | unsigned pollin : 1; | |
835 | unsigned pollout : 1; | |
bcda7baa JA |
836 | /* op supports buffer selection */ |
837 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
838 | /* must always have async data allocated */ |
839 | unsigned needs_async_data : 1; | |
27926b68 JA |
840 | /* should block plug */ |
841 | unsigned plug : 1; | |
e8c2bc1f JA |
842 | /* size of async data needed, if any */ |
843 | unsigned short async_size; | |
d3656344 JA |
844 | }; |
845 | ||
0918682b | 846 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
847 | [IORING_OP_NOP] = {}, |
848 | [IORING_OP_READV] = { | |
d3656344 JA |
849 | .needs_file = 1, |
850 | .unbound_nonreg_file = 1, | |
8a72758c | 851 | .pollin = 1, |
4d954c25 | 852 | .buffer_select = 1, |
e8c2bc1f | 853 | .needs_async_data = 1, |
27926b68 | 854 | .plug = 1, |
e8c2bc1f | 855 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 856 | }, |
0463b6c5 | 857 | [IORING_OP_WRITEV] = { |
d3656344 JA |
858 | .needs_file = 1, |
859 | .hash_reg_file = 1, | |
860 | .unbound_nonreg_file = 1, | |
8a72758c | 861 | .pollout = 1, |
e8c2bc1f | 862 | .needs_async_data = 1, |
27926b68 | 863 | .plug = 1, |
e8c2bc1f | 864 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 865 | }, |
0463b6c5 | 866 | [IORING_OP_FSYNC] = { |
d3656344 JA |
867 | .needs_file = 1, |
868 | }, | |
0463b6c5 | 869 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
870 | .needs_file = 1, |
871 | .unbound_nonreg_file = 1, | |
8a72758c | 872 | .pollin = 1, |
27926b68 | 873 | .plug = 1, |
e8c2bc1f | 874 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 875 | }, |
0463b6c5 | 876 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
877 | .needs_file = 1, |
878 | .hash_reg_file = 1, | |
879 | .unbound_nonreg_file = 1, | |
8a72758c | 880 | .pollout = 1, |
27926b68 | 881 | .plug = 1, |
e8c2bc1f | 882 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 883 | }, |
0463b6c5 | 884 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
885 | .needs_file = 1, |
886 | .unbound_nonreg_file = 1, | |
887 | }, | |
0463b6c5 PB |
888 | [IORING_OP_POLL_REMOVE] = {}, |
889 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
890 | .needs_file = 1, |
891 | }, | |
0463b6c5 | 892 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
893 | .needs_file = 1, |
894 | .unbound_nonreg_file = 1, | |
8a72758c | 895 | .pollout = 1, |
e8c2bc1f JA |
896 | .needs_async_data = 1, |
897 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 898 | }, |
0463b6c5 | 899 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
900 | .needs_file = 1, |
901 | .unbound_nonreg_file = 1, | |
8a72758c | 902 | .pollin = 1, |
52de1fe1 | 903 | .buffer_select = 1, |
e8c2bc1f JA |
904 | .needs_async_data = 1, |
905 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 906 | }, |
0463b6c5 | 907 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
908 | .needs_async_data = 1, |
909 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 910 | }, |
9c8e11b3 PB |
911 | [IORING_OP_TIMEOUT_REMOVE] = { |
912 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 913 | }, |
0463b6c5 | 914 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
915 | .needs_file = 1, |
916 | .unbound_nonreg_file = 1, | |
8a72758c | 917 | .pollin = 1, |
d3656344 | 918 | }, |
0463b6c5 PB |
919 | [IORING_OP_ASYNC_CANCEL] = {}, |
920 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
921 | .needs_async_data = 1, |
922 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 923 | }, |
0463b6c5 | 924 | [IORING_OP_CONNECT] = { |
d3656344 JA |
925 | .needs_file = 1, |
926 | .unbound_nonreg_file = 1, | |
8a72758c | 927 | .pollout = 1, |
e8c2bc1f JA |
928 | .needs_async_data = 1, |
929 | .async_size = sizeof(struct io_async_connect), | |
d3656344 | 930 | }, |
0463b6c5 | 931 | [IORING_OP_FALLOCATE] = { |
d3656344 | 932 | .needs_file = 1, |
d3656344 | 933 | }, |
44526bed JA |
934 | [IORING_OP_OPENAT] = {}, |
935 | [IORING_OP_CLOSE] = {}, | |
936 | [IORING_OP_FILES_UPDATE] = {}, | |
937 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 938 | [IORING_OP_READ] = { |
3a6820f2 JA |
939 | .needs_file = 1, |
940 | .unbound_nonreg_file = 1, | |
8a72758c | 941 | .pollin = 1, |
bcda7baa | 942 | .buffer_select = 1, |
27926b68 | 943 | .plug = 1, |
e8c2bc1f | 944 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 945 | }, |
0463b6c5 | 946 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
947 | .needs_file = 1, |
948 | .unbound_nonreg_file = 1, | |
8a72758c | 949 | .pollout = 1, |
27926b68 | 950 | .plug = 1, |
e8c2bc1f | 951 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 952 | }, |
0463b6c5 | 953 | [IORING_OP_FADVISE] = { |
4840e418 | 954 | .needs_file = 1, |
c1ca757b | 955 | }, |
44526bed | 956 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 957 | [IORING_OP_SEND] = { |
fddaface JA |
958 | .needs_file = 1, |
959 | .unbound_nonreg_file = 1, | |
8a72758c | 960 | .pollout = 1, |
fddaface | 961 | }, |
0463b6c5 | 962 | [IORING_OP_RECV] = { |
fddaface JA |
963 | .needs_file = 1, |
964 | .unbound_nonreg_file = 1, | |
8a72758c | 965 | .pollin = 1, |
bcda7baa | 966 | .buffer_select = 1, |
fddaface | 967 | }, |
0463b6c5 | 968 | [IORING_OP_OPENAT2] = { |
cebdb986 | 969 | }, |
3e4827b0 JA |
970 | [IORING_OP_EPOLL_CTL] = { |
971 | .unbound_nonreg_file = 1, | |
3e4827b0 | 972 | }, |
7d67af2c PB |
973 | [IORING_OP_SPLICE] = { |
974 | .needs_file = 1, | |
975 | .hash_reg_file = 1, | |
976 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
977 | }, |
978 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 979 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
980 | [IORING_OP_TEE] = { |
981 | .needs_file = 1, | |
982 | .hash_reg_file = 1, | |
983 | .unbound_nonreg_file = 1, | |
984 | }, | |
36f4fa68 JA |
985 | [IORING_OP_SHUTDOWN] = { |
986 | .needs_file = 1, | |
987 | }, | |
44526bed JA |
988 | [IORING_OP_RENAMEAT] = {}, |
989 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
990 | }; |
991 | ||
9936c7c2 PB |
992 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
993 | struct task_struct *task, | |
994 | struct files_struct *files); | |
37d1e2e3 | 995 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
269bbe5f | 996 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 997 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 998 | struct io_ring_ctx *ctx); |
f2303b1f | 999 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
1ffc5422 | 1000 | |
23faba36 | 1001 | static bool io_rw_reissue(struct io_kiocb *req); |
78e19bbe | 1002 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1003 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1004 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1005 | static void io_double_put_req(struct io_kiocb *req); |
c7dae4ba JA |
1006 | static void io_dismantle_req(struct io_kiocb *req); |
1007 | static void io_put_task(struct task_struct *task, int nr); | |
1008 | static void io_queue_next(struct io_kiocb *req); | |
94ae5e77 | 1009 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1010 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1011 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1012 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1013 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1014 | unsigned nr_args); |
3ca405eb | 1015 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1016 | static struct file *io_file_get(struct io_submit_state *state, |
1017 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1018 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1019 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1020 | |
847595de PB |
1021 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1022 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1023 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1024 | const struct iovec *fast_iov, | |
227c0c96 | 1025 | struct iov_iter *iter, bool force); |
907d1df3 | 1026 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1027 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1028 | struct io_ring_ctx *ctx); | |
de0617e4 | 1029 | |
2b188cc1 JA |
1030 | static struct kmem_cache *req_cachep; |
1031 | ||
0918682b | 1032 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1033 | |
1034 | struct sock *io_uring_get_socket(struct file *file) | |
1035 | { | |
1036 | #if defined(CONFIG_UNIX) | |
1037 | if (file->f_op == &io_uring_fops) { | |
1038 | struct io_ring_ctx *ctx = file->private_data; | |
1039 | ||
1040 | return ctx->ring_sock->sk; | |
1041 | } | |
1042 | #endif | |
1043 | return NULL; | |
1044 | } | |
1045 | EXPORT_SYMBOL(io_uring_get_socket); | |
1046 | ||
f2f87370 PB |
1047 | #define io_for_each_link(pos, head) \ |
1048 | for (pos = (head); pos; pos = pos->link) | |
1049 | ||
3ca405eb PB |
1050 | static inline void io_clean_op(struct io_kiocb *req) |
1051 | { | |
9d5c8190 | 1052 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1053 | __io_clean_op(req); |
1054 | } | |
1055 | ||
36f72fe2 PB |
1056 | static inline void io_set_resource_node(struct io_kiocb *req) |
1057 | { | |
1058 | struct io_ring_ctx *ctx = req->ctx; | |
1059 | ||
269bbe5f BM |
1060 | if (!req->fixed_rsrc_refs) { |
1061 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1062 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1063 | } |
1064 | } | |
1065 | ||
88f171ab PB |
1066 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1067 | { | |
1068 | if (!percpu_ref_tryget(ref)) { | |
1069 | /* already at zero, wait for ->release() */ | |
1070 | if (!try_wait_for_completion(compl)) | |
1071 | synchronize_rcu(); | |
1072 | return false; | |
1073 | } | |
1074 | ||
1075 | percpu_ref_resurrect(ref); | |
1076 | reinit_completion(compl); | |
1077 | percpu_ref_put(ref); | |
1078 | return true; | |
1079 | } | |
1080 | ||
08d23634 PB |
1081 | static bool io_match_task(struct io_kiocb *head, |
1082 | struct task_struct *task, | |
1083 | struct files_struct *files) | |
1084 | { | |
1085 | struct io_kiocb *req; | |
1086 | ||
84965ff8 JA |
1087 | if (task && head->task != task) { |
1088 | /* in terms of cancelation, always match if req task is dead */ | |
1089 | if (head->task->flags & PF_EXITING) | |
1090 | return true; | |
08d23634 | 1091 | return false; |
84965ff8 | 1092 | } |
08d23634 PB |
1093 | if (!files) |
1094 | return true; | |
1095 | ||
1096 | io_for_each_link(req, head) { | |
02a13674 JA |
1097 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1098 | continue; | |
1099 | if (req->file && req->file->f_op == &io_uring_fops) | |
1100 | return true; | |
4379bf8b | 1101 | if (req->task->files == files) |
08d23634 PB |
1102 | return true; |
1103 | } | |
1104 | return false; | |
1105 | } | |
1106 | ||
c40f6379 JA |
1107 | static inline void req_set_fail_links(struct io_kiocb *req) |
1108 | { | |
1109 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1110 | req->flags |= REQ_F_FAIL_LINK; | |
1111 | } | |
4a38aed2 | 1112 | |
ec99ca6c PB |
1113 | static inline void __io_req_init_async(struct io_kiocb *req) |
1114 | { | |
1115 | memset(&req->work, 0, sizeof(req->work)); | |
1116 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1117 | } | |
1118 | ||
7cdaf587 XW |
1119 | /* |
1120 | * Note: must call io_req_init_async() for the first time you | |
1121 | * touch any members of io_wq_work. | |
1122 | */ | |
1123 | static inline void io_req_init_async(struct io_kiocb *req) | |
1124 | { | |
1125 | if (req->flags & REQ_F_WORK_INITIALIZED) | |
1126 | return; | |
1127 | ||
ec99ca6c | 1128 | __io_req_init_async(req); |
7cdaf587 XW |
1129 | } |
1130 | ||
2b188cc1 JA |
1131 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1132 | { | |
1133 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1134 | ||
0f158b4c | 1135 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1136 | } |
1137 | ||
8eb7e2d0 PB |
1138 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1139 | { | |
1140 | return !req->timeout.off; | |
1141 | } | |
1142 | ||
2b188cc1 JA |
1143 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1144 | { | |
1145 | struct io_ring_ctx *ctx; | |
78076bb6 | 1146 | int hash_bits; |
2b188cc1 JA |
1147 | |
1148 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1149 | if (!ctx) | |
1150 | return NULL; | |
1151 | ||
78076bb6 JA |
1152 | /* |
1153 | * Use 5 bits less than the max cq entries, that should give us around | |
1154 | * 32 entries per hash list if totally full and uniformly spread. | |
1155 | */ | |
1156 | hash_bits = ilog2(p->cq_entries); | |
1157 | hash_bits -= 5; | |
1158 | if (hash_bits <= 0) | |
1159 | hash_bits = 1; | |
1160 | ctx->cancel_hash_bits = hash_bits; | |
1161 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1162 | GFP_KERNEL); | |
1163 | if (!ctx->cancel_hash) | |
1164 | goto err; | |
1165 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1166 | ||
21482896 | 1167 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1168 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1169 | goto err; | |
2b188cc1 JA |
1170 | |
1171 | ctx->flags = p->flags; | |
90554200 | 1172 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1173 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1174 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1175 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1176 | init_completion(&ctx->ref_comp); |
1177 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1178 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1179 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1180 | mutex_init(&ctx->uring_lock); |
1181 | init_waitqueue_head(&ctx->wait); | |
1182 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1183 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1184 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1185 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1186 | spin_lock_init(&ctx->inflight_lock); |
1187 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1188 | spin_lock_init(&ctx->rsrc_ref_lock); |
1189 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1190 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1191 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1192 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1193 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1194 | return ctx; |
206aefde | 1195 | err: |
78076bb6 | 1196 | kfree(ctx->cancel_hash); |
206aefde JA |
1197 | kfree(ctx); |
1198 | return NULL; | |
2b188cc1 JA |
1199 | } |
1200 | ||
9cf7c104 | 1201 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1202 | { |
2bc9930e JA |
1203 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1204 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1205 | |
9cf7c104 | 1206 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1207 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1208 | } |
de0617e4 | 1209 | |
9d858b21 | 1210 | return false; |
de0617e4 JA |
1211 | } |
1212 | ||
4edf20f9 | 1213 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1214 | { |
7cdaf587 | 1215 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1216 | return; |
51a4cc11 | 1217 | |
4379bf8b JA |
1218 | if (req->work.creds) { |
1219 | put_cred(req->work.creds); | |
1220 | req->work.creds = NULL; | |
1221 | } | |
34e08fed PB |
1222 | if (req->flags & REQ_F_INFLIGHT) { |
1223 | struct io_ring_ctx *ctx = req->ctx; | |
1224 | struct io_uring_task *tctx = req->task->io_uring; | |
1225 | unsigned long flags; | |
1226 | ||
1227 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1228 | list_del(&req->inflight_entry); | |
1229 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1230 | req->flags &= ~REQ_F_INFLIGHT; | |
1231 | if (atomic_read(&tctx->in_idle)) | |
1232 | wake_up(&tctx->wait); | |
1233 | } | |
51a4cc11 | 1234 | |
e86d0047 | 1235 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
561fb04a JA |
1236 | } |
1237 | ||
ce3d5aae PB |
1238 | static void io_req_track_inflight(struct io_kiocb *req) |
1239 | { | |
1240 | struct io_ring_ctx *ctx = req->ctx; | |
1241 | ||
1242 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1243 | io_req_init_async(req); | |
1244 | req->flags |= REQ_F_INFLIGHT; | |
1245 | ||
1246 | spin_lock_irq(&ctx->inflight_lock); | |
1247 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1248 | spin_unlock_irq(&ctx->inflight_lock); | |
1249 | } | |
1250 | } | |
1251 | ||
1e6fa521 JA |
1252 | static void io_prep_async_work(struct io_kiocb *req) |
1253 | { | |
1254 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1255 | struct io_ring_ctx *ctx = req->ctx; |
1256 | ||
1257 | io_req_init_async(req); | |
1258 | ||
feaadc4f PB |
1259 | if (req->flags & REQ_F_FORCE_ASYNC) |
1260 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1261 | ||
1e6fa521 JA |
1262 | if (req->flags & REQ_F_ISREG) { |
1263 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1264 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1265 | } else { | |
1266 | if (def->unbound_nonreg_file) | |
1267 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1268 | } | |
4379bf8b JA |
1269 | if (!req->work.creds) |
1270 | req->work.creds = get_current_cred(); | |
561fb04a | 1271 | } |
cccf0ee8 | 1272 | |
cbdcb435 | 1273 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1274 | { |
cbdcb435 | 1275 | struct io_kiocb *cur; |
54a91f3b | 1276 | |
f2f87370 PB |
1277 | io_for_each_link(cur, req) |
1278 | io_prep_async_work(cur); | |
561fb04a JA |
1279 | } |
1280 | ||
7271ef3a | 1281 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1282 | { |
a197f664 | 1283 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1284 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1285 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1286 | |
3bfe6106 JA |
1287 | BUG_ON(!tctx); |
1288 | BUG_ON(!tctx->io_wq); | |
1289 | ||
8766dd51 PB |
1290 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1291 | &req->work, req->flags); | |
5aa75ed5 | 1292 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a | 1293 | return link; |
18d9be1a JA |
1294 | } |
1295 | ||
cbdcb435 PB |
1296 | static void io_queue_async_work(struct io_kiocb *req) |
1297 | { | |
7271ef3a JA |
1298 | struct io_kiocb *link; |
1299 | ||
cbdcb435 PB |
1300 | /* init ->work of the whole link before punting */ |
1301 | io_prep_async_link(req); | |
7271ef3a JA |
1302 | link = __io_queue_async_work(req); |
1303 | ||
1304 | if (link) | |
1305 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1306 | } |
1307 | ||
5262f567 JA |
1308 | static void io_kill_timeout(struct io_kiocb *req) |
1309 | { | |
e8c2bc1f | 1310 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1311 | int ret; |
1312 | ||
e8c2bc1f | 1313 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1314 | if (ret != -1) { |
01cec8c1 PB |
1315 | atomic_set(&req->ctx->cq_timeouts, |
1316 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1317 | list_del_init(&req->timeout.list); |
78e19bbe | 1318 | io_cqring_fill_event(req, 0); |
216578e5 | 1319 | io_put_req_deferred(req, 1); |
5262f567 JA |
1320 | } |
1321 | } | |
1322 | ||
76e1b642 JA |
1323 | /* |
1324 | * Returns true if we found and killed one or more timeouts | |
1325 | */ | |
6b81928d PB |
1326 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1327 | struct files_struct *files) | |
5262f567 JA |
1328 | { |
1329 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1330 | int canceled = 0; |
5262f567 JA |
1331 | |
1332 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1333 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1334 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1335 | io_kill_timeout(req); |
76e1b642 JA |
1336 | canceled++; |
1337 | } | |
f3606e3a | 1338 | } |
5262f567 | 1339 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1340 | return canceled != 0; |
5262f567 JA |
1341 | } |
1342 | ||
04518945 | 1343 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1344 | { |
04518945 | 1345 | do { |
27dc8338 PB |
1346 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1347 | struct io_defer_entry, list); | |
de0617e4 | 1348 | |
9cf7c104 | 1349 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1350 | break; |
27dc8338 | 1351 | list_del_init(&de->list); |
907d1df3 | 1352 | io_req_task_queue(de->req); |
27dc8338 | 1353 | kfree(de); |
04518945 PB |
1354 | } while (!list_empty(&ctx->defer_list)); |
1355 | } | |
1356 | ||
360428f8 | 1357 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1358 | { |
f010505b MDG |
1359 | u32 seq; |
1360 | ||
1361 | if (list_empty(&ctx->timeout_list)) | |
1362 | return; | |
1363 | ||
1364 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1365 | ||
1366 | do { | |
1367 | u32 events_needed, events_got; | |
360428f8 | 1368 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1369 | struct io_kiocb, timeout.list); |
de0617e4 | 1370 | |
8eb7e2d0 | 1371 | if (io_is_timeout_noseq(req)) |
360428f8 | 1372 | break; |
f010505b MDG |
1373 | |
1374 | /* | |
1375 | * Since seq can easily wrap around over time, subtract | |
1376 | * the last seq at which timeouts were flushed before comparing. | |
1377 | * Assuming not more than 2^31-1 events have happened since, | |
1378 | * these subtractions won't have wrapped, so we can check if | |
1379 | * target is in [last_seq, current_seq] by comparing the two. | |
1380 | */ | |
1381 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1382 | events_got = seq - ctx->cq_last_tm_flush; | |
1383 | if (events_got < events_needed) | |
360428f8 | 1384 | break; |
bfe68a22 | 1385 | |
135fcde8 | 1386 | list_del_init(&req->timeout.list); |
5262f567 | 1387 | io_kill_timeout(req); |
f010505b MDG |
1388 | } while (!list_empty(&ctx->timeout_list)); |
1389 | ||
1390 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1391 | } |
5262f567 | 1392 | |
360428f8 PB |
1393 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1394 | { | |
1395 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1396 | |
1397 | /* order cqe stores with ring update */ | |
1398 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1399 | |
04518945 PB |
1400 | if (unlikely(!list_empty(&ctx->defer_list))) |
1401 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1402 | } |
1403 | ||
90554200 JA |
1404 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1405 | { | |
1406 | struct io_rings *r = ctx->rings; | |
1407 | ||
1408 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1409 | } | |
1410 | ||
888aae2e PB |
1411 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1412 | { | |
1413 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1414 | } | |
1415 | ||
2b188cc1 JA |
1416 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1417 | { | |
75b28aff | 1418 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1419 | unsigned tail; |
1420 | ||
115e12e5 SB |
1421 | /* |
1422 | * writes to the cq entry need to come after reading head; the | |
1423 | * control dependency is enough as we're using WRITE_ONCE to | |
1424 | * fill the cq entry | |
1425 | */ | |
888aae2e | 1426 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1427 | return NULL; |
1428 | ||
888aae2e | 1429 | tail = ctx->cached_cq_tail++; |
75b28aff | 1430 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1431 | } |
1432 | ||
f2842ab5 JA |
1433 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1434 | { | |
f0b493e6 JA |
1435 | if (!ctx->cq_ev_fd) |
1436 | return false; | |
7e55a19c SG |
1437 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1438 | return false; | |
f2842ab5 JA |
1439 | if (!ctx->eventfd_async) |
1440 | return true; | |
b41e9852 | 1441 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1442 | } |
1443 | ||
b41e9852 | 1444 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1445 | { |
b1445e59 PB |
1446 | /* see waitqueue_active() comment */ |
1447 | smp_mb(); | |
1448 | ||
1d7bb1d5 JA |
1449 | if (waitqueue_active(&ctx->wait)) |
1450 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1451 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1452 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1453 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1454 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1455 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1456 | wake_up_interruptible(&ctx->cq_wait); |
1457 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1458 | } | |
1d7bb1d5 JA |
1459 | } |
1460 | ||
80c18e4a PB |
1461 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1462 | { | |
b1445e59 PB |
1463 | /* see waitqueue_active() comment */ |
1464 | smp_mb(); | |
1465 | ||
80c18e4a PB |
1466 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1467 | if (waitqueue_active(&ctx->wait)) | |
1468 | wake_up(&ctx->wait); | |
1469 | } | |
1470 | if (io_should_trigger_evfd(ctx)) | |
1471 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1472 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1473 | wake_up_interruptible(&ctx->cq_wait); |
1474 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1475 | } | |
80c18e4a PB |
1476 | } |
1477 | ||
c4a2ed72 | 1478 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1479 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1480 | struct task_struct *tsk, | |
1481 | struct files_struct *files) | |
1d7bb1d5 JA |
1482 | { |
1483 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1484 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1485 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1486 | unsigned long flags; |
b18032bb | 1487 | bool all_flushed, posted; |
1d7bb1d5 JA |
1488 | LIST_HEAD(list); |
1489 | ||
e23de15f PB |
1490 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1491 | return false; | |
1d7bb1d5 | 1492 | |
b18032bb | 1493 | posted = false; |
1d7bb1d5 | 1494 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1495 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1496 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1497 | continue; |
1498 | ||
1d7bb1d5 JA |
1499 | cqe = io_get_cqring(ctx); |
1500 | if (!cqe && !force) | |
1501 | break; | |
1502 | ||
40d8ddd4 | 1503 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1504 | if (cqe) { |
1505 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1506 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1507 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1508 | } else { |
2c3bac6d | 1509 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1510 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1511 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1512 | } |
b18032bb | 1513 | posted = true; |
1d7bb1d5 JA |
1514 | } |
1515 | ||
09e88404 PB |
1516 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1517 | if (all_flushed) { | |
1518 | clear_bit(0, &ctx->sq_check_overflow); | |
1519 | clear_bit(0, &ctx->cq_check_overflow); | |
1520 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1521 | } | |
46930143 | 1522 | |
b18032bb JA |
1523 | if (posted) |
1524 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1525 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1526 | if (posted) |
1527 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1528 | |
1529 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1530 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1531 | list_del(&req->compl.list); | |
ec9c02ad | 1532 | io_put_req(req); |
1d7bb1d5 | 1533 | } |
c4a2ed72 | 1534 | |
09e88404 | 1535 | return all_flushed; |
1d7bb1d5 JA |
1536 | } |
1537 | ||
6c503150 PB |
1538 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1539 | struct task_struct *tsk, | |
1540 | struct files_struct *files) | |
1541 | { | |
1542 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1543 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1544 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1545 | mutex_lock(&ctx->uring_lock); | |
1546 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1547 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1548 | mutex_unlock(&ctx->uring_lock); | |
1549 | } | |
1550 | } | |
1551 | ||
bcda7baa | 1552 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1553 | { |
78e19bbe | 1554 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1555 | struct io_uring_cqe *cqe; |
1556 | ||
78e19bbe | 1557 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1558 | |
2b188cc1 JA |
1559 | /* |
1560 | * If we can't get a cq entry, userspace overflowed the | |
1561 | * submission (by quite a lot). Increment the overflow count in | |
1562 | * the ring. | |
1563 | */ | |
1564 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1565 | if (likely(cqe)) { |
78e19bbe | 1566 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1567 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1568 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1569 | } else if (ctx->cq_overflow_flushed || |
1570 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1571 | /* |
1572 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1573 | * then we cannot store the request for later flushing, we need | |
1574 | * to drop it on the floor. | |
1575 | */ | |
2c3bac6d PB |
1576 | ctx->cached_cq_overflow++; |
1577 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1578 | } else { |
ad3eb2c8 JA |
1579 | if (list_empty(&ctx->cq_overflow_list)) { |
1580 | set_bit(0, &ctx->sq_check_overflow); | |
1581 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1582 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1583 | } |
40d8ddd4 | 1584 | io_clean_op(req); |
1d7bb1d5 | 1585 | req->result = res; |
0f7e466b | 1586 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1587 | refcount_inc(&req->refs); |
1588 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1589 | } |
1590 | } | |
1591 | ||
bcda7baa JA |
1592 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1593 | { | |
1594 | __io_cqring_fill_event(req, res, 0); | |
1595 | } | |
1596 | ||
c7dae4ba JA |
1597 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1598 | unsigned int cflags) | |
2b188cc1 | 1599 | { |
78e19bbe | 1600 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1601 | unsigned long flags; |
1602 | ||
1603 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1604 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1605 | io_commit_cqring(ctx); |
c7dae4ba JA |
1606 | /* |
1607 | * If we're the last reference to this request, add to our locked | |
1608 | * free_list cache. | |
1609 | */ | |
1610 | if (refcount_dec_and_test(&req->refs)) { | |
1611 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1612 | ||
1613 | io_dismantle_req(req); | |
1614 | io_put_task(req->task, 1); | |
1615 | list_add(&req->compl.list, &cs->locked_free_list); | |
1616 | cs->locked_free_nr++; | |
1617 | } else | |
1618 | req = NULL; | |
2b188cc1 JA |
1619 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1620 | ||
8c838788 | 1621 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1622 | if (req) { |
1623 | io_queue_next(req); | |
1624 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1625 | } |
229a7b63 JA |
1626 | } |
1627 | ||
a38d68db | 1628 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1629 | unsigned int cflags) |
229a7b63 | 1630 | { |
a38d68db PB |
1631 | io_clean_op(req); |
1632 | req->result = res; | |
1633 | req->compl.cflags = cflags; | |
e342c807 | 1634 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1635 | } |
1636 | ||
889fca73 PB |
1637 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1638 | long res, unsigned cflags) | |
bcda7baa | 1639 | { |
889fca73 PB |
1640 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1641 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1642 | else |
c7dae4ba | 1643 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1644 | } |
1645 | ||
a38d68db | 1646 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1647 | { |
889fca73 | 1648 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1649 | } |
1650 | ||
c7dae4ba | 1651 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1652 | { |
c7dae4ba JA |
1653 | struct io_submit_state *state = &ctx->submit_state; |
1654 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 1655 | struct io_kiocb *req = NULL; |
0ddf92e8 | 1656 | |
c7dae4ba JA |
1657 | /* |
1658 | * If we have more than a batch's worth of requests in our IRQ side | |
1659 | * locked cache, grab the lock and move them over to our submission | |
1660 | * side cache. | |
1661 | */ | |
1662 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
1663 | spin_lock_irq(&ctx->completion_lock); | |
1664 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1665 | cs->locked_free_nr = 0; | |
1666 | spin_unlock_irq(&ctx->completion_lock); | |
1667 | } | |
0ddf92e8 | 1668 | |
c7dae4ba JA |
1669 | while (!list_empty(&cs->free_list)) { |
1670 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
1671 | compl.list); |
1672 | list_del(&req->compl.list); | |
e5d1bc0a PB |
1673 | state->reqs[state->free_reqs++] = req; |
1674 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
1675 | break; | |
1b4c351f JA |
1676 | } |
1677 | ||
e5d1bc0a | 1678 | return req != NULL; |
0ddf92e8 JA |
1679 | } |
1680 | ||
e5d1bc0a | 1681 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1682 | { |
e5d1bc0a PB |
1683 | struct io_submit_state *state = &ctx->submit_state; |
1684 | ||
1685 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1686 | ||
f6b6c7d6 | 1687 | if (!state->free_reqs) { |
291b2821 | 1688 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1689 | int ret; |
1690 | ||
c7dae4ba | 1691 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1692 | goto got_req; |
1693 | ||
bf019da7 PB |
1694 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1695 | state->reqs); | |
fd6fab2c JA |
1696 | |
1697 | /* | |
1698 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1699 | * retry single alloc to be on the safe side. | |
1700 | */ | |
1701 | if (unlikely(ret <= 0)) { | |
1702 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1703 | if (!state->reqs[0]) | |
3893f39f | 1704 | return NULL; |
fd6fab2c JA |
1705 | ret = 1; |
1706 | } | |
291b2821 | 1707 | state->free_reqs = ret; |
2b188cc1 | 1708 | } |
e5d1bc0a | 1709 | got_req: |
291b2821 PB |
1710 | state->free_reqs--; |
1711 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1712 | } |
1713 | ||
8da11c19 PB |
1714 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1715 | bool fixed) | |
1716 | { | |
36f72fe2 | 1717 | if (!fixed) |
8da11c19 PB |
1718 | fput(file); |
1719 | } | |
1720 | ||
4edf20f9 | 1721 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1722 | { |
3ca405eb | 1723 | io_clean_op(req); |
929a3af9 | 1724 | |
e8c2bc1f JA |
1725 | if (req->async_data) |
1726 | kfree(req->async_data); | |
8da11c19 PB |
1727 | if (req->file) |
1728 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
1729 | if (req->fixed_rsrc_refs) |
1730 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 1731 | io_req_clean_work(req); |
e65ef56d JA |
1732 | } |
1733 | ||
7c660731 PB |
1734 | static inline void io_put_task(struct task_struct *task, int nr) |
1735 | { | |
1736 | struct io_uring_task *tctx = task->io_uring; | |
1737 | ||
1738 | percpu_counter_sub(&tctx->inflight, nr); | |
1739 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1740 | wake_up(&tctx->wait); | |
1741 | put_task_struct_many(task, nr); | |
1742 | } | |
1743 | ||
216578e5 | 1744 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1745 | { |
51a4cc11 | 1746 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1747 | |
216578e5 | 1748 | io_dismantle_req(req); |
7c660731 | 1749 | io_put_task(req->task, 1); |
c6ca97b3 | 1750 | |
3893f39f | 1751 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1752 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1753 | } |
1754 | ||
f2f87370 PB |
1755 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1756 | { | |
1757 | struct io_kiocb *nxt = req->link; | |
1758 | ||
1759 | req->link = nxt->link; | |
1760 | nxt->link = NULL; | |
1761 | } | |
1762 | ||
c9abd7ad | 1763 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1764 | { |
a197f664 | 1765 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1766 | struct io_kiocb *link; |
c9abd7ad PB |
1767 | bool cancelled = false; |
1768 | unsigned long flags; | |
7c86ffee | 1769 | |
c9abd7ad | 1770 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1771 | link = req->link; |
1772 | ||
900fad45 PB |
1773 | /* |
1774 | * Can happen if a linked timeout fired and link had been like | |
1775 | * req -> link t-out -> link t-out [-> ...] | |
1776 | */ | |
c9abd7ad PB |
1777 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1778 | struct io_timeout_data *io = link->async_data; | |
1779 | int ret; | |
7c86ffee | 1780 | |
f2f87370 | 1781 | io_remove_next_linked(req); |
90cd7e42 | 1782 | link->timeout.head = NULL; |
c9abd7ad PB |
1783 | ret = hrtimer_try_to_cancel(&io->timer); |
1784 | if (ret != -1) { | |
1785 | io_cqring_fill_event(link, -ECANCELED); | |
1786 | io_commit_cqring(ctx); | |
1787 | cancelled = true; | |
1788 | } | |
1789 | } | |
7c86ffee | 1790 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 1791 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 1792 | |
c9abd7ad | 1793 | if (cancelled) { |
7c86ffee | 1794 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
1795 | io_put_req(link); |
1796 | } | |
7c86ffee PB |
1797 | } |
1798 | ||
9e645e11 | 1799 | |
d148ca4b | 1800 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1801 | { |
f2f87370 | 1802 | struct io_kiocb *link, *nxt; |
2665abfd | 1803 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 1804 | unsigned long flags; |
9e645e11 | 1805 | |
d148ca4b | 1806 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1807 | link = req->link; |
1808 | req->link = NULL; | |
9e645e11 | 1809 | |
f2f87370 PB |
1810 | while (link) { |
1811 | nxt = link->link; | |
1812 | link->link = NULL; | |
2665abfd | 1813 | |
f2f87370 | 1814 | trace_io_uring_fail_link(req, link); |
7c86ffee | 1815 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
1816 | |
1817 | /* | |
1818 | * It's ok to free under spinlock as they're not linked anymore, | |
1819 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
1820 | * work.fs->lock. | |
1821 | */ | |
1822 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
1823 | io_put_req_deferred(link, 2); | |
1824 | else | |
1825 | io_double_put_req(link); | |
f2f87370 | 1826 | link = nxt; |
9e645e11 | 1827 | } |
2665abfd | 1828 | io_commit_cqring(ctx); |
216578e5 | 1829 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 1830 | |
2665abfd | 1831 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1832 | } |
1833 | ||
3fa5e0f3 | 1834 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1835 | { |
7c86ffee PB |
1836 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1837 | io_kill_linked_timeout(req); | |
944e58bf | 1838 | |
9e645e11 JA |
1839 | /* |
1840 | * If LINK is set, we have dependent requests in this chain. If we | |
1841 | * didn't fail this request, queue the first one up, moving any other | |
1842 | * dependencies to the next request. In case of failure, fail the rest | |
1843 | * of the chain. | |
1844 | */ | |
f2f87370 PB |
1845 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
1846 | struct io_kiocb *nxt = req->link; | |
1847 | ||
1848 | req->link = NULL; | |
1849 | return nxt; | |
1850 | } | |
9b5f7bd9 PB |
1851 | io_fail_links(req); |
1852 | return NULL; | |
4d7dd462 | 1853 | } |
9e645e11 | 1854 | |
f2f87370 | 1855 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1856 | { |
cdbff982 | 1857 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1858 | return NULL; |
1859 | return __io_req_find_next(req); | |
1860 | } | |
1861 | ||
7cbf1722 | 1862 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1863 | { |
65453d1e | 1864 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1865 | struct io_wq_work_list list; |
1866 | struct io_wq_work_node *node; | |
c2c4c83c | 1867 | |
7cbf1722 JA |
1868 | if (wq_list_empty(&tctx->task_list)) |
1869 | return false; | |
6200b0ae | 1870 | |
0b81e80c | 1871 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1872 | list = tctx->task_list; |
1873 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1874 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1875 | |
7cbf1722 JA |
1876 | node = list.first; |
1877 | while (node) { | |
1878 | struct io_wq_work_node *next = node->next; | |
65453d1e | 1879 | struct io_ring_ctx *this_ctx; |
7cbf1722 | 1880 | struct io_kiocb *req; |
0ba9c9ed | 1881 | |
7cbf1722 | 1882 | req = container_of(node, struct io_kiocb, io_task_work.node); |
65453d1e | 1883 | this_ctx = req->ctx; |
7cbf1722 JA |
1884 | req->task_work.func(&req->task_work); |
1885 | node = next; | |
65453d1e JA |
1886 | |
1887 | if (!ctx) { | |
1888 | ctx = this_ctx; | |
1889 | } else if (ctx != this_ctx) { | |
1890 | mutex_lock(&ctx->uring_lock); | |
1891 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1892 | mutex_unlock(&ctx->uring_lock); | |
1893 | ctx = this_ctx; | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | if (ctx && ctx->submit_state.comp.nr) { | |
1898 | mutex_lock(&ctx->uring_lock); | |
1899 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1900 | mutex_unlock(&ctx->uring_lock); | |
7cbf1722 JA |
1901 | } |
1902 | ||
1903 | return list.first != NULL; | |
c2c4c83c JA |
1904 | } |
1905 | ||
7cbf1722 | 1906 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1907 | { |
7cbf1722 | 1908 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1909 | |
7cbf1722 JA |
1910 | while (__tctx_task_work(tctx)) |
1911 | cond_resched(); | |
1912 | ||
1913 | clear_bit(0, &tctx->task_state); | |
1914 | } | |
1915 | ||
1916 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
1917 | enum task_work_notify_mode notify) | |
1918 | { | |
1919 | struct io_uring_task *tctx = tsk->io_uring; | |
1920 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 1921 | unsigned long flags; |
7cbf1722 JA |
1922 | int ret; |
1923 | ||
1924 | WARN_ON_ONCE(!tctx); | |
1925 | ||
0b81e80c | 1926 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1927 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1928 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1929 | |
1930 | /* task_work already pending, we're done */ | |
1931 | if (test_bit(0, &tctx->task_state) || | |
1932 | test_and_set_bit(0, &tctx->task_state)) | |
1933 | return 0; | |
1934 | ||
1935 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
1936 | return 0; | |
1937 | ||
1938 | /* | |
1939 | * Slow path - we failed, find and delete work. if the work is not | |
1940 | * in the list, it got run and we're fine. | |
1941 | */ | |
1942 | ret = 0; | |
0b81e80c | 1943 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1944 | wq_list_for_each(node, prev, &tctx->task_list) { |
1945 | if (&req->io_task_work.node == node) { | |
1946 | wq_list_del(&tctx->task_list, node, prev); | |
1947 | ret = 1; | |
1948 | break; | |
1949 | } | |
1950 | } | |
0b81e80c | 1951 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1952 | clear_bit(0, &tctx->task_state); |
1953 | return ret; | |
1954 | } | |
1955 | ||
355fb9e2 | 1956 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
1957 | { |
1958 | struct task_struct *tsk = req->task; | |
1959 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
1960 | enum task_work_notify_mode notify; |
1961 | int ret; | |
c2c4c83c | 1962 | |
6200b0ae JA |
1963 | if (tsk->flags & PF_EXITING) |
1964 | return -ESRCH; | |
1965 | ||
c2c4c83c | 1966 | /* |
0ba9c9ed JA |
1967 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1968 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1969 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1970 | * will do the job. | |
c2c4c83c | 1971 | */ |
91989c70 | 1972 | notify = TWA_NONE; |
355fb9e2 | 1973 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
1974 | notify = TWA_SIGNAL; |
1975 | ||
7cbf1722 | 1976 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
1977 | if (!ret) |
1978 | wake_up_process(tsk); | |
0ba9c9ed | 1979 | |
c2c4c83c JA |
1980 | return ret; |
1981 | } | |
1982 | ||
eab30c4d | 1983 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 1984 | task_work_func_t cb) |
eab30c4d | 1985 | { |
7c25c0d1 JA |
1986 | struct io_ring_ctx *ctx = req->ctx; |
1987 | struct callback_head *head; | |
eab30c4d PB |
1988 | |
1989 | init_task_work(&req->task_work, cb); | |
7c25c0d1 JA |
1990 | do { |
1991 | head = READ_ONCE(ctx->exit_task_work); | |
1992 | req->task_work.next = head; | |
1993 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); | |
eab30c4d PB |
1994 | } |
1995 | ||
c40f6379 JA |
1996 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1997 | { | |
1998 | struct io_ring_ctx *ctx = req->ctx; | |
1999 | ||
2000 | spin_lock_irq(&ctx->completion_lock); | |
2001 | io_cqring_fill_event(req, error); | |
2002 | io_commit_cqring(ctx); | |
2003 | spin_unlock_irq(&ctx->completion_lock); | |
2004 | ||
2005 | io_cqring_ev_posted(ctx); | |
2006 | req_set_fail_links(req); | |
2007 | io_double_put_req(req); | |
2008 | } | |
2009 | ||
2010 | static void io_req_task_cancel(struct callback_head *cb) | |
2011 | { | |
2012 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2013 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2014 | |
792bb6eb | 2015 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 2016 | __io_req_task_cancel(req, req->result); |
792bb6eb | 2017 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 2018 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2019 | } |
2020 | ||
2021 | static void __io_req_task_submit(struct io_kiocb *req) | |
2022 | { | |
2023 | struct io_ring_ctx *ctx = req->ctx; | |
2024 | ||
04fc6c80 | 2025 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2026 | mutex_lock(&ctx->uring_lock); |
37d1e2e3 | 2027 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING)) |
c5eef2b9 | 2028 | __io_queue_sqe(req); |
81b6d05c | 2029 | else |
c40f6379 | 2030 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2031 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2032 | } |
2033 | ||
2034 | static void io_req_task_submit(struct callback_head *cb) | |
2035 | { | |
2036 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2037 | ||
2038 | __io_req_task_submit(req); | |
2039 | } | |
2040 | ||
2041 | static void io_req_task_queue(struct io_kiocb *req) | |
2042 | { | |
c40f6379 JA |
2043 | int ret; |
2044 | ||
7cbf1722 | 2045 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 2046 | ret = io_req_task_work_add(req); |
c40f6379 | 2047 | if (unlikely(ret)) { |
a3df7698 | 2048 | req->result = -ECANCELED; |
04fc6c80 | 2049 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 2050 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 2051 | } |
c40f6379 JA |
2052 | } |
2053 | ||
a3df7698 PB |
2054 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
2055 | { | |
2056 | percpu_ref_get(&req->ctx->refs); | |
2057 | req->result = ret; | |
2058 | req->task_work.func = io_req_task_cancel; | |
2059 | ||
2060 | if (unlikely(io_req_task_work_add(req))) | |
2061 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2062 | } | |
2063 | ||
f2f87370 | 2064 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2065 | { |
9b5f7bd9 | 2066 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2067 | |
2068 | if (nxt) | |
906a8c3f | 2069 | io_req_task_queue(nxt); |
c69f8dbe JL |
2070 | } |
2071 | ||
c3524383 | 2072 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2073 | { |
c3524383 PB |
2074 | io_queue_next(req); |
2075 | __io_free_req(req); | |
2076 | } | |
8766dd51 | 2077 | |
2d6500d4 | 2078 | struct req_batch { |
5af1d13e PB |
2079 | struct task_struct *task; |
2080 | int task_refs; | |
1b4c351f | 2081 | int ctx_refs; |
2d6500d4 PB |
2082 | }; |
2083 | ||
5af1d13e PB |
2084 | static inline void io_init_req_batch(struct req_batch *rb) |
2085 | { | |
5af1d13e | 2086 | rb->task_refs = 0; |
9ae72463 | 2087 | rb->ctx_refs = 0; |
5af1d13e PB |
2088 | rb->task = NULL; |
2089 | } | |
2090 | ||
2d6500d4 PB |
2091 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2092 | struct req_batch *rb) | |
2093 | { | |
6e833d53 | 2094 | if (rb->task) |
7c660731 | 2095 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2096 | if (rb->ctx_refs) |
2097 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2098 | } |
2099 | ||
6ff119a6 PB |
2100 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2101 | struct io_submit_state *state) | |
2d6500d4 | 2102 | { |
f2f87370 | 2103 | io_queue_next(req); |
2d6500d4 | 2104 | |
e3bc8e9d | 2105 | if (req->task != rb->task) { |
7c660731 PB |
2106 | if (rb->task) |
2107 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2108 | rb->task = req->task; |
2109 | rb->task_refs = 0; | |
5af1d13e | 2110 | } |
e3bc8e9d | 2111 | rb->task_refs++; |
9ae72463 | 2112 | rb->ctx_refs++; |
5af1d13e | 2113 | |
4edf20f9 | 2114 | io_dismantle_req(req); |
bd759045 | 2115 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2116 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2117 | else |
2118 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2119 | } |
2120 | ||
905c172f PB |
2121 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2122 | struct io_ring_ctx *ctx) | |
2123 | { | |
2124 | int i, nr = cs->nr; | |
2125 | struct io_kiocb *req; | |
2126 | struct req_batch rb; | |
2127 | ||
2128 | io_init_req_batch(&rb); | |
2129 | spin_lock_irq(&ctx->completion_lock); | |
2130 | for (i = 0; i < nr; i++) { | |
2131 | req = cs->reqs[i]; | |
2132 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2133 | } | |
2134 | io_commit_cqring(ctx); | |
2135 | spin_unlock_irq(&ctx->completion_lock); | |
2136 | ||
2137 | io_cqring_ev_posted(ctx); | |
2138 | for (i = 0; i < nr; i++) { | |
2139 | req = cs->reqs[i]; | |
2140 | ||
2141 | /* submission and completion refs */ | |
2142 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2143 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2144 | } |
2145 | ||
2146 | io_req_free_batch_finish(ctx, &rb); | |
2147 | cs->nr = 0; | |
7a743e22 PB |
2148 | } |
2149 | ||
ba816ad6 JA |
2150 | /* |
2151 | * Drop reference to request, return next in chain (if there is one) if this | |
2152 | * was the last reference to this request. | |
2153 | */ | |
9b5f7bd9 | 2154 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2155 | { |
9b5f7bd9 PB |
2156 | struct io_kiocb *nxt = NULL; |
2157 | ||
2a44f467 | 2158 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2159 | nxt = io_req_find_next(req); |
4d7dd462 | 2160 | __io_free_req(req); |
2a44f467 | 2161 | } |
9b5f7bd9 | 2162 | return nxt; |
2b188cc1 JA |
2163 | } |
2164 | ||
e65ef56d JA |
2165 | static void io_put_req(struct io_kiocb *req) |
2166 | { | |
2167 | if (refcount_dec_and_test(&req->refs)) | |
2168 | io_free_req(req); | |
2b188cc1 JA |
2169 | } |
2170 | ||
216578e5 PB |
2171 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2172 | { | |
2173 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2174 | ||
2175 | io_free_req(req); | |
2176 | } | |
2177 | ||
2178 | static void io_free_req_deferred(struct io_kiocb *req) | |
2179 | { | |
2180 | int ret; | |
2181 | ||
7cbf1722 | 2182 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2183 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2184 | if (unlikely(ret)) |
2185 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2186 | } |
2187 | ||
2188 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2189 | { | |
2190 | if (refcount_sub_and_test(refs, &req->refs)) | |
2191 | io_free_req_deferred(req); | |
2192 | } | |
2193 | ||
978db57e JA |
2194 | static void io_double_put_req(struct io_kiocb *req) |
2195 | { | |
2196 | /* drop both submit and complete references */ | |
2197 | if (refcount_sub_and_test(2, &req->refs)) | |
2198 | io_free_req(req); | |
2199 | } | |
2200 | ||
6c503150 | 2201 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2202 | { |
2203 | /* See comment at the top of this file */ | |
2204 | smp_rmb(); | |
e23de15f | 2205 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2206 | } |
2207 | ||
fb5ccc98 PB |
2208 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2209 | { | |
2210 | struct io_rings *rings = ctx->rings; | |
2211 | ||
2212 | /* make sure SQ entry isn't read before tail */ | |
2213 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2214 | } | |
2215 | ||
8ff069bf | 2216 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2217 | { |
8ff069bf | 2218 | unsigned int cflags; |
e94f141b | 2219 | |
bcda7baa JA |
2220 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2221 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2222 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2223 | kfree(kbuf); |
2224 | return cflags; | |
e94f141b JA |
2225 | } |
2226 | ||
8ff069bf | 2227 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2228 | { |
4d954c25 | 2229 | struct io_buffer *kbuf; |
bcda7baa | 2230 | |
4d954c25 | 2231 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2232 | return io_put_kbuf(req, kbuf); |
2233 | } | |
2234 | ||
4c6e277c JA |
2235 | static inline bool io_run_task_work(void) |
2236 | { | |
6200b0ae JA |
2237 | /* |
2238 | * Not safe to run on exiting task, and the task_work handling will | |
2239 | * not add work to such a task. | |
2240 | */ | |
2241 | if (unlikely(current->flags & PF_EXITING)) | |
2242 | return false; | |
4c6e277c JA |
2243 | if (current->task_works) { |
2244 | __set_current_state(TASK_RUNNING); | |
2245 | task_work_run(); | |
2246 | return true; | |
2247 | } | |
2248 | ||
2249 | return false; | |
bcda7baa JA |
2250 | } |
2251 | ||
def596e9 JA |
2252 | /* |
2253 | * Find and free completed poll iocbs | |
2254 | */ | |
2255 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2256 | struct list_head *done) | |
2257 | { | |
8237e045 | 2258 | struct req_batch rb; |
def596e9 | 2259 | struct io_kiocb *req; |
bbde017a XW |
2260 | |
2261 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2262 | smp_rmb(); | |
def596e9 | 2263 | |
5af1d13e | 2264 | io_init_req_batch(&rb); |
def596e9 | 2265 | while (!list_empty(done)) { |
bcda7baa JA |
2266 | int cflags = 0; |
2267 | ||
d21ffe7e | 2268 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2269 | list_del(&req->inflight_entry); |
2270 | ||
bbde017a XW |
2271 | if (READ_ONCE(req->result) == -EAGAIN) { |
2272 | req->iopoll_completed = 0; | |
23faba36 | 2273 | if (io_rw_reissue(req)) |
f161340d | 2274 | continue; |
bbde017a | 2275 | } |
def596e9 | 2276 | |
bcda7baa | 2277 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2278 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2279 | |
2280 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2281 | (*nr_events)++; |
2282 | ||
c3524383 | 2283 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2284 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2285 | } |
def596e9 | 2286 | |
09bb8394 | 2287 | io_commit_cqring(ctx); |
80c18e4a | 2288 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2289 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2290 | } |
2291 | ||
def596e9 JA |
2292 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2293 | long min) | |
2294 | { | |
2295 | struct io_kiocb *req, *tmp; | |
2296 | LIST_HEAD(done); | |
2297 | bool spin; | |
2298 | int ret; | |
2299 | ||
2300 | /* | |
2301 | * Only spin for completions if we don't have multiple devices hanging | |
2302 | * off our complete list, and we're under the requested amount. | |
2303 | */ | |
2304 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2305 | ||
2306 | ret = 0; | |
d21ffe7e | 2307 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2308 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2309 | |
2310 | /* | |
581f9810 BM |
2311 | * Move completed and retryable entries to our local lists. |
2312 | * If we find a request that requires polling, break out | |
2313 | * and complete those lists first, if we have entries there. | |
def596e9 | 2314 | */ |
65a6543d | 2315 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2316 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2317 | continue; |
2318 | } | |
2319 | if (!list_empty(&done)) | |
2320 | break; | |
2321 | ||
2322 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2323 | if (ret < 0) | |
2324 | break; | |
2325 | ||
3aadc23e PB |
2326 | /* iopoll may have completed current req */ |
2327 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2328 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2329 | |
def596e9 JA |
2330 | if (ret && spin) |
2331 | spin = false; | |
2332 | ret = 0; | |
2333 | } | |
2334 | ||
2335 | if (!list_empty(&done)) | |
2336 | io_iopoll_complete(ctx, nr_events, &done); | |
2337 | ||
2338 | return ret; | |
2339 | } | |
2340 | ||
2341 | /* | |
d195a66e | 2342 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2343 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2344 | * as a non-spinning completion check. | |
2345 | */ | |
2346 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2347 | long min) | |
2348 | { | |
540e32a0 | 2349 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2350 | int ret; |
2351 | ||
2352 | ret = io_do_iopoll(ctx, nr_events, min); | |
2353 | if (ret < 0) | |
2354 | return ret; | |
eba0a4dd | 2355 | if (*nr_events >= min) |
def596e9 JA |
2356 | return 0; |
2357 | } | |
2358 | ||
2359 | return 1; | |
2360 | } | |
2361 | ||
2362 | /* | |
2363 | * We can't just wait for polled events to come to us, we have to actively | |
2364 | * find and complete them. | |
2365 | */ | |
b2edc0a7 | 2366 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2367 | { |
2368 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2369 | return; | |
2370 | ||
2371 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2372 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2373 | unsigned int nr_events = 0; |
2374 | ||
b2edc0a7 | 2375 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2376 | |
b2edc0a7 PB |
2377 | /* let it sleep and repeat later if can't complete a request */ |
2378 | if (nr_events == 0) | |
2379 | break; | |
08f5439f JA |
2380 | /* |
2381 | * Ensure we allow local-to-the-cpu processing to take place, | |
2382 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2383 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2384 | */ |
3fcee5a6 PB |
2385 | if (need_resched()) { |
2386 | mutex_unlock(&ctx->uring_lock); | |
2387 | cond_resched(); | |
2388 | mutex_lock(&ctx->uring_lock); | |
2389 | } | |
def596e9 JA |
2390 | } |
2391 | mutex_unlock(&ctx->uring_lock); | |
2392 | } | |
2393 | ||
7668b92a | 2394 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2395 | { |
7668b92a | 2396 | unsigned int nr_events = 0; |
2b2ed975 | 2397 | int iters = 0, ret = 0; |
500f9fba | 2398 | |
c7849be9 XW |
2399 | /* |
2400 | * We disallow the app entering submit/complete with polling, but we | |
2401 | * still need to lock the ring to prevent racing with polled issue | |
2402 | * that got punted to a workqueue. | |
2403 | */ | |
2404 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2405 | do { |
a3a0e43f JA |
2406 | /* |
2407 | * Don't enter poll loop if we already have events pending. | |
2408 | * If we do, we can potentially be spinning for commands that | |
2409 | * already triggered a CQE (eg in error). | |
2410 | */ | |
6c503150 PB |
2411 | if (test_bit(0, &ctx->cq_check_overflow)) |
2412 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2413 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2414 | break; |
2415 | ||
500f9fba JA |
2416 | /* |
2417 | * If a submit got punted to a workqueue, we can have the | |
2418 | * application entering polling for a command before it gets | |
2419 | * issued. That app will hold the uring_lock for the duration | |
2420 | * of the poll right here, so we need to take a breather every | |
2421 | * now and then to ensure that the issue has a chance to add | |
2422 | * the poll to the issued list. Otherwise we can spin here | |
2423 | * forever, while the workqueue is stuck trying to acquire the | |
2424 | * very same mutex. | |
2425 | */ | |
2426 | if (!(++iters & 7)) { | |
2427 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2428 | io_run_task_work(); |
500f9fba JA |
2429 | mutex_lock(&ctx->uring_lock); |
2430 | } | |
2431 | ||
7668b92a | 2432 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2433 | if (ret <= 0) |
2434 | break; | |
2435 | ret = 0; | |
7668b92a | 2436 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2437 | |
500f9fba | 2438 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2439 | return ret; |
2440 | } | |
2441 | ||
491381ce | 2442 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2443 | { |
491381ce JA |
2444 | /* |
2445 | * Tell lockdep we inherited freeze protection from submission | |
2446 | * thread. | |
2447 | */ | |
2448 | if (req->flags & REQ_F_ISREG) { | |
2449 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2450 | |
491381ce | 2451 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2452 | } |
491381ce | 2453 | file_end_write(req->file); |
2b188cc1 JA |
2454 | } |
2455 | ||
b63534c4 | 2456 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2457 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2458 | { |
2459 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2460 | int rw, ret; |
b63534c4 | 2461 | struct iov_iter iter; |
b63534c4 | 2462 | |
dc2a6e9a PB |
2463 | /* already prepared */ |
2464 | if (req->async_data) | |
2465 | return true; | |
b63534c4 JA |
2466 | |
2467 | switch (req->opcode) { | |
2468 | case IORING_OP_READV: | |
2469 | case IORING_OP_READ_FIXED: | |
2470 | case IORING_OP_READ: | |
2471 | rw = READ; | |
2472 | break; | |
2473 | case IORING_OP_WRITEV: | |
2474 | case IORING_OP_WRITE_FIXED: | |
2475 | case IORING_OP_WRITE: | |
2476 | rw = WRITE; | |
2477 | break; | |
2478 | default: | |
2479 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2480 | req->opcode); | |
dc2a6e9a | 2481 | return false; |
b63534c4 JA |
2482 | } |
2483 | ||
dc2a6e9a PB |
2484 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2485 | if (ret < 0) | |
2486 | return false; | |
6bf985dc | 2487 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2488 | } |
b63534c4 JA |
2489 | #endif |
2490 | ||
23faba36 | 2491 | static bool io_rw_reissue(struct io_kiocb *req) |
b63534c4 JA |
2492 | { |
2493 | #ifdef CONFIG_BLOCK | |
355afaeb | 2494 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 | 2495 | |
355afaeb JA |
2496 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2497 | return false; | |
75c668cd | 2498 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
b63534c4 JA |
2499 | return false; |
2500 | ||
55e6ac1e PB |
2501 | lockdep_assert_held(&req->ctx->uring_lock); |
2502 | ||
37d1e2e3 | 2503 | if (io_resubmit_prep(req)) { |
fdee946d JA |
2504 | refcount_inc(&req->refs); |
2505 | io_queue_async_work(req); | |
b63534c4 | 2506 | return true; |
fdee946d | 2507 | } |
dc2a6e9a | 2508 | req_set_fail_links(req); |
b63534c4 JA |
2509 | #endif |
2510 | return false; | |
2511 | } | |
2512 | ||
a1d7c393 | 2513 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2514 | unsigned int issue_flags) |
a1d7c393 | 2515 | { |
2f8e45f1 PB |
2516 | int cflags = 0; |
2517 | ||
23faba36 PB |
2518 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2519 | return; | |
2f8e45f1 PB |
2520 | if (res != req->result) |
2521 | req_set_fail_links(req); | |
23faba36 | 2522 | |
2f8e45f1 PB |
2523 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2524 | kiocb_end_write(req); | |
2525 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2526 | cflags = io_put_rw_kbuf(req); | |
2527 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2528 | } |
2529 | ||
2530 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2531 | { | |
9adbd45d | 2532 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2533 | |
889fca73 | 2534 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2535 | } |
2536 | ||
def596e9 JA |
2537 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2538 | { | |
9adbd45d | 2539 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2540 | |
491381ce JA |
2541 | if (kiocb->ki_flags & IOCB_WRITE) |
2542 | kiocb_end_write(req); | |
def596e9 | 2543 | |
2d7d6792 | 2544 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2545 | req_set_fail_links(req); |
bbde017a XW |
2546 | |
2547 | WRITE_ONCE(req->result, res); | |
2548 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2549 | smp_wmb(); |
2550 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2551 | } |
2552 | ||
2553 | /* | |
2554 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2555 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2556 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2557 | * accessing the kiocb cookie. | |
2558 | */ | |
2e9dbe90 | 2559 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2560 | { |
2561 | struct io_ring_ctx *ctx = req->ctx; | |
2562 | ||
2563 | /* | |
2564 | * Track whether we have multiple files in our lists. This will impact | |
2565 | * how we do polling eventually, not spinning if we're on potentially | |
2566 | * different devices. | |
2567 | */ | |
540e32a0 | 2568 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2569 | ctx->poll_multi_file = false; |
2570 | } else if (!ctx->poll_multi_file) { | |
2571 | struct io_kiocb *list_req; | |
2572 | ||
540e32a0 | 2573 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2574 | inflight_entry); |
9adbd45d | 2575 | if (list_req->file != req->file) |
def596e9 JA |
2576 | ctx->poll_multi_file = true; |
2577 | } | |
2578 | ||
2579 | /* | |
2580 | * For fast devices, IO may have already completed. If it has, add | |
2581 | * it to the front so we find it first. | |
2582 | */ | |
65a6543d | 2583 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2584 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2585 | else |
d21ffe7e | 2586 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2587 | |
2e9dbe90 XW |
2588 | /* |
2589 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2590 | * task context or in io worker task context. If current task context is | |
2591 | * sq thread, we don't need to check whether should wake up sq thread. | |
2592 | */ | |
2593 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2594 | wq_has_sleeper(&ctx->sq_data->wait)) |
2595 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2596 | } |
2597 | ||
9f13c35b PB |
2598 | static inline void io_state_file_put(struct io_submit_state *state) |
2599 | { | |
02b23a9a PB |
2600 | if (state->file_refs) { |
2601 | fput_many(state->file, state->file_refs); | |
2602 | state->file_refs = 0; | |
2603 | } | |
9a56a232 JA |
2604 | } |
2605 | ||
2606 | /* | |
2607 | * Get as many references to a file as we have IOs left in this submission, | |
2608 | * assuming most submissions are for one file, or at least that each file | |
2609 | * has more than one submission. | |
2610 | */ | |
8da11c19 | 2611 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2612 | { |
2613 | if (!state) | |
2614 | return fget(fd); | |
2615 | ||
6e1271e6 | 2616 | if (state->file_refs) { |
9a56a232 | 2617 | if (state->fd == fd) { |
6e1271e6 | 2618 | state->file_refs--; |
9a56a232 JA |
2619 | return state->file; |
2620 | } | |
02b23a9a | 2621 | io_state_file_put(state); |
9a56a232 JA |
2622 | } |
2623 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2624 | if (unlikely(!state->file)) |
9a56a232 JA |
2625 | return NULL; |
2626 | ||
2627 | state->fd = fd; | |
6e1271e6 | 2628 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2629 | return state->file; |
2630 | } | |
2631 | ||
4503b767 JA |
2632 | static bool io_bdev_nowait(struct block_device *bdev) |
2633 | { | |
9ba0d0c8 | 2634 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2635 | } |
2636 | ||
2b188cc1 JA |
2637 | /* |
2638 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2639 | * any file. For now, just ensure that anything potentially problematic is done | |
2640 | * inline. | |
2641 | */ | |
af197f50 | 2642 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2643 | { |
2644 | umode_t mode = file_inode(file)->i_mode; | |
2645 | ||
4503b767 | 2646 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2647 | if (IS_ENABLED(CONFIG_BLOCK) && |
2648 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2649 | return true; |
2650 | return false; | |
2651 | } | |
2652 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2653 | return true; |
4503b767 | 2654 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2655 | if (IS_ENABLED(CONFIG_BLOCK) && |
2656 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2657 | file->f_op != &io_uring_fops) |
2658 | return true; | |
2659 | return false; | |
2660 | } | |
2b188cc1 | 2661 | |
c5b85625 JA |
2662 | /* any ->read/write should understand O_NONBLOCK */ |
2663 | if (file->f_flags & O_NONBLOCK) | |
2664 | return true; | |
2665 | ||
af197f50 JA |
2666 | if (!(file->f_mode & FMODE_NOWAIT)) |
2667 | return false; | |
2668 | ||
2669 | if (rw == READ) | |
2670 | return file->f_op->read_iter != NULL; | |
2671 | ||
2672 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2673 | } |
2674 | ||
a88fc400 | 2675 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2676 | { |
def596e9 | 2677 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2678 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2679 | struct file *file = req->file; |
09bb8394 JA |
2680 | unsigned ioprio; |
2681 | int ret; | |
2b188cc1 | 2682 | |
75c668cd | 2683 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2684 | req->flags |= REQ_F_ISREG; |
2685 | ||
2b188cc1 | 2686 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2687 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2688 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2689 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2690 | } |
2b188cc1 | 2691 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2692 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2693 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2694 | if (unlikely(ret)) | |
2695 | return ret; | |
2b188cc1 | 2696 | |
75c668cd PB |
2697 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2698 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2699 | req->flags |= REQ_F_NOWAIT; | |
2700 | ||
2b188cc1 JA |
2701 | ioprio = READ_ONCE(sqe->ioprio); |
2702 | if (ioprio) { | |
2703 | ret = ioprio_check_cap(ioprio); | |
2704 | if (ret) | |
09bb8394 | 2705 | return ret; |
2b188cc1 JA |
2706 | |
2707 | kiocb->ki_ioprio = ioprio; | |
2708 | } else | |
2709 | kiocb->ki_ioprio = get_current_ioprio(); | |
2710 | ||
def596e9 | 2711 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2712 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2713 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2714 | return -EOPNOTSUPP; |
2b188cc1 | 2715 | |
def596e9 JA |
2716 | kiocb->ki_flags |= IOCB_HIPRI; |
2717 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2718 | req->iopoll_completed = 0; |
def596e9 | 2719 | } else { |
09bb8394 JA |
2720 | if (kiocb->ki_flags & IOCB_HIPRI) |
2721 | return -EINVAL; | |
def596e9 JA |
2722 | kiocb->ki_complete = io_complete_rw; |
2723 | } | |
9adbd45d | 2724 | |
3529d8c2 JA |
2725 | req->rw.addr = READ_ONCE(sqe->addr); |
2726 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2727 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2728 | return 0; |
2b188cc1 JA |
2729 | } |
2730 | ||
2731 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2732 | { | |
2733 | switch (ret) { | |
2734 | case -EIOCBQUEUED: | |
2735 | break; | |
2736 | case -ERESTARTSYS: | |
2737 | case -ERESTARTNOINTR: | |
2738 | case -ERESTARTNOHAND: | |
2739 | case -ERESTART_RESTARTBLOCK: | |
2740 | /* | |
2741 | * We can't just restart the syscall, since previously | |
2742 | * submitted sqes may already be in progress. Just fail this | |
2743 | * IO with EINTR. | |
2744 | */ | |
2745 | ret = -EINTR; | |
df561f66 | 2746 | fallthrough; |
2b188cc1 JA |
2747 | default: |
2748 | kiocb->ki_complete(kiocb, ret, 0); | |
2749 | } | |
2750 | } | |
2751 | ||
a1d7c393 | 2752 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2753 | unsigned int issue_flags) |
ba816ad6 | 2754 | { |
ba04291e | 2755 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2756 | struct io_async_rw *io = req->async_data; |
ba04291e | 2757 | |
227c0c96 | 2758 | /* add previously done IO, if any */ |
e8c2bc1f | 2759 | if (io && io->bytes_done > 0) { |
227c0c96 | 2760 | if (ret < 0) |
e8c2bc1f | 2761 | ret = io->bytes_done; |
227c0c96 | 2762 | else |
e8c2bc1f | 2763 | ret += io->bytes_done; |
227c0c96 JA |
2764 | } |
2765 | ||
ba04291e JA |
2766 | if (req->flags & REQ_F_CUR_POS) |
2767 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2768 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2769 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2770 | else |
2771 | io_rw_done(kiocb, ret); | |
2772 | } | |
2773 | ||
847595de | 2774 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2775 | { |
9adbd45d JA |
2776 | struct io_ring_ctx *ctx = req->ctx; |
2777 | size_t len = req->rw.len; | |
edafccee | 2778 | struct io_mapped_ubuf *imu; |
4be1c615 | 2779 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2780 | size_t offset; |
2781 | u64 buf_addr; | |
2782 | ||
edafccee JA |
2783 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2784 | return -EFAULT; | |
edafccee JA |
2785 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2786 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2787 | buf_addr = req->rw.addr; |
edafccee JA |
2788 | |
2789 | /* overflow */ | |
2790 | if (buf_addr + len < buf_addr) | |
2791 | return -EFAULT; | |
2792 | /* not inside the mapped region */ | |
2793 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2794 | return -EFAULT; | |
2795 | ||
2796 | /* | |
2797 | * May not be a start of buffer, set size appropriately | |
2798 | * and advance us to the beginning. | |
2799 | */ | |
2800 | offset = buf_addr - imu->ubuf; | |
2801 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2802 | |
2803 | if (offset) { | |
2804 | /* | |
2805 | * Don't use iov_iter_advance() here, as it's really slow for | |
2806 | * using the latter parts of a big fixed buffer - it iterates | |
2807 | * over each segment manually. We can cheat a bit here, because | |
2808 | * we know that: | |
2809 | * | |
2810 | * 1) it's a BVEC iter, we set it up | |
2811 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2812 | * first and last bvec | |
2813 | * | |
2814 | * So just find our index, and adjust the iterator afterwards. | |
2815 | * If the offset is within the first bvec (or the whole first | |
2816 | * bvec, just use iov_iter_advance(). This makes it easier | |
2817 | * since we can just skip the first segment, which may not | |
2818 | * be PAGE_SIZE aligned. | |
2819 | */ | |
2820 | const struct bio_vec *bvec = imu->bvec; | |
2821 | ||
2822 | if (offset <= bvec->bv_len) { | |
2823 | iov_iter_advance(iter, offset); | |
2824 | } else { | |
2825 | unsigned long seg_skip; | |
2826 | ||
2827 | /* skip first vec */ | |
2828 | offset -= bvec->bv_len; | |
2829 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2830 | ||
2831 | iter->bvec = bvec + seg_skip; | |
2832 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2833 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2834 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2835 | } |
2836 | } | |
2837 | ||
847595de | 2838 | return 0; |
edafccee JA |
2839 | } |
2840 | ||
bcda7baa JA |
2841 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2842 | { | |
2843 | if (needs_lock) | |
2844 | mutex_unlock(&ctx->uring_lock); | |
2845 | } | |
2846 | ||
2847 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2848 | { | |
2849 | /* | |
2850 | * "Normal" inline submissions always hold the uring_lock, since we | |
2851 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2852 | * The only exception is when we've detached the request and issue it | |
2853 | * from an async worker thread, grab the lock for that case. | |
2854 | */ | |
2855 | if (needs_lock) | |
2856 | mutex_lock(&ctx->uring_lock); | |
2857 | } | |
2858 | ||
2859 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2860 | int bgid, struct io_buffer *kbuf, | |
2861 | bool needs_lock) | |
2862 | { | |
2863 | struct io_buffer *head; | |
2864 | ||
2865 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2866 | return kbuf; | |
2867 | ||
2868 | io_ring_submit_lock(req->ctx, needs_lock); | |
2869 | ||
2870 | lockdep_assert_held(&req->ctx->uring_lock); | |
2871 | ||
2872 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2873 | if (head) { | |
2874 | if (!list_empty(&head->list)) { | |
2875 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2876 | list); | |
2877 | list_del(&kbuf->list); | |
2878 | } else { | |
2879 | kbuf = head; | |
2880 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2881 | } | |
2882 | if (*len > kbuf->len) | |
2883 | *len = kbuf->len; | |
2884 | } else { | |
2885 | kbuf = ERR_PTR(-ENOBUFS); | |
2886 | } | |
2887 | ||
2888 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2889 | ||
2890 | return kbuf; | |
2891 | } | |
2892 | ||
4d954c25 JA |
2893 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2894 | bool needs_lock) | |
2895 | { | |
2896 | struct io_buffer *kbuf; | |
4f4eeba8 | 2897 | u16 bgid; |
4d954c25 JA |
2898 | |
2899 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2900 | bgid = req->buf_index; |
4d954c25 JA |
2901 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2902 | if (IS_ERR(kbuf)) | |
2903 | return kbuf; | |
2904 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2905 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2906 | return u64_to_user_ptr(kbuf->addr); | |
2907 | } | |
2908 | ||
2909 | #ifdef CONFIG_COMPAT | |
2910 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2911 | bool needs_lock) | |
2912 | { | |
2913 | struct compat_iovec __user *uiov; | |
2914 | compat_ssize_t clen; | |
2915 | void __user *buf; | |
2916 | ssize_t len; | |
2917 | ||
2918 | uiov = u64_to_user_ptr(req->rw.addr); | |
2919 | if (!access_ok(uiov, sizeof(*uiov))) | |
2920 | return -EFAULT; | |
2921 | if (__get_user(clen, &uiov->iov_len)) | |
2922 | return -EFAULT; | |
2923 | if (clen < 0) | |
2924 | return -EINVAL; | |
2925 | ||
2926 | len = clen; | |
2927 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2928 | if (IS_ERR(buf)) | |
2929 | return PTR_ERR(buf); | |
2930 | iov[0].iov_base = buf; | |
2931 | iov[0].iov_len = (compat_size_t) len; | |
2932 | return 0; | |
2933 | } | |
2934 | #endif | |
2935 | ||
2936 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2937 | bool needs_lock) | |
2938 | { | |
2939 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2940 | void __user *buf; | |
2941 | ssize_t len; | |
2942 | ||
2943 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2944 | return -EFAULT; | |
2945 | ||
2946 | len = iov[0].iov_len; | |
2947 | if (len < 0) | |
2948 | return -EINVAL; | |
2949 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2950 | if (IS_ERR(buf)) | |
2951 | return PTR_ERR(buf); | |
2952 | iov[0].iov_base = buf; | |
2953 | iov[0].iov_len = len; | |
2954 | return 0; | |
2955 | } | |
2956 | ||
2957 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2958 | bool needs_lock) | |
2959 | { | |
dddb3e26 JA |
2960 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2961 | struct io_buffer *kbuf; | |
2962 | ||
2963 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2964 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2965 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2966 | return 0; |
dddb3e26 | 2967 | } |
dd201662 | 2968 | if (req->rw.len != 1) |
4d954c25 JA |
2969 | return -EINVAL; |
2970 | ||
2971 | #ifdef CONFIG_COMPAT | |
2972 | if (req->ctx->compat) | |
2973 | return io_compat_import(req, iov, needs_lock); | |
2974 | #endif | |
2975 | ||
2976 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2977 | } | |
2978 | ||
847595de PB |
2979 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2980 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2981 | { |
9adbd45d JA |
2982 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2983 | size_t sqe_len = req->rw.len; | |
847595de | 2984 | u8 opcode = req->opcode; |
4d954c25 | 2985 | ssize_t ret; |
edafccee | 2986 | |
7d009165 | 2987 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2988 | *iovec = NULL; |
9adbd45d | 2989 | return io_import_fixed(req, rw, iter); |
edafccee | 2990 | } |
2b188cc1 | 2991 | |
bcda7baa | 2992 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2993 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2994 | return -EINVAL; |
2995 | ||
3a6820f2 | 2996 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2997 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2998 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2999 | if (IS_ERR(buf)) |
4d954c25 | 3000 | return PTR_ERR(buf); |
3f9d6441 | 3001 | req->rw.len = sqe_len; |
bcda7baa JA |
3002 | } |
3003 | ||
3a6820f2 JA |
3004 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3005 | *iovec = NULL; | |
10fc72e4 | 3006 | return ret; |
3a6820f2 JA |
3007 | } |
3008 | ||
4d954c25 JA |
3009 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3010 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3011 | if (!ret) |
3012 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3013 | *iovec = NULL; |
3014 | return ret; | |
3015 | } | |
3016 | ||
89cd35c5 CH |
3017 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3018 | req->ctx->compat); | |
2b188cc1 JA |
3019 | } |
3020 | ||
0fef9483 JA |
3021 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3022 | { | |
5b09e37e | 3023 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3024 | } |
3025 | ||
31b51510 | 3026 | /* |
32960613 JA |
3027 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3028 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3029 | */ |
4017eb91 | 3030 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3031 | { |
4017eb91 JA |
3032 | struct kiocb *kiocb = &req->rw.kiocb; |
3033 | struct file *file = req->file; | |
32960613 JA |
3034 | ssize_t ret = 0; |
3035 | ||
3036 | /* | |
3037 | * Don't support polled IO through this interface, and we can't | |
3038 | * support non-blocking either. For the latter, this just causes | |
3039 | * the kiocb to be handled from an async context. | |
3040 | */ | |
3041 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3042 | return -EOPNOTSUPP; | |
3043 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3044 | return -EAGAIN; | |
3045 | ||
3046 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3047 | struct iovec iovec; |
32960613 JA |
3048 | ssize_t nr; |
3049 | ||
311ae9e1 PB |
3050 | if (!iov_iter_is_bvec(iter)) { |
3051 | iovec = iov_iter_iovec(iter); | |
3052 | } else { | |
4017eb91 JA |
3053 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3054 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3055 | } |
3056 | ||
32960613 JA |
3057 | if (rw == READ) { |
3058 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3059 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3060 | } else { |
3061 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3062 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3063 | } |
3064 | ||
3065 | if (nr < 0) { | |
3066 | if (!ret) | |
3067 | ret = nr; | |
3068 | break; | |
3069 | } | |
3070 | ret += nr; | |
3071 | if (nr != iovec.iov_len) | |
3072 | break; | |
4017eb91 JA |
3073 | req->rw.len -= nr; |
3074 | req->rw.addr += nr; | |
32960613 JA |
3075 | iov_iter_advance(iter, nr); |
3076 | } | |
3077 | ||
3078 | return ret; | |
3079 | } | |
3080 | ||
ff6165b2 JA |
3081 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3082 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3083 | { |
e8c2bc1f | 3084 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3085 | |
ff6165b2 | 3086 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3087 | rw->free_iovec = iovec; |
227c0c96 | 3088 | rw->bytes_done = 0; |
ff6165b2 | 3089 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3090 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3091 | return; |
b64e3444 | 3092 | if (!iovec) { |
ff6165b2 JA |
3093 | unsigned iov_off = 0; |
3094 | ||
3095 | rw->iter.iov = rw->fast_iov; | |
3096 | if (iter->iov != fast_iov) { | |
3097 | iov_off = iter->iov - fast_iov; | |
3098 | rw->iter.iov += iov_off; | |
3099 | } | |
3100 | if (rw->fast_iov != fast_iov) | |
3101 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3102 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3103 | } else { |
3104 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3105 | } |
3106 | } | |
3107 | ||
e8c2bc1f | 3108 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3109 | { |
e8c2bc1f JA |
3110 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3111 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3112 | return req->async_data == NULL; | |
3d9932a8 XW |
3113 | } |
3114 | ||
e8c2bc1f | 3115 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3116 | { |
e8c2bc1f | 3117 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3118 | return 0; |
3d9932a8 | 3119 | |
e8c2bc1f | 3120 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3121 | } |
3122 | ||
ff6165b2 JA |
3123 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3124 | const struct iovec *fast_iov, | |
227c0c96 | 3125 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3126 | { |
e8c2bc1f | 3127 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3128 | return 0; |
e8c2bc1f | 3129 | if (!req->async_data) { |
6bf985dc PB |
3130 | if (__io_alloc_async_data(req)) { |
3131 | kfree(iovec); | |
5d204bcf | 3132 | return -ENOMEM; |
6bf985dc | 3133 | } |
b7bb4f7d | 3134 | |
ff6165b2 | 3135 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3136 | } |
b7bb4f7d | 3137 | return 0; |
f67676d1 JA |
3138 | } |
3139 | ||
73debe68 | 3140 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3141 | { |
e8c2bc1f | 3142 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3143 | struct iovec *iov = iorw->fast_iov; |
847595de | 3144 | int ret; |
c3e330a4 | 3145 | |
2846c481 | 3146 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3147 | if (unlikely(ret < 0)) |
3148 | return ret; | |
3149 | ||
ab0b196c PB |
3150 | iorw->bytes_done = 0; |
3151 | iorw->free_iovec = iov; | |
3152 | if (iov) | |
3153 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3154 | return 0; |
3155 | } | |
3156 | ||
73debe68 | 3157 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3158 | { |
3529d8c2 JA |
3159 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3160 | return -EBADF; | |
93642ef8 | 3161 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3162 | } |
3163 | ||
c1dd91d1 JA |
3164 | /* |
3165 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3166 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3167 | * This gets called when the page is unlocked, and we generally expect that to | |
3168 | * happen when the page IO is completed and the page is now uptodate. This will | |
3169 | * queue a task_work based retry of the operation, attempting to copy the data | |
3170 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3171 | * do a thread based blocking retry of the operation. That's the unexpected | |
3172 | * slow path. | |
3173 | */ | |
bcf5a063 JA |
3174 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3175 | int sync, void *arg) | |
3176 | { | |
3177 | struct wait_page_queue *wpq; | |
3178 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3179 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3180 | |
3181 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3182 | ||
cdc8fcb4 LT |
3183 | if (!wake_page_match(wpq, key)) |
3184 | return 0; | |
3185 | ||
c8d317aa | 3186 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3187 | list_del_init(&wait->entry); |
3188 | ||
bcf5a063 JA |
3189 | /* submit ref gets dropped, acquire a new one */ |
3190 | refcount_inc(&req->refs); | |
921b9054 | 3191 | io_req_task_queue(req); |
bcf5a063 JA |
3192 | return 1; |
3193 | } | |
3194 | ||
c1dd91d1 JA |
3195 | /* |
3196 | * This controls whether a given IO request should be armed for async page | |
3197 | * based retry. If we return false here, the request is handed to the async | |
3198 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3199 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3200 | * will either succeed because the page is now uptodate and unlocked, or it | |
3201 | * will register a callback when the page is unlocked at IO completion. Through | |
3202 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3203 | * That retry will attempt the buffered read again. The retry will generally | |
3204 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3205 | * async worker threads for a blocking retry. | |
3206 | */ | |
227c0c96 | 3207 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3208 | { |
e8c2bc1f JA |
3209 | struct io_async_rw *rw = req->async_data; |
3210 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3211 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3212 | |
bcf5a063 JA |
3213 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3214 | if (req->flags & REQ_F_NOWAIT) | |
3215 | return false; | |
f67676d1 | 3216 | |
227c0c96 | 3217 | /* Only for buffered IO */ |
3b2a4439 | 3218 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3219 | return false; |
3b2a4439 | 3220 | |
bcf5a063 JA |
3221 | /* |
3222 | * just use poll if we can, and don't attempt if the fs doesn't | |
3223 | * support callback based unlocks | |
3224 | */ | |
3225 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3226 | return false; | |
f67676d1 | 3227 | |
3b2a4439 JA |
3228 | wait->wait.func = io_async_buf_func; |
3229 | wait->wait.private = req; | |
3230 | wait->wait.flags = 0; | |
3231 | INIT_LIST_HEAD(&wait->wait.entry); | |
3232 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3233 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3234 | kiocb->ki_waitq = wait; |
3b2a4439 | 3235 | return true; |
bcf5a063 JA |
3236 | } |
3237 | ||
3238 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3239 | { | |
3240 | if (req->file->f_op->read_iter) | |
3241 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3242 | else if (req->file->f_op->read) |
4017eb91 | 3243 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3244 | else |
3245 | return -EINVAL; | |
f67676d1 JA |
3246 | } |
3247 | ||
889fca73 | 3248 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3249 | { |
3250 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3251 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3252 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3253 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3254 | ssize_t io_size, ret, ret2; |
45d189c6 | 3255 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3256 | |
2846c481 | 3257 | if (rw) { |
e8c2bc1f | 3258 | iter = &rw->iter; |
2846c481 PB |
3259 | iovec = NULL; |
3260 | } else { | |
3261 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3262 | if (ret < 0) | |
3263 | return ret; | |
3264 | } | |
632546c4 | 3265 | io_size = iov_iter_count(iter); |
fa15bafb | 3266 | req->result = io_size; |
2b188cc1 | 3267 | |
fd6c2e4c JA |
3268 | /* Ensure we clear previously set non-block flag */ |
3269 | if (!force_nonblock) | |
29de5f6a | 3270 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3271 | else |
3272 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3273 | ||
24c74678 | 3274 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3275 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3276 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3277 | return ret ?: -EAGAIN; |
6713e7a6 | 3278 | } |
9e645e11 | 3279 | |
632546c4 | 3280 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3281 | if (unlikely(ret)) { |
3282 | kfree(iovec); | |
3283 | return ret; | |
3284 | } | |
2b188cc1 | 3285 | |
227c0c96 | 3286 | ret = io_iter_do_read(req, iter); |
32960613 | 3287 | |
57cd657b | 3288 | if (ret == -EIOCBQUEUED) { |
fe1cdd55 | 3289 | goto out_free; |
227c0c96 | 3290 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3291 | /* IOPOLL retry should happen for io-wq threads */ |
3292 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3293 | goto done; |
75c668cd PB |
3294 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3295 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3296 | goto done; |
84216315 | 3297 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3298 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3299 | ret = 0; |
7335e3bf | 3300 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3301 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3302 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3303 | goto done; |
227c0c96 JA |
3304 | } |
3305 | ||
227c0c96 | 3306 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3307 | if (ret2) |
3308 | return ret2; | |
3309 | ||
fe1cdd55 | 3310 | iovec = NULL; |
e8c2bc1f | 3311 | rw = req->async_data; |
227c0c96 | 3312 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3313 | iter = &rw->iter; |
227c0c96 | 3314 | |
b23df91b PB |
3315 | do { |
3316 | io_size -= ret; | |
3317 | rw->bytes_done += ret; | |
3318 | /* if we can retry, do so with the callbacks armed */ | |
3319 | if (!io_rw_should_retry(req)) { | |
3320 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3321 | return -EAGAIN; | |
3322 | } | |
3323 | ||
3324 | /* | |
3325 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3326 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3327 | * desired page gets unlocked. We can also get a partial read | |
3328 | * here, and if we do, then just retry at the new offset. | |
3329 | */ | |
3330 | ret = io_iter_do_read(req, iter); | |
3331 | if (ret == -EIOCBQUEUED) | |
3332 | return 0; | |
227c0c96 | 3333 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3334 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3335 | done: |
889fca73 | 3336 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3337 | out_free: |
3338 | /* it's faster to check here then delegate to kfree */ | |
3339 | if (iovec) | |
3340 | kfree(iovec); | |
5ea5dd45 | 3341 | return 0; |
2b188cc1 JA |
3342 | } |
3343 | ||
73debe68 | 3344 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3345 | { |
3529d8c2 JA |
3346 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3347 | return -EBADF; | |
93642ef8 | 3348 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3349 | } |
3350 | ||
889fca73 | 3351 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3352 | { |
3353 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3354 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3355 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3356 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3357 | ssize_t ret, ret2, io_size; |
45d189c6 | 3358 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3359 | |
2846c481 | 3360 | if (rw) { |
e8c2bc1f | 3361 | iter = &rw->iter; |
2846c481 PB |
3362 | iovec = NULL; |
3363 | } else { | |
3364 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3365 | if (ret < 0) | |
3366 | return ret; | |
3367 | } | |
632546c4 | 3368 | io_size = iov_iter_count(iter); |
fa15bafb | 3369 | req->result = io_size; |
2b188cc1 | 3370 | |
fd6c2e4c JA |
3371 | /* Ensure we clear previously set non-block flag */ |
3372 | if (!force_nonblock) | |
a88fc400 PB |
3373 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3374 | else | |
3375 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3376 | |
24c74678 | 3377 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3378 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3379 | goto copy_iov; |
31b51510 | 3380 | |
10d59345 JA |
3381 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3382 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3383 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3384 | goto copy_iov; |
31b51510 | 3385 | |
632546c4 | 3386 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3387 | if (unlikely(ret)) |
3388 | goto out_free; | |
4ed734b0 | 3389 | |
fa15bafb PB |
3390 | /* |
3391 | * Open-code file_start_write here to grab freeze protection, | |
3392 | * which will be released by another thread in | |
3393 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3394 | * released so that it doesn't complain about the held lock when | |
3395 | * we return to userspace. | |
3396 | */ | |
3397 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3398 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3399 | __sb_writers_release(file_inode(req->file)->i_sb, |
3400 | SB_FREEZE_WRITE); | |
3401 | } | |
3402 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3403 | |
fa15bafb | 3404 | if (req->file->f_op->write_iter) |
ff6165b2 | 3405 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3406 | else if (req->file->f_op->write) |
4017eb91 | 3407 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3408 | else |
3409 | ret2 = -EINVAL; | |
4ed734b0 | 3410 | |
fa15bafb PB |
3411 | /* |
3412 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3413 | * retry them without IOCB_NOWAIT. | |
3414 | */ | |
3415 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3416 | ret2 = -EAGAIN; | |
75c668cd PB |
3417 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3418 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3419 | goto done; |
fa15bafb | 3420 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3421 | /* IOPOLL retry should happen for io-wq threads */ |
3422 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3423 | goto copy_iov; | |
355afaeb | 3424 | done: |
889fca73 | 3425 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3426 | } else { |
f67676d1 | 3427 | copy_iov: |
84216315 | 3428 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3429 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3430 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3431 | return ret ?: -EAGAIN; |
2b188cc1 | 3432 | } |
31b51510 | 3433 | out_free: |
f261c168 | 3434 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3435 | if (iovec) |
6f2cc166 | 3436 | kfree(iovec); |
2b188cc1 JA |
3437 | return ret; |
3438 | } | |
3439 | ||
80a261fd JA |
3440 | static int io_renameat_prep(struct io_kiocb *req, |
3441 | const struct io_uring_sqe *sqe) | |
3442 | { | |
3443 | struct io_rename *ren = &req->rename; | |
3444 | const char __user *oldf, *newf; | |
3445 | ||
3446 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3447 | return -EBADF; | |
3448 | ||
3449 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3450 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3451 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3452 | ren->new_dfd = READ_ONCE(sqe->len); | |
3453 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3454 | ||
3455 | ren->oldpath = getname(oldf); | |
3456 | if (IS_ERR(ren->oldpath)) | |
3457 | return PTR_ERR(ren->oldpath); | |
3458 | ||
3459 | ren->newpath = getname(newf); | |
3460 | if (IS_ERR(ren->newpath)) { | |
3461 | putname(ren->oldpath); | |
3462 | return PTR_ERR(ren->newpath); | |
3463 | } | |
3464 | ||
3465 | req->flags |= REQ_F_NEED_CLEANUP; | |
3466 | return 0; | |
3467 | } | |
3468 | ||
45d189c6 | 3469 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3470 | { |
3471 | struct io_rename *ren = &req->rename; | |
3472 | int ret; | |
3473 | ||
45d189c6 | 3474 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3475 | return -EAGAIN; |
3476 | ||
3477 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3478 | ren->newpath, ren->flags); | |
3479 | ||
3480 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3481 | if (ret < 0) | |
3482 | req_set_fail_links(req); | |
3483 | io_req_complete(req, ret); | |
3484 | return 0; | |
3485 | } | |
3486 | ||
14a1143b JA |
3487 | static int io_unlinkat_prep(struct io_kiocb *req, |
3488 | const struct io_uring_sqe *sqe) | |
3489 | { | |
3490 | struct io_unlink *un = &req->unlink; | |
3491 | const char __user *fname; | |
3492 | ||
3493 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3494 | return -EBADF; | |
3495 | ||
3496 | un->dfd = READ_ONCE(sqe->fd); | |
3497 | ||
3498 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3499 | if (un->flags & ~AT_REMOVEDIR) | |
3500 | return -EINVAL; | |
3501 | ||
3502 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3503 | un->filename = getname(fname); | |
3504 | if (IS_ERR(un->filename)) | |
3505 | return PTR_ERR(un->filename); | |
3506 | ||
3507 | req->flags |= REQ_F_NEED_CLEANUP; | |
3508 | return 0; | |
3509 | } | |
3510 | ||
45d189c6 | 3511 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3512 | { |
3513 | struct io_unlink *un = &req->unlink; | |
3514 | int ret; | |
3515 | ||
45d189c6 | 3516 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3517 | return -EAGAIN; |
3518 | ||
3519 | if (un->flags & AT_REMOVEDIR) | |
3520 | ret = do_rmdir(un->dfd, un->filename); | |
3521 | else | |
3522 | ret = do_unlinkat(un->dfd, un->filename); | |
3523 | ||
3524 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3525 | if (ret < 0) | |
3526 | req_set_fail_links(req); | |
3527 | io_req_complete(req, ret); | |
3528 | return 0; | |
3529 | } | |
3530 | ||
36f4fa68 JA |
3531 | static int io_shutdown_prep(struct io_kiocb *req, |
3532 | const struct io_uring_sqe *sqe) | |
3533 | { | |
3534 | #if defined(CONFIG_NET) | |
3535 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3536 | return -EINVAL; | |
3537 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3538 | sqe->buf_index) | |
3539 | return -EINVAL; | |
3540 | ||
3541 | req->shutdown.how = READ_ONCE(sqe->len); | |
3542 | return 0; | |
3543 | #else | |
3544 | return -EOPNOTSUPP; | |
3545 | #endif | |
3546 | } | |
3547 | ||
45d189c6 | 3548 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3549 | { |
3550 | #if defined(CONFIG_NET) | |
3551 | struct socket *sock; | |
3552 | int ret; | |
3553 | ||
45d189c6 | 3554 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3555 | return -EAGAIN; |
3556 | ||
48aba79b | 3557 | sock = sock_from_file(req->file); |
36f4fa68 | 3558 | if (unlikely(!sock)) |
48aba79b | 3559 | return -ENOTSOCK; |
36f4fa68 JA |
3560 | |
3561 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3562 | if (ret < 0) |
3563 | req_set_fail_links(req); | |
36f4fa68 JA |
3564 | io_req_complete(req, ret); |
3565 | return 0; | |
3566 | #else | |
3567 | return -EOPNOTSUPP; | |
3568 | #endif | |
3569 | } | |
3570 | ||
f2a8d5c7 PB |
3571 | static int __io_splice_prep(struct io_kiocb *req, |
3572 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3573 | { |
3574 | struct io_splice* sp = &req->splice; | |
3575 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3576 | |
3232dd02 PB |
3577 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3578 | return -EINVAL; | |
7d67af2c PB |
3579 | |
3580 | sp->file_in = NULL; | |
7d67af2c PB |
3581 | sp->len = READ_ONCE(sqe->len); |
3582 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3583 | ||
3584 | if (unlikely(sp->flags & ~valid_flags)) | |
3585 | return -EINVAL; | |
3586 | ||
8371adf5 PB |
3587 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3588 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3589 | if (!sp->file_in) | |
3590 | return -EBADF; | |
7d67af2c PB |
3591 | req->flags |= REQ_F_NEED_CLEANUP; |
3592 | ||
7cdaf587 XW |
3593 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3594 | /* | |
3595 | * Splice operation will be punted aync, and here need to | |
3596 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3597 | */ | |
3598 | io_req_init_async(req); | |
7d67af2c | 3599 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3600 | } |
7d67af2c PB |
3601 | |
3602 | return 0; | |
3603 | } | |
3604 | ||
f2a8d5c7 PB |
3605 | static int io_tee_prep(struct io_kiocb *req, |
3606 | const struct io_uring_sqe *sqe) | |
3607 | { | |
3608 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3609 | return -EINVAL; | |
3610 | return __io_splice_prep(req, sqe); | |
3611 | } | |
3612 | ||
45d189c6 | 3613 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3614 | { |
3615 | struct io_splice *sp = &req->splice; | |
3616 | struct file *in = sp->file_in; | |
3617 | struct file *out = sp->file_out; | |
3618 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3619 | long ret = 0; | |
3620 | ||
45d189c6 | 3621 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3622 | return -EAGAIN; |
3623 | if (sp->len) | |
3624 | ret = do_tee(in, out, sp->len, flags); | |
3625 | ||
3626 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3627 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3628 | ||
f2a8d5c7 PB |
3629 | if (ret != sp->len) |
3630 | req_set_fail_links(req); | |
e1e16097 | 3631 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3632 | return 0; |
3633 | } | |
3634 | ||
3635 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3636 | { | |
3637 | struct io_splice* sp = &req->splice; | |
3638 | ||
3639 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3640 | sp->off_out = READ_ONCE(sqe->off); | |
3641 | return __io_splice_prep(req, sqe); | |
3642 | } | |
3643 | ||
45d189c6 | 3644 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3645 | { |
3646 | struct io_splice *sp = &req->splice; | |
3647 | struct file *in = sp->file_in; | |
3648 | struct file *out = sp->file_out; | |
3649 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3650 | loff_t *poff_in, *poff_out; | |
c9687426 | 3651 | long ret = 0; |
7d67af2c | 3652 | |
45d189c6 | 3653 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3654 | return -EAGAIN; |
7d67af2c PB |
3655 | |
3656 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3657 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3658 | |
948a7749 | 3659 | if (sp->len) |
c9687426 | 3660 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3661 | |
3662 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3663 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3664 | ||
7d67af2c PB |
3665 | if (ret != sp->len) |
3666 | req_set_fail_links(req); | |
e1e16097 | 3667 | io_req_complete(req, ret); |
7d67af2c PB |
3668 | return 0; |
3669 | } | |
3670 | ||
2b188cc1 JA |
3671 | /* |
3672 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3673 | */ | |
889fca73 | 3674 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3675 | { |
3676 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3677 | |
def596e9 JA |
3678 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3679 | return -EINVAL; | |
3680 | ||
889fca73 | 3681 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3682 | return 0; |
3683 | } | |
3684 | ||
1155c76a | 3685 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3686 | { |
6b06314c | 3687 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3688 | |
09bb8394 JA |
3689 | if (!req->file) |
3690 | return -EBADF; | |
c992fe29 | 3691 | |
6b06314c | 3692 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3693 | return -EINVAL; |
edafccee | 3694 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3695 | return -EINVAL; |
3696 | ||
8ed8d3c3 JA |
3697 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3698 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3699 | return -EINVAL; | |
3700 | ||
3701 | req->sync.off = READ_ONCE(sqe->off); | |
3702 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3703 | return 0; |
3704 | } | |
3705 | ||
45d189c6 | 3706 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3707 | { |
8ed8d3c3 | 3708 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3709 | int ret; |
3710 | ||
ac45abc0 | 3711 | /* fsync always requires a blocking context */ |
45d189c6 | 3712 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3713 | return -EAGAIN; |
3714 | ||
9adbd45d | 3715 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3716 | end > 0 ? end : LLONG_MAX, |
3717 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3718 | if (ret < 0) | |
3719 | req_set_fail_links(req); | |
e1e16097 | 3720 | io_req_complete(req, ret); |
c992fe29 CH |
3721 | return 0; |
3722 | } | |
3723 | ||
d63d1b5e JA |
3724 | static int io_fallocate_prep(struct io_kiocb *req, |
3725 | const struct io_uring_sqe *sqe) | |
3726 | { | |
3727 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3728 | return -EINVAL; | |
3232dd02 PB |
3729 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3730 | return -EINVAL; | |
d63d1b5e JA |
3731 | |
3732 | req->sync.off = READ_ONCE(sqe->off); | |
3733 | req->sync.len = READ_ONCE(sqe->addr); | |
3734 | req->sync.mode = READ_ONCE(sqe->len); | |
3735 | return 0; | |
3736 | } | |
3737 | ||
45d189c6 | 3738 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3739 | { |
ac45abc0 PB |
3740 | int ret; |
3741 | ||
d63d1b5e | 3742 | /* fallocate always requiring blocking context */ |
45d189c6 | 3743 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3744 | return -EAGAIN; |
ac45abc0 PB |
3745 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3746 | req->sync.len); | |
ac45abc0 PB |
3747 | if (ret < 0) |
3748 | req_set_fail_links(req); | |
e1e16097 | 3749 | io_req_complete(req, ret); |
5d17b4a4 JA |
3750 | return 0; |
3751 | } | |
3752 | ||
ec65fea5 | 3753 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3754 | { |
f8748881 | 3755 | const char __user *fname; |
15b71abe | 3756 | int ret; |
b7bb4f7d | 3757 | |
ec65fea5 | 3758 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3759 | return -EINVAL; |
ec65fea5 | 3760 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3761 | return -EBADF; |
03b1230c | 3762 | |
ec65fea5 PB |
3763 | /* open.how should be already initialised */ |
3764 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3765 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3766 | |
25e72d10 PB |
3767 | req->open.dfd = READ_ONCE(sqe->fd); |
3768 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3769 | req->open.filename = getname(fname); |
15b71abe JA |
3770 | if (IS_ERR(req->open.filename)) { |
3771 | ret = PTR_ERR(req->open.filename); | |
3772 | req->open.filename = NULL; | |
3773 | return ret; | |
3774 | } | |
4022e7af | 3775 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3776 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3777 | return 0; |
03b1230c JA |
3778 | } |
3779 | ||
ec65fea5 PB |
3780 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3781 | { | |
3782 | u64 flags, mode; | |
3783 | ||
14587a46 | 3784 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3785 | return -EINVAL; |
ec65fea5 PB |
3786 | mode = READ_ONCE(sqe->len); |
3787 | flags = READ_ONCE(sqe->open_flags); | |
3788 | req->open.how = build_open_how(flags, mode); | |
3789 | return __io_openat_prep(req, sqe); | |
3790 | } | |
3791 | ||
cebdb986 | 3792 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3793 | { |
cebdb986 | 3794 | struct open_how __user *how; |
cebdb986 | 3795 | size_t len; |
0fa03c62 JA |
3796 | int ret; |
3797 | ||
14587a46 | 3798 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3799 | return -EINVAL; |
cebdb986 JA |
3800 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3801 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3802 | if (len < OPEN_HOW_SIZE_VER0) |
3803 | return -EINVAL; | |
3529d8c2 | 3804 | |
cebdb986 JA |
3805 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3806 | len); | |
3807 | if (ret) | |
3808 | return ret; | |
3529d8c2 | 3809 | |
ec65fea5 | 3810 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3811 | } |
3812 | ||
45d189c6 | 3813 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3814 | { |
3815 | struct open_flags op; | |
15b71abe | 3816 | struct file *file; |
3a81fd02 JA |
3817 | bool nonblock_set; |
3818 | bool resolve_nonblock; | |
15b71abe JA |
3819 | int ret; |
3820 | ||
cebdb986 | 3821 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3822 | if (ret) |
3823 | goto err; | |
3a81fd02 JA |
3824 | nonblock_set = op.open_flag & O_NONBLOCK; |
3825 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3826 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3827 | /* |
3828 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3829 | * it'll always -EAGAIN | |
3830 | */ | |
3831 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3832 | return -EAGAIN; | |
3833 | op.lookup_flags |= LOOKUP_CACHED; | |
3834 | op.open_flag |= O_NONBLOCK; | |
3835 | } | |
15b71abe | 3836 | |
4022e7af | 3837 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3838 | if (ret < 0) |
3839 | goto err; | |
3840 | ||
3841 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3842 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3843 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3844 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3845 | /* |
3a81fd02 JA |
3846 | * We could hang on to this 'fd', but seems like marginal |
3847 | * gain for something that is now known to be a slower path. | |
3848 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3849 | */ |
3a81fd02 JA |
3850 | put_unused_fd(ret); |
3851 | return -EAGAIN; | |
3852 | } | |
3853 | ||
15b71abe JA |
3854 | if (IS_ERR(file)) { |
3855 | put_unused_fd(ret); | |
3856 | ret = PTR_ERR(file); | |
3857 | } else { | |
45d189c6 | 3858 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3859 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3860 | fsnotify_open(file); |
3861 | fd_install(ret, file); | |
3862 | } | |
3863 | err: | |
3864 | putname(req->open.filename); | |
8fef80bf | 3865 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3866 | if (ret < 0) |
3867 | req_set_fail_links(req); | |
e1e16097 | 3868 | io_req_complete(req, ret); |
15b71abe JA |
3869 | return 0; |
3870 | } | |
3871 | ||
45d189c6 | 3872 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3873 | { |
45d189c6 | 3874 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
3875 | } |
3876 | ||
067524e9 JA |
3877 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3878 | const struct io_uring_sqe *sqe) | |
3879 | { | |
3880 | struct io_provide_buf *p = &req->pbuf; | |
3881 | u64 tmp; | |
3882 | ||
3883 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3884 | return -EINVAL; | |
3885 | ||
3886 | tmp = READ_ONCE(sqe->fd); | |
3887 | if (!tmp || tmp > USHRT_MAX) | |
3888 | return -EINVAL; | |
3889 | ||
3890 | memset(p, 0, sizeof(*p)); | |
3891 | p->nbufs = tmp; | |
3892 | p->bgid = READ_ONCE(sqe->buf_group); | |
3893 | return 0; | |
3894 | } | |
3895 | ||
3896 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3897 | int bgid, unsigned nbufs) | |
3898 | { | |
3899 | unsigned i = 0; | |
3900 | ||
3901 | /* shouldn't happen */ | |
3902 | if (!nbufs) | |
3903 | return 0; | |
3904 | ||
3905 | /* the head kbuf is the list itself */ | |
3906 | while (!list_empty(&buf->list)) { | |
3907 | struct io_buffer *nxt; | |
3908 | ||
3909 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3910 | list_del(&nxt->list); | |
3911 | kfree(nxt); | |
3912 | if (++i == nbufs) | |
3913 | return i; | |
3914 | } | |
3915 | i++; | |
3916 | kfree(buf); | |
3917 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3918 | ||
3919 | return i; | |
3920 | } | |
3921 | ||
889fca73 | 3922 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3923 | { |
3924 | struct io_provide_buf *p = &req->pbuf; | |
3925 | struct io_ring_ctx *ctx = req->ctx; | |
3926 | struct io_buffer *head; | |
3927 | int ret = 0; | |
45d189c6 | 3928 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3929 | |
3930 | io_ring_submit_lock(ctx, !force_nonblock); | |
3931 | ||
3932 | lockdep_assert_held(&ctx->uring_lock); | |
3933 | ||
3934 | ret = -ENOENT; | |
3935 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3936 | if (head) | |
3937 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3938 | if (ret < 0) |
3939 | req_set_fail_links(req); | |
067524e9 | 3940 | |
31bff9a5 PB |
3941 | /* need to hold the lock to complete IOPOLL requests */ |
3942 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 3943 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
3944 | io_ring_submit_unlock(ctx, !force_nonblock); |
3945 | } else { | |
3946 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 3947 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 3948 | } |
067524e9 JA |
3949 | return 0; |
3950 | } | |
3951 | ||
ddf0322d JA |
3952 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3953 | const struct io_uring_sqe *sqe) | |
3954 | { | |
3955 | struct io_provide_buf *p = &req->pbuf; | |
3956 | u64 tmp; | |
3957 | ||
3958 | if (sqe->ioprio || sqe->rw_flags) | |
3959 | return -EINVAL; | |
3960 | ||
3961 | tmp = READ_ONCE(sqe->fd); | |
3962 | if (!tmp || tmp > USHRT_MAX) | |
3963 | return -E2BIG; | |
3964 | p->nbufs = tmp; | |
3965 | p->addr = READ_ONCE(sqe->addr); | |
3966 | p->len = READ_ONCE(sqe->len); | |
3967 | ||
efe68c1c | 3968 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3969 | return -EFAULT; |
3970 | ||
3971 | p->bgid = READ_ONCE(sqe->buf_group); | |
3972 | tmp = READ_ONCE(sqe->off); | |
3973 | if (tmp > USHRT_MAX) | |
3974 | return -E2BIG; | |
3975 | p->bid = tmp; | |
3976 | return 0; | |
3977 | } | |
3978 | ||
3979 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3980 | { | |
3981 | struct io_buffer *buf; | |
3982 | u64 addr = pbuf->addr; | |
3983 | int i, bid = pbuf->bid; | |
3984 | ||
3985 | for (i = 0; i < pbuf->nbufs; i++) { | |
3986 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3987 | if (!buf) | |
3988 | break; | |
3989 | ||
3990 | buf->addr = addr; | |
3991 | buf->len = pbuf->len; | |
3992 | buf->bid = bid; | |
3993 | addr += pbuf->len; | |
3994 | bid++; | |
3995 | if (!*head) { | |
3996 | INIT_LIST_HEAD(&buf->list); | |
3997 | *head = buf; | |
3998 | } else { | |
3999 | list_add_tail(&buf->list, &(*head)->list); | |
4000 | } | |
4001 | } | |
4002 | ||
4003 | return i ? i : -ENOMEM; | |
4004 | } | |
4005 | ||
889fca73 | 4006 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4007 | { |
4008 | struct io_provide_buf *p = &req->pbuf; | |
4009 | struct io_ring_ctx *ctx = req->ctx; | |
4010 | struct io_buffer *head, *list; | |
4011 | int ret = 0; | |
45d189c6 | 4012 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4013 | |
4014 | io_ring_submit_lock(ctx, !force_nonblock); | |
4015 | ||
4016 | lockdep_assert_held(&ctx->uring_lock); | |
4017 | ||
4018 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4019 | ||
4020 | ret = io_add_buffers(p, &head); | |
4021 | if (ret < 0) | |
4022 | goto out; | |
4023 | ||
4024 | if (!list) { | |
4025 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4026 | GFP_KERNEL); | |
4027 | if (ret < 0) { | |
067524e9 | 4028 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4029 | goto out; |
4030 | } | |
4031 | } | |
4032 | out: | |
ddf0322d JA |
4033 | if (ret < 0) |
4034 | req_set_fail_links(req); | |
31bff9a5 PB |
4035 | |
4036 | /* need to hold the lock to complete IOPOLL requests */ | |
4037 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4038 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4039 | io_ring_submit_unlock(ctx, !force_nonblock); |
4040 | } else { | |
4041 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4042 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4043 | } |
ddf0322d | 4044 | return 0; |
cebdb986 JA |
4045 | } |
4046 | ||
3e4827b0 JA |
4047 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4048 | const struct io_uring_sqe *sqe) | |
4049 | { | |
4050 | #if defined(CONFIG_EPOLL) | |
4051 | if (sqe->ioprio || sqe->buf_index) | |
4052 | return -EINVAL; | |
6ca56f84 | 4053 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4054 | return -EINVAL; |
3e4827b0 JA |
4055 | |
4056 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4057 | req->epoll.op = READ_ONCE(sqe->len); | |
4058 | req->epoll.fd = READ_ONCE(sqe->off); | |
4059 | ||
4060 | if (ep_op_has_event(req->epoll.op)) { | |
4061 | struct epoll_event __user *ev; | |
4062 | ||
4063 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4064 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4065 | return -EFAULT; | |
4066 | } | |
4067 | ||
4068 | return 0; | |
4069 | #else | |
4070 | return -EOPNOTSUPP; | |
4071 | #endif | |
4072 | } | |
4073 | ||
889fca73 | 4074 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4075 | { |
4076 | #if defined(CONFIG_EPOLL) | |
4077 | struct io_epoll *ie = &req->epoll; | |
4078 | int ret; | |
45d189c6 | 4079 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4080 | |
4081 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4082 | if (force_nonblock && ret == -EAGAIN) | |
4083 | return -EAGAIN; | |
4084 | ||
4085 | if (ret < 0) | |
4086 | req_set_fail_links(req); | |
889fca73 | 4087 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4088 | return 0; |
4089 | #else | |
4090 | return -EOPNOTSUPP; | |
4091 | #endif | |
4092 | } | |
4093 | ||
c1ca757b JA |
4094 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4095 | { | |
4096 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4097 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4098 | return -EINVAL; | |
3232dd02 PB |
4099 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4100 | return -EINVAL; | |
c1ca757b JA |
4101 | |
4102 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4103 | req->madvise.len = READ_ONCE(sqe->len); | |
4104 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4105 | return 0; | |
4106 | #else | |
4107 | return -EOPNOTSUPP; | |
4108 | #endif | |
4109 | } | |
4110 | ||
45d189c6 | 4111 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4112 | { |
4113 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4114 | struct io_madvise *ma = &req->madvise; | |
4115 | int ret; | |
4116 | ||
45d189c6 | 4117 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4118 | return -EAGAIN; |
4119 | ||
0726b01e | 4120 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4121 | if (ret < 0) |
4122 | req_set_fail_links(req); | |
e1e16097 | 4123 | io_req_complete(req, ret); |
c1ca757b JA |
4124 | return 0; |
4125 | #else | |
4126 | return -EOPNOTSUPP; | |
4127 | #endif | |
4128 | } | |
4129 | ||
4840e418 JA |
4130 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4131 | { | |
4132 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4133 | return -EINVAL; | |
3232dd02 PB |
4134 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4135 | return -EINVAL; | |
4840e418 JA |
4136 | |
4137 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4138 | req->fadvise.len = READ_ONCE(sqe->len); | |
4139 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4140 | return 0; | |
4141 | } | |
4142 | ||
45d189c6 | 4143 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4144 | { |
4145 | struct io_fadvise *fa = &req->fadvise; | |
4146 | int ret; | |
4147 | ||
45d189c6 | 4148 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4149 | switch (fa->advice) { |
4150 | case POSIX_FADV_NORMAL: | |
4151 | case POSIX_FADV_RANDOM: | |
4152 | case POSIX_FADV_SEQUENTIAL: | |
4153 | break; | |
4154 | default: | |
4155 | return -EAGAIN; | |
4156 | } | |
4157 | } | |
4840e418 JA |
4158 | |
4159 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4160 | if (ret < 0) | |
4161 | req_set_fail_links(req); | |
e1e16097 | 4162 | io_req_complete(req, ret); |
4840e418 JA |
4163 | return 0; |
4164 | } | |
4165 | ||
eddc7ef5 JA |
4166 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4167 | { | |
6ca56f84 | 4168 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4169 | return -EINVAL; |
eddc7ef5 JA |
4170 | if (sqe->ioprio || sqe->buf_index) |
4171 | return -EINVAL; | |
9c280f90 | 4172 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4173 | return -EBADF; |
eddc7ef5 | 4174 | |
1d9e1288 BM |
4175 | req->statx.dfd = READ_ONCE(sqe->fd); |
4176 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4177 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4178 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4179 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4180 | |
4181 | return 0; | |
4182 | } | |
4183 | ||
45d189c6 | 4184 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4185 | { |
1d9e1288 | 4186 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4187 | int ret; |
4188 | ||
45d189c6 | 4189 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4190 | /* only need file table for an actual valid fd */ |
4191 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4192 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4193 | return -EAGAIN; |
5b0bbee4 | 4194 | } |
eddc7ef5 | 4195 | |
e62753e4 BM |
4196 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4197 | ctx->buffer); | |
eddc7ef5 | 4198 | |
eddc7ef5 JA |
4199 | if (ret < 0) |
4200 | req_set_fail_links(req); | |
e1e16097 | 4201 | io_req_complete(req, ret); |
eddc7ef5 JA |
4202 | return 0; |
4203 | } | |
4204 | ||
b5dba59e JA |
4205 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4206 | { | |
14587a46 | 4207 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4208 | return -EINVAL; |
b5dba59e JA |
4209 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4210 | sqe->rw_flags || sqe->buf_index) | |
4211 | return -EINVAL; | |
9c280f90 | 4212 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4213 | return -EBADF; |
b5dba59e JA |
4214 | |
4215 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4216 | return 0; |
b5dba59e JA |
4217 | } |
4218 | ||
889fca73 | 4219 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4220 | { |
9eac1904 | 4221 | struct files_struct *files = current->files; |
3af73b28 | 4222 | struct io_close *close = &req->close; |
9eac1904 JA |
4223 | struct fdtable *fdt; |
4224 | struct file *file; | |
b5dba59e JA |
4225 | int ret; |
4226 | ||
9eac1904 JA |
4227 | file = NULL; |
4228 | ret = -EBADF; | |
4229 | spin_lock(&files->file_lock); | |
4230 | fdt = files_fdtable(files); | |
4231 | if (close->fd >= fdt->max_fds) { | |
4232 | spin_unlock(&files->file_lock); | |
4233 | goto err; | |
4234 | } | |
4235 | file = fdt->fd[close->fd]; | |
4236 | if (!file) { | |
4237 | spin_unlock(&files->file_lock); | |
4238 | goto err; | |
4239 | } | |
4240 | ||
4241 | if (file->f_op == &io_uring_fops) { | |
4242 | spin_unlock(&files->file_lock); | |
4243 | file = NULL; | |
4244 | goto err; | |
3af73b28 | 4245 | } |
b5dba59e JA |
4246 | |
4247 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4248 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4249 | spin_unlock(&files->file_lock); |
0bf0eefd | 4250 | return -EAGAIN; |
a2100672 | 4251 | } |
b5dba59e | 4252 | |
9eac1904 JA |
4253 | ret = __close_fd_get_file(close->fd, &file); |
4254 | spin_unlock(&files->file_lock); | |
4255 | if (ret < 0) { | |
4256 | if (ret == -ENOENT) | |
4257 | ret = -EBADF; | |
4258 | goto err; | |
4259 | } | |
4260 | ||
3af73b28 | 4261 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4262 | ret = filp_close(file, current->files); |
4263 | err: | |
3af73b28 PB |
4264 | if (ret < 0) |
4265 | req_set_fail_links(req); | |
9eac1904 JA |
4266 | if (file) |
4267 | fput(file); | |
889fca73 | 4268 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4269 | return 0; |
b5dba59e JA |
4270 | } |
4271 | ||
1155c76a | 4272 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4273 | { |
4274 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4275 | |
5d17b4a4 JA |
4276 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4277 | return -EINVAL; | |
4278 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4279 | return -EINVAL; | |
4280 | ||
8ed8d3c3 JA |
4281 | req->sync.off = READ_ONCE(sqe->off); |
4282 | req->sync.len = READ_ONCE(sqe->len); | |
4283 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4284 | return 0; |
4285 | } | |
4286 | ||
45d189c6 | 4287 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4288 | { |
8ed8d3c3 JA |
4289 | int ret; |
4290 | ||
ac45abc0 | 4291 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4292 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4293 | return -EAGAIN; |
4294 | ||
9adbd45d | 4295 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4296 | req->sync.flags); |
4297 | if (ret < 0) | |
4298 | req_set_fail_links(req); | |
e1e16097 | 4299 | io_req_complete(req, ret); |
5d17b4a4 JA |
4300 | return 0; |
4301 | } | |
4302 | ||
469956e8 | 4303 | #if defined(CONFIG_NET) |
02d27d89 PB |
4304 | static int io_setup_async_msg(struct io_kiocb *req, |
4305 | struct io_async_msghdr *kmsg) | |
4306 | { | |
e8c2bc1f JA |
4307 | struct io_async_msghdr *async_msg = req->async_data; |
4308 | ||
4309 | if (async_msg) | |
02d27d89 | 4310 | return -EAGAIN; |
e8c2bc1f | 4311 | if (io_alloc_async_data(req)) { |
257e84a5 | 4312 | kfree(kmsg->free_iov); |
02d27d89 PB |
4313 | return -ENOMEM; |
4314 | } | |
e8c2bc1f | 4315 | async_msg = req->async_data; |
02d27d89 | 4316 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4317 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4318 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4319 | /* if were using fast_iov, set it to the new one */ |
4320 | if (!async_msg->free_iov) | |
4321 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4322 | ||
02d27d89 PB |
4323 | return -EAGAIN; |
4324 | } | |
4325 | ||
2ae523ed PB |
4326 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4327 | struct io_async_msghdr *iomsg) | |
4328 | { | |
2ae523ed | 4329 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4330 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4331 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4332 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4333 | } |
4334 | ||
93642ef8 PB |
4335 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4336 | { | |
4337 | int ret; | |
4338 | ||
4339 | if (!io_op_defs[req->opcode].needs_async_data) | |
4340 | return 0; | |
4341 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4342 | if (!ret) | |
4343 | req->flags |= REQ_F_NEED_CLEANUP; | |
4344 | return ret; | |
4345 | } | |
4346 | ||
3529d8c2 | 4347 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4348 | { |
e47293fd | 4349 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4350 | |
d2b6f48b PB |
4351 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4352 | return -EINVAL; | |
4353 | ||
e47293fd | 4354 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4355 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4356 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4357 | |
d8768362 JA |
4358 | #ifdef CONFIG_COMPAT |
4359 | if (req->ctx->compat) | |
4360 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4361 | #endif | |
93642ef8 | 4362 | return 0; |
03b1230c JA |
4363 | } |
4364 | ||
889fca73 | 4365 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4366 | { |
6b754c8b | 4367 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4368 | struct socket *sock; |
7a7cacba | 4369 | unsigned flags; |
0fa03c62 JA |
4370 | int ret; |
4371 | ||
dba4a925 | 4372 | sock = sock_from_file(req->file); |
7a7cacba | 4373 | if (unlikely(!sock)) |
dba4a925 | 4374 | return -ENOTSOCK; |
3529d8c2 | 4375 | |
257e84a5 PB |
4376 | kmsg = req->async_data; |
4377 | if (!kmsg) { | |
7a7cacba PB |
4378 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4379 | if (ret) | |
4380 | return ret; | |
4381 | kmsg = &iomsg; | |
0fa03c62 | 4382 | } |
0fa03c62 | 4383 | |
7a7cacba PB |
4384 | flags = req->sr_msg.msg_flags; |
4385 | if (flags & MSG_DONTWAIT) | |
4386 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4387 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4388 | flags |= MSG_DONTWAIT; |
e47293fd | 4389 | |
7a7cacba | 4390 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4391 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4392 | return io_setup_async_msg(req, kmsg); |
4393 | if (ret == -ERESTARTSYS) | |
4394 | ret = -EINTR; | |
0fa03c62 | 4395 | |
257e84a5 PB |
4396 | /* fast path, check for non-NULL to avoid function call */ |
4397 | if (kmsg->free_iov) | |
4398 | kfree(kmsg->free_iov); | |
99bc4c38 | 4399 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4400 | if (ret < 0) |
4401 | req_set_fail_links(req); | |
889fca73 | 4402 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4403 | return 0; |
03b1230c | 4404 | } |
aa1fa28f | 4405 | |
889fca73 | 4406 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4407 | { |
7a7cacba PB |
4408 | struct io_sr_msg *sr = &req->sr_msg; |
4409 | struct msghdr msg; | |
4410 | struct iovec iov; | |
fddaface | 4411 | struct socket *sock; |
7a7cacba | 4412 | unsigned flags; |
fddaface JA |
4413 | int ret; |
4414 | ||
dba4a925 | 4415 | sock = sock_from_file(req->file); |
7a7cacba | 4416 | if (unlikely(!sock)) |
dba4a925 | 4417 | return -ENOTSOCK; |
fddaface | 4418 | |
7a7cacba PB |
4419 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4420 | if (unlikely(ret)) | |
14db8411 | 4421 | return ret; |
fddaface | 4422 | |
7a7cacba PB |
4423 | msg.msg_name = NULL; |
4424 | msg.msg_control = NULL; | |
4425 | msg.msg_controllen = 0; | |
4426 | msg.msg_namelen = 0; | |
fddaface | 4427 | |
7a7cacba PB |
4428 | flags = req->sr_msg.msg_flags; |
4429 | if (flags & MSG_DONTWAIT) | |
4430 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4431 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4432 | flags |= MSG_DONTWAIT; |
fddaface | 4433 | |
7a7cacba PB |
4434 | msg.msg_flags = flags; |
4435 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4436 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4437 | return -EAGAIN; |
4438 | if (ret == -ERESTARTSYS) | |
4439 | ret = -EINTR; | |
fddaface | 4440 | |
fddaface JA |
4441 | if (ret < 0) |
4442 | req_set_fail_links(req); | |
889fca73 | 4443 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4444 | return 0; |
fddaface JA |
4445 | } |
4446 | ||
1400e697 PB |
4447 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4448 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4449 | { |
4450 | struct io_sr_msg *sr = &req->sr_msg; | |
4451 | struct iovec __user *uiov; | |
4452 | size_t iov_len; | |
4453 | int ret; | |
4454 | ||
1400e697 PB |
4455 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4456 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4457 | if (ret) |
4458 | return ret; | |
4459 | ||
4460 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4461 | if (iov_len > 1) | |
4462 | return -EINVAL; | |
5476dfed | 4463 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4464 | return -EFAULT; |
5476dfed | 4465 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4466 | iomsg->free_iov = NULL; |
52de1fe1 | 4467 | } else { |
257e84a5 | 4468 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4469 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4470 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4471 | false); |
52de1fe1 JA |
4472 | if (ret > 0) |
4473 | ret = 0; | |
4474 | } | |
4475 | ||
4476 | return ret; | |
4477 | } | |
4478 | ||
4479 | #ifdef CONFIG_COMPAT | |
4480 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4481 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4482 | { |
4483 | struct compat_msghdr __user *msg_compat; | |
4484 | struct io_sr_msg *sr = &req->sr_msg; | |
4485 | struct compat_iovec __user *uiov; | |
4486 | compat_uptr_t ptr; | |
4487 | compat_size_t len; | |
4488 | int ret; | |
4489 | ||
270a5940 | 4490 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4491 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4492 | &ptr, &len); |
4493 | if (ret) | |
4494 | return ret; | |
4495 | ||
4496 | uiov = compat_ptr(ptr); | |
4497 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4498 | compat_ssize_t clen; | |
4499 | ||
4500 | if (len > 1) | |
4501 | return -EINVAL; | |
4502 | if (!access_ok(uiov, sizeof(*uiov))) | |
4503 | return -EFAULT; | |
4504 | if (__get_user(clen, &uiov->iov_len)) | |
4505 | return -EFAULT; | |
4506 | if (clen < 0) | |
4507 | return -EINVAL; | |
2d280bc8 | 4508 | sr->len = clen; |
257e84a5 | 4509 | iomsg->free_iov = NULL; |
52de1fe1 | 4510 | } else { |
257e84a5 | 4511 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4512 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4513 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4514 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4515 | if (ret < 0) |
4516 | return ret; | |
4517 | } | |
4518 | ||
4519 | return 0; | |
4520 | } | |
4521 | #endif | |
4522 | ||
1400e697 PB |
4523 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4524 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4525 | { |
1400e697 | 4526 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4527 | |
4528 | #ifdef CONFIG_COMPAT | |
4529 | if (req->ctx->compat) | |
1400e697 | 4530 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4531 | #endif |
52de1fe1 | 4532 | |
1400e697 | 4533 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4534 | } |
4535 | ||
bcda7baa | 4536 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4537 | bool needs_lock) |
bcda7baa JA |
4538 | { |
4539 | struct io_sr_msg *sr = &req->sr_msg; | |
4540 | struct io_buffer *kbuf; | |
4541 | ||
bcda7baa JA |
4542 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4543 | if (IS_ERR(kbuf)) | |
4544 | return kbuf; | |
4545 | ||
4546 | sr->kbuf = kbuf; | |
4547 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4548 | return kbuf; |
fddaface JA |
4549 | } |
4550 | ||
7fbb1b54 PB |
4551 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4552 | { | |
4553 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4554 | } | |
4555 | ||
93642ef8 | 4556 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4557 | { |
99bc4c38 | 4558 | int ret; |
3529d8c2 | 4559 | |
93642ef8 PB |
4560 | if (!io_op_defs[req->opcode].needs_async_data) |
4561 | return 0; | |
4562 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4563 | if (!ret) | |
4564 | req->flags |= REQ_F_NEED_CLEANUP; | |
4565 | return ret; | |
4566 | } | |
4567 | ||
4568 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4569 | { | |
4570 | struct io_sr_msg *sr = &req->sr_msg; | |
4571 | ||
d2b6f48b PB |
4572 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4573 | return -EINVAL; | |
4574 | ||
3529d8c2 | 4575 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4576 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4577 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4578 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4579 | |
d8768362 JA |
4580 | #ifdef CONFIG_COMPAT |
4581 | if (req->ctx->compat) | |
4582 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4583 | #endif | |
93642ef8 | 4584 | return 0; |
aa1fa28f JA |
4585 | } |
4586 | ||
889fca73 | 4587 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4588 | { |
6b754c8b | 4589 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4590 | struct socket *sock; |
7fbb1b54 | 4591 | struct io_buffer *kbuf; |
7a7cacba | 4592 | unsigned flags; |
52de1fe1 | 4593 | int ret, cflags = 0; |
45d189c6 | 4594 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4595 | |
dba4a925 | 4596 | sock = sock_from_file(req->file); |
7a7cacba | 4597 | if (unlikely(!sock)) |
dba4a925 | 4598 | return -ENOTSOCK; |
3529d8c2 | 4599 | |
257e84a5 PB |
4600 | kmsg = req->async_data; |
4601 | if (!kmsg) { | |
7a7cacba PB |
4602 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4603 | if (ret) | |
681fda8d | 4604 | return ret; |
7a7cacba PB |
4605 | kmsg = &iomsg; |
4606 | } | |
03b1230c | 4607 | |
bc02ef33 | 4608 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4609 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4610 | if (IS_ERR(kbuf)) |
52de1fe1 | 4611 | return PTR_ERR(kbuf); |
7a7cacba | 4612 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4613 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4614 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4615 | 1, req->sr_msg.len); |
4616 | } | |
52de1fe1 | 4617 | |
7a7cacba PB |
4618 | flags = req->sr_msg.msg_flags; |
4619 | if (flags & MSG_DONTWAIT) | |
4620 | req->flags |= REQ_F_NOWAIT; | |
4621 | else if (force_nonblock) | |
4622 | flags |= MSG_DONTWAIT; | |
e47293fd | 4623 | |
7a7cacba PB |
4624 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4625 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4626 | if (force_nonblock && ret == -EAGAIN) |
4627 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4628 | if (ret == -ERESTARTSYS) |
4629 | ret = -EINTR; | |
03b1230c | 4630 | |
7fbb1b54 PB |
4631 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4632 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4633 | /* fast path, check for non-NULL to avoid function call */ |
4634 | if (kmsg->free_iov) | |
4635 | kfree(kmsg->free_iov); | |
99bc4c38 | 4636 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4637 | if (ret < 0) |
4638 | req_set_fail_links(req); | |
889fca73 | 4639 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4640 | return 0; |
0fa03c62 | 4641 | } |
5d17b4a4 | 4642 | |
889fca73 | 4643 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4644 | { |
6b754c8b | 4645 | struct io_buffer *kbuf; |
7a7cacba PB |
4646 | struct io_sr_msg *sr = &req->sr_msg; |
4647 | struct msghdr msg; | |
4648 | void __user *buf = sr->buf; | |
fddaface | 4649 | struct socket *sock; |
7a7cacba PB |
4650 | struct iovec iov; |
4651 | unsigned flags; | |
bcda7baa | 4652 | int ret, cflags = 0; |
45d189c6 | 4653 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4654 | |
dba4a925 | 4655 | sock = sock_from_file(req->file); |
7a7cacba | 4656 | if (unlikely(!sock)) |
dba4a925 | 4657 | return -ENOTSOCK; |
fddaface | 4658 | |
bc02ef33 | 4659 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4660 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4661 | if (IS_ERR(kbuf)) |
4662 | return PTR_ERR(kbuf); | |
7a7cacba | 4663 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4664 | } |
bcda7baa | 4665 | |
7a7cacba | 4666 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4667 | if (unlikely(ret)) |
4668 | goto out_free; | |
fddaface | 4669 | |
7a7cacba PB |
4670 | msg.msg_name = NULL; |
4671 | msg.msg_control = NULL; | |
4672 | msg.msg_controllen = 0; | |
4673 | msg.msg_namelen = 0; | |
4674 | msg.msg_iocb = NULL; | |
4675 | msg.msg_flags = 0; | |
fddaface | 4676 | |
7a7cacba PB |
4677 | flags = req->sr_msg.msg_flags; |
4678 | if (flags & MSG_DONTWAIT) | |
4679 | req->flags |= REQ_F_NOWAIT; | |
4680 | else if (force_nonblock) | |
4681 | flags |= MSG_DONTWAIT; | |
4682 | ||
4683 | ret = sock_recvmsg(sock, &msg, flags); | |
4684 | if (force_nonblock && ret == -EAGAIN) | |
4685 | return -EAGAIN; | |
4686 | if (ret == -ERESTARTSYS) | |
4687 | ret = -EINTR; | |
14c32eee | 4688 | out_free: |
7fbb1b54 PB |
4689 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4690 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4691 | if (ret < 0) |
4692 | req_set_fail_links(req); | |
889fca73 | 4693 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4694 | return 0; |
fddaface JA |
4695 | } |
4696 | ||
3529d8c2 | 4697 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4698 | { |
8ed8d3c3 JA |
4699 | struct io_accept *accept = &req->accept; |
4700 | ||
14587a46 | 4701 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4702 | return -EINVAL; |
8042d6ce | 4703 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4704 | return -EINVAL; |
4705 | ||
d55e5f5b JA |
4706 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4707 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4708 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4709 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4710 | return 0; |
8ed8d3c3 | 4711 | } |
17f2fe35 | 4712 | |
889fca73 | 4713 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4714 | { |
4715 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4716 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4717 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4718 | int ret; |
4719 | ||
e697deed JX |
4720 | if (req->file->f_flags & O_NONBLOCK) |
4721 | req->flags |= REQ_F_NOWAIT; | |
4722 | ||
8ed8d3c3 | 4723 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4724 | accept->addr_len, accept->flags, |
4725 | accept->nofile); | |
8ed8d3c3 | 4726 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4727 | return -EAGAIN; |
ac45abc0 PB |
4728 | if (ret < 0) { |
4729 | if (ret == -ERESTARTSYS) | |
4730 | ret = -EINTR; | |
4e88d6e7 | 4731 | req_set_fail_links(req); |
ac45abc0 | 4732 | } |
889fca73 | 4733 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4734 | return 0; |
8ed8d3c3 JA |
4735 | } |
4736 | ||
93642ef8 PB |
4737 | static int io_connect_prep_async(struct io_kiocb *req) |
4738 | { | |
4739 | struct io_async_connect *io = req->async_data; | |
4740 | struct io_connect *conn = &req->connect; | |
4741 | ||
4742 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4743 | } | |
4744 | ||
3529d8c2 | 4745 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4746 | { |
3529d8c2 | 4747 | struct io_connect *conn = &req->connect; |
f499a021 | 4748 | |
14587a46 | 4749 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4750 | return -EINVAL; |
4751 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4752 | return -EINVAL; | |
4753 | ||
3529d8c2 JA |
4754 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4755 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4756 | return 0; |
f499a021 JA |
4757 | } |
4758 | ||
889fca73 | 4759 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4760 | { |
e8c2bc1f | 4761 | struct io_async_connect __io, *io; |
f8e85cf2 | 4762 | unsigned file_flags; |
3fbb51c1 | 4763 | int ret; |
45d189c6 | 4764 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4765 | |
e8c2bc1f JA |
4766 | if (req->async_data) { |
4767 | io = req->async_data; | |
f499a021 | 4768 | } else { |
3529d8c2 JA |
4769 | ret = move_addr_to_kernel(req->connect.addr, |
4770 | req->connect.addr_len, | |
e8c2bc1f | 4771 | &__io.address); |
f499a021 JA |
4772 | if (ret) |
4773 | goto out; | |
4774 | io = &__io; | |
4775 | } | |
4776 | ||
3fbb51c1 JA |
4777 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4778 | ||
e8c2bc1f | 4779 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4780 | req->connect.addr_len, file_flags); |
87f80d62 | 4781 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4782 | if (req->async_data) |
b7bb4f7d | 4783 | return -EAGAIN; |
e8c2bc1f | 4784 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4785 | ret = -ENOMEM; |
4786 | goto out; | |
4787 | } | |
e8c2bc1f JA |
4788 | io = req->async_data; |
4789 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4790 | return -EAGAIN; |
f499a021 | 4791 | } |
f8e85cf2 JA |
4792 | if (ret == -ERESTARTSYS) |
4793 | ret = -EINTR; | |
f499a021 | 4794 | out: |
4e88d6e7 JA |
4795 | if (ret < 0) |
4796 | req_set_fail_links(req); | |
889fca73 | 4797 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4798 | return 0; |
469956e8 Y |
4799 | } |
4800 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4801 | #define IO_NETOP_FN(op) \ |
4802 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4803 | { \ | |
4804 | return -EOPNOTSUPP; \ | |
4805 | } | |
4806 | ||
4807 | #define IO_NETOP_PREP(op) \ | |
4808 | IO_NETOP_FN(op) \ | |
4809 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4810 | { \ | |
4811 | return -EOPNOTSUPP; \ | |
4812 | } \ | |
4813 | ||
4814 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4815 | IO_NETOP_PREP(op) \ | |
4816 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4817 | { \ | |
4818 | return -EOPNOTSUPP; \ | |
4819 | } | |
4820 | ||
4821 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4822 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4823 | IO_NETOP_PREP_ASYNC(connect); | |
4824 | IO_NETOP_PREP(accept); | |
4825 | IO_NETOP_FN(send); | |
4826 | IO_NETOP_FN(recv); | |
469956e8 | 4827 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4828 | |
d7718a9d JA |
4829 | struct io_poll_table { |
4830 | struct poll_table_struct pt; | |
4831 | struct io_kiocb *req; | |
4832 | int error; | |
4833 | }; | |
ce593a6c | 4834 | |
d7718a9d JA |
4835 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4836 | __poll_t mask, task_work_func_t func) | |
4837 | { | |
aa96bf8a | 4838 | int ret; |
d7718a9d JA |
4839 | |
4840 | /* for instances that support it check for an event match first: */ | |
4841 | if (mask && !(mask & poll->events)) | |
4842 | return 0; | |
4843 | ||
4844 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4845 | ||
4846 | list_del_init(&poll->wait.entry); | |
4847 | ||
d7718a9d | 4848 | req->result = mask; |
7cbf1722 | 4849 | req->task_work.func = func; |
6d816e08 JA |
4850 | percpu_ref_get(&req->ctx->refs); |
4851 | ||
d7718a9d | 4852 | /* |
e3aabf95 JA |
4853 | * If this fails, then the task is exiting. When a task exits, the |
4854 | * work gets canceled, so just cancel this request as well instead | |
4855 | * of executing it. We can't safely execute it anyway, as we may not | |
4856 | * have the needed state needed for it anyway. | |
d7718a9d | 4857 | */ |
355fb9e2 | 4858 | ret = io_req_task_work_add(req); |
aa96bf8a | 4859 | if (unlikely(ret)) { |
e3aabf95 | 4860 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4861 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4862 | } |
d7718a9d JA |
4863 | return 1; |
4864 | } | |
4865 | ||
74ce6ce4 JA |
4866 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4867 | __acquires(&req->ctx->completion_lock) | |
4868 | { | |
4869 | struct io_ring_ctx *ctx = req->ctx; | |
4870 | ||
4871 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4872 | struct poll_table_struct pt = { ._key = poll->events }; | |
4873 | ||
4874 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4875 | } | |
4876 | ||
4877 | spin_lock_irq(&ctx->completion_lock); | |
4878 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4879 | add_wait_queue(poll->head, &poll->wait); | |
4880 | return true; | |
4881 | } | |
4882 | ||
4883 | return false; | |
4884 | } | |
4885 | ||
d4e7cd36 | 4886 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4887 | { |
e8c2bc1f | 4888 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4889 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4890 | return req->async_data; |
d4e7cd36 JA |
4891 | return req->apoll->double_poll; |
4892 | } | |
4893 | ||
4894 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4895 | { | |
4896 | if (req->opcode == IORING_OP_POLL_ADD) | |
4897 | return &req->poll; | |
4898 | return &req->apoll->poll; | |
4899 | } | |
4900 | ||
4901 | static void io_poll_remove_double(struct io_kiocb *req) | |
4902 | { | |
4903 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4904 | |
4905 | lockdep_assert_held(&req->ctx->completion_lock); | |
4906 | ||
4907 | if (poll && poll->head) { | |
4908 | struct wait_queue_head *head = poll->head; | |
4909 | ||
4910 | spin_lock(&head->lock); | |
4911 | list_del_init(&poll->wait.entry); | |
4912 | if (poll->wait.private) | |
4913 | refcount_dec(&req->refs); | |
4914 | poll->head = NULL; | |
4915 | spin_unlock(&head->lock); | |
4916 | } | |
4917 | } | |
4918 | ||
4919 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4920 | { | |
4921 | struct io_ring_ctx *ctx = req->ctx; | |
4922 | ||
d4e7cd36 | 4923 | io_poll_remove_double(req); |
18bceab1 JA |
4924 | req->poll.done = true; |
4925 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4926 | io_commit_cqring(ctx); | |
4927 | } | |
4928 | ||
dd221f46 | 4929 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4930 | { |
dd221f46 | 4931 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4932 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4933 | struct io_kiocb *nxt; |
18bceab1 JA |
4934 | |
4935 | if (io_poll_rewait(req, &req->poll)) { | |
4936 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
4937 | } else { |
4938 | hash_del(&req->hash_node); | |
4939 | io_poll_complete(req, req->result, 0); | |
4940 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 4941 | |
dd221f46 PB |
4942 | nxt = io_put_req_find_next(req); |
4943 | io_cqring_ev_posted(ctx); | |
4944 | if (nxt) | |
4945 | __io_req_task_submit(nxt); | |
4946 | } | |
18bceab1 | 4947 | |
6d816e08 | 4948 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4949 | } |
4950 | ||
4951 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4952 | int sync, void *key) | |
4953 | { | |
4954 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4955 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4956 | __poll_t mask = key_to_poll(key); |
4957 | ||
4958 | /* for instances that support it check for an event match first: */ | |
4959 | if (mask && !(mask & poll->events)) | |
4960 | return 0; | |
4961 | ||
8706e04e JA |
4962 | list_del_init(&wait->entry); |
4963 | ||
807abcb0 | 4964 | if (poll && poll->head) { |
18bceab1 JA |
4965 | bool done; |
4966 | ||
807abcb0 JA |
4967 | spin_lock(&poll->head->lock); |
4968 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4969 | if (!done) |
807abcb0 | 4970 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4971 | /* make sure double remove sees this as being gone */ |
4972 | wait->private = NULL; | |
807abcb0 | 4973 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4974 | if (!done) { |
4975 | /* use wait func handler, so it matches the rq type */ | |
4976 | poll->wait.func(&poll->wait, mode, sync, key); | |
4977 | } | |
18bceab1 JA |
4978 | } |
4979 | refcount_dec(&req->refs); | |
4980 | return 1; | |
4981 | } | |
4982 | ||
4983 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4984 | wait_queue_func_t wake_func) | |
4985 | { | |
4986 | poll->head = NULL; | |
4987 | poll->done = false; | |
4988 | poll->canceled = false; | |
4989 | poll->events = events; | |
4990 | INIT_LIST_HEAD(&poll->wait.entry); | |
4991 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4992 | } | |
4993 | ||
4994 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4995 | struct wait_queue_head *head, |
4996 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4997 | { |
4998 | struct io_kiocb *req = pt->req; | |
4999 | ||
5000 | /* | |
5001 | * If poll->head is already set, it's because the file being polled | |
5002 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5003 | * for write). Setup a separate io_poll_iocb if this happens. | |
5004 | */ | |
5005 | if (unlikely(poll->head)) { | |
58852d4d PB |
5006 | struct io_poll_iocb *poll_one = poll; |
5007 | ||
18bceab1 | 5008 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5009 | if (*poll_ptr) { |
18bceab1 JA |
5010 | pt->error = -EINVAL; |
5011 | return; | |
5012 | } | |
5013 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5014 | if (!poll) { | |
5015 | pt->error = -ENOMEM; | |
5016 | return; | |
5017 | } | |
58852d4d | 5018 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5019 | refcount_inc(&req->refs); |
5020 | poll->wait.private = req; | |
807abcb0 | 5021 | *poll_ptr = poll; |
18bceab1 JA |
5022 | } |
5023 | ||
5024 | pt->error = 0; | |
5025 | poll->head = head; | |
a31eb4a2 JX |
5026 | |
5027 | if (poll->events & EPOLLEXCLUSIVE) | |
5028 | add_wait_queue_exclusive(head, &poll->wait); | |
5029 | else | |
5030 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5031 | } |
5032 | ||
5033 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5034 | struct poll_table_struct *p) | |
5035 | { | |
5036 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5037 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5038 | |
807abcb0 | 5039 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5040 | } |
5041 | ||
d7718a9d JA |
5042 | static void io_async_task_func(struct callback_head *cb) |
5043 | { | |
5044 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5045 | struct async_poll *apoll = req->apoll; | |
5046 | struct io_ring_ctx *ctx = req->ctx; | |
5047 | ||
5048 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5049 | ||
74ce6ce4 | 5050 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5051 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5052 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5053 | return; |
d7718a9d JA |
5054 | } |
5055 | ||
31067255 | 5056 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5057 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5058 | hash_del(&req->hash_node); |
2bae047e | 5059 | |
d4e7cd36 | 5060 | io_poll_remove_double(req); |
74ce6ce4 JA |
5061 | spin_unlock_irq(&ctx->completion_lock); |
5062 | ||
0be0b0e3 PB |
5063 | if (!READ_ONCE(apoll->poll.canceled)) |
5064 | __io_req_task_submit(req); | |
5065 | else | |
5066 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5067 | |
6d816e08 | 5068 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5069 | kfree(apoll->double_poll); |
31067255 | 5070 | kfree(apoll); |
d7718a9d JA |
5071 | } |
5072 | ||
5073 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5074 | void *key) | |
5075 | { | |
5076 | struct io_kiocb *req = wait->private; | |
5077 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5078 | ||
5079 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5080 | key_to_poll(key)); | |
5081 | ||
5082 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5083 | } | |
5084 | ||
5085 | static void io_poll_req_insert(struct io_kiocb *req) | |
5086 | { | |
5087 | struct io_ring_ctx *ctx = req->ctx; | |
5088 | struct hlist_head *list; | |
5089 | ||
5090 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5091 | hlist_add_head(&req->hash_node, list); | |
5092 | } | |
5093 | ||
5094 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5095 | struct io_poll_iocb *poll, | |
5096 | struct io_poll_table *ipt, __poll_t mask, | |
5097 | wait_queue_func_t wake_func) | |
5098 | __acquires(&ctx->completion_lock) | |
5099 | { | |
5100 | struct io_ring_ctx *ctx = req->ctx; | |
5101 | bool cancel = false; | |
5102 | ||
4d52f338 | 5103 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5104 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5105 | poll->file = req->file; |
18bceab1 | 5106 | poll->wait.private = req; |
d7718a9d JA |
5107 | |
5108 | ipt->pt._key = mask; | |
5109 | ipt->req = req; | |
5110 | ipt->error = -EINVAL; | |
5111 | ||
d7718a9d JA |
5112 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5113 | ||
5114 | spin_lock_irq(&ctx->completion_lock); | |
5115 | if (likely(poll->head)) { | |
5116 | spin_lock(&poll->head->lock); | |
5117 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5118 | if (ipt->error) | |
5119 | cancel = true; | |
5120 | ipt->error = 0; | |
5121 | mask = 0; | |
5122 | } | |
5123 | if (mask || ipt->error) | |
5124 | list_del_init(&poll->wait.entry); | |
5125 | else if (cancel) | |
5126 | WRITE_ONCE(poll->canceled, true); | |
5127 | else if (!poll->done) /* actually waiting for an event */ | |
5128 | io_poll_req_insert(req); | |
5129 | spin_unlock(&poll->head->lock); | |
5130 | } | |
5131 | ||
5132 | return mask; | |
5133 | } | |
5134 | ||
5135 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5136 | { | |
5137 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5138 | struct io_ring_ctx *ctx = req->ctx; | |
5139 | struct async_poll *apoll; | |
5140 | struct io_poll_table ipt; | |
5141 | __poll_t mask, ret; | |
9dab14b8 | 5142 | int rw; |
d7718a9d JA |
5143 | |
5144 | if (!req->file || !file_can_poll(req->file)) | |
5145 | return false; | |
24c74678 | 5146 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5147 | return false; |
9dab14b8 JA |
5148 | if (def->pollin) |
5149 | rw = READ; | |
5150 | else if (def->pollout) | |
5151 | rw = WRITE; | |
5152 | else | |
5153 | return false; | |
5154 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5155 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5156 | return false; |
5157 | ||
5158 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5159 | if (unlikely(!apoll)) | |
5160 | return false; | |
807abcb0 | 5161 | apoll->double_poll = NULL; |
d7718a9d JA |
5162 | |
5163 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5164 | req->apoll = apoll; |
d7718a9d | 5165 | |
8755d97a | 5166 | mask = 0; |
d7718a9d | 5167 | if (def->pollin) |
8755d97a | 5168 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5169 | if (def->pollout) |
5170 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5171 | |
5172 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5173 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5174 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5175 | mask &= ~POLLIN; | |
5176 | ||
d7718a9d JA |
5177 | mask |= POLLERR | POLLPRI; |
5178 | ||
5179 | ipt.pt._qproc = io_async_queue_proc; | |
5180 | ||
5181 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5182 | io_async_wake); | |
a36da65c | 5183 | if (ret || ipt.error) { |
d4e7cd36 | 5184 | io_poll_remove_double(req); |
d7718a9d | 5185 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5186 | kfree(apoll->double_poll); |
d7718a9d JA |
5187 | kfree(apoll); |
5188 | return false; | |
5189 | } | |
5190 | spin_unlock_irq(&ctx->completion_lock); | |
5191 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5192 | apoll->poll.events); | |
5193 | return true; | |
5194 | } | |
5195 | ||
5196 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5197 | struct io_poll_iocb *poll) | |
221c5eb2 | 5198 | { |
b41e9852 | 5199 | bool do_complete = false; |
221c5eb2 JA |
5200 | |
5201 | spin_lock(&poll->head->lock); | |
5202 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5203 | if (!list_empty(&poll->wait.entry)) { |
5204 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5205 | do_complete = true; |
221c5eb2 JA |
5206 | } |
5207 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5208 | hash_del(&req->hash_node); |
d7718a9d JA |
5209 | return do_complete; |
5210 | } | |
5211 | ||
5212 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5213 | { | |
5214 | bool do_complete; | |
5215 | ||
d4e7cd36 JA |
5216 | io_poll_remove_double(req); |
5217 | ||
d7718a9d JA |
5218 | if (req->opcode == IORING_OP_POLL_ADD) { |
5219 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5220 | } else { | |
3bfa5bcb JA |
5221 | struct async_poll *apoll = req->apoll; |
5222 | ||
d7718a9d | 5223 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5224 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5225 | if (do_complete) { | |
d7718a9d | 5226 | io_put_req(req); |
807abcb0 | 5227 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5228 | kfree(apoll); |
5229 | } | |
b1f573bd XW |
5230 | } |
5231 | ||
b41e9852 JA |
5232 | if (do_complete) { |
5233 | io_cqring_fill_event(req, -ECANCELED); | |
5234 | io_commit_cqring(req->ctx); | |
f254ac04 | 5235 | req_set_fail_links(req); |
216578e5 | 5236 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5237 | } |
5238 | ||
5239 | return do_complete; | |
221c5eb2 JA |
5240 | } |
5241 | ||
76e1b642 JA |
5242 | /* |
5243 | * Returns true if we found and killed one or more poll requests | |
5244 | */ | |
6b81928d PB |
5245 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5246 | struct files_struct *files) | |
221c5eb2 | 5247 | { |
78076bb6 | 5248 | struct hlist_node *tmp; |
221c5eb2 | 5249 | struct io_kiocb *req; |
8e2e1faf | 5250 | int posted = 0, i; |
221c5eb2 JA |
5251 | |
5252 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5253 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5254 | struct hlist_head *list; | |
5255 | ||
5256 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5257 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5258 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5259 | posted += io_poll_remove_one(req); |
5260 | } | |
221c5eb2 JA |
5261 | } |
5262 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5263 | |
8e2e1faf JA |
5264 | if (posted) |
5265 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5266 | |
5267 | return posted != 0; | |
221c5eb2 JA |
5268 | } |
5269 | ||
47f46768 JA |
5270 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5271 | { | |
78076bb6 | 5272 | struct hlist_head *list; |
47f46768 JA |
5273 | struct io_kiocb *req; |
5274 | ||
78076bb6 JA |
5275 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5276 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5277 | if (sqe_addr != req->user_data) |
5278 | continue; | |
5279 | if (io_poll_remove_one(req)) | |
eac406c6 | 5280 | return 0; |
b41e9852 | 5281 | return -EALREADY; |
47f46768 JA |
5282 | } |
5283 | ||
5284 | return -ENOENT; | |
5285 | } | |
5286 | ||
3529d8c2 JA |
5287 | static int io_poll_remove_prep(struct io_kiocb *req, |
5288 | const struct io_uring_sqe *sqe) | |
0969e783 | 5289 | { |
0969e783 JA |
5290 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5291 | return -EINVAL; | |
5292 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5293 | sqe->poll_events) | |
5294 | return -EINVAL; | |
5295 | ||
018043be | 5296 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5297 | return 0; |
5298 | } | |
5299 | ||
221c5eb2 JA |
5300 | /* |
5301 | * Find a running poll command that matches one specified in sqe->addr, | |
5302 | * and remove it if found. | |
5303 | */ | |
61e98203 | 5304 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5305 | { |
5306 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5307 | int ret; |
221c5eb2 | 5308 | |
221c5eb2 | 5309 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5310 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5311 | spin_unlock_irq(&ctx->completion_lock); |
5312 | ||
4e88d6e7 JA |
5313 | if (ret < 0) |
5314 | req_set_fail_links(req); | |
e1e16097 | 5315 | io_req_complete(req, ret); |
221c5eb2 JA |
5316 | return 0; |
5317 | } | |
5318 | ||
221c5eb2 JA |
5319 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5320 | void *key) | |
5321 | { | |
c2f2eb7d JA |
5322 | struct io_kiocb *req = wait->private; |
5323 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5324 | |
d7718a9d | 5325 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5326 | } |
5327 | ||
221c5eb2 JA |
5328 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5329 | struct poll_table_struct *p) | |
5330 | { | |
5331 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5332 | ||
e8c2bc1f | 5333 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5334 | } |
5335 | ||
3529d8c2 | 5336 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5337 | { |
5338 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5339 | u32 events; |
221c5eb2 JA |
5340 | |
5341 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5342 | return -EINVAL; | |
5343 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5344 | return -EINVAL; | |
5345 | ||
5769a351 JX |
5346 | events = READ_ONCE(sqe->poll32_events); |
5347 | #ifdef __BIG_ENDIAN | |
5348 | events = swahw32(events); | |
5349 | #endif | |
a31eb4a2 JX |
5350 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5351 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5352 | return 0; |
5353 | } | |
5354 | ||
61e98203 | 5355 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5356 | { |
5357 | struct io_poll_iocb *poll = &req->poll; | |
5358 | struct io_ring_ctx *ctx = req->ctx; | |
5359 | struct io_poll_table ipt; | |
0969e783 | 5360 | __poll_t mask; |
0969e783 | 5361 | |
d7718a9d | 5362 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5363 | |
d7718a9d JA |
5364 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5365 | io_poll_wake); | |
221c5eb2 | 5366 | |
8c838788 | 5367 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5368 | ipt.error = 0; |
b0dd8a41 | 5369 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5370 | } |
221c5eb2 JA |
5371 | spin_unlock_irq(&ctx->completion_lock); |
5372 | ||
8c838788 JA |
5373 | if (mask) { |
5374 | io_cqring_ev_posted(ctx); | |
014db007 | 5375 | io_put_req(req); |
221c5eb2 | 5376 | } |
8c838788 | 5377 | return ipt.error; |
221c5eb2 JA |
5378 | } |
5379 | ||
5262f567 JA |
5380 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5381 | { | |
ad8a48ac JA |
5382 | struct io_timeout_data *data = container_of(timer, |
5383 | struct io_timeout_data, timer); | |
5384 | struct io_kiocb *req = data->req; | |
5385 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5386 | unsigned long flags; |
5387 | ||
5262f567 | 5388 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5389 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5390 | atomic_set(&req->ctx->cq_timeouts, |
5391 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5392 | ||
78e19bbe | 5393 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5394 | io_commit_cqring(ctx); |
5395 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5396 | ||
5397 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5398 | req_set_fail_links(req); |
5262f567 JA |
5399 | io_put_req(req); |
5400 | return HRTIMER_NORESTART; | |
5401 | } | |
5402 | ||
fbd15848 PB |
5403 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5404 | __u64 user_data) | |
f254ac04 | 5405 | { |
fbd15848 | 5406 | struct io_timeout_data *io; |
47f46768 JA |
5407 | struct io_kiocb *req; |
5408 | int ret = -ENOENT; | |
f254ac04 | 5409 | |
135fcde8 | 5410 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5411 | if (user_data == req->user_data) { |
47f46768 JA |
5412 | ret = 0; |
5413 | break; | |
5414 | } | |
5415 | } | |
5416 | ||
5417 | if (ret == -ENOENT) | |
fbd15848 PB |
5418 | return ERR_PTR(ret); |
5419 | ||
5420 | io = req->async_data; | |
e8c2bc1f | 5421 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5422 | if (ret == -1) |
fbd15848 | 5423 | return ERR_PTR(-EALREADY); |
a71976f3 | 5424 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5425 | return req; |
5426 | } | |
47f46768 | 5427 | |
fbd15848 PB |
5428 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5429 | { | |
5430 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5431 | ||
5432 | if (IS_ERR(req)) | |
5433 | return PTR_ERR(req); | |
f254ac04 JA |
5434 | |
5435 | req_set_fail_links(req); | |
f254ac04 | 5436 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5437 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5438 | return 0; |
5439 | } | |
5440 | ||
9c8e11b3 PB |
5441 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5442 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5443 | { |
9c8e11b3 PB |
5444 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5445 | struct io_timeout_data *data; | |
47f46768 | 5446 | |
9c8e11b3 PB |
5447 | if (IS_ERR(req)) |
5448 | return PTR_ERR(req); | |
47f46768 | 5449 | |
9c8e11b3 PB |
5450 | req->timeout.off = 0; /* noseq */ |
5451 | data = req->async_data; | |
5452 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5453 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5454 | data->timer.function = io_timeout_fn; | |
5455 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5456 | return 0; | |
47f46768 JA |
5457 | } |
5458 | ||
3529d8c2 JA |
5459 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5460 | const struct io_uring_sqe *sqe) | |
b29472ee | 5461 | { |
9c8e11b3 PB |
5462 | struct io_timeout_rem *tr = &req->timeout_rem; |
5463 | ||
b29472ee JA |
5464 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5465 | return -EINVAL; | |
61710e43 DA |
5466 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5467 | return -EINVAL; | |
9c8e11b3 | 5468 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5469 | return -EINVAL; |
5470 | ||
9c8e11b3 PB |
5471 | tr->addr = READ_ONCE(sqe->addr); |
5472 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5473 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5474 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5475 | return -EINVAL; | |
5476 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5477 | return -EFAULT; | |
5478 | } else if (tr->flags) { | |
5479 | /* timeout removal doesn't support flags */ | |
b29472ee | 5480 | return -EINVAL; |
9c8e11b3 | 5481 | } |
b29472ee | 5482 | |
b29472ee JA |
5483 | return 0; |
5484 | } | |
5485 | ||
8662daec PB |
5486 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5487 | { | |
5488 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5489 | : HRTIMER_MODE_REL; | |
5490 | } | |
5491 | ||
11365043 JA |
5492 | /* |
5493 | * Remove or update an existing timeout command | |
5494 | */ | |
61e98203 | 5495 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5496 | { |
9c8e11b3 | 5497 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5498 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5499 | int ret; |
11365043 | 5500 | |
11365043 | 5501 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5502 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5503 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5504 | else |
5505 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5506 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5507 | |
47f46768 | 5508 | io_cqring_fill_event(req, ret); |
11365043 JA |
5509 | io_commit_cqring(ctx); |
5510 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5511 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5512 | if (ret < 0) |
5513 | req_set_fail_links(req); | |
ec9c02ad | 5514 | io_put_req(req); |
11365043 | 5515 | return 0; |
5262f567 JA |
5516 | } |
5517 | ||
3529d8c2 | 5518 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5519 | bool is_timeout_link) |
5262f567 | 5520 | { |
ad8a48ac | 5521 | struct io_timeout_data *data; |
a41525ab | 5522 | unsigned flags; |
56080b02 | 5523 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5524 | |
ad8a48ac | 5525 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5526 | return -EINVAL; |
ad8a48ac | 5527 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5528 | return -EINVAL; |
56080b02 | 5529 | if (off && is_timeout_link) |
2d28390a | 5530 | return -EINVAL; |
a41525ab JA |
5531 | flags = READ_ONCE(sqe->timeout_flags); |
5532 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5533 | return -EINVAL; |
bdf20073 | 5534 | |
bfe68a22 | 5535 | req->timeout.off = off; |
26a61679 | 5536 | |
e8c2bc1f | 5537 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5538 | return -ENOMEM; |
5539 | ||
e8c2bc1f | 5540 | data = req->async_data; |
ad8a48ac | 5541 | data->req = req; |
ad8a48ac JA |
5542 | |
5543 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5544 | return -EFAULT; |
5545 | ||
8662daec | 5546 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5547 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5548 | return 0; | |
5549 | } | |
5550 | ||
61e98203 | 5551 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5552 | { |
ad8a48ac | 5553 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5554 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5555 | struct list_head *entry; |
bfe68a22 | 5556 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5557 | |
733f5c95 | 5558 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5559 | |
5262f567 JA |
5560 | /* |
5561 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5562 | * timeout event to be satisfied. If it isn't set, then this is |
5563 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5564 | */ |
8eb7e2d0 | 5565 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5566 | entry = ctx->timeout_list.prev; |
5567 | goto add; | |
5568 | } | |
5262f567 | 5569 | |
bfe68a22 PB |
5570 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5571 | req->timeout.target_seq = tail + off; | |
5262f567 | 5572 | |
f010505b MDG |
5573 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5574 | * This is safe because ->completion_lock is held, and submissions | |
5575 | * and completions are never mixed in the same ->completion_lock section. | |
5576 | */ | |
5577 | ctx->cq_last_tm_flush = tail; | |
5578 | ||
5262f567 JA |
5579 | /* |
5580 | * Insertion sort, ensuring the first entry in the list is always | |
5581 | * the one we need first. | |
5582 | */ | |
5262f567 | 5583 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5584 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5585 | timeout.list); | |
5262f567 | 5586 | |
8eb7e2d0 | 5587 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5588 | continue; |
bfe68a22 PB |
5589 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5590 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5591 | break; |
5592 | } | |
93bd25bb | 5593 | add: |
135fcde8 | 5594 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5595 | data->timer.function = io_timeout_fn; |
5596 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5597 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5598 | return 0; |
5599 | } | |
5262f567 | 5600 | |
62755e35 JA |
5601 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5602 | { | |
5603 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5604 | ||
5605 | return req->user_data == (unsigned long) data; | |
5606 | } | |
5607 | ||
5aa75ed5 | 5608 | static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr) |
62755e35 | 5609 | { |
62755e35 | 5610 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5611 | int ret = 0; |
5612 | ||
5aa75ed5 JA |
5613 | if (!tctx->io_wq) |
5614 | return -ENOENT; | |
5615 | ||
5616 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false); | |
62755e35 JA |
5617 | switch (cancel_ret) { |
5618 | case IO_WQ_CANCEL_OK: | |
5619 | ret = 0; | |
5620 | break; | |
5621 | case IO_WQ_CANCEL_RUNNING: | |
5622 | ret = -EALREADY; | |
5623 | break; | |
5624 | case IO_WQ_CANCEL_NOTFOUND: | |
5625 | ret = -ENOENT; | |
5626 | break; | |
5627 | } | |
5628 | ||
e977d6d3 JA |
5629 | return ret; |
5630 | } | |
5631 | ||
47f46768 JA |
5632 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5633 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5634 | int success_ret) |
47f46768 JA |
5635 | { |
5636 | unsigned long flags; | |
5637 | int ret; | |
5638 | ||
5aa75ed5 JA |
5639 | ret = io_async_cancel_one(req->task->io_uring, |
5640 | (void *) (unsigned long) sqe_addr); | |
47f46768 JA |
5641 | if (ret != -ENOENT) { |
5642 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5643 | goto done; | |
5644 | } | |
5645 | ||
5646 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5647 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5648 | if (ret != -ENOENT) | |
5649 | goto done; | |
5650 | ret = io_poll_cancel(ctx, sqe_addr); | |
5651 | done: | |
b0dd8a41 JA |
5652 | if (!ret) |
5653 | ret = success_ret; | |
47f46768 JA |
5654 | io_cqring_fill_event(req, ret); |
5655 | io_commit_cqring(ctx); | |
5656 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5657 | io_cqring_ev_posted(ctx); | |
5658 | ||
4e88d6e7 JA |
5659 | if (ret < 0) |
5660 | req_set_fail_links(req); | |
014db007 | 5661 | io_put_req(req); |
47f46768 JA |
5662 | } |
5663 | ||
3529d8c2 JA |
5664 | static int io_async_cancel_prep(struct io_kiocb *req, |
5665 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5666 | { |
fbf23849 | 5667 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5668 | return -EINVAL; |
61710e43 DA |
5669 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5670 | return -EINVAL; | |
5671 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5672 | return -EINVAL; |
5673 | ||
fbf23849 JA |
5674 | req->cancel.addr = READ_ONCE(sqe->addr); |
5675 | return 0; | |
5676 | } | |
5677 | ||
61e98203 | 5678 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5679 | { |
5680 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5681 | |
014db007 | 5682 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5683 | return 0; |
5684 | } | |
5685 | ||
269bbe5f | 5686 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5687 | const struct io_uring_sqe *sqe) |
5688 | { | |
6ca56f84 JA |
5689 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5690 | return -EINVAL; | |
61710e43 DA |
5691 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5692 | return -EINVAL; | |
5693 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5694 | return -EINVAL; |
5695 | ||
269bbe5f BM |
5696 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5697 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5698 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5699 | return -EINVAL; |
269bbe5f | 5700 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5701 | return 0; |
5702 | } | |
5703 | ||
889fca73 | 5704 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5705 | { |
5706 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5707 | struct io_uring_rsrc_update up; |
05f3fb3c | 5708 | int ret; |
fbf23849 | 5709 | |
45d189c6 | 5710 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5711 | return -EAGAIN; |
05f3fb3c | 5712 | |
269bbe5f BM |
5713 | up.offset = req->rsrc_update.offset; |
5714 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5715 | |
5716 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5717 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5718 | mutex_unlock(&ctx->uring_lock); |
5719 | ||
5720 | if (ret < 0) | |
5721 | req_set_fail_links(req); | |
889fca73 | 5722 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5723 | return 0; |
5724 | } | |
5725 | ||
bfe76559 | 5726 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5727 | { |
d625c6ee | 5728 | switch (req->opcode) { |
e781573e | 5729 | case IORING_OP_NOP: |
bfe76559 | 5730 | return 0; |
f67676d1 JA |
5731 | case IORING_OP_READV: |
5732 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5733 | case IORING_OP_READ: |
bfe76559 | 5734 | return io_read_prep(req, sqe); |
f67676d1 JA |
5735 | case IORING_OP_WRITEV: |
5736 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5737 | case IORING_OP_WRITE: |
bfe76559 | 5738 | return io_write_prep(req, sqe); |
0969e783 | 5739 | case IORING_OP_POLL_ADD: |
bfe76559 | 5740 | return io_poll_add_prep(req, sqe); |
0969e783 | 5741 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5742 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5743 | case IORING_OP_FSYNC: |
1155c76a | 5744 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5745 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5746 | return io_sfr_prep(req, sqe); |
03b1230c | 5747 | case IORING_OP_SENDMSG: |
fddaface | 5748 | case IORING_OP_SEND: |
bfe76559 | 5749 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5750 | case IORING_OP_RECVMSG: |
fddaface | 5751 | case IORING_OP_RECV: |
bfe76559 | 5752 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5753 | case IORING_OP_CONNECT: |
bfe76559 | 5754 | return io_connect_prep(req, sqe); |
2d28390a | 5755 | case IORING_OP_TIMEOUT: |
bfe76559 | 5756 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5757 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5758 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5759 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5760 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5761 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5762 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5763 | case IORING_OP_ACCEPT: |
bfe76559 | 5764 | return io_accept_prep(req, sqe); |
d63d1b5e | 5765 | case IORING_OP_FALLOCATE: |
bfe76559 | 5766 | return io_fallocate_prep(req, sqe); |
15b71abe | 5767 | case IORING_OP_OPENAT: |
bfe76559 | 5768 | return io_openat_prep(req, sqe); |
b5dba59e | 5769 | case IORING_OP_CLOSE: |
bfe76559 | 5770 | return io_close_prep(req, sqe); |
05f3fb3c | 5771 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5772 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5773 | case IORING_OP_STATX: |
bfe76559 | 5774 | return io_statx_prep(req, sqe); |
4840e418 | 5775 | case IORING_OP_FADVISE: |
bfe76559 | 5776 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5777 | case IORING_OP_MADVISE: |
bfe76559 | 5778 | return io_madvise_prep(req, sqe); |
cebdb986 | 5779 | case IORING_OP_OPENAT2: |
bfe76559 | 5780 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5781 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5782 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5783 | case IORING_OP_SPLICE: |
bfe76559 | 5784 | return io_splice_prep(req, sqe); |
ddf0322d | 5785 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5786 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5787 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5788 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5789 | case IORING_OP_TEE: |
bfe76559 | 5790 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5791 | case IORING_OP_SHUTDOWN: |
5792 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5793 | case IORING_OP_RENAMEAT: |
5794 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5795 | case IORING_OP_UNLINKAT: |
5796 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5797 | } |
5798 | ||
bfe76559 PB |
5799 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5800 | req->opcode); | |
5801 | return-EINVAL; | |
5802 | } | |
5803 | ||
93642ef8 | 5804 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5805 | { |
93642ef8 PB |
5806 | switch (req->opcode) { |
5807 | case IORING_OP_READV: | |
5808 | case IORING_OP_READ_FIXED: | |
5809 | case IORING_OP_READ: | |
5810 | return io_rw_prep_async(req, READ); | |
5811 | case IORING_OP_WRITEV: | |
5812 | case IORING_OP_WRITE_FIXED: | |
5813 | case IORING_OP_WRITE: | |
5814 | return io_rw_prep_async(req, WRITE); | |
5815 | case IORING_OP_SENDMSG: | |
5816 | case IORING_OP_SEND: | |
5817 | return io_sendmsg_prep_async(req); | |
5818 | case IORING_OP_RECVMSG: | |
5819 | case IORING_OP_RECV: | |
5820 | return io_recvmsg_prep_async(req); | |
5821 | case IORING_OP_CONNECT: | |
5822 | return io_connect_prep_async(req); | |
5823 | } | |
5824 | return 0; | |
5825 | } | |
5826 | ||
be7053b7 | 5827 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 5828 | { |
be7053b7 | 5829 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 5830 | return 0; |
be7053b7 | 5831 | /* some opcodes init it during the inital prep */ |
93642ef8 | 5832 | if (req->async_data) |
be7053b7 PB |
5833 | return 0; |
5834 | if (__io_alloc_async_data(req)) | |
bfe76559 | 5835 | return -EAGAIN; |
be7053b7 | 5836 | return io_req_prep_async(req); |
f67676d1 JA |
5837 | } |
5838 | ||
9cf7c104 PB |
5839 | static u32 io_get_sequence(struct io_kiocb *req) |
5840 | { | |
5841 | struct io_kiocb *pos; | |
5842 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 5843 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 5844 | |
f2f87370 PB |
5845 | io_for_each_link(pos, req) |
5846 | nr_reqs++; | |
9cf7c104 PB |
5847 | |
5848 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5849 | return total_submitted - nr_reqs; | |
5850 | } | |
5851 | ||
be7053b7 | 5852 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 5853 | { |
a197f664 | 5854 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5855 | struct io_defer_entry *de; |
f67676d1 | 5856 | int ret; |
9cf7c104 | 5857 | u32 seq; |
de0617e4 | 5858 | |
9d858b21 | 5859 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5860 | if (likely(list_empty_careful(&ctx->defer_list) && |
5861 | !(req->flags & REQ_F_IO_DRAIN))) | |
5862 | return 0; | |
5863 | ||
5864 | seq = io_get_sequence(req); | |
5865 | /* Still a chance to pass the sequence check */ | |
5866 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5867 | return 0; |
5868 | ||
be7053b7 PB |
5869 | ret = io_req_defer_prep(req); |
5870 | if (ret) | |
5871 | return ret; | |
cbdcb435 | 5872 | io_prep_async_link(req); |
27dc8338 PB |
5873 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5874 | if (!de) | |
5875 | return -ENOMEM; | |
2d28390a | 5876 | |
de0617e4 | 5877 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5878 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5879 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5880 | kfree(de); |
ae34817b PB |
5881 | io_queue_async_work(req); |
5882 | return -EIOCBQUEUED; | |
de0617e4 JA |
5883 | } |
5884 | ||
915967f6 | 5885 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5886 | de->req = req; |
9cf7c104 | 5887 | de->seq = seq; |
27dc8338 | 5888 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5889 | spin_unlock_irq(&ctx->completion_lock); |
5890 | return -EIOCBQUEUED; | |
5891 | } | |
5892 | ||
3ca405eb | 5893 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5894 | { |
0e1b6fe3 PB |
5895 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5896 | switch (req->opcode) { | |
5897 | case IORING_OP_READV: | |
5898 | case IORING_OP_READ_FIXED: | |
5899 | case IORING_OP_READ: | |
bcda7baa | 5900 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5901 | break; |
5902 | case IORING_OP_RECVMSG: | |
5903 | case IORING_OP_RECV: | |
bcda7baa | 5904 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5905 | break; |
5906 | } | |
5907 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5908 | } |
5909 | ||
0e1b6fe3 PB |
5910 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5911 | switch (req->opcode) { | |
5912 | case IORING_OP_READV: | |
5913 | case IORING_OP_READ_FIXED: | |
5914 | case IORING_OP_READ: | |
5915 | case IORING_OP_WRITEV: | |
5916 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5917 | case IORING_OP_WRITE: { |
5918 | struct io_async_rw *io = req->async_data; | |
5919 | if (io->free_iovec) | |
5920 | kfree(io->free_iovec); | |
0e1b6fe3 | 5921 | break; |
e8c2bc1f | 5922 | } |
0e1b6fe3 | 5923 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5924 | case IORING_OP_SENDMSG: { |
5925 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
5926 | |
5927 | kfree(io->free_iov); | |
0e1b6fe3 | 5928 | break; |
e8c2bc1f | 5929 | } |
0e1b6fe3 PB |
5930 | case IORING_OP_SPLICE: |
5931 | case IORING_OP_TEE: | |
5932 | io_put_file(req, req->splice.file_in, | |
5933 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5934 | break; | |
f3cd4850 JA |
5935 | case IORING_OP_OPENAT: |
5936 | case IORING_OP_OPENAT2: | |
5937 | if (req->open.filename) | |
5938 | putname(req->open.filename); | |
5939 | break; | |
80a261fd JA |
5940 | case IORING_OP_RENAMEAT: |
5941 | putname(req->rename.oldpath); | |
5942 | putname(req->rename.newpath); | |
5943 | break; | |
14a1143b JA |
5944 | case IORING_OP_UNLINKAT: |
5945 | putname(req->unlink.filename); | |
5946 | break; | |
0e1b6fe3 PB |
5947 | } |
5948 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5949 | } |
99bc4c38 PB |
5950 | } |
5951 | ||
889fca73 | 5952 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 5953 | { |
a197f664 | 5954 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 5955 | int ret; |
2b188cc1 | 5956 | |
d625c6ee | 5957 | switch (req->opcode) { |
2b188cc1 | 5958 | case IORING_OP_NOP: |
889fca73 | 5959 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
5960 | break; |
5961 | case IORING_OP_READV: | |
edafccee | 5962 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5963 | case IORING_OP_READ: |
889fca73 | 5964 | ret = io_read(req, issue_flags); |
edafccee | 5965 | break; |
3529d8c2 | 5966 | case IORING_OP_WRITEV: |
edafccee | 5967 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5968 | case IORING_OP_WRITE: |
889fca73 | 5969 | ret = io_write(req, issue_flags); |
2b188cc1 | 5970 | break; |
c992fe29 | 5971 | case IORING_OP_FSYNC: |
45d189c6 | 5972 | ret = io_fsync(req, issue_flags); |
c992fe29 | 5973 | break; |
221c5eb2 | 5974 | case IORING_OP_POLL_ADD: |
61e98203 | 5975 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
5976 | break; |
5977 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 5978 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 5979 | break; |
5d17b4a4 | 5980 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 5981 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 5982 | break; |
0fa03c62 | 5983 | case IORING_OP_SENDMSG: |
889fca73 | 5984 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 5985 | break; |
fddaface | 5986 | case IORING_OP_SEND: |
889fca73 | 5987 | ret = io_send(req, issue_flags); |
0fa03c62 | 5988 | break; |
aa1fa28f | 5989 | case IORING_OP_RECVMSG: |
889fca73 | 5990 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 5991 | break; |
fddaface | 5992 | case IORING_OP_RECV: |
889fca73 | 5993 | ret = io_recv(req, issue_flags); |
aa1fa28f | 5994 | break; |
5262f567 | 5995 | case IORING_OP_TIMEOUT: |
61e98203 | 5996 | ret = io_timeout(req, issue_flags); |
5262f567 | 5997 | break; |
11365043 | 5998 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 5999 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6000 | break; |
17f2fe35 | 6001 | case IORING_OP_ACCEPT: |
889fca73 | 6002 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6003 | break; |
f8e85cf2 | 6004 | case IORING_OP_CONNECT: |
889fca73 | 6005 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6006 | break; |
62755e35 | 6007 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6008 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6009 | break; |
d63d1b5e | 6010 | case IORING_OP_FALLOCATE: |
45d189c6 | 6011 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6012 | break; |
15b71abe | 6013 | case IORING_OP_OPENAT: |
45d189c6 | 6014 | ret = io_openat(req, issue_flags); |
15b71abe | 6015 | break; |
b5dba59e | 6016 | case IORING_OP_CLOSE: |
889fca73 | 6017 | ret = io_close(req, issue_flags); |
b5dba59e | 6018 | break; |
05f3fb3c | 6019 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6020 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6021 | break; |
eddc7ef5 | 6022 | case IORING_OP_STATX: |
45d189c6 | 6023 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6024 | break; |
4840e418 | 6025 | case IORING_OP_FADVISE: |
45d189c6 | 6026 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6027 | break; |
c1ca757b | 6028 | case IORING_OP_MADVISE: |
45d189c6 | 6029 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6030 | break; |
cebdb986 | 6031 | case IORING_OP_OPENAT2: |
45d189c6 | 6032 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6033 | break; |
3e4827b0 | 6034 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6035 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6036 | break; |
7d67af2c | 6037 | case IORING_OP_SPLICE: |
45d189c6 | 6038 | ret = io_splice(req, issue_flags); |
7d67af2c | 6039 | break; |
ddf0322d | 6040 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6041 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6042 | break; |
067524e9 | 6043 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6044 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6045 | break; |
f2a8d5c7 | 6046 | case IORING_OP_TEE: |
45d189c6 | 6047 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6048 | break; |
36f4fa68 | 6049 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6050 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6051 | break; |
80a261fd | 6052 | case IORING_OP_RENAMEAT: |
45d189c6 | 6053 | ret = io_renameat(req, issue_flags); |
80a261fd | 6054 | break; |
14a1143b | 6055 | case IORING_OP_UNLINKAT: |
45d189c6 | 6056 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6057 | break; |
2b188cc1 JA |
6058 | default: |
6059 | ret = -EINVAL; | |
6060 | break; | |
6061 | } | |
6062 | ||
def596e9 JA |
6063 | if (ret) |
6064 | return ret; | |
6065 | ||
b532576e JA |
6066 | /* If the op doesn't have a file, we're not polling for it */ |
6067 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6068 | const bool in_async = io_wq_current_is_worker(); |
6069 | ||
11ba820b JA |
6070 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6071 | if (in_async) | |
6072 | mutex_lock(&ctx->uring_lock); | |
6073 | ||
2e9dbe90 | 6074 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6075 | |
6076 | if (in_async) | |
6077 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6078 | } |
6079 | ||
6080 | return 0; | |
2b188cc1 JA |
6081 | } |
6082 | ||
5280f7e5 | 6083 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6084 | { |
6085 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6086 | struct io_kiocb *timeout; |
561fb04a | 6087 | int ret = 0; |
2b188cc1 | 6088 | |
6df1db6b PB |
6089 | timeout = io_prep_linked_timeout(req); |
6090 | if (timeout) | |
6091 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6092 | |
4014d943 | 6093 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6094 | ret = -ECANCELED; |
31b51510 | 6095 | |
561fb04a | 6096 | if (!ret) { |
561fb04a | 6097 | do { |
889fca73 | 6098 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6099 | /* |
6100 | * We can get EAGAIN for polled IO even though we're | |
6101 | * forcing a sync submission from here, since we can't | |
6102 | * wait for request slots on the block side. | |
6103 | */ | |
6104 | if (ret != -EAGAIN) | |
6105 | break; | |
6106 | cond_resched(); | |
6107 | } while (1); | |
6108 | } | |
31b51510 | 6109 | |
a3df7698 | 6110 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6111 | if (ret) { |
a3df7698 PB |
6112 | /* io-wq is going to take one down */ |
6113 | refcount_inc(&req->refs); | |
6114 | io_req_task_queue_fail(req, ret); | |
edafccee | 6115 | } |
2b188cc1 JA |
6116 | } |
6117 | ||
65e19f54 JA |
6118 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6119 | int index) | |
6120 | { | |
269bbe5f | 6121 | struct fixed_rsrc_table *table; |
65e19f54 | 6122 | |
05f3fb3c | 6123 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6124 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6125 | } |
6126 | ||
8371adf5 PB |
6127 | static struct file *io_file_get(struct io_submit_state *state, |
6128 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6129 | { |
a197f664 | 6130 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6131 | struct file *file; |
09bb8394 | 6132 | |
8da11c19 | 6133 | if (fixed) { |
479f517b | 6134 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6135 | return NULL; |
b7620121 | 6136 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6137 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6138 | io_set_resource_node(req); |
09bb8394 | 6139 | } else { |
c826bd7a | 6140 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6141 | file = __io_file_get(state, fd); |
09bb8394 JA |
6142 | } |
6143 | ||
ce3d5aae PB |
6144 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6145 | io_req_track_inflight(req); | |
8371adf5 | 6146 | return file; |
09bb8394 JA |
6147 | } |
6148 | ||
2665abfd | 6149 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6150 | { |
ad8a48ac JA |
6151 | struct io_timeout_data *data = container_of(timer, |
6152 | struct io_timeout_data, timer); | |
90cd7e42 | 6153 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6154 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6155 | unsigned long flags; |
2665abfd JA |
6156 | |
6157 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6158 | prev = req->timeout.head; |
6159 | req->timeout.head = NULL; | |
2665abfd JA |
6160 | |
6161 | /* | |
6162 | * We don't expect the list to be empty, that will only happen if we | |
6163 | * race with the completion of the linked work. | |
6164 | */ | |
90cd7e42 | 6165 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6166 | io_remove_next_linked(prev); |
90cd7e42 PB |
6167 | else |
6168 | prev = NULL; | |
2665abfd JA |
6169 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6170 | ||
6171 | if (prev) { | |
4e88d6e7 | 6172 | req_set_fail_links(prev); |
014db007 | 6173 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6174 | io_put_req_deferred(prev, 1); |
47f46768 | 6175 | } else { |
9ae1f8dd PB |
6176 | io_req_complete_post(req, -ETIME, 0); |
6177 | io_put_req_deferred(req, 1); | |
2665abfd | 6178 | } |
2665abfd JA |
6179 | return HRTIMER_NORESTART; |
6180 | } | |
6181 | ||
7271ef3a | 6182 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6183 | { |
76a46e06 | 6184 | /* |
f2f87370 PB |
6185 | * If the back reference is NULL, then our linked request finished |
6186 | * before we got a chance to setup the timer | |
76a46e06 | 6187 | */ |
90cd7e42 | 6188 | if (req->timeout.head) { |
e8c2bc1f | 6189 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6190 | |
ad8a48ac JA |
6191 | data->timer.function = io_link_timeout_fn; |
6192 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6193 | data->mode); | |
2665abfd | 6194 | } |
7271ef3a JA |
6195 | } |
6196 | ||
6197 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6198 | { | |
6199 | struct io_ring_ctx *ctx = req->ctx; | |
6200 | ||
6201 | spin_lock_irq(&ctx->completion_lock); | |
6202 | __io_queue_linked_timeout(req); | |
76a46e06 | 6203 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6204 | |
2665abfd | 6205 | /* drop submission reference */ |
76a46e06 JA |
6206 | io_put_req(req); |
6207 | } | |
2665abfd | 6208 | |
ad8a48ac | 6209 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6210 | { |
f2f87370 | 6211 | struct io_kiocb *nxt = req->link; |
2665abfd | 6212 | |
f2f87370 PB |
6213 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6214 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6215 | return NULL; |
2665abfd | 6216 | |
90cd7e42 | 6217 | nxt->timeout.head = req; |
900fad45 | 6218 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6219 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6220 | return nxt; |
2665abfd JA |
6221 | } |
6222 | ||
c5eef2b9 | 6223 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6224 | { |
d3d7298d | 6225 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
193155c8 | 6226 | const struct cred *old_creds = NULL; |
e0c5c576 | 6227 | int ret; |
2b188cc1 | 6228 | |
4379bf8b JA |
6229 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
6230 | req->work.creds != current_cred()) | |
6231 | old_creds = override_creds(req->work.creds); | |
193155c8 | 6232 | |
c5eef2b9 | 6233 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6234 | |
d3d7298d PB |
6235 | if (old_creds) |
6236 | revert_creds(old_creds); | |
491381ce JA |
6237 | |
6238 | /* | |
6239 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6240 | * doesn't support non-blocking read/write attempts | |
6241 | */ | |
24c74678 | 6242 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6243 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6244 | /* |
6245 | * Queued up for async execution, worker will release | |
6246 | * submit reference when the iocb is actually submitted. | |
6247 | */ | |
6248 | io_queue_async_work(req); | |
2b188cc1 | 6249 | } |
0d63c148 PB |
6250 | } else if (likely(!ret)) { |
6251 | /* drop submission reference */ | |
e342c807 | 6252 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6253 | struct io_ring_ctx *ctx = req->ctx; |
6254 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6255 | |
6dd0be1e | 6256 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6257 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6258 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6259 | } else { |
d3d7298d | 6260 | io_put_req(req); |
0d63c148 PB |
6261 | } |
6262 | } else { | |
4e88d6e7 | 6263 | req_set_fail_links(req); |
e65ef56d | 6264 | io_put_req(req); |
e1e16097 | 6265 | io_req_complete(req, ret); |
9e645e11 | 6266 | } |
d3d7298d PB |
6267 | if (linked_timeout) |
6268 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6269 | } |
6270 | ||
be7053b7 | 6271 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6272 | { |
6273 | int ret; | |
6274 | ||
be7053b7 | 6275 | ret = io_req_defer(req); |
4fe2c963 JL |
6276 | if (ret) { |
6277 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6278 | fail_req: |
4e88d6e7 | 6279 | req_set_fail_links(req); |
e1e16097 JA |
6280 | io_put_req(req); |
6281 | io_req_complete(req, ret); | |
4fe2c963 | 6282 | } |
2550878f | 6283 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6284 | ret = io_req_defer_prep(req); |
6285 | if (unlikely(ret)) | |
6286 | goto fail_req; | |
ce35a47a JA |
6287 | io_queue_async_work(req); |
6288 | } else { | |
c5eef2b9 | 6289 | __io_queue_sqe(req); |
ce35a47a | 6290 | } |
4fe2c963 JL |
6291 | } |
6292 | ||
b16fed66 PB |
6293 | /* |
6294 | * Check SQE restrictions (opcode and flags). | |
6295 | * | |
6296 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6297 | */ | |
6298 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6299 | struct io_kiocb *req, | |
6300 | unsigned int sqe_flags) | |
4fe2c963 | 6301 | { |
b16fed66 PB |
6302 | if (!ctx->restricted) |
6303 | return true; | |
6304 | ||
6305 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6306 | return false; | |
6307 | ||
6308 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6309 | ctx->restrictions.sqe_flags_required) | |
6310 | return false; | |
6311 | ||
6312 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6313 | ctx->restrictions.sqe_flags_required)) | |
6314 | return false; | |
6315 | ||
6316 | return true; | |
4fe2c963 JL |
6317 | } |
6318 | ||
b16fed66 PB |
6319 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6320 | const struct io_uring_sqe *sqe) | |
6321 | { | |
6322 | struct io_submit_state *state; | |
6323 | unsigned int sqe_flags; | |
6324 | int id, ret = 0; | |
6325 | ||
6326 | req->opcode = READ_ONCE(sqe->opcode); | |
6327 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6328 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6329 | req->user_data = READ_ONCE(sqe->user_data); | |
6330 | req->async_data = NULL; | |
6331 | req->file = NULL; | |
6332 | req->ctx = ctx; | |
6333 | req->link = NULL; | |
6334 | req->fixed_rsrc_refs = NULL; | |
6335 | /* one is dropped after submission, the other at completion */ | |
6336 | refcount_set(&req->refs, 2); | |
6337 | req->task = current; | |
6338 | req->result = 0; | |
863e0560 | 6339 | |
b16fed66 | 6340 | /* enforce forwards compatibility on users */ |
ebf4a5db PB |
6341 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6342 | req->flags = 0; | |
b16fed66 | 6343 | return -EINVAL; |
ebf4a5db | 6344 | } |
b16fed66 PB |
6345 | |
6346 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6347 | return -EINVAL; | |
6348 | ||
b16fed66 PB |
6349 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6350 | return -EACCES; | |
6351 | ||
6352 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6353 | !io_op_defs[req->opcode].buffer_select) | |
6354 | return -EOPNOTSUPP; | |
6355 | ||
6356 | id = READ_ONCE(sqe->personality); | |
6357 | if (id) { | |
b16fed66 | 6358 | __io_req_init_async(req); |
4379bf8b JA |
6359 | req->work.creds = idr_find(&ctx->personality_idr, id); |
6360 | if (unlikely(!req->work.creds)) | |
6361 | return -EINVAL; | |
6362 | get_cred(req->work.creds); | |
b16fed66 PB |
6363 | } |
6364 | ||
6365 | state = &ctx->submit_state; | |
6366 | ||
6367 | /* | |
6368 | * Plug now if we have more than 1 IO left after this, and the target | |
6369 | * is potentially a read/write to block based storage. | |
6370 | */ | |
6371 | if (!state->plug_started && state->ios_left > 1 && | |
6372 | io_op_defs[req->opcode].plug) { | |
6373 | blk_start_plug(&state->plug); | |
6374 | state->plug_started = true; | |
6375 | } | |
6376 | ||
6377 | if (io_op_defs[req->opcode].needs_file) { | |
6378 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6379 | ||
6380 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6381 | if (unlikely(!req->file)) | |
6382 | ret = -EBADF; | |
6383 | } | |
6384 | ||
6385 | state->ios_left--; | |
6386 | return ret; | |
6387 | } | |
6388 | ||
a6b8cadc | 6389 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6390 | const struct io_uring_sqe *sqe) |
9e645e11 | 6391 | { |
a1ab7b35 | 6392 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6393 | int ret; |
9e645e11 | 6394 | |
a6b8cadc PB |
6395 | ret = io_init_req(ctx, req, sqe); |
6396 | if (unlikely(ret)) { | |
6397 | fail_req: | |
6398 | io_put_req(req); | |
6399 | io_req_complete(req, ret); | |
de59bc10 PB |
6400 | if (link->head) { |
6401 | /* fail even hard links since we don't submit */ | |
cf109604 | 6402 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6403 | io_put_req(link->head); |
6404 | io_req_complete(link->head, -ECANCELED); | |
6405 | link->head = NULL; | |
6406 | } | |
a6b8cadc PB |
6407 | return ret; |
6408 | } | |
be7053b7 PB |
6409 | ret = io_req_prep(req, sqe); |
6410 | if (unlikely(ret)) | |
6411 | goto fail_req; | |
a6b8cadc | 6412 | |
be7053b7 | 6413 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6414 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6415 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6416 | ||
9e645e11 JA |
6417 | /* |
6418 | * If we already have a head request, queue this one for async | |
6419 | * submittal once the head completes. If we don't have a head but | |
6420 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6421 | * submitted sync once the chain is complete. If none of those | |
6422 | * conditions are true (normal request), then just queue it. | |
6423 | */ | |
863e0560 PB |
6424 | if (link->head) { |
6425 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6426 | |
8cdf2193 PB |
6427 | /* |
6428 | * Taking sequential execution of a link, draining both sides | |
6429 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6430 | * requests in the link. So, it drains the head and the | |
6431 | * next after the link request. The last one is done via | |
6432 | * drain_next flag to persist the effect across calls. | |
6433 | */ | |
ef4ff581 | 6434 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6435 | head->flags |= REQ_F_IO_DRAIN; |
6436 | ctx->drain_next = 1; | |
6437 | } | |
be7053b7 | 6438 | ret = io_req_defer_prep(req); |
cf109604 | 6439 | if (unlikely(ret)) |
a6b8cadc | 6440 | goto fail_req; |
9d76377f | 6441 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6442 | link->last->link = req; |
863e0560 | 6443 | link->last = req; |
32fe525b PB |
6444 | |
6445 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6446 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6447 | io_queue_sqe(head); |
863e0560 | 6448 | link->head = NULL; |
32fe525b | 6449 | } |
9e645e11 | 6450 | } else { |
711be031 PB |
6451 | if (unlikely(ctx->drain_next)) { |
6452 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6453 | ctx->drain_next = 0; |
711be031 | 6454 | } |
ef4ff581 | 6455 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6456 | link->head = req; |
6457 | link->last = req; | |
711be031 | 6458 | } else { |
be7053b7 | 6459 | io_queue_sqe(req); |
711be031 | 6460 | } |
9e645e11 | 6461 | } |
2e6e1fde | 6462 | |
1d4240cc | 6463 | return 0; |
9e645e11 JA |
6464 | } |
6465 | ||
9a56a232 JA |
6466 | /* |
6467 | * Batched submission is done, ensure local IO is flushed out. | |
6468 | */ | |
ba88ff11 PB |
6469 | static void io_submit_state_end(struct io_submit_state *state, |
6470 | struct io_ring_ctx *ctx) | |
9a56a232 | 6471 | { |
a1ab7b35 | 6472 | if (state->link.head) |
de59bc10 | 6473 | io_queue_sqe(state->link.head); |
6dd0be1e | 6474 | if (state->comp.nr) |
ba88ff11 | 6475 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6476 | if (state->plug_started) |
6477 | blk_finish_plug(&state->plug); | |
9f13c35b | 6478 | io_state_file_put(state); |
9a56a232 JA |
6479 | } |
6480 | ||
6481 | /* | |
6482 | * Start submission side cache. | |
6483 | */ | |
6484 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6485 | unsigned int max_ios) |
9a56a232 | 6486 | { |
27926b68 | 6487 | state->plug_started = false; |
9a56a232 | 6488 | state->ios_left = max_ios; |
a1ab7b35 PB |
6489 | /* set only head, no need to init link_last in advance */ |
6490 | state->link.head = NULL; | |
9a56a232 JA |
6491 | } |
6492 | ||
2b188cc1 JA |
6493 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6494 | { | |
75b28aff | 6495 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6496 | |
caf582c6 PB |
6497 | /* |
6498 | * Ensure any loads from the SQEs are done at this point, | |
6499 | * since once we write the new head, the application could | |
6500 | * write new data to them. | |
6501 | */ | |
6502 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6503 | } |
6504 | ||
2b188cc1 | 6505 | /* |
3529d8c2 | 6506 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6507 | * that is mapped by userspace. This means that care needs to be taken to |
6508 | * ensure that reads are stable, as we cannot rely on userspace always | |
6509 | * being a good citizen. If members of the sqe are validated and then later | |
6510 | * used, it's important that those reads are done through READ_ONCE() to | |
6511 | * prevent a re-load down the line. | |
6512 | */ | |
709b302f | 6513 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6514 | { |
75b28aff | 6515 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6516 | unsigned head; |
6517 | ||
6518 | /* | |
6519 | * The cached sq head (or cq tail) serves two purposes: | |
6520 | * | |
6521 | * 1) allows us to batch the cost of updating the user visible | |
6522 | * head updates. | |
6523 | * 2) allows the kernel side to track the head on its own, even | |
6524 | * though the application is the one updating it. | |
6525 | */ | |
4fccfcbb | 6526 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6527 | if (likely(head < ctx->sq_entries)) |
6528 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6529 | |
6530 | /* drop invalid entries */ | |
498ccd9e | 6531 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6532 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6533 | return NULL; |
6534 | } | |
6535 | ||
0f212204 | 6536 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6537 | { |
46c4e16a | 6538 | int submitted = 0; |
6c271ce2 | 6539 | |
c4a2ed72 | 6540 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6541 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6542 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6543 | return -EBUSY; |
6544 | } | |
6c271ce2 | 6545 | |
ee7d46d9 PB |
6546 | /* make sure SQ entry isn't read before tail */ |
6547 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6548 | |
2b85edfc PB |
6549 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6550 | return -EAGAIN; | |
6c271ce2 | 6551 | |
d8a6df10 | 6552 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6553 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6554 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6555 | |
46c4e16a | 6556 | while (submitted < nr) { |
3529d8c2 | 6557 | const struct io_uring_sqe *sqe; |
196be95c | 6558 | struct io_kiocb *req; |
fb5ccc98 | 6559 | |
258b29a9 | 6560 | req = io_alloc_req(ctx); |
196be95c PB |
6561 | if (unlikely(!req)) { |
6562 | if (!submitted) | |
6563 | submitted = -EAGAIN; | |
fb5ccc98 | 6564 | break; |
196be95c | 6565 | } |
4fccfcbb PB |
6566 | sqe = io_get_sqe(ctx); |
6567 | if (unlikely(!sqe)) { | |
6568 | kmem_cache_free(req_cachep, req); | |
6569 | break; | |
6570 | } | |
d3656344 JA |
6571 | /* will complete beyond this point, count as submitted */ |
6572 | submitted++; | |
a1ab7b35 | 6573 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6574 | break; |
6c271ce2 JA |
6575 | } |
6576 | ||
9466f437 PB |
6577 | if (unlikely(submitted != nr)) { |
6578 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6579 | struct io_uring_task *tctx = current->io_uring; |
6580 | int unused = nr - ref_used; | |
9466f437 | 6581 | |
d8a6df10 JA |
6582 | percpu_ref_put_many(&ctx->refs, unused); |
6583 | percpu_counter_sub(&tctx->inflight, unused); | |
6584 | put_task_struct_many(current, unused); | |
9466f437 | 6585 | } |
6c271ce2 | 6586 | |
a1ab7b35 | 6587 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6588 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6589 | io_commit_sqring(ctx); | |
6590 | ||
6c271ce2 JA |
6591 | return submitted; |
6592 | } | |
6593 | ||
23b3628e XW |
6594 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6595 | { | |
6596 | /* Tell userspace we may need a wakeup call */ | |
6597 | spin_lock_irq(&ctx->completion_lock); | |
6598 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6599 | spin_unlock_irq(&ctx->completion_lock); | |
6600 | } | |
6601 | ||
6602 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6603 | { | |
6604 | spin_lock_irq(&ctx->completion_lock); | |
6605 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6606 | spin_unlock_irq(&ctx->completion_lock); | |
6607 | } | |
6608 | ||
08369246 | 6609 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6610 | { |
c8d1ba58 | 6611 | unsigned int to_submit; |
bdcd3eab | 6612 | int ret = 0; |
6c271ce2 | 6613 | |
c8d1ba58 | 6614 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6615 | /* if we're handling multiple rings, cap submit size for fairness */ |
6616 | if (cap_entries && to_submit > 8) | |
6617 | to_submit = 8; | |
6618 | ||
906a3c6f | 6619 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6620 | unsigned nr_events = 0; |
a4c0b3de | 6621 | |
c8d1ba58 | 6622 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6623 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6624 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6625 | |
d9d05217 PB |
6626 | if (to_submit && !ctx->sqo_dead && |
6627 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6628 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6629 | mutex_unlock(&ctx->uring_lock); |
6630 | } | |
6c271ce2 | 6631 | |
90554200 JA |
6632 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6633 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6634 | |
08369246 XW |
6635 | return ret; |
6636 | } | |
6c271ce2 | 6637 | |
08369246 XW |
6638 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6639 | { | |
6640 | struct io_ring_ctx *ctx; | |
6641 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6642 | |
08369246 XW |
6643 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6644 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6645 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6646 | } |
c1edbf5f | 6647 | |
08369246 | 6648 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6649 | } |
6c271ce2 | 6650 | |
69fb2131 JA |
6651 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6652 | { | |
6653 | struct io_ring_ctx *ctx; | |
6654 | ||
6655 | while (!list_empty(&sqd->ctx_new_list)) { | |
6656 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6657 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6658 | complete(&ctx->sq_thread_comp); | |
6659 | } | |
08369246 XW |
6660 | |
6661 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6662 | } |
6663 | ||
37d1e2e3 JA |
6664 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
6665 | { | |
6666 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
6667 | } | |
6668 | ||
6669 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) | |
6670 | { | |
6671 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
6672 | } | |
6673 | ||
6674 | static void io_sq_thread_parkme(struct io_sq_data *sqd) | |
6675 | { | |
6676 | for (;;) { | |
6677 | /* | |
6678 | * TASK_PARKED is a special state; we must serialize against | |
6679 | * possible pending wakeups to avoid store-store collisions on | |
6680 | * task->state. | |
6681 | * | |
6682 | * Such a collision might possibly result in the task state | |
6683 | * changin from TASK_PARKED and us failing the | |
6684 | * wait_task_inactive() in kthread_park(). | |
6685 | */ | |
6686 | set_special_state(TASK_PARKED); | |
6687 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) | |
6688 | break; | |
6689 | ||
6690 | /* | |
6691 | * Thread is going to call schedule(), do not preempt it, | |
6692 | * or the caller of kthread_park() may spend more time in | |
6693 | * wait_task_inactive(). | |
6694 | */ | |
6695 | preempt_disable(); | |
6696 | complete(&sqd->completion); | |
6697 | schedule_preempt_disabled(); | |
6698 | preempt_enable(); | |
6699 | } | |
6700 | __set_current_state(TASK_RUNNING); | |
6701 | } | |
6702 | ||
c8d1ba58 JA |
6703 | static int io_sq_thread(void *data) |
6704 | { | |
69fb2131 JA |
6705 | struct io_sq_data *sqd = data; |
6706 | struct io_ring_ctx *ctx; | |
a0d9205f | 6707 | unsigned long timeout = 0; |
37d1e2e3 | 6708 | char buf[TASK_COMM_LEN]; |
08369246 | 6709 | DEFINE_WAIT(wait); |
6c271ce2 | 6710 | |
37d1e2e3 JA |
6711 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
6712 | set_task_comm(current, buf); | |
6713 | sqd->thread = current; | |
6714 | current->pf_io_worker = NULL; | |
6715 | ||
6716 | if (sqd->sq_cpu != -1) | |
6717 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6718 | else | |
6719 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6720 | current->flags |= PF_NO_SETAFFINITY; | |
6721 | ||
6722 | complete(&sqd->completion); | |
6c271ce2 | 6723 | |
37d1e2e3 JA |
6724 | wait_for_completion(&sqd->startup); |
6725 | ||
6726 | while (!io_sq_thread_should_stop(sqd)) { | |
08369246 XW |
6727 | int ret; |
6728 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
6729 | |
6730 | /* | |
69fb2131 | 6731 | * Any changes to the sqd lists are synchronized through the |
37d1e2e3 | 6732 | * thread parking. This synchronizes the thread vs users, |
69fb2131 | 6733 | * the users are synchronized on the sqd->ctx_lock. |
c1edbf5f | 6734 | */ |
37d1e2e3 JA |
6735 | if (io_sq_thread_should_park(sqd)) { |
6736 | io_sq_thread_parkme(sqd); | |
6737 | continue; | |
65b2b213 | 6738 | } |
08369246 | 6739 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 6740 | io_sqd_init_new(sqd); |
08369246 XW |
6741 | timeout = jiffies + sqd->sq_thread_idle; |
6742 | } | |
37d1e2e3 JA |
6743 | if (fatal_signal_pending(current)) |
6744 | break; | |
08369246 | 6745 | sqt_spin = false; |
e95eee2d | 6746 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6747 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
08369246 XW |
6748 | ret = __io_sq_thread(ctx, cap_entries); |
6749 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
6750 | sqt_spin = true; | |
69fb2131 | 6751 | } |
6c271ce2 | 6752 | |
08369246 | 6753 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6754 | io_run_task_work(); |
6755 | cond_resched(); | |
08369246 XW |
6756 | if (sqt_spin) |
6757 | timeout = jiffies + sqd->sq_thread_idle; | |
6758 | continue; | |
6759 | } | |
6760 | ||
08369246 XW |
6761 | needs_sched = true; |
6762 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6763 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6764 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6765 | !list_empty_careful(&ctx->iopoll_list)) { | |
6766 | needs_sched = false; | |
6767 | break; | |
6768 | } | |
6769 | if (io_sqring_entries(ctx)) { | |
6770 | needs_sched = false; | |
6771 | break; | |
6772 | } | |
6773 | } | |
6774 | ||
37d1e2e3 | 6775 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
69fb2131 JA |
6776 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6777 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6778 | |
69fb2131 | 6779 | schedule(); |
69fb2131 JA |
6780 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6781 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6782 | } |
08369246 XW |
6783 | |
6784 | finish_wait(&sqd->wait, &wait); | |
6785 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
6786 | } |
6787 | ||
37d1e2e3 JA |
6788 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6789 | io_uring_cancel_sqpoll(ctx); | |
b41e9852 | 6790 | |
37d1e2e3 | 6791 | io_run_task_work(); |
28cea78a | 6792 | |
37d1e2e3 JA |
6793 | /* |
6794 | * Clear thread under lock so that concurrent parks work correctly | |
6795 | */ | |
6796 | complete_all(&sqd->completion); | |
6797 | mutex_lock(&sqd->lock); | |
6798 | sqd->thread = NULL; | |
6799 | mutex_unlock(&sqd->lock); | |
06058632 | 6800 | |
37d1e2e3 JA |
6801 | complete(&sqd->exited); |
6802 | do_exit(0); | |
6c271ce2 JA |
6803 | } |
6804 | ||
bda52162 JA |
6805 | struct io_wait_queue { |
6806 | struct wait_queue_entry wq; | |
6807 | struct io_ring_ctx *ctx; | |
6808 | unsigned to_wait; | |
6809 | unsigned nr_timeouts; | |
6810 | }; | |
6811 | ||
6c503150 | 6812 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6813 | { |
6814 | struct io_ring_ctx *ctx = iowq->ctx; | |
6815 | ||
6816 | /* | |
d195a66e | 6817 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6818 | * started waiting. For timeouts, we always want to return to userspace, |
6819 | * regardless of event count. | |
6820 | */ | |
6c503150 | 6821 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6822 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6823 | } | |
6824 | ||
6825 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6826 | int wake_flags, void *key) | |
6827 | { | |
6828 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6829 | wq); | |
6830 | ||
6c503150 PB |
6831 | /* |
6832 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6833 | * the task, and the next invocation will do it. | |
6834 | */ | |
6835 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6836 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6837 | return -1; | |
bda52162 JA |
6838 | } |
6839 | ||
af9c1a44 JA |
6840 | static int io_run_task_work_sig(void) |
6841 | { | |
6842 | if (io_run_task_work()) | |
6843 | return 1; | |
6844 | if (!signal_pending(current)) | |
6845 | return 0; | |
792ee0f6 JA |
6846 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
6847 | return -ERESTARTSYS; | |
af9c1a44 JA |
6848 | return -EINTR; |
6849 | } | |
6850 | ||
eeb60b9a PB |
6851 | /* when returns >0, the caller should retry */ |
6852 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6853 | struct io_wait_queue *iowq, | |
6854 | signed long *timeout) | |
6855 | { | |
6856 | int ret; | |
6857 | ||
6858 | /* make sure we run task_work before checking for signals */ | |
6859 | ret = io_run_task_work_sig(); | |
6860 | if (ret || io_should_wake(iowq)) | |
6861 | return ret; | |
6862 | /* let the caller flush overflows, retry */ | |
6863 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6864 | return 1; | |
6865 | ||
6866 | *timeout = schedule_timeout(*timeout); | |
6867 | return !*timeout ? -ETIME : 1; | |
6868 | } | |
6869 | ||
2b188cc1 JA |
6870 | /* |
6871 | * Wait until events become available, if we don't already have some. The | |
6872 | * application must reap them itself, as they reside on the shared cq ring. | |
6873 | */ | |
6874 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
6875 | const sigset_t __user *sig, size_t sigsz, |
6876 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 6877 | { |
bda52162 JA |
6878 | struct io_wait_queue iowq = { |
6879 | .wq = { | |
6880 | .private = current, | |
6881 | .func = io_wake_function, | |
6882 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6883 | }, | |
6884 | .ctx = ctx, | |
6885 | .to_wait = min_events, | |
6886 | }; | |
75b28aff | 6887 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
6888 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
6889 | int ret; | |
2b188cc1 | 6890 | |
b41e9852 | 6891 | do { |
6c503150 PB |
6892 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6893 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 6894 | return 0; |
4c6e277c | 6895 | if (!io_run_task_work()) |
b41e9852 | 6896 | break; |
b41e9852 | 6897 | } while (1); |
2b188cc1 JA |
6898 | |
6899 | if (sig) { | |
9e75ad5d AB |
6900 | #ifdef CONFIG_COMPAT |
6901 | if (in_compat_syscall()) | |
6902 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6903 | sigsz); |
9e75ad5d AB |
6904 | else |
6905 | #endif | |
b772434b | 6906 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6907 | |
2b188cc1 JA |
6908 | if (ret) |
6909 | return ret; | |
6910 | } | |
6911 | ||
c73ebb68 | 6912 | if (uts) { |
c1d5a224 PB |
6913 | struct timespec64 ts; |
6914 | ||
c73ebb68 HX |
6915 | if (get_timespec64(&ts, uts)) |
6916 | return -EFAULT; | |
6917 | timeout = timespec64_to_jiffies(&ts); | |
6918 | } | |
6919 | ||
bda52162 | 6920 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6921 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 6922 | do { |
6c503150 | 6923 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
6924 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
6925 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
6926 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
6927 | finish_wait(&ctx->wait, &iowq.wq); | |
6928 | } while (ret > 0); | |
bda52162 | 6929 | |
b7db41c9 | 6930 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6931 | |
75b28aff | 6932 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6933 | } |
6934 | ||
6b06314c JA |
6935 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6936 | { | |
6937 | #if defined(CONFIG_UNIX) | |
6938 | if (ctx->ring_sock) { | |
6939 | struct sock *sock = ctx->ring_sock->sk; | |
6940 | struct sk_buff *skb; | |
6941 | ||
6942 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6943 | kfree_skb(skb); | |
6944 | } | |
6945 | #else | |
6946 | int i; | |
6947 | ||
65e19f54 JA |
6948 | for (i = 0; i < ctx->nr_user_files; i++) { |
6949 | struct file *file; | |
6950 | ||
6951 | file = io_file_from_index(ctx, i); | |
6952 | if (file) | |
6953 | fput(file); | |
6954 | } | |
6b06314c JA |
6955 | #endif |
6956 | } | |
6957 | ||
00835dce | 6958 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 6959 | { |
269bbe5f | 6960 | struct fixed_rsrc_data *data; |
05f3fb3c | 6961 | |
269bbe5f | 6962 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
6963 | complete(&data->done); |
6964 | } | |
6965 | ||
2a63b2d9 | 6966 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 6967 | { |
2a63b2d9 | 6968 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
6969 | } |
6970 | ||
2a63b2d9 | 6971 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 6972 | { |
2a63b2d9 BM |
6973 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
6974 | } | |
65e19f54 | 6975 | |
d67d2263 BM |
6976 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
6977 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 6978 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 6979 | { |
2a63b2d9 | 6980 | io_rsrc_ref_lock(ctx); |
269bbe5f | 6981 | rsrc_data->node = ref_node; |
d67d2263 | 6982 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 6983 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 6984 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
6985 | } |
6986 | ||
8bad28d8 | 6987 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 6988 | { |
8bad28d8 | 6989 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 6990 | |
2a63b2d9 | 6991 | io_rsrc_ref_lock(ctx); |
1e5d770b | 6992 | ref_node = data->node; |
e6cb007c | 6993 | data->node = NULL; |
2a63b2d9 | 6994 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
6995 | if (ref_node) |
6996 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 | 6997 | } |
05589553 | 6998 | |
8bad28d8 HX |
6999 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7000 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
7001 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
7002 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 7003 | { |
f2303b1f | 7004 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 7005 | int ret; |
05589553 | 7006 | |
8bad28d8 HX |
7007 | if (data->quiesce) |
7008 | return -ENXIO; | |
05589553 | 7009 | |
8bad28d8 | 7010 | data->quiesce = true; |
1ffc5422 | 7011 | do { |
f2303b1f PB |
7012 | ret = -ENOMEM; |
7013 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7014 | if (!backup_node) | |
7015 | break; | |
7016 | backup_node->rsrc_data = data; | |
7017 | backup_node->rsrc_put = rsrc_put; | |
7018 | ||
8bad28d8 HX |
7019 | io_sqe_rsrc_kill_node(ctx, data); |
7020 | percpu_ref_kill(&data->refs); | |
7021 | flush_delayed_work(&ctx->rsrc_put_work); | |
7022 | ||
1ffc5422 | 7023 | ret = wait_for_completion_interruptible(&data->done); |
88f171ab | 7024 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
1ffc5422 | 7025 | break; |
8bad28d8 | 7026 | |
8bad28d8 HX |
7027 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7028 | backup_node = NULL; | |
8bad28d8 | 7029 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7030 | ret = io_run_task_work_sig(); |
8bad28d8 | 7031 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7032 | } while (ret >= 0); |
8bad28d8 | 7033 | data->quiesce = false; |
05f3fb3c | 7034 | |
8bad28d8 HX |
7035 | if (backup_node) |
7036 | destroy_fixed_rsrc_ref_node(backup_node); | |
7037 | return ret; | |
d7954b2b BM |
7038 | } |
7039 | ||
1ad555c6 BM |
7040 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7041 | { | |
7042 | struct fixed_rsrc_data *data; | |
7043 | ||
7044 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7045 | if (!data) | |
7046 | return NULL; | |
7047 | ||
00835dce | 7048 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7049 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7050 | kfree(data); | |
7051 | return NULL; | |
7052 | } | |
7053 | data->ctx = ctx; | |
7054 | init_completion(&data->done); | |
7055 | return data; | |
7056 | } | |
7057 | ||
7058 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7059 | { | |
7060 | percpu_ref_exit(&data->refs); | |
7061 | kfree(data->table); | |
7062 | kfree(data); | |
7063 | } | |
7064 | ||
d7954b2b BM |
7065 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7066 | { | |
7067 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7068 | unsigned nr_tables, i; |
7069 | int ret; | |
7070 | ||
8bad28d8 HX |
7071 | /* |
7072 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7073 | * Since we possibly drop uring lock later in this function to | |
7074 | * run task work. | |
7075 | */ | |
7076 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7077 | return -ENXIO; |
f2303b1f | 7078 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7079 | if (ret) |
7080 | return ret; | |
7081 | ||
6b06314c | 7082 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7083 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7084 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7085 | kfree(data->table[i].files); |
1ad555c6 | 7086 | free_fixed_rsrc_data(data); |
05f3fb3c | 7087 | ctx->file_data = NULL; |
6b06314c JA |
7088 | ctx->nr_user_files = 0; |
7089 | return 0; | |
7090 | } | |
7091 | ||
37d1e2e3 JA |
7092 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7093 | __releases(&sqd->lock) | |
7094 | { | |
7095 | if (!sqd->thread) | |
7096 | return; | |
7097 | if (sqd->thread == current) | |
7098 | return; | |
7099 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7100 | wake_up_state(sqd->thread, TASK_PARKED); | |
7101 | mutex_unlock(&sqd->lock); | |
7102 | } | |
7103 | ||
7104 | static bool io_sq_thread_park(struct io_sq_data *sqd) | |
7105 | __acquires(&sqd->lock) | |
7106 | { | |
7107 | if (sqd->thread == current) | |
7108 | return true; | |
7109 | mutex_lock(&sqd->lock); | |
7110 | if (!sqd->thread) { | |
7111 | mutex_unlock(&sqd->lock); | |
7112 | return false; | |
7113 | } | |
7114 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7115 | wake_up_process(sqd->thread); | |
7116 | wait_for_completion(&sqd->completion); | |
7117 | return true; | |
7118 | } | |
7119 | ||
7120 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7121 | { | |
7122 | if (!sqd->thread) | |
7123 | return; | |
7124 | ||
7125 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
7126 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); | |
7127 | wake_up_process(sqd->thread); | |
7128 | wait_for_completion(&sqd->exited); | |
7129 | } | |
7130 | ||
534ca6d6 | 7131 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7132 | { |
534ca6d6 | 7133 | if (refcount_dec_and_test(&sqd->refs)) { |
37d1e2e3 JA |
7134 | io_sq_thread_stop(sqd); |
7135 | kfree(sqd); | |
7136 | } | |
7137 | } | |
7138 | ||
7139 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7140 | { | |
7141 | struct io_sq_data *sqd = ctx->sq_data; | |
7142 | ||
7143 | if (sqd) { | |
534ca6d6 | 7144 | if (sqd->thread) { |
37d1e2e3 JA |
7145 | wait_for_completion(&ctx->sq_thread_comp); |
7146 | io_sq_thread_park(sqd); | |
534ca6d6 JA |
7147 | } |
7148 | ||
37d1e2e3 JA |
7149 | mutex_lock(&sqd->ctx_lock); |
7150 | list_del(&ctx->sqd_list); | |
7151 | io_sqd_update_thread_idle(sqd); | |
7152 | mutex_unlock(&sqd->ctx_lock); | |
7153 | ||
7154 | if (sqd->thread) | |
7155 | io_sq_thread_unpark(sqd); | |
7156 | ||
7157 | io_put_sq_data(sqd); | |
7158 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7159 | } |
7160 | } | |
7161 | ||
aa06165d JA |
7162 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7163 | { | |
7164 | struct io_ring_ctx *ctx_attach; | |
7165 | struct io_sq_data *sqd; | |
7166 | struct fd f; | |
7167 | ||
7168 | f = fdget(p->wq_fd); | |
7169 | if (!f.file) | |
7170 | return ERR_PTR(-ENXIO); | |
7171 | if (f.file->f_op != &io_uring_fops) { | |
7172 | fdput(f); | |
7173 | return ERR_PTR(-EINVAL); | |
7174 | } | |
7175 | ||
7176 | ctx_attach = f.file->private_data; | |
7177 | sqd = ctx_attach->sq_data; | |
7178 | if (!sqd) { | |
7179 | fdput(f); | |
7180 | return ERR_PTR(-EINVAL); | |
7181 | } | |
7182 | ||
7183 | refcount_inc(&sqd->refs); | |
7184 | fdput(f); | |
7185 | return sqd; | |
7186 | } | |
7187 | ||
534ca6d6 JA |
7188 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7189 | { | |
7190 | struct io_sq_data *sqd; | |
7191 | ||
aa06165d JA |
7192 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7193 | return io_attach_sq_data(p); | |
7194 | ||
534ca6d6 JA |
7195 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7196 | if (!sqd) | |
7197 | return ERR_PTR(-ENOMEM); | |
7198 | ||
7199 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7200 | INIT_LIST_HEAD(&sqd->ctx_list); |
7201 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7202 | mutex_init(&sqd->ctx_lock); | |
7203 | mutex_init(&sqd->lock); | |
534ca6d6 | 7204 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 JA |
7205 | init_completion(&sqd->startup); |
7206 | init_completion(&sqd->completion); | |
7207 | init_completion(&sqd->exited); | |
534ca6d6 JA |
7208 | return sqd; |
7209 | } | |
7210 | ||
6b06314c | 7211 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7212 | /* |
7213 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7214 | * the io_uring can be safely unregistered on process exit, even if we have | |
7215 | * loops in the file referencing. | |
7216 | */ | |
7217 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7218 | { | |
7219 | struct sock *sk = ctx->ring_sock->sk; | |
7220 | struct scm_fp_list *fpl; | |
7221 | struct sk_buff *skb; | |
08a45173 | 7222 | int i, nr_files; |
6b06314c | 7223 | |
6b06314c JA |
7224 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7225 | if (!fpl) | |
7226 | return -ENOMEM; | |
7227 | ||
7228 | skb = alloc_skb(0, GFP_KERNEL); | |
7229 | if (!skb) { | |
7230 | kfree(fpl); | |
7231 | return -ENOMEM; | |
7232 | } | |
7233 | ||
7234 | skb->sk = sk; | |
6b06314c | 7235 | |
08a45173 | 7236 | nr_files = 0; |
62e398be | 7237 | fpl->user = get_uid(current_user()); |
6b06314c | 7238 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7239 | struct file *file = io_file_from_index(ctx, i + offset); |
7240 | ||
7241 | if (!file) | |
08a45173 | 7242 | continue; |
65e19f54 | 7243 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7244 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7245 | nr_files++; | |
6b06314c JA |
7246 | } |
7247 | ||
08a45173 JA |
7248 | if (nr_files) { |
7249 | fpl->max = SCM_MAX_FD; | |
7250 | fpl->count = nr_files; | |
7251 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7252 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7253 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7254 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7255 | |
08a45173 JA |
7256 | for (i = 0; i < nr_files; i++) |
7257 | fput(fpl->fp[i]); | |
7258 | } else { | |
7259 | kfree_skb(skb); | |
7260 | kfree(fpl); | |
7261 | } | |
6b06314c JA |
7262 | |
7263 | return 0; | |
7264 | } | |
7265 | ||
7266 | /* | |
7267 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7268 | * causes regular reference counting to break down. We rely on the UNIX | |
7269 | * garbage collection to take care of this problem for us. | |
7270 | */ | |
7271 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7272 | { | |
7273 | unsigned left, total; | |
7274 | int ret = 0; | |
7275 | ||
7276 | total = 0; | |
7277 | left = ctx->nr_user_files; | |
7278 | while (left) { | |
7279 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7280 | |
7281 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7282 | if (ret) | |
7283 | break; | |
7284 | left -= this_files; | |
7285 | total += this_files; | |
7286 | } | |
7287 | ||
7288 | if (!ret) | |
7289 | return 0; | |
7290 | ||
7291 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7292 | struct file *file = io_file_from_index(ctx, total); |
7293 | ||
7294 | if (file) | |
7295 | fput(file); | |
6b06314c JA |
7296 | total++; |
7297 | } | |
7298 | ||
7299 | return ret; | |
7300 | } | |
7301 | #else | |
7302 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7303 | { | |
7304 | return 0; | |
7305 | } | |
7306 | #endif | |
7307 | ||
269bbe5f | 7308 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7309 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7310 | { |
7311 | int i; | |
7312 | ||
7313 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7314 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7315 | unsigned this_files; |
7316 | ||
7317 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7318 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7319 | GFP_KERNEL); | |
7320 | if (!table->files) | |
7321 | break; | |
7322 | nr_files -= this_files; | |
7323 | } | |
7324 | ||
7325 | if (i == nr_tables) | |
7326 | return 0; | |
7327 | ||
7328 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7329 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7330 | kfree(table->files); |
7331 | } | |
7332 | return 1; | |
7333 | } | |
7334 | ||
50238531 | 7335 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7336 | { |
50238531 | 7337 | struct file *file = prsrc->file; |
05f3fb3c JA |
7338 | #if defined(CONFIG_UNIX) |
7339 | struct sock *sock = ctx->ring_sock->sk; | |
7340 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7341 | struct sk_buff *skb; | |
7342 | int i; | |
7343 | ||
7344 | __skb_queue_head_init(&list); | |
7345 | ||
7346 | /* | |
7347 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7348 | * remove this entry and rearrange the file array. | |
7349 | */ | |
7350 | skb = skb_dequeue(head); | |
7351 | while (skb) { | |
7352 | struct scm_fp_list *fp; | |
7353 | ||
7354 | fp = UNIXCB(skb).fp; | |
7355 | for (i = 0; i < fp->count; i++) { | |
7356 | int left; | |
7357 | ||
7358 | if (fp->fp[i] != file) | |
7359 | continue; | |
7360 | ||
7361 | unix_notinflight(fp->user, fp->fp[i]); | |
7362 | left = fp->count - 1 - i; | |
7363 | if (left) { | |
7364 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7365 | left * sizeof(struct file *)); | |
7366 | } | |
7367 | fp->count--; | |
7368 | if (!fp->count) { | |
7369 | kfree_skb(skb); | |
7370 | skb = NULL; | |
7371 | } else { | |
7372 | __skb_queue_tail(&list, skb); | |
7373 | } | |
7374 | fput(file); | |
7375 | file = NULL; | |
7376 | break; | |
7377 | } | |
7378 | ||
7379 | if (!file) | |
7380 | break; | |
7381 | ||
7382 | __skb_queue_tail(&list, skb); | |
7383 | ||
7384 | skb = skb_dequeue(head); | |
7385 | } | |
7386 | ||
7387 | if (skb_peek(&list)) { | |
7388 | spin_lock_irq(&head->lock); | |
7389 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7390 | __skb_queue_tail(head, skb); | |
7391 | spin_unlock_irq(&head->lock); | |
7392 | } | |
7393 | #else | |
7394 | fput(file); | |
7395 | #endif | |
7396 | } | |
7397 | ||
269bbe5f | 7398 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7399 | { |
269bbe5f BM |
7400 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7401 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7402 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7403 | |
269bbe5f BM |
7404 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7405 | list_del(&prsrc->list); | |
50238531 | 7406 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7407 | kfree(prsrc); |
65e19f54 | 7408 | } |
05589553 | 7409 | |
05589553 XW |
7410 | percpu_ref_exit(&ref_node->refs); |
7411 | kfree(ref_node); | |
269bbe5f | 7412 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7413 | } |
65e19f54 | 7414 | |
269bbe5f | 7415 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7416 | { |
7417 | struct io_ring_ctx *ctx; | |
7418 | struct llist_node *node; | |
7419 | ||
269bbe5f BM |
7420 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7421 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7422 | |
7423 | while (node) { | |
269bbe5f | 7424 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7425 | struct llist_node *next = node->next; |
7426 | ||
269bbe5f BM |
7427 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7428 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7429 | node = next; |
7430 | } | |
7431 | } | |
7432 | ||
ea64ec02 PB |
7433 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7434 | unsigned i) | |
2faf852d | 7435 | { |
ea64ec02 PB |
7436 | struct fixed_rsrc_table *table; |
7437 | ||
7438 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7439 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7440 | } | |
7441 | ||
00835dce | 7442 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7443 | { |
269bbe5f BM |
7444 | struct fixed_rsrc_ref_node *ref_node; |
7445 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7446 | struct io_ring_ctx *ctx; |
e297822b | 7447 | bool first_add = false; |
4a38aed2 | 7448 | int delay = HZ; |
65e19f54 | 7449 | |
269bbe5f BM |
7450 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7451 | data = ref_node->rsrc_data; | |
e297822b PB |
7452 | ctx = data->ctx; |
7453 | ||
2a63b2d9 | 7454 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7455 | ref_node->done = true; |
7456 | ||
d67d2263 BM |
7457 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7458 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7459 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7460 | /* recycle ref nodes in order */ |
7461 | if (!ref_node->done) | |
7462 | break; | |
7463 | list_del(&ref_node->node); | |
269bbe5f | 7464 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7465 | } |
2a63b2d9 | 7466 | io_rsrc_ref_unlock(ctx); |
05589553 | 7467 | |
e297822b | 7468 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7469 | delay = 0; |
05589553 | 7470 | |
4a38aed2 | 7471 | if (!delay) |
269bbe5f | 7472 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7473 | else if (first_add) |
269bbe5f | 7474 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7475 | } |
65e19f54 | 7476 | |
6802535d | 7477 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7478 | struct io_ring_ctx *ctx) |
05f3fb3c | 7479 | { |
269bbe5f | 7480 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7481 | |
05589553 XW |
7482 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7483 | if (!ref_node) | |
3e2224c5 | 7484 | return NULL; |
05f3fb3c | 7485 | |
00835dce | 7486 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7487 | 0, GFP_KERNEL)) { |
7488 | kfree(ref_node); | |
3e2224c5 | 7489 | return NULL; |
05589553 XW |
7490 | } |
7491 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7492 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7493 | ref_node->done = false; |
05589553 | 7494 | return ref_node; |
05589553 XW |
7495 | } |
7496 | ||
bc9744cd PB |
7497 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7498 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7499 | { |
269bbe5f | 7500 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7501 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7502 | } |
7503 | ||
269bbe5f | 7504 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7505 | { |
7506 | percpu_ref_exit(&ref_node->refs); | |
7507 | kfree(ref_node); | |
65e19f54 JA |
7508 | } |
7509 | ||
ea64ec02 | 7510 | |
6b06314c JA |
7511 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7512 | unsigned nr_args) | |
7513 | { | |
7514 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7515 | unsigned nr_tables, i; |
05f3fb3c | 7516 | struct file *file; |
600cf3f8 | 7517 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7518 | struct fixed_rsrc_ref_node *ref_node; |
7519 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7520 | |
05f3fb3c | 7521 | if (ctx->file_data) |
6b06314c JA |
7522 | return -EBUSY; |
7523 | if (!nr_args) | |
7524 | return -EINVAL; | |
7525 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7526 | return -EMFILE; | |
7527 | ||
1ad555c6 | 7528 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7529 | if (!file_data) |
05f3fb3c | 7530 | return -ENOMEM; |
13770a71 | 7531 | ctx->file_data = file_data; |
05f3fb3c | 7532 | |
65e19f54 | 7533 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7534 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7535 | GFP_KERNEL); |
600cf3f8 PB |
7536 | if (!file_data->table) |
7537 | goto out_free; | |
05f3fb3c | 7538 | |
600cf3f8 | 7539 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7540 | goto out_free; |
65e19f54 | 7541 | |
08a45173 | 7542 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7543 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7544 | ret = -EFAULT; | |
7545 | goto out_fput; | |
7546 | } | |
08a45173 | 7547 | /* allow sparse sets */ |
600cf3f8 | 7548 | if (fd == -1) |
08a45173 | 7549 | continue; |
6b06314c | 7550 | |
05f3fb3c | 7551 | file = fget(fd); |
6b06314c | 7552 | ret = -EBADF; |
05f3fb3c | 7553 | if (!file) |
600cf3f8 | 7554 | goto out_fput; |
05f3fb3c | 7555 | |
6b06314c JA |
7556 | /* |
7557 | * Don't allow io_uring instances to be registered. If UNIX | |
7558 | * isn't enabled, then this causes a reference cycle and this | |
7559 | * instance can never get freed. If UNIX is enabled we'll | |
7560 | * handle it just fine, but there's still no point in allowing | |
7561 | * a ring fd as it doesn't support regular read/write anyway. | |
7562 | */ | |
05f3fb3c JA |
7563 | if (file->f_op == &io_uring_fops) { |
7564 | fput(file); | |
600cf3f8 | 7565 | goto out_fput; |
6b06314c | 7566 | } |
ea64ec02 | 7567 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7568 | } |
7569 | ||
6b06314c | 7570 | ret = io_sqe_files_scm(ctx); |
05589553 | 7571 | if (ret) { |
6b06314c | 7572 | io_sqe_files_unregister(ctx); |
05589553 XW |
7573 | return ret; |
7574 | } | |
6b06314c | 7575 | |
bc9744cd | 7576 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7577 | if (!ref_node) { |
05589553 | 7578 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7579 | return -ENOMEM; |
05589553 | 7580 | } |
bc9744cd | 7581 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7582 | |
d67d2263 | 7583 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7584 | return ret; |
600cf3f8 PB |
7585 | out_fput: |
7586 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7587 | file = io_file_from_index(ctx, i); | |
7588 | if (file) | |
7589 | fput(file); | |
7590 | } | |
7591 | for (i = 0; i < nr_tables; i++) | |
7592 | kfree(file_data->table[i].files); | |
7593 | ctx->nr_user_files = 0; | |
600cf3f8 | 7594 | out_free: |
1ad555c6 | 7595 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7596 | ctx->file_data = NULL; |
6b06314c JA |
7597 | return ret; |
7598 | } | |
7599 | ||
c3a31e60 JA |
7600 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7601 | int index) | |
7602 | { | |
7603 | #if defined(CONFIG_UNIX) | |
7604 | struct sock *sock = ctx->ring_sock->sk; | |
7605 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7606 | struct sk_buff *skb; | |
7607 | ||
7608 | /* | |
7609 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7610 | * file set. If there's no room, fall back to allocating a new skb | |
7611 | * and filling it in. | |
7612 | */ | |
7613 | spin_lock_irq(&head->lock); | |
7614 | skb = skb_peek(head); | |
7615 | if (skb) { | |
7616 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7617 | ||
7618 | if (fpl->count < SCM_MAX_FD) { | |
7619 | __skb_unlink(skb, head); | |
7620 | spin_unlock_irq(&head->lock); | |
7621 | fpl->fp[fpl->count] = get_file(file); | |
7622 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7623 | fpl->count++; | |
7624 | spin_lock_irq(&head->lock); | |
7625 | __skb_queue_head(head, skb); | |
7626 | } else { | |
7627 | skb = NULL; | |
7628 | } | |
7629 | } | |
7630 | spin_unlock_irq(&head->lock); | |
7631 | ||
7632 | if (skb) { | |
7633 | fput(file); | |
7634 | return 0; | |
7635 | } | |
7636 | ||
7637 | return __io_sqe_files_scm(ctx, 1, index); | |
7638 | #else | |
7639 | return 0; | |
7640 | #endif | |
7641 | } | |
7642 | ||
50238531 | 7643 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7644 | { |
269bbe5f BM |
7645 | struct io_rsrc_put *prsrc; |
7646 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7647 | |
269bbe5f BM |
7648 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7649 | if (!prsrc) | |
a5318d3c | 7650 | return -ENOMEM; |
05f3fb3c | 7651 | |
50238531 | 7652 | prsrc->rsrc = rsrc; |
269bbe5f | 7653 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7654 | |
a5318d3c | 7655 | return 0; |
05f3fb3c JA |
7656 | } |
7657 | ||
269bbe5f BM |
7658 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7659 | struct file *file) | |
7660 | { | |
50238531 | 7661 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7662 | } |
7663 | ||
05f3fb3c | 7664 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7665 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7666 | unsigned nr_args) |
7667 | { | |
269bbe5f BM |
7668 | struct fixed_rsrc_data *data = ctx->file_data; |
7669 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7670 | struct file *file, **file_slot; |
c3a31e60 JA |
7671 | __s32 __user *fds; |
7672 | int fd, i, err; | |
7673 | __u32 done; | |
05589553 | 7674 | bool needs_switch = false; |
c3a31e60 | 7675 | |
05f3fb3c | 7676 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7677 | return -EOVERFLOW; |
7678 | if (done > ctx->nr_user_files) | |
7679 | return -EINVAL; | |
7680 | ||
bc9744cd | 7681 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
7682 | if (!ref_node) |
7683 | return -ENOMEM; | |
bc9744cd | 7684 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7685 | |
269bbe5f | 7686 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7687 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7688 | err = 0; |
7689 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7690 | err = -EFAULT; | |
7691 | break; | |
7692 | } | |
4e0377a1 | 7693 | if (fd == IORING_REGISTER_FILES_SKIP) |
7694 | continue; | |
7695 | ||
67973b93 | 7696 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7697 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7698 | ||
7699 | if (*file_slot) { | |
7700 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
7701 | if (err) |
7702 | break; | |
ea64ec02 | 7703 | *file_slot = NULL; |
05589553 | 7704 | needs_switch = true; |
c3a31e60 JA |
7705 | } |
7706 | if (fd != -1) { | |
c3a31e60 JA |
7707 | file = fget(fd); |
7708 | if (!file) { | |
7709 | err = -EBADF; | |
7710 | break; | |
7711 | } | |
7712 | /* | |
7713 | * Don't allow io_uring instances to be registered. If | |
7714 | * UNIX isn't enabled, then this causes a reference | |
7715 | * cycle and this instance can never get freed. If UNIX | |
7716 | * is enabled we'll handle it just fine, but there's | |
7717 | * still no point in allowing a ring fd as it doesn't | |
7718 | * support regular read/write anyway. | |
7719 | */ | |
7720 | if (file->f_op == &io_uring_fops) { | |
7721 | fput(file); | |
7722 | err = -EBADF; | |
7723 | break; | |
7724 | } | |
e68a3ff8 | 7725 | *file_slot = file; |
c3a31e60 | 7726 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7727 | if (err) { |
e68a3ff8 | 7728 | *file_slot = NULL; |
f3bd9dae | 7729 | fput(file); |
c3a31e60 | 7730 | break; |
f3bd9dae | 7731 | } |
c3a31e60 | 7732 | } |
05f3fb3c JA |
7733 | } |
7734 | ||
05589553 | 7735 | if (needs_switch) { |
b2e96852 | 7736 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 7737 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 7738 | } else |
269bbe5f | 7739 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
7740 | |
7741 | return done ? done : err; | |
7742 | } | |
05589553 | 7743 | |
05f3fb3c JA |
7744 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7745 | unsigned nr_args) | |
7746 | { | |
269bbe5f | 7747 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7748 | |
7749 | if (!ctx->file_data) | |
7750 | return -ENXIO; | |
7751 | if (!nr_args) | |
7752 | return -EINVAL; | |
7753 | if (copy_from_user(&up, arg, sizeof(up))) | |
7754 | return -EFAULT; | |
7755 | if (up.resv) | |
7756 | return -EINVAL; | |
7757 | ||
7758 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7759 | } | |
c3a31e60 | 7760 | |
5280f7e5 | 7761 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7762 | { |
7763 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7764 | ||
5280f7e5 PB |
7765 | req = io_put_req_find_next(req); |
7766 | return req ? &req->work : NULL; | |
7d723065 JA |
7767 | } |
7768 | ||
5aa75ed5 | 7769 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
24369c2e | 7770 | { |
e941894e | 7771 | struct io_wq_hash *hash; |
24369c2e | 7772 | struct io_wq_data data; |
24369c2e | 7773 | unsigned int concurrency; |
24369c2e | 7774 | |
e941894e JA |
7775 | hash = ctx->hash_map; |
7776 | if (!hash) { | |
7777 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
7778 | if (!hash) | |
7779 | return ERR_PTR(-ENOMEM); | |
7780 | refcount_set(&hash->refs, 1); | |
7781 | init_waitqueue_head(&hash->wait); | |
7782 | ctx->hash_map = hash; | |
7783 | } | |
7784 | ||
7785 | data.hash = hash; | |
e9fd9396 | 7786 | data.free_work = io_free_work; |
f5fa38c5 | 7787 | data.do_work = io_wq_submit_work; |
24369c2e | 7788 | |
d25e3a3d JA |
7789 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7790 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7791 | |
5aa75ed5 | 7792 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7793 | } |
7794 | ||
5aa75ed5 JA |
7795 | static int io_uring_alloc_task_context(struct task_struct *task, |
7796 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7797 | { |
7798 | struct io_uring_task *tctx; | |
d8a6df10 | 7799 | int ret; |
0f212204 JA |
7800 | |
7801 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7802 | if (unlikely(!tctx)) | |
7803 | return -ENOMEM; | |
7804 | ||
d8a6df10 JA |
7805 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7806 | if (unlikely(ret)) { | |
7807 | kfree(tctx); | |
7808 | return ret; | |
7809 | } | |
7810 | ||
5aa75ed5 JA |
7811 | tctx->io_wq = io_init_wq_offload(ctx); |
7812 | if (IS_ERR(tctx->io_wq)) { | |
7813 | ret = PTR_ERR(tctx->io_wq); | |
7814 | percpu_counter_destroy(&tctx->inflight); | |
7815 | kfree(tctx); | |
7816 | return ret; | |
7817 | } | |
7818 | ||
0f212204 JA |
7819 | xa_init(&tctx->xa); |
7820 | init_waitqueue_head(&tctx->wait); | |
7821 | tctx->last = NULL; | |
fdaf083c JA |
7822 | atomic_set(&tctx->in_idle, 0); |
7823 | tctx->sqpoll = false; | |
0f212204 | 7824 | task->io_uring = tctx; |
7cbf1722 JA |
7825 | spin_lock_init(&tctx->task_lock); |
7826 | INIT_WQ_LIST(&tctx->task_list); | |
7827 | tctx->task_state = 0; | |
7828 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7829 | return 0; |
7830 | } | |
7831 | ||
7832 | void __io_uring_free(struct task_struct *tsk) | |
7833 | { | |
7834 | struct io_uring_task *tctx = tsk->io_uring; | |
7835 | ||
7836 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
d8a6df10 | 7837 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7838 | kfree(tctx); |
7839 | tsk->io_uring = NULL; | |
7840 | } | |
7841 | ||
7e84e1c7 SG |
7842 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7843 | struct io_uring_params *p) | |
2b188cc1 JA |
7844 | { |
7845 | int ret; | |
7846 | ||
d25e3a3d JA |
7847 | /* Retain compatibility with failing for an invalid attach attempt */ |
7848 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7849 | IORING_SETUP_ATTACH_WQ) { | |
7850 | struct fd f; | |
7851 | ||
7852 | f = fdget(p->wq_fd); | |
7853 | if (!f.file) | |
7854 | return -ENXIO; | |
7855 | if (f.file->f_op != &io_uring_fops) { | |
7856 | fdput(f); | |
7857 | return -EINVAL; | |
7858 | } | |
7859 | fdput(f); | |
7860 | } | |
6c271ce2 | 7861 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
7862 | struct io_sq_data *sqd; |
7863 | ||
3ec482d1 | 7864 | ret = -EPERM; |
ce59fc69 | 7865 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
7866 | goto err; |
7867 | ||
534ca6d6 JA |
7868 | sqd = io_get_sq_data(p); |
7869 | if (IS_ERR(sqd)) { | |
7870 | ret = PTR_ERR(sqd); | |
7871 | goto err; | |
7872 | } | |
69fb2131 | 7873 | |
534ca6d6 | 7874 | ctx->sq_data = sqd; |
69fb2131 JA |
7875 | io_sq_thread_park(sqd); |
7876 | mutex_lock(&sqd->ctx_lock); | |
7877 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
7878 | mutex_unlock(&sqd->ctx_lock); | |
7879 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 7880 | |
917257da JA |
7881 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7882 | if (!ctx->sq_thread_idle) | |
7883 | ctx->sq_thread_idle = HZ; | |
7884 | ||
aa06165d | 7885 | if (sqd->thread) |
5aa75ed5 | 7886 | return 0; |
aa06165d | 7887 | |
6c271ce2 | 7888 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7889 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7890 | |
917257da | 7891 | ret = -EINVAL; |
44a9bd18 JA |
7892 | if (cpu >= nr_cpu_ids) |
7893 | goto err; | |
7889f44d | 7894 | if (!cpu_online(cpu)) |
917257da JA |
7895 | goto err; |
7896 | ||
37d1e2e3 | 7897 | sqd->sq_cpu = cpu; |
6c271ce2 | 7898 | } else { |
37d1e2e3 | 7899 | sqd->sq_cpu = -1; |
6c271ce2 | 7900 | } |
37d1e2e3 JA |
7901 | |
7902 | sqd->task_pid = current->pid; | |
7903 | current->flags |= PF_IO_WORKER; | |
7904 | ret = io_wq_fork_thread(io_sq_thread, sqd); | |
7905 | current->flags &= ~PF_IO_WORKER; | |
7906 | if (ret < 0) { | |
534ca6d6 | 7907 | sqd->thread = NULL; |
6c271ce2 JA |
7908 | goto err; |
7909 | } | |
37d1e2e3 | 7910 | wait_for_completion(&sqd->completion); |
5aa75ed5 | 7911 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
0f212204 JA |
7912 | if (ret) |
7913 | goto err; | |
6c271ce2 JA |
7914 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7915 | /* Can't have SQ_AFF without SQPOLL */ | |
7916 | ret = -EINVAL; | |
7917 | goto err; | |
7918 | } | |
7919 | ||
2b188cc1 JA |
7920 | return 0; |
7921 | err: | |
37d1e2e3 | 7922 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
7923 | return ret; |
7924 | } | |
7925 | ||
7e84e1c7 SG |
7926 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7927 | { | |
534ca6d6 JA |
7928 | struct io_sq_data *sqd = ctx->sq_data; |
7929 | ||
7930 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
37d1e2e3 | 7931 | complete(&sqd->startup); |
7e84e1c7 SG |
7932 | } |
7933 | ||
a087e2b5 BM |
7934 | static inline void __io_unaccount_mem(struct user_struct *user, |
7935 | unsigned long nr_pages) | |
2b188cc1 JA |
7936 | { |
7937 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7938 | } | |
7939 | ||
a087e2b5 BM |
7940 | static inline int __io_account_mem(struct user_struct *user, |
7941 | unsigned long nr_pages) | |
2b188cc1 JA |
7942 | { |
7943 | unsigned long page_limit, cur_pages, new_pages; | |
7944 | ||
7945 | /* Don't allow more pages than we can safely lock */ | |
7946 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7947 | ||
7948 | do { | |
7949 | cur_pages = atomic_long_read(&user->locked_vm); | |
7950 | new_pages = cur_pages + nr_pages; | |
7951 | if (new_pages > page_limit) | |
7952 | return -ENOMEM; | |
7953 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7954 | new_pages) != cur_pages); | |
7955 | ||
7956 | return 0; | |
7957 | } | |
7958 | ||
26bfa89e | 7959 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7960 | { |
62e398be | 7961 | if (ctx->user) |
a087e2b5 | 7962 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7963 | |
26bfa89e JA |
7964 | if (ctx->mm_account) |
7965 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7966 | } |
7967 | ||
26bfa89e | 7968 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7969 | { |
30975825 BM |
7970 | int ret; |
7971 | ||
62e398be | 7972 | if (ctx->user) { |
30975825 BM |
7973 | ret = __io_account_mem(ctx->user, nr_pages); |
7974 | if (ret) | |
7975 | return ret; | |
7976 | } | |
7977 | ||
26bfa89e JA |
7978 | if (ctx->mm_account) |
7979 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7980 | |
7981 | return 0; | |
7982 | } | |
7983 | ||
2b188cc1 JA |
7984 | static void io_mem_free(void *ptr) |
7985 | { | |
52e04ef4 MR |
7986 | struct page *page; |
7987 | ||
7988 | if (!ptr) | |
7989 | return; | |
2b188cc1 | 7990 | |
52e04ef4 | 7991 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7992 | if (put_page_testzero(page)) |
7993 | free_compound_page(page); | |
7994 | } | |
7995 | ||
7996 | static void *io_mem_alloc(size_t size) | |
7997 | { | |
7998 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 7999 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8000 | |
8001 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8002 | } | |
8003 | ||
75b28aff HV |
8004 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8005 | size_t *sq_offset) | |
8006 | { | |
8007 | struct io_rings *rings; | |
8008 | size_t off, sq_array_size; | |
8009 | ||
8010 | off = struct_size(rings, cqes, cq_entries); | |
8011 | if (off == SIZE_MAX) | |
8012 | return SIZE_MAX; | |
8013 | ||
8014 | #ifdef CONFIG_SMP | |
8015 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8016 | if (off == 0) | |
8017 | return SIZE_MAX; | |
8018 | #endif | |
8019 | ||
b36200f5 DV |
8020 | if (sq_offset) |
8021 | *sq_offset = off; | |
8022 | ||
75b28aff HV |
8023 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8024 | if (sq_array_size == SIZE_MAX) | |
8025 | return SIZE_MAX; | |
8026 | ||
8027 | if (check_add_overflow(off, sq_array_size, &off)) | |
8028 | return SIZE_MAX; | |
8029 | ||
75b28aff HV |
8030 | return off; |
8031 | } | |
8032 | ||
0a96bbe4 | 8033 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8034 | { |
8035 | int i, j; | |
8036 | ||
8037 | if (!ctx->user_bufs) | |
8038 | return -ENXIO; | |
8039 | ||
8040 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8041 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8042 | ||
8043 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8044 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8045 | |
de293938 | 8046 | if (imu->acct_pages) |
26bfa89e | 8047 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8048 | kvfree(imu->bvec); |
edafccee JA |
8049 | imu->nr_bvecs = 0; |
8050 | } | |
8051 | ||
8052 | kfree(ctx->user_bufs); | |
8053 | ctx->user_bufs = NULL; | |
8054 | ctx->nr_user_bufs = 0; | |
8055 | return 0; | |
8056 | } | |
8057 | ||
8058 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8059 | void __user *arg, unsigned index) | |
8060 | { | |
8061 | struct iovec __user *src; | |
8062 | ||
8063 | #ifdef CONFIG_COMPAT | |
8064 | if (ctx->compat) { | |
8065 | struct compat_iovec __user *ciovs; | |
8066 | struct compat_iovec ciov; | |
8067 | ||
8068 | ciovs = (struct compat_iovec __user *) arg; | |
8069 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8070 | return -EFAULT; | |
8071 | ||
d55e5f5b | 8072 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8073 | dst->iov_len = ciov.iov_len; |
8074 | return 0; | |
8075 | } | |
8076 | #endif | |
8077 | src = (struct iovec __user *) arg; | |
8078 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8079 | return -EFAULT; | |
8080 | return 0; | |
8081 | } | |
8082 | ||
de293938 JA |
8083 | /* |
8084 | * Not super efficient, but this is just a registration time. And we do cache | |
8085 | * the last compound head, so generally we'll only do a full search if we don't | |
8086 | * match that one. | |
8087 | * | |
8088 | * We check if the given compound head page has already been accounted, to | |
8089 | * avoid double accounting it. This allows us to account the full size of the | |
8090 | * page, not just the constituent pages of a huge page. | |
8091 | */ | |
8092 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8093 | int nr_pages, struct page *hpage) | |
8094 | { | |
8095 | int i, j; | |
8096 | ||
8097 | /* check current page array */ | |
8098 | for (i = 0; i < nr_pages; i++) { | |
8099 | if (!PageCompound(pages[i])) | |
8100 | continue; | |
8101 | if (compound_head(pages[i]) == hpage) | |
8102 | return true; | |
8103 | } | |
8104 | ||
8105 | /* check previously registered pages */ | |
8106 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8107 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8108 | ||
8109 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8110 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8111 | continue; | |
8112 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8113 | return true; | |
8114 | } | |
8115 | } | |
8116 | ||
8117 | return false; | |
8118 | } | |
8119 | ||
8120 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8121 | int nr_pages, struct io_mapped_ubuf *imu, | |
8122 | struct page **last_hpage) | |
8123 | { | |
8124 | int i, ret; | |
8125 | ||
8126 | for (i = 0; i < nr_pages; i++) { | |
8127 | if (!PageCompound(pages[i])) { | |
8128 | imu->acct_pages++; | |
8129 | } else { | |
8130 | struct page *hpage; | |
8131 | ||
8132 | hpage = compound_head(pages[i]); | |
8133 | if (hpage == *last_hpage) | |
8134 | continue; | |
8135 | *last_hpage = hpage; | |
8136 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8137 | continue; | |
8138 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8139 | } | |
8140 | } | |
8141 | ||
8142 | if (!imu->acct_pages) | |
8143 | return 0; | |
8144 | ||
26bfa89e | 8145 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8146 | if (ret) |
8147 | imu->acct_pages = 0; | |
8148 | return ret; | |
8149 | } | |
8150 | ||
0a96bbe4 BM |
8151 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8152 | struct io_mapped_ubuf *imu, | |
8153 | struct page **last_hpage) | |
edafccee JA |
8154 | { |
8155 | struct vm_area_struct **vmas = NULL; | |
8156 | struct page **pages = NULL; | |
0a96bbe4 BM |
8157 | unsigned long off, start, end, ubuf; |
8158 | size_t size; | |
8159 | int ret, pret, nr_pages, i; | |
8160 | ||
8161 | ubuf = (unsigned long) iov->iov_base; | |
8162 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8163 | start = ubuf >> PAGE_SHIFT; | |
8164 | nr_pages = end - start; | |
8165 | ||
8166 | ret = -ENOMEM; | |
8167 | ||
8168 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8169 | if (!pages) | |
8170 | goto done; | |
8171 | ||
8172 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8173 | GFP_KERNEL); | |
8174 | if (!vmas) | |
8175 | goto done; | |
edafccee | 8176 | |
0a96bbe4 BM |
8177 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8178 | GFP_KERNEL); | |
8179 | if (!imu->bvec) | |
8180 | goto done; | |
8181 | ||
8182 | ret = 0; | |
8183 | mmap_read_lock(current->mm); | |
8184 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8185 | pages, vmas); | |
8186 | if (pret == nr_pages) { | |
8187 | /* don't support file backed memory */ | |
8188 | for (i = 0; i < nr_pages; i++) { | |
8189 | struct vm_area_struct *vma = vmas[i]; | |
8190 | ||
8191 | if (vma->vm_file && | |
8192 | !is_file_hugepages(vma->vm_file)) { | |
8193 | ret = -EOPNOTSUPP; | |
8194 | break; | |
8195 | } | |
8196 | } | |
8197 | } else { | |
8198 | ret = pret < 0 ? pret : -EFAULT; | |
8199 | } | |
8200 | mmap_read_unlock(current->mm); | |
8201 | if (ret) { | |
8202 | /* | |
8203 | * if we did partial map, or found file backed vmas, | |
8204 | * release any pages we did get | |
8205 | */ | |
8206 | if (pret > 0) | |
8207 | unpin_user_pages(pages, pret); | |
8208 | kvfree(imu->bvec); | |
8209 | goto done; | |
8210 | } | |
8211 | ||
8212 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8213 | if (ret) { | |
8214 | unpin_user_pages(pages, pret); | |
8215 | kvfree(imu->bvec); | |
8216 | goto done; | |
8217 | } | |
8218 | ||
8219 | off = ubuf & ~PAGE_MASK; | |
8220 | size = iov->iov_len; | |
8221 | for (i = 0; i < nr_pages; i++) { | |
8222 | size_t vec_len; | |
8223 | ||
8224 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8225 | imu->bvec[i].bv_page = pages[i]; | |
8226 | imu->bvec[i].bv_len = vec_len; | |
8227 | imu->bvec[i].bv_offset = off; | |
8228 | off = 0; | |
8229 | size -= vec_len; | |
8230 | } | |
8231 | /* store original address for later verification */ | |
8232 | imu->ubuf = ubuf; | |
8233 | imu->len = iov->iov_len; | |
8234 | imu->nr_bvecs = nr_pages; | |
8235 | ret = 0; | |
8236 | done: | |
8237 | kvfree(pages); | |
8238 | kvfree(vmas); | |
8239 | return ret; | |
8240 | } | |
8241 | ||
2b358604 | 8242 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8243 | { |
edafccee JA |
8244 | if (ctx->user_bufs) |
8245 | return -EBUSY; | |
8246 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8247 | return -EINVAL; | |
8248 | ||
8249 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8250 | GFP_KERNEL); | |
8251 | if (!ctx->user_bufs) | |
8252 | return -ENOMEM; | |
8253 | ||
2b358604 BM |
8254 | return 0; |
8255 | } | |
edafccee | 8256 | |
2b358604 BM |
8257 | static int io_buffer_validate(struct iovec *iov) |
8258 | { | |
8259 | /* | |
8260 | * Don't impose further limits on the size and buffer | |
8261 | * constraints here, we'll -EINVAL later when IO is | |
8262 | * submitted if they are wrong. | |
8263 | */ | |
8264 | if (!iov->iov_base || !iov->iov_len) | |
8265 | return -EFAULT; | |
edafccee | 8266 | |
2b358604 BM |
8267 | /* arbitrary limit, but we need something */ |
8268 | if (iov->iov_len > SZ_1G) | |
8269 | return -EFAULT; | |
edafccee | 8270 | |
2b358604 BM |
8271 | return 0; |
8272 | } | |
edafccee | 8273 | |
2b358604 BM |
8274 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8275 | unsigned int nr_args) | |
8276 | { | |
8277 | int i, ret; | |
8278 | struct iovec iov; | |
8279 | struct page *last_hpage = NULL; | |
edafccee | 8280 | |
2b358604 BM |
8281 | ret = io_buffers_map_alloc(ctx, nr_args); |
8282 | if (ret) | |
8283 | return ret; | |
edafccee | 8284 | |
edafccee JA |
8285 | for (i = 0; i < nr_args; i++) { |
8286 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8287 | |
edafccee JA |
8288 | ret = io_copy_iov(ctx, &iov, arg, i); |
8289 | if (ret) | |
0a96bbe4 | 8290 | break; |
de293938 | 8291 | |
2b358604 BM |
8292 | ret = io_buffer_validate(&iov); |
8293 | if (ret) | |
0a96bbe4 | 8294 | break; |
edafccee | 8295 | |
0a96bbe4 BM |
8296 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8297 | if (ret) | |
8298 | break; | |
edafccee JA |
8299 | |
8300 | ctx->nr_user_bufs++; | |
8301 | } | |
0a96bbe4 BM |
8302 | |
8303 | if (ret) | |
8304 | io_sqe_buffers_unregister(ctx); | |
8305 | ||
edafccee JA |
8306 | return ret; |
8307 | } | |
8308 | ||
9b402849 JA |
8309 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8310 | { | |
8311 | __s32 __user *fds = arg; | |
8312 | int fd; | |
8313 | ||
8314 | if (ctx->cq_ev_fd) | |
8315 | return -EBUSY; | |
8316 | ||
8317 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8318 | return -EFAULT; | |
8319 | ||
8320 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8321 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8322 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8323 | ctx->cq_ev_fd = NULL; | |
8324 | return ret; | |
8325 | } | |
8326 | ||
8327 | return 0; | |
8328 | } | |
8329 | ||
8330 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8331 | { | |
8332 | if (ctx->cq_ev_fd) { | |
8333 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8334 | ctx->cq_ev_fd = NULL; | |
8335 | return 0; | |
8336 | } | |
8337 | ||
8338 | return -ENXIO; | |
8339 | } | |
8340 | ||
5a2e745d JA |
8341 | static int __io_destroy_buffers(int id, void *p, void *data) |
8342 | { | |
8343 | struct io_ring_ctx *ctx = data; | |
8344 | struct io_buffer *buf = p; | |
8345 | ||
067524e9 | 8346 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8347 | return 0; |
8348 | } | |
8349 | ||
8350 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8351 | { | |
8352 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8353 | idr_destroy(&ctx->io_buffer_idr); | |
8354 | } | |
8355 | ||
68e68ee6 | 8356 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8357 | { |
68e68ee6 | 8358 | struct io_kiocb *req, *nxt; |
1b4c351f | 8359 | |
68e68ee6 JA |
8360 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8361 | if (tsk && req->task != tsk) | |
8362 | continue; | |
1b4c351f JA |
8363 | list_del(&req->compl.list); |
8364 | kmem_cache_free(req_cachep, req); | |
8365 | } | |
8366 | } | |
8367 | ||
9a4fdbd8 | 8368 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
2b188cc1 | 8369 | { |
bf019da7 PB |
8370 | struct io_submit_state *submit_state = &ctx->submit_state; |
8371 | ||
9a4fdbd8 JA |
8372 | mutex_lock(&ctx->uring_lock); |
8373 | ||
8374 | if (submit_state->free_reqs) | |
8375 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, | |
8376 | submit_state->reqs); | |
8377 | ||
8378 | io_req_cache_free(&submit_state->comp.free_list, NULL); | |
8379 | ||
8380 | spin_lock_irq(&ctx->completion_lock); | |
8381 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); | |
8382 | spin_unlock_irq(&ctx->completion_lock); | |
8383 | ||
8384 | mutex_unlock(&ctx->uring_lock); | |
8385 | } | |
8386 | ||
2b188cc1 JA |
8387 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8388 | { | |
04fc6c80 PB |
8389 | /* |
8390 | * Some may use context even when all refs and requests have been put, | |
8391 | * and they are free to do so while still holding uring_lock, see | |
8392 | * __io_req_task_submit(). Wait for them to finish. | |
8393 | */ | |
8394 | mutex_lock(&ctx->uring_lock); | |
8395 | mutex_unlock(&ctx->uring_lock); | |
8396 | ||
37d1e2e3 | 8397 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8398 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8399 | |
37d1e2e3 | 8400 | if (ctx->mm_account) { |
2aede0e4 JA |
8401 | mmdrop(ctx->mm_account); |
8402 | ctx->mm_account = NULL; | |
30975825 | 8403 | } |
def596e9 | 8404 | |
8bad28d8 | 8405 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8406 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8407 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8408 | io_eventfd_unregister(ctx); |
5a2e745d | 8409 | io_destroy_buffers(ctx); |
41726c9a | 8410 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8411 | |
2b188cc1 | 8412 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8413 | if (ctx->ring_sock) { |
8414 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8415 | sock_release(ctx->ring_sock); |
355e8d26 | 8416 | } |
2b188cc1 JA |
8417 | #endif |
8418 | ||
75b28aff | 8419 | io_mem_free(ctx->rings); |
2b188cc1 | 8420 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8421 | |
8422 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8423 | free_uid(ctx->user); |
9a4fdbd8 | 8424 | io_req_caches_free(ctx, NULL); |
e941894e JA |
8425 | if (ctx->hash_map) |
8426 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8427 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8428 | kfree(ctx); |
8429 | } | |
8430 | ||
8431 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8432 | { | |
8433 | struct io_ring_ctx *ctx = file->private_data; | |
8434 | __poll_t mask = 0; | |
8435 | ||
8436 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8437 | /* |
8438 | * synchronizes with barrier from wq_has_sleeper call in | |
8439 | * io_commit_cqring | |
8440 | */ | |
2b188cc1 | 8441 | smp_rmb(); |
90554200 | 8442 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8443 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8444 | |
8445 | /* | |
8446 | * Don't flush cqring overflow list here, just do a simple check. | |
8447 | * Otherwise there could possible be ABBA deadlock: | |
8448 | * CPU0 CPU1 | |
8449 | * ---- ---- | |
8450 | * lock(&ctx->uring_lock); | |
8451 | * lock(&ep->mtx); | |
8452 | * lock(&ctx->uring_lock); | |
8453 | * lock(&ep->mtx); | |
8454 | * | |
8455 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8456 | * pushs them to do the flush. | |
8457 | */ | |
8458 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8459 | mask |= EPOLLIN | EPOLLRDNORM; |
8460 | ||
8461 | return mask; | |
8462 | } | |
8463 | ||
8464 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8465 | { | |
8466 | struct io_ring_ctx *ctx = file->private_data; | |
8467 | ||
8468 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8469 | } | |
8470 | ||
0bead8cd | 8471 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8472 | { |
4379bf8b | 8473 | const struct cred *creds; |
071698e1 | 8474 | |
4379bf8b JA |
8475 | creds = idr_remove(&ctx->personality_idr, id); |
8476 | if (creds) { | |
8477 | put_cred(creds); | |
0bead8cd | 8478 | return 0; |
1e6fa521 | 8479 | } |
0bead8cd YD |
8480 | |
8481 | return -EINVAL; | |
8482 | } | |
8483 | ||
8484 | static int io_remove_personalities(int id, void *p, void *data) | |
8485 | { | |
8486 | struct io_ring_ctx *ctx = data; | |
8487 | ||
8488 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8489 | return 0; |
8490 | } | |
8491 | ||
7c25c0d1 JA |
8492 | static void io_run_ctx_fallback(struct io_ring_ctx *ctx) |
8493 | { | |
8494 | struct callback_head *work, *head, *next; | |
8495 | ||
8496 | do { | |
8497 | do { | |
8498 | head = NULL; | |
8499 | work = READ_ONCE(ctx->exit_task_work); | |
8500 | } while (cmpxchg(&ctx->exit_task_work, work, head) != work); | |
8501 | ||
8502 | if (!work) | |
8503 | break; | |
8504 | ||
8505 | do { | |
8506 | next = work->next; | |
8507 | work->func(work); | |
8508 | work = next; | |
8509 | cond_resched(); | |
8510 | } while (work); | |
8511 | } while (1); | |
8512 | } | |
8513 | ||
85faa7b8 JA |
8514 | static void io_ring_exit_work(struct work_struct *work) |
8515 | { | |
b2edc0a7 PB |
8516 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8517 | exit_work); | |
85faa7b8 | 8518 | |
56952e91 JA |
8519 | /* |
8520 | * If we're doing polled IO and end up having requests being | |
8521 | * submitted async (out-of-line), then completions can come in while | |
8522 | * we're waiting for refs to drop. We need to reap these manually, | |
8523 | * as nobody else will be looking for them. | |
8524 | */ | |
b2edc0a7 | 8525 | do { |
9936c7c2 | 8526 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
7c25c0d1 | 8527 | io_run_ctx_fallback(ctx); |
b2edc0a7 | 8528 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8529 | io_ring_ctx_free(ctx); |
8530 | } | |
8531 | ||
2b188cc1 JA |
8532 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8533 | { | |
8534 | mutex_lock(&ctx->uring_lock); | |
8535 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8536 | |
8537 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8538 | ctx->sqo_dead = 1; | |
8539 | ||
cda286f0 PB |
8540 | /* if force is set, the ring is going away. always drop after that */ |
8541 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8542 | if (ctx->rings) |
6c503150 | 8543 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8544 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8545 | mutex_unlock(&ctx->uring_lock); |
8546 | ||
6b81928d PB |
8547 | io_kill_timeouts(ctx, NULL, NULL); |
8548 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8549 | |
15dff286 | 8550 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8551 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8552 | |
85faa7b8 | 8553 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8554 | /* |
8555 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8556 | * if we're exiting a ton of rings at the same time. It just adds | |
8557 | * noise and overhead, there's no discernable change in runtime | |
8558 | * over using system_wq. | |
8559 | */ | |
8560 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8561 | } |
8562 | ||
8563 | static int io_uring_release(struct inode *inode, struct file *file) | |
8564 | { | |
8565 | struct io_ring_ctx *ctx = file->private_data; | |
8566 | ||
8567 | file->private_data = NULL; | |
8568 | io_ring_ctx_wait_and_kill(ctx); | |
8569 | return 0; | |
8570 | } | |
8571 | ||
f6edbabb PB |
8572 | struct io_task_cancel { |
8573 | struct task_struct *task; | |
8574 | struct files_struct *files; | |
8575 | }; | |
f254ac04 | 8576 | |
f6edbabb | 8577 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8578 | { |
9a472ef7 | 8579 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8580 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8581 | bool ret; |
8582 | ||
f6edbabb | 8583 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8584 | unsigned long flags; |
8585 | struct io_ring_ctx *ctx = req->ctx; | |
8586 | ||
8587 | /* protect against races with linked timeouts */ | |
8588 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8589 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8590 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8591 | } else { | |
f6edbabb | 8592 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8593 | } |
8594 | return ret; | |
b711d4ea JA |
8595 | } |
8596 | ||
b7ddce3c | 8597 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8598 | struct task_struct *task, |
b7ddce3c PB |
8599 | struct files_struct *files) |
8600 | { | |
8601 | struct io_defer_entry *de = NULL; | |
8602 | LIST_HEAD(list); | |
8603 | ||
8604 | spin_lock_irq(&ctx->completion_lock); | |
8605 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8606 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8607 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8608 | break; | |
8609 | } | |
8610 | } | |
8611 | spin_unlock_irq(&ctx->completion_lock); | |
8612 | ||
8613 | while (!list_empty(&list)) { | |
8614 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8615 | list_del_init(&de->list); | |
8616 | req_set_fail_links(de->req); | |
8617 | io_put_req(de->req); | |
8618 | io_req_complete(de->req, -ECANCELED); | |
8619 | kfree(de); | |
8620 | } | |
8621 | } | |
8622 | ||
9936c7c2 PB |
8623 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8624 | struct task_struct *task, | |
8625 | struct files_struct *files) | |
8626 | { | |
8627 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
5aa75ed5 | 8628 | struct io_uring_task *tctx = current->io_uring; |
9936c7c2 PB |
8629 | |
8630 | while (1) { | |
8631 | enum io_wq_cancel cret; | |
8632 | bool ret = false; | |
8633 | ||
5aa75ed5 JA |
8634 | if (tctx && tctx->io_wq) { |
8635 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, | |
9936c7c2 PB |
8636 | &cancel, true); |
8637 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8638 | } | |
8639 | ||
8640 | /* SQPOLL thread does its own polling */ | |
8641 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8642 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8643 | io_iopoll_try_reap_events(ctx); | |
8644 | ret = true; | |
8645 | } | |
8646 | } | |
8647 | ||
8648 | ret |= io_poll_remove_all(ctx, task, files); | |
8649 | ret |= io_kill_timeouts(ctx, task, files); | |
8650 | ret |= io_run_task_work(); | |
8651 | io_cqring_overflow_flush(ctx, true, task, files); | |
8652 | if (!ret) | |
8653 | break; | |
8654 | cond_resched(); | |
8655 | } | |
8656 | } | |
8657 | ||
ca70f00b PB |
8658 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8659 | struct task_struct *task, | |
8660 | struct files_struct *files) | |
8661 | { | |
8662 | struct io_kiocb *req; | |
8663 | int cnt = 0; | |
8664 | ||
8665 | spin_lock_irq(&ctx->inflight_lock); | |
8666 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8667 | cnt += io_match_task(req, task, files); | |
8668 | spin_unlock_irq(&ctx->inflight_lock); | |
8669 | return cnt; | |
8670 | } | |
8671 | ||
b52fda00 | 8672 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8673 | struct task_struct *task, |
fcb323cc JA |
8674 | struct files_struct *files) |
8675 | { | |
fcb323cc | 8676 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 8677 | DEFINE_WAIT(wait); |
ca70f00b | 8678 | int inflight; |
fcb323cc | 8679 | |
ca70f00b PB |
8680 | inflight = io_uring_count_inflight(ctx, task, files); |
8681 | if (!inflight) | |
fcb323cc | 8682 | break; |
f6edbabb | 8683 | |
9936c7c2 | 8684 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 8685 | |
34343786 PB |
8686 | if (ctx->sq_data) |
8687 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
8688 | prepare_to_wait(&task->io_uring->wait, &wait, |
8689 | TASK_UNINTERRUPTIBLE); | |
8690 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8691 | schedule(); | |
c98de08c | 8692 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
8693 | if (ctx->sq_data) |
8694 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 8695 | } |
0f212204 JA |
8696 | } |
8697 | ||
d9d05217 PB |
8698 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
8699 | { | |
d9d05217 PB |
8700 | mutex_lock(&ctx->uring_lock); |
8701 | ctx->sqo_dead = 1; | |
8702 | mutex_unlock(&ctx->uring_lock); | |
8703 | ||
8704 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
8705 | if (ctx->rings) |
8706 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
8707 | } |
8708 | ||
0f212204 JA |
8709 | /* |
8710 | * We need to iteratively cancel requests, in case a request has dependent | |
8711 | * hard links. These persist even for failure of cancelations, hence keep | |
8712 | * looping until none are found. | |
8713 | */ | |
8714 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8715 | struct files_struct *files) | |
8716 | { | |
8717 | struct task_struct *task = current; | |
37d1e2e3 | 8718 | bool did_park = false; |
0f212204 | 8719 | |
fdaf083c | 8720 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 8721 | io_disable_sqo_submit(ctx); |
37d1e2e3 JA |
8722 | did_park = io_sq_thread_park(ctx->sq_data); |
8723 | if (did_park) { | |
8724 | task = ctx->sq_data->thread; | |
8725 | atomic_inc(&task->io_uring->in_idle); | |
8726 | } | |
fdaf083c | 8727 | } |
0f212204 | 8728 | |
df9923f9 | 8729 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 8730 | |
3a7efd1a | 8731 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 8732 | if (!files) |
9936c7c2 | 8733 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c | 8734 | |
37d1e2e3 | 8735 | if (did_park) { |
fdaf083c | 8736 | atomic_dec(&task->io_uring->in_idle); |
fdaf083c JA |
8737 | io_sq_thread_unpark(ctx->sq_data); |
8738 | } | |
0f212204 JA |
8739 | } |
8740 | ||
8741 | /* | |
8742 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8743 | */ | |
fdaf083c | 8744 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8745 | { |
236434c3 | 8746 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 8747 | int ret; |
236434c3 MWO |
8748 | |
8749 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8750 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8751 | if (unlikely(ret)) |
8752 | return ret; | |
236434c3 | 8753 | tctx = current->io_uring; |
0f212204 | 8754 | } |
236434c3 MWO |
8755 | if (tctx->last != file) { |
8756 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8757 | |
236434c3 | 8758 | if (!old) { |
0f212204 | 8759 | get_file(file); |
a528b04e PB |
8760 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
8761 | file, GFP_KERNEL)); | |
8762 | if (ret) { | |
8763 | fput(file); | |
8764 | return ret; | |
8765 | } | |
ecfc8492 PB |
8766 | |
8767 | /* one and only SQPOLL file note, held by sqo_task */ | |
8768 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
8769 | current != ctx->sqo_task); | |
0f212204 | 8770 | } |
236434c3 | 8771 | tctx->last = file; |
0f212204 JA |
8772 | } |
8773 | ||
fdaf083c JA |
8774 | /* |
8775 | * This is race safe in that the task itself is doing this, hence it | |
8776 | * cannot be going through the exit/cancel paths at the same time. | |
8777 | * This cannot be modified while exit/cancel is running. | |
8778 | */ | |
8779 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8780 | tctx->sqpoll = true; | |
8781 | ||
0f212204 JA |
8782 | return 0; |
8783 | } | |
8784 | ||
8785 | /* | |
8786 | * Remove this io_uring_file -> task mapping. | |
8787 | */ | |
8788 | static void io_uring_del_task_file(struct file *file) | |
8789 | { | |
8790 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
8791 | |
8792 | if (tctx->last == file) | |
8793 | tctx->last = NULL; | |
5e2ed8c4 | 8794 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
8795 | if (file) |
8796 | fput(file); | |
8797 | } | |
8798 | ||
de7f1d9e PB |
8799 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
8800 | { | |
8801 | struct file *file; | |
8802 | unsigned long index; | |
8803 | ||
8804 | xa_for_each(&tctx->xa, index, file) | |
8805 | io_uring_del_task_file(file); | |
8806 | } | |
8807 | ||
0f212204 JA |
8808 | void __io_uring_files_cancel(struct files_struct *files) |
8809 | { | |
8810 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
8811 | struct file *file; |
8812 | unsigned long index; | |
0f212204 JA |
8813 | |
8814 | /* make sure overflow events are dropped */ | |
fdaf083c | 8815 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
8816 | xa_for_each(&tctx->xa, index, file) |
8817 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 8818 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8819 | |
5aa75ed5 | 8820 | if (files) { |
de7f1d9e | 8821 | io_uring_remove_task_files(tctx); |
8a378fb0 JA |
8822 | if (tctx->io_wq) { |
8823 | io_wq_destroy(tctx->io_wq); | |
8824 | tctx->io_wq = NULL; | |
8825 | } | |
5aa75ed5 | 8826 | } |
fdaf083c JA |
8827 | } |
8828 | ||
8829 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8830 | { | |
0e9ddb39 PB |
8831 | return percpu_counter_sum(&tctx->inflight); |
8832 | } | |
fdaf083c | 8833 | |
0e9ddb39 PB |
8834 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8835 | { | |
37d1e2e3 | 8836 | struct io_sq_data *sqd = ctx->sq_data; |
0e9ddb39 PB |
8837 | struct io_uring_task *tctx; |
8838 | s64 inflight; | |
8839 | DEFINE_WAIT(wait); | |
fdaf083c | 8840 | |
37d1e2e3 | 8841 | if (!sqd) |
0e9ddb39 | 8842 | return; |
0e9ddb39 | 8843 | io_disable_sqo_submit(ctx); |
37d1e2e3 JA |
8844 | if (!io_sq_thread_park(sqd)) |
8845 | return; | |
8846 | tctx = ctx->sq_data->thread->io_uring; | |
fdaf083c | 8847 | |
0e9ddb39 PB |
8848 | atomic_inc(&tctx->in_idle); |
8849 | do { | |
8850 | /* read completions before cancelations */ | |
8851 | inflight = tctx_inflight(tctx); | |
8852 | if (!inflight) | |
8853 | break; | |
8854 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 8855 | |
0e9ddb39 PB |
8856 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
8857 | /* | |
8858 | * If we've seen completions, retry without waiting. This | |
8859 | * avoids a race where a completion comes in before we did | |
8860 | * prepare_to_wait(). | |
8861 | */ | |
8862 | if (inflight == tctx_inflight(tctx)) | |
8863 | schedule(); | |
8864 | finish_wait(&tctx->wait, &wait); | |
8865 | } while (1); | |
8866 | atomic_dec(&tctx->in_idle); | |
37d1e2e3 | 8867 | io_sq_thread_unpark(sqd); |
0f212204 JA |
8868 | } |
8869 | ||
0f212204 JA |
8870 | /* |
8871 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8872 | * requests. | |
8873 | */ | |
8874 | void __io_uring_task_cancel(void) | |
8875 | { | |
8876 | struct io_uring_task *tctx = current->io_uring; | |
8877 | DEFINE_WAIT(wait); | |
d8a6df10 | 8878 | s64 inflight; |
0f212204 JA |
8879 | |
8880 | /* make sure overflow events are dropped */ | |
fdaf083c | 8881 | atomic_inc(&tctx->in_idle); |
0f212204 | 8882 | |
0b5cd6c3 | 8883 | /* trigger io_disable_sqo_submit() */ |
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 PB |
8912 | |
8913 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
8914 | } |
8915 | ||
fcb323cc JA |
8916 | static int io_uring_flush(struct file *file, void *data) |
8917 | { | |
6b5733eb | 8918 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 8919 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 8920 | |
3bfe6106 JA |
8921 | /* Ignore helper thread files exit */ |
8922 | if (current->flags & PF_IO_WORKER) | |
8923 | return 0; | |
8924 | ||
41be53e9 | 8925 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
84965ff8 | 8926 | io_uring_cancel_task_requests(ctx, NULL); |
41be53e9 JA |
8927 | io_req_caches_free(ctx, current); |
8928 | } | |
84965ff8 | 8929 | |
7c25c0d1 JA |
8930 | io_run_ctx_fallback(ctx); |
8931 | ||
6b5733eb | 8932 | if (!tctx) |
4f793dc4 PB |
8933 | return 0; |
8934 | ||
6b5733eb PB |
8935 | /* we should have cancelled and erased it before PF_EXITING */ |
8936 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
8937 | xa_load(&tctx->xa, (unsigned long)file)); | |
8938 | ||
4f793dc4 PB |
8939 | /* |
8940 | * fput() is pending, will be 2 if the only other ref is our potential | |
8941 | * task file note. If the task is exiting, drop regardless of count. | |
8942 | */ | |
6b5733eb PB |
8943 | if (atomic_long_read(&file->f_count) != 2) |
8944 | return 0; | |
4f793dc4 | 8945 | |
d9d05217 PB |
8946 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
8947 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
8948 | WARN_ON_ONCE(ctx->sqo_task != current && |
8949 | xa_load(&tctx->xa, (unsigned long)file)); | |
8950 | /* sqo_dead check is for when this happens after cancellation */ | |
8951 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
8952 | !xa_load(&tctx->xa, (unsigned long)file)); |
8953 | ||
8954 | io_disable_sqo_submit(ctx); | |
8955 | } | |
8956 | ||
8957 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
8958 | io_uring_del_task_file(file); | |
fcb323cc JA |
8959 | return 0; |
8960 | } | |
8961 | ||
6c5c240e RP |
8962 | static void *io_uring_validate_mmap_request(struct file *file, |
8963 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8964 | { |
2b188cc1 | 8965 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8966 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8967 | struct page *page; |
8968 | void *ptr; | |
8969 | ||
8970 | switch (offset) { | |
8971 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8972 | case IORING_OFF_CQ_RING: |
8973 | ptr = ctx->rings; | |
2b188cc1 JA |
8974 | break; |
8975 | case IORING_OFF_SQES: | |
8976 | ptr = ctx->sq_sqes; | |
8977 | break; | |
2b188cc1 | 8978 | default: |
6c5c240e | 8979 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8980 | } |
8981 | ||
8982 | page = virt_to_head_page(ptr); | |
a50b854e | 8983 | if (sz > page_size(page)) |
6c5c240e RP |
8984 | return ERR_PTR(-EINVAL); |
8985 | ||
8986 | return ptr; | |
8987 | } | |
8988 | ||
8989 | #ifdef CONFIG_MMU | |
8990 | ||
8991 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8992 | { | |
8993 | size_t sz = vma->vm_end - vma->vm_start; | |
8994 | unsigned long pfn; | |
8995 | void *ptr; | |
8996 | ||
8997 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8998 | if (IS_ERR(ptr)) | |
8999 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9000 | |
9001 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9002 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9003 | } | |
9004 | ||
6c5c240e RP |
9005 | #else /* !CONFIG_MMU */ |
9006 | ||
9007 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9008 | { | |
9009 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9010 | } | |
9011 | ||
9012 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9013 | { | |
9014 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9015 | } | |
9016 | ||
9017 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9018 | unsigned long addr, unsigned long len, | |
9019 | unsigned long pgoff, unsigned long flags) | |
9020 | { | |
9021 | void *ptr; | |
9022 | ||
9023 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9024 | if (IS_ERR(ptr)) | |
9025 | return PTR_ERR(ptr); | |
9026 | ||
9027 | return (unsigned long) ptr; | |
9028 | } | |
9029 | ||
9030 | #endif /* !CONFIG_MMU */ | |
9031 | ||
d9d05217 | 9032 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9033 | { |
d9d05217 | 9034 | int ret = 0; |
90554200 JA |
9035 | DEFINE_WAIT(wait); |
9036 | ||
9037 | do { | |
9038 | if (!io_sqring_full(ctx)) | |
9039 | break; | |
9040 | ||
9041 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9042 | ||
d9d05217 PB |
9043 | if (unlikely(ctx->sqo_dead)) { |
9044 | ret = -EOWNERDEAD; | |
9045 | goto out; | |
9046 | } | |
9047 | ||
90554200 JA |
9048 | if (!io_sqring_full(ctx)) |
9049 | break; | |
9050 | ||
9051 | schedule(); | |
9052 | } while (!signal_pending(current)); | |
9053 | ||
9054 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9055 | out: |
9056 | return ret; | |
90554200 JA |
9057 | } |
9058 | ||
c73ebb68 HX |
9059 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9060 | struct __kernel_timespec __user **ts, | |
9061 | const sigset_t __user **sig) | |
9062 | { | |
9063 | struct io_uring_getevents_arg arg; | |
9064 | ||
9065 | /* | |
9066 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9067 | * is just a pointer to the sigset_t. | |
9068 | */ | |
9069 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9070 | *sig = (const sigset_t __user *) argp; | |
9071 | *ts = NULL; | |
9072 | return 0; | |
9073 | } | |
9074 | ||
9075 | /* | |
9076 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9077 | * timespec and sigset_t pointers if good. | |
9078 | */ | |
9079 | if (*argsz != sizeof(arg)) | |
9080 | return -EINVAL; | |
9081 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9082 | return -EFAULT; | |
9083 | *sig = u64_to_user_ptr(arg.sigmask); | |
9084 | *argsz = arg.sigmask_sz; | |
9085 | *ts = u64_to_user_ptr(arg.ts); | |
9086 | return 0; | |
9087 | } | |
9088 | ||
2b188cc1 | 9089 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9090 | u32, min_complete, u32, flags, const void __user *, argp, |
9091 | size_t, argsz) | |
2b188cc1 JA |
9092 | { |
9093 | struct io_ring_ctx *ctx; | |
9094 | long ret = -EBADF; | |
9095 | int submitted = 0; | |
9096 | struct fd f; | |
9097 | ||
4c6e277c | 9098 | io_run_task_work(); |
b41e9852 | 9099 | |
90554200 | 9100 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9101 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9102 | return -EINVAL; |
9103 | ||
9104 | f = fdget(fd); | |
9105 | if (!f.file) | |
9106 | return -EBADF; | |
9107 | ||
9108 | ret = -EOPNOTSUPP; | |
9109 | if (f.file->f_op != &io_uring_fops) | |
9110 | goto out_fput; | |
9111 | ||
9112 | ret = -ENXIO; | |
9113 | ctx = f.file->private_data; | |
9114 | if (!percpu_ref_tryget(&ctx->refs)) | |
9115 | goto out_fput; | |
9116 | ||
7e84e1c7 SG |
9117 | ret = -EBADFD; |
9118 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9119 | goto out; | |
9120 | ||
6c271ce2 JA |
9121 | /* |
9122 | * For SQ polling, the thread will do all submissions and completions. | |
9123 | * Just return the requested submit count, and wake the thread if | |
9124 | * we were asked to. | |
9125 | */ | |
b2a9eada | 9126 | ret = 0; |
6c271ce2 | 9127 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9128 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9129 | |
d9d05217 PB |
9130 | ret = -EOWNERDEAD; |
9131 | if (unlikely(ctx->sqo_dead)) | |
9132 | goto out; | |
6c271ce2 | 9133 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9134 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9135 | if (flags & IORING_ENTER_SQ_WAIT) { |
9136 | ret = io_sqpoll_wait_sq(ctx); | |
9137 | if (ret) | |
9138 | goto out; | |
9139 | } | |
6c271ce2 | 9140 | submitted = to_submit; |
b2a9eada | 9141 | } else if (to_submit) { |
fdaf083c | 9142 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9143 | if (unlikely(ret)) |
9144 | goto out; | |
2b188cc1 | 9145 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9146 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9147 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9148 | |
9149 | if (submitted != to_submit) | |
9150 | goto out; | |
2b188cc1 JA |
9151 | } |
9152 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9153 | const sigset_t __user *sig; |
9154 | struct __kernel_timespec __user *ts; | |
9155 | ||
9156 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9157 | if (unlikely(ret)) | |
9158 | goto out; | |
9159 | ||
2b188cc1 JA |
9160 | min_complete = min(min_complete, ctx->cq_entries); |
9161 | ||
32b2244a XW |
9162 | /* |
9163 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9164 | * space applications don't need to do io completion events | |
9165 | * polling again, they can rely on io_sq_thread to do polling | |
9166 | * work, which can reduce cpu usage and uring_lock contention. | |
9167 | */ | |
9168 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9169 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9170 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9171 | } else { |
c73ebb68 | 9172 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9173 | } |
2b188cc1 JA |
9174 | } |
9175 | ||
7c504e65 | 9176 | out: |
6805b32e | 9177 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9178 | out_fput: |
9179 | fdput(f); | |
9180 | return submitted ? submitted : ret; | |
9181 | } | |
9182 | ||
bebdb65e | 9183 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9184 | static int io_uring_show_cred(int id, void *p, void *data) |
9185 | { | |
4379bf8b | 9186 | const struct cred *cred = p; |
87ce955b JA |
9187 | struct seq_file *m = data; |
9188 | struct user_namespace *uns = seq_user_ns(m); | |
9189 | struct group_info *gi; | |
9190 | kernel_cap_t cap; | |
9191 | unsigned __capi; | |
9192 | int g; | |
9193 | ||
9194 | seq_printf(m, "%5d\n", id); | |
9195 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9196 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9197 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9198 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9199 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9200 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9201 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9202 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9203 | seq_puts(m, "\n\tGroups:\t"); | |
9204 | gi = cred->group_info; | |
9205 | for (g = 0; g < gi->ngroups; g++) { | |
9206 | seq_put_decimal_ull(m, g ? " " : "", | |
9207 | from_kgid_munged(uns, gi->gid[g])); | |
9208 | } | |
9209 | seq_puts(m, "\n\tCapEff:\t"); | |
9210 | cap = cred->cap_effective; | |
9211 | CAP_FOR_EACH_U32(__capi) | |
9212 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9213 | seq_putc(m, '\n'); | |
9214 | return 0; | |
9215 | } | |
9216 | ||
9217 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9218 | { | |
dbbe9c64 | 9219 | struct io_sq_data *sq = NULL; |
fad8e0de | 9220 | bool has_lock; |
87ce955b JA |
9221 | int i; |
9222 | ||
fad8e0de JA |
9223 | /* |
9224 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9225 | * since fdinfo case grabs it in the opposite direction of normal use | |
9226 | * cases. If we fail to get the lock, we just don't iterate any | |
9227 | * structures that could be going away outside the io_uring mutex. | |
9228 | */ | |
9229 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9230 | ||
dbbe9c64 JQ |
9231 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9232 | sq = ctx->sq_data; | |
9233 | ||
9234 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9235 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9236 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9237 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9238 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9239 | |
87ce955b JA |
9240 | if (f) |
9241 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9242 | else | |
9243 | seq_printf(m, "%5u: <none>\n", i); | |
9244 | } | |
9245 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9246 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9247 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9248 | ||
9249 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9250 | (unsigned int) buf->len); | |
9251 | } | |
fad8e0de | 9252 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9253 | seq_printf(m, "Personalities:\n"); |
9254 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9255 | } | |
d7718a9d JA |
9256 | seq_printf(m, "PollList:\n"); |
9257 | spin_lock_irq(&ctx->completion_lock); | |
9258 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9259 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9260 | struct io_kiocb *req; | |
9261 | ||
9262 | hlist_for_each_entry(req, list, hash_node) | |
9263 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9264 | req->task->task_works != NULL); | |
9265 | } | |
9266 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9267 | if (has_lock) |
9268 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9269 | } |
9270 | ||
9271 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9272 | { | |
9273 | struct io_ring_ctx *ctx = f->private_data; | |
9274 | ||
9275 | if (percpu_ref_tryget(&ctx->refs)) { | |
9276 | __io_uring_show_fdinfo(ctx, m); | |
9277 | percpu_ref_put(&ctx->refs); | |
9278 | } | |
9279 | } | |
bebdb65e | 9280 | #endif |
87ce955b | 9281 | |
2b188cc1 JA |
9282 | static const struct file_operations io_uring_fops = { |
9283 | .release = io_uring_release, | |
fcb323cc | 9284 | .flush = io_uring_flush, |
2b188cc1 | 9285 | .mmap = io_uring_mmap, |
6c5c240e RP |
9286 | #ifndef CONFIG_MMU |
9287 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9288 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9289 | #endif | |
2b188cc1 JA |
9290 | .poll = io_uring_poll, |
9291 | .fasync = io_uring_fasync, | |
bebdb65e | 9292 | #ifdef CONFIG_PROC_FS |
87ce955b | 9293 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9294 | #endif |
2b188cc1 JA |
9295 | }; |
9296 | ||
9297 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9298 | struct io_uring_params *p) | |
9299 | { | |
75b28aff HV |
9300 | struct io_rings *rings; |
9301 | size_t size, sq_array_offset; | |
2b188cc1 | 9302 | |
bd740481 JA |
9303 | /* make sure these are sane, as we already accounted them */ |
9304 | ctx->sq_entries = p->sq_entries; | |
9305 | ctx->cq_entries = p->cq_entries; | |
9306 | ||
75b28aff HV |
9307 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9308 | if (size == SIZE_MAX) | |
9309 | return -EOVERFLOW; | |
9310 | ||
9311 | rings = io_mem_alloc(size); | |
9312 | if (!rings) | |
2b188cc1 JA |
9313 | return -ENOMEM; |
9314 | ||
75b28aff HV |
9315 | ctx->rings = rings; |
9316 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9317 | rings->sq_ring_mask = p->sq_entries - 1; | |
9318 | rings->cq_ring_mask = p->cq_entries - 1; | |
9319 | rings->sq_ring_entries = p->sq_entries; | |
9320 | rings->cq_ring_entries = p->cq_entries; | |
9321 | ctx->sq_mask = rings->sq_ring_mask; | |
9322 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9323 | |
9324 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9325 | if (size == SIZE_MAX) { |
9326 | io_mem_free(ctx->rings); | |
9327 | ctx->rings = NULL; | |
2b188cc1 | 9328 | return -EOVERFLOW; |
eb065d30 | 9329 | } |
2b188cc1 JA |
9330 | |
9331 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9332 | if (!ctx->sq_sqes) { |
9333 | io_mem_free(ctx->rings); | |
9334 | ctx->rings = NULL; | |
2b188cc1 | 9335 | return -ENOMEM; |
eb065d30 | 9336 | } |
2b188cc1 | 9337 | |
2b188cc1 JA |
9338 | return 0; |
9339 | } | |
9340 | ||
9faadcc8 PB |
9341 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9342 | { | |
9343 | int ret, fd; | |
9344 | ||
9345 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9346 | if (fd < 0) | |
9347 | return fd; | |
9348 | ||
9349 | ret = io_uring_add_task_file(ctx, file); | |
9350 | if (ret) { | |
9351 | put_unused_fd(fd); | |
9352 | return ret; | |
9353 | } | |
9354 | fd_install(fd, file); | |
9355 | return fd; | |
9356 | } | |
9357 | ||
2b188cc1 JA |
9358 | /* |
9359 | * Allocate an anonymous fd, this is what constitutes the application | |
9360 | * visible backing of an io_uring instance. The application mmaps this | |
9361 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9362 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9363 | */ | |
9faadcc8 | 9364 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9365 | { |
9366 | struct file *file; | |
9faadcc8 | 9367 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9368 | int ret; |
9369 | ||
2b188cc1 JA |
9370 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9371 | &ctx->ring_sock); | |
9372 | if (ret) | |
9faadcc8 | 9373 | return ERR_PTR(ret); |
2b188cc1 JA |
9374 | #endif |
9375 | ||
2b188cc1 JA |
9376 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9377 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9378 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9379 | if (IS_ERR(file)) { |
9380 | sock_release(ctx->ring_sock); | |
9381 | ctx->ring_sock = NULL; | |
9382 | } else { | |
9383 | ctx->ring_sock->file = file; | |
0f212204 | 9384 | } |
2b188cc1 | 9385 | #endif |
9faadcc8 | 9386 | return file; |
2b188cc1 JA |
9387 | } |
9388 | ||
7f13657d XW |
9389 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9390 | struct io_uring_params __user *params) | |
2b188cc1 | 9391 | { |
2b188cc1 | 9392 | struct io_ring_ctx *ctx; |
9faadcc8 | 9393 | struct file *file; |
2b188cc1 JA |
9394 | int ret; |
9395 | ||
8110c1a6 | 9396 | if (!entries) |
2b188cc1 | 9397 | return -EINVAL; |
8110c1a6 JA |
9398 | if (entries > IORING_MAX_ENTRIES) { |
9399 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9400 | return -EINVAL; | |
9401 | entries = IORING_MAX_ENTRIES; | |
9402 | } | |
2b188cc1 JA |
9403 | |
9404 | /* | |
9405 | * Use twice as many entries for the CQ ring. It's possible for the | |
9406 | * application to drive a higher depth than the size of the SQ ring, | |
9407 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9408 | * some flexibility in overcommitting a bit. If the application has |
9409 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9410 | * of CQ ring entries manually. | |
2b188cc1 JA |
9411 | */ |
9412 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9413 | if (p->flags & IORING_SETUP_CQSIZE) { |
9414 | /* | |
9415 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9416 | * to a power-of-two, if it isn't already. We do NOT impose | |
9417 | * any cq vs sq ring sizing. | |
9418 | */ | |
eb2667b3 | 9419 | if (!p->cq_entries) |
33a107f0 | 9420 | return -EINVAL; |
8110c1a6 JA |
9421 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9422 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9423 | return -EINVAL; | |
9424 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9425 | } | |
eb2667b3 JQ |
9426 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9427 | if (p->cq_entries < p->sq_entries) | |
9428 | return -EINVAL; | |
33a107f0 JA |
9429 | } else { |
9430 | p->cq_entries = 2 * p->sq_entries; | |
9431 | } | |
2b188cc1 | 9432 | |
2b188cc1 | 9433 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9434 | if (!ctx) |
2b188cc1 | 9435 | return -ENOMEM; |
2b188cc1 | 9436 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9437 | if (!capable(CAP_IPC_LOCK)) |
9438 | ctx->user = get_uid(current_user()); | |
37d1e2e3 | 9439 | ctx->sqo_task = current; |
2aede0e4 JA |
9440 | |
9441 | /* | |
9442 | * This is just grabbed for accounting purposes. When a process exits, | |
9443 | * the mm is exited and dropped before the files, hence we need to hang | |
9444 | * on to this mm purely for the purposes of being able to unaccount | |
9445 | * memory (locked/pinned vm). It's not used for anything else. | |
9446 | */ | |
6b7898eb | 9447 | mmgrab(current->mm); |
2aede0e4 | 9448 | ctx->mm_account = current->mm; |
6b7898eb | 9449 | |
2b188cc1 JA |
9450 | ret = io_allocate_scq_urings(ctx, p); |
9451 | if (ret) | |
9452 | goto err; | |
9453 | ||
7e84e1c7 | 9454 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9455 | if (ret) |
9456 | goto err; | |
9457 | ||
7e84e1c7 SG |
9458 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9459 | io_sq_offload_start(ctx); | |
9460 | ||
2b188cc1 | 9461 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9462 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9463 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9464 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9465 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9466 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9467 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9468 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9469 | |
9470 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9471 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9472 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9473 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9474 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9475 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9476 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9477 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9478 | |
7f13657d XW |
9479 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9480 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9481 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9482 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9483 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9484 | |
9485 | if (copy_to_user(params, p, sizeof(*p))) { | |
9486 | ret = -EFAULT; | |
9487 | goto err; | |
9488 | } | |
d1719f70 | 9489 | |
9faadcc8 PB |
9490 | file = io_uring_get_file(ctx); |
9491 | if (IS_ERR(file)) { | |
9492 | ret = PTR_ERR(file); | |
9493 | goto err; | |
9494 | } | |
9495 | ||
044c1ab3 JA |
9496 | /* |
9497 | * Install ring fd as the very last thing, so we don't risk someone | |
9498 | * having closed it before we finish setup | |
9499 | */ | |
9faadcc8 PB |
9500 | ret = io_uring_install_fd(ctx, file); |
9501 | if (ret < 0) { | |
06585c49 | 9502 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9503 | /* fput will clean it up */ |
9504 | fput(file); | |
9505 | return ret; | |
9506 | } | |
044c1ab3 | 9507 | |
c826bd7a | 9508 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9509 | return ret; |
9510 | err: | |
d9d05217 | 9511 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9512 | io_ring_ctx_wait_and_kill(ctx); |
9513 | return ret; | |
9514 | } | |
9515 | ||
9516 | /* | |
9517 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9518 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9519 | * params structure passed in. | |
9520 | */ | |
9521 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9522 | { | |
9523 | struct io_uring_params p; | |
2b188cc1 JA |
9524 | int i; |
9525 | ||
9526 | if (copy_from_user(&p, params, sizeof(p))) | |
9527 | return -EFAULT; | |
9528 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9529 | if (p.resv[i]) | |
9530 | return -EINVAL; | |
9531 | } | |
9532 | ||
6c271ce2 | 9533 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9534 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9535 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9536 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9537 | return -EINVAL; |
9538 | ||
7f13657d | 9539 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9540 | } |
9541 | ||
9542 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9543 | struct io_uring_params __user *, params) | |
9544 | { | |
9545 | return io_uring_setup(entries, params); | |
9546 | } | |
9547 | ||
66f4af93 JA |
9548 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9549 | { | |
9550 | struct io_uring_probe *p; | |
9551 | size_t size; | |
9552 | int i, ret; | |
9553 | ||
9554 | size = struct_size(p, ops, nr_args); | |
9555 | if (size == SIZE_MAX) | |
9556 | return -EOVERFLOW; | |
9557 | p = kzalloc(size, GFP_KERNEL); | |
9558 | if (!p) | |
9559 | return -ENOMEM; | |
9560 | ||
9561 | ret = -EFAULT; | |
9562 | if (copy_from_user(p, arg, size)) | |
9563 | goto out; | |
9564 | ret = -EINVAL; | |
9565 | if (memchr_inv(p, 0, size)) | |
9566 | goto out; | |
9567 | ||
9568 | p->last_op = IORING_OP_LAST - 1; | |
9569 | if (nr_args > IORING_OP_LAST) | |
9570 | nr_args = IORING_OP_LAST; | |
9571 | ||
9572 | for (i = 0; i < nr_args; i++) { | |
9573 | p->ops[i].op = i; | |
9574 | if (!io_op_defs[i].not_supported) | |
9575 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9576 | } | |
9577 | p->ops_len = i; | |
9578 | ||
9579 | ret = 0; | |
9580 | if (copy_to_user(arg, p, size)) | |
9581 | ret = -EFAULT; | |
9582 | out: | |
9583 | kfree(p); | |
9584 | return ret; | |
9585 | } | |
9586 | ||
071698e1 JA |
9587 | static int io_register_personality(struct io_ring_ctx *ctx) |
9588 | { | |
4379bf8b | 9589 | const struct cred *creds; |
1e6fa521 | 9590 | int ret; |
071698e1 | 9591 | |
4379bf8b | 9592 | creds = get_current_cred(); |
1e6fa521 | 9593 | |
4379bf8b JA |
9594 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
9595 | USHRT_MAX, GFP_KERNEL); | |
9596 | if (ret < 0) | |
9597 | put_cred(creds); | |
1e6fa521 | 9598 | return ret; |
071698e1 JA |
9599 | } |
9600 | ||
21b55dbc SG |
9601 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9602 | unsigned int nr_args) | |
9603 | { | |
9604 | struct io_uring_restriction *res; | |
9605 | size_t size; | |
9606 | int i, ret; | |
9607 | ||
7e84e1c7 SG |
9608 | /* Restrictions allowed only if rings started disabled */ |
9609 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9610 | return -EBADFD; | |
9611 | ||
21b55dbc | 9612 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9613 | if (ctx->restrictions.registered) |
21b55dbc SG |
9614 | return -EBUSY; |
9615 | ||
9616 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9617 | return -EINVAL; | |
9618 | ||
9619 | size = array_size(nr_args, sizeof(*res)); | |
9620 | if (size == SIZE_MAX) | |
9621 | return -EOVERFLOW; | |
9622 | ||
9623 | res = memdup_user(arg, size); | |
9624 | if (IS_ERR(res)) | |
9625 | return PTR_ERR(res); | |
9626 | ||
9627 | ret = 0; | |
9628 | ||
9629 | for (i = 0; i < nr_args; i++) { | |
9630 | switch (res[i].opcode) { | |
9631 | case IORING_RESTRICTION_REGISTER_OP: | |
9632 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9633 | ret = -EINVAL; | |
9634 | goto out; | |
9635 | } | |
9636 | ||
9637 | __set_bit(res[i].register_op, | |
9638 | ctx->restrictions.register_op); | |
9639 | break; | |
9640 | case IORING_RESTRICTION_SQE_OP: | |
9641 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9642 | ret = -EINVAL; | |
9643 | goto out; | |
9644 | } | |
9645 | ||
9646 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9647 | break; | |
9648 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9649 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9650 | break; | |
9651 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9652 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9653 | break; | |
9654 | default: | |
9655 | ret = -EINVAL; | |
9656 | goto out; | |
9657 | } | |
9658 | } | |
9659 | ||
9660 | out: | |
9661 | /* Reset all restrictions if an error happened */ | |
9662 | if (ret != 0) | |
9663 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9664 | else | |
7e84e1c7 | 9665 | ctx->restrictions.registered = true; |
21b55dbc SG |
9666 | |
9667 | kfree(res); | |
9668 | return ret; | |
9669 | } | |
9670 | ||
7e84e1c7 SG |
9671 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9672 | { | |
9673 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9674 | return -EBADFD; | |
9675 | ||
9676 | if (ctx->restrictions.registered) | |
9677 | ctx->restricted = 1; | |
9678 | ||
9679 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9680 | ||
9681 | io_sq_offload_start(ctx); | |
9682 | ||
9683 | return 0; | |
9684 | } | |
9685 | ||
071698e1 JA |
9686 | static bool io_register_op_must_quiesce(int op) |
9687 | { | |
9688 | switch (op) { | |
9689 | case IORING_UNREGISTER_FILES: | |
9690 | case IORING_REGISTER_FILES_UPDATE: | |
9691 | case IORING_REGISTER_PROBE: | |
9692 | case IORING_REGISTER_PERSONALITY: | |
9693 | case IORING_UNREGISTER_PERSONALITY: | |
9694 | return false; | |
9695 | default: | |
9696 | return true; | |
9697 | } | |
9698 | } | |
9699 | ||
edafccee JA |
9700 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9701 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9702 | __releases(ctx->uring_lock) |
9703 | __acquires(ctx->uring_lock) | |
edafccee JA |
9704 | { |
9705 | int ret; | |
9706 | ||
35fa71a0 JA |
9707 | /* |
9708 | * We're inside the ring mutex, if the ref is already dying, then | |
9709 | * someone else killed the ctx or is already going through | |
9710 | * io_uring_register(). | |
9711 | */ | |
9712 | if (percpu_ref_is_dying(&ctx->refs)) | |
9713 | return -ENXIO; | |
9714 | ||
071698e1 | 9715 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9716 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9717 | |
05f3fb3c JA |
9718 | /* |
9719 | * Drop uring mutex before waiting for references to exit. If | |
9720 | * another thread is currently inside io_uring_enter() it might | |
9721 | * need to grab the uring_lock to make progress. If we hold it | |
9722 | * here across the drain wait, then we can deadlock. It's safe | |
9723 | * to drop the mutex here, since no new references will come in | |
9724 | * after we've killed the percpu ref. | |
9725 | */ | |
9726 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9727 | do { |
9728 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9729 | if (!ret) | |
9730 | break; | |
ed6930c9 JA |
9731 | ret = io_run_task_work_sig(); |
9732 | if (ret < 0) | |
9733 | break; | |
af9c1a44 JA |
9734 | } while (1); |
9735 | ||
05f3fb3c | 9736 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9737 | |
88f171ab PB |
9738 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
9739 | return ret; | |
21b55dbc SG |
9740 | } |
9741 | ||
9742 | if (ctx->restricted) { | |
9743 | if (opcode >= IORING_REGISTER_LAST) { | |
9744 | ret = -EINVAL; | |
9745 | goto out; | |
9746 | } | |
9747 | ||
9748 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9749 | ret = -EACCES; | |
c150368b JA |
9750 | goto out; |
9751 | } | |
05f3fb3c | 9752 | } |
edafccee JA |
9753 | |
9754 | switch (opcode) { | |
9755 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9756 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9757 | break; |
9758 | case IORING_UNREGISTER_BUFFERS: | |
9759 | ret = -EINVAL; | |
9760 | if (arg || nr_args) | |
9761 | break; | |
0a96bbe4 | 9762 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9763 | break; |
6b06314c JA |
9764 | case IORING_REGISTER_FILES: |
9765 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9766 | break; | |
9767 | case IORING_UNREGISTER_FILES: | |
9768 | ret = -EINVAL; | |
9769 | if (arg || nr_args) | |
9770 | break; | |
9771 | ret = io_sqe_files_unregister(ctx); | |
9772 | break; | |
c3a31e60 JA |
9773 | case IORING_REGISTER_FILES_UPDATE: |
9774 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9775 | break; | |
9b402849 | 9776 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9777 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9778 | ret = -EINVAL; |
9779 | if (nr_args != 1) | |
9780 | break; | |
9781 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9782 | if (ret) |
9783 | break; | |
9784 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9785 | ctx->eventfd_async = 1; | |
9786 | else | |
9787 | ctx->eventfd_async = 0; | |
9b402849 JA |
9788 | break; |
9789 | case IORING_UNREGISTER_EVENTFD: | |
9790 | ret = -EINVAL; | |
9791 | if (arg || nr_args) | |
9792 | break; | |
9793 | ret = io_eventfd_unregister(ctx); | |
9794 | break; | |
66f4af93 JA |
9795 | case IORING_REGISTER_PROBE: |
9796 | ret = -EINVAL; | |
9797 | if (!arg || nr_args > 256) | |
9798 | break; | |
9799 | ret = io_probe(ctx, arg, nr_args); | |
9800 | break; | |
071698e1 JA |
9801 | case IORING_REGISTER_PERSONALITY: |
9802 | ret = -EINVAL; | |
9803 | if (arg || nr_args) | |
9804 | break; | |
9805 | ret = io_register_personality(ctx); | |
9806 | break; | |
9807 | case IORING_UNREGISTER_PERSONALITY: | |
9808 | ret = -EINVAL; | |
9809 | if (arg) | |
9810 | break; | |
9811 | ret = io_unregister_personality(ctx, nr_args); | |
9812 | break; | |
7e84e1c7 SG |
9813 | case IORING_REGISTER_ENABLE_RINGS: |
9814 | ret = -EINVAL; | |
9815 | if (arg || nr_args) | |
9816 | break; | |
9817 | ret = io_register_enable_rings(ctx); | |
9818 | break; | |
21b55dbc SG |
9819 | case IORING_REGISTER_RESTRICTIONS: |
9820 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9821 | break; | |
edafccee JA |
9822 | default: |
9823 | ret = -EINVAL; | |
9824 | break; | |
9825 | } | |
9826 | ||
21b55dbc | 9827 | out: |
071698e1 | 9828 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9829 | /* bring the ctx back to life */ |
05f3fb3c | 9830 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 9831 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9832 | } |
edafccee JA |
9833 | return ret; |
9834 | } | |
9835 | ||
9836 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9837 | void __user *, arg, unsigned int, nr_args) | |
9838 | { | |
9839 | struct io_ring_ctx *ctx; | |
9840 | long ret = -EBADF; | |
9841 | struct fd f; | |
9842 | ||
9843 | f = fdget(fd); | |
9844 | if (!f.file) | |
9845 | return -EBADF; | |
9846 | ||
9847 | ret = -EOPNOTSUPP; | |
9848 | if (f.file->f_op != &io_uring_fops) | |
9849 | goto out_fput; | |
9850 | ||
9851 | ctx = f.file->private_data; | |
9852 | ||
b6c23dd5 PB |
9853 | io_run_task_work(); |
9854 | ||
edafccee JA |
9855 | mutex_lock(&ctx->uring_lock); |
9856 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9857 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9858 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9859 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9860 | out_fput: |
9861 | fdput(f); | |
9862 | return ret; | |
9863 | } | |
9864 | ||
2b188cc1 JA |
9865 | static int __init io_uring_init(void) |
9866 | { | |
d7f62e82 SM |
9867 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9868 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9869 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9870 | } while (0) | |
9871 | ||
9872 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9873 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9874 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9875 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9876 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9877 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9878 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9879 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9880 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9881 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9882 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9883 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9884 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9885 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9886 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9887 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9888 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9889 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9890 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9891 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9892 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9893 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9894 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9895 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9896 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9897 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9898 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9899 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9900 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9901 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9902 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9903 | |
d3656344 | 9904 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9905 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
9906 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
9907 | SLAB_ACCOUNT); | |
2b188cc1 JA |
9908 | return 0; |
9909 | }; | |
9910 | __initcall(io_uring_init); |