]>
Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
14 | * through a control-dependency in io_get_cqring (smp_store_release to | |
15 | * store head will do). Failure to do so could lead to reading invalid | |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
6c271ce2 | 60 | #include <linux/kthread.h> |
2b188cc1 | 61 | #include <linux/blkdev.h> |
edafccee | 62 | #include <linux/bvec.h> |
2b188cc1 JA |
63 | #include <linux/net.h> |
64 | #include <net/sock.h> | |
65 | #include <net/af_unix.h> | |
6b06314c | 66 | #include <net/scm.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
ff002b30 | 78 | #include <linux/fs_struct.h> |
7d67af2c | 79 | #include <linux/splice.h> |
b41e9852 | 80 | #include <linux/task_work.h> |
bcf5a063 | 81 | #include <linux/pagemap.h> |
0f212204 | 82 | #include <linux/io_uring.h> |
91d8f519 | 83 | #include <linux/blk-cgroup.h> |
4ea33a97 | 84 | #include <linux/audit.h> |
2b188cc1 | 85 | |
c826bd7a DD |
86 | #define CREATE_TRACE_POINTS |
87 | #include <trace/events/io_uring.h> | |
88 | ||
2b188cc1 JA |
89 | #include <uapi/linux/io_uring.h> |
90 | ||
91 | #include "internal.h" | |
561fb04a | 92 | #include "io-wq.h" |
2b188cc1 | 93 | |
5277deaa | 94 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
96 | |
97 | /* | |
98 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
99 | */ | |
100 | #define IORING_FILE_TABLE_SHIFT 9 | |
101 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
102 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
103 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
104 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
105 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 JA |
106 | |
107 | struct io_uring { | |
108 | u32 head ____cacheline_aligned_in_smp; | |
109 | u32 tail ____cacheline_aligned_in_smp; | |
110 | }; | |
111 | ||
1e84b97b | 112 | /* |
75b28aff HV |
113 | * This data is shared with the application through the mmap at offsets |
114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
115 | * |
116 | * The offsets to the member fields are published through struct | |
117 | * io_sqring_offsets when calling io_uring_setup. | |
118 | */ | |
75b28aff | 119 | struct io_rings { |
1e84b97b SB |
120 | /* |
121 | * Head and tail offsets into the ring; the offsets need to be | |
122 | * masked to get valid indices. | |
123 | * | |
75b28aff HV |
124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
125 | * and the application controls tail of the sq ring and the head of the | |
126 | * cq ring. | |
1e84b97b | 127 | */ |
75b28aff | 128 | struct io_uring sq, cq; |
1e84b97b | 129 | /* |
75b28aff | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
131 | * ring_entries - 1) |
132 | */ | |
75b28aff HV |
133 | u32 sq_ring_mask, cq_ring_mask; |
134 | /* Ring sizes (constant, power of 2) */ | |
135 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
136 | /* |
137 | * Number of invalid entries dropped by the kernel due to | |
138 | * invalid index stored in array | |
139 | * | |
140 | * Written by the kernel, shouldn't be modified by the | |
141 | * application (i.e. get number of "new events" by comparing to | |
142 | * cached value). | |
143 | * | |
144 | * After a new SQ head value was read by the application this | |
145 | * counter includes all submissions that were dropped reaching | |
146 | * the new SQ head (and possibly more). | |
147 | */ | |
75b28aff | 148 | u32 sq_dropped; |
1e84b97b | 149 | /* |
0d9b5b3a | 150 | * Runtime SQ flags |
1e84b97b SB |
151 | * |
152 | * Written by the kernel, shouldn't be modified by the | |
153 | * application. | |
154 | * | |
155 | * The application needs a full memory barrier before checking | |
156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
157 | */ | |
75b28aff | 158 | u32 sq_flags; |
0d9b5b3a SG |
159 | /* |
160 | * Runtime CQ flags | |
161 | * | |
162 | * Written by the application, shouldn't be modified by the | |
163 | * kernel. | |
164 | */ | |
165 | u32 cq_flags; | |
1e84b97b SB |
166 | /* |
167 | * Number of completion events lost because the queue was full; | |
168 | * this should be avoided by the application by making sure | |
0b4295b5 | 169 | * there are not more requests pending than there is space in |
1e84b97b SB |
170 | * the completion queue. |
171 | * | |
172 | * Written by the kernel, shouldn't be modified by the | |
173 | * application (i.e. get number of "new events" by comparing to | |
174 | * cached value). | |
175 | * | |
176 | * As completion events come in out of order this counter is not | |
177 | * ordered with any other data. | |
178 | */ | |
75b28aff | 179 | u32 cq_overflow; |
1e84b97b SB |
180 | /* |
181 | * Ring buffer of completion events. | |
182 | * | |
183 | * The kernel writes completion events fresh every time they are | |
184 | * produced, so the application is allowed to modify pending | |
185 | * entries. | |
186 | */ | |
75b28aff | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
188 | }; |
189 | ||
edafccee JA |
190 | struct io_mapped_ubuf { |
191 | u64 ubuf; | |
192 | size_t len; | |
193 | struct bio_vec *bvec; | |
194 | unsigned int nr_bvecs; | |
de293938 | 195 | unsigned long acct_pages; |
edafccee JA |
196 | }; |
197 | ||
65e19f54 JA |
198 | struct fixed_file_table { |
199 | struct file **files; | |
31b51510 JA |
200 | }; |
201 | ||
05589553 XW |
202 | struct fixed_file_ref_node { |
203 | struct percpu_ref refs; | |
204 | struct list_head node; | |
205 | struct list_head file_list; | |
206 | struct fixed_file_data *file_data; | |
4a38aed2 | 207 | struct llist_node llist; |
e297822b | 208 | bool done; |
05589553 XW |
209 | }; |
210 | ||
05f3fb3c JA |
211 | struct fixed_file_data { |
212 | struct fixed_file_table *table; | |
213 | struct io_ring_ctx *ctx; | |
214 | ||
b2e96852 | 215 | struct fixed_file_ref_node *node; |
05f3fb3c | 216 | struct percpu_ref refs; |
05f3fb3c | 217 | struct completion done; |
05589553 XW |
218 | struct list_head ref_list; |
219 | spinlock_t lock; | |
05f3fb3c JA |
220 | }; |
221 | ||
5a2e745d JA |
222 | struct io_buffer { |
223 | struct list_head list; | |
224 | __u64 addr; | |
225 | __s32 len; | |
226 | __u16 bid; | |
227 | }; | |
228 | ||
21b55dbc SG |
229 | struct io_restriction { |
230 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
231 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
232 | u8 sqe_flags_allowed; | |
233 | u8 sqe_flags_required; | |
7e84e1c7 | 234 | bool registered; |
21b55dbc SG |
235 | }; |
236 | ||
534ca6d6 JA |
237 | struct io_sq_data { |
238 | refcount_t refs; | |
69fb2131 JA |
239 | struct mutex lock; |
240 | ||
241 | /* ctx's that are using this sqd */ | |
242 | struct list_head ctx_list; | |
243 | struct list_head ctx_new_list; | |
244 | struct mutex ctx_lock; | |
245 | ||
534ca6d6 JA |
246 | struct task_struct *thread; |
247 | struct wait_queue_head wait; | |
248 | }; | |
249 | ||
2b188cc1 JA |
250 | struct io_ring_ctx { |
251 | struct { | |
252 | struct percpu_ref refs; | |
253 | } ____cacheline_aligned_in_smp; | |
254 | ||
255 | struct { | |
256 | unsigned int flags; | |
e1d85334 | 257 | unsigned int compat: 1; |
aad5d8da | 258 | unsigned int limit_mem: 1; |
e1d85334 RD |
259 | unsigned int cq_overflow_flushed: 1; |
260 | unsigned int drain_next: 1; | |
261 | unsigned int eventfd_async: 1; | |
21b55dbc | 262 | unsigned int restricted: 1; |
2b188cc1 | 263 | |
75b28aff HV |
264 | /* |
265 | * Ring buffer of indices into array of io_uring_sqe, which is | |
266 | * mmapped by the application using the IORING_OFF_SQES offset. | |
267 | * | |
268 | * This indirection could e.g. be used to assign fixed | |
269 | * io_uring_sqe entries to operations and only submit them to | |
270 | * the queue when needed. | |
271 | * | |
272 | * The kernel modifies neither the indices array nor the entries | |
273 | * array. | |
274 | */ | |
275 | u32 *sq_array; | |
2b188cc1 JA |
276 | unsigned cached_sq_head; |
277 | unsigned sq_entries; | |
278 | unsigned sq_mask; | |
6c271ce2 | 279 | unsigned sq_thread_idle; |
498ccd9e | 280 | unsigned cached_sq_dropped; |
2c3bac6d | 281 | unsigned cached_cq_overflow; |
ad3eb2c8 | 282 | unsigned long sq_check_overflow; |
de0617e4 JA |
283 | |
284 | struct list_head defer_list; | |
5262f567 | 285 | struct list_head timeout_list; |
1d7bb1d5 | 286 | struct list_head cq_overflow_list; |
fcb323cc JA |
287 | |
288 | wait_queue_head_t inflight_wait; | |
ad3eb2c8 | 289 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
290 | } ____cacheline_aligned_in_smp; |
291 | ||
206aefde JA |
292 | struct io_rings *rings; |
293 | ||
2b188cc1 | 294 | /* IO offload */ |
561fb04a | 295 | struct io_wq *io_wq; |
2aede0e4 JA |
296 | |
297 | /* | |
298 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
299 | * have access to the ->files | |
300 | */ | |
301 | struct task_struct *sqo_task; | |
302 | ||
303 | /* Only used for accounting purposes */ | |
304 | struct mm_struct *mm_account; | |
305 | ||
91d8f519 DZ |
306 | #ifdef CONFIG_BLK_CGROUP |
307 | struct cgroup_subsys_state *sqo_blkcg_css; | |
308 | #endif | |
309 | ||
534ca6d6 JA |
310 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
311 | ||
90554200 | 312 | struct wait_queue_head sqo_sq_wait; |
6a779382 | 313 | struct wait_queue_entry sqo_wait_entry; |
69fb2131 | 314 | struct list_head sqd_list; |
75b28aff | 315 | |
6b06314c JA |
316 | /* |
317 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
318 | * readers must ensure that ->refs is alive as long as the file* is | |
319 | * used. Only updated through io_uring_register(2). | |
320 | */ | |
05f3fb3c | 321 | struct fixed_file_data *file_data; |
6b06314c JA |
322 | unsigned nr_user_files; |
323 | ||
edafccee JA |
324 | /* if used, fixed mapped user buffers */ |
325 | unsigned nr_user_bufs; | |
326 | struct io_mapped_ubuf *user_bufs; | |
327 | ||
2b188cc1 JA |
328 | struct user_struct *user; |
329 | ||
0b8c0ec7 | 330 | const struct cred *creds; |
181e448d | 331 | |
4ea33a97 JA |
332 | #ifdef CONFIG_AUDIT |
333 | kuid_t loginuid; | |
334 | unsigned int sessionid; | |
335 | #endif | |
336 | ||
0f158b4c JA |
337 | struct completion ref_comp; |
338 | struct completion sq_thread_comp; | |
206aefde | 339 | |
0ddf92e8 JA |
340 | /* if all else fails... */ |
341 | struct io_kiocb *fallback_req; | |
342 | ||
206aefde JA |
343 | #if defined(CONFIG_UNIX) |
344 | struct socket *ring_sock; | |
345 | #endif | |
346 | ||
5a2e745d JA |
347 | struct idr io_buffer_idr; |
348 | ||
071698e1 JA |
349 | struct idr personality_idr; |
350 | ||
206aefde JA |
351 | struct { |
352 | unsigned cached_cq_tail; | |
353 | unsigned cq_entries; | |
354 | unsigned cq_mask; | |
355 | atomic_t cq_timeouts; | |
ad3eb2c8 | 356 | unsigned long cq_check_overflow; |
206aefde JA |
357 | struct wait_queue_head cq_wait; |
358 | struct fasync_struct *cq_fasync; | |
359 | struct eventfd_ctx *cq_ev_fd; | |
360 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
361 | |
362 | struct { | |
363 | struct mutex uring_lock; | |
364 | wait_queue_head_t wait; | |
365 | } ____cacheline_aligned_in_smp; | |
366 | ||
367 | struct { | |
368 | spinlock_t completion_lock; | |
e94f141b | 369 | |
def596e9 | 370 | /* |
540e32a0 | 371 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
372 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
373 | * For SQPOLL, only the single threaded io_sq_thread() will | |
374 | * manipulate the list, hence no extra locking is needed there. | |
375 | */ | |
540e32a0 | 376 | struct list_head iopoll_list; |
78076bb6 JA |
377 | struct hlist_head *cancel_hash; |
378 | unsigned cancel_hash_bits; | |
e94f141b | 379 | bool poll_multi_file; |
31b51510 | 380 | |
fcb323cc JA |
381 | spinlock_t inflight_lock; |
382 | struct list_head inflight_list; | |
2b188cc1 | 383 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 384 | |
4a38aed2 JA |
385 | struct delayed_work file_put_work; |
386 | struct llist_head file_put_llist; | |
387 | ||
85faa7b8 | 388 | struct work_struct exit_work; |
21b55dbc | 389 | struct io_restriction restrictions; |
2b188cc1 JA |
390 | }; |
391 | ||
09bb8394 JA |
392 | /* |
393 | * First field must be the file pointer in all the | |
394 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
395 | */ | |
221c5eb2 JA |
396 | struct io_poll_iocb { |
397 | struct file *file; | |
0969e783 JA |
398 | union { |
399 | struct wait_queue_head *head; | |
400 | u64 addr; | |
401 | }; | |
221c5eb2 | 402 | __poll_t events; |
8c838788 | 403 | bool done; |
221c5eb2 | 404 | bool canceled; |
392edb45 | 405 | struct wait_queue_entry wait; |
221c5eb2 JA |
406 | }; |
407 | ||
b5dba59e JA |
408 | struct io_close { |
409 | struct file *file; | |
410 | struct file *put_file; | |
411 | int fd; | |
412 | }; | |
413 | ||
ad8a48ac JA |
414 | struct io_timeout_data { |
415 | struct io_kiocb *req; | |
416 | struct hrtimer timer; | |
417 | struct timespec64 ts; | |
418 | enum hrtimer_mode mode; | |
419 | }; | |
420 | ||
8ed8d3c3 JA |
421 | struct io_accept { |
422 | struct file *file; | |
423 | struct sockaddr __user *addr; | |
424 | int __user *addr_len; | |
425 | int flags; | |
09952e3e | 426 | unsigned long nofile; |
8ed8d3c3 JA |
427 | }; |
428 | ||
429 | struct io_sync { | |
430 | struct file *file; | |
431 | loff_t len; | |
432 | loff_t off; | |
433 | int flags; | |
d63d1b5e | 434 | int mode; |
8ed8d3c3 JA |
435 | }; |
436 | ||
fbf23849 JA |
437 | struct io_cancel { |
438 | struct file *file; | |
439 | u64 addr; | |
440 | }; | |
441 | ||
b29472ee JA |
442 | struct io_timeout { |
443 | struct file *file; | |
bfe68a22 PB |
444 | u32 off; |
445 | u32 target_seq; | |
135fcde8 | 446 | struct list_head list; |
b29472ee JA |
447 | }; |
448 | ||
0bdf7a2d PB |
449 | struct io_timeout_rem { |
450 | struct file *file; | |
451 | u64 addr; | |
452 | }; | |
453 | ||
9adbd45d JA |
454 | struct io_rw { |
455 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
456 | struct kiocb kiocb; | |
457 | u64 addr; | |
458 | u64 len; | |
459 | }; | |
460 | ||
3fbb51c1 JA |
461 | struct io_connect { |
462 | struct file *file; | |
463 | struct sockaddr __user *addr; | |
464 | int addr_len; | |
465 | }; | |
466 | ||
e47293fd JA |
467 | struct io_sr_msg { |
468 | struct file *file; | |
fddaface | 469 | union { |
270a5940 | 470 | struct user_msghdr __user *umsg; |
fddaface JA |
471 | void __user *buf; |
472 | }; | |
e47293fd | 473 | int msg_flags; |
bcda7baa | 474 | int bgid; |
fddaface | 475 | size_t len; |
bcda7baa | 476 | struct io_buffer *kbuf; |
e47293fd JA |
477 | }; |
478 | ||
15b71abe JA |
479 | struct io_open { |
480 | struct file *file; | |
481 | int dfd; | |
944d1444 | 482 | bool ignore_nonblock; |
15b71abe | 483 | struct filename *filename; |
c12cedf2 | 484 | struct open_how how; |
4022e7af | 485 | unsigned long nofile; |
15b71abe JA |
486 | }; |
487 | ||
05f3fb3c JA |
488 | struct io_files_update { |
489 | struct file *file; | |
490 | u64 arg; | |
491 | u32 nr_args; | |
492 | u32 offset; | |
493 | }; | |
494 | ||
4840e418 JA |
495 | struct io_fadvise { |
496 | struct file *file; | |
497 | u64 offset; | |
498 | u32 len; | |
499 | u32 advice; | |
500 | }; | |
501 | ||
c1ca757b JA |
502 | struct io_madvise { |
503 | struct file *file; | |
504 | u64 addr; | |
505 | u32 len; | |
506 | u32 advice; | |
507 | }; | |
508 | ||
3e4827b0 JA |
509 | struct io_epoll { |
510 | struct file *file; | |
511 | int epfd; | |
512 | int op; | |
513 | int fd; | |
514 | struct epoll_event event; | |
e47293fd JA |
515 | }; |
516 | ||
7d67af2c PB |
517 | struct io_splice { |
518 | struct file *file_out; | |
519 | struct file *file_in; | |
520 | loff_t off_out; | |
521 | loff_t off_in; | |
522 | u64 len; | |
523 | unsigned int flags; | |
524 | }; | |
525 | ||
ddf0322d JA |
526 | struct io_provide_buf { |
527 | struct file *file; | |
528 | __u64 addr; | |
529 | __s32 len; | |
530 | __u32 bgid; | |
531 | __u16 nbufs; | |
532 | __u16 bid; | |
533 | }; | |
534 | ||
1d9e1288 BM |
535 | struct io_statx { |
536 | struct file *file; | |
537 | int dfd; | |
538 | unsigned int mask; | |
539 | unsigned int flags; | |
e62753e4 | 540 | const char __user *filename; |
1d9e1288 BM |
541 | struct statx __user *buffer; |
542 | }; | |
543 | ||
3ca405eb PB |
544 | struct io_completion { |
545 | struct file *file; | |
546 | struct list_head list; | |
0f7e466b | 547 | int cflags; |
3ca405eb PB |
548 | }; |
549 | ||
f499a021 JA |
550 | struct io_async_connect { |
551 | struct sockaddr_storage address; | |
552 | }; | |
553 | ||
03b1230c JA |
554 | struct io_async_msghdr { |
555 | struct iovec fast_iov[UIO_FASTIOV]; | |
556 | struct iovec *iov; | |
557 | struct sockaddr __user *uaddr; | |
558 | struct msghdr msg; | |
b537916c | 559 | struct sockaddr_storage addr; |
03b1230c JA |
560 | }; |
561 | ||
f67676d1 JA |
562 | struct io_async_rw { |
563 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
564 | const struct iovec *free_iovec; |
565 | struct iov_iter iter; | |
227c0c96 | 566 | size_t bytes_done; |
bcf5a063 | 567 | struct wait_page_queue wpq; |
f67676d1 JA |
568 | }; |
569 | ||
6b47ee6e PB |
570 | enum { |
571 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
572 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
573 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
574 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
575 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 576 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 577 | |
dea3b49c | 578 | REQ_F_LINK_HEAD_BIT, |
6b47ee6e PB |
579 | REQ_F_FAIL_LINK_BIT, |
580 | REQ_F_INFLIGHT_BIT, | |
581 | REQ_F_CUR_POS_BIT, | |
582 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 583 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 584 | REQ_F_ISREG_BIT, |
99bc4c38 | 585 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 586 | REQ_F_POLLED_BIT, |
bcda7baa | 587 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 588 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 589 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 590 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
84557871 JA |
591 | |
592 | /* not a real bit, just to check we're not overflowing the space */ | |
593 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
594 | }; |
595 | ||
596 | enum { | |
597 | /* ctx owns file */ | |
598 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
599 | /* drain existing IO first */ | |
600 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
601 | /* linked sqes */ | |
602 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
603 | /* doesn't sever on completion < 0 */ | |
604 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
605 | /* IOSQE_ASYNC */ | |
606 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
607 | /* IOSQE_BUFFER_SELECT */ |
608 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 609 | |
dea3b49c PB |
610 | /* head of a link */ |
611 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), | |
6b47ee6e PB |
612 | /* fail rest of links */ |
613 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
614 | /* on inflight list */ | |
615 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
616 | /* read/write uses file position */ | |
617 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
618 | /* must not punt to workers */ | |
619 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 620 | /* has or had linked timeout */ |
6b47ee6e | 621 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
622 | /* regular file */ |
623 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
624 | /* needs cleanup */ |
625 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
626 | /* already went through poll handler */ |
627 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
628 | /* buffer already selected */ |
629 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
630 | /* doesn't need file table for this request */ |
631 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
632 | /* io_wq_work is initialized */ |
633 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
634 | /* linked timeout is active, i.e. prepared by link's head */ |
635 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
d7718a9d JA |
636 | }; |
637 | ||
638 | struct async_poll { | |
639 | struct io_poll_iocb poll; | |
807abcb0 | 640 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
641 | }; |
642 | ||
09bb8394 JA |
643 | /* |
644 | * NOTE! Each of the iocb union members has the file pointer | |
645 | * as the first entry in their struct definition. So you can | |
646 | * access the file pointer through any of the sub-structs, | |
647 | * or directly as just 'ki_filp' in this struct. | |
648 | */ | |
2b188cc1 | 649 | struct io_kiocb { |
221c5eb2 | 650 | union { |
09bb8394 | 651 | struct file *file; |
9adbd45d | 652 | struct io_rw rw; |
221c5eb2 | 653 | struct io_poll_iocb poll; |
8ed8d3c3 JA |
654 | struct io_accept accept; |
655 | struct io_sync sync; | |
fbf23849 | 656 | struct io_cancel cancel; |
b29472ee | 657 | struct io_timeout timeout; |
0bdf7a2d | 658 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 659 | struct io_connect connect; |
e47293fd | 660 | struct io_sr_msg sr_msg; |
15b71abe | 661 | struct io_open open; |
b5dba59e | 662 | struct io_close close; |
05f3fb3c | 663 | struct io_files_update files_update; |
4840e418 | 664 | struct io_fadvise fadvise; |
c1ca757b | 665 | struct io_madvise madvise; |
3e4827b0 | 666 | struct io_epoll epoll; |
7d67af2c | 667 | struct io_splice splice; |
ddf0322d | 668 | struct io_provide_buf pbuf; |
1d9e1288 | 669 | struct io_statx statx; |
3ca405eb PB |
670 | /* use only after cleaning per-op data, see io_clean_op() */ |
671 | struct io_completion compl; | |
221c5eb2 | 672 | }; |
2b188cc1 | 673 | |
e8c2bc1f JA |
674 | /* opcode allocated if it needs to store data for async defer */ |
675 | void *async_data; | |
d625c6ee | 676 | u8 opcode; |
65a6543d XW |
677 | /* polled IO has completed */ |
678 | u8 iopoll_completed; | |
2b188cc1 | 679 | |
4f4eeba8 | 680 | u16 buf_index; |
9cf7c104 | 681 | u32 result; |
4f4eeba8 | 682 | |
010e8e6b PB |
683 | struct io_ring_ctx *ctx; |
684 | unsigned int flags; | |
685 | refcount_t refs; | |
686 | struct task_struct *task; | |
687 | u64 user_data; | |
d7718a9d | 688 | |
010e8e6b | 689 | struct list_head link_list; |
fcb323cc | 690 | |
d21ffe7e PB |
691 | /* |
692 | * 1. used with ctx->iopoll_list with reads/writes | |
693 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
694 | */ | |
010e8e6b PB |
695 | struct list_head inflight_entry; |
696 | ||
697 | struct percpu_ref *fixed_file_refs; | |
698 | struct callback_head task_work; | |
699 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
700 | struct hlist_node hash_node; | |
701 | struct async_poll *apoll; | |
702 | struct io_wq_work work; | |
2b188cc1 | 703 | }; |
05589553 | 704 | |
27dc8338 PB |
705 | struct io_defer_entry { |
706 | struct list_head list; | |
707 | struct io_kiocb *req; | |
9cf7c104 | 708 | u32 seq; |
2b188cc1 JA |
709 | }; |
710 | ||
def596e9 | 711 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 712 | |
013538bd JA |
713 | struct io_comp_state { |
714 | unsigned int nr; | |
715 | struct list_head list; | |
716 | struct io_ring_ctx *ctx; | |
717 | }; | |
718 | ||
9a56a232 JA |
719 | struct io_submit_state { |
720 | struct blk_plug plug; | |
721 | ||
2579f913 JA |
722 | /* |
723 | * io_kiocb alloc cache | |
724 | */ | |
725 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 726 | unsigned int free_reqs; |
2579f913 | 727 | |
013538bd JA |
728 | /* |
729 | * Batch completion logic | |
730 | */ | |
731 | struct io_comp_state comp; | |
732 | ||
9a56a232 JA |
733 | /* |
734 | * File reference cache | |
735 | */ | |
736 | struct file *file; | |
737 | unsigned int fd; | |
738 | unsigned int has_refs; | |
9a56a232 JA |
739 | unsigned int ios_left; |
740 | }; | |
741 | ||
d3656344 | 742 | struct io_op_def { |
d3656344 JA |
743 | /* needs req->file assigned */ |
744 | unsigned needs_file : 1; | |
fd2206e4 JA |
745 | /* don't fail if file grab fails */ |
746 | unsigned needs_file_no_error : 1; | |
d3656344 JA |
747 | /* hash wq insertion if file is a regular file */ |
748 | unsigned hash_reg_file : 1; | |
749 | /* unbound wq insertion if file is a non-regular file */ | |
750 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
751 | /* opcode is not supported by this kernel */ |
752 | unsigned not_supported : 1; | |
8a72758c JA |
753 | /* set if opcode supports polled "wait" */ |
754 | unsigned pollin : 1; | |
755 | unsigned pollout : 1; | |
bcda7baa JA |
756 | /* op supports buffer selection */ |
757 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
758 | /* must always have async data allocated */ |
759 | unsigned needs_async_data : 1; | |
760 | /* size of async data needed, if any */ | |
761 | unsigned short async_size; | |
0f203765 | 762 | unsigned work_flags; |
d3656344 JA |
763 | }; |
764 | ||
0918682b | 765 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
766 | [IORING_OP_NOP] = {}, |
767 | [IORING_OP_READV] = { | |
d3656344 JA |
768 | .needs_file = 1, |
769 | .unbound_nonreg_file = 1, | |
8a72758c | 770 | .pollin = 1, |
4d954c25 | 771 | .buffer_select = 1, |
e8c2bc1f JA |
772 | .needs_async_data = 1, |
773 | .async_size = sizeof(struct io_async_rw), | |
0f203765 | 774 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 775 | }, |
0463b6c5 | 776 | [IORING_OP_WRITEV] = { |
d3656344 JA |
777 | .needs_file = 1, |
778 | .hash_reg_file = 1, | |
779 | .unbound_nonreg_file = 1, | |
8a72758c | 780 | .pollout = 1, |
e8c2bc1f JA |
781 | .needs_async_data = 1, |
782 | .async_size = sizeof(struct io_async_rw), | |
69228338 JA |
783 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
784 | IO_WQ_WORK_FSIZE, | |
d3656344 | 785 | }, |
0463b6c5 | 786 | [IORING_OP_FSYNC] = { |
d3656344 | 787 | .needs_file = 1, |
0f203765 | 788 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 789 | }, |
0463b6c5 | 790 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
791 | .needs_file = 1, |
792 | .unbound_nonreg_file = 1, | |
8a72758c | 793 | .pollin = 1, |
e8c2bc1f | 794 | .async_size = sizeof(struct io_async_rw), |
4017eb91 | 795 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
d3656344 | 796 | }, |
0463b6c5 | 797 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
798 | .needs_file = 1, |
799 | .hash_reg_file = 1, | |
800 | .unbound_nonreg_file = 1, | |
8a72758c | 801 | .pollout = 1, |
e8c2bc1f | 802 | .async_size = sizeof(struct io_async_rw), |
4017eb91 JA |
803 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
804 | IO_WQ_WORK_MM, | |
d3656344 | 805 | }, |
0463b6c5 | 806 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
807 | .needs_file = 1, |
808 | .unbound_nonreg_file = 1, | |
809 | }, | |
0463b6c5 PB |
810 | [IORING_OP_POLL_REMOVE] = {}, |
811 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 812 | .needs_file = 1, |
0f203765 | 813 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 814 | }, |
0463b6c5 | 815 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
816 | .needs_file = 1, |
817 | .unbound_nonreg_file = 1, | |
8a72758c | 818 | .pollout = 1, |
e8c2bc1f JA |
819 | .needs_async_data = 1, |
820 | .async_size = sizeof(struct io_async_msghdr), | |
0f203765 JA |
821 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
822 | IO_WQ_WORK_FS, | |
d3656344 | 823 | }, |
0463b6c5 | 824 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
825 | .needs_file = 1, |
826 | .unbound_nonreg_file = 1, | |
8a72758c | 827 | .pollin = 1, |
52de1fe1 | 828 | .buffer_select = 1, |
e8c2bc1f JA |
829 | .needs_async_data = 1, |
830 | .async_size = sizeof(struct io_async_msghdr), | |
0f203765 JA |
831 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
832 | IO_WQ_WORK_FS, | |
d3656344 | 833 | }, |
0463b6c5 | 834 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
835 | .needs_async_data = 1, |
836 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 837 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 838 | }, |
0463b6c5 PB |
839 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
840 | [IORING_OP_ACCEPT] = { | |
d3656344 JA |
841 | .needs_file = 1, |
842 | .unbound_nonreg_file = 1, | |
8a72758c | 843 | .pollin = 1, |
0f203765 | 844 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
d3656344 | 845 | }, |
0463b6c5 PB |
846 | [IORING_OP_ASYNC_CANCEL] = {}, |
847 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
848 | .needs_async_data = 1, |
849 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 850 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 851 | }, |
0463b6c5 | 852 | [IORING_OP_CONNECT] = { |
d3656344 JA |
853 | .needs_file = 1, |
854 | .unbound_nonreg_file = 1, | |
8a72758c | 855 | .pollout = 1, |
e8c2bc1f JA |
856 | .needs_async_data = 1, |
857 | .async_size = sizeof(struct io_async_connect), | |
0f203765 | 858 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 859 | }, |
0463b6c5 | 860 | [IORING_OP_FALLOCATE] = { |
d3656344 | 861 | .needs_file = 1, |
69228338 | 862 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
d3656344 | 863 | }, |
0463b6c5 | 864 | [IORING_OP_OPENAT] = { |
0f203765 JA |
865 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
866 | IO_WQ_WORK_FS, | |
d3656344 | 867 | }, |
0463b6c5 | 868 | [IORING_OP_CLOSE] = { |
fd2206e4 JA |
869 | .needs_file = 1, |
870 | .needs_file_no_error = 1, | |
0f203765 | 871 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 872 | }, |
0463b6c5 | 873 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 874 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 875 | }, |
0463b6c5 | 876 | [IORING_OP_STATX] = { |
0f203765 JA |
877 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
878 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 879 | }, |
0463b6c5 | 880 | [IORING_OP_READ] = { |
3a6820f2 JA |
881 | .needs_file = 1, |
882 | .unbound_nonreg_file = 1, | |
8a72758c | 883 | .pollin = 1, |
bcda7baa | 884 | .buffer_select = 1, |
e8c2bc1f | 885 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 886 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 887 | }, |
0463b6c5 | 888 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
889 | .needs_file = 1, |
890 | .unbound_nonreg_file = 1, | |
8a72758c | 891 | .pollout = 1, |
e8c2bc1f | 892 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
893 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
894 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 895 | }, |
0463b6c5 | 896 | [IORING_OP_FADVISE] = { |
4840e418 | 897 | .needs_file = 1, |
0f203765 | 898 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 899 | }, |
0463b6c5 | 900 | [IORING_OP_MADVISE] = { |
0f203765 | 901 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 902 | }, |
0463b6c5 | 903 | [IORING_OP_SEND] = { |
fddaface JA |
904 | .needs_file = 1, |
905 | .unbound_nonreg_file = 1, | |
8a72758c | 906 | .pollout = 1, |
0f203765 | 907 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 908 | }, |
0463b6c5 | 909 | [IORING_OP_RECV] = { |
fddaface JA |
910 | .needs_file = 1, |
911 | .unbound_nonreg_file = 1, | |
8a72758c | 912 | .pollin = 1, |
bcda7baa | 913 | .buffer_select = 1, |
0f203765 | 914 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 915 | }, |
0463b6c5 | 916 | [IORING_OP_OPENAT2] = { |
0f203765 JA |
917 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
918 | IO_WQ_WORK_BLKCG, | |
cebdb986 | 919 | }, |
3e4827b0 JA |
920 | [IORING_OP_EPOLL_CTL] = { |
921 | .unbound_nonreg_file = 1, | |
0f203765 | 922 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 923 | }, |
7d67af2c PB |
924 | [IORING_OP_SPLICE] = { |
925 | .needs_file = 1, | |
926 | .hash_reg_file = 1, | |
927 | .unbound_nonreg_file = 1, | |
0f203765 | 928 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
929 | }, |
930 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 931 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
932 | [IORING_OP_TEE] = { |
933 | .needs_file = 1, | |
934 | .hash_reg_file = 1, | |
935 | .unbound_nonreg_file = 1, | |
936 | }, | |
d3656344 JA |
937 | }; |
938 | ||
2e0464d4 BM |
939 | enum io_mem_account { |
940 | ACCT_LOCKED, | |
941 | ACCT_PINNED, | |
942 | }; | |
943 | ||
34fb0062 PB |
944 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node); |
945 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( | |
946 | struct io_ring_ctx *ctx); | |
947 | ||
81b68a5c PB |
948 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
949 | struct io_comp_state *cs); | |
78e19bbe | 950 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 951 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 952 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 953 | static void io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 954 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 955 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 956 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c JA |
957 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
958 | struct io_uring_files_update *ip, | |
959 | unsigned nr_args); | |
3ca405eb | 960 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
961 | static struct file *io_file_get(struct io_submit_state *state, |
962 | struct io_kiocb *req, int fd, bool fixed); | |
c1379e24 | 963 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
4349f30e | 964 | static void io_file_put_work(struct work_struct *work); |
de0617e4 | 965 | |
b63534c4 JA |
966 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
967 | struct iovec **iovec, struct iov_iter *iter, | |
968 | bool needs_lock); | |
ff6165b2 JA |
969 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
970 | const struct iovec *fast_iov, | |
227c0c96 | 971 | struct iov_iter *iter, bool force); |
de0617e4 | 972 | |
2b188cc1 JA |
973 | static struct kmem_cache *req_cachep; |
974 | ||
0918682b | 975 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
976 | |
977 | struct sock *io_uring_get_socket(struct file *file) | |
978 | { | |
979 | #if defined(CONFIG_UNIX) | |
980 | if (file->f_op == &io_uring_fops) { | |
981 | struct io_ring_ctx *ctx = file->private_data; | |
982 | ||
983 | return ctx->ring_sock->sk; | |
984 | } | |
985 | #endif | |
986 | return NULL; | |
987 | } | |
988 | EXPORT_SYMBOL(io_uring_get_socket); | |
989 | ||
3ca405eb PB |
990 | static inline void io_clean_op(struct io_kiocb *req) |
991 | { | |
bb175342 PB |
992 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
993 | REQ_F_INFLIGHT)) | |
3ca405eb PB |
994 | __io_clean_op(req); |
995 | } | |
996 | ||
4349f30e | 997 | static void io_sq_thread_drop_mm(void) |
c40f6379 JA |
998 | { |
999 | struct mm_struct *mm = current->mm; | |
1000 | ||
1001 | if (mm) { | |
1002 | kthread_unuse_mm(mm); | |
1003 | mmput(mm); | |
4b70cf9d | 1004 | current->mm = NULL; |
c40f6379 JA |
1005 | } |
1006 | } | |
1007 | ||
1008 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1009 | { | |
4b70cf9d JA |
1010 | struct mm_struct *mm; |
1011 | ||
1012 | if (current->mm) | |
1013 | return 0; | |
1014 | ||
1015 | /* Should never happen */ | |
1016 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) | |
1017 | return -EFAULT; | |
1018 | ||
1019 | task_lock(ctx->sqo_task); | |
1020 | mm = ctx->sqo_task->mm; | |
1021 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1022 | mm = NULL; | |
1023 | task_unlock(ctx->sqo_task); | |
1024 | ||
1025 | if (mm) { | |
1026 | kthread_use_mm(mm); | |
1027 | return 0; | |
c40f6379 JA |
1028 | } |
1029 | ||
4b70cf9d | 1030 | return -EFAULT; |
c40f6379 JA |
1031 | } |
1032 | ||
1033 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, | |
1034 | struct io_kiocb *req) | |
1035 | { | |
0f203765 | 1036 | if (!(io_op_defs[req->opcode].work_flags & IO_WQ_WORK_MM)) |
c40f6379 JA |
1037 | return 0; |
1038 | return __io_sq_thread_acquire_mm(ctx); | |
1039 | } | |
1040 | ||
91d8f519 DZ |
1041 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1042 | struct cgroup_subsys_state **cur_css) | |
1043 | ||
1044 | { | |
1045 | #ifdef CONFIG_BLK_CGROUP | |
1046 | /* puts the old one when swapping */ | |
1047 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1048 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1049 | *cur_css = ctx->sqo_blkcg_css; | |
1050 | } | |
1051 | #endif | |
1052 | } | |
1053 | ||
1054 | static void io_sq_thread_unassociate_blkcg(void) | |
1055 | { | |
1056 | #ifdef CONFIG_BLK_CGROUP | |
1057 | kthread_associate_blkcg(NULL); | |
1058 | #endif | |
1059 | } | |
1060 | ||
c40f6379 JA |
1061 | static inline void req_set_fail_links(struct io_kiocb *req) |
1062 | { | |
1063 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1064 | req->flags |= REQ_F_FAIL_LINK; | |
1065 | } | |
4a38aed2 | 1066 | |
1e6fa521 JA |
1067 | /* |
1068 | * None of these are dereferenced, they are simply used to check if any of | |
1069 | * them have changed. If we're under current and check they are still the | |
1070 | * same, we're fine to grab references to them for actual out-of-line use. | |
1071 | */ | |
1072 | static void io_init_identity(struct io_identity *id) | |
1073 | { | |
1074 | id->files = current->files; | |
1075 | id->mm = current->mm; | |
1076 | #ifdef CONFIG_BLK_CGROUP | |
1077 | rcu_read_lock(); | |
1078 | id->blkcg_css = blkcg_css(); | |
1079 | rcu_read_unlock(); | |
1080 | #endif | |
1081 | id->creds = current_cred(); | |
1082 | id->nsproxy = current->nsproxy; | |
1083 | id->fs = current->fs; | |
1084 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1085 | #ifdef CONFIG_AUDIT |
1086 | id->loginuid = current->loginuid; | |
1087 | id->sessionid = current->sessionid; | |
1088 | #endif | |
1e6fa521 JA |
1089 | refcount_set(&id->count, 1); |
1090 | } | |
1091 | ||
ec99ca6c PB |
1092 | static inline void __io_req_init_async(struct io_kiocb *req) |
1093 | { | |
1094 | memset(&req->work, 0, sizeof(req->work)); | |
1095 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1096 | } | |
1097 | ||
7cdaf587 XW |
1098 | /* |
1099 | * Note: must call io_req_init_async() for the first time you | |
1100 | * touch any members of io_wq_work. | |
1101 | */ | |
1102 | static inline void io_req_init_async(struct io_kiocb *req) | |
1103 | { | |
500a373d JA |
1104 | struct io_uring_task *tctx = current->io_uring; |
1105 | ||
7cdaf587 XW |
1106 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1107 | return; | |
1108 | ||
ec99ca6c | 1109 | __io_req_init_async(req); |
500a373d JA |
1110 | |
1111 | /* Grab a ref if this isn't our static identity */ | |
1112 | req->work.identity = tctx->identity; | |
1113 | if (tctx->identity != &tctx->__identity) | |
1114 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1115 | } |
1116 | ||
0cdaf760 PB |
1117 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
1118 | { | |
1119 | return ctx->flags & IORING_SETUP_SQPOLL; | |
1120 | } | |
1121 | ||
2b188cc1 JA |
1122 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1123 | { | |
1124 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1125 | ||
0f158b4c | 1126 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1127 | } |
1128 | ||
8eb7e2d0 PB |
1129 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1130 | { | |
1131 | return !req->timeout.off; | |
1132 | } | |
1133 | ||
2b188cc1 JA |
1134 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1135 | { | |
1136 | struct io_ring_ctx *ctx; | |
78076bb6 | 1137 | int hash_bits; |
2b188cc1 JA |
1138 | |
1139 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1140 | if (!ctx) | |
1141 | return NULL; | |
1142 | ||
0ddf92e8 JA |
1143 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
1144 | if (!ctx->fallback_req) | |
1145 | goto err; | |
1146 | ||
78076bb6 JA |
1147 | /* |
1148 | * Use 5 bits less than the max cq entries, that should give us around | |
1149 | * 32 entries per hash list if totally full and uniformly spread. | |
1150 | */ | |
1151 | hash_bits = ilog2(p->cq_entries); | |
1152 | hash_bits -= 5; | |
1153 | if (hash_bits <= 0) | |
1154 | hash_bits = 1; | |
1155 | ctx->cancel_hash_bits = hash_bits; | |
1156 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1157 | GFP_KERNEL); | |
1158 | if (!ctx->cancel_hash) | |
1159 | goto err; | |
1160 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1161 | ||
21482896 | 1162 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1163 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1164 | goto err; | |
2b188cc1 JA |
1165 | |
1166 | ctx->flags = p->flags; | |
90554200 | 1167 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1168 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1169 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1170 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1171 | init_completion(&ctx->ref_comp); |
1172 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1173 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1174 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1175 | mutex_init(&ctx->uring_lock); |
1176 | init_waitqueue_head(&ctx->wait); | |
1177 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1178 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1179 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1180 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1181 | init_waitqueue_head(&ctx->inflight_wait); |
1182 | spin_lock_init(&ctx->inflight_lock); | |
1183 | INIT_LIST_HEAD(&ctx->inflight_list); | |
4a38aed2 JA |
1184 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
1185 | init_llist_head(&ctx->file_put_llist); | |
2b188cc1 | 1186 | return ctx; |
206aefde | 1187 | err: |
0ddf92e8 JA |
1188 | if (ctx->fallback_req) |
1189 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
78076bb6 | 1190 | kfree(ctx->cancel_hash); |
206aefde JA |
1191 | kfree(ctx); |
1192 | return NULL; | |
2b188cc1 JA |
1193 | } |
1194 | ||
9cf7c104 | 1195 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1196 | { |
2bc9930e JA |
1197 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1198 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1199 | |
9cf7c104 | 1200 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1201 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1202 | } |
de0617e4 | 1203 | |
9d858b21 | 1204 | return false; |
de0617e4 JA |
1205 | } |
1206 | ||
de0617e4 | 1207 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 1208 | { |
75b28aff | 1209 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 1210 | |
07910158 PB |
1211 | /* order cqe stores with ring update */ |
1212 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 | 1213 | |
07910158 PB |
1214 | if (wq_has_sleeper(&ctx->cq_wait)) { |
1215 | wake_up_interruptible(&ctx->cq_wait); | |
1216 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
2b188cc1 JA |
1217 | } |
1218 | } | |
1219 | ||
5c3462cf | 1220 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1221 | { |
500a373d | 1222 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1223 | return; |
1224 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1225 | kfree(req->work.identity); | |
1226 | } | |
1227 | ||
4edf20f9 | 1228 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1229 | { |
7cdaf587 | 1230 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1231 | return; |
51a4cc11 JA |
1232 | |
1233 | req->flags &= ~REQ_F_WORK_INITIALIZED; | |
7cdaf587 | 1234 | |
dfead8a8 | 1235 | if (req->work.flags & IO_WQ_WORK_MM) { |
98447d65 | 1236 | mmdrop(req->work.identity->mm); |
dfead8a8 | 1237 | req->work.flags &= ~IO_WQ_WORK_MM; |
cccf0ee8 | 1238 | } |
91d8f519 | 1239 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 | 1240 | if (req->work.flags & IO_WQ_WORK_BLKCG) { |
98447d65 | 1241 | css_put(req->work.identity->blkcg_css); |
dfead8a8 JA |
1242 | req->work.flags &= ~IO_WQ_WORK_BLKCG; |
1243 | } | |
91d8f519 | 1244 | #endif |
dfead8a8 | 1245 | if (req->work.flags & IO_WQ_WORK_CREDS) { |
98447d65 | 1246 | put_cred(req->work.identity->creds); |
dfead8a8 | 1247 | req->work.flags &= ~IO_WQ_WORK_CREDS; |
cccf0ee8 | 1248 | } |
dfead8a8 | 1249 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1250 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1251 | |
98447d65 | 1252 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1253 | if (--fs->users) |
1254 | fs = NULL; | |
98447d65 | 1255 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1256 | if (fs) |
1257 | free_fs_struct(fs); | |
dfead8a8 | 1258 | req->work.flags &= ~IO_WQ_WORK_FS; |
ff002b30 | 1259 | } |
51a4cc11 | 1260 | |
5c3462cf | 1261 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1262 | } |
1263 | ||
1e6fa521 JA |
1264 | /* |
1265 | * Create a private copy of io_identity, since some fields don't match | |
1266 | * the current context. | |
1267 | */ | |
1268 | static bool io_identity_cow(struct io_kiocb *req) | |
1269 | { | |
5c3462cf | 1270 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1271 | const struct cred *creds = NULL; |
1272 | struct io_identity *id; | |
1273 | ||
1274 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1275 | creds = req->work.identity->creds; | |
1276 | ||
1277 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1278 | if (unlikely(!id)) { | |
1279 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1280 | return false; | |
1281 | } | |
1282 | ||
1283 | /* | |
1284 | * We can safely just re-init the creds we copied Either the field | |
1285 | * matches the current one, or we haven't grabbed it yet. The only | |
1286 | * exception is ->creds, through registered personalities, so handle | |
1287 | * that one separately. | |
1288 | */ | |
1289 | io_init_identity(id); | |
1290 | if (creds) | |
e8c954df | 1291 | id->creds = creds; |
1e6fa521 JA |
1292 | |
1293 | /* add one for this request */ | |
1294 | refcount_inc(&id->count); | |
1295 | ||
cb8a8ae3 JA |
1296 | /* drop tctx and req identity references, if needed */ |
1297 | if (tctx->identity != &tctx->__identity && | |
1298 | refcount_dec_and_test(&tctx->identity->count)) | |
1299 | kfree(tctx->identity); | |
1300 | if (req->work.identity != &tctx->__identity && | |
1301 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1302 | kfree(req->work.identity); |
1303 | ||
1304 | req->work.identity = id; | |
500a373d | 1305 | tctx->identity = id; |
1e6fa521 JA |
1306 | return true; |
1307 | } | |
1308 | ||
1309 | static bool io_grab_identity(struct io_kiocb *req) | |
18d9be1a | 1310 | { |
d3656344 | 1311 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1312 | struct io_identity *id = req->work.identity; |
23329513 | 1313 | struct io_ring_ctx *ctx = req->ctx; |
54a91f3b | 1314 | |
69228338 JA |
1315 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1316 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1317 | return false; | |
1318 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1319 | } | |
91d8f519 | 1320 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1321 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1322 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1323 | rcu_read_lock(); |
1e6fa521 JA |
1324 | if (id->blkcg_css != blkcg_css()) { |
1325 | rcu_read_unlock(); | |
1326 | return false; | |
1327 | } | |
91d8f519 DZ |
1328 | /* |
1329 | * This should be rare, either the cgroup is dying or the task | |
1330 | * is moving cgroups. Just punt to root for the handful of ios. | |
1331 | */ | |
1e6fa521 | 1332 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1333 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1334 | rcu_read_unlock(); |
1335 | } | |
1336 | #endif | |
dfead8a8 | 1337 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1338 | if (id->creds != current_cred()) |
1339 | return false; | |
1340 | get_cred(id->creds); | |
dfead8a8 JA |
1341 | req->work.flags |= IO_WQ_WORK_CREDS; |
1342 | } | |
4ea33a97 JA |
1343 | #ifdef CONFIG_AUDIT |
1344 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1345 | current->sessionid != id->sessionid) | |
1346 | return false; | |
1347 | #endif | |
dfead8a8 JA |
1348 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1349 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1350 | if (current->fs != id->fs) |
1351 | return false; | |
1352 | spin_lock(&id->fs->lock); | |
1353 | if (!id->fs->in_exec) { | |
1354 | id->fs->users++; | |
dfead8a8 | 1355 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1356 | } else { |
1357 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1358 | } | |
1359 | spin_unlock(¤t->fs->lock); | |
1360 | } | |
af604703 PB |
1361 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1362 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1363 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1364 | if (id->files != current->files || | |
1365 | id->nsproxy != current->nsproxy) | |
1366 | return false; | |
1367 | atomic_inc(&id->files->count); | |
1368 | get_nsproxy(id->nsproxy); | |
1369 | req->flags |= REQ_F_INFLIGHT; | |
1370 | ||
1371 | spin_lock_irq(&ctx->inflight_lock); | |
1372 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1373 | spin_unlock_irq(&ctx->inflight_lock); | |
1374 | req->work.flags |= IO_WQ_WORK_FILES; | |
1375 | } | |
91dc8bea JA |
1376 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1377 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1378 | if (id->mm != current->mm) | |
1379 | return false; | |
1380 | mmgrab(id->mm); | |
1381 | req->work.flags |= IO_WQ_WORK_MM; | |
1382 | } | |
1e6fa521 JA |
1383 | |
1384 | return true; | |
1385 | } | |
1386 | ||
1387 | static void io_prep_async_work(struct io_kiocb *req) | |
1388 | { | |
1389 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 | 1390 | struct io_ring_ctx *ctx = req->ctx; |
5c3462cf | 1391 | struct io_identity *id; |
1e6fa521 JA |
1392 | |
1393 | io_req_init_async(req); | |
5c3462cf | 1394 | id = req->work.identity; |
1e6fa521 | 1395 | |
feaadc4f PB |
1396 | if (req->flags & REQ_F_FORCE_ASYNC) |
1397 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1398 | ||
1e6fa521 JA |
1399 | if (req->flags & REQ_F_ISREG) { |
1400 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1401 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1402 | } else { | |
1403 | if (def->unbound_nonreg_file) | |
1404 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1405 | } | |
1406 | ||
1e6fa521 JA |
1407 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1408 | if (io_grab_identity(req)) | |
1409 | return; | |
1410 | ||
1411 | if (!io_identity_cow(req)) | |
1412 | return; | |
1413 | ||
1414 | /* can't fail at this point */ | |
1415 | if (!io_grab_identity(req)) | |
1416 | WARN_ON(1); | |
561fb04a | 1417 | } |
cccf0ee8 | 1418 | |
cbdcb435 | 1419 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1420 | { |
cbdcb435 | 1421 | struct io_kiocb *cur; |
54a91f3b | 1422 | |
cbdcb435 PB |
1423 | io_prep_async_work(req); |
1424 | if (req->flags & REQ_F_LINK_HEAD) | |
1425 | list_for_each_entry(cur, &req->link_list, link_list) | |
1426 | io_prep_async_work(cur); | |
561fb04a JA |
1427 | } |
1428 | ||
7271ef3a | 1429 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1430 | { |
a197f664 | 1431 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1432 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1433 | |
8766dd51 PB |
1434 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1435 | &req->work, req->flags); | |
1436 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1437 | return link; |
18d9be1a JA |
1438 | } |
1439 | ||
cbdcb435 PB |
1440 | static void io_queue_async_work(struct io_kiocb *req) |
1441 | { | |
7271ef3a JA |
1442 | struct io_kiocb *link; |
1443 | ||
cbdcb435 PB |
1444 | /* init ->work of the whole link before punting */ |
1445 | io_prep_async_link(req); | |
7271ef3a JA |
1446 | link = __io_queue_async_work(req); |
1447 | ||
1448 | if (link) | |
1449 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1450 | } |
1451 | ||
5262f567 JA |
1452 | static void io_kill_timeout(struct io_kiocb *req) |
1453 | { | |
e8c2bc1f | 1454 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1455 | int ret; |
1456 | ||
e8c2bc1f | 1457 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1458 | if (ret != -1) { |
01cec8c1 PB |
1459 | atomic_set(&req->ctx->cq_timeouts, |
1460 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1461 | list_del_init(&req->timeout.list); |
78e19bbe | 1462 | io_cqring_fill_event(req, 0); |
216578e5 | 1463 | io_put_req_deferred(req, 1); |
5262f567 JA |
1464 | } |
1465 | } | |
1466 | ||
f3606e3a JA |
1467 | static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk) |
1468 | { | |
1469 | struct io_ring_ctx *ctx = req->ctx; | |
1470 | ||
1471 | if (!tsk || req->task == tsk) | |
1472 | return true; | |
534ca6d6 JA |
1473 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1474 | if (ctx->sq_data && req->task == ctx->sq_data->thread) | |
1475 | return true; | |
1476 | } | |
f3606e3a JA |
1477 | return false; |
1478 | } | |
1479 | ||
76e1b642 JA |
1480 | /* |
1481 | * Returns true if we found and killed one or more timeouts | |
1482 | */ | |
1483 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk) | |
5262f567 JA |
1484 | { |
1485 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1486 | int canceled = 0; |
5262f567 JA |
1487 | |
1488 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1489 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
76e1b642 | 1490 | if (io_task_match(req, tsk)) { |
f3606e3a | 1491 | io_kill_timeout(req); |
76e1b642 JA |
1492 | canceled++; |
1493 | } | |
f3606e3a | 1494 | } |
5262f567 | 1495 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1496 | return canceled != 0; |
5262f567 JA |
1497 | } |
1498 | ||
04518945 | 1499 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1500 | { |
04518945 | 1501 | do { |
27dc8338 PB |
1502 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1503 | struct io_defer_entry, list); | |
7271ef3a | 1504 | struct io_kiocb *link; |
de0617e4 | 1505 | |
9cf7c104 | 1506 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1507 | break; |
27dc8338 | 1508 | list_del_init(&de->list); |
cbdcb435 | 1509 | /* punt-init is done before queueing for defer */ |
7271ef3a JA |
1510 | link = __io_queue_async_work(de->req); |
1511 | if (link) { | |
1512 | __io_queue_linked_timeout(link); | |
1513 | /* drop submission reference */ | |
216578e5 | 1514 | io_put_req_deferred(link, 1); |
7271ef3a | 1515 | } |
27dc8338 | 1516 | kfree(de); |
04518945 PB |
1517 | } while (!list_empty(&ctx->defer_list)); |
1518 | } | |
1519 | ||
360428f8 | 1520 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1521 | { |
360428f8 PB |
1522 | while (!list_empty(&ctx->timeout_list)) { |
1523 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, | |
135fcde8 | 1524 | struct io_kiocb, timeout.list); |
de0617e4 | 1525 | |
8eb7e2d0 | 1526 | if (io_is_timeout_noseq(req)) |
360428f8 | 1527 | break; |
bfe68a22 PB |
1528 | if (req->timeout.target_seq != ctx->cached_cq_tail |
1529 | - atomic_read(&ctx->cq_timeouts)) | |
360428f8 | 1530 | break; |
bfe68a22 | 1531 | |
135fcde8 | 1532 | list_del_init(&req->timeout.list); |
5262f567 | 1533 | io_kill_timeout(req); |
360428f8 PB |
1534 | } |
1535 | } | |
5262f567 | 1536 | |
360428f8 PB |
1537 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1538 | { | |
1539 | io_flush_timeouts(ctx); | |
de0617e4 JA |
1540 | __io_commit_cqring(ctx); |
1541 | ||
04518945 PB |
1542 | if (unlikely(!list_empty(&ctx->defer_list))) |
1543 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1544 | } |
1545 | ||
90554200 JA |
1546 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1547 | { | |
1548 | struct io_rings *r = ctx->rings; | |
1549 | ||
1550 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1551 | } | |
1552 | ||
2b188cc1 JA |
1553 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1554 | { | |
75b28aff | 1555 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1556 | unsigned tail; |
1557 | ||
1558 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
1559 | /* |
1560 | * writes to the cq entry need to come after reading head; the | |
1561 | * control dependency is enough as we're using WRITE_ONCE to | |
1562 | * fill the cq entry | |
1563 | */ | |
75b28aff | 1564 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
1565 | return NULL; |
1566 | ||
1567 | ctx->cached_cq_tail++; | |
75b28aff | 1568 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1569 | } |
1570 | ||
f2842ab5 JA |
1571 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1572 | { | |
f0b493e6 JA |
1573 | if (!ctx->cq_ev_fd) |
1574 | return false; | |
7e55a19c SG |
1575 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1576 | return false; | |
f2842ab5 JA |
1577 | if (!ctx->eventfd_async) |
1578 | return true; | |
b41e9852 | 1579 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1580 | } |
1581 | ||
b41e9852 | 1582 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 JA |
1583 | { |
1584 | if (waitqueue_active(&ctx->wait)) | |
1585 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1586 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1587 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1588 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1589 | eventfd_signal(ctx->cq_ev_fd, 1); |
1590 | } | |
1591 | ||
46930143 PB |
1592 | static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
1593 | { | |
1594 | if (list_empty(&ctx->cq_overflow_list)) { | |
1595 | clear_bit(0, &ctx->sq_check_overflow); | |
1596 | clear_bit(0, &ctx->cq_check_overflow); | |
1597 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1598 | } | |
1599 | } | |
1600 | ||
99b32808 PB |
1601 | static inline bool __io_match_files(struct io_kiocb *req, |
1602 | struct files_struct *files) | |
e6c8aa9a | 1603 | { |
99b32808 PB |
1604 | return ((req->flags & REQ_F_WORK_INITIALIZED) && |
1605 | (req->work.flags & IO_WQ_WORK_FILES)) && | |
1606 | req->work.identity->files == files; | |
1607 | } | |
1608 | ||
1609 | static bool io_match_files(struct io_kiocb *req, | |
1610 | struct files_struct *files) | |
1611 | { | |
1612 | struct io_kiocb *link; | |
1613 | ||
e6c8aa9a JA |
1614 | if (!files) |
1615 | return true; | |
99b32808 PB |
1616 | if (__io_match_files(req, files)) |
1617 | return true; | |
1618 | if (req->flags & REQ_F_LINK_HEAD) { | |
1619 | list_for_each_entry(link, &req->link_list, link_list) { | |
1620 | if (__io_match_files(link, files)) | |
1621 | return true; | |
1622 | } | |
1623 | } | |
e6c8aa9a JA |
1624 | return false; |
1625 | } | |
1626 | ||
c4a2ed72 | 1627 | /* Returns true if there are no backlogged entries after the flush */ |
e6c8aa9a JA |
1628 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1629 | struct task_struct *tsk, | |
1630 | struct files_struct *files) | |
1d7bb1d5 JA |
1631 | { |
1632 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1633 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1634 | struct io_uring_cqe *cqe; |
1d7bb1d5 JA |
1635 | unsigned long flags; |
1636 | LIST_HEAD(list); | |
1637 | ||
1638 | if (!force) { | |
1639 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
c4a2ed72 | 1640 | return true; |
1d7bb1d5 JA |
1641 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
1642 | rings->cq_ring_entries)) | |
c4a2ed72 | 1643 | return false; |
1d7bb1d5 JA |
1644 | } |
1645 | ||
1646 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1647 | ||
c4a2ed72 | 1648 | cqe = NULL; |
e6c8aa9a JA |
1649 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
1650 | if (tsk && req->task != tsk) | |
1651 | continue; | |
1652 | if (!io_match_files(req, files)) | |
1653 | continue; | |
1654 | ||
1d7bb1d5 JA |
1655 | cqe = io_get_cqring(ctx); |
1656 | if (!cqe && !force) | |
1657 | break; | |
1658 | ||
40d8ddd4 | 1659 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1660 | if (cqe) { |
1661 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1662 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1663 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1664 | } else { |
2c3bac6d | 1665 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1666 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1667 | ctx->cached_cq_overflow); |
1d7bb1d5 JA |
1668 | } |
1669 | } | |
1670 | ||
1671 | io_commit_cqring(ctx); | |
46930143 PB |
1672 | io_cqring_mark_overflow(ctx); |
1673 | ||
1d7bb1d5 JA |
1674 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1675 | io_cqring_ev_posted(ctx); | |
1676 | ||
1677 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1678 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1679 | list_del(&req->compl.list); | |
ec9c02ad | 1680 | io_put_req(req); |
1d7bb1d5 | 1681 | } |
c4a2ed72 JA |
1682 | |
1683 | return cqe != NULL; | |
1d7bb1d5 JA |
1684 | } |
1685 | ||
bcda7baa | 1686 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1687 | { |
78e19bbe | 1688 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1689 | struct io_uring_cqe *cqe; |
1690 | ||
78e19bbe | 1691 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1692 | |
2b188cc1 JA |
1693 | /* |
1694 | * If we can't get a cq entry, userspace overflowed the | |
1695 | * submission (by quite a lot). Increment the overflow count in | |
1696 | * the ring. | |
1697 | */ | |
1698 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1699 | if (likely(cqe)) { |
78e19bbe | 1700 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1701 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1702 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1703 | } else if (ctx->cq_overflow_flushed || |
1704 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1705 | /* |
1706 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1707 | * then we cannot store the request for later flushing, we need | |
1708 | * to drop it on the floor. | |
1709 | */ | |
2c3bac6d PB |
1710 | ctx->cached_cq_overflow++; |
1711 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1712 | } else { |
ad3eb2c8 JA |
1713 | if (list_empty(&ctx->cq_overflow_list)) { |
1714 | set_bit(0, &ctx->sq_check_overflow); | |
1715 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1716 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1717 | } |
40d8ddd4 | 1718 | io_clean_op(req); |
1d7bb1d5 | 1719 | req->result = res; |
0f7e466b | 1720 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1721 | refcount_inc(&req->refs); |
1722 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1723 | } |
1724 | } | |
1725 | ||
bcda7baa JA |
1726 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1727 | { | |
1728 | __io_cqring_fill_event(req, res, 0); | |
1729 | } | |
1730 | ||
e1e16097 | 1731 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1732 | { |
78e19bbe | 1733 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1734 | unsigned long flags; |
1735 | ||
1736 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1737 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1738 | io_commit_cqring(ctx); |
1739 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1740 | ||
8c838788 | 1741 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1742 | } |
1743 | ||
229a7b63 | 1744 | static void io_submit_flush_completions(struct io_comp_state *cs) |
bcda7baa | 1745 | { |
229a7b63 JA |
1746 | struct io_ring_ctx *ctx = cs->ctx; |
1747 | ||
1748 | spin_lock_irq(&ctx->completion_lock); | |
1749 | while (!list_empty(&cs->list)) { | |
1750 | struct io_kiocb *req; | |
1751 | ||
3ca405eb PB |
1752 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
1753 | list_del(&req->compl.list); | |
0f7e466b | 1754 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
216578e5 PB |
1755 | |
1756 | /* | |
1757 | * io_free_req() doesn't care about completion_lock unless one | |
1758 | * of these flags is set. REQ_F_WORK_INITIALIZED is in the list | |
1759 | * because of a potential deadlock with req->work.fs->lock | |
1760 | */ | |
1761 | if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT | |
1762 | |REQ_F_WORK_INITIALIZED)) { | |
229a7b63 JA |
1763 | spin_unlock_irq(&ctx->completion_lock); |
1764 | io_put_req(req); | |
1765 | spin_lock_irq(&ctx->completion_lock); | |
216578e5 PB |
1766 | } else { |
1767 | io_put_req(req); | |
229a7b63 JA |
1768 | } |
1769 | } | |
1770 | io_commit_cqring(ctx); | |
1771 | spin_unlock_irq(&ctx->completion_lock); | |
1772 | ||
1773 | io_cqring_ev_posted(ctx); | |
1774 | cs->nr = 0; | |
1775 | } | |
1776 | ||
1777 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, | |
1778 | struct io_comp_state *cs) | |
1779 | { | |
1780 | if (!cs) { | |
1781 | io_cqring_add_event(req, res, cflags); | |
1782 | io_put_req(req); | |
1783 | } else { | |
3ca405eb | 1784 | io_clean_op(req); |
229a7b63 | 1785 | req->result = res; |
0f7e466b | 1786 | req->compl.cflags = cflags; |
3ca405eb | 1787 | list_add_tail(&req->compl.list, &cs->list); |
229a7b63 JA |
1788 | if (++cs->nr >= 32) |
1789 | io_submit_flush_completions(cs); | |
1790 | } | |
e1e16097 JA |
1791 | } |
1792 | ||
1793 | static void io_req_complete(struct io_kiocb *req, long res) | |
bcda7baa | 1794 | { |
229a7b63 | 1795 | __io_req_complete(req, res, 0, NULL); |
bcda7baa JA |
1796 | } |
1797 | ||
0ddf92e8 JA |
1798 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1799 | { | |
1800 | return req == (struct io_kiocb *) | |
1801 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1802 | } | |
1803 | ||
1804 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1805 | { | |
1806 | struct io_kiocb *req; | |
1807 | ||
1808 | req = ctx->fallback_req; | |
dd461af6 | 1809 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
0ddf92e8 JA |
1810 | return req; |
1811 | ||
1812 | return NULL; | |
1813 | } | |
1814 | ||
0553b8bd PB |
1815 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
1816 | struct io_submit_state *state) | |
2b188cc1 | 1817 | { |
f6b6c7d6 | 1818 | if (!state->free_reqs) { |
291b2821 | 1819 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1820 | size_t sz; |
1821 | int ret; | |
1822 | ||
1823 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1824 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1825 | ||
1826 | /* | |
1827 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1828 | * retry single alloc to be on the safe side. | |
1829 | */ | |
1830 | if (unlikely(ret <= 0)) { | |
1831 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1832 | if (!state->reqs[0]) | |
0ddf92e8 | 1833 | goto fallback; |
fd6fab2c JA |
1834 | ret = 1; |
1835 | } | |
291b2821 | 1836 | state->free_reqs = ret; |
2b188cc1 JA |
1837 | } |
1838 | ||
291b2821 PB |
1839 | state->free_reqs--; |
1840 | return state->reqs[state->free_reqs]; | |
0ddf92e8 | 1841 | fallback: |
0553b8bd | 1842 | return io_get_fallback_req(ctx); |
2b188cc1 JA |
1843 | } |
1844 | ||
8da11c19 PB |
1845 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1846 | bool fixed) | |
1847 | { | |
1848 | if (fixed) | |
05589553 | 1849 | percpu_ref_put(req->fixed_file_refs); |
8da11c19 PB |
1850 | else |
1851 | fput(file); | |
1852 | } | |
1853 | ||
4edf20f9 | 1854 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1855 | { |
3ca405eb | 1856 | io_clean_op(req); |
929a3af9 | 1857 | |
e8c2bc1f JA |
1858 | if (req->async_data) |
1859 | kfree(req->async_data); | |
8da11c19 PB |
1860 | if (req->file) |
1861 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
fcb323cc | 1862 | |
4edf20f9 | 1863 | io_req_clean_work(req); |
e65ef56d JA |
1864 | } |
1865 | ||
216578e5 | 1866 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1867 | { |
0f212204 | 1868 | struct io_uring_task *tctx = req->task->io_uring; |
51a4cc11 | 1869 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1870 | |
216578e5 | 1871 | io_dismantle_req(req); |
c6ca97b3 | 1872 | |
d8a6df10 | 1873 | percpu_counter_dec(&tctx->inflight); |
fdaf083c | 1874 | if (atomic_read(&tctx->in_idle)) |
0f212204 | 1875 | wake_up(&tctx->wait); |
e3bc8e9d JA |
1876 | put_task_struct(req->task); |
1877 | ||
b1e50e54 PB |
1878 | if (likely(!io_is_fallback_req(req))) |
1879 | kmem_cache_free(req_cachep, req); | |
1880 | else | |
ecfc5177 PB |
1881 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
1882 | percpu_ref_put(&ctx->refs); | |
e65ef56d JA |
1883 | } |
1884 | ||
c9abd7ad | 1885 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1886 | { |
a197f664 | 1887 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1888 | struct io_kiocb *link; |
c9abd7ad PB |
1889 | bool cancelled = false; |
1890 | unsigned long flags; | |
7c86ffee | 1891 | |
c9abd7ad PB |
1892 | spin_lock_irqsave(&ctx->completion_lock, flags); |
1893 | link = list_first_entry_or_null(&req->link_list, struct io_kiocb, | |
1894 | link_list); | |
900fad45 PB |
1895 | /* |
1896 | * Can happen if a linked timeout fired and link had been like | |
1897 | * req -> link t-out -> link t-out [-> ...] | |
1898 | */ | |
c9abd7ad PB |
1899 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1900 | struct io_timeout_data *io = link->async_data; | |
1901 | int ret; | |
7c86ffee | 1902 | |
c9abd7ad PB |
1903 | list_del_init(&link->link_list); |
1904 | ret = hrtimer_try_to_cancel(&io->timer); | |
1905 | if (ret != -1) { | |
1906 | io_cqring_fill_event(link, -ECANCELED); | |
1907 | io_commit_cqring(ctx); | |
1908 | cancelled = true; | |
1909 | } | |
1910 | } | |
7c86ffee | 1911 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 1912 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 1913 | |
c9abd7ad | 1914 | if (cancelled) { |
7c86ffee | 1915 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
1916 | io_put_req(link); |
1917 | } | |
7c86ffee PB |
1918 | } |
1919 | ||
9b5f7bd9 | 1920 | static struct io_kiocb *io_req_link_next(struct io_kiocb *req) |
7c86ffee PB |
1921 | { |
1922 | struct io_kiocb *nxt; | |
4d7dd462 | 1923 | |
9e645e11 JA |
1924 | /* |
1925 | * The list should never be empty when we are called here. But could | |
1926 | * potentially happen if the chain is messed up, check to be on the | |
1927 | * safe side. | |
1928 | */ | |
7c86ffee | 1929 | if (unlikely(list_empty(&req->link_list))) |
9b5f7bd9 | 1930 | return NULL; |
2665abfd | 1931 | |
7c86ffee PB |
1932 | nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
1933 | list_del_init(&req->link_list); | |
1934 | if (!list_empty(&nxt->link_list)) | |
1935 | nxt->flags |= REQ_F_LINK_HEAD; | |
9b5f7bd9 | 1936 | return nxt; |
9e645e11 JA |
1937 | } |
1938 | ||
1939 | /* | |
dea3b49c | 1940 | * Called if REQ_F_LINK_HEAD is set, and we fail the head request |
9e645e11 | 1941 | */ |
d148ca4b | 1942 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1943 | { |
2665abfd | 1944 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 1945 | unsigned long flags; |
9e645e11 | 1946 | |
d148ca4b | 1947 | spin_lock_irqsave(&ctx->completion_lock, flags); |
9e645e11 | 1948 | while (!list_empty(&req->link_list)) { |
4493233e PB |
1949 | struct io_kiocb *link = list_first_entry(&req->link_list, |
1950 | struct io_kiocb, link_list); | |
9e645e11 | 1951 | |
4493233e | 1952 | list_del_init(&link->link_list); |
c826bd7a | 1953 | trace_io_uring_fail_link(req, link); |
2665abfd | 1954 | |
7c86ffee | 1955 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
1956 | |
1957 | /* | |
1958 | * It's ok to free under spinlock as they're not linked anymore, | |
1959 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
1960 | * work.fs->lock. | |
1961 | */ | |
1962 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
1963 | io_put_req_deferred(link, 2); | |
1964 | else | |
1965 | io_double_put_req(link); | |
9e645e11 | 1966 | } |
2665abfd JA |
1967 | |
1968 | io_commit_cqring(ctx); | |
216578e5 | 1969 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 1970 | |
2665abfd | 1971 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1972 | } |
1973 | ||
3fa5e0f3 | 1974 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1975 | { |
9b0d911a | 1976 | req->flags &= ~REQ_F_LINK_HEAD; |
7c86ffee PB |
1977 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1978 | io_kill_linked_timeout(req); | |
944e58bf | 1979 | |
9e645e11 JA |
1980 | /* |
1981 | * If LINK is set, we have dependent requests in this chain. If we | |
1982 | * didn't fail this request, queue the first one up, moving any other | |
1983 | * dependencies to the next request. In case of failure, fail the rest | |
1984 | * of the chain. | |
1985 | */ | |
9b5f7bd9 PB |
1986 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) |
1987 | return io_req_link_next(req); | |
1988 | io_fail_links(req); | |
1989 | return NULL; | |
4d7dd462 | 1990 | } |
9e645e11 | 1991 | |
3fa5e0f3 PB |
1992 | static struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
1993 | { | |
1994 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) | |
1995 | return NULL; | |
1996 | return __io_req_find_next(req); | |
1997 | } | |
1998 | ||
87c4311f | 1999 | static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok) |
c2c4c83c JA |
2000 | { |
2001 | struct task_struct *tsk = req->task; | |
2002 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2003 | enum task_work_notify_mode notify; |
2004 | int ret; | |
c2c4c83c | 2005 | |
6200b0ae JA |
2006 | if (tsk->flags & PF_EXITING) |
2007 | return -ESRCH; | |
2008 | ||
c2c4c83c | 2009 | /* |
0ba9c9ed JA |
2010 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2011 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2012 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2013 | * will do the job. | |
c2c4c83c | 2014 | */ |
91989c70 | 2015 | notify = TWA_NONE; |
fd7d6de2 | 2016 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok) |
c2c4c83c JA |
2017 | notify = TWA_SIGNAL; |
2018 | ||
87c4311f | 2019 | ret = task_work_add(tsk, &req->task_work, notify); |
c2c4c83c JA |
2020 | if (!ret) |
2021 | wake_up_process(tsk); | |
0ba9c9ed | 2022 | |
c2c4c83c JA |
2023 | return ret; |
2024 | } | |
2025 | ||
c40f6379 JA |
2026 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2027 | { | |
2028 | struct io_ring_ctx *ctx = req->ctx; | |
2029 | ||
2030 | spin_lock_irq(&ctx->completion_lock); | |
2031 | io_cqring_fill_event(req, error); | |
2032 | io_commit_cqring(ctx); | |
2033 | spin_unlock_irq(&ctx->completion_lock); | |
2034 | ||
2035 | io_cqring_ev_posted(ctx); | |
2036 | req_set_fail_links(req); | |
2037 | io_double_put_req(req); | |
2038 | } | |
2039 | ||
2040 | static void io_req_task_cancel(struct callback_head *cb) | |
2041 | { | |
2042 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2043 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2044 | |
2045 | __io_req_task_cancel(req, -ECANCELED); | |
87ceb6a6 | 2046 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2047 | } |
2048 | ||
2049 | static void __io_req_task_submit(struct io_kiocb *req) | |
2050 | { | |
2051 | struct io_ring_ctx *ctx = req->ctx; | |
2052 | ||
c40f6379 JA |
2053 | if (!__io_sq_thread_acquire_mm(ctx)) { |
2054 | mutex_lock(&ctx->uring_lock); | |
c1379e24 | 2055 | __io_queue_sqe(req, NULL); |
c40f6379 JA |
2056 | mutex_unlock(&ctx->uring_lock); |
2057 | } else { | |
2058 | __io_req_task_cancel(req, -EFAULT); | |
2059 | } | |
2060 | } | |
2061 | ||
2062 | static void io_req_task_submit(struct callback_head *cb) | |
2063 | { | |
2064 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 2065 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2066 | |
2067 | __io_req_task_submit(req); | |
6d816e08 | 2068 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2069 | } |
2070 | ||
2071 | static void io_req_task_queue(struct io_kiocb *req) | |
2072 | { | |
c40f6379 JA |
2073 | int ret; |
2074 | ||
2075 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 2076 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 2077 | |
87c4311f | 2078 | ret = io_req_task_work_add(req, true); |
c40f6379 | 2079 | if (unlikely(ret)) { |
c2c4c83c JA |
2080 | struct task_struct *tsk; |
2081 | ||
c40f6379 JA |
2082 | init_task_work(&req->task_work, io_req_task_cancel); |
2083 | tsk = io_wq_get_task(req->ctx->io_wq); | |
91989c70 | 2084 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 2085 | wake_up_process(tsk); |
c40f6379 | 2086 | } |
c40f6379 JA |
2087 | } |
2088 | ||
c3524383 | 2089 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2090 | { |
9b5f7bd9 | 2091 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2092 | |
2093 | if (nxt) | |
906a8c3f | 2094 | io_req_task_queue(nxt); |
c69f8dbe JL |
2095 | } |
2096 | ||
c3524383 | 2097 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2098 | { |
c3524383 PB |
2099 | io_queue_next(req); |
2100 | __io_free_req(req); | |
2101 | } | |
8766dd51 | 2102 | |
2d6500d4 PB |
2103 | struct req_batch { |
2104 | void *reqs[IO_IOPOLL_BATCH]; | |
2105 | int to_free; | |
7a743e22 | 2106 | |
5af1d13e PB |
2107 | struct task_struct *task; |
2108 | int task_refs; | |
2d6500d4 PB |
2109 | }; |
2110 | ||
5af1d13e PB |
2111 | static inline void io_init_req_batch(struct req_batch *rb) |
2112 | { | |
2113 | rb->to_free = 0; | |
2114 | rb->task_refs = 0; | |
2115 | rb->task = NULL; | |
2116 | } | |
2117 | ||
2d6500d4 PB |
2118 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
2119 | struct req_batch *rb) | |
2120 | { | |
2121 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
2122 | percpu_ref_put_many(&ctx->refs, rb->to_free); | |
2123 | rb->to_free = 0; | |
2124 | } | |
2125 | ||
2126 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
2127 | struct req_batch *rb) | |
2128 | { | |
2129 | if (rb->to_free) | |
2130 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e | 2131 | if (rb->task) { |
d8a6df10 JA |
2132 | struct io_uring_task *tctx = rb->task->io_uring; |
2133 | ||
2134 | percpu_counter_sub(&tctx->inflight, rb->task_refs); | |
5af1d13e PB |
2135 | put_task_struct_many(rb->task, rb->task_refs); |
2136 | rb->task = NULL; | |
2137 | } | |
2d6500d4 PB |
2138 | } |
2139 | ||
2140 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
2141 | { | |
2142 | if (unlikely(io_is_fallback_req(req))) { | |
2143 | io_free_req(req); | |
2144 | return; | |
2145 | } | |
2146 | if (req->flags & REQ_F_LINK_HEAD) | |
2147 | io_queue_next(req); | |
2148 | ||
e3bc8e9d | 2149 | if (req->task != rb->task) { |
0f212204 | 2150 | if (rb->task) { |
d8a6df10 JA |
2151 | struct io_uring_task *tctx = rb->task->io_uring; |
2152 | ||
2153 | percpu_counter_sub(&tctx->inflight, rb->task_refs); | |
e3bc8e9d | 2154 | put_task_struct_many(rb->task, rb->task_refs); |
5af1d13e | 2155 | } |
e3bc8e9d JA |
2156 | rb->task = req->task; |
2157 | rb->task_refs = 0; | |
5af1d13e | 2158 | } |
e3bc8e9d | 2159 | rb->task_refs++; |
5af1d13e | 2160 | |
4edf20f9 | 2161 | io_dismantle_req(req); |
2d6500d4 PB |
2162 | rb->reqs[rb->to_free++] = req; |
2163 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
2164 | __io_req_free_batch_flush(req->ctx, rb); | |
7a743e22 PB |
2165 | } |
2166 | ||
ba816ad6 JA |
2167 | /* |
2168 | * Drop reference to request, return next in chain (if there is one) if this | |
2169 | * was the last reference to this request. | |
2170 | */ | |
9b5f7bd9 | 2171 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2172 | { |
9b5f7bd9 PB |
2173 | struct io_kiocb *nxt = NULL; |
2174 | ||
2a44f467 | 2175 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2176 | nxt = io_req_find_next(req); |
4d7dd462 | 2177 | __io_free_req(req); |
2a44f467 | 2178 | } |
9b5f7bd9 | 2179 | return nxt; |
2b188cc1 JA |
2180 | } |
2181 | ||
e65ef56d JA |
2182 | static void io_put_req(struct io_kiocb *req) |
2183 | { | |
2184 | if (refcount_dec_and_test(&req->refs)) | |
2185 | io_free_req(req); | |
2b188cc1 JA |
2186 | } |
2187 | ||
216578e5 PB |
2188 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2189 | { | |
2190 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2191 | ||
2192 | io_free_req(req); | |
2193 | } | |
2194 | ||
2195 | static void io_free_req_deferred(struct io_kiocb *req) | |
2196 | { | |
2197 | int ret; | |
2198 | ||
2199 | init_task_work(&req->task_work, io_put_req_deferred_cb); | |
2200 | ret = io_req_task_work_add(req, true); | |
2201 | if (unlikely(ret)) { | |
2202 | struct task_struct *tsk; | |
2203 | ||
2204 | tsk = io_wq_get_task(req->ctx->io_wq); | |
91989c70 | 2205 | task_work_add(tsk, &req->task_work, TWA_NONE); |
216578e5 PB |
2206 | wake_up_process(tsk); |
2207 | } | |
2208 | } | |
2209 | ||
2210 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2211 | { | |
2212 | if (refcount_sub_and_test(refs, &req->refs)) | |
2213 | io_free_req_deferred(req); | |
2214 | } | |
2215 | ||
f4db7182 | 2216 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
7a743e22 | 2217 | { |
6df1db6b | 2218 | struct io_kiocb *nxt; |
f4db7182 | 2219 | |
7a743e22 | 2220 | /* |
f4db7182 PB |
2221 | * A ref is owned by io-wq in which context we're. So, if that's the |
2222 | * last one, it's safe to steal next work. False negatives are Ok, | |
2223 | * it just will be re-punted async in io_put_work() | |
7a743e22 | 2224 | */ |
f4db7182 PB |
2225 | if (refcount_read(&req->refs) != 1) |
2226 | return NULL; | |
7a743e22 | 2227 | |
9b5f7bd9 | 2228 | nxt = io_req_find_next(req); |
6df1db6b | 2229 | return nxt ? &nxt->work : NULL; |
7a743e22 PB |
2230 | } |
2231 | ||
978db57e JA |
2232 | static void io_double_put_req(struct io_kiocb *req) |
2233 | { | |
2234 | /* drop both submit and complete references */ | |
2235 | if (refcount_sub_and_test(2, &req->refs)) | |
2236 | io_free_req(req); | |
2237 | } | |
2238 | ||
1d7bb1d5 | 2239 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 2240 | { |
84f97dc2 JA |
2241 | struct io_rings *rings = ctx->rings; |
2242 | ||
ad3eb2c8 JA |
2243 | if (test_bit(0, &ctx->cq_check_overflow)) { |
2244 | /* | |
2245 | * noflush == true is from the waitqueue handler, just ensure | |
2246 | * we wake up the task, and the next invocation will flush the | |
2247 | * entries. We cannot safely to it from here. | |
2248 | */ | |
94d612e0 | 2249 | if (noflush) |
ad3eb2c8 | 2250 | return -1U; |
1d7bb1d5 | 2251 | |
e6c8aa9a | 2252 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
ad3eb2c8 | 2253 | } |
1d7bb1d5 | 2254 | |
a3a0e43f JA |
2255 | /* See comment at the top of this file */ |
2256 | smp_rmb(); | |
ad3eb2c8 | 2257 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
2258 | } |
2259 | ||
fb5ccc98 PB |
2260 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2261 | { | |
2262 | struct io_rings *rings = ctx->rings; | |
2263 | ||
2264 | /* make sure SQ entry isn't read before tail */ | |
2265 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2266 | } | |
2267 | ||
8ff069bf | 2268 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2269 | { |
8ff069bf | 2270 | unsigned int cflags; |
e94f141b | 2271 | |
bcda7baa JA |
2272 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2273 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2274 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2275 | kfree(kbuf); |
2276 | return cflags; | |
e94f141b JA |
2277 | } |
2278 | ||
8ff069bf | 2279 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2280 | { |
4d954c25 | 2281 | struct io_buffer *kbuf; |
bcda7baa | 2282 | |
4d954c25 | 2283 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2284 | return io_put_kbuf(req, kbuf); |
2285 | } | |
2286 | ||
4c6e277c JA |
2287 | static inline bool io_run_task_work(void) |
2288 | { | |
6200b0ae JA |
2289 | /* |
2290 | * Not safe to run on exiting task, and the task_work handling will | |
2291 | * not add work to such a task. | |
2292 | */ | |
2293 | if (unlikely(current->flags & PF_EXITING)) | |
2294 | return false; | |
4c6e277c JA |
2295 | if (current->task_works) { |
2296 | __set_current_state(TASK_RUNNING); | |
2297 | task_work_run(); | |
2298 | return true; | |
2299 | } | |
2300 | ||
2301 | return false; | |
bcda7baa JA |
2302 | } |
2303 | ||
bbde017a XW |
2304 | static void io_iopoll_queue(struct list_head *again) |
2305 | { | |
2306 | struct io_kiocb *req; | |
2307 | ||
2308 | do { | |
d21ffe7e PB |
2309 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2310 | list_del(&req->inflight_entry); | |
81b68a5c | 2311 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
bbde017a XW |
2312 | } while (!list_empty(again)); |
2313 | } | |
2314 | ||
def596e9 JA |
2315 | /* |
2316 | * Find and free completed poll iocbs | |
2317 | */ | |
2318 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2319 | struct list_head *done) | |
2320 | { | |
8237e045 | 2321 | struct req_batch rb; |
def596e9 | 2322 | struct io_kiocb *req; |
bbde017a XW |
2323 | LIST_HEAD(again); |
2324 | ||
2325 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2326 | smp_rmb(); | |
def596e9 | 2327 | |
5af1d13e | 2328 | io_init_req_batch(&rb); |
def596e9 | 2329 | while (!list_empty(done)) { |
bcda7baa JA |
2330 | int cflags = 0; |
2331 | ||
d21ffe7e | 2332 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a | 2333 | if (READ_ONCE(req->result) == -EAGAIN) { |
56450c20 | 2334 | req->result = 0; |
bbde017a | 2335 | req->iopoll_completed = 0; |
d21ffe7e | 2336 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2337 | continue; |
2338 | } | |
d21ffe7e | 2339 | list_del(&req->inflight_entry); |
def596e9 | 2340 | |
bcda7baa | 2341 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2342 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2343 | |
2344 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2345 | (*nr_events)++; |
2346 | ||
c3524383 | 2347 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2348 | io_req_free_batch(&rb, req); |
def596e9 | 2349 | } |
def596e9 | 2350 | |
09bb8394 | 2351 | io_commit_cqring(ctx); |
32b2244a XW |
2352 | if (ctx->flags & IORING_SETUP_SQPOLL) |
2353 | io_cqring_ev_posted(ctx); | |
2d6500d4 | 2354 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2355 | |
bbde017a XW |
2356 | if (!list_empty(&again)) |
2357 | io_iopoll_queue(&again); | |
581f9810 BM |
2358 | } |
2359 | ||
def596e9 JA |
2360 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2361 | long min) | |
2362 | { | |
2363 | struct io_kiocb *req, *tmp; | |
2364 | LIST_HEAD(done); | |
2365 | bool spin; | |
2366 | int ret; | |
2367 | ||
2368 | /* | |
2369 | * Only spin for completions if we don't have multiple devices hanging | |
2370 | * off our complete list, and we're under the requested amount. | |
2371 | */ | |
2372 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2373 | ||
2374 | ret = 0; | |
d21ffe7e | 2375 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2376 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2377 | |
2378 | /* | |
581f9810 BM |
2379 | * Move completed and retryable entries to our local lists. |
2380 | * If we find a request that requires polling, break out | |
2381 | * and complete those lists first, if we have entries there. | |
def596e9 | 2382 | */ |
65a6543d | 2383 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2384 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2385 | continue; |
2386 | } | |
2387 | if (!list_empty(&done)) | |
2388 | break; | |
2389 | ||
2390 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2391 | if (ret < 0) | |
2392 | break; | |
2393 | ||
3aadc23e PB |
2394 | /* iopoll may have completed current req */ |
2395 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2396 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2397 | |
def596e9 JA |
2398 | if (ret && spin) |
2399 | spin = false; | |
2400 | ret = 0; | |
2401 | } | |
2402 | ||
2403 | if (!list_empty(&done)) | |
2404 | io_iopoll_complete(ctx, nr_events, &done); | |
2405 | ||
2406 | return ret; | |
2407 | } | |
2408 | ||
2409 | /* | |
d195a66e | 2410 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2411 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2412 | * as a non-spinning completion check. | |
2413 | */ | |
2414 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2415 | long min) | |
2416 | { | |
540e32a0 | 2417 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2418 | int ret; |
2419 | ||
2420 | ret = io_do_iopoll(ctx, nr_events, min); | |
2421 | if (ret < 0) | |
2422 | return ret; | |
eba0a4dd | 2423 | if (*nr_events >= min) |
def596e9 JA |
2424 | return 0; |
2425 | } | |
2426 | ||
2427 | return 1; | |
2428 | } | |
2429 | ||
2430 | /* | |
2431 | * We can't just wait for polled events to come to us, we have to actively | |
2432 | * find and complete them. | |
2433 | */ | |
b2edc0a7 | 2434 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2435 | { |
2436 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2437 | return; | |
2438 | ||
2439 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2440 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2441 | unsigned int nr_events = 0; |
2442 | ||
b2edc0a7 | 2443 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2444 | |
b2edc0a7 PB |
2445 | /* let it sleep and repeat later if can't complete a request */ |
2446 | if (nr_events == 0) | |
2447 | break; | |
08f5439f JA |
2448 | /* |
2449 | * Ensure we allow local-to-the-cpu processing to take place, | |
2450 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2451 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2452 | */ |
3fcee5a6 PB |
2453 | if (need_resched()) { |
2454 | mutex_unlock(&ctx->uring_lock); | |
2455 | cond_resched(); | |
2456 | mutex_lock(&ctx->uring_lock); | |
2457 | } | |
def596e9 JA |
2458 | } |
2459 | mutex_unlock(&ctx->uring_lock); | |
2460 | } | |
2461 | ||
7668b92a | 2462 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2463 | { |
7668b92a | 2464 | unsigned int nr_events = 0; |
2b2ed975 | 2465 | int iters = 0, ret = 0; |
500f9fba | 2466 | |
c7849be9 XW |
2467 | /* |
2468 | * We disallow the app entering submit/complete with polling, but we | |
2469 | * still need to lock the ring to prevent racing with polled issue | |
2470 | * that got punted to a workqueue. | |
2471 | */ | |
2472 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2473 | do { |
a3a0e43f JA |
2474 | /* |
2475 | * Don't enter poll loop if we already have events pending. | |
2476 | * If we do, we can potentially be spinning for commands that | |
2477 | * already triggered a CQE (eg in error). | |
2478 | */ | |
1d7bb1d5 | 2479 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
2480 | break; |
2481 | ||
500f9fba JA |
2482 | /* |
2483 | * If a submit got punted to a workqueue, we can have the | |
2484 | * application entering polling for a command before it gets | |
2485 | * issued. That app will hold the uring_lock for the duration | |
2486 | * of the poll right here, so we need to take a breather every | |
2487 | * now and then to ensure that the issue has a chance to add | |
2488 | * the poll to the issued list. Otherwise we can spin here | |
2489 | * forever, while the workqueue is stuck trying to acquire the | |
2490 | * very same mutex. | |
2491 | */ | |
2492 | if (!(++iters & 7)) { | |
2493 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2494 | io_run_task_work(); |
500f9fba JA |
2495 | mutex_lock(&ctx->uring_lock); |
2496 | } | |
2497 | ||
7668b92a | 2498 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2499 | if (ret <= 0) |
2500 | break; | |
2501 | ret = 0; | |
7668b92a | 2502 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2503 | |
500f9fba | 2504 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2505 | return ret; |
2506 | } | |
2507 | ||
491381ce | 2508 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2509 | { |
491381ce JA |
2510 | /* |
2511 | * Tell lockdep we inherited freeze protection from submission | |
2512 | * thread. | |
2513 | */ | |
2514 | if (req->flags & REQ_F_ISREG) { | |
2515 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2516 | |
491381ce | 2517 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2518 | } |
491381ce | 2519 | file_end_write(req->file); |
2b188cc1 JA |
2520 | } |
2521 | ||
a1d7c393 JA |
2522 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
2523 | struct io_comp_state *cs) | |
2b188cc1 | 2524 | { |
9adbd45d | 2525 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2526 | int cflags = 0; |
2b188cc1 | 2527 | |
491381ce JA |
2528 | if (kiocb->ki_flags & IOCB_WRITE) |
2529 | kiocb_end_write(req); | |
2b188cc1 | 2530 | |
4e88d6e7 JA |
2531 | if (res != req->result) |
2532 | req_set_fail_links(req); | |
bcda7baa | 2533 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2534 | cflags = io_put_rw_kbuf(req); |
a1d7c393 | 2535 | __io_req_complete(req, res, cflags, cs); |
ba816ad6 JA |
2536 | } |
2537 | ||
b63534c4 JA |
2538 | #ifdef CONFIG_BLOCK |
2539 | static bool io_resubmit_prep(struct io_kiocb *req, int error) | |
2540 | { | |
2541 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
2542 | ssize_t ret = -ECANCELED; | |
2543 | struct iov_iter iter; | |
2544 | int rw; | |
2545 | ||
2546 | if (error) { | |
2547 | ret = error; | |
2548 | goto end_req; | |
2549 | } | |
2550 | ||
2551 | switch (req->opcode) { | |
2552 | case IORING_OP_READV: | |
2553 | case IORING_OP_READ_FIXED: | |
2554 | case IORING_OP_READ: | |
2555 | rw = READ; | |
2556 | break; | |
2557 | case IORING_OP_WRITEV: | |
2558 | case IORING_OP_WRITE_FIXED: | |
2559 | case IORING_OP_WRITE: | |
2560 | rw = WRITE; | |
2561 | break; | |
2562 | default: | |
2563 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2564 | req->opcode); | |
2565 | goto end_req; | |
2566 | } | |
2567 | ||
e8c2bc1f | 2568 | if (!req->async_data) { |
8f3d7496 JA |
2569 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2570 | if (ret < 0) | |
2571 | goto end_req; | |
2572 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); | |
2573 | if (!ret) | |
2574 | return true; | |
2575 | kfree(iovec); | |
2576 | } else { | |
b63534c4 | 2577 | return true; |
8f3d7496 | 2578 | } |
b63534c4 | 2579 | end_req: |
b63534c4 | 2580 | req_set_fail_links(req); |
b63534c4 JA |
2581 | return false; |
2582 | } | |
b63534c4 JA |
2583 | #endif |
2584 | ||
2585 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2586 | { | |
2587 | #ifdef CONFIG_BLOCK | |
355afaeb | 2588 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2589 | int ret; |
2590 | ||
355afaeb JA |
2591 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2592 | return false; | |
b63534c4 JA |
2593 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
2594 | return false; | |
2595 | ||
fdee946d | 2596 | ret = io_sq_thread_acquire_mm(req->ctx, req); |
6d816e08 | 2597 | |
fdee946d JA |
2598 | if (io_resubmit_prep(req, ret)) { |
2599 | refcount_inc(&req->refs); | |
2600 | io_queue_async_work(req); | |
b63534c4 | 2601 | return true; |
fdee946d JA |
2602 | } |
2603 | ||
b63534c4 JA |
2604 | #endif |
2605 | return false; | |
2606 | } | |
2607 | ||
a1d7c393 JA |
2608 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2609 | struct io_comp_state *cs) | |
2610 | { | |
2611 | if (!io_rw_reissue(req, res)) | |
2612 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2613 | } |
2614 | ||
2615 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2616 | { | |
9adbd45d | 2617 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2618 | |
a1d7c393 | 2619 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2620 | } |
2621 | ||
def596e9 JA |
2622 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2623 | { | |
9adbd45d | 2624 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2625 | |
491381ce JA |
2626 | if (kiocb->ki_flags & IOCB_WRITE) |
2627 | kiocb_end_write(req); | |
def596e9 | 2628 | |
2d7d6792 | 2629 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2630 | req_set_fail_links(req); |
bbde017a XW |
2631 | |
2632 | WRITE_ONCE(req->result, res); | |
2633 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2634 | smp_wmb(); |
2635 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2636 | } |
2637 | ||
2638 | /* | |
2639 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2640 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2641 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2642 | * accessing the kiocb cookie. | |
2643 | */ | |
2644 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
2645 | { | |
2646 | struct io_ring_ctx *ctx = req->ctx; | |
2647 | ||
2648 | /* | |
2649 | * Track whether we have multiple files in our lists. This will impact | |
2650 | * how we do polling eventually, not spinning if we're on potentially | |
2651 | * different devices. | |
2652 | */ | |
540e32a0 | 2653 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2654 | ctx->poll_multi_file = false; |
2655 | } else if (!ctx->poll_multi_file) { | |
2656 | struct io_kiocb *list_req; | |
2657 | ||
540e32a0 | 2658 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2659 | inflight_entry); |
9adbd45d | 2660 | if (list_req->file != req->file) |
def596e9 JA |
2661 | ctx->poll_multi_file = true; |
2662 | } | |
2663 | ||
2664 | /* | |
2665 | * For fast devices, IO may have already completed. If it has, add | |
2666 | * it to the front so we find it first. | |
2667 | */ | |
65a6543d | 2668 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2669 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2670 | else |
d21ffe7e | 2671 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab XW |
2672 | |
2673 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2674 | wq_has_sleeper(&ctx->sq_data->wait)) |
2675 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2676 | } |
2677 | ||
9f13c35b | 2678 | static void __io_state_file_put(struct io_submit_state *state) |
9a56a232 | 2679 | { |
06ef3608 PB |
2680 | if (state->has_refs) |
2681 | fput_many(state->file, state->has_refs); | |
9f13c35b PB |
2682 | state->file = NULL; |
2683 | } | |
2684 | ||
2685 | static inline void io_state_file_put(struct io_submit_state *state) | |
2686 | { | |
2687 | if (state->file) | |
2688 | __io_state_file_put(state); | |
9a56a232 JA |
2689 | } |
2690 | ||
2691 | /* | |
2692 | * Get as many references to a file as we have IOs left in this submission, | |
2693 | * assuming most submissions are for one file, or at least that each file | |
2694 | * has more than one submission. | |
2695 | */ | |
8da11c19 | 2696 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2697 | { |
2698 | if (!state) | |
2699 | return fget(fd); | |
2700 | ||
2701 | if (state->file) { | |
2702 | if (state->fd == fd) { | |
06ef3608 | 2703 | state->has_refs--; |
9a56a232 JA |
2704 | return state->file; |
2705 | } | |
9f13c35b | 2706 | __io_state_file_put(state); |
9a56a232 JA |
2707 | } |
2708 | state->file = fget_many(fd, state->ios_left); | |
2709 | if (!state->file) | |
2710 | return NULL; | |
2711 | ||
2712 | state->fd = fd; | |
71b547c0 | 2713 | state->has_refs = state->ios_left - 1; |
9a56a232 JA |
2714 | return state->file; |
2715 | } | |
2716 | ||
4503b767 JA |
2717 | static bool io_bdev_nowait(struct block_device *bdev) |
2718 | { | |
2719 | #ifdef CONFIG_BLOCK | |
9ba0d0c8 | 2720 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2721 | #else |
2722 | return true; | |
2723 | #endif | |
2724 | } | |
2725 | ||
2b188cc1 JA |
2726 | /* |
2727 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2728 | * any file. For now, just ensure that anything potentially problematic is done | |
2729 | * inline. | |
2730 | */ | |
af197f50 | 2731 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2732 | { |
2733 | umode_t mode = file_inode(file)->i_mode; | |
2734 | ||
4503b767 JA |
2735 | if (S_ISBLK(mode)) { |
2736 | if (io_bdev_nowait(file->f_inode->i_bdev)) | |
2737 | return true; | |
2738 | return false; | |
2739 | } | |
2740 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2741 | return true; |
4503b767 JA |
2742 | if (S_ISREG(mode)) { |
2743 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
2744 | file->f_op != &io_uring_fops) | |
2745 | return true; | |
2746 | return false; | |
2747 | } | |
2b188cc1 | 2748 | |
c5b85625 JA |
2749 | /* any ->read/write should understand O_NONBLOCK */ |
2750 | if (file->f_flags & O_NONBLOCK) | |
2751 | return true; | |
2752 | ||
af197f50 JA |
2753 | if (!(file->f_mode & FMODE_NOWAIT)) |
2754 | return false; | |
2755 | ||
2756 | if (rw == READ) | |
2757 | return file->f_op->read_iter != NULL; | |
2758 | ||
2759 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2760 | } |
2761 | ||
a88fc400 | 2762 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2763 | { |
def596e9 | 2764 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2765 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2766 | unsigned ioprio; |
2767 | int ret; | |
2b188cc1 | 2768 | |
491381ce JA |
2769 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2770 | req->flags |= REQ_F_ISREG; | |
2771 | ||
2b188cc1 | 2772 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2773 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2774 | req->flags |= REQ_F_CUR_POS; | |
2775 | kiocb->ki_pos = req->file->f_pos; | |
2776 | } | |
2b188cc1 | 2777 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2778 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2779 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2780 | if (unlikely(ret)) | |
2781 | return ret; | |
2b188cc1 JA |
2782 | |
2783 | ioprio = READ_ONCE(sqe->ioprio); | |
2784 | if (ioprio) { | |
2785 | ret = ioprio_check_cap(ioprio); | |
2786 | if (ret) | |
09bb8394 | 2787 | return ret; |
2b188cc1 JA |
2788 | |
2789 | kiocb->ki_ioprio = ioprio; | |
2790 | } else | |
2791 | kiocb->ki_ioprio = get_current_ioprio(); | |
2792 | ||
8449eeda | 2793 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2794 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2795 | req->flags |= REQ_F_NOWAIT; |
2796 | ||
def596e9 | 2797 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2798 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2799 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2800 | return -EOPNOTSUPP; |
2b188cc1 | 2801 | |
def596e9 JA |
2802 | kiocb->ki_flags |= IOCB_HIPRI; |
2803 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2804 | req->iopoll_completed = 0; |
def596e9 | 2805 | } else { |
09bb8394 JA |
2806 | if (kiocb->ki_flags & IOCB_HIPRI) |
2807 | return -EINVAL; | |
def596e9 JA |
2808 | kiocb->ki_complete = io_complete_rw; |
2809 | } | |
9adbd45d | 2810 | |
3529d8c2 JA |
2811 | req->rw.addr = READ_ONCE(sqe->addr); |
2812 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2813 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2814 | return 0; |
2b188cc1 JA |
2815 | } |
2816 | ||
2817 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2818 | { | |
2819 | switch (ret) { | |
2820 | case -EIOCBQUEUED: | |
2821 | break; | |
2822 | case -ERESTARTSYS: | |
2823 | case -ERESTARTNOINTR: | |
2824 | case -ERESTARTNOHAND: | |
2825 | case -ERESTART_RESTARTBLOCK: | |
2826 | /* | |
2827 | * We can't just restart the syscall, since previously | |
2828 | * submitted sqes may already be in progress. Just fail this | |
2829 | * IO with EINTR. | |
2830 | */ | |
2831 | ret = -EINTR; | |
df561f66 | 2832 | fallthrough; |
2b188cc1 JA |
2833 | default: |
2834 | kiocb->ki_complete(kiocb, ret, 0); | |
2835 | } | |
2836 | } | |
2837 | ||
a1d7c393 JA |
2838 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2839 | struct io_comp_state *cs) | |
ba816ad6 | 2840 | { |
ba04291e | 2841 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2842 | struct io_async_rw *io = req->async_data; |
ba04291e | 2843 | |
227c0c96 | 2844 | /* add previously done IO, if any */ |
e8c2bc1f | 2845 | if (io && io->bytes_done > 0) { |
227c0c96 | 2846 | if (ret < 0) |
e8c2bc1f | 2847 | ret = io->bytes_done; |
227c0c96 | 2848 | else |
e8c2bc1f | 2849 | ret += io->bytes_done; |
227c0c96 JA |
2850 | } |
2851 | ||
ba04291e JA |
2852 | if (req->flags & REQ_F_CUR_POS) |
2853 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2854 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 2855 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
2856 | else |
2857 | io_rw_done(kiocb, ret); | |
2858 | } | |
2859 | ||
9adbd45d | 2860 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 2861 | struct iov_iter *iter) |
edafccee | 2862 | { |
9adbd45d JA |
2863 | struct io_ring_ctx *ctx = req->ctx; |
2864 | size_t len = req->rw.len; | |
edafccee | 2865 | struct io_mapped_ubuf *imu; |
4be1c615 | 2866 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2867 | size_t offset; |
2868 | u64 buf_addr; | |
2869 | ||
edafccee JA |
2870 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2871 | return -EFAULT; | |
edafccee JA |
2872 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2873 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2874 | buf_addr = req->rw.addr; |
edafccee JA |
2875 | |
2876 | /* overflow */ | |
2877 | if (buf_addr + len < buf_addr) | |
2878 | return -EFAULT; | |
2879 | /* not inside the mapped region */ | |
2880 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2881 | return -EFAULT; | |
2882 | ||
2883 | /* | |
2884 | * May not be a start of buffer, set size appropriately | |
2885 | * and advance us to the beginning. | |
2886 | */ | |
2887 | offset = buf_addr - imu->ubuf; | |
2888 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2889 | |
2890 | if (offset) { | |
2891 | /* | |
2892 | * Don't use iov_iter_advance() here, as it's really slow for | |
2893 | * using the latter parts of a big fixed buffer - it iterates | |
2894 | * over each segment manually. We can cheat a bit here, because | |
2895 | * we know that: | |
2896 | * | |
2897 | * 1) it's a BVEC iter, we set it up | |
2898 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2899 | * first and last bvec | |
2900 | * | |
2901 | * So just find our index, and adjust the iterator afterwards. | |
2902 | * If the offset is within the first bvec (or the whole first | |
2903 | * bvec, just use iov_iter_advance(). This makes it easier | |
2904 | * since we can just skip the first segment, which may not | |
2905 | * be PAGE_SIZE aligned. | |
2906 | */ | |
2907 | const struct bio_vec *bvec = imu->bvec; | |
2908 | ||
2909 | if (offset <= bvec->bv_len) { | |
2910 | iov_iter_advance(iter, offset); | |
2911 | } else { | |
2912 | unsigned long seg_skip; | |
2913 | ||
2914 | /* skip first vec */ | |
2915 | offset -= bvec->bv_len; | |
2916 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2917 | ||
2918 | iter->bvec = bvec + seg_skip; | |
2919 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2920 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2921 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2922 | } |
2923 | } | |
2924 | ||
5e559561 | 2925 | return len; |
edafccee JA |
2926 | } |
2927 | ||
bcda7baa JA |
2928 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2929 | { | |
2930 | if (needs_lock) | |
2931 | mutex_unlock(&ctx->uring_lock); | |
2932 | } | |
2933 | ||
2934 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2935 | { | |
2936 | /* | |
2937 | * "Normal" inline submissions always hold the uring_lock, since we | |
2938 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2939 | * The only exception is when we've detached the request and issue it | |
2940 | * from an async worker thread, grab the lock for that case. | |
2941 | */ | |
2942 | if (needs_lock) | |
2943 | mutex_lock(&ctx->uring_lock); | |
2944 | } | |
2945 | ||
2946 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2947 | int bgid, struct io_buffer *kbuf, | |
2948 | bool needs_lock) | |
2949 | { | |
2950 | struct io_buffer *head; | |
2951 | ||
2952 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2953 | return kbuf; | |
2954 | ||
2955 | io_ring_submit_lock(req->ctx, needs_lock); | |
2956 | ||
2957 | lockdep_assert_held(&req->ctx->uring_lock); | |
2958 | ||
2959 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2960 | if (head) { | |
2961 | if (!list_empty(&head->list)) { | |
2962 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2963 | list); | |
2964 | list_del(&kbuf->list); | |
2965 | } else { | |
2966 | kbuf = head; | |
2967 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2968 | } | |
2969 | if (*len > kbuf->len) | |
2970 | *len = kbuf->len; | |
2971 | } else { | |
2972 | kbuf = ERR_PTR(-ENOBUFS); | |
2973 | } | |
2974 | ||
2975 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2976 | ||
2977 | return kbuf; | |
2978 | } | |
2979 | ||
4d954c25 JA |
2980 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2981 | bool needs_lock) | |
2982 | { | |
2983 | struct io_buffer *kbuf; | |
4f4eeba8 | 2984 | u16 bgid; |
4d954c25 JA |
2985 | |
2986 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2987 | bgid = req->buf_index; |
4d954c25 JA |
2988 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2989 | if (IS_ERR(kbuf)) | |
2990 | return kbuf; | |
2991 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2992 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2993 | return u64_to_user_ptr(kbuf->addr); | |
2994 | } | |
2995 | ||
2996 | #ifdef CONFIG_COMPAT | |
2997 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2998 | bool needs_lock) | |
2999 | { | |
3000 | struct compat_iovec __user *uiov; | |
3001 | compat_ssize_t clen; | |
3002 | void __user *buf; | |
3003 | ssize_t len; | |
3004 | ||
3005 | uiov = u64_to_user_ptr(req->rw.addr); | |
3006 | if (!access_ok(uiov, sizeof(*uiov))) | |
3007 | return -EFAULT; | |
3008 | if (__get_user(clen, &uiov->iov_len)) | |
3009 | return -EFAULT; | |
3010 | if (clen < 0) | |
3011 | return -EINVAL; | |
3012 | ||
3013 | len = clen; | |
3014 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3015 | if (IS_ERR(buf)) | |
3016 | return PTR_ERR(buf); | |
3017 | iov[0].iov_base = buf; | |
3018 | iov[0].iov_len = (compat_size_t) len; | |
3019 | return 0; | |
3020 | } | |
3021 | #endif | |
3022 | ||
3023 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3024 | bool needs_lock) | |
3025 | { | |
3026 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3027 | void __user *buf; | |
3028 | ssize_t len; | |
3029 | ||
3030 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3031 | return -EFAULT; | |
3032 | ||
3033 | len = iov[0].iov_len; | |
3034 | if (len < 0) | |
3035 | return -EINVAL; | |
3036 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3037 | if (IS_ERR(buf)) | |
3038 | return PTR_ERR(buf); | |
3039 | iov[0].iov_base = buf; | |
3040 | iov[0].iov_len = len; | |
3041 | return 0; | |
3042 | } | |
3043 | ||
3044 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3045 | bool needs_lock) | |
3046 | { | |
dddb3e26 JA |
3047 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3048 | struct io_buffer *kbuf; | |
3049 | ||
3050 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3051 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3052 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3053 | return 0; |
dddb3e26 | 3054 | } |
6968c2a0 | 3055 | if (req->rw.len != 1) |
4d954c25 JA |
3056 | return -EINVAL; |
3057 | ||
3058 | #ifdef CONFIG_COMPAT | |
3059 | if (req->ctx->compat) | |
3060 | return io_compat_import(req, iov, needs_lock); | |
3061 | #endif | |
3062 | ||
3063 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3064 | } | |
3065 | ||
8452fd0c JA |
3066 | static ssize_t __io_import_iovec(int rw, struct io_kiocb *req, |
3067 | struct iovec **iovec, struct iov_iter *iter, | |
3068 | bool needs_lock) | |
2b188cc1 | 3069 | { |
9adbd45d JA |
3070 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3071 | size_t sqe_len = req->rw.len; | |
4d954c25 | 3072 | ssize_t ret; |
edafccee JA |
3073 | u8 opcode; |
3074 | ||
d625c6ee | 3075 | opcode = req->opcode; |
7d009165 | 3076 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3077 | *iovec = NULL; |
9adbd45d | 3078 | return io_import_fixed(req, rw, iter); |
edafccee | 3079 | } |
2b188cc1 | 3080 | |
bcda7baa | 3081 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3082 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3083 | return -EINVAL; |
3084 | ||
3a6820f2 | 3085 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3086 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3087 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3088 | if (IS_ERR(buf)) |
4d954c25 | 3089 | return PTR_ERR(buf); |
3f9d6441 | 3090 | req->rw.len = sqe_len; |
bcda7baa JA |
3091 | } |
3092 | ||
3a6820f2 JA |
3093 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3094 | *iovec = NULL; | |
3a901598 | 3095 | return ret < 0 ? ret : sqe_len; |
3a6820f2 JA |
3096 | } |
3097 | ||
4d954c25 JA |
3098 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3099 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
3100 | if (!ret) { |
3101 | ret = (*iovec)->iov_len; | |
3102 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
3103 | } | |
4d954c25 JA |
3104 | *iovec = NULL; |
3105 | return ret; | |
3106 | } | |
3107 | ||
89cd35c5 CH |
3108 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3109 | req->ctx->compat); | |
2b188cc1 JA |
3110 | } |
3111 | ||
8452fd0c JA |
3112 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
3113 | struct iovec **iovec, struct iov_iter *iter, | |
3114 | bool needs_lock) | |
3115 | { | |
e8c2bc1f JA |
3116 | struct io_async_rw *iorw = req->async_data; |
3117 | ||
3118 | if (!iorw) | |
8452fd0c JA |
3119 | return __io_import_iovec(rw, req, iovec, iter, needs_lock); |
3120 | *iovec = NULL; | |
e8c2bc1f | 3121 | return iov_iter_count(&iorw->iter); |
8452fd0c JA |
3122 | } |
3123 | ||
0fef9483 JA |
3124 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3125 | { | |
5b09e37e | 3126 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3127 | } |
3128 | ||
31b51510 | 3129 | /* |
32960613 JA |
3130 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3131 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3132 | */ |
4017eb91 | 3133 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3134 | { |
4017eb91 JA |
3135 | struct kiocb *kiocb = &req->rw.kiocb; |
3136 | struct file *file = req->file; | |
32960613 JA |
3137 | ssize_t ret = 0; |
3138 | ||
3139 | /* | |
3140 | * Don't support polled IO through this interface, and we can't | |
3141 | * support non-blocking either. For the latter, this just causes | |
3142 | * the kiocb to be handled from an async context. | |
3143 | */ | |
3144 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3145 | return -EOPNOTSUPP; | |
3146 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3147 | return -EAGAIN; | |
3148 | ||
3149 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3150 | struct iovec iovec; |
32960613 JA |
3151 | ssize_t nr; |
3152 | ||
311ae9e1 PB |
3153 | if (!iov_iter_is_bvec(iter)) { |
3154 | iovec = iov_iter_iovec(iter); | |
3155 | } else { | |
4017eb91 JA |
3156 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3157 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3158 | } |
3159 | ||
32960613 JA |
3160 | if (rw == READ) { |
3161 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3162 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3163 | } else { |
3164 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3165 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3166 | } |
3167 | ||
3168 | if (nr < 0) { | |
3169 | if (!ret) | |
3170 | ret = nr; | |
3171 | break; | |
3172 | } | |
3173 | ret += nr; | |
3174 | if (nr != iovec.iov_len) | |
3175 | break; | |
4017eb91 JA |
3176 | req->rw.len -= nr; |
3177 | req->rw.addr += nr; | |
32960613 JA |
3178 | iov_iter_advance(iter, nr); |
3179 | } | |
3180 | ||
3181 | return ret; | |
3182 | } | |
3183 | ||
ff6165b2 JA |
3184 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3185 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3186 | { |
e8c2bc1f | 3187 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3188 | |
ff6165b2 | 3189 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3190 | rw->free_iovec = iovec; |
227c0c96 | 3191 | rw->bytes_done = 0; |
ff6165b2 | 3192 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3193 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3194 | return; |
b64e3444 | 3195 | if (!iovec) { |
ff6165b2 JA |
3196 | unsigned iov_off = 0; |
3197 | ||
3198 | rw->iter.iov = rw->fast_iov; | |
3199 | if (iter->iov != fast_iov) { | |
3200 | iov_off = iter->iov - fast_iov; | |
3201 | rw->iter.iov += iov_off; | |
3202 | } | |
3203 | if (rw->fast_iov != fast_iov) | |
3204 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3205 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3206 | } else { |
3207 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3208 | } |
3209 | } | |
3210 | ||
e8c2bc1f | 3211 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3212 | { |
e8c2bc1f JA |
3213 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3214 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3215 | return req->async_data == NULL; | |
3d9932a8 XW |
3216 | } |
3217 | ||
e8c2bc1f | 3218 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3219 | { |
e8c2bc1f | 3220 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3221 | return 0; |
3d9932a8 | 3222 | |
e8c2bc1f | 3223 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3224 | } |
3225 | ||
ff6165b2 JA |
3226 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3227 | const struct iovec *fast_iov, | |
227c0c96 | 3228 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3229 | { |
e8c2bc1f | 3230 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3231 | return 0; |
e8c2bc1f JA |
3232 | if (!req->async_data) { |
3233 | if (__io_alloc_async_data(req)) | |
5d204bcf | 3234 | return -ENOMEM; |
b7bb4f7d | 3235 | |
ff6165b2 | 3236 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3237 | } |
b7bb4f7d | 3238 | return 0; |
f67676d1 JA |
3239 | } |
3240 | ||
73debe68 | 3241 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3242 | { |
e8c2bc1f | 3243 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3244 | struct iovec *iov = iorw->fast_iov; |
c3e330a4 PB |
3245 | ssize_t ret; |
3246 | ||
73debe68 | 3247 | ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3248 | if (unlikely(ret < 0)) |
3249 | return ret; | |
3250 | ||
ab0b196c PB |
3251 | iorw->bytes_done = 0; |
3252 | iorw->free_iovec = iov; | |
3253 | if (iov) | |
3254 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3255 | return 0; |
3256 | } | |
3257 | ||
73debe68 | 3258 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3259 | { |
3260 | ssize_t ret; | |
3261 | ||
a88fc400 | 3262 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3263 | if (ret) |
3264 | return ret; | |
f67676d1 | 3265 | |
3529d8c2 JA |
3266 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3267 | return -EBADF; | |
f67676d1 | 3268 | |
5f798bea | 3269 | /* either don't need iovec imported or already have it */ |
2d199895 | 3270 | if (!req->async_data) |
3529d8c2 | 3271 | return 0; |
73debe68 | 3272 | return io_rw_prep_async(req, READ); |
f67676d1 JA |
3273 | } |
3274 | ||
c1dd91d1 JA |
3275 | /* |
3276 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3277 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3278 | * This gets called when the page is unlocked, and we generally expect that to | |
3279 | * happen when the page IO is completed and the page is now uptodate. This will | |
3280 | * queue a task_work based retry of the operation, attempting to copy the data | |
3281 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3282 | * do a thread based blocking retry of the operation. That's the unexpected | |
3283 | * slow path. | |
3284 | */ | |
bcf5a063 JA |
3285 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3286 | int sync, void *arg) | |
3287 | { | |
3288 | struct wait_page_queue *wpq; | |
3289 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3290 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3291 | int ret; |
3292 | ||
3293 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3294 | ||
cdc8fcb4 LT |
3295 | if (!wake_page_match(wpq, key)) |
3296 | return 0; | |
3297 | ||
c8d317aa | 3298 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3299 | list_del_init(&wait->entry); |
3300 | ||
e7375122 | 3301 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3302 | percpu_ref_get(&req->ctx->refs); |
3303 | ||
bcf5a063 JA |
3304 | /* submit ref gets dropped, acquire a new one */ |
3305 | refcount_inc(&req->refs); | |
87c4311f | 3306 | ret = io_req_task_work_add(req, true); |
bcf5a063 | 3307 | if (unlikely(ret)) { |
c2c4c83c JA |
3308 | struct task_struct *tsk; |
3309 | ||
bcf5a063 | 3310 | /* queue just for cancelation */ |
e7375122 | 3311 | init_task_work(&req->task_work, io_req_task_cancel); |
bcf5a063 | 3312 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 3313 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 3314 | wake_up_process(tsk); |
bcf5a063 | 3315 | } |
bcf5a063 JA |
3316 | return 1; |
3317 | } | |
3318 | ||
c1dd91d1 JA |
3319 | /* |
3320 | * This controls whether a given IO request should be armed for async page | |
3321 | * based retry. If we return false here, the request is handed to the async | |
3322 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3323 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3324 | * will either succeed because the page is now uptodate and unlocked, or it | |
3325 | * will register a callback when the page is unlocked at IO completion. Through | |
3326 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3327 | * That retry will attempt the buffered read again. The retry will generally | |
3328 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3329 | * async worker threads for a blocking retry. | |
3330 | */ | |
227c0c96 | 3331 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3332 | { |
e8c2bc1f JA |
3333 | struct io_async_rw *rw = req->async_data; |
3334 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3335 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3336 | |
bcf5a063 JA |
3337 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3338 | if (req->flags & REQ_F_NOWAIT) | |
3339 | return false; | |
f67676d1 | 3340 | |
227c0c96 | 3341 | /* Only for buffered IO */ |
3b2a4439 | 3342 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3343 | return false; |
3b2a4439 | 3344 | |
bcf5a063 JA |
3345 | /* |
3346 | * just use poll if we can, and don't attempt if the fs doesn't | |
3347 | * support callback based unlocks | |
3348 | */ | |
3349 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3350 | return false; | |
f67676d1 | 3351 | |
3b2a4439 JA |
3352 | wait->wait.func = io_async_buf_func; |
3353 | wait->wait.private = req; | |
3354 | wait->wait.flags = 0; | |
3355 | INIT_LIST_HEAD(&wait->wait.entry); | |
3356 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3357 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3358 | kiocb->ki_waitq = wait; |
3b2a4439 | 3359 | return true; |
bcf5a063 JA |
3360 | } |
3361 | ||
3362 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3363 | { | |
3364 | if (req->file->f_op->read_iter) | |
3365 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3366 | else if (req->file->f_op->read) |
4017eb91 | 3367 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3368 | else |
3369 | return -EINVAL; | |
f67676d1 JA |
3370 | } |
3371 | ||
a1d7c393 JA |
3372 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3373 | struct io_comp_state *cs) | |
2b188cc1 JA |
3374 | { |
3375 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3376 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3377 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3378 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3379 | ssize_t io_size, ret, ret2; |
31b51510 | 3380 | size_t iov_count; |
f5cac8b1 | 3381 | bool no_async; |
ff6165b2 | 3382 | |
e8c2bc1f JA |
3383 | if (rw) |
3384 | iter = &rw->iter; | |
2b188cc1 | 3385 | |
ff6165b2 | 3386 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
06b76d44 JA |
3387 | if (ret < 0) |
3388 | return ret; | |
eefdf30f | 3389 | iov_count = iov_iter_count(iter); |
fa15bafb PB |
3390 | io_size = ret; |
3391 | req->result = io_size; | |
227c0c96 | 3392 | ret = 0; |
2b188cc1 | 3393 | |
fd6c2e4c JA |
3394 | /* Ensure we clear previously set non-block flag */ |
3395 | if (!force_nonblock) | |
29de5f6a | 3396 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3397 | else |
3398 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3399 | ||
fd6c2e4c | 3400 | |
24c74678 | 3401 | /* If the file doesn't support async, just async punt */ |
f5cac8b1 JA |
3402 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
3403 | if (no_async) | |
f67676d1 | 3404 | goto copy_iov; |
9e645e11 | 3405 | |
0fef9483 | 3406 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count); |
fa15bafb PB |
3407 | if (unlikely(ret)) |
3408 | goto out_free; | |
2b188cc1 | 3409 | |
227c0c96 | 3410 | ret = io_iter_do_read(req, iter); |
32960613 | 3411 | |
227c0c96 JA |
3412 | if (!ret) { |
3413 | goto done; | |
3414 | } else if (ret == -EIOCBQUEUED) { | |
3415 | ret = 0; | |
3416 | goto out_free; | |
3417 | } else if (ret == -EAGAIN) { | |
eefdf30f JA |
3418 | /* IOPOLL retry should happen for io-wq threads */ |
3419 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3420 | goto done; |
355afaeb JA |
3421 | /* no retry on NONBLOCK marked file */ |
3422 | if (req->file->f_flags & O_NONBLOCK) | |
3423 | goto done; | |
84216315 JA |
3424 | /* some cases will consume bytes even on error returns */ |
3425 | iov_iter_revert(iter, iov_count - iov_iter_count(iter)); | |
f38c7e3a JA |
3426 | ret = 0; |
3427 | goto copy_iov; | |
227c0c96 | 3428 | } else if (ret < 0) { |
00d23d51 JA |
3429 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
3430 | goto done; | |
227c0c96 JA |
3431 | } |
3432 | ||
3433 | /* read it all, or we did blocking attempt. no retry. */ | |
f91daf56 JA |
3434 | if (!iov_iter_count(iter) || !force_nonblock || |
3435 | (req->file->f_flags & O_NONBLOCK)) | |
227c0c96 JA |
3436 | goto done; |
3437 | ||
3438 | io_size -= ret; | |
3439 | copy_iov: | |
3440 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
3441 | if (ret2) { | |
3442 | ret = ret2; | |
3443 | goto out_free; | |
3444 | } | |
f5cac8b1 JA |
3445 | if (no_async) |
3446 | return -EAGAIN; | |
e8c2bc1f | 3447 | rw = req->async_data; |
227c0c96 JA |
3448 | /* it's copied and will be cleaned with ->io */ |
3449 | iovec = NULL; | |
3450 | /* now use our persistent iterator, if we aren't already */ | |
e8c2bc1f | 3451 | iter = &rw->iter; |
227c0c96 | 3452 | retry: |
e8c2bc1f | 3453 | rw->bytes_done += ret; |
227c0c96 JA |
3454 | /* if we can retry, do so with the callbacks armed */ |
3455 | if (!io_rw_should_retry(req)) { | |
fa15bafb PB |
3456 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3457 | return -EAGAIN; | |
2b188cc1 | 3458 | } |
227c0c96 JA |
3459 | |
3460 | /* | |
3461 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we | |
3462 | * get -EIOCBQUEUED, then we'll get a notification when the desired | |
3463 | * page gets unlocked. We can also get a partial read here, and if we | |
3464 | * do, then just retry at the new offset. | |
3465 | */ | |
3466 | ret = io_iter_do_read(req, iter); | |
3467 | if (ret == -EIOCBQUEUED) { | |
3468 | ret = 0; | |
3469 | goto out_free; | |
3470 | } else if (ret > 0 && ret < io_size) { | |
3471 | /* we got some bytes, but not all. retry. */ | |
3472 | goto retry; | |
3473 | } | |
3474 | done: | |
3475 | kiocb_done(kiocb, ret, cs); | |
3476 | ret = 0; | |
f67676d1 | 3477 | out_free: |
f261c168 | 3478 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3479 | if (iovec) |
6f2cc166 | 3480 | kfree(iovec); |
2b188cc1 JA |
3481 | return ret; |
3482 | } | |
3483 | ||
73debe68 | 3484 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3485 | { |
3486 | ssize_t ret; | |
3487 | ||
a88fc400 | 3488 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3489 | if (ret) |
3490 | return ret; | |
f67676d1 | 3491 | |
3529d8c2 JA |
3492 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3493 | return -EBADF; | |
f67676d1 | 3494 | |
5f798bea | 3495 | /* either don't need iovec imported or already have it */ |
2d199895 | 3496 | if (!req->async_data) |
3529d8c2 | 3497 | return 0; |
73debe68 | 3498 | return io_rw_prep_async(req, WRITE); |
f67676d1 JA |
3499 | } |
3500 | ||
a1d7c393 JA |
3501 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3502 | struct io_comp_state *cs) | |
2b188cc1 JA |
3503 | { |
3504 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3505 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3506 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3507 | struct io_async_rw *rw = req->async_data; |
31b51510 | 3508 | size_t iov_count; |
fa15bafb | 3509 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3510 | |
e8c2bc1f JA |
3511 | if (rw) |
3512 | iter = &rw->iter; | |
ff6165b2 JA |
3513 | |
3514 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
06b76d44 JA |
3515 | if (ret < 0) |
3516 | return ret; | |
eefdf30f | 3517 | iov_count = iov_iter_count(iter); |
fa15bafb PB |
3518 | io_size = ret; |
3519 | req->result = io_size; | |
2b188cc1 | 3520 | |
fd6c2e4c JA |
3521 | /* Ensure we clear previously set non-block flag */ |
3522 | if (!force_nonblock) | |
a88fc400 PB |
3523 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3524 | else | |
3525 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3526 | |
24c74678 | 3527 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3528 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3529 | goto copy_iov; |
31b51510 | 3530 | |
10d59345 JA |
3531 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3532 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3533 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3534 | goto copy_iov; |
31b51510 | 3535 | |
0fef9483 | 3536 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count); |
fa15bafb PB |
3537 | if (unlikely(ret)) |
3538 | goto out_free; | |
4ed734b0 | 3539 | |
fa15bafb PB |
3540 | /* |
3541 | * Open-code file_start_write here to grab freeze protection, | |
3542 | * which will be released by another thread in | |
3543 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3544 | * released so that it doesn't complain about the held lock when | |
3545 | * we return to userspace. | |
3546 | */ | |
3547 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3548 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3549 | __sb_writers_release(file_inode(req->file)->i_sb, |
3550 | SB_FREEZE_WRITE); | |
3551 | } | |
3552 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3553 | |
fa15bafb | 3554 | if (req->file->f_op->write_iter) |
ff6165b2 | 3555 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3556 | else if (req->file->f_op->write) |
4017eb91 | 3557 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3558 | else |
3559 | ret2 = -EINVAL; | |
4ed734b0 | 3560 | |
fa15bafb PB |
3561 | /* |
3562 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3563 | * retry them without IOCB_NOWAIT. | |
3564 | */ | |
3565 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3566 | ret2 = -EAGAIN; | |
355afaeb JA |
3567 | /* no retry on NONBLOCK marked file */ |
3568 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) | |
3569 | goto done; | |
fa15bafb | 3570 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3571 | /* IOPOLL retry should happen for io-wq threads */ |
3572 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3573 | goto copy_iov; | |
355afaeb | 3574 | done: |
fa15bafb PB |
3575 | kiocb_done(kiocb, ret2, cs); |
3576 | } else { | |
f67676d1 | 3577 | copy_iov: |
84216315 JA |
3578 | /* some cases will consume bytes even on error returns */ |
3579 | iov_iter_revert(iter, iov_count - iov_iter_count(iter)); | |
227c0c96 | 3580 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
ff6165b2 JA |
3581 | if (!ret) |
3582 | return -EAGAIN; | |
2b188cc1 | 3583 | } |
31b51510 | 3584 | out_free: |
f261c168 | 3585 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3586 | if (iovec) |
6f2cc166 | 3587 | kfree(iovec); |
2b188cc1 JA |
3588 | return ret; |
3589 | } | |
3590 | ||
f2a8d5c7 PB |
3591 | static int __io_splice_prep(struct io_kiocb *req, |
3592 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3593 | { |
3594 | struct io_splice* sp = &req->splice; | |
3595 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3596 | |
3232dd02 PB |
3597 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3598 | return -EINVAL; | |
7d67af2c PB |
3599 | |
3600 | sp->file_in = NULL; | |
7d67af2c PB |
3601 | sp->len = READ_ONCE(sqe->len); |
3602 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3603 | ||
3604 | if (unlikely(sp->flags & ~valid_flags)) | |
3605 | return -EINVAL; | |
3606 | ||
8371adf5 PB |
3607 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3608 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3609 | if (!sp->file_in) | |
3610 | return -EBADF; | |
7d67af2c PB |
3611 | req->flags |= REQ_F_NEED_CLEANUP; |
3612 | ||
7cdaf587 XW |
3613 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3614 | /* | |
3615 | * Splice operation will be punted aync, and here need to | |
3616 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3617 | */ | |
3618 | io_req_init_async(req); | |
7d67af2c | 3619 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3620 | } |
7d67af2c PB |
3621 | |
3622 | return 0; | |
3623 | } | |
3624 | ||
f2a8d5c7 PB |
3625 | static int io_tee_prep(struct io_kiocb *req, |
3626 | const struct io_uring_sqe *sqe) | |
3627 | { | |
3628 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3629 | return -EINVAL; | |
3630 | return __io_splice_prep(req, sqe); | |
3631 | } | |
3632 | ||
3633 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3634 | { | |
3635 | struct io_splice *sp = &req->splice; | |
3636 | struct file *in = sp->file_in; | |
3637 | struct file *out = sp->file_out; | |
3638 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3639 | long ret = 0; | |
3640 | ||
3641 | if (force_nonblock) | |
3642 | return -EAGAIN; | |
3643 | if (sp->len) | |
3644 | ret = do_tee(in, out, sp->len, flags); | |
3645 | ||
3646 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3647 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3648 | ||
f2a8d5c7 PB |
3649 | if (ret != sp->len) |
3650 | req_set_fail_links(req); | |
e1e16097 | 3651 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3652 | return 0; |
3653 | } | |
3654 | ||
3655 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3656 | { | |
3657 | struct io_splice* sp = &req->splice; | |
3658 | ||
3659 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3660 | sp->off_out = READ_ONCE(sqe->off); | |
3661 | return __io_splice_prep(req, sqe); | |
3662 | } | |
3663 | ||
014db007 | 3664 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3665 | { |
3666 | struct io_splice *sp = &req->splice; | |
3667 | struct file *in = sp->file_in; | |
3668 | struct file *out = sp->file_out; | |
3669 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3670 | loff_t *poff_in, *poff_out; | |
c9687426 | 3671 | long ret = 0; |
7d67af2c | 3672 | |
2fb3e822 PB |
3673 | if (force_nonblock) |
3674 | return -EAGAIN; | |
7d67af2c PB |
3675 | |
3676 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3677 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3678 | |
948a7749 | 3679 | if (sp->len) |
c9687426 | 3680 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3681 | |
3682 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3683 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3684 | ||
7d67af2c PB |
3685 | if (ret != sp->len) |
3686 | req_set_fail_links(req); | |
e1e16097 | 3687 | io_req_complete(req, ret); |
7d67af2c PB |
3688 | return 0; |
3689 | } | |
3690 | ||
2b188cc1 JA |
3691 | /* |
3692 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3693 | */ | |
229a7b63 | 3694 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3695 | { |
3696 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3697 | |
def596e9 JA |
3698 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3699 | return -EINVAL; | |
3700 | ||
229a7b63 | 3701 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3702 | return 0; |
3703 | } | |
3704 | ||
3529d8c2 | 3705 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3706 | { |
6b06314c | 3707 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3708 | |
09bb8394 JA |
3709 | if (!req->file) |
3710 | return -EBADF; | |
c992fe29 | 3711 | |
6b06314c | 3712 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3713 | return -EINVAL; |
edafccee | 3714 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3715 | return -EINVAL; |
3716 | ||
8ed8d3c3 JA |
3717 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3718 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3719 | return -EINVAL; | |
3720 | ||
3721 | req->sync.off = READ_ONCE(sqe->off); | |
3722 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3723 | return 0; |
3724 | } | |
3725 | ||
ac45abc0 | 3726 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3727 | { |
8ed8d3c3 | 3728 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3729 | int ret; |
3730 | ||
ac45abc0 PB |
3731 | /* fsync always requires a blocking context */ |
3732 | if (force_nonblock) | |
3733 | return -EAGAIN; | |
3734 | ||
9adbd45d | 3735 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3736 | end > 0 ? end : LLONG_MAX, |
3737 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3738 | if (ret < 0) | |
3739 | req_set_fail_links(req); | |
e1e16097 | 3740 | io_req_complete(req, ret); |
c992fe29 CH |
3741 | return 0; |
3742 | } | |
3743 | ||
d63d1b5e JA |
3744 | static int io_fallocate_prep(struct io_kiocb *req, |
3745 | const struct io_uring_sqe *sqe) | |
3746 | { | |
3747 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3748 | return -EINVAL; | |
3232dd02 PB |
3749 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3750 | return -EINVAL; | |
d63d1b5e JA |
3751 | |
3752 | req->sync.off = READ_ONCE(sqe->off); | |
3753 | req->sync.len = READ_ONCE(sqe->addr); | |
3754 | req->sync.mode = READ_ONCE(sqe->len); | |
3755 | return 0; | |
3756 | } | |
3757 | ||
014db007 | 3758 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 3759 | { |
ac45abc0 PB |
3760 | int ret; |
3761 | ||
d63d1b5e | 3762 | /* fallocate always requiring blocking context */ |
ac45abc0 | 3763 | if (force_nonblock) |
5d17b4a4 | 3764 | return -EAGAIN; |
ac45abc0 PB |
3765 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3766 | req->sync.len); | |
ac45abc0 PB |
3767 | if (ret < 0) |
3768 | req_set_fail_links(req); | |
e1e16097 | 3769 | io_req_complete(req, ret); |
5d17b4a4 JA |
3770 | return 0; |
3771 | } | |
3772 | ||
ec65fea5 | 3773 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3774 | { |
f8748881 | 3775 | const char __user *fname; |
15b71abe | 3776 | int ret; |
b7bb4f7d | 3777 | |
ec65fea5 | 3778 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3779 | return -EINVAL; |
ec65fea5 | 3780 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3781 | return -EBADF; |
03b1230c | 3782 | |
ec65fea5 PB |
3783 | /* open.how should be already initialised */ |
3784 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3785 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3786 | |
25e72d10 PB |
3787 | req->open.dfd = READ_ONCE(sqe->fd); |
3788 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3789 | req->open.filename = getname(fname); |
15b71abe JA |
3790 | if (IS_ERR(req->open.filename)) { |
3791 | ret = PTR_ERR(req->open.filename); | |
3792 | req->open.filename = NULL; | |
3793 | return ret; | |
3794 | } | |
4022e7af | 3795 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
944d1444 | 3796 | req->open.ignore_nonblock = false; |
8fef80bf | 3797 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3798 | return 0; |
03b1230c JA |
3799 | } |
3800 | ||
ec65fea5 PB |
3801 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3802 | { | |
3803 | u64 flags, mode; | |
3804 | ||
4eb8dded JA |
3805 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3806 | return -EINVAL; | |
ec65fea5 PB |
3807 | mode = READ_ONCE(sqe->len); |
3808 | flags = READ_ONCE(sqe->open_flags); | |
3809 | req->open.how = build_open_how(flags, mode); | |
3810 | return __io_openat_prep(req, sqe); | |
3811 | } | |
3812 | ||
cebdb986 | 3813 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3814 | { |
cebdb986 | 3815 | struct open_how __user *how; |
cebdb986 | 3816 | size_t len; |
0fa03c62 JA |
3817 | int ret; |
3818 | ||
4eb8dded JA |
3819 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3820 | return -EINVAL; | |
cebdb986 JA |
3821 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3822 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3823 | if (len < OPEN_HOW_SIZE_VER0) |
3824 | return -EINVAL; | |
3529d8c2 | 3825 | |
cebdb986 JA |
3826 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3827 | len); | |
3828 | if (ret) | |
3829 | return ret; | |
3529d8c2 | 3830 | |
ec65fea5 | 3831 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3832 | } |
3833 | ||
014db007 | 3834 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
3835 | { |
3836 | struct open_flags op; | |
15b71abe JA |
3837 | struct file *file; |
3838 | int ret; | |
3839 | ||
944d1444 | 3840 | if (force_nonblock && !req->open.ignore_nonblock) |
15b71abe | 3841 | return -EAGAIN; |
15b71abe | 3842 | |
cebdb986 | 3843 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3844 | if (ret) |
3845 | goto err; | |
3846 | ||
4022e7af | 3847 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3848 | if (ret < 0) |
3849 | goto err; | |
3850 | ||
3851 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3852 | if (IS_ERR(file)) { | |
3853 | put_unused_fd(ret); | |
3854 | ret = PTR_ERR(file); | |
944d1444 JA |
3855 | /* |
3856 | * A work-around to ensure that /proc/self works that way | |
3857 | * that it should - if we get -EOPNOTSUPP back, then assume | |
3858 | * that proc_self_get_link() failed us because we're in async | |
3859 | * context. We should be safe to retry this from the task | |
3860 | * itself with force_nonblock == false set, as it should not | |
3861 | * block on lookup. Would be nice to know this upfront and | |
3862 | * avoid the async dance, but doesn't seem feasible. | |
3863 | */ | |
3864 | if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) { | |
3865 | req->open.ignore_nonblock = true; | |
3866 | refcount_inc(&req->refs); | |
3867 | io_req_task_queue(req); | |
3868 | return 0; | |
3869 | } | |
15b71abe JA |
3870 | } else { |
3871 | fsnotify_open(file); | |
3872 | fd_install(ret, file); | |
3873 | } | |
3874 | err: | |
3875 | putname(req->open.filename); | |
8fef80bf | 3876 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3877 | if (ret < 0) |
3878 | req_set_fail_links(req); | |
e1e16097 | 3879 | io_req_complete(req, ret); |
15b71abe JA |
3880 | return 0; |
3881 | } | |
3882 | ||
014db007 | 3883 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 3884 | { |
014db007 | 3885 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
3886 | } |
3887 | ||
067524e9 JA |
3888 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3889 | const struct io_uring_sqe *sqe) | |
3890 | { | |
3891 | struct io_provide_buf *p = &req->pbuf; | |
3892 | u64 tmp; | |
3893 | ||
3894 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3895 | return -EINVAL; | |
3896 | ||
3897 | tmp = READ_ONCE(sqe->fd); | |
3898 | if (!tmp || tmp > USHRT_MAX) | |
3899 | return -EINVAL; | |
3900 | ||
3901 | memset(p, 0, sizeof(*p)); | |
3902 | p->nbufs = tmp; | |
3903 | p->bgid = READ_ONCE(sqe->buf_group); | |
3904 | return 0; | |
3905 | } | |
3906 | ||
3907 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3908 | int bgid, unsigned nbufs) | |
3909 | { | |
3910 | unsigned i = 0; | |
3911 | ||
3912 | /* shouldn't happen */ | |
3913 | if (!nbufs) | |
3914 | return 0; | |
3915 | ||
3916 | /* the head kbuf is the list itself */ | |
3917 | while (!list_empty(&buf->list)) { | |
3918 | struct io_buffer *nxt; | |
3919 | ||
3920 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3921 | list_del(&nxt->list); | |
3922 | kfree(nxt); | |
3923 | if (++i == nbufs) | |
3924 | return i; | |
3925 | } | |
3926 | i++; | |
3927 | kfree(buf); | |
3928 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3929 | ||
3930 | return i; | |
3931 | } | |
3932 | ||
229a7b63 JA |
3933 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
3934 | struct io_comp_state *cs) | |
067524e9 JA |
3935 | { |
3936 | struct io_provide_buf *p = &req->pbuf; | |
3937 | struct io_ring_ctx *ctx = req->ctx; | |
3938 | struct io_buffer *head; | |
3939 | int ret = 0; | |
3940 | ||
3941 | io_ring_submit_lock(ctx, !force_nonblock); | |
3942 | ||
3943 | lockdep_assert_held(&ctx->uring_lock); | |
3944 | ||
3945 | ret = -ENOENT; | |
3946 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3947 | if (head) | |
3948 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3949 | if (ret < 0) |
3950 | req_set_fail_links(req); | |
d33c7590 PB |
3951 | |
3952 | /* need to hold the lock to complete IOPOLL requests */ | |
3953 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
3954 | __io_req_complete(req, ret, 0, cs); | |
3955 | io_ring_submit_unlock(ctx, !force_nonblock); | |
3956 | } else { | |
3957 | io_ring_submit_unlock(ctx, !force_nonblock); | |
3958 | __io_req_complete(req, ret, 0, cs); | |
3959 | } | |
067524e9 JA |
3960 | return 0; |
3961 | } | |
3962 | ||
ddf0322d JA |
3963 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3964 | const struct io_uring_sqe *sqe) | |
3965 | { | |
3966 | struct io_provide_buf *p = &req->pbuf; | |
3967 | u64 tmp; | |
3968 | ||
3969 | if (sqe->ioprio || sqe->rw_flags) | |
3970 | return -EINVAL; | |
3971 | ||
3972 | tmp = READ_ONCE(sqe->fd); | |
3973 | if (!tmp || tmp > USHRT_MAX) | |
3974 | return -E2BIG; | |
3975 | p->nbufs = tmp; | |
3976 | p->addr = READ_ONCE(sqe->addr); | |
3977 | p->len = READ_ONCE(sqe->len); | |
3978 | ||
efe68c1c | 3979 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3980 | return -EFAULT; |
3981 | ||
3982 | p->bgid = READ_ONCE(sqe->buf_group); | |
3983 | tmp = READ_ONCE(sqe->off); | |
3984 | if (tmp > USHRT_MAX) | |
3985 | return -E2BIG; | |
3986 | p->bid = tmp; | |
3987 | return 0; | |
3988 | } | |
3989 | ||
3990 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3991 | { | |
3992 | struct io_buffer *buf; | |
3993 | u64 addr = pbuf->addr; | |
3994 | int i, bid = pbuf->bid; | |
3995 | ||
3996 | for (i = 0; i < pbuf->nbufs; i++) { | |
3997 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3998 | if (!buf) | |
3999 | break; | |
4000 | ||
4001 | buf->addr = addr; | |
4002 | buf->len = pbuf->len; | |
4003 | buf->bid = bid; | |
4004 | addr += pbuf->len; | |
4005 | bid++; | |
4006 | if (!*head) { | |
4007 | INIT_LIST_HEAD(&buf->list); | |
4008 | *head = buf; | |
4009 | } else { | |
4010 | list_add_tail(&buf->list, &(*head)->list); | |
4011 | } | |
4012 | } | |
4013 | ||
4014 | return i ? i : -ENOMEM; | |
4015 | } | |
4016 | ||
229a7b63 JA |
4017 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
4018 | struct io_comp_state *cs) | |
ddf0322d JA |
4019 | { |
4020 | struct io_provide_buf *p = &req->pbuf; | |
4021 | struct io_ring_ctx *ctx = req->ctx; | |
4022 | struct io_buffer *head, *list; | |
4023 | int ret = 0; | |
4024 | ||
4025 | io_ring_submit_lock(ctx, !force_nonblock); | |
4026 | ||
4027 | lockdep_assert_held(&ctx->uring_lock); | |
4028 | ||
4029 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4030 | ||
4031 | ret = io_add_buffers(p, &head); | |
4032 | if (ret < 0) | |
4033 | goto out; | |
4034 | ||
4035 | if (!list) { | |
4036 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4037 | GFP_KERNEL); | |
4038 | if (ret < 0) { | |
067524e9 | 4039 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4040 | goto out; |
4041 | } | |
4042 | } | |
4043 | out: | |
ddf0322d JA |
4044 | if (ret < 0) |
4045 | req_set_fail_links(req); | |
d33c7590 PB |
4046 | |
4047 | /* need to hold the lock to complete IOPOLL requests */ | |
4048 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4049 | __io_req_complete(req, ret, 0, cs); | |
4050 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4051 | } else { | |
4052 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4053 | __io_req_complete(req, ret, 0, cs); | |
4054 | } | |
ddf0322d | 4055 | return 0; |
cebdb986 JA |
4056 | } |
4057 | ||
3e4827b0 JA |
4058 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4059 | const struct io_uring_sqe *sqe) | |
4060 | { | |
4061 | #if defined(CONFIG_EPOLL) | |
4062 | if (sqe->ioprio || sqe->buf_index) | |
4063 | return -EINVAL; | |
6ca56f84 | 4064 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4065 | return -EINVAL; |
3e4827b0 JA |
4066 | |
4067 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4068 | req->epoll.op = READ_ONCE(sqe->len); | |
4069 | req->epoll.fd = READ_ONCE(sqe->off); | |
4070 | ||
4071 | if (ep_op_has_event(req->epoll.op)) { | |
4072 | struct epoll_event __user *ev; | |
4073 | ||
4074 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4075 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4076 | return -EFAULT; | |
4077 | } | |
4078 | ||
4079 | return 0; | |
4080 | #else | |
4081 | return -EOPNOTSUPP; | |
4082 | #endif | |
4083 | } | |
4084 | ||
229a7b63 JA |
4085 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
4086 | struct io_comp_state *cs) | |
3e4827b0 JA |
4087 | { |
4088 | #if defined(CONFIG_EPOLL) | |
4089 | struct io_epoll *ie = &req->epoll; | |
4090 | int ret; | |
4091 | ||
4092 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4093 | if (force_nonblock && ret == -EAGAIN) | |
4094 | return -EAGAIN; | |
4095 | ||
4096 | if (ret < 0) | |
4097 | req_set_fail_links(req); | |
229a7b63 | 4098 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
4099 | return 0; |
4100 | #else | |
4101 | return -EOPNOTSUPP; | |
4102 | #endif | |
4103 | } | |
4104 | ||
c1ca757b JA |
4105 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4106 | { | |
4107 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4108 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4109 | return -EINVAL; | |
3232dd02 PB |
4110 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4111 | return -EINVAL; | |
c1ca757b JA |
4112 | |
4113 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4114 | req->madvise.len = READ_ONCE(sqe->len); | |
4115 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4116 | return 0; | |
4117 | #else | |
4118 | return -EOPNOTSUPP; | |
4119 | #endif | |
4120 | } | |
4121 | ||
014db007 | 4122 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
4123 | { |
4124 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4125 | struct io_madvise *ma = &req->madvise; | |
4126 | int ret; | |
4127 | ||
4128 | if (force_nonblock) | |
4129 | return -EAGAIN; | |
4130 | ||
0726b01e | 4131 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4132 | if (ret < 0) |
4133 | req_set_fail_links(req); | |
e1e16097 | 4134 | io_req_complete(req, ret); |
c1ca757b JA |
4135 | return 0; |
4136 | #else | |
4137 | return -EOPNOTSUPP; | |
4138 | #endif | |
4139 | } | |
4140 | ||
4840e418 JA |
4141 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4142 | { | |
4143 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4144 | return -EINVAL; | |
3232dd02 PB |
4145 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4146 | return -EINVAL; | |
4840e418 JA |
4147 | |
4148 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4149 | req->fadvise.len = READ_ONCE(sqe->len); | |
4150 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4151 | return 0; | |
4152 | } | |
4153 | ||
014db007 | 4154 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
4155 | { |
4156 | struct io_fadvise *fa = &req->fadvise; | |
4157 | int ret; | |
4158 | ||
3e69426d JA |
4159 | if (force_nonblock) { |
4160 | switch (fa->advice) { | |
4161 | case POSIX_FADV_NORMAL: | |
4162 | case POSIX_FADV_RANDOM: | |
4163 | case POSIX_FADV_SEQUENTIAL: | |
4164 | break; | |
4165 | default: | |
4166 | return -EAGAIN; | |
4167 | } | |
4168 | } | |
4840e418 JA |
4169 | |
4170 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4171 | if (ret < 0) | |
4172 | req_set_fail_links(req); | |
e1e16097 | 4173 | io_req_complete(req, ret); |
4840e418 JA |
4174 | return 0; |
4175 | } | |
4176 | ||
eddc7ef5 JA |
4177 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4178 | { | |
6ca56f84 | 4179 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4180 | return -EINVAL; |
eddc7ef5 JA |
4181 | if (sqe->ioprio || sqe->buf_index) |
4182 | return -EINVAL; | |
9c280f90 | 4183 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4184 | return -EBADF; |
eddc7ef5 | 4185 | |
1d9e1288 BM |
4186 | req->statx.dfd = READ_ONCE(sqe->fd); |
4187 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4188 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4189 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4190 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4191 | |
4192 | return 0; | |
4193 | } | |
4194 | ||
014db007 | 4195 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 4196 | { |
1d9e1288 | 4197 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4198 | int ret; |
4199 | ||
5b0bbee4 JA |
4200 | if (force_nonblock) { |
4201 | /* only need file table for an actual valid fd */ | |
4202 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4203 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4204 | return -EAGAIN; |
5b0bbee4 | 4205 | } |
eddc7ef5 | 4206 | |
e62753e4 BM |
4207 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4208 | ctx->buffer); | |
eddc7ef5 | 4209 | |
eddc7ef5 JA |
4210 | if (ret < 0) |
4211 | req_set_fail_links(req); | |
e1e16097 | 4212 | io_req_complete(req, ret); |
eddc7ef5 JA |
4213 | return 0; |
4214 | } | |
4215 | ||
b5dba59e JA |
4216 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4217 | { | |
4218 | /* | |
4219 | * If we queue this for async, it must not be cancellable. That would | |
7cdaf587 XW |
4220 | * leave the 'file' in an undeterminate state, and here need to modify |
4221 | * io_wq_work.flags, so initialize io_wq_work firstly. | |
b5dba59e | 4222 | */ |
7cdaf587 | 4223 | io_req_init_async(req); |
b5dba59e JA |
4224 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
4225 | ||
3232dd02 PB |
4226 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4227 | return -EINVAL; | |
b5dba59e JA |
4228 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4229 | sqe->rw_flags || sqe->buf_index) | |
4230 | return -EINVAL; | |
9c280f90 | 4231 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4232 | return -EBADF; |
b5dba59e JA |
4233 | |
4234 | req->close.fd = READ_ONCE(sqe->fd); | |
0f212204 | 4235 | if ((req->file && req->file->f_op == &io_uring_fops)) |
fd2206e4 | 4236 | return -EBADF; |
b5dba59e | 4237 | |
3af73b28 | 4238 | req->close.put_file = NULL; |
b5dba59e | 4239 | return 0; |
b5dba59e JA |
4240 | } |
4241 | ||
229a7b63 JA |
4242 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
4243 | struct io_comp_state *cs) | |
b5dba59e | 4244 | { |
3af73b28 | 4245 | struct io_close *close = &req->close; |
b5dba59e JA |
4246 | int ret; |
4247 | ||
3af73b28 PB |
4248 | /* might be already done during nonblock submission */ |
4249 | if (!close->put_file) { | |
4250 | ret = __close_fd_get_file(close->fd, &close->put_file); | |
4251 | if (ret < 0) | |
4252 | return (ret == -ENOENT) ? -EBADF : ret; | |
4253 | } | |
b5dba59e JA |
4254 | |
4255 | /* if the file has a flush method, be safe and punt to async */ | |
3af73b28 | 4256 | if (close->put_file->f_op->flush && force_nonblock) { |
24c74678 PB |
4257 | /* was never set, but play safe */ |
4258 | req->flags &= ~REQ_F_NOWAIT; | |
0bf0eefd | 4259 | /* avoid grabbing files - we don't need the files */ |
24c74678 | 4260 | req->flags |= REQ_F_NO_FILE_TABLE; |
0bf0eefd | 4261 | return -EAGAIN; |
a2100672 | 4262 | } |
b5dba59e | 4263 | |
3af73b28 | 4264 | /* No ->flush() or already async, safely close from here */ |
98447d65 | 4265 | ret = filp_close(close->put_file, req->work.identity->files); |
3af73b28 PB |
4266 | if (ret < 0) |
4267 | req_set_fail_links(req); | |
3af73b28 PB |
4268 | fput(close->put_file); |
4269 | close->put_file = NULL; | |
229a7b63 | 4270 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 4271 | return 0; |
b5dba59e JA |
4272 | } |
4273 | ||
3529d8c2 | 4274 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4275 | { |
4276 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4277 | |
4278 | if (!req->file) | |
4279 | return -EBADF; | |
5d17b4a4 JA |
4280 | |
4281 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4282 | return -EINVAL; | |
4283 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4284 | return -EINVAL; | |
4285 | ||
8ed8d3c3 JA |
4286 | req->sync.off = READ_ONCE(sqe->off); |
4287 | req->sync.len = READ_ONCE(sqe->len); | |
4288 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4289 | return 0; |
4290 | } | |
4291 | ||
ac45abc0 | 4292 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4293 | { |
8ed8d3c3 JA |
4294 | int ret; |
4295 | ||
ac45abc0 PB |
4296 | /* sync_file_range always requires a blocking context */ |
4297 | if (force_nonblock) | |
4298 | return -EAGAIN; | |
4299 | ||
9adbd45d | 4300 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4301 | req->sync.flags); |
4302 | if (ret < 0) | |
4303 | req_set_fail_links(req); | |
e1e16097 | 4304 | io_req_complete(req, ret); |
5d17b4a4 JA |
4305 | return 0; |
4306 | } | |
4307 | ||
469956e8 | 4308 | #if defined(CONFIG_NET) |
02d27d89 PB |
4309 | static int io_setup_async_msg(struct io_kiocb *req, |
4310 | struct io_async_msghdr *kmsg) | |
4311 | { | |
e8c2bc1f JA |
4312 | struct io_async_msghdr *async_msg = req->async_data; |
4313 | ||
4314 | if (async_msg) | |
02d27d89 | 4315 | return -EAGAIN; |
e8c2bc1f | 4316 | if (io_alloc_async_data(req)) { |
02d27d89 PB |
4317 | if (kmsg->iov != kmsg->fast_iov) |
4318 | kfree(kmsg->iov); | |
4319 | return -ENOMEM; | |
4320 | } | |
e8c2bc1f | 4321 | async_msg = req->async_data; |
02d27d89 | 4322 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4323 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
02d27d89 PB |
4324 | return -EAGAIN; |
4325 | } | |
4326 | ||
2ae523ed PB |
4327 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4328 | struct io_async_msghdr *iomsg) | |
4329 | { | |
4330 | iomsg->iov = iomsg->fast_iov; | |
4331 | iomsg->msg.msg_name = &iomsg->addr; | |
4332 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4333 | req->sr_msg.msg_flags, &iomsg->iov); | |
4334 | } | |
4335 | ||
3529d8c2 | 4336 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4337 | { |
e8c2bc1f | 4338 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4339 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4340 | int ret; |
03b1230c | 4341 | |
d2b6f48b PB |
4342 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4343 | return -EINVAL; | |
4344 | ||
e47293fd | 4345 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4346 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4347 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4348 | |
d8768362 JA |
4349 | #ifdef CONFIG_COMPAT |
4350 | if (req->ctx->compat) | |
4351 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4352 | #endif | |
4353 | ||
e8c2bc1f | 4354 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4355 | return 0; |
e8c2bc1f | 4356 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4357 | if (!ret) |
4358 | req->flags |= REQ_F_NEED_CLEANUP; | |
4359 | return ret; | |
03b1230c JA |
4360 | } |
4361 | ||
229a7b63 JA |
4362 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4363 | struct io_comp_state *cs) | |
aa1fa28f | 4364 | { |
6b754c8b | 4365 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4366 | struct socket *sock; |
7a7cacba | 4367 | unsigned flags; |
0fa03c62 JA |
4368 | int ret; |
4369 | ||
0fa03c62 | 4370 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4371 | if (unlikely(!sock)) |
4372 | return ret; | |
3529d8c2 | 4373 | |
e8c2bc1f JA |
4374 | if (req->async_data) { |
4375 | kmsg = req->async_data; | |
4376 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4377 | /* if iov is set, it's allocated already */ |
4378 | if (!kmsg->iov) | |
4379 | kmsg->iov = kmsg->fast_iov; | |
4380 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4381 | } else { | |
4382 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4383 | if (ret) | |
4384 | return ret; | |
4385 | kmsg = &iomsg; | |
0fa03c62 | 4386 | } |
0fa03c62 | 4387 | |
7a7cacba PB |
4388 | flags = req->sr_msg.msg_flags; |
4389 | if (flags & MSG_DONTWAIT) | |
4390 | req->flags |= REQ_F_NOWAIT; | |
4391 | else if (force_nonblock) | |
4392 | flags |= MSG_DONTWAIT; | |
e47293fd | 4393 | |
7a7cacba PB |
4394 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4395 | if (force_nonblock && ret == -EAGAIN) | |
4396 | return io_setup_async_msg(req, kmsg); | |
4397 | if (ret == -ERESTARTSYS) | |
4398 | ret = -EINTR; | |
0fa03c62 | 4399 | |
6b754c8b | 4400 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4401 | kfree(kmsg->iov); |
99bc4c38 | 4402 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4403 | if (ret < 0) |
4404 | req_set_fail_links(req); | |
229a7b63 | 4405 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4406 | return 0; |
03b1230c | 4407 | } |
aa1fa28f | 4408 | |
229a7b63 JA |
4409 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4410 | struct io_comp_state *cs) | |
fddaface | 4411 | { |
7a7cacba PB |
4412 | struct io_sr_msg *sr = &req->sr_msg; |
4413 | struct msghdr msg; | |
4414 | struct iovec iov; | |
fddaface | 4415 | struct socket *sock; |
7a7cacba | 4416 | unsigned flags; |
fddaface JA |
4417 | int ret; |
4418 | ||
fddaface | 4419 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4420 | if (unlikely(!sock)) |
4421 | return ret; | |
fddaface | 4422 | |
7a7cacba PB |
4423 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4424 | if (unlikely(ret)) | |
14db8411 | 4425 | return ret; |
fddaface | 4426 | |
7a7cacba PB |
4427 | msg.msg_name = NULL; |
4428 | msg.msg_control = NULL; | |
4429 | msg.msg_controllen = 0; | |
4430 | msg.msg_namelen = 0; | |
fddaface | 4431 | |
7a7cacba PB |
4432 | flags = req->sr_msg.msg_flags; |
4433 | if (flags & MSG_DONTWAIT) | |
4434 | req->flags |= REQ_F_NOWAIT; | |
4435 | else if (force_nonblock) | |
4436 | flags |= MSG_DONTWAIT; | |
fddaface | 4437 | |
7a7cacba PB |
4438 | msg.msg_flags = flags; |
4439 | ret = sock_sendmsg(sock, &msg); | |
4440 | if (force_nonblock && ret == -EAGAIN) | |
4441 | return -EAGAIN; | |
4442 | if (ret == -ERESTARTSYS) | |
4443 | ret = -EINTR; | |
fddaface | 4444 | |
fddaface JA |
4445 | if (ret < 0) |
4446 | req_set_fail_links(req); | |
229a7b63 | 4447 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4448 | return 0; |
fddaface JA |
4449 | } |
4450 | ||
1400e697 PB |
4451 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4452 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4453 | { |
4454 | struct io_sr_msg *sr = &req->sr_msg; | |
4455 | struct iovec __user *uiov; | |
4456 | size_t iov_len; | |
4457 | int ret; | |
4458 | ||
1400e697 PB |
4459 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4460 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4461 | if (ret) |
4462 | return ret; | |
4463 | ||
4464 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4465 | if (iov_len > 1) | |
4466 | return -EINVAL; | |
1400e697 | 4467 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4468 | return -EFAULT; |
1400e697 PB |
4469 | sr->len = iomsg->iov[0].iov_len; |
4470 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4471 | sr->len); |
1400e697 | 4472 | iomsg->iov = NULL; |
52de1fe1 | 4473 | } else { |
89cd35c5 CH |
4474 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
4475 | &iomsg->iov, &iomsg->msg.msg_iter, | |
4476 | false); | |
52de1fe1 JA |
4477 | if (ret > 0) |
4478 | ret = 0; | |
4479 | } | |
4480 | ||
4481 | return ret; | |
4482 | } | |
4483 | ||
4484 | #ifdef CONFIG_COMPAT | |
4485 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4486 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4487 | { |
4488 | struct compat_msghdr __user *msg_compat; | |
4489 | struct io_sr_msg *sr = &req->sr_msg; | |
4490 | struct compat_iovec __user *uiov; | |
4491 | compat_uptr_t ptr; | |
4492 | compat_size_t len; | |
4493 | int ret; | |
4494 | ||
270a5940 | 4495 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4496 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4497 | &ptr, &len); |
4498 | if (ret) | |
4499 | return ret; | |
4500 | ||
4501 | uiov = compat_ptr(ptr); | |
4502 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4503 | compat_ssize_t clen; | |
4504 | ||
4505 | if (len > 1) | |
4506 | return -EINVAL; | |
4507 | if (!access_ok(uiov, sizeof(*uiov))) | |
4508 | return -EFAULT; | |
4509 | if (__get_user(clen, &uiov->iov_len)) | |
4510 | return -EFAULT; | |
4511 | if (clen < 0) | |
4512 | return -EINVAL; | |
2d280bc8 PB |
4513 | sr->len = clen; |
4514 | iomsg->iov[0].iov_len = clen; | |
1400e697 | 4515 | iomsg->iov = NULL; |
52de1fe1 | 4516 | } else { |
89cd35c5 CH |
4517 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
4518 | UIO_FASTIOV, &iomsg->iov, | |
4519 | &iomsg->msg.msg_iter, true); | |
52de1fe1 JA |
4520 | if (ret < 0) |
4521 | return ret; | |
4522 | } | |
4523 | ||
4524 | return 0; | |
4525 | } | |
4526 | #endif | |
4527 | ||
1400e697 PB |
4528 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4529 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4530 | { |
1400e697 PB |
4531 | iomsg->msg.msg_name = &iomsg->addr; |
4532 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4533 | |
4534 | #ifdef CONFIG_COMPAT | |
4535 | if (req->ctx->compat) | |
1400e697 | 4536 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4537 | #endif |
52de1fe1 | 4538 | |
1400e697 | 4539 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4540 | } |
4541 | ||
bcda7baa | 4542 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4543 | bool needs_lock) |
bcda7baa JA |
4544 | { |
4545 | struct io_sr_msg *sr = &req->sr_msg; | |
4546 | struct io_buffer *kbuf; | |
4547 | ||
bcda7baa JA |
4548 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4549 | if (IS_ERR(kbuf)) | |
4550 | return kbuf; | |
4551 | ||
4552 | sr->kbuf = kbuf; | |
4553 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4554 | return kbuf; |
fddaface JA |
4555 | } |
4556 | ||
7fbb1b54 PB |
4557 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4558 | { | |
4559 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4560 | } | |
4561 | ||
3529d8c2 JA |
4562 | static int io_recvmsg_prep(struct io_kiocb *req, |
4563 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4564 | { |
e8c2bc1f | 4565 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4566 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4567 | int ret; |
3529d8c2 | 4568 | |
d2b6f48b PB |
4569 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4570 | return -EINVAL; | |
4571 | ||
3529d8c2 | 4572 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4573 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4574 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4575 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4576 | |
d8768362 JA |
4577 | #ifdef CONFIG_COMPAT |
4578 | if (req->ctx->compat) | |
4579 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4580 | #endif | |
4581 | ||
e8c2bc1f | 4582 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
5f798bea | 4583 | return 0; |
e8c2bc1f | 4584 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4585 | if (!ret) |
4586 | req->flags |= REQ_F_NEED_CLEANUP; | |
4587 | return ret; | |
aa1fa28f JA |
4588 | } |
4589 | ||
229a7b63 JA |
4590 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4591 | struct io_comp_state *cs) | |
aa1fa28f | 4592 | { |
6b754c8b | 4593 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4594 | struct socket *sock; |
7fbb1b54 | 4595 | struct io_buffer *kbuf; |
7a7cacba | 4596 | unsigned flags; |
52de1fe1 | 4597 | int ret, cflags = 0; |
03b1230c | 4598 | |
03b1230c | 4599 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4600 | if (unlikely(!sock)) |
4601 | return ret; | |
3529d8c2 | 4602 | |
e8c2bc1f JA |
4603 | if (req->async_data) { |
4604 | kmsg = req->async_data; | |
4605 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4606 | /* if iov is set, it's allocated already */ |
4607 | if (!kmsg->iov) | |
4608 | kmsg->iov = kmsg->fast_iov; | |
4609 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4610 | } else { | |
4611 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4612 | if (ret) | |
681fda8d | 4613 | return ret; |
7a7cacba PB |
4614 | kmsg = &iomsg; |
4615 | } | |
03b1230c | 4616 | |
bc02ef33 | 4617 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4618 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4619 | if (IS_ERR(kbuf)) |
52de1fe1 | 4620 | return PTR_ERR(kbuf); |
7a7cacba PB |
4621 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4622 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4623 | 1, req->sr_msg.len); | |
4624 | } | |
52de1fe1 | 4625 | |
7a7cacba PB |
4626 | flags = req->sr_msg.msg_flags; |
4627 | if (flags & MSG_DONTWAIT) | |
4628 | req->flags |= REQ_F_NOWAIT; | |
4629 | else if (force_nonblock) | |
4630 | flags |= MSG_DONTWAIT; | |
e47293fd | 4631 | |
7a7cacba PB |
4632 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4633 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4634 | if (force_nonblock && ret == -EAGAIN) |
4635 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4636 | if (ret == -ERESTARTSYS) |
4637 | ret = -EINTR; | |
03b1230c | 4638 | |
7fbb1b54 PB |
4639 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4640 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4641 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4642 | kfree(kmsg->iov); |
99bc4c38 | 4643 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4644 | if (ret < 0) |
4645 | req_set_fail_links(req); | |
229a7b63 | 4646 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4647 | return 0; |
0fa03c62 | 4648 | } |
5d17b4a4 | 4649 | |
229a7b63 JA |
4650 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4651 | struct io_comp_state *cs) | |
fddaface | 4652 | { |
6b754c8b | 4653 | struct io_buffer *kbuf; |
7a7cacba PB |
4654 | struct io_sr_msg *sr = &req->sr_msg; |
4655 | struct msghdr msg; | |
4656 | void __user *buf = sr->buf; | |
fddaface | 4657 | struct socket *sock; |
7a7cacba PB |
4658 | struct iovec iov; |
4659 | unsigned flags; | |
bcda7baa | 4660 | int ret, cflags = 0; |
fddaface | 4661 | |
fddaface | 4662 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4663 | if (unlikely(!sock)) |
4664 | return ret; | |
fddaface | 4665 | |
bc02ef33 | 4666 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4667 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4668 | if (IS_ERR(kbuf)) |
4669 | return PTR_ERR(kbuf); | |
7a7cacba | 4670 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4671 | } |
bcda7baa | 4672 | |
7a7cacba | 4673 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4674 | if (unlikely(ret)) |
4675 | goto out_free; | |
fddaface | 4676 | |
7a7cacba PB |
4677 | msg.msg_name = NULL; |
4678 | msg.msg_control = NULL; | |
4679 | msg.msg_controllen = 0; | |
4680 | msg.msg_namelen = 0; | |
4681 | msg.msg_iocb = NULL; | |
4682 | msg.msg_flags = 0; | |
fddaface | 4683 | |
7a7cacba PB |
4684 | flags = req->sr_msg.msg_flags; |
4685 | if (flags & MSG_DONTWAIT) | |
4686 | req->flags |= REQ_F_NOWAIT; | |
4687 | else if (force_nonblock) | |
4688 | flags |= MSG_DONTWAIT; | |
4689 | ||
4690 | ret = sock_recvmsg(sock, &msg, flags); | |
4691 | if (force_nonblock && ret == -EAGAIN) | |
4692 | return -EAGAIN; | |
4693 | if (ret == -ERESTARTSYS) | |
4694 | ret = -EINTR; | |
14c32eee | 4695 | out_free: |
7fbb1b54 PB |
4696 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4697 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4698 | if (ret < 0) |
4699 | req_set_fail_links(req); | |
229a7b63 | 4700 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4701 | return 0; |
fddaface JA |
4702 | } |
4703 | ||
3529d8c2 | 4704 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4705 | { |
8ed8d3c3 JA |
4706 | struct io_accept *accept = &req->accept; |
4707 | ||
17f2fe35 JA |
4708 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4709 | return -EINVAL; | |
8042d6ce | 4710 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4711 | return -EINVAL; |
4712 | ||
d55e5f5b JA |
4713 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4714 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4715 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4716 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4717 | return 0; |
8ed8d3c3 | 4718 | } |
17f2fe35 | 4719 | |
229a7b63 JA |
4720 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4721 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
4722 | { |
4723 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 4724 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4725 | int ret; |
4726 | ||
e697deed JX |
4727 | if (req->file->f_flags & O_NONBLOCK) |
4728 | req->flags |= REQ_F_NOWAIT; | |
4729 | ||
8ed8d3c3 | 4730 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4731 | accept->addr_len, accept->flags, |
4732 | accept->nofile); | |
8ed8d3c3 | 4733 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4734 | return -EAGAIN; |
ac45abc0 PB |
4735 | if (ret < 0) { |
4736 | if (ret == -ERESTARTSYS) | |
4737 | ret = -EINTR; | |
4e88d6e7 | 4738 | req_set_fail_links(req); |
ac45abc0 | 4739 | } |
229a7b63 | 4740 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 4741 | return 0; |
8ed8d3c3 JA |
4742 | } |
4743 | ||
3529d8c2 | 4744 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4745 | { |
3529d8c2 | 4746 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 4747 | struct io_async_connect *io = req->async_data; |
f499a021 | 4748 | |
3fbb51c1 JA |
4749 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4750 | return -EINVAL; | |
4751 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4752 | return -EINVAL; | |
4753 | ||
3529d8c2 JA |
4754 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4755 | conn->addr_len = READ_ONCE(sqe->addr2); | |
4756 | ||
4757 | if (!io) | |
4758 | return 0; | |
4759 | ||
4760 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 4761 | &io->address); |
f499a021 JA |
4762 | } |
4763 | ||
229a7b63 JA |
4764 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4765 | struct io_comp_state *cs) | |
f8e85cf2 | 4766 | { |
e8c2bc1f | 4767 | struct io_async_connect __io, *io; |
f8e85cf2 | 4768 | unsigned file_flags; |
3fbb51c1 | 4769 | int ret; |
f8e85cf2 | 4770 | |
e8c2bc1f JA |
4771 | if (req->async_data) { |
4772 | io = req->async_data; | |
f499a021 | 4773 | } else { |
3529d8c2 JA |
4774 | ret = move_addr_to_kernel(req->connect.addr, |
4775 | req->connect.addr_len, | |
e8c2bc1f | 4776 | &__io.address); |
f499a021 JA |
4777 | if (ret) |
4778 | goto out; | |
4779 | io = &__io; | |
4780 | } | |
4781 | ||
3fbb51c1 JA |
4782 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4783 | ||
e8c2bc1f | 4784 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4785 | req->connect.addr_len, file_flags); |
87f80d62 | 4786 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4787 | if (req->async_data) |
b7bb4f7d | 4788 | return -EAGAIN; |
e8c2bc1f | 4789 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4790 | ret = -ENOMEM; |
4791 | goto out; | |
4792 | } | |
e8c2bc1f JA |
4793 | io = req->async_data; |
4794 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4795 | return -EAGAIN; |
f499a021 | 4796 | } |
f8e85cf2 JA |
4797 | if (ret == -ERESTARTSYS) |
4798 | ret = -EINTR; | |
f499a021 | 4799 | out: |
4e88d6e7 JA |
4800 | if (ret < 0) |
4801 | req_set_fail_links(req); | |
229a7b63 | 4802 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 4803 | return 0; |
469956e8 Y |
4804 | } |
4805 | #else /* !CONFIG_NET */ | |
4806 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4807 | { | |
f8e85cf2 | 4808 | return -EOPNOTSUPP; |
f8e85cf2 JA |
4809 | } |
4810 | ||
1e16c2f9 RD |
4811 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4812 | struct io_comp_state *cs) | |
469956e8 Y |
4813 | { |
4814 | return -EOPNOTSUPP; | |
4815 | } | |
4816 | ||
1e16c2f9 RD |
4817 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4818 | struct io_comp_state *cs) | |
469956e8 Y |
4819 | { |
4820 | return -EOPNOTSUPP; | |
4821 | } | |
4822 | ||
4823 | static int io_recvmsg_prep(struct io_kiocb *req, | |
4824 | const struct io_uring_sqe *sqe) | |
4825 | { | |
4826 | return -EOPNOTSUPP; | |
4827 | } | |
4828 | ||
1e16c2f9 RD |
4829 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4830 | struct io_comp_state *cs) | |
469956e8 Y |
4831 | { |
4832 | return -EOPNOTSUPP; | |
4833 | } | |
4834 | ||
1e16c2f9 RD |
4835 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4836 | struct io_comp_state *cs) | |
469956e8 Y |
4837 | { |
4838 | return -EOPNOTSUPP; | |
4839 | } | |
4840 | ||
4841 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4842 | { | |
4843 | return -EOPNOTSUPP; | |
4844 | } | |
4845 | ||
1e16c2f9 RD |
4846 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4847 | struct io_comp_state *cs) | |
469956e8 Y |
4848 | { |
4849 | return -EOPNOTSUPP; | |
4850 | } | |
ce593a6c | 4851 | |
469956e8 Y |
4852 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4853 | { | |
4854 | return -EOPNOTSUPP; | |
4855 | } | |
4856 | ||
1e16c2f9 RD |
4857 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4858 | struct io_comp_state *cs) | |
469956e8 | 4859 | { |
f8e85cf2 | 4860 | return -EOPNOTSUPP; |
ce593a6c | 4861 | } |
469956e8 | 4862 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4863 | |
d7718a9d JA |
4864 | struct io_poll_table { |
4865 | struct poll_table_struct pt; | |
4866 | struct io_kiocb *req; | |
4867 | int error; | |
4868 | }; | |
ce593a6c | 4869 | |
d7718a9d JA |
4870 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4871 | __poll_t mask, task_work_func_t func) | |
4872 | { | |
fd7d6de2 | 4873 | bool twa_signal_ok; |
aa96bf8a | 4874 | int ret; |
d7718a9d JA |
4875 | |
4876 | /* for instances that support it check for an event match first: */ | |
4877 | if (mask && !(mask & poll->events)) | |
4878 | return 0; | |
4879 | ||
4880 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4881 | ||
4882 | list_del_init(&poll->wait.entry); | |
4883 | ||
d7718a9d JA |
4884 | req->result = mask; |
4885 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
4886 | percpu_ref_get(&req->ctx->refs); |
4887 | ||
fd7d6de2 JA |
4888 | /* |
4889 | * If we using the signalfd wait_queue_head for this wakeup, then | |
4890 | * it's not safe to use TWA_SIGNAL as we could be recursing on the | |
4891 | * tsk->sighand->siglock on doing the wakeup. Should not be needed | |
4892 | * either, as the normal wakeup will suffice. | |
4893 | */ | |
4894 | twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh); | |
4895 | ||
d7718a9d | 4896 | /* |
e3aabf95 JA |
4897 | * If this fails, then the task is exiting. When a task exits, the |
4898 | * work gets canceled, so just cancel this request as well instead | |
4899 | * of executing it. We can't safely execute it anyway, as we may not | |
4900 | * have the needed state needed for it anyway. | |
d7718a9d | 4901 | */ |
87c4311f | 4902 | ret = io_req_task_work_add(req, twa_signal_ok); |
aa96bf8a | 4903 | if (unlikely(ret)) { |
c2c4c83c JA |
4904 | struct task_struct *tsk; |
4905 | ||
e3aabf95 | 4906 | WRITE_ONCE(poll->canceled, true); |
aa96bf8a | 4907 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 4908 | task_work_add(tsk, &req->task_work, TWA_NONE); |
ce593a6c | 4909 | wake_up_process(tsk); |
aa96bf8a | 4910 | } |
d7718a9d JA |
4911 | return 1; |
4912 | } | |
4913 | ||
74ce6ce4 JA |
4914 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4915 | __acquires(&req->ctx->completion_lock) | |
4916 | { | |
4917 | struct io_ring_ctx *ctx = req->ctx; | |
4918 | ||
4919 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4920 | struct poll_table_struct pt = { ._key = poll->events }; | |
4921 | ||
4922 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4923 | } | |
4924 | ||
4925 | spin_lock_irq(&ctx->completion_lock); | |
4926 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4927 | add_wait_queue(poll->head, &poll->wait); | |
4928 | return true; | |
4929 | } | |
4930 | ||
4931 | return false; | |
4932 | } | |
4933 | ||
d4e7cd36 | 4934 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4935 | { |
e8c2bc1f | 4936 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4937 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4938 | return req->async_data; |
d4e7cd36 JA |
4939 | return req->apoll->double_poll; |
4940 | } | |
4941 | ||
4942 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4943 | { | |
4944 | if (req->opcode == IORING_OP_POLL_ADD) | |
4945 | return &req->poll; | |
4946 | return &req->apoll->poll; | |
4947 | } | |
4948 | ||
4949 | static void io_poll_remove_double(struct io_kiocb *req) | |
4950 | { | |
4951 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4952 | |
4953 | lockdep_assert_held(&req->ctx->completion_lock); | |
4954 | ||
4955 | if (poll && poll->head) { | |
4956 | struct wait_queue_head *head = poll->head; | |
4957 | ||
4958 | spin_lock(&head->lock); | |
4959 | list_del_init(&poll->wait.entry); | |
4960 | if (poll->wait.private) | |
4961 | refcount_dec(&req->refs); | |
4962 | poll->head = NULL; | |
4963 | spin_unlock(&head->lock); | |
4964 | } | |
4965 | } | |
4966 | ||
4967 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4968 | { | |
4969 | struct io_ring_ctx *ctx = req->ctx; | |
4970 | ||
d4e7cd36 | 4971 | io_poll_remove_double(req); |
18bceab1 JA |
4972 | req->poll.done = true; |
4973 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4974 | io_commit_cqring(ctx); | |
4975 | } | |
4976 | ||
dd221f46 | 4977 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4978 | { |
dd221f46 | 4979 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4980 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4981 | struct io_kiocb *nxt; |
18bceab1 JA |
4982 | |
4983 | if (io_poll_rewait(req, &req->poll)) { | |
4984 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
4985 | } else { |
4986 | hash_del(&req->hash_node); | |
4987 | io_poll_complete(req, req->result, 0); | |
4988 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 4989 | |
dd221f46 PB |
4990 | nxt = io_put_req_find_next(req); |
4991 | io_cqring_ev_posted(ctx); | |
4992 | if (nxt) | |
4993 | __io_req_task_submit(nxt); | |
4994 | } | |
18bceab1 | 4995 | |
6d816e08 | 4996 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4997 | } |
4998 | ||
4999 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5000 | int sync, void *key) | |
5001 | { | |
5002 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5003 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5004 | __poll_t mask = key_to_poll(key); |
5005 | ||
5006 | /* for instances that support it check for an event match first: */ | |
5007 | if (mask && !(mask & poll->events)) | |
5008 | return 0; | |
5009 | ||
8706e04e JA |
5010 | list_del_init(&wait->entry); |
5011 | ||
807abcb0 | 5012 | if (poll && poll->head) { |
18bceab1 JA |
5013 | bool done; |
5014 | ||
807abcb0 JA |
5015 | spin_lock(&poll->head->lock); |
5016 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5017 | if (!done) |
807abcb0 | 5018 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5019 | /* make sure double remove sees this as being gone */ |
5020 | wait->private = NULL; | |
807abcb0 | 5021 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5022 | if (!done) { |
5023 | /* use wait func handler, so it matches the rq type */ | |
5024 | poll->wait.func(&poll->wait, mode, sync, key); | |
5025 | } | |
18bceab1 JA |
5026 | } |
5027 | refcount_dec(&req->refs); | |
5028 | return 1; | |
5029 | } | |
5030 | ||
5031 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5032 | wait_queue_func_t wake_func) | |
5033 | { | |
5034 | poll->head = NULL; | |
5035 | poll->done = false; | |
5036 | poll->canceled = false; | |
5037 | poll->events = events; | |
5038 | INIT_LIST_HEAD(&poll->wait.entry); | |
5039 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5040 | } | |
5041 | ||
5042 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5043 | struct wait_queue_head *head, |
5044 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5045 | { |
5046 | struct io_kiocb *req = pt->req; | |
5047 | ||
5048 | /* | |
5049 | * If poll->head is already set, it's because the file being polled | |
5050 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5051 | * for write). Setup a separate io_poll_iocb if this happens. | |
5052 | */ | |
5053 | if (unlikely(poll->head)) { | |
58852d4d PB |
5054 | struct io_poll_iocb *poll_one = poll; |
5055 | ||
18bceab1 | 5056 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5057 | if (*poll_ptr) { |
18bceab1 JA |
5058 | pt->error = -EINVAL; |
5059 | return; | |
5060 | } | |
5061 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5062 | if (!poll) { | |
5063 | pt->error = -ENOMEM; | |
5064 | return; | |
5065 | } | |
58852d4d | 5066 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5067 | refcount_inc(&req->refs); |
5068 | poll->wait.private = req; | |
807abcb0 | 5069 | *poll_ptr = poll; |
18bceab1 JA |
5070 | } |
5071 | ||
5072 | pt->error = 0; | |
5073 | poll->head = head; | |
a31eb4a2 JX |
5074 | |
5075 | if (poll->events & EPOLLEXCLUSIVE) | |
5076 | add_wait_queue_exclusive(head, &poll->wait); | |
5077 | else | |
5078 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5079 | } |
5080 | ||
5081 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5082 | struct poll_table_struct *p) | |
5083 | { | |
5084 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5085 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5086 | |
807abcb0 | 5087 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5088 | } |
5089 | ||
d7718a9d JA |
5090 | static void io_async_task_func(struct callback_head *cb) |
5091 | { | |
5092 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5093 | struct async_poll *apoll = req->apoll; | |
5094 | struct io_ring_ctx *ctx = req->ctx; | |
5095 | ||
5096 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5097 | ||
74ce6ce4 | 5098 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5099 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5100 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5101 | return; |
d7718a9d JA |
5102 | } |
5103 | ||
31067255 | 5104 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5105 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5106 | hash_del(&req->hash_node); |
2bae047e | 5107 | |
d4e7cd36 | 5108 | io_poll_remove_double(req); |
74ce6ce4 JA |
5109 | spin_unlock_irq(&ctx->completion_lock); |
5110 | ||
0be0b0e3 PB |
5111 | if (!READ_ONCE(apoll->poll.canceled)) |
5112 | __io_req_task_submit(req); | |
5113 | else | |
5114 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5115 | |
6d816e08 | 5116 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5117 | kfree(apoll->double_poll); |
31067255 | 5118 | kfree(apoll); |
d7718a9d JA |
5119 | } |
5120 | ||
5121 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5122 | void *key) | |
5123 | { | |
5124 | struct io_kiocb *req = wait->private; | |
5125 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5126 | ||
5127 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5128 | key_to_poll(key)); | |
5129 | ||
5130 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5131 | } | |
5132 | ||
5133 | static void io_poll_req_insert(struct io_kiocb *req) | |
5134 | { | |
5135 | struct io_ring_ctx *ctx = req->ctx; | |
5136 | struct hlist_head *list; | |
5137 | ||
5138 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5139 | hlist_add_head(&req->hash_node, list); | |
5140 | } | |
5141 | ||
5142 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5143 | struct io_poll_iocb *poll, | |
5144 | struct io_poll_table *ipt, __poll_t mask, | |
5145 | wait_queue_func_t wake_func) | |
5146 | __acquires(&ctx->completion_lock) | |
5147 | { | |
5148 | struct io_ring_ctx *ctx = req->ctx; | |
5149 | bool cancel = false; | |
5150 | ||
4d52f338 | 5151 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5152 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5153 | poll->file = req->file; |
18bceab1 | 5154 | poll->wait.private = req; |
d7718a9d JA |
5155 | |
5156 | ipt->pt._key = mask; | |
5157 | ipt->req = req; | |
5158 | ipt->error = -EINVAL; | |
5159 | ||
d7718a9d JA |
5160 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5161 | ||
5162 | spin_lock_irq(&ctx->completion_lock); | |
5163 | if (likely(poll->head)) { | |
5164 | spin_lock(&poll->head->lock); | |
5165 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5166 | if (ipt->error) | |
5167 | cancel = true; | |
5168 | ipt->error = 0; | |
5169 | mask = 0; | |
5170 | } | |
5171 | if (mask || ipt->error) | |
5172 | list_del_init(&poll->wait.entry); | |
5173 | else if (cancel) | |
5174 | WRITE_ONCE(poll->canceled, true); | |
5175 | else if (!poll->done) /* actually waiting for an event */ | |
5176 | io_poll_req_insert(req); | |
5177 | spin_unlock(&poll->head->lock); | |
5178 | } | |
5179 | ||
5180 | return mask; | |
5181 | } | |
5182 | ||
5183 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5184 | { | |
5185 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5186 | struct io_ring_ctx *ctx = req->ctx; | |
5187 | struct async_poll *apoll; | |
5188 | struct io_poll_table ipt; | |
5189 | __poll_t mask, ret; | |
9dab14b8 | 5190 | int rw; |
d7718a9d JA |
5191 | |
5192 | if (!req->file || !file_can_poll(req->file)) | |
5193 | return false; | |
24c74678 | 5194 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5195 | return false; |
9dab14b8 JA |
5196 | if (def->pollin) |
5197 | rw = READ; | |
5198 | else if (def->pollout) | |
5199 | rw = WRITE; | |
5200 | else | |
5201 | return false; | |
5202 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5203 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5204 | return false; |
5205 | ||
5206 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5207 | if (unlikely(!apoll)) | |
5208 | return false; | |
807abcb0 | 5209 | apoll->double_poll = NULL; |
d7718a9d JA |
5210 | |
5211 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5212 | req->apoll = apoll; |
d7718a9d | 5213 | |
8755d97a | 5214 | mask = 0; |
d7718a9d | 5215 | if (def->pollin) |
8755d97a | 5216 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5217 | if (def->pollout) |
5218 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5219 | |
5220 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5221 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5222 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5223 | mask &= ~POLLIN; | |
5224 | ||
d7718a9d JA |
5225 | mask |= POLLERR | POLLPRI; |
5226 | ||
5227 | ipt.pt._qproc = io_async_queue_proc; | |
5228 | ||
5229 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5230 | io_async_wake); | |
a36da65c | 5231 | if (ret || ipt.error) { |
d4e7cd36 | 5232 | io_poll_remove_double(req); |
d7718a9d | 5233 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5234 | kfree(apoll->double_poll); |
d7718a9d JA |
5235 | kfree(apoll); |
5236 | return false; | |
5237 | } | |
5238 | spin_unlock_irq(&ctx->completion_lock); | |
5239 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5240 | apoll->poll.events); | |
5241 | return true; | |
5242 | } | |
5243 | ||
5244 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5245 | struct io_poll_iocb *poll) | |
221c5eb2 | 5246 | { |
b41e9852 | 5247 | bool do_complete = false; |
221c5eb2 JA |
5248 | |
5249 | spin_lock(&poll->head->lock); | |
5250 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5251 | if (!list_empty(&poll->wait.entry)) { |
5252 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5253 | do_complete = true; |
221c5eb2 JA |
5254 | } |
5255 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5256 | hash_del(&req->hash_node); |
d7718a9d JA |
5257 | return do_complete; |
5258 | } | |
5259 | ||
5260 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5261 | { | |
5262 | bool do_complete; | |
5263 | ||
d4e7cd36 JA |
5264 | io_poll_remove_double(req); |
5265 | ||
d7718a9d JA |
5266 | if (req->opcode == IORING_OP_POLL_ADD) { |
5267 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5268 | } else { | |
3bfa5bcb JA |
5269 | struct async_poll *apoll = req->apoll; |
5270 | ||
d7718a9d | 5271 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5272 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5273 | if (do_complete) { | |
d7718a9d | 5274 | io_put_req(req); |
807abcb0 | 5275 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5276 | kfree(apoll); |
5277 | } | |
b1f573bd XW |
5278 | } |
5279 | ||
b41e9852 JA |
5280 | if (do_complete) { |
5281 | io_cqring_fill_event(req, -ECANCELED); | |
5282 | io_commit_cqring(req->ctx); | |
f254ac04 | 5283 | req_set_fail_links(req); |
216578e5 | 5284 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5285 | } |
5286 | ||
5287 | return do_complete; | |
221c5eb2 JA |
5288 | } |
5289 | ||
76e1b642 JA |
5290 | /* |
5291 | * Returns true if we found and killed one or more poll requests | |
5292 | */ | |
5293 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk) | |
221c5eb2 | 5294 | { |
78076bb6 | 5295 | struct hlist_node *tmp; |
221c5eb2 | 5296 | struct io_kiocb *req; |
8e2e1faf | 5297 | int posted = 0, i; |
221c5eb2 JA |
5298 | |
5299 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5300 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5301 | struct hlist_head *list; | |
5302 | ||
5303 | list = &ctx->cancel_hash[i]; | |
f3606e3a JA |
5304 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
5305 | if (io_task_match(req, tsk)) | |
5306 | posted += io_poll_remove_one(req); | |
5307 | } | |
221c5eb2 JA |
5308 | } |
5309 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5310 | |
8e2e1faf JA |
5311 | if (posted) |
5312 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5313 | |
5314 | return posted != 0; | |
221c5eb2 JA |
5315 | } |
5316 | ||
47f46768 JA |
5317 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5318 | { | |
78076bb6 | 5319 | struct hlist_head *list; |
47f46768 JA |
5320 | struct io_kiocb *req; |
5321 | ||
78076bb6 JA |
5322 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5323 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5324 | if (sqe_addr != req->user_data) |
5325 | continue; | |
5326 | if (io_poll_remove_one(req)) | |
eac406c6 | 5327 | return 0; |
b41e9852 | 5328 | return -EALREADY; |
47f46768 JA |
5329 | } |
5330 | ||
5331 | return -ENOENT; | |
5332 | } | |
5333 | ||
3529d8c2 JA |
5334 | static int io_poll_remove_prep(struct io_kiocb *req, |
5335 | const struct io_uring_sqe *sqe) | |
0969e783 | 5336 | { |
0969e783 JA |
5337 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5338 | return -EINVAL; | |
5339 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5340 | sqe->poll_events) | |
5341 | return -EINVAL; | |
5342 | ||
5343 | req->poll.addr = READ_ONCE(sqe->addr); | |
0969e783 JA |
5344 | return 0; |
5345 | } | |
5346 | ||
221c5eb2 JA |
5347 | /* |
5348 | * Find a running poll command that matches one specified in sqe->addr, | |
5349 | * and remove it if found. | |
5350 | */ | |
fc4df999 | 5351 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5352 | { |
5353 | struct io_ring_ctx *ctx = req->ctx; | |
0969e783 | 5354 | u64 addr; |
47f46768 | 5355 | int ret; |
221c5eb2 | 5356 | |
0969e783 | 5357 | addr = req->poll.addr; |
221c5eb2 | 5358 | spin_lock_irq(&ctx->completion_lock); |
0969e783 | 5359 | ret = io_poll_cancel(ctx, addr); |
221c5eb2 JA |
5360 | spin_unlock_irq(&ctx->completion_lock); |
5361 | ||
4e88d6e7 JA |
5362 | if (ret < 0) |
5363 | req_set_fail_links(req); | |
e1e16097 | 5364 | io_req_complete(req, ret); |
221c5eb2 JA |
5365 | return 0; |
5366 | } | |
5367 | ||
221c5eb2 JA |
5368 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5369 | void *key) | |
5370 | { | |
c2f2eb7d JA |
5371 | struct io_kiocb *req = wait->private; |
5372 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5373 | |
d7718a9d | 5374 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5375 | } |
5376 | ||
221c5eb2 JA |
5377 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5378 | struct poll_table_struct *p) | |
5379 | { | |
5380 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5381 | ||
e8c2bc1f | 5382 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5383 | } |
5384 | ||
3529d8c2 | 5385 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5386 | { |
5387 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5388 | u32 events; |
221c5eb2 JA |
5389 | |
5390 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5391 | return -EINVAL; | |
5392 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5393 | return -EINVAL; | |
5394 | ||
5769a351 JX |
5395 | events = READ_ONCE(sqe->poll32_events); |
5396 | #ifdef __BIG_ENDIAN | |
5397 | events = swahw32(events); | |
5398 | #endif | |
a31eb4a2 JX |
5399 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5400 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5401 | return 0; |
5402 | } | |
5403 | ||
014db007 | 5404 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5405 | { |
5406 | struct io_poll_iocb *poll = &req->poll; | |
5407 | struct io_ring_ctx *ctx = req->ctx; | |
5408 | struct io_poll_table ipt; | |
0969e783 | 5409 | __poll_t mask; |
0969e783 | 5410 | |
d7718a9d | 5411 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5412 | |
d7718a9d JA |
5413 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5414 | io_poll_wake); | |
221c5eb2 | 5415 | |
8c838788 | 5416 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5417 | ipt.error = 0; |
b0dd8a41 | 5418 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5419 | } |
221c5eb2 JA |
5420 | spin_unlock_irq(&ctx->completion_lock); |
5421 | ||
8c838788 JA |
5422 | if (mask) { |
5423 | io_cqring_ev_posted(ctx); | |
014db007 | 5424 | io_put_req(req); |
221c5eb2 | 5425 | } |
8c838788 | 5426 | return ipt.error; |
221c5eb2 JA |
5427 | } |
5428 | ||
5262f567 JA |
5429 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5430 | { | |
ad8a48ac JA |
5431 | struct io_timeout_data *data = container_of(timer, |
5432 | struct io_timeout_data, timer); | |
5433 | struct io_kiocb *req = data->req; | |
5434 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5435 | unsigned long flags; |
5436 | ||
5262f567 | 5437 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5438 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5439 | atomic_set(&req->ctx->cq_timeouts, |
5440 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5441 | ||
78e19bbe | 5442 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5443 | io_commit_cqring(ctx); |
5444 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5445 | ||
5446 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5447 | req_set_fail_links(req); |
5262f567 JA |
5448 | io_put_req(req); |
5449 | return HRTIMER_NORESTART; | |
5450 | } | |
5451 | ||
f254ac04 JA |
5452 | static int __io_timeout_cancel(struct io_kiocb *req) |
5453 | { | |
e8c2bc1f | 5454 | struct io_timeout_data *io = req->async_data; |
f254ac04 JA |
5455 | int ret; |
5456 | ||
e8c2bc1f | 5457 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 JA |
5458 | if (ret == -1) |
5459 | return -EALREADY; | |
a71976f3 | 5460 | list_del_init(&req->timeout.list); |
f254ac04 JA |
5461 | |
5462 | req_set_fail_links(req); | |
f254ac04 | 5463 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5464 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5465 | return 0; |
5466 | } | |
5467 | ||
47f46768 JA |
5468 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5469 | { | |
5470 | struct io_kiocb *req; | |
5471 | int ret = -ENOENT; | |
5472 | ||
135fcde8 | 5473 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5474 | if (user_data == req->user_data) { |
47f46768 JA |
5475 | ret = 0; |
5476 | break; | |
5477 | } | |
5478 | } | |
5479 | ||
5480 | if (ret == -ENOENT) | |
5481 | return ret; | |
5482 | ||
f254ac04 | 5483 | return __io_timeout_cancel(req); |
47f46768 JA |
5484 | } |
5485 | ||
3529d8c2 JA |
5486 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5487 | const struct io_uring_sqe *sqe) | |
b29472ee | 5488 | { |
b29472ee JA |
5489 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5490 | return -EINVAL; | |
61710e43 DA |
5491 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5492 | return -EINVAL; | |
0bdf7a2d | 5493 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags) |
b29472ee JA |
5494 | return -EINVAL; |
5495 | ||
0bdf7a2d | 5496 | req->timeout_rem.addr = READ_ONCE(sqe->addr); |
b29472ee JA |
5497 | return 0; |
5498 | } | |
5499 | ||
11365043 JA |
5500 | /* |
5501 | * Remove or update an existing timeout command | |
5502 | */ | |
fc4df999 | 5503 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 JA |
5504 | { |
5505 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5506 | int ret; |
11365043 | 5507 | |
11365043 | 5508 | spin_lock_irq(&ctx->completion_lock); |
0bdf7a2d | 5509 | ret = io_timeout_cancel(ctx, req->timeout_rem.addr); |
11365043 | 5510 | |
47f46768 | 5511 | io_cqring_fill_event(req, ret); |
11365043 JA |
5512 | io_commit_cqring(ctx); |
5513 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5514 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5515 | if (ret < 0) |
5516 | req_set_fail_links(req); | |
ec9c02ad | 5517 | io_put_req(req); |
11365043 | 5518 | return 0; |
5262f567 JA |
5519 | } |
5520 | ||
3529d8c2 | 5521 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5522 | bool is_timeout_link) |
5262f567 | 5523 | { |
ad8a48ac | 5524 | struct io_timeout_data *data; |
a41525ab | 5525 | unsigned flags; |
56080b02 | 5526 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5527 | |
ad8a48ac | 5528 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5529 | return -EINVAL; |
ad8a48ac | 5530 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5531 | return -EINVAL; |
56080b02 | 5532 | if (off && is_timeout_link) |
2d28390a | 5533 | return -EINVAL; |
a41525ab JA |
5534 | flags = READ_ONCE(sqe->timeout_flags); |
5535 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5536 | return -EINVAL; |
bdf20073 | 5537 | |
bfe68a22 | 5538 | req->timeout.off = off; |
26a61679 | 5539 | |
e8c2bc1f | 5540 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5541 | return -ENOMEM; |
5542 | ||
e8c2bc1f | 5543 | data = req->async_data; |
ad8a48ac | 5544 | data->req = req; |
ad8a48ac JA |
5545 | |
5546 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5547 | return -EFAULT; |
5548 | ||
11365043 | 5549 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 5550 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 5551 | else |
ad8a48ac | 5552 | data->mode = HRTIMER_MODE_REL; |
11365043 | 5553 | |
ad8a48ac JA |
5554 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5555 | return 0; | |
5556 | } | |
5557 | ||
fc4df999 | 5558 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5559 | { |
ad8a48ac | 5560 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5561 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5562 | struct list_head *entry; |
bfe68a22 | 5563 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5564 | |
733f5c95 | 5565 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5566 | |
5262f567 JA |
5567 | /* |
5568 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5569 | * timeout event to be satisfied. If it isn't set, then this is |
5570 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5571 | */ |
8eb7e2d0 | 5572 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5573 | entry = ctx->timeout_list.prev; |
5574 | goto add; | |
5575 | } | |
5262f567 | 5576 | |
bfe68a22 PB |
5577 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5578 | req->timeout.target_seq = tail + off; | |
5262f567 JA |
5579 | |
5580 | /* | |
5581 | * Insertion sort, ensuring the first entry in the list is always | |
5582 | * the one we need first. | |
5583 | */ | |
5262f567 | 5584 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5585 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5586 | timeout.list); | |
5262f567 | 5587 | |
8eb7e2d0 | 5588 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5589 | continue; |
bfe68a22 PB |
5590 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5591 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5592 | break; |
5593 | } | |
93bd25bb | 5594 | add: |
135fcde8 | 5595 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5596 | data->timer.function = io_timeout_fn; |
5597 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5598 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5599 | return 0; |
5600 | } | |
5262f567 | 5601 | |
62755e35 JA |
5602 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5603 | { | |
5604 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5605 | ||
5606 | return req->user_data == (unsigned long) data; | |
5607 | } | |
5608 | ||
e977d6d3 | 5609 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5610 | { |
62755e35 | 5611 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5612 | int ret = 0; |
5613 | ||
4f26bda1 | 5614 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5615 | switch (cancel_ret) { |
5616 | case IO_WQ_CANCEL_OK: | |
5617 | ret = 0; | |
5618 | break; | |
5619 | case IO_WQ_CANCEL_RUNNING: | |
5620 | ret = -EALREADY; | |
5621 | break; | |
5622 | case IO_WQ_CANCEL_NOTFOUND: | |
5623 | ret = -ENOENT; | |
5624 | break; | |
5625 | } | |
5626 | ||
e977d6d3 JA |
5627 | return ret; |
5628 | } | |
5629 | ||
47f46768 JA |
5630 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5631 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5632 | int success_ret) |
47f46768 JA |
5633 | { |
5634 | unsigned long flags; | |
5635 | int ret; | |
5636 | ||
5637 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5638 | if (ret != -ENOENT) { | |
5639 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5640 | goto done; | |
5641 | } | |
5642 | ||
5643 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5644 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5645 | if (ret != -ENOENT) | |
5646 | goto done; | |
5647 | ret = io_poll_cancel(ctx, sqe_addr); | |
5648 | done: | |
b0dd8a41 JA |
5649 | if (!ret) |
5650 | ret = success_ret; | |
47f46768 JA |
5651 | io_cqring_fill_event(req, ret); |
5652 | io_commit_cqring(ctx); | |
5653 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5654 | io_cqring_ev_posted(ctx); | |
5655 | ||
4e88d6e7 JA |
5656 | if (ret < 0) |
5657 | req_set_fail_links(req); | |
014db007 | 5658 | io_put_req(req); |
47f46768 JA |
5659 | } |
5660 | ||
3529d8c2 JA |
5661 | static int io_async_cancel_prep(struct io_kiocb *req, |
5662 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5663 | { |
fbf23849 | 5664 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5665 | return -EINVAL; |
61710e43 DA |
5666 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5667 | return -EINVAL; | |
5668 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5669 | return -EINVAL; |
5670 | ||
fbf23849 JA |
5671 | req->cancel.addr = READ_ONCE(sqe->addr); |
5672 | return 0; | |
5673 | } | |
5674 | ||
014db007 | 5675 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
5676 | { |
5677 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5678 | |
014db007 | 5679 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5680 | return 0; |
5681 | } | |
5682 | ||
05f3fb3c JA |
5683 | static int io_files_update_prep(struct io_kiocb *req, |
5684 | const struct io_uring_sqe *sqe) | |
5685 | { | |
6ca56f84 JA |
5686 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5687 | return -EINVAL; | |
61710e43 DA |
5688 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5689 | return -EINVAL; | |
5690 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5691 | return -EINVAL; |
5692 | ||
5693 | req->files_update.offset = READ_ONCE(sqe->off); | |
5694 | req->files_update.nr_args = READ_ONCE(sqe->len); | |
5695 | if (!req->files_update.nr_args) | |
5696 | return -EINVAL; | |
5697 | req->files_update.arg = READ_ONCE(sqe->addr); | |
5698 | return 0; | |
5699 | } | |
5700 | ||
229a7b63 JA |
5701 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
5702 | struct io_comp_state *cs) | |
fbf23849 JA |
5703 | { |
5704 | struct io_ring_ctx *ctx = req->ctx; | |
05f3fb3c JA |
5705 | struct io_uring_files_update up; |
5706 | int ret; | |
fbf23849 | 5707 | |
f86cd20c | 5708 | if (force_nonblock) |
05f3fb3c | 5709 | return -EAGAIN; |
05f3fb3c JA |
5710 | |
5711 | up.offset = req->files_update.offset; | |
5712 | up.fds = req->files_update.arg; | |
5713 | ||
5714 | mutex_lock(&ctx->uring_lock); | |
5715 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); | |
5716 | mutex_unlock(&ctx->uring_lock); | |
5717 | ||
5718 | if (ret < 0) | |
5719 | req_set_fail_links(req); | |
229a7b63 | 5720 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
5721 | return 0; |
5722 | } | |
5723 | ||
bfe76559 | 5724 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5725 | { |
d625c6ee | 5726 | switch (req->opcode) { |
e781573e | 5727 | case IORING_OP_NOP: |
bfe76559 | 5728 | return 0; |
f67676d1 JA |
5729 | case IORING_OP_READV: |
5730 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5731 | case IORING_OP_READ: |
bfe76559 | 5732 | return io_read_prep(req, sqe); |
f67676d1 JA |
5733 | case IORING_OP_WRITEV: |
5734 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5735 | case IORING_OP_WRITE: |
bfe76559 | 5736 | return io_write_prep(req, sqe); |
0969e783 | 5737 | case IORING_OP_POLL_ADD: |
bfe76559 | 5738 | return io_poll_add_prep(req, sqe); |
0969e783 | 5739 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5740 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5741 | case IORING_OP_FSYNC: |
bfe76559 | 5742 | return io_prep_fsync(req, sqe); |
8ed8d3c3 | 5743 | case IORING_OP_SYNC_FILE_RANGE: |
bfe76559 | 5744 | return io_prep_sfr(req, sqe); |
03b1230c | 5745 | case IORING_OP_SENDMSG: |
fddaface | 5746 | case IORING_OP_SEND: |
bfe76559 | 5747 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5748 | case IORING_OP_RECVMSG: |
fddaface | 5749 | case IORING_OP_RECV: |
bfe76559 | 5750 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5751 | case IORING_OP_CONNECT: |
bfe76559 | 5752 | return io_connect_prep(req, sqe); |
2d28390a | 5753 | case IORING_OP_TIMEOUT: |
bfe76559 | 5754 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5755 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5756 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5757 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5758 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5759 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5760 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5761 | case IORING_OP_ACCEPT: |
bfe76559 | 5762 | return io_accept_prep(req, sqe); |
d63d1b5e | 5763 | case IORING_OP_FALLOCATE: |
bfe76559 | 5764 | return io_fallocate_prep(req, sqe); |
15b71abe | 5765 | case IORING_OP_OPENAT: |
bfe76559 | 5766 | return io_openat_prep(req, sqe); |
b5dba59e | 5767 | case IORING_OP_CLOSE: |
bfe76559 | 5768 | return io_close_prep(req, sqe); |
05f3fb3c | 5769 | case IORING_OP_FILES_UPDATE: |
bfe76559 | 5770 | return io_files_update_prep(req, sqe); |
eddc7ef5 | 5771 | case IORING_OP_STATX: |
bfe76559 | 5772 | return io_statx_prep(req, sqe); |
4840e418 | 5773 | case IORING_OP_FADVISE: |
bfe76559 | 5774 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5775 | case IORING_OP_MADVISE: |
bfe76559 | 5776 | return io_madvise_prep(req, sqe); |
cebdb986 | 5777 | case IORING_OP_OPENAT2: |
bfe76559 | 5778 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5779 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5780 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5781 | case IORING_OP_SPLICE: |
bfe76559 | 5782 | return io_splice_prep(req, sqe); |
ddf0322d | 5783 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5784 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5785 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5786 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5787 | case IORING_OP_TEE: |
bfe76559 | 5788 | return io_tee_prep(req, sqe); |
f67676d1 JA |
5789 | } |
5790 | ||
bfe76559 PB |
5791 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5792 | req->opcode); | |
5793 | return-EINVAL; | |
5794 | } | |
5795 | ||
5796 | static int io_req_defer_prep(struct io_kiocb *req, | |
5797 | const struct io_uring_sqe *sqe) | |
5798 | { | |
bfe76559 PB |
5799 | if (!sqe) |
5800 | return 0; | |
5801 | if (io_alloc_async_data(req)) | |
5802 | return -EAGAIN; | |
bfe76559 | 5803 | return io_req_prep(req, sqe); |
f67676d1 JA |
5804 | } |
5805 | ||
9cf7c104 PB |
5806 | static u32 io_get_sequence(struct io_kiocb *req) |
5807 | { | |
5808 | struct io_kiocb *pos; | |
5809 | struct io_ring_ctx *ctx = req->ctx; | |
5810 | u32 total_submitted, nr_reqs = 1; | |
5811 | ||
5812 | if (req->flags & REQ_F_LINK_HEAD) | |
5813 | list_for_each_entry(pos, &req->link_list, link_list) | |
5814 | nr_reqs++; | |
5815 | ||
5816 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5817 | return total_submitted - nr_reqs; | |
5818 | } | |
5819 | ||
3529d8c2 | 5820 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 5821 | { |
a197f664 | 5822 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5823 | struct io_defer_entry *de; |
f67676d1 | 5824 | int ret; |
9cf7c104 | 5825 | u32 seq; |
de0617e4 | 5826 | |
9d858b21 | 5827 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5828 | if (likely(list_empty_careful(&ctx->defer_list) && |
5829 | !(req->flags & REQ_F_IO_DRAIN))) | |
5830 | return 0; | |
5831 | ||
5832 | seq = io_get_sequence(req); | |
5833 | /* Still a chance to pass the sequence check */ | |
5834 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5835 | return 0; |
5836 | ||
e8c2bc1f | 5837 | if (!req->async_data) { |
650b5481 | 5838 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 5839 | if (ret) |
650b5481 PB |
5840 | return ret; |
5841 | } | |
cbdcb435 | 5842 | io_prep_async_link(req); |
27dc8338 PB |
5843 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5844 | if (!de) | |
5845 | return -ENOMEM; | |
2d28390a | 5846 | |
de0617e4 | 5847 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5848 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5849 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5850 | kfree(de); |
ae34817b PB |
5851 | io_queue_async_work(req); |
5852 | return -EIOCBQUEUED; | |
de0617e4 JA |
5853 | } |
5854 | ||
915967f6 | 5855 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5856 | de->req = req; |
9cf7c104 | 5857 | de->seq = seq; |
27dc8338 | 5858 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5859 | spin_unlock_irq(&ctx->completion_lock); |
5860 | return -EIOCBQUEUED; | |
5861 | } | |
5862 | ||
f573d384 | 5863 | static void io_req_drop_files(struct io_kiocb *req) |
99bc4c38 | 5864 | { |
f573d384 JA |
5865 | struct io_ring_ctx *ctx = req->ctx; |
5866 | unsigned long flags; | |
5867 | ||
59090a32 PB |
5868 | put_files_struct(req->work.identity->files); |
5869 | put_nsproxy(req->work.identity->nsproxy); | |
f573d384 JA |
5870 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
5871 | list_del(&req->inflight_entry); | |
f573d384 JA |
5872 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
5873 | req->flags &= ~REQ_F_INFLIGHT; | |
dfead8a8 | 5874 | req->work.flags &= ~IO_WQ_WORK_FILES; |
59090a32 PB |
5875 | if (waitqueue_active(&ctx->inflight_wait)) |
5876 | wake_up(&ctx->inflight_wait); | |
f573d384 | 5877 | } |
99bc4c38 | 5878 | |
3ca405eb | 5879 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5880 | { |
0e1b6fe3 PB |
5881 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5882 | switch (req->opcode) { | |
5883 | case IORING_OP_READV: | |
5884 | case IORING_OP_READ_FIXED: | |
5885 | case IORING_OP_READ: | |
bcda7baa | 5886 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5887 | break; |
5888 | case IORING_OP_RECVMSG: | |
5889 | case IORING_OP_RECV: | |
bcda7baa | 5890 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5891 | break; |
5892 | } | |
5893 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5894 | } |
5895 | ||
0e1b6fe3 PB |
5896 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5897 | switch (req->opcode) { | |
5898 | case IORING_OP_READV: | |
5899 | case IORING_OP_READ_FIXED: | |
5900 | case IORING_OP_READ: | |
5901 | case IORING_OP_WRITEV: | |
5902 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5903 | case IORING_OP_WRITE: { |
5904 | struct io_async_rw *io = req->async_data; | |
5905 | if (io->free_iovec) | |
5906 | kfree(io->free_iovec); | |
0e1b6fe3 | 5907 | break; |
e8c2bc1f | 5908 | } |
0e1b6fe3 | 5909 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5910 | case IORING_OP_SENDMSG: { |
5911 | struct io_async_msghdr *io = req->async_data; | |
5912 | if (io->iov != io->fast_iov) | |
5913 | kfree(io->iov); | |
0e1b6fe3 | 5914 | break; |
e8c2bc1f | 5915 | } |
0e1b6fe3 PB |
5916 | case IORING_OP_SPLICE: |
5917 | case IORING_OP_TEE: | |
5918 | io_put_file(req, req->splice.file_in, | |
5919 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5920 | break; | |
f3cd4850 JA |
5921 | case IORING_OP_OPENAT: |
5922 | case IORING_OP_OPENAT2: | |
5923 | if (req->open.filename) | |
5924 | putname(req->open.filename); | |
5925 | break; | |
0e1b6fe3 PB |
5926 | } |
5927 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5928 | } |
bb175342 | 5929 | |
f573d384 JA |
5930 | if (req->flags & REQ_F_INFLIGHT) |
5931 | io_req_drop_files(req); | |
99bc4c38 PB |
5932 | } |
5933 | ||
c1379e24 PB |
5934 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
5935 | struct io_comp_state *cs) | |
2b188cc1 | 5936 | { |
a197f664 | 5937 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 5938 | int ret; |
2b188cc1 | 5939 | |
d625c6ee | 5940 | switch (req->opcode) { |
2b188cc1 | 5941 | case IORING_OP_NOP: |
229a7b63 | 5942 | ret = io_nop(req, cs); |
2b188cc1 JA |
5943 | break; |
5944 | case IORING_OP_READV: | |
edafccee | 5945 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5946 | case IORING_OP_READ: |
a1d7c393 | 5947 | ret = io_read(req, force_nonblock, cs); |
edafccee | 5948 | break; |
3529d8c2 | 5949 | case IORING_OP_WRITEV: |
edafccee | 5950 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5951 | case IORING_OP_WRITE: |
a1d7c393 | 5952 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 5953 | break; |
c992fe29 | 5954 | case IORING_OP_FSYNC: |
014db007 | 5955 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 5956 | break; |
221c5eb2 | 5957 | case IORING_OP_POLL_ADD: |
014db007 | 5958 | ret = io_poll_add(req); |
221c5eb2 JA |
5959 | break; |
5960 | case IORING_OP_POLL_REMOVE: | |
fc4df999 | 5961 | ret = io_poll_remove(req); |
221c5eb2 | 5962 | break; |
5d17b4a4 | 5963 | case IORING_OP_SYNC_FILE_RANGE: |
014db007 | 5964 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 5965 | break; |
0fa03c62 | 5966 | case IORING_OP_SENDMSG: |
062d04d7 PB |
5967 | ret = io_sendmsg(req, force_nonblock, cs); |
5968 | break; | |
fddaface | 5969 | case IORING_OP_SEND: |
062d04d7 | 5970 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 5971 | break; |
aa1fa28f | 5972 | case IORING_OP_RECVMSG: |
062d04d7 PB |
5973 | ret = io_recvmsg(req, force_nonblock, cs); |
5974 | break; | |
fddaface | 5975 | case IORING_OP_RECV: |
062d04d7 | 5976 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 5977 | break; |
5262f567 | 5978 | case IORING_OP_TIMEOUT: |
fc4df999 | 5979 | ret = io_timeout(req); |
5262f567 | 5980 | break; |
11365043 | 5981 | case IORING_OP_TIMEOUT_REMOVE: |
fc4df999 | 5982 | ret = io_timeout_remove(req); |
11365043 | 5983 | break; |
17f2fe35 | 5984 | case IORING_OP_ACCEPT: |
229a7b63 | 5985 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 5986 | break; |
f8e85cf2 | 5987 | case IORING_OP_CONNECT: |
229a7b63 | 5988 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 5989 | break; |
62755e35 | 5990 | case IORING_OP_ASYNC_CANCEL: |
014db007 | 5991 | ret = io_async_cancel(req); |
62755e35 | 5992 | break; |
d63d1b5e | 5993 | case IORING_OP_FALLOCATE: |
014db007 | 5994 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 5995 | break; |
15b71abe | 5996 | case IORING_OP_OPENAT: |
014db007 | 5997 | ret = io_openat(req, force_nonblock); |
15b71abe | 5998 | break; |
b5dba59e | 5999 | case IORING_OP_CLOSE: |
229a7b63 | 6000 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 6001 | break; |
05f3fb3c | 6002 | case IORING_OP_FILES_UPDATE: |
229a7b63 | 6003 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 6004 | break; |
eddc7ef5 | 6005 | case IORING_OP_STATX: |
014db007 | 6006 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 6007 | break; |
4840e418 | 6008 | case IORING_OP_FADVISE: |
014db007 | 6009 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 6010 | break; |
c1ca757b | 6011 | case IORING_OP_MADVISE: |
014db007 | 6012 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 6013 | break; |
cebdb986 | 6014 | case IORING_OP_OPENAT2: |
014db007 | 6015 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 6016 | break; |
3e4827b0 | 6017 | case IORING_OP_EPOLL_CTL: |
229a7b63 | 6018 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 6019 | break; |
7d67af2c | 6020 | case IORING_OP_SPLICE: |
014db007 | 6021 | ret = io_splice(req, force_nonblock); |
7d67af2c | 6022 | break; |
ddf0322d | 6023 | case IORING_OP_PROVIDE_BUFFERS: |
229a7b63 | 6024 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 6025 | break; |
067524e9 | 6026 | case IORING_OP_REMOVE_BUFFERS: |
229a7b63 | 6027 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 6028 | break; |
f2a8d5c7 | 6029 | case IORING_OP_TEE: |
f2a8d5c7 PB |
6030 | ret = io_tee(req, force_nonblock); |
6031 | break; | |
2b188cc1 JA |
6032 | default: |
6033 | ret = -EINVAL; | |
6034 | break; | |
6035 | } | |
6036 | ||
def596e9 JA |
6037 | if (ret) |
6038 | return ret; | |
6039 | ||
b532576e JA |
6040 | /* If the op doesn't have a file, we're not polling for it */ |
6041 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6042 | const bool in_async = io_wq_current_is_worker(); |
6043 | ||
11ba820b JA |
6044 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6045 | if (in_async) | |
6046 | mutex_lock(&ctx->uring_lock); | |
6047 | ||
def596e9 | 6048 | io_iopoll_req_issued(req); |
11ba820b JA |
6049 | |
6050 | if (in_async) | |
6051 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6052 | } |
6053 | ||
6054 | return 0; | |
2b188cc1 JA |
6055 | } |
6056 | ||
f4db7182 | 6057 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6058 | { |
6059 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6060 | struct io_kiocb *timeout; |
561fb04a | 6061 | int ret = 0; |
2b188cc1 | 6062 | |
6df1db6b PB |
6063 | timeout = io_prep_linked_timeout(req); |
6064 | if (timeout) | |
6065 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6066 | |
0c9d5ccd JA |
6067 | /* if NO_CANCEL is set, we must still run the work */ |
6068 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
6069 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 6070 | ret = -ECANCELED; |
0c9d5ccd | 6071 | } |
31b51510 | 6072 | |
561fb04a | 6073 | if (!ret) { |
561fb04a | 6074 | do { |
c1379e24 | 6075 | ret = io_issue_sqe(req, false, NULL); |
561fb04a JA |
6076 | /* |
6077 | * We can get EAGAIN for polled IO even though we're | |
6078 | * forcing a sync submission from here, since we can't | |
6079 | * wait for request slots on the block side. | |
6080 | */ | |
6081 | if (ret != -EAGAIN) | |
6082 | break; | |
6083 | cond_resched(); | |
6084 | } while (1); | |
6085 | } | |
31b51510 | 6086 | |
561fb04a | 6087 | if (ret) { |
69da0eaa XW |
6088 | struct io_ring_ctx *lock_ctx = NULL; |
6089 | ||
6090 | if (req->ctx->flags & IORING_SETUP_IOPOLL) | |
6091 | lock_ctx = req->ctx; | |
6092 | ||
613ec5af | 6093 | /* |
69da0eaa XW |
6094 | * io_iopoll_complete() does not hold completion_lock to |
6095 | * complete polled io, so here for polled io, we can not call | |
6096 | * io_req_complete() directly, otherwise there maybe concurrent | |
6097 | * access to cqring, defer_list, etc, which is not safe. Given | |
6098 | * that io_iopoll_complete() is always called under uring_lock, | |
6099 | * so here for polled io, we also get uring_lock to complete | |
6100 | * it. | |
613ec5af | 6101 | */ |
69da0eaa XW |
6102 | if (lock_ctx) |
6103 | mutex_lock(&lock_ctx->uring_lock); | |
613ec5af | 6104 | |
69da0eaa XW |
6105 | req_set_fail_links(req); |
6106 | io_req_complete(req, ret); | |
6107 | ||
6108 | if (lock_ctx) | |
6109 | mutex_unlock(&lock_ctx->uring_lock); | |
edafccee | 6110 | } |
2b188cc1 | 6111 | |
f4db7182 | 6112 | return io_steal_work(req); |
2b188cc1 JA |
6113 | } |
6114 | ||
65e19f54 JA |
6115 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6116 | int index) | |
6117 | { | |
6118 | struct fixed_file_table *table; | |
6119 | ||
05f3fb3c | 6120 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6121 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6122 | } |
6123 | ||
8371adf5 PB |
6124 | static struct file *io_file_get(struct io_submit_state *state, |
6125 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6126 | { |
a197f664 | 6127 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6128 | struct file *file; |
09bb8394 | 6129 | |
8da11c19 | 6130 | if (fixed) { |
479f517b | 6131 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6132 | return NULL; |
b7620121 | 6133 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6134 | file = io_file_from_index(ctx, fd); |
fd2206e4 | 6135 | if (file) { |
b2e96852 | 6136 | req->fixed_file_refs = &ctx->file_data->node->refs; |
fd2206e4 JA |
6137 | percpu_ref_get(req->fixed_file_refs); |
6138 | } | |
09bb8394 | 6139 | } else { |
c826bd7a | 6140 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6141 | file = __io_file_get(state, fd); |
09bb8394 JA |
6142 | } |
6143 | ||
8371adf5 | 6144 | return file; |
09bb8394 JA |
6145 | } |
6146 | ||
8da11c19 | 6147 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
63ff8223 | 6148 | int fd) |
8da11c19 | 6149 | { |
8da11c19 PB |
6150 | bool fixed; |
6151 | ||
63ff8223 | 6152 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
0cdaf760 | 6153 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
8da11c19 PB |
6154 | return -EBADF; |
6155 | ||
8371adf5 PB |
6156 | req->file = io_file_get(state, req, fd, fixed); |
6157 | if (req->file || io_op_defs[req->opcode].needs_file_no_error) | |
f86cd20c | 6158 | return 0; |
8371adf5 | 6159 | return -EBADF; |
f56040b8 PB |
6160 | } |
6161 | ||
2665abfd | 6162 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6163 | { |
ad8a48ac JA |
6164 | struct io_timeout_data *data = container_of(timer, |
6165 | struct io_timeout_data, timer); | |
6166 | struct io_kiocb *req = data->req; | |
2665abfd JA |
6167 | struct io_ring_ctx *ctx = req->ctx; |
6168 | struct io_kiocb *prev = NULL; | |
6169 | unsigned long flags; | |
2665abfd JA |
6170 | |
6171 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
6172 | ||
6173 | /* | |
6174 | * We don't expect the list to be empty, that will only happen if we | |
6175 | * race with the completion of the linked work. | |
6176 | */ | |
4493233e PB |
6177 | if (!list_empty(&req->link_list)) { |
6178 | prev = list_entry(req->link_list.prev, struct io_kiocb, | |
6179 | link_list); | |
900fad45 | 6180 | if (refcount_inc_not_zero(&prev->refs)) |
4493233e | 6181 | list_del_init(&req->link_list); |
900fad45 | 6182 | else |
76a46e06 | 6183 | prev = NULL; |
2665abfd JA |
6184 | } |
6185 | ||
6186 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6187 | ||
6188 | if (prev) { | |
4e88d6e7 | 6189 | req_set_fail_links(prev); |
014db007 | 6190 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
76a46e06 | 6191 | io_put_req(prev); |
47f46768 | 6192 | } else { |
e1e16097 | 6193 | io_req_complete(req, -ETIME); |
2665abfd | 6194 | } |
2665abfd JA |
6195 | return HRTIMER_NORESTART; |
6196 | } | |
6197 | ||
7271ef3a | 6198 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6199 | { |
76a46e06 JA |
6200 | /* |
6201 | * If the list is now empty, then our linked request finished before | |
6202 | * we got a chance to setup the timer | |
6203 | */ | |
4493233e | 6204 | if (!list_empty(&req->link_list)) { |
e8c2bc1f | 6205 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6206 | |
ad8a48ac JA |
6207 | data->timer.function = io_link_timeout_fn; |
6208 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6209 | data->mode); | |
2665abfd | 6210 | } |
7271ef3a JA |
6211 | } |
6212 | ||
6213 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6214 | { | |
6215 | struct io_ring_ctx *ctx = req->ctx; | |
6216 | ||
6217 | spin_lock_irq(&ctx->completion_lock); | |
6218 | __io_queue_linked_timeout(req); | |
76a46e06 | 6219 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6220 | |
2665abfd | 6221 | /* drop submission reference */ |
76a46e06 JA |
6222 | io_put_req(req); |
6223 | } | |
2665abfd | 6224 | |
ad8a48ac | 6225 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd JA |
6226 | { |
6227 | struct io_kiocb *nxt; | |
6228 | ||
dea3b49c | 6229 | if (!(req->flags & REQ_F_LINK_HEAD)) |
2665abfd | 6230 | return NULL; |
6df1db6b | 6231 | if (req->flags & REQ_F_LINK_TIMEOUT) |
d7718a9d | 6232 | return NULL; |
2665abfd | 6233 | |
4493233e PB |
6234 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
6235 | link_list); | |
d625c6ee | 6236 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
76a46e06 | 6237 | return NULL; |
2665abfd | 6238 | |
900fad45 | 6239 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6240 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6241 | return nxt; |
2665abfd JA |
6242 | } |
6243 | ||
c1379e24 | 6244 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 | 6245 | { |
4a0a7a18 | 6246 | struct io_kiocb *linked_timeout; |
193155c8 | 6247 | const struct cred *old_creds = NULL; |
e0c5c576 | 6248 | int ret; |
2b188cc1 | 6249 | |
4a0a7a18 JA |
6250 | again: |
6251 | linked_timeout = io_prep_linked_timeout(req); | |
6252 | ||
2e5aa6cb PB |
6253 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6254 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
98447d65 | 6255 | req->work.identity->creds != current_cred()) { |
193155c8 JA |
6256 | if (old_creds) |
6257 | revert_creds(old_creds); | |
98447d65 | 6258 | if (old_creds == req->work.identity->creds) |
193155c8 JA |
6259 | old_creds = NULL; /* restored original creds */ |
6260 | else | |
98447d65 | 6261 | old_creds = override_creds(req->work.identity->creds); |
193155c8 JA |
6262 | } |
6263 | ||
c1379e24 | 6264 | ret = io_issue_sqe(req, true, cs); |
491381ce JA |
6265 | |
6266 | /* | |
6267 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6268 | * doesn't support non-blocking read/write attempts | |
6269 | */ | |
24c74678 | 6270 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6271 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6272 | /* |
6273 | * Queued up for async execution, worker will release | |
6274 | * submit reference when the iocb is actually submitted. | |
6275 | */ | |
6276 | io_queue_async_work(req); | |
2b188cc1 | 6277 | } |
bbad27b2 | 6278 | |
f063c547 PB |
6279 | if (linked_timeout) |
6280 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 PB |
6281 | } else if (likely(!ret)) { |
6282 | /* drop submission reference */ | |
6283 | req = io_put_req_find_next(req); | |
6284 | if (linked_timeout) | |
6285 | io_queue_linked_timeout(linked_timeout); | |
e65ef56d | 6286 | |
0d63c148 PB |
6287 | if (req) { |
6288 | if (!(req->flags & REQ_F_FORCE_ASYNC)) | |
6289 | goto again; | |
6290 | io_queue_async_work(req); | |
6291 | } | |
6292 | } else { | |
652532ad PB |
6293 | /* un-prep timeout, so it'll be killed as any other linked */ |
6294 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6295 | req_set_fail_links(req); |
e65ef56d | 6296 | io_put_req(req); |
e1e16097 | 6297 | io_req_complete(req, ret); |
9e645e11 | 6298 | } |
652532ad | 6299 | |
193155c8 JA |
6300 | if (old_creds) |
6301 | revert_creds(old_creds); | |
2b188cc1 JA |
6302 | } |
6303 | ||
f13fad7b JA |
6304 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6305 | struct io_comp_state *cs) | |
4fe2c963 JL |
6306 | { |
6307 | int ret; | |
6308 | ||
3529d8c2 | 6309 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6310 | if (ret) { |
6311 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6312 | fail_req: |
4e88d6e7 | 6313 | req_set_fail_links(req); |
e1e16097 JA |
6314 | io_put_req(req); |
6315 | io_req_complete(req, ret); | |
4fe2c963 | 6316 | } |
2550878f | 6317 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6318 | if (!req->async_data) { |
bd2ab18a | 6319 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6320 | if (unlikely(ret)) |
bd2ab18a PB |
6321 | goto fail_req; |
6322 | } | |
ce35a47a JA |
6323 | io_queue_async_work(req); |
6324 | } else { | |
c1379e24 PB |
6325 | if (sqe) { |
6326 | ret = io_req_prep(req, sqe); | |
6327 | if (unlikely(ret)) | |
6328 | goto fail_req; | |
6329 | } | |
6330 | __io_queue_sqe(req, cs); | |
ce35a47a | 6331 | } |
4fe2c963 JL |
6332 | } |
6333 | ||
f13fad7b JA |
6334 | static inline void io_queue_link_head(struct io_kiocb *req, |
6335 | struct io_comp_state *cs) | |
4fe2c963 | 6336 | { |
94ae5e77 | 6337 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6338 | io_put_req(req); |
6339 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6340 | } else |
f13fad7b | 6341 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6342 | } |
6343 | ||
1d4240cc | 6344 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
f13fad7b | 6345 | struct io_kiocb **link, struct io_comp_state *cs) |
9e645e11 | 6346 | { |
a197f664 | 6347 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6348 | int ret; |
9e645e11 | 6349 | |
9e645e11 JA |
6350 | /* |
6351 | * If we already have a head request, queue this one for async | |
6352 | * submittal once the head completes. If we don't have a head but | |
6353 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6354 | * submitted sync once the chain is complete. If none of those | |
6355 | * conditions are true (normal request), then just queue it. | |
6356 | */ | |
6357 | if (*link) { | |
9d76377f | 6358 | struct io_kiocb *head = *link; |
4e88d6e7 | 6359 | |
8cdf2193 PB |
6360 | /* |
6361 | * Taking sequential execution of a link, draining both sides | |
6362 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6363 | * requests in the link. So, it drains the head and the | |
6364 | * next after the link request. The last one is done via | |
6365 | * drain_next flag to persist the effect across calls. | |
6366 | */ | |
ef4ff581 | 6367 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6368 | head->flags |= REQ_F_IO_DRAIN; |
6369 | ctx->drain_next = 1; | |
6370 | } | |
3529d8c2 | 6371 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6372 | if (unlikely(ret)) { |
4e88d6e7 | 6373 | /* fail even hard links since we don't submit */ |
9d76377f | 6374 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6375 | return ret; |
2d28390a | 6376 | } |
9d76377f PB |
6377 | trace_io_uring_link(ctx, req, head); |
6378 | list_add_tail(&req->link_list, &head->link_list); | |
32fe525b PB |
6379 | |
6380 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6381 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6382 | io_queue_link_head(head, cs); |
32fe525b PB |
6383 | *link = NULL; |
6384 | } | |
9e645e11 | 6385 | } else { |
711be031 PB |
6386 | if (unlikely(ctx->drain_next)) { |
6387 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6388 | ctx->drain_next = 0; |
711be031 | 6389 | } |
ef4ff581 | 6390 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
dea3b49c | 6391 | req->flags |= REQ_F_LINK_HEAD; |
711be031 | 6392 | INIT_LIST_HEAD(&req->link_list); |
f1d96a8f | 6393 | |
711be031 | 6394 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6395 | if (unlikely(ret)) |
711be031 PB |
6396 | req->flags |= REQ_F_FAIL_LINK; |
6397 | *link = req; | |
6398 | } else { | |
f13fad7b | 6399 | io_queue_sqe(req, sqe, cs); |
711be031 | 6400 | } |
9e645e11 | 6401 | } |
2e6e1fde | 6402 | |
1d4240cc | 6403 | return 0; |
9e645e11 JA |
6404 | } |
6405 | ||
9a56a232 JA |
6406 | /* |
6407 | * Batched submission is done, ensure local IO is flushed out. | |
6408 | */ | |
6409 | static void io_submit_state_end(struct io_submit_state *state) | |
6410 | { | |
f13fad7b JA |
6411 | if (!list_empty(&state->comp.list)) |
6412 | io_submit_flush_completions(&state->comp); | |
9a56a232 | 6413 | blk_finish_plug(&state->plug); |
9f13c35b | 6414 | io_state_file_put(state); |
2579f913 | 6415 | if (state->free_reqs) |
6c8a3134 | 6416 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6417 | } |
6418 | ||
6419 | /* | |
6420 | * Start submission side cache. | |
6421 | */ | |
6422 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6423 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 JA |
6424 | { |
6425 | blk_start_plug(&state->plug); | |
013538bd JA |
6426 | state->comp.nr = 0; |
6427 | INIT_LIST_HEAD(&state->comp.list); | |
6428 | state->comp.ctx = ctx; | |
2579f913 | 6429 | state->free_reqs = 0; |
9a56a232 JA |
6430 | state->file = NULL; |
6431 | state->ios_left = max_ios; | |
6432 | } | |
6433 | ||
2b188cc1 JA |
6434 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6435 | { | |
75b28aff | 6436 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6437 | |
caf582c6 PB |
6438 | /* |
6439 | * Ensure any loads from the SQEs are done at this point, | |
6440 | * since once we write the new head, the application could | |
6441 | * write new data to them. | |
6442 | */ | |
6443 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6444 | } |
6445 | ||
2b188cc1 | 6446 | /* |
3529d8c2 | 6447 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6448 | * that is mapped by userspace. This means that care needs to be taken to |
6449 | * ensure that reads are stable, as we cannot rely on userspace always | |
6450 | * being a good citizen. If members of the sqe are validated and then later | |
6451 | * used, it's important that those reads are done through READ_ONCE() to | |
6452 | * prevent a re-load down the line. | |
6453 | */ | |
709b302f | 6454 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6455 | { |
75b28aff | 6456 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6457 | unsigned head; |
6458 | ||
6459 | /* | |
6460 | * The cached sq head (or cq tail) serves two purposes: | |
6461 | * | |
6462 | * 1) allows us to batch the cost of updating the user visible | |
6463 | * head updates. | |
6464 | * 2) allows the kernel side to track the head on its own, even | |
6465 | * though the application is the one updating it. | |
6466 | */ | |
ee7d46d9 | 6467 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6468 | if (likely(head < ctx->sq_entries)) |
6469 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6470 | |
6471 | /* drop invalid entries */ | |
498ccd9e | 6472 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6473 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6474 | return NULL; |
6475 | } | |
6476 | ||
6477 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6478 | { | |
6479 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6480 | } |
6481 | ||
21b55dbc SG |
6482 | /* |
6483 | * Check SQE restrictions (opcode and flags). | |
6484 | * | |
6485 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6486 | */ | |
6487 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6488 | struct io_kiocb *req, | |
6489 | unsigned int sqe_flags) | |
6490 | { | |
6491 | if (!ctx->restricted) | |
6492 | return true; | |
6493 | ||
6494 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6495 | return false; | |
6496 | ||
6497 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6498 | ctx->restrictions.sqe_flags_required) | |
6499 | return false; | |
6500 | ||
6501 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6502 | ctx->restrictions.sqe_flags_required)) | |
6503 | return false; | |
6504 | ||
6505 | return true; | |
6506 | } | |
6507 | ||
ef4ff581 PB |
6508 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6509 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6510 | IOSQE_BUFFER_SELECT) | |
6511 | ||
6512 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6513 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6514 | struct io_submit_state *state) |
0553b8bd | 6515 | { |
ef4ff581 | 6516 | unsigned int sqe_flags; |
71b547c0 | 6517 | int id, ret; |
ef4ff581 | 6518 | |
0553b8bd PB |
6519 | req->opcode = READ_ONCE(sqe->opcode); |
6520 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6521 | req->async_data = NULL; |
0553b8bd PB |
6522 | req->file = NULL; |
6523 | req->ctx = ctx; | |
6524 | req->flags = 0; | |
6525 | /* one is dropped after submission, the other at completion */ | |
6526 | refcount_set(&req->refs, 2); | |
4dd2824d | 6527 | req->task = current; |
0553b8bd | 6528 | req->result = 0; |
ef4ff581 PB |
6529 | |
6530 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6531 | return -EINVAL; | |
6532 | ||
9d8426a0 JA |
6533 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
6534 | return -EFAULT; | |
ef4ff581 PB |
6535 | |
6536 | sqe_flags = READ_ONCE(sqe->flags); | |
6537 | /* enforce forwards compatibility on users */ | |
6538 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6539 | return -EINVAL; | |
6540 | ||
21b55dbc SG |
6541 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6542 | return -EACCES; | |
6543 | ||
ef4ff581 PB |
6544 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6545 | !io_op_defs[req->opcode].buffer_select) | |
6546 | return -EOPNOTSUPP; | |
6547 | ||
6548 | id = READ_ONCE(sqe->personality); | |
6549 | if (id) { | |
1e6fa521 JA |
6550 | struct io_identity *iod; |
6551 | ||
1e6fa521 JA |
6552 | iod = idr_find(&ctx->personality_idr, id); |
6553 | if (unlikely(!iod)) | |
ef4ff581 | 6554 | return -EINVAL; |
1e6fa521 | 6555 | refcount_inc(&iod->count); |
ec99ca6c PB |
6556 | |
6557 | __io_req_init_async(req); | |
1e6fa521 JA |
6558 | get_cred(iod->creds); |
6559 | req->work.identity = iod; | |
dfead8a8 | 6560 | req->work.flags |= IO_WQ_WORK_CREDS; |
ef4ff581 PB |
6561 | } |
6562 | ||
6563 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6564 | req->flags |= sqe_flags; |
ef4ff581 | 6565 | |
63ff8223 JA |
6566 | if (!io_op_defs[req->opcode].needs_file) |
6567 | return 0; | |
6568 | ||
71b547c0 PB |
6569 | ret = io_req_set_file(state, req, READ_ONCE(sqe->fd)); |
6570 | state->ios_left--; | |
6571 | return ret; | |
0553b8bd PB |
6572 | } |
6573 | ||
0f212204 | 6574 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6575 | { |
ac8691c4 | 6576 | struct io_submit_state state; |
9e645e11 | 6577 | struct io_kiocb *link = NULL; |
9e645e11 | 6578 | int i, submitted = 0; |
6c271ce2 | 6579 | |
c4a2ed72 | 6580 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 JA |
6581 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6582 | if (!list_empty(&ctx->cq_overflow_list) && | |
e6c8aa9a | 6583 | !io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6584 | return -EBUSY; |
6585 | } | |
6c271ce2 | 6586 | |
ee7d46d9 PB |
6587 | /* make sure SQ entry isn't read before tail */ |
6588 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6589 | |
2b85edfc PB |
6590 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6591 | return -EAGAIN; | |
6c271ce2 | 6592 | |
d8a6df10 | 6593 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6594 | refcount_add(nr, ¤t->usage); |
6c271ce2 | 6595 | |
013538bd | 6596 | io_submit_state_start(&state, ctx, nr); |
b14cca0c | 6597 | |
6c271ce2 | 6598 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6599 | const struct io_uring_sqe *sqe; |
196be95c | 6600 | struct io_kiocb *req; |
1cb1edb2 | 6601 | int err; |
fb5ccc98 | 6602 | |
b1e50e54 PB |
6603 | sqe = io_get_sqe(ctx); |
6604 | if (unlikely(!sqe)) { | |
6605 | io_consume_sqe(ctx); | |
6606 | break; | |
6607 | } | |
ac8691c4 | 6608 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6609 | if (unlikely(!req)) { |
6610 | if (!submitted) | |
6611 | submitted = -EAGAIN; | |
fb5ccc98 | 6612 | break; |
196be95c | 6613 | } |
709b302f | 6614 | io_consume_sqe(ctx); |
d3656344 JA |
6615 | /* will complete beyond this point, count as submitted */ |
6616 | submitted++; | |
6617 | ||
692d8363 | 6618 | err = io_init_req(ctx, req, sqe, &state); |
ef4ff581 | 6619 | if (unlikely(err)) { |
1cb1edb2 | 6620 | fail_req: |
e1e16097 JA |
6621 | io_put_req(req); |
6622 | io_req_complete(req, err); | |
196be95c PB |
6623 | break; |
6624 | } | |
fb5ccc98 | 6625 | |
354420f7 | 6626 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
0cdaf760 | 6627 | true, io_async_submit(ctx)); |
f13fad7b | 6628 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6629 | if (err) |
6630 | goto fail_req; | |
6c271ce2 JA |
6631 | } |
6632 | ||
9466f437 PB |
6633 | if (unlikely(submitted != nr)) { |
6634 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6635 | struct io_uring_task *tctx = current->io_uring; |
6636 | int unused = nr - ref_used; | |
9466f437 | 6637 | |
d8a6df10 JA |
6638 | percpu_ref_put_many(&ctx->refs, unused); |
6639 | percpu_counter_sub(&tctx->inflight, unused); | |
6640 | put_task_struct_many(current, unused); | |
9466f437 | 6641 | } |
9e645e11 | 6642 | if (link) |
f13fad7b | 6643 | io_queue_link_head(link, &state.comp); |
ac8691c4 | 6644 | io_submit_state_end(&state); |
6c271ce2 | 6645 | |
ae9428ca PB |
6646 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6647 | io_commit_sqring(ctx); | |
6648 | ||
6c271ce2 JA |
6649 | return submitted; |
6650 | } | |
6651 | ||
23b3628e XW |
6652 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6653 | { | |
6654 | /* Tell userspace we may need a wakeup call */ | |
6655 | spin_lock_irq(&ctx->completion_lock); | |
6656 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6657 | spin_unlock_irq(&ctx->completion_lock); | |
6658 | } | |
6659 | ||
6660 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6661 | { | |
6662 | spin_lock_irq(&ctx->completion_lock); | |
6663 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6664 | spin_unlock_irq(&ctx->completion_lock); | |
6665 | } | |
6666 | ||
3f0e64d0 JA |
6667 | static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode, |
6668 | int sync, void *key) | |
6c271ce2 | 6669 | { |
3f0e64d0 JA |
6670 | struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry); |
6671 | int ret; | |
6672 | ||
6673 | ret = autoremove_wake_function(wqe, mode, sync, key); | |
6674 | if (ret) { | |
6675 | unsigned long flags; | |
6676 | ||
6677 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
6678 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6679 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6680 | } | |
6681 | return ret; | |
6682 | } | |
6683 | ||
c8d1ba58 JA |
6684 | enum sq_ret { |
6685 | SQT_IDLE = 1, | |
6686 | SQT_SPIN = 2, | |
6687 | SQT_DID_WORK = 4, | |
6688 | }; | |
6689 | ||
6690 | static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx, | |
e95eee2d | 6691 | unsigned long start_jiffies, bool cap_entries) |
6c271ce2 | 6692 | { |
c8d1ba58 | 6693 | unsigned long timeout = start_jiffies + ctx->sq_thread_idle; |
534ca6d6 | 6694 | struct io_sq_data *sqd = ctx->sq_data; |
c8d1ba58 | 6695 | unsigned int to_submit; |
bdcd3eab | 6696 | int ret = 0; |
6c271ce2 | 6697 | |
c8d1ba58 JA |
6698 | again: |
6699 | if (!list_empty(&ctx->iopoll_list)) { | |
6700 | unsigned nr_events = 0; | |
a4c0b3de | 6701 | |
c8d1ba58 JA |
6702 | mutex_lock(&ctx->uring_lock); |
6703 | if (!list_empty(&ctx->iopoll_list) && !need_resched()) | |
6704 | io_do_iopoll(ctx, &nr_events, 0); | |
6705 | mutex_unlock(&ctx->uring_lock); | |
6706 | } | |
6c271ce2 | 6707 | |
c8d1ba58 | 6708 | to_submit = io_sqring_entries(ctx); |
6c271ce2 | 6709 | |
c8d1ba58 JA |
6710 | /* |
6711 | * If submit got -EBUSY, flag us as needing the application | |
6712 | * to enter the kernel to reap and flush events. | |
6713 | */ | |
6714 | if (!to_submit || ret == -EBUSY || need_resched()) { | |
6715 | /* | |
6716 | * Drop cur_mm before scheduling, we can't hold it for | |
6717 | * long periods (or over schedule()). Do this before | |
6718 | * adding ourselves to the waitqueue, as the unuse/drop | |
6719 | * may sleep. | |
6720 | */ | |
6721 | io_sq_thread_drop_mm(); | |
6c271ce2 | 6722 | |
c8d1ba58 JA |
6723 | /* |
6724 | * We're polling. If we're within the defined idle | |
6725 | * period, then let us spin without work before going | |
6726 | * to sleep. The exception is if we got EBUSY doing | |
6727 | * more IO, we should wait for the application to | |
6728 | * reap events and wake us up. | |
6729 | */ | |
6730 | if (!list_empty(&ctx->iopoll_list) || need_resched() || | |
6731 | (!time_after(jiffies, timeout) && ret != -EBUSY && | |
6732 | !percpu_ref_is_dying(&ctx->refs))) | |
6733 | return SQT_SPIN; | |
6c271ce2 | 6734 | |
534ca6d6 | 6735 | prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry, |
c8d1ba58 | 6736 | TASK_INTERRUPTIBLE); |
6c271ce2 | 6737 | |
c8d1ba58 JA |
6738 | /* |
6739 | * While doing polled IO, before going to sleep, we need | |
6740 | * to check if there are new reqs added to iopoll_list, | |
6741 | * it is because reqs may have been punted to io worker | |
6742 | * and will be added to iopoll_list later, hence check | |
6743 | * the iopoll_list again. | |
6744 | */ | |
6745 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6746 | !list_empty_careful(&ctx->iopoll_list)) { | |
534ca6d6 | 6747 | finish_wait(&sqd->wait, &ctx->sqo_wait_entry); |
c8d1ba58 | 6748 | goto again; |
6c271ce2 JA |
6749 | } |
6750 | ||
fb5ccc98 | 6751 | to_submit = io_sqring_entries(ctx); |
c8d1ba58 JA |
6752 | if (!to_submit || ret == -EBUSY) |
6753 | return SQT_IDLE; | |
6754 | } | |
c1edbf5f | 6755 | |
534ca6d6 | 6756 | finish_wait(&sqd->wait, &ctx->sqo_wait_entry); |
c8d1ba58 | 6757 | io_ring_clear_wakeup_flag(ctx); |
7143b5ac | 6758 | |
e95eee2d JA |
6759 | /* if we're handling multiple rings, cap submit size for fairness */ |
6760 | if (cap_entries && to_submit > 8) | |
6761 | to_submit = 8; | |
6762 | ||
c8d1ba58 JA |
6763 | mutex_lock(&ctx->uring_lock); |
6764 | if (likely(!percpu_ref_is_dying(&ctx->refs))) | |
6765 | ret = io_submit_sqes(ctx, to_submit); | |
6766 | mutex_unlock(&ctx->uring_lock); | |
90554200 JA |
6767 | |
6768 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) | |
6769 | wake_up(&ctx->sqo_sq_wait); | |
6770 | ||
c8d1ba58 JA |
6771 | return SQT_DID_WORK; |
6772 | } | |
6c271ce2 | 6773 | |
69fb2131 JA |
6774 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6775 | { | |
6776 | struct io_ring_ctx *ctx; | |
6777 | ||
6778 | while (!list_empty(&sqd->ctx_new_list)) { | |
6779 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
6780 | init_wait(&ctx->sqo_wait_entry); | |
6781 | ctx->sqo_wait_entry.func = io_sq_wake_function; | |
6782 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); | |
6783 | complete(&ctx->sq_thread_comp); | |
6784 | } | |
6785 | } | |
6786 | ||
c8d1ba58 JA |
6787 | static int io_sq_thread(void *data) |
6788 | { | |
91d8f519 | 6789 | struct cgroup_subsys_state *cur_css = NULL; |
69fb2131 JA |
6790 | const struct cred *old_cred = NULL; |
6791 | struct io_sq_data *sqd = data; | |
6792 | struct io_ring_ctx *ctx; | |
c8d1ba58 | 6793 | unsigned long start_jiffies; |
6c271ce2 | 6794 | |
69fb2131 JA |
6795 | start_jiffies = jiffies; |
6796 | while (!kthread_should_stop()) { | |
6797 | enum sq_ret ret = 0; | |
e95eee2d | 6798 | bool cap_entries; |
c1edbf5f JA |
6799 | |
6800 | /* | |
69fb2131 JA |
6801 | * Any changes to the sqd lists are synchronized through the |
6802 | * kthread parking. This synchronizes the thread vs users, | |
6803 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 6804 | */ |
21e00d8d | 6805 | if (kthread_should_park()) { |
69fb2131 | 6806 | kthread_parkme(); |
21e00d8d XW |
6807 | /* |
6808 | * When sq thread is unparked, in case the previous park operation | |
6809 | * comes from io_put_sq_data(), which means that sq thread is going | |
6810 | * to be stopped, so here needs to have a check. | |
6811 | */ | |
6812 | if (kthread_should_stop()) | |
6813 | break; | |
6814 | } | |
7143b5ac | 6815 | |
69fb2131 JA |
6816 | if (unlikely(!list_empty(&sqd->ctx_new_list))) |
6817 | io_sqd_init_new(sqd); | |
6c271ce2 | 6818 | |
e95eee2d | 6819 | cap_entries = !list_is_singular(&sqd->ctx_list); |
6c271ce2 | 6820 | |
69fb2131 JA |
6821 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6822 | if (current->cred != ctx->creds) { | |
6823 | if (old_cred) | |
6824 | revert_creds(old_cred); | |
6825 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 6826 | } |
91d8f519 | 6827 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
6828 | #ifdef CONFIG_AUDIT |
6829 | current->loginuid = ctx->loginuid; | |
6830 | current->sessionid = ctx->sessionid; | |
6831 | #endif | |
bdcd3eab | 6832 | |
e95eee2d | 6833 | ret |= __io_sq_thread(ctx, start_jiffies, cap_entries); |
6c271ce2 | 6834 | |
69fb2131 JA |
6835 | io_sq_thread_drop_mm(); |
6836 | } | |
6c271ce2 | 6837 | |
69fb2131 | 6838 | if (ret & SQT_SPIN) { |
c8d1ba58 JA |
6839 | io_run_task_work(); |
6840 | cond_resched(); | |
69fb2131 JA |
6841 | } else if (ret == SQT_IDLE) { |
6842 | if (kthread_should_park()) | |
6c271ce2 | 6843 | continue; |
69fb2131 JA |
6844 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6845 | io_ring_set_wakeup_flag(ctx); | |
6846 | schedule(); | |
6847 | start_jiffies = jiffies; | |
6848 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
6849 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6850 | } |
6c271ce2 JA |
6851 | } |
6852 | ||
4c6e277c | 6853 | io_run_task_work(); |
b41e9852 | 6854 | |
91d8f519 DZ |
6855 | if (cur_css) |
6856 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
6857 | if (old_cred) |
6858 | revert_creds(old_cred); | |
06058632 | 6859 | |
2bbcd6d3 | 6860 | kthread_parkme(); |
06058632 | 6861 | |
6c271ce2 JA |
6862 | return 0; |
6863 | } | |
6864 | ||
bda52162 JA |
6865 | struct io_wait_queue { |
6866 | struct wait_queue_entry wq; | |
6867 | struct io_ring_ctx *ctx; | |
6868 | unsigned to_wait; | |
6869 | unsigned nr_timeouts; | |
6870 | }; | |
6871 | ||
1d7bb1d5 | 6872 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
6873 | { |
6874 | struct io_ring_ctx *ctx = iowq->ctx; | |
6875 | ||
6876 | /* | |
d195a66e | 6877 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6878 | * started waiting. For timeouts, we always want to return to userspace, |
6879 | * regardless of event count. | |
6880 | */ | |
1d7bb1d5 | 6881 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
6882 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6883 | } | |
6884 | ||
6885 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6886 | int wake_flags, void *key) | |
6887 | { | |
6888 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6889 | wq); | |
6890 | ||
1d7bb1d5 JA |
6891 | /* use noflush == true, as we can't safely rely on locking context */ |
6892 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
6893 | return -1; |
6894 | ||
6895 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6896 | } | |
6897 | ||
af9c1a44 JA |
6898 | static int io_run_task_work_sig(void) |
6899 | { | |
6900 | if (io_run_task_work()) | |
6901 | return 1; | |
6902 | if (!signal_pending(current)) | |
6903 | return 0; | |
6904 | if (current->jobctl & JOBCTL_TASK_WORK) { | |
6905 | spin_lock_irq(¤t->sighand->siglock); | |
6906 | current->jobctl &= ~JOBCTL_TASK_WORK; | |
6907 | recalc_sigpending(); | |
6908 | spin_unlock_irq(¤t->sighand->siglock); | |
6909 | return 1; | |
6910 | } | |
6911 | return -EINTR; | |
6912 | } | |
6913 | ||
2b188cc1 JA |
6914 | /* |
6915 | * Wait until events become available, if we don't already have some. The | |
6916 | * application must reap them itself, as they reside on the shared cq ring. | |
6917 | */ | |
6918 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
6919 | const sigset_t __user *sig, size_t sigsz) | |
6920 | { | |
bda52162 JA |
6921 | struct io_wait_queue iowq = { |
6922 | .wq = { | |
6923 | .private = current, | |
6924 | .func = io_wake_function, | |
6925 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6926 | }, | |
6927 | .ctx = ctx, | |
6928 | .to_wait = min_events, | |
6929 | }; | |
75b28aff | 6930 | struct io_rings *rings = ctx->rings; |
e9ffa5c2 | 6931 | int ret = 0; |
2b188cc1 | 6932 | |
b41e9852 JA |
6933 | do { |
6934 | if (io_cqring_events(ctx, false) >= min_events) | |
6935 | return 0; | |
4c6e277c | 6936 | if (!io_run_task_work()) |
b41e9852 | 6937 | break; |
b41e9852 | 6938 | } while (1); |
2b188cc1 JA |
6939 | |
6940 | if (sig) { | |
9e75ad5d AB |
6941 | #ifdef CONFIG_COMPAT |
6942 | if (in_compat_syscall()) | |
6943 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6944 | sigsz); |
9e75ad5d AB |
6945 | else |
6946 | #endif | |
b772434b | 6947 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6948 | |
2b188cc1 JA |
6949 | if (ret) |
6950 | return ret; | |
6951 | } | |
6952 | ||
bda52162 | 6953 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6954 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
6955 | do { |
6956 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
6957 | TASK_INTERRUPTIBLE); | |
ce593a6c | 6958 | /* make sure we run task_work before checking for signals */ |
af9c1a44 JA |
6959 | ret = io_run_task_work_sig(); |
6960 | if (ret > 0) | |
4c6e277c | 6961 | continue; |
af9c1a44 | 6962 | else if (ret < 0) |
bda52162 | 6963 | break; |
ce593a6c JA |
6964 | if (io_should_wake(&iowq, false)) |
6965 | break; | |
6966 | schedule(); | |
bda52162 JA |
6967 | } while (1); |
6968 | finish_wait(&ctx->wait, &iowq.wq); | |
6969 | ||
b7db41c9 | 6970 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6971 | |
75b28aff | 6972 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6973 | } |
6974 | ||
6b06314c JA |
6975 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6976 | { | |
6977 | #if defined(CONFIG_UNIX) | |
6978 | if (ctx->ring_sock) { | |
6979 | struct sock *sock = ctx->ring_sock->sk; | |
6980 | struct sk_buff *skb; | |
6981 | ||
6982 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6983 | kfree_skb(skb); | |
6984 | } | |
6985 | #else | |
6986 | int i; | |
6987 | ||
65e19f54 JA |
6988 | for (i = 0; i < ctx->nr_user_files; i++) { |
6989 | struct file *file; | |
6990 | ||
6991 | file = io_file_from_index(ctx, i); | |
6992 | if (file) | |
6993 | fput(file); | |
6994 | } | |
6b06314c JA |
6995 | #endif |
6996 | } | |
6997 | ||
05f3fb3c JA |
6998 | static void io_file_ref_kill(struct percpu_ref *ref) |
6999 | { | |
7000 | struct fixed_file_data *data; | |
7001 | ||
7002 | data = container_of(ref, struct fixed_file_data, refs); | |
7003 | complete(&data->done); | |
7004 | } | |
7005 | ||
e0d59d3c PB |
7006 | static void io_sqe_files_set_node(struct fixed_file_data *file_data, |
7007 | struct fixed_file_ref_node *ref_node) | |
7008 | { | |
7009 | spin_lock_bh(&file_data->lock); | |
7010 | file_data->node = ref_node; | |
7011 | list_add_tail(&ref_node->node, &file_data->ref_list); | |
7012 | spin_unlock_bh(&file_data->lock); | |
7013 | percpu_ref_get(&file_data->refs); | |
7014 | } | |
7015 | ||
6b06314c JA |
7016 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7017 | { | |
05f3fb3c | 7018 | struct fixed_file_data *data = ctx->file_data; |
34fb0062 | 7019 | struct fixed_file_ref_node *backup_node, *ref_node = NULL; |
65e19f54 | 7020 | unsigned nr_tables, i; |
34fb0062 | 7021 | int ret; |
65e19f54 | 7022 | |
05f3fb3c | 7023 | if (!data) |
6b06314c | 7024 | return -ENXIO; |
34fb0062 PB |
7025 | backup_node = alloc_fixed_file_ref_node(ctx); |
7026 | if (!backup_node) | |
7027 | return -ENOMEM; | |
6b06314c | 7028 | |
8522fdc5 | 7029 | spin_lock_bh(&data->lock); |
1e5d770b | 7030 | ref_node = data->node; |
8522fdc5 | 7031 | spin_unlock_bh(&data->lock); |
05589553 XW |
7032 | if (ref_node) |
7033 | percpu_ref_kill(&ref_node->refs); | |
7034 | ||
7035 | percpu_ref_kill(&data->refs); | |
7036 | ||
7037 | /* wait for all refs nodes to complete */ | |
4a38aed2 | 7038 | flush_delayed_work(&ctx->file_put_work); |
34fb0062 PB |
7039 | do { |
7040 | ret = wait_for_completion_interruptible(&data->done); | |
7041 | if (!ret) | |
7042 | break; | |
7043 | ret = io_run_task_work_sig(); | |
7044 | if (ret < 0) { | |
7045 | percpu_ref_resurrect(&data->refs); | |
7046 | reinit_completion(&data->done); | |
7047 | io_sqe_files_set_node(data, backup_node); | |
7048 | return ret; | |
7049 | } | |
7050 | } while (1); | |
05f3fb3c | 7051 | |
6b06314c | 7052 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7053 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7054 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
7055 | kfree(data->table[i].files); |
7056 | kfree(data->table); | |
05589553 XW |
7057 | percpu_ref_exit(&data->refs); |
7058 | kfree(data); | |
05f3fb3c | 7059 | ctx->file_data = NULL; |
6b06314c | 7060 | ctx->nr_user_files = 0; |
34fb0062 | 7061 | destroy_fixed_file_ref_node(backup_node); |
6b06314c JA |
7062 | return 0; |
7063 | } | |
7064 | ||
534ca6d6 | 7065 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7066 | { |
534ca6d6 | 7067 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7068 | /* |
7069 | * The park is a bit of a work-around, without it we get | |
7070 | * warning spews on shutdown with SQPOLL set and affinity | |
7071 | * set to a single CPU. | |
7072 | */ | |
534ca6d6 JA |
7073 | if (sqd->thread) { |
7074 | kthread_park(sqd->thread); | |
7075 | kthread_stop(sqd->thread); | |
7076 | } | |
7077 | ||
7078 | kfree(sqd); | |
7079 | } | |
7080 | } | |
7081 | ||
aa06165d JA |
7082 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7083 | { | |
7084 | struct io_ring_ctx *ctx_attach; | |
7085 | struct io_sq_data *sqd; | |
7086 | struct fd f; | |
7087 | ||
7088 | f = fdget(p->wq_fd); | |
7089 | if (!f.file) | |
7090 | return ERR_PTR(-ENXIO); | |
7091 | if (f.file->f_op != &io_uring_fops) { | |
7092 | fdput(f); | |
7093 | return ERR_PTR(-EINVAL); | |
7094 | } | |
7095 | ||
7096 | ctx_attach = f.file->private_data; | |
7097 | sqd = ctx_attach->sq_data; | |
7098 | if (!sqd) { | |
7099 | fdput(f); | |
7100 | return ERR_PTR(-EINVAL); | |
7101 | } | |
7102 | ||
7103 | refcount_inc(&sqd->refs); | |
7104 | fdput(f); | |
7105 | return sqd; | |
7106 | } | |
7107 | ||
534ca6d6 JA |
7108 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7109 | { | |
7110 | struct io_sq_data *sqd; | |
7111 | ||
aa06165d JA |
7112 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7113 | return io_attach_sq_data(p); | |
7114 | ||
534ca6d6 JA |
7115 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7116 | if (!sqd) | |
7117 | return ERR_PTR(-ENOMEM); | |
7118 | ||
7119 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7120 | INIT_LIST_HEAD(&sqd->ctx_list); |
7121 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7122 | mutex_init(&sqd->ctx_lock); | |
7123 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7124 | init_waitqueue_head(&sqd->wait); |
7125 | return sqd; | |
7126 | } | |
7127 | ||
69fb2131 JA |
7128 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7129 | __releases(&sqd->lock) | |
7130 | { | |
7131 | if (!sqd->thread) | |
7132 | return; | |
7133 | kthread_unpark(sqd->thread); | |
7134 | mutex_unlock(&sqd->lock); | |
7135 | } | |
7136 | ||
7137 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7138 | __acquires(&sqd->lock) | |
7139 | { | |
7140 | if (!sqd->thread) | |
7141 | return; | |
7142 | mutex_lock(&sqd->lock); | |
7143 | kthread_park(sqd->thread); | |
7144 | } | |
7145 | ||
534ca6d6 JA |
7146 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7147 | { | |
7148 | struct io_sq_data *sqd = ctx->sq_data; | |
7149 | ||
7150 | if (sqd) { | |
7151 | if (sqd->thread) { | |
7152 | /* | |
7153 | * We may arrive here from the error branch in | |
7154 | * io_sq_offload_create() where the kthread is created | |
7155 | * without being waked up, thus wake it up now to make | |
7156 | * sure the wait will complete. | |
7157 | */ | |
7158 | wake_up_process(sqd->thread); | |
7159 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7160 | |
7161 | io_sq_thread_park(sqd); | |
7162 | } | |
7163 | ||
7164 | mutex_lock(&sqd->ctx_lock); | |
7165 | list_del(&ctx->sqd_list); | |
7166 | mutex_unlock(&sqd->ctx_lock); | |
7167 | ||
7168 | if (sqd->thread) { | |
7169 | finish_wait(&sqd->wait, &ctx->sqo_wait_entry); | |
7170 | io_sq_thread_unpark(sqd); | |
534ca6d6 JA |
7171 | } |
7172 | ||
7173 | io_put_sq_data(sqd); | |
7174 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7175 | } |
7176 | } | |
7177 | ||
6b06314c JA |
7178 | static void io_finish_async(struct io_ring_ctx *ctx) |
7179 | { | |
6c271ce2 JA |
7180 | io_sq_thread_stop(ctx); |
7181 | ||
561fb04a JA |
7182 | if (ctx->io_wq) { |
7183 | io_wq_destroy(ctx->io_wq); | |
7184 | ctx->io_wq = NULL; | |
6b06314c JA |
7185 | } |
7186 | } | |
7187 | ||
7188 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7189 | /* |
7190 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7191 | * the io_uring can be safely unregistered on process exit, even if we have | |
7192 | * loops in the file referencing. | |
7193 | */ | |
7194 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7195 | { | |
7196 | struct sock *sk = ctx->ring_sock->sk; | |
7197 | struct scm_fp_list *fpl; | |
7198 | struct sk_buff *skb; | |
08a45173 | 7199 | int i, nr_files; |
6b06314c | 7200 | |
6b06314c JA |
7201 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7202 | if (!fpl) | |
7203 | return -ENOMEM; | |
7204 | ||
7205 | skb = alloc_skb(0, GFP_KERNEL); | |
7206 | if (!skb) { | |
7207 | kfree(fpl); | |
7208 | return -ENOMEM; | |
7209 | } | |
7210 | ||
7211 | skb->sk = sk; | |
6b06314c | 7212 | |
08a45173 | 7213 | nr_files = 0; |
6b06314c JA |
7214 | fpl->user = get_uid(ctx->user); |
7215 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7216 | struct file *file = io_file_from_index(ctx, i + offset); |
7217 | ||
7218 | if (!file) | |
08a45173 | 7219 | continue; |
65e19f54 | 7220 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7221 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7222 | nr_files++; | |
6b06314c JA |
7223 | } |
7224 | ||
08a45173 JA |
7225 | if (nr_files) { |
7226 | fpl->max = SCM_MAX_FD; | |
7227 | fpl->count = nr_files; | |
7228 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7229 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7230 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7231 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7232 | |
08a45173 JA |
7233 | for (i = 0; i < nr_files; i++) |
7234 | fput(fpl->fp[i]); | |
7235 | } else { | |
7236 | kfree_skb(skb); | |
7237 | kfree(fpl); | |
7238 | } | |
6b06314c JA |
7239 | |
7240 | return 0; | |
7241 | } | |
7242 | ||
7243 | /* | |
7244 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7245 | * causes regular reference counting to break down. We rely on the UNIX | |
7246 | * garbage collection to take care of this problem for us. | |
7247 | */ | |
7248 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7249 | { | |
7250 | unsigned left, total; | |
7251 | int ret = 0; | |
7252 | ||
7253 | total = 0; | |
7254 | left = ctx->nr_user_files; | |
7255 | while (left) { | |
7256 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7257 | |
7258 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7259 | if (ret) | |
7260 | break; | |
7261 | left -= this_files; | |
7262 | total += this_files; | |
7263 | } | |
7264 | ||
7265 | if (!ret) | |
7266 | return 0; | |
7267 | ||
7268 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7269 | struct file *file = io_file_from_index(ctx, total); |
7270 | ||
7271 | if (file) | |
7272 | fput(file); | |
6b06314c JA |
7273 | total++; |
7274 | } | |
7275 | ||
7276 | return ret; | |
7277 | } | |
7278 | #else | |
7279 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7280 | { | |
7281 | return 0; | |
7282 | } | |
7283 | #endif | |
7284 | ||
5398ae69 PB |
7285 | static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data, |
7286 | unsigned nr_tables, unsigned nr_files) | |
65e19f54 JA |
7287 | { |
7288 | int i; | |
7289 | ||
7290 | for (i = 0; i < nr_tables; i++) { | |
5398ae69 | 7291 | struct fixed_file_table *table = &file_data->table[i]; |
65e19f54 JA |
7292 | unsigned this_files; |
7293 | ||
7294 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7295 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7296 | GFP_KERNEL); | |
7297 | if (!table->files) | |
7298 | break; | |
7299 | nr_files -= this_files; | |
7300 | } | |
7301 | ||
7302 | if (i == nr_tables) | |
7303 | return 0; | |
7304 | ||
7305 | for (i = 0; i < nr_tables; i++) { | |
5398ae69 | 7306 | struct fixed_file_table *table = &file_data->table[i]; |
65e19f54 JA |
7307 | kfree(table->files); |
7308 | } | |
7309 | return 1; | |
7310 | } | |
7311 | ||
05f3fb3c JA |
7312 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
7313 | { | |
7314 | #if defined(CONFIG_UNIX) | |
7315 | struct sock *sock = ctx->ring_sock->sk; | |
7316 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7317 | struct sk_buff *skb; | |
7318 | int i; | |
7319 | ||
7320 | __skb_queue_head_init(&list); | |
7321 | ||
7322 | /* | |
7323 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7324 | * remove this entry and rearrange the file array. | |
7325 | */ | |
7326 | skb = skb_dequeue(head); | |
7327 | while (skb) { | |
7328 | struct scm_fp_list *fp; | |
7329 | ||
7330 | fp = UNIXCB(skb).fp; | |
7331 | for (i = 0; i < fp->count; i++) { | |
7332 | int left; | |
7333 | ||
7334 | if (fp->fp[i] != file) | |
7335 | continue; | |
7336 | ||
7337 | unix_notinflight(fp->user, fp->fp[i]); | |
7338 | left = fp->count - 1 - i; | |
7339 | if (left) { | |
7340 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7341 | left * sizeof(struct file *)); | |
7342 | } | |
7343 | fp->count--; | |
7344 | if (!fp->count) { | |
7345 | kfree_skb(skb); | |
7346 | skb = NULL; | |
7347 | } else { | |
7348 | __skb_queue_tail(&list, skb); | |
7349 | } | |
7350 | fput(file); | |
7351 | file = NULL; | |
7352 | break; | |
7353 | } | |
7354 | ||
7355 | if (!file) | |
7356 | break; | |
7357 | ||
7358 | __skb_queue_tail(&list, skb); | |
7359 | ||
7360 | skb = skb_dequeue(head); | |
7361 | } | |
7362 | ||
7363 | if (skb_peek(&list)) { | |
7364 | spin_lock_irq(&head->lock); | |
7365 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7366 | __skb_queue_tail(head, skb); | |
7367 | spin_unlock_irq(&head->lock); | |
7368 | } | |
7369 | #else | |
7370 | fput(file); | |
7371 | #endif | |
7372 | } | |
7373 | ||
7374 | struct io_file_put { | |
05589553 | 7375 | struct list_head list; |
05f3fb3c | 7376 | struct file *file; |
05f3fb3c JA |
7377 | }; |
7378 | ||
4a38aed2 | 7379 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
65e19f54 | 7380 | { |
4a38aed2 JA |
7381 | struct fixed_file_data *file_data = ref_node->file_data; |
7382 | struct io_ring_ctx *ctx = file_data->ctx; | |
05f3fb3c | 7383 | struct io_file_put *pfile, *tmp; |
05589553 XW |
7384 | |
7385 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { | |
6a4d07cd | 7386 | list_del(&pfile->list); |
05589553 XW |
7387 | io_ring_file_put(ctx, pfile->file); |
7388 | kfree(pfile); | |
65e19f54 | 7389 | } |
05589553 | 7390 | |
05589553 XW |
7391 | percpu_ref_exit(&ref_node->refs); |
7392 | kfree(ref_node); | |
7393 | percpu_ref_put(&file_data->refs); | |
2faf852d | 7394 | } |
65e19f54 | 7395 | |
4a38aed2 JA |
7396 | static void io_file_put_work(struct work_struct *work) |
7397 | { | |
7398 | struct io_ring_ctx *ctx; | |
7399 | struct llist_node *node; | |
7400 | ||
7401 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); | |
7402 | node = llist_del_all(&ctx->file_put_llist); | |
7403 | ||
7404 | while (node) { | |
7405 | struct fixed_file_ref_node *ref_node; | |
7406 | struct llist_node *next = node->next; | |
7407 | ||
7408 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); | |
7409 | __io_file_put_work(ref_node); | |
7410 | node = next; | |
7411 | } | |
7412 | } | |
7413 | ||
05589553 | 7414 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
2faf852d | 7415 | { |
05589553 | 7416 | struct fixed_file_ref_node *ref_node; |
e297822b | 7417 | struct fixed_file_data *data; |
4a38aed2 | 7418 | struct io_ring_ctx *ctx; |
e297822b | 7419 | bool first_add = false; |
4a38aed2 | 7420 | int delay = HZ; |
65e19f54 | 7421 | |
05589553 | 7422 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
e297822b PB |
7423 | data = ref_node->file_data; |
7424 | ctx = data->ctx; | |
7425 | ||
8522fdc5 | 7426 | spin_lock_bh(&data->lock); |
e297822b PB |
7427 | ref_node->done = true; |
7428 | ||
7429 | while (!list_empty(&data->ref_list)) { | |
7430 | ref_node = list_first_entry(&data->ref_list, | |
7431 | struct fixed_file_ref_node, node); | |
7432 | /* recycle ref nodes in order */ | |
7433 | if (!ref_node->done) | |
7434 | break; | |
7435 | list_del(&ref_node->node); | |
7436 | first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist); | |
7437 | } | |
8522fdc5 | 7438 | spin_unlock_bh(&data->lock); |
05589553 | 7439 | |
e297822b | 7440 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7441 | delay = 0; |
05589553 | 7442 | |
4a38aed2 JA |
7443 | if (!delay) |
7444 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); | |
7445 | else if (first_add) | |
7446 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); | |
05f3fb3c | 7447 | } |
65e19f54 | 7448 | |
05589553 XW |
7449 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
7450 | struct io_ring_ctx *ctx) | |
05f3fb3c | 7451 | { |
05589553 | 7452 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7453 | |
05589553 XW |
7454 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7455 | if (!ref_node) | |
7456 | return ERR_PTR(-ENOMEM); | |
05f3fb3c | 7457 | |
05589553 XW |
7458 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
7459 | 0, GFP_KERNEL)) { | |
7460 | kfree(ref_node); | |
7461 | return ERR_PTR(-ENOMEM); | |
7462 | } | |
7463 | INIT_LIST_HEAD(&ref_node->node); | |
7464 | INIT_LIST_HEAD(&ref_node->file_list); | |
05589553 | 7465 | ref_node->file_data = ctx->file_data; |
e297822b | 7466 | ref_node->done = false; |
05589553 | 7467 | return ref_node; |
05589553 XW |
7468 | } |
7469 | ||
7470 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) | |
7471 | { | |
7472 | percpu_ref_exit(&ref_node->refs); | |
7473 | kfree(ref_node); | |
65e19f54 JA |
7474 | } |
7475 | ||
6b06314c JA |
7476 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7477 | unsigned nr_args) | |
7478 | { | |
7479 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7480 | unsigned nr_tables, i; |
05f3fb3c | 7481 | struct file *file; |
600cf3f8 | 7482 | int fd, ret = -ENOMEM; |
05589553 | 7483 | struct fixed_file_ref_node *ref_node; |
5398ae69 | 7484 | struct fixed_file_data *file_data; |
6b06314c | 7485 | |
05f3fb3c | 7486 | if (ctx->file_data) |
6b06314c JA |
7487 | return -EBUSY; |
7488 | if (!nr_args) | |
7489 | return -EINVAL; | |
7490 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7491 | return -EMFILE; | |
7492 | ||
5398ae69 PB |
7493 | file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
7494 | if (!file_data) | |
05f3fb3c | 7495 | return -ENOMEM; |
5398ae69 PB |
7496 | file_data->ctx = ctx; |
7497 | init_completion(&file_data->done); | |
7498 | INIT_LIST_HEAD(&file_data->ref_list); | |
7499 | spin_lock_init(&file_data->lock); | |
05f3fb3c | 7500 | |
65e19f54 | 7501 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7502 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7503 | GFP_KERNEL); |
600cf3f8 PB |
7504 | if (!file_data->table) |
7505 | goto out_free; | |
05f3fb3c | 7506 | |
5398ae69 | 7507 | if (percpu_ref_init(&file_data->refs, io_file_ref_kill, |
600cf3f8 PB |
7508 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
7509 | goto out_free; | |
6b06314c | 7510 | |
600cf3f8 PB |
7511 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
7512 | goto out_ref; | |
55cbc256 | 7513 | ctx->file_data = file_data; |
65e19f54 | 7514 | |
08a45173 | 7515 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
7516 | struct fixed_file_table *table; |
7517 | unsigned index; | |
7518 | ||
600cf3f8 PB |
7519 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7520 | ret = -EFAULT; | |
7521 | goto out_fput; | |
7522 | } | |
08a45173 | 7523 | /* allow sparse sets */ |
600cf3f8 | 7524 | if (fd == -1) |
08a45173 | 7525 | continue; |
6b06314c | 7526 | |
05f3fb3c | 7527 | file = fget(fd); |
6b06314c | 7528 | ret = -EBADF; |
05f3fb3c | 7529 | if (!file) |
600cf3f8 | 7530 | goto out_fput; |
05f3fb3c | 7531 | |
6b06314c JA |
7532 | /* |
7533 | * Don't allow io_uring instances to be registered. If UNIX | |
7534 | * isn't enabled, then this causes a reference cycle and this | |
7535 | * instance can never get freed. If UNIX is enabled we'll | |
7536 | * handle it just fine, but there's still no point in allowing | |
7537 | * a ring fd as it doesn't support regular read/write anyway. | |
7538 | */ | |
05f3fb3c JA |
7539 | if (file->f_op == &io_uring_fops) { |
7540 | fput(file); | |
600cf3f8 | 7541 | goto out_fput; |
6b06314c | 7542 | } |
600cf3f8 PB |
7543 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
7544 | index = i & IORING_FILE_TABLE_MASK; | |
05f3fb3c | 7545 | table->files[index] = file; |
6b06314c JA |
7546 | } |
7547 | ||
6b06314c | 7548 | ret = io_sqe_files_scm(ctx); |
05589553 | 7549 | if (ret) { |
6b06314c | 7550 | io_sqe_files_unregister(ctx); |
05589553 XW |
7551 | return ret; |
7552 | } | |
6b06314c | 7553 | |
05589553 XW |
7554 | ref_node = alloc_fixed_file_ref_node(ctx); |
7555 | if (IS_ERR(ref_node)) { | |
7556 | io_sqe_files_unregister(ctx); | |
7557 | return PTR_ERR(ref_node); | |
7558 | } | |
7559 | ||
e0d59d3c | 7560 | io_sqe_files_set_node(file_data, ref_node); |
6b06314c | 7561 | return ret; |
600cf3f8 PB |
7562 | out_fput: |
7563 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7564 | file = io_file_from_index(ctx, i); | |
7565 | if (file) | |
7566 | fput(file); | |
7567 | } | |
7568 | for (i = 0; i < nr_tables; i++) | |
7569 | kfree(file_data->table[i].files); | |
7570 | ctx->nr_user_files = 0; | |
7571 | out_ref: | |
7572 | percpu_ref_exit(&file_data->refs); | |
7573 | out_free: | |
7574 | kfree(file_data->table); | |
7575 | kfree(file_data); | |
55cbc256 | 7576 | ctx->file_data = NULL; |
6b06314c JA |
7577 | return ret; |
7578 | } | |
7579 | ||
c3a31e60 JA |
7580 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7581 | int index) | |
7582 | { | |
7583 | #if defined(CONFIG_UNIX) | |
7584 | struct sock *sock = ctx->ring_sock->sk; | |
7585 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7586 | struct sk_buff *skb; | |
7587 | ||
7588 | /* | |
7589 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7590 | * file set. If there's no room, fall back to allocating a new skb | |
7591 | * and filling it in. | |
7592 | */ | |
7593 | spin_lock_irq(&head->lock); | |
7594 | skb = skb_peek(head); | |
7595 | if (skb) { | |
7596 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7597 | ||
7598 | if (fpl->count < SCM_MAX_FD) { | |
7599 | __skb_unlink(skb, head); | |
7600 | spin_unlock_irq(&head->lock); | |
7601 | fpl->fp[fpl->count] = get_file(file); | |
7602 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7603 | fpl->count++; | |
7604 | spin_lock_irq(&head->lock); | |
7605 | __skb_queue_head(head, skb); | |
7606 | } else { | |
7607 | skb = NULL; | |
7608 | } | |
7609 | } | |
7610 | spin_unlock_irq(&head->lock); | |
7611 | ||
7612 | if (skb) { | |
7613 | fput(file); | |
7614 | return 0; | |
7615 | } | |
7616 | ||
7617 | return __io_sqe_files_scm(ctx, 1, index); | |
7618 | #else | |
7619 | return 0; | |
7620 | #endif | |
7621 | } | |
7622 | ||
a5318d3c | 7623 | static int io_queue_file_removal(struct fixed_file_data *data, |
05589553 | 7624 | struct file *file) |
05f3fb3c | 7625 | { |
a5318d3c | 7626 | struct io_file_put *pfile; |
b2e96852 | 7627 | struct fixed_file_ref_node *ref_node = data->node; |
05f3fb3c | 7628 | |
05f3fb3c | 7629 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
a5318d3c HD |
7630 | if (!pfile) |
7631 | return -ENOMEM; | |
05f3fb3c JA |
7632 | |
7633 | pfile->file = file; | |
05589553 XW |
7634 | list_add(&pfile->list, &ref_node->file_list); |
7635 | ||
a5318d3c | 7636 | return 0; |
05f3fb3c JA |
7637 | } |
7638 | ||
7639 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
7640 | struct io_uring_files_update *up, | |
7641 | unsigned nr_args) | |
7642 | { | |
7643 | struct fixed_file_data *data = ctx->file_data; | |
05589553 | 7644 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7645 | struct file *file; |
c3a31e60 JA |
7646 | __s32 __user *fds; |
7647 | int fd, i, err; | |
7648 | __u32 done; | |
05589553 | 7649 | bool needs_switch = false; |
c3a31e60 | 7650 | |
05f3fb3c | 7651 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7652 | return -EOVERFLOW; |
7653 | if (done > ctx->nr_user_files) | |
7654 | return -EINVAL; | |
7655 | ||
05589553 XW |
7656 | ref_node = alloc_fixed_file_ref_node(ctx); |
7657 | if (IS_ERR(ref_node)) | |
7658 | return PTR_ERR(ref_node); | |
7659 | ||
c3a31e60 | 7660 | done = 0; |
05f3fb3c | 7661 | fds = u64_to_user_ptr(up->fds); |
c3a31e60 | 7662 | while (nr_args) { |
65e19f54 JA |
7663 | struct fixed_file_table *table; |
7664 | unsigned index; | |
7665 | ||
c3a31e60 JA |
7666 | err = 0; |
7667 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7668 | err = -EFAULT; | |
7669 | break; | |
7670 | } | |
05f3fb3c JA |
7671 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
7672 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
7673 | index = i & IORING_FILE_TABLE_MASK; |
7674 | if (table->files[index]) { | |
98dfd502 | 7675 | file = table->files[index]; |
a5318d3c HD |
7676 | err = io_queue_file_removal(data, file); |
7677 | if (err) | |
7678 | break; | |
65e19f54 | 7679 | table->files[index] = NULL; |
05589553 | 7680 | needs_switch = true; |
c3a31e60 JA |
7681 | } |
7682 | if (fd != -1) { | |
c3a31e60 JA |
7683 | file = fget(fd); |
7684 | if (!file) { | |
7685 | err = -EBADF; | |
7686 | break; | |
7687 | } | |
7688 | /* | |
7689 | * Don't allow io_uring instances to be registered. If | |
7690 | * UNIX isn't enabled, then this causes a reference | |
7691 | * cycle and this instance can never get freed. If UNIX | |
7692 | * is enabled we'll handle it just fine, but there's | |
7693 | * still no point in allowing a ring fd as it doesn't | |
7694 | * support regular read/write anyway. | |
7695 | */ | |
7696 | if (file->f_op == &io_uring_fops) { | |
7697 | fput(file); | |
7698 | err = -EBADF; | |
7699 | break; | |
7700 | } | |
65e19f54 | 7701 | table->files[index] = file; |
c3a31e60 | 7702 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7703 | if (err) { |
95d1c8e5 | 7704 | table->files[index] = NULL; |
f3bd9dae | 7705 | fput(file); |
c3a31e60 | 7706 | break; |
f3bd9dae | 7707 | } |
c3a31e60 JA |
7708 | } |
7709 | nr_args--; | |
7710 | done++; | |
05f3fb3c JA |
7711 | up->offset++; |
7712 | } | |
7713 | ||
05589553 | 7714 | if (needs_switch) { |
b2e96852 | 7715 | percpu_ref_kill(&data->node->refs); |
e0d59d3c | 7716 | io_sqe_files_set_node(data, ref_node); |
05589553 XW |
7717 | } else |
7718 | destroy_fixed_file_ref_node(ref_node); | |
c3a31e60 JA |
7719 | |
7720 | return done ? done : err; | |
7721 | } | |
05589553 | 7722 | |
05f3fb3c JA |
7723 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7724 | unsigned nr_args) | |
7725 | { | |
7726 | struct io_uring_files_update up; | |
7727 | ||
7728 | if (!ctx->file_data) | |
7729 | return -ENXIO; | |
7730 | if (!nr_args) | |
7731 | return -EINVAL; | |
7732 | if (copy_from_user(&up, arg, sizeof(up))) | |
7733 | return -EFAULT; | |
7734 | if (up.resv) | |
7735 | return -EINVAL; | |
7736 | ||
7737 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7738 | } | |
c3a31e60 | 7739 | |
e9fd9396 | 7740 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
7741 | { |
7742 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7743 | ||
e9fd9396 | 7744 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
7745 | io_put_req(req); |
7746 | } | |
7747 | ||
24369c2e PB |
7748 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
7749 | struct io_uring_params *p) | |
7750 | { | |
7751 | struct io_wq_data data; | |
7752 | struct fd f; | |
7753 | struct io_ring_ctx *ctx_attach; | |
7754 | unsigned int concurrency; | |
7755 | int ret = 0; | |
7756 | ||
7757 | data.user = ctx->user; | |
e9fd9396 | 7758 | data.free_work = io_free_work; |
f5fa38c5 | 7759 | data.do_work = io_wq_submit_work; |
24369c2e PB |
7760 | |
7761 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
7762 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
7763 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
7764 | ||
7765 | ctx->io_wq = io_wq_create(concurrency, &data); | |
7766 | if (IS_ERR(ctx->io_wq)) { | |
7767 | ret = PTR_ERR(ctx->io_wq); | |
7768 | ctx->io_wq = NULL; | |
7769 | } | |
7770 | return ret; | |
7771 | } | |
7772 | ||
7773 | f = fdget(p->wq_fd); | |
7774 | if (!f.file) | |
7775 | return -EBADF; | |
7776 | ||
7777 | if (f.file->f_op != &io_uring_fops) { | |
7778 | ret = -EINVAL; | |
7779 | goto out_fput; | |
7780 | } | |
7781 | ||
7782 | ctx_attach = f.file->private_data; | |
7783 | /* @io_wq is protected by holding the fd */ | |
7784 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
7785 | ret = -EINVAL; | |
7786 | goto out_fput; | |
7787 | } | |
7788 | ||
7789 | ctx->io_wq = ctx_attach->io_wq; | |
7790 | out_fput: | |
7791 | fdput(f); | |
7792 | return ret; | |
7793 | } | |
7794 | ||
0f212204 JA |
7795 | static int io_uring_alloc_task_context(struct task_struct *task) |
7796 | { | |
7797 | struct io_uring_task *tctx; | |
d8a6df10 | 7798 | int ret; |
0f212204 JA |
7799 | |
7800 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7801 | if (unlikely(!tctx)) | |
7802 | return -ENOMEM; | |
7803 | ||
d8a6df10 JA |
7804 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7805 | if (unlikely(ret)) { | |
7806 | kfree(tctx); | |
7807 | return ret; | |
7808 | } | |
7809 | ||
0f212204 JA |
7810 | xa_init(&tctx->xa); |
7811 | init_waitqueue_head(&tctx->wait); | |
7812 | tctx->last = NULL; | |
fdaf083c JA |
7813 | atomic_set(&tctx->in_idle, 0); |
7814 | tctx->sqpoll = false; | |
500a373d JA |
7815 | io_init_identity(&tctx->__identity); |
7816 | tctx->identity = &tctx->__identity; | |
0f212204 JA |
7817 | task->io_uring = tctx; |
7818 | return 0; | |
7819 | } | |
7820 | ||
7821 | void __io_uring_free(struct task_struct *tsk) | |
7822 | { | |
7823 | struct io_uring_task *tctx = tsk->io_uring; | |
7824 | ||
7825 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
7826 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
7827 | if (tctx->identity != &tctx->__identity) | |
7828 | kfree(tctx->identity); | |
d8a6df10 | 7829 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7830 | kfree(tctx); |
7831 | tsk->io_uring = NULL; | |
7832 | } | |
7833 | ||
7e84e1c7 SG |
7834 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7835 | struct io_uring_params *p) | |
2b188cc1 JA |
7836 | { |
7837 | int ret; | |
7838 | ||
6c271ce2 | 7839 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
7840 | struct io_sq_data *sqd; |
7841 | ||
3ec482d1 JA |
7842 | ret = -EPERM; |
7843 | if (!capable(CAP_SYS_ADMIN)) | |
7844 | goto err; | |
7845 | ||
534ca6d6 JA |
7846 | sqd = io_get_sq_data(p); |
7847 | if (IS_ERR(sqd)) { | |
7848 | ret = PTR_ERR(sqd); | |
7849 | goto err; | |
7850 | } | |
69fb2131 | 7851 | |
534ca6d6 | 7852 | ctx->sq_data = sqd; |
69fb2131 JA |
7853 | io_sq_thread_park(sqd); |
7854 | mutex_lock(&sqd->ctx_lock); | |
7855 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
7856 | mutex_unlock(&sqd->ctx_lock); | |
7857 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 7858 | |
917257da JA |
7859 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7860 | if (!ctx->sq_thread_idle) | |
7861 | ctx->sq_thread_idle = HZ; | |
7862 | ||
aa06165d JA |
7863 | if (sqd->thread) |
7864 | goto done; | |
7865 | ||
6c271ce2 | 7866 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7867 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7868 | |
917257da | 7869 | ret = -EINVAL; |
44a9bd18 JA |
7870 | if (cpu >= nr_cpu_ids) |
7871 | goto err; | |
7889f44d | 7872 | if (!cpu_online(cpu)) |
917257da JA |
7873 | goto err; |
7874 | ||
69fb2131 | 7875 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 7876 | cpu, "io_uring-sq"); |
6c271ce2 | 7877 | } else { |
69fb2131 | 7878 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
7879 | "io_uring-sq"); |
7880 | } | |
534ca6d6 JA |
7881 | if (IS_ERR(sqd->thread)) { |
7882 | ret = PTR_ERR(sqd->thread); | |
7883 | sqd->thread = NULL; | |
6c271ce2 JA |
7884 | goto err; |
7885 | } | |
534ca6d6 | 7886 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
7887 | if (ret) |
7888 | goto err; | |
6c271ce2 JA |
7889 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7890 | /* Can't have SQ_AFF without SQPOLL */ | |
7891 | ret = -EINVAL; | |
7892 | goto err; | |
7893 | } | |
7894 | ||
aa06165d | 7895 | done: |
24369c2e PB |
7896 | ret = io_init_wq_offload(ctx, p); |
7897 | if (ret) | |
2b188cc1 | 7898 | goto err; |
2b188cc1 JA |
7899 | |
7900 | return 0; | |
7901 | err: | |
54a91f3b | 7902 | io_finish_async(ctx); |
2b188cc1 JA |
7903 | return ret; |
7904 | } | |
7905 | ||
7e84e1c7 SG |
7906 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7907 | { | |
534ca6d6 JA |
7908 | struct io_sq_data *sqd = ctx->sq_data; |
7909 | ||
7910 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
7911 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
7912 | } |
7913 | ||
a087e2b5 BM |
7914 | static inline void __io_unaccount_mem(struct user_struct *user, |
7915 | unsigned long nr_pages) | |
2b188cc1 JA |
7916 | { |
7917 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7918 | } | |
7919 | ||
a087e2b5 BM |
7920 | static inline int __io_account_mem(struct user_struct *user, |
7921 | unsigned long nr_pages) | |
2b188cc1 JA |
7922 | { |
7923 | unsigned long page_limit, cur_pages, new_pages; | |
7924 | ||
7925 | /* Don't allow more pages than we can safely lock */ | |
7926 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7927 | ||
7928 | do { | |
7929 | cur_pages = atomic_long_read(&user->locked_vm); | |
7930 | new_pages = cur_pages + nr_pages; | |
7931 | if (new_pages > page_limit) | |
7932 | return -ENOMEM; | |
7933 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7934 | new_pages) != cur_pages); | |
7935 | ||
7936 | return 0; | |
7937 | } | |
7938 | ||
2e0464d4 BM |
7939 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
7940 | enum io_mem_account acct) | |
a087e2b5 | 7941 | { |
aad5d8da | 7942 | if (ctx->limit_mem) |
a087e2b5 | 7943 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7944 | |
2aede0e4 | 7945 | if (ctx->mm_account) { |
2e0464d4 | 7946 | if (acct == ACCT_LOCKED) |
2aede0e4 | 7947 | ctx->mm_account->locked_vm -= nr_pages; |
2e0464d4 | 7948 | else if (acct == ACCT_PINNED) |
2aede0e4 | 7949 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
2e0464d4 | 7950 | } |
a087e2b5 BM |
7951 | } |
7952 | ||
2e0464d4 BM |
7953 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
7954 | enum io_mem_account acct) | |
a087e2b5 | 7955 | { |
30975825 BM |
7956 | int ret; |
7957 | ||
7958 | if (ctx->limit_mem) { | |
7959 | ret = __io_account_mem(ctx->user, nr_pages); | |
7960 | if (ret) | |
7961 | return ret; | |
7962 | } | |
7963 | ||
2aede0e4 | 7964 | if (ctx->mm_account) { |
2e0464d4 | 7965 | if (acct == ACCT_LOCKED) |
2aede0e4 | 7966 | ctx->mm_account->locked_vm += nr_pages; |
2e0464d4 | 7967 | else if (acct == ACCT_PINNED) |
2aede0e4 | 7968 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
2e0464d4 | 7969 | } |
a087e2b5 BM |
7970 | |
7971 | return 0; | |
7972 | } | |
7973 | ||
2b188cc1 JA |
7974 | static void io_mem_free(void *ptr) |
7975 | { | |
52e04ef4 MR |
7976 | struct page *page; |
7977 | ||
7978 | if (!ptr) | |
7979 | return; | |
2b188cc1 | 7980 | |
52e04ef4 | 7981 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7982 | if (put_page_testzero(page)) |
7983 | free_compound_page(page); | |
7984 | } | |
7985 | ||
7986 | static void *io_mem_alloc(size_t size) | |
7987 | { | |
7988 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
7989 | __GFP_NORETRY; | |
7990 | ||
7991 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
7992 | } | |
7993 | ||
75b28aff HV |
7994 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
7995 | size_t *sq_offset) | |
7996 | { | |
7997 | struct io_rings *rings; | |
7998 | size_t off, sq_array_size; | |
7999 | ||
8000 | off = struct_size(rings, cqes, cq_entries); | |
8001 | if (off == SIZE_MAX) | |
8002 | return SIZE_MAX; | |
8003 | ||
8004 | #ifdef CONFIG_SMP | |
8005 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8006 | if (off == 0) | |
8007 | return SIZE_MAX; | |
8008 | #endif | |
8009 | ||
b36200f5 DV |
8010 | if (sq_offset) |
8011 | *sq_offset = off; | |
8012 | ||
75b28aff HV |
8013 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8014 | if (sq_array_size == SIZE_MAX) | |
8015 | return SIZE_MAX; | |
8016 | ||
8017 | if (check_add_overflow(off, sq_array_size, &off)) | |
8018 | return SIZE_MAX; | |
8019 | ||
75b28aff HV |
8020 | return off; |
8021 | } | |
8022 | ||
2b188cc1 JA |
8023 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8024 | { | |
75b28aff | 8025 | size_t pages; |
2b188cc1 | 8026 | |
75b28aff HV |
8027 | pages = (size_t)1 << get_order( |
8028 | rings_size(sq_entries, cq_entries, NULL)); | |
8029 | pages += (size_t)1 << get_order( | |
8030 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8031 | |
75b28aff | 8032 | return pages; |
2b188cc1 JA |
8033 | } |
8034 | ||
edafccee JA |
8035 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
8036 | { | |
8037 | int i, j; | |
8038 | ||
8039 | if (!ctx->user_bufs) | |
8040 | return -ENXIO; | |
8041 | ||
8042 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8043 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8044 | ||
8045 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8046 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8047 | |
de293938 JA |
8048 | if (imu->acct_pages) |
8049 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8050 | kvfree(imu->bvec); |
edafccee JA |
8051 | imu->nr_bvecs = 0; |
8052 | } | |
8053 | ||
8054 | kfree(ctx->user_bufs); | |
8055 | ctx->user_bufs = NULL; | |
8056 | ctx->nr_user_bufs = 0; | |
8057 | return 0; | |
8058 | } | |
8059 | ||
8060 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8061 | void __user *arg, unsigned index) | |
8062 | { | |
8063 | struct iovec __user *src; | |
8064 | ||
8065 | #ifdef CONFIG_COMPAT | |
8066 | if (ctx->compat) { | |
8067 | struct compat_iovec __user *ciovs; | |
8068 | struct compat_iovec ciov; | |
8069 | ||
8070 | ciovs = (struct compat_iovec __user *) arg; | |
8071 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8072 | return -EFAULT; | |
8073 | ||
d55e5f5b | 8074 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8075 | dst->iov_len = ciov.iov_len; |
8076 | return 0; | |
8077 | } | |
8078 | #endif | |
8079 | src = (struct iovec __user *) arg; | |
8080 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8081 | return -EFAULT; | |
8082 | return 0; | |
8083 | } | |
8084 | ||
de293938 JA |
8085 | /* |
8086 | * Not super efficient, but this is just a registration time. And we do cache | |
8087 | * the last compound head, so generally we'll only do a full search if we don't | |
8088 | * match that one. | |
8089 | * | |
8090 | * We check if the given compound head page has already been accounted, to | |
8091 | * avoid double accounting it. This allows us to account the full size of the | |
8092 | * page, not just the constituent pages of a huge page. | |
8093 | */ | |
8094 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8095 | int nr_pages, struct page *hpage) | |
8096 | { | |
8097 | int i, j; | |
8098 | ||
8099 | /* check current page array */ | |
8100 | for (i = 0; i < nr_pages; i++) { | |
8101 | if (!PageCompound(pages[i])) | |
8102 | continue; | |
8103 | if (compound_head(pages[i]) == hpage) | |
8104 | return true; | |
8105 | } | |
8106 | ||
8107 | /* check previously registered pages */ | |
8108 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8109 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8110 | ||
8111 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8112 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8113 | continue; | |
8114 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8115 | return true; | |
8116 | } | |
8117 | } | |
8118 | ||
8119 | return false; | |
8120 | } | |
8121 | ||
8122 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8123 | int nr_pages, struct io_mapped_ubuf *imu, | |
8124 | struct page **last_hpage) | |
8125 | { | |
8126 | int i, ret; | |
8127 | ||
8128 | for (i = 0; i < nr_pages; i++) { | |
8129 | if (!PageCompound(pages[i])) { | |
8130 | imu->acct_pages++; | |
8131 | } else { | |
8132 | struct page *hpage; | |
8133 | ||
8134 | hpage = compound_head(pages[i]); | |
8135 | if (hpage == *last_hpage) | |
8136 | continue; | |
8137 | *last_hpage = hpage; | |
8138 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8139 | continue; | |
8140 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8141 | } | |
8142 | } | |
8143 | ||
8144 | if (!imu->acct_pages) | |
8145 | return 0; | |
8146 | ||
8147 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8148 | if (ret) | |
8149 | imu->acct_pages = 0; | |
8150 | return ret; | |
8151 | } | |
8152 | ||
edafccee JA |
8153 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
8154 | unsigned nr_args) | |
8155 | { | |
8156 | struct vm_area_struct **vmas = NULL; | |
8157 | struct page **pages = NULL; | |
de293938 | 8158 | struct page *last_hpage = NULL; |
edafccee JA |
8159 | int i, j, got_pages = 0; |
8160 | int ret = -EINVAL; | |
8161 | ||
8162 | if (ctx->user_bufs) | |
8163 | return -EBUSY; | |
8164 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8165 | return -EINVAL; | |
8166 | ||
8167 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8168 | GFP_KERNEL); | |
8169 | if (!ctx->user_bufs) | |
8170 | return -ENOMEM; | |
8171 | ||
8172 | for (i = 0; i < nr_args; i++) { | |
8173 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8174 | unsigned long off, start, end, ubuf; | |
8175 | int pret, nr_pages; | |
8176 | struct iovec iov; | |
8177 | size_t size; | |
8178 | ||
8179 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8180 | if (ret) | |
a278682d | 8181 | goto err; |
edafccee JA |
8182 | |
8183 | /* | |
8184 | * Don't impose further limits on the size and buffer | |
8185 | * constraints here, we'll -EINVAL later when IO is | |
8186 | * submitted if they are wrong. | |
8187 | */ | |
8188 | ret = -EFAULT; | |
8189 | if (!iov.iov_base || !iov.iov_len) | |
8190 | goto err; | |
8191 | ||
8192 | /* arbitrary limit, but we need something */ | |
8193 | if (iov.iov_len > SZ_1G) | |
8194 | goto err; | |
8195 | ||
8196 | ubuf = (unsigned long) iov.iov_base; | |
8197 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8198 | start = ubuf >> PAGE_SHIFT; | |
8199 | nr_pages = end - start; | |
8200 | ||
edafccee JA |
8201 | ret = 0; |
8202 | if (!pages || nr_pages > got_pages) { | |
a8c73c1a DE |
8203 | kvfree(vmas); |
8204 | kvfree(pages); | |
d4ef6475 | 8205 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 8206 | GFP_KERNEL); |
d4ef6475 | 8207 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
8208 | sizeof(struct vm_area_struct *), |
8209 | GFP_KERNEL); | |
8210 | if (!pages || !vmas) { | |
8211 | ret = -ENOMEM; | |
edafccee JA |
8212 | goto err; |
8213 | } | |
8214 | got_pages = nr_pages; | |
8215 | } | |
8216 | ||
d4ef6475 | 8217 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
8218 | GFP_KERNEL); |
8219 | ret = -ENOMEM; | |
de293938 | 8220 | if (!imu->bvec) |
edafccee | 8221 | goto err; |
edafccee JA |
8222 | |
8223 | ret = 0; | |
d8ed45c5 | 8224 | mmap_read_lock(current->mm); |
2113b05d | 8225 | pret = pin_user_pages(ubuf, nr_pages, |
932f4a63 IW |
8226 | FOLL_WRITE | FOLL_LONGTERM, |
8227 | pages, vmas); | |
edafccee JA |
8228 | if (pret == nr_pages) { |
8229 | /* don't support file backed memory */ | |
8230 | for (j = 0; j < nr_pages; j++) { | |
8231 | struct vm_area_struct *vma = vmas[j]; | |
8232 | ||
8233 | if (vma->vm_file && | |
8234 | !is_file_hugepages(vma->vm_file)) { | |
8235 | ret = -EOPNOTSUPP; | |
8236 | break; | |
8237 | } | |
8238 | } | |
8239 | } else { | |
8240 | ret = pret < 0 ? pret : -EFAULT; | |
8241 | } | |
d8ed45c5 | 8242 | mmap_read_unlock(current->mm); |
edafccee JA |
8243 | if (ret) { |
8244 | /* | |
8245 | * if we did partial map, or found file backed vmas, | |
8246 | * release any pages we did get | |
8247 | */ | |
27c4d3a3 | 8248 | if (pret > 0) |
f1f6a7dd | 8249 | unpin_user_pages(pages, pret); |
de293938 JA |
8250 | kvfree(imu->bvec); |
8251 | goto err; | |
8252 | } | |
8253 | ||
8254 | ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage); | |
8255 | if (ret) { | |
8256 | unpin_user_pages(pages, pret); | |
d4ef6475 | 8257 | kvfree(imu->bvec); |
edafccee JA |
8258 | goto err; |
8259 | } | |
8260 | ||
8261 | off = ubuf & ~PAGE_MASK; | |
8262 | size = iov.iov_len; | |
8263 | for (j = 0; j < nr_pages; j++) { | |
8264 | size_t vec_len; | |
8265 | ||
8266 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8267 | imu->bvec[j].bv_page = pages[j]; | |
8268 | imu->bvec[j].bv_len = vec_len; | |
8269 | imu->bvec[j].bv_offset = off; | |
8270 | off = 0; | |
8271 | size -= vec_len; | |
8272 | } | |
8273 | /* store original address for later verification */ | |
8274 | imu->ubuf = ubuf; | |
8275 | imu->len = iov.iov_len; | |
8276 | imu->nr_bvecs = nr_pages; | |
8277 | ||
8278 | ctx->nr_user_bufs++; | |
8279 | } | |
d4ef6475 MR |
8280 | kvfree(pages); |
8281 | kvfree(vmas); | |
edafccee JA |
8282 | return 0; |
8283 | err: | |
d4ef6475 MR |
8284 | kvfree(pages); |
8285 | kvfree(vmas); | |
edafccee JA |
8286 | io_sqe_buffer_unregister(ctx); |
8287 | return ret; | |
8288 | } | |
8289 | ||
9b402849 JA |
8290 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8291 | { | |
8292 | __s32 __user *fds = arg; | |
8293 | int fd; | |
8294 | ||
8295 | if (ctx->cq_ev_fd) | |
8296 | return -EBUSY; | |
8297 | ||
8298 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8299 | return -EFAULT; | |
8300 | ||
8301 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8302 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8303 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8304 | ctx->cq_ev_fd = NULL; | |
8305 | return ret; | |
8306 | } | |
8307 | ||
8308 | return 0; | |
8309 | } | |
8310 | ||
8311 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8312 | { | |
8313 | if (ctx->cq_ev_fd) { | |
8314 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8315 | ctx->cq_ev_fd = NULL; | |
8316 | return 0; | |
8317 | } | |
8318 | ||
8319 | return -ENXIO; | |
8320 | } | |
8321 | ||
5a2e745d JA |
8322 | static int __io_destroy_buffers(int id, void *p, void *data) |
8323 | { | |
8324 | struct io_ring_ctx *ctx = data; | |
8325 | struct io_buffer *buf = p; | |
8326 | ||
067524e9 | 8327 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8328 | return 0; |
8329 | } | |
8330 | ||
8331 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8332 | { | |
8333 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8334 | idr_destroy(&ctx->io_buffer_idr); | |
8335 | } | |
8336 | ||
2b188cc1 JA |
8337 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8338 | { | |
6b06314c | 8339 | io_finish_async(ctx); |
5dbcad51 | 8340 | io_sqe_buffer_unregister(ctx); |
2aede0e4 JA |
8341 | |
8342 | if (ctx->sqo_task) { | |
8343 | put_task_struct(ctx->sqo_task); | |
8344 | ctx->sqo_task = NULL; | |
8345 | mmdrop(ctx->mm_account); | |
8346 | ctx->mm_account = NULL; | |
30975825 | 8347 | } |
def596e9 | 8348 | |
91d8f519 DZ |
8349 | #ifdef CONFIG_BLK_CGROUP |
8350 | if (ctx->sqo_blkcg_css) | |
8351 | css_put(ctx->sqo_blkcg_css); | |
8352 | #endif | |
8353 | ||
6b06314c | 8354 | io_sqe_files_unregister(ctx); |
9b402849 | 8355 | io_eventfd_unregister(ctx); |
5a2e745d | 8356 | io_destroy_buffers(ctx); |
41726c9a | 8357 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8358 | |
2b188cc1 | 8359 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8360 | if (ctx->ring_sock) { |
8361 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8362 | sock_release(ctx->ring_sock); |
355e8d26 | 8363 | } |
2b188cc1 JA |
8364 | #endif |
8365 | ||
75b28aff | 8366 | io_mem_free(ctx->rings); |
2b188cc1 | 8367 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8368 | |
8369 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8370 | free_uid(ctx->user); |
181e448d | 8371 | put_cred(ctx->creds); |
78076bb6 | 8372 | kfree(ctx->cancel_hash); |
0ddf92e8 | 8373 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
8374 | kfree(ctx); |
8375 | } | |
8376 | ||
8377 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8378 | { | |
8379 | struct io_ring_ctx *ctx = file->private_data; | |
8380 | __poll_t mask = 0; | |
8381 | ||
8382 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8383 | /* |
8384 | * synchronizes with barrier from wq_has_sleeper call in | |
8385 | * io_commit_cqring | |
8386 | */ | |
2b188cc1 | 8387 | smp_rmb(); |
90554200 | 8388 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8389 | mask |= EPOLLOUT | EPOLLWRNORM; |
63e5d81f | 8390 | if (io_cqring_events(ctx, false)) |
2b188cc1 JA |
8391 | mask |= EPOLLIN | EPOLLRDNORM; |
8392 | ||
8393 | return mask; | |
8394 | } | |
8395 | ||
8396 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8397 | { | |
8398 | struct io_ring_ctx *ctx = file->private_data; | |
8399 | ||
8400 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8401 | } | |
8402 | ||
071698e1 JA |
8403 | static int io_remove_personalities(int id, void *p, void *data) |
8404 | { | |
8405 | struct io_ring_ctx *ctx = data; | |
1e6fa521 | 8406 | struct io_identity *iod; |
071698e1 | 8407 | |
1e6fa521 JA |
8408 | iod = idr_remove(&ctx->personality_idr, id); |
8409 | if (iod) { | |
8410 | put_cred(iod->creds); | |
8411 | if (refcount_dec_and_test(&iod->count)) | |
8412 | kfree(iod); | |
8413 | } | |
071698e1 JA |
8414 | return 0; |
8415 | } | |
8416 | ||
85faa7b8 JA |
8417 | static void io_ring_exit_work(struct work_struct *work) |
8418 | { | |
b2edc0a7 PB |
8419 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8420 | exit_work); | |
85faa7b8 | 8421 | |
56952e91 JA |
8422 | /* |
8423 | * If we're doing polled IO and end up having requests being | |
8424 | * submitted async (out-of-line), then completions can come in while | |
8425 | * we're waiting for refs to drop. We need to reap these manually, | |
8426 | * as nobody else will be looking for them. | |
8427 | */ | |
b2edc0a7 | 8428 | do { |
b2edc0a7 PB |
8429 | io_iopoll_try_reap_events(ctx); |
8430 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); | |
85faa7b8 JA |
8431 | io_ring_ctx_free(ctx); |
8432 | } | |
8433 | ||
1ecf05fd JA |
8434 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8435 | { | |
8436 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8437 | ||
8438 | return req->ctx == data; | |
8439 | } | |
8440 | ||
2b188cc1 JA |
8441 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8442 | { | |
8443 | mutex_lock(&ctx->uring_lock); | |
8444 | percpu_ref_kill(&ctx->refs); | |
1813aa63 PB |
8445 | /* if force is set, the ring is going away. always drop after that */ |
8446 | ctx->cq_overflow_flushed = 1; | |
95a0f0a1 PB |
8447 | if (ctx->rings) |
8448 | io_cqring_overflow_flush(ctx, true, NULL, NULL); | |
2b188cc1 JA |
8449 | mutex_unlock(&ctx->uring_lock); |
8450 | ||
f3606e3a JA |
8451 | io_kill_timeouts(ctx, NULL); |
8452 | io_poll_remove_all(ctx, NULL); | |
561fb04a JA |
8453 | |
8454 | if (ctx->io_wq) | |
1ecf05fd | 8455 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8456 | |
15dff286 | 8457 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8458 | io_iopoll_try_reap_events(ctx); |
071698e1 | 8459 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
309fc03a JA |
8460 | |
8461 | /* | |
8462 | * Do this upfront, so we won't have a grace period where the ring | |
8463 | * is closed but resources aren't reaped yet. This can cause | |
8464 | * spurious failure in setting up a new ring. | |
8465 | */ | |
760618f7 JA |
8466 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8467 | ACCT_LOCKED); | |
309fc03a | 8468 | |
85faa7b8 | 8469 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8470 | /* |
8471 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8472 | * if we're exiting a ton of rings at the same time. It just adds | |
8473 | * noise and overhead, there's no discernable change in runtime | |
8474 | * over using system_wq. | |
8475 | */ | |
8476 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8477 | } |
8478 | ||
8479 | static int io_uring_release(struct inode *inode, struct file *file) | |
8480 | { | |
8481 | struct io_ring_ctx *ctx = file->private_data; | |
8482 | ||
8483 | file->private_data = NULL; | |
8484 | io_ring_ctx_wait_and_kill(ctx); | |
8485 | return 0; | |
8486 | } | |
8487 | ||
f254ac04 JA |
8488 | /* |
8489 | * Returns true if 'preq' is the link parent of 'req' | |
8490 | */ | |
8491 | static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req) | |
8492 | { | |
8493 | struct io_kiocb *link; | |
8494 | ||
8495 | if (!(preq->flags & REQ_F_LINK_HEAD)) | |
8496 | return false; | |
8497 | ||
8498 | list_for_each_entry(link, &preq->link_list, link_list) { | |
8499 | if (link == req) | |
8500 | return true; | |
8501 | } | |
8502 | ||
8503 | return false; | |
8504 | } | |
8505 | ||
8506 | /* | |
8507 | * We're looking to cancel 'req' because it's holding on to our files, but | |
8508 | * 'req' could be a link to another request. See if it is, and cancel that | |
8509 | * parent request if so. | |
8510 | */ | |
8511 | static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req) | |
8512 | { | |
8513 | struct hlist_node *tmp; | |
8514 | struct io_kiocb *preq; | |
8515 | bool found = false; | |
8516 | int i; | |
8517 | ||
8518 | spin_lock_irq(&ctx->completion_lock); | |
8519 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
8520 | struct hlist_head *list; | |
8521 | ||
8522 | list = &ctx->cancel_hash[i]; | |
8523 | hlist_for_each_entry_safe(preq, tmp, list, hash_node) { | |
8524 | found = io_match_link(preq, req); | |
8525 | if (found) { | |
8526 | io_poll_remove_one(preq); | |
8527 | break; | |
8528 | } | |
8529 | } | |
8530 | } | |
8531 | spin_unlock_irq(&ctx->completion_lock); | |
8532 | return found; | |
8533 | } | |
8534 | ||
8535 | static bool io_timeout_remove_link(struct io_ring_ctx *ctx, | |
8536 | struct io_kiocb *req) | |
8537 | { | |
8538 | struct io_kiocb *preq; | |
8539 | bool found = false; | |
8540 | ||
8541 | spin_lock_irq(&ctx->completion_lock); | |
8542 | list_for_each_entry(preq, &ctx->timeout_list, timeout.list) { | |
8543 | found = io_match_link(preq, req); | |
8544 | if (found) { | |
8545 | __io_timeout_cancel(preq); | |
8546 | break; | |
8547 | } | |
8548 | } | |
8549 | spin_unlock_irq(&ctx->completion_lock); | |
8550 | return found; | |
8551 | } | |
8552 | ||
b711d4ea JA |
8553 | static bool io_cancel_link_cb(struct io_wq_work *work, void *data) |
8554 | { | |
9a472ef7 PB |
8555 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
8556 | bool ret; | |
8557 | ||
8558 | if (req->flags & REQ_F_LINK_TIMEOUT) { | |
8559 | unsigned long flags; | |
8560 | struct io_ring_ctx *ctx = req->ctx; | |
8561 | ||
8562 | /* protect against races with linked timeouts */ | |
8563 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
8564 | ret = io_match_link(req, data); | |
8565 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
8566 | } else { | |
8567 | ret = io_match_link(req, data); | |
8568 | } | |
8569 | return ret; | |
b711d4ea JA |
8570 | } |
8571 | ||
8572 | static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req) | |
8573 | { | |
8574 | enum io_wq_cancel cret; | |
8575 | ||
8576 | /* cancel this particular work, if it's running */ | |
8577 | cret = io_wq_cancel_work(ctx->io_wq, &req->work); | |
8578 | if (cret != IO_WQ_CANCEL_NOTFOUND) | |
8579 | return; | |
8580 | ||
8581 | /* find links that hold this pending, cancel those */ | |
8582 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true); | |
8583 | if (cret != IO_WQ_CANCEL_NOTFOUND) | |
8584 | return; | |
8585 | ||
8586 | /* if we have a poll link holding this pending, cancel that */ | |
8587 | if (io_poll_remove_link(ctx, req)) | |
8588 | return; | |
8589 | ||
8590 | /* final option, timeout link is holding this req pending */ | |
8591 | io_timeout_remove_link(ctx, req); | |
8592 | } | |
8593 | ||
b7ddce3c | 8594 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8595 | struct task_struct *task, |
b7ddce3c PB |
8596 | struct files_struct *files) |
8597 | { | |
8598 | struct io_defer_entry *de = NULL; | |
8599 | LIST_HEAD(list); | |
8600 | ||
8601 | spin_lock_irq(&ctx->completion_lock); | |
8602 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
ef9865a4 PB |
8603 | if (io_task_match(de->req, task) && |
8604 | io_match_files(de->req, files)) { | |
b7ddce3c PB |
8605 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8606 | break; | |
8607 | } | |
8608 | } | |
8609 | spin_unlock_irq(&ctx->completion_lock); | |
8610 | ||
8611 | while (!list_empty(&list)) { | |
8612 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8613 | list_del_init(&de->list); | |
8614 | req_set_fail_links(de->req); | |
8615 | io_put_req(de->req); | |
8616 | io_req_complete(de->req, -ECANCELED); | |
8617 | kfree(de); | |
8618 | } | |
8619 | } | |
8620 | ||
76e1b642 JA |
8621 | /* |
8622 | * Returns true if we found and killed one or more files pinning requests | |
8623 | */ | |
8624 | static bool io_uring_cancel_files(struct io_ring_ctx *ctx, | |
658e4986 | 8625 | struct task_struct *task, |
fcb323cc JA |
8626 | struct files_struct *files) |
8627 | { | |
67c4d9e6 | 8628 | if (list_empty_careful(&ctx->inflight_list)) |
76e1b642 | 8629 | return false; |
67c4d9e6 | 8630 | |
fcb323cc | 8631 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 XW |
8632 | struct io_kiocb *cancel_req = NULL, *req; |
8633 | DEFINE_WAIT(wait); | |
fcb323cc JA |
8634 | |
8635 | spin_lock_irq(&ctx->inflight_lock); | |
8636 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
658e4986 PB |
8637 | if (req->task == task && |
8638 | (req->work.flags & IO_WQ_WORK_FILES) && | |
98447d65 | 8639 | req->work.identity->files != files) |
768134d4 JA |
8640 | continue; |
8641 | /* req is being completed, ignore */ | |
8642 | if (!refcount_inc_not_zero(&req->refs)) | |
8643 | continue; | |
8644 | cancel_req = req; | |
8645 | break; | |
fcb323cc | 8646 | } |
768134d4 | 8647 | if (cancel_req) |
fcb323cc | 8648 | prepare_to_wait(&ctx->inflight_wait, &wait, |
768134d4 | 8649 | TASK_UNINTERRUPTIBLE); |
fcb323cc JA |
8650 | spin_unlock_irq(&ctx->inflight_lock); |
8651 | ||
768134d4 JA |
8652 | /* We need to keep going until we don't find a matching req */ |
8653 | if (!cancel_req) | |
fcb323cc | 8654 | break; |
bb175342 PB |
8655 | /* cancel this request, or head link requests */ |
8656 | io_attempt_cancel(ctx, cancel_req); | |
8657 | io_put_req(cancel_req); | |
6200b0ae JA |
8658 | /* cancellations _may_ trigger task work */ |
8659 | io_run_task_work(); | |
fcb323cc | 8660 | schedule(); |
d8f1b971 | 8661 | finish_wait(&ctx->inflight_wait, &wait); |
fcb323cc | 8662 | } |
76e1b642 JA |
8663 | |
8664 | return true; | |
fcb323cc JA |
8665 | } |
8666 | ||
801dd57b | 8667 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
44e728b8 | 8668 | { |
801dd57b PB |
8669 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
8670 | struct task_struct *task = data; | |
44e728b8 | 8671 | |
f3606e3a | 8672 | return io_task_match(req, task); |
44e728b8 PB |
8673 | } |
8674 | ||
0f212204 JA |
8675 | static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
8676 | struct task_struct *task, | |
8677 | struct files_struct *files) | |
8678 | { | |
8679 | bool ret; | |
8680 | ||
658e4986 | 8681 | ret = io_uring_cancel_files(ctx, task, files); |
0f212204 JA |
8682 | if (!files) { |
8683 | enum io_wq_cancel cret; | |
8684 | ||
8685 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true); | |
8686 | if (cret != IO_WQ_CANCEL_NOTFOUND) | |
8687 | ret = true; | |
8688 | ||
8689 | /* SQPOLL thread does its own polling */ | |
8690 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8691 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8692 | io_iopoll_try_reap_events(ctx); | |
8693 | ret = true; | |
8694 | } | |
8695 | } | |
8696 | ||
8697 | ret |= io_poll_remove_all(ctx, task); | |
8698 | ret |= io_kill_timeouts(ctx, task); | |
8699 | } | |
8700 | ||
8701 | return ret; | |
8702 | } | |
8703 | ||
8704 | /* | |
8705 | * We need to iteratively cancel requests, in case a request has dependent | |
8706 | * hard links. These persist even for failure of cancelations, hence keep | |
8707 | * looping until none are found. | |
8708 | */ | |
8709 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8710 | struct files_struct *files) | |
8711 | { | |
8712 | struct task_struct *task = current; | |
8713 | ||
fdaf083c | 8714 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
534ca6d6 | 8715 | task = ctx->sq_data->thread; |
fdaf083c JA |
8716 | atomic_inc(&task->io_uring->in_idle); |
8717 | io_sq_thread_park(ctx->sq_data); | |
8718 | } | |
0f212204 | 8719 | |
658e4986 | 8720 | io_cancel_defer_files(ctx, task, files); |
95a0f0a1 | 8721 | io_ring_submit_lock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); |
0f212204 | 8722 | io_cqring_overflow_flush(ctx, true, task, files); |
95a0f0a1 | 8723 | io_ring_submit_unlock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); |
0f212204 JA |
8724 | |
8725 | while (__io_uring_cancel_task_requests(ctx, task, files)) { | |
8726 | io_run_task_work(); | |
8727 | cond_resched(); | |
8728 | } | |
fdaf083c JA |
8729 | |
8730 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
8731 | atomic_dec(&task->io_uring->in_idle); | |
8732 | /* | |
8733 | * If the files that are going away are the ones in the thread | |
8734 | * identity, clear them out. | |
8735 | */ | |
8736 | if (task->io_uring->identity->files == files) | |
8737 | task->io_uring->identity->files = NULL; | |
8738 | io_sq_thread_unpark(ctx->sq_data); | |
8739 | } | |
0f212204 JA |
8740 | } |
8741 | ||
8742 | /* | |
8743 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8744 | */ | |
fdaf083c | 8745 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8746 | { |
236434c3 | 8747 | struct io_uring_task *tctx = current->io_uring; |
b36843ac | 8748 | int ret; |
236434c3 MWO |
8749 | |
8750 | if (unlikely(!tctx)) { | |
0f212204 JA |
8751 | ret = io_uring_alloc_task_context(current); |
8752 | if (unlikely(ret)) | |
8753 | return ret; | |
236434c3 | 8754 | tctx = current->io_uring; |
0f212204 | 8755 | } |
236434c3 MWO |
8756 | if (tctx->last != file) { |
8757 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8758 | |
236434c3 | 8759 | if (!old) { |
0f212204 | 8760 | get_file(file); |
b36843ac PB |
8761 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
8762 | file, GFP_KERNEL)); | |
8763 | if (ret) { | |
8764 | fput(file); | |
8765 | return ret; | |
8766 | } | |
0f212204 | 8767 | } |
236434c3 | 8768 | tctx->last = file; |
0f212204 JA |
8769 | } |
8770 | ||
fdaf083c JA |
8771 | /* |
8772 | * This is race safe in that the task itself is doing this, hence it | |
8773 | * cannot be going through the exit/cancel paths at the same time. | |
8774 | * This cannot be modified while exit/cancel is running. | |
8775 | */ | |
8776 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8777 | tctx->sqpoll = true; | |
8778 | ||
0f212204 JA |
8779 | return 0; |
8780 | } | |
8781 | ||
8782 | /* | |
8783 | * Remove this io_uring_file -> task mapping. | |
8784 | */ | |
8785 | static void io_uring_del_task_file(struct file *file) | |
8786 | { | |
8787 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
8788 | |
8789 | if (tctx->last == file) | |
8790 | tctx->last = NULL; | |
5e2ed8c4 | 8791 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
8792 | if (file) |
8793 | fput(file); | |
8794 | } | |
8795 | ||
0f212204 JA |
8796 | /* |
8797 | * Drop task note for this file if we're the only ones that hold it after | |
8798 | * pending fput() | |
8799 | */ | |
c8fb20b5 | 8800 | static void io_uring_attempt_task_drop(struct file *file) |
0f212204 JA |
8801 | { |
8802 | if (!current->io_uring) | |
8803 | return; | |
8804 | /* | |
8805 | * fput() is pending, will be 2 if the only other ref is our potential | |
8806 | * task file note. If the task is exiting, drop regardless of count. | |
8807 | */ | |
c8fb20b5 PB |
8808 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING) || |
8809 | atomic_long_read(&file->f_count) == 2) | |
8810 | io_uring_del_task_file(file); | |
0f212204 JA |
8811 | } |
8812 | ||
8813 | void __io_uring_files_cancel(struct files_struct *files) | |
8814 | { | |
8815 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
8816 | struct file *file; |
8817 | unsigned long index; | |
0f212204 JA |
8818 | |
8819 | /* make sure overflow events are dropped */ | |
fdaf083c | 8820 | atomic_inc(&tctx->in_idle); |
0f212204 | 8821 | |
ce765372 MWO |
8822 | xa_for_each(&tctx->xa, index, file) { |
8823 | struct io_ring_ctx *ctx = file->private_data; | |
0f212204 JA |
8824 | |
8825 | io_uring_cancel_task_requests(ctx, files); | |
8826 | if (files) | |
8827 | io_uring_del_task_file(file); | |
ce765372 | 8828 | } |
fdaf083c JA |
8829 | |
8830 | atomic_dec(&tctx->in_idle); | |
8831 | } | |
8832 | ||
8833 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8834 | { | |
8835 | unsigned long index; | |
8836 | struct file *file; | |
8837 | s64 inflight; | |
8838 | ||
8839 | inflight = percpu_counter_sum(&tctx->inflight); | |
8840 | if (!tctx->sqpoll) | |
8841 | return inflight; | |
8842 | ||
8843 | /* | |
8844 | * If we have SQPOLL rings, then we need to iterate and find them, and | |
8845 | * add the pending count for those. | |
8846 | */ | |
8847 | xa_for_each(&tctx->xa, index, file) { | |
8848 | struct io_ring_ctx *ctx = file->private_data; | |
8849 | ||
8850 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
8851 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; | |
8852 | ||
8853 | inflight += percpu_counter_sum(&__tctx->inflight); | |
8854 | } | |
8855 | } | |
8856 | ||
8857 | return inflight; | |
0f212204 JA |
8858 | } |
8859 | ||
0f212204 JA |
8860 | /* |
8861 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8862 | * requests. | |
8863 | */ | |
8864 | void __io_uring_task_cancel(void) | |
8865 | { | |
8866 | struct io_uring_task *tctx = current->io_uring; | |
8867 | DEFINE_WAIT(wait); | |
d8a6df10 | 8868 | s64 inflight; |
0f212204 JA |
8869 | |
8870 | /* make sure overflow events are dropped */ | |
fdaf083c | 8871 | atomic_inc(&tctx->in_idle); |
0f212204 | 8872 | |
d8a6df10 | 8873 | do { |
0f212204 | 8874 | /* read completions before cancelations */ |
fdaf083c | 8875 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
8876 | if (!inflight) |
8877 | break; | |
0f212204 JA |
8878 | __io_uring_files_cancel(NULL); |
8879 | ||
8880 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8881 | ||
8882 | /* | |
8883 | * If we've seen completions, retry. This avoids a race where | |
8884 | * a completion comes in before we did prepare_to_wait(). | |
8885 | */ | |
fdaf083c | 8886 | if (inflight != tctx_inflight(tctx)) |
0f212204 | 8887 | continue; |
0f212204 | 8888 | schedule(); |
d8a6df10 | 8889 | } while (1); |
0f212204 JA |
8890 | |
8891 | finish_wait(&tctx->wait, &wait); | |
fdaf083c | 8892 | atomic_dec(&tctx->in_idle); |
44e728b8 PB |
8893 | } |
8894 | ||
fcb323cc JA |
8895 | static int io_uring_flush(struct file *file, void *data) |
8896 | { | |
c8fb20b5 | 8897 | io_uring_attempt_task_drop(file); |
fcb323cc JA |
8898 | return 0; |
8899 | } | |
8900 | ||
6c5c240e RP |
8901 | static void *io_uring_validate_mmap_request(struct file *file, |
8902 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8903 | { |
2b188cc1 | 8904 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8905 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8906 | struct page *page; |
8907 | void *ptr; | |
8908 | ||
8909 | switch (offset) { | |
8910 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8911 | case IORING_OFF_CQ_RING: |
8912 | ptr = ctx->rings; | |
2b188cc1 JA |
8913 | break; |
8914 | case IORING_OFF_SQES: | |
8915 | ptr = ctx->sq_sqes; | |
8916 | break; | |
2b188cc1 | 8917 | default: |
6c5c240e | 8918 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8919 | } |
8920 | ||
8921 | page = virt_to_head_page(ptr); | |
a50b854e | 8922 | if (sz > page_size(page)) |
6c5c240e RP |
8923 | return ERR_PTR(-EINVAL); |
8924 | ||
8925 | return ptr; | |
8926 | } | |
8927 | ||
8928 | #ifdef CONFIG_MMU | |
8929 | ||
8930 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8931 | { | |
8932 | size_t sz = vma->vm_end - vma->vm_start; | |
8933 | unsigned long pfn; | |
8934 | void *ptr; | |
8935 | ||
8936 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8937 | if (IS_ERR(ptr)) | |
8938 | return PTR_ERR(ptr); | |
2b188cc1 JA |
8939 | |
8940 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
8941 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
8942 | } | |
8943 | ||
6c5c240e RP |
8944 | #else /* !CONFIG_MMU */ |
8945 | ||
8946 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8947 | { | |
8948 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
8949 | } | |
8950 | ||
8951 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
8952 | { | |
8953 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
8954 | } | |
8955 | ||
8956 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
8957 | unsigned long addr, unsigned long len, | |
8958 | unsigned long pgoff, unsigned long flags) | |
8959 | { | |
8960 | void *ptr; | |
8961 | ||
8962 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
8963 | if (IS_ERR(ptr)) | |
8964 | return PTR_ERR(ptr); | |
8965 | ||
8966 | return (unsigned long) ptr; | |
8967 | } | |
8968 | ||
8969 | #endif /* !CONFIG_MMU */ | |
8970 | ||
90554200 JA |
8971 | static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
8972 | { | |
8973 | DEFINE_WAIT(wait); | |
8974 | ||
8975 | do { | |
8976 | if (!io_sqring_full(ctx)) | |
8977 | break; | |
8978 | ||
8979 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
8980 | ||
8981 | if (!io_sqring_full(ctx)) | |
8982 | break; | |
8983 | ||
8984 | schedule(); | |
8985 | } while (!signal_pending(current)); | |
8986 | ||
8987 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
8988 | } | |
8989 | ||
2b188cc1 JA |
8990 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
8991 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
8992 | size_t, sigsz) | |
8993 | { | |
8994 | struct io_ring_ctx *ctx; | |
8995 | long ret = -EBADF; | |
8996 | int submitted = 0; | |
8997 | struct fd f; | |
8998 | ||
4c6e277c | 8999 | io_run_task_work(); |
b41e9852 | 9000 | |
90554200 JA |
9001 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9002 | IORING_ENTER_SQ_WAIT)) | |
2b188cc1 JA |
9003 | return -EINVAL; |
9004 | ||
9005 | f = fdget(fd); | |
9006 | if (!f.file) | |
9007 | return -EBADF; | |
9008 | ||
9009 | ret = -EOPNOTSUPP; | |
9010 | if (f.file->f_op != &io_uring_fops) | |
9011 | goto out_fput; | |
9012 | ||
9013 | ret = -ENXIO; | |
9014 | ctx = f.file->private_data; | |
9015 | if (!percpu_ref_tryget(&ctx->refs)) | |
9016 | goto out_fput; | |
9017 | ||
7e84e1c7 SG |
9018 | ret = -EBADFD; |
9019 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9020 | goto out; | |
9021 | ||
6c271ce2 JA |
9022 | /* |
9023 | * For SQ polling, the thread will do all submissions and completions. | |
9024 | * Just return the requested submit count, and wake the thread if | |
9025 | * we were asked to. | |
9026 | */ | |
b2a9eada | 9027 | ret = 0; |
6c271ce2 | 9028 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
95a0f0a1 | 9029 | io_ring_submit_lock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); |
c1edbf5f | 9030 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
e6c8aa9a | 9031 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
95a0f0a1 | 9032 | io_ring_submit_unlock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); |
6c271ce2 | 9033 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9034 | wake_up(&ctx->sq_data->wait); |
90554200 JA |
9035 | if (flags & IORING_ENTER_SQ_WAIT) |
9036 | io_sqpoll_wait_sq(ctx); | |
6c271ce2 | 9037 | submitted = to_submit; |
b2a9eada | 9038 | } else if (to_submit) { |
fdaf083c | 9039 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9040 | if (unlikely(ret)) |
9041 | goto out; | |
2b188cc1 | 9042 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9043 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9044 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9045 | |
9046 | if (submitted != to_submit) | |
9047 | goto out; | |
2b188cc1 JA |
9048 | } |
9049 | if (flags & IORING_ENTER_GETEVENTS) { | |
9050 | min_complete = min(min_complete, ctx->cq_entries); | |
9051 | ||
32b2244a XW |
9052 | /* |
9053 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9054 | * space applications don't need to do io completion events | |
9055 | * polling again, they can rely on io_sq_thread to do polling | |
9056 | * work, which can reduce cpu usage and uring_lock contention. | |
9057 | */ | |
9058 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9059 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9060 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 JA |
9061 | } else { |
9062 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
9063 | } | |
2b188cc1 JA |
9064 | } |
9065 | ||
7c504e65 | 9066 | out: |
6805b32e | 9067 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9068 | out_fput: |
9069 | fdput(f); | |
9070 | return submitted ? submitted : ret; | |
9071 | } | |
9072 | ||
bebdb65e | 9073 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9074 | static int io_uring_show_cred(int id, void *p, void *data) |
9075 | { | |
6b47ab81 JA |
9076 | struct io_identity *iod = p; |
9077 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9078 | struct seq_file *m = data; |
9079 | struct user_namespace *uns = seq_user_ns(m); | |
9080 | struct group_info *gi; | |
9081 | kernel_cap_t cap; | |
9082 | unsigned __capi; | |
9083 | int g; | |
9084 | ||
9085 | seq_printf(m, "%5d\n", id); | |
9086 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9087 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9088 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9089 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9090 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9091 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9092 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9093 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9094 | seq_puts(m, "\n\tGroups:\t"); | |
9095 | gi = cred->group_info; | |
9096 | for (g = 0; g < gi->ngroups; g++) { | |
9097 | seq_put_decimal_ull(m, g ? " " : "", | |
9098 | from_kgid_munged(uns, gi->gid[g])); | |
9099 | } | |
9100 | seq_puts(m, "\n\tCapEff:\t"); | |
9101 | cap = cred->cap_effective; | |
9102 | CAP_FOR_EACH_U32(__capi) | |
9103 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9104 | seq_putc(m, '\n'); | |
9105 | return 0; | |
9106 | } | |
9107 | ||
9108 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9109 | { | |
dbbe9c64 | 9110 | struct io_sq_data *sq = NULL; |
fad8e0de | 9111 | bool has_lock; |
87ce955b JA |
9112 | int i; |
9113 | ||
fad8e0de JA |
9114 | /* |
9115 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9116 | * since fdinfo case grabs it in the opposite direction of normal use | |
9117 | * cases. If we fail to get the lock, we just don't iterate any | |
9118 | * structures that could be going away outside the io_uring mutex. | |
9119 | */ | |
9120 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9121 | ||
dbbe9c64 JQ |
9122 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9123 | sq = ctx->sq_data; | |
9124 | ||
9125 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9126 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9127 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9128 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
87ce955b JA |
9129 | struct fixed_file_table *table; |
9130 | struct file *f; | |
9131 | ||
9132 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
9133 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
9134 | if (f) | |
9135 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9136 | else | |
9137 | seq_printf(m, "%5u: <none>\n", i); | |
9138 | } | |
9139 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9140 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9141 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9142 | ||
9143 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9144 | (unsigned int) buf->len); | |
9145 | } | |
fad8e0de | 9146 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9147 | seq_printf(m, "Personalities:\n"); |
9148 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9149 | } | |
d7718a9d JA |
9150 | seq_printf(m, "PollList:\n"); |
9151 | spin_lock_irq(&ctx->completion_lock); | |
9152 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9153 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9154 | struct io_kiocb *req; | |
9155 | ||
9156 | hlist_for_each_entry(req, list, hash_node) | |
9157 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9158 | req->task->task_works != NULL); | |
9159 | } | |
9160 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9161 | if (has_lock) |
9162 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9163 | } |
9164 | ||
9165 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9166 | { | |
9167 | struct io_ring_ctx *ctx = f->private_data; | |
9168 | ||
9169 | if (percpu_ref_tryget(&ctx->refs)) { | |
9170 | __io_uring_show_fdinfo(ctx, m); | |
9171 | percpu_ref_put(&ctx->refs); | |
9172 | } | |
9173 | } | |
bebdb65e | 9174 | #endif |
87ce955b | 9175 | |
2b188cc1 JA |
9176 | static const struct file_operations io_uring_fops = { |
9177 | .release = io_uring_release, | |
fcb323cc | 9178 | .flush = io_uring_flush, |
2b188cc1 | 9179 | .mmap = io_uring_mmap, |
6c5c240e RP |
9180 | #ifndef CONFIG_MMU |
9181 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9182 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9183 | #endif | |
2b188cc1 JA |
9184 | .poll = io_uring_poll, |
9185 | .fasync = io_uring_fasync, | |
bebdb65e | 9186 | #ifdef CONFIG_PROC_FS |
87ce955b | 9187 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9188 | #endif |
2b188cc1 JA |
9189 | }; |
9190 | ||
9191 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9192 | struct io_uring_params *p) | |
9193 | { | |
75b28aff HV |
9194 | struct io_rings *rings; |
9195 | size_t size, sq_array_offset; | |
2b188cc1 | 9196 | |
bd740481 JA |
9197 | /* make sure these are sane, as we already accounted them */ |
9198 | ctx->sq_entries = p->sq_entries; | |
9199 | ctx->cq_entries = p->cq_entries; | |
9200 | ||
75b28aff HV |
9201 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9202 | if (size == SIZE_MAX) | |
9203 | return -EOVERFLOW; | |
9204 | ||
9205 | rings = io_mem_alloc(size); | |
9206 | if (!rings) | |
2b188cc1 JA |
9207 | return -ENOMEM; |
9208 | ||
75b28aff HV |
9209 | ctx->rings = rings; |
9210 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9211 | rings->sq_ring_mask = p->sq_entries - 1; | |
9212 | rings->cq_ring_mask = p->cq_entries - 1; | |
9213 | rings->sq_ring_entries = p->sq_entries; | |
9214 | rings->cq_ring_entries = p->cq_entries; | |
9215 | ctx->sq_mask = rings->sq_ring_mask; | |
9216 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9217 | |
9218 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9219 | if (size == SIZE_MAX) { |
9220 | io_mem_free(ctx->rings); | |
9221 | ctx->rings = NULL; | |
2b188cc1 | 9222 | return -EOVERFLOW; |
eb065d30 | 9223 | } |
2b188cc1 JA |
9224 | |
9225 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9226 | if (!ctx->sq_sqes) { |
9227 | io_mem_free(ctx->rings); | |
9228 | ctx->rings = NULL; | |
2b188cc1 | 9229 | return -ENOMEM; |
eb065d30 | 9230 | } |
2b188cc1 | 9231 | |
2b188cc1 JA |
9232 | return 0; |
9233 | } | |
9234 | ||
01ceab31 PB |
9235 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9236 | { | |
9237 | int ret, fd; | |
9238 | ||
9239 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9240 | if (fd < 0) | |
9241 | return fd; | |
9242 | ||
9243 | ret = io_uring_add_task_file(ctx, file); | |
9244 | if (ret) { | |
9245 | put_unused_fd(fd); | |
9246 | return ret; | |
9247 | } | |
9248 | fd_install(fd, file); | |
9249 | return fd; | |
9250 | } | |
9251 | ||
2b188cc1 JA |
9252 | /* |
9253 | * Allocate an anonymous fd, this is what constitutes the application | |
9254 | * visible backing of an io_uring instance. The application mmaps this | |
9255 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9256 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9257 | */ | |
01ceab31 | 9258 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9259 | { |
9260 | struct file *file; | |
01ceab31 | 9261 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9262 | int ret; |
9263 | ||
2b188cc1 JA |
9264 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9265 | &ctx->ring_sock); | |
9266 | if (ret) | |
01ceab31 | 9267 | return ERR_PTR(ret); |
2b188cc1 JA |
9268 | #endif |
9269 | ||
2b188cc1 JA |
9270 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9271 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9272 | #if defined(CONFIG_UNIX) |
01ceab31 PB |
9273 | if (IS_ERR(file)) { |
9274 | sock_release(ctx->ring_sock); | |
9275 | ctx->ring_sock = NULL; | |
9276 | } else { | |
9277 | ctx->ring_sock->file = file; | |
0f212204 | 9278 | } |
2b188cc1 | 9279 | #endif |
01ceab31 | 9280 | return file; |
2b188cc1 JA |
9281 | } |
9282 | ||
7f13657d XW |
9283 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9284 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9285 | { |
9286 | struct user_struct *user = NULL; | |
9287 | struct io_ring_ctx *ctx; | |
01ceab31 | 9288 | struct file *file; |
aad5d8da | 9289 | bool limit_mem; |
2b188cc1 JA |
9290 | int ret; |
9291 | ||
8110c1a6 | 9292 | if (!entries) |
2b188cc1 | 9293 | return -EINVAL; |
8110c1a6 JA |
9294 | if (entries > IORING_MAX_ENTRIES) { |
9295 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9296 | return -EINVAL; | |
9297 | entries = IORING_MAX_ENTRIES; | |
9298 | } | |
2b188cc1 JA |
9299 | |
9300 | /* | |
9301 | * Use twice as many entries for the CQ ring. It's possible for the | |
9302 | * application to drive a higher depth than the size of the SQ ring, | |
9303 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9304 | * some flexibility in overcommitting a bit. If the application has |
9305 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9306 | * of CQ ring entries manually. | |
2b188cc1 JA |
9307 | */ |
9308 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9309 | if (p->flags & IORING_SETUP_CQSIZE) { |
9310 | /* | |
9311 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9312 | * to a power-of-two, if it isn't already. We do NOT impose | |
9313 | * any cq vs sq ring sizing. | |
9314 | */ | |
eb2667b3 | 9315 | if (!p->cq_entries) |
33a107f0 | 9316 | return -EINVAL; |
8110c1a6 JA |
9317 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9318 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9319 | return -EINVAL; | |
9320 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9321 | } | |
eb2667b3 JQ |
9322 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9323 | if (p->cq_entries < p->sq_entries) | |
9324 | return -EINVAL; | |
33a107f0 JA |
9325 | } else { |
9326 | p->cq_entries = 2 * p->sq_entries; | |
9327 | } | |
2b188cc1 JA |
9328 | |
9329 | user = get_uid(current_user()); | |
aad5d8da | 9330 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9331 | |
aad5d8da | 9332 | if (limit_mem) { |
a087e2b5 | 9333 | ret = __io_account_mem(user, |
2b188cc1 JA |
9334 | ring_pages(p->sq_entries, p->cq_entries)); |
9335 | if (ret) { | |
9336 | free_uid(user); | |
9337 | return ret; | |
9338 | } | |
9339 | } | |
9340 | ||
9341 | ctx = io_ring_ctx_alloc(p); | |
9342 | if (!ctx) { | |
aad5d8da | 9343 | if (limit_mem) |
a087e2b5 | 9344 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9345 | p->cq_entries)); |
9346 | free_uid(user); | |
9347 | return -ENOMEM; | |
9348 | } | |
9349 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9350 | ctx->user = user; |
0b8c0ec7 | 9351 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9352 | #ifdef CONFIG_AUDIT |
9353 | ctx->loginuid = current->loginuid; | |
9354 | ctx->sessionid = current->sessionid; | |
9355 | #endif | |
2aede0e4 JA |
9356 | ctx->sqo_task = get_task_struct(current); |
9357 | ||
9358 | /* | |
9359 | * This is just grabbed for accounting purposes. When a process exits, | |
9360 | * the mm is exited and dropped before the files, hence we need to hang | |
9361 | * on to this mm purely for the purposes of being able to unaccount | |
9362 | * memory (locked/pinned vm). It's not used for anything else. | |
9363 | */ | |
6b7898eb | 9364 | mmgrab(current->mm); |
2aede0e4 | 9365 | ctx->mm_account = current->mm; |
6b7898eb | 9366 | |
91d8f519 DZ |
9367 | #ifdef CONFIG_BLK_CGROUP |
9368 | /* | |
9369 | * The sq thread will belong to the original cgroup it was inited in. | |
9370 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9371 | * issued bios will be associated with the closest cgroup later in the | |
9372 | * block layer. | |
9373 | */ | |
9374 | rcu_read_lock(); | |
9375 | ctx->sqo_blkcg_css = blkcg_css(); | |
9376 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9377 | rcu_read_unlock(); | |
9378 | if (!ret) { | |
9379 | /* don't init against a dying cgroup, have the user try again */ | |
9380 | ctx->sqo_blkcg_css = NULL; | |
9381 | ret = -ENODEV; | |
9382 | goto err; | |
9383 | } | |
9384 | #endif | |
6b7898eb | 9385 | |
f74441e6 JA |
9386 | /* |
9387 | * Account memory _before_ installing the file descriptor. Once | |
9388 | * the descriptor is installed, it can get closed at any time. Also | |
9389 | * do this before hitting the general error path, as ring freeing | |
9390 | * will un-account as well. | |
9391 | */ | |
9392 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9393 | ACCT_LOCKED); | |
9394 | ctx->limit_mem = limit_mem; | |
9395 | ||
2b188cc1 JA |
9396 | ret = io_allocate_scq_urings(ctx, p); |
9397 | if (ret) | |
9398 | goto err; | |
9399 | ||
7e84e1c7 | 9400 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9401 | if (ret) |
9402 | goto err; | |
9403 | ||
7e84e1c7 SG |
9404 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9405 | io_sq_offload_start(ctx); | |
9406 | ||
2b188cc1 | 9407 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9408 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9409 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9410 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9411 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9412 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9413 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9414 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9415 | |
9416 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9417 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9418 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9419 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9420 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9421 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9422 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9423 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9424 | |
7f13657d XW |
9425 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9426 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 JX |
9427 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
9428 | IORING_FEAT_POLL_32BITS; | |
7f13657d XW |
9429 | |
9430 | if (copy_to_user(params, p, sizeof(*p))) { | |
9431 | ret = -EFAULT; | |
9432 | goto err; | |
9433 | } | |
d1719f70 | 9434 | |
01ceab31 PB |
9435 | file = io_uring_get_file(ctx); |
9436 | if (IS_ERR(file)) { | |
9437 | ret = PTR_ERR(file); | |
9438 | goto err; | |
9439 | } | |
9440 | ||
044c1ab3 JA |
9441 | /* |
9442 | * Install ring fd as the very last thing, so we don't risk someone | |
9443 | * having closed it before we finish setup | |
9444 | */ | |
01ceab31 PB |
9445 | ret = io_uring_install_fd(ctx, file); |
9446 | if (ret < 0) { | |
9447 | /* fput will clean it up */ | |
9448 | fput(file); | |
9449 | return ret; | |
9450 | } | |
044c1ab3 | 9451 | |
c826bd7a | 9452 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9453 | return ret; |
9454 | err: | |
9455 | io_ring_ctx_wait_and_kill(ctx); | |
9456 | return ret; | |
9457 | } | |
9458 | ||
9459 | /* | |
9460 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9461 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9462 | * params structure passed in. | |
9463 | */ | |
9464 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9465 | { | |
9466 | struct io_uring_params p; | |
2b188cc1 JA |
9467 | int i; |
9468 | ||
9469 | if (copy_from_user(&p, params, sizeof(p))) | |
9470 | return -EFAULT; | |
9471 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9472 | if (p.resv[i]) | |
9473 | return -EINVAL; | |
9474 | } | |
9475 | ||
6c271ce2 | 9476 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9477 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9478 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9479 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9480 | return -EINVAL; |
9481 | ||
7f13657d | 9482 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9483 | } |
9484 | ||
9485 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9486 | struct io_uring_params __user *, params) | |
9487 | { | |
9488 | return io_uring_setup(entries, params); | |
9489 | } | |
9490 | ||
66f4af93 JA |
9491 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9492 | { | |
9493 | struct io_uring_probe *p; | |
9494 | size_t size; | |
9495 | int i, ret; | |
9496 | ||
9497 | size = struct_size(p, ops, nr_args); | |
9498 | if (size == SIZE_MAX) | |
9499 | return -EOVERFLOW; | |
9500 | p = kzalloc(size, GFP_KERNEL); | |
9501 | if (!p) | |
9502 | return -ENOMEM; | |
9503 | ||
9504 | ret = -EFAULT; | |
9505 | if (copy_from_user(p, arg, size)) | |
9506 | goto out; | |
9507 | ret = -EINVAL; | |
9508 | if (memchr_inv(p, 0, size)) | |
9509 | goto out; | |
9510 | ||
9511 | p->last_op = IORING_OP_LAST - 1; | |
9512 | if (nr_args > IORING_OP_LAST) | |
9513 | nr_args = IORING_OP_LAST; | |
9514 | ||
9515 | for (i = 0; i < nr_args; i++) { | |
9516 | p->ops[i].op = i; | |
9517 | if (!io_op_defs[i].not_supported) | |
9518 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9519 | } | |
9520 | p->ops_len = i; | |
9521 | ||
9522 | ret = 0; | |
9523 | if (copy_to_user(arg, p, size)) | |
9524 | ret = -EFAULT; | |
9525 | out: | |
9526 | kfree(p); | |
9527 | return ret; | |
9528 | } | |
9529 | ||
071698e1 JA |
9530 | static int io_register_personality(struct io_ring_ctx *ctx) |
9531 | { | |
1e6fa521 JA |
9532 | struct io_identity *id; |
9533 | int ret; | |
071698e1 | 9534 | |
1e6fa521 JA |
9535 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9536 | if (unlikely(!id)) | |
9537 | return -ENOMEM; | |
9538 | ||
9539 | io_init_identity(id); | |
9540 | id->creds = get_current_cred(); | |
9541 | ||
9542 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9543 | if (ret < 0) { | |
9544 | put_cred(id->creds); | |
9545 | kfree(id); | |
9546 | } | |
9547 | return ret; | |
071698e1 JA |
9548 | } |
9549 | ||
9550 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
9551 | { | |
1e6fa521 | 9552 | struct io_identity *iod; |
071698e1 | 9553 | |
1e6fa521 JA |
9554 | iod = idr_remove(&ctx->personality_idr, id); |
9555 | if (iod) { | |
9556 | put_cred(iod->creds); | |
9557 | if (refcount_dec_and_test(&iod->count)) | |
9558 | kfree(iod); | |
071698e1 JA |
9559 | return 0; |
9560 | } | |
9561 | ||
9562 | return -EINVAL; | |
9563 | } | |
9564 | ||
21b55dbc SG |
9565 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9566 | unsigned int nr_args) | |
9567 | { | |
9568 | struct io_uring_restriction *res; | |
9569 | size_t size; | |
9570 | int i, ret; | |
9571 | ||
7e84e1c7 SG |
9572 | /* Restrictions allowed only if rings started disabled */ |
9573 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9574 | return -EBADFD; | |
9575 | ||
21b55dbc | 9576 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9577 | if (ctx->restrictions.registered) |
21b55dbc SG |
9578 | return -EBUSY; |
9579 | ||
9580 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9581 | return -EINVAL; | |
9582 | ||
9583 | size = array_size(nr_args, sizeof(*res)); | |
9584 | if (size == SIZE_MAX) | |
9585 | return -EOVERFLOW; | |
9586 | ||
9587 | res = memdup_user(arg, size); | |
9588 | if (IS_ERR(res)) | |
9589 | return PTR_ERR(res); | |
9590 | ||
9591 | ret = 0; | |
9592 | ||
9593 | for (i = 0; i < nr_args; i++) { | |
9594 | switch (res[i].opcode) { | |
9595 | case IORING_RESTRICTION_REGISTER_OP: | |
9596 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9597 | ret = -EINVAL; | |
9598 | goto out; | |
9599 | } | |
9600 | ||
9601 | __set_bit(res[i].register_op, | |
9602 | ctx->restrictions.register_op); | |
9603 | break; | |
9604 | case IORING_RESTRICTION_SQE_OP: | |
9605 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9606 | ret = -EINVAL; | |
9607 | goto out; | |
9608 | } | |
9609 | ||
9610 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9611 | break; | |
9612 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9613 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9614 | break; | |
9615 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9616 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9617 | break; | |
9618 | default: | |
9619 | ret = -EINVAL; | |
9620 | goto out; | |
9621 | } | |
9622 | } | |
9623 | ||
9624 | out: | |
9625 | /* Reset all restrictions if an error happened */ | |
9626 | if (ret != 0) | |
9627 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9628 | else | |
7e84e1c7 | 9629 | ctx->restrictions.registered = true; |
21b55dbc SG |
9630 | |
9631 | kfree(res); | |
9632 | return ret; | |
9633 | } | |
9634 | ||
7e84e1c7 SG |
9635 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9636 | { | |
9637 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9638 | return -EBADFD; | |
9639 | ||
9640 | if (ctx->restrictions.registered) | |
9641 | ctx->restricted = 1; | |
9642 | ||
9643 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9644 | ||
9645 | io_sq_offload_start(ctx); | |
9646 | ||
9647 | return 0; | |
9648 | } | |
9649 | ||
071698e1 JA |
9650 | static bool io_register_op_must_quiesce(int op) |
9651 | { | |
9652 | switch (op) { | |
9653 | case IORING_UNREGISTER_FILES: | |
9654 | case IORING_REGISTER_FILES_UPDATE: | |
9655 | case IORING_REGISTER_PROBE: | |
9656 | case IORING_REGISTER_PERSONALITY: | |
9657 | case IORING_UNREGISTER_PERSONALITY: | |
9658 | return false; | |
9659 | default: | |
9660 | return true; | |
9661 | } | |
9662 | } | |
9663 | ||
edafccee JA |
9664 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9665 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9666 | __releases(ctx->uring_lock) |
9667 | __acquires(ctx->uring_lock) | |
edafccee JA |
9668 | { |
9669 | int ret; | |
9670 | ||
35fa71a0 JA |
9671 | /* |
9672 | * We're inside the ring mutex, if the ref is already dying, then | |
9673 | * someone else killed the ctx or is already going through | |
9674 | * io_uring_register(). | |
9675 | */ | |
9676 | if (percpu_ref_is_dying(&ctx->refs)) | |
9677 | return -ENXIO; | |
9678 | ||
071698e1 | 9679 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9680 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9681 | |
05f3fb3c JA |
9682 | /* |
9683 | * Drop uring mutex before waiting for references to exit. If | |
9684 | * another thread is currently inside io_uring_enter() it might | |
9685 | * need to grab the uring_lock to make progress. If we hold it | |
9686 | * here across the drain wait, then we can deadlock. It's safe | |
9687 | * to drop the mutex here, since no new references will come in | |
9688 | * after we've killed the percpu ref. | |
9689 | */ | |
9690 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9691 | do { |
9692 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9693 | if (!ret) | |
9694 | break; | |
ed6930c9 JA |
9695 | ret = io_run_task_work_sig(); |
9696 | if (ret < 0) | |
9697 | break; | |
af9c1a44 JA |
9698 | } while (1); |
9699 | ||
05f3fb3c | 9700 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9701 | |
c150368b JA |
9702 | if (ret) { |
9703 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
9704 | goto out_quiesce; |
9705 | } | |
9706 | } | |
9707 | ||
9708 | if (ctx->restricted) { | |
9709 | if (opcode >= IORING_REGISTER_LAST) { | |
9710 | ret = -EINVAL; | |
9711 | goto out; | |
9712 | } | |
9713 | ||
9714 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9715 | ret = -EACCES; | |
c150368b JA |
9716 | goto out; |
9717 | } | |
05f3fb3c | 9718 | } |
edafccee JA |
9719 | |
9720 | switch (opcode) { | |
9721 | case IORING_REGISTER_BUFFERS: | |
9722 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
9723 | break; | |
9724 | case IORING_UNREGISTER_BUFFERS: | |
9725 | ret = -EINVAL; | |
9726 | if (arg || nr_args) | |
9727 | break; | |
9728 | ret = io_sqe_buffer_unregister(ctx); | |
9729 | break; | |
6b06314c JA |
9730 | case IORING_REGISTER_FILES: |
9731 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9732 | break; | |
9733 | case IORING_UNREGISTER_FILES: | |
9734 | ret = -EINVAL; | |
9735 | if (arg || nr_args) | |
9736 | break; | |
9737 | ret = io_sqe_files_unregister(ctx); | |
9738 | break; | |
c3a31e60 JA |
9739 | case IORING_REGISTER_FILES_UPDATE: |
9740 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9741 | break; | |
9b402849 | 9742 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9743 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9744 | ret = -EINVAL; |
9745 | if (nr_args != 1) | |
9746 | break; | |
9747 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9748 | if (ret) |
9749 | break; | |
9750 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9751 | ctx->eventfd_async = 1; | |
9752 | else | |
9753 | ctx->eventfd_async = 0; | |
9b402849 JA |
9754 | break; |
9755 | case IORING_UNREGISTER_EVENTFD: | |
9756 | ret = -EINVAL; | |
9757 | if (arg || nr_args) | |
9758 | break; | |
9759 | ret = io_eventfd_unregister(ctx); | |
9760 | break; | |
66f4af93 JA |
9761 | case IORING_REGISTER_PROBE: |
9762 | ret = -EINVAL; | |
9763 | if (!arg || nr_args > 256) | |
9764 | break; | |
9765 | ret = io_probe(ctx, arg, nr_args); | |
9766 | break; | |
071698e1 JA |
9767 | case IORING_REGISTER_PERSONALITY: |
9768 | ret = -EINVAL; | |
9769 | if (arg || nr_args) | |
9770 | break; | |
9771 | ret = io_register_personality(ctx); | |
9772 | break; | |
9773 | case IORING_UNREGISTER_PERSONALITY: | |
9774 | ret = -EINVAL; | |
9775 | if (arg) | |
9776 | break; | |
9777 | ret = io_unregister_personality(ctx, nr_args); | |
9778 | break; | |
7e84e1c7 SG |
9779 | case IORING_REGISTER_ENABLE_RINGS: |
9780 | ret = -EINVAL; | |
9781 | if (arg || nr_args) | |
9782 | break; | |
9783 | ret = io_register_enable_rings(ctx); | |
9784 | break; | |
21b55dbc SG |
9785 | case IORING_REGISTER_RESTRICTIONS: |
9786 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9787 | break; | |
edafccee JA |
9788 | default: |
9789 | ret = -EINVAL; | |
9790 | break; | |
9791 | } | |
9792 | ||
21b55dbc | 9793 | out: |
071698e1 | 9794 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9795 | /* bring the ctx back to life */ |
05f3fb3c | 9796 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 9797 | out_quiesce: |
0f158b4c | 9798 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9799 | } |
edafccee JA |
9800 | return ret; |
9801 | } | |
9802 | ||
9803 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9804 | void __user *, arg, unsigned int, nr_args) | |
9805 | { | |
9806 | struct io_ring_ctx *ctx; | |
9807 | long ret = -EBADF; | |
9808 | struct fd f; | |
9809 | ||
9810 | f = fdget(fd); | |
9811 | if (!f.file) | |
9812 | return -EBADF; | |
9813 | ||
9814 | ret = -EOPNOTSUPP; | |
9815 | if (f.file->f_op != &io_uring_fops) | |
9816 | goto out_fput; | |
9817 | ||
9818 | ctx = f.file->private_data; | |
9819 | ||
9820 | mutex_lock(&ctx->uring_lock); | |
9821 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9822 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9823 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9824 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9825 | out_fput: |
9826 | fdput(f); | |
9827 | return ret; | |
9828 | } | |
9829 | ||
2b188cc1 JA |
9830 | static int __init io_uring_init(void) |
9831 | { | |
d7f62e82 SM |
9832 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9833 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9834 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9835 | } while (0) | |
9836 | ||
9837 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9838 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9839 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9840 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9841 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9842 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9843 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9844 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9845 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9846 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9847 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9848 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9849 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9850 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9851 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9852 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9853 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9854 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9855 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9856 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9857 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9858 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9859 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9860 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9861 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9862 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9863 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9864 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9865 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9866 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9867 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9868 | |
d3656344 | 9869 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9870 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
9871 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
9872 | return 0; | |
9873 | }; | |
9874 | __initcall(io_uring_init); |