]>
Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
14 | * through a control-dependency in io_get_cqring (smp_store_release to | |
15 | * store head will do). Failure to do so could lead to reading invalid | |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
6c271ce2 | 60 | #include <linux/kthread.h> |
2b188cc1 | 61 | #include <linux/blkdev.h> |
edafccee | 62 | #include <linux/bvec.h> |
2b188cc1 JA |
63 | #include <linux/net.h> |
64 | #include <net/sock.h> | |
65 | #include <net/af_unix.h> | |
6b06314c | 66 | #include <net/scm.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
ff002b30 | 78 | #include <linux/fs_struct.h> |
7d67af2c | 79 | #include <linux/splice.h> |
b41e9852 | 80 | #include <linux/task_work.h> |
bcf5a063 | 81 | #include <linux/pagemap.h> |
0f212204 | 82 | #include <linux/io_uring.h> |
91d8f519 | 83 | #include <linux/blk-cgroup.h> |
4ea33a97 | 84 | #include <linux/audit.h> |
2b188cc1 | 85 | |
c826bd7a DD |
86 | #define CREATE_TRACE_POINTS |
87 | #include <trace/events/io_uring.h> | |
88 | ||
2b188cc1 JA |
89 | #include <uapi/linux/io_uring.h> |
90 | ||
91 | #include "internal.h" | |
561fb04a | 92 | #include "io-wq.h" |
2b188cc1 | 93 | |
5277deaa | 94 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
96 | |
97 | /* | |
98 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
99 | */ | |
100 | #define IORING_FILE_TABLE_SHIFT 9 | |
101 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
102 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
103 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
104 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
105 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 106 | |
b16fed66 PB |
107 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
108 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
109 | IOSQE_BUFFER_SELECT) | |
110 | ||
2b188cc1 JA |
111 | struct io_uring { |
112 | u32 head ____cacheline_aligned_in_smp; | |
113 | u32 tail ____cacheline_aligned_in_smp; | |
114 | }; | |
115 | ||
1e84b97b | 116 | /* |
75b28aff HV |
117 | * This data is shared with the application through the mmap at offsets |
118 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
119 | * |
120 | * The offsets to the member fields are published through struct | |
121 | * io_sqring_offsets when calling io_uring_setup. | |
122 | */ | |
75b28aff | 123 | struct io_rings { |
1e84b97b SB |
124 | /* |
125 | * Head and tail offsets into the ring; the offsets need to be | |
126 | * masked to get valid indices. | |
127 | * | |
75b28aff HV |
128 | * The kernel controls head of the sq ring and the tail of the cq ring, |
129 | * and the application controls tail of the sq ring and the head of the | |
130 | * cq ring. | |
1e84b97b | 131 | */ |
75b28aff | 132 | struct io_uring sq, cq; |
1e84b97b | 133 | /* |
75b28aff | 134 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
135 | * ring_entries - 1) |
136 | */ | |
75b28aff HV |
137 | u32 sq_ring_mask, cq_ring_mask; |
138 | /* Ring sizes (constant, power of 2) */ | |
139 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
140 | /* |
141 | * Number of invalid entries dropped by the kernel due to | |
142 | * invalid index stored in array | |
143 | * | |
144 | * Written by the kernel, shouldn't be modified by the | |
145 | * application (i.e. get number of "new events" by comparing to | |
146 | * cached value). | |
147 | * | |
148 | * After a new SQ head value was read by the application this | |
149 | * counter includes all submissions that were dropped reaching | |
150 | * the new SQ head (and possibly more). | |
151 | */ | |
75b28aff | 152 | u32 sq_dropped; |
1e84b97b | 153 | /* |
0d9b5b3a | 154 | * Runtime SQ flags |
1e84b97b SB |
155 | * |
156 | * Written by the kernel, shouldn't be modified by the | |
157 | * application. | |
158 | * | |
159 | * The application needs a full memory barrier before checking | |
160 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
161 | */ | |
75b28aff | 162 | u32 sq_flags; |
0d9b5b3a SG |
163 | /* |
164 | * Runtime CQ flags | |
165 | * | |
166 | * Written by the application, shouldn't be modified by the | |
167 | * kernel. | |
168 | */ | |
169 | u32 cq_flags; | |
1e84b97b SB |
170 | /* |
171 | * Number of completion events lost because the queue was full; | |
172 | * this should be avoided by the application by making sure | |
0b4295b5 | 173 | * there are not more requests pending than there is space in |
1e84b97b SB |
174 | * the completion queue. |
175 | * | |
176 | * Written by the kernel, shouldn't be modified by the | |
177 | * application (i.e. get number of "new events" by comparing to | |
178 | * cached value). | |
179 | * | |
180 | * As completion events come in out of order this counter is not | |
181 | * ordered with any other data. | |
182 | */ | |
75b28aff | 183 | u32 cq_overflow; |
1e84b97b SB |
184 | /* |
185 | * Ring buffer of completion events. | |
186 | * | |
187 | * The kernel writes completion events fresh every time they are | |
188 | * produced, so the application is allowed to modify pending | |
189 | * entries. | |
190 | */ | |
75b28aff | 191 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
192 | }; |
193 | ||
45d189c6 PB |
194 | enum io_uring_cmd_flags { |
195 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 196 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
197 | }; |
198 | ||
edafccee JA |
199 | struct io_mapped_ubuf { |
200 | u64 ubuf; | |
201 | size_t len; | |
202 | struct bio_vec *bvec; | |
203 | unsigned int nr_bvecs; | |
de293938 | 204 | unsigned long acct_pages; |
edafccee JA |
205 | }; |
206 | ||
50238531 BM |
207 | struct io_ring_ctx; |
208 | ||
269bbe5f BM |
209 | struct io_rsrc_put { |
210 | struct list_head list; | |
50238531 BM |
211 | union { |
212 | void *rsrc; | |
213 | struct file *file; | |
214 | }; | |
269bbe5f BM |
215 | }; |
216 | ||
217 | struct fixed_rsrc_table { | |
65e19f54 | 218 | struct file **files; |
31b51510 JA |
219 | }; |
220 | ||
269bbe5f | 221 | struct fixed_rsrc_ref_node { |
05589553 XW |
222 | struct percpu_ref refs; |
223 | struct list_head node; | |
269bbe5f BM |
224 | struct list_head rsrc_list; |
225 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
226 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
227 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 228 | struct llist_node llist; |
e297822b | 229 | bool done; |
05589553 XW |
230 | }; |
231 | ||
269bbe5f BM |
232 | struct fixed_rsrc_data { |
233 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
234 | struct io_ring_ctx *ctx; |
235 | ||
269bbe5f | 236 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 237 | struct percpu_ref refs; |
05f3fb3c | 238 | struct completion done; |
8bad28d8 | 239 | bool quiesce; |
05f3fb3c JA |
240 | }; |
241 | ||
5a2e745d JA |
242 | struct io_buffer { |
243 | struct list_head list; | |
244 | __u64 addr; | |
245 | __s32 len; | |
246 | __u16 bid; | |
247 | }; | |
248 | ||
21b55dbc SG |
249 | struct io_restriction { |
250 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
251 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
252 | u8 sqe_flags_allowed; | |
253 | u8 sqe_flags_required; | |
7e84e1c7 | 254 | bool registered; |
21b55dbc SG |
255 | }; |
256 | ||
534ca6d6 JA |
257 | struct io_sq_data { |
258 | refcount_t refs; | |
69fb2131 JA |
259 | struct mutex lock; |
260 | ||
261 | /* ctx's that are using this sqd */ | |
262 | struct list_head ctx_list; | |
263 | struct list_head ctx_new_list; | |
264 | struct mutex ctx_lock; | |
265 | ||
534ca6d6 JA |
266 | struct task_struct *thread; |
267 | struct wait_queue_head wait; | |
08369246 XW |
268 | |
269 | unsigned sq_thread_idle; | |
534ca6d6 JA |
270 | }; |
271 | ||
258b29a9 | 272 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 273 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 274 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 275 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
276 | |
277 | struct io_comp_state { | |
6dd0be1e | 278 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 279 | unsigned int nr; |
c7dae4ba JA |
280 | unsigned int locked_free_nr; |
281 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 282 | struct list_head free_list; |
c7dae4ba JA |
283 | /* IRQ completion list, under ->completion_lock */ |
284 | struct list_head locked_free_list; | |
258b29a9 PB |
285 | }; |
286 | ||
a1ab7b35 PB |
287 | struct io_submit_link { |
288 | struct io_kiocb *head; | |
289 | struct io_kiocb *last; | |
290 | }; | |
291 | ||
258b29a9 PB |
292 | struct io_submit_state { |
293 | struct blk_plug plug; | |
a1ab7b35 | 294 | struct io_submit_link link; |
258b29a9 PB |
295 | |
296 | /* | |
297 | * io_kiocb alloc cache | |
298 | */ | |
bf019da7 | 299 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
300 | unsigned int free_reqs; |
301 | ||
302 | bool plug_started; | |
303 | ||
304 | /* | |
305 | * Batch completion logic | |
306 | */ | |
307 | struct io_comp_state comp; | |
308 | ||
309 | /* | |
310 | * File reference cache | |
311 | */ | |
312 | struct file *file; | |
313 | unsigned int fd; | |
314 | unsigned int file_refs; | |
315 | unsigned int ios_left; | |
316 | }; | |
317 | ||
2b188cc1 JA |
318 | struct io_ring_ctx { |
319 | struct { | |
320 | struct percpu_ref refs; | |
321 | } ____cacheline_aligned_in_smp; | |
322 | ||
323 | struct { | |
324 | unsigned int flags; | |
e1d85334 | 325 | unsigned int compat: 1; |
aad5d8da | 326 | unsigned int limit_mem: 1; |
e1d85334 RD |
327 | unsigned int cq_overflow_flushed: 1; |
328 | unsigned int drain_next: 1; | |
329 | unsigned int eventfd_async: 1; | |
21b55dbc | 330 | unsigned int restricted: 1; |
d9d05217 | 331 | unsigned int sqo_dead: 1; |
2b188cc1 | 332 | |
75b28aff HV |
333 | /* |
334 | * Ring buffer of indices into array of io_uring_sqe, which is | |
335 | * mmapped by the application using the IORING_OFF_SQES offset. | |
336 | * | |
337 | * This indirection could e.g. be used to assign fixed | |
338 | * io_uring_sqe entries to operations and only submit them to | |
339 | * the queue when needed. | |
340 | * | |
341 | * The kernel modifies neither the indices array nor the entries | |
342 | * array. | |
343 | */ | |
344 | u32 *sq_array; | |
2b188cc1 JA |
345 | unsigned cached_sq_head; |
346 | unsigned sq_entries; | |
347 | unsigned sq_mask; | |
6c271ce2 | 348 | unsigned sq_thread_idle; |
498ccd9e | 349 | unsigned cached_sq_dropped; |
2c3bac6d | 350 | unsigned cached_cq_overflow; |
ad3eb2c8 | 351 | unsigned long sq_check_overflow; |
de0617e4 JA |
352 | |
353 | struct list_head defer_list; | |
5262f567 | 354 | struct list_head timeout_list; |
1d7bb1d5 | 355 | struct list_head cq_overflow_list; |
fcb323cc | 356 | |
ad3eb2c8 | 357 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
358 | } ____cacheline_aligned_in_smp; |
359 | ||
3c1a2ead JA |
360 | struct { |
361 | struct mutex uring_lock; | |
362 | wait_queue_head_t wait; | |
363 | } ____cacheline_aligned_in_smp; | |
364 | ||
365 | struct io_submit_state submit_state; | |
366 | ||
206aefde JA |
367 | struct io_rings *rings; |
368 | ||
2aede0e4 JA |
369 | /* |
370 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
371 | * have access to the ->files | |
372 | */ | |
373 | struct task_struct *sqo_task; | |
374 | ||
375 | /* Only used for accounting purposes */ | |
376 | struct mm_struct *mm_account; | |
377 | ||
91d8f519 DZ |
378 | #ifdef CONFIG_BLK_CGROUP |
379 | struct cgroup_subsys_state *sqo_blkcg_css; | |
380 | #endif | |
381 | ||
534ca6d6 JA |
382 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
383 | ||
90554200 | 384 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 385 | struct list_head sqd_list; |
75b28aff | 386 | |
6b06314c JA |
387 | /* |
388 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
389 | * readers must ensure that ->refs is alive as long as the file* is | |
390 | * used. Only updated through io_uring_register(2). | |
391 | */ | |
269bbe5f | 392 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
393 | unsigned nr_user_files; |
394 | ||
edafccee JA |
395 | /* if used, fixed mapped user buffers */ |
396 | unsigned nr_user_bufs; | |
397 | struct io_mapped_ubuf *user_bufs; | |
398 | ||
2b188cc1 JA |
399 | struct user_struct *user; |
400 | ||
0b8c0ec7 | 401 | const struct cred *creds; |
181e448d | 402 | |
4ea33a97 JA |
403 | #ifdef CONFIG_AUDIT |
404 | kuid_t loginuid; | |
405 | unsigned int sessionid; | |
406 | #endif | |
407 | ||
0f158b4c JA |
408 | struct completion ref_comp; |
409 | struct completion sq_thread_comp; | |
206aefde JA |
410 | |
411 | #if defined(CONFIG_UNIX) | |
412 | struct socket *ring_sock; | |
413 | #endif | |
414 | ||
5a2e745d JA |
415 | struct idr io_buffer_idr; |
416 | ||
071698e1 JA |
417 | struct idr personality_idr; |
418 | ||
206aefde JA |
419 | struct { |
420 | unsigned cached_cq_tail; | |
421 | unsigned cq_entries; | |
422 | unsigned cq_mask; | |
423 | atomic_t cq_timeouts; | |
f010505b | 424 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 425 | unsigned long cq_check_overflow; |
206aefde JA |
426 | struct wait_queue_head cq_wait; |
427 | struct fasync_struct *cq_fasync; | |
428 | struct eventfd_ctx *cq_ev_fd; | |
429 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 430 | |
2b188cc1 JA |
431 | struct { |
432 | spinlock_t completion_lock; | |
e94f141b | 433 | |
def596e9 | 434 | /* |
540e32a0 | 435 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
436 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
437 | * For SQPOLL, only the single threaded io_sq_thread() will | |
438 | * manipulate the list, hence no extra locking is needed there. | |
439 | */ | |
540e32a0 | 440 | struct list_head iopoll_list; |
78076bb6 JA |
441 | struct hlist_head *cancel_hash; |
442 | unsigned cancel_hash_bits; | |
e94f141b | 443 | bool poll_multi_file; |
31b51510 | 444 | |
fcb323cc JA |
445 | spinlock_t inflight_lock; |
446 | struct list_head inflight_list; | |
2b188cc1 | 447 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 448 | |
269bbe5f BM |
449 | struct delayed_work rsrc_put_work; |
450 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
451 | struct list_head rsrc_ref_list; |
452 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 453 | |
21b55dbc | 454 | struct io_restriction restrictions; |
3c1a2ead | 455 | |
7c25c0d1 JA |
456 | /* exit task_work */ |
457 | struct callback_head *exit_task_work; | |
458 | ||
3c1a2ead JA |
459 | /* Keep this last, we don't need it for the fast path */ |
460 | struct work_struct exit_work; | |
2b188cc1 JA |
461 | }; |
462 | ||
09bb8394 JA |
463 | /* |
464 | * First field must be the file pointer in all the | |
465 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
466 | */ | |
221c5eb2 JA |
467 | struct io_poll_iocb { |
468 | struct file *file; | |
018043be | 469 | struct wait_queue_head *head; |
221c5eb2 | 470 | __poll_t events; |
8c838788 | 471 | bool done; |
221c5eb2 | 472 | bool canceled; |
392edb45 | 473 | struct wait_queue_entry wait; |
221c5eb2 JA |
474 | }; |
475 | ||
018043be PB |
476 | struct io_poll_remove { |
477 | struct file *file; | |
478 | u64 addr; | |
479 | }; | |
480 | ||
b5dba59e JA |
481 | struct io_close { |
482 | struct file *file; | |
b5dba59e JA |
483 | int fd; |
484 | }; | |
485 | ||
ad8a48ac JA |
486 | struct io_timeout_data { |
487 | struct io_kiocb *req; | |
488 | struct hrtimer timer; | |
489 | struct timespec64 ts; | |
490 | enum hrtimer_mode mode; | |
491 | }; | |
492 | ||
8ed8d3c3 JA |
493 | struct io_accept { |
494 | struct file *file; | |
495 | struct sockaddr __user *addr; | |
496 | int __user *addr_len; | |
497 | int flags; | |
09952e3e | 498 | unsigned long nofile; |
8ed8d3c3 JA |
499 | }; |
500 | ||
501 | struct io_sync { | |
502 | struct file *file; | |
503 | loff_t len; | |
504 | loff_t off; | |
505 | int flags; | |
d63d1b5e | 506 | int mode; |
8ed8d3c3 JA |
507 | }; |
508 | ||
fbf23849 JA |
509 | struct io_cancel { |
510 | struct file *file; | |
511 | u64 addr; | |
512 | }; | |
513 | ||
b29472ee JA |
514 | struct io_timeout { |
515 | struct file *file; | |
bfe68a22 PB |
516 | u32 off; |
517 | u32 target_seq; | |
135fcde8 | 518 | struct list_head list; |
90cd7e42 PB |
519 | /* head of the link, used by linked timeouts only */ |
520 | struct io_kiocb *head; | |
b29472ee JA |
521 | }; |
522 | ||
0bdf7a2d PB |
523 | struct io_timeout_rem { |
524 | struct file *file; | |
525 | u64 addr; | |
9c8e11b3 PB |
526 | |
527 | /* timeout update */ | |
528 | struct timespec64 ts; | |
529 | u32 flags; | |
0bdf7a2d PB |
530 | }; |
531 | ||
9adbd45d JA |
532 | struct io_rw { |
533 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
534 | struct kiocb kiocb; | |
535 | u64 addr; | |
536 | u64 len; | |
537 | }; | |
538 | ||
3fbb51c1 JA |
539 | struct io_connect { |
540 | struct file *file; | |
541 | struct sockaddr __user *addr; | |
542 | int addr_len; | |
543 | }; | |
544 | ||
e47293fd JA |
545 | struct io_sr_msg { |
546 | struct file *file; | |
fddaface | 547 | union { |
270a5940 | 548 | struct user_msghdr __user *umsg; |
fddaface JA |
549 | void __user *buf; |
550 | }; | |
e47293fd | 551 | int msg_flags; |
bcda7baa | 552 | int bgid; |
fddaface | 553 | size_t len; |
bcda7baa | 554 | struct io_buffer *kbuf; |
e47293fd JA |
555 | }; |
556 | ||
15b71abe JA |
557 | struct io_open { |
558 | struct file *file; | |
559 | int dfd; | |
15b71abe | 560 | struct filename *filename; |
c12cedf2 | 561 | struct open_how how; |
4022e7af | 562 | unsigned long nofile; |
15b71abe JA |
563 | }; |
564 | ||
269bbe5f | 565 | struct io_rsrc_update { |
05f3fb3c JA |
566 | struct file *file; |
567 | u64 arg; | |
568 | u32 nr_args; | |
569 | u32 offset; | |
570 | }; | |
571 | ||
4840e418 JA |
572 | struct io_fadvise { |
573 | struct file *file; | |
574 | u64 offset; | |
575 | u32 len; | |
576 | u32 advice; | |
577 | }; | |
578 | ||
c1ca757b JA |
579 | struct io_madvise { |
580 | struct file *file; | |
581 | u64 addr; | |
582 | u32 len; | |
583 | u32 advice; | |
584 | }; | |
585 | ||
3e4827b0 JA |
586 | struct io_epoll { |
587 | struct file *file; | |
588 | int epfd; | |
589 | int op; | |
590 | int fd; | |
591 | struct epoll_event event; | |
e47293fd JA |
592 | }; |
593 | ||
7d67af2c PB |
594 | struct io_splice { |
595 | struct file *file_out; | |
596 | struct file *file_in; | |
597 | loff_t off_out; | |
598 | loff_t off_in; | |
599 | u64 len; | |
600 | unsigned int flags; | |
601 | }; | |
602 | ||
ddf0322d JA |
603 | struct io_provide_buf { |
604 | struct file *file; | |
605 | __u64 addr; | |
606 | __s32 len; | |
607 | __u32 bgid; | |
608 | __u16 nbufs; | |
609 | __u16 bid; | |
610 | }; | |
611 | ||
1d9e1288 BM |
612 | struct io_statx { |
613 | struct file *file; | |
614 | int dfd; | |
615 | unsigned int mask; | |
616 | unsigned int flags; | |
e62753e4 | 617 | const char __user *filename; |
1d9e1288 BM |
618 | struct statx __user *buffer; |
619 | }; | |
620 | ||
36f4fa68 JA |
621 | struct io_shutdown { |
622 | struct file *file; | |
623 | int how; | |
624 | }; | |
625 | ||
80a261fd JA |
626 | struct io_rename { |
627 | struct file *file; | |
628 | int old_dfd; | |
629 | int new_dfd; | |
630 | struct filename *oldpath; | |
631 | struct filename *newpath; | |
632 | int flags; | |
633 | }; | |
634 | ||
14a1143b JA |
635 | struct io_unlink { |
636 | struct file *file; | |
637 | int dfd; | |
638 | int flags; | |
639 | struct filename *filename; | |
640 | }; | |
641 | ||
3ca405eb PB |
642 | struct io_completion { |
643 | struct file *file; | |
644 | struct list_head list; | |
0f7e466b | 645 | int cflags; |
3ca405eb PB |
646 | }; |
647 | ||
f499a021 JA |
648 | struct io_async_connect { |
649 | struct sockaddr_storage address; | |
650 | }; | |
651 | ||
03b1230c JA |
652 | struct io_async_msghdr { |
653 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
654 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
655 | struct iovec *free_iov; | |
03b1230c JA |
656 | struct sockaddr __user *uaddr; |
657 | struct msghdr msg; | |
b537916c | 658 | struct sockaddr_storage addr; |
03b1230c JA |
659 | }; |
660 | ||
f67676d1 JA |
661 | struct io_async_rw { |
662 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
663 | const struct iovec *free_iovec; |
664 | struct iov_iter iter; | |
227c0c96 | 665 | size_t bytes_done; |
bcf5a063 | 666 | struct wait_page_queue wpq; |
f67676d1 JA |
667 | }; |
668 | ||
6b47ee6e PB |
669 | enum { |
670 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
671 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
672 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
673 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
674 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 675 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 676 | |
6b47ee6e PB |
677 | REQ_F_FAIL_LINK_BIT, |
678 | REQ_F_INFLIGHT_BIT, | |
679 | REQ_F_CUR_POS_BIT, | |
680 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 681 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 682 | REQ_F_ISREG_BIT, |
99bc4c38 | 683 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 684 | REQ_F_POLLED_BIT, |
bcda7baa | 685 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 686 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 687 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 688 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 689 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
690 | |
691 | /* not a real bit, just to check we're not overflowing the space */ | |
692 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
693 | }; |
694 | ||
695 | enum { | |
696 | /* ctx owns file */ | |
697 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
698 | /* drain existing IO first */ | |
699 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
700 | /* linked sqes */ | |
701 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
702 | /* doesn't sever on completion < 0 */ | |
703 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
704 | /* IOSQE_ASYNC */ | |
705 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
706 | /* IOSQE_BUFFER_SELECT */ |
707 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 708 | |
6b47ee6e PB |
709 | /* fail rest of links */ |
710 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
711 | /* on inflight list */ | |
712 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
713 | /* read/write uses file position */ | |
714 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
715 | /* must not punt to workers */ | |
716 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 717 | /* has or had linked timeout */ |
6b47ee6e | 718 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
719 | /* regular file */ |
720 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
721 | /* needs cleanup */ |
722 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
723 | /* already went through poll handler */ |
724 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
725 | /* buffer already selected */ |
726 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
727 | /* doesn't need file table for this request */ |
728 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
729 | /* io_wq_work is initialized */ |
730 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
731 | /* linked timeout is active, i.e. prepared by link's head */ |
732 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
733 | /* completion is deferred through io_comp_state */ |
734 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
735 | }; |
736 | ||
737 | struct async_poll { | |
738 | struct io_poll_iocb poll; | |
807abcb0 | 739 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
740 | }; |
741 | ||
7cbf1722 JA |
742 | struct io_task_work { |
743 | struct io_wq_work_node node; | |
744 | task_work_func_t func; | |
745 | }; | |
746 | ||
09bb8394 JA |
747 | /* |
748 | * NOTE! Each of the iocb union members has the file pointer | |
749 | * as the first entry in their struct definition. So you can | |
750 | * access the file pointer through any of the sub-structs, | |
751 | * or directly as just 'ki_filp' in this struct. | |
752 | */ | |
2b188cc1 | 753 | struct io_kiocb { |
221c5eb2 | 754 | union { |
09bb8394 | 755 | struct file *file; |
9adbd45d | 756 | struct io_rw rw; |
221c5eb2 | 757 | struct io_poll_iocb poll; |
018043be | 758 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
759 | struct io_accept accept; |
760 | struct io_sync sync; | |
fbf23849 | 761 | struct io_cancel cancel; |
b29472ee | 762 | struct io_timeout timeout; |
0bdf7a2d | 763 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 764 | struct io_connect connect; |
e47293fd | 765 | struct io_sr_msg sr_msg; |
15b71abe | 766 | struct io_open open; |
b5dba59e | 767 | struct io_close close; |
269bbe5f | 768 | struct io_rsrc_update rsrc_update; |
4840e418 | 769 | struct io_fadvise fadvise; |
c1ca757b | 770 | struct io_madvise madvise; |
3e4827b0 | 771 | struct io_epoll epoll; |
7d67af2c | 772 | struct io_splice splice; |
ddf0322d | 773 | struct io_provide_buf pbuf; |
1d9e1288 | 774 | struct io_statx statx; |
36f4fa68 | 775 | struct io_shutdown shutdown; |
80a261fd | 776 | struct io_rename rename; |
14a1143b | 777 | struct io_unlink unlink; |
3ca405eb PB |
778 | /* use only after cleaning per-op data, see io_clean_op() */ |
779 | struct io_completion compl; | |
221c5eb2 | 780 | }; |
2b188cc1 | 781 | |
e8c2bc1f JA |
782 | /* opcode allocated if it needs to store data for async defer */ |
783 | void *async_data; | |
d625c6ee | 784 | u8 opcode; |
65a6543d XW |
785 | /* polled IO has completed */ |
786 | u8 iopoll_completed; | |
2b188cc1 | 787 | |
4f4eeba8 | 788 | u16 buf_index; |
9cf7c104 | 789 | u32 result; |
4f4eeba8 | 790 | |
010e8e6b PB |
791 | struct io_ring_ctx *ctx; |
792 | unsigned int flags; | |
793 | refcount_t refs; | |
794 | struct task_struct *task; | |
795 | u64 user_data; | |
d7718a9d | 796 | |
f2f87370 | 797 | struct io_kiocb *link; |
269bbe5f | 798 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 799 | |
d21ffe7e PB |
800 | /* |
801 | * 1. used with ctx->iopoll_list with reads/writes | |
802 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
803 | */ | |
010e8e6b | 804 | struct list_head inflight_entry; |
7cbf1722 JA |
805 | union { |
806 | struct io_task_work io_task_work; | |
807 | struct callback_head task_work; | |
808 | }; | |
010e8e6b PB |
809 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
810 | struct hlist_node hash_node; | |
811 | struct async_poll *apoll; | |
812 | struct io_wq_work work; | |
2b188cc1 | 813 | }; |
05589553 | 814 | |
27dc8338 PB |
815 | struct io_defer_entry { |
816 | struct list_head list; | |
817 | struct io_kiocb *req; | |
9cf7c104 | 818 | u32 seq; |
2b188cc1 JA |
819 | }; |
820 | ||
d3656344 | 821 | struct io_op_def { |
d3656344 JA |
822 | /* needs req->file assigned */ |
823 | unsigned needs_file : 1; | |
d3656344 JA |
824 | /* hash wq insertion if file is a regular file */ |
825 | unsigned hash_reg_file : 1; | |
826 | /* unbound wq insertion if file is a non-regular file */ | |
827 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
828 | /* opcode is not supported by this kernel */ |
829 | unsigned not_supported : 1; | |
8a72758c JA |
830 | /* set if opcode supports polled "wait" */ |
831 | unsigned pollin : 1; | |
832 | unsigned pollout : 1; | |
bcda7baa JA |
833 | /* op supports buffer selection */ |
834 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
835 | /* must always have async data allocated */ |
836 | unsigned needs_async_data : 1; | |
27926b68 JA |
837 | /* should block plug */ |
838 | unsigned plug : 1; | |
e8c2bc1f JA |
839 | /* size of async data needed, if any */ |
840 | unsigned short async_size; | |
0f203765 | 841 | unsigned work_flags; |
d3656344 JA |
842 | }; |
843 | ||
0918682b | 844 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
845 | [IORING_OP_NOP] = {}, |
846 | [IORING_OP_READV] = { | |
d3656344 JA |
847 | .needs_file = 1, |
848 | .unbound_nonreg_file = 1, | |
8a72758c | 849 | .pollin = 1, |
4d954c25 | 850 | .buffer_select = 1, |
e8c2bc1f | 851 | .needs_async_data = 1, |
27926b68 | 852 | .plug = 1, |
e8c2bc1f | 853 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 854 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 855 | }, |
0463b6c5 | 856 | [IORING_OP_WRITEV] = { |
d3656344 JA |
857 | .needs_file = 1, |
858 | .hash_reg_file = 1, | |
859 | .unbound_nonreg_file = 1, | |
8a72758c | 860 | .pollout = 1, |
e8c2bc1f | 861 | .needs_async_data = 1, |
27926b68 | 862 | .plug = 1, |
e8c2bc1f | 863 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
864 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
865 | IO_WQ_WORK_FSIZE, | |
d3656344 | 866 | }, |
0463b6c5 | 867 | [IORING_OP_FSYNC] = { |
d3656344 | 868 | .needs_file = 1, |
0f203765 | 869 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 870 | }, |
0463b6c5 | 871 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
872 | .needs_file = 1, |
873 | .unbound_nonreg_file = 1, | |
8a72758c | 874 | .pollin = 1, |
27926b68 | 875 | .plug = 1, |
e8c2bc1f | 876 | .async_size = sizeof(struct io_async_rw), |
4017eb91 | 877 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
d3656344 | 878 | }, |
0463b6c5 | 879 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
880 | .needs_file = 1, |
881 | .hash_reg_file = 1, | |
882 | .unbound_nonreg_file = 1, | |
8a72758c | 883 | .pollout = 1, |
27926b68 | 884 | .plug = 1, |
e8c2bc1f | 885 | .async_size = sizeof(struct io_async_rw), |
4017eb91 JA |
886 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
887 | IO_WQ_WORK_MM, | |
d3656344 | 888 | }, |
0463b6c5 | 889 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
890 | .needs_file = 1, |
891 | .unbound_nonreg_file = 1, | |
892 | }, | |
0463b6c5 PB |
893 | [IORING_OP_POLL_REMOVE] = {}, |
894 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 895 | .needs_file = 1, |
0f203765 | 896 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 897 | }, |
0463b6c5 | 898 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
899 | .needs_file = 1, |
900 | .unbound_nonreg_file = 1, | |
8a72758c | 901 | .pollout = 1, |
e8c2bc1f JA |
902 | .needs_async_data = 1, |
903 | .async_size = sizeof(struct io_async_msghdr), | |
92c75f75 JA |
904 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
905 | IO_WQ_WORK_FS, | |
d3656344 | 906 | }, |
0463b6c5 | 907 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
908 | .needs_file = 1, |
909 | .unbound_nonreg_file = 1, | |
8a72758c | 910 | .pollin = 1, |
52de1fe1 | 911 | .buffer_select = 1, |
e8c2bc1f JA |
912 | .needs_async_data = 1, |
913 | .async_size = sizeof(struct io_async_msghdr), | |
92c75f75 JA |
914 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
915 | IO_WQ_WORK_FS, | |
d3656344 | 916 | }, |
0463b6c5 | 917 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
918 | .needs_async_data = 1, |
919 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 920 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 921 | }, |
9c8e11b3 PB |
922 | [IORING_OP_TIMEOUT_REMOVE] = { |
923 | /* used by timeout updates' prep() */ | |
924 | .work_flags = IO_WQ_WORK_MM, | |
925 | }, | |
0463b6c5 | 926 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
927 | .needs_file = 1, |
928 | .unbound_nonreg_file = 1, | |
8a72758c | 929 | .pollin = 1, |
0f203765 | 930 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
d3656344 | 931 | }, |
0463b6c5 PB |
932 | [IORING_OP_ASYNC_CANCEL] = {}, |
933 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
934 | .needs_async_data = 1, |
935 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 936 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 937 | }, |
0463b6c5 | 938 | [IORING_OP_CONNECT] = { |
d3656344 JA |
939 | .needs_file = 1, |
940 | .unbound_nonreg_file = 1, | |
8a72758c | 941 | .pollout = 1, |
e8c2bc1f JA |
942 | .needs_async_data = 1, |
943 | .async_size = sizeof(struct io_async_connect), | |
0f203765 | 944 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 945 | }, |
0463b6c5 | 946 | [IORING_OP_FALLOCATE] = { |
d3656344 | 947 | .needs_file = 1, |
69228338 | 948 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
d3656344 | 949 | }, |
0463b6c5 | 950 | [IORING_OP_OPENAT] = { |
0f203765 | 951 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
14587a46 | 952 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
d3656344 | 953 | }, |
0463b6c5 | 954 | [IORING_OP_CLOSE] = { |
0f203765 | 955 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 956 | }, |
0463b6c5 | 957 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 958 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 959 | }, |
0463b6c5 | 960 | [IORING_OP_STATX] = { |
0f203765 JA |
961 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
962 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 963 | }, |
0463b6c5 | 964 | [IORING_OP_READ] = { |
3a6820f2 JA |
965 | .needs_file = 1, |
966 | .unbound_nonreg_file = 1, | |
8a72758c | 967 | .pollin = 1, |
bcda7baa | 968 | .buffer_select = 1, |
27926b68 | 969 | .plug = 1, |
e8c2bc1f | 970 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 971 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 972 | }, |
0463b6c5 | 973 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
974 | .needs_file = 1, |
975 | .unbound_nonreg_file = 1, | |
8a72758c | 976 | .pollout = 1, |
27926b68 | 977 | .plug = 1, |
e8c2bc1f | 978 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
979 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
980 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 981 | }, |
0463b6c5 | 982 | [IORING_OP_FADVISE] = { |
4840e418 | 983 | .needs_file = 1, |
0f203765 | 984 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 985 | }, |
0463b6c5 | 986 | [IORING_OP_MADVISE] = { |
0f203765 | 987 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 988 | }, |
0463b6c5 | 989 | [IORING_OP_SEND] = { |
fddaface JA |
990 | .needs_file = 1, |
991 | .unbound_nonreg_file = 1, | |
8a72758c | 992 | .pollout = 1, |
0f203765 | 993 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 994 | }, |
0463b6c5 | 995 | [IORING_OP_RECV] = { |
fddaface JA |
996 | .needs_file = 1, |
997 | .unbound_nonreg_file = 1, | |
8a72758c | 998 | .pollin = 1, |
bcda7baa | 999 | .buffer_select = 1, |
0f203765 | 1000 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 1001 | }, |
0463b6c5 | 1002 | [IORING_OP_OPENAT2] = { |
0f203765 | 1003 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
14587a46 | 1004 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
cebdb986 | 1005 | }, |
3e4827b0 JA |
1006 | [IORING_OP_EPOLL_CTL] = { |
1007 | .unbound_nonreg_file = 1, | |
0f203765 | 1008 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 1009 | }, |
7d67af2c PB |
1010 | [IORING_OP_SPLICE] = { |
1011 | .needs_file = 1, | |
1012 | .hash_reg_file = 1, | |
1013 | .unbound_nonreg_file = 1, | |
0f203765 | 1014 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
1015 | }, |
1016 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1017 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1018 | [IORING_OP_TEE] = { |
1019 | .needs_file = 1, | |
1020 | .hash_reg_file = 1, | |
1021 | .unbound_nonreg_file = 1, | |
1022 | }, | |
36f4fa68 JA |
1023 | [IORING_OP_SHUTDOWN] = { |
1024 | .needs_file = 1, | |
1025 | }, | |
80a261fd JA |
1026 | [IORING_OP_RENAMEAT] = { |
1027 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
1028 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
1029 | }, | |
14a1143b JA |
1030 | [IORING_OP_UNLINKAT] = { |
1031 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
1032 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
1033 | }, | |
d3656344 JA |
1034 | }; |
1035 | ||
9936c7c2 PB |
1036 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1037 | struct task_struct *task, | |
1038 | struct files_struct *files); | |
269bbe5f | 1039 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 1040 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 1041 | struct io_ring_ctx *ctx); |
f2303b1f | 1042 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
1ffc5422 | 1043 | |
23faba36 | 1044 | static bool io_rw_reissue(struct io_kiocb *req); |
78e19bbe | 1045 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1046 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1047 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1048 | static void io_double_put_req(struct io_kiocb *req); |
c7dae4ba JA |
1049 | static void io_dismantle_req(struct io_kiocb *req); |
1050 | static void io_put_task(struct task_struct *task, int nr); | |
1051 | static void io_queue_next(struct io_kiocb *req); | |
94ae5e77 | 1052 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1053 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1054 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1055 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1056 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1057 | unsigned nr_args); |
3ca405eb | 1058 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1059 | static struct file *io_file_get(struct io_submit_state *state, |
1060 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1061 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1062 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1063 | |
847595de PB |
1064 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1065 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1066 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1067 | const struct iovec *fast_iov, | |
227c0c96 | 1068 | struct iov_iter *iter, bool force); |
907d1df3 | 1069 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1070 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1071 | struct io_ring_ctx *ctx); | |
de0617e4 | 1072 | |
2b188cc1 JA |
1073 | static struct kmem_cache *req_cachep; |
1074 | ||
0918682b | 1075 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1076 | |
1077 | struct sock *io_uring_get_socket(struct file *file) | |
1078 | { | |
1079 | #if defined(CONFIG_UNIX) | |
1080 | if (file->f_op == &io_uring_fops) { | |
1081 | struct io_ring_ctx *ctx = file->private_data; | |
1082 | ||
1083 | return ctx->ring_sock->sk; | |
1084 | } | |
1085 | #endif | |
1086 | return NULL; | |
1087 | } | |
1088 | EXPORT_SYMBOL(io_uring_get_socket); | |
1089 | ||
f2f87370 PB |
1090 | #define io_for_each_link(pos, head) \ |
1091 | for (pos = (head); pos; pos = pos->link) | |
1092 | ||
3ca405eb PB |
1093 | static inline void io_clean_op(struct io_kiocb *req) |
1094 | { | |
9d5c8190 | 1095 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1096 | __io_clean_op(req); |
1097 | } | |
1098 | ||
36f72fe2 PB |
1099 | static inline void io_set_resource_node(struct io_kiocb *req) |
1100 | { | |
1101 | struct io_ring_ctx *ctx = req->ctx; | |
1102 | ||
269bbe5f BM |
1103 | if (!req->fixed_rsrc_refs) { |
1104 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1105 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1106 | } |
1107 | } | |
1108 | ||
88f171ab PB |
1109 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1110 | { | |
1111 | if (!percpu_ref_tryget(ref)) { | |
1112 | /* already at zero, wait for ->release() */ | |
1113 | if (!try_wait_for_completion(compl)) | |
1114 | synchronize_rcu(); | |
1115 | return false; | |
1116 | } | |
1117 | ||
1118 | percpu_ref_resurrect(ref); | |
1119 | reinit_completion(compl); | |
1120 | percpu_ref_put(ref); | |
1121 | return true; | |
1122 | } | |
1123 | ||
08d23634 PB |
1124 | static bool io_match_task(struct io_kiocb *head, |
1125 | struct task_struct *task, | |
1126 | struct files_struct *files) | |
1127 | { | |
1128 | struct io_kiocb *req; | |
1129 | ||
84965ff8 JA |
1130 | if (task && head->task != task) { |
1131 | /* in terms of cancelation, always match if req task is dead */ | |
1132 | if (head->task->flags & PF_EXITING) | |
1133 | return true; | |
08d23634 | 1134 | return false; |
84965ff8 | 1135 | } |
08d23634 PB |
1136 | if (!files) |
1137 | return true; | |
1138 | ||
1139 | io_for_each_link(req, head) { | |
02a13674 JA |
1140 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1141 | continue; | |
1142 | if (req->file && req->file->f_op == &io_uring_fops) | |
1143 | return true; | |
1144 | if ((req->work.flags & IO_WQ_WORK_FILES) && | |
08d23634 PB |
1145 | req->work.identity->files == files) |
1146 | return true; | |
1147 | } | |
1148 | return false; | |
1149 | } | |
1150 | ||
28cea78a | 1151 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1152 | { |
28cea78a | 1153 | struct files_struct *files = current->files; |
c40f6379 JA |
1154 | struct mm_struct *mm = current->mm; |
1155 | ||
1156 | if (mm) { | |
1157 | kthread_unuse_mm(mm); | |
1158 | mmput(mm); | |
4b70cf9d | 1159 | current->mm = NULL; |
c40f6379 | 1160 | } |
28cea78a JA |
1161 | if (files) { |
1162 | struct nsproxy *nsproxy = current->nsproxy; | |
1163 | ||
1164 | task_lock(current); | |
1165 | current->files = NULL; | |
1166 | current->nsproxy = NULL; | |
1167 | task_unlock(current); | |
1168 | put_files_struct(files); | |
1169 | put_nsproxy(nsproxy); | |
1170 | } | |
1171 | } | |
1172 | ||
1a38ffc9 | 1173 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a JA |
1174 | { |
1175 | if (!current->files) { | |
1176 | struct files_struct *files; | |
1177 | struct nsproxy *nsproxy; | |
1178 | ||
1179 | task_lock(ctx->sqo_task); | |
1180 | files = ctx->sqo_task->files; | |
1181 | if (!files) { | |
1182 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1183 | return -EOWNERDEAD; |
28cea78a JA |
1184 | } |
1185 | atomic_inc(&files->count); | |
1186 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1187 | nsproxy = ctx->sqo_task->nsproxy; | |
1188 | task_unlock(ctx->sqo_task); | |
1189 | ||
1190 | task_lock(current); | |
1191 | current->files = files; | |
1192 | current->nsproxy = nsproxy; | |
1193 | task_unlock(current); | |
1194 | } | |
1a38ffc9 | 1195 | return 0; |
c40f6379 JA |
1196 | } |
1197 | ||
1198 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1199 | { | |
4b70cf9d JA |
1200 | struct mm_struct *mm; |
1201 | ||
1202 | if (current->mm) | |
1203 | return 0; | |
1204 | ||
4b70cf9d JA |
1205 | task_lock(ctx->sqo_task); |
1206 | mm = ctx->sqo_task->mm; | |
1207 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1208 | mm = NULL; | |
1209 | task_unlock(ctx->sqo_task); | |
1210 | ||
1211 | if (mm) { | |
1212 | kthread_use_mm(mm); | |
1213 | return 0; | |
c40f6379 JA |
1214 | } |
1215 | ||
4b70cf9d | 1216 | return -EFAULT; |
c40f6379 JA |
1217 | } |
1218 | ||
4e326358 PB |
1219 | static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1220 | struct io_kiocb *req) | |
c40f6379 | 1221 | { |
28cea78a | 1222 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1223 | int ret; |
28cea78a JA |
1224 | |
1225 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1226 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1227 | if (unlikely(ret)) |
1228 | return ret; | |
1229 | } | |
1230 | ||
1a38ffc9 PB |
1231 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1232 | ret = __io_sq_thread_acquire_files(ctx); | |
1233 | if (unlikely(ret)) | |
1234 | return ret; | |
1235 | } | |
28cea78a JA |
1236 | |
1237 | return 0; | |
c40f6379 JA |
1238 | } |
1239 | ||
4e326358 PB |
1240 | static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1241 | struct io_kiocb *req) | |
1242 | { | |
4e326358 PB |
1243 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
1244 | return 0; | |
1245 | return __io_sq_thread_acquire_mm_files(ctx, req); | |
1246 | } | |
1247 | ||
91d8f519 DZ |
1248 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1249 | struct cgroup_subsys_state **cur_css) | |
1250 | ||
1251 | { | |
1252 | #ifdef CONFIG_BLK_CGROUP | |
1253 | /* puts the old one when swapping */ | |
1254 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1255 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1256 | *cur_css = ctx->sqo_blkcg_css; | |
1257 | } | |
1258 | #endif | |
1259 | } | |
1260 | ||
1261 | static void io_sq_thread_unassociate_blkcg(void) | |
1262 | { | |
1263 | #ifdef CONFIG_BLK_CGROUP | |
1264 | kthread_associate_blkcg(NULL); | |
1265 | #endif | |
1266 | } | |
1267 | ||
c40f6379 JA |
1268 | static inline void req_set_fail_links(struct io_kiocb *req) |
1269 | { | |
1270 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1271 | req->flags |= REQ_F_FAIL_LINK; | |
1272 | } | |
4a38aed2 | 1273 | |
1e6fa521 JA |
1274 | /* |
1275 | * None of these are dereferenced, they are simply used to check if any of | |
1276 | * them have changed. If we're under current and check they are still the | |
1277 | * same, we're fine to grab references to them for actual out-of-line use. | |
1278 | */ | |
1279 | static void io_init_identity(struct io_identity *id) | |
1280 | { | |
1281 | id->files = current->files; | |
1282 | id->mm = current->mm; | |
1283 | #ifdef CONFIG_BLK_CGROUP | |
1284 | rcu_read_lock(); | |
1285 | id->blkcg_css = blkcg_css(); | |
1286 | rcu_read_unlock(); | |
1287 | #endif | |
1288 | id->creds = current_cred(); | |
1289 | id->nsproxy = current->nsproxy; | |
1290 | id->fs = current->fs; | |
1291 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1292 | #ifdef CONFIG_AUDIT |
1293 | id->loginuid = current->loginuid; | |
1294 | id->sessionid = current->sessionid; | |
1295 | #endif | |
1e6fa521 JA |
1296 | refcount_set(&id->count, 1); |
1297 | } | |
1298 | ||
ec99ca6c PB |
1299 | static inline void __io_req_init_async(struct io_kiocb *req) |
1300 | { | |
1301 | memset(&req->work, 0, sizeof(req->work)); | |
1302 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1303 | } | |
1304 | ||
7cdaf587 XW |
1305 | /* |
1306 | * Note: must call io_req_init_async() for the first time you | |
1307 | * touch any members of io_wq_work. | |
1308 | */ | |
1309 | static inline void io_req_init_async(struct io_kiocb *req) | |
1310 | { | |
500a373d JA |
1311 | struct io_uring_task *tctx = current->io_uring; |
1312 | ||
7cdaf587 XW |
1313 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1314 | return; | |
1315 | ||
ec99ca6c | 1316 | __io_req_init_async(req); |
500a373d JA |
1317 | |
1318 | /* Grab a ref if this isn't our static identity */ | |
1319 | req->work.identity = tctx->identity; | |
1320 | if (tctx->identity != &tctx->__identity) | |
1321 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1322 | } |
1323 | ||
2b188cc1 JA |
1324 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1325 | { | |
1326 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1327 | ||
0f158b4c | 1328 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1329 | } |
1330 | ||
8eb7e2d0 PB |
1331 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1332 | { | |
1333 | return !req->timeout.off; | |
1334 | } | |
1335 | ||
2b188cc1 JA |
1336 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1337 | { | |
1338 | struct io_ring_ctx *ctx; | |
78076bb6 | 1339 | int hash_bits; |
2b188cc1 JA |
1340 | |
1341 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1342 | if (!ctx) | |
1343 | return NULL; | |
1344 | ||
78076bb6 JA |
1345 | /* |
1346 | * Use 5 bits less than the max cq entries, that should give us around | |
1347 | * 32 entries per hash list if totally full and uniformly spread. | |
1348 | */ | |
1349 | hash_bits = ilog2(p->cq_entries); | |
1350 | hash_bits -= 5; | |
1351 | if (hash_bits <= 0) | |
1352 | hash_bits = 1; | |
1353 | ctx->cancel_hash_bits = hash_bits; | |
1354 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1355 | GFP_KERNEL); | |
1356 | if (!ctx->cancel_hash) | |
1357 | goto err; | |
1358 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1359 | ||
21482896 | 1360 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1361 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1362 | goto err; | |
2b188cc1 JA |
1363 | |
1364 | ctx->flags = p->flags; | |
90554200 | 1365 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1366 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1367 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1368 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1369 | init_completion(&ctx->ref_comp); |
1370 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1371 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1372 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1373 | mutex_init(&ctx->uring_lock); |
1374 | init_waitqueue_head(&ctx->wait); | |
1375 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1376 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1377 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1378 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1379 | spin_lock_init(&ctx->inflight_lock); |
1380 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1381 | spin_lock_init(&ctx->rsrc_ref_lock); |
1382 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1383 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1384 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1385 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1386 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1387 | return ctx; |
206aefde | 1388 | err: |
78076bb6 | 1389 | kfree(ctx->cancel_hash); |
206aefde JA |
1390 | kfree(ctx); |
1391 | return NULL; | |
2b188cc1 JA |
1392 | } |
1393 | ||
9cf7c104 | 1394 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1395 | { |
2bc9930e JA |
1396 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1397 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1398 | |
9cf7c104 | 1399 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1400 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1401 | } |
de0617e4 | 1402 | |
9d858b21 | 1403 | return false; |
de0617e4 JA |
1404 | } |
1405 | ||
5c3462cf | 1406 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1407 | { |
500a373d | 1408 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1409 | return; |
1410 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1411 | kfree(req->work.identity); | |
1412 | } | |
1413 | ||
4edf20f9 | 1414 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1415 | { |
7cdaf587 | 1416 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1417 | return; |
51a4cc11 | 1418 | |
e86d0047 | 1419 | if (req->work.flags & IO_WQ_WORK_MM) |
98447d65 | 1420 | mmdrop(req->work.identity->mm); |
91d8f519 | 1421 | #ifdef CONFIG_BLK_CGROUP |
e86d0047 | 1422 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
98447d65 | 1423 | css_put(req->work.identity->blkcg_css); |
91d8f519 | 1424 | #endif |
e86d0047 | 1425 | if (req->work.flags & IO_WQ_WORK_CREDS) |
98447d65 | 1426 | put_cred(req->work.identity->creds); |
dfead8a8 | 1427 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1428 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1429 | |
98447d65 | 1430 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1431 | if (--fs->users) |
1432 | fs = NULL; | |
98447d65 | 1433 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1434 | if (fs) |
1435 | free_fs_struct(fs); | |
1436 | } | |
34e08fed PB |
1437 | if (req->work.flags & IO_WQ_WORK_FILES) { |
1438 | put_files_struct(req->work.identity->files); | |
1439 | put_nsproxy(req->work.identity->nsproxy); | |
34e08fed PB |
1440 | } |
1441 | if (req->flags & REQ_F_INFLIGHT) { | |
1442 | struct io_ring_ctx *ctx = req->ctx; | |
1443 | struct io_uring_task *tctx = req->task->io_uring; | |
1444 | unsigned long flags; | |
1445 | ||
1446 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1447 | list_del(&req->inflight_entry); | |
1448 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1449 | req->flags &= ~REQ_F_INFLIGHT; | |
1450 | if (atomic_read(&tctx->in_idle)) | |
1451 | wake_up(&tctx->wait); | |
1452 | } | |
51a4cc11 | 1453 | |
e86d0047 PB |
1454 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
1455 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | | |
1456 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); | |
5c3462cf | 1457 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1458 | } |
1459 | ||
1e6fa521 JA |
1460 | /* |
1461 | * Create a private copy of io_identity, since some fields don't match | |
1462 | * the current context. | |
1463 | */ | |
1464 | static bool io_identity_cow(struct io_kiocb *req) | |
1465 | { | |
5c3462cf | 1466 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1467 | const struct cred *creds = NULL; |
1468 | struct io_identity *id; | |
1469 | ||
1470 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1471 | creds = req->work.identity->creds; | |
1472 | ||
1473 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1474 | if (unlikely(!id)) { | |
1475 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1476 | return false; | |
1477 | } | |
1478 | ||
1479 | /* | |
1480 | * We can safely just re-init the creds we copied Either the field | |
1481 | * matches the current one, or we haven't grabbed it yet. The only | |
1482 | * exception is ->creds, through registered personalities, so handle | |
1483 | * that one separately. | |
1484 | */ | |
1485 | io_init_identity(id); | |
1486 | if (creds) | |
e8c954df | 1487 | id->creds = creds; |
1e6fa521 JA |
1488 | |
1489 | /* add one for this request */ | |
1490 | refcount_inc(&id->count); | |
1491 | ||
cb8a8ae3 JA |
1492 | /* drop tctx and req identity references, if needed */ |
1493 | if (tctx->identity != &tctx->__identity && | |
1494 | refcount_dec_and_test(&tctx->identity->count)) | |
1495 | kfree(tctx->identity); | |
1496 | if (req->work.identity != &tctx->__identity && | |
1497 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1498 | kfree(req->work.identity); |
1499 | ||
1500 | req->work.identity = id; | |
500a373d | 1501 | tctx->identity = id; |
1e6fa521 JA |
1502 | return true; |
1503 | } | |
1504 | ||
ce3d5aae PB |
1505 | static void io_req_track_inflight(struct io_kiocb *req) |
1506 | { | |
1507 | struct io_ring_ctx *ctx = req->ctx; | |
1508 | ||
1509 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1510 | io_req_init_async(req); | |
1511 | req->flags |= REQ_F_INFLIGHT; | |
1512 | ||
1513 | spin_lock_irq(&ctx->inflight_lock); | |
1514 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1515 | spin_unlock_irq(&ctx->inflight_lock); | |
1516 | } | |
1517 | } | |
1518 | ||
1e6fa521 | 1519 | static bool io_grab_identity(struct io_kiocb *req) |
18d9be1a | 1520 | { |
d3656344 | 1521 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1522 | struct io_identity *id = req->work.identity; |
54a91f3b | 1523 | |
69228338 JA |
1524 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1525 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1526 | return false; | |
1527 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1528 | } | |
91d8f519 | 1529 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1530 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1531 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1532 | rcu_read_lock(); |
1e6fa521 JA |
1533 | if (id->blkcg_css != blkcg_css()) { |
1534 | rcu_read_unlock(); | |
1535 | return false; | |
1536 | } | |
91d8f519 DZ |
1537 | /* |
1538 | * This should be rare, either the cgroup is dying or the task | |
1539 | * is moving cgroups. Just punt to root for the handful of ios. | |
1540 | */ | |
1e6fa521 | 1541 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1542 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1543 | rcu_read_unlock(); |
1544 | } | |
1545 | #endif | |
dfead8a8 | 1546 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1547 | if (id->creds != current_cred()) |
1548 | return false; | |
1549 | get_cred(id->creds); | |
dfead8a8 JA |
1550 | req->work.flags |= IO_WQ_WORK_CREDS; |
1551 | } | |
4ea33a97 JA |
1552 | #ifdef CONFIG_AUDIT |
1553 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1554 | current->sessionid != id->sessionid) | |
1555 | return false; | |
1556 | #endif | |
dfead8a8 JA |
1557 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1558 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1559 | if (current->fs != id->fs) |
1560 | return false; | |
1561 | spin_lock(&id->fs->lock); | |
1562 | if (!id->fs->in_exec) { | |
1563 | id->fs->users++; | |
dfead8a8 | 1564 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1565 | } else { |
1566 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1567 | } | |
1568 | spin_unlock(¤t->fs->lock); | |
1569 | } | |
af604703 PB |
1570 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1571 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1572 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1573 | if (id->files != current->files || | |
1574 | id->nsproxy != current->nsproxy) | |
1575 | return false; | |
1576 | atomic_inc(&id->files->count); | |
1577 | get_nsproxy(id->nsproxy); | |
af604703 | 1578 | req->work.flags |= IO_WQ_WORK_FILES; |
ce3d5aae | 1579 | io_req_track_inflight(req); |
af604703 | 1580 | } |
77788775 JA |
1581 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1582 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1583 | if (id->mm != current->mm) | |
1584 | return false; | |
1585 | mmgrab(id->mm); | |
1586 | req->work.flags |= IO_WQ_WORK_MM; | |
1587 | } | |
1e6fa521 JA |
1588 | |
1589 | return true; | |
1590 | } | |
1591 | ||
1592 | static void io_prep_async_work(struct io_kiocb *req) | |
1593 | { | |
1594 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1595 | struct io_ring_ctx *ctx = req->ctx; |
1596 | ||
1597 | io_req_init_async(req); | |
1598 | ||
feaadc4f PB |
1599 | if (req->flags & REQ_F_FORCE_ASYNC) |
1600 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1601 | ||
1e6fa521 JA |
1602 | if (req->flags & REQ_F_ISREG) { |
1603 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1604 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1605 | } else { | |
1606 | if (def->unbound_nonreg_file) | |
1607 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1608 | } | |
1609 | ||
1e6fa521 JA |
1610 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1611 | if (io_grab_identity(req)) | |
1612 | return; | |
1613 | ||
1614 | if (!io_identity_cow(req)) | |
1615 | return; | |
1616 | ||
1617 | /* can't fail at this point */ | |
1618 | if (!io_grab_identity(req)) | |
1619 | WARN_ON(1); | |
561fb04a | 1620 | } |
cccf0ee8 | 1621 | |
cbdcb435 | 1622 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1623 | { |
cbdcb435 | 1624 | struct io_kiocb *cur; |
54a91f3b | 1625 | |
f2f87370 PB |
1626 | io_for_each_link(cur, req) |
1627 | io_prep_async_work(cur); | |
561fb04a JA |
1628 | } |
1629 | ||
7271ef3a | 1630 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1631 | { |
a197f664 | 1632 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1633 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1634 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1635 | |
8766dd51 PB |
1636 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1637 | &req->work, req->flags); | |
5aa75ed5 | 1638 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a | 1639 | return link; |
18d9be1a JA |
1640 | } |
1641 | ||
cbdcb435 PB |
1642 | static void io_queue_async_work(struct io_kiocb *req) |
1643 | { | |
7271ef3a JA |
1644 | struct io_kiocb *link; |
1645 | ||
cbdcb435 PB |
1646 | /* init ->work of the whole link before punting */ |
1647 | io_prep_async_link(req); | |
7271ef3a JA |
1648 | link = __io_queue_async_work(req); |
1649 | ||
1650 | if (link) | |
1651 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1652 | } |
1653 | ||
5262f567 JA |
1654 | static void io_kill_timeout(struct io_kiocb *req) |
1655 | { | |
e8c2bc1f | 1656 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1657 | int ret; |
1658 | ||
e8c2bc1f | 1659 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1660 | if (ret != -1) { |
01cec8c1 PB |
1661 | atomic_set(&req->ctx->cq_timeouts, |
1662 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1663 | list_del_init(&req->timeout.list); |
78e19bbe | 1664 | io_cqring_fill_event(req, 0); |
216578e5 | 1665 | io_put_req_deferred(req, 1); |
5262f567 JA |
1666 | } |
1667 | } | |
1668 | ||
76e1b642 JA |
1669 | /* |
1670 | * Returns true if we found and killed one or more timeouts | |
1671 | */ | |
6b81928d PB |
1672 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1673 | struct files_struct *files) | |
5262f567 JA |
1674 | { |
1675 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1676 | int canceled = 0; |
5262f567 JA |
1677 | |
1678 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1679 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1680 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1681 | io_kill_timeout(req); |
76e1b642 JA |
1682 | canceled++; |
1683 | } | |
f3606e3a | 1684 | } |
5262f567 | 1685 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1686 | return canceled != 0; |
5262f567 JA |
1687 | } |
1688 | ||
04518945 | 1689 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1690 | { |
04518945 | 1691 | do { |
27dc8338 PB |
1692 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1693 | struct io_defer_entry, list); | |
de0617e4 | 1694 | |
9cf7c104 | 1695 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1696 | break; |
27dc8338 | 1697 | list_del_init(&de->list); |
907d1df3 | 1698 | io_req_task_queue(de->req); |
27dc8338 | 1699 | kfree(de); |
04518945 PB |
1700 | } while (!list_empty(&ctx->defer_list)); |
1701 | } | |
1702 | ||
360428f8 | 1703 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1704 | { |
f010505b MDG |
1705 | u32 seq; |
1706 | ||
1707 | if (list_empty(&ctx->timeout_list)) | |
1708 | return; | |
1709 | ||
1710 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1711 | ||
1712 | do { | |
1713 | u32 events_needed, events_got; | |
360428f8 | 1714 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1715 | struct io_kiocb, timeout.list); |
de0617e4 | 1716 | |
8eb7e2d0 | 1717 | if (io_is_timeout_noseq(req)) |
360428f8 | 1718 | break; |
f010505b MDG |
1719 | |
1720 | /* | |
1721 | * Since seq can easily wrap around over time, subtract | |
1722 | * the last seq at which timeouts were flushed before comparing. | |
1723 | * Assuming not more than 2^31-1 events have happened since, | |
1724 | * these subtractions won't have wrapped, so we can check if | |
1725 | * target is in [last_seq, current_seq] by comparing the two. | |
1726 | */ | |
1727 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1728 | events_got = seq - ctx->cq_last_tm_flush; | |
1729 | if (events_got < events_needed) | |
360428f8 | 1730 | break; |
bfe68a22 | 1731 | |
135fcde8 | 1732 | list_del_init(&req->timeout.list); |
5262f567 | 1733 | io_kill_timeout(req); |
f010505b MDG |
1734 | } while (!list_empty(&ctx->timeout_list)); |
1735 | ||
1736 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1737 | } |
5262f567 | 1738 | |
360428f8 PB |
1739 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1740 | { | |
1741 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1742 | |
1743 | /* order cqe stores with ring update */ | |
1744 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1745 | |
04518945 PB |
1746 | if (unlikely(!list_empty(&ctx->defer_list))) |
1747 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1748 | } |
1749 | ||
90554200 JA |
1750 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1751 | { | |
1752 | struct io_rings *r = ctx->rings; | |
1753 | ||
1754 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1755 | } | |
1756 | ||
888aae2e PB |
1757 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1758 | { | |
1759 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1760 | } | |
1761 | ||
2b188cc1 JA |
1762 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1763 | { | |
75b28aff | 1764 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1765 | unsigned tail; |
1766 | ||
115e12e5 SB |
1767 | /* |
1768 | * writes to the cq entry need to come after reading head; the | |
1769 | * control dependency is enough as we're using WRITE_ONCE to | |
1770 | * fill the cq entry | |
1771 | */ | |
888aae2e | 1772 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1773 | return NULL; |
1774 | ||
888aae2e | 1775 | tail = ctx->cached_cq_tail++; |
75b28aff | 1776 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1777 | } |
1778 | ||
f2842ab5 JA |
1779 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1780 | { | |
f0b493e6 JA |
1781 | if (!ctx->cq_ev_fd) |
1782 | return false; | |
7e55a19c SG |
1783 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1784 | return false; | |
f2842ab5 JA |
1785 | if (!ctx->eventfd_async) |
1786 | return true; | |
b41e9852 | 1787 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1788 | } |
1789 | ||
b41e9852 | 1790 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1791 | { |
b1445e59 PB |
1792 | /* see waitqueue_active() comment */ |
1793 | smp_mb(); | |
1794 | ||
1d7bb1d5 JA |
1795 | if (waitqueue_active(&ctx->wait)) |
1796 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1797 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1798 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1799 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1800 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1801 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1802 | wake_up_interruptible(&ctx->cq_wait); |
1803 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1804 | } | |
1d7bb1d5 JA |
1805 | } |
1806 | ||
80c18e4a PB |
1807 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1808 | { | |
b1445e59 PB |
1809 | /* see waitqueue_active() comment */ |
1810 | smp_mb(); | |
1811 | ||
80c18e4a PB |
1812 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1813 | if (waitqueue_active(&ctx->wait)) | |
1814 | wake_up(&ctx->wait); | |
1815 | } | |
1816 | if (io_should_trigger_evfd(ctx)) | |
1817 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1818 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1819 | wake_up_interruptible(&ctx->cq_wait); |
1820 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1821 | } | |
80c18e4a PB |
1822 | } |
1823 | ||
c4a2ed72 | 1824 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1825 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1826 | struct task_struct *tsk, | |
1827 | struct files_struct *files) | |
1d7bb1d5 JA |
1828 | { |
1829 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1830 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1831 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1832 | unsigned long flags; |
b18032bb | 1833 | bool all_flushed, posted; |
1d7bb1d5 JA |
1834 | LIST_HEAD(list); |
1835 | ||
e23de15f PB |
1836 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1837 | return false; | |
1d7bb1d5 | 1838 | |
b18032bb | 1839 | posted = false; |
1d7bb1d5 | 1840 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1841 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1842 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1843 | continue; |
1844 | ||
1d7bb1d5 JA |
1845 | cqe = io_get_cqring(ctx); |
1846 | if (!cqe && !force) | |
1847 | break; | |
1848 | ||
40d8ddd4 | 1849 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1850 | if (cqe) { |
1851 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1852 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1853 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1854 | } else { |
2c3bac6d | 1855 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1856 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1857 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1858 | } |
b18032bb | 1859 | posted = true; |
1d7bb1d5 JA |
1860 | } |
1861 | ||
09e88404 PB |
1862 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1863 | if (all_flushed) { | |
1864 | clear_bit(0, &ctx->sq_check_overflow); | |
1865 | clear_bit(0, &ctx->cq_check_overflow); | |
1866 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1867 | } | |
46930143 | 1868 | |
b18032bb JA |
1869 | if (posted) |
1870 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1871 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1872 | if (posted) |
1873 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1874 | |
1875 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1876 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1877 | list_del(&req->compl.list); | |
ec9c02ad | 1878 | io_put_req(req); |
1d7bb1d5 | 1879 | } |
c4a2ed72 | 1880 | |
09e88404 | 1881 | return all_flushed; |
1d7bb1d5 JA |
1882 | } |
1883 | ||
6c503150 PB |
1884 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1885 | struct task_struct *tsk, | |
1886 | struct files_struct *files) | |
1887 | { | |
1888 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1889 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1890 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1891 | mutex_lock(&ctx->uring_lock); | |
1892 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1893 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1894 | mutex_unlock(&ctx->uring_lock); | |
1895 | } | |
1896 | } | |
1897 | ||
bcda7baa | 1898 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1899 | { |
78e19bbe | 1900 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1901 | struct io_uring_cqe *cqe; |
1902 | ||
78e19bbe | 1903 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1904 | |
2b188cc1 JA |
1905 | /* |
1906 | * If we can't get a cq entry, userspace overflowed the | |
1907 | * submission (by quite a lot). Increment the overflow count in | |
1908 | * the ring. | |
1909 | */ | |
1910 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1911 | if (likely(cqe)) { |
78e19bbe | 1912 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1913 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1914 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1915 | } else if (ctx->cq_overflow_flushed || |
1916 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1917 | /* |
1918 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1919 | * then we cannot store the request for later flushing, we need | |
1920 | * to drop it on the floor. | |
1921 | */ | |
2c3bac6d PB |
1922 | ctx->cached_cq_overflow++; |
1923 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1924 | } else { |
ad3eb2c8 JA |
1925 | if (list_empty(&ctx->cq_overflow_list)) { |
1926 | set_bit(0, &ctx->sq_check_overflow); | |
1927 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1928 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1929 | } |
40d8ddd4 | 1930 | io_clean_op(req); |
1d7bb1d5 | 1931 | req->result = res; |
0f7e466b | 1932 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1933 | refcount_inc(&req->refs); |
1934 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1935 | } |
1936 | } | |
1937 | ||
bcda7baa JA |
1938 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1939 | { | |
1940 | __io_cqring_fill_event(req, res, 0); | |
1941 | } | |
1942 | ||
c7dae4ba JA |
1943 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1944 | unsigned int cflags) | |
2b188cc1 | 1945 | { |
78e19bbe | 1946 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1947 | unsigned long flags; |
1948 | ||
1949 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1950 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1951 | io_commit_cqring(ctx); |
c7dae4ba JA |
1952 | /* |
1953 | * If we're the last reference to this request, add to our locked | |
1954 | * free_list cache. | |
1955 | */ | |
1956 | if (refcount_dec_and_test(&req->refs)) { | |
1957 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1958 | ||
1959 | io_dismantle_req(req); | |
1960 | io_put_task(req->task, 1); | |
1961 | list_add(&req->compl.list, &cs->locked_free_list); | |
1962 | cs->locked_free_nr++; | |
1963 | } else | |
1964 | req = NULL; | |
2b188cc1 JA |
1965 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1966 | ||
8c838788 | 1967 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1968 | if (req) { |
1969 | io_queue_next(req); | |
1970 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1971 | } |
229a7b63 JA |
1972 | } |
1973 | ||
a38d68db | 1974 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1975 | unsigned int cflags) |
229a7b63 | 1976 | { |
a38d68db PB |
1977 | io_clean_op(req); |
1978 | req->result = res; | |
1979 | req->compl.cflags = cflags; | |
e342c807 | 1980 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1981 | } |
1982 | ||
889fca73 PB |
1983 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1984 | long res, unsigned cflags) | |
bcda7baa | 1985 | { |
889fca73 PB |
1986 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1987 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1988 | else |
c7dae4ba | 1989 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1990 | } |
1991 | ||
a38d68db | 1992 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1993 | { |
889fca73 | 1994 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1995 | } |
1996 | ||
c7dae4ba | 1997 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1998 | { |
c7dae4ba JA |
1999 | struct io_submit_state *state = &ctx->submit_state; |
2000 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 2001 | struct io_kiocb *req = NULL; |
0ddf92e8 | 2002 | |
c7dae4ba JA |
2003 | /* |
2004 | * If we have more than a batch's worth of requests in our IRQ side | |
2005 | * locked cache, grab the lock and move them over to our submission | |
2006 | * side cache. | |
2007 | */ | |
2008 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
2009 | spin_lock_irq(&ctx->completion_lock); | |
2010 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
2011 | cs->locked_free_nr = 0; | |
2012 | spin_unlock_irq(&ctx->completion_lock); | |
2013 | } | |
0ddf92e8 | 2014 | |
c7dae4ba JA |
2015 | while (!list_empty(&cs->free_list)) { |
2016 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
2017 | compl.list); |
2018 | list_del(&req->compl.list); | |
e5d1bc0a PB |
2019 | state->reqs[state->free_reqs++] = req; |
2020 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
2021 | break; | |
1b4c351f JA |
2022 | } |
2023 | ||
e5d1bc0a | 2024 | return req != NULL; |
0ddf92e8 JA |
2025 | } |
2026 | ||
e5d1bc0a | 2027 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 2028 | { |
e5d1bc0a PB |
2029 | struct io_submit_state *state = &ctx->submit_state; |
2030 | ||
2031 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
2032 | ||
f6b6c7d6 | 2033 | if (!state->free_reqs) { |
291b2821 | 2034 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
2035 | int ret; |
2036 | ||
c7dae4ba | 2037 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
2038 | goto got_req; |
2039 | ||
bf019da7 PB |
2040 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
2041 | state->reqs); | |
fd6fab2c JA |
2042 | |
2043 | /* | |
2044 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2045 | * retry single alloc to be on the safe side. | |
2046 | */ | |
2047 | if (unlikely(ret <= 0)) { | |
2048 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
2049 | if (!state->reqs[0]) | |
3893f39f | 2050 | return NULL; |
fd6fab2c JA |
2051 | ret = 1; |
2052 | } | |
291b2821 | 2053 | state->free_reqs = ret; |
2b188cc1 | 2054 | } |
e5d1bc0a | 2055 | got_req: |
291b2821 PB |
2056 | state->free_reqs--; |
2057 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
2058 | } |
2059 | ||
8da11c19 PB |
2060 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
2061 | bool fixed) | |
2062 | { | |
36f72fe2 | 2063 | if (!fixed) |
8da11c19 PB |
2064 | fput(file); |
2065 | } | |
2066 | ||
4edf20f9 | 2067 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2068 | { |
3ca405eb | 2069 | io_clean_op(req); |
929a3af9 | 2070 | |
e8c2bc1f JA |
2071 | if (req->async_data) |
2072 | kfree(req->async_data); | |
8da11c19 PB |
2073 | if (req->file) |
2074 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
2075 | if (req->fixed_rsrc_refs) |
2076 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 2077 | io_req_clean_work(req); |
e65ef56d JA |
2078 | } |
2079 | ||
7c660731 PB |
2080 | static inline void io_put_task(struct task_struct *task, int nr) |
2081 | { | |
2082 | struct io_uring_task *tctx = task->io_uring; | |
2083 | ||
2084 | percpu_counter_sub(&tctx->inflight, nr); | |
2085 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2086 | wake_up(&tctx->wait); | |
2087 | put_task_struct_many(task, nr); | |
2088 | } | |
2089 | ||
216578e5 | 2090 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2091 | { |
51a4cc11 | 2092 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2093 | |
216578e5 | 2094 | io_dismantle_req(req); |
7c660731 | 2095 | io_put_task(req->task, 1); |
c6ca97b3 | 2096 | |
3893f39f | 2097 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 2098 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
2099 | } |
2100 | ||
f2f87370 PB |
2101 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2102 | { | |
2103 | struct io_kiocb *nxt = req->link; | |
2104 | ||
2105 | req->link = nxt->link; | |
2106 | nxt->link = NULL; | |
2107 | } | |
2108 | ||
c9abd7ad | 2109 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 2110 | { |
a197f664 | 2111 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 2112 | struct io_kiocb *link; |
c9abd7ad PB |
2113 | bool cancelled = false; |
2114 | unsigned long flags; | |
7c86ffee | 2115 | |
c9abd7ad | 2116 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2117 | link = req->link; |
2118 | ||
900fad45 PB |
2119 | /* |
2120 | * Can happen if a linked timeout fired and link had been like | |
2121 | * req -> link t-out -> link t-out [-> ...] | |
2122 | */ | |
c9abd7ad PB |
2123 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
2124 | struct io_timeout_data *io = link->async_data; | |
2125 | int ret; | |
7c86ffee | 2126 | |
f2f87370 | 2127 | io_remove_next_linked(req); |
90cd7e42 | 2128 | link->timeout.head = NULL; |
c9abd7ad PB |
2129 | ret = hrtimer_try_to_cancel(&io->timer); |
2130 | if (ret != -1) { | |
2131 | io_cqring_fill_event(link, -ECANCELED); | |
2132 | io_commit_cqring(ctx); | |
2133 | cancelled = true; | |
2134 | } | |
2135 | } | |
7c86ffee | 2136 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2137 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2138 | |
c9abd7ad | 2139 | if (cancelled) { |
7c86ffee | 2140 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2141 | io_put_req(link); |
2142 | } | |
7c86ffee PB |
2143 | } |
2144 | ||
9e645e11 | 2145 | |
d148ca4b | 2146 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2147 | { |
f2f87370 | 2148 | struct io_kiocb *link, *nxt; |
2665abfd | 2149 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2150 | unsigned long flags; |
9e645e11 | 2151 | |
d148ca4b | 2152 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2153 | link = req->link; |
2154 | req->link = NULL; | |
9e645e11 | 2155 | |
f2f87370 PB |
2156 | while (link) { |
2157 | nxt = link->link; | |
2158 | link->link = NULL; | |
2665abfd | 2159 | |
f2f87370 | 2160 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2161 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2162 | |
2163 | /* | |
2164 | * It's ok to free under spinlock as they're not linked anymore, | |
2165 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2166 | * work.fs->lock. | |
2167 | */ | |
2168 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2169 | io_put_req_deferred(link, 2); | |
2170 | else | |
2171 | io_double_put_req(link); | |
f2f87370 | 2172 | link = nxt; |
9e645e11 | 2173 | } |
2665abfd | 2174 | io_commit_cqring(ctx); |
216578e5 | 2175 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2176 | |
2665abfd | 2177 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2178 | } |
2179 | ||
3fa5e0f3 | 2180 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2181 | { |
7c86ffee PB |
2182 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2183 | io_kill_linked_timeout(req); | |
944e58bf | 2184 | |
9e645e11 JA |
2185 | /* |
2186 | * If LINK is set, we have dependent requests in this chain. If we | |
2187 | * didn't fail this request, queue the first one up, moving any other | |
2188 | * dependencies to the next request. In case of failure, fail the rest | |
2189 | * of the chain. | |
2190 | */ | |
f2f87370 PB |
2191 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2192 | struct io_kiocb *nxt = req->link; | |
2193 | ||
2194 | req->link = NULL; | |
2195 | return nxt; | |
2196 | } | |
9b5f7bd9 PB |
2197 | io_fail_links(req); |
2198 | return NULL; | |
4d7dd462 | 2199 | } |
9e645e11 | 2200 | |
f2f87370 | 2201 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2202 | { |
cdbff982 | 2203 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2204 | return NULL; |
2205 | return __io_req_find_next(req); | |
2206 | } | |
2207 | ||
7cbf1722 | 2208 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 2209 | { |
65453d1e | 2210 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
2211 | struct io_wq_work_list list; |
2212 | struct io_wq_work_node *node; | |
c2c4c83c | 2213 | |
7cbf1722 JA |
2214 | if (wq_list_empty(&tctx->task_list)) |
2215 | return false; | |
6200b0ae | 2216 | |
0b81e80c | 2217 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
2218 | list = tctx->task_list; |
2219 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2220 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 2221 | |
7cbf1722 JA |
2222 | node = list.first; |
2223 | while (node) { | |
2224 | struct io_wq_work_node *next = node->next; | |
65453d1e | 2225 | struct io_ring_ctx *this_ctx; |
7cbf1722 | 2226 | struct io_kiocb *req; |
0ba9c9ed | 2227 | |
7cbf1722 | 2228 | req = container_of(node, struct io_kiocb, io_task_work.node); |
65453d1e | 2229 | this_ctx = req->ctx; |
7cbf1722 JA |
2230 | req->task_work.func(&req->task_work); |
2231 | node = next; | |
65453d1e JA |
2232 | |
2233 | if (!ctx) { | |
2234 | ctx = this_ctx; | |
2235 | } else if (ctx != this_ctx) { | |
2236 | mutex_lock(&ctx->uring_lock); | |
2237 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
2238 | mutex_unlock(&ctx->uring_lock); | |
2239 | ctx = this_ctx; | |
2240 | } | |
2241 | } | |
2242 | ||
2243 | if (ctx && ctx->submit_state.comp.nr) { | |
2244 | mutex_lock(&ctx->uring_lock); | |
2245 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
2246 | mutex_unlock(&ctx->uring_lock); | |
7cbf1722 JA |
2247 | } |
2248 | ||
2249 | return list.first != NULL; | |
c2c4c83c JA |
2250 | } |
2251 | ||
7cbf1722 | 2252 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2253 | { |
7cbf1722 | 2254 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 2255 | |
7cbf1722 JA |
2256 | while (__tctx_task_work(tctx)) |
2257 | cond_resched(); | |
2258 | ||
2259 | clear_bit(0, &tctx->task_state); | |
2260 | } | |
2261 | ||
2262 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
2263 | enum task_work_notify_mode notify) | |
2264 | { | |
2265 | struct io_uring_task *tctx = tsk->io_uring; | |
2266 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 2267 | unsigned long flags; |
7cbf1722 JA |
2268 | int ret; |
2269 | ||
2270 | WARN_ON_ONCE(!tctx); | |
2271 | ||
0b81e80c | 2272 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2273 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 2274 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2275 | |
2276 | /* task_work already pending, we're done */ | |
2277 | if (test_bit(0, &tctx->task_state) || | |
2278 | test_and_set_bit(0, &tctx->task_state)) | |
2279 | return 0; | |
2280 | ||
2281 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
2282 | return 0; | |
2283 | ||
2284 | /* | |
2285 | * Slow path - we failed, find and delete work. if the work is not | |
2286 | * in the list, it got run and we're fine. | |
2287 | */ | |
2288 | ret = 0; | |
0b81e80c | 2289 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
2290 | wq_list_for_each(node, prev, &tctx->task_list) { |
2291 | if (&req->io_task_work.node == node) { | |
2292 | wq_list_del(&tctx->task_list, node, prev); | |
2293 | ret = 1; | |
2294 | break; | |
2295 | } | |
2296 | } | |
0b81e80c | 2297 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2298 | clear_bit(0, &tctx->task_state); |
2299 | return ret; | |
2300 | } | |
2301 | ||
355fb9e2 | 2302 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
2303 | { |
2304 | struct task_struct *tsk = req->task; | |
2305 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2306 | enum task_work_notify_mode notify; |
2307 | int ret; | |
c2c4c83c | 2308 | |
6200b0ae JA |
2309 | if (tsk->flags & PF_EXITING) |
2310 | return -ESRCH; | |
2311 | ||
c2c4c83c | 2312 | /* |
0ba9c9ed JA |
2313 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2314 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2315 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2316 | * will do the job. | |
c2c4c83c | 2317 | */ |
91989c70 | 2318 | notify = TWA_NONE; |
355fb9e2 | 2319 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
2320 | notify = TWA_SIGNAL; |
2321 | ||
7cbf1722 | 2322 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
2323 | if (!ret) |
2324 | wake_up_process(tsk); | |
0ba9c9ed | 2325 | |
c2c4c83c JA |
2326 | return ret; |
2327 | } | |
2328 | ||
eab30c4d | 2329 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 2330 | task_work_func_t cb) |
eab30c4d | 2331 | { |
7c25c0d1 JA |
2332 | struct io_ring_ctx *ctx = req->ctx; |
2333 | struct callback_head *head; | |
eab30c4d PB |
2334 | |
2335 | init_task_work(&req->task_work, cb); | |
7c25c0d1 JA |
2336 | do { |
2337 | head = READ_ONCE(ctx->exit_task_work); | |
2338 | req->task_work.next = head; | |
2339 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); | |
eab30c4d PB |
2340 | } |
2341 | ||
c40f6379 JA |
2342 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2343 | { | |
2344 | struct io_ring_ctx *ctx = req->ctx; | |
2345 | ||
2346 | spin_lock_irq(&ctx->completion_lock); | |
2347 | io_cqring_fill_event(req, error); | |
2348 | io_commit_cqring(ctx); | |
2349 | spin_unlock_irq(&ctx->completion_lock); | |
2350 | ||
2351 | io_cqring_ev_posted(ctx); | |
2352 | req_set_fail_links(req); | |
2353 | io_double_put_req(req); | |
2354 | } | |
2355 | ||
2356 | static void io_req_task_cancel(struct callback_head *cb) | |
2357 | { | |
2358 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2359 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2360 | |
792bb6eb | 2361 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 2362 | __io_req_task_cancel(req, req->result); |
792bb6eb | 2363 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 2364 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2365 | } |
2366 | ||
2367 | static void __io_req_task_submit(struct io_kiocb *req) | |
2368 | { | |
2369 | struct io_ring_ctx *ctx = req->ctx; | |
2370 | ||
04fc6c80 | 2371 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2372 | mutex_lock(&ctx->uring_lock); |
dc0eced5 PB |
2373 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && |
2374 | !io_sq_thread_acquire_mm_files(ctx, req)) | |
c5eef2b9 | 2375 | __io_queue_sqe(req); |
81b6d05c | 2376 | else |
c40f6379 | 2377 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2378 | mutex_unlock(&ctx->uring_lock); |
aec18a57 PB |
2379 | |
2380 | if (ctx->flags & IORING_SETUP_SQPOLL) | |
2381 | io_sq_thread_drop_mm_files(); | |
c40f6379 JA |
2382 | } |
2383 | ||
2384 | static void io_req_task_submit(struct callback_head *cb) | |
2385 | { | |
2386 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2387 | ||
2388 | __io_req_task_submit(req); | |
2389 | } | |
2390 | ||
2391 | static void io_req_task_queue(struct io_kiocb *req) | |
2392 | { | |
c40f6379 JA |
2393 | int ret; |
2394 | ||
7cbf1722 | 2395 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 2396 | ret = io_req_task_work_add(req); |
c40f6379 | 2397 | if (unlikely(ret)) { |
a3df7698 | 2398 | req->result = -ECANCELED; |
04fc6c80 | 2399 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 2400 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 2401 | } |
c40f6379 JA |
2402 | } |
2403 | ||
a3df7698 PB |
2404 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
2405 | { | |
2406 | percpu_ref_get(&req->ctx->refs); | |
2407 | req->result = ret; | |
2408 | req->task_work.func = io_req_task_cancel; | |
2409 | ||
2410 | if (unlikely(io_req_task_work_add(req))) | |
2411 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2412 | } | |
2413 | ||
f2f87370 | 2414 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2415 | { |
9b5f7bd9 | 2416 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2417 | |
2418 | if (nxt) | |
906a8c3f | 2419 | io_req_task_queue(nxt); |
c69f8dbe JL |
2420 | } |
2421 | ||
c3524383 | 2422 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2423 | { |
c3524383 PB |
2424 | io_queue_next(req); |
2425 | __io_free_req(req); | |
2426 | } | |
8766dd51 | 2427 | |
2d6500d4 | 2428 | struct req_batch { |
5af1d13e PB |
2429 | struct task_struct *task; |
2430 | int task_refs; | |
1b4c351f | 2431 | int ctx_refs; |
2d6500d4 PB |
2432 | }; |
2433 | ||
5af1d13e PB |
2434 | static inline void io_init_req_batch(struct req_batch *rb) |
2435 | { | |
5af1d13e | 2436 | rb->task_refs = 0; |
9ae72463 | 2437 | rb->ctx_refs = 0; |
5af1d13e PB |
2438 | rb->task = NULL; |
2439 | } | |
2440 | ||
2d6500d4 PB |
2441 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2442 | struct req_batch *rb) | |
2443 | { | |
6e833d53 | 2444 | if (rb->task) |
7c660731 | 2445 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2446 | if (rb->ctx_refs) |
2447 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2448 | } |
2449 | ||
6ff119a6 PB |
2450 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2451 | struct io_submit_state *state) | |
2d6500d4 | 2452 | { |
f2f87370 | 2453 | io_queue_next(req); |
2d6500d4 | 2454 | |
e3bc8e9d | 2455 | if (req->task != rb->task) { |
7c660731 PB |
2456 | if (rb->task) |
2457 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2458 | rb->task = req->task; |
2459 | rb->task_refs = 0; | |
5af1d13e | 2460 | } |
e3bc8e9d | 2461 | rb->task_refs++; |
9ae72463 | 2462 | rb->ctx_refs++; |
5af1d13e | 2463 | |
4edf20f9 | 2464 | io_dismantle_req(req); |
bd759045 | 2465 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2466 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2467 | else |
2468 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2469 | } |
2470 | ||
905c172f PB |
2471 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2472 | struct io_ring_ctx *ctx) | |
2473 | { | |
2474 | int i, nr = cs->nr; | |
2475 | struct io_kiocb *req; | |
2476 | struct req_batch rb; | |
2477 | ||
2478 | io_init_req_batch(&rb); | |
2479 | spin_lock_irq(&ctx->completion_lock); | |
2480 | for (i = 0; i < nr; i++) { | |
2481 | req = cs->reqs[i]; | |
2482 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2483 | } | |
2484 | io_commit_cqring(ctx); | |
2485 | spin_unlock_irq(&ctx->completion_lock); | |
2486 | ||
2487 | io_cqring_ev_posted(ctx); | |
2488 | for (i = 0; i < nr; i++) { | |
2489 | req = cs->reqs[i]; | |
2490 | ||
2491 | /* submission and completion refs */ | |
2492 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2493 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2494 | } |
2495 | ||
2496 | io_req_free_batch_finish(ctx, &rb); | |
2497 | cs->nr = 0; | |
7a743e22 PB |
2498 | } |
2499 | ||
ba816ad6 JA |
2500 | /* |
2501 | * Drop reference to request, return next in chain (if there is one) if this | |
2502 | * was the last reference to this request. | |
2503 | */ | |
9b5f7bd9 | 2504 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2505 | { |
9b5f7bd9 PB |
2506 | struct io_kiocb *nxt = NULL; |
2507 | ||
2a44f467 | 2508 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2509 | nxt = io_req_find_next(req); |
4d7dd462 | 2510 | __io_free_req(req); |
2a44f467 | 2511 | } |
9b5f7bd9 | 2512 | return nxt; |
2b188cc1 JA |
2513 | } |
2514 | ||
e65ef56d JA |
2515 | static void io_put_req(struct io_kiocb *req) |
2516 | { | |
2517 | if (refcount_dec_and_test(&req->refs)) | |
2518 | io_free_req(req); | |
2b188cc1 JA |
2519 | } |
2520 | ||
216578e5 PB |
2521 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2522 | { | |
2523 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2524 | ||
2525 | io_free_req(req); | |
2526 | } | |
2527 | ||
2528 | static void io_free_req_deferred(struct io_kiocb *req) | |
2529 | { | |
2530 | int ret; | |
2531 | ||
7cbf1722 | 2532 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2533 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2534 | if (unlikely(ret)) |
2535 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2536 | } |
2537 | ||
2538 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2539 | { | |
2540 | if (refcount_sub_and_test(refs, &req->refs)) | |
2541 | io_free_req_deferred(req); | |
2542 | } | |
2543 | ||
978db57e JA |
2544 | static void io_double_put_req(struct io_kiocb *req) |
2545 | { | |
2546 | /* drop both submit and complete references */ | |
2547 | if (refcount_sub_and_test(2, &req->refs)) | |
2548 | io_free_req(req); | |
2549 | } | |
2550 | ||
6c503150 | 2551 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2552 | { |
2553 | /* See comment at the top of this file */ | |
2554 | smp_rmb(); | |
e23de15f | 2555 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2556 | } |
2557 | ||
fb5ccc98 PB |
2558 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2559 | { | |
2560 | struct io_rings *rings = ctx->rings; | |
2561 | ||
2562 | /* make sure SQ entry isn't read before tail */ | |
2563 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2564 | } | |
2565 | ||
8ff069bf | 2566 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2567 | { |
8ff069bf | 2568 | unsigned int cflags; |
e94f141b | 2569 | |
bcda7baa JA |
2570 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2571 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2572 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2573 | kfree(kbuf); |
2574 | return cflags; | |
e94f141b JA |
2575 | } |
2576 | ||
8ff069bf | 2577 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2578 | { |
4d954c25 | 2579 | struct io_buffer *kbuf; |
bcda7baa | 2580 | |
4d954c25 | 2581 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2582 | return io_put_kbuf(req, kbuf); |
2583 | } | |
2584 | ||
4c6e277c JA |
2585 | static inline bool io_run_task_work(void) |
2586 | { | |
6200b0ae JA |
2587 | /* |
2588 | * Not safe to run on exiting task, and the task_work handling will | |
2589 | * not add work to such a task. | |
2590 | */ | |
2591 | if (unlikely(current->flags & PF_EXITING)) | |
2592 | return false; | |
4c6e277c JA |
2593 | if (current->task_works) { |
2594 | __set_current_state(TASK_RUNNING); | |
2595 | task_work_run(); | |
2596 | return true; | |
2597 | } | |
2598 | ||
2599 | return false; | |
bcda7baa JA |
2600 | } |
2601 | ||
def596e9 JA |
2602 | /* |
2603 | * Find and free completed poll iocbs | |
2604 | */ | |
2605 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2606 | struct list_head *done) | |
2607 | { | |
8237e045 | 2608 | struct req_batch rb; |
def596e9 | 2609 | struct io_kiocb *req; |
bbde017a XW |
2610 | |
2611 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2612 | smp_rmb(); | |
def596e9 | 2613 | |
5af1d13e | 2614 | io_init_req_batch(&rb); |
def596e9 | 2615 | while (!list_empty(done)) { |
bcda7baa JA |
2616 | int cflags = 0; |
2617 | ||
d21ffe7e | 2618 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2619 | list_del(&req->inflight_entry); |
2620 | ||
bbde017a XW |
2621 | if (READ_ONCE(req->result) == -EAGAIN) { |
2622 | req->iopoll_completed = 0; | |
23faba36 | 2623 | if (io_rw_reissue(req)) |
f161340d | 2624 | continue; |
bbde017a | 2625 | } |
def596e9 | 2626 | |
bcda7baa | 2627 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2628 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2629 | |
2630 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2631 | (*nr_events)++; |
2632 | ||
c3524383 | 2633 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2634 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2635 | } |
def596e9 | 2636 | |
09bb8394 | 2637 | io_commit_cqring(ctx); |
80c18e4a | 2638 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2639 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2640 | } |
2641 | ||
def596e9 JA |
2642 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2643 | long min) | |
2644 | { | |
2645 | struct io_kiocb *req, *tmp; | |
2646 | LIST_HEAD(done); | |
2647 | bool spin; | |
2648 | int ret; | |
2649 | ||
2650 | /* | |
2651 | * Only spin for completions if we don't have multiple devices hanging | |
2652 | * off our complete list, and we're under the requested amount. | |
2653 | */ | |
2654 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2655 | ||
2656 | ret = 0; | |
d21ffe7e | 2657 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2658 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2659 | |
2660 | /* | |
581f9810 BM |
2661 | * Move completed and retryable entries to our local lists. |
2662 | * If we find a request that requires polling, break out | |
2663 | * and complete those lists first, if we have entries there. | |
def596e9 | 2664 | */ |
65a6543d | 2665 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2666 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2667 | continue; |
2668 | } | |
2669 | if (!list_empty(&done)) | |
2670 | break; | |
2671 | ||
2672 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2673 | if (ret < 0) | |
2674 | break; | |
2675 | ||
3aadc23e PB |
2676 | /* iopoll may have completed current req */ |
2677 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2678 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2679 | |
def596e9 JA |
2680 | if (ret && spin) |
2681 | spin = false; | |
2682 | ret = 0; | |
2683 | } | |
2684 | ||
2685 | if (!list_empty(&done)) | |
2686 | io_iopoll_complete(ctx, nr_events, &done); | |
2687 | ||
2688 | return ret; | |
2689 | } | |
2690 | ||
2691 | /* | |
d195a66e | 2692 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2693 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2694 | * as a non-spinning completion check. | |
2695 | */ | |
2696 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2697 | long min) | |
2698 | { | |
540e32a0 | 2699 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2700 | int ret; |
2701 | ||
2702 | ret = io_do_iopoll(ctx, nr_events, min); | |
2703 | if (ret < 0) | |
2704 | return ret; | |
eba0a4dd | 2705 | if (*nr_events >= min) |
def596e9 JA |
2706 | return 0; |
2707 | } | |
2708 | ||
2709 | return 1; | |
2710 | } | |
2711 | ||
2712 | /* | |
2713 | * We can't just wait for polled events to come to us, we have to actively | |
2714 | * find and complete them. | |
2715 | */ | |
b2edc0a7 | 2716 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2717 | { |
2718 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2719 | return; | |
2720 | ||
2721 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2722 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2723 | unsigned int nr_events = 0; |
2724 | ||
b2edc0a7 | 2725 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2726 | |
b2edc0a7 PB |
2727 | /* let it sleep and repeat later if can't complete a request */ |
2728 | if (nr_events == 0) | |
2729 | break; | |
08f5439f JA |
2730 | /* |
2731 | * Ensure we allow local-to-the-cpu processing to take place, | |
2732 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2733 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2734 | */ |
3fcee5a6 PB |
2735 | if (need_resched()) { |
2736 | mutex_unlock(&ctx->uring_lock); | |
2737 | cond_resched(); | |
2738 | mutex_lock(&ctx->uring_lock); | |
2739 | } | |
def596e9 JA |
2740 | } |
2741 | mutex_unlock(&ctx->uring_lock); | |
2742 | } | |
2743 | ||
7668b92a | 2744 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2745 | { |
7668b92a | 2746 | unsigned int nr_events = 0; |
2b2ed975 | 2747 | int iters = 0, ret = 0; |
500f9fba | 2748 | |
c7849be9 XW |
2749 | /* |
2750 | * We disallow the app entering submit/complete with polling, but we | |
2751 | * still need to lock the ring to prevent racing with polled issue | |
2752 | * that got punted to a workqueue. | |
2753 | */ | |
2754 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2755 | do { |
a3a0e43f JA |
2756 | /* |
2757 | * Don't enter poll loop if we already have events pending. | |
2758 | * If we do, we can potentially be spinning for commands that | |
2759 | * already triggered a CQE (eg in error). | |
2760 | */ | |
6c503150 PB |
2761 | if (test_bit(0, &ctx->cq_check_overflow)) |
2762 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2763 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2764 | break; |
2765 | ||
500f9fba JA |
2766 | /* |
2767 | * If a submit got punted to a workqueue, we can have the | |
2768 | * application entering polling for a command before it gets | |
2769 | * issued. That app will hold the uring_lock for the duration | |
2770 | * of the poll right here, so we need to take a breather every | |
2771 | * now and then to ensure that the issue has a chance to add | |
2772 | * the poll to the issued list. Otherwise we can spin here | |
2773 | * forever, while the workqueue is stuck trying to acquire the | |
2774 | * very same mutex. | |
2775 | */ | |
2776 | if (!(++iters & 7)) { | |
2777 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2778 | io_run_task_work(); |
500f9fba JA |
2779 | mutex_lock(&ctx->uring_lock); |
2780 | } | |
2781 | ||
7668b92a | 2782 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2783 | if (ret <= 0) |
2784 | break; | |
2785 | ret = 0; | |
7668b92a | 2786 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2787 | |
500f9fba | 2788 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2789 | return ret; |
2790 | } | |
2791 | ||
491381ce | 2792 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2793 | { |
491381ce JA |
2794 | /* |
2795 | * Tell lockdep we inherited freeze protection from submission | |
2796 | * thread. | |
2797 | */ | |
2798 | if (req->flags & REQ_F_ISREG) { | |
2799 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2800 | |
491381ce | 2801 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2802 | } |
491381ce | 2803 | file_end_write(req->file); |
2b188cc1 JA |
2804 | } |
2805 | ||
b63534c4 | 2806 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2807 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2808 | { |
2809 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2810 | int rw, ret; |
b63534c4 | 2811 | struct iov_iter iter; |
b63534c4 | 2812 | |
dc2a6e9a PB |
2813 | /* already prepared */ |
2814 | if (req->async_data) | |
2815 | return true; | |
b63534c4 JA |
2816 | |
2817 | switch (req->opcode) { | |
2818 | case IORING_OP_READV: | |
2819 | case IORING_OP_READ_FIXED: | |
2820 | case IORING_OP_READ: | |
2821 | rw = READ; | |
2822 | break; | |
2823 | case IORING_OP_WRITEV: | |
2824 | case IORING_OP_WRITE_FIXED: | |
2825 | case IORING_OP_WRITE: | |
2826 | rw = WRITE; | |
2827 | break; | |
2828 | default: | |
2829 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2830 | req->opcode); | |
dc2a6e9a | 2831 | return false; |
b63534c4 JA |
2832 | } |
2833 | ||
dc2a6e9a PB |
2834 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2835 | if (ret < 0) | |
2836 | return false; | |
6bf985dc | 2837 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2838 | } |
b63534c4 JA |
2839 | #endif |
2840 | ||
23faba36 | 2841 | static bool io_rw_reissue(struct io_kiocb *req) |
b63534c4 JA |
2842 | { |
2843 | #ifdef CONFIG_BLOCK | |
355afaeb | 2844 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2845 | int ret; |
2846 | ||
355afaeb JA |
2847 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2848 | return false; | |
75c668cd | 2849 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
b63534c4 JA |
2850 | return false; |
2851 | ||
55e6ac1e PB |
2852 | lockdep_assert_held(&req->ctx->uring_lock); |
2853 | ||
28cea78a | 2854 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2855 | |
dc2a6e9a | 2856 | if (!ret && io_resubmit_prep(req)) { |
fdee946d JA |
2857 | refcount_inc(&req->refs); |
2858 | io_queue_async_work(req); | |
b63534c4 | 2859 | return true; |
fdee946d | 2860 | } |
dc2a6e9a | 2861 | req_set_fail_links(req); |
b63534c4 JA |
2862 | #endif |
2863 | return false; | |
2864 | } | |
2865 | ||
a1d7c393 | 2866 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2867 | unsigned int issue_flags) |
a1d7c393 | 2868 | { |
2f8e45f1 PB |
2869 | int cflags = 0; |
2870 | ||
23faba36 PB |
2871 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2872 | return; | |
2f8e45f1 PB |
2873 | if (res != req->result) |
2874 | req_set_fail_links(req); | |
23faba36 | 2875 | |
2f8e45f1 PB |
2876 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2877 | kiocb_end_write(req); | |
2878 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2879 | cflags = io_put_rw_kbuf(req); | |
2880 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2881 | } |
2882 | ||
2883 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2884 | { | |
9adbd45d | 2885 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2886 | |
889fca73 | 2887 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2888 | } |
2889 | ||
def596e9 JA |
2890 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2891 | { | |
9adbd45d | 2892 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2893 | |
491381ce JA |
2894 | if (kiocb->ki_flags & IOCB_WRITE) |
2895 | kiocb_end_write(req); | |
def596e9 | 2896 | |
2d7d6792 | 2897 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2898 | req_set_fail_links(req); |
bbde017a XW |
2899 | |
2900 | WRITE_ONCE(req->result, res); | |
2901 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2902 | smp_wmb(); |
2903 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2904 | } |
2905 | ||
2906 | /* | |
2907 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2908 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2909 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2910 | * accessing the kiocb cookie. | |
2911 | */ | |
2e9dbe90 | 2912 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2913 | { |
2914 | struct io_ring_ctx *ctx = req->ctx; | |
2915 | ||
2916 | /* | |
2917 | * Track whether we have multiple files in our lists. This will impact | |
2918 | * how we do polling eventually, not spinning if we're on potentially | |
2919 | * different devices. | |
2920 | */ | |
540e32a0 | 2921 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2922 | ctx->poll_multi_file = false; |
2923 | } else if (!ctx->poll_multi_file) { | |
2924 | struct io_kiocb *list_req; | |
2925 | ||
540e32a0 | 2926 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2927 | inflight_entry); |
9adbd45d | 2928 | if (list_req->file != req->file) |
def596e9 JA |
2929 | ctx->poll_multi_file = true; |
2930 | } | |
2931 | ||
2932 | /* | |
2933 | * For fast devices, IO may have already completed. If it has, add | |
2934 | * it to the front so we find it first. | |
2935 | */ | |
65a6543d | 2936 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2937 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2938 | else |
d21ffe7e | 2939 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2940 | |
2e9dbe90 XW |
2941 | /* |
2942 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2943 | * task context or in io worker task context. If current task context is | |
2944 | * sq thread, we don't need to check whether should wake up sq thread. | |
2945 | */ | |
2946 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2947 | wq_has_sleeper(&ctx->sq_data->wait)) |
2948 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2949 | } |
2950 | ||
9f13c35b PB |
2951 | static inline void io_state_file_put(struct io_submit_state *state) |
2952 | { | |
02b23a9a PB |
2953 | if (state->file_refs) { |
2954 | fput_many(state->file, state->file_refs); | |
2955 | state->file_refs = 0; | |
2956 | } | |
9a56a232 JA |
2957 | } |
2958 | ||
2959 | /* | |
2960 | * Get as many references to a file as we have IOs left in this submission, | |
2961 | * assuming most submissions are for one file, or at least that each file | |
2962 | * has more than one submission. | |
2963 | */ | |
8da11c19 | 2964 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2965 | { |
2966 | if (!state) | |
2967 | return fget(fd); | |
2968 | ||
6e1271e6 | 2969 | if (state->file_refs) { |
9a56a232 | 2970 | if (state->fd == fd) { |
6e1271e6 | 2971 | state->file_refs--; |
9a56a232 JA |
2972 | return state->file; |
2973 | } | |
02b23a9a | 2974 | io_state_file_put(state); |
9a56a232 JA |
2975 | } |
2976 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2977 | if (unlikely(!state->file)) |
9a56a232 JA |
2978 | return NULL; |
2979 | ||
2980 | state->fd = fd; | |
6e1271e6 | 2981 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2982 | return state->file; |
2983 | } | |
2984 | ||
4503b767 JA |
2985 | static bool io_bdev_nowait(struct block_device *bdev) |
2986 | { | |
9ba0d0c8 | 2987 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2988 | } |
2989 | ||
2b188cc1 JA |
2990 | /* |
2991 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2992 | * any file. For now, just ensure that anything potentially problematic is done | |
2993 | * inline. | |
2994 | */ | |
af197f50 | 2995 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2996 | { |
2997 | umode_t mode = file_inode(file)->i_mode; | |
2998 | ||
4503b767 | 2999 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3000 | if (IS_ENABLED(CONFIG_BLOCK) && |
3001 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3002 | return true; |
3003 | return false; | |
3004 | } | |
3005 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 3006 | return true; |
4503b767 | 3007 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3008 | if (IS_ENABLED(CONFIG_BLOCK) && |
3009 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3010 | file->f_op != &io_uring_fops) |
3011 | return true; | |
3012 | return false; | |
3013 | } | |
2b188cc1 | 3014 | |
c5b85625 JA |
3015 | /* any ->read/write should understand O_NONBLOCK */ |
3016 | if (file->f_flags & O_NONBLOCK) | |
3017 | return true; | |
3018 | ||
af197f50 JA |
3019 | if (!(file->f_mode & FMODE_NOWAIT)) |
3020 | return false; | |
3021 | ||
3022 | if (rw == READ) | |
3023 | return file->f_op->read_iter != NULL; | |
3024 | ||
3025 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
3026 | } |
3027 | ||
a88fc400 | 3028 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3029 | { |
def596e9 | 3030 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 3031 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 3032 | struct file *file = req->file; |
09bb8394 JA |
3033 | unsigned ioprio; |
3034 | int ret; | |
2b188cc1 | 3035 | |
75c668cd | 3036 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
3037 | req->flags |= REQ_F_ISREG; |
3038 | ||
2b188cc1 | 3039 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 3040 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 3041 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 3042 | kiocb->ki_pos = file->f_pos; |
ba04291e | 3043 | } |
2b188cc1 | 3044 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
3045 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
3046 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
3047 | if (unlikely(ret)) | |
3048 | return ret; | |
2b188cc1 | 3049 | |
75c668cd PB |
3050 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
3051 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
3052 | req->flags |= REQ_F_NOWAIT; | |
3053 | ||
2b188cc1 JA |
3054 | ioprio = READ_ONCE(sqe->ioprio); |
3055 | if (ioprio) { | |
3056 | ret = ioprio_check_cap(ioprio); | |
3057 | if (ret) | |
09bb8394 | 3058 | return ret; |
2b188cc1 JA |
3059 | |
3060 | kiocb->ki_ioprio = ioprio; | |
3061 | } else | |
3062 | kiocb->ki_ioprio = get_current_ioprio(); | |
3063 | ||
def596e9 | 3064 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
3065 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
3066 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 3067 | return -EOPNOTSUPP; |
2b188cc1 | 3068 | |
def596e9 JA |
3069 | kiocb->ki_flags |= IOCB_HIPRI; |
3070 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 3071 | req->iopoll_completed = 0; |
def596e9 | 3072 | } else { |
09bb8394 JA |
3073 | if (kiocb->ki_flags & IOCB_HIPRI) |
3074 | return -EINVAL; | |
def596e9 JA |
3075 | kiocb->ki_complete = io_complete_rw; |
3076 | } | |
9adbd45d | 3077 | |
3529d8c2 JA |
3078 | req->rw.addr = READ_ONCE(sqe->addr); |
3079 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3080 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3081 | return 0; |
2b188cc1 JA |
3082 | } |
3083 | ||
3084 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3085 | { | |
3086 | switch (ret) { | |
3087 | case -EIOCBQUEUED: | |
3088 | break; | |
3089 | case -ERESTARTSYS: | |
3090 | case -ERESTARTNOINTR: | |
3091 | case -ERESTARTNOHAND: | |
3092 | case -ERESTART_RESTARTBLOCK: | |
3093 | /* | |
3094 | * We can't just restart the syscall, since previously | |
3095 | * submitted sqes may already be in progress. Just fail this | |
3096 | * IO with EINTR. | |
3097 | */ | |
3098 | ret = -EINTR; | |
df561f66 | 3099 | fallthrough; |
2b188cc1 JA |
3100 | default: |
3101 | kiocb->ki_complete(kiocb, ret, 0); | |
3102 | } | |
3103 | } | |
3104 | ||
a1d7c393 | 3105 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 3106 | unsigned int issue_flags) |
ba816ad6 | 3107 | { |
ba04291e | 3108 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 3109 | struct io_async_rw *io = req->async_data; |
ba04291e | 3110 | |
227c0c96 | 3111 | /* add previously done IO, if any */ |
e8c2bc1f | 3112 | if (io && io->bytes_done > 0) { |
227c0c96 | 3113 | if (ret < 0) |
e8c2bc1f | 3114 | ret = io->bytes_done; |
227c0c96 | 3115 | else |
e8c2bc1f | 3116 | ret += io->bytes_done; |
227c0c96 JA |
3117 | } |
3118 | ||
ba04291e JA |
3119 | if (req->flags & REQ_F_CUR_POS) |
3120 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 3121 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 3122 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
3123 | else |
3124 | io_rw_done(kiocb, ret); | |
3125 | } | |
3126 | ||
847595de | 3127 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 3128 | { |
9adbd45d JA |
3129 | struct io_ring_ctx *ctx = req->ctx; |
3130 | size_t len = req->rw.len; | |
edafccee | 3131 | struct io_mapped_ubuf *imu; |
4be1c615 | 3132 | u16 index, buf_index = req->buf_index; |
edafccee JA |
3133 | size_t offset; |
3134 | u64 buf_addr; | |
3135 | ||
edafccee JA |
3136 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3137 | return -EFAULT; | |
edafccee JA |
3138 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3139 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 3140 | buf_addr = req->rw.addr; |
edafccee JA |
3141 | |
3142 | /* overflow */ | |
3143 | if (buf_addr + len < buf_addr) | |
3144 | return -EFAULT; | |
3145 | /* not inside the mapped region */ | |
3146 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
3147 | return -EFAULT; | |
3148 | ||
3149 | /* | |
3150 | * May not be a start of buffer, set size appropriately | |
3151 | * and advance us to the beginning. | |
3152 | */ | |
3153 | offset = buf_addr - imu->ubuf; | |
3154 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3155 | |
3156 | if (offset) { | |
3157 | /* | |
3158 | * Don't use iov_iter_advance() here, as it's really slow for | |
3159 | * using the latter parts of a big fixed buffer - it iterates | |
3160 | * over each segment manually. We can cheat a bit here, because | |
3161 | * we know that: | |
3162 | * | |
3163 | * 1) it's a BVEC iter, we set it up | |
3164 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3165 | * first and last bvec | |
3166 | * | |
3167 | * So just find our index, and adjust the iterator afterwards. | |
3168 | * If the offset is within the first bvec (or the whole first | |
3169 | * bvec, just use iov_iter_advance(). This makes it easier | |
3170 | * since we can just skip the first segment, which may not | |
3171 | * be PAGE_SIZE aligned. | |
3172 | */ | |
3173 | const struct bio_vec *bvec = imu->bvec; | |
3174 | ||
3175 | if (offset <= bvec->bv_len) { | |
3176 | iov_iter_advance(iter, offset); | |
3177 | } else { | |
3178 | unsigned long seg_skip; | |
3179 | ||
3180 | /* skip first vec */ | |
3181 | offset -= bvec->bv_len; | |
3182 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3183 | ||
3184 | iter->bvec = bvec + seg_skip; | |
3185 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3186 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3187 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3188 | } |
3189 | } | |
3190 | ||
847595de | 3191 | return 0; |
edafccee JA |
3192 | } |
3193 | ||
bcda7baa JA |
3194 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3195 | { | |
3196 | if (needs_lock) | |
3197 | mutex_unlock(&ctx->uring_lock); | |
3198 | } | |
3199 | ||
3200 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3201 | { | |
3202 | /* | |
3203 | * "Normal" inline submissions always hold the uring_lock, since we | |
3204 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3205 | * The only exception is when we've detached the request and issue it | |
3206 | * from an async worker thread, grab the lock for that case. | |
3207 | */ | |
3208 | if (needs_lock) | |
3209 | mutex_lock(&ctx->uring_lock); | |
3210 | } | |
3211 | ||
3212 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3213 | int bgid, struct io_buffer *kbuf, | |
3214 | bool needs_lock) | |
3215 | { | |
3216 | struct io_buffer *head; | |
3217 | ||
3218 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3219 | return kbuf; | |
3220 | ||
3221 | io_ring_submit_lock(req->ctx, needs_lock); | |
3222 | ||
3223 | lockdep_assert_held(&req->ctx->uring_lock); | |
3224 | ||
3225 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3226 | if (head) { | |
3227 | if (!list_empty(&head->list)) { | |
3228 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3229 | list); | |
3230 | list_del(&kbuf->list); | |
3231 | } else { | |
3232 | kbuf = head; | |
3233 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3234 | } | |
3235 | if (*len > kbuf->len) | |
3236 | *len = kbuf->len; | |
3237 | } else { | |
3238 | kbuf = ERR_PTR(-ENOBUFS); | |
3239 | } | |
3240 | ||
3241 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3242 | ||
3243 | return kbuf; | |
3244 | } | |
3245 | ||
4d954c25 JA |
3246 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3247 | bool needs_lock) | |
3248 | { | |
3249 | struct io_buffer *kbuf; | |
4f4eeba8 | 3250 | u16 bgid; |
4d954c25 JA |
3251 | |
3252 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3253 | bgid = req->buf_index; |
4d954c25 JA |
3254 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3255 | if (IS_ERR(kbuf)) | |
3256 | return kbuf; | |
3257 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3258 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3259 | return u64_to_user_ptr(kbuf->addr); | |
3260 | } | |
3261 | ||
3262 | #ifdef CONFIG_COMPAT | |
3263 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3264 | bool needs_lock) | |
3265 | { | |
3266 | struct compat_iovec __user *uiov; | |
3267 | compat_ssize_t clen; | |
3268 | void __user *buf; | |
3269 | ssize_t len; | |
3270 | ||
3271 | uiov = u64_to_user_ptr(req->rw.addr); | |
3272 | if (!access_ok(uiov, sizeof(*uiov))) | |
3273 | return -EFAULT; | |
3274 | if (__get_user(clen, &uiov->iov_len)) | |
3275 | return -EFAULT; | |
3276 | if (clen < 0) | |
3277 | return -EINVAL; | |
3278 | ||
3279 | len = clen; | |
3280 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3281 | if (IS_ERR(buf)) | |
3282 | return PTR_ERR(buf); | |
3283 | iov[0].iov_base = buf; | |
3284 | iov[0].iov_len = (compat_size_t) len; | |
3285 | return 0; | |
3286 | } | |
3287 | #endif | |
3288 | ||
3289 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3290 | bool needs_lock) | |
3291 | { | |
3292 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3293 | void __user *buf; | |
3294 | ssize_t len; | |
3295 | ||
3296 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3297 | return -EFAULT; | |
3298 | ||
3299 | len = iov[0].iov_len; | |
3300 | if (len < 0) | |
3301 | return -EINVAL; | |
3302 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3303 | if (IS_ERR(buf)) | |
3304 | return PTR_ERR(buf); | |
3305 | iov[0].iov_base = buf; | |
3306 | iov[0].iov_len = len; | |
3307 | return 0; | |
3308 | } | |
3309 | ||
3310 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3311 | bool needs_lock) | |
3312 | { | |
dddb3e26 JA |
3313 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3314 | struct io_buffer *kbuf; | |
3315 | ||
3316 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3317 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3318 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3319 | return 0; |
dddb3e26 | 3320 | } |
dd201662 | 3321 | if (req->rw.len != 1) |
4d954c25 JA |
3322 | return -EINVAL; |
3323 | ||
3324 | #ifdef CONFIG_COMPAT | |
3325 | if (req->ctx->compat) | |
3326 | return io_compat_import(req, iov, needs_lock); | |
3327 | #endif | |
3328 | ||
3329 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3330 | } | |
3331 | ||
847595de PB |
3332 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3333 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3334 | { |
9adbd45d JA |
3335 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3336 | size_t sqe_len = req->rw.len; | |
847595de | 3337 | u8 opcode = req->opcode; |
4d954c25 | 3338 | ssize_t ret; |
edafccee | 3339 | |
7d009165 | 3340 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3341 | *iovec = NULL; |
9adbd45d | 3342 | return io_import_fixed(req, rw, iter); |
edafccee | 3343 | } |
2b188cc1 | 3344 | |
bcda7baa | 3345 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3346 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3347 | return -EINVAL; |
3348 | ||
3a6820f2 | 3349 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3350 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3351 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3352 | if (IS_ERR(buf)) |
4d954c25 | 3353 | return PTR_ERR(buf); |
3f9d6441 | 3354 | req->rw.len = sqe_len; |
bcda7baa JA |
3355 | } |
3356 | ||
3a6820f2 JA |
3357 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3358 | *iovec = NULL; | |
10fc72e4 | 3359 | return ret; |
3a6820f2 JA |
3360 | } |
3361 | ||
4d954c25 JA |
3362 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3363 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3364 | if (!ret) |
3365 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3366 | *iovec = NULL; |
3367 | return ret; | |
3368 | } | |
3369 | ||
89cd35c5 CH |
3370 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3371 | req->ctx->compat); | |
2b188cc1 JA |
3372 | } |
3373 | ||
0fef9483 JA |
3374 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3375 | { | |
5b09e37e | 3376 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3377 | } |
3378 | ||
31b51510 | 3379 | /* |
32960613 JA |
3380 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3381 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3382 | */ |
4017eb91 | 3383 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3384 | { |
4017eb91 JA |
3385 | struct kiocb *kiocb = &req->rw.kiocb; |
3386 | struct file *file = req->file; | |
32960613 JA |
3387 | ssize_t ret = 0; |
3388 | ||
3389 | /* | |
3390 | * Don't support polled IO through this interface, and we can't | |
3391 | * support non-blocking either. For the latter, this just causes | |
3392 | * the kiocb to be handled from an async context. | |
3393 | */ | |
3394 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3395 | return -EOPNOTSUPP; | |
3396 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3397 | return -EAGAIN; | |
3398 | ||
3399 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3400 | struct iovec iovec; |
32960613 JA |
3401 | ssize_t nr; |
3402 | ||
311ae9e1 PB |
3403 | if (!iov_iter_is_bvec(iter)) { |
3404 | iovec = iov_iter_iovec(iter); | |
3405 | } else { | |
4017eb91 JA |
3406 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3407 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3408 | } |
3409 | ||
32960613 JA |
3410 | if (rw == READ) { |
3411 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3412 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3413 | } else { |
3414 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3415 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3416 | } |
3417 | ||
3418 | if (nr < 0) { | |
3419 | if (!ret) | |
3420 | ret = nr; | |
3421 | break; | |
3422 | } | |
3423 | ret += nr; | |
3424 | if (nr != iovec.iov_len) | |
3425 | break; | |
4017eb91 JA |
3426 | req->rw.len -= nr; |
3427 | req->rw.addr += nr; | |
32960613 JA |
3428 | iov_iter_advance(iter, nr); |
3429 | } | |
3430 | ||
3431 | return ret; | |
3432 | } | |
3433 | ||
ff6165b2 JA |
3434 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3435 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3436 | { |
e8c2bc1f | 3437 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3438 | |
ff6165b2 | 3439 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3440 | rw->free_iovec = iovec; |
227c0c96 | 3441 | rw->bytes_done = 0; |
ff6165b2 | 3442 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3443 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3444 | return; |
b64e3444 | 3445 | if (!iovec) { |
ff6165b2 JA |
3446 | unsigned iov_off = 0; |
3447 | ||
3448 | rw->iter.iov = rw->fast_iov; | |
3449 | if (iter->iov != fast_iov) { | |
3450 | iov_off = iter->iov - fast_iov; | |
3451 | rw->iter.iov += iov_off; | |
3452 | } | |
3453 | if (rw->fast_iov != fast_iov) | |
3454 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3455 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3456 | } else { |
3457 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3458 | } |
3459 | } | |
3460 | ||
e8c2bc1f | 3461 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3462 | { |
e8c2bc1f JA |
3463 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3464 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3465 | return req->async_data == NULL; | |
3d9932a8 XW |
3466 | } |
3467 | ||
e8c2bc1f | 3468 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3469 | { |
e8c2bc1f | 3470 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3471 | return 0; |
3d9932a8 | 3472 | |
e8c2bc1f | 3473 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3474 | } |
3475 | ||
ff6165b2 JA |
3476 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3477 | const struct iovec *fast_iov, | |
227c0c96 | 3478 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3479 | { |
e8c2bc1f | 3480 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3481 | return 0; |
e8c2bc1f | 3482 | if (!req->async_data) { |
6bf985dc PB |
3483 | if (__io_alloc_async_data(req)) { |
3484 | kfree(iovec); | |
5d204bcf | 3485 | return -ENOMEM; |
6bf985dc | 3486 | } |
b7bb4f7d | 3487 | |
ff6165b2 | 3488 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3489 | } |
b7bb4f7d | 3490 | return 0; |
f67676d1 JA |
3491 | } |
3492 | ||
73debe68 | 3493 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3494 | { |
e8c2bc1f | 3495 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3496 | struct iovec *iov = iorw->fast_iov; |
847595de | 3497 | int ret; |
c3e330a4 | 3498 | |
2846c481 | 3499 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3500 | if (unlikely(ret < 0)) |
3501 | return ret; | |
3502 | ||
ab0b196c PB |
3503 | iorw->bytes_done = 0; |
3504 | iorw->free_iovec = iov; | |
3505 | if (iov) | |
3506 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3507 | return 0; |
3508 | } | |
3509 | ||
73debe68 | 3510 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3511 | { |
3529d8c2 JA |
3512 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3513 | return -EBADF; | |
93642ef8 | 3514 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3515 | } |
3516 | ||
c1dd91d1 JA |
3517 | /* |
3518 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3519 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3520 | * This gets called when the page is unlocked, and we generally expect that to | |
3521 | * happen when the page IO is completed and the page is now uptodate. This will | |
3522 | * queue a task_work based retry of the operation, attempting to copy the data | |
3523 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3524 | * do a thread based blocking retry of the operation. That's the unexpected | |
3525 | * slow path. | |
3526 | */ | |
bcf5a063 JA |
3527 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3528 | int sync, void *arg) | |
3529 | { | |
3530 | struct wait_page_queue *wpq; | |
3531 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3532 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3533 | |
3534 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3535 | ||
cdc8fcb4 LT |
3536 | if (!wake_page_match(wpq, key)) |
3537 | return 0; | |
3538 | ||
c8d317aa | 3539 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3540 | list_del_init(&wait->entry); |
3541 | ||
bcf5a063 JA |
3542 | /* submit ref gets dropped, acquire a new one */ |
3543 | refcount_inc(&req->refs); | |
921b9054 | 3544 | io_req_task_queue(req); |
bcf5a063 JA |
3545 | return 1; |
3546 | } | |
3547 | ||
c1dd91d1 JA |
3548 | /* |
3549 | * This controls whether a given IO request should be armed for async page | |
3550 | * based retry. If we return false here, the request is handed to the async | |
3551 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3552 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3553 | * will either succeed because the page is now uptodate and unlocked, or it | |
3554 | * will register a callback when the page is unlocked at IO completion. Through | |
3555 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3556 | * That retry will attempt the buffered read again. The retry will generally | |
3557 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3558 | * async worker threads for a blocking retry. | |
3559 | */ | |
227c0c96 | 3560 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3561 | { |
e8c2bc1f JA |
3562 | struct io_async_rw *rw = req->async_data; |
3563 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3564 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3565 | |
bcf5a063 JA |
3566 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3567 | if (req->flags & REQ_F_NOWAIT) | |
3568 | return false; | |
f67676d1 | 3569 | |
227c0c96 | 3570 | /* Only for buffered IO */ |
3b2a4439 | 3571 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3572 | return false; |
3b2a4439 | 3573 | |
bcf5a063 JA |
3574 | /* |
3575 | * just use poll if we can, and don't attempt if the fs doesn't | |
3576 | * support callback based unlocks | |
3577 | */ | |
3578 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3579 | return false; | |
f67676d1 | 3580 | |
3b2a4439 JA |
3581 | wait->wait.func = io_async_buf_func; |
3582 | wait->wait.private = req; | |
3583 | wait->wait.flags = 0; | |
3584 | INIT_LIST_HEAD(&wait->wait.entry); | |
3585 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3586 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3587 | kiocb->ki_waitq = wait; |
3b2a4439 | 3588 | return true; |
bcf5a063 JA |
3589 | } |
3590 | ||
3591 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3592 | { | |
3593 | if (req->file->f_op->read_iter) | |
3594 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3595 | else if (req->file->f_op->read) |
4017eb91 | 3596 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3597 | else |
3598 | return -EINVAL; | |
f67676d1 JA |
3599 | } |
3600 | ||
889fca73 | 3601 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3602 | { |
3603 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3604 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3605 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3606 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3607 | ssize_t io_size, ret, ret2; |
45d189c6 | 3608 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3609 | |
2846c481 | 3610 | if (rw) { |
e8c2bc1f | 3611 | iter = &rw->iter; |
2846c481 PB |
3612 | iovec = NULL; |
3613 | } else { | |
3614 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3615 | if (ret < 0) | |
3616 | return ret; | |
3617 | } | |
632546c4 | 3618 | io_size = iov_iter_count(iter); |
fa15bafb | 3619 | req->result = io_size; |
2b188cc1 | 3620 | |
fd6c2e4c JA |
3621 | /* Ensure we clear previously set non-block flag */ |
3622 | if (!force_nonblock) | |
29de5f6a | 3623 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3624 | else |
3625 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3626 | ||
24c74678 | 3627 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3628 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3629 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3630 | return ret ?: -EAGAIN; |
6713e7a6 | 3631 | } |
9e645e11 | 3632 | |
632546c4 | 3633 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3634 | if (unlikely(ret)) { |
3635 | kfree(iovec); | |
3636 | return ret; | |
3637 | } | |
2b188cc1 | 3638 | |
227c0c96 | 3639 | ret = io_iter_do_read(req, iter); |
32960613 | 3640 | |
57cd657b | 3641 | if (ret == -EIOCBQUEUED) { |
fe1cdd55 | 3642 | goto out_free; |
227c0c96 | 3643 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3644 | /* IOPOLL retry should happen for io-wq threads */ |
3645 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3646 | goto done; |
75c668cd PB |
3647 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3648 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3649 | goto done; |
84216315 | 3650 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3651 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3652 | ret = 0; |
7335e3bf | 3653 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3654 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3655 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3656 | goto done; |
227c0c96 JA |
3657 | } |
3658 | ||
227c0c96 | 3659 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3660 | if (ret2) |
3661 | return ret2; | |
3662 | ||
fe1cdd55 | 3663 | iovec = NULL; |
e8c2bc1f | 3664 | rw = req->async_data; |
227c0c96 | 3665 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3666 | iter = &rw->iter; |
227c0c96 | 3667 | |
b23df91b PB |
3668 | do { |
3669 | io_size -= ret; | |
3670 | rw->bytes_done += ret; | |
3671 | /* if we can retry, do so with the callbacks armed */ | |
3672 | if (!io_rw_should_retry(req)) { | |
3673 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3674 | return -EAGAIN; | |
3675 | } | |
3676 | ||
3677 | /* | |
3678 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3679 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3680 | * desired page gets unlocked. We can also get a partial read | |
3681 | * here, and if we do, then just retry at the new offset. | |
3682 | */ | |
3683 | ret = io_iter_do_read(req, iter); | |
3684 | if (ret == -EIOCBQUEUED) | |
3685 | return 0; | |
227c0c96 | 3686 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3687 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3688 | done: |
889fca73 | 3689 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3690 | out_free: |
3691 | /* it's faster to check here then delegate to kfree */ | |
3692 | if (iovec) | |
3693 | kfree(iovec); | |
5ea5dd45 | 3694 | return 0; |
2b188cc1 JA |
3695 | } |
3696 | ||
73debe68 | 3697 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3698 | { |
3529d8c2 JA |
3699 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3700 | return -EBADF; | |
93642ef8 | 3701 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3702 | } |
3703 | ||
889fca73 | 3704 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3705 | { |
3706 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3707 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3708 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3709 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3710 | ssize_t ret, ret2, io_size; |
45d189c6 | 3711 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3712 | |
2846c481 | 3713 | if (rw) { |
e8c2bc1f | 3714 | iter = &rw->iter; |
2846c481 PB |
3715 | iovec = NULL; |
3716 | } else { | |
3717 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3718 | if (ret < 0) | |
3719 | return ret; | |
3720 | } | |
632546c4 | 3721 | io_size = iov_iter_count(iter); |
fa15bafb | 3722 | req->result = io_size; |
2b188cc1 | 3723 | |
fd6c2e4c JA |
3724 | /* Ensure we clear previously set non-block flag */ |
3725 | if (!force_nonblock) | |
a88fc400 PB |
3726 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3727 | else | |
3728 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3729 | |
24c74678 | 3730 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3731 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3732 | goto copy_iov; |
31b51510 | 3733 | |
10d59345 JA |
3734 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3735 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3736 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3737 | goto copy_iov; |
31b51510 | 3738 | |
632546c4 | 3739 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3740 | if (unlikely(ret)) |
3741 | goto out_free; | |
4ed734b0 | 3742 | |
fa15bafb PB |
3743 | /* |
3744 | * Open-code file_start_write here to grab freeze protection, | |
3745 | * which will be released by another thread in | |
3746 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3747 | * released so that it doesn't complain about the held lock when | |
3748 | * we return to userspace. | |
3749 | */ | |
3750 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3751 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3752 | __sb_writers_release(file_inode(req->file)->i_sb, |
3753 | SB_FREEZE_WRITE); | |
3754 | } | |
3755 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3756 | |
fa15bafb | 3757 | if (req->file->f_op->write_iter) |
ff6165b2 | 3758 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3759 | else if (req->file->f_op->write) |
4017eb91 | 3760 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3761 | else |
3762 | ret2 = -EINVAL; | |
4ed734b0 | 3763 | |
fa15bafb PB |
3764 | /* |
3765 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3766 | * retry them without IOCB_NOWAIT. | |
3767 | */ | |
3768 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3769 | ret2 = -EAGAIN; | |
75c668cd PB |
3770 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3771 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3772 | goto done; |
fa15bafb | 3773 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3774 | /* IOPOLL retry should happen for io-wq threads */ |
3775 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3776 | goto copy_iov; | |
355afaeb | 3777 | done: |
889fca73 | 3778 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3779 | } else { |
f67676d1 | 3780 | copy_iov: |
84216315 | 3781 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3782 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3783 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3784 | return ret ?: -EAGAIN; |
2b188cc1 | 3785 | } |
31b51510 | 3786 | out_free: |
f261c168 | 3787 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3788 | if (iovec) |
6f2cc166 | 3789 | kfree(iovec); |
2b188cc1 JA |
3790 | return ret; |
3791 | } | |
3792 | ||
80a261fd JA |
3793 | static int io_renameat_prep(struct io_kiocb *req, |
3794 | const struct io_uring_sqe *sqe) | |
3795 | { | |
3796 | struct io_rename *ren = &req->rename; | |
3797 | const char __user *oldf, *newf; | |
3798 | ||
3799 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3800 | return -EBADF; | |
3801 | ||
3802 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3803 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3804 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3805 | ren->new_dfd = READ_ONCE(sqe->len); | |
3806 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3807 | ||
3808 | ren->oldpath = getname(oldf); | |
3809 | if (IS_ERR(ren->oldpath)) | |
3810 | return PTR_ERR(ren->oldpath); | |
3811 | ||
3812 | ren->newpath = getname(newf); | |
3813 | if (IS_ERR(ren->newpath)) { | |
3814 | putname(ren->oldpath); | |
3815 | return PTR_ERR(ren->newpath); | |
3816 | } | |
3817 | ||
3818 | req->flags |= REQ_F_NEED_CLEANUP; | |
3819 | return 0; | |
3820 | } | |
3821 | ||
45d189c6 | 3822 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3823 | { |
3824 | struct io_rename *ren = &req->rename; | |
3825 | int ret; | |
3826 | ||
45d189c6 | 3827 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3828 | return -EAGAIN; |
3829 | ||
3830 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3831 | ren->newpath, ren->flags); | |
3832 | ||
3833 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3834 | if (ret < 0) | |
3835 | req_set_fail_links(req); | |
3836 | io_req_complete(req, ret); | |
3837 | return 0; | |
3838 | } | |
3839 | ||
14a1143b JA |
3840 | static int io_unlinkat_prep(struct io_kiocb *req, |
3841 | const struct io_uring_sqe *sqe) | |
3842 | { | |
3843 | struct io_unlink *un = &req->unlink; | |
3844 | const char __user *fname; | |
3845 | ||
3846 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3847 | return -EBADF; | |
3848 | ||
3849 | un->dfd = READ_ONCE(sqe->fd); | |
3850 | ||
3851 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3852 | if (un->flags & ~AT_REMOVEDIR) | |
3853 | return -EINVAL; | |
3854 | ||
3855 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3856 | un->filename = getname(fname); | |
3857 | if (IS_ERR(un->filename)) | |
3858 | return PTR_ERR(un->filename); | |
3859 | ||
3860 | req->flags |= REQ_F_NEED_CLEANUP; | |
3861 | return 0; | |
3862 | } | |
3863 | ||
45d189c6 | 3864 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3865 | { |
3866 | struct io_unlink *un = &req->unlink; | |
3867 | int ret; | |
3868 | ||
45d189c6 | 3869 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3870 | return -EAGAIN; |
3871 | ||
3872 | if (un->flags & AT_REMOVEDIR) | |
3873 | ret = do_rmdir(un->dfd, un->filename); | |
3874 | else | |
3875 | ret = do_unlinkat(un->dfd, un->filename); | |
3876 | ||
3877 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3878 | if (ret < 0) | |
3879 | req_set_fail_links(req); | |
3880 | io_req_complete(req, ret); | |
3881 | return 0; | |
3882 | } | |
3883 | ||
36f4fa68 JA |
3884 | static int io_shutdown_prep(struct io_kiocb *req, |
3885 | const struct io_uring_sqe *sqe) | |
3886 | { | |
3887 | #if defined(CONFIG_NET) | |
3888 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3889 | return -EINVAL; | |
3890 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3891 | sqe->buf_index) | |
3892 | return -EINVAL; | |
3893 | ||
3894 | req->shutdown.how = READ_ONCE(sqe->len); | |
3895 | return 0; | |
3896 | #else | |
3897 | return -EOPNOTSUPP; | |
3898 | #endif | |
3899 | } | |
3900 | ||
45d189c6 | 3901 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3902 | { |
3903 | #if defined(CONFIG_NET) | |
3904 | struct socket *sock; | |
3905 | int ret; | |
3906 | ||
45d189c6 | 3907 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3908 | return -EAGAIN; |
3909 | ||
48aba79b | 3910 | sock = sock_from_file(req->file); |
36f4fa68 | 3911 | if (unlikely(!sock)) |
48aba79b | 3912 | return -ENOTSOCK; |
36f4fa68 JA |
3913 | |
3914 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3915 | if (ret < 0) |
3916 | req_set_fail_links(req); | |
36f4fa68 JA |
3917 | io_req_complete(req, ret); |
3918 | return 0; | |
3919 | #else | |
3920 | return -EOPNOTSUPP; | |
3921 | #endif | |
3922 | } | |
3923 | ||
f2a8d5c7 PB |
3924 | static int __io_splice_prep(struct io_kiocb *req, |
3925 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3926 | { |
3927 | struct io_splice* sp = &req->splice; | |
3928 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3929 | |
3232dd02 PB |
3930 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3931 | return -EINVAL; | |
7d67af2c PB |
3932 | |
3933 | sp->file_in = NULL; | |
7d67af2c PB |
3934 | sp->len = READ_ONCE(sqe->len); |
3935 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3936 | ||
3937 | if (unlikely(sp->flags & ~valid_flags)) | |
3938 | return -EINVAL; | |
3939 | ||
8371adf5 PB |
3940 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3941 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3942 | if (!sp->file_in) | |
3943 | return -EBADF; | |
7d67af2c PB |
3944 | req->flags |= REQ_F_NEED_CLEANUP; |
3945 | ||
7cdaf587 XW |
3946 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3947 | /* | |
3948 | * Splice operation will be punted aync, and here need to | |
3949 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3950 | */ | |
3951 | io_req_init_async(req); | |
7d67af2c | 3952 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3953 | } |
7d67af2c PB |
3954 | |
3955 | return 0; | |
3956 | } | |
3957 | ||
f2a8d5c7 PB |
3958 | static int io_tee_prep(struct io_kiocb *req, |
3959 | const struct io_uring_sqe *sqe) | |
3960 | { | |
3961 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3962 | return -EINVAL; | |
3963 | return __io_splice_prep(req, sqe); | |
3964 | } | |
3965 | ||
45d189c6 | 3966 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3967 | { |
3968 | struct io_splice *sp = &req->splice; | |
3969 | struct file *in = sp->file_in; | |
3970 | struct file *out = sp->file_out; | |
3971 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3972 | long ret = 0; | |
3973 | ||
45d189c6 | 3974 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3975 | return -EAGAIN; |
3976 | if (sp->len) | |
3977 | ret = do_tee(in, out, sp->len, flags); | |
3978 | ||
3979 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3980 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3981 | ||
f2a8d5c7 PB |
3982 | if (ret != sp->len) |
3983 | req_set_fail_links(req); | |
e1e16097 | 3984 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3985 | return 0; |
3986 | } | |
3987 | ||
3988 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3989 | { | |
3990 | struct io_splice* sp = &req->splice; | |
3991 | ||
3992 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3993 | sp->off_out = READ_ONCE(sqe->off); | |
3994 | return __io_splice_prep(req, sqe); | |
3995 | } | |
3996 | ||
45d189c6 | 3997 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3998 | { |
3999 | struct io_splice *sp = &req->splice; | |
4000 | struct file *in = sp->file_in; | |
4001 | struct file *out = sp->file_out; | |
4002 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4003 | loff_t *poff_in, *poff_out; | |
c9687426 | 4004 | long ret = 0; |
7d67af2c | 4005 | |
45d189c6 | 4006 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4007 | return -EAGAIN; |
7d67af2c PB |
4008 | |
4009 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4010 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4011 | |
948a7749 | 4012 | if (sp->len) |
c9687426 | 4013 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
4014 | |
4015 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
4016 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4017 | ||
7d67af2c PB |
4018 | if (ret != sp->len) |
4019 | req_set_fail_links(req); | |
e1e16097 | 4020 | io_req_complete(req, ret); |
7d67af2c PB |
4021 | return 0; |
4022 | } | |
4023 | ||
2b188cc1 JA |
4024 | /* |
4025 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4026 | */ | |
889fca73 | 4027 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4028 | { |
4029 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4030 | |
def596e9 JA |
4031 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4032 | return -EINVAL; | |
4033 | ||
889fca73 | 4034 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4035 | return 0; |
4036 | } | |
4037 | ||
1155c76a | 4038 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4039 | { |
6b06314c | 4040 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4041 | |
09bb8394 JA |
4042 | if (!req->file) |
4043 | return -EBADF; | |
c992fe29 | 4044 | |
6b06314c | 4045 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4046 | return -EINVAL; |
edafccee | 4047 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
4048 | return -EINVAL; |
4049 | ||
8ed8d3c3 JA |
4050 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4051 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4052 | return -EINVAL; | |
4053 | ||
4054 | req->sync.off = READ_ONCE(sqe->off); | |
4055 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4056 | return 0; |
4057 | } | |
4058 | ||
45d189c6 | 4059 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4060 | { |
8ed8d3c3 | 4061 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4062 | int ret; |
4063 | ||
ac45abc0 | 4064 | /* fsync always requires a blocking context */ |
45d189c6 | 4065 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4066 | return -EAGAIN; |
4067 | ||
9adbd45d | 4068 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4069 | end > 0 ? end : LLONG_MAX, |
4070 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4071 | if (ret < 0) | |
4072 | req_set_fail_links(req); | |
e1e16097 | 4073 | io_req_complete(req, ret); |
c992fe29 CH |
4074 | return 0; |
4075 | } | |
4076 | ||
d63d1b5e JA |
4077 | static int io_fallocate_prep(struct io_kiocb *req, |
4078 | const struct io_uring_sqe *sqe) | |
4079 | { | |
4080 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
4081 | return -EINVAL; | |
3232dd02 PB |
4082 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4083 | return -EINVAL; | |
d63d1b5e JA |
4084 | |
4085 | req->sync.off = READ_ONCE(sqe->off); | |
4086 | req->sync.len = READ_ONCE(sqe->addr); | |
4087 | req->sync.mode = READ_ONCE(sqe->len); | |
4088 | return 0; | |
4089 | } | |
4090 | ||
45d189c6 | 4091 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4092 | { |
ac45abc0 PB |
4093 | int ret; |
4094 | ||
d63d1b5e | 4095 | /* fallocate always requiring blocking context */ |
45d189c6 | 4096 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4097 | return -EAGAIN; |
ac45abc0 PB |
4098 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4099 | req->sync.len); | |
ac45abc0 PB |
4100 | if (ret < 0) |
4101 | req_set_fail_links(req); | |
e1e16097 | 4102 | io_req_complete(req, ret); |
5d17b4a4 JA |
4103 | return 0; |
4104 | } | |
4105 | ||
ec65fea5 | 4106 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4107 | { |
f8748881 | 4108 | const char __user *fname; |
15b71abe | 4109 | int ret; |
b7bb4f7d | 4110 | |
ec65fea5 | 4111 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4112 | return -EINVAL; |
ec65fea5 | 4113 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4114 | return -EBADF; |
03b1230c | 4115 | |
ec65fea5 PB |
4116 | /* open.how should be already initialised */ |
4117 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4118 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4119 | |
25e72d10 PB |
4120 | req->open.dfd = READ_ONCE(sqe->fd); |
4121 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4122 | req->open.filename = getname(fname); |
15b71abe JA |
4123 | if (IS_ERR(req->open.filename)) { |
4124 | ret = PTR_ERR(req->open.filename); | |
4125 | req->open.filename = NULL; | |
4126 | return ret; | |
4127 | } | |
4022e7af | 4128 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4129 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4130 | return 0; |
03b1230c JA |
4131 | } |
4132 | ||
ec65fea5 PB |
4133 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4134 | { | |
4135 | u64 flags, mode; | |
4136 | ||
14587a46 | 4137 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4138 | return -EINVAL; |
ec65fea5 PB |
4139 | mode = READ_ONCE(sqe->len); |
4140 | flags = READ_ONCE(sqe->open_flags); | |
4141 | req->open.how = build_open_how(flags, mode); | |
4142 | return __io_openat_prep(req, sqe); | |
4143 | } | |
4144 | ||
cebdb986 | 4145 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4146 | { |
cebdb986 | 4147 | struct open_how __user *how; |
cebdb986 | 4148 | size_t len; |
0fa03c62 JA |
4149 | int ret; |
4150 | ||
14587a46 | 4151 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4152 | return -EINVAL; |
cebdb986 JA |
4153 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4154 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4155 | if (len < OPEN_HOW_SIZE_VER0) |
4156 | return -EINVAL; | |
3529d8c2 | 4157 | |
cebdb986 JA |
4158 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4159 | len); | |
4160 | if (ret) | |
4161 | return ret; | |
3529d8c2 | 4162 | |
ec65fea5 | 4163 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4164 | } |
4165 | ||
45d189c6 | 4166 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4167 | { |
4168 | struct open_flags op; | |
15b71abe | 4169 | struct file *file; |
3a81fd02 JA |
4170 | bool nonblock_set; |
4171 | bool resolve_nonblock; | |
15b71abe JA |
4172 | int ret; |
4173 | ||
cebdb986 | 4174 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4175 | if (ret) |
4176 | goto err; | |
3a81fd02 JA |
4177 | nonblock_set = op.open_flag & O_NONBLOCK; |
4178 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4179 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4180 | /* |
4181 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4182 | * it'll always -EAGAIN | |
4183 | */ | |
4184 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4185 | return -EAGAIN; | |
4186 | op.lookup_flags |= LOOKUP_CACHED; | |
4187 | op.open_flag |= O_NONBLOCK; | |
4188 | } | |
15b71abe | 4189 | |
4022e7af | 4190 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4191 | if (ret < 0) |
4192 | goto err; | |
4193 | ||
4194 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 4195 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
4196 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
4197 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 4198 | /* |
3a81fd02 JA |
4199 | * We could hang on to this 'fd', but seems like marginal |
4200 | * gain for something that is now known to be a slower path. | |
4201 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 4202 | */ |
3a81fd02 JA |
4203 | put_unused_fd(ret); |
4204 | return -EAGAIN; | |
4205 | } | |
4206 | ||
15b71abe JA |
4207 | if (IS_ERR(file)) { |
4208 | put_unused_fd(ret); | |
4209 | ret = PTR_ERR(file); | |
4210 | } else { | |
45d189c6 | 4211 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 4212 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
4213 | fsnotify_open(file); |
4214 | fd_install(ret, file); | |
4215 | } | |
4216 | err: | |
4217 | putname(req->open.filename); | |
8fef80bf | 4218 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4219 | if (ret < 0) |
4220 | req_set_fail_links(req); | |
e1e16097 | 4221 | io_req_complete(req, ret); |
15b71abe JA |
4222 | return 0; |
4223 | } | |
4224 | ||
45d189c6 | 4225 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4226 | { |
45d189c6 | 4227 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
4228 | } |
4229 | ||
067524e9 JA |
4230 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4231 | const struct io_uring_sqe *sqe) | |
4232 | { | |
4233 | struct io_provide_buf *p = &req->pbuf; | |
4234 | u64 tmp; | |
4235 | ||
4236 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4237 | return -EINVAL; | |
4238 | ||
4239 | tmp = READ_ONCE(sqe->fd); | |
4240 | if (!tmp || tmp > USHRT_MAX) | |
4241 | return -EINVAL; | |
4242 | ||
4243 | memset(p, 0, sizeof(*p)); | |
4244 | p->nbufs = tmp; | |
4245 | p->bgid = READ_ONCE(sqe->buf_group); | |
4246 | return 0; | |
4247 | } | |
4248 | ||
4249 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4250 | int bgid, unsigned nbufs) | |
4251 | { | |
4252 | unsigned i = 0; | |
4253 | ||
4254 | /* shouldn't happen */ | |
4255 | if (!nbufs) | |
4256 | return 0; | |
4257 | ||
4258 | /* the head kbuf is the list itself */ | |
4259 | while (!list_empty(&buf->list)) { | |
4260 | struct io_buffer *nxt; | |
4261 | ||
4262 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4263 | list_del(&nxt->list); | |
4264 | kfree(nxt); | |
4265 | if (++i == nbufs) | |
4266 | return i; | |
4267 | } | |
4268 | i++; | |
4269 | kfree(buf); | |
4270 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4271 | ||
4272 | return i; | |
4273 | } | |
4274 | ||
889fca73 | 4275 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4276 | { |
4277 | struct io_provide_buf *p = &req->pbuf; | |
4278 | struct io_ring_ctx *ctx = req->ctx; | |
4279 | struct io_buffer *head; | |
4280 | int ret = 0; | |
45d189c6 | 4281 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4282 | |
4283 | io_ring_submit_lock(ctx, !force_nonblock); | |
4284 | ||
4285 | lockdep_assert_held(&ctx->uring_lock); | |
4286 | ||
4287 | ret = -ENOENT; | |
4288 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4289 | if (head) | |
4290 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4291 | if (ret < 0) |
4292 | req_set_fail_links(req); | |
067524e9 | 4293 | |
31bff9a5 PB |
4294 | /* need to hold the lock to complete IOPOLL requests */ |
4295 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4296 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4297 | io_ring_submit_unlock(ctx, !force_nonblock); |
4298 | } else { | |
4299 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4300 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4301 | } |
067524e9 JA |
4302 | return 0; |
4303 | } | |
4304 | ||
ddf0322d JA |
4305 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4306 | const struct io_uring_sqe *sqe) | |
4307 | { | |
4308 | struct io_provide_buf *p = &req->pbuf; | |
4309 | u64 tmp; | |
4310 | ||
4311 | if (sqe->ioprio || sqe->rw_flags) | |
4312 | return -EINVAL; | |
4313 | ||
4314 | tmp = READ_ONCE(sqe->fd); | |
4315 | if (!tmp || tmp > USHRT_MAX) | |
4316 | return -E2BIG; | |
4317 | p->nbufs = tmp; | |
4318 | p->addr = READ_ONCE(sqe->addr); | |
4319 | p->len = READ_ONCE(sqe->len); | |
4320 | ||
efe68c1c | 4321 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4322 | return -EFAULT; |
4323 | ||
4324 | p->bgid = READ_ONCE(sqe->buf_group); | |
4325 | tmp = READ_ONCE(sqe->off); | |
4326 | if (tmp > USHRT_MAX) | |
4327 | return -E2BIG; | |
4328 | p->bid = tmp; | |
4329 | return 0; | |
4330 | } | |
4331 | ||
4332 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4333 | { | |
4334 | struct io_buffer *buf; | |
4335 | u64 addr = pbuf->addr; | |
4336 | int i, bid = pbuf->bid; | |
4337 | ||
4338 | for (i = 0; i < pbuf->nbufs; i++) { | |
4339 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4340 | if (!buf) | |
4341 | break; | |
4342 | ||
4343 | buf->addr = addr; | |
4344 | buf->len = pbuf->len; | |
4345 | buf->bid = bid; | |
4346 | addr += pbuf->len; | |
4347 | bid++; | |
4348 | if (!*head) { | |
4349 | INIT_LIST_HEAD(&buf->list); | |
4350 | *head = buf; | |
4351 | } else { | |
4352 | list_add_tail(&buf->list, &(*head)->list); | |
4353 | } | |
4354 | } | |
4355 | ||
4356 | return i ? i : -ENOMEM; | |
4357 | } | |
4358 | ||
889fca73 | 4359 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4360 | { |
4361 | struct io_provide_buf *p = &req->pbuf; | |
4362 | struct io_ring_ctx *ctx = req->ctx; | |
4363 | struct io_buffer *head, *list; | |
4364 | int ret = 0; | |
45d189c6 | 4365 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4366 | |
4367 | io_ring_submit_lock(ctx, !force_nonblock); | |
4368 | ||
4369 | lockdep_assert_held(&ctx->uring_lock); | |
4370 | ||
4371 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4372 | ||
4373 | ret = io_add_buffers(p, &head); | |
4374 | if (ret < 0) | |
4375 | goto out; | |
4376 | ||
4377 | if (!list) { | |
4378 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4379 | GFP_KERNEL); | |
4380 | if (ret < 0) { | |
067524e9 | 4381 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4382 | goto out; |
4383 | } | |
4384 | } | |
4385 | out: | |
ddf0322d JA |
4386 | if (ret < 0) |
4387 | req_set_fail_links(req); | |
31bff9a5 PB |
4388 | |
4389 | /* need to hold the lock to complete IOPOLL requests */ | |
4390 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4391 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4392 | io_ring_submit_unlock(ctx, !force_nonblock); |
4393 | } else { | |
4394 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4395 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4396 | } |
ddf0322d | 4397 | return 0; |
cebdb986 JA |
4398 | } |
4399 | ||
3e4827b0 JA |
4400 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4401 | const struct io_uring_sqe *sqe) | |
4402 | { | |
4403 | #if defined(CONFIG_EPOLL) | |
4404 | if (sqe->ioprio || sqe->buf_index) | |
4405 | return -EINVAL; | |
6ca56f84 | 4406 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4407 | return -EINVAL; |
3e4827b0 JA |
4408 | |
4409 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4410 | req->epoll.op = READ_ONCE(sqe->len); | |
4411 | req->epoll.fd = READ_ONCE(sqe->off); | |
4412 | ||
4413 | if (ep_op_has_event(req->epoll.op)) { | |
4414 | struct epoll_event __user *ev; | |
4415 | ||
4416 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4417 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4418 | return -EFAULT; | |
4419 | } | |
4420 | ||
4421 | return 0; | |
4422 | #else | |
4423 | return -EOPNOTSUPP; | |
4424 | #endif | |
4425 | } | |
4426 | ||
889fca73 | 4427 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4428 | { |
4429 | #if defined(CONFIG_EPOLL) | |
4430 | struct io_epoll *ie = &req->epoll; | |
4431 | int ret; | |
45d189c6 | 4432 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4433 | |
4434 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4435 | if (force_nonblock && ret == -EAGAIN) | |
4436 | return -EAGAIN; | |
4437 | ||
4438 | if (ret < 0) | |
4439 | req_set_fail_links(req); | |
889fca73 | 4440 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4441 | return 0; |
4442 | #else | |
4443 | return -EOPNOTSUPP; | |
4444 | #endif | |
4445 | } | |
4446 | ||
c1ca757b JA |
4447 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4448 | { | |
4449 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4450 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4451 | return -EINVAL; | |
3232dd02 PB |
4452 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4453 | return -EINVAL; | |
c1ca757b JA |
4454 | |
4455 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4456 | req->madvise.len = READ_ONCE(sqe->len); | |
4457 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4458 | return 0; | |
4459 | #else | |
4460 | return -EOPNOTSUPP; | |
4461 | #endif | |
4462 | } | |
4463 | ||
45d189c6 | 4464 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4465 | { |
4466 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4467 | struct io_madvise *ma = &req->madvise; | |
4468 | int ret; | |
4469 | ||
45d189c6 | 4470 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4471 | return -EAGAIN; |
4472 | ||
0726b01e | 4473 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4474 | if (ret < 0) |
4475 | req_set_fail_links(req); | |
e1e16097 | 4476 | io_req_complete(req, ret); |
c1ca757b JA |
4477 | return 0; |
4478 | #else | |
4479 | return -EOPNOTSUPP; | |
4480 | #endif | |
4481 | } | |
4482 | ||
4840e418 JA |
4483 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4484 | { | |
4485 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4486 | return -EINVAL; | |
3232dd02 PB |
4487 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4488 | return -EINVAL; | |
4840e418 JA |
4489 | |
4490 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4491 | req->fadvise.len = READ_ONCE(sqe->len); | |
4492 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4493 | return 0; | |
4494 | } | |
4495 | ||
45d189c6 | 4496 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4497 | { |
4498 | struct io_fadvise *fa = &req->fadvise; | |
4499 | int ret; | |
4500 | ||
45d189c6 | 4501 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4502 | switch (fa->advice) { |
4503 | case POSIX_FADV_NORMAL: | |
4504 | case POSIX_FADV_RANDOM: | |
4505 | case POSIX_FADV_SEQUENTIAL: | |
4506 | break; | |
4507 | default: | |
4508 | return -EAGAIN; | |
4509 | } | |
4510 | } | |
4840e418 JA |
4511 | |
4512 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4513 | if (ret < 0) | |
4514 | req_set_fail_links(req); | |
e1e16097 | 4515 | io_req_complete(req, ret); |
4840e418 JA |
4516 | return 0; |
4517 | } | |
4518 | ||
eddc7ef5 JA |
4519 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4520 | { | |
6ca56f84 | 4521 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4522 | return -EINVAL; |
eddc7ef5 JA |
4523 | if (sqe->ioprio || sqe->buf_index) |
4524 | return -EINVAL; | |
9c280f90 | 4525 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4526 | return -EBADF; |
eddc7ef5 | 4527 | |
1d9e1288 BM |
4528 | req->statx.dfd = READ_ONCE(sqe->fd); |
4529 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4530 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4531 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4532 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4533 | |
4534 | return 0; | |
4535 | } | |
4536 | ||
45d189c6 | 4537 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4538 | { |
1d9e1288 | 4539 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4540 | int ret; |
4541 | ||
45d189c6 | 4542 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4543 | /* only need file table for an actual valid fd */ |
4544 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4545 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4546 | return -EAGAIN; |
5b0bbee4 | 4547 | } |
eddc7ef5 | 4548 | |
e62753e4 BM |
4549 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4550 | ctx->buffer); | |
eddc7ef5 | 4551 | |
eddc7ef5 JA |
4552 | if (ret < 0) |
4553 | req_set_fail_links(req); | |
e1e16097 | 4554 | io_req_complete(req, ret); |
eddc7ef5 JA |
4555 | return 0; |
4556 | } | |
4557 | ||
b5dba59e JA |
4558 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4559 | { | |
14587a46 | 4560 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4561 | return -EINVAL; |
b5dba59e JA |
4562 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4563 | sqe->rw_flags || sqe->buf_index) | |
4564 | return -EINVAL; | |
9c280f90 | 4565 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4566 | return -EBADF; |
b5dba59e JA |
4567 | |
4568 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4569 | return 0; |
b5dba59e JA |
4570 | } |
4571 | ||
889fca73 | 4572 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4573 | { |
9eac1904 | 4574 | struct files_struct *files = current->files; |
3af73b28 | 4575 | struct io_close *close = &req->close; |
9eac1904 JA |
4576 | struct fdtable *fdt; |
4577 | struct file *file; | |
b5dba59e JA |
4578 | int ret; |
4579 | ||
9eac1904 JA |
4580 | file = NULL; |
4581 | ret = -EBADF; | |
4582 | spin_lock(&files->file_lock); | |
4583 | fdt = files_fdtable(files); | |
4584 | if (close->fd >= fdt->max_fds) { | |
4585 | spin_unlock(&files->file_lock); | |
4586 | goto err; | |
4587 | } | |
4588 | file = fdt->fd[close->fd]; | |
4589 | if (!file) { | |
4590 | spin_unlock(&files->file_lock); | |
4591 | goto err; | |
4592 | } | |
4593 | ||
4594 | if (file->f_op == &io_uring_fops) { | |
4595 | spin_unlock(&files->file_lock); | |
4596 | file = NULL; | |
4597 | goto err; | |
3af73b28 | 4598 | } |
b5dba59e JA |
4599 | |
4600 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4601 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4602 | spin_unlock(&files->file_lock); |
0bf0eefd | 4603 | return -EAGAIN; |
a2100672 | 4604 | } |
b5dba59e | 4605 | |
9eac1904 JA |
4606 | ret = __close_fd_get_file(close->fd, &file); |
4607 | spin_unlock(&files->file_lock); | |
4608 | if (ret < 0) { | |
4609 | if (ret == -ENOENT) | |
4610 | ret = -EBADF; | |
4611 | goto err; | |
4612 | } | |
4613 | ||
3af73b28 | 4614 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4615 | ret = filp_close(file, current->files); |
4616 | err: | |
3af73b28 PB |
4617 | if (ret < 0) |
4618 | req_set_fail_links(req); | |
9eac1904 JA |
4619 | if (file) |
4620 | fput(file); | |
889fca73 | 4621 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4622 | return 0; |
b5dba59e JA |
4623 | } |
4624 | ||
1155c76a | 4625 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4626 | { |
4627 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4628 | |
5d17b4a4 JA |
4629 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4630 | return -EINVAL; | |
4631 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4632 | return -EINVAL; | |
4633 | ||
8ed8d3c3 JA |
4634 | req->sync.off = READ_ONCE(sqe->off); |
4635 | req->sync.len = READ_ONCE(sqe->len); | |
4636 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4637 | return 0; |
4638 | } | |
4639 | ||
45d189c6 | 4640 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4641 | { |
8ed8d3c3 JA |
4642 | int ret; |
4643 | ||
ac45abc0 | 4644 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4645 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4646 | return -EAGAIN; |
4647 | ||
9adbd45d | 4648 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4649 | req->sync.flags); |
4650 | if (ret < 0) | |
4651 | req_set_fail_links(req); | |
e1e16097 | 4652 | io_req_complete(req, ret); |
5d17b4a4 JA |
4653 | return 0; |
4654 | } | |
4655 | ||
469956e8 | 4656 | #if defined(CONFIG_NET) |
02d27d89 PB |
4657 | static int io_setup_async_msg(struct io_kiocb *req, |
4658 | struct io_async_msghdr *kmsg) | |
4659 | { | |
e8c2bc1f JA |
4660 | struct io_async_msghdr *async_msg = req->async_data; |
4661 | ||
4662 | if (async_msg) | |
02d27d89 | 4663 | return -EAGAIN; |
e8c2bc1f | 4664 | if (io_alloc_async_data(req)) { |
257e84a5 | 4665 | kfree(kmsg->free_iov); |
02d27d89 PB |
4666 | return -ENOMEM; |
4667 | } | |
e8c2bc1f | 4668 | async_msg = req->async_data; |
02d27d89 | 4669 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4670 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4671 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4672 | /* if were using fast_iov, set it to the new one */ |
4673 | if (!async_msg->free_iov) | |
4674 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4675 | ||
02d27d89 PB |
4676 | return -EAGAIN; |
4677 | } | |
4678 | ||
2ae523ed PB |
4679 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4680 | struct io_async_msghdr *iomsg) | |
4681 | { | |
2ae523ed | 4682 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4683 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4684 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4685 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4686 | } |
4687 | ||
93642ef8 PB |
4688 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4689 | { | |
4690 | int ret; | |
4691 | ||
4692 | if (!io_op_defs[req->opcode].needs_async_data) | |
4693 | return 0; | |
4694 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4695 | if (!ret) | |
4696 | req->flags |= REQ_F_NEED_CLEANUP; | |
4697 | return ret; | |
4698 | } | |
4699 | ||
3529d8c2 | 4700 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4701 | { |
e47293fd | 4702 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4703 | |
d2b6f48b PB |
4704 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4705 | return -EINVAL; | |
4706 | ||
e47293fd | 4707 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4708 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4709 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4710 | |
d8768362 JA |
4711 | #ifdef CONFIG_COMPAT |
4712 | if (req->ctx->compat) | |
4713 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4714 | #endif | |
93642ef8 | 4715 | return 0; |
03b1230c JA |
4716 | } |
4717 | ||
889fca73 | 4718 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4719 | { |
6b754c8b | 4720 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4721 | struct socket *sock; |
7a7cacba | 4722 | unsigned flags; |
0fa03c62 JA |
4723 | int ret; |
4724 | ||
dba4a925 | 4725 | sock = sock_from_file(req->file); |
7a7cacba | 4726 | if (unlikely(!sock)) |
dba4a925 | 4727 | return -ENOTSOCK; |
3529d8c2 | 4728 | |
257e84a5 PB |
4729 | kmsg = req->async_data; |
4730 | if (!kmsg) { | |
7a7cacba PB |
4731 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4732 | if (ret) | |
4733 | return ret; | |
4734 | kmsg = &iomsg; | |
0fa03c62 | 4735 | } |
0fa03c62 | 4736 | |
7a7cacba PB |
4737 | flags = req->sr_msg.msg_flags; |
4738 | if (flags & MSG_DONTWAIT) | |
4739 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4740 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4741 | flags |= MSG_DONTWAIT; |
e47293fd | 4742 | |
7a7cacba | 4743 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4744 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4745 | return io_setup_async_msg(req, kmsg); |
4746 | if (ret == -ERESTARTSYS) | |
4747 | ret = -EINTR; | |
0fa03c62 | 4748 | |
257e84a5 PB |
4749 | /* fast path, check for non-NULL to avoid function call */ |
4750 | if (kmsg->free_iov) | |
4751 | kfree(kmsg->free_iov); | |
99bc4c38 | 4752 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4753 | if (ret < 0) |
4754 | req_set_fail_links(req); | |
889fca73 | 4755 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4756 | return 0; |
03b1230c | 4757 | } |
aa1fa28f | 4758 | |
889fca73 | 4759 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4760 | { |
7a7cacba PB |
4761 | struct io_sr_msg *sr = &req->sr_msg; |
4762 | struct msghdr msg; | |
4763 | struct iovec iov; | |
fddaface | 4764 | struct socket *sock; |
7a7cacba | 4765 | unsigned flags; |
fddaface JA |
4766 | int ret; |
4767 | ||
dba4a925 | 4768 | sock = sock_from_file(req->file); |
7a7cacba | 4769 | if (unlikely(!sock)) |
dba4a925 | 4770 | return -ENOTSOCK; |
fddaface | 4771 | |
7a7cacba PB |
4772 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4773 | if (unlikely(ret)) | |
14db8411 | 4774 | return ret; |
fddaface | 4775 | |
7a7cacba PB |
4776 | msg.msg_name = NULL; |
4777 | msg.msg_control = NULL; | |
4778 | msg.msg_controllen = 0; | |
4779 | msg.msg_namelen = 0; | |
fddaface | 4780 | |
7a7cacba PB |
4781 | flags = req->sr_msg.msg_flags; |
4782 | if (flags & MSG_DONTWAIT) | |
4783 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4784 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4785 | flags |= MSG_DONTWAIT; |
fddaface | 4786 | |
7a7cacba PB |
4787 | msg.msg_flags = flags; |
4788 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4789 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4790 | return -EAGAIN; |
4791 | if (ret == -ERESTARTSYS) | |
4792 | ret = -EINTR; | |
fddaface | 4793 | |
fddaface JA |
4794 | if (ret < 0) |
4795 | req_set_fail_links(req); | |
889fca73 | 4796 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4797 | return 0; |
fddaface JA |
4798 | } |
4799 | ||
1400e697 PB |
4800 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4801 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4802 | { |
4803 | struct io_sr_msg *sr = &req->sr_msg; | |
4804 | struct iovec __user *uiov; | |
4805 | size_t iov_len; | |
4806 | int ret; | |
4807 | ||
1400e697 PB |
4808 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4809 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4810 | if (ret) |
4811 | return ret; | |
4812 | ||
4813 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4814 | if (iov_len > 1) | |
4815 | return -EINVAL; | |
5476dfed | 4816 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4817 | return -EFAULT; |
5476dfed | 4818 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4819 | iomsg->free_iov = NULL; |
52de1fe1 | 4820 | } else { |
257e84a5 | 4821 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4822 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4823 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4824 | false); |
52de1fe1 JA |
4825 | if (ret > 0) |
4826 | ret = 0; | |
4827 | } | |
4828 | ||
4829 | return ret; | |
4830 | } | |
4831 | ||
4832 | #ifdef CONFIG_COMPAT | |
4833 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4834 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4835 | { |
4836 | struct compat_msghdr __user *msg_compat; | |
4837 | struct io_sr_msg *sr = &req->sr_msg; | |
4838 | struct compat_iovec __user *uiov; | |
4839 | compat_uptr_t ptr; | |
4840 | compat_size_t len; | |
4841 | int ret; | |
4842 | ||
270a5940 | 4843 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4844 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4845 | &ptr, &len); |
4846 | if (ret) | |
4847 | return ret; | |
4848 | ||
4849 | uiov = compat_ptr(ptr); | |
4850 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4851 | compat_ssize_t clen; | |
4852 | ||
4853 | if (len > 1) | |
4854 | return -EINVAL; | |
4855 | if (!access_ok(uiov, sizeof(*uiov))) | |
4856 | return -EFAULT; | |
4857 | if (__get_user(clen, &uiov->iov_len)) | |
4858 | return -EFAULT; | |
4859 | if (clen < 0) | |
4860 | return -EINVAL; | |
2d280bc8 | 4861 | sr->len = clen; |
257e84a5 | 4862 | iomsg->free_iov = NULL; |
52de1fe1 | 4863 | } else { |
257e84a5 | 4864 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4865 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4866 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4867 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4868 | if (ret < 0) |
4869 | return ret; | |
4870 | } | |
4871 | ||
4872 | return 0; | |
4873 | } | |
4874 | #endif | |
4875 | ||
1400e697 PB |
4876 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4877 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4878 | { |
1400e697 | 4879 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4880 | |
4881 | #ifdef CONFIG_COMPAT | |
4882 | if (req->ctx->compat) | |
1400e697 | 4883 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4884 | #endif |
52de1fe1 | 4885 | |
1400e697 | 4886 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4887 | } |
4888 | ||
bcda7baa | 4889 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4890 | bool needs_lock) |
bcda7baa JA |
4891 | { |
4892 | struct io_sr_msg *sr = &req->sr_msg; | |
4893 | struct io_buffer *kbuf; | |
4894 | ||
bcda7baa JA |
4895 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4896 | if (IS_ERR(kbuf)) | |
4897 | return kbuf; | |
4898 | ||
4899 | sr->kbuf = kbuf; | |
4900 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4901 | return kbuf; |
fddaface JA |
4902 | } |
4903 | ||
7fbb1b54 PB |
4904 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4905 | { | |
4906 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4907 | } | |
4908 | ||
93642ef8 | 4909 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4910 | { |
99bc4c38 | 4911 | int ret; |
3529d8c2 | 4912 | |
93642ef8 PB |
4913 | if (!io_op_defs[req->opcode].needs_async_data) |
4914 | return 0; | |
4915 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4916 | if (!ret) | |
4917 | req->flags |= REQ_F_NEED_CLEANUP; | |
4918 | return ret; | |
4919 | } | |
4920 | ||
4921 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4922 | { | |
4923 | struct io_sr_msg *sr = &req->sr_msg; | |
4924 | ||
d2b6f48b PB |
4925 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4926 | return -EINVAL; | |
4927 | ||
3529d8c2 | 4928 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4929 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4930 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4931 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4932 | |
d8768362 JA |
4933 | #ifdef CONFIG_COMPAT |
4934 | if (req->ctx->compat) | |
4935 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4936 | #endif | |
93642ef8 | 4937 | return 0; |
aa1fa28f JA |
4938 | } |
4939 | ||
889fca73 | 4940 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4941 | { |
6b754c8b | 4942 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4943 | struct socket *sock; |
7fbb1b54 | 4944 | struct io_buffer *kbuf; |
7a7cacba | 4945 | unsigned flags; |
52de1fe1 | 4946 | int ret, cflags = 0; |
45d189c6 | 4947 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4948 | |
dba4a925 | 4949 | sock = sock_from_file(req->file); |
7a7cacba | 4950 | if (unlikely(!sock)) |
dba4a925 | 4951 | return -ENOTSOCK; |
3529d8c2 | 4952 | |
257e84a5 PB |
4953 | kmsg = req->async_data; |
4954 | if (!kmsg) { | |
7a7cacba PB |
4955 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4956 | if (ret) | |
681fda8d | 4957 | return ret; |
7a7cacba PB |
4958 | kmsg = &iomsg; |
4959 | } | |
03b1230c | 4960 | |
bc02ef33 | 4961 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4962 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4963 | if (IS_ERR(kbuf)) |
52de1fe1 | 4964 | return PTR_ERR(kbuf); |
7a7cacba | 4965 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4966 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4967 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4968 | 1, req->sr_msg.len); |
4969 | } | |
52de1fe1 | 4970 | |
7a7cacba PB |
4971 | flags = req->sr_msg.msg_flags; |
4972 | if (flags & MSG_DONTWAIT) | |
4973 | req->flags |= REQ_F_NOWAIT; | |
4974 | else if (force_nonblock) | |
4975 | flags |= MSG_DONTWAIT; | |
e47293fd | 4976 | |
7a7cacba PB |
4977 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4978 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4979 | if (force_nonblock && ret == -EAGAIN) |
4980 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4981 | if (ret == -ERESTARTSYS) |
4982 | ret = -EINTR; | |
03b1230c | 4983 | |
7fbb1b54 PB |
4984 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4985 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4986 | /* fast path, check for non-NULL to avoid function call */ |
4987 | if (kmsg->free_iov) | |
4988 | kfree(kmsg->free_iov); | |
99bc4c38 | 4989 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4990 | if (ret < 0) |
4991 | req_set_fail_links(req); | |
889fca73 | 4992 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4993 | return 0; |
0fa03c62 | 4994 | } |
5d17b4a4 | 4995 | |
889fca73 | 4996 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4997 | { |
6b754c8b | 4998 | struct io_buffer *kbuf; |
7a7cacba PB |
4999 | struct io_sr_msg *sr = &req->sr_msg; |
5000 | struct msghdr msg; | |
5001 | void __user *buf = sr->buf; | |
fddaface | 5002 | struct socket *sock; |
7a7cacba PB |
5003 | struct iovec iov; |
5004 | unsigned flags; | |
bcda7baa | 5005 | int ret, cflags = 0; |
45d189c6 | 5006 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5007 | |
dba4a925 | 5008 | sock = sock_from_file(req->file); |
7a7cacba | 5009 | if (unlikely(!sock)) |
dba4a925 | 5010 | return -ENOTSOCK; |
fddaface | 5011 | |
bc02ef33 | 5012 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 5013 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
5014 | if (IS_ERR(kbuf)) |
5015 | return PTR_ERR(kbuf); | |
7a7cacba | 5016 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5017 | } |
bcda7baa | 5018 | |
7a7cacba | 5019 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5020 | if (unlikely(ret)) |
5021 | goto out_free; | |
fddaface | 5022 | |
7a7cacba PB |
5023 | msg.msg_name = NULL; |
5024 | msg.msg_control = NULL; | |
5025 | msg.msg_controllen = 0; | |
5026 | msg.msg_namelen = 0; | |
5027 | msg.msg_iocb = NULL; | |
5028 | msg.msg_flags = 0; | |
fddaface | 5029 | |
7a7cacba PB |
5030 | flags = req->sr_msg.msg_flags; |
5031 | if (flags & MSG_DONTWAIT) | |
5032 | req->flags |= REQ_F_NOWAIT; | |
5033 | else if (force_nonblock) | |
5034 | flags |= MSG_DONTWAIT; | |
5035 | ||
5036 | ret = sock_recvmsg(sock, &msg, flags); | |
5037 | if (force_nonblock && ret == -EAGAIN) | |
5038 | return -EAGAIN; | |
5039 | if (ret == -ERESTARTSYS) | |
5040 | ret = -EINTR; | |
14c32eee | 5041 | out_free: |
7fbb1b54 PB |
5042 | if (req->flags & REQ_F_BUFFER_SELECTED) |
5043 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
5044 | if (ret < 0) |
5045 | req_set_fail_links(req); | |
889fca73 | 5046 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 5047 | return 0; |
fddaface JA |
5048 | } |
5049 | ||
3529d8c2 | 5050 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5051 | { |
8ed8d3c3 JA |
5052 | struct io_accept *accept = &req->accept; |
5053 | ||
14587a46 | 5054 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5055 | return -EINVAL; |
8042d6ce | 5056 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5057 | return -EINVAL; |
5058 | ||
d55e5f5b JA |
5059 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5060 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5061 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5062 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 5063 | return 0; |
8ed8d3c3 | 5064 | } |
17f2fe35 | 5065 | |
889fca73 | 5066 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5067 | { |
5068 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5069 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5070 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
5071 | int ret; |
5072 | ||
e697deed JX |
5073 | if (req->file->f_flags & O_NONBLOCK) |
5074 | req->flags |= REQ_F_NOWAIT; | |
5075 | ||
8ed8d3c3 | 5076 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
5077 | accept->addr_len, accept->flags, |
5078 | accept->nofile); | |
8ed8d3c3 | 5079 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 5080 | return -EAGAIN; |
ac45abc0 PB |
5081 | if (ret < 0) { |
5082 | if (ret == -ERESTARTSYS) | |
5083 | ret = -EINTR; | |
4e88d6e7 | 5084 | req_set_fail_links(req); |
ac45abc0 | 5085 | } |
889fca73 | 5086 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5087 | return 0; |
8ed8d3c3 JA |
5088 | } |
5089 | ||
93642ef8 PB |
5090 | static int io_connect_prep_async(struct io_kiocb *req) |
5091 | { | |
5092 | struct io_async_connect *io = req->async_data; | |
5093 | struct io_connect *conn = &req->connect; | |
5094 | ||
5095 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5096 | } | |
5097 | ||
3529d8c2 | 5098 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5099 | { |
3529d8c2 | 5100 | struct io_connect *conn = &req->connect; |
f499a021 | 5101 | |
14587a46 | 5102 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
5103 | return -EINVAL; |
5104 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
5105 | return -EINVAL; | |
5106 | ||
3529d8c2 JA |
5107 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5108 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5109 | return 0; |
f499a021 JA |
5110 | } |
5111 | ||
889fca73 | 5112 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5113 | { |
e8c2bc1f | 5114 | struct io_async_connect __io, *io; |
f8e85cf2 | 5115 | unsigned file_flags; |
3fbb51c1 | 5116 | int ret; |
45d189c6 | 5117 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5118 | |
e8c2bc1f JA |
5119 | if (req->async_data) { |
5120 | io = req->async_data; | |
f499a021 | 5121 | } else { |
3529d8c2 JA |
5122 | ret = move_addr_to_kernel(req->connect.addr, |
5123 | req->connect.addr_len, | |
e8c2bc1f | 5124 | &__io.address); |
f499a021 JA |
5125 | if (ret) |
5126 | goto out; | |
5127 | io = &__io; | |
5128 | } | |
5129 | ||
3fbb51c1 JA |
5130 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5131 | ||
e8c2bc1f | 5132 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5133 | req->connect.addr_len, file_flags); |
87f80d62 | 5134 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5135 | if (req->async_data) |
b7bb4f7d | 5136 | return -EAGAIN; |
e8c2bc1f | 5137 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5138 | ret = -ENOMEM; |
5139 | goto out; | |
5140 | } | |
e8c2bc1f JA |
5141 | io = req->async_data; |
5142 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5143 | return -EAGAIN; |
f499a021 | 5144 | } |
f8e85cf2 JA |
5145 | if (ret == -ERESTARTSYS) |
5146 | ret = -EINTR; | |
f499a021 | 5147 | out: |
4e88d6e7 JA |
5148 | if (ret < 0) |
5149 | req_set_fail_links(req); | |
889fca73 | 5150 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5151 | return 0; |
469956e8 Y |
5152 | } |
5153 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5154 | #define IO_NETOP_FN(op) \ |
5155 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5156 | { \ | |
5157 | return -EOPNOTSUPP; \ | |
5158 | } | |
5159 | ||
5160 | #define IO_NETOP_PREP(op) \ | |
5161 | IO_NETOP_FN(op) \ | |
5162 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5163 | { \ | |
5164 | return -EOPNOTSUPP; \ | |
5165 | } \ | |
5166 | ||
5167 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5168 | IO_NETOP_PREP(op) \ | |
5169 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5170 | { \ | |
5171 | return -EOPNOTSUPP; \ | |
5172 | } | |
5173 | ||
5174 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5175 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5176 | IO_NETOP_PREP_ASYNC(connect); | |
5177 | IO_NETOP_PREP(accept); | |
5178 | IO_NETOP_FN(send); | |
5179 | IO_NETOP_FN(recv); | |
469956e8 | 5180 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5181 | |
d7718a9d JA |
5182 | struct io_poll_table { |
5183 | struct poll_table_struct pt; | |
5184 | struct io_kiocb *req; | |
5185 | int error; | |
5186 | }; | |
ce593a6c | 5187 | |
d7718a9d JA |
5188 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5189 | __poll_t mask, task_work_func_t func) | |
5190 | { | |
aa96bf8a | 5191 | int ret; |
d7718a9d JA |
5192 | |
5193 | /* for instances that support it check for an event match first: */ | |
5194 | if (mask && !(mask & poll->events)) | |
5195 | return 0; | |
5196 | ||
5197 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5198 | ||
5199 | list_del_init(&poll->wait.entry); | |
5200 | ||
d7718a9d | 5201 | req->result = mask; |
7cbf1722 | 5202 | req->task_work.func = func; |
6d816e08 JA |
5203 | percpu_ref_get(&req->ctx->refs); |
5204 | ||
d7718a9d | 5205 | /* |
e3aabf95 JA |
5206 | * If this fails, then the task is exiting. When a task exits, the |
5207 | * work gets canceled, so just cancel this request as well instead | |
5208 | * of executing it. We can't safely execute it anyway, as we may not | |
5209 | * have the needed state needed for it anyway. | |
d7718a9d | 5210 | */ |
355fb9e2 | 5211 | ret = io_req_task_work_add(req); |
aa96bf8a | 5212 | if (unlikely(ret)) { |
e3aabf95 | 5213 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 5214 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 5215 | } |
d7718a9d JA |
5216 | return 1; |
5217 | } | |
5218 | ||
74ce6ce4 JA |
5219 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5220 | __acquires(&req->ctx->completion_lock) | |
5221 | { | |
5222 | struct io_ring_ctx *ctx = req->ctx; | |
5223 | ||
5224 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5225 | struct poll_table_struct pt = { ._key = poll->events }; | |
5226 | ||
5227 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5228 | } | |
5229 | ||
5230 | spin_lock_irq(&ctx->completion_lock); | |
5231 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5232 | add_wait_queue(poll->head, &poll->wait); | |
5233 | return true; | |
5234 | } | |
5235 | ||
5236 | return false; | |
5237 | } | |
5238 | ||
d4e7cd36 | 5239 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5240 | { |
e8c2bc1f | 5241 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5242 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5243 | return req->async_data; |
d4e7cd36 JA |
5244 | return req->apoll->double_poll; |
5245 | } | |
5246 | ||
5247 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5248 | { | |
5249 | if (req->opcode == IORING_OP_POLL_ADD) | |
5250 | return &req->poll; | |
5251 | return &req->apoll->poll; | |
5252 | } | |
5253 | ||
5254 | static void io_poll_remove_double(struct io_kiocb *req) | |
5255 | { | |
5256 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5257 | |
5258 | lockdep_assert_held(&req->ctx->completion_lock); | |
5259 | ||
5260 | if (poll && poll->head) { | |
5261 | struct wait_queue_head *head = poll->head; | |
5262 | ||
5263 | spin_lock(&head->lock); | |
5264 | list_del_init(&poll->wait.entry); | |
5265 | if (poll->wait.private) | |
5266 | refcount_dec(&req->refs); | |
5267 | poll->head = NULL; | |
5268 | spin_unlock(&head->lock); | |
5269 | } | |
5270 | } | |
5271 | ||
5272 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5273 | { | |
5274 | struct io_ring_ctx *ctx = req->ctx; | |
5275 | ||
d4e7cd36 | 5276 | io_poll_remove_double(req); |
18bceab1 JA |
5277 | req->poll.done = true; |
5278 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5279 | io_commit_cqring(ctx); | |
5280 | } | |
5281 | ||
dd221f46 | 5282 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5283 | { |
dd221f46 | 5284 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5285 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5286 | struct io_kiocb *nxt; |
18bceab1 JA |
5287 | |
5288 | if (io_poll_rewait(req, &req->poll)) { | |
5289 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5290 | } else { |
5291 | hash_del(&req->hash_node); | |
5292 | io_poll_complete(req, req->result, 0); | |
5293 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5294 | |
dd221f46 PB |
5295 | nxt = io_put_req_find_next(req); |
5296 | io_cqring_ev_posted(ctx); | |
5297 | if (nxt) | |
5298 | __io_req_task_submit(nxt); | |
5299 | } | |
18bceab1 | 5300 | |
6d816e08 | 5301 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5302 | } |
5303 | ||
5304 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5305 | int sync, void *key) | |
5306 | { | |
5307 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5308 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5309 | __poll_t mask = key_to_poll(key); |
5310 | ||
5311 | /* for instances that support it check for an event match first: */ | |
5312 | if (mask && !(mask & poll->events)) | |
5313 | return 0; | |
5314 | ||
8706e04e JA |
5315 | list_del_init(&wait->entry); |
5316 | ||
807abcb0 | 5317 | if (poll && poll->head) { |
18bceab1 JA |
5318 | bool done; |
5319 | ||
807abcb0 JA |
5320 | spin_lock(&poll->head->lock); |
5321 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5322 | if (!done) |
807abcb0 | 5323 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5324 | /* make sure double remove sees this as being gone */ |
5325 | wait->private = NULL; | |
807abcb0 | 5326 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5327 | if (!done) { |
5328 | /* use wait func handler, so it matches the rq type */ | |
5329 | poll->wait.func(&poll->wait, mode, sync, key); | |
5330 | } | |
18bceab1 JA |
5331 | } |
5332 | refcount_dec(&req->refs); | |
5333 | return 1; | |
5334 | } | |
5335 | ||
5336 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5337 | wait_queue_func_t wake_func) | |
5338 | { | |
5339 | poll->head = NULL; | |
5340 | poll->done = false; | |
5341 | poll->canceled = false; | |
5342 | poll->events = events; | |
5343 | INIT_LIST_HEAD(&poll->wait.entry); | |
5344 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5345 | } | |
5346 | ||
5347 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5348 | struct wait_queue_head *head, |
5349 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5350 | { |
5351 | struct io_kiocb *req = pt->req; | |
5352 | ||
5353 | /* | |
5354 | * If poll->head is already set, it's because the file being polled | |
5355 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5356 | * for write). Setup a separate io_poll_iocb if this happens. | |
5357 | */ | |
5358 | if (unlikely(poll->head)) { | |
58852d4d PB |
5359 | struct io_poll_iocb *poll_one = poll; |
5360 | ||
18bceab1 | 5361 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5362 | if (*poll_ptr) { |
18bceab1 JA |
5363 | pt->error = -EINVAL; |
5364 | return; | |
5365 | } | |
5366 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5367 | if (!poll) { | |
5368 | pt->error = -ENOMEM; | |
5369 | return; | |
5370 | } | |
58852d4d | 5371 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5372 | refcount_inc(&req->refs); |
5373 | poll->wait.private = req; | |
807abcb0 | 5374 | *poll_ptr = poll; |
18bceab1 JA |
5375 | } |
5376 | ||
5377 | pt->error = 0; | |
5378 | poll->head = head; | |
a31eb4a2 JX |
5379 | |
5380 | if (poll->events & EPOLLEXCLUSIVE) | |
5381 | add_wait_queue_exclusive(head, &poll->wait); | |
5382 | else | |
5383 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5384 | } |
5385 | ||
5386 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5387 | struct poll_table_struct *p) | |
5388 | { | |
5389 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5390 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5391 | |
807abcb0 | 5392 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5393 | } |
5394 | ||
d7718a9d JA |
5395 | static void io_async_task_func(struct callback_head *cb) |
5396 | { | |
5397 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5398 | struct async_poll *apoll = req->apoll; | |
5399 | struct io_ring_ctx *ctx = req->ctx; | |
5400 | ||
5401 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5402 | ||
74ce6ce4 | 5403 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5404 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5405 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5406 | return; |
d7718a9d JA |
5407 | } |
5408 | ||
31067255 | 5409 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5410 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5411 | hash_del(&req->hash_node); |
2bae047e | 5412 | |
d4e7cd36 | 5413 | io_poll_remove_double(req); |
74ce6ce4 JA |
5414 | spin_unlock_irq(&ctx->completion_lock); |
5415 | ||
0be0b0e3 PB |
5416 | if (!READ_ONCE(apoll->poll.canceled)) |
5417 | __io_req_task_submit(req); | |
5418 | else | |
5419 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5420 | |
6d816e08 | 5421 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5422 | kfree(apoll->double_poll); |
31067255 | 5423 | kfree(apoll); |
d7718a9d JA |
5424 | } |
5425 | ||
5426 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5427 | void *key) | |
5428 | { | |
5429 | struct io_kiocb *req = wait->private; | |
5430 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5431 | ||
5432 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5433 | key_to_poll(key)); | |
5434 | ||
5435 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5436 | } | |
5437 | ||
5438 | static void io_poll_req_insert(struct io_kiocb *req) | |
5439 | { | |
5440 | struct io_ring_ctx *ctx = req->ctx; | |
5441 | struct hlist_head *list; | |
5442 | ||
5443 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5444 | hlist_add_head(&req->hash_node, list); | |
5445 | } | |
5446 | ||
5447 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5448 | struct io_poll_iocb *poll, | |
5449 | struct io_poll_table *ipt, __poll_t mask, | |
5450 | wait_queue_func_t wake_func) | |
5451 | __acquires(&ctx->completion_lock) | |
5452 | { | |
5453 | struct io_ring_ctx *ctx = req->ctx; | |
5454 | bool cancel = false; | |
5455 | ||
4d52f338 | 5456 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5457 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5458 | poll->file = req->file; |
18bceab1 | 5459 | poll->wait.private = req; |
d7718a9d JA |
5460 | |
5461 | ipt->pt._key = mask; | |
5462 | ipt->req = req; | |
5463 | ipt->error = -EINVAL; | |
5464 | ||
d7718a9d JA |
5465 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5466 | ||
5467 | spin_lock_irq(&ctx->completion_lock); | |
5468 | if (likely(poll->head)) { | |
5469 | spin_lock(&poll->head->lock); | |
5470 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5471 | if (ipt->error) | |
5472 | cancel = true; | |
5473 | ipt->error = 0; | |
5474 | mask = 0; | |
5475 | } | |
5476 | if (mask || ipt->error) | |
5477 | list_del_init(&poll->wait.entry); | |
5478 | else if (cancel) | |
5479 | WRITE_ONCE(poll->canceled, true); | |
5480 | else if (!poll->done) /* actually waiting for an event */ | |
5481 | io_poll_req_insert(req); | |
5482 | spin_unlock(&poll->head->lock); | |
5483 | } | |
5484 | ||
5485 | return mask; | |
5486 | } | |
5487 | ||
5488 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5489 | { | |
5490 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5491 | struct io_ring_ctx *ctx = req->ctx; | |
5492 | struct async_poll *apoll; | |
5493 | struct io_poll_table ipt; | |
5494 | __poll_t mask, ret; | |
9dab14b8 | 5495 | int rw; |
d7718a9d JA |
5496 | |
5497 | if (!req->file || !file_can_poll(req->file)) | |
5498 | return false; | |
24c74678 | 5499 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5500 | return false; |
9dab14b8 JA |
5501 | if (def->pollin) |
5502 | rw = READ; | |
5503 | else if (def->pollout) | |
5504 | rw = WRITE; | |
5505 | else | |
5506 | return false; | |
5507 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5508 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5509 | return false; |
5510 | ||
5511 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5512 | if (unlikely(!apoll)) | |
5513 | return false; | |
807abcb0 | 5514 | apoll->double_poll = NULL; |
d7718a9d JA |
5515 | |
5516 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5517 | req->apoll = apoll; |
d7718a9d | 5518 | |
8755d97a | 5519 | mask = 0; |
d7718a9d | 5520 | if (def->pollin) |
8755d97a | 5521 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5522 | if (def->pollout) |
5523 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5524 | |
5525 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5526 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5527 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5528 | mask &= ~POLLIN; | |
5529 | ||
d7718a9d JA |
5530 | mask |= POLLERR | POLLPRI; |
5531 | ||
5532 | ipt.pt._qproc = io_async_queue_proc; | |
5533 | ||
5534 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5535 | io_async_wake); | |
a36da65c | 5536 | if (ret || ipt.error) { |
d4e7cd36 | 5537 | io_poll_remove_double(req); |
d7718a9d | 5538 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5539 | kfree(apoll->double_poll); |
d7718a9d JA |
5540 | kfree(apoll); |
5541 | return false; | |
5542 | } | |
5543 | spin_unlock_irq(&ctx->completion_lock); | |
5544 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5545 | apoll->poll.events); | |
5546 | return true; | |
5547 | } | |
5548 | ||
5549 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5550 | struct io_poll_iocb *poll) | |
221c5eb2 | 5551 | { |
b41e9852 | 5552 | bool do_complete = false; |
221c5eb2 JA |
5553 | |
5554 | spin_lock(&poll->head->lock); | |
5555 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5556 | if (!list_empty(&poll->wait.entry)) { |
5557 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5558 | do_complete = true; |
221c5eb2 JA |
5559 | } |
5560 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5561 | hash_del(&req->hash_node); |
d7718a9d JA |
5562 | return do_complete; |
5563 | } | |
5564 | ||
5565 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5566 | { | |
5567 | bool do_complete; | |
5568 | ||
d4e7cd36 JA |
5569 | io_poll_remove_double(req); |
5570 | ||
d7718a9d JA |
5571 | if (req->opcode == IORING_OP_POLL_ADD) { |
5572 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5573 | } else { | |
3bfa5bcb JA |
5574 | struct async_poll *apoll = req->apoll; |
5575 | ||
d7718a9d | 5576 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5577 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5578 | if (do_complete) { | |
d7718a9d | 5579 | io_put_req(req); |
807abcb0 | 5580 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5581 | kfree(apoll); |
5582 | } | |
b1f573bd XW |
5583 | } |
5584 | ||
b41e9852 JA |
5585 | if (do_complete) { |
5586 | io_cqring_fill_event(req, -ECANCELED); | |
5587 | io_commit_cqring(req->ctx); | |
f254ac04 | 5588 | req_set_fail_links(req); |
216578e5 | 5589 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5590 | } |
5591 | ||
5592 | return do_complete; | |
221c5eb2 JA |
5593 | } |
5594 | ||
76e1b642 JA |
5595 | /* |
5596 | * Returns true if we found and killed one or more poll requests | |
5597 | */ | |
6b81928d PB |
5598 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5599 | struct files_struct *files) | |
221c5eb2 | 5600 | { |
78076bb6 | 5601 | struct hlist_node *tmp; |
221c5eb2 | 5602 | struct io_kiocb *req; |
8e2e1faf | 5603 | int posted = 0, i; |
221c5eb2 JA |
5604 | |
5605 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5606 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5607 | struct hlist_head *list; | |
5608 | ||
5609 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5610 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5611 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5612 | posted += io_poll_remove_one(req); |
5613 | } | |
221c5eb2 JA |
5614 | } |
5615 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5616 | |
8e2e1faf JA |
5617 | if (posted) |
5618 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5619 | |
5620 | return posted != 0; | |
221c5eb2 JA |
5621 | } |
5622 | ||
47f46768 JA |
5623 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5624 | { | |
78076bb6 | 5625 | struct hlist_head *list; |
47f46768 JA |
5626 | struct io_kiocb *req; |
5627 | ||
78076bb6 JA |
5628 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5629 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5630 | if (sqe_addr != req->user_data) |
5631 | continue; | |
5632 | if (io_poll_remove_one(req)) | |
eac406c6 | 5633 | return 0; |
b41e9852 | 5634 | return -EALREADY; |
47f46768 JA |
5635 | } |
5636 | ||
5637 | return -ENOENT; | |
5638 | } | |
5639 | ||
3529d8c2 JA |
5640 | static int io_poll_remove_prep(struct io_kiocb *req, |
5641 | const struct io_uring_sqe *sqe) | |
0969e783 | 5642 | { |
0969e783 JA |
5643 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5644 | return -EINVAL; | |
5645 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5646 | sqe->poll_events) | |
5647 | return -EINVAL; | |
5648 | ||
018043be | 5649 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5650 | return 0; |
5651 | } | |
5652 | ||
221c5eb2 JA |
5653 | /* |
5654 | * Find a running poll command that matches one specified in sqe->addr, | |
5655 | * and remove it if found. | |
5656 | */ | |
61e98203 | 5657 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5658 | { |
5659 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5660 | int ret; |
221c5eb2 | 5661 | |
221c5eb2 | 5662 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5663 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5664 | spin_unlock_irq(&ctx->completion_lock); |
5665 | ||
4e88d6e7 JA |
5666 | if (ret < 0) |
5667 | req_set_fail_links(req); | |
e1e16097 | 5668 | io_req_complete(req, ret); |
221c5eb2 JA |
5669 | return 0; |
5670 | } | |
5671 | ||
221c5eb2 JA |
5672 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5673 | void *key) | |
5674 | { | |
c2f2eb7d JA |
5675 | struct io_kiocb *req = wait->private; |
5676 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5677 | |
d7718a9d | 5678 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5679 | } |
5680 | ||
221c5eb2 JA |
5681 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5682 | struct poll_table_struct *p) | |
5683 | { | |
5684 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5685 | ||
e8c2bc1f | 5686 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5687 | } |
5688 | ||
3529d8c2 | 5689 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5690 | { |
5691 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5692 | u32 events; |
221c5eb2 JA |
5693 | |
5694 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5695 | return -EINVAL; | |
5696 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5697 | return -EINVAL; | |
5698 | ||
5769a351 JX |
5699 | events = READ_ONCE(sqe->poll32_events); |
5700 | #ifdef __BIG_ENDIAN | |
5701 | events = swahw32(events); | |
5702 | #endif | |
a31eb4a2 JX |
5703 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5704 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5705 | return 0; |
5706 | } | |
5707 | ||
61e98203 | 5708 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5709 | { |
5710 | struct io_poll_iocb *poll = &req->poll; | |
5711 | struct io_ring_ctx *ctx = req->ctx; | |
5712 | struct io_poll_table ipt; | |
0969e783 | 5713 | __poll_t mask; |
0969e783 | 5714 | |
d7718a9d | 5715 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5716 | |
d7718a9d JA |
5717 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5718 | io_poll_wake); | |
221c5eb2 | 5719 | |
8c838788 | 5720 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5721 | ipt.error = 0; |
b0dd8a41 | 5722 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5723 | } |
221c5eb2 JA |
5724 | spin_unlock_irq(&ctx->completion_lock); |
5725 | ||
8c838788 JA |
5726 | if (mask) { |
5727 | io_cqring_ev_posted(ctx); | |
014db007 | 5728 | io_put_req(req); |
221c5eb2 | 5729 | } |
8c838788 | 5730 | return ipt.error; |
221c5eb2 JA |
5731 | } |
5732 | ||
5262f567 JA |
5733 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5734 | { | |
ad8a48ac JA |
5735 | struct io_timeout_data *data = container_of(timer, |
5736 | struct io_timeout_data, timer); | |
5737 | struct io_kiocb *req = data->req; | |
5738 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5739 | unsigned long flags; |
5740 | ||
5262f567 | 5741 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5742 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5743 | atomic_set(&req->ctx->cq_timeouts, |
5744 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5745 | ||
78e19bbe | 5746 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5747 | io_commit_cqring(ctx); |
5748 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5749 | ||
5750 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5751 | req_set_fail_links(req); |
5262f567 JA |
5752 | io_put_req(req); |
5753 | return HRTIMER_NORESTART; | |
5754 | } | |
5755 | ||
fbd15848 PB |
5756 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5757 | __u64 user_data) | |
f254ac04 | 5758 | { |
fbd15848 | 5759 | struct io_timeout_data *io; |
47f46768 JA |
5760 | struct io_kiocb *req; |
5761 | int ret = -ENOENT; | |
f254ac04 | 5762 | |
135fcde8 | 5763 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5764 | if (user_data == req->user_data) { |
47f46768 JA |
5765 | ret = 0; |
5766 | break; | |
5767 | } | |
5768 | } | |
5769 | ||
5770 | if (ret == -ENOENT) | |
fbd15848 PB |
5771 | return ERR_PTR(ret); |
5772 | ||
5773 | io = req->async_data; | |
e8c2bc1f | 5774 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5775 | if (ret == -1) |
fbd15848 | 5776 | return ERR_PTR(-EALREADY); |
a71976f3 | 5777 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5778 | return req; |
5779 | } | |
47f46768 | 5780 | |
fbd15848 PB |
5781 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5782 | { | |
5783 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5784 | ||
5785 | if (IS_ERR(req)) | |
5786 | return PTR_ERR(req); | |
f254ac04 JA |
5787 | |
5788 | req_set_fail_links(req); | |
f254ac04 | 5789 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5790 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5791 | return 0; |
5792 | } | |
5793 | ||
9c8e11b3 PB |
5794 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5795 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5796 | { |
9c8e11b3 PB |
5797 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5798 | struct io_timeout_data *data; | |
47f46768 | 5799 | |
9c8e11b3 PB |
5800 | if (IS_ERR(req)) |
5801 | return PTR_ERR(req); | |
47f46768 | 5802 | |
9c8e11b3 PB |
5803 | req->timeout.off = 0; /* noseq */ |
5804 | data = req->async_data; | |
5805 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5806 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5807 | data->timer.function = io_timeout_fn; | |
5808 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5809 | return 0; | |
47f46768 JA |
5810 | } |
5811 | ||
3529d8c2 JA |
5812 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5813 | const struct io_uring_sqe *sqe) | |
b29472ee | 5814 | { |
9c8e11b3 PB |
5815 | struct io_timeout_rem *tr = &req->timeout_rem; |
5816 | ||
b29472ee JA |
5817 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5818 | return -EINVAL; | |
61710e43 DA |
5819 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5820 | return -EINVAL; | |
9c8e11b3 | 5821 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5822 | return -EINVAL; |
5823 | ||
9c8e11b3 PB |
5824 | tr->addr = READ_ONCE(sqe->addr); |
5825 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5826 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5827 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5828 | return -EINVAL; | |
5829 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5830 | return -EFAULT; | |
5831 | } else if (tr->flags) { | |
5832 | /* timeout removal doesn't support flags */ | |
b29472ee | 5833 | return -EINVAL; |
9c8e11b3 | 5834 | } |
b29472ee | 5835 | |
b29472ee JA |
5836 | return 0; |
5837 | } | |
5838 | ||
8662daec PB |
5839 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5840 | { | |
5841 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5842 | : HRTIMER_MODE_REL; | |
5843 | } | |
5844 | ||
11365043 JA |
5845 | /* |
5846 | * Remove or update an existing timeout command | |
5847 | */ | |
61e98203 | 5848 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5849 | { |
9c8e11b3 | 5850 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5851 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5852 | int ret; |
11365043 | 5853 | |
11365043 | 5854 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5855 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5856 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5857 | else |
5858 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5859 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5860 | |
47f46768 | 5861 | io_cqring_fill_event(req, ret); |
11365043 JA |
5862 | io_commit_cqring(ctx); |
5863 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5864 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5865 | if (ret < 0) |
5866 | req_set_fail_links(req); | |
ec9c02ad | 5867 | io_put_req(req); |
11365043 | 5868 | return 0; |
5262f567 JA |
5869 | } |
5870 | ||
3529d8c2 | 5871 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5872 | bool is_timeout_link) |
5262f567 | 5873 | { |
ad8a48ac | 5874 | struct io_timeout_data *data; |
a41525ab | 5875 | unsigned flags; |
56080b02 | 5876 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5877 | |
ad8a48ac | 5878 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5879 | return -EINVAL; |
ad8a48ac | 5880 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5881 | return -EINVAL; |
56080b02 | 5882 | if (off && is_timeout_link) |
2d28390a | 5883 | return -EINVAL; |
a41525ab JA |
5884 | flags = READ_ONCE(sqe->timeout_flags); |
5885 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5886 | return -EINVAL; |
bdf20073 | 5887 | |
bfe68a22 | 5888 | req->timeout.off = off; |
26a61679 | 5889 | |
e8c2bc1f | 5890 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5891 | return -ENOMEM; |
5892 | ||
e8c2bc1f | 5893 | data = req->async_data; |
ad8a48ac | 5894 | data->req = req; |
ad8a48ac JA |
5895 | |
5896 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5897 | return -EFAULT; |
5898 | ||
8662daec | 5899 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5900 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5901 | return 0; | |
5902 | } | |
5903 | ||
61e98203 | 5904 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5905 | { |
ad8a48ac | 5906 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5907 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5908 | struct list_head *entry; |
bfe68a22 | 5909 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5910 | |
733f5c95 | 5911 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5912 | |
5262f567 JA |
5913 | /* |
5914 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5915 | * timeout event to be satisfied. If it isn't set, then this is |
5916 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5917 | */ |
8eb7e2d0 | 5918 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5919 | entry = ctx->timeout_list.prev; |
5920 | goto add; | |
5921 | } | |
5262f567 | 5922 | |
bfe68a22 PB |
5923 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5924 | req->timeout.target_seq = tail + off; | |
5262f567 | 5925 | |
f010505b MDG |
5926 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5927 | * This is safe because ->completion_lock is held, and submissions | |
5928 | * and completions are never mixed in the same ->completion_lock section. | |
5929 | */ | |
5930 | ctx->cq_last_tm_flush = tail; | |
5931 | ||
5262f567 JA |
5932 | /* |
5933 | * Insertion sort, ensuring the first entry in the list is always | |
5934 | * the one we need first. | |
5935 | */ | |
5262f567 | 5936 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5937 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5938 | timeout.list); | |
5262f567 | 5939 | |
8eb7e2d0 | 5940 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5941 | continue; |
bfe68a22 PB |
5942 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5943 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5944 | break; |
5945 | } | |
93bd25bb | 5946 | add: |
135fcde8 | 5947 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5948 | data->timer.function = io_timeout_fn; |
5949 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5950 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5951 | return 0; |
5952 | } | |
5262f567 | 5953 | |
62755e35 JA |
5954 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5955 | { | |
5956 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5957 | ||
5958 | return req->user_data == (unsigned long) data; | |
5959 | } | |
5960 | ||
5aa75ed5 | 5961 | static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr) |
62755e35 | 5962 | { |
62755e35 | 5963 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5964 | int ret = 0; |
5965 | ||
5aa75ed5 JA |
5966 | if (!tctx->io_wq) |
5967 | return -ENOENT; | |
5968 | ||
5969 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false); | |
62755e35 JA |
5970 | switch (cancel_ret) { |
5971 | case IO_WQ_CANCEL_OK: | |
5972 | ret = 0; | |
5973 | break; | |
5974 | case IO_WQ_CANCEL_RUNNING: | |
5975 | ret = -EALREADY; | |
5976 | break; | |
5977 | case IO_WQ_CANCEL_NOTFOUND: | |
5978 | ret = -ENOENT; | |
5979 | break; | |
5980 | } | |
5981 | ||
e977d6d3 JA |
5982 | return ret; |
5983 | } | |
5984 | ||
47f46768 JA |
5985 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5986 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5987 | int success_ret) |
47f46768 JA |
5988 | { |
5989 | unsigned long flags; | |
5990 | int ret; | |
5991 | ||
5aa75ed5 JA |
5992 | ret = io_async_cancel_one(req->task->io_uring, |
5993 | (void *) (unsigned long) sqe_addr); | |
47f46768 JA |
5994 | if (ret != -ENOENT) { |
5995 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5996 | goto done; | |
5997 | } | |
5998 | ||
5999 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
6000 | ret = io_timeout_cancel(ctx, sqe_addr); | |
6001 | if (ret != -ENOENT) | |
6002 | goto done; | |
6003 | ret = io_poll_cancel(ctx, sqe_addr); | |
6004 | done: | |
b0dd8a41 JA |
6005 | if (!ret) |
6006 | ret = success_ret; | |
47f46768 JA |
6007 | io_cqring_fill_event(req, ret); |
6008 | io_commit_cqring(ctx); | |
6009 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6010 | io_cqring_ev_posted(ctx); | |
6011 | ||
4e88d6e7 JA |
6012 | if (ret < 0) |
6013 | req_set_fail_links(req); | |
014db007 | 6014 | io_put_req(req); |
47f46768 JA |
6015 | } |
6016 | ||
3529d8c2 JA |
6017 | static int io_async_cancel_prep(struct io_kiocb *req, |
6018 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6019 | { |
fbf23849 | 6020 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6021 | return -EINVAL; |
61710e43 DA |
6022 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6023 | return -EINVAL; | |
6024 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
6025 | return -EINVAL; |
6026 | ||
fbf23849 JA |
6027 | req->cancel.addr = READ_ONCE(sqe->addr); |
6028 | return 0; | |
6029 | } | |
6030 | ||
61e98203 | 6031 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6032 | { |
6033 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 6034 | |
014db007 | 6035 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
6036 | return 0; |
6037 | } | |
6038 | ||
269bbe5f | 6039 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6040 | const struct io_uring_sqe *sqe) |
6041 | { | |
6ca56f84 JA |
6042 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
6043 | return -EINVAL; | |
61710e43 DA |
6044 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6045 | return -EINVAL; | |
6046 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
6047 | return -EINVAL; |
6048 | ||
269bbe5f BM |
6049 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6050 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6051 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6052 | return -EINVAL; |
269bbe5f | 6053 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6054 | return 0; |
6055 | } | |
6056 | ||
889fca73 | 6057 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6058 | { |
6059 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 6060 | struct io_uring_rsrc_update up; |
05f3fb3c | 6061 | int ret; |
fbf23849 | 6062 | |
45d189c6 | 6063 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 6064 | return -EAGAIN; |
05f3fb3c | 6065 | |
269bbe5f BM |
6066 | up.offset = req->rsrc_update.offset; |
6067 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
6068 | |
6069 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 6070 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6071 | mutex_unlock(&ctx->uring_lock); |
6072 | ||
6073 | if (ret < 0) | |
6074 | req_set_fail_links(req); | |
889fca73 | 6075 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6076 | return 0; |
6077 | } | |
6078 | ||
bfe76559 | 6079 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6080 | { |
d625c6ee | 6081 | switch (req->opcode) { |
e781573e | 6082 | case IORING_OP_NOP: |
bfe76559 | 6083 | return 0; |
f67676d1 JA |
6084 | case IORING_OP_READV: |
6085 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6086 | case IORING_OP_READ: |
bfe76559 | 6087 | return io_read_prep(req, sqe); |
f67676d1 JA |
6088 | case IORING_OP_WRITEV: |
6089 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6090 | case IORING_OP_WRITE: |
bfe76559 | 6091 | return io_write_prep(req, sqe); |
0969e783 | 6092 | case IORING_OP_POLL_ADD: |
bfe76559 | 6093 | return io_poll_add_prep(req, sqe); |
0969e783 | 6094 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 6095 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 6096 | case IORING_OP_FSYNC: |
1155c76a | 6097 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6098 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6099 | return io_sfr_prep(req, sqe); |
03b1230c | 6100 | case IORING_OP_SENDMSG: |
fddaface | 6101 | case IORING_OP_SEND: |
bfe76559 | 6102 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6103 | case IORING_OP_RECVMSG: |
fddaface | 6104 | case IORING_OP_RECV: |
bfe76559 | 6105 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6106 | case IORING_OP_CONNECT: |
bfe76559 | 6107 | return io_connect_prep(req, sqe); |
2d28390a | 6108 | case IORING_OP_TIMEOUT: |
bfe76559 | 6109 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6110 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6111 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6112 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6113 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6114 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6115 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6116 | case IORING_OP_ACCEPT: |
bfe76559 | 6117 | return io_accept_prep(req, sqe); |
d63d1b5e | 6118 | case IORING_OP_FALLOCATE: |
bfe76559 | 6119 | return io_fallocate_prep(req, sqe); |
15b71abe | 6120 | case IORING_OP_OPENAT: |
bfe76559 | 6121 | return io_openat_prep(req, sqe); |
b5dba59e | 6122 | case IORING_OP_CLOSE: |
bfe76559 | 6123 | return io_close_prep(req, sqe); |
05f3fb3c | 6124 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6125 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6126 | case IORING_OP_STATX: |
bfe76559 | 6127 | return io_statx_prep(req, sqe); |
4840e418 | 6128 | case IORING_OP_FADVISE: |
bfe76559 | 6129 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6130 | case IORING_OP_MADVISE: |
bfe76559 | 6131 | return io_madvise_prep(req, sqe); |
cebdb986 | 6132 | case IORING_OP_OPENAT2: |
bfe76559 | 6133 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6134 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6135 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6136 | case IORING_OP_SPLICE: |
bfe76559 | 6137 | return io_splice_prep(req, sqe); |
ddf0322d | 6138 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6139 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6140 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6141 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6142 | case IORING_OP_TEE: |
bfe76559 | 6143 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6144 | case IORING_OP_SHUTDOWN: |
6145 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6146 | case IORING_OP_RENAMEAT: |
6147 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6148 | case IORING_OP_UNLINKAT: |
6149 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6150 | } |
6151 | ||
bfe76559 PB |
6152 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6153 | req->opcode); | |
6154 | return-EINVAL; | |
6155 | } | |
6156 | ||
93642ef8 | 6157 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6158 | { |
93642ef8 PB |
6159 | switch (req->opcode) { |
6160 | case IORING_OP_READV: | |
6161 | case IORING_OP_READ_FIXED: | |
6162 | case IORING_OP_READ: | |
6163 | return io_rw_prep_async(req, READ); | |
6164 | case IORING_OP_WRITEV: | |
6165 | case IORING_OP_WRITE_FIXED: | |
6166 | case IORING_OP_WRITE: | |
6167 | return io_rw_prep_async(req, WRITE); | |
6168 | case IORING_OP_SENDMSG: | |
6169 | case IORING_OP_SEND: | |
6170 | return io_sendmsg_prep_async(req); | |
6171 | case IORING_OP_RECVMSG: | |
6172 | case IORING_OP_RECV: | |
6173 | return io_recvmsg_prep_async(req); | |
6174 | case IORING_OP_CONNECT: | |
6175 | return io_connect_prep_async(req); | |
6176 | } | |
6177 | return 0; | |
6178 | } | |
6179 | ||
be7053b7 | 6180 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 6181 | { |
be7053b7 | 6182 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 6183 | return 0; |
be7053b7 | 6184 | /* some opcodes init it during the inital prep */ |
93642ef8 | 6185 | if (req->async_data) |
be7053b7 PB |
6186 | return 0; |
6187 | if (__io_alloc_async_data(req)) | |
bfe76559 | 6188 | return -EAGAIN; |
be7053b7 | 6189 | return io_req_prep_async(req); |
f67676d1 JA |
6190 | } |
6191 | ||
9cf7c104 PB |
6192 | static u32 io_get_sequence(struct io_kiocb *req) |
6193 | { | |
6194 | struct io_kiocb *pos; | |
6195 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6196 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6197 | |
f2f87370 PB |
6198 | io_for_each_link(pos, req) |
6199 | nr_reqs++; | |
9cf7c104 PB |
6200 | |
6201 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6202 | return total_submitted - nr_reqs; | |
6203 | } | |
6204 | ||
be7053b7 | 6205 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 6206 | { |
a197f664 | 6207 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6208 | struct io_defer_entry *de; |
f67676d1 | 6209 | int ret; |
9cf7c104 | 6210 | u32 seq; |
de0617e4 | 6211 | |
9d858b21 | 6212 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6213 | if (likely(list_empty_careful(&ctx->defer_list) && |
6214 | !(req->flags & REQ_F_IO_DRAIN))) | |
6215 | return 0; | |
6216 | ||
6217 | seq = io_get_sequence(req); | |
6218 | /* Still a chance to pass the sequence check */ | |
6219 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6220 | return 0; |
6221 | ||
be7053b7 PB |
6222 | ret = io_req_defer_prep(req); |
6223 | if (ret) | |
6224 | return ret; | |
cbdcb435 | 6225 | io_prep_async_link(req); |
27dc8338 PB |
6226 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6227 | if (!de) | |
6228 | return -ENOMEM; | |
2d28390a | 6229 | |
de0617e4 | 6230 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6231 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6232 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6233 | kfree(de); |
ae34817b PB |
6234 | io_queue_async_work(req); |
6235 | return -EIOCBQUEUED; | |
de0617e4 JA |
6236 | } |
6237 | ||
915967f6 | 6238 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6239 | de->req = req; |
9cf7c104 | 6240 | de->seq = seq; |
27dc8338 | 6241 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6242 | spin_unlock_irq(&ctx->completion_lock); |
6243 | return -EIOCBQUEUED; | |
6244 | } | |
6245 | ||
3ca405eb | 6246 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6247 | { |
0e1b6fe3 PB |
6248 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6249 | switch (req->opcode) { | |
6250 | case IORING_OP_READV: | |
6251 | case IORING_OP_READ_FIXED: | |
6252 | case IORING_OP_READ: | |
bcda7baa | 6253 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6254 | break; |
6255 | case IORING_OP_RECVMSG: | |
6256 | case IORING_OP_RECV: | |
bcda7baa | 6257 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6258 | break; |
6259 | } | |
6260 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6261 | } |
6262 | ||
0e1b6fe3 PB |
6263 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6264 | switch (req->opcode) { | |
6265 | case IORING_OP_READV: | |
6266 | case IORING_OP_READ_FIXED: | |
6267 | case IORING_OP_READ: | |
6268 | case IORING_OP_WRITEV: | |
6269 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6270 | case IORING_OP_WRITE: { |
6271 | struct io_async_rw *io = req->async_data; | |
6272 | if (io->free_iovec) | |
6273 | kfree(io->free_iovec); | |
0e1b6fe3 | 6274 | break; |
e8c2bc1f | 6275 | } |
0e1b6fe3 | 6276 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6277 | case IORING_OP_SENDMSG: { |
6278 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6279 | |
6280 | kfree(io->free_iov); | |
0e1b6fe3 | 6281 | break; |
e8c2bc1f | 6282 | } |
0e1b6fe3 PB |
6283 | case IORING_OP_SPLICE: |
6284 | case IORING_OP_TEE: | |
6285 | io_put_file(req, req->splice.file_in, | |
6286 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6287 | break; | |
f3cd4850 JA |
6288 | case IORING_OP_OPENAT: |
6289 | case IORING_OP_OPENAT2: | |
6290 | if (req->open.filename) | |
6291 | putname(req->open.filename); | |
6292 | break; | |
80a261fd JA |
6293 | case IORING_OP_RENAMEAT: |
6294 | putname(req->rename.oldpath); | |
6295 | putname(req->rename.newpath); | |
6296 | break; | |
14a1143b JA |
6297 | case IORING_OP_UNLINKAT: |
6298 | putname(req->unlink.filename); | |
6299 | break; | |
0e1b6fe3 PB |
6300 | } |
6301 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6302 | } |
99bc4c38 PB |
6303 | } |
6304 | ||
889fca73 | 6305 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6306 | { |
a197f664 | 6307 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6308 | int ret; |
2b188cc1 | 6309 | |
d625c6ee | 6310 | switch (req->opcode) { |
2b188cc1 | 6311 | case IORING_OP_NOP: |
889fca73 | 6312 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6313 | break; |
6314 | case IORING_OP_READV: | |
edafccee | 6315 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6316 | case IORING_OP_READ: |
889fca73 | 6317 | ret = io_read(req, issue_flags); |
edafccee | 6318 | break; |
3529d8c2 | 6319 | case IORING_OP_WRITEV: |
edafccee | 6320 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6321 | case IORING_OP_WRITE: |
889fca73 | 6322 | ret = io_write(req, issue_flags); |
2b188cc1 | 6323 | break; |
c992fe29 | 6324 | case IORING_OP_FSYNC: |
45d189c6 | 6325 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6326 | break; |
221c5eb2 | 6327 | case IORING_OP_POLL_ADD: |
61e98203 | 6328 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6329 | break; |
6330 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 6331 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 6332 | break; |
5d17b4a4 | 6333 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6334 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6335 | break; |
0fa03c62 | 6336 | case IORING_OP_SENDMSG: |
889fca73 | 6337 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6338 | break; |
fddaface | 6339 | case IORING_OP_SEND: |
889fca73 | 6340 | ret = io_send(req, issue_flags); |
0fa03c62 | 6341 | break; |
aa1fa28f | 6342 | case IORING_OP_RECVMSG: |
889fca73 | 6343 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6344 | break; |
fddaface | 6345 | case IORING_OP_RECV: |
889fca73 | 6346 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6347 | break; |
5262f567 | 6348 | case IORING_OP_TIMEOUT: |
61e98203 | 6349 | ret = io_timeout(req, issue_flags); |
5262f567 | 6350 | break; |
11365043 | 6351 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6352 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6353 | break; |
17f2fe35 | 6354 | case IORING_OP_ACCEPT: |
889fca73 | 6355 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6356 | break; |
f8e85cf2 | 6357 | case IORING_OP_CONNECT: |
889fca73 | 6358 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6359 | break; |
62755e35 | 6360 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6361 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6362 | break; |
d63d1b5e | 6363 | case IORING_OP_FALLOCATE: |
45d189c6 | 6364 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6365 | break; |
15b71abe | 6366 | case IORING_OP_OPENAT: |
45d189c6 | 6367 | ret = io_openat(req, issue_flags); |
15b71abe | 6368 | break; |
b5dba59e | 6369 | case IORING_OP_CLOSE: |
889fca73 | 6370 | ret = io_close(req, issue_flags); |
b5dba59e | 6371 | break; |
05f3fb3c | 6372 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6373 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6374 | break; |
eddc7ef5 | 6375 | case IORING_OP_STATX: |
45d189c6 | 6376 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6377 | break; |
4840e418 | 6378 | case IORING_OP_FADVISE: |
45d189c6 | 6379 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6380 | break; |
c1ca757b | 6381 | case IORING_OP_MADVISE: |
45d189c6 | 6382 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6383 | break; |
cebdb986 | 6384 | case IORING_OP_OPENAT2: |
45d189c6 | 6385 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6386 | break; |
3e4827b0 | 6387 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6388 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6389 | break; |
7d67af2c | 6390 | case IORING_OP_SPLICE: |
45d189c6 | 6391 | ret = io_splice(req, issue_flags); |
7d67af2c | 6392 | break; |
ddf0322d | 6393 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6394 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6395 | break; |
067524e9 | 6396 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6397 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6398 | break; |
f2a8d5c7 | 6399 | case IORING_OP_TEE: |
45d189c6 | 6400 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6401 | break; |
36f4fa68 | 6402 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6403 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6404 | break; |
80a261fd | 6405 | case IORING_OP_RENAMEAT: |
45d189c6 | 6406 | ret = io_renameat(req, issue_flags); |
80a261fd | 6407 | break; |
14a1143b | 6408 | case IORING_OP_UNLINKAT: |
45d189c6 | 6409 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6410 | break; |
2b188cc1 JA |
6411 | default: |
6412 | ret = -EINVAL; | |
6413 | break; | |
6414 | } | |
6415 | ||
def596e9 JA |
6416 | if (ret) |
6417 | return ret; | |
6418 | ||
b532576e JA |
6419 | /* If the op doesn't have a file, we're not polling for it */ |
6420 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6421 | const bool in_async = io_wq_current_is_worker(); |
6422 | ||
11ba820b JA |
6423 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6424 | if (in_async) | |
6425 | mutex_lock(&ctx->uring_lock); | |
6426 | ||
2e9dbe90 | 6427 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6428 | |
6429 | if (in_async) | |
6430 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6431 | } |
6432 | ||
6433 | return 0; | |
2b188cc1 JA |
6434 | } |
6435 | ||
5280f7e5 | 6436 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6437 | { |
6438 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6439 | struct io_kiocb *timeout; |
561fb04a | 6440 | int ret = 0; |
2b188cc1 | 6441 | |
6df1db6b PB |
6442 | timeout = io_prep_linked_timeout(req); |
6443 | if (timeout) | |
6444 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6445 | |
4014d943 | 6446 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6447 | ret = -ECANCELED; |
31b51510 | 6448 | |
561fb04a | 6449 | if (!ret) { |
561fb04a | 6450 | do { |
889fca73 | 6451 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6452 | /* |
6453 | * We can get EAGAIN for polled IO even though we're | |
6454 | * forcing a sync submission from here, since we can't | |
6455 | * wait for request slots on the block side. | |
6456 | */ | |
6457 | if (ret != -EAGAIN) | |
6458 | break; | |
6459 | cond_resched(); | |
6460 | } while (1); | |
6461 | } | |
31b51510 | 6462 | |
a3df7698 | 6463 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6464 | if (ret) { |
a3df7698 PB |
6465 | /* io-wq is going to take one down */ |
6466 | refcount_inc(&req->refs); | |
6467 | io_req_task_queue_fail(req, ret); | |
edafccee | 6468 | } |
2b188cc1 JA |
6469 | } |
6470 | ||
65e19f54 JA |
6471 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6472 | int index) | |
6473 | { | |
269bbe5f | 6474 | struct fixed_rsrc_table *table; |
65e19f54 | 6475 | |
05f3fb3c | 6476 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6477 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6478 | } |
6479 | ||
8371adf5 PB |
6480 | static struct file *io_file_get(struct io_submit_state *state, |
6481 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6482 | { |
a197f664 | 6483 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6484 | struct file *file; |
09bb8394 | 6485 | |
8da11c19 | 6486 | if (fixed) { |
479f517b | 6487 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6488 | return NULL; |
b7620121 | 6489 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6490 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6491 | io_set_resource_node(req); |
09bb8394 | 6492 | } else { |
c826bd7a | 6493 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6494 | file = __io_file_get(state, fd); |
09bb8394 JA |
6495 | } |
6496 | ||
ce3d5aae PB |
6497 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6498 | io_req_track_inflight(req); | |
8371adf5 | 6499 | return file; |
09bb8394 JA |
6500 | } |
6501 | ||
2665abfd | 6502 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6503 | { |
ad8a48ac JA |
6504 | struct io_timeout_data *data = container_of(timer, |
6505 | struct io_timeout_data, timer); | |
90cd7e42 | 6506 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6507 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6508 | unsigned long flags; |
2665abfd JA |
6509 | |
6510 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6511 | prev = req->timeout.head; |
6512 | req->timeout.head = NULL; | |
2665abfd JA |
6513 | |
6514 | /* | |
6515 | * We don't expect the list to be empty, that will only happen if we | |
6516 | * race with the completion of the linked work. | |
6517 | */ | |
90cd7e42 | 6518 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6519 | io_remove_next_linked(prev); |
90cd7e42 PB |
6520 | else |
6521 | prev = NULL; | |
2665abfd JA |
6522 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6523 | ||
6524 | if (prev) { | |
4e88d6e7 | 6525 | req_set_fail_links(prev); |
014db007 | 6526 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6527 | io_put_req_deferred(prev, 1); |
47f46768 | 6528 | } else { |
9ae1f8dd PB |
6529 | io_req_complete_post(req, -ETIME, 0); |
6530 | io_put_req_deferred(req, 1); | |
2665abfd | 6531 | } |
2665abfd JA |
6532 | return HRTIMER_NORESTART; |
6533 | } | |
6534 | ||
7271ef3a | 6535 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6536 | { |
76a46e06 | 6537 | /* |
f2f87370 PB |
6538 | * If the back reference is NULL, then our linked request finished |
6539 | * before we got a chance to setup the timer | |
76a46e06 | 6540 | */ |
90cd7e42 | 6541 | if (req->timeout.head) { |
e8c2bc1f | 6542 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6543 | |
ad8a48ac JA |
6544 | data->timer.function = io_link_timeout_fn; |
6545 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6546 | data->mode); | |
2665abfd | 6547 | } |
7271ef3a JA |
6548 | } |
6549 | ||
6550 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6551 | { | |
6552 | struct io_ring_ctx *ctx = req->ctx; | |
6553 | ||
6554 | spin_lock_irq(&ctx->completion_lock); | |
6555 | __io_queue_linked_timeout(req); | |
76a46e06 | 6556 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6557 | |
2665abfd | 6558 | /* drop submission reference */ |
76a46e06 JA |
6559 | io_put_req(req); |
6560 | } | |
2665abfd | 6561 | |
ad8a48ac | 6562 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6563 | { |
f2f87370 | 6564 | struct io_kiocb *nxt = req->link; |
2665abfd | 6565 | |
f2f87370 PB |
6566 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6567 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6568 | return NULL; |
2665abfd | 6569 | |
90cd7e42 | 6570 | nxt->timeout.head = req; |
900fad45 | 6571 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6572 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6573 | return nxt; |
2665abfd JA |
6574 | } |
6575 | ||
c5eef2b9 | 6576 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6577 | { |
d3d7298d | 6578 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
193155c8 | 6579 | const struct cred *old_creds = NULL; |
e0c5c576 | 6580 | int ret; |
2b188cc1 | 6581 | |
2e5aa6cb PB |
6582 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6583 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
d3d7298d PB |
6584 | req->work.identity->creds != current_cred()) |
6585 | old_creds = override_creds(req->work.identity->creds); | |
193155c8 | 6586 | |
c5eef2b9 | 6587 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6588 | |
d3d7298d PB |
6589 | if (old_creds) |
6590 | revert_creds(old_creds); | |
491381ce JA |
6591 | |
6592 | /* | |
6593 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6594 | * doesn't support non-blocking read/write attempts | |
6595 | */ | |
24c74678 | 6596 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6597 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6598 | /* |
6599 | * Queued up for async execution, worker will release | |
6600 | * submit reference when the iocb is actually submitted. | |
6601 | */ | |
6602 | io_queue_async_work(req); | |
2b188cc1 | 6603 | } |
0d63c148 PB |
6604 | } else if (likely(!ret)) { |
6605 | /* drop submission reference */ | |
e342c807 | 6606 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6607 | struct io_ring_ctx *ctx = req->ctx; |
6608 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6609 | |
6dd0be1e | 6610 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6611 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6612 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6613 | } else { |
d3d7298d | 6614 | io_put_req(req); |
0d63c148 PB |
6615 | } |
6616 | } else { | |
4e88d6e7 | 6617 | req_set_fail_links(req); |
e65ef56d | 6618 | io_put_req(req); |
e1e16097 | 6619 | io_req_complete(req, ret); |
9e645e11 | 6620 | } |
d3d7298d PB |
6621 | if (linked_timeout) |
6622 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6623 | } |
6624 | ||
be7053b7 | 6625 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6626 | { |
6627 | int ret; | |
6628 | ||
be7053b7 | 6629 | ret = io_req_defer(req); |
4fe2c963 JL |
6630 | if (ret) { |
6631 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6632 | fail_req: |
4e88d6e7 | 6633 | req_set_fail_links(req); |
e1e16097 JA |
6634 | io_put_req(req); |
6635 | io_req_complete(req, ret); | |
4fe2c963 | 6636 | } |
2550878f | 6637 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6638 | ret = io_req_defer_prep(req); |
6639 | if (unlikely(ret)) | |
6640 | goto fail_req; | |
ce35a47a JA |
6641 | io_queue_async_work(req); |
6642 | } else { | |
c5eef2b9 | 6643 | __io_queue_sqe(req); |
ce35a47a | 6644 | } |
4fe2c963 JL |
6645 | } |
6646 | ||
b16fed66 PB |
6647 | /* |
6648 | * Check SQE restrictions (opcode and flags). | |
6649 | * | |
6650 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6651 | */ | |
6652 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6653 | struct io_kiocb *req, | |
6654 | unsigned int sqe_flags) | |
4fe2c963 | 6655 | { |
b16fed66 PB |
6656 | if (!ctx->restricted) |
6657 | return true; | |
6658 | ||
6659 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6660 | return false; | |
6661 | ||
6662 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6663 | ctx->restrictions.sqe_flags_required) | |
6664 | return false; | |
6665 | ||
6666 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6667 | ctx->restrictions.sqe_flags_required)) | |
6668 | return false; | |
6669 | ||
6670 | return true; | |
4fe2c963 JL |
6671 | } |
6672 | ||
b16fed66 PB |
6673 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6674 | const struct io_uring_sqe *sqe) | |
6675 | { | |
6676 | struct io_submit_state *state; | |
6677 | unsigned int sqe_flags; | |
6678 | int id, ret = 0; | |
6679 | ||
6680 | req->opcode = READ_ONCE(sqe->opcode); | |
6681 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6682 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6683 | req->user_data = READ_ONCE(sqe->user_data); | |
6684 | req->async_data = NULL; | |
6685 | req->file = NULL; | |
6686 | req->ctx = ctx; | |
6687 | req->link = NULL; | |
6688 | req->fixed_rsrc_refs = NULL; | |
6689 | /* one is dropped after submission, the other at completion */ | |
6690 | refcount_set(&req->refs, 2); | |
6691 | req->task = current; | |
6692 | req->result = 0; | |
863e0560 | 6693 | |
b16fed66 | 6694 | /* enforce forwards compatibility on users */ |
ebf4a5db PB |
6695 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6696 | req->flags = 0; | |
b16fed66 | 6697 | return -EINVAL; |
ebf4a5db | 6698 | } |
b16fed66 PB |
6699 | |
6700 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6701 | return -EINVAL; | |
6702 | ||
6703 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) | |
6704 | return -EFAULT; | |
6705 | ||
6706 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) | |
6707 | return -EACCES; | |
6708 | ||
6709 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6710 | !io_op_defs[req->opcode].buffer_select) | |
6711 | return -EOPNOTSUPP; | |
6712 | ||
6713 | id = READ_ONCE(sqe->personality); | |
6714 | if (id) { | |
6715 | struct io_identity *iod; | |
6716 | ||
6717 | iod = idr_find(&ctx->personality_idr, id); | |
6718 | if (unlikely(!iod)) | |
6719 | return -EINVAL; | |
6720 | refcount_inc(&iod->count); | |
6721 | ||
6722 | __io_req_init_async(req); | |
6723 | get_cred(iod->creds); | |
6724 | req->work.identity = iod; | |
6725 | req->work.flags |= IO_WQ_WORK_CREDS; | |
6726 | } | |
6727 | ||
6728 | state = &ctx->submit_state; | |
6729 | ||
6730 | /* | |
6731 | * Plug now if we have more than 1 IO left after this, and the target | |
6732 | * is potentially a read/write to block based storage. | |
6733 | */ | |
6734 | if (!state->plug_started && state->ios_left > 1 && | |
6735 | io_op_defs[req->opcode].plug) { | |
6736 | blk_start_plug(&state->plug); | |
6737 | state->plug_started = true; | |
6738 | } | |
6739 | ||
6740 | if (io_op_defs[req->opcode].needs_file) { | |
6741 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6742 | ||
6743 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6744 | if (unlikely(!req->file)) | |
6745 | ret = -EBADF; | |
6746 | } | |
6747 | ||
6748 | state->ios_left--; | |
6749 | return ret; | |
6750 | } | |
6751 | ||
a6b8cadc | 6752 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6753 | const struct io_uring_sqe *sqe) |
9e645e11 | 6754 | { |
a1ab7b35 | 6755 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6756 | int ret; |
9e645e11 | 6757 | |
a6b8cadc PB |
6758 | ret = io_init_req(ctx, req, sqe); |
6759 | if (unlikely(ret)) { | |
6760 | fail_req: | |
6761 | io_put_req(req); | |
6762 | io_req_complete(req, ret); | |
de59bc10 PB |
6763 | if (link->head) { |
6764 | /* fail even hard links since we don't submit */ | |
cf109604 | 6765 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6766 | io_put_req(link->head); |
6767 | io_req_complete(link->head, -ECANCELED); | |
6768 | link->head = NULL; | |
6769 | } | |
a6b8cadc PB |
6770 | return ret; |
6771 | } | |
be7053b7 PB |
6772 | ret = io_req_prep(req, sqe); |
6773 | if (unlikely(ret)) | |
6774 | goto fail_req; | |
a6b8cadc | 6775 | |
be7053b7 | 6776 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6777 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6778 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6779 | ||
9e645e11 JA |
6780 | /* |
6781 | * If we already have a head request, queue this one for async | |
6782 | * submittal once the head completes. If we don't have a head but | |
6783 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6784 | * submitted sync once the chain is complete. If none of those | |
6785 | * conditions are true (normal request), then just queue it. | |
6786 | */ | |
863e0560 PB |
6787 | if (link->head) { |
6788 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6789 | |
8cdf2193 PB |
6790 | /* |
6791 | * Taking sequential execution of a link, draining both sides | |
6792 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6793 | * requests in the link. So, it drains the head and the | |
6794 | * next after the link request. The last one is done via | |
6795 | * drain_next flag to persist the effect across calls. | |
6796 | */ | |
ef4ff581 | 6797 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6798 | head->flags |= REQ_F_IO_DRAIN; |
6799 | ctx->drain_next = 1; | |
6800 | } | |
be7053b7 | 6801 | ret = io_req_defer_prep(req); |
cf109604 | 6802 | if (unlikely(ret)) |
a6b8cadc | 6803 | goto fail_req; |
9d76377f | 6804 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6805 | link->last->link = req; |
863e0560 | 6806 | link->last = req; |
32fe525b PB |
6807 | |
6808 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6809 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6810 | io_queue_sqe(head); |
863e0560 | 6811 | link->head = NULL; |
32fe525b | 6812 | } |
9e645e11 | 6813 | } else { |
711be031 PB |
6814 | if (unlikely(ctx->drain_next)) { |
6815 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6816 | ctx->drain_next = 0; |
711be031 | 6817 | } |
ef4ff581 | 6818 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6819 | link->head = req; |
6820 | link->last = req; | |
711be031 | 6821 | } else { |
be7053b7 | 6822 | io_queue_sqe(req); |
711be031 | 6823 | } |
9e645e11 | 6824 | } |
2e6e1fde | 6825 | |
1d4240cc | 6826 | return 0; |
9e645e11 JA |
6827 | } |
6828 | ||
9a56a232 JA |
6829 | /* |
6830 | * Batched submission is done, ensure local IO is flushed out. | |
6831 | */ | |
ba88ff11 PB |
6832 | static void io_submit_state_end(struct io_submit_state *state, |
6833 | struct io_ring_ctx *ctx) | |
9a56a232 | 6834 | { |
a1ab7b35 | 6835 | if (state->link.head) |
de59bc10 | 6836 | io_queue_sqe(state->link.head); |
6dd0be1e | 6837 | if (state->comp.nr) |
ba88ff11 | 6838 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6839 | if (state->plug_started) |
6840 | blk_finish_plug(&state->plug); | |
9f13c35b | 6841 | io_state_file_put(state); |
9a56a232 JA |
6842 | } |
6843 | ||
6844 | /* | |
6845 | * Start submission side cache. | |
6846 | */ | |
6847 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6848 | unsigned int max_ios) |
9a56a232 | 6849 | { |
27926b68 | 6850 | state->plug_started = false; |
9a56a232 | 6851 | state->ios_left = max_ios; |
a1ab7b35 PB |
6852 | /* set only head, no need to init link_last in advance */ |
6853 | state->link.head = NULL; | |
9a56a232 JA |
6854 | } |
6855 | ||
2b188cc1 JA |
6856 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6857 | { | |
75b28aff | 6858 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6859 | |
caf582c6 PB |
6860 | /* |
6861 | * Ensure any loads from the SQEs are done at this point, | |
6862 | * since once we write the new head, the application could | |
6863 | * write new data to them. | |
6864 | */ | |
6865 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6866 | } |
6867 | ||
2b188cc1 | 6868 | /* |
3529d8c2 | 6869 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6870 | * that is mapped by userspace. This means that care needs to be taken to |
6871 | * ensure that reads are stable, as we cannot rely on userspace always | |
6872 | * being a good citizen. If members of the sqe are validated and then later | |
6873 | * used, it's important that those reads are done through READ_ONCE() to | |
6874 | * prevent a re-load down the line. | |
6875 | */ | |
709b302f | 6876 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6877 | { |
75b28aff | 6878 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6879 | unsigned head; |
6880 | ||
6881 | /* | |
6882 | * The cached sq head (or cq tail) serves two purposes: | |
6883 | * | |
6884 | * 1) allows us to batch the cost of updating the user visible | |
6885 | * head updates. | |
6886 | * 2) allows the kernel side to track the head on its own, even | |
6887 | * though the application is the one updating it. | |
6888 | */ | |
4fccfcbb | 6889 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6890 | if (likely(head < ctx->sq_entries)) |
6891 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6892 | |
6893 | /* drop invalid entries */ | |
498ccd9e | 6894 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6895 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6896 | return NULL; |
6897 | } | |
6898 | ||
0f212204 | 6899 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6900 | { |
46c4e16a | 6901 | int submitted = 0; |
6c271ce2 | 6902 | |
c4a2ed72 | 6903 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6904 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6905 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6906 | return -EBUSY; |
6907 | } | |
6c271ce2 | 6908 | |
ee7d46d9 PB |
6909 | /* make sure SQ entry isn't read before tail */ |
6910 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6911 | |
2b85edfc PB |
6912 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6913 | return -EAGAIN; | |
6c271ce2 | 6914 | |
d8a6df10 | 6915 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6916 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6917 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6918 | |
46c4e16a | 6919 | while (submitted < nr) { |
3529d8c2 | 6920 | const struct io_uring_sqe *sqe; |
196be95c | 6921 | struct io_kiocb *req; |
fb5ccc98 | 6922 | |
258b29a9 | 6923 | req = io_alloc_req(ctx); |
196be95c PB |
6924 | if (unlikely(!req)) { |
6925 | if (!submitted) | |
6926 | submitted = -EAGAIN; | |
fb5ccc98 | 6927 | break; |
196be95c | 6928 | } |
4fccfcbb PB |
6929 | sqe = io_get_sqe(ctx); |
6930 | if (unlikely(!sqe)) { | |
6931 | kmem_cache_free(req_cachep, req); | |
6932 | break; | |
6933 | } | |
d3656344 JA |
6934 | /* will complete beyond this point, count as submitted */ |
6935 | submitted++; | |
a1ab7b35 | 6936 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6937 | break; |
6c271ce2 JA |
6938 | } |
6939 | ||
9466f437 PB |
6940 | if (unlikely(submitted != nr)) { |
6941 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6942 | struct io_uring_task *tctx = current->io_uring; |
6943 | int unused = nr - ref_used; | |
9466f437 | 6944 | |
d8a6df10 JA |
6945 | percpu_ref_put_many(&ctx->refs, unused); |
6946 | percpu_counter_sub(&tctx->inflight, unused); | |
6947 | put_task_struct_many(current, unused); | |
9466f437 | 6948 | } |
6c271ce2 | 6949 | |
a1ab7b35 | 6950 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6951 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6952 | io_commit_sqring(ctx); | |
6953 | ||
6c271ce2 JA |
6954 | return submitted; |
6955 | } | |
6956 | ||
23b3628e XW |
6957 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6958 | { | |
6959 | /* Tell userspace we may need a wakeup call */ | |
6960 | spin_lock_irq(&ctx->completion_lock); | |
6961 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6962 | spin_unlock_irq(&ctx->completion_lock); | |
6963 | } | |
6964 | ||
6965 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6966 | { | |
6967 | spin_lock_irq(&ctx->completion_lock); | |
6968 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6969 | spin_unlock_irq(&ctx->completion_lock); | |
6970 | } | |
6971 | ||
08369246 | 6972 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6973 | { |
c8d1ba58 | 6974 | unsigned int to_submit; |
bdcd3eab | 6975 | int ret = 0; |
6c271ce2 | 6976 | |
c8d1ba58 | 6977 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6978 | /* if we're handling multiple rings, cap submit size for fairness */ |
6979 | if (cap_entries && to_submit > 8) | |
6980 | to_submit = 8; | |
6981 | ||
906a3c6f | 6982 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6983 | unsigned nr_events = 0; |
a4c0b3de | 6984 | |
c8d1ba58 | 6985 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6986 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6987 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6988 | |
d9d05217 PB |
6989 | if (to_submit && !ctx->sqo_dead && |
6990 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6991 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6992 | mutex_unlock(&ctx->uring_lock); |
6993 | } | |
6c271ce2 | 6994 | |
90554200 JA |
6995 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6996 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6997 | |
08369246 XW |
6998 | return ret; |
6999 | } | |
6c271ce2 | 7000 | |
08369246 XW |
7001 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7002 | { | |
7003 | struct io_ring_ctx *ctx; | |
7004 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7005 | |
08369246 XW |
7006 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7007 | if (sq_thread_idle < ctx->sq_thread_idle) | |
7008 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 7009 | } |
c1edbf5f | 7010 | |
08369246 | 7011 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7012 | } |
6c271ce2 | 7013 | |
69fb2131 JA |
7014 | static void io_sqd_init_new(struct io_sq_data *sqd) |
7015 | { | |
7016 | struct io_ring_ctx *ctx; | |
7017 | ||
7018 | while (!list_empty(&sqd->ctx_new_list)) { | |
7019 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
7020 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
7021 | complete(&ctx->sq_thread_comp); | |
7022 | } | |
08369246 XW |
7023 | |
7024 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
7025 | } |
7026 | ||
c8d1ba58 JA |
7027 | static int io_sq_thread(void *data) |
7028 | { | |
91d8f519 | 7029 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
7030 | struct files_struct *old_files = current->files; |
7031 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
7032 | const struct cred *old_cred = NULL; |
7033 | struct io_sq_data *sqd = data; | |
7034 | struct io_ring_ctx *ctx; | |
a0d9205f | 7035 | unsigned long timeout = 0; |
08369246 | 7036 | DEFINE_WAIT(wait); |
6c271ce2 | 7037 | |
28cea78a JA |
7038 | task_lock(current); |
7039 | current->files = NULL; | |
7040 | current->nsproxy = NULL; | |
7041 | task_unlock(current); | |
6c271ce2 | 7042 | |
69fb2131 | 7043 | while (!kthread_should_stop()) { |
08369246 XW |
7044 | int ret; |
7045 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
7046 | |
7047 | /* | |
69fb2131 JA |
7048 | * Any changes to the sqd lists are synchronized through the |
7049 | * kthread parking. This synchronizes the thread vs users, | |
7050 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7051 | */ |
65b2b213 | 7052 | if (kthread_should_park()) { |
69fb2131 | 7053 | kthread_parkme(); |
65b2b213 XW |
7054 | /* |
7055 | * When sq thread is unparked, in case the previous park operation | |
7056 | * comes from io_put_sq_data(), which means that sq thread is going | |
7057 | * to be stopped, so here needs to have a check. | |
7058 | */ | |
7059 | if (kthread_should_stop()) | |
7060 | break; | |
7061 | } | |
7143b5ac | 7062 | |
08369246 | 7063 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7064 | io_sqd_init_new(sqd); |
08369246 XW |
7065 | timeout = jiffies + sqd->sq_thread_idle; |
7066 | } | |
6c271ce2 | 7067 | |
08369246 | 7068 | sqt_spin = false; |
e95eee2d | 7069 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7070 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7071 | if (current->cred != ctx->creds) { | |
7072 | if (old_cred) | |
7073 | revert_creds(old_cred); | |
7074 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7075 | } |
91d8f519 | 7076 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7077 | #ifdef CONFIG_AUDIT |
7078 | current->loginuid = ctx->loginuid; | |
7079 | current->sessionid = ctx->sessionid; | |
7080 | #endif | |
bdcd3eab | 7081 | |
08369246 XW |
7082 | ret = __io_sq_thread(ctx, cap_entries); |
7083 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7084 | sqt_spin = true; | |
6c271ce2 | 7085 | |
28cea78a | 7086 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7087 | } |
6c271ce2 | 7088 | |
08369246 | 7089 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7090 | io_run_task_work(); |
d434ab6d | 7091 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7092 | cond_resched(); |
08369246 XW |
7093 | if (sqt_spin) |
7094 | timeout = jiffies + sqd->sq_thread_idle; | |
7095 | continue; | |
7096 | } | |
7097 | ||
08369246 XW |
7098 | needs_sched = true; |
7099 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7100 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7101 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7102 | !list_empty_careful(&ctx->iopoll_list)) { | |
7103 | needs_sched = false; | |
7104 | break; | |
7105 | } | |
7106 | if (io_sqring_entries(ctx)) { | |
7107 | needs_sched = false; | |
7108 | break; | |
7109 | } | |
7110 | } | |
7111 | ||
8b28fdf2 | 7112 | if (needs_sched && !kthread_should_park()) { |
69fb2131 JA |
7113 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7114 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7115 | |
69fb2131 | 7116 | schedule(); |
69fb2131 JA |
7117 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7118 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7119 | } |
08369246 XW |
7120 | |
7121 | finish_wait(&sqd->wait, &wait); | |
7122 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7123 | } |
7124 | ||
4c6e277c | 7125 | io_run_task_work(); |
d434ab6d | 7126 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7127 | |
91d8f519 DZ |
7128 | if (cur_css) |
7129 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7130 | if (old_cred) |
7131 | revert_creds(old_cred); | |
06058632 | 7132 | |
28cea78a JA |
7133 | task_lock(current); |
7134 | current->files = old_files; | |
7135 | current->nsproxy = old_nsproxy; | |
7136 | task_unlock(current); | |
7137 | ||
2bbcd6d3 | 7138 | kthread_parkme(); |
06058632 | 7139 | |
6c271ce2 JA |
7140 | return 0; |
7141 | } | |
7142 | ||
bda52162 JA |
7143 | struct io_wait_queue { |
7144 | struct wait_queue_entry wq; | |
7145 | struct io_ring_ctx *ctx; | |
7146 | unsigned to_wait; | |
7147 | unsigned nr_timeouts; | |
7148 | }; | |
7149 | ||
6c503150 | 7150 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7151 | { |
7152 | struct io_ring_ctx *ctx = iowq->ctx; | |
7153 | ||
7154 | /* | |
d195a66e | 7155 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7156 | * started waiting. For timeouts, we always want to return to userspace, |
7157 | * regardless of event count. | |
7158 | */ | |
6c503150 | 7159 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7160 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7161 | } | |
7162 | ||
7163 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7164 | int wake_flags, void *key) | |
7165 | { | |
7166 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7167 | wq); | |
7168 | ||
6c503150 PB |
7169 | /* |
7170 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7171 | * the task, and the next invocation will do it. | |
7172 | */ | |
7173 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7174 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7175 | return -1; | |
bda52162 JA |
7176 | } |
7177 | ||
af9c1a44 JA |
7178 | static int io_run_task_work_sig(void) |
7179 | { | |
7180 | if (io_run_task_work()) | |
7181 | return 1; | |
7182 | if (!signal_pending(current)) | |
7183 | return 0; | |
792ee0f6 JA |
7184 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7185 | return -ERESTARTSYS; | |
af9c1a44 JA |
7186 | return -EINTR; |
7187 | } | |
7188 | ||
eeb60b9a PB |
7189 | /* when returns >0, the caller should retry */ |
7190 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7191 | struct io_wait_queue *iowq, | |
7192 | signed long *timeout) | |
7193 | { | |
7194 | int ret; | |
7195 | ||
7196 | /* make sure we run task_work before checking for signals */ | |
7197 | ret = io_run_task_work_sig(); | |
7198 | if (ret || io_should_wake(iowq)) | |
7199 | return ret; | |
7200 | /* let the caller flush overflows, retry */ | |
7201 | if (test_bit(0, &ctx->cq_check_overflow)) | |
7202 | return 1; | |
7203 | ||
7204 | *timeout = schedule_timeout(*timeout); | |
7205 | return !*timeout ? -ETIME : 1; | |
7206 | } | |
7207 | ||
2b188cc1 JA |
7208 | /* |
7209 | * Wait until events become available, if we don't already have some. The | |
7210 | * application must reap them itself, as they reside on the shared cq ring. | |
7211 | */ | |
7212 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7213 | const sigset_t __user *sig, size_t sigsz, |
7214 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7215 | { |
bda52162 JA |
7216 | struct io_wait_queue iowq = { |
7217 | .wq = { | |
7218 | .private = current, | |
7219 | .func = io_wake_function, | |
7220 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7221 | }, | |
7222 | .ctx = ctx, | |
7223 | .to_wait = min_events, | |
7224 | }; | |
75b28aff | 7225 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7226 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7227 | int ret; | |
2b188cc1 | 7228 | |
b41e9852 | 7229 | do { |
6c503150 PB |
7230 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7231 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7232 | return 0; |
4c6e277c | 7233 | if (!io_run_task_work()) |
b41e9852 | 7234 | break; |
b41e9852 | 7235 | } while (1); |
2b188cc1 JA |
7236 | |
7237 | if (sig) { | |
9e75ad5d AB |
7238 | #ifdef CONFIG_COMPAT |
7239 | if (in_compat_syscall()) | |
7240 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7241 | sigsz); |
9e75ad5d AB |
7242 | else |
7243 | #endif | |
b772434b | 7244 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7245 | |
2b188cc1 JA |
7246 | if (ret) |
7247 | return ret; | |
7248 | } | |
7249 | ||
c73ebb68 | 7250 | if (uts) { |
c1d5a224 PB |
7251 | struct timespec64 ts; |
7252 | ||
c73ebb68 HX |
7253 | if (get_timespec64(&ts, uts)) |
7254 | return -EFAULT; | |
7255 | timeout = timespec64_to_jiffies(&ts); | |
7256 | } | |
7257 | ||
bda52162 | 7258 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7259 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7260 | do { |
6c503150 | 7261 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7262 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7263 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
7264 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
7265 | finish_wait(&ctx->wait, &iowq.wq); | |
7266 | } while (ret > 0); | |
bda52162 | 7267 | |
b7db41c9 | 7268 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7269 | |
75b28aff | 7270 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7271 | } |
7272 | ||
6b06314c JA |
7273 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7274 | { | |
7275 | #if defined(CONFIG_UNIX) | |
7276 | if (ctx->ring_sock) { | |
7277 | struct sock *sock = ctx->ring_sock->sk; | |
7278 | struct sk_buff *skb; | |
7279 | ||
7280 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7281 | kfree_skb(skb); | |
7282 | } | |
7283 | #else | |
7284 | int i; | |
7285 | ||
65e19f54 JA |
7286 | for (i = 0; i < ctx->nr_user_files; i++) { |
7287 | struct file *file; | |
7288 | ||
7289 | file = io_file_from_index(ctx, i); | |
7290 | if (file) | |
7291 | fput(file); | |
7292 | } | |
6b06314c JA |
7293 | #endif |
7294 | } | |
7295 | ||
00835dce | 7296 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7297 | { |
269bbe5f | 7298 | struct fixed_rsrc_data *data; |
05f3fb3c | 7299 | |
269bbe5f | 7300 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7301 | complete(&data->done); |
7302 | } | |
7303 | ||
2a63b2d9 | 7304 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 7305 | { |
2a63b2d9 | 7306 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
7307 | } |
7308 | ||
2a63b2d9 | 7309 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 7310 | { |
2a63b2d9 BM |
7311 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
7312 | } | |
65e19f54 | 7313 | |
d67d2263 BM |
7314 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7315 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7316 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7317 | { |
2a63b2d9 | 7318 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7319 | rsrc_data->node = ref_node; |
d67d2263 | 7320 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7321 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7322 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7323 | } |
7324 | ||
8bad28d8 | 7325 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 7326 | { |
8bad28d8 | 7327 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 7328 | |
2a63b2d9 | 7329 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7330 | ref_node = data->node; |
e6cb007c | 7331 | data->node = NULL; |
2a63b2d9 | 7332 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7333 | if (ref_node) |
7334 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 | 7335 | } |
05589553 | 7336 | |
8bad28d8 HX |
7337 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7338 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
7339 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
7340 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 7341 | { |
f2303b1f | 7342 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 7343 | int ret; |
05589553 | 7344 | |
8bad28d8 HX |
7345 | if (data->quiesce) |
7346 | return -ENXIO; | |
05589553 | 7347 | |
8bad28d8 | 7348 | data->quiesce = true; |
1ffc5422 | 7349 | do { |
f2303b1f PB |
7350 | ret = -ENOMEM; |
7351 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7352 | if (!backup_node) | |
7353 | break; | |
7354 | backup_node->rsrc_data = data; | |
7355 | backup_node->rsrc_put = rsrc_put; | |
7356 | ||
8bad28d8 HX |
7357 | io_sqe_rsrc_kill_node(ctx, data); |
7358 | percpu_ref_kill(&data->refs); | |
7359 | flush_delayed_work(&ctx->rsrc_put_work); | |
7360 | ||
1ffc5422 | 7361 | ret = wait_for_completion_interruptible(&data->done); |
88f171ab | 7362 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
1ffc5422 | 7363 | break; |
8bad28d8 | 7364 | |
8bad28d8 HX |
7365 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7366 | backup_node = NULL; | |
8bad28d8 | 7367 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7368 | ret = io_run_task_work_sig(); |
8bad28d8 | 7369 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7370 | } while (ret >= 0); |
8bad28d8 | 7371 | data->quiesce = false; |
05f3fb3c | 7372 | |
8bad28d8 HX |
7373 | if (backup_node) |
7374 | destroy_fixed_rsrc_ref_node(backup_node); | |
7375 | return ret; | |
d7954b2b BM |
7376 | } |
7377 | ||
1ad555c6 BM |
7378 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7379 | { | |
7380 | struct fixed_rsrc_data *data; | |
7381 | ||
7382 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7383 | if (!data) | |
7384 | return NULL; | |
7385 | ||
00835dce | 7386 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7387 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7388 | kfree(data); | |
7389 | return NULL; | |
7390 | } | |
7391 | data->ctx = ctx; | |
7392 | init_completion(&data->done); | |
7393 | return data; | |
7394 | } | |
7395 | ||
7396 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7397 | { | |
7398 | percpu_ref_exit(&data->refs); | |
7399 | kfree(data->table); | |
7400 | kfree(data); | |
7401 | } | |
7402 | ||
d7954b2b BM |
7403 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7404 | { | |
7405 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7406 | unsigned nr_tables, i; |
7407 | int ret; | |
7408 | ||
8bad28d8 HX |
7409 | /* |
7410 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7411 | * Since we possibly drop uring lock later in this function to | |
7412 | * run task work. | |
7413 | */ | |
7414 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7415 | return -ENXIO; |
f2303b1f | 7416 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7417 | if (ret) |
7418 | return ret; | |
7419 | ||
6b06314c | 7420 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7421 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7422 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7423 | kfree(data->table[i].files); |
1ad555c6 | 7424 | free_fixed_rsrc_data(data); |
05f3fb3c | 7425 | ctx->file_data = NULL; |
6b06314c JA |
7426 | ctx->nr_user_files = 0; |
7427 | return 0; | |
7428 | } | |
7429 | ||
534ca6d6 | 7430 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7431 | { |
534ca6d6 | 7432 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7433 | /* |
7434 | * The park is a bit of a work-around, without it we get | |
7435 | * warning spews on shutdown with SQPOLL set and affinity | |
7436 | * set to a single CPU. | |
7437 | */ | |
534ca6d6 JA |
7438 | if (sqd->thread) { |
7439 | kthread_park(sqd->thread); | |
7440 | kthread_stop(sqd->thread); | |
7441 | } | |
7442 | ||
7443 | kfree(sqd); | |
7444 | } | |
7445 | } | |
7446 | ||
aa06165d JA |
7447 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7448 | { | |
7449 | struct io_ring_ctx *ctx_attach; | |
7450 | struct io_sq_data *sqd; | |
7451 | struct fd f; | |
7452 | ||
7453 | f = fdget(p->wq_fd); | |
7454 | if (!f.file) | |
7455 | return ERR_PTR(-ENXIO); | |
7456 | if (f.file->f_op != &io_uring_fops) { | |
7457 | fdput(f); | |
7458 | return ERR_PTR(-EINVAL); | |
7459 | } | |
7460 | ||
7461 | ctx_attach = f.file->private_data; | |
7462 | sqd = ctx_attach->sq_data; | |
7463 | if (!sqd) { | |
7464 | fdput(f); | |
7465 | return ERR_PTR(-EINVAL); | |
7466 | } | |
7467 | ||
7468 | refcount_inc(&sqd->refs); | |
7469 | fdput(f); | |
7470 | return sqd; | |
7471 | } | |
7472 | ||
534ca6d6 JA |
7473 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7474 | { | |
7475 | struct io_sq_data *sqd; | |
7476 | ||
aa06165d JA |
7477 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7478 | return io_attach_sq_data(p); | |
7479 | ||
534ca6d6 JA |
7480 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7481 | if (!sqd) | |
7482 | return ERR_PTR(-ENOMEM); | |
7483 | ||
7484 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7485 | INIT_LIST_HEAD(&sqd->ctx_list); |
7486 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7487 | mutex_init(&sqd->ctx_lock); | |
7488 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7489 | init_waitqueue_head(&sqd->wait); |
7490 | return sqd; | |
7491 | } | |
7492 | ||
69fb2131 JA |
7493 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7494 | __releases(&sqd->lock) | |
7495 | { | |
7496 | if (!sqd->thread) | |
7497 | return; | |
7498 | kthread_unpark(sqd->thread); | |
7499 | mutex_unlock(&sqd->lock); | |
7500 | } | |
7501 | ||
7502 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7503 | __acquires(&sqd->lock) | |
7504 | { | |
7505 | if (!sqd->thread) | |
7506 | return; | |
7507 | mutex_lock(&sqd->lock); | |
7508 | kthread_park(sqd->thread); | |
7509 | } | |
7510 | ||
534ca6d6 JA |
7511 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7512 | { | |
7513 | struct io_sq_data *sqd = ctx->sq_data; | |
7514 | ||
7515 | if (sqd) { | |
7516 | if (sqd->thread) { | |
7517 | /* | |
7518 | * We may arrive here from the error branch in | |
7519 | * io_sq_offload_create() where the kthread is created | |
7520 | * without being waked up, thus wake it up now to make | |
7521 | * sure the wait will complete. | |
7522 | */ | |
7523 | wake_up_process(sqd->thread); | |
7524 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7525 | |
7526 | io_sq_thread_park(sqd); | |
7527 | } | |
7528 | ||
7529 | mutex_lock(&sqd->ctx_lock); | |
7530 | list_del(&ctx->sqd_list); | |
08369246 | 7531 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7532 | mutex_unlock(&sqd->ctx_lock); |
7533 | ||
08369246 | 7534 | if (sqd->thread) |
69fb2131 | 7535 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7536 | |
7537 | io_put_sq_data(sqd); | |
7538 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7539 | } |
7540 | } | |
7541 | ||
6b06314c | 7542 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7543 | /* |
7544 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7545 | * the io_uring can be safely unregistered on process exit, even if we have | |
7546 | * loops in the file referencing. | |
7547 | */ | |
7548 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7549 | { | |
7550 | struct sock *sk = ctx->ring_sock->sk; | |
7551 | struct scm_fp_list *fpl; | |
7552 | struct sk_buff *skb; | |
08a45173 | 7553 | int i, nr_files; |
6b06314c | 7554 | |
6b06314c JA |
7555 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7556 | if (!fpl) | |
7557 | return -ENOMEM; | |
7558 | ||
7559 | skb = alloc_skb(0, GFP_KERNEL); | |
7560 | if (!skb) { | |
7561 | kfree(fpl); | |
7562 | return -ENOMEM; | |
7563 | } | |
7564 | ||
7565 | skb->sk = sk; | |
6b06314c | 7566 | |
08a45173 | 7567 | nr_files = 0; |
6b06314c JA |
7568 | fpl->user = get_uid(ctx->user); |
7569 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7570 | struct file *file = io_file_from_index(ctx, i + offset); |
7571 | ||
7572 | if (!file) | |
08a45173 | 7573 | continue; |
65e19f54 | 7574 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7575 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7576 | nr_files++; | |
6b06314c JA |
7577 | } |
7578 | ||
08a45173 JA |
7579 | if (nr_files) { |
7580 | fpl->max = SCM_MAX_FD; | |
7581 | fpl->count = nr_files; | |
7582 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7583 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7584 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7585 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7586 | |
08a45173 JA |
7587 | for (i = 0; i < nr_files; i++) |
7588 | fput(fpl->fp[i]); | |
7589 | } else { | |
7590 | kfree_skb(skb); | |
7591 | kfree(fpl); | |
7592 | } | |
6b06314c JA |
7593 | |
7594 | return 0; | |
7595 | } | |
7596 | ||
7597 | /* | |
7598 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7599 | * causes regular reference counting to break down. We rely on the UNIX | |
7600 | * garbage collection to take care of this problem for us. | |
7601 | */ | |
7602 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7603 | { | |
7604 | unsigned left, total; | |
7605 | int ret = 0; | |
7606 | ||
7607 | total = 0; | |
7608 | left = ctx->nr_user_files; | |
7609 | while (left) { | |
7610 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7611 | |
7612 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7613 | if (ret) | |
7614 | break; | |
7615 | left -= this_files; | |
7616 | total += this_files; | |
7617 | } | |
7618 | ||
7619 | if (!ret) | |
7620 | return 0; | |
7621 | ||
7622 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7623 | struct file *file = io_file_from_index(ctx, total); |
7624 | ||
7625 | if (file) | |
7626 | fput(file); | |
6b06314c JA |
7627 | total++; |
7628 | } | |
7629 | ||
7630 | return ret; | |
7631 | } | |
7632 | #else | |
7633 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7634 | { | |
7635 | return 0; | |
7636 | } | |
7637 | #endif | |
7638 | ||
269bbe5f | 7639 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7640 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7641 | { |
7642 | int i; | |
7643 | ||
7644 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7645 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7646 | unsigned this_files; |
7647 | ||
7648 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7649 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7650 | GFP_KERNEL); | |
7651 | if (!table->files) | |
7652 | break; | |
7653 | nr_files -= this_files; | |
7654 | } | |
7655 | ||
7656 | if (i == nr_tables) | |
7657 | return 0; | |
7658 | ||
7659 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7660 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7661 | kfree(table->files); |
7662 | } | |
7663 | return 1; | |
7664 | } | |
7665 | ||
50238531 | 7666 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7667 | { |
50238531 | 7668 | struct file *file = prsrc->file; |
05f3fb3c JA |
7669 | #if defined(CONFIG_UNIX) |
7670 | struct sock *sock = ctx->ring_sock->sk; | |
7671 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7672 | struct sk_buff *skb; | |
7673 | int i; | |
7674 | ||
7675 | __skb_queue_head_init(&list); | |
7676 | ||
7677 | /* | |
7678 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7679 | * remove this entry and rearrange the file array. | |
7680 | */ | |
7681 | skb = skb_dequeue(head); | |
7682 | while (skb) { | |
7683 | struct scm_fp_list *fp; | |
7684 | ||
7685 | fp = UNIXCB(skb).fp; | |
7686 | for (i = 0; i < fp->count; i++) { | |
7687 | int left; | |
7688 | ||
7689 | if (fp->fp[i] != file) | |
7690 | continue; | |
7691 | ||
7692 | unix_notinflight(fp->user, fp->fp[i]); | |
7693 | left = fp->count - 1 - i; | |
7694 | if (left) { | |
7695 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7696 | left * sizeof(struct file *)); | |
7697 | } | |
7698 | fp->count--; | |
7699 | if (!fp->count) { | |
7700 | kfree_skb(skb); | |
7701 | skb = NULL; | |
7702 | } else { | |
7703 | __skb_queue_tail(&list, skb); | |
7704 | } | |
7705 | fput(file); | |
7706 | file = NULL; | |
7707 | break; | |
7708 | } | |
7709 | ||
7710 | if (!file) | |
7711 | break; | |
7712 | ||
7713 | __skb_queue_tail(&list, skb); | |
7714 | ||
7715 | skb = skb_dequeue(head); | |
7716 | } | |
7717 | ||
7718 | if (skb_peek(&list)) { | |
7719 | spin_lock_irq(&head->lock); | |
7720 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7721 | __skb_queue_tail(head, skb); | |
7722 | spin_unlock_irq(&head->lock); | |
7723 | } | |
7724 | #else | |
7725 | fput(file); | |
7726 | #endif | |
7727 | } | |
7728 | ||
269bbe5f | 7729 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7730 | { |
269bbe5f BM |
7731 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7732 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7733 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7734 | |
269bbe5f BM |
7735 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7736 | list_del(&prsrc->list); | |
50238531 | 7737 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7738 | kfree(prsrc); |
65e19f54 | 7739 | } |
05589553 | 7740 | |
05589553 XW |
7741 | percpu_ref_exit(&ref_node->refs); |
7742 | kfree(ref_node); | |
269bbe5f | 7743 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7744 | } |
65e19f54 | 7745 | |
269bbe5f | 7746 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7747 | { |
7748 | struct io_ring_ctx *ctx; | |
7749 | struct llist_node *node; | |
7750 | ||
269bbe5f BM |
7751 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7752 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7753 | |
7754 | while (node) { | |
269bbe5f | 7755 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7756 | struct llist_node *next = node->next; |
7757 | ||
269bbe5f BM |
7758 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7759 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7760 | node = next; |
7761 | } | |
7762 | } | |
7763 | ||
ea64ec02 PB |
7764 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7765 | unsigned i) | |
2faf852d | 7766 | { |
ea64ec02 PB |
7767 | struct fixed_rsrc_table *table; |
7768 | ||
7769 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7770 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7771 | } | |
7772 | ||
00835dce | 7773 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7774 | { |
269bbe5f BM |
7775 | struct fixed_rsrc_ref_node *ref_node; |
7776 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7777 | struct io_ring_ctx *ctx; |
e297822b | 7778 | bool first_add = false; |
4a38aed2 | 7779 | int delay = HZ; |
65e19f54 | 7780 | |
269bbe5f BM |
7781 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7782 | data = ref_node->rsrc_data; | |
e297822b PB |
7783 | ctx = data->ctx; |
7784 | ||
2a63b2d9 | 7785 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7786 | ref_node->done = true; |
7787 | ||
d67d2263 BM |
7788 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7789 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7790 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7791 | /* recycle ref nodes in order */ |
7792 | if (!ref_node->done) | |
7793 | break; | |
7794 | list_del(&ref_node->node); | |
269bbe5f | 7795 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7796 | } |
2a63b2d9 | 7797 | io_rsrc_ref_unlock(ctx); |
05589553 | 7798 | |
e297822b | 7799 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7800 | delay = 0; |
05589553 | 7801 | |
4a38aed2 | 7802 | if (!delay) |
269bbe5f | 7803 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7804 | else if (first_add) |
269bbe5f | 7805 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7806 | } |
65e19f54 | 7807 | |
6802535d | 7808 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7809 | struct io_ring_ctx *ctx) |
05f3fb3c | 7810 | { |
269bbe5f | 7811 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7812 | |
05589553 XW |
7813 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7814 | if (!ref_node) | |
3e2224c5 | 7815 | return NULL; |
05f3fb3c | 7816 | |
00835dce | 7817 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7818 | 0, GFP_KERNEL)) { |
7819 | kfree(ref_node); | |
3e2224c5 | 7820 | return NULL; |
05589553 XW |
7821 | } |
7822 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7823 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7824 | ref_node->done = false; |
05589553 | 7825 | return ref_node; |
05589553 XW |
7826 | } |
7827 | ||
bc9744cd PB |
7828 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7829 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7830 | { |
269bbe5f | 7831 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7832 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7833 | } |
7834 | ||
269bbe5f | 7835 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7836 | { |
7837 | percpu_ref_exit(&ref_node->refs); | |
7838 | kfree(ref_node); | |
65e19f54 JA |
7839 | } |
7840 | ||
ea64ec02 | 7841 | |
6b06314c JA |
7842 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7843 | unsigned nr_args) | |
7844 | { | |
7845 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7846 | unsigned nr_tables, i; |
05f3fb3c | 7847 | struct file *file; |
600cf3f8 | 7848 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7849 | struct fixed_rsrc_ref_node *ref_node; |
7850 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7851 | |
05f3fb3c | 7852 | if (ctx->file_data) |
6b06314c JA |
7853 | return -EBUSY; |
7854 | if (!nr_args) | |
7855 | return -EINVAL; | |
7856 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7857 | return -EMFILE; | |
7858 | ||
1ad555c6 | 7859 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7860 | if (!file_data) |
05f3fb3c | 7861 | return -ENOMEM; |
13770a71 | 7862 | ctx->file_data = file_data; |
05f3fb3c | 7863 | |
65e19f54 | 7864 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7865 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7866 | GFP_KERNEL); |
600cf3f8 PB |
7867 | if (!file_data->table) |
7868 | goto out_free; | |
05f3fb3c | 7869 | |
600cf3f8 | 7870 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7871 | goto out_free; |
65e19f54 | 7872 | |
08a45173 | 7873 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7874 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7875 | ret = -EFAULT; | |
7876 | goto out_fput; | |
7877 | } | |
08a45173 | 7878 | /* allow sparse sets */ |
600cf3f8 | 7879 | if (fd == -1) |
08a45173 | 7880 | continue; |
6b06314c | 7881 | |
05f3fb3c | 7882 | file = fget(fd); |
6b06314c | 7883 | ret = -EBADF; |
05f3fb3c | 7884 | if (!file) |
600cf3f8 | 7885 | goto out_fput; |
05f3fb3c | 7886 | |
6b06314c JA |
7887 | /* |
7888 | * Don't allow io_uring instances to be registered. If UNIX | |
7889 | * isn't enabled, then this causes a reference cycle and this | |
7890 | * instance can never get freed. If UNIX is enabled we'll | |
7891 | * handle it just fine, but there's still no point in allowing | |
7892 | * a ring fd as it doesn't support regular read/write anyway. | |
7893 | */ | |
05f3fb3c JA |
7894 | if (file->f_op == &io_uring_fops) { |
7895 | fput(file); | |
600cf3f8 | 7896 | goto out_fput; |
6b06314c | 7897 | } |
ea64ec02 | 7898 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7899 | } |
7900 | ||
6b06314c | 7901 | ret = io_sqe_files_scm(ctx); |
05589553 | 7902 | if (ret) { |
6b06314c | 7903 | io_sqe_files_unregister(ctx); |
05589553 XW |
7904 | return ret; |
7905 | } | |
6b06314c | 7906 | |
bc9744cd | 7907 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7908 | if (!ref_node) { |
05589553 | 7909 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7910 | return -ENOMEM; |
05589553 | 7911 | } |
bc9744cd | 7912 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7913 | |
d67d2263 | 7914 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7915 | return ret; |
600cf3f8 PB |
7916 | out_fput: |
7917 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7918 | file = io_file_from_index(ctx, i); | |
7919 | if (file) | |
7920 | fput(file); | |
7921 | } | |
7922 | for (i = 0; i < nr_tables; i++) | |
7923 | kfree(file_data->table[i].files); | |
7924 | ctx->nr_user_files = 0; | |
600cf3f8 | 7925 | out_free: |
1ad555c6 | 7926 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7927 | ctx->file_data = NULL; |
6b06314c JA |
7928 | return ret; |
7929 | } | |
7930 | ||
c3a31e60 JA |
7931 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7932 | int index) | |
7933 | { | |
7934 | #if defined(CONFIG_UNIX) | |
7935 | struct sock *sock = ctx->ring_sock->sk; | |
7936 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7937 | struct sk_buff *skb; | |
7938 | ||
7939 | /* | |
7940 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7941 | * file set. If there's no room, fall back to allocating a new skb | |
7942 | * and filling it in. | |
7943 | */ | |
7944 | spin_lock_irq(&head->lock); | |
7945 | skb = skb_peek(head); | |
7946 | if (skb) { | |
7947 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7948 | ||
7949 | if (fpl->count < SCM_MAX_FD) { | |
7950 | __skb_unlink(skb, head); | |
7951 | spin_unlock_irq(&head->lock); | |
7952 | fpl->fp[fpl->count] = get_file(file); | |
7953 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7954 | fpl->count++; | |
7955 | spin_lock_irq(&head->lock); | |
7956 | __skb_queue_head(head, skb); | |
7957 | } else { | |
7958 | skb = NULL; | |
7959 | } | |
7960 | } | |
7961 | spin_unlock_irq(&head->lock); | |
7962 | ||
7963 | if (skb) { | |
7964 | fput(file); | |
7965 | return 0; | |
7966 | } | |
7967 | ||
7968 | return __io_sqe_files_scm(ctx, 1, index); | |
7969 | #else | |
7970 | return 0; | |
7971 | #endif | |
7972 | } | |
7973 | ||
50238531 | 7974 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7975 | { |
269bbe5f BM |
7976 | struct io_rsrc_put *prsrc; |
7977 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7978 | |
269bbe5f BM |
7979 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7980 | if (!prsrc) | |
a5318d3c | 7981 | return -ENOMEM; |
05f3fb3c | 7982 | |
50238531 | 7983 | prsrc->rsrc = rsrc; |
269bbe5f | 7984 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7985 | |
a5318d3c | 7986 | return 0; |
05f3fb3c JA |
7987 | } |
7988 | ||
269bbe5f BM |
7989 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7990 | struct file *file) | |
7991 | { | |
50238531 | 7992 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7993 | } |
7994 | ||
05f3fb3c | 7995 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7996 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7997 | unsigned nr_args) |
7998 | { | |
269bbe5f BM |
7999 | struct fixed_rsrc_data *data = ctx->file_data; |
8000 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 8001 | struct file *file, **file_slot; |
c3a31e60 JA |
8002 | __s32 __user *fds; |
8003 | int fd, i, err; | |
8004 | __u32 done; | |
05589553 | 8005 | bool needs_switch = false; |
c3a31e60 | 8006 | |
05f3fb3c | 8007 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
8008 | return -EOVERFLOW; |
8009 | if (done > ctx->nr_user_files) | |
8010 | return -EINVAL; | |
8011 | ||
bc9744cd | 8012 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
8013 | if (!ref_node) |
8014 | return -ENOMEM; | |
bc9744cd | 8015 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 8016 | |
269bbe5f | 8017 | fds = u64_to_user_ptr(up->data); |
67973b93 | 8018 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
8019 | err = 0; |
8020 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
8021 | err = -EFAULT; | |
8022 | break; | |
8023 | } | |
4e0377a1 | 8024 | if (fd == IORING_REGISTER_FILES_SKIP) |
8025 | continue; | |
8026 | ||
67973b93 | 8027 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
8028 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
8029 | ||
8030 | if (*file_slot) { | |
8031 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
8032 | if (err) |
8033 | break; | |
ea64ec02 | 8034 | *file_slot = NULL; |
05589553 | 8035 | needs_switch = true; |
c3a31e60 JA |
8036 | } |
8037 | if (fd != -1) { | |
c3a31e60 JA |
8038 | file = fget(fd); |
8039 | if (!file) { | |
8040 | err = -EBADF; | |
8041 | break; | |
8042 | } | |
8043 | /* | |
8044 | * Don't allow io_uring instances to be registered. If | |
8045 | * UNIX isn't enabled, then this causes a reference | |
8046 | * cycle and this instance can never get freed. If UNIX | |
8047 | * is enabled we'll handle it just fine, but there's | |
8048 | * still no point in allowing a ring fd as it doesn't | |
8049 | * support regular read/write anyway. | |
8050 | */ | |
8051 | if (file->f_op == &io_uring_fops) { | |
8052 | fput(file); | |
8053 | err = -EBADF; | |
8054 | break; | |
8055 | } | |
e68a3ff8 | 8056 | *file_slot = file; |
c3a31e60 | 8057 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8058 | if (err) { |
e68a3ff8 | 8059 | *file_slot = NULL; |
f3bd9dae | 8060 | fput(file); |
c3a31e60 | 8061 | break; |
f3bd9dae | 8062 | } |
c3a31e60 | 8063 | } |
05f3fb3c JA |
8064 | } |
8065 | ||
05589553 | 8066 | if (needs_switch) { |
b2e96852 | 8067 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 8068 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 8069 | } else |
269bbe5f | 8070 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
8071 | |
8072 | return done ? done : err; | |
8073 | } | |
05589553 | 8074 | |
05f3fb3c JA |
8075 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
8076 | unsigned nr_args) | |
8077 | { | |
269bbe5f | 8078 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
8079 | |
8080 | if (!ctx->file_data) | |
8081 | return -ENXIO; | |
8082 | if (!nr_args) | |
8083 | return -EINVAL; | |
8084 | if (copy_from_user(&up, arg, sizeof(up))) | |
8085 | return -EFAULT; | |
8086 | if (up.resv) | |
8087 | return -EINVAL; | |
8088 | ||
8089 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8090 | } | |
c3a31e60 | 8091 | |
5280f7e5 | 8092 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
8093 | { |
8094 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8095 | ||
5280f7e5 PB |
8096 | req = io_put_req_find_next(req); |
8097 | return req ? &req->work : NULL; | |
7d723065 JA |
8098 | } |
8099 | ||
5aa75ed5 | 8100 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
24369c2e PB |
8101 | { |
8102 | struct io_wq_data data; | |
24369c2e | 8103 | unsigned int concurrency; |
24369c2e PB |
8104 | |
8105 | data.user = ctx->user; | |
e9fd9396 | 8106 | data.free_work = io_free_work; |
f5fa38c5 | 8107 | data.do_work = io_wq_submit_work; |
24369c2e | 8108 | |
d25e3a3d JA |
8109 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8110 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8111 | |
5aa75ed5 | 8112 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8113 | } |
8114 | ||
5aa75ed5 JA |
8115 | static int io_uring_alloc_task_context(struct task_struct *task, |
8116 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8117 | { |
8118 | struct io_uring_task *tctx; | |
d8a6df10 | 8119 | int ret; |
0f212204 JA |
8120 | |
8121 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8122 | if (unlikely(!tctx)) | |
8123 | return -ENOMEM; | |
8124 | ||
d8a6df10 JA |
8125 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8126 | if (unlikely(ret)) { | |
8127 | kfree(tctx); | |
8128 | return ret; | |
8129 | } | |
8130 | ||
5aa75ed5 JA |
8131 | tctx->io_wq = io_init_wq_offload(ctx); |
8132 | if (IS_ERR(tctx->io_wq)) { | |
8133 | ret = PTR_ERR(tctx->io_wq); | |
8134 | percpu_counter_destroy(&tctx->inflight); | |
8135 | kfree(tctx); | |
8136 | return ret; | |
8137 | } | |
8138 | ||
0f212204 JA |
8139 | xa_init(&tctx->xa); |
8140 | init_waitqueue_head(&tctx->wait); | |
8141 | tctx->last = NULL; | |
fdaf083c JA |
8142 | atomic_set(&tctx->in_idle, 0); |
8143 | tctx->sqpoll = false; | |
500a373d JA |
8144 | io_init_identity(&tctx->__identity); |
8145 | tctx->identity = &tctx->__identity; | |
0f212204 | 8146 | task->io_uring = tctx; |
7cbf1722 JA |
8147 | spin_lock_init(&tctx->task_lock); |
8148 | INIT_WQ_LIST(&tctx->task_list); | |
8149 | tctx->task_state = 0; | |
8150 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
8151 | return 0; |
8152 | } | |
8153 | ||
8154 | void __io_uring_free(struct task_struct *tsk) | |
8155 | { | |
8156 | struct io_uring_task *tctx = tsk->io_uring; | |
8157 | ||
8158 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8159 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8160 | if (tctx->identity != &tctx->__identity) | |
8161 | kfree(tctx->identity); | |
d8a6df10 | 8162 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8163 | kfree(tctx); |
8164 | tsk->io_uring = NULL; | |
8165 | } | |
8166 | ||
7e84e1c7 SG |
8167 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8168 | struct io_uring_params *p) | |
2b188cc1 JA |
8169 | { |
8170 | int ret; | |
8171 | ||
d25e3a3d JA |
8172 | /* Retain compatibility with failing for an invalid attach attempt */ |
8173 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8174 | IORING_SETUP_ATTACH_WQ) { | |
8175 | struct fd f; | |
8176 | ||
8177 | f = fdget(p->wq_fd); | |
8178 | if (!f.file) | |
8179 | return -ENXIO; | |
8180 | if (f.file->f_op != &io_uring_fops) { | |
8181 | fdput(f); | |
8182 | return -EINVAL; | |
8183 | } | |
8184 | fdput(f); | |
8185 | } | |
6c271ce2 | 8186 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8187 | struct io_sq_data *sqd; |
8188 | ||
3ec482d1 | 8189 | ret = -EPERM; |
ce59fc69 | 8190 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8191 | goto err; |
8192 | ||
534ca6d6 JA |
8193 | sqd = io_get_sq_data(p); |
8194 | if (IS_ERR(sqd)) { | |
8195 | ret = PTR_ERR(sqd); | |
8196 | goto err; | |
8197 | } | |
69fb2131 | 8198 | |
534ca6d6 | 8199 | ctx->sq_data = sqd; |
69fb2131 JA |
8200 | io_sq_thread_park(sqd); |
8201 | mutex_lock(&sqd->ctx_lock); | |
8202 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8203 | mutex_unlock(&sqd->ctx_lock); | |
8204 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8205 | |
917257da JA |
8206 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8207 | if (!ctx->sq_thread_idle) | |
8208 | ctx->sq_thread_idle = HZ; | |
8209 | ||
aa06165d | 8210 | if (sqd->thread) |
5aa75ed5 | 8211 | return 0; |
aa06165d | 8212 | |
6c271ce2 | 8213 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8214 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8215 | |
917257da | 8216 | ret = -EINVAL; |
44a9bd18 JA |
8217 | if (cpu >= nr_cpu_ids) |
8218 | goto err; | |
7889f44d | 8219 | if (!cpu_online(cpu)) |
917257da JA |
8220 | goto err; |
8221 | ||
69fb2131 | 8222 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8223 | cpu, "io_uring-sq"); |
6c271ce2 | 8224 | } else { |
69fb2131 | 8225 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8226 | "io_uring-sq"); |
8227 | } | |
534ca6d6 JA |
8228 | if (IS_ERR(sqd->thread)) { |
8229 | ret = PTR_ERR(sqd->thread); | |
8230 | sqd->thread = NULL; | |
6c271ce2 JA |
8231 | goto err; |
8232 | } | |
5aa75ed5 | 8233 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
0f212204 JA |
8234 | if (ret) |
8235 | goto err; | |
6c271ce2 JA |
8236 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8237 | /* Can't have SQ_AFF without SQPOLL */ | |
8238 | ret = -EINVAL; | |
8239 | goto err; | |
8240 | } | |
8241 | ||
2b188cc1 JA |
8242 | return 0; |
8243 | err: | |
5aa75ed5 | 8244 | io_sq_thread_stop(ctx); |
2b188cc1 JA |
8245 | return ret; |
8246 | } | |
8247 | ||
7e84e1c7 SG |
8248 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8249 | { | |
534ca6d6 JA |
8250 | struct io_sq_data *sqd = ctx->sq_data; |
8251 | ||
8252 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8253 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8254 | } |
8255 | ||
a087e2b5 BM |
8256 | static inline void __io_unaccount_mem(struct user_struct *user, |
8257 | unsigned long nr_pages) | |
2b188cc1 JA |
8258 | { |
8259 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8260 | } | |
8261 | ||
a087e2b5 BM |
8262 | static inline int __io_account_mem(struct user_struct *user, |
8263 | unsigned long nr_pages) | |
2b188cc1 JA |
8264 | { |
8265 | unsigned long page_limit, cur_pages, new_pages; | |
8266 | ||
8267 | /* Don't allow more pages than we can safely lock */ | |
8268 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8269 | ||
8270 | do { | |
8271 | cur_pages = atomic_long_read(&user->locked_vm); | |
8272 | new_pages = cur_pages + nr_pages; | |
8273 | if (new_pages > page_limit) | |
8274 | return -ENOMEM; | |
8275 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8276 | new_pages) != cur_pages); | |
8277 | ||
8278 | return 0; | |
8279 | } | |
8280 | ||
26bfa89e | 8281 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8282 | { |
aad5d8da | 8283 | if (ctx->limit_mem) |
a087e2b5 | 8284 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8285 | |
26bfa89e JA |
8286 | if (ctx->mm_account) |
8287 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8288 | } |
8289 | ||
26bfa89e | 8290 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8291 | { |
30975825 BM |
8292 | int ret; |
8293 | ||
8294 | if (ctx->limit_mem) { | |
8295 | ret = __io_account_mem(ctx->user, nr_pages); | |
8296 | if (ret) | |
8297 | return ret; | |
8298 | } | |
8299 | ||
26bfa89e JA |
8300 | if (ctx->mm_account) |
8301 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8302 | |
8303 | return 0; | |
8304 | } | |
8305 | ||
2b188cc1 JA |
8306 | static void io_mem_free(void *ptr) |
8307 | { | |
52e04ef4 MR |
8308 | struct page *page; |
8309 | ||
8310 | if (!ptr) | |
8311 | return; | |
2b188cc1 | 8312 | |
52e04ef4 | 8313 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8314 | if (put_page_testzero(page)) |
8315 | free_compound_page(page); | |
8316 | } | |
8317 | ||
8318 | static void *io_mem_alloc(size_t size) | |
8319 | { | |
8320 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8321 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8322 | |
8323 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8324 | } | |
8325 | ||
75b28aff HV |
8326 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8327 | size_t *sq_offset) | |
8328 | { | |
8329 | struct io_rings *rings; | |
8330 | size_t off, sq_array_size; | |
8331 | ||
8332 | off = struct_size(rings, cqes, cq_entries); | |
8333 | if (off == SIZE_MAX) | |
8334 | return SIZE_MAX; | |
8335 | ||
8336 | #ifdef CONFIG_SMP | |
8337 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8338 | if (off == 0) | |
8339 | return SIZE_MAX; | |
8340 | #endif | |
8341 | ||
b36200f5 DV |
8342 | if (sq_offset) |
8343 | *sq_offset = off; | |
8344 | ||
75b28aff HV |
8345 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8346 | if (sq_array_size == SIZE_MAX) | |
8347 | return SIZE_MAX; | |
8348 | ||
8349 | if (check_add_overflow(off, sq_array_size, &off)) | |
8350 | return SIZE_MAX; | |
8351 | ||
75b28aff HV |
8352 | return off; |
8353 | } | |
8354 | ||
0a96bbe4 | 8355 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8356 | { |
8357 | int i, j; | |
8358 | ||
8359 | if (!ctx->user_bufs) | |
8360 | return -ENXIO; | |
8361 | ||
8362 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8363 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8364 | ||
8365 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8366 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8367 | |
de293938 | 8368 | if (imu->acct_pages) |
26bfa89e | 8369 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8370 | kvfree(imu->bvec); |
edafccee JA |
8371 | imu->nr_bvecs = 0; |
8372 | } | |
8373 | ||
8374 | kfree(ctx->user_bufs); | |
8375 | ctx->user_bufs = NULL; | |
8376 | ctx->nr_user_bufs = 0; | |
8377 | return 0; | |
8378 | } | |
8379 | ||
8380 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8381 | void __user *arg, unsigned index) | |
8382 | { | |
8383 | struct iovec __user *src; | |
8384 | ||
8385 | #ifdef CONFIG_COMPAT | |
8386 | if (ctx->compat) { | |
8387 | struct compat_iovec __user *ciovs; | |
8388 | struct compat_iovec ciov; | |
8389 | ||
8390 | ciovs = (struct compat_iovec __user *) arg; | |
8391 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8392 | return -EFAULT; | |
8393 | ||
d55e5f5b | 8394 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8395 | dst->iov_len = ciov.iov_len; |
8396 | return 0; | |
8397 | } | |
8398 | #endif | |
8399 | src = (struct iovec __user *) arg; | |
8400 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8401 | return -EFAULT; | |
8402 | return 0; | |
8403 | } | |
8404 | ||
de293938 JA |
8405 | /* |
8406 | * Not super efficient, but this is just a registration time. And we do cache | |
8407 | * the last compound head, so generally we'll only do a full search if we don't | |
8408 | * match that one. | |
8409 | * | |
8410 | * We check if the given compound head page has already been accounted, to | |
8411 | * avoid double accounting it. This allows us to account the full size of the | |
8412 | * page, not just the constituent pages of a huge page. | |
8413 | */ | |
8414 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8415 | int nr_pages, struct page *hpage) | |
8416 | { | |
8417 | int i, j; | |
8418 | ||
8419 | /* check current page array */ | |
8420 | for (i = 0; i < nr_pages; i++) { | |
8421 | if (!PageCompound(pages[i])) | |
8422 | continue; | |
8423 | if (compound_head(pages[i]) == hpage) | |
8424 | return true; | |
8425 | } | |
8426 | ||
8427 | /* check previously registered pages */ | |
8428 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8429 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8430 | ||
8431 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8432 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8433 | continue; | |
8434 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8435 | return true; | |
8436 | } | |
8437 | } | |
8438 | ||
8439 | return false; | |
8440 | } | |
8441 | ||
8442 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8443 | int nr_pages, struct io_mapped_ubuf *imu, | |
8444 | struct page **last_hpage) | |
8445 | { | |
8446 | int i, ret; | |
8447 | ||
8448 | for (i = 0; i < nr_pages; i++) { | |
8449 | if (!PageCompound(pages[i])) { | |
8450 | imu->acct_pages++; | |
8451 | } else { | |
8452 | struct page *hpage; | |
8453 | ||
8454 | hpage = compound_head(pages[i]); | |
8455 | if (hpage == *last_hpage) | |
8456 | continue; | |
8457 | *last_hpage = hpage; | |
8458 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8459 | continue; | |
8460 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8461 | } | |
8462 | } | |
8463 | ||
8464 | if (!imu->acct_pages) | |
8465 | return 0; | |
8466 | ||
26bfa89e | 8467 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8468 | if (ret) |
8469 | imu->acct_pages = 0; | |
8470 | return ret; | |
8471 | } | |
8472 | ||
0a96bbe4 BM |
8473 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8474 | struct io_mapped_ubuf *imu, | |
8475 | struct page **last_hpage) | |
edafccee JA |
8476 | { |
8477 | struct vm_area_struct **vmas = NULL; | |
8478 | struct page **pages = NULL; | |
0a96bbe4 BM |
8479 | unsigned long off, start, end, ubuf; |
8480 | size_t size; | |
8481 | int ret, pret, nr_pages, i; | |
8482 | ||
8483 | ubuf = (unsigned long) iov->iov_base; | |
8484 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8485 | start = ubuf >> PAGE_SHIFT; | |
8486 | nr_pages = end - start; | |
8487 | ||
8488 | ret = -ENOMEM; | |
8489 | ||
8490 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8491 | if (!pages) | |
8492 | goto done; | |
8493 | ||
8494 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8495 | GFP_KERNEL); | |
8496 | if (!vmas) | |
8497 | goto done; | |
edafccee | 8498 | |
0a96bbe4 BM |
8499 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8500 | GFP_KERNEL); | |
8501 | if (!imu->bvec) | |
8502 | goto done; | |
8503 | ||
8504 | ret = 0; | |
8505 | mmap_read_lock(current->mm); | |
8506 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8507 | pages, vmas); | |
8508 | if (pret == nr_pages) { | |
8509 | /* don't support file backed memory */ | |
8510 | for (i = 0; i < nr_pages; i++) { | |
8511 | struct vm_area_struct *vma = vmas[i]; | |
8512 | ||
8513 | if (vma->vm_file && | |
8514 | !is_file_hugepages(vma->vm_file)) { | |
8515 | ret = -EOPNOTSUPP; | |
8516 | break; | |
8517 | } | |
8518 | } | |
8519 | } else { | |
8520 | ret = pret < 0 ? pret : -EFAULT; | |
8521 | } | |
8522 | mmap_read_unlock(current->mm); | |
8523 | if (ret) { | |
8524 | /* | |
8525 | * if we did partial map, or found file backed vmas, | |
8526 | * release any pages we did get | |
8527 | */ | |
8528 | if (pret > 0) | |
8529 | unpin_user_pages(pages, pret); | |
8530 | kvfree(imu->bvec); | |
8531 | goto done; | |
8532 | } | |
8533 | ||
8534 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8535 | if (ret) { | |
8536 | unpin_user_pages(pages, pret); | |
8537 | kvfree(imu->bvec); | |
8538 | goto done; | |
8539 | } | |
8540 | ||
8541 | off = ubuf & ~PAGE_MASK; | |
8542 | size = iov->iov_len; | |
8543 | for (i = 0; i < nr_pages; i++) { | |
8544 | size_t vec_len; | |
8545 | ||
8546 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8547 | imu->bvec[i].bv_page = pages[i]; | |
8548 | imu->bvec[i].bv_len = vec_len; | |
8549 | imu->bvec[i].bv_offset = off; | |
8550 | off = 0; | |
8551 | size -= vec_len; | |
8552 | } | |
8553 | /* store original address for later verification */ | |
8554 | imu->ubuf = ubuf; | |
8555 | imu->len = iov->iov_len; | |
8556 | imu->nr_bvecs = nr_pages; | |
8557 | ret = 0; | |
8558 | done: | |
8559 | kvfree(pages); | |
8560 | kvfree(vmas); | |
8561 | return ret; | |
8562 | } | |
8563 | ||
2b358604 | 8564 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8565 | { |
edafccee JA |
8566 | if (ctx->user_bufs) |
8567 | return -EBUSY; | |
8568 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8569 | return -EINVAL; | |
8570 | ||
8571 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8572 | GFP_KERNEL); | |
8573 | if (!ctx->user_bufs) | |
8574 | return -ENOMEM; | |
8575 | ||
2b358604 BM |
8576 | return 0; |
8577 | } | |
edafccee | 8578 | |
2b358604 BM |
8579 | static int io_buffer_validate(struct iovec *iov) |
8580 | { | |
8581 | /* | |
8582 | * Don't impose further limits on the size and buffer | |
8583 | * constraints here, we'll -EINVAL later when IO is | |
8584 | * submitted if they are wrong. | |
8585 | */ | |
8586 | if (!iov->iov_base || !iov->iov_len) | |
8587 | return -EFAULT; | |
edafccee | 8588 | |
2b358604 BM |
8589 | /* arbitrary limit, but we need something */ |
8590 | if (iov->iov_len > SZ_1G) | |
8591 | return -EFAULT; | |
edafccee | 8592 | |
2b358604 BM |
8593 | return 0; |
8594 | } | |
edafccee | 8595 | |
2b358604 BM |
8596 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8597 | unsigned int nr_args) | |
8598 | { | |
8599 | int i, ret; | |
8600 | struct iovec iov; | |
8601 | struct page *last_hpage = NULL; | |
edafccee | 8602 | |
2b358604 BM |
8603 | ret = io_buffers_map_alloc(ctx, nr_args); |
8604 | if (ret) | |
8605 | return ret; | |
edafccee | 8606 | |
edafccee JA |
8607 | for (i = 0; i < nr_args; i++) { |
8608 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8609 | |
edafccee JA |
8610 | ret = io_copy_iov(ctx, &iov, arg, i); |
8611 | if (ret) | |
0a96bbe4 | 8612 | break; |
de293938 | 8613 | |
2b358604 BM |
8614 | ret = io_buffer_validate(&iov); |
8615 | if (ret) | |
0a96bbe4 | 8616 | break; |
edafccee | 8617 | |
0a96bbe4 BM |
8618 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8619 | if (ret) | |
8620 | break; | |
edafccee JA |
8621 | |
8622 | ctx->nr_user_bufs++; | |
8623 | } | |
0a96bbe4 BM |
8624 | |
8625 | if (ret) | |
8626 | io_sqe_buffers_unregister(ctx); | |
8627 | ||
edafccee JA |
8628 | return ret; |
8629 | } | |
8630 | ||
9b402849 JA |
8631 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8632 | { | |
8633 | __s32 __user *fds = arg; | |
8634 | int fd; | |
8635 | ||
8636 | if (ctx->cq_ev_fd) | |
8637 | return -EBUSY; | |
8638 | ||
8639 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8640 | return -EFAULT; | |
8641 | ||
8642 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8643 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8644 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8645 | ctx->cq_ev_fd = NULL; | |
8646 | return ret; | |
8647 | } | |
8648 | ||
8649 | return 0; | |
8650 | } | |
8651 | ||
8652 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8653 | { | |
8654 | if (ctx->cq_ev_fd) { | |
8655 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8656 | ctx->cq_ev_fd = NULL; | |
8657 | return 0; | |
8658 | } | |
8659 | ||
8660 | return -ENXIO; | |
8661 | } | |
8662 | ||
5a2e745d JA |
8663 | static int __io_destroy_buffers(int id, void *p, void *data) |
8664 | { | |
8665 | struct io_ring_ctx *ctx = data; | |
8666 | struct io_buffer *buf = p; | |
8667 | ||
067524e9 | 8668 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8669 | return 0; |
8670 | } | |
8671 | ||
8672 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8673 | { | |
8674 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8675 | idr_destroy(&ctx->io_buffer_idr); | |
8676 | } | |
8677 | ||
68e68ee6 | 8678 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8679 | { |
68e68ee6 | 8680 | struct io_kiocb *req, *nxt; |
1b4c351f | 8681 | |
68e68ee6 JA |
8682 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8683 | if (tsk && req->task != tsk) | |
8684 | continue; | |
1b4c351f JA |
8685 | list_del(&req->compl.list); |
8686 | kmem_cache_free(req_cachep, req); | |
8687 | } | |
8688 | } | |
8689 | ||
9a4fdbd8 | 8690 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
2b188cc1 | 8691 | { |
bf019da7 PB |
8692 | struct io_submit_state *submit_state = &ctx->submit_state; |
8693 | ||
9a4fdbd8 JA |
8694 | mutex_lock(&ctx->uring_lock); |
8695 | ||
8696 | if (submit_state->free_reqs) | |
8697 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, | |
8698 | submit_state->reqs); | |
8699 | ||
8700 | io_req_cache_free(&submit_state->comp.free_list, NULL); | |
8701 | ||
8702 | spin_lock_irq(&ctx->completion_lock); | |
8703 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); | |
8704 | spin_unlock_irq(&ctx->completion_lock); | |
8705 | ||
8706 | mutex_unlock(&ctx->uring_lock); | |
8707 | } | |
8708 | ||
2b188cc1 JA |
8709 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8710 | { | |
04fc6c80 PB |
8711 | /* |
8712 | * Some may use context even when all refs and requests have been put, | |
8713 | * and they are free to do so while still holding uring_lock, see | |
8714 | * __io_req_task_submit(). Wait for them to finish. | |
8715 | */ | |
8716 | mutex_lock(&ctx->uring_lock); | |
8717 | mutex_unlock(&ctx->uring_lock); | |
8718 | ||
5aa75ed5 | 8719 | io_sq_thread_stop(ctx); |
0a96bbe4 | 8720 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8721 | |
8722 | if (ctx->sqo_task) { | |
8723 | put_task_struct(ctx->sqo_task); | |
8724 | ctx->sqo_task = NULL; | |
8725 | mmdrop(ctx->mm_account); | |
8726 | ctx->mm_account = NULL; | |
30975825 | 8727 | } |
def596e9 | 8728 | |
91d8f519 DZ |
8729 | #ifdef CONFIG_BLK_CGROUP |
8730 | if (ctx->sqo_blkcg_css) | |
8731 | css_put(ctx->sqo_blkcg_css); | |
8732 | #endif | |
8733 | ||
8bad28d8 | 8734 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8735 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8736 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8737 | io_eventfd_unregister(ctx); |
5a2e745d | 8738 | io_destroy_buffers(ctx); |
41726c9a | 8739 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8740 | |
2b188cc1 | 8741 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8742 | if (ctx->ring_sock) { |
8743 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8744 | sock_release(ctx->ring_sock); |
355e8d26 | 8745 | } |
2b188cc1 JA |
8746 | #endif |
8747 | ||
75b28aff | 8748 | io_mem_free(ctx->rings); |
2b188cc1 | 8749 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8750 | |
8751 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8752 | free_uid(ctx->user); |
181e448d | 8753 | put_cred(ctx->creds); |
9a4fdbd8 | 8754 | io_req_caches_free(ctx, NULL); |
78076bb6 | 8755 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8756 | kfree(ctx); |
8757 | } | |
8758 | ||
8759 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8760 | { | |
8761 | struct io_ring_ctx *ctx = file->private_data; | |
8762 | __poll_t mask = 0; | |
8763 | ||
8764 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8765 | /* |
8766 | * synchronizes with barrier from wq_has_sleeper call in | |
8767 | * io_commit_cqring | |
8768 | */ | |
2b188cc1 | 8769 | smp_rmb(); |
90554200 | 8770 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8771 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8772 | |
8773 | /* | |
8774 | * Don't flush cqring overflow list here, just do a simple check. | |
8775 | * Otherwise there could possible be ABBA deadlock: | |
8776 | * CPU0 CPU1 | |
8777 | * ---- ---- | |
8778 | * lock(&ctx->uring_lock); | |
8779 | * lock(&ep->mtx); | |
8780 | * lock(&ctx->uring_lock); | |
8781 | * lock(&ep->mtx); | |
8782 | * | |
8783 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8784 | * pushs them to do the flush. | |
8785 | */ | |
8786 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8787 | mask |= EPOLLIN | EPOLLRDNORM; |
8788 | ||
8789 | return mask; | |
8790 | } | |
8791 | ||
8792 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8793 | { | |
8794 | struct io_ring_ctx *ctx = file->private_data; | |
8795 | ||
8796 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8797 | } | |
8798 | ||
0bead8cd | 8799 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8800 | { |
1e6fa521 | 8801 | struct io_identity *iod; |
071698e1 | 8802 | |
1e6fa521 JA |
8803 | iod = idr_remove(&ctx->personality_idr, id); |
8804 | if (iod) { | |
8805 | put_cred(iod->creds); | |
8806 | if (refcount_dec_and_test(&iod->count)) | |
8807 | kfree(iod); | |
0bead8cd | 8808 | return 0; |
1e6fa521 | 8809 | } |
0bead8cd YD |
8810 | |
8811 | return -EINVAL; | |
8812 | } | |
8813 | ||
8814 | static int io_remove_personalities(int id, void *p, void *data) | |
8815 | { | |
8816 | struct io_ring_ctx *ctx = data; | |
8817 | ||
8818 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8819 | return 0; |
8820 | } | |
8821 | ||
7c25c0d1 JA |
8822 | static void io_run_ctx_fallback(struct io_ring_ctx *ctx) |
8823 | { | |
8824 | struct callback_head *work, *head, *next; | |
8825 | ||
8826 | do { | |
8827 | do { | |
8828 | head = NULL; | |
8829 | work = READ_ONCE(ctx->exit_task_work); | |
8830 | } while (cmpxchg(&ctx->exit_task_work, work, head) != work); | |
8831 | ||
8832 | if (!work) | |
8833 | break; | |
8834 | ||
8835 | do { | |
8836 | next = work->next; | |
8837 | work->func(work); | |
8838 | work = next; | |
8839 | cond_resched(); | |
8840 | } while (work); | |
8841 | } while (1); | |
8842 | } | |
8843 | ||
85faa7b8 JA |
8844 | static void io_ring_exit_work(struct work_struct *work) |
8845 | { | |
b2edc0a7 PB |
8846 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8847 | exit_work); | |
85faa7b8 | 8848 | |
56952e91 JA |
8849 | /* |
8850 | * If we're doing polled IO and end up having requests being | |
8851 | * submitted async (out-of-line), then completions can come in while | |
8852 | * we're waiting for refs to drop. We need to reap these manually, | |
8853 | * as nobody else will be looking for them. | |
8854 | */ | |
b2edc0a7 | 8855 | do { |
9936c7c2 | 8856 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
7c25c0d1 | 8857 | io_run_ctx_fallback(ctx); |
b2edc0a7 | 8858 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8859 | io_ring_ctx_free(ctx); |
8860 | } | |
8861 | ||
2b188cc1 JA |
8862 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8863 | { | |
8864 | mutex_lock(&ctx->uring_lock); | |
8865 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8866 | |
8867 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8868 | ctx->sqo_dead = 1; | |
8869 | ||
cda286f0 PB |
8870 | /* if force is set, the ring is going away. always drop after that */ |
8871 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8872 | if (ctx->rings) |
6c503150 | 8873 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8874 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8875 | mutex_unlock(&ctx->uring_lock); |
8876 | ||
6b81928d PB |
8877 | io_kill_timeouts(ctx, NULL, NULL); |
8878 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8879 | |
15dff286 | 8880 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8881 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8882 | |
85faa7b8 | 8883 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8884 | /* |
8885 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8886 | * if we're exiting a ton of rings at the same time. It just adds | |
8887 | * noise and overhead, there's no discernable change in runtime | |
8888 | * over using system_wq. | |
8889 | */ | |
8890 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8891 | } |
8892 | ||
8893 | static int io_uring_release(struct inode *inode, struct file *file) | |
8894 | { | |
8895 | struct io_ring_ctx *ctx = file->private_data; | |
8896 | ||
8897 | file->private_data = NULL; | |
8898 | io_ring_ctx_wait_and_kill(ctx); | |
8899 | return 0; | |
8900 | } | |
8901 | ||
f6edbabb PB |
8902 | struct io_task_cancel { |
8903 | struct task_struct *task; | |
8904 | struct files_struct *files; | |
8905 | }; | |
f254ac04 | 8906 | |
f6edbabb | 8907 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8908 | { |
9a472ef7 | 8909 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8910 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8911 | bool ret; |
8912 | ||
f6edbabb | 8913 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8914 | unsigned long flags; |
8915 | struct io_ring_ctx *ctx = req->ctx; | |
8916 | ||
8917 | /* protect against races with linked timeouts */ | |
8918 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8919 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8920 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8921 | } else { | |
f6edbabb | 8922 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8923 | } |
8924 | return ret; | |
b711d4ea JA |
8925 | } |
8926 | ||
b7ddce3c | 8927 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8928 | struct task_struct *task, |
b7ddce3c PB |
8929 | struct files_struct *files) |
8930 | { | |
8931 | struct io_defer_entry *de = NULL; | |
8932 | LIST_HEAD(list); | |
8933 | ||
8934 | spin_lock_irq(&ctx->completion_lock); | |
8935 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8936 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8937 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8938 | break; | |
8939 | } | |
8940 | } | |
8941 | spin_unlock_irq(&ctx->completion_lock); | |
8942 | ||
8943 | while (!list_empty(&list)) { | |
8944 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8945 | list_del_init(&de->list); | |
8946 | req_set_fail_links(de->req); | |
8947 | io_put_req(de->req); | |
8948 | io_req_complete(de->req, -ECANCELED); | |
8949 | kfree(de); | |
8950 | } | |
8951 | } | |
8952 | ||
9936c7c2 PB |
8953 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8954 | struct task_struct *task, | |
8955 | struct files_struct *files) | |
8956 | { | |
8957 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
5aa75ed5 | 8958 | struct io_uring_task *tctx = current->io_uring; |
9936c7c2 PB |
8959 | |
8960 | while (1) { | |
8961 | enum io_wq_cancel cret; | |
8962 | bool ret = false; | |
8963 | ||
5aa75ed5 JA |
8964 | if (tctx && tctx->io_wq) { |
8965 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, | |
9936c7c2 PB |
8966 | &cancel, true); |
8967 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8968 | } | |
8969 | ||
8970 | /* SQPOLL thread does its own polling */ | |
8971 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8972 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8973 | io_iopoll_try_reap_events(ctx); | |
8974 | ret = true; | |
8975 | } | |
8976 | } | |
8977 | ||
8978 | ret |= io_poll_remove_all(ctx, task, files); | |
8979 | ret |= io_kill_timeouts(ctx, task, files); | |
8980 | ret |= io_run_task_work(); | |
8981 | io_cqring_overflow_flush(ctx, true, task, files); | |
8982 | if (!ret) | |
8983 | break; | |
8984 | cond_resched(); | |
8985 | } | |
8986 | } | |
8987 | ||
ca70f00b PB |
8988 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8989 | struct task_struct *task, | |
8990 | struct files_struct *files) | |
8991 | { | |
8992 | struct io_kiocb *req; | |
8993 | int cnt = 0; | |
8994 | ||
8995 | spin_lock_irq(&ctx->inflight_lock); | |
8996 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8997 | cnt += io_match_task(req, task, files); | |
8998 | spin_unlock_irq(&ctx->inflight_lock); | |
8999 | return cnt; | |
9000 | } | |
9001 | ||
b52fda00 | 9002 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 9003 | struct task_struct *task, |
fcb323cc JA |
9004 | struct files_struct *files) |
9005 | { | |
fcb323cc | 9006 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 9007 | DEFINE_WAIT(wait); |
ca70f00b | 9008 | int inflight; |
fcb323cc | 9009 | |
ca70f00b PB |
9010 | inflight = io_uring_count_inflight(ctx, task, files); |
9011 | if (!inflight) | |
fcb323cc | 9012 | break; |
f6edbabb | 9013 | |
9936c7c2 | 9014 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 9015 | |
34343786 PB |
9016 | if (ctx->sq_data) |
9017 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
9018 | prepare_to_wait(&task->io_uring->wait, &wait, |
9019 | TASK_UNINTERRUPTIBLE); | |
9020 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
9021 | schedule(); | |
c98de08c | 9022 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
9023 | if (ctx->sq_data) |
9024 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 9025 | } |
0f212204 JA |
9026 | } |
9027 | ||
d9d05217 PB |
9028 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
9029 | { | |
d9d05217 PB |
9030 | mutex_lock(&ctx->uring_lock); |
9031 | ctx->sqo_dead = 1; | |
9032 | mutex_unlock(&ctx->uring_lock); | |
9033 | ||
9034 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
9035 | if (ctx->rings) |
9036 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
9037 | } |
9038 | ||
0f212204 JA |
9039 | /* |
9040 | * We need to iteratively cancel requests, in case a request has dependent | |
9041 | * hard links. These persist even for failure of cancelations, hence keep | |
9042 | * looping until none are found. | |
9043 | */ | |
9044 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
9045 | struct files_struct *files) | |
9046 | { | |
9047 | struct task_struct *task = current; | |
9048 | ||
fdaf083c | 9049 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 9050 | io_disable_sqo_submit(ctx); |
534ca6d6 | 9051 | task = ctx->sq_data->thread; |
fdaf083c JA |
9052 | atomic_inc(&task->io_uring->in_idle); |
9053 | io_sq_thread_park(ctx->sq_data); | |
9054 | } | |
0f212204 | 9055 | |
df9923f9 | 9056 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 9057 | |
3a7efd1a | 9058 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 9059 | if (!files) |
9936c7c2 | 9060 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c JA |
9061 | |
9062 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
9063 | atomic_dec(&task->io_uring->in_idle); | |
fdaf083c JA |
9064 | io_sq_thread_unpark(ctx->sq_data); |
9065 | } | |
0f212204 JA |
9066 | } |
9067 | ||
9068 | /* | |
9069 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9070 | */ | |
fdaf083c | 9071 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 9072 | { |
236434c3 | 9073 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 9074 | int ret; |
236434c3 MWO |
9075 | |
9076 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9077 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9078 | if (unlikely(ret)) |
9079 | return ret; | |
236434c3 | 9080 | tctx = current->io_uring; |
0f212204 | 9081 | } |
236434c3 MWO |
9082 | if (tctx->last != file) { |
9083 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 9084 | |
236434c3 | 9085 | if (!old) { |
0f212204 | 9086 | get_file(file); |
a528b04e PB |
9087 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
9088 | file, GFP_KERNEL)); | |
9089 | if (ret) { | |
9090 | fput(file); | |
9091 | return ret; | |
9092 | } | |
ecfc8492 PB |
9093 | |
9094 | /* one and only SQPOLL file note, held by sqo_task */ | |
9095 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
9096 | current != ctx->sqo_task); | |
0f212204 | 9097 | } |
236434c3 | 9098 | tctx->last = file; |
0f212204 JA |
9099 | } |
9100 | ||
fdaf083c JA |
9101 | /* |
9102 | * This is race safe in that the task itself is doing this, hence it | |
9103 | * cannot be going through the exit/cancel paths at the same time. | |
9104 | * This cannot be modified while exit/cancel is running. | |
9105 | */ | |
9106 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9107 | tctx->sqpoll = true; | |
9108 | ||
0f212204 JA |
9109 | return 0; |
9110 | } | |
9111 | ||
9112 | /* | |
9113 | * Remove this io_uring_file -> task mapping. | |
9114 | */ | |
9115 | static void io_uring_del_task_file(struct file *file) | |
9116 | { | |
9117 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9118 | |
9119 | if (tctx->last == file) | |
9120 | tctx->last = NULL; | |
5e2ed8c4 | 9121 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9122 | if (file) |
9123 | fput(file); | |
9124 | } | |
9125 | ||
de7f1d9e PB |
9126 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9127 | { | |
9128 | struct file *file; | |
9129 | unsigned long index; | |
9130 | ||
9131 | xa_for_each(&tctx->xa, index, file) | |
9132 | io_uring_del_task_file(file); | |
9133 | } | |
9134 | ||
0f212204 JA |
9135 | void __io_uring_files_cancel(struct files_struct *files) |
9136 | { | |
9137 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9138 | struct file *file; |
9139 | unsigned long index; | |
0f212204 JA |
9140 | |
9141 | /* make sure overflow events are dropped */ | |
fdaf083c | 9142 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9143 | xa_for_each(&tctx->xa, index, file) |
9144 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9145 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9146 | |
5aa75ed5 | 9147 | if (files) { |
de7f1d9e | 9148 | io_uring_remove_task_files(tctx); |
5aa75ed5 JA |
9149 | } else if (tctx->io_wq && current->flags & PF_EXITING) { |
9150 | io_wq_destroy(tctx->io_wq); | |
9151 | tctx->io_wq = NULL; | |
9152 | } | |
fdaf083c JA |
9153 | } |
9154 | ||
9155 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9156 | { | |
0e9ddb39 PB |
9157 | return percpu_counter_sum(&tctx->inflight); |
9158 | } | |
fdaf083c | 9159 | |
0e9ddb39 PB |
9160 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
9161 | { | |
9162 | struct io_uring_task *tctx; | |
9163 | s64 inflight; | |
9164 | DEFINE_WAIT(wait); | |
fdaf083c | 9165 | |
0e9ddb39 PB |
9166 | if (!ctx->sq_data) |
9167 | return; | |
9168 | tctx = ctx->sq_data->thread->io_uring; | |
9169 | io_disable_sqo_submit(ctx); | |
fdaf083c | 9170 | |
0e9ddb39 PB |
9171 | atomic_inc(&tctx->in_idle); |
9172 | do { | |
9173 | /* read completions before cancelations */ | |
9174 | inflight = tctx_inflight(tctx); | |
9175 | if (!inflight) | |
9176 | break; | |
9177 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 9178 | |
0e9ddb39 PB |
9179 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
9180 | /* | |
9181 | * If we've seen completions, retry without waiting. This | |
9182 | * avoids a race where a completion comes in before we did | |
9183 | * prepare_to_wait(). | |
9184 | */ | |
9185 | if (inflight == tctx_inflight(tctx)) | |
9186 | schedule(); | |
9187 | finish_wait(&tctx->wait, &wait); | |
9188 | } while (1); | |
9189 | atomic_dec(&tctx->in_idle); | |
0f212204 JA |
9190 | } |
9191 | ||
0f212204 JA |
9192 | /* |
9193 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9194 | * requests. | |
9195 | */ | |
9196 | void __io_uring_task_cancel(void) | |
9197 | { | |
9198 | struct io_uring_task *tctx = current->io_uring; | |
9199 | DEFINE_WAIT(wait); | |
d8a6df10 | 9200 | s64 inflight; |
0f212204 JA |
9201 | |
9202 | /* make sure overflow events are dropped */ | |
fdaf083c | 9203 | atomic_inc(&tctx->in_idle); |
0f212204 | 9204 | |
0b5cd6c3 | 9205 | /* trigger io_disable_sqo_submit() */ |
0e9ddb39 PB |
9206 | if (tctx->sqpoll) { |
9207 | struct file *file; | |
9208 | unsigned long index; | |
9209 | ||
9210 | xa_for_each(&tctx->xa, index, file) | |
9211 | io_uring_cancel_sqpoll(file->private_data); | |
9212 | } | |
0b5cd6c3 | 9213 | |
d8a6df10 | 9214 | do { |
0f212204 | 9215 | /* read completions before cancelations */ |
fdaf083c | 9216 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9217 | if (!inflight) |
9218 | break; | |
0f212204 JA |
9219 | __io_uring_files_cancel(NULL); |
9220 | ||
9221 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9222 | ||
9223 | /* | |
a1bb3cd5 PB |
9224 | * If we've seen completions, retry without waiting. This |
9225 | * avoids a race where a completion comes in before we did | |
9226 | * prepare_to_wait(). | |
0f212204 | 9227 | */ |
a1bb3cd5 PB |
9228 | if (inflight == tctx_inflight(tctx)) |
9229 | schedule(); | |
f57555ed | 9230 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9231 | } while (1); |
0f212204 | 9232 | |
fdaf083c | 9233 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9234 | |
9235 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9236 | } |
9237 | ||
fcb323cc JA |
9238 | static int io_uring_flush(struct file *file, void *data) |
9239 | { | |
6b5733eb | 9240 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9241 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9242 | |
41be53e9 | 9243 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
84965ff8 | 9244 | io_uring_cancel_task_requests(ctx, NULL); |
41be53e9 JA |
9245 | io_req_caches_free(ctx, current); |
9246 | } | |
84965ff8 | 9247 | |
7c25c0d1 JA |
9248 | io_run_ctx_fallback(ctx); |
9249 | ||
6b5733eb | 9250 | if (!tctx) |
4f793dc4 PB |
9251 | return 0; |
9252 | ||
6b5733eb PB |
9253 | /* we should have cancelled and erased it before PF_EXITING */ |
9254 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9255 | xa_load(&tctx->xa, (unsigned long)file)); | |
9256 | ||
4f793dc4 PB |
9257 | /* |
9258 | * fput() is pending, will be 2 if the only other ref is our potential | |
9259 | * task file note. If the task is exiting, drop regardless of count. | |
9260 | */ | |
6b5733eb PB |
9261 | if (atomic_long_read(&file->f_count) != 2) |
9262 | return 0; | |
4f793dc4 | 9263 | |
d9d05217 PB |
9264 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9265 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9266 | WARN_ON_ONCE(ctx->sqo_task != current && |
9267 | xa_load(&tctx->xa, (unsigned long)file)); | |
9268 | /* sqo_dead check is for when this happens after cancellation */ | |
9269 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9270 | !xa_load(&tctx->xa, (unsigned long)file)); |
9271 | ||
9272 | io_disable_sqo_submit(ctx); | |
9273 | } | |
9274 | ||
9275 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9276 | io_uring_del_task_file(file); | |
fcb323cc JA |
9277 | return 0; |
9278 | } | |
9279 | ||
6c5c240e RP |
9280 | static void *io_uring_validate_mmap_request(struct file *file, |
9281 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9282 | { |
2b188cc1 | 9283 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9284 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9285 | struct page *page; |
9286 | void *ptr; | |
9287 | ||
9288 | switch (offset) { | |
9289 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9290 | case IORING_OFF_CQ_RING: |
9291 | ptr = ctx->rings; | |
2b188cc1 JA |
9292 | break; |
9293 | case IORING_OFF_SQES: | |
9294 | ptr = ctx->sq_sqes; | |
9295 | break; | |
2b188cc1 | 9296 | default: |
6c5c240e | 9297 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9298 | } |
9299 | ||
9300 | page = virt_to_head_page(ptr); | |
a50b854e | 9301 | if (sz > page_size(page)) |
6c5c240e RP |
9302 | return ERR_PTR(-EINVAL); |
9303 | ||
9304 | return ptr; | |
9305 | } | |
9306 | ||
9307 | #ifdef CONFIG_MMU | |
9308 | ||
9309 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9310 | { | |
9311 | size_t sz = vma->vm_end - vma->vm_start; | |
9312 | unsigned long pfn; | |
9313 | void *ptr; | |
9314 | ||
9315 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9316 | if (IS_ERR(ptr)) | |
9317 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9318 | |
9319 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9320 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9321 | } | |
9322 | ||
6c5c240e RP |
9323 | #else /* !CONFIG_MMU */ |
9324 | ||
9325 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9326 | { | |
9327 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9328 | } | |
9329 | ||
9330 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9331 | { | |
9332 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9333 | } | |
9334 | ||
9335 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9336 | unsigned long addr, unsigned long len, | |
9337 | unsigned long pgoff, unsigned long flags) | |
9338 | { | |
9339 | void *ptr; | |
9340 | ||
9341 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9342 | if (IS_ERR(ptr)) | |
9343 | return PTR_ERR(ptr); | |
9344 | ||
9345 | return (unsigned long) ptr; | |
9346 | } | |
9347 | ||
9348 | #endif /* !CONFIG_MMU */ | |
9349 | ||
d9d05217 | 9350 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9351 | { |
d9d05217 | 9352 | int ret = 0; |
90554200 JA |
9353 | DEFINE_WAIT(wait); |
9354 | ||
9355 | do { | |
9356 | if (!io_sqring_full(ctx)) | |
9357 | break; | |
9358 | ||
9359 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9360 | ||
d9d05217 PB |
9361 | if (unlikely(ctx->sqo_dead)) { |
9362 | ret = -EOWNERDEAD; | |
9363 | goto out; | |
9364 | } | |
9365 | ||
90554200 JA |
9366 | if (!io_sqring_full(ctx)) |
9367 | break; | |
9368 | ||
9369 | schedule(); | |
9370 | } while (!signal_pending(current)); | |
9371 | ||
9372 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9373 | out: |
9374 | return ret; | |
90554200 JA |
9375 | } |
9376 | ||
c73ebb68 HX |
9377 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9378 | struct __kernel_timespec __user **ts, | |
9379 | const sigset_t __user **sig) | |
9380 | { | |
9381 | struct io_uring_getevents_arg arg; | |
9382 | ||
9383 | /* | |
9384 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9385 | * is just a pointer to the sigset_t. | |
9386 | */ | |
9387 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9388 | *sig = (const sigset_t __user *) argp; | |
9389 | *ts = NULL; | |
9390 | return 0; | |
9391 | } | |
9392 | ||
9393 | /* | |
9394 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9395 | * timespec and sigset_t pointers if good. | |
9396 | */ | |
9397 | if (*argsz != sizeof(arg)) | |
9398 | return -EINVAL; | |
9399 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9400 | return -EFAULT; | |
9401 | *sig = u64_to_user_ptr(arg.sigmask); | |
9402 | *argsz = arg.sigmask_sz; | |
9403 | *ts = u64_to_user_ptr(arg.ts); | |
9404 | return 0; | |
9405 | } | |
9406 | ||
2b188cc1 | 9407 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9408 | u32, min_complete, u32, flags, const void __user *, argp, |
9409 | size_t, argsz) | |
2b188cc1 JA |
9410 | { |
9411 | struct io_ring_ctx *ctx; | |
9412 | long ret = -EBADF; | |
9413 | int submitted = 0; | |
9414 | struct fd f; | |
9415 | ||
4c6e277c | 9416 | io_run_task_work(); |
b41e9852 | 9417 | |
90554200 | 9418 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9419 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9420 | return -EINVAL; |
9421 | ||
9422 | f = fdget(fd); | |
9423 | if (!f.file) | |
9424 | return -EBADF; | |
9425 | ||
9426 | ret = -EOPNOTSUPP; | |
9427 | if (f.file->f_op != &io_uring_fops) | |
9428 | goto out_fput; | |
9429 | ||
9430 | ret = -ENXIO; | |
9431 | ctx = f.file->private_data; | |
9432 | if (!percpu_ref_tryget(&ctx->refs)) | |
9433 | goto out_fput; | |
9434 | ||
7e84e1c7 SG |
9435 | ret = -EBADFD; |
9436 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9437 | goto out; | |
9438 | ||
6c271ce2 JA |
9439 | /* |
9440 | * For SQ polling, the thread will do all submissions and completions. | |
9441 | * Just return the requested submit count, and wake the thread if | |
9442 | * we were asked to. | |
9443 | */ | |
b2a9eada | 9444 | ret = 0; |
6c271ce2 | 9445 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9446 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9447 | |
d9d05217 PB |
9448 | ret = -EOWNERDEAD; |
9449 | if (unlikely(ctx->sqo_dead)) | |
9450 | goto out; | |
6c271ce2 | 9451 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9452 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9453 | if (flags & IORING_ENTER_SQ_WAIT) { |
9454 | ret = io_sqpoll_wait_sq(ctx); | |
9455 | if (ret) | |
9456 | goto out; | |
9457 | } | |
6c271ce2 | 9458 | submitted = to_submit; |
b2a9eada | 9459 | } else if (to_submit) { |
fdaf083c | 9460 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9461 | if (unlikely(ret)) |
9462 | goto out; | |
2b188cc1 | 9463 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9464 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9465 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9466 | |
9467 | if (submitted != to_submit) | |
9468 | goto out; | |
2b188cc1 JA |
9469 | } |
9470 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9471 | const sigset_t __user *sig; |
9472 | struct __kernel_timespec __user *ts; | |
9473 | ||
9474 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9475 | if (unlikely(ret)) | |
9476 | goto out; | |
9477 | ||
2b188cc1 JA |
9478 | min_complete = min(min_complete, ctx->cq_entries); |
9479 | ||
32b2244a XW |
9480 | /* |
9481 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9482 | * space applications don't need to do io completion events | |
9483 | * polling again, they can rely on io_sq_thread to do polling | |
9484 | * work, which can reduce cpu usage and uring_lock contention. | |
9485 | */ | |
9486 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9487 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9488 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9489 | } else { |
c73ebb68 | 9490 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9491 | } |
2b188cc1 JA |
9492 | } |
9493 | ||
7c504e65 | 9494 | out: |
6805b32e | 9495 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9496 | out_fput: |
9497 | fdput(f); | |
9498 | return submitted ? submitted : ret; | |
9499 | } | |
9500 | ||
bebdb65e | 9501 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9502 | static int io_uring_show_cred(int id, void *p, void *data) |
9503 | { | |
6b47ab81 JA |
9504 | struct io_identity *iod = p; |
9505 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9506 | struct seq_file *m = data; |
9507 | struct user_namespace *uns = seq_user_ns(m); | |
9508 | struct group_info *gi; | |
9509 | kernel_cap_t cap; | |
9510 | unsigned __capi; | |
9511 | int g; | |
9512 | ||
9513 | seq_printf(m, "%5d\n", id); | |
9514 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9515 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9516 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9517 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9518 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9519 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9520 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9521 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9522 | seq_puts(m, "\n\tGroups:\t"); | |
9523 | gi = cred->group_info; | |
9524 | for (g = 0; g < gi->ngroups; g++) { | |
9525 | seq_put_decimal_ull(m, g ? " " : "", | |
9526 | from_kgid_munged(uns, gi->gid[g])); | |
9527 | } | |
9528 | seq_puts(m, "\n\tCapEff:\t"); | |
9529 | cap = cred->cap_effective; | |
9530 | CAP_FOR_EACH_U32(__capi) | |
9531 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9532 | seq_putc(m, '\n'); | |
9533 | return 0; | |
9534 | } | |
9535 | ||
9536 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9537 | { | |
dbbe9c64 | 9538 | struct io_sq_data *sq = NULL; |
fad8e0de | 9539 | bool has_lock; |
87ce955b JA |
9540 | int i; |
9541 | ||
fad8e0de JA |
9542 | /* |
9543 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9544 | * since fdinfo case grabs it in the opposite direction of normal use | |
9545 | * cases. If we fail to get the lock, we just don't iterate any | |
9546 | * structures that could be going away outside the io_uring mutex. | |
9547 | */ | |
9548 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9549 | ||
dbbe9c64 JQ |
9550 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9551 | sq = ctx->sq_data; | |
9552 | ||
9553 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9554 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9555 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9556 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9557 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9558 | |
87ce955b JA |
9559 | if (f) |
9560 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9561 | else | |
9562 | seq_printf(m, "%5u: <none>\n", i); | |
9563 | } | |
9564 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9565 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9566 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9567 | ||
9568 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9569 | (unsigned int) buf->len); | |
9570 | } | |
fad8e0de | 9571 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9572 | seq_printf(m, "Personalities:\n"); |
9573 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9574 | } | |
d7718a9d JA |
9575 | seq_printf(m, "PollList:\n"); |
9576 | spin_lock_irq(&ctx->completion_lock); | |
9577 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9578 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9579 | struct io_kiocb *req; | |
9580 | ||
9581 | hlist_for_each_entry(req, list, hash_node) | |
9582 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9583 | req->task->task_works != NULL); | |
9584 | } | |
9585 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9586 | if (has_lock) |
9587 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9588 | } |
9589 | ||
9590 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9591 | { | |
9592 | struct io_ring_ctx *ctx = f->private_data; | |
9593 | ||
9594 | if (percpu_ref_tryget(&ctx->refs)) { | |
9595 | __io_uring_show_fdinfo(ctx, m); | |
9596 | percpu_ref_put(&ctx->refs); | |
9597 | } | |
9598 | } | |
bebdb65e | 9599 | #endif |
87ce955b | 9600 | |
2b188cc1 JA |
9601 | static const struct file_operations io_uring_fops = { |
9602 | .release = io_uring_release, | |
fcb323cc | 9603 | .flush = io_uring_flush, |
2b188cc1 | 9604 | .mmap = io_uring_mmap, |
6c5c240e RP |
9605 | #ifndef CONFIG_MMU |
9606 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9607 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9608 | #endif | |
2b188cc1 JA |
9609 | .poll = io_uring_poll, |
9610 | .fasync = io_uring_fasync, | |
bebdb65e | 9611 | #ifdef CONFIG_PROC_FS |
87ce955b | 9612 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9613 | #endif |
2b188cc1 JA |
9614 | }; |
9615 | ||
9616 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9617 | struct io_uring_params *p) | |
9618 | { | |
75b28aff HV |
9619 | struct io_rings *rings; |
9620 | size_t size, sq_array_offset; | |
2b188cc1 | 9621 | |
bd740481 JA |
9622 | /* make sure these are sane, as we already accounted them */ |
9623 | ctx->sq_entries = p->sq_entries; | |
9624 | ctx->cq_entries = p->cq_entries; | |
9625 | ||
75b28aff HV |
9626 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9627 | if (size == SIZE_MAX) | |
9628 | return -EOVERFLOW; | |
9629 | ||
9630 | rings = io_mem_alloc(size); | |
9631 | if (!rings) | |
2b188cc1 JA |
9632 | return -ENOMEM; |
9633 | ||
75b28aff HV |
9634 | ctx->rings = rings; |
9635 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9636 | rings->sq_ring_mask = p->sq_entries - 1; | |
9637 | rings->cq_ring_mask = p->cq_entries - 1; | |
9638 | rings->sq_ring_entries = p->sq_entries; | |
9639 | rings->cq_ring_entries = p->cq_entries; | |
9640 | ctx->sq_mask = rings->sq_ring_mask; | |
9641 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9642 | |
9643 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9644 | if (size == SIZE_MAX) { |
9645 | io_mem_free(ctx->rings); | |
9646 | ctx->rings = NULL; | |
2b188cc1 | 9647 | return -EOVERFLOW; |
eb065d30 | 9648 | } |
2b188cc1 JA |
9649 | |
9650 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9651 | if (!ctx->sq_sqes) { |
9652 | io_mem_free(ctx->rings); | |
9653 | ctx->rings = NULL; | |
2b188cc1 | 9654 | return -ENOMEM; |
eb065d30 | 9655 | } |
2b188cc1 | 9656 | |
2b188cc1 JA |
9657 | return 0; |
9658 | } | |
9659 | ||
9faadcc8 PB |
9660 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9661 | { | |
9662 | int ret, fd; | |
9663 | ||
9664 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9665 | if (fd < 0) | |
9666 | return fd; | |
9667 | ||
9668 | ret = io_uring_add_task_file(ctx, file); | |
9669 | if (ret) { | |
9670 | put_unused_fd(fd); | |
9671 | return ret; | |
9672 | } | |
9673 | fd_install(fd, file); | |
9674 | return fd; | |
9675 | } | |
9676 | ||
2b188cc1 JA |
9677 | /* |
9678 | * Allocate an anonymous fd, this is what constitutes the application | |
9679 | * visible backing of an io_uring instance. The application mmaps this | |
9680 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9681 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9682 | */ | |
9faadcc8 | 9683 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9684 | { |
9685 | struct file *file; | |
9faadcc8 | 9686 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9687 | int ret; |
9688 | ||
2b188cc1 JA |
9689 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9690 | &ctx->ring_sock); | |
9691 | if (ret) | |
9faadcc8 | 9692 | return ERR_PTR(ret); |
2b188cc1 JA |
9693 | #endif |
9694 | ||
2b188cc1 JA |
9695 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9696 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9697 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9698 | if (IS_ERR(file)) { |
9699 | sock_release(ctx->ring_sock); | |
9700 | ctx->ring_sock = NULL; | |
9701 | } else { | |
9702 | ctx->ring_sock->file = file; | |
0f212204 | 9703 | } |
2b188cc1 | 9704 | #endif |
9faadcc8 | 9705 | return file; |
2b188cc1 JA |
9706 | } |
9707 | ||
7f13657d XW |
9708 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9709 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9710 | { |
9711 | struct user_struct *user = NULL; | |
9712 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9713 | struct file *file; |
2b188cc1 JA |
9714 | int ret; |
9715 | ||
8110c1a6 | 9716 | if (!entries) |
2b188cc1 | 9717 | return -EINVAL; |
8110c1a6 JA |
9718 | if (entries > IORING_MAX_ENTRIES) { |
9719 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9720 | return -EINVAL; | |
9721 | entries = IORING_MAX_ENTRIES; | |
9722 | } | |
2b188cc1 JA |
9723 | |
9724 | /* | |
9725 | * Use twice as many entries for the CQ ring. It's possible for the | |
9726 | * application to drive a higher depth than the size of the SQ ring, | |
9727 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9728 | * some flexibility in overcommitting a bit. If the application has |
9729 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9730 | * of CQ ring entries manually. | |
2b188cc1 JA |
9731 | */ |
9732 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9733 | if (p->flags & IORING_SETUP_CQSIZE) { |
9734 | /* | |
9735 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9736 | * to a power-of-two, if it isn't already. We do NOT impose | |
9737 | * any cq vs sq ring sizing. | |
9738 | */ | |
eb2667b3 | 9739 | if (!p->cq_entries) |
33a107f0 | 9740 | return -EINVAL; |
8110c1a6 JA |
9741 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9742 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9743 | return -EINVAL; | |
9744 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9745 | } | |
eb2667b3 JQ |
9746 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9747 | if (p->cq_entries < p->sq_entries) | |
9748 | return -EINVAL; | |
33a107f0 JA |
9749 | } else { |
9750 | p->cq_entries = 2 * p->sq_entries; | |
9751 | } | |
2b188cc1 JA |
9752 | |
9753 | user = get_uid(current_user()); | |
2b188cc1 JA |
9754 | |
9755 | ctx = io_ring_ctx_alloc(p); | |
9756 | if (!ctx) { | |
2b188cc1 JA |
9757 | free_uid(user); |
9758 | return -ENOMEM; | |
9759 | } | |
9760 | ctx->compat = in_compat_syscall(); | |
26bfa89e | 9761 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9762 | ctx->user = user; |
0b8c0ec7 | 9763 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9764 | #ifdef CONFIG_AUDIT |
9765 | ctx->loginuid = current->loginuid; | |
9766 | ctx->sessionid = current->sessionid; | |
9767 | #endif | |
2aede0e4 JA |
9768 | ctx->sqo_task = get_task_struct(current); |
9769 | ||
9770 | /* | |
9771 | * This is just grabbed for accounting purposes. When a process exits, | |
9772 | * the mm is exited and dropped before the files, hence we need to hang | |
9773 | * on to this mm purely for the purposes of being able to unaccount | |
9774 | * memory (locked/pinned vm). It's not used for anything else. | |
9775 | */ | |
6b7898eb | 9776 | mmgrab(current->mm); |
2aede0e4 | 9777 | ctx->mm_account = current->mm; |
6b7898eb | 9778 | |
91d8f519 DZ |
9779 | #ifdef CONFIG_BLK_CGROUP |
9780 | /* | |
9781 | * The sq thread will belong to the original cgroup it was inited in. | |
9782 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9783 | * issued bios will be associated with the closest cgroup later in the | |
9784 | * block layer. | |
9785 | */ | |
9786 | rcu_read_lock(); | |
9787 | ctx->sqo_blkcg_css = blkcg_css(); | |
9788 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9789 | rcu_read_unlock(); | |
9790 | if (!ret) { | |
9791 | /* don't init against a dying cgroup, have the user try again */ | |
9792 | ctx->sqo_blkcg_css = NULL; | |
9793 | ret = -ENODEV; | |
9794 | goto err; | |
9795 | } | |
9796 | #endif | |
2b188cc1 JA |
9797 | ret = io_allocate_scq_urings(ctx, p); |
9798 | if (ret) | |
9799 | goto err; | |
9800 | ||
7e84e1c7 | 9801 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9802 | if (ret) |
9803 | goto err; | |
9804 | ||
7e84e1c7 SG |
9805 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9806 | io_sq_offload_start(ctx); | |
9807 | ||
2b188cc1 | 9808 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9809 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9810 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9811 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9812 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9813 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9814 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9815 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9816 | |
9817 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9818 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9819 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9820 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9821 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9822 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9823 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9824 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9825 | |
7f13657d XW |
9826 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9827 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9828 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9829 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9830 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9831 | |
9832 | if (copy_to_user(params, p, sizeof(*p))) { | |
9833 | ret = -EFAULT; | |
9834 | goto err; | |
9835 | } | |
d1719f70 | 9836 | |
9faadcc8 PB |
9837 | file = io_uring_get_file(ctx); |
9838 | if (IS_ERR(file)) { | |
9839 | ret = PTR_ERR(file); | |
9840 | goto err; | |
9841 | } | |
9842 | ||
044c1ab3 JA |
9843 | /* |
9844 | * Install ring fd as the very last thing, so we don't risk someone | |
9845 | * having closed it before we finish setup | |
9846 | */ | |
9faadcc8 PB |
9847 | ret = io_uring_install_fd(ctx, file); |
9848 | if (ret < 0) { | |
06585c49 | 9849 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9850 | /* fput will clean it up */ |
9851 | fput(file); | |
9852 | return ret; | |
9853 | } | |
044c1ab3 | 9854 | |
c826bd7a | 9855 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9856 | return ret; |
9857 | err: | |
d9d05217 | 9858 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9859 | io_ring_ctx_wait_and_kill(ctx); |
9860 | return ret; | |
9861 | } | |
9862 | ||
9863 | /* | |
9864 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9865 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9866 | * params structure passed in. | |
9867 | */ | |
9868 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9869 | { | |
9870 | struct io_uring_params p; | |
2b188cc1 JA |
9871 | int i; |
9872 | ||
9873 | if (copy_from_user(&p, params, sizeof(p))) | |
9874 | return -EFAULT; | |
9875 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9876 | if (p.resv[i]) | |
9877 | return -EINVAL; | |
9878 | } | |
9879 | ||
6c271ce2 | 9880 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9881 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9882 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9883 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9884 | return -EINVAL; |
9885 | ||
7f13657d | 9886 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9887 | } |
9888 | ||
9889 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9890 | struct io_uring_params __user *, params) | |
9891 | { | |
9892 | return io_uring_setup(entries, params); | |
9893 | } | |
9894 | ||
66f4af93 JA |
9895 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9896 | { | |
9897 | struct io_uring_probe *p; | |
9898 | size_t size; | |
9899 | int i, ret; | |
9900 | ||
9901 | size = struct_size(p, ops, nr_args); | |
9902 | if (size == SIZE_MAX) | |
9903 | return -EOVERFLOW; | |
9904 | p = kzalloc(size, GFP_KERNEL); | |
9905 | if (!p) | |
9906 | return -ENOMEM; | |
9907 | ||
9908 | ret = -EFAULT; | |
9909 | if (copy_from_user(p, arg, size)) | |
9910 | goto out; | |
9911 | ret = -EINVAL; | |
9912 | if (memchr_inv(p, 0, size)) | |
9913 | goto out; | |
9914 | ||
9915 | p->last_op = IORING_OP_LAST - 1; | |
9916 | if (nr_args > IORING_OP_LAST) | |
9917 | nr_args = IORING_OP_LAST; | |
9918 | ||
9919 | for (i = 0; i < nr_args; i++) { | |
9920 | p->ops[i].op = i; | |
9921 | if (!io_op_defs[i].not_supported) | |
9922 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9923 | } | |
9924 | p->ops_len = i; | |
9925 | ||
9926 | ret = 0; | |
9927 | if (copy_to_user(arg, p, size)) | |
9928 | ret = -EFAULT; | |
9929 | out: | |
9930 | kfree(p); | |
9931 | return ret; | |
9932 | } | |
9933 | ||
071698e1 JA |
9934 | static int io_register_personality(struct io_ring_ctx *ctx) |
9935 | { | |
1e6fa521 JA |
9936 | struct io_identity *id; |
9937 | int ret; | |
071698e1 | 9938 | |
1e6fa521 JA |
9939 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9940 | if (unlikely(!id)) | |
9941 | return -ENOMEM; | |
9942 | ||
9943 | io_init_identity(id); | |
9944 | id->creds = get_current_cred(); | |
9945 | ||
9946 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9947 | if (ret < 0) { | |
9948 | put_cred(id->creds); | |
9949 | kfree(id); | |
9950 | } | |
9951 | return ret; | |
071698e1 JA |
9952 | } |
9953 | ||
21b55dbc SG |
9954 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9955 | unsigned int nr_args) | |
9956 | { | |
9957 | struct io_uring_restriction *res; | |
9958 | size_t size; | |
9959 | int i, ret; | |
9960 | ||
7e84e1c7 SG |
9961 | /* Restrictions allowed only if rings started disabled */ |
9962 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9963 | return -EBADFD; | |
9964 | ||
21b55dbc | 9965 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9966 | if (ctx->restrictions.registered) |
21b55dbc SG |
9967 | return -EBUSY; |
9968 | ||
9969 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9970 | return -EINVAL; | |
9971 | ||
9972 | size = array_size(nr_args, sizeof(*res)); | |
9973 | if (size == SIZE_MAX) | |
9974 | return -EOVERFLOW; | |
9975 | ||
9976 | res = memdup_user(arg, size); | |
9977 | if (IS_ERR(res)) | |
9978 | return PTR_ERR(res); | |
9979 | ||
9980 | ret = 0; | |
9981 | ||
9982 | for (i = 0; i < nr_args; i++) { | |
9983 | switch (res[i].opcode) { | |
9984 | case IORING_RESTRICTION_REGISTER_OP: | |
9985 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9986 | ret = -EINVAL; | |
9987 | goto out; | |
9988 | } | |
9989 | ||
9990 | __set_bit(res[i].register_op, | |
9991 | ctx->restrictions.register_op); | |
9992 | break; | |
9993 | case IORING_RESTRICTION_SQE_OP: | |
9994 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9995 | ret = -EINVAL; | |
9996 | goto out; | |
9997 | } | |
9998 | ||
9999 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10000 | break; | |
10001 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10002 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10003 | break; | |
10004 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10005 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10006 | break; | |
10007 | default: | |
10008 | ret = -EINVAL; | |
10009 | goto out; | |
10010 | } | |
10011 | } | |
10012 | ||
10013 | out: | |
10014 | /* Reset all restrictions if an error happened */ | |
10015 | if (ret != 0) | |
10016 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10017 | else | |
7e84e1c7 | 10018 | ctx->restrictions.registered = true; |
21b55dbc SG |
10019 | |
10020 | kfree(res); | |
10021 | return ret; | |
10022 | } | |
10023 | ||
7e84e1c7 SG |
10024 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10025 | { | |
10026 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10027 | return -EBADFD; | |
10028 | ||
10029 | if (ctx->restrictions.registered) | |
10030 | ctx->restricted = 1; | |
10031 | ||
10032 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
10033 | ||
10034 | io_sq_offload_start(ctx); | |
10035 | ||
10036 | return 0; | |
10037 | } | |
10038 | ||
071698e1 JA |
10039 | static bool io_register_op_must_quiesce(int op) |
10040 | { | |
10041 | switch (op) { | |
10042 | case IORING_UNREGISTER_FILES: | |
10043 | case IORING_REGISTER_FILES_UPDATE: | |
10044 | case IORING_REGISTER_PROBE: | |
10045 | case IORING_REGISTER_PERSONALITY: | |
10046 | case IORING_UNREGISTER_PERSONALITY: | |
10047 | return false; | |
10048 | default: | |
10049 | return true; | |
10050 | } | |
10051 | } | |
10052 | ||
edafccee JA |
10053 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10054 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10055 | __releases(ctx->uring_lock) |
10056 | __acquires(ctx->uring_lock) | |
edafccee JA |
10057 | { |
10058 | int ret; | |
10059 | ||
35fa71a0 JA |
10060 | /* |
10061 | * We're inside the ring mutex, if the ref is already dying, then | |
10062 | * someone else killed the ctx or is already going through | |
10063 | * io_uring_register(). | |
10064 | */ | |
10065 | if (percpu_ref_is_dying(&ctx->refs)) | |
10066 | return -ENXIO; | |
10067 | ||
071698e1 | 10068 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10069 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10070 | |
05f3fb3c JA |
10071 | /* |
10072 | * Drop uring mutex before waiting for references to exit. If | |
10073 | * another thread is currently inside io_uring_enter() it might | |
10074 | * need to grab the uring_lock to make progress. If we hold it | |
10075 | * here across the drain wait, then we can deadlock. It's safe | |
10076 | * to drop the mutex here, since no new references will come in | |
10077 | * after we've killed the percpu ref. | |
10078 | */ | |
10079 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10080 | do { |
10081 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10082 | if (!ret) | |
10083 | break; | |
ed6930c9 JA |
10084 | ret = io_run_task_work_sig(); |
10085 | if (ret < 0) | |
10086 | break; | |
af9c1a44 JA |
10087 | } while (1); |
10088 | ||
05f3fb3c | 10089 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10090 | |
88f171ab PB |
10091 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
10092 | return ret; | |
21b55dbc SG |
10093 | } |
10094 | ||
10095 | if (ctx->restricted) { | |
10096 | if (opcode >= IORING_REGISTER_LAST) { | |
10097 | ret = -EINVAL; | |
10098 | goto out; | |
10099 | } | |
10100 | ||
10101 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
10102 | ret = -EACCES; | |
c150368b JA |
10103 | goto out; |
10104 | } | |
05f3fb3c | 10105 | } |
edafccee JA |
10106 | |
10107 | switch (opcode) { | |
10108 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 10109 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
10110 | break; |
10111 | case IORING_UNREGISTER_BUFFERS: | |
10112 | ret = -EINVAL; | |
10113 | if (arg || nr_args) | |
10114 | break; | |
0a96bbe4 | 10115 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10116 | break; |
6b06314c JA |
10117 | case IORING_REGISTER_FILES: |
10118 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
10119 | break; | |
10120 | case IORING_UNREGISTER_FILES: | |
10121 | ret = -EINVAL; | |
10122 | if (arg || nr_args) | |
10123 | break; | |
10124 | ret = io_sqe_files_unregister(ctx); | |
10125 | break; | |
c3a31e60 JA |
10126 | case IORING_REGISTER_FILES_UPDATE: |
10127 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
10128 | break; | |
9b402849 | 10129 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10130 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10131 | ret = -EINVAL; |
10132 | if (nr_args != 1) | |
10133 | break; | |
10134 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10135 | if (ret) |
10136 | break; | |
10137 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10138 | ctx->eventfd_async = 1; | |
10139 | else | |
10140 | ctx->eventfd_async = 0; | |
9b402849 JA |
10141 | break; |
10142 | case IORING_UNREGISTER_EVENTFD: | |
10143 | ret = -EINVAL; | |
10144 | if (arg || nr_args) | |
10145 | break; | |
10146 | ret = io_eventfd_unregister(ctx); | |
10147 | break; | |
66f4af93 JA |
10148 | case IORING_REGISTER_PROBE: |
10149 | ret = -EINVAL; | |
10150 | if (!arg || nr_args > 256) | |
10151 | break; | |
10152 | ret = io_probe(ctx, arg, nr_args); | |
10153 | break; | |
071698e1 JA |
10154 | case IORING_REGISTER_PERSONALITY: |
10155 | ret = -EINVAL; | |
10156 | if (arg || nr_args) | |
10157 | break; | |
10158 | ret = io_register_personality(ctx); | |
10159 | break; | |
10160 | case IORING_UNREGISTER_PERSONALITY: | |
10161 | ret = -EINVAL; | |
10162 | if (arg) | |
10163 | break; | |
10164 | ret = io_unregister_personality(ctx, nr_args); | |
10165 | break; | |
7e84e1c7 SG |
10166 | case IORING_REGISTER_ENABLE_RINGS: |
10167 | ret = -EINVAL; | |
10168 | if (arg || nr_args) | |
10169 | break; | |
10170 | ret = io_register_enable_rings(ctx); | |
10171 | break; | |
21b55dbc SG |
10172 | case IORING_REGISTER_RESTRICTIONS: |
10173 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10174 | break; | |
edafccee JA |
10175 | default: |
10176 | ret = -EINVAL; | |
10177 | break; | |
10178 | } | |
10179 | ||
21b55dbc | 10180 | out: |
071698e1 | 10181 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10182 | /* bring the ctx back to life */ |
05f3fb3c | 10183 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10184 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10185 | } |
edafccee JA |
10186 | return ret; |
10187 | } | |
10188 | ||
10189 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10190 | void __user *, arg, unsigned int, nr_args) | |
10191 | { | |
10192 | struct io_ring_ctx *ctx; | |
10193 | long ret = -EBADF; | |
10194 | struct fd f; | |
10195 | ||
10196 | f = fdget(fd); | |
10197 | if (!f.file) | |
10198 | return -EBADF; | |
10199 | ||
10200 | ret = -EOPNOTSUPP; | |
10201 | if (f.file->f_op != &io_uring_fops) | |
10202 | goto out_fput; | |
10203 | ||
10204 | ctx = f.file->private_data; | |
10205 | ||
b6c23dd5 PB |
10206 | io_run_task_work(); |
10207 | ||
edafccee JA |
10208 | mutex_lock(&ctx->uring_lock); |
10209 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10210 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10211 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10212 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10213 | out_fput: |
10214 | fdput(f); | |
10215 | return ret; | |
10216 | } | |
10217 | ||
2b188cc1 JA |
10218 | static int __init io_uring_init(void) |
10219 | { | |
d7f62e82 SM |
10220 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10221 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10222 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10223 | } while (0) | |
10224 | ||
10225 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10226 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10227 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10228 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10229 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10230 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10231 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10232 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10233 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10234 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10235 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10236 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10237 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10238 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10239 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10240 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10241 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10242 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10243 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10244 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10245 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10246 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10247 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10248 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10249 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10250 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10251 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10252 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10253 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10254 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10255 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10256 | |
d3656344 | 10257 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10258 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
10259 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10260 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10261 | return 0; |
10262 | }; | |
10263 | __initcall(io_uring_init); |