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> | |
6c271ce2 | 60 | #include <linux/kthread.h> |
2b188cc1 | 61 | #include <linux/blkdev.h> |
edafccee | 62 | #include <linux/bvec.h> |
2b188cc1 JA |
63 | #include <linux/net.h> |
64 | #include <net/sock.h> | |
65 | #include <net/af_unix.h> | |
6b06314c | 66 | #include <net/scm.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
ff002b30 | 78 | #include <linux/fs_struct.h> |
7d67af2c | 79 | #include <linux/splice.h> |
b41e9852 | 80 | #include <linux/task_work.h> |
bcf5a063 | 81 | #include <linux/pagemap.h> |
0f212204 | 82 | #include <linux/io_uring.h> |
91d8f519 | 83 | #include <linux/blk-cgroup.h> |
4ea33a97 | 84 | #include <linux/audit.h> |
2b188cc1 | 85 | |
c826bd7a DD |
86 | #define CREATE_TRACE_POINTS |
87 | #include <trace/events/io_uring.h> | |
88 | ||
2b188cc1 JA |
89 | #include <uapi/linux/io_uring.h> |
90 | ||
91 | #include "internal.h" | |
561fb04a | 92 | #include "io-wq.h" |
2b188cc1 | 93 | |
5277deaa | 94 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
96 | |
97 | /* | |
98 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
99 | */ | |
100 | #define IORING_FILE_TABLE_SHIFT 9 | |
101 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
102 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
103 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
104 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
105 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 106 | |
b16fed66 PB |
107 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
108 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
109 | IOSQE_BUFFER_SELECT) | |
110 | ||
2b188cc1 JA |
111 | struct io_uring { |
112 | u32 head ____cacheline_aligned_in_smp; | |
113 | u32 tail ____cacheline_aligned_in_smp; | |
114 | }; | |
115 | ||
1e84b97b | 116 | /* |
75b28aff HV |
117 | * This data is shared with the application through the mmap at offsets |
118 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
119 | * |
120 | * The offsets to the member fields are published through struct | |
121 | * io_sqring_offsets when calling io_uring_setup. | |
122 | */ | |
75b28aff | 123 | struct io_rings { |
1e84b97b SB |
124 | /* |
125 | * Head and tail offsets into the ring; the offsets need to be | |
126 | * masked to get valid indices. | |
127 | * | |
75b28aff HV |
128 | * The kernel controls head of the sq ring and the tail of the cq ring, |
129 | * and the application controls tail of the sq ring and the head of the | |
130 | * cq ring. | |
1e84b97b | 131 | */ |
75b28aff | 132 | struct io_uring sq, cq; |
1e84b97b | 133 | /* |
75b28aff | 134 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
135 | * ring_entries - 1) |
136 | */ | |
75b28aff HV |
137 | u32 sq_ring_mask, cq_ring_mask; |
138 | /* Ring sizes (constant, power of 2) */ | |
139 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
140 | /* |
141 | * Number of invalid entries dropped by the kernel due to | |
142 | * invalid index stored in array | |
143 | * | |
144 | * Written by the kernel, shouldn't be modified by the | |
145 | * application (i.e. get number of "new events" by comparing to | |
146 | * cached value). | |
147 | * | |
148 | * After a new SQ head value was read by the application this | |
149 | * counter includes all submissions that were dropped reaching | |
150 | * the new SQ head (and possibly more). | |
151 | */ | |
75b28aff | 152 | u32 sq_dropped; |
1e84b97b | 153 | /* |
0d9b5b3a | 154 | * Runtime SQ flags |
1e84b97b SB |
155 | * |
156 | * Written by the kernel, shouldn't be modified by the | |
157 | * application. | |
158 | * | |
159 | * The application needs a full memory barrier before checking | |
160 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
161 | */ | |
75b28aff | 162 | u32 sq_flags; |
0d9b5b3a SG |
163 | /* |
164 | * Runtime CQ flags | |
165 | * | |
166 | * Written by the application, shouldn't be modified by the | |
167 | * kernel. | |
168 | */ | |
169 | u32 cq_flags; | |
1e84b97b SB |
170 | /* |
171 | * Number of completion events lost because the queue was full; | |
172 | * this should be avoided by the application by making sure | |
0b4295b5 | 173 | * there are not more requests pending than there is space in |
1e84b97b SB |
174 | * the completion queue. |
175 | * | |
176 | * Written by the kernel, shouldn't be modified by the | |
177 | * application (i.e. get number of "new events" by comparing to | |
178 | * cached value). | |
179 | * | |
180 | * As completion events come in out of order this counter is not | |
181 | * ordered with any other data. | |
182 | */ | |
75b28aff | 183 | u32 cq_overflow; |
1e84b97b SB |
184 | /* |
185 | * Ring buffer of completion events. | |
186 | * | |
187 | * The kernel writes completion events fresh every time they are | |
188 | * produced, so the application is allowed to modify pending | |
189 | * entries. | |
190 | */ | |
75b28aff | 191 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
192 | }; |
193 | ||
45d189c6 PB |
194 | enum io_uring_cmd_flags { |
195 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 196 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
197 | }; |
198 | ||
edafccee JA |
199 | struct io_mapped_ubuf { |
200 | u64 ubuf; | |
201 | size_t len; | |
202 | struct bio_vec *bvec; | |
203 | unsigned int nr_bvecs; | |
de293938 | 204 | unsigned long acct_pages; |
edafccee JA |
205 | }; |
206 | ||
50238531 BM |
207 | struct io_ring_ctx; |
208 | ||
269bbe5f BM |
209 | struct io_rsrc_put { |
210 | struct list_head list; | |
50238531 BM |
211 | union { |
212 | void *rsrc; | |
213 | struct file *file; | |
214 | }; | |
269bbe5f BM |
215 | }; |
216 | ||
217 | struct fixed_rsrc_table { | |
65e19f54 | 218 | struct file **files; |
31b51510 JA |
219 | }; |
220 | ||
269bbe5f | 221 | struct fixed_rsrc_ref_node { |
05589553 XW |
222 | struct percpu_ref refs; |
223 | struct list_head node; | |
269bbe5f BM |
224 | struct list_head rsrc_list; |
225 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
226 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
227 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 228 | struct llist_node llist; |
e297822b | 229 | bool done; |
05589553 XW |
230 | }; |
231 | ||
269bbe5f BM |
232 | struct fixed_rsrc_data { |
233 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
234 | struct io_ring_ctx *ctx; |
235 | ||
269bbe5f | 236 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 237 | struct percpu_ref refs; |
05f3fb3c | 238 | struct completion done; |
8bad28d8 | 239 | bool quiesce; |
05f3fb3c JA |
240 | }; |
241 | ||
5a2e745d JA |
242 | struct io_buffer { |
243 | struct list_head list; | |
244 | __u64 addr; | |
245 | __s32 len; | |
246 | __u16 bid; | |
247 | }; | |
248 | ||
21b55dbc SG |
249 | struct io_restriction { |
250 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
251 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
252 | u8 sqe_flags_allowed; | |
253 | u8 sqe_flags_required; | |
7e84e1c7 | 254 | bool registered; |
21b55dbc SG |
255 | }; |
256 | ||
534ca6d6 JA |
257 | struct io_sq_data { |
258 | refcount_t refs; | |
69fb2131 JA |
259 | struct mutex lock; |
260 | ||
261 | /* ctx's that are using this sqd */ | |
262 | struct list_head ctx_list; | |
263 | struct list_head ctx_new_list; | |
264 | struct mutex ctx_lock; | |
265 | ||
534ca6d6 JA |
266 | struct task_struct *thread; |
267 | struct wait_queue_head wait; | |
08369246 XW |
268 | |
269 | unsigned sq_thread_idle; | |
534ca6d6 JA |
270 | }; |
271 | ||
258b29a9 | 272 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 273 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 274 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 275 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
276 | |
277 | struct io_comp_state { | |
6dd0be1e | 278 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 279 | unsigned int nr; |
c7dae4ba JA |
280 | unsigned int locked_free_nr; |
281 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 282 | struct list_head free_list; |
c7dae4ba JA |
283 | /* IRQ completion list, under ->completion_lock */ |
284 | struct list_head locked_free_list; | |
258b29a9 PB |
285 | }; |
286 | ||
a1ab7b35 PB |
287 | struct io_submit_link { |
288 | struct io_kiocb *head; | |
289 | struct io_kiocb *last; | |
290 | }; | |
291 | ||
258b29a9 PB |
292 | struct io_submit_state { |
293 | struct blk_plug plug; | |
a1ab7b35 | 294 | struct io_submit_link link; |
258b29a9 PB |
295 | |
296 | /* | |
297 | * io_kiocb alloc cache | |
298 | */ | |
bf019da7 | 299 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
300 | unsigned int free_reqs; |
301 | ||
302 | bool plug_started; | |
303 | ||
304 | /* | |
305 | * Batch completion logic | |
306 | */ | |
307 | struct io_comp_state comp; | |
308 | ||
309 | /* | |
310 | * File reference cache | |
311 | */ | |
312 | struct file *file; | |
313 | unsigned int fd; | |
314 | unsigned int file_refs; | |
315 | unsigned int ios_left; | |
316 | }; | |
317 | ||
2b188cc1 JA |
318 | struct io_ring_ctx { |
319 | struct { | |
320 | struct percpu_ref refs; | |
321 | } ____cacheline_aligned_in_smp; | |
322 | ||
323 | struct { | |
324 | unsigned int flags; | |
e1d85334 | 325 | unsigned int compat: 1; |
aad5d8da | 326 | unsigned int limit_mem: 1; |
e1d85334 RD |
327 | unsigned int cq_overflow_flushed: 1; |
328 | unsigned int drain_next: 1; | |
329 | unsigned int eventfd_async: 1; | |
21b55dbc | 330 | unsigned int restricted: 1; |
d9d05217 | 331 | unsigned int sqo_dead: 1; |
2b188cc1 | 332 | |
75b28aff HV |
333 | /* |
334 | * Ring buffer of indices into array of io_uring_sqe, which is | |
335 | * mmapped by the application using the IORING_OFF_SQES offset. | |
336 | * | |
337 | * This indirection could e.g. be used to assign fixed | |
338 | * io_uring_sqe entries to operations and only submit them to | |
339 | * the queue when needed. | |
340 | * | |
341 | * The kernel modifies neither the indices array nor the entries | |
342 | * array. | |
343 | */ | |
344 | u32 *sq_array; | |
2b188cc1 JA |
345 | unsigned cached_sq_head; |
346 | unsigned sq_entries; | |
347 | unsigned sq_mask; | |
6c271ce2 | 348 | unsigned sq_thread_idle; |
498ccd9e | 349 | unsigned cached_sq_dropped; |
2c3bac6d | 350 | unsigned cached_cq_overflow; |
ad3eb2c8 | 351 | unsigned long sq_check_overflow; |
de0617e4 JA |
352 | |
353 | struct list_head defer_list; | |
5262f567 | 354 | struct list_head timeout_list; |
1d7bb1d5 | 355 | struct list_head cq_overflow_list; |
fcb323cc | 356 | |
ad3eb2c8 | 357 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
358 | } ____cacheline_aligned_in_smp; |
359 | ||
3c1a2ead JA |
360 | struct { |
361 | struct mutex uring_lock; | |
362 | wait_queue_head_t wait; | |
363 | } ____cacheline_aligned_in_smp; | |
364 | ||
365 | struct io_submit_state submit_state; | |
366 | ||
206aefde JA |
367 | struct io_rings *rings; |
368 | ||
2b188cc1 | 369 | /* IO offload */ |
561fb04a | 370 | struct io_wq *io_wq; |
2aede0e4 JA |
371 | |
372 | /* | |
373 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
374 | * have access to the ->files | |
375 | */ | |
376 | struct task_struct *sqo_task; | |
377 | ||
378 | /* Only used for accounting purposes */ | |
379 | struct mm_struct *mm_account; | |
380 | ||
91d8f519 DZ |
381 | #ifdef CONFIG_BLK_CGROUP |
382 | struct cgroup_subsys_state *sqo_blkcg_css; | |
383 | #endif | |
384 | ||
534ca6d6 JA |
385 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
386 | ||
90554200 | 387 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 388 | struct list_head sqd_list; |
75b28aff | 389 | |
6b06314c JA |
390 | /* |
391 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
392 | * readers must ensure that ->refs is alive as long as the file* is | |
393 | * used. Only updated through io_uring_register(2). | |
394 | */ | |
269bbe5f | 395 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
396 | unsigned nr_user_files; |
397 | ||
edafccee JA |
398 | /* if used, fixed mapped user buffers */ |
399 | unsigned nr_user_bufs; | |
400 | struct io_mapped_ubuf *user_bufs; | |
401 | ||
2b188cc1 JA |
402 | struct user_struct *user; |
403 | ||
0b8c0ec7 | 404 | const struct cred *creds; |
181e448d | 405 | |
4ea33a97 JA |
406 | #ifdef CONFIG_AUDIT |
407 | kuid_t loginuid; | |
408 | unsigned int sessionid; | |
409 | #endif | |
410 | ||
0f158b4c JA |
411 | struct completion ref_comp; |
412 | struct completion sq_thread_comp; | |
206aefde JA |
413 | |
414 | #if defined(CONFIG_UNIX) | |
415 | struct socket *ring_sock; | |
416 | #endif | |
417 | ||
5a2e745d JA |
418 | struct idr io_buffer_idr; |
419 | ||
071698e1 JA |
420 | struct idr personality_idr; |
421 | ||
206aefde JA |
422 | struct { |
423 | unsigned cached_cq_tail; | |
424 | unsigned cq_entries; | |
425 | unsigned cq_mask; | |
426 | atomic_t cq_timeouts; | |
f010505b | 427 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 428 | unsigned long cq_check_overflow; |
206aefde JA |
429 | struct wait_queue_head cq_wait; |
430 | struct fasync_struct *cq_fasync; | |
431 | struct eventfd_ctx *cq_ev_fd; | |
432 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 433 | |
2b188cc1 JA |
434 | struct { |
435 | spinlock_t completion_lock; | |
e94f141b | 436 | |
def596e9 | 437 | /* |
540e32a0 | 438 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
439 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
440 | * For SQPOLL, only the single threaded io_sq_thread() will | |
441 | * manipulate the list, hence no extra locking is needed there. | |
442 | */ | |
540e32a0 | 443 | struct list_head iopoll_list; |
78076bb6 JA |
444 | struct hlist_head *cancel_hash; |
445 | unsigned cancel_hash_bits; | |
e94f141b | 446 | bool poll_multi_file; |
31b51510 | 447 | |
fcb323cc JA |
448 | spinlock_t inflight_lock; |
449 | struct list_head inflight_list; | |
2b188cc1 | 450 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 451 | |
269bbe5f BM |
452 | struct delayed_work rsrc_put_work; |
453 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
454 | struct list_head rsrc_ref_list; |
455 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 456 | |
21b55dbc | 457 | struct io_restriction restrictions; |
3c1a2ead JA |
458 | |
459 | /* Keep this last, we don't need it for the fast path */ | |
460 | struct work_struct exit_work; | |
2b188cc1 JA |
461 | }; |
462 | ||
09bb8394 JA |
463 | /* |
464 | * First field must be the file pointer in all the | |
465 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
466 | */ | |
221c5eb2 JA |
467 | struct io_poll_iocb { |
468 | struct file *file; | |
018043be | 469 | struct wait_queue_head *head; |
221c5eb2 | 470 | __poll_t events; |
8c838788 | 471 | bool done; |
221c5eb2 | 472 | bool canceled; |
392edb45 | 473 | struct wait_queue_entry wait; |
221c5eb2 JA |
474 | }; |
475 | ||
018043be PB |
476 | struct io_poll_remove { |
477 | struct file *file; | |
478 | u64 addr; | |
479 | }; | |
480 | ||
b5dba59e JA |
481 | struct io_close { |
482 | struct file *file; | |
b5dba59e JA |
483 | int fd; |
484 | }; | |
485 | ||
ad8a48ac JA |
486 | struct io_timeout_data { |
487 | struct io_kiocb *req; | |
488 | struct hrtimer timer; | |
489 | struct timespec64 ts; | |
490 | enum hrtimer_mode mode; | |
491 | }; | |
492 | ||
8ed8d3c3 JA |
493 | struct io_accept { |
494 | struct file *file; | |
495 | struct sockaddr __user *addr; | |
496 | int __user *addr_len; | |
497 | int flags; | |
09952e3e | 498 | unsigned long nofile; |
8ed8d3c3 JA |
499 | }; |
500 | ||
501 | struct io_sync { | |
502 | struct file *file; | |
503 | loff_t len; | |
504 | loff_t off; | |
505 | int flags; | |
d63d1b5e | 506 | int mode; |
8ed8d3c3 JA |
507 | }; |
508 | ||
fbf23849 JA |
509 | struct io_cancel { |
510 | struct file *file; | |
511 | u64 addr; | |
512 | }; | |
513 | ||
b29472ee JA |
514 | struct io_timeout { |
515 | struct file *file; | |
bfe68a22 PB |
516 | u32 off; |
517 | u32 target_seq; | |
135fcde8 | 518 | struct list_head list; |
90cd7e42 PB |
519 | /* head of the link, used by linked timeouts only */ |
520 | struct io_kiocb *head; | |
b29472ee JA |
521 | }; |
522 | ||
0bdf7a2d PB |
523 | struct io_timeout_rem { |
524 | struct file *file; | |
525 | u64 addr; | |
9c8e11b3 PB |
526 | |
527 | /* timeout update */ | |
528 | struct timespec64 ts; | |
529 | u32 flags; | |
0bdf7a2d PB |
530 | }; |
531 | ||
9adbd45d JA |
532 | struct io_rw { |
533 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
534 | struct kiocb kiocb; | |
535 | u64 addr; | |
536 | u64 len; | |
537 | }; | |
538 | ||
3fbb51c1 JA |
539 | struct io_connect { |
540 | struct file *file; | |
541 | struct sockaddr __user *addr; | |
542 | int addr_len; | |
543 | }; | |
544 | ||
e47293fd JA |
545 | struct io_sr_msg { |
546 | struct file *file; | |
fddaface | 547 | union { |
270a5940 | 548 | struct user_msghdr __user *umsg; |
fddaface JA |
549 | void __user *buf; |
550 | }; | |
e47293fd | 551 | int msg_flags; |
bcda7baa | 552 | int bgid; |
fddaface | 553 | size_t len; |
bcda7baa | 554 | struct io_buffer *kbuf; |
e47293fd JA |
555 | }; |
556 | ||
15b71abe JA |
557 | struct io_open { |
558 | struct file *file; | |
559 | int dfd; | |
15b71abe | 560 | struct filename *filename; |
c12cedf2 | 561 | struct open_how how; |
4022e7af | 562 | unsigned long nofile; |
15b71abe JA |
563 | }; |
564 | ||
269bbe5f | 565 | struct io_rsrc_update { |
05f3fb3c JA |
566 | struct file *file; |
567 | u64 arg; | |
568 | u32 nr_args; | |
569 | u32 offset; | |
570 | }; | |
571 | ||
4840e418 JA |
572 | struct io_fadvise { |
573 | struct file *file; | |
574 | u64 offset; | |
575 | u32 len; | |
576 | u32 advice; | |
577 | }; | |
578 | ||
c1ca757b JA |
579 | struct io_madvise { |
580 | struct file *file; | |
581 | u64 addr; | |
582 | u32 len; | |
583 | u32 advice; | |
584 | }; | |
585 | ||
3e4827b0 JA |
586 | struct io_epoll { |
587 | struct file *file; | |
588 | int epfd; | |
589 | int op; | |
590 | int fd; | |
591 | struct epoll_event event; | |
e47293fd JA |
592 | }; |
593 | ||
7d67af2c PB |
594 | struct io_splice { |
595 | struct file *file_out; | |
596 | struct file *file_in; | |
597 | loff_t off_out; | |
598 | loff_t off_in; | |
599 | u64 len; | |
600 | unsigned int flags; | |
601 | }; | |
602 | ||
ddf0322d JA |
603 | struct io_provide_buf { |
604 | struct file *file; | |
605 | __u64 addr; | |
606 | __s32 len; | |
607 | __u32 bgid; | |
608 | __u16 nbufs; | |
609 | __u16 bid; | |
610 | }; | |
611 | ||
1d9e1288 BM |
612 | struct io_statx { |
613 | struct file *file; | |
614 | int dfd; | |
615 | unsigned int mask; | |
616 | unsigned int flags; | |
e62753e4 | 617 | const char __user *filename; |
1d9e1288 BM |
618 | struct statx __user *buffer; |
619 | }; | |
620 | ||
36f4fa68 JA |
621 | struct io_shutdown { |
622 | struct file *file; | |
623 | int how; | |
624 | }; | |
625 | ||
80a261fd JA |
626 | struct io_rename { |
627 | struct file *file; | |
628 | int old_dfd; | |
629 | int new_dfd; | |
630 | struct filename *oldpath; | |
631 | struct filename *newpath; | |
632 | int flags; | |
633 | }; | |
634 | ||
14a1143b JA |
635 | struct io_unlink { |
636 | struct file *file; | |
637 | int dfd; | |
638 | int flags; | |
639 | struct filename *filename; | |
640 | }; | |
641 | ||
3ca405eb PB |
642 | struct io_completion { |
643 | struct file *file; | |
644 | struct list_head list; | |
0f7e466b | 645 | int cflags; |
3ca405eb PB |
646 | }; |
647 | ||
f499a021 JA |
648 | struct io_async_connect { |
649 | struct sockaddr_storage address; | |
650 | }; | |
651 | ||
03b1230c JA |
652 | struct io_async_msghdr { |
653 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
654 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
655 | struct iovec *free_iov; | |
03b1230c JA |
656 | struct sockaddr __user *uaddr; |
657 | struct msghdr msg; | |
b537916c | 658 | struct sockaddr_storage addr; |
03b1230c JA |
659 | }; |
660 | ||
f67676d1 JA |
661 | struct io_async_rw { |
662 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
663 | const struct iovec *free_iovec; |
664 | struct iov_iter iter; | |
227c0c96 | 665 | size_t bytes_done; |
bcf5a063 | 666 | struct wait_page_queue wpq; |
f67676d1 JA |
667 | }; |
668 | ||
6b47ee6e PB |
669 | enum { |
670 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
671 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
672 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
673 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
674 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 675 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 676 | |
6b47ee6e PB |
677 | REQ_F_FAIL_LINK_BIT, |
678 | REQ_F_INFLIGHT_BIT, | |
679 | REQ_F_CUR_POS_BIT, | |
680 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 681 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 682 | REQ_F_ISREG_BIT, |
99bc4c38 | 683 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 684 | REQ_F_POLLED_BIT, |
bcda7baa | 685 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 686 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 687 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 688 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 689 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
690 | |
691 | /* not a real bit, just to check we're not overflowing the space */ | |
692 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
693 | }; |
694 | ||
695 | enum { | |
696 | /* ctx owns file */ | |
697 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
698 | /* drain existing IO first */ | |
699 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
700 | /* linked sqes */ | |
701 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
702 | /* doesn't sever on completion < 0 */ | |
703 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
704 | /* IOSQE_ASYNC */ | |
705 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
706 | /* IOSQE_BUFFER_SELECT */ |
707 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 708 | |
6b47ee6e PB |
709 | /* fail rest of links */ |
710 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
711 | /* on inflight list */ | |
712 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
713 | /* read/write uses file position */ | |
714 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
715 | /* must not punt to workers */ | |
716 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 717 | /* has or had linked timeout */ |
6b47ee6e | 718 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
719 | /* regular file */ |
720 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
721 | /* needs cleanup */ |
722 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
723 | /* already went through poll handler */ |
724 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
725 | /* buffer already selected */ |
726 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
727 | /* doesn't need file table for this request */ |
728 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
729 | /* io_wq_work is initialized */ |
730 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
731 | /* linked timeout is active, i.e. prepared by link's head */ |
732 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
733 | /* completion is deferred through io_comp_state */ |
734 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
735 | }; |
736 | ||
737 | struct async_poll { | |
738 | struct io_poll_iocb poll; | |
807abcb0 | 739 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
740 | }; |
741 | ||
7cbf1722 JA |
742 | struct io_task_work { |
743 | struct io_wq_work_node node; | |
744 | task_work_func_t func; | |
745 | }; | |
746 | ||
09bb8394 JA |
747 | /* |
748 | * NOTE! Each of the iocb union members has the file pointer | |
749 | * as the first entry in their struct definition. So you can | |
750 | * access the file pointer through any of the sub-structs, | |
751 | * or directly as just 'ki_filp' in this struct. | |
752 | */ | |
2b188cc1 | 753 | struct io_kiocb { |
221c5eb2 | 754 | union { |
09bb8394 | 755 | struct file *file; |
9adbd45d | 756 | struct io_rw rw; |
221c5eb2 | 757 | struct io_poll_iocb poll; |
018043be | 758 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
759 | struct io_accept accept; |
760 | struct io_sync sync; | |
fbf23849 | 761 | struct io_cancel cancel; |
b29472ee | 762 | struct io_timeout timeout; |
0bdf7a2d | 763 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 764 | struct io_connect connect; |
e47293fd | 765 | struct io_sr_msg sr_msg; |
15b71abe | 766 | struct io_open open; |
b5dba59e | 767 | struct io_close close; |
269bbe5f | 768 | struct io_rsrc_update rsrc_update; |
4840e418 | 769 | struct io_fadvise fadvise; |
c1ca757b | 770 | struct io_madvise madvise; |
3e4827b0 | 771 | struct io_epoll epoll; |
7d67af2c | 772 | struct io_splice splice; |
ddf0322d | 773 | struct io_provide_buf pbuf; |
1d9e1288 | 774 | struct io_statx statx; |
36f4fa68 | 775 | struct io_shutdown shutdown; |
80a261fd | 776 | struct io_rename rename; |
14a1143b | 777 | struct io_unlink unlink; |
3ca405eb PB |
778 | /* use only after cleaning per-op data, see io_clean_op() */ |
779 | struct io_completion compl; | |
221c5eb2 | 780 | }; |
2b188cc1 | 781 | |
e8c2bc1f JA |
782 | /* opcode allocated if it needs to store data for async defer */ |
783 | void *async_data; | |
d625c6ee | 784 | u8 opcode; |
65a6543d XW |
785 | /* polled IO has completed */ |
786 | u8 iopoll_completed; | |
2b188cc1 | 787 | |
4f4eeba8 | 788 | u16 buf_index; |
9cf7c104 | 789 | u32 result; |
4f4eeba8 | 790 | |
010e8e6b PB |
791 | struct io_ring_ctx *ctx; |
792 | unsigned int flags; | |
793 | refcount_t refs; | |
794 | struct task_struct *task; | |
795 | u64 user_data; | |
d7718a9d | 796 | |
f2f87370 | 797 | struct io_kiocb *link; |
269bbe5f | 798 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 799 | |
d21ffe7e PB |
800 | /* |
801 | * 1. used with ctx->iopoll_list with reads/writes | |
802 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
803 | */ | |
010e8e6b | 804 | struct list_head inflight_entry; |
7cbf1722 JA |
805 | union { |
806 | struct io_task_work io_task_work; | |
807 | struct callback_head task_work; | |
808 | }; | |
010e8e6b PB |
809 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
810 | struct hlist_node hash_node; | |
811 | struct async_poll *apoll; | |
812 | struct io_wq_work work; | |
2b188cc1 | 813 | }; |
05589553 | 814 | |
27dc8338 PB |
815 | struct io_defer_entry { |
816 | struct list_head list; | |
817 | struct io_kiocb *req; | |
9cf7c104 | 818 | u32 seq; |
2b188cc1 JA |
819 | }; |
820 | ||
d3656344 | 821 | struct io_op_def { |
d3656344 JA |
822 | /* needs req->file assigned */ |
823 | unsigned needs_file : 1; | |
d3656344 JA |
824 | /* hash wq insertion if file is a regular file */ |
825 | unsigned hash_reg_file : 1; | |
826 | /* unbound wq insertion if file is a non-regular file */ | |
827 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
828 | /* opcode is not supported by this kernel */ |
829 | unsigned not_supported : 1; | |
8a72758c JA |
830 | /* set if opcode supports polled "wait" */ |
831 | unsigned pollin : 1; | |
832 | unsigned pollout : 1; | |
bcda7baa JA |
833 | /* op supports buffer selection */ |
834 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
835 | /* must always have async data allocated */ |
836 | unsigned needs_async_data : 1; | |
27926b68 JA |
837 | /* should block plug */ |
838 | unsigned plug : 1; | |
e8c2bc1f JA |
839 | /* size of async data needed, if any */ |
840 | unsigned short async_size; | |
0f203765 | 841 | unsigned work_flags; |
d3656344 JA |
842 | }; |
843 | ||
0918682b | 844 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
845 | [IORING_OP_NOP] = {}, |
846 | [IORING_OP_READV] = { | |
d3656344 JA |
847 | .needs_file = 1, |
848 | .unbound_nonreg_file = 1, | |
8a72758c | 849 | .pollin = 1, |
4d954c25 | 850 | .buffer_select = 1, |
e8c2bc1f | 851 | .needs_async_data = 1, |
27926b68 | 852 | .plug = 1, |
e8c2bc1f | 853 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 854 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 855 | }, |
0463b6c5 | 856 | [IORING_OP_WRITEV] = { |
d3656344 JA |
857 | .needs_file = 1, |
858 | .hash_reg_file = 1, | |
859 | .unbound_nonreg_file = 1, | |
8a72758c | 860 | .pollout = 1, |
e8c2bc1f | 861 | .needs_async_data = 1, |
27926b68 | 862 | .plug = 1, |
e8c2bc1f | 863 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
864 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
865 | IO_WQ_WORK_FSIZE, | |
d3656344 | 866 | }, |
0463b6c5 | 867 | [IORING_OP_FSYNC] = { |
d3656344 | 868 | .needs_file = 1, |
0f203765 | 869 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 870 | }, |
0463b6c5 | 871 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
872 | .needs_file = 1, |
873 | .unbound_nonreg_file = 1, | |
8a72758c | 874 | .pollin = 1, |
27926b68 | 875 | .plug = 1, |
e8c2bc1f | 876 | .async_size = sizeof(struct io_async_rw), |
4017eb91 | 877 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
d3656344 | 878 | }, |
0463b6c5 | 879 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
880 | .needs_file = 1, |
881 | .hash_reg_file = 1, | |
882 | .unbound_nonreg_file = 1, | |
8a72758c | 883 | .pollout = 1, |
27926b68 | 884 | .plug = 1, |
e8c2bc1f | 885 | .async_size = sizeof(struct io_async_rw), |
4017eb91 JA |
886 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
887 | IO_WQ_WORK_MM, | |
d3656344 | 888 | }, |
0463b6c5 | 889 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
890 | .needs_file = 1, |
891 | .unbound_nonreg_file = 1, | |
892 | }, | |
0463b6c5 PB |
893 | [IORING_OP_POLL_REMOVE] = {}, |
894 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 895 | .needs_file = 1, |
0f203765 | 896 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 897 | }, |
0463b6c5 | 898 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
899 | .needs_file = 1, |
900 | .unbound_nonreg_file = 1, | |
8a72758c | 901 | .pollout = 1, |
e8c2bc1f JA |
902 | .needs_async_data = 1, |
903 | .async_size = sizeof(struct io_async_msghdr), | |
92c75f75 JA |
904 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
905 | IO_WQ_WORK_FS, | |
d3656344 | 906 | }, |
0463b6c5 | 907 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
908 | .needs_file = 1, |
909 | .unbound_nonreg_file = 1, | |
8a72758c | 910 | .pollin = 1, |
52de1fe1 | 911 | .buffer_select = 1, |
e8c2bc1f JA |
912 | .needs_async_data = 1, |
913 | .async_size = sizeof(struct io_async_msghdr), | |
92c75f75 JA |
914 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
915 | IO_WQ_WORK_FS, | |
d3656344 | 916 | }, |
0463b6c5 | 917 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
918 | .needs_async_data = 1, |
919 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 920 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 921 | }, |
9c8e11b3 PB |
922 | [IORING_OP_TIMEOUT_REMOVE] = { |
923 | /* used by timeout updates' prep() */ | |
924 | .work_flags = IO_WQ_WORK_MM, | |
925 | }, | |
0463b6c5 | 926 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
927 | .needs_file = 1, |
928 | .unbound_nonreg_file = 1, | |
8a72758c | 929 | .pollin = 1, |
0f203765 | 930 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
d3656344 | 931 | }, |
0463b6c5 PB |
932 | [IORING_OP_ASYNC_CANCEL] = {}, |
933 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
934 | .needs_async_data = 1, |
935 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 936 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 937 | }, |
0463b6c5 | 938 | [IORING_OP_CONNECT] = { |
d3656344 JA |
939 | .needs_file = 1, |
940 | .unbound_nonreg_file = 1, | |
8a72758c | 941 | .pollout = 1, |
e8c2bc1f JA |
942 | .needs_async_data = 1, |
943 | .async_size = sizeof(struct io_async_connect), | |
0f203765 | 944 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 945 | }, |
0463b6c5 | 946 | [IORING_OP_FALLOCATE] = { |
d3656344 | 947 | .needs_file = 1, |
69228338 | 948 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
d3656344 | 949 | }, |
0463b6c5 | 950 | [IORING_OP_OPENAT] = { |
0f203765 | 951 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
14587a46 | 952 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
d3656344 | 953 | }, |
0463b6c5 | 954 | [IORING_OP_CLOSE] = { |
0f203765 | 955 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 956 | }, |
0463b6c5 | 957 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 958 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 959 | }, |
0463b6c5 | 960 | [IORING_OP_STATX] = { |
0f203765 JA |
961 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
962 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 963 | }, |
0463b6c5 | 964 | [IORING_OP_READ] = { |
3a6820f2 JA |
965 | .needs_file = 1, |
966 | .unbound_nonreg_file = 1, | |
8a72758c | 967 | .pollin = 1, |
bcda7baa | 968 | .buffer_select = 1, |
27926b68 | 969 | .plug = 1, |
e8c2bc1f | 970 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 971 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 972 | }, |
0463b6c5 | 973 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
974 | .needs_file = 1, |
975 | .unbound_nonreg_file = 1, | |
8a72758c | 976 | .pollout = 1, |
27926b68 | 977 | .plug = 1, |
e8c2bc1f | 978 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
979 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
980 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 981 | }, |
0463b6c5 | 982 | [IORING_OP_FADVISE] = { |
4840e418 | 983 | .needs_file = 1, |
0f203765 | 984 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 985 | }, |
0463b6c5 | 986 | [IORING_OP_MADVISE] = { |
0f203765 | 987 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 988 | }, |
0463b6c5 | 989 | [IORING_OP_SEND] = { |
fddaface JA |
990 | .needs_file = 1, |
991 | .unbound_nonreg_file = 1, | |
8a72758c | 992 | .pollout = 1, |
0f203765 | 993 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 994 | }, |
0463b6c5 | 995 | [IORING_OP_RECV] = { |
fddaface JA |
996 | .needs_file = 1, |
997 | .unbound_nonreg_file = 1, | |
8a72758c | 998 | .pollin = 1, |
bcda7baa | 999 | .buffer_select = 1, |
0f203765 | 1000 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 1001 | }, |
0463b6c5 | 1002 | [IORING_OP_OPENAT2] = { |
0f203765 | 1003 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
14587a46 | 1004 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
cebdb986 | 1005 | }, |
3e4827b0 JA |
1006 | [IORING_OP_EPOLL_CTL] = { |
1007 | .unbound_nonreg_file = 1, | |
0f203765 | 1008 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 1009 | }, |
7d67af2c PB |
1010 | [IORING_OP_SPLICE] = { |
1011 | .needs_file = 1, | |
1012 | .hash_reg_file = 1, | |
1013 | .unbound_nonreg_file = 1, | |
0f203765 | 1014 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
1015 | }, |
1016 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1017 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1018 | [IORING_OP_TEE] = { |
1019 | .needs_file = 1, | |
1020 | .hash_reg_file = 1, | |
1021 | .unbound_nonreg_file = 1, | |
1022 | }, | |
36f4fa68 JA |
1023 | [IORING_OP_SHUTDOWN] = { |
1024 | .needs_file = 1, | |
1025 | }, | |
80a261fd JA |
1026 | [IORING_OP_RENAMEAT] = { |
1027 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
1028 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
1029 | }, | |
14a1143b JA |
1030 | [IORING_OP_UNLINKAT] = { |
1031 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
1032 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
1033 | }, | |
d3656344 JA |
1034 | }; |
1035 | ||
9936c7c2 PB |
1036 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1037 | struct task_struct *task, | |
1038 | struct files_struct *files); | |
269bbe5f | 1039 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 1040 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 1041 | struct io_ring_ctx *ctx); |
f2303b1f | 1042 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
1ffc5422 | 1043 | |
23faba36 | 1044 | static bool io_rw_reissue(struct io_kiocb *req); |
78e19bbe | 1045 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1046 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1047 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1048 | static void io_double_put_req(struct io_kiocb *req); |
c7dae4ba JA |
1049 | static void io_dismantle_req(struct io_kiocb *req); |
1050 | static void io_put_task(struct task_struct *task, int nr); | |
1051 | static void io_queue_next(struct io_kiocb *req); | |
94ae5e77 | 1052 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1053 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1054 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1055 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1056 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1057 | unsigned nr_args); |
3ca405eb | 1058 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1059 | static struct file *io_file_get(struct io_submit_state *state, |
1060 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1061 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1062 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1063 | |
847595de PB |
1064 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1065 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1066 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1067 | const struct iovec *fast_iov, | |
227c0c96 | 1068 | struct iov_iter *iter, bool force); |
907d1df3 | 1069 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1070 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1071 | struct io_ring_ctx *ctx); | |
de0617e4 | 1072 | |
2b188cc1 JA |
1073 | static struct kmem_cache *req_cachep; |
1074 | ||
0918682b | 1075 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1076 | |
1077 | struct sock *io_uring_get_socket(struct file *file) | |
1078 | { | |
1079 | #if defined(CONFIG_UNIX) | |
1080 | if (file->f_op == &io_uring_fops) { | |
1081 | struct io_ring_ctx *ctx = file->private_data; | |
1082 | ||
1083 | return ctx->ring_sock->sk; | |
1084 | } | |
1085 | #endif | |
1086 | return NULL; | |
1087 | } | |
1088 | EXPORT_SYMBOL(io_uring_get_socket); | |
1089 | ||
f2f87370 PB |
1090 | #define io_for_each_link(pos, head) \ |
1091 | for (pos = (head); pos; pos = pos->link) | |
1092 | ||
3ca405eb PB |
1093 | static inline void io_clean_op(struct io_kiocb *req) |
1094 | { | |
9d5c8190 | 1095 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1096 | __io_clean_op(req); |
1097 | } | |
1098 | ||
36f72fe2 PB |
1099 | static inline void io_set_resource_node(struct io_kiocb *req) |
1100 | { | |
1101 | struct io_ring_ctx *ctx = req->ctx; | |
1102 | ||
269bbe5f BM |
1103 | if (!req->fixed_rsrc_refs) { |
1104 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1105 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1106 | } |
1107 | } | |
1108 | ||
08d23634 PB |
1109 | static bool io_match_task(struct io_kiocb *head, |
1110 | struct task_struct *task, | |
1111 | struct files_struct *files) | |
1112 | { | |
1113 | struct io_kiocb *req; | |
1114 | ||
84965ff8 JA |
1115 | if (task && head->task != task) { |
1116 | /* in terms of cancelation, always match if req task is dead */ | |
1117 | if (head->task->flags & PF_EXITING) | |
1118 | return true; | |
08d23634 | 1119 | return false; |
84965ff8 | 1120 | } |
08d23634 PB |
1121 | if (!files) |
1122 | return true; | |
1123 | ||
1124 | io_for_each_link(req, head) { | |
02a13674 JA |
1125 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1126 | continue; | |
1127 | if (req->file && req->file->f_op == &io_uring_fops) | |
1128 | return true; | |
1129 | if ((req->work.flags & IO_WQ_WORK_FILES) && | |
08d23634 PB |
1130 | req->work.identity->files == files) |
1131 | return true; | |
1132 | } | |
1133 | return false; | |
1134 | } | |
1135 | ||
28cea78a | 1136 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1137 | { |
28cea78a | 1138 | struct files_struct *files = current->files; |
c40f6379 JA |
1139 | struct mm_struct *mm = current->mm; |
1140 | ||
1141 | if (mm) { | |
1142 | kthread_unuse_mm(mm); | |
1143 | mmput(mm); | |
4b70cf9d | 1144 | current->mm = NULL; |
c40f6379 | 1145 | } |
28cea78a JA |
1146 | if (files) { |
1147 | struct nsproxy *nsproxy = current->nsproxy; | |
1148 | ||
1149 | task_lock(current); | |
1150 | current->files = NULL; | |
1151 | current->nsproxy = NULL; | |
1152 | task_unlock(current); | |
1153 | put_files_struct(files); | |
1154 | put_nsproxy(nsproxy); | |
1155 | } | |
1156 | } | |
1157 | ||
1a38ffc9 | 1158 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a JA |
1159 | { |
1160 | if (!current->files) { | |
1161 | struct files_struct *files; | |
1162 | struct nsproxy *nsproxy; | |
1163 | ||
1164 | task_lock(ctx->sqo_task); | |
1165 | files = ctx->sqo_task->files; | |
1166 | if (!files) { | |
1167 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1168 | return -EOWNERDEAD; |
28cea78a JA |
1169 | } |
1170 | atomic_inc(&files->count); | |
1171 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1172 | nsproxy = ctx->sqo_task->nsproxy; | |
1173 | task_unlock(ctx->sqo_task); | |
1174 | ||
1175 | task_lock(current); | |
1176 | current->files = files; | |
1177 | current->nsproxy = nsproxy; | |
1178 | task_unlock(current); | |
1179 | } | |
1a38ffc9 | 1180 | return 0; |
c40f6379 JA |
1181 | } |
1182 | ||
1183 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1184 | { | |
4b70cf9d JA |
1185 | struct mm_struct *mm; |
1186 | ||
1187 | if (current->mm) | |
1188 | return 0; | |
1189 | ||
4b70cf9d JA |
1190 | task_lock(ctx->sqo_task); |
1191 | mm = ctx->sqo_task->mm; | |
1192 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1193 | mm = NULL; | |
1194 | task_unlock(ctx->sqo_task); | |
1195 | ||
1196 | if (mm) { | |
1197 | kthread_use_mm(mm); | |
1198 | return 0; | |
c40f6379 JA |
1199 | } |
1200 | ||
4b70cf9d | 1201 | return -EFAULT; |
c40f6379 JA |
1202 | } |
1203 | ||
4e326358 PB |
1204 | static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1205 | struct io_kiocb *req) | |
c40f6379 | 1206 | { |
28cea78a | 1207 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1208 | int ret; |
28cea78a JA |
1209 | |
1210 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1211 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1212 | if (unlikely(ret)) |
1213 | return ret; | |
1214 | } | |
1215 | ||
1a38ffc9 PB |
1216 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1217 | ret = __io_sq_thread_acquire_files(ctx); | |
1218 | if (unlikely(ret)) | |
1219 | return ret; | |
1220 | } | |
28cea78a JA |
1221 | |
1222 | return 0; | |
c40f6379 JA |
1223 | } |
1224 | ||
4e326358 PB |
1225 | static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1226 | struct io_kiocb *req) | |
1227 | { | |
4e326358 PB |
1228 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
1229 | return 0; | |
1230 | return __io_sq_thread_acquire_mm_files(ctx, req); | |
1231 | } | |
1232 | ||
91d8f519 DZ |
1233 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1234 | struct cgroup_subsys_state **cur_css) | |
1235 | ||
1236 | { | |
1237 | #ifdef CONFIG_BLK_CGROUP | |
1238 | /* puts the old one when swapping */ | |
1239 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1240 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1241 | *cur_css = ctx->sqo_blkcg_css; | |
1242 | } | |
1243 | #endif | |
1244 | } | |
1245 | ||
1246 | static void io_sq_thread_unassociate_blkcg(void) | |
1247 | { | |
1248 | #ifdef CONFIG_BLK_CGROUP | |
1249 | kthread_associate_blkcg(NULL); | |
1250 | #endif | |
1251 | } | |
1252 | ||
c40f6379 JA |
1253 | static inline void req_set_fail_links(struct io_kiocb *req) |
1254 | { | |
1255 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1256 | req->flags |= REQ_F_FAIL_LINK; | |
1257 | } | |
4a38aed2 | 1258 | |
1e6fa521 JA |
1259 | /* |
1260 | * None of these are dereferenced, they are simply used to check if any of | |
1261 | * them have changed. If we're under current and check they are still the | |
1262 | * same, we're fine to grab references to them for actual out-of-line use. | |
1263 | */ | |
1264 | static void io_init_identity(struct io_identity *id) | |
1265 | { | |
1266 | id->files = current->files; | |
1267 | id->mm = current->mm; | |
1268 | #ifdef CONFIG_BLK_CGROUP | |
1269 | rcu_read_lock(); | |
1270 | id->blkcg_css = blkcg_css(); | |
1271 | rcu_read_unlock(); | |
1272 | #endif | |
1273 | id->creds = current_cred(); | |
1274 | id->nsproxy = current->nsproxy; | |
1275 | id->fs = current->fs; | |
1276 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1277 | #ifdef CONFIG_AUDIT |
1278 | id->loginuid = current->loginuid; | |
1279 | id->sessionid = current->sessionid; | |
1280 | #endif | |
1e6fa521 JA |
1281 | refcount_set(&id->count, 1); |
1282 | } | |
1283 | ||
ec99ca6c PB |
1284 | static inline void __io_req_init_async(struct io_kiocb *req) |
1285 | { | |
1286 | memset(&req->work, 0, sizeof(req->work)); | |
1287 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1288 | } | |
1289 | ||
7cdaf587 XW |
1290 | /* |
1291 | * Note: must call io_req_init_async() for the first time you | |
1292 | * touch any members of io_wq_work. | |
1293 | */ | |
1294 | static inline void io_req_init_async(struct io_kiocb *req) | |
1295 | { | |
500a373d JA |
1296 | struct io_uring_task *tctx = current->io_uring; |
1297 | ||
7cdaf587 XW |
1298 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1299 | return; | |
1300 | ||
ec99ca6c | 1301 | __io_req_init_async(req); |
500a373d JA |
1302 | |
1303 | /* Grab a ref if this isn't our static identity */ | |
1304 | req->work.identity = tctx->identity; | |
1305 | if (tctx->identity != &tctx->__identity) | |
1306 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1307 | } |
1308 | ||
2b188cc1 JA |
1309 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1310 | { | |
1311 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1312 | ||
0f158b4c | 1313 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1314 | } |
1315 | ||
8eb7e2d0 PB |
1316 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1317 | { | |
1318 | return !req->timeout.off; | |
1319 | } | |
1320 | ||
2b188cc1 JA |
1321 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1322 | { | |
1323 | struct io_ring_ctx *ctx; | |
78076bb6 | 1324 | int hash_bits; |
2b188cc1 JA |
1325 | |
1326 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1327 | if (!ctx) | |
1328 | return NULL; | |
1329 | ||
78076bb6 JA |
1330 | /* |
1331 | * Use 5 bits less than the max cq entries, that should give us around | |
1332 | * 32 entries per hash list if totally full and uniformly spread. | |
1333 | */ | |
1334 | hash_bits = ilog2(p->cq_entries); | |
1335 | hash_bits -= 5; | |
1336 | if (hash_bits <= 0) | |
1337 | hash_bits = 1; | |
1338 | ctx->cancel_hash_bits = hash_bits; | |
1339 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1340 | GFP_KERNEL); | |
1341 | if (!ctx->cancel_hash) | |
1342 | goto err; | |
1343 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1344 | ||
21482896 | 1345 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1346 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1347 | goto err; | |
2b188cc1 JA |
1348 | |
1349 | ctx->flags = p->flags; | |
90554200 | 1350 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1351 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1352 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1353 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1354 | init_completion(&ctx->ref_comp); |
1355 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1356 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1357 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1358 | mutex_init(&ctx->uring_lock); |
1359 | init_waitqueue_head(&ctx->wait); | |
1360 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1361 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1362 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1363 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1364 | spin_lock_init(&ctx->inflight_lock); |
1365 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1366 | spin_lock_init(&ctx->rsrc_ref_lock); |
1367 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1368 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1369 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1370 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1371 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1372 | return ctx; |
206aefde | 1373 | err: |
78076bb6 | 1374 | kfree(ctx->cancel_hash); |
206aefde JA |
1375 | kfree(ctx); |
1376 | return NULL; | |
2b188cc1 JA |
1377 | } |
1378 | ||
9cf7c104 | 1379 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1380 | { |
2bc9930e JA |
1381 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1382 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1383 | |
9cf7c104 | 1384 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1385 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1386 | } |
de0617e4 | 1387 | |
9d858b21 | 1388 | return false; |
de0617e4 JA |
1389 | } |
1390 | ||
5c3462cf | 1391 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1392 | { |
500a373d | 1393 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1394 | return; |
1395 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1396 | kfree(req->work.identity); | |
1397 | } | |
1398 | ||
4edf20f9 | 1399 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1400 | { |
7cdaf587 | 1401 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1402 | return; |
51a4cc11 | 1403 | |
e86d0047 | 1404 | if (req->work.flags & IO_WQ_WORK_MM) |
98447d65 | 1405 | mmdrop(req->work.identity->mm); |
91d8f519 | 1406 | #ifdef CONFIG_BLK_CGROUP |
e86d0047 | 1407 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
98447d65 | 1408 | css_put(req->work.identity->blkcg_css); |
91d8f519 | 1409 | #endif |
e86d0047 | 1410 | if (req->work.flags & IO_WQ_WORK_CREDS) |
98447d65 | 1411 | put_cred(req->work.identity->creds); |
dfead8a8 | 1412 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1413 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1414 | |
98447d65 | 1415 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1416 | if (--fs->users) |
1417 | fs = NULL; | |
98447d65 | 1418 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1419 | if (fs) |
1420 | free_fs_struct(fs); | |
1421 | } | |
34e08fed PB |
1422 | if (req->work.flags & IO_WQ_WORK_FILES) { |
1423 | put_files_struct(req->work.identity->files); | |
1424 | put_nsproxy(req->work.identity->nsproxy); | |
34e08fed PB |
1425 | } |
1426 | if (req->flags & REQ_F_INFLIGHT) { | |
1427 | struct io_ring_ctx *ctx = req->ctx; | |
1428 | struct io_uring_task *tctx = req->task->io_uring; | |
1429 | unsigned long flags; | |
1430 | ||
1431 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1432 | list_del(&req->inflight_entry); | |
1433 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1434 | req->flags &= ~REQ_F_INFLIGHT; | |
1435 | if (atomic_read(&tctx->in_idle)) | |
1436 | wake_up(&tctx->wait); | |
1437 | } | |
51a4cc11 | 1438 | |
e86d0047 PB |
1439 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
1440 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | | |
1441 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); | |
5c3462cf | 1442 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1443 | } |
1444 | ||
1e6fa521 JA |
1445 | /* |
1446 | * Create a private copy of io_identity, since some fields don't match | |
1447 | * the current context. | |
1448 | */ | |
1449 | static bool io_identity_cow(struct io_kiocb *req) | |
1450 | { | |
5c3462cf | 1451 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1452 | const struct cred *creds = NULL; |
1453 | struct io_identity *id; | |
1454 | ||
1455 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1456 | creds = req->work.identity->creds; | |
1457 | ||
1458 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1459 | if (unlikely(!id)) { | |
1460 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1461 | return false; | |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | * We can safely just re-init the creds we copied Either the field | |
1466 | * matches the current one, or we haven't grabbed it yet. The only | |
1467 | * exception is ->creds, through registered personalities, so handle | |
1468 | * that one separately. | |
1469 | */ | |
1470 | io_init_identity(id); | |
1471 | if (creds) | |
e8c954df | 1472 | id->creds = creds; |
1e6fa521 JA |
1473 | |
1474 | /* add one for this request */ | |
1475 | refcount_inc(&id->count); | |
1476 | ||
cb8a8ae3 JA |
1477 | /* drop tctx and req identity references, if needed */ |
1478 | if (tctx->identity != &tctx->__identity && | |
1479 | refcount_dec_and_test(&tctx->identity->count)) | |
1480 | kfree(tctx->identity); | |
1481 | if (req->work.identity != &tctx->__identity && | |
1482 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1483 | kfree(req->work.identity); |
1484 | ||
1485 | req->work.identity = id; | |
500a373d | 1486 | tctx->identity = id; |
1e6fa521 JA |
1487 | return true; |
1488 | } | |
1489 | ||
ce3d5aae PB |
1490 | static void io_req_track_inflight(struct io_kiocb *req) |
1491 | { | |
1492 | struct io_ring_ctx *ctx = req->ctx; | |
1493 | ||
1494 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1495 | io_req_init_async(req); | |
1496 | req->flags |= REQ_F_INFLIGHT; | |
1497 | ||
1498 | spin_lock_irq(&ctx->inflight_lock); | |
1499 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1500 | spin_unlock_irq(&ctx->inflight_lock); | |
1501 | } | |
1502 | } | |
1503 | ||
1e6fa521 | 1504 | static bool io_grab_identity(struct io_kiocb *req) |
18d9be1a | 1505 | { |
d3656344 | 1506 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1507 | struct io_identity *id = req->work.identity; |
54a91f3b | 1508 | |
69228338 JA |
1509 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1510 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1511 | return false; | |
1512 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1513 | } | |
91d8f519 | 1514 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1515 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1516 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1517 | rcu_read_lock(); |
1e6fa521 JA |
1518 | if (id->blkcg_css != blkcg_css()) { |
1519 | rcu_read_unlock(); | |
1520 | return false; | |
1521 | } | |
91d8f519 DZ |
1522 | /* |
1523 | * This should be rare, either the cgroup is dying or the task | |
1524 | * is moving cgroups. Just punt to root for the handful of ios. | |
1525 | */ | |
1e6fa521 | 1526 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1527 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1528 | rcu_read_unlock(); |
1529 | } | |
1530 | #endif | |
dfead8a8 | 1531 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1532 | if (id->creds != current_cred()) |
1533 | return false; | |
1534 | get_cred(id->creds); | |
dfead8a8 JA |
1535 | req->work.flags |= IO_WQ_WORK_CREDS; |
1536 | } | |
4ea33a97 JA |
1537 | #ifdef CONFIG_AUDIT |
1538 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1539 | current->sessionid != id->sessionid) | |
1540 | return false; | |
1541 | #endif | |
dfead8a8 JA |
1542 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1543 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1544 | if (current->fs != id->fs) |
1545 | return false; | |
1546 | spin_lock(&id->fs->lock); | |
1547 | if (!id->fs->in_exec) { | |
1548 | id->fs->users++; | |
dfead8a8 | 1549 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1550 | } else { |
1551 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1552 | } | |
1553 | spin_unlock(¤t->fs->lock); | |
1554 | } | |
af604703 PB |
1555 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1556 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1557 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1558 | if (id->files != current->files || | |
1559 | id->nsproxy != current->nsproxy) | |
1560 | return false; | |
1561 | atomic_inc(&id->files->count); | |
1562 | get_nsproxy(id->nsproxy); | |
af604703 | 1563 | req->work.flags |= IO_WQ_WORK_FILES; |
ce3d5aae | 1564 | io_req_track_inflight(req); |
af604703 | 1565 | } |
77788775 JA |
1566 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1567 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1568 | if (id->mm != current->mm) | |
1569 | return false; | |
1570 | mmgrab(id->mm); | |
1571 | req->work.flags |= IO_WQ_WORK_MM; | |
1572 | } | |
1e6fa521 JA |
1573 | |
1574 | return true; | |
1575 | } | |
1576 | ||
1577 | static void io_prep_async_work(struct io_kiocb *req) | |
1578 | { | |
1579 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1580 | struct io_ring_ctx *ctx = req->ctx; |
1581 | ||
1582 | io_req_init_async(req); | |
1583 | ||
feaadc4f PB |
1584 | if (req->flags & REQ_F_FORCE_ASYNC) |
1585 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1586 | ||
1e6fa521 JA |
1587 | if (req->flags & REQ_F_ISREG) { |
1588 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1589 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1590 | } else { | |
1591 | if (def->unbound_nonreg_file) | |
1592 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1593 | } | |
1594 | ||
1e6fa521 JA |
1595 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1596 | if (io_grab_identity(req)) | |
1597 | return; | |
1598 | ||
1599 | if (!io_identity_cow(req)) | |
1600 | return; | |
1601 | ||
1602 | /* can't fail at this point */ | |
1603 | if (!io_grab_identity(req)) | |
1604 | WARN_ON(1); | |
561fb04a | 1605 | } |
cccf0ee8 | 1606 | |
cbdcb435 | 1607 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1608 | { |
cbdcb435 | 1609 | struct io_kiocb *cur; |
54a91f3b | 1610 | |
f2f87370 PB |
1611 | io_for_each_link(cur, req) |
1612 | io_prep_async_work(cur); | |
561fb04a JA |
1613 | } |
1614 | ||
7271ef3a | 1615 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1616 | { |
a197f664 | 1617 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1618 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1619 | |
8766dd51 PB |
1620 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1621 | &req->work, req->flags); | |
1622 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1623 | return link; |
18d9be1a JA |
1624 | } |
1625 | ||
cbdcb435 PB |
1626 | static void io_queue_async_work(struct io_kiocb *req) |
1627 | { | |
7271ef3a JA |
1628 | struct io_kiocb *link; |
1629 | ||
cbdcb435 PB |
1630 | /* init ->work of the whole link before punting */ |
1631 | io_prep_async_link(req); | |
7271ef3a JA |
1632 | link = __io_queue_async_work(req); |
1633 | ||
1634 | if (link) | |
1635 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1636 | } |
1637 | ||
5262f567 JA |
1638 | static void io_kill_timeout(struct io_kiocb *req) |
1639 | { | |
e8c2bc1f | 1640 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1641 | int ret; |
1642 | ||
e8c2bc1f | 1643 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1644 | if (ret != -1) { |
01cec8c1 PB |
1645 | atomic_set(&req->ctx->cq_timeouts, |
1646 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1647 | list_del_init(&req->timeout.list); |
78e19bbe | 1648 | io_cqring_fill_event(req, 0); |
216578e5 | 1649 | io_put_req_deferred(req, 1); |
5262f567 JA |
1650 | } |
1651 | } | |
1652 | ||
76e1b642 JA |
1653 | /* |
1654 | * Returns true if we found and killed one or more timeouts | |
1655 | */ | |
6b81928d PB |
1656 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1657 | struct files_struct *files) | |
5262f567 JA |
1658 | { |
1659 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1660 | int canceled = 0; |
5262f567 JA |
1661 | |
1662 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1663 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1664 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1665 | io_kill_timeout(req); |
76e1b642 JA |
1666 | canceled++; |
1667 | } | |
f3606e3a | 1668 | } |
5262f567 | 1669 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1670 | return canceled != 0; |
5262f567 JA |
1671 | } |
1672 | ||
04518945 | 1673 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1674 | { |
04518945 | 1675 | do { |
27dc8338 PB |
1676 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1677 | struct io_defer_entry, list); | |
de0617e4 | 1678 | |
9cf7c104 | 1679 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1680 | break; |
27dc8338 | 1681 | list_del_init(&de->list); |
907d1df3 | 1682 | io_req_task_queue(de->req); |
27dc8338 | 1683 | kfree(de); |
04518945 PB |
1684 | } while (!list_empty(&ctx->defer_list)); |
1685 | } | |
1686 | ||
360428f8 | 1687 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1688 | { |
f010505b MDG |
1689 | u32 seq; |
1690 | ||
1691 | if (list_empty(&ctx->timeout_list)) | |
1692 | return; | |
1693 | ||
1694 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1695 | ||
1696 | do { | |
1697 | u32 events_needed, events_got; | |
360428f8 | 1698 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1699 | struct io_kiocb, timeout.list); |
de0617e4 | 1700 | |
8eb7e2d0 | 1701 | if (io_is_timeout_noseq(req)) |
360428f8 | 1702 | break; |
f010505b MDG |
1703 | |
1704 | /* | |
1705 | * Since seq can easily wrap around over time, subtract | |
1706 | * the last seq at which timeouts were flushed before comparing. | |
1707 | * Assuming not more than 2^31-1 events have happened since, | |
1708 | * these subtractions won't have wrapped, so we can check if | |
1709 | * target is in [last_seq, current_seq] by comparing the two. | |
1710 | */ | |
1711 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1712 | events_got = seq - ctx->cq_last_tm_flush; | |
1713 | if (events_got < events_needed) | |
360428f8 | 1714 | break; |
bfe68a22 | 1715 | |
135fcde8 | 1716 | list_del_init(&req->timeout.list); |
5262f567 | 1717 | io_kill_timeout(req); |
f010505b MDG |
1718 | } while (!list_empty(&ctx->timeout_list)); |
1719 | ||
1720 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1721 | } |
5262f567 | 1722 | |
360428f8 PB |
1723 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1724 | { | |
1725 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1726 | |
1727 | /* order cqe stores with ring update */ | |
1728 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1729 | |
04518945 PB |
1730 | if (unlikely(!list_empty(&ctx->defer_list))) |
1731 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1732 | } |
1733 | ||
90554200 JA |
1734 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1735 | { | |
1736 | struct io_rings *r = ctx->rings; | |
1737 | ||
1738 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1739 | } | |
1740 | ||
888aae2e PB |
1741 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1742 | { | |
1743 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1744 | } | |
1745 | ||
2b188cc1 JA |
1746 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1747 | { | |
75b28aff | 1748 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1749 | unsigned tail; |
1750 | ||
115e12e5 SB |
1751 | /* |
1752 | * writes to the cq entry need to come after reading head; the | |
1753 | * control dependency is enough as we're using WRITE_ONCE to | |
1754 | * fill the cq entry | |
1755 | */ | |
888aae2e | 1756 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1757 | return NULL; |
1758 | ||
888aae2e | 1759 | tail = ctx->cached_cq_tail++; |
75b28aff | 1760 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1761 | } |
1762 | ||
f2842ab5 JA |
1763 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1764 | { | |
f0b493e6 JA |
1765 | if (!ctx->cq_ev_fd) |
1766 | return false; | |
7e55a19c SG |
1767 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1768 | return false; | |
f2842ab5 JA |
1769 | if (!ctx->eventfd_async) |
1770 | return true; | |
b41e9852 | 1771 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1772 | } |
1773 | ||
b41e9852 | 1774 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1775 | { |
b1445e59 PB |
1776 | /* see waitqueue_active() comment */ |
1777 | smp_mb(); | |
1778 | ||
1d7bb1d5 JA |
1779 | if (waitqueue_active(&ctx->wait)) |
1780 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1781 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1782 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1783 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1784 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1785 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1786 | wake_up_interruptible(&ctx->cq_wait); |
1787 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1788 | } | |
1d7bb1d5 JA |
1789 | } |
1790 | ||
80c18e4a PB |
1791 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1792 | { | |
b1445e59 PB |
1793 | /* see waitqueue_active() comment */ |
1794 | smp_mb(); | |
1795 | ||
80c18e4a PB |
1796 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1797 | if (waitqueue_active(&ctx->wait)) | |
1798 | wake_up(&ctx->wait); | |
1799 | } | |
1800 | if (io_should_trigger_evfd(ctx)) | |
1801 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1802 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1803 | wake_up_interruptible(&ctx->cq_wait); |
1804 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1805 | } | |
80c18e4a PB |
1806 | } |
1807 | ||
c4a2ed72 | 1808 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1809 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1810 | struct task_struct *tsk, | |
1811 | struct files_struct *files) | |
1d7bb1d5 JA |
1812 | { |
1813 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1814 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1815 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1816 | unsigned long flags; |
b18032bb | 1817 | bool all_flushed, posted; |
1d7bb1d5 JA |
1818 | LIST_HEAD(list); |
1819 | ||
e23de15f PB |
1820 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1821 | return false; | |
1d7bb1d5 | 1822 | |
b18032bb | 1823 | posted = false; |
1d7bb1d5 | 1824 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1825 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1826 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1827 | continue; |
1828 | ||
1d7bb1d5 JA |
1829 | cqe = io_get_cqring(ctx); |
1830 | if (!cqe && !force) | |
1831 | break; | |
1832 | ||
40d8ddd4 | 1833 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1834 | if (cqe) { |
1835 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1836 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1837 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1838 | } else { |
2c3bac6d | 1839 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1840 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1841 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1842 | } |
b18032bb | 1843 | posted = true; |
1d7bb1d5 JA |
1844 | } |
1845 | ||
09e88404 PB |
1846 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1847 | if (all_flushed) { | |
1848 | clear_bit(0, &ctx->sq_check_overflow); | |
1849 | clear_bit(0, &ctx->cq_check_overflow); | |
1850 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1851 | } | |
46930143 | 1852 | |
b18032bb JA |
1853 | if (posted) |
1854 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1855 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1856 | if (posted) |
1857 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1858 | |
1859 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1860 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1861 | list_del(&req->compl.list); | |
ec9c02ad | 1862 | io_put_req(req); |
1d7bb1d5 | 1863 | } |
c4a2ed72 | 1864 | |
09e88404 | 1865 | return all_flushed; |
1d7bb1d5 JA |
1866 | } |
1867 | ||
6c503150 PB |
1868 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1869 | struct task_struct *tsk, | |
1870 | struct files_struct *files) | |
1871 | { | |
1872 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1873 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1874 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1875 | mutex_lock(&ctx->uring_lock); | |
1876 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1877 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1878 | mutex_unlock(&ctx->uring_lock); | |
1879 | } | |
1880 | } | |
1881 | ||
bcda7baa | 1882 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1883 | { |
78e19bbe | 1884 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1885 | struct io_uring_cqe *cqe; |
1886 | ||
78e19bbe | 1887 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1888 | |
2b188cc1 JA |
1889 | /* |
1890 | * If we can't get a cq entry, userspace overflowed the | |
1891 | * submission (by quite a lot). Increment the overflow count in | |
1892 | * the ring. | |
1893 | */ | |
1894 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1895 | if (likely(cqe)) { |
78e19bbe | 1896 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1897 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1898 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1899 | } else if (ctx->cq_overflow_flushed || |
1900 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1901 | /* |
1902 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1903 | * then we cannot store the request for later flushing, we need | |
1904 | * to drop it on the floor. | |
1905 | */ | |
2c3bac6d PB |
1906 | ctx->cached_cq_overflow++; |
1907 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1908 | } else { |
ad3eb2c8 JA |
1909 | if (list_empty(&ctx->cq_overflow_list)) { |
1910 | set_bit(0, &ctx->sq_check_overflow); | |
1911 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1912 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1913 | } |
40d8ddd4 | 1914 | io_clean_op(req); |
1d7bb1d5 | 1915 | req->result = res; |
0f7e466b | 1916 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1917 | refcount_inc(&req->refs); |
1918 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1919 | } |
1920 | } | |
1921 | ||
bcda7baa JA |
1922 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1923 | { | |
1924 | __io_cqring_fill_event(req, res, 0); | |
1925 | } | |
1926 | ||
c7dae4ba JA |
1927 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1928 | unsigned int cflags) | |
2b188cc1 | 1929 | { |
78e19bbe | 1930 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1931 | unsigned long flags; |
1932 | ||
1933 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1934 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1935 | io_commit_cqring(ctx); |
c7dae4ba JA |
1936 | /* |
1937 | * If we're the last reference to this request, add to our locked | |
1938 | * free_list cache. | |
1939 | */ | |
1940 | if (refcount_dec_and_test(&req->refs)) { | |
1941 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1942 | ||
1943 | io_dismantle_req(req); | |
1944 | io_put_task(req->task, 1); | |
1945 | list_add(&req->compl.list, &cs->locked_free_list); | |
1946 | cs->locked_free_nr++; | |
1947 | } else | |
1948 | req = NULL; | |
2b188cc1 JA |
1949 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1950 | ||
8c838788 | 1951 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1952 | if (req) { |
1953 | io_queue_next(req); | |
1954 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1955 | } |
229a7b63 JA |
1956 | } |
1957 | ||
a38d68db | 1958 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1959 | unsigned int cflags) |
229a7b63 | 1960 | { |
a38d68db PB |
1961 | io_clean_op(req); |
1962 | req->result = res; | |
1963 | req->compl.cflags = cflags; | |
e342c807 | 1964 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1965 | } |
1966 | ||
889fca73 PB |
1967 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1968 | long res, unsigned cflags) | |
bcda7baa | 1969 | { |
889fca73 PB |
1970 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1971 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1972 | else |
c7dae4ba | 1973 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1974 | } |
1975 | ||
a38d68db | 1976 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1977 | { |
889fca73 | 1978 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1979 | } |
1980 | ||
c7dae4ba | 1981 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1982 | { |
c7dae4ba JA |
1983 | struct io_submit_state *state = &ctx->submit_state; |
1984 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 1985 | struct io_kiocb *req = NULL; |
0ddf92e8 | 1986 | |
c7dae4ba JA |
1987 | /* |
1988 | * If we have more than a batch's worth of requests in our IRQ side | |
1989 | * locked cache, grab the lock and move them over to our submission | |
1990 | * side cache. | |
1991 | */ | |
1992 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
1993 | spin_lock_irq(&ctx->completion_lock); | |
1994 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1995 | cs->locked_free_nr = 0; | |
1996 | spin_unlock_irq(&ctx->completion_lock); | |
1997 | } | |
0ddf92e8 | 1998 | |
c7dae4ba JA |
1999 | while (!list_empty(&cs->free_list)) { |
2000 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
2001 | compl.list); |
2002 | list_del(&req->compl.list); | |
e5d1bc0a PB |
2003 | state->reqs[state->free_reqs++] = req; |
2004 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
2005 | break; | |
1b4c351f JA |
2006 | } |
2007 | ||
e5d1bc0a | 2008 | return req != NULL; |
0ddf92e8 JA |
2009 | } |
2010 | ||
e5d1bc0a | 2011 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 2012 | { |
e5d1bc0a PB |
2013 | struct io_submit_state *state = &ctx->submit_state; |
2014 | ||
2015 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
2016 | ||
f6b6c7d6 | 2017 | if (!state->free_reqs) { |
291b2821 | 2018 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
2019 | int ret; |
2020 | ||
c7dae4ba | 2021 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
2022 | goto got_req; |
2023 | ||
bf019da7 PB |
2024 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
2025 | state->reqs); | |
fd6fab2c JA |
2026 | |
2027 | /* | |
2028 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2029 | * retry single alloc to be on the safe side. | |
2030 | */ | |
2031 | if (unlikely(ret <= 0)) { | |
2032 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
2033 | if (!state->reqs[0]) | |
3893f39f | 2034 | return NULL; |
fd6fab2c JA |
2035 | ret = 1; |
2036 | } | |
291b2821 | 2037 | state->free_reqs = ret; |
2b188cc1 | 2038 | } |
e5d1bc0a | 2039 | got_req: |
291b2821 PB |
2040 | state->free_reqs--; |
2041 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
2042 | } |
2043 | ||
8da11c19 PB |
2044 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
2045 | bool fixed) | |
2046 | { | |
36f72fe2 | 2047 | if (!fixed) |
8da11c19 PB |
2048 | fput(file); |
2049 | } | |
2050 | ||
4edf20f9 | 2051 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2052 | { |
3ca405eb | 2053 | io_clean_op(req); |
929a3af9 | 2054 | |
e8c2bc1f JA |
2055 | if (req->async_data) |
2056 | kfree(req->async_data); | |
8da11c19 PB |
2057 | if (req->file) |
2058 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
2059 | if (req->fixed_rsrc_refs) |
2060 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 2061 | io_req_clean_work(req); |
e65ef56d JA |
2062 | } |
2063 | ||
7c660731 PB |
2064 | static inline void io_put_task(struct task_struct *task, int nr) |
2065 | { | |
2066 | struct io_uring_task *tctx = task->io_uring; | |
2067 | ||
2068 | percpu_counter_sub(&tctx->inflight, nr); | |
2069 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2070 | wake_up(&tctx->wait); | |
2071 | put_task_struct_many(task, nr); | |
2072 | } | |
2073 | ||
216578e5 | 2074 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2075 | { |
51a4cc11 | 2076 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2077 | |
216578e5 | 2078 | io_dismantle_req(req); |
7c660731 | 2079 | io_put_task(req->task, 1); |
c6ca97b3 | 2080 | |
3893f39f | 2081 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 2082 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
2083 | } |
2084 | ||
f2f87370 PB |
2085 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2086 | { | |
2087 | struct io_kiocb *nxt = req->link; | |
2088 | ||
2089 | req->link = nxt->link; | |
2090 | nxt->link = NULL; | |
2091 | } | |
2092 | ||
c9abd7ad | 2093 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 2094 | { |
a197f664 | 2095 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 2096 | struct io_kiocb *link; |
c9abd7ad PB |
2097 | bool cancelled = false; |
2098 | unsigned long flags; | |
7c86ffee | 2099 | |
c9abd7ad | 2100 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2101 | link = req->link; |
2102 | ||
900fad45 PB |
2103 | /* |
2104 | * Can happen if a linked timeout fired and link had been like | |
2105 | * req -> link t-out -> link t-out [-> ...] | |
2106 | */ | |
c9abd7ad PB |
2107 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
2108 | struct io_timeout_data *io = link->async_data; | |
2109 | int ret; | |
7c86ffee | 2110 | |
f2f87370 | 2111 | io_remove_next_linked(req); |
90cd7e42 | 2112 | link->timeout.head = NULL; |
c9abd7ad PB |
2113 | ret = hrtimer_try_to_cancel(&io->timer); |
2114 | if (ret != -1) { | |
2115 | io_cqring_fill_event(link, -ECANCELED); | |
2116 | io_commit_cqring(ctx); | |
2117 | cancelled = true; | |
2118 | } | |
2119 | } | |
7c86ffee | 2120 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2121 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2122 | |
c9abd7ad | 2123 | if (cancelled) { |
7c86ffee | 2124 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2125 | io_put_req(link); |
2126 | } | |
7c86ffee PB |
2127 | } |
2128 | ||
9e645e11 | 2129 | |
d148ca4b | 2130 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2131 | { |
f2f87370 | 2132 | struct io_kiocb *link, *nxt; |
2665abfd | 2133 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2134 | unsigned long flags; |
9e645e11 | 2135 | |
d148ca4b | 2136 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2137 | link = req->link; |
2138 | req->link = NULL; | |
9e645e11 | 2139 | |
f2f87370 PB |
2140 | while (link) { |
2141 | nxt = link->link; | |
2142 | link->link = NULL; | |
2665abfd | 2143 | |
f2f87370 | 2144 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2145 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2146 | |
2147 | /* | |
2148 | * It's ok to free under spinlock as they're not linked anymore, | |
2149 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2150 | * work.fs->lock. | |
2151 | */ | |
2152 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2153 | io_put_req_deferred(link, 2); | |
2154 | else | |
2155 | io_double_put_req(link); | |
f2f87370 | 2156 | link = nxt; |
9e645e11 | 2157 | } |
2665abfd | 2158 | io_commit_cqring(ctx); |
216578e5 | 2159 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2160 | |
2665abfd | 2161 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2162 | } |
2163 | ||
3fa5e0f3 | 2164 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2165 | { |
7c86ffee PB |
2166 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2167 | io_kill_linked_timeout(req); | |
944e58bf | 2168 | |
9e645e11 JA |
2169 | /* |
2170 | * If LINK is set, we have dependent requests in this chain. If we | |
2171 | * didn't fail this request, queue the first one up, moving any other | |
2172 | * dependencies to the next request. In case of failure, fail the rest | |
2173 | * of the chain. | |
2174 | */ | |
f2f87370 PB |
2175 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2176 | struct io_kiocb *nxt = req->link; | |
2177 | ||
2178 | req->link = NULL; | |
2179 | return nxt; | |
2180 | } | |
9b5f7bd9 PB |
2181 | io_fail_links(req); |
2182 | return NULL; | |
4d7dd462 | 2183 | } |
9e645e11 | 2184 | |
f2f87370 | 2185 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2186 | { |
cdbff982 | 2187 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2188 | return NULL; |
2189 | return __io_req_find_next(req); | |
2190 | } | |
2191 | ||
7cbf1722 | 2192 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 2193 | { |
65453d1e | 2194 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
2195 | struct io_wq_work_list list; |
2196 | struct io_wq_work_node *node; | |
c2c4c83c | 2197 | |
7cbf1722 JA |
2198 | if (wq_list_empty(&tctx->task_list)) |
2199 | return false; | |
6200b0ae | 2200 | |
0b81e80c | 2201 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
2202 | list = tctx->task_list; |
2203 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2204 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 2205 | |
7cbf1722 JA |
2206 | node = list.first; |
2207 | while (node) { | |
2208 | struct io_wq_work_node *next = node->next; | |
65453d1e | 2209 | struct io_ring_ctx *this_ctx; |
7cbf1722 | 2210 | struct io_kiocb *req; |
0ba9c9ed | 2211 | |
7cbf1722 | 2212 | req = container_of(node, struct io_kiocb, io_task_work.node); |
65453d1e | 2213 | this_ctx = req->ctx; |
7cbf1722 JA |
2214 | req->task_work.func(&req->task_work); |
2215 | node = next; | |
65453d1e JA |
2216 | |
2217 | if (!ctx) { | |
2218 | ctx = this_ctx; | |
2219 | } else if (ctx != this_ctx) { | |
2220 | mutex_lock(&ctx->uring_lock); | |
2221 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
2222 | mutex_unlock(&ctx->uring_lock); | |
2223 | ctx = this_ctx; | |
2224 | } | |
2225 | } | |
2226 | ||
2227 | if (ctx && ctx->submit_state.comp.nr) { | |
2228 | mutex_lock(&ctx->uring_lock); | |
2229 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
2230 | mutex_unlock(&ctx->uring_lock); | |
7cbf1722 JA |
2231 | } |
2232 | ||
2233 | return list.first != NULL; | |
c2c4c83c JA |
2234 | } |
2235 | ||
7cbf1722 | 2236 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2237 | { |
7cbf1722 | 2238 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 2239 | |
7cbf1722 JA |
2240 | while (__tctx_task_work(tctx)) |
2241 | cond_resched(); | |
2242 | ||
2243 | clear_bit(0, &tctx->task_state); | |
2244 | } | |
2245 | ||
2246 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
2247 | enum task_work_notify_mode notify) | |
2248 | { | |
2249 | struct io_uring_task *tctx = tsk->io_uring; | |
2250 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 2251 | unsigned long flags; |
7cbf1722 JA |
2252 | int ret; |
2253 | ||
2254 | WARN_ON_ONCE(!tctx); | |
2255 | ||
0b81e80c | 2256 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2257 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 2258 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2259 | |
2260 | /* task_work already pending, we're done */ | |
2261 | if (test_bit(0, &tctx->task_state) || | |
2262 | test_and_set_bit(0, &tctx->task_state)) | |
2263 | return 0; | |
2264 | ||
2265 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
2266 | return 0; | |
2267 | ||
2268 | /* | |
2269 | * Slow path - we failed, find and delete work. if the work is not | |
2270 | * in the list, it got run and we're fine. | |
2271 | */ | |
2272 | ret = 0; | |
0b81e80c | 2273 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
2274 | wq_list_for_each(node, prev, &tctx->task_list) { |
2275 | if (&req->io_task_work.node == node) { | |
2276 | wq_list_del(&tctx->task_list, node, prev); | |
2277 | ret = 1; | |
2278 | break; | |
2279 | } | |
2280 | } | |
0b81e80c | 2281 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2282 | clear_bit(0, &tctx->task_state); |
2283 | return ret; | |
2284 | } | |
2285 | ||
355fb9e2 | 2286 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
2287 | { |
2288 | struct task_struct *tsk = req->task; | |
2289 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2290 | enum task_work_notify_mode notify; |
2291 | int ret; | |
c2c4c83c | 2292 | |
6200b0ae JA |
2293 | if (tsk->flags & PF_EXITING) |
2294 | return -ESRCH; | |
2295 | ||
c2c4c83c | 2296 | /* |
0ba9c9ed JA |
2297 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2298 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2299 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2300 | * will do the job. | |
c2c4c83c | 2301 | */ |
91989c70 | 2302 | notify = TWA_NONE; |
355fb9e2 | 2303 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
2304 | notify = TWA_SIGNAL; |
2305 | ||
7cbf1722 | 2306 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
2307 | if (!ret) |
2308 | wake_up_process(tsk); | |
0ba9c9ed | 2309 | |
c2c4c83c JA |
2310 | return ret; |
2311 | } | |
2312 | ||
eab30c4d | 2313 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 2314 | task_work_func_t cb) |
eab30c4d PB |
2315 | { |
2316 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); | |
2317 | ||
2318 | init_task_work(&req->task_work, cb); | |
2319 | task_work_add(tsk, &req->task_work, TWA_NONE); | |
2320 | wake_up_process(tsk); | |
2321 | } | |
2322 | ||
c40f6379 JA |
2323 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2324 | { | |
2325 | struct io_ring_ctx *ctx = req->ctx; | |
2326 | ||
2327 | spin_lock_irq(&ctx->completion_lock); | |
2328 | io_cqring_fill_event(req, error); | |
2329 | io_commit_cqring(ctx); | |
2330 | spin_unlock_irq(&ctx->completion_lock); | |
2331 | ||
2332 | io_cqring_ev_posted(ctx); | |
2333 | req_set_fail_links(req); | |
2334 | io_double_put_req(req); | |
2335 | } | |
2336 | ||
2337 | static void io_req_task_cancel(struct callback_head *cb) | |
2338 | { | |
2339 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2340 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2341 | |
792bb6eb | 2342 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 2343 | __io_req_task_cancel(req, req->result); |
792bb6eb | 2344 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 2345 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2346 | } |
2347 | ||
2348 | static void __io_req_task_submit(struct io_kiocb *req) | |
2349 | { | |
2350 | struct io_ring_ctx *ctx = req->ctx; | |
2351 | ||
04fc6c80 | 2352 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2353 | mutex_lock(&ctx->uring_lock); |
dc0eced5 PB |
2354 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
2355 | !io_sq_thread_acquire_mm_files(ctx, req)) | |
c5eef2b9 | 2356 | __io_queue_sqe(req); |
81b6d05c | 2357 | else |
c40f6379 | 2358 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2359 | mutex_unlock(&ctx->uring_lock); |
aec18a57 PB |
2360 | |
2361 | if (ctx->flags & IORING_SETUP_SQPOLL) | |
2362 | io_sq_thread_drop_mm_files(); | |
c40f6379 JA |
2363 | } |
2364 | ||
2365 | static void io_req_task_submit(struct callback_head *cb) | |
2366 | { | |
2367 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2368 | ||
2369 | __io_req_task_submit(req); | |
2370 | } | |
2371 | ||
2372 | static void io_req_task_queue(struct io_kiocb *req) | |
2373 | { | |
c40f6379 JA |
2374 | int ret; |
2375 | ||
7cbf1722 | 2376 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 2377 | ret = io_req_task_work_add(req); |
c40f6379 | 2378 | if (unlikely(ret)) { |
a3df7698 | 2379 | req->result = -ECANCELED; |
04fc6c80 | 2380 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 2381 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 2382 | } |
c40f6379 JA |
2383 | } |
2384 | ||
a3df7698 PB |
2385 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
2386 | { | |
2387 | percpu_ref_get(&req->ctx->refs); | |
2388 | req->result = ret; | |
2389 | req->task_work.func = io_req_task_cancel; | |
2390 | ||
2391 | if (unlikely(io_req_task_work_add(req))) | |
2392 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2393 | } | |
2394 | ||
f2f87370 | 2395 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2396 | { |
9b5f7bd9 | 2397 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2398 | |
2399 | if (nxt) | |
906a8c3f | 2400 | io_req_task_queue(nxt); |
c69f8dbe JL |
2401 | } |
2402 | ||
c3524383 | 2403 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2404 | { |
c3524383 PB |
2405 | io_queue_next(req); |
2406 | __io_free_req(req); | |
2407 | } | |
8766dd51 | 2408 | |
2d6500d4 | 2409 | struct req_batch { |
5af1d13e PB |
2410 | struct task_struct *task; |
2411 | int task_refs; | |
1b4c351f | 2412 | int ctx_refs; |
2d6500d4 PB |
2413 | }; |
2414 | ||
5af1d13e PB |
2415 | static inline void io_init_req_batch(struct req_batch *rb) |
2416 | { | |
5af1d13e | 2417 | rb->task_refs = 0; |
9ae72463 | 2418 | rb->ctx_refs = 0; |
5af1d13e PB |
2419 | rb->task = NULL; |
2420 | } | |
2421 | ||
2d6500d4 PB |
2422 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2423 | struct req_batch *rb) | |
2424 | { | |
6e833d53 | 2425 | if (rb->task) |
7c660731 | 2426 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2427 | if (rb->ctx_refs) |
2428 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2429 | } |
2430 | ||
6ff119a6 PB |
2431 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2432 | struct io_submit_state *state) | |
2d6500d4 | 2433 | { |
f2f87370 | 2434 | io_queue_next(req); |
2d6500d4 | 2435 | |
e3bc8e9d | 2436 | if (req->task != rb->task) { |
7c660731 PB |
2437 | if (rb->task) |
2438 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2439 | rb->task = req->task; |
2440 | rb->task_refs = 0; | |
5af1d13e | 2441 | } |
e3bc8e9d | 2442 | rb->task_refs++; |
9ae72463 | 2443 | rb->ctx_refs++; |
5af1d13e | 2444 | |
4edf20f9 | 2445 | io_dismantle_req(req); |
bd759045 | 2446 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2447 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2448 | else |
2449 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2450 | } |
2451 | ||
905c172f PB |
2452 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2453 | struct io_ring_ctx *ctx) | |
2454 | { | |
2455 | int i, nr = cs->nr; | |
2456 | struct io_kiocb *req; | |
2457 | struct req_batch rb; | |
2458 | ||
2459 | io_init_req_batch(&rb); | |
2460 | spin_lock_irq(&ctx->completion_lock); | |
2461 | for (i = 0; i < nr; i++) { | |
2462 | req = cs->reqs[i]; | |
2463 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2464 | } | |
2465 | io_commit_cqring(ctx); | |
2466 | spin_unlock_irq(&ctx->completion_lock); | |
2467 | ||
2468 | io_cqring_ev_posted(ctx); | |
2469 | for (i = 0; i < nr; i++) { | |
2470 | req = cs->reqs[i]; | |
2471 | ||
2472 | /* submission and completion refs */ | |
2473 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2474 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2475 | } |
2476 | ||
2477 | io_req_free_batch_finish(ctx, &rb); | |
2478 | cs->nr = 0; | |
7a743e22 PB |
2479 | } |
2480 | ||
ba816ad6 JA |
2481 | /* |
2482 | * Drop reference to request, return next in chain (if there is one) if this | |
2483 | * was the last reference to this request. | |
2484 | */ | |
9b5f7bd9 | 2485 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2486 | { |
9b5f7bd9 PB |
2487 | struct io_kiocb *nxt = NULL; |
2488 | ||
2a44f467 | 2489 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2490 | nxt = io_req_find_next(req); |
4d7dd462 | 2491 | __io_free_req(req); |
2a44f467 | 2492 | } |
9b5f7bd9 | 2493 | return nxt; |
2b188cc1 JA |
2494 | } |
2495 | ||
e65ef56d JA |
2496 | static void io_put_req(struct io_kiocb *req) |
2497 | { | |
2498 | if (refcount_dec_and_test(&req->refs)) | |
2499 | io_free_req(req); | |
2b188cc1 JA |
2500 | } |
2501 | ||
216578e5 PB |
2502 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2503 | { | |
2504 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2505 | ||
2506 | io_free_req(req); | |
2507 | } | |
2508 | ||
2509 | static void io_free_req_deferred(struct io_kiocb *req) | |
2510 | { | |
2511 | int ret; | |
2512 | ||
7cbf1722 | 2513 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2514 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2515 | if (unlikely(ret)) |
2516 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2517 | } |
2518 | ||
2519 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2520 | { | |
2521 | if (refcount_sub_and_test(refs, &req->refs)) | |
2522 | io_free_req_deferred(req); | |
2523 | } | |
2524 | ||
978db57e JA |
2525 | static void io_double_put_req(struct io_kiocb *req) |
2526 | { | |
2527 | /* drop both submit and complete references */ | |
2528 | if (refcount_sub_and_test(2, &req->refs)) | |
2529 | io_free_req(req); | |
2530 | } | |
2531 | ||
6c503150 | 2532 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2533 | { |
2534 | /* See comment at the top of this file */ | |
2535 | smp_rmb(); | |
e23de15f | 2536 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2537 | } |
2538 | ||
fb5ccc98 PB |
2539 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2540 | { | |
2541 | struct io_rings *rings = ctx->rings; | |
2542 | ||
2543 | /* make sure SQ entry isn't read before tail */ | |
2544 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2545 | } | |
2546 | ||
8ff069bf | 2547 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2548 | { |
8ff069bf | 2549 | unsigned int cflags; |
e94f141b | 2550 | |
bcda7baa JA |
2551 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2552 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2553 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2554 | kfree(kbuf); |
2555 | return cflags; | |
e94f141b JA |
2556 | } |
2557 | ||
8ff069bf | 2558 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2559 | { |
4d954c25 | 2560 | struct io_buffer *kbuf; |
bcda7baa | 2561 | |
4d954c25 | 2562 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2563 | return io_put_kbuf(req, kbuf); |
2564 | } | |
2565 | ||
4c6e277c JA |
2566 | static inline bool io_run_task_work(void) |
2567 | { | |
6200b0ae JA |
2568 | /* |
2569 | * Not safe to run on exiting task, and the task_work handling will | |
2570 | * not add work to such a task. | |
2571 | */ | |
2572 | if (unlikely(current->flags & PF_EXITING)) | |
2573 | return false; | |
4c6e277c JA |
2574 | if (current->task_works) { |
2575 | __set_current_state(TASK_RUNNING); | |
2576 | task_work_run(); | |
2577 | return true; | |
2578 | } | |
2579 | ||
2580 | return false; | |
bcda7baa JA |
2581 | } |
2582 | ||
def596e9 JA |
2583 | /* |
2584 | * Find and free completed poll iocbs | |
2585 | */ | |
2586 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2587 | struct list_head *done) | |
2588 | { | |
8237e045 | 2589 | struct req_batch rb; |
def596e9 | 2590 | struct io_kiocb *req; |
bbde017a XW |
2591 | |
2592 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2593 | smp_rmb(); | |
def596e9 | 2594 | |
5af1d13e | 2595 | io_init_req_batch(&rb); |
def596e9 | 2596 | while (!list_empty(done)) { |
bcda7baa JA |
2597 | int cflags = 0; |
2598 | ||
d21ffe7e | 2599 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2600 | list_del(&req->inflight_entry); |
2601 | ||
bbde017a XW |
2602 | if (READ_ONCE(req->result) == -EAGAIN) { |
2603 | req->iopoll_completed = 0; | |
23faba36 | 2604 | if (io_rw_reissue(req)) |
f161340d | 2605 | continue; |
bbde017a | 2606 | } |
def596e9 | 2607 | |
bcda7baa | 2608 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2609 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2610 | |
2611 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2612 | (*nr_events)++; |
2613 | ||
c3524383 | 2614 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2615 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2616 | } |
def596e9 | 2617 | |
09bb8394 | 2618 | io_commit_cqring(ctx); |
80c18e4a | 2619 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2620 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2621 | } |
2622 | ||
def596e9 JA |
2623 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2624 | long min) | |
2625 | { | |
2626 | struct io_kiocb *req, *tmp; | |
2627 | LIST_HEAD(done); | |
2628 | bool spin; | |
2629 | int ret; | |
2630 | ||
2631 | /* | |
2632 | * Only spin for completions if we don't have multiple devices hanging | |
2633 | * off our complete list, and we're under the requested amount. | |
2634 | */ | |
2635 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2636 | ||
2637 | ret = 0; | |
d21ffe7e | 2638 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2639 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2640 | |
2641 | /* | |
581f9810 BM |
2642 | * Move completed and retryable entries to our local lists. |
2643 | * If we find a request that requires polling, break out | |
2644 | * and complete those lists first, if we have entries there. | |
def596e9 | 2645 | */ |
65a6543d | 2646 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2647 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2648 | continue; |
2649 | } | |
2650 | if (!list_empty(&done)) | |
2651 | break; | |
2652 | ||
2653 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2654 | if (ret < 0) | |
2655 | break; | |
2656 | ||
3aadc23e PB |
2657 | /* iopoll may have completed current req */ |
2658 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2659 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2660 | |
def596e9 JA |
2661 | if (ret && spin) |
2662 | spin = false; | |
2663 | ret = 0; | |
2664 | } | |
2665 | ||
2666 | if (!list_empty(&done)) | |
2667 | io_iopoll_complete(ctx, nr_events, &done); | |
2668 | ||
2669 | return ret; | |
2670 | } | |
2671 | ||
2672 | /* | |
d195a66e | 2673 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2674 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2675 | * as a non-spinning completion check. | |
2676 | */ | |
2677 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2678 | long min) | |
2679 | { | |
540e32a0 | 2680 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2681 | int ret; |
2682 | ||
2683 | ret = io_do_iopoll(ctx, nr_events, min); | |
2684 | if (ret < 0) | |
2685 | return ret; | |
eba0a4dd | 2686 | if (*nr_events >= min) |
def596e9 JA |
2687 | return 0; |
2688 | } | |
2689 | ||
2690 | return 1; | |
2691 | } | |
2692 | ||
2693 | /* | |
2694 | * We can't just wait for polled events to come to us, we have to actively | |
2695 | * find and complete them. | |
2696 | */ | |
b2edc0a7 | 2697 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2698 | { |
2699 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2700 | return; | |
2701 | ||
2702 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2703 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2704 | unsigned int nr_events = 0; |
2705 | ||
b2edc0a7 | 2706 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2707 | |
b2edc0a7 PB |
2708 | /* let it sleep and repeat later if can't complete a request */ |
2709 | if (nr_events == 0) | |
2710 | break; | |
08f5439f JA |
2711 | /* |
2712 | * Ensure we allow local-to-the-cpu processing to take place, | |
2713 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2714 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2715 | */ |
3fcee5a6 PB |
2716 | if (need_resched()) { |
2717 | mutex_unlock(&ctx->uring_lock); | |
2718 | cond_resched(); | |
2719 | mutex_lock(&ctx->uring_lock); | |
2720 | } | |
def596e9 JA |
2721 | } |
2722 | mutex_unlock(&ctx->uring_lock); | |
2723 | } | |
2724 | ||
7668b92a | 2725 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2726 | { |
7668b92a | 2727 | unsigned int nr_events = 0; |
2b2ed975 | 2728 | int iters = 0, ret = 0; |
500f9fba | 2729 | |
c7849be9 XW |
2730 | /* |
2731 | * We disallow the app entering submit/complete with polling, but we | |
2732 | * still need to lock the ring to prevent racing with polled issue | |
2733 | * that got punted to a workqueue. | |
2734 | */ | |
2735 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2736 | do { |
a3a0e43f JA |
2737 | /* |
2738 | * Don't enter poll loop if we already have events pending. | |
2739 | * If we do, we can potentially be spinning for commands that | |
2740 | * already triggered a CQE (eg in error). | |
2741 | */ | |
6c503150 PB |
2742 | if (test_bit(0, &ctx->cq_check_overflow)) |
2743 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2744 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2745 | break; |
2746 | ||
500f9fba JA |
2747 | /* |
2748 | * If a submit got punted to a workqueue, we can have the | |
2749 | * application entering polling for a command before it gets | |
2750 | * issued. That app will hold the uring_lock for the duration | |
2751 | * of the poll right here, so we need to take a breather every | |
2752 | * now and then to ensure that the issue has a chance to add | |
2753 | * the poll to the issued list. Otherwise we can spin here | |
2754 | * forever, while the workqueue is stuck trying to acquire the | |
2755 | * very same mutex. | |
2756 | */ | |
2757 | if (!(++iters & 7)) { | |
2758 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2759 | io_run_task_work(); |
500f9fba JA |
2760 | mutex_lock(&ctx->uring_lock); |
2761 | } | |
2762 | ||
7668b92a | 2763 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2764 | if (ret <= 0) |
2765 | break; | |
2766 | ret = 0; | |
7668b92a | 2767 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2768 | |
500f9fba | 2769 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2770 | return ret; |
2771 | } | |
2772 | ||
491381ce | 2773 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2774 | { |
491381ce JA |
2775 | /* |
2776 | * Tell lockdep we inherited freeze protection from submission | |
2777 | * thread. | |
2778 | */ | |
2779 | if (req->flags & REQ_F_ISREG) { | |
2780 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2781 | |
491381ce | 2782 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2783 | } |
491381ce | 2784 | file_end_write(req->file); |
2b188cc1 JA |
2785 | } |
2786 | ||
b63534c4 | 2787 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2788 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2789 | { |
2790 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2791 | int rw, ret; |
b63534c4 | 2792 | struct iov_iter iter; |
b63534c4 | 2793 | |
dc2a6e9a PB |
2794 | /* already prepared */ |
2795 | if (req->async_data) | |
2796 | return true; | |
b63534c4 JA |
2797 | |
2798 | switch (req->opcode) { | |
2799 | case IORING_OP_READV: | |
2800 | case IORING_OP_READ_FIXED: | |
2801 | case IORING_OP_READ: | |
2802 | rw = READ; | |
2803 | break; | |
2804 | case IORING_OP_WRITEV: | |
2805 | case IORING_OP_WRITE_FIXED: | |
2806 | case IORING_OP_WRITE: | |
2807 | rw = WRITE; | |
2808 | break; | |
2809 | default: | |
2810 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2811 | req->opcode); | |
dc2a6e9a | 2812 | return false; |
b63534c4 JA |
2813 | } |
2814 | ||
dc2a6e9a PB |
2815 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2816 | if (ret < 0) | |
2817 | return false; | |
6bf985dc | 2818 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2819 | } |
b63534c4 JA |
2820 | #endif |
2821 | ||
23faba36 | 2822 | static bool io_rw_reissue(struct io_kiocb *req) |
b63534c4 JA |
2823 | { |
2824 | #ifdef CONFIG_BLOCK | |
355afaeb | 2825 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2826 | int ret; |
2827 | ||
355afaeb JA |
2828 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2829 | return false; | |
75c668cd | 2830 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
b63534c4 | 2831 | return false; |
7c977a58 JA |
2832 | /* |
2833 | * If ref is dying, we might be running poll reap from the exit work. | |
2834 | * Don't attempt to reissue from that path, just let it fail with | |
2835 | * -EAGAIN. | |
2836 | */ | |
2837 | if (percpu_ref_is_dying(&req->ctx->refs)) | |
2838 | return false; | |
b63534c4 | 2839 | |
55e6ac1e PB |
2840 | lockdep_assert_held(&req->ctx->uring_lock); |
2841 | ||
28cea78a | 2842 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2843 | |
dc2a6e9a | 2844 | if (!ret && io_resubmit_prep(req)) { |
fdee946d JA |
2845 | refcount_inc(&req->refs); |
2846 | io_queue_async_work(req); | |
b63534c4 | 2847 | return true; |
fdee946d | 2848 | } |
dc2a6e9a | 2849 | req_set_fail_links(req); |
b63534c4 JA |
2850 | #endif |
2851 | return false; | |
2852 | } | |
2853 | ||
a1d7c393 | 2854 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2855 | unsigned int issue_flags) |
a1d7c393 | 2856 | { |
2f8e45f1 PB |
2857 | int cflags = 0; |
2858 | ||
23faba36 PB |
2859 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2860 | return; | |
2f8e45f1 PB |
2861 | if (res != req->result) |
2862 | req_set_fail_links(req); | |
23faba36 | 2863 | |
2f8e45f1 PB |
2864 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2865 | kiocb_end_write(req); | |
2866 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2867 | cflags = io_put_rw_kbuf(req); | |
2868 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2869 | } |
2870 | ||
2871 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2872 | { | |
9adbd45d | 2873 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2874 | |
889fca73 | 2875 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2876 | } |
2877 | ||
def596e9 JA |
2878 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2879 | { | |
9adbd45d | 2880 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2881 | |
491381ce JA |
2882 | if (kiocb->ki_flags & IOCB_WRITE) |
2883 | kiocb_end_write(req); | |
def596e9 | 2884 | |
2d7d6792 | 2885 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2886 | req_set_fail_links(req); |
bbde017a XW |
2887 | |
2888 | WRITE_ONCE(req->result, res); | |
2889 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2890 | smp_wmb(); |
2891 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2892 | } |
2893 | ||
2894 | /* | |
2895 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2896 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2897 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2898 | * accessing the kiocb cookie. | |
2899 | */ | |
2e9dbe90 | 2900 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2901 | { |
2902 | struct io_ring_ctx *ctx = req->ctx; | |
2903 | ||
2904 | /* | |
2905 | * Track whether we have multiple files in our lists. This will impact | |
2906 | * how we do polling eventually, not spinning if we're on potentially | |
2907 | * different devices. | |
2908 | */ | |
540e32a0 | 2909 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2910 | ctx->poll_multi_file = false; |
2911 | } else if (!ctx->poll_multi_file) { | |
2912 | struct io_kiocb *list_req; | |
2913 | ||
540e32a0 | 2914 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2915 | inflight_entry); |
9adbd45d | 2916 | if (list_req->file != req->file) |
def596e9 JA |
2917 | ctx->poll_multi_file = true; |
2918 | } | |
2919 | ||
2920 | /* | |
2921 | * For fast devices, IO may have already completed. If it has, add | |
2922 | * it to the front so we find it first. | |
2923 | */ | |
65a6543d | 2924 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2925 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2926 | else |
d21ffe7e | 2927 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2928 | |
2e9dbe90 XW |
2929 | /* |
2930 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2931 | * task context or in io worker task context. If current task context is | |
2932 | * sq thread, we don't need to check whether should wake up sq thread. | |
2933 | */ | |
2934 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2935 | wq_has_sleeper(&ctx->sq_data->wait)) |
2936 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2937 | } |
2938 | ||
9f13c35b PB |
2939 | static inline void io_state_file_put(struct io_submit_state *state) |
2940 | { | |
02b23a9a PB |
2941 | if (state->file_refs) { |
2942 | fput_many(state->file, state->file_refs); | |
2943 | state->file_refs = 0; | |
2944 | } | |
9a56a232 JA |
2945 | } |
2946 | ||
2947 | /* | |
2948 | * Get as many references to a file as we have IOs left in this submission, | |
2949 | * assuming most submissions are for one file, or at least that each file | |
2950 | * has more than one submission. | |
2951 | */ | |
8da11c19 | 2952 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2953 | { |
2954 | if (!state) | |
2955 | return fget(fd); | |
2956 | ||
6e1271e6 | 2957 | if (state->file_refs) { |
9a56a232 | 2958 | if (state->fd == fd) { |
6e1271e6 | 2959 | state->file_refs--; |
9a56a232 JA |
2960 | return state->file; |
2961 | } | |
02b23a9a | 2962 | io_state_file_put(state); |
9a56a232 JA |
2963 | } |
2964 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2965 | if (unlikely(!state->file)) |
9a56a232 JA |
2966 | return NULL; |
2967 | ||
2968 | state->fd = fd; | |
6e1271e6 | 2969 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2970 | return state->file; |
2971 | } | |
2972 | ||
4503b767 JA |
2973 | static bool io_bdev_nowait(struct block_device *bdev) |
2974 | { | |
9ba0d0c8 | 2975 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2976 | } |
2977 | ||
2b188cc1 JA |
2978 | /* |
2979 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2980 | * any file. For now, just ensure that anything potentially problematic is done | |
2981 | * inline. | |
2982 | */ | |
af197f50 | 2983 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2984 | { |
2985 | umode_t mode = file_inode(file)->i_mode; | |
2986 | ||
4503b767 | 2987 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2988 | if (IS_ENABLED(CONFIG_BLOCK) && |
2989 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2990 | return true; |
2991 | return false; | |
2992 | } | |
2993 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2994 | return true; |
4503b767 | 2995 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2996 | if (IS_ENABLED(CONFIG_BLOCK) && |
2997 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2998 | file->f_op != &io_uring_fops) |
2999 | return true; | |
3000 | return false; | |
3001 | } | |
2b188cc1 | 3002 | |
c5b85625 JA |
3003 | /* any ->read/write should understand O_NONBLOCK */ |
3004 | if (file->f_flags & O_NONBLOCK) | |
3005 | return true; | |
3006 | ||
af197f50 JA |
3007 | if (!(file->f_mode & FMODE_NOWAIT)) |
3008 | return false; | |
3009 | ||
3010 | if (rw == READ) | |
3011 | return file->f_op->read_iter != NULL; | |
3012 | ||
3013 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
3014 | } |
3015 | ||
a88fc400 | 3016 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3017 | { |
def596e9 | 3018 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 3019 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 3020 | struct file *file = req->file; |
09bb8394 JA |
3021 | unsigned ioprio; |
3022 | int ret; | |
2b188cc1 | 3023 | |
75c668cd | 3024 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
3025 | req->flags |= REQ_F_ISREG; |
3026 | ||
2b188cc1 | 3027 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 3028 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 3029 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 3030 | kiocb->ki_pos = file->f_pos; |
ba04291e | 3031 | } |
2b188cc1 | 3032 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
3033 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
3034 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
3035 | if (unlikely(ret)) | |
3036 | return ret; | |
2b188cc1 | 3037 | |
75c668cd PB |
3038 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
3039 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
3040 | req->flags |= REQ_F_NOWAIT; | |
3041 | ||
2b188cc1 JA |
3042 | ioprio = READ_ONCE(sqe->ioprio); |
3043 | if (ioprio) { | |
3044 | ret = ioprio_check_cap(ioprio); | |
3045 | if (ret) | |
09bb8394 | 3046 | return ret; |
2b188cc1 JA |
3047 | |
3048 | kiocb->ki_ioprio = ioprio; | |
3049 | } else | |
3050 | kiocb->ki_ioprio = get_current_ioprio(); | |
3051 | ||
def596e9 | 3052 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
3053 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
3054 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 3055 | return -EOPNOTSUPP; |
2b188cc1 | 3056 | |
def596e9 JA |
3057 | kiocb->ki_flags |= IOCB_HIPRI; |
3058 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 3059 | req->iopoll_completed = 0; |
def596e9 | 3060 | } else { |
09bb8394 JA |
3061 | if (kiocb->ki_flags & IOCB_HIPRI) |
3062 | return -EINVAL; | |
def596e9 JA |
3063 | kiocb->ki_complete = io_complete_rw; |
3064 | } | |
9adbd45d | 3065 | |
3529d8c2 JA |
3066 | req->rw.addr = READ_ONCE(sqe->addr); |
3067 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3068 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3069 | return 0; |
2b188cc1 JA |
3070 | } |
3071 | ||
3072 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3073 | { | |
3074 | switch (ret) { | |
3075 | case -EIOCBQUEUED: | |
3076 | break; | |
3077 | case -ERESTARTSYS: | |
3078 | case -ERESTARTNOINTR: | |
3079 | case -ERESTARTNOHAND: | |
3080 | case -ERESTART_RESTARTBLOCK: | |
3081 | /* | |
3082 | * We can't just restart the syscall, since previously | |
3083 | * submitted sqes may already be in progress. Just fail this | |
3084 | * IO with EINTR. | |
3085 | */ | |
3086 | ret = -EINTR; | |
df561f66 | 3087 | fallthrough; |
2b188cc1 JA |
3088 | default: |
3089 | kiocb->ki_complete(kiocb, ret, 0); | |
3090 | } | |
3091 | } | |
3092 | ||
a1d7c393 | 3093 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 3094 | unsigned int issue_flags) |
ba816ad6 | 3095 | { |
ba04291e | 3096 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 3097 | struct io_async_rw *io = req->async_data; |
ba04291e | 3098 | |
227c0c96 | 3099 | /* add previously done IO, if any */ |
e8c2bc1f | 3100 | if (io && io->bytes_done > 0) { |
227c0c96 | 3101 | if (ret < 0) |
e8c2bc1f | 3102 | ret = io->bytes_done; |
227c0c96 | 3103 | else |
e8c2bc1f | 3104 | ret += io->bytes_done; |
227c0c96 JA |
3105 | } |
3106 | ||
ba04291e JA |
3107 | if (req->flags & REQ_F_CUR_POS) |
3108 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 3109 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 3110 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
3111 | else |
3112 | io_rw_done(kiocb, ret); | |
3113 | } | |
3114 | ||
847595de | 3115 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 3116 | { |
9adbd45d JA |
3117 | struct io_ring_ctx *ctx = req->ctx; |
3118 | size_t len = req->rw.len; | |
edafccee | 3119 | struct io_mapped_ubuf *imu; |
4be1c615 | 3120 | u16 index, buf_index = req->buf_index; |
edafccee JA |
3121 | size_t offset; |
3122 | u64 buf_addr; | |
3123 | ||
edafccee JA |
3124 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3125 | return -EFAULT; | |
edafccee JA |
3126 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3127 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 3128 | buf_addr = req->rw.addr; |
edafccee JA |
3129 | |
3130 | /* overflow */ | |
3131 | if (buf_addr + len < buf_addr) | |
3132 | return -EFAULT; | |
3133 | /* not inside the mapped region */ | |
3134 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
3135 | return -EFAULT; | |
3136 | ||
3137 | /* | |
3138 | * May not be a start of buffer, set size appropriately | |
3139 | * and advance us to the beginning. | |
3140 | */ | |
3141 | offset = buf_addr - imu->ubuf; | |
3142 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3143 | |
3144 | if (offset) { | |
3145 | /* | |
3146 | * Don't use iov_iter_advance() here, as it's really slow for | |
3147 | * using the latter parts of a big fixed buffer - it iterates | |
3148 | * over each segment manually. We can cheat a bit here, because | |
3149 | * we know that: | |
3150 | * | |
3151 | * 1) it's a BVEC iter, we set it up | |
3152 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3153 | * first and last bvec | |
3154 | * | |
3155 | * So just find our index, and adjust the iterator afterwards. | |
3156 | * If the offset is within the first bvec (or the whole first | |
3157 | * bvec, just use iov_iter_advance(). This makes it easier | |
3158 | * since we can just skip the first segment, which may not | |
3159 | * be PAGE_SIZE aligned. | |
3160 | */ | |
3161 | const struct bio_vec *bvec = imu->bvec; | |
3162 | ||
3163 | if (offset <= bvec->bv_len) { | |
3164 | iov_iter_advance(iter, offset); | |
3165 | } else { | |
3166 | unsigned long seg_skip; | |
3167 | ||
3168 | /* skip first vec */ | |
3169 | offset -= bvec->bv_len; | |
3170 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3171 | ||
3172 | iter->bvec = bvec + seg_skip; | |
3173 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3174 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3175 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3176 | } |
3177 | } | |
3178 | ||
847595de | 3179 | return 0; |
edafccee JA |
3180 | } |
3181 | ||
bcda7baa JA |
3182 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3183 | { | |
3184 | if (needs_lock) | |
3185 | mutex_unlock(&ctx->uring_lock); | |
3186 | } | |
3187 | ||
3188 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3189 | { | |
3190 | /* | |
3191 | * "Normal" inline submissions always hold the uring_lock, since we | |
3192 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3193 | * The only exception is when we've detached the request and issue it | |
3194 | * from an async worker thread, grab the lock for that case. | |
3195 | */ | |
3196 | if (needs_lock) | |
3197 | mutex_lock(&ctx->uring_lock); | |
3198 | } | |
3199 | ||
3200 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3201 | int bgid, struct io_buffer *kbuf, | |
3202 | bool needs_lock) | |
3203 | { | |
3204 | struct io_buffer *head; | |
3205 | ||
3206 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3207 | return kbuf; | |
3208 | ||
3209 | io_ring_submit_lock(req->ctx, needs_lock); | |
3210 | ||
3211 | lockdep_assert_held(&req->ctx->uring_lock); | |
3212 | ||
3213 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3214 | if (head) { | |
3215 | if (!list_empty(&head->list)) { | |
3216 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3217 | list); | |
3218 | list_del(&kbuf->list); | |
3219 | } else { | |
3220 | kbuf = head; | |
3221 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3222 | } | |
3223 | if (*len > kbuf->len) | |
3224 | *len = kbuf->len; | |
3225 | } else { | |
3226 | kbuf = ERR_PTR(-ENOBUFS); | |
3227 | } | |
3228 | ||
3229 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3230 | ||
3231 | return kbuf; | |
3232 | } | |
3233 | ||
4d954c25 JA |
3234 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3235 | bool needs_lock) | |
3236 | { | |
3237 | struct io_buffer *kbuf; | |
4f4eeba8 | 3238 | u16 bgid; |
4d954c25 JA |
3239 | |
3240 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3241 | bgid = req->buf_index; |
4d954c25 JA |
3242 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3243 | if (IS_ERR(kbuf)) | |
3244 | return kbuf; | |
3245 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3246 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3247 | return u64_to_user_ptr(kbuf->addr); | |
3248 | } | |
3249 | ||
3250 | #ifdef CONFIG_COMPAT | |
3251 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3252 | bool needs_lock) | |
3253 | { | |
3254 | struct compat_iovec __user *uiov; | |
3255 | compat_ssize_t clen; | |
3256 | void __user *buf; | |
3257 | ssize_t len; | |
3258 | ||
3259 | uiov = u64_to_user_ptr(req->rw.addr); | |
3260 | if (!access_ok(uiov, sizeof(*uiov))) | |
3261 | return -EFAULT; | |
3262 | if (__get_user(clen, &uiov->iov_len)) | |
3263 | return -EFAULT; | |
3264 | if (clen < 0) | |
3265 | return -EINVAL; | |
3266 | ||
3267 | len = clen; | |
3268 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3269 | if (IS_ERR(buf)) | |
3270 | return PTR_ERR(buf); | |
3271 | iov[0].iov_base = buf; | |
3272 | iov[0].iov_len = (compat_size_t) len; | |
3273 | return 0; | |
3274 | } | |
3275 | #endif | |
3276 | ||
3277 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3278 | bool needs_lock) | |
3279 | { | |
3280 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3281 | void __user *buf; | |
3282 | ssize_t len; | |
3283 | ||
3284 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3285 | return -EFAULT; | |
3286 | ||
3287 | len = iov[0].iov_len; | |
3288 | if (len < 0) | |
3289 | return -EINVAL; | |
3290 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3291 | if (IS_ERR(buf)) | |
3292 | return PTR_ERR(buf); | |
3293 | iov[0].iov_base = buf; | |
3294 | iov[0].iov_len = len; | |
3295 | return 0; | |
3296 | } | |
3297 | ||
3298 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3299 | bool needs_lock) | |
3300 | { | |
dddb3e26 JA |
3301 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3302 | struct io_buffer *kbuf; | |
3303 | ||
3304 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3305 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3306 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3307 | return 0; |
dddb3e26 | 3308 | } |
dd201662 | 3309 | if (req->rw.len != 1) |
4d954c25 JA |
3310 | return -EINVAL; |
3311 | ||
3312 | #ifdef CONFIG_COMPAT | |
3313 | if (req->ctx->compat) | |
3314 | return io_compat_import(req, iov, needs_lock); | |
3315 | #endif | |
3316 | ||
3317 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3318 | } | |
3319 | ||
847595de PB |
3320 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3321 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3322 | { |
9adbd45d JA |
3323 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3324 | size_t sqe_len = req->rw.len; | |
847595de | 3325 | u8 opcode = req->opcode; |
4d954c25 | 3326 | ssize_t ret; |
edafccee | 3327 | |
7d009165 | 3328 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3329 | *iovec = NULL; |
9adbd45d | 3330 | return io_import_fixed(req, rw, iter); |
edafccee | 3331 | } |
2b188cc1 | 3332 | |
bcda7baa | 3333 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3334 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3335 | return -EINVAL; |
3336 | ||
3a6820f2 | 3337 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3338 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3339 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3340 | if (IS_ERR(buf)) |
4d954c25 | 3341 | return PTR_ERR(buf); |
3f9d6441 | 3342 | req->rw.len = sqe_len; |
bcda7baa JA |
3343 | } |
3344 | ||
3a6820f2 JA |
3345 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3346 | *iovec = NULL; | |
10fc72e4 | 3347 | return ret; |
3a6820f2 JA |
3348 | } |
3349 | ||
4d954c25 JA |
3350 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3351 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3352 | if (!ret) |
3353 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3354 | *iovec = NULL; |
3355 | return ret; | |
3356 | } | |
3357 | ||
89cd35c5 CH |
3358 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3359 | req->ctx->compat); | |
2b188cc1 JA |
3360 | } |
3361 | ||
0fef9483 JA |
3362 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3363 | { | |
5b09e37e | 3364 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3365 | } |
3366 | ||
31b51510 | 3367 | /* |
32960613 JA |
3368 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3369 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3370 | */ |
4017eb91 | 3371 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3372 | { |
4017eb91 JA |
3373 | struct kiocb *kiocb = &req->rw.kiocb; |
3374 | struct file *file = req->file; | |
32960613 JA |
3375 | ssize_t ret = 0; |
3376 | ||
3377 | /* | |
3378 | * Don't support polled IO through this interface, and we can't | |
3379 | * support non-blocking either. For the latter, this just causes | |
3380 | * the kiocb to be handled from an async context. | |
3381 | */ | |
3382 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3383 | return -EOPNOTSUPP; | |
3384 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3385 | return -EAGAIN; | |
3386 | ||
3387 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3388 | struct iovec iovec; |
32960613 JA |
3389 | ssize_t nr; |
3390 | ||
311ae9e1 PB |
3391 | if (!iov_iter_is_bvec(iter)) { |
3392 | iovec = iov_iter_iovec(iter); | |
3393 | } else { | |
4017eb91 JA |
3394 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3395 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3396 | } |
3397 | ||
32960613 JA |
3398 | if (rw == READ) { |
3399 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3400 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3401 | } else { |
3402 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3403 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3404 | } |
3405 | ||
3406 | if (nr < 0) { | |
3407 | if (!ret) | |
3408 | ret = nr; | |
3409 | break; | |
3410 | } | |
3411 | ret += nr; | |
3412 | if (nr != iovec.iov_len) | |
3413 | break; | |
4017eb91 JA |
3414 | req->rw.len -= nr; |
3415 | req->rw.addr += nr; | |
32960613 JA |
3416 | iov_iter_advance(iter, nr); |
3417 | } | |
3418 | ||
3419 | return ret; | |
3420 | } | |
3421 | ||
ff6165b2 JA |
3422 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3423 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3424 | { |
e8c2bc1f | 3425 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3426 | |
ff6165b2 | 3427 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3428 | rw->free_iovec = iovec; |
227c0c96 | 3429 | rw->bytes_done = 0; |
ff6165b2 | 3430 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3431 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3432 | return; |
b64e3444 | 3433 | if (!iovec) { |
ff6165b2 JA |
3434 | unsigned iov_off = 0; |
3435 | ||
3436 | rw->iter.iov = rw->fast_iov; | |
3437 | if (iter->iov != fast_iov) { | |
3438 | iov_off = iter->iov - fast_iov; | |
3439 | rw->iter.iov += iov_off; | |
3440 | } | |
3441 | if (rw->fast_iov != fast_iov) | |
3442 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3443 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3444 | } else { |
3445 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3446 | } |
3447 | } | |
3448 | ||
e8c2bc1f | 3449 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3450 | { |
e8c2bc1f JA |
3451 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3452 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3453 | return req->async_data == NULL; | |
3d9932a8 XW |
3454 | } |
3455 | ||
e8c2bc1f | 3456 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3457 | { |
e8c2bc1f | 3458 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3459 | return 0; |
3d9932a8 | 3460 | |
e8c2bc1f | 3461 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3462 | } |
3463 | ||
ff6165b2 JA |
3464 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3465 | const struct iovec *fast_iov, | |
227c0c96 | 3466 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3467 | { |
e8c2bc1f | 3468 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3469 | return 0; |
e8c2bc1f | 3470 | if (!req->async_data) { |
6bf985dc PB |
3471 | if (__io_alloc_async_data(req)) { |
3472 | kfree(iovec); | |
5d204bcf | 3473 | return -ENOMEM; |
6bf985dc | 3474 | } |
b7bb4f7d | 3475 | |
ff6165b2 | 3476 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3477 | } |
b7bb4f7d | 3478 | return 0; |
f67676d1 JA |
3479 | } |
3480 | ||
73debe68 | 3481 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3482 | { |
e8c2bc1f | 3483 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3484 | struct iovec *iov = iorw->fast_iov; |
847595de | 3485 | int ret; |
c3e330a4 | 3486 | |
2846c481 | 3487 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3488 | if (unlikely(ret < 0)) |
3489 | return ret; | |
3490 | ||
ab0b196c PB |
3491 | iorw->bytes_done = 0; |
3492 | iorw->free_iovec = iov; | |
3493 | if (iov) | |
3494 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3495 | return 0; |
3496 | } | |
3497 | ||
73debe68 | 3498 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3499 | { |
3529d8c2 JA |
3500 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3501 | return -EBADF; | |
93642ef8 | 3502 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3503 | } |
3504 | ||
c1dd91d1 JA |
3505 | /* |
3506 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3507 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3508 | * This gets called when the page is unlocked, and we generally expect that to | |
3509 | * happen when the page IO is completed and the page is now uptodate. This will | |
3510 | * queue a task_work based retry of the operation, attempting to copy the data | |
3511 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3512 | * do a thread based blocking retry of the operation. That's the unexpected | |
3513 | * slow path. | |
3514 | */ | |
bcf5a063 JA |
3515 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3516 | int sync, void *arg) | |
3517 | { | |
3518 | struct wait_page_queue *wpq; | |
3519 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3520 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3521 | |
3522 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3523 | ||
cdc8fcb4 LT |
3524 | if (!wake_page_match(wpq, key)) |
3525 | return 0; | |
3526 | ||
c8d317aa | 3527 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3528 | list_del_init(&wait->entry); |
3529 | ||
bcf5a063 JA |
3530 | /* submit ref gets dropped, acquire a new one */ |
3531 | refcount_inc(&req->refs); | |
921b9054 | 3532 | io_req_task_queue(req); |
bcf5a063 JA |
3533 | return 1; |
3534 | } | |
3535 | ||
c1dd91d1 JA |
3536 | /* |
3537 | * This controls whether a given IO request should be armed for async page | |
3538 | * based retry. If we return false here, the request is handed to the async | |
3539 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3540 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3541 | * will either succeed because the page is now uptodate and unlocked, or it | |
3542 | * will register a callback when the page is unlocked at IO completion. Through | |
3543 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3544 | * That retry will attempt the buffered read again. The retry will generally | |
3545 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3546 | * async worker threads for a blocking retry. | |
3547 | */ | |
227c0c96 | 3548 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3549 | { |
e8c2bc1f JA |
3550 | struct io_async_rw *rw = req->async_data; |
3551 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3552 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3553 | |
bcf5a063 JA |
3554 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3555 | if (req->flags & REQ_F_NOWAIT) | |
3556 | return false; | |
f67676d1 | 3557 | |
227c0c96 | 3558 | /* Only for buffered IO */ |
3b2a4439 | 3559 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3560 | return false; |
3b2a4439 | 3561 | |
bcf5a063 JA |
3562 | /* |
3563 | * just use poll if we can, and don't attempt if the fs doesn't | |
3564 | * support callback based unlocks | |
3565 | */ | |
3566 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3567 | return false; | |
f67676d1 | 3568 | |
3b2a4439 JA |
3569 | wait->wait.func = io_async_buf_func; |
3570 | wait->wait.private = req; | |
3571 | wait->wait.flags = 0; | |
3572 | INIT_LIST_HEAD(&wait->wait.entry); | |
3573 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3574 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3575 | kiocb->ki_waitq = wait; |
3b2a4439 | 3576 | return true; |
bcf5a063 JA |
3577 | } |
3578 | ||
3579 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3580 | { | |
3581 | if (req->file->f_op->read_iter) | |
3582 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3583 | else if (req->file->f_op->read) |
4017eb91 | 3584 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3585 | else |
3586 | return -EINVAL; | |
f67676d1 JA |
3587 | } |
3588 | ||
889fca73 | 3589 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3590 | { |
3591 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3592 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3593 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3594 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3595 | ssize_t io_size, ret, ret2; |
45d189c6 | 3596 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3597 | |
2846c481 | 3598 | if (rw) { |
e8c2bc1f | 3599 | iter = &rw->iter; |
2846c481 PB |
3600 | iovec = NULL; |
3601 | } else { | |
3602 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3603 | if (ret < 0) | |
3604 | return ret; | |
3605 | } | |
632546c4 | 3606 | io_size = iov_iter_count(iter); |
fa15bafb | 3607 | req->result = io_size; |
2b188cc1 | 3608 | |
fd6c2e4c JA |
3609 | /* Ensure we clear previously set non-block flag */ |
3610 | if (!force_nonblock) | |
29de5f6a | 3611 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3612 | else |
3613 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3614 | ||
24c74678 | 3615 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3616 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3617 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3618 | return ret ?: -EAGAIN; |
6713e7a6 | 3619 | } |
9e645e11 | 3620 | |
632546c4 | 3621 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3622 | if (unlikely(ret)) { |
3623 | kfree(iovec); | |
3624 | return ret; | |
3625 | } | |
2b188cc1 | 3626 | |
227c0c96 | 3627 | ret = io_iter_do_read(req, iter); |
32960613 | 3628 | |
57cd657b | 3629 | if (ret == -EIOCBQUEUED) { |
fe1cdd55 | 3630 | goto out_free; |
227c0c96 | 3631 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3632 | /* IOPOLL retry should happen for io-wq threads */ |
3633 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3634 | goto done; |
75c668cd PB |
3635 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3636 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3637 | goto done; |
84216315 | 3638 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3639 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3640 | ret = 0; |
7335e3bf | 3641 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3642 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3643 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3644 | goto done; |
227c0c96 JA |
3645 | } |
3646 | ||
227c0c96 | 3647 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3648 | if (ret2) |
3649 | return ret2; | |
3650 | ||
fe1cdd55 | 3651 | iovec = NULL; |
e8c2bc1f | 3652 | rw = req->async_data; |
227c0c96 | 3653 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3654 | iter = &rw->iter; |
227c0c96 | 3655 | |
b23df91b PB |
3656 | do { |
3657 | io_size -= ret; | |
3658 | rw->bytes_done += ret; | |
3659 | /* if we can retry, do so with the callbacks armed */ | |
3660 | if (!io_rw_should_retry(req)) { | |
3661 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3662 | return -EAGAIN; | |
3663 | } | |
3664 | ||
3665 | /* | |
3666 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3667 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3668 | * desired page gets unlocked. We can also get a partial read | |
3669 | * here, and if we do, then just retry at the new offset. | |
3670 | */ | |
3671 | ret = io_iter_do_read(req, iter); | |
3672 | if (ret == -EIOCBQUEUED) | |
3673 | return 0; | |
227c0c96 | 3674 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3675 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3676 | done: |
889fca73 | 3677 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3678 | out_free: |
3679 | /* it's faster to check here then delegate to kfree */ | |
3680 | if (iovec) | |
3681 | kfree(iovec); | |
5ea5dd45 | 3682 | return 0; |
2b188cc1 JA |
3683 | } |
3684 | ||
73debe68 | 3685 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3686 | { |
3529d8c2 JA |
3687 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3688 | return -EBADF; | |
93642ef8 | 3689 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3690 | } |
3691 | ||
889fca73 | 3692 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3693 | { |
3694 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3695 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3696 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3697 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3698 | ssize_t ret, ret2, io_size; |
45d189c6 | 3699 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3700 | |
2846c481 | 3701 | if (rw) { |
e8c2bc1f | 3702 | iter = &rw->iter; |
2846c481 PB |
3703 | iovec = NULL; |
3704 | } else { | |
3705 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3706 | if (ret < 0) | |
3707 | return ret; | |
3708 | } | |
632546c4 | 3709 | io_size = iov_iter_count(iter); |
fa15bafb | 3710 | req->result = io_size; |
2b188cc1 | 3711 | |
fd6c2e4c JA |
3712 | /* Ensure we clear previously set non-block flag */ |
3713 | if (!force_nonblock) | |
a88fc400 PB |
3714 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3715 | else | |
3716 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3717 | |
24c74678 | 3718 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3719 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3720 | goto copy_iov; |
31b51510 | 3721 | |
10d59345 JA |
3722 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3723 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3724 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3725 | goto copy_iov; |
31b51510 | 3726 | |
632546c4 | 3727 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3728 | if (unlikely(ret)) |
3729 | goto out_free; | |
4ed734b0 | 3730 | |
fa15bafb PB |
3731 | /* |
3732 | * Open-code file_start_write here to grab freeze protection, | |
3733 | * which will be released by another thread in | |
3734 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3735 | * released so that it doesn't complain about the held lock when | |
3736 | * we return to userspace. | |
3737 | */ | |
3738 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3739 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3740 | __sb_writers_release(file_inode(req->file)->i_sb, |
3741 | SB_FREEZE_WRITE); | |
3742 | } | |
3743 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3744 | |
fa15bafb | 3745 | if (req->file->f_op->write_iter) |
ff6165b2 | 3746 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3747 | else if (req->file->f_op->write) |
4017eb91 | 3748 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3749 | else |
3750 | ret2 = -EINVAL; | |
4ed734b0 | 3751 | |
fa15bafb PB |
3752 | /* |
3753 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3754 | * retry them without IOCB_NOWAIT. | |
3755 | */ | |
3756 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3757 | ret2 = -EAGAIN; | |
75c668cd PB |
3758 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3759 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3760 | goto done; |
fa15bafb | 3761 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3762 | /* IOPOLL retry should happen for io-wq threads */ |
3763 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3764 | goto copy_iov; | |
355afaeb | 3765 | done: |
889fca73 | 3766 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3767 | } else { |
f67676d1 | 3768 | copy_iov: |
84216315 | 3769 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3770 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3771 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3772 | return ret ?: -EAGAIN; |
2b188cc1 | 3773 | } |
31b51510 | 3774 | out_free: |
f261c168 | 3775 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3776 | if (iovec) |
6f2cc166 | 3777 | kfree(iovec); |
2b188cc1 JA |
3778 | return ret; |
3779 | } | |
3780 | ||
80a261fd JA |
3781 | static int io_renameat_prep(struct io_kiocb *req, |
3782 | const struct io_uring_sqe *sqe) | |
3783 | { | |
3784 | struct io_rename *ren = &req->rename; | |
3785 | const char __user *oldf, *newf; | |
3786 | ||
3787 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3788 | return -EBADF; | |
3789 | ||
3790 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3791 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3792 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3793 | ren->new_dfd = READ_ONCE(sqe->len); | |
3794 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3795 | ||
3796 | ren->oldpath = getname(oldf); | |
3797 | if (IS_ERR(ren->oldpath)) | |
3798 | return PTR_ERR(ren->oldpath); | |
3799 | ||
3800 | ren->newpath = getname(newf); | |
3801 | if (IS_ERR(ren->newpath)) { | |
3802 | putname(ren->oldpath); | |
3803 | return PTR_ERR(ren->newpath); | |
3804 | } | |
3805 | ||
3806 | req->flags |= REQ_F_NEED_CLEANUP; | |
3807 | return 0; | |
3808 | } | |
3809 | ||
45d189c6 | 3810 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3811 | { |
3812 | struct io_rename *ren = &req->rename; | |
3813 | int ret; | |
3814 | ||
45d189c6 | 3815 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3816 | return -EAGAIN; |
3817 | ||
3818 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3819 | ren->newpath, ren->flags); | |
3820 | ||
3821 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3822 | if (ret < 0) | |
3823 | req_set_fail_links(req); | |
3824 | io_req_complete(req, ret); | |
3825 | return 0; | |
3826 | } | |
3827 | ||
14a1143b JA |
3828 | static int io_unlinkat_prep(struct io_kiocb *req, |
3829 | const struct io_uring_sqe *sqe) | |
3830 | { | |
3831 | struct io_unlink *un = &req->unlink; | |
3832 | const char __user *fname; | |
3833 | ||
3834 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3835 | return -EBADF; | |
3836 | ||
3837 | un->dfd = READ_ONCE(sqe->fd); | |
3838 | ||
3839 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3840 | if (un->flags & ~AT_REMOVEDIR) | |
3841 | return -EINVAL; | |
3842 | ||
3843 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3844 | un->filename = getname(fname); | |
3845 | if (IS_ERR(un->filename)) | |
3846 | return PTR_ERR(un->filename); | |
3847 | ||
3848 | req->flags |= REQ_F_NEED_CLEANUP; | |
3849 | return 0; | |
3850 | } | |
3851 | ||
45d189c6 | 3852 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3853 | { |
3854 | struct io_unlink *un = &req->unlink; | |
3855 | int ret; | |
3856 | ||
45d189c6 | 3857 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3858 | return -EAGAIN; |
3859 | ||
3860 | if (un->flags & AT_REMOVEDIR) | |
3861 | ret = do_rmdir(un->dfd, un->filename); | |
3862 | else | |
3863 | ret = do_unlinkat(un->dfd, un->filename); | |
3864 | ||
3865 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3866 | if (ret < 0) | |
3867 | req_set_fail_links(req); | |
3868 | io_req_complete(req, ret); | |
3869 | return 0; | |
3870 | } | |
3871 | ||
36f4fa68 JA |
3872 | static int io_shutdown_prep(struct io_kiocb *req, |
3873 | const struct io_uring_sqe *sqe) | |
3874 | { | |
3875 | #if defined(CONFIG_NET) | |
3876 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3877 | return -EINVAL; | |
3878 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3879 | sqe->buf_index) | |
3880 | return -EINVAL; | |
3881 | ||
3882 | req->shutdown.how = READ_ONCE(sqe->len); | |
3883 | return 0; | |
3884 | #else | |
3885 | return -EOPNOTSUPP; | |
3886 | #endif | |
3887 | } | |
3888 | ||
45d189c6 | 3889 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3890 | { |
3891 | #if defined(CONFIG_NET) | |
3892 | struct socket *sock; | |
3893 | int ret; | |
3894 | ||
45d189c6 | 3895 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3896 | return -EAGAIN; |
3897 | ||
48aba79b | 3898 | sock = sock_from_file(req->file); |
36f4fa68 | 3899 | if (unlikely(!sock)) |
48aba79b | 3900 | return -ENOTSOCK; |
36f4fa68 JA |
3901 | |
3902 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3903 | if (ret < 0) |
3904 | req_set_fail_links(req); | |
36f4fa68 JA |
3905 | io_req_complete(req, ret); |
3906 | return 0; | |
3907 | #else | |
3908 | return -EOPNOTSUPP; | |
3909 | #endif | |
3910 | } | |
3911 | ||
f2a8d5c7 PB |
3912 | static int __io_splice_prep(struct io_kiocb *req, |
3913 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3914 | { |
3915 | struct io_splice* sp = &req->splice; | |
3916 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3917 | |
3232dd02 PB |
3918 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3919 | return -EINVAL; | |
7d67af2c PB |
3920 | |
3921 | sp->file_in = NULL; | |
7d67af2c PB |
3922 | sp->len = READ_ONCE(sqe->len); |
3923 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3924 | ||
3925 | if (unlikely(sp->flags & ~valid_flags)) | |
3926 | return -EINVAL; | |
3927 | ||
8371adf5 PB |
3928 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3929 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3930 | if (!sp->file_in) | |
3931 | return -EBADF; | |
7d67af2c PB |
3932 | req->flags |= REQ_F_NEED_CLEANUP; |
3933 | ||
7cdaf587 XW |
3934 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3935 | /* | |
3936 | * Splice operation will be punted aync, and here need to | |
3937 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3938 | */ | |
3939 | io_req_init_async(req); | |
7d67af2c | 3940 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3941 | } |
7d67af2c PB |
3942 | |
3943 | return 0; | |
3944 | } | |
3945 | ||
f2a8d5c7 PB |
3946 | static int io_tee_prep(struct io_kiocb *req, |
3947 | const struct io_uring_sqe *sqe) | |
3948 | { | |
3949 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3950 | return -EINVAL; | |
3951 | return __io_splice_prep(req, sqe); | |
3952 | } | |
3953 | ||
45d189c6 | 3954 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3955 | { |
3956 | struct io_splice *sp = &req->splice; | |
3957 | struct file *in = sp->file_in; | |
3958 | struct file *out = sp->file_out; | |
3959 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3960 | long ret = 0; | |
3961 | ||
45d189c6 | 3962 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3963 | return -EAGAIN; |
3964 | if (sp->len) | |
3965 | ret = do_tee(in, out, sp->len, flags); | |
3966 | ||
3967 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3968 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3969 | ||
f2a8d5c7 PB |
3970 | if (ret != sp->len) |
3971 | req_set_fail_links(req); | |
e1e16097 | 3972 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3973 | return 0; |
3974 | } | |
3975 | ||
3976 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3977 | { | |
3978 | struct io_splice* sp = &req->splice; | |
3979 | ||
3980 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3981 | sp->off_out = READ_ONCE(sqe->off); | |
3982 | return __io_splice_prep(req, sqe); | |
3983 | } | |
3984 | ||
45d189c6 | 3985 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3986 | { |
3987 | struct io_splice *sp = &req->splice; | |
3988 | struct file *in = sp->file_in; | |
3989 | struct file *out = sp->file_out; | |
3990 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3991 | loff_t *poff_in, *poff_out; | |
c9687426 | 3992 | long ret = 0; |
7d67af2c | 3993 | |
45d189c6 | 3994 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3995 | return -EAGAIN; |
7d67af2c PB |
3996 | |
3997 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3998 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3999 | |
948a7749 | 4000 | if (sp->len) |
c9687426 | 4001 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
4002 | |
4003 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
4004 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4005 | ||
7d67af2c PB |
4006 | if (ret != sp->len) |
4007 | req_set_fail_links(req); | |
e1e16097 | 4008 | io_req_complete(req, ret); |
7d67af2c PB |
4009 | return 0; |
4010 | } | |
4011 | ||
2b188cc1 JA |
4012 | /* |
4013 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4014 | */ | |
889fca73 | 4015 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4016 | { |
4017 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4018 | |
def596e9 JA |
4019 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4020 | return -EINVAL; | |
4021 | ||
889fca73 | 4022 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4023 | return 0; |
4024 | } | |
4025 | ||
1155c76a | 4026 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4027 | { |
6b06314c | 4028 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4029 | |
09bb8394 JA |
4030 | if (!req->file) |
4031 | return -EBADF; | |
c992fe29 | 4032 | |
6b06314c | 4033 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4034 | return -EINVAL; |
edafccee | 4035 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
4036 | return -EINVAL; |
4037 | ||
8ed8d3c3 JA |
4038 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4039 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4040 | return -EINVAL; | |
4041 | ||
4042 | req->sync.off = READ_ONCE(sqe->off); | |
4043 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4044 | return 0; |
4045 | } | |
4046 | ||
45d189c6 | 4047 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4048 | { |
8ed8d3c3 | 4049 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4050 | int ret; |
4051 | ||
ac45abc0 | 4052 | /* fsync always requires a blocking context */ |
45d189c6 | 4053 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4054 | return -EAGAIN; |
4055 | ||
9adbd45d | 4056 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4057 | end > 0 ? end : LLONG_MAX, |
4058 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4059 | if (ret < 0) | |
4060 | req_set_fail_links(req); | |
e1e16097 | 4061 | io_req_complete(req, ret); |
c992fe29 CH |
4062 | return 0; |
4063 | } | |
4064 | ||
d63d1b5e JA |
4065 | static int io_fallocate_prep(struct io_kiocb *req, |
4066 | const struct io_uring_sqe *sqe) | |
4067 | { | |
4068 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
4069 | return -EINVAL; | |
3232dd02 PB |
4070 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4071 | return -EINVAL; | |
d63d1b5e JA |
4072 | |
4073 | req->sync.off = READ_ONCE(sqe->off); | |
4074 | req->sync.len = READ_ONCE(sqe->addr); | |
4075 | req->sync.mode = READ_ONCE(sqe->len); | |
4076 | return 0; | |
4077 | } | |
4078 | ||
45d189c6 | 4079 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4080 | { |
ac45abc0 PB |
4081 | int ret; |
4082 | ||
d63d1b5e | 4083 | /* fallocate always requiring blocking context */ |
45d189c6 | 4084 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4085 | return -EAGAIN; |
ac45abc0 PB |
4086 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4087 | req->sync.len); | |
ac45abc0 PB |
4088 | if (ret < 0) |
4089 | req_set_fail_links(req); | |
e1e16097 | 4090 | io_req_complete(req, ret); |
5d17b4a4 JA |
4091 | return 0; |
4092 | } | |
4093 | ||
ec65fea5 | 4094 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4095 | { |
f8748881 | 4096 | const char __user *fname; |
15b71abe | 4097 | int ret; |
b7bb4f7d | 4098 | |
ec65fea5 | 4099 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4100 | return -EINVAL; |
ec65fea5 | 4101 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4102 | return -EBADF; |
03b1230c | 4103 | |
ec65fea5 PB |
4104 | /* open.how should be already initialised */ |
4105 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4106 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4107 | |
25e72d10 PB |
4108 | req->open.dfd = READ_ONCE(sqe->fd); |
4109 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4110 | req->open.filename = getname(fname); |
15b71abe JA |
4111 | if (IS_ERR(req->open.filename)) { |
4112 | ret = PTR_ERR(req->open.filename); | |
4113 | req->open.filename = NULL; | |
4114 | return ret; | |
4115 | } | |
4022e7af | 4116 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4117 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4118 | return 0; |
03b1230c JA |
4119 | } |
4120 | ||
ec65fea5 PB |
4121 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4122 | { | |
4123 | u64 flags, mode; | |
4124 | ||
14587a46 | 4125 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4126 | return -EINVAL; |
ec65fea5 PB |
4127 | mode = READ_ONCE(sqe->len); |
4128 | flags = READ_ONCE(sqe->open_flags); | |
4129 | req->open.how = build_open_how(flags, mode); | |
4130 | return __io_openat_prep(req, sqe); | |
4131 | } | |
4132 | ||
cebdb986 | 4133 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4134 | { |
cebdb986 | 4135 | struct open_how __user *how; |
cebdb986 | 4136 | size_t len; |
0fa03c62 JA |
4137 | int ret; |
4138 | ||
14587a46 | 4139 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4140 | return -EINVAL; |
cebdb986 JA |
4141 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4142 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4143 | if (len < OPEN_HOW_SIZE_VER0) |
4144 | return -EINVAL; | |
3529d8c2 | 4145 | |
cebdb986 JA |
4146 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4147 | len); | |
4148 | if (ret) | |
4149 | return ret; | |
3529d8c2 | 4150 | |
ec65fea5 | 4151 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4152 | } |
4153 | ||
45d189c6 | 4154 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4155 | { |
4156 | struct open_flags op; | |
15b71abe | 4157 | struct file *file; |
3a81fd02 JA |
4158 | bool nonblock_set; |
4159 | bool resolve_nonblock; | |
15b71abe JA |
4160 | int ret; |
4161 | ||
cebdb986 | 4162 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4163 | if (ret) |
4164 | goto err; | |
3a81fd02 JA |
4165 | nonblock_set = op.open_flag & O_NONBLOCK; |
4166 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4167 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4168 | /* |
4169 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4170 | * it'll always -EAGAIN | |
4171 | */ | |
4172 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4173 | return -EAGAIN; | |
4174 | op.lookup_flags |= LOOKUP_CACHED; | |
4175 | op.open_flag |= O_NONBLOCK; | |
4176 | } | |
15b71abe | 4177 | |
4022e7af | 4178 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4179 | if (ret < 0) |
4180 | goto err; | |
4181 | ||
4182 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 4183 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
4184 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
4185 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 4186 | /* |
3a81fd02 JA |
4187 | * We could hang on to this 'fd', but seems like marginal |
4188 | * gain for something that is now known to be a slower path. | |
4189 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 4190 | */ |
3a81fd02 JA |
4191 | put_unused_fd(ret); |
4192 | return -EAGAIN; | |
4193 | } | |
4194 | ||
15b71abe JA |
4195 | if (IS_ERR(file)) { |
4196 | put_unused_fd(ret); | |
4197 | ret = PTR_ERR(file); | |
4198 | } else { | |
45d189c6 | 4199 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 4200 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
4201 | fsnotify_open(file); |
4202 | fd_install(ret, file); | |
4203 | } | |
4204 | err: | |
4205 | putname(req->open.filename); | |
8fef80bf | 4206 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4207 | if (ret < 0) |
4208 | req_set_fail_links(req); | |
e1e16097 | 4209 | io_req_complete(req, ret); |
15b71abe JA |
4210 | return 0; |
4211 | } | |
4212 | ||
45d189c6 | 4213 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4214 | { |
45d189c6 | 4215 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
4216 | } |
4217 | ||
067524e9 JA |
4218 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4219 | const struct io_uring_sqe *sqe) | |
4220 | { | |
4221 | struct io_provide_buf *p = &req->pbuf; | |
4222 | u64 tmp; | |
4223 | ||
4224 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4225 | return -EINVAL; | |
4226 | ||
4227 | tmp = READ_ONCE(sqe->fd); | |
4228 | if (!tmp || tmp > USHRT_MAX) | |
4229 | return -EINVAL; | |
4230 | ||
4231 | memset(p, 0, sizeof(*p)); | |
4232 | p->nbufs = tmp; | |
4233 | p->bgid = READ_ONCE(sqe->buf_group); | |
4234 | return 0; | |
4235 | } | |
4236 | ||
4237 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4238 | int bgid, unsigned nbufs) | |
4239 | { | |
4240 | unsigned i = 0; | |
4241 | ||
4242 | /* shouldn't happen */ | |
4243 | if (!nbufs) | |
4244 | return 0; | |
4245 | ||
4246 | /* the head kbuf is the list itself */ | |
4247 | while (!list_empty(&buf->list)) { | |
4248 | struct io_buffer *nxt; | |
4249 | ||
4250 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4251 | list_del(&nxt->list); | |
4252 | kfree(nxt); | |
4253 | if (++i == nbufs) | |
4254 | return i; | |
4255 | } | |
4256 | i++; | |
4257 | kfree(buf); | |
4258 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4259 | ||
4260 | return i; | |
4261 | } | |
4262 | ||
889fca73 | 4263 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4264 | { |
4265 | struct io_provide_buf *p = &req->pbuf; | |
4266 | struct io_ring_ctx *ctx = req->ctx; | |
4267 | struct io_buffer *head; | |
4268 | int ret = 0; | |
45d189c6 | 4269 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4270 | |
4271 | io_ring_submit_lock(ctx, !force_nonblock); | |
4272 | ||
4273 | lockdep_assert_held(&ctx->uring_lock); | |
4274 | ||
4275 | ret = -ENOENT; | |
4276 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4277 | if (head) | |
4278 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4279 | if (ret < 0) |
4280 | req_set_fail_links(req); | |
067524e9 | 4281 | |
31bff9a5 PB |
4282 | /* need to hold the lock to complete IOPOLL requests */ |
4283 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4284 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4285 | io_ring_submit_unlock(ctx, !force_nonblock); |
4286 | } else { | |
4287 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4288 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4289 | } |
067524e9 JA |
4290 | return 0; |
4291 | } | |
4292 | ||
ddf0322d JA |
4293 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4294 | const struct io_uring_sqe *sqe) | |
4295 | { | |
4296 | struct io_provide_buf *p = &req->pbuf; | |
4297 | u64 tmp; | |
4298 | ||
4299 | if (sqe->ioprio || sqe->rw_flags) | |
4300 | return -EINVAL; | |
4301 | ||
4302 | tmp = READ_ONCE(sqe->fd); | |
4303 | if (!tmp || tmp > USHRT_MAX) | |
4304 | return -E2BIG; | |
4305 | p->nbufs = tmp; | |
4306 | p->addr = READ_ONCE(sqe->addr); | |
4307 | p->len = READ_ONCE(sqe->len); | |
4308 | ||
efe68c1c | 4309 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4310 | return -EFAULT; |
4311 | ||
4312 | p->bgid = READ_ONCE(sqe->buf_group); | |
4313 | tmp = READ_ONCE(sqe->off); | |
4314 | if (tmp > USHRT_MAX) | |
4315 | return -E2BIG; | |
4316 | p->bid = tmp; | |
4317 | return 0; | |
4318 | } | |
4319 | ||
4320 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4321 | { | |
4322 | struct io_buffer *buf; | |
4323 | u64 addr = pbuf->addr; | |
4324 | int i, bid = pbuf->bid; | |
4325 | ||
4326 | for (i = 0; i < pbuf->nbufs; i++) { | |
4327 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4328 | if (!buf) | |
4329 | break; | |
4330 | ||
4331 | buf->addr = addr; | |
4332 | buf->len = pbuf->len; | |
4333 | buf->bid = bid; | |
4334 | addr += pbuf->len; | |
4335 | bid++; | |
4336 | if (!*head) { | |
4337 | INIT_LIST_HEAD(&buf->list); | |
4338 | *head = buf; | |
4339 | } else { | |
4340 | list_add_tail(&buf->list, &(*head)->list); | |
4341 | } | |
4342 | } | |
4343 | ||
4344 | return i ? i : -ENOMEM; | |
4345 | } | |
4346 | ||
889fca73 | 4347 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4348 | { |
4349 | struct io_provide_buf *p = &req->pbuf; | |
4350 | struct io_ring_ctx *ctx = req->ctx; | |
4351 | struct io_buffer *head, *list; | |
4352 | int ret = 0; | |
45d189c6 | 4353 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4354 | |
4355 | io_ring_submit_lock(ctx, !force_nonblock); | |
4356 | ||
4357 | lockdep_assert_held(&ctx->uring_lock); | |
4358 | ||
4359 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4360 | ||
4361 | ret = io_add_buffers(p, &head); | |
4362 | if (ret < 0) | |
4363 | goto out; | |
4364 | ||
4365 | if (!list) { | |
4366 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4367 | GFP_KERNEL); | |
4368 | if (ret < 0) { | |
067524e9 | 4369 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4370 | goto out; |
4371 | } | |
4372 | } | |
4373 | out: | |
ddf0322d JA |
4374 | if (ret < 0) |
4375 | req_set_fail_links(req); | |
31bff9a5 PB |
4376 | |
4377 | /* need to hold the lock to complete IOPOLL requests */ | |
4378 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4379 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4380 | io_ring_submit_unlock(ctx, !force_nonblock); |
4381 | } else { | |
4382 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4383 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4384 | } |
ddf0322d | 4385 | return 0; |
cebdb986 JA |
4386 | } |
4387 | ||
3e4827b0 JA |
4388 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4389 | const struct io_uring_sqe *sqe) | |
4390 | { | |
4391 | #if defined(CONFIG_EPOLL) | |
4392 | if (sqe->ioprio || sqe->buf_index) | |
4393 | return -EINVAL; | |
6ca56f84 | 4394 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4395 | return -EINVAL; |
3e4827b0 JA |
4396 | |
4397 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4398 | req->epoll.op = READ_ONCE(sqe->len); | |
4399 | req->epoll.fd = READ_ONCE(sqe->off); | |
4400 | ||
4401 | if (ep_op_has_event(req->epoll.op)) { | |
4402 | struct epoll_event __user *ev; | |
4403 | ||
4404 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4405 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4406 | return -EFAULT; | |
4407 | } | |
4408 | ||
4409 | return 0; | |
4410 | #else | |
4411 | return -EOPNOTSUPP; | |
4412 | #endif | |
4413 | } | |
4414 | ||
889fca73 | 4415 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4416 | { |
4417 | #if defined(CONFIG_EPOLL) | |
4418 | struct io_epoll *ie = &req->epoll; | |
4419 | int ret; | |
45d189c6 | 4420 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4421 | |
4422 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4423 | if (force_nonblock && ret == -EAGAIN) | |
4424 | return -EAGAIN; | |
4425 | ||
4426 | if (ret < 0) | |
4427 | req_set_fail_links(req); | |
889fca73 | 4428 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4429 | return 0; |
4430 | #else | |
4431 | return -EOPNOTSUPP; | |
4432 | #endif | |
4433 | } | |
4434 | ||
c1ca757b JA |
4435 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4436 | { | |
4437 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4438 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4439 | return -EINVAL; | |
3232dd02 PB |
4440 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4441 | return -EINVAL; | |
c1ca757b JA |
4442 | |
4443 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4444 | req->madvise.len = READ_ONCE(sqe->len); | |
4445 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4446 | return 0; | |
4447 | #else | |
4448 | return -EOPNOTSUPP; | |
4449 | #endif | |
4450 | } | |
4451 | ||
45d189c6 | 4452 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4453 | { |
4454 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4455 | struct io_madvise *ma = &req->madvise; | |
4456 | int ret; | |
4457 | ||
45d189c6 | 4458 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4459 | return -EAGAIN; |
4460 | ||
0726b01e | 4461 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4462 | if (ret < 0) |
4463 | req_set_fail_links(req); | |
e1e16097 | 4464 | io_req_complete(req, ret); |
c1ca757b JA |
4465 | return 0; |
4466 | #else | |
4467 | return -EOPNOTSUPP; | |
4468 | #endif | |
4469 | } | |
4470 | ||
4840e418 JA |
4471 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4472 | { | |
4473 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4474 | return -EINVAL; | |
3232dd02 PB |
4475 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4476 | return -EINVAL; | |
4840e418 JA |
4477 | |
4478 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4479 | req->fadvise.len = READ_ONCE(sqe->len); | |
4480 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4481 | return 0; | |
4482 | } | |
4483 | ||
45d189c6 | 4484 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4485 | { |
4486 | struct io_fadvise *fa = &req->fadvise; | |
4487 | int ret; | |
4488 | ||
45d189c6 | 4489 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4490 | switch (fa->advice) { |
4491 | case POSIX_FADV_NORMAL: | |
4492 | case POSIX_FADV_RANDOM: | |
4493 | case POSIX_FADV_SEQUENTIAL: | |
4494 | break; | |
4495 | default: | |
4496 | return -EAGAIN; | |
4497 | } | |
4498 | } | |
4840e418 JA |
4499 | |
4500 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4501 | if (ret < 0) | |
4502 | req_set_fail_links(req); | |
e1e16097 | 4503 | io_req_complete(req, ret); |
4840e418 JA |
4504 | return 0; |
4505 | } | |
4506 | ||
eddc7ef5 JA |
4507 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4508 | { | |
6ca56f84 | 4509 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4510 | return -EINVAL; |
eddc7ef5 JA |
4511 | if (sqe->ioprio || sqe->buf_index) |
4512 | return -EINVAL; | |
9c280f90 | 4513 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4514 | return -EBADF; |
eddc7ef5 | 4515 | |
1d9e1288 BM |
4516 | req->statx.dfd = READ_ONCE(sqe->fd); |
4517 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4518 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4519 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4520 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4521 | |
4522 | return 0; | |
4523 | } | |
4524 | ||
45d189c6 | 4525 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4526 | { |
1d9e1288 | 4527 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4528 | int ret; |
4529 | ||
45d189c6 | 4530 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4531 | /* only need file table for an actual valid fd */ |
4532 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4533 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4534 | return -EAGAIN; |
5b0bbee4 | 4535 | } |
eddc7ef5 | 4536 | |
e62753e4 BM |
4537 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4538 | ctx->buffer); | |
eddc7ef5 | 4539 | |
eddc7ef5 JA |
4540 | if (ret < 0) |
4541 | req_set_fail_links(req); | |
e1e16097 | 4542 | io_req_complete(req, ret); |
eddc7ef5 JA |
4543 | return 0; |
4544 | } | |
4545 | ||
b5dba59e JA |
4546 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4547 | { | |
14587a46 | 4548 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4549 | return -EINVAL; |
b5dba59e JA |
4550 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4551 | sqe->rw_flags || sqe->buf_index) | |
4552 | return -EINVAL; | |
9c280f90 | 4553 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4554 | return -EBADF; |
b5dba59e JA |
4555 | |
4556 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4557 | return 0; |
b5dba59e JA |
4558 | } |
4559 | ||
889fca73 | 4560 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4561 | { |
9eac1904 | 4562 | struct files_struct *files = current->files; |
3af73b28 | 4563 | struct io_close *close = &req->close; |
9eac1904 JA |
4564 | struct fdtable *fdt; |
4565 | struct file *file; | |
b5dba59e JA |
4566 | int ret; |
4567 | ||
9eac1904 JA |
4568 | file = NULL; |
4569 | ret = -EBADF; | |
4570 | spin_lock(&files->file_lock); | |
4571 | fdt = files_fdtable(files); | |
4572 | if (close->fd >= fdt->max_fds) { | |
4573 | spin_unlock(&files->file_lock); | |
4574 | goto err; | |
4575 | } | |
4576 | file = fdt->fd[close->fd]; | |
4577 | if (!file) { | |
4578 | spin_unlock(&files->file_lock); | |
4579 | goto err; | |
4580 | } | |
4581 | ||
4582 | if (file->f_op == &io_uring_fops) { | |
4583 | spin_unlock(&files->file_lock); | |
4584 | file = NULL; | |
4585 | goto err; | |
3af73b28 | 4586 | } |
b5dba59e JA |
4587 | |
4588 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4589 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4590 | spin_unlock(&files->file_lock); |
0bf0eefd | 4591 | return -EAGAIN; |
a2100672 | 4592 | } |
b5dba59e | 4593 | |
9eac1904 JA |
4594 | ret = __close_fd_get_file(close->fd, &file); |
4595 | spin_unlock(&files->file_lock); | |
4596 | if (ret < 0) { | |
4597 | if (ret == -ENOENT) | |
4598 | ret = -EBADF; | |
4599 | goto err; | |
4600 | } | |
4601 | ||
3af73b28 | 4602 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4603 | ret = filp_close(file, current->files); |
4604 | err: | |
3af73b28 PB |
4605 | if (ret < 0) |
4606 | req_set_fail_links(req); | |
9eac1904 JA |
4607 | if (file) |
4608 | fput(file); | |
889fca73 | 4609 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4610 | return 0; |
b5dba59e JA |
4611 | } |
4612 | ||
1155c76a | 4613 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4614 | { |
4615 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4616 | |
5d17b4a4 JA |
4617 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4618 | return -EINVAL; | |
4619 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4620 | return -EINVAL; | |
4621 | ||
8ed8d3c3 JA |
4622 | req->sync.off = READ_ONCE(sqe->off); |
4623 | req->sync.len = READ_ONCE(sqe->len); | |
4624 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4625 | return 0; |
4626 | } | |
4627 | ||
45d189c6 | 4628 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4629 | { |
8ed8d3c3 JA |
4630 | int ret; |
4631 | ||
ac45abc0 | 4632 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4633 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4634 | return -EAGAIN; |
4635 | ||
9adbd45d | 4636 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4637 | req->sync.flags); |
4638 | if (ret < 0) | |
4639 | req_set_fail_links(req); | |
e1e16097 | 4640 | io_req_complete(req, ret); |
5d17b4a4 JA |
4641 | return 0; |
4642 | } | |
4643 | ||
469956e8 | 4644 | #if defined(CONFIG_NET) |
02d27d89 PB |
4645 | static int io_setup_async_msg(struct io_kiocb *req, |
4646 | struct io_async_msghdr *kmsg) | |
4647 | { | |
e8c2bc1f JA |
4648 | struct io_async_msghdr *async_msg = req->async_data; |
4649 | ||
4650 | if (async_msg) | |
02d27d89 | 4651 | return -EAGAIN; |
e8c2bc1f | 4652 | if (io_alloc_async_data(req)) { |
257e84a5 | 4653 | kfree(kmsg->free_iov); |
02d27d89 PB |
4654 | return -ENOMEM; |
4655 | } | |
e8c2bc1f | 4656 | async_msg = req->async_data; |
02d27d89 | 4657 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4658 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4659 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4660 | /* if were using fast_iov, set it to the new one */ |
4661 | if (!async_msg->free_iov) | |
4662 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4663 | ||
02d27d89 PB |
4664 | return -EAGAIN; |
4665 | } | |
4666 | ||
2ae523ed PB |
4667 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4668 | struct io_async_msghdr *iomsg) | |
4669 | { | |
2ae523ed | 4670 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4671 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4672 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4673 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4674 | } |
4675 | ||
93642ef8 PB |
4676 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4677 | { | |
4678 | int ret; | |
4679 | ||
4680 | if (!io_op_defs[req->opcode].needs_async_data) | |
4681 | return 0; | |
4682 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4683 | if (!ret) | |
4684 | req->flags |= REQ_F_NEED_CLEANUP; | |
4685 | return ret; | |
4686 | } | |
4687 | ||
3529d8c2 | 4688 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4689 | { |
e47293fd | 4690 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4691 | |
d2b6f48b PB |
4692 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4693 | return -EINVAL; | |
4694 | ||
e47293fd | 4695 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4696 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4697 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4698 | |
d8768362 JA |
4699 | #ifdef CONFIG_COMPAT |
4700 | if (req->ctx->compat) | |
4701 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4702 | #endif | |
93642ef8 | 4703 | return 0; |
03b1230c JA |
4704 | } |
4705 | ||
889fca73 | 4706 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4707 | { |
6b754c8b | 4708 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4709 | struct socket *sock; |
7a7cacba | 4710 | unsigned flags; |
0fa03c62 JA |
4711 | int ret; |
4712 | ||
dba4a925 | 4713 | sock = sock_from_file(req->file); |
7a7cacba | 4714 | if (unlikely(!sock)) |
dba4a925 | 4715 | return -ENOTSOCK; |
3529d8c2 | 4716 | |
257e84a5 PB |
4717 | kmsg = req->async_data; |
4718 | if (!kmsg) { | |
7a7cacba PB |
4719 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4720 | if (ret) | |
4721 | return ret; | |
4722 | kmsg = &iomsg; | |
0fa03c62 | 4723 | } |
0fa03c62 | 4724 | |
7a7cacba PB |
4725 | flags = req->sr_msg.msg_flags; |
4726 | if (flags & MSG_DONTWAIT) | |
4727 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4728 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4729 | flags |= MSG_DONTWAIT; |
e47293fd | 4730 | |
7a7cacba | 4731 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4732 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4733 | return io_setup_async_msg(req, kmsg); |
4734 | if (ret == -ERESTARTSYS) | |
4735 | ret = -EINTR; | |
0fa03c62 | 4736 | |
257e84a5 PB |
4737 | /* fast path, check for non-NULL to avoid function call */ |
4738 | if (kmsg->free_iov) | |
4739 | kfree(kmsg->free_iov); | |
99bc4c38 | 4740 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4741 | if (ret < 0) |
4742 | req_set_fail_links(req); | |
889fca73 | 4743 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4744 | return 0; |
03b1230c | 4745 | } |
aa1fa28f | 4746 | |
889fca73 | 4747 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4748 | { |
7a7cacba PB |
4749 | struct io_sr_msg *sr = &req->sr_msg; |
4750 | struct msghdr msg; | |
4751 | struct iovec iov; | |
fddaface | 4752 | struct socket *sock; |
7a7cacba | 4753 | unsigned flags; |
fddaface JA |
4754 | int ret; |
4755 | ||
dba4a925 | 4756 | sock = sock_from_file(req->file); |
7a7cacba | 4757 | if (unlikely(!sock)) |
dba4a925 | 4758 | return -ENOTSOCK; |
fddaface | 4759 | |
7a7cacba PB |
4760 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4761 | if (unlikely(ret)) | |
14db8411 | 4762 | return ret; |
fddaface | 4763 | |
7a7cacba PB |
4764 | msg.msg_name = NULL; |
4765 | msg.msg_control = NULL; | |
4766 | msg.msg_controllen = 0; | |
4767 | msg.msg_namelen = 0; | |
fddaface | 4768 | |
7a7cacba PB |
4769 | flags = req->sr_msg.msg_flags; |
4770 | if (flags & MSG_DONTWAIT) | |
4771 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4772 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4773 | flags |= MSG_DONTWAIT; |
fddaface | 4774 | |
7a7cacba PB |
4775 | msg.msg_flags = flags; |
4776 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4777 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4778 | return -EAGAIN; |
4779 | if (ret == -ERESTARTSYS) | |
4780 | ret = -EINTR; | |
fddaface | 4781 | |
fddaface JA |
4782 | if (ret < 0) |
4783 | req_set_fail_links(req); | |
889fca73 | 4784 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4785 | return 0; |
fddaface JA |
4786 | } |
4787 | ||
1400e697 PB |
4788 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4789 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4790 | { |
4791 | struct io_sr_msg *sr = &req->sr_msg; | |
4792 | struct iovec __user *uiov; | |
4793 | size_t iov_len; | |
4794 | int ret; | |
4795 | ||
1400e697 PB |
4796 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4797 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4798 | if (ret) |
4799 | return ret; | |
4800 | ||
4801 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4802 | if (iov_len > 1) | |
4803 | return -EINVAL; | |
5476dfed | 4804 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4805 | return -EFAULT; |
5476dfed | 4806 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4807 | iomsg->free_iov = NULL; |
52de1fe1 | 4808 | } else { |
257e84a5 | 4809 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4810 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4811 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4812 | false); |
52de1fe1 JA |
4813 | if (ret > 0) |
4814 | ret = 0; | |
4815 | } | |
4816 | ||
4817 | return ret; | |
4818 | } | |
4819 | ||
4820 | #ifdef CONFIG_COMPAT | |
4821 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4822 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4823 | { |
4824 | struct compat_msghdr __user *msg_compat; | |
4825 | struct io_sr_msg *sr = &req->sr_msg; | |
4826 | struct compat_iovec __user *uiov; | |
4827 | compat_uptr_t ptr; | |
4828 | compat_size_t len; | |
4829 | int ret; | |
4830 | ||
270a5940 | 4831 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4832 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4833 | &ptr, &len); |
4834 | if (ret) | |
4835 | return ret; | |
4836 | ||
4837 | uiov = compat_ptr(ptr); | |
4838 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4839 | compat_ssize_t clen; | |
4840 | ||
4841 | if (len > 1) | |
4842 | return -EINVAL; | |
4843 | if (!access_ok(uiov, sizeof(*uiov))) | |
4844 | return -EFAULT; | |
4845 | if (__get_user(clen, &uiov->iov_len)) | |
4846 | return -EFAULT; | |
4847 | if (clen < 0) | |
4848 | return -EINVAL; | |
2d280bc8 | 4849 | sr->len = clen; |
257e84a5 | 4850 | iomsg->free_iov = NULL; |
52de1fe1 | 4851 | } else { |
257e84a5 | 4852 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4853 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4854 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4855 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4856 | if (ret < 0) |
4857 | return ret; | |
4858 | } | |
4859 | ||
4860 | return 0; | |
4861 | } | |
4862 | #endif | |
4863 | ||
1400e697 PB |
4864 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4865 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4866 | { |
1400e697 | 4867 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4868 | |
4869 | #ifdef CONFIG_COMPAT | |
4870 | if (req->ctx->compat) | |
1400e697 | 4871 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4872 | #endif |
52de1fe1 | 4873 | |
1400e697 | 4874 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4875 | } |
4876 | ||
bcda7baa | 4877 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4878 | bool needs_lock) |
bcda7baa JA |
4879 | { |
4880 | struct io_sr_msg *sr = &req->sr_msg; | |
4881 | struct io_buffer *kbuf; | |
4882 | ||
bcda7baa JA |
4883 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4884 | if (IS_ERR(kbuf)) | |
4885 | return kbuf; | |
4886 | ||
4887 | sr->kbuf = kbuf; | |
4888 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4889 | return kbuf; |
fddaface JA |
4890 | } |
4891 | ||
7fbb1b54 PB |
4892 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4893 | { | |
4894 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4895 | } | |
4896 | ||
93642ef8 | 4897 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4898 | { |
99bc4c38 | 4899 | int ret; |
3529d8c2 | 4900 | |
93642ef8 PB |
4901 | if (!io_op_defs[req->opcode].needs_async_data) |
4902 | return 0; | |
4903 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4904 | if (!ret) | |
4905 | req->flags |= REQ_F_NEED_CLEANUP; | |
4906 | return ret; | |
4907 | } | |
4908 | ||
4909 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4910 | { | |
4911 | struct io_sr_msg *sr = &req->sr_msg; | |
4912 | ||
d2b6f48b PB |
4913 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4914 | return -EINVAL; | |
4915 | ||
3529d8c2 | 4916 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4917 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4918 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4919 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4920 | |
d8768362 JA |
4921 | #ifdef CONFIG_COMPAT |
4922 | if (req->ctx->compat) | |
4923 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4924 | #endif | |
93642ef8 | 4925 | return 0; |
aa1fa28f JA |
4926 | } |
4927 | ||
889fca73 | 4928 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4929 | { |
6b754c8b | 4930 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4931 | struct socket *sock; |
7fbb1b54 | 4932 | struct io_buffer *kbuf; |
7a7cacba | 4933 | unsigned flags; |
52de1fe1 | 4934 | int ret, cflags = 0; |
45d189c6 | 4935 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4936 | |
dba4a925 | 4937 | sock = sock_from_file(req->file); |
7a7cacba | 4938 | if (unlikely(!sock)) |
dba4a925 | 4939 | return -ENOTSOCK; |
3529d8c2 | 4940 | |
257e84a5 PB |
4941 | kmsg = req->async_data; |
4942 | if (!kmsg) { | |
7a7cacba PB |
4943 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4944 | if (ret) | |
681fda8d | 4945 | return ret; |
7a7cacba PB |
4946 | kmsg = &iomsg; |
4947 | } | |
03b1230c | 4948 | |
bc02ef33 | 4949 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4950 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4951 | if (IS_ERR(kbuf)) |
52de1fe1 | 4952 | return PTR_ERR(kbuf); |
7a7cacba | 4953 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4954 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4955 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4956 | 1, req->sr_msg.len); |
4957 | } | |
52de1fe1 | 4958 | |
7a7cacba PB |
4959 | flags = req->sr_msg.msg_flags; |
4960 | if (flags & MSG_DONTWAIT) | |
4961 | req->flags |= REQ_F_NOWAIT; | |
4962 | else if (force_nonblock) | |
4963 | flags |= MSG_DONTWAIT; | |
e47293fd | 4964 | |
7a7cacba PB |
4965 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4966 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4967 | if (force_nonblock && ret == -EAGAIN) |
4968 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4969 | if (ret == -ERESTARTSYS) |
4970 | ret = -EINTR; | |
03b1230c | 4971 | |
7fbb1b54 PB |
4972 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4973 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4974 | /* fast path, check for non-NULL to avoid function call */ |
4975 | if (kmsg->free_iov) | |
4976 | kfree(kmsg->free_iov); | |
99bc4c38 | 4977 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4978 | if (ret < 0) |
4979 | req_set_fail_links(req); | |
889fca73 | 4980 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4981 | return 0; |
0fa03c62 | 4982 | } |
5d17b4a4 | 4983 | |
889fca73 | 4984 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4985 | { |
6b754c8b | 4986 | struct io_buffer *kbuf; |
7a7cacba PB |
4987 | struct io_sr_msg *sr = &req->sr_msg; |
4988 | struct msghdr msg; | |
4989 | void __user *buf = sr->buf; | |
fddaface | 4990 | struct socket *sock; |
7a7cacba PB |
4991 | struct iovec iov; |
4992 | unsigned flags; | |
bcda7baa | 4993 | int ret, cflags = 0; |
45d189c6 | 4994 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4995 | |
dba4a925 | 4996 | sock = sock_from_file(req->file); |
7a7cacba | 4997 | if (unlikely(!sock)) |
dba4a925 | 4998 | return -ENOTSOCK; |
fddaface | 4999 | |
bc02ef33 | 5000 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 5001 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
5002 | if (IS_ERR(kbuf)) |
5003 | return PTR_ERR(kbuf); | |
7a7cacba | 5004 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5005 | } |
bcda7baa | 5006 | |
7a7cacba | 5007 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5008 | if (unlikely(ret)) |
5009 | goto out_free; | |
fddaface | 5010 | |
7a7cacba PB |
5011 | msg.msg_name = NULL; |
5012 | msg.msg_control = NULL; | |
5013 | msg.msg_controllen = 0; | |
5014 | msg.msg_namelen = 0; | |
5015 | msg.msg_iocb = NULL; | |
5016 | msg.msg_flags = 0; | |
fddaface | 5017 | |
7a7cacba PB |
5018 | flags = req->sr_msg.msg_flags; |
5019 | if (flags & MSG_DONTWAIT) | |
5020 | req->flags |= REQ_F_NOWAIT; | |
5021 | else if (force_nonblock) | |
5022 | flags |= MSG_DONTWAIT; | |
5023 | ||
5024 | ret = sock_recvmsg(sock, &msg, flags); | |
5025 | if (force_nonblock && ret == -EAGAIN) | |
5026 | return -EAGAIN; | |
5027 | if (ret == -ERESTARTSYS) | |
5028 | ret = -EINTR; | |
14c32eee | 5029 | out_free: |
7fbb1b54 PB |
5030 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5031 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
5032 | if (ret < 0) |
5033 | req_set_fail_links(req); | |
889fca73 | 5034 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5035 | return 0; |
fddaface JA |
5036 | } |
5037 | ||
3529d8c2 | 5038 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5039 | { |
8ed8d3c3 JA |
5040 | struct io_accept *accept = &req->accept; |
5041 | ||
14587a46 | 5042 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5043 | return -EINVAL; |
8042d6ce | 5044 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5045 | return -EINVAL; |
5046 | ||
d55e5f5b JA |
5047 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5048 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5049 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5050 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 5051 | return 0; |
8ed8d3c3 | 5052 | } |
17f2fe35 | 5053 | |
889fca73 | 5054 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5055 | { |
5056 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5057 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5058 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
5059 | int ret; |
5060 | ||
e697deed JX |
5061 | if (req->file->f_flags & O_NONBLOCK) |
5062 | req->flags |= REQ_F_NOWAIT; | |
5063 | ||
8ed8d3c3 | 5064 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
5065 | accept->addr_len, accept->flags, |
5066 | accept->nofile); | |
8ed8d3c3 | 5067 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 5068 | return -EAGAIN; |
ac45abc0 PB |
5069 | if (ret < 0) { |
5070 | if (ret == -ERESTARTSYS) | |
5071 | ret = -EINTR; | |
4e88d6e7 | 5072 | req_set_fail_links(req); |
ac45abc0 | 5073 | } |
889fca73 | 5074 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5075 | return 0; |
8ed8d3c3 JA |
5076 | } |
5077 | ||
93642ef8 PB |
5078 | static int io_connect_prep_async(struct io_kiocb *req) |
5079 | { | |
5080 | struct io_async_connect *io = req->async_data; | |
5081 | struct io_connect *conn = &req->connect; | |
5082 | ||
5083 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5084 | } | |
5085 | ||
3529d8c2 | 5086 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5087 | { |
3529d8c2 | 5088 | struct io_connect *conn = &req->connect; |
f499a021 | 5089 | |
14587a46 | 5090 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
5091 | return -EINVAL; |
5092 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
5093 | return -EINVAL; | |
5094 | ||
3529d8c2 JA |
5095 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5096 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5097 | return 0; |
f499a021 JA |
5098 | } |
5099 | ||
889fca73 | 5100 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5101 | { |
e8c2bc1f | 5102 | struct io_async_connect __io, *io; |
f8e85cf2 | 5103 | unsigned file_flags; |
3fbb51c1 | 5104 | int ret; |
45d189c6 | 5105 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5106 | |
e8c2bc1f JA |
5107 | if (req->async_data) { |
5108 | io = req->async_data; | |
f499a021 | 5109 | } else { |
3529d8c2 JA |
5110 | ret = move_addr_to_kernel(req->connect.addr, |
5111 | req->connect.addr_len, | |
e8c2bc1f | 5112 | &__io.address); |
f499a021 JA |
5113 | if (ret) |
5114 | goto out; | |
5115 | io = &__io; | |
5116 | } | |
5117 | ||
3fbb51c1 JA |
5118 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5119 | ||
e8c2bc1f | 5120 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5121 | req->connect.addr_len, file_flags); |
87f80d62 | 5122 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5123 | if (req->async_data) |
b7bb4f7d | 5124 | return -EAGAIN; |
e8c2bc1f | 5125 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5126 | ret = -ENOMEM; |
5127 | goto out; | |
5128 | } | |
e8c2bc1f JA |
5129 | io = req->async_data; |
5130 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5131 | return -EAGAIN; |
f499a021 | 5132 | } |
f8e85cf2 JA |
5133 | if (ret == -ERESTARTSYS) |
5134 | ret = -EINTR; | |
f499a021 | 5135 | out: |
4e88d6e7 JA |
5136 | if (ret < 0) |
5137 | req_set_fail_links(req); | |
889fca73 | 5138 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5139 | return 0; |
469956e8 Y |
5140 | } |
5141 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5142 | #define IO_NETOP_FN(op) \ |
5143 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5144 | { \ | |
5145 | return -EOPNOTSUPP; \ | |
5146 | } | |
5147 | ||
5148 | #define IO_NETOP_PREP(op) \ | |
5149 | IO_NETOP_FN(op) \ | |
5150 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5151 | { \ | |
5152 | return -EOPNOTSUPP; \ | |
5153 | } \ | |
5154 | ||
5155 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5156 | IO_NETOP_PREP(op) \ | |
5157 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5158 | { \ | |
5159 | return -EOPNOTSUPP; \ | |
5160 | } | |
5161 | ||
5162 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5163 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5164 | IO_NETOP_PREP_ASYNC(connect); | |
5165 | IO_NETOP_PREP(accept); | |
5166 | IO_NETOP_FN(send); | |
5167 | IO_NETOP_FN(recv); | |
469956e8 | 5168 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5169 | |
d7718a9d JA |
5170 | struct io_poll_table { |
5171 | struct poll_table_struct pt; | |
5172 | struct io_kiocb *req; | |
5173 | int error; | |
5174 | }; | |
ce593a6c | 5175 | |
d7718a9d JA |
5176 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5177 | __poll_t mask, task_work_func_t func) | |
5178 | { | |
aa96bf8a | 5179 | int ret; |
d7718a9d JA |
5180 | |
5181 | /* for instances that support it check for an event match first: */ | |
5182 | if (mask && !(mask & poll->events)) | |
5183 | return 0; | |
5184 | ||
5185 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5186 | ||
5187 | list_del_init(&poll->wait.entry); | |
5188 | ||
d7718a9d | 5189 | req->result = mask; |
7cbf1722 | 5190 | req->task_work.func = func; |
6d816e08 JA |
5191 | percpu_ref_get(&req->ctx->refs); |
5192 | ||
d7718a9d | 5193 | /* |
e3aabf95 JA |
5194 | * If this fails, then the task is exiting. When a task exits, the |
5195 | * work gets canceled, so just cancel this request as well instead | |
5196 | * of executing it. We can't safely execute it anyway, as we may not | |
5197 | * have the needed state needed for it anyway. | |
d7718a9d | 5198 | */ |
355fb9e2 | 5199 | ret = io_req_task_work_add(req); |
aa96bf8a | 5200 | if (unlikely(ret)) { |
e3aabf95 | 5201 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 5202 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 5203 | } |
d7718a9d JA |
5204 | return 1; |
5205 | } | |
5206 | ||
74ce6ce4 JA |
5207 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5208 | __acquires(&req->ctx->completion_lock) | |
5209 | { | |
5210 | struct io_ring_ctx *ctx = req->ctx; | |
5211 | ||
5212 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5213 | struct poll_table_struct pt = { ._key = poll->events }; | |
5214 | ||
5215 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5216 | } | |
5217 | ||
5218 | spin_lock_irq(&ctx->completion_lock); | |
5219 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5220 | add_wait_queue(poll->head, &poll->wait); | |
5221 | return true; | |
5222 | } | |
5223 | ||
5224 | return false; | |
5225 | } | |
5226 | ||
d4e7cd36 | 5227 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5228 | { |
e8c2bc1f | 5229 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5230 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5231 | return req->async_data; |
d4e7cd36 JA |
5232 | return req->apoll->double_poll; |
5233 | } | |
5234 | ||
5235 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5236 | { | |
5237 | if (req->opcode == IORING_OP_POLL_ADD) | |
5238 | return &req->poll; | |
5239 | return &req->apoll->poll; | |
5240 | } | |
5241 | ||
5242 | static void io_poll_remove_double(struct io_kiocb *req) | |
5243 | { | |
5244 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5245 | |
5246 | lockdep_assert_held(&req->ctx->completion_lock); | |
5247 | ||
5248 | if (poll && poll->head) { | |
5249 | struct wait_queue_head *head = poll->head; | |
5250 | ||
5251 | spin_lock(&head->lock); | |
5252 | list_del_init(&poll->wait.entry); | |
5253 | if (poll->wait.private) | |
5254 | refcount_dec(&req->refs); | |
5255 | poll->head = NULL; | |
5256 | spin_unlock(&head->lock); | |
5257 | } | |
5258 | } | |
5259 | ||
5260 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5261 | { | |
5262 | struct io_ring_ctx *ctx = req->ctx; | |
5263 | ||
d4e7cd36 | 5264 | io_poll_remove_double(req); |
18bceab1 JA |
5265 | req->poll.done = true; |
5266 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5267 | io_commit_cqring(ctx); | |
5268 | } | |
5269 | ||
dd221f46 | 5270 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5271 | { |
dd221f46 | 5272 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5273 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5274 | struct io_kiocb *nxt; |
18bceab1 JA |
5275 | |
5276 | if (io_poll_rewait(req, &req->poll)) { | |
5277 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5278 | } else { |
5279 | hash_del(&req->hash_node); | |
5280 | io_poll_complete(req, req->result, 0); | |
5281 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5282 | |
dd221f46 PB |
5283 | nxt = io_put_req_find_next(req); |
5284 | io_cqring_ev_posted(ctx); | |
5285 | if (nxt) | |
5286 | __io_req_task_submit(nxt); | |
5287 | } | |
18bceab1 | 5288 | |
6d816e08 | 5289 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5290 | } |
5291 | ||
5292 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5293 | int sync, void *key) | |
5294 | { | |
5295 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5296 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5297 | __poll_t mask = key_to_poll(key); |
5298 | ||
5299 | /* for instances that support it check for an event match first: */ | |
5300 | if (mask && !(mask & poll->events)) | |
5301 | return 0; | |
5302 | ||
8706e04e JA |
5303 | list_del_init(&wait->entry); |
5304 | ||
807abcb0 | 5305 | if (poll && poll->head) { |
18bceab1 JA |
5306 | bool done; |
5307 | ||
807abcb0 JA |
5308 | spin_lock(&poll->head->lock); |
5309 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5310 | if (!done) |
807abcb0 | 5311 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5312 | /* make sure double remove sees this as being gone */ |
5313 | wait->private = NULL; | |
807abcb0 | 5314 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5315 | if (!done) { |
5316 | /* use wait func handler, so it matches the rq type */ | |
5317 | poll->wait.func(&poll->wait, mode, sync, key); | |
5318 | } | |
18bceab1 JA |
5319 | } |
5320 | refcount_dec(&req->refs); | |
5321 | return 1; | |
5322 | } | |
5323 | ||
5324 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5325 | wait_queue_func_t wake_func) | |
5326 | { | |
5327 | poll->head = NULL; | |
5328 | poll->done = false; | |
5329 | poll->canceled = false; | |
5330 | poll->events = events; | |
5331 | INIT_LIST_HEAD(&poll->wait.entry); | |
5332 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5333 | } | |
5334 | ||
5335 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5336 | struct wait_queue_head *head, |
5337 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5338 | { |
5339 | struct io_kiocb *req = pt->req; | |
5340 | ||
5341 | /* | |
5342 | * If poll->head is already set, it's because the file being polled | |
5343 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5344 | * for write). Setup a separate io_poll_iocb if this happens. | |
5345 | */ | |
5346 | if (unlikely(poll->head)) { | |
58852d4d PB |
5347 | struct io_poll_iocb *poll_one = poll; |
5348 | ||
18bceab1 | 5349 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5350 | if (*poll_ptr) { |
18bceab1 JA |
5351 | pt->error = -EINVAL; |
5352 | return; | |
5353 | } | |
5354 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5355 | if (!poll) { | |
5356 | pt->error = -ENOMEM; | |
5357 | return; | |
5358 | } | |
58852d4d | 5359 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5360 | refcount_inc(&req->refs); |
5361 | poll->wait.private = req; | |
807abcb0 | 5362 | *poll_ptr = poll; |
18bceab1 JA |
5363 | } |
5364 | ||
5365 | pt->error = 0; | |
5366 | poll->head = head; | |
a31eb4a2 JX |
5367 | |
5368 | if (poll->events & EPOLLEXCLUSIVE) | |
5369 | add_wait_queue_exclusive(head, &poll->wait); | |
5370 | else | |
5371 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5372 | } |
5373 | ||
5374 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5375 | struct poll_table_struct *p) | |
5376 | { | |
5377 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5378 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5379 | |
807abcb0 | 5380 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5381 | } |
5382 | ||
d7718a9d JA |
5383 | static void io_async_task_func(struct callback_head *cb) |
5384 | { | |
5385 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5386 | struct async_poll *apoll = req->apoll; | |
5387 | struct io_ring_ctx *ctx = req->ctx; | |
5388 | ||
5389 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5390 | ||
74ce6ce4 | 5391 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5392 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5393 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5394 | return; |
d7718a9d JA |
5395 | } |
5396 | ||
31067255 | 5397 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5398 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5399 | hash_del(&req->hash_node); |
2bae047e | 5400 | |
d4e7cd36 | 5401 | io_poll_remove_double(req); |
74ce6ce4 JA |
5402 | spin_unlock_irq(&ctx->completion_lock); |
5403 | ||
0be0b0e3 PB |
5404 | if (!READ_ONCE(apoll->poll.canceled)) |
5405 | __io_req_task_submit(req); | |
5406 | else | |
5407 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5408 | |
6d816e08 | 5409 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5410 | kfree(apoll->double_poll); |
31067255 | 5411 | kfree(apoll); |
d7718a9d JA |
5412 | } |
5413 | ||
5414 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5415 | void *key) | |
5416 | { | |
5417 | struct io_kiocb *req = wait->private; | |
5418 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5419 | ||
5420 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5421 | key_to_poll(key)); | |
5422 | ||
5423 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5424 | } | |
5425 | ||
5426 | static void io_poll_req_insert(struct io_kiocb *req) | |
5427 | { | |
5428 | struct io_ring_ctx *ctx = req->ctx; | |
5429 | struct hlist_head *list; | |
5430 | ||
5431 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5432 | hlist_add_head(&req->hash_node, list); | |
5433 | } | |
5434 | ||
5435 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5436 | struct io_poll_iocb *poll, | |
5437 | struct io_poll_table *ipt, __poll_t mask, | |
5438 | wait_queue_func_t wake_func) | |
5439 | __acquires(&ctx->completion_lock) | |
5440 | { | |
5441 | struct io_ring_ctx *ctx = req->ctx; | |
5442 | bool cancel = false; | |
5443 | ||
4d52f338 | 5444 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5445 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5446 | poll->file = req->file; |
18bceab1 | 5447 | poll->wait.private = req; |
d7718a9d JA |
5448 | |
5449 | ipt->pt._key = mask; | |
5450 | ipt->req = req; | |
5451 | ipt->error = -EINVAL; | |
5452 | ||
d7718a9d JA |
5453 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5454 | ||
5455 | spin_lock_irq(&ctx->completion_lock); | |
5456 | if (likely(poll->head)) { | |
5457 | spin_lock(&poll->head->lock); | |
5458 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5459 | if (ipt->error) | |
5460 | cancel = true; | |
5461 | ipt->error = 0; | |
5462 | mask = 0; | |
5463 | } | |
5464 | if (mask || ipt->error) | |
5465 | list_del_init(&poll->wait.entry); | |
5466 | else if (cancel) | |
5467 | WRITE_ONCE(poll->canceled, true); | |
5468 | else if (!poll->done) /* actually waiting for an event */ | |
5469 | io_poll_req_insert(req); | |
5470 | spin_unlock(&poll->head->lock); | |
5471 | } | |
5472 | ||
5473 | return mask; | |
5474 | } | |
5475 | ||
5476 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5477 | { | |
5478 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5479 | struct io_ring_ctx *ctx = req->ctx; | |
5480 | struct async_poll *apoll; | |
5481 | struct io_poll_table ipt; | |
5482 | __poll_t mask, ret; | |
9dab14b8 | 5483 | int rw; |
d7718a9d JA |
5484 | |
5485 | if (!req->file || !file_can_poll(req->file)) | |
5486 | return false; | |
24c74678 | 5487 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5488 | return false; |
9dab14b8 JA |
5489 | if (def->pollin) |
5490 | rw = READ; | |
5491 | else if (def->pollout) | |
5492 | rw = WRITE; | |
5493 | else | |
5494 | return false; | |
5495 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5496 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5497 | return false; |
5498 | ||
5499 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5500 | if (unlikely(!apoll)) | |
5501 | return false; | |
807abcb0 | 5502 | apoll->double_poll = NULL; |
d7718a9d JA |
5503 | |
5504 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5505 | req->apoll = apoll; |
d7718a9d | 5506 | |
8755d97a | 5507 | mask = 0; |
d7718a9d | 5508 | if (def->pollin) |
8755d97a | 5509 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5510 | if (def->pollout) |
5511 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5512 | |
5513 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5514 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5515 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5516 | mask &= ~POLLIN; | |
5517 | ||
d7718a9d JA |
5518 | mask |= POLLERR | POLLPRI; |
5519 | ||
5520 | ipt.pt._qproc = io_async_queue_proc; | |
5521 | ||
5522 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5523 | io_async_wake); | |
a36da65c | 5524 | if (ret || ipt.error) { |
d4e7cd36 | 5525 | io_poll_remove_double(req); |
d7718a9d | 5526 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5527 | kfree(apoll->double_poll); |
d7718a9d JA |
5528 | kfree(apoll); |
5529 | return false; | |
5530 | } | |
5531 | spin_unlock_irq(&ctx->completion_lock); | |
5532 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5533 | apoll->poll.events); | |
5534 | return true; | |
5535 | } | |
5536 | ||
5537 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5538 | struct io_poll_iocb *poll) | |
221c5eb2 | 5539 | { |
b41e9852 | 5540 | bool do_complete = false; |
221c5eb2 JA |
5541 | |
5542 | spin_lock(&poll->head->lock); | |
5543 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5544 | if (!list_empty(&poll->wait.entry)) { |
5545 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5546 | do_complete = true; |
221c5eb2 JA |
5547 | } |
5548 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5549 | hash_del(&req->hash_node); |
d7718a9d JA |
5550 | return do_complete; |
5551 | } | |
5552 | ||
5553 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5554 | { | |
5555 | bool do_complete; | |
5556 | ||
d4e7cd36 JA |
5557 | io_poll_remove_double(req); |
5558 | ||
d7718a9d JA |
5559 | if (req->opcode == IORING_OP_POLL_ADD) { |
5560 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5561 | } else { | |
3bfa5bcb JA |
5562 | struct async_poll *apoll = req->apoll; |
5563 | ||
d7718a9d | 5564 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5565 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5566 | if (do_complete) { | |
d7718a9d | 5567 | io_put_req(req); |
807abcb0 | 5568 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5569 | kfree(apoll); |
5570 | } | |
b1f573bd XW |
5571 | } |
5572 | ||
b41e9852 JA |
5573 | if (do_complete) { |
5574 | io_cqring_fill_event(req, -ECANCELED); | |
5575 | io_commit_cqring(req->ctx); | |
f254ac04 | 5576 | req_set_fail_links(req); |
216578e5 | 5577 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5578 | } |
5579 | ||
5580 | return do_complete; | |
221c5eb2 JA |
5581 | } |
5582 | ||
76e1b642 JA |
5583 | /* |
5584 | * Returns true if we found and killed one or more poll requests | |
5585 | */ | |
6b81928d PB |
5586 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5587 | struct files_struct *files) | |
221c5eb2 | 5588 | { |
78076bb6 | 5589 | struct hlist_node *tmp; |
221c5eb2 | 5590 | struct io_kiocb *req; |
8e2e1faf | 5591 | int posted = 0, i; |
221c5eb2 JA |
5592 | |
5593 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5594 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5595 | struct hlist_head *list; | |
5596 | ||
5597 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5598 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5599 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5600 | posted += io_poll_remove_one(req); |
5601 | } | |
221c5eb2 JA |
5602 | } |
5603 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5604 | |
8e2e1faf JA |
5605 | if (posted) |
5606 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5607 | |
5608 | return posted != 0; | |
221c5eb2 JA |
5609 | } |
5610 | ||
47f46768 JA |
5611 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5612 | { | |
78076bb6 | 5613 | struct hlist_head *list; |
47f46768 JA |
5614 | struct io_kiocb *req; |
5615 | ||
78076bb6 JA |
5616 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5617 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5618 | if (sqe_addr != req->user_data) |
5619 | continue; | |
5620 | if (io_poll_remove_one(req)) | |
eac406c6 | 5621 | return 0; |
b41e9852 | 5622 | return -EALREADY; |
47f46768 JA |
5623 | } |
5624 | ||
5625 | return -ENOENT; | |
5626 | } | |
5627 | ||
3529d8c2 JA |
5628 | static int io_poll_remove_prep(struct io_kiocb *req, |
5629 | const struct io_uring_sqe *sqe) | |
0969e783 | 5630 | { |
0969e783 JA |
5631 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5632 | return -EINVAL; | |
5633 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5634 | sqe->poll_events) | |
5635 | return -EINVAL; | |
5636 | ||
018043be | 5637 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5638 | return 0; |
5639 | } | |
5640 | ||
221c5eb2 JA |
5641 | /* |
5642 | * Find a running poll command that matches one specified in sqe->addr, | |
5643 | * and remove it if found. | |
5644 | */ | |
61e98203 | 5645 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5646 | { |
5647 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5648 | int ret; |
221c5eb2 | 5649 | |
221c5eb2 | 5650 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5651 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5652 | spin_unlock_irq(&ctx->completion_lock); |
5653 | ||
4e88d6e7 JA |
5654 | if (ret < 0) |
5655 | req_set_fail_links(req); | |
e1e16097 | 5656 | io_req_complete(req, ret); |
221c5eb2 JA |
5657 | return 0; |
5658 | } | |
5659 | ||
221c5eb2 JA |
5660 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5661 | void *key) | |
5662 | { | |
c2f2eb7d JA |
5663 | struct io_kiocb *req = wait->private; |
5664 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5665 | |
d7718a9d | 5666 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5667 | } |
5668 | ||
221c5eb2 JA |
5669 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5670 | struct poll_table_struct *p) | |
5671 | { | |
5672 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5673 | ||
e8c2bc1f | 5674 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5675 | } |
5676 | ||
3529d8c2 | 5677 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5678 | { |
5679 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5680 | u32 events; |
221c5eb2 JA |
5681 | |
5682 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5683 | return -EINVAL; | |
5684 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5685 | return -EINVAL; | |
5686 | ||
5769a351 JX |
5687 | events = READ_ONCE(sqe->poll32_events); |
5688 | #ifdef __BIG_ENDIAN | |
5689 | events = swahw32(events); | |
5690 | #endif | |
a31eb4a2 JX |
5691 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5692 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5693 | return 0; |
5694 | } | |
5695 | ||
61e98203 | 5696 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5697 | { |
5698 | struct io_poll_iocb *poll = &req->poll; | |
5699 | struct io_ring_ctx *ctx = req->ctx; | |
5700 | struct io_poll_table ipt; | |
0969e783 | 5701 | __poll_t mask; |
0969e783 | 5702 | |
d7718a9d | 5703 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5704 | |
d7718a9d JA |
5705 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5706 | io_poll_wake); | |
221c5eb2 | 5707 | |
8c838788 | 5708 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5709 | ipt.error = 0; |
b0dd8a41 | 5710 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5711 | } |
221c5eb2 JA |
5712 | spin_unlock_irq(&ctx->completion_lock); |
5713 | ||
8c838788 JA |
5714 | if (mask) { |
5715 | io_cqring_ev_posted(ctx); | |
014db007 | 5716 | io_put_req(req); |
221c5eb2 | 5717 | } |
8c838788 | 5718 | return ipt.error; |
221c5eb2 JA |
5719 | } |
5720 | ||
5262f567 JA |
5721 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5722 | { | |
ad8a48ac JA |
5723 | struct io_timeout_data *data = container_of(timer, |
5724 | struct io_timeout_data, timer); | |
5725 | struct io_kiocb *req = data->req; | |
5726 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5727 | unsigned long flags; |
5728 | ||
5262f567 | 5729 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5730 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5731 | atomic_set(&req->ctx->cq_timeouts, |
5732 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5733 | ||
78e19bbe | 5734 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5735 | io_commit_cqring(ctx); |
5736 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5737 | ||
5738 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5739 | req_set_fail_links(req); |
5262f567 JA |
5740 | io_put_req(req); |
5741 | return HRTIMER_NORESTART; | |
5742 | } | |
5743 | ||
fbd15848 PB |
5744 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5745 | __u64 user_data) | |
f254ac04 | 5746 | { |
fbd15848 | 5747 | struct io_timeout_data *io; |
47f46768 JA |
5748 | struct io_kiocb *req; |
5749 | int ret = -ENOENT; | |
f254ac04 | 5750 | |
135fcde8 | 5751 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5752 | if (user_data == req->user_data) { |
47f46768 JA |
5753 | ret = 0; |
5754 | break; | |
5755 | } | |
5756 | } | |
5757 | ||
5758 | if (ret == -ENOENT) | |
fbd15848 PB |
5759 | return ERR_PTR(ret); |
5760 | ||
5761 | io = req->async_data; | |
e8c2bc1f | 5762 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5763 | if (ret == -1) |
fbd15848 | 5764 | return ERR_PTR(-EALREADY); |
a71976f3 | 5765 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5766 | return req; |
5767 | } | |
47f46768 | 5768 | |
fbd15848 PB |
5769 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5770 | { | |
5771 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5772 | ||
5773 | if (IS_ERR(req)) | |
5774 | return PTR_ERR(req); | |
f254ac04 JA |
5775 | |
5776 | req_set_fail_links(req); | |
f254ac04 | 5777 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5778 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5779 | return 0; |
5780 | } | |
5781 | ||
9c8e11b3 PB |
5782 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5783 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5784 | { |
9c8e11b3 PB |
5785 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5786 | struct io_timeout_data *data; | |
47f46768 | 5787 | |
9c8e11b3 PB |
5788 | if (IS_ERR(req)) |
5789 | return PTR_ERR(req); | |
47f46768 | 5790 | |
9c8e11b3 PB |
5791 | req->timeout.off = 0; /* noseq */ |
5792 | data = req->async_data; | |
5793 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5794 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5795 | data->timer.function = io_timeout_fn; | |
5796 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5797 | return 0; | |
47f46768 JA |
5798 | } |
5799 | ||
3529d8c2 JA |
5800 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5801 | const struct io_uring_sqe *sqe) | |
b29472ee | 5802 | { |
9c8e11b3 PB |
5803 | struct io_timeout_rem *tr = &req->timeout_rem; |
5804 | ||
b29472ee JA |
5805 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5806 | return -EINVAL; | |
61710e43 DA |
5807 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5808 | return -EINVAL; | |
9c8e11b3 | 5809 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5810 | return -EINVAL; |
5811 | ||
9c8e11b3 PB |
5812 | tr->addr = READ_ONCE(sqe->addr); |
5813 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5814 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5815 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5816 | return -EINVAL; | |
5817 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5818 | return -EFAULT; | |
5819 | } else if (tr->flags) { | |
5820 | /* timeout removal doesn't support flags */ | |
b29472ee | 5821 | return -EINVAL; |
9c8e11b3 | 5822 | } |
b29472ee | 5823 | |
b29472ee JA |
5824 | return 0; |
5825 | } | |
5826 | ||
8662daec PB |
5827 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5828 | { | |
5829 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5830 | : HRTIMER_MODE_REL; | |
5831 | } | |
5832 | ||
11365043 JA |
5833 | /* |
5834 | * Remove or update an existing timeout command | |
5835 | */ | |
61e98203 | 5836 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5837 | { |
9c8e11b3 | 5838 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5839 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5840 | int ret; |
11365043 | 5841 | |
11365043 | 5842 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5843 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5844 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5845 | else |
5846 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5847 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5848 | |
47f46768 | 5849 | io_cqring_fill_event(req, ret); |
11365043 JA |
5850 | io_commit_cqring(ctx); |
5851 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5852 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5853 | if (ret < 0) |
5854 | req_set_fail_links(req); | |
ec9c02ad | 5855 | io_put_req(req); |
11365043 | 5856 | return 0; |
5262f567 JA |
5857 | } |
5858 | ||
3529d8c2 | 5859 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5860 | bool is_timeout_link) |
5262f567 | 5861 | { |
ad8a48ac | 5862 | struct io_timeout_data *data; |
a41525ab | 5863 | unsigned flags; |
56080b02 | 5864 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5865 | |
ad8a48ac | 5866 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5867 | return -EINVAL; |
ad8a48ac | 5868 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5869 | return -EINVAL; |
56080b02 | 5870 | if (off && is_timeout_link) |
2d28390a | 5871 | return -EINVAL; |
a41525ab JA |
5872 | flags = READ_ONCE(sqe->timeout_flags); |
5873 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5874 | return -EINVAL; |
bdf20073 | 5875 | |
bfe68a22 | 5876 | req->timeout.off = off; |
26a61679 | 5877 | |
e8c2bc1f | 5878 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5879 | return -ENOMEM; |
5880 | ||
e8c2bc1f | 5881 | data = req->async_data; |
ad8a48ac | 5882 | data->req = req; |
ad8a48ac JA |
5883 | |
5884 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5885 | return -EFAULT; |
5886 | ||
8662daec | 5887 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5888 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5889 | return 0; | |
5890 | } | |
5891 | ||
61e98203 | 5892 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5893 | { |
ad8a48ac | 5894 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5895 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5896 | struct list_head *entry; |
bfe68a22 | 5897 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5898 | |
733f5c95 | 5899 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5900 | |
5262f567 JA |
5901 | /* |
5902 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5903 | * timeout event to be satisfied. If it isn't set, then this is |
5904 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5905 | */ |
8eb7e2d0 | 5906 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5907 | entry = ctx->timeout_list.prev; |
5908 | goto add; | |
5909 | } | |
5262f567 | 5910 | |
bfe68a22 PB |
5911 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5912 | req->timeout.target_seq = tail + off; | |
5262f567 | 5913 | |
f010505b MDG |
5914 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5915 | * This is safe because ->completion_lock is held, and submissions | |
5916 | * and completions are never mixed in the same ->completion_lock section. | |
5917 | */ | |
5918 | ctx->cq_last_tm_flush = tail; | |
5919 | ||
5262f567 JA |
5920 | /* |
5921 | * Insertion sort, ensuring the first entry in the list is always | |
5922 | * the one we need first. | |
5923 | */ | |
5262f567 | 5924 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5925 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5926 | timeout.list); | |
5262f567 | 5927 | |
8eb7e2d0 | 5928 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5929 | continue; |
bfe68a22 PB |
5930 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5931 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5932 | break; |
5933 | } | |
93bd25bb | 5934 | add: |
135fcde8 | 5935 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5936 | data->timer.function = io_timeout_fn; |
5937 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5938 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5939 | return 0; |
5940 | } | |
5262f567 | 5941 | |
62755e35 JA |
5942 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5943 | { | |
5944 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5945 | ||
5946 | return req->user_data == (unsigned long) data; | |
5947 | } | |
5948 | ||
e977d6d3 | 5949 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5950 | { |
62755e35 | 5951 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5952 | int ret = 0; |
5953 | ||
4f26bda1 | 5954 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5955 | switch (cancel_ret) { |
5956 | case IO_WQ_CANCEL_OK: | |
5957 | ret = 0; | |
5958 | break; | |
5959 | case IO_WQ_CANCEL_RUNNING: | |
5960 | ret = -EALREADY; | |
5961 | break; | |
5962 | case IO_WQ_CANCEL_NOTFOUND: | |
5963 | ret = -ENOENT; | |
5964 | break; | |
5965 | } | |
5966 | ||
e977d6d3 JA |
5967 | return ret; |
5968 | } | |
5969 | ||
47f46768 JA |
5970 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5971 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5972 | int success_ret) |
47f46768 JA |
5973 | { |
5974 | unsigned long flags; | |
5975 | int ret; | |
5976 | ||
5977 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5978 | if (ret != -ENOENT) { | |
5979 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5980 | goto done; | |
5981 | } | |
5982 | ||
5983 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5984 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5985 | if (ret != -ENOENT) | |
5986 | goto done; | |
5987 | ret = io_poll_cancel(ctx, sqe_addr); | |
5988 | done: | |
b0dd8a41 JA |
5989 | if (!ret) |
5990 | ret = success_ret; | |
47f46768 JA |
5991 | io_cqring_fill_event(req, ret); |
5992 | io_commit_cqring(ctx); | |
5993 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5994 | io_cqring_ev_posted(ctx); | |
5995 | ||
4e88d6e7 JA |
5996 | if (ret < 0) |
5997 | req_set_fail_links(req); | |
014db007 | 5998 | io_put_req(req); |
47f46768 JA |
5999 | } |
6000 | ||
3529d8c2 JA |
6001 | static int io_async_cancel_prep(struct io_kiocb *req, |
6002 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6003 | { |
fbf23849 | 6004 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6005 | return -EINVAL; |
61710e43 DA |
6006 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6007 | return -EINVAL; | |
6008 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
6009 | return -EINVAL; |
6010 | ||
fbf23849 JA |
6011 | req->cancel.addr = READ_ONCE(sqe->addr); |
6012 | return 0; | |
6013 | } | |
6014 | ||
61e98203 | 6015 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6016 | { |
6017 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 6018 | |
014db007 | 6019 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
6020 | return 0; |
6021 | } | |
6022 | ||
269bbe5f | 6023 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6024 | const struct io_uring_sqe *sqe) |
6025 | { | |
6ca56f84 JA |
6026 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
6027 | return -EINVAL; | |
61710e43 DA |
6028 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6029 | return -EINVAL; | |
6030 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
6031 | return -EINVAL; |
6032 | ||
269bbe5f BM |
6033 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6034 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6035 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6036 | return -EINVAL; |
269bbe5f | 6037 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6038 | return 0; |
6039 | } | |
6040 | ||
889fca73 | 6041 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6042 | { |
6043 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 6044 | struct io_uring_rsrc_update up; |
05f3fb3c | 6045 | int ret; |
fbf23849 | 6046 | |
45d189c6 | 6047 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 6048 | return -EAGAIN; |
05f3fb3c | 6049 | |
269bbe5f BM |
6050 | up.offset = req->rsrc_update.offset; |
6051 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
6052 | |
6053 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 6054 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6055 | mutex_unlock(&ctx->uring_lock); |
6056 | ||
6057 | if (ret < 0) | |
6058 | req_set_fail_links(req); | |
889fca73 | 6059 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6060 | return 0; |
6061 | } | |
6062 | ||
bfe76559 | 6063 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6064 | { |
d625c6ee | 6065 | switch (req->opcode) { |
e781573e | 6066 | case IORING_OP_NOP: |
bfe76559 | 6067 | return 0; |
f67676d1 JA |
6068 | case IORING_OP_READV: |
6069 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6070 | case IORING_OP_READ: |
bfe76559 | 6071 | return io_read_prep(req, sqe); |
f67676d1 JA |
6072 | case IORING_OP_WRITEV: |
6073 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6074 | case IORING_OP_WRITE: |
bfe76559 | 6075 | return io_write_prep(req, sqe); |
0969e783 | 6076 | case IORING_OP_POLL_ADD: |
bfe76559 | 6077 | return io_poll_add_prep(req, sqe); |
0969e783 | 6078 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 6079 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 6080 | case IORING_OP_FSYNC: |
1155c76a | 6081 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6082 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6083 | return io_sfr_prep(req, sqe); |
03b1230c | 6084 | case IORING_OP_SENDMSG: |
fddaface | 6085 | case IORING_OP_SEND: |
bfe76559 | 6086 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6087 | case IORING_OP_RECVMSG: |
fddaface | 6088 | case IORING_OP_RECV: |
bfe76559 | 6089 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6090 | case IORING_OP_CONNECT: |
bfe76559 | 6091 | return io_connect_prep(req, sqe); |
2d28390a | 6092 | case IORING_OP_TIMEOUT: |
bfe76559 | 6093 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6094 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6095 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6096 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6097 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6098 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6099 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6100 | case IORING_OP_ACCEPT: |
bfe76559 | 6101 | return io_accept_prep(req, sqe); |
d63d1b5e | 6102 | case IORING_OP_FALLOCATE: |
bfe76559 | 6103 | return io_fallocate_prep(req, sqe); |
15b71abe | 6104 | case IORING_OP_OPENAT: |
bfe76559 | 6105 | return io_openat_prep(req, sqe); |
b5dba59e | 6106 | case IORING_OP_CLOSE: |
bfe76559 | 6107 | return io_close_prep(req, sqe); |
05f3fb3c | 6108 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6109 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6110 | case IORING_OP_STATX: |
bfe76559 | 6111 | return io_statx_prep(req, sqe); |
4840e418 | 6112 | case IORING_OP_FADVISE: |
bfe76559 | 6113 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6114 | case IORING_OP_MADVISE: |
bfe76559 | 6115 | return io_madvise_prep(req, sqe); |
cebdb986 | 6116 | case IORING_OP_OPENAT2: |
bfe76559 | 6117 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6118 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6119 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6120 | case IORING_OP_SPLICE: |
bfe76559 | 6121 | return io_splice_prep(req, sqe); |
ddf0322d | 6122 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6123 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6124 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6125 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6126 | case IORING_OP_TEE: |
bfe76559 | 6127 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6128 | case IORING_OP_SHUTDOWN: |
6129 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6130 | case IORING_OP_RENAMEAT: |
6131 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6132 | case IORING_OP_UNLINKAT: |
6133 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6134 | } |
6135 | ||
bfe76559 PB |
6136 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6137 | req->opcode); | |
6138 | return-EINVAL; | |
6139 | } | |
6140 | ||
93642ef8 | 6141 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6142 | { |
93642ef8 PB |
6143 | switch (req->opcode) { |
6144 | case IORING_OP_READV: | |
6145 | case IORING_OP_READ_FIXED: | |
6146 | case IORING_OP_READ: | |
6147 | return io_rw_prep_async(req, READ); | |
6148 | case IORING_OP_WRITEV: | |
6149 | case IORING_OP_WRITE_FIXED: | |
6150 | case IORING_OP_WRITE: | |
6151 | return io_rw_prep_async(req, WRITE); | |
6152 | case IORING_OP_SENDMSG: | |
6153 | case IORING_OP_SEND: | |
6154 | return io_sendmsg_prep_async(req); | |
6155 | case IORING_OP_RECVMSG: | |
6156 | case IORING_OP_RECV: | |
6157 | return io_recvmsg_prep_async(req); | |
6158 | case IORING_OP_CONNECT: | |
6159 | return io_connect_prep_async(req); | |
6160 | } | |
6161 | return 0; | |
6162 | } | |
6163 | ||
be7053b7 | 6164 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 6165 | { |
be7053b7 | 6166 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 6167 | return 0; |
be7053b7 | 6168 | /* some opcodes init it during the inital prep */ |
93642ef8 | 6169 | if (req->async_data) |
be7053b7 PB |
6170 | return 0; |
6171 | if (__io_alloc_async_data(req)) | |
bfe76559 | 6172 | return -EAGAIN; |
be7053b7 | 6173 | return io_req_prep_async(req); |
f67676d1 JA |
6174 | } |
6175 | ||
9cf7c104 PB |
6176 | static u32 io_get_sequence(struct io_kiocb *req) |
6177 | { | |
6178 | struct io_kiocb *pos; | |
6179 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6180 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6181 | |
f2f87370 PB |
6182 | io_for_each_link(pos, req) |
6183 | nr_reqs++; | |
9cf7c104 PB |
6184 | |
6185 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6186 | return total_submitted - nr_reqs; | |
6187 | } | |
6188 | ||
be7053b7 | 6189 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 6190 | { |
a197f664 | 6191 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6192 | struct io_defer_entry *de; |
f67676d1 | 6193 | int ret; |
9cf7c104 | 6194 | u32 seq; |
de0617e4 | 6195 | |
9d858b21 | 6196 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6197 | if (likely(list_empty_careful(&ctx->defer_list) && |
6198 | !(req->flags & REQ_F_IO_DRAIN))) | |
6199 | return 0; | |
6200 | ||
6201 | seq = io_get_sequence(req); | |
6202 | /* Still a chance to pass the sequence check */ | |
6203 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6204 | return 0; |
6205 | ||
be7053b7 PB |
6206 | ret = io_req_defer_prep(req); |
6207 | if (ret) | |
6208 | return ret; | |
cbdcb435 | 6209 | io_prep_async_link(req); |
27dc8338 PB |
6210 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6211 | if (!de) | |
6212 | return -ENOMEM; | |
2d28390a | 6213 | |
de0617e4 | 6214 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6215 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6216 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6217 | kfree(de); |
ae34817b PB |
6218 | io_queue_async_work(req); |
6219 | return -EIOCBQUEUED; | |
de0617e4 JA |
6220 | } |
6221 | ||
915967f6 | 6222 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6223 | de->req = req; |
9cf7c104 | 6224 | de->seq = seq; |
27dc8338 | 6225 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6226 | spin_unlock_irq(&ctx->completion_lock); |
6227 | return -EIOCBQUEUED; | |
6228 | } | |
6229 | ||
3ca405eb | 6230 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6231 | { |
0e1b6fe3 PB |
6232 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6233 | switch (req->opcode) { | |
6234 | case IORING_OP_READV: | |
6235 | case IORING_OP_READ_FIXED: | |
6236 | case IORING_OP_READ: | |
bcda7baa | 6237 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6238 | break; |
6239 | case IORING_OP_RECVMSG: | |
6240 | case IORING_OP_RECV: | |
bcda7baa | 6241 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6242 | break; |
6243 | } | |
6244 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6245 | } |
6246 | ||
0e1b6fe3 PB |
6247 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6248 | switch (req->opcode) { | |
6249 | case IORING_OP_READV: | |
6250 | case IORING_OP_READ_FIXED: | |
6251 | case IORING_OP_READ: | |
6252 | case IORING_OP_WRITEV: | |
6253 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6254 | case IORING_OP_WRITE: { |
6255 | struct io_async_rw *io = req->async_data; | |
6256 | if (io->free_iovec) | |
6257 | kfree(io->free_iovec); | |
0e1b6fe3 | 6258 | break; |
e8c2bc1f | 6259 | } |
0e1b6fe3 | 6260 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6261 | case IORING_OP_SENDMSG: { |
6262 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6263 | |
6264 | kfree(io->free_iov); | |
0e1b6fe3 | 6265 | break; |
e8c2bc1f | 6266 | } |
0e1b6fe3 PB |
6267 | case IORING_OP_SPLICE: |
6268 | case IORING_OP_TEE: | |
6269 | io_put_file(req, req->splice.file_in, | |
6270 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6271 | break; | |
f3cd4850 JA |
6272 | case IORING_OP_OPENAT: |
6273 | case IORING_OP_OPENAT2: | |
6274 | if (req->open.filename) | |
6275 | putname(req->open.filename); | |
6276 | break; | |
80a261fd JA |
6277 | case IORING_OP_RENAMEAT: |
6278 | putname(req->rename.oldpath); | |
6279 | putname(req->rename.newpath); | |
6280 | break; | |
14a1143b JA |
6281 | case IORING_OP_UNLINKAT: |
6282 | putname(req->unlink.filename); | |
6283 | break; | |
0e1b6fe3 PB |
6284 | } |
6285 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6286 | } |
99bc4c38 PB |
6287 | } |
6288 | ||
889fca73 | 6289 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6290 | { |
a197f664 | 6291 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6292 | int ret; |
2b188cc1 | 6293 | |
d625c6ee | 6294 | switch (req->opcode) { |
2b188cc1 | 6295 | case IORING_OP_NOP: |
889fca73 | 6296 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6297 | break; |
6298 | case IORING_OP_READV: | |
edafccee | 6299 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6300 | case IORING_OP_READ: |
889fca73 | 6301 | ret = io_read(req, issue_flags); |
edafccee | 6302 | break; |
3529d8c2 | 6303 | case IORING_OP_WRITEV: |
edafccee | 6304 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6305 | case IORING_OP_WRITE: |
889fca73 | 6306 | ret = io_write(req, issue_flags); |
2b188cc1 | 6307 | break; |
c992fe29 | 6308 | case IORING_OP_FSYNC: |
45d189c6 | 6309 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6310 | break; |
221c5eb2 | 6311 | case IORING_OP_POLL_ADD: |
61e98203 | 6312 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6313 | break; |
6314 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 6315 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 6316 | break; |
5d17b4a4 | 6317 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6318 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6319 | break; |
0fa03c62 | 6320 | case IORING_OP_SENDMSG: |
889fca73 | 6321 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6322 | break; |
fddaface | 6323 | case IORING_OP_SEND: |
889fca73 | 6324 | ret = io_send(req, issue_flags); |
0fa03c62 | 6325 | break; |
aa1fa28f | 6326 | case IORING_OP_RECVMSG: |
889fca73 | 6327 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6328 | break; |
fddaface | 6329 | case IORING_OP_RECV: |
889fca73 | 6330 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6331 | break; |
5262f567 | 6332 | case IORING_OP_TIMEOUT: |
61e98203 | 6333 | ret = io_timeout(req, issue_flags); |
5262f567 | 6334 | break; |
11365043 | 6335 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6336 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6337 | break; |
17f2fe35 | 6338 | case IORING_OP_ACCEPT: |
889fca73 | 6339 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6340 | break; |
f8e85cf2 | 6341 | case IORING_OP_CONNECT: |
889fca73 | 6342 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6343 | break; |
62755e35 | 6344 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6345 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6346 | break; |
d63d1b5e | 6347 | case IORING_OP_FALLOCATE: |
45d189c6 | 6348 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6349 | break; |
15b71abe | 6350 | case IORING_OP_OPENAT: |
45d189c6 | 6351 | ret = io_openat(req, issue_flags); |
15b71abe | 6352 | break; |
b5dba59e | 6353 | case IORING_OP_CLOSE: |
889fca73 | 6354 | ret = io_close(req, issue_flags); |
b5dba59e | 6355 | break; |
05f3fb3c | 6356 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6357 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6358 | break; |
eddc7ef5 | 6359 | case IORING_OP_STATX: |
45d189c6 | 6360 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6361 | break; |
4840e418 | 6362 | case IORING_OP_FADVISE: |
45d189c6 | 6363 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6364 | break; |
c1ca757b | 6365 | case IORING_OP_MADVISE: |
45d189c6 | 6366 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6367 | break; |
cebdb986 | 6368 | case IORING_OP_OPENAT2: |
45d189c6 | 6369 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6370 | break; |
3e4827b0 | 6371 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6372 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6373 | break; |
7d67af2c | 6374 | case IORING_OP_SPLICE: |
45d189c6 | 6375 | ret = io_splice(req, issue_flags); |
7d67af2c | 6376 | break; |
ddf0322d | 6377 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6378 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6379 | break; |
067524e9 | 6380 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6381 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6382 | break; |
f2a8d5c7 | 6383 | case IORING_OP_TEE: |
45d189c6 | 6384 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6385 | break; |
36f4fa68 | 6386 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6387 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6388 | break; |
80a261fd | 6389 | case IORING_OP_RENAMEAT: |
45d189c6 | 6390 | ret = io_renameat(req, issue_flags); |
80a261fd | 6391 | break; |
14a1143b | 6392 | case IORING_OP_UNLINKAT: |
45d189c6 | 6393 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6394 | break; |
2b188cc1 JA |
6395 | default: |
6396 | ret = -EINVAL; | |
6397 | break; | |
6398 | } | |
6399 | ||
def596e9 JA |
6400 | if (ret) |
6401 | return ret; | |
6402 | ||
b532576e JA |
6403 | /* If the op doesn't have a file, we're not polling for it */ |
6404 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6405 | const bool in_async = io_wq_current_is_worker(); |
6406 | ||
11ba820b JA |
6407 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6408 | if (in_async) | |
6409 | mutex_lock(&ctx->uring_lock); | |
6410 | ||
2e9dbe90 | 6411 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6412 | |
6413 | if (in_async) | |
6414 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6415 | } |
6416 | ||
6417 | return 0; | |
2b188cc1 JA |
6418 | } |
6419 | ||
5280f7e5 | 6420 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6421 | { |
6422 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6423 | struct io_kiocb *timeout; |
561fb04a | 6424 | int ret = 0; |
2b188cc1 | 6425 | |
6df1db6b PB |
6426 | timeout = io_prep_linked_timeout(req); |
6427 | if (timeout) | |
6428 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6429 | |
4014d943 | 6430 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6431 | ret = -ECANCELED; |
31b51510 | 6432 | |
561fb04a | 6433 | if (!ret) { |
561fb04a | 6434 | do { |
889fca73 | 6435 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6436 | /* |
6437 | * We can get EAGAIN for polled IO even though we're | |
6438 | * forcing a sync submission from here, since we can't | |
6439 | * wait for request slots on the block side. | |
6440 | */ | |
6441 | if (ret != -EAGAIN) | |
6442 | break; | |
6443 | cond_resched(); | |
6444 | } while (1); | |
6445 | } | |
31b51510 | 6446 | |
a3df7698 | 6447 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6448 | if (ret) { |
a3df7698 PB |
6449 | /* io-wq is going to take one down */ |
6450 | refcount_inc(&req->refs); | |
6451 | io_req_task_queue_fail(req, ret); | |
edafccee | 6452 | } |
2b188cc1 JA |
6453 | } |
6454 | ||
65e19f54 JA |
6455 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6456 | int index) | |
6457 | { | |
269bbe5f | 6458 | struct fixed_rsrc_table *table; |
65e19f54 | 6459 | |
05f3fb3c | 6460 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6461 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6462 | } |
6463 | ||
8371adf5 PB |
6464 | static struct file *io_file_get(struct io_submit_state *state, |
6465 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6466 | { |
a197f664 | 6467 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6468 | struct file *file; |
09bb8394 | 6469 | |
8da11c19 | 6470 | if (fixed) { |
479f517b | 6471 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6472 | return NULL; |
b7620121 | 6473 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6474 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6475 | io_set_resource_node(req); |
09bb8394 | 6476 | } else { |
c826bd7a | 6477 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6478 | file = __io_file_get(state, fd); |
09bb8394 JA |
6479 | } |
6480 | ||
ce3d5aae PB |
6481 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6482 | io_req_track_inflight(req); | |
8371adf5 | 6483 | return file; |
09bb8394 JA |
6484 | } |
6485 | ||
2665abfd | 6486 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6487 | { |
ad8a48ac JA |
6488 | struct io_timeout_data *data = container_of(timer, |
6489 | struct io_timeout_data, timer); | |
90cd7e42 | 6490 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6491 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6492 | unsigned long flags; |
2665abfd JA |
6493 | |
6494 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6495 | prev = req->timeout.head; |
6496 | req->timeout.head = NULL; | |
2665abfd JA |
6497 | |
6498 | /* | |
6499 | * We don't expect the list to be empty, that will only happen if we | |
6500 | * race with the completion of the linked work. | |
6501 | */ | |
90cd7e42 | 6502 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6503 | io_remove_next_linked(prev); |
90cd7e42 PB |
6504 | else |
6505 | prev = NULL; | |
2665abfd JA |
6506 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6507 | ||
6508 | if (prev) { | |
4e88d6e7 | 6509 | req_set_fail_links(prev); |
014db007 | 6510 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6511 | io_put_req_deferred(prev, 1); |
47f46768 | 6512 | } else { |
9ae1f8dd PB |
6513 | io_req_complete_post(req, -ETIME, 0); |
6514 | io_put_req_deferred(req, 1); | |
2665abfd | 6515 | } |
2665abfd JA |
6516 | return HRTIMER_NORESTART; |
6517 | } | |
6518 | ||
7271ef3a | 6519 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6520 | { |
76a46e06 | 6521 | /* |
f2f87370 PB |
6522 | * If the back reference is NULL, then our linked request finished |
6523 | * before we got a chance to setup the timer | |
76a46e06 | 6524 | */ |
90cd7e42 | 6525 | if (req->timeout.head) { |
e8c2bc1f | 6526 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6527 | |
ad8a48ac JA |
6528 | data->timer.function = io_link_timeout_fn; |
6529 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6530 | data->mode); | |
2665abfd | 6531 | } |
7271ef3a JA |
6532 | } |
6533 | ||
6534 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6535 | { | |
6536 | struct io_ring_ctx *ctx = req->ctx; | |
6537 | ||
6538 | spin_lock_irq(&ctx->completion_lock); | |
6539 | __io_queue_linked_timeout(req); | |
76a46e06 | 6540 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6541 | |
2665abfd | 6542 | /* drop submission reference */ |
76a46e06 JA |
6543 | io_put_req(req); |
6544 | } | |
2665abfd | 6545 | |
ad8a48ac | 6546 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6547 | { |
f2f87370 | 6548 | struct io_kiocb *nxt = req->link; |
2665abfd | 6549 | |
f2f87370 PB |
6550 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6551 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6552 | return NULL; |
2665abfd | 6553 | |
90cd7e42 | 6554 | nxt->timeout.head = req; |
900fad45 | 6555 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6556 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6557 | return nxt; |
2665abfd JA |
6558 | } |
6559 | ||
c5eef2b9 | 6560 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6561 | { |
d3d7298d | 6562 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
193155c8 | 6563 | const struct cred *old_creds = NULL; |
e0c5c576 | 6564 | int ret; |
2b188cc1 | 6565 | |
2e5aa6cb PB |
6566 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6567 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
d3d7298d PB |
6568 | req->work.identity->creds != current_cred()) |
6569 | old_creds = override_creds(req->work.identity->creds); | |
193155c8 | 6570 | |
c5eef2b9 | 6571 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6572 | |
d3d7298d PB |
6573 | if (old_creds) |
6574 | revert_creds(old_creds); | |
491381ce JA |
6575 | |
6576 | /* | |
6577 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6578 | * doesn't support non-blocking read/write attempts | |
6579 | */ | |
24c74678 | 6580 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6581 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6582 | /* |
6583 | * Queued up for async execution, worker will release | |
6584 | * submit reference when the iocb is actually submitted. | |
6585 | */ | |
6586 | io_queue_async_work(req); | |
2b188cc1 | 6587 | } |
0d63c148 PB |
6588 | } else if (likely(!ret)) { |
6589 | /* drop submission reference */ | |
e342c807 | 6590 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6591 | struct io_ring_ctx *ctx = req->ctx; |
6592 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6593 | |
6dd0be1e | 6594 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6595 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6596 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6597 | } else { |
d3d7298d | 6598 | io_put_req(req); |
0d63c148 PB |
6599 | } |
6600 | } else { | |
4e88d6e7 | 6601 | req_set_fail_links(req); |
e65ef56d | 6602 | io_put_req(req); |
e1e16097 | 6603 | io_req_complete(req, ret); |
9e645e11 | 6604 | } |
d3d7298d PB |
6605 | if (linked_timeout) |
6606 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6607 | } |
6608 | ||
be7053b7 | 6609 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6610 | { |
6611 | int ret; | |
6612 | ||
be7053b7 | 6613 | ret = io_req_defer(req); |
4fe2c963 JL |
6614 | if (ret) { |
6615 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6616 | fail_req: |
4e88d6e7 | 6617 | req_set_fail_links(req); |
e1e16097 JA |
6618 | io_put_req(req); |
6619 | io_req_complete(req, ret); | |
4fe2c963 | 6620 | } |
2550878f | 6621 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6622 | ret = io_req_defer_prep(req); |
6623 | if (unlikely(ret)) | |
6624 | goto fail_req; | |
ce35a47a JA |
6625 | io_queue_async_work(req); |
6626 | } else { | |
c5eef2b9 | 6627 | __io_queue_sqe(req); |
ce35a47a | 6628 | } |
4fe2c963 JL |
6629 | } |
6630 | ||
b16fed66 PB |
6631 | /* |
6632 | * Check SQE restrictions (opcode and flags). | |
6633 | * | |
6634 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6635 | */ | |
6636 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6637 | struct io_kiocb *req, | |
6638 | unsigned int sqe_flags) | |
4fe2c963 | 6639 | { |
b16fed66 PB |
6640 | if (!ctx->restricted) |
6641 | return true; | |
6642 | ||
6643 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6644 | return false; | |
6645 | ||
6646 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6647 | ctx->restrictions.sqe_flags_required) | |
6648 | return false; | |
6649 | ||
6650 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6651 | ctx->restrictions.sqe_flags_required)) | |
6652 | return false; | |
6653 | ||
6654 | return true; | |
4fe2c963 JL |
6655 | } |
6656 | ||
b16fed66 PB |
6657 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6658 | const struct io_uring_sqe *sqe) | |
6659 | { | |
6660 | struct io_submit_state *state; | |
6661 | unsigned int sqe_flags; | |
6662 | int id, ret = 0; | |
6663 | ||
6664 | req->opcode = READ_ONCE(sqe->opcode); | |
6665 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6666 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6667 | req->user_data = READ_ONCE(sqe->user_data); | |
6668 | req->async_data = NULL; | |
6669 | req->file = NULL; | |
6670 | req->ctx = ctx; | |
6671 | req->link = NULL; | |
6672 | req->fixed_rsrc_refs = NULL; | |
6673 | /* one is dropped after submission, the other at completion */ | |
6674 | refcount_set(&req->refs, 2); | |
6675 | req->task = current; | |
6676 | req->result = 0; | |
6677 | ||
6678 | /* enforce forwards compatibility on users */ | |
ebf4a5db PB |
6679 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6680 | req->flags = 0; | |
b16fed66 | 6681 | return -EINVAL; |
ebf4a5db | 6682 | } |
b16fed66 PB |
6683 | |
6684 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6685 | return -EINVAL; | |
6686 | ||
6687 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) | |
6688 | return -EFAULT; | |
6689 | ||
6690 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) | |
6691 | return -EACCES; | |
6692 | ||
6693 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6694 | !io_op_defs[req->opcode].buffer_select) | |
6695 | return -EOPNOTSUPP; | |
863e0560 | 6696 | |
b16fed66 PB |
6697 | id = READ_ONCE(sqe->personality); |
6698 | if (id) { | |
6699 | struct io_identity *iod; | |
6700 | ||
6701 | iod = idr_find(&ctx->personality_idr, id); | |
6702 | if (unlikely(!iod)) | |
6703 | return -EINVAL; | |
6704 | refcount_inc(&iod->count); | |
6705 | ||
6706 | __io_req_init_async(req); | |
6707 | get_cred(iod->creds); | |
6708 | req->work.identity = iod; | |
6709 | req->work.flags |= IO_WQ_WORK_CREDS; | |
6710 | } | |
6711 | ||
6712 | state = &ctx->submit_state; | |
6713 | ||
6714 | /* | |
6715 | * Plug now if we have more than 1 IO left after this, and the target | |
6716 | * is potentially a read/write to block based storage. | |
6717 | */ | |
6718 | if (!state->plug_started && state->ios_left > 1 && | |
6719 | io_op_defs[req->opcode].plug) { | |
6720 | blk_start_plug(&state->plug); | |
6721 | state->plug_started = true; | |
6722 | } | |
6723 | ||
6724 | if (io_op_defs[req->opcode].needs_file) { | |
6725 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6726 | ||
6727 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6728 | if (unlikely(!req->file)) | |
6729 | ret = -EBADF; | |
6730 | } | |
6731 | ||
6732 | state->ios_left--; | |
6733 | return ret; | |
6734 | } | |
6735 | ||
a6b8cadc | 6736 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6737 | const struct io_uring_sqe *sqe) |
9e645e11 | 6738 | { |
a1ab7b35 | 6739 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6740 | int ret; |
9e645e11 | 6741 | |
a6b8cadc PB |
6742 | ret = io_init_req(ctx, req, sqe); |
6743 | if (unlikely(ret)) { | |
6744 | fail_req: | |
6745 | io_put_req(req); | |
6746 | io_req_complete(req, ret); | |
de59bc10 PB |
6747 | if (link->head) { |
6748 | /* fail even hard links since we don't submit */ | |
cf109604 | 6749 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6750 | io_put_req(link->head); |
6751 | io_req_complete(link->head, -ECANCELED); | |
6752 | link->head = NULL; | |
6753 | } | |
a6b8cadc PB |
6754 | return ret; |
6755 | } | |
be7053b7 PB |
6756 | ret = io_req_prep(req, sqe); |
6757 | if (unlikely(ret)) | |
6758 | goto fail_req; | |
a6b8cadc | 6759 | |
be7053b7 | 6760 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6761 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6762 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6763 | ||
9e645e11 JA |
6764 | /* |
6765 | * If we already have a head request, queue this one for async | |
6766 | * submittal once the head completes. If we don't have a head but | |
6767 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6768 | * submitted sync once the chain is complete. If none of those | |
6769 | * conditions are true (normal request), then just queue it. | |
6770 | */ | |
863e0560 PB |
6771 | if (link->head) { |
6772 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6773 | |
8cdf2193 PB |
6774 | /* |
6775 | * Taking sequential execution of a link, draining both sides | |
6776 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6777 | * requests in the link. So, it drains the head and the | |
6778 | * next after the link request. The last one is done via | |
6779 | * drain_next flag to persist the effect across calls. | |
6780 | */ | |
ef4ff581 | 6781 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6782 | head->flags |= REQ_F_IO_DRAIN; |
6783 | ctx->drain_next = 1; | |
6784 | } | |
be7053b7 | 6785 | ret = io_req_defer_prep(req); |
cf109604 | 6786 | if (unlikely(ret)) |
a6b8cadc | 6787 | goto fail_req; |
9d76377f | 6788 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6789 | link->last->link = req; |
863e0560 | 6790 | link->last = req; |
32fe525b PB |
6791 | |
6792 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6793 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6794 | io_queue_sqe(head); |
863e0560 | 6795 | link->head = NULL; |
32fe525b | 6796 | } |
9e645e11 | 6797 | } else { |
711be031 PB |
6798 | if (unlikely(ctx->drain_next)) { |
6799 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6800 | ctx->drain_next = 0; |
711be031 | 6801 | } |
ef4ff581 | 6802 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6803 | link->head = req; |
6804 | link->last = req; | |
711be031 | 6805 | } else { |
be7053b7 | 6806 | io_queue_sqe(req); |
711be031 | 6807 | } |
9e645e11 | 6808 | } |
2e6e1fde | 6809 | |
1d4240cc | 6810 | return 0; |
9e645e11 JA |
6811 | } |
6812 | ||
9a56a232 JA |
6813 | /* |
6814 | * Batched submission is done, ensure local IO is flushed out. | |
6815 | */ | |
ba88ff11 PB |
6816 | static void io_submit_state_end(struct io_submit_state *state, |
6817 | struct io_ring_ctx *ctx) | |
9a56a232 | 6818 | { |
a1ab7b35 | 6819 | if (state->link.head) |
de59bc10 | 6820 | io_queue_sqe(state->link.head); |
6dd0be1e | 6821 | if (state->comp.nr) |
ba88ff11 | 6822 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6823 | if (state->plug_started) |
6824 | blk_finish_plug(&state->plug); | |
9f13c35b | 6825 | io_state_file_put(state); |
9a56a232 JA |
6826 | } |
6827 | ||
6828 | /* | |
6829 | * Start submission side cache. | |
6830 | */ | |
6831 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6832 | unsigned int max_ios) |
9a56a232 | 6833 | { |
27926b68 | 6834 | state->plug_started = false; |
9a56a232 | 6835 | state->ios_left = max_ios; |
a1ab7b35 PB |
6836 | /* set only head, no need to init link_last in advance */ |
6837 | state->link.head = NULL; | |
9a56a232 JA |
6838 | } |
6839 | ||
2b188cc1 JA |
6840 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6841 | { | |
75b28aff | 6842 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6843 | |
caf582c6 PB |
6844 | /* |
6845 | * Ensure any loads from the SQEs are done at this point, | |
6846 | * since once we write the new head, the application could | |
6847 | * write new data to them. | |
6848 | */ | |
6849 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6850 | } |
6851 | ||
2b188cc1 | 6852 | /* |
3529d8c2 | 6853 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6854 | * that is mapped by userspace. This means that care needs to be taken to |
6855 | * ensure that reads are stable, as we cannot rely on userspace always | |
6856 | * being a good citizen. If members of the sqe are validated and then later | |
6857 | * used, it's important that those reads are done through READ_ONCE() to | |
6858 | * prevent a re-load down the line. | |
6859 | */ | |
709b302f | 6860 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6861 | { |
75b28aff | 6862 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6863 | unsigned head; |
6864 | ||
6865 | /* | |
6866 | * The cached sq head (or cq tail) serves two purposes: | |
6867 | * | |
6868 | * 1) allows us to batch the cost of updating the user visible | |
6869 | * head updates. | |
6870 | * 2) allows the kernel side to track the head on its own, even | |
6871 | * though the application is the one updating it. | |
6872 | */ | |
4fccfcbb | 6873 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6874 | if (likely(head < ctx->sq_entries)) |
6875 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6876 | |
6877 | /* drop invalid entries */ | |
498ccd9e | 6878 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6879 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6880 | return NULL; |
6881 | } | |
6882 | ||
0f212204 | 6883 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6884 | { |
46c4e16a | 6885 | int submitted = 0; |
6c271ce2 | 6886 | |
c4a2ed72 | 6887 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6888 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6889 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6890 | return -EBUSY; |
6891 | } | |
6c271ce2 | 6892 | |
ee7d46d9 PB |
6893 | /* make sure SQ entry isn't read before tail */ |
6894 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6895 | |
2b85edfc PB |
6896 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6897 | return -EAGAIN; | |
6c271ce2 | 6898 | |
d8a6df10 | 6899 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6900 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6901 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6902 | |
46c4e16a | 6903 | while (submitted < nr) { |
3529d8c2 | 6904 | const struct io_uring_sqe *sqe; |
196be95c | 6905 | struct io_kiocb *req; |
fb5ccc98 | 6906 | |
258b29a9 | 6907 | req = io_alloc_req(ctx); |
196be95c PB |
6908 | if (unlikely(!req)) { |
6909 | if (!submitted) | |
6910 | submitted = -EAGAIN; | |
fb5ccc98 | 6911 | break; |
196be95c | 6912 | } |
4fccfcbb PB |
6913 | sqe = io_get_sqe(ctx); |
6914 | if (unlikely(!sqe)) { | |
6915 | kmem_cache_free(req_cachep, req); | |
6916 | break; | |
6917 | } | |
d3656344 JA |
6918 | /* will complete beyond this point, count as submitted */ |
6919 | submitted++; | |
a1ab7b35 | 6920 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6921 | break; |
6c271ce2 JA |
6922 | } |
6923 | ||
9466f437 PB |
6924 | if (unlikely(submitted != nr)) { |
6925 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6926 | struct io_uring_task *tctx = current->io_uring; |
6927 | int unused = nr - ref_used; | |
9466f437 | 6928 | |
d8a6df10 JA |
6929 | percpu_ref_put_many(&ctx->refs, unused); |
6930 | percpu_counter_sub(&tctx->inflight, unused); | |
6931 | put_task_struct_many(current, unused); | |
9466f437 | 6932 | } |
6c271ce2 | 6933 | |
a1ab7b35 | 6934 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6935 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6936 | io_commit_sqring(ctx); | |
6937 | ||
6c271ce2 JA |
6938 | return submitted; |
6939 | } | |
6940 | ||
23b3628e XW |
6941 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6942 | { | |
6943 | /* Tell userspace we may need a wakeup call */ | |
6944 | spin_lock_irq(&ctx->completion_lock); | |
6945 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6946 | spin_unlock_irq(&ctx->completion_lock); | |
6947 | } | |
6948 | ||
6949 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6950 | { | |
6951 | spin_lock_irq(&ctx->completion_lock); | |
6952 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6953 | spin_unlock_irq(&ctx->completion_lock); | |
6954 | } | |
6955 | ||
08369246 | 6956 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6957 | { |
c8d1ba58 | 6958 | unsigned int to_submit; |
bdcd3eab | 6959 | int ret = 0; |
6c271ce2 | 6960 | |
c8d1ba58 | 6961 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6962 | /* if we're handling multiple rings, cap submit size for fairness */ |
6963 | if (cap_entries && to_submit > 8) | |
6964 | to_submit = 8; | |
6965 | ||
906a3c6f | 6966 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6967 | unsigned nr_events = 0; |
a4c0b3de | 6968 | |
c8d1ba58 | 6969 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6970 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6971 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6972 | |
d9d05217 PB |
6973 | if (to_submit && !ctx->sqo_dead && |
6974 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6975 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6976 | mutex_unlock(&ctx->uring_lock); |
6977 | } | |
6c271ce2 | 6978 | |
90554200 JA |
6979 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6980 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6981 | |
08369246 XW |
6982 | return ret; |
6983 | } | |
6c271ce2 | 6984 | |
08369246 XW |
6985 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6986 | { | |
6987 | struct io_ring_ctx *ctx; | |
6988 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6989 | |
08369246 XW |
6990 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6991 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6992 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6993 | } |
c1edbf5f | 6994 | |
08369246 | 6995 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6996 | } |
6c271ce2 | 6997 | |
69fb2131 JA |
6998 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6999 | { | |
7000 | struct io_ring_ctx *ctx; | |
7001 | ||
7002 | while (!list_empty(&sqd->ctx_new_list)) { | |
7003 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
7004 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
7005 | complete(&ctx->sq_thread_comp); | |
7006 | } | |
08369246 XW |
7007 | |
7008 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
7009 | } |
7010 | ||
c8d1ba58 JA |
7011 | static int io_sq_thread(void *data) |
7012 | { | |
91d8f519 | 7013 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
7014 | struct files_struct *old_files = current->files; |
7015 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
7016 | const struct cred *old_cred = NULL; |
7017 | struct io_sq_data *sqd = data; | |
7018 | struct io_ring_ctx *ctx; | |
a0d9205f | 7019 | unsigned long timeout = 0; |
08369246 | 7020 | DEFINE_WAIT(wait); |
6c271ce2 | 7021 | |
28cea78a JA |
7022 | task_lock(current); |
7023 | current->files = NULL; | |
7024 | current->nsproxy = NULL; | |
7025 | task_unlock(current); | |
6c271ce2 | 7026 | |
69fb2131 | 7027 | while (!kthread_should_stop()) { |
08369246 XW |
7028 | int ret; |
7029 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
7030 | |
7031 | /* | |
69fb2131 JA |
7032 | * Any changes to the sqd lists are synchronized through the |
7033 | * kthread parking. This synchronizes the thread vs users, | |
7034 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7035 | */ |
65b2b213 | 7036 | if (kthread_should_park()) { |
69fb2131 | 7037 | kthread_parkme(); |
65b2b213 XW |
7038 | /* |
7039 | * When sq thread is unparked, in case the previous park operation | |
7040 | * comes from io_put_sq_data(), which means that sq thread is going | |
7041 | * to be stopped, so here needs to have a check. | |
7042 | */ | |
7043 | if (kthread_should_stop()) | |
7044 | break; | |
7045 | } | |
7143b5ac | 7046 | |
08369246 | 7047 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7048 | io_sqd_init_new(sqd); |
08369246 XW |
7049 | timeout = jiffies + sqd->sq_thread_idle; |
7050 | } | |
6c271ce2 | 7051 | |
08369246 | 7052 | sqt_spin = false; |
e95eee2d | 7053 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7054 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7055 | if (current->cred != ctx->creds) { | |
7056 | if (old_cred) | |
7057 | revert_creds(old_cred); | |
7058 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7059 | } |
91d8f519 | 7060 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7061 | #ifdef CONFIG_AUDIT |
7062 | current->loginuid = ctx->loginuid; | |
7063 | current->sessionid = ctx->sessionid; | |
7064 | #endif | |
bdcd3eab | 7065 | |
08369246 XW |
7066 | ret = __io_sq_thread(ctx, cap_entries); |
7067 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7068 | sqt_spin = true; | |
6c271ce2 | 7069 | |
28cea78a | 7070 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7071 | } |
6c271ce2 | 7072 | |
08369246 | 7073 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7074 | io_run_task_work(); |
d434ab6d | 7075 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7076 | cond_resched(); |
08369246 XW |
7077 | if (sqt_spin) |
7078 | timeout = jiffies + sqd->sq_thread_idle; | |
7079 | continue; | |
7080 | } | |
7081 | ||
08369246 XW |
7082 | needs_sched = true; |
7083 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7084 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7085 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7086 | !list_empty_careful(&ctx->iopoll_list)) { | |
7087 | needs_sched = false; | |
7088 | break; | |
7089 | } | |
7090 | if (io_sqring_entries(ctx)) { | |
7091 | needs_sched = false; | |
7092 | break; | |
7093 | } | |
7094 | } | |
7095 | ||
8b28fdf2 | 7096 | if (needs_sched && !kthread_should_park()) { |
69fb2131 JA |
7097 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7098 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7099 | |
69fb2131 | 7100 | schedule(); |
69fb2131 JA |
7101 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7102 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7103 | } |
08369246 XW |
7104 | |
7105 | finish_wait(&sqd->wait, &wait); | |
7106 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7107 | } |
7108 | ||
4c6e277c | 7109 | io_run_task_work(); |
d434ab6d | 7110 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7111 | |
91d8f519 DZ |
7112 | if (cur_css) |
7113 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7114 | if (old_cred) |
7115 | revert_creds(old_cred); | |
06058632 | 7116 | |
28cea78a JA |
7117 | task_lock(current); |
7118 | current->files = old_files; | |
7119 | current->nsproxy = old_nsproxy; | |
7120 | task_unlock(current); | |
7121 | ||
2bbcd6d3 | 7122 | kthread_parkme(); |
06058632 | 7123 | |
6c271ce2 JA |
7124 | return 0; |
7125 | } | |
7126 | ||
bda52162 JA |
7127 | struct io_wait_queue { |
7128 | struct wait_queue_entry wq; | |
7129 | struct io_ring_ctx *ctx; | |
7130 | unsigned to_wait; | |
7131 | unsigned nr_timeouts; | |
7132 | }; | |
7133 | ||
6c503150 | 7134 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7135 | { |
7136 | struct io_ring_ctx *ctx = iowq->ctx; | |
7137 | ||
7138 | /* | |
d195a66e | 7139 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7140 | * started waiting. For timeouts, we always want to return to userspace, |
7141 | * regardless of event count. | |
7142 | */ | |
6c503150 | 7143 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7144 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7145 | } | |
7146 | ||
7147 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7148 | int wake_flags, void *key) | |
7149 | { | |
7150 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7151 | wq); | |
7152 | ||
6c503150 PB |
7153 | /* |
7154 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7155 | * the task, and the next invocation will do it. | |
7156 | */ | |
7157 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7158 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7159 | return -1; | |
bda52162 JA |
7160 | } |
7161 | ||
af9c1a44 JA |
7162 | static int io_run_task_work_sig(void) |
7163 | { | |
7164 | if (io_run_task_work()) | |
7165 | return 1; | |
7166 | if (!signal_pending(current)) | |
7167 | return 0; | |
792ee0f6 JA |
7168 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7169 | return -ERESTARTSYS; | |
af9c1a44 JA |
7170 | return -EINTR; |
7171 | } | |
7172 | ||
eeb60b9a PB |
7173 | /* when returns >0, the caller should retry */ |
7174 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7175 | struct io_wait_queue *iowq, | |
7176 | signed long *timeout) | |
7177 | { | |
7178 | int ret; | |
7179 | ||
7180 | /* make sure we run task_work before checking for signals */ | |
7181 | ret = io_run_task_work_sig(); | |
7182 | if (ret || io_should_wake(iowq)) | |
7183 | return ret; | |
7184 | /* let the caller flush overflows, retry */ | |
7185 | if (test_bit(0, &ctx->cq_check_overflow)) | |
7186 | return 1; | |
7187 | ||
7188 | *timeout = schedule_timeout(*timeout); | |
7189 | return !*timeout ? -ETIME : 1; | |
7190 | } | |
7191 | ||
2b188cc1 JA |
7192 | /* |
7193 | * Wait until events become available, if we don't already have some. The | |
7194 | * application must reap them itself, as they reside on the shared cq ring. | |
7195 | */ | |
7196 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7197 | const sigset_t __user *sig, size_t sigsz, |
7198 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7199 | { |
bda52162 JA |
7200 | struct io_wait_queue iowq = { |
7201 | .wq = { | |
7202 | .private = current, | |
7203 | .func = io_wake_function, | |
7204 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7205 | }, | |
7206 | .ctx = ctx, | |
7207 | .to_wait = min_events, | |
7208 | }; | |
75b28aff | 7209 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7210 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7211 | int ret; | |
2b188cc1 | 7212 | |
b41e9852 | 7213 | do { |
6c503150 PB |
7214 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7215 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7216 | return 0; |
4c6e277c | 7217 | if (!io_run_task_work()) |
b41e9852 | 7218 | break; |
b41e9852 | 7219 | } while (1); |
2b188cc1 JA |
7220 | |
7221 | if (sig) { | |
9e75ad5d AB |
7222 | #ifdef CONFIG_COMPAT |
7223 | if (in_compat_syscall()) | |
7224 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7225 | sigsz); |
9e75ad5d AB |
7226 | else |
7227 | #endif | |
b772434b | 7228 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7229 | |
2b188cc1 JA |
7230 | if (ret) |
7231 | return ret; | |
7232 | } | |
7233 | ||
c73ebb68 | 7234 | if (uts) { |
c1d5a224 PB |
7235 | struct timespec64 ts; |
7236 | ||
c73ebb68 HX |
7237 | if (get_timespec64(&ts, uts)) |
7238 | return -EFAULT; | |
7239 | timeout = timespec64_to_jiffies(&ts); | |
7240 | } | |
7241 | ||
bda52162 | 7242 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7243 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7244 | do { |
6c503150 | 7245 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7246 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7247 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
7248 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
7249 | finish_wait(&ctx->wait, &iowq.wq); | |
7250 | } while (ret > 0); | |
bda52162 | 7251 | |
b7db41c9 | 7252 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7253 | |
75b28aff | 7254 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7255 | } |
7256 | ||
6b06314c JA |
7257 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7258 | { | |
7259 | #if defined(CONFIG_UNIX) | |
7260 | if (ctx->ring_sock) { | |
7261 | struct sock *sock = ctx->ring_sock->sk; | |
7262 | struct sk_buff *skb; | |
7263 | ||
7264 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7265 | kfree_skb(skb); | |
7266 | } | |
7267 | #else | |
7268 | int i; | |
7269 | ||
65e19f54 JA |
7270 | for (i = 0; i < ctx->nr_user_files; i++) { |
7271 | struct file *file; | |
7272 | ||
7273 | file = io_file_from_index(ctx, i); | |
7274 | if (file) | |
7275 | fput(file); | |
7276 | } | |
6b06314c JA |
7277 | #endif |
7278 | } | |
7279 | ||
00835dce | 7280 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7281 | { |
269bbe5f | 7282 | struct fixed_rsrc_data *data; |
05f3fb3c | 7283 | |
269bbe5f | 7284 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7285 | complete(&data->done); |
7286 | } | |
7287 | ||
2a63b2d9 | 7288 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 7289 | { |
2a63b2d9 | 7290 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
7291 | } |
7292 | ||
2a63b2d9 | 7293 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 7294 | { |
2a63b2d9 BM |
7295 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
7296 | } | |
65e19f54 | 7297 | |
d67d2263 BM |
7298 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7299 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7300 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7301 | { |
2a63b2d9 | 7302 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7303 | rsrc_data->node = ref_node; |
d67d2263 | 7304 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7305 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7306 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7307 | } |
7308 | ||
8bad28d8 | 7309 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 7310 | { |
8bad28d8 | 7311 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 7312 | |
2a63b2d9 | 7313 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7314 | ref_node = data->node; |
e6cb007c | 7315 | data->node = NULL; |
2a63b2d9 | 7316 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7317 | if (ref_node) |
7318 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 HX |
7319 | } |
7320 | ||
7321 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, | |
7322 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
7323 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
7324 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 7325 | { |
f2303b1f | 7326 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 7327 | int ret; |
05589553 | 7328 | |
8bad28d8 HX |
7329 | if (data->quiesce) |
7330 | return -ENXIO; | |
05589553 | 7331 | |
8bad28d8 | 7332 | data->quiesce = true; |
1ffc5422 | 7333 | do { |
f2303b1f PB |
7334 | ret = -ENOMEM; |
7335 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7336 | if (!backup_node) | |
7337 | break; | |
7338 | backup_node->rsrc_data = data; | |
7339 | backup_node->rsrc_put = rsrc_put; | |
7340 | ||
8bad28d8 HX |
7341 | io_sqe_rsrc_kill_node(ctx, data); |
7342 | percpu_ref_kill(&data->refs); | |
7343 | flush_delayed_work(&ctx->rsrc_put_work); | |
7344 | ||
1ffc5422 PB |
7345 | ret = wait_for_completion_interruptible(&data->done); |
7346 | if (!ret) | |
7347 | break; | |
8bad28d8 | 7348 | |
cb5e1b81 | 7349 | percpu_ref_resurrect(&data->refs); |
8bad28d8 HX |
7350 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7351 | backup_node = NULL; | |
cb5e1b81 | 7352 | reinit_completion(&data->done); |
8bad28d8 | 7353 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7354 | ret = io_run_task_work_sig(); |
8bad28d8 | 7355 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7356 | } while (ret >= 0); |
8bad28d8 | 7357 | data->quiesce = false; |
05f3fb3c | 7358 | |
8bad28d8 HX |
7359 | if (backup_node) |
7360 | destroy_fixed_rsrc_ref_node(backup_node); | |
7361 | return ret; | |
d7954b2b BM |
7362 | } |
7363 | ||
1ad555c6 BM |
7364 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7365 | { | |
7366 | struct fixed_rsrc_data *data; | |
7367 | ||
7368 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7369 | if (!data) | |
7370 | return NULL; | |
7371 | ||
00835dce | 7372 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7373 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7374 | kfree(data); | |
7375 | return NULL; | |
7376 | } | |
7377 | data->ctx = ctx; | |
7378 | init_completion(&data->done); | |
7379 | return data; | |
7380 | } | |
7381 | ||
7382 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7383 | { | |
7384 | percpu_ref_exit(&data->refs); | |
7385 | kfree(data->table); | |
7386 | kfree(data); | |
7387 | } | |
7388 | ||
d7954b2b BM |
7389 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7390 | { | |
7391 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7392 | unsigned nr_tables, i; |
7393 | int ret; | |
7394 | ||
8bad28d8 HX |
7395 | /* |
7396 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7397 | * Since we possibly drop uring lock later in this function to | |
7398 | * run task work. | |
7399 | */ | |
7400 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7401 | return -ENXIO; |
f2303b1f | 7402 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7403 | if (ret) |
7404 | return ret; | |
7405 | ||
6b06314c | 7406 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7407 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7408 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7409 | kfree(data->table[i].files); |
1ad555c6 | 7410 | free_fixed_rsrc_data(data); |
05f3fb3c | 7411 | ctx->file_data = NULL; |
6b06314c JA |
7412 | ctx->nr_user_files = 0; |
7413 | return 0; | |
7414 | } | |
7415 | ||
534ca6d6 | 7416 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7417 | { |
534ca6d6 | 7418 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7419 | /* |
7420 | * The park is a bit of a work-around, without it we get | |
7421 | * warning spews on shutdown with SQPOLL set and affinity | |
7422 | * set to a single CPU. | |
7423 | */ | |
534ca6d6 JA |
7424 | if (sqd->thread) { |
7425 | kthread_park(sqd->thread); | |
7426 | kthread_stop(sqd->thread); | |
7427 | } | |
7428 | ||
7429 | kfree(sqd); | |
7430 | } | |
7431 | } | |
7432 | ||
aa06165d JA |
7433 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7434 | { | |
7435 | struct io_ring_ctx *ctx_attach; | |
7436 | struct io_sq_data *sqd; | |
7437 | struct fd f; | |
7438 | ||
7439 | f = fdget(p->wq_fd); | |
7440 | if (!f.file) | |
7441 | return ERR_PTR(-ENXIO); | |
7442 | if (f.file->f_op != &io_uring_fops) { | |
7443 | fdput(f); | |
7444 | return ERR_PTR(-EINVAL); | |
7445 | } | |
7446 | ||
7447 | ctx_attach = f.file->private_data; | |
7448 | sqd = ctx_attach->sq_data; | |
7449 | if (!sqd) { | |
7450 | fdput(f); | |
7451 | return ERR_PTR(-EINVAL); | |
7452 | } | |
7453 | ||
7454 | refcount_inc(&sqd->refs); | |
7455 | fdput(f); | |
7456 | return sqd; | |
7457 | } | |
7458 | ||
534ca6d6 JA |
7459 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7460 | { | |
7461 | struct io_sq_data *sqd; | |
7462 | ||
aa06165d JA |
7463 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7464 | return io_attach_sq_data(p); | |
7465 | ||
534ca6d6 JA |
7466 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7467 | if (!sqd) | |
7468 | return ERR_PTR(-ENOMEM); | |
7469 | ||
7470 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7471 | INIT_LIST_HEAD(&sqd->ctx_list); |
7472 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7473 | mutex_init(&sqd->ctx_lock); | |
7474 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7475 | init_waitqueue_head(&sqd->wait); |
7476 | return sqd; | |
7477 | } | |
7478 | ||
69fb2131 JA |
7479 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7480 | __releases(&sqd->lock) | |
7481 | { | |
7482 | if (!sqd->thread) | |
7483 | return; | |
7484 | kthread_unpark(sqd->thread); | |
7485 | mutex_unlock(&sqd->lock); | |
7486 | } | |
7487 | ||
7488 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7489 | __acquires(&sqd->lock) | |
7490 | { | |
7491 | if (!sqd->thread) | |
7492 | return; | |
7493 | mutex_lock(&sqd->lock); | |
7494 | kthread_park(sqd->thread); | |
7495 | } | |
7496 | ||
534ca6d6 JA |
7497 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7498 | { | |
7499 | struct io_sq_data *sqd = ctx->sq_data; | |
7500 | ||
7501 | if (sqd) { | |
7502 | if (sqd->thread) { | |
7503 | /* | |
7504 | * We may arrive here from the error branch in | |
7505 | * io_sq_offload_create() where the kthread is created | |
7506 | * without being waked up, thus wake it up now to make | |
7507 | * sure the wait will complete. | |
7508 | */ | |
7509 | wake_up_process(sqd->thread); | |
7510 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7511 | |
7512 | io_sq_thread_park(sqd); | |
7513 | } | |
7514 | ||
7515 | mutex_lock(&sqd->ctx_lock); | |
7516 | list_del(&ctx->sqd_list); | |
08369246 | 7517 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7518 | mutex_unlock(&sqd->ctx_lock); |
7519 | ||
08369246 | 7520 | if (sqd->thread) |
69fb2131 | 7521 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7522 | |
7523 | io_put_sq_data(sqd); | |
7524 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7525 | } |
7526 | } | |
7527 | ||
6b06314c JA |
7528 | static void io_finish_async(struct io_ring_ctx *ctx) |
7529 | { | |
6c271ce2 JA |
7530 | io_sq_thread_stop(ctx); |
7531 | ||
561fb04a JA |
7532 | if (ctx->io_wq) { |
7533 | io_wq_destroy(ctx->io_wq); | |
7534 | ctx->io_wq = NULL; | |
6b06314c JA |
7535 | } |
7536 | } | |
7537 | ||
7538 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7539 | /* |
7540 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7541 | * the io_uring can be safely unregistered on process exit, even if we have | |
7542 | * loops in the file referencing. | |
7543 | */ | |
7544 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7545 | { | |
7546 | struct sock *sk = ctx->ring_sock->sk; | |
7547 | struct scm_fp_list *fpl; | |
7548 | struct sk_buff *skb; | |
08a45173 | 7549 | int i, nr_files; |
6b06314c | 7550 | |
6b06314c JA |
7551 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7552 | if (!fpl) | |
7553 | return -ENOMEM; | |
7554 | ||
7555 | skb = alloc_skb(0, GFP_KERNEL); | |
7556 | if (!skb) { | |
7557 | kfree(fpl); | |
7558 | return -ENOMEM; | |
7559 | } | |
7560 | ||
7561 | skb->sk = sk; | |
6b06314c | 7562 | |
08a45173 | 7563 | nr_files = 0; |
6b06314c JA |
7564 | fpl->user = get_uid(ctx->user); |
7565 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7566 | struct file *file = io_file_from_index(ctx, i + offset); |
7567 | ||
7568 | if (!file) | |
08a45173 | 7569 | continue; |
65e19f54 | 7570 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7571 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7572 | nr_files++; | |
6b06314c JA |
7573 | } |
7574 | ||
08a45173 JA |
7575 | if (nr_files) { |
7576 | fpl->max = SCM_MAX_FD; | |
7577 | fpl->count = nr_files; | |
7578 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7579 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7580 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7581 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7582 | |
08a45173 JA |
7583 | for (i = 0; i < nr_files; i++) |
7584 | fput(fpl->fp[i]); | |
7585 | } else { | |
7586 | kfree_skb(skb); | |
7587 | kfree(fpl); | |
7588 | } | |
6b06314c JA |
7589 | |
7590 | return 0; | |
7591 | } | |
7592 | ||
7593 | /* | |
7594 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7595 | * causes regular reference counting to break down. We rely on the UNIX | |
7596 | * garbage collection to take care of this problem for us. | |
7597 | */ | |
7598 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7599 | { | |
7600 | unsigned left, total; | |
7601 | int ret = 0; | |
7602 | ||
7603 | total = 0; | |
7604 | left = ctx->nr_user_files; | |
7605 | while (left) { | |
7606 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7607 | |
7608 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7609 | if (ret) | |
7610 | break; | |
7611 | left -= this_files; | |
7612 | total += this_files; | |
7613 | } | |
7614 | ||
7615 | if (!ret) | |
7616 | return 0; | |
7617 | ||
7618 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7619 | struct file *file = io_file_from_index(ctx, total); |
7620 | ||
7621 | if (file) | |
7622 | fput(file); | |
6b06314c JA |
7623 | total++; |
7624 | } | |
7625 | ||
7626 | return ret; | |
7627 | } | |
7628 | #else | |
7629 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7630 | { | |
7631 | return 0; | |
7632 | } | |
7633 | #endif | |
7634 | ||
269bbe5f | 7635 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7636 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7637 | { |
7638 | int i; | |
7639 | ||
7640 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7641 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7642 | unsigned this_files; |
7643 | ||
7644 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7645 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7646 | GFP_KERNEL); | |
7647 | if (!table->files) | |
7648 | break; | |
7649 | nr_files -= this_files; | |
7650 | } | |
7651 | ||
7652 | if (i == nr_tables) | |
7653 | return 0; | |
7654 | ||
7655 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7656 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7657 | kfree(table->files); |
7658 | } | |
7659 | return 1; | |
7660 | } | |
7661 | ||
50238531 | 7662 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7663 | { |
50238531 | 7664 | struct file *file = prsrc->file; |
05f3fb3c JA |
7665 | #if defined(CONFIG_UNIX) |
7666 | struct sock *sock = ctx->ring_sock->sk; | |
7667 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7668 | struct sk_buff *skb; | |
7669 | int i; | |
7670 | ||
7671 | __skb_queue_head_init(&list); | |
7672 | ||
7673 | /* | |
7674 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7675 | * remove this entry and rearrange the file array. | |
7676 | */ | |
7677 | skb = skb_dequeue(head); | |
7678 | while (skb) { | |
7679 | struct scm_fp_list *fp; | |
7680 | ||
7681 | fp = UNIXCB(skb).fp; | |
7682 | for (i = 0; i < fp->count; i++) { | |
7683 | int left; | |
7684 | ||
7685 | if (fp->fp[i] != file) | |
7686 | continue; | |
7687 | ||
7688 | unix_notinflight(fp->user, fp->fp[i]); | |
7689 | left = fp->count - 1 - i; | |
7690 | if (left) { | |
7691 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7692 | left * sizeof(struct file *)); | |
7693 | } | |
7694 | fp->count--; | |
7695 | if (!fp->count) { | |
7696 | kfree_skb(skb); | |
7697 | skb = NULL; | |
7698 | } else { | |
7699 | __skb_queue_tail(&list, skb); | |
7700 | } | |
7701 | fput(file); | |
7702 | file = NULL; | |
7703 | break; | |
7704 | } | |
7705 | ||
7706 | if (!file) | |
7707 | break; | |
7708 | ||
7709 | __skb_queue_tail(&list, skb); | |
7710 | ||
7711 | skb = skb_dequeue(head); | |
7712 | } | |
7713 | ||
7714 | if (skb_peek(&list)) { | |
7715 | spin_lock_irq(&head->lock); | |
7716 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7717 | __skb_queue_tail(head, skb); | |
7718 | spin_unlock_irq(&head->lock); | |
7719 | } | |
7720 | #else | |
7721 | fput(file); | |
7722 | #endif | |
7723 | } | |
7724 | ||
269bbe5f | 7725 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7726 | { |
269bbe5f BM |
7727 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7728 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7729 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7730 | |
269bbe5f BM |
7731 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7732 | list_del(&prsrc->list); | |
50238531 | 7733 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7734 | kfree(prsrc); |
65e19f54 | 7735 | } |
05589553 | 7736 | |
05589553 XW |
7737 | percpu_ref_exit(&ref_node->refs); |
7738 | kfree(ref_node); | |
269bbe5f | 7739 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7740 | } |
65e19f54 | 7741 | |
269bbe5f | 7742 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7743 | { |
7744 | struct io_ring_ctx *ctx; | |
7745 | struct llist_node *node; | |
7746 | ||
269bbe5f BM |
7747 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7748 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7749 | |
7750 | while (node) { | |
269bbe5f | 7751 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7752 | struct llist_node *next = node->next; |
7753 | ||
269bbe5f BM |
7754 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7755 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7756 | node = next; |
7757 | } | |
7758 | } | |
7759 | ||
ea64ec02 PB |
7760 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7761 | unsigned i) | |
2faf852d | 7762 | { |
ea64ec02 PB |
7763 | struct fixed_rsrc_table *table; |
7764 | ||
7765 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7766 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7767 | } | |
7768 | ||
00835dce | 7769 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7770 | { |
269bbe5f BM |
7771 | struct fixed_rsrc_ref_node *ref_node; |
7772 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7773 | struct io_ring_ctx *ctx; |
e297822b | 7774 | bool first_add = false; |
4a38aed2 | 7775 | int delay = HZ; |
65e19f54 | 7776 | |
269bbe5f BM |
7777 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7778 | data = ref_node->rsrc_data; | |
e297822b PB |
7779 | ctx = data->ctx; |
7780 | ||
2a63b2d9 | 7781 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7782 | ref_node->done = true; |
7783 | ||
d67d2263 BM |
7784 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7785 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7786 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7787 | /* recycle ref nodes in order */ |
7788 | if (!ref_node->done) | |
7789 | break; | |
7790 | list_del(&ref_node->node); | |
269bbe5f | 7791 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7792 | } |
2a63b2d9 | 7793 | io_rsrc_ref_unlock(ctx); |
05589553 | 7794 | |
e297822b | 7795 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7796 | delay = 0; |
05589553 | 7797 | |
4a38aed2 | 7798 | if (!delay) |
269bbe5f | 7799 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7800 | else if (first_add) |
269bbe5f | 7801 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7802 | } |
65e19f54 | 7803 | |
6802535d | 7804 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7805 | struct io_ring_ctx *ctx) |
05f3fb3c | 7806 | { |
269bbe5f | 7807 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7808 | |
05589553 XW |
7809 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7810 | if (!ref_node) | |
3e2224c5 | 7811 | return NULL; |
05f3fb3c | 7812 | |
00835dce | 7813 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7814 | 0, GFP_KERNEL)) { |
7815 | kfree(ref_node); | |
3e2224c5 | 7816 | return NULL; |
05589553 XW |
7817 | } |
7818 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7819 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7820 | ref_node->done = false; |
05589553 | 7821 | return ref_node; |
05589553 XW |
7822 | } |
7823 | ||
bc9744cd PB |
7824 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7825 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7826 | { |
269bbe5f | 7827 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7828 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7829 | } |
7830 | ||
269bbe5f | 7831 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7832 | { |
7833 | percpu_ref_exit(&ref_node->refs); | |
7834 | kfree(ref_node); | |
65e19f54 JA |
7835 | } |
7836 | ||
ea64ec02 | 7837 | |
6b06314c JA |
7838 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7839 | unsigned nr_args) | |
7840 | { | |
7841 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7842 | unsigned nr_tables, i; |
05f3fb3c | 7843 | struct file *file; |
600cf3f8 | 7844 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7845 | struct fixed_rsrc_ref_node *ref_node; |
7846 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7847 | |
05f3fb3c | 7848 | if (ctx->file_data) |
6b06314c JA |
7849 | return -EBUSY; |
7850 | if (!nr_args) | |
7851 | return -EINVAL; | |
7852 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7853 | return -EMFILE; | |
7854 | ||
1ad555c6 | 7855 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7856 | if (!file_data) |
05f3fb3c | 7857 | return -ENOMEM; |
13770a71 | 7858 | ctx->file_data = file_data; |
05f3fb3c | 7859 | |
65e19f54 | 7860 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7861 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7862 | GFP_KERNEL); |
600cf3f8 PB |
7863 | if (!file_data->table) |
7864 | goto out_free; | |
05f3fb3c | 7865 | |
600cf3f8 | 7866 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7867 | goto out_free; |
65e19f54 | 7868 | |
08a45173 | 7869 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7870 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7871 | ret = -EFAULT; | |
7872 | goto out_fput; | |
7873 | } | |
08a45173 | 7874 | /* allow sparse sets */ |
600cf3f8 | 7875 | if (fd == -1) |
08a45173 | 7876 | continue; |
6b06314c | 7877 | |
05f3fb3c | 7878 | file = fget(fd); |
6b06314c | 7879 | ret = -EBADF; |
05f3fb3c | 7880 | if (!file) |
600cf3f8 | 7881 | goto out_fput; |
05f3fb3c | 7882 | |
6b06314c JA |
7883 | /* |
7884 | * Don't allow io_uring instances to be registered. If UNIX | |
7885 | * isn't enabled, then this causes a reference cycle and this | |
7886 | * instance can never get freed. If UNIX is enabled we'll | |
7887 | * handle it just fine, but there's still no point in allowing | |
7888 | * a ring fd as it doesn't support regular read/write anyway. | |
7889 | */ | |
05f3fb3c JA |
7890 | if (file->f_op == &io_uring_fops) { |
7891 | fput(file); | |
600cf3f8 | 7892 | goto out_fput; |
6b06314c | 7893 | } |
ea64ec02 | 7894 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7895 | } |
7896 | ||
6b06314c | 7897 | ret = io_sqe_files_scm(ctx); |
05589553 | 7898 | if (ret) { |
6b06314c | 7899 | io_sqe_files_unregister(ctx); |
05589553 XW |
7900 | return ret; |
7901 | } | |
6b06314c | 7902 | |
bc9744cd | 7903 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7904 | if (!ref_node) { |
05589553 | 7905 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7906 | return -ENOMEM; |
05589553 | 7907 | } |
bc9744cd | 7908 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7909 | |
d67d2263 | 7910 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7911 | return ret; |
600cf3f8 PB |
7912 | out_fput: |
7913 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7914 | file = io_file_from_index(ctx, i); | |
7915 | if (file) | |
7916 | fput(file); | |
7917 | } | |
7918 | for (i = 0; i < nr_tables; i++) | |
7919 | kfree(file_data->table[i].files); | |
7920 | ctx->nr_user_files = 0; | |
600cf3f8 | 7921 | out_free: |
1ad555c6 | 7922 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7923 | ctx->file_data = NULL; |
6b06314c JA |
7924 | return ret; |
7925 | } | |
7926 | ||
c3a31e60 JA |
7927 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7928 | int index) | |
7929 | { | |
7930 | #if defined(CONFIG_UNIX) | |
7931 | struct sock *sock = ctx->ring_sock->sk; | |
7932 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7933 | struct sk_buff *skb; | |
7934 | ||
7935 | /* | |
7936 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7937 | * file set. If there's no room, fall back to allocating a new skb | |
7938 | * and filling it in. | |
7939 | */ | |
7940 | spin_lock_irq(&head->lock); | |
7941 | skb = skb_peek(head); | |
7942 | if (skb) { | |
7943 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7944 | ||
7945 | if (fpl->count < SCM_MAX_FD) { | |
7946 | __skb_unlink(skb, head); | |
7947 | spin_unlock_irq(&head->lock); | |
7948 | fpl->fp[fpl->count] = get_file(file); | |
7949 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7950 | fpl->count++; | |
7951 | spin_lock_irq(&head->lock); | |
7952 | __skb_queue_head(head, skb); | |
7953 | } else { | |
7954 | skb = NULL; | |
7955 | } | |
7956 | } | |
7957 | spin_unlock_irq(&head->lock); | |
7958 | ||
7959 | if (skb) { | |
7960 | fput(file); | |
7961 | return 0; | |
7962 | } | |
7963 | ||
7964 | return __io_sqe_files_scm(ctx, 1, index); | |
7965 | #else | |
7966 | return 0; | |
7967 | #endif | |
7968 | } | |
7969 | ||
50238531 | 7970 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7971 | { |
269bbe5f BM |
7972 | struct io_rsrc_put *prsrc; |
7973 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7974 | |
269bbe5f BM |
7975 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7976 | if (!prsrc) | |
a5318d3c | 7977 | return -ENOMEM; |
05f3fb3c | 7978 | |
50238531 | 7979 | prsrc->rsrc = rsrc; |
269bbe5f | 7980 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7981 | |
a5318d3c | 7982 | return 0; |
05f3fb3c JA |
7983 | } |
7984 | ||
269bbe5f BM |
7985 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7986 | struct file *file) | |
7987 | { | |
50238531 | 7988 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7989 | } |
7990 | ||
05f3fb3c | 7991 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7992 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7993 | unsigned nr_args) |
7994 | { | |
269bbe5f BM |
7995 | struct fixed_rsrc_data *data = ctx->file_data; |
7996 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7997 | struct file *file, **file_slot; |
c3a31e60 JA |
7998 | __s32 __user *fds; |
7999 | int fd, i, err; | |
8000 | __u32 done; | |
05589553 | 8001 | bool needs_switch = false; |
c3a31e60 | 8002 | |
05f3fb3c | 8003 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
8004 | return -EOVERFLOW; |
8005 | if (done > ctx->nr_user_files) | |
8006 | return -EINVAL; | |
8007 | ||
bc9744cd | 8008 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
8009 | if (!ref_node) |
8010 | return -ENOMEM; | |
bc9744cd | 8011 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 8012 | |
269bbe5f | 8013 | fds = u64_to_user_ptr(up->data); |
67973b93 | 8014 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
8015 | err = 0; |
8016 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
8017 | err = -EFAULT; | |
8018 | break; | |
8019 | } | |
4e0377a1 | 8020 | if (fd == IORING_REGISTER_FILES_SKIP) |
8021 | continue; | |
8022 | ||
67973b93 | 8023 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
8024 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
8025 | ||
8026 | if (*file_slot) { | |
8027 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
8028 | if (err) |
8029 | break; | |
ea64ec02 | 8030 | *file_slot = NULL; |
05589553 | 8031 | needs_switch = true; |
c3a31e60 JA |
8032 | } |
8033 | if (fd != -1) { | |
c3a31e60 JA |
8034 | file = fget(fd); |
8035 | if (!file) { | |
8036 | err = -EBADF; | |
8037 | break; | |
8038 | } | |
8039 | /* | |
8040 | * Don't allow io_uring instances to be registered. If | |
8041 | * UNIX isn't enabled, then this causes a reference | |
8042 | * cycle and this instance can never get freed. If UNIX | |
8043 | * is enabled we'll handle it just fine, but there's | |
8044 | * still no point in allowing a ring fd as it doesn't | |
8045 | * support regular read/write anyway. | |
8046 | */ | |
8047 | if (file->f_op == &io_uring_fops) { | |
8048 | fput(file); | |
8049 | err = -EBADF; | |
8050 | break; | |
8051 | } | |
e68a3ff8 | 8052 | *file_slot = file; |
c3a31e60 | 8053 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8054 | if (err) { |
e68a3ff8 | 8055 | *file_slot = NULL; |
f3bd9dae | 8056 | fput(file); |
c3a31e60 | 8057 | break; |
f3bd9dae | 8058 | } |
c3a31e60 | 8059 | } |
05f3fb3c JA |
8060 | } |
8061 | ||
05589553 | 8062 | if (needs_switch) { |
b2e96852 | 8063 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 8064 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 8065 | } else |
269bbe5f | 8066 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
8067 | |
8068 | return done ? done : err; | |
8069 | } | |
05589553 | 8070 | |
05f3fb3c JA |
8071 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
8072 | unsigned nr_args) | |
8073 | { | |
269bbe5f | 8074 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
8075 | |
8076 | if (!ctx->file_data) | |
8077 | return -ENXIO; | |
8078 | if (!nr_args) | |
8079 | return -EINVAL; | |
8080 | if (copy_from_user(&up, arg, sizeof(up))) | |
8081 | return -EFAULT; | |
8082 | if (up.resv) | |
8083 | return -EINVAL; | |
8084 | ||
8085 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8086 | } | |
c3a31e60 | 8087 | |
5280f7e5 | 8088 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
8089 | { |
8090 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8091 | ||
5280f7e5 PB |
8092 | req = io_put_req_find_next(req); |
8093 | return req ? &req->work : NULL; | |
7d723065 JA |
8094 | } |
8095 | ||
24369c2e PB |
8096 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
8097 | struct io_uring_params *p) | |
8098 | { | |
8099 | struct io_wq_data data; | |
8100 | struct fd f; | |
8101 | struct io_ring_ctx *ctx_attach; | |
8102 | unsigned int concurrency; | |
8103 | int ret = 0; | |
8104 | ||
8105 | data.user = ctx->user; | |
e9fd9396 | 8106 | data.free_work = io_free_work; |
f5fa38c5 | 8107 | data.do_work = io_wq_submit_work; |
24369c2e PB |
8108 | |
8109 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
8110 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
8111 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
8112 | ||
8113 | ctx->io_wq = io_wq_create(concurrency, &data); | |
8114 | if (IS_ERR(ctx->io_wq)) { | |
8115 | ret = PTR_ERR(ctx->io_wq); | |
8116 | ctx->io_wq = NULL; | |
8117 | } | |
8118 | return ret; | |
8119 | } | |
8120 | ||
8121 | f = fdget(p->wq_fd); | |
8122 | if (!f.file) | |
8123 | return -EBADF; | |
8124 | ||
8125 | if (f.file->f_op != &io_uring_fops) { | |
8126 | ret = -EINVAL; | |
8127 | goto out_fput; | |
8128 | } | |
8129 | ||
8130 | ctx_attach = f.file->private_data; | |
8131 | /* @io_wq is protected by holding the fd */ | |
8132 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8133 | ret = -EINVAL; | |
8134 | goto out_fput; | |
8135 | } | |
8136 | ||
8137 | ctx->io_wq = ctx_attach->io_wq; | |
8138 | out_fput: | |
8139 | fdput(f); | |
8140 | return ret; | |
8141 | } | |
8142 | ||
0f212204 JA |
8143 | static int io_uring_alloc_task_context(struct task_struct *task) |
8144 | { | |
8145 | struct io_uring_task *tctx; | |
d8a6df10 | 8146 | int ret; |
0f212204 JA |
8147 | |
8148 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8149 | if (unlikely(!tctx)) | |
8150 | return -ENOMEM; | |
8151 | ||
d8a6df10 JA |
8152 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8153 | if (unlikely(ret)) { | |
8154 | kfree(tctx); | |
8155 | return ret; | |
8156 | } | |
8157 | ||
0f212204 JA |
8158 | xa_init(&tctx->xa); |
8159 | init_waitqueue_head(&tctx->wait); | |
8160 | tctx->last = NULL; | |
fdaf083c JA |
8161 | atomic_set(&tctx->in_idle, 0); |
8162 | tctx->sqpoll = false; | |
500a373d JA |
8163 | io_init_identity(&tctx->__identity); |
8164 | tctx->identity = &tctx->__identity; | |
0f212204 | 8165 | task->io_uring = tctx; |
7cbf1722 JA |
8166 | spin_lock_init(&tctx->task_lock); |
8167 | INIT_WQ_LIST(&tctx->task_list); | |
8168 | tctx->task_state = 0; | |
8169 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
8170 | return 0; |
8171 | } | |
8172 | ||
8173 | void __io_uring_free(struct task_struct *tsk) | |
8174 | { | |
8175 | struct io_uring_task *tctx = tsk->io_uring; | |
8176 | ||
8177 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8178 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8179 | if (tctx->identity != &tctx->__identity) | |
8180 | kfree(tctx->identity); | |
d8a6df10 | 8181 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8182 | kfree(tctx); |
8183 | tsk->io_uring = NULL; | |
8184 | } | |
8185 | ||
7e84e1c7 SG |
8186 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8187 | struct io_uring_params *p) | |
2b188cc1 JA |
8188 | { |
8189 | int ret; | |
8190 | ||
6c271ce2 | 8191 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8192 | struct io_sq_data *sqd; |
8193 | ||
3ec482d1 | 8194 | ret = -EPERM; |
ce59fc69 | 8195 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8196 | goto err; |
8197 | ||
534ca6d6 JA |
8198 | sqd = io_get_sq_data(p); |
8199 | if (IS_ERR(sqd)) { | |
8200 | ret = PTR_ERR(sqd); | |
8201 | goto err; | |
8202 | } | |
69fb2131 | 8203 | |
534ca6d6 | 8204 | ctx->sq_data = sqd; |
69fb2131 JA |
8205 | io_sq_thread_park(sqd); |
8206 | mutex_lock(&sqd->ctx_lock); | |
8207 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8208 | mutex_unlock(&sqd->ctx_lock); | |
8209 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8210 | |
917257da JA |
8211 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8212 | if (!ctx->sq_thread_idle) | |
8213 | ctx->sq_thread_idle = HZ; | |
8214 | ||
aa06165d JA |
8215 | if (sqd->thread) |
8216 | goto done; | |
8217 | ||
6c271ce2 | 8218 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8219 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8220 | |
917257da | 8221 | ret = -EINVAL; |
44a9bd18 JA |
8222 | if (cpu >= nr_cpu_ids) |
8223 | goto err; | |
7889f44d | 8224 | if (!cpu_online(cpu)) |
917257da JA |
8225 | goto err; |
8226 | ||
69fb2131 | 8227 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8228 | cpu, "io_uring-sq"); |
6c271ce2 | 8229 | } else { |
69fb2131 | 8230 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8231 | "io_uring-sq"); |
8232 | } | |
534ca6d6 JA |
8233 | if (IS_ERR(sqd->thread)) { |
8234 | ret = PTR_ERR(sqd->thread); | |
8235 | sqd->thread = NULL; | |
6c271ce2 JA |
8236 | goto err; |
8237 | } | |
534ca6d6 | 8238 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8239 | if (ret) |
8240 | goto err; | |
6c271ce2 JA |
8241 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8242 | /* Can't have SQ_AFF without SQPOLL */ | |
8243 | ret = -EINVAL; | |
8244 | goto err; | |
8245 | } | |
8246 | ||
aa06165d | 8247 | done: |
24369c2e PB |
8248 | ret = io_init_wq_offload(ctx, p); |
8249 | if (ret) | |
2b188cc1 | 8250 | goto err; |
2b188cc1 JA |
8251 | |
8252 | return 0; | |
8253 | err: | |
54a91f3b | 8254 | io_finish_async(ctx); |
2b188cc1 JA |
8255 | return ret; |
8256 | } | |
8257 | ||
7e84e1c7 SG |
8258 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8259 | { | |
534ca6d6 JA |
8260 | struct io_sq_data *sqd = ctx->sq_data; |
8261 | ||
8262 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8263 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8264 | } |
8265 | ||
a087e2b5 BM |
8266 | static inline void __io_unaccount_mem(struct user_struct *user, |
8267 | unsigned long nr_pages) | |
2b188cc1 JA |
8268 | { |
8269 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8270 | } | |
8271 | ||
a087e2b5 BM |
8272 | static inline int __io_account_mem(struct user_struct *user, |
8273 | unsigned long nr_pages) | |
2b188cc1 JA |
8274 | { |
8275 | unsigned long page_limit, cur_pages, new_pages; | |
8276 | ||
8277 | /* Don't allow more pages than we can safely lock */ | |
8278 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8279 | ||
8280 | do { | |
8281 | cur_pages = atomic_long_read(&user->locked_vm); | |
8282 | new_pages = cur_pages + nr_pages; | |
8283 | if (new_pages > page_limit) | |
8284 | return -ENOMEM; | |
8285 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8286 | new_pages) != cur_pages); | |
8287 | ||
8288 | return 0; | |
8289 | } | |
8290 | ||
26bfa89e | 8291 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8292 | { |
aad5d8da | 8293 | if (ctx->limit_mem) |
a087e2b5 | 8294 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8295 | |
26bfa89e JA |
8296 | if (ctx->mm_account) |
8297 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8298 | } |
8299 | ||
26bfa89e | 8300 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8301 | { |
30975825 BM |
8302 | int ret; |
8303 | ||
8304 | if (ctx->limit_mem) { | |
8305 | ret = __io_account_mem(ctx->user, nr_pages); | |
8306 | if (ret) | |
8307 | return ret; | |
8308 | } | |
8309 | ||
26bfa89e JA |
8310 | if (ctx->mm_account) |
8311 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8312 | |
8313 | return 0; | |
8314 | } | |
8315 | ||
2b188cc1 JA |
8316 | static void io_mem_free(void *ptr) |
8317 | { | |
52e04ef4 MR |
8318 | struct page *page; |
8319 | ||
8320 | if (!ptr) | |
8321 | return; | |
2b188cc1 | 8322 | |
52e04ef4 | 8323 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8324 | if (put_page_testzero(page)) |
8325 | free_compound_page(page); | |
8326 | } | |
8327 | ||
8328 | static void *io_mem_alloc(size_t size) | |
8329 | { | |
8330 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8331 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8332 | |
8333 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8334 | } | |
8335 | ||
75b28aff HV |
8336 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8337 | size_t *sq_offset) | |
8338 | { | |
8339 | struct io_rings *rings; | |
8340 | size_t off, sq_array_size; | |
8341 | ||
8342 | off = struct_size(rings, cqes, cq_entries); | |
8343 | if (off == SIZE_MAX) | |
8344 | return SIZE_MAX; | |
8345 | ||
8346 | #ifdef CONFIG_SMP | |
8347 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8348 | if (off == 0) | |
8349 | return SIZE_MAX; | |
8350 | #endif | |
8351 | ||
b36200f5 DV |
8352 | if (sq_offset) |
8353 | *sq_offset = off; | |
8354 | ||
75b28aff HV |
8355 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8356 | if (sq_array_size == SIZE_MAX) | |
8357 | return SIZE_MAX; | |
8358 | ||
8359 | if (check_add_overflow(off, sq_array_size, &off)) | |
8360 | return SIZE_MAX; | |
8361 | ||
75b28aff HV |
8362 | return off; |
8363 | } | |
8364 | ||
0a96bbe4 | 8365 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8366 | { |
8367 | int i, j; | |
8368 | ||
8369 | if (!ctx->user_bufs) | |
8370 | return -ENXIO; | |
8371 | ||
8372 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8373 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8374 | ||
8375 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8376 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8377 | |
de293938 | 8378 | if (imu->acct_pages) |
26bfa89e | 8379 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8380 | kvfree(imu->bvec); |
edafccee JA |
8381 | imu->nr_bvecs = 0; |
8382 | } | |
8383 | ||
8384 | kfree(ctx->user_bufs); | |
8385 | ctx->user_bufs = NULL; | |
8386 | ctx->nr_user_bufs = 0; | |
8387 | return 0; | |
8388 | } | |
8389 | ||
8390 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8391 | void __user *arg, unsigned index) | |
8392 | { | |
8393 | struct iovec __user *src; | |
8394 | ||
8395 | #ifdef CONFIG_COMPAT | |
8396 | if (ctx->compat) { | |
8397 | struct compat_iovec __user *ciovs; | |
8398 | struct compat_iovec ciov; | |
8399 | ||
8400 | ciovs = (struct compat_iovec __user *) arg; | |
8401 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8402 | return -EFAULT; | |
8403 | ||
d55e5f5b | 8404 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8405 | dst->iov_len = ciov.iov_len; |
8406 | return 0; | |
8407 | } | |
8408 | #endif | |
8409 | src = (struct iovec __user *) arg; | |
8410 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8411 | return -EFAULT; | |
8412 | return 0; | |
8413 | } | |
8414 | ||
de293938 JA |
8415 | /* |
8416 | * Not super efficient, but this is just a registration time. And we do cache | |
8417 | * the last compound head, so generally we'll only do a full search if we don't | |
8418 | * match that one. | |
8419 | * | |
8420 | * We check if the given compound head page has already been accounted, to | |
8421 | * avoid double accounting it. This allows us to account the full size of the | |
8422 | * page, not just the constituent pages of a huge page. | |
8423 | */ | |
8424 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8425 | int nr_pages, struct page *hpage) | |
8426 | { | |
8427 | int i, j; | |
8428 | ||
8429 | /* check current page array */ | |
8430 | for (i = 0; i < nr_pages; i++) { | |
8431 | if (!PageCompound(pages[i])) | |
8432 | continue; | |
8433 | if (compound_head(pages[i]) == hpage) | |
8434 | return true; | |
8435 | } | |
8436 | ||
8437 | /* check previously registered pages */ | |
8438 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8439 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8440 | ||
8441 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8442 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8443 | continue; | |
8444 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8445 | return true; | |
8446 | } | |
8447 | } | |
8448 | ||
8449 | return false; | |
8450 | } | |
8451 | ||
8452 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8453 | int nr_pages, struct io_mapped_ubuf *imu, | |
8454 | struct page **last_hpage) | |
8455 | { | |
8456 | int i, ret; | |
8457 | ||
8458 | for (i = 0; i < nr_pages; i++) { | |
8459 | if (!PageCompound(pages[i])) { | |
8460 | imu->acct_pages++; | |
8461 | } else { | |
8462 | struct page *hpage; | |
8463 | ||
8464 | hpage = compound_head(pages[i]); | |
8465 | if (hpage == *last_hpage) | |
8466 | continue; | |
8467 | *last_hpage = hpage; | |
8468 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8469 | continue; | |
8470 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8471 | } | |
8472 | } | |
8473 | ||
8474 | if (!imu->acct_pages) | |
8475 | return 0; | |
8476 | ||
26bfa89e | 8477 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8478 | if (ret) |
8479 | imu->acct_pages = 0; | |
8480 | return ret; | |
8481 | } | |
8482 | ||
0a96bbe4 BM |
8483 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8484 | struct io_mapped_ubuf *imu, | |
8485 | struct page **last_hpage) | |
edafccee JA |
8486 | { |
8487 | struct vm_area_struct **vmas = NULL; | |
8488 | struct page **pages = NULL; | |
0a96bbe4 BM |
8489 | unsigned long off, start, end, ubuf; |
8490 | size_t size; | |
8491 | int ret, pret, nr_pages, i; | |
8492 | ||
8493 | ubuf = (unsigned long) iov->iov_base; | |
8494 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8495 | start = ubuf >> PAGE_SHIFT; | |
8496 | nr_pages = end - start; | |
8497 | ||
8498 | ret = -ENOMEM; | |
8499 | ||
8500 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8501 | if (!pages) | |
8502 | goto done; | |
8503 | ||
8504 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8505 | GFP_KERNEL); | |
8506 | if (!vmas) | |
8507 | goto done; | |
edafccee | 8508 | |
0a96bbe4 BM |
8509 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8510 | GFP_KERNEL); | |
8511 | if (!imu->bvec) | |
8512 | goto done; | |
8513 | ||
8514 | ret = 0; | |
8515 | mmap_read_lock(current->mm); | |
8516 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8517 | pages, vmas); | |
8518 | if (pret == nr_pages) { | |
8519 | /* don't support file backed memory */ | |
8520 | for (i = 0; i < nr_pages; i++) { | |
8521 | struct vm_area_struct *vma = vmas[i]; | |
8522 | ||
8523 | if (vma->vm_file && | |
8524 | !is_file_hugepages(vma->vm_file)) { | |
8525 | ret = -EOPNOTSUPP; | |
8526 | break; | |
8527 | } | |
8528 | } | |
8529 | } else { | |
8530 | ret = pret < 0 ? pret : -EFAULT; | |
8531 | } | |
8532 | mmap_read_unlock(current->mm); | |
8533 | if (ret) { | |
8534 | /* | |
8535 | * if we did partial map, or found file backed vmas, | |
8536 | * release any pages we did get | |
8537 | */ | |
8538 | if (pret > 0) | |
8539 | unpin_user_pages(pages, pret); | |
8540 | kvfree(imu->bvec); | |
8541 | goto done; | |
8542 | } | |
8543 | ||
8544 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8545 | if (ret) { | |
8546 | unpin_user_pages(pages, pret); | |
8547 | kvfree(imu->bvec); | |
8548 | goto done; | |
8549 | } | |
8550 | ||
8551 | off = ubuf & ~PAGE_MASK; | |
8552 | size = iov->iov_len; | |
8553 | for (i = 0; i < nr_pages; i++) { | |
8554 | size_t vec_len; | |
8555 | ||
8556 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8557 | imu->bvec[i].bv_page = pages[i]; | |
8558 | imu->bvec[i].bv_len = vec_len; | |
8559 | imu->bvec[i].bv_offset = off; | |
8560 | off = 0; | |
8561 | size -= vec_len; | |
8562 | } | |
8563 | /* store original address for later verification */ | |
8564 | imu->ubuf = ubuf; | |
8565 | imu->len = iov->iov_len; | |
8566 | imu->nr_bvecs = nr_pages; | |
8567 | ret = 0; | |
8568 | done: | |
8569 | kvfree(pages); | |
8570 | kvfree(vmas); | |
8571 | return ret; | |
8572 | } | |
8573 | ||
2b358604 | 8574 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8575 | { |
edafccee JA |
8576 | if (ctx->user_bufs) |
8577 | return -EBUSY; | |
8578 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8579 | return -EINVAL; | |
8580 | ||
8581 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8582 | GFP_KERNEL); | |
8583 | if (!ctx->user_bufs) | |
8584 | return -ENOMEM; | |
8585 | ||
2b358604 BM |
8586 | return 0; |
8587 | } | |
edafccee | 8588 | |
2b358604 BM |
8589 | static int io_buffer_validate(struct iovec *iov) |
8590 | { | |
8591 | /* | |
8592 | * Don't impose further limits on the size and buffer | |
8593 | * constraints here, we'll -EINVAL later when IO is | |
8594 | * submitted if they are wrong. | |
8595 | */ | |
8596 | if (!iov->iov_base || !iov->iov_len) | |
8597 | return -EFAULT; | |
edafccee | 8598 | |
2b358604 BM |
8599 | /* arbitrary limit, but we need something */ |
8600 | if (iov->iov_len > SZ_1G) | |
8601 | return -EFAULT; | |
edafccee | 8602 | |
2b358604 BM |
8603 | return 0; |
8604 | } | |
edafccee | 8605 | |
2b358604 BM |
8606 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8607 | unsigned int nr_args) | |
8608 | { | |
8609 | int i, ret; | |
8610 | struct iovec iov; | |
8611 | struct page *last_hpage = NULL; | |
edafccee | 8612 | |
2b358604 BM |
8613 | ret = io_buffers_map_alloc(ctx, nr_args); |
8614 | if (ret) | |
8615 | return ret; | |
edafccee | 8616 | |
edafccee JA |
8617 | for (i = 0; i < nr_args; i++) { |
8618 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8619 | |
edafccee JA |
8620 | ret = io_copy_iov(ctx, &iov, arg, i); |
8621 | if (ret) | |
0a96bbe4 | 8622 | break; |
de293938 | 8623 | |
2b358604 BM |
8624 | ret = io_buffer_validate(&iov); |
8625 | if (ret) | |
0a96bbe4 | 8626 | break; |
edafccee | 8627 | |
0a96bbe4 BM |
8628 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8629 | if (ret) | |
8630 | break; | |
edafccee JA |
8631 | |
8632 | ctx->nr_user_bufs++; | |
8633 | } | |
0a96bbe4 BM |
8634 | |
8635 | if (ret) | |
8636 | io_sqe_buffers_unregister(ctx); | |
8637 | ||
edafccee JA |
8638 | return ret; |
8639 | } | |
8640 | ||
9b402849 JA |
8641 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8642 | { | |
8643 | __s32 __user *fds = arg; | |
8644 | int fd; | |
8645 | ||
8646 | if (ctx->cq_ev_fd) | |
8647 | return -EBUSY; | |
8648 | ||
8649 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8650 | return -EFAULT; | |
8651 | ||
8652 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8653 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8654 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8655 | ctx->cq_ev_fd = NULL; | |
8656 | return ret; | |
8657 | } | |
8658 | ||
8659 | return 0; | |
8660 | } | |
8661 | ||
8662 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8663 | { | |
8664 | if (ctx->cq_ev_fd) { | |
8665 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8666 | ctx->cq_ev_fd = NULL; | |
8667 | return 0; | |
8668 | } | |
8669 | ||
8670 | return -ENXIO; | |
8671 | } | |
8672 | ||
5a2e745d JA |
8673 | static int __io_destroy_buffers(int id, void *p, void *data) |
8674 | { | |
8675 | struct io_ring_ctx *ctx = data; | |
8676 | struct io_buffer *buf = p; | |
8677 | ||
067524e9 | 8678 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8679 | return 0; |
8680 | } | |
8681 | ||
8682 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8683 | { | |
8684 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8685 | idr_destroy(&ctx->io_buffer_idr); | |
8686 | } | |
8687 | ||
68e68ee6 | 8688 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8689 | { |
68e68ee6 | 8690 | struct io_kiocb *req, *nxt; |
1b4c351f | 8691 | |
68e68ee6 JA |
8692 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8693 | if (tsk && req->task != tsk) | |
8694 | continue; | |
1b4c351f JA |
8695 | list_del(&req->compl.list); |
8696 | kmem_cache_free(req_cachep, req); | |
8697 | } | |
8698 | } | |
8699 | ||
9a4fdbd8 | 8700 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
2b188cc1 | 8701 | { |
bf019da7 | 8702 | struct io_submit_state *submit_state = &ctx->submit_state; |
e5547d2c | 8703 | struct io_comp_state *cs = &ctx->submit_state.comp; |
bf019da7 | 8704 | |
9a4fdbd8 JA |
8705 | mutex_lock(&ctx->uring_lock); |
8706 | ||
8e5c66c4 | 8707 | if (submit_state->free_reqs) { |
9a4fdbd8 JA |
8708 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
8709 | submit_state->reqs); | |
8e5c66c4 PB |
8710 | submit_state->free_reqs = 0; |
8711 | } | |
9a4fdbd8 JA |
8712 | |
8713 | spin_lock_irq(&ctx->completion_lock); | |
e5547d2c PB |
8714 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
8715 | cs->locked_free_nr = 0; | |
9a4fdbd8 JA |
8716 | spin_unlock_irq(&ctx->completion_lock); |
8717 | ||
e5547d2c PB |
8718 | io_req_cache_free(&cs->free_list, NULL); |
8719 | ||
9a4fdbd8 JA |
8720 | mutex_unlock(&ctx->uring_lock); |
8721 | } | |
8722 | ||
2b188cc1 JA |
8723 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8724 | { | |
04fc6c80 PB |
8725 | /* |
8726 | * Some may use context even when all refs and requests have been put, | |
8727 | * and they are free to do so while still holding uring_lock, see | |
8728 | * __io_req_task_submit(). Wait for them to finish. | |
8729 | */ | |
8730 | mutex_lock(&ctx->uring_lock); | |
8731 | mutex_unlock(&ctx->uring_lock); | |
8732 | ||
6b06314c | 8733 | io_finish_async(ctx); |
0a96bbe4 | 8734 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8735 | |
8736 | if (ctx->sqo_task) { | |
8737 | put_task_struct(ctx->sqo_task); | |
8738 | ctx->sqo_task = NULL; | |
8739 | mmdrop(ctx->mm_account); | |
8740 | ctx->mm_account = NULL; | |
30975825 | 8741 | } |
def596e9 | 8742 | |
91d8f519 DZ |
8743 | #ifdef CONFIG_BLK_CGROUP |
8744 | if (ctx->sqo_blkcg_css) | |
8745 | css_put(ctx->sqo_blkcg_css); | |
8746 | #endif | |
8747 | ||
8bad28d8 | 8748 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8749 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8750 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8751 | io_eventfd_unregister(ctx); |
5a2e745d | 8752 | io_destroy_buffers(ctx); |
41726c9a | 8753 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8754 | |
2b188cc1 | 8755 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8756 | if (ctx->ring_sock) { |
8757 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8758 | sock_release(ctx->ring_sock); |
355e8d26 | 8759 | } |
2b188cc1 JA |
8760 | #endif |
8761 | ||
75b28aff | 8762 | io_mem_free(ctx->rings); |
2b188cc1 | 8763 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8764 | |
8765 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8766 | free_uid(ctx->user); |
181e448d | 8767 | put_cred(ctx->creds); |
9a4fdbd8 | 8768 | io_req_caches_free(ctx, NULL); |
78076bb6 | 8769 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8770 | kfree(ctx); |
8771 | } | |
8772 | ||
8773 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8774 | { | |
8775 | struct io_ring_ctx *ctx = file->private_data; | |
8776 | __poll_t mask = 0; | |
8777 | ||
8778 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8779 | /* |
8780 | * synchronizes with barrier from wq_has_sleeper call in | |
8781 | * io_commit_cqring | |
8782 | */ | |
2b188cc1 | 8783 | smp_rmb(); |
90554200 | 8784 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8785 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8786 | |
8787 | /* | |
8788 | * Don't flush cqring overflow list here, just do a simple check. | |
8789 | * Otherwise there could possible be ABBA deadlock: | |
8790 | * CPU0 CPU1 | |
8791 | * ---- ---- | |
8792 | * lock(&ctx->uring_lock); | |
8793 | * lock(&ep->mtx); | |
8794 | * lock(&ctx->uring_lock); | |
8795 | * lock(&ep->mtx); | |
8796 | * | |
8797 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8798 | * pushs them to do the flush. | |
8799 | */ | |
8800 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8801 | mask |= EPOLLIN | EPOLLRDNORM; |
8802 | ||
8803 | return mask; | |
8804 | } | |
8805 | ||
8806 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8807 | { | |
8808 | struct io_ring_ctx *ctx = file->private_data; | |
8809 | ||
8810 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8811 | } | |
8812 | ||
0bead8cd | 8813 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8814 | { |
1e6fa521 | 8815 | struct io_identity *iod; |
071698e1 | 8816 | |
1e6fa521 JA |
8817 | iod = idr_remove(&ctx->personality_idr, id); |
8818 | if (iod) { | |
8819 | put_cred(iod->creds); | |
8820 | if (refcount_dec_and_test(&iod->count)) | |
8821 | kfree(iod); | |
0bead8cd | 8822 | return 0; |
1e6fa521 | 8823 | } |
0bead8cd YD |
8824 | |
8825 | return -EINVAL; | |
8826 | } | |
8827 | ||
8828 | static int io_remove_personalities(int id, void *p, void *data) | |
8829 | { | |
8830 | struct io_ring_ctx *ctx = data; | |
8831 | ||
8832 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8833 | return 0; |
8834 | } | |
8835 | ||
85faa7b8 JA |
8836 | static void io_ring_exit_work(struct work_struct *work) |
8837 | { | |
b2edc0a7 PB |
8838 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8839 | exit_work); | |
85faa7b8 | 8840 | |
56952e91 JA |
8841 | /* |
8842 | * If we're doing polled IO and end up having requests being | |
8843 | * submitted async (out-of-line), then completions can come in while | |
8844 | * we're waiting for refs to drop. We need to reap these manually, | |
8845 | * as nobody else will be looking for them. | |
8846 | */ | |
b2edc0a7 | 8847 | do { |
9936c7c2 | 8848 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b2edc0a7 | 8849 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8850 | io_ring_ctx_free(ctx); |
8851 | } | |
8852 | ||
00c18640 JA |
8853 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8854 | { | |
8855 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8856 | ||
8857 | return req->ctx == data; | |
8858 | } | |
8859 | ||
2b188cc1 JA |
8860 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8861 | { | |
8862 | mutex_lock(&ctx->uring_lock); | |
8863 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8864 | |
8865 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8866 | ctx->sqo_dead = 1; | |
8867 | ||
cda286f0 PB |
8868 | /* if force is set, the ring is going away. always drop after that */ |
8869 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8870 | if (ctx->rings) |
6c503150 | 8871 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8872 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8873 | mutex_unlock(&ctx->uring_lock); |
8874 | ||
6b81928d PB |
8875 | io_kill_timeouts(ctx, NULL, NULL); |
8876 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8877 | |
8878 | if (ctx->io_wq) | |
00c18640 | 8879 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8880 | |
15dff286 | 8881 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8882 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8883 | |
85faa7b8 | 8884 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8885 | /* |
8886 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8887 | * if we're exiting a ton of rings at the same time. It just adds | |
8888 | * noise and overhead, there's no discernable change in runtime | |
8889 | * over using system_wq. | |
8890 | */ | |
8891 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8892 | } |
8893 | ||
8894 | static int io_uring_release(struct inode *inode, struct file *file) | |
8895 | { | |
8896 | struct io_ring_ctx *ctx = file->private_data; | |
8897 | ||
8898 | file->private_data = NULL; | |
8899 | io_ring_ctx_wait_and_kill(ctx); | |
8900 | return 0; | |
8901 | } | |
8902 | ||
f6edbabb PB |
8903 | struct io_task_cancel { |
8904 | struct task_struct *task; | |
8905 | struct files_struct *files; | |
8906 | }; | |
f254ac04 | 8907 | |
f6edbabb | 8908 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8909 | { |
9a472ef7 | 8910 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8911 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8912 | bool ret; |
8913 | ||
f6edbabb | 8914 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8915 | unsigned long flags; |
8916 | struct io_ring_ctx *ctx = req->ctx; | |
8917 | ||
8918 | /* protect against races with linked timeouts */ | |
8919 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8920 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8921 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8922 | } else { | |
f6edbabb | 8923 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8924 | } |
8925 | return ret; | |
b711d4ea JA |
8926 | } |
8927 | ||
b7ddce3c | 8928 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8929 | struct task_struct *task, |
b7ddce3c PB |
8930 | struct files_struct *files) |
8931 | { | |
8932 | struct io_defer_entry *de = NULL; | |
8933 | LIST_HEAD(list); | |
8934 | ||
8935 | spin_lock_irq(&ctx->completion_lock); | |
8936 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8937 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8938 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8939 | break; | |
8940 | } | |
8941 | } | |
8942 | spin_unlock_irq(&ctx->completion_lock); | |
8943 | ||
8944 | while (!list_empty(&list)) { | |
8945 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8946 | list_del_init(&de->list); | |
8947 | req_set_fail_links(de->req); | |
8948 | io_put_req(de->req); | |
8949 | io_req_complete(de->req, -ECANCELED); | |
8950 | kfree(de); | |
8951 | } | |
8952 | } | |
8953 | ||
9936c7c2 PB |
8954 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8955 | struct task_struct *task, | |
8956 | struct files_struct *files) | |
8957 | { | |
8958 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
8959 | ||
8960 | while (1) { | |
8961 | enum io_wq_cancel cret; | |
8962 | bool ret = false; | |
8963 | ||
8964 | if (ctx->io_wq) { | |
8965 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, | |
8966 | &cancel, true); | |
8967 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8968 | } | |
8969 | ||
8970 | /* SQPOLL thread does its own polling */ | |
8971 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8972 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8973 | io_iopoll_try_reap_events(ctx); | |
8974 | ret = true; | |
8975 | } | |
8976 | } | |
8977 | ||
8978 | ret |= io_poll_remove_all(ctx, task, files); | |
8979 | ret |= io_kill_timeouts(ctx, task, files); | |
8980 | ret |= io_run_task_work(); | |
8981 | io_cqring_overflow_flush(ctx, true, task, files); | |
8982 | if (!ret) | |
8983 | break; | |
8984 | cond_resched(); | |
8985 | } | |
8986 | } | |
8987 | ||
ca70f00b PB |
8988 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8989 | struct task_struct *task, | |
8990 | struct files_struct *files) | |
8991 | { | |
8992 | struct io_kiocb *req; | |
8993 | int cnt = 0; | |
8994 | ||
8995 | spin_lock_irq(&ctx->inflight_lock); | |
8996 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8997 | cnt += io_match_task(req, task, files); | |
8998 | spin_unlock_irq(&ctx->inflight_lock); | |
8999 | return cnt; | |
9000 | } | |
9001 | ||
b52fda00 | 9002 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 9003 | struct task_struct *task, |
fcb323cc JA |
9004 | struct files_struct *files) |
9005 | { | |
fcb323cc | 9006 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 9007 | DEFINE_WAIT(wait); |
ca70f00b | 9008 | int inflight; |
fcb323cc | 9009 | |
ca70f00b PB |
9010 | inflight = io_uring_count_inflight(ctx, task, files); |
9011 | if (!inflight) | |
fcb323cc | 9012 | break; |
f6edbabb | 9013 | |
9936c7c2 | 9014 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 9015 | |
34343786 PB |
9016 | if (ctx->sq_data) |
9017 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
9018 | prepare_to_wait(&task->io_uring->wait, &wait, |
9019 | TASK_UNINTERRUPTIBLE); | |
9020 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
9021 | schedule(); | |
c98de08c | 9022 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
9023 | if (ctx->sq_data) |
9024 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 9025 | } |
0f212204 JA |
9026 | } |
9027 | ||
d9d05217 PB |
9028 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
9029 | { | |
d9d05217 PB |
9030 | mutex_lock(&ctx->uring_lock); |
9031 | ctx->sqo_dead = 1; | |
9032 | mutex_unlock(&ctx->uring_lock); | |
9033 | ||
9034 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
9035 | if (ctx->rings) |
9036 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
9037 | } |
9038 | ||
0f212204 JA |
9039 | /* |
9040 | * We need to iteratively cancel requests, in case a request has dependent | |
9041 | * hard links. These persist even for failure of cancelations, hence keep | |
9042 | * looping until none are found. | |
9043 | */ | |
9044 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
9045 | struct files_struct *files) | |
9046 | { | |
9047 | struct task_struct *task = current; | |
9048 | ||
fdaf083c | 9049 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 9050 | io_disable_sqo_submit(ctx); |
534ca6d6 | 9051 | task = ctx->sq_data->thread; |
fdaf083c JA |
9052 | atomic_inc(&task->io_uring->in_idle); |
9053 | io_sq_thread_park(ctx->sq_data); | |
9054 | } | |
0f212204 | 9055 | |
df9923f9 | 9056 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 9057 | |
3a7efd1a | 9058 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 9059 | if (!files) |
9936c7c2 | 9060 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c JA |
9061 | |
9062 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
9063 | atomic_dec(&task->io_uring->in_idle); | |
fdaf083c JA |
9064 | io_sq_thread_unpark(ctx->sq_data); |
9065 | } | |
0f212204 JA |
9066 | } |
9067 | ||
9068 | /* | |
9069 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9070 | */ | |
fdaf083c | 9071 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 9072 | { |
236434c3 | 9073 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 9074 | int ret; |
236434c3 MWO |
9075 | |
9076 | if (unlikely(!tctx)) { | |
0f212204 JA |
9077 | ret = io_uring_alloc_task_context(current); |
9078 | if (unlikely(ret)) | |
9079 | return ret; | |
236434c3 | 9080 | tctx = current->io_uring; |
0f212204 | 9081 | } |
236434c3 MWO |
9082 | if (tctx->last != file) { |
9083 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 9084 | |
236434c3 | 9085 | if (!old) { |
0f212204 | 9086 | get_file(file); |
a528b04e PB |
9087 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
9088 | file, GFP_KERNEL)); | |
9089 | if (ret) { | |
9090 | fput(file); | |
9091 | return ret; | |
9092 | } | |
ecfc8492 PB |
9093 | |
9094 | /* one and only SQPOLL file note, held by sqo_task */ | |
9095 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
9096 | current != ctx->sqo_task); | |
0f212204 | 9097 | } |
236434c3 | 9098 | tctx->last = file; |
0f212204 JA |
9099 | } |
9100 | ||
fdaf083c JA |
9101 | /* |
9102 | * This is race safe in that the task itself is doing this, hence it | |
9103 | * cannot be going through the exit/cancel paths at the same time. | |
9104 | * This cannot be modified while exit/cancel is running. | |
9105 | */ | |
9106 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9107 | tctx->sqpoll = true; | |
9108 | ||
0f212204 JA |
9109 | return 0; |
9110 | } | |
9111 | ||
9112 | /* | |
9113 | * Remove this io_uring_file -> task mapping. | |
9114 | */ | |
9115 | static void io_uring_del_task_file(struct file *file) | |
9116 | { | |
9117 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9118 | |
9119 | if (tctx->last == file) | |
9120 | tctx->last = NULL; | |
5e2ed8c4 | 9121 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9122 | if (file) |
9123 | fput(file); | |
9124 | } | |
9125 | ||
de7f1d9e PB |
9126 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9127 | { | |
9128 | struct file *file; | |
9129 | unsigned long index; | |
9130 | ||
9131 | xa_for_each(&tctx->xa, index, file) | |
9132 | io_uring_del_task_file(file); | |
9133 | } | |
9134 | ||
0f212204 JA |
9135 | void __io_uring_files_cancel(struct files_struct *files) |
9136 | { | |
9137 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9138 | struct file *file; |
9139 | unsigned long index; | |
0f212204 JA |
9140 | |
9141 | /* make sure overflow events are dropped */ | |
fdaf083c | 9142 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9143 | xa_for_each(&tctx->xa, index, file) |
9144 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9145 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9146 | |
9147 | if (files) | |
9148 | io_uring_remove_task_files(tctx); | |
fdaf083c JA |
9149 | } |
9150 | ||
9151 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9152 | { | |
0e9ddb39 PB |
9153 | return percpu_counter_sum(&tctx->inflight); |
9154 | } | |
fdaf083c | 9155 | |
0e9ddb39 PB |
9156 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
9157 | { | |
9158 | struct io_uring_task *tctx; | |
9159 | s64 inflight; | |
9160 | DEFINE_WAIT(wait); | |
fdaf083c | 9161 | |
0e9ddb39 PB |
9162 | if (!ctx->sq_data) |
9163 | return; | |
9164 | tctx = ctx->sq_data->thread->io_uring; | |
9165 | io_disable_sqo_submit(ctx); | |
fdaf083c | 9166 | |
0e9ddb39 PB |
9167 | atomic_inc(&tctx->in_idle); |
9168 | do { | |
9169 | /* read completions before cancelations */ | |
9170 | inflight = tctx_inflight(tctx); | |
9171 | if (!inflight) | |
9172 | break; | |
9173 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 9174 | |
0e9ddb39 PB |
9175 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
9176 | /* | |
9177 | * If we've seen completions, retry without waiting. This | |
9178 | * avoids a race where a completion comes in before we did | |
9179 | * prepare_to_wait(). | |
9180 | */ | |
9181 | if (inflight == tctx_inflight(tctx)) | |
9182 | schedule(); | |
9183 | finish_wait(&tctx->wait, &wait); | |
9184 | } while (1); | |
9185 | atomic_dec(&tctx->in_idle); | |
0f212204 JA |
9186 | } |
9187 | ||
0f212204 JA |
9188 | /* |
9189 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9190 | * requests. | |
9191 | */ | |
9192 | void __io_uring_task_cancel(void) | |
9193 | { | |
9194 | struct io_uring_task *tctx = current->io_uring; | |
9195 | DEFINE_WAIT(wait); | |
d8a6df10 | 9196 | s64 inflight; |
0f212204 JA |
9197 | |
9198 | /* make sure overflow events are dropped */ | |
fdaf083c | 9199 | atomic_inc(&tctx->in_idle); |
0f212204 | 9200 | |
0b5cd6c3 | 9201 | /* trigger io_disable_sqo_submit() */ |
0e9ddb39 PB |
9202 | if (tctx->sqpoll) { |
9203 | struct file *file; | |
9204 | unsigned long index; | |
9205 | ||
9206 | xa_for_each(&tctx->xa, index, file) | |
9207 | io_uring_cancel_sqpoll(file->private_data); | |
9208 | } | |
0b5cd6c3 | 9209 | |
d8a6df10 | 9210 | do { |
0f212204 | 9211 | /* read completions before cancelations */ |
fdaf083c | 9212 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9213 | if (!inflight) |
9214 | break; | |
0f212204 JA |
9215 | __io_uring_files_cancel(NULL); |
9216 | ||
9217 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9218 | ||
9219 | /* | |
a1bb3cd5 PB |
9220 | * If we've seen completions, retry without waiting. This |
9221 | * avoids a race where a completion comes in before we did | |
9222 | * prepare_to_wait(). | |
0f212204 | 9223 | */ |
a1bb3cd5 PB |
9224 | if (inflight == tctx_inflight(tctx)) |
9225 | schedule(); | |
f57555ed | 9226 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9227 | } while (1); |
0f212204 | 9228 | |
fdaf083c | 9229 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9230 | |
9231 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9232 | } |
9233 | ||
fcb323cc JA |
9234 | static int io_uring_flush(struct file *file, void *data) |
9235 | { | |
6b5733eb | 9236 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9237 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9238 | |
41be53e9 | 9239 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
84965ff8 | 9240 | io_uring_cancel_task_requests(ctx, NULL); |
41be53e9 JA |
9241 | io_req_caches_free(ctx, current); |
9242 | } | |
84965ff8 | 9243 | |
6b5733eb | 9244 | if (!tctx) |
4f793dc4 PB |
9245 | return 0; |
9246 | ||
6b5733eb PB |
9247 | /* we should have cancelled and erased it before PF_EXITING */ |
9248 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9249 | xa_load(&tctx->xa, (unsigned long)file)); | |
9250 | ||
4f793dc4 PB |
9251 | /* |
9252 | * fput() is pending, will be 2 if the only other ref is our potential | |
9253 | * task file note. If the task is exiting, drop regardless of count. | |
9254 | */ | |
6b5733eb PB |
9255 | if (atomic_long_read(&file->f_count) != 2) |
9256 | return 0; | |
4f793dc4 | 9257 | |
d9d05217 PB |
9258 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9259 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9260 | WARN_ON_ONCE(ctx->sqo_task != current && |
9261 | xa_load(&tctx->xa, (unsigned long)file)); | |
9262 | /* sqo_dead check is for when this happens after cancellation */ | |
9263 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9264 | !xa_load(&tctx->xa, (unsigned long)file)); |
9265 | ||
9266 | io_disable_sqo_submit(ctx); | |
9267 | } | |
9268 | ||
9269 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9270 | io_uring_del_task_file(file); | |
fcb323cc JA |
9271 | return 0; |
9272 | } | |
9273 | ||
6c5c240e RP |
9274 | static void *io_uring_validate_mmap_request(struct file *file, |
9275 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9276 | { |
2b188cc1 | 9277 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9278 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9279 | struct page *page; |
9280 | void *ptr; | |
9281 | ||
9282 | switch (offset) { | |
9283 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9284 | case IORING_OFF_CQ_RING: |
9285 | ptr = ctx->rings; | |
2b188cc1 JA |
9286 | break; |
9287 | case IORING_OFF_SQES: | |
9288 | ptr = ctx->sq_sqes; | |
9289 | break; | |
2b188cc1 | 9290 | default: |
6c5c240e | 9291 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9292 | } |
9293 | ||
9294 | page = virt_to_head_page(ptr); | |
a50b854e | 9295 | if (sz > page_size(page)) |
6c5c240e RP |
9296 | return ERR_PTR(-EINVAL); |
9297 | ||
9298 | return ptr; | |
9299 | } | |
9300 | ||
9301 | #ifdef CONFIG_MMU | |
9302 | ||
9303 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9304 | { | |
9305 | size_t sz = vma->vm_end - vma->vm_start; | |
9306 | unsigned long pfn; | |
9307 | void *ptr; | |
9308 | ||
9309 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9310 | if (IS_ERR(ptr)) | |
9311 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9312 | |
9313 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9314 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9315 | } | |
9316 | ||
6c5c240e RP |
9317 | #else /* !CONFIG_MMU */ |
9318 | ||
9319 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9320 | { | |
9321 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9322 | } | |
9323 | ||
9324 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9325 | { | |
9326 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9327 | } | |
9328 | ||
9329 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9330 | unsigned long addr, unsigned long len, | |
9331 | unsigned long pgoff, unsigned long flags) | |
9332 | { | |
9333 | void *ptr; | |
9334 | ||
9335 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9336 | if (IS_ERR(ptr)) | |
9337 | return PTR_ERR(ptr); | |
9338 | ||
9339 | return (unsigned long) ptr; | |
9340 | } | |
9341 | ||
9342 | #endif /* !CONFIG_MMU */ | |
9343 | ||
d9d05217 | 9344 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9345 | { |
d9d05217 | 9346 | int ret = 0; |
90554200 JA |
9347 | DEFINE_WAIT(wait); |
9348 | ||
9349 | do { | |
9350 | if (!io_sqring_full(ctx)) | |
9351 | break; | |
9352 | ||
9353 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9354 | ||
d9d05217 PB |
9355 | if (unlikely(ctx->sqo_dead)) { |
9356 | ret = -EOWNERDEAD; | |
9357 | goto out; | |
9358 | } | |
9359 | ||
90554200 JA |
9360 | if (!io_sqring_full(ctx)) |
9361 | break; | |
9362 | ||
9363 | schedule(); | |
9364 | } while (!signal_pending(current)); | |
9365 | ||
9366 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9367 | out: |
9368 | return ret; | |
90554200 JA |
9369 | } |
9370 | ||
c73ebb68 HX |
9371 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9372 | struct __kernel_timespec __user **ts, | |
9373 | const sigset_t __user **sig) | |
9374 | { | |
9375 | struct io_uring_getevents_arg arg; | |
9376 | ||
9377 | /* | |
9378 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9379 | * is just a pointer to the sigset_t. | |
9380 | */ | |
9381 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9382 | *sig = (const sigset_t __user *) argp; | |
9383 | *ts = NULL; | |
9384 | return 0; | |
9385 | } | |
9386 | ||
9387 | /* | |
9388 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9389 | * timespec and sigset_t pointers if good. | |
9390 | */ | |
9391 | if (*argsz != sizeof(arg)) | |
9392 | return -EINVAL; | |
9393 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9394 | return -EFAULT; | |
9395 | *sig = u64_to_user_ptr(arg.sigmask); | |
9396 | *argsz = arg.sigmask_sz; | |
9397 | *ts = u64_to_user_ptr(arg.ts); | |
9398 | return 0; | |
9399 | } | |
9400 | ||
2b188cc1 | 9401 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9402 | u32, min_complete, u32, flags, const void __user *, argp, |
9403 | size_t, argsz) | |
2b188cc1 JA |
9404 | { |
9405 | struct io_ring_ctx *ctx; | |
9406 | long ret = -EBADF; | |
9407 | int submitted = 0; | |
9408 | struct fd f; | |
9409 | ||
4c6e277c | 9410 | io_run_task_work(); |
b41e9852 | 9411 | |
90554200 | 9412 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9413 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9414 | return -EINVAL; |
9415 | ||
9416 | f = fdget(fd); | |
9417 | if (!f.file) | |
9418 | return -EBADF; | |
9419 | ||
9420 | ret = -EOPNOTSUPP; | |
9421 | if (f.file->f_op != &io_uring_fops) | |
9422 | goto out_fput; | |
9423 | ||
9424 | ret = -ENXIO; | |
9425 | ctx = f.file->private_data; | |
9426 | if (!percpu_ref_tryget(&ctx->refs)) | |
9427 | goto out_fput; | |
9428 | ||
7e84e1c7 SG |
9429 | ret = -EBADFD; |
9430 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9431 | goto out; | |
9432 | ||
6c271ce2 JA |
9433 | /* |
9434 | * For SQ polling, the thread will do all submissions and completions. | |
9435 | * Just return the requested submit count, and wake the thread if | |
9436 | * we were asked to. | |
9437 | */ | |
b2a9eada | 9438 | ret = 0; |
6c271ce2 | 9439 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9440 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9441 | |
d9d05217 PB |
9442 | ret = -EOWNERDEAD; |
9443 | if (unlikely(ctx->sqo_dead)) | |
9444 | goto out; | |
6c271ce2 | 9445 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9446 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9447 | if (flags & IORING_ENTER_SQ_WAIT) { |
9448 | ret = io_sqpoll_wait_sq(ctx); | |
9449 | if (ret) | |
9450 | goto out; | |
9451 | } | |
6c271ce2 | 9452 | submitted = to_submit; |
b2a9eada | 9453 | } else if (to_submit) { |
fdaf083c | 9454 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9455 | if (unlikely(ret)) |
9456 | goto out; | |
2b188cc1 | 9457 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9458 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9459 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9460 | |
9461 | if (submitted != to_submit) | |
9462 | goto out; | |
2b188cc1 JA |
9463 | } |
9464 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9465 | const sigset_t __user *sig; |
9466 | struct __kernel_timespec __user *ts; | |
9467 | ||
9468 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9469 | if (unlikely(ret)) | |
9470 | goto out; | |
9471 | ||
2b188cc1 JA |
9472 | min_complete = min(min_complete, ctx->cq_entries); |
9473 | ||
32b2244a XW |
9474 | /* |
9475 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9476 | * space applications don't need to do io completion events | |
9477 | * polling again, they can rely on io_sq_thread to do polling | |
9478 | * work, which can reduce cpu usage and uring_lock contention. | |
9479 | */ | |
9480 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9481 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9482 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9483 | } else { |
c73ebb68 | 9484 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9485 | } |
2b188cc1 JA |
9486 | } |
9487 | ||
7c504e65 | 9488 | out: |
6805b32e | 9489 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9490 | out_fput: |
9491 | fdput(f); | |
9492 | return submitted ? submitted : ret; | |
9493 | } | |
9494 | ||
bebdb65e | 9495 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9496 | static int io_uring_show_cred(int id, void *p, void *data) |
9497 | { | |
6b47ab81 JA |
9498 | struct io_identity *iod = p; |
9499 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9500 | struct seq_file *m = data; |
9501 | struct user_namespace *uns = seq_user_ns(m); | |
9502 | struct group_info *gi; | |
9503 | kernel_cap_t cap; | |
9504 | unsigned __capi; | |
9505 | int g; | |
9506 | ||
9507 | seq_printf(m, "%5d\n", id); | |
9508 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9509 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9510 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9511 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9512 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9513 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9514 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9515 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9516 | seq_puts(m, "\n\tGroups:\t"); | |
9517 | gi = cred->group_info; | |
9518 | for (g = 0; g < gi->ngroups; g++) { | |
9519 | seq_put_decimal_ull(m, g ? " " : "", | |
9520 | from_kgid_munged(uns, gi->gid[g])); | |
9521 | } | |
9522 | seq_puts(m, "\n\tCapEff:\t"); | |
9523 | cap = cred->cap_effective; | |
9524 | CAP_FOR_EACH_U32(__capi) | |
9525 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9526 | seq_putc(m, '\n'); | |
9527 | return 0; | |
9528 | } | |
9529 | ||
9530 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9531 | { | |
dbbe9c64 | 9532 | struct io_sq_data *sq = NULL; |
fad8e0de | 9533 | bool has_lock; |
87ce955b JA |
9534 | int i; |
9535 | ||
fad8e0de JA |
9536 | /* |
9537 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9538 | * since fdinfo case grabs it in the opposite direction of normal use | |
9539 | * cases. If we fail to get the lock, we just don't iterate any | |
9540 | * structures that could be going away outside the io_uring mutex. | |
9541 | */ | |
9542 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9543 | ||
dbbe9c64 JQ |
9544 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9545 | sq = ctx->sq_data; | |
9546 | ||
9547 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9548 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9549 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9550 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9551 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9552 | |
87ce955b JA |
9553 | if (f) |
9554 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9555 | else | |
9556 | seq_printf(m, "%5u: <none>\n", i); | |
9557 | } | |
9558 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9559 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9560 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9561 | ||
9562 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9563 | (unsigned int) buf->len); | |
9564 | } | |
fad8e0de | 9565 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9566 | seq_printf(m, "Personalities:\n"); |
9567 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9568 | } | |
d7718a9d JA |
9569 | seq_printf(m, "PollList:\n"); |
9570 | spin_lock_irq(&ctx->completion_lock); | |
9571 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9572 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9573 | struct io_kiocb *req; | |
9574 | ||
9575 | hlist_for_each_entry(req, list, hash_node) | |
9576 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9577 | req->task->task_works != NULL); | |
9578 | } | |
9579 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9580 | if (has_lock) |
9581 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9582 | } |
9583 | ||
9584 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9585 | { | |
9586 | struct io_ring_ctx *ctx = f->private_data; | |
9587 | ||
9588 | if (percpu_ref_tryget(&ctx->refs)) { | |
9589 | __io_uring_show_fdinfo(ctx, m); | |
9590 | percpu_ref_put(&ctx->refs); | |
9591 | } | |
9592 | } | |
bebdb65e | 9593 | #endif |
87ce955b | 9594 | |
2b188cc1 JA |
9595 | static const struct file_operations io_uring_fops = { |
9596 | .release = io_uring_release, | |
fcb323cc | 9597 | .flush = io_uring_flush, |
2b188cc1 | 9598 | .mmap = io_uring_mmap, |
6c5c240e RP |
9599 | #ifndef CONFIG_MMU |
9600 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9601 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9602 | #endif | |
2b188cc1 JA |
9603 | .poll = io_uring_poll, |
9604 | .fasync = io_uring_fasync, | |
bebdb65e | 9605 | #ifdef CONFIG_PROC_FS |
87ce955b | 9606 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9607 | #endif |
2b188cc1 JA |
9608 | }; |
9609 | ||
9610 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9611 | struct io_uring_params *p) | |
9612 | { | |
75b28aff HV |
9613 | struct io_rings *rings; |
9614 | size_t size, sq_array_offset; | |
2b188cc1 | 9615 | |
bd740481 JA |
9616 | /* make sure these are sane, as we already accounted them */ |
9617 | ctx->sq_entries = p->sq_entries; | |
9618 | ctx->cq_entries = p->cq_entries; | |
9619 | ||
75b28aff HV |
9620 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9621 | if (size == SIZE_MAX) | |
9622 | return -EOVERFLOW; | |
9623 | ||
9624 | rings = io_mem_alloc(size); | |
9625 | if (!rings) | |
2b188cc1 JA |
9626 | return -ENOMEM; |
9627 | ||
75b28aff HV |
9628 | ctx->rings = rings; |
9629 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9630 | rings->sq_ring_mask = p->sq_entries - 1; | |
9631 | rings->cq_ring_mask = p->cq_entries - 1; | |
9632 | rings->sq_ring_entries = p->sq_entries; | |
9633 | rings->cq_ring_entries = p->cq_entries; | |
9634 | ctx->sq_mask = rings->sq_ring_mask; | |
9635 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9636 | |
9637 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9638 | if (size == SIZE_MAX) { |
9639 | io_mem_free(ctx->rings); | |
9640 | ctx->rings = NULL; | |
2b188cc1 | 9641 | return -EOVERFLOW; |
eb065d30 | 9642 | } |
2b188cc1 JA |
9643 | |
9644 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9645 | if (!ctx->sq_sqes) { |
9646 | io_mem_free(ctx->rings); | |
9647 | ctx->rings = NULL; | |
2b188cc1 | 9648 | return -ENOMEM; |
eb065d30 | 9649 | } |
2b188cc1 | 9650 | |
2b188cc1 JA |
9651 | return 0; |
9652 | } | |
9653 | ||
9faadcc8 PB |
9654 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9655 | { | |
9656 | int ret, fd; | |
9657 | ||
9658 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9659 | if (fd < 0) | |
9660 | return fd; | |
9661 | ||
9662 | ret = io_uring_add_task_file(ctx, file); | |
9663 | if (ret) { | |
9664 | put_unused_fd(fd); | |
9665 | return ret; | |
9666 | } | |
9667 | fd_install(fd, file); | |
9668 | return fd; | |
9669 | } | |
9670 | ||
2b188cc1 JA |
9671 | /* |
9672 | * Allocate an anonymous fd, this is what constitutes the application | |
9673 | * visible backing of an io_uring instance. The application mmaps this | |
9674 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9675 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9676 | */ | |
9faadcc8 | 9677 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9678 | { |
9679 | struct file *file; | |
9faadcc8 | 9680 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9681 | int ret; |
9682 | ||
2b188cc1 JA |
9683 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9684 | &ctx->ring_sock); | |
9685 | if (ret) | |
9faadcc8 | 9686 | return ERR_PTR(ret); |
2b188cc1 JA |
9687 | #endif |
9688 | ||
2b188cc1 JA |
9689 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9690 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9691 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9692 | if (IS_ERR(file)) { |
9693 | sock_release(ctx->ring_sock); | |
9694 | ctx->ring_sock = NULL; | |
9695 | } else { | |
9696 | ctx->ring_sock->file = file; | |
0f212204 | 9697 | } |
2b188cc1 | 9698 | #endif |
9faadcc8 | 9699 | return file; |
2b188cc1 JA |
9700 | } |
9701 | ||
7f13657d XW |
9702 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9703 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9704 | { |
9705 | struct user_struct *user = NULL; | |
9706 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9707 | struct file *file; |
2b188cc1 JA |
9708 | int ret; |
9709 | ||
8110c1a6 | 9710 | if (!entries) |
2b188cc1 | 9711 | return -EINVAL; |
8110c1a6 JA |
9712 | if (entries > IORING_MAX_ENTRIES) { |
9713 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9714 | return -EINVAL; | |
9715 | entries = IORING_MAX_ENTRIES; | |
9716 | } | |
2b188cc1 JA |
9717 | |
9718 | /* | |
9719 | * Use twice as many entries for the CQ ring. It's possible for the | |
9720 | * application to drive a higher depth than the size of the SQ ring, | |
9721 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9722 | * some flexibility in overcommitting a bit. If the application has |
9723 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9724 | * of CQ ring entries manually. | |
2b188cc1 JA |
9725 | */ |
9726 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9727 | if (p->flags & IORING_SETUP_CQSIZE) { |
9728 | /* | |
9729 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9730 | * to a power-of-two, if it isn't already. We do NOT impose | |
9731 | * any cq vs sq ring sizing. | |
9732 | */ | |
eb2667b3 | 9733 | if (!p->cq_entries) |
33a107f0 | 9734 | return -EINVAL; |
8110c1a6 JA |
9735 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9736 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9737 | return -EINVAL; | |
9738 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9739 | } | |
eb2667b3 JQ |
9740 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9741 | if (p->cq_entries < p->sq_entries) | |
9742 | return -EINVAL; | |
33a107f0 JA |
9743 | } else { |
9744 | p->cq_entries = 2 * p->sq_entries; | |
9745 | } | |
2b188cc1 JA |
9746 | |
9747 | user = get_uid(current_user()); | |
2b188cc1 JA |
9748 | |
9749 | ctx = io_ring_ctx_alloc(p); | |
9750 | if (!ctx) { | |
2b188cc1 JA |
9751 | free_uid(user); |
9752 | return -ENOMEM; | |
9753 | } | |
9754 | ctx->compat = in_compat_syscall(); | |
26bfa89e | 9755 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9756 | ctx->user = user; |
0b8c0ec7 | 9757 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9758 | #ifdef CONFIG_AUDIT |
9759 | ctx->loginuid = current->loginuid; | |
9760 | ctx->sessionid = current->sessionid; | |
9761 | #endif | |
2aede0e4 JA |
9762 | ctx->sqo_task = get_task_struct(current); |
9763 | ||
9764 | /* | |
9765 | * This is just grabbed for accounting purposes. When a process exits, | |
9766 | * the mm is exited and dropped before the files, hence we need to hang | |
9767 | * on to this mm purely for the purposes of being able to unaccount | |
9768 | * memory (locked/pinned vm). It's not used for anything else. | |
9769 | */ | |
6b7898eb | 9770 | mmgrab(current->mm); |
2aede0e4 | 9771 | ctx->mm_account = current->mm; |
6b7898eb | 9772 | |
91d8f519 DZ |
9773 | #ifdef CONFIG_BLK_CGROUP |
9774 | /* | |
9775 | * The sq thread will belong to the original cgroup it was inited in. | |
9776 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9777 | * issued bios will be associated with the closest cgroup later in the | |
9778 | * block layer. | |
9779 | */ | |
9780 | rcu_read_lock(); | |
9781 | ctx->sqo_blkcg_css = blkcg_css(); | |
9782 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9783 | rcu_read_unlock(); | |
9784 | if (!ret) { | |
9785 | /* don't init against a dying cgroup, have the user try again */ | |
9786 | ctx->sqo_blkcg_css = NULL; | |
9787 | ret = -ENODEV; | |
9788 | goto err; | |
9789 | } | |
9790 | #endif | |
2b188cc1 JA |
9791 | ret = io_allocate_scq_urings(ctx, p); |
9792 | if (ret) | |
9793 | goto err; | |
9794 | ||
7e84e1c7 | 9795 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9796 | if (ret) |
9797 | goto err; | |
9798 | ||
7e84e1c7 SG |
9799 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9800 | io_sq_offload_start(ctx); | |
9801 | ||
2b188cc1 | 9802 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9803 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9804 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9805 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9806 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9807 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9808 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9809 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9810 | |
9811 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9812 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9813 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9814 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9815 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9816 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9817 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9818 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9819 | |
7f13657d XW |
9820 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9821 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9822 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9823 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9824 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9825 | |
9826 | if (copy_to_user(params, p, sizeof(*p))) { | |
9827 | ret = -EFAULT; | |
9828 | goto err; | |
9829 | } | |
d1719f70 | 9830 | |
9faadcc8 PB |
9831 | file = io_uring_get_file(ctx); |
9832 | if (IS_ERR(file)) { | |
9833 | ret = PTR_ERR(file); | |
9834 | goto err; | |
9835 | } | |
9836 | ||
044c1ab3 JA |
9837 | /* |
9838 | * Install ring fd as the very last thing, so we don't risk someone | |
9839 | * having closed it before we finish setup | |
9840 | */ | |
9faadcc8 PB |
9841 | ret = io_uring_install_fd(ctx, file); |
9842 | if (ret < 0) { | |
06585c49 | 9843 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9844 | /* fput will clean it up */ |
9845 | fput(file); | |
9846 | return ret; | |
9847 | } | |
044c1ab3 | 9848 | |
c826bd7a | 9849 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9850 | return ret; |
9851 | err: | |
d9d05217 | 9852 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9853 | io_ring_ctx_wait_and_kill(ctx); |
9854 | return ret; | |
9855 | } | |
9856 | ||
9857 | /* | |
9858 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9859 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9860 | * params structure passed in. | |
9861 | */ | |
9862 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9863 | { | |
9864 | struct io_uring_params p; | |
2b188cc1 JA |
9865 | int i; |
9866 | ||
9867 | if (copy_from_user(&p, params, sizeof(p))) | |
9868 | return -EFAULT; | |
9869 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9870 | if (p.resv[i]) | |
9871 | return -EINVAL; | |
9872 | } | |
9873 | ||
6c271ce2 | 9874 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9875 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9876 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9877 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9878 | return -EINVAL; |
9879 | ||
7f13657d | 9880 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9881 | } |
9882 | ||
9883 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9884 | struct io_uring_params __user *, params) | |
9885 | { | |
9886 | return io_uring_setup(entries, params); | |
9887 | } | |
9888 | ||
66f4af93 JA |
9889 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9890 | { | |
9891 | struct io_uring_probe *p; | |
9892 | size_t size; | |
9893 | int i, ret; | |
9894 | ||
9895 | size = struct_size(p, ops, nr_args); | |
9896 | if (size == SIZE_MAX) | |
9897 | return -EOVERFLOW; | |
9898 | p = kzalloc(size, GFP_KERNEL); | |
9899 | if (!p) | |
9900 | return -ENOMEM; | |
9901 | ||
9902 | ret = -EFAULT; | |
9903 | if (copy_from_user(p, arg, size)) | |
9904 | goto out; | |
9905 | ret = -EINVAL; | |
9906 | if (memchr_inv(p, 0, size)) | |
9907 | goto out; | |
9908 | ||
9909 | p->last_op = IORING_OP_LAST - 1; | |
9910 | if (nr_args > IORING_OP_LAST) | |
9911 | nr_args = IORING_OP_LAST; | |
9912 | ||
9913 | for (i = 0; i < nr_args; i++) { | |
9914 | p->ops[i].op = i; | |
9915 | if (!io_op_defs[i].not_supported) | |
9916 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9917 | } | |
9918 | p->ops_len = i; | |
9919 | ||
9920 | ret = 0; | |
9921 | if (copy_to_user(arg, p, size)) | |
9922 | ret = -EFAULT; | |
9923 | out: | |
9924 | kfree(p); | |
9925 | return ret; | |
9926 | } | |
9927 | ||
071698e1 JA |
9928 | static int io_register_personality(struct io_ring_ctx *ctx) |
9929 | { | |
1e6fa521 JA |
9930 | struct io_identity *id; |
9931 | int ret; | |
071698e1 | 9932 | |
1e6fa521 JA |
9933 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9934 | if (unlikely(!id)) | |
9935 | return -ENOMEM; | |
9936 | ||
9937 | io_init_identity(id); | |
9938 | id->creds = get_current_cred(); | |
9939 | ||
9940 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9941 | if (ret < 0) { | |
9942 | put_cred(id->creds); | |
9943 | kfree(id); | |
9944 | } | |
9945 | return ret; | |
071698e1 JA |
9946 | } |
9947 | ||
21b55dbc SG |
9948 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9949 | unsigned int nr_args) | |
9950 | { | |
9951 | struct io_uring_restriction *res; | |
9952 | size_t size; | |
9953 | int i, ret; | |
9954 | ||
7e84e1c7 SG |
9955 | /* Restrictions allowed only if rings started disabled */ |
9956 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9957 | return -EBADFD; | |
9958 | ||
21b55dbc | 9959 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9960 | if (ctx->restrictions.registered) |
21b55dbc SG |
9961 | return -EBUSY; |
9962 | ||
9963 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9964 | return -EINVAL; | |
9965 | ||
9966 | size = array_size(nr_args, sizeof(*res)); | |
9967 | if (size == SIZE_MAX) | |
9968 | return -EOVERFLOW; | |
9969 | ||
9970 | res = memdup_user(arg, size); | |
9971 | if (IS_ERR(res)) | |
9972 | return PTR_ERR(res); | |
9973 | ||
9974 | ret = 0; | |
9975 | ||
9976 | for (i = 0; i < nr_args; i++) { | |
9977 | switch (res[i].opcode) { | |
9978 | case IORING_RESTRICTION_REGISTER_OP: | |
9979 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9980 | ret = -EINVAL; | |
9981 | goto out; | |
9982 | } | |
9983 | ||
9984 | __set_bit(res[i].register_op, | |
9985 | ctx->restrictions.register_op); | |
9986 | break; | |
9987 | case IORING_RESTRICTION_SQE_OP: | |
9988 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9989 | ret = -EINVAL; | |
9990 | goto out; | |
9991 | } | |
9992 | ||
9993 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9994 | break; | |
9995 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9996 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9997 | break; | |
9998 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9999 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10000 | break; | |
10001 | default: | |
10002 | ret = -EINVAL; | |
10003 | goto out; | |
10004 | } | |
10005 | } | |
10006 | ||
10007 | out: | |
10008 | /* Reset all restrictions if an error happened */ | |
10009 | if (ret != 0) | |
10010 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10011 | else | |
7e84e1c7 | 10012 | ctx->restrictions.registered = true; |
21b55dbc SG |
10013 | |
10014 | kfree(res); | |
10015 | return ret; | |
10016 | } | |
10017 | ||
7e84e1c7 SG |
10018 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10019 | { | |
10020 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10021 | return -EBADFD; | |
10022 | ||
10023 | if (ctx->restrictions.registered) | |
10024 | ctx->restricted = 1; | |
10025 | ||
10026 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
10027 | ||
10028 | io_sq_offload_start(ctx); | |
10029 | ||
10030 | return 0; | |
10031 | } | |
10032 | ||
071698e1 JA |
10033 | static bool io_register_op_must_quiesce(int op) |
10034 | { | |
10035 | switch (op) { | |
10036 | case IORING_UNREGISTER_FILES: | |
10037 | case IORING_REGISTER_FILES_UPDATE: | |
10038 | case IORING_REGISTER_PROBE: | |
10039 | case IORING_REGISTER_PERSONALITY: | |
10040 | case IORING_UNREGISTER_PERSONALITY: | |
10041 | return false; | |
10042 | default: | |
10043 | return true; | |
10044 | } | |
10045 | } | |
10046 | ||
edafccee JA |
10047 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10048 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10049 | __releases(ctx->uring_lock) |
10050 | __acquires(ctx->uring_lock) | |
edafccee JA |
10051 | { |
10052 | int ret; | |
10053 | ||
35fa71a0 JA |
10054 | /* |
10055 | * We're inside the ring mutex, if the ref is already dying, then | |
10056 | * someone else killed the ctx or is already going through | |
10057 | * io_uring_register(). | |
10058 | */ | |
10059 | if (percpu_ref_is_dying(&ctx->refs)) | |
10060 | return -ENXIO; | |
10061 | ||
071698e1 | 10062 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10063 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10064 | |
05f3fb3c JA |
10065 | /* |
10066 | * Drop uring mutex before waiting for references to exit. If | |
10067 | * another thread is currently inside io_uring_enter() it might | |
10068 | * need to grab the uring_lock to make progress. If we hold it | |
10069 | * here across the drain wait, then we can deadlock. It's safe | |
10070 | * to drop the mutex here, since no new references will come in | |
10071 | * after we've killed the percpu ref. | |
10072 | */ | |
10073 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10074 | do { |
10075 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10076 | if (!ret) | |
10077 | break; | |
ed6930c9 JA |
10078 | ret = io_run_task_work_sig(); |
10079 | if (ret < 0) | |
10080 | break; | |
af9c1a44 JA |
10081 | } while (1); |
10082 | ||
05f3fb3c | 10083 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10084 | |
c150368b JA |
10085 | if (ret) { |
10086 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
10087 | goto out_quiesce; |
10088 | } | |
10089 | } | |
10090 | ||
10091 | if (ctx->restricted) { | |
10092 | if (opcode >= IORING_REGISTER_LAST) { | |
10093 | ret = -EINVAL; | |
10094 | goto out; | |
10095 | } | |
10096 | ||
10097 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
10098 | ret = -EACCES; | |
c150368b JA |
10099 | goto out; |
10100 | } | |
05f3fb3c | 10101 | } |
edafccee JA |
10102 | |
10103 | switch (opcode) { | |
10104 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 10105 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
10106 | break; |
10107 | case IORING_UNREGISTER_BUFFERS: | |
10108 | ret = -EINVAL; | |
10109 | if (arg || nr_args) | |
10110 | break; | |
0a96bbe4 | 10111 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10112 | break; |
6b06314c JA |
10113 | case IORING_REGISTER_FILES: |
10114 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
10115 | break; | |
10116 | case IORING_UNREGISTER_FILES: | |
10117 | ret = -EINVAL; | |
10118 | if (arg || nr_args) | |
10119 | break; | |
10120 | ret = io_sqe_files_unregister(ctx); | |
10121 | break; | |
c3a31e60 JA |
10122 | case IORING_REGISTER_FILES_UPDATE: |
10123 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
10124 | break; | |
9b402849 | 10125 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10126 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10127 | ret = -EINVAL; |
10128 | if (nr_args != 1) | |
10129 | break; | |
10130 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10131 | if (ret) |
10132 | break; | |
10133 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10134 | ctx->eventfd_async = 1; | |
10135 | else | |
10136 | ctx->eventfd_async = 0; | |
9b402849 JA |
10137 | break; |
10138 | case IORING_UNREGISTER_EVENTFD: | |
10139 | ret = -EINVAL; | |
10140 | if (arg || nr_args) | |
10141 | break; | |
10142 | ret = io_eventfd_unregister(ctx); | |
10143 | break; | |
66f4af93 JA |
10144 | case IORING_REGISTER_PROBE: |
10145 | ret = -EINVAL; | |
10146 | if (!arg || nr_args > 256) | |
10147 | break; | |
10148 | ret = io_probe(ctx, arg, nr_args); | |
10149 | break; | |
071698e1 JA |
10150 | case IORING_REGISTER_PERSONALITY: |
10151 | ret = -EINVAL; | |
10152 | if (arg || nr_args) | |
10153 | break; | |
10154 | ret = io_register_personality(ctx); | |
10155 | break; | |
10156 | case IORING_UNREGISTER_PERSONALITY: | |
10157 | ret = -EINVAL; | |
10158 | if (arg) | |
10159 | break; | |
10160 | ret = io_unregister_personality(ctx, nr_args); | |
10161 | break; | |
7e84e1c7 SG |
10162 | case IORING_REGISTER_ENABLE_RINGS: |
10163 | ret = -EINVAL; | |
10164 | if (arg || nr_args) | |
10165 | break; | |
10166 | ret = io_register_enable_rings(ctx); | |
10167 | break; | |
21b55dbc SG |
10168 | case IORING_REGISTER_RESTRICTIONS: |
10169 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10170 | break; | |
edafccee JA |
10171 | default: |
10172 | ret = -EINVAL; | |
10173 | break; | |
10174 | } | |
10175 | ||
21b55dbc | 10176 | out: |
071698e1 | 10177 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10178 | /* bring the ctx back to life */ |
05f3fb3c | 10179 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 10180 | out_quiesce: |
0f158b4c | 10181 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10182 | } |
edafccee JA |
10183 | return ret; |
10184 | } | |
10185 | ||
10186 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10187 | void __user *, arg, unsigned int, nr_args) | |
10188 | { | |
10189 | struct io_ring_ctx *ctx; | |
10190 | long ret = -EBADF; | |
10191 | struct fd f; | |
10192 | ||
10193 | f = fdget(fd); | |
10194 | if (!f.file) | |
10195 | return -EBADF; | |
10196 | ||
10197 | ret = -EOPNOTSUPP; | |
10198 | if (f.file->f_op != &io_uring_fops) | |
10199 | goto out_fput; | |
10200 | ||
10201 | ctx = f.file->private_data; | |
10202 | ||
b6c23dd5 PB |
10203 | io_run_task_work(); |
10204 | ||
edafccee JA |
10205 | mutex_lock(&ctx->uring_lock); |
10206 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10207 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10208 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10209 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10210 | out_fput: |
10211 | fdput(f); | |
10212 | return ret; | |
10213 | } | |
10214 | ||
2b188cc1 JA |
10215 | static int __init io_uring_init(void) |
10216 | { | |
d7f62e82 SM |
10217 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10218 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10219 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10220 | } while (0) | |
10221 | ||
10222 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10223 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10224 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10225 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10226 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10227 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10228 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10229 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10230 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10231 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10232 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10233 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10234 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10235 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10236 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10237 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10238 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10239 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10240 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10241 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10242 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10243 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10244 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10245 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10246 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10247 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10248 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10249 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10250 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10251 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10252 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10253 | |
d3656344 | 10254 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10255 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
10256 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10257 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10258 | return 0; |
10259 | }; | |
10260 | __initcall(io_uring_init); |