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 JA |
362 | |
363 | struct list_head defer_list; | |
5262f567 | 364 | struct list_head timeout_list; |
1d7bb1d5 | 365 | struct list_head cq_overflow_list; |
fcb323cc | 366 | |
ad3eb2c8 | 367 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
368 | } ____cacheline_aligned_in_smp; |
369 | ||
3c1a2ead JA |
370 | struct { |
371 | struct mutex uring_lock; | |
372 | wait_queue_head_t wait; | |
373 | } ____cacheline_aligned_in_smp; | |
374 | ||
375 | struct io_submit_state submit_state; | |
376 | ||
206aefde JA |
377 | struct io_rings *rings; |
378 | ||
2aede0e4 | 379 | /* |
37d1e2e3 | 380 | * For SQPOLL usage |
2aede0e4 JA |
381 | */ |
382 | struct task_struct *sqo_task; | |
383 | ||
384 | /* Only used for accounting purposes */ | |
385 | struct mm_struct *mm_account; | |
386 | ||
534ca6d6 JA |
387 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
388 | ||
90554200 | 389 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 390 | struct list_head sqd_list; |
75b28aff | 391 | |
6b06314c JA |
392 | /* |
393 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
394 | * readers must ensure that ->refs is alive as long as the file* is | |
395 | * used. Only updated through io_uring_register(2). | |
396 | */ | |
269bbe5f | 397 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
398 | unsigned nr_user_files; |
399 | ||
edafccee JA |
400 | /* if used, fixed mapped user buffers */ |
401 | unsigned nr_user_bufs; | |
402 | struct io_mapped_ubuf *user_bufs; | |
403 | ||
2b188cc1 JA |
404 | struct user_struct *user; |
405 | ||
0f158b4c JA |
406 | struct completion ref_comp; |
407 | struct completion sq_thread_comp; | |
206aefde JA |
408 | |
409 | #if defined(CONFIG_UNIX) | |
410 | struct socket *ring_sock; | |
411 | #endif | |
412 | ||
5a2e745d JA |
413 | struct idr io_buffer_idr; |
414 | ||
071698e1 JA |
415 | struct idr personality_idr; |
416 | ||
206aefde JA |
417 | struct { |
418 | unsigned cached_cq_tail; | |
419 | unsigned cq_entries; | |
420 | unsigned cq_mask; | |
421 | atomic_t cq_timeouts; | |
f010505b | 422 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 423 | unsigned long cq_check_overflow; |
206aefde JA |
424 | struct wait_queue_head cq_wait; |
425 | struct fasync_struct *cq_fasync; | |
426 | struct eventfd_ctx *cq_ev_fd; | |
427 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 428 | |
2b188cc1 JA |
429 | struct { |
430 | spinlock_t completion_lock; | |
e94f141b | 431 | |
def596e9 | 432 | /* |
540e32a0 | 433 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
434 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
435 | * For SQPOLL, only the single threaded io_sq_thread() will | |
436 | * manipulate the list, hence no extra locking is needed there. | |
437 | */ | |
540e32a0 | 438 | struct list_head iopoll_list; |
78076bb6 JA |
439 | struct hlist_head *cancel_hash; |
440 | unsigned cancel_hash_bits; | |
e94f141b | 441 | bool poll_multi_file; |
31b51510 | 442 | |
fcb323cc JA |
443 | spinlock_t inflight_lock; |
444 | struct list_head inflight_list; | |
2b188cc1 | 445 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 446 | |
269bbe5f BM |
447 | struct delayed_work rsrc_put_work; |
448 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
449 | struct list_head rsrc_ref_list; |
450 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 451 | |
21b55dbc | 452 | struct io_restriction restrictions; |
3c1a2ead | 453 | |
7c25c0d1 JA |
454 | /* exit task_work */ |
455 | struct callback_head *exit_task_work; | |
456 | ||
3c1a2ead JA |
457 | /* Keep this last, we don't need it for the fast path */ |
458 | struct work_struct exit_work; | |
2b188cc1 JA |
459 | }; |
460 | ||
09bb8394 JA |
461 | /* |
462 | * First field must be the file pointer in all the | |
463 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
464 | */ | |
221c5eb2 JA |
465 | struct io_poll_iocb { |
466 | struct file *file; | |
018043be | 467 | struct wait_queue_head *head; |
221c5eb2 | 468 | __poll_t events; |
8c838788 | 469 | bool done; |
221c5eb2 | 470 | bool canceled; |
392edb45 | 471 | struct wait_queue_entry wait; |
221c5eb2 JA |
472 | }; |
473 | ||
018043be PB |
474 | struct io_poll_remove { |
475 | struct file *file; | |
476 | u64 addr; | |
477 | }; | |
478 | ||
b5dba59e JA |
479 | struct io_close { |
480 | struct file *file; | |
b5dba59e JA |
481 | int fd; |
482 | }; | |
483 | ||
ad8a48ac JA |
484 | struct io_timeout_data { |
485 | struct io_kiocb *req; | |
486 | struct hrtimer timer; | |
487 | struct timespec64 ts; | |
488 | enum hrtimer_mode mode; | |
489 | }; | |
490 | ||
8ed8d3c3 JA |
491 | struct io_accept { |
492 | struct file *file; | |
493 | struct sockaddr __user *addr; | |
494 | int __user *addr_len; | |
495 | int flags; | |
09952e3e | 496 | unsigned long nofile; |
8ed8d3c3 JA |
497 | }; |
498 | ||
499 | struct io_sync { | |
500 | struct file *file; | |
501 | loff_t len; | |
502 | loff_t off; | |
503 | int flags; | |
d63d1b5e | 504 | int mode; |
8ed8d3c3 JA |
505 | }; |
506 | ||
fbf23849 JA |
507 | struct io_cancel { |
508 | struct file *file; | |
509 | u64 addr; | |
510 | }; | |
511 | ||
b29472ee JA |
512 | struct io_timeout { |
513 | struct file *file; | |
bfe68a22 PB |
514 | u32 off; |
515 | u32 target_seq; | |
135fcde8 | 516 | struct list_head list; |
90cd7e42 PB |
517 | /* head of the link, used by linked timeouts only */ |
518 | struct io_kiocb *head; | |
b29472ee JA |
519 | }; |
520 | ||
0bdf7a2d PB |
521 | struct io_timeout_rem { |
522 | struct file *file; | |
523 | u64 addr; | |
9c8e11b3 PB |
524 | |
525 | /* timeout update */ | |
526 | struct timespec64 ts; | |
527 | u32 flags; | |
0bdf7a2d PB |
528 | }; |
529 | ||
9adbd45d JA |
530 | struct io_rw { |
531 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
532 | struct kiocb kiocb; | |
533 | u64 addr; | |
534 | u64 len; | |
535 | }; | |
536 | ||
3fbb51c1 JA |
537 | struct io_connect { |
538 | struct file *file; | |
539 | struct sockaddr __user *addr; | |
540 | int addr_len; | |
541 | }; | |
542 | ||
e47293fd JA |
543 | struct io_sr_msg { |
544 | struct file *file; | |
fddaface | 545 | union { |
270a5940 | 546 | struct user_msghdr __user *umsg; |
fddaface JA |
547 | void __user *buf; |
548 | }; | |
e47293fd | 549 | int msg_flags; |
bcda7baa | 550 | int bgid; |
fddaface | 551 | size_t len; |
bcda7baa | 552 | struct io_buffer *kbuf; |
e47293fd JA |
553 | }; |
554 | ||
15b71abe JA |
555 | struct io_open { |
556 | struct file *file; | |
557 | int dfd; | |
15b71abe | 558 | struct filename *filename; |
c12cedf2 | 559 | struct open_how how; |
4022e7af | 560 | unsigned long nofile; |
15b71abe JA |
561 | }; |
562 | ||
269bbe5f | 563 | struct io_rsrc_update { |
05f3fb3c JA |
564 | struct file *file; |
565 | u64 arg; | |
566 | u32 nr_args; | |
567 | u32 offset; | |
568 | }; | |
569 | ||
4840e418 JA |
570 | struct io_fadvise { |
571 | struct file *file; | |
572 | u64 offset; | |
573 | u32 len; | |
574 | u32 advice; | |
575 | }; | |
576 | ||
c1ca757b JA |
577 | struct io_madvise { |
578 | struct file *file; | |
579 | u64 addr; | |
580 | u32 len; | |
581 | u32 advice; | |
582 | }; | |
583 | ||
3e4827b0 JA |
584 | struct io_epoll { |
585 | struct file *file; | |
586 | int epfd; | |
587 | int op; | |
588 | int fd; | |
589 | struct epoll_event event; | |
e47293fd JA |
590 | }; |
591 | ||
7d67af2c PB |
592 | struct io_splice { |
593 | struct file *file_out; | |
594 | struct file *file_in; | |
595 | loff_t off_out; | |
596 | loff_t off_in; | |
597 | u64 len; | |
598 | unsigned int flags; | |
599 | }; | |
600 | ||
ddf0322d JA |
601 | struct io_provide_buf { |
602 | struct file *file; | |
603 | __u64 addr; | |
604 | __s32 len; | |
605 | __u32 bgid; | |
606 | __u16 nbufs; | |
607 | __u16 bid; | |
608 | }; | |
609 | ||
1d9e1288 BM |
610 | struct io_statx { |
611 | struct file *file; | |
612 | int dfd; | |
613 | unsigned int mask; | |
614 | unsigned int flags; | |
e62753e4 | 615 | const char __user *filename; |
1d9e1288 BM |
616 | struct statx __user *buffer; |
617 | }; | |
618 | ||
36f4fa68 JA |
619 | struct io_shutdown { |
620 | struct file *file; | |
621 | int how; | |
622 | }; | |
623 | ||
80a261fd JA |
624 | struct io_rename { |
625 | struct file *file; | |
626 | int old_dfd; | |
627 | int new_dfd; | |
628 | struct filename *oldpath; | |
629 | struct filename *newpath; | |
630 | int flags; | |
631 | }; | |
632 | ||
14a1143b JA |
633 | struct io_unlink { |
634 | struct file *file; | |
635 | int dfd; | |
636 | int flags; | |
637 | struct filename *filename; | |
638 | }; | |
639 | ||
3ca405eb PB |
640 | struct io_completion { |
641 | struct file *file; | |
642 | struct list_head list; | |
0f7e466b | 643 | int cflags; |
3ca405eb PB |
644 | }; |
645 | ||
f499a021 JA |
646 | struct io_async_connect { |
647 | struct sockaddr_storage address; | |
648 | }; | |
649 | ||
03b1230c JA |
650 | struct io_async_msghdr { |
651 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
652 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
653 | struct iovec *free_iov; | |
03b1230c JA |
654 | struct sockaddr __user *uaddr; |
655 | struct msghdr msg; | |
b537916c | 656 | struct sockaddr_storage addr; |
03b1230c JA |
657 | }; |
658 | ||
f67676d1 JA |
659 | struct io_async_rw { |
660 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
661 | const struct iovec *free_iovec; |
662 | struct iov_iter iter; | |
227c0c96 | 663 | size_t bytes_done; |
bcf5a063 | 664 | struct wait_page_queue wpq; |
f67676d1 JA |
665 | }; |
666 | ||
6b47ee6e PB |
667 | enum { |
668 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
669 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
670 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
671 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
672 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 673 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 674 | |
6b47ee6e PB |
675 | REQ_F_FAIL_LINK_BIT, |
676 | REQ_F_INFLIGHT_BIT, | |
677 | REQ_F_CUR_POS_BIT, | |
678 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 679 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 680 | REQ_F_ISREG_BIT, |
99bc4c38 | 681 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 682 | REQ_F_POLLED_BIT, |
bcda7baa | 683 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 684 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 685 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 686 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 687 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
688 | |
689 | /* not a real bit, just to check we're not overflowing the space */ | |
690 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
691 | }; |
692 | ||
693 | enum { | |
694 | /* ctx owns file */ | |
695 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
696 | /* drain existing IO first */ | |
697 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
698 | /* linked sqes */ | |
699 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
700 | /* doesn't sever on completion < 0 */ | |
701 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
702 | /* IOSQE_ASYNC */ | |
703 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
704 | /* IOSQE_BUFFER_SELECT */ |
705 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 706 | |
6b47ee6e PB |
707 | /* fail rest of links */ |
708 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
709 | /* on inflight list */ | |
710 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
711 | /* read/write uses file position */ | |
712 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
713 | /* must not punt to workers */ | |
714 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 715 | /* has or had linked timeout */ |
6b47ee6e | 716 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
717 | /* regular file */ |
718 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
719 | /* needs cleanup */ |
720 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
721 | /* already went through poll handler */ |
722 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
723 | /* buffer already selected */ |
724 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
725 | /* doesn't need file table for this request */ |
726 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
727 | /* io_wq_work is initialized */ |
728 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
729 | /* linked timeout is active, i.e. prepared by link's head */ |
730 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
731 | /* completion is deferred through io_comp_state */ |
732 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
733 | }; |
734 | ||
735 | struct async_poll { | |
736 | struct io_poll_iocb poll; | |
807abcb0 | 737 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
738 | }; |
739 | ||
7cbf1722 JA |
740 | struct io_task_work { |
741 | struct io_wq_work_node node; | |
742 | task_work_func_t func; | |
743 | }; | |
744 | ||
09bb8394 JA |
745 | /* |
746 | * NOTE! Each of the iocb union members has the file pointer | |
747 | * as the first entry in their struct definition. So you can | |
748 | * access the file pointer through any of the sub-structs, | |
749 | * or directly as just 'ki_filp' in this struct. | |
750 | */ | |
2b188cc1 | 751 | struct io_kiocb { |
221c5eb2 | 752 | union { |
09bb8394 | 753 | struct file *file; |
9adbd45d | 754 | struct io_rw rw; |
221c5eb2 | 755 | struct io_poll_iocb poll; |
018043be | 756 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
757 | struct io_accept accept; |
758 | struct io_sync sync; | |
fbf23849 | 759 | struct io_cancel cancel; |
b29472ee | 760 | struct io_timeout timeout; |
0bdf7a2d | 761 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 762 | struct io_connect connect; |
e47293fd | 763 | struct io_sr_msg sr_msg; |
15b71abe | 764 | struct io_open open; |
b5dba59e | 765 | struct io_close close; |
269bbe5f | 766 | struct io_rsrc_update rsrc_update; |
4840e418 | 767 | struct io_fadvise fadvise; |
c1ca757b | 768 | struct io_madvise madvise; |
3e4827b0 | 769 | struct io_epoll epoll; |
7d67af2c | 770 | struct io_splice splice; |
ddf0322d | 771 | struct io_provide_buf pbuf; |
1d9e1288 | 772 | struct io_statx statx; |
36f4fa68 | 773 | struct io_shutdown shutdown; |
80a261fd | 774 | struct io_rename rename; |
14a1143b | 775 | struct io_unlink unlink; |
3ca405eb PB |
776 | /* use only after cleaning per-op data, see io_clean_op() */ |
777 | struct io_completion compl; | |
221c5eb2 | 778 | }; |
2b188cc1 | 779 | |
e8c2bc1f JA |
780 | /* opcode allocated if it needs to store data for async defer */ |
781 | void *async_data; | |
d625c6ee | 782 | u8 opcode; |
65a6543d XW |
783 | /* polled IO has completed */ |
784 | u8 iopoll_completed; | |
2b188cc1 | 785 | |
4f4eeba8 | 786 | u16 buf_index; |
9cf7c104 | 787 | u32 result; |
4f4eeba8 | 788 | |
010e8e6b PB |
789 | struct io_ring_ctx *ctx; |
790 | unsigned int flags; | |
791 | refcount_t refs; | |
792 | struct task_struct *task; | |
793 | u64 user_data; | |
d7718a9d | 794 | |
f2f87370 | 795 | struct io_kiocb *link; |
269bbe5f | 796 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 797 | |
d21ffe7e PB |
798 | /* |
799 | * 1. used with ctx->iopoll_list with reads/writes | |
800 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
801 | */ | |
010e8e6b | 802 | struct list_head inflight_entry; |
7cbf1722 JA |
803 | union { |
804 | struct io_task_work io_task_work; | |
805 | struct callback_head task_work; | |
806 | }; | |
010e8e6b PB |
807 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
808 | struct hlist_node hash_node; | |
809 | struct async_poll *apoll; | |
810 | struct io_wq_work work; | |
2b188cc1 | 811 | }; |
05589553 | 812 | |
27dc8338 PB |
813 | struct io_defer_entry { |
814 | struct list_head list; | |
815 | struct io_kiocb *req; | |
9cf7c104 | 816 | u32 seq; |
2b188cc1 JA |
817 | }; |
818 | ||
d3656344 | 819 | struct io_op_def { |
d3656344 JA |
820 | /* needs req->file assigned */ |
821 | unsigned needs_file : 1; | |
d3656344 JA |
822 | /* hash wq insertion if file is a regular file */ |
823 | unsigned hash_reg_file : 1; | |
824 | /* unbound wq insertion if file is a non-regular file */ | |
825 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
826 | /* opcode is not supported by this kernel */ |
827 | unsigned not_supported : 1; | |
8a72758c JA |
828 | /* set if opcode supports polled "wait" */ |
829 | unsigned pollin : 1; | |
830 | unsigned pollout : 1; | |
bcda7baa JA |
831 | /* op supports buffer selection */ |
832 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
833 | /* must always have async data allocated */ |
834 | unsigned needs_async_data : 1; | |
27926b68 JA |
835 | /* should block plug */ |
836 | unsigned plug : 1; | |
e8c2bc1f JA |
837 | /* size of async data needed, if any */ |
838 | unsigned short async_size; | |
d3656344 JA |
839 | }; |
840 | ||
0918682b | 841 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
842 | [IORING_OP_NOP] = {}, |
843 | [IORING_OP_READV] = { | |
d3656344 JA |
844 | .needs_file = 1, |
845 | .unbound_nonreg_file = 1, | |
8a72758c | 846 | .pollin = 1, |
4d954c25 | 847 | .buffer_select = 1, |
e8c2bc1f | 848 | .needs_async_data = 1, |
27926b68 | 849 | .plug = 1, |
e8c2bc1f | 850 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 851 | }, |
0463b6c5 | 852 | [IORING_OP_WRITEV] = { |
d3656344 JA |
853 | .needs_file = 1, |
854 | .hash_reg_file = 1, | |
855 | .unbound_nonreg_file = 1, | |
8a72758c | 856 | .pollout = 1, |
e8c2bc1f | 857 | .needs_async_data = 1, |
27926b68 | 858 | .plug = 1, |
e8c2bc1f | 859 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 860 | }, |
0463b6c5 | 861 | [IORING_OP_FSYNC] = { |
d3656344 JA |
862 | .needs_file = 1, |
863 | }, | |
0463b6c5 | 864 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
865 | .needs_file = 1, |
866 | .unbound_nonreg_file = 1, | |
8a72758c | 867 | .pollin = 1, |
27926b68 | 868 | .plug = 1, |
e8c2bc1f | 869 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 870 | }, |
0463b6c5 | 871 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
872 | .needs_file = 1, |
873 | .hash_reg_file = 1, | |
874 | .unbound_nonreg_file = 1, | |
8a72758c | 875 | .pollout = 1, |
27926b68 | 876 | .plug = 1, |
e8c2bc1f | 877 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 878 | }, |
0463b6c5 | 879 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
880 | .needs_file = 1, |
881 | .unbound_nonreg_file = 1, | |
882 | }, | |
0463b6c5 PB |
883 | [IORING_OP_POLL_REMOVE] = {}, |
884 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
885 | .needs_file = 1, |
886 | }, | |
0463b6c5 | 887 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
888 | .needs_file = 1, |
889 | .unbound_nonreg_file = 1, | |
8a72758c | 890 | .pollout = 1, |
e8c2bc1f JA |
891 | .needs_async_data = 1, |
892 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 893 | }, |
0463b6c5 | 894 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
895 | .needs_file = 1, |
896 | .unbound_nonreg_file = 1, | |
8a72758c | 897 | .pollin = 1, |
52de1fe1 | 898 | .buffer_select = 1, |
e8c2bc1f JA |
899 | .needs_async_data = 1, |
900 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 901 | }, |
0463b6c5 | 902 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
903 | .needs_async_data = 1, |
904 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 905 | }, |
9c8e11b3 PB |
906 | [IORING_OP_TIMEOUT_REMOVE] = { |
907 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 908 | }, |
0463b6c5 | 909 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
910 | .needs_file = 1, |
911 | .unbound_nonreg_file = 1, | |
8a72758c | 912 | .pollin = 1, |
d3656344 | 913 | }, |
0463b6c5 PB |
914 | [IORING_OP_ASYNC_CANCEL] = {}, |
915 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
916 | .needs_async_data = 1, |
917 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 918 | }, |
0463b6c5 | 919 | [IORING_OP_CONNECT] = { |
d3656344 JA |
920 | .needs_file = 1, |
921 | .unbound_nonreg_file = 1, | |
8a72758c | 922 | .pollout = 1, |
e8c2bc1f JA |
923 | .needs_async_data = 1, |
924 | .async_size = sizeof(struct io_async_connect), | |
d3656344 | 925 | }, |
0463b6c5 | 926 | [IORING_OP_FALLOCATE] = { |
d3656344 | 927 | .needs_file = 1, |
d3656344 | 928 | }, |
44526bed JA |
929 | [IORING_OP_OPENAT] = {}, |
930 | [IORING_OP_CLOSE] = {}, | |
931 | [IORING_OP_FILES_UPDATE] = {}, | |
932 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 933 | [IORING_OP_READ] = { |
3a6820f2 JA |
934 | .needs_file = 1, |
935 | .unbound_nonreg_file = 1, | |
8a72758c | 936 | .pollin = 1, |
bcda7baa | 937 | .buffer_select = 1, |
27926b68 | 938 | .plug = 1, |
e8c2bc1f | 939 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 940 | }, |
0463b6c5 | 941 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
942 | .needs_file = 1, |
943 | .unbound_nonreg_file = 1, | |
8a72758c | 944 | .pollout = 1, |
27926b68 | 945 | .plug = 1, |
e8c2bc1f | 946 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 947 | }, |
0463b6c5 | 948 | [IORING_OP_FADVISE] = { |
4840e418 | 949 | .needs_file = 1, |
c1ca757b | 950 | }, |
44526bed | 951 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 952 | [IORING_OP_SEND] = { |
fddaface JA |
953 | .needs_file = 1, |
954 | .unbound_nonreg_file = 1, | |
8a72758c | 955 | .pollout = 1, |
fddaface | 956 | }, |
0463b6c5 | 957 | [IORING_OP_RECV] = { |
fddaface JA |
958 | .needs_file = 1, |
959 | .unbound_nonreg_file = 1, | |
8a72758c | 960 | .pollin = 1, |
bcda7baa | 961 | .buffer_select = 1, |
fddaface | 962 | }, |
0463b6c5 | 963 | [IORING_OP_OPENAT2] = { |
cebdb986 | 964 | }, |
3e4827b0 JA |
965 | [IORING_OP_EPOLL_CTL] = { |
966 | .unbound_nonreg_file = 1, | |
3e4827b0 | 967 | }, |
7d67af2c PB |
968 | [IORING_OP_SPLICE] = { |
969 | .needs_file = 1, | |
970 | .hash_reg_file = 1, | |
971 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
972 | }, |
973 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 974 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
975 | [IORING_OP_TEE] = { |
976 | .needs_file = 1, | |
977 | .hash_reg_file = 1, | |
978 | .unbound_nonreg_file = 1, | |
979 | }, | |
36f4fa68 JA |
980 | [IORING_OP_SHUTDOWN] = { |
981 | .needs_file = 1, | |
982 | }, | |
44526bed JA |
983 | [IORING_OP_RENAMEAT] = {}, |
984 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
985 | }; |
986 | ||
9936c7c2 PB |
987 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
988 | struct task_struct *task, | |
989 | struct files_struct *files); | |
37d1e2e3 | 990 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
269bbe5f | 991 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 992 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 993 | struct io_ring_ctx *ctx); |
f2303b1f | 994 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
1ffc5422 | 995 | |
23faba36 | 996 | static bool io_rw_reissue(struct io_kiocb *req); |
78e19bbe | 997 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 998 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 999 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1000 | static void io_double_put_req(struct io_kiocb *req); |
c7dae4ba JA |
1001 | static void io_dismantle_req(struct io_kiocb *req); |
1002 | static void io_put_task(struct task_struct *task, int nr); | |
1003 | static void io_queue_next(struct io_kiocb *req); | |
94ae5e77 | 1004 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1005 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1006 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1007 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1008 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1009 | unsigned nr_args); |
3ca405eb | 1010 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1011 | static struct file *io_file_get(struct io_submit_state *state, |
1012 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1013 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1014 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1015 | |
847595de PB |
1016 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1017 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1018 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1019 | const struct iovec *fast_iov, | |
227c0c96 | 1020 | struct iov_iter *iter, bool force); |
907d1df3 | 1021 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1022 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1023 | struct io_ring_ctx *ctx); | |
de0617e4 | 1024 | |
2b188cc1 JA |
1025 | static struct kmem_cache *req_cachep; |
1026 | ||
0918682b | 1027 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1028 | |
1029 | struct sock *io_uring_get_socket(struct file *file) | |
1030 | { | |
1031 | #if defined(CONFIG_UNIX) | |
1032 | if (file->f_op == &io_uring_fops) { | |
1033 | struct io_ring_ctx *ctx = file->private_data; | |
1034 | ||
1035 | return ctx->ring_sock->sk; | |
1036 | } | |
1037 | #endif | |
1038 | return NULL; | |
1039 | } | |
1040 | EXPORT_SYMBOL(io_uring_get_socket); | |
1041 | ||
f2f87370 PB |
1042 | #define io_for_each_link(pos, head) \ |
1043 | for (pos = (head); pos; pos = pos->link) | |
1044 | ||
3ca405eb PB |
1045 | static inline void io_clean_op(struct io_kiocb *req) |
1046 | { | |
9d5c8190 | 1047 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1048 | __io_clean_op(req); |
1049 | } | |
1050 | ||
36f72fe2 PB |
1051 | static inline void io_set_resource_node(struct io_kiocb *req) |
1052 | { | |
1053 | struct io_ring_ctx *ctx = req->ctx; | |
1054 | ||
269bbe5f BM |
1055 | if (!req->fixed_rsrc_refs) { |
1056 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1057 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1058 | } |
1059 | } | |
1060 | ||
88f171ab PB |
1061 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1062 | { | |
1063 | if (!percpu_ref_tryget(ref)) { | |
1064 | /* already at zero, wait for ->release() */ | |
1065 | if (!try_wait_for_completion(compl)) | |
1066 | synchronize_rcu(); | |
1067 | return false; | |
1068 | } | |
1069 | ||
1070 | percpu_ref_resurrect(ref); | |
1071 | reinit_completion(compl); | |
1072 | percpu_ref_put(ref); | |
1073 | return true; | |
1074 | } | |
1075 | ||
08d23634 PB |
1076 | static bool io_match_task(struct io_kiocb *head, |
1077 | struct task_struct *task, | |
1078 | struct files_struct *files) | |
1079 | { | |
1080 | struct io_kiocb *req; | |
1081 | ||
84965ff8 JA |
1082 | if (task && head->task != task) { |
1083 | /* in terms of cancelation, always match if req task is dead */ | |
1084 | if (head->task->flags & PF_EXITING) | |
1085 | return true; | |
08d23634 | 1086 | return false; |
84965ff8 | 1087 | } |
08d23634 PB |
1088 | if (!files) |
1089 | return true; | |
1090 | ||
1091 | io_for_each_link(req, head) { | |
02a13674 JA |
1092 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1093 | continue; | |
1094 | if (req->file && req->file->f_op == &io_uring_fops) | |
1095 | return true; | |
4379bf8b | 1096 | if (req->task->files == files) |
08d23634 PB |
1097 | return true; |
1098 | } | |
1099 | return false; | |
1100 | } | |
1101 | ||
c40f6379 JA |
1102 | static inline void req_set_fail_links(struct io_kiocb *req) |
1103 | { | |
1104 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1105 | req->flags |= REQ_F_FAIL_LINK; | |
1106 | } | |
4a38aed2 | 1107 | |
ec99ca6c PB |
1108 | static inline void __io_req_init_async(struct io_kiocb *req) |
1109 | { | |
1110 | memset(&req->work, 0, sizeof(req->work)); | |
1111 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1112 | } | |
1113 | ||
7cdaf587 XW |
1114 | /* |
1115 | * Note: must call io_req_init_async() for the first time you | |
1116 | * touch any members of io_wq_work. | |
1117 | */ | |
1118 | static inline void io_req_init_async(struct io_kiocb *req) | |
1119 | { | |
1120 | if (req->flags & REQ_F_WORK_INITIALIZED) | |
1121 | return; | |
1122 | ||
ec99ca6c | 1123 | __io_req_init_async(req); |
7cdaf587 XW |
1124 | } |
1125 | ||
2b188cc1 JA |
1126 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1127 | { | |
1128 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1129 | ||
0f158b4c | 1130 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1131 | } |
1132 | ||
8eb7e2d0 PB |
1133 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1134 | { | |
1135 | return !req->timeout.off; | |
1136 | } | |
1137 | ||
2b188cc1 JA |
1138 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1139 | { | |
1140 | struct io_ring_ctx *ctx; | |
78076bb6 | 1141 | int hash_bits; |
2b188cc1 JA |
1142 | |
1143 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1144 | if (!ctx) | |
1145 | return NULL; | |
1146 | ||
78076bb6 JA |
1147 | /* |
1148 | * Use 5 bits less than the max cq entries, that should give us around | |
1149 | * 32 entries per hash list if totally full and uniformly spread. | |
1150 | */ | |
1151 | hash_bits = ilog2(p->cq_entries); | |
1152 | hash_bits -= 5; | |
1153 | if (hash_bits <= 0) | |
1154 | hash_bits = 1; | |
1155 | ctx->cancel_hash_bits = hash_bits; | |
1156 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1157 | GFP_KERNEL); | |
1158 | if (!ctx->cancel_hash) | |
1159 | goto err; | |
1160 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1161 | ||
21482896 | 1162 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1163 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1164 | goto err; | |
2b188cc1 JA |
1165 | |
1166 | ctx->flags = p->flags; | |
90554200 | 1167 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1168 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1169 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1170 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1171 | init_completion(&ctx->ref_comp); |
1172 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1173 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1174 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1175 | mutex_init(&ctx->uring_lock); |
1176 | init_waitqueue_head(&ctx->wait); | |
1177 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1178 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1179 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1180 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1181 | spin_lock_init(&ctx->inflight_lock); |
1182 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1183 | spin_lock_init(&ctx->rsrc_ref_lock); |
1184 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1185 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1186 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1187 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1188 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1189 | return ctx; |
206aefde | 1190 | err: |
78076bb6 | 1191 | kfree(ctx->cancel_hash); |
206aefde JA |
1192 | kfree(ctx); |
1193 | return NULL; | |
2b188cc1 JA |
1194 | } |
1195 | ||
9cf7c104 | 1196 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1197 | { |
2bc9930e JA |
1198 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1199 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1200 | |
9cf7c104 | 1201 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1202 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1203 | } |
de0617e4 | 1204 | |
9d858b21 | 1205 | return false; |
de0617e4 JA |
1206 | } |
1207 | ||
4edf20f9 | 1208 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1209 | { |
7cdaf587 | 1210 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1211 | return; |
51a4cc11 | 1212 | |
4379bf8b JA |
1213 | if (req->work.creds) { |
1214 | put_cred(req->work.creds); | |
1215 | req->work.creds = NULL; | |
1216 | } | |
34e08fed PB |
1217 | if (req->flags & REQ_F_INFLIGHT) { |
1218 | struct io_ring_ctx *ctx = req->ctx; | |
1219 | struct io_uring_task *tctx = req->task->io_uring; | |
1220 | unsigned long flags; | |
1221 | ||
1222 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1223 | list_del(&req->inflight_entry); | |
1224 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1225 | req->flags &= ~REQ_F_INFLIGHT; | |
1226 | if (atomic_read(&tctx->in_idle)) | |
1227 | wake_up(&tctx->wait); | |
1228 | } | |
51a4cc11 | 1229 | |
e86d0047 | 1230 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
561fb04a JA |
1231 | } |
1232 | ||
ce3d5aae PB |
1233 | static void io_req_track_inflight(struct io_kiocb *req) |
1234 | { | |
1235 | struct io_ring_ctx *ctx = req->ctx; | |
1236 | ||
1237 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1238 | io_req_init_async(req); | |
1239 | req->flags |= REQ_F_INFLIGHT; | |
1240 | ||
1241 | spin_lock_irq(&ctx->inflight_lock); | |
1242 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1243 | spin_unlock_irq(&ctx->inflight_lock); | |
1244 | } | |
1245 | } | |
1246 | ||
1e6fa521 JA |
1247 | static void io_prep_async_work(struct io_kiocb *req) |
1248 | { | |
1249 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1250 | struct io_ring_ctx *ctx = req->ctx; |
1251 | ||
1252 | io_req_init_async(req); | |
1253 | ||
feaadc4f PB |
1254 | if (req->flags & REQ_F_FORCE_ASYNC) |
1255 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1256 | ||
1e6fa521 JA |
1257 | if (req->flags & REQ_F_ISREG) { |
1258 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1259 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1260 | } else { | |
1261 | if (def->unbound_nonreg_file) | |
1262 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1263 | } | |
4379bf8b JA |
1264 | if (!req->work.creds) |
1265 | req->work.creds = get_current_cred(); | |
561fb04a | 1266 | } |
cccf0ee8 | 1267 | |
cbdcb435 | 1268 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1269 | { |
cbdcb435 | 1270 | struct io_kiocb *cur; |
54a91f3b | 1271 | |
f2f87370 PB |
1272 | io_for_each_link(cur, req) |
1273 | io_prep_async_work(cur); | |
561fb04a JA |
1274 | } |
1275 | ||
7271ef3a | 1276 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1277 | { |
a197f664 | 1278 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1279 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1280 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1281 | |
3bfe6106 JA |
1282 | BUG_ON(!tctx); |
1283 | BUG_ON(!tctx->io_wq); | |
1284 | ||
8766dd51 PB |
1285 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1286 | &req->work, req->flags); | |
5aa75ed5 | 1287 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a | 1288 | return link; |
18d9be1a JA |
1289 | } |
1290 | ||
cbdcb435 PB |
1291 | static void io_queue_async_work(struct io_kiocb *req) |
1292 | { | |
7271ef3a JA |
1293 | struct io_kiocb *link; |
1294 | ||
cbdcb435 PB |
1295 | /* init ->work of the whole link before punting */ |
1296 | io_prep_async_link(req); | |
7271ef3a JA |
1297 | link = __io_queue_async_work(req); |
1298 | ||
1299 | if (link) | |
1300 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1301 | } |
1302 | ||
5262f567 JA |
1303 | static void io_kill_timeout(struct io_kiocb *req) |
1304 | { | |
e8c2bc1f | 1305 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1306 | int ret; |
1307 | ||
e8c2bc1f | 1308 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1309 | if (ret != -1) { |
01cec8c1 PB |
1310 | atomic_set(&req->ctx->cq_timeouts, |
1311 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1312 | list_del_init(&req->timeout.list); |
78e19bbe | 1313 | io_cqring_fill_event(req, 0); |
216578e5 | 1314 | io_put_req_deferred(req, 1); |
5262f567 JA |
1315 | } |
1316 | } | |
1317 | ||
76e1b642 JA |
1318 | /* |
1319 | * Returns true if we found and killed one or more timeouts | |
1320 | */ | |
6b81928d PB |
1321 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1322 | struct files_struct *files) | |
5262f567 JA |
1323 | { |
1324 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1325 | int canceled = 0; |
5262f567 JA |
1326 | |
1327 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1328 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1329 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1330 | io_kill_timeout(req); |
76e1b642 JA |
1331 | canceled++; |
1332 | } | |
f3606e3a | 1333 | } |
5262f567 | 1334 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1335 | return canceled != 0; |
5262f567 JA |
1336 | } |
1337 | ||
04518945 | 1338 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1339 | { |
04518945 | 1340 | do { |
27dc8338 PB |
1341 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1342 | struct io_defer_entry, list); | |
de0617e4 | 1343 | |
9cf7c104 | 1344 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1345 | break; |
27dc8338 | 1346 | list_del_init(&de->list); |
907d1df3 | 1347 | io_req_task_queue(de->req); |
27dc8338 | 1348 | kfree(de); |
04518945 PB |
1349 | } while (!list_empty(&ctx->defer_list)); |
1350 | } | |
1351 | ||
360428f8 | 1352 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1353 | { |
f010505b MDG |
1354 | u32 seq; |
1355 | ||
1356 | if (list_empty(&ctx->timeout_list)) | |
1357 | return; | |
1358 | ||
1359 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1360 | ||
1361 | do { | |
1362 | u32 events_needed, events_got; | |
360428f8 | 1363 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1364 | struct io_kiocb, timeout.list); |
de0617e4 | 1365 | |
8eb7e2d0 | 1366 | if (io_is_timeout_noseq(req)) |
360428f8 | 1367 | break; |
f010505b MDG |
1368 | |
1369 | /* | |
1370 | * Since seq can easily wrap around over time, subtract | |
1371 | * the last seq at which timeouts were flushed before comparing. | |
1372 | * Assuming not more than 2^31-1 events have happened since, | |
1373 | * these subtractions won't have wrapped, so we can check if | |
1374 | * target is in [last_seq, current_seq] by comparing the two. | |
1375 | */ | |
1376 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1377 | events_got = seq - ctx->cq_last_tm_flush; | |
1378 | if (events_got < events_needed) | |
360428f8 | 1379 | break; |
bfe68a22 | 1380 | |
135fcde8 | 1381 | list_del_init(&req->timeout.list); |
5262f567 | 1382 | io_kill_timeout(req); |
f010505b MDG |
1383 | } while (!list_empty(&ctx->timeout_list)); |
1384 | ||
1385 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1386 | } |
5262f567 | 1387 | |
360428f8 PB |
1388 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1389 | { | |
1390 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1391 | |
1392 | /* order cqe stores with ring update */ | |
1393 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1394 | |
04518945 PB |
1395 | if (unlikely(!list_empty(&ctx->defer_list))) |
1396 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1397 | } |
1398 | ||
90554200 JA |
1399 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1400 | { | |
1401 | struct io_rings *r = ctx->rings; | |
1402 | ||
1403 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1404 | } | |
1405 | ||
888aae2e PB |
1406 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1407 | { | |
1408 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1409 | } | |
1410 | ||
2b188cc1 JA |
1411 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1412 | { | |
75b28aff | 1413 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1414 | unsigned tail; |
1415 | ||
115e12e5 SB |
1416 | /* |
1417 | * writes to the cq entry need to come after reading head; the | |
1418 | * control dependency is enough as we're using WRITE_ONCE to | |
1419 | * fill the cq entry | |
1420 | */ | |
888aae2e | 1421 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1422 | return NULL; |
1423 | ||
888aae2e | 1424 | tail = ctx->cached_cq_tail++; |
75b28aff | 1425 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1426 | } |
1427 | ||
f2842ab5 JA |
1428 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1429 | { | |
f0b493e6 JA |
1430 | if (!ctx->cq_ev_fd) |
1431 | return false; | |
7e55a19c SG |
1432 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1433 | return false; | |
f2842ab5 JA |
1434 | if (!ctx->eventfd_async) |
1435 | return true; | |
b41e9852 | 1436 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1437 | } |
1438 | ||
b41e9852 | 1439 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1440 | { |
b1445e59 PB |
1441 | /* see waitqueue_active() comment */ |
1442 | smp_mb(); | |
1443 | ||
1d7bb1d5 JA |
1444 | if (waitqueue_active(&ctx->wait)) |
1445 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1446 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1447 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1448 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1449 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1450 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1451 | wake_up_interruptible(&ctx->cq_wait); |
1452 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1453 | } | |
1d7bb1d5 JA |
1454 | } |
1455 | ||
80c18e4a PB |
1456 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1457 | { | |
b1445e59 PB |
1458 | /* see waitqueue_active() comment */ |
1459 | smp_mb(); | |
1460 | ||
80c18e4a PB |
1461 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1462 | if (waitqueue_active(&ctx->wait)) | |
1463 | wake_up(&ctx->wait); | |
1464 | } | |
1465 | if (io_should_trigger_evfd(ctx)) | |
1466 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1467 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1468 | wake_up_interruptible(&ctx->cq_wait); |
1469 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1470 | } | |
80c18e4a PB |
1471 | } |
1472 | ||
c4a2ed72 | 1473 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1474 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1475 | struct task_struct *tsk, | |
1476 | struct files_struct *files) | |
1d7bb1d5 JA |
1477 | { |
1478 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1479 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1480 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1481 | unsigned long flags; |
b18032bb | 1482 | bool all_flushed, posted; |
1d7bb1d5 JA |
1483 | LIST_HEAD(list); |
1484 | ||
e23de15f PB |
1485 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1486 | return false; | |
1d7bb1d5 | 1487 | |
b18032bb | 1488 | posted = false; |
1d7bb1d5 | 1489 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1490 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1491 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1492 | continue; |
1493 | ||
1d7bb1d5 JA |
1494 | cqe = io_get_cqring(ctx); |
1495 | if (!cqe && !force) | |
1496 | break; | |
1497 | ||
40d8ddd4 | 1498 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1499 | if (cqe) { |
1500 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1501 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1502 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1503 | } else { |
2c3bac6d | 1504 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1505 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1506 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1507 | } |
b18032bb | 1508 | posted = true; |
1d7bb1d5 JA |
1509 | } |
1510 | ||
09e88404 PB |
1511 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1512 | if (all_flushed) { | |
1513 | clear_bit(0, &ctx->sq_check_overflow); | |
1514 | clear_bit(0, &ctx->cq_check_overflow); | |
1515 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1516 | } | |
46930143 | 1517 | |
b18032bb JA |
1518 | if (posted) |
1519 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1520 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1521 | if (posted) |
1522 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1523 | |
1524 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1525 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1526 | list_del(&req->compl.list); | |
ec9c02ad | 1527 | io_put_req(req); |
1d7bb1d5 | 1528 | } |
c4a2ed72 | 1529 | |
09e88404 | 1530 | return all_flushed; |
1d7bb1d5 JA |
1531 | } |
1532 | ||
6c503150 PB |
1533 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1534 | struct task_struct *tsk, | |
1535 | struct files_struct *files) | |
1536 | { | |
1537 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1538 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1539 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1540 | mutex_lock(&ctx->uring_lock); | |
1541 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1542 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1543 | mutex_unlock(&ctx->uring_lock); | |
1544 | } | |
1545 | } | |
1546 | ||
bcda7baa | 1547 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1548 | { |
78e19bbe | 1549 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1550 | struct io_uring_cqe *cqe; |
1551 | ||
78e19bbe | 1552 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1553 | |
2b188cc1 JA |
1554 | /* |
1555 | * If we can't get a cq entry, userspace overflowed the | |
1556 | * submission (by quite a lot). Increment the overflow count in | |
1557 | * the ring. | |
1558 | */ | |
1559 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1560 | if (likely(cqe)) { |
78e19bbe | 1561 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1562 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1563 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1564 | } else if (ctx->cq_overflow_flushed || |
1565 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1566 | /* |
1567 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1568 | * then we cannot store the request for later flushing, we need | |
1569 | * to drop it on the floor. | |
1570 | */ | |
2c3bac6d PB |
1571 | ctx->cached_cq_overflow++; |
1572 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1573 | } else { |
ad3eb2c8 JA |
1574 | if (list_empty(&ctx->cq_overflow_list)) { |
1575 | set_bit(0, &ctx->sq_check_overflow); | |
1576 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1577 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1578 | } |
40d8ddd4 | 1579 | io_clean_op(req); |
1d7bb1d5 | 1580 | req->result = res; |
0f7e466b | 1581 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1582 | refcount_inc(&req->refs); |
1583 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1584 | } |
1585 | } | |
1586 | ||
bcda7baa JA |
1587 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1588 | { | |
1589 | __io_cqring_fill_event(req, res, 0); | |
1590 | } | |
1591 | ||
c7dae4ba JA |
1592 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1593 | unsigned int cflags) | |
2b188cc1 | 1594 | { |
78e19bbe | 1595 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1596 | unsigned long flags; |
1597 | ||
1598 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1599 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1600 | io_commit_cqring(ctx); |
c7dae4ba JA |
1601 | /* |
1602 | * If we're the last reference to this request, add to our locked | |
1603 | * free_list cache. | |
1604 | */ | |
1605 | if (refcount_dec_and_test(&req->refs)) { | |
1606 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1607 | ||
1608 | io_dismantle_req(req); | |
1609 | io_put_task(req->task, 1); | |
1610 | list_add(&req->compl.list, &cs->locked_free_list); | |
1611 | cs->locked_free_nr++; | |
1612 | } else | |
1613 | req = NULL; | |
2b188cc1 JA |
1614 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1615 | ||
8c838788 | 1616 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1617 | if (req) { |
1618 | io_queue_next(req); | |
1619 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1620 | } |
229a7b63 JA |
1621 | } |
1622 | ||
a38d68db | 1623 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1624 | unsigned int cflags) |
229a7b63 | 1625 | { |
a38d68db PB |
1626 | io_clean_op(req); |
1627 | req->result = res; | |
1628 | req->compl.cflags = cflags; | |
e342c807 | 1629 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1630 | } |
1631 | ||
889fca73 PB |
1632 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1633 | long res, unsigned cflags) | |
bcda7baa | 1634 | { |
889fca73 PB |
1635 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1636 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1637 | else |
c7dae4ba | 1638 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1639 | } |
1640 | ||
a38d68db | 1641 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1642 | { |
889fca73 | 1643 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1644 | } |
1645 | ||
c7dae4ba | 1646 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1647 | { |
c7dae4ba JA |
1648 | struct io_submit_state *state = &ctx->submit_state; |
1649 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 1650 | struct io_kiocb *req = NULL; |
0ddf92e8 | 1651 | |
c7dae4ba JA |
1652 | /* |
1653 | * If we have more than a batch's worth of requests in our IRQ side | |
1654 | * locked cache, grab the lock and move them over to our submission | |
1655 | * side cache. | |
1656 | */ | |
1657 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
1658 | spin_lock_irq(&ctx->completion_lock); | |
1659 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1660 | cs->locked_free_nr = 0; | |
1661 | spin_unlock_irq(&ctx->completion_lock); | |
1662 | } | |
0ddf92e8 | 1663 | |
c7dae4ba JA |
1664 | while (!list_empty(&cs->free_list)) { |
1665 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
1666 | compl.list); |
1667 | list_del(&req->compl.list); | |
e5d1bc0a PB |
1668 | state->reqs[state->free_reqs++] = req; |
1669 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
1670 | break; | |
1b4c351f JA |
1671 | } |
1672 | ||
e5d1bc0a | 1673 | return req != NULL; |
0ddf92e8 JA |
1674 | } |
1675 | ||
e5d1bc0a | 1676 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1677 | { |
e5d1bc0a PB |
1678 | struct io_submit_state *state = &ctx->submit_state; |
1679 | ||
1680 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1681 | ||
f6b6c7d6 | 1682 | if (!state->free_reqs) { |
291b2821 | 1683 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1684 | int ret; |
1685 | ||
c7dae4ba | 1686 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1687 | goto got_req; |
1688 | ||
bf019da7 PB |
1689 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1690 | state->reqs); | |
fd6fab2c JA |
1691 | |
1692 | /* | |
1693 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1694 | * retry single alloc to be on the safe side. | |
1695 | */ | |
1696 | if (unlikely(ret <= 0)) { | |
1697 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1698 | if (!state->reqs[0]) | |
3893f39f | 1699 | return NULL; |
fd6fab2c JA |
1700 | ret = 1; |
1701 | } | |
291b2821 | 1702 | state->free_reqs = ret; |
2b188cc1 | 1703 | } |
e5d1bc0a | 1704 | got_req: |
291b2821 PB |
1705 | state->free_reqs--; |
1706 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1707 | } |
1708 | ||
8da11c19 PB |
1709 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1710 | bool fixed) | |
1711 | { | |
36f72fe2 | 1712 | if (!fixed) |
8da11c19 PB |
1713 | fput(file); |
1714 | } | |
1715 | ||
4edf20f9 | 1716 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1717 | { |
3ca405eb | 1718 | io_clean_op(req); |
929a3af9 | 1719 | |
e8c2bc1f JA |
1720 | if (req->async_data) |
1721 | kfree(req->async_data); | |
8da11c19 PB |
1722 | if (req->file) |
1723 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
1724 | if (req->fixed_rsrc_refs) |
1725 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 1726 | io_req_clean_work(req); |
e65ef56d JA |
1727 | } |
1728 | ||
7c660731 PB |
1729 | static inline void io_put_task(struct task_struct *task, int nr) |
1730 | { | |
1731 | struct io_uring_task *tctx = task->io_uring; | |
1732 | ||
1733 | percpu_counter_sub(&tctx->inflight, nr); | |
1734 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1735 | wake_up(&tctx->wait); | |
1736 | put_task_struct_many(task, nr); | |
1737 | } | |
1738 | ||
216578e5 | 1739 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1740 | { |
51a4cc11 | 1741 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1742 | |
216578e5 | 1743 | io_dismantle_req(req); |
7c660731 | 1744 | io_put_task(req->task, 1); |
c6ca97b3 | 1745 | |
3893f39f | 1746 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1747 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1748 | } |
1749 | ||
f2f87370 PB |
1750 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1751 | { | |
1752 | struct io_kiocb *nxt = req->link; | |
1753 | ||
1754 | req->link = nxt->link; | |
1755 | nxt->link = NULL; | |
1756 | } | |
1757 | ||
c9abd7ad | 1758 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1759 | { |
a197f664 | 1760 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1761 | struct io_kiocb *link; |
c9abd7ad PB |
1762 | bool cancelled = false; |
1763 | unsigned long flags; | |
7c86ffee | 1764 | |
c9abd7ad | 1765 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1766 | link = req->link; |
1767 | ||
900fad45 PB |
1768 | /* |
1769 | * Can happen if a linked timeout fired and link had been like | |
1770 | * req -> link t-out -> link t-out [-> ...] | |
1771 | */ | |
c9abd7ad PB |
1772 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1773 | struct io_timeout_data *io = link->async_data; | |
1774 | int ret; | |
7c86ffee | 1775 | |
f2f87370 | 1776 | io_remove_next_linked(req); |
90cd7e42 | 1777 | link->timeout.head = NULL; |
c9abd7ad PB |
1778 | ret = hrtimer_try_to_cancel(&io->timer); |
1779 | if (ret != -1) { | |
1780 | io_cqring_fill_event(link, -ECANCELED); | |
1781 | io_commit_cqring(ctx); | |
1782 | cancelled = true; | |
1783 | } | |
1784 | } | |
7c86ffee | 1785 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 1786 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 1787 | |
c9abd7ad | 1788 | if (cancelled) { |
7c86ffee | 1789 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
1790 | io_put_req(link); |
1791 | } | |
7c86ffee PB |
1792 | } |
1793 | ||
9e645e11 | 1794 | |
d148ca4b | 1795 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1796 | { |
f2f87370 | 1797 | struct io_kiocb *link, *nxt; |
2665abfd | 1798 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 1799 | unsigned long flags; |
9e645e11 | 1800 | |
d148ca4b | 1801 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1802 | link = req->link; |
1803 | req->link = NULL; | |
9e645e11 | 1804 | |
f2f87370 PB |
1805 | while (link) { |
1806 | nxt = link->link; | |
1807 | link->link = NULL; | |
2665abfd | 1808 | |
f2f87370 | 1809 | trace_io_uring_fail_link(req, link); |
7c86ffee | 1810 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
1811 | |
1812 | /* | |
1813 | * It's ok to free under spinlock as they're not linked anymore, | |
1814 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
1815 | * work.fs->lock. | |
1816 | */ | |
1817 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
1818 | io_put_req_deferred(link, 2); | |
1819 | else | |
1820 | io_double_put_req(link); | |
f2f87370 | 1821 | link = nxt; |
9e645e11 | 1822 | } |
2665abfd | 1823 | io_commit_cqring(ctx); |
216578e5 | 1824 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 1825 | |
2665abfd | 1826 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1827 | } |
1828 | ||
3fa5e0f3 | 1829 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1830 | { |
7c86ffee PB |
1831 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1832 | io_kill_linked_timeout(req); | |
944e58bf | 1833 | |
9e645e11 JA |
1834 | /* |
1835 | * If LINK is set, we have dependent requests in this chain. If we | |
1836 | * didn't fail this request, queue the first one up, moving any other | |
1837 | * dependencies to the next request. In case of failure, fail the rest | |
1838 | * of the chain. | |
1839 | */ | |
f2f87370 PB |
1840 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
1841 | struct io_kiocb *nxt = req->link; | |
1842 | ||
1843 | req->link = NULL; | |
1844 | return nxt; | |
1845 | } | |
9b5f7bd9 PB |
1846 | io_fail_links(req); |
1847 | return NULL; | |
4d7dd462 | 1848 | } |
9e645e11 | 1849 | |
f2f87370 | 1850 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1851 | { |
cdbff982 | 1852 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1853 | return NULL; |
1854 | return __io_req_find_next(req); | |
1855 | } | |
1856 | ||
7cbf1722 | 1857 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1858 | { |
65453d1e | 1859 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1860 | struct io_wq_work_list list; |
1861 | struct io_wq_work_node *node; | |
c2c4c83c | 1862 | |
7cbf1722 JA |
1863 | if (wq_list_empty(&tctx->task_list)) |
1864 | return false; | |
6200b0ae | 1865 | |
0b81e80c | 1866 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1867 | list = tctx->task_list; |
1868 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1869 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1870 | |
7cbf1722 JA |
1871 | node = list.first; |
1872 | while (node) { | |
1873 | struct io_wq_work_node *next = node->next; | |
65453d1e | 1874 | struct io_ring_ctx *this_ctx; |
7cbf1722 | 1875 | struct io_kiocb *req; |
0ba9c9ed | 1876 | |
7cbf1722 | 1877 | req = container_of(node, struct io_kiocb, io_task_work.node); |
65453d1e | 1878 | this_ctx = req->ctx; |
7cbf1722 JA |
1879 | req->task_work.func(&req->task_work); |
1880 | node = next; | |
65453d1e JA |
1881 | |
1882 | if (!ctx) { | |
1883 | ctx = this_ctx; | |
1884 | } else if (ctx != this_ctx) { | |
1885 | mutex_lock(&ctx->uring_lock); | |
1886 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1887 | mutex_unlock(&ctx->uring_lock); | |
1888 | ctx = this_ctx; | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | if (ctx && ctx->submit_state.comp.nr) { | |
1893 | mutex_lock(&ctx->uring_lock); | |
1894 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1895 | mutex_unlock(&ctx->uring_lock); | |
7cbf1722 JA |
1896 | } |
1897 | ||
1898 | return list.first != NULL; | |
c2c4c83c JA |
1899 | } |
1900 | ||
7cbf1722 | 1901 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1902 | { |
7cbf1722 | 1903 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1904 | |
7cbf1722 JA |
1905 | while (__tctx_task_work(tctx)) |
1906 | cond_resched(); | |
1907 | ||
1908 | clear_bit(0, &tctx->task_state); | |
1909 | } | |
1910 | ||
1911 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
1912 | enum task_work_notify_mode notify) | |
1913 | { | |
1914 | struct io_uring_task *tctx = tsk->io_uring; | |
1915 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 1916 | unsigned long flags; |
7cbf1722 JA |
1917 | int ret; |
1918 | ||
1919 | WARN_ON_ONCE(!tctx); | |
1920 | ||
0b81e80c | 1921 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1922 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1923 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1924 | |
1925 | /* task_work already pending, we're done */ | |
1926 | if (test_bit(0, &tctx->task_state) || | |
1927 | test_and_set_bit(0, &tctx->task_state)) | |
1928 | return 0; | |
1929 | ||
1930 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
1931 | return 0; | |
1932 | ||
1933 | /* | |
1934 | * Slow path - we failed, find and delete work. if the work is not | |
1935 | * in the list, it got run and we're fine. | |
1936 | */ | |
1937 | ret = 0; | |
0b81e80c | 1938 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1939 | wq_list_for_each(node, prev, &tctx->task_list) { |
1940 | if (&req->io_task_work.node == node) { | |
1941 | wq_list_del(&tctx->task_list, node, prev); | |
1942 | ret = 1; | |
1943 | break; | |
1944 | } | |
1945 | } | |
0b81e80c | 1946 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1947 | clear_bit(0, &tctx->task_state); |
1948 | return ret; | |
1949 | } | |
1950 | ||
355fb9e2 | 1951 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
1952 | { |
1953 | struct task_struct *tsk = req->task; | |
1954 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
1955 | enum task_work_notify_mode notify; |
1956 | int ret; | |
c2c4c83c | 1957 | |
6200b0ae JA |
1958 | if (tsk->flags & PF_EXITING) |
1959 | return -ESRCH; | |
1960 | ||
c2c4c83c | 1961 | /* |
0ba9c9ed JA |
1962 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1963 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1964 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1965 | * will do the job. | |
c2c4c83c | 1966 | */ |
91989c70 | 1967 | notify = TWA_NONE; |
355fb9e2 | 1968 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
1969 | notify = TWA_SIGNAL; |
1970 | ||
7cbf1722 | 1971 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
1972 | if (!ret) |
1973 | wake_up_process(tsk); | |
0ba9c9ed | 1974 | |
c2c4c83c JA |
1975 | return ret; |
1976 | } | |
1977 | ||
eab30c4d | 1978 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 1979 | task_work_func_t cb) |
eab30c4d | 1980 | { |
7c25c0d1 JA |
1981 | struct io_ring_ctx *ctx = req->ctx; |
1982 | struct callback_head *head; | |
eab30c4d PB |
1983 | |
1984 | init_task_work(&req->task_work, cb); | |
7c25c0d1 JA |
1985 | do { |
1986 | head = READ_ONCE(ctx->exit_task_work); | |
1987 | req->task_work.next = head; | |
1988 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); | |
eab30c4d PB |
1989 | } |
1990 | ||
c40f6379 JA |
1991 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1992 | { | |
1993 | struct io_ring_ctx *ctx = req->ctx; | |
1994 | ||
1995 | spin_lock_irq(&ctx->completion_lock); | |
1996 | io_cqring_fill_event(req, error); | |
1997 | io_commit_cqring(ctx); | |
1998 | spin_unlock_irq(&ctx->completion_lock); | |
1999 | ||
2000 | io_cqring_ev_posted(ctx); | |
2001 | req_set_fail_links(req); | |
2002 | io_double_put_req(req); | |
2003 | } | |
2004 | ||
2005 | static void io_req_task_cancel(struct callback_head *cb) | |
2006 | { | |
2007 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2008 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2009 | |
792bb6eb | 2010 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 2011 | __io_req_task_cancel(req, req->result); |
792bb6eb | 2012 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 2013 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2014 | } |
2015 | ||
2016 | static void __io_req_task_submit(struct io_kiocb *req) | |
2017 | { | |
2018 | struct io_ring_ctx *ctx = req->ctx; | |
2019 | ||
04fc6c80 | 2020 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2021 | mutex_lock(&ctx->uring_lock); |
37d1e2e3 | 2022 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING)) |
c5eef2b9 | 2023 | __io_queue_sqe(req); |
81b6d05c | 2024 | else |
c40f6379 | 2025 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2026 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2027 | } |
2028 | ||
2029 | static void io_req_task_submit(struct callback_head *cb) | |
2030 | { | |
2031 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2032 | ||
2033 | __io_req_task_submit(req); | |
2034 | } | |
2035 | ||
2036 | static void io_req_task_queue(struct io_kiocb *req) | |
2037 | { | |
c40f6379 JA |
2038 | int ret; |
2039 | ||
7cbf1722 | 2040 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 2041 | ret = io_req_task_work_add(req); |
c40f6379 | 2042 | if (unlikely(ret)) { |
a3df7698 | 2043 | req->result = -ECANCELED; |
04fc6c80 | 2044 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 2045 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 2046 | } |
c40f6379 JA |
2047 | } |
2048 | ||
a3df7698 PB |
2049 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
2050 | { | |
2051 | percpu_ref_get(&req->ctx->refs); | |
2052 | req->result = ret; | |
2053 | req->task_work.func = io_req_task_cancel; | |
2054 | ||
2055 | if (unlikely(io_req_task_work_add(req))) | |
2056 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2057 | } | |
2058 | ||
f2f87370 | 2059 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2060 | { |
9b5f7bd9 | 2061 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2062 | |
2063 | if (nxt) | |
906a8c3f | 2064 | io_req_task_queue(nxt); |
c69f8dbe JL |
2065 | } |
2066 | ||
c3524383 | 2067 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2068 | { |
c3524383 PB |
2069 | io_queue_next(req); |
2070 | __io_free_req(req); | |
2071 | } | |
8766dd51 | 2072 | |
2d6500d4 | 2073 | struct req_batch { |
5af1d13e PB |
2074 | struct task_struct *task; |
2075 | int task_refs; | |
1b4c351f | 2076 | int ctx_refs; |
2d6500d4 PB |
2077 | }; |
2078 | ||
5af1d13e PB |
2079 | static inline void io_init_req_batch(struct req_batch *rb) |
2080 | { | |
5af1d13e | 2081 | rb->task_refs = 0; |
9ae72463 | 2082 | rb->ctx_refs = 0; |
5af1d13e PB |
2083 | rb->task = NULL; |
2084 | } | |
2085 | ||
2d6500d4 PB |
2086 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2087 | struct req_batch *rb) | |
2088 | { | |
6e833d53 | 2089 | if (rb->task) |
7c660731 | 2090 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2091 | if (rb->ctx_refs) |
2092 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2093 | } |
2094 | ||
6ff119a6 PB |
2095 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2096 | struct io_submit_state *state) | |
2d6500d4 | 2097 | { |
f2f87370 | 2098 | io_queue_next(req); |
2d6500d4 | 2099 | |
e3bc8e9d | 2100 | if (req->task != rb->task) { |
7c660731 PB |
2101 | if (rb->task) |
2102 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2103 | rb->task = req->task; |
2104 | rb->task_refs = 0; | |
5af1d13e | 2105 | } |
e3bc8e9d | 2106 | rb->task_refs++; |
9ae72463 | 2107 | rb->ctx_refs++; |
5af1d13e | 2108 | |
4edf20f9 | 2109 | io_dismantle_req(req); |
bd759045 | 2110 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2111 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2112 | else |
2113 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2114 | } |
2115 | ||
905c172f PB |
2116 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2117 | struct io_ring_ctx *ctx) | |
2118 | { | |
2119 | int i, nr = cs->nr; | |
2120 | struct io_kiocb *req; | |
2121 | struct req_batch rb; | |
2122 | ||
2123 | io_init_req_batch(&rb); | |
2124 | spin_lock_irq(&ctx->completion_lock); | |
2125 | for (i = 0; i < nr; i++) { | |
2126 | req = cs->reqs[i]; | |
2127 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2128 | } | |
2129 | io_commit_cqring(ctx); | |
2130 | spin_unlock_irq(&ctx->completion_lock); | |
2131 | ||
2132 | io_cqring_ev_posted(ctx); | |
2133 | for (i = 0; i < nr; i++) { | |
2134 | req = cs->reqs[i]; | |
2135 | ||
2136 | /* submission and completion refs */ | |
2137 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2138 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2139 | } |
2140 | ||
2141 | io_req_free_batch_finish(ctx, &rb); | |
2142 | cs->nr = 0; | |
7a743e22 PB |
2143 | } |
2144 | ||
ba816ad6 JA |
2145 | /* |
2146 | * Drop reference to request, return next in chain (if there is one) if this | |
2147 | * was the last reference to this request. | |
2148 | */ | |
9b5f7bd9 | 2149 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2150 | { |
9b5f7bd9 PB |
2151 | struct io_kiocb *nxt = NULL; |
2152 | ||
2a44f467 | 2153 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2154 | nxt = io_req_find_next(req); |
4d7dd462 | 2155 | __io_free_req(req); |
2a44f467 | 2156 | } |
9b5f7bd9 | 2157 | return nxt; |
2b188cc1 JA |
2158 | } |
2159 | ||
e65ef56d JA |
2160 | static void io_put_req(struct io_kiocb *req) |
2161 | { | |
2162 | if (refcount_dec_and_test(&req->refs)) | |
2163 | io_free_req(req); | |
2b188cc1 JA |
2164 | } |
2165 | ||
216578e5 PB |
2166 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2167 | { | |
2168 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2169 | ||
2170 | io_free_req(req); | |
2171 | } | |
2172 | ||
2173 | static void io_free_req_deferred(struct io_kiocb *req) | |
2174 | { | |
2175 | int ret; | |
2176 | ||
7cbf1722 | 2177 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2178 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2179 | if (unlikely(ret)) |
2180 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2181 | } |
2182 | ||
2183 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2184 | { | |
2185 | if (refcount_sub_and_test(refs, &req->refs)) | |
2186 | io_free_req_deferred(req); | |
2187 | } | |
2188 | ||
978db57e JA |
2189 | static void io_double_put_req(struct io_kiocb *req) |
2190 | { | |
2191 | /* drop both submit and complete references */ | |
2192 | if (refcount_sub_and_test(2, &req->refs)) | |
2193 | io_free_req(req); | |
2194 | } | |
2195 | ||
6c503150 | 2196 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2197 | { |
2198 | /* See comment at the top of this file */ | |
2199 | smp_rmb(); | |
e23de15f | 2200 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2201 | } |
2202 | ||
fb5ccc98 PB |
2203 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2204 | { | |
2205 | struct io_rings *rings = ctx->rings; | |
2206 | ||
2207 | /* make sure SQ entry isn't read before tail */ | |
2208 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2209 | } | |
2210 | ||
8ff069bf | 2211 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2212 | { |
8ff069bf | 2213 | unsigned int cflags; |
e94f141b | 2214 | |
bcda7baa JA |
2215 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2216 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2217 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2218 | kfree(kbuf); |
2219 | return cflags; | |
e94f141b JA |
2220 | } |
2221 | ||
8ff069bf | 2222 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2223 | { |
4d954c25 | 2224 | struct io_buffer *kbuf; |
bcda7baa | 2225 | |
4d954c25 | 2226 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2227 | return io_put_kbuf(req, kbuf); |
2228 | } | |
2229 | ||
4c6e277c JA |
2230 | static inline bool io_run_task_work(void) |
2231 | { | |
6200b0ae JA |
2232 | /* |
2233 | * Not safe to run on exiting task, and the task_work handling will | |
2234 | * not add work to such a task. | |
2235 | */ | |
2236 | if (unlikely(current->flags & PF_EXITING)) | |
2237 | return false; | |
4c6e277c JA |
2238 | if (current->task_works) { |
2239 | __set_current_state(TASK_RUNNING); | |
2240 | task_work_run(); | |
2241 | return true; | |
2242 | } | |
2243 | ||
2244 | return false; | |
bcda7baa JA |
2245 | } |
2246 | ||
def596e9 JA |
2247 | /* |
2248 | * Find and free completed poll iocbs | |
2249 | */ | |
2250 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2251 | struct list_head *done) | |
2252 | { | |
8237e045 | 2253 | struct req_batch rb; |
def596e9 | 2254 | struct io_kiocb *req; |
bbde017a XW |
2255 | |
2256 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2257 | smp_rmb(); | |
def596e9 | 2258 | |
5af1d13e | 2259 | io_init_req_batch(&rb); |
def596e9 | 2260 | while (!list_empty(done)) { |
bcda7baa JA |
2261 | int cflags = 0; |
2262 | ||
d21ffe7e | 2263 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2264 | list_del(&req->inflight_entry); |
2265 | ||
bbde017a XW |
2266 | if (READ_ONCE(req->result) == -EAGAIN) { |
2267 | req->iopoll_completed = 0; | |
23faba36 | 2268 | if (io_rw_reissue(req)) |
f161340d | 2269 | continue; |
bbde017a | 2270 | } |
def596e9 | 2271 | |
bcda7baa | 2272 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2273 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2274 | |
2275 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2276 | (*nr_events)++; |
2277 | ||
c3524383 | 2278 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2279 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2280 | } |
def596e9 | 2281 | |
09bb8394 | 2282 | io_commit_cqring(ctx); |
80c18e4a | 2283 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2284 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2285 | } |
2286 | ||
def596e9 JA |
2287 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2288 | long min) | |
2289 | { | |
2290 | struct io_kiocb *req, *tmp; | |
2291 | LIST_HEAD(done); | |
2292 | bool spin; | |
2293 | int ret; | |
2294 | ||
2295 | /* | |
2296 | * Only spin for completions if we don't have multiple devices hanging | |
2297 | * off our complete list, and we're under the requested amount. | |
2298 | */ | |
2299 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2300 | ||
2301 | ret = 0; | |
d21ffe7e | 2302 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2303 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2304 | |
2305 | /* | |
581f9810 BM |
2306 | * Move completed and retryable entries to our local lists. |
2307 | * If we find a request that requires polling, break out | |
2308 | * and complete those lists first, if we have entries there. | |
def596e9 | 2309 | */ |
65a6543d | 2310 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2311 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2312 | continue; |
2313 | } | |
2314 | if (!list_empty(&done)) | |
2315 | break; | |
2316 | ||
2317 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2318 | if (ret < 0) | |
2319 | break; | |
2320 | ||
3aadc23e PB |
2321 | /* iopoll may have completed current req */ |
2322 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2323 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2324 | |
def596e9 JA |
2325 | if (ret && spin) |
2326 | spin = false; | |
2327 | ret = 0; | |
2328 | } | |
2329 | ||
2330 | if (!list_empty(&done)) | |
2331 | io_iopoll_complete(ctx, nr_events, &done); | |
2332 | ||
2333 | return ret; | |
2334 | } | |
2335 | ||
2336 | /* | |
d195a66e | 2337 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2338 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2339 | * as a non-spinning completion check. | |
2340 | */ | |
2341 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2342 | long min) | |
2343 | { | |
540e32a0 | 2344 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2345 | int ret; |
2346 | ||
2347 | ret = io_do_iopoll(ctx, nr_events, min); | |
2348 | if (ret < 0) | |
2349 | return ret; | |
eba0a4dd | 2350 | if (*nr_events >= min) |
def596e9 JA |
2351 | return 0; |
2352 | } | |
2353 | ||
2354 | return 1; | |
2355 | } | |
2356 | ||
2357 | /* | |
2358 | * We can't just wait for polled events to come to us, we have to actively | |
2359 | * find and complete them. | |
2360 | */ | |
b2edc0a7 | 2361 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2362 | { |
2363 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2364 | return; | |
2365 | ||
2366 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2367 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2368 | unsigned int nr_events = 0; |
2369 | ||
b2edc0a7 | 2370 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2371 | |
b2edc0a7 PB |
2372 | /* let it sleep and repeat later if can't complete a request */ |
2373 | if (nr_events == 0) | |
2374 | break; | |
08f5439f JA |
2375 | /* |
2376 | * Ensure we allow local-to-the-cpu processing to take place, | |
2377 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2378 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2379 | */ |
3fcee5a6 PB |
2380 | if (need_resched()) { |
2381 | mutex_unlock(&ctx->uring_lock); | |
2382 | cond_resched(); | |
2383 | mutex_lock(&ctx->uring_lock); | |
2384 | } | |
def596e9 JA |
2385 | } |
2386 | mutex_unlock(&ctx->uring_lock); | |
2387 | } | |
2388 | ||
7668b92a | 2389 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2390 | { |
7668b92a | 2391 | unsigned int nr_events = 0; |
2b2ed975 | 2392 | int iters = 0, ret = 0; |
500f9fba | 2393 | |
c7849be9 XW |
2394 | /* |
2395 | * We disallow the app entering submit/complete with polling, but we | |
2396 | * still need to lock the ring to prevent racing with polled issue | |
2397 | * that got punted to a workqueue. | |
2398 | */ | |
2399 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2400 | do { |
a3a0e43f JA |
2401 | /* |
2402 | * Don't enter poll loop if we already have events pending. | |
2403 | * If we do, we can potentially be spinning for commands that | |
2404 | * already triggered a CQE (eg in error). | |
2405 | */ | |
6c503150 PB |
2406 | if (test_bit(0, &ctx->cq_check_overflow)) |
2407 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2408 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2409 | break; |
2410 | ||
500f9fba JA |
2411 | /* |
2412 | * If a submit got punted to a workqueue, we can have the | |
2413 | * application entering polling for a command before it gets | |
2414 | * issued. That app will hold the uring_lock for the duration | |
2415 | * of the poll right here, so we need to take a breather every | |
2416 | * now and then to ensure that the issue has a chance to add | |
2417 | * the poll to the issued list. Otherwise we can spin here | |
2418 | * forever, while the workqueue is stuck trying to acquire the | |
2419 | * very same mutex. | |
2420 | */ | |
2421 | if (!(++iters & 7)) { | |
2422 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2423 | io_run_task_work(); |
500f9fba JA |
2424 | mutex_lock(&ctx->uring_lock); |
2425 | } | |
2426 | ||
7668b92a | 2427 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2428 | if (ret <= 0) |
2429 | break; | |
2430 | ret = 0; | |
7668b92a | 2431 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2432 | |
500f9fba | 2433 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2434 | return ret; |
2435 | } | |
2436 | ||
491381ce | 2437 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2438 | { |
491381ce JA |
2439 | /* |
2440 | * Tell lockdep we inherited freeze protection from submission | |
2441 | * thread. | |
2442 | */ | |
2443 | if (req->flags & REQ_F_ISREG) { | |
2444 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2445 | |
491381ce | 2446 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2447 | } |
491381ce | 2448 | file_end_write(req->file); |
2b188cc1 JA |
2449 | } |
2450 | ||
b63534c4 | 2451 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2452 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2453 | { |
2454 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2455 | int rw, ret; |
b63534c4 | 2456 | struct iov_iter iter; |
b63534c4 | 2457 | |
dc2a6e9a PB |
2458 | /* already prepared */ |
2459 | if (req->async_data) | |
2460 | return true; | |
b63534c4 JA |
2461 | |
2462 | switch (req->opcode) { | |
2463 | case IORING_OP_READV: | |
2464 | case IORING_OP_READ_FIXED: | |
2465 | case IORING_OP_READ: | |
2466 | rw = READ; | |
2467 | break; | |
2468 | case IORING_OP_WRITEV: | |
2469 | case IORING_OP_WRITE_FIXED: | |
2470 | case IORING_OP_WRITE: | |
2471 | rw = WRITE; | |
2472 | break; | |
2473 | default: | |
2474 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2475 | req->opcode); | |
dc2a6e9a | 2476 | return false; |
b63534c4 JA |
2477 | } |
2478 | ||
dc2a6e9a PB |
2479 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2480 | if (ret < 0) | |
2481 | return false; | |
6bf985dc | 2482 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2483 | } |
b63534c4 JA |
2484 | #endif |
2485 | ||
23faba36 | 2486 | static bool io_rw_reissue(struct io_kiocb *req) |
b63534c4 JA |
2487 | { |
2488 | #ifdef CONFIG_BLOCK | |
355afaeb | 2489 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 | 2490 | |
355afaeb JA |
2491 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2492 | return false; | |
75c668cd | 2493 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
b63534c4 JA |
2494 | return false; |
2495 | ||
55e6ac1e PB |
2496 | lockdep_assert_held(&req->ctx->uring_lock); |
2497 | ||
37d1e2e3 | 2498 | if (io_resubmit_prep(req)) { |
fdee946d JA |
2499 | refcount_inc(&req->refs); |
2500 | io_queue_async_work(req); | |
b63534c4 | 2501 | return true; |
fdee946d | 2502 | } |
dc2a6e9a | 2503 | req_set_fail_links(req); |
b63534c4 JA |
2504 | #endif |
2505 | return false; | |
2506 | } | |
2507 | ||
a1d7c393 | 2508 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2509 | unsigned int issue_flags) |
a1d7c393 | 2510 | { |
2f8e45f1 PB |
2511 | int cflags = 0; |
2512 | ||
23faba36 PB |
2513 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2514 | return; | |
2f8e45f1 PB |
2515 | if (res != req->result) |
2516 | req_set_fail_links(req); | |
23faba36 | 2517 | |
2f8e45f1 PB |
2518 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2519 | kiocb_end_write(req); | |
2520 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2521 | cflags = io_put_rw_kbuf(req); | |
2522 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2523 | } |
2524 | ||
2525 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2526 | { | |
9adbd45d | 2527 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2528 | |
889fca73 | 2529 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2530 | } |
2531 | ||
def596e9 JA |
2532 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2533 | { | |
9adbd45d | 2534 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2535 | |
491381ce JA |
2536 | if (kiocb->ki_flags & IOCB_WRITE) |
2537 | kiocb_end_write(req); | |
def596e9 | 2538 | |
2d7d6792 | 2539 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2540 | req_set_fail_links(req); |
bbde017a XW |
2541 | |
2542 | WRITE_ONCE(req->result, res); | |
2543 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2544 | smp_wmb(); |
2545 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2546 | } |
2547 | ||
2548 | /* | |
2549 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2550 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2551 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2552 | * accessing the kiocb cookie. | |
2553 | */ | |
2e9dbe90 | 2554 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2555 | { |
2556 | struct io_ring_ctx *ctx = req->ctx; | |
2557 | ||
2558 | /* | |
2559 | * Track whether we have multiple files in our lists. This will impact | |
2560 | * how we do polling eventually, not spinning if we're on potentially | |
2561 | * different devices. | |
2562 | */ | |
540e32a0 | 2563 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2564 | ctx->poll_multi_file = false; |
2565 | } else if (!ctx->poll_multi_file) { | |
2566 | struct io_kiocb *list_req; | |
2567 | ||
540e32a0 | 2568 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2569 | inflight_entry); |
9adbd45d | 2570 | if (list_req->file != req->file) |
def596e9 JA |
2571 | ctx->poll_multi_file = true; |
2572 | } | |
2573 | ||
2574 | /* | |
2575 | * For fast devices, IO may have already completed. If it has, add | |
2576 | * it to the front so we find it first. | |
2577 | */ | |
65a6543d | 2578 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2579 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2580 | else |
d21ffe7e | 2581 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2582 | |
2e9dbe90 XW |
2583 | /* |
2584 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2585 | * task context or in io worker task context. If current task context is | |
2586 | * sq thread, we don't need to check whether should wake up sq thread. | |
2587 | */ | |
2588 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2589 | wq_has_sleeper(&ctx->sq_data->wait)) |
2590 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2591 | } |
2592 | ||
9f13c35b PB |
2593 | static inline void io_state_file_put(struct io_submit_state *state) |
2594 | { | |
02b23a9a PB |
2595 | if (state->file_refs) { |
2596 | fput_many(state->file, state->file_refs); | |
2597 | state->file_refs = 0; | |
2598 | } | |
9a56a232 JA |
2599 | } |
2600 | ||
2601 | /* | |
2602 | * Get as many references to a file as we have IOs left in this submission, | |
2603 | * assuming most submissions are for one file, or at least that each file | |
2604 | * has more than one submission. | |
2605 | */ | |
8da11c19 | 2606 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2607 | { |
2608 | if (!state) | |
2609 | return fget(fd); | |
2610 | ||
6e1271e6 | 2611 | if (state->file_refs) { |
9a56a232 | 2612 | if (state->fd == fd) { |
6e1271e6 | 2613 | state->file_refs--; |
9a56a232 JA |
2614 | return state->file; |
2615 | } | |
02b23a9a | 2616 | io_state_file_put(state); |
9a56a232 JA |
2617 | } |
2618 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2619 | if (unlikely(!state->file)) |
9a56a232 JA |
2620 | return NULL; |
2621 | ||
2622 | state->fd = fd; | |
6e1271e6 | 2623 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2624 | return state->file; |
2625 | } | |
2626 | ||
4503b767 JA |
2627 | static bool io_bdev_nowait(struct block_device *bdev) |
2628 | { | |
9ba0d0c8 | 2629 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2630 | } |
2631 | ||
2b188cc1 JA |
2632 | /* |
2633 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2634 | * any file. For now, just ensure that anything potentially problematic is done | |
2635 | * inline. | |
2636 | */ | |
af197f50 | 2637 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2638 | { |
2639 | umode_t mode = file_inode(file)->i_mode; | |
2640 | ||
4503b767 | 2641 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2642 | if (IS_ENABLED(CONFIG_BLOCK) && |
2643 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2644 | return true; |
2645 | return false; | |
2646 | } | |
2647 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2648 | return true; |
4503b767 | 2649 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2650 | if (IS_ENABLED(CONFIG_BLOCK) && |
2651 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2652 | file->f_op != &io_uring_fops) |
2653 | return true; | |
2654 | return false; | |
2655 | } | |
2b188cc1 | 2656 | |
c5b85625 JA |
2657 | /* any ->read/write should understand O_NONBLOCK */ |
2658 | if (file->f_flags & O_NONBLOCK) | |
2659 | return true; | |
2660 | ||
af197f50 JA |
2661 | if (!(file->f_mode & FMODE_NOWAIT)) |
2662 | return false; | |
2663 | ||
2664 | if (rw == READ) | |
2665 | return file->f_op->read_iter != NULL; | |
2666 | ||
2667 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2668 | } |
2669 | ||
a88fc400 | 2670 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2671 | { |
def596e9 | 2672 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2673 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2674 | struct file *file = req->file; |
09bb8394 JA |
2675 | unsigned ioprio; |
2676 | int ret; | |
2b188cc1 | 2677 | |
75c668cd | 2678 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2679 | req->flags |= REQ_F_ISREG; |
2680 | ||
2b188cc1 | 2681 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2682 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2683 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2684 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2685 | } |
2b188cc1 | 2686 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2687 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2688 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2689 | if (unlikely(ret)) | |
2690 | return ret; | |
2b188cc1 | 2691 | |
75c668cd PB |
2692 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2693 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2694 | req->flags |= REQ_F_NOWAIT; | |
2695 | ||
2b188cc1 JA |
2696 | ioprio = READ_ONCE(sqe->ioprio); |
2697 | if (ioprio) { | |
2698 | ret = ioprio_check_cap(ioprio); | |
2699 | if (ret) | |
09bb8394 | 2700 | return ret; |
2b188cc1 JA |
2701 | |
2702 | kiocb->ki_ioprio = ioprio; | |
2703 | } else | |
2704 | kiocb->ki_ioprio = get_current_ioprio(); | |
2705 | ||
def596e9 | 2706 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2707 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2708 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2709 | return -EOPNOTSUPP; |
2b188cc1 | 2710 | |
def596e9 JA |
2711 | kiocb->ki_flags |= IOCB_HIPRI; |
2712 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2713 | req->iopoll_completed = 0; |
def596e9 | 2714 | } else { |
09bb8394 JA |
2715 | if (kiocb->ki_flags & IOCB_HIPRI) |
2716 | return -EINVAL; | |
def596e9 JA |
2717 | kiocb->ki_complete = io_complete_rw; |
2718 | } | |
9adbd45d | 2719 | |
3529d8c2 JA |
2720 | req->rw.addr = READ_ONCE(sqe->addr); |
2721 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2722 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2723 | return 0; |
2b188cc1 JA |
2724 | } |
2725 | ||
2726 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2727 | { | |
2728 | switch (ret) { | |
2729 | case -EIOCBQUEUED: | |
2730 | break; | |
2731 | case -ERESTARTSYS: | |
2732 | case -ERESTARTNOINTR: | |
2733 | case -ERESTARTNOHAND: | |
2734 | case -ERESTART_RESTARTBLOCK: | |
2735 | /* | |
2736 | * We can't just restart the syscall, since previously | |
2737 | * submitted sqes may already be in progress. Just fail this | |
2738 | * IO with EINTR. | |
2739 | */ | |
2740 | ret = -EINTR; | |
df561f66 | 2741 | fallthrough; |
2b188cc1 JA |
2742 | default: |
2743 | kiocb->ki_complete(kiocb, ret, 0); | |
2744 | } | |
2745 | } | |
2746 | ||
a1d7c393 | 2747 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2748 | unsigned int issue_flags) |
ba816ad6 | 2749 | { |
ba04291e | 2750 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2751 | struct io_async_rw *io = req->async_data; |
ba04291e | 2752 | |
227c0c96 | 2753 | /* add previously done IO, if any */ |
e8c2bc1f | 2754 | if (io && io->bytes_done > 0) { |
227c0c96 | 2755 | if (ret < 0) |
e8c2bc1f | 2756 | ret = io->bytes_done; |
227c0c96 | 2757 | else |
e8c2bc1f | 2758 | ret += io->bytes_done; |
227c0c96 JA |
2759 | } |
2760 | ||
ba04291e JA |
2761 | if (req->flags & REQ_F_CUR_POS) |
2762 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2763 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2764 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2765 | else |
2766 | io_rw_done(kiocb, ret); | |
2767 | } | |
2768 | ||
847595de | 2769 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2770 | { |
9adbd45d JA |
2771 | struct io_ring_ctx *ctx = req->ctx; |
2772 | size_t len = req->rw.len; | |
edafccee | 2773 | struct io_mapped_ubuf *imu; |
4be1c615 | 2774 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2775 | size_t offset; |
2776 | u64 buf_addr; | |
2777 | ||
edafccee JA |
2778 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2779 | return -EFAULT; | |
edafccee JA |
2780 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2781 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2782 | buf_addr = req->rw.addr; |
edafccee JA |
2783 | |
2784 | /* overflow */ | |
2785 | if (buf_addr + len < buf_addr) | |
2786 | return -EFAULT; | |
2787 | /* not inside the mapped region */ | |
2788 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2789 | return -EFAULT; | |
2790 | ||
2791 | /* | |
2792 | * May not be a start of buffer, set size appropriately | |
2793 | * and advance us to the beginning. | |
2794 | */ | |
2795 | offset = buf_addr - imu->ubuf; | |
2796 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2797 | |
2798 | if (offset) { | |
2799 | /* | |
2800 | * Don't use iov_iter_advance() here, as it's really slow for | |
2801 | * using the latter parts of a big fixed buffer - it iterates | |
2802 | * over each segment manually. We can cheat a bit here, because | |
2803 | * we know that: | |
2804 | * | |
2805 | * 1) it's a BVEC iter, we set it up | |
2806 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2807 | * first and last bvec | |
2808 | * | |
2809 | * So just find our index, and adjust the iterator afterwards. | |
2810 | * If the offset is within the first bvec (or the whole first | |
2811 | * bvec, just use iov_iter_advance(). This makes it easier | |
2812 | * since we can just skip the first segment, which may not | |
2813 | * be PAGE_SIZE aligned. | |
2814 | */ | |
2815 | const struct bio_vec *bvec = imu->bvec; | |
2816 | ||
2817 | if (offset <= bvec->bv_len) { | |
2818 | iov_iter_advance(iter, offset); | |
2819 | } else { | |
2820 | unsigned long seg_skip; | |
2821 | ||
2822 | /* skip first vec */ | |
2823 | offset -= bvec->bv_len; | |
2824 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2825 | ||
2826 | iter->bvec = bvec + seg_skip; | |
2827 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2828 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2829 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2830 | } |
2831 | } | |
2832 | ||
847595de | 2833 | return 0; |
edafccee JA |
2834 | } |
2835 | ||
bcda7baa JA |
2836 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2837 | { | |
2838 | if (needs_lock) | |
2839 | mutex_unlock(&ctx->uring_lock); | |
2840 | } | |
2841 | ||
2842 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2843 | { | |
2844 | /* | |
2845 | * "Normal" inline submissions always hold the uring_lock, since we | |
2846 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2847 | * The only exception is when we've detached the request and issue it | |
2848 | * from an async worker thread, grab the lock for that case. | |
2849 | */ | |
2850 | if (needs_lock) | |
2851 | mutex_lock(&ctx->uring_lock); | |
2852 | } | |
2853 | ||
2854 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2855 | int bgid, struct io_buffer *kbuf, | |
2856 | bool needs_lock) | |
2857 | { | |
2858 | struct io_buffer *head; | |
2859 | ||
2860 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2861 | return kbuf; | |
2862 | ||
2863 | io_ring_submit_lock(req->ctx, needs_lock); | |
2864 | ||
2865 | lockdep_assert_held(&req->ctx->uring_lock); | |
2866 | ||
2867 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2868 | if (head) { | |
2869 | if (!list_empty(&head->list)) { | |
2870 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2871 | list); | |
2872 | list_del(&kbuf->list); | |
2873 | } else { | |
2874 | kbuf = head; | |
2875 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2876 | } | |
2877 | if (*len > kbuf->len) | |
2878 | *len = kbuf->len; | |
2879 | } else { | |
2880 | kbuf = ERR_PTR(-ENOBUFS); | |
2881 | } | |
2882 | ||
2883 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2884 | ||
2885 | return kbuf; | |
2886 | } | |
2887 | ||
4d954c25 JA |
2888 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2889 | bool needs_lock) | |
2890 | { | |
2891 | struct io_buffer *kbuf; | |
4f4eeba8 | 2892 | u16 bgid; |
4d954c25 JA |
2893 | |
2894 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2895 | bgid = req->buf_index; |
4d954c25 JA |
2896 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2897 | if (IS_ERR(kbuf)) | |
2898 | return kbuf; | |
2899 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2900 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2901 | return u64_to_user_ptr(kbuf->addr); | |
2902 | } | |
2903 | ||
2904 | #ifdef CONFIG_COMPAT | |
2905 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2906 | bool needs_lock) | |
2907 | { | |
2908 | struct compat_iovec __user *uiov; | |
2909 | compat_ssize_t clen; | |
2910 | void __user *buf; | |
2911 | ssize_t len; | |
2912 | ||
2913 | uiov = u64_to_user_ptr(req->rw.addr); | |
2914 | if (!access_ok(uiov, sizeof(*uiov))) | |
2915 | return -EFAULT; | |
2916 | if (__get_user(clen, &uiov->iov_len)) | |
2917 | return -EFAULT; | |
2918 | if (clen < 0) | |
2919 | return -EINVAL; | |
2920 | ||
2921 | len = clen; | |
2922 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2923 | if (IS_ERR(buf)) | |
2924 | return PTR_ERR(buf); | |
2925 | iov[0].iov_base = buf; | |
2926 | iov[0].iov_len = (compat_size_t) len; | |
2927 | return 0; | |
2928 | } | |
2929 | #endif | |
2930 | ||
2931 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2932 | bool needs_lock) | |
2933 | { | |
2934 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2935 | void __user *buf; | |
2936 | ssize_t len; | |
2937 | ||
2938 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2939 | return -EFAULT; | |
2940 | ||
2941 | len = iov[0].iov_len; | |
2942 | if (len < 0) | |
2943 | return -EINVAL; | |
2944 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2945 | if (IS_ERR(buf)) | |
2946 | return PTR_ERR(buf); | |
2947 | iov[0].iov_base = buf; | |
2948 | iov[0].iov_len = len; | |
2949 | return 0; | |
2950 | } | |
2951 | ||
2952 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2953 | bool needs_lock) | |
2954 | { | |
dddb3e26 JA |
2955 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2956 | struct io_buffer *kbuf; | |
2957 | ||
2958 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2959 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2960 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2961 | return 0; |
dddb3e26 | 2962 | } |
dd201662 | 2963 | if (req->rw.len != 1) |
4d954c25 JA |
2964 | return -EINVAL; |
2965 | ||
2966 | #ifdef CONFIG_COMPAT | |
2967 | if (req->ctx->compat) | |
2968 | return io_compat_import(req, iov, needs_lock); | |
2969 | #endif | |
2970 | ||
2971 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2972 | } | |
2973 | ||
847595de PB |
2974 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2975 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2976 | { |
9adbd45d JA |
2977 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2978 | size_t sqe_len = req->rw.len; | |
847595de | 2979 | u8 opcode = req->opcode; |
4d954c25 | 2980 | ssize_t ret; |
edafccee | 2981 | |
7d009165 | 2982 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2983 | *iovec = NULL; |
9adbd45d | 2984 | return io_import_fixed(req, rw, iter); |
edafccee | 2985 | } |
2b188cc1 | 2986 | |
bcda7baa | 2987 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2988 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2989 | return -EINVAL; |
2990 | ||
3a6820f2 | 2991 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2992 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2993 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2994 | if (IS_ERR(buf)) |
4d954c25 | 2995 | return PTR_ERR(buf); |
3f9d6441 | 2996 | req->rw.len = sqe_len; |
bcda7baa JA |
2997 | } |
2998 | ||
3a6820f2 JA |
2999 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3000 | *iovec = NULL; | |
10fc72e4 | 3001 | return ret; |
3a6820f2 JA |
3002 | } |
3003 | ||
4d954c25 JA |
3004 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3005 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3006 | if (!ret) |
3007 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3008 | *iovec = NULL; |
3009 | return ret; | |
3010 | } | |
3011 | ||
89cd35c5 CH |
3012 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3013 | req->ctx->compat); | |
2b188cc1 JA |
3014 | } |
3015 | ||
0fef9483 JA |
3016 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3017 | { | |
5b09e37e | 3018 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3019 | } |
3020 | ||
31b51510 | 3021 | /* |
32960613 JA |
3022 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3023 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3024 | */ |
4017eb91 | 3025 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3026 | { |
4017eb91 JA |
3027 | struct kiocb *kiocb = &req->rw.kiocb; |
3028 | struct file *file = req->file; | |
32960613 JA |
3029 | ssize_t ret = 0; |
3030 | ||
3031 | /* | |
3032 | * Don't support polled IO through this interface, and we can't | |
3033 | * support non-blocking either. For the latter, this just causes | |
3034 | * the kiocb to be handled from an async context. | |
3035 | */ | |
3036 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3037 | return -EOPNOTSUPP; | |
3038 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3039 | return -EAGAIN; | |
3040 | ||
3041 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3042 | struct iovec iovec; |
32960613 JA |
3043 | ssize_t nr; |
3044 | ||
311ae9e1 PB |
3045 | if (!iov_iter_is_bvec(iter)) { |
3046 | iovec = iov_iter_iovec(iter); | |
3047 | } else { | |
4017eb91 JA |
3048 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3049 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3050 | } |
3051 | ||
32960613 JA |
3052 | if (rw == READ) { |
3053 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3054 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3055 | } else { |
3056 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3057 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3058 | } |
3059 | ||
3060 | if (nr < 0) { | |
3061 | if (!ret) | |
3062 | ret = nr; | |
3063 | break; | |
3064 | } | |
3065 | ret += nr; | |
3066 | if (nr != iovec.iov_len) | |
3067 | break; | |
4017eb91 JA |
3068 | req->rw.len -= nr; |
3069 | req->rw.addr += nr; | |
32960613 JA |
3070 | iov_iter_advance(iter, nr); |
3071 | } | |
3072 | ||
3073 | return ret; | |
3074 | } | |
3075 | ||
ff6165b2 JA |
3076 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3077 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3078 | { |
e8c2bc1f | 3079 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3080 | |
ff6165b2 | 3081 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3082 | rw->free_iovec = iovec; |
227c0c96 | 3083 | rw->bytes_done = 0; |
ff6165b2 | 3084 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3085 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3086 | return; |
b64e3444 | 3087 | if (!iovec) { |
ff6165b2 JA |
3088 | unsigned iov_off = 0; |
3089 | ||
3090 | rw->iter.iov = rw->fast_iov; | |
3091 | if (iter->iov != fast_iov) { | |
3092 | iov_off = iter->iov - fast_iov; | |
3093 | rw->iter.iov += iov_off; | |
3094 | } | |
3095 | if (rw->fast_iov != fast_iov) | |
3096 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3097 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3098 | } else { |
3099 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3100 | } |
3101 | } | |
3102 | ||
e8c2bc1f | 3103 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3104 | { |
e8c2bc1f JA |
3105 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3106 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3107 | return req->async_data == NULL; | |
3d9932a8 XW |
3108 | } |
3109 | ||
e8c2bc1f | 3110 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3111 | { |
e8c2bc1f | 3112 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3113 | return 0; |
3d9932a8 | 3114 | |
e8c2bc1f | 3115 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3116 | } |
3117 | ||
ff6165b2 JA |
3118 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3119 | const struct iovec *fast_iov, | |
227c0c96 | 3120 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3121 | { |
e8c2bc1f | 3122 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3123 | return 0; |
e8c2bc1f | 3124 | if (!req->async_data) { |
6bf985dc PB |
3125 | if (__io_alloc_async_data(req)) { |
3126 | kfree(iovec); | |
5d204bcf | 3127 | return -ENOMEM; |
6bf985dc | 3128 | } |
b7bb4f7d | 3129 | |
ff6165b2 | 3130 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3131 | } |
b7bb4f7d | 3132 | return 0; |
f67676d1 JA |
3133 | } |
3134 | ||
73debe68 | 3135 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3136 | { |
e8c2bc1f | 3137 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3138 | struct iovec *iov = iorw->fast_iov; |
847595de | 3139 | int ret; |
c3e330a4 | 3140 | |
2846c481 | 3141 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3142 | if (unlikely(ret < 0)) |
3143 | return ret; | |
3144 | ||
ab0b196c PB |
3145 | iorw->bytes_done = 0; |
3146 | iorw->free_iovec = iov; | |
3147 | if (iov) | |
3148 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3149 | return 0; |
3150 | } | |
3151 | ||
73debe68 | 3152 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3153 | { |
3529d8c2 JA |
3154 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3155 | return -EBADF; | |
93642ef8 | 3156 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3157 | } |
3158 | ||
c1dd91d1 JA |
3159 | /* |
3160 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3161 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3162 | * This gets called when the page is unlocked, and we generally expect that to | |
3163 | * happen when the page IO is completed and the page is now uptodate. This will | |
3164 | * queue a task_work based retry of the operation, attempting to copy the data | |
3165 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3166 | * do a thread based blocking retry of the operation. That's the unexpected | |
3167 | * slow path. | |
3168 | */ | |
bcf5a063 JA |
3169 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3170 | int sync, void *arg) | |
3171 | { | |
3172 | struct wait_page_queue *wpq; | |
3173 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3174 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3175 | |
3176 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3177 | ||
cdc8fcb4 LT |
3178 | if (!wake_page_match(wpq, key)) |
3179 | return 0; | |
3180 | ||
c8d317aa | 3181 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3182 | list_del_init(&wait->entry); |
3183 | ||
bcf5a063 JA |
3184 | /* submit ref gets dropped, acquire a new one */ |
3185 | refcount_inc(&req->refs); | |
921b9054 | 3186 | io_req_task_queue(req); |
bcf5a063 JA |
3187 | return 1; |
3188 | } | |
3189 | ||
c1dd91d1 JA |
3190 | /* |
3191 | * This controls whether a given IO request should be armed for async page | |
3192 | * based retry. If we return false here, the request is handed to the async | |
3193 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3194 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3195 | * will either succeed because the page is now uptodate and unlocked, or it | |
3196 | * will register a callback when the page is unlocked at IO completion. Through | |
3197 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3198 | * That retry will attempt the buffered read again. The retry will generally | |
3199 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3200 | * async worker threads for a blocking retry. | |
3201 | */ | |
227c0c96 | 3202 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3203 | { |
e8c2bc1f JA |
3204 | struct io_async_rw *rw = req->async_data; |
3205 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3206 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3207 | |
bcf5a063 JA |
3208 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3209 | if (req->flags & REQ_F_NOWAIT) | |
3210 | return false; | |
f67676d1 | 3211 | |
227c0c96 | 3212 | /* Only for buffered IO */ |
3b2a4439 | 3213 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3214 | return false; |
3b2a4439 | 3215 | |
bcf5a063 JA |
3216 | /* |
3217 | * just use poll if we can, and don't attempt if the fs doesn't | |
3218 | * support callback based unlocks | |
3219 | */ | |
3220 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3221 | return false; | |
f67676d1 | 3222 | |
3b2a4439 JA |
3223 | wait->wait.func = io_async_buf_func; |
3224 | wait->wait.private = req; | |
3225 | wait->wait.flags = 0; | |
3226 | INIT_LIST_HEAD(&wait->wait.entry); | |
3227 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3228 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3229 | kiocb->ki_waitq = wait; |
3b2a4439 | 3230 | return true; |
bcf5a063 JA |
3231 | } |
3232 | ||
3233 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3234 | { | |
3235 | if (req->file->f_op->read_iter) | |
3236 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3237 | else if (req->file->f_op->read) |
4017eb91 | 3238 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3239 | else |
3240 | return -EINVAL; | |
f67676d1 JA |
3241 | } |
3242 | ||
889fca73 | 3243 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3244 | { |
3245 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3246 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3247 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3248 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3249 | ssize_t io_size, ret, ret2; |
45d189c6 | 3250 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3251 | |
2846c481 | 3252 | if (rw) { |
e8c2bc1f | 3253 | iter = &rw->iter; |
2846c481 PB |
3254 | iovec = NULL; |
3255 | } else { | |
3256 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3257 | if (ret < 0) | |
3258 | return ret; | |
3259 | } | |
632546c4 | 3260 | io_size = iov_iter_count(iter); |
fa15bafb | 3261 | req->result = io_size; |
2b188cc1 | 3262 | |
fd6c2e4c JA |
3263 | /* Ensure we clear previously set non-block flag */ |
3264 | if (!force_nonblock) | |
29de5f6a | 3265 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3266 | else |
3267 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3268 | ||
24c74678 | 3269 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3270 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3271 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3272 | return ret ?: -EAGAIN; |
6713e7a6 | 3273 | } |
9e645e11 | 3274 | |
632546c4 | 3275 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3276 | if (unlikely(ret)) { |
3277 | kfree(iovec); | |
3278 | return ret; | |
3279 | } | |
2b188cc1 | 3280 | |
227c0c96 | 3281 | ret = io_iter_do_read(req, iter); |
32960613 | 3282 | |
57cd657b | 3283 | if (ret == -EIOCBQUEUED) { |
fe1cdd55 | 3284 | goto out_free; |
227c0c96 | 3285 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3286 | /* IOPOLL retry should happen for io-wq threads */ |
3287 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3288 | goto done; |
75c668cd PB |
3289 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3290 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3291 | goto done; |
84216315 | 3292 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3293 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3294 | ret = 0; |
7335e3bf | 3295 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3296 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3297 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3298 | goto done; |
227c0c96 JA |
3299 | } |
3300 | ||
227c0c96 | 3301 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3302 | if (ret2) |
3303 | return ret2; | |
3304 | ||
fe1cdd55 | 3305 | iovec = NULL; |
e8c2bc1f | 3306 | rw = req->async_data; |
227c0c96 | 3307 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3308 | iter = &rw->iter; |
227c0c96 | 3309 | |
b23df91b PB |
3310 | do { |
3311 | io_size -= ret; | |
3312 | rw->bytes_done += ret; | |
3313 | /* if we can retry, do so with the callbacks armed */ | |
3314 | if (!io_rw_should_retry(req)) { | |
3315 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3316 | return -EAGAIN; | |
3317 | } | |
3318 | ||
3319 | /* | |
3320 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3321 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3322 | * desired page gets unlocked. We can also get a partial read | |
3323 | * here, and if we do, then just retry at the new offset. | |
3324 | */ | |
3325 | ret = io_iter_do_read(req, iter); | |
3326 | if (ret == -EIOCBQUEUED) | |
3327 | return 0; | |
227c0c96 | 3328 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3329 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3330 | done: |
889fca73 | 3331 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3332 | out_free: |
3333 | /* it's faster to check here then delegate to kfree */ | |
3334 | if (iovec) | |
3335 | kfree(iovec); | |
5ea5dd45 | 3336 | return 0; |
2b188cc1 JA |
3337 | } |
3338 | ||
73debe68 | 3339 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3340 | { |
3529d8c2 JA |
3341 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3342 | return -EBADF; | |
93642ef8 | 3343 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3344 | } |
3345 | ||
889fca73 | 3346 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3347 | { |
3348 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3349 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3350 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3351 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3352 | ssize_t ret, ret2, io_size; |
45d189c6 | 3353 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3354 | |
2846c481 | 3355 | if (rw) { |
e8c2bc1f | 3356 | iter = &rw->iter; |
2846c481 PB |
3357 | iovec = NULL; |
3358 | } else { | |
3359 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3360 | if (ret < 0) | |
3361 | return ret; | |
3362 | } | |
632546c4 | 3363 | io_size = iov_iter_count(iter); |
fa15bafb | 3364 | req->result = io_size; |
2b188cc1 | 3365 | |
fd6c2e4c JA |
3366 | /* Ensure we clear previously set non-block flag */ |
3367 | if (!force_nonblock) | |
a88fc400 PB |
3368 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3369 | else | |
3370 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3371 | |
24c74678 | 3372 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3373 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3374 | goto copy_iov; |
31b51510 | 3375 | |
10d59345 JA |
3376 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3377 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3378 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3379 | goto copy_iov; |
31b51510 | 3380 | |
632546c4 | 3381 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3382 | if (unlikely(ret)) |
3383 | goto out_free; | |
4ed734b0 | 3384 | |
fa15bafb PB |
3385 | /* |
3386 | * Open-code file_start_write here to grab freeze protection, | |
3387 | * which will be released by another thread in | |
3388 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3389 | * released so that it doesn't complain about the held lock when | |
3390 | * we return to userspace. | |
3391 | */ | |
3392 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3393 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3394 | __sb_writers_release(file_inode(req->file)->i_sb, |
3395 | SB_FREEZE_WRITE); | |
3396 | } | |
3397 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3398 | |
fa15bafb | 3399 | if (req->file->f_op->write_iter) |
ff6165b2 | 3400 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3401 | else if (req->file->f_op->write) |
4017eb91 | 3402 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3403 | else |
3404 | ret2 = -EINVAL; | |
4ed734b0 | 3405 | |
fa15bafb PB |
3406 | /* |
3407 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3408 | * retry them without IOCB_NOWAIT. | |
3409 | */ | |
3410 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3411 | ret2 = -EAGAIN; | |
75c668cd PB |
3412 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3413 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3414 | goto done; |
fa15bafb | 3415 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3416 | /* IOPOLL retry should happen for io-wq threads */ |
3417 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3418 | goto copy_iov; | |
355afaeb | 3419 | done: |
889fca73 | 3420 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3421 | } else { |
f67676d1 | 3422 | copy_iov: |
84216315 | 3423 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3424 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3425 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3426 | return ret ?: -EAGAIN; |
2b188cc1 | 3427 | } |
31b51510 | 3428 | out_free: |
f261c168 | 3429 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3430 | if (iovec) |
6f2cc166 | 3431 | kfree(iovec); |
2b188cc1 JA |
3432 | return ret; |
3433 | } | |
3434 | ||
80a261fd JA |
3435 | static int io_renameat_prep(struct io_kiocb *req, |
3436 | const struct io_uring_sqe *sqe) | |
3437 | { | |
3438 | struct io_rename *ren = &req->rename; | |
3439 | const char __user *oldf, *newf; | |
3440 | ||
3441 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3442 | return -EBADF; | |
3443 | ||
3444 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3445 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3446 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3447 | ren->new_dfd = READ_ONCE(sqe->len); | |
3448 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3449 | ||
3450 | ren->oldpath = getname(oldf); | |
3451 | if (IS_ERR(ren->oldpath)) | |
3452 | return PTR_ERR(ren->oldpath); | |
3453 | ||
3454 | ren->newpath = getname(newf); | |
3455 | if (IS_ERR(ren->newpath)) { | |
3456 | putname(ren->oldpath); | |
3457 | return PTR_ERR(ren->newpath); | |
3458 | } | |
3459 | ||
3460 | req->flags |= REQ_F_NEED_CLEANUP; | |
3461 | return 0; | |
3462 | } | |
3463 | ||
45d189c6 | 3464 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3465 | { |
3466 | struct io_rename *ren = &req->rename; | |
3467 | int ret; | |
3468 | ||
45d189c6 | 3469 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3470 | return -EAGAIN; |
3471 | ||
3472 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3473 | ren->newpath, ren->flags); | |
3474 | ||
3475 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3476 | if (ret < 0) | |
3477 | req_set_fail_links(req); | |
3478 | io_req_complete(req, ret); | |
3479 | return 0; | |
3480 | } | |
3481 | ||
14a1143b JA |
3482 | static int io_unlinkat_prep(struct io_kiocb *req, |
3483 | const struct io_uring_sqe *sqe) | |
3484 | { | |
3485 | struct io_unlink *un = &req->unlink; | |
3486 | const char __user *fname; | |
3487 | ||
3488 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3489 | return -EBADF; | |
3490 | ||
3491 | un->dfd = READ_ONCE(sqe->fd); | |
3492 | ||
3493 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3494 | if (un->flags & ~AT_REMOVEDIR) | |
3495 | return -EINVAL; | |
3496 | ||
3497 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3498 | un->filename = getname(fname); | |
3499 | if (IS_ERR(un->filename)) | |
3500 | return PTR_ERR(un->filename); | |
3501 | ||
3502 | req->flags |= REQ_F_NEED_CLEANUP; | |
3503 | return 0; | |
3504 | } | |
3505 | ||
45d189c6 | 3506 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3507 | { |
3508 | struct io_unlink *un = &req->unlink; | |
3509 | int ret; | |
3510 | ||
45d189c6 | 3511 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3512 | return -EAGAIN; |
3513 | ||
3514 | if (un->flags & AT_REMOVEDIR) | |
3515 | ret = do_rmdir(un->dfd, un->filename); | |
3516 | else | |
3517 | ret = do_unlinkat(un->dfd, un->filename); | |
3518 | ||
3519 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3520 | if (ret < 0) | |
3521 | req_set_fail_links(req); | |
3522 | io_req_complete(req, ret); | |
3523 | return 0; | |
3524 | } | |
3525 | ||
36f4fa68 JA |
3526 | static int io_shutdown_prep(struct io_kiocb *req, |
3527 | const struct io_uring_sqe *sqe) | |
3528 | { | |
3529 | #if defined(CONFIG_NET) | |
3530 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3531 | return -EINVAL; | |
3532 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3533 | sqe->buf_index) | |
3534 | return -EINVAL; | |
3535 | ||
3536 | req->shutdown.how = READ_ONCE(sqe->len); | |
3537 | return 0; | |
3538 | #else | |
3539 | return -EOPNOTSUPP; | |
3540 | #endif | |
3541 | } | |
3542 | ||
45d189c6 | 3543 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3544 | { |
3545 | #if defined(CONFIG_NET) | |
3546 | struct socket *sock; | |
3547 | int ret; | |
3548 | ||
45d189c6 | 3549 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3550 | return -EAGAIN; |
3551 | ||
48aba79b | 3552 | sock = sock_from_file(req->file); |
36f4fa68 | 3553 | if (unlikely(!sock)) |
48aba79b | 3554 | return -ENOTSOCK; |
36f4fa68 JA |
3555 | |
3556 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3557 | if (ret < 0) |
3558 | req_set_fail_links(req); | |
36f4fa68 JA |
3559 | io_req_complete(req, ret); |
3560 | return 0; | |
3561 | #else | |
3562 | return -EOPNOTSUPP; | |
3563 | #endif | |
3564 | } | |
3565 | ||
f2a8d5c7 PB |
3566 | static int __io_splice_prep(struct io_kiocb *req, |
3567 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3568 | { |
3569 | struct io_splice* sp = &req->splice; | |
3570 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3571 | |
3232dd02 PB |
3572 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3573 | return -EINVAL; | |
7d67af2c PB |
3574 | |
3575 | sp->file_in = NULL; | |
7d67af2c PB |
3576 | sp->len = READ_ONCE(sqe->len); |
3577 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3578 | ||
3579 | if (unlikely(sp->flags & ~valid_flags)) | |
3580 | return -EINVAL; | |
3581 | ||
8371adf5 PB |
3582 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3583 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3584 | if (!sp->file_in) | |
3585 | return -EBADF; | |
7d67af2c PB |
3586 | req->flags |= REQ_F_NEED_CLEANUP; |
3587 | ||
7cdaf587 XW |
3588 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3589 | /* | |
3590 | * Splice operation will be punted aync, and here need to | |
3591 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3592 | */ | |
3593 | io_req_init_async(req); | |
7d67af2c | 3594 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3595 | } |
7d67af2c PB |
3596 | |
3597 | return 0; | |
3598 | } | |
3599 | ||
f2a8d5c7 PB |
3600 | static int io_tee_prep(struct io_kiocb *req, |
3601 | const struct io_uring_sqe *sqe) | |
3602 | { | |
3603 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3604 | return -EINVAL; | |
3605 | return __io_splice_prep(req, sqe); | |
3606 | } | |
3607 | ||
45d189c6 | 3608 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3609 | { |
3610 | struct io_splice *sp = &req->splice; | |
3611 | struct file *in = sp->file_in; | |
3612 | struct file *out = sp->file_out; | |
3613 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3614 | long ret = 0; | |
3615 | ||
45d189c6 | 3616 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3617 | return -EAGAIN; |
3618 | if (sp->len) | |
3619 | ret = do_tee(in, out, sp->len, flags); | |
3620 | ||
3621 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3622 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3623 | ||
f2a8d5c7 PB |
3624 | if (ret != sp->len) |
3625 | req_set_fail_links(req); | |
e1e16097 | 3626 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3627 | return 0; |
3628 | } | |
3629 | ||
3630 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3631 | { | |
3632 | struct io_splice* sp = &req->splice; | |
3633 | ||
3634 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3635 | sp->off_out = READ_ONCE(sqe->off); | |
3636 | return __io_splice_prep(req, sqe); | |
3637 | } | |
3638 | ||
45d189c6 | 3639 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3640 | { |
3641 | struct io_splice *sp = &req->splice; | |
3642 | struct file *in = sp->file_in; | |
3643 | struct file *out = sp->file_out; | |
3644 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3645 | loff_t *poff_in, *poff_out; | |
c9687426 | 3646 | long ret = 0; |
7d67af2c | 3647 | |
45d189c6 | 3648 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3649 | return -EAGAIN; |
7d67af2c PB |
3650 | |
3651 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3652 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3653 | |
948a7749 | 3654 | if (sp->len) |
c9687426 | 3655 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3656 | |
3657 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3658 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3659 | ||
7d67af2c PB |
3660 | if (ret != sp->len) |
3661 | req_set_fail_links(req); | |
e1e16097 | 3662 | io_req_complete(req, ret); |
7d67af2c PB |
3663 | return 0; |
3664 | } | |
3665 | ||
2b188cc1 JA |
3666 | /* |
3667 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3668 | */ | |
889fca73 | 3669 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3670 | { |
3671 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3672 | |
def596e9 JA |
3673 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3674 | return -EINVAL; | |
3675 | ||
889fca73 | 3676 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3677 | return 0; |
3678 | } | |
3679 | ||
1155c76a | 3680 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3681 | { |
6b06314c | 3682 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3683 | |
09bb8394 JA |
3684 | if (!req->file) |
3685 | return -EBADF; | |
c992fe29 | 3686 | |
6b06314c | 3687 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3688 | return -EINVAL; |
edafccee | 3689 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3690 | return -EINVAL; |
3691 | ||
8ed8d3c3 JA |
3692 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3693 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3694 | return -EINVAL; | |
3695 | ||
3696 | req->sync.off = READ_ONCE(sqe->off); | |
3697 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3698 | return 0; |
3699 | } | |
3700 | ||
45d189c6 | 3701 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3702 | { |
8ed8d3c3 | 3703 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3704 | int ret; |
3705 | ||
ac45abc0 | 3706 | /* fsync always requires a blocking context */ |
45d189c6 | 3707 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3708 | return -EAGAIN; |
3709 | ||
9adbd45d | 3710 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3711 | end > 0 ? end : LLONG_MAX, |
3712 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3713 | if (ret < 0) | |
3714 | req_set_fail_links(req); | |
e1e16097 | 3715 | io_req_complete(req, ret); |
c992fe29 CH |
3716 | return 0; |
3717 | } | |
3718 | ||
d63d1b5e JA |
3719 | static int io_fallocate_prep(struct io_kiocb *req, |
3720 | const struct io_uring_sqe *sqe) | |
3721 | { | |
3722 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3723 | return -EINVAL; | |
3232dd02 PB |
3724 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3725 | return -EINVAL; | |
d63d1b5e JA |
3726 | |
3727 | req->sync.off = READ_ONCE(sqe->off); | |
3728 | req->sync.len = READ_ONCE(sqe->addr); | |
3729 | req->sync.mode = READ_ONCE(sqe->len); | |
3730 | return 0; | |
3731 | } | |
3732 | ||
45d189c6 | 3733 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3734 | { |
ac45abc0 PB |
3735 | int ret; |
3736 | ||
d63d1b5e | 3737 | /* fallocate always requiring blocking context */ |
45d189c6 | 3738 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3739 | return -EAGAIN; |
ac45abc0 PB |
3740 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3741 | req->sync.len); | |
ac45abc0 PB |
3742 | if (ret < 0) |
3743 | req_set_fail_links(req); | |
e1e16097 | 3744 | io_req_complete(req, ret); |
5d17b4a4 JA |
3745 | return 0; |
3746 | } | |
3747 | ||
ec65fea5 | 3748 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3749 | { |
f8748881 | 3750 | const char __user *fname; |
15b71abe | 3751 | int ret; |
b7bb4f7d | 3752 | |
ec65fea5 | 3753 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3754 | return -EINVAL; |
ec65fea5 | 3755 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3756 | return -EBADF; |
03b1230c | 3757 | |
ec65fea5 PB |
3758 | /* open.how should be already initialised */ |
3759 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3760 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3761 | |
25e72d10 PB |
3762 | req->open.dfd = READ_ONCE(sqe->fd); |
3763 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3764 | req->open.filename = getname(fname); |
15b71abe JA |
3765 | if (IS_ERR(req->open.filename)) { |
3766 | ret = PTR_ERR(req->open.filename); | |
3767 | req->open.filename = NULL; | |
3768 | return ret; | |
3769 | } | |
4022e7af | 3770 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3771 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3772 | return 0; |
03b1230c JA |
3773 | } |
3774 | ||
ec65fea5 PB |
3775 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3776 | { | |
3777 | u64 flags, mode; | |
3778 | ||
14587a46 | 3779 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3780 | return -EINVAL; |
ec65fea5 PB |
3781 | mode = READ_ONCE(sqe->len); |
3782 | flags = READ_ONCE(sqe->open_flags); | |
3783 | req->open.how = build_open_how(flags, mode); | |
3784 | return __io_openat_prep(req, sqe); | |
3785 | } | |
3786 | ||
cebdb986 | 3787 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3788 | { |
cebdb986 | 3789 | struct open_how __user *how; |
cebdb986 | 3790 | size_t len; |
0fa03c62 JA |
3791 | int ret; |
3792 | ||
14587a46 | 3793 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3794 | return -EINVAL; |
cebdb986 JA |
3795 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3796 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3797 | if (len < OPEN_HOW_SIZE_VER0) |
3798 | return -EINVAL; | |
3529d8c2 | 3799 | |
cebdb986 JA |
3800 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3801 | len); | |
3802 | if (ret) | |
3803 | return ret; | |
3529d8c2 | 3804 | |
ec65fea5 | 3805 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3806 | } |
3807 | ||
45d189c6 | 3808 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3809 | { |
3810 | struct open_flags op; | |
15b71abe | 3811 | struct file *file; |
3a81fd02 JA |
3812 | bool nonblock_set; |
3813 | bool resolve_nonblock; | |
15b71abe JA |
3814 | int ret; |
3815 | ||
cebdb986 | 3816 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3817 | if (ret) |
3818 | goto err; | |
3a81fd02 JA |
3819 | nonblock_set = op.open_flag & O_NONBLOCK; |
3820 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3821 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3822 | /* |
3823 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3824 | * it'll always -EAGAIN | |
3825 | */ | |
3826 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3827 | return -EAGAIN; | |
3828 | op.lookup_flags |= LOOKUP_CACHED; | |
3829 | op.open_flag |= O_NONBLOCK; | |
3830 | } | |
15b71abe | 3831 | |
4022e7af | 3832 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3833 | if (ret < 0) |
3834 | goto err; | |
3835 | ||
3836 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3837 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3838 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3839 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3840 | /* |
3a81fd02 JA |
3841 | * We could hang on to this 'fd', but seems like marginal |
3842 | * gain for something that is now known to be a slower path. | |
3843 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3844 | */ |
3a81fd02 JA |
3845 | put_unused_fd(ret); |
3846 | return -EAGAIN; | |
3847 | } | |
3848 | ||
15b71abe JA |
3849 | if (IS_ERR(file)) { |
3850 | put_unused_fd(ret); | |
3851 | ret = PTR_ERR(file); | |
3852 | } else { | |
45d189c6 | 3853 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3854 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3855 | fsnotify_open(file); |
3856 | fd_install(ret, file); | |
3857 | } | |
3858 | err: | |
3859 | putname(req->open.filename); | |
8fef80bf | 3860 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3861 | if (ret < 0) |
3862 | req_set_fail_links(req); | |
e1e16097 | 3863 | io_req_complete(req, ret); |
15b71abe JA |
3864 | return 0; |
3865 | } | |
3866 | ||
45d189c6 | 3867 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3868 | { |
45d189c6 | 3869 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
3870 | } |
3871 | ||
067524e9 JA |
3872 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3873 | const struct io_uring_sqe *sqe) | |
3874 | { | |
3875 | struct io_provide_buf *p = &req->pbuf; | |
3876 | u64 tmp; | |
3877 | ||
3878 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3879 | return -EINVAL; | |
3880 | ||
3881 | tmp = READ_ONCE(sqe->fd); | |
3882 | if (!tmp || tmp > USHRT_MAX) | |
3883 | return -EINVAL; | |
3884 | ||
3885 | memset(p, 0, sizeof(*p)); | |
3886 | p->nbufs = tmp; | |
3887 | p->bgid = READ_ONCE(sqe->buf_group); | |
3888 | return 0; | |
3889 | } | |
3890 | ||
3891 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3892 | int bgid, unsigned nbufs) | |
3893 | { | |
3894 | unsigned i = 0; | |
3895 | ||
3896 | /* shouldn't happen */ | |
3897 | if (!nbufs) | |
3898 | return 0; | |
3899 | ||
3900 | /* the head kbuf is the list itself */ | |
3901 | while (!list_empty(&buf->list)) { | |
3902 | struct io_buffer *nxt; | |
3903 | ||
3904 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3905 | list_del(&nxt->list); | |
3906 | kfree(nxt); | |
3907 | if (++i == nbufs) | |
3908 | return i; | |
3909 | } | |
3910 | i++; | |
3911 | kfree(buf); | |
3912 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3913 | ||
3914 | return i; | |
3915 | } | |
3916 | ||
889fca73 | 3917 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3918 | { |
3919 | struct io_provide_buf *p = &req->pbuf; | |
3920 | struct io_ring_ctx *ctx = req->ctx; | |
3921 | struct io_buffer *head; | |
3922 | int ret = 0; | |
45d189c6 | 3923 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3924 | |
3925 | io_ring_submit_lock(ctx, !force_nonblock); | |
3926 | ||
3927 | lockdep_assert_held(&ctx->uring_lock); | |
3928 | ||
3929 | ret = -ENOENT; | |
3930 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3931 | if (head) | |
3932 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3933 | if (ret < 0) |
3934 | req_set_fail_links(req); | |
067524e9 | 3935 | |
31bff9a5 PB |
3936 | /* need to hold the lock to complete IOPOLL requests */ |
3937 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 3938 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
3939 | io_ring_submit_unlock(ctx, !force_nonblock); |
3940 | } else { | |
3941 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 3942 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 3943 | } |
067524e9 JA |
3944 | return 0; |
3945 | } | |
3946 | ||
ddf0322d JA |
3947 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3948 | const struct io_uring_sqe *sqe) | |
3949 | { | |
3950 | struct io_provide_buf *p = &req->pbuf; | |
3951 | u64 tmp; | |
3952 | ||
3953 | if (sqe->ioprio || sqe->rw_flags) | |
3954 | return -EINVAL; | |
3955 | ||
3956 | tmp = READ_ONCE(sqe->fd); | |
3957 | if (!tmp || tmp > USHRT_MAX) | |
3958 | return -E2BIG; | |
3959 | p->nbufs = tmp; | |
3960 | p->addr = READ_ONCE(sqe->addr); | |
3961 | p->len = READ_ONCE(sqe->len); | |
3962 | ||
efe68c1c | 3963 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3964 | return -EFAULT; |
3965 | ||
3966 | p->bgid = READ_ONCE(sqe->buf_group); | |
3967 | tmp = READ_ONCE(sqe->off); | |
3968 | if (tmp > USHRT_MAX) | |
3969 | return -E2BIG; | |
3970 | p->bid = tmp; | |
3971 | return 0; | |
3972 | } | |
3973 | ||
3974 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3975 | { | |
3976 | struct io_buffer *buf; | |
3977 | u64 addr = pbuf->addr; | |
3978 | int i, bid = pbuf->bid; | |
3979 | ||
3980 | for (i = 0; i < pbuf->nbufs; i++) { | |
3981 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3982 | if (!buf) | |
3983 | break; | |
3984 | ||
3985 | buf->addr = addr; | |
3986 | buf->len = pbuf->len; | |
3987 | buf->bid = bid; | |
3988 | addr += pbuf->len; | |
3989 | bid++; | |
3990 | if (!*head) { | |
3991 | INIT_LIST_HEAD(&buf->list); | |
3992 | *head = buf; | |
3993 | } else { | |
3994 | list_add_tail(&buf->list, &(*head)->list); | |
3995 | } | |
3996 | } | |
3997 | ||
3998 | return i ? i : -ENOMEM; | |
3999 | } | |
4000 | ||
889fca73 | 4001 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4002 | { |
4003 | struct io_provide_buf *p = &req->pbuf; | |
4004 | struct io_ring_ctx *ctx = req->ctx; | |
4005 | struct io_buffer *head, *list; | |
4006 | int ret = 0; | |
45d189c6 | 4007 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4008 | |
4009 | io_ring_submit_lock(ctx, !force_nonblock); | |
4010 | ||
4011 | lockdep_assert_held(&ctx->uring_lock); | |
4012 | ||
4013 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4014 | ||
4015 | ret = io_add_buffers(p, &head); | |
4016 | if (ret < 0) | |
4017 | goto out; | |
4018 | ||
4019 | if (!list) { | |
4020 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4021 | GFP_KERNEL); | |
4022 | if (ret < 0) { | |
067524e9 | 4023 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4024 | goto out; |
4025 | } | |
4026 | } | |
4027 | out: | |
ddf0322d JA |
4028 | if (ret < 0) |
4029 | req_set_fail_links(req); | |
31bff9a5 PB |
4030 | |
4031 | /* need to hold the lock to complete IOPOLL requests */ | |
4032 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4033 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4034 | io_ring_submit_unlock(ctx, !force_nonblock); |
4035 | } else { | |
4036 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4037 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4038 | } |
ddf0322d | 4039 | return 0; |
cebdb986 JA |
4040 | } |
4041 | ||
3e4827b0 JA |
4042 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4043 | const struct io_uring_sqe *sqe) | |
4044 | { | |
4045 | #if defined(CONFIG_EPOLL) | |
4046 | if (sqe->ioprio || sqe->buf_index) | |
4047 | return -EINVAL; | |
6ca56f84 | 4048 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4049 | return -EINVAL; |
3e4827b0 JA |
4050 | |
4051 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4052 | req->epoll.op = READ_ONCE(sqe->len); | |
4053 | req->epoll.fd = READ_ONCE(sqe->off); | |
4054 | ||
4055 | if (ep_op_has_event(req->epoll.op)) { | |
4056 | struct epoll_event __user *ev; | |
4057 | ||
4058 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4059 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4060 | return -EFAULT; | |
4061 | } | |
4062 | ||
4063 | return 0; | |
4064 | #else | |
4065 | return -EOPNOTSUPP; | |
4066 | #endif | |
4067 | } | |
4068 | ||
889fca73 | 4069 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4070 | { |
4071 | #if defined(CONFIG_EPOLL) | |
4072 | struct io_epoll *ie = &req->epoll; | |
4073 | int ret; | |
45d189c6 | 4074 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4075 | |
4076 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4077 | if (force_nonblock && ret == -EAGAIN) | |
4078 | return -EAGAIN; | |
4079 | ||
4080 | if (ret < 0) | |
4081 | req_set_fail_links(req); | |
889fca73 | 4082 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4083 | return 0; |
4084 | #else | |
4085 | return -EOPNOTSUPP; | |
4086 | #endif | |
4087 | } | |
4088 | ||
c1ca757b JA |
4089 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4090 | { | |
4091 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4092 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4093 | return -EINVAL; | |
3232dd02 PB |
4094 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4095 | return -EINVAL; | |
c1ca757b JA |
4096 | |
4097 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4098 | req->madvise.len = READ_ONCE(sqe->len); | |
4099 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4100 | return 0; | |
4101 | #else | |
4102 | return -EOPNOTSUPP; | |
4103 | #endif | |
4104 | } | |
4105 | ||
45d189c6 | 4106 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4107 | { |
4108 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4109 | struct io_madvise *ma = &req->madvise; | |
4110 | int ret; | |
4111 | ||
45d189c6 | 4112 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4113 | return -EAGAIN; |
4114 | ||
0726b01e | 4115 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4116 | if (ret < 0) |
4117 | req_set_fail_links(req); | |
e1e16097 | 4118 | io_req_complete(req, ret); |
c1ca757b JA |
4119 | return 0; |
4120 | #else | |
4121 | return -EOPNOTSUPP; | |
4122 | #endif | |
4123 | } | |
4124 | ||
4840e418 JA |
4125 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4126 | { | |
4127 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4128 | return -EINVAL; | |
3232dd02 PB |
4129 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4130 | return -EINVAL; | |
4840e418 JA |
4131 | |
4132 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4133 | req->fadvise.len = READ_ONCE(sqe->len); | |
4134 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4135 | return 0; | |
4136 | } | |
4137 | ||
45d189c6 | 4138 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4139 | { |
4140 | struct io_fadvise *fa = &req->fadvise; | |
4141 | int ret; | |
4142 | ||
45d189c6 | 4143 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4144 | switch (fa->advice) { |
4145 | case POSIX_FADV_NORMAL: | |
4146 | case POSIX_FADV_RANDOM: | |
4147 | case POSIX_FADV_SEQUENTIAL: | |
4148 | break; | |
4149 | default: | |
4150 | return -EAGAIN; | |
4151 | } | |
4152 | } | |
4840e418 JA |
4153 | |
4154 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4155 | if (ret < 0) | |
4156 | req_set_fail_links(req); | |
e1e16097 | 4157 | io_req_complete(req, ret); |
4840e418 JA |
4158 | return 0; |
4159 | } | |
4160 | ||
eddc7ef5 JA |
4161 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4162 | { | |
6ca56f84 | 4163 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4164 | return -EINVAL; |
eddc7ef5 JA |
4165 | if (sqe->ioprio || sqe->buf_index) |
4166 | return -EINVAL; | |
9c280f90 | 4167 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4168 | return -EBADF; |
eddc7ef5 | 4169 | |
1d9e1288 BM |
4170 | req->statx.dfd = READ_ONCE(sqe->fd); |
4171 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4172 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4173 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4174 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4175 | |
4176 | return 0; | |
4177 | } | |
4178 | ||
45d189c6 | 4179 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4180 | { |
1d9e1288 | 4181 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4182 | int ret; |
4183 | ||
45d189c6 | 4184 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4185 | /* only need file table for an actual valid fd */ |
4186 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4187 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4188 | return -EAGAIN; |
5b0bbee4 | 4189 | } |
eddc7ef5 | 4190 | |
e62753e4 BM |
4191 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4192 | ctx->buffer); | |
eddc7ef5 | 4193 | |
eddc7ef5 JA |
4194 | if (ret < 0) |
4195 | req_set_fail_links(req); | |
e1e16097 | 4196 | io_req_complete(req, ret); |
eddc7ef5 JA |
4197 | return 0; |
4198 | } | |
4199 | ||
b5dba59e JA |
4200 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4201 | { | |
14587a46 | 4202 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4203 | return -EINVAL; |
b5dba59e JA |
4204 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4205 | sqe->rw_flags || sqe->buf_index) | |
4206 | return -EINVAL; | |
9c280f90 | 4207 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4208 | return -EBADF; |
b5dba59e JA |
4209 | |
4210 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4211 | return 0; |
b5dba59e JA |
4212 | } |
4213 | ||
889fca73 | 4214 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4215 | { |
9eac1904 | 4216 | struct files_struct *files = current->files; |
3af73b28 | 4217 | struct io_close *close = &req->close; |
9eac1904 JA |
4218 | struct fdtable *fdt; |
4219 | struct file *file; | |
b5dba59e JA |
4220 | int ret; |
4221 | ||
9eac1904 JA |
4222 | file = NULL; |
4223 | ret = -EBADF; | |
4224 | spin_lock(&files->file_lock); | |
4225 | fdt = files_fdtable(files); | |
4226 | if (close->fd >= fdt->max_fds) { | |
4227 | spin_unlock(&files->file_lock); | |
4228 | goto err; | |
4229 | } | |
4230 | file = fdt->fd[close->fd]; | |
4231 | if (!file) { | |
4232 | spin_unlock(&files->file_lock); | |
4233 | goto err; | |
4234 | } | |
4235 | ||
4236 | if (file->f_op == &io_uring_fops) { | |
4237 | spin_unlock(&files->file_lock); | |
4238 | file = NULL; | |
4239 | goto err; | |
3af73b28 | 4240 | } |
b5dba59e JA |
4241 | |
4242 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4243 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4244 | spin_unlock(&files->file_lock); |
0bf0eefd | 4245 | return -EAGAIN; |
a2100672 | 4246 | } |
b5dba59e | 4247 | |
9eac1904 JA |
4248 | ret = __close_fd_get_file(close->fd, &file); |
4249 | spin_unlock(&files->file_lock); | |
4250 | if (ret < 0) { | |
4251 | if (ret == -ENOENT) | |
4252 | ret = -EBADF; | |
4253 | goto err; | |
4254 | } | |
4255 | ||
3af73b28 | 4256 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4257 | ret = filp_close(file, current->files); |
4258 | err: | |
3af73b28 PB |
4259 | if (ret < 0) |
4260 | req_set_fail_links(req); | |
9eac1904 JA |
4261 | if (file) |
4262 | fput(file); | |
889fca73 | 4263 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4264 | return 0; |
b5dba59e JA |
4265 | } |
4266 | ||
1155c76a | 4267 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4268 | { |
4269 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4270 | |
5d17b4a4 JA |
4271 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4272 | return -EINVAL; | |
4273 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4274 | return -EINVAL; | |
4275 | ||
8ed8d3c3 JA |
4276 | req->sync.off = READ_ONCE(sqe->off); |
4277 | req->sync.len = READ_ONCE(sqe->len); | |
4278 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4279 | return 0; |
4280 | } | |
4281 | ||
45d189c6 | 4282 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4283 | { |
8ed8d3c3 JA |
4284 | int ret; |
4285 | ||
ac45abc0 | 4286 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4287 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4288 | return -EAGAIN; |
4289 | ||
9adbd45d | 4290 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4291 | req->sync.flags); |
4292 | if (ret < 0) | |
4293 | req_set_fail_links(req); | |
e1e16097 | 4294 | io_req_complete(req, ret); |
5d17b4a4 JA |
4295 | return 0; |
4296 | } | |
4297 | ||
469956e8 | 4298 | #if defined(CONFIG_NET) |
02d27d89 PB |
4299 | static int io_setup_async_msg(struct io_kiocb *req, |
4300 | struct io_async_msghdr *kmsg) | |
4301 | { | |
e8c2bc1f JA |
4302 | struct io_async_msghdr *async_msg = req->async_data; |
4303 | ||
4304 | if (async_msg) | |
02d27d89 | 4305 | return -EAGAIN; |
e8c2bc1f | 4306 | if (io_alloc_async_data(req)) { |
257e84a5 | 4307 | kfree(kmsg->free_iov); |
02d27d89 PB |
4308 | return -ENOMEM; |
4309 | } | |
e8c2bc1f | 4310 | async_msg = req->async_data; |
02d27d89 | 4311 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4312 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4313 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4314 | /* if were using fast_iov, set it to the new one */ |
4315 | if (!async_msg->free_iov) | |
4316 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4317 | ||
02d27d89 PB |
4318 | return -EAGAIN; |
4319 | } | |
4320 | ||
2ae523ed PB |
4321 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4322 | struct io_async_msghdr *iomsg) | |
4323 | { | |
2ae523ed | 4324 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4325 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4326 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4327 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4328 | } |
4329 | ||
93642ef8 PB |
4330 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4331 | { | |
4332 | int ret; | |
4333 | ||
4334 | if (!io_op_defs[req->opcode].needs_async_data) | |
4335 | return 0; | |
4336 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4337 | if (!ret) | |
4338 | req->flags |= REQ_F_NEED_CLEANUP; | |
4339 | return ret; | |
4340 | } | |
4341 | ||
3529d8c2 | 4342 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4343 | { |
e47293fd | 4344 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4345 | |
d2b6f48b PB |
4346 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4347 | return -EINVAL; | |
4348 | ||
e47293fd | 4349 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4350 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4351 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4352 | |
d8768362 JA |
4353 | #ifdef CONFIG_COMPAT |
4354 | if (req->ctx->compat) | |
4355 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4356 | #endif | |
93642ef8 | 4357 | return 0; |
03b1230c JA |
4358 | } |
4359 | ||
889fca73 | 4360 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4361 | { |
6b754c8b | 4362 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4363 | struct socket *sock; |
7a7cacba | 4364 | unsigned flags; |
0fa03c62 JA |
4365 | int ret; |
4366 | ||
dba4a925 | 4367 | sock = sock_from_file(req->file); |
7a7cacba | 4368 | if (unlikely(!sock)) |
dba4a925 | 4369 | return -ENOTSOCK; |
3529d8c2 | 4370 | |
257e84a5 PB |
4371 | kmsg = req->async_data; |
4372 | if (!kmsg) { | |
7a7cacba PB |
4373 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4374 | if (ret) | |
4375 | return ret; | |
4376 | kmsg = &iomsg; | |
0fa03c62 | 4377 | } |
0fa03c62 | 4378 | |
7a7cacba PB |
4379 | flags = req->sr_msg.msg_flags; |
4380 | if (flags & MSG_DONTWAIT) | |
4381 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4382 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4383 | flags |= MSG_DONTWAIT; |
e47293fd | 4384 | |
7a7cacba | 4385 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4386 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4387 | return io_setup_async_msg(req, kmsg); |
4388 | if (ret == -ERESTARTSYS) | |
4389 | ret = -EINTR; | |
0fa03c62 | 4390 | |
257e84a5 PB |
4391 | /* fast path, check for non-NULL to avoid function call */ |
4392 | if (kmsg->free_iov) | |
4393 | kfree(kmsg->free_iov); | |
99bc4c38 | 4394 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4395 | if (ret < 0) |
4396 | req_set_fail_links(req); | |
889fca73 | 4397 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4398 | return 0; |
03b1230c | 4399 | } |
aa1fa28f | 4400 | |
889fca73 | 4401 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4402 | { |
7a7cacba PB |
4403 | struct io_sr_msg *sr = &req->sr_msg; |
4404 | struct msghdr msg; | |
4405 | struct iovec iov; | |
fddaface | 4406 | struct socket *sock; |
7a7cacba | 4407 | unsigned flags; |
fddaface JA |
4408 | int ret; |
4409 | ||
dba4a925 | 4410 | sock = sock_from_file(req->file); |
7a7cacba | 4411 | if (unlikely(!sock)) |
dba4a925 | 4412 | return -ENOTSOCK; |
fddaface | 4413 | |
7a7cacba PB |
4414 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4415 | if (unlikely(ret)) | |
14db8411 | 4416 | return ret; |
fddaface | 4417 | |
7a7cacba PB |
4418 | msg.msg_name = NULL; |
4419 | msg.msg_control = NULL; | |
4420 | msg.msg_controllen = 0; | |
4421 | msg.msg_namelen = 0; | |
fddaface | 4422 | |
7a7cacba PB |
4423 | flags = req->sr_msg.msg_flags; |
4424 | if (flags & MSG_DONTWAIT) | |
4425 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4426 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4427 | flags |= MSG_DONTWAIT; |
fddaface | 4428 | |
7a7cacba PB |
4429 | msg.msg_flags = flags; |
4430 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4431 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4432 | return -EAGAIN; |
4433 | if (ret == -ERESTARTSYS) | |
4434 | ret = -EINTR; | |
fddaface | 4435 | |
fddaface JA |
4436 | if (ret < 0) |
4437 | req_set_fail_links(req); | |
889fca73 | 4438 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4439 | return 0; |
fddaface JA |
4440 | } |
4441 | ||
1400e697 PB |
4442 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4443 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4444 | { |
4445 | struct io_sr_msg *sr = &req->sr_msg; | |
4446 | struct iovec __user *uiov; | |
4447 | size_t iov_len; | |
4448 | int ret; | |
4449 | ||
1400e697 PB |
4450 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4451 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4452 | if (ret) |
4453 | return ret; | |
4454 | ||
4455 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4456 | if (iov_len > 1) | |
4457 | return -EINVAL; | |
5476dfed | 4458 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4459 | return -EFAULT; |
5476dfed | 4460 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4461 | iomsg->free_iov = NULL; |
52de1fe1 | 4462 | } else { |
257e84a5 | 4463 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4464 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4465 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4466 | false); |
52de1fe1 JA |
4467 | if (ret > 0) |
4468 | ret = 0; | |
4469 | } | |
4470 | ||
4471 | return ret; | |
4472 | } | |
4473 | ||
4474 | #ifdef CONFIG_COMPAT | |
4475 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4476 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4477 | { |
4478 | struct compat_msghdr __user *msg_compat; | |
4479 | struct io_sr_msg *sr = &req->sr_msg; | |
4480 | struct compat_iovec __user *uiov; | |
4481 | compat_uptr_t ptr; | |
4482 | compat_size_t len; | |
4483 | int ret; | |
4484 | ||
270a5940 | 4485 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4486 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4487 | &ptr, &len); |
4488 | if (ret) | |
4489 | return ret; | |
4490 | ||
4491 | uiov = compat_ptr(ptr); | |
4492 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4493 | compat_ssize_t clen; | |
4494 | ||
4495 | if (len > 1) | |
4496 | return -EINVAL; | |
4497 | if (!access_ok(uiov, sizeof(*uiov))) | |
4498 | return -EFAULT; | |
4499 | if (__get_user(clen, &uiov->iov_len)) | |
4500 | return -EFAULT; | |
4501 | if (clen < 0) | |
4502 | return -EINVAL; | |
2d280bc8 | 4503 | sr->len = clen; |
257e84a5 | 4504 | iomsg->free_iov = NULL; |
52de1fe1 | 4505 | } else { |
257e84a5 | 4506 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4507 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4508 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4509 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4510 | if (ret < 0) |
4511 | return ret; | |
4512 | } | |
4513 | ||
4514 | return 0; | |
4515 | } | |
4516 | #endif | |
4517 | ||
1400e697 PB |
4518 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4519 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4520 | { |
1400e697 | 4521 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4522 | |
4523 | #ifdef CONFIG_COMPAT | |
4524 | if (req->ctx->compat) | |
1400e697 | 4525 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4526 | #endif |
52de1fe1 | 4527 | |
1400e697 | 4528 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4529 | } |
4530 | ||
bcda7baa | 4531 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4532 | bool needs_lock) |
bcda7baa JA |
4533 | { |
4534 | struct io_sr_msg *sr = &req->sr_msg; | |
4535 | struct io_buffer *kbuf; | |
4536 | ||
bcda7baa JA |
4537 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4538 | if (IS_ERR(kbuf)) | |
4539 | return kbuf; | |
4540 | ||
4541 | sr->kbuf = kbuf; | |
4542 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4543 | return kbuf; |
fddaface JA |
4544 | } |
4545 | ||
7fbb1b54 PB |
4546 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4547 | { | |
4548 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4549 | } | |
4550 | ||
93642ef8 | 4551 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4552 | { |
99bc4c38 | 4553 | int ret; |
3529d8c2 | 4554 | |
93642ef8 PB |
4555 | if (!io_op_defs[req->opcode].needs_async_data) |
4556 | return 0; | |
4557 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4558 | if (!ret) | |
4559 | req->flags |= REQ_F_NEED_CLEANUP; | |
4560 | return ret; | |
4561 | } | |
4562 | ||
4563 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4564 | { | |
4565 | struct io_sr_msg *sr = &req->sr_msg; | |
4566 | ||
d2b6f48b PB |
4567 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4568 | return -EINVAL; | |
4569 | ||
3529d8c2 | 4570 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4571 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4572 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4573 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4574 | |
d8768362 JA |
4575 | #ifdef CONFIG_COMPAT |
4576 | if (req->ctx->compat) | |
4577 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4578 | #endif | |
93642ef8 | 4579 | return 0; |
aa1fa28f JA |
4580 | } |
4581 | ||
889fca73 | 4582 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4583 | { |
6b754c8b | 4584 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4585 | struct socket *sock; |
7fbb1b54 | 4586 | struct io_buffer *kbuf; |
7a7cacba | 4587 | unsigned flags; |
52de1fe1 | 4588 | int ret, cflags = 0; |
45d189c6 | 4589 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4590 | |
dba4a925 | 4591 | sock = sock_from_file(req->file); |
7a7cacba | 4592 | if (unlikely(!sock)) |
dba4a925 | 4593 | return -ENOTSOCK; |
3529d8c2 | 4594 | |
257e84a5 PB |
4595 | kmsg = req->async_data; |
4596 | if (!kmsg) { | |
7a7cacba PB |
4597 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4598 | if (ret) | |
681fda8d | 4599 | return ret; |
7a7cacba PB |
4600 | kmsg = &iomsg; |
4601 | } | |
03b1230c | 4602 | |
bc02ef33 | 4603 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4604 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4605 | if (IS_ERR(kbuf)) |
52de1fe1 | 4606 | return PTR_ERR(kbuf); |
7a7cacba | 4607 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4608 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4609 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4610 | 1, req->sr_msg.len); |
4611 | } | |
52de1fe1 | 4612 | |
7a7cacba PB |
4613 | flags = req->sr_msg.msg_flags; |
4614 | if (flags & MSG_DONTWAIT) | |
4615 | req->flags |= REQ_F_NOWAIT; | |
4616 | else if (force_nonblock) | |
4617 | flags |= MSG_DONTWAIT; | |
e47293fd | 4618 | |
7a7cacba PB |
4619 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4620 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4621 | if (force_nonblock && ret == -EAGAIN) |
4622 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4623 | if (ret == -ERESTARTSYS) |
4624 | ret = -EINTR; | |
03b1230c | 4625 | |
7fbb1b54 PB |
4626 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4627 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4628 | /* fast path, check for non-NULL to avoid function call */ |
4629 | if (kmsg->free_iov) | |
4630 | kfree(kmsg->free_iov); | |
99bc4c38 | 4631 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4632 | if (ret < 0) |
4633 | req_set_fail_links(req); | |
889fca73 | 4634 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4635 | return 0; |
0fa03c62 | 4636 | } |
5d17b4a4 | 4637 | |
889fca73 | 4638 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4639 | { |
6b754c8b | 4640 | struct io_buffer *kbuf; |
7a7cacba PB |
4641 | struct io_sr_msg *sr = &req->sr_msg; |
4642 | struct msghdr msg; | |
4643 | void __user *buf = sr->buf; | |
fddaface | 4644 | struct socket *sock; |
7a7cacba PB |
4645 | struct iovec iov; |
4646 | unsigned flags; | |
bcda7baa | 4647 | int ret, cflags = 0; |
45d189c6 | 4648 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4649 | |
dba4a925 | 4650 | sock = sock_from_file(req->file); |
7a7cacba | 4651 | if (unlikely(!sock)) |
dba4a925 | 4652 | return -ENOTSOCK; |
fddaface | 4653 | |
bc02ef33 | 4654 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4655 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4656 | if (IS_ERR(kbuf)) |
4657 | return PTR_ERR(kbuf); | |
7a7cacba | 4658 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4659 | } |
bcda7baa | 4660 | |
7a7cacba | 4661 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4662 | if (unlikely(ret)) |
4663 | goto out_free; | |
fddaface | 4664 | |
7a7cacba PB |
4665 | msg.msg_name = NULL; |
4666 | msg.msg_control = NULL; | |
4667 | msg.msg_controllen = 0; | |
4668 | msg.msg_namelen = 0; | |
4669 | msg.msg_iocb = NULL; | |
4670 | msg.msg_flags = 0; | |
fddaface | 4671 | |
7a7cacba PB |
4672 | flags = req->sr_msg.msg_flags; |
4673 | if (flags & MSG_DONTWAIT) | |
4674 | req->flags |= REQ_F_NOWAIT; | |
4675 | else if (force_nonblock) | |
4676 | flags |= MSG_DONTWAIT; | |
4677 | ||
4678 | ret = sock_recvmsg(sock, &msg, flags); | |
4679 | if (force_nonblock && ret == -EAGAIN) | |
4680 | return -EAGAIN; | |
4681 | if (ret == -ERESTARTSYS) | |
4682 | ret = -EINTR; | |
14c32eee | 4683 | out_free: |
7fbb1b54 PB |
4684 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4685 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4686 | if (ret < 0) |
4687 | req_set_fail_links(req); | |
889fca73 | 4688 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4689 | return 0; |
fddaface JA |
4690 | } |
4691 | ||
3529d8c2 | 4692 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4693 | { |
8ed8d3c3 JA |
4694 | struct io_accept *accept = &req->accept; |
4695 | ||
14587a46 | 4696 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4697 | return -EINVAL; |
8042d6ce | 4698 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4699 | return -EINVAL; |
4700 | ||
d55e5f5b JA |
4701 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4702 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4703 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4704 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4705 | return 0; |
8ed8d3c3 | 4706 | } |
17f2fe35 | 4707 | |
889fca73 | 4708 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4709 | { |
4710 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4711 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4712 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4713 | int ret; |
4714 | ||
e697deed JX |
4715 | if (req->file->f_flags & O_NONBLOCK) |
4716 | req->flags |= REQ_F_NOWAIT; | |
4717 | ||
8ed8d3c3 | 4718 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4719 | accept->addr_len, accept->flags, |
4720 | accept->nofile); | |
8ed8d3c3 | 4721 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4722 | return -EAGAIN; |
ac45abc0 PB |
4723 | if (ret < 0) { |
4724 | if (ret == -ERESTARTSYS) | |
4725 | ret = -EINTR; | |
4e88d6e7 | 4726 | req_set_fail_links(req); |
ac45abc0 | 4727 | } |
889fca73 | 4728 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4729 | return 0; |
8ed8d3c3 JA |
4730 | } |
4731 | ||
93642ef8 PB |
4732 | static int io_connect_prep_async(struct io_kiocb *req) |
4733 | { | |
4734 | struct io_async_connect *io = req->async_data; | |
4735 | struct io_connect *conn = &req->connect; | |
4736 | ||
4737 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4738 | } | |
4739 | ||
3529d8c2 | 4740 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4741 | { |
3529d8c2 | 4742 | struct io_connect *conn = &req->connect; |
f499a021 | 4743 | |
14587a46 | 4744 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4745 | return -EINVAL; |
4746 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4747 | return -EINVAL; | |
4748 | ||
3529d8c2 JA |
4749 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4750 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4751 | return 0; |
f499a021 JA |
4752 | } |
4753 | ||
889fca73 | 4754 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4755 | { |
e8c2bc1f | 4756 | struct io_async_connect __io, *io; |
f8e85cf2 | 4757 | unsigned file_flags; |
3fbb51c1 | 4758 | int ret; |
45d189c6 | 4759 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4760 | |
e8c2bc1f JA |
4761 | if (req->async_data) { |
4762 | io = req->async_data; | |
f499a021 | 4763 | } else { |
3529d8c2 JA |
4764 | ret = move_addr_to_kernel(req->connect.addr, |
4765 | req->connect.addr_len, | |
e8c2bc1f | 4766 | &__io.address); |
f499a021 JA |
4767 | if (ret) |
4768 | goto out; | |
4769 | io = &__io; | |
4770 | } | |
4771 | ||
3fbb51c1 JA |
4772 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4773 | ||
e8c2bc1f | 4774 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4775 | req->connect.addr_len, file_flags); |
87f80d62 | 4776 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4777 | if (req->async_data) |
b7bb4f7d | 4778 | return -EAGAIN; |
e8c2bc1f | 4779 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4780 | ret = -ENOMEM; |
4781 | goto out; | |
4782 | } | |
e8c2bc1f JA |
4783 | io = req->async_data; |
4784 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4785 | return -EAGAIN; |
f499a021 | 4786 | } |
f8e85cf2 JA |
4787 | if (ret == -ERESTARTSYS) |
4788 | ret = -EINTR; | |
f499a021 | 4789 | out: |
4e88d6e7 JA |
4790 | if (ret < 0) |
4791 | req_set_fail_links(req); | |
889fca73 | 4792 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4793 | return 0; |
469956e8 Y |
4794 | } |
4795 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4796 | #define IO_NETOP_FN(op) \ |
4797 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4798 | { \ | |
4799 | return -EOPNOTSUPP; \ | |
4800 | } | |
4801 | ||
4802 | #define IO_NETOP_PREP(op) \ | |
4803 | IO_NETOP_FN(op) \ | |
4804 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4805 | { \ | |
4806 | return -EOPNOTSUPP; \ | |
4807 | } \ | |
4808 | ||
4809 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4810 | IO_NETOP_PREP(op) \ | |
4811 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4812 | { \ | |
4813 | return -EOPNOTSUPP; \ | |
4814 | } | |
4815 | ||
4816 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4817 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4818 | IO_NETOP_PREP_ASYNC(connect); | |
4819 | IO_NETOP_PREP(accept); | |
4820 | IO_NETOP_FN(send); | |
4821 | IO_NETOP_FN(recv); | |
469956e8 | 4822 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4823 | |
d7718a9d JA |
4824 | struct io_poll_table { |
4825 | struct poll_table_struct pt; | |
4826 | struct io_kiocb *req; | |
4827 | int error; | |
4828 | }; | |
ce593a6c | 4829 | |
d7718a9d JA |
4830 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4831 | __poll_t mask, task_work_func_t func) | |
4832 | { | |
aa96bf8a | 4833 | int ret; |
d7718a9d JA |
4834 | |
4835 | /* for instances that support it check for an event match first: */ | |
4836 | if (mask && !(mask & poll->events)) | |
4837 | return 0; | |
4838 | ||
4839 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4840 | ||
4841 | list_del_init(&poll->wait.entry); | |
4842 | ||
d7718a9d | 4843 | req->result = mask; |
7cbf1722 | 4844 | req->task_work.func = func; |
6d816e08 JA |
4845 | percpu_ref_get(&req->ctx->refs); |
4846 | ||
d7718a9d | 4847 | /* |
e3aabf95 JA |
4848 | * If this fails, then the task is exiting. When a task exits, the |
4849 | * work gets canceled, so just cancel this request as well instead | |
4850 | * of executing it. We can't safely execute it anyway, as we may not | |
4851 | * have the needed state needed for it anyway. | |
d7718a9d | 4852 | */ |
355fb9e2 | 4853 | ret = io_req_task_work_add(req); |
aa96bf8a | 4854 | if (unlikely(ret)) { |
e3aabf95 | 4855 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4856 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4857 | } |
d7718a9d JA |
4858 | return 1; |
4859 | } | |
4860 | ||
74ce6ce4 JA |
4861 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4862 | __acquires(&req->ctx->completion_lock) | |
4863 | { | |
4864 | struct io_ring_ctx *ctx = req->ctx; | |
4865 | ||
4866 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4867 | struct poll_table_struct pt = { ._key = poll->events }; | |
4868 | ||
4869 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4870 | } | |
4871 | ||
4872 | spin_lock_irq(&ctx->completion_lock); | |
4873 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4874 | add_wait_queue(poll->head, &poll->wait); | |
4875 | return true; | |
4876 | } | |
4877 | ||
4878 | return false; | |
4879 | } | |
4880 | ||
d4e7cd36 | 4881 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4882 | { |
e8c2bc1f | 4883 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4884 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4885 | return req->async_data; |
d4e7cd36 JA |
4886 | return req->apoll->double_poll; |
4887 | } | |
4888 | ||
4889 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4890 | { | |
4891 | if (req->opcode == IORING_OP_POLL_ADD) | |
4892 | return &req->poll; | |
4893 | return &req->apoll->poll; | |
4894 | } | |
4895 | ||
4896 | static void io_poll_remove_double(struct io_kiocb *req) | |
4897 | { | |
4898 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4899 | |
4900 | lockdep_assert_held(&req->ctx->completion_lock); | |
4901 | ||
4902 | if (poll && poll->head) { | |
4903 | struct wait_queue_head *head = poll->head; | |
4904 | ||
4905 | spin_lock(&head->lock); | |
4906 | list_del_init(&poll->wait.entry); | |
4907 | if (poll->wait.private) | |
4908 | refcount_dec(&req->refs); | |
4909 | poll->head = NULL; | |
4910 | spin_unlock(&head->lock); | |
4911 | } | |
4912 | } | |
4913 | ||
4914 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4915 | { | |
4916 | struct io_ring_ctx *ctx = req->ctx; | |
4917 | ||
d4e7cd36 | 4918 | io_poll_remove_double(req); |
18bceab1 JA |
4919 | req->poll.done = true; |
4920 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4921 | io_commit_cqring(ctx); | |
4922 | } | |
4923 | ||
dd221f46 | 4924 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4925 | { |
dd221f46 | 4926 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4927 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4928 | struct io_kiocb *nxt; |
18bceab1 JA |
4929 | |
4930 | if (io_poll_rewait(req, &req->poll)) { | |
4931 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
4932 | } else { |
4933 | hash_del(&req->hash_node); | |
4934 | io_poll_complete(req, req->result, 0); | |
4935 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 4936 | |
dd221f46 PB |
4937 | nxt = io_put_req_find_next(req); |
4938 | io_cqring_ev_posted(ctx); | |
4939 | if (nxt) | |
4940 | __io_req_task_submit(nxt); | |
4941 | } | |
18bceab1 | 4942 | |
6d816e08 | 4943 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4944 | } |
4945 | ||
4946 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4947 | int sync, void *key) | |
4948 | { | |
4949 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4950 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4951 | __poll_t mask = key_to_poll(key); |
4952 | ||
4953 | /* for instances that support it check for an event match first: */ | |
4954 | if (mask && !(mask & poll->events)) | |
4955 | return 0; | |
4956 | ||
8706e04e JA |
4957 | list_del_init(&wait->entry); |
4958 | ||
807abcb0 | 4959 | if (poll && poll->head) { |
18bceab1 JA |
4960 | bool done; |
4961 | ||
807abcb0 JA |
4962 | spin_lock(&poll->head->lock); |
4963 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4964 | if (!done) |
807abcb0 | 4965 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4966 | /* make sure double remove sees this as being gone */ |
4967 | wait->private = NULL; | |
807abcb0 | 4968 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4969 | if (!done) { |
4970 | /* use wait func handler, so it matches the rq type */ | |
4971 | poll->wait.func(&poll->wait, mode, sync, key); | |
4972 | } | |
18bceab1 JA |
4973 | } |
4974 | refcount_dec(&req->refs); | |
4975 | return 1; | |
4976 | } | |
4977 | ||
4978 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4979 | wait_queue_func_t wake_func) | |
4980 | { | |
4981 | poll->head = NULL; | |
4982 | poll->done = false; | |
4983 | poll->canceled = false; | |
4984 | poll->events = events; | |
4985 | INIT_LIST_HEAD(&poll->wait.entry); | |
4986 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4987 | } | |
4988 | ||
4989 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4990 | struct wait_queue_head *head, |
4991 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4992 | { |
4993 | struct io_kiocb *req = pt->req; | |
4994 | ||
4995 | /* | |
4996 | * If poll->head is already set, it's because the file being polled | |
4997 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4998 | * for write). Setup a separate io_poll_iocb if this happens. | |
4999 | */ | |
5000 | if (unlikely(poll->head)) { | |
58852d4d PB |
5001 | struct io_poll_iocb *poll_one = poll; |
5002 | ||
18bceab1 | 5003 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5004 | if (*poll_ptr) { |
18bceab1 JA |
5005 | pt->error = -EINVAL; |
5006 | return; | |
5007 | } | |
5008 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5009 | if (!poll) { | |
5010 | pt->error = -ENOMEM; | |
5011 | return; | |
5012 | } | |
58852d4d | 5013 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5014 | refcount_inc(&req->refs); |
5015 | poll->wait.private = req; | |
807abcb0 | 5016 | *poll_ptr = poll; |
18bceab1 JA |
5017 | } |
5018 | ||
5019 | pt->error = 0; | |
5020 | poll->head = head; | |
a31eb4a2 JX |
5021 | |
5022 | if (poll->events & EPOLLEXCLUSIVE) | |
5023 | add_wait_queue_exclusive(head, &poll->wait); | |
5024 | else | |
5025 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5026 | } |
5027 | ||
5028 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5029 | struct poll_table_struct *p) | |
5030 | { | |
5031 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5032 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5033 | |
807abcb0 | 5034 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5035 | } |
5036 | ||
d7718a9d JA |
5037 | static void io_async_task_func(struct callback_head *cb) |
5038 | { | |
5039 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5040 | struct async_poll *apoll = req->apoll; | |
5041 | struct io_ring_ctx *ctx = req->ctx; | |
5042 | ||
5043 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5044 | ||
74ce6ce4 | 5045 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5046 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5047 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5048 | return; |
d7718a9d JA |
5049 | } |
5050 | ||
31067255 | 5051 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5052 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5053 | hash_del(&req->hash_node); |
2bae047e | 5054 | |
d4e7cd36 | 5055 | io_poll_remove_double(req); |
74ce6ce4 JA |
5056 | spin_unlock_irq(&ctx->completion_lock); |
5057 | ||
0be0b0e3 PB |
5058 | if (!READ_ONCE(apoll->poll.canceled)) |
5059 | __io_req_task_submit(req); | |
5060 | else | |
5061 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5062 | |
6d816e08 | 5063 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5064 | kfree(apoll->double_poll); |
31067255 | 5065 | kfree(apoll); |
d7718a9d JA |
5066 | } |
5067 | ||
5068 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5069 | void *key) | |
5070 | { | |
5071 | struct io_kiocb *req = wait->private; | |
5072 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5073 | ||
5074 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5075 | key_to_poll(key)); | |
5076 | ||
5077 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5078 | } | |
5079 | ||
5080 | static void io_poll_req_insert(struct io_kiocb *req) | |
5081 | { | |
5082 | struct io_ring_ctx *ctx = req->ctx; | |
5083 | struct hlist_head *list; | |
5084 | ||
5085 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5086 | hlist_add_head(&req->hash_node, list); | |
5087 | } | |
5088 | ||
5089 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5090 | struct io_poll_iocb *poll, | |
5091 | struct io_poll_table *ipt, __poll_t mask, | |
5092 | wait_queue_func_t wake_func) | |
5093 | __acquires(&ctx->completion_lock) | |
5094 | { | |
5095 | struct io_ring_ctx *ctx = req->ctx; | |
5096 | bool cancel = false; | |
5097 | ||
4d52f338 | 5098 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5099 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5100 | poll->file = req->file; |
18bceab1 | 5101 | poll->wait.private = req; |
d7718a9d JA |
5102 | |
5103 | ipt->pt._key = mask; | |
5104 | ipt->req = req; | |
5105 | ipt->error = -EINVAL; | |
5106 | ||
d7718a9d JA |
5107 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5108 | ||
5109 | spin_lock_irq(&ctx->completion_lock); | |
5110 | if (likely(poll->head)) { | |
5111 | spin_lock(&poll->head->lock); | |
5112 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5113 | if (ipt->error) | |
5114 | cancel = true; | |
5115 | ipt->error = 0; | |
5116 | mask = 0; | |
5117 | } | |
5118 | if (mask || ipt->error) | |
5119 | list_del_init(&poll->wait.entry); | |
5120 | else if (cancel) | |
5121 | WRITE_ONCE(poll->canceled, true); | |
5122 | else if (!poll->done) /* actually waiting for an event */ | |
5123 | io_poll_req_insert(req); | |
5124 | spin_unlock(&poll->head->lock); | |
5125 | } | |
5126 | ||
5127 | return mask; | |
5128 | } | |
5129 | ||
5130 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5131 | { | |
5132 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5133 | struct io_ring_ctx *ctx = req->ctx; | |
5134 | struct async_poll *apoll; | |
5135 | struct io_poll_table ipt; | |
5136 | __poll_t mask, ret; | |
9dab14b8 | 5137 | int rw; |
d7718a9d JA |
5138 | |
5139 | if (!req->file || !file_can_poll(req->file)) | |
5140 | return false; | |
24c74678 | 5141 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5142 | return false; |
9dab14b8 JA |
5143 | if (def->pollin) |
5144 | rw = READ; | |
5145 | else if (def->pollout) | |
5146 | rw = WRITE; | |
5147 | else | |
5148 | return false; | |
5149 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5150 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5151 | return false; |
5152 | ||
5153 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5154 | if (unlikely(!apoll)) | |
5155 | return false; | |
807abcb0 | 5156 | apoll->double_poll = NULL; |
d7718a9d JA |
5157 | |
5158 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5159 | req->apoll = apoll; |
d7718a9d | 5160 | |
8755d97a | 5161 | mask = 0; |
d7718a9d | 5162 | if (def->pollin) |
8755d97a | 5163 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5164 | if (def->pollout) |
5165 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5166 | |
5167 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5168 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5169 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5170 | mask &= ~POLLIN; | |
5171 | ||
d7718a9d JA |
5172 | mask |= POLLERR | POLLPRI; |
5173 | ||
5174 | ipt.pt._qproc = io_async_queue_proc; | |
5175 | ||
5176 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5177 | io_async_wake); | |
a36da65c | 5178 | if (ret || ipt.error) { |
d4e7cd36 | 5179 | io_poll_remove_double(req); |
d7718a9d | 5180 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5181 | kfree(apoll->double_poll); |
d7718a9d JA |
5182 | kfree(apoll); |
5183 | return false; | |
5184 | } | |
5185 | spin_unlock_irq(&ctx->completion_lock); | |
5186 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5187 | apoll->poll.events); | |
5188 | return true; | |
5189 | } | |
5190 | ||
5191 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5192 | struct io_poll_iocb *poll) | |
221c5eb2 | 5193 | { |
b41e9852 | 5194 | bool do_complete = false; |
221c5eb2 JA |
5195 | |
5196 | spin_lock(&poll->head->lock); | |
5197 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5198 | if (!list_empty(&poll->wait.entry)) { |
5199 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5200 | do_complete = true; |
221c5eb2 JA |
5201 | } |
5202 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5203 | hash_del(&req->hash_node); |
d7718a9d JA |
5204 | return do_complete; |
5205 | } | |
5206 | ||
5207 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5208 | { | |
5209 | bool do_complete; | |
5210 | ||
d4e7cd36 JA |
5211 | io_poll_remove_double(req); |
5212 | ||
d7718a9d JA |
5213 | if (req->opcode == IORING_OP_POLL_ADD) { |
5214 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5215 | } else { | |
3bfa5bcb JA |
5216 | struct async_poll *apoll = req->apoll; |
5217 | ||
d7718a9d | 5218 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5219 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5220 | if (do_complete) { | |
d7718a9d | 5221 | io_put_req(req); |
807abcb0 | 5222 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5223 | kfree(apoll); |
5224 | } | |
b1f573bd XW |
5225 | } |
5226 | ||
b41e9852 JA |
5227 | if (do_complete) { |
5228 | io_cqring_fill_event(req, -ECANCELED); | |
5229 | io_commit_cqring(req->ctx); | |
f254ac04 | 5230 | req_set_fail_links(req); |
216578e5 | 5231 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5232 | } |
5233 | ||
5234 | return do_complete; | |
221c5eb2 JA |
5235 | } |
5236 | ||
76e1b642 JA |
5237 | /* |
5238 | * Returns true if we found and killed one or more poll requests | |
5239 | */ | |
6b81928d PB |
5240 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5241 | struct files_struct *files) | |
221c5eb2 | 5242 | { |
78076bb6 | 5243 | struct hlist_node *tmp; |
221c5eb2 | 5244 | struct io_kiocb *req; |
8e2e1faf | 5245 | int posted = 0, i; |
221c5eb2 JA |
5246 | |
5247 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5248 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5249 | struct hlist_head *list; | |
5250 | ||
5251 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5252 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5253 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5254 | posted += io_poll_remove_one(req); |
5255 | } | |
221c5eb2 JA |
5256 | } |
5257 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5258 | |
8e2e1faf JA |
5259 | if (posted) |
5260 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5261 | |
5262 | return posted != 0; | |
221c5eb2 JA |
5263 | } |
5264 | ||
47f46768 JA |
5265 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5266 | { | |
78076bb6 | 5267 | struct hlist_head *list; |
47f46768 JA |
5268 | struct io_kiocb *req; |
5269 | ||
78076bb6 JA |
5270 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5271 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5272 | if (sqe_addr != req->user_data) |
5273 | continue; | |
5274 | if (io_poll_remove_one(req)) | |
eac406c6 | 5275 | return 0; |
b41e9852 | 5276 | return -EALREADY; |
47f46768 JA |
5277 | } |
5278 | ||
5279 | return -ENOENT; | |
5280 | } | |
5281 | ||
3529d8c2 JA |
5282 | static int io_poll_remove_prep(struct io_kiocb *req, |
5283 | const struct io_uring_sqe *sqe) | |
0969e783 | 5284 | { |
0969e783 JA |
5285 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5286 | return -EINVAL; | |
5287 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5288 | sqe->poll_events) | |
5289 | return -EINVAL; | |
5290 | ||
018043be | 5291 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5292 | return 0; |
5293 | } | |
5294 | ||
221c5eb2 JA |
5295 | /* |
5296 | * Find a running poll command that matches one specified in sqe->addr, | |
5297 | * and remove it if found. | |
5298 | */ | |
61e98203 | 5299 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5300 | { |
5301 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5302 | int ret; |
221c5eb2 | 5303 | |
221c5eb2 | 5304 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5305 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5306 | spin_unlock_irq(&ctx->completion_lock); |
5307 | ||
4e88d6e7 JA |
5308 | if (ret < 0) |
5309 | req_set_fail_links(req); | |
e1e16097 | 5310 | io_req_complete(req, ret); |
221c5eb2 JA |
5311 | return 0; |
5312 | } | |
5313 | ||
221c5eb2 JA |
5314 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5315 | void *key) | |
5316 | { | |
c2f2eb7d JA |
5317 | struct io_kiocb *req = wait->private; |
5318 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5319 | |
d7718a9d | 5320 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5321 | } |
5322 | ||
221c5eb2 JA |
5323 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5324 | struct poll_table_struct *p) | |
5325 | { | |
5326 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5327 | ||
e8c2bc1f | 5328 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5329 | } |
5330 | ||
3529d8c2 | 5331 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5332 | { |
5333 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5334 | u32 events; |
221c5eb2 JA |
5335 | |
5336 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5337 | return -EINVAL; | |
5338 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5339 | return -EINVAL; | |
5340 | ||
5769a351 JX |
5341 | events = READ_ONCE(sqe->poll32_events); |
5342 | #ifdef __BIG_ENDIAN | |
5343 | events = swahw32(events); | |
5344 | #endif | |
a31eb4a2 JX |
5345 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5346 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5347 | return 0; |
5348 | } | |
5349 | ||
61e98203 | 5350 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5351 | { |
5352 | struct io_poll_iocb *poll = &req->poll; | |
5353 | struct io_ring_ctx *ctx = req->ctx; | |
5354 | struct io_poll_table ipt; | |
0969e783 | 5355 | __poll_t mask; |
0969e783 | 5356 | |
d7718a9d | 5357 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5358 | |
d7718a9d JA |
5359 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5360 | io_poll_wake); | |
221c5eb2 | 5361 | |
8c838788 | 5362 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5363 | ipt.error = 0; |
b0dd8a41 | 5364 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5365 | } |
221c5eb2 JA |
5366 | spin_unlock_irq(&ctx->completion_lock); |
5367 | ||
8c838788 JA |
5368 | if (mask) { |
5369 | io_cqring_ev_posted(ctx); | |
014db007 | 5370 | io_put_req(req); |
221c5eb2 | 5371 | } |
8c838788 | 5372 | return ipt.error; |
221c5eb2 JA |
5373 | } |
5374 | ||
5262f567 JA |
5375 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5376 | { | |
ad8a48ac JA |
5377 | struct io_timeout_data *data = container_of(timer, |
5378 | struct io_timeout_data, timer); | |
5379 | struct io_kiocb *req = data->req; | |
5380 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5381 | unsigned long flags; |
5382 | ||
5262f567 | 5383 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5384 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5385 | atomic_set(&req->ctx->cq_timeouts, |
5386 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5387 | ||
78e19bbe | 5388 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5389 | io_commit_cqring(ctx); |
5390 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5391 | ||
5392 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5393 | req_set_fail_links(req); |
5262f567 JA |
5394 | io_put_req(req); |
5395 | return HRTIMER_NORESTART; | |
5396 | } | |
5397 | ||
fbd15848 PB |
5398 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5399 | __u64 user_data) | |
f254ac04 | 5400 | { |
fbd15848 | 5401 | struct io_timeout_data *io; |
47f46768 JA |
5402 | struct io_kiocb *req; |
5403 | int ret = -ENOENT; | |
f254ac04 | 5404 | |
135fcde8 | 5405 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5406 | if (user_data == req->user_data) { |
47f46768 JA |
5407 | ret = 0; |
5408 | break; | |
5409 | } | |
5410 | } | |
5411 | ||
5412 | if (ret == -ENOENT) | |
fbd15848 PB |
5413 | return ERR_PTR(ret); |
5414 | ||
5415 | io = req->async_data; | |
e8c2bc1f | 5416 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5417 | if (ret == -1) |
fbd15848 | 5418 | return ERR_PTR(-EALREADY); |
a71976f3 | 5419 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5420 | return req; |
5421 | } | |
47f46768 | 5422 | |
fbd15848 PB |
5423 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5424 | { | |
5425 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5426 | ||
5427 | if (IS_ERR(req)) | |
5428 | return PTR_ERR(req); | |
f254ac04 JA |
5429 | |
5430 | req_set_fail_links(req); | |
f254ac04 | 5431 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5432 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5433 | return 0; |
5434 | } | |
5435 | ||
9c8e11b3 PB |
5436 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5437 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5438 | { |
9c8e11b3 PB |
5439 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5440 | struct io_timeout_data *data; | |
47f46768 | 5441 | |
9c8e11b3 PB |
5442 | if (IS_ERR(req)) |
5443 | return PTR_ERR(req); | |
47f46768 | 5444 | |
9c8e11b3 PB |
5445 | req->timeout.off = 0; /* noseq */ |
5446 | data = req->async_data; | |
5447 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5448 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5449 | data->timer.function = io_timeout_fn; | |
5450 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5451 | return 0; | |
47f46768 JA |
5452 | } |
5453 | ||
3529d8c2 JA |
5454 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5455 | const struct io_uring_sqe *sqe) | |
b29472ee | 5456 | { |
9c8e11b3 PB |
5457 | struct io_timeout_rem *tr = &req->timeout_rem; |
5458 | ||
b29472ee JA |
5459 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5460 | return -EINVAL; | |
61710e43 DA |
5461 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5462 | return -EINVAL; | |
9c8e11b3 | 5463 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5464 | return -EINVAL; |
5465 | ||
9c8e11b3 PB |
5466 | tr->addr = READ_ONCE(sqe->addr); |
5467 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5468 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5469 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5470 | return -EINVAL; | |
5471 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5472 | return -EFAULT; | |
5473 | } else if (tr->flags) { | |
5474 | /* timeout removal doesn't support flags */ | |
b29472ee | 5475 | return -EINVAL; |
9c8e11b3 | 5476 | } |
b29472ee | 5477 | |
b29472ee JA |
5478 | return 0; |
5479 | } | |
5480 | ||
8662daec PB |
5481 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5482 | { | |
5483 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5484 | : HRTIMER_MODE_REL; | |
5485 | } | |
5486 | ||
11365043 JA |
5487 | /* |
5488 | * Remove or update an existing timeout command | |
5489 | */ | |
61e98203 | 5490 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5491 | { |
9c8e11b3 | 5492 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5493 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5494 | int ret; |
11365043 | 5495 | |
11365043 | 5496 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5497 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5498 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5499 | else |
5500 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5501 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5502 | |
47f46768 | 5503 | io_cqring_fill_event(req, ret); |
11365043 JA |
5504 | io_commit_cqring(ctx); |
5505 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5506 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5507 | if (ret < 0) |
5508 | req_set_fail_links(req); | |
ec9c02ad | 5509 | io_put_req(req); |
11365043 | 5510 | return 0; |
5262f567 JA |
5511 | } |
5512 | ||
3529d8c2 | 5513 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5514 | bool is_timeout_link) |
5262f567 | 5515 | { |
ad8a48ac | 5516 | struct io_timeout_data *data; |
a41525ab | 5517 | unsigned flags; |
56080b02 | 5518 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5519 | |
ad8a48ac | 5520 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5521 | return -EINVAL; |
ad8a48ac | 5522 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5523 | return -EINVAL; |
56080b02 | 5524 | if (off && is_timeout_link) |
2d28390a | 5525 | return -EINVAL; |
a41525ab JA |
5526 | flags = READ_ONCE(sqe->timeout_flags); |
5527 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5528 | return -EINVAL; |
bdf20073 | 5529 | |
bfe68a22 | 5530 | req->timeout.off = off; |
26a61679 | 5531 | |
e8c2bc1f | 5532 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5533 | return -ENOMEM; |
5534 | ||
e8c2bc1f | 5535 | data = req->async_data; |
ad8a48ac | 5536 | data->req = req; |
ad8a48ac JA |
5537 | |
5538 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5539 | return -EFAULT; |
5540 | ||
8662daec | 5541 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5542 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5543 | return 0; | |
5544 | } | |
5545 | ||
61e98203 | 5546 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5547 | { |
ad8a48ac | 5548 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5549 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5550 | struct list_head *entry; |
bfe68a22 | 5551 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5552 | |
733f5c95 | 5553 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5554 | |
5262f567 JA |
5555 | /* |
5556 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5557 | * timeout event to be satisfied. If it isn't set, then this is |
5558 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5559 | */ |
8eb7e2d0 | 5560 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5561 | entry = ctx->timeout_list.prev; |
5562 | goto add; | |
5563 | } | |
5262f567 | 5564 | |
bfe68a22 PB |
5565 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5566 | req->timeout.target_seq = tail + off; | |
5262f567 | 5567 | |
f010505b MDG |
5568 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5569 | * This is safe because ->completion_lock is held, and submissions | |
5570 | * and completions are never mixed in the same ->completion_lock section. | |
5571 | */ | |
5572 | ctx->cq_last_tm_flush = tail; | |
5573 | ||
5262f567 JA |
5574 | /* |
5575 | * Insertion sort, ensuring the first entry in the list is always | |
5576 | * the one we need first. | |
5577 | */ | |
5262f567 | 5578 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5579 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5580 | timeout.list); | |
5262f567 | 5581 | |
8eb7e2d0 | 5582 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5583 | continue; |
bfe68a22 PB |
5584 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5585 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5586 | break; |
5587 | } | |
93bd25bb | 5588 | add: |
135fcde8 | 5589 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5590 | data->timer.function = io_timeout_fn; |
5591 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5592 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5593 | return 0; |
5594 | } | |
5262f567 | 5595 | |
62755e35 JA |
5596 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5597 | { | |
5598 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5599 | ||
5600 | return req->user_data == (unsigned long) data; | |
5601 | } | |
5602 | ||
5aa75ed5 | 5603 | static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr) |
62755e35 | 5604 | { |
62755e35 | 5605 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5606 | int ret = 0; |
5607 | ||
5aa75ed5 JA |
5608 | if (!tctx->io_wq) |
5609 | return -ENOENT; | |
5610 | ||
5611 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false); | |
62755e35 JA |
5612 | switch (cancel_ret) { |
5613 | case IO_WQ_CANCEL_OK: | |
5614 | ret = 0; | |
5615 | break; | |
5616 | case IO_WQ_CANCEL_RUNNING: | |
5617 | ret = -EALREADY; | |
5618 | break; | |
5619 | case IO_WQ_CANCEL_NOTFOUND: | |
5620 | ret = -ENOENT; | |
5621 | break; | |
5622 | } | |
5623 | ||
e977d6d3 JA |
5624 | return ret; |
5625 | } | |
5626 | ||
47f46768 JA |
5627 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5628 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5629 | int success_ret) |
47f46768 JA |
5630 | { |
5631 | unsigned long flags; | |
5632 | int ret; | |
5633 | ||
5aa75ed5 JA |
5634 | ret = io_async_cancel_one(req->task->io_uring, |
5635 | (void *) (unsigned long) sqe_addr); | |
47f46768 JA |
5636 | if (ret != -ENOENT) { |
5637 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5638 | goto done; | |
5639 | } | |
5640 | ||
5641 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5642 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5643 | if (ret != -ENOENT) | |
5644 | goto done; | |
5645 | ret = io_poll_cancel(ctx, sqe_addr); | |
5646 | done: | |
b0dd8a41 JA |
5647 | if (!ret) |
5648 | ret = success_ret; | |
47f46768 JA |
5649 | io_cqring_fill_event(req, ret); |
5650 | io_commit_cqring(ctx); | |
5651 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5652 | io_cqring_ev_posted(ctx); | |
5653 | ||
4e88d6e7 JA |
5654 | if (ret < 0) |
5655 | req_set_fail_links(req); | |
014db007 | 5656 | io_put_req(req); |
47f46768 JA |
5657 | } |
5658 | ||
3529d8c2 JA |
5659 | static int io_async_cancel_prep(struct io_kiocb *req, |
5660 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5661 | { |
fbf23849 | 5662 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5663 | return -EINVAL; |
61710e43 DA |
5664 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5665 | return -EINVAL; | |
5666 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5667 | return -EINVAL; |
5668 | ||
fbf23849 JA |
5669 | req->cancel.addr = READ_ONCE(sqe->addr); |
5670 | return 0; | |
5671 | } | |
5672 | ||
61e98203 | 5673 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5674 | { |
5675 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5676 | |
014db007 | 5677 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5678 | return 0; |
5679 | } | |
5680 | ||
269bbe5f | 5681 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5682 | const struct io_uring_sqe *sqe) |
5683 | { | |
6ca56f84 JA |
5684 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5685 | return -EINVAL; | |
61710e43 DA |
5686 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5687 | return -EINVAL; | |
5688 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5689 | return -EINVAL; |
5690 | ||
269bbe5f BM |
5691 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5692 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5693 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5694 | return -EINVAL; |
269bbe5f | 5695 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5696 | return 0; |
5697 | } | |
5698 | ||
889fca73 | 5699 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5700 | { |
5701 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5702 | struct io_uring_rsrc_update up; |
05f3fb3c | 5703 | int ret; |
fbf23849 | 5704 | |
45d189c6 | 5705 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5706 | return -EAGAIN; |
05f3fb3c | 5707 | |
269bbe5f BM |
5708 | up.offset = req->rsrc_update.offset; |
5709 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5710 | |
5711 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5712 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5713 | mutex_unlock(&ctx->uring_lock); |
5714 | ||
5715 | if (ret < 0) | |
5716 | req_set_fail_links(req); | |
889fca73 | 5717 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5718 | return 0; |
5719 | } | |
5720 | ||
bfe76559 | 5721 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5722 | { |
d625c6ee | 5723 | switch (req->opcode) { |
e781573e | 5724 | case IORING_OP_NOP: |
bfe76559 | 5725 | return 0; |
f67676d1 JA |
5726 | case IORING_OP_READV: |
5727 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5728 | case IORING_OP_READ: |
bfe76559 | 5729 | return io_read_prep(req, sqe); |
f67676d1 JA |
5730 | case IORING_OP_WRITEV: |
5731 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5732 | case IORING_OP_WRITE: |
bfe76559 | 5733 | return io_write_prep(req, sqe); |
0969e783 | 5734 | case IORING_OP_POLL_ADD: |
bfe76559 | 5735 | return io_poll_add_prep(req, sqe); |
0969e783 | 5736 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5737 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5738 | case IORING_OP_FSYNC: |
1155c76a | 5739 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5740 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5741 | return io_sfr_prep(req, sqe); |
03b1230c | 5742 | case IORING_OP_SENDMSG: |
fddaface | 5743 | case IORING_OP_SEND: |
bfe76559 | 5744 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5745 | case IORING_OP_RECVMSG: |
fddaface | 5746 | case IORING_OP_RECV: |
bfe76559 | 5747 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5748 | case IORING_OP_CONNECT: |
bfe76559 | 5749 | return io_connect_prep(req, sqe); |
2d28390a | 5750 | case IORING_OP_TIMEOUT: |
bfe76559 | 5751 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5752 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5753 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5754 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5755 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5756 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5757 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5758 | case IORING_OP_ACCEPT: |
bfe76559 | 5759 | return io_accept_prep(req, sqe); |
d63d1b5e | 5760 | case IORING_OP_FALLOCATE: |
bfe76559 | 5761 | return io_fallocate_prep(req, sqe); |
15b71abe | 5762 | case IORING_OP_OPENAT: |
bfe76559 | 5763 | return io_openat_prep(req, sqe); |
b5dba59e | 5764 | case IORING_OP_CLOSE: |
bfe76559 | 5765 | return io_close_prep(req, sqe); |
05f3fb3c | 5766 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5767 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5768 | case IORING_OP_STATX: |
bfe76559 | 5769 | return io_statx_prep(req, sqe); |
4840e418 | 5770 | case IORING_OP_FADVISE: |
bfe76559 | 5771 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5772 | case IORING_OP_MADVISE: |
bfe76559 | 5773 | return io_madvise_prep(req, sqe); |
cebdb986 | 5774 | case IORING_OP_OPENAT2: |
bfe76559 | 5775 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5776 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5777 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5778 | case IORING_OP_SPLICE: |
bfe76559 | 5779 | return io_splice_prep(req, sqe); |
ddf0322d | 5780 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5781 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5782 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5783 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5784 | case IORING_OP_TEE: |
bfe76559 | 5785 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5786 | case IORING_OP_SHUTDOWN: |
5787 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5788 | case IORING_OP_RENAMEAT: |
5789 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5790 | case IORING_OP_UNLINKAT: |
5791 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5792 | } |
5793 | ||
bfe76559 PB |
5794 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5795 | req->opcode); | |
5796 | return-EINVAL; | |
5797 | } | |
5798 | ||
93642ef8 | 5799 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5800 | { |
93642ef8 PB |
5801 | switch (req->opcode) { |
5802 | case IORING_OP_READV: | |
5803 | case IORING_OP_READ_FIXED: | |
5804 | case IORING_OP_READ: | |
5805 | return io_rw_prep_async(req, READ); | |
5806 | case IORING_OP_WRITEV: | |
5807 | case IORING_OP_WRITE_FIXED: | |
5808 | case IORING_OP_WRITE: | |
5809 | return io_rw_prep_async(req, WRITE); | |
5810 | case IORING_OP_SENDMSG: | |
5811 | case IORING_OP_SEND: | |
5812 | return io_sendmsg_prep_async(req); | |
5813 | case IORING_OP_RECVMSG: | |
5814 | case IORING_OP_RECV: | |
5815 | return io_recvmsg_prep_async(req); | |
5816 | case IORING_OP_CONNECT: | |
5817 | return io_connect_prep_async(req); | |
5818 | } | |
5819 | return 0; | |
5820 | } | |
5821 | ||
be7053b7 | 5822 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 5823 | { |
be7053b7 | 5824 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 5825 | return 0; |
be7053b7 | 5826 | /* some opcodes init it during the inital prep */ |
93642ef8 | 5827 | if (req->async_data) |
be7053b7 PB |
5828 | return 0; |
5829 | if (__io_alloc_async_data(req)) | |
bfe76559 | 5830 | return -EAGAIN; |
be7053b7 | 5831 | return io_req_prep_async(req); |
f67676d1 JA |
5832 | } |
5833 | ||
9cf7c104 PB |
5834 | static u32 io_get_sequence(struct io_kiocb *req) |
5835 | { | |
5836 | struct io_kiocb *pos; | |
5837 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 5838 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 5839 | |
f2f87370 PB |
5840 | io_for_each_link(pos, req) |
5841 | nr_reqs++; | |
9cf7c104 PB |
5842 | |
5843 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5844 | return total_submitted - nr_reqs; | |
5845 | } | |
5846 | ||
be7053b7 | 5847 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 5848 | { |
a197f664 | 5849 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5850 | struct io_defer_entry *de; |
f67676d1 | 5851 | int ret; |
9cf7c104 | 5852 | u32 seq; |
de0617e4 | 5853 | |
9d858b21 | 5854 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5855 | if (likely(list_empty_careful(&ctx->defer_list) && |
5856 | !(req->flags & REQ_F_IO_DRAIN))) | |
5857 | return 0; | |
5858 | ||
5859 | seq = io_get_sequence(req); | |
5860 | /* Still a chance to pass the sequence check */ | |
5861 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5862 | return 0; |
5863 | ||
be7053b7 PB |
5864 | ret = io_req_defer_prep(req); |
5865 | if (ret) | |
5866 | return ret; | |
cbdcb435 | 5867 | io_prep_async_link(req); |
27dc8338 PB |
5868 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5869 | if (!de) | |
5870 | return -ENOMEM; | |
2d28390a | 5871 | |
de0617e4 | 5872 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5873 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5874 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5875 | kfree(de); |
ae34817b PB |
5876 | io_queue_async_work(req); |
5877 | return -EIOCBQUEUED; | |
de0617e4 JA |
5878 | } |
5879 | ||
915967f6 | 5880 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5881 | de->req = req; |
9cf7c104 | 5882 | de->seq = seq; |
27dc8338 | 5883 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5884 | spin_unlock_irq(&ctx->completion_lock); |
5885 | return -EIOCBQUEUED; | |
5886 | } | |
5887 | ||
3ca405eb | 5888 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5889 | { |
0e1b6fe3 PB |
5890 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5891 | switch (req->opcode) { | |
5892 | case IORING_OP_READV: | |
5893 | case IORING_OP_READ_FIXED: | |
5894 | case IORING_OP_READ: | |
bcda7baa | 5895 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5896 | break; |
5897 | case IORING_OP_RECVMSG: | |
5898 | case IORING_OP_RECV: | |
bcda7baa | 5899 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5900 | break; |
5901 | } | |
5902 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5903 | } |
5904 | ||
0e1b6fe3 PB |
5905 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5906 | switch (req->opcode) { | |
5907 | case IORING_OP_READV: | |
5908 | case IORING_OP_READ_FIXED: | |
5909 | case IORING_OP_READ: | |
5910 | case IORING_OP_WRITEV: | |
5911 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5912 | case IORING_OP_WRITE: { |
5913 | struct io_async_rw *io = req->async_data; | |
5914 | if (io->free_iovec) | |
5915 | kfree(io->free_iovec); | |
0e1b6fe3 | 5916 | break; |
e8c2bc1f | 5917 | } |
0e1b6fe3 | 5918 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5919 | case IORING_OP_SENDMSG: { |
5920 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
5921 | |
5922 | kfree(io->free_iov); | |
0e1b6fe3 | 5923 | break; |
e8c2bc1f | 5924 | } |
0e1b6fe3 PB |
5925 | case IORING_OP_SPLICE: |
5926 | case IORING_OP_TEE: | |
5927 | io_put_file(req, req->splice.file_in, | |
5928 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5929 | break; | |
f3cd4850 JA |
5930 | case IORING_OP_OPENAT: |
5931 | case IORING_OP_OPENAT2: | |
5932 | if (req->open.filename) | |
5933 | putname(req->open.filename); | |
5934 | break; | |
80a261fd JA |
5935 | case IORING_OP_RENAMEAT: |
5936 | putname(req->rename.oldpath); | |
5937 | putname(req->rename.newpath); | |
5938 | break; | |
14a1143b JA |
5939 | case IORING_OP_UNLINKAT: |
5940 | putname(req->unlink.filename); | |
5941 | break; | |
0e1b6fe3 PB |
5942 | } |
5943 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5944 | } |
99bc4c38 PB |
5945 | } |
5946 | ||
889fca73 | 5947 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 5948 | { |
a197f664 | 5949 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 5950 | int ret; |
2b188cc1 | 5951 | |
d625c6ee | 5952 | switch (req->opcode) { |
2b188cc1 | 5953 | case IORING_OP_NOP: |
889fca73 | 5954 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
5955 | break; |
5956 | case IORING_OP_READV: | |
edafccee | 5957 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5958 | case IORING_OP_READ: |
889fca73 | 5959 | ret = io_read(req, issue_flags); |
edafccee | 5960 | break; |
3529d8c2 | 5961 | case IORING_OP_WRITEV: |
edafccee | 5962 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5963 | case IORING_OP_WRITE: |
889fca73 | 5964 | ret = io_write(req, issue_flags); |
2b188cc1 | 5965 | break; |
c992fe29 | 5966 | case IORING_OP_FSYNC: |
45d189c6 | 5967 | ret = io_fsync(req, issue_flags); |
c992fe29 | 5968 | break; |
221c5eb2 | 5969 | case IORING_OP_POLL_ADD: |
61e98203 | 5970 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
5971 | break; |
5972 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 5973 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 5974 | break; |
5d17b4a4 | 5975 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 5976 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 5977 | break; |
0fa03c62 | 5978 | case IORING_OP_SENDMSG: |
889fca73 | 5979 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 5980 | break; |
fddaface | 5981 | case IORING_OP_SEND: |
889fca73 | 5982 | ret = io_send(req, issue_flags); |
0fa03c62 | 5983 | break; |
aa1fa28f | 5984 | case IORING_OP_RECVMSG: |
889fca73 | 5985 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 5986 | break; |
fddaface | 5987 | case IORING_OP_RECV: |
889fca73 | 5988 | ret = io_recv(req, issue_flags); |
aa1fa28f | 5989 | break; |
5262f567 | 5990 | case IORING_OP_TIMEOUT: |
61e98203 | 5991 | ret = io_timeout(req, issue_flags); |
5262f567 | 5992 | break; |
11365043 | 5993 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 5994 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 5995 | break; |
17f2fe35 | 5996 | case IORING_OP_ACCEPT: |
889fca73 | 5997 | ret = io_accept(req, issue_flags); |
17f2fe35 | 5998 | break; |
f8e85cf2 | 5999 | case IORING_OP_CONNECT: |
889fca73 | 6000 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6001 | break; |
62755e35 | 6002 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6003 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6004 | break; |
d63d1b5e | 6005 | case IORING_OP_FALLOCATE: |
45d189c6 | 6006 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6007 | break; |
15b71abe | 6008 | case IORING_OP_OPENAT: |
45d189c6 | 6009 | ret = io_openat(req, issue_flags); |
15b71abe | 6010 | break; |
b5dba59e | 6011 | case IORING_OP_CLOSE: |
889fca73 | 6012 | ret = io_close(req, issue_flags); |
b5dba59e | 6013 | break; |
05f3fb3c | 6014 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6015 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6016 | break; |
eddc7ef5 | 6017 | case IORING_OP_STATX: |
45d189c6 | 6018 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6019 | break; |
4840e418 | 6020 | case IORING_OP_FADVISE: |
45d189c6 | 6021 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6022 | break; |
c1ca757b | 6023 | case IORING_OP_MADVISE: |
45d189c6 | 6024 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6025 | break; |
cebdb986 | 6026 | case IORING_OP_OPENAT2: |
45d189c6 | 6027 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6028 | break; |
3e4827b0 | 6029 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6030 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6031 | break; |
7d67af2c | 6032 | case IORING_OP_SPLICE: |
45d189c6 | 6033 | ret = io_splice(req, issue_flags); |
7d67af2c | 6034 | break; |
ddf0322d | 6035 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6036 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6037 | break; |
067524e9 | 6038 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6039 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6040 | break; |
f2a8d5c7 | 6041 | case IORING_OP_TEE: |
45d189c6 | 6042 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6043 | break; |
36f4fa68 | 6044 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6045 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6046 | break; |
80a261fd | 6047 | case IORING_OP_RENAMEAT: |
45d189c6 | 6048 | ret = io_renameat(req, issue_flags); |
80a261fd | 6049 | break; |
14a1143b | 6050 | case IORING_OP_UNLINKAT: |
45d189c6 | 6051 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6052 | break; |
2b188cc1 JA |
6053 | default: |
6054 | ret = -EINVAL; | |
6055 | break; | |
6056 | } | |
6057 | ||
def596e9 JA |
6058 | if (ret) |
6059 | return ret; | |
6060 | ||
b532576e JA |
6061 | /* If the op doesn't have a file, we're not polling for it */ |
6062 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6063 | const bool in_async = io_wq_current_is_worker(); |
6064 | ||
11ba820b JA |
6065 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6066 | if (in_async) | |
6067 | mutex_lock(&ctx->uring_lock); | |
6068 | ||
2e9dbe90 | 6069 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6070 | |
6071 | if (in_async) | |
6072 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6073 | } |
6074 | ||
6075 | return 0; | |
2b188cc1 JA |
6076 | } |
6077 | ||
5280f7e5 | 6078 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6079 | { |
6080 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6081 | struct io_kiocb *timeout; |
561fb04a | 6082 | int ret = 0; |
2b188cc1 | 6083 | |
6df1db6b PB |
6084 | timeout = io_prep_linked_timeout(req); |
6085 | if (timeout) | |
6086 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6087 | |
4014d943 | 6088 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6089 | ret = -ECANCELED; |
31b51510 | 6090 | |
561fb04a | 6091 | if (!ret) { |
561fb04a | 6092 | do { |
889fca73 | 6093 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6094 | /* |
6095 | * We can get EAGAIN for polled IO even though we're | |
6096 | * forcing a sync submission from here, since we can't | |
6097 | * wait for request slots on the block side. | |
6098 | */ | |
6099 | if (ret != -EAGAIN) | |
6100 | break; | |
6101 | cond_resched(); | |
6102 | } while (1); | |
6103 | } | |
31b51510 | 6104 | |
a3df7698 | 6105 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6106 | if (ret) { |
a3df7698 PB |
6107 | /* io-wq is going to take one down */ |
6108 | refcount_inc(&req->refs); | |
6109 | io_req_task_queue_fail(req, ret); | |
edafccee | 6110 | } |
2b188cc1 JA |
6111 | } |
6112 | ||
65e19f54 JA |
6113 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6114 | int index) | |
6115 | { | |
269bbe5f | 6116 | struct fixed_rsrc_table *table; |
65e19f54 | 6117 | |
05f3fb3c | 6118 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6119 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6120 | } |
6121 | ||
8371adf5 PB |
6122 | static struct file *io_file_get(struct io_submit_state *state, |
6123 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6124 | { |
a197f664 | 6125 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6126 | struct file *file; |
09bb8394 | 6127 | |
8da11c19 | 6128 | if (fixed) { |
479f517b | 6129 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6130 | return NULL; |
b7620121 | 6131 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6132 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6133 | io_set_resource_node(req); |
09bb8394 | 6134 | } else { |
c826bd7a | 6135 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6136 | file = __io_file_get(state, fd); |
09bb8394 JA |
6137 | } |
6138 | ||
ce3d5aae PB |
6139 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6140 | io_req_track_inflight(req); | |
8371adf5 | 6141 | return file; |
09bb8394 JA |
6142 | } |
6143 | ||
2665abfd | 6144 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6145 | { |
ad8a48ac JA |
6146 | struct io_timeout_data *data = container_of(timer, |
6147 | struct io_timeout_data, timer); | |
90cd7e42 | 6148 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6149 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6150 | unsigned long flags; |
2665abfd JA |
6151 | |
6152 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6153 | prev = req->timeout.head; |
6154 | req->timeout.head = NULL; | |
2665abfd JA |
6155 | |
6156 | /* | |
6157 | * We don't expect the list to be empty, that will only happen if we | |
6158 | * race with the completion of the linked work. | |
6159 | */ | |
90cd7e42 | 6160 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6161 | io_remove_next_linked(prev); |
90cd7e42 PB |
6162 | else |
6163 | prev = NULL; | |
2665abfd JA |
6164 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6165 | ||
6166 | if (prev) { | |
4e88d6e7 | 6167 | req_set_fail_links(prev); |
014db007 | 6168 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6169 | io_put_req_deferred(prev, 1); |
47f46768 | 6170 | } else { |
9ae1f8dd PB |
6171 | io_req_complete_post(req, -ETIME, 0); |
6172 | io_put_req_deferred(req, 1); | |
2665abfd | 6173 | } |
2665abfd JA |
6174 | return HRTIMER_NORESTART; |
6175 | } | |
6176 | ||
7271ef3a | 6177 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6178 | { |
76a46e06 | 6179 | /* |
f2f87370 PB |
6180 | * If the back reference is NULL, then our linked request finished |
6181 | * before we got a chance to setup the timer | |
76a46e06 | 6182 | */ |
90cd7e42 | 6183 | if (req->timeout.head) { |
e8c2bc1f | 6184 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6185 | |
ad8a48ac JA |
6186 | data->timer.function = io_link_timeout_fn; |
6187 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6188 | data->mode); | |
2665abfd | 6189 | } |
7271ef3a JA |
6190 | } |
6191 | ||
6192 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6193 | { | |
6194 | struct io_ring_ctx *ctx = req->ctx; | |
6195 | ||
6196 | spin_lock_irq(&ctx->completion_lock); | |
6197 | __io_queue_linked_timeout(req); | |
76a46e06 | 6198 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6199 | |
2665abfd | 6200 | /* drop submission reference */ |
76a46e06 JA |
6201 | io_put_req(req); |
6202 | } | |
2665abfd | 6203 | |
ad8a48ac | 6204 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6205 | { |
f2f87370 | 6206 | struct io_kiocb *nxt = req->link; |
2665abfd | 6207 | |
f2f87370 PB |
6208 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6209 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6210 | return NULL; |
2665abfd | 6211 | |
90cd7e42 | 6212 | nxt->timeout.head = req; |
900fad45 | 6213 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6214 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6215 | return nxt; |
2665abfd JA |
6216 | } |
6217 | ||
c5eef2b9 | 6218 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6219 | { |
d3d7298d | 6220 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
193155c8 | 6221 | const struct cred *old_creds = NULL; |
e0c5c576 | 6222 | int ret; |
2b188cc1 | 6223 | |
4379bf8b JA |
6224 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
6225 | req->work.creds != current_cred()) | |
6226 | old_creds = override_creds(req->work.creds); | |
193155c8 | 6227 | |
c5eef2b9 | 6228 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6229 | |
d3d7298d PB |
6230 | if (old_creds) |
6231 | revert_creds(old_creds); | |
491381ce JA |
6232 | |
6233 | /* | |
6234 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6235 | * doesn't support non-blocking read/write attempts | |
6236 | */ | |
24c74678 | 6237 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6238 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6239 | /* |
6240 | * Queued up for async execution, worker will release | |
6241 | * submit reference when the iocb is actually submitted. | |
6242 | */ | |
6243 | io_queue_async_work(req); | |
2b188cc1 | 6244 | } |
0d63c148 PB |
6245 | } else if (likely(!ret)) { |
6246 | /* drop submission reference */ | |
e342c807 | 6247 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6248 | struct io_ring_ctx *ctx = req->ctx; |
6249 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6250 | |
6dd0be1e | 6251 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6252 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6253 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6254 | } else { |
d3d7298d | 6255 | io_put_req(req); |
0d63c148 PB |
6256 | } |
6257 | } else { | |
4e88d6e7 | 6258 | req_set_fail_links(req); |
e65ef56d | 6259 | io_put_req(req); |
e1e16097 | 6260 | io_req_complete(req, ret); |
9e645e11 | 6261 | } |
d3d7298d PB |
6262 | if (linked_timeout) |
6263 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6264 | } |
6265 | ||
be7053b7 | 6266 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6267 | { |
6268 | int ret; | |
6269 | ||
be7053b7 | 6270 | ret = io_req_defer(req); |
4fe2c963 JL |
6271 | if (ret) { |
6272 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6273 | fail_req: |
4e88d6e7 | 6274 | req_set_fail_links(req); |
e1e16097 JA |
6275 | io_put_req(req); |
6276 | io_req_complete(req, ret); | |
4fe2c963 | 6277 | } |
2550878f | 6278 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6279 | ret = io_req_defer_prep(req); |
6280 | if (unlikely(ret)) | |
6281 | goto fail_req; | |
ce35a47a JA |
6282 | io_queue_async_work(req); |
6283 | } else { | |
c5eef2b9 | 6284 | __io_queue_sqe(req); |
ce35a47a | 6285 | } |
4fe2c963 JL |
6286 | } |
6287 | ||
b16fed66 PB |
6288 | /* |
6289 | * Check SQE restrictions (opcode and flags). | |
6290 | * | |
6291 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6292 | */ | |
6293 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6294 | struct io_kiocb *req, | |
6295 | unsigned int sqe_flags) | |
4fe2c963 | 6296 | { |
b16fed66 PB |
6297 | if (!ctx->restricted) |
6298 | return true; | |
6299 | ||
6300 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6301 | return false; | |
6302 | ||
6303 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6304 | ctx->restrictions.sqe_flags_required) | |
6305 | return false; | |
6306 | ||
6307 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6308 | ctx->restrictions.sqe_flags_required)) | |
6309 | return false; | |
6310 | ||
6311 | return true; | |
4fe2c963 JL |
6312 | } |
6313 | ||
b16fed66 PB |
6314 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6315 | const struct io_uring_sqe *sqe) | |
6316 | { | |
6317 | struct io_submit_state *state; | |
6318 | unsigned int sqe_flags; | |
6319 | int id, ret = 0; | |
6320 | ||
6321 | req->opcode = READ_ONCE(sqe->opcode); | |
6322 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6323 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6324 | req->user_data = READ_ONCE(sqe->user_data); | |
6325 | req->async_data = NULL; | |
6326 | req->file = NULL; | |
6327 | req->ctx = ctx; | |
6328 | req->link = NULL; | |
6329 | req->fixed_rsrc_refs = NULL; | |
6330 | /* one is dropped after submission, the other at completion */ | |
6331 | refcount_set(&req->refs, 2); | |
6332 | req->task = current; | |
6333 | req->result = 0; | |
863e0560 | 6334 | |
b16fed66 | 6335 | /* enforce forwards compatibility on users */ |
ebf4a5db PB |
6336 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6337 | req->flags = 0; | |
b16fed66 | 6338 | return -EINVAL; |
ebf4a5db | 6339 | } |
b16fed66 PB |
6340 | |
6341 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6342 | return -EINVAL; | |
6343 | ||
b16fed66 PB |
6344 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6345 | return -EACCES; | |
6346 | ||
6347 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6348 | !io_op_defs[req->opcode].buffer_select) | |
6349 | return -EOPNOTSUPP; | |
6350 | ||
6351 | id = READ_ONCE(sqe->personality); | |
6352 | if (id) { | |
b16fed66 | 6353 | __io_req_init_async(req); |
4379bf8b JA |
6354 | req->work.creds = idr_find(&ctx->personality_idr, id); |
6355 | if (unlikely(!req->work.creds)) | |
6356 | return -EINVAL; | |
6357 | get_cred(req->work.creds); | |
b16fed66 PB |
6358 | } |
6359 | ||
6360 | state = &ctx->submit_state; | |
6361 | ||
6362 | /* | |
6363 | * Plug now if we have more than 1 IO left after this, and the target | |
6364 | * is potentially a read/write to block based storage. | |
6365 | */ | |
6366 | if (!state->plug_started && state->ios_left > 1 && | |
6367 | io_op_defs[req->opcode].plug) { | |
6368 | blk_start_plug(&state->plug); | |
6369 | state->plug_started = true; | |
6370 | } | |
6371 | ||
6372 | if (io_op_defs[req->opcode].needs_file) { | |
6373 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6374 | ||
6375 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6376 | if (unlikely(!req->file)) | |
6377 | ret = -EBADF; | |
6378 | } | |
6379 | ||
6380 | state->ios_left--; | |
6381 | return ret; | |
6382 | } | |
6383 | ||
a6b8cadc | 6384 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6385 | const struct io_uring_sqe *sqe) |
9e645e11 | 6386 | { |
a1ab7b35 | 6387 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6388 | int ret; |
9e645e11 | 6389 | |
a6b8cadc PB |
6390 | ret = io_init_req(ctx, req, sqe); |
6391 | if (unlikely(ret)) { | |
6392 | fail_req: | |
6393 | io_put_req(req); | |
6394 | io_req_complete(req, ret); | |
de59bc10 PB |
6395 | if (link->head) { |
6396 | /* fail even hard links since we don't submit */ | |
cf109604 | 6397 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6398 | io_put_req(link->head); |
6399 | io_req_complete(link->head, -ECANCELED); | |
6400 | link->head = NULL; | |
6401 | } | |
a6b8cadc PB |
6402 | return ret; |
6403 | } | |
be7053b7 PB |
6404 | ret = io_req_prep(req, sqe); |
6405 | if (unlikely(ret)) | |
6406 | goto fail_req; | |
a6b8cadc | 6407 | |
be7053b7 | 6408 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6409 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6410 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6411 | ||
9e645e11 JA |
6412 | /* |
6413 | * If we already have a head request, queue this one for async | |
6414 | * submittal once the head completes. If we don't have a head but | |
6415 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6416 | * submitted sync once the chain is complete. If none of those | |
6417 | * conditions are true (normal request), then just queue it. | |
6418 | */ | |
863e0560 PB |
6419 | if (link->head) { |
6420 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6421 | |
8cdf2193 PB |
6422 | /* |
6423 | * Taking sequential execution of a link, draining both sides | |
6424 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6425 | * requests in the link. So, it drains the head and the | |
6426 | * next after the link request. The last one is done via | |
6427 | * drain_next flag to persist the effect across calls. | |
6428 | */ | |
ef4ff581 | 6429 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6430 | head->flags |= REQ_F_IO_DRAIN; |
6431 | ctx->drain_next = 1; | |
6432 | } | |
be7053b7 | 6433 | ret = io_req_defer_prep(req); |
cf109604 | 6434 | if (unlikely(ret)) |
a6b8cadc | 6435 | goto fail_req; |
9d76377f | 6436 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6437 | link->last->link = req; |
863e0560 | 6438 | link->last = req; |
32fe525b PB |
6439 | |
6440 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6441 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6442 | io_queue_sqe(head); |
863e0560 | 6443 | link->head = NULL; |
32fe525b | 6444 | } |
9e645e11 | 6445 | } else { |
711be031 PB |
6446 | if (unlikely(ctx->drain_next)) { |
6447 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6448 | ctx->drain_next = 0; |
711be031 | 6449 | } |
ef4ff581 | 6450 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6451 | link->head = req; |
6452 | link->last = req; | |
711be031 | 6453 | } else { |
be7053b7 | 6454 | io_queue_sqe(req); |
711be031 | 6455 | } |
9e645e11 | 6456 | } |
2e6e1fde | 6457 | |
1d4240cc | 6458 | return 0; |
9e645e11 JA |
6459 | } |
6460 | ||
9a56a232 JA |
6461 | /* |
6462 | * Batched submission is done, ensure local IO is flushed out. | |
6463 | */ | |
ba88ff11 PB |
6464 | static void io_submit_state_end(struct io_submit_state *state, |
6465 | struct io_ring_ctx *ctx) | |
9a56a232 | 6466 | { |
a1ab7b35 | 6467 | if (state->link.head) |
de59bc10 | 6468 | io_queue_sqe(state->link.head); |
6dd0be1e | 6469 | if (state->comp.nr) |
ba88ff11 | 6470 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6471 | if (state->plug_started) |
6472 | blk_finish_plug(&state->plug); | |
9f13c35b | 6473 | io_state_file_put(state); |
9a56a232 JA |
6474 | } |
6475 | ||
6476 | /* | |
6477 | * Start submission side cache. | |
6478 | */ | |
6479 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6480 | unsigned int max_ios) |
9a56a232 | 6481 | { |
27926b68 | 6482 | state->plug_started = false; |
9a56a232 | 6483 | state->ios_left = max_ios; |
a1ab7b35 PB |
6484 | /* set only head, no need to init link_last in advance */ |
6485 | state->link.head = NULL; | |
9a56a232 JA |
6486 | } |
6487 | ||
2b188cc1 JA |
6488 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6489 | { | |
75b28aff | 6490 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6491 | |
caf582c6 PB |
6492 | /* |
6493 | * Ensure any loads from the SQEs are done at this point, | |
6494 | * since once we write the new head, the application could | |
6495 | * write new data to them. | |
6496 | */ | |
6497 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6498 | } |
6499 | ||
2b188cc1 | 6500 | /* |
3529d8c2 | 6501 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6502 | * that is mapped by userspace. This means that care needs to be taken to |
6503 | * ensure that reads are stable, as we cannot rely on userspace always | |
6504 | * being a good citizen. If members of the sqe are validated and then later | |
6505 | * used, it's important that those reads are done through READ_ONCE() to | |
6506 | * prevent a re-load down the line. | |
6507 | */ | |
709b302f | 6508 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6509 | { |
75b28aff | 6510 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6511 | unsigned head; |
6512 | ||
6513 | /* | |
6514 | * The cached sq head (or cq tail) serves two purposes: | |
6515 | * | |
6516 | * 1) allows us to batch the cost of updating the user visible | |
6517 | * head updates. | |
6518 | * 2) allows the kernel side to track the head on its own, even | |
6519 | * though the application is the one updating it. | |
6520 | */ | |
4fccfcbb | 6521 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6522 | if (likely(head < ctx->sq_entries)) |
6523 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6524 | |
6525 | /* drop invalid entries */ | |
498ccd9e | 6526 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6527 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6528 | return NULL; |
6529 | } | |
6530 | ||
0f212204 | 6531 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6532 | { |
46c4e16a | 6533 | int submitted = 0; |
6c271ce2 | 6534 | |
c4a2ed72 | 6535 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6536 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6537 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6538 | return -EBUSY; |
6539 | } | |
6c271ce2 | 6540 | |
ee7d46d9 PB |
6541 | /* make sure SQ entry isn't read before tail */ |
6542 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6543 | |
2b85edfc PB |
6544 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6545 | return -EAGAIN; | |
6c271ce2 | 6546 | |
d8a6df10 | 6547 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6548 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6549 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6550 | |
46c4e16a | 6551 | while (submitted < nr) { |
3529d8c2 | 6552 | const struct io_uring_sqe *sqe; |
196be95c | 6553 | struct io_kiocb *req; |
fb5ccc98 | 6554 | |
258b29a9 | 6555 | req = io_alloc_req(ctx); |
196be95c PB |
6556 | if (unlikely(!req)) { |
6557 | if (!submitted) | |
6558 | submitted = -EAGAIN; | |
fb5ccc98 | 6559 | break; |
196be95c | 6560 | } |
4fccfcbb PB |
6561 | sqe = io_get_sqe(ctx); |
6562 | if (unlikely(!sqe)) { | |
6563 | kmem_cache_free(req_cachep, req); | |
6564 | break; | |
6565 | } | |
d3656344 JA |
6566 | /* will complete beyond this point, count as submitted */ |
6567 | submitted++; | |
a1ab7b35 | 6568 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6569 | break; |
6c271ce2 JA |
6570 | } |
6571 | ||
9466f437 PB |
6572 | if (unlikely(submitted != nr)) { |
6573 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6574 | struct io_uring_task *tctx = current->io_uring; |
6575 | int unused = nr - ref_used; | |
9466f437 | 6576 | |
d8a6df10 JA |
6577 | percpu_ref_put_many(&ctx->refs, unused); |
6578 | percpu_counter_sub(&tctx->inflight, unused); | |
6579 | put_task_struct_many(current, unused); | |
9466f437 | 6580 | } |
6c271ce2 | 6581 | |
a1ab7b35 | 6582 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6583 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6584 | io_commit_sqring(ctx); | |
6585 | ||
6c271ce2 JA |
6586 | return submitted; |
6587 | } | |
6588 | ||
23b3628e XW |
6589 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6590 | { | |
6591 | /* Tell userspace we may need a wakeup call */ | |
6592 | spin_lock_irq(&ctx->completion_lock); | |
6593 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6594 | spin_unlock_irq(&ctx->completion_lock); | |
6595 | } | |
6596 | ||
6597 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6598 | { | |
6599 | spin_lock_irq(&ctx->completion_lock); | |
6600 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6601 | spin_unlock_irq(&ctx->completion_lock); | |
6602 | } | |
6603 | ||
08369246 | 6604 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6605 | { |
c8d1ba58 | 6606 | unsigned int to_submit; |
bdcd3eab | 6607 | int ret = 0; |
6c271ce2 | 6608 | |
c8d1ba58 | 6609 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6610 | /* if we're handling multiple rings, cap submit size for fairness */ |
6611 | if (cap_entries && to_submit > 8) | |
6612 | to_submit = 8; | |
6613 | ||
906a3c6f | 6614 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6615 | unsigned nr_events = 0; |
a4c0b3de | 6616 | |
c8d1ba58 | 6617 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6618 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6619 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6620 | |
d9d05217 PB |
6621 | if (to_submit && !ctx->sqo_dead && |
6622 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6623 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6624 | mutex_unlock(&ctx->uring_lock); |
6625 | } | |
6c271ce2 | 6626 | |
90554200 JA |
6627 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6628 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6629 | |
08369246 XW |
6630 | return ret; |
6631 | } | |
6c271ce2 | 6632 | |
08369246 XW |
6633 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6634 | { | |
6635 | struct io_ring_ctx *ctx; | |
6636 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6637 | |
08369246 XW |
6638 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6639 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6640 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6641 | } |
c1edbf5f | 6642 | |
08369246 | 6643 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6644 | } |
6c271ce2 | 6645 | |
69fb2131 JA |
6646 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6647 | { | |
6648 | struct io_ring_ctx *ctx; | |
6649 | ||
6650 | while (!list_empty(&sqd->ctx_new_list)) { | |
6651 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6652 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6653 | complete(&ctx->sq_thread_comp); | |
6654 | } | |
08369246 XW |
6655 | |
6656 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6657 | } |
6658 | ||
37d1e2e3 JA |
6659 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
6660 | { | |
6661 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
6662 | } | |
6663 | ||
6664 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) | |
6665 | { | |
6666 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
6667 | } | |
6668 | ||
6669 | static void io_sq_thread_parkme(struct io_sq_data *sqd) | |
6670 | { | |
6671 | for (;;) { | |
6672 | /* | |
6673 | * TASK_PARKED is a special state; we must serialize against | |
6674 | * possible pending wakeups to avoid store-store collisions on | |
6675 | * task->state. | |
6676 | * | |
6677 | * Such a collision might possibly result in the task state | |
6678 | * changin from TASK_PARKED and us failing the | |
6679 | * wait_task_inactive() in kthread_park(). | |
6680 | */ | |
6681 | set_special_state(TASK_PARKED); | |
6682 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) | |
6683 | break; | |
6684 | ||
6685 | /* | |
6686 | * Thread is going to call schedule(), do not preempt it, | |
6687 | * or the caller of kthread_park() may spend more time in | |
6688 | * wait_task_inactive(). | |
6689 | */ | |
6690 | preempt_disable(); | |
6691 | complete(&sqd->completion); | |
6692 | schedule_preempt_disabled(); | |
6693 | preempt_enable(); | |
6694 | } | |
6695 | __set_current_state(TASK_RUNNING); | |
6696 | } | |
6697 | ||
c8d1ba58 JA |
6698 | static int io_sq_thread(void *data) |
6699 | { | |
69fb2131 JA |
6700 | struct io_sq_data *sqd = data; |
6701 | struct io_ring_ctx *ctx; | |
a0d9205f | 6702 | unsigned long timeout = 0; |
37d1e2e3 | 6703 | char buf[TASK_COMM_LEN]; |
08369246 | 6704 | DEFINE_WAIT(wait); |
6c271ce2 | 6705 | |
37d1e2e3 JA |
6706 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
6707 | set_task_comm(current, buf); | |
6708 | sqd->thread = current; | |
6709 | current->pf_io_worker = NULL; | |
6710 | ||
6711 | if (sqd->sq_cpu != -1) | |
6712 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6713 | else | |
6714 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6715 | current->flags |= PF_NO_SETAFFINITY; | |
6716 | ||
6717 | complete(&sqd->completion); | |
6c271ce2 | 6718 | |
37d1e2e3 JA |
6719 | wait_for_completion(&sqd->startup); |
6720 | ||
6721 | while (!io_sq_thread_should_stop(sqd)) { | |
08369246 XW |
6722 | int ret; |
6723 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
6724 | |
6725 | /* | |
69fb2131 | 6726 | * Any changes to the sqd lists are synchronized through the |
37d1e2e3 | 6727 | * thread parking. This synchronizes the thread vs users, |
69fb2131 | 6728 | * the users are synchronized on the sqd->ctx_lock. |
c1edbf5f | 6729 | */ |
37d1e2e3 JA |
6730 | if (io_sq_thread_should_park(sqd)) { |
6731 | io_sq_thread_parkme(sqd); | |
6732 | continue; | |
65b2b213 | 6733 | } |
08369246 | 6734 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 6735 | io_sqd_init_new(sqd); |
08369246 XW |
6736 | timeout = jiffies + sqd->sq_thread_idle; |
6737 | } | |
37d1e2e3 JA |
6738 | if (fatal_signal_pending(current)) |
6739 | break; | |
08369246 | 6740 | sqt_spin = false; |
e95eee2d | 6741 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6742 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
08369246 XW |
6743 | ret = __io_sq_thread(ctx, cap_entries); |
6744 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
6745 | sqt_spin = true; | |
69fb2131 | 6746 | } |
6c271ce2 | 6747 | |
08369246 | 6748 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6749 | io_run_task_work(); |
6750 | cond_resched(); | |
08369246 XW |
6751 | if (sqt_spin) |
6752 | timeout = jiffies + sqd->sq_thread_idle; | |
6753 | continue; | |
6754 | } | |
6755 | ||
08369246 XW |
6756 | needs_sched = true; |
6757 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6758 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6759 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6760 | !list_empty_careful(&ctx->iopoll_list)) { | |
6761 | needs_sched = false; | |
6762 | break; | |
6763 | } | |
6764 | if (io_sqring_entries(ctx)) { | |
6765 | needs_sched = false; | |
6766 | break; | |
6767 | } | |
6768 | } | |
6769 | ||
37d1e2e3 | 6770 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
69fb2131 JA |
6771 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6772 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6773 | |
69fb2131 | 6774 | schedule(); |
69fb2131 JA |
6775 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6776 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6777 | } |
08369246 XW |
6778 | |
6779 | finish_wait(&sqd->wait, &wait); | |
6780 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
6781 | } |
6782 | ||
37d1e2e3 JA |
6783 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6784 | io_uring_cancel_sqpoll(ctx); | |
b41e9852 | 6785 | |
37d1e2e3 | 6786 | io_run_task_work(); |
28cea78a | 6787 | |
37d1e2e3 JA |
6788 | /* |
6789 | * Clear thread under lock so that concurrent parks work correctly | |
6790 | */ | |
6791 | complete_all(&sqd->completion); | |
6792 | mutex_lock(&sqd->lock); | |
6793 | sqd->thread = NULL; | |
6794 | mutex_unlock(&sqd->lock); | |
06058632 | 6795 | |
37d1e2e3 JA |
6796 | complete(&sqd->exited); |
6797 | do_exit(0); | |
6c271ce2 JA |
6798 | } |
6799 | ||
bda52162 JA |
6800 | struct io_wait_queue { |
6801 | struct wait_queue_entry wq; | |
6802 | struct io_ring_ctx *ctx; | |
6803 | unsigned to_wait; | |
6804 | unsigned nr_timeouts; | |
6805 | }; | |
6806 | ||
6c503150 | 6807 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6808 | { |
6809 | struct io_ring_ctx *ctx = iowq->ctx; | |
6810 | ||
6811 | /* | |
d195a66e | 6812 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6813 | * started waiting. For timeouts, we always want to return to userspace, |
6814 | * regardless of event count. | |
6815 | */ | |
6c503150 | 6816 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6817 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6818 | } | |
6819 | ||
6820 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6821 | int wake_flags, void *key) | |
6822 | { | |
6823 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6824 | wq); | |
6825 | ||
6c503150 PB |
6826 | /* |
6827 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6828 | * the task, and the next invocation will do it. | |
6829 | */ | |
6830 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6831 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6832 | return -1; | |
bda52162 JA |
6833 | } |
6834 | ||
af9c1a44 JA |
6835 | static int io_run_task_work_sig(void) |
6836 | { | |
6837 | if (io_run_task_work()) | |
6838 | return 1; | |
6839 | if (!signal_pending(current)) | |
6840 | return 0; | |
792ee0f6 JA |
6841 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
6842 | return -ERESTARTSYS; | |
af9c1a44 JA |
6843 | return -EINTR; |
6844 | } | |
6845 | ||
eeb60b9a PB |
6846 | /* when returns >0, the caller should retry */ |
6847 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6848 | struct io_wait_queue *iowq, | |
6849 | signed long *timeout) | |
6850 | { | |
6851 | int ret; | |
6852 | ||
6853 | /* make sure we run task_work before checking for signals */ | |
6854 | ret = io_run_task_work_sig(); | |
6855 | if (ret || io_should_wake(iowq)) | |
6856 | return ret; | |
6857 | /* let the caller flush overflows, retry */ | |
6858 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6859 | return 1; | |
6860 | ||
6861 | *timeout = schedule_timeout(*timeout); | |
6862 | return !*timeout ? -ETIME : 1; | |
6863 | } | |
6864 | ||
2b188cc1 JA |
6865 | /* |
6866 | * Wait until events become available, if we don't already have some. The | |
6867 | * application must reap them itself, as they reside on the shared cq ring. | |
6868 | */ | |
6869 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
6870 | const sigset_t __user *sig, size_t sigsz, |
6871 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 6872 | { |
bda52162 JA |
6873 | struct io_wait_queue iowq = { |
6874 | .wq = { | |
6875 | .private = current, | |
6876 | .func = io_wake_function, | |
6877 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6878 | }, | |
6879 | .ctx = ctx, | |
6880 | .to_wait = min_events, | |
6881 | }; | |
75b28aff | 6882 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
6883 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
6884 | int ret; | |
2b188cc1 | 6885 | |
b41e9852 | 6886 | do { |
6c503150 PB |
6887 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6888 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 6889 | return 0; |
4c6e277c | 6890 | if (!io_run_task_work()) |
b41e9852 | 6891 | break; |
b41e9852 | 6892 | } while (1); |
2b188cc1 JA |
6893 | |
6894 | if (sig) { | |
9e75ad5d AB |
6895 | #ifdef CONFIG_COMPAT |
6896 | if (in_compat_syscall()) | |
6897 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6898 | sigsz); |
9e75ad5d AB |
6899 | else |
6900 | #endif | |
b772434b | 6901 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6902 | |
2b188cc1 JA |
6903 | if (ret) |
6904 | return ret; | |
6905 | } | |
6906 | ||
c73ebb68 | 6907 | if (uts) { |
c1d5a224 PB |
6908 | struct timespec64 ts; |
6909 | ||
c73ebb68 HX |
6910 | if (get_timespec64(&ts, uts)) |
6911 | return -EFAULT; | |
6912 | timeout = timespec64_to_jiffies(&ts); | |
6913 | } | |
6914 | ||
bda52162 | 6915 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6916 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 6917 | do { |
6c503150 | 6918 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
6919 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
6920 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
6921 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
6922 | finish_wait(&ctx->wait, &iowq.wq); | |
6923 | } while (ret > 0); | |
bda52162 | 6924 | |
b7db41c9 | 6925 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6926 | |
75b28aff | 6927 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6928 | } |
6929 | ||
6b06314c JA |
6930 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6931 | { | |
6932 | #if defined(CONFIG_UNIX) | |
6933 | if (ctx->ring_sock) { | |
6934 | struct sock *sock = ctx->ring_sock->sk; | |
6935 | struct sk_buff *skb; | |
6936 | ||
6937 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6938 | kfree_skb(skb); | |
6939 | } | |
6940 | #else | |
6941 | int i; | |
6942 | ||
65e19f54 JA |
6943 | for (i = 0; i < ctx->nr_user_files; i++) { |
6944 | struct file *file; | |
6945 | ||
6946 | file = io_file_from_index(ctx, i); | |
6947 | if (file) | |
6948 | fput(file); | |
6949 | } | |
6b06314c JA |
6950 | #endif |
6951 | } | |
6952 | ||
00835dce | 6953 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 6954 | { |
269bbe5f | 6955 | struct fixed_rsrc_data *data; |
05f3fb3c | 6956 | |
269bbe5f | 6957 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
6958 | complete(&data->done); |
6959 | } | |
6960 | ||
2a63b2d9 | 6961 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 6962 | { |
2a63b2d9 | 6963 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
6964 | } |
6965 | ||
2a63b2d9 | 6966 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 6967 | { |
2a63b2d9 BM |
6968 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
6969 | } | |
65e19f54 | 6970 | |
d67d2263 BM |
6971 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
6972 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 6973 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 6974 | { |
2a63b2d9 | 6975 | io_rsrc_ref_lock(ctx); |
269bbe5f | 6976 | rsrc_data->node = ref_node; |
d67d2263 | 6977 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 6978 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 6979 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
6980 | } |
6981 | ||
8bad28d8 | 6982 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 6983 | { |
8bad28d8 | 6984 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 6985 | |
2a63b2d9 | 6986 | io_rsrc_ref_lock(ctx); |
1e5d770b | 6987 | ref_node = data->node; |
e6cb007c | 6988 | data->node = NULL; |
2a63b2d9 | 6989 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
6990 | if (ref_node) |
6991 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 | 6992 | } |
05589553 | 6993 | |
8bad28d8 HX |
6994 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
6995 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
6996 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
6997 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 6998 | { |
f2303b1f | 6999 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 7000 | int ret; |
05589553 | 7001 | |
8bad28d8 HX |
7002 | if (data->quiesce) |
7003 | return -ENXIO; | |
05589553 | 7004 | |
8bad28d8 | 7005 | data->quiesce = true; |
1ffc5422 | 7006 | do { |
f2303b1f PB |
7007 | ret = -ENOMEM; |
7008 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7009 | if (!backup_node) | |
7010 | break; | |
7011 | backup_node->rsrc_data = data; | |
7012 | backup_node->rsrc_put = rsrc_put; | |
7013 | ||
8bad28d8 HX |
7014 | io_sqe_rsrc_kill_node(ctx, data); |
7015 | percpu_ref_kill(&data->refs); | |
7016 | flush_delayed_work(&ctx->rsrc_put_work); | |
7017 | ||
1ffc5422 | 7018 | ret = wait_for_completion_interruptible(&data->done); |
88f171ab | 7019 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
1ffc5422 | 7020 | break; |
8bad28d8 | 7021 | |
8bad28d8 HX |
7022 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7023 | backup_node = NULL; | |
8bad28d8 | 7024 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7025 | ret = io_run_task_work_sig(); |
8bad28d8 | 7026 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7027 | } while (ret >= 0); |
8bad28d8 | 7028 | data->quiesce = false; |
05f3fb3c | 7029 | |
8bad28d8 HX |
7030 | if (backup_node) |
7031 | destroy_fixed_rsrc_ref_node(backup_node); | |
7032 | return ret; | |
d7954b2b BM |
7033 | } |
7034 | ||
1ad555c6 BM |
7035 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7036 | { | |
7037 | struct fixed_rsrc_data *data; | |
7038 | ||
7039 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7040 | if (!data) | |
7041 | return NULL; | |
7042 | ||
00835dce | 7043 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7044 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7045 | kfree(data); | |
7046 | return NULL; | |
7047 | } | |
7048 | data->ctx = ctx; | |
7049 | init_completion(&data->done); | |
7050 | return data; | |
7051 | } | |
7052 | ||
7053 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7054 | { | |
7055 | percpu_ref_exit(&data->refs); | |
7056 | kfree(data->table); | |
7057 | kfree(data); | |
7058 | } | |
7059 | ||
d7954b2b BM |
7060 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7061 | { | |
7062 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7063 | unsigned nr_tables, i; |
7064 | int ret; | |
7065 | ||
8bad28d8 HX |
7066 | /* |
7067 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7068 | * Since we possibly drop uring lock later in this function to | |
7069 | * run task work. | |
7070 | */ | |
7071 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7072 | return -ENXIO; |
f2303b1f | 7073 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7074 | if (ret) |
7075 | return ret; | |
7076 | ||
6b06314c | 7077 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7078 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7079 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7080 | kfree(data->table[i].files); |
1ad555c6 | 7081 | free_fixed_rsrc_data(data); |
05f3fb3c | 7082 | ctx->file_data = NULL; |
6b06314c JA |
7083 | ctx->nr_user_files = 0; |
7084 | return 0; | |
7085 | } | |
7086 | ||
37d1e2e3 JA |
7087 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7088 | __releases(&sqd->lock) | |
7089 | { | |
7090 | if (!sqd->thread) | |
7091 | return; | |
7092 | if (sqd->thread == current) | |
7093 | return; | |
7094 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7095 | wake_up_state(sqd->thread, TASK_PARKED); | |
7096 | mutex_unlock(&sqd->lock); | |
7097 | } | |
7098 | ||
7099 | static bool io_sq_thread_park(struct io_sq_data *sqd) | |
7100 | __acquires(&sqd->lock) | |
7101 | { | |
7102 | if (sqd->thread == current) | |
7103 | return true; | |
7104 | mutex_lock(&sqd->lock); | |
7105 | if (!sqd->thread) { | |
7106 | mutex_unlock(&sqd->lock); | |
7107 | return false; | |
7108 | } | |
7109 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7110 | wake_up_process(sqd->thread); | |
7111 | wait_for_completion(&sqd->completion); | |
7112 | return true; | |
7113 | } | |
7114 | ||
7115 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7116 | { | |
7117 | if (!sqd->thread) | |
7118 | return; | |
7119 | ||
7120 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
7121 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); | |
7122 | wake_up_process(sqd->thread); | |
7123 | wait_for_completion(&sqd->exited); | |
7124 | } | |
7125 | ||
534ca6d6 | 7126 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7127 | { |
534ca6d6 | 7128 | if (refcount_dec_and_test(&sqd->refs)) { |
37d1e2e3 JA |
7129 | io_sq_thread_stop(sqd); |
7130 | kfree(sqd); | |
7131 | } | |
7132 | } | |
7133 | ||
7134 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7135 | { | |
7136 | struct io_sq_data *sqd = ctx->sq_data; | |
7137 | ||
7138 | if (sqd) { | |
534ca6d6 | 7139 | if (sqd->thread) { |
37d1e2e3 JA |
7140 | wait_for_completion(&ctx->sq_thread_comp); |
7141 | io_sq_thread_park(sqd); | |
534ca6d6 JA |
7142 | } |
7143 | ||
37d1e2e3 JA |
7144 | mutex_lock(&sqd->ctx_lock); |
7145 | list_del(&ctx->sqd_list); | |
7146 | io_sqd_update_thread_idle(sqd); | |
7147 | mutex_unlock(&sqd->ctx_lock); | |
7148 | ||
7149 | if (sqd->thread) | |
7150 | io_sq_thread_unpark(sqd); | |
7151 | ||
7152 | io_put_sq_data(sqd); | |
7153 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7154 | } |
7155 | } | |
7156 | ||
aa06165d JA |
7157 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7158 | { | |
7159 | struct io_ring_ctx *ctx_attach; | |
7160 | struct io_sq_data *sqd; | |
7161 | struct fd f; | |
7162 | ||
7163 | f = fdget(p->wq_fd); | |
7164 | if (!f.file) | |
7165 | return ERR_PTR(-ENXIO); | |
7166 | if (f.file->f_op != &io_uring_fops) { | |
7167 | fdput(f); | |
7168 | return ERR_PTR(-EINVAL); | |
7169 | } | |
7170 | ||
7171 | ctx_attach = f.file->private_data; | |
7172 | sqd = ctx_attach->sq_data; | |
7173 | if (!sqd) { | |
7174 | fdput(f); | |
7175 | return ERR_PTR(-EINVAL); | |
7176 | } | |
7177 | ||
7178 | refcount_inc(&sqd->refs); | |
7179 | fdput(f); | |
7180 | return sqd; | |
7181 | } | |
7182 | ||
534ca6d6 JA |
7183 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7184 | { | |
7185 | struct io_sq_data *sqd; | |
7186 | ||
aa06165d JA |
7187 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7188 | return io_attach_sq_data(p); | |
7189 | ||
534ca6d6 JA |
7190 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7191 | if (!sqd) | |
7192 | return ERR_PTR(-ENOMEM); | |
7193 | ||
7194 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7195 | INIT_LIST_HEAD(&sqd->ctx_list); |
7196 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7197 | mutex_init(&sqd->ctx_lock); | |
7198 | mutex_init(&sqd->lock); | |
534ca6d6 | 7199 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 JA |
7200 | init_completion(&sqd->startup); |
7201 | init_completion(&sqd->completion); | |
7202 | init_completion(&sqd->exited); | |
534ca6d6 JA |
7203 | return sqd; |
7204 | } | |
7205 | ||
6b06314c | 7206 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7207 | /* |
7208 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7209 | * the io_uring can be safely unregistered on process exit, even if we have | |
7210 | * loops in the file referencing. | |
7211 | */ | |
7212 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7213 | { | |
7214 | struct sock *sk = ctx->ring_sock->sk; | |
7215 | struct scm_fp_list *fpl; | |
7216 | struct sk_buff *skb; | |
08a45173 | 7217 | int i, nr_files; |
6b06314c | 7218 | |
6b06314c JA |
7219 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7220 | if (!fpl) | |
7221 | return -ENOMEM; | |
7222 | ||
7223 | skb = alloc_skb(0, GFP_KERNEL); | |
7224 | if (!skb) { | |
7225 | kfree(fpl); | |
7226 | return -ENOMEM; | |
7227 | } | |
7228 | ||
7229 | skb->sk = sk; | |
6b06314c | 7230 | |
08a45173 | 7231 | nr_files = 0; |
62e398be | 7232 | fpl->user = get_uid(current_user()); |
6b06314c | 7233 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7234 | struct file *file = io_file_from_index(ctx, i + offset); |
7235 | ||
7236 | if (!file) | |
08a45173 | 7237 | continue; |
65e19f54 | 7238 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7239 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7240 | nr_files++; | |
6b06314c JA |
7241 | } |
7242 | ||
08a45173 JA |
7243 | if (nr_files) { |
7244 | fpl->max = SCM_MAX_FD; | |
7245 | fpl->count = nr_files; | |
7246 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7247 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7248 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7249 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7250 | |
08a45173 JA |
7251 | for (i = 0; i < nr_files; i++) |
7252 | fput(fpl->fp[i]); | |
7253 | } else { | |
7254 | kfree_skb(skb); | |
7255 | kfree(fpl); | |
7256 | } | |
6b06314c JA |
7257 | |
7258 | return 0; | |
7259 | } | |
7260 | ||
7261 | /* | |
7262 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7263 | * causes regular reference counting to break down. We rely on the UNIX | |
7264 | * garbage collection to take care of this problem for us. | |
7265 | */ | |
7266 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7267 | { | |
7268 | unsigned left, total; | |
7269 | int ret = 0; | |
7270 | ||
7271 | total = 0; | |
7272 | left = ctx->nr_user_files; | |
7273 | while (left) { | |
7274 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7275 | |
7276 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7277 | if (ret) | |
7278 | break; | |
7279 | left -= this_files; | |
7280 | total += this_files; | |
7281 | } | |
7282 | ||
7283 | if (!ret) | |
7284 | return 0; | |
7285 | ||
7286 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7287 | struct file *file = io_file_from_index(ctx, total); |
7288 | ||
7289 | if (file) | |
7290 | fput(file); | |
6b06314c JA |
7291 | total++; |
7292 | } | |
7293 | ||
7294 | return ret; | |
7295 | } | |
7296 | #else | |
7297 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7298 | { | |
7299 | return 0; | |
7300 | } | |
7301 | #endif | |
7302 | ||
269bbe5f | 7303 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7304 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7305 | { |
7306 | int i; | |
7307 | ||
7308 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7309 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7310 | unsigned this_files; |
7311 | ||
7312 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7313 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7314 | GFP_KERNEL); | |
7315 | if (!table->files) | |
7316 | break; | |
7317 | nr_files -= this_files; | |
7318 | } | |
7319 | ||
7320 | if (i == nr_tables) | |
7321 | return 0; | |
7322 | ||
7323 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7324 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7325 | kfree(table->files); |
7326 | } | |
7327 | return 1; | |
7328 | } | |
7329 | ||
50238531 | 7330 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7331 | { |
50238531 | 7332 | struct file *file = prsrc->file; |
05f3fb3c JA |
7333 | #if defined(CONFIG_UNIX) |
7334 | struct sock *sock = ctx->ring_sock->sk; | |
7335 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7336 | struct sk_buff *skb; | |
7337 | int i; | |
7338 | ||
7339 | __skb_queue_head_init(&list); | |
7340 | ||
7341 | /* | |
7342 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7343 | * remove this entry and rearrange the file array. | |
7344 | */ | |
7345 | skb = skb_dequeue(head); | |
7346 | while (skb) { | |
7347 | struct scm_fp_list *fp; | |
7348 | ||
7349 | fp = UNIXCB(skb).fp; | |
7350 | for (i = 0; i < fp->count; i++) { | |
7351 | int left; | |
7352 | ||
7353 | if (fp->fp[i] != file) | |
7354 | continue; | |
7355 | ||
7356 | unix_notinflight(fp->user, fp->fp[i]); | |
7357 | left = fp->count - 1 - i; | |
7358 | if (left) { | |
7359 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7360 | left * sizeof(struct file *)); | |
7361 | } | |
7362 | fp->count--; | |
7363 | if (!fp->count) { | |
7364 | kfree_skb(skb); | |
7365 | skb = NULL; | |
7366 | } else { | |
7367 | __skb_queue_tail(&list, skb); | |
7368 | } | |
7369 | fput(file); | |
7370 | file = NULL; | |
7371 | break; | |
7372 | } | |
7373 | ||
7374 | if (!file) | |
7375 | break; | |
7376 | ||
7377 | __skb_queue_tail(&list, skb); | |
7378 | ||
7379 | skb = skb_dequeue(head); | |
7380 | } | |
7381 | ||
7382 | if (skb_peek(&list)) { | |
7383 | spin_lock_irq(&head->lock); | |
7384 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7385 | __skb_queue_tail(head, skb); | |
7386 | spin_unlock_irq(&head->lock); | |
7387 | } | |
7388 | #else | |
7389 | fput(file); | |
7390 | #endif | |
7391 | } | |
7392 | ||
269bbe5f | 7393 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7394 | { |
269bbe5f BM |
7395 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7396 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7397 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7398 | |
269bbe5f BM |
7399 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7400 | list_del(&prsrc->list); | |
50238531 | 7401 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7402 | kfree(prsrc); |
65e19f54 | 7403 | } |
05589553 | 7404 | |
05589553 XW |
7405 | percpu_ref_exit(&ref_node->refs); |
7406 | kfree(ref_node); | |
269bbe5f | 7407 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7408 | } |
65e19f54 | 7409 | |
269bbe5f | 7410 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7411 | { |
7412 | struct io_ring_ctx *ctx; | |
7413 | struct llist_node *node; | |
7414 | ||
269bbe5f BM |
7415 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7416 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7417 | |
7418 | while (node) { | |
269bbe5f | 7419 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7420 | struct llist_node *next = node->next; |
7421 | ||
269bbe5f BM |
7422 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7423 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7424 | node = next; |
7425 | } | |
7426 | } | |
7427 | ||
ea64ec02 PB |
7428 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7429 | unsigned i) | |
2faf852d | 7430 | { |
ea64ec02 PB |
7431 | struct fixed_rsrc_table *table; |
7432 | ||
7433 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7434 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7435 | } | |
7436 | ||
00835dce | 7437 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7438 | { |
269bbe5f BM |
7439 | struct fixed_rsrc_ref_node *ref_node; |
7440 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7441 | struct io_ring_ctx *ctx; |
e297822b | 7442 | bool first_add = false; |
4a38aed2 | 7443 | int delay = HZ; |
65e19f54 | 7444 | |
269bbe5f BM |
7445 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7446 | data = ref_node->rsrc_data; | |
e297822b PB |
7447 | ctx = data->ctx; |
7448 | ||
2a63b2d9 | 7449 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7450 | ref_node->done = true; |
7451 | ||
d67d2263 BM |
7452 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7453 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7454 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7455 | /* recycle ref nodes in order */ |
7456 | if (!ref_node->done) | |
7457 | break; | |
7458 | list_del(&ref_node->node); | |
269bbe5f | 7459 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7460 | } |
2a63b2d9 | 7461 | io_rsrc_ref_unlock(ctx); |
05589553 | 7462 | |
e297822b | 7463 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7464 | delay = 0; |
05589553 | 7465 | |
4a38aed2 | 7466 | if (!delay) |
269bbe5f | 7467 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7468 | else if (first_add) |
269bbe5f | 7469 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7470 | } |
65e19f54 | 7471 | |
6802535d | 7472 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7473 | struct io_ring_ctx *ctx) |
05f3fb3c | 7474 | { |
269bbe5f | 7475 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7476 | |
05589553 XW |
7477 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7478 | if (!ref_node) | |
3e2224c5 | 7479 | return NULL; |
05f3fb3c | 7480 | |
00835dce | 7481 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7482 | 0, GFP_KERNEL)) { |
7483 | kfree(ref_node); | |
3e2224c5 | 7484 | return NULL; |
05589553 XW |
7485 | } |
7486 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7487 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7488 | ref_node->done = false; |
05589553 | 7489 | return ref_node; |
05589553 XW |
7490 | } |
7491 | ||
bc9744cd PB |
7492 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7493 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7494 | { |
269bbe5f | 7495 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7496 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7497 | } |
7498 | ||
269bbe5f | 7499 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7500 | { |
7501 | percpu_ref_exit(&ref_node->refs); | |
7502 | kfree(ref_node); | |
65e19f54 JA |
7503 | } |
7504 | ||
ea64ec02 | 7505 | |
6b06314c JA |
7506 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7507 | unsigned nr_args) | |
7508 | { | |
7509 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7510 | unsigned nr_tables, i; |
05f3fb3c | 7511 | struct file *file; |
600cf3f8 | 7512 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7513 | struct fixed_rsrc_ref_node *ref_node; |
7514 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7515 | |
05f3fb3c | 7516 | if (ctx->file_data) |
6b06314c JA |
7517 | return -EBUSY; |
7518 | if (!nr_args) | |
7519 | return -EINVAL; | |
7520 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7521 | return -EMFILE; | |
7522 | ||
1ad555c6 | 7523 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7524 | if (!file_data) |
05f3fb3c | 7525 | return -ENOMEM; |
13770a71 | 7526 | ctx->file_data = file_data; |
05f3fb3c | 7527 | |
65e19f54 | 7528 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7529 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7530 | GFP_KERNEL); |
600cf3f8 PB |
7531 | if (!file_data->table) |
7532 | goto out_free; | |
05f3fb3c | 7533 | |
600cf3f8 | 7534 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7535 | goto out_free; |
65e19f54 | 7536 | |
08a45173 | 7537 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7538 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7539 | ret = -EFAULT; | |
7540 | goto out_fput; | |
7541 | } | |
08a45173 | 7542 | /* allow sparse sets */ |
600cf3f8 | 7543 | if (fd == -1) |
08a45173 | 7544 | continue; |
6b06314c | 7545 | |
05f3fb3c | 7546 | file = fget(fd); |
6b06314c | 7547 | ret = -EBADF; |
05f3fb3c | 7548 | if (!file) |
600cf3f8 | 7549 | goto out_fput; |
05f3fb3c | 7550 | |
6b06314c JA |
7551 | /* |
7552 | * Don't allow io_uring instances to be registered. If UNIX | |
7553 | * isn't enabled, then this causes a reference cycle and this | |
7554 | * instance can never get freed. If UNIX is enabled we'll | |
7555 | * handle it just fine, but there's still no point in allowing | |
7556 | * a ring fd as it doesn't support regular read/write anyway. | |
7557 | */ | |
05f3fb3c JA |
7558 | if (file->f_op == &io_uring_fops) { |
7559 | fput(file); | |
600cf3f8 | 7560 | goto out_fput; |
6b06314c | 7561 | } |
ea64ec02 | 7562 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7563 | } |
7564 | ||
6b06314c | 7565 | ret = io_sqe_files_scm(ctx); |
05589553 | 7566 | if (ret) { |
6b06314c | 7567 | io_sqe_files_unregister(ctx); |
05589553 XW |
7568 | return ret; |
7569 | } | |
6b06314c | 7570 | |
bc9744cd | 7571 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7572 | if (!ref_node) { |
05589553 | 7573 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7574 | return -ENOMEM; |
05589553 | 7575 | } |
bc9744cd | 7576 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7577 | |
d67d2263 | 7578 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7579 | return ret; |
600cf3f8 PB |
7580 | out_fput: |
7581 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7582 | file = io_file_from_index(ctx, i); | |
7583 | if (file) | |
7584 | fput(file); | |
7585 | } | |
7586 | for (i = 0; i < nr_tables; i++) | |
7587 | kfree(file_data->table[i].files); | |
7588 | ctx->nr_user_files = 0; | |
600cf3f8 | 7589 | out_free: |
1ad555c6 | 7590 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7591 | ctx->file_data = NULL; |
6b06314c JA |
7592 | return ret; |
7593 | } | |
7594 | ||
c3a31e60 JA |
7595 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7596 | int index) | |
7597 | { | |
7598 | #if defined(CONFIG_UNIX) | |
7599 | struct sock *sock = ctx->ring_sock->sk; | |
7600 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7601 | struct sk_buff *skb; | |
7602 | ||
7603 | /* | |
7604 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7605 | * file set. If there's no room, fall back to allocating a new skb | |
7606 | * and filling it in. | |
7607 | */ | |
7608 | spin_lock_irq(&head->lock); | |
7609 | skb = skb_peek(head); | |
7610 | if (skb) { | |
7611 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7612 | ||
7613 | if (fpl->count < SCM_MAX_FD) { | |
7614 | __skb_unlink(skb, head); | |
7615 | spin_unlock_irq(&head->lock); | |
7616 | fpl->fp[fpl->count] = get_file(file); | |
7617 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7618 | fpl->count++; | |
7619 | spin_lock_irq(&head->lock); | |
7620 | __skb_queue_head(head, skb); | |
7621 | } else { | |
7622 | skb = NULL; | |
7623 | } | |
7624 | } | |
7625 | spin_unlock_irq(&head->lock); | |
7626 | ||
7627 | if (skb) { | |
7628 | fput(file); | |
7629 | return 0; | |
7630 | } | |
7631 | ||
7632 | return __io_sqe_files_scm(ctx, 1, index); | |
7633 | #else | |
7634 | return 0; | |
7635 | #endif | |
7636 | } | |
7637 | ||
50238531 | 7638 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7639 | { |
269bbe5f BM |
7640 | struct io_rsrc_put *prsrc; |
7641 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7642 | |
269bbe5f BM |
7643 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7644 | if (!prsrc) | |
a5318d3c | 7645 | return -ENOMEM; |
05f3fb3c | 7646 | |
50238531 | 7647 | prsrc->rsrc = rsrc; |
269bbe5f | 7648 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7649 | |
a5318d3c | 7650 | return 0; |
05f3fb3c JA |
7651 | } |
7652 | ||
269bbe5f BM |
7653 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7654 | struct file *file) | |
7655 | { | |
50238531 | 7656 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7657 | } |
7658 | ||
05f3fb3c | 7659 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7660 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7661 | unsigned nr_args) |
7662 | { | |
269bbe5f BM |
7663 | struct fixed_rsrc_data *data = ctx->file_data; |
7664 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7665 | struct file *file, **file_slot; |
c3a31e60 JA |
7666 | __s32 __user *fds; |
7667 | int fd, i, err; | |
7668 | __u32 done; | |
05589553 | 7669 | bool needs_switch = false; |
c3a31e60 | 7670 | |
05f3fb3c | 7671 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7672 | return -EOVERFLOW; |
7673 | if (done > ctx->nr_user_files) | |
7674 | return -EINVAL; | |
7675 | ||
bc9744cd | 7676 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
7677 | if (!ref_node) |
7678 | return -ENOMEM; | |
bc9744cd | 7679 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7680 | |
269bbe5f | 7681 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7682 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7683 | err = 0; |
7684 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7685 | err = -EFAULT; | |
7686 | break; | |
7687 | } | |
4e0377a1 | 7688 | if (fd == IORING_REGISTER_FILES_SKIP) |
7689 | continue; | |
7690 | ||
67973b93 | 7691 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7692 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7693 | ||
7694 | if (*file_slot) { | |
7695 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
7696 | if (err) |
7697 | break; | |
ea64ec02 | 7698 | *file_slot = NULL; |
05589553 | 7699 | needs_switch = true; |
c3a31e60 JA |
7700 | } |
7701 | if (fd != -1) { | |
c3a31e60 JA |
7702 | file = fget(fd); |
7703 | if (!file) { | |
7704 | err = -EBADF; | |
7705 | break; | |
7706 | } | |
7707 | /* | |
7708 | * Don't allow io_uring instances to be registered. If | |
7709 | * UNIX isn't enabled, then this causes a reference | |
7710 | * cycle and this instance can never get freed. If UNIX | |
7711 | * is enabled we'll handle it just fine, but there's | |
7712 | * still no point in allowing a ring fd as it doesn't | |
7713 | * support regular read/write anyway. | |
7714 | */ | |
7715 | if (file->f_op == &io_uring_fops) { | |
7716 | fput(file); | |
7717 | err = -EBADF; | |
7718 | break; | |
7719 | } | |
e68a3ff8 | 7720 | *file_slot = file; |
c3a31e60 | 7721 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7722 | if (err) { |
e68a3ff8 | 7723 | *file_slot = NULL; |
f3bd9dae | 7724 | fput(file); |
c3a31e60 | 7725 | break; |
f3bd9dae | 7726 | } |
c3a31e60 | 7727 | } |
05f3fb3c JA |
7728 | } |
7729 | ||
05589553 | 7730 | if (needs_switch) { |
b2e96852 | 7731 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 7732 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 7733 | } else |
269bbe5f | 7734 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
7735 | |
7736 | return done ? done : err; | |
7737 | } | |
05589553 | 7738 | |
05f3fb3c JA |
7739 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7740 | unsigned nr_args) | |
7741 | { | |
269bbe5f | 7742 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7743 | |
7744 | if (!ctx->file_data) | |
7745 | return -ENXIO; | |
7746 | if (!nr_args) | |
7747 | return -EINVAL; | |
7748 | if (copy_from_user(&up, arg, sizeof(up))) | |
7749 | return -EFAULT; | |
7750 | if (up.resv) | |
7751 | return -EINVAL; | |
7752 | ||
7753 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7754 | } | |
c3a31e60 | 7755 | |
5280f7e5 | 7756 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7757 | { |
7758 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7759 | ||
5280f7e5 PB |
7760 | req = io_put_req_find_next(req); |
7761 | return req ? &req->work : NULL; | |
7d723065 JA |
7762 | } |
7763 | ||
5aa75ed5 | 7764 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
24369c2e PB |
7765 | { |
7766 | struct io_wq_data data; | |
24369c2e | 7767 | unsigned int concurrency; |
24369c2e | 7768 | |
e9fd9396 | 7769 | data.free_work = io_free_work; |
f5fa38c5 | 7770 | data.do_work = io_wq_submit_work; |
24369c2e | 7771 | |
d25e3a3d JA |
7772 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7773 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7774 | |
5aa75ed5 | 7775 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7776 | } |
7777 | ||
5aa75ed5 JA |
7778 | static int io_uring_alloc_task_context(struct task_struct *task, |
7779 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7780 | { |
7781 | struct io_uring_task *tctx; | |
d8a6df10 | 7782 | int ret; |
0f212204 JA |
7783 | |
7784 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7785 | if (unlikely(!tctx)) | |
7786 | return -ENOMEM; | |
7787 | ||
d8a6df10 JA |
7788 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7789 | if (unlikely(ret)) { | |
7790 | kfree(tctx); | |
7791 | return ret; | |
7792 | } | |
7793 | ||
5aa75ed5 JA |
7794 | tctx->io_wq = io_init_wq_offload(ctx); |
7795 | if (IS_ERR(tctx->io_wq)) { | |
7796 | ret = PTR_ERR(tctx->io_wq); | |
7797 | percpu_counter_destroy(&tctx->inflight); | |
7798 | kfree(tctx); | |
7799 | return ret; | |
7800 | } | |
7801 | ||
0f212204 JA |
7802 | xa_init(&tctx->xa); |
7803 | init_waitqueue_head(&tctx->wait); | |
7804 | tctx->last = NULL; | |
fdaf083c JA |
7805 | atomic_set(&tctx->in_idle, 0); |
7806 | tctx->sqpoll = false; | |
0f212204 | 7807 | task->io_uring = tctx; |
7cbf1722 JA |
7808 | spin_lock_init(&tctx->task_lock); |
7809 | INIT_WQ_LIST(&tctx->task_list); | |
7810 | tctx->task_state = 0; | |
7811 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7812 | return 0; |
7813 | } | |
7814 | ||
7815 | void __io_uring_free(struct task_struct *tsk) | |
7816 | { | |
7817 | struct io_uring_task *tctx = tsk->io_uring; | |
7818 | ||
7819 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
d8a6df10 | 7820 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7821 | kfree(tctx); |
7822 | tsk->io_uring = NULL; | |
7823 | } | |
7824 | ||
7e84e1c7 SG |
7825 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7826 | struct io_uring_params *p) | |
2b188cc1 JA |
7827 | { |
7828 | int ret; | |
7829 | ||
d25e3a3d JA |
7830 | /* Retain compatibility with failing for an invalid attach attempt */ |
7831 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7832 | IORING_SETUP_ATTACH_WQ) { | |
7833 | struct fd f; | |
7834 | ||
7835 | f = fdget(p->wq_fd); | |
7836 | if (!f.file) | |
7837 | return -ENXIO; | |
7838 | if (f.file->f_op != &io_uring_fops) { | |
7839 | fdput(f); | |
7840 | return -EINVAL; | |
7841 | } | |
7842 | fdput(f); | |
7843 | } | |
6c271ce2 | 7844 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
7845 | struct io_sq_data *sqd; |
7846 | ||
3ec482d1 | 7847 | ret = -EPERM; |
ce59fc69 | 7848 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
7849 | goto err; |
7850 | ||
534ca6d6 JA |
7851 | sqd = io_get_sq_data(p); |
7852 | if (IS_ERR(sqd)) { | |
7853 | ret = PTR_ERR(sqd); | |
7854 | goto err; | |
7855 | } | |
69fb2131 | 7856 | |
534ca6d6 | 7857 | ctx->sq_data = sqd; |
69fb2131 JA |
7858 | io_sq_thread_park(sqd); |
7859 | mutex_lock(&sqd->ctx_lock); | |
7860 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
7861 | mutex_unlock(&sqd->ctx_lock); | |
7862 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 7863 | |
917257da JA |
7864 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7865 | if (!ctx->sq_thread_idle) | |
7866 | ctx->sq_thread_idle = HZ; | |
7867 | ||
aa06165d | 7868 | if (sqd->thread) |
5aa75ed5 | 7869 | return 0; |
aa06165d | 7870 | |
6c271ce2 | 7871 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7872 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7873 | |
917257da | 7874 | ret = -EINVAL; |
44a9bd18 JA |
7875 | if (cpu >= nr_cpu_ids) |
7876 | goto err; | |
7889f44d | 7877 | if (!cpu_online(cpu)) |
917257da JA |
7878 | goto err; |
7879 | ||
37d1e2e3 | 7880 | sqd->sq_cpu = cpu; |
6c271ce2 | 7881 | } else { |
37d1e2e3 | 7882 | sqd->sq_cpu = -1; |
6c271ce2 | 7883 | } |
37d1e2e3 JA |
7884 | |
7885 | sqd->task_pid = current->pid; | |
7886 | current->flags |= PF_IO_WORKER; | |
7887 | ret = io_wq_fork_thread(io_sq_thread, sqd); | |
7888 | current->flags &= ~PF_IO_WORKER; | |
7889 | if (ret < 0) { | |
534ca6d6 | 7890 | sqd->thread = NULL; |
6c271ce2 JA |
7891 | goto err; |
7892 | } | |
37d1e2e3 | 7893 | wait_for_completion(&sqd->completion); |
5aa75ed5 | 7894 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
0f212204 JA |
7895 | if (ret) |
7896 | goto err; | |
6c271ce2 JA |
7897 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7898 | /* Can't have SQ_AFF without SQPOLL */ | |
7899 | ret = -EINVAL; | |
7900 | goto err; | |
7901 | } | |
7902 | ||
2b188cc1 JA |
7903 | return 0; |
7904 | err: | |
37d1e2e3 | 7905 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
7906 | return ret; |
7907 | } | |
7908 | ||
7e84e1c7 SG |
7909 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7910 | { | |
534ca6d6 JA |
7911 | struct io_sq_data *sqd = ctx->sq_data; |
7912 | ||
7913 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
37d1e2e3 | 7914 | complete(&sqd->startup); |
7e84e1c7 SG |
7915 | } |
7916 | ||
a087e2b5 BM |
7917 | static inline void __io_unaccount_mem(struct user_struct *user, |
7918 | unsigned long nr_pages) | |
2b188cc1 JA |
7919 | { |
7920 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7921 | } | |
7922 | ||
a087e2b5 BM |
7923 | static inline int __io_account_mem(struct user_struct *user, |
7924 | unsigned long nr_pages) | |
2b188cc1 JA |
7925 | { |
7926 | unsigned long page_limit, cur_pages, new_pages; | |
7927 | ||
7928 | /* Don't allow more pages than we can safely lock */ | |
7929 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7930 | ||
7931 | do { | |
7932 | cur_pages = atomic_long_read(&user->locked_vm); | |
7933 | new_pages = cur_pages + nr_pages; | |
7934 | if (new_pages > page_limit) | |
7935 | return -ENOMEM; | |
7936 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7937 | new_pages) != cur_pages); | |
7938 | ||
7939 | return 0; | |
7940 | } | |
7941 | ||
26bfa89e | 7942 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7943 | { |
62e398be | 7944 | if (ctx->user) |
a087e2b5 | 7945 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7946 | |
26bfa89e JA |
7947 | if (ctx->mm_account) |
7948 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7949 | } |
7950 | ||
26bfa89e | 7951 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7952 | { |
30975825 BM |
7953 | int ret; |
7954 | ||
62e398be | 7955 | if (ctx->user) { |
30975825 BM |
7956 | ret = __io_account_mem(ctx->user, nr_pages); |
7957 | if (ret) | |
7958 | return ret; | |
7959 | } | |
7960 | ||
26bfa89e JA |
7961 | if (ctx->mm_account) |
7962 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7963 | |
7964 | return 0; | |
7965 | } | |
7966 | ||
2b188cc1 JA |
7967 | static void io_mem_free(void *ptr) |
7968 | { | |
52e04ef4 MR |
7969 | struct page *page; |
7970 | ||
7971 | if (!ptr) | |
7972 | return; | |
2b188cc1 | 7973 | |
52e04ef4 | 7974 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7975 | if (put_page_testzero(page)) |
7976 | free_compound_page(page); | |
7977 | } | |
7978 | ||
7979 | static void *io_mem_alloc(size_t size) | |
7980 | { | |
7981 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 7982 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
7983 | |
7984 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
7985 | } | |
7986 | ||
75b28aff HV |
7987 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
7988 | size_t *sq_offset) | |
7989 | { | |
7990 | struct io_rings *rings; | |
7991 | size_t off, sq_array_size; | |
7992 | ||
7993 | off = struct_size(rings, cqes, cq_entries); | |
7994 | if (off == SIZE_MAX) | |
7995 | return SIZE_MAX; | |
7996 | ||
7997 | #ifdef CONFIG_SMP | |
7998 | off = ALIGN(off, SMP_CACHE_BYTES); | |
7999 | if (off == 0) | |
8000 | return SIZE_MAX; | |
8001 | #endif | |
8002 | ||
b36200f5 DV |
8003 | if (sq_offset) |
8004 | *sq_offset = off; | |
8005 | ||
75b28aff HV |
8006 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8007 | if (sq_array_size == SIZE_MAX) | |
8008 | return SIZE_MAX; | |
8009 | ||
8010 | if (check_add_overflow(off, sq_array_size, &off)) | |
8011 | return SIZE_MAX; | |
8012 | ||
75b28aff HV |
8013 | return off; |
8014 | } | |
8015 | ||
0a96bbe4 | 8016 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8017 | { |
8018 | int i, j; | |
8019 | ||
8020 | if (!ctx->user_bufs) | |
8021 | return -ENXIO; | |
8022 | ||
8023 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8024 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8025 | ||
8026 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8027 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8028 | |
de293938 | 8029 | if (imu->acct_pages) |
26bfa89e | 8030 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8031 | kvfree(imu->bvec); |
edafccee JA |
8032 | imu->nr_bvecs = 0; |
8033 | } | |
8034 | ||
8035 | kfree(ctx->user_bufs); | |
8036 | ctx->user_bufs = NULL; | |
8037 | ctx->nr_user_bufs = 0; | |
8038 | return 0; | |
8039 | } | |
8040 | ||
8041 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8042 | void __user *arg, unsigned index) | |
8043 | { | |
8044 | struct iovec __user *src; | |
8045 | ||
8046 | #ifdef CONFIG_COMPAT | |
8047 | if (ctx->compat) { | |
8048 | struct compat_iovec __user *ciovs; | |
8049 | struct compat_iovec ciov; | |
8050 | ||
8051 | ciovs = (struct compat_iovec __user *) arg; | |
8052 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8053 | return -EFAULT; | |
8054 | ||
d55e5f5b | 8055 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8056 | dst->iov_len = ciov.iov_len; |
8057 | return 0; | |
8058 | } | |
8059 | #endif | |
8060 | src = (struct iovec __user *) arg; | |
8061 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8062 | return -EFAULT; | |
8063 | return 0; | |
8064 | } | |
8065 | ||
de293938 JA |
8066 | /* |
8067 | * Not super efficient, but this is just a registration time. And we do cache | |
8068 | * the last compound head, so generally we'll only do a full search if we don't | |
8069 | * match that one. | |
8070 | * | |
8071 | * We check if the given compound head page has already been accounted, to | |
8072 | * avoid double accounting it. This allows us to account the full size of the | |
8073 | * page, not just the constituent pages of a huge page. | |
8074 | */ | |
8075 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8076 | int nr_pages, struct page *hpage) | |
8077 | { | |
8078 | int i, j; | |
8079 | ||
8080 | /* check current page array */ | |
8081 | for (i = 0; i < nr_pages; i++) { | |
8082 | if (!PageCompound(pages[i])) | |
8083 | continue; | |
8084 | if (compound_head(pages[i]) == hpage) | |
8085 | return true; | |
8086 | } | |
8087 | ||
8088 | /* check previously registered pages */ | |
8089 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8090 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8091 | ||
8092 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8093 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8094 | continue; | |
8095 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8096 | return true; | |
8097 | } | |
8098 | } | |
8099 | ||
8100 | return false; | |
8101 | } | |
8102 | ||
8103 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8104 | int nr_pages, struct io_mapped_ubuf *imu, | |
8105 | struct page **last_hpage) | |
8106 | { | |
8107 | int i, ret; | |
8108 | ||
8109 | for (i = 0; i < nr_pages; i++) { | |
8110 | if (!PageCompound(pages[i])) { | |
8111 | imu->acct_pages++; | |
8112 | } else { | |
8113 | struct page *hpage; | |
8114 | ||
8115 | hpage = compound_head(pages[i]); | |
8116 | if (hpage == *last_hpage) | |
8117 | continue; | |
8118 | *last_hpage = hpage; | |
8119 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8120 | continue; | |
8121 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8122 | } | |
8123 | } | |
8124 | ||
8125 | if (!imu->acct_pages) | |
8126 | return 0; | |
8127 | ||
26bfa89e | 8128 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8129 | if (ret) |
8130 | imu->acct_pages = 0; | |
8131 | return ret; | |
8132 | } | |
8133 | ||
0a96bbe4 BM |
8134 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8135 | struct io_mapped_ubuf *imu, | |
8136 | struct page **last_hpage) | |
edafccee JA |
8137 | { |
8138 | struct vm_area_struct **vmas = NULL; | |
8139 | struct page **pages = NULL; | |
0a96bbe4 BM |
8140 | unsigned long off, start, end, ubuf; |
8141 | size_t size; | |
8142 | int ret, pret, nr_pages, i; | |
8143 | ||
8144 | ubuf = (unsigned long) iov->iov_base; | |
8145 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8146 | start = ubuf >> PAGE_SHIFT; | |
8147 | nr_pages = end - start; | |
8148 | ||
8149 | ret = -ENOMEM; | |
8150 | ||
8151 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8152 | if (!pages) | |
8153 | goto done; | |
8154 | ||
8155 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8156 | GFP_KERNEL); | |
8157 | if (!vmas) | |
8158 | goto done; | |
edafccee | 8159 | |
0a96bbe4 BM |
8160 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8161 | GFP_KERNEL); | |
8162 | if (!imu->bvec) | |
8163 | goto done; | |
8164 | ||
8165 | ret = 0; | |
8166 | mmap_read_lock(current->mm); | |
8167 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8168 | pages, vmas); | |
8169 | if (pret == nr_pages) { | |
8170 | /* don't support file backed memory */ | |
8171 | for (i = 0; i < nr_pages; i++) { | |
8172 | struct vm_area_struct *vma = vmas[i]; | |
8173 | ||
8174 | if (vma->vm_file && | |
8175 | !is_file_hugepages(vma->vm_file)) { | |
8176 | ret = -EOPNOTSUPP; | |
8177 | break; | |
8178 | } | |
8179 | } | |
8180 | } else { | |
8181 | ret = pret < 0 ? pret : -EFAULT; | |
8182 | } | |
8183 | mmap_read_unlock(current->mm); | |
8184 | if (ret) { | |
8185 | /* | |
8186 | * if we did partial map, or found file backed vmas, | |
8187 | * release any pages we did get | |
8188 | */ | |
8189 | if (pret > 0) | |
8190 | unpin_user_pages(pages, pret); | |
8191 | kvfree(imu->bvec); | |
8192 | goto done; | |
8193 | } | |
8194 | ||
8195 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8196 | if (ret) { | |
8197 | unpin_user_pages(pages, pret); | |
8198 | kvfree(imu->bvec); | |
8199 | goto done; | |
8200 | } | |
8201 | ||
8202 | off = ubuf & ~PAGE_MASK; | |
8203 | size = iov->iov_len; | |
8204 | for (i = 0; i < nr_pages; i++) { | |
8205 | size_t vec_len; | |
8206 | ||
8207 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8208 | imu->bvec[i].bv_page = pages[i]; | |
8209 | imu->bvec[i].bv_len = vec_len; | |
8210 | imu->bvec[i].bv_offset = off; | |
8211 | off = 0; | |
8212 | size -= vec_len; | |
8213 | } | |
8214 | /* store original address for later verification */ | |
8215 | imu->ubuf = ubuf; | |
8216 | imu->len = iov->iov_len; | |
8217 | imu->nr_bvecs = nr_pages; | |
8218 | ret = 0; | |
8219 | done: | |
8220 | kvfree(pages); | |
8221 | kvfree(vmas); | |
8222 | return ret; | |
8223 | } | |
8224 | ||
2b358604 | 8225 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8226 | { |
edafccee JA |
8227 | if (ctx->user_bufs) |
8228 | return -EBUSY; | |
8229 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8230 | return -EINVAL; | |
8231 | ||
8232 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8233 | GFP_KERNEL); | |
8234 | if (!ctx->user_bufs) | |
8235 | return -ENOMEM; | |
8236 | ||
2b358604 BM |
8237 | return 0; |
8238 | } | |
edafccee | 8239 | |
2b358604 BM |
8240 | static int io_buffer_validate(struct iovec *iov) |
8241 | { | |
8242 | /* | |
8243 | * Don't impose further limits on the size and buffer | |
8244 | * constraints here, we'll -EINVAL later when IO is | |
8245 | * submitted if they are wrong. | |
8246 | */ | |
8247 | if (!iov->iov_base || !iov->iov_len) | |
8248 | return -EFAULT; | |
edafccee | 8249 | |
2b358604 BM |
8250 | /* arbitrary limit, but we need something */ |
8251 | if (iov->iov_len > SZ_1G) | |
8252 | return -EFAULT; | |
edafccee | 8253 | |
2b358604 BM |
8254 | return 0; |
8255 | } | |
edafccee | 8256 | |
2b358604 BM |
8257 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8258 | unsigned int nr_args) | |
8259 | { | |
8260 | int i, ret; | |
8261 | struct iovec iov; | |
8262 | struct page *last_hpage = NULL; | |
edafccee | 8263 | |
2b358604 BM |
8264 | ret = io_buffers_map_alloc(ctx, nr_args); |
8265 | if (ret) | |
8266 | return ret; | |
edafccee | 8267 | |
edafccee JA |
8268 | for (i = 0; i < nr_args; i++) { |
8269 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8270 | |
edafccee JA |
8271 | ret = io_copy_iov(ctx, &iov, arg, i); |
8272 | if (ret) | |
0a96bbe4 | 8273 | break; |
de293938 | 8274 | |
2b358604 BM |
8275 | ret = io_buffer_validate(&iov); |
8276 | if (ret) | |
0a96bbe4 | 8277 | break; |
edafccee | 8278 | |
0a96bbe4 BM |
8279 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8280 | if (ret) | |
8281 | break; | |
edafccee JA |
8282 | |
8283 | ctx->nr_user_bufs++; | |
8284 | } | |
0a96bbe4 BM |
8285 | |
8286 | if (ret) | |
8287 | io_sqe_buffers_unregister(ctx); | |
8288 | ||
edafccee JA |
8289 | return ret; |
8290 | } | |
8291 | ||
9b402849 JA |
8292 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8293 | { | |
8294 | __s32 __user *fds = arg; | |
8295 | int fd; | |
8296 | ||
8297 | if (ctx->cq_ev_fd) | |
8298 | return -EBUSY; | |
8299 | ||
8300 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8301 | return -EFAULT; | |
8302 | ||
8303 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8304 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8305 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8306 | ctx->cq_ev_fd = NULL; | |
8307 | return ret; | |
8308 | } | |
8309 | ||
8310 | return 0; | |
8311 | } | |
8312 | ||
8313 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8314 | { | |
8315 | if (ctx->cq_ev_fd) { | |
8316 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8317 | ctx->cq_ev_fd = NULL; | |
8318 | return 0; | |
8319 | } | |
8320 | ||
8321 | return -ENXIO; | |
8322 | } | |
8323 | ||
5a2e745d JA |
8324 | static int __io_destroy_buffers(int id, void *p, void *data) |
8325 | { | |
8326 | struct io_ring_ctx *ctx = data; | |
8327 | struct io_buffer *buf = p; | |
8328 | ||
067524e9 | 8329 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8330 | return 0; |
8331 | } | |
8332 | ||
8333 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8334 | { | |
8335 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8336 | idr_destroy(&ctx->io_buffer_idr); | |
8337 | } | |
8338 | ||
68e68ee6 | 8339 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8340 | { |
68e68ee6 | 8341 | struct io_kiocb *req, *nxt; |
1b4c351f | 8342 | |
68e68ee6 JA |
8343 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8344 | if (tsk && req->task != tsk) | |
8345 | continue; | |
1b4c351f JA |
8346 | list_del(&req->compl.list); |
8347 | kmem_cache_free(req_cachep, req); | |
8348 | } | |
8349 | } | |
8350 | ||
9a4fdbd8 | 8351 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
2b188cc1 | 8352 | { |
bf019da7 PB |
8353 | struct io_submit_state *submit_state = &ctx->submit_state; |
8354 | ||
9a4fdbd8 JA |
8355 | mutex_lock(&ctx->uring_lock); |
8356 | ||
8357 | if (submit_state->free_reqs) | |
8358 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, | |
8359 | submit_state->reqs); | |
8360 | ||
8361 | io_req_cache_free(&submit_state->comp.free_list, NULL); | |
8362 | ||
8363 | spin_lock_irq(&ctx->completion_lock); | |
8364 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); | |
8365 | spin_unlock_irq(&ctx->completion_lock); | |
8366 | ||
8367 | mutex_unlock(&ctx->uring_lock); | |
8368 | } | |
8369 | ||
2b188cc1 JA |
8370 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8371 | { | |
04fc6c80 PB |
8372 | /* |
8373 | * Some may use context even when all refs and requests have been put, | |
8374 | * and they are free to do so while still holding uring_lock, see | |
8375 | * __io_req_task_submit(). Wait for them to finish. | |
8376 | */ | |
8377 | mutex_lock(&ctx->uring_lock); | |
8378 | mutex_unlock(&ctx->uring_lock); | |
8379 | ||
37d1e2e3 | 8380 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8381 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8382 | |
37d1e2e3 | 8383 | if (ctx->mm_account) { |
2aede0e4 JA |
8384 | mmdrop(ctx->mm_account); |
8385 | ctx->mm_account = NULL; | |
30975825 | 8386 | } |
def596e9 | 8387 | |
8bad28d8 | 8388 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8389 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8390 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8391 | io_eventfd_unregister(ctx); |
5a2e745d | 8392 | io_destroy_buffers(ctx); |
41726c9a | 8393 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8394 | |
2b188cc1 | 8395 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8396 | if (ctx->ring_sock) { |
8397 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8398 | sock_release(ctx->ring_sock); |
355e8d26 | 8399 | } |
2b188cc1 JA |
8400 | #endif |
8401 | ||
75b28aff | 8402 | io_mem_free(ctx->rings); |
2b188cc1 | 8403 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8404 | |
8405 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8406 | free_uid(ctx->user); |
9a4fdbd8 | 8407 | io_req_caches_free(ctx, NULL); |
78076bb6 | 8408 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8409 | kfree(ctx); |
8410 | } | |
8411 | ||
8412 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8413 | { | |
8414 | struct io_ring_ctx *ctx = file->private_data; | |
8415 | __poll_t mask = 0; | |
8416 | ||
8417 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8418 | /* |
8419 | * synchronizes with barrier from wq_has_sleeper call in | |
8420 | * io_commit_cqring | |
8421 | */ | |
2b188cc1 | 8422 | smp_rmb(); |
90554200 | 8423 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8424 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8425 | |
8426 | /* | |
8427 | * Don't flush cqring overflow list here, just do a simple check. | |
8428 | * Otherwise there could possible be ABBA deadlock: | |
8429 | * CPU0 CPU1 | |
8430 | * ---- ---- | |
8431 | * lock(&ctx->uring_lock); | |
8432 | * lock(&ep->mtx); | |
8433 | * lock(&ctx->uring_lock); | |
8434 | * lock(&ep->mtx); | |
8435 | * | |
8436 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8437 | * pushs them to do the flush. | |
8438 | */ | |
8439 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8440 | mask |= EPOLLIN | EPOLLRDNORM; |
8441 | ||
8442 | return mask; | |
8443 | } | |
8444 | ||
8445 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8446 | { | |
8447 | struct io_ring_ctx *ctx = file->private_data; | |
8448 | ||
8449 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8450 | } | |
8451 | ||
0bead8cd | 8452 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8453 | { |
4379bf8b | 8454 | const struct cred *creds; |
071698e1 | 8455 | |
4379bf8b JA |
8456 | creds = idr_remove(&ctx->personality_idr, id); |
8457 | if (creds) { | |
8458 | put_cred(creds); | |
0bead8cd | 8459 | return 0; |
1e6fa521 | 8460 | } |
0bead8cd YD |
8461 | |
8462 | return -EINVAL; | |
8463 | } | |
8464 | ||
8465 | static int io_remove_personalities(int id, void *p, void *data) | |
8466 | { | |
8467 | struct io_ring_ctx *ctx = data; | |
8468 | ||
8469 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8470 | return 0; |
8471 | } | |
8472 | ||
7c25c0d1 JA |
8473 | static void io_run_ctx_fallback(struct io_ring_ctx *ctx) |
8474 | { | |
8475 | struct callback_head *work, *head, *next; | |
8476 | ||
8477 | do { | |
8478 | do { | |
8479 | head = NULL; | |
8480 | work = READ_ONCE(ctx->exit_task_work); | |
8481 | } while (cmpxchg(&ctx->exit_task_work, work, head) != work); | |
8482 | ||
8483 | if (!work) | |
8484 | break; | |
8485 | ||
8486 | do { | |
8487 | next = work->next; | |
8488 | work->func(work); | |
8489 | work = next; | |
8490 | cond_resched(); | |
8491 | } while (work); | |
8492 | } while (1); | |
8493 | } | |
8494 | ||
85faa7b8 JA |
8495 | static void io_ring_exit_work(struct work_struct *work) |
8496 | { | |
b2edc0a7 PB |
8497 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8498 | exit_work); | |
85faa7b8 | 8499 | |
56952e91 JA |
8500 | /* |
8501 | * If we're doing polled IO and end up having requests being | |
8502 | * submitted async (out-of-line), then completions can come in while | |
8503 | * we're waiting for refs to drop. We need to reap these manually, | |
8504 | * as nobody else will be looking for them. | |
8505 | */ | |
b2edc0a7 | 8506 | do { |
9936c7c2 | 8507 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
7c25c0d1 | 8508 | io_run_ctx_fallback(ctx); |
b2edc0a7 | 8509 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8510 | io_ring_ctx_free(ctx); |
8511 | } | |
8512 | ||
2b188cc1 JA |
8513 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8514 | { | |
8515 | mutex_lock(&ctx->uring_lock); | |
8516 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8517 | |
8518 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8519 | ctx->sqo_dead = 1; | |
8520 | ||
cda286f0 PB |
8521 | /* if force is set, the ring is going away. always drop after that */ |
8522 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8523 | if (ctx->rings) |
6c503150 | 8524 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8525 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8526 | mutex_unlock(&ctx->uring_lock); |
8527 | ||
6b81928d PB |
8528 | io_kill_timeouts(ctx, NULL, NULL); |
8529 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8530 | |
15dff286 | 8531 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8532 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8533 | |
85faa7b8 | 8534 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8535 | /* |
8536 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8537 | * if we're exiting a ton of rings at the same time. It just adds | |
8538 | * noise and overhead, there's no discernable change in runtime | |
8539 | * over using system_wq. | |
8540 | */ | |
8541 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8542 | } |
8543 | ||
8544 | static int io_uring_release(struct inode *inode, struct file *file) | |
8545 | { | |
8546 | struct io_ring_ctx *ctx = file->private_data; | |
8547 | ||
8548 | file->private_data = NULL; | |
8549 | io_ring_ctx_wait_and_kill(ctx); | |
8550 | return 0; | |
8551 | } | |
8552 | ||
f6edbabb PB |
8553 | struct io_task_cancel { |
8554 | struct task_struct *task; | |
8555 | struct files_struct *files; | |
8556 | }; | |
f254ac04 | 8557 | |
f6edbabb | 8558 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8559 | { |
9a472ef7 | 8560 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8561 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8562 | bool ret; |
8563 | ||
f6edbabb | 8564 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8565 | unsigned long flags; |
8566 | struct io_ring_ctx *ctx = req->ctx; | |
8567 | ||
8568 | /* protect against races with linked timeouts */ | |
8569 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8570 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8571 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8572 | } else { | |
f6edbabb | 8573 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8574 | } |
8575 | return ret; | |
b711d4ea JA |
8576 | } |
8577 | ||
b7ddce3c | 8578 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8579 | struct task_struct *task, |
b7ddce3c PB |
8580 | struct files_struct *files) |
8581 | { | |
8582 | struct io_defer_entry *de = NULL; | |
8583 | LIST_HEAD(list); | |
8584 | ||
8585 | spin_lock_irq(&ctx->completion_lock); | |
8586 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8587 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8588 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8589 | break; | |
8590 | } | |
8591 | } | |
8592 | spin_unlock_irq(&ctx->completion_lock); | |
8593 | ||
8594 | while (!list_empty(&list)) { | |
8595 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8596 | list_del_init(&de->list); | |
8597 | req_set_fail_links(de->req); | |
8598 | io_put_req(de->req); | |
8599 | io_req_complete(de->req, -ECANCELED); | |
8600 | kfree(de); | |
8601 | } | |
8602 | } | |
8603 | ||
9936c7c2 PB |
8604 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8605 | struct task_struct *task, | |
8606 | struct files_struct *files) | |
8607 | { | |
8608 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
5aa75ed5 | 8609 | struct io_uring_task *tctx = current->io_uring; |
9936c7c2 PB |
8610 | |
8611 | while (1) { | |
8612 | enum io_wq_cancel cret; | |
8613 | bool ret = false; | |
8614 | ||
5aa75ed5 JA |
8615 | if (tctx && tctx->io_wq) { |
8616 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, | |
9936c7c2 PB |
8617 | &cancel, true); |
8618 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8619 | } | |
8620 | ||
8621 | /* SQPOLL thread does its own polling */ | |
8622 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8623 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8624 | io_iopoll_try_reap_events(ctx); | |
8625 | ret = true; | |
8626 | } | |
8627 | } | |
8628 | ||
8629 | ret |= io_poll_remove_all(ctx, task, files); | |
8630 | ret |= io_kill_timeouts(ctx, task, files); | |
8631 | ret |= io_run_task_work(); | |
8632 | io_cqring_overflow_flush(ctx, true, task, files); | |
8633 | if (!ret) | |
8634 | break; | |
8635 | cond_resched(); | |
8636 | } | |
8637 | } | |
8638 | ||
ca70f00b PB |
8639 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8640 | struct task_struct *task, | |
8641 | struct files_struct *files) | |
8642 | { | |
8643 | struct io_kiocb *req; | |
8644 | int cnt = 0; | |
8645 | ||
8646 | spin_lock_irq(&ctx->inflight_lock); | |
8647 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8648 | cnt += io_match_task(req, task, files); | |
8649 | spin_unlock_irq(&ctx->inflight_lock); | |
8650 | return cnt; | |
8651 | } | |
8652 | ||
b52fda00 | 8653 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8654 | struct task_struct *task, |
fcb323cc JA |
8655 | struct files_struct *files) |
8656 | { | |
fcb323cc | 8657 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 8658 | DEFINE_WAIT(wait); |
ca70f00b | 8659 | int inflight; |
fcb323cc | 8660 | |
ca70f00b PB |
8661 | inflight = io_uring_count_inflight(ctx, task, files); |
8662 | if (!inflight) | |
fcb323cc | 8663 | break; |
f6edbabb | 8664 | |
9936c7c2 | 8665 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 8666 | |
34343786 PB |
8667 | if (ctx->sq_data) |
8668 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
8669 | prepare_to_wait(&task->io_uring->wait, &wait, |
8670 | TASK_UNINTERRUPTIBLE); | |
8671 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8672 | schedule(); | |
c98de08c | 8673 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
8674 | if (ctx->sq_data) |
8675 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 8676 | } |
0f212204 JA |
8677 | } |
8678 | ||
d9d05217 PB |
8679 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
8680 | { | |
d9d05217 PB |
8681 | mutex_lock(&ctx->uring_lock); |
8682 | ctx->sqo_dead = 1; | |
8683 | mutex_unlock(&ctx->uring_lock); | |
8684 | ||
8685 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
8686 | if (ctx->rings) |
8687 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
8688 | } |
8689 | ||
0f212204 JA |
8690 | /* |
8691 | * We need to iteratively cancel requests, in case a request has dependent | |
8692 | * hard links. These persist even for failure of cancelations, hence keep | |
8693 | * looping until none are found. | |
8694 | */ | |
8695 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8696 | struct files_struct *files) | |
8697 | { | |
8698 | struct task_struct *task = current; | |
37d1e2e3 | 8699 | bool did_park = false; |
0f212204 | 8700 | |
fdaf083c | 8701 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 8702 | io_disable_sqo_submit(ctx); |
37d1e2e3 JA |
8703 | did_park = io_sq_thread_park(ctx->sq_data); |
8704 | if (did_park) { | |
8705 | task = ctx->sq_data->thread; | |
8706 | atomic_inc(&task->io_uring->in_idle); | |
8707 | } | |
fdaf083c | 8708 | } |
0f212204 | 8709 | |
df9923f9 | 8710 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 8711 | |
3a7efd1a | 8712 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 8713 | if (!files) |
9936c7c2 | 8714 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c | 8715 | |
37d1e2e3 | 8716 | if (did_park) { |
fdaf083c | 8717 | atomic_dec(&task->io_uring->in_idle); |
fdaf083c JA |
8718 | io_sq_thread_unpark(ctx->sq_data); |
8719 | } | |
0f212204 JA |
8720 | } |
8721 | ||
8722 | /* | |
8723 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8724 | */ | |
fdaf083c | 8725 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8726 | { |
236434c3 | 8727 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 8728 | int ret; |
236434c3 MWO |
8729 | |
8730 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8731 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8732 | if (unlikely(ret)) |
8733 | return ret; | |
236434c3 | 8734 | tctx = current->io_uring; |
0f212204 | 8735 | } |
236434c3 MWO |
8736 | if (tctx->last != file) { |
8737 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8738 | |
236434c3 | 8739 | if (!old) { |
0f212204 | 8740 | get_file(file); |
a528b04e PB |
8741 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
8742 | file, GFP_KERNEL)); | |
8743 | if (ret) { | |
8744 | fput(file); | |
8745 | return ret; | |
8746 | } | |
ecfc8492 PB |
8747 | |
8748 | /* one and only SQPOLL file note, held by sqo_task */ | |
8749 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
8750 | current != ctx->sqo_task); | |
0f212204 | 8751 | } |
236434c3 | 8752 | tctx->last = file; |
0f212204 JA |
8753 | } |
8754 | ||
fdaf083c JA |
8755 | /* |
8756 | * This is race safe in that the task itself is doing this, hence it | |
8757 | * cannot be going through the exit/cancel paths at the same time. | |
8758 | * This cannot be modified while exit/cancel is running. | |
8759 | */ | |
8760 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8761 | tctx->sqpoll = true; | |
8762 | ||
0f212204 JA |
8763 | return 0; |
8764 | } | |
8765 | ||
8766 | /* | |
8767 | * Remove this io_uring_file -> task mapping. | |
8768 | */ | |
8769 | static void io_uring_del_task_file(struct file *file) | |
8770 | { | |
8771 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
8772 | |
8773 | if (tctx->last == file) | |
8774 | tctx->last = NULL; | |
5e2ed8c4 | 8775 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
8776 | if (file) |
8777 | fput(file); | |
8778 | } | |
8779 | ||
de7f1d9e PB |
8780 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
8781 | { | |
8782 | struct file *file; | |
8783 | unsigned long index; | |
8784 | ||
8785 | xa_for_each(&tctx->xa, index, file) | |
8786 | io_uring_del_task_file(file); | |
8787 | } | |
8788 | ||
0f212204 JA |
8789 | void __io_uring_files_cancel(struct files_struct *files) |
8790 | { | |
8791 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
8792 | struct file *file; |
8793 | unsigned long index; | |
0f212204 JA |
8794 | |
8795 | /* make sure overflow events are dropped */ | |
fdaf083c | 8796 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
8797 | xa_for_each(&tctx->xa, index, file) |
8798 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 8799 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8800 | |
5aa75ed5 | 8801 | if (files) { |
de7f1d9e | 8802 | io_uring_remove_task_files(tctx); |
5aa75ed5 JA |
8803 | } else if (tctx->io_wq && current->flags & PF_EXITING) { |
8804 | io_wq_destroy(tctx->io_wq); | |
8805 | tctx->io_wq = NULL; | |
8806 | } | |
fdaf083c JA |
8807 | } |
8808 | ||
8809 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8810 | { | |
0e9ddb39 PB |
8811 | return percpu_counter_sum(&tctx->inflight); |
8812 | } | |
fdaf083c | 8813 | |
0e9ddb39 PB |
8814 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8815 | { | |
37d1e2e3 | 8816 | struct io_sq_data *sqd = ctx->sq_data; |
0e9ddb39 PB |
8817 | struct io_uring_task *tctx; |
8818 | s64 inflight; | |
8819 | DEFINE_WAIT(wait); | |
fdaf083c | 8820 | |
37d1e2e3 | 8821 | if (!sqd) |
0e9ddb39 | 8822 | return; |
0e9ddb39 | 8823 | io_disable_sqo_submit(ctx); |
37d1e2e3 JA |
8824 | if (!io_sq_thread_park(sqd)) |
8825 | return; | |
8826 | tctx = ctx->sq_data->thread->io_uring; | |
fdaf083c | 8827 | |
0e9ddb39 PB |
8828 | atomic_inc(&tctx->in_idle); |
8829 | do { | |
8830 | /* read completions before cancelations */ | |
8831 | inflight = tctx_inflight(tctx); | |
8832 | if (!inflight) | |
8833 | break; | |
8834 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 8835 | |
0e9ddb39 PB |
8836 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
8837 | /* | |
8838 | * If we've seen completions, retry without waiting. This | |
8839 | * avoids a race where a completion comes in before we did | |
8840 | * prepare_to_wait(). | |
8841 | */ | |
8842 | if (inflight == tctx_inflight(tctx)) | |
8843 | schedule(); | |
8844 | finish_wait(&tctx->wait, &wait); | |
8845 | } while (1); | |
8846 | atomic_dec(&tctx->in_idle); | |
37d1e2e3 | 8847 | io_sq_thread_unpark(sqd); |
0f212204 JA |
8848 | } |
8849 | ||
0f212204 JA |
8850 | /* |
8851 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8852 | * requests. | |
8853 | */ | |
8854 | void __io_uring_task_cancel(void) | |
8855 | { | |
8856 | struct io_uring_task *tctx = current->io_uring; | |
8857 | DEFINE_WAIT(wait); | |
d8a6df10 | 8858 | s64 inflight; |
0f212204 JA |
8859 | |
8860 | /* make sure overflow events are dropped */ | |
fdaf083c | 8861 | atomic_inc(&tctx->in_idle); |
0f212204 | 8862 | |
0b5cd6c3 | 8863 | /* trigger io_disable_sqo_submit() */ |
0e9ddb39 PB |
8864 | if (tctx->sqpoll) { |
8865 | struct file *file; | |
8866 | unsigned long index; | |
8867 | ||
8868 | xa_for_each(&tctx->xa, index, file) | |
8869 | io_uring_cancel_sqpoll(file->private_data); | |
8870 | } | |
0b5cd6c3 | 8871 | |
d8a6df10 | 8872 | do { |
0f212204 | 8873 | /* read completions before cancelations */ |
fdaf083c | 8874 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
8875 | if (!inflight) |
8876 | break; | |
0f212204 JA |
8877 | __io_uring_files_cancel(NULL); |
8878 | ||
8879 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8880 | ||
8881 | /* | |
a1bb3cd5 PB |
8882 | * If we've seen completions, retry without waiting. This |
8883 | * avoids a race where a completion comes in before we did | |
8884 | * prepare_to_wait(). | |
0f212204 | 8885 | */ |
a1bb3cd5 PB |
8886 | if (inflight == tctx_inflight(tctx)) |
8887 | schedule(); | |
f57555ed | 8888 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 8889 | } while (1); |
0f212204 | 8890 | |
fdaf083c | 8891 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
8892 | |
8893 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
8894 | } |
8895 | ||
fcb323cc JA |
8896 | static int io_uring_flush(struct file *file, void *data) |
8897 | { | |
6b5733eb | 8898 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 8899 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 8900 | |
3bfe6106 JA |
8901 | /* Ignore helper thread files exit */ |
8902 | if (current->flags & PF_IO_WORKER) | |
8903 | return 0; | |
8904 | ||
41be53e9 | 8905 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
84965ff8 | 8906 | io_uring_cancel_task_requests(ctx, NULL); |
41be53e9 JA |
8907 | io_req_caches_free(ctx, current); |
8908 | } | |
84965ff8 | 8909 | |
7c25c0d1 JA |
8910 | io_run_ctx_fallback(ctx); |
8911 | ||
6b5733eb | 8912 | if (!tctx) |
4f793dc4 PB |
8913 | return 0; |
8914 | ||
6b5733eb PB |
8915 | /* we should have cancelled and erased it before PF_EXITING */ |
8916 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
8917 | xa_load(&tctx->xa, (unsigned long)file)); | |
8918 | ||
4f793dc4 PB |
8919 | /* |
8920 | * fput() is pending, will be 2 if the only other ref is our potential | |
8921 | * task file note. If the task is exiting, drop regardless of count. | |
8922 | */ | |
6b5733eb PB |
8923 | if (atomic_long_read(&file->f_count) != 2) |
8924 | return 0; | |
4f793dc4 | 8925 | |
d9d05217 PB |
8926 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
8927 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
8928 | WARN_ON_ONCE(ctx->sqo_task != current && |
8929 | xa_load(&tctx->xa, (unsigned long)file)); | |
8930 | /* sqo_dead check is for when this happens after cancellation */ | |
8931 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
8932 | !xa_load(&tctx->xa, (unsigned long)file)); |
8933 | ||
8934 | io_disable_sqo_submit(ctx); | |
8935 | } | |
8936 | ||
8937 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
8938 | io_uring_del_task_file(file); | |
fcb323cc JA |
8939 | return 0; |
8940 | } | |
8941 | ||
6c5c240e RP |
8942 | static void *io_uring_validate_mmap_request(struct file *file, |
8943 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8944 | { |
2b188cc1 | 8945 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8946 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8947 | struct page *page; |
8948 | void *ptr; | |
8949 | ||
8950 | switch (offset) { | |
8951 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8952 | case IORING_OFF_CQ_RING: |
8953 | ptr = ctx->rings; | |
2b188cc1 JA |
8954 | break; |
8955 | case IORING_OFF_SQES: | |
8956 | ptr = ctx->sq_sqes; | |
8957 | break; | |
2b188cc1 | 8958 | default: |
6c5c240e | 8959 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8960 | } |
8961 | ||
8962 | page = virt_to_head_page(ptr); | |
a50b854e | 8963 | if (sz > page_size(page)) |
6c5c240e RP |
8964 | return ERR_PTR(-EINVAL); |
8965 | ||
8966 | return ptr; | |
8967 | } | |
8968 | ||
8969 | #ifdef CONFIG_MMU | |
8970 | ||
8971 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8972 | { | |
8973 | size_t sz = vma->vm_end - vma->vm_start; | |
8974 | unsigned long pfn; | |
8975 | void *ptr; | |
8976 | ||
8977 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8978 | if (IS_ERR(ptr)) | |
8979 | return PTR_ERR(ptr); | |
2b188cc1 JA |
8980 | |
8981 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
8982 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
8983 | } | |
8984 | ||
6c5c240e RP |
8985 | #else /* !CONFIG_MMU */ |
8986 | ||
8987 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8988 | { | |
8989 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
8990 | } | |
8991 | ||
8992 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
8993 | { | |
8994 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
8995 | } | |
8996 | ||
8997 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
8998 | unsigned long addr, unsigned long len, | |
8999 | unsigned long pgoff, unsigned long flags) | |
9000 | { | |
9001 | void *ptr; | |
9002 | ||
9003 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9004 | if (IS_ERR(ptr)) | |
9005 | return PTR_ERR(ptr); | |
9006 | ||
9007 | return (unsigned long) ptr; | |
9008 | } | |
9009 | ||
9010 | #endif /* !CONFIG_MMU */ | |
9011 | ||
d9d05217 | 9012 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9013 | { |
d9d05217 | 9014 | int ret = 0; |
90554200 JA |
9015 | DEFINE_WAIT(wait); |
9016 | ||
9017 | do { | |
9018 | if (!io_sqring_full(ctx)) | |
9019 | break; | |
9020 | ||
9021 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9022 | ||
d9d05217 PB |
9023 | if (unlikely(ctx->sqo_dead)) { |
9024 | ret = -EOWNERDEAD; | |
9025 | goto out; | |
9026 | } | |
9027 | ||
90554200 JA |
9028 | if (!io_sqring_full(ctx)) |
9029 | break; | |
9030 | ||
9031 | schedule(); | |
9032 | } while (!signal_pending(current)); | |
9033 | ||
9034 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9035 | out: |
9036 | return ret; | |
90554200 JA |
9037 | } |
9038 | ||
c73ebb68 HX |
9039 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9040 | struct __kernel_timespec __user **ts, | |
9041 | const sigset_t __user **sig) | |
9042 | { | |
9043 | struct io_uring_getevents_arg arg; | |
9044 | ||
9045 | /* | |
9046 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9047 | * is just a pointer to the sigset_t. | |
9048 | */ | |
9049 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9050 | *sig = (const sigset_t __user *) argp; | |
9051 | *ts = NULL; | |
9052 | return 0; | |
9053 | } | |
9054 | ||
9055 | /* | |
9056 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9057 | * timespec and sigset_t pointers if good. | |
9058 | */ | |
9059 | if (*argsz != sizeof(arg)) | |
9060 | return -EINVAL; | |
9061 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9062 | return -EFAULT; | |
9063 | *sig = u64_to_user_ptr(arg.sigmask); | |
9064 | *argsz = arg.sigmask_sz; | |
9065 | *ts = u64_to_user_ptr(arg.ts); | |
9066 | return 0; | |
9067 | } | |
9068 | ||
2b188cc1 | 9069 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9070 | u32, min_complete, u32, flags, const void __user *, argp, |
9071 | size_t, argsz) | |
2b188cc1 JA |
9072 | { |
9073 | struct io_ring_ctx *ctx; | |
9074 | long ret = -EBADF; | |
9075 | int submitted = 0; | |
9076 | struct fd f; | |
9077 | ||
4c6e277c | 9078 | io_run_task_work(); |
b41e9852 | 9079 | |
90554200 | 9080 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9081 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9082 | return -EINVAL; |
9083 | ||
9084 | f = fdget(fd); | |
9085 | if (!f.file) | |
9086 | return -EBADF; | |
9087 | ||
9088 | ret = -EOPNOTSUPP; | |
9089 | if (f.file->f_op != &io_uring_fops) | |
9090 | goto out_fput; | |
9091 | ||
9092 | ret = -ENXIO; | |
9093 | ctx = f.file->private_data; | |
9094 | if (!percpu_ref_tryget(&ctx->refs)) | |
9095 | goto out_fput; | |
9096 | ||
7e84e1c7 SG |
9097 | ret = -EBADFD; |
9098 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9099 | goto out; | |
9100 | ||
6c271ce2 JA |
9101 | /* |
9102 | * For SQ polling, the thread will do all submissions and completions. | |
9103 | * Just return the requested submit count, and wake the thread if | |
9104 | * we were asked to. | |
9105 | */ | |
b2a9eada | 9106 | ret = 0; |
6c271ce2 | 9107 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9108 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9109 | |
d9d05217 PB |
9110 | ret = -EOWNERDEAD; |
9111 | if (unlikely(ctx->sqo_dead)) | |
9112 | goto out; | |
6c271ce2 | 9113 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9114 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9115 | if (flags & IORING_ENTER_SQ_WAIT) { |
9116 | ret = io_sqpoll_wait_sq(ctx); | |
9117 | if (ret) | |
9118 | goto out; | |
9119 | } | |
6c271ce2 | 9120 | submitted = to_submit; |
b2a9eada | 9121 | } else if (to_submit) { |
fdaf083c | 9122 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9123 | if (unlikely(ret)) |
9124 | goto out; | |
2b188cc1 | 9125 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9126 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9127 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9128 | |
9129 | if (submitted != to_submit) | |
9130 | goto out; | |
2b188cc1 JA |
9131 | } |
9132 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9133 | const sigset_t __user *sig; |
9134 | struct __kernel_timespec __user *ts; | |
9135 | ||
9136 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9137 | if (unlikely(ret)) | |
9138 | goto out; | |
9139 | ||
2b188cc1 JA |
9140 | min_complete = min(min_complete, ctx->cq_entries); |
9141 | ||
32b2244a XW |
9142 | /* |
9143 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9144 | * space applications don't need to do io completion events | |
9145 | * polling again, they can rely on io_sq_thread to do polling | |
9146 | * work, which can reduce cpu usage and uring_lock contention. | |
9147 | */ | |
9148 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9149 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9150 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9151 | } else { |
c73ebb68 | 9152 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9153 | } |
2b188cc1 JA |
9154 | } |
9155 | ||
7c504e65 | 9156 | out: |
6805b32e | 9157 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9158 | out_fput: |
9159 | fdput(f); | |
9160 | return submitted ? submitted : ret; | |
9161 | } | |
9162 | ||
bebdb65e | 9163 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9164 | static int io_uring_show_cred(int id, void *p, void *data) |
9165 | { | |
4379bf8b | 9166 | const struct cred *cred = p; |
87ce955b JA |
9167 | struct seq_file *m = data; |
9168 | struct user_namespace *uns = seq_user_ns(m); | |
9169 | struct group_info *gi; | |
9170 | kernel_cap_t cap; | |
9171 | unsigned __capi; | |
9172 | int g; | |
9173 | ||
9174 | seq_printf(m, "%5d\n", id); | |
9175 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9176 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9177 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9178 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9179 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9180 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9181 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9182 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9183 | seq_puts(m, "\n\tGroups:\t"); | |
9184 | gi = cred->group_info; | |
9185 | for (g = 0; g < gi->ngroups; g++) { | |
9186 | seq_put_decimal_ull(m, g ? " " : "", | |
9187 | from_kgid_munged(uns, gi->gid[g])); | |
9188 | } | |
9189 | seq_puts(m, "\n\tCapEff:\t"); | |
9190 | cap = cred->cap_effective; | |
9191 | CAP_FOR_EACH_U32(__capi) | |
9192 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9193 | seq_putc(m, '\n'); | |
9194 | return 0; | |
9195 | } | |
9196 | ||
9197 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9198 | { | |
dbbe9c64 | 9199 | struct io_sq_data *sq = NULL; |
fad8e0de | 9200 | bool has_lock; |
87ce955b JA |
9201 | int i; |
9202 | ||
fad8e0de JA |
9203 | /* |
9204 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9205 | * since fdinfo case grabs it in the opposite direction of normal use | |
9206 | * cases. If we fail to get the lock, we just don't iterate any | |
9207 | * structures that could be going away outside the io_uring mutex. | |
9208 | */ | |
9209 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9210 | ||
dbbe9c64 JQ |
9211 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9212 | sq = ctx->sq_data; | |
9213 | ||
9214 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9215 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9216 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9217 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9218 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9219 | |
87ce955b JA |
9220 | if (f) |
9221 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9222 | else | |
9223 | seq_printf(m, "%5u: <none>\n", i); | |
9224 | } | |
9225 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9226 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9227 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9228 | ||
9229 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9230 | (unsigned int) buf->len); | |
9231 | } | |
fad8e0de | 9232 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9233 | seq_printf(m, "Personalities:\n"); |
9234 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9235 | } | |
d7718a9d JA |
9236 | seq_printf(m, "PollList:\n"); |
9237 | spin_lock_irq(&ctx->completion_lock); | |
9238 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9239 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9240 | struct io_kiocb *req; | |
9241 | ||
9242 | hlist_for_each_entry(req, list, hash_node) | |
9243 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9244 | req->task->task_works != NULL); | |
9245 | } | |
9246 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9247 | if (has_lock) |
9248 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9249 | } |
9250 | ||
9251 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9252 | { | |
9253 | struct io_ring_ctx *ctx = f->private_data; | |
9254 | ||
9255 | if (percpu_ref_tryget(&ctx->refs)) { | |
9256 | __io_uring_show_fdinfo(ctx, m); | |
9257 | percpu_ref_put(&ctx->refs); | |
9258 | } | |
9259 | } | |
bebdb65e | 9260 | #endif |
87ce955b | 9261 | |
2b188cc1 JA |
9262 | static const struct file_operations io_uring_fops = { |
9263 | .release = io_uring_release, | |
fcb323cc | 9264 | .flush = io_uring_flush, |
2b188cc1 | 9265 | .mmap = io_uring_mmap, |
6c5c240e RP |
9266 | #ifndef CONFIG_MMU |
9267 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9268 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9269 | #endif | |
2b188cc1 JA |
9270 | .poll = io_uring_poll, |
9271 | .fasync = io_uring_fasync, | |
bebdb65e | 9272 | #ifdef CONFIG_PROC_FS |
87ce955b | 9273 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9274 | #endif |
2b188cc1 JA |
9275 | }; |
9276 | ||
9277 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9278 | struct io_uring_params *p) | |
9279 | { | |
75b28aff HV |
9280 | struct io_rings *rings; |
9281 | size_t size, sq_array_offset; | |
2b188cc1 | 9282 | |
bd740481 JA |
9283 | /* make sure these are sane, as we already accounted them */ |
9284 | ctx->sq_entries = p->sq_entries; | |
9285 | ctx->cq_entries = p->cq_entries; | |
9286 | ||
75b28aff HV |
9287 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9288 | if (size == SIZE_MAX) | |
9289 | return -EOVERFLOW; | |
9290 | ||
9291 | rings = io_mem_alloc(size); | |
9292 | if (!rings) | |
2b188cc1 JA |
9293 | return -ENOMEM; |
9294 | ||
75b28aff HV |
9295 | ctx->rings = rings; |
9296 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9297 | rings->sq_ring_mask = p->sq_entries - 1; | |
9298 | rings->cq_ring_mask = p->cq_entries - 1; | |
9299 | rings->sq_ring_entries = p->sq_entries; | |
9300 | rings->cq_ring_entries = p->cq_entries; | |
9301 | ctx->sq_mask = rings->sq_ring_mask; | |
9302 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9303 | |
9304 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9305 | if (size == SIZE_MAX) { |
9306 | io_mem_free(ctx->rings); | |
9307 | ctx->rings = NULL; | |
2b188cc1 | 9308 | return -EOVERFLOW; |
eb065d30 | 9309 | } |
2b188cc1 JA |
9310 | |
9311 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9312 | if (!ctx->sq_sqes) { |
9313 | io_mem_free(ctx->rings); | |
9314 | ctx->rings = NULL; | |
2b188cc1 | 9315 | return -ENOMEM; |
eb065d30 | 9316 | } |
2b188cc1 | 9317 | |
2b188cc1 JA |
9318 | return 0; |
9319 | } | |
9320 | ||
9faadcc8 PB |
9321 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9322 | { | |
9323 | int ret, fd; | |
9324 | ||
9325 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9326 | if (fd < 0) | |
9327 | return fd; | |
9328 | ||
9329 | ret = io_uring_add_task_file(ctx, file); | |
9330 | if (ret) { | |
9331 | put_unused_fd(fd); | |
9332 | return ret; | |
9333 | } | |
9334 | fd_install(fd, file); | |
9335 | return fd; | |
9336 | } | |
9337 | ||
2b188cc1 JA |
9338 | /* |
9339 | * Allocate an anonymous fd, this is what constitutes the application | |
9340 | * visible backing of an io_uring instance. The application mmaps this | |
9341 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9342 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9343 | */ | |
9faadcc8 | 9344 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9345 | { |
9346 | struct file *file; | |
9faadcc8 | 9347 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9348 | int ret; |
9349 | ||
2b188cc1 JA |
9350 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9351 | &ctx->ring_sock); | |
9352 | if (ret) | |
9faadcc8 | 9353 | return ERR_PTR(ret); |
2b188cc1 JA |
9354 | #endif |
9355 | ||
2b188cc1 JA |
9356 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9357 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9358 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9359 | if (IS_ERR(file)) { |
9360 | sock_release(ctx->ring_sock); | |
9361 | ctx->ring_sock = NULL; | |
9362 | } else { | |
9363 | ctx->ring_sock->file = file; | |
0f212204 | 9364 | } |
2b188cc1 | 9365 | #endif |
9faadcc8 | 9366 | return file; |
2b188cc1 JA |
9367 | } |
9368 | ||
7f13657d XW |
9369 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9370 | struct io_uring_params __user *params) | |
2b188cc1 | 9371 | { |
2b188cc1 | 9372 | struct io_ring_ctx *ctx; |
9faadcc8 | 9373 | struct file *file; |
2b188cc1 JA |
9374 | int ret; |
9375 | ||
8110c1a6 | 9376 | if (!entries) |
2b188cc1 | 9377 | return -EINVAL; |
8110c1a6 JA |
9378 | if (entries > IORING_MAX_ENTRIES) { |
9379 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9380 | return -EINVAL; | |
9381 | entries = IORING_MAX_ENTRIES; | |
9382 | } | |
2b188cc1 JA |
9383 | |
9384 | /* | |
9385 | * Use twice as many entries for the CQ ring. It's possible for the | |
9386 | * application to drive a higher depth than the size of the SQ ring, | |
9387 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9388 | * some flexibility in overcommitting a bit. If the application has |
9389 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9390 | * of CQ ring entries manually. | |
2b188cc1 JA |
9391 | */ |
9392 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9393 | if (p->flags & IORING_SETUP_CQSIZE) { |
9394 | /* | |
9395 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9396 | * to a power-of-two, if it isn't already. We do NOT impose | |
9397 | * any cq vs sq ring sizing. | |
9398 | */ | |
eb2667b3 | 9399 | if (!p->cq_entries) |
33a107f0 | 9400 | return -EINVAL; |
8110c1a6 JA |
9401 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9402 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9403 | return -EINVAL; | |
9404 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9405 | } | |
eb2667b3 JQ |
9406 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9407 | if (p->cq_entries < p->sq_entries) | |
9408 | return -EINVAL; | |
33a107f0 JA |
9409 | } else { |
9410 | p->cq_entries = 2 * p->sq_entries; | |
9411 | } | |
2b188cc1 | 9412 | |
2b188cc1 | 9413 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9414 | if (!ctx) |
2b188cc1 | 9415 | return -ENOMEM; |
2b188cc1 | 9416 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9417 | if (!capable(CAP_IPC_LOCK)) |
9418 | ctx->user = get_uid(current_user()); | |
37d1e2e3 | 9419 | ctx->sqo_task = current; |
2aede0e4 JA |
9420 | |
9421 | /* | |
9422 | * This is just grabbed for accounting purposes. When a process exits, | |
9423 | * the mm is exited and dropped before the files, hence we need to hang | |
9424 | * on to this mm purely for the purposes of being able to unaccount | |
9425 | * memory (locked/pinned vm). It's not used for anything else. | |
9426 | */ | |
6b7898eb | 9427 | mmgrab(current->mm); |
2aede0e4 | 9428 | ctx->mm_account = current->mm; |
6b7898eb | 9429 | |
2b188cc1 JA |
9430 | ret = io_allocate_scq_urings(ctx, p); |
9431 | if (ret) | |
9432 | goto err; | |
9433 | ||
7e84e1c7 | 9434 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9435 | if (ret) |
9436 | goto err; | |
9437 | ||
7e84e1c7 SG |
9438 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9439 | io_sq_offload_start(ctx); | |
9440 | ||
2b188cc1 | 9441 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9442 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9443 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9444 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9445 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9446 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9447 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9448 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9449 | |
9450 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9451 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9452 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9453 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9454 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9455 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9456 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9457 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9458 | |
7f13657d XW |
9459 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9460 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9461 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9462 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9463 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9464 | |
9465 | if (copy_to_user(params, p, sizeof(*p))) { | |
9466 | ret = -EFAULT; | |
9467 | goto err; | |
9468 | } | |
d1719f70 | 9469 | |
9faadcc8 PB |
9470 | file = io_uring_get_file(ctx); |
9471 | if (IS_ERR(file)) { | |
9472 | ret = PTR_ERR(file); | |
9473 | goto err; | |
9474 | } | |
9475 | ||
044c1ab3 JA |
9476 | /* |
9477 | * Install ring fd as the very last thing, so we don't risk someone | |
9478 | * having closed it before we finish setup | |
9479 | */ | |
9faadcc8 PB |
9480 | ret = io_uring_install_fd(ctx, file); |
9481 | if (ret < 0) { | |
06585c49 | 9482 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9483 | /* fput will clean it up */ |
9484 | fput(file); | |
9485 | return ret; | |
9486 | } | |
044c1ab3 | 9487 | |
c826bd7a | 9488 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9489 | return ret; |
9490 | err: | |
d9d05217 | 9491 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9492 | io_ring_ctx_wait_and_kill(ctx); |
9493 | return ret; | |
9494 | } | |
9495 | ||
9496 | /* | |
9497 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9498 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9499 | * params structure passed in. | |
9500 | */ | |
9501 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9502 | { | |
9503 | struct io_uring_params p; | |
2b188cc1 JA |
9504 | int i; |
9505 | ||
9506 | if (copy_from_user(&p, params, sizeof(p))) | |
9507 | return -EFAULT; | |
9508 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9509 | if (p.resv[i]) | |
9510 | return -EINVAL; | |
9511 | } | |
9512 | ||
6c271ce2 | 9513 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9514 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9515 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9516 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9517 | return -EINVAL; |
9518 | ||
7f13657d | 9519 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9520 | } |
9521 | ||
9522 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9523 | struct io_uring_params __user *, params) | |
9524 | { | |
9525 | return io_uring_setup(entries, params); | |
9526 | } | |
9527 | ||
66f4af93 JA |
9528 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9529 | { | |
9530 | struct io_uring_probe *p; | |
9531 | size_t size; | |
9532 | int i, ret; | |
9533 | ||
9534 | size = struct_size(p, ops, nr_args); | |
9535 | if (size == SIZE_MAX) | |
9536 | return -EOVERFLOW; | |
9537 | p = kzalloc(size, GFP_KERNEL); | |
9538 | if (!p) | |
9539 | return -ENOMEM; | |
9540 | ||
9541 | ret = -EFAULT; | |
9542 | if (copy_from_user(p, arg, size)) | |
9543 | goto out; | |
9544 | ret = -EINVAL; | |
9545 | if (memchr_inv(p, 0, size)) | |
9546 | goto out; | |
9547 | ||
9548 | p->last_op = IORING_OP_LAST - 1; | |
9549 | if (nr_args > IORING_OP_LAST) | |
9550 | nr_args = IORING_OP_LAST; | |
9551 | ||
9552 | for (i = 0; i < nr_args; i++) { | |
9553 | p->ops[i].op = i; | |
9554 | if (!io_op_defs[i].not_supported) | |
9555 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9556 | } | |
9557 | p->ops_len = i; | |
9558 | ||
9559 | ret = 0; | |
9560 | if (copy_to_user(arg, p, size)) | |
9561 | ret = -EFAULT; | |
9562 | out: | |
9563 | kfree(p); | |
9564 | return ret; | |
9565 | } | |
9566 | ||
071698e1 JA |
9567 | static int io_register_personality(struct io_ring_ctx *ctx) |
9568 | { | |
4379bf8b | 9569 | const struct cred *creds; |
1e6fa521 | 9570 | int ret; |
071698e1 | 9571 | |
4379bf8b | 9572 | creds = get_current_cred(); |
1e6fa521 | 9573 | |
4379bf8b JA |
9574 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
9575 | USHRT_MAX, GFP_KERNEL); | |
9576 | if (ret < 0) | |
9577 | put_cred(creds); | |
1e6fa521 | 9578 | return ret; |
071698e1 JA |
9579 | } |
9580 | ||
21b55dbc SG |
9581 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9582 | unsigned int nr_args) | |
9583 | { | |
9584 | struct io_uring_restriction *res; | |
9585 | size_t size; | |
9586 | int i, ret; | |
9587 | ||
7e84e1c7 SG |
9588 | /* Restrictions allowed only if rings started disabled */ |
9589 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9590 | return -EBADFD; | |
9591 | ||
21b55dbc | 9592 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9593 | if (ctx->restrictions.registered) |
21b55dbc SG |
9594 | return -EBUSY; |
9595 | ||
9596 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9597 | return -EINVAL; | |
9598 | ||
9599 | size = array_size(nr_args, sizeof(*res)); | |
9600 | if (size == SIZE_MAX) | |
9601 | return -EOVERFLOW; | |
9602 | ||
9603 | res = memdup_user(arg, size); | |
9604 | if (IS_ERR(res)) | |
9605 | return PTR_ERR(res); | |
9606 | ||
9607 | ret = 0; | |
9608 | ||
9609 | for (i = 0; i < nr_args; i++) { | |
9610 | switch (res[i].opcode) { | |
9611 | case IORING_RESTRICTION_REGISTER_OP: | |
9612 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9613 | ret = -EINVAL; | |
9614 | goto out; | |
9615 | } | |
9616 | ||
9617 | __set_bit(res[i].register_op, | |
9618 | ctx->restrictions.register_op); | |
9619 | break; | |
9620 | case IORING_RESTRICTION_SQE_OP: | |
9621 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9622 | ret = -EINVAL; | |
9623 | goto out; | |
9624 | } | |
9625 | ||
9626 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9627 | break; | |
9628 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9629 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9630 | break; | |
9631 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9632 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9633 | break; | |
9634 | default: | |
9635 | ret = -EINVAL; | |
9636 | goto out; | |
9637 | } | |
9638 | } | |
9639 | ||
9640 | out: | |
9641 | /* Reset all restrictions if an error happened */ | |
9642 | if (ret != 0) | |
9643 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9644 | else | |
7e84e1c7 | 9645 | ctx->restrictions.registered = true; |
21b55dbc SG |
9646 | |
9647 | kfree(res); | |
9648 | return ret; | |
9649 | } | |
9650 | ||
7e84e1c7 SG |
9651 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9652 | { | |
9653 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9654 | return -EBADFD; | |
9655 | ||
9656 | if (ctx->restrictions.registered) | |
9657 | ctx->restricted = 1; | |
9658 | ||
9659 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9660 | ||
9661 | io_sq_offload_start(ctx); | |
9662 | ||
9663 | return 0; | |
9664 | } | |
9665 | ||
071698e1 JA |
9666 | static bool io_register_op_must_quiesce(int op) |
9667 | { | |
9668 | switch (op) { | |
9669 | case IORING_UNREGISTER_FILES: | |
9670 | case IORING_REGISTER_FILES_UPDATE: | |
9671 | case IORING_REGISTER_PROBE: | |
9672 | case IORING_REGISTER_PERSONALITY: | |
9673 | case IORING_UNREGISTER_PERSONALITY: | |
9674 | return false; | |
9675 | default: | |
9676 | return true; | |
9677 | } | |
9678 | } | |
9679 | ||
edafccee JA |
9680 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9681 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9682 | __releases(ctx->uring_lock) |
9683 | __acquires(ctx->uring_lock) | |
edafccee JA |
9684 | { |
9685 | int ret; | |
9686 | ||
35fa71a0 JA |
9687 | /* |
9688 | * We're inside the ring mutex, if the ref is already dying, then | |
9689 | * someone else killed the ctx or is already going through | |
9690 | * io_uring_register(). | |
9691 | */ | |
9692 | if (percpu_ref_is_dying(&ctx->refs)) | |
9693 | return -ENXIO; | |
9694 | ||
071698e1 | 9695 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9696 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9697 | |
05f3fb3c JA |
9698 | /* |
9699 | * Drop uring mutex before waiting for references to exit. If | |
9700 | * another thread is currently inside io_uring_enter() it might | |
9701 | * need to grab the uring_lock to make progress. If we hold it | |
9702 | * here across the drain wait, then we can deadlock. It's safe | |
9703 | * to drop the mutex here, since no new references will come in | |
9704 | * after we've killed the percpu ref. | |
9705 | */ | |
9706 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9707 | do { |
9708 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9709 | if (!ret) | |
9710 | break; | |
ed6930c9 JA |
9711 | ret = io_run_task_work_sig(); |
9712 | if (ret < 0) | |
9713 | break; | |
af9c1a44 JA |
9714 | } while (1); |
9715 | ||
05f3fb3c | 9716 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9717 | |
88f171ab PB |
9718 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
9719 | return ret; | |
21b55dbc SG |
9720 | } |
9721 | ||
9722 | if (ctx->restricted) { | |
9723 | if (opcode >= IORING_REGISTER_LAST) { | |
9724 | ret = -EINVAL; | |
9725 | goto out; | |
9726 | } | |
9727 | ||
9728 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9729 | ret = -EACCES; | |
c150368b JA |
9730 | goto out; |
9731 | } | |
05f3fb3c | 9732 | } |
edafccee JA |
9733 | |
9734 | switch (opcode) { | |
9735 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9736 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9737 | break; |
9738 | case IORING_UNREGISTER_BUFFERS: | |
9739 | ret = -EINVAL; | |
9740 | if (arg || nr_args) | |
9741 | break; | |
0a96bbe4 | 9742 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9743 | break; |
6b06314c JA |
9744 | case IORING_REGISTER_FILES: |
9745 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9746 | break; | |
9747 | case IORING_UNREGISTER_FILES: | |
9748 | ret = -EINVAL; | |
9749 | if (arg || nr_args) | |
9750 | break; | |
9751 | ret = io_sqe_files_unregister(ctx); | |
9752 | break; | |
c3a31e60 JA |
9753 | case IORING_REGISTER_FILES_UPDATE: |
9754 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9755 | break; | |
9b402849 | 9756 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9757 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9758 | ret = -EINVAL; |
9759 | if (nr_args != 1) | |
9760 | break; | |
9761 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9762 | if (ret) |
9763 | break; | |
9764 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9765 | ctx->eventfd_async = 1; | |
9766 | else | |
9767 | ctx->eventfd_async = 0; | |
9b402849 JA |
9768 | break; |
9769 | case IORING_UNREGISTER_EVENTFD: | |
9770 | ret = -EINVAL; | |
9771 | if (arg || nr_args) | |
9772 | break; | |
9773 | ret = io_eventfd_unregister(ctx); | |
9774 | break; | |
66f4af93 JA |
9775 | case IORING_REGISTER_PROBE: |
9776 | ret = -EINVAL; | |
9777 | if (!arg || nr_args > 256) | |
9778 | break; | |
9779 | ret = io_probe(ctx, arg, nr_args); | |
9780 | break; | |
071698e1 JA |
9781 | case IORING_REGISTER_PERSONALITY: |
9782 | ret = -EINVAL; | |
9783 | if (arg || nr_args) | |
9784 | break; | |
9785 | ret = io_register_personality(ctx); | |
9786 | break; | |
9787 | case IORING_UNREGISTER_PERSONALITY: | |
9788 | ret = -EINVAL; | |
9789 | if (arg) | |
9790 | break; | |
9791 | ret = io_unregister_personality(ctx, nr_args); | |
9792 | break; | |
7e84e1c7 SG |
9793 | case IORING_REGISTER_ENABLE_RINGS: |
9794 | ret = -EINVAL; | |
9795 | if (arg || nr_args) | |
9796 | break; | |
9797 | ret = io_register_enable_rings(ctx); | |
9798 | break; | |
21b55dbc SG |
9799 | case IORING_REGISTER_RESTRICTIONS: |
9800 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9801 | break; | |
edafccee JA |
9802 | default: |
9803 | ret = -EINVAL; | |
9804 | break; | |
9805 | } | |
9806 | ||
21b55dbc | 9807 | out: |
071698e1 | 9808 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9809 | /* bring the ctx back to life */ |
05f3fb3c | 9810 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 9811 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9812 | } |
edafccee JA |
9813 | return ret; |
9814 | } | |
9815 | ||
9816 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9817 | void __user *, arg, unsigned int, nr_args) | |
9818 | { | |
9819 | struct io_ring_ctx *ctx; | |
9820 | long ret = -EBADF; | |
9821 | struct fd f; | |
9822 | ||
9823 | f = fdget(fd); | |
9824 | if (!f.file) | |
9825 | return -EBADF; | |
9826 | ||
9827 | ret = -EOPNOTSUPP; | |
9828 | if (f.file->f_op != &io_uring_fops) | |
9829 | goto out_fput; | |
9830 | ||
9831 | ctx = f.file->private_data; | |
9832 | ||
b6c23dd5 PB |
9833 | io_run_task_work(); |
9834 | ||
edafccee JA |
9835 | mutex_lock(&ctx->uring_lock); |
9836 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9837 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9838 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9839 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9840 | out_fput: |
9841 | fdput(f); | |
9842 | return ret; | |
9843 | } | |
9844 | ||
2b188cc1 JA |
9845 | static int __init io_uring_init(void) |
9846 | { | |
d7f62e82 SM |
9847 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9848 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9849 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9850 | } while (0) | |
9851 | ||
9852 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9853 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9854 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9855 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9856 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9857 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9858 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9859 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9860 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9861 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9862 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9863 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9864 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9865 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9866 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9867 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9868 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9869 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9870 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9871 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9872 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9873 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9874 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9875 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9876 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9877 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9878 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9879 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9880 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9881 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9882 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9883 | |
d3656344 | 9884 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9885 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
9886 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
9887 | SLAB_ACCOUNT); | |
2b188cc1 JA |
9888 | return 0; |
9889 | }; | |
9890 | __initcall(io_uring_init); |