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 JA |
106 | |
107 | struct io_uring { | |
108 | u32 head ____cacheline_aligned_in_smp; | |
109 | u32 tail ____cacheline_aligned_in_smp; | |
110 | }; | |
111 | ||
1e84b97b | 112 | /* |
75b28aff HV |
113 | * This data is shared with the application through the mmap at offsets |
114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
115 | * |
116 | * The offsets to the member fields are published through struct | |
117 | * io_sqring_offsets when calling io_uring_setup. | |
118 | */ | |
75b28aff | 119 | struct io_rings { |
1e84b97b SB |
120 | /* |
121 | * Head and tail offsets into the ring; the offsets need to be | |
122 | * masked to get valid indices. | |
123 | * | |
75b28aff HV |
124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
125 | * and the application controls tail of the sq ring and the head of the | |
126 | * cq ring. | |
1e84b97b | 127 | */ |
75b28aff | 128 | struct io_uring sq, cq; |
1e84b97b | 129 | /* |
75b28aff | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
131 | * ring_entries - 1) |
132 | */ | |
75b28aff HV |
133 | u32 sq_ring_mask, cq_ring_mask; |
134 | /* Ring sizes (constant, power of 2) */ | |
135 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
136 | /* |
137 | * Number of invalid entries dropped by the kernel due to | |
138 | * invalid index stored in array | |
139 | * | |
140 | * Written by the kernel, shouldn't be modified by the | |
141 | * application (i.e. get number of "new events" by comparing to | |
142 | * cached value). | |
143 | * | |
144 | * After a new SQ head value was read by the application this | |
145 | * counter includes all submissions that were dropped reaching | |
146 | * the new SQ head (and possibly more). | |
147 | */ | |
75b28aff | 148 | u32 sq_dropped; |
1e84b97b | 149 | /* |
0d9b5b3a | 150 | * Runtime SQ flags |
1e84b97b SB |
151 | * |
152 | * Written by the kernel, shouldn't be modified by the | |
153 | * application. | |
154 | * | |
155 | * The application needs a full memory barrier before checking | |
156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
157 | */ | |
75b28aff | 158 | u32 sq_flags; |
0d9b5b3a SG |
159 | /* |
160 | * Runtime CQ flags | |
161 | * | |
162 | * Written by the application, shouldn't be modified by the | |
163 | * kernel. | |
164 | */ | |
165 | u32 cq_flags; | |
1e84b97b SB |
166 | /* |
167 | * Number of completion events lost because the queue was full; | |
168 | * this should be avoided by the application by making sure | |
0b4295b5 | 169 | * there are not more requests pending than there is space in |
1e84b97b SB |
170 | * the completion queue. |
171 | * | |
172 | * Written by the kernel, shouldn't be modified by the | |
173 | * application (i.e. get number of "new events" by comparing to | |
174 | * cached value). | |
175 | * | |
176 | * As completion events come in out of order this counter is not | |
177 | * ordered with any other data. | |
178 | */ | |
75b28aff | 179 | u32 cq_overflow; |
1e84b97b SB |
180 | /* |
181 | * Ring buffer of completion events. | |
182 | * | |
183 | * The kernel writes completion events fresh every time they are | |
184 | * produced, so the application is allowed to modify pending | |
185 | * entries. | |
186 | */ | |
75b28aff | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
188 | }; |
189 | ||
45d189c6 PB |
190 | enum io_uring_cmd_flags { |
191 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 192 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
193 | }; |
194 | ||
edafccee JA |
195 | struct io_mapped_ubuf { |
196 | u64 ubuf; | |
197 | size_t len; | |
198 | struct bio_vec *bvec; | |
199 | unsigned int nr_bvecs; | |
de293938 | 200 | unsigned long acct_pages; |
edafccee JA |
201 | }; |
202 | ||
50238531 BM |
203 | struct io_ring_ctx; |
204 | ||
269bbe5f BM |
205 | struct io_rsrc_put { |
206 | struct list_head list; | |
50238531 BM |
207 | union { |
208 | void *rsrc; | |
209 | struct file *file; | |
210 | }; | |
269bbe5f BM |
211 | }; |
212 | ||
213 | struct fixed_rsrc_table { | |
65e19f54 | 214 | struct file **files; |
31b51510 JA |
215 | }; |
216 | ||
269bbe5f | 217 | struct fixed_rsrc_ref_node { |
05589553 XW |
218 | struct percpu_ref refs; |
219 | struct list_head node; | |
269bbe5f BM |
220 | struct list_head rsrc_list; |
221 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
222 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
223 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 224 | struct llist_node llist; |
e297822b | 225 | bool done; |
05589553 XW |
226 | }; |
227 | ||
269bbe5f BM |
228 | struct fixed_rsrc_data { |
229 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
230 | struct io_ring_ctx *ctx; |
231 | ||
269bbe5f | 232 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 233 | struct percpu_ref refs; |
05f3fb3c JA |
234 | struct completion done; |
235 | }; | |
236 | ||
5a2e745d JA |
237 | struct io_buffer { |
238 | struct list_head list; | |
239 | __u64 addr; | |
240 | __s32 len; | |
241 | __u16 bid; | |
242 | }; | |
243 | ||
21b55dbc SG |
244 | struct io_restriction { |
245 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
246 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
247 | u8 sqe_flags_allowed; | |
248 | u8 sqe_flags_required; | |
7e84e1c7 | 249 | bool registered; |
21b55dbc SG |
250 | }; |
251 | ||
534ca6d6 JA |
252 | struct io_sq_data { |
253 | refcount_t refs; | |
69fb2131 JA |
254 | struct mutex lock; |
255 | ||
256 | /* ctx's that are using this sqd */ | |
257 | struct list_head ctx_list; | |
258 | struct list_head ctx_new_list; | |
259 | struct mutex ctx_lock; | |
260 | ||
534ca6d6 JA |
261 | struct task_struct *thread; |
262 | struct wait_queue_head wait; | |
08369246 XW |
263 | |
264 | unsigned sq_thread_idle; | |
534ca6d6 JA |
265 | }; |
266 | ||
258b29a9 | 267 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 268 | #define IO_COMPL_BATCH 32 |
258b29a9 PB |
269 | |
270 | struct io_comp_state { | |
271 | unsigned int nr; | |
6dd0be1e | 272 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
258b29a9 PB |
273 | }; |
274 | ||
275 | struct io_submit_state { | |
276 | struct blk_plug plug; | |
277 | ||
278 | /* | |
279 | * io_kiocb alloc cache | |
280 | */ | |
281 | void *reqs[IO_IOPOLL_BATCH]; | |
282 | unsigned int free_reqs; | |
283 | ||
284 | bool plug_started; | |
285 | ||
286 | /* | |
287 | * Batch completion logic | |
288 | */ | |
289 | struct io_comp_state comp; | |
290 | ||
291 | /* | |
292 | * File reference cache | |
293 | */ | |
294 | struct file *file; | |
295 | unsigned int fd; | |
296 | unsigned int file_refs; | |
297 | unsigned int ios_left; | |
298 | }; | |
299 | ||
2b188cc1 JA |
300 | struct io_ring_ctx { |
301 | struct { | |
302 | struct percpu_ref refs; | |
303 | } ____cacheline_aligned_in_smp; | |
304 | ||
305 | struct { | |
306 | unsigned int flags; | |
e1d85334 | 307 | unsigned int compat: 1; |
aad5d8da | 308 | unsigned int limit_mem: 1; |
e1d85334 RD |
309 | unsigned int cq_overflow_flushed: 1; |
310 | unsigned int drain_next: 1; | |
311 | unsigned int eventfd_async: 1; | |
21b55dbc | 312 | unsigned int restricted: 1; |
d9d05217 | 313 | unsigned int sqo_dead: 1; |
2b188cc1 | 314 | |
75b28aff HV |
315 | /* |
316 | * Ring buffer of indices into array of io_uring_sqe, which is | |
317 | * mmapped by the application using the IORING_OFF_SQES offset. | |
318 | * | |
319 | * This indirection could e.g. be used to assign fixed | |
320 | * io_uring_sqe entries to operations and only submit them to | |
321 | * the queue when needed. | |
322 | * | |
323 | * The kernel modifies neither the indices array nor the entries | |
324 | * array. | |
325 | */ | |
326 | u32 *sq_array; | |
2b188cc1 JA |
327 | unsigned cached_sq_head; |
328 | unsigned sq_entries; | |
329 | unsigned sq_mask; | |
6c271ce2 | 330 | unsigned sq_thread_idle; |
498ccd9e | 331 | unsigned cached_sq_dropped; |
2c3bac6d | 332 | unsigned cached_cq_overflow; |
ad3eb2c8 | 333 | unsigned long sq_check_overflow; |
de0617e4 JA |
334 | |
335 | struct list_head defer_list; | |
5262f567 | 336 | struct list_head timeout_list; |
1d7bb1d5 | 337 | struct list_head cq_overflow_list; |
fcb323cc | 338 | |
ad3eb2c8 | 339 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
340 | } ____cacheline_aligned_in_smp; |
341 | ||
206aefde JA |
342 | struct io_rings *rings; |
343 | ||
2b188cc1 | 344 | /* IO offload */ |
561fb04a | 345 | struct io_wq *io_wq; |
2aede0e4 JA |
346 | |
347 | /* | |
348 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
349 | * have access to the ->files | |
350 | */ | |
351 | struct task_struct *sqo_task; | |
352 | ||
353 | /* Only used for accounting purposes */ | |
354 | struct mm_struct *mm_account; | |
355 | ||
91d8f519 DZ |
356 | #ifdef CONFIG_BLK_CGROUP |
357 | struct cgroup_subsys_state *sqo_blkcg_css; | |
358 | #endif | |
359 | ||
534ca6d6 JA |
360 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
361 | ||
90554200 | 362 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 363 | struct list_head sqd_list; |
75b28aff | 364 | |
6b06314c JA |
365 | /* |
366 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
367 | * readers must ensure that ->refs is alive as long as the file* is | |
368 | * used. Only updated through io_uring_register(2). | |
369 | */ | |
269bbe5f | 370 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
371 | unsigned nr_user_files; |
372 | ||
edafccee JA |
373 | /* if used, fixed mapped user buffers */ |
374 | unsigned nr_user_bufs; | |
375 | struct io_mapped_ubuf *user_bufs; | |
376 | ||
2b188cc1 JA |
377 | struct user_struct *user; |
378 | ||
0b8c0ec7 | 379 | const struct cred *creds; |
181e448d | 380 | |
4ea33a97 JA |
381 | #ifdef CONFIG_AUDIT |
382 | kuid_t loginuid; | |
383 | unsigned int sessionid; | |
384 | #endif | |
385 | ||
0f158b4c JA |
386 | struct completion ref_comp; |
387 | struct completion sq_thread_comp; | |
206aefde JA |
388 | |
389 | #if defined(CONFIG_UNIX) | |
390 | struct socket *ring_sock; | |
391 | #endif | |
392 | ||
5a2e745d JA |
393 | struct idr io_buffer_idr; |
394 | ||
071698e1 JA |
395 | struct idr personality_idr; |
396 | ||
206aefde JA |
397 | struct { |
398 | unsigned cached_cq_tail; | |
399 | unsigned cq_entries; | |
400 | unsigned cq_mask; | |
401 | atomic_t cq_timeouts; | |
f010505b | 402 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 403 | unsigned long cq_check_overflow; |
206aefde JA |
404 | struct wait_queue_head cq_wait; |
405 | struct fasync_struct *cq_fasync; | |
406 | struct eventfd_ctx *cq_ev_fd; | |
407 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
408 | |
409 | struct { | |
410 | struct mutex uring_lock; | |
411 | wait_queue_head_t wait; | |
412 | } ____cacheline_aligned_in_smp; | |
413 | ||
414 | struct { | |
415 | spinlock_t completion_lock; | |
e94f141b | 416 | |
def596e9 | 417 | /* |
540e32a0 | 418 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
419 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
420 | * For SQPOLL, only the single threaded io_sq_thread() will | |
421 | * manipulate the list, hence no extra locking is needed there. | |
422 | */ | |
540e32a0 | 423 | struct list_head iopoll_list; |
78076bb6 JA |
424 | struct hlist_head *cancel_hash; |
425 | unsigned cancel_hash_bits; | |
e94f141b | 426 | bool poll_multi_file; |
31b51510 | 427 | |
fcb323cc JA |
428 | spinlock_t inflight_lock; |
429 | struct list_head inflight_list; | |
2b188cc1 | 430 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 431 | |
269bbe5f BM |
432 | struct delayed_work rsrc_put_work; |
433 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
434 | struct list_head rsrc_ref_list; |
435 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 436 | |
85faa7b8 | 437 | struct work_struct exit_work; |
21b55dbc | 438 | struct io_restriction restrictions; |
258b29a9 | 439 | struct io_submit_state submit_state; |
2b188cc1 JA |
440 | }; |
441 | ||
09bb8394 JA |
442 | /* |
443 | * First field must be the file pointer in all the | |
444 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
445 | */ | |
221c5eb2 JA |
446 | struct io_poll_iocb { |
447 | struct file *file; | |
018043be | 448 | struct wait_queue_head *head; |
221c5eb2 | 449 | __poll_t events; |
8c838788 | 450 | bool done; |
221c5eb2 | 451 | bool canceled; |
392edb45 | 452 | struct wait_queue_entry wait; |
221c5eb2 JA |
453 | }; |
454 | ||
018043be PB |
455 | struct io_poll_remove { |
456 | struct file *file; | |
457 | u64 addr; | |
458 | }; | |
459 | ||
b5dba59e JA |
460 | struct io_close { |
461 | struct file *file; | |
b5dba59e JA |
462 | int fd; |
463 | }; | |
464 | ||
ad8a48ac JA |
465 | struct io_timeout_data { |
466 | struct io_kiocb *req; | |
467 | struct hrtimer timer; | |
468 | struct timespec64 ts; | |
469 | enum hrtimer_mode mode; | |
470 | }; | |
471 | ||
8ed8d3c3 JA |
472 | struct io_accept { |
473 | struct file *file; | |
474 | struct sockaddr __user *addr; | |
475 | int __user *addr_len; | |
476 | int flags; | |
09952e3e | 477 | unsigned long nofile; |
8ed8d3c3 JA |
478 | }; |
479 | ||
480 | struct io_sync { | |
481 | struct file *file; | |
482 | loff_t len; | |
483 | loff_t off; | |
484 | int flags; | |
d63d1b5e | 485 | int mode; |
8ed8d3c3 JA |
486 | }; |
487 | ||
fbf23849 JA |
488 | struct io_cancel { |
489 | struct file *file; | |
490 | u64 addr; | |
491 | }; | |
492 | ||
b29472ee JA |
493 | struct io_timeout { |
494 | struct file *file; | |
bfe68a22 PB |
495 | u32 off; |
496 | u32 target_seq; | |
135fcde8 | 497 | struct list_head list; |
90cd7e42 PB |
498 | /* head of the link, used by linked timeouts only */ |
499 | struct io_kiocb *head; | |
b29472ee JA |
500 | }; |
501 | ||
0bdf7a2d PB |
502 | struct io_timeout_rem { |
503 | struct file *file; | |
504 | u64 addr; | |
9c8e11b3 PB |
505 | |
506 | /* timeout update */ | |
507 | struct timespec64 ts; | |
508 | u32 flags; | |
0bdf7a2d PB |
509 | }; |
510 | ||
9adbd45d JA |
511 | struct io_rw { |
512 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
513 | struct kiocb kiocb; | |
514 | u64 addr; | |
515 | u64 len; | |
516 | }; | |
517 | ||
3fbb51c1 JA |
518 | struct io_connect { |
519 | struct file *file; | |
520 | struct sockaddr __user *addr; | |
521 | int addr_len; | |
522 | }; | |
523 | ||
e47293fd JA |
524 | struct io_sr_msg { |
525 | struct file *file; | |
fddaface | 526 | union { |
270a5940 | 527 | struct user_msghdr __user *umsg; |
fddaface JA |
528 | void __user *buf; |
529 | }; | |
e47293fd | 530 | int msg_flags; |
bcda7baa | 531 | int bgid; |
fddaface | 532 | size_t len; |
bcda7baa | 533 | struct io_buffer *kbuf; |
e47293fd JA |
534 | }; |
535 | ||
15b71abe JA |
536 | struct io_open { |
537 | struct file *file; | |
538 | int dfd; | |
15b71abe | 539 | struct filename *filename; |
c12cedf2 | 540 | struct open_how how; |
4022e7af | 541 | unsigned long nofile; |
15b71abe JA |
542 | }; |
543 | ||
269bbe5f | 544 | struct io_rsrc_update { |
05f3fb3c JA |
545 | struct file *file; |
546 | u64 arg; | |
547 | u32 nr_args; | |
548 | u32 offset; | |
549 | }; | |
550 | ||
4840e418 JA |
551 | struct io_fadvise { |
552 | struct file *file; | |
553 | u64 offset; | |
554 | u32 len; | |
555 | u32 advice; | |
556 | }; | |
557 | ||
c1ca757b JA |
558 | struct io_madvise { |
559 | struct file *file; | |
560 | u64 addr; | |
561 | u32 len; | |
562 | u32 advice; | |
563 | }; | |
564 | ||
3e4827b0 JA |
565 | struct io_epoll { |
566 | struct file *file; | |
567 | int epfd; | |
568 | int op; | |
569 | int fd; | |
570 | struct epoll_event event; | |
e47293fd JA |
571 | }; |
572 | ||
7d67af2c PB |
573 | struct io_splice { |
574 | struct file *file_out; | |
575 | struct file *file_in; | |
576 | loff_t off_out; | |
577 | loff_t off_in; | |
578 | u64 len; | |
579 | unsigned int flags; | |
580 | }; | |
581 | ||
ddf0322d JA |
582 | struct io_provide_buf { |
583 | struct file *file; | |
584 | __u64 addr; | |
585 | __s32 len; | |
586 | __u32 bgid; | |
587 | __u16 nbufs; | |
588 | __u16 bid; | |
589 | }; | |
590 | ||
1d9e1288 BM |
591 | struct io_statx { |
592 | struct file *file; | |
593 | int dfd; | |
594 | unsigned int mask; | |
595 | unsigned int flags; | |
e62753e4 | 596 | const char __user *filename; |
1d9e1288 BM |
597 | struct statx __user *buffer; |
598 | }; | |
599 | ||
36f4fa68 JA |
600 | struct io_shutdown { |
601 | struct file *file; | |
602 | int how; | |
603 | }; | |
604 | ||
80a261fd JA |
605 | struct io_rename { |
606 | struct file *file; | |
607 | int old_dfd; | |
608 | int new_dfd; | |
609 | struct filename *oldpath; | |
610 | struct filename *newpath; | |
611 | int flags; | |
612 | }; | |
613 | ||
14a1143b JA |
614 | struct io_unlink { |
615 | struct file *file; | |
616 | int dfd; | |
617 | int flags; | |
618 | struct filename *filename; | |
619 | }; | |
620 | ||
3ca405eb PB |
621 | struct io_completion { |
622 | struct file *file; | |
623 | struct list_head list; | |
0f7e466b | 624 | int cflags; |
3ca405eb PB |
625 | }; |
626 | ||
f499a021 JA |
627 | struct io_async_connect { |
628 | struct sockaddr_storage address; | |
629 | }; | |
630 | ||
03b1230c JA |
631 | struct io_async_msghdr { |
632 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
633 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
634 | struct iovec *free_iov; | |
03b1230c JA |
635 | struct sockaddr __user *uaddr; |
636 | struct msghdr msg; | |
b537916c | 637 | struct sockaddr_storage addr; |
03b1230c JA |
638 | }; |
639 | ||
f67676d1 JA |
640 | struct io_async_rw { |
641 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
642 | const struct iovec *free_iovec; |
643 | struct iov_iter iter; | |
227c0c96 | 644 | size_t bytes_done; |
bcf5a063 | 645 | struct wait_page_queue wpq; |
f67676d1 JA |
646 | }; |
647 | ||
6b47ee6e PB |
648 | enum { |
649 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
650 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
651 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
652 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
653 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 654 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 655 | |
6b47ee6e PB |
656 | REQ_F_FAIL_LINK_BIT, |
657 | REQ_F_INFLIGHT_BIT, | |
658 | REQ_F_CUR_POS_BIT, | |
659 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 660 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 661 | REQ_F_ISREG_BIT, |
99bc4c38 | 662 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 663 | REQ_F_POLLED_BIT, |
bcda7baa | 664 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 665 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 666 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 667 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 668 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
669 | |
670 | /* not a real bit, just to check we're not overflowing the space */ | |
671 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
672 | }; |
673 | ||
674 | enum { | |
675 | /* ctx owns file */ | |
676 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
677 | /* drain existing IO first */ | |
678 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
679 | /* linked sqes */ | |
680 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
681 | /* doesn't sever on completion < 0 */ | |
682 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
683 | /* IOSQE_ASYNC */ | |
684 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
685 | /* IOSQE_BUFFER_SELECT */ |
686 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 687 | |
6b47ee6e PB |
688 | /* fail rest of links */ |
689 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
690 | /* on inflight list */ | |
691 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
692 | /* read/write uses file position */ | |
693 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
694 | /* must not punt to workers */ | |
695 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 696 | /* has or had linked timeout */ |
6b47ee6e | 697 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
698 | /* regular file */ |
699 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
700 | /* needs cleanup */ |
701 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
702 | /* already went through poll handler */ |
703 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
704 | /* buffer already selected */ |
705 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
706 | /* doesn't need file table for this request */ |
707 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
708 | /* io_wq_work is initialized */ |
709 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
710 | /* linked timeout is active, i.e. prepared by link's head */ |
711 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
712 | /* completion is deferred through io_comp_state */ |
713 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
714 | }; |
715 | ||
716 | struct async_poll { | |
717 | struct io_poll_iocb poll; | |
807abcb0 | 718 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
719 | }; |
720 | ||
09bb8394 JA |
721 | /* |
722 | * NOTE! Each of the iocb union members has the file pointer | |
723 | * as the first entry in their struct definition. So you can | |
724 | * access the file pointer through any of the sub-structs, | |
725 | * or directly as just 'ki_filp' in this struct. | |
726 | */ | |
2b188cc1 | 727 | struct io_kiocb { |
221c5eb2 | 728 | union { |
09bb8394 | 729 | struct file *file; |
9adbd45d | 730 | struct io_rw rw; |
221c5eb2 | 731 | struct io_poll_iocb poll; |
018043be | 732 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
733 | struct io_accept accept; |
734 | struct io_sync sync; | |
fbf23849 | 735 | struct io_cancel cancel; |
b29472ee | 736 | struct io_timeout timeout; |
0bdf7a2d | 737 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 738 | struct io_connect connect; |
e47293fd | 739 | struct io_sr_msg sr_msg; |
15b71abe | 740 | struct io_open open; |
b5dba59e | 741 | struct io_close close; |
269bbe5f | 742 | struct io_rsrc_update rsrc_update; |
4840e418 | 743 | struct io_fadvise fadvise; |
c1ca757b | 744 | struct io_madvise madvise; |
3e4827b0 | 745 | struct io_epoll epoll; |
7d67af2c | 746 | struct io_splice splice; |
ddf0322d | 747 | struct io_provide_buf pbuf; |
1d9e1288 | 748 | struct io_statx statx; |
36f4fa68 | 749 | struct io_shutdown shutdown; |
80a261fd | 750 | struct io_rename rename; |
14a1143b | 751 | struct io_unlink unlink; |
3ca405eb PB |
752 | /* use only after cleaning per-op data, see io_clean_op() */ |
753 | struct io_completion compl; | |
221c5eb2 | 754 | }; |
2b188cc1 | 755 | |
e8c2bc1f JA |
756 | /* opcode allocated if it needs to store data for async defer */ |
757 | void *async_data; | |
d625c6ee | 758 | u8 opcode; |
65a6543d XW |
759 | /* polled IO has completed */ |
760 | u8 iopoll_completed; | |
2b188cc1 | 761 | |
4f4eeba8 | 762 | u16 buf_index; |
9cf7c104 | 763 | u32 result; |
4f4eeba8 | 764 | |
010e8e6b PB |
765 | struct io_ring_ctx *ctx; |
766 | unsigned int flags; | |
767 | refcount_t refs; | |
768 | struct task_struct *task; | |
769 | u64 user_data; | |
d7718a9d | 770 | |
f2f87370 | 771 | struct io_kiocb *link; |
269bbe5f | 772 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 773 | |
d21ffe7e PB |
774 | /* |
775 | * 1. used with ctx->iopoll_list with reads/writes | |
776 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
777 | */ | |
010e8e6b | 778 | struct list_head inflight_entry; |
010e8e6b PB |
779 | struct callback_head task_work; |
780 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
781 | struct hlist_node hash_node; | |
782 | struct async_poll *apoll; | |
783 | struct io_wq_work work; | |
2b188cc1 | 784 | }; |
05589553 | 785 | |
27dc8338 PB |
786 | struct io_defer_entry { |
787 | struct list_head list; | |
788 | struct io_kiocb *req; | |
9cf7c104 | 789 | u32 seq; |
2b188cc1 JA |
790 | }; |
791 | ||
d3656344 | 792 | struct io_op_def { |
d3656344 JA |
793 | /* needs req->file assigned */ |
794 | unsigned needs_file : 1; | |
d3656344 JA |
795 | /* hash wq insertion if file is a regular file */ |
796 | unsigned hash_reg_file : 1; | |
797 | /* unbound wq insertion if file is a non-regular file */ | |
798 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
799 | /* opcode is not supported by this kernel */ |
800 | unsigned not_supported : 1; | |
8a72758c JA |
801 | /* set if opcode supports polled "wait" */ |
802 | unsigned pollin : 1; | |
803 | unsigned pollout : 1; | |
bcda7baa JA |
804 | /* op supports buffer selection */ |
805 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
806 | /* must always have async data allocated */ |
807 | unsigned needs_async_data : 1; | |
27926b68 JA |
808 | /* should block plug */ |
809 | unsigned plug : 1; | |
e8c2bc1f JA |
810 | /* size of async data needed, if any */ |
811 | unsigned short async_size; | |
0f203765 | 812 | unsigned work_flags; |
d3656344 JA |
813 | }; |
814 | ||
0918682b | 815 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
816 | [IORING_OP_NOP] = {}, |
817 | [IORING_OP_READV] = { | |
d3656344 JA |
818 | .needs_file = 1, |
819 | .unbound_nonreg_file = 1, | |
8a72758c | 820 | .pollin = 1, |
4d954c25 | 821 | .buffer_select = 1, |
e8c2bc1f | 822 | .needs_async_data = 1, |
27926b68 | 823 | .plug = 1, |
e8c2bc1f | 824 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 825 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 826 | }, |
0463b6c5 | 827 | [IORING_OP_WRITEV] = { |
d3656344 JA |
828 | .needs_file = 1, |
829 | .hash_reg_file = 1, | |
830 | .unbound_nonreg_file = 1, | |
8a72758c | 831 | .pollout = 1, |
e8c2bc1f | 832 | .needs_async_data = 1, |
27926b68 | 833 | .plug = 1, |
e8c2bc1f | 834 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
835 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
836 | IO_WQ_WORK_FSIZE, | |
d3656344 | 837 | }, |
0463b6c5 | 838 | [IORING_OP_FSYNC] = { |
d3656344 | 839 | .needs_file = 1, |
0f203765 | 840 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 841 | }, |
0463b6c5 | 842 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
843 | .needs_file = 1, |
844 | .unbound_nonreg_file = 1, | |
8a72758c | 845 | .pollin = 1, |
27926b68 | 846 | .plug = 1, |
e8c2bc1f | 847 | .async_size = sizeof(struct io_async_rw), |
4017eb91 | 848 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
d3656344 | 849 | }, |
0463b6c5 | 850 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
851 | .needs_file = 1, |
852 | .hash_reg_file = 1, | |
853 | .unbound_nonreg_file = 1, | |
8a72758c | 854 | .pollout = 1, |
27926b68 | 855 | .plug = 1, |
e8c2bc1f | 856 | .async_size = sizeof(struct io_async_rw), |
4017eb91 JA |
857 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
858 | IO_WQ_WORK_MM, | |
d3656344 | 859 | }, |
0463b6c5 | 860 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
861 | .needs_file = 1, |
862 | .unbound_nonreg_file = 1, | |
863 | }, | |
0463b6c5 PB |
864 | [IORING_OP_POLL_REMOVE] = {}, |
865 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 866 | .needs_file = 1, |
0f203765 | 867 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 868 | }, |
0463b6c5 | 869 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
870 | .needs_file = 1, |
871 | .unbound_nonreg_file = 1, | |
8a72758c | 872 | .pollout = 1, |
e8c2bc1f JA |
873 | .needs_async_data = 1, |
874 | .async_size = sizeof(struct io_async_msghdr), | |
10cad2c4 | 875 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 876 | }, |
0463b6c5 | 877 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
878 | .needs_file = 1, |
879 | .unbound_nonreg_file = 1, | |
8a72758c | 880 | .pollin = 1, |
52de1fe1 | 881 | .buffer_select = 1, |
e8c2bc1f JA |
882 | .needs_async_data = 1, |
883 | .async_size = sizeof(struct io_async_msghdr), | |
10cad2c4 | 884 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 885 | }, |
0463b6c5 | 886 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
887 | .needs_async_data = 1, |
888 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 889 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 890 | }, |
9c8e11b3 PB |
891 | [IORING_OP_TIMEOUT_REMOVE] = { |
892 | /* used by timeout updates' prep() */ | |
893 | .work_flags = IO_WQ_WORK_MM, | |
894 | }, | |
0463b6c5 | 895 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
896 | .needs_file = 1, |
897 | .unbound_nonreg_file = 1, | |
8a72758c | 898 | .pollin = 1, |
0f203765 | 899 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
d3656344 | 900 | }, |
0463b6c5 PB |
901 | [IORING_OP_ASYNC_CANCEL] = {}, |
902 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
903 | .needs_async_data = 1, |
904 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 905 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 906 | }, |
0463b6c5 | 907 | [IORING_OP_CONNECT] = { |
d3656344 JA |
908 | .needs_file = 1, |
909 | .unbound_nonreg_file = 1, | |
8a72758c | 910 | .pollout = 1, |
e8c2bc1f JA |
911 | .needs_async_data = 1, |
912 | .async_size = sizeof(struct io_async_connect), | |
0f203765 | 913 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 914 | }, |
0463b6c5 | 915 | [IORING_OP_FALLOCATE] = { |
d3656344 | 916 | .needs_file = 1, |
69228338 | 917 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
d3656344 | 918 | }, |
0463b6c5 | 919 | [IORING_OP_OPENAT] = { |
0f203765 | 920 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
14587a46 | 921 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
d3656344 | 922 | }, |
0463b6c5 | 923 | [IORING_OP_CLOSE] = { |
0f203765 | 924 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 925 | }, |
0463b6c5 | 926 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 927 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 928 | }, |
0463b6c5 | 929 | [IORING_OP_STATX] = { |
0f203765 JA |
930 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
931 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 932 | }, |
0463b6c5 | 933 | [IORING_OP_READ] = { |
3a6820f2 JA |
934 | .needs_file = 1, |
935 | .unbound_nonreg_file = 1, | |
8a72758c | 936 | .pollin = 1, |
bcda7baa | 937 | .buffer_select = 1, |
27926b68 | 938 | .plug = 1, |
e8c2bc1f | 939 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 940 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 941 | }, |
0463b6c5 | 942 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
943 | .needs_file = 1, |
944 | .unbound_nonreg_file = 1, | |
8a72758c | 945 | .pollout = 1, |
27926b68 | 946 | .plug = 1, |
e8c2bc1f | 947 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
948 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
949 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 950 | }, |
0463b6c5 | 951 | [IORING_OP_FADVISE] = { |
4840e418 | 952 | .needs_file = 1, |
0f203765 | 953 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 954 | }, |
0463b6c5 | 955 | [IORING_OP_MADVISE] = { |
0f203765 | 956 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 957 | }, |
0463b6c5 | 958 | [IORING_OP_SEND] = { |
fddaface JA |
959 | .needs_file = 1, |
960 | .unbound_nonreg_file = 1, | |
8a72758c | 961 | .pollout = 1, |
0f203765 | 962 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 963 | }, |
0463b6c5 | 964 | [IORING_OP_RECV] = { |
fddaface JA |
965 | .needs_file = 1, |
966 | .unbound_nonreg_file = 1, | |
8a72758c | 967 | .pollin = 1, |
bcda7baa | 968 | .buffer_select = 1, |
0f203765 | 969 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 970 | }, |
0463b6c5 | 971 | [IORING_OP_OPENAT2] = { |
0f203765 | 972 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
14587a46 | 973 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
cebdb986 | 974 | }, |
3e4827b0 JA |
975 | [IORING_OP_EPOLL_CTL] = { |
976 | .unbound_nonreg_file = 1, | |
0f203765 | 977 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 978 | }, |
7d67af2c PB |
979 | [IORING_OP_SPLICE] = { |
980 | .needs_file = 1, | |
981 | .hash_reg_file = 1, | |
982 | .unbound_nonreg_file = 1, | |
0f203765 | 983 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
984 | }, |
985 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 986 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
987 | [IORING_OP_TEE] = { |
988 | .needs_file = 1, | |
989 | .hash_reg_file = 1, | |
990 | .unbound_nonreg_file = 1, | |
991 | }, | |
36f4fa68 JA |
992 | [IORING_OP_SHUTDOWN] = { |
993 | .needs_file = 1, | |
994 | }, | |
80a261fd JA |
995 | [IORING_OP_RENAMEAT] = { |
996 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
997 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
998 | }, | |
14a1143b JA |
999 | [IORING_OP_UNLINKAT] = { |
1000 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
1001 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
1002 | }, | |
d3656344 JA |
1003 | }; |
1004 | ||
2e0464d4 BM |
1005 | enum io_mem_account { |
1006 | ACCT_LOCKED, | |
1007 | ACCT_PINNED, | |
1008 | }; | |
1009 | ||
9936c7c2 PB |
1010 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1011 | struct task_struct *task, | |
1012 | struct files_struct *files); | |
269bbe5f | 1013 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 1014 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 1015 | struct io_ring_ctx *ctx); |
bc9744cd PB |
1016 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
1017 | struct fixed_rsrc_ref_node *ref_node); | |
1ffc5422 | 1018 | |
81b68a5c | 1019 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 1020 | unsigned int issue_flags); |
78e19bbe | 1021 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1022 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1023 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1024 | static void io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 1025 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1026 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1027 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1028 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1029 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1030 | unsigned nr_args); |
3ca405eb | 1031 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1032 | static struct file *io_file_get(struct io_submit_state *state, |
1033 | struct io_kiocb *req, int fd, bool fixed); | |
c1379e24 | 1034 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
269bbe5f | 1035 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1036 | |
847595de PB |
1037 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1038 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1039 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1040 | const struct iovec *fast_iov, | |
227c0c96 | 1041 | struct iov_iter *iter, bool force); |
907d1df3 | 1042 | static void io_req_task_queue(struct io_kiocb *req); |
de0617e4 | 1043 | |
2b188cc1 JA |
1044 | static struct kmem_cache *req_cachep; |
1045 | ||
0918682b | 1046 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1047 | |
1048 | struct sock *io_uring_get_socket(struct file *file) | |
1049 | { | |
1050 | #if defined(CONFIG_UNIX) | |
1051 | if (file->f_op == &io_uring_fops) { | |
1052 | struct io_ring_ctx *ctx = file->private_data; | |
1053 | ||
1054 | return ctx->ring_sock->sk; | |
1055 | } | |
1056 | #endif | |
1057 | return NULL; | |
1058 | } | |
1059 | EXPORT_SYMBOL(io_uring_get_socket); | |
1060 | ||
f2f87370 PB |
1061 | #define io_for_each_link(pos, head) \ |
1062 | for (pos = (head); pos; pos = pos->link) | |
1063 | ||
3ca405eb PB |
1064 | static inline void io_clean_op(struct io_kiocb *req) |
1065 | { | |
9d5c8190 | 1066 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1067 | __io_clean_op(req); |
1068 | } | |
1069 | ||
36f72fe2 PB |
1070 | static inline void io_set_resource_node(struct io_kiocb *req) |
1071 | { | |
1072 | struct io_ring_ctx *ctx = req->ctx; | |
1073 | ||
269bbe5f BM |
1074 | if (!req->fixed_rsrc_refs) { |
1075 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1076 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1077 | } |
1078 | } | |
1079 | ||
08d23634 PB |
1080 | static bool io_match_task(struct io_kiocb *head, |
1081 | struct task_struct *task, | |
1082 | struct files_struct *files) | |
1083 | { | |
1084 | struct io_kiocb *req; | |
1085 | ||
84965ff8 JA |
1086 | if (task && head->task != task) { |
1087 | /* in terms of cancelation, always match if req task is dead */ | |
1088 | if (head->task->flags & PF_EXITING) | |
1089 | return true; | |
08d23634 | 1090 | return false; |
84965ff8 | 1091 | } |
08d23634 PB |
1092 | if (!files) |
1093 | return true; | |
1094 | ||
1095 | io_for_each_link(req, head) { | |
02a13674 JA |
1096 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1097 | continue; | |
1098 | if (req->file && req->file->f_op == &io_uring_fops) | |
1099 | return true; | |
1100 | if ((req->work.flags & IO_WQ_WORK_FILES) && | |
08d23634 PB |
1101 | req->work.identity->files == files) |
1102 | return true; | |
1103 | } | |
1104 | return false; | |
1105 | } | |
1106 | ||
28cea78a | 1107 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1108 | { |
28cea78a | 1109 | struct files_struct *files = current->files; |
c40f6379 JA |
1110 | struct mm_struct *mm = current->mm; |
1111 | ||
1112 | if (mm) { | |
1113 | kthread_unuse_mm(mm); | |
1114 | mmput(mm); | |
4b70cf9d | 1115 | current->mm = NULL; |
c40f6379 | 1116 | } |
28cea78a JA |
1117 | if (files) { |
1118 | struct nsproxy *nsproxy = current->nsproxy; | |
1119 | ||
1120 | task_lock(current); | |
1121 | current->files = NULL; | |
1122 | current->nsproxy = NULL; | |
1123 | task_unlock(current); | |
1124 | put_files_struct(files); | |
1125 | put_nsproxy(nsproxy); | |
1126 | } | |
1127 | } | |
1128 | ||
1a38ffc9 | 1129 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a | 1130 | { |
621fadc2 PB |
1131 | if (current->flags & PF_EXITING) |
1132 | return -EFAULT; | |
1133 | ||
28cea78a JA |
1134 | if (!current->files) { |
1135 | struct files_struct *files; | |
1136 | struct nsproxy *nsproxy; | |
1137 | ||
1138 | task_lock(ctx->sqo_task); | |
1139 | files = ctx->sqo_task->files; | |
1140 | if (!files) { | |
1141 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1142 | return -EOWNERDEAD; |
28cea78a JA |
1143 | } |
1144 | atomic_inc(&files->count); | |
1145 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1146 | nsproxy = ctx->sqo_task->nsproxy; | |
1147 | task_unlock(ctx->sqo_task); | |
1148 | ||
1149 | task_lock(current); | |
1150 | current->files = files; | |
1151 | current->nsproxy = nsproxy; | |
1152 | task_unlock(current); | |
1153 | } | |
1a38ffc9 | 1154 | return 0; |
c40f6379 JA |
1155 | } |
1156 | ||
1157 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1158 | { | |
4b70cf9d JA |
1159 | struct mm_struct *mm; |
1160 | ||
621fadc2 PB |
1161 | if (current->flags & PF_EXITING) |
1162 | return -EFAULT; | |
4b70cf9d JA |
1163 | if (current->mm) |
1164 | return 0; | |
1165 | ||
1166 | /* Should never happen */ | |
1167 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) | |
1168 | return -EFAULT; | |
1169 | ||
1170 | task_lock(ctx->sqo_task); | |
1171 | mm = ctx->sqo_task->mm; | |
1172 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1173 | mm = NULL; | |
1174 | task_unlock(ctx->sqo_task); | |
1175 | ||
1176 | if (mm) { | |
1177 | kthread_use_mm(mm); | |
1178 | return 0; | |
c40f6379 JA |
1179 | } |
1180 | ||
4b70cf9d | 1181 | return -EFAULT; |
c40f6379 JA |
1182 | } |
1183 | ||
28cea78a JA |
1184 | static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1185 | struct io_kiocb *req) | |
c40f6379 | 1186 | { |
28cea78a | 1187 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1188 | int ret; |
28cea78a JA |
1189 | |
1190 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1191 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1192 | if (unlikely(ret)) |
1193 | return ret; | |
1194 | } | |
1195 | ||
1a38ffc9 PB |
1196 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1197 | ret = __io_sq_thread_acquire_files(ctx); | |
1198 | if (unlikely(ret)) | |
1199 | return ret; | |
1200 | } | |
28cea78a JA |
1201 | |
1202 | return 0; | |
c40f6379 JA |
1203 | } |
1204 | ||
91d8f519 DZ |
1205 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1206 | struct cgroup_subsys_state **cur_css) | |
1207 | ||
1208 | { | |
1209 | #ifdef CONFIG_BLK_CGROUP | |
1210 | /* puts the old one when swapping */ | |
1211 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1212 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1213 | *cur_css = ctx->sqo_blkcg_css; | |
1214 | } | |
1215 | #endif | |
1216 | } | |
1217 | ||
1218 | static void io_sq_thread_unassociate_blkcg(void) | |
1219 | { | |
1220 | #ifdef CONFIG_BLK_CGROUP | |
1221 | kthread_associate_blkcg(NULL); | |
1222 | #endif | |
1223 | } | |
1224 | ||
c40f6379 JA |
1225 | static inline void req_set_fail_links(struct io_kiocb *req) |
1226 | { | |
1227 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1228 | req->flags |= REQ_F_FAIL_LINK; | |
1229 | } | |
4a38aed2 | 1230 | |
1e6fa521 JA |
1231 | /* |
1232 | * None of these are dereferenced, they are simply used to check if any of | |
1233 | * them have changed. If we're under current and check they are still the | |
1234 | * same, we're fine to grab references to them for actual out-of-line use. | |
1235 | */ | |
1236 | static void io_init_identity(struct io_identity *id) | |
1237 | { | |
1238 | id->files = current->files; | |
1239 | id->mm = current->mm; | |
1240 | #ifdef CONFIG_BLK_CGROUP | |
1241 | rcu_read_lock(); | |
1242 | id->blkcg_css = blkcg_css(); | |
1243 | rcu_read_unlock(); | |
1244 | #endif | |
1245 | id->creds = current_cred(); | |
1246 | id->nsproxy = current->nsproxy; | |
1247 | id->fs = current->fs; | |
1248 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1249 | #ifdef CONFIG_AUDIT |
1250 | id->loginuid = current->loginuid; | |
1251 | id->sessionid = current->sessionid; | |
1252 | #endif | |
1e6fa521 JA |
1253 | refcount_set(&id->count, 1); |
1254 | } | |
1255 | ||
ec99ca6c PB |
1256 | static inline void __io_req_init_async(struct io_kiocb *req) |
1257 | { | |
1258 | memset(&req->work, 0, sizeof(req->work)); | |
1259 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1260 | } | |
1261 | ||
7cdaf587 XW |
1262 | /* |
1263 | * Note: must call io_req_init_async() for the first time you | |
1264 | * touch any members of io_wq_work. | |
1265 | */ | |
1266 | static inline void io_req_init_async(struct io_kiocb *req) | |
1267 | { | |
500a373d JA |
1268 | struct io_uring_task *tctx = current->io_uring; |
1269 | ||
7cdaf587 XW |
1270 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1271 | return; | |
1272 | ||
ec99ca6c | 1273 | __io_req_init_async(req); |
500a373d JA |
1274 | |
1275 | /* Grab a ref if this isn't our static identity */ | |
1276 | req->work.identity = tctx->identity; | |
1277 | if (tctx->identity != &tctx->__identity) | |
1278 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1279 | } |
1280 | ||
2b188cc1 JA |
1281 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1282 | { | |
1283 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1284 | ||
0f158b4c | 1285 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1286 | } |
1287 | ||
8eb7e2d0 PB |
1288 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1289 | { | |
1290 | return !req->timeout.off; | |
1291 | } | |
1292 | ||
2b188cc1 JA |
1293 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1294 | { | |
1295 | struct io_ring_ctx *ctx; | |
78076bb6 | 1296 | int hash_bits; |
2b188cc1 JA |
1297 | |
1298 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1299 | if (!ctx) | |
1300 | return NULL; | |
1301 | ||
78076bb6 JA |
1302 | /* |
1303 | * Use 5 bits less than the max cq entries, that should give us around | |
1304 | * 32 entries per hash list if totally full and uniformly spread. | |
1305 | */ | |
1306 | hash_bits = ilog2(p->cq_entries); | |
1307 | hash_bits -= 5; | |
1308 | if (hash_bits <= 0) | |
1309 | hash_bits = 1; | |
1310 | ctx->cancel_hash_bits = hash_bits; | |
1311 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1312 | GFP_KERNEL); | |
1313 | if (!ctx->cancel_hash) | |
1314 | goto err; | |
1315 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1316 | ||
21482896 | 1317 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1318 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1319 | goto err; | |
2b188cc1 JA |
1320 | |
1321 | ctx->flags = p->flags; | |
90554200 | 1322 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1323 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1324 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1325 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1326 | init_completion(&ctx->ref_comp); |
1327 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1328 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1329 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1330 | mutex_init(&ctx->uring_lock); |
1331 | init_waitqueue_head(&ctx->wait); | |
1332 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1333 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1334 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1335 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1336 | spin_lock_init(&ctx->inflight_lock); |
1337 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1338 | spin_lock_init(&ctx->rsrc_ref_lock); |
1339 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1340 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1341 | init_llist_head(&ctx->rsrc_put_llist); | |
2b188cc1 | 1342 | return ctx; |
206aefde | 1343 | err: |
78076bb6 | 1344 | kfree(ctx->cancel_hash); |
206aefde JA |
1345 | kfree(ctx); |
1346 | return NULL; | |
2b188cc1 JA |
1347 | } |
1348 | ||
9cf7c104 | 1349 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1350 | { |
2bc9930e JA |
1351 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1352 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1353 | |
9cf7c104 | 1354 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1355 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1356 | } |
de0617e4 | 1357 | |
9d858b21 | 1358 | return false; |
de0617e4 JA |
1359 | } |
1360 | ||
5c3462cf | 1361 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1362 | { |
500a373d | 1363 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1364 | return; |
1365 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1366 | kfree(req->work.identity); | |
1367 | } | |
1368 | ||
4edf20f9 | 1369 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1370 | { |
7cdaf587 | 1371 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1372 | return; |
51a4cc11 | 1373 | |
e86d0047 | 1374 | if (req->work.flags & IO_WQ_WORK_MM) |
98447d65 | 1375 | mmdrop(req->work.identity->mm); |
91d8f519 | 1376 | #ifdef CONFIG_BLK_CGROUP |
e86d0047 | 1377 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
98447d65 | 1378 | css_put(req->work.identity->blkcg_css); |
91d8f519 | 1379 | #endif |
e86d0047 | 1380 | if (req->work.flags & IO_WQ_WORK_CREDS) |
98447d65 | 1381 | put_cred(req->work.identity->creds); |
dfead8a8 | 1382 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1383 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1384 | |
98447d65 | 1385 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1386 | if (--fs->users) |
1387 | fs = NULL; | |
98447d65 | 1388 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1389 | if (fs) |
1390 | free_fs_struct(fs); | |
1391 | } | |
34e08fed PB |
1392 | if (req->work.flags & IO_WQ_WORK_FILES) { |
1393 | put_files_struct(req->work.identity->files); | |
1394 | put_nsproxy(req->work.identity->nsproxy); | |
34e08fed PB |
1395 | } |
1396 | if (req->flags & REQ_F_INFLIGHT) { | |
1397 | struct io_ring_ctx *ctx = req->ctx; | |
1398 | struct io_uring_task *tctx = req->task->io_uring; | |
1399 | unsigned long flags; | |
1400 | ||
1401 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1402 | list_del(&req->inflight_entry); | |
1403 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1404 | req->flags &= ~REQ_F_INFLIGHT; | |
1405 | if (atomic_read(&tctx->in_idle)) | |
1406 | wake_up(&tctx->wait); | |
1407 | } | |
51a4cc11 | 1408 | |
e86d0047 PB |
1409 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
1410 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | | |
1411 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); | |
5c3462cf | 1412 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1413 | } |
1414 | ||
1e6fa521 JA |
1415 | /* |
1416 | * Create a private copy of io_identity, since some fields don't match | |
1417 | * the current context. | |
1418 | */ | |
1419 | static bool io_identity_cow(struct io_kiocb *req) | |
1420 | { | |
5c3462cf | 1421 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1422 | const struct cred *creds = NULL; |
1423 | struct io_identity *id; | |
1424 | ||
1425 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1426 | creds = req->work.identity->creds; | |
1427 | ||
1428 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1429 | if (unlikely(!id)) { | |
1430 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1431 | return false; | |
1432 | } | |
1433 | ||
1434 | /* | |
1435 | * We can safely just re-init the creds we copied Either the field | |
1436 | * matches the current one, or we haven't grabbed it yet. The only | |
1437 | * exception is ->creds, through registered personalities, so handle | |
1438 | * that one separately. | |
1439 | */ | |
1440 | io_init_identity(id); | |
1441 | if (creds) | |
e8c954df | 1442 | id->creds = creds; |
1e6fa521 JA |
1443 | |
1444 | /* add one for this request */ | |
1445 | refcount_inc(&id->count); | |
1446 | ||
cb8a8ae3 JA |
1447 | /* drop tctx and req identity references, if needed */ |
1448 | if (tctx->identity != &tctx->__identity && | |
1449 | refcount_dec_and_test(&tctx->identity->count)) | |
1450 | kfree(tctx->identity); | |
1451 | if (req->work.identity != &tctx->__identity && | |
1452 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1453 | kfree(req->work.identity); |
1454 | ||
1455 | req->work.identity = id; | |
500a373d | 1456 | tctx->identity = id; |
1e6fa521 JA |
1457 | return true; |
1458 | } | |
1459 | ||
ce3d5aae PB |
1460 | static void io_req_track_inflight(struct io_kiocb *req) |
1461 | { | |
1462 | struct io_ring_ctx *ctx = req->ctx; | |
1463 | ||
1464 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1465 | io_req_init_async(req); | |
1466 | req->flags |= REQ_F_INFLIGHT; | |
1467 | ||
1468 | spin_lock_irq(&ctx->inflight_lock); | |
1469 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1470 | spin_unlock_irq(&ctx->inflight_lock); | |
1471 | } | |
1472 | } | |
1473 | ||
1e6fa521 | 1474 | static bool io_grab_identity(struct io_kiocb *req) |
18d9be1a | 1475 | { |
d3656344 | 1476 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1477 | struct io_identity *id = req->work.identity; |
54a91f3b | 1478 | |
69228338 JA |
1479 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1480 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1481 | return false; | |
1482 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1483 | } | |
91d8f519 | 1484 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1485 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1486 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1487 | rcu_read_lock(); |
1e6fa521 JA |
1488 | if (id->blkcg_css != blkcg_css()) { |
1489 | rcu_read_unlock(); | |
1490 | return false; | |
1491 | } | |
91d8f519 DZ |
1492 | /* |
1493 | * This should be rare, either the cgroup is dying or the task | |
1494 | * is moving cgroups. Just punt to root for the handful of ios. | |
1495 | */ | |
1e6fa521 | 1496 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1497 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1498 | rcu_read_unlock(); |
1499 | } | |
1500 | #endif | |
dfead8a8 | 1501 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1502 | if (id->creds != current_cred()) |
1503 | return false; | |
1504 | get_cred(id->creds); | |
dfead8a8 JA |
1505 | req->work.flags |= IO_WQ_WORK_CREDS; |
1506 | } | |
4ea33a97 JA |
1507 | #ifdef CONFIG_AUDIT |
1508 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1509 | current->sessionid != id->sessionid) | |
1510 | return false; | |
1511 | #endif | |
dfead8a8 JA |
1512 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1513 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1514 | if (current->fs != id->fs) |
1515 | return false; | |
1516 | spin_lock(&id->fs->lock); | |
1517 | if (!id->fs->in_exec) { | |
1518 | id->fs->users++; | |
dfead8a8 | 1519 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1520 | } else { |
1521 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1522 | } | |
1523 | spin_unlock(¤t->fs->lock); | |
1524 | } | |
af604703 PB |
1525 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1526 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1527 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1528 | if (id->files != current->files || | |
1529 | id->nsproxy != current->nsproxy) | |
1530 | return false; | |
1531 | atomic_inc(&id->files->count); | |
1532 | get_nsproxy(id->nsproxy); | |
af604703 | 1533 | req->work.flags |= IO_WQ_WORK_FILES; |
ce3d5aae | 1534 | io_req_track_inflight(req); |
af604703 | 1535 | } |
77788775 JA |
1536 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1537 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1538 | if (id->mm != current->mm) | |
1539 | return false; | |
1540 | mmgrab(id->mm); | |
1541 | req->work.flags |= IO_WQ_WORK_MM; | |
1542 | } | |
1e6fa521 JA |
1543 | |
1544 | return true; | |
1545 | } | |
1546 | ||
1547 | static void io_prep_async_work(struct io_kiocb *req) | |
1548 | { | |
1549 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1550 | struct io_ring_ctx *ctx = req->ctx; |
1551 | ||
1552 | io_req_init_async(req); | |
1553 | ||
feaadc4f PB |
1554 | if (req->flags & REQ_F_FORCE_ASYNC) |
1555 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1556 | ||
1e6fa521 JA |
1557 | if (req->flags & REQ_F_ISREG) { |
1558 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1559 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1560 | } else { | |
1561 | if (def->unbound_nonreg_file) | |
1562 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1563 | } | |
1564 | ||
1e6fa521 JA |
1565 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1566 | if (io_grab_identity(req)) | |
1567 | return; | |
1568 | ||
1569 | if (!io_identity_cow(req)) | |
1570 | return; | |
1571 | ||
1572 | /* can't fail at this point */ | |
1573 | if (!io_grab_identity(req)) | |
1574 | WARN_ON(1); | |
561fb04a | 1575 | } |
cccf0ee8 | 1576 | |
cbdcb435 | 1577 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1578 | { |
cbdcb435 | 1579 | struct io_kiocb *cur; |
54a91f3b | 1580 | |
f2f87370 PB |
1581 | io_for_each_link(cur, req) |
1582 | io_prep_async_work(cur); | |
561fb04a JA |
1583 | } |
1584 | ||
7271ef3a | 1585 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1586 | { |
a197f664 | 1587 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1588 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1589 | |
8766dd51 PB |
1590 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1591 | &req->work, req->flags); | |
1592 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1593 | return link; |
18d9be1a JA |
1594 | } |
1595 | ||
cbdcb435 PB |
1596 | static void io_queue_async_work(struct io_kiocb *req) |
1597 | { | |
7271ef3a JA |
1598 | struct io_kiocb *link; |
1599 | ||
cbdcb435 PB |
1600 | /* init ->work of the whole link before punting */ |
1601 | io_prep_async_link(req); | |
7271ef3a JA |
1602 | link = __io_queue_async_work(req); |
1603 | ||
1604 | if (link) | |
1605 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1606 | } |
1607 | ||
5262f567 JA |
1608 | static void io_kill_timeout(struct io_kiocb *req) |
1609 | { | |
e8c2bc1f | 1610 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1611 | int ret; |
1612 | ||
e8c2bc1f | 1613 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1614 | if (ret != -1) { |
01cec8c1 PB |
1615 | atomic_set(&req->ctx->cq_timeouts, |
1616 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1617 | list_del_init(&req->timeout.list); |
78e19bbe | 1618 | io_cqring_fill_event(req, 0); |
216578e5 | 1619 | io_put_req_deferred(req, 1); |
5262f567 JA |
1620 | } |
1621 | } | |
1622 | ||
76e1b642 JA |
1623 | /* |
1624 | * Returns true if we found and killed one or more timeouts | |
1625 | */ | |
6b81928d PB |
1626 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1627 | struct files_struct *files) | |
5262f567 JA |
1628 | { |
1629 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1630 | int canceled = 0; |
5262f567 JA |
1631 | |
1632 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1633 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1634 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1635 | io_kill_timeout(req); |
76e1b642 JA |
1636 | canceled++; |
1637 | } | |
f3606e3a | 1638 | } |
5262f567 | 1639 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1640 | return canceled != 0; |
5262f567 JA |
1641 | } |
1642 | ||
04518945 | 1643 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1644 | { |
04518945 | 1645 | do { |
27dc8338 PB |
1646 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1647 | struct io_defer_entry, list); | |
de0617e4 | 1648 | |
9cf7c104 | 1649 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1650 | break; |
27dc8338 | 1651 | list_del_init(&de->list); |
907d1df3 | 1652 | io_req_task_queue(de->req); |
27dc8338 | 1653 | kfree(de); |
04518945 PB |
1654 | } while (!list_empty(&ctx->defer_list)); |
1655 | } | |
1656 | ||
360428f8 | 1657 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1658 | { |
f010505b MDG |
1659 | u32 seq; |
1660 | ||
1661 | if (list_empty(&ctx->timeout_list)) | |
1662 | return; | |
1663 | ||
1664 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1665 | ||
1666 | do { | |
1667 | u32 events_needed, events_got; | |
360428f8 | 1668 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1669 | struct io_kiocb, timeout.list); |
de0617e4 | 1670 | |
8eb7e2d0 | 1671 | if (io_is_timeout_noseq(req)) |
360428f8 | 1672 | break; |
f010505b MDG |
1673 | |
1674 | /* | |
1675 | * Since seq can easily wrap around over time, subtract | |
1676 | * the last seq at which timeouts were flushed before comparing. | |
1677 | * Assuming not more than 2^31-1 events have happened since, | |
1678 | * these subtractions won't have wrapped, so we can check if | |
1679 | * target is in [last_seq, current_seq] by comparing the two. | |
1680 | */ | |
1681 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1682 | events_got = seq - ctx->cq_last_tm_flush; | |
1683 | if (events_got < events_needed) | |
360428f8 | 1684 | break; |
bfe68a22 | 1685 | |
135fcde8 | 1686 | list_del_init(&req->timeout.list); |
5262f567 | 1687 | io_kill_timeout(req); |
f010505b MDG |
1688 | } while (!list_empty(&ctx->timeout_list)); |
1689 | ||
1690 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1691 | } |
5262f567 | 1692 | |
360428f8 PB |
1693 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1694 | { | |
1695 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1696 | |
1697 | /* order cqe stores with ring update */ | |
1698 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1699 | |
04518945 PB |
1700 | if (unlikely(!list_empty(&ctx->defer_list))) |
1701 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1702 | } |
1703 | ||
90554200 JA |
1704 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1705 | { | |
1706 | struct io_rings *r = ctx->rings; | |
1707 | ||
1708 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1709 | } | |
1710 | ||
888aae2e PB |
1711 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1712 | { | |
1713 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1714 | } | |
1715 | ||
2b188cc1 JA |
1716 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1717 | { | |
75b28aff | 1718 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1719 | unsigned tail; |
1720 | ||
115e12e5 SB |
1721 | /* |
1722 | * writes to the cq entry need to come after reading head; the | |
1723 | * control dependency is enough as we're using WRITE_ONCE to | |
1724 | * fill the cq entry | |
1725 | */ | |
888aae2e | 1726 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1727 | return NULL; |
1728 | ||
888aae2e | 1729 | tail = ctx->cached_cq_tail++; |
75b28aff | 1730 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1731 | } |
1732 | ||
f2842ab5 JA |
1733 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1734 | { | |
f0b493e6 JA |
1735 | if (!ctx->cq_ev_fd) |
1736 | return false; | |
7e55a19c SG |
1737 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1738 | return false; | |
f2842ab5 JA |
1739 | if (!ctx->eventfd_async) |
1740 | return true; | |
b41e9852 | 1741 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1742 | } |
1743 | ||
b41e9852 | 1744 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1745 | { |
b1445e59 PB |
1746 | /* see waitqueue_active() comment */ |
1747 | smp_mb(); | |
1748 | ||
1d7bb1d5 JA |
1749 | if (waitqueue_active(&ctx->wait)) |
1750 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1751 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1752 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1753 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1754 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1755 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1756 | wake_up_interruptible(&ctx->cq_wait); |
1757 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1758 | } | |
1d7bb1d5 JA |
1759 | } |
1760 | ||
80c18e4a PB |
1761 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1762 | { | |
b1445e59 PB |
1763 | /* see waitqueue_active() comment */ |
1764 | smp_mb(); | |
1765 | ||
80c18e4a PB |
1766 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1767 | if (waitqueue_active(&ctx->wait)) | |
1768 | wake_up(&ctx->wait); | |
1769 | } | |
1770 | if (io_should_trigger_evfd(ctx)) | |
1771 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1772 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1773 | wake_up_interruptible(&ctx->cq_wait); |
1774 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1775 | } | |
80c18e4a PB |
1776 | } |
1777 | ||
c4a2ed72 | 1778 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1779 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1780 | struct task_struct *tsk, | |
1781 | struct files_struct *files) | |
1d7bb1d5 JA |
1782 | { |
1783 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1784 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1785 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1786 | unsigned long flags; |
b18032bb | 1787 | bool all_flushed, posted; |
1d7bb1d5 JA |
1788 | LIST_HEAD(list); |
1789 | ||
e23de15f PB |
1790 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1791 | return false; | |
1d7bb1d5 | 1792 | |
b18032bb | 1793 | posted = false; |
1d7bb1d5 | 1794 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1795 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1796 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1797 | continue; |
1798 | ||
1d7bb1d5 JA |
1799 | cqe = io_get_cqring(ctx); |
1800 | if (!cqe && !force) | |
1801 | break; | |
1802 | ||
40d8ddd4 | 1803 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1804 | if (cqe) { |
1805 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1806 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1807 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1808 | } else { |
2c3bac6d | 1809 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1810 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1811 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1812 | } |
b18032bb | 1813 | posted = true; |
1d7bb1d5 JA |
1814 | } |
1815 | ||
09e88404 PB |
1816 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1817 | if (all_flushed) { | |
1818 | clear_bit(0, &ctx->sq_check_overflow); | |
1819 | clear_bit(0, &ctx->cq_check_overflow); | |
1820 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1821 | } | |
46930143 | 1822 | |
b18032bb JA |
1823 | if (posted) |
1824 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1825 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1826 | if (posted) |
1827 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1828 | |
1829 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1830 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1831 | list_del(&req->compl.list); | |
ec9c02ad | 1832 | io_put_req(req); |
1d7bb1d5 | 1833 | } |
c4a2ed72 | 1834 | |
09e88404 | 1835 | return all_flushed; |
1d7bb1d5 JA |
1836 | } |
1837 | ||
6c503150 PB |
1838 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1839 | struct task_struct *tsk, | |
1840 | struct files_struct *files) | |
1841 | { | |
1842 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1843 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1844 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1845 | mutex_lock(&ctx->uring_lock); | |
1846 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1847 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1848 | mutex_unlock(&ctx->uring_lock); | |
1849 | } | |
1850 | } | |
1851 | ||
bcda7baa | 1852 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1853 | { |
78e19bbe | 1854 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1855 | struct io_uring_cqe *cqe; |
1856 | ||
78e19bbe | 1857 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1858 | |
2b188cc1 JA |
1859 | /* |
1860 | * If we can't get a cq entry, userspace overflowed the | |
1861 | * submission (by quite a lot). Increment the overflow count in | |
1862 | * the ring. | |
1863 | */ | |
1864 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1865 | if (likely(cqe)) { |
78e19bbe | 1866 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1867 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1868 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1869 | } else if (ctx->cq_overflow_flushed || |
1870 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1871 | /* |
1872 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1873 | * then we cannot store the request for later flushing, we need | |
1874 | * to drop it on the floor. | |
1875 | */ | |
2c3bac6d PB |
1876 | ctx->cached_cq_overflow++; |
1877 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1878 | } else { |
ad3eb2c8 JA |
1879 | if (list_empty(&ctx->cq_overflow_list)) { |
1880 | set_bit(0, &ctx->sq_check_overflow); | |
1881 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1882 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1883 | } |
40d8ddd4 | 1884 | io_clean_op(req); |
1d7bb1d5 | 1885 | req->result = res; |
0f7e466b | 1886 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1887 | refcount_inc(&req->refs); |
1888 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1889 | } |
1890 | } | |
1891 | ||
bcda7baa JA |
1892 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1893 | { | |
1894 | __io_cqring_fill_event(req, res, 0); | |
1895 | } | |
1896 | ||
9ae1f8dd PB |
1897 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1898 | unsigned int cflags) | |
2b188cc1 | 1899 | { |
78e19bbe | 1900 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1901 | unsigned long flags; |
1902 | ||
1903 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1904 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1905 | io_commit_cqring(ctx); |
1906 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1907 | ||
8c838788 | 1908 | io_cqring_ev_posted(ctx); |
9ae1f8dd PB |
1909 | } |
1910 | ||
1911 | static inline void io_req_complete_nostate(struct io_kiocb *req, long res, | |
1912 | unsigned int cflags) | |
1913 | { | |
1914 | io_req_complete_post(req, res, cflags); | |
a38d68db | 1915 | io_put_req(req); |
2b188cc1 JA |
1916 | } |
1917 | ||
a38d68db | 1918 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1919 | unsigned int cflags) |
229a7b63 | 1920 | { |
a38d68db PB |
1921 | io_clean_op(req); |
1922 | req->result = res; | |
1923 | req->compl.cflags = cflags; | |
e342c807 | 1924 | req->flags |= REQ_F_COMPLETE_INLINE; |
a38d68db PB |
1925 | } |
1926 | ||
889fca73 PB |
1927 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1928 | long res, unsigned cflags) | |
a38d68db | 1929 | { |
889fca73 PB |
1930 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1931 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1932 | else |
889fca73 | 1933 | io_req_complete_nostate(req, res, cflags); |
e1e16097 JA |
1934 | } |
1935 | ||
a38d68db | 1936 | static inline void io_req_complete(struct io_kiocb *req, long res) |
bcda7baa | 1937 | { |
889fca73 | 1938 | __io_req_complete(req, 0, res, 0); |
bcda7baa JA |
1939 | } |
1940 | ||
258b29a9 | 1941 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1942 | { |
258b29a9 PB |
1943 | struct io_submit_state *state = &ctx->submit_state; |
1944 | ||
f6b6c7d6 | 1945 | if (!state->free_reqs) { |
291b2821 | 1946 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1947 | size_t sz; |
1948 | int ret; | |
1949 | ||
1950 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1951 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1952 | ||
1953 | /* | |
1954 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1955 | * retry single alloc to be on the safe side. | |
1956 | */ | |
1957 | if (unlikely(ret <= 0)) { | |
1958 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1959 | if (!state->reqs[0]) | |
3893f39f | 1960 | return NULL; |
fd6fab2c JA |
1961 | ret = 1; |
1962 | } | |
291b2821 | 1963 | state->free_reqs = ret; |
2b188cc1 JA |
1964 | } |
1965 | ||
291b2821 PB |
1966 | state->free_reqs--; |
1967 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1968 | } |
1969 | ||
8da11c19 PB |
1970 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1971 | bool fixed) | |
1972 | { | |
36f72fe2 | 1973 | if (!fixed) |
8da11c19 PB |
1974 | fput(file); |
1975 | } | |
1976 | ||
4edf20f9 | 1977 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1978 | { |
3ca405eb | 1979 | io_clean_op(req); |
929a3af9 | 1980 | |
e8c2bc1f JA |
1981 | if (req->async_data) |
1982 | kfree(req->async_data); | |
8da11c19 PB |
1983 | if (req->file) |
1984 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
1985 | if (req->fixed_rsrc_refs) |
1986 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 1987 | io_req_clean_work(req); |
e65ef56d JA |
1988 | } |
1989 | ||
7c660731 PB |
1990 | static inline void io_put_task(struct task_struct *task, int nr) |
1991 | { | |
1992 | struct io_uring_task *tctx = task->io_uring; | |
1993 | ||
1994 | percpu_counter_sub(&tctx->inflight, nr); | |
1995 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1996 | wake_up(&tctx->wait); | |
1997 | put_task_struct_many(task, nr); | |
1998 | } | |
1999 | ||
216578e5 | 2000 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2001 | { |
51a4cc11 | 2002 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2003 | |
216578e5 | 2004 | io_dismantle_req(req); |
7c660731 | 2005 | io_put_task(req->task, 1); |
e3bc8e9d | 2006 | |
3893f39f | 2007 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 2008 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
2009 | } |
2010 | ||
f2f87370 PB |
2011 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2012 | { | |
2013 | struct io_kiocb *nxt = req->link; | |
2014 | ||
2015 | req->link = nxt->link; | |
2016 | nxt->link = NULL; | |
2017 | } | |
2018 | ||
c9abd7ad | 2019 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 2020 | { |
a197f664 | 2021 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 2022 | struct io_kiocb *link; |
c9abd7ad PB |
2023 | bool cancelled = false; |
2024 | unsigned long flags; | |
7c86ffee | 2025 | |
c9abd7ad | 2026 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2027 | link = req->link; |
2028 | ||
900fad45 PB |
2029 | /* |
2030 | * Can happen if a linked timeout fired and link had been like | |
2031 | * req -> link t-out -> link t-out [-> ...] | |
2032 | */ | |
c9abd7ad PB |
2033 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
2034 | struct io_timeout_data *io = link->async_data; | |
2035 | int ret; | |
7c86ffee | 2036 | |
f2f87370 | 2037 | io_remove_next_linked(req); |
90cd7e42 | 2038 | link->timeout.head = NULL; |
c9abd7ad PB |
2039 | ret = hrtimer_try_to_cancel(&io->timer); |
2040 | if (ret != -1) { | |
2041 | io_cqring_fill_event(link, -ECANCELED); | |
2042 | io_commit_cqring(ctx); | |
2043 | cancelled = true; | |
2044 | } | |
2045 | } | |
7c86ffee | 2046 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2047 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2048 | |
c9abd7ad | 2049 | if (cancelled) { |
7c86ffee | 2050 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2051 | io_put_req(link); |
2052 | } | |
7c86ffee PB |
2053 | } |
2054 | ||
9e645e11 | 2055 | |
d148ca4b | 2056 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2057 | { |
f2f87370 | 2058 | struct io_kiocb *link, *nxt; |
2665abfd | 2059 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2060 | unsigned long flags; |
9e645e11 | 2061 | |
d148ca4b | 2062 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2063 | link = req->link; |
2064 | req->link = NULL; | |
9e645e11 | 2065 | |
f2f87370 PB |
2066 | while (link) { |
2067 | nxt = link->link; | |
2068 | link->link = NULL; | |
2665abfd | 2069 | |
f2f87370 | 2070 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2071 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2072 | |
2073 | /* | |
2074 | * It's ok to free under spinlock as they're not linked anymore, | |
2075 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2076 | * work.fs->lock. | |
2077 | */ | |
2078 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2079 | io_put_req_deferred(link, 2); | |
2080 | else | |
2081 | io_double_put_req(link); | |
f2f87370 | 2082 | link = nxt; |
9e645e11 | 2083 | } |
2665abfd | 2084 | io_commit_cqring(ctx); |
216578e5 | 2085 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2086 | |
2665abfd | 2087 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2088 | } |
2089 | ||
3fa5e0f3 | 2090 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2091 | { |
7c86ffee PB |
2092 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2093 | io_kill_linked_timeout(req); | |
944e58bf | 2094 | |
9e645e11 JA |
2095 | /* |
2096 | * If LINK is set, we have dependent requests in this chain. If we | |
2097 | * didn't fail this request, queue the first one up, moving any other | |
2098 | * dependencies to the next request. In case of failure, fail the rest | |
2099 | * of the chain. | |
2100 | */ | |
f2f87370 PB |
2101 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2102 | struct io_kiocb *nxt = req->link; | |
2103 | ||
2104 | req->link = NULL; | |
2105 | return nxt; | |
2106 | } | |
9b5f7bd9 PB |
2107 | io_fail_links(req); |
2108 | return NULL; | |
4d7dd462 | 2109 | } |
9e645e11 | 2110 | |
f2f87370 | 2111 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2112 | { |
f2f87370 | 2113 | if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT))) |
3fa5e0f3 PB |
2114 | return NULL; |
2115 | return __io_req_find_next(req); | |
2116 | } | |
2117 | ||
355fb9e2 | 2118 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
2119 | { |
2120 | struct task_struct *tsk = req->task; | |
2121 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2122 | enum task_work_notify_mode notify; |
2123 | int ret; | |
c2c4c83c | 2124 | |
6200b0ae JA |
2125 | if (tsk->flags & PF_EXITING) |
2126 | return -ESRCH; | |
2127 | ||
c2c4c83c | 2128 | /* |
0ba9c9ed JA |
2129 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2130 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2131 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2132 | * will do the job. | |
c2c4c83c | 2133 | */ |
91989c70 | 2134 | notify = TWA_NONE; |
355fb9e2 | 2135 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
2136 | notify = TWA_SIGNAL; |
2137 | ||
87c4311f | 2138 | ret = task_work_add(tsk, &req->task_work, notify); |
c2c4c83c JA |
2139 | if (!ret) |
2140 | wake_up_process(tsk); | |
0ba9c9ed | 2141 | |
c2c4c83c JA |
2142 | return ret; |
2143 | } | |
2144 | ||
eab30c4d PB |
2145 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
2146 | void (*cb)(struct callback_head *)) | |
2147 | { | |
2148 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); | |
2149 | ||
2150 | init_task_work(&req->task_work, cb); | |
2151 | task_work_add(tsk, &req->task_work, TWA_NONE); | |
2152 | wake_up_process(tsk); | |
2153 | } | |
2154 | ||
c40f6379 JA |
2155 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2156 | { | |
2157 | struct io_ring_ctx *ctx = req->ctx; | |
2158 | ||
2159 | spin_lock_irq(&ctx->completion_lock); | |
2160 | io_cqring_fill_event(req, error); | |
2161 | io_commit_cqring(ctx); | |
2162 | spin_unlock_irq(&ctx->completion_lock); | |
2163 | ||
2164 | io_cqring_ev_posted(ctx); | |
2165 | req_set_fail_links(req); | |
2166 | io_double_put_req(req); | |
2167 | } | |
2168 | ||
2169 | static void io_req_task_cancel(struct callback_head *cb) | |
2170 | { | |
2171 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2172 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2173 | |
2174 | __io_req_task_cancel(req, -ECANCELED); | |
87ceb6a6 | 2175 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2176 | } |
2177 | ||
2178 | static void __io_req_task_submit(struct io_kiocb *req) | |
2179 | { | |
2180 | struct io_ring_ctx *ctx = req->ctx; | |
2181 | ||
81b6d05c | 2182 | mutex_lock(&ctx->uring_lock); |
d9d05217 PB |
2183 | if (!ctx->sqo_dead && |
2184 | !__io_sq_thread_acquire_mm(ctx) && | |
2185 | !__io_sq_thread_acquire_files(ctx)) | |
c1379e24 | 2186 | __io_queue_sqe(req, NULL); |
81b6d05c | 2187 | else |
c40f6379 | 2188 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2189 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2190 | } |
2191 | ||
2192 | static void io_req_task_submit(struct callback_head *cb) | |
2193 | { | |
2194 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 2195 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2196 | |
2197 | __io_req_task_submit(req); | |
6d816e08 | 2198 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2199 | } |
2200 | ||
2201 | static void io_req_task_queue(struct io_kiocb *req) | |
2202 | { | |
c40f6379 JA |
2203 | int ret; |
2204 | ||
2205 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 2206 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 2207 | |
355fb9e2 | 2208 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2209 | if (unlikely(ret)) |
2210 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
c40f6379 JA |
2211 | } |
2212 | ||
f2f87370 | 2213 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2214 | { |
9b5f7bd9 | 2215 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2216 | |
2217 | if (nxt) | |
906a8c3f | 2218 | io_req_task_queue(nxt); |
c69f8dbe JL |
2219 | } |
2220 | ||
c3524383 | 2221 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2222 | { |
c3524383 PB |
2223 | io_queue_next(req); |
2224 | __io_free_req(req); | |
2225 | } | |
8766dd51 | 2226 | |
2d6500d4 PB |
2227 | struct req_batch { |
2228 | void *reqs[IO_IOPOLL_BATCH]; | |
2229 | int to_free; | |
9ae72463 | 2230 | int ctx_refs; |
7a743e22 | 2231 | |
5af1d13e PB |
2232 | struct task_struct *task; |
2233 | int task_refs; | |
2d6500d4 PB |
2234 | }; |
2235 | ||
5af1d13e PB |
2236 | static inline void io_init_req_batch(struct req_batch *rb) |
2237 | { | |
2238 | rb->to_free = 0; | |
2239 | rb->task_refs = 0; | |
9ae72463 | 2240 | rb->ctx_refs = 0; |
5af1d13e PB |
2241 | rb->task = NULL; |
2242 | } | |
2243 | ||
2d6500d4 PB |
2244 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
2245 | struct req_batch *rb) | |
2246 | { | |
2247 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
2d6500d4 PB |
2248 | rb->to_free = 0; |
2249 | } | |
2250 | ||
2251 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
2252 | struct req_batch *rb) | |
2253 | { | |
2254 | if (rb->to_free) | |
2255 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e | 2256 | if (rb->task) { |
7c660731 | 2257 | io_put_task(rb->task, rb->task_refs); |
5af1d13e PB |
2258 | rb->task = NULL; |
2259 | } | |
9ae72463 PB |
2260 | if (rb->ctx_refs) |
2261 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2262 | } |
2263 | ||
2264 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
2265 | { | |
f2f87370 | 2266 | io_queue_next(req); |
2d6500d4 | 2267 | |
e3bc8e9d | 2268 | if (req->task != rb->task) { |
7c660731 PB |
2269 | if (rb->task) |
2270 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2271 | rb->task = req->task; |
2272 | rb->task_refs = 0; | |
5af1d13e | 2273 | } |
e3bc8e9d | 2274 | rb->task_refs++; |
9ae72463 | 2275 | rb->ctx_refs++; |
5af1d13e | 2276 | |
4edf20f9 | 2277 | io_dismantle_req(req); |
2d6500d4 PB |
2278 | rb->reqs[rb->to_free++] = req; |
2279 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
2280 | __io_req_free_batch_flush(req->ctx, rb); | |
7a743e22 PB |
2281 | } |
2282 | ||
905c172f PB |
2283 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2284 | struct io_ring_ctx *ctx) | |
2285 | { | |
2286 | int i, nr = cs->nr; | |
2287 | struct io_kiocb *req; | |
2288 | struct req_batch rb; | |
2289 | ||
2290 | io_init_req_batch(&rb); | |
2291 | spin_lock_irq(&ctx->completion_lock); | |
2292 | for (i = 0; i < nr; i++) { | |
2293 | req = cs->reqs[i]; | |
2294 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2295 | } | |
2296 | io_commit_cqring(ctx); | |
2297 | spin_unlock_irq(&ctx->completion_lock); | |
2298 | ||
2299 | io_cqring_ev_posted(ctx); | |
2300 | for (i = 0; i < nr; i++) { | |
2301 | req = cs->reqs[i]; | |
2302 | ||
2303 | /* submission and completion refs */ | |
2304 | if (refcount_sub_and_test(2, &req->refs)) | |
2305 | io_req_free_batch(&rb, req); | |
2306 | } | |
2307 | ||
2308 | io_req_free_batch_finish(ctx, &rb); | |
2309 | cs->nr = 0; | |
2310 | } | |
2311 | ||
ba816ad6 JA |
2312 | /* |
2313 | * Drop reference to request, return next in chain (if there is one) if this | |
2314 | * was the last reference to this request. | |
2315 | */ | |
9b5f7bd9 | 2316 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2317 | { |
9b5f7bd9 PB |
2318 | struct io_kiocb *nxt = NULL; |
2319 | ||
2a44f467 | 2320 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2321 | nxt = io_req_find_next(req); |
4d7dd462 | 2322 | __io_free_req(req); |
2a44f467 | 2323 | } |
9b5f7bd9 | 2324 | return nxt; |
2b188cc1 JA |
2325 | } |
2326 | ||
e65ef56d JA |
2327 | static void io_put_req(struct io_kiocb *req) |
2328 | { | |
2329 | if (refcount_dec_and_test(&req->refs)) | |
2330 | io_free_req(req); | |
2b188cc1 JA |
2331 | } |
2332 | ||
216578e5 PB |
2333 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2334 | { | |
2335 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2336 | ||
2337 | io_free_req(req); | |
2338 | } | |
2339 | ||
2340 | static void io_free_req_deferred(struct io_kiocb *req) | |
2341 | { | |
2342 | int ret; | |
2343 | ||
2344 | init_task_work(&req->task_work, io_put_req_deferred_cb); | |
355fb9e2 | 2345 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2346 | if (unlikely(ret)) |
2347 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2348 | } |
2349 | ||
2350 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2351 | { | |
2352 | if (refcount_sub_and_test(refs, &req->refs)) | |
2353 | io_free_req_deferred(req); | |
2354 | } | |
2355 | ||
978db57e JA |
2356 | static void io_double_put_req(struct io_kiocb *req) |
2357 | { | |
2358 | /* drop both submit and complete references */ | |
2359 | if (refcount_sub_and_test(2, &req->refs)) | |
2360 | io_free_req(req); | |
2361 | } | |
2362 | ||
6c503150 | 2363 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2364 | { |
2365 | /* See comment at the top of this file */ | |
2366 | smp_rmb(); | |
e23de15f | 2367 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2368 | } |
2369 | ||
fb5ccc98 PB |
2370 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2371 | { | |
2372 | struct io_rings *rings = ctx->rings; | |
2373 | ||
2374 | /* make sure SQ entry isn't read before tail */ | |
2375 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2376 | } | |
2377 | ||
8ff069bf | 2378 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2379 | { |
8ff069bf | 2380 | unsigned int cflags; |
e94f141b | 2381 | |
bcda7baa JA |
2382 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2383 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2384 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2385 | kfree(kbuf); |
2386 | return cflags; | |
e94f141b JA |
2387 | } |
2388 | ||
8ff069bf | 2389 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2390 | { |
4d954c25 | 2391 | struct io_buffer *kbuf; |
bcda7baa | 2392 | |
4d954c25 | 2393 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2394 | return io_put_kbuf(req, kbuf); |
2395 | } | |
2396 | ||
4c6e277c JA |
2397 | static inline bool io_run_task_work(void) |
2398 | { | |
6200b0ae JA |
2399 | /* |
2400 | * Not safe to run on exiting task, and the task_work handling will | |
2401 | * not add work to such a task. | |
2402 | */ | |
2403 | if (unlikely(current->flags & PF_EXITING)) | |
2404 | return false; | |
4c6e277c JA |
2405 | if (current->task_works) { |
2406 | __set_current_state(TASK_RUNNING); | |
2407 | task_work_run(); | |
2408 | return true; | |
2409 | } | |
2410 | ||
2411 | return false; | |
bcda7baa JA |
2412 | } |
2413 | ||
bbde017a XW |
2414 | static void io_iopoll_queue(struct list_head *again) |
2415 | { | |
2416 | struct io_kiocb *req; | |
2417 | ||
2418 | do { | |
d21ffe7e PB |
2419 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2420 | list_del(&req->inflight_entry); | |
889fca73 | 2421 | __io_complete_rw(req, -EAGAIN, 0, 0); |
bbde017a XW |
2422 | } while (!list_empty(again)); |
2423 | } | |
2424 | ||
def596e9 JA |
2425 | /* |
2426 | * Find and free completed poll iocbs | |
2427 | */ | |
2428 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2429 | struct list_head *done) | |
2430 | { | |
8237e045 | 2431 | struct req_batch rb; |
def596e9 | 2432 | struct io_kiocb *req; |
bbde017a XW |
2433 | LIST_HEAD(again); |
2434 | ||
2435 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2436 | smp_rmb(); | |
def596e9 | 2437 | |
5af1d13e | 2438 | io_init_req_batch(&rb); |
def596e9 | 2439 | while (!list_empty(done)) { |
bcda7baa JA |
2440 | int cflags = 0; |
2441 | ||
d21ffe7e | 2442 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a | 2443 | if (READ_ONCE(req->result) == -EAGAIN) { |
56450c20 | 2444 | req->result = 0; |
bbde017a | 2445 | req->iopoll_completed = 0; |
d21ffe7e | 2446 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2447 | continue; |
2448 | } | |
d21ffe7e | 2449 | list_del(&req->inflight_entry); |
def596e9 | 2450 | |
bcda7baa | 2451 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2452 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2453 | |
2454 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2455 | (*nr_events)++; |
2456 | ||
c3524383 | 2457 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2458 | io_req_free_batch(&rb, req); |
def596e9 | 2459 | } |
def596e9 | 2460 | |
09bb8394 | 2461 | io_commit_cqring(ctx); |
80c18e4a | 2462 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2463 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2464 | |
bbde017a XW |
2465 | if (!list_empty(&again)) |
2466 | io_iopoll_queue(&again); | |
581f9810 BM |
2467 | } |
2468 | ||
def596e9 JA |
2469 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2470 | long min) | |
2471 | { | |
2472 | struct io_kiocb *req, *tmp; | |
2473 | LIST_HEAD(done); | |
2474 | bool spin; | |
2475 | int ret; | |
2476 | ||
2477 | /* | |
2478 | * Only spin for completions if we don't have multiple devices hanging | |
2479 | * off our complete list, and we're under the requested amount. | |
2480 | */ | |
2481 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2482 | ||
2483 | ret = 0; | |
d21ffe7e | 2484 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2485 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2486 | |
2487 | /* | |
581f9810 BM |
2488 | * Move completed and retryable entries to our local lists. |
2489 | * If we find a request that requires polling, break out | |
2490 | * and complete those lists first, if we have entries there. | |
def596e9 | 2491 | */ |
65a6543d | 2492 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2493 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2494 | continue; |
2495 | } | |
2496 | if (!list_empty(&done)) | |
2497 | break; | |
2498 | ||
2499 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2500 | if (ret < 0) | |
2501 | break; | |
2502 | ||
3aadc23e PB |
2503 | /* iopoll may have completed current req */ |
2504 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2505 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2506 | |
def596e9 JA |
2507 | if (ret && spin) |
2508 | spin = false; | |
2509 | ret = 0; | |
2510 | } | |
2511 | ||
2512 | if (!list_empty(&done)) | |
2513 | io_iopoll_complete(ctx, nr_events, &done); | |
2514 | ||
2515 | return ret; | |
2516 | } | |
2517 | ||
2518 | /* | |
d195a66e | 2519 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2520 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2521 | * as a non-spinning completion check. | |
2522 | */ | |
2523 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2524 | long min) | |
2525 | { | |
540e32a0 | 2526 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2527 | int ret; |
2528 | ||
2529 | ret = io_do_iopoll(ctx, nr_events, min); | |
2530 | if (ret < 0) | |
2531 | return ret; | |
eba0a4dd | 2532 | if (*nr_events >= min) |
def596e9 JA |
2533 | return 0; |
2534 | } | |
2535 | ||
2536 | return 1; | |
2537 | } | |
2538 | ||
2539 | /* | |
2540 | * We can't just wait for polled events to come to us, we have to actively | |
2541 | * find and complete them. | |
2542 | */ | |
b2edc0a7 | 2543 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2544 | { |
2545 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2546 | return; | |
2547 | ||
2548 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2549 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2550 | unsigned int nr_events = 0; |
2551 | ||
b2edc0a7 | 2552 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2553 | |
b2edc0a7 PB |
2554 | /* let it sleep and repeat later if can't complete a request */ |
2555 | if (nr_events == 0) | |
2556 | break; | |
08f5439f JA |
2557 | /* |
2558 | * Ensure we allow local-to-the-cpu processing to take place, | |
2559 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2560 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2561 | */ |
3fcee5a6 PB |
2562 | if (need_resched()) { |
2563 | mutex_unlock(&ctx->uring_lock); | |
2564 | cond_resched(); | |
2565 | mutex_lock(&ctx->uring_lock); | |
2566 | } | |
def596e9 JA |
2567 | } |
2568 | mutex_unlock(&ctx->uring_lock); | |
2569 | } | |
2570 | ||
7668b92a | 2571 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2572 | { |
7668b92a | 2573 | unsigned int nr_events = 0; |
2b2ed975 | 2574 | int iters = 0, ret = 0; |
500f9fba | 2575 | |
c7849be9 XW |
2576 | /* |
2577 | * We disallow the app entering submit/complete with polling, but we | |
2578 | * still need to lock the ring to prevent racing with polled issue | |
2579 | * that got punted to a workqueue. | |
2580 | */ | |
2581 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2582 | do { |
a3a0e43f JA |
2583 | /* |
2584 | * Don't enter poll loop if we already have events pending. | |
2585 | * If we do, we can potentially be spinning for commands that | |
2586 | * already triggered a CQE (eg in error). | |
2587 | */ | |
6c503150 PB |
2588 | if (test_bit(0, &ctx->cq_check_overflow)) |
2589 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2590 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2591 | break; |
2592 | ||
500f9fba JA |
2593 | /* |
2594 | * If a submit got punted to a workqueue, we can have the | |
2595 | * application entering polling for a command before it gets | |
2596 | * issued. That app will hold the uring_lock for the duration | |
2597 | * of the poll right here, so we need to take a breather every | |
2598 | * now and then to ensure that the issue has a chance to add | |
2599 | * the poll to the issued list. Otherwise we can spin here | |
2600 | * forever, while the workqueue is stuck trying to acquire the | |
2601 | * very same mutex. | |
2602 | */ | |
2603 | if (!(++iters & 7)) { | |
2604 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2605 | io_run_task_work(); |
500f9fba JA |
2606 | mutex_lock(&ctx->uring_lock); |
2607 | } | |
2608 | ||
7668b92a | 2609 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2610 | if (ret <= 0) |
2611 | break; | |
2612 | ret = 0; | |
7668b92a | 2613 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2614 | |
500f9fba | 2615 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2616 | return ret; |
2617 | } | |
2618 | ||
491381ce | 2619 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2620 | { |
491381ce JA |
2621 | /* |
2622 | * Tell lockdep we inherited freeze protection from submission | |
2623 | * thread. | |
2624 | */ | |
2625 | if (req->flags & REQ_F_ISREG) { | |
2626 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2627 | |
491381ce | 2628 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2629 | } |
491381ce | 2630 | file_end_write(req->file); |
2b188cc1 JA |
2631 | } |
2632 | ||
a1d7c393 | 2633 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
889fca73 | 2634 | unsigned int issue_flags) |
2b188cc1 | 2635 | { |
9adbd45d | 2636 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2637 | int cflags = 0; |
2b188cc1 | 2638 | |
491381ce JA |
2639 | if (kiocb->ki_flags & IOCB_WRITE) |
2640 | kiocb_end_write(req); | |
2b188cc1 | 2641 | |
4e88d6e7 JA |
2642 | if (res != req->result) |
2643 | req_set_fail_links(req); | |
bcda7baa | 2644 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2645 | cflags = io_put_rw_kbuf(req); |
889fca73 | 2646 | __io_req_complete(req, issue_flags, res, cflags); |
ba816ad6 JA |
2647 | } |
2648 | ||
b63534c4 | 2649 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2650 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2651 | { |
2652 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
847595de | 2653 | int rw, ret = -ECANCELED; |
b63534c4 | 2654 | struct iov_iter iter; |
b63534c4 | 2655 | |
dc2a6e9a PB |
2656 | /* already prepared */ |
2657 | if (req->async_data) | |
2658 | return true; | |
b63534c4 JA |
2659 | |
2660 | switch (req->opcode) { | |
2661 | case IORING_OP_READV: | |
2662 | case IORING_OP_READ_FIXED: | |
2663 | case IORING_OP_READ: | |
2664 | rw = READ; | |
2665 | break; | |
2666 | case IORING_OP_WRITEV: | |
2667 | case IORING_OP_WRITE_FIXED: | |
2668 | case IORING_OP_WRITE: | |
2669 | rw = WRITE; | |
2670 | break; | |
2671 | default: | |
2672 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2673 | req->opcode); | |
dc2a6e9a | 2674 | return false; |
b63534c4 JA |
2675 | } |
2676 | ||
dc2a6e9a PB |
2677 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2678 | if (ret < 0) | |
2679 | return false; | |
6bf985dc | 2680 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2681 | } |
b63534c4 JA |
2682 | #endif |
2683 | ||
2684 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2685 | { | |
2686 | #ifdef CONFIG_BLOCK | |
bf6182b6 | 2687 | umode_t mode; |
b63534c4 JA |
2688 | int ret; |
2689 | ||
bf6182b6 | 2690 | if (res != -EAGAIN && res != -EOPNOTSUPP) |
355afaeb | 2691 | return false; |
bf6182b6 | 2692 | mode = file_inode(req->file)->i_mode; |
75c668cd PB |
2693 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2694 | return false; | |
2695 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) | |
b63534c4 JA |
2696 | return false; |
2697 | ||
55e6ac1e PB |
2698 | lockdep_assert_held(&req->ctx->uring_lock); |
2699 | ||
28cea78a | 2700 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2701 | |
dc2a6e9a | 2702 | if (!ret && io_resubmit_prep(req)) { |
fdee946d JA |
2703 | refcount_inc(&req->refs); |
2704 | io_queue_async_work(req); | |
b63534c4 | 2705 | return true; |
fdee946d | 2706 | } |
dc2a6e9a | 2707 | req_set_fail_links(req); |
b63534c4 JA |
2708 | #endif |
2709 | return false; | |
2710 | } | |
2711 | ||
a1d7c393 | 2712 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2713 | unsigned int issue_flags) |
a1d7c393 JA |
2714 | { |
2715 | if (!io_rw_reissue(req, res)) | |
889fca73 | 2716 | io_complete_rw_common(&req->rw.kiocb, res, issue_flags); |
ba816ad6 JA |
2717 | } |
2718 | ||
2719 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2720 | { | |
9adbd45d | 2721 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2722 | |
889fca73 | 2723 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2724 | } |
2725 | ||
def596e9 JA |
2726 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2727 | { | |
9adbd45d | 2728 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2729 | |
491381ce JA |
2730 | if (kiocb->ki_flags & IOCB_WRITE) |
2731 | kiocb_end_write(req); | |
def596e9 | 2732 | |
2d7d6792 | 2733 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2734 | req_set_fail_links(req); |
bbde017a XW |
2735 | |
2736 | WRITE_ONCE(req->result, res); | |
2737 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2738 | smp_wmb(); |
2739 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2740 | } |
2741 | ||
2742 | /* | |
2743 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2744 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2745 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2746 | * accessing the kiocb cookie. | |
2747 | */ | |
2e9dbe90 | 2748 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2749 | { |
2750 | struct io_ring_ctx *ctx = req->ctx; | |
2751 | ||
2752 | /* | |
2753 | * Track whether we have multiple files in our lists. This will impact | |
2754 | * how we do polling eventually, not spinning if we're on potentially | |
2755 | * different devices. | |
2756 | */ | |
540e32a0 | 2757 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2758 | ctx->poll_multi_file = false; |
2759 | } else if (!ctx->poll_multi_file) { | |
2760 | struct io_kiocb *list_req; | |
2761 | ||
540e32a0 | 2762 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2763 | inflight_entry); |
9adbd45d | 2764 | if (list_req->file != req->file) |
def596e9 JA |
2765 | ctx->poll_multi_file = true; |
2766 | } | |
2767 | ||
2768 | /* | |
2769 | * For fast devices, IO may have already completed. If it has, add | |
2770 | * it to the front so we find it first. | |
2771 | */ | |
65a6543d | 2772 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2773 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2774 | else |
d21ffe7e | 2775 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2776 | |
2e9dbe90 XW |
2777 | /* |
2778 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2779 | * task context or in io worker task context. If current task context is | |
2780 | * sq thread, we don't need to check whether should wake up sq thread. | |
2781 | */ | |
2782 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2783 | wq_has_sleeper(&ctx->sq_data->wait)) |
2784 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2785 | } |
2786 | ||
9f13c35b PB |
2787 | static inline void io_state_file_put(struct io_submit_state *state) |
2788 | { | |
02b23a9a PB |
2789 | if (state->file_refs) { |
2790 | fput_many(state->file, state->file_refs); | |
2791 | state->file_refs = 0; | |
2792 | } | |
9a56a232 JA |
2793 | } |
2794 | ||
2795 | /* | |
2796 | * Get as many references to a file as we have IOs left in this submission, | |
2797 | * assuming most submissions are for one file, or at least that each file | |
2798 | * has more than one submission. | |
2799 | */ | |
8da11c19 | 2800 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2801 | { |
2802 | if (!state) | |
2803 | return fget(fd); | |
2804 | ||
6e1271e6 | 2805 | if (state->file_refs) { |
9a56a232 | 2806 | if (state->fd == fd) { |
6e1271e6 | 2807 | state->file_refs--; |
9a56a232 JA |
2808 | return state->file; |
2809 | } | |
02b23a9a | 2810 | io_state_file_put(state); |
9a56a232 JA |
2811 | } |
2812 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2813 | if (unlikely(!state->file)) |
9a56a232 JA |
2814 | return NULL; |
2815 | ||
2816 | state->fd = fd; | |
6e1271e6 | 2817 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2818 | return state->file; |
2819 | } | |
2820 | ||
4503b767 JA |
2821 | static bool io_bdev_nowait(struct block_device *bdev) |
2822 | { | |
9ba0d0c8 | 2823 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2824 | } |
2825 | ||
2b188cc1 JA |
2826 | /* |
2827 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2828 | * any file. For now, just ensure that anything potentially problematic is done | |
2829 | * inline. | |
2830 | */ | |
af197f50 | 2831 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2832 | { |
2833 | umode_t mode = file_inode(file)->i_mode; | |
2834 | ||
4503b767 | 2835 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2836 | if (IS_ENABLED(CONFIG_BLOCK) && |
2837 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2838 | return true; |
2839 | return false; | |
2840 | } | |
2841 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2842 | return true; |
4503b767 | 2843 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2844 | if (IS_ENABLED(CONFIG_BLOCK) && |
2845 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2846 | file->f_op != &io_uring_fops) |
2847 | return true; | |
2848 | return false; | |
2849 | } | |
2b188cc1 | 2850 | |
c5b85625 JA |
2851 | /* any ->read/write should understand O_NONBLOCK */ |
2852 | if (file->f_flags & O_NONBLOCK) | |
2853 | return true; | |
2854 | ||
af197f50 JA |
2855 | if (!(file->f_mode & FMODE_NOWAIT)) |
2856 | return false; | |
2857 | ||
2858 | if (rw == READ) | |
2859 | return file->f_op->read_iter != NULL; | |
2860 | ||
2861 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2862 | } |
2863 | ||
a88fc400 | 2864 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2865 | { |
def596e9 | 2866 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2867 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2868 | struct file *file = req->file; |
09bb8394 JA |
2869 | unsigned ioprio; |
2870 | int ret; | |
2b188cc1 | 2871 | |
75c668cd | 2872 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2873 | req->flags |= REQ_F_ISREG; |
2874 | ||
2b188cc1 | 2875 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2876 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2877 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2878 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2879 | } |
2b188cc1 | 2880 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2881 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2882 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2883 | if (unlikely(ret)) | |
2884 | return ret; | |
2b188cc1 | 2885 | |
75c668cd PB |
2886 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2887 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2888 | req->flags |= REQ_F_NOWAIT; | |
2889 | ||
2b188cc1 JA |
2890 | ioprio = READ_ONCE(sqe->ioprio); |
2891 | if (ioprio) { | |
2892 | ret = ioprio_check_cap(ioprio); | |
2893 | if (ret) | |
09bb8394 | 2894 | return ret; |
2b188cc1 JA |
2895 | |
2896 | kiocb->ki_ioprio = ioprio; | |
2897 | } else | |
2898 | kiocb->ki_ioprio = get_current_ioprio(); | |
2899 | ||
def596e9 | 2900 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2901 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2902 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2903 | return -EOPNOTSUPP; |
2b188cc1 | 2904 | |
def596e9 JA |
2905 | kiocb->ki_flags |= IOCB_HIPRI; |
2906 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2907 | req->iopoll_completed = 0; |
def596e9 | 2908 | } else { |
09bb8394 JA |
2909 | if (kiocb->ki_flags & IOCB_HIPRI) |
2910 | return -EINVAL; | |
def596e9 JA |
2911 | kiocb->ki_complete = io_complete_rw; |
2912 | } | |
9adbd45d | 2913 | |
3529d8c2 JA |
2914 | req->rw.addr = READ_ONCE(sqe->addr); |
2915 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2916 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2917 | return 0; |
2b188cc1 JA |
2918 | } |
2919 | ||
2920 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2921 | { | |
2922 | switch (ret) { | |
2923 | case -EIOCBQUEUED: | |
2924 | break; | |
2925 | case -ERESTARTSYS: | |
2926 | case -ERESTARTNOINTR: | |
2927 | case -ERESTARTNOHAND: | |
2928 | case -ERESTART_RESTARTBLOCK: | |
2929 | /* | |
2930 | * We can't just restart the syscall, since previously | |
2931 | * submitted sqes may already be in progress. Just fail this | |
2932 | * IO with EINTR. | |
2933 | */ | |
2934 | ret = -EINTR; | |
df561f66 | 2935 | fallthrough; |
2b188cc1 JA |
2936 | default: |
2937 | kiocb->ki_complete(kiocb, ret, 0); | |
2938 | } | |
2939 | } | |
2940 | ||
a1d7c393 | 2941 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2942 | unsigned int issue_flags) |
ba816ad6 | 2943 | { |
ba04291e | 2944 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2945 | struct io_async_rw *io = req->async_data; |
ba04291e | 2946 | |
227c0c96 | 2947 | /* add previously done IO, if any */ |
e8c2bc1f | 2948 | if (io && io->bytes_done > 0) { |
227c0c96 | 2949 | if (ret < 0) |
e8c2bc1f | 2950 | ret = io->bytes_done; |
227c0c96 | 2951 | else |
e8c2bc1f | 2952 | ret += io->bytes_done; |
227c0c96 JA |
2953 | } |
2954 | ||
ba04291e JA |
2955 | if (req->flags & REQ_F_CUR_POS) |
2956 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2957 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2958 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2959 | else |
2960 | io_rw_done(kiocb, ret); | |
2961 | } | |
2962 | ||
847595de | 2963 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2964 | { |
9adbd45d JA |
2965 | struct io_ring_ctx *ctx = req->ctx; |
2966 | size_t len = req->rw.len; | |
edafccee | 2967 | struct io_mapped_ubuf *imu; |
4be1c615 | 2968 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2969 | size_t offset; |
2970 | u64 buf_addr; | |
2971 | ||
edafccee JA |
2972 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2973 | return -EFAULT; | |
edafccee JA |
2974 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2975 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2976 | buf_addr = req->rw.addr; |
edafccee JA |
2977 | |
2978 | /* overflow */ | |
2979 | if (buf_addr + len < buf_addr) | |
2980 | return -EFAULT; | |
2981 | /* not inside the mapped region */ | |
2982 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2983 | return -EFAULT; | |
2984 | ||
2985 | /* | |
2986 | * May not be a start of buffer, set size appropriately | |
2987 | * and advance us to the beginning. | |
2988 | */ | |
2989 | offset = buf_addr - imu->ubuf; | |
2990 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2991 | |
2992 | if (offset) { | |
2993 | /* | |
2994 | * Don't use iov_iter_advance() here, as it's really slow for | |
2995 | * using the latter parts of a big fixed buffer - it iterates | |
2996 | * over each segment manually. We can cheat a bit here, because | |
2997 | * we know that: | |
2998 | * | |
2999 | * 1) it's a BVEC iter, we set it up | |
3000 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3001 | * first and last bvec | |
3002 | * | |
3003 | * So just find our index, and adjust the iterator afterwards. | |
3004 | * If the offset is within the first bvec (or the whole first | |
3005 | * bvec, just use iov_iter_advance(). This makes it easier | |
3006 | * since we can just skip the first segment, which may not | |
3007 | * be PAGE_SIZE aligned. | |
3008 | */ | |
3009 | const struct bio_vec *bvec = imu->bvec; | |
3010 | ||
3011 | if (offset <= bvec->bv_len) { | |
3012 | iov_iter_advance(iter, offset); | |
3013 | } else { | |
3014 | unsigned long seg_skip; | |
3015 | ||
3016 | /* skip first vec */ | |
3017 | offset -= bvec->bv_len; | |
3018 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3019 | ||
3020 | iter->bvec = bvec + seg_skip; | |
3021 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3022 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3023 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3024 | } |
3025 | } | |
3026 | ||
847595de | 3027 | return 0; |
edafccee JA |
3028 | } |
3029 | ||
bcda7baa JA |
3030 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3031 | { | |
3032 | if (needs_lock) | |
3033 | mutex_unlock(&ctx->uring_lock); | |
3034 | } | |
3035 | ||
3036 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3037 | { | |
3038 | /* | |
3039 | * "Normal" inline submissions always hold the uring_lock, since we | |
3040 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3041 | * The only exception is when we've detached the request and issue it | |
3042 | * from an async worker thread, grab the lock for that case. | |
3043 | */ | |
3044 | if (needs_lock) | |
3045 | mutex_lock(&ctx->uring_lock); | |
3046 | } | |
3047 | ||
3048 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3049 | int bgid, struct io_buffer *kbuf, | |
3050 | bool needs_lock) | |
3051 | { | |
3052 | struct io_buffer *head; | |
3053 | ||
3054 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3055 | return kbuf; | |
3056 | ||
3057 | io_ring_submit_lock(req->ctx, needs_lock); | |
3058 | ||
3059 | lockdep_assert_held(&req->ctx->uring_lock); | |
3060 | ||
3061 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3062 | if (head) { | |
3063 | if (!list_empty(&head->list)) { | |
3064 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3065 | list); | |
3066 | list_del(&kbuf->list); | |
3067 | } else { | |
3068 | kbuf = head; | |
3069 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3070 | } | |
3071 | if (*len > kbuf->len) | |
3072 | *len = kbuf->len; | |
3073 | } else { | |
3074 | kbuf = ERR_PTR(-ENOBUFS); | |
3075 | } | |
3076 | ||
3077 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3078 | ||
3079 | return kbuf; | |
3080 | } | |
3081 | ||
4d954c25 JA |
3082 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3083 | bool needs_lock) | |
3084 | { | |
3085 | struct io_buffer *kbuf; | |
4f4eeba8 | 3086 | u16 bgid; |
4d954c25 JA |
3087 | |
3088 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3089 | bgid = req->buf_index; |
4d954c25 JA |
3090 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3091 | if (IS_ERR(kbuf)) | |
3092 | return kbuf; | |
3093 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3094 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3095 | return u64_to_user_ptr(kbuf->addr); | |
3096 | } | |
3097 | ||
3098 | #ifdef CONFIG_COMPAT | |
3099 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3100 | bool needs_lock) | |
3101 | { | |
3102 | struct compat_iovec __user *uiov; | |
3103 | compat_ssize_t clen; | |
3104 | void __user *buf; | |
3105 | ssize_t len; | |
3106 | ||
3107 | uiov = u64_to_user_ptr(req->rw.addr); | |
3108 | if (!access_ok(uiov, sizeof(*uiov))) | |
3109 | return -EFAULT; | |
3110 | if (__get_user(clen, &uiov->iov_len)) | |
3111 | return -EFAULT; | |
3112 | if (clen < 0) | |
3113 | return -EINVAL; | |
3114 | ||
3115 | len = clen; | |
3116 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3117 | if (IS_ERR(buf)) | |
3118 | return PTR_ERR(buf); | |
3119 | iov[0].iov_base = buf; | |
3120 | iov[0].iov_len = (compat_size_t) len; | |
3121 | return 0; | |
3122 | } | |
3123 | #endif | |
3124 | ||
3125 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3126 | bool needs_lock) | |
3127 | { | |
3128 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3129 | void __user *buf; | |
3130 | ssize_t len; | |
3131 | ||
3132 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3133 | return -EFAULT; | |
3134 | ||
3135 | len = iov[0].iov_len; | |
3136 | if (len < 0) | |
3137 | return -EINVAL; | |
3138 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3139 | if (IS_ERR(buf)) | |
3140 | return PTR_ERR(buf); | |
3141 | iov[0].iov_base = buf; | |
3142 | iov[0].iov_len = len; | |
3143 | return 0; | |
3144 | } | |
3145 | ||
3146 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3147 | bool needs_lock) | |
3148 | { | |
dddb3e26 JA |
3149 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3150 | struct io_buffer *kbuf; | |
3151 | ||
3152 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3153 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3154 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3155 | return 0; |
dddb3e26 | 3156 | } |
dd201662 | 3157 | if (req->rw.len != 1) |
4d954c25 JA |
3158 | return -EINVAL; |
3159 | ||
3160 | #ifdef CONFIG_COMPAT | |
3161 | if (req->ctx->compat) | |
3162 | return io_compat_import(req, iov, needs_lock); | |
3163 | #endif | |
3164 | ||
3165 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3166 | } | |
3167 | ||
847595de PB |
3168 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3169 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3170 | { |
9adbd45d JA |
3171 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3172 | size_t sqe_len = req->rw.len; | |
847595de | 3173 | u8 opcode = req->opcode; |
4d954c25 | 3174 | ssize_t ret; |
edafccee | 3175 | |
7d009165 | 3176 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3177 | *iovec = NULL; |
9adbd45d | 3178 | return io_import_fixed(req, rw, iter); |
edafccee | 3179 | } |
2b188cc1 | 3180 | |
bcda7baa | 3181 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3182 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3183 | return -EINVAL; |
3184 | ||
3a6820f2 | 3185 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3186 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3187 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3188 | if (IS_ERR(buf)) |
4d954c25 | 3189 | return PTR_ERR(buf); |
3f9d6441 | 3190 | req->rw.len = sqe_len; |
bcda7baa JA |
3191 | } |
3192 | ||
3a6820f2 JA |
3193 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3194 | *iovec = NULL; | |
10fc72e4 | 3195 | return ret; |
3a6820f2 JA |
3196 | } |
3197 | ||
4d954c25 JA |
3198 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3199 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3200 | if (!ret) |
3201 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3202 | *iovec = NULL; |
3203 | return ret; | |
3204 | } | |
3205 | ||
89cd35c5 CH |
3206 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3207 | req->ctx->compat); | |
2b188cc1 JA |
3208 | } |
3209 | ||
0fef9483 JA |
3210 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3211 | { | |
5b09e37e | 3212 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3213 | } |
3214 | ||
31b51510 | 3215 | /* |
32960613 JA |
3216 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3217 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3218 | */ |
4017eb91 | 3219 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3220 | { |
4017eb91 JA |
3221 | struct kiocb *kiocb = &req->rw.kiocb; |
3222 | struct file *file = req->file; | |
32960613 JA |
3223 | ssize_t ret = 0; |
3224 | ||
3225 | /* | |
3226 | * Don't support polled IO through this interface, and we can't | |
3227 | * support non-blocking either. For the latter, this just causes | |
3228 | * the kiocb to be handled from an async context. | |
3229 | */ | |
3230 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3231 | return -EOPNOTSUPP; | |
3232 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3233 | return -EAGAIN; | |
3234 | ||
3235 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3236 | struct iovec iovec; |
32960613 JA |
3237 | ssize_t nr; |
3238 | ||
311ae9e1 PB |
3239 | if (!iov_iter_is_bvec(iter)) { |
3240 | iovec = iov_iter_iovec(iter); | |
3241 | } else { | |
4017eb91 JA |
3242 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3243 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3244 | } |
3245 | ||
32960613 JA |
3246 | if (rw == READ) { |
3247 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3248 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3249 | } else { |
3250 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3251 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3252 | } |
3253 | ||
3254 | if (nr < 0) { | |
3255 | if (!ret) | |
3256 | ret = nr; | |
3257 | break; | |
3258 | } | |
3259 | ret += nr; | |
3260 | if (nr != iovec.iov_len) | |
3261 | break; | |
4017eb91 JA |
3262 | req->rw.len -= nr; |
3263 | req->rw.addr += nr; | |
32960613 JA |
3264 | iov_iter_advance(iter, nr); |
3265 | } | |
3266 | ||
3267 | return ret; | |
3268 | } | |
3269 | ||
ff6165b2 JA |
3270 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3271 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3272 | { |
e8c2bc1f | 3273 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3274 | |
ff6165b2 | 3275 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3276 | rw->free_iovec = iovec; |
227c0c96 | 3277 | rw->bytes_done = 0; |
ff6165b2 | 3278 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3279 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3280 | return; |
b64e3444 | 3281 | if (!iovec) { |
ff6165b2 JA |
3282 | unsigned iov_off = 0; |
3283 | ||
3284 | rw->iter.iov = rw->fast_iov; | |
3285 | if (iter->iov != fast_iov) { | |
3286 | iov_off = iter->iov - fast_iov; | |
3287 | rw->iter.iov += iov_off; | |
3288 | } | |
3289 | if (rw->fast_iov != fast_iov) | |
3290 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3291 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3292 | } else { |
3293 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3294 | } |
3295 | } | |
3296 | ||
e8c2bc1f | 3297 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3298 | { |
e8c2bc1f JA |
3299 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3300 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3301 | return req->async_data == NULL; | |
3d9932a8 XW |
3302 | } |
3303 | ||
e8c2bc1f | 3304 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3305 | { |
e8c2bc1f | 3306 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3307 | return 0; |
3d9932a8 | 3308 | |
e8c2bc1f | 3309 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3310 | } |
3311 | ||
ff6165b2 JA |
3312 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3313 | const struct iovec *fast_iov, | |
227c0c96 | 3314 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3315 | { |
e8c2bc1f | 3316 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3317 | return 0; |
e8c2bc1f | 3318 | if (!req->async_data) { |
6bf985dc PB |
3319 | if (__io_alloc_async_data(req)) { |
3320 | kfree(iovec); | |
5d204bcf | 3321 | return -ENOMEM; |
6bf985dc | 3322 | } |
b7bb4f7d | 3323 | |
ff6165b2 | 3324 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3325 | } |
b7bb4f7d | 3326 | return 0; |
f67676d1 JA |
3327 | } |
3328 | ||
73debe68 | 3329 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3330 | { |
e8c2bc1f | 3331 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3332 | struct iovec *iov = iorw->fast_iov; |
847595de | 3333 | int ret; |
c3e330a4 | 3334 | |
2846c481 | 3335 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3336 | if (unlikely(ret < 0)) |
3337 | return ret; | |
3338 | ||
ab0b196c PB |
3339 | iorw->bytes_done = 0; |
3340 | iorw->free_iovec = iov; | |
3341 | if (iov) | |
3342 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3343 | return 0; |
3344 | } | |
3345 | ||
73debe68 | 3346 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3347 | { |
3348 | ssize_t ret; | |
3349 | ||
a88fc400 | 3350 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3351 | if (ret) |
3352 | return ret; | |
f67676d1 | 3353 | |
3529d8c2 JA |
3354 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3355 | return -EBADF; | |
f67676d1 | 3356 | |
5f798bea | 3357 | /* either don't need iovec imported or already have it */ |
2d199895 | 3358 | if (!req->async_data) |
3529d8c2 | 3359 | return 0; |
73debe68 | 3360 | return io_rw_prep_async(req, READ); |
f67676d1 JA |
3361 | } |
3362 | ||
c1dd91d1 JA |
3363 | /* |
3364 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3365 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3366 | * This gets called when the page is unlocked, and we generally expect that to | |
3367 | * happen when the page IO is completed and the page is now uptodate. This will | |
3368 | * queue a task_work based retry of the operation, attempting to copy the data | |
3369 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3370 | * do a thread based blocking retry of the operation. That's the unexpected | |
3371 | * slow path. | |
3372 | */ | |
bcf5a063 JA |
3373 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3374 | int sync, void *arg) | |
3375 | { | |
3376 | struct wait_page_queue *wpq; | |
3377 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3378 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3379 | int ret; |
3380 | ||
3381 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3382 | ||
cdc8fcb4 LT |
3383 | if (!wake_page_match(wpq, key)) |
3384 | return 0; | |
3385 | ||
c8d317aa | 3386 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3387 | list_del_init(&wait->entry); |
3388 | ||
e7375122 | 3389 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3390 | percpu_ref_get(&req->ctx->refs); |
3391 | ||
bcf5a063 JA |
3392 | /* submit ref gets dropped, acquire a new one */ |
3393 | refcount_inc(&req->refs); | |
355fb9e2 | 3394 | ret = io_req_task_work_add(req); |
eab30c4d PB |
3395 | if (unlikely(ret)) |
3396 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
bcf5a063 JA |
3397 | return 1; |
3398 | } | |
3399 | ||
c1dd91d1 JA |
3400 | /* |
3401 | * This controls whether a given IO request should be armed for async page | |
3402 | * based retry. If we return false here, the request is handed to the async | |
3403 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3404 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3405 | * will either succeed because the page is now uptodate and unlocked, or it | |
3406 | * will register a callback when the page is unlocked at IO completion. Through | |
3407 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3408 | * That retry will attempt the buffered read again. The retry will generally | |
3409 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3410 | * async worker threads for a blocking retry. | |
3411 | */ | |
227c0c96 | 3412 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3413 | { |
e8c2bc1f JA |
3414 | struct io_async_rw *rw = req->async_data; |
3415 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3416 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3417 | |
bcf5a063 JA |
3418 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3419 | if (req->flags & REQ_F_NOWAIT) | |
3420 | return false; | |
f67676d1 | 3421 | |
227c0c96 | 3422 | /* Only for buffered IO */ |
3b2a4439 | 3423 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3424 | return false; |
3b2a4439 | 3425 | |
bcf5a063 JA |
3426 | /* |
3427 | * just use poll if we can, and don't attempt if the fs doesn't | |
3428 | * support callback based unlocks | |
3429 | */ | |
3430 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3431 | return false; | |
f67676d1 | 3432 | |
3b2a4439 JA |
3433 | wait->wait.func = io_async_buf_func; |
3434 | wait->wait.private = req; | |
3435 | wait->wait.flags = 0; | |
3436 | INIT_LIST_HEAD(&wait->wait.entry); | |
3437 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3438 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3439 | kiocb->ki_waitq = wait; |
3b2a4439 | 3440 | return true; |
bcf5a063 JA |
3441 | } |
3442 | ||
3443 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3444 | { | |
3445 | if (req->file->f_op->read_iter) | |
3446 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3447 | else if (req->file->f_op->read) |
4017eb91 | 3448 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3449 | else |
3450 | return -EINVAL; | |
f67676d1 JA |
3451 | } |
3452 | ||
889fca73 | 3453 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3454 | { |
3455 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3456 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3457 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3458 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3459 | ssize_t io_size, ret, ret2; |
45d189c6 | 3460 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3461 | |
2846c481 | 3462 | if (rw) { |
e8c2bc1f | 3463 | iter = &rw->iter; |
2846c481 PB |
3464 | iovec = NULL; |
3465 | } else { | |
3466 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3467 | if (ret < 0) | |
3468 | return ret; | |
3469 | } | |
632546c4 | 3470 | io_size = iov_iter_count(iter); |
fa15bafb | 3471 | req->result = io_size; |
2b188cc1 | 3472 | |
fd6c2e4c JA |
3473 | /* Ensure we clear previously set non-block flag */ |
3474 | if (!force_nonblock) | |
29de5f6a | 3475 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3476 | else |
3477 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3478 | ||
24c74678 | 3479 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3480 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3481 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3482 | return ret ?: -EAGAIN; |
6713e7a6 | 3483 | } |
9e645e11 | 3484 | |
632546c4 | 3485 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3486 | if (unlikely(ret)) { |
3487 | kfree(iovec); | |
3488 | return ret; | |
3489 | } | |
2b188cc1 | 3490 | |
227c0c96 | 3491 | ret = io_iter_do_read(req, iter); |
32960613 | 3492 | |
57cd657b | 3493 | if (ret == -EIOCBQUEUED) { |
5ea5dd45 PB |
3494 | /* it's faster to check here then delegate to kfree */ |
3495 | if (iovec) | |
3496 | kfree(iovec); | |
3497 | return 0; | |
227c0c96 | 3498 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3499 | /* IOPOLL retry should happen for io-wq threads */ |
3500 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3501 | goto done; |
75c668cd PB |
3502 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3503 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3504 | goto done; |
84216315 | 3505 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3506 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3507 | ret = 0; |
7335e3bf | 3508 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3509 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3510 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3511 | goto done; |
1a2cc0ce | 3512 | } |
227c0c96 | 3513 | |
227c0c96 | 3514 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3515 | if (ret2) |
3516 | return ret2; | |
3517 | ||
e8c2bc1f | 3518 | rw = req->async_data; |
227c0c96 | 3519 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3520 | iter = &rw->iter; |
227c0c96 | 3521 | |
b23df91b PB |
3522 | do { |
3523 | io_size -= ret; | |
3524 | rw->bytes_done += ret; | |
3525 | /* if we can retry, do so with the callbacks armed */ | |
3526 | if (!io_rw_should_retry(req)) { | |
3527 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3528 | return -EAGAIN; | |
3529 | } | |
3530 | ||
3531 | /* | |
3532 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3533 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3534 | * desired page gets unlocked. We can also get a partial read | |
3535 | * here, and if we do, then just retry at the new offset. | |
3536 | */ | |
3537 | ret = io_iter_do_read(req, iter); | |
3538 | if (ret == -EIOCBQUEUED) | |
3539 | return 0; | |
3540 | /* we got some bytes, but not all. retry. */ | |
3541 | } while (ret > 0 && ret < io_size); | |
227c0c96 | 3542 | done: |
889fca73 | 3543 | kiocb_done(kiocb, ret, issue_flags); |
5ea5dd45 | 3544 | return 0; |
2b188cc1 JA |
3545 | } |
3546 | ||
73debe68 | 3547 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3548 | { |
3549 | ssize_t ret; | |
3550 | ||
a88fc400 | 3551 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3552 | if (ret) |
3553 | return ret; | |
f67676d1 | 3554 | |
3529d8c2 JA |
3555 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3556 | return -EBADF; | |
f67676d1 | 3557 | |
5f798bea | 3558 | /* either don't need iovec imported or already have it */ |
2d199895 | 3559 | if (!req->async_data) |
3529d8c2 | 3560 | return 0; |
73debe68 | 3561 | return io_rw_prep_async(req, WRITE); |
f67676d1 JA |
3562 | } |
3563 | ||
889fca73 | 3564 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3565 | { |
3566 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3567 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3568 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3569 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3570 | ssize_t ret, ret2, io_size; |
45d189c6 | 3571 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3572 | |
2846c481 | 3573 | if (rw) { |
e8c2bc1f | 3574 | iter = &rw->iter; |
2846c481 PB |
3575 | iovec = NULL; |
3576 | } else { | |
3577 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3578 | if (ret < 0) | |
3579 | return ret; | |
3580 | } | |
632546c4 | 3581 | io_size = iov_iter_count(iter); |
fa15bafb | 3582 | req->result = io_size; |
2b188cc1 | 3583 | |
fd6c2e4c JA |
3584 | /* Ensure we clear previously set non-block flag */ |
3585 | if (!force_nonblock) | |
a88fc400 PB |
3586 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3587 | else | |
3588 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3589 | |
24c74678 | 3590 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3591 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3592 | goto copy_iov; |
31b51510 | 3593 | |
10d59345 JA |
3594 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3595 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3596 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3597 | goto copy_iov; |
31b51510 | 3598 | |
632546c4 | 3599 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3600 | if (unlikely(ret)) |
3601 | goto out_free; | |
4ed734b0 | 3602 | |
fa15bafb PB |
3603 | /* |
3604 | * Open-code file_start_write here to grab freeze protection, | |
3605 | * which will be released by another thread in | |
3606 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3607 | * released so that it doesn't complain about the held lock when | |
3608 | * we return to userspace. | |
3609 | */ | |
3610 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3611 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3612 | __sb_writers_release(file_inode(req->file)->i_sb, |
3613 | SB_FREEZE_WRITE); | |
3614 | } | |
3615 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3616 | |
fa15bafb | 3617 | if (req->file->f_op->write_iter) |
ff6165b2 | 3618 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3619 | else if (req->file->f_op->write) |
4017eb91 | 3620 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3621 | else |
3622 | ret2 = -EINVAL; | |
4ed734b0 | 3623 | |
fa15bafb PB |
3624 | /* |
3625 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3626 | * retry them without IOCB_NOWAIT. | |
3627 | */ | |
3628 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3629 | ret2 = -EAGAIN; | |
75c668cd PB |
3630 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3631 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3632 | goto done; |
fa15bafb | 3633 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3634 | /* IOPOLL retry should happen for io-wq threads */ |
3635 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3636 | goto copy_iov; | |
355afaeb | 3637 | done: |
889fca73 | 3638 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3639 | } else { |
f67676d1 | 3640 | copy_iov: |
84216315 | 3641 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3642 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3643 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3644 | return ret ?: -EAGAIN; |
2b188cc1 | 3645 | } |
31b51510 | 3646 | out_free: |
f261c168 | 3647 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3648 | if (iovec) |
6f2cc166 | 3649 | kfree(iovec); |
2b188cc1 JA |
3650 | return ret; |
3651 | } | |
3652 | ||
80a261fd JA |
3653 | static int io_renameat_prep(struct io_kiocb *req, |
3654 | const struct io_uring_sqe *sqe) | |
3655 | { | |
3656 | struct io_rename *ren = &req->rename; | |
3657 | const char __user *oldf, *newf; | |
3658 | ||
3659 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3660 | return -EBADF; | |
3661 | ||
3662 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3663 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3664 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3665 | ren->new_dfd = READ_ONCE(sqe->len); | |
3666 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3667 | ||
3668 | ren->oldpath = getname(oldf); | |
3669 | if (IS_ERR(ren->oldpath)) | |
3670 | return PTR_ERR(ren->oldpath); | |
3671 | ||
3672 | ren->newpath = getname(newf); | |
3673 | if (IS_ERR(ren->newpath)) { | |
3674 | putname(ren->oldpath); | |
3675 | return PTR_ERR(ren->newpath); | |
3676 | } | |
3677 | ||
3678 | req->flags |= REQ_F_NEED_CLEANUP; | |
3679 | return 0; | |
3680 | } | |
3681 | ||
45d189c6 | 3682 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3683 | { |
3684 | struct io_rename *ren = &req->rename; | |
3685 | int ret; | |
3686 | ||
45d189c6 | 3687 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3688 | return -EAGAIN; |
3689 | ||
3690 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3691 | ren->newpath, ren->flags); | |
3692 | ||
3693 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3694 | if (ret < 0) | |
3695 | req_set_fail_links(req); | |
3696 | io_req_complete(req, ret); | |
3697 | return 0; | |
3698 | } | |
3699 | ||
14a1143b JA |
3700 | static int io_unlinkat_prep(struct io_kiocb *req, |
3701 | const struct io_uring_sqe *sqe) | |
3702 | { | |
3703 | struct io_unlink *un = &req->unlink; | |
3704 | const char __user *fname; | |
3705 | ||
3706 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3707 | return -EBADF; | |
3708 | ||
3709 | un->dfd = READ_ONCE(sqe->fd); | |
3710 | ||
3711 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3712 | if (un->flags & ~AT_REMOVEDIR) | |
3713 | return -EINVAL; | |
3714 | ||
3715 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3716 | un->filename = getname(fname); | |
3717 | if (IS_ERR(un->filename)) | |
3718 | return PTR_ERR(un->filename); | |
3719 | ||
3720 | req->flags |= REQ_F_NEED_CLEANUP; | |
3721 | return 0; | |
3722 | } | |
3723 | ||
45d189c6 | 3724 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3725 | { |
3726 | struct io_unlink *un = &req->unlink; | |
3727 | int ret; | |
3728 | ||
45d189c6 | 3729 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3730 | return -EAGAIN; |
3731 | ||
3732 | if (un->flags & AT_REMOVEDIR) | |
3733 | ret = do_rmdir(un->dfd, un->filename); | |
3734 | else | |
3735 | ret = do_unlinkat(un->dfd, un->filename); | |
3736 | ||
3737 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3738 | if (ret < 0) | |
3739 | req_set_fail_links(req); | |
3740 | io_req_complete(req, ret); | |
3741 | return 0; | |
3742 | } | |
3743 | ||
36f4fa68 JA |
3744 | static int io_shutdown_prep(struct io_kiocb *req, |
3745 | const struct io_uring_sqe *sqe) | |
3746 | { | |
3747 | #if defined(CONFIG_NET) | |
3748 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3749 | return -EINVAL; | |
3750 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3751 | sqe->buf_index) | |
3752 | return -EINVAL; | |
3753 | ||
3754 | req->shutdown.how = READ_ONCE(sqe->len); | |
3755 | return 0; | |
3756 | #else | |
3757 | return -EOPNOTSUPP; | |
3758 | #endif | |
3759 | } | |
3760 | ||
45d189c6 | 3761 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3762 | { |
3763 | #if defined(CONFIG_NET) | |
3764 | struct socket *sock; | |
3765 | int ret; | |
3766 | ||
45d189c6 | 3767 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3768 | return -EAGAIN; |
3769 | ||
48aba79b | 3770 | sock = sock_from_file(req->file); |
36f4fa68 | 3771 | if (unlikely(!sock)) |
48aba79b | 3772 | return -ENOTSOCK; |
36f4fa68 JA |
3773 | |
3774 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3775 | if (ret < 0) |
3776 | req_set_fail_links(req); | |
36f4fa68 JA |
3777 | io_req_complete(req, ret); |
3778 | return 0; | |
3779 | #else | |
3780 | return -EOPNOTSUPP; | |
3781 | #endif | |
3782 | } | |
3783 | ||
f2a8d5c7 PB |
3784 | static int __io_splice_prep(struct io_kiocb *req, |
3785 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3786 | { |
3787 | struct io_splice* sp = &req->splice; | |
3788 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3789 | |
3232dd02 PB |
3790 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3791 | return -EINVAL; | |
7d67af2c PB |
3792 | |
3793 | sp->file_in = NULL; | |
7d67af2c PB |
3794 | sp->len = READ_ONCE(sqe->len); |
3795 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3796 | ||
3797 | if (unlikely(sp->flags & ~valid_flags)) | |
3798 | return -EINVAL; | |
3799 | ||
8371adf5 PB |
3800 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3801 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3802 | if (!sp->file_in) | |
3803 | return -EBADF; | |
7d67af2c PB |
3804 | req->flags |= REQ_F_NEED_CLEANUP; |
3805 | ||
7cdaf587 XW |
3806 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3807 | /* | |
3808 | * Splice operation will be punted aync, and here need to | |
3809 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3810 | */ | |
3811 | io_req_init_async(req); | |
7d67af2c | 3812 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3813 | } |
7d67af2c PB |
3814 | |
3815 | return 0; | |
3816 | } | |
3817 | ||
f2a8d5c7 PB |
3818 | static int io_tee_prep(struct io_kiocb *req, |
3819 | const struct io_uring_sqe *sqe) | |
3820 | { | |
3821 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3822 | return -EINVAL; | |
3823 | return __io_splice_prep(req, sqe); | |
3824 | } | |
3825 | ||
45d189c6 | 3826 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3827 | { |
3828 | struct io_splice *sp = &req->splice; | |
3829 | struct file *in = sp->file_in; | |
3830 | struct file *out = sp->file_out; | |
3831 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3832 | long ret = 0; | |
3833 | ||
45d189c6 | 3834 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3835 | return -EAGAIN; |
3836 | if (sp->len) | |
3837 | ret = do_tee(in, out, sp->len, flags); | |
3838 | ||
3839 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3840 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3841 | ||
f2a8d5c7 PB |
3842 | if (ret != sp->len) |
3843 | req_set_fail_links(req); | |
e1e16097 | 3844 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3845 | return 0; |
3846 | } | |
3847 | ||
3848 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3849 | { | |
3850 | struct io_splice* sp = &req->splice; | |
3851 | ||
3852 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3853 | sp->off_out = READ_ONCE(sqe->off); | |
3854 | return __io_splice_prep(req, sqe); | |
3855 | } | |
3856 | ||
45d189c6 | 3857 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3858 | { |
3859 | struct io_splice *sp = &req->splice; | |
3860 | struct file *in = sp->file_in; | |
3861 | struct file *out = sp->file_out; | |
3862 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3863 | loff_t *poff_in, *poff_out; | |
c9687426 | 3864 | long ret = 0; |
7d67af2c | 3865 | |
45d189c6 | 3866 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3867 | return -EAGAIN; |
7d67af2c PB |
3868 | |
3869 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3870 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3871 | |
948a7749 | 3872 | if (sp->len) |
c9687426 | 3873 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3874 | |
3875 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3876 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3877 | ||
7d67af2c PB |
3878 | if (ret != sp->len) |
3879 | req_set_fail_links(req); | |
e1e16097 | 3880 | io_req_complete(req, ret); |
7d67af2c PB |
3881 | return 0; |
3882 | } | |
3883 | ||
2b188cc1 JA |
3884 | /* |
3885 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3886 | */ | |
889fca73 | 3887 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3888 | { |
3889 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3890 | |
def596e9 JA |
3891 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3892 | return -EINVAL; | |
3893 | ||
889fca73 | 3894 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3895 | return 0; |
3896 | } | |
3897 | ||
3529d8c2 | 3898 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3899 | { |
6b06314c | 3900 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3901 | |
09bb8394 JA |
3902 | if (!req->file) |
3903 | return -EBADF; | |
c992fe29 | 3904 | |
6b06314c | 3905 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3906 | return -EINVAL; |
edafccee | 3907 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3908 | return -EINVAL; |
3909 | ||
8ed8d3c3 JA |
3910 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3911 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3912 | return -EINVAL; | |
3913 | ||
3914 | req->sync.off = READ_ONCE(sqe->off); | |
3915 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3916 | return 0; |
3917 | } | |
3918 | ||
45d189c6 | 3919 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3920 | { |
8ed8d3c3 | 3921 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3922 | int ret; |
3923 | ||
ac45abc0 | 3924 | /* fsync always requires a blocking context */ |
45d189c6 | 3925 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3926 | return -EAGAIN; |
3927 | ||
9adbd45d | 3928 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3929 | end > 0 ? end : LLONG_MAX, |
3930 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3931 | if (ret < 0) | |
3932 | req_set_fail_links(req); | |
e1e16097 | 3933 | io_req_complete(req, ret); |
c992fe29 CH |
3934 | return 0; |
3935 | } | |
3936 | ||
d63d1b5e JA |
3937 | static int io_fallocate_prep(struct io_kiocb *req, |
3938 | const struct io_uring_sqe *sqe) | |
3939 | { | |
3940 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3941 | return -EINVAL; | |
3232dd02 PB |
3942 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3943 | return -EINVAL; | |
d63d1b5e JA |
3944 | |
3945 | req->sync.off = READ_ONCE(sqe->off); | |
3946 | req->sync.len = READ_ONCE(sqe->addr); | |
3947 | req->sync.mode = READ_ONCE(sqe->len); | |
3948 | return 0; | |
3949 | } | |
3950 | ||
45d189c6 | 3951 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3952 | { |
ac45abc0 PB |
3953 | int ret; |
3954 | ||
d63d1b5e | 3955 | /* fallocate always requiring blocking context */ |
45d189c6 | 3956 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3957 | return -EAGAIN; |
ac45abc0 PB |
3958 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3959 | req->sync.len); | |
ac45abc0 PB |
3960 | if (ret < 0) |
3961 | req_set_fail_links(req); | |
e1e16097 | 3962 | io_req_complete(req, ret); |
5d17b4a4 JA |
3963 | return 0; |
3964 | } | |
3965 | ||
ec65fea5 | 3966 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3967 | { |
f8748881 | 3968 | const char __user *fname; |
15b71abe | 3969 | int ret; |
b7bb4f7d | 3970 | |
ec65fea5 | 3971 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3972 | return -EINVAL; |
ec65fea5 | 3973 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3974 | return -EBADF; |
03b1230c | 3975 | |
ec65fea5 PB |
3976 | /* open.how should be already initialised */ |
3977 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3978 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3979 | |
25e72d10 PB |
3980 | req->open.dfd = READ_ONCE(sqe->fd); |
3981 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3982 | req->open.filename = getname(fname); |
15b71abe JA |
3983 | if (IS_ERR(req->open.filename)) { |
3984 | ret = PTR_ERR(req->open.filename); | |
3985 | req->open.filename = NULL; | |
3986 | return ret; | |
3987 | } | |
4022e7af | 3988 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3989 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3990 | return 0; |
03b1230c JA |
3991 | } |
3992 | ||
ec65fea5 PB |
3993 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3994 | { | |
3995 | u64 flags, mode; | |
3996 | ||
14587a46 | 3997 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3998 | return -EINVAL; |
ec65fea5 PB |
3999 | mode = READ_ONCE(sqe->len); |
4000 | flags = READ_ONCE(sqe->open_flags); | |
4001 | req->open.how = build_open_how(flags, mode); | |
4002 | return __io_openat_prep(req, sqe); | |
4003 | } | |
4004 | ||
cebdb986 | 4005 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4006 | { |
cebdb986 | 4007 | struct open_how __user *how; |
cebdb986 | 4008 | size_t len; |
0fa03c62 JA |
4009 | int ret; |
4010 | ||
14587a46 | 4011 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4012 | return -EINVAL; |
cebdb986 JA |
4013 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4014 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4015 | if (len < OPEN_HOW_SIZE_VER0) |
4016 | return -EINVAL; | |
3529d8c2 | 4017 | |
cebdb986 JA |
4018 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4019 | len); | |
4020 | if (ret) | |
4021 | return ret; | |
3529d8c2 | 4022 | |
ec65fea5 | 4023 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4024 | } |
4025 | ||
45d189c6 | 4026 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4027 | { |
4028 | struct open_flags op; | |
15b71abe | 4029 | struct file *file; |
3a81fd02 JA |
4030 | bool nonblock_set; |
4031 | bool resolve_nonblock; | |
15b71abe JA |
4032 | int ret; |
4033 | ||
cebdb986 | 4034 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4035 | if (ret) |
4036 | goto err; | |
3a81fd02 JA |
4037 | nonblock_set = op.open_flag & O_NONBLOCK; |
4038 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4039 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4040 | /* |
4041 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4042 | * it'll always -EAGAIN | |
4043 | */ | |
4044 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4045 | return -EAGAIN; | |
4046 | op.lookup_flags |= LOOKUP_CACHED; | |
4047 | op.open_flag |= O_NONBLOCK; | |
4048 | } | |
15b71abe | 4049 | |
4022e7af | 4050 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4051 | if (ret < 0) |
4052 | goto err; | |
4053 | ||
4054 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 4055 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
4056 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
4057 | file == ERR_PTR(-EAGAIN)) { | |
3a81fd02 JA |
4058 | /* |
4059 | * We could hang on to this 'fd', but seems like marginal | |
4060 | * gain for something that is now known to be a slower path. | |
4061 | * So just put it, and we'll get a new one when we retry. | |
4062 | */ | |
4063 | put_unused_fd(ret); | |
4064 | return -EAGAIN; | |
4065 | } | |
4066 | ||
15b71abe JA |
4067 | if (IS_ERR(file)) { |
4068 | put_unused_fd(ret); | |
4069 | ret = PTR_ERR(file); | |
4070 | } else { | |
45d189c6 | 4071 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 4072 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
4073 | fsnotify_open(file); |
4074 | fd_install(ret, file); | |
4075 | } | |
4076 | err: | |
4077 | putname(req->open.filename); | |
8fef80bf | 4078 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4079 | if (ret < 0) |
4080 | req_set_fail_links(req); | |
e1e16097 | 4081 | io_req_complete(req, ret); |
15b71abe JA |
4082 | return 0; |
4083 | } | |
4084 | ||
45d189c6 | 4085 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4086 | { |
45d189c6 | 4087 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
4088 | } |
4089 | ||
067524e9 JA |
4090 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4091 | const struct io_uring_sqe *sqe) | |
4092 | { | |
4093 | struct io_provide_buf *p = &req->pbuf; | |
4094 | u64 tmp; | |
4095 | ||
4096 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4097 | return -EINVAL; | |
4098 | ||
4099 | tmp = READ_ONCE(sqe->fd); | |
4100 | if (!tmp || tmp > USHRT_MAX) | |
4101 | return -EINVAL; | |
4102 | ||
4103 | memset(p, 0, sizeof(*p)); | |
4104 | p->nbufs = tmp; | |
4105 | p->bgid = READ_ONCE(sqe->buf_group); | |
4106 | return 0; | |
4107 | } | |
4108 | ||
4109 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4110 | int bgid, unsigned nbufs) | |
4111 | { | |
4112 | unsigned i = 0; | |
4113 | ||
4114 | /* shouldn't happen */ | |
4115 | if (!nbufs) | |
4116 | return 0; | |
4117 | ||
4118 | /* the head kbuf is the list itself */ | |
4119 | while (!list_empty(&buf->list)) { | |
4120 | struct io_buffer *nxt; | |
4121 | ||
4122 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4123 | list_del(&nxt->list); | |
4124 | kfree(nxt); | |
4125 | if (++i == nbufs) | |
4126 | return i; | |
4127 | } | |
4128 | i++; | |
4129 | kfree(buf); | |
4130 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4131 | ||
4132 | return i; | |
4133 | } | |
4134 | ||
889fca73 | 4135 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4136 | { |
4137 | struct io_provide_buf *p = &req->pbuf; | |
4138 | struct io_ring_ctx *ctx = req->ctx; | |
4139 | struct io_buffer *head; | |
4140 | int ret = 0; | |
45d189c6 | 4141 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4142 | |
4143 | io_ring_submit_lock(ctx, !force_nonblock); | |
4144 | ||
4145 | lockdep_assert_held(&ctx->uring_lock); | |
4146 | ||
4147 | ret = -ENOENT; | |
4148 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4149 | if (head) | |
4150 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4151 | if (ret < 0) |
4152 | req_set_fail_links(req); | |
067524e9 | 4153 | |
31bff9a5 PB |
4154 | /* need to hold the lock to complete IOPOLL requests */ |
4155 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4156 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4157 | io_ring_submit_unlock(ctx, !force_nonblock); |
4158 | } else { | |
4159 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4160 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4161 | } |
067524e9 JA |
4162 | return 0; |
4163 | } | |
4164 | ||
ddf0322d JA |
4165 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4166 | const struct io_uring_sqe *sqe) | |
4167 | { | |
4168 | struct io_provide_buf *p = &req->pbuf; | |
4169 | u64 tmp; | |
4170 | ||
4171 | if (sqe->ioprio || sqe->rw_flags) | |
4172 | return -EINVAL; | |
4173 | ||
4174 | tmp = READ_ONCE(sqe->fd); | |
4175 | if (!tmp || tmp > USHRT_MAX) | |
4176 | return -E2BIG; | |
4177 | p->nbufs = tmp; | |
4178 | p->addr = READ_ONCE(sqe->addr); | |
4179 | p->len = READ_ONCE(sqe->len); | |
4180 | ||
efe68c1c | 4181 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4182 | return -EFAULT; |
4183 | ||
4184 | p->bgid = READ_ONCE(sqe->buf_group); | |
4185 | tmp = READ_ONCE(sqe->off); | |
4186 | if (tmp > USHRT_MAX) | |
4187 | return -E2BIG; | |
4188 | p->bid = tmp; | |
4189 | return 0; | |
4190 | } | |
4191 | ||
4192 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4193 | { | |
4194 | struct io_buffer *buf; | |
4195 | u64 addr = pbuf->addr; | |
4196 | int i, bid = pbuf->bid; | |
4197 | ||
4198 | for (i = 0; i < pbuf->nbufs; i++) { | |
4199 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4200 | if (!buf) | |
4201 | break; | |
4202 | ||
4203 | buf->addr = addr; | |
4204 | buf->len = pbuf->len; | |
4205 | buf->bid = bid; | |
4206 | addr += pbuf->len; | |
4207 | bid++; | |
4208 | if (!*head) { | |
4209 | INIT_LIST_HEAD(&buf->list); | |
4210 | *head = buf; | |
4211 | } else { | |
4212 | list_add_tail(&buf->list, &(*head)->list); | |
4213 | } | |
4214 | } | |
4215 | ||
4216 | return i ? i : -ENOMEM; | |
4217 | } | |
4218 | ||
889fca73 | 4219 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4220 | { |
4221 | struct io_provide_buf *p = &req->pbuf; | |
4222 | struct io_ring_ctx *ctx = req->ctx; | |
4223 | struct io_buffer *head, *list; | |
4224 | int ret = 0; | |
45d189c6 | 4225 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4226 | |
4227 | io_ring_submit_lock(ctx, !force_nonblock); | |
4228 | ||
4229 | lockdep_assert_held(&ctx->uring_lock); | |
4230 | ||
4231 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4232 | ||
4233 | ret = io_add_buffers(p, &head); | |
4234 | if (ret < 0) | |
4235 | goto out; | |
4236 | ||
4237 | if (!list) { | |
4238 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4239 | GFP_KERNEL); | |
4240 | if (ret < 0) { | |
067524e9 | 4241 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4242 | goto out; |
4243 | } | |
4244 | } | |
4245 | out: | |
ddf0322d JA |
4246 | if (ret < 0) |
4247 | req_set_fail_links(req); | |
31bff9a5 PB |
4248 | |
4249 | /* need to hold the lock to complete IOPOLL requests */ | |
4250 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4251 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4252 | io_ring_submit_unlock(ctx, !force_nonblock); |
4253 | } else { | |
4254 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4255 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4256 | } |
ddf0322d | 4257 | return 0; |
cebdb986 JA |
4258 | } |
4259 | ||
3e4827b0 JA |
4260 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4261 | const struct io_uring_sqe *sqe) | |
4262 | { | |
4263 | #if defined(CONFIG_EPOLL) | |
4264 | if (sqe->ioprio || sqe->buf_index) | |
4265 | return -EINVAL; | |
6ca56f84 | 4266 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4267 | return -EINVAL; |
3e4827b0 JA |
4268 | |
4269 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4270 | req->epoll.op = READ_ONCE(sqe->len); | |
4271 | req->epoll.fd = READ_ONCE(sqe->off); | |
4272 | ||
4273 | if (ep_op_has_event(req->epoll.op)) { | |
4274 | struct epoll_event __user *ev; | |
4275 | ||
4276 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4277 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4278 | return -EFAULT; | |
4279 | } | |
4280 | ||
4281 | return 0; | |
4282 | #else | |
4283 | return -EOPNOTSUPP; | |
4284 | #endif | |
4285 | } | |
4286 | ||
889fca73 | 4287 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4288 | { |
4289 | #if defined(CONFIG_EPOLL) | |
4290 | struct io_epoll *ie = &req->epoll; | |
4291 | int ret; | |
45d189c6 | 4292 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4293 | |
4294 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4295 | if (force_nonblock && ret == -EAGAIN) | |
4296 | return -EAGAIN; | |
4297 | ||
4298 | if (ret < 0) | |
4299 | req_set_fail_links(req); | |
889fca73 | 4300 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4301 | return 0; |
4302 | #else | |
4303 | return -EOPNOTSUPP; | |
4304 | #endif | |
4305 | } | |
4306 | ||
c1ca757b JA |
4307 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4308 | { | |
4309 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4310 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4311 | return -EINVAL; | |
3232dd02 PB |
4312 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4313 | return -EINVAL; | |
c1ca757b JA |
4314 | |
4315 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4316 | req->madvise.len = READ_ONCE(sqe->len); | |
4317 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4318 | return 0; | |
4319 | #else | |
4320 | return -EOPNOTSUPP; | |
4321 | #endif | |
4322 | } | |
4323 | ||
45d189c6 | 4324 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4325 | { |
4326 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4327 | struct io_madvise *ma = &req->madvise; | |
4328 | int ret; | |
4329 | ||
45d189c6 | 4330 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4331 | return -EAGAIN; |
4332 | ||
0726b01e | 4333 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4334 | if (ret < 0) |
4335 | req_set_fail_links(req); | |
e1e16097 | 4336 | io_req_complete(req, ret); |
c1ca757b JA |
4337 | return 0; |
4338 | #else | |
4339 | return -EOPNOTSUPP; | |
4340 | #endif | |
4341 | } | |
4342 | ||
4840e418 JA |
4343 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4344 | { | |
4345 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4346 | return -EINVAL; | |
3232dd02 PB |
4347 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4348 | return -EINVAL; | |
4840e418 JA |
4349 | |
4350 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4351 | req->fadvise.len = READ_ONCE(sqe->len); | |
4352 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4353 | return 0; | |
4354 | } | |
4355 | ||
45d189c6 | 4356 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4357 | { |
4358 | struct io_fadvise *fa = &req->fadvise; | |
4359 | int ret; | |
4360 | ||
45d189c6 | 4361 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4362 | switch (fa->advice) { |
4363 | case POSIX_FADV_NORMAL: | |
4364 | case POSIX_FADV_RANDOM: | |
4365 | case POSIX_FADV_SEQUENTIAL: | |
4366 | break; | |
4367 | default: | |
4368 | return -EAGAIN; | |
4369 | } | |
4370 | } | |
4840e418 JA |
4371 | |
4372 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4373 | if (ret < 0) | |
4374 | req_set_fail_links(req); | |
e1e16097 | 4375 | io_req_complete(req, ret); |
4840e418 JA |
4376 | return 0; |
4377 | } | |
4378 | ||
eddc7ef5 JA |
4379 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4380 | { | |
6ca56f84 | 4381 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4382 | return -EINVAL; |
eddc7ef5 JA |
4383 | if (sqe->ioprio || sqe->buf_index) |
4384 | return -EINVAL; | |
9c280f90 | 4385 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4386 | return -EBADF; |
eddc7ef5 | 4387 | |
1d9e1288 BM |
4388 | req->statx.dfd = READ_ONCE(sqe->fd); |
4389 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4390 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4391 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4392 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4393 | |
4394 | return 0; | |
4395 | } | |
4396 | ||
45d189c6 | 4397 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4398 | { |
1d9e1288 | 4399 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4400 | int ret; |
4401 | ||
45d189c6 | 4402 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4403 | /* only need file table for an actual valid fd */ |
4404 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4405 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4406 | return -EAGAIN; |
5b0bbee4 | 4407 | } |
eddc7ef5 | 4408 | |
e62753e4 BM |
4409 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4410 | ctx->buffer); | |
eddc7ef5 | 4411 | |
eddc7ef5 JA |
4412 | if (ret < 0) |
4413 | req_set_fail_links(req); | |
e1e16097 | 4414 | io_req_complete(req, ret); |
eddc7ef5 JA |
4415 | return 0; |
4416 | } | |
4417 | ||
b5dba59e JA |
4418 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4419 | { | |
14587a46 | 4420 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4421 | return -EINVAL; |
b5dba59e JA |
4422 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4423 | sqe->rw_flags || sqe->buf_index) | |
4424 | return -EINVAL; | |
9c280f90 | 4425 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4426 | return -EBADF; |
b5dba59e JA |
4427 | |
4428 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4429 | return 0; |
b5dba59e JA |
4430 | } |
4431 | ||
889fca73 | 4432 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4433 | { |
9eac1904 | 4434 | struct files_struct *files = current->files; |
3af73b28 | 4435 | struct io_close *close = &req->close; |
9eac1904 JA |
4436 | struct fdtable *fdt; |
4437 | struct file *file; | |
b5dba59e JA |
4438 | int ret; |
4439 | ||
9eac1904 JA |
4440 | file = NULL; |
4441 | ret = -EBADF; | |
4442 | spin_lock(&files->file_lock); | |
4443 | fdt = files_fdtable(files); | |
4444 | if (close->fd >= fdt->max_fds) { | |
4445 | spin_unlock(&files->file_lock); | |
4446 | goto err; | |
4447 | } | |
4448 | file = fdt->fd[close->fd]; | |
4449 | if (!file) { | |
4450 | spin_unlock(&files->file_lock); | |
4451 | goto err; | |
4452 | } | |
4453 | ||
4454 | if (file->f_op == &io_uring_fops) { | |
4455 | spin_unlock(&files->file_lock); | |
4456 | file = NULL; | |
4457 | goto err; | |
3af73b28 | 4458 | } |
b5dba59e JA |
4459 | |
4460 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4461 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4462 | spin_unlock(&files->file_lock); |
0bf0eefd | 4463 | return -EAGAIN; |
a2100672 | 4464 | } |
b5dba59e | 4465 | |
9eac1904 JA |
4466 | ret = __close_fd_get_file(close->fd, &file); |
4467 | spin_unlock(&files->file_lock); | |
4468 | if (ret < 0) { | |
4469 | if (ret == -ENOENT) | |
4470 | ret = -EBADF; | |
4471 | goto err; | |
4472 | } | |
4473 | ||
3af73b28 | 4474 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4475 | ret = filp_close(file, current->files); |
4476 | err: | |
3af73b28 PB |
4477 | if (ret < 0) |
4478 | req_set_fail_links(req); | |
9eac1904 JA |
4479 | if (file) |
4480 | fput(file); | |
889fca73 | 4481 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4482 | return 0; |
b5dba59e JA |
4483 | } |
4484 | ||
3529d8c2 | 4485 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4486 | { |
4487 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4488 | |
4489 | if (!req->file) | |
4490 | return -EBADF; | |
5d17b4a4 JA |
4491 | |
4492 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4493 | return -EINVAL; | |
4494 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4495 | return -EINVAL; | |
4496 | ||
8ed8d3c3 JA |
4497 | req->sync.off = READ_ONCE(sqe->off); |
4498 | req->sync.len = READ_ONCE(sqe->len); | |
4499 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4500 | return 0; |
4501 | } | |
4502 | ||
45d189c6 | 4503 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4504 | { |
8ed8d3c3 JA |
4505 | int ret; |
4506 | ||
ac45abc0 | 4507 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4508 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4509 | return -EAGAIN; |
4510 | ||
9adbd45d | 4511 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4512 | req->sync.flags); |
4513 | if (ret < 0) | |
4514 | req_set_fail_links(req); | |
e1e16097 | 4515 | io_req_complete(req, ret); |
5d17b4a4 JA |
4516 | return 0; |
4517 | } | |
4518 | ||
469956e8 | 4519 | #if defined(CONFIG_NET) |
02d27d89 PB |
4520 | static int io_setup_async_msg(struct io_kiocb *req, |
4521 | struct io_async_msghdr *kmsg) | |
4522 | { | |
e8c2bc1f JA |
4523 | struct io_async_msghdr *async_msg = req->async_data; |
4524 | ||
4525 | if (async_msg) | |
02d27d89 | 4526 | return -EAGAIN; |
e8c2bc1f | 4527 | if (io_alloc_async_data(req)) { |
257e84a5 | 4528 | kfree(kmsg->free_iov); |
02d27d89 PB |
4529 | return -ENOMEM; |
4530 | } | |
e8c2bc1f | 4531 | async_msg = req->async_data; |
02d27d89 | 4532 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4533 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4534 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4535 | /* if were using fast_iov, set it to the new one */ |
4536 | if (!async_msg->free_iov) | |
4537 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4538 | ||
02d27d89 PB |
4539 | return -EAGAIN; |
4540 | } | |
4541 | ||
2ae523ed PB |
4542 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4543 | struct io_async_msghdr *iomsg) | |
4544 | { | |
2ae523ed | 4545 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4546 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4547 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4548 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4549 | } |
4550 | ||
3529d8c2 | 4551 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4552 | { |
e8c2bc1f | 4553 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4554 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4555 | int ret; |
03b1230c | 4556 | |
d2b6f48b PB |
4557 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4558 | return -EINVAL; | |
4559 | ||
e47293fd | 4560 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4561 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4562 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4563 | |
d8768362 JA |
4564 | #ifdef CONFIG_COMPAT |
4565 | if (req->ctx->compat) | |
4566 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4567 | #endif | |
4568 | ||
e8c2bc1f | 4569 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4570 | return 0; |
e8c2bc1f | 4571 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4572 | if (!ret) |
4573 | req->flags |= REQ_F_NEED_CLEANUP; | |
4574 | return ret; | |
03b1230c JA |
4575 | } |
4576 | ||
889fca73 | 4577 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4578 | { |
6b754c8b | 4579 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4580 | struct socket *sock; |
7a7cacba | 4581 | unsigned flags; |
0fa03c62 JA |
4582 | int ret; |
4583 | ||
dba4a925 | 4584 | sock = sock_from_file(req->file); |
7a7cacba | 4585 | if (unlikely(!sock)) |
dba4a925 | 4586 | return -ENOTSOCK; |
3529d8c2 | 4587 | |
257e84a5 PB |
4588 | kmsg = req->async_data; |
4589 | if (!kmsg) { | |
7a7cacba PB |
4590 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4591 | if (ret) | |
4592 | return ret; | |
4593 | kmsg = &iomsg; | |
0fa03c62 | 4594 | } |
0fa03c62 | 4595 | |
7a7cacba PB |
4596 | flags = req->sr_msg.msg_flags; |
4597 | if (flags & MSG_DONTWAIT) | |
4598 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4599 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4600 | flags |= MSG_DONTWAIT; |
e47293fd | 4601 | |
7a7cacba | 4602 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4603 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4604 | return io_setup_async_msg(req, kmsg); |
4605 | if (ret == -ERESTARTSYS) | |
4606 | ret = -EINTR; | |
0fa03c62 | 4607 | |
257e84a5 PB |
4608 | /* fast path, check for non-NULL to avoid function call */ |
4609 | if (kmsg->free_iov) | |
4610 | kfree(kmsg->free_iov); | |
99bc4c38 | 4611 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4612 | if (ret < 0) |
4613 | req_set_fail_links(req); | |
889fca73 | 4614 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4615 | return 0; |
03b1230c | 4616 | } |
aa1fa28f | 4617 | |
889fca73 | 4618 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4619 | { |
7a7cacba PB |
4620 | struct io_sr_msg *sr = &req->sr_msg; |
4621 | struct msghdr msg; | |
4622 | struct iovec iov; | |
fddaface | 4623 | struct socket *sock; |
7a7cacba | 4624 | unsigned flags; |
fddaface JA |
4625 | int ret; |
4626 | ||
dba4a925 | 4627 | sock = sock_from_file(req->file); |
7a7cacba | 4628 | if (unlikely(!sock)) |
dba4a925 | 4629 | return -ENOTSOCK; |
fddaface | 4630 | |
7a7cacba PB |
4631 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4632 | if (unlikely(ret)) | |
14db8411 | 4633 | return ret; |
fddaface | 4634 | |
7a7cacba PB |
4635 | msg.msg_name = NULL; |
4636 | msg.msg_control = NULL; | |
4637 | msg.msg_controllen = 0; | |
4638 | msg.msg_namelen = 0; | |
fddaface | 4639 | |
7a7cacba PB |
4640 | flags = req->sr_msg.msg_flags; |
4641 | if (flags & MSG_DONTWAIT) | |
4642 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4643 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4644 | flags |= MSG_DONTWAIT; |
fddaface | 4645 | |
7a7cacba PB |
4646 | msg.msg_flags = flags; |
4647 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4648 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4649 | return -EAGAIN; |
4650 | if (ret == -ERESTARTSYS) | |
4651 | ret = -EINTR; | |
fddaface | 4652 | |
fddaface JA |
4653 | if (ret < 0) |
4654 | req_set_fail_links(req); | |
889fca73 | 4655 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4656 | return 0; |
fddaface JA |
4657 | } |
4658 | ||
1400e697 PB |
4659 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4660 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4661 | { |
4662 | struct io_sr_msg *sr = &req->sr_msg; | |
4663 | struct iovec __user *uiov; | |
4664 | size_t iov_len; | |
4665 | int ret; | |
4666 | ||
1400e697 PB |
4667 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4668 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4669 | if (ret) |
4670 | return ret; | |
4671 | ||
4672 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4673 | if (iov_len > 1) | |
4674 | return -EINVAL; | |
5476dfed | 4675 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4676 | return -EFAULT; |
5476dfed | 4677 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4678 | iomsg->free_iov = NULL; |
52de1fe1 | 4679 | } else { |
257e84a5 | 4680 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4681 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4682 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4683 | false); |
52de1fe1 JA |
4684 | if (ret > 0) |
4685 | ret = 0; | |
4686 | } | |
4687 | ||
4688 | return ret; | |
4689 | } | |
4690 | ||
4691 | #ifdef CONFIG_COMPAT | |
4692 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4693 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4694 | { |
4695 | struct compat_msghdr __user *msg_compat; | |
4696 | struct io_sr_msg *sr = &req->sr_msg; | |
4697 | struct compat_iovec __user *uiov; | |
4698 | compat_uptr_t ptr; | |
4699 | compat_size_t len; | |
4700 | int ret; | |
4701 | ||
270a5940 | 4702 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4703 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4704 | &ptr, &len); |
4705 | if (ret) | |
4706 | return ret; | |
4707 | ||
4708 | uiov = compat_ptr(ptr); | |
4709 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4710 | compat_ssize_t clen; | |
4711 | ||
4712 | if (len > 1) | |
4713 | return -EINVAL; | |
4714 | if (!access_ok(uiov, sizeof(*uiov))) | |
4715 | return -EFAULT; | |
4716 | if (__get_user(clen, &uiov->iov_len)) | |
4717 | return -EFAULT; | |
4718 | if (clen < 0) | |
4719 | return -EINVAL; | |
2d280bc8 | 4720 | sr->len = clen; |
257e84a5 | 4721 | iomsg->free_iov = NULL; |
52de1fe1 | 4722 | } else { |
257e84a5 | 4723 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4724 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4725 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4726 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4727 | if (ret < 0) |
4728 | return ret; | |
4729 | } | |
4730 | ||
4731 | return 0; | |
4732 | } | |
4733 | #endif | |
4734 | ||
1400e697 PB |
4735 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4736 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4737 | { |
1400e697 | 4738 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4739 | |
4740 | #ifdef CONFIG_COMPAT | |
4741 | if (req->ctx->compat) | |
1400e697 | 4742 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4743 | #endif |
52de1fe1 | 4744 | |
1400e697 | 4745 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4746 | } |
4747 | ||
bcda7baa | 4748 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4749 | bool needs_lock) |
bcda7baa JA |
4750 | { |
4751 | struct io_sr_msg *sr = &req->sr_msg; | |
4752 | struct io_buffer *kbuf; | |
4753 | ||
bcda7baa JA |
4754 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4755 | if (IS_ERR(kbuf)) | |
4756 | return kbuf; | |
4757 | ||
4758 | sr->kbuf = kbuf; | |
4759 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4760 | return kbuf; |
fddaface JA |
4761 | } |
4762 | ||
7fbb1b54 PB |
4763 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4764 | { | |
4765 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4766 | } | |
4767 | ||
3529d8c2 JA |
4768 | static int io_recvmsg_prep(struct io_kiocb *req, |
4769 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4770 | { |
e8c2bc1f | 4771 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4772 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4773 | int ret; |
3529d8c2 | 4774 | |
d2b6f48b PB |
4775 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4776 | return -EINVAL; | |
4777 | ||
3529d8c2 | 4778 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4779 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4780 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4781 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4782 | |
d8768362 JA |
4783 | #ifdef CONFIG_COMPAT |
4784 | if (req->ctx->compat) | |
4785 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4786 | #endif | |
4787 | ||
e8c2bc1f | 4788 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
5f798bea | 4789 | return 0; |
e8c2bc1f | 4790 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4791 | if (!ret) |
4792 | req->flags |= REQ_F_NEED_CLEANUP; | |
4793 | return ret; | |
aa1fa28f JA |
4794 | } |
4795 | ||
889fca73 | 4796 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4797 | { |
6b754c8b | 4798 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4799 | struct socket *sock; |
7fbb1b54 | 4800 | struct io_buffer *kbuf; |
7a7cacba | 4801 | unsigned flags; |
52de1fe1 | 4802 | int ret, cflags = 0; |
45d189c6 | 4803 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4804 | |
dba4a925 | 4805 | sock = sock_from_file(req->file); |
7a7cacba | 4806 | if (unlikely(!sock)) |
dba4a925 | 4807 | return -ENOTSOCK; |
3529d8c2 | 4808 | |
257e84a5 PB |
4809 | kmsg = req->async_data; |
4810 | if (!kmsg) { | |
7a7cacba PB |
4811 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4812 | if (ret) | |
681fda8d | 4813 | return ret; |
7a7cacba PB |
4814 | kmsg = &iomsg; |
4815 | } | |
03b1230c | 4816 | |
bc02ef33 | 4817 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4818 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4819 | if (IS_ERR(kbuf)) |
52de1fe1 | 4820 | return PTR_ERR(kbuf); |
7a7cacba | 4821 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4822 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4823 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4824 | 1, req->sr_msg.len); |
4825 | } | |
52de1fe1 | 4826 | |
7a7cacba PB |
4827 | flags = req->sr_msg.msg_flags; |
4828 | if (flags & MSG_DONTWAIT) | |
4829 | req->flags |= REQ_F_NOWAIT; | |
4830 | else if (force_nonblock) | |
4831 | flags |= MSG_DONTWAIT; | |
e47293fd | 4832 | |
7a7cacba PB |
4833 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4834 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4835 | if (force_nonblock && ret == -EAGAIN) |
4836 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4837 | if (ret == -ERESTARTSYS) |
4838 | ret = -EINTR; | |
03b1230c | 4839 | |
7fbb1b54 PB |
4840 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4841 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4842 | /* fast path, check for non-NULL to avoid function call */ |
4843 | if (kmsg->free_iov) | |
4844 | kfree(kmsg->free_iov); | |
99bc4c38 | 4845 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4846 | if (ret < 0) |
4847 | req_set_fail_links(req); | |
889fca73 | 4848 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4849 | return 0; |
0fa03c62 | 4850 | } |
5d17b4a4 | 4851 | |
889fca73 | 4852 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4853 | { |
6b754c8b | 4854 | struct io_buffer *kbuf; |
7a7cacba PB |
4855 | struct io_sr_msg *sr = &req->sr_msg; |
4856 | struct msghdr msg; | |
4857 | void __user *buf = sr->buf; | |
fddaface | 4858 | struct socket *sock; |
7a7cacba PB |
4859 | struct iovec iov; |
4860 | unsigned flags; | |
bcda7baa | 4861 | int ret, cflags = 0; |
45d189c6 | 4862 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4863 | |
dba4a925 | 4864 | sock = sock_from_file(req->file); |
7a7cacba | 4865 | if (unlikely(!sock)) |
dba4a925 | 4866 | return -ENOTSOCK; |
fddaface | 4867 | |
bc02ef33 | 4868 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4869 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4870 | if (IS_ERR(kbuf)) |
4871 | return PTR_ERR(kbuf); | |
7a7cacba | 4872 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4873 | } |
bcda7baa | 4874 | |
7a7cacba | 4875 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4876 | if (unlikely(ret)) |
4877 | goto out_free; | |
fddaface | 4878 | |
7a7cacba PB |
4879 | msg.msg_name = NULL; |
4880 | msg.msg_control = NULL; | |
4881 | msg.msg_controllen = 0; | |
4882 | msg.msg_namelen = 0; | |
4883 | msg.msg_iocb = NULL; | |
4884 | msg.msg_flags = 0; | |
fddaface | 4885 | |
7a7cacba PB |
4886 | flags = req->sr_msg.msg_flags; |
4887 | if (flags & MSG_DONTWAIT) | |
4888 | req->flags |= REQ_F_NOWAIT; | |
4889 | else if (force_nonblock) | |
4890 | flags |= MSG_DONTWAIT; | |
4891 | ||
4892 | ret = sock_recvmsg(sock, &msg, flags); | |
4893 | if (force_nonblock && ret == -EAGAIN) | |
4894 | return -EAGAIN; | |
4895 | if (ret == -ERESTARTSYS) | |
4896 | ret = -EINTR; | |
14c32eee | 4897 | out_free: |
7fbb1b54 PB |
4898 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4899 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4900 | if (ret < 0) |
4901 | req_set_fail_links(req); | |
889fca73 | 4902 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4903 | return 0; |
fddaface JA |
4904 | } |
4905 | ||
3529d8c2 | 4906 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4907 | { |
8ed8d3c3 JA |
4908 | struct io_accept *accept = &req->accept; |
4909 | ||
14587a46 | 4910 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4911 | return -EINVAL; |
8042d6ce | 4912 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4913 | return -EINVAL; |
4914 | ||
d55e5f5b JA |
4915 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4916 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4917 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4918 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4919 | return 0; |
8ed8d3c3 | 4920 | } |
17f2fe35 | 4921 | |
889fca73 | 4922 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4923 | { |
4924 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4925 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4926 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4927 | int ret; |
4928 | ||
e697deed JX |
4929 | if (req->file->f_flags & O_NONBLOCK) |
4930 | req->flags |= REQ_F_NOWAIT; | |
4931 | ||
8ed8d3c3 | 4932 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4933 | accept->addr_len, accept->flags, |
4934 | accept->nofile); | |
8ed8d3c3 | 4935 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4936 | return -EAGAIN; |
ac45abc0 PB |
4937 | if (ret < 0) { |
4938 | if (ret == -ERESTARTSYS) | |
4939 | ret = -EINTR; | |
4e88d6e7 | 4940 | req_set_fail_links(req); |
ac45abc0 | 4941 | } |
889fca73 | 4942 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4943 | return 0; |
8ed8d3c3 JA |
4944 | } |
4945 | ||
3529d8c2 | 4946 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4947 | { |
3529d8c2 | 4948 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 4949 | struct io_async_connect *io = req->async_data; |
f499a021 | 4950 | |
14587a46 | 4951 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4952 | return -EINVAL; |
4953 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4954 | return -EINVAL; | |
4955 | ||
3529d8c2 JA |
4956 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4957 | conn->addr_len = READ_ONCE(sqe->addr2); | |
4958 | ||
4959 | if (!io) | |
4960 | return 0; | |
4961 | ||
4962 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 4963 | &io->address); |
f499a021 JA |
4964 | } |
4965 | ||
889fca73 | 4966 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4967 | { |
e8c2bc1f | 4968 | struct io_async_connect __io, *io; |
f8e85cf2 | 4969 | unsigned file_flags; |
3fbb51c1 | 4970 | int ret; |
45d189c6 | 4971 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4972 | |
e8c2bc1f JA |
4973 | if (req->async_data) { |
4974 | io = req->async_data; | |
f499a021 | 4975 | } else { |
3529d8c2 JA |
4976 | ret = move_addr_to_kernel(req->connect.addr, |
4977 | req->connect.addr_len, | |
e8c2bc1f | 4978 | &__io.address); |
f499a021 JA |
4979 | if (ret) |
4980 | goto out; | |
4981 | io = &__io; | |
4982 | } | |
4983 | ||
3fbb51c1 JA |
4984 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4985 | ||
e8c2bc1f | 4986 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4987 | req->connect.addr_len, file_flags); |
87f80d62 | 4988 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4989 | if (req->async_data) |
b7bb4f7d | 4990 | return -EAGAIN; |
e8c2bc1f | 4991 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4992 | ret = -ENOMEM; |
4993 | goto out; | |
4994 | } | |
e8c2bc1f JA |
4995 | io = req->async_data; |
4996 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4997 | return -EAGAIN; |
f499a021 | 4998 | } |
f8e85cf2 JA |
4999 | if (ret == -ERESTARTSYS) |
5000 | ret = -EINTR; | |
f499a021 | 5001 | out: |
4e88d6e7 JA |
5002 | if (ret < 0) |
5003 | req_set_fail_links(req); | |
889fca73 | 5004 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5005 | return 0; |
469956e8 Y |
5006 | } |
5007 | #else /* !CONFIG_NET */ | |
5008 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5009 | { | |
f8e85cf2 | 5010 | return -EOPNOTSUPP; |
f8e85cf2 JA |
5011 | } |
5012 | ||
889fca73 | 5013 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
469956e8 Y |
5014 | { |
5015 | return -EOPNOTSUPP; | |
5016 | } | |
5017 | ||
889fca73 | 5018 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
469956e8 Y |
5019 | { |
5020 | return -EOPNOTSUPP; | |
5021 | } | |
5022 | ||
5023 | static int io_recvmsg_prep(struct io_kiocb *req, | |
5024 | const struct io_uring_sqe *sqe) | |
5025 | { | |
5026 | return -EOPNOTSUPP; | |
5027 | } | |
5028 | ||
889fca73 | 5029 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
469956e8 Y |
5030 | { |
5031 | return -EOPNOTSUPP; | |
5032 | } | |
5033 | ||
889fca73 | 5034 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
469956e8 Y |
5035 | { |
5036 | return -EOPNOTSUPP; | |
5037 | } | |
5038 | ||
5039 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5040 | { | |
5041 | return -EOPNOTSUPP; | |
5042 | } | |
5043 | ||
889fca73 | 5044 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
469956e8 Y |
5045 | { |
5046 | return -EOPNOTSUPP; | |
5047 | } | |
ce593a6c | 5048 | |
469956e8 Y |
5049 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5050 | { | |
5051 | return -EOPNOTSUPP; | |
5052 | } | |
5053 | ||
889fca73 | 5054 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
469956e8 | 5055 | { |
f8e85cf2 | 5056 | return -EOPNOTSUPP; |
ce593a6c | 5057 | } |
469956e8 | 5058 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5059 | |
d7718a9d JA |
5060 | struct io_poll_table { |
5061 | struct poll_table_struct pt; | |
5062 | struct io_kiocb *req; | |
5063 | int error; | |
5064 | }; | |
ce593a6c | 5065 | |
d7718a9d JA |
5066 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5067 | __poll_t mask, task_work_func_t func) | |
5068 | { | |
aa96bf8a | 5069 | int ret; |
d7718a9d JA |
5070 | |
5071 | /* for instances that support it check for an event match first: */ | |
5072 | if (mask && !(mask & poll->events)) | |
5073 | return 0; | |
5074 | ||
5075 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5076 | ||
5077 | list_del_init(&poll->wait.entry); | |
5078 | ||
d7718a9d JA |
5079 | req->result = mask; |
5080 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
5081 | percpu_ref_get(&req->ctx->refs); |
5082 | ||
d7718a9d | 5083 | /* |
e3aabf95 JA |
5084 | * If this fails, then the task is exiting. When a task exits, the |
5085 | * work gets canceled, so just cancel this request as well instead | |
5086 | * of executing it. We can't safely execute it anyway, as we may not | |
5087 | * have the needed state needed for it anyway. | |
d7718a9d | 5088 | */ |
355fb9e2 | 5089 | ret = io_req_task_work_add(req); |
aa96bf8a | 5090 | if (unlikely(ret)) { |
e3aabf95 | 5091 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 5092 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 5093 | } |
d7718a9d JA |
5094 | return 1; |
5095 | } | |
5096 | ||
74ce6ce4 JA |
5097 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5098 | __acquires(&req->ctx->completion_lock) | |
5099 | { | |
5100 | struct io_ring_ctx *ctx = req->ctx; | |
5101 | ||
5102 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5103 | struct poll_table_struct pt = { ._key = poll->events }; | |
5104 | ||
5105 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5106 | } | |
5107 | ||
5108 | spin_lock_irq(&ctx->completion_lock); | |
5109 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5110 | add_wait_queue(poll->head, &poll->wait); | |
5111 | return true; | |
5112 | } | |
5113 | ||
5114 | return false; | |
5115 | } | |
5116 | ||
d4e7cd36 | 5117 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5118 | { |
e8c2bc1f | 5119 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5120 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5121 | return req->async_data; |
d4e7cd36 JA |
5122 | return req->apoll->double_poll; |
5123 | } | |
5124 | ||
5125 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5126 | { | |
5127 | if (req->opcode == IORING_OP_POLL_ADD) | |
5128 | return &req->poll; | |
5129 | return &req->apoll->poll; | |
5130 | } | |
5131 | ||
5132 | static void io_poll_remove_double(struct io_kiocb *req) | |
5133 | { | |
5134 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5135 | |
5136 | lockdep_assert_held(&req->ctx->completion_lock); | |
5137 | ||
5138 | if (poll && poll->head) { | |
5139 | struct wait_queue_head *head = poll->head; | |
5140 | ||
5141 | spin_lock(&head->lock); | |
5142 | list_del_init(&poll->wait.entry); | |
5143 | if (poll->wait.private) | |
5144 | refcount_dec(&req->refs); | |
5145 | poll->head = NULL; | |
5146 | spin_unlock(&head->lock); | |
5147 | } | |
5148 | } | |
5149 | ||
5150 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5151 | { | |
5152 | struct io_ring_ctx *ctx = req->ctx; | |
5153 | ||
d4e7cd36 | 5154 | io_poll_remove_double(req); |
18bceab1 JA |
5155 | req->poll.done = true; |
5156 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5157 | io_commit_cqring(ctx); | |
5158 | } | |
5159 | ||
dd221f46 | 5160 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5161 | { |
dd221f46 | 5162 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5163 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5164 | struct io_kiocb *nxt; |
18bceab1 JA |
5165 | |
5166 | if (io_poll_rewait(req, &req->poll)) { | |
5167 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5168 | } else { |
5169 | hash_del(&req->hash_node); | |
5170 | io_poll_complete(req, req->result, 0); | |
5171 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5172 | |
dd221f46 PB |
5173 | nxt = io_put_req_find_next(req); |
5174 | io_cqring_ev_posted(ctx); | |
5175 | if (nxt) | |
5176 | __io_req_task_submit(nxt); | |
5177 | } | |
18bceab1 | 5178 | |
6d816e08 | 5179 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5180 | } |
5181 | ||
5182 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5183 | int sync, void *key) | |
5184 | { | |
5185 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5186 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5187 | __poll_t mask = key_to_poll(key); |
5188 | ||
5189 | /* for instances that support it check for an event match first: */ | |
5190 | if (mask && !(mask & poll->events)) | |
5191 | return 0; | |
5192 | ||
8706e04e JA |
5193 | list_del_init(&wait->entry); |
5194 | ||
807abcb0 | 5195 | if (poll && poll->head) { |
18bceab1 JA |
5196 | bool done; |
5197 | ||
807abcb0 JA |
5198 | spin_lock(&poll->head->lock); |
5199 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5200 | if (!done) |
807abcb0 | 5201 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5202 | /* make sure double remove sees this as being gone */ |
5203 | wait->private = NULL; | |
807abcb0 | 5204 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5205 | if (!done) { |
5206 | /* use wait func handler, so it matches the rq type */ | |
5207 | poll->wait.func(&poll->wait, mode, sync, key); | |
5208 | } | |
18bceab1 JA |
5209 | } |
5210 | refcount_dec(&req->refs); | |
5211 | return 1; | |
5212 | } | |
5213 | ||
5214 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5215 | wait_queue_func_t wake_func) | |
5216 | { | |
5217 | poll->head = NULL; | |
5218 | poll->done = false; | |
5219 | poll->canceled = false; | |
5220 | poll->events = events; | |
5221 | INIT_LIST_HEAD(&poll->wait.entry); | |
5222 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5223 | } | |
5224 | ||
5225 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5226 | struct wait_queue_head *head, |
5227 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5228 | { |
5229 | struct io_kiocb *req = pt->req; | |
5230 | ||
5231 | /* | |
5232 | * If poll->head is already set, it's because the file being polled | |
5233 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5234 | * for write). Setup a separate io_poll_iocb if this happens. | |
5235 | */ | |
5236 | if (unlikely(poll->head)) { | |
58852d4d PB |
5237 | struct io_poll_iocb *poll_one = poll; |
5238 | ||
18bceab1 | 5239 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5240 | if (*poll_ptr) { |
18bceab1 JA |
5241 | pt->error = -EINVAL; |
5242 | return; | |
5243 | } | |
5244 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5245 | if (!poll) { | |
5246 | pt->error = -ENOMEM; | |
5247 | return; | |
5248 | } | |
58852d4d | 5249 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5250 | refcount_inc(&req->refs); |
5251 | poll->wait.private = req; | |
807abcb0 | 5252 | *poll_ptr = poll; |
18bceab1 JA |
5253 | } |
5254 | ||
5255 | pt->error = 0; | |
5256 | poll->head = head; | |
a31eb4a2 JX |
5257 | |
5258 | if (poll->events & EPOLLEXCLUSIVE) | |
5259 | add_wait_queue_exclusive(head, &poll->wait); | |
5260 | else | |
5261 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5262 | } |
5263 | ||
5264 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5265 | struct poll_table_struct *p) | |
5266 | { | |
5267 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5268 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5269 | |
807abcb0 | 5270 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5271 | } |
5272 | ||
d7718a9d JA |
5273 | static void io_async_task_func(struct callback_head *cb) |
5274 | { | |
5275 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5276 | struct async_poll *apoll = req->apoll; | |
5277 | struct io_ring_ctx *ctx = req->ctx; | |
5278 | ||
5279 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5280 | ||
74ce6ce4 | 5281 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5282 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5283 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5284 | return; |
d7718a9d JA |
5285 | } |
5286 | ||
31067255 | 5287 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5288 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5289 | hash_del(&req->hash_node); |
2bae047e | 5290 | |
d4e7cd36 | 5291 | io_poll_remove_double(req); |
74ce6ce4 JA |
5292 | spin_unlock_irq(&ctx->completion_lock); |
5293 | ||
0be0b0e3 PB |
5294 | if (!READ_ONCE(apoll->poll.canceled)) |
5295 | __io_req_task_submit(req); | |
5296 | else | |
5297 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5298 | |
6d816e08 | 5299 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5300 | kfree(apoll->double_poll); |
31067255 | 5301 | kfree(apoll); |
d7718a9d JA |
5302 | } |
5303 | ||
5304 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5305 | void *key) | |
5306 | { | |
5307 | struct io_kiocb *req = wait->private; | |
5308 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5309 | ||
5310 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5311 | key_to_poll(key)); | |
5312 | ||
5313 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5314 | } | |
5315 | ||
5316 | static void io_poll_req_insert(struct io_kiocb *req) | |
5317 | { | |
5318 | struct io_ring_ctx *ctx = req->ctx; | |
5319 | struct hlist_head *list; | |
5320 | ||
5321 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5322 | hlist_add_head(&req->hash_node, list); | |
5323 | } | |
5324 | ||
5325 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5326 | struct io_poll_iocb *poll, | |
5327 | struct io_poll_table *ipt, __poll_t mask, | |
5328 | wait_queue_func_t wake_func) | |
5329 | __acquires(&ctx->completion_lock) | |
5330 | { | |
5331 | struct io_ring_ctx *ctx = req->ctx; | |
5332 | bool cancel = false; | |
5333 | ||
4d52f338 | 5334 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5335 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5336 | poll->file = req->file; |
18bceab1 | 5337 | poll->wait.private = req; |
d7718a9d JA |
5338 | |
5339 | ipt->pt._key = mask; | |
5340 | ipt->req = req; | |
5341 | ipt->error = -EINVAL; | |
5342 | ||
d7718a9d JA |
5343 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5344 | ||
5345 | spin_lock_irq(&ctx->completion_lock); | |
5346 | if (likely(poll->head)) { | |
5347 | spin_lock(&poll->head->lock); | |
5348 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5349 | if (ipt->error) | |
5350 | cancel = true; | |
5351 | ipt->error = 0; | |
5352 | mask = 0; | |
5353 | } | |
5354 | if (mask || ipt->error) | |
5355 | list_del_init(&poll->wait.entry); | |
5356 | else if (cancel) | |
5357 | WRITE_ONCE(poll->canceled, true); | |
5358 | else if (!poll->done) /* actually waiting for an event */ | |
5359 | io_poll_req_insert(req); | |
5360 | spin_unlock(&poll->head->lock); | |
5361 | } | |
5362 | ||
5363 | return mask; | |
5364 | } | |
5365 | ||
5366 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5367 | { | |
5368 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5369 | struct io_ring_ctx *ctx = req->ctx; | |
5370 | struct async_poll *apoll; | |
5371 | struct io_poll_table ipt; | |
5372 | __poll_t mask, ret; | |
9dab14b8 | 5373 | int rw; |
d7718a9d JA |
5374 | |
5375 | if (!req->file || !file_can_poll(req->file)) | |
5376 | return false; | |
24c74678 | 5377 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5378 | return false; |
9dab14b8 JA |
5379 | if (def->pollin) |
5380 | rw = READ; | |
5381 | else if (def->pollout) | |
5382 | rw = WRITE; | |
5383 | else | |
5384 | return false; | |
5385 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5386 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5387 | return false; |
5388 | ||
5389 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5390 | if (unlikely(!apoll)) | |
5391 | return false; | |
807abcb0 | 5392 | apoll->double_poll = NULL; |
d7718a9d JA |
5393 | |
5394 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5395 | req->apoll = apoll; |
d7718a9d | 5396 | |
8755d97a | 5397 | mask = 0; |
d7718a9d | 5398 | if (def->pollin) |
8755d97a | 5399 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5400 | if (def->pollout) |
5401 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5402 | |
5403 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5404 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5405 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5406 | mask &= ~POLLIN; | |
5407 | ||
d7718a9d JA |
5408 | mask |= POLLERR | POLLPRI; |
5409 | ||
5410 | ipt.pt._qproc = io_async_queue_proc; | |
5411 | ||
5412 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5413 | io_async_wake); | |
a36da65c | 5414 | if (ret || ipt.error) { |
d4e7cd36 | 5415 | io_poll_remove_double(req); |
d7718a9d | 5416 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5417 | kfree(apoll->double_poll); |
d7718a9d JA |
5418 | kfree(apoll); |
5419 | return false; | |
5420 | } | |
5421 | spin_unlock_irq(&ctx->completion_lock); | |
5422 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5423 | apoll->poll.events); | |
5424 | return true; | |
5425 | } | |
5426 | ||
5427 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5428 | struct io_poll_iocb *poll) | |
221c5eb2 | 5429 | { |
b41e9852 | 5430 | bool do_complete = false; |
221c5eb2 JA |
5431 | |
5432 | spin_lock(&poll->head->lock); | |
5433 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5434 | if (!list_empty(&poll->wait.entry)) { |
5435 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5436 | do_complete = true; |
221c5eb2 JA |
5437 | } |
5438 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5439 | hash_del(&req->hash_node); |
d7718a9d JA |
5440 | return do_complete; |
5441 | } | |
5442 | ||
5443 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5444 | { | |
5445 | bool do_complete; | |
5446 | ||
d4e7cd36 JA |
5447 | io_poll_remove_double(req); |
5448 | ||
d7718a9d JA |
5449 | if (req->opcode == IORING_OP_POLL_ADD) { |
5450 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5451 | } else { | |
3bfa5bcb JA |
5452 | struct async_poll *apoll = req->apoll; |
5453 | ||
d7718a9d | 5454 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5455 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5456 | if (do_complete) { | |
d7718a9d | 5457 | io_put_req(req); |
807abcb0 | 5458 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5459 | kfree(apoll); |
5460 | } | |
b1f573bd XW |
5461 | } |
5462 | ||
b41e9852 JA |
5463 | if (do_complete) { |
5464 | io_cqring_fill_event(req, -ECANCELED); | |
5465 | io_commit_cqring(req->ctx); | |
f254ac04 | 5466 | req_set_fail_links(req); |
216578e5 | 5467 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5468 | } |
5469 | ||
5470 | return do_complete; | |
221c5eb2 JA |
5471 | } |
5472 | ||
76e1b642 JA |
5473 | /* |
5474 | * Returns true if we found and killed one or more poll requests | |
5475 | */ | |
6b81928d PB |
5476 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5477 | struct files_struct *files) | |
221c5eb2 | 5478 | { |
78076bb6 | 5479 | struct hlist_node *tmp; |
221c5eb2 | 5480 | struct io_kiocb *req; |
8e2e1faf | 5481 | int posted = 0, i; |
221c5eb2 JA |
5482 | |
5483 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5484 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5485 | struct hlist_head *list; | |
5486 | ||
5487 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5488 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5489 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5490 | posted += io_poll_remove_one(req); |
5491 | } | |
221c5eb2 JA |
5492 | } |
5493 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5494 | |
8e2e1faf JA |
5495 | if (posted) |
5496 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5497 | |
5498 | return posted != 0; | |
221c5eb2 JA |
5499 | } |
5500 | ||
47f46768 JA |
5501 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5502 | { | |
78076bb6 | 5503 | struct hlist_head *list; |
47f46768 JA |
5504 | struct io_kiocb *req; |
5505 | ||
78076bb6 JA |
5506 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5507 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5508 | if (sqe_addr != req->user_data) |
5509 | continue; | |
5510 | if (io_poll_remove_one(req)) | |
eac406c6 | 5511 | return 0; |
b41e9852 | 5512 | return -EALREADY; |
47f46768 JA |
5513 | } |
5514 | ||
5515 | return -ENOENT; | |
5516 | } | |
5517 | ||
3529d8c2 JA |
5518 | static int io_poll_remove_prep(struct io_kiocb *req, |
5519 | const struct io_uring_sqe *sqe) | |
0969e783 | 5520 | { |
0969e783 JA |
5521 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5522 | return -EINVAL; | |
5523 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5524 | sqe->poll_events) | |
5525 | return -EINVAL; | |
5526 | ||
018043be | 5527 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5528 | return 0; |
5529 | } | |
5530 | ||
221c5eb2 JA |
5531 | /* |
5532 | * Find a running poll command that matches one specified in sqe->addr, | |
5533 | * and remove it if found. | |
5534 | */ | |
61e98203 | 5535 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5536 | { |
5537 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5538 | int ret; |
221c5eb2 | 5539 | |
221c5eb2 | 5540 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5541 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5542 | spin_unlock_irq(&ctx->completion_lock); |
5543 | ||
4e88d6e7 JA |
5544 | if (ret < 0) |
5545 | req_set_fail_links(req); | |
e1e16097 | 5546 | io_req_complete(req, ret); |
221c5eb2 JA |
5547 | return 0; |
5548 | } | |
5549 | ||
221c5eb2 JA |
5550 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5551 | void *key) | |
5552 | { | |
c2f2eb7d JA |
5553 | struct io_kiocb *req = wait->private; |
5554 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5555 | |
d7718a9d | 5556 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5557 | } |
5558 | ||
221c5eb2 JA |
5559 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5560 | struct poll_table_struct *p) | |
5561 | { | |
5562 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5563 | ||
e8c2bc1f | 5564 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5565 | } |
5566 | ||
3529d8c2 | 5567 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5568 | { |
5569 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5570 | u32 events; |
221c5eb2 JA |
5571 | |
5572 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5573 | return -EINVAL; | |
5574 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5575 | return -EINVAL; | |
5576 | ||
5769a351 JX |
5577 | events = READ_ONCE(sqe->poll32_events); |
5578 | #ifdef __BIG_ENDIAN | |
5579 | events = swahw32(events); | |
5580 | #endif | |
a31eb4a2 JX |
5581 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5582 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5583 | return 0; |
5584 | } | |
5585 | ||
61e98203 | 5586 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5587 | { |
5588 | struct io_poll_iocb *poll = &req->poll; | |
5589 | struct io_ring_ctx *ctx = req->ctx; | |
5590 | struct io_poll_table ipt; | |
0969e783 | 5591 | __poll_t mask; |
0969e783 | 5592 | |
d7718a9d | 5593 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5594 | |
d7718a9d JA |
5595 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5596 | io_poll_wake); | |
221c5eb2 | 5597 | |
8c838788 | 5598 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5599 | ipt.error = 0; |
b0dd8a41 | 5600 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5601 | } |
221c5eb2 JA |
5602 | spin_unlock_irq(&ctx->completion_lock); |
5603 | ||
8c838788 JA |
5604 | if (mask) { |
5605 | io_cqring_ev_posted(ctx); | |
014db007 | 5606 | io_put_req(req); |
221c5eb2 | 5607 | } |
8c838788 | 5608 | return ipt.error; |
221c5eb2 JA |
5609 | } |
5610 | ||
5262f567 JA |
5611 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5612 | { | |
ad8a48ac JA |
5613 | struct io_timeout_data *data = container_of(timer, |
5614 | struct io_timeout_data, timer); | |
5615 | struct io_kiocb *req = data->req; | |
5616 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5617 | unsigned long flags; |
5618 | ||
5262f567 | 5619 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5620 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5621 | atomic_set(&req->ctx->cq_timeouts, |
5622 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5623 | ||
78e19bbe | 5624 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5625 | io_commit_cqring(ctx); |
5626 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5627 | ||
5628 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5629 | req_set_fail_links(req); |
5262f567 JA |
5630 | io_put_req(req); |
5631 | return HRTIMER_NORESTART; | |
5632 | } | |
5633 | ||
fbd15848 PB |
5634 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5635 | __u64 user_data) | |
f254ac04 | 5636 | { |
fbd15848 | 5637 | struct io_timeout_data *io; |
47f46768 JA |
5638 | struct io_kiocb *req; |
5639 | int ret = -ENOENT; | |
f254ac04 | 5640 | |
135fcde8 | 5641 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5642 | if (user_data == req->user_data) { |
47f46768 JA |
5643 | ret = 0; |
5644 | break; | |
5645 | } | |
5646 | } | |
5647 | ||
5648 | if (ret == -ENOENT) | |
fbd15848 PB |
5649 | return ERR_PTR(ret); |
5650 | ||
5651 | io = req->async_data; | |
e8c2bc1f | 5652 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5653 | if (ret == -1) |
fbd15848 | 5654 | return ERR_PTR(-EALREADY); |
a71976f3 | 5655 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5656 | return req; |
5657 | } | |
47f46768 | 5658 | |
fbd15848 PB |
5659 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5660 | { | |
5661 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5662 | ||
5663 | if (IS_ERR(req)) | |
5664 | return PTR_ERR(req); | |
f254ac04 JA |
5665 | |
5666 | req_set_fail_links(req); | |
f254ac04 | 5667 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5668 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5669 | return 0; |
5670 | } | |
5671 | ||
9c8e11b3 PB |
5672 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5673 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5674 | { |
9c8e11b3 PB |
5675 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5676 | struct io_timeout_data *data; | |
47f46768 | 5677 | |
9c8e11b3 PB |
5678 | if (IS_ERR(req)) |
5679 | return PTR_ERR(req); | |
47f46768 | 5680 | |
9c8e11b3 PB |
5681 | req->timeout.off = 0; /* noseq */ |
5682 | data = req->async_data; | |
5683 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5684 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5685 | data->timer.function = io_timeout_fn; | |
5686 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5687 | return 0; | |
47f46768 JA |
5688 | } |
5689 | ||
3529d8c2 JA |
5690 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5691 | const struct io_uring_sqe *sqe) | |
b29472ee | 5692 | { |
9c8e11b3 PB |
5693 | struct io_timeout_rem *tr = &req->timeout_rem; |
5694 | ||
b29472ee JA |
5695 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5696 | return -EINVAL; | |
61710e43 DA |
5697 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5698 | return -EINVAL; | |
9c8e11b3 | 5699 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5700 | return -EINVAL; |
5701 | ||
9c8e11b3 PB |
5702 | tr->addr = READ_ONCE(sqe->addr); |
5703 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5704 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5705 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5706 | return -EINVAL; | |
5707 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5708 | return -EFAULT; | |
5709 | } else if (tr->flags) { | |
5710 | /* timeout removal doesn't support flags */ | |
b29472ee | 5711 | return -EINVAL; |
9c8e11b3 | 5712 | } |
b29472ee | 5713 | |
b29472ee JA |
5714 | return 0; |
5715 | } | |
5716 | ||
8662daec PB |
5717 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5718 | { | |
5719 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5720 | : HRTIMER_MODE_REL; | |
5721 | } | |
5722 | ||
11365043 JA |
5723 | /* |
5724 | * Remove or update an existing timeout command | |
5725 | */ | |
61e98203 | 5726 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5727 | { |
9c8e11b3 | 5728 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5729 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5730 | int ret; |
11365043 | 5731 | |
11365043 | 5732 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5733 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5734 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5735 | else |
5736 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5737 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5738 | |
47f46768 | 5739 | io_cqring_fill_event(req, ret); |
11365043 JA |
5740 | io_commit_cqring(ctx); |
5741 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5742 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5743 | if (ret < 0) |
5744 | req_set_fail_links(req); | |
ec9c02ad | 5745 | io_put_req(req); |
11365043 | 5746 | return 0; |
5262f567 JA |
5747 | } |
5748 | ||
3529d8c2 | 5749 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5750 | bool is_timeout_link) |
5262f567 | 5751 | { |
ad8a48ac | 5752 | struct io_timeout_data *data; |
a41525ab | 5753 | unsigned flags; |
56080b02 | 5754 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5755 | |
ad8a48ac | 5756 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5757 | return -EINVAL; |
ad8a48ac | 5758 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5759 | return -EINVAL; |
56080b02 | 5760 | if (off && is_timeout_link) |
2d28390a | 5761 | return -EINVAL; |
a41525ab JA |
5762 | flags = READ_ONCE(sqe->timeout_flags); |
5763 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5764 | return -EINVAL; |
bdf20073 | 5765 | |
bfe68a22 | 5766 | req->timeout.off = off; |
26a61679 | 5767 | |
e8c2bc1f | 5768 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5769 | return -ENOMEM; |
5770 | ||
e8c2bc1f | 5771 | data = req->async_data; |
ad8a48ac | 5772 | data->req = req; |
ad8a48ac JA |
5773 | |
5774 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5775 | return -EFAULT; |
5776 | ||
8662daec | 5777 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5778 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5779 | return 0; | |
5780 | } | |
5781 | ||
61e98203 | 5782 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5783 | { |
ad8a48ac | 5784 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5785 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5786 | struct list_head *entry; |
bfe68a22 | 5787 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5788 | |
733f5c95 | 5789 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5790 | |
5262f567 JA |
5791 | /* |
5792 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5793 | * timeout event to be satisfied. If it isn't set, then this is |
5794 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5795 | */ |
8eb7e2d0 | 5796 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5797 | entry = ctx->timeout_list.prev; |
5798 | goto add; | |
5799 | } | |
5262f567 | 5800 | |
bfe68a22 PB |
5801 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5802 | req->timeout.target_seq = tail + off; | |
5262f567 | 5803 | |
f010505b MDG |
5804 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5805 | * This is safe because ->completion_lock is held, and submissions | |
5806 | * and completions are never mixed in the same ->completion_lock section. | |
5807 | */ | |
5808 | ctx->cq_last_tm_flush = tail; | |
5809 | ||
5262f567 JA |
5810 | /* |
5811 | * Insertion sort, ensuring the first entry in the list is always | |
5812 | * the one we need first. | |
5813 | */ | |
5262f567 | 5814 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5815 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5816 | timeout.list); | |
5262f567 | 5817 | |
8eb7e2d0 | 5818 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5819 | continue; |
bfe68a22 PB |
5820 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5821 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5822 | break; |
5823 | } | |
93bd25bb | 5824 | add: |
135fcde8 | 5825 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5826 | data->timer.function = io_timeout_fn; |
5827 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5828 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5829 | return 0; |
5830 | } | |
5262f567 | 5831 | |
62755e35 JA |
5832 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5833 | { | |
5834 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5835 | ||
5836 | return req->user_data == (unsigned long) data; | |
5837 | } | |
5838 | ||
e977d6d3 | 5839 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5840 | { |
62755e35 | 5841 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5842 | int ret = 0; |
5843 | ||
4f26bda1 | 5844 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5845 | switch (cancel_ret) { |
5846 | case IO_WQ_CANCEL_OK: | |
5847 | ret = 0; | |
5848 | break; | |
5849 | case IO_WQ_CANCEL_RUNNING: | |
5850 | ret = -EALREADY; | |
5851 | break; | |
5852 | case IO_WQ_CANCEL_NOTFOUND: | |
5853 | ret = -ENOENT; | |
5854 | break; | |
5855 | } | |
5856 | ||
e977d6d3 JA |
5857 | return ret; |
5858 | } | |
5859 | ||
47f46768 JA |
5860 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5861 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5862 | int success_ret) |
47f46768 JA |
5863 | { |
5864 | unsigned long flags; | |
5865 | int ret; | |
5866 | ||
5867 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5868 | if (ret != -ENOENT) { | |
5869 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5870 | goto done; | |
5871 | } | |
5872 | ||
5873 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5874 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5875 | if (ret != -ENOENT) | |
5876 | goto done; | |
5877 | ret = io_poll_cancel(ctx, sqe_addr); | |
5878 | done: | |
b0dd8a41 JA |
5879 | if (!ret) |
5880 | ret = success_ret; | |
47f46768 JA |
5881 | io_cqring_fill_event(req, ret); |
5882 | io_commit_cqring(ctx); | |
5883 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5884 | io_cqring_ev_posted(ctx); | |
5885 | ||
4e88d6e7 JA |
5886 | if (ret < 0) |
5887 | req_set_fail_links(req); | |
014db007 | 5888 | io_put_req(req); |
47f46768 JA |
5889 | } |
5890 | ||
3529d8c2 JA |
5891 | static int io_async_cancel_prep(struct io_kiocb *req, |
5892 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5893 | { |
fbf23849 | 5894 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5895 | return -EINVAL; |
61710e43 DA |
5896 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5897 | return -EINVAL; | |
5898 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5899 | return -EINVAL; |
5900 | ||
fbf23849 JA |
5901 | req->cancel.addr = READ_ONCE(sqe->addr); |
5902 | return 0; | |
5903 | } | |
5904 | ||
61e98203 | 5905 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5906 | { |
5907 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5908 | |
014db007 | 5909 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5910 | return 0; |
5911 | } | |
5912 | ||
269bbe5f | 5913 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5914 | const struct io_uring_sqe *sqe) |
5915 | { | |
6ca56f84 JA |
5916 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5917 | return -EINVAL; | |
61710e43 DA |
5918 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5919 | return -EINVAL; | |
5920 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5921 | return -EINVAL; |
5922 | ||
269bbe5f BM |
5923 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5924 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5925 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5926 | return -EINVAL; |
269bbe5f | 5927 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5928 | return 0; |
5929 | } | |
5930 | ||
889fca73 | 5931 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5932 | { |
5933 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5934 | struct io_uring_rsrc_update up; |
05f3fb3c | 5935 | int ret; |
fbf23849 | 5936 | |
45d189c6 | 5937 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5938 | return -EAGAIN; |
05f3fb3c | 5939 | |
269bbe5f BM |
5940 | up.offset = req->rsrc_update.offset; |
5941 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5942 | |
5943 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5944 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5945 | mutex_unlock(&ctx->uring_lock); |
5946 | ||
5947 | if (ret < 0) | |
5948 | req_set_fail_links(req); | |
889fca73 | 5949 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5950 | return 0; |
5951 | } | |
5952 | ||
bfe76559 | 5953 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5954 | { |
d625c6ee | 5955 | switch (req->opcode) { |
e781573e | 5956 | case IORING_OP_NOP: |
bfe76559 | 5957 | return 0; |
f67676d1 JA |
5958 | case IORING_OP_READV: |
5959 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5960 | case IORING_OP_READ: |
bfe76559 | 5961 | return io_read_prep(req, sqe); |
f67676d1 JA |
5962 | case IORING_OP_WRITEV: |
5963 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5964 | case IORING_OP_WRITE: |
bfe76559 | 5965 | return io_write_prep(req, sqe); |
0969e783 | 5966 | case IORING_OP_POLL_ADD: |
bfe76559 | 5967 | return io_poll_add_prep(req, sqe); |
0969e783 | 5968 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5969 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5970 | case IORING_OP_FSYNC: |
bfe76559 | 5971 | return io_prep_fsync(req, sqe); |
8ed8d3c3 | 5972 | case IORING_OP_SYNC_FILE_RANGE: |
bfe76559 | 5973 | return io_prep_sfr(req, sqe); |
03b1230c | 5974 | case IORING_OP_SENDMSG: |
fddaface | 5975 | case IORING_OP_SEND: |
bfe76559 | 5976 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5977 | case IORING_OP_RECVMSG: |
fddaface | 5978 | case IORING_OP_RECV: |
bfe76559 | 5979 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5980 | case IORING_OP_CONNECT: |
bfe76559 | 5981 | return io_connect_prep(req, sqe); |
2d28390a | 5982 | case IORING_OP_TIMEOUT: |
bfe76559 | 5983 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5984 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5985 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5986 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5987 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5988 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5989 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5990 | case IORING_OP_ACCEPT: |
bfe76559 | 5991 | return io_accept_prep(req, sqe); |
d63d1b5e | 5992 | case IORING_OP_FALLOCATE: |
bfe76559 | 5993 | return io_fallocate_prep(req, sqe); |
15b71abe | 5994 | case IORING_OP_OPENAT: |
bfe76559 | 5995 | return io_openat_prep(req, sqe); |
b5dba59e | 5996 | case IORING_OP_CLOSE: |
bfe76559 | 5997 | return io_close_prep(req, sqe); |
05f3fb3c | 5998 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5999 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6000 | case IORING_OP_STATX: |
bfe76559 | 6001 | return io_statx_prep(req, sqe); |
4840e418 | 6002 | case IORING_OP_FADVISE: |
bfe76559 | 6003 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6004 | case IORING_OP_MADVISE: |
bfe76559 | 6005 | return io_madvise_prep(req, sqe); |
cebdb986 | 6006 | case IORING_OP_OPENAT2: |
bfe76559 | 6007 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6008 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6009 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6010 | case IORING_OP_SPLICE: |
bfe76559 | 6011 | return io_splice_prep(req, sqe); |
ddf0322d | 6012 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6013 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6014 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6015 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6016 | case IORING_OP_TEE: |
bfe76559 | 6017 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6018 | case IORING_OP_SHUTDOWN: |
6019 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6020 | case IORING_OP_RENAMEAT: |
6021 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6022 | case IORING_OP_UNLINKAT: |
6023 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6024 | } |
6025 | ||
bfe76559 PB |
6026 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6027 | req->opcode); | |
6028 | return-EINVAL; | |
6029 | } | |
6030 | ||
6031 | static int io_req_defer_prep(struct io_kiocb *req, | |
6032 | const struct io_uring_sqe *sqe) | |
6033 | { | |
bfe76559 PB |
6034 | if (!sqe) |
6035 | return 0; | |
6036 | if (io_alloc_async_data(req)) | |
6037 | return -EAGAIN; | |
bfe76559 | 6038 | return io_req_prep(req, sqe); |
f67676d1 JA |
6039 | } |
6040 | ||
9cf7c104 PB |
6041 | static u32 io_get_sequence(struct io_kiocb *req) |
6042 | { | |
6043 | struct io_kiocb *pos; | |
6044 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6045 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6046 | |
f2f87370 PB |
6047 | io_for_each_link(pos, req) |
6048 | nr_reqs++; | |
9cf7c104 PB |
6049 | |
6050 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6051 | return total_submitted - nr_reqs; | |
6052 | } | |
6053 | ||
3529d8c2 | 6054 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 6055 | { |
a197f664 | 6056 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6057 | struct io_defer_entry *de; |
f67676d1 | 6058 | int ret; |
9cf7c104 | 6059 | u32 seq; |
de0617e4 | 6060 | |
9d858b21 | 6061 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6062 | if (likely(list_empty_careful(&ctx->defer_list) && |
6063 | !(req->flags & REQ_F_IO_DRAIN))) | |
6064 | return 0; | |
6065 | ||
6066 | seq = io_get_sequence(req); | |
6067 | /* Still a chance to pass the sequence check */ | |
6068 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6069 | return 0; |
6070 | ||
e8c2bc1f | 6071 | if (!req->async_data) { |
650b5481 | 6072 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6073 | if (ret) |
650b5481 PB |
6074 | return ret; |
6075 | } | |
cbdcb435 | 6076 | io_prep_async_link(req); |
27dc8338 PB |
6077 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6078 | if (!de) | |
6079 | return -ENOMEM; | |
2d28390a | 6080 | |
de0617e4 | 6081 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6082 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6083 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6084 | kfree(de); |
ae34817b PB |
6085 | io_queue_async_work(req); |
6086 | return -EIOCBQUEUED; | |
de0617e4 JA |
6087 | } |
6088 | ||
915967f6 | 6089 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6090 | de->req = req; |
9cf7c104 | 6091 | de->seq = seq; |
27dc8338 | 6092 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6093 | spin_unlock_irq(&ctx->completion_lock); |
6094 | return -EIOCBQUEUED; | |
6095 | } | |
6096 | ||
3ca405eb | 6097 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6098 | { |
0e1b6fe3 PB |
6099 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6100 | switch (req->opcode) { | |
6101 | case IORING_OP_READV: | |
6102 | case IORING_OP_READ_FIXED: | |
6103 | case IORING_OP_READ: | |
bcda7baa | 6104 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6105 | break; |
6106 | case IORING_OP_RECVMSG: | |
6107 | case IORING_OP_RECV: | |
bcda7baa | 6108 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6109 | break; |
6110 | } | |
6111 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6112 | } |
6113 | ||
0e1b6fe3 PB |
6114 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6115 | switch (req->opcode) { | |
6116 | case IORING_OP_READV: | |
6117 | case IORING_OP_READ_FIXED: | |
6118 | case IORING_OP_READ: | |
6119 | case IORING_OP_WRITEV: | |
6120 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6121 | case IORING_OP_WRITE: { |
6122 | struct io_async_rw *io = req->async_data; | |
6123 | if (io->free_iovec) | |
6124 | kfree(io->free_iovec); | |
0e1b6fe3 | 6125 | break; |
e8c2bc1f | 6126 | } |
0e1b6fe3 | 6127 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6128 | case IORING_OP_SENDMSG: { |
6129 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6130 | |
6131 | kfree(io->free_iov); | |
0e1b6fe3 | 6132 | break; |
e8c2bc1f | 6133 | } |
0e1b6fe3 PB |
6134 | case IORING_OP_SPLICE: |
6135 | case IORING_OP_TEE: | |
6136 | io_put_file(req, req->splice.file_in, | |
6137 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6138 | break; | |
f3cd4850 JA |
6139 | case IORING_OP_OPENAT: |
6140 | case IORING_OP_OPENAT2: | |
6141 | if (req->open.filename) | |
6142 | putname(req->open.filename); | |
6143 | break; | |
80a261fd JA |
6144 | case IORING_OP_RENAMEAT: |
6145 | putname(req->rename.oldpath); | |
6146 | putname(req->rename.newpath); | |
6147 | break; | |
14a1143b JA |
6148 | case IORING_OP_UNLINKAT: |
6149 | putname(req->unlink.filename); | |
6150 | break; | |
0e1b6fe3 PB |
6151 | } |
6152 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6153 | } |
99bc4c38 PB |
6154 | } |
6155 | ||
889fca73 | 6156 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6157 | { |
a197f664 | 6158 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6159 | int ret; |
2b188cc1 | 6160 | |
d625c6ee | 6161 | switch (req->opcode) { |
2b188cc1 | 6162 | case IORING_OP_NOP: |
889fca73 | 6163 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6164 | break; |
6165 | case IORING_OP_READV: | |
edafccee | 6166 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6167 | case IORING_OP_READ: |
889fca73 | 6168 | ret = io_read(req, issue_flags); |
edafccee | 6169 | break; |
3529d8c2 | 6170 | case IORING_OP_WRITEV: |
edafccee | 6171 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6172 | case IORING_OP_WRITE: |
889fca73 | 6173 | ret = io_write(req, issue_flags); |
2b188cc1 | 6174 | break; |
c992fe29 | 6175 | case IORING_OP_FSYNC: |
45d189c6 | 6176 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6177 | break; |
221c5eb2 | 6178 | case IORING_OP_POLL_ADD: |
61e98203 | 6179 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6180 | break; |
6181 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 6182 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 6183 | break; |
5d17b4a4 | 6184 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6185 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6186 | break; |
0fa03c62 | 6187 | case IORING_OP_SENDMSG: |
889fca73 | 6188 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6189 | break; |
fddaface | 6190 | case IORING_OP_SEND: |
889fca73 | 6191 | ret = io_send(req, issue_flags); |
0fa03c62 | 6192 | break; |
aa1fa28f | 6193 | case IORING_OP_RECVMSG: |
889fca73 | 6194 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6195 | break; |
fddaface | 6196 | case IORING_OP_RECV: |
889fca73 | 6197 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6198 | break; |
5262f567 | 6199 | case IORING_OP_TIMEOUT: |
61e98203 | 6200 | ret = io_timeout(req, issue_flags); |
5262f567 | 6201 | break; |
11365043 | 6202 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6203 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6204 | break; |
17f2fe35 | 6205 | case IORING_OP_ACCEPT: |
889fca73 | 6206 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6207 | break; |
f8e85cf2 | 6208 | case IORING_OP_CONNECT: |
889fca73 | 6209 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6210 | break; |
62755e35 | 6211 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6212 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6213 | break; |
d63d1b5e | 6214 | case IORING_OP_FALLOCATE: |
45d189c6 | 6215 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6216 | break; |
15b71abe | 6217 | case IORING_OP_OPENAT: |
45d189c6 | 6218 | ret = io_openat(req, issue_flags); |
15b71abe | 6219 | break; |
b5dba59e | 6220 | case IORING_OP_CLOSE: |
889fca73 | 6221 | ret = io_close(req, issue_flags); |
b5dba59e | 6222 | break; |
05f3fb3c | 6223 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6224 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6225 | break; |
eddc7ef5 | 6226 | case IORING_OP_STATX: |
45d189c6 | 6227 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6228 | break; |
4840e418 | 6229 | case IORING_OP_FADVISE: |
45d189c6 | 6230 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6231 | break; |
c1ca757b | 6232 | case IORING_OP_MADVISE: |
45d189c6 | 6233 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6234 | break; |
cebdb986 | 6235 | case IORING_OP_OPENAT2: |
45d189c6 | 6236 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6237 | break; |
3e4827b0 | 6238 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6239 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6240 | break; |
7d67af2c | 6241 | case IORING_OP_SPLICE: |
45d189c6 | 6242 | ret = io_splice(req, issue_flags); |
7d67af2c | 6243 | break; |
ddf0322d | 6244 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6245 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6246 | break; |
067524e9 | 6247 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6248 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6249 | break; |
f2a8d5c7 | 6250 | case IORING_OP_TEE: |
45d189c6 | 6251 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6252 | break; |
36f4fa68 | 6253 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6254 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6255 | break; |
80a261fd | 6256 | case IORING_OP_RENAMEAT: |
45d189c6 | 6257 | ret = io_renameat(req, issue_flags); |
80a261fd | 6258 | break; |
14a1143b | 6259 | case IORING_OP_UNLINKAT: |
45d189c6 | 6260 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6261 | break; |
2b188cc1 JA |
6262 | default: |
6263 | ret = -EINVAL; | |
6264 | break; | |
6265 | } | |
6266 | ||
def596e9 JA |
6267 | if (ret) |
6268 | return ret; | |
6269 | ||
b532576e JA |
6270 | /* If the op doesn't have a file, we're not polling for it */ |
6271 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6272 | const bool in_async = io_wq_current_is_worker(); |
6273 | ||
11ba820b JA |
6274 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6275 | if (in_async) | |
6276 | mutex_lock(&ctx->uring_lock); | |
6277 | ||
2e9dbe90 | 6278 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6279 | |
6280 | if (in_async) | |
6281 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6282 | } |
6283 | ||
6284 | return 0; | |
2b188cc1 JA |
6285 | } |
6286 | ||
5280f7e5 | 6287 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6288 | { |
6289 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6290 | struct io_kiocb *timeout; |
561fb04a | 6291 | int ret = 0; |
2b188cc1 | 6292 | |
6df1db6b PB |
6293 | timeout = io_prep_linked_timeout(req); |
6294 | if (timeout) | |
6295 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6296 | |
4014d943 | 6297 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6298 | ret = -ECANCELED; |
31b51510 | 6299 | |
561fb04a | 6300 | if (!ret) { |
561fb04a | 6301 | do { |
889fca73 | 6302 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6303 | /* |
6304 | * We can get EAGAIN for polled IO even though we're | |
6305 | * forcing a sync submission from here, since we can't | |
6306 | * wait for request slots on the block side. | |
6307 | */ | |
6308 | if (ret != -EAGAIN) | |
6309 | break; | |
6310 | cond_resched(); | |
6311 | } while (1); | |
6312 | } | |
31b51510 | 6313 | |
561fb04a | 6314 | if (ret) { |
c07e6719 XW |
6315 | struct io_ring_ctx *lock_ctx = NULL; |
6316 | ||
6317 | if (req->ctx->flags & IORING_SETUP_IOPOLL) | |
6318 | lock_ctx = req->ctx; | |
6319 | ||
dad1b124 | 6320 | /* |
c07e6719 XW |
6321 | * io_iopoll_complete() does not hold completion_lock to |
6322 | * complete polled io, so here for polled io, we can not call | |
6323 | * io_req_complete() directly, otherwise there maybe concurrent | |
6324 | * access to cqring, defer_list, etc, which is not safe. Given | |
6325 | * that io_iopoll_complete() is always called under uring_lock, | |
6326 | * so here for polled io, we also get uring_lock to complete | |
6327 | * it. | |
dad1b124 | 6328 | */ |
c07e6719 XW |
6329 | if (lock_ctx) |
6330 | mutex_lock(&lock_ctx->uring_lock); | |
dad1b124 | 6331 | |
c07e6719 XW |
6332 | req_set_fail_links(req); |
6333 | io_req_complete(req, ret); | |
6334 | ||
6335 | if (lock_ctx) | |
6336 | mutex_unlock(&lock_ctx->uring_lock); | |
edafccee | 6337 | } |
2b188cc1 JA |
6338 | } |
6339 | ||
65e19f54 JA |
6340 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6341 | int index) | |
6342 | { | |
269bbe5f | 6343 | struct fixed_rsrc_table *table; |
65e19f54 | 6344 | |
05f3fb3c | 6345 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6346 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6347 | } |
6348 | ||
8371adf5 PB |
6349 | static struct file *io_file_get(struct io_submit_state *state, |
6350 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6351 | { |
a197f664 | 6352 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6353 | struct file *file; |
09bb8394 | 6354 | |
8da11c19 | 6355 | if (fixed) { |
479f517b | 6356 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6357 | return NULL; |
b7620121 | 6358 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6359 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6360 | io_set_resource_node(req); |
09bb8394 | 6361 | } else { |
c826bd7a | 6362 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6363 | file = __io_file_get(state, fd); |
09bb8394 JA |
6364 | } |
6365 | ||
ce3d5aae PB |
6366 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6367 | io_req_track_inflight(req); | |
8371adf5 | 6368 | return file; |
09bb8394 JA |
6369 | } |
6370 | ||
2665abfd | 6371 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6372 | { |
ad8a48ac JA |
6373 | struct io_timeout_data *data = container_of(timer, |
6374 | struct io_timeout_data, timer); | |
90cd7e42 | 6375 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6376 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6377 | unsigned long flags; |
2665abfd JA |
6378 | |
6379 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6380 | prev = req->timeout.head; |
6381 | req->timeout.head = NULL; | |
2665abfd JA |
6382 | |
6383 | /* | |
6384 | * We don't expect the list to be empty, that will only happen if we | |
6385 | * race with the completion of the linked work. | |
6386 | */ | |
90cd7e42 | 6387 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6388 | io_remove_next_linked(prev); |
90cd7e42 PB |
6389 | else |
6390 | prev = NULL; | |
2665abfd JA |
6391 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6392 | ||
6393 | if (prev) { | |
4e88d6e7 | 6394 | req_set_fail_links(prev); |
014db007 | 6395 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6396 | io_put_req_deferred(prev, 1); |
47f46768 | 6397 | } else { |
9ae1f8dd PB |
6398 | io_req_complete_post(req, -ETIME, 0); |
6399 | io_put_req_deferred(req, 1); | |
2665abfd | 6400 | } |
2665abfd JA |
6401 | return HRTIMER_NORESTART; |
6402 | } | |
6403 | ||
7271ef3a | 6404 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6405 | { |
76a46e06 | 6406 | /* |
f2f87370 PB |
6407 | * If the back reference is NULL, then our linked request finished |
6408 | * before we got a chance to setup the timer | |
76a46e06 | 6409 | */ |
90cd7e42 | 6410 | if (req->timeout.head) { |
e8c2bc1f | 6411 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6412 | |
ad8a48ac JA |
6413 | data->timer.function = io_link_timeout_fn; |
6414 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6415 | data->mode); | |
2665abfd | 6416 | } |
7271ef3a JA |
6417 | } |
6418 | ||
6419 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6420 | { | |
6421 | struct io_ring_ctx *ctx = req->ctx; | |
6422 | ||
6423 | spin_lock_irq(&ctx->completion_lock); | |
6424 | __io_queue_linked_timeout(req); | |
76a46e06 | 6425 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6426 | |
2665abfd | 6427 | /* drop submission reference */ |
76a46e06 JA |
6428 | io_put_req(req); |
6429 | } | |
2665abfd | 6430 | |
ad8a48ac | 6431 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6432 | { |
f2f87370 | 6433 | struct io_kiocb *nxt = req->link; |
2665abfd | 6434 | |
f2f87370 PB |
6435 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6436 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6437 | return NULL; |
2665abfd | 6438 | |
90cd7e42 | 6439 | nxt->timeout.head = req; |
900fad45 | 6440 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6441 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6442 | return nxt; |
2665abfd JA |
6443 | } |
6444 | ||
c1379e24 | 6445 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 | 6446 | { |
4a0a7a18 | 6447 | struct io_kiocb *linked_timeout; |
193155c8 | 6448 | const struct cred *old_creds = NULL; |
889fca73 | 6449 | int ret, issue_flags = IO_URING_F_NONBLOCK; |
2b188cc1 | 6450 | |
889fca73 PB |
6451 | if (cs) |
6452 | issue_flags |= IO_URING_F_COMPLETE_DEFER; | |
4a0a7a18 JA |
6453 | again: |
6454 | linked_timeout = io_prep_linked_timeout(req); | |
6455 | ||
2e5aa6cb PB |
6456 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6457 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
98447d65 | 6458 | req->work.identity->creds != current_cred()) { |
193155c8 JA |
6459 | if (old_creds) |
6460 | revert_creds(old_creds); | |
98447d65 | 6461 | if (old_creds == req->work.identity->creds) |
193155c8 JA |
6462 | old_creds = NULL; /* restored original creds */ |
6463 | else | |
98447d65 | 6464 | old_creds = override_creds(req->work.identity->creds); |
193155c8 JA |
6465 | } |
6466 | ||
889fca73 | 6467 | ret = io_issue_sqe(req, issue_flags); |
491381ce JA |
6468 | |
6469 | /* | |
6470 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6471 | * doesn't support non-blocking read/write attempts | |
6472 | */ | |
24c74678 | 6473 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6474 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6475 | /* |
6476 | * Queued up for async execution, worker will release | |
6477 | * submit reference when the iocb is actually submitted. | |
6478 | */ | |
6479 | io_queue_async_work(req); | |
2b188cc1 | 6480 | } |
bbad27b2 | 6481 | |
f063c547 PB |
6482 | if (linked_timeout) |
6483 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 PB |
6484 | } else if (likely(!ret)) { |
6485 | /* drop submission reference */ | |
e342c807 | 6486 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6dd0be1e PB |
6487 | cs->reqs[cs->nr++] = req; |
6488 | if (cs->nr == IO_COMPL_BATCH) | |
ba88ff11 | 6489 | io_submit_flush_completions(cs, req->ctx); |
9affd664 PB |
6490 | req = NULL; |
6491 | } else { | |
6492 | req = io_put_req_find_next(req); | |
6493 | } | |
6494 | ||
0d63c148 PB |
6495 | if (linked_timeout) |
6496 | io_queue_linked_timeout(linked_timeout); | |
e65ef56d | 6497 | |
0d63c148 PB |
6498 | if (req) { |
6499 | if (!(req->flags & REQ_F_FORCE_ASYNC)) | |
6500 | goto again; | |
6501 | io_queue_async_work(req); | |
6502 | } | |
6503 | } else { | |
652532ad PB |
6504 | /* un-prep timeout, so it'll be killed as any other linked */ |
6505 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6506 | req_set_fail_links(req); |
e65ef56d | 6507 | io_put_req(req); |
e1e16097 | 6508 | io_req_complete(req, ret); |
9e645e11 | 6509 | } |
652532ad | 6510 | |
193155c8 JA |
6511 | if (old_creds) |
6512 | revert_creds(old_creds); | |
2b188cc1 JA |
6513 | } |
6514 | ||
f13fad7b JA |
6515 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6516 | struct io_comp_state *cs) | |
4fe2c963 JL |
6517 | { |
6518 | int ret; | |
6519 | ||
3529d8c2 | 6520 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6521 | if (ret) { |
6522 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6523 | fail_req: |
4e88d6e7 | 6524 | req_set_fail_links(req); |
e1e16097 JA |
6525 | io_put_req(req); |
6526 | io_req_complete(req, ret); | |
4fe2c963 | 6527 | } |
2550878f | 6528 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6529 | if (!req->async_data) { |
bd2ab18a | 6530 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6531 | if (unlikely(ret)) |
bd2ab18a PB |
6532 | goto fail_req; |
6533 | } | |
ce35a47a JA |
6534 | io_queue_async_work(req); |
6535 | } else { | |
c1379e24 PB |
6536 | if (sqe) { |
6537 | ret = io_req_prep(req, sqe); | |
6538 | if (unlikely(ret)) | |
6539 | goto fail_req; | |
6540 | } | |
6541 | __io_queue_sqe(req, cs); | |
ce35a47a | 6542 | } |
4fe2c963 JL |
6543 | } |
6544 | ||
f13fad7b JA |
6545 | static inline void io_queue_link_head(struct io_kiocb *req, |
6546 | struct io_comp_state *cs) | |
4fe2c963 | 6547 | { |
94ae5e77 | 6548 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6549 | io_put_req(req); |
6550 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6551 | } else |
f13fad7b | 6552 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6553 | } |
6554 | ||
863e0560 PB |
6555 | struct io_submit_link { |
6556 | struct io_kiocb *head; | |
6557 | struct io_kiocb *last; | |
6558 | }; | |
6559 | ||
1d4240cc | 6560 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
863e0560 | 6561 | struct io_submit_link *link, struct io_comp_state *cs) |
9e645e11 | 6562 | { |
a197f664 | 6563 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6564 | int ret; |
9e645e11 | 6565 | |
9e645e11 JA |
6566 | /* |
6567 | * If we already have a head request, queue this one for async | |
6568 | * submittal once the head completes. If we don't have a head but | |
6569 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6570 | * submitted sync once the chain is complete. If none of those | |
6571 | * conditions are true (normal request), then just queue it. | |
6572 | */ | |
863e0560 PB |
6573 | if (link->head) { |
6574 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6575 | |
8cdf2193 PB |
6576 | /* |
6577 | * Taking sequential execution of a link, draining both sides | |
6578 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6579 | * requests in the link. So, it drains the head and the | |
6580 | * next after the link request. The last one is done via | |
6581 | * drain_next flag to persist the effect across calls. | |
6582 | */ | |
ef4ff581 | 6583 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6584 | head->flags |= REQ_F_IO_DRAIN; |
6585 | ctx->drain_next = 1; | |
6586 | } | |
3529d8c2 | 6587 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6588 | if (unlikely(ret)) { |
4e88d6e7 | 6589 | /* fail even hard links since we don't submit */ |
9d76377f | 6590 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6591 | return ret; |
2d28390a | 6592 | } |
9d76377f | 6593 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6594 | link->last->link = req; |
863e0560 | 6595 | link->last = req; |
32fe525b PB |
6596 | |
6597 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6598 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6599 | io_queue_link_head(head, cs); |
863e0560 | 6600 | link->head = NULL; |
32fe525b | 6601 | } |
9e645e11 | 6602 | } else { |
711be031 PB |
6603 | if (unlikely(ctx->drain_next)) { |
6604 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6605 | ctx->drain_next = 0; |
711be031 | 6606 | } |
ef4ff581 | 6607 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
711be031 | 6608 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6609 | if (unlikely(ret)) |
711be031 | 6610 | req->flags |= REQ_F_FAIL_LINK; |
863e0560 PB |
6611 | link->head = req; |
6612 | link->last = req; | |
711be031 | 6613 | } else { |
f13fad7b | 6614 | io_queue_sqe(req, sqe, cs); |
711be031 | 6615 | } |
9e645e11 | 6616 | } |
2e6e1fde | 6617 | |
1d4240cc | 6618 | return 0; |
9e645e11 JA |
6619 | } |
6620 | ||
9a56a232 JA |
6621 | /* |
6622 | * Batched submission is done, ensure local IO is flushed out. | |
6623 | */ | |
ba88ff11 PB |
6624 | static void io_submit_state_end(struct io_submit_state *state, |
6625 | struct io_ring_ctx *ctx) | |
9a56a232 | 6626 | { |
6dd0be1e | 6627 | if (state->comp.nr) |
ba88ff11 | 6628 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6629 | if (state->plug_started) |
6630 | blk_finish_plug(&state->plug); | |
9f13c35b | 6631 | io_state_file_put(state); |
5087275d | 6632 | if (state->free_reqs) { |
6c8a3134 | 6633 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
5087275d PB |
6634 | state->free_reqs = 0; |
6635 | } | |
9a56a232 JA |
6636 | } |
6637 | ||
6638 | /* | |
6639 | * Start submission side cache. | |
6640 | */ | |
6641 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6642 | unsigned int max_ios) |
9a56a232 | 6643 | { |
27926b68 | 6644 | state->plug_started = false; |
9a56a232 JA |
6645 | state->ios_left = max_ios; |
6646 | } | |
6647 | ||
2b188cc1 JA |
6648 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6649 | { | |
75b28aff | 6650 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6651 | |
caf582c6 PB |
6652 | /* |
6653 | * Ensure any loads from the SQEs are done at this point, | |
6654 | * since once we write the new head, the application could | |
6655 | * write new data to them. | |
6656 | */ | |
6657 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6658 | } |
6659 | ||
2b188cc1 | 6660 | /* |
3529d8c2 | 6661 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6662 | * that is mapped by userspace. This means that care needs to be taken to |
6663 | * ensure that reads are stable, as we cannot rely on userspace always | |
6664 | * being a good citizen. If members of the sqe are validated and then later | |
6665 | * used, it's important that those reads are done through READ_ONCE() to | |
6666 | * prevent a re-load down the line. | |
6667 | */ | |
709b302f | 6668 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6669 | { |
75b28aff | 6670 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6671 | unsigned head; |
6672 | ||
6673 | /* | |
6674 | * The cached sq head (or cq tail) serves two purposes: | |
6675 | * | |
6676 | * 1) allows us to batch the cost of updating the user visible | |
6677 | * head updates. | |
6678 | * 2) allows the kernel side to track the head on its own, even | |
6679 | * though the application is the one updating it. | |
6680 | */ | |
ee7d46d9 | 6681 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6682 | if (likely(head < ctx->sq_entries)) |
6683 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6684 | |
6685 | /* drop invalid entries */ | |
498ccd9e | 6686 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6687 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6688 | return NULL; |
6689 | } | |
6690 | ||
6691 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6692 | { | |
6693 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6694 | } |
6695 | ||
21b55dbc SG |
6696 | /* |
6697 | * Check SQE restrictions (opcode and flags). | |
6698 | * | |
6699 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6700 | */ | |
6701 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6702 | struct io_kiocb *req, | |
6703 | unsigned int sqe_flags) | |
6704 | { | |
6705 | if (!ctx->restricted) | |
6706 | return true; | |
6707 | ||
6708 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6709 | return false; | |
6710 | ||
6711 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6712 | ctx->restrictions.sqe_flags_required) | |
6713 | return false; | |
6714 | ||
6715 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6716 | ctx->restrictions.sqe_flags_required)) | |
6717 | return false; | |
6718 | ||
6719 | return true; | |
6720 | } | |
6721 | ||
ef4ff581 PB |
6722 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6723 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6724 | IOSQE_BUFFER_SELECT) | |
6725 | ||
6726 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
258b29a9 | 6727 | const struct io_uring_sqe *sqe) |
0553b8bd | 6728 | { |
258b29a9 | 6729 | struct io_submit_state *state; |
ef4ff581 | 6730 | unsigned int sqe_flags; |
71b547c0 | 6731 | int id, ret; |
ef4ff581 | 6732 | |
0553b8bd PB |
6733 | req->opcode = READ_ONCE(sqe->opcode); |
6734 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6735 | req->async_data = NULL; |
0553b8bd PB |
6736 | req->file = NULL; |
6737 | req->ctx = ctx; | |
6738 | req->flags = 0; | |
f2f87370 | 6739 | req->link = NULL; |
269bbe5f | 6740 | req->fixed_rsrc_refs = NULL; |
0553b8bd PB |
6741 | /* one is dropped after submission, the other at completion */ |
6742 | refcount_set(&req->refs, 2); | |
4dd2824d | 6743 | req->task = current; |
0553b8bd | 6744 | req->result = 0; |
ef4ff581 PB |
6745 | |
6746 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6747 | return -EINVAL; | |
6748 | ||
28cea78a | 6749 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
9d8426a0 | 6750 | return -EFAULT; |
ef4ff581 PB |
6751 | |
6752 | sqe_flags = READ_ONCE(sqe->flags); | |
6753 | /* enforce forwards compatibility on users */ | |
6754 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6755 | return -EINVAL; | |
6756 | ||
21b55dbc SG |
6757 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6758 | return -EACCES; | |
6759 | ||
ef4ff581 PB |
6760 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6761 | !io_op_defs[req->opcode].buffer_select) | |
6762 | return -EOPNOTSUPP; | |
6763 | ||
6764 | id = READ_ONCE(sqe->personality); | |
6765 | if (id) { | |
1e6fa521 JA |
6766 | struct io_identity *iod; |
6767 | ||
1e6fa521 JA |
6768 | iod = idr_find(&ctx->personality_idr, id); |
6769 | if (unlikely(!iod)) | |
ef4ff581 | 6770 | return -EINVAL; |
1e6fa521 | 6771 | refcount_inc(&iod->count); |
ec99ca6c PB |
6772 | |
6773 | __io_req_init_async(req); | |
1e6fa521 JA |
6774 | get_cred(iod->creds); |
6775 | req->work.identity = iod; | |
dfead8a8 | 6776 | req->work.flags |= IO_WQ_WORK_CREDS; |
ef4ff581 PB |
6777 | } |
6778 | ||
6779 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6780 | req->flags |= sqe_flags; |
258b29a9 | 6781 | state = &ctx->submit_state; |
ef4ff581 | 6782 | |
27926b68 JA |
6783 | /* |
6784 | * Plug now if we have more than 1 IO left after this, and the target | |
6785 | * is potentially a read/write to block based storage. | |
6786 | */ | |
6787 | if (!state->plug_started && state->ios_left > 1 && | |
6788 | io_op_defs[req->opcode].plug) { | |
6789 | blk_start_plug(&state->plug); | |
6790 | state->plug_started = true; | |
6791 | } | |
6792 | ||
bd5bbda7 PB |
6793 | ret = 0; |
6794 | if (io_op_defs[req->opcode].needs_file) { | |
6795 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6796 | ||
6797 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
ba13e23f | 6798 | if (unlikely(!req->file)) |
bd5bbda7 PB |
6799 | ret = -EBADF; |
6800 | } | |
63ff8223 | 6801 | |
71b547c0 PB |
6802 | state->ios_left--; |
6803 | return ret; | |
0553b8bd PB |
6804 | } |
6805 | ||
0f212204 | 6806 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6807 | { |
863e0560 | 6808 | struct io_submit_link link; |
9e645e11 | 6809 | int i, submitted = 0; |
6c271ce2 | 6810 | |
c4a2ed72 | 6811 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6812 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6813 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6814 | return -EBUSY; |
6815 | } | |
6c271ce2 | 6816 | |
ee7d46d9 PB |
6817 | /* make sure SQ entry isn't read before tail */ |
6818 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6819 | |
2b85edfc PB |
6820 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6821 | return -EAGAIN; | |
6c271ce2 | 6822 | |
d8a6df10 | 6823 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6824 | refcount_add(nr, ¤t->usage); |
6c271ce2 | 6825 | |
ba88ff11 | 6826 | io_submit_state_start(&ctx->submit_state, nr); |
863e0560 | 6827 | link.head = NULL; |
b14cca0c | 6828 | |
6c271ce2 | 6829 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6830 | const struct io_uring_sqe *sqe; |
196be95c | 6831 | struct io_kiocb *req; |
1cb1edb2 | 6832 | int err; |
fb5ccc98 | 6833 | |
b1e50e54 PB |
6834 | sqe = io_get_sqe(ctx); |
6835 | if (unlikely(!sqe)) { | |
6836 | io_consume_sqe(ctx); | |
6837 | break; | |
6838 | } | |
258b29a9 | 6839 | req = io_alloc_req(ctx); |
196be95c PB |
6840 | if (unlikely(!req)) { |
6841 | if (!submitted) | |
6842 | submitted = -EAGAIN; | |
fb5ccc98 | 6843 | break; |
196be95c | 6844 | } |
709b302f | 6845 | io_consume_sqe(ctx); |
d3656344 JA |
6846 | /* will complete beyond this point, count as submitted */ |
6847 | submitted++; | |
6848 | ||
258b29a9 | 6849 | err = io_init_req(ctx, req, sqe); |
ef4ff581 | 6850 | if (unlikely(err)) { |
1cb1edb2 | 6851 | fail_req: |
e1e16097 JA |
6852 | io_put_req(req); |
6853 | io_req_complete(req, err); | |
196be95c PB |
6854 | break; |
6855 | } | |
fb5ccc98 | 6856 | |
354420f7 | 6857 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
2d7e9358 | 6858 | true, ctx->flags & IORING_SETUP_SQPOLL); |
258b29a9 | 6859 | err = io_submit_sqe(req, sqe, &link, &ctx->submit_state.comp); |
1d4240cc PB |
6860 | if (err) |
6861 | goto fail_req; | |
6c271ce2 JA |
6862 | } |
6863 | ||
9466f437 PB |
6864 | if (unlikely(submitted != nr)) { |
6865 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6866 | struct io_uring_task *tctx = current->io_uring; |
6867 | int unused = nr - ref_used; | |
9466f437 | 6868 | |
d8a6df10 JA |
6869 | percpu_ref_put_many(&ctx->refs, unused); |
6870 | percpu_counter_sub(&tctx->inflight, unused); | |
6871 | put_task_struct_many(current, unused); | |
9466f437 | 6872 | } |
863e0560 | 6873 | if (link.head) |
258b29a9 | 6874 | io_queue_link_head(link.head, &ctx->submit_state.comp); |
ba88ff11 | 6875 | io_submit_state_end(&ctx->submit_state, ctx); |
6c271ce2 | 6876 | |
ae9428ca PB |
6877 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6878 | io_commit_sqring(ctx); | |
6879 | ||
6c271ce2 JA |
6880 | return submitted; |
6881 | } | |
6882 | ||
23b3628e XW |
6883 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6884 | { | |
6885 | /* Tell userspace we may need a wakeup call */ | |
6886 | spin_lock_irq(&ctx->completion_lock); | |
6887 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6888 | spin_unlock_irq(&ctx->completion_lock); | |
6889 | } | |
6890 | ||
6891 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6892 | { | |
6893 | spin_lock_irq(&ctx->completion_lock); | |
6894 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6895 | spin_unlock_irq(&ctx->completion_lock); | |
6896 | } | |
6897 | ||
08369246 | 6898 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6899 | { |
c8d1ba58 | 6900 | unsigned int to_submit; |
bdcd3eab | 6901 | int ret = 0; |
6c271ce2 | 6902 | |
c8d1ba58 | 6903 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6904 | /* if we're handling multiple rings, cap submit size for fairness */ |
6905 | if (cap_entries && to_submit > 8) | |
6906 | to_submit = 8; | |
6907 | ||
906a3c6f | 6908 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6909 | unsigned nr_events = 0; |
a4c0b3de | 6910 | |
c8d1ba58 | 6911 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6912 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6913 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6914 | |
d9d05217 PB |
6915 | if (to_submit && !ctx->sqo_dead && |
6916 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6917 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6918 | mutex_unlock(&ctx->uring_lock); |
6919 | } | |
6c271ce2 | 6920 | |
90554200 JA |
6921 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6922 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6923 | |
08369246 XW |
6924 | return ret; |
6925 | } | |
6c271ce2 | 6926 | |
08369246 XW |
6927 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6928 | { | |
6929 | struct io_ring_ctx *ctx; | |
6930 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6931 | |
08369246 XW |
6932 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6933 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6934 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6935 | } |
c1edbf5f | 6936 | |
08369246 | 6937 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6938 | } |
6c271ce2 | 6939 | |
69fb2131 JA |
6940 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6941 | { | |
6942 | struct io_ring_ctx *ctx; | |
6943 | ||
6944 | while (!list_empty(&sqd->ctx_new_list)) { | |
6945 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6946 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6947 | complete(&ctx->sq_thread_comp); | |
6948 | } | |
08369246 XW |
6949 | |
6950 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6951 | } |
6952 | ||
c8d1ba58 JA |
6953 | static int io_sq_thread(void *data) |
6954 | { | |
91d8f519 | 6955 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
6956 | struct files_struct *old_files = current->files; |
6957 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
6958 | const struct cred *old_cred = NULL; |
6959 | struct io_sq_data *sqd = data; | |
6960 | struct io_ring_ctx *ctx; | |
a0d9205f | 6961 | unsigned long timeout = 0; |
08369246 | 6962 | DEFINE_WAIT(wait); |
6c271ce2 | 6963 | |
28cea78a JA |
6964 | task_lock(current); |
6965 | current->files = NULL; | |
6966 | current->nsproxy = NULL; | |
6967 | task_unlock(current); | |
6c271ce2 | 6968 | |
69fb2131 | 6969 | while (!kthread_should_stop()) { |
08369246 XW |
6970 | int ret; |
6971 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
6972 | |
6973 | /* | |
69fb2131 JA |
6974 | * Any changes to the sqd lists are synchronized through the |
6975 | * kthread parking. This synchronizes the thread vs users, | |
6976 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 6977 | */ |
65b2b213 | 6978 | if (kthread_should_park()) { |
69fb2131 | 6979 | kthread_parkme(); |
65b2b213 XW |
6980 | /* |
6981 | * When sq thread is unparked, in case the previous park operation | |
6982 | * comes from io_put_sq_data(), which means that sq thread is going | |
6983 | * to be stopped, so here needs to have a check. | |
6984 | */ | |
6985 | if (kthread_should_stop()) | |
6986 | break; | |
6987 | } | |
7143b5ac | 6988 | |
08369246 | 6989 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 6990 | io_sqd_init_new(sqd); |
08369246 XW |
6991 | timeout = jiffies + sqd->sq_thread_idle; |
6992 | } | |
6c271ce2 | 6993 | |
08369246 | 6994 | sqt_spin = false; |
e95eee2d | 6995 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
6996 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6997 | if (current->cred != ctx->creds) { | |
6998 | if (old_cred) | |
6999 | revert_creds(old_cred); | |
7000 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7001 | } |
91d8f519 | 7002 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7003 | #ifdef CONFIG_AUDIT |
7004 | current->loginuid = ctx->loginuid; | |
7005 | current->sessionid = ctx->sessionid; | |
7006 | #endif | |
bdcd3eab | 7007 | |
08369246 XW |
7008 | ret = __io_sq_thread(ctx, cap_entries); |
7009 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7010 | sqt_spin = true; | |
6c271ce2 | 7011 | |
28cea78a | 7012 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7013 | } |
6c271ce2 | 7014 | |
08369246 | 7015 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7016 | io_run_task_work(); |
d434ab6d | 7017 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7018 | cond_resched(); |
08369246 XW |
7019 | if (sqt_spin) |
7020 | timeout = jiffies + sqd->sq_thread_idle; | |
7021 | continue; | |
7022 | } | |
7023 | ||
08369246 XW |
7024 | needs_sched = true; |
7025 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7026 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7027 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7028 | !list_empty_careful(&ctx->iopoll_list)) { | |
7029 | needs_sched = false; | |
7030 | break; | |
7031 | } | |
7032 | if (io_sqring_entries(ctx)) { | |
7033 | needs_sched = false; | |
7034 | break; | |
7035 | } | |
7036 | } | |
7037 | ||
8b28fdf2 | 7038 | if (needs_sched && !kthread_should_park()) { |
69fb2131 JA |
7039 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7040 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7041 | |
69fb2131 | 7042 | schedule(); |
69fb2131 JA |
7043 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7044 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7045 | } |
08369246 XW |
7046 | |
7047 | finish_wait(&sqd->wait, &wait); | |
7048 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7049 | } |
7050 | ||
4c6e277c | 7051 | io_run_task_work(); |
d434ab6d | 7052 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7053 | |
91d8f519 DZ |
7054 | if (cur_css) |
7055 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7056 | if (old_cred) |
7057 | revert_creds(old_cred); | |
06058632 | 7058 | |
28cea78a JA |
7059 | task_lock(current); |
7060 | current->files = old_files; | |
7061 | current->nsproxy = old_nsproxy; | |
7062 | task_unlock(current); | |
7063 | ||
2bbcd6d3 | 7064 | kthread_parkme(); |
06058632 | 7065 | |
6c271ce2 JA |
7066 | return 0; |
7067 | } | |
7068 | ||
bda52162 JA |
7069 | struct io_wait_queue { |
7070 | struct wait_queue_entry wq; | |
7071 | struct io_ring_ctx *ctx; | |
7072 | unsigned to_wait; | |
7073 | unsigned nr_timeouts; | |
7074 | }; | |
7075 | ||
6c503150 | 7076 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7077 | { |
7078 | struct io_ring_ctx *ctx = iowq->ctx; | |
7079 | ||
7080 | /* | |
d195a66e | 7081 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7082 | * started waiting. For timeouts, we always want to return to userspace, |
7083 | * regardless of event count. | |
7084 | */ | |
6c503150 | 7085 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7086 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7087 | } | |
7088 | ||
7089 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7090 | int wake_flags, void *key) | |
7091 | { | |
7092 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7093 | wq); | |
7094 | ||
6c503150 PB |
7095 | /* |
7096 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7097 | * the task, and the next invocation will do it. | |
7098 | */ | |
7099 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7100 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7101 | return -1; | |
bda52162 JA |
7102 | } |
7103 | ||
af9c1a44 JA |
7104 | static int io_run_task_work_sig(void) |
7105 | { | |
7106 | if (io_run_task_work()) | |
7107 | return 1; | |
7108 | if (!signal_pending(current)) | |
7109 | return 0; | |
792ee0f6 JA |
7110 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7111 | return -ERESTARTSYS; | |
af9c1a44 JA |
7112 | return -EINTR; |
7113 | } | |
7114 | ||
eeb60b9a PB |
7115 | /* when returns >0, the caller should retry */ |
7116 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7117 | struct io_wait_queue *iowq, | |
7118 | signed long *timeout) | |
7119 | { | |
7120 | int ret; | |
7121 | ||
7122 | /* make sure we run task_work before checking for signals */ | |
7123 | ret = io_run_task_work_sig(); | |
7124 | if (ret || io_should_wake(iowq)) | |
7125 | return ret; | |
7126 | /* let the caller flush overflows, retry */ | |
7127 | if (test_bit(0, &ctx->cq_check_overflow)) | |
7128 | return 1; | |
7129 | ||
7130 | *timeout = schedule_timeout(*timeout); | |
7131 | return !*timeout ? -ETIME : 1; | |
7132 | } | |
7133 | ||
2b188cc1 JA |
7134 | /* |
7135 | * Wait until events become available, if we don't already have some. The | |
7136 | * application must reap them itself, as they reside on the shared cq ring. | |
7137 | */ | |
7138 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7139 | const sigset_t __user *sig, size_t sigsz, |
7140 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7141 | { |
bda52162 JA |
7142 | struct io_wait_queue iowq = { |
7143 | .wq = { | |
7144 | .private = current, | |
7145 | .func = io_wake_function, | |
7146 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7147 | }, | |
7148 | .ctx = ctx, | |
7149 | .to_wait = min_events, | |
7150 | }; | |
75b28aff | 7151 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7152 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7153 | int ret; | |
2b188cc1 | 7154 | |
b41e9852 | 7155 | do { |
6c503150 PB |
7156 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7157 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7158 | return 0; |
4c6e277c | 7159 | if (!io_run_task_work()) |
b41e9852 | 7160 | break; |
b41e9852 | 7161 | } while (1); |
2b188cc1 JA |
7162 | |
7163 | if (sig) { | |
9e75ad5d AB |
7164 | #ifdef CONFIG_COMPAT |
7165 | if (in_compat_syscall()) | |
7166 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7167 | sigsz); |
9e75ad5d AB |
7168 | else |
7169 | #endif | |
b772434b | 7170 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7171 | |
2b188cc1 JA |
7172 | if (ret) |
7173 | return ret; | |
7174 | } | |
7175 | ||
c73ebb68 | 7176 | if (uts) { |
c1d5a224 PB |
7177 | struct timespec64 ts; |
7178 | ||
c73ebb68 HX |
7179 | if (get_timespec64(&ts, uts)) |
7180 | return -EFAULT; | |
7181 | timeout = timespec64_to_jiffies(&ts); | |
7182 | } | |
7183 | ||
bda52162 | 7184 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7185 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7186 | do { |
6c503150 | 7187 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7188 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7189 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
7190 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
7191 | finish_wait(&ctx->wait, &iowq.wq); | |
7192 | } while (ret > 0); | |
bda52162 | 7193 | |
b7db41c9 | 7194 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7195 | |
75b28aff | 7196 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7197 | } |
7198 | ||
6b06314c JA |
7199 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7200 | { | |
7201 | #if defined(CONFIG_UNIX) | |
7202 | if (ctx->ring_sock) { | |
7203 | struct sock *sock = ctx->ring_sock->sk; | |
7204 | struct sk_buff *skb; | |
7205 | ||
7206 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7207 | kfree_skb(skb); | |
7208 | } | |
7209 | #else | |
7210 | int i; | |
7211 | ||
65e19f54 JA |
7212 | for (i = 0; i < ctx->nr_user_files; i++) { |
7213 | struct file *file; | |
7214 | ||
7215 | file = io_file_from_index(ctx, i); | |
7216 | if (file) | |
7217 | fput(file); | |
7218 | } | |
6b06314c JA |
7219 | #endif |
7220 | } | |
7221 | ||
00835dce | 7222 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7223 | { |
269bbe5f | 7224 | struct fixed_rsrc_data *data; |
05f3fb3c | 7225 | |
269bbe5f | 7226 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7227 | complete(&data->done); |
7228 | } | |
7229 | ||
2a63b2d9 BM |
7230 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
7231 | { | |
7232 | spin_lock_bh(&ctx->rsrc_ref_lock); | |
7233 | } | |
7234 | ||
7235 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) | |
7236 | { | |
7237 | spin_unlock_bh(&ctx->rsrc_ref_lock); | |
7238 | } | |
7239 | ||
d67d2263 BM |
7240 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7241 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7242 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7243 | { |
2a63b2d9 | 7244 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7245 | rsrc_data->node = ref_node; |
d67d2263 | 7246 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7247 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7248 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7249 | } |
7250 | ||
d7954b2b BM |
7251 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7252 | struct io_ring_ctx *ctx, | |
7253 | struct fixed_rsrc_ref_node *backup_node) | |
6b06314c | 7254 | { |
d7954b2b | 7255 | struct fixed_rsrc_ref_node *ref_node; |
1ffc5422 | 7256 | int ret; |
65e19f54 | 7257 | |
2a63b2d9 | 7258 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7259 | ref_node = data->node; |
2a63b2d9 | 7260 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7261 | if (ref_node) |
7262 | percpu_ref_kill(&ref_node->refs); | |
7263 | ||
7264 | percpu_ref_kill(&data->refs); | |
7265 | ||
7266 | /* wait for all refs nodes to complete */ | |
269bbe5f | 7267 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 PB |
7268 | do { |
7269 | ret = wait_for_completion_interruptible(&data->done); | |
7270 | if (!ret) | |
7271 | break; | |
7272 | ret = io_run_task_work_sig(); | |
7273 | if (ret < 0) { | |
7274 | percpu_ref_resurrect(&data->refs); | |
7275 | reinit_completion(&data->done); | |
d67d2263 | 7276 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
1ffc5422 PB |
7277 | return ret; |
7278 | } | |
7279 | } while (1); | |
05f3fb3c | 7280 | |
d7954b2b BM |
7281 | destroy_fixed_rsrc_ref_node(backup_node); |
7282 | return 0; | |
7283 | } | |
7284 | ||
1ad555c6 BM |
7285 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7286 | { | |
7287 | struct fixed_rsrc_data *data; | |
7288 | ||
7289 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7290 | if (!data) | |
7291 | return NULL; | |
7292 | ||
00835dce | 7293 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7294 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7295 | kfree(data); | |
7296 | return NULL; | |
7297 | } | |
7298 | data->ctx = ctx; | |
7299 | init_completion(&data->done); | |
7300 | return data; | |
7301 | } | |
7302 | ||
7303 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7304 | { | |
7305 | percpu_ref_exit(&data->refs); | |
7306 | kfree(data->table); | |
7307 | kfree(data); | |
7308 | } | |
7309 | ||
d7954b2b BM |
7310 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7311 | { | |
7312 | struct fixed_rsrc_data *data = ctx->file_data; | |
7313 | struct fixed_rsrc_ref_node *backup_node; | |
7314 | unsigned nr_tables, i; | |
7315 | int ret; | |
7316 | ||
7317 | if (!data) | |
7318 | return -ENXIO; | |
7319 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7320 | if (!backup_node) | |
7321 | return -ENOMEM; | |
7322 | init_fixed_file_ref_node(ctx, backup_node); | |
7323 | ||
7324 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); | |
7325 | if (ret) | |
7326 | return ret; | |
7327 | ||
6b06314c | 7328 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7329 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7330 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7331 | kfree(data->table[i].files); |
1ad555c6 | 7332 | free_fixed_rsrc_data(data); |
05f3fb3c | 7333 | ctx->file_data = NULL; |
6b06314c JA |
7334 | ctx->nr_user_files = 0; |
7335 | return 0; | |
7336 | } | |
7337 | ||
534ca6d6 | 7338 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7339 | { |
534ca6d6 | 7340 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7341 | /* |
7342 | * The park is a bit of a work-around, without it we get | |
7343 | * warning spews on shutdown with SQPOLL set and affinity | |
7344 | * set to a single CPU. | |
7345 | */ | |
534ca6d6 JA |
7346 | if (sqd->thread) { |
7347 | kthread_park(sqd->thread); | |
7348 | kthread_stop(sqd->thread); | |
7349 | } | |
7350 | ||
7351 | kfree(sqd); | |
7352 | } | |
7353 | } | |
7354 | ||
aa06165d JA |
7355 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7356 | { | |
7357 | struct io_ring_ctx *ctx_attach; | |
7358 | struct io_sq_data *sqd; | |
7359 | struct fd f; | |
7360 | ||
7361 | f = fdget(p->wq_fd); | |
7362 | if (!f.file) | |
7363 | return ERR_PTR(-ENXIO); | |
7364 | if (f.file->f_op != &io_uring_fops) { | |
7365 | fdput(f); | |
7366 | return ERR_PTR(-EINVAL); | |
7367 | } | |
7368 | ||
7369 | ctx_attach = f.file->private_data; | |
7370 | sqd = ctx_attach->sq_data; | |
7371 | if (!sqd) { | |
7372 | fdput(f); | |
7373 | return ERR_PTR(-EINVAL); | |
7374 | } | |
7375 | ||
7376 | refcount_inc(&sqd->refs); | |
7377 | fdput(f); | |
7378 | return sqd; | |
7379 | } | |
7380 | ||
534ca6d6 JA |
7381 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7382 | { | |
7383 | struct io_sq_data *sqd; | |
7384 | ||
aa06165d JA |
7385 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7386 | return io_attach_sq_data(p); | |
7387 | ||
534ca6d6 JA |
7388 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7389 | if (!sqd) | |
7390 | return ERR_PTR(-ENOMEM); | |
7391 | ||
7392 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7393 | INIT_LIST_HEAD(&sqd->ctx_list); |
7394 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7395 | mutex_init(&sqd->ctx_lock); | |
7396 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7397 | init_waitqueue_head(&sqd->wait); |
7398 | return sqd; | |
7399 | } | |
7400 | ||
69fb2131 JA |
7401 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7402 | __releases(&sqd->lock) | |
7403 | { | |
7404 | if (!sqd->thread) | |
7405 | return; | |
7406 | kthread_unpark(sqd->thread); | |
7407 | mutex_unlock(&sqd->lock); | |
7408 | } | |
7409 | ||
7410 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7411 | __acquires(&sqd->lock) | |
7412 | { | |
7413 | if (!sqd->thread) | |
7414 | return; | |
7415 | mutex_lock(&sqd->lock); | |
7416 | kthread_park(sqd->thread); | |
7417 | } | |
7418 | ||
534ca6d6 JA |
7419 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7420 | { | |
7421 | struct io_sq_data *sqd = ctx->sq_data; | |
7422 | ||
7423 | if (sqd) { | |
7424 | if (sqd->thread) { | |
7425 | /* | |
7426 | * We may arrive here from the error branch in | |
7427 | * io_sq_offload_create() where the kthread is created | |
7428 | * without being waked up, thus wake it up now to make | |
7429 | * sure the wait will complete. | |
7430 | */ | |
7431 | wake_up_process(sqd->thread); | |
7432 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7433 | |
7434 | io_sq_thread_park(sqd); | |
7435 | } | |
7436 | ||
7437 | mutex_lock(&sqd->ctx_lock); | |
7438 | list_del(&ctx->sqd_list); | |
08369246 | 7439 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7440 | mutex_unlock(&sqd->ctx_lock); |
7441 | ||
08369246 | 7442 | if (sqd->thread) |
69fb2131 | 7443 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7444 | |
7445 | io_put_sq_data(sqd); | |
7446 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7447 | } |
7448 | } | |
7449 | ||
6b06314c JA |
7450 | static void io_finish_async(struct io_ring_ctx *ctx) |
7451 | { | |
6c271ce2 JA |
7452 | io_sq_thread_stop(ctx); |
7453 | ||
561fb04a JA |
7454 | if (ctx->io_wq) { |
7455 | io_wq_destroy(ctx->io_wq); | |
7456 | ctx->io_wq = NULL; | |
6b06314c JA |
7457 | } |
7458 | } | |
7459 | ||
7460 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7461 | /* |
7462 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7463 | * the io_uring can be safely unregistered on process exit, even if we have | |
7464 | * loops in the file referencing. | |
7465 | */ | |
7466 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7467 | { | |
7468 | struct sock *sk = ctx->ring_sock->sk; | |
7469 | struct scm_fp_list *fpl; | |
7470 | struct sk_buff *skb; | |
08a45173 | 7471 | int i, nr_files; |
6b06314c | 7472 | |
6b06314c JA |
7473 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7474 | if (!fpl) | |
7475 | return -ENOMEM; | |
7476 | ||
7477 | skb = alloc_skb(0, GFP_KERNEL); | |
7478 | if (!skb) { | |
7479 | kfree(fpl); | |
7480 | return -ENOMEM; | |
7481 | } | |
7482 | ||
7483 | skb->sk = sk; | |
6b06314c | 7484 | |
08a45173 | 7485 | nr_files = 0; |
6b06314c JA |
7486 | fpl->user = get_uid(ctx->user); |
7487 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7488 | struct file *file = io_file_from_index(ctx, i + offset); |
7489 | ||
7490 | if (!file) | |
08a45173 | 7491 | continue; |
65e19f54 | 7492 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7493 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7494 | nr_files++; | |
6b06314c JA |
7495 | } |
7496 | ||
08a45173 JA |
7497 | if (nr_files) { |
7498 | fpl->max = SCM_MAX_FD; | |
7499 | fpl->count = nr_files; | |
7500 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7501 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7502 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7503 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7504 | |
08a45173 JA |
7505 | for (i = 0; i < nr_files; i++) |
7506 | fput(fpl->fp[i]); | |
7507 | } else { | |
7508 | kfree_skb(skb); | |
7509 | kfree(fpl); | |
7510 | } | |
6b06314c JA |
7511 | |
7512 | return 0; | |
7513 | } | |
7514 | ||
7515 | /* | |
7516 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7517 | * causes regular reference counting to break down. We rely on the UNIX | |
7518 | * garbage collection to take care of this problem for us. | |
7519 | */ | |
7520 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7521 | { | |
7522 | unsigned left, total; | |
7523 | int ret = 0; | |
7524 | ||
7525 | total = 0; | |
7526 | left = ctx->nr_user_files; | |
7527 | while (left) { | |
7528 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7529 | |
7530 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7531 | if (ret) | |
7532 | break; | |
7533 | left -= this_files; | |
7534 | total += this_files; | |
7535 | } | |
7536 | ||
7537 | if (!ret) | |
7538 | return 0; | |
7539 | ||
7540 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7541 | struct file *file = io_file_from_index(ctx, total); |
7542 | ||
7543 | if (file) | |
7544 | fput(file); | |
6b06314c JA |
7545 | total++; |
7546 | } | |
7547 | ||
7548 | return ret; | |
7549 | } | |
7550 | #else | |
7551 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7552 | { | |
7553 | return 0; | |
7554 | } | |
7555 | #endif | |
7556 | ||
269bbe5f | 7557 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7558 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7559 | { |
7560 | int i; | |
7561 | ||
7562 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7563 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7564 | unsigned this_files; |
7565 | ||
7566 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7567 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7568 | GFP_KERNEL); | |
7569 | if (!table->files) | |
7570 | break; | |
7571 | nr_files -= this_files; | |
7572 | } | |
7573 | ||
7574 | if (i == nr_tables) | |
7575 | return 0; | |
7576 | ||
7577 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7578 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7579 | kfree(table->files); |
7580 | } | |
7581 | return 1; | |
7582 | } | |
7583 | ||
50238531 | 7584 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7585 | { |
50238531 | 7586 | struct file *file = prsrc->file; |
05f3fb3c JA |
7587 | #if defined(CONFIG_UNIX) |
7588 | struct sock *sock = ctx->ring_sock->sk; | |
7589 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7590 | struct sk_buff *skb; | |
7591 | int i; | |
7592 | ||
7593 | __skb_queue_head_init(&list); | |
7594 | ||
7595 | /* | |
7596 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7597 | * remove this entry and rearrange the file array. | |
7598 | */ | |
7599 | skb = skb_dequeue(head); | |
7600 | while (skb) { | |
7601 | struct scm_fp_list *fp; | |
7602 | ||
7603 | fp = UNIXCB(skb).fp; | |
7604 | for (i = 0; i < fp->count; i++) { | |
7605 | int left; | |
7606 | ||
7607 | if (fp->fp[i] != file) | |
7608 | continue; | |
7609 | ||
7610 | unix_notinflight(fp->user, fp->fp[i]); | |
7611 | left = fp->count - 1 - i; | |
7612 | if (left) { | |
7613 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7614 | left * sizeof(struct file *)); | |
7615 | } | |
7616 | fp->count--; | |
7617 | if (!fp->count) { | |
7618 | kfree_skb(skb); | |
7619 | skb = NULL; | |
7620 | } else { | |
7621 | __skb_queue_tail(&list, skb); | |
7622 | } | |
7623 | fput(file); | |
7624 | file = NULL; | |
7625 | break; | |
7626 | } | |
7627 | ||
7628 | if (!file) | |
7629 | break; | |
7630 | ||
7631 | __skb_queue_tail(&list, skb); | |
7632 | ||
7633 | skb = skb_dequeue(head); | |
7634 | } | |
7635 | ||
7636 | if (skb_peek(&list)) { | |
7637 | spin_lock_irq(&head->lock); | |
7638 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7639 | __skb_queue_tail(head, skb); | |
7640 | spin_unlock_irq(&head->lock); | |
7641 | } | |
7642 | #else | |
7643 | fput(file); | |
7644 | #endif | |
7645 | } | |
7646 | ||
269bbe5f | 7647 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7648 | { |
269bbe5f BM |
7649 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7650 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7651 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7652 | |
269bbe5f BM |
7653 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7654 | list_del(&prsrc->list); | |
50238531 | 7655 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7656 | kfree(prsrc); |
65e19f54 | 7657 | } |
05589553 | 7658 | |
05589553 XW |
7659 | percpu_ref_exit(&ref_node->refs); |
7660 | kfree(ref_node); | |
269bbe5f | 7661 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7662 | } |
65e19f54 | 7663 | |
269bbe5f | 7664 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7665 | { |
7666 | struct io_ring_ctx *ctx; | |
7667 | struct llist_node *node; | |
7668 | ||
269bbe5f BM |
7669 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7670 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7671 | |
7672 | while (node) { | |
269bbe5f | 7673 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7674 | struct llist_node *next = node->next; |
7675 | ||
269bbe5f BM |
7676 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7677 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7678 | node = next; |
7679 | } | |
7680 | } | |
7681 | ||
ea64ec02 PB |
7682 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7683 | unsigned i) | |
7684 | { | |
7685 | struct fixed_rsrc_table *table; | |
7686 | ||
7687 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7688 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7689 | } | |
7690 | ||
00835dce | 7691 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7692 | { |
269bbe5f BM |
7693 | struct fixed_rsrc_ref_node *ref_node; |
7694 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7695 | struct io_ring_ctx *ctx; |
e297822b | 7696 | bool first_add = false; |
4a38aed2 | 7697 | int delay = HZ; |
65e19f54 | 7698 | |
269bbe5f BM |
7699 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7700 | data = ref_node->rsrc_data; | |
e297822b PB |
7701 | ctx = data->ctx; |
7702 | ||
2a63b2d9 | 7703 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7704 | ref_node->done = true; |
7705 | ||
d67d2263 BM |
7706 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7707 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7708 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7709 | /* recycle ref nodes in order */ |
7710 | if (!ref_node->done) | |
7711 | break; | |
7712 | list_del(&ref_node->node); | |
269bbe5f | 7713 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7714 | } |
2a63b2d9 | 7715 | io_rsrc_ref_unlock(ctx); |
05589553 | 7716 | |
e297822b | 7717 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7718 | delay = 0; |
05589553 | 7719 | |
4a38aed2 | 7720 | if (!delay) |
269bbe5f | 7721 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7722 | else if (first_add) |
269bbe5f | 7723 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7724 | } |
65e19f54 | 7725 | |
6802535d | 7726 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7727 | struct io_ring_ctx *ctx) |
05f3fb3c | 7728 | { |
269bbe5f | 7729 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7730 | |
05589553 XW |
7731 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7732 | if (!ref_node) | |
3e2224c5 | 7733 | return NULL; |
05f3fb3c | 7734 | |
00835dce | 7735 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7736 | 0, GFP_KERNEL)) { |
7737 | kfree(ref_node); | |
3e2224c5 | 7738 | return NULL; |
05589553 XW |
7739 | } |
7740 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7741 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
6802535d BM |
7742 | ref_node->done = false; |
7743 | return ref_node; | |
7744 | } | |
7745 | ||
bc9744cd PB |
7746 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7747 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7748 | { |
269bbe5f | 7749 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7750 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7751 | } |
7752 | ||
269bbe5f | 7753 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7754 | { |
7755 | percpu_ref_exit(&ref_node->refs); | |
7756 | kfree(ref_node); | |
65e19f54 JA |
7757 | } |
7758 | ||
ea64ec02 | 7759 | |
6b06314c JA |
7760 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7761 | unsigned nr_args) | |
7762 | { | |
7763 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7764 | unsigned nr_tables, i; |
05f3fb3c | 7765 | struct file *file; |
600cf3f8 | 7766 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7767 | struct fixed_rsrc_ref_node *ref_node; |
7768 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7769 | |
05f3fb3c | 7770 | if (ctx->file_data) |
6b06314c JA |
7771 | return -EBUSY; |
7772 | if (!nr_args) | |
7773 | return -EINVAL; | |
7774 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7775 | return -EMFILE; | |
7776 | ||
1ad555c6 | 7777 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7778 | if (!file_data) |
05f3fb3c | 7779 | return -ENOMEM; |
13770a71 | 7780 | ctx->file_data = file_data; |
05f3fb3c | 7781 | |
65e19f54 | 7782 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7783 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7784 | GFP_KERNEL); |
600cf3f8 PB |
7785 | if (!file_data->table) |
7786 | goto out_free; | |
05f3fb3c | 7787 | |
600cf3f8 | 7788 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7789 | goto out_free; |
65e19f54 | 7790 | |
08a45173 | 7791 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7792 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7793 | ret = -EFAULT; | |
7794 | goto out_fput; | |
7795 | } | |
08a45173 | 7796 | /* allow sparse sets */ |
600cf3f8 | 7797 | if (fd == -1) |
08a45173 | 7798 | continue; |
6b06314c | 7799 | |
05f3fb3c | 7800 | file = fget(fd); |
6b06314c | 7801 | ret = -EBADF; |
05f3fb3c | 7802 | if (!file) |
600cf3f8 | 7803 | goto out_fput; |
05f3fb3c | 7804 | |
6b06314c JA |
7805 | /* |
7806 | * Don't allow io_uring instances to be registered. If UNIX | |
7807 | * isn't enabled, then this causes a reference cycle and this | |
7808 | * instance can never get freed. If UNIX is enabled we'll | |
7809 | * handle it just fine, but there's still no point in allowing | |
7810 | * a ring fd as it doesn't support regular read/write anyway. | |
7811 | */ | |
05f3fb3c JA |
7812 | if (file->f_op == &io_uring_fops) { |
7813 | fput(file); | |
600cf3f8 | 7814 | goto out_fput; |
6b06314c | 7815 | } |
ea64ec02 | 7816 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7817 | } |
7818 | ||
6b06314c | 7819 | ret = io_sqe_files_scm(ctx); |
05589553 | 7820 | if (ret) { |
6b06314c | 7821 | io_sqe_files_unregister(ctx); |
05589553 XW |
7822 | return ret; |
7823 | } | |
6b06314c | 7824 | |
bc9744cd | 7825 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7826 | if (!ref_node) { |
05589553 | 7827 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7828 | return -ENOMEM; |
05589553 | 7829 | } |
bc9744cd | 7830 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7831 | |
d67d2263 | 7832 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7833 | return ret; |
600cf3f8 PB |
7834 | out_fput: |
7835 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7836 | file = io_file_from_index(ctx, i); | |
7837 | if (file) | |
7838 | fput(file); | |
7839 | } | |
7840 | for (i = 0; i < nr_tables; i++) | |
7841 | kfree(file_data->table[i].files); | |
7842 | ctx->nr_user_files = 0; | |
600cf3f8 | 7843 | out_free: |
1ad555c6 | 7844 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7845 | ctx->file_data = NULL; |
6b06314c JA |
7846 | return ret; |
7847 | } | |
7848 | ||
c3a31e60 JA |
7849 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7850 | int index) | |
7851 | { | |
7852 | #if defined(CONFIG_UNIX) | |
7853 | struct sock *sock = ctx->ring_sock->sk; | |
7854 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7855 | struct sk_buff *skb; | |
7856 | ||
7857 | /* | |
7858 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7859 | * file set. If there's no room, fall back to allocating a new skb | |
7860 | * and filling it in. | |
7861 | */ | |
7862 | spin_lock_irq(&head->lock); | |
7863 | skb = skb_peek(head); | |
7864 | if (skb) { | |
7865 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7866 | ||
7867 | if (fpl->count < SCM_MAX_FD) { | |
7868 | __skb_unlink(skb, head); | |
7869 | spin_unlock_irq(&head->lock); | |
7870 | fpl->fp[fpl->count] = get_file(file); | |
7871 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7872 | fpl->count++; | |
7873 | spin_lock_irq(&head->lock); | |
7874 | __skb_queue_head(head, skb); | |
7875 | } else { | |
7876 | skb = NULL; | |
7877 | } | |
7878 | } | |
7879 | spin_unlock_irq(&head->lock); | |
7880 | ||
7881 | if (skb) { | |
7882 | fput(file); | |
7883 | return 0; | |
7884 | } | |
7885 | ||
7886 | return __io_sqe_files_scm(ctx, 1, index); | |
7887 | #else | |
7888 | return 0; | |
7889 | #endif | |
7890 | } | |
7891 | ||
50238531 | 7892 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7893 | { |
269bbe5f BM |
7894 | struct io_rsrc_put *prsrc; |
7895 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7896 | |
269bbe5f BM |
7897 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7898 | if (!prsrc) | |
a5318d3c | 7899 | return -ENOMEM; |
05f3fb3c | 7900 | |
50238531 | 7901 | prsrc->rsrc = rsrc; |
269bbe5f | 7902 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7903 | |
a5318d3c | 7904 | return 0; |
05f3fb3c JA |
7905 | } |
7906 | ||
269bbe5f BM |
7907 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7908 | struct file *file) | |
7909 | { | |
50238531 | 7910 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7911 | } |
7912 | ||
05f3fb3c | 7913 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7914 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7915 | unsigned nr_args) |
7916 | { | |
269bbe5f BM |
7917 | struct fixed_rsrc_data *data = ctx->file_data; |
7918 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7919 | struct file *file, **file_slot; |
c3a31e60 JA |
7920 | __s32 __user *fds; |
7921 | int fd, i, err; | |
7922 | __u32 done; | |
05589553 | 7923 | bool needs_switch = false; |
c3a31e60 | 7924 | |
05f3fb3c | 7925 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7926 | return -EOVERFLOW; |
7927 | if (done > ctx->nr_user_files) | |
7928 | return -EINVAL; | |
7929 | ||
bc9744cd | 7930 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
7931 | if (!ref_node) |
7932 | return -ENOMEM; | |
bc9744cd | 7933 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7934 | |
269bbe5f | 7935 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7936 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7937 | err = 0; |
7938 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7939 | err = -EFAULT; | |
7940 | break; | |
7941 | } | |
4e0377a1 | 7942 | if (fd == IORING_REGISTER_FILES_SKIP) |
7943 | continue; | |
7944 | ||
67973b93 | 7945 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7946 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7947 | ||
7948 | if (*file_slot) { | |
7949 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
7950 | if (err) |
7951 | break; | |
ea64ec02 | 7952 | *file_slot = NULL; |
05589553 | 7953 | needs_switch = true; |
c3a31e60 JA |
7954 | } |
7955 | if (fd != -1) { | |
c3a31e60 JA |
7956 | file = fget(fd); |
7957 | if (!file) { | |
7958 | err = -EBADF; | |
7959 | break; | |
7960 | } | |
7961 | /* | |
7962 | * Don't allow io_uring instances to be registered. If | |
7963 | * UNIX isn't enabled, then this causes a reference | |
7964 | * cycle and this instance can never get freed. If UNIX | |
7965 | * is enabled we'll handle it just fine, but there's | |
7966 | * still no point in allowing a ring fd as it doesn't | |
7967 | * support regular read/write anyway. | |
7968 | */ | |
7969 | if (file->f_op == &io_uring_fops) { | |
7970 | fput(file); | |
7971 | err = -EBADF; | |
7972 | break; | |
7973 | } | |
c3a31e60 | 7974 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae YY |
7975 | if (err) { |
7976 | fput(file); | |
c3a31e60 | 7977 | break; |
f3bd9dae | 7978 | } |
ea64ec02 | 7979 | *file_slot = file; |
c3a31e60 | 7980 | } |
05f3fb3c JA |
7981 | } |
7982 | ||
05589553 | 7983 | if (needs_switch) { |
b2e96852 | 7984 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 7985 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 7986 | } else |
269bbe5f | 7987 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
7988 | |
7989 | return done ? done : err; | |
7990 | } | |
05589553 | 7991 | |
05f3fb3c JA |
7992 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7993 | unsigned nr_args) | |
7994 | { | |
269bbe5f | 7995 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7996 | |
7997 | if (!ctx->file_data) | |
7998 | return -ENXIO; | |
7999 | if (!nr_args) | |
8000 | return -EINVAL; | |
8001 | if (copy_from_user(&up, arg, sizeof(up))) | |
8002 | return -EFAULT; | |
8003 | if (up.resv) | |
8004 | return -EINVAL; | |
8005 | ||
8006 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8007 | } | |
c3a31e60 | 8008 | |
5280f7e5 | 8009 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
8010 | { |
8011 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8012 | ||
5280f7e5 PB |
8013 | req = io_put_req_find_next(req); |
8014 | return req ? &req->work : NULL; | |
7d723065 JA |
8015 | } |
8016 | ||
24369c2e PB |
8017 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
8018 | struct io_uring_params *p) | |
8019 | { | |
8020 | struct io_wq_data data; | |
8021 | struct fd f; | |
8022 | struct io_ring_ctx *ctx_attach; | |
8023 | unsigned int concurrency; | |
8024 | int ret = 0; | |
8025 | ||
8026 | data.user = ctx->user; | |
e9fd9396 | 8027 | data.free_work = io_free_work; |
f5fa38c5 | 8028 | data.do_work = io_wq_submit_work; |
24369c2e PB |
8029 | |
8030 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
8031 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
8032 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
8033 | ||
8034 | ctx->io_wq = io_wq_create(concurrency, &data); | |
8035 | if (IS_ERR(ctx->io_wq)) { | |
8036 | ret = PTR_ERR(ctx->io_wq); | |
8037 | ctx->io_wq = NULL; | |
8038 | } | |
8039 | return ret; | |
8040 | } | |
8041 | ||
8042 | f = fdget(p->wq_fd); | |
8043 | if (!f.file) | |
8044 | return -EBADF; | |
8045 | ||
8046 | if (f.file->f_op != &io_uring_fops) { | |
8047 | ret = -EINVAL; | |
8048 | goto out_fput; | |
8049 | } | |
8050 | ||
8051 | ctx_attach = f.file->private_data; | |
8052 | /* @io_wq is protected by holding the fd */ | |
8053 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8054 | ret = -EINVAL; | |
8055 | goto out_fput; | |
8056 | } | |
8057 | ||
8058 | ctx->io_wq = ctx_attach->io_wq; | |
8059 | out_fput: | |
8060 | fdput(f); | |
8061 | return ret; | |
8062 | } | |
8063 | ||
0f212204 JA |
8064 | static int io_uring_alloc_task_context(struct task_struct *task) |
8065 | { | |
8066 | struct io_uring_task *tctx; | |
d8a6df10 | 8067 | int ret; |
0f212204 JA |
8068 | |
8069 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8070 | if (unlikely(!tctx)) | |
8071 | return -ENOMEM; | |
8072 | ||
d8a6df10 JA |
8073 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8074 | if (unlikely(ret)) { | |
8075 | kfree(tctx); | |
8076 | return ret; | |
8077 | } | |
8078 | ||
0f212204 JA |
8079 | xa_init(&tctx->xa); |
8080 | init_waitqueue_head(&tctx->wait); | |
8081 | tctx->last = NULL; | |
fdaf083c JA |
8082 | atomic_set(&tctx->in_idle, 0); |
8083 | tctx->sqpoll = false; | |
500a373d JA |
8084 | io_init_identity(&tctx->__identity); |
8085 | tctx->identity = &tctx->__identity; | |
0f212204 JA |
8086 | task->io_uring = tctx; |
8087 | return 0; | |
8088 | } | |
8089 | ||
8090 | void __io_uring_free(struct task_struct *tsk) | |
8091 | { | |
8092 | struct io_uring_task *tctx = tsk->io_uring; | |
8093 | ||
8094 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8095 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8096 | if (tctx->identity != &tctx->__identity) | |
8097 | kfree(tctx->identity); | |
d8a6df10 | 8098 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8099 | kfree(tctx); |
8100 | tsk->io_uring = NULL; | |
8101 | } | |
8102 | ||
7e84e1c7 SG |
8103 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8104 | struct io_uring_params *p) | |
2b188cc1 JA |
8105 | { |
8106 | int ret; | |
8107 | ||
6c271ce2 | 8108 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8109 | struct io_sq_data *sqd; |
8110 | ||
3ec482d1 | 8111 | ret = -EPERM; |
ce59fc69 | 8112 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8113 | goto err; |
8114 | ||
534ca6d6 JA |
8115 | sqd = io_get_sq_data(p); |
8116 | if (IS_ERR(sqd)) { | |
8117 | ret = PTR_ERR(sqd); | |
8118 | goto err; | |
8119 | } | |
69fb2131 | 8120 | |
534ca6d6 | 8121 | ctx->sq_data = sqd; |
69fb2131 JA |
8122 | io_sq_thread_park(sqd); |
8123 | mutex_lock(&sqd->ctx_lock); | |
8124 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8125 | mutex_unlock(&sqd->ctx_lock); | |
8126 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8127 | |
917257da JA |
8128 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8129 | if (!ctx->sq_thread_idle) | |
8130 | ctx->sq_thread_idle = HZ; | |
8131 | ||
aa06165d JA |
8132 | if (sqd->thread) |
8133 | goto done; | |
8134 | ||
6c271ce2 | 8135 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8136 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8137 | |
917257da | 8138 | ret = -EINVAL; |
44a9bd18 JA |
8139 | if (cpu >= nr_cpu_ids) |
8140 | goto err; | |
7889f44d | 8141 | if (!cpu_online(cpu)) |
917257da JA |
8142 | goto err; |
8143 | ||
69fb2131 | 8144 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8145 | cpu, "io_uring-sq"); |
6c271ce2 | 8146 | } else { |
69fb2131 | 8147 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8148 | "io_uring-sq"); |
8149 | } | |
534ca6d6 JA |
8150 | if (IS_ERR(sqd->thread)) { |
8151 | ret = PTR_ERR(sqd->thread); | |
8152 | sqd->thread = NULL; | |
6c271ce2 JA |
8153 | goto err; |
8154 | } | |
534ca6d6 | 8155 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8156 | if (ret) |
8157 | goto err; | |
6c271ce2 JA |
8158 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8159 | /* Can't have SQ_AFF without SQPOLL */ | |
8160 | ret = -EINVAL; | |
8161 | goto err; | |
8162 | } | |
8163 | ||
aa06165d | 8164 | done: |
24369c2e PB |
8165 | ret = io_init_wq_offload(ctx, p); |
8166 | if (ret) | |
2b188cc1 | 8167 | goto err; |
2b188cc1 JA |
8168 | |
8169 | return 0; | |
8170 | err: | |
54a91f3b | 8171 | io_finish_async(ctx); |
2b188cc1 JA |
8172 | return ret; |
8173 | } | |
8174 | ||
7e84e1c7 SG |
8175 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8176 | { | |
534ca6d6 JA |
8177 | struct io_sq_data *sqd = ctx->sq_data; |
8178 | ||
8179 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8180 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8181 | } |
8182 | ||
a087e2b5 BM |
8183 | static inline void __io_unaccount_mem(struct user_struct *user, |
8184 | unsigned long nr_pages) | |
2b188cc1 JA |
8185 | { |
8186 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8187 | } | |
8188 | ||
a087e2b5 BM |
8189 | static inline int __io_account_mem(struct user_struct *user, |
8190 | unsigned long nr_pages) | |
2b188cc1 JA |
8191 | { |
8192 | unsigned long page_limit, cur_pages, new_pages; | |
8193 | ||
8194 | /* Don't allow more pages than we can safely lock */ | |
8195 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8196 | ||
8197 | do { | |
8198 | cur_pages = atomic_long_read(&user->locked_vm); | |
8199 | new_pages = cur_pages + nr_pages; | |
8200 | if (new_pages > page_limit) | |
8201 | return -ENOMEM; | |
8202 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8203 | new_pages) != cur_pages); | |
8204 | ||
8205 | return 0; | |
8206 | } | |
8207 | ||
2e0464d4 BM |
8208 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8209 | enum io_mem_account acct) | |
a087e2b5 | 8210 | { |
aad5d8da | 8211 | if (ctx->limit_mem) |
a087e2b5 | 8212 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8213 | |
2aede0e4 | 8214 | if (ctx->mm_account) { |
4bc4a912 JA |
8215 | if (acct == ACCT_LOCKED) { |
8216 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8217 | ctx->mm_account->locked_vm -= nr_pages; |
4bc4a912 JA |
8218 | mmap_write_unlock(ctx->mm_account); |
8219 | }else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8220 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8221 | } |
2e0464d4 | 8222 | } |
a087e2b5 BM |
8223 | } |
8224 | ||
2e0464d4 BM |
8225 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8226 | enum io_mem_account acct) | |
a087e2b5 | 8227 | { |
30975825 BM |
8228 | int ret; |
8229 | ||
8230 | if (ctx->limit_mem) { | |
8231 | ret = __io_account_mem(ctx->user, nr_pages); | |
8232 | if (ret) | |
8233 | return ret; | |
8234 | } | |
8235 | ||
2aede0e4 | 8236 | if (ctx->mm_account) { |
4bc4a912 JA |
8237 | if (acct == ACCT_LOCKED) { |
8238 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8239 | ctx->mm_account->locked_vm += nr_pages; |
4bc4a912 JA |
8240 | mmap_write_unlock(ctx->mm_account); |
8241 | } else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8242 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8243 | } |
2e0464d4 | 8244 | } |
a087e2b5 BM |
8245 | |
8246 | return 0; | |
8247 | } | |
8248 | ||
2b188cc1 JA |
8249 | static void io_mem_free(void *ptr) |
8250 | { | |
52e04ef4 MR |
8251 | struct page *page; |
8252 | ||
8253 | if (!ptr) | |
8254 | return; | |
2b188cc1 | 8255 | |
52e04ef4 | 8256 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8257 | if (put_page_testzero(page)) |
8258 | free_compound_page(page); | |
8259 | } | |
8260 | ||
8261 | static void *io_mem_alloc(size_t size) | |
8262 | { | |
8263 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
8264 | __GFP_NORETRY; | |
8265 | ||
8266 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8267 | } | |
8268 | ||
75b28aff HV |
8269 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8270 | size_t *sq_offset) | |
8271 | { | |
8272 | struct io_rings *rings; | |
8273 | size_t off, sq_array_size; | |
8274 | ||
8275 | off = struct_size(rings, cqes, cq_entries); | |
8276 | if (off == SIZE_MAX) | |
8277 | return SIZE_MAX; | |
8278 | ||
8279 | #ifdef CONFIG_SMP | |
8280 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8281 | if (off == 0) | |
8282 | return SIZE_MAX; | |
8283 | #endif | |
8284 | ||
b36200f5 DV |
8285 | if (sq_offset) |
8286 | *sq_offset = off; | |
8287 | ||
75b28aff HV |
8288 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8289 | if (sq_array_size == SIZE_MAX) | |
8290 | return SIZE_MAX; | |
8291 | ||
8292 | if (check_add_overflow(off, sq_array_size, &off)) | |
8293 | return SIZE_MAX; | |
8294 | ||
75b28aff HV |
8295 | return off; |
8296 | } | |
8297 | ||
2b188cc1 JA |
8298 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8299 | { | |
75b28aff | 8300 | size_t pages; |
2b188cc1 | 8301 | |
75b28aff HV |
8302 | pages = (size_t)1 << get_order( |
8303 | rings_size(sq_entries, cq_entries, NULL)); | |
8304 | pages += (size_t)1 << get_order( | |
8305 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8306 | |
75b28aff | 8307 | return pages; |
2b188cc1 JA |
8308 | } |
8309 | ||
0a96bbe4 | 8310 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8311 | { |
8312 | int i, j; | |
8313 | ||
8314 | if (!ctx->user_bufs) | |
8315 | return -ENXIO; | |
8316 | ||
8317 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8318 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8319 | ||
8320 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8321 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8322 | |
de293938 JA |
8323 | if (imu->acct_pages) |
8324 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8325 | kvfree(imu->bvec); |
edafccee JA |
8326 | imu->nr_bvecs = 0; |
8327 | } | |
8328 | ||
8329 | kfree(ctx->user_bufs); | |
8330 | ctx->user_bufs = NULL; | |
8331 | ctx->nr_user_bufs = 0; | |
8332 | return 0; | |
8333 | } | |
8334 | ||
8335 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8336 | void __user *arg, unsigned index) | |
8337 | { | |
8338 | struct iovec __user *src; | |
8339 | ||
8340 | #ifdef CONFIG_COMPAT | |
8341 | if (ctx->compat) { | |
8342 | struct compat_iovec __user *ciovs; | |
8343 | struct compat_iovec ciov; | |
8344 | ||
8345 | ciovs = (struct compat_iovec __user *) arg; | |
8346 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8347 | return -EFAULT; | |
8348 | ||
d55e5f5b | 8349 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8350 | dst->iov_len = ciov.iov_len; |
8351 | return 0; | |
8352 | } | |
8353 | #endif | |
8354 | src = (struct iovec __user *) arg; | |
8355 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8356 | return -EFAULT; | |
8357 | return 0; | |
8358 | } | |
8359 | ||
de293938 JA |
8360 | /* |
8361 | * Not super efficient, but this is just a registration time. And we do cache | |
8362 | * the last compound head, so generally we'll only do a full search if we don't | |
8363 | * match that one. | |
8364 | * | |
8365 | * We check if the given compound head page has already been accounted, to | |
8366 | * avoid double accounting it. This allows us to account the full size of the | |
8367 | * page, not just the constituent pages of a huge page. | |
8368 | */ | |
8369 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8370 | int nr_pages, struct page *hpage) | |
8371 | { | |
8372 | int i, j; | |
8373 | ||
8374 | /* check current page array */ | |
8375 | for (i = 0; i < nr_pages; i++) { | |
8376 | if (!PageCompound(pages[i])) | |
8377 | continue; | |
8378 | if (compound_head(pages[i]) == hpage) | |
8379 | return true; | |
8380 | } | |
8381 | ||
8382 | /* check previously registered pages */ | |
8383 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8384 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8385 | ||
8386 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8387 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8388 | continue; | |
8389 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8390 | return true; | |
8391 | } | |
8392 | } | |
8393 | ||
8394 | return false; | |
8395 | } | |
8396 | ||
8397 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8398 | int nr_pages, struct io_mapped_ubuf *imu, | |
8399 | struct page **last_hpage) | |
8400 | { | |
8401 | int i, ret; | |
8402 | ||
8403 | for (i = 0; i < nr_pages; i++) { | |
8404 | if (!PageCompound(pages[i])) { | |
8405 | imu->acct_pages++; | |
8406 | } else { | |
8407 | struct page *hpage; | |
8408 | ||
8409 | hpage = compound_head(pages[i]); | |
8410 | if (hpage == *last_hpage) | |
8411 | continue; | |
8412 | *last_hpage = hpage; | |
8413 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8414 | continue; | |
8415 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8416 | } | |
8417 | } | |
8418 | ||
8419 | if (!imu->acct_pages) | |
8420 | return 0; | |
8421 | ||
8422 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8423 | if (ret) | |
8424 | imu->acct_pages = 0; | |
8425 | return ret; | |
8426 | } | |
8427 | ||
0a96bbe4 BM |
8428 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8429 | struct io_mapped_ubuf *imu, | |
8430 | struct page **last_hpage) | |
edafccee JA |
8431 | { |
8432 | struct vm_area_struct **vmas = NULL; | |
8433 | struct page **pages = NULL; | |
0a96bbe4 BM |
8434 | unsigned long off, start, end, ubuf; |
8435 | size_t size; | |
8436 | int ret, pret, nr_pages, i; | |
8437 | ||
8438 | ubuf = (unsigned long) iov->iov_base; | |
8439 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8440 | start = ubuf >> PAGE_SHIFT; | |
8441 | nr_pages = end - start; | |
8442 | ||
8443 | ret = -ENOMEM; | |
8444 | ||
8445 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8446 | if (!pages) | |
8447 | goto done; | |
8448 | ||
8449 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8450 | GFP_KERNEL); | |
8451 | if (!vmas) | |
8452 | goto done; | |
8453 | ||
8454 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), | |
8455 | GFP_KERNEL); | |
8456 | if (!imu->bvec) | |
8457 | goto done; | |
8458 | ||
8459 | ret = 0; | |
8460 | mmap_read_lock(current->mm); | |
8461 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8462 | pages, vmas); | |
8463 | if (pret == nr_pages) { | |
8464 | /* don't support file backed memory */ | |
8465 | for (i = 0; i < nr_pages; i++) { | |
8466 | struct vm_area_struct *vma = vmas[i]; | |
8467 | ||
8468 | if (vma->vm_file && | |
8469 | !is_file_hugepages(vma->vm_file)) { | |
8470 | ret = -EOPNOTSUPP; | |
8471 | break; | |
8472 | } | |
8473 | } | |
8474 | } else { | |
8475 | ret = pret < 0 ? pret : -EFAULT; | |
8476 | } | |
8477 | mmap_read_unlock(current->mm); | |
8478 | if (ret) { | |
8479 | /* | |
8480 | * if we did partial map, or found file backed vmas, | |
8481 | * release any pages we did get | |
8482 | */ | |
8483 | if (pret > 0) | |
8484 | unpin_user_pages(pages, pret); | |
8485 | kvfree(imu->bvec); | |
8486 | goto done; | |
8487 | } | |
8488 | ||
8489 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8490 | if (ret) { | |
8491 | unpin_user_pages(pages, pret); | |
8492 | kvfree(imu->bvec); | |
8493 | goto done; | |
8494 | } | |
8495 | ||
8496 | off = ubuf & ~PAGE_MASK; | |
8497 | size = iov->iov_len; | |
8498 | for (i = 0; i < nr_pages; i++) { | |
8499 | size_t vec_len; | |
8500 | ||
8501 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8502 | imu->bvec[i].bv_page = pages[i]; | |
8503 | imu->bvec[i].bv_len = vec_len; | |
8504 | imu->bvec[i].bv_offset = off; | |
8505 | off = 0; | |
8506 | size -= vec_len; | |
8507 | } | |
8508 | /* store original address for later verification */ | |
8509 | imu->ubuf = ubuf; | |
8510 | imu->len = iov->iov_len; | |
8511 | imu->nr_bvecs = nr_pages; | |
8512 | ret = 0; | |
8513 | done: | |
8514 | kvfree(pages); | |
8515 | kvfree(vmas); | |
8516 | return ret; | |
8517 | } | |
8518 | ||
2b358604 | 8519 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8520 | { |
edafccee JA |
8521 | if (ctx->user_bufs) |
8522 | return -EBUSY; | |
8523 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8524 | return -EINVAL; | |
8525 | ||
8526 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8527 | GFP_KERNEL); | |
8528 | if (!ctx->user_bufs) | |
8529 | return -ENOMEM; | |
8530 | ||
2b358604 BM |
8531 | return 0; |
8532 | } | |
8533 | ||
8534 | static int io_buffer_validate(struct iovec *iov) | |
8535 | { | |
8536 | /* | |
8537 | * Don't impose further limits on the size and buffer | |
8538 | * constraints here, we'll -EINVAL later when IO is | |
8539 | * submitted if they are wrong. | |
8540 | */ | |
8541 | if (!iov->iov_base || !iov->iov_len) | |
8542 | return -EFAULT; | |
8543 | ||
8544 | /* arbitrary limit, but we need something */ | |
8545 | if (iov->iov_len > SZ_1G) | |
8546 | return -EFAULT; | |
8547 | ||
8548 | return 0; | |
8549 | } | |
8550 | ||
8551 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
8552 | unsigned int nr_args) | |
8553 | { | |
8554 | int i, ret; | |
8555 | struct iovec iov; | |
8556 | struct page *last_hpage = NULL; | |
8557 | ||
8558 | ret = io_buffers_map_alloc(ctx, nr_args); | |
8559 | if (ret) | |
8560 | return ret; | |
8561 | ||
edafccee JA |
8562 | for (i = 0; i < nr_args; i++) { |
8563 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee JA |
8564 | |
8565 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8566 | if (ret) | |
0a96bbe4 | 8567 | break; |
edafccee | 8568 | |
2b358604 BM |
8569 | ret = io_buffer_validate(&iov); |
8570 | if (ret) | |
0a96bbe4 | 8571 | break; |
edafccee | 8572 | |
0a96bbe4 BM |
8573 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8574 | if (ret) | |
8575 | break; | |
edafccee JA |
8576 | |
8577 | ctx->nr_user_bufs++; | |
8578 | } | |
0a96bbe4 BM |
8579 | |
8580 | if (ret) | |
8581 | io_sqe_buffers_unregister(ctx); | |
8582 | ||
edafccee JA |
8583 | return ret; |
8584 | } | |
8585 | ||
9b402849 JA |
8586 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8587 | { | |
8588 | __s32 __user *fds = arg; | |
8589 | int fd; | |
8590 | ||
8591 | if (ctx->cq_ev_fd) | |
8592 | return -EBUSY; | |
8593 | ||
8594 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8595 | return -EFAULT; | |
8596 | ||
8597 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8598 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8599 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8600 | ctx->cq_ev_fd = NULL; | |
8601 | return ret; | |
8602 | } | |
8603 | ||
8604 | return 0; | |
8605 | } | |
8606 | ||
8607 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8608 | { | |
8609 | if (ctx->cq_ev_fd) { | |
8610 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8611 | ctx->cq_ev_fd = NULL; | |
8612 | return 0; | |
8613 | } | |
8614 | ||
8615 | return -ENXIO; | |
8616 | } | |
8617 | ||
5a2e745d JA |
8618 | static int __io_destroy_buffers(int id, void *p, void *data) |
8619 | { | |
8620 | struct io_ring_ctx *ctx = data; | |
8621 | struct io_buffer *buf = p; | |
8622 | ||
067524e9 | 8623 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8624 | return 0; |
8625 | } | |
8626 | ||
8627 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8628 | { | |
8629 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8630 | idr_destroy(&ctx->io_buffer_idr); | |
8631 | } | |
8632 | ||
2b188cc1 JA |
8633 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8634 | { | |
6b06314c | 8635 | io_finish_async(ctx); |
0a96bbe4 | 8636 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8637 | |
8638 | if (ctx->sqo_task) { | |
8639 | put_task_struct(ctx->sqo_task); | |
8640 | ctx->sqo_task = NULL; | |
8641 | mmdrop(ctx->mm_account); | |
8642 | ctx->mm_account = NULL; | |
30975825 | 8643 | } |
def596e9 | 8644 | |
91d8f519 DZ |
8645 | #ifdef CONFIG_BLK_CGROUP |
8646 | if (ctx->sqo_blkcg_css) | |
8647 | css_put(ctx->sqo_blkcg_css); | |
8648 | #endif | |
8649 | ||
6b06314c | 8650 | io_sqe_files_unregister(ctx); |
9b402849 | 8651 | io_eventfd_unregister(ctx); |
5a2e745d | 8652 | io_destroy_buffers(ctx); |
41726c9a | 8653 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8654 | |
2b188cc1 | 8655 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8656 | if (ctx->ring_sock) { |
8657 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8658 | sock_release(ctx->ring_sock); |
355e8d26 | 8659 | } |
2b188cc1 JA |
8660 | #endif |
8661 | ||
75b28aff | 8662 | io_mem_free(ctx->rings); |
2b188cc1 | 8663 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8664 | |
8665 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8666 | free_uid(ctx->user); |
181e448d | 8667 | put_cred(ctx->creds); |
78076bb6 | 8668 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8669 | kfree(ctx); |
8670 | } | |
8671 | ||
8672 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8673 | { | |
8674 | struct io_ring_ctx *ctx = file->private_data; | |
8675 | __poll_t mask = 0; | |
8676 | ||
8677 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8678 | /* |
8679 | * synchronizes with barrier from wq_has_sleeper call in | |
8680 | * io_commit_cqring | |
8681 | */ | |
2b188cc1 | 8682 | smp_rmb(); |
90554200 | 8683 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8684 | mask |= EPOLLOUT | EPOLLWRNORM; |
6c503150 PB |
8685 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
8686 | if (io_cqring_events(ctx)) | |
2b188cc1 JA |
8687 | mask |= EPOLLIN | EPOLLRDNORM; |
8688 | ||
8689 | return mask; | |
8690 | } | |
8691 | ||
8692 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8693 | { | |
8694 | struct io_ring_ctx *ctx = file->private_data; | |
8695 | ||
8696 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8697 | } | |
8698 | ||
0bead8cd | 8699 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8700 | { |
1e6fa521 | 8701 | struct io_identity *iod; |
071698e1 | 8702 | |
1e6fa521 JA |
8703 | iod = idr_remove(&ctx->personality_idr, id); |
8704 | if (iod) { | |
8705 | put_cred(iod->creds); | |
8706 | if (refcount_dec_and_test(&iod->count)) | |
8707 | kfree(iod); | |
0bead8cd | 8708 | return 0; |
1e6fa521 | 8709 | } |
0bead8cd YD |
8710 | |
8711 | return -EINVAL; | |
8712 | } | |
8713 | ||
8714 | static int io_remove_personalities(int id, void *p, void *data) | |
8715 | { | |
8716 | struct io_ring_ctx *ctx = data; | |
8717 | ||
8718 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8719 | return 0; |
8720 | } | |
8721 | ||
85faa7b8 JA |
8722 | static void io_ring_exit_work(struct work_struct *work) |
8723 | { | |
b2edc0a7 PB |
8724 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8725 | exit_work); | |
85faa7b8 | 8726 | |
56952e91 JA |
8727 | /* |
8728 | * If we're doing polled IO and end up having requests being | |
8729 | * submitted async (out-of-line), then completions can come in while | |
8730 | * we're waiting for refs to drop. We need to reap these manually, | |
8731 | * as nobody else will be looking for them. | |
8732 | */ | |
b2edc0a7 | 8733 | do { |
9936c7c2 | 8734 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b2edc0a7 | 8735 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8736 | io_ring_ctx_free(ctx); |
8737 | } | |
8738 | ||
00c18640 JA |
8739 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8740 | { | |
8741 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8742 | ||
8743 | return req->ctx == data; | |
8744 | } | |
8745 | ||
2b188cc1 JA |
8746 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8747 | { | |
8748 | mutex_lock(&ctx->uring_lock); | |
8749 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8750 | |
8751 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8752 | ctx->sqo_dead = 1; | |
8753 | ||
cda286f0 PB |
8754 | /* if force is set, the ring is going away. always drop after that */ |
8755 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8756 | if (ctx->rings) |
6c503150 | 8757 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8758 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8759 | mutex_unlock(&ctx->uring_lock); |
8760 | ||
6b81928d PB |
8761 | io_kill_timeouts(ctx, NULL, NULL); |
8762 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8763 | |
8764 | if (ctx->io_wq) | |
00c18640 | 8765 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8766 | |
15dff286 | 8767 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8768 | io_iopoll_try_reap_events(ctx); |
309fc03a JA |
8769 | |
8770 | /* | |
8771 | * Do this upfront, so we won't have a grace period where the ring | |
8772 | * is closed but resources aren't reaped yet. This can cause | |
8773 | * spurious failure in setting up a new ring. | |
8774 | */ | |
760618f7 JA |
8775 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8776 | ACCT_LOCKED); | |
309fc03a | 8777 | |
85faa7b8 | 8778 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8779 | /* |
8780 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8781 | * if we're exiting a ton of rings at the same time. It just adds | |
8782 | * noise and overhead, there's no discernable change in runtime | |
8783 | * over using system_wq. | |
8784 | */ | |
8785 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8786 | } |
8787 | ||
8788 | static int io_uring_release(struct inode *inode, struct file *file) | |
8789 | { | |
8790 | struct io_ring_ctx *ctx = file->private_data; | |
8791 | ||
8792 | file->private_data = NULL; | |
8793 | io_ring_ctx_wait_and_kill(ctx); | |
8794 | return 0; | |
8795 | } | |
8796 | ||
f6edbabb PB |
8797 | struct io_task_cancel { |
8798 | struct task_struct *task; | |
8799 | struct files_struct *files; | |
8800 | }; | |
f254ac04 | 8801 | |
f6edbabb | 8802 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8803 | { |
9a472ef7 | 8804 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8805 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8806 | bool ret; |
8807 | ||
f6edbabb | 8808 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8809 | unsigned long flags; |
8810 | struct io_ring_ctx *ctx = req->ctx; | |
8811 | ||
8812 | /* protect against races with linked timeouts */ | |
8813 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8814 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8815 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8816 | } else { | |
f6edbabb | 8817 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8818 | } |
8819 | return ret; | |
b711d4ea JA |
8820 | } |
8821 | ||
b7ddce3c | 8822 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8823 | struct task_struct *task, |
b7ddce3c PB |
8824 | struct files_struct *files) |
8825 | { | |
8826 | struct io_defer_entry *de = NULL; | |
8827 | LIST_HEAD(list); | |
8828 | ||
8829 | spin_lock_irq(&ctx->completion_lock); | |
8830 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8831 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8832 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8833 | break; | |
8834 | } | |
8835 | } | |
8836 | spin_unlock_irq(&ctx->completion_lock); | |
8837 | ||
8838 | while (!list_empty(&list)) { | |
8839 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8840 | list_del_init(&de->list); | |
8841 | req_set_fail_links(de->req); | |
8842 | io_put_req(de->req); | |
8843 | io_req_complete(de->req, -ECANCELED); | |
8844 | kfree(de); | |
8845 | } | |
8846 | } | |
8847 | ||
9936c7c2 PB |
8848 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8849 | struct task_struct *task, | |
8850 | struct files_struct *files) | |
8851 | { | |
8852 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
8853 | ||
8854 | while (1) { | |
8855 | enum io_wq_cancel cret; | |
8856 | bool ret = false; | |
8857 | ||
8858 | if (ctx->io_wq) { | |
8859 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, | |
8860 | &cancel, true); | |
8861 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8862 | } | |
8863 | ||
8864 | /* SQPOLL thread does its own polling */ | |
8865 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8866 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8867 | io_iopoll_try_reap_events(ctx); | |
8868 | ret = true; | |
8869 | } | |
8870 | } | |
8871 | ||
8872 | ret |= io_poll_remove_all(ctx, task, files); | |
8873 | ret |= io_kill_timeouts(ctx, task, files); | |
8874 | ret |= io_run_task_work(); | |
8875 | io_cqring_overflow_flush(ctx, true, task, files); | |
8876 | if (!ret) | |
8877 | break; | |
8878 | cond_resched(); | |
8879 | } | |
8880 | } | |
8881 | ||
ca70f00b PB |
8882 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8883 | struct task_struct *task, | |
8884 | struct files_struct *files) | |
8885 | { | |
8886 | struct io_kiocb *req; | |
8887 | int cnt = 0; | |
8888 | ||
8889 | spin_lock_irq(&ctx->inflight_lock); | |
8890 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8891 | cnt += io_match_task(req, task, files); | |
8892 | spin_unlock_irq(&ctx->inflight_lock); | |
8893 | return cnt; | |
8894 | } | |
8895 | ||
b52fda00 | 8896 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8897 | struct task_struct *task, |
fcb323cc JA |
8898 | struct files_struct *files) |
8899 | { | |
fcb323cc | 8900 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 8901 | DEFINE_WAIT(wait); |
ca70f00b | 8902 | int inflight; |
fcb323cc | 8903 | |
ca70f00b PB |
8904 | inflight = io_uring_count_inflight(ctx, task, files); |
8905 | if (!inflight) | |
fcb323cc | 8906 | break; |
f6edbabb | 8907 | |
9936c7c2 | 8908 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b PB |
8909 | prepare_to_wait(&task->io_uring->wait, &wait, |
8910 | TASK_UNINTERRUPTIBLE); | |
8911 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8912 | schedule(); | |
c98de08c | 8913 | finish_wait(&task->io_uring->wait, &wait); |
fcb323cc | 8914 | } |
44e728b8 PB |
8915 | } |
8916 | ||
d9d05217 PB |
8917 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
8918 | { | |
d9d05217 PB |
8919 | mutex_lock(&ctx->uring_lock); |
8920 | ctx->sqo_dead = 1; | |
8921 | mutex_unlock(&ctx->uring_lock); | |
8922 | ||
8923 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
8924 | if (ctx->rings) |
8925 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
8926 | } |
8927 | ||
0f212204 JA |
8928 | /* |
8929 | * We need to iteratively cancel requests, in case a request has dependent | |
8930 | * hard links. These persist even for failure of cancelations, hence keep | |
8931 | * looping until none are found. | |
8932 | */ | |
8933 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8934 | struct files_struct *files) | |
8935 | { | |
8936 | struct task_struct *task = current; | |
8937 | ||
fdaf083c | 8938 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 8939 | io_disable_sqo_submit(ctx); |
534ca6d6 | 8940 | task = ctx->sq_data->thread; |
fdaf083c JA |
8941 | atomic_inc(&task->io_uring->in_idle); |
8942 | io_sq_thread_park(ctx->sq_data); | |
8943 | } | |
0f212204 | 8944 | |
df9923f9 | 8945 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 8946 | |
3a7efd1a | 8947 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 8948 | if (!files) |
9936c7c2 | 8949 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c JA |
8950 | |
8951 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
8952 | atomic_dec(&task->io_uring->in_idle); | |
8953 | /* | |
8954 | * If the files that are going away are the ones in the thread | |
8955 | * identity, clear them out. | |
8956 | */ | |
8957 | if (task->io_uring->identity->files == files) | |
8958 | task->io_uring->identity->files = NULL; | |
8959 | io_sq_thread_unpark(ctx->sq_data); | |
8960 | } | |
0f212204 JA |
8961 | } |
8962 | ||
8963 | /* | |
8964 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8965 | */ | |
fdaf083c | 8966 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8967 | { |
236434c3 | 8968 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 8969 | int ret; |
236434c3 MWO |
8970 | |
8971 | if (unlikely(!tctx)) { | |
0f212204 JA |
8972 | ret = io_uring_alloc_task_context(current); |
8973 | if (unlikely(ret)) | |
8974 | return ret; | |
236434c3 | 8975 | tctx = current->io_uring; |
0f212204 | 8976 | } |
236434c3 MWO |
8977 | if (tctx->last != file) { |
8978 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8979 | |
236434c3 | 8980 | if (!old) { |
0f212204 | 8981 | get_file(file); |
a528b04e PB |
8982 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
8983 | file, GFP_KERNEL)); | |
8984 | if (ret) { | |
8985 | fput(file); | |
8986 | return ret; | |
8987 | } | |
ecfc8492 PB |
8988 | |
8989 | /* one and only SQPOLL file note, held by sqo_task */ | |
8990 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
8991 | current != ctx->sqo_task); | |
0f212204 | 8992 | } |
236434c3 | 8993 | tctx->last = file; |
0f212204 JA |
8994 | } |
8995 | ||
fdaf083c JA |
8996 | /* |
8997 | * This is race safe in that the task itself is doing this, hence it | |
8998 | * cannot be going through the exit/cancel paths at the same time. | |
8999 | * This cannot be modified while exit/cancel is running. | |
9000 | */ | |
9001 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9002 | tctx->sqpoll = true; | |
9003 | ||
0f212204 JA |
9004 | return 0; |
9005 | } | |
9006 | ||
9007 | /* | |
9008 | * Remove this io_uring_file -> task mapping. | |
9009 | */ | |
9010 | static void io_uring_del_task_file(struct file *file) | |
9011 | { | |
9012 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9013 | |
9014 | if (tctx->last == file) | |
9015 | tctx->last = NULL; | |
5e2ed8c4 | 9016 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9017 | if (file) |
9018 | fput(file); | |
9019 | } | |
9020 | ||
de7f1d9e PB |
9021 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9022 | { | |
9023 | struct file *file; | |
9024 | unsigned long index; | |
9025 | ||
9026 | xa_for_each(&tctx->xa, index, file) | |
9027 | io_uring_del_task_file(file); | |
9028 | } | |
9029 | ||
0f212204 JA |
9030 | void __io_uring_files_cancel(struct files_struct *files) |
9031 | { | |
9032 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9033 | struct file *file; |
9034 | unsigned long index; | |
0f212204 JA |
9035 | |
9036 | /* make sure overflow events are dropped */ | |
fdaf083c | 9037 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9038 | xa_for_each(&tctx->xa, index, file) |
9039 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9040 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9041 | |
9042 | if (files) | |
9043 | io_uring_remove_task_files(tctx); | |
fdaf083c JA |
9044 | } |
9045 | ||
9046 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9047 | { | |
0e9ddb39 PB |
9048 | return percpu_counter_sum(&tctx->inflight); |
9049 | } | |
fdaf083c | 9050 | |
0e9ddb39 PB |
9051 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
9052 | { | |
9053 | struct io_uring_task *tctx; | |
9054 | s64 inflight; | |
9055 | DEFINE_WAIT(wait); | |
fdaf083c | 9056 | |
0e9ddb39 PB |
9057 | if (!ctx->sq_data) |
9058 | return; | |
9059 | tctx = ctx->sq_data->thread->io_uring; | |
9060 | io_disable_sqo_submit(ctx); | |
fdaf083c | 9061 | |
0e9ddb39 PB |
9062 | atomic_inc(&tctx->in_idle); |
9063 | do { | |
9064 | /* read completions before cancelations */ | |
9065 | inflight = tctx_inflight(tctx); | |
9066 | if (!inflight) | |
9067 | break; | |
9068 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 9069 | |
0e9ddb39 PB |
9070 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
9071 | /* | |
9072 | * If we've seen completions, retry without waiting. This | |
9073 | * avoids a race where a completion comes in before we did | |
9074 | * prepare_to_wait(). | |
9075 | */ | |
9076 | if (inflight == tctx_inflight(tctx)) | |
9077 | schedule(); | |
9078 | finish_wait(&tctx->wait, &wait); | |
9079 | } while (1); | |
9080 | atomic_dec(&tctx->in_idle); | |
0f212204 JA |
9081 | } |
9082 | ||
0f212204 JA |
9083 | /* |
9084 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9085 | * requests. | |
9086 | */ | |
9087 | void __io_uring_task_cancel(void) | |
9088 | { | |
9089 | struct io_uring_task *tctx = current->io_uring; | |
9090 | DEFINE_WAIT(wait); | |
d8a6df10 | 9091 | s64 inflight; |
0f212204 JA |
9092 | |
9093 | /* make sure overflow events are dropped */ | |
fdaf083c | 9094 | atomic_inc(&tctx->in_idle); |
0f212204 | 9095 | |
0b5cd6c3 | 9096 | /* trigger io_disable_sqo_submit() */ |
0e9ddb39 PB |
9097 | if (tctx->sqpoll) { |
9098 | struct file *file; | |
9099 | unsigned long index; | |
9100 | ||
9101 | xa_for_each(&tctx->xa, index, file) | |
9102 | io_uring_cancel_sqpoll(file->private_data); | |
9103 | } | |
0b5cd6c3 | 9104 | |
d8a6df10 | 9105 | do { |
0f212204 | 9106 | /* read completions before cancelations */ |
fdaf083c | 9107 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9108 | if (!inflight) |
9109 | break; | |
0f212204 JA |
9110 | __io_uring_files_cancel(NULL); |
9111 | ||
9112 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9113 | ||
9114 | /* | |
a1bb3cd5 PB |
9115 | * If we've seen completions, retry without waiting. This |
9116 | * avoids a race where a completion comes in before we did | |
9117 | * prepare_to_wait(). | |
0f212204 | 9118 | */ |
a1bb3cd5 PB |
9119 | if (inflight == tctx_inflight(tctx)) |
9120 | schedule(); | |
f57555ed | 9121 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9122 | } while (1); |
0f212204 | 9123 | |
fdaf083c | 9124 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9125 | |
9126 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9127 | } |
9128 | ||
fcb323cc JA |
9129 | static int io_uring_flush(struct file *file, void *data) |
9130 | { | |
6b5733eb | 9131 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9132 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9133 | |
84965ff8 JA |
9134 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
9135 | io_uring_cancel_task_requests(ctx, NULL); | |
9136 | ||
6b5733eb | 9137 | if (!tctx) |
4f793dc4 PB |
9138 | return 0; |
9139 | ||
6b5733eb PB |
9140 | /* we should have cancelled and erased it before PF_EXITING */ |
9141 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9142 | xa_load(&tctx->xa, (unsigned long)file)); | |
9143 | ||
4f793dc4 PB |
9144 | /* |
9145 | * fput() is pending, will be 2 if the only other ref is our potential | |
9146 | * task file note. If the task is exiting, drop regardless of count. | |
9147 | */ | |
6b5733eb PB |
9148 | if (atomic_long_read(&file->f_count) != 2) |
9149 | return 0; | |
4f793dc4 | 9150 | |
d9d05217 PB |
9151 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9152 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9153 | WARN_ON_ONCE(ctx->sqo_task != current && |
9154 | xa_load(&tctx->xa, (unsigned long)file)); | |
9155 | /* sqo_dead check is for when this happens after cancellation */ | |
9156 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9157 | !xa_load(&tctx->xa, (unsigned long)file)); |
9158 | ||
9159 | io_disable_sqo_submit(ctx); | |
9160 | } | |
9161 | ||
9162 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9163 | io_uring_del_task_file(file); | |
fcb323cc JA |
9164 | return 0; |
9165 | } | |
9166 | ||
6c5c240e RP |
9167 | static void *io_uring_validate_mmap_request(struct file *file, |
9168 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9169 | { |
2b188cc1 | 9170 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9171 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9172 | struct page *page; |
9173 | void *ptr; | |
9174 | ||
9175 | switch (offset) { | |
9176 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9177 | case IORING_OFF_CQ_RING: |
9178 | ptr = ctx->rings; | |
2b188cc1 JA |
9179 | break; |
9180 | case IORING_OFF_SQES: | |
9181 | ptr = ctx->sq_sqes; | |
9182 | break; | |
2b188cc1 | 9183 | default: |
6c5c240e | 9184 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9185 | } |
9186 | ||
9187 | page = virt_to_head_page(ptr); | |
a50b854e | 9188 | if (sz > page_size(page)) |
6c5c240e RP |
9189 | return ERR_PTR(-EINVAL); |
9190 | ||
9191 | return ptr; | |
9192 | } | |
9193 | ||
9194 | #ifdef CONFIG_MMU | |
9195 | ||
9196 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9197 | { | |
9198 | size_t sz = vma->vm_end - vma->vm_start; | |
9199 | unsigned long pfn; | |
9200 | void *ptr; | |
9201 | ||
9202 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9203 | if (IS_ERR(ptr)) | |
9204 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9205 | |
9206 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9207 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9208 | } | |
9209 | ||
6c5c240e RP |
9210 | #else /* !CONFIG_MMU */ |
9211 | ||
9212 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9213 | { | |
9214 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9215 | } | |
9216 | ||
9217 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9218 | { | |
9219 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9220 | } | |
9221 | ||
9222 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9223 | unsigned long addr, unsigned long len, | |
9224 | unsigned long pgoff, unsigned long flags) | |
9225 | { | |
9226 | void *ptr; | |
9227 | ||
9228 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9229 | if (IS_ERR(ptr)) | |
9230 | return PTR_ERR(ptr); | |
9231 | ||
9232 | return (unsigned long) ptr; | |
9233 | } | |
9234 | ||
9235 | #endif /* !CONFIG_MMU */ | |
9236 | ||
d9d05217 | 9237 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9238 | { |
d9d05217 | 9239 | int ret = 0; |
90554200 JA |
9240 | DEFINE_WAIT(wait); |
9241 | ||
9242 | do { | |
9243 | if (!io_sqring_full(ctx)) | |
9244 | break; | |
9245 | ||
9246 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9247 | ||
d9d05217 PB |
9248 | if (unlikely(ctx->sqo_dead)) { |
9249 | ret = -EOWNERDEAD; | |
9250 | goto out; | |
9251 | } | |
9252 | ||
90554200 JA |
9253 | if (!io_sqring_full(ctx)) |
9254 | break; | |
9255 | ||
9256 | schedule(); | |
9257 | } while (!signal_pending(current)); | |
9258 | ||
9259 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9260 | out: |
9261 | return ret; | |
90554200 JA |
9262 | } |
9263 | ||
c73ebb68 HX |
9264 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9265 | struct __kernel_timespec __user **ts, | |
9266 | const sigset_t __user **sig) | |
9267 | { | |
9268 | struct io_uring_getevents_arg arg; | |
9269 | ||
9270 | /* | |
9271 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9272 | * is just a pointer to the sigset_t. | |
9273 | */ | |
9274 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9275 | *sig = (const sigset_t __user *) argp; | |
9276 | *ts = NULL; | |
9277 | return 0; | |
9278 | } | |
9279 | ||
9280 | /* | |
9281 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9282 | * timespec and sigset_t pointers if good. | |
9283 | */ | |
9284 | if (*argsz != sizeof(arg)) | |
9285 | return -EINVAL; | |
9286 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9287 | return -EFAULT; | |
9288 | *sig = u64_to_user_ptr(arg.sigmask); | |
9289 | *argsz = arg.sigmask_sz; | |
9290 | *ts = u64_to_user_ptr(arg.ts); | |
9291 | return 0; | |
9292 | } | |
9293 | ||
2b188cc1 | 9294 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9295 | u32, min_complete, u32, flags, const void __user *, argp, |
9296 | size_t, argsz) | |
2b188cc1 JA |
9297 | { |
9298 | struct io_ring_ctx *ctx; | |
9299 | long ret = -EBADF; | |
9300 | int submitted = 0; | |
9301 | struct fd f; | |
9302 | ||
4c6e277c | 9303 | io_run_task_work(); |
b41e9852 | 9304 | |
90554200 | 9305 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9306 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9307 | return -EINVAL; |
9308 | ||
9309 | f = fdget(fd); | |
9310 | if (!f.file) | |
9311 | return -EBADF; | |
9312 | ||
9313 | ret = -EOPNOTSUPP; | |
9314 | if (f.file->f_op != &io_uring_fops) | |
9315 | goto out_fput; | |
9316 | ||
9317 | ret = -ENXIO; | |
9318 | ctx = f.file->private_data; | |
9319 | if (!percpu_ref_tryget(&ctx->refs)) | |
9320 | goto out_fput; | |
9321 | ||
7e84e1c7 SG |
9322 | ret = -EBADFD; |
9323 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9324 | goto out; | |
9325 | ||
6c271ce2 JA |
9326 | /* |
9327 | * For SQ polling, the thread will do all submissions and completions. | |
9328 | * Just return the requested submit count, and wake the thread if | |
9329 | * we were asked to. | |
9330 | */ | |
b2a9eada | 9331 | ret = 0; |
6c271ce2 | 9332 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9333 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9334 | |
d9d05217 PB |
9335 | ret = -EOWNERDEAD; |
9336 | if (unlikely(ctx->sqo_dead)) | |
9337 | goto out; | |
6c271ce2 | 9338 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9339 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9340 | if (flags & IORING_ENTER_SQ_WAIT) { |
9341 | ret = io_sqpoll_wait_sq(ctx); | |
9342 | if (ret) | |
9343 | goto out; | |
9344 | } | |
6c271ce2 | 9345 | submitted = to_submit; |
b2a9eada | 9346 | } else if (to_submit) { |
fdaf083c | 9347 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9348 | if (unlikely(ret)) |
9349 | goto out; | |
2b188cc1 | 9350 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9351 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9352 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9353 | |
9354 | if (submitted != to_submit) | |
9355 | goto out; | |
2b188cc1 JA |
9356 | } |
9357 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9358 | const sigset_t __user *sig; |
9359 | struct __kernel_timespec __user *ts; | |
9360 | ||
9361 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9362 | if (unlikely(ret)) | |
9363 | goto out; | |
9364 | ||
2b188cc1 JA |
9365 | min_complete = min(min_complete, ctx->cq_entries); |
9366 | ||
32b2244a XW |
9367 | /* |
9368 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9369 | * space applications don't need to do io completion events | |
9370 | * polling again, they can rely on io_sq_thread to do polling | |
9371 | * work, which can reduce cpu usage and uring_lock contention. | |
9372 | */ | |
9373 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9374 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9375 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9376 | } else { |
c73ebb68 | 9377 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9378 | } |
2b188cc1 JA |
9379 | } |
9380 | ||
7c504e65 | 9381 | out: |
6805b32e | 9382 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9383 | out_fput: |
9384 | fdput(f); | |
9385 | return submitted ? submitted : ret; | |
9386 | } | |
9387 | ||
bebdb65e | 9388 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9389 | static int io_uring_show_cred(int id, void *p, void *data) |
9390 | { | |
6b47ab81 JA |
9391 | struct io_identity *iod = p; |
9392 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9393 | struct seq_file *m = data; |
9394 | struct user_namespace *uns = seq_user_ns(m); | |
9395 | struct group_info *gi; | |
9396 | kernel_cap_t cap; | |
9397 | unsigned __capi; | |
9398 | int g; | |
9399 | ||
9400 | seq_printf(m, "%5d\n", id); | |
9401 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9402 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9403 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9404 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9405 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9406 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9407 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9408 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9409 | seq_puts(m, "\n\tGroups:\t"); | |
9410 | gi = cred->group_info; | |
9411 | for (g = 0; g < gi->ngroups; g++) { | |
9412 | seq_put_decimal_ull(m, g ? " " : "", | |
9413 | from_kgid_munged(uns, gi->gid[g])); | |
9414 | } | |
9415 | seq_puts(m, "\n\tCapEff:\t"); | |
9416 | cap = cred->cap_effective; | |
9417 | CAP_FOR_EACH_U32(__capi) | |
9418 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9419 | seq_putc(m, '\n'); | |
9420 | return 0; | |
9421 | } | |
9422 | ||
9423 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9424 | { | |
dbbe9c64 | 9425 | struct io_sq_data *sq = NULL; |
fad8e0de | 9426 | bool has_lock; |
87ce955b JA |
9427 | int i; |
9428 | ||
fad8e0de JA |
9429 | /* |
9430 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9431 | * since fdinfo case grabs it in the opposite direction of normal use | |
9432 | * cases. If we fail to get the lock, we just don't iterate any | |
9433 | * structures that could be going away outside the io_uring mutex. | |
9434 | */ | |
9435 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9436 | ||
dbbe9c64 JQ |
9437 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9438 | sq = ctx->sq_data; | |
9439 | ||
9440 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9441 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9442 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9443 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9444 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9445 | |
87ce955b JA |
9446 | if (f) |
9447 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9448 | else | |
9449 | seq_printf(m, "%5u: <none>\n", i); | |
9450 | } | |
9451 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9452 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9453 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9454 | ||
9455 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9456 | (unsigned int) buf->len); | |
9457 | } | |
fad8e0de | 9458 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9459 | seq_printf(m, "Personalities:\n"); |
9460 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9461 | } | |
d7718a9d JA |
9462 | seq_printf(m, "PollList:\n"); |
9463 | spin_lock_irq(&ctx->completion_lock); | |
9464 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9465 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9466 | struct io_kiocb *req; | |
9467 | ||
9468 | hlist_for_each_entry(req, list, hash_node) | |
9469 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9470 | req->task->task_works != NULL); | |
9471 | } | |
9472 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9473 | if (has_lock) |
9474 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9475 | } |
9476 | ||
9477 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9478 | { | |
9479 | struct io_ring_ctx *ctx = f->private_data; | |
9480 | ||
9481 | if (percpu_ref_tryget(&ctx->refs)) { | |
9482 | __io_uring_show_fdinfo(ctx, m); | |
9483 | percpu_ref_put(&ctx->refs); | |
9484 | } | |
9485 | } | |
bebdb65e | 9486 | #endif |
87ce955b | 9487 | |
2b188cc1 JA |
9488 | static const struct file_operations io_uring_fops = { |
9489 | .release = io_uring_release, | |
fcb323cc | 9490 | .flush = io_uring_flush, |
2b188cc1 | 9491 | .mmap = io_uring_mmap, |
6c5c240e RP |
9492 | #ifndef CONFIG_MMU |
9493 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9494 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9495 | #endif | |
2b188cc1 JA |
9496 | .poll = io_uring_poll, |
9497 | .fasync = io_uring_fasync, | |
bebdb65e | 9498 | #ifdef CONFIG_PROC_FS |
87ce955b | 9499 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9500 | #endif |
2b188cc1 JA |
9501 | }; |
9502 | ||
9503 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9504 | struct io_uring_params *p) | |
9505 | { | |
75b28aff HV |
9506 | struct io_rings *rings; |
9507 | size_t size, sq_array_offset; | |
2b188cc1 | 9508 | |
bd740481 JA |
9509 | /* make sure these are sane, as we already accounted them */ |
9510 | ctx->sq_entries = p->sq_entries; | |
9511 | ctx->cq_entries = p->cq_entries; | |
9512 | ||
75b28aff HV |
9513 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9514 | if (size == SIZE_MAX) | |
9515 | return -EOVERFLOW; | |
9516 | ||
9517 | rings = io_mem_alloc(size); | |
9518 | if (!rings) | |
2b188cc1 JA |
9519 | return -ENOMEM; |
9520 | ||
75b28aff HV |
9521 | ctx->rings = rings; |
9522 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9523 | rings->sq_ring_mask = p->sq_entries - 1; | |
9524 | rings->cq_ring_mask = p->cq_entries - 1; | |
9525 | rings->sq_ring_entries = p->sq_entries; | |
9526 | rings->cq_ring_entries = p->cq_entries; | |
9527 | ctx->sq_mask = rings->sq_ring_mask; | |
9528 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9529 | |
9530 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9531 | if (size == SIZE_MAX) { |
9532 | io_mem_free(ctx->rings); | |
9533 | ctx->rings = NULL; | |
2b188cc1 | 9534 | return -EOVERFLOW; |
eb065d30 | 9535 | } |
2b188cc1 JA |
9536 | |
9537 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9538 | if (!ctx->sq_sqes) { |
9539 | io_mem_free(ctx->rings); | |
9540 | ctx->rings = NULL; | |
2b188cc1 | 9541 | return -ENOMEM; |
eb065d30 | 9542 | } |
2b188cc1 | 9543 | |
2b188cc1 JA |
9544 | return 0; |
9545 | } | |
9546 | ||
9faadcc8 PB |
9547 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9548 | { | |
9549 | int ret, fd; | |
9550 | ||
9551 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9552 | if (fd < 0) | |
9553 | return fd; | |
9554 | ||
9555 | ret = io_uring_add_task_file(ctx, file); | |
9556 | if (ret) { | |
9557 | put_unused_fd(fd); | |
9558 | return ret; | |
9559 | } | |
9560 | fd_install(fd, file); | |
9561 | return fd; | |
9562 | } | |
9563 | ||
2b188cc1 JA |
9564 | /* |
9565 | * Allocate an anonymous fd, this is what constitutes the application | |
9566 | * visible backing of an io_uring instance. The application mmaps this | |
9567 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9568 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9569 | */ | |
9faadcc8 | 9570 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9571 | { |
9572 | struct file *file; | |
9faadcc8 | 9573 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9574 | int ret; |
9575 | ||
2b188cc1 JA |
9576 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9577 | &ctx->ring_sock); | |
9578 | if (ret) | |
9faadcc8 | 9579 | return ERR_PTR(ret); |
2b188cc1 JA |
9580 | #endif |
9581 | ||
2b188cc1 JA |
9582 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9583 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9584 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9585 | if (IS_ERR(file)) { |
9586 | sock_release(ctx->ring_sock); | |
9587 | ctx->ring_sock = NULL; | |
9588 | } else { | |
9589 | ctx->ring_sock->file = file; | |
0f212204 | 9590 | } |
2b188cc1 | 9591 | #endif |
9faadcc8 | 9592 | return file; |
2b188cc1 JA |
9593 | } |
9594 | ||
7f13657d XW |
9595 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9596 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9597 | { |
9598 | struct user_struct *user = NULL; | |
9599 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9600 | struct file *file; |
aad5d8da | 9601 | bool limit_mem; |
2b188cc1 JA |
9602 | int ret; |
9603 | ||
8110c1a6 | 9604 | if (!entries) |
2b188cc1 | 9605 | return -EINVAL; |
8110c1a6 JA |
9606 | if (entries > IORING_MAX_ENTRIES) { |
9607 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9608 | return -EINVAL; | |
9609 | entries = IORING_MAX_ENTRIES; | |
9610 | } | |
2b188cc1 JA |
9611 | |
9612 | /* | |
9613 | * Use twice as many entries for the CQ ring. It's possible for the | |
9614 | * application to drive a higher depth than the size of the SQ ring, | |
9615 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9616 | * some flexibility in overcommitting a bit. If the application has |
9617 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9618 | * of CQ ring entries manually. | |
2b188cc1 JA |
9619 | */ |
9620 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9621 | if (p->flags & IORING_SETUP_CQSIZE) { |
9622 | /* | |
9623 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9624 | * to a power-of-two, if it isn't already. We do NOT impose | |
9625 | * any cq vs sq ring sizing. | |
9626 | */ | |
eb2667b3 | 9627 | if (!p->cq_entries) |
33a107f0 | 9628 | return -EINVAL; |
8110c1a6 JA |
9629 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9630 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9631 | return -EINVAL; | |
9632 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9633 | } | |
eb2667b3 JQ |
9634 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9635 | if (p->cq_entries < p->sq_entries) | |
9636 | return -EINVAL; | |
33a107f0 JA |
9637 | } else { |
9638 | p->cq_entries = 2 * p->sq_entries; | |
9639 | } | |
2b188cc1 JA |
9640 | |
9641 | user = get_uid(current_user()); | |
aad5d8da | 9642 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9643 | |
aad5d8da | 9644 | if (limit_mem) { |
a087e2b5 | 9645 | ret = __io_account_mem(user, |
2b188cc1 JA |
9646 | ring_pages(p->sq_entries, p->cq_entries)); |
9647 | if (ret) { | |
9648 | free_uid(user); | |
9649 | return ret; | |
9650 | } | |
9651 | } | |
9652 | ||
9653 | ctx = io_ring_ctx_alloc(p); | |
9654 | if (!ctx) { | |
aad5d8da | 9655 | if (limit_mem) |
a087e2b5 | 9656 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9657 | p->cq_entries)); |
9658 | free_uid(user); | |
9659 | return -ENOMEM; | |
9660 | } | |
9661 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9662 | ctx->user = user; |
0b8c0ec7 | 9663 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9664 | #ifdef CONFIG_AUDIT |
9665 | ctx->loginuid = current->loginuid; | |
9666 | ctx->sessionid = current->sessionid; | |
9667 | #endif | |
2aede0e4 JA |
9668 | ctx->sqo_task = get_task_struct(current); |
9669 | ||
9670 | /* | |
9671 | * This is just grabbed for accounting purposes. When a process exits, | |
9672 | * the mm is exited and dropped before the files, hence we need to hang | |
9673 | * on to this mm purely for the purposes of being able to unaccount | |
9674 | * memory (locked/pinned vm). It's not used for anything else. | |
9675 | */ | |
6b7898eb | 9676 | mmgrab(current->mm); |
2aede0e4 | 9677 | ctx->mm_account = current->mm; |
6b7898eb | 9678 | |
91d8f519 DZ |
9679 | #ifdef CONFIG_BLK_CGROUP |
9680 | /* | |
9681 | * The sq thread will belong to the original cgroup it was inited in. | |
9682 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9683 | * issued bios will be associated with the closest cgroup later in the | |
9684 | * block layer. | |
9685 | */ | |
9686 | rcu_read_lock(); | |
9687 | ctx->sqo_blkcg_css = blkcg_css(); | |
9688 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9689 | rcu_read_unlock(); | |
9690 | if (!ret) { | |
9691 | /* don't init against a dying cgroup, have the user try again */ | |
9692 | ctx->sqo_blkcg_css = NULL; | |
9693 | ret = -ENODEV; | |
9694 | goto err; | |
9695 | } | |
9696 | #endif | |
6b7898eb | 9697 | |
f74441e6 JA |
9698 | /* |
9699 | * Account memory _before_ installing the file descriptor. Once | |
9700 | * the descriptor is installed, it can get closed at any time. Also | |
9701 | * do this before hitting the general error path, as ring freeing | |
9702 | * will un-account as well. | |
9703 | */ | |
9704 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9705 | ACCT_LOCKED); | |
9706 | ctx->limit_mem = limit_mem; | |
9707 | ||
2b188cc1 JA |
9708 | ret = io_allocate_scq_urings(ctx, p); |
9709 | if (ret) | |
9710 | goto err; | |
9711 | ||
7e84e1c7 | 9712 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9713 | if (ret) |
9714 | goto err; | |
9715 | ||
7e84e1c7 SG |
9716 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9717 | io_sq_offload_start(ctx); | |
9718 | ||
2b188cc1 | 9719 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9720 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9721 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9722 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9723 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9724 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9725 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9726 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9727 | |
9728 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9729 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9730 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9731 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9732 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9733 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9734 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9735 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9736 | |
7f13657d XW |
9737 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9738 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9739 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9740 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9741 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9742 | |
9743 | if (copy_to_user(params, p, sizeof(*p))) { | |
9744 | ret = -EFAULT; | |
9745 | goto err; | |
9746 | } | |
d1719f70 | 9747 | |
9faadcc8 PB |
9748 | file = io_uring_get_file(ctx); |
9749 | if (IS_ERR(file)) { | |
9750 | ret = PTR_ERR(file); | |
9751 | goto err; | |
9752 | } | |
9753 | ||
044c1ab3 JA |
9754 | /* |
9755 | * Install ring fd as the very last thing, so we don't risk someone | |
9756 | * having closed it before we finish setup | |
9757 | */ | |
9faadcc8 PB |
9758 | ret = io_uring_install_fd(ctx, file); |
9759 | if (ret < 0) { | |
06585c49 | 9760 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9761 | /* fput will clean it up */ |
9762 | fput(file); | |
9763 | return ret; | |
9764 | } | |
044c1ab3 | 9765 | |
c826bd7a | 9766 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9767 | return ret; |
9768 | err: | |
d9d05217 | 9769 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9770 | io_ring_ctx_wait_and_kill(ctx); |
9771 | return ret; | |
9772 | } | |
9773 | ||
9774 | /* | |
9775 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9776 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9777 | * params structure passed in. | |
9778 | */ | |
9779 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9780 | { | |
9781 | struct io_uring_params p; | |
2b188cc1 JA |
9782 | int i; |
9783 | ||
9784 | if (copy_from_user(&p, params, sizeof(p))) | |
9785 | return -EFAULT; | |
9786 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9787 | if (p.resv[i]) | |
9788 | return -EINVAL; | |
9789 | } | |
9790 | ||
6c271ce2 | 9791 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9792 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9793 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9794 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9795 | return -EINVAL; |
9796 | ||
7f13657d | 9797 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9798 | } |
9799 | ||
9800 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9801 | struct io_uring_params __user *, params) | |
9802 | { | |
9803 | return io_uring_setup(entries, params); | |
9804 | } | |
9805 | ||
66f4af93 JA |
9806 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9807 | { | |
9808 | struct io_uring_probe *p; | |
9809 | size_t size; | |
9810 | int i, ret; | |
9811 | ||
9812 | size = struct_size(p, ops, nr_args); | |
9813 | if (size == SIZE_MAX) | |
9814 | return -EOVERFLOW; | |
9815 | p = kzalloc(size, GFP_KERNEL); | |
9816 | if (!p) | |
9817 | return -ENOMEM; | |
9818 | ||
9819 | ret = -EFAULT; | |
9820 | if (copy_from_user(p, arg, size)) | |
9821 | goto out; | |
9822 | ret = -EINVAL; | |
9823 | if (memchr_inv(p, 0, size)) | |
9824 | goto out; | |
9825 | ||
9826 | p->last_op = IORING_OP_LAST - 1; | |
9827 | if (nr_args > IORING_OP_LAST) | |
9828 | nr_args = IORING_OP_LAST; | |
9829 | ||
9830 | for (i = 0; i < nr_args; i++) { | |
9831 | p->ops[i].op = i; | |
9832 | if (!io_op_defs[i].not_supported) | |
9833 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9834 | } | |
9835 | p->ops_len = i; | |
9836 | ||
9837 | ret = 0; | |
9838 | if (copy_to_user(arg, p, size)) | |
9839 | ret = -EFAULT; | |
9840 | out: | |
9841 | kfree(p); | |
9842 | return ret; | |
9843 | } | |
9844 | ||
071698e1 JA |
9845 | static int io_register_personality(struct io_ring_ctx *ctx) |
9846 | { | |
1e6fa521 JA |
9847 | struct io_identity *id; |
9848 | int ret; | |
071698e1 | 9849 | |
1e6fa521 JA |
9850 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9851 | if (unlikely(!id)) | |
9852 | return -ENOMEM; | |
9853 | ||
9854 | io_init_identity(id); | |
9855 | id->creds = get_current_cred(); | |
9856 | ||
9857 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9858 | if (ret < 0) { | |
9859 | put_cred(id->creds); | |
9860 | kfree(id); | |
9861 | } | |
9862 | return ret; | |
071698e1 JA |
9863 | } |
9864 | ||
21b55dbc SG |
9865 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9866 | unsigned int nr_args) | |
9867 | { | |
9868 | struct io_uring_restriction *res; | |
9869 | size_t size; | |
9870 | int i, ret; | |
9871 | ||
7e84e1c7 SG |
9872 | /* Restrictions allowed only if rings started disabled */ |
9873 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9874 | return -EBADFD; | |
9875 | ||
21b55dbc | 9876 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9877 | if (ctx->restrictions.registered) |
21b55dbc SG |
9878 | return -EBUSY; |
9879 | ||
9880 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9881 | return -EINVAL; | |
9882 | ||
9883 | size = array_size(nr_args, sizeof(*res)); | |
9884 | if (size == SIZE_MAX) | |
9885 | return -EOVERFLOW; | |
9886 | ||
9887 | res = memdup_user(arg, size); | |
9888 | if (IS_ERR(res)) | |
9889 | return PTR_ERR(res); | |
9890 | ||
9891 | ret = 0; | |
9892 | ||
9893 | for (i = 0; i < nr_args; i++) { | |
9894 | switch (res[i].opcode) { | |
9895 | case IORING_RESTRICTION_REGISTER_OP: | |
9896 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9897 | ret = -EINVAL; | |
9898 | goto out; | |
9899 | } | |
9900 | ||
9901 | __set_bit(res[i].register_op, | |
9902 | ctx->restrictions.register_op); | |
9903 | break; | |
9904 | case IORING_RESTRICTION_SQE_OP: | |
9905 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9906 | ret = -EINVAL; | |
9907 | goto out; | |
9908 | } | |
9909 | ||
9910 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9911 | break; | |
9912 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9913 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9914 | break; | |
9915 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9916 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9917 | break; | |
9918 | default: | |
9919 | ret = -EINVAL; | |
9920 | goto out; | |
9921 | } | |
9922 | } | |
9923 | ||
9924 | out: | |
9925 | /* Reset all restrictions if an error happened */ | |
9926 | if (ret != 0) | |
9927 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9928 | else | |
7e84e1c7 | 9929 | ctx->restrictions.registered = true; |
21b55dbc SG |
9930 | |
9931 | kfree(res); | |
9932 | return ret; | |
9933 | } | |
9934 | ||
7e84e1c7 SG |
9935 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9936 | { | |
9937 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9938 | return -EBADFD; | |
9939 | ||
9940 | if (ctx->restrictions.registered) | |
9941 | ctx->restricted = 1; | |
9942 | ||
9943 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9944 | ||
9945 | io_sq_offload_start(ctx); | |
9946 | ||
9947 | return 0; | |
9948 | } | |
9949 | ||
071698e1 JA |
9950 | static bool io_register_op_must_quiesce(int op) |
9951 | { | |
9952 | switch (op) { | |
9953 | case IORING_UNREGISTER_FILES: | |
9954 | case IORING_REGISTER_FILES_UPDATE: | |
9955 | case IORING_REGISTER_PROBE: | |
9956 | case IORING_REGISTER_PERSONALITY: | |
9957 | case IORING_UNREGISTER_PERSONALITY: | |
9958 | return false; | |
9959 | default: | |
9960 | return true; | |
9961 | } | |
9962 | } | |
9963 | ||
edafccee JA |
9964 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9965 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9966 | __releases(ctx->uring_lock) |
9967 | __acquires(ctx->uring_lock) | |
edafccee JA |
9968 | { |
9969 | int ret; | |
9970 | ||
35fa71a0 JA |
9971 | /* |
9972 | * We're inside the ring mutex, if the ref is already dying, then | |
9973 | * someone else killed the ctx or is already going through | |
9974 | * io_uring_register(). | |
9975 | */ | |
9976 | if (percpu_ref_is_dying(&ctx->refs)) | |
9977 | return -ENXIO; | |
9978 | ||
071698e1 | 9979 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9980 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9981 | |
05f3fb3c JA |
9982 | /* |
9983 | * Drop uring mutex before waiting for references to exit. If | |
9984 | * another thread is currently inside io_uring_enter() it might | |
9985 | * need to grab the uring_lock to make progress. If we hold it | |
9986 | * here across the drain wait, then we can deadlock. It's safe | |
9987 | * to drop the mutex here, since no new references will come in | |
9988 | * after we've killed the percpu ref. | |
9989 | */ | |
9990 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9991 | do { |
9992 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9993 | if (!ret) | |
9994 | break; | |
ed6930c9 JA |
9995 | ret = io_run_task_work_sig(); |
9996 | if (ret < 0) | |
9997 | break; | |
af9c1a44 JA |
9998 | } while (1); |
9999 | ||
05f3fb3c | 10000 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10001 | |
c150368b JA |
10002 | if (ret) { |
10003 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
10004 | goto out_quiesce; |
10005 | } | |
10006 | } | |
10007 | ||
10008 | if (ctx->restricted) { | |
10009 | if (opcode >= IORING_REGISTER_LAST) { | |
10010 | ret = -EINVAL; | |
10011 | goto out; | |
10012 | } | |
10013 | ||
10014 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
10015 | ret = -EACCES; | |
c150368b JA |
10016 | goto out; |
10017 | } | |
05f3fb3c | 10018 | } |
edafccee JA |
10019 | |
10020 | switch (opcode) { | |
10021 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 10022 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
10023 | break; |
10024 | case IORING_UNREGISTER_BUFFERS: | |
10025 | ret = -EINVAL; | |
10026 | if (arg || nr_args) | |
10027 | break; | |
0a96bbe4 | 10028 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10029 | break; |
6b06314c JA |
10030 | case IORING_REGISTER_FILES: |
10031 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
10032 | break; | |
10033 | case IORING_UNREGISTER_FILES: | |
10034 | ret = -EINVAL; | |
10035 | if (arg || nr_args) | |
10036 | break; | |
10037 | ret = io_sqe_files_unregister(ctx); | |
10038 | break; | |
c3a31e60 JA |
10039 | case IORING_REGISTER_FILES_UPDATE: |
10040 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
10041 | break; | |
9b402849 | 10042 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10043 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10044 | ret = -EINVAL; |
10045 | if (nr_args != 1) | |
10046 | break; | |
10047 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10048 | if (ret) |
10049 | break; | |
10050 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10051 | ctx->eventfd_async = 1; | |
10052 | else | |
10053 | ctx->eventfd_async = 0; | |
9b402849 JA |
10054 | break; |
10055 | case IORING_UNREGISTER_EVENTFD: | |
10056 | ret = -EINVAL; | |
10057 | if (arg || nr_args) | |
10058 | break; | |
10059 | ret = io_eventfd_unregister(ctx); | |
10060 | break; | |
66f4af93 JA |
10061 | case IORING_REGISTER_PROBE: |
10062 | ret = -EINVAL; | |
10063 | if (!arg || nr_args > 256) | |
10064 | break; | |
10065 | ret = io_probe(ctx, arg, nr_args); | |
10066 | break; | |
071698e1 JA |
10067 | case IORING_REGISTER_PERSONALITY: |
10068 | ret = -EINVAL; | |
10069 | if (arg || nr_args) | |
10070 | break; | |
10071 | ret = io_register_personality(ctx); | |
10072 | break; | |
10073 | case IORING_UNREGISTER_PERSONALITY: | |
10074 | ret = -EINVAL; | |
10075 | if (arg) | |
10076 | break; | |
10077 | ret = io_unregister_personality(ctx, nr_args); | |
10078 | break; | |
7e84e1c7 SG |
10079 | case IORING_REGISTER_ENABLE_RINGS: |
10080 | ret = -EINVAL; | |
10081 | if (arg || nr_args) | |
10082 | break; | |
10083 | ret = io_register_enable_rings(ctx); | |
10084 | break; | |
21b55dbc SG |
10085 | case IORING_REGISTER_RESTRICTIONS: |
10086 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10087 | break; | |
edafccee JA |
10088 | default: |
10089 | ret = -EINVAL; | |
10090 | break; | |
10091 | } | |
10092 | ||
21b55dbc | 10093 | out: |
071698e1 | 10094 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10095 | /* bring the ctx back to life */ |
05f3fb3c | 10096 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 10097 | out_quiesce: |
0f158b4c | 10098 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10099 | } |
edafccee JA |
10100 | return ret; |
10101 | } | |
10102 | ||
10103 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10104 | void __user *, arg, unsigned int, nr_args) | |
10105 | { | |
10106 | struct io_ring_ctx *ctx; | |
10107 | long ret = -EBADF; | |
10108 | struct fd f; | |
10109 | ||
10110 | f = fdget(fd); | |
10111 | if (!f.file) | |
10112 | return -EBADF; | |
10113 | ||
10114 | ret = -EOPNOTSUPP; | |
10115 | if (f.file->f_op != &io_uring_fops) | |
10116 | goto out_fput; | |
10117 | ||
10118 | ctx = f.file->private_data; | |
10119 | ||
10120 | mutex_lock(&ctx->uring_lock); | |
10121 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10122 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10123 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10124 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10125 | out_fput: |
10126 | fdput(f); | |
10127 | return ret; | |
10128 | } | |
10129 | ||
2b188cc1 JA |
10130 | static int __init io_uring_init(void) |
10131 | { | |
d7f62e82 SM |
10132 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10133 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10134 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10135 | } while (0) | |
10136 | ||
10137 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10138 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10139 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10140 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10141 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10142 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10143 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10144 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10145 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10146 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10147 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10148 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10149 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10150 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10151 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10152 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10153 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10154 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10155 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10156 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10157 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10158 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10159 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10160 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10161 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10162 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10163 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10164 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10165 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10166 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10167 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10168 | |
d3656344 | 10169 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10170 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
10171 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
10172 | return 0; | |
10173 | }; | |
10174 | __initcall(io_uring_init); |