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