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 | ||
88f171ab PB |
1109 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1110 | { | |
1111 | if (!percpu_ref_tryget(ref)) { | |
1112 | /* already at zero, wait for ->release() */ | |
1113 | if (!try_wait_for_completion(compl)) | |
1114 | synchronize_rcu(); | |
1115 | return false; | |
1116 | } | |
1117 | ||
1118 | percpu_ref_resurrect(ref); | |
1119 | reinit_completion(compl); | |
1120 | percpu_ref_put(ref); | |
1121 | return true; | |
1122 | } | |
1123 | ||
08d23634 PB |
1124 | static bool io_match_task(struct io_kiocb *head, |
1125 | struct task_struct *task, | |
1126 | struct files_struct *files) | |
1127 | { | |
1128 | struct io_kiocb *req; | |
1129 | ||
84965ff8 JA |
1130 | if (task && head->task != task) { |
1131 | /* in terms of cancelation, always match if req task is dead */ | |
1132 | if (head->task->flags & PF_EXITING) | |
1133 | return true; | |
08d23634 | 1134 | return false; |
84965ff8 | 1135 | } |
08d23634 PB |
1136 | if (!files) |
1137 | return true; | |
1138 | ||
1139 | io_for_each_link(req, head) { | |
02a13674 JA |
1140 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1141 | continue; | |
1142 | if (req->file && req->file->f_op == &io_uring_fops) | |
1143 | return true; | |
1144 | if ((req->work.flags & IO_WQ_WORK_FILES) && | |
08d23634 PB |
1145 | req->work.identity->files == files) |
1146 | return true; | |
1147 | } | |
1148 | return false; | |
1149 | } | |
1150 | ||
28cea78a | 1151 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1152 | { |
28cea78a | 1153 | struct files_struct *files = current->files; |
c40f6379 JA |
1154 | struct mm_struct *mm = current->mm; |
1155 | ||
1156 | if (mm) { | |
1157 | kthread_unuse_mm(mm); | |
1158 | mmput(mm); | |
4b70cf9d | 1159 | current->mm = NULL; |
c40f6379 | 1160 | } |
28cea78a JA |
1161 | if (files) { |
1162 | struct nsproxy *nsproxy = current->nsproxy; | |
1163 | ||
1164 | task_lock(current); | |
1165 | current->files = NULL; | |
1166 | current->nsproxy = NULL; | |
1167 | task_unlock(current); | |
1168 | put_files_struct(files); | |
1169 | put_nsproxy(nsproxy); | |
1170 | } | |
1171 | } | |
1172 | ||
1a38ffc9 | 1173 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a JA |
1174 | { |
1175 | if (!current->files) { | |
1176 | struct files_struct *files; | |
1177 | struct nsproxy *nsproxy; | |
1178 | ||
1179 | task_lock(ctx->sqo_task); | |
1180 | files = ctx->sqo_task->files; | |
1181 | if (!files) { | |
1182 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1183 | return -EOWNERDEAD; |
28cea78a JA |
1184 | } |
1185 | atomic_inc(&files->count); | |
1186 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1187 | nsproxy = ctx->sqo_task->nsproxy; | |
1188 | task_unlock(ctx->sqo_task); | |
1189 | ||
1190 | task_lock(current); | |
1191 | current->files = files; | |
1192 | current->nsproxy = nsproxy; | |
1193 | task_unlock(current); | |
1194 | } | |
1a38ffc9 | 1195 | return 0; |
c40f6379 JA |
1196 | } |
1197 | ||
1198 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1199 | { | |
4b70cf9d JA |
1200 | struct mm_struct *mm; |
1201 | ||
1202 | if (current->mm) | |
1203 | return 0; | |
1204 | ||
4b70cf9d JA |
1205 | task_lock(ctx->sqo_task); |
1206 | mm = ctx->sqo_task->mm; | |
1207 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1208 | mm = NULL; | |
1209 | task_unlock(ctx->sqo_task); | |
1210 | ||
1211 | if (mm) { | |
1212 | kthread_use_mm(mm); | |
1213 | return 0; | |
c40f6379 JA |
1214 | } |
1215 | ||
4b70cf9d | 1216 | return -EFAULT; |
c40f6379 JA |
1217 | } |
1218 | ||
4e326358 PB |
1219 | static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1220 | struct io_kiocb *req) | |
c40f6379 | 1221 | { |
28cea78a | 1222 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1223 | int ret; |
28cea78a JA |
1224 | |
1225 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1226 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1227 | if (unlikely(ret)) |
1228 | return ret; | |
1229 | } | |
1230 | ||
1a38ffc9 PB |
1231 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1232 | ret = __io_sq_thread_acquire_files(ctx); | |
1233 | if (unlikely(ret)) | |
1234 | return ret; | |
1235 | } | |
28cea78a JA |
1236 | |
1237 | return 0; | |
c40f6379 JA |
1238 | } |
1239 | ||
4e326358 PB |
1240 | static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1241 | struct io_kiocb *req) | |
1242 | { | |
4e326358 PB |
1243 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
1244 | return 0; | |
1245 | return __io_sq_thread_acquire_mm_files(ctx, req); | |
1246 | } | |
1247 | ||
91d8f519 DZ |
1248 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1249 | struct cgroup_subsys_state **cur_css) | |
1250 | ||
1251 | { | |
1252 | #ifdef CONFIG_BLK_CGROUP | |
1253 | /* puts the old one when swapping */ | |
1254 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1255 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1256 | *cur_css = ctx->sqo_blkcg_css; | |
1257 | } | |
1258 | #endif | |
1259 | } | |
1260 | ||
1261 | static void io_sq_thread_unassociate_blkcg(void) | |
1262 | { | |
1263 | #ifdef CONFIG_BLK_CGROUP | |
1264 | kthread_associate_blkcg(NULL); | |
1265 | #endif | |
1266 | } | |
1267 | ||
c40f6379 JA |
1268 | static inline void req_set_fail_links(struct io_kiocb *req) |
1269 | { | |
1270 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1271 | req->flags |= REQ_F_FAIL_LINK; | |
1272 | } | |
4a38aed2 | 1273 | |
1e6fa521 JA |
1274 | /* |
1275 | * None of these are dereferenced, they are simply used to check if any of | |
1276 | * them have changed. If we're under current and check they are still the | |
1277 | * same, we're fine to grab references to them for actual out-of-line use. | |
1278 | */ | |
1279 | static void io_init_identity(struct io_identity *id) | |
1280 | { | |
1281 | id->files = current->files; | |
1282 | id->mm = current->mm; | |
1283 | #ifdef CONFIG_BLK_CGROUP | |
1284 | rcu_read_lock(); | |
1285 | id->blkcg_css = blkcg_css(); | |
1286 | rcu_read_unlock(); | |
1287 | #endif | |
1288 | id->creds = current_cred(); | |
1289 | id->nsproxy = current->nsproxy; | |
1290 | id->fs = current->fs; | |
1291 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1292 | #ifdef CONFIG_AUDIT |
1293 | id->loginuid = current->loginuid; | |
1294 | id->sessionid = current->sessionid; | |
1295 | #endif | |
1e6fa521 JA |
1296 | refcount_set(&id->count, 1); |
1297 | } | |
1298 | ||
ec99ca6c PB |
1299 | static inline void __io_req_init_async(struct io_kiocb *req) |
1300 | { | |
1301 | memset(&req->work, 0, sizeof(req->work)); | |
1302 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1303 | } | |
1304 | ||
7cdaf587 XW |
1305 | /* |
1306 | * Note: must call io_req_init_async() for the first time you | |
1307 | * touch any members of io_wq_work. | |
1308 | */ | |
1309 | static inline void io_req_init_async(struct io_kiocb *req) | |
1310 | { | |
500a373d JA |
1311 | struct io_uring_task *tctx = current->io_uring; |
1312 | ||
7cdaf587 XW |
1313 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1314 | return; | |
1315 | ||
ec99ca6c | 1316 | __io_req_init_async(req); |
500a373d JA |
1317 | |
1318 | /* Grab a ref if this isn't our static identity */ | |
1319 | req->work.identity = tctx->identity; | |
1320 | if (tctx->identity != &tctx->__identity) | |
1321 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1322 | } |
1323 | ||
2b188cc1 JA |
1324 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1325 | { | |
1326 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1327 | ||
0f158b4c | 1328 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1329 | } |
1330 | ||
8eb7e2d0 PB |
1331 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1332 | { | |
1333 | return !req->timeout.off; | |
1334 | } | |
1335 | ||
2b188cc1 JA |
1336 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1337 | { | |
1338 | struct io_ring_ctx *ctx; | |
78076bb6 | 1339 | int hash_bits; |
2b188cc1 JA |
1340 | |
1341 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1342 | if (!ctx) | |
1343 | return NULL; | |
1344 | ||
78076bb6 JA |
1345 | /* |
1346 | * Use 5 bits less than the max cq entries, that should give us around | |
1347 | * 32 entries per hash list if totally full and uniformly spread. | |
1348 | */ | |
1349 | hash_bits = ilog2(p->cq_entries); | |
1350 | hash_bits -= 5; | |
1351 | if (hash_bits <= 0) | |
1352 | hash_bits = 1; | |
1353 | ctx->cancel_hash_bits = hash_bits; | |
1354 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1355 | GFP_KERNEL); | |
1356 | if (!ctx->cancel_hash) | |
1357 | goto err; | |
1358 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1359 | ||
21482896 | 1360 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1361 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1362 | goto err; | |
2b188cc1 JA |
1363 | |
1364 | ctx->flags = p->flags; | |
90554200 | 1365 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1366 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1367 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1368 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1369 | init_completion(&ctx->ref_comp); |
1370 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1371 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1372 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1373 | mutex_init(&ctx->uring_lock); |
1374 | init_waitqueue_head(&ctx->wait); | |
1375 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1376 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1377 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1378 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1379 | spin_lock_init(&ctx->inflight_lock); |
1380 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1381 | spin_lock_init(&ctx->rsrc_ref_lock); |
1382 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1383 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1384 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1385 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1386 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1387 | return ctx; |
206aefde | 1388 | err: |
78076bb6 | 1389 | kfree(ctx->cancel_hash); |
206aefde JA |
1390 | kfree(ctx); |
1391 | return NULL; | |
2b188cc1 JA |
1392 | } |
1393 | ||
9cf7c104 | 1394 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1395 | { |
2bc9930e JA |
1396 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1397 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1398 | |
9cf7c104 | 1399 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1400 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1401 | } |
de0617e4 | 1402 | |
9d858b21 | 1403 | return false; |
de0617e4 JA |
1404 | } |
1405 | ||
5c3462cf | 1406 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1407 | { |
500a373d | 1408 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1409 | return; |
1410 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1411 | kfree(req->work.identity); | |
1412 | } | |
1413 | ||
4edf20f9 | 1414 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1415 | { |
7cdaf587 | 1416 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1417 | return; |
51a4cc11 | 1418 | |
e86d0047 | 1419 | if (req->work.flags & IO_WQ_WORK_MM) |
98447d65 | 1420 | mmdrop(req->work.identity->mm); |
91d8f519 | 1421 | #ifdef CONFIG_BLK_CGROUP |
e86d0047 | 1422 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
98447d65 | 1423 | css_put(req->work.identity->blkcg_css); |
91d8f519 | 1424 | #endif |
e86d0047 | 1425 | if (req->work.flags & IO_WQ_WORK_CREDS) |
98447d65 | 1426 | put_cred(req->work.identity->creds); |
dfead8a8 | 1427 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1428 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1429 | |
98447d65 | 1430 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1431 | if (--fs->users) |
1432 | fs = NULL; | |
98447d65 | 1433 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1434 | if (fs) |
1435 | free_fs_struct(fs); | |
1436 | } | |
34e08fed PB |
1437 | if (req->work.flags & IO_WQ_WORK_FILES) { |
1438 | put_files_struct(req->work.identity->files); | |
1439 | put_nsproxy(req->work.identity->nsproxy); | |
34e08fed PB |
1440 | } |
1441 | if (req->flags & REQ_F_INFLIGHT) { | |
1442 | struct io_ring_ctx *ctx = req->ctx; | |
1443 | struct io_uring_task *tctx = req->task->io_uring; | |
1444 | unsigned long flags; | |
1445 | ||
1446 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1447 | list_del(&req->inflight_entry); | |
1448 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1449 | req->flags &= ~REQ_F_INFLIGHT; | |
1450 | if (atomic_read(&tctx->in_idle)) | |
1451 | wake_up(&tctx->wait); | |
1452 | } | |
51a4cc11 | 1453 | |
e86d0047 PB |
1454 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
1455 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | | |
1456 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); | |
5c3462cf | 1457 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1458 | } |
1459 | ||
1e6fa521 JA |
1460 | /* |
1461 | * Create a private copy of io_identity, since some fields don't match | |
1462 | * the current context. | |
1463 | */ | |
1464 | static bool io_identity_cow(struct io_kiocb *req) | |
1465 | { | |
5c3462cf | 1466 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1467 | const struct cred *creds = NULL; |
1468 | struct io_identity *id; | |
1469 | ||
1470 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1471 | creds = req->work.identity->creds; | |
1472 | ||
1473 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1474 | if (unlikely(!id)) { | |
1475 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1476 | return false; | |
1477 | } | |
1478 | ||
1479 | /* | |
1480 | * We can safely just re-init the creds we copied Either the field | |
1481 | * matches the current one, or we haven't grabbed it yet. The only | |
1482 | * exception is ->creds, through registered personalities, so handle | |
1483 | * that one separately. | |
1484 | */ | |
1485 | io_init_identity(id); | |
1486 | if (creds) | |
e8c954df | 1487 | id->creds = creds; |
1e6fa521 JA |
1488 | |
1489 | /* add one for this request */ | |
1490 | refcount_inc(&id->count); | |
1491 | ||
cb8a8ae3 JA |
1492 | /* drop tctx and req identity references, if needed */ |
1493 | if (tctx->identity != &tctx->__identity && | |
1494 | refcount_dec_and_test(&tctx->identity->count)) | |
1495 | kfree(tctx->identity); | |
1496 | if (req->work.identity != &tctx->__identity && | |
1497 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1498 | kfree(req->work.identity); |
1499 | ||
1500 | req->work.identity = id; | |
500a373d | 1501 | tctx->identity = id; |
1e6fa521 JA |
1502 | return true; |
1503 | } | |
1504 | ||
ce3d5aae PB |
1505 | static void io_req_track_inflight(struct io_kiocb *req) |
1506 | { | |
1507 | struct io_ring_ctx *ctx = req->ctx; | |
1508 | ||
1509 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1510 | io_req_init_async(req); | |
1511 | req->flags |= REQ_F_INFLIGHT; | |
1512 | ||
1513 | spin_lock_irq(&ctx->inflight_lock); | |
1514 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1515 | spin_unlock_irq(&ctx->inflight_lock); | |
1516 | } | |
1517 | } | |
1518 | ||
1e6fa521 | 1519 | static bool io_grab_identity(struct io_kiocb *req) |
18d9be1a | 1520 | { |
d3656344 | 1521 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1522 | struct io_identity *id = req->work.identity; |
54a91f3b | 1523 | |
69228338 JA |
1524 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1525 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1526 | return false; | |
1527 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1528 | } | |
91d8f519 | 1529 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1530 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1531 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1532 | rcu_read_lock(); |
1e6fa521 JA |
1533 | if (id->blkcg_css != blkcg_css()) { |
1534 | rcu_read_unlock(); | |
1535 | return false; | |
1536 | } | |
91d8f519 DZ |
1537 | /* |
1538 | * This should be rare, either the cgroup is dying or the task | |
1539 | * is moving cgroups. Just punt to root for the handful of ios. | |
1540 | */ | |
1e6fa521 | 1541 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1542 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1543 | rcu_read_unlock(); |
1544 | } | |
1545 | #endif | |
dfead8a8 | 1546 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1547 | if (id->creds != current_cred()) |
1548 | return false; | |
1549 | get_cred(id->creds); | |
dfead8a8 JA |
1550 | req->work.flags |= IO_WQ_WORK_CREDS; |
1551 | } | |
4ea33a97 JA |
1552 | #ifdef CONFIG_AUDIT |
1553 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1554 | current->sessionid != id->sessionid) | |
1555 | return false; | |
1556 | #endif | |
dfead8a8 JA |
1557 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1558 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1559 | if (current->fs != id->fs) |
1560 | return false; | |
1561 | spin_lock(&id->fs->lock); | |
1562 | if (!id->fs->in_exec) { | |
1563 | id->fs->users++; | |
dfead8a8 | 1564 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1565 | } else { |
1566 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1567 | } | |
1568 | spin_unlock(¤t->fs->lock); | |
1569 | } | |
af604703 PB |
1570 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1571 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1572 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1573 | if (id->files != current->files || | |
1574 | id->nsproxy != current->nsproxy) | |
1575 | return false; | |
1576 | atomic_inc(&id->files->count); | |
1577 | get_nsproxy(id->nsproxy); | |
af604703 | 1578 | req->work.flags |= IO_WQ_WORK_FILES; |
ce3d5aae | 1579 | io_req_track_inflight(req); |
af604703 | 1580 | } |
77788775 JA |
1581 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1582 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1583 | if (id->mm != current->mm) | |
1584 | return false; | |
1585 | mmgrab(id->mm); | |
1586 | req->work.flags |= IO_WQ_WORK_MM; | |
1587 | } | |
1e6fa521 JA |
1588 | |
1589 | return true; | |
1590 | } | |
1591 | ||
1592 | static void io_prep_async_work(struct io_kiocb *req) | |
1593 | { | |
1594 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1595 | struct io_ring_ctx *ctx = req->ctx; |
1596 | ||
1597 | io_req_init_async(req); | |
1598 | ||
feaadc4f PB |
1599 | if (req->flags & REQ_F_FORCE_ASYNC) |
1600 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1601 | ||
1e6fa521 JA |
1602 | if (req->flags & REQ_F_ISREG) { |
1603 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1604 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1605 | } else { | |
1606 | if (def->unbound_nonreg_file) | |
1607 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1608 | } | |
1609 | ||
1e6fa521 JA |
1610 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1611 | if (io_grab_identity(req)) | |
1612 | return; | |
1613 | ||
1614 | if (!io_identity_cow(req)) | |
1615 | return; | |
1616 | ||
1617 | /* can't fail at this point */ | |
1618 | if (!io_grab_identity(req)) | |
1619 | WARN_ON(1); | |
561fb04a | 1620 | } |
cccf0ee8 | 1621 | |
cbdcb435 | 1622 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1623 | { |
cbdcb435 | 1624 | struct io_kiocb *cur; |
54a91f3b | 1625 | |
f2f87370 PB |
1626 | io_for_each_link(cur, req) |
1627 | io_prep_async_work(cur); | |
561fb04a JA |
1628 | } |
1629 | ||
7271ef3a | 1630 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1631 | { |
a197f664 | 1632 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1633 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1634 | |
8766dd51 PB |
1635 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1636 | &req->work, req->flags); | |
1637 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1638 | return link; |
18d9be1a JA |
1639 | } |
1640 | ||
cbdcb435 PB |
1641 | static void io_queue_async_work(struct io_kiocb *req) |
1642 | { | |
7271ef3a JA |
1643 | struct io_kiocb *link; |
1644 | ||
cbdcb435 PB |
1645 | /* init ->work of the whole link before punting */ |
1646 | io_prep_async_link(req); | |
7271ef3a JA |
1647 | link = __io_queue_async_work(req); |
1648 | ||
1649 | if (link) | |
1650 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1651 | } |
1652 | ||
5262f567 JA |
1653 | static void io_kill_timeout(struct io_kiocb *req) |
1654 | { | |
e8c2bc1f | 1655 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1656 | int ret; |
1657 | ||
e8c2bc1f | 1658 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1659 | if (ret != -1) { |
01cec8c1 PB |
1660 | atomic_set(&req->ctx->cq_timeouts, |
1661 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1662 | list_del_init(&req->timeout.list); |
78e19bbe | 1663 | io_cqring_fill_event(req, 0); |
216578e5 | 1664 | io_put_req_deferred(req, 1); |
5262f567 JA |
1665 | } |
1666 | } | |
1667 | ||
76e1b642 JA |
1668 | /* |
1669 | * Returns true if we found and killed one or more timeouts | |
1670 | */ | |
6b81928d PB |
1671 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1672 | struct files_struct *files) | |
5262f567 JA |
1673 | { |
1674 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1675 | int canceled = 0; |
5262f567 JA |
1676 | |
1677 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1678 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1679 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1680 | io_kill_timeout(req); |
76e1b642 JA |
1681 | canceled++; |
1682 | } | |
f3606e3a | 1683 | } |
5262f567 | 1684 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1685 | return canceled != 0; |
5262f567 JA |
1686 | } |
1687 | ||
04518945 | 1688 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1689 | { |
04518945 | 1690 | do { |
27dc8338 PB |
1691 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1692 | struct io_defer_entry, list); | |
de0617e4 | 1693 | |
9cf7c104 | 1694 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1695 | break; |
27dc8338 | 1696 | list_del_init(&de->list); |
907d1df3 | 1697 | io_req_task_queue(de->req); |
27dc8338 | 1698 | kfree(de); |
04518945 PB |
1699 | } while (!list_empty(&ctx->defer_list)); |
1700 | } | |
1701 | ||
360428f8 | 1702 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1703 | { |
f010505b MDG |
1704 | u32 seq; |
1705 | ||
1706 | if (list_empty(&ctx->timeout_list)) | |
1707 | return; | |
1708 | ||
1709 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1710 | ||
1711 | do { | |
1712 | u32 events_needed, events_got; | |
360428f8 | 1713 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1714 | struct io_kiocb, timeout.list); |
de0617e4 | 1715 | |
8eb7e2d0 | 1716 | if (io_is_timeout_noseq(req)) |
360428f8 | 1717 | break; |
f010505b MDG |
1718 | |
1719 | /* | |
1720 | * Since seq can easily wrap around over time, subtract | |
1721 | * the last seq at which timeouts were flushed before comparing. | |
1722 | * Assuming not more than 2^31-1 events have happened since, | |
1723 | * these subtractions won't have wrapped, so we can check if | |
1724 | * target is in [last_seq, current_seq] by comparing the two. | |
1725 | */ | |
1726 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1727 | events_got = seq - ctx->cq_last_tm_flush; | |
1728 | if (events_got < events_needed) | |
360428f8 | 1729 | break; |
bfe68a22 | 1730 | |
135fcde8 | 1731 | list_del_init(&req->timeout.list); |
5262f567 | 1732 | io_kill_timeout(req); |
f010505b MDG |
1733 | } while (!list_empty(&ctx->timeout_list)); |
1734 | ||
1735 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1736 | } |
5262f567 | 1737 | |
360428f8 PB |
1738 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1739 | { | |
1740 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1741 | |
1742 | /* order cqe stores with ring update */ | |
1743 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1744 | |
04518945 PB |
1745 | if (unlikely(!list_empty(&ctx->defer_list))) |
1746 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1747 | } |
1748 | ||
90554200 JA |
1749 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1750 | { | |
1751 | struct io_rings *r = ctx->rings; | |
1752 | ||
1753 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1754 | } | |
1755 | ||
888aae2e PB |
1756 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1757 | { | |
1758 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1759 | } | |
1760 | ||
2b188cc1 JA |
1761 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1762 | { | |
75b28aff | 1763 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1764 | unsigned tail; |
1765 | ||
115e12e5 SB |
1766 | /* |
1767 | * writes to the cq entry need to come after reading head; the | |
1768 | * control dependency is enough as we're using WRITE_ONCE to | |
1769 | * fill the cq entry | |
1770 | */ | |
888aae2e | 1771 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1772 | return NULL; |
1773 | ||
888aae2e | 1774 | tail = ctx->cached_cq_tail++; |
75b28aff | 1775 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1776 | } |
1777 | ||
f2842ab5 JA |
1778 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1779 | { | |
f0b493e6 JA |
1780 | if (!ctx->cq_ev_fd) |
1781 | return false; | |
7e55a19c SG |
1782 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1783 | return false; | |
f2842ab5 JA |
1784 | if (!ctx->eventfd_async) |
1785 | return true; | |
b41e9852 | 1786 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1787 | } |
1788 | ||
b41e9852 | 1789 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1790 | { |
b1445e59 PB |
1791 | /* see waitqueue_active() comment */ |
1792 | smp_mb(); | |
1793 | ||
1d7bb1d5 JA |
1794 | if (waitqueue_active(&ctx->wait)) |
1795 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1796 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1797 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1798 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1799 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1800 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1801 | wake_up_interruptible(&ctx->cq_wait); |
1802 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1803 | } | |
1d7bb1d5 JA |
1804 | } |
1805 | ||
80c18e4a PB |
1806 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1807 | { | |
b1445e59 PB |
1808 | /* see waitqueue_active() comment */ |
1809 | smp_mb(); | |
1810 | ||
80c18e4a PB |
1811 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1812 | if (waitqueue_active(&ctx->wait)) | |
1813 | wake_up(&ctx->wait); | |
1814 | } | |
1815 | if (io_should_trigger_evfd(ctx)) | |
1816 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1817 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1818 | wake_up_interruptible(&ctx->cq_wait); |
1819 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1820 | } | |
80c18e4a PB |
1821 | } |
1822 | ||
c4a2ed72 | 1823 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1824 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1825 | struct task_struct *tsk, | |
1826 | struct files_struct *files) | |
1d7bb1d5 JA |
1827 | { |
1828 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1829 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1830 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1831 | unsigned long flags; |
b18032bb | 1832 | bool all_flushed, posted; |
1d7bb1d5 JA |
1833 | LIST_HEAD(list); |
1834 | ||
e23de15f PB |
1835 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1836 | return false; | |
1d7bb1d5 | 1837 | |
b18032bb | 1838 | posted = false; |
1d7bb1d5 | 1839 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1840 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1841 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1842 | continue; |
1843 | ||
1d7bb1d5 JA |
1844 | cqe = io_get_cqring(ctx); |
1845 | if (!cqe && !force) | |
1846 | break; | |
1847 | ||
40d8ddd4 | 1848 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1849 | if (cqe) { |
1850 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1851 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1852 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1853 | } else { |
2c3bac6d | 1854 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1855 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1856 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1857 | } |
b18032bb | 1858 | posted = true; |
1d7bb1d5 JA |
1859 | } |
1860 | ||
09e88404 PB |
1861 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1862 | if (all_flushed) { | |
1863 | clear_bit(0, &ctx->sq_check_overflow); | |
1864 | clear_bit(0, &ctx->cq_check_overflow); | |
1865 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1866 | } | |
46930143 | 1867 | |
b18032bb JA |
1868 | if (posted) |
1869 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1870 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1871 | if (posted) |
1872 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1873 | |
1874 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1875 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1876 | list_del(&req->compl.list); | |
ec9c02ad | 1877 | io_put_req(req); |
1d7bb1d5 | 1878 | } |
c4a2ed72 | 1879 | |
09e88404 | 1880 | return all_flushed; |
1d7bb1d5 JA |
1881 | } |
1882 | ||
6c503150 PB |
1883 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1884 | struct task_struct *tsk, | |
1885 | struct files_struct *files) | |
1886 | { | |
1887 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1888 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1889 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1890 | mutex_lock(&ctx->uring_lock); | |
1891 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1892 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1893 | mutex_unlock(&ctx->uring_lock); | |
1894 | } | |
1895 | } | |
1896 | ||
bcda7baa | 1897 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1898 | { |
78e19bbe | 1899 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1900 | struct io_uring_cqe *cqe; |
1901 | ||
78e19bbe | 1902 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1903 | |
2b188cc1 JA |
1904 | /* |
1905 | * If we can't get a cq entry, userspace overflowed the | |
1906 | * submission (by quite a lot). Increment the overflow count in | |
1907 | * the ring. | |
1908 | */ | |
1909 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1910 | if (likely(cqe)) { |
78e19bbe | 1911 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1912 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1913 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1914 | } else if (ctx->cq_overflow_flushed || |
1915 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1916 | /* |
1917 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1918 | * then we cannot store the request for later flushing, we need | |
1919 | * to drop it on the floor. | |
1920 | */ | |
2c3bac6d PB |
1921 | ctx->cached_cq_overflow++; |
1922 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1923 | } else { |
ad3eb2c8 JA |
1924 | if (list_empty(&ctx->cq_overflow_list)) { |
1925 | set_bit(0, &ctx->sq_check_overflow); | |
1926 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1927 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1928 | } |
40d8ddd4 | 1929 | io_clean_op(req); |
1d7bb1d5 | 1930 | req->result = res; |
0f7e466b | 1931 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1932 | refcount_inc(&req->refs); |
1933 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1934 | } |
1935 | } | |
1936 | ||
bcda7baa JA |
1937 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1938 | { | |
1939 | __io_cqring_fill_event(req, res, 0); | |
1940 | } | |
1941 | ||
c7dae4ba JA |
1942 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1943 | unsigned int cflags) | |
2b188cc1 | 1944 | { |
78e19bbe | 1945 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1946 | unsigned long flags; |
1947 | ||
1948 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1949 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1950 | io_commit_cqring(ctx); |
c7dae4ba JA |
1951 | /* |
1952 | * If we're the last reference to this request, add to our locked | |
1953 | * free_list cache. | |
1954 | */ | |
1955 | if (refcount_dec_and_test(&req->refs)) { | |
1956 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1957 | ||
1958 | io_dismantle_req(req); | |
1959 | io_put_task(req->task, 1); | |
1960 | list_add(&req->compl.list, &cs->locked_free_list); | |
1961 | cs->locked_free_nr++; | |
1962 | } else | |
1963 | req = NULL; | |
2b188cc1 JA |
1964 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1965 | ||
8c838788 | 1966 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1967 | if (req) { |
1968 | io_queue_next(req); | |
1969 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1970 | } |
229a7b63 JA |
1971 | } |
1972 | ||
a38d68db | 1973 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1974 | unsigned int cflags) |
229a7b63 | 1975 | { |
a38d68db PB |
1976 | io_clean_op(req); |
1977 | req->result = res; | |
1978 | req->compl.cflags = cflags; | |
e342c807 | 1979 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1980 | } |
1981 | ||
889fca73 PB |
1982 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1983 | long res, unsigned cflags) | |
bcda7baa | 1984 | { |
889fca73 PB |
1985 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1986 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1987 | else |
c7dae4ba | 1988 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1989 | } |
1990 | ||
a38d68db | 1991 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1992 | { |
889fca73 | 1993 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1994 | } |
1995 | ||
c7dae4ba | 1996 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1997 | { |
c7dae4ba JA |
1998 | struct io_submit_state *state = &ctx->submit_state; |
1999 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 2000 | struct io_kiocb *req = NULL; |
0ddf92e8 | 2001 | |
c7dae4ba JA |
2002 | /* |
2003 | * If we have more than a batch's worth of requests in our IRQ side | |
2004 | * locked cache, grab the lock and move them over to our submission | |
2005 | * side cache. | |
2006 | */ | |
2007 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
2008 | spin_lock_irq(&ctx->completion_lock); | |
2009 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
2010 | cs->locked_free_nr = 0; | |
2011 | spin_unlock_irq(&ctx->completion_lock); | |
2012 | } | |
0ddf92e8 | 2013 | |
c7dae4ba JA |
2014 | while (!list_empty(&cs->free_list)) { |
2015 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
2016 | compl.list); |
2017 | list_del(&req->compl.list); | |
e5d1bc0a PB |
2018 | state->reqs[state->free_reqs++] = req; |
2019 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
2020 | break; | |
1b4c351f JA |
2021 | } |
2022 | ||
e5d1bc0a | 2023 | return req != NULL; |
0ddf92e8 JA |
2024 | } |
2025 | ||
e5d1bc0a | 2026 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 2027 | { |
e5d1bc0a PB |
2028 | struct io_submit_state *state = &ctx->submit_state; |
2029 | ||
2030 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
2031 | ||
f6b6c7d6 | 2032 | if (!state->free_reqs) { |
291b2821 | 2033 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
2034 | int ret; |
2035 | ||
c7dae4ba | 2036 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
2037 | goto got_req; |
2038 | ||
bf019da7 PB |
2039 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
2040 | state->reqs); | |
fd6fab2c JA |
2041 | |
2042 | /* | |
2043 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2044 | * retry single alloc to be on the safe side. | |
2045 | */ | |
2046 | if (unlikely(ret <= 0)) { | |
2047 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
2048 | if (!state->reqs[0]) | |
3893f39f | 2049 | return NULL; |
fd6fab2c JA |
2050 | ret = 1; |
2051 | } | |
291b2821 | 2052 | state->free_reqs = ret; |
2b188cc1 | 2053 | } |
e5d1bc0a | 2054 | got_req: |
291b2821 PB |
2055 | state->free_reqs--; |
2056 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
2057 | } |
2058 | ||
8da11c19 PB |
2059 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
2060 | bool fixed) | |
2061 | { | |
36f72fe2 | 2062 | if (!fixed) |
8da11c19 PB |
2063 | fput(file); |
2064 | } | |
2065 | ||
4edf20f9 | 2066 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2067 | { |
3ca405eb | 2068 | io_clean_op(req); |
929a3af9 | 2069 | |
e8c2bc1f JA |
2070 | if (req->async_data) |
2071 | kfree(req->async_data); | |
8da11c19 PB |
2072 | if (req->file) |
2073 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
2074 | if (req->fixed_rsrc_refs) |
2075 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 2076 | io_req_clean_work(req); |
e65ef56d JA |
2077 | } |
2078 | ||
7c660731 PB |
2079 | static inline void io_put_task(struct task_struct *task, int nr) |
2080 | { | |
2081 | struct io_uring_task *tctx = task->io_uring; | |
2082 | ||
2083 | percpu_counter_sub(&tctx->inflight, nr); | |
2084 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2085 | wake_up(&tctx->wait); | |
2086 | put_task_struct_many(task, nr); | |
2087 | } | |
2088 | ||
216578e5 | 2089 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2090 | { |
51a4cc11 | 2091 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2092 | |
216578e5 | 2093 | io_dismantle_req(req); |
7c660731 | 2094 | io_put_task(req->task, 1); |
c6ca97b3 | 2095 | |
3893f39f | 2096 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 2097 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
2098 | } |
2099 | ||
f2f87370 PB |
2100 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2101 | { | |
2102 | struct io_kiocb *nxt = req->link; | |
2103 | ||
2104 | req->link = nxt->link; | |
2105 | nxt->link = NULL; | |
2106 | } | |
2107 | ||
c9abd7ad | 2108 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 2109 | { |
a197f664 | 2110 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 2111 | struct io_kiocb *link; |
c9abd7ad PB |
2112 | bool cancelled = false; |
2113 | unsigned long flags; | |
7c86ffee | 2114 | |
c9abd7ad | 2115 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2116 | link = req->link; |
2117 | ||
900fad45 PB |
2118 | /* |
2119 | * Can happen if a linked timeout fired and link had been like | |
2120 | * req -> link t-out -> link t-out [-> ...] | |
2121 | */ | |
c9abd7ad PB |
2122 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
2123 | struct io_timeout_data *io = link->async_data; | |
2124 | int ret; | |
7c86ffee | 2125 | |
f2f87370 | 2126 | io_remove_next_linked(req); |
90cd7e42 | 2127 | link->timeout.head = NULL; |
c9abd7ad PB |
2128 | ret = hrtimer_try_to_cancel(&io->timer); |
2129 | if (ret != -1) { | |
2130 | io_cqring_fill_event(link, -ECANCELED); | |
2131 | io_commit_cqring(ctx); | |
2132 | cancelled = true; | |
2133 | } | |
2134 | } | |
7c86ffee | 2135 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2136 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2137 | |
c9abd7ad | 2138 | if (cancelled) { |
7c86ffee | 2139 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2140 | io_put_req(link); |
2141 | } | |
7c86ffee PB |
2142 | } |
2143 | ||
9e645e11 | 2144 | |
d148ca4b | 2145 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2146 | { |
f2f87370 | 2147 | struct io_kiocb *link, *nxt; |
2665abfd | 2148 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2149 | unsigned long flags; |
9e645e11 | 2150 | |
d148ca4b | 2151 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2152 | link = req->link; |
2153 | req->link = NULL; | |
9e645e11 | 2154 | |
f2f87370 PB |
2155 | while (link) { |
2156 | nxt = link->link; | |
2157 | link->link = NULL; | |
2665abfd | 2158 | |
f2f87370 | 2159 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2160 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2161 | |
2162 | /* | |
2163 | * It's ok to free under spinlock as they're not linked anymore, | |
2164 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2165 | * work.fs->lock. | |
2166 | */ | |
2167 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2168 | io_put_req_deferred(link, 2); | |
2169 | else | |
2170 | io_double_put_req(link); | |
f2f87370 | 2171 | link = nxt; |
9e645e11 | 2172 | } |
2665abfd | 2173 | io_commit_cqring(ctx); |
216578e5 | 2174 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2175 | |
2665abfd | 2176 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2177 | } |
2178 | ||
3fa5e0f3 | 2179 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2180 | { |
7c86ffee PB |
2181 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2182 | io_kill_linked_timeout(req); | |
944e58bf | 2183 | |
9e645e11 JA |
2184 | /* |
2185 | * If LINK is set, we have dependent requests in this chain. If we | |
2186 | * didn't fail this request, queue the first one up, moving any other | |
2187 | * dependencies to the next request. In case of failure, fail the rest | |
2188 | * of the chain. | |
2189 | */ | |
f2f87370 PB |
2190 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2191 | struct io_kiocb *nxt = req->link; | |
2192 | ||
2193 | req->link = NULL; | |
2194 | return nxt; | |
2195 | } | |
9b5f7bd9 PB |
2196 | io_fail_links(req); |
2197 | return NULL; | |
4d7dd462 | 2198 | } |
9e645e11 | 2199 | |
f2f87370 | 2200 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2201 | { |
cdbff982 | 2202 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2203 | return NULL; |
2204 | return __io_req_find_next(req); | |
2205 | } | |
2206 | ||
7cbf1722 | 2207 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 2208 | { |
65453d1e | 2209 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
2210 | struct io_wq_work_list list; |
2211 | struct io_wq_work_node *node; | |
c2c4c83c | 2212 | |
7cbf1722 JA |
2213 | if (wq_list_empty(&tctx->task_list)) |
2214 | return false; | |
6200b0ae | 2215 | |
0b81e80c | 2216 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
2217 | list = tctx->task_list; |
2218 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2219 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 2220 | |
7cbf1722 JA |
2221 | node = list.first; |
2222 | while (node) { | |
2223 | struct io_wq_work_node *next = node->next; | |
65453d1e | 2224 | struct io_ring_ctx *this_ctx; |
7cbf1722 | 2225 | struct io_kiocb *req; |
0ba9c9ed | 2226 | |
7cbf1722 | 2227 | req = container_of(node, struct io_kiocb, io_task_work.node); |
65453d1e | 2228 | this_ctx = req->ctx; |
7cbf1722 JA |
2229 | req->task_work.func(&req->task_work); |
2230 | node = next; | |
65453d1e JA |
2231 | |
2232 | if (!ctx) { | |
2233 | ctx = this_ctx; | |
2234 | } else if (ctx != this_ctx) { | |
2235 | mutex_lock(&ctx->uring_lock); | |
2236 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
2237 | mutex_unlock(&ctx->uring_lock); | |
2238 | ctx = this_ctx; | |
2239 | } | |
2240 | } | |
2241 | ||
2242 | if (ctx && ctx->submit_state.comp.nr) { | |
2243 | mutex_lock(&ctx->uring_lock); | |
2244 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
2245 | mutex_unlock(&ctx->uring_lock); | |
7cbf1722 JA |
2246 | } |
2247 | ||
2248 | return list.first != NULL; | |
c2c4c83c JA |
2249 | } |
2250 | ||
7cbf1722 | 2251 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2252 | { |
7cbf1722 | 2253 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 2254 | |
7cbf1722 JA |
2255 | while (__tctx_task_work(tctx)) |
2256 | cond_resched(); | |
2257 | ||
2258 | clear_bit(0, &tctx->task_state); | |
2259 | } | |
2260 | ||
2261 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
2262 | enum task_work_notify_mode notify) | |
2263 | { | |
2264 | struct io_uring_task *tctx = tsk->io_uring; | |
2265 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 2266 | unsigned long flags; |
7cbf1722 JA |
2267 | int ret; |
2268 | ||
2269 | WARN_ON_ONCE(!tctx); | |
2270 | ||
0b81e80c | 2271 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2272 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 2273 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2274 | |
2275 | /* task_work already pending, we're done */ | |
2276 | if (test_bit(0, &tctx->task_state) || | |
2277 | test_and_set_bit(0, &tctx->task_state)) | |
2278 | return 0; | |
2279 | ||
2280 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
2281 | return 0; | |
2282 | ||
2283 | /* | |
2284 | * Slow path - we failed, find and delete work. if the work is not | |
2285 | * in the list, it got run and we're fine. | |
2286 | */ | |
2287 | ret = 0; | |
0b81e80c | 2288 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
2289 | wq_list_for_each(node, prev, &tctx->task_list) { |
2290 | if (&req->io_task_work.node == node) { | |
2291 | wq_list_del(&tctx->task_list, node, prev); | |
2292 | ret = 1; | |
2293 | break; | |
2294 | } | |
2295 | } | |
0b81e80c | 2296 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2297 | clear_bit(0, &tctx->task_state); |
2298 | return ret; | |
2299 | } | |
2300 | ||
355fb9e2 | 2301 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
2302 | { |
2303 | struct task_struct *tsk = req->task; | |
2304 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2305 | enum task_work_notify_mode notify; |
2306 | int ret; | |
c2c4c83c | 2307 | |
6200b0ae JA |
2308 | if (tsk->flags & PF_EXITING) |
2309 | return -ESRCH; | |
2310 | ||
c2c4c83c | 2311 | /* |
0ba9c9ed JA |
2312 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2313 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2314 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2315 | * will do the job. | |
c2c4c83c | 2316 | */ |
91989c70 | 2317 | notify = TWA_NONE; |
355fb9e2 | 2318 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
2319 | notify = TWA_SIGNAL; |
2320 | ||
7cbf1722 | 2321 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
2322 | if (!ret) |
2323 | wake_up_process(tsk); | |
0ba9c9ed | 2324 | |
c2c4c83c JA |
2325 | return ret; |
2326 | } | |
2327 | ||
eab30c4d | 2328 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 2329 | task_work_func_t cb) |
eab30c4d PB |
2330 | { |
2331 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); | |
2332 | ||
2333 | init_task_work(&req->task_work, cb); | |
2334 | task_work_add(tsk, &req->task_work, TWA_NONE); | |
2335 | wake_up_process(tsk); | |
2336 | } | |
2337 | ||
c40f6379 JA |
2338 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2339 | { | |
2340 | struct io_ring_ctx *ctx = req->ctx; | |
2341 | ||
2342 | spin_lock_irq(&ctx->completion_lock); | |
2343 | io_cqring_fill_event(req, error); | |
2344 | io_commit_cqring(ctx); | |
2345 | spin_unlock_irq(&ctx->completion_lock); | |
2346 | ||
2347 | io_cqring_ev_posted(ctx); | |
2348 | req_set_fail_links(req); | |
2349 | io_double_put_req(req); | |
2350 | } | |
2351 | ||
2352 | static void io_req_task_cancel(struct callback_head *cb) | |
2353 | { | |
2354 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2355 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2356 | |
792bb6eb | 2357 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 2358 | __io_req_task_cancel(req, req->result); |
792bb6eb | 2359 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 2360 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2361 | } |
2362 | ||
2363 | static void __io_req_task_submit(struct io_kiocb *req) | |
2364 | { | |
2365 | struct io_ring_ctx *ctx = req->ctx; | |
2366 | ||
04fc6c80 | 2367 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2368 | mutex_lock(&ctx->uring_lock); |
dc0eced5 PB |
2369 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
2370 | !io_sq_thread_acquire_mm_files(ctx, req)) | |
c5eef2b9 | 2371 | __io_queue_sqe(req); |
81b6d05c | 2372 | else |
c40f6379 | 2373 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2374 | mutex_unlock(&ctx->uring_lock); |
aec18a57 PB |
2375 | |
2376 | if (ctx->flags & IORING_SETUP_SQPOLL) | |
2377 | io_sq_thread_drop_mm_files(); | |
c40f6379 JA |
2378 | } |
2379 | ||
2380 | static void io_req_task_submit(struct callback_head *cb) | |
2381 | { | |
2382 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2383 | ||
2384 | __io_req_task_submit(req); | |
2385 | } | |
2386 | ||
2387 | static void io_req_task_queue(struct io_kiocb *req) | |
2388 | { | |
c40f6379 JA |
2389 | int ret; |
2390 | ||
7cbf1722 | 2391 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 2392 | ret = io_req_task_work_add(req); |
c40f6379 | 2393 | if (unlikely(ret)) { |
a3df7698 | 2394 | req->result = -ECANCELED; |
04fc6c80 | 2395 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 2396 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 2397 | } |
c40f6379 JA |
2398 | } |
2399 | ||
a3df7698 PB |
2400 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
2401 | { | |
2402 | percpu_ref_get(&req->ctx->refs); | |
2403 | req->result = ret; | |
2404 | req->task_work.func = io_req_task_cancel; | |
2405 | ||
2406 | if (unlikely(io_req_task_work_add(req))) | |
2407 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2408 | } | |
2409 | ||
f2f87370 | 2410 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2411 | { |
9b5f7bd9 | 2412 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2413 | |
2414 | if (nxt) | |
906a8c3f | 2415 | io_req_task_queue(nxt); |
c69f8dbe JL |
2416 | } |
2417 | ||
c3524383 | 2418 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2419 | { |
c3524383 PB |
2420 | io_queue_next(req); |
2421 | __io_free_req(req); | |
2422 | } | |
8766dd51 | 2423 | |
2d6500d4 | 2424 | struct req_batch { |
5af1d13e PB |
2425 | struct task_struct *task; |
2426 | int task_refs; | |
1b4c351f | 2427 | int ctx_refs; |
2d6500d4 PB |
2428 | }; |
2429 | ||
5af1d13e PB |
2430 | static inline void io_init_req_batch(struct req_batch *rb) |
2431 | { | |
5af1d13e | 2432 | rb->task_refs = 0; |
9ae72463 | 2433 | rb->ctx_refs = 0; |
5af1d13e PB |
2434 | rb->task = NULL; |
2435 | } | |
2436 | ||
2d6500d4 PB |
2437 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2438 | struct req_batch *rb) | |
2439 | { | |
6e833d53 | 2440 | if (rb->task) |
7c660731 | 2441 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2442 | if (rb->ctx_refs) |
2443 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2444 | } |
2445 | ||
6ff119a6 PB |
2446 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2447 | struct io_submit_state *state) | |
2d6500d4 | 2448 | { |
f2f87370 | 2449 | io_queue_next(req); |
2d6500d4 | 2450 | |
e3bc8e9d | 2451 | if (req->task != rb->task) { |
7c660731 PB |
2452 | if (rb->task) |
2453 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2454 | rb->task = req->task; |
2455 | rb->task_refs = 0; | |
5af1d13e | 2456 | } |
e3bc8e9d | 2457 | rb->task_refs++; |
9ae72463 | 2458 | rb->ctx_refs++; |
5af1d13e | 2459 | |
4edf20f9 | 2460 | io_dismantle_req(req); |
bd759045 | 2461 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2462 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2463 | else |
2464 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2465 | } |
2466 | ||
905c172f PB |
2467 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2468 | struct io_ring_ctx *ctx) | |
2469 | { | |
2470 | int i, nr = cs->nr; | |
2471 | struct io_kiocb *req; | |
2472 | struct req_batch rb; | |
2473 | ||
2474 | io_init_req_batch(&rb); | |
2475 | spin_lock_irq(&ctx->completion_lock); | |
2476 | for (i = 0; i < nr; i++) { | |
2477 | req = cs->reqs[i]; | |
2478 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2479 | } | |
2480 | io_commit_cqring(ctx); | |
2481 | spin_unlock_irq(&ctx->completion_lock); | |
2482 | ||
2483 | io_cqring_ev_posted(ctx); | |
2484 | for (i = 0; i < nr; i++) { | |
2485 | req = cs->reqs[i]; | |
2486 | ||
2487 | /* submission and completion refs */ | |
2488 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2489 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2490 | } |
2491 | ||
2492 | io_req_free_batch_finish(ctx, &rb); | |
2493 | cs->nr = 0; | |
7a743e22 PB |
2494 | } |
2495 | ||
ba816ad6 JA |
2496 | /* |
2497 | * Drop reference to request, return next in chain (if there is one) if this | |
2498 | * was the last reference to this request. | |
2499 | */ | |
9b5f7bd9 | 2500 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2501 | { |
9b5f7bd9 PB |
2502 | struct io_kiocb *nxt = NULL; |
2503 | ||
2a44f467 | 2504 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2505 | nxt = io_req_find_next(req); |
4d7dd462 | 2506 | __io_free_req(req); |
2a44f467 | 2507 | } |
9b5f7bd9 | 2508 | return nxt; |
2b188cc1 JA |
2509 | } |
2510 | ||
e65ef56d JA |
2511 | static void io_put_req(struct io_kiocb *req) |
2512 | { | |
2513 | if (refcount_dec_and_test(&req->refs)) | |
2514 | io_free_req(req); | |
2b188cc1 JA |
2515 | } |
2516 | ||
216578e5 PB |
2517 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2518 | { | |
2519 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2520 | ||
2521 | io_free_req(req); | |
2522 | } | |
2523 | ||
2524 | static void io_free_req_deferred(struct io_kiocb *req) | |
2525 | { | |
2526 | int ret; | |
2527 | ||
7cbf1722 | 2528 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2529 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2530 | if (unlikely(ret)) |
2531 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2532 | } |
2533 | ||
2534 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2535 | { | |
2536 | if (refcount_sub_and_test(refs, &req->refs)) | |
2537 | io_free_req_deferred(req); | |
2538 | } | |
2539 | ||
978db57e JA |
2540 | static void io_double_put_req(struct io_kiocb *req) |
2541 | { | |
2542 | /* drop both submit and complete references */ | |
2543 | if (refcount_sub_and_test(2, &req->refs)) | |
2544 | io_free_req(req); | |
2545 | } | |
2546 | ||
6c503150 | 2547 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2548 | { |
2549 | /* See comment at the top of this file */ | |
2550 | smp_rmb(); | |
e23de15f | 2551 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2552 | } |
2553 | ||
fb5ccc98 PB |
2554 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2555 | { | |
2556 | struct io_rings *rings = ctx->rings; | |
2557 | ||
2558 | /* make sure SQ entry isn't read before tail */ | |
2559 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2560 | } | |
2561 | ||
8ff069bf | 2562 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2563 | { |
8ff069bf | 2564 | unsigned int cflags; |
e94f141b | 2565 | |
bcda7baa JA |
2566 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2567 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2568 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2569 | kfree(kbuf); |
2570 | return cflags; | |
e94f141b JA |
2571 | } |
2572 | ||
8ff069bf | 2573 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2574 | { |
4d954c25 | 2575 | struct io_buffer *kbuf; |
bcda7baa | 2576 | |
4d954c25 | 2577 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2578 | return io_put_kbuf(req, kbuf); |
2579 | } | |
2580 | ||
4c6e277c JA |
2581 | static inline bool io_run_task_work(void) |
2582 | { | |
6200b0ae JA |
2583 | /* |
2584 | * Not safe to run on exiting task, and the task_work handling will | |
2585 | * not add work to such a task. | |
2586 | */ | |
2587 | if (unlikely(current->flags & PF_EXITING)) | |
2588 | return false; | |
4c6e277c JA |
2589 | if (current->task_works) { |
2590 | __set_current_state(TASK_RUNNING); | |
2591 | task_work_run(); | |
2592 | return true; | |
2593 | } | |
2594 | ||
2595 | return false; | |
bcda7baa JA |
2596 | } |
2597 | ||
def596e9 JA |
2598 | /* |
2599 | * Find and free completed poll iocbs | |
2600 | */ | |
2601 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2602 | struct list_head *done) | |
2603 | { | |
8237e045 | 2604 | struct req_batch rb; |
def596e9 | 2605 | struct io_kiocb *req; |
bbde017a XW |
2606 | |
2607 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2608 | smp_rmb(); | |
def596e9 | 2609 | |
5af1d13e | 2610 | io_init_req_batch(&rb); |
def596e9 | 2611 | while (!list_empty(done)) { |
bcda7baa JA |
2612 | int cflags = 0; |
2613 | ||
d21ffe7e | 2614 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2615 | list_del(&req->inflight_entry); |
2616 | ||
bbde017a XW |
2617 | if (READ_ONCE(req->result) == -EAGAIN) { |
2618 | req->iopoll_completed = 0; | |
23faba36 | 2619 | if (io_rw_reissue(req)) |
f161340d | 2620 | continue; |
bbde017a | 2621 | } |
def596e9 | 2622 | |
bcda7baa | 2623 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2624 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2625 | |
2626 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2627 | (*nr_events)++; |
2628 | ||
c3524383 | 2629 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2630 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2631 | } |
def596e9 | 2632 | |
09bb8394 | 2633 | io_commit_cqring(ctx); |
80c18e4a | 2634 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2635 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2636 | } |
2637 | ||
def596e9 JA |
2638 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2639 | long min) | |
2640 | { | |
2641 | struct io_kiocb *req, *tmp; | |
2642 | LIST_HEAD(done); | |
2643 | bool spin; | |
2644 | int ret; | |
2645 | ||
2646 | /* | |
2647 | * Only spin for completions if we don't have multiple devices hanging | |
2648 | * off our complete list, and we're under the requested amount. | |
2649 | */ | |
2650 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2651 | ||
2652 | ret = 0; | |
d21ffe7e | 2653 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2654 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2655 | |
2656 | /* | |
581f9810 BM |
2657 | * Move completed and retryable entries to our local lists. |
2658 | * If we find a request that requires polling, break out | |
2659 | * and complete those lists first, if we have entries there. | |
def596e9 | 2660 | */ |
65a6543d | 2661 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2662 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2663 | continue; |
2664 | } | |
2665 | if (!list_empty(&done)) | |
2666 | break; | |
2667 | ||
2668 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2669 | if (ret < 0) | |
2670 | break; | |
2671 | ||
3aadc23e PB |
2672 | /* iopoll may have completed current req */ |
2673 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2674 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2675 | |
def596e9 JA |
2676 | if (ret && spin) |
2677 | spin = false; | |
2678 | ret = 0; | |
2679 | } | |
2680 | ||
2681 | if (!list_empty(&done)) | |
2682 | io_iopoll_complete(ctx, nr_events, &done); | |
2683 | ||
2684 | return ret; | |
2685 | } | |
2686 | ||
2687 | /* | |
d195a66e | 2688 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2689 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2690 | * as a non-spinning completion check. | |
2691 | */ | |
2692 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2693 | long min) | |
2694 | { | |
540e32a0 | 2695 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2696 | int ret; |
2697 | ||
2698 | ret = io_do_iopoll(ctx, nr_events, min); | |
2699 | if (ret < 0) | |
2700 | return ret; | |
eba0a4dd | 2701 | if (*nr_events >= min) |
def596e9 JA |
2702 | return 0; |
2703 | } | |
2704 | ||
2705 | return 1; | |
2706 | } | |
2707 | ||
2708 | /* | |
2709 | * We can't just wait for polled events to come to us, we have to actively | |
2710 | * find and complete them. | |
2711 | */ | |
b2edc0a7 | 2712 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2713 | { |
2714 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2715 | return; | |
2716 | ||
2717 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2718 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2719 | unsigned int nr_events = 0; |
2720 | ||
b2edc0a7 | 2721 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2722 | |
b2edc0a7 PB |
2723 | /* let it sleep and repeat later if can't complete a request */ |
2724 | if (nr_events == 0) | |
2725 | break; | |
08f5439f JA |
2726 | /* |
2727 | * Ensure we allow local-to-the-cpu processing to take place, | |
2728 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2729 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2730 | */ |
3fcee5a6 PB |
2731 | if (need_resched()) { |
2732 | mutex_unlock(&ctx->uring_lock); | |
2733 | cond_resched(); | |
2734 | mutex_lock(&ctx->uring_lock); | |
2735 | } | |
def596e9 JA |
2736 | } |
2737 | mutex_unlock(&ctx->uring_lock); | |
2738 | } | |
2739 | ||
7668b92a | 2740 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2741 | { |
7668b92a | 2742 | unsigned int nr_events = 0; |
2b2ed975 | 2743 | int iters = 0, ret = 0; |
500f9fba | 2744 | |
c7849be9 XW |
2745 | /* |
2746 | * We disallow the app entering submit/complete with polling, but we | |
2747 | * still need to lock the ring to prevent racing with polled issue | |
2748 | * that got punted to a workqueue. | |
2749 | */ | |
2750 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2751 | do { |
a3a0e43f JA |
2752 | /* |
2753 | * Don't enter poll loop if we already have events pending. | |
2754 | * If we do, we can potentially be spinning for commands that | |
2755 | * already triggered a CQE (eg in error). | |
2756 | */ | |
6c503150 PB |
2757 | if (test_bit(0, &ctx->cq_check_overflow)) |
2758 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2759 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2760 | break; |
2761 | ||
500f9fba JA |
2762 | /* |
2763 | * If a submit got punted to a workqueue, we can have the | |
2764 | * application entering polling for a command before it gets | |
2765 | * issued. That app will hold the uring_lock for the duration | |
2766 | * of the poll right here, so we need to take a breather every | |
2767 | * now and then to ensure that the issue has a chance to add | |
2768 | * the poll to the issued list. Otherwise we can spin here | |
2769 | * forever, while the workqueue is stuck trying to acquire the | |
2770 | * very same mutex. | |
2771 | */ | |
2772 | if (!(++iters & 7)) { | |
2773 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2774 | io_run_task_work(); |
500f9fba JA |
2775 | mutex_lock(&ctx->uring_lock); |
2776 | } | |
2777 | ||
7668b92a | 2778 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2779 | if (ret <= 0) |
2780 | break; | |
2781 | ret = 0; | |
7668b92a | 2782 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2783 | |
500f9fba | 2784 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2785 | return ret; |
2786 | } | |
2787 | ||
491381ce | 2788 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2789 | { |
491381ce JA |
2790 | /* |
2791 | * Tell lockdep we inherited freeze protection from submission | |
2792 | * thread. | |
2793 | */ | |
2794 | if (req->flags & REQ_F_ISREG) { | |
2795 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2796 | |
491381ce | 2797 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2798 | } |
491381ce | 2799 | file_end_write(req->file); |
2b188cc1 JA |
2800 | } |
2801 | ||
b63534c4 | 2802 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2803 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2804 | { |
2805 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2806 | int rw, ret; |
b63534c4 | 2807 | struct iov_iter iter; |
b63534c4 | 2808 | |
dc2a6e9a PB |
2809 | /* already prepared */ |
2810 | if (req->async_data) | |
2811 | return true; | |
b63534c4 JA |
2812 | |
2813 | switch (req->opcode) { | |
2814 | case IORING_OP_READV: | |
2815 | case IORING_OP_READ_FIXED: | |
2816 | case IORING_OP_READ: | |
2817 | rw = READ; | |
2818 | break; | |
2819 | case IORING_OP_WRITEV: | |
2820 | case IORING_OP_WRITE_FIXED: | |
2821 | case IORING_OP_WRITE: | |
2822 | rw = WRITE; | |
2823 | break; | |
2824 | default: | |
2825 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2826 | req->opcode); | |
dc2a6e9a | 2827 | return false; |
b63534c4 JA |
2828 | } |
2829 | ||
dc2a6e9a PB |
2830 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2831 | if (ret < 0) | |
2832 | return false; | |
6bf985dc | 2833 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2834 | } |
b63534c4 JA |
2835 | #endif |
2836 | ||
23faba36 | 2837 | static bool io_rw_reissue(struct io_kiocb *req) |
b63534c4 JA |
2838 | { |
2839 | #ifdef CONFIG_BLOCK | |
355afaeb | 2840 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2841 | int ret; |
2842 | ||
355afaeb JA |
2843 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2844 | return false; | |
75c668cd | 2845 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
b63534c4 JA |
2846 | return false; |
2847 | ||
55e6ac1e PB |
2848 | lockdep_assert_held(&req->ctx->uring_lock); |
2849 | ||
28cea78a | 2850 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2851 | |
dc2a6e9a | 2852 | if (!ret && io_resubmit_prep(req)) { |
fdee946d JA |
2853 | refcount_inc(&req->refs); |
2854 | io_queue_async_work(req); | |
b63534c4 | 2855 | return true; |
fdee946d | 2856 | } |
dc2a6e9a | 2857 | req_set_fail_links(req); |
b63534c4 JA |
2858 | #endif |
2859 | return false; | |
2860 | } | |
2861 | ||
a1d7c393 | 2862 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2863 | unsigned int issue_flags) |
a1d7c393 | 2864 | { |
2f8e45f1 PB |
2865 | int cflags = 0; |
2866 | ||
23faba36 PB |
2867 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2868 | return; | |
2f8e45f1 PB |
2869 | if (res != req->result) |
2870 | req_set_fail_links(req); | |
23faba36 | 2871 | |
2f8e45f1 PB |
2872 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2873 | kiocb_end_write(req); | |
2874 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2875 | cflags = io_put_rw_kbuf(req); | |
2876 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2877 | } |
2878 | ||
2879 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2880 | { | |
9adbd45d | 2881 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2882 | |
889fca73 | 2883 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2884 | } |
2885 | ||
def596e9 JA |
2886 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2887 | { | |
9adbd45d | 2888 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2889 | |
491381ce JA |
2890 | if (kiocb->ki_flags & IOCB_WRITE) |
2891 | kiocb_end_write(req); | |
def596e9 | 2892 | |
2d7d6792 | 2893 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2894 | req_set_fail_links(req); |
bbde017a XW |
2895 | |
2896 | WRITE_ONCE(req->result, res); | |
2897 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2898 | smp_wmb(); |
2899 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2900 | } |
2901 | ||
2902 | /* | |
2903 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2904 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2905 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2906 | * accessing the kiocb cookie. | |
2907 | */ | |
2e9dbe90 | 2908 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2909 | { |
2910 | struct io_ring_ctx *ctx = req->ctx; | |
2911 | ||
2912 | /* | |
2913 | * Track whether we have multiple files in our lists. This will impact | |
2914 | * how we do polling eventually, not spinning if we're on potentially | |
2915 | * different devices. | |
2916 | */ | |
540e32a0 | 2917 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2918 | ctx->poll_multi_file = false; |
2919 | } else if (!ctx->poll_multi_file) { | |
2920 | struct io_kiocb *list_req; | |
2921 | ||
540e32a0 | 2922 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2923 | inflight_entry); |
9adbd45d | 2924 | if (list_req->file != req->file) |
def596e9 JA |
2925 | ctx->poll_multi_file = true; |
2926 | } | |
2927 | ||
2928 | /* | |
2929 | * For fast devices, IO may have already completed. If it has, add | |
2930 | * it to the front so we find it first. | |
2931 | */ | |
65a6543d | 2932 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2933 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2934 | else |
d21ffe7e | 2935 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2936 | |
2e9dbe90 XW |
2937 | /* |
2938 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2939 | * task context or in io worker task context. If current task context is | |
2940 | * sq thread, we don't need to check whether should wake up sq thread. | |
2941 | */ | |
2942 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2943 | wq_has_sleeper(&ctx->sq_data->wait)) |
2944 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2945 | } |
2946 | ||
9f13c35b PB |
2947 | static inline void io_state_file_put(struct io_submit_state *state) |
2948 | { | |
02b23a9a PB |
2949 | if (state->file_refs) { |
2950 | fput_many(state->file, state->file_refs); | |
2951 | state->file_refs = 0; | |
2952 | } | |
9a56a232 JA |
2953 | } |
2954 | ||
2955 | /* | |
2956 | * Get as many references to a file as we have IOs left in this submission, | |
2957 | * assuming most submissions are for one file, or at least that each file | |
2958 | * has more than one submission. | |
2959 | */ | |
8da11c19 | 2960 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2961 | { |
2962 | if (!state) | |
2963 | return fget(fd); | |
2964 | ||
6e1271e6 | 2965 | if (state->file_refs) { |
9a56a232 | 2966 | if (state->fd == fd) { |
6e1271e6 | 2967 | state->file_refs--; |
9a56a232 JA |
2968 | return state->file; |
2969 | } | |
02b23a9a | 2970 | io_state_file_put(state); |
9a56a232 JA |
2971 | } |
2972 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2973 | if (unlikely(!state->file)) |
9a56a232 JA |
2974 | return NULL; |
2975 | ||
2976 | state->fd = fd; | |
6e1271e6 | 2977 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2978 | return state->file; |
2979 | } | |
2980 | ||
4503b767 JA |
2981 | static bool io_bdev_nowait(struct block_device *bdev) |
2982 | { | |
9ba0d0c8 | 2983 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2984 | } |
2985 | ||
2b188cc1 JA |
2986 | /* |
2987 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2988 | * any file. For now, just ensure that anything potentially problematic is done | |
2989 | * inline. | |
2990 | */ | |
af197f50 | 2991 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2992 | { |
2993 | umode_t mode = file_inode(file)->i_mode; | |
2994 | ||
4503b767 | 2995 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2996 | if (IS_ENABLED(CONFIG_BLOCK) && |
2997 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2998 | return true; |
2999 | return false; | |
3000 | } | |
3001 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 3002 | return true; |
4503b767 | 3003 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3004 | if (IS_ENABLED(CONFIG_BLOCK) && |
3005 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3006 | file->f_op != &io_uring_fops) |
3007 | return true; | |
3008 | return false; | |
3009 | } | |
2b188cc1 | 3010 | |
c5b85625 JA |
3011 | /* any ->read/write should understand O_NONBLOCK */ |
3012 | if (file->f_flags & O_NONBLOCK) | |
3013 | return true; | |
3014 | ||
af197f50 JA |
3015 | if (!(file->f_mode & FMODE_NOWAIT)) |
3016 | return false; | |
3017 | ||
3018 | if (rw == READ) | |
3019 | return file->f_op->read_iter != NULL; | |
3020 | ||
3021 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
3022 | } |
3023 | ||
a88fc400 | 3024 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3025 | { |
def596e9 | 3026 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 3027 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 3028 | struct file *file = req->file; |
09bb8394 JA |
3029 | unsigned ioprio; |
3030 | int ret; | |
2b188cc1 | 3031 | |
75c668cd | 3032 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
3033 | req->flags |= REQ_F_ISREG; |
3034 | ||
2b188cc1 | 3035 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 3036 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 3037 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 3038 | kiocb->ki_pos = file->f_pos; |
ba04291e | 3039 | } |
2b188cc1 | 3040 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
3041 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
3042 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
3043 | if (unlikely(ret)) | |
3044 | return ret; | |
2b188cc1 | 3045 | |
75c668cd PB |
3046 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
3047 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
3048 | req->flags |= REQ_F_NOWAIT; | |
3049 | ||
2b188cc1 JA |
3050 | ioprio = READ_ONCE(sqe->ioprio); |
3051 | if (ioprio) { | |
3052 | ret = ioprio_check_cap(ioprio); | |
3053 | if (ret) | |
09bb8394 | 3054 | return ret; |
2b188cc1 JA |
3055 | |
3056 | kiocb->ki_ioprio = ioprio; | |
3057 | } else | |
3058 | kiocb->ki_ioprio = get_current_ioprio(); | |
3059 | ||
def596e9 | 3060 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
3061 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
3062 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 3063 | return -EOPNOTSUPP; |
2b188cc1 | 3064 | |
def596e9 JA |
3065 | kiocb->ki_flags |= IOCB_HIPRI; |
3066 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 3067 | req->iopoll_completed = 0; |
def596e9 | 3068 | } else { |
09bb8394 JA |
3069 | if (kiocb->ki_flags & IOCB_HIPRI) |
3070 | return -EINVAL; | |
def596e9 JA |
3071 | kiocb->ki_complete = io_complete_rw; |
3072 | } | |
9adbd45d | 3073 | |
3529d8c2 JA |
3074 | req->rw.addr = READ_ONCE(sqe->addr); |
3075 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3076 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3077 | return 0; |
2b188cc1 JA |
3078 | } |
3079 | ||
3080 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3081 | { | |
3082 | switch (ret) { | |
3083 | case -EIOCBQUEUED: | |
3084 | break; | |
3085 | case -ERESTARTSYS: | |
3086 | case -ERESTARTNOINTR: | |
3087 | case -ERESTARTNOHAND: | |
3088 | case -ERESTART_RESTARTBLOCK: | |
3089 | /* | |
3090 | * We can't just restart the syscall, since previously | |
3091 | * submitted sqes may already be in progress. Just fail this | |
3092 | * IO with EINTR. | |
3093 | */ | |
3094 | ret = -EINTR; | |
df561f66 | 3095 | fallthrough; |
2b188cc1 JA |
3096 | default: |
3097 | kiocb->ki_complete(kiocb, ret, 0); | |
3098 | } | |
3099 | } | |
3100 | ||
a1d7c393 | 3101 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 3102 | unsigned int issue_flags) |
ba816ad6 | 3103 | { |
ba04291e | 3104 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 3105 | struct io_async_rw *io = req->async_data; |
ba04291e | 3106 | |
227c0c96 | 3107 | /* add previously done IO, if any */ |
e8c2bc1f | 3108 | if (io && io->bytes_done > 0) { |
227c0c96 | 3109 | if (ret < 0) |
e8c2bc1f | 3110 | ret = io->bytes_done; |
227c0c96 | 3111 | else |
e8c2bc1f | 3112 | ret += io->bytes_done; |
227c0c96 JA |
3113 | } |
3114 | ||
ba04291e JA |
3115 | if (req->flags & REQ_F_CUR_POS) |
3116 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 3117 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 3118 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
3119 | else |
3120 | io_rw_done(kiocb, ret); | |
3121 | } | |
3122 | ||
847595de | 3123 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 3124 | { |
9adbd45d JA |
3125 | struct io_ring_ctx *ctx = req->ctx; |
3126 | size_t len = req->rw.len; | |
edafccee | 3127 | struct io_mapped_ubuf *imu; |
4be1c615 | 3128 | u16 index, buf_index = req->buf_index; |
edafccee JA |
3129 | size_t offset; |
3130 | u64 buf_addr; | |
3131 | ||
edafccee JA |
3132 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3133 | return -EFAULT; | |
edafccee JA |
3134 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3135 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 3136 | buf_addr = req->rw.addr; |
edafccee JA |
3137 | |
3138 | /* overflow */ | |
3139 | if (buf_addr + len < buf_addr) | |
3140 | return -EFAULT; | |
3141 | /* not inside the mapped region */ | |
3142 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
3143 | return -EFAULT; | |
3144 | ||
3145 | /* | |
3146 | * May not be a start of buffer, set size appropriately | |
3147 | * and advance us to the beginning. | |
3148 | */ | |
3149 | offset = buf_addr - imu->ubuf; | |
3150 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3151 | |
3152 | if (offset) { | |
3153 | /* | |
3154 | * Don't use iov_iter_advance() here, as it's really slow for | |
3155 | * using the latter parts of a big fixed buffer - it iterates | |
3156 | * over each segment manually. We can cheat a bit here, because | |
3157 | * we know that: | |
3158 | * | |
3159 | * 1) it's a BVEC iter, we set it up | |
3160 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3161 | * first and last bvec | |
3162 | * | |
3163 | * So just find our index, and adjust the iterator afterwards. | |
3164 | * If the offset is within the first bvec (or the whole first | |
3165 | * bvec, just use iov_iter_advance(). This makes it easier | |
3166 | * since we can just skip the first segment, which may not | |
3167 | * be PAGE_SIZE aligned. | |
3168 | */ | |
3169 | const struct bio_vec *bvec = imu->bvec; | |
3170 | ||
3171 | if (offset <= bvec->bv_len) { | |
3172 | iov_iter_advance(iter, offset); | |
3173 | } else { | |
3174 | unsigned long seg_skip; | |
3175 | ||
3176 | /* skip first vec */ | |
3177 | offset -= bvec->bv_len; | |
3178 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3179 | ||
3180 | iter->bvec = bvec + seg_skip; | |
3181 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3182 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3183 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3184 | } |
3185 | } | |
3186 | ||
847595de | 3187 | return 0; |
edafccee JA |
3188 | } |
3189 | ||
bcda7baa JA |
3190 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3191 | { | |
3192 | if (needs_lock) | |
3193 | mutex_unlock(&ctx->uring_lock); | |
3194 | } | |
3195 | ||
3196 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3197 | { | |
3198 | /* | |
3199 | * "Normal" inline submissions always hold the uring_lock, since we | |
3200 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3201 | * The only exception is when we've detached the request and issue it | |
3202 | * from an async worker thread, grab the lock for that case. | |
3203 | */ | |
3204 | if (needs_lock) | |
3205 | mutex_lock(&ctx->uring_lock); | |
3206 | } | |
3207 | ||
3208 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3209 | int bgid, struct io_buffer *kbuf, | |
3210 | bool needs_lock) | |
3211 | { | |
3212 | struct io_buffer *head; | |
3213 | ||
3214 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3215 | return kbuf; | |
3216 | ||
3217 | io_ring_submit_lock(req->ctx, needs_lock); | |
3218 | ||
3219 | lockdep_assert_held(&req->ctx->uring_lock); | |
3220 | ||
3221 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3222 | if (head) { | |
3223 | if (!list_empty(&head->list)) { | |
3224 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3225 | list); | |
3226 | list_del(&kbuf->list); | |
3227 | } else { | |
3228 | kbuf = head; | |
3229 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3230 | } | |
3231 | if (*len > kbuf->len) | |
3232 | *len = kbuf->len; | |
3233 | } else { | |
3234 | kbuf = ERR_PTR(-ENOBUFS); | |
3235 | } | |
3236 | ||
3237 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3238 | ||
3239 | return kbuf; | |
3240 | } | |
3241 | ||
4d954c25 JA |
3242 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3243 | bool needs_lock) | |
3244 | { | |
3245 | struct io_buffer *kbuf; | |
4f4eeba8 | 3246 | u16 bgid; |
4d954c25 JA |
3247 | |
3248 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3249 | bgid = req->buf_index; |
4d954c25 JA |
3250 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3251 | if (IS_ERR(kbuf)) | |
3252 | return kbuf; | |
3253 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3254 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3255 | return u64_to_user_ptr(kbuf->addr); | |
3256 | } | |
3257 | ||
3258 | #ifdef CONFIG_COMPAT | |
3259 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3260 | bool needs_lock) | |
3261 | { | |
3262 | struct compat_iovec __user *uiov; | |
3263 | compat_ssize_t clen; | |
3264 | void __user *buf; | |
3265 | ssize_t len; | |
3266 | ||
3267 | uiov = u64_to_user_ptr(req->rw.addr); | |
3268 | if (!access_ok(uiov, sizeof(*uiov))) | |
3269 | return -EFAULT; | |
3270 | if (__get_user(clen, &uiov->iov_len)) | |
3271 | return -EFAULT; | |
3272 | if (clen < 0) | |
3273 | return -EINVAL; | |
3274 | ||
3275 | len = clen; | |
3276 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3277 | if (IS_ERR(buf)) | |
3278 | return PTR_ERR(buf); | |
3279 | iov[0].iov_base = buf; | |
3280 | iov[0].iov_len = (compat_size_t) len; | |
3281 | return 0; | |
3282 | } | |
3283 | #endif | |
3284 | ||
3285 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3286 | bool needs_lock) | |
3287 | { | |
3288 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3289 | void __user *buf; | |
3290 | ssize_t len; | |
3291 | ||
3292 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3293 | return -EFAULT; | |
3294 | ||
3295 | len = iov[0].iov_len; | |
3296 | if (len < 0) | |
3297 | return -EINVAL; | |
3298 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3299 | if (IS_ERR(buf)) | |
3300 | return PTR_ERR(buf); | |
3301 | iov[0].iov_base = buf; | |
3302 | iov[0].iov_len = len; | |
3303 | return 0; | |
3304 | } | |
3305 | ||
3306 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3307 | bool needs_lock) | |
3308 | { | |
dddb3e26 JA |
3309 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3310 | struct io_buffer *kbuf; | |
3311 | ||
3312 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3313 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3314 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3315 | return 0; |
dddb3e26 | 3316 | } |
dd201662 | 3317 | if (req->rw.len != 1) |
4d954c25 JA |
3318 | return -EINVAL; |
3319 | ||
3320 | #ifdef CONFIG_COMPAT | |
3321 | if (req->ctx->compat) | |
3322 | return io_compat_import(req, iov, needs_lock); | |
3323 | #endif | |
3324 | ||
3325 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3326 | } | |
3327 | ||
847595de PB |
3328 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3329 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3330 | { |
9adbd45d JA |
3331 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3332 | size_t sqe_len = req->rw.len; | |
847595de | 3333 | u8 opcode = req->opcode; |
4d954c25 | 3334 | ssize_t ret; |
edafccee | 3335 | |
7d009165 | 3336 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3337 | *iovec = NULL; |
9adbd45d | 3338 | return io_import_fixed(req, rw, iter); |
edafccee | 3339 | } |
2b188cc1 | 3340 | |
bcda7baa | 3341 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3342 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3343 | return -EINVAL; |
3344 | ||
3a6820f2 | 3345 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3346 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3347 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3348 | if (IS_ERR(buf)) |
4d954c25 | 3349 | return PTR_ERR(buf); |
3f9d6441 | 3350 | req->rw.len = sqe_len; |
bcda7baa JA |
3351 | } |
3352 | ||
3a6820f2 JA |
3353 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3354 | *iovec = NULL; | |
10fc72e4 | 3355 | return ret; |
3a6820f2 JA |
3356 | } |
3357 | ||
4d954c25 JA |
3358 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3359 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3360 | if (!ret) |
3361 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3362 | *iovec = NULL; |
3363 | return ret; | |
3364 | } | |
3365 | ||
89cd35c5 CH |
3366 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3367 | req->ctx->compat); | |
2b188cc1 JA |
3368 | } |
3369 | ||
0fef9483 JA |
3370 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3371 | { | |
5b09e37e | 3372 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3373 | } |
3374 | ||
31b51510 | 3375 | /* |
32960613 JA |
3376 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3377 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3378 | */ |
4017eb91 | 3379 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3380 | { |
4017eb91 JA |
3381 | struct kiocb *kiocb = &req->rw.kiocb; |
3382 | struct file *file = req->file; | |
32960613 JA |
3383 | ssize_t ret = 0; |
3384 | ||
3385 | /* | |
3386 | * Don't support polled IO through this interface, and we can't | |
3387 | * support non-blocking either. For the latter, this just causes | |
3388 | * the kiocb to be handled from an async context. | |
3389 | */ | |
3390 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3391 | return -EOPNOTSUPP; | |
3392 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3393 | return -EAGAIN; | |
3394 | ||
3395 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3396 | struct iovec iovec; |
32960613 JA |
3397 | ssize_t nr; |
3398 | ||
311ae9e1 PB |
3399 | if (!iov_iter_is_bvec(iter)) { |
3400 | iovec = iov_iter_iovec(iter); | |
3401 | } else { | |
4017eb91 JA |
3402 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3403 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3404 | } |
3405 | ||
32960613 JA |
3406 | if (rw == READ) { |
3407 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3408 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3409 | } else { |
3410 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3411 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3412 | } |
3413 | ||
3414 | if (nr < 0) { | |
3415 | if (!ret) | |
3416 | ret = nr; | |
3417 | break; | |
3418 | } | |
3419 | ret += nr; | |
3420 | if (nr != iovec.iov_len) | |
3421 | break; | |
4017eb91 JA |
3422 | req->rw.len -= nr; |
3423 | req->rw.addr += nr; | |
32960613 JA |
3424 | iov_iter_advance(iter, nr); |
3425 | } | |
3426 | ||
3427 | return ret; | |
3428 | } | |
3429 | ||
ff6165b2 JA |
3430 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3431 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3432 | { |
e8c2bc1f | 3433 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3434 | |
ff6165b2 | 3435 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3436 | rw->free_iovec = iovec; |
227c0c96 | 3437 | rw->bytes_done = 0; |
ff6165b2 | 3438 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3439 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3440 | return; |
b64e3444 | 3441 | if (!iovec) { |
ff6165b2 JA |
3442 | unsigned iov_off = 0; |
3443 | ||
3444 | rw->iter.iov = rw->fast_iov; | |
3445 | if (iter->iov != fast_iov) { | |
3446 | iov_off = iter->iov - fast_iov; | |
3447 | rw->iter.iov += iov_off; | |
3448 | } | |
3449 | if (rw->fast_iov != fast_iov) | |
3450 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3451 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3452 | } else { |
3453 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3454 | } |
3455 | } | |
3456 | ||
e8c2bc1f | 3457 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3458 | { |
e8c2bc1f JA |
3459 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3460 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3461 | return req->async_data == NULL; | |
3d9932a8 XW |
3462 | } |
3463 | ||
e8c2bc1f | 3464 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3465 | { |
e8c2bc1f | 3466 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3467 | return 0; |
3d9932a8 | 3468 | |
e8c2bc1f | 3469 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3470 | } |
3471 | ||
ff6165b2 JA |
3472 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3473 | const struct iovec *fast_iov, | |
227c0c96 | 3474 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3475 | { |
e8c2bc1f | 3476 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3477 | return 0; |
e8c2bc1f | 3478 | if (!req->async_data) { |
6bf985dc PB |
3479 | if (__io_alloc_async_data(req)) { |
3480 | kfree(iovec); | |
5d204bcf | 3481 | return -ENOMEM; |
6bf985dc | 3482 | } |
b7bb4f7d | 3483 | |
ff6165b2 | 3484 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3485 | } |
b7bb4f7d | 3486 | return 0; |
f67676d1 JA |
3487 | } |
3488 | ||
73debe68 | 3489 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3490 | { |
e8c2bc1f | 3491 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3492 | struct iovec *iov = iorw->fast_iov; |
847595de | 3493 | int ret; |
c3e330a4 | 3494 | |
2846c481 | 3495 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3496 | if (unlikely(ret < 0)) |
3497 | return ret; | |
3498 | ||
ab0b196c PB |
3499 | iorw->bytes_done = 0; |
3500 | iorw->free_iovec = iov; | |
3501 | if (iov) | |
3502 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3503 | return 0; |
3504 | } | |
3505 | ||
73debe68 | 3506 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3507 | { |
3529d8c2 JA |
3508 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3509 | return -EBADF; | |
93642ef8 | 3510 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3511 | } |
3512 | ||
c1dd91d1 JA |
3513 | /* |
3514 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3515 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3516 | * This gets called when the page is unlocked, and we generally expect that to | |
3517 | * happen when the page IO is completed and the page is now uptodate. This will | |
3518 | * queue a task_work based retry of the operation, attempting to copy the data | |
3519 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3520 | * do a thread based blocking retry of the operation. That's the unexpected | |
3521 | * slow path. | |
3522 | */ | |
bcf5a063 JA |
3523 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3524 | int sync, void *arg) | |
3525 | { | |
3526 | struct wait_page_queue *wpq; | |
3527 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3528 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3529 | |
3530 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3531 | ||
cdc8fcb4 LT |
3532 | if (!wake_page_match(wpq, key)) |
3533 | return 0; | |
3534 | ||
c8d317aa | 3535 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3536 | list_del_init(&wait->entry); |
3537 | ||
bcf5a063 JA |
3538 | /* submit ref gets dropped, acquire a new one */ |
3539 | refcount_inc(&req->refs); | |
921b9054 | 3540 | io_req_task_queue(req); |
bcf5a063 JA |
3541 | return 1; |
3542 | } | |
3543 | ||
c1dd91d1 JA |
3544 | /* |
3545 | * This controls whether a given IO request should be armed for async page | |
3546 | * based retry. If we return false here, the request is handed to the async | |
3547 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3548 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3549 | * will either succeed because the page is now uptodate and unlocked, or it | |
3550 | * will register a callback when the page is unlocked at IO completion. Through | |
3551 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3552 | * That retry will attempt the buffered read again. The retry will generally | |
3553 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3554 | * async worker threads for a blocking retry. | |
3555 | */ | |
227c0c96 | 3556 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3557 | { |
e8c2bc1f JA |
3558 | struct io_async_rw *rw = req->async_data; |
3559 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3560 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3561 | |
bcf5a063 JA |
3562 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3563 | if (req->flags & REQ_F_NOWAIT) | |
3564 | return false; | |
f67676d1 | 3565 | |
227c0c96 | 3566 | /* Only for buffered IO */ |
3b2a4439 | 3567 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3568 | return false; |
3b2a4439 | 3569 | |
bcf5a063 JA |
3570 | /* |
3571 | * just use poll if we can, and don't attempt if the fs doesn't | |
3572 | * support callback based unlocks | |
3573 | */ | |
3574 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3575 | return false; | |
f67676d1 | 3576 | |
3b2a4439 JA |
3577 | wait->wait.func = io_async_buf_func; |
3578 | wait->wait.private = req; | |
3579 | wait->wait.flags = 0; | |
3580 | INIT_LIST_HEAD(&wait->wait.entry); | |
3581 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3582 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3583 | kiocb->ki_waitq = wait; |
3b2a4439 | 3584 | return true; |
bcf5a063 JA |
3585 | } |
3586 | ||
3587 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3588 | { | |
3589 | if (req->file->f_op->read_iter) | |
3590 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3591 | else if (req->file->f_op->read) |
4017eb91 | 3592 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3593 | else |
3594 | return -EINVAL; | |
f67676d1 JA |
3595 | } |
3596 | ||
889fca73 | 3597 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3598 | { |
3599 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3600 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3601 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3602 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3603 | ssize_t io_size, ret, ret2; |
45d189c6 | 3604 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3605 | |
2846c481 | 3606 | if (rw) { |
e8c2bc1f | 3607 | iter = &rw->iter; |
2846c481 PB |
3608 | iovec = NULL; |
3609 | } else { | |
3610 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3611 | if (ret < 0) | |
3612 | return ret; | |
3613 | } | |
632546c4 | 3614 | io_size = iov_iter_count(iter); |
fa15bafb | 3615 | req->result = io_size; |
2b188cc1 | 3616 | |
fd6c2e4c JA |
3617 | /* Ensure we clear previously set non-block flag */ |
3618 | if (!force_nonblock) | |
29de5f6a | 3619 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3620 | else |
3621 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3622 | ||
24c74678 | 3623 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3624 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3625 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3626 | return ret ?: -EAGAIN; |
6713e7a6 | 3627 | } |
9e645e11 | 3628 | |
632546c4 | 3629 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3630 | if (unlikely(ret)) { |
3631 | kfree(iovec); | |
3632 | return ret; | |
3633 | } | |
2b188cc1 | 3634 | |
227c0c96 | 3635 | ret = io_iter_do_read(req, iter); |
32960613 | 3636 | |
57cd657b | 3637 | if (ret == -EIOCBQUEUED) { |
fe1cdd55 | 3638 | goto out_free; |
227c0c96 | 3639 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3640 | /* IOPOLL retry should happen for io-wq threads */ |
3641 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3642 | goto done; |
75c668cd PB |
3643 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3644 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3645 | goto done; |
84216315 | 3646 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3647 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3648 | ret = 0; |
7335e3bf | 3649 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3650 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3651 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3652 | goto done; |
227c0c96 JA |
3653 | } |
3654 | ||
227c0c96 | 3655 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3656 | if (ret2) |
3657 | return ret2; | |
3658 | ||
fe1cdd55 | 3659 | iovec = NULL; |
e8c2bc1f | 3660 | rw = req->async_data; |
227c0c96 | 3661 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3662 | iter = &rw->iter; |
227c0c96 | 3663 | |
b23df91b PB |
3664 | do { |
3665 | io_size -= ret; | |
3666 | rw->bytes_done += ret; | |
3667 | /* if we can retry, do so with the callbacks armed */ | |
3668 | if (!io_rw_should_retry(req)) { | |
3669 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3670 | return -EAGAIN; | |
3671 | } | |
3672 | ||
3673 | /* | |
3674 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3675 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3676 | * desired page gets unlocked. We can also get a partial read | |
3677 | * here, and if we do, then just retry at the new offset. | |
3678 | */ | |
3679 | ret = io_iter_do_read(req, iter); | |
3680 | if (ret == -EIOCBQUEUED) | |
3681 | return 0; | |
227c0c96 | 3682 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3683 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3684 | done: |
889fca73 | 3685 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3686 | out_free: |
3687 | /* it's faster to check here then delegate to kfree */ | |
3688 | if (iovec) | |
3689 | kfree(iovec); | |
5ea5dd45 | 3690 | return 0; |
2b188cc1 JA |
3691 | } |
3692 | ||
73debe68 | 3693 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3694 | { |
3529d8c2 JA |
3695 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3696 | return -EBADF; | |
93642ef8 | 3697 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3698 | } |
3699 | ||
889fca73 | 3700 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3701 | { |
3702 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3703 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3704 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3705 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3706 | ssize_t ret, ret2, io_size; |
45d189c6 | 3707 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3708 | |
2846c481 | 3709 | if (rw) { |
e8c2bc1f | 3710 | iter = &rw->iter; |
2846c481 PB |
3711 | iovec = NULL; |
3712 | } else { | |
3713 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3714 | if (ret < 0) | |
3715 | return ret; | |
3716 | } | |
632546c4 | 3717 | io_size = iov_iter_count(iter); |
fa15bafb | 3718 | req->result = io_size; |
2b188cc1 | 3719 | |
fd6c2e4c JA |
3720 | /* Ensure we clear previously set non-block flag */ |
3721 | if (!force_nonblock) | |
a88fc400 PB |
3722 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3723 | else | |
3724 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3725 | |
24c74678 | 3726 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3727 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3728 | goto copy_iov; |
31b51510 | 3729 | |
10d59345 JA |
3730 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3731 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3732 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3733 | goto copy_iov; |
31b51510 | 3734 | |
632546c4 | 3735 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3736 | if (unlikely(ret)) |
3737 | goto out_free; | |
4ed734b0 | 3738 | |
fa15bafb PB |
3739 | /* |
3740 | * Open-code file_start_write here to grab freeze protection, | |
3741 | * which will be released by another thread in | |
3742 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3743 | * released so that it doesn't complain about the held lock when | |
3744 | * we return to userspace. | |
3745 | */ | |
3746 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3747 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3748 | __sb_writers_release(file_inode(req->file)->i_sb, |
3749 | SB_FREEZE_WRITE); | |
3750 | } | |
3751 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3752 | |
fa15bafb | 3753 | if (req->file->f_op->write_iter) |
ff6165b2 | 3754 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3755 | else if (req->file->f_op->write) |
4017eb91 | 3756 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3757 | else |
3758 | ret2 = -EINVAL; | |
4ed734b0 | 3759 | |
fa15bafb PB |
3760 | /* |
3761 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3762 | * retry them without IOCB_NOWAIT. | |
3763 | */ | |
3764 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3765 | ret2 = -EAGAIN; | |
75c668cd PB |
3766 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3767 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3768 | goto done; |
fa15bafb | 3769 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3770 | /* IOPOLL retry should happen for io-wq threads */ |
3771 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3772 | goto copy_iov; | |
355afaeb | 3773 | done: |
889fca73 | 3774 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3775 | } else { |
f67676d1 | 3776 | copy_iov: |
84216315 | 3777 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3778 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3779 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3780 | return ret ?: -EAGAIN; |
2b188cc1 | 3781 | } |
31b51510 | 3782 | out_free: |
f261c168 | 3783 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3784 | if (iovec) |
6f2cc166 | 3785 | kfree(iovec); |
2b188cc1 JA |
3786 | return ret; |
3787 | } | |
3788 | ||
80a261fd JA |
3789 | static int io_renameat_prep(struct io_kiocb *req, |
3790 | const struct io_uring_sqe *sqe) | |
3791 | { | |
3792 | struct io_rename *ren = &req->rename; | |
3793 | const char __user *oldf, *newf; | |
3794 | ||
3795 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3796 | return -EBADF; | |
3797 | ||
3798 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3799 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3800 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3801 | ren->new_dfd = READ_ONCE(sqe->len); | |
3802 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3803 | ||
3804 | ren->oldpath = getname(oldf); | |
3805 | if (IS_ERR(ren->oldpath)) | |
3806 | return PTR_ERR(ren->oldpath); | |
3807 | ||
3808 | ren->newpath = getname(newf); | |
3809 | if (IS_ERR(ren->newpath)) { | |
3810 | putname(ren->oldpath); | |
3811 | return PTR_ERR(ren->newpath); | |
3812 | } | |
3813 | ||
3814 | req->flags |= REQ_F_NEED_CLEANUP; | |
3815 | return 0; | |
3816 | } | |
3817 | ||
45d189c6 | 3818 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3819 | { |
3820 | struct io_rename *ren = &req->rename; | |
3821 | int ret; | |
3822 | ||
45d189c6 | 3823 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3824 | return -EAGAIN; |
3825 | ||
3826 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3827 | ren->newpath, ren->flags); | |
3828 | ||
3829 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3830 | if (ret < 0) | |
3831 | req_set_fail_links(req); | |
3832 | io_req_complete(req, ret); | |
3833 | return 0; | |
3834 | } | |
3835 | ||
14a1143b JA |
3836 | static int io_unlinkat_prep(struct io_kiocb *req, |
3837 | const struct io_uring_sqe *sqe) | |
3838 | { | |
3839 | struct io_unlink *un = &req->unlink; | |
3840 | const char __user *fname; | |
3841 | ||
3842 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3843 | return -EBADF; | |
3844 | ||
3845 | un->dfd = READ_ONCE(sqe->fd); | |
3846 | ||
3847 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3848 | if (un->flags & ~AT_REMOVEDIR) | |
3849 | return -EINVAL; | |
3850 | ||
3851 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3852 | un->filename = getname(fname); | |
3853 | if (IS_ERR(un->filename)) | |
3854 | return PTR_ERR(un->filename); | |
3855 | ||
3856 | req->flags |= REQ_F_NEED_CLEANUP; | |
3857 | return 0; | |
3858 | } | |
3859 | ||
45d189c6 | 3860 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3861 | { |
3862 | struct io_unlink *un = &req->unlink; | |
3863 | int ret; | |
3864 | ||
45d189c6 | 3865 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3866 | return -EAGAIN; |
3867 | ||
3868 | if (un->flags & AT_REMOVEDIR) | |
3869 | ret = do_rmdir(un->dfd, un->filename); | |
3870 | else | |
3871 | ret = do_unlinkat(un->dfd, un->filename); | |
3872 | ||
3873 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3874 | if (ret < 0) | |
3875 | req_set_fail_links(req); | |
3876 | io_req_complete(req, ret); | |
3877 | return 0; | |
3878 | } | |
3879 | ||
36f4fa68 JA |
3880 | static int io_shutdown_prep(struct io_kiocb *req, |
3881 | const struct io_uring_sqe *sqe) | |
3882 | { | |
3883 | #if defined(CONFIG_NET) | |
3884 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3885 | return -EINVAL; | |
3886 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3887 | sqe->buf_index) | |
3888 | return -EINVAL; | |
3889 | ||
3890 | req->shutdown.how = READ_ONCE(sqe->len); | |
3891 | return 0; | |
3892 | #else | |
3893 | return -EOPNOTSUPP; | |
3894 | #endif | |
3895 | } | |
3896 | ||
45d189c6 | 3897 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3898 | { |
3899 | #if defined(CONFIG_NET) | |
3900 | struct socket *sock; | |
3901 | int ret; | |
3902 | ||
45d189c6 | 3903 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3904 | return -EAGAIN; |
3905 | ||
48aba79b | 3906 | sock = sock_from_file(req->file); |
36f4fa68 | 3907 | if (unlikely(!sock)) |
48aba79b | 3908 | return -ENOTSOCK; |
36f4fa68 JA |
3909 | |
3910 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3911 | if (ret < 0) |
3912 | req_set_fail_links(req); | |
36f4fa68 JA |
3913 | io_req_complete(req, ret); |
3914 | return 0; | |
3915 | #else | |
3916 | return -EOPNOTSUPP; | |
3917 | #endif | |
3918 | } | |
3919 | ||
f2a8d5c7 PB |
3920 | static int __io_splice_prep(struct io_kiocb *req, |
3921 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3922 | { |
3923 | struct io_splice* sp = &req->splice; | |
3924 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3925 | |
3232dd02 PB |
3926 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3927 | return -EINVAL; | |
7d67af2c PB |
3928 | |
3929 | sp->file_in = NULL; | |
7d67af2c PB |
3930 | sp->len = READ_ONCE(sqe->len); |
3931 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3932 | ||
3933 | if (unlikely(sp->flags & ~valid_flags)) | |
3934 | return -EINVAL; | |
3935 | ||
8371adf5 PB |
3936 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3937 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3938 | if (!sp->file_in) | |
3939 | return -EBADF; | |
7d67af2c PB |
3940 | req->flags |= REQ_F_NEED_CLEANUP; |
3941 | ||
7cdaf587 XW |
3942 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3943 | /* | |
3944 | * Splice operation will be punted aync, and here need to | |
3945 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3946 | */ | |
3947 | io_req_init_async(req); | |
7d67af2c | 3948 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3949 | } |
7d67af2c PB |
3950 | |
3951 | return 0; | |
3952 | } | |
3953 | ||
f2a8d5c7 PB |
3954 | static int io_tee_prep(struct io_kiocb *req, |
3955 | const struct io_uring_sqe *sqe) | |
3956 | { | |
3957 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3958 | return -EINVAL; | |
3959 | return __io_splice_prep(req, sqe); | |
3960 | } | |
3961 | ||
45d189c6 | 3962 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3963 | { |
3964 | struct io_splice *sp = &req->splice; | |
3965 | struct file *in = sp->file_in; | |
3966 | struct file *out = sp->file_out; | |
3967 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3968 | long ret = 0; | |
3969 | ||
45d189c6 | 3970 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3971 | return -EAGAIN; |
3972 | if (sp->len) | |
3973 | ret = do_tee(in, out, sp->len, flags); | |
3974 | ||
3975 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3976 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3977 | ||
f2a8d5c7 PB |
3978 | if (ret != sp->len) |
3979 | req_set_fail_links(req); | |
e1e16097 | 3980 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3981 | return 0; |
3982 | } | |
3983 | ||
3984 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3985 | { | |
3986 | struct io_splice* sp = &req->splice; | |
3987 | ||
3988 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3989 | sp->off_out = READ_ONCE(sqe->off); | |
3990 | return __io_splice_prep(req, sqe); | |
3991 | } | |
3992 | ||
45d189c6 | 3993 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3994 | { |
3995 | struct io_splice *sp = &req->splice; | |
3996 | struct file *in = sp->file_in; | |
3997 | struct file *out = sp->file_out; | |
3998 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3999 | loff_t *poff_in, *poff_out; | |
c9687426 | 4000 | long ret = 0; |
7d67af2c | 4001 | |
45d189c6 | 4002 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4003 | return -EAGAIN; |
7d67af2c PB |
4004 | |
4005 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4006 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4007 | |
948a7749 | 4008 | if (sp->len) |
c9687426 | 4009 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
4010 | |
4011 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
4012 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4013 | ||
7d67af2c PB |
4014 | if (ret != sp->len) |
4015 | req_set_fail_links(req); | |
e1e16097 | 4016 | io_req_complete(req, ret); |
7d67af2c PB |
4017 | return 0; |
4018 | } | |
4019 | ||
2b188cc1 JA |
4020 | /* |
4021 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4022 | */ | |
889fca73 | 4023 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4024 | { |
4025 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4026 | |
def596e9 JA |
4027 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4028 | return -EINVAL; | |
4029 | ||
889fca73 | 4030 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4031 | return 0; |
4032 | } | |
4033 | ||
1155c76a | 4034 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4035 | { |
6b06314c | 4036 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4037 | |
09bb8394 JA |
4038 | if (!req->file) |
4039 | return -EBADF; | |
c992fe29 | 4040 | |
6b06314c | 4041 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4042 | return -EINVAL; |
edafccee | 4043 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
4044 | return -EINVAL; |
4045 | ||
8ed8d3c3 JA |
4046 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4047 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4048 | return -EINVAL; | |
4049 | ||
4050 | req->sync.off = READ_ONCE(sqe->off); | |
4051 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4052 | return 0; |
4053 | } | |
4054 | ||
45d189c6 | 4055 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4056 | { |
8ed8d3c3 | 4057 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4058 | int ret; |
4059 | ||
ac45abc0 | 4060 | /* fsync always requires a blocking context */ |
45d189c6 | 4061 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4062 | return -EAGAIN; |
4063 | ||
9adbd45d | 4064 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4065 | end > 0 ? end : LLONG_MAX, |
4066 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4067 | if (ret < 0) | |
4068 | req_set_fail_links(req); | |
e1e16097 | 4069 | io_req_complete(req, ret); |
c992fe29 CH |
4070 | return 0; |
4071 | } | |
4072 | ||
d63d1b5e JA |
4073 | static int io_fallocate_prep(struct io_kiocb *req, |
4074 | const struct io_uring_sqe *sqe) | |
4075 | { | |
4076 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
4077 | return -EINVAL; | |
3232dd02 PB |
4078 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4079 | return -EINVAL; | |
d63d1b5e JA |
4080 | |
4081 | req->sync.off = READ_ONCE(sqe->off); | |
4082 | req->sync.len = READ_ONCE(sqe->addr); | |
4083 | req->sync.mode = READ_ONCE(sqe->len); | |
4084 | return 0; | |
4085 | } | |
4086 | ||
45d189c6 | 4087 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4088 | { |
ac45abc0 PB |
4089 | int ret; |
4090 | ||
d63d1b5e | 4091 | /* fallocate always requiring blocking context */ |
45d189c6 | 4092 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4093 | return -EAGAIN; |
ac45abc0 PB |
4094 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4095 | req->sync.len); | |
ac45abc0 PB |
4096 | if (ret < 0) |
4097 | req_set_fail_links(req); | |
e1e16097 | 4098 | io_req_complete(req, ret); |
5d17b4a4 JA |
4099 | return 0; |
4100 | } | |
4101 | ||
ec65fea5 | 4102 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4103 | { |
f8748881 | 4104 | const char __user *fname; |
15b71abe | 4105 | int ret; |
b7bb4f7d | 4106 | |
ec65fea5 | 4107 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4108 | return -EINVAL; |
ec65fea5 | 4109 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4110 | return -EBADF; |
03b1230c | 4111 | |
ec65fea5 PB |
4112 | /* open.how should be already initialised */ |
4113 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4114 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4115 | |
25e72d10 PB |
4116 | req->open.dfd = READ_ONCE(sqe->fd); |
4117 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4118 | req->open.filename = getname(fname); |
15b71abe JA |
4119 | if (IS_ERR(req->open.filename)) { |
4120 | ret = PTR_ERR(req->open.filename); | |
4121 | req->open.filename = NULL; | |
4122 | return ret; | |
4123 | } | |
4022e7af | 4124 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4125 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4126 | return 0; |
03b1230c JA |
4127 | } |
4128 | ||
ec65fea5 PB |
4129 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4130 | { | |
4131 | u64 flags, mode; | |
4132 | ||
14587a46 | 4133 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4134 | return -EINVAL; |
ec65fea5 PB |
4135 | mode = READ_ONCE(sqe->len); |
4136 | flags = READ_ONCE(sqe->open_flags); | |
4137 | req->open.how = build_open_how(flags, mode); | |
4138 | return __io_openat_prep(req, sqe); | |
4139 | } | |
4140 | ||
cebdb986 | 4141 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4142 | { |
cebdb986 | 4143 | struct open_how __user *how; |
cebdb986 | 4144 | size_t len; |
0fa03c62 JA |
4145 | int ret; |
4146 | ||
14587a46 | 4147 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4148 | return -EINVAL; |
cebdb986 JA |
4149 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4150 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4151 | if (len < OPEN_HOW_SIZE_VER0) |
4152 | return -EINVAL; | |
3529d8c2 | 4153 | |
cebdb986 JA |
4154 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4155 | len); | |
4156 | if (ret) | |
4157 | return ret; | |
3529d8c2 | 4158 | |
ec65fea5 | 4159 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4160 | } |
4161 | ||
45d189c6 | 4162 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4163 | { |
4164 | struct open_flags op; | |
15b71abe | 4165 | struct file *file; |
3a81fd02 JA |
4166 | bool nonblock_set; |
4167 | bool resolve_nonblock; | |
15b71abe JA |
4168 | int ret; |
4169 | ||
cebdb986 | 4170 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4171 | if (ret) |
4172 | goto err; | |
3a81fd02 JA |
4173 | nonblock_set = op.open_flag & O_NONBLOCK; |
4174 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4175 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4176 | /* |
4177 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4178 | * it'll always -EAGAIN | |
4179 | */ | |
4180 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4181 | return -EAGAIN; | |
4182 | op.lookup_flags |= LOOKUP_CACHED; | |
4183 | op.open_flag |= O_NONBLOCK; | |
4184 | } | |
15b71abe | 4185 | |
4022e7af | 4186 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4187 | if (ret < 0) |
4188 | goto err; | |
4189 | ||
4190 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 4191 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
4192 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
4193 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 4194 | /* |
3a81fd02 JA |
4195 | * We could hang on to this 'fd', but seems like marginal |
4196 | * gain for something that is now known to be a slower path. | |
4197 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 4198 | */ |
3a81fd02 JA |
4199 | put_unused_fd(ret); |
4200 | return -EAGAIN; | |
4201 | } | |
4202 | ||
15b71abe JA |
4203 | if (IS_ERR(file)) { |
4204 | put_unused_fd(ret); | |
4205 | ret = PTR_ERR(file); | |
4206 | } else { | |
45d189c6 | 4207 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 4208 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
4209 | fsnotify_open(file); |
4210 | fd_install(ret, file); | |
4211 | } | |
4212 | err: | |
4213 | putname(req->open.filename); | |
8fef80bf | 4214 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4215 | if (ret < 0) |
4216 | req_set_fail_links(req); | |
e1e16097 | 4217 | io_req_complete(req, ret); |
15b71abe JA |
4218 | return 0; |
4219 | } | |
4220 | ||
45d189c6 | 4221 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4222 | { |
45d189c6 | 4223 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
4224 | } |
4225 | ||
067524e9 JA |
4226 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4227 | const struct io_uring_sqe *sqe) | |
4228 | { | |
4229 | struct io_provide_buf *p = &req->pbuf; | |
4230 | u64 tmp; | |
4231 | ||
4232 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4233 | return -EINVAL; | |
4234 | ||
4235 | tmp = READ_ONCE(sqe->fd); | |
4236 | if (!tmp || tmp > USHRT_MAX) | |
4237 | return -EINVAL; | |
4238 | ||
4239 | memset(p, 0, sizeof(*p)); | |
4240 | p->nbufs = tmp; | |
4241 | p->bgid = READ_ONCE(sqe->buf_group); | |
4242 | return 0; | |
4243 | } | |
4244 | ||
4245 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4246 | int bgid, unsigned nbufs) | |
4247 | { | |
4248 | unsigned i = 0; | |
4249 | ||
4250 | /* shouldn't happen */ | |
4251 | if (!nbufs) | |
4252 | return 0; | |
4253 | ||
4254 | /* the head kbuf is the list itself */ | |
4255 | while (!list_empty(&buf->list)) { | |
4256 | struct io_buffer *nxt; | |
4257 | ||
4258 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4259 | list_del(&nxt->list); | |
4260 | kfree(nxt); | |
4261 | if (++i == nbufs) | |
4262 | return i; | |
4263 | } | |
4264 | i++; | |
4265 | kfree(buf); | |
4266 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4267 | ||
4268 | return i; | |
4269 | } | |
4270 | ||
889fca73 | 4271 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4272 | { |
4273 | struct io_provide_buf *p = &req->pbuf; | |
4274 | struct io_ring_ctx *ctx = req->ctx; | |
4275 | struct io_buffer *head; | |
4276 | int ret = 0; | |
45d189c6 | 4277 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4278 | |
4279 | io_ring_submit_lock(ctx, !force_nonblock); | |
4280 | ||
4281 | lockdep_assert_held(&ctx->uring_lock); | |
4282 | ||
4283 | ret = -ENOENT; | |
4284 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4285 | if (head) | |
4286 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4287 | if (ret < 0) |
4288 | req_set_fail_links(req); | |
067524e9 | 4289 | |
31bff9a5 PB |
4290 | /* need to hold the lock to complete IOPOLL requests */ |
4291 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4292 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4293 | io_ring_submit_unlock(ctx, !force_nonblock); |
4294 | } else { | |
4295 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4296 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4297 | } |
067524e9 JA |
4298 | return 0; |
4299 | } | |
4300 | ||
ddf0322d JA |
4301 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4302 | const struct io_uring_sqe *sqe) | |
4303 | { | |
4304 | struct io_provide_buf *p = &req->pbuf; | |
4305 | u64 tmp; | |
4306 | ||
4307 | if (sqe->ioprio || sqe->rw_flags) | |
4308 | return -EINVAL; | |
4309 | ||
4310 | tmp = READ_ONCE(sqe->fd); | |
4311 | if (!tmp || tmp > USHRT_MAX) | |
4312 | return -E2BIG; | |
4313 | p->nbufs = tmp; | |
4314 | p->addr = READ_ONCE(sqe->addr); | |
4315 | p->len = READ_ONCE(sqe->len); | |
4316 | ||
efe68c1c | 4317 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4318 | return -EFAULT; |
4319 | ||
4320 | p->bgid = READ_ONCE(sqe->buf_group); | |
4321 | tmp = READ_ONCE(sqe->off); | |
4322 | if (tmp > USHRT_MAX) | |
4323 | return -E2BIG; | |
4324 | p->bid = tmp; | |
4325 | return 0; | |
4326 | } | |
4327 | ||
4328 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4329 | { | |
4330 | struct io_buffer *buf; | |
4331 | u64 addr = pbuf->addr; | |
4332 | int i, bid = pbuf->bid; | |
4333 | ||
4334 | for (i = 0; i < pbuf->nbufs; i++) { | |
4335 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4336 | if (!buf) | |
4337 | break; | |
4338 | ||
4339 | buf->addr = addr; | |
4340 | buf->len = pbuf->len; | |
4341 | buf->bid = bid; | |
4342 | addr += pbuf->len; | |
4343 | bid++; | |
4344 | if (!*head) { | |
4345 | INIT_LIST_HEAD(&buf->list); | |
4346 | *head = buf; | |
4347 | } else { | |
4348 | list_add_tail(&buf->list, &(*head)->list); | |
4349 | } | |
4350 | } | |
4351 | ||
4352 | return i ? i : -ENOMEM; | |
4353 | } | |
4354 | ||
889fca73 | 4355 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4356 | { |
4357 | struct io_provide_buf *p = &req->pbuf; | |
4358 | struct io_ring_ctx *ctx = req->ctx; | |
4359 | struct io_buffer *head, *list; | |
4360 | int ret = 0; | |
45d189c6 | 4361 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4362 | |
4363 | io_ring_submit_lock(ctx, !force_nonblock); | |
4364 | ||
4365 | lockdep_assert_held(&ctx->uring_lock); | |
4366 | ||
4367 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4368 | ||
4369 | ret = io_add_buffers(p, &head); | |
4370 | if (ret < 0) | |
4371 | goto out; | |
4372 | ||
4373 | if (!list) { | |
4374 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4375 | GFP_KERNEL); | |
4376 | if (ret < 0) { | |
067524e9 | 4377 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4378 | goto out; |
4379 | } | |
4380 | } | |
4381 | out: | |
ddf0322d JA |
4382 | if (ret < 0) |
4383 | req_set_fail_links(req); | |
31bff9a5 PB |
4384 | |
4385 | /* need to hold the lock to complete IOPOLL requests */ | |
4386 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4387 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4388 | io_ring_submit_unlock(ctx, !force_nonblock); |
4389 | } else { | |
4390 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4391 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4392 | } |
ddf0322d | 4393 | return 0; |
cebdb986 JA |
4394 | } |
4395 | ||
3e4827b0 JA |
4396 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4397 | const struct io_uring_sqe *sqe) | |
4398 | { | |
4399 | #if defined(CONFIG_EPOLL) | |
4400 | if (sqe->ioprio || sqe->buf_index) | |
4401 | return -EINVAL; | |
6ca56f84 | 4402 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4403 | return -EINVAL; |
3e4827b0 JA |
4404 | |
4405 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4406 | req->epoll.op = READ_ONCE(sqe->len); | |
4407 | req->epoll.fd = READ_ONCE(sqe->off); | |
4408 | ||
4409 | if (ep_op_has_event(req->epoll.op)) { | |
4410 | struct epoll_event __user *ev; | |
4411 | ||
4412 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4413 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4414 | return -EFAULT; | |
4415 | } | |
4416 | ||
4417 | return 0; | |
4418 | #else | |
4419 | return -EOPNOTSUPP; | |
4420 | #endif | |
4421 | } | |
4422 | ||
889fca73 | 4423 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4424 | { |
4425 | #if defined(CONFIG_EPOLL) | |
4426 | struct io_epoll *ie = &req->epoll; | |
4427 | int ret; | |
45d189c6 | 4428 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4429 | |
4430 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4431 | if (force_nonblock && ret == -EAGAIN) | |
4432 | return -EAGAIN; | |
4433 | ||
4434 | if (ret < 0) | |
4435 | req_set_fail_links(req); | |
889fca73 | 4436 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4437 | return 0; |
4438 | #else | |
4439 | return -EOPNOTSUPP; | |
4440 | #endif | |
4441 | } | |
4442 | ||
c1ca757b JA |
4443 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4444 | { | |
4445 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4446 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4447 | return -EINVAL; | |
3232dd02 PB |
4448 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4449 | return -EINVAL; | |
c1ca757b JA |
4450 | |
4451 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4452 | req->madvise.len = READ_ONCE(sqe->len); | |
4453 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4454 | return 0; | |
4455 | #else | |
4456 | return -EOPNOTSUPP; | |
4457 | #endif | |
4458 | } | |
4459 | ||
45d189c6 | 4460 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4461 | { |
4462 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4463 | struct io_madvise *ma = &req->madvise; | |
4464 | int ret; | |
4465 | ||
45d189c6 | 4466 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4467 | return -EAGAIN; |
4468 | ||
0726b01e | 4469 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4470 | if (ret < 0) |
4471 | req_set_fail_links(req); | |
e1e16097 | 4472 | io_req_complete(req, ret); |
c1ca757b JA |
4473 | return 0; |
4474 | #else | |
4475 | return -EOPNOTSUPP; | |
4476 | #endif | |
4477 | } | |
4478 | ||
4840e418 JA |
4479 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4480 | { | |
4481 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4482 | return -EINVAL; | |
3232dd02 PB |
4483 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4484 | return -EINVAL; | |
4840e418 JA |
4485 | |
4486 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4487 | req->fadvise.len = READ_ONCE(sqe->len); | |
4488 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4489 | return 0; | |
4490 | } | |
4491 | ||
45d189c6 | 4492 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4493 | { |
4494 | struct io_fadvise *fa = &req->fadvise; | |
4495 | int ret; | |
4496 | ||
45d189c6 | 4497 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4498 | switch (fa->advice) { |
4499 | case POSIX_FADV_NORMAL: | |
4500 | case POSIX_FADV_RANDOM: | |
4501 | case POSIX_FADV_SEQUENTIAL: | |
4502 | break; | |
4503 | default: | |
4504 | return -EAGAIN; | |
4505 | } | |
4506 | } | |
4840e418 JA |
4507 | |
4508 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4509 | if (ret < 0) | |
4510 | req_set_fail_links(req); | |
e1e16097 | 4511 | io_req_complete(req, ret); |
4840e418 JA |
4512 | return 0; |
4513 | } | |
4514 | ||
eddc7ef5 JA |
4515 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4516 | { | |
6ca56f84 | 4517 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4518 | return -EINVAL; |
eddc7ef5 JA |
4519 | if (sqe->ioprio || sqe->buf_index) |
4520 | return -EINVAL; | |
9c280f90 | 4521 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4522 | return -EBADF; |
eddc7ef5 | 4523 | |
1d9e1288 BM |
4524 | req->statx.dfd = READ_ONCE(sqe->fd); |
4525 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4526 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4527 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4528 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4529 | |
4530 | return 0; | |
4531 | } | |
4532 | ||
45d189c6 | 4533 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4534 | { |
1d9e1288 | 4535 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4536 | int ret; |
4537 | ||
45d189c6 | 4538 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4539 | /* only need file table for an actual valid fd */ |
4540 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4541 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4542 | return -EAGAIN; |
5b0bbee4 | 4543 | } |
eddc7ef5 | 4544 | |
e62753e4 BM |
4545 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4546 | ctx->buffer); | |
eddc7ef5 | 4547 | |
eddc7ef5 JA |
4548 | if (ret < 0) |
4549 | req_set_fail_links(req); | |
e1e16097 | 4550 | io_req_complete(req, ret); |
eddc7ef5 JA |
4551 | return 0; |
4552 | } | |
4553 | ||
b5dba59e JA |
4554 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4555 | { | |
14587a46 | 4556 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4557 | return -EINVAL; |
b5dba59e JA |
4558 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4559 | sqe->rw_flags || sqe->buf_index) | |
4560 | return -EINVAL; | |
9c280f90 | 4561 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4562 | return -EBADF; |
b5dba59e JA |
4563 | |
4564 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4565 | return 0; |
b5dba59e JA |
4566 | } |
4567 | ||
889fca73 | 4568 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4569 | { |
9eac1904 | 4570 | struct files_struct *files = current->files; |
3af73b28 | 4571 | struct io_close *close = &req->close; |
9eac1904 JA |
4572 | struct fdtable *fdt; |
4573 | struct file *file; | |
b5dba59e JA |
4574 | int ret; |
4575 | ||
9eac1904 JA |
4576 | file = NULL; |
4577 | ret = -EBADF; | |
4578 | spin_lock(&files->file_lock); | |
4579 | fdt = files_fdtable(files); | |
4580 | if (close->fd >= fdt->max_fds) { | |
4581 | spin_unlock(&files->file_lock); | |
4582 | goto err; | |
4583 | } | |
4584 | file = fdt->fd[close->fd]; | |
4585 | if (!file) { | |
4586 | spin_unlock(&files->file_lock); | |
4587 | goto err; | |
4588 | } | |
4589 | ||
4590 | if (file->f_op == &io_uring_fops) { | |
4591 | spin_unlock(&files->file_lock); | |
4592 | file = NULL; | |
4593 | goto err; | |
3af73b28 | 4594 | } |
b5dba59e JA |
4595 | |
4596 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4597 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4598 | spin_unlock(&files->file_lock); |
0bf0eefd | 4599 | return -EAGAIN; |
a2100672 | 4600 | } |
b5dba59e | 4601 | |
9eac1904 JA |
4602 | ret = __close_fd_get_file(close->fd, &file); |
4603 | spin_unlock(&files->file_lock); | |
4604 | if (ret < 0) { | |
4605 | if (ret == -ENOENT) | |
4606 | ret = -EBADF; | |
4607 | goto err; | |
4608 | } | |
4609 | ||
3af73b28 | 4610 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4611 | ret = filp_close(file, current->files); |
4612 | err: | |
3af73b28 PB |
4613 | if (ret < 0) |
4614 | req_set_fail_links(req); | |
9eac1904 JA |
4615 | if (file) |
4616 | fput(file); | |
889fca73 | 4617 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4618 | return 0; |
b5dba59e JA |
4619 | } |
4620 | ||
1155c76a | 4621 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4622 | { |
4623 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4624 | |
5d17b4a4 JA |
4625 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4626 | return -EINVAL; | |
4627 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4628 | return -EINVAL; | |
4629 | ||
8ed8d3c3 JA |
4630 | req->sync.off = READ_ONCE(sqe->off); |
4631 | req->sync.len = READ_ONCE(sqe->len); | |
4632 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4633 | return 0; |
4634 | } | |
4635 | ||
45d189c6 | 4636 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4637 | { |
8ed8d3c3 JA |
4638 | int ret; |
4639 | ||
ac45abc0 | 4640 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4641 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4642 | return -EAGAIN; |
4643 | ||
9adbd45d | 4644 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4645 | req->sync.flags); |
4646 | if (ret < 0) | |
4647 | req_set_fail_links(req); | |
e1e16097 | 4648 | io_req_complete(req, ret); |
5d17b4a4 JA |
4649 | return 0; |
4650 | } | |
4651 | ||
469956e8 | 4652 | #if defined(CONFIG_NET) |
02d27d89 PB |
4653 | static int io_setup_async_msg(struct io_kiocb *req, |
4654 | struct io_async_msghdr *kmsg) | |
4655 | { | |
e8c2bc1f JA |
4656 | struct io_async_msghdr *async_msg = req->async_data; |
4657 | ||
4658 | if (async_msg) | |
02d27d89 | 4659 | return -EAGAIN; |
e8c2bc1f | 4660 | if (io_alloc_async_data(req)) { |
257e84a5 | 4661 | kfree(kmsg->free_iov); |
02d27d89 PB |
4662 | return -ENOMEM; |
4663 | } | |
e8c2bc1f | 4664 | async_msg = req->async_data; |
02d27d89 | 4665 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4666 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4667 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4668 | /* if were using fast_iov, set it to the new one */ |
4669 | if (!async_msg->free_iov) | |
4670 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4671 | ||
02d27d89 PB |
4672 | return -EAGAIN; |
4673 | } | |
4674 | ||
2ae523ed PB |
4675 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4676 | struct io_async_msghdr *iomsg) | |
4677 | { | |
2ae523ed | 4678 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4679 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4680 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4681 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4682 | } |
4683 | ||
93642ef8 PB |
4684 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4685 | { | |
4686 | int ret; | |
4687 | ||
4688 | if (!io_op_defs[req->opcode].needs_async_data) | |
4689 | return 0; | |
4690 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4691 | if (!ret) | |
4692 | req->flags |= REQ_F_NEED_CLEANUP; | |
4693 | return ret; | |
4694 | } | |
4695 | ||
3529d8c2 | 4696 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4697 | { |
e47293fd | 4698 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4699 | |
d2b6f48b PB |
4700 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4701 | return -EINVAL; | |
4702 | ||
e47293fd | 4703 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4704 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4705 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4706 | |
d8768362 JA |
4707 | #ifdef CONFIG_COMPAT |
4708 | if (req->ctx->compat) | |
4709 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4710 | #endif | |
93642ef8 | 4711 | return 0; |
03b1230c JA |
4712 | } |
4713 | ||
889fca73 | 4714 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4715 | { |
6b754c8b | 4716 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4717 | struct socket *sock; |
7a7cacba | 4718 | unsigned flags; |
0fa03c62 JA |
4719 | int ret; |
4720 | ||
dba4a925 | 4721 | sock = sock_from_file(req->file); |
7a7cacba | 4722 | if (unlikely(!sock)) |
dba4a925 | 4723 | return -ENOTSOCK; |
3529d8c2 | 4724 | |
257e84a5 PB |
4725 | kmsg = req->async_data; |
4726 | if (!kmsg) { | |
7a7cacba PB |
4727 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4728 | if (ret) | |
4729 | return ret; | |
4730 | kmsg = &iomsg; | |
0fa03c62 | 4731 | } |
0fa03c62 | 4732 | |
7a7cacba PB |
4733 | flags = req->sr_msg.msg_flags; |
4734 | if (flags & MSG_DONTWAIT) | |
4735 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4736 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4737 | flags |= MSG_DONTWAIT; |
e47293fd | 4738 | |
7a7cacba | 4739 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4740 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4741 | return io_setup_async_msg(req, kmsg); |
4742 | if (ret == -ERESTARTSYS) | |
4743 | ret = -EINTR; | |
0fa03c62 | 4744 | |
257e84a5 PB |
4745 | /* fast path, check for non-NULL to avoid function call */ |
4746 | if (kmsg->free_iov) | |
4747 | kfree(kmsg->free_iov); | |
99bc4c38 | 4748 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4749 | if (ret < 0) |
4750 | req_set_fail_links(req); | |
889fca73 | 4751 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4752 | return 0; |
03b1230c | 4753 | } |
aa1fa28f | 4754 | |
889fca73 | 4755 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4756 | { |
7a7cacba PB |
4757 | struct io_sr_msg *sr = &req->sr_msg; |
4758 | struct msghdr msg; | |
4759 | struct iovec iov; | |
fddaface | 4760 | struct socket *sock; |
7a7cacba | 4761 | unsigned flags; |
fddaface JA |
4762 | int ret; |
4763 | ||
dba4a925 | 4764 | sock = sock_from_file(req->file); |
7a7cacba | 4765 | if (unlikely(!sock)) |
dba4a925 | 4766 | return -ENOTSOCK; |
fddaface | 4767 | |
7a7cacba PB |
4768 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4769 | if (unlikely(ret)) | |
14db8411 | 4770 | return ret; |
fddaface | 4771 | |
7a7cacba PB |
4772 | msg.msg_name = NULL; |
4773 | msg.msg_control = NULL; | |
4774 | msg.msg_controllen = 0; | |
4775 | msg.msg_namelen = 0; | |
fddaface | 4776 | |
7a7cacba PB |
4777 | flags = req->sr_msg.msg_flags; |
4778 | if (flags & MSG_DONTWAIT) | |
4779 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4780 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4781 | flags |= MSG_DONTWAIT; |
fddaface | 4782 | |
7a7cacba PB |
4783 | msg.msg_flags = flags; |
4784 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4785 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4786 | return -EAGAIN; |
4787 | if (ret == -ERESTARTSYS) | |
4788 | ret = -EINTR; | |
fddaface | 4789 | |
fddaface JA |
4790 | if (ret < 0) |
4791 | req_set_fail_links(req); | |
889fca73 | 4792 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4793 | return 0; |
fddaface JA |
4794 | } |
4795 | ||
1400e697 PB |
4796 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4797 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4798 | { |
4799 | struct io_sr_msg *sr = &req->sr_msg; | |
4800 | struct iovec __user *uiov; | |
4801 | size_t iov_len; | |
4802 | int ret; | |
4803 | ||
1400e697 PB |
4804 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4805 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4806 | if (ret) |
4807 | return ret; | |
4808 | ||
4809 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4810 | if (iov_len > 1) | |
4811 | return -EINVAL; | |
5476dfed | 4812 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4813 | return -EFAULT; |
5476dfed | 4814 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4815 | iomsg->free_iov = NULL; |
52de1fe1 | 4816 | } else { |
257e84a5 | 4817 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4818 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4819 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4820 | false); |
52de1fe1 JA |
4821 | if (ret > 0) |
4822 | ret = 0; | |
4823 | } | |
4824 | ||
4825 | return ret; | |
4826 | } | |
4827 | ||
4828 | #ifdef CONFIG_COMPAT | |
4829 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4830 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4831 | { |
4832 | struct compat_msghdr __user *msg_compat; | |
4833 | struct io_sr_msg *sr = &req->sr_msg; | |
4834 | struct compat_iovec __user *uiov; | |
4835 | compat_uptr_t ptr; | |
4836 | compat_size_t len; | |
4837 | int ret; | |
4838 | ||
270a5940 | 4839 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4840 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4841 | &ptr, &len); |
4842 | if (ret) | |
4843 | return ret; | |
4844 | ||
4845 | uiov = compat_ptr(ptr); | |
4846 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4847 | compat_ssize_t clen; | |
4848 | ||
4849 | if (len > 1) | |
4850 | return -EINVAL; | |
4851 | if (!access_ok(uiov, sizeof(*uiov))) | |
4852 | return -EFAULT; | |
4853 | if (__get_user(clen, &uiov->iov_len)) | |
4854 | return -EFAULT; | |
4855 | if (clen < 0) | |
4856 | return -EINVAL; | |
2d280bc8 | 4857 | sr->len = clen; |
257e84a5 | 4858 | iomsg->free_iov = NULL; |
52de1fe1 | 4859 | } else { |
257e84a5 | 4860 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4861 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4862 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4863 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4864 | if (ret < 0) |
4865 | return ret; | |
4866 | } | |
4867 | ||
4868 | return 0; | |
4869 | } | |
4870 | #endif | |
4871 | ||
1400e697 PB |
4872 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4873 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4874 | { |
1400e697 | 4875 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4876 | |
4877 | #ifdef CONFIG_COMPAT | |
4878 | if (req->ctx->compat) | |
1400e697 | 4879 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4880 | #endif |
52de1fe1 | 4881 | |
1400e697 | 4882 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4883 | } |
4884 | ||
bcda7baa | 4885 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4886 | bool needs_lock) |
bcda7baa JA |
4887 | { |
4888 | struct io_sr_msg *sr = &req->sr_msg; | |
4889 | struct io_buffer *kbuf; | |
4890 | ||
bcda7baa JA |
4891 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4892 | if (IS_ERR(kbuf)) | |
4893 | return kbuf; | |
4894 | ||
4895 | sr->kbuf = kbuf; | |
4896 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4897 | return kbuf; |
fddaface JA |
4898 | } |
4899 | ||
7fbb1b54 PB |
4900 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4901 | { | |
4902 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4903 | } | |
4904 | ||
93642ef8 | 4905 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4906 | { |
99bc4c38 | 4907 | int ret; |
3529d8c2 | 4908 | |
93642ef8 PB |
4909 | if (!io_op_defs[req->opcode].needs_async_data) |
4910 | return 0; | |
4911 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4912 | if (!ret) | |
4913 | req->flags |= REQ_F_NEED_CLEANUP; | |
4914 | return ret; | |
4915 | } | |
4916 | ||
4917 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4918 | { | |
4919 | struct io_sr_msg *sr = &req->sr_msg; | |
4920 | ||
d2b6f48b PB |
4921 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4922 | return -EINVAL; | |
4923 | ||
3529d8c2 | 4924 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4925 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4926 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4927 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4928 | |
d8768362 JA |
4929 | #ifdef CONFIG_COMPAT |
4930 | if (req->ctx->compat) | |
4931 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4932 | #endif | |
93642ef8 | 4933 | return 0; |
aa1fa28f JA |
4934 | } |
4935 | ||
889fca73 | 4936 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4937 | { |
6b754c8b | 4938 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4939 | struct socket *sock; |
7fbb1b54 | 4940 | struct io_buffer *kbuf; |
7a7cacba | 4941 | unsigned flags; |
52de1fe1 | 4942 | int ret, cflags = 0; |
45d189c6 | 4943 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4944 | |
dba4a925 | 4945 | sock = sock_from_file(req->file); |
7a7cacba | 4946 | if (unlikely(!sock)) |
dba4a925 | 4947 | return -ENOTSOCK; |
3529d8c2 | 4948 | |
257e84a5 PB |
4949 | kmsg = req->async_data; |
4950 | if (!kmsg) { | |
7a7cacba PB |
4951 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4952 | if (ret) | |
681fda8d | 4953 | return ret; |
7a7cacba PB |
4954 | kmsg = &iomsg; |
4955 | } | |
03b1230c | 4956 | |
bc02ef33 | 4957 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4958 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4959 | if (IS_ERR(kbuf)) |
52de1fe1 | 4960 | return PTR_ERR(kbuf); |
7a7cacba | 4961 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4962 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4963 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4964 | 1, req->sr_msg.len); |
4965 | } | |
52de1fe1 | 4966 | |
7a7cacba PB |
4967 | flags = req->sr_msg.msg_flags; |
4968 | if (flags & MSG_DONTWAIT) | |
4969 | req->flags |= REQ_F_NOWAIT; | |
4970 | else if (force_nonblock) | |
4971 | flags |= MSG_DONTWAIT; | |
e47293fd | 4972 | |
7a7cacba PB |
4973 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4974 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4975 | if (force_nonblock && ret == -EAGAIN) |
4976 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4977 | if (ret == -ERESTARTSYS) |
4978 | ret = -EINTR; | |
03b1230c | 4979 | |
7fbb1b54 PB |
4980 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4981 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4982 | /* fast path, check for non-NULL to avoid function call */ |
4983 | if (kmsg->free_iov) | |
4984 | kfree(kmsg->free_iov); | |
99bc4c38 | 4985 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4986 | if (ret < 0) |
4987 | req_set_fail_links(req); | |
889fca73 | 4988 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4989 | return 0; |
0fa03c62 | 4990 | } |
5d17b4a4 | 4991 | |
889fca73 | 4992 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4993 | { |
6b754c8b | 4994 | struct io_buffer *kbuf; |
7a7cacba PB |
4995 | struct io_sr_msg *sr = &req->sr_msg; |
4996 | struct msghdr msg; | |
4997 | void __user *buf = sr->buf; | |
fddaface | 4998 | struct socket *sock; |
7a7cacba PB |
4999 | struct iovec iov; |
5000 | unsigned flags; | |
bcda7baa | 5001 | int ret, cflags = 0; |
45d189c6 | 5002 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5003 | |
dba4a925 | 5004 | sock = sock_from_file(req->file); |
7a7cacba | 5005 | if (unlikely(!sock)) |
dba4a925 | 5006 | return -ENOTSOCK; |
fddaface | 5007 | |
bc02ef33 | 5008 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 5009 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
5010 | if (IS_ERR(kbuf)) |
5011 | return PTR_ERR(kbuf); | |
7a7cacba | 5012 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5013 | } |
bcda7baa | 5014 | |
7a7cacba | 5015 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5016 | if (unlikely(ret)) |
5017 | goto out_free; | |
fddaface | 5018 | |
7a7cacba PB |
5019 | msg.msg_name = NULL; |
5020 | msg.msg_control = NULL; | |
5021 | msg.msg_controllen = 0; | |
5022 | msg.msg_namelen = 0; | |
5023 | msg.msg_iocb = NULL; | |
5024 | msg.msg_flags = 0; | |
fddaface | 5025 | |
7a7cacba PB |
5026 | flags = req->sr_msg.msg_flags; |
5027 | if (flags & MSG_DONTWAIT) | |
5028 | req->flags |= REQ_F_NOWAIT; | |
5029 | else if (force_nonblock) | |
5030 | flags |= MSG_DONTWAIT; | |
5031 | ||
5032 | ret = sock_recvmsg(sock, &msg, flags); | |
5033 | if (force_nonblock && ret == -EAGAIN) | |
5034 | return -EAGAIN; | |
5035 | if (ret == -ERESTARTSYS) | |
5036 | ret = -EINTR; | |
14c32eee | 5037 | out_free: |
7fbb1b54 PB |
5038 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5039 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
5040 | if (ret < 0) |
5041 | req_set_fail_links(req); | |
889fca73 | 5042 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5043 | return 0; |
fddaface JA |
5044 | } |
5045 | ||
3529d8c2 | 5046 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5047 | { |
8ed8d3c3 JA |
5048 | struct io_accept *accept = &req->accept; |
5049 | ||
14587a46 | 5050 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5051 | return -EINVAL; |
8042d6ce | 5052 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5053 | return -EINVAL; |
5054 | ||
d55e5f5b JA |
5055 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5056 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5057 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5058 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 5059 | return 0; |
8ed8d3c3 | 5060 | } |
17f2fe35 | 5061 | |
889fca73 | 5062 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5063 | { |
5064 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5065 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5066 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
5067 | int ret; |
5068 | ||
e697deed JX |
5069 | if (req->file->f_flags & O_NONBLOCK) |
5070 | req->flags |= REQ_F_NOWAIT; | |
5071 | ||
8ed8d3c3 | 5072 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
5073 | accept->addr_len, accept->flags, |
5074 | accept->nofile); | |
8ed8d3c3 | 5075 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 5076 | return -EAGAIN; |
ac45abc0 PB |
5077 | if (ret < 0) { |
5078 | if (ret == -ERESTARTSYS) | |
5079 | ret = -EINTR; | |
4e88d6e7 | 5080 | req_set_fail_links(req); |
ac45abc0 | 5081 | } |
889fca73 | 5082 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5083 | return 0; |
8ed8d3c3 JA |
5084 | } |
5085 | ||
93642ef8 PB |
5086 | static int io_connect_prep_async(struct io_kiocb *req) |
5087 | { | |
5088 | struct io_async_connect *io = req->async_data; | |
5089 | struct io_connect *conn = &req->connect; | |
5090 | ||
5091 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5092 | } | |
5093 | ||
3529d8c2 | 5094 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5095 | { |
3529d8c2 | 5096 | struct io_connect *conn = &req->connect; |
f499a021 | 5097 | |
14587a46 | 5098 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
5099 | return -EINVAL; |
5100 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
5101 | return -EINVAL; | |
5102 | ||
3529d8c2 JA |
5103 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5104 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5105 | return 0; |
f499a021 JA |
5106 | } |
5107 | ||
889fca73 | 5108 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5109 | { |
e8c2bc1f | 5110 | struct io_async_connect __io, *io; |
f8e85cf2 | 5111 | unsigned file_flags; |
3fbb51c1 | 5112 | int ret; |
45d189c6 | 5113 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5114 | |
e8c2bc1f JA |
5115 | if (req->async_data) { |
5116 | io = req->async_data; | |
f499a021 | 5117 | } else { |
3529d8c2 JA |
5118 | ret = move_addr_to_kernel(req->connect.addr, |
5119 | req->connect.addr_len, | |
e8c2bc1f | 5120 | &__io.address); |
f499a021 JA |
5121 | if (ret) |
5122 | goto out; | |
5123 | io = &__io; | |
5124 | } | |
5125 | ||
3fbb51c1 JA |
5126 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5127 | ||
e8c2bc1f | 5128 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5129 | req->connect.addr_len, file_flags); |
87f80d62 | 5130 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5131 | if (req->async_data) |
b7bb4f7d | 5132 | return -EAGAIN; |
e8c2bc1f | 5133 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5134 | ret = -ENOMEM; |
5135 | goto out; | |
5136 | } | |
e8c2bc1f JA |
5137 | io = req->async_data; |
5138 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5139 | return -EAGAIN; |
f499a021 | 5140 | } |
f8e85cf2 JA |
5141 | if (ret == -ERESTARTSYS) |
5142 | ret = -EINTR; | |
f499a021 | 5143 | out: |
4e88d6e7 JA |
5144 | if (ret < 0) |
5145 | req_set_fail_links(req); | |
889fca73 | 5146 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5147 | return 0; |
469956e8 Y |
5148 | } |
5149 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5150 | #define IO_NETOP_FN(op) \ |
5151 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5152 | { \ | |
5153 | return -EOPNOTSUPP; \ | |
5154 | } | |
5155 | ||
5156 | #define IO_NETOP_PREP(op) \ | |
5157 | IO_NETOP_FN(op) \ | |
5158 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5159 | { \ | |
5160 | return -EOPNOTSUPP; \ | |
5161 | } \ | |
5162 | ||
5163 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5164 | IO_NETOP_PREP(op) \ | |
5165 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5166 | { \ | |
5167 | return -EOPNOTSUPP; \ | |
5168 | } | |
5169 | ||
5170 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5171 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5172 | IO_NETOP_PREP_ASYNC(connect); | |
5173 | IO_NETOP_PREP(accept); | |
5174 | IO_NETOP_FN(send); | |
5175 | IO_NETOP_FN(recv); | |
469956e8 | 5176 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5177 | |
d7718a9d JA |
5178 | struct io_poll_table { |
5179 | struct poll_table_struct pt; | |
5180 | struct io_kiocb *req; | |
5181 | int error; | |
5182 | }; | |
ce593a6c | 5183 | |
d7718a9d JA |
5184 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5185 | __poll_t mask, task_work_func_t func) | |
5186 | { | |
aa96bf8a | 5187 | int ret; |
d7718a9d JA |
5188 | |
5189 | /* for instances that support it check for an event match first: */ | |
5190 | if (mask && !(mask & poll->events)) | |
5191 | return 0; | |
5192 | ||
5193 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5194 | ||
5195 | list_del_init(&poll->wait.entry); | |
5196 | ||
d7718a9d | 5197 | req->result = mask; |
7cbf1722 | 5198 | req->task_work.func = func; |
6d816e08 JA |
5199 | percpu_ref_get(&req->ctx->refs); |
5200 | ||
d7718a9d | 5201 | /* |
e3aabf95 JA |
5202 | * If this fails, then the task is exiting. When a task exits, the |
5203 | * work gets canceled, so just cancel this request as well instead | |
5204 | * of executing it. We can't safely execute it anyway, as we may not | |
5205 | * have the needed state needed for it anyway. | |
d7718a9d | 5206 | */ |
355fb9e2 | 5207 | ret = io_req_task_work_add(req); |
aa96bf8a | 5208 | if (unlikely(ret)) { |
e3aabf95 | 5209 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 5210 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 5211 | } |
d7718a9d JA |
5212 | return 1; |
5213 | } | |
5214 | ||
74ce6ce4 JA |
5215 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5216 | __acquires(&req->ctx->completion_lock) | |
5217 | { | |
5218 | struct io_ring_ctx *ctx = req->ctx; | |
5219 | ||
5220 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5221 | struct poll_table_struct pt = { ._key = poll->events }; | |
5222 | ||
5223 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5224 | } | |
5225 | ||
5226 | spin_lock_irq(&ctx->completion_lock); | |
5227 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5228 | add_wait_queue(poll->head, &poll->wait); | |
5229 | return true; | |
5230 | } | |
5231 | ||
5232 | return false; | |
5233 | } | |
5234 | ||
d4e7cd36 | 5235 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5236 | { |
e8c2bc1f | 5237 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5238 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5239 | return req->async_data; |
d4e7cd36 JA |
5240 | return req->apoll->double_poll; |
5241 | } | |
5242 | ||
5243 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5244 | { | |
5245 | if (req->opcode == IORING_OP_POLL_ADD) | |
5246 | return &req->poll; | |
5247 | return &req->apoll->poll; | |
5248 | } | |
5249 | ||
5250 | static void io_poll_remove_double(struct io_kiocb *req) | |
5251 | { | |
5252 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5253 | |
5254 | lockdep_assert_held(&req->ctx->completion_lock); | |
5255 | ||
5256 | if (poll && poll->head) { | |
5257 | struct wait_queue_head *head = poll->head; | |
5258 | ||
5259 | spin_lock(&head->lock); | |
5260 | list_del_init(&poll->wait.entry); | |
5261 | if (poll->wait.private) | |
5262 | refcount_dec(&req->refs); | |
5263 | poll->head = NULL; | |
5264 | spin_unlock(&head->lock); | |
5265 | } | |
5266 | } | |
5267 | ||
5268 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5269 | { | |
5270 | struct io_ring_ctx *ctx = req->ctx; | |
5271 | ||
d4e7cd36 | 5272 | io_poll_remove_double(req); |
18bceab1 JA |
5273 | req->poll.done = true; |
5274 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5275 | io_commit_cqring(ctx); | |
5276 | } | |
5277 | ||
dd221f46 | 5278 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5279 | { |
dd221f46 | 5280 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5281 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5282 | struct io_kiocb *nxt; |
18bceab1 JA |
5283 | |
5284 | if (io_poll_rewait(req, &req->poll)) { | |
5285 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5286 | } else { |
5287 | hash_del(&req->hash_node); | |
5288 | io_poll_complete(req, req->result, 0); | |
5289 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5290 | |
dd221f46 PB |
5291 | nxt = io_put_req_find_next(req); |
5292 | io_cqring_ev_posted(ctx); | |
5293 | if (nxt) | |
5294 | __io_req_task_submit(nxt); | |
5295 | } | |
18bceab1 | 5296 | |
6d816e08 | 5297 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5298 | } |
5299 | ||
5300 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5301 | int sync, void *key) | |
5302 | { | |
5303 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5304 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5305 | __poll_t mask = key_to_poll(key); |
5306 | ||
5307 | /* for instances that support it check for an event match first: */ | |
5308 | if (mask && !(mask & poll->events)) | |
5309 | return 0; | |
5310 | ||
8706e04e JA |
5311 | list_del_init(&wait->entry); |
5312 | ||
807abcb0 | 5313 | if (poll && poll->head) { |
18bceab1 JA |
5314 | bool done; |
5315 | ||
807abcb0 JA |
5316 | spin_lock(&poll->head->lock); |
5317 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5318 | if (!done) |
807abcb0 | 5319 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5320 | /* make sure double remove sees this as being gone */ |
5321 | wait->private = NULL; | |
807abcb0 | 5322 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5323 | if (!done) { |
5324 | /* use wait func handler, so it matches the rq type */ | |
5325 | poll->wait.func(&poll->wait, mode, sync, key); | |
5326 | } | |
18bceab1 JA |
5327 | } |
5328 | refcount_dec(&req->refs); | |
5329 | return 1; | |
5330 | } | |
5331 | ||
5332 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5333 | wait_queue_func_t wake_func) | |
5334 | { | |
5335 | poll->head = NULL; | |
5336 | poll->done = false; | |
5337 | poll->canceled = false; | |
5338 | poll->events = events; | |
5339 | INIT_LIST_HEAD(&poll->wait.entry); | |
5340 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5341 | } | |
5342 | ||
5343 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5344 | struct wait_queue_head *head, |
5345 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5346 | { |
5347 | struct io_kiocb *req = pt->req; | |
5348 | ||
5349 | /* | |
5350 | * If poll->head is already set, it's because the file being polled | |
5351 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5352 | * for write). Setup a separate io_poll_iocb if this happens. | |
5353 | */ | |
5354 | if (unlikely(poll->head)) { | |
58852d4d PB |
5355 | struct io_poll_iocb *poll_one = poll; |
5356 | ||
18bceab1 | 5357 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5358 | if (*poll_ptr) { |
18bceab1 JA |
5359 | pt->error = -EINVAL; |
5360 | return; | |
5361 | } | |
5362 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5363 | if (!poll) { | |
5364 | pt->error = -ENOMEM; | |
5365 | return; | |
5366 | } | |
58852d4d | 5367 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5368 | refcount_inc(&req->refs); |
5369 | poll->wait.private = req; | |
807abcb0 | 5370 | *poll_ptr = poll; |
18bceab1 JA |
5371 | } |
5372 | ||
5373 | pt->error = 0; | |
5374 | poll->head = head; | |
a31eb4a2 JX |
5375 | |
5376 | if (poll->events & EPOLLEXCLUSIVE) | |
5377 | add_wait_queue_exclusive(head, &poll->wait); | |
5378 | else | |
5379 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5380 | } |
5381 | ||
5382 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5383 | struct poll_table_struct *p) | |
5384 | { | |
5385 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5386 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5387 | |
807abcb0 | 5388 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5389 | } |
5390 | ||
d7718a9d JA |
5391 | static void io_async_task_func(struct callback_head *cb) |
5392 | { | |
5393 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5394 | struct async_poll *apoll = req->apoll; | |
5395 | struct io_ring_ctx *ctx = req->ctx; | |
5396 | ||
5397 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5398 | ||
74ce6ce4 | 5399 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5400 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5401 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5402 | return; |
d7718a9d JA |
5403 | } |
5404 | ||
31067255 | 5405 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5406 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5407 | hash_del(&req->hash_node); |
2bae047e | 5408 | |
d4e7cd36 | 5409 | io_poll_remove_double(req); |
74ce6ce4 JA |
5410 | spin_unlock_irq(&ctx->completion_lock); |
5411 | ||
0be0b0e3 PB |
5412 | if (!READ_ONCE(apoll->poll.canceled)) |
5413 | __io_req_task_submit(req); | |
5414 | else | |
5415 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5416 | |
6d816e08 | 5417 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5418 | kfree(apoll->double_poll); |
31067255 | 5419 | kfree(apoll); |
d7718a9d JA |
5420 | } |
5421 | ||
5422 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5423 | void *key) | |
5424 | { | |
5425 | struct io_kiocb *req = wait->private; | |
5426 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5427 | ||
5428 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5429 | key_to_poll(key)); | |
5430 | ||
5431 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5432 | } | |
5433 | ||
5434 | static void io_poll_req_insert(struct io_kiocb *req) | |
5435 | { | |
5436 | struct io_ring_ctx *ctx = req->ctx; | |
5437 | struct hlist_head *list; | |
5438 | ||
5439 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5440 | hlist_add_head(&req->hash_node, list); | |
5441 | } | |
5442 | ||
5443 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5444 | struct io_poll_iocb *poll, | |
5445 | struct io_poll_table *ipt, __poll_t mask, | |
5446 | wait_queue_func_t wake_func) | |
5447 | __acquires(&ctx->completion_lock) | |
5448 | { | |
5449 | struct io_ring_ctx *ctx = req->ctx; | |
5450 | bool cancel = false; | |
5451 | ||
4d52f338 | 5452 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5453 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5454 | poll->file = req->file; |
18bceab1 | 5455 | poll->wait.private = req; |
d7718a9d JA |
5456 | |
5457 | ipt->pt._key = mask; | |
5458 | ipt->req = req; | |
5459 | ipt->error = -EINVAL; | |
5460 | ||
d7718a9d JA |
5461 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5462 | ||
5463 | spin_lock_irq(&ctx->completion_lock); | |
5464 | if (likely(poll->head)) { | |
5465 | spin_lock(&poll->head->lock); | |
5466 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5467 | if (ipt->error) | |
5468 | cancel = true; | |
5469 | ipt->error = 0; | |
5470 | mask = 0; | |
5471 | } | |
5472 | if (mask || ipt->error) | |
5473 | list_del_init(&poll->wait.entry); | |
5474 | else if (cancel) | |
5475 | WRITE_ONCE(poll->canceled, true); | |
5476 | else if (!poll->done) /* actually waiting for an event */ | |
5477 | io_poll_req_insert(req); | |
5478 | spin_unlock(&poll->head->lock); | |
5479 | } | |
5480 | ||
5481 | return mask; | |
5482 | } | |
5483 | ||
5484 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5485 | { | |
5486 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5487 | struct io_ring_ctx *ctx = req->ctx; | |
5488 | struct async_poll *apoll; | |
5489 | struct io_poll_table ipt; | |
5490 | __poll_t mask, ret; | |
9dab14b8 | 5491 | int rw; |
d7718a9d JA |
5492 | |
5493 | if (!req->file || !file_can_poll(req->file)) | |
5494 | return false; | |
24c74678 | 5495 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5496 | return false; |
9dab14b8 JA |
5497 | if (def->pollin) |
5498 | rw = READ; | |
5499 | else if (def->pollout) | |
5500 | rw = WRITE; | |
5501 | else | |
5502 | return false; | |
5503 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5504 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5505 | return false; |
5506 | ||
5507 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5508 | if (unlikely(!apoll)) | |
5509 | return false; | |
807abcb0 | 5510 | apoll->double_poll = NULL; |
d7718a9d JA |
5511 | |
5512 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5513 | req->apoll = apoll; |
d7718a9d | 5514 | |
8755d97a | 5515 | mask = 0; |
d7718a9d | 5516 | if (def->pollin) |
8755d97a | 5517 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5518 | if (def->pollout) |
5519 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5520 | |
5521 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5522 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5523 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5524 | mask &= ~POLLIN; | |
5525 | ||
d7718a9d JA |
5526 | mask |= POLLERR | POLLPRI; |
5527 | ||
5528 | ipt.pt._qproc = io_async_queue_proc; | |
5529 | ||
5530 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5531 | io_async_wake); | |
a36da65c | 5532 | if (ret || ipt.error) { |
d4e7cd36 | 5533 | io_poll_remove_double(req); |
d7718a9d | 5534 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5535 | kfree(apoll->double_poll); |
d7718a9d JA |
5536 | kfree(apoll); |
5537 | return false; | |
5538 | } | |
5539 | spin_unlock_irq(&ctx->completion_lock); | |
5540 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5541 | apoll->poll.events); | |
5542 | return true; | |
5543 | } | |
5544 | ||
5545 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5546 | struct io_poll_iocb *poll) | |
221c5eb2 | 5547 | { |
b41e9852 | 5548 | bool do_complete = false; |
221c5eb2 JA |
5549 | |
5550 | spin_lock(&poll->head->lock); | |
5551 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5552 | if (!list_empty(&poll->wait.entry)) { |
5553 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5554 | do_complete = true; |
221c5eb2 JA |
5555 | } |
5556 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5557 | hash_del(&req->hash_node); |
d7718a9d JA |
5558 | return do_complete; |
5559 | } | |
5560 | ||
5561 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5562 | { | |
5563 | bool do_complete; | |
5564 | ||
d4e7cd36 JA |
5565 | io_poll_remove_double(req); |
5566 | ||
d7718a9d JA |
5567 | if (req->opcode == IORING_OP_POLL_ADD) { |
5568 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5569 | } else { | |
3bfa5bcb JA |
5570 | struct async_poll *apoll = req->apoll; |
5571 | ||
d7718a9d | 5572 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5573 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5574 | if (do_complete) { | |
d7718a9d | 5575 | io_put_req(req); |
807abcb0 | 5576 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5577 | kfree(apoll); |
5578 | } | |
b1f573bd XW |
5579 | } |
5580 | ||
b41e9852 JA |
5581 | if (do_complete) { |
5582 | io_cqring_fill_event(req, -ECANCELED); | |
5583 | io_commit_cqring(req->ctx); | |
f254ac04 | 5584 | req_set_fail_links(req); |
216578e5 | 5585 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5586 | } |
5587 | ||
5588 | return do_complete; | |
221c5eb2 JA |
5589 | } |
5590 | ||
76e1b642 JA |
5591 | /* |
5592 | * Returns true if we found and killed one or more poll requests | |
5593 | */ | |
6b81928d PB |
5594 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5595 | struct files_struct *files) | |
221c5eb2 | 5596 | { |
78076bb6 | 5597 | struct hlist_node *tmp; |
221c5eb2 | 5598 | struct io_kiocb *req; |
8e2e1faf | 5599 | int posted = 0, i; |
221c5eb2 JA |
5600 | |
5601 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5602 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5603 | struct hlist_head *list; | |
5604 | ||
5605 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5606 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5607 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5608 | posted += io_poll_remove_one(req); |
5609 | } | |
221c5eb2 JA |
5610 | } |
5611 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5612 | |
8e2e1faf JA |
5613 | if (posted) |
5614 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5615 | |
5616 | return posted != 0; | |
221c5eb2 JA |
5617 | } |
5618 | ||
47f46768 JA |
5619 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5620 | { | |
78076bb6 | 5621 | struct hlist_head *list; |
47f46768 JA |
5622 | struct io_kiocb *req; |
5623 | ||
78076bb6 JA |
5624 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5625 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5626 | if (sqe_addr != req->user_data) |
5627 | continue; | |
5628 | if (io_poll_remove_one(req)) | |
eac406c6 | 5629 | return 0; |
b41e9852 | 5630 | return -EALREADY; |
47f46768 JA |
5631 | } |
5632 | ||
5633 | return -ENOENT; | |
5634 | } | |
5635 | ||
3529d8c2 JA |
5636 | static int io_poll_remove_prep(struct io_kiocb *req, |
5637 | const struct io_uring_sqe *sqe) | |
0969e783 | 5638 | { |
0969e783 JA |
5639 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5640 | return -EINVAL; | |
5641 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5642 | sqe->poll_events) | |
5643 | return -EINVAL; | |
5644 | ||
018043be | 5645 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5646 | return 0; |
5647 | } | |
5648 | ||
221c5eb2 JA |
5649 | /* |
5650 | * Find a running poll command that matches one specified in sqe->addr, | |
5651 | * and remove it if found. | |
5652 | */ | |
61e98203 | 5653 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5654 | { |
5655 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5656 | int ret; |
221c5eb2 | 5657 | |
221c5eb2 | 5658 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5659 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5660 | spin_unlock_irq(&ctx->completion_lock); |
5661 | ||
4e88d6e7 JA |
5662 | if (ret < 0) |
5663 | req_set_fail_links(req); | |
e1e16097 | 5664 | io_req_complete(req, ret); |
221c5eb2 JA |
5665 | return 0; |
5666 | } | |
5667 | ||
221c5eb2 JA |
5668 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5669 | void *key) | |
5670 | { | |
c2f2eb7d JA |
5671 | struct io_kiocb *req = wait->private; |
5672 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5673 | |
d7718a9d | 5674 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5675 | } |
5676 | ||
221c5eb2 JA |
5677 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5678 | struct poll_table_struct *p) | |
5679 | { | |
5680 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5681 | ||
e8c2bc1f | 5682 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5683 | } |
5684 | ||
3529d8c2 | 5685 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5686 | { |
5687 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5688 | u32 events; |
221c5eb2 JA |
5689 | |
5690 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5691 | return -EINVAL; | |
5692 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5693 | return -EINVAL; | |
5694 | ||
5769a351 JX |
5695 | events = READ_ONCE(sqe->poll32_events); |
5696 | #ifdef __BIG_ENDIAN | |
5697 | events = swahw32(events); | |
5698 | #endif | |
a31eb4a2 JX |
5699 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5700 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5701 | return 0; |
5702 | } | |
5703 | ||
61e98203 | 5704 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5705 | { |
5706 | struct io_poll_iocb *poll = &req->poll; | |
5707 | struct io_ring_ctx *ctx = req->ctx; | |
5708 | struct io_poll_table ipt; | |
0969e783 | 5709 | __poll_t mask; |
0969e783 | 5710 | |
d7718a9d | 5711 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5712 | |
d7718a9d JA |
5713 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5714 | io_poll_wake); | |
221c5eb2 | 5715 | |
8c838788 | 5716 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5717 | ipt.error = 0; |
b0dd8a41 | 5718 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5719 | } |
221c5eb2 JA |
5720 | spin_unlock_irq(&ctx->completion_lock); |
5721 | ||
8c838788 JA |
5722 | if (mask) { |
5723 | io_cqring_ev_posted(ctx); | |
014db007 | 5724 | io_put_req(req); |
221c5eb2 | 5725 | } |
8c838788 | 5726 | return ipt.error; |
221c5eb2 JA |
5727 | } |
5728 | ||
5262f567 JA |
5729 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5730 | { | |
ad8a48ac JA |
5731 | struct io_timeout_data *data = container_of(timer, |
5732 | struct io_timeout_data, timer); | |
5733 | struct io_kiocb *req = data->req; | |
5734 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5735 | unsigned long flags; |
5736 | ||
5262f567 | 5737 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5738 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5739 | atomic_set(&req->ctx->cq_timeouts, |
5740 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5741 | ||
78e19bbe | 5742 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5743 | io_commit_cqring(ctx); |
5744 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5745 | ||
5746 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5747 | req_set_fail_links(req); |
5262f567 JA |
5748 | io_put_req(req); |
5749 | return HRTIMER_NORESTART; | |
5750 | } | |
5751 | ||
fbd15848 PB |
5752 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5753 | __u64 user_data) | |
f254ac04 | 5754 | { |
fbd15848 | 5755 | struct io_timeout_data *io; |
47f46768 JA |
5756 | struct io_kiocb *req; |
5757 | int ret = -ENOENT; | |
f254ac04 | 5758 | |
135fcde8 | 5759 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5760 | if (user_data == req->user_data) { |
47f46768 JA |
5761 | ret = 0; |
5762 | break; | |
5763 | } | |
5764 | } | |
5765 | ||
5766 | if (ret == -ENOENT) | |
fbd15848 PB |
5767 | return ERR_PTR(ret); |
5768 | ||
5769 | io = req->async_data; | |
e8c2bc1f | 5770 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5771 | if (ret == -1) |
fbd15848 | 5772 | return ERR_PTR(-EALREADY); |
a71976f3 | 5773 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5774 | return req; |
5775 | } | |
47f46768 | 5776 | |
fbd15848 PB |
5777 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5778 | { | |
5779 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5780 | ||
5781 | if (IS_ERR(req)) | |
5782 | return PTR_ERR(req); | |
f254ac04 JA |
5783 | |
5784 | req_set_fail_links(req); | |
f254ac04 | 5785 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5786 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5787 | return 0; |
5788 | } | |
5789 | ||
9c8e11b3 PB |
5790 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5791 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5792 | { |
9c8e11b3 PB |
5793 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5794 | struct io_timeout_data *data; | |
47f46768 | 5795 | |
9c8e11b3 PB |
5796 | if (IS_ERR(req)) |
5797 | return PTR_ERR(req); | |
47f46768 | 5798 | |
9c8e11b3 PB |
5799 | req->timeout.off = 0; /* noseq */ |
5800 | data = req->async_data; | |
5801 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5802 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5803 | data->timer.function = io_timeout_fn; | |
5804 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5805 | return 0; | |
47f46768 JA |
5806 | } |
5807 | ||
3529d8c2 JA |
5808 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5809 | const struct io_uring_sqe *sqe) | |
b29472ee | 5810 | { |
9c8e11b3 PB |
5811 | struct io_timeout_rem *tr = &req->timeout_rem; |
5812 | ||
b29472ee JA |
5813 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5814 | return -EINVAL; | |
61710e43 DA |
5815 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5816 | return -EINVAL; | |
9c8e11b3 | 5817 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5818 | return -EINVAL; |
5819 | ||
9c8e11b3 PB |
5820 | tr->addr = READ_ONCE(sqe->addr); |
5821 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5822 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5823 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5824 | return -EINVAL; | |
5825 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5826 | return -EFAULT; | |
5827 | } else if (tr->flags) { | |
5828 | /* timeout removal doesn't support flags */ | |
b29472ee | 5829 | return -EINVAL; |
9c8e11b3 | 5830 | } |
b29472ee | 5831 | |
b29472ee JA |
5832 | return 0; |
5833 | } | |
5834 | ||
8662daec PB |
5835 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5836 | { | |
5837 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5838 | : HRTIMER_MODE_REL; | |
5839 | } | |
5840 | ||
11365043 JA |
5841 | /* |
5842 | * Remove or update an existing timeout command | |
5843 | */ | |
61e98203 | 5844 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5845 | { |
9c8e11b3 | 5846 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5847 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5848 | int ret; |
11365043 | 5849 | |
11365043 | 5850 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5851 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5852 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5853 | else |
5854 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5855 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5856 | |
47f46768 | 5857 | io_cqring_fill_event(req, ret); |
11365043 JA |
5858 | io_commit_cqring(ctx); |
5859 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5860 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5861 | if (ret < 0) |
5862 | req_set_fail_links(req); | |
ec9c02ad | 5863 | io_put_req(req); |
11365043 | 5864 | return 0; |
5262f567 JA |
5865 | } |
5866 | ||
3529d8c2 | 5867 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5868 | bool is_timeout_link) |
5262f567 | 5869 | { |
ad8a48ac | 5870 | struct io_timeout_data *data; |
a41525ab | 5871 | unsigned flags; |
56080b02 | 5872 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5873 | |
ad8a48ac | 5874 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5875 | return -EINVAL; |
ad8a48ac | 5876 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5877 | return -EINVAL; |
56080b02 | 5878 | if (off && is_timeout_link) |
2d28390a | 5879 | return -EINVAL; |
a41525ab JA |
5880 | flags = READ_ONCE(sqe->timeout_flags); |
5881 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5882 | return -EINVAL; |
bdf20073 | 5883 | |
bfe68a22 | 5884 | req->timeout.off = off; |
26a61679 | 5885 | |
e8c2bc1f | 5886 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5887 | return -ENOMEM; |
5888 | ||
e8c2bc1f | 5889 | data = req->async_data; |
ad8a48ac | 5890 | data->req = req; |
ad8a48ac JA |
5891 | |
5892 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5893 | return -EFAULT; |
5894 | ||
8662daec | 5895 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5896 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5897 | return 0; | |
5898 | } | |
5899 | ||
61e98203 | 5900 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5901 | { |
ad8a48ac | 5902 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5903 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5904 | struct list_head *entry; |
bfe68a22 | 5905 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5906 | |
733f5c95 | 5907 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5908 | |
5262f567 JA |
5909 | /* |
5910 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5911 | * timeout event to be satisfied. If it isn't set, then this is |
5912 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5913 | */ |
8eb7e2d0 | 5914 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5915 | entry = ctx->timeout_list.prev; |
5916 | goto add; | |
5917 | } | |
5262f567 | 5918 | |
bfe68a22 PB |
5919 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5920 | req->timeout.target_seq = tail + off; | |
5262f567 | 5921 | |
f010505b MDG |
5922 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5923 | * This is safe because ->completion_lock is held, and submissions | |
5924 | * and completions are never mixed in the same ->completion_lock section. | |
5925 | */ | |
5926 | ctx->cq_last_tm_flush = tail; | |
5927 | ||
5262f567 JA |
5928 | /* |
5929 | * Insertion sort, ensuring the first entry in the list is always | |
5930 | * the one we need first. | |
5931 | */ | |
5262f567 | 5932 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5933 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5934 | timeout.list); | |
5262f567 | 5935 | |
8eb7e2d0 | 5936 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5937 | continue; |
bfe68a22 PB |
5938 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5939 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5940 | break; |
5941 | } | |
93bd25bb | 5942 | add: |
135fcde8 | 5943 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5944 | data->timer.function = io_timeout_fn; |
5945 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5946 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5947 | return 0; |
5948 | } | |
5262f567 | 5949 | |
62755e35 JA |
5950 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5951 | { | |
5952 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5953 | ||
5954 | return req->user_data == (unsigned long) data; | |
5955 | } | |
5956 | ||
e977d6d3 | 5957 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5958 | { |
62755e35 | 5959 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5960 | int ret = 0; |
5961 | ||
4f26bda1 | 5962 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5963 | switch (cancel_ret) { |
5964 | case IO_WQ_CANCEL_OK: | |
5965 | ret = 0; | |
5966 | break; | |
5967 | case IO_WQ_CANCEL_RUNNING: | |
5968 | ret = -EALREADY; | |
5969 | break; | |
5970 | case IO_WQ_CANCEL_NOTFOUND: | |
5971 | ret = -ENOENT; | |
5972 | break; | |
5973 | } | |
5974 | ||
e977d6d3 JA |
5975 | return ret; |
5976 | } | |
5977 | ||
47f46768 JA |
5978 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5979 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5980 | int success_ret) |
47f46768 JA |
5981 | { |
5982 | unsigned long flags; | |
5983 | int ret; | |
5984 | ||
5985 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5986 | if (ret != -ENOENT) { | |
5987 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5988 | goto done; | |
5989 | } | |
5990 | ||
5991 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5992 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5993 | if (ret != -ENOENT) | |
5994 | goto done; | |
5995 | ret = io_poll_cancel(ctx, sqe_addr); | |
5996 | done: | |
b0dd8a41 JA |
5997 | if (!ret) |
5998 | ret = success_ret; | |
47f46768 JA |
5999 | io_cqring_fill_event(req, ret); |
6000 | io_commit_cqring(ctx); | |
6001 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6002 | io_cqring_ev_posted(ctx); | |
6003 | ||
4e88d6e7 JA |
6004 | if (ret < 0) |
6005 | req_set_fail_links(req); | |
014db007 | 6006 | io_put_req(req); |
47f46768 JA |
6007 | } |
6008 | ||
3529d8c2 JA |
6009 | static int io_async_cancel_prep(struct io_kiocb *req, |
6010 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6011 | { |
fbf23849 | 6012 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6013 | return -EINVAL; |
61710e43 DA |
6014 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6015 | return -EINVAL; | |
6016 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
6017 | return -EINVAL; |
6018 | ||
fbf23849 JA |
6019 | req->cancel.addr = READ_ONCE(sqe->addr); |
6020 | return 0; | |
6021 | } | |
6022 | ||
61e98203 | 6023 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6024 | { |
6025 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 6026 | |
014db007 | 6027 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
6028 | return 0; |
6029 | } | |
6030 | ||
269bbe5f | 6031 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6032 | const struct io_uring_sqe *sqe) |
6033 | { | |
6ca56f84 JA |
6034 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
6035 | return -EINVAL; | |
61710e43 DA |
6036 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6037 | return -EINVAL; | |
6038 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
6039 | return -EINVAL; |
6040 | ||
269bbe5f BM |
6041 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6042 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6043 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6044 | return -EINVAL; |
269bbe5f | 6045 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6046 | return 0; |
6047 | } | |
6048 | ||
889fca73 | 6049 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6050 | { |
6051 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 6052 | struct io_uring_rsrc_update up; |
05f3fb3c | 6053 | int ret; |
fbf23849 | 6054 | |
45d189c6 | 6055 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 6056 | return -EAGAIN; |
05f3fb3c | 6057 | |
269bbe5f BM |
6058 | up.offset = req->rsrc_update.offset; |
6059 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
6060 | |
6061 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 6062 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6063 | mutex_unlock(&ctx->uring_lock); |
6064 | ||
6065 | if (ret < 0) | |
6066 | req_set_fail_links(req); | |
889fca73 | 6067 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6068 | return 0; |
6069 | } | |
6070 | ||
bfe76559 | 6071 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6072 | { |
d625c6ee | 6073 | switch (req->opcode) { |
e781573e | 6074 | case IORING_OP_NOP: |
bfe76559 | 6075 | return 0; |
f67676d1 JA |
6076 | case IORING_OP_READV: |
6077 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6078 | case IORING_OP_READ: |
bfe76559 | 6079 | return io_read_prep(req, sqe); |
f67676d1 JA |
6080 | case IORING_OP_WRITEV: |
6081 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6082 | case IORING_OP_WRITE: |
bfe76559 | 6083 | return io_write_prep(req, sqe); |
0969e783 | 6084 | case IORING_OP_POLL_ADD: |
bfe76559 | 6085 | return io_poll_add_prep(req, sqe); |
0969e783 | 6086 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 6087 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 6088 | case IORING_OP_FSYNC: |
1155c76a | 6089 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6090 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6091 | return io_sfr_prep(req, sqe); |
03b1230c | 6092 | case IORING_OP_SENDMSG: |
fddaface | 6093 | case IORING_OP_SEND: |
bfe76559 | 6094 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6095 | case IORING_OP_RECVMSG: |
fddaface | 6096 | case IORING_OP_RECV: |
bfe76559 | 6097 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6098 | case IORING_OP_CONNECT: |
bfe76559 | 6099 | return io_connect_prep(req, sqe); |
2d28390a | 6100 | case IORING_OP_TIMEOUT: |
bfe76559 | 6101 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6102 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6103 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6104 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6105 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6106 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6107 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6108 | case IORING_OP_ACCEPT: |
bfe76559 | 6109 | return io_accept_prep(req, sqe); |
d63d1b5e | 6110 | case IORING_OP_FALLOCATE: |
bfe76559 | 6111 | return io_fallocate_prep(req, sqe); |
15b71abe | 6112 | case IORING_OP_OPENAT: |
bfe76559 | 6113 | return io_openat_prep(req, sqe); |
b5dba59e | 6114 | case IORING_OP_CLOSE: |
bfe76559 | 6115 | return io_close_prep(req, sqe); |
05f3fb3c | 6116 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6117 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6118 | case IORING_OP_STATX: |
bfe76559 | 6119 | return io_statx_prep(req, sqe); |
4840e418 | 6120 | case IORING_OP_FADVISE: |
bfe76559 | 6121 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6122 | case IORING_OP_MADVISE: |
bfe76559 | 6123 | return io_madvise_prep(req, sqe); |
cebdb986 | 6124 | case IORING_OP_OPENAT2: |
bfe76559 | 6125 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6126 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6127 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6128 | case IORING_OP_SPLICE: |
bfe76559 | 6129 | return io_splice_prep(req, sqe); |
ddf0322d | 6130 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6131 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6132 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6133 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6134 | case IORING_OP_TEE: |
bfe76559 | 6135 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6136 | case IORING_OP_SHUTDOWN: |
6137 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6138 | case IORING_OP_RENAMEAT: |
6139 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6140 | case IORING_OP_UNLINKAT: |
6141 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6142 | } |
6143 | ||
bfe76559 PB |
6144 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6145 | req->opcode); | |
6146 | return-EINVAL; | |
6147 | } | |
6148 | ||
93642ef8 | 6149 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6150 | { |
93642ef8 PB |
6151 | switch (req->opcode) { |
6152 | case IORING_OP_READV: | |
6153 | case IORING_OP_READ_FIXED: | |
6154 | case IORING_OP_READ: | |
6155 | return io_rw_prep_async(req, READ); | |
6156 | case IORING_OP_WRITEV: | |
6157 | case IORING_OP_WRITE_FIXED: | |
6158 | case IORING_OP_WRITE: | |
6159 | return io_rw_prep_async(req, WRITE); | |
6160 | case IORING_OP_SENDMSG: | |
6161 | case IORING_OP_SEND: | |
6162 | return io_sendmsg_prep_async(req); | |
6163 | case IORING_OP_RECVMSG: | |
6164 | case IORING_OP_RECV: | |
6165 | return io_recvmsg_prep_async(req); | |
6166 | case IORING_OP_CONNECT: | |
6167 | return io_connect_prep_async(req); | |
6168 | } | |
6169 | return 0; | |
6170 | } | |
6171 | ||
be7053b7 | 6172 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 6173 | { |
be7053b7 | 6174 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 6175 | return 0; |
be7053b7 | 6176 | /* some opcodes init it during the inital prep */ |
93642ef8 | 6177 | if (req->async_data) |
be7053b7 PB |
6178 | return 0; |
6179 | if (__io_alloc_async_data(req)) | |
bfe76559 | 6180 | return -EAGAIN; |
be7053b7 | 6181 | return io_req_prep_async(req); |
f67676d1 JA |
6182 | } |
6183 | ||
9cf7c104 PB |
6184 | static u32 io_get_sequence(struct io_kiocb *req) |
6185 | { | |
6186 | struct io_kiocb *pos; | |
6187 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6188 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6189 | |
f2f87370 PB |
6190 | io_for_each_link(pos, req) |
6191 | nr_reqs++; | |
9cf7c104 PB |
6192 | |
6193 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6194 | return total_submitted - nr_reqs; | |
6195 | } | |
6196 | ||
be7053b7 | 6197 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 6198 | { |
a197f664 | 6199 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6200 | struct io_defer_entry *de; |
f67676d1 | 6201 | int ret; |
9cf7c104 | 6202 | u32 seq; |
de0617e4 | 6203 | |
9d858b21 | 6204 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6205 | if (likely(list_empty_careful(&ctx->defer_list) && |
6206 | !(req->flags & REQ_F_IO_DRAIN))) | |
6207 | return 0; | |
6208 | ||
6209 | seq = io_get_sequence(req); | |
6210 | /* Still a chance to pass the sequence check */ | |
6211 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6212 | return 0; |
6213 | ||
be7053b7 PB |
6214 | ret = io_req_defer_prep(req); |
6215 | if (ret) | |
6216 | return ret; | |
cbdcb435 | 6217 | io_prep_async_link(req); |
27dc8338 PB |
6218 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6219 | if (!de) | |
6220 | return -ENOMEM; | |
2d28390a | 6221 | |
de0617e4 | 6222 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6223 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6224 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6225 | kfree(de); |
ae34817b PB |
6226 | io_queue_async_work(req); |
6227 | return -EIOCBQUEUED; | |
de0617e4 JA |
6228 | } |
6229 | ||
915967f6 | 6230 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6231 | de->req = req; |
9cf7c104 | 6232 | de->seq = seq; |
27dc8338 | 6233 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6234 | spin_unlock_irq(&ctx->completion_lock); |
6235 | return -EIOCBQUEUED; | |
6236 | } | |
6237 | ||
3ca405eb | 6238 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6239 | { |
0e1b6fe3 PB |
6240 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6241 | switch (req->opcode) { | |
6242 | case IORING_OP_READV: | |
6243 | case IORING_OP_READ_FIXED: | |
6244 | case IORING_OP_READ: | |
bcda7baa | 6245 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6246 | break; |
6247 | case IORING_OP_RECVMSG: | |
6248 | case IORING_OP_RECV: | |
bcda7baa | 6249 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6250 | break; |
6251 | } | |
6252 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6253 | } |
6254 | ||
0e1b6fe3 PB |
6255 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6256 | switch (req->opcode) { | |
6257 | case IORING_OP_READV: | |
6258 | case IORING_OP_READ_FIXED: | |
6259 | case IORING_OP_READ: | |
6260 | case IORING_OP_WRITEV: | |
6261 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6262 | case IORING_OP_WRITE: { |
6263 | struct io_async_rw *io = req->async_data; | |
6264 | if (io->free_iovec) | |
6265 | kfree(io->free_iovec); | |
0e1b6fe3 | 6266 | break; |
e8c2bc1f | 6267 | } |
0e1b6fe3 | 6268 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6269 | case IORING_OP_SENDMSG: { |
6270 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6271 | |
6272 | kfree(io->free_iov); | |
0e1b6fe3 | 6273 | break; |
e8c2bc1f | 6274 | } |
0e1b6fe3 PB |
6275 | case IORING_OP_SPLICE: |
6276 | case IORING_OP_TEE: | |
6277 | io_put_file(req, req->splice.file_in, | |
6278 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6279 | break; | |
f3cd4850 JA |
6280 | case IORING_OP_OPENAT: |
6281 | case IORING_OP_OPENAT2: | |
6282 | if (req->open.filename) | |
6283 | putname(req->open.filename); | |
6284 | break; | |
80a261fd JA |
6285 | case IORING_OP_RENAMEAT: |
6286 | putname(req->rename.oldpath); | |
6287 | putname(req->rename.newpath); | |
6288 | break; | |
14a1143b JA |
6289 | case IORING_OP_UNLINKAT: |
6290 | putname(req->unlink.filename); | |
6291 | break; | |
0e1b6fe3 PB |
6292 | } |
6293 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6294 | } |
99bc4c38 PB |
6295 | } |
6296 | ||
889fca73 | 6297 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6298 | { |
a197f664 | 6299 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6300 | int ret; |
2b188cc1 | 6301 | |
d625c6ee | 6302 | switch (req->opcode) { |
2b188cc1 | 6303 | case IORING_OP_NOP: |
889fca73 | 6304 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6305 | break; |
6306 | case IORING_OP_READV: | |
edafccee | 6307 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6308 | case IORING_OP_READ: |
889fca73 | 6309 | ret = io_read(req, issue_flags); |
edafccee | 6310 | break; |
3529d8c2 | 6311 | case IORING_OP_WRITEV: |
edafccee | 6312 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6313 | case IORING_OP_WRITE: |
889fca73 | 6314 | ret = io_write(req, issue_flags); |
2b188cc1 | 6315 | break; |
c992fe29 | 6316 | case IORING_OP_FSYNC: |
45d189c6 | 6317 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6318 | break; |
221c5eb2 | 6319 | case IORING_OP_POLL_ADD: |
61e98203 | 6320 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6321 | break; |
6322 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 6323 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 6324 | break; |
5d17b4a4 | 6325 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6326 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6327 | break; |
0fa03c62 | 6328 | case IORING_OP_SENDMSG: |
889fca73 | 6329 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6330 | break; |
fddaface | 6331 | case IORING_OP_SEND: |
889fca73 | 6332 | ret = io_send(req, issue_flags); |
0fa03c62 | 6333 | break; |
aa1fa28f | 6334 | case IORING_OP_RECVMSG: |
889fca73 | 6335 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6336 | break; |
fddaface | 6337 | case IORING_OP_RECV: |
889fca73 | 6338 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6339 | break; |
5262f567 | 6340 | case IORING_OP_TIMEOUT: |
61e98203 | 6341 | ret = io_timeout(req, issue_flags); |
5262f567 | 6342 | break; |
11365043 | 6343 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6344 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6345 | break; |
17f2fe35 | 6346 | case IORING_OP_ACCEPT: |
889fca73 | 6347 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6348 | break; |
f8e85cf2 | 6349 | case IORING_OP_CONNECT: |
889fca73 | 6350 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6351 | break; |
62755e35 | 6352 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6353 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6354 | break; |
d63d1b5e | 6355 | case IORING_OP_FALLOCATE: |
45d189c6 | 6356 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6357 | break; |
15b71abe | 6358 | case IORING_OP_OPENAT: |
45d189c6 | 6359 | ret = io_openat(req, issue_flags); |
15b71abe | 6360 | break; |
b5dba59e | 6361 | case IORING_OP_CLOSE: |
889fca73 | 6362 | ret = io_close(req, issue_flags); |
b5dba59e | 6363 | break; |
05f3fb3c | 6364 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6365 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6366 | break; |
eddc7ef5 | 6367 | case IORING_OP_STATX: |
45d189c6 | 6368 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6369 | break; |
4840e418 | 6370 | case IORING_OP_FADVISE: |
45d189c6 | 6371 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6372 | break; |
c1ca757b | 6373 | case IORING_OP_MADVISE: |
45d189c6 | 6374 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6375 | break; |
cebdb986 | 6376 | case IORING_OP_OPENAT2: |
45d189c6 | 6377 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6378 | break; |
3e4827b0 | 6379 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6380 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6381 | break; |
7d67af2c | 6382 | case IORING_OP_SPLICE: |
45d189c6 | 6383 | ret = io_splice(req, issue_flags); |
7d67af2c | 6384 | break; |
ddf0322d | 6385 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6386 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6387 | break; |
067524e9 | 6388 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6389 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6390 | break; |
f2a8d5c7 | 6391 | case IORING_OP_TEE: |
45d189c6 | 6392 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6393 | break; |
36f4fa68 | 6394 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6395 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6396 | break; |
80a261fd | 6397 | case IORING_OP_RENAMEAT: |
45d189c6 | 6398 | ret = io_renameat(req, issue_flags); |
80a261fd | 6399 | break; |
14a1143b | 6400 | case IORING_OP_UNLINKAT: |
45d189c6 | 6401 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6402 | break; |
2b188cc1 JA |
6403 | default: |
6404 | ret = -EINVAL; | |
6405 | break; | |
6406 | } | |
6407 | ||
def596e9 JA |
6408 | if (ret) |
6409 | return ret; | |
6410 | ||
b532576e JA |
6411 | /* If the op doesn't have a file, we're not polling for it */ |
6412 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6413 | const bool in_async = io_wq_current_is_worker(); |
6414 | ||
11ba820b JA |
6415 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6416 | if (in_async) | |
6417 | mutex_lock(&ctx->uring_lock); | |
6418 | ||
2e9dbe90 | 6419 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6420 | |
6421 | if (in_async) | |
6422 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6423 | } |
6424 | ||
6425 | return 0; | |
2b188cc1 JA |
6426 | } |
6427 | ||
5280f7e5 | 6428 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6429 | { |
6430 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6431 | struct io_kiocb *timeout; |
561fb04a | 6432 | int ret = 0; |
2b188cc1 | 6433 | |
6df1db6b PB |
6434 | timeout = io_prep_linked_timeout(req); |
6435 | if (timeout) | |
6436 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6437 | |
4014d943 | 6438 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6439 | ret = -ECANCELED; |
31b51510 | 6440 | |
561fb04a | 6441 | if (!ret) { |
561fb04a | 6442 | do { |
889fca73 | 6443 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6444 | /* |
6445 | * We can get EAGAIN for polled IO even though we're | |
6446 | * forcing a sync submission from here, since we can't | |
6447 | * wait for request slots on the block side. | |
6448 | */ | |
6449 | if (ret != -EAGAIN) | |
6450 | break; | |
6451 | cond_resched(); | |
6452 | } while (1); | |
6453 | } | |
31b51510 | 6454 | |
a3df7698 | 6455 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6456 | if (ret) { |
a3df7698 PB |
6457 | /* io-wq is going to take one down */ |
6458 | refcount_inc(&req->refs); | |
6459 | io_req_task_queue_fail(req, ret); | |
edafccee | 6460 | } |
2b188cc1 JA |
6461 | } |
6462 | ||
65e19f54 JA |
6463 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6464 | int index) | |
6465 | { | |
269bbe5f | 6466 | struct fixed_rsrc_table *table; |
65e19f54 | 6467 | |
05f3fb3c | 6468 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6469 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6470 | } |
6471 | ||
8371adf5 PB |
6472 | static struct file *io_file_get(struct io_submit_state *state, |
6473 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6474 | { |
a197f664 | 6475 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6476 | struct file *file; |
09bb8394 | 6477 | |
8da11c19 | 6478 | if (fixed) { |
479f517b | 6479 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6480 | return NULL; |
b7620121 | 6481 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6482 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6483 | io_set_resource_node(req); |
09bb8394 | 6484 | } else { |
c826bd7a | 6485 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6486 | file = __io_file_get(state, fd); |
09bb8394 JA |
6487 | } |
6488 | ||
ce3d5aae PB |
6489 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6490 | io_req_track_inflight(req); | |
8371adf5 | 6491 | return file; |
09bb8394 JA |
6492 | } |
6493 | ||
2665abfd | 6494 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6495 | { |
ad8a48ac JA |
6496 | struct io_timeout_data *data = container_of(timer, |
6497 | struct io_timeout_data, timer); | |
90cd7e42 | 6498 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6499 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6500 | unsigned long flags; |
2665abfd JA |
6501 | |
6502 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6503 | prev = req->timeout.head; |
6504 | req->timeout.head = NULL; | |
2665abfd JA |
6505 | |
6506 | /* | |
6507 | * We don't expect the list to be empty, that will only happen if we | |
6508 | * race with the completion of the linked work. | |
6509 | */ | |
90cd7e42 | 6510 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6511 | io_remove_next_linked(prev); |
90cd7e42 PB |
6512 | else |
6513 | prev = NULL; | |
2665abfd JA |
6514 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6515 | ||
6516 | if (prev) { | |
4e88d6e7 | 6517 | req_set_fail_links(prev); |
014db007 | 6518 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6519 | io_put_req_deferred(prev, 1); |
47f46768 | 6520 | } else { |
9ae1f8dd PB |
6521 | io_req_complete_post(req, -ETIME, 0); |
6522 | io_put_req_deferred(req, 1); | |
2665abfd | 6523 | } |
2665abfd JA |
6524 | return HRTIMER_NORESTART; |
6525 | } | |
6526 | ||
7271ef3a | 6527 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6528 | { |
76a46e06 | 6529 | /* |
f2f87370 PB |
6530 | * If the back reference is NULL, then our linked request finished |
6531 | * before we got a chance to setup the timer | |
76a46e06 | 6532 | */ |
90cd7e42 | 6533 | if (req->timeout.head) { |
e8c2bc1f | 6534 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6535 | |
ad8a48ac JA |
6536 | data->timer.function = io_link_timeout_fn; |
6537 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6538 | data->mode); | |
2665abfd | 6539 | } |
7271ef3a JA |
6540 | } |
6541 | ||
6542 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6543 | { | |
6544 | struct io_ring_ctx *ctx = req->ctx; | |
6545 | ||
6546 | spin_lock_irq(&ctx->completion_lock); | |
6547 | __io_queue_linked_timeout(req); | |
76a46e06 | 6548 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6549 | |
2665abfd | 6550 | /* drop submission reference */ |
76a46e06 JA |
6551 | io_put_req(req); |
6552 | } | |
2665abfd | 6553 | |
ad8a48ac | 6554 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6555 | { |
f2f87370 | 6556 | struct io_kiocb *nxt = req->link; |
2665abfd | 6557 | |
f2f87370 PB |
6558 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6559 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6560 | return NULL; |
2665abfd | 6561 | |
90cd7e42 | 6562 | nxt->timeout.head = req; |
900fad45 | 6563 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6564 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6565 | return nxt; |
2665abfd JA |
6566 | } |
6567 | ||
c5eef2b9 | 6568 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6569 | { |
d3d7298d | 6570 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
193155c8 | 6571 | const struct cred *old_creds = NULL; |
e0c5c576 | 6572 | int ret; |
2b188cc1 | 6573 | |
2e5aa6cb PB |
6574 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6575 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
d3d7298d PB |
6576 | req->work.identity->creds != current_cred()) |
6577 | old_creds = override_creds(req->work.identity->creds); | |
193155c8 | 6578 | |
c5eef2b9 | 6579 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6580 | |
d3d7298d PB |
6581 | if (old_creds) |
6582 | revert_creds(old_creds); | |
491381ce JA |
6583 | |
6584 | /* | |
6585 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6586 | * doesn't support non-blocking read/write attempts | |
6587 | */ | |
24c74678 | 6588 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6589 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6590 | /* |
6591 | * Queued up for async execution, worker will release | |
6592 | * submit reference when the iocb is actually submitted. | |
6593 | */ | |
6594 | io_queue_async_work(req); | |
2b188cc1 | 6595 | } |
0d63c148 PB |
6596 | } else if (likely(!ret)) { |
6597 | /* drop submission reference */ | |
e342c807 | 6598 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6599 | struct io_ring_ctx *ctx = req->ctx; |
6600 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6601 | |
6dd0be1e | 6602 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6603 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6604 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6605 | } else { |
d3d7298d | 6606 | io_put_req(req); |
0d63c148 PB |
6607 | } |
6608 | } else { | |
4e88d6e7 | 6609 | req_set_fail_links(req); |
e65ef56d | 6610 | io_put_req(req); |
e1e16097 | 6611 | io_req_complete(req, ret); |
9e645e11 | 6612 | } |
d3d7298d PB |
6613 | if (linked_timeout) |
6614 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6615 | } |
6616 | ||
be7053b7 | 6617 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6618 | { |
6619 | int ret; | |
6620 | ||
be7053b7 | 6621 | ret = io_req_defer(req); |
4fe2c963 JL |
6622 | if (ret) { |
6623 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6624 | fail_req: |
4e88d6e7 | 6625 | req_set_fail_links(req); |
e1e16097 JA |
6626 | io_put_req(req); |
6627 | io_req_complete(req, ret); | |
4fe2c963 | 6628 | } |
2550878f | 6629 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6630 | ret = io_req_defer_prep(req); |
6631 | if (unlikely(ret)) | |
6632 | goto fail_req; | |
ce35a47a JA |
6633 | io_queue_async_work(req); |
6634 | } else { | |
c5eef2b9 | 6635 | __io_queue_sqe(req); |
ce35a47a | 6636 | } |
4fe2c963 JL |
6637 | } |
6638 | ||
b16fed66 PB |
6639 | /* |
6640 | * Check SQE restrictions (opcode and flags). | |
6641 | * | |
6642 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6643 | */ | |
6644 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6645 | struct io_kiocb *req, | |
6646 | unsigned int sqe_flags) | |
4fe2c963 | 6647 | { |
b16fed66 PB |
6648 | if (!ctx->restricted) |
6649 | return true; | |
6650 | ||
6651 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6652 | return false; | |
6653 | ||
6654 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6655 | ctx->restrictions.sqe_flags_required) | |
6656 | return false; | |
6657 | ||
6658 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6659 | ctx->restrictions.sqe_flags_required)) | |
6660 | return false; | |
6661 | ||
6662 | return true; | |
4fe2c963 JL |
6663 | } |
6664 | ||
b16fed66 PB |
6665 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6666 | const struct io_uring_sqe *sqe) | |
6667 | { | |
6668 | struct io_submit_state *state; | |
6669 | unsigned int sqe_flags; | |
6670 | int id, ret = 0; | |
6671 | ||
6672 | req->opcode = READ_ONCE(sqe->opcode); | |
6673 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6674 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6675 | req->user_data = READ_ONCE(sqe->user_data); | |
6676 | req->async_data = NULL; | |
6677 | req->file = NULL; | |
6678 | req->ctx = ctx; | |
6679 | req->link = NULL; | |
6680 | req->fixed_rsrc_refs = NULL; | |
6681 | /* one is dropped after submission, the other at completion */ | |
6682 | refcount_set(&req->refs, 2); | |
6683 | req->task = current; | |
6684 | req->result = 0; | |
863e0560 | 6685 | |
b16fed66 | 6686 | /* enforce forwards compatibility on users */ |
ebf4a5db PB |
6687 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6688 | req->flags = 0; | |
b16fed66 | 6689 | return -EINVAL; |
ebf4a5db | 6690 | } |
b16fed66 PB |
6691 | |
6692 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6693 | return -EINVAL; | |
6694 | ||
6695 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) | |
6696 | return -EFAULT; | |
6697 | ||
6698 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) | |
6699 | return -EACCES; | |
6700 | ||
6701 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6702 | !io_op_defs[req->opcode].buffer_select) | |
6703 | return -EOPNOTSUPP; | |
6704 | ||
6705 | id = READ_ONCE(sqe->personality); | |
6706 | if (id) { | |
6707 | struct io_identity *iod; | |
6708 | ||
6709 | iod = idr_find(&ctx->personality_idr, id); | |
6710 | if (unlikely(!iod)) | |
6711 | return -EINVAL; | |
6712 | refcount_inc(&iod->count); | |
6713 | ||
6714 | __io_req_init_async(req); | |
6715 | get_cred(iod->creds); | |
6716 | req->work.identity = iod; | |
6717 | req->work.flags |= IO_WQ_WORK_CREDS; | |
6718 | } | |
6719 | ||
6720 | state = &ctx->submit_state; | |
6721 | ||
6722 | /* | |
6723 | * Plug now if we have more than 1 IO left after this, and the target | |
6724 | * is potentially a read/write to block based storage. | |
6725 | */ | |
6726 | if (!state->plug_started && state->ios_left > 1 && | |
6727 | io_op_defs[req->opcode].plug) { | |
6728 | blk_start_plug(&state->plug); | |
6729 | state->plug_started = true; | |
6730 | } | |
6731 | ||
6732 | if (io_op_defs[req->opcode].needs_file) { | |
6733 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6734 | ||
6735 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6736 | if (unlikely(!req->file)) | |
6737 | ret = -EBADF; | |
6738 | } | |
6739 | ||
6740 | state->ios_left--; | |
6741 | return ret; | |
6742 | } | |
6743 | ||
a6b8cadc | 6744 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6745 | const struct io_uring_sqe *sqe) |
9e645e11 | 6746 | { |
a1ab7b35 | 6747 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6748 | int ret; |
9e645e11 | 6749 | |
a6b8cadc PB |
6750 | ret = io_init_req(ctx, req, sqe); |
6751 | if (unlikely(ret)) { | |
6752 | fail_req: | |
6753 | io_put_req(req); | |
6754 | io_req_complete(req, ret); | |
de59bc10 PB |
6755 | if (link->head) { |
6756 | /* fail even hard links since we don't submit */ | |
cf109604 | 6757 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6758 | io_put_req(link->head); |
6759 | io_req_complete(link->head, -ECANCELED); | |
6760 | link->head = NULL; | |
6761 | } | |
a6b8cadc PB |
6762 | return ret; |
6763 | } | |
be7053b7 PB |
6764 | ret = io_req_prep(req, sqe); |
6765 | if (unlikely(ret)) | |
6766 | goto fail_req; | |
a6b8cadc | 6767 | |
be7053b7 | 6768 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6769 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6770 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6771 | ||
9e645e11 JA |
6772 | /* |
6773 | * If we already have a head request, queue this one for async | |
6774 | * submittal once the head completes. If we don't have a head but | |
6775 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6776 | * submitted sync once the chain is complete. If none of those | |
6777 | * conditions are true (normal request), then just queue it. | |
6778 | */ | |
863e0560 PB |
6779 | if (link->head) { |
6780 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6781 | |
8cdf2193 PB |
6782 | /* |
6783 | * Taking sequential execution of a link, draining both sides | |
6784 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6785 | * requests in the link. So, it drains the head and the | |
6786 | * next after the link request. The last one is done via | |
6787 | * drain_next flag to persist the effect across calls. | |
6788 | */ | |
ef4ff581 | 6789 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6790 | head->flags |= REQ_F_IO_DRAIN; |
6791 | ctx->drain_next = 1; | |
6792 | } | |
be7053b7 | 6793 | ret = io_req_defer_prep(req); |
cf109604 | 6794 | if (unlikely(ret)) |
a6b8cadc | 6795 | goto fail_req; |
9d76377f | 6796 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6797 | link->last->link = req; |
863e0560 | 6798 | link->last = req; |
32fe525b PB |
6799 | |
6800 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6801 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6802 | io_queue_sqe(head); |
863e0560 | 6803 | link->head = NULL; |
32fe525b | 6804 | } |
9e645e11 | 6805 | } else { |
711be031 PB |
6806 | if (unlikely(ctx->drain_next)) { |
6807 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6808 | ctx->drain_next = 0; |
711be031 | 6809 | } |
ef4ff581 | 6810 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6811 | link->head = req; |
6812 | link->last = req; | |
711be031 | 6813 | } else { |
be7053b7 | 6814 | io_queue_sqe(req); |
711be031 | 6815 | } |
9e645e11 | 6816 | } |
2e6e1fde | 6817 | |
1d4240cc | 6818 | return 0; |
9e645e11 JA |
6819 | } |
6820 | ||
9a56a232 JA |
6821 | /* |
6822 | * Batched submission is done, ensure local IO is flushed out. | |
6823 | */ | |
ba88ff11 PB |
6824 | static void io_submit_state_end(struct io_submit_state *state, |
6825 | struct io_ring_ctx *ctx) | |
9a56a232 | 6826 | { |
a1ab7b35 | 6827 | if (state->link.head) |
de59bc10 | 6828 | io_queue_sqe(state->link.head); |
6dd0be1e | 6829 | if (state->comp.nr) |
ba88ff11 | 6830 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6831 | if (state->plug_started) |
6832 | blk_finish_plug(&state->plug); | |
9f13c35b | 6833 | io_state_file_put(state); |
9a56a232 JA |
6834 | } |
6835 | ||
6836 | /* | |
6837 | * Start submission side cache. | |
6838 | */ | |
6839 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6840 | unsigned int max_ios) |
9a56a232 | 6841 | { |
27926b68 | 6842 | state->plug_started = false; |
9a56a232 | 6843 | state->ios_left = max_ios; |
a1ab7b35 PB |
6844 | /* set only head, no need to init link_last in advance */ |
6845 | state->link.head = NULL; | |
9a56a232 JA |
6846 | } |
6847 | ||
2b188cc1 JA |
6848 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6849 | { | |
75b28aff | 6850 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6851 | |
caf582c6 PB |
6852 | /* |
6853 | * Ensure any loads from the SQEs are done at this point, | |
6854 | * since once we write the new head, the application could | |
6855 | * write new data to them. | |
6856 | */ | |
6857 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6858 | } |
6859 | ||
2b188cc1 | 6860 | /* |
3529d8c2 | 6861 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6862 | * that is mapped by userspace. This means that care needs to be taken to |
6863 | * ensure that reads are stable, as we cannot rely on userspace always | |
6864 | * being a good citizen. If members of the sqe are validated and then later | |
6865 | * used, it's important that those reads are done through READ_ONCE() to | |
6866 | * prevent a re-load down the line. | |
6867 | */ | |
709b302f | 6868 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6869 | { |
75b28aff | 6870 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6871 | unsigned head; |
6872 | ||
6873 | /* | |
6874 | * The cached sq head (or cq tail) serves two purposes: | |
6875 | * | |
6876 | * 1) allows us to batch the cost of updating the user visible | |
6877 | * head updates. | |
6878 | * 2) allows the kernel side to track the head on its own, even | |
6879 | * though the application is the one updating it. | |
6880 | */ | |
4fccfcbb | 6881 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6882 | if (likely(head < ctx->sq_entries)) |
6883 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6884 | |
6885 | /* drop invalid entries */ | |
498ccd9e | 6886 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6887 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6888 | return NULL; |
6889 | } | |
6890 | ||
0f212204 | 6891 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6892 | { |
46c4e16a | 6893 | int submitted = 0; |
6c271ce2 | 6894 | |
c4a2ed72 | 6895 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6896 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6897 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6898 | return -EBUSY; |
6899 | } | |
6c271ce2 | 6900 | |
ee7d46d9 PB |
6901 | /* make sure SQ entry isn't read before tail */ |
6902 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6903 | |
2b85edfc PB |
6904 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6905 | return -EAGAIN; | |
6c271ce2 | 6906 | |
d8a6df10 | 6907 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6908 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6909 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6910 | |
46c4e16a | 6911 | while (submitted < nr) { |
3529d8c2 | 6912 | const struct io_uring_sqe *sqe; |
196be95c | 6913 | struct io_kiocb *req; |
fb5ccc98 | 6914 | |
258b29a9 | 6915 | req = io_alloc_req(ctx); |
196be95c PB |
6916 | if (unlikely(!req)) { |
6917 | if (!submitted) | |
6918 | submitted = -EAGAIN; | |
fb5ccc98 | 6919 | break; |
196be95c | 6920 | } |
4fccfcbb PB |
6921 | sqe = io_get_sqe(ctx); |
6922 | if (unlikely(!sqe)) { | |
6923 | kmem_cache_free(req_cachep, req); | |
6924 | break; | |
6925 | } | |
d3656344 JA |
6926 | /* will complete beyond this point, count as submitted */ |
6927 | submitted++; | |
a1ab7b35 | 6928 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6929 | break; |
6c271ce2 JA |
6930 | } |
6931 | ||
9466f437 PB |
6932 | if (unlikely(submitted != nr)) { |
6933 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6934 | struct io_uring_task *tctx = current->io_uring; |
6935 | int unused = nr - ref_used; | |
9466f437 | 6936 | |
d8a6df10 JA |
6937 | percpu_ref_put_many(&ctx->refs, unused); |
6938 | percpu_counter_sub(&tctx->inflight, unused); | |
6939 | put_task_struct_many(current, unused); | |
9466f437 | 6940 | } |
6c271ce2 | 6941 | |
a1ab7b35 | 6942 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6943 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6944 | io_commit_sqring(ctx); | |
6945 | ||
6c271ce2 JA |
6946 | return submitted; |
6947 | } | |
6948 | ||
23b3628e XW |
6949 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6950 | { | |
6951 | /* Tell userspace we may need a wakeup call */ | |
6952 | spin_lock_irq(&ctx->completion_lock); | |
6953 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6954 | spin_unlock_irq(&ctx->completion_lock); | |
6955 | } | |
6956 | ||
6957 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6958 | { | |
6959 | spin_lock_irq(&ctx->completion_lock); | |
6960 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6961 | spin_unlock_irq(&ctx->completion_lock); | |
6962 | } | |
6963 | ||
08369246 | 6964 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6965 | { |
c8d1ba58 | 6966 | unsigned int to_submit; |
bdcd3eab | 6967 | int ret = 0; |
6c271ce2 | 6968 | |
c8d1ba58 | 6969 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6970 | /* if we're handling multiple rings, cap submit size for fairness */ |
6971 | if (cap_entries && to_submit > 8) | |
6972 | to_submit = 8; | |
6973 | ||
906a3c6f | 6974 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6975 | unsigned nr_events = 0; |
a4c0b3de | 6976 | |
c8d1ba58 | 6977 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6978 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6979 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6980 | |
d9d05217 PB |
6981 | if (to_submit && !ctx->sqo_dead && |
6982 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6983 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6984 | mutex_unlock(&ctx->uring_lock); |
6985 | } | |
6c271ce2 | 6986 | |
90554200 JA |
6987 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6988 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6989 | |
08369246 XW |
6990 | return ret; |
6991 | } | |
6c271ce2 | 6992 | |
08369246 XW |
6993 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6994 | { | |
6995 | struct io_ring_ctx *ctx; | |
6996 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6997 | |
08369246 XW |
6998 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6999 | if (sq_thread_idle < ctx->sq_thread_idle) | |
7000 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 7001 | } |
c1edbf5f | 7002 | |
08369246 | 7003 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7004 | } |
6c271ce2 | 7005 | |
69fb2131 JA |
7006 | static void io_sqd_init_new(struct io_sq_data *sqd) |
7007 | { | |
7008 | struct io_ring_ctx *ctx; | |
7009 | ||
7010 | while (!list_empty(&sqd->ctx_new_list)) { | |
7011 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
7012 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
7013 | complete(&ctx->sq_thread_comp); | |
7014 | } | |
08369246 XW |
7015 | |
7016 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
7017 | } |
7018 | ||
c8d1ba58 JA |
7019 | static int io_sq_thread(void *data) |
7020 | { | |
91d8f519 | 7021 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
7022 | struct files_struct *old_files = current->files; |
7023 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
7024 | const struct cred *old_cred = NULL; |
7025 | struct io_sq_data *sqd = data; | |
7026 | struct io_ring_ctx *ctx; | |
a0d9205f | 7027 | unsigned long timeout = 0; |
08369246 | 7028 | DEFINE_WAIT(wait); |
6c271ce2 | 7029 | |
28cea78a JA |
7030 | task_lock(current); |
7031 | current->files = NULL; | |
7032 | current->nsproxy = NULL; | |
7033 | task_unlock(current); | |
6c271ce2 | 7034 | |
69fb2131 | 7035 | while (!kthread_should_stop()) { |
08369246 XW |
7036 | int ret; |
7037 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
7038 | |
7039 | /* | |
69fb2131 JA |
7040 | * Any changes to the sqd lists are synchronized through the |
7041 | * kthread parking. This synchronizes the thread vs users, | |
7042 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7043 | */ |
65b2b213 | 7044 | if (kthread_should_park()) { |
69fb2131 | 7045 | kthread_parkme(); |
65b2b213 XW |
7046 | /* |
7047 | * When sq thread is unparked, in case the previous park operation | |
7048 | * comes from io_put_sq_data(), which means that sq thread is going | |
7049 | * to be stopped, so here needs to have a check. | |
7050 | */ | |
7051 | if (kthread_should_stop()) | |
7052 | break; | |
7053 | } | |
7143b5ac | 7054 | |
08369246 | 7055 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7056 | io_sqd_init_new(sqd); |
08369246 XW |
7057 | timeout = jiffies + sqd->sq_thread_idle; |
7058 | } | |
6c271ce2 | 7059 | |
08369246 | 7060 | sqt_spin = false; |
e95eee2d | 7061 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7062 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7063 | if (current->cred != ctx->creds) { | |
7064 | if (old_cred) | |
7065 | revert_creds(old_cred); | |
7066 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7067 | } |
91d8f519 | 7068 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7069 | #ifdef CONFIG_AUDIT |
7070 | current->loginuid = ctx->loginuid; | |
7071 | current->sessionid = ctx->sessionid; | |
7072 | #endif | |
bdcd3eab | 7073 | |
08369246 XW |
7074 | ret = __io_sq_thread(ctx, cap_entries); |
7075 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7076 | sqt_spin = true; | |
6c271ce2 | 7077 | |
28cea78a | 7078 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7079 | } |
6c271ce2 | 7080 | |
08369246 | 7081 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7082 | io_run_task_work(); |
d434ab6d | 7083 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7084 | cond_resched(); |
08369246 XW |
7085 | if (sqt_spin) |
7086 | timeout = jiffies + sqd->sq_thread_idle; | |
7087 | continue; | |
7088 | } | |
7089 | ||
08369246 XW |
7090 | needs_sched = true; |
7091 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7092 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7093 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7094 | !list_empty_careful(&ctx->iopoll_list)) { | |
7095 | needs_sched = false; | |
7096 | break; | |
7097 | } | |
7098 | if (io_sqring_entries(ctx)) { | |
7099 | needs_sched = false; | |
7100 | break; | |
7101 | } | |
7102 | } | |
7103 | ||
8b28fdf2 | 7104 | if (needs_sched && !kthread_should_park()) { |
69fb2131 JA |
7105 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7106 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7107 | |
69fb2131 | 7108 | schedule(); |
69fb2131 JA |
7109 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7110 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7111 | } |
08369246 XW |
7112 | |
7113 | finish_wait(&sqd->wait, &wait); | |
7114 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7115 | } |
7116 | ||
4c6e277c | 7117 | io_run_task_work(); |
d434ab6d | 7118 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7119 | |
91d8f519 DZ |
7120 | if (cur_css) |
7121 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7122 | if (old_cred) |
7123 | revert_creds(old_cred); | |
06058632 | 7124 | |
28cea78a JA |
7125 | task_lock(current); |
7126 | current->files = old_files; | |
7127 | current->nsproxy = old_nsproxy; | |
7128 | task_unlock(current); | |
7129 | ||
2bbcd6d3 | 7130 | kthread_parkme(); |
06058632 | 7131 | |
6c271ce2 JA |
7132 | return 0; |
7133 | } | |
7134 | ||
bda52162 JA |
7135 | struct io_wait_queue { |
7136 | struct wait_queue_entry wq; | |
7137 | struct io_ring_ctx *ctx; | |
7138 | unsigned to_wait; | |
7139 | unsigned nr_timeouts; | |
7140 | }; | |
7141 | ||
6c503150 | 7142 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7143 | { |
7144 | struct io_ring_ctx *ctx = iowq->ctx; | |
7145 | ||
7146 | /* | |
d195a66e | 7147 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7148 | * started waiting. For timeouts, we always want to return to userspace, |
7149 | * regardless of event count. | |
7150 | */ | |
6c503150 | 7151 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7152 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7153 | } | |
7154 | ||
7155 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7156 | int wake_flags, void *key) | |
7157 | { | |
7158 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7159 | wq); | |
7160 | ||
6c503150 PB |
7161 | /* |
7162 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7163 | * the task, and the next invocation will do it. | |
7164 | */ | |
7165 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7166 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7167 | return -1; | |
bda52162 JA |
7168 | } |
7169 | ||
af9c1a44 JA |
7170 | static int io_run_task_work_sig(void) |
7171 | { | |
7172 | if (io_run_task_work()) | |
7173 | return 1; | |
7174 | if (!signal_pending(current)) | |
7175 | return 0; | |
792ee0f6 JA |
7176 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7177 | return -ERESTARTSYS; | |
af9c1a44 JA |
7178 | return -EINTR; |
7179 | } | |
7180 | ||
eeb60b9a PB |
7181 | /* when returns >0, the caller should retry */ |
7182 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7183 | struct io_wait_queue *iowq, | |
7184 | signed long *timeout) | |
7185 | { | |
7186 | int ret; | |
7187 | ||
7188 | /* make sure we run task_work before checking for signals */ | |
7189 | ret = io_run_task_work_sig(); | |
7190 | if (ret || io_should_wake(iowq)) | |
7191 | return ret; | |
7192 | /* let the caller flush overflows, retry */ | |
7193 | if (test_bit(0, &ctx->cq_check_overflow)) | |
7194 | return 1; | |
7195 | ||
7196 | *timeout = schedule_timeout(*timeout); | |
7197 | return !*timeout ? -ETIME : 1; | |
7198 | } | |
7199 | ||
2b188cc1 JA |
7200 | /* |
7201 | * Wait until events become available, if we don't already have some. The | |
7202 | * application must reap them itself, as they reside on the shared cq ring. | |
7203 | */ | |
7204 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7205 | const sigset_t __user *sig, size_t sigsz, |
7206 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7207 | { |
bda52162 JA |
7208 | struct io_wait_queue iowq = { |
7209 | .wq = { | |
7210 | .private = current, | |
7211 | .func = io_wake_function, | |
7212 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7213 | }, | |
7214 | .ctx = ctx, | |
7215 | .to_wait = min_events, | |
7216 | }; | |
75b28aff | 7217 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7218 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7219 | int ret; | |
2b188cc1 | 7220 | |
b41e9852 | 7221 | do { |
6c503150 PB |
7222 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7223 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7224 | return 0; |
4c6e277c | 7225 | if (!io_run_task_work()) |
b41e9852 | 7226 | break; |
b41e9852 | 7227 | } while (1); |
2b188cc1 JA |
7228 | |
7229 | if (sig) { | |
9e75ad5d AB |
7230 | #ifdef CONFIG_COMPAT |
7231 | if (in_compat_syscall()) | |
7232 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7233 | sigsz); |
9e75ad5d AB |
7234 | else |
7235 | #endif | |
b772434b | 7236 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7237 | |
2b188cc1 JA |
7238 | if (ret) |
7239 | return ret; | |
7240 | } | |
7241 | ||
c73ebb68 | 7242 | if (uts) { |
c1d5a224 PB |
7243 | struct timespec64 ts; |
7244 | ||
c73ebb68 HX |
7245 | if (get_timespec64(&ts, uts)) |
7246 | return -EFAULT; | |
7247 | timeout = timespec64_to_jiffies(&ts); | |
7248 | } | |
7249 | ||
bda52162 | 7250 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7251 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7252 | do { |
6c503150 | 7253 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7254 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7255 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
7256 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
7257 | finish_wait(&ctx->wait, &iowq.wq); | |
7258 | } while (ret > 0); | |
bda52162 | 7259 | |
b7db41c9 | 7260 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7261 | |
75b28aff | 7262 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7263 | } |
7264 | ||
6b06314c JA |
7265 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7266 | { | |
7267 | #if defined(CONFIG_UNIX) | |
7268 | if (ctx->ring_sock) { | |
7269 | struct sock *sock = ctx->ring_sock->sk; | |
7270 | struct sk_buff *skb; | |
7271 | ||
7272 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7273 | kfree_skb(skb); | |
7274 | } | |
7275 | #else | |
7276 | int i; | |
7277 | ||
65e19f54 JA |
7278 | for (i = 0; i < ctx->nr_user_files; i++) { |
7279 | struct file *file; | |
7280 | ||
7281 | file = io_file_from_index(ctx, i); | |
7282 | if (file) | |
7283 | fput(file); | |
7284 | } | |
6b06314c JA |
7285 | #endif |
7286 | } | |
7287 | ||
00835dce | 7288 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7289 | { |
269bbe5f | 7290 | struct fixed_rsrc_data *data; |
05f3fb3c | 7291 | |
269bbe5f | 7292 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7293 | complete(&data->done); |
7294 | } | |
7295 | ||
2a63b2d9 | 7296 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 7297 | { |
2a63b2d9 | 7298 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
7299 | } |
7300 | ||
2a63b2d9 | 7301 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 7302 | { |
2a63b2d9 BM |
7303 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
7304 | } | |
65e19f54 | 7305 | |
d67d2263 BM |
7306 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7307 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7308 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7309 | { |
2a63b2d9 | 7310 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7311 | rsrc_data->node = ref_node; |
d67d2263 | 7312 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7313 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7314 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7315 | } |
7316 | ||
8bad28d8 | 7317 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 7318 | { |
8bad28d8 | 7319 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 7320 | |
2a63b2d9 | 7321 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7322 | ref_node = data->node; |
e6cb007c | 7323 | data->node = NULL; |
2a63b2d9 | 7324 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7325 | if (ref_node) |
7326 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 | 7327 | } |
05589553 | 7328 | |
8bad28d8 HX |
7329 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7330 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
7331 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
7332 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 7333 | { |
f2303b1f | 7334 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 7335 | int ret; |
05589553 | 7336 | |
8bad28d8 HX |
7337 | if (data->quiesce) |
7338 | return -ENXIO; | |
05589553 | 7339 | |
8bad28d8 | 7340 | data->quiesce = true; |
1ffc5422 | 7341 | do { |
f2303b1f PB |
7342 | ret = -ENOMEM; |
7343 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7344 | if (!backup_node) | |
7345 | break; | |
7346 | backup_node->rsrc_data = data; | |
7347 | backup_node->rsrc_put = rsrc_put; | |
7348 | ||
8bad28d8 HX |
7349 | io_sqe_rsrc_kill_node(ctx, data); |
7350 | percpu_ref_kill(&data->refs); | |
7351 | flush_delayed_work(&ctx->rsrc_put_work); | |
7352 | ||
1ffc5422 | 7353 | ret = wait_for_completion_interruptible(&data->done); |
88f171ab | 7354 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
1ffc5422 | 7355 | break; |
8bad28d8 | 7356 | |
8bad28d8 HX |
7357 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7358 | backup_node = NULL; | |
8bad28d8 | 7359 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7360 | ret = io_run_task_work_sig(); |
8bad28d8 | 7361 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7362 | } while (ret >= 0); |
8bad28d8 | 7363 | data->quiesce = false; |
05f3fb3c | 7364 | |
8bad28d8 HX |
7365 | if (backup_node) |
7366 | destroy_fixed_rsrc_ref_node(backup_node); | |
7367 | return ret; | |
d7954b2b BM |
7368 | } |
7369 | ||
1ad555c6 BM |
7370 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7371 | { | |
7372 | struct fixed_rsrc_data *data; | |
7373 | ||
7374 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7375 | if (!data) | |
7376 | return NULL; | |
7377 | ||
00835dce | 7378 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7379 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7380 | kfree(data); | |
7381 | return NULL; | |
7382 | } | |
7383 | data->ctx = ctx; | |
7384 | init_completion(&data->done); | |
7385 | return data; | |
7386 | } | |
7387 | ||
7388 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7389 | { | |
7390 | percpu_ref_exit(&data->refs); | |
7391 | kfree(data->table); | |
7392 | kfree(data); | |
7393 | } | |
7394 | ||
d7954b2b BM |
7395 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7396 | { | |
7397 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7398 | unsigned nr_tables, i; |
7399 | int ret; | |
7400 | ||
8bad28d8 HX |
7401 | /* |
7402 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7403 | * Since we possibly drop uring lock later in this function to | |
7404 | * run task work. | |
7405 | */ | |
7406 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7407 | return -ENXIO; |
f2303b1f | 7408 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7409 | if (ret) |
7410 | return ret; | |
7411 | ||
6b06314c | 7412 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7413 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7414 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7415 | kfree(data->table[i].files); |
1ad555c6 | 7416 | free_fixed_rsrc_data(data); |
05f3fb3c | 7417 | ctx->file_data = NULL; |
6b06314c JA |
7418 | ctx->nr_user_files = 0; |
7419 | return 0; | |
7420 | } | |
7421 | ||
534ca6d6 | 7422 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7423 | { |
534ca6d6 | 7424 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7425 | /* |
7426 | * The park is a bit of a work-around, without it we get | |
7427 | * warning spews on shutdown with SQPOLL set and affinity | |
7428 | * set to a single CPU. | |
7429 | */ | |
534ca6d6 JA |
7430 | if (sqd->thread) { |
7431 | kthread_park(sqd->thread); | |
7432 | kthread_stop(sqd->thread); | |
7433 | } | |
7434 | ||
7435 | kfree(sqd); | |
7436 | } | |
7437 | } | |
7438 | ||
aa06165d JA |
7439 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7440 | { | |
7441 | struct io_ring_ctx *ctx_attach; | |
7442 | struct io_sq_data *sqd; | |
7443 | struct fd f; | |
7444 | ||
7445 | f = fdget(p->wq_fd); | |
7446 | if (!f.file) | |
7447 | return ERR_PTR(-ENXIO); | |
7448 | if (f.file->f_op != &io_uring_fops) { | |
7449 | fdput(f); | |
7450 | return ERR_PTR(-EINVAL); | |
7451 | } | |
7452 | ||
7453 | ctx_attach = f.file->private_data; | |
7454 | sqd = ctx_attach->sq_data; | |
7455 | if (!sqd) { | |
7456 | fdput(f); | |
7457 | return ERR_PTR(-EINVAL); | |
7458 | } | |
7459 | ||
7460 | refcount_inc(&sqd->refs); | |
7461 | fdput(f); | |
7462 | return sqd; | |
7463 | } | |
7464 | ||
534ca6d6 JA |
7465 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7466 | { | |
7467 | struct io_sq_data *sqd; | |
7468 | ||
aa06165d JA |
7469 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7470 | return io_attach_sq_data(p); | |
7471 | ||
534ca6d6 JA |
7472 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7473 | if (!sqd) | |
7474 | return ERR_PTR(-ENOMEM); | |
7475 | ||
7476 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7477 | INIT_LIST_HEAD(&sqd->ctx_list); |
7478 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7479 | mutex_init(&sqd->ctx_lock); | |
7480 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7481 | init_waitqueue_head(&sqd->wait); |
7482 | return sqd; | |
7483 | } | |
7484 | ||
69fb2131 JA |
7485 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7486 | __releases(&sqd->lock) | |
7487 | { | |
7488 | if (!sqd->thread) | |
7489 | return; | |
7490 | kthread_unpark(sqd->thread); | |
7491 | mutex_unlock(&sqd->lock); | |
7492 | } | |
7493 | ||
7494 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7495 | __acquires(&sqd->lock) | |
7496 | { | |
7497 | if (!sqd->thread) | |
7498 | return; | |
7499 | mutex_lock(&sqd->lock); | |
7500 | kthread_park(sqd->thread); | |
7501 | } | |
7502 | ||
534ca6d6 JA |
7503 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7504 | { | |
7505 | struct io_sq_data *sqd = ctx->sq_data; | |
7506 | ||
7507 | if (sqd) { | |
7508 | if (sqd->thread) { | |
7509 | /* | |
7510 | * We may arrive here from the error branch in | |
7511 | * io_sq_offload_create() where the kthread is created | |
7512 | * without being waked up, thus wake it up now to make | |
7513 | * sure the wait will complete. | |
7514 | */ | |
7515 | wake_up_process(sqd->thread); | |
7516 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7517 | |
7518 | io_sq_thread_park(sqd); | |
7519 | } | |
7520 | ||
7521 | mutex_lock(&sqd->ctx_lock); | |
7522 | list_del(&ctx->sqd_list); | |
08369246 | 7523 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7524 | mutex_unlock(&sqd->ctx_lock); |
7525 | ||
08369246 | 7526 | if (sqd->thread) |
69fb2131 | 7527 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7528 | |
7529 | io_put_sq_data(sqd); | |
7530 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7531 | } |
7532 | } | |
7533 | ||
6b06314c JA |
7534 | static void io_finish_async(struct io_ring_ctx *ctx) |
7535 | { | |
6c271ce2 JA |
7536 | io_sq_thread_stop(ctx); |
7537 | ||
561fb04a JA |
7538 | if (ctx->io_wq) { |
7539 | io_wq_destroy(ctx->io_wq); | |
7540 | ctx->io_wq = NULL; | |
6b06314c JA |
7541 | } |
7542 | } | |
7543 | ||
7544 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7545 | /* |
7546 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7547 | * the io_uring can be safely unregistered on process exit, even if we have | |
7548 | * loops in the file referencing. | |
7549 | */ | |
7550 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7551 | { | |
7552 | struct sock *sk = ctx->ring_sock->sk; | |
7553 | struct scm_fp_list *fpl; | |
7554 | struct sk_buff *skb; | |
08a45173 | 7555 | int i, nr_files; |
6b06314c | 7556 | |
6b06314c JA |
7557 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7558 | if (!fpl) | |
7559 | return -ENOMEM; | |
7560 | ||
7561 | skb = alloc_skb(0, GFP_KERNEL); | |
7562 | if (!skb) { | |
7563 | kfree(fpl); | |
7564 | return -ENOMEM; | |
7565 | } | |
7566 | ||
7567 | skb->sk = sk; | |
6b06314c | 7568 | |
08a45173 | 7569 | nr_files = 0; |
6b06314c JA |
7570 | fpl->user = get_uid(ctx->user); |
7571 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7572 | struct file *file = io_file_from_index(ctx, i + offset); |
7573 | ||
7574 | if (!file) | |
08a45173 | 7575 | continue; |
65e19f54 | 7576 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7577 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7578 | nr_files++; | |
6b06314c JA |
7579 | } |
7580 | ||
08a45173 JA |
7581 | if (nr_files) { |
7582 | fpl->max = SCM_MAX_FD; | |
7583 | fpl->count = nr_files; | |
7584 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7585 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7586 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7587 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7588 | |
08a45173 JA |
7589 | for (i = 0; i < nr_files; i++) |
7590 | fput(fpl->fp[i]); | |
7591 | } else { | |
7592 | kfree_skb(skb); | |
7593 | kfree(fpl); | |
7594 | } | |
6b06314c JA |
7595 | |
7596 | return 0; | |
7597 | } | |
7598 | ||
7599 | /* | |
7600 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7601 | * causes regular reference counting to break down. We rely on the UNIX | |
7602 | * garbage collection to take care of this problem for us. | |
7603 | */ | |
7604 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7605 | { | |
7606 | unsigned left, total; | |
7607 | int ret = 0; | |
7608 | ||
7609 | total = 0; | |
7610 | left = ctx->nr_user_files; | |
7611 | while (left) { | |
7612 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7613 | |
7614 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7615 | if (ret) | |
7616 | break; | |
7617 | left -= this_files; | |
7618 | total += this_files; | |
7619 | } | |
7620 | ||
7621 | if (!ret) | |
7622 | return 0; | |
7623 | ||
7624 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7625 | struct file *file = io_file_from_index(ctx, total); |
7626 | ||
7627 | if (file) | |
7628 | fput(file); | |
6b06314c JA |
7629 | total++; |
7630 | } | |
7631 | ||
7632 | return ret; | |
7633 | } | |
7634 | #else | |
7635 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7636 | { | |
7637 | return 0; | |
7638 | } | |
7639 | #endif | |
7640 | ||
269bbe5f | 7641 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7642 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7643 | { |
7644 | int i; | |
7645 | ||
7646 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7647 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7648 | unsigned this_files; |
7649 | ||
7650 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7651 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7652 | GFP_KERNEL); | |
7653 | if (!table->files) | |
7654 | break; | |
7655 | nr_files -= this_files; | |
7656 | } | |
7657 | ||
7658 | if (i == nr_tables) | |
7659 | return 0; | |
7660 | ||
7661 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7662 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7663 | kfree(table->files); |
7664 | } | |
7665 | return 1; | |
7666 | } | |
7667 | ||
50238531 | 7668 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7669 | { |
50238531 | 7670 | struct file *file = prsrc->file; |
05f3fb3c JA |
7671 | #if defined(CONFIG_UNIX) |
7672 | struct sock *sock = ctx->ring_sock->sk; | |
7673 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7674 | struct sk_buff *skb; | |
7675 | int i; | |
7676 | ||
7677 | __skb_queue_head_init(&list); | |
7678 | ||
7679 | /* | |
7680 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7681 | * remove this entry and rearrange the file array. | |
7682 | */ | |
7683 | skb = skb_dequeue(head); | |
7684 | while (skb) { | |
7685 | struct scm_fp_list *fp; | |
7686 | ||
7687 | fp = UNIXCB(skb).fp; | |
7688 | for (i = 0; i < fp->count; i++) { | |
7689 | int left; | |
7690 | ||
7691 | if (fp->fp[i] != file) | |
7692 | continue; | |
7693 | ||
7694 | unix_notinflight(fp->user, fp->fp[i]); | |
7695 | left = fp->count - 1 - i; | |
7696 | if (left) { | |
7697 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7698 | left * sizeof(struct file *)); | |
7699 | } | |
7700 | fp->count--; | |
7701 | if (!fp->count) { | |
7702 | kfree_skb(skb); | |
7703 | skb = NULL; | |
7704 | } else { | |
7705 | __skb_queue_tail(&list, skb); | |
7706 | } | |
7707 | fput(file); | |
7708 | file = NULL; | |
7709 | break; | |
7710 | } | |
7711 | ||
7712 | if (!file) | |
7713 | break; | |
7714 | ||
7715 | __skb_queue_tail(&list, skb); | |
7716 | ||
7717 | skb = skb_dequeue(head); | |
7718 | } | |
7719 | ||
7720 | if (skb_peek(&list)) { | |
7721 | spin_lock_irq(&head->lock); | |
7722 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7723 | __skb_queue_tail(head, skb); | |
7724 | spin_unlock_irq(&head->lock); | |
7725 | } | |
7726 | #else | |
7727 | fput(file); | |
7728 | #endif | |
7729 | } | |
7730 | ||
269bbe5f | 7731 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7732 | { |
269bbe5f BM |
7733 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7734 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7735 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7736 | |
269bbe5f BM |
7737 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7738 | list_del(&prsrc->list); | |
50238531 | 7739 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7740 | kfree(prsrc); |
65e19f54 | 7741 | } |
05589553 | 7742 | |
05589553 XW |
7743 | percpu_ref_exit(&ref_node->refs); |
7744 | kfree(ref_node); | |
269bbe5f | 7745 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7746 | } |
65e19f54 | 7747 | |
269bbe5f | 7748 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7749 | { |
7750 | struct io_ring_ctx *ctx; | |
7751 | struct llist_node *node; | |
7752 | ||
269bbe5f BM |
7753 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7754 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7755 | |
7756 | while (node) { | |
269bbe5f | 7757 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7758 | struct llist_node *next = node->next; |
7759 | ||
269bbe5f BM |
7760 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7761 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7762 | node = next; |
7763 | } | |
7764 | } | |
7765 | ||
ea64ec02 PB |
7766 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7767 | unsigned i) | |
2faf852d | 7768 | { |
ea64ec02 PB |
7769 | struct fixed_rsrc_table *table; |
7770 | ||
7771 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7772 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7773 | } | |
7774 | ||
00835dce | 7775 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7776 | { |
269bbe5f BM |
7777 | struct fixed_rsrc_ref_node *ref_node; |
7778 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7779 | struct io_ring_ctx *ctx; |
e297822b | 7780 | bool first_add = false; |
4a38aed2 | 7781 | int delay = HZ; |
65e19f54 | 7782 | |
269bbe5f BM |
7783 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7784 | data = ref_node->rsrc_data; | |
e297822b PB |
7785 | ctx = data->ctx; |
7786 | ||
2a63b2d9 | 7787 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7788 | ref_node->done = true; |
7789 | ||
d67d2263 BM |
7790 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7791 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7792 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7793 | /* recycle ref nodes in order */ |
7794 | if (!ref_node->done) | |
7795 | break; | |
7796 | list_del(&ref_node->node); | |
269bbe5f | 7797 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7798 | } |
2a63b2d9 | 7799 | io_rsrc_ref_unlock(ctx); |
05589553 | 7800 | |
e297822b | 7801 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7802 | delay = 0; |
05589553 | 7803 | |
4a38aed2 | 7804 | if (!delay) |
269bbe5f | 7805 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7806 | else if (first_add) |
269bbe5f | 7807 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7808 | } |
65e19f54 | 7809 | |
6802535d | 7810 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7811 | struct io_ring_ctx *ctx) |
05f3fb3c | 7812 | { |
269bbe5f | 7813 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7814 | |
05589553 XW |
7815 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7816 | if (!ref_node) | |
3e2224c5 | 7817 | return NULL; |
05f3fb3c | 7818 | |
00835dce | 7819 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7820 | 0, GFP_KERNEL)) { |
7821 | kfree(ref_node); | |
3e2224c5 | 7822 | return NULL; |
05589553 XW |
7823 | } |
7824 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7825 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7826 | ref_node->done = false; |
05589553 | 7827 | return ref_node; |
05589553 XW |
7828 | } |
7829 | ||
bc9744cd PB |
7830 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7831 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7832 | { |
269bbe5f | 7833 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7834 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7835 | } |
7836 | ||
269bbe5f | 7837 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7838 | { |
7839 | percpu_ref_exit(&ref_node->refs); | |
7840 | kfree(ref_node); | |
65e19f54 JA |
7841 | } |
7842 | ||
ea64ec02 | 7843 | |
6b06314c JA |
7844 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7845 | unsigned nr_args) | |
7846 | { | |
7847 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7848 | unsigned nr_tables, i; |
05f3fb3c | 7849 | struct file *file; |
600cf3f8 | 7850 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7851 | struct fixed_rsrc_ref_node *ref_node; |
7852 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7853 | |
05f3fb3c | 7854 | if (ctx->file_data) |
6b06314c JA |
7855 | return -EBUSY; |
7856 | if (!nr_args) | |
7857 | return -EINVAL; | |
7858 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7859 | return -EMFILE; | |
7860 | ||
1ad555c6 | 7861 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7862 | if (!file_data) |
05f3fb3c | 7863 | return -ENOMEM; |
13770a71 | 7864 | ctx->file_data = file_data; |
05f3fb3c | 7865 | |
65e19f54 | 7866 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7867 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7868 | GFP_KERNEL); |
600cf3f8 PB |
7869 | if (!file_data->table) |
7870 | goto out_free; | |
05f3fb3c | 7871 | |
600cf3f8 | 7872 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7873 | goto out_free; |
65e19f54 | 7874 | |
08a45173 | 7875 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7876 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7877 | ret = -EFAULT; | |
7878 | goto out_fput; | |
7879 | } | |
08a45173 | 7880 | /* allow sparse sets */ |
600cf3f8 | 7881 | if (fd == -1) |
08a45173 | 7882 | continue; |
6b06314c | 7883 | |
05f3fb3c | 7884 | file = fget(fd); |
6b06314c | 7885 | ret = -EBADF; |
05f3fb3c | 7886 | if (!file) |
600cf3f8 | 7887 | goto out_fput; |
05f3fb3c | 7888 | |
6b06314c JA |
7889 | /* |
7890 | * Don't allow io_uring instances to be registered. If UNIX | |
7891 | * isn't enabled, then this causes a reference cycle and this | |
7892 | * instance can never get freed. If UNIX is enabled we'll | |
7893 | * handle it just fine, but there's still no point in allowing | |
7894 | * a ring fd as it doesn't support regular read/write anyway. | |
7895 | */ | |
05f3fb3c JA |
7896 | if (file->f_op == &io_uring_fops) { |
7897 | fput(file); | |
600cf3f8 | 7898 | goto out_fput; |
6b06314c | 7899 | } |
ea64ec02 | 7900 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7901 | } |
7902 | ||
6b06314c | 7903 | ret = io_sqe_files_scm(ctx); |
05589553 | 7904 | if (ret) { |
6b06314c | 7905 | io_sqe_files_unregister(ctx); |
05589553 XW |
7906 | return ret; |
7907 | } | |
6b06314c | 7908 | |
bc9744cd | 7909 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7910 | if (!ref_node) { |
05589553 | 7911 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7912 | return -ENOMEM; |
05589553 | 7913 | } |
bc9744cd | 7914 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7915 | |
d67d2263 | 7916 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7917 | return ret; |
600cf3f8 PB |
7918 | out_fput: |
7919 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7920 | file = io_file_from_index(ctx, i); | |
7921 | if (file) | |
7922 | fput(file); | |
7923 | } | |
7924 | for (i = 0; i < nr_tables; i++) | |
7925 | kfree(file_data->table[i].files); | |
7926 | ctx->nr_user_files = 0; | |
600cf3f8 | 7927 | out_free: |
1ad555c6 | 7928 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7929 | ctx->file_data = NULL; |
6b06314c JA |
7930 | return ret; |
7931 | } | |
7932 | ||
c3a31e60 JA |
7933 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7934 | int index) | |
7935 | { | |
7936 | #if defined(CONFIG_UNIX) | |
7937 | struct sock *sock = ctx->ring_sock->sk; | |
7938 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7939 | struct sk_buff *skb; | |
7940 | ||
7941 | /* | |
7942 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7943 | * file set. If there's no room, fall back to allocating a new skb | |
7944 | * and filling it in. | |
7945 | */ | |
7946 | spin_lock_irq(&head->lock); | |
7947 | skb = skb_peek(head); | |
7948 | if (skb) { | |
7949 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7950 | ||
7951 | if (fpl->count < SCM_MAX_FD) { | |
7952 | __skb_unlink(skb, head); | |
7953 | spin_unlock_irq(&head->lock); | |
7954 | fpl->fp[fpl->count] = get_file(file); | |
7955 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7956 | fpl->count++; | |
7957 | spin_lock_irq(&head->lock); | |
7958 | __skb_queue_head(head, skb); | |
7959 | } else { | |
7960 | skb = NULL; | |
7961 | } | |
7962 | } | |
7963 | spin_unlock_irq(&head->lock); | |
7964 | ||
7965 | if (skb) { | |
7966 | fput(file); | |
7967 | return 0; | |
7968 | } | |
7969 | ||
7970 | return __io_sqe_files_scm(ctx, 1, index); | |
7971 | #else | |
7972 | return 0; | |
7973 | #endif | |
7974 | } | |
7975 | ||
50238531 | 7976 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7977 | { |
269bbe5f BM |
7978 | struct io_rsrc_put *prsrc; |
7979 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7980 | |
269bbe5f BM |
7981 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7982 | if (!prsrc) | |
a5318d3c | 7983 | return -ENOMEM; |
05f3fb3c | 7984 | |
50238531 | 7985 | prsrc->rsrc = rsrc; |
269bbe5f | 7986 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7987 | |
a5318d3c | 7988 | return 0; |
05f3fb3c JA |
7989 | } |
7990 | ||
269bbe5f BM |
7991 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7992 | struct file *file) | |
7993 | { | |
50238531 | 7994 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7995 | } |
7996 | ||
05f3fb3c | 7997 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7998 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7999 | unsigned nr_args) |
8000 | { | |
269bbe5f BM |
8001 | struct fixed_rsrc_data *data = ctx->file_data; |
8002 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 8003 | struct file *file, **file_slot; |
c3a31e60 JA |
8004 | __s32 __user *fds; |
8005 | int fd, i, err; | |
8006 | __u32 done; | |
05589553 | 8007 | bool needs_switch = false; |
c3a31e60 | 8008 | |
05f3fb3c | 8009 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
8010 | return -EOVERFLOW; |
8011 | if (done > ctx->nr_user_files) | |
8012 | return -EINVAL; | |
8013 | ||
bc9744cd | 8014 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
8015 | if (!ref_node) |
8016 | return -ENOMEM; | |
bc9744cd | 8017 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 8018 | |
269bbe5f | 8019 | fds = u64_to_user_ptr(up->data); |
67973b93 | 8020 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
8021 | err = 0; |
8022 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
8023 | err = -EFAULT; | |
8024 | break; | |
8025 | } | |
4e0377a1 | 8026 | if (fd == IORING_REGISTER_FILES_SKIP) |
8027 | continue; | |
8028 | ||
67973b93 | 8029 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
8030 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
8031 | ||
8032 | if (*file_slot) { | |
8033 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
8034 | if (err) |
8035 | break; | |
ea64ec02 | 8036 | *file_slot = NULL; |
05589553 | 8037 | needs_switch = true; |
c3a31e60 JA |
8038 | } |
8039 | if (fd != -1) { | |
c3a31e60 JA |
8040 | file = fget(fd); |
8041 | if (!file) { | |
8042 | err = -EBADF; | |
8043 | break; | |
8044 | } | |
8045 | /* | |
8046 | * Don't allow io_uring instances to be registered. If | |
8047 | * UNIX isn't enabled, then this causes a reference | |
8048 | * cycle and this instance can never get freed. If UNIX | |
8049 | * is enabled we'll handle it just fine, but there's | |
8050 | * still no point in allowing a ring fd as it doesn't | |
8051 | * support regular read/write anyway. | |
8052 | */ | |
8053 | if (file->f_op == &io_uring_fops) { | |
8054 | fput(file); | |
8055 | err = -EBADF; | |
8056 | break; | |
8057 | } | |
e68a3ff8 | 8058 | *file_slot = file; |
c3a31e60 | 8059 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8060 | if (err) { |
e68a3ff8 | 8061 | *file_slot = NULL; |
f3bd9dae | 8062 | fput(file); |
c3a31e60 | 8063 | break; |
f3bd9dae | 8064 | } |
c3a31e60 | 8065 | } |
05f3fb3c JA |
8066 | } |
8067 | ||
05589553 | 8068 | if (needs_switch) { |
b2e96852 | 8069 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 8070 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 8071 | } else |
269bbe5f | 8072 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
8073 | |
8074 | return done ? done : err; | |
8075 | } | |
05589553 | 8076 | |
05f3fb3c JA |
8077 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
8078 | unsigned nr_args) | |
8079 | { | |
269bbe5f | 8080 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
8081 | |
8082 | if (!ctx->file_data) | |
8083 | return -ENXIO; | |
8084 | if (!nr_args) | |
8085 | return -EINVAL; | |
8086 | if (copy_from_user(&up, arg, sizeof(up))) | |
8087 | return -EFAULT; | |
8088 | if (up.resv) | |
8089 | return -EINVAL; | |
8090 | ||
8091 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8092 | } | |
c3a31e60 | 8093 | |
5280f7e5 | 8094 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
8095 | { |
8096 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8097 | ||
5280f7e5 PB |
8098 | req = io_put_req_find_next(req); |
8099 | return req ? &req->work : NULL; | |
7d723065 JA |
8100 | } |
8101 | ||
24369c2e PB |
8102 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
8103 | struct io_uring_params *p) | |
8104 | { | |
8105 | struct io_wq_data data; | |
8106 | struct fd f; | |
8107 | struct io_ring_ctx *ctx_attach; | |
8108 | unsigned int concurrency; | |
8109 | int ret = 0; | |
8110 | ||
8111 | data.user = ctx->user; | |
e9fd9396 | 8112 | data.free_work = io_free_work; |
f5fa38c5 | 8113 | data.do_work = io_wq_submit_work; |
24369c2e PB |
8114 | |
8115 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
8116 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
8117 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
8118 | ||
8119 | ctx->io_wq = io_wq_create(concurrency, &data); | |
8120 | if (IS_ERR(ctx->io_wq)) { | |
8121 | ret = PTR_ERR(ctx->io_wq); | |
8122 | ctx->io_wq = NULL; | |
8123 | } | |
8124 | return ret; | |
8125 | } | |
8126 | ||
8127 | f = fdget(p->wq_fd); | |
8128 | if (!f.file) | |
8129 | return -EBADF; | |
8130 | ||
8131 | if (f.file->f_op != &io_uring_fops) { | |
8132 | ret = -EINVAL; | |
8133 | goto out_fput; | |
8134 | } | |
8135 | ||
8136 | ctx_attach = f.file->private_data; | |
8137 | /* @io_wq is protected by holding the fd */ | |
8138 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8139 | ret = -EINVAL; | |
8140 | goto out_fput; | |
8141 | } | |
8142 | ||
8143 | ctx->io_wq = ctx_attach->io_wq; | |
8144 | out_fput: | |
8145 | fdput(f); | |
8146 | return ret; | |
8147 | } | |
8148 | ||
0f212204 JA |
8149 | static int io_uring_alloc_task_context(struct task_struct *task) |
8150 | { | |
8151 | struct io_uring_task *tctx; | |
d8a6df10 | 8152 | int ret; |
0f212204 JA |
8153 | |
8154 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8155 | if (unlikely(!tctx)) | |
8156 | return -ENOMEM; | |
8157 | ||
d8a6df10 JA |
8158 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8159 | if (unlikely(ret)) { | |
8160 | kfree(tctx); | |
8161 | return ret; | |
8162 | } | |
8163 | ||
0f212204 JA |
8164 | xa_init(&tctx->xa); |
8165 | init_waitqueue_head(&tctx->wait); | |
8166 | tctx->last = NULL; | |
fdaf083c JA |
8167 | atomic_set(&tctx->in_idle, 0); |
8168 | tctx->sqpoll = false; | |
500a373d JA |
8169 | io_init_identity(&tctx->__identity); |
8170 | tctx->identity = &tctx->__identity; | |
0f212204 | 8171 | task->io_uring = tctx; |
7cbf1722 JA |
8172 | spin_lock_init(&tctx->task_lock); |
8173 | INIT_WQ_LIST(&tctx->task_list); | |
8174 | tctx->task_state = 0; | |
8175 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
8176 | return 0; |
8177 | } | |
8178 | ||
8179 | void __io_uring_free(struct task_struct *tsk) | |
8180 | { | |
8181 | struct io_uring_task *tctx = tsk->io_uring; | |
8182 | ||
8183 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8184 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8185 | if (tctx->identity != &tctx->__identity) | |
8186 | kfree(tctx->identity); | |
d8a6df10 | 8187 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8188 | kfree(tctx); |
8189 | tsk->io_uring = NULL; | |
8190 | } | |
8191 | ||
7e84e1c7 SG |
8192 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8193 | struct io_uring_params *p) | |
2b188cc1 JA |
8194 | { |
8195 | int ret; | |
8196 | ||
6c271ce2 | 8197 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8198 | struct io_sq_data *sqd; |
8199 | ||
3ec482d1 | 8200 | ret = -EPERM; |
ce59fc69 | 8201 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8202 | goto err; |
8203 | ||
534ca6d6 JA |
8204 | sqd = io_get_sq_data(p); |
8205 | if (IS_ERR(sqd)) { | |
8206 | ret = PTR_ERR(sqd); | |
8207 | goto err; | |
8208 | } | |
69fb2131 | 8209 | |
534ca6d6 | 8210 | ctx->sq_data = sqd; |
69fb2131 JA |
8211 | io_sq_thread_park(sqd); |
8212 | mutex_lock(&sqd->ctx_lock); | |
8213 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8214 | mutex_unlock(&sqd->ctx_lock); | |
8215 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8216 | |
917257da JA |
8217 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8218 | if (!ctx->sq_thread_idle) | |
8219 | ctx->sq_thread_idle = HZ; | |
8220 | ||
aa06165d JA |
8221 | if (sqd->thread) |
8222 | goto done; | |
8223 | ||
6c271ce2 | 8224 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8225 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8226 | |
917257da | 8227 | ret = -EINVAL; |
44a9bd18 JA |
8228 | if (cpu >= nr_cpu_ids) |
8229 | goto err; | |
7889f44d | 8230 | if (!cpu_online(cpu)) |
917257da JA |
8231 | goto err; |
8232 | ||
69fb2131 | 8233 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8234 | cpu, "io_uring-sq"); |
6c271ce2 | 8235 | } else { |
69fb2131 | 8236 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8237 | "io_uring-sq"); |
8238 | } | |
534ca6d6 JA |
8239 | if (IS_ERR(sqd->thread)) { |
8240 | ret = PTR_ERR(sqd->thread); | |
8241 | sqd->thread = NULL; | |
6c271ce2 JA |
8242 | goto err; |
8243 | } | |
534ca6d6 | 8244 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8245 | if (ret) |
8246 | goto err; | |
6c271ce2 JA |
8247 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8248 | /* Can't have SQ_AFF without SQPOLL */ | |
8249 | ret = -EINVAL; | |
8250 | goto err; | |
8251 | } | |
8252 | ||
aa06165d | 8253 | done: |
24369c2e PB |
8254 | ret = io_init_wq_offload(ctx, p); |
8255 | if (ret) | |
2b188cc1 | 8256 | goto err; |
2b188cc1 JA |
8257 | |
8258 | return 0; | |
8259 | err: | |
54a91f3b | 8260 | io_finish_async(ctx); |
2b188cc1 JA |
8261 | return ret; |
8262 | } | |
8263 | ||
7e84e1c7 SG |
8264 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8265 | { | |
534ca6d6 JA |
8266 | struct io_sq_data *sqd = ctx->sq_data; |
8267 | ||
8268 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8269 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8270 | } |
8271 | ||
a087e2b5 BM |
8272 | static inline void __io_unaccount_mem(struct user_struct *user, |
8273 | unsigned long nr_pages) | |
2b188cc1 JA |
8274 | { |
8275 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8276 | } | |
8277 | ||
a087e2b5 BM |
8278 | static inline int __io_account_mem(struct user_struct *user, |
8279 | unsigned long nr_pages) | |
2b188cc1 JA |
8280 | { |
8281 | unsigned long page_limit, cur_pages, new_pages; | |
8282 | ||
8283 | /* Don't allow more pages than we can safely lock */ | |
8284 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8285 | ||
8286 | do { | |
8287 | cur_pages = atomic_long_read(&user->locked_vm); | |
8288 | new_pages = cur_pages + nr_pages; | |
8289 | if (new_pages > page_limit) | |
8290 | return -ENOMEM; | |
8291 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8292 | new_pages) != cur_pages); | |
8293 | ||
8294 | return 0; | |
8295 | } | |
8296 | ||
26bfa89e | 8297 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8298 | { |
aad5d8da | 8299 | if (ctx->limit_mem) |
a087e2b5 | 8300 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8301 | |
26bfa89e JA |
8302 | if (ctx->mm_account) |
8303 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8304 | } |
8305 | ||
26bfa89e | 8306 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8307 | { |
30975825 BM |
8308 | int ret; |
8309 | ||
8310 | if (ctx->limit_mem) { | |
8311 | ret = __io_account_mem(ctx->user, nr_pages); | |
8312 | if (ret) | |
8313 | return ret; | |
8314 | } | |
8315 | ||
26bfa89e JA |
8316 | if (ctx->mm_account) |
8317 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8318 | |
8319 | return 0; | |
8320 | } | |
8321 | ||
2b188cc1 JA |
8322 | static void io_mem_free(void *ptr) |
8323 | { | |
52e04ef4 MR |
8324 | struct page *page; |
8325 | ||
8326 | if (!ptr) | |
8327 | return; | |
2b188cc1 | 8328 | |
52e04ef4 | 8329 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8330 | if (put_page_testzero(page)) |
8331 | free_compound_page(page); | |
8332 | } | |
8333 | ||
8334 | static void *io_mem_alloc(size_t size) | |
8335 | { | |
8336 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8337 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8338 | |
8339 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8340 | } | |
8341 | ||
75b28aff HV |
8342 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8343 | size_t *sq_offset) | |
8344 | { | |
8345 | struct io_rings *rings; | |
8346 | size_t off, sq_array_size; | |
8347 | ||
8348 | off = struct_size(rings, cqes, cq_entries); | |
8349 | if (off == SIZE_MAX) | |
8350 | return SIZE_MAX; | |
8351 | ||
8352 | #ifdef CONFIG_SMP | |
8353 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8354 | if (off == 0) | |
8355 | return SIZE_MAX; | |
8356 | #endif | |
8357 | ||
b36200f5 DV |
8358 | if (sq_offset) |
8359 | *sq_offset = off; | |
8360 | ||
75b28aff HV |
8361 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8362 | if (sq_array_size == SIZE_MAX) | |
8363 | return SIZE_MAX; | |
8364 | ||
8365 | if (check_add_overflow(off, sq_array_size, &off)) | |
8366 | return SIZE_MAX; | |
8367 | ||
75b28aff HV |
8368 | return off; |
8369 | } | |
8370 | ||
0a96bbe4 | 8371 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8372 | { |
8373 | int i, j; | |
8374 | ||
8375 | if (!ctx->user_bufs) | |
8376 | return -ENXIO; | |
8377 | ||
8378 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8379 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8380 | ||
8381 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8382 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8383 | |
de293938 | 8384 | if (imu->acct_pages) |
26bfa89e | 8385 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8386 | kvfree(imu->bvec); |
edafccee JA |
8387 | imu->nr_bvecs = 0; |
8388 | } | |
8389 | ||
8390 | kfree(ctx->user_bufs); | |
8391 | ctx->user_bufs = NULL; | |
8392 | ctx->nr_user_bufs = 0; | |
8393 | return 0; | |
8394 | } | |
8395 | ||
8396 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8397 | void __user *arg, unsigned index) | |
8398 | { | |
8399 | struct iovec __user *src; | |
8400 | ||
8401 | #ifdef CONFIG_COMPAT | |
8402 | if (ctx->compat) { | |
8403 | struct compat_iovec __user *ciovs; | |
8404 | struct compat_iovec ciov; | |
8405 | ||
8406 | ciovs = (struct compat_iovec __user *) arg; | |
8407 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8408 | return -EFAULT; | |
8409 | ||
d55e5f5b | 8410 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8411 | dst->iov_len = ciov.iov_len; |
8412 | return 0; | |
8413 | } | |
8414 | #endif | |
8415 | src = (struct iovec __user *) arg; | |
8416 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8417 | return -EFAULT; | |
8418 | return 0; | |
8419 | } | |
8420 | ||
de293938 JA |
8421 | /* |
8422 | * Not super efficient, but this is just a registration time. And we do cache | |
8423 | * the last compound head, so generally we'll only do a full search if we don't | |
8424 | * match that one. | |
8425 | * | |
8426 | * We check if the given compound head page has already been accounted, to | |
8427 | * avoid double accounting it. This allows us to account the full size of the | |
8428 | * page, not just the constituent pages of a huge page. | |
8429 | */ | |
8430 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8431 | int nr_pages, struct page *hpage) | |
8432 | { | |
8433 | int i, j; | |
8434 | ||
8435 | /* check current page array */ | |
8436 | for (i = 0; i < nr_pages; i++) { | |
8437 | if (!PageCompound(pages[i])) | |
8438 | continue; | |
8439 | if (compound_head(pages[i]) == hpage) | |
8440 | return true; | |
8441 | } | |
8442 | ||
8443 | /* check previously registered pages */ | |
8444 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8445 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8446 | ||
8447 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8448 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8449 | continue; | |
8450 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8451 | return true; | |
8452 | } | |
8453 | } | |
8454 | ||
8455 | return false; | |
8456 | } | |
8457 | ||
8458 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8459 | int nr_pages, struct io_mapped_ubuf *imu, | |
8460 | struct page **last_hpage) | |
8461 | { | |
8462 | int i, ret; | |
8463 | ||
8464 | for (i = 0; i < nr_pages; i++) { | |
8465 | if (!PageCompound(pages[i])) { | |
8466 | imu->acct_pages++; | |
8467 | } else { | |
8468 | struct page *hpage; | |
8469 | ||
8470 | hpage = compound_head(pages[i]); | |
8471 | if (hpage == *last_hpage) | |
8472 | continue; | |
8473 | *last_hpage = hpage; | |
8474 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8475 | continue; | |
8476 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8477 | } | |
8478 | } | |
8479 | ||
8480 | if (!imu->acct_pages) | |
8481 | return 0; | |
8482 | ||
26bfa89e | 8483 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8484 | if (ret) |
8485 | imu->acct_pages = 0; | |
8486 | return ret; | |
8487 | } | |
8488 | ||
0a96bbe4 BM |
8489 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8490 | struct io_mapped_ubuf *imu, | |
8491 | struct page **last_hpage) | |
edafccee JA |
8492 | { |
8493 | struct vm_area_struct **vmas = NULL; | |
8494 | struct page **pages = NULL; | |
0a96bbe4 BM |
8495 | unsigned long off, start, end, ubuf; |
8496 | size_t size; | |
8497 | int ret, pret, nr_pages, i; | |
8498 | ||
8499 | ubuf = (unsigned long) iov->iov_base; | |
8500 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8501 | start = ubuf >> PAGE_SHIFT; | |
8502 | nr_pages = end - start; | |
8503 | ||
8504 | ret = -ENOMEM; | |
8505 | ||
8506 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8507 | if (!pages) | |
8508 | goto done; | |
8509 | ||
8510 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8511 | GFP_KERNEL); | |
8512 | if (!vmas) | |
8513 | goto done; | |
edafccee | 8514 | |
0a96bbe4 BM |
8515 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8516 | GFP_KERNEL); | |
8517 | if (!imu->bvec) | |
8518 | goto done; | |
8519 | ||
8520 | ret = 0; | |
8521 | mmap_read_lock(current->mm); | |
8522 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8523 | pages, vmas); | |
8524 | if (pret == nr_pages) { | |
8525 | /* don't support file backed memory */ | |
8526 | for (i = 0; i < nr_pages; i++) { | |
8527 | struct vm_area_struct *vma = vmas[i]; | |
8528 | ||
8529 | if (vma->vm_file && | |
8530 | !is_file_hugepages(vma->vm_file)) { | |
8531 | ret = -EOPNOTSUPP; | |
8532 | break; | |
8533 | } | |
8534 | } | |
8535 | } else { | |
8536 | ret = pret < 0 ? pret : -EFAULT; | |
8537 | } | |
8538 | mmap_read_unlock(current->mm); | |
8539 | if (ret) { | |
8540 | /* | |
8541 | * if we did partial map, or found file backed vmas, | |
8542 | * release any pages we did get | |
8543 | */ | |
8544 | if (pret > 0) | |
8545 | unpin_user_pages(pages, pret); | |
8546 | kvfree(imu->bvec); | |
8547 | goto done; | |
8548 | } | |
8549 | ||
8550 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8551 | if (ret) { | |
8552 | unpin_user_pages(pages, pret); | |
8553 | kvfree(imu->bvec); | |
8554 | goto done; | |
8555 | } | |
8556 | ||
8557 | off = ubuf & ~PAGE_MASK; | |
8558 | size = iov->iov_len; | |
8559 | for (i = 0; i < nr_pages; i++) { | |
8560 | size_t vec_len; | |
8561 | ||
8562 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8563 | imu->bvec[i].bv_page = pages[i]; | |
8564 | imu->bvec[i].bv_len = vec_len; | |
8565 | imu->bvec[i].bv_offset = off; | |
8566 | off = 0; | |
8567 | size -= vec_len; | |
8568 | } | |
8569 | /* store original address for later verification */ | |
8570 | imu->ubuf = ubuf; | |
8571 | imu->len = iov->iov_len; | |
8572 | imu->nr_bvecs = nr_pages; | |
8573 | ret = 0; | |
8574 | done: | |
8575 | kvfree(pages); | |
8576 | kvfree(vmas); | |
8577 | return ret; | |
8578 | } | |
8579 | ||
2b358604 | 8580 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8581 | { |
edafccee JA |
8582 | if (ctx->user_bufs) |
8583 | return -EBUSY; | |
8584 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8585 | return -EINVAL; | |
8586 | ||
8587 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8588 | GFP_KERNEL); | |
8589 | if (!ctx->user_bufs) | |
8590 | return -ENOMEM; | |
8591 | ||
2b358604 BM |
8592 | return 0; |
8593 | } | |
edafccee | 8594 | |
2b358604 BM |
8595 | static int io_buffer_validate(struct iovec *iov) |
8596 | { | |
8597 | /* | |
8598 | * Don't impose further limits on the size and buffer | |
8599 | * constraints here, we'll -EINVAL later when IO is | |
8600 | * submitted if they are wrong. | |
8601 | */ | |
8602 | if (!iov->iov_base || !iov->iov_len) | |
8603 | return -EFAULT; | |
edafccee | 8604 | |
2b358604 BM |
8605 | /* arbitrary limit, but we need something */ |
8606 | if (iov->iov_len > SZ_1G) | |
8607 | return -EFAULT; | |
edafccee | 8608 | |
2b358604 BM |
8609 | return 0; |
8610 | } | |
edafccee | 8611 | |
2b358604 BM |
8612 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8613 | unsigned int nr_args) | |
8614 | { | |
8615 | int i, ret; | |
8616 | struct iovec iov; | |
8617 | struct page *last_hpage = NULL; | |
edafccee | 8618 | |
2b358604 BM |
8619 | ret = io_buffers_map_alloc(ctx, nr_args); |
8620 | if (ret) | |
8621 | return ret; | |
edafccee | 8622 | |
edafccee JA |
8623 | for (i = 0; i < nr_args; i++) { |
8624 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8625 | |
edafccee JA |
8626 | ret = io_copy_iov(ctx, &iov, arg, i); |
8627 | if (ret) | |
0a96bbe4 | 8628 | break; |
de293938 | 8629 | |
2b358604 BM |
8630 | ret = io_buffer_validate(&iov); |
8631 | if (ret) | |
0a96bbe4 | 8632 | break; |
edafccee | 8633 | |
0a96bbe4 BM |
8634 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8635 | if (ret) | |
8636 | break; | |
edafccee JA |
8637 | |
8638 | ctx->nr_user_bufs++; | |
8639 | } | |
0a96bbe4 BM |
8640 | |
8641 | if (ret) | |
8642 | io_sqe_buffers_unregister(ctx); | |
8643 | ||
edafccee JA |
8644 | return ret; |
8645 | } | |
8646 | ||
9b402849 JA |
8647 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8648 | { | |
8649 | __s32 __user *fds = arg; | |
8650 | int fd; | |
8651 | ||
8652 | if (ctx->cq_ev_fd) | |
8653 | return -EBUSY; | |
8654 | ||
8655 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8656 | return -EFAULT; | |
8657 | ||
8658 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8659 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8660 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8661 | ctx->cq_ev_fd = NULL; | |
8662 | return ret; | |
8663 | } | |
8664 | ||
8665 | return 0; | |
8666 | } | |
8667 | ||
8668 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8669 | { | |
8670 | if (ctx->cq_ev_fd) { | |
8671 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8672 | ctx->cq_ev_fd = NULL; | |
8673 | return 0; | |
8674 | } | |
8675 | ||
8676 | return -ENXIO; | |
8677 | } | |
8678 | ||
5a2e745d JA |
8679 | static int __io_destroy_buffers(int id, void *p, void *data) |
8680 | { | |
8681 | struct io_ring_ctx *ctx = data; | |
8682 | struct io_buffer *buf = p; | |
8683 | ||
067524e9 | 8684 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8685 | return 0; |
8686 | } | |
8687 | ||
8688 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8689 | { | |
8690 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8691 | idr_destroy(&ctx->io_buffer_idr); | |
8692 | } | |
8693 | ||
68e68ee6 | 8694 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8695 | { |
68e68ee6 | 8696 | struct io_kiocb *req, *nxt; |
1b4c351f | 8697 | |
68e68ee6 JA |
8698 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8699 | if (tsk && req->task != tsk) | |
8700 | continue; | |
1b4c351f JA |
8701 | list_del(&req->compl.list); |
8702 | kmem_cache_free(req_cachep, req); | |
8703 | } | |
8704 | } | |
8705 | ||
9a4fdbd8 | 8706 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
2b188cc1 | 8707 | { |
bf019da7 PB |
8708 | struct io_submit_state *submit_state = &ctx->submit_state; |
8709 | ||
9a4fdbd8 JA |
8710 | mutex_lock(&ctx->uring_lock); |
8711 | ||
8712 | if (submit_state->free_reqs) | |
8713 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, | |
8714 | submit_state->reqs); | |
8715 | ||
8716 | io_req_cache_free(&submit_state->comp.free_list, NULL); | |
8717 | ||
8718 | spin_lock_irq(&ctx->completion_lock); | |
8719 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); | |
8720 | spin_unlock_irq(&ctx->completion_lock); | |
8721 | ||
8722 | mutex_unlock(&ctx->uring_lock); | |
8723 | } | |
8724 | ||
2b188cc1 JA |
8725 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8726 | { | |
04fc6c80 PB |
8727 | /* |
8728 | * Some may use context even when all refs and requests have been put, | |
8729 | * and they are free to do so while still holding uring_lock, see | |
8730 | * __io_req_task_submit(). Wait for them to finish. | |
8731 | */ | |
8732 | mutex_lock(&ctx->uring_lock); | |
8733 | mutex_unlock(&ctx->uring_lock); | |
8734 | ||
6b06314c | 8735 | io_finish_async(ctx); |
0a96bbe4 | 8736 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8737 | |
8738 | if (ctx->sqo_task) { | |
8739 | put_task_struct(ctx->sqo_task); | |
8740 | ctx->sqo_task = NULL; | |
8741 | mmdrop(ctx->mm_account); | |
8742 | ctx->mm_account = NULL; | |
30975825 | 8743 | } |
def596e9 | 8744 | |
91d8f519 DZ |
8745 | #ifdef CONFIG_BLK_CGROUP |
8746 | if (ctx->sqo_blkcg_css) | |
8747 | css_put(ctx->sqo_blkcg_css); | |
8748 | #endif | |
8749 | ||
8bad28d8 | 8750 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8751 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8752 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8753 | io_eventfd_unregister(ctx); |
5a2e745d | 8754 | io_destroy_buffers(ctx); |
41726c9a | 8755 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8756 | |
2b188cc1 | 8757 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8758 | if (ctx->ring_sock) { |
8759 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8760 | sock_release(ctx->ring_sock); |
355e8d26 | 8761 | } |
2b188cc1 JA |
8762 | #endif |
8763 | ||
75b28aff | 8764 | io_mem_free(ctx->rings); |
2b188cc1 | 8765 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8766 | |
8767 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8768 | free_uid(ctx->user); |
181e448d | 8769 | put_cred(ctx->creds); |
9a4fdbd8 | 8770 | io_req_caches_free(ctx, NULL); |
78076bb6 | 8771 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8772 | kfree(ctx); |
8773 | } | |
8774 | ||
8775 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8776 | { | |
8777 | struct io_ring_ctx *ctx = file->private_data; | |
8778 | __poll_t mask = 0; | |
8779 | ||
8780 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8781 | /* |
8782 | * synchronizes with barrier from wq_has_sleeper call in | |
8783 | * io_commit_cqring | |
8784 | */ | |
2b188cc1 | 8785 | smp_rmb(); |
90554200 | 8786 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8787 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8788 | |
8789 | /* | |
8790 | * Don't flush cqring overflow list here, just do a simple check. | |
8791 | * Otherwise there could possible be ABBA deadlock: | |
8792 | * CPU0 CPU1 | |
8793 | * ---- ---- | |
8794 | * lock(&ctx->uring_lock); | |
8795 | * lock(&ep->mtx); | |
8796 | * lock(&ctx->uring_lock); | |
8797 | * lock(&ep->mtx); | |
8798 | * | |
8799 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8800 | * pushs them to do the flush. | |
8801 | */ | |
8802 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8803 | mask |= EPOLLIN | EPOLLRDNORM; |
8804 | ||
8805 | return mask; | |
8806 | } | |
8807 | ||
8808 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8809 | { | |
8810 | struct io_ring_ctx *ctx = file->private_data; | |
8811 | ||
8812 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8813 | } | |
8814 | ||
0bead8cd | 8815 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8816 | { |
1e6fa521 | 8817 | struct io_identity *iod; |
071698e1 | 8818 | |
1e6fa521 JA |
8819 | iod = idr_remove(&ctx->personality_idr, id); |
8820 | if (iod) { | |
8821 | put_cred(iod->creds); | |
8822 | if (refcount_dec_and_test(&iod->count)) | |
8823 | kfree(iod); | |
0bead8cd | 8824 | return 0; |
1e6fa521 | 8825 | } |
0bead8cd YD |
8826 | |
8827 | return -EINVAL; | |
8828 | } | |
8829 | ||
8830 | static int io_remove_personalities(int id, void *p, void *data) | |
8831 | { | |
8832 | struct io_ring_ctx *ctx = data; | |
8833 | ||
8834 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8835 | return 0; |
8836 | } | |
8837 | ||
85faa7b8 JA |
8838 | static void io_ring_exit_work(struct work_struct *work) |
8839 | { | |
b2edc0a7 PB |
8840 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8841 | exit_work); | |
85faa7b8 | 8842 | |
56952e91 JA |
8843 | /* |
8844 | * If we're doing polled IO and end up having requests being | |
8845 | * submitted async (out-of-line), then completions can come in while | |
8846 | * we're waiting for refs to drop. We need to reap these manually, | |
8847 | * as nobody else will be looking for them. | |
8848 | */ | |
b2edc0a7 | 8849 | do { |
9936c7c2 | 8850 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b2edc0a7 | 8851 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8852 | io_ring_ctx_free(ctx); |
8853 | } | |
8854 | ||
00c18640 JA |
8855 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8856 | { | |
8857 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8858 | ||
8859 | return req->ctx == data; | |
8860 | } | |
8861 | ||
2b188cc1 JA |
8862 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8863 | { | |
8864 | mutex_lock(&ctx->uring_lock); | |
8865 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8866 | |
8867 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8868 | ctx->sqo_dead = 1; | |
8869 | ||
cda286f0 PB |
8870 | /* if force is set, the ring is going away. always drop after that */ |
8871 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8872 | if (ctx->rings) |
6c503150 | 8873 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8874 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8875 | mutex_unlock(&ctx->uring_lock); |
8876 | ||
6b81928d PB |
8877 | io_kill_timeouts(ctx, NULL, NULL); |
8878 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8879 | |
8880 | if (ctx->io_wq) | |
00c18640 | 8881 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8882 | |
15dff286 | 8883 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8884 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8885 | |
85faa7b8 | 8886 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8887 | /* |
8888 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8889 | * if we're exiting a ton of rings at the same time. It just adds | |
8890 | * noise and overhead, there's no discernable change in runtime | |
8891 | * over using system_wq. | |
8892 | */ | |
8893 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8894 | } |
8895 | ||
8896 | static int io_uring_release(struct inode *inode, struct file *file) | |
8897 | { | |
8898 | struct io_ring_ctx *ctx = file->private_data; | |
8899 | ||
8900 | file->private_data = NULL; | |
8901 | io_ring_ctx_wait_and_kill(ctx); | |
8902 | return 0; | |
8903 | } | |
8904 | ||
f6edbabb PB |
8905 | struct io_task_cancel { |
8906 | struct task_struct *task; | |
8907 | struct files_struct *files; | |
8908 | }; | |
f254ac04 | 8909 | |
f6edbabb | 8910 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8911 | { |
9a472ef7 | 8912 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8913 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8914 | bool ret; |
8915 | ||
f6edbabb | 8916 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8917 | unsigned long flags; |
8918 | struct io_ring_ctx *ctx = req->ctx; | |
8919 | ||
8920 | /* protect against races with linked timeouts */ | |
8921 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8922 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8923 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8924 | } else { | |
f6edbabb | 8925 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8926 | } |
8927 | return ret; | |
b711d4ea JA |
8928 | } |
8929 | ||
b7ddce3c | 8930 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8931 | struct task_struct *task, |
b7ddce3c PB |
8932 | struct files_struct *files) |
8933 | { | |
8934 | struct io_defer_entry *de = NULL; | |
8935 | LIST_HEAD(list); | |
8936 | ||
8937 | spin_lock_irq(&ctx->completion_lock); | |
8938 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8939 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8940 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8941 | break; | |
8942 | } | |
8943 | } | |
8944 | spin_unlock_irq(&ctx->completion_lock); | |
8945 | ||
8946 | while (!list_empty(&list)) { | |
8947 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8948 | list_del_init(&de->list); | |
8949 | req_set_fail_links(de->req); | |
8950 | io_put_req(de->req); | |
8951 | io_req_complete(de->req, -ECANCELED); | |
8952 | kfree(de); | |
8953 | } | |
8954 | } | |
8955 | ||
9936c7c2 PB |
8956 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8957 | struct task_struct *task, | |
8958 | struct files_struct *files) | |
8959 | { | |
8960 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
8961 | ||
8962 | while (1) { | |
8963 | enum io_wq_cancel cret; | |
8964 | bool ret = false; | |
8965 | ||
8966 | if (ctx->io_wq) { | |
8967 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, | |
8968 | &cancel, true); | |
8969 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8970 | } | |
8971 | ||
8972 | /* SQPOLL thread does its own polling */ | |
8973 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8974 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8975 | io_iopoll_try_reap_events(ctx); | |
8976 | ret = true; | |
8977 | } | |
8978 | } | |
8979 | ||
8980 | ret |= io_poll_remove_all(ctx, task, files); | |
8981 | ret |= io_kill_timeouts(ctx, task, files); | |
8982 | ret |= io_run_task_work(); | |
8983 | io_cqring_overflow_flush(ctx, true, task, files); | |
8984 | if (!ret) | |
8985 | break; | |
8986 | cond_resched(); | |
8987 | } | |
8988 | } | |
8989 | ||
ca70f00b PB |
8990 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8991 | struct task_struct *task, | |
8992 | struct files_struct *files) | |
8993 | { | |
8994 | struct io_kiocb *req; | |
8995 | int cnt = 0; | |
8996 | ||
8997 | spin_lock_irq(&ctx->inflight_lock); | |
8998 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8999 | cnt += io_match_task(req, task, files); | |
9000 | spin_unlock_irq(&ctx->inflight_lock); | |
9001 | return cnt; | |
9002 | } | |
9003 | ||
b52fda00 | 9004 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 9005 | struct task_struct *task, |
fcb323cc JA |
9006 | struct files_struct *files) |
9007 | { | |
fcb323cc | 9008 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 9009 | DEFINE_WAIT(wait); |
ca70f00b | 9010 | int inflight; |
fcb323cc | 9011 | |
ca70f00b PB |
9012 | inflight = io_uring_count_inflight(ctx, task, files); |
9013 | if (!inflight) | |
fcb323cc | 9014 | break; |
f6edbabb | 9015 | |
9936c7c2 | 9016 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 9017 | |
34343786 PB |
9018 | if (ctx->sq_data) |
9019 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
9020 | prepare_to_wait(&task->io_uring->wait, &wait, |
9021 | TASK_UNINTERRUPTIBLE); | |
9022 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
9023 | schedule(); | |
c98de08c | 9024 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
9025 | if (ctx->sq_data) |
9026 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 9027 | } |
0f212204 JA |
9028 | } |
9029 | ||
d9d05217 PB |
9030 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
9031 | { | |
d9d05217 PB |
9032 | mutex_lock(&ctx->uring_lock); |
9033 | ctx->sqo_dead = 1; | |
9034 | mutex_unlock(&ctx->uring_lock); | |
9035 | ||
9036 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
9037 | if (ctx->rings) |
9038 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
9039 | } |
9040 | ||
0f212204 JA |
9041 | /* |
9042 | * We need to iteratively cancel requests, in case a request has dependent | |
9043 | * hard links. These persist even for failure of cancelations, hence keep | |
9044 | * looping until none are found. | |
9045 | */ | |
9046 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
9047 | struct files_struct *files) | |
9048 | { | |
9049 | struct task_struct *task = current; | |
9050 | ||
fdaf083c | 9051 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 9052 | io_disable_sqo_submit(ctx); |
534ca6d6 | 9053 | task = ctx->sq_data->thread; |
fdaf083c JA |
9054 | atomic_inc(&task->io_uring->in_idle); |
9055 | io_sq_thread_park(ctx->sq_data); | |
9056 | } | |
0f212204 | 9057 | |
df9923f9 | 9058 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 9059 | |
3a7efd1a | 9060 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 9061 | if (!files) |
9936c7c2 | 9062 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c JA |
9063 | |
9064 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
9065 | atomic_dec(&task->io_uring->in_idle); | |
fdaf083c JA |
9066 | io_sq_thread_unpark(ctx->sq_data); |
9067 | } | |
0f212204 JA |
9068 | } |
9069 | ||
9070 | /* | |
9071 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9072 | */ | |
fdaf083c | 9073 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 9074 | { |
236434c3 | 9075 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 9076 | int ret; |
236434c3 MWO |
9077 | |
9078 | if (unlikely(!tctx)) { | |
0f212204 JA |
9079 | ret = io_uring_alloc_task_context(current); |
9080 | if (unlikely(ret)) | |
9081 | return ret; | |
236434c3 | 9082 | tctx = current->io_uring; |
0f212204 | 9083 | } |
236434c3 MWO |
9084 | if (tctx->last != file) { |
9085 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 9086 | |
236434c3 | 9087 | if (!old) { |
0f212204 | 9088 | get_file(file); |
a528b04e PB |
9089 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
9090 | file, GFP_KERNEL)); | |
9091 | if (ret) { | |
9092 | fput(file); | |
9093 | return ret; | |
9094 | } | |
ecfc8492 PB |
9095 | |
9096 | /* one and only SQPOLL file note, held by sqo_task */ | |
9097 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
9098 | current != ctx->sqo_task); | |
0f212204 | 9099 | } |
236434c3 | 9100 | tctx->last = file; |
0f212204 JA |
9101 | } |
9102 | ||
fdaf083c JA |
9103 | /* |
9104 | * This is race safe in that the task itself is doing this, hence it | |
9105 | * cannot be going through the exit/cancel paths at the same time. | |
9106 | * This cannot be modified while exit/cancel is running. | |
9107 | */ | |
9108 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9109 | tctx->sqpoll = true; | |
9110 | ||
0f212204 JA |
9111 | return 0; |
9112 | } | |
9113 | ||
9114 | /* | |
9115 | * Remove this io_uring_file -> task mapping. | |
9116 | */ | |
9117 | static void io_uring_del_task_file(struct file *file) | |
9118 | { | |
9119 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9120 | |
9121 | if (tctx->last == file) | |
9122 | tctx->last = NULL; | |
5e2ed8c4 | 9123 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9124 | if (file) |
9125 | fput(file); | |
9126 | } | |
9127 | ||
de7f1d9e PB |
9128 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9129 | { | |
9130 | struct file *file; | |
9131 | unsigned long index; | |
9132 | ||
9133 | xa_for_each(&tctx->xa, index, file) | |
9134 | io_uring_del_task_file(file); | |
9135 | } | |
9136 | ||
0f212204 JA |
9137 | void __io_uring_files_cancel(struct files_struct *files) |
9138 | { | |
9139 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9140 | struct file *file; |
9141 | unsigned long index; | |
0f212204 JA |
9142 | |
9143 | /* make sure overflow events are dropped */ | |
fdaf083c | 9144 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9145 | xa_for_each(&tctx->xa, index, file) |
9146 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9147 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9148 | |
9149 | if (files) | |
9150 | io_uring_remove_task_files(tctx); | |
fdaf083c JA |
9151 | } |
9152 | ||
9153 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9154 | { | |
0e9ddb39 PB |
9155 | return percpu_counter_sum(&tctx->inflight); |
9156 | } | |
fdaf083c | 9157 | |
0e9ddb39 PB |
9158 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
9159 | { | |
9160 | struct io_uring_task *tctx; | |
9161 | s64 inflight; | |
9162 | DEFINE_WAIT(wait); | |
fdaf083c | 9163 | |
0e9ddb39 PB |
9164 | if (!ctx->sq_data) |
9165 | return; | |
9166 | tctx = ctx->sq_data->thread->io_uring; | |
9167 | io_disable_sqo_submit(ctx); | |
fdaf083c | 9168 | |
0e9ddb39 PB |
9169 | atomic_inc(&tctx->in_idle); |
9170 | do { | |
9171 | /* read completions before cancelations */ | |
9172 | inflight = tctx_inflight(tctx); | |
9173 | if (!inflight) | |
9174 | break; | |
9175 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 9176 | |
0e9ddb39 PB |
9177 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
9178 | /* | |
9179 | * If we've seen completions, retry without waiting. This | |
9180 | * avoids a race where a completion comes in before we did | |
9181 | * prepare_to_wait(). | |
9182 | */ | |
9183 | if (inflight == tctx_inflight(tctx)) | |
9184 | schedule(); | |
9185 | finish_wait(&tctx->wait, &wait); | |
9186 | } while (1); | |
9187 | atomic_dec(&tctx->in_idle); | |
0f212204 JA |
9188 | } |
9189 | ||
0f212204 JA |
9190 | /* |
9191 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9192 | * requests. | |
9193 | */ | |
9194 | void __io_uring_task_cancel(void) | |
9195 | { | |
9196 | struct io_uring_task *tctx = current->io_uring; | |
9197 | DEFINE_WAIT(wait); | |
d8a6df10 | 9198 | s64 inflight; |
0f212204 JA |
9199 | |
9200 | /* make sure overflow events are dropped */ | |
fdaf083c | 9201 | atomic_inc(&tctx->in_idle); |
0f212204 | 9202 | |
0b5cd6c3 | 9203 | /* trigger io_disable_sqo_submit() */ |
0e9ddb39 PB |
9204 | if (tctx->sqpoll) { |
9205 | struct file *file; | |
9206 | unsigned long index; | |
9207 | ||
9208 | xa_for_each(&tctx->xa, index, file) | |
9209 | io_uring_cancel_sqpoll(file->private_data); | |
9210 | } | |
0b5cd6c3 | 9211 | |
d8a6df10 | 9212 | do { |
0f212204 | 9213 | /* read completions before cancelations */ |
fdaf083c | 9214 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9215 | if (!inflight) |
9216 | break; | |
0f212204 JA |
9217 | __io_uring_files_cancel(NULL); |
9218 | ||
9219 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9220 | ||
9221 | /* | |
a1bb3cd5 PB |
9222 | * If we've seen completions, retry without waiting. This |
9223 | * avoids a race where a completion comes in before we did | |
9224 | * prepare_to_wait(). | |
0f212204 | 9225 | */ |
a1bb3cd5 PB |
9226 | if (inflight == tctx_inflight(tctx)) |
9227 | schedule(); | |
f57555ed | 9228 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9229 | } while (1); |
0f212204 | 9230 | |
fdaf083c | 9231 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9232 | |
9233 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9234 | } |
9235 | ||
fcb323cc JA |
9236 | static int io_uring_flush(struct file *file, void *data) |
9237 | { | |
6b5733eb | 9238 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9239 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9240 | |
41be53e9 | 9241 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
84965ff8 | 9242 | io_uring_cancel_task_requests(ctx, NULL); |
41be53e9 JA |
9243 | io_req_caches_free(ctx, current); |
9244 | } | |
84965ff8 | 9245 | |
6b5733eb | 9246 | if (!tctx) |
4f793dc4 PB |
9247 | return 0; |
9248 | ||
6b5733eb PB |
9249 | /* we should have cancelled and erased it before PF_EXITING */ |
9250 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9251 | xa_load(&tctx->xa, (unsigned long)file)); | |
9252 | ||
4f793dc4 PB |
9253 | /* |
9254 | * fput() is pending, will be 2 if the only other ref is our potential | |
9255 | * task file note. If the task is exiting, drop regardless of count. | |
9256 | */ | |
6b5733eb PB |
9257 | if (atomic_long_read(&file->f_count) != 2) |
9258 | return 0; | |
4f793dc4 | 9259 | |
d9d05217 PB |
9260 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9261 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9262 | WARN_ON_ONCE(ctx->sqo_task != current && |
9263 | xa_load(&tctx->xa, (unsigned long)file)); | |
9264 | /* sqo_dead check is for when this happens after cancellation */ | |
9265 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9266 | !xa_load(&tctx->xa, (unsigned long)file)); |
9267 | ||
9268 | io_disable_sqo_submit(ctx); | |
9269 | } | |
9270 | ||
9271 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9272 | io_uring_del_task_file(file); | |
fcb323cc JA |
9273 | return 0; |
9274 | } | |
9275 | ||
6c5c240e RP |
9276 | static void *io_uring_validate_mmap_request(struct file *file, |
9277 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9278 | { |
2b188cc1 | 9279 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9280 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9281 | struct page *page; |
9282 | void *ptr; | |
9283 | ||
9284 | switch (offset) { | |
9285 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9286 | case IORING_OFF_CQ_RING: |
9287 | ptr = ctx->rings; | |
2b188cc1 JA |
9288 | break; |
9289 | case IORING_OFF_SQES: | |
9290 | ptr = ctx->sq_sqes; | |
9291 | break; | |
2b188cc1 | 9292 | default: |
6c5c240e | 9293 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9294 | } |
9295 | ||
9296 | page = virt_to_head_page(ptr); | |
a50b854e | 9297 | if (sz > page_size(page)) |
6c5c240e RP |
9298 | return ERR_PTR(-EINVAL); |
9299 | ||
9300 | return ptr; | |
9301 | } | |
9302 | ||
9303 | #ifdef CONFIG_MMU | |
9304 | ||
9305 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9306 | { | |
9307 | size_t sz = vma->vm_end - vma->vm_start; | |
9308 | unsigned long pfn; | |
9309 | void *ptr; | |
9310 | ||
9311 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9312 | if (IS_ERR(ptr)) | |
9313 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9314 | |
9315 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9316 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9317 | } | |
9318 | ||
6c5c240e RP |
9319 | #else /* !CONFIG_MMU */ |
9320 | ||
9321 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9322 | { | |
9323 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9324 | } | |
9325 | ||
9326 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9327 | { | |
9328 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9329 | } | |
9330 | ||
9331 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9332 | unsigned long addr, unsigned long len, | |
9333 | unsigned long pgoff, unsigned long flags) | |
9334 | { | |
9335 | void *ptr; | |
9336 | ||
9337 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9338 | if (IS_ERR(ptr)) | |
9339 | return PTR_ERR(ptr); | |
9340 | ||
9341 | return (unsigned long) ptr; | |
9342 | } | |
9343 | ||
9344 | #endif /* !CONFIG_MMU */ | |
9345 | ||
d9d05217 | 9346 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9347 | { |
d9d05217 | 9348 | int ret = 0; |
90554200 JA |
9349 | DEFINE_WAIT(wait); |
9350 | ||
9351 | do { | |
9352 | if (!io_sqring_full(ctx)) | |
9353 | break; | |
9354 | ||
9355 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9356 | ||
d9d05217 PB |
9357 | if (unlikely(ctx->sqo_dead)) { |
9358 | ret = -EOWNERDEAD; | |
9359 | goto out; | |
9360 | } | |
9361 | ||
90554200 JA |
9362 | if (!io_sqring_full(ctx)) |
9363 | break; | |
9364 | ||
9365 | schedule(); | |
9366 | } while (!signal_pending(current)); | |
9367 | ||
9368 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9369 | out: |
9370 | return ret; | |
90554200 JA |
9371 | } |
9372 | ||
c73ebb68 HX |
9373 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9374 | struct __kernel_timespec __user **ts, | |
9375 | const sigset_t __user **sig) | |
9376 | { | |
9377 | struct io_uring_getevents_arg arg; | |
9378 | ||
9379 | /* | |
9380 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9381 | * is just a pointer to the sigset_t. | |
9382 | */ | |
9383 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9384 | *sig = (const sigset_t __user *) argp; | |
9385 | *ts = NULL; | |
9386 | return 0; | |
9387 | } | |
9388 | ||
9389 | /* | |
9390 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9391 | * timespec and sigset_t pointers if good. | |
9392 | */ | |
9393 | if (*argsz != sizeof(arg)) | |
9394 | return -EINVAL; | |
9395 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9396 | return -EFAULT; | |
9397 | *sig = u64_to_user_ptr(arg.sigmask); | |
9398 | *argsz = arg.sigmask_sz; | |
9399 | *ts = u64_to_user_ptr(arg.ts); | |
9400 | return 0; | |
9401 | } | |
9402 | ||
2b188cc1 | 9403 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9404 | u32, min_complete, u32, flags, const void __user *, argp, |
9405 | size_t, argsz) | |
2b188cc1 JA |
9406 | { |
9407 | struct io_ring_ctx *ctx; | |
9408 | long ret = -EBADF; | |
9409 | int submitted = 0; | |
9410 | struct fd f; | |
9411 | ||
4c6e277c | 9412 | io_run_task_work(); |
b41e9852 | 9413 | |
90554200 | 9414 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9415 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9416 | return -EINVAL; |
9417 | ||
9418 | f = fdget(fd); | |
9419 | if (!f.file) | |
9420 | return -EBADF; | |
9421 | ||
9422 | ret = -EOPNOTSUPP; | |
9423 | if (f.file->f_op != &io_uring_fops) | |
9424 | goto out_fput; | |
9425 | ||
9426 | ret = -ENXIO; | |
9427 | ctx = f.file->private_data; | |
9428 | if (!percpu_ref_tryget(&ctx->refs)) | |
9429 | goto out_fput; | |
9430 | ||
7e84e1c7 SG |
9431 | ret = -EBADFD; |
9432 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9433 | goto out; | |
9434 | ||
6c271ce2 JA |
9435 | /* |
9436 | * For SQ polling, the thread will do all submissions and completions. | |
9437 | * Just return the requested submit count, and wake the thread if | |
9438 | * we were asked to. | |
9439 | */ | |
b2a9eada | 9440 | ret = 0; |
6c271ce2 | 9441 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9442 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9443 | |
d9d05217 PB |
9444 | ret = -EOWNERDEAD; |
9445 | if (unlikely(ctx->sqo_dead)) | |
9446 | goto out; | |
6c271ce2 | 9447 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9448 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9449 | if (flags & IORING_ENTER_SQ_WAIT) { |
9450 | ret = io_sqpoll_wait_sq(ctx); | |
9451 | if (ret) | |
9452 | goto out; | |
9453 | } | |
6c271ce2 | 9454 | submitted = to_submit; |
b2a9eada | 9455 | } else if (to_submit) { |
fdaf083c | 9456 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9457 | if (unlikely(ret)) |
9458 | goto out; | |
2b188cc1 | 9459 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9460 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9461 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9462 | |
9463 | if (submitted != to_submit) | |
9464 | goto out; | |
2b188cc1 JA |
9465 | } |
9466 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9467 | const sigset_t __user *sig; |
9468 | struct __kernel_timespec __user *ts; | |
9469 | ||
9470 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9471 | if (unlikely(ret)) | |
9472 | goto out; | |
9473 | ||
2b188cc1 JA |
9474 | min_complete = min(min_complete, ctx->cq_entries); |
9475 | ||
32b2244a XW |
9476 | /* |
9477 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9478 | * space applications don't need to do io completion events | |
9479 | * polling again, they can rely on io_sq_thread to do polling | |
9480 | * work, which can reduce cpu usage and uring_lock contention. | |
9481 | */ | |
9482 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9483 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9484 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9485 | } else { |
c73ebb68 | 9486 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9487 | } |
2b188cc1 JA |
9488 | } |
9489 | ||
7c504e65 | 9490 | out: |
6805b32e | 9491 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9492 | out_fput: |
9493 | fdput(f); | |
9494 | return submitted ? submitted : ret; | |
9495 | } | |
9496 | ||
bebdb65e | 9497 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9498 | static int io_uring_show_cred(int id, void *p, void *data) |
9499 | { | |
6b47ab81 JA |
9500 | struct io_identity *iod = p; |
9501 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9502 | struct seq_file *m = data; |
9503 | struct user_namespace *uns = seq_user_ns(m); | |
9504 | struct group_info *gi; | |
9505 | kernel_cap_t cap; | |
9506 | unsigned __capi; | |
9507 | int g; | |
9508 | ||
9509 | seq_printf(m, "%5d\n", id); | |
9510 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9511 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9512 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9513 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9514 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9515 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9516 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9517 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9518 | seq_puts(m, "\n\tGroups:\t"); | |
9519 | gi = cred->group_info; | |
9520 | for (g = 0; g < gi->ngroups; g++) { | |
9521 | seq_put_decimal_ull(m, g ? " " : "", | |
9522 | from_kgid_munged(uns, gi->gid[g])); | |
9523 | } | |
9524 | seq_puts(m, "\n\tCapEff:\t"); | |
9525 | cap = cred->cap_effective; | |
9526 | CAP_FOR_EACH_U32(__capi) | |
9527 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9528 | seq_putc(m, '\n'); | |
9529 | return 0; | |
9530 | } | |
9531 | ||
9532 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9533 | { | |
dbbe9c64 | 9534 | struct io_sq_data *sq = NULL; |
fad8e0de | 9535 | bool has_lock; |
87ce955b JA |
9536 | int i; |
9537 | ||
fad8e0de JA |
9538 | /* |
9539 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9540 | * since fdinfo case grabs it in the opposite direction of normal use | |
9541 | * cases. If we fail to get the lock, we just don't iterate any | |
9542 | * structures that could be going away outside the io_uring mutex. | |
9543 | */ | |
9544 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9545 | ||
dbbe9c64 JQ |
9546 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9547 | sq = ctx->sq_data; | |
9548 | ||
9549 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9550 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9551 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9552 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9553 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9554 | |
87ce955b JA |
9555 | if (f) |
9556 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9557 | else | |
9558 | seq_printf(m, "%5u: <none>\n", i); | |
9559 | } | |
9560 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9561 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9562 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9563 | ||
9564 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9565 | (unsigned int) buf->len); | |
9566 | } | |
fad8e0de | 9567 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9568 | seq_printf(m, "Personalities:\n"); |
9569 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9570 | } | |
d7718a9d JA |
9571 | seq_printf(m, "PollList:\n"); |
9572 | spin_lock_irq(&ctx->completion_lock); | |
9573 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9574 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9575 | struct io_kiocb *req; | |
9576 | ||
9577 | hlist_for_each_entry(req, list, hash_node) | |
9578 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9579 | req->task->task_works != NULL); | |
9580 | } | |
9581 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9582 | if (has_lock) |
9583 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9584 | } |
9585 | ||
9586 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9587 | { | |
9588 | struct io_ring_ctx *ctx = f->private_data; | |
9589 | ||
9590 | if (percpu_ref_tryget(&ctx->refs)) { | |
9591 | __io_uring_show_fdinfo(ctx, m); | |
9592 | percpu_ref_put(&ctx->refs); | |
9593 | } | |
9594 | } | |
bebdb65e | 9595 | #endif |
87ce955b | 9596 | |
2b188cc1 JA |
9597 | static const struct file_operations io_uring_fops = { |
9598 | .release = io_uring_release, | |
fcb323cc | 9599 | .flush = io_uring_flush, |
2b188cc1 | 9600 | .mmap = io_uring_mmap, |
6c5c240e RP |
9601 | #ifndef CONFIG_MMU |
9602 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9603 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9604 | #endif | |
2b188cc1 JA |
9605 | .poll = io_uring_poll, |
9606 | .fasync = io_uring_fasync, | |
bebdb65e | 9607 | #ifdef CONFIG_PROC_FS |
87ce955b | 9608 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9609 | #endif |
2b188cc1 JA |
9610 | }; |
9611 | ||
9612 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9613 | struct io_uring_params *p) | |
9614 | { | |
75b28aff HV |
9615 | struct io_rings *rings; |
9616 | size_t size, sq_array_offset; | |
2b188cc1 | 9617 | |
bd740481 JA |
9618 | /* make sure these are sane, as we already accounted them */ |
9619 | ctx->sq_entries = p->sq_entries; | |
9620 | ctx->cq_entries = p->cq_entries; | |
9621 | ||
75b28aff HV |
9622 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9623 | if (size == SIZE_MAX) | |
9624 | return -EOVERFLOW; | |
9625 | ||
9626 | rings = io_mem_alloc(size); | |
9627 | if (!rings) | |
2b188cc1 JA |
9628 | return -ENOMEM; |
9629 | ||
75b28aff HV |
9630 | ctx->rings = rings; |
9631 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9632 | rings->sq_ring_mask = p->sq_entries - 1; | |
9633 | rings->cq_ring_mask = p->cq_entries - 1; | |
9634 | rings->sq_ring_entries = p->sq_entries; | |
9635 | rings->cq_ring_entries = p->cq_entries; | |
9636 | ctx->sq_mask = rings->sq_ring_mask; | |
9637 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9638 | |
9639 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9640 | if (size == SIZE_MAX) { |
9641 | io_mem_free(ctx->rings); | |
9642 | ctx->rings = NULL; | |
2b188cc1 | 9643 | return -EOVERFLOW; |
eb065d30 | 9644 | } |
2b188cc1 JA |
9645 | |
9646 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9647 | if (!ctx->sq_sqes) { |
9648 | io_mem_free(ctx->rings); | |
9649 | ctx->rings = NULL; | |
2b188cc1 | 9650 | return -ENOMEM; |
eb065d30 | 9651 | } |
2b188cc1 | 9652 | |
2b188cc1 JA |
9653 | return 0; |
9654 | } | |
9655 | ||
9faadcc8 PB |
9656 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9657 | { | |
9658 | int ret, fd; | |
9659 | ||
9660 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9661 | if (fd < 0) | |
9662 | return fd; | |
9663 | ||
9664 | ret = io_uring_add_task_file(ctx, file); | |
9665 | if (ret) { | |
9666 | put_unused_fd(fd); | |
9667 | return ret; | |
9668 | } | |
9669 | fd_install(fd, file); | |
9670 | return fd; | |
9671 | } | |
9672 | ||
2b188cc1 JA |
9673 | /* |
9674 | * Allocate an anonymous fd, this is what constitutes the application | |
9675 | * visible backing of an io_uring instance. The application mmaps this | |
9676 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9677 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9678 | */ | |
9faadcc8 | 9679 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9680 | { |
9681 | struct file *file; | |
9faadcc8 | 9682 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9683 | int ret; |
9684 | ||
2b188cc1 JA |
9685 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9686 | &ctx->ring_sock); | |
9687 | if (ret) | |
9faadcc8 | 9688 | return ERR_PTR(ret); |
2b188cc1 JA |
9689 | #endif |
9690 | ||
2b188cc1 JA |
9691 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9692 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9693 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9694 | if (IS_ERR(file)) { |
9695 | sock_release(ctx->ring_sock); | |
9696 | ctx->ring_sock = NULL; | |
9697 | } else { | |
9698 | ctx->ring_sock->file = file; | |
0f212204 | 9699 | } |
2b188cc1 | 9700 | #endif |
9faadcc8 | 9701 | return file; |
2b188cc1 JA |
9702 | } |
9703 | ||
7f13657d XW |
9704 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9705 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9706 | { |
9707 | struct user_struct *user = NULL; | |
9708 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9709 | struct file *file; |
2b188cc1 JA |
9710 | int ret; |
9711 | ||
8110c1a6 | 9712 | if (!entries) |
2b188cc1 | 9713 | return -EINVAL; |
8110c1a6 JA |
9714 | if (entries > IORING_MAX_ENTRIES) { |
9715 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9716 | return -EINVAL; | |
9717 | entries = IORING_MAX_ENTRIES; | |
9718 | } | |
2b188cc1 JA |
9719 | |
9720 | /* | |
9721 | * Use twice as many entries for the CQ ring. It's possible for the | |
9722 | * application to drive a higher depth than the size of the SQ ring, | |
9723 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9724 | * some flexibility in overcommitting a bit. If the application has |
9725 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9726 | * of CQ ring entries manually. | |
2b188cc1 JA |
9727 | */ |
9728 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9729 | if (p->flags & IORING_SETUP_CQSIZE) { |
9730 | /* | |
9731 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9732 | * to a power-of-two, if it isn't already. We do NOT impose | |
9733 | * any cq vs sq ring sizing. | |
9734 | */ | |
eb2667b3 | 9735 | if (!p->cq_entries) |
33a107f0 | 9736 | return -EINVAL; |
8110c1a6 JA |
9737 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9738 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9739 | return -EINVAL; | |
9740 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9741 | } | |
eb2667b3 JQ |
9742 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9743 | if (p->cq_entries < p->sq_entries) | |
9744 | return -EINVAL; | |
33a107f0 JA |
9745 | } else { |
9746 | p->cq_entries = 2 * p->sq_entries; | |
9747 | } | |
2b188cc1 JA |
9748 | |
9749 | user = get_uid(current_user()); | |
2b188cc1 JA |
9750 | |
9751 | ctx = io_ring_ctx_alloc(p); | |
9752 | if (!ctx) { | |
2b188cc1 JA |
9753 | free_uid(user); |
9754 | return -ENOMEM; | |
9755 | } | |
9756 | ctx->compat = in_compat_syscall(); | |
26bfa89e | 9757 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9758 | ctx->user = user; |
0b8c0ec7 | 9759 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9760 | #ifdef CONFIG_AUDIT |
9761 | ctx->loginuid = current->loginuid; | |
9762 | ctx->sessionid = current->sessionid; | |
9763 | #endif | |
2aede0e4 JA |
9764 | ctx->sqo_task = get_task_struct(current); |
9765 | ||
9766 | /* | |
9767 | * This is just grabbed for accounting purposes. When a process exits, | |
9768 | * the mm is exited and dropped before the files, hence we need to hang | |
9769 | * on to this mm purely for the purposes of being able to unaccount | |
9770 | * memory (locked/pinned vm). It's not used for anything else. | |
9771 | */ | |
6b7898eb | 9772 | mmgrab(current->mm); |
2aede0e4 | 9773 | ctx->mm_account = current->mm; |
6b7898eb | 9774 | |
91d8f519 DZ |
9775 | #ifdef CONFIG_BLK_CGROUP |
9776 | /* | |
9777 | * The sq thread will belong to the original cgroup it was inited in. | |
9778 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9779 | * issued bios will be associated with the closest cgroup later in the | |
9780 | * block layer. | |
9781 | */ | |
9782 | rcu_read_lock(); | |
9783 | ctx->sqo_blkcg_css = blkcg_css(); | |
9784 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9785 | rcu_read_unlock(); | |
9786 | if (!ret) { | |
9787 | /* don't init against a dying cgroup, have the user try again */ | |
9788 | ctx->sqo_blkcg_css = NULL; | |
9789 | ret = -ENODEV; | |
9790 | goto err; | |
9791 | } | |
9792 | #endif | |
2b188cc1 JA |
9793 | ret = io_allocate_scq_urings(ctx, p); |
9794 | if (ret) | |
9795 | goto err; | |
9796 | ||
7e84e1c7 | 9797 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9798 | if (ret) |
9799 | goto err; | |
9800 | ||
7e84e1c7 SG |
9801 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9802 | io_sq_offload_start(ctx); | |
9803 | ||
2b188cc1 | 9804 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9805 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9806 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9807 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9808 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9809 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9810 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9811 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9812 | |
9813 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9814 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9815 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9816 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9817 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9818 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9819 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9820 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9821 | |
7f13657d XW |
9822 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9823 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9824 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9825 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9826 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9827 | |
9828 | if (copy_to_user(params, p, sizeof(*p))) { | |
9829 | ret = -EFAULT; | |
9830 | goto err; | |
9831 | } | |
d1719f70 | 9832 | |
9faadcc8 PB |
9833 | file = io_uring_get_file(ctx); |
9834 | if (IS_ERR(file)) { | |
9835 | ret = PTR_ERR(file); | |
9836 | goto err; | |
9837 | } | |
9838 | ||
044c1ab3 JA |
9839 | /* |
9840 | * Install ring fd as the very last thing, so we don't risk someone | |
9841 | * having closed it before we finish setup | |
9842 | */ | |
9faadcc8 PB |
9843 | ret = io_uring_install_fd(ctx, file); |
9844 | if (ret < 0) { | |
06585c49 | 9845 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9846 | /* fput will clean it up */ |
9847 | fput(file); | |
9848 | return ret; | |
9849 | } | |
044c1ab3 | 9850 | |
c826bd7a | 9851 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9852 | return ret; |
9853 | err: | |
d9d05217 | 9854 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9855 | io_ring_ctx_wait_and_kill(ctx); |
9856 | return ret; | |
9857 | } | |
9858 | ||
9859 | /* | |
9860 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9861 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9862 | * params structure passed in. | |
9863 | */ | |
9864 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9865 | { | |
9866 | struct io_uring_params p; | |
2b188cc1 JA |
9867 | int i; |
9868 | ||
9869 | if (copy_from_user(&p, params, sizeof(p))) | |
9870 | return -EFAULT; | |
9871 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9872 | if (p.resv[i]) | |
9873 | return -EINVAL; | |
9874 | } | |
9875 | ||
6c271ce2 | 9876 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9877 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9878 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9879 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9880 | return -EINVAL; |
9881 | ||
7f13657d | 9882 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9883 | } |
9884 | ||
9885 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9886 | struct io_uring_params __user *, params) | |
9887 | { | |
9888 | return io_uring_setup(entries, params); | |
9889 | } | |
9890 | ||
66f4af93 JA |
9891 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9892 | { | |
9893 | struct io_uring_probe *p; | |
9894 | size_t size; | |
9895 | int i, ret; | |
9896 | ||
9897 | size = struct_size(p, ops, nr_args); | |
9898 | if (size == SIZE_MAX) | |
9899 | return -EOVERFLOW; | |
9900 | p = kzalloc(size, GFP_KERNEL); | |
9901 | if (!p) | |
9902 | return -ENOMEM; | |
9903 | ||
9904 | ret = -EFAULT; | |
9905 | if (copy_from_user(p, arg, size)) | |
9906 | goto out; | |
9907 | ret = -EINVAL; | |
9908 | if (memchr_inv(p, 0, size)) | |
9909 | goto out; | |
9910 | ||
9911 | p->last_op = IORING_OP_LAST - 1; | |
9912 | if (nr_args > IORING_OP_LAST) | |
9913 | nr_args = IORING_OP_LAST; | |
9914 | ||
9915 | for (i = 0; i < nr_args; i++) { | |
9916 | p->ops[i].op = i; | |
9917 | if (!io_op_defs[i].not_supported) | |
9918 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9919 | } | |
9920 | p->ops_len = i; | |
9921 | ||
9922 | ret = 0; | |
9923 | if (copy_to_user(arg, p, size)) | |
9924 | ret = -EFAULT; | |
9925 | out: | |
9926 | kfree(p); | |
9927 | return ret; | |
9928 | } | |
9929 | ||
071698e1 JA |
9930 | static int io_register_personality(struct io_ring_ctx *ctx) |
9931 | { | |
1e6fa521 JA |
9932 | struct io_identity *id; |
9933 | int ret; | |
071698e1 | 9934 | |
1e6fa521 JA |
9935 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9936 | if (unlikely(!id)) | |
9937 | return -ENOMEM; | |
9938 | ||
9939 | io_init_identity(id); | |
9940 | id->creds = get_current_cred(); | |
9941 | ||
9942 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9943 | if (ret < 0) { | |
9944 | put_cred(id->creds); | |
9945 | kfree(id); | |
9946 | } | |
9947 | return ret; | |
071698e1 JA |
9948 | } |
9949 | ||
21b55dbc SG |
9950 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9951 | unsigned int nr_args) | |
9952 | { | |
9953 | struct io_uring_restriction *res; | |
9954 | size_t size; | |
9955 | int i, ret; | |
9956 | ||
7e84e1c7 SG |
9957 | /* Restrictions allowed only if rings started disabled */ |
9958 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9959 | return -EBADFD; | |
9960 | ||
21b55dbc | 9961 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9962 | if (ctx->restrictions.registered) |
21b55dbc SG |
9963 | return -EBUSY; |
9964 | ||
9965 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9966 | return -EINVAL; | |
9967 | ||
9968 | size = array_size(nr_args, sizeof(*res)); | |
9969 | if (size == SIZE_MAX) | |
9970 | return -EOVERFLOW; | |
9971 | ||
9972 | res = memdup_user(arg, size); | |
9973 | if (IS_ERR(res)) | |
9974 | return PTR_ERR(res); | |
9975 | ||
9976 | ret = 0; | |
9977 | ||
9978 | for (i = 0; i < nr_args; i++) { | |
9979 | switch (res[i].opcode) { | |
9980 | case IORING_RESTRICTION_REGISTER_OP: | |
9981 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9982 | ret = -EINVAL; | |
9983 | goto out; | |
9984 | } | |
9985 | ||
9986 | __set_bit(res[i].register_op, | |
9987 | ctx->restrictions.register_op); | |
9988 | break; | |
9989 | case IORING_RESTRICTION_SQE_OP: | |
9990 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9991 | ret = -EINVAL; | |
9992 | goto out; | |
9993 | } | |
9994 | ||
9995 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9996 | break; | |
9997 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9998 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9999 | break; | |
10000 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10001 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10002 | break; | |
10003 | default: | |
10004 | ret = -EINVAL; | |
10005 | goto out; | |
10006 | } | |
10007 | } | |
10008 | ||
10009 | out: | |
10010 | /* Reset all restrictions if an error happened */ | |
10011 | if (ret != 0) | |
10012 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10013 | else | |
7e84e1c7 | 10014 | ctx->restrictions.registered = true; |
21b55dbc SG |
10015 | |
10016 | kfree(res); | |
10017 | return ret; | |
10018 | } | |
10019 | ||
7e84e1c7 SG |
10020 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10021 | { | |
10022 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10023 | return -EBADFD; | |
10024 | ||
10025 | if (ctx->restrictions.registered) | |
10026 | ctx->restricted = 1; | |
10027 | ||
10028 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
10029 | ||
10030 | io_sq_offload_start(ctx); | |
10031 | ||
10032 | return 0; | |
10033 | } | |
10034 | ||
071698e1 JA |
10035 | static bool io_register_op_must_quiesce(int op) |
10036 | { | |
10037 | switch (op) { | |
10038 | case IORING_UNREGISTER_FILES: | |
10039 | case IORING_REGISTER_FILES_UPDATE: | |
10040 | case IORING_REGISTER_PROBE: | |
10041 | case IORING_REGISTER_PERSONALITY: | |
10042 | case IORING_UNREGISTER_PERSONALITY: | |
10043 | return false; | |
10044 | default: | |
10045 | return true; | |
10046 | } | |
10047 | } | |
10048 | ||
edafccee JA |
10049 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10050 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10051 | __releases(ctx->uring_lock) |
10052 | __acquires(ctx->uring_lock) | |
edafccee JA |
10053 | { |
10054 | int ret; | |
10055 | ||
35fa71a0 JA |
10056 | /* |
10057 | * We're inside the ring mutex, if the ref is already dying, then | |
10058 | * someone else killed the ctx or is already going through | |
10059 | * io_uring_register(). | |
10060 | */ | |
10061 | if (percpu_ref_is_dying(&ctx->refs)) | |
10062 | return -ENXIO; | |
10063 | ||
071698e1 | 10064 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10065 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10066 | |
05f3fb3c JA |
10067 | /* |
10068 | * Drop uring mutex before waiting for references to exit. If | |
10069 | * another thread is currently inside io_uring_enter() it might | |
10070 | * need to grab the uring_lock to make progress. If we hold it | |
10071 | * here across the drain wait, then we can deadlock. It's safe | |
10072 | * to drop the mutex here, since no new references will come in | |
10073 | * after we've killed the percpu ref. | |
10074 | */ | |
10075 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10076 | do { |
10077 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10078 | if (!ret) | |
10079 | break; | |
ed6930c9 JA |
10080 | ret = io_run_task_work_sig(); |
10081 | if (ret < 0) | |
10082 | break; | |
af9c1a44 JA |
10083 | } while (1); |
10084 | ||
05f3fb3c | 10085 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10086 | |
88f171ab PB |
10087 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
10088 | return ret; | |
21b55dbc SG |
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); |
0f158b4c | 10180 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10181 | } |
edafccee JA |
10182 | return ret; |
10183 | } | |
10184 | ||
10185 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10186 | void __user *, arg, unsigned int, nr_args) | |
10187 | { | |
10188 | struct io_ring_ctx *ctx; | |
10189 | long ret = -EBADF; | |
10190 | struct fd f; | |
10191 | ||
10192 | f = fdget(fd); | |
10193 | if (!f.file) | |
10194 | return -EBADF; | |
10195 | ||
10196 | ret = -EOPNOTSUPP; | |
10197 | if (f.file->f_op != &io_uring_fops) | |
10198 | goto out_fput; | |
10199 | ||
10200 | ctx = f.file->private_data; | |
10201 | ||
b6c23dd5 PB |
10202 | io_run_task_work(); |
10203 | ||
edafccee JA |
10204 | mutex_lock(&ctx->uring_lock); |
10205 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10206 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10207 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10208 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10209 | out_fput: |
10210 | fdput(f); | |
10211 | return ret; | |
10212 | } | |
10213 | ||
2b188cc1 JA |
10214 | static int __init io_uring_init(void) |
10215 | { | |
d7f62e82 SM |
10216 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10217 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10218 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10219 | } while (0) | |
10220 | ||
10221 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10222 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10223 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10224 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10225 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10226 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10227 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10228 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10229 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10230 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10231 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10232 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10233 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10234 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10235 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10236 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10237 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10238 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10239 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10240 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10241 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10242 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10243 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10244 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10245 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10246 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10247 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10248 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10249 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10250 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10251 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10252 | |
d3656344 | 10253 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10254 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
10255 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10256 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10257 | return 0; |
10258 | }; | |
10259 | __initcall(io_uring_init); |