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> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
95 | |
96 | /* | |
97 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
98 | */ | |
99 | #define IORING_FILE_TABLE_SHIFT 9 | |
100 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
101 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
102 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
103 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
104 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 JA |
105 | |
106 | struct io_uring { | |
107 | u32 head ____cacheline_aligned_in_smp; | |
108 | u32 tail ____cacheline_aligned_in_smp; | |
109 | }; | |
110 | ||
1e84b97b | 111 | /* |
75b28aff HV |
112 | * This data is shared with the application through the mmap at offsets |
113 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
114 | * |
115 | * The offsets to the member fields are published through struct | |
116 | * io_sqring_offsets when calling io_uring_setup. | |
117 | */ | |
75b28aff | 118 | struct io_rings { |
1e84b97b SB |
119 | /* |
120 | * Head and tail offsets into the ring; the offsets need to be | |
121 | * masked to get valid indices. | |
122 | * | |
75b28aff HV |
123 | * The kernel controls head of the sq ring and the tail of the cq ring, |
124 | * and the application controls tail of the sq ring and the head of the | |
125 | * cq ring. | |
1e84b97b | 126 | */ |
75b28aff | 127 | struct io_uring sq, cq; |
1e84b97b | 128 | /* |
75b28aff | 129 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
130 | * ring_entries - 1) |
131 | */ | |
75b28aff HV |
132 | u32 sq_ring_mask, cq_ring_mask; |
133 | /* Ring sizes (constant, power of 2) */ | |
134 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
135 | /* |
136 | * Number of invalid entries dropped by the kernel due to | |
137 | * invalid index stored in array | |
138 | * | |
139 | * Written by the kernel, shouldn't be modified by the | |
140 | * application (i.e. get number of "new events" by comparing to | |
141 | * cached value). | |
142 | * | |
143 | * After a new SQ head value was read by the application this | |
144 | * counter includes all submissions that were dropped reaching | |
145 | * the new SQ head (and possibly more). | |
146 | */ | |
75b28aff | 147 | u32 sq_dropped; |
1e84b97b | 148 | /* |
0d9b5b3a | 149 | * Runtime SQ flags |
1e84b97b SB |
150 | * |
151 | * Written by the kernel, shouldn't be modified by the | |
152 | * application. | |
153 | * | |
154 | * The application needs a full memory barrier before checking | |
155 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
156 | */ | |
75b28aff | 157 | u32 sq_flags; |
0d9b5b3a SG |
158 | /* |
159 | * Runtime CQ flags | |
160 | * | |
161 | * Written by the application, shouldn't be modified by the | |
162 | * kernel. | |
163 | */ | |
164 | u32 cq_flags; | |
1e84b97b SB |
165 | /* |
166 | * Number of completion events lost because the queue was full; | |
167 | * this should be avoided by the application by making sure | |
0b4295b5 | 168 | * there are not more requests pending than there is space in |
1e84b97b SB |
169 | * the completion queue. |
170 | * | |
171 | * Written by the kernel, shouldn't be modified by the | |
172 | * application (i.e. get number of "new events" by comparing to | |
173 | * cached value). | |
174 | * | |
175 | * As completion events come in out of order this counter is not | |
176 | * ordered with any other data. | |
177 | */ | |
75b28aff | 178 | u32 cq_overflow; |
1e84b97b SB |
179 | /* |
180 | * Ring buffer of completion events. | |
181 | * | |
182 | * The kernel writes completion events fresh every time they are | |
183 | * produced, so the application is allowed to modify pending | |
184 | * entries. | |
185 | */ | |
75b28aff | 186 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
187 | }; |
188 | ||
edafccee JA |
189 | struct io_mapped_ubuf { |
190 | u64 ubuf; | |
191 | size_t len; | |
192 | struct bio_vec *bvec; | |
193 | unsigned int nr_bvecs; | |
de293938 | 194 | unsigned long acct_pages; |
edafccee JA |
195 | }; |
196 | ||
65e19f54 JA |
197 | struct fixed_file_table { |
198 | struct file **files; | |
31b51510 JA |
199 | }; |
200 | ||
05589553 XW |
201 | struct fixed_file_ref_node { |
202 | struct percpu_ref refs; | |
203 | struct list_head node; | |
204 | struct list_head file_list; | |
205 | struct fixed_file_data *file_data; | |
4a38aed2 | 206 | struct llist_node llist; |
05589553 XW |
207 | }; |
208 | ||
05f3fb3c JA |
209 | struct fixed_file_data { |
210 | struct fixed_file_table *table; | |
211 | struct io_ring_ctx *ctx; | |
212 | ||
05589553 | 213 | struct percpu_ref *cur_refs; |
05f3fb3c | 214 | struct percpu_ref refs; |
05f3fb3c | 215 | struct completion done; |
05589553 XW |
216 | struct list_head ref_list; |
217 | spinlock_t lock; | |
05f3fb3c JA |
218 | }; |
219 | ||
5a2e745d JA |
220 | struct io_buffer { |
221 | struct list_head list; | |
222 | __u64 addr; | |
223 | __s32 len; | |
224 | __u16 bid; | |
225 | }; | |
226 | ||
21b55dbc SG |
227 | struct io_restriction { |
228 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
229 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
230 | u8 sqe_flags_allowed; | |
231 | u8 sqe_flags_required; | |
7e84e1c7 | 232 | bool registered; |
21b55dbc SG |
233 | }; |
234 | ||
534ca6d6 JA |
235 | struct io_sq_data { |
236 | refcount_t refs; | |
69fb2131 JA |
237 | struct mutex lock; |
238 | ||
239 | /* ctx's that are using this sqd */ | |
240 | struct list_head ctx_list; | |
241 | struct list_head ctx_new_list; | |
242 | struct mutex ctx_lock; | |
243 | ||
534ca6d6 JA |
244 | struct task_struct *thread; |
245 | struct wait_queue_head wait; | |
246 | }; | |
247 | ||
2b188cc1 JA |
248 | struct io_ring_ctx { |
249 | struct { | |
250 | struct percpu_ref refs; | |
251 | } ____cacheline_aligned_in_smp; | |
252 | ||
253 | struct { | |
254 | unsigned int flags; | |
e1d85334 | 255 | unsigned int compat: 1; |
aad5d8da | 256 | unsigned int limit_mem: 1; |
e1d85334 RD |
257 | unsigned int cq_overflow_flushed: 1; |
258 | unsigned int drain_next: 1; | |
259 | unsigned int eventfd_async: 1; | |
21b55dbc | 260 | unsigned int restricted: 1; |
2b188cc1 | 261 | |
75b28aff HV |
262 | /* |
263 | * Ring buffer of indices into array of io_uring_sqe, which is | |
264 | * mmapped by the application using the IORING_OFF_SQES offset. | |
265 | * | |
266 | * This indirection could e.g. be used to assign fixed | |
267 | * io_uring_sqe entries to operations and only submit them to | |
268 | * the queue when needed. | |
269 | * | |
270 | * The kernel modifies neither the indices array nor the entries | |
271 | * array. | |
272 | */ | |
273 | u32 *sq_array; | |
2b188cc1 JA |
274 | unsigned cached_sq_head; |
275 | unsigned sq_entries; | |
276 | unsigned sq_mask; | |
6c271ce2 | 277 | unsigned sq_thread_idle; |
498ccd9e | 278 | unsigned cached_sq_dropped; |
206aefde | 279 | atomic_t cached_cq_overflow; |
ad3eb2c8 | 280 | unsigned long sq_check_overflow; |
de0617e4 JA |
281 | |
282 | struct list_head defer_list; | |
5262f567 | 283 | struct list_head timeout_list; |
1d7bb1d5 | 284 | struct list_head cq_overflow_list; |
fcb323cc JA |
285 | |
286 | wait_queue_head_t inflight_wait; | |
ad3eb2c8 | 287 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
288 | } ____cacheline_aligned_in_smp; |
289 | ||
206aefde JA |
290 | struct io_rings *rings; |
291 | ||
2b188cc1 | 292 | /* IO offload */ |
561fb04a | 293 | struct io_wq *io_wq; |
2aede0e4 JA |
294 | |
295 | /* | |
296 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
297 | * have access to the ->files | |
298 | */ | |
299 | struct task_struct *sqo_task; | |
300 | ||
301 | /* Only used for accounting purposes */ | |
302 | struct mm_struct *mm_account; | |
303 | ||
91d8f519 DZ |
304 | #ifdef CONFIG_BLK_CGROUP |
305 | struct cgroup_subsys_state *sqo_blkcg_css; | |
306 | #endif | |
307 | ||
534ca6d6 JA |
308 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
309 | ||
90554200 | 310 | struct wait_queue_head sqo_sq_wait; |
6a779382 | 311 | struct wait_queue_entry sqo_wait_entry; |
69fb2131 | 312 | struct list_head sqd_list; |
75b28aff | 313 | |
6b06314c JA |
314 | /* |
315 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
316 | * readers must ensure that ->refs is alive as long as the file* is | |
317 | * used. Only updated through io_uring_register(2). | |
318 | */ | |
05f3fb3c | 319 | struct fixed_file_data *file_data; |
6b06314c JA |
320 | unsigned nr_user_files; |
321 | ||
edafccee JA |
322 | /* if used, fixed mapped user buffers */ |
323 | unsigned nr_user_bufs; | |
324 | struct io_mapped_ubuf *user_bufs; | |
325 | ||
2b188cc1 JA |
326 | struct user_struct *user; |
327 | ||
0b8c0ec7 | 328 | const struct cred *creds; |
181e448d | 329 | |
0f158b4c JA |
330 | struct completion ref_comp; |
331 | struct completion sq_thread_comp; | |
206aefde | 332 | |
0ddf92e8 JA |
333 | /* if all else fails... */ |
334 | struct io_kiocb *fallback_req; | |
335 | ||
206aefde JA |
336 | #if defined(CONFIG_UNIX) |
337 | struct socket *ring_sock; | |
338 | #endif | |
339 | ||
5a2e745d JA |
340 | struct idr io_buffer_idr; |
341 | ||
071698e1 JA |
342 | struct idr personality_idr; |
343 | ||
206aefde JA |
344 | struct { |
345 | unsigned cached_cq_tail; | |
346 | unsigned cq_entries; | |
347 | unsigned cq_mask; | |
348 | atomic_t cq_timeouts; | |
ad3eb2c8 | 349 | unsigned long cq_check_overflow; |
206aefde JA |
350 | struct wait_queue_head cq_wait; |
351 | struct fasync_struct *cq_fasync; | |
352 | struct eventfd_ctx *cq_ev_fd; | |
353 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
354 | |
355 | struct { | |
356 | struct mutex uring_lock; | |
357 | wait_queue_head_t wait; | |
358 | } ____cacheline_aligned_in_smp; | |
359 | ||
360 | struct { | |
361 | spinlock_t completion_lock; | |
e94f141b | 362 | |
def596e9 | 363 | /* |
540e32a0 | 364 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
365 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
366 | * For SQPOLL, only the single threaded io_sq_thread() will | |
367 | * manipulate the list, hence no extra locking is needed there. | |
368 | */ | |
540e32a0 | 369 | struct list_head iopoll_list; |
78076bb6 JA |
370 | struct hlist_head *cancel_hash; |
371 | unsigned cancel_hash_bits; | |
e94f141b | 372 | bool poll_multi_file; |
31b51510 | 373 | |
fcb323cc JA |
374 | spinlock_t inflight_lock; |
375 | struct list_head inflight_list; | |
2b188cc1 | 376 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 377 | |
4a38aed2 JA |
378 | struct delayed_work file_put_work; |
379 | struct llist_head file_put_llist; | |
380 | ||
85faa7b8 | 381 | struct work_struct exit_work; |
21b55dbc | 382 | struct io_restriction restrictions; |
2b188cc1 JA |
383 | }; |
384 | ||
09bb8394 JA |
385 | /* |
386 | * First field must be the file pointer in all the | |
387 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
388 | */ | |
221c5eb2 JA |
389 | struct io_poll_iocb { |
390 | struct file *file; | |
0969e783 JA |
391 | union { |
392 | struct wait_queue_head *head; | |
393 | u64 addr; | |
394 | }; | |
221c5eb2 | 395 | __poll_t events; |
8c838788 | 396 | bool done; |
221c5eb2 | 397 | bool canceled; |
392edb45 | 398 | struct wait_queue_entry wait; |
221c5eb2 JA |
399 | }; |
400 | ||
b5dba59e JA |
401 | struct io_close { |
402 | struct file *file; | |
403 | struct file *put_file; | |
404 | int fd; | |
405 | }; | |
406 | ||
ad8a48ac JA |
407 | struct io_timeout_data { |
408 | struct io_kiocb *req; | |
409 | struct hrtimer timer; | |
410 | struct timespec64 ts; | |
411 | enum hrtimer_mode mode; | |
412 | }; | |
413 | ||
8ed8d3c3 JA |
414 | struct io_accept { |
415 | struct file *file; | |
416 | struct sockaddr __user *addr; | |
417 | int __user *addr_len; | |
418 | int flags; | |
09952e3e | 419 | unsigned long nofile; |
8ed8d3c3 JA |
420 | }; |
421 | ||
422 | struct io_sync { | |
423 | struct file *file; | |
424 | loff_t len; | |
425 | loff_t off; | |
426 | int flags; | |
d63d1b5e | 427 | int mode; |
8ed8d3c3 JA |
428 | }; |
429 | ||
fbf23849 JA |
430 | struct io_cancel { |
431 | struct file *file; | |
432 | u64 addr; | |
433 | }; | |
434 | ||
b29472ee JA |
435 | struct io_timeout { |
436 | struct file *file; | |
437 | u64 addr; | |
438 | int flags; | |
bfe68a22 PB |
439 | u32 off; |
440 | u32 target_seq; | |
135fcde8 | 441 | struct list_head list; |
b29472ee JA |
442 | }; |
443 | ||
9adbd45d JA |
444 | struct io_rw { |
445 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
446 | struct kiocb kiocb; | |
447 | u64 addr; | |
448 | u64 len; | |
449 | }; | |
450 | ||
3fbb51c1 JA |
451 | struct io_connect { |
452 | struct file *file; | |
453 | struct sockaddr __user *addr; | |
454 | int addr_len; | |
455 | }; | |
456 | ||
e47293fd JA |
457 | struct io_sr_msg { |
458 | struct file *file; | |
fddaface | 459 | union { |
270a5940 | 460 | struct user_msghdr __user *umsg; |
fddaface JA |
461 | void __user *buf; |
462 | }; | |
e47293fd | 463 | int msg_flags; |
bcda7baa | 464 | int bgid; |
fddaface | 465 | size_t len; |
bcda7baa | 466 | struct io_buffer *kbuf; |
e47293fd JA |
467 | }; |
468 | ||
15b71abe JA |
469 | struct io_open { |
470 | struct file *file; | |
471 | int dfd; | |
15b71abe | 472 | struct filename *filename; |
c12cedf2 | 473 | struct open_how how; |
4022e7af | 474 | unsigned long nofile; |
15b71abe JA |
475 | }; |
476 | ||
05f3fb3c JA |
477 | struct io_files_update { |
478 | struct file *file; | |
479 | u64 arg; | |
480 | u32 nr_args; | |
481 | u32 offset; | |
482 | }; | |
483 | ||
4840e418 JA |
484 | struct io_fadvise { |
485 | struct file *file; | |
486 | u64 offset; | |
487 | u32 len; | |
488 | u32 advice; | |
489 | }; | |
490 | ||
c1ca757b JA |
491 | struct io_madvise { |
492 | struct file *file; | |
493 | u64 addr; | |
494 | u32 len; | |
495 | u32 advice; | |
496 | }; | |
497 | ||
3e4827b0 JA |
498 | struct io_epoll { |
499 | struct file *file; | |
500 | int epfd; | |
501 | int op; | |
502 | int fd; | |
503 | struct epoll_event event; | |
e47293fd JA |
504 | }; |
505 | ||
7d67af2c PB |
506 | struct io_splice { |
507 | struct file *file_out; | |
508 | struct file *file_in; | |
509 | loff_t off_out; | |
510 | loff_t off_in; | |
511 | u64 len; | |
512 | unsigned int flags; | |
513 | }; | |
514 | ||
ddf0322d JA |
515 | struct io_provide_buf { |
516 | struct file *file; | |
517 | __u64 addr; | |
518 | __s32 len; | |
519 | __u32 bgid; | |
520 | __u16 nbufs; | |
521 | __u16 bid; | |
522 | }; | |
523 | ||
1d9e1288 BM |
524 | struct io_statx { |
525 | struct file *file; | |
526 | int dfd; | |
527 | unsigned int mask; | |
528 | unsigned int flags; | |
e62753e4 | 529 | const char __user *filename; |
1d9e1288 BM |
530 | struct statx __user *buffer; |
531 | }; | |
532 | ||
3ca405eb PB |
533 | struct io_completion { |
534 | struct file *file; | |
535 | struct list_head list; | |
0f7e466b | 536 | int cflags; |
3ca405eb PB |
537 | }; |
538 | ||
f499a021 JA |
539 | struct io_async_connect { |
540 | struct sockaddr_storage address; | |
541 | }; | |
542 | ||
03b1230c JA |
543 | struct io_async_msghdr { |
544 | struct iovec fast_iov[UIO_FASTIOV]; | |
545 | struct iovec *iov; | |
546 | struct sockaddr __user *uaddr; | |
547 | struct msghdr msg; | |
b537916c | 548 | struct sockaddr_storage addr; |
03b1230c JA |
549 | }; |
550 | ||
f67676d1 JA |
551 | struct io_async_rw { |
552 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
553 | const struct iovec *free_iovec; |
554 | struct iov_iter iter; | |
227c0c96 | 555 | size_t bytes_done; |
bcf5a063 | 556 | struct wait_page_queue wpq; |
f67676d1 JA |
557 | }; |
558 | ||
6b47ee6e PB |
559 | enum { |
560 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
561 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
562 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
563 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
564 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 565 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 566 | |
dea3b49c | 567 | REQ_F_LINK_HEAD_BIT, |
6b47ee6e PB |
568 | REQ_F_FAIL_LINK_BIT, |
569 | REQ_F_INFLIGHT_BIT, | |
570 | REQ_F_CUR_POS_BIT, | |
571 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 572 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 573 | REQ_F_ISREG_BIT, |
6b47ee6e | 574 | REQ_F_COMP_LOCKED_BIT, |
99bc4c38 | 575 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 576 | REQ_F_POLLED_BIT, |
bcda7baa | 577 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 578 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 579 | REQ_F_WORK_INITIALIZED_BIT, |
84557871 JA |
580 | |
581 | /* not a real bit, just to check we're not overflowing the space */ | |
582 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
583 | }; |
584 | ||
585 | enum { | |
586 | /* ctx owns file */ | |
587 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
588 | /* drain existing IO first */ | |
589 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
590 | /* linked sqes */ | |
591 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
592 | /* doesn't sever on completion < 0 */ | |
593 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
594 | /* IOSQE_ASYNC */ | |
595 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
596 | /* IOSQE_BUFFER_SELECT */ |
597 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 598 | |
dea3b49c PB |
599 | /* head of a link */ |
600 | REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), | |
6b47ee6e PB |
601 | /* fail rest of links */ |
602 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
603 | /* on inflight list */ | |
604 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
605 | /* read/write uses file position */ | |
606 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
607 | /* must not punt to workers */ | |
608 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
6b47ee6e PB |
609 | /* has linked timeout */ |
610 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), | |
6b47ee6e PB |
611 | /* regular file */ |
612 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
6b47ee6e PB |
613 | /* completion under lock */ |
614 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), | |
99bc4c38 PB |
615 | /* needs cleanup */ |
616 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
617 | /* already went through poll handler */ |
618 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
619 | /* buffer already selected */ |
620 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
621 | /* doesn't need file table for this request */ |
622 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
623 | /* io_wq_work is initialized */ |
624 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
d7718a9d JA |
625 | }; |
626 | ||
627 | struct async_poll { | |
628 | struct io_poll_iocb poll; | |
807abcb0 | 629 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
630 | }; |
631 | ||
09bb8394 JA |
632 | /* |
633 | * NOTE! Each of the iocb union members has the file pointer | |
634 | * as the first entry in their struct definition. So you can | |
635 | * access the file pointer through any of the sub-structs, | |
636 | * or directly as just 'ki_filp' in this struct. | |
637 | */ | |
2b188cc1 | 638 | struct io_kiocb { |
221c5eb2 | 639 | union { |
09bb8394 | 640 | struct file *file; |
9adbd45d | 641 | struct io_rw rw; |
221c5eb2 | 642 | struct io_poll_iocb poll; |
8ed8d3c3 JA |
643 | struct io_accept accept; |
644 | struct io_sync sync; | |
fbf23849 | 645 | struct io_cancel cancel; |
b29472ee | 646 | struct io_timeout timeout; |
3fbb51c1 | 647 | struct io_connect connect; |
e47293fd | 648 | struct io_sr_msg sr_msg; |
15b71abe | 649 | struct io_open open; |
b5dba59e | 650 | struct io_close close; |
05f3fb3c | 651 | struct io_files_update files_update; |
4840e418 | 652 | struct io_fadvise fadvise; |
c1ca757b | 653 | struct io_madvise madvise; |
3e4827b0 | 654 | struct io_epoll epoll; |
7d67af2c | 655 | struct io_splice splice; |
ddf0322d | 656 | struct io_provide_buf pbuf; |
1d9e1288 | 657 | struct io_statx statx; |
3ca405eb PB |
658 | /* use only after cleaning per-op data, see io_clean_op() */ |
659 | struct io_completion compl; | |
221c5eb2 | 660 | }; |
2b188cc1 | 661 | |
e8c2bc1f JA |
662 | /* opcode allocated if it needs to store data for async defer */ |
663 | void *async_data; | |
d625c6ee | 664 | u8 opcode; |
65a6543d XW |
665 | /* polled IO has completed */ |
666 | u8 iopoll_completed; | |
2b188cc1 | 667 | |
4f4eeba8 | 668 | u16 buf_index; |
9cf7c104 | 669 | u32 result; |
4f4eeba8 | 670 | |
010e8e6b PB |
671 | struct io_ring_ctx *ctx; |
672 | unsigned int flags; | |
673 | refcount_t refs; | |
674 | struct task_struct *task; | |
675 | u64 user_data; | |
d7718a9d | 676 | |
010e8e6b | 677 | struct list_head link_list; |
fcb323cc | 678 | |
d21ffe7e PB |
679 | /* |
680 | * 1. used with ctx->iopoll_list with reads/writes | |
681 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
682 | */ | |
010e8e6b PB |
683 | struct list_head inflight_entry; |
684 | ||
685 | struct percpu_ref *fixed_file_refs; | |
686 | struct callback_head task_work; | |
687 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
688 | struct hlist_node hash_node; | |
689 | struct async_poll *apoll; | |
690 | struct io_wq_work work; | |
2b188cc1 | 691 | }; |
05589553 | 692 | |
27dc8338 PB |
693 | struct io_defer_entry { |
694 | struct list_head list; | |
695 | struct io_kiocb *req; | |
9cf7c104 | 696 | u32 seq; |
2b188cc1 JA |
697 | }; |
698 | ||
def596e9 | 699 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 700 | |
013538bd JA |
701 | struct io_comp_state { |
702 | unsigned int nr; | |
703 | struct list_head list; | |
704 | struct io_ring_ctx *ctx; | |
705 | }; | |
706 | ||
9a56a232 JA |
707 | struct io_submit_state { |
708 | struct blk_plug plug; | |
709 | ||
2579f913 JA |
710 | /* |
711 | * io_kiocb alloc cache | |
712 | */ | |
713 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 714 | unsigned int free_reqs; |
2579f913 | 715 | |
013538bd JA |
716 | /* |
717 | * Batch completion logic | |
718 | */ | |
719 | struct io_comp_state comp; | |
720 | ||
9a56a232 JA |
721 | /* |
722 | * File reference cache | |
723 | */ | |
724 | struct file *file; | |
725 | unsigned int fd; | |
726 | unsigned int has_refs; | |
9a56a232 JA |
727 | unsigned int ios_left; |
728 | }; | |
729 | ||
d3656344 | 730 | struct io_op_def { |
d3656344 JA |
731 | /* needs current->mm setup, does mm access */ |
732 | unsigned needs_mm : 1; | |
733 | /* needs req->file assigned */ | |
734 | unsigned needs_file : 1; | |
fd2206e4 JA |
735 | /* don't fail if file grab fails */ |
736 | unsigned needs_file_no_error : 1; | |
d3656344 JA |
737 | /* hash wq insertion if file is a regular file */ |
738 | unsigned hash_reg_file : 1; | |
739 | /* unbound wq insertion if file is a non-regular file */ | |
740 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
741 | /* opcode is not supported by this kernel */ |
742 | unsigned not_supported : 1; | |
f86cd20c JA |
743 | /* needs file table */ |
744 | unsigned file_table : 1; | |
ff002b30 JA |
745 | /* needs ->fs */ |
746 | unsigned needs_fs : 1; | |
8a72758c JA |
747 | /* set if opcode supports polled "wait" */ |
748 | unsigned pollin : 1; | |
749 | unsigned pollout : 1; | |
bcda7baa JA |
750 | /* op supports buffer selection */ |
751 | unsigned buffer_select : 1; | |
e8c2bc1f | 752 | /* needs rlimit(RLIMIT_FSIZE) assigned */ |
57f1a649 | 753 | unsigned needs_fsize : 1; |
e8c2bc1f JA |
754 | /* must always have async data allocated */ |
755 | unsigned needs_async_data : 1; | |
91d8f519 DZ |
756 | /* needs blkcg context, issues async io potentially */ |
757 | unsigned needs_blkcg : 1; | |
e8c2bc1f JA |
758 | /* size of async data needed, if any */ |
759 | unsigned short async_size; | |
d3656344 JA |
760 | }; |
761 | ||
738277ad | 762 | static const struct io_op_def io_op_defs[] __read_mostly = { |
0463b6c5 PB |
763 | [IORING_OP_NOP] = {}, |
764 | [IORING_OP_READV] = { | |
d3656344 JA |
765 | .needs_mm = 1, |
766 | .needs_file = 1, | |
767 | .unbound_nonreg_file = 1, | |
8a72758c | 768 | .pollin = 1, |
4d954c25 | 769 | .buffer_select = 1, |
e8c2bc1f | 770 | .needs_async_data = 1, |
91d8f519 | 771 | .needs_blkcg = 1, |
e8c2bc1f | 772 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 773 | }, |
0463b6c5 | 774 | [IORING_OP_WRITEV] = { |
d3656344 JA |
775 | .needs_mm = 1, |
776 | .needs_file = 1, | |
777 | .hash_reg_file = 1, | |
778 | .unbound_nonreg_file = 1, | |
8a72758c | 779 | .pollout = 1, |
57f1a649 | 780 | .needs_fsize = 1, |
e8c2bc1f | 781 | .needs_async_data = 1, |
91d8f519 | 782 | .needs_blkcg = 1, |
e8c2bc1f | 783 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 784 | }, |
0463b6c5 | 785 | [IORING_OP_FSYNC] = { |
d3656344 | 786 | .needs_file = 1, |
91d8f519 | 787 | .needs_blkcg = 1, |
d3656344 | 788 | }, |
0463b6c5 | 789 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
790 | .needs_file = 1, |
791 | .unbound_nonreg_file = 1, | |
8a72758c | 792 | .pollin = 1, |
91d8f519 | 793 | .needs_blkcg = 1, |
e8c2bc1f | 794 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 795 | }, |
0463b6c5 | 796 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
797 | .needs_file = 1, |
798 | .hash_reg_file = 1, | |
799 | .unbound_nonreg_file = 1, | |
8a72758c | 800 | .pollout = 1, |
57f1a649 | 801 | .needs_fsize = 1, |
91d8f519 | 802 | .needs_blkcg = 1, |
e8c2bc1f | 803 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 804 | }, |
0463b6c5 | 805 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
806 | .needs_file = 1, |
807 | .unbound_nonreg_file = 1, | |
808 | }, | |
0463b6c5 PB |
809 | [IORING_OP_POLL_REMOVE] = {}, |
810 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 811 | .needs_file = 1, |
91d8f519 | 812 | .needs_blkcg = 1, |
d3656344 | 813 | }, |
0463b6c5 | 814 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
815 | .needs_mm = 1, |
816 | .needs_file = 1, | |
817 | .unbound_nonreg_file = 1, | |
ff002b30 | 818 | .needs_fs = 1, |
8a72758c | 819 | .pollout = 1, |
e8c2bc1f | 820 | .needs_async_data = 1, |
91d8f519 | 821 | .needs_blkcg = 1, |
e8c2bc1f | 822 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 823 | }, |
0463b6c5 | 824 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
825 | .needs_mm = 1, |
826 | .needs_file = 1, | |
827 | .unbound_nonreg_file = 1, | |
ff002b30 | 828 | .needs_fs = 1, |
8a72758c | 829 | .pollin = 1, |
52de1fe1 | 830 | .buffer_select = 1, |
e8c2bc1f | 831 | .needs_async_data = 1, |
91d8f519 | 832 | .needs_blkcg = 1, |
e8c2bc1f | 833 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 834 | }, |
0463b6c5 | 835 | [IORING_OP_TIMEOUT] = { |
d3656344 | 836 | .needs_mm = 1, |
e8c2bc1f JA |
837 | .needs_async_data = 1, |
838 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 839 | }, |
0463b6c5 PB |
840 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
841 | [IORING_OP_ACCEPT] = { | |
d3656344 JA |
842 | .needs_mm = 1, |
843 | .needs_file = 1, | |
844 | .unbound_nonreg_file = 1, | |
f86cd20c | 845 | .file_table = 1, |
8a72758c | 846 | .pollin = 1, |
d3656344 | 847 | }, |
0463b6c5 PB |
848 | [IORING_OP_ASYNC_CANCEL] = {}, |
849 | [IORING_OP_LINK_TIMEOUT] = { | |
d3656344 | 850 | .needs_mm = 1, |
e8c2bc1f JA |
851 | .needs_async_data = 1, |
852 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 853 | }, |
0463b6c5 | 854 | [IORING_OP_CONNECT] = { |
d3656344 JA |
855 | .needs_mm = 1, |
856 | .needs_file = 1, | |
857 | .unbound_nonreg_file = 1, | |
8a72758c | 858 | .pollout = 1, |
e8c2bc1f JA |
859 | .needs_async_data = 1, |
860 | .async_size = sizeof(struct io_async_connect), | |
d3656344 | 861 | }, |
0463b6c5 | 862 | [IORING_OP_FALLOCATE] = { |
d3656344 | 863 | .needs_file = 1, |
57f1a649 | 864 | .needs_fsize = 1, |
91d8f519 | 865 | .needs_blkcg = 1, |
d3656344 | 866 | }, |
0463b6c5 | 867 | [IORING_OP_OPENAT] = { |
f86cd20c | 868 | .file_table = 1, |
ff002b30 | 869 | .needs_fs = 1, |
91d8f519 | 870 | .needs_blkcg = 1, |
d3656344 | 871 | }, |
0463b6c5 | 872 | [IORING_OP_CLOSE] = { |
fd2206e4 JA |
873 | .needs_file = 1, |
874 | .needs_file_no_error = 1, | |
f86cd20c | 875 | .file_table = 1, |
91d8f519 | 876 | .needs_blkcg = 1, |
d3656344 | 877 | }, |
0463b6c5 | 878 | [IORING_OP_FILES_UPDATE] = { |
d3656344 | 879 | .needs_mm = 1, |
f86cd20c | 880 | .file_table = 1, |
d3656344 | 881 | }, |
0463b6c5 | 882 | [IORING_OP_STATX] = { |
d3656344 | 883 | .needs_mm = 1, |
ff002b30 | 884 | .needs_fs = 1, |
5b0bbee4 | 885 | .file_table = 1, |
91d8f519 | 886 | .needs_blkcg = 1, |
d3656344 | 887 | }, |
0463b6c5 | 888 | [IORING_OP_READ] = { |
3a6820f2 JA |
889 | .needs_mm = 1, |
890 | .needs_file = 1, | |
891 | .unbound_nonreg_file = 1, | |
8a72758c | 892 | .pollin = 1, |
bcda7baa | 893 | .buffer_select = 1, |
91d8f519 | 894 | .needs_blkcg = 1, |
e8c2bc1f | 895 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 896 | }, |
0463b6c5 | 897 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
898 | .needs_mm = 1, |
899 | .needs_file = 1, | |
900 | .unbound_nonreg_file = 1, | |
8a72758c | 901 | .pollout = 1, |
57f1a649 | 902 | .needs_fsize = 1, |
91d8f519 | 903 | .needs_blkcg = 1, |
e8c2bc1f | 904 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 905 | }, |
0463b6c5 | 906 | [IORING_OP_FADVISE] = { |
4840e418 | 907 | .needs_file = 1, |
91d8f519 | 908 | .needs_blkcg = 1, |
4840e418 | 909 | }, |
0463b6c5 | 910 | [IORING_OP_MADVISE] = { |
c1ca757b | 911 | .needs_mm = 1, |
91d8f519 | 912 | .needs_blkcg = 1, |
c1ca757b | 913 | }, |
0463b6c5 | 914 | [IORING_OP_SEND] = { |
fddaface JA |
915 | .needs_mm = 1, |
916 | .needs_file = 1, | |
917 | .unbound_nonreg_file = 1, | |
8a72758c | 918 | .pollout = 1, |
91d8f519 | 919 | .needs_blkcg = 1, |
fddaface | 920 | }, |
0463b6c5 | 921 | [IORING_OP_RECV] = { |
fddaface JA |
922 | .needs_mm = 1, |
923 | .needs_file = 1, | |
924 | .unbound_nonreg_file = 1, | |
8a72758c | 925 | .pollin = 1, |
bcda7baa | 926 | .buffer_select = 1, |
91d8f519 | 927 | .needs_blkcg = 1, |
fddaface | 928 | }, |
0463b6c5 | 929 | [IORING_OP_OPENAT2] = { |
f86cd20c | 930 | .file_table = 1, |
ff002b30 | 931 | .needs_fs = 1, |
91d8f519 | 932 | .needs_blkcg = 1, |
cebdb986 | 933 | }, |
3e4827b0 JA |
934 | [IORING_OP_EPOLL_CTL] = { |
935 | .unbound_nonreg_file = 1, | |
936 | .file_table = 1, | |
937 | }, | |
7d67af2c PB |
938 | [IORING_OP_SPLICE] = { |
939 | .needs_file = 1, | |
940 | .hash_reg_file = 1, | |
941 | .unbound_nonreg_file = 1, | |
91d8f519 | 942 | .needs_blkcg = 1, |
ddf0322d JA |
943 | }, |
944 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 945 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
946 | [IORING_OP_TEE] = { |
947 | .needs_file = 1, | |
948 | .hash_reg_file = 1, | |
949 | .unbound_nonreg_file = 1, | |
950 | }, | |
d3656344 JA |
951 | }; |
952 | ||
2e0464d4 BM |
953 | enum io_mem_account { |
954 | ACCT_LOCKED, | |
955 | ACCT_PINNED, | |
956 | }; | |
957 | ||
81b68a5c PB |
958 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
959 | struct io_comp_state *cs); | |
78e19bbe | 960 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 961 | static void io_put_req(struct io_kiocb *req); |
c40f6379 | 962 | static void io_double_put_req(struct io_kiocb *req); |
978db57e | 963 | static void __io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 964 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 965 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 966 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c JA |
967 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
968 | struct io_uring_files_update *ip, | |
969 | unsigned nr_args); | |
f56040b8 | 970 | static int io_prep_work_files(struct io_kiocb *req); |
3ca405eb | 971 | static void __io_clean_op(struct io_kiocb *req); |
b41e9852 JA |
972 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
973 | int fd, struct file **out_file, bool fixed); | |
974 | static void __io_queue_sqe(struct io_kiocb *req, | |
f13fad7b JA |
975 | const struct io_uring_sqe *sqe, |
976 | struct io_comp_state *cs); | |
4349f30e | 977 | static void io_file_put_work(struct work_struct *work); |
de0617e4 | 978 | |
b63534c4 JA |
979 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
980 | struct iovec **iovec, struct iov_iter *iter, | |
981 | bool needs_lock); | |
ff6165b2 JA |
982 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
983 | const struct iovec *fast_iov, | |
227c0c96 | 984 | struct iov_iter *iter, bool force); |
de0617e4 | 985 | |
2b188cc1 JA |
986 | static struct kmem_cache *req_cachep; |
987 | ||
738277ad | 988 | static const struct file_operations io_uring_fops __read_mostly; |
2b188cc1 JA |
989 | |
990 | struct sock *io_uring_get_socket(struct file *file) | |
991 | { | |
992 | #if defined(CONFIG_UNIX) | |
993 | if (file->f_op == &io_uring_fops) { | |
994 | struct io_ring_ctx *ctx = file->private_data; | |
995 | ||
996 | return ctx->ring_sock->sk; | |
997 | } | |
998 | #endif | |
999 | return NULL; | |
1000 | } | |
1001 | EXPORT_SYMBOL(io_uring_get_socket); | |
1002 | ||
3ca405eb PB |
1003 | static inline void io_clean_op(struct io_kiocb *req) |
1004 | { | |
bb175342 PB |
1005 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
1006 | REQ_F_INFLIGHT)) | |
3ca405eb PB |
1007 | __io_clean_op(req); |
1008 | } | |
1009 | ||
4349f30e | 1010 | static void io_sq_thread_drop_mm(void) |
c40f6379 JA |
1011 | { |
1012 | struct mm_struct *mm = current->mm; | |
1013 | ||
1014 | if (mm) { | |
1015 | kthread_unuse_mm(mm); | |
1016 | mmput(mm); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1021 | { | |
1022 | if (!current->mm) { | |
cbcf7214 | 1023 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) || |
2aede0e4 JA |
1024 | !ctx->sqo_task->mm || |
1025 | !mmget_not_zero(ctx->sqo_task->mm))) | |
c40f6379 | 1026 | return -EFAULT; |
2aede0e4 | 1027 | kthread_use_mm(ctx->sqo_task->mm); |
c40f6379 JA |
1028 | } |
1029 | ||
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, | |
1034 | struct io_kiocb *req) | |
1035 | { | |
1036 | if (!io_op_defs[req->opcode].needs_mm) | |
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 | |
7cdaf587 XW |
1067 | /* |
1068 | * Note: must call io_req_init_async() for the first time you | |
1069 | * touch any members of io_wq_work. | |
1070 | */ | |
1071 | static inline void io_req_init_async(struct io_kiocb *req) | |
1072 | { | |
1073 | if (req->flags & REQ_F_WORK_INITIALIZED) | |
1074 | return; | |
1075 | ||
1076 | memset(&req->work, 0, sizeof(req->work)); | |
1077 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1078 | } | |
1079 | ||
0cdaf760 PB |
1080 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
1081 | { | |
1082 | return ctx->flags & IORING_SETUP_SQPOLL; | |
1083 | } | |
1084 | ||
2b188cc1 JA |
1085 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1086 | { | |
1087 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1088 | ||
0f158b4c | 1089 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1090 | } |
1091 | ||
8eb7e2d0 PB |
1092 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1093 | { | |
1094 | return !req->timeout.off; | |
1095 | } | |
1096 | ||
2b188cc1 JA |
1097 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1098 | { | |
1099 | struct io_ring_ctx *ctx; | |
78076bb6 | 1100 | int hash_bits; |
2b188cc1 JA |
1101 | |
1102 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1103 | if (!ctx) | |
1104 | return NULL; | |
1105 | ||
0ddf92e8 JA |
1106 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
1107 | if (!ctx->fallback_req) | |
1108 | goto err; | |
1109 | ||
78076bb6 JA |
1110 | /* |
1111 | * Use 5 bits less than the max cq entries, that should give us around | |
1112 | * 32 entries per hash list if totally full and uniformly spread. | |
1113 | */ | |
1114 | hash_bits = ilog2(p->cq_entries); | |
1115 | hash_bits -= 5; | |
1116 | if (hash_bits <= 0) | |
1117 | hash_bits = 1; | |
1118 | ctx->cancel_hash_bits = hash_bits; | |
1119 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1120 | GFP_KERNEL); | |
1121 | if (!ctx->cancel_hash) | |
1122 | goto err; | |
1123 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1124 | ||
21482896 | 1125 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1126 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1127 | goto err; | |
2b188cc1 JA |
1128 | |
1129 | ctx->flags = p->flags; | |
90554200 | 1130 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1131 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1132 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1133 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1134 | init_completion(&ctx->ref_comp); |
1135 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1136 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1137 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1138 | mutex_init(&ctx->uring_lock); |
1139 | init_waitqueue_head(&ctx->wait); | |
1140 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1141 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1142 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1143 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1144 | init_waitqueue_head(&ctx->inflight_wait); |
1145 | spin_lock_init(&ctx->inflight_lock); | |
1146 | INIT_LIST_HEAD(&ctx->inflight_list); | |
4a38aed2 JA |
1147 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
1148 | init_llist_head(&ctx->file_put_llist); | |
2b188cc1 | 1149 | return ctx; |
206aefde | 1150 | err: |
0ddf92e8 JA |
1151 | if (ctx->fallback_req) |
1152 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
78076bb6 | 1153 | kfree(ctx->cancel_hash); |
206aefde JA |
1154 | kfree(ctx); |
1155 | return NULL; | |
2b188cc1 JA |
1156 | } |
1157 | ||
9cf7c104 | 1158 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1159 | { |
2bc9930e JA |
1160 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1161 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1162 | |
9cf7c104 | 1163 | return seq != ctx->cached_cq_tail |
31af27c7 | 1164 | + atomic_read(&ctx->cached_cq_overflow); |
2bc9930e | 1165 | } |
de0617e4 | 1166 | |
9d858b21 | 1167 | return false; |
de0617e4 JA |
1168 | } |
1169 | ||
de0617e4 | 1170 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 1171 | { |
75b28aff | 1172 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 1173 | |
07910158 PB |
1174 | /* order cqe stores with ring update */ |
1175 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 | 1176 | |
07910158 PB |
1177 | if (wq_has_sleeper(&ctx->cq_wait)) { |
1178 | wake_up_interruptible(&ctx->cq_wait); | |
1179 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
2b188cc1 JA |
1180 | } |
1181 | } | |
1182 | ||
51a4cc11 JA |
1183 | /* |
1184 | * Returns true if we need to defer file table putting. This can only happen | |
1185 | * from the error path with REQ_F_COMP_LOCKED set. | |
1186 | */ | |
1187 | static bool io_req_clean_work(struct io_kiocb *req) | |
18d9be1a | 1188 | { |
7cdaf587 | 1189 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
51a4cc11 JA |
1190 | return false; |
1191 | ||
1192 | req->flags &= ~REQ_F_WORK_INITIALIZED; | |
7cdaf587 | 1193 | |
cccf0ee8 JA |
1194 | if (req->work.mm) { |
1195 | mmdrop(req->work.mm); | |
1196 | req->work.mm = NULL; | |
1197 | } | |
91d8f519 DZ |
1198 | #ifdef CONFIG_BLK_CGROUP |
1199 | if (req->work.blkcg_css) | |
1200 | css_put(req->work.blkcg_css); | |
1201 | #endif | |
cccf0ee8 JA |
1202 | if (req->work.creds) { |
1203 | put_cred(req->work.creds); | |
1204 | req->work.creds = NULL; | |
1205 | } | |
ff002b30 JA |
1206 | if (req->work.fs) { |
1207 | struct fs_struct *fs = req->work.fs; | |
1208 | ||
51a4cc11 JA |
1209 | if (req->flags & REQ_F_COMP_LOCKED) |
1210 | return true; | |
1211 | ||
ff002b30 JA |
1212 | spin_lock(&req->work.fs->lock); |
1213 | if (--fs->users) | |
1214 | fs = NULL; | |
1215 | spin_unlock(&req->work.fs->lock); | |
1216 | if (fs) | |
1217 | free_fs_struct(fs); | |
b65e0dd6 | 1218 | req->work.fs = NULL; |
ff002b30 | 1219 | } |
51a4cc11 JA |
1220 | |
1221 | return false; | |
561fb04a JA |
1222 | } |
1223 | ||
cbdcb435 | 1224 | static void io_prep_async_work(struct io_kiocb *req) |
18d9be1a | 1225 | { |
d3656344 | 1226 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
54a91f3b | 1227 | |
16d59803 PB |
1228 | io_req_init_async(req); |
1229 | ||
d3656344 | 1230 | if (req->flags & REQ_F_ISREG) { |
eefdf30f | 1231 | if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL)) |
8766dd51 | 1232 | io_wq_hash_work(&req->work, file_inode(req->file)); |
d3656344 JA |
1233 | } else { |
1234 | if (def->unbound_nonreg_file) | |
3529d8c2 | 1235 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
54a91f3b | 1236 | } |
dca9cf8b PB |
1237 | if (!req->work.mm && def->needs_mm) { |
1238 | mmgrab(current->mm); | |
1239 | req->work.mm = current->mm; | |
1240 | } | |
91d8f519 DZ |
1241 | #ifdef CONFIG_BLK_CGROUP |
1242 | if (!req->work.blkcg_css && def->needs_blkcg) { | |
1243 | rcu_read_lock(); | |
1244 | req->work.blkcg_css = blkcg_css(); | |
1245 | /* | |
1246 | * This should be rare, either the cgroup is dying or the task | |
1247 | * is moving cgroups. Just punt to root for the handful of ios. | |
1248 | */ | |
1249 | if (!css_tryget_online(req->work.blkcg_css)) | |
1250 | req->work.blkcg_css = NULL; | |
1251 | rcu_read_unlock(); | |
1252 | } | |
1253 | #endif | |
dca9cf8b PB |
1254 | if (!req->work.creds) |
1255 | req->work.creds = get_current_cred(); | |
1256 | if (!req->work.fs && def->needs_fs) { | |
1257 | spin_lock(¤t->fs->lock); | |
1258 | if (!current->fs->in_exec) { | |
1259 | req->work.fs = current->fs; | |
1260 | req->work.fs->users++; | |
1261 | } else { | |
1262 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1263 | } | |
1264 | spin_unlock(¤t->fs->lock); | |
1265 | } | |
57f1a649 PB |
1266 | if (def->needs_fsize) |
1267 | req->work.fsize = rlimit(RLIMIT_FSIZE); | |
1268 | else | |
1269 | req->work.fsize = RLIM_INFINITY; | |
561fb04a | 1270 | } |
cccf0ee8 | 1271 | |
cbdcb435 | 1272 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1273 | { |
cbdcb435 | 1274 | struct io_kiocb *cur; |
54a91f3b | 1275 | |
cbdcb435 PB |
1276 | io_prep_async_work(req); |
1277 | if (req->flags & REQ_F_LINK_HEAD) | |
1278 | list_for_each_entry(cur, &req->link_list, link_list) | |
1279 | io_prep_async_work(cur); | |
561fb04a JA |
1280 | } |
1281 | ||
7271ef3a | 1282 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1283 | { |
a197f664 | 1284 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1285 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1286 | |
8766dd51 PB |
1287 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1288 | &req->work, req->flags); | |
1289 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1290 | return link; |
18d9be1a JA |
1291 | } |
1292 | ||
cbdcb435 PB |
1293 | static void io_queue_async_work(struct io_kiocb *req) |
1294 | { | |
7271ef3a JA |
1295 | struct io_kiocb *link; |
1296 | ||
cbdcb435 PB |
1297 | /* init ->work of the whole link before punting */ |
1298 | io_prep_async_link(req); | |
7271ef3a JA |
1299 | link = __io_queue_async_work(req); |
1300 | ||
1301 | if (link) | |
1302 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1303 | } |
1304 | ||
5262f567 JA |
1305 | static void io_kill_timeout(struct io_kiocb *req) |
1306 | { | |
e8c2bc1f | 1307 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1308 | int ret; |
1309 | ||
e8c2bc1f | 1310 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1311 | if (ret != -1) { |
01cec8c1 PB |
1312 | atomic_set(&req->ctx->cq_timeouts, |
1313 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1314 | list_del_init(&req->timeout.list); |
f0e20b89 | 1315 | req->flags |= REQ_F_COMP_LOCKED; |
78e19bbe | 1316 | io_cqring_fill_event(req, 0); |
ec9c02ad | 1317 | io_put_req(req); |
5262f567 JA |
1318 | } |
1319 | } | |
1320 | ||
f3606e3a JA |
1321 | static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk) |
1322 | { | |
1323 | struct io_ring_ctx *ctx = req->ctx; | |
1324 | ||
1325 | if (!tsk || req->task == tsk) | |
1326 | return true; | |
534ca6d6 JA |
1327 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1328 | if (ctx->sq_data && req->task == ctx->sq_data->thread) | |
1329 | return true; | |
1330 | } | |
f3606e3a JA |
1331 | return false; |
1332 | } | |
1333 | ||
76e1b642 JA |
1334 | /* |
1335 | * Returns true if we found and killed one or more timeouts | |
1336 | */ | |
1337 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk) | |
5262f567 JA |
1338 | { |
1339 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1340 | int canceled = 0; |
5262f567 JA |
1341 | |
1342 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1343 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
76e1b642 | 1344 | if (io_task_match(req, tsk)) { |
f3606e3a | 1345 | io_kill_timeout(req); |
76e1b642 JA |
1346 | canceled++; |
1347 | } | |
f3606e3a | 1348 | } |
5262f567 | 1349 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1350 | return canceled != 0; |
5262f567 JA |
1351 | } |
1352 | ||
04518945 | 1353 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1354 | { |
04518945 | 1355 | do { |
27dc8338 PB |
1356 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1357 | struct io_defer_entry, list); | |
7271ef3a | 1358 | struct io_kiocb *link; |
de0617e4 | 1359 | |
9cf7c104 | 1360 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1361 | break; |
27dc8338 | 1362 | list_del_init(&de->list); |
cbdcb435 | 1363 | /* punt-init is done before queueing for defer */ |
7271ef3a JA |
1364 | link = __io_queue_async_work(de->req); |
1365 | if (link) { | |
1366 | __io_queue_linked_timeout(link); | |
1367 | /* drop submission reference */ | |
1368 | link->flags |= REQ_F_COMP_LOCKED; | |
1369 | io_put_req(link); | |
1370 | } | |
27dc8338 | 1371 | kfree(de); |
04518945 PB |
1372 | } while (!list_empty(&ctx->defer_list)); |
1373 | } | |
1374 | ||
360428f8 | 1375 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1376 | { |
360428f8 PB |
1377 | while (!list_empty(&ctx->timeout_list)) { |
1378 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, | |
135fcde8 | 1379 | struct io_kiocb, timeout.list); |
de0617e4 | 1380 | |
8eb7e2d0 | 1381 | if (io_is_timeout_noseq(req)) |
360428f8 | 1382 | break; |
bfe68a22 PB |
1383 | if (req->timeout.target_seq != ctx->cached_cq_tail |
1384 | - atomic_read(&ctx->cq_timeouts)) | |
360428f8 | 1385 | break; |
bfe68a22 | 1386 | |
135fcde8 | 1387 | list_del_init(&req->timeout.list); |
5262f567 | 1388 | io_kill_timeout(req); |
360428f8 PB |
1389 | } |
1390 | } | |
5262f567 | 1391 | |
360428f8 PB |
1392 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1393 | { | |
1394 | io_flush_timeouts(ctx); | |
de0617e4 JA |
1395 | __io_commit_cqring(ctx); |
1396 | ||
04518945 PB |
1397 | if (unlikely(!list_empty(&ctx->defer_list))) |
1398 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1399 | } |
1400 | ||
90554200 JA |
1401 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1402 | { | |
1403 | struct io_rings *r = ctx->rings; | |
1404 | ||
1405 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1406 | } | |
1407 | ||
2b188cc1 JA |
1408 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1409 | { | |
75b28aff | 1410 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1411 | unsigned tail; |
1412 | ||
1413 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
1414 | /* |
1415 | * writes to the cq entry need to come after reading head; the | |
1416 | * control dependency is enough as we're using WRITE_ONCE to | |
1417 | * fill the cq entry | |
1418 | */ | |
75b28aff | 1419 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
1420 | return NULL; |
1421 | ||
1422 | ctx->cached_cq_tail++; | |
75b28aff | 1423 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1424 | } |
1425 | ||
f2842ab5 JA |
1426 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1427 | { | |
f0b493e6 JA |
1428 | if (!ctx->cq_ev_fd) |
1429 | return false; | |
7e55a19c SG |
1430 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1431 | return false; | |
f2842ab5 JA |
1432 | if (!ctx->eventfd_async) |
1433 | return true; | |
b41e9852 | 1434 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1435 | } |
1436 | ||
b41e9852 | 1437 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 JA |
1438 | { |
1439 | if (waitqueue_active(&ctx->wait)) | |
1440 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1441 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1442 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1443 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1444 | eventfd_signal(ctx->cq_ev_fd, 1); |
1445 | } | |
1446 | ||
46930143 PB |
1447 | static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
1448 | { | |
1449 | if (list_empty(&ctx->cq_overflow_list)) { | |
1450 | clear_bit(0, &ctx->sq_check_overflow); | |
1451 | clear_bit(0, &ctx->cq_check_overflow); | |
1452 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1453 | } | |
1454 | } | |
1455 | ||
e6c8aa9a JA |
1456 | static inline bool io_match_files(struct io_kiocb *req, |
1457 | struct files_struct *files) | |
1458 | { | |
1459 | if (!files) | |
1460 | return true; | |
1461 | if (req->flags & REQ_F_WORK_INITIALIZED) | |
1462 | return req->work.files == files; | |
1463 | return false; | |
1464 | } | |
1465 | ||
c4a2ed72 | 1466 | /* Returns true if there are no backlogged entries after the flush */ |
e6c8aa9a JA |
1467 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1468 | struct task_struct *tsk, | |
1469 | struct files_struct *files) | |
1d7bb1d5 JA |
1470 | { |
1471 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1472 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1473 | struct io_uring_cqe *cqe; |
1d7bb1d5 JA |
1474 | unsigned long flags; |
1475 | LIST_HEAD(list); | |
1476 | ||
1477 | if (!force) { | |
1478 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
c4a2ed72 | 1479 | return true; |
1d7bb1d5 JA |
1480 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
1481 | rings->cq_ring_entries)) | |
c4a2ed72 | 1482 | return false; |
1d7bb1d5 JA |
1483 | } |
1484 | ||
1485 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1486 | ||
1487 | /* if force is set, the ring is going away. always drop after that */ | |
1488 | if (force) | |
69b3e546 | 1489 | ctx->cq_overflow_flushed = 1; |
1d7bb1d5 | 1490 | |
c4a2ed72 | 1491 | cqe = NULL; |
e6c8aa9a JA |
1492 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
1493 | if (tsk && req->task != tsk) | |
1494 | continue; | |
1495 | if (!io_match_files(req, files)) | |
1496 | continue; | |
1497 | ||
1d7bb1d5 JA |
1498 | cqe = io_get_cqring(ctx); |
1499 | if (!cqe && !force) | |
1500 | break; | |
1501 | ||
40d8ddd4 | 1502 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1503 | if (cqe) { |
1504 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1505 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1506 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 JA |
1507 | } else { |
1508 | WRITE_ONCE(ctx->rings->cq_overflow, | |
1509 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | io_commit_cqring(ctx); | |
46930143 PB |
1514 | io_cqring_mark_overflow(ctx); |
1515 | ||
1d7bb1d5 JA |
1516 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1517 | io_cqring_ev_posted(ctx); | |
1518 | ||
1519 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1520 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1521 | list_del(&req->compl.list); | |
ec9c02ad | 1522 | io_put_req(req); |
1d7bb1d5 | 1523 | } |
c4a2ed72 JA |
1524 | |
1525 | return cqe != NULL; | |
1d7bb1d5 JA |
1526 | } |
1527 | ||
bcda7baa | 1528 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1529 | { |
78e19bbe | 1530 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1531 | struct io_uring_cqe *cqe; |
1532 | ||
78e19bbe | 1533 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1534 | |
2b188cc1 JA |
1535 | /* |
1536 | * If we can't get a cq entry, userspace overflowed the | |
1537 | * submission (by quite a lot). Increment the overflow count in | |
1538 | * the ring. | |
1539 | */ | |
1540 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1541 | if (likely(cqe)) { |
78e19bbe | 1542 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1543 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1544 | WRITE_ONCE(cqe->flags, cflags); |
0f212204 JA |
1545 | } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) { |
1546 | /* | |
1547 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1548 | * then we cannot store the request for later flushing, we need | |
1549 | * to drop it on the floor. | |
1550 | */ | |
498ccd9e JA |
1551 | WRITE_ONCE(ctx->rings->cq_overflow, |
1552 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1d7bb1d5 | 1553 | } else { |
ad3eb2c8 JA |
1554 | if (list_empty(&ctx->cq_overflow_list)) { |
1555 | set_bit(0, &ctx->sq_check_overflow); | |
1556 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1557 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1558 | } |
40d8ddd4 | 1559 | io_clean_op(req); |
1d7bb1d5 | 1560 | req->result = res; |
0f7e466b | 1561 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1562 | refcount_inc(&req->refs); |
1563 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1564 | } |
1565 | } | |
1566 | ||
bcda7baa JA |
1567 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1568 | { | |
1569 | __io_cqring_fill_event(req, res, 0); | |
1570 | } | |
1571 | ||
e1e16097 | 1572 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1573 | { |
78e19bbe | 1574 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1575 | unsigned long flags; |
1576 | ||
1577 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1578 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1579 | io_commit_cqring(ctx); |
1580 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1581 | ||
8c838788 | 1582 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1583 | } |
1584 | ||
229a7b63 | 1585 | static void io_submit_flush_completions(struct io_comp_state *cs) |
bcda7baa | 1586 | { |
229a7b63 JA |
1587 | struct io_ring_ctx *ctx = cs->ctx; |
1588 | ||
1589 | spin_lock_irq(&ctx->completion_lock); | |
1590 | while (!list_empty(&cs->list)) { | |
1591 | struct io_kiocb *req; | |
1592 | ||
3ca405eb PB |
1593 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
1594 | list_del(&req->compl.list); | |
0f7e466b | 1595 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
229a7b63 JA |
1596 | if (!(req->flags & REQ_F_LINK_HEAD)) { |
1597 | req->flags |= REQ_F_COMP_LOCKED; | |
1598 | io_put_req(req); | |
1599 | } else { | |
1600 | spin_unlock_irq(&ctx->completion_lock); | |
1601 | io_put_req(req); | |
1602 | spin_lock_irq(&ctx->completion_lock); | |
1603 | } | |
1604 | } | |
1605 | io_commit_cqring(ctx); | |
1606 | spin_unlock_irq(&ctx->completion_lock); | |
1607 | ||
1608 | io_cqring_ev_posted(ctx); | |
1609 | cs->nr = 0; | |
1610 | } | |
1611 | ||
1612 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, | |
1613 | struct io_comp_state *cs) | |
1614 | { | |
1615 | if (!cs) { | |
1616 | io_cqring_add_event(req, res, cflags); | |
1617 | io_put_req(req); | |
1618 | } else { | |
3ca405eb | 1619 | io_clean_op(req); |
229a7b63 | 1620 | req->result = res; |
0f7e466b | 1621 | req->compl.cflags = cflags; |
3ca405eb | 1622 | list_add_tail(&req->compl.list, &cs->list); |
229a7b63 JA |
1623 | if (++cs->nr >= 32) |
1624 | io_submit_flush_completions(cs); | |
1625 | } | |
e1e16097 JA |
1626 | } |
1627 | ||
1628 | static void io_req_complete(struct io_kiocb *req, long res) | |
bcda7baa | 1629 | { |
229a7b63 | 1630 | __io_req_complete(req, res, 0, NULL); |
bcda7baa JA |
1631 | } |
1632 | ||
0ddf92e8 JA |
1633 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1634 | { | |
1635 | return req == (struct io_kiocb *) | |
1636 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1637 | } | |
1638 | ||
1639 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1640 | { | |
1641 | struct io_kiocb *req; | |
1642 | ||
1643 | req = ctx->fallback_req; | |
dd461af6 | 1644 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
0ddf92e8 JA |
1645 | return req; |
1646 | ||
1647 | return NULL; | |
1648 | } | |
1649 | ||
0553b8bd PB |
1650 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
1651 | struct io_submit_state *state) | |
2b188cc1 | 1652 | { |
f6b6c7d6 | 1653 | if (!state->free_reqs) { |
291b2821 | 1654 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1655 | size_t sz; |
1656 | int ret; | |
1657 | ||
1658 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1659 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1660 | ||
1661 | /* | |
1662 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1663 | * retry single alloc to be on the safe side. | |
1664 | */ | |
1665 | if (unlikely(ret <= 0)) { | |
1666 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1667 | if (!state->reqs[0]) | |
0ddf92e8 | 1668 | goto fallback; |
fd6fab2c JA |
1669 | ret = 1; |
1670 | } | |
291b2821 | 1671 | state->free_reqs = ret; |
2b188cc1 JA |
1672 | } |
1673 | ||
291b2821 PB |
1674 | state->free_reqs--; |
1675 | return state->reqs[state->free_reqs]; | |
0ddf92e8 | 1676 | fallback: |
0553b8bd | 1677 | return io_get_fallback_req(ctx); |
2b188cc1 JA |
1678 | } |
1679 | ||
8da11c19 PB |
1680 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1681 | bool fixed) | |
1682 | { | |
1683 | if (fixed) | |
05589553 | 1684 | percpu_ref_put(req->fixed_file_refs); |
8da11c19 PB |
1685 | else |
1686 | fput(file); | |
1687 | } | |
1688 | ||
51a4cc11 | 1689 | static bool io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1690 | { |
3ca405eb | 1691 | io_clean_op(req); |
929a3af9 | 1692 | |
e8c2bc1f JA |
1693 | if (req->async_data) |
1694 | kfree(req->async_data); | |
8da11c19 PB |
1695 | if (req->file) |
1696 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
fcb323cc | 1697 | |
51a4cc11 | 1698 | return io_req_clean_work(req); |
e65ef56d JA |
1699 | } |
1700 | ||
51a4cc11 | 1701 | static void __io_free_req_finish(struct io_kiocb *req) |
c6ca97b3 | 1702 | { |
0f212204 | 1703 | struct io_uring_task *tctx = req->task->io_uring; |
51a4cc11 | 1704 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1705 | |
0f212204 JA |
1706 | atomic_long_inc(&tctx->req_complete); |
1707 | if (tctx->in_idle) | |
1708 | wake_up(&tctx->wait); | |
e3bc8e9d JA |
1709 | put_task_struct(req->task); |
1710 | ||
b1e50e54 PB |
1711 | if (likely(!io_is_fallback_req(req))) |
1712 | kmem_cache_free(req_cachep, req); | |
1713 | else | |
ecfc5177 PB |
1714 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
1715 | percpu_ref_put(&ctx->refs); | |
e65ef56d JA |
1716 | } |
1717 | ||
51a4cc11 JA |
1718 | static void io_req_task_file_table_put(struct callback_head *cb) |
1719 | { | |
1720 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
1721 | struct fs_struct *fs = req->work.fs; | |
1722 | ||
1723 | spin_lock(&req->work.fs->lock); | |
1724 | if (--fs->users) | |
1725 | fs = NULL; | |
1726 | spin_unlock(&req->work.fs->lock); | |
1727 | if (fs) | |
1728 | free_fs_struct(fs); | |
1729 | req->work.fs = NULL; | |
1730 | __io_free_req_finish(req); | |
1731 | } | |
1732 | ||
1733 | static void __io_free_req(struct io_kiocb *req) | |
1734 | { | |
1735 | if (!io_dismantle_req(req)) { | |
1736 | __io_free_req_finish(req); | |
1737 | } else { | |
1738 | int ret; | |
1739 | ||
1740 | init_task_work(&req->task_work, io_req_task_file_table_put); | |
1741 | ret = task_work_add(req->task, &req->task_work, TWA_RESUME); | |
1742 | if (unlikely(ret)) { | |
1743 | struct task_struct *tsk; | |
1744 | ||
1745 | tsk = io_wq_get_task(req->ctx->io_wq); | |
1746 | task_work_add(tsk, &req->task_work, 0); | |
1747 | } | |
1748 | } | |
1749 | } | |
1750 | ||
a197f664 | 1751 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
2665abfd | 1752 | { |
e8c2bc1f | 1753 | struct io_timeout_data *io = req->async_data; |
a197f664 | 1754 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
1755 | int ret; |
1756 | ||
e8c2bc1f | 1757 | ret = hrtimer_try_to_cancel(&io->timer); |
2665abfd | 1758 | if (ret != -1) { |
78e19bbe | 1759 | io_cqring_fill_event(req, -ECANCELED); |
2665abfd | 1760 | io_commit_cqring(ctx); |
dea3b49c | 1761 | req->flags &= ~REQ_F_LINK_HEAD; |
ec9c02ad | 1762 | io_put_req(req); |
2665abfd JA |
1763 | return true; |
1764 | } | |
1765 | ||
1766 | return false; | |
e65ef56d JA |
1767 | } |
1768 | ||
ab0b6451 | 1769 | static bool __io_kill_linked_timeout(struct io_kiocb *req) |
9e645e11 | 1770 | { |
7c86ffee | 1771 | struct io_kiocb *link; |
ab0b6451 | 1772 | bool wake_ev; |
7c86ffee PB |
1773 | |
1774 | if (list_empty(&req->link_list)) | |
ab0b6451 | 1775 | return false; |
7c86ffee PB |
1776 | link = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
1777 | if (link->opcode != IORING_OP_LINK_TIMEOUT) | |
ab0b6451 | 1778 | return false; |
7c86ffee PB |
1779 | |
1780 | list_del_init(&link->link_list); | |
9b7adba9 | 1781 | link->flags |= REQ_F_COMP_LOCKED; |
7c86ffee PB |
1782 | wake_ev = io_link_cancel_timeout(link); |
1783 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
ab0b6451 JA |
1784 | return wake_ev; |
1785 | } | |
1786 | ||
1787 | static void io_kill_linked_timeout(struct io_kiocb *req) | |
9e645e11 | 1788 | { |
2665abfd | 1789 | struct io_ring_ctx *ctx = req->ctx; |
ab0b6451 | 1790 | bool wake_ev; |
9e645e11 | 1791 | |
ab0b6451 JA |
1792 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
1793 | unsigned long flags; | |
1794 | ||
1795 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1796 | wake_ev = __io_kill_linked_timeout(req); | |
7c86ffee | 1797 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 JA |
1798 | } else { |
1799 | wake_ev = __io_kill_linked_timeout(req); | |
1800 | } | |
1801 | ||
7c86ffee PB |
1802 | if (wake_ev) |
1803 | io_cqring_ev_posted(ctx); | |
1804 | } | |
1805 | ||
9b5f7bd9 | 1806 | static struct io_kiocb *io_req_link_next(struct io_kiocb *req) |
7c86ffee PB |
1807 | { |
1808 | struct io_kiocb *nxt; | |
4d7dd462 | 1809 | |
9e645e11 JA |
1810 | /* |
1811 | * The list should never be empty when we are called here. But could | |
1812 | * potentially happen if the chain is messed up, check to be on the | |
1813 | * safe side. | |
1814 | */ | |
7c86ffee | 1815 | if (unlikely(list_empty(&req->link_list))) |
9b5f7bd9 | 1816 | return NULL; |
2665abfd | 1817 | |
7c86ffee PB |
1818 | nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
1819 | list_del_init(&req->link_list); | |
1820 | if (!list_empty(&nxt->link_list)) | |
1821 | nxt->flags |= REQ_F_LINK_HEAD; | |
9b5f7bd9 | 1822 | return nxt; |
9e645e11 JA |
1823 | } |
1824 | ||
1825 | /* | |
dea3b49c | 1826 | * Called if REQ_F_LINK_HEAD is set, and we fail the head request |
9e645e11 | 1827 | */ |
7c86ffee | 1828 | static void __io_fail_links(struct io_kiocb *req) |
9e645e11 | 1829 | { |
2665abfd | 1830 | struct io_ring_ctx *ctx = req->ctx; |
9e645e11 JA |
1831 | |
1832 | while (!list_empty(&req->link_list)) { | |
4493233e PB |
1833 | struct io_kiocb *link = list_first_entry(&req->link_list, |
1834 | struct io_kiocb, link_list); | |
9e645e11 | 1835 | |
4493233e | 1836 | list_del_init(&link->link_list); |
c826bd7a | 1837 | trace_io_uring_fail_link(req, link); |
2665abfd | 1838 | |
7c86ffee | 1839 | io_cqring_fill_event(link, -ECANCELED); |
9b7adba9 | 1840 | link->flags |= REQ_F_COMP_LOCKED; |
7c86ffee | 1841 | __io_double_put_req(link); |
5d960724 | 1842 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
9e645e11 | 1843 | } |
2665abfd JA |
1844 | |
1845 | io_commit_cqring(ctx); | |
2665abfd | 1846 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1847 | } |
1848 | ||
7c86ffee | 1849 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1850 | { |
7c86ffee | 1851 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 1852 | |
7c86ffee | 1853 | if (!(req->flags & REQ_F_COMP_LOCKED)) { |
2665abfd JA |
1854 | unsigned long flags; |
1855 | ||
2665abfd | 1856 | spin_lock_irqsave(&ctx->completion_lock, flags); |
7c86ffee | 1857 | __io_fail_links(req); |
2665abfd JA |
1858 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1859 | } else { | |
7c86ffee | 1860 | __io_fail_links(req); |
9e645e11 JA |
1861 | } |
1862 | ||
2665abfd | 1863 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1864 | } |
1865 | ||
3fa5e0f3 | 1866 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1867 | { |
9b0d911a | 1868 | req->flags &= ~REQ_F_LINK_HEAD; |
7c86ffee PB |
1869 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1870 | io_kill_linked_timeout(req); | |
944e58bf | 1871 | |
9e645e11 JA |
1872 | /* |
1873 | * If LINK is set, we have dependent requests in this chain. If we | |
1874 | * didn't fail this request, queue the first one up, moving any other | |
1875 | * dependencies to the next request. In case of failure, fail the rest | |
1876 | * of the chain. | |
1877 | */ | |
9b5f7bd9 PB |
1878 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) |
1879 | return io_req_link_next(req); | |
1880 | io_fail_links(req); | |
1881 | return NULL; | |
4d7dd462 | 1882 | } |
9e645e11 | 1883 | |
3fa5e0f3 PB |
1884 | static struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
1885 | { | |
1886 | if (likely(!(req->flags & REQ_F_LINK_HEAD))) | |
1887 | return NULL; | |
1888 | return __io_req_find_next(req); | |
1889 | } | |
1890 | ||
fd7d6de2 JA |
1891 | static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb, |
1892 | bool twa_signal_ok) | |
c2c4c83c JA |
1893 | { |
1894 | struct task_struct *tsk = req->task; | |
1895 | struct io_ring_ctx *ctx = req->ctx; | |
0ba9c9ed | 1896 | int ret, notify; |
c2c4c83c | 1897 | |
6200b0ae JA |
1898 | if (tsk->flags & PF_EXITING) |
1899 | return -ESRCH; | |
1900 | ||
c2c4c83c | 1901 | /* |
0ba9c9ed JA |
1902 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1903 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1904 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1905 | * will do the job. | |
c2c4c83c | 1906 | */ |
0ba9c9ed | 1907 | notify = 0; |
fd7d6de2 | 1908 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok) |
c2c4c83c JA |
1909 | notify = TWA_SIGNAL; |
1910 | ||
1911 | ret = task_work_add(tsk, cb, notify); | |
1912 | if (!ret) | |
1913 | wake_up_process(tsk); | |
0ba9c9ed | 1914 | |
c2c4c83c JA |
1915 | return ret; |
1916 | } | |
1917 | ||
c40f6379 JA |
1918 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1919 | { | |
1920 | struct io_ring_ctx *ctx = req->ctx; | |
1921 | ||
1922 | spin_lock_irq(&ctx->completion_lock); | |
1923 | io_cqring_fill_event(req, error); | |
1924 | io_commit_cqring(ctx); | |
1925 | spin_unlock_irq(&ctx->completion_lock); | |
1926 | ||
1927 | io_cqring_ev_posted(ctx); | |
1928 | req_set_fail_links(req); | |
1929 | io_double_put_req(req); | |
1930 | } | |
1931 | ||
1932 | static void io_req_task_cancel(struct callback_head *cb) | |
1933 | { | |
1934 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 1935 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
1936 | |
1937 | __io_req_task_cancel(req, -ECANCELED); | |
87ceb6a6 | 1938 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
1939 | } |
1940 | ||
1941 | static void __io_req_task_submit(struct io_kiocb *req) | |
1942 | { | |
1943 | struct io_ring_ctx *ctx = req->ctx; | |
1944 | ||
c40f6379 JA |
1945 | if (!__io_sq_thread_acquire_mm(ctx)) { |
1946 | mutex_lock(&ctx->uring_lock); | |
1947 | __io_queue_sqe(req, NULL, NULL); | |
1948 | mutex_unlock(&ctx->uring_lock); | |
1949 | } else { | |
1950 | __io_req_task_cancel(req, -EFAULT); | |
1951 | } | |
1952 | } | |
1953 | ||
1954 | static void io_req_task_submit(struct callback_head *cb) | |
1955 | { | |
1956 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 1957 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
1958 | |
1959 | __io_req_task_submit(req); | |
6d816e08 | 1960 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
1961 | } |
1962 | ||
1963 | static void io_req_task_queue(struct io_kiocb *req) | |
1964 | { | |
c40f6379 JA |
1965 | int ret; |
1966 | ||
1967 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 1968 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 1969 | |
fd7d6de2 | 1970 | ret = io_req_task_work_add(req, &req->task_work, true); |
c40f6379 | 1971 | if (unlikely(ret)) { |
c2c4c83c JA |
1972 | struct task_struct *tsk; |
1973 | ||
c40f6379 JA |
1974 | init_task_work(&req->task_work, io_req_task_cancel); |
1975 | tsk = io_wq_get_task(req->ctx->io_wq); | |
c2c4c83c JA |
1976 | task_work_add(tsk, &req->task_work, 0); |
1977 | wake_up_process(tsk); | |
c40f6379 | 1978 | } |
c40f6379 JA |
1979 | } |
1980 | ||
c3524383 | 1981 | static void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 1982 | { |
9b5f7bd9 | 1983 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
1984 | |
1985 | if (nxt) | |
906a8c3f | 1986 | io_req_task_queue(nxt); |
c69f8dbe JL |
1987 | } |
1988 | ||
c3524383 | 1989 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 1990 | { |
c3524383 PB |
1991 | io_queue_next(req); |
1992 | __io_free_req(req); | |
1993 | } | |
8766dd51 | 1994 | |
2d6500d4 PB |
1995 | struct req_batch { |
1996 | void *reqs[IO_IOPOLL_BATCH]; | |
1997 | int to_free; | |
7a743e22 | 1998 | |
5af1d13e PB |
1999 | struct task_struct *task; |
2000 | int task_refs; | |
2d6500d4 PB |
2001 | }; |
2002 | ||
5af1d13e PB |
2003 | static inline void io_init_req_batch(struct req_batch *rb) |
2004 | { | |
2005 | rb->to_free = 0; | |
2006 | rb->task_refs = 0; | |
2007 | rb->task = NULL; | |
2008 | } | |
2009 | ||
2d6500d4 PB |
2010 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
2011 | struct req_batch *rb) | |
2012 | { | |
2013 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
2014 | percpu_ref_put_many(&ctx->refs, rb->to_free); | |
2015 | rb->to_free = 0; | |
2016 | } | |
2017 | ||
2018 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
2019 | struct req_batch *rb) | |
2020 | { | |
2021 | if (rb->to_free) | |
2022 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e | 2023 | if (rb->task) { |
0f212204 | 2024 | atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete); |
5af1d13e PB |
2025 | put_task_struct_many(rb->task, rb->task_refs); |
2026 | rb->task = NULL; | |
2027 | } | |
2d6500d4 PB |
2028 | } |
2029 | ||
2030 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
2031 | { | |
2032 | if (unlikely(io_is_fallback_req(req))) { | |
2033 | io_free_req(req); | |
2034 | return; | |
2035 | } | |
2036 | if (req->flags & REQ_F_LINK_HEAD) | |
2037 | io_queue_next(req); | |
2038 | ||
e3bc8e9d | 2039 | if (req->task != rb->task) { |
0f212204 JA |
2040 | if (rb->task) { |
2041 | atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete); | |
e3bc8e9d | 2042 | put_task_struct_many(rb->task, rb->task_refs); |
0f212204 | 2043 | } |
e3bc8e9d JA |
2044 | rb->task = req->task; |
2045 | rb->task_refs = 0; | |
5af1d13e | 2046 | } |
e3bc8e9d | 2047 | rb->task_refs++; |
5af1d13e | 2048 | |
51a4cc11 | 2049 | WARN_ON_ONCE(io_dismantle_req(req)); |
2d6500d4 PB |
2050 | rb->reqs[rb->to_free++] = req; |
2051 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
2052 | __io_req_free_batch_flush(req->ctx, rb); | |
7a743e22 PB |
2053 | } |
2054 | ||
ba816ad6 JA |
2055 | /* |
2056 | * Drop reference to request, return next in chain (if there is one) if this | |
2057 | * was the last reference to this request. | |
2058 | */ | |
9b5f7bd9 | 2059 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2060 | { |
9b5f7bd9 PB |
2061 | struct io_kiocb *nxt = NULL; |
2062 | ||
2a44f467 | 2063 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2064 | nxt = io_req_find_next(req); |
4d7dd462 | 2065 | __io_free_req(req); |
2a44f467 | 2066 | } |
9b5f7bd9 | 2067 | return nxt; |
2b188cc1 JA |
2068 | } |
2069 | ||
e65ef56d JA |
2070 | static void io_put_req(struct io_kiocb *req) |
2071 | { | |
2072 | if (refcount_dec_and_test(&req->refs)) | |
2073 | io_free_req(req); | |
2b188cc1 JA |
2074 | } |
2075 | ||
f4db7182 | 2076 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
7a743e22 | 2077 | { |
6df1db6b | 2078 | struct io_kiocb *nxt; |
f4db7182 | 2079 | |
7a743e22 | 2080 | /* |
f4db7182 PB |
2081 | * A ref is owned by io-wq in which context we're. So, if that's the |
2082 | * last one, it's safe to steal next work. False negatives are Ok, | |
2083 | * it just will be re-punted async in io_put_work() | |
7a743e22 | 2084 | */ |
f4db7182 PB |
2085 | if (refcount_read(&req->refs) != 1) |
2086 | return NULL; | |
7a743e22 | 2087 | |
9b5f7bd9 | 2088 | nxt = io_req_find_next(req); |
6df1db6b | 2089 | return nxt ? &nxt->work : NULL; |
7a743e22 PB |
2090 | } |
2091 | ||
978db57e JA |
2092 | /* |
2093 | * Must only be used if we don't need to care about links, usually from | |
2094 | * within the completion handling itself. | |
2095 | */ | |
2096 | static void __io_double_put_req(struct io_kiocb *req) | |
78e19bbe JA |
2097 | { |
2098 | /* drop both submit and complete references */ | |
2099 | if (refcount_sub_and_test(2, &req->refs)) | |
2100 | __io_free_req(req); | |
2101 | } | |
2102 | ||
978db57e JA |
2103 | static void io_double_put_req(struct io_kiocb *req) |
2104 | { | |
2105 | /* drop both submit and complete references */ | |
2106 | if (refcount_sub_and_test(2, &req->refs)) | |
2107 | io_free_req(req); | |
2108 | } | |
2109 | ||
1d7bb1d5 | 2110 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 2111 | { |
84f97dc2 JA |
2112 | struct io_rings *rings = ctx->rings; |
2113 | ||
ad3eb2c8 JA |
2114 | if (test_bit(0, &ctx->cq_check_overflow)) { |
2115 | /* | |
2116 | * noflush == true is from the waitqueue handler, just ensure | |
2117 | * we wake up the task, and the next invocation will flush the | |
2118 | * entries. We cannot safely to it from here. | |
2119 | */ | |
2120 | if (noflush && !list_empty(&ctx->cq_overflow_list)) | |
2121 | return -1U; | |
1d7bb1d5 | 2122 | |
e6c8aa9a | 2123 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
ad3eb2c8 | 2124 | } |
1d7bb1d5 | 2125 | |
a3a0e43f JA |
2126 | /* See comment at the top of this file */ |
2127 | smp_rmb(); | |
ad3eb2c8 | 2128 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
2129 | } |
2130 | ||
fb5ccc98 PB |
2131 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2132 | { | |
2133 | struct io_rings *rings = ctx->rings; | |
2134 | ||
2135 | /* make sure SQ entry isn't read before tail */ | |
2136 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2137 | } | |
2138 | ||
8ff069bf | 2139 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2140 | { |
8ff069bf | 2141 | unsigned int cflags; |
e94f141b | 2142 | |
bcda7baa JA |
2143 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2144 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2145 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2146 | kfree(kbuf); |
2147 | return cflags; | |
e94f141b JA |
2148 | } |
2149 | ||
8ff069bf | 2150 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2151 | { |
4d954c25 | 2152 | struct io_buffer *kbuf; |
bcda7baa | 2153 | |
4d954c25 | 2154 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2155 | return io_put_kbuf(req, kbuf); |
2156 | } | |
2157 | ||
4c6e277c JA |
2158 | static inline bool io_run_task_work(void) |
2159 | { | |
6200b0ae JA |
2160 | /* |
2161 | * Not safe to run on exiting task, and the task_work handling will | |
2162 | * not add work to such a task. | |
2163 | */ | |
2164 | if (unlikely(current->flags & PF_EXITING)) | |
2165 | return false; | |
4c6e277c JA |
2166 | if (current->task_works) { |
2167 | __set_current_state(TASK_RUNNING); | |
2168 | task_work_run(); | |
2169 | return true; | |
2170 | } | |
2171 | ||
2172 | return false; | |
bcda7baa JA |
2173 | } |
2174 | ||
bbde017a XW |
2175 | static void io_iopoll_queue(struct list_head *again) |
2176 | { | |
2177 | struct io_kiocb *req; | |
2178 | ||
2179 | do { | |
d21ffe7e PB |
2180 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2181 | list_del(&req->inflight_entry); | |
81b68a5c | 2182 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
bbde017a XW |
2183 | } while (!list_empty(again)); |
2184 | } | |
2185 | ||
def596e9 JA |
2186 | /* |
2187 | * Find and free completed poll iocbs | |
2188 | */ | |
2189 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2190 | struct list_head *done) | |
2191 | { | |
8237e045 | 2192 | struct req_batch rb; |
def596e9 | 2193 | struct io_kiocb *req; |
bbde017a XW |
2194 | LIST_HEAD(again); |
2195 | ||
2196 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2197 | smp_rmb(); | |
def596e9 | 2198 | |
5af1d13e | 2199 | io_init_req_batch(&rb); |
def596e9 | 2200 | while (!list_empty(done)) { |
bcda7baa JA |
2201 | int cflags = 0; |
2202 | ||
d21ffe7e | 2203 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a | 2204 | if (READ_ONCE(req->result) == -EAGAIN) { |
56450c20 | 2205 | req->result = 0; |
bbde017a | 2206 | req->iopoll_completed = 0; |
d21ffe7e | 2207 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2208 | continue; |
2209 | } | |
d21ffe7e | 2210 | list_del(&req->inflight_entry); |
def596e9 | 2211 | |
bcda7baa | 2212 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2213 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2214 | |
2215 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2216 | (*nr_events)++; |
2217 | ||
c3524383 | 2218 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2219 | io_req_free_batch(&rb, req); |
def596e9 | 2220 | } |
def596e9 | 2221 | |
09bb8394 | 2222 | io_commit_cqring(ctx); |
32b2244a XW |
2223 | if (ctx->flags & IORING_SETUP_SQPOLL) |
2224 | io_cqring_ev_posted(ctx); | |
2d6500d4 | 2225 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2226 | |
bbde017a XW |
2227 | if (!list_empty(&again)) |
2228 | io_iopoll_queue(&again); | |
581f9810 BM |
2229 | } |
2230 | ||
def596e9 JA |
2231 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2232 | long min) | |
2233 | { | |
2234 | struct io_kiocb *req, *tmp; | |
2235 | LIST_HEAD(done); | |
2236 | bool spin; | |
2237 | int ret; | |
2238 | ||
2239 | /* | |
2240 | * Only spin for completions if we don't have multiple devices hanging | |
2241 | * off our complete list, and we're under the requested amount. | |
2242 | */ | |
2243 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2244 | ||
2245 | ret = 0; | |
d21ffe7e | 2246 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2247 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2248 | |
2249 | /* | |
581f9810 BM |
2250 | * Move completed and retryable entries to our local lists. |
2251 | * If we find a request that requires polling, break out | |
2252 | * and complete those lists first, if we have entries there. | |
def596e9 | 2253 | */ |
65a6543d | 2254 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2255 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2256 | continue; |
2257 | } | |
2258 | if (!list_empty(&done)) | |
2259 | break; | |
2260 | ||
2261 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2262 | if (ret < 0) | |
2263 | break; | |
2264 | ||
3aadc23e PB |
2265 | /* iopoll may have completed current req */ |
2266 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2267 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2268 | |
def596e9 JA |
2269 | if (ret && spin) |
2270 | spin = false; | |
2271 | ret = 0; | |
2272 | } | |
2273 | ||
2274 | if (!list_empty(&done)) | |
2275 | io_iopoll_complete(ctx, nr_events, &done); | |
2276 | ||
2277 | return ret; | |
2278 | } | |
2279 | ||
2280 | /* | |
d195a66e | 2281 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2282 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2283 | * as a non-spinning completion check. | |
2284 | */ | |
2285 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2286 | long min) | |
2287 | { | |
540e32a0 | 2288 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2289 | int ret; |
2290 | ||
2291 | ret = io_do_iopoll(ctx, nr_events, min); | |
2292 | if (ret < 0) | |
2293 | return ret; | |
eba0a4dd | 2294 | if (*nr_events >= min) |
def596e9 JA |
2295 | return 0; |
2296 | } | |
2297 | ||
2298 | return 1; | |
2299 | } | |
2300 | ||
2301 | /* | |
2302 | * We can't just wait for polled events to come to us, we have to actively | |
2303 | * find and complete them. | |
2304 | */ | |
b2edc0a7 | 2305 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2306 | { |
2307 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2308 | return; | |
2309 | ||
2310 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2311 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2312 | unsigned int nr_events = 0; |
2313 | ||
b2edc0a7 | 2314 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2315 | |
b2edc0a7 PB |
2316 | /* let it sleep and repeat later if can't complete a request */ |
2317 | if (nr_events == 0) | |
2318 | break; | |
08f5439f JA |
2319 | /* |
2320 | * Ensure we allow local-to-the-cpu processing to take place, | |
2321 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2322 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2323 | */ |
3fcee5a6 PB |
2324 | if (need_resched()) { |
2325 | mutex_unlock(&ctx->uring_lock); | |
2326 | cond_resched(); | |
2327 | mutex_lock(&ctx->uring_lock); | |
2328 | } | |
def596e9 JA |
2329 | } |
2330 | mutex_unlock(&ctx->uring_lock); | |
2331 | } | |
2332 | ||
7668b92a | 2333 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2334 | { |
7668b92a | 2335 | unsigned int nr_events = 0; |
2b2ed975 | 2336 | int iters = 0, ret = 0; |
500f9fba | 2337 | |
c7849be9 XW |
2338 | /* |
2339 | * We disallow the app entering submit/complete with polling, but we | |
2340 | * still need to lock the ring to prevent racing with polled issue | |
2341 | * that got punted to a workqueue. | |
2342 | */ | |
2343 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2344 | do { |
a3a0e43f JA |
2345 | /* |
2346 | * Don't enter poll loop if we already have events pending. | |
2347 | * If we do, we can potentially be spinning for commands that | |
2348 | * already triggered a CQE (eg in error). | |
2349 | */ | |
1d7bb1d5 | 2350 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
2351 | break; |
2352 | ||
500f9fba JA |
2353 | /* |
2354 | * If a submit got punted to a workqueue, we can have the | |
2355 | * application entering polling for a command before it gets | |
2356 | * issued. That app will hold the uring_lock for the duration | |
2357 | * of the poll right here, so we need to take a breather every | |
2358 | * now and then to ensure that the issue has a chance to add | |
2359 | * the poll to the issued list. Otherwise we can spin here | |
2360 | * forever, while the workqueue is stuck trying to acquire the | |
2361 | * very same mutex. | |
2362 | */ | |
2363 | if (!(++iters & 7)) { | |
2364 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2365 | io_run_task_work(); |
500f9fba JA |
2366 | mutex_lock(&ctx->uring_lock); |
2367 | } | |
2368 | ||
7668b92a | 2369 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2370 | if (ret <= 0) |
2371 | break; | |
2372 | ret = 0; | |
7668b92a | 2373 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2374 | |
500f9fba | 2375 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2376 | return ret; |
2377 | } | |
2378 | ||
491381ce | 2379 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2380 | { |
491381ce JA |
2381 | /* |
2382 | * Tell lockdep we inherited freeze protection from submission | |
2383 | * thread. | |
2384 | */ | |
2385 | if (req->flags & REQ_F_ISREG) { | |
2386 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2387 | |
491381ce | 2388 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2389 | } |
491381ce | 2390 | file_end_write(req->file); |
2b188cc1 JA |
2391 | } |
2392 | ||
a1d7c393 JA |
2393 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
2394 | struct io_comp_state *cs) | |
2b188cc1 | 2395 | { |
9adbd45d | 2396 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2397 | int cflags = 0; |
2b188cc1 | 2398 | |
491381ce JA |
2399 | if (kiocb->ki_flags & IOCB_WRITE) |
2400 | kiocb_end_write(req); | |
2b188cc1 | 2401 | |
4e88d6e7 JA |
2402 | if (res != req->result) |
2403 | req_set_fail_links(req); | |
bcda7baa | 2404 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2405 | cflags = io_put_rw_kbuf(req); |
a1d7c393 | 2406 | __io_req_complete(req, res, cflags, cs); |
ba816ad6 JA |
2407 | } |
2408 | ||
b63534c4 JA |
2409 | #ifdef CONFIG_BLOCK |
2410 | static bool io_resubmit_prep(struct io_kiocb *req, int error) | |
2411 | { | |
2412 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
2413 | ssize_t ret = -ECANCELED; | |
2414 | struct iov_iter iter; | |
2415 | int rw; | |
2416 | ||
2417 | if (error) { | |
2418 | ret = error; | |
2419 | goto end_req; | |
2420 | } | |
2421 | ||
2422 | switch (req->opcode) { | |
2423 | case IORING_OP_READV: | |
2424 | case IORING_OP_READ_FIXED: | |
2425 | case IORING_OP_READ: | |
2426 | rw = READ; | |
2427 | break; | |
2428 | case IORING_OP_WRITEV: | |
2429 | case IORING_OP_WRITE_FIXED: | |
2430 | case IORING_OP_WRITE: | |
2431 | rw = WRITE; | |
2432 | break; | |
2433 | default: | |
2434 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2435 | req->opcode); | |
2436 | goto end_req; | |
2437 | } | |
2438 | ||
e8c2bc1f | 2439 | if (!req->async_data) { |
8f3d7496 JA |
2440 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2441 | if (ret < 0) | |
2442 | goto end_req; | |
2443 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); | |
2444 | if (!ret) | |
2445 | return true; | |
2446 | kfree(iovec); | |
2447 | } else { | |
b63534c4 | 2448 | return true; |
8f3d7496 | 2449 | } |
b63534c4 | 2450 | end_req: |
b63534c4 | 2451 | req_set_fail_links(req); |
e1e16097 | 2452 | io_req_complete(req, ret); |
b63534c4 JA |
2453 | return false; |
2454 | } | |
b63534c4 JA |
2455 | #endif |
2456 | ||
2457 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2458 | { | |
2459 | #ifdef CONFIG_BLOCK | |
355afaeb | 2460 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2461 | int ret; |
2462 | ||
355afaeb JA |
2463 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2464 | return false; | |
b63534c4 JA |
2465 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
2466 | return false; | |
2467 | ||
fdee946d | 2468 | ret = io_sq_thread_acquire_mm(req->ctx, req); |
6d816e08 | 2469 | |
fdee946d JA |
2470 | if (io_resubmit_prep(req, ret)) { |
2471 | refcount_inc(&req->refs); | |
2472 | io_queue_async_work(req); | |
b63534c4 | 2473 | return true; |
fdee946d JA |
2474 | } |
2475 | ||
b63534c4 JA |
2476 | #endif |
2477 | return false; | |
2478 | } | |
2479 | ||
a1d7c393 JA |
2480 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2481 | struct io_comp_state *cs) | |
2482 | { | |
2483 | if (!io_rw_reissue(req, res)) | |
2484 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2485 | } |
2486 | ||
2487 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2488 | { | |
9adbd45d | 2489 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2490 | |
a1d7c393 | 2491 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2492 | } |
2493 | ||
def596e9 JA |
2494 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2495 | { | |
9adbd45d | 2496 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2497 | |
491381ce JA |
2498 | if (kiocb->ki_flags & IOCB_WRITE) |
2499 | kiocb_end_write(req); | |
def596e9 | 2500 | |
2d7d6792 | 2501 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2502 | req_set_fail_links(req); |
bbde017a XW |
2503 | |
2504 | WRITE_ONCE(req->result, res); | |
2505 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2506 | smp_wmb(); |
2507 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2508 | } |
2509 | ||
2510 | /* | |
2511 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2512 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2513 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2514 | * accessing the kiocb cookie. | |
2515 | */ | |
2516 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
2517 | { | |
2518 | struct io_ring_ctx *ctx = req->ctx; | |
2519 | ||
2520 | /* | |
2521 | * Track whether we have multiple files in our lists. This will impact | |
2522 | * how we do polling eventually, not spinning if we're on potentially | |
2523 | * different devices. | |
2524 | */ | |
540e32a0 | 2525 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2526 | ctx->poll_multi_file = false; |
2527 | } else if (!ctx->poll_multi_file) { | |
2528 | struct io_kiocb *list_req; | |
2529 | ||
540e32a0 | 2530 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2531 | inflight_entry); |
9adbd45d | 2532 | if (list_req->file != req->file) |
def596e9 JA |
2533 | ctx->poll_multi_file = true; |
2534 | } | |
2535 | ||
2536 | /* | |
2537 | * For fast devices, IO may have already completed. If it has, add | |
2538 | * it to the front so we find it first. | |
2539 | */ | |
65a6543d | 2540 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2541 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2542 | else |
d21ffe7e | 2543 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2544 | |
534ca6d6 JA |
2545 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
2546 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2547 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2548 | } |
2549 | ||
9f13c35b | 2550 | static void __io_state_file_put(struct io_submit_state *state) |
9a56a232 | 2551 | { |
06ef3608 PB |
2552 | if (state->has_refs) |
2553 | fput_many(state->file, state->has_refs); | |
9f13c35b PB |
2554 | state->file = NULL; |
2555 | } | |
2556 | ||
2557 | static inline void io_state_file_put(struct io_submit_state *state) | |
2558 | { | |
2559 | if (state->file) | |
2560 | __io_state_file_put(state); | |
9a56a232 JA |
2561 | } |
2562 | ||
2563 | /* | |
2564 | * Get as many references to a file as we have IOs left in this submission, | |
2565 | * assuming most submissions are for one file, or at least that each file | |
2566 | * has more than one submission. | |
2567 | */ | |
8da11c19 | 2568 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2569 | { |
2570 | if (!state) | |
2571 | return fget(fd); | |
2572 | ||
2573 | if (state->file) { | |
2574 | if (state->fd == fd) { | |
06ef3608 | 2575 | state->has_refs--; |
9a56a232 JA |
2576 | state->ios_left--; |
2577 | return state->file; | |
2578 | } | |
9f13c35b | 2579 | __io_state_file_put(state); |
9a56a232 JA |
2580 | } |
2581 | state->file = fget_many(fd, state->ios_left); | |
2582 | if (!state->file) | |
2583 | return NULL; | |
2584 | ||
2585 | state->fd = fd; | |
9a56a232 | 2586 | state->ios_left--; |
06ef3608 | 2587 | state->has_refs = state->ios_left; |
9a56a232 JA |
2588 | return state->file; |
2589 | } | |
2590 | ||
4503b767 JA |
2591 | static bool io_bdev_nowait(struct block_device *bdev) |
2592 | { | |
2593 | #ifdef CONFIG_BLOCK | |
2594 | return !bdev || queue_is_mq(bdev_get_queue(bdev)); | |
2595 | #else | |
2596 | return true; | |
2597 | #endif | |
2598 | } | |
2599 | ||
2b188cc1 JA |
2600 | /* |
2601 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2602 | * any file. For now, just ensure that anything potentially problematic is done | |
2603 | * inline. | |
2604 | */ | |
af197f50 | 2605 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2606 | { |
2607 | umode_t mode = file_inode(file)->i_mode; | |
2608 | ||
4503b767 JA |
2609 | if (S_ISBLK(mode)) { |
2610 | if (io_bdev_nowait(file->f_inode->i_bdev)) | |
2611 | return true; | |
2612 | return false; | |
2613 | } | |
2614 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2615 | return true; |
4503b767 JA |
2616 | if (S_ISREG(mode)) { |
2617 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
2618 | file->f_op != &io_uring_fops) | |
2619 | return true; | |
2620 | return false; | |
2621 | } | |
2b188cc1 | 2622 | |
c5b85625 JA |
2623 | /* any ->read/write should understand O_NONBLOCK */ |
2624 | if (file->f_flags & O_NONBLOCK) | |
2625 | return true; | |
2626 | ||
af197f50 JA |
2627 | if (!(file->f_mode & FMODE_NOWAIT)) |
2628 | return false; | |
2629 | ||
2630 | if (rw == READ) | |
2631 | return file->f_op->read_iter != NULL; | |
2632 | ||
2633 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2634 | } |
2635 | ||
3529d8c2 JA |
2636 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2637 | bool force_nonblock) | |
2b188cc1 | 2638 | { |
def596e9 | 2639 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2640 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2641 | unsigned ioprio; |
2642 | int ret; | |
2b188cc1 | 2643 | |
491381ce JA |
2644 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2645 | req->flags |= REQ_F_ISREG; | |
2646 | ||
2b188cc1 | 2647 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2648 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2649 | req->flags |= REQ_F_CUR_POS; | |
2650 | kiocb->ki_pos = req->file->f_pos; | |
2651 | } | |
2b188cc1 | 2652 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2653 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2654 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2655 | if (unlikely(ret)) | |
2656 | return ret; | |
2b188cc1 JA |
2657 | |
2658 | ioprio = READ_ONCE(sqe->ioprio); | |
2659 | if (ioprio) { | |
2660 | ret = ioprio_check_cap(ioprio); | |
2661 | if (ret) | |
09bb8394 | 2662 | return ret; |
2b188cc1 JA |
2663 | |
2664 | kiocb->ki_ioprio = ioprio; | |
2665 | } else | |
2666 | kiocb->ki_ioprio = get_current_ioprio(); | |
2667 | ||
8449eeda | 2668 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2669 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2670 | req->flags |= REQ_F_NOWAIT; |
2671 | ||
2672 | if (force_nonblock) | |
2b188cc1 | 2673 | kiocb->ki_flags |= IOCB_NOWAIT; |
8449eeda | 2674 | |
def596e9 | 2675 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2676 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2677 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2678 | return -EOPNOTSUPP; |
2b188cc1 | 2679 | |
def596e9 JA |
2680 | kiocb->ki_flags |= IOCB_HIPRI; |
2681 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2682 | req->iopoll_completed = 0; |
def596e9 | 2683 | } else { |
09bb8394 JA |
2684 | if (kiocb->ki_flags & IOCB_HIPRI) |
2685 | return -EINVAL; | |
def596e9 JA |
2686 | kiocb->ki_complete = io_complete_rw; |
2687 | } | |
9adbd45d | 2688 | |
3529d8c2 JA |
2689 | req->rw.addr = READ_ONCE(sqe->addr); |
2690 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2691 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2692 | return 0; |
2b188cc1 JA |
2693 | } |
2694 | ||
2695 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2696 | { | |
2697 | switch (ret) { | |
2698 | case -EIOCBQUEUED: | |
2699 | break; | |
2700 | case -ERESTARTSYS: | |
2701 | case -ERESTARTNOINTR: | |
2702 | case -ERESTARTNOHAND: | |
2703 | case -ERESTART_RESTARTBLOCK: | |
2704 | /* | |
2705 | * We can't just restart the syscall, since previously | |
2706 | * submitted sqes may already be in progress. Just fail this | |
2707 | * IO with EINTR. | |
2708 | */ | |
2709 | ret = -EINTR; | |
df561f66 | 2710 | fallthrough; |
2b188cc1 JA |
2711 | default: |
2712 | kiocb->ki_complete(kiocb, ret, 0); | |
2713 | } | |
2714 | } | |
2715 | ||
a1d7c393 JA |
2716 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2717 | struct io_comp_state *cs) | |
ba816ad6 | 2718 | { |
ba04291e | 2719 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2720 | struct io_async_rw *io = req->async_data; |
ba04291e | 2721 | |
227c0c96 | 2722 | /* add previously done IO, if any */ |
e8c2bc1f | 2723 | if (io && io->bytes_done > 0) { |
227c0c96 | 2724 | if (ret < 0) |
e8c2bc1f | 2725 | ret = io->bytes_done; |
227c0c96 | 2726 | else |
e8c2bc1f | 2727 | ret += io->bytes_done; |
227c0c96 JA |
2728 | } |
2729 | ||
ba04291e JA |
2730 | if (req->flags & REQ_F_CUR_POS) |
2731 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2732 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 2733 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
2734 | else |
2735 | io_rw_done(kiocb, ret); | |
2736 | } | |
2737 | ||
9adbd45d | 2738 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 2739 | struct iov_iter *iter) |
edafccee | 2740 | { |
9adbd45d JA |
2741 | struct io_ring_ctx *ctx = req->ctx; |
2742 | size_t len = req->rw.len; | |
edafccee | 2743 | struct io_mapped_ubuf *imu; |
4be1c615 | 2744 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2745 | size_t offset; |
2746 | u64 buf_addr; | |
2747 | ||
edafccee JA |
2748 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2749 | return -EFAULT; | |
edafccee JA |
2750 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2751 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2752 | buf_addr = req->rw.addr; |
edafccee JA |
2753 | |
2754 | /* overflow */ | |
2755 | if (buf_addr + len < buf_addr) | |
2756 | return -EFAULT; | |
2757 | /* not inside the mapped region */ | |
2758 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2759 | return -EFAULT; | |
2760 | ||
2761 | /* | |
2762 | * May not be a start of buffer, set size appropriately | |
2763 | * and advance us to the beginning. | |
2764 | */ | |
2765 | offset = buf_addr - imu->ubuf; | |
2766 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2767 | |
2768 | if (offset) { | |
2769 | /* | |
2770 | * Don't use iov_iter_advance() here, as it's really slow for | |
2771 | * using the latter parts of a big fixed buffer - it iterates | |
2772 | * over each segment manually. We can cheat a bit here, because | |
2773 | * we know that: | |
2774 | * | |
2775 | * 1) it's a BVEC iter, we set it up | |
2776 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2777 | * first and last bvec | |
2778 | * | |
2779 | * So just find our index, and adjust the iterator afterwards. | |
2780 | * If the offset is within the first bvec (or the whole first | |
2781 | * bvec, just use iov_iter_advance(). This makes it easier | |
2782 | * since we can just skip the first segment, which may not | |
2783 | * be PAGE_SIZE aligned. | |
2784 | */ | |
2785 | const struct bio_vec *bvec = imu->bvec; | |
2786 | ||
2787 | if (offset <= bvec->bv_len) { | |
2788 | iov_iter_advance(iter, offset); | |
2789 | } else { | |
2790 | unsigned long seg_skip; | |
2791 | ||
2792 | /* skip first vec */ | |
2793 | offset -= bvec->bv_len; | |
2794 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2795 | ||
2796 | iter->bvec = bvec + seg_skip; | |
2797 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2798 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2799 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2800 | } |
2801 | } | |
2802 | ||
5e559561 | 2803 | return len; |
edafccee JA |
2804 | } |
2805 | ||
bcda7baa JA |
2806 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2807 | { | |
2808 | if (needs_lock) | |
2809 | mutex_unlock(&ctx->uring_lock); | |
2810 | } | |
2811 | ||
2812 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2813 | { | |
2814 | /* | |
2815 | * "Normal" inline submissions always hold the uring_lock, since we | |
2816 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2817 | * The only exception is when we've detached the request and issue it | |
2818 | * from an async worker thread, grab the lock for that case. | |
2819 | */ | |
2820 | if (needs_lock) | |
2821 | mutex_lock(&ctx->uring_lock); | |
2822 | } | |
2823 | ||
2824 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2825 | int bgid, struct io_buffer *kbuf, | |
2826 | bool needs_lock) | |
2827 | { | |
2828 | struct io_buffer *head; | |
2829 | ||
2830 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2831 | return kbuf; | |
2832 | ||
2833 | io_ring_submit_lock(req->ctx, needs_lock); | |
2834 | ||
2835 | lockdep_assert_held(&req->ctx->uring_lock); | |
2836 | ||
2837 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2838 | if (head) { | |
2839 | if (!list_empty(&head->list)) { | |
2840 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2841 | list); | |
2842 | list_del(&kbuf->list); | |
2843 | } else { | |
2844 | kbuf = head; | |
2845 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2846 | } | |
2847 | if (*len > kbuf->len) | |
2848 | *len = kbuf->len; | |
2849 | } else { | |
2850 | kbuf = ERR_PTR(-ENOBUFS); | |
2851 | } | |
2852 | ||
2853 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2854 | ||
2855 | return kbuf; | |
2856 | } | |
2857 | ||
4d954c25 JA |
2858 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2859 | bool needs_lock) | |
2860 | { | |
2861 | struct io_buffer *kbuf; | |
4f4eeba8 | 2862 | u16 bgid; |
4d954c25 JA |
2863 | |
2864 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2865 | bgid = req->buf_index; |
4d954c25 JA |
2866 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2867 | if (IS_ERR(kbuf)) | |
2868 | return kbuf; | |
2869 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2870 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2871 | return u64_to_user_ptr(kbuf->addr); | |
2872 | } | |
2873 | ||
2874 | #ifdef CONFIG_COMPAT | |
2875 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2876 | bool needs_lock) | |
2877 | { | |
2878 | struct compat_iovec __user *uiov; | |
2879 | compat_ssize_t clen; | |
2880 | void __user *buf; | |
2881 | ssize_t len; | |
2882 | ||
2883 | uiov = u64_to_user_ptr(req->rw.addr); | |
2884 | if (!access_ok(uiov, sizeof(*uiov))) | |
2885 | return -EFAULT; | |
2886 | if (__get_user(clen, &uiov->iov_len)) | |
2887 | return -EFAULT; | |
2888 | if (clen < 0) | |
2889 | return -EINVAL; | |
2890 | ||
2891 | len = clen; | |
2892 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2893 | if (IS_ERR(buf)) | |
2894 | return PTR_ERR(buf); | |
2895 | iov[0].iov_base = buf; | |
2896 | iov[0].iov_len = (compat_size_t) len; | |
2897 | return 0; | |
2898 | } | |
2899 | #endif | |
2900 | ||
2901 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2902 | bool needs_lock) | |
2903 | { | |
2904 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2905 | void __user *buf; | |
2906 | ssize_t len; | |
2907 | ||
2908 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2909 | return -EFAULT; | |
2910 | ||
2911 | len = iov[0].iov_len; | |
2912 | if (len < 0) | |
2913 | return -EINVAL; | |
2914 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2915 | if (IS_ERR(buf)) | |
2916 | return PTR_ERR(buf); | |
2917 | iov[0].iov_base = buf; | |
2918 | iov[0].iov_len = len; | |
2919 | return 0; | |
2920 | } | |
2921 | ||
2922 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2923 | bool needs_lock) | |
2924 | { | |
dddb3e26 JA |
2925 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2926 | struct io_buffer *kbuf; | |
2927 | ||
2928 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2929 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2930 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2931 | return 0; |
dddb3e26 | 2932 | } |
4d954c25 JA |
2933 | if (!req->rw.len) |
2934 | return 0; | |
2935 | else if (req->rw.len > 1) | |
2936 | return -EINVAL; | |
2937 | ||
2938 | #ifdef CONFIG_COMPAT | |
2939 | if (req->ctx->compat) | |
2940 | return io_compat_import(req, iov, needs_lock); | |
2941 | #endif | |
2942 | ||
2943 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2944 | } | |
2945 | ||
8452fd0c JA |
2946 | static ssize_t __io_import_iovec(int rw, struct io_kiocb *req, |
2947 | struct iovec **iovec, struct iov_iter *iter, | |
2948 | bool needs_lock) | |
2b188cc1 | 2949 | { |
9adbd45d JA |
2950 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2951 | size_t sqe_len = req->rw.len; | |
4d954c25 | 2952 | ssize_t ret; |
edafccee JA |
2953 | u8 opcode; |
2954 | ||
d625c6ee | 2955 | opcode = req->opcode; |
7d009165 | 2956 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2957 | *iovec = NULL; |
9adbd45d | 2958 | return io_import_fixed(req, rw, iter); |
edafccee | 2959 | } |
2b188cc1 | 2960 | |
bcda7baa | 2961 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2962 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2963 | return -EINVAL; |
2964 | ||
3a6820f2 | 2965 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2966 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2967 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2968 | if (IS_ERR(buf)) |
4d954c25 | 2969 | return PTR_ERR(buf); |
3f9d6441 | 2970 | req->rw.len = sqe_len; |
bcda7baa JA |
2971 | } |
2972 | ||
3a6820f2 JA |
2973 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
2974 | *iovec = NULL; | |
3a901598 | 2975 | return ret < 0 ? ret : sqe_len; |
3a6820f2 JA |
2976 | } |
2977 | ||
4d954c25 JA |
2978 | if (req->flags & REQ_F_BUFFER_SELECT) { |
2979 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
2980 | if (!ret) { |
2981 | ret = (*iovec)->iov_len; | |
2982 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
2983 | } | |
4d954c25 JA |
2984 | *iovec = NULL; |
2985 | return ret; | |
2986 | } | |
2987 | ||
2b188cc1 | 2988 | #ifdef CONFIG_COMPAT |
cf6fd4bd | 2989 | if (req->ctx->compat) |
2b188cc1 JA |
2990 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
2991 | iovec, iter); | |
2992 | #endif | |
2993 | ||
2994 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
2995 | } | |
2996 | ||
8452fd0c JA |
2997 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
2998 | struct iovec **iovec, struct iov_iter *iter, | |
2999 | bool needs_lock) | |
3000 | { | |
e8c2bc1f JA |
3001 | struct io_async_rw *iorw = req->async_data; |
3002 | ||
3003 | if (!iorw) | |
8452fd0c JA |
3004 | return __io_import_iovec(rw, req, iovec, iter, needs_lock); |
3005 | *iovec = NULL; | |
e8c2bc1f | 3006 | return iov_iter_count(&iorw->iter); |
8452fd0c JA |
3007 | } |
3008 | ||
0fef9483 JA |
3009 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3010 | { | |
5b09e37e | 3011 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3012 | } |
3013 | ||
31b51510 | 3014 | /* |
32960613 JA |
3015 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3016 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3017 | */ |
32960613 JA |
3018 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
3019 | struct iov_iter *iter) | |
3020 | { | |
3021 | ssize_t ret = 0; | |
3022 | ||
3023 | /* | |
3024 | * Don't support polled IO through this interface, and we can't | |
3025 | * support non-blocking either. For the latter, this just causes | |
3026 | * the kiocb to be handled from an async context. | |
3027 | */ | |
3028 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3029 | return -EOPNOTSUPP; | |
3030 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3031 | return -EAGAIN; | |
3032 | ||
3033 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3034 | struct iovec iovec; |
32960613 JA |
3035 | ssize_t nr; |
3036 | ||
311ae9e1 PB |
3037 | if (!iov_iter_is_bvec(iter)) { |
3038 | iovec = iov_iter_iovec(iter); | |
3039 | } else { | |
3040 | /* fixed buffers import bvec */ | |
3041 | iovec.iov_base = kmap(iter->bvec->bv_page) | |
3042 | + iter->iov_offset; | |
3043 | iovec.iov_len = min(iter->count, | |
3044 | iter->bvec->bv_len - iter->iov_offset); | |
3045 | } | |
3046 | ||
32960613 JA |
3047 | if (rw == READ) { |
3048 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3049 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3050 | } else { |
3051 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3052 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3053 | } |
3054 | ||
311ae9e1 PB |
3055 | if (iov_iter_is_bvec(iter)) |
3056 | kunmap(iter->bvec->bv_page); | |
3057 | ||
32960613 JA |
3058 | if (nr < 0) { |
3059 | if (!ret) | |
3060 | ret = nr; | |
3061 | break; | |
3062 | } | |
3063 | ret += nr; | |
3064 | if (nr != iovec.iov_len) | |
3065 | break; | |
3066 | iov_iter_advance(iter, nr); | |
3067 | } | |
3068 | ||
3069 | return ret; | |
3070 | } | |
3071 | ||
ff6165b2 JA |
3072 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3073 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3074 | { |
e8c2bc1f | 3075 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3076 | |
ff6165b2 | 3077 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3078 | rw->free_iovec = iovec; |
227c0c96 | 3079 | rw->bytes_done = 0; |
ff6165b2 JA |
3080 | /* can only be fixed buffers, no need to do anything */ |
3081 | if (iter->type == ITER_BVEC) | |
3082 | return; | |
b64e3444 | 3083 | if (!iovec) { |
ff6165b2 JA |
3084 | unsigned iov_off = 0; |
3085 | ||
3086 | rw->iter.iov = rw->fast_iov; | |
3087 | if (iter->iov != fast_iov) { | |
3088 | iov_off = iter->iov - fast_iov; | |
3089 | rw->iter.iov += iov_off; | |
3090 | } | |
3091 | if (rw->fast_iov != fast_iov) | |
3092 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3093 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3094 | } else { |
3095 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3096 | } |
3097 | } | |
3098 | ||
e8c2bc1f | 3099 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3100 | { |
e8c2bc1f JA |
3101 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3102 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3103 | return req->async_data == NULL; | |
3d9932a8 XW |
3104 | } |
3105 | ||
e8c2bc1f | 3106 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3107 | { |
e8c2bc1f | 3108 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3109 | return 0; |
3d9932a8 | 3110 | |
e8c2bc1f | 3111 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3112 | } |
3113 | ||
ff6165b2 JA |
3114 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3115 | const struct iovec *fast_iov, | |
227c0c96 | 3116 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3117 | { |
e8c2bc1f | 3118 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3119 | return 0; |
e8c2bc1f JA |
3120 | if (!req->async_data) { |
3121 | if (__io_alloc_async_data(req)) | |
5d204bcf | 3122 | return -ENOMEM; |
b7bb4f7d | 3123 | |
ff6165b2 | 3124 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3125 | } |
b7bb4f7d | 3126 | return 0; |
f67676d1 JA |
3127 | } |
3128 | ||
c3e330a4 PB |
3129 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw, |
3130 | bool force_nonblock) | |
3131 | { | |
e8c2bc1f | 3132 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3133 | struct iovec *iov = iorw->fast_iov; |
c3e330a4 PB |
3134 | ssize_t ret; |
3135 | ||
c183edff | 3136 | ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock); |
c3e330a4 PB |
3137 | if (unlikely(ret < 0)) |
3138 | return ret; | |
3139 | ||
ab0b196c PB |
3140 | iorw->bytes_done = 0; |
3141 | iorw->free_iovec = iov; | |
3142 | if (iov) | |
3143 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3144 | return 0; |
3145 | } | |
3146 | ||
3529d8c2 JA |
3147 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
3148 | bool force_nonblock) | |
f67676d1 JA |
3149 | { |
3150 | ssize_t ret; | |
3151 | ||
3529d8c2 JA |
3152 | ret = io_prep_rw(req, sqe, force_nonblock); |
3153 | if (ret) | |
3154 | return ret; | |
f67676d1 | 3155 | |
3529d8c2 JA |
3156 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3157 | return -EBADF; | |
f67676d1 | 3158 | |
5f798bea | 3159 | /* either don't need iovec imported or already have it */ |
2d199895 | 3160 | if (!req->async_data) |
3529d8c2 | 3161 | return 0; |
c3e330a4 | 3162 | return io_rw_prep_async(req, READ, force_nonblock); |
f67676d1 JA |
3163 | } |
3164 | ||
c1dd91d1 JA |
3165 | /* |
3166 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3167 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3168 | * This gets called when the page is unlocked, and we generally expect that to | |
3169 | * happen when the page IO is completed and the page is now uptodate. This will | |
3170 | * queue a task_work based retry of the operation, attempting to copy the data | |
3171 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3172 | * do a thread based blocking retry of the operation. That's the unexpected | |
3173 | * slow path. | |
3174 | */ | |
bcf5a063 JA |
3175 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3176 | int sync, void *arg) | |
3177 | { | |
3178 | struct wait_page_queue *wpq; | |
3179 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3180 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3181 | int ret; |
3182 | ||
3183 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3184 | ||
cdc8fcb4 LT |
3185 | if (!wake_page_match(wpq, key)) |
3186 | return 0; | |
3187 | ||
c8d317aa | 3188 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3189 | list_del_init(&wait->entry); |
3190 | ||
e7375122 | 3191 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3192 | percpu_ref_get(&req->ctx->refs); |
3193 | ||
bcf5a063 JA |
3194 | /* submit ref gets dropped, acquire a new one */ |
3195 | refcount_inc(&req->refs); | |
fd7d6de2 | 3196 | ret = io_req_task_work_add(req, &req->task_work, true); |
bcf5a063 | 3197 | if (unlikely(ret)) { |
c2c4c83c JA |
3198 | struct task_struct *tsk; |
3199 | ||
bcf5a063 | 3200 | /* queue just for cancelation */ |
e7375122 | 3201 | init_task_work(&req->task_work, io_req_task_cancel); |
bcf5a063 | 3202 | tsk = io_wq_get_task(req->ctx->io_wq); |
e7375122 | 3203 | task_work_add(tsk, &req->task_work, 0); |
c2c4c83c | 3204 | wake_up_process(tsk); |
bcf5a063 | 3205 | } |
bcf5a063 JA |
3206 | return 1; |
3207 | } | |
3208 | ||
c1dd91d1 JA |
3209 | /* |
3210 | * This controls whether a given IO request should be armed for async page | |
3211 | * based retry. If we return false here, the request is handed to the async | |
3212 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3213 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3214 | * will either succeed because the page is now uptodate and unlocked, or it | |
3215 | * will register a callback when the page is unlocked at IO completion. Through | |
3216 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3217 | * That retry will attempt the buffered read again. The retry will generally | |
3218 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3219 | * async worker threads for a blocking retry. | |
3220 | */ | |
227c0c96 | 3221 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3222 | { |
e8c2bc1f JA |
3223 | struct io_async_rw *rw = req->async_data; |
3224 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3225 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3226 | |
bcf5a063 JA |
3227 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3228 | if (req->flags & REQ_F_NOWAIT) | |
3229 | return false; | |
f67676d1 | 3230 | |
227c0c96 | 3231 | /* Only for buffered IO */ |
3b2a4439 | 3232 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3233 | return false; |
3b2a4439 | 3234 | |
bcf5a063 JA |
3235 | /* |
3236 | * just use poll if we can, and don't attempt if the fs doesn't | |
3237 | * support callback based unlocks | |
3238 | */ | |
3239 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3240 | return false; | |
f67676d1 | 3241 | |
3b2a4439 JA |
3242 | wait->wait.func = io_async_buf_func; |
3243 | wait->wait.private = req; | |
3244 | wait->wait.flags = 0; | |
3245 | INIT_LIST_HEAD(&wait->wait.entry); | |
3246 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3247 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3248 | kiocb->ki_waitq = wait; |
3b2a4439 | 3249 | return true; |
bcf5a063 JA |
3250 | } |
3251 | ||
3252 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3253 | { | |
3254 | if (req->file->f_op->read_iter) | |
3255 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d GH |
3256 | else if (req->file->f_op->read) |
3257 | return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter); | |
3258 | else | |
3259 | return -EINVAL; | |
f67676d1 JA |
3260 | } |
3261 | ||
a1d7c393 JA |
3262 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3263 | struct io_comp_state *cs) | |
2b188cc1 JA |
3264 | { |
3265 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3266 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3267 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3268 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3269 | ssize_t io_size, ret, ret2; |
31b51510 | 3270 | size_t iov_count; |
f5cac8b1 | 3271 | bool no_async; |
ff6165b2 | 3272 | |
e8c2bc1f JA |
3273 | if (rw) |
3274 | iter = &rw->iter; | |
2b188cc1 | 3275 | |
ff6165b2 | 3276 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
06b76d44 JA |
3277 | if (ret < 0) |
3278 | return ret; | |
eefdf30f | 3279 | iov_count = iov_iter_count(iter); |
fa15bafb PB |
3280 | io_size = ret; |
3281 | req->result = io_size; | |
227c0c96 | 3282 | ret = 0; |
2b188cc1 | 3283 | |
fd6c2e4c JA |
3284 | /* Ensure we clear previously set non-block flag */ |
3285 | if (!force_nonblock) | |
29de5f6a | 3286 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 3287 | |
24c74678 | 3288 | /* If the file doesn't support async, just async punt */ |
f5cac8b1 JA |
3289 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
3290 | if (no_async) | |
f67676d1 | 3291 | goto copy_iov; |
9e645e11 | 3292 | |
0fef9483 | 3293 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count); |
fa15bafb PB |
3294 | if (unlikely(ret)) |
3295 | goto out_free; | |
2b188cc1 | 3296 | |
227c0c96 | 3297 | ret = io_iter_do_read(req, iter); |
32960613 | 3298 | |
227c0c96 JA |
3299 | if (!ret) { |
3300 | goto done; | |
3301 | } else if (ret == -EIOCBQUEUED) { | |
3302 | ret = 0; | |
3303 | goto out_free; | |
3304 | } else if (ret == -EAGAIN) { | |
eefdf30f JA |
3305 | /* IOPOLL retry should happen for io-wq threads */ |
3306 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3307 | goto done; |
355afaeb JA |
3308 | /* no retry on NONBLOCK marked file */ |
3309 | if (req->file->f_flags & O_NONBLOCK) | |
3310 | goto done; | |
84216315 JA |
3311 | /* some cases will consume bytes even on error returns */ |
3312 | iov_iter_revert(iter, iov_count - iov_iter_count(iter)); | |
f38c7e3a JA |
3313 | ret = 0; |
3314 | goto copy_iov; | |
227c0c96 | 3315 | } else if (ret < 0) { |
00d23d51 JA |
3316 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
3317 | goto done; | |
227c0c96 JA |
3318 | } |
3319 | ||
3320 | /* read it all, or we did blocking attempt. no retry. */ | |
f91daf56 JA |
3321 | if (!iov_iter_count(iter) || !force_nonblock || |
3322 | (req->file->f_flags & O_NONBLOCK)) | |
227c0c96 JA |
3323 | goto done; |
3324 | ||
3325 | io_size -= ret; | |
3326 | copy_iov: | |
3327 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
3328 | if (ret2) { | |
3329 | ret = ret2; | |
3330 | goto out_free; | |
3331 | } | |
f5cac8b1 JA |
3332 | if (no_async) |
3333 | return -EAGAIN; | |
e8c2bc1f | 3334 | rw = req->async_data; |
227c0c96 JA |
3335 | /* it's copied and will be cleaned with ->io */ |
3336 | iovec = NULL; | |
3337 | /* now use our persistent iterator, if we aren't already */ | |
e8c2bc1f | 3338 | iter = &rw->iter; |
227c0c96 | 3339 | retry: |
e8c2bc1f | 3340 | rw->bytes_done += ret; |
227c0c96 JA |
3341 | /* if we can retry, do so with the callbacks armed */ |
3342 | if (!io_rw_should_retry(req)) { | |
fa15bafb PB |
3343 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3344 | return -EAGAIN; | |
2b188cc1 | 3345 | } |
227c0c96 JA |
3346 | |
3347 | /* | |
3348 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we | |
3349 | * get -EIOCBQUEUED, then we'll get a notification when the desired | |
3350 | * page gets unlocked. We can also get a partial read here, and if we | |
3351 | * do, then just retry at the new offset. | |
3352 | */ | |
3353 | ret = io_iter_do_read(req, iter); | |
3354 | if (ret == -EIOCBQUEUED) { | |
3355 | ret = 0; | |
3356 | goto out_free; | |
3357 | } else if (ret > 0 && ret < io_size) { | |
3358 | /* we got some bytes, but not all. retry. */ | |
3359 | goto retry; | |
3360 | } | |
3361 | done: | |
3362 | kiocb_done(kiocb, ret, cs); | |
3363 | ret = 0; | |
f67676d1 | 3364 | out_free: |
f261c168 | 3365 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3366 | if (iovec) |
6f2cc166 | 3367 | kfree(iovec); |
2b188cc1 JA |
3368 | return ret; |
3369 | } | |
3370 | ||
3529d8c2 JA |
3371 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
3372 | bool force_nonblock) | |
f67676d1 JA |
3373 | { |
3374 | ssize_t ret; | |
3375 | ||
3529d8c2 JA |
3376 | ret = io_prep_rw(req, sqe, force_nonblock); |
3377 | if (ret) | |
3378 | return ret; | |
f67676d1 | 3379 | |
3529d8c2 JA |
3380 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3381 | return -EBADF; | |
f67676d1 | 3382 | |
5f798bea | 3383 | /* either don't need iovec imported or already have it */ |
2d199895 | 3384 | if (!req->async_data) |
3529d8c2 | 3385 | return 0; |
c3e330a4 | 3386 | return io_rw_prep_async(req, WRITE, force_nonblock); |
f67676d1 JA |
3387 | } |
3388 | ||
a1d7c393 JA |
3389 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3390 | struct io_comp_state *cs) | |
2b188cc1 JA |
3391 | { |
3392 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3393 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3394 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3395 | struct io_async_rw *rw = req->async_data; |
31b51510 | 3396 | size_t iov_count; |
fa15bafb | 3397 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3398 | |
e8c2bc1f JA |
3399 | if (rw) |
3400 | iter = &rw->iter; | |
ff6165b2 JA |
3401 | |
3402 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
06b76d44 JA |
3403 | if (ret < 0) |
3404 | return ret; | |
eefdf30f | 3405 | iov_count = iov_iter_count(iter); |
fa15bafb PB |
3406 | io_size = ret; |
3407 | req->result = io_size; | |
2b188cc1 | 3408 | |
fd6c2e4c JA |
3409 | /* Ensure we clear previously set non-block flag */ |
3410 | if (!force_nonblock) | |
9adbd45d | 3411 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 3412 | |
24c74678 | 3413 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3414 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3415 | goto copy_iov; |
31b51510 | 3416 | |
10d59345 JA |
3417 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3418 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3419 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3420 | goto copy_iov; |
31b51510 | 3421 | |
0fef9483 | 3422 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count); |
fa15bafb PB |
3423 | if (unlikely(ret)) |
3424 | goto out_free; | |
4ed734b0 | 3425 | |
fa15bafb PB |
3426 | /* |
3427 | * Open-code file_start_write here to grab freeze protection, | |
3428 | * which will be released by another thread in | |
3429 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3430 | * released so that it doesn't complain about the held lock when | |
3431 | * we return to userspace. | |
3432 | */ | |
3433 | if (req->flags & REQ_F_ISREG) { | |
3434 | __sb_start_write(file_inode(req->file)->i_sb, | |
3435 | SB_FREEZE_WRITE, true); | |
3436 | __sb_writers_release(file_inode(req->file)->i_sb, | |
3437 | SB_FREEZE_WRITE); | |
3438 | } | |
3439 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3440 | |
fa15bafb | 3441 | if (req->file->f_op->write_iter) |
ff6165b2 | 3442 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3443 | else if (req->file->f_op->write) |
ff6165b2 | 3444 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter); |
2dd2111d GH |
3445 | else |
3446 | ret2 = -EINVAL; | |
4ed734b0 | 3447 | |
fa15bafb PB |
3448 | /* |
3449 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3450 | * retry them without IOCB_NOWAIT. | |
3451 | */ | |
3452 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3453 | ret2 = -EAGAIN; | |
355afaeb JA |
3454 | /* no retry on NONBLOCK marked file */ |
3455 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) | |
3456 | goto done; | |
fa15bafb | 3457 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3458 | /* IOPOLL retry should happen for io-wq threads */ |
3459 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3460 | goto copy_iov; | |
355afaeb | 3461 | done: |
fa15bafb PB |
3462 | kiocb_done(kiocb, ret2, cs); |
3463 | } else { | |
f67676d1 | 3464 | copy_iov: |
84216315 JA |
3465 | /* some cases will consume bytes even on error returns */ |
3466 | iov_iter_revert(iter, iov_count - iov_iter_count(iter)); | |
227c0c96 | 3467 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
ff6165b2 JA |
3468 | if (!ret) |
3469 | return -EAGAIN; | |
2b188cc1 | 3470 | } |
31b51510 | 3471 | out_free: |
f261c168 | 3472 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3473 | if (iovec) |
6f2cc166 | 3474 | kfree(iovec); |
2b188cc1 JA |
3475 | return ret; |
3476 | } | |
3477 | ||
f2a8d5c7 PB |
3478 | static int __io_splice_prep(struct io_kiocb *req, |
3479 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3480 | { |
3481 | struct io_splice* sp = &req->splice; | |
3482 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
3483 | int ret; | |
3484 | ||
3232dd02 PB |
3485 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3486 | return -EINVAL; | |
7d67af2c PB |
3487 | |
3488 | sp->file_in = NULL; | |
7d67af2c PB |
3489 | sp->len = READ_ONCE(sqe->len); |
3490 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3491 | ||
3492 | if (unlikely(sp->flags & ~valid_flags)) | |
3493 | return -EINVAL; | |
3494 | ||
3495 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, | |
3496 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3497 | if (ret) | |
3498 | return ret; | |
3499 | req->flags |= REQ_F_NEED_CLEANUP; | |
3500 | ||
7cdaf587 XW |
3501 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3502 | /* | |
3503 | * Splice operation will be punted aync, and here need to | |
3504 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3505 | */ | |
3506 | io_req_init_async(req); | |
7d67af2c | 3507 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3508 | } |
7d67af2c PB |
3509 | |
3510 | return 0; | |
3511 | } | |
3512 | ||
f2a8d5c7 PB |
3513 | static int io_tee_prep(struct io_kiocb *req, |
3514 | const struct io_uring_sqe *sqe) | |
3515 | { | |
3516 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3517 | return -EINVAL; | |
3518 | return __io_splice_prep(req, sqe); | |
3519 | } | |
3520 | ||
3521 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3522 | { | |
3523 | struct io_splice *sp = &req->splice; | |
3524 | struct file *in = sp->file_in; | |
3525 | struct file *out = sp->file_out; | |
3526 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3527 | long ret = 0; | |
3528 | ||
3529 | if (force_nonblock) | |
3530 | return -EAGAIN; | |
3531 | if (sp->len) | |
3532 | ret = do_tee(in, out, sp->len, flags); | |
3533 | ||
3534 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3535 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3536 | ||
f2a8d5c7 PB |
3537 | if (ret != sp->len) |
3538 | req_set_fail_links(req); | |
e1e16097 | 3539 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3540 | return 0; |
3541 | } | |
3542 | ||
3543 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3544 | { | |
3545 | struct io_splice* sp = &req->splice; | |
3546 | ||
3547 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3548 | sp->off_out = READ_ONCE(sqe->off); | |
3549 | return __io_splice_prep(req, sqe); | |
3550 | } | |
3551 | ||
014db007 | 3552 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3553 | { |
3554 | struct io_splice *sp = &req->splice; | |
3555 | struct file *in = sp->file_in; | |
3556 | struct file *out = sp->file_out; | |
3557 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3558 | loff_t *poff_in, *poff_out; | |
c9687426 | 3559 | long ret = 0; |
7d67af2c | 3560 | |
2fb3e822 PB |
3561 | if (force_nonblock) |
3562 | return -EAGAIN; | |
7d67af2c PB |
3563 | |
3564 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3565 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3566 | |
948a7749 | 3567 | if (sp->len) |
c9687426 | 3568 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3569 | |
3570 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3571 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3572 | ||
7d67af2c PB |
3573 | if (ret != sp->len) |
3574 | req_set_fail_links(req); | |
e1e16097 | 3575 | io_req_complete(req, ret); |
7d67af2c PB |
3576 | return 0; |
3577 | } | |
3578 | ||
2b188cc1 JA |
3579 | /* |
3580 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3581 | */ | |
229a7b63 | 3582 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3583 | { |
3584 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3585 | |
def596e9 JA |
3586 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3587 | return -EINVAL; | |
3588 | ||
229a7b63 | 3589 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3590 | return 0; |
3591 | } | |
3592 | ||
3529d8c2 | 3593 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3594 | { |
6b06314c | 3595 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3596 | |
09bb8394 JA |
3597 | if (!req->file) |
3598 | return -EBADF; | |
c992fe29 | 3599 | |
6b06314c | 3600 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3601 | return -EINVAL; |
edafccee | 3602 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3603 | return -EINVAL; |
3604 | ||
8ed8d3c3 JA |
3605 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3606 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3607 | return -EINVAL; | |
3608 | ||
3609 | req->sync.off = READ_ONCE(sqe->off); | |
3610 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3611 | return 0; |
3612 | } | |
3613 | ||
ac45abc0 | 3614 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3615 | { |
8ed8d3c3 | 3616 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3617 | int ret; |
3618 | ||
ac45abc0 PB |
3619 | /* fsync always requires a blocking context */ |
3620 | if (force_nonblock) | |
3621 | return -EAGAIN; | |
3622 | ||
9adbd45d | 3623 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3624 | end > 0 ? end : LLONG_MAX, |
3625 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3626 | if (ret < 0) | |
3627 | req_set_fail_links(req); | |
e1e16097 | 3628 | io_req_complete(req, ret); |
c992fe29 CH |
3629 | return 0; |
3630 | } | |
3631 | ||
d63d1b5e JA |
3632 | static int io_fallocate_prep(struct io_kiocb *req, |
3633 | const struct io_uring_sqe *sqe) | |
3634 | { | |
3635 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3636 | return -EINVAL; | |
3232dd02 PB |
3637 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3638 | return -EINVAL; | |
d63d1b5e JA |
3639 | |
3640 | req->sync.off = READ_ONCE(sqe->off); | |
3641 | req->sync.len = READ_ONCE(sqe->addr); | |
3642 | req->sync.mode = READ_ONCE(sqe->len); | |
3643 | return 0; | |
3644 | } | |
3645 | ||
014db007 | 3646 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 3647 | { |
ac45abc0 PB |
3648 | int ret; |
3649 | ||
d63d1b5e | 3650 | /* fallocate always requiring blocking context */ |
ac45abc0 | 3651 | if (force_nonblock) |
5d17b4a4 | 3652 | return -EAGAIN; |
ac45abc0 PB |
3653 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3654 | req->sync.len); | |
ac45abc0 PB |
3655 | if (ret < 0) |
3656 | req_set_fail_links(req); | |
e1e16097 | 3657 | io_req_complete(req, ret); |
5d17b4a4 JA |
3658 | return 0; |
3659 | } | |
3660 | ||
ec65fea5 | 3661 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3662 | { |
f8748881 | 3663 | const char __user *fname; |
15b71abe | 3664 | int ret; |
b7bb4f7d | 3665 | |
ec65fea5 | 3666 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3667 | return -EINVAL; |
ec65fea5 | 3668 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3669 | return -EBADF; |
03b1230c | 3670 | |
ec65fea5 PB |
3671 | /* open.how should be already initialised */ |
3672 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3673 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3674 | |
25e72d10 PB |
3675 | req->open.dfd = READ_ONCE(sqe->fd); |
3676 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3677 | req->open.filename = getname(fname); |
15b71abe JA |
3678 | if (IS_ERR(req->open.filename)) { |
3679 | ret = PTR_ERR(req->open.filename); | |
3680 | req->open.filename = NULL; | |
3681 | return ret; | |
3682 | } | |
4022e7af | 3683 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3684 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3685 | return 0; |
03b1230c JA |
3686 | } |
3687 | ||
ec65fea5 PB |
3688 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3689 | { | |
3690 | u64 flags, mode; | |
3691 | ||
4eb8dded JA |
3692 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3693 | return -EINVAL; | |
ec65fea5 PB |
3694 | mode = READ_ONCE(sqe->len); |
3695 | flags = READ_ONCE(sqe->open_flags); | |
3696 | req->open.how = build_open_how(flags, mode); | |
3697 | return __io_openat_prep(req, sqe); | |
3698 | } | |
3699 | ||
cebdb986 | 3700 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3701 | { |
cebdb986 | 3702 | struct open_how __user *how; |
cebdb986 | 3703 | size_t len; |
0fa03c62 JA |
3704 | int ret; |
3705 | ||
4eb8dded JA |
3706 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3707 | return -EINVAL; | |
cebdb986 JA |
3708 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3709 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3710 | if (len < OPEN_HOW_SIZE_VER0) |
3711 | return -EINVAL; | |
3529d8c2 | 3712 | |
cebdb986 JA |
3713 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3714 | len); | |
3715 | if (ret) | |
3716 | return ret; | |
3529d8c2 | 3717 | |
ec65fea5 | 3718 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3719 | } |
3720 | ||
014db007 | 3721 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
3722 | { |
3723 | struct open_flags op; | |
15b71abe JA |
3724 | struct file *file; |
3725 | int ret; | |
3726 | ||
f86cd20c | 3727 | if (force_nonblock) |
15b71abe | 3728 | return -EAGAIN; |
15b71abe | 3729 | |
cebdb986 | 3730 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3731 | if (ret) |
3732 | goto err; | |
3733 | ||
4022e7af | 3734 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3735 | if (ret < 0) |
3736 | goto err; | |
3737 | ||
3738 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3739 | if (IS_ERR(file)) { | |
3740 | put_unused_fd(ret); | |
3741 | ret = PTR_ERR(file); | |
3742 | } else { | |
3743 | fsnotify_open(file); | |
3744 | fd_install(ret, file); | |
3745 | } | |
3746 | err: | |
3747 | putname(req->open.filename); | |
8fef80bf | 3748 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3749 | if (ret < 0) |
3750 | req_set_fail_links(req); | |
e1e16097 | 3751 | io_req_complete(req, ret); |
15b71abe JA |
3752 | return 0; |
3753 | } | |
3754 | ||
014db007 | 3755 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 3756 | { |
014db007 | 3757 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
3758 | } |
3759 | ||
067524e9 JA |
3760 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3761 | const struct io_uring_sqe *sqe) | |
3762 | { | |
3763 | struct io_provide_buf *p = &req->pbuf; | |
3764 | u64 tmp; | |
3765 | ||
3766 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3767 | return -EINVAL; | |
3768 | ||
3769 | tmp = READ_ONCE(sqe->fd); | |
3770 | if (!tmp || tmp > USHRT_MAX) | |
3771 | return -EINVAL; | |
3772 | ||
3773 | memset(p, 0, sizeof(*p)); | |
3774 | p->nbufs = tmp; | |
3775 | p->bgid = READ_ONCE(sqe->buf_group); | |
3776 | return 0; | |
3777 | } | |
3778 | ||
3779 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3780 | int bgid, unsigned nbufs) | |
3781 | { | |
3782 | unsigned i = 0; | |
3783 | ||
3784 | /* shouldn't happen */ | |
3785 | if (!nbufs) | |
3786 | return 0; | |
3787 | ||
3788 | /* the head kbuf is the list itself */ | |
3789 | while (!list_empty(&buf->list)) { | |
3790 | struct io_buffer *nxt; | |
3791 | ||
3792 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3793 | list_del(&nxt->list); | |
3794 | kfree(nxt); | |
3795 | if (++i == nbufs) | |
3796 | return i; | |
3797 | } | |
3798 | i++; | |
3799 | kfree(buf); | |
3800 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3801 | ||
3802 | return i; | |
3803 | } | |
3804 | ||
229a7b63 JA |
3805 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
3806 | struct io_comp_state *cs) | |
067524e9 JA |
3807 | { |
3808 | struct io_provide_buf *p = &req->pbuf; | |
3809 | struct io_ring_ctx *ctx = req->ctx; | |
3810 | struct io_buffer *head; | |
3811 | int ret = 0; | |
3812 | ||
3813 | io_ring_submit_lock(ctx, !force_nonblock); | |
3814 | ||
3815 | lockdep_assert_held(&ctx->uring_lock); | |
3816 | ||
3817 | ret = -ENOENT; | |
3818 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3819 | if (head) | |
3820 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
3821 | ||
3822 | io_ring_submit_lock(ctx, !force_nonblock); | |
3823 | if (ret < 0) | |
3824 | req_set_fail_links(req); | |
229a7b63 | 3825 | __io_req_complete(req, ret, 0, cs); |
067524e9 JA |
3826 | return 0; |
3827 | } | |
3828 | ||
ddf0322d JA |
3829 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3830 | const struct io_uring_sqe *sqe) | |
3831 | { | |
3832 | struct io_provide_buf *p = &req->pbuf; | |
3833 | u64 tmp; | |
3834 | ||
3835 | if (sqe->ioprio || sqe->rw_flags) | |
3836 | return -EINVAL; | |
3837 | ||
3838 | tmp = READ_ONCE(sqe->fd); | |
3839 | if (!tmp || tmp > USHRT_MAX) | |
3840 | return -E2BIG; | |
3841 | p->nbufs = tmp; | |
3842 | p->addr = READ_ONCE(sqe->addr); | |
3843 | p->len = READ_ONCE(sqe->len); | |
3844 | ||
efe68c1c | 3845 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3846 | return -EFAULT; |
3847 | ||
3848 | p->bgid = READ_ONCE(sqe->buf_group); | |
3849 | tmp = READ_ONCE(sqe->off); | |
3850 | if (tmp > USHRT_MAX) | |
3851 | return -E2BIG; | |
3852 | p->bid = tmp; | |
3853 | return 0; | |
3854 | } | |
3855 | ||
3856 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3857 | { | |
3858 | struct io_buffer *buf; | |
3859 | u64 addr = pbuf->addr; | |
3860 | int i, bid = pbuf->bid; | |
3861 | ||
3862 | for (i = 0; i < pbuf->nbufs; i++) { | |
3863 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3864 | if (!buf) | |
3865 | break; | |
3866 | ||
3867 | buf->addr = addr; | |
3868 | buf->len = pbuf->len; | |
3869 | buf->bid = bid; | |
3870 | addr += pbuf->len; | |
3871 | bid++; | |
3872 | if (!*head) { | |
3873 | INIT_LIST_HEAD(&buf->list); | |
3874 | *head = buf; | |
3875 | } else { | |
3876 | list_add_tail(&buf->list, &(*head)->list); | |
3877 | } | |
3878 | } | |
3879 | ||
3880 | return i ? i : -ENOMEM; | |
3881 | } | |
3882 | ||
229a7b63 JA |
3883 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
3884 | struct io_comp_state *cs) | |
ddf0322d JA |
3885 | { |
3886 | struct io_provide_buf *p = &req->pbuf; | |
3887 | struct io_ring_ctx *ctx = req->ctx; | |
3888 | struct io_buffer *head, *list; | |
3889 | int ret = 0; | |
3890 | ||
3891 | io_ring_submit_lock(ctx, !force_nonblock); | |
3892 | ||
3893 | lockdep_assert_held(&ctx->uring_lock); | |
3894 | ||
3895 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3896 | ||
3897 | ret = io_add_buffers(p, &head); | |
3898 | if (ret < 0) | |
3899 | goto out; | |
3900 | ||
3901 | if (!list) { | |
3902 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
3903 | GFP_KERNEL); | |
3904 | if (ret < 0) { | |
067524e9 | 3905 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
3906 | goto out; |
3907 | } | |
3908 | } | |
3909 | out: | |
3910 | io_ring_submit_unlock(ctx, !force_nonblock); | |
3911 | if (ret < 0) | |
3912 | req_set_fail_links(req); | |
229a7b63 | 3913 | __io_req_complete(req, ret, 0, cs); |
ddf0322d | 3914 | return 0; |
cebdb986 JA |
3915 | } |
3916 | ||
3e4827b0 JA |
3917 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
3918 | const struct io_uring_sqe *sqe) | |
3919 | { | |
3920 | #if defined(CONFIG_EPOLL) | |
3921 | if (sqe->ioprio || sqe->buf_index) | |
3922 | return -EINVAL; | |
6ca56f84 | 3923 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 3924 | return -EINVAL; |
3e4827b0 JA |
3925 | |
3926 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
3927 | req->epoll.op = READ_ONCE(sqe->len); | |
3928 | req->epoll.fd = READ_ONCE(sqe->off); | |
3929 | ||
3930 | if (ep_op_has_event(req->epoll.op)) { | |
3931 | struct epoll_event __user *ev; | |
3932 | ||
3933 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3934 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
3935 | return -EFAULT; | |
3936 | } | |
3937 | ||
3938 | return 0; | |
3939 | #else | |
3940 | return -EOPNOTSUPP; | |
3941 | #endif | |
3942 | } | |
3943 | ||
229a7b63 JA |
3944 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
3945 | struct io_comp_state *cs) | |
3e4827b0 JA |
3946 | { |
3947 | #if defined(CONFIG_EPOLL) | |
3948 | struct io_epoll *ie = &req->epoll; | |
3949 | int ret; | |
3950 | ||
3951 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
3952 | if (force_nonblock && ret == -EAGAIN) | |
3953 | return -EAGAIN; | |
3954 | ||
3955 | if (ret < 0) | |
3956 | req_set_fail_links(req); | |
229a7b63 | 3957 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
3958 | return 0; |
3959 | #else | |
3960 | return -EOPNOTSUPP; | |
3961 | #endif | |
3962 | } | |
3963 | ||
c1ca757b JA |
3964 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3965 | { | |
3966 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
3967 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
3968 | return -EINVAL; | |
3232dd02 PB |
3969 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3970 | return -EINVAL; | |
c1ca757b JA |
3971 | |
3972 | req->madvise.addr = READ_ONCE(sqe->addr); | |
3973 | req->madvise.len = READ_ONCE(sqe->len); | |
3974 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
3975 | return 0; | |
3976 | #else | |
3977 | return -EOPNOTSUPP; | |
3978 | #endif | |
3979 | } | |
3980 | ||
014db007 | 3981 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
3982 | { |
3983 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
3984 | struct io_madvise *ma = &req->madvise; | |
3985 | int ret; | |
3986 | ||
3987 | if (force_nonblock) | |
3988 | return -EAGAIN; | |
3989 | ||
3990 | ret = do_madvise(ma->addr, ma->len, ma->advice); | |
3991 | if (ret < 0) | |
3992 | req_set_fail_links(req); | |
e1e16097 | 3993 | io_req_complete(req, ret); |
c1ca757b JA |
3994 | return 0; |
3995 | #else | |
3996 | return -EOPNOTSUPP; | |
3997 | #endif | |
3998 | } | |
3999 | ||
4840e418 JA |
4000 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4001 | { | |
4002 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4003 | return -EINVAL; | |
3232dd02 PB |
4004 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4005 | return -EINVAL; | |
4840e418 JA |
4006 | |
4007 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4008 | req->fadvise.len = READ_ONCE(sqe->len); | |
4009 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4010 | return 0; | |
4011 | } | |
4012 | ||
014db007 | 4013 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
4014 | { |
4015 | struct io_fadvise *fa = &req->fadvise; | |
4016 | int ret; | |
4017 | ||
3e69426d JA |
4018 | if (force_nonblock) { |
4019 | switch (fa->advice) { | |
4020 | case POSIX_FADV_NORMAL: | |
4021 | case POSIX_FADV_RANDOM: | |
4022 | case POSIX_FADV_SEQUENTIAL: | |
4023 | break; | |
4024 | default: | |
4025 | return -EAGAIN; | |
4026 | } | |
4027 | } | |
4840e418 JA |
4028 | |
4029 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4030 | if (ret < 0) | |
4031 | req_set_fail_links(req); | |
e1e16097 | 4032 | io_req_complete(req, ret); |
4840e418 JA |
4033 | return 0; |
4034 | } | |
4035 | ||
eddc7ef5 JA |
4036 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4037 | { | |
6ca56f84 | 4038 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4039 | return -EINVAL; |
eddc7ef5 JA |
4040 | if (sqe->ioprio || sqe->buf_index) |
4041 | return -EINVAL; | |
9c280f90 | 4042 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4043 | return -EBADF; |
eddc7ef5 | 4044 | |
1d9e1288 BM |
4045 | req->statx.dfd = READ_ONCE(sqe->fd); |
4046 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4047 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4048 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4049 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4050 | |
4051 | return 0; | |
4052 | } | |
4053 | ||
014db007 | 4054 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 4055 | { |
1d9e1288 | 4056 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4057 | int ret; |
4058 | ||
5b0bbee4 JA |
4059 | if (force_nonblock) { |
4060 | /* only need file table for an actual valid fd */ | |
4061 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4062 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4063 | return -EAGAIN; |
5b0bbee4 | 4064 | } |
eddc7ef5 | 4065 | |
e62753e4 BM |
4066 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4067 | ctx->buffer); | |
eddc7ef5 | 4068 | |
eddc7ef5 JA |
4069 | if (ret < 0) |
4070 | req_set_fail_links(req); | |
e1e16097 | 4071 | io_req_complete(req, ret); |
eddc7ef5 JA |
4072 | return 0; |
4073 | } | |
4074 | ||
b5dba59e JA |
4075 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4076 | { | |
4077 | /* | |
4078 | * If we queue this for async, it must not be cancellable. That would | |
7cdaf587 XW |
4079 | * leave the 'file' in an undeterminate state, and here need to modify |
4080 | * io_wq_work.flags, so initialize io_wq_work firstly. | |
b5dba59e | 4081 | */ |
7cdaf587 | 4082 | io_req_init_async(req); |
b5dba59e JA |
4083 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
4084 | ||
3232dd02 PB |
4085 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4086 | return -EINVAL; | |
b5dba59e JA |
4087 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4088 | sqe->rw_flags || sqe->buf_index) | |
4089 | return -EINVAL; | |
9c280f90 | 4090 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4091 | return -EBADF; |
b5dba59e JA |
4092 | |
4093 | req->close.fd = READ_ONCE(sqe->fd); | |
0f212204 | 4094 | if ((req->file && req->file->f_op == &io_uring_fops)) |
fd2206e4 | 4095 | return -EBADF; |
b5dba59e | 4096 | |
3af73b28 | 4097 | req->close.put_file = NULL; |
b5dba59e | 4098 | return 0; |
b5dba59e JA |
4099 | } |
4100 | ||
229a7b63 JA |
4101 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
4102 | struct io_comp_state *cs) | |
b5dba59e | 4103 | { |
3af73b28 | 4104 | struct io_close *close = &req->close; |
b5dba59e JA |
4105 | int ret; |
4106 | ||
3af73b28 PB |
4107 | /* might be already done during nonblock submission */ |
4108 | if (!close->put_file) { | |
4109 | ret = __close_fd_get_file(close->fd, &close->put_file); | |
4110 | if (ret < 0) | |
4111 | return (ret == -ENOENT) ? -EBADF : ret; | |
4112 | } | |
b5dba59e JA |
4113 | |
4114 | /* if the file has a flush method, be safe and punt to async */ | |
3af73b28 | 4115 | if (close->put_file->f_op->flush && force_nonblock) { |
24c74678 PB |
4116 | /* was never set, but play safe */ |
4117 | req->flags &= ~REQ_F_NOWAIT; | |
0bf0eefd | 4118 | /* avoid grabbing files - we don't need the files */ |
24c74678 | 4119 | req->flags |= REQ_F_NO_FILE_TABLE; |
0bf0eefd | 4120 | return -EAGAIN; |
a2100672 | 4121 | } |
b5dba59e | 4122 | |
3af73b28 PB |
4123 | /* No ->flush() or already async, safely close from here */ |
4124 | ret = filp_close(close->put_file, req->work.files); | |
4125 | if (ret < 0) | |
4126 | req_set_fail_links(req); | |
3af73b28 PB |
4127 | fput(close->put_file); |
4128 | close->put_file = NULL; | |
229a7b63 | 4129 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 4130 | return 0; |
b5dba59e JA |
4131 | } |
4132 | ||
3529d8c2 | 4133 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4134 | { |
4135 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4136 | |
4137 | if (!req->file) | |
4138 | return -EBADF; | |
5d17b4a4 JA |
4139 | |
4140 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4141 | return -EINVAL; | |
4142 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4143 | return -EINVAL; | |
4144 | ||
8ed8d3c3 JA |
4145 | req->sync.off = READ_ONCE(sqe->off); |
4146 | req->sync.len = READ_ONCE(sqe->len); | |
4147 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4148 | return 0; |
4149 | } | |
4150 | ||
ac45abc0 | 4151 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4152 | { |
8ed8d3c3 JA |
4153 | int ret; |
4154 | ||
ac45abc0 PB |
4155 | /* sync_file_range always requires a blocking context */ |
4156 | if (force_nonblock) | |
4157 | return -EAGAIN; | |
4158 | ||
9adbd45d | 4159 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4160 | req->sync.flags); |
4161 | if (ret < 0) | |
4162 | req_set_fail_links(req); | |
e1e16097 | 4163 | io_req_complete(req, ret); |
5d17b4a4 JA |
4164 | return 0; |
4165 | } | |
4166 | ||
469956e8 | 4167 | #if defined(CONFIG_NET) |
02d27d89 PB |
4168 | static int io_setup_async_msg(struct io_kiocb *req, |
4169 | struct io_async_msghdr *kmsg) | |
4170 | { | |
e8c2bc1f JA |
4171 | struct io_async_msghdr *async_msg = req->async_data; |
4172 | ||
4173 | if (async_msg) | |
02d27d89 | 4174 | return -EAGAIN; |
e8c2bc1f | 4175 | if (io_alloc_async_data(req)) { |
02d27d89 PB |
4176 | if (kmsg->iov != kmsg->fast_iov) |
4177 | kfree(kmsg->iov); | |
4178 | return -ENOMEM; | |
4179 | } | |
e8c2bc1f | 4180 | async_msg = req->async_data; |
02d27d89 | 4181 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4182 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
02d27d89 PB |
4183 | return -EAGAIN; |
4184 | } | |
4185 | ||
2ae523ed PB |
4186 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4187 | struct io_async_msghdr *iomsg) | |
4188 | { | |
4189 | iomsg->iov = iomsg->fast_iov; | |
4190 | iomsg->msg.msg_name = &iomsg->addr; | |
4191 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4192 | req->sr_msg.msg_flags, &iomsg->iov); | |
4193 | } | |
4194 | ||
3529d8c2 | 4195 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4196 | { |
e8c2bc1f | 4197 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4198 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4199 | int ret; |
03b1230c | 4200 | |
d2b6f48b PB |
4201 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4202 | return -EINVAL; | |
4203 | ||
e47293fd | 4204 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4205 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4206 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4207 | |
d8768362 JA |
4208 | #ifdef CONFIG_COMPAT |
4209 | if (req->ctx->compat) | |
4210 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4211 | #endif | |
4212 | ||
e8c2bc1f | 4213 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4214 | return 0; |
e8c2bc1f | 4215 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4216 | if (!ret) |
4217 | req->flags |= REQ_F_NEED_CLEANUP; | |
4218 | return ret; | |
03b1230c JA |
4219 | } |
4220 | ||
229a7b63 JA |
4221 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4222 | struct io_comp_state *cs) | |
aa1fa28f | 4223 | { |
6b754c8b | 4224 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4225 | struct socket *sock; |
7a7cacba | 4226 | unsigned flags; |
0fa03c62 JA |
4227 | int ret; |
4228 | ||
0fa03c62 | 4229 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4230 | if (unlikely(!sock)) |
4231 | return ret; | |
3529d8c2 | 4232 | |
e8c2bc1f JA |
4233 | if (req->async_data) { |
4234 | kmsg = req->async_data; | |
4235 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4236 | /* if iov is set, it's allocated already */ |
4237 | if (!kmsg->iov) | |
4238 | kmsg->iov = kmsg->fast_iov; | |
4239 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4240 | } else { | |
4241 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4242 | if (ret) | |
4243 | return ret; | |
4244 | kmsg = &iomsg; | |
0fa03c62 | 4245 | } |
0fa03c62 | 4246 | |
7a7cacba PB |
4247 | flags = req->sr_msg.msg_flags; |
4248 | if (flags & MSG_DONTWAIT) | |
4249 | req->flags |= REQ_F_NOWAIT; | |
4250 | else if (force_nonblock) | |
4251 | flags |= MSG_DONTWAIT; | |
e47293fd | 4252 | |
7a7cacba PB |
4253 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4254 | if (force_nonblock && ret == -EAGAIN) | |
4255 | return io_setup_async_msg(req, kmsg); | |
4256 | if (ret == -ERESTARTSYS) | |
4257 | ret = -EINTR; | |
0fa03c62 | 4258 | |
6b754c8b | 4259 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4260 | kfree(kmsg->iov); |
99bc4c38 | 4261 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4262 | if (ret < 0) |
4263 | req_set_fail_links(req); | |
229a7b63 | 4264 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4265 | return 0; |
03b1230c | 4266 | } |
aa1fa28f | 4267 | |
229a7b63 JA |
4268 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4269 | struct io_comp_state *cs) | |
fddaface | 4270 | { |
7a7cacba PB |
4271 | struct io_sr_msg *sr = &req->sr_msg; |
4272 | struct msghdr msg; | |
4273 | struct iovec iov; | |
fddaface | 4274 | struct socket *sock; |
7a7cacba | 4275 | unsigned flags; |
fddaface JA |
4276 | int ret; |
4277 | ||
fddaface | 4278 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4279 | if (unlikely(!sock)) |
4280 | return ret; | |
fddaface | 4281 | |
7a7cacba PB |
4282 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4283 | if (unlikely(ret)) | |
14db8411 | 4284 | return ret; |
fddaface | 4285 | |
7a7cacba PB |
4286 | msg.msg_name = NULL; |
4287 | msg.msg_control = NULL; | |
4288 | msg.msg_controllen = 0; | |
4289 | msg.msg_namelen = 0; | |
fddaface | 4290 | |
7a7cacba PB |
4291 | flags = req->sr_msg.msg_flags; |
4292 | if (flags & MSG_DONTWAIT) | |
4293 | req->flags |= REQ_F_NOWAIT; | |
4294 | else if (force_nonblock) | |
4295 | flags |= MSG_DONTWAIT; | |
fddaface | 4296 | |
7a7cacba PB |
4297 | msg.msg_flags = flags; |
4298 | ret = sock_sendmsg(sock, &msg); | |
4299 | if (force_nonblock && ret == -EAGAIN) | |
4300 | return -EAGAIN; | |
4301 | if (ret == -ERESTARTSYS) | |
4302 | ret = -EINTR; | |
fddaface | 4303 | |
fddaface JA |
4304 | if (ret < 0) |
4305 | req_set_fail_links(req); | |
229a7b63 | 4306 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4307 | return 0; |
fddaface JA |
4308 | } |
4309 | ||
1400e697 PB |
4310 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4311 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4312 | { |
4313 | struct io_sr_msg *sr = &req->sr_msg; | |
4314 | struct iovec __user *uiov; | |
4315 | size_t iov_len; | |
4316 | int ret; | |
4317 | ||
1400e697 PB |
4318 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4319 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4320 | if (ret) |
4321 | return ret; | |
4322 | ||
4323 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4324 | if (iov_len > 1) | |
4325 | return -EINVAL; | |
1400e697 | 4326 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4327 | return -EFAULT; |
1400e697 PB |
4328 | sr->len = iomsg->iov[0].iov_len; |
4329 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4330 | sr->len); |
1400e697 | 4331 | iomsg->iov = NULL; |
52de1fe1 JA |
4332 | } else { |
4333 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, | |
1400e697 | 4334 | &iomsg->iov, &iomsg->msg.msg_iter); |
52de1fe1 JA |
4335 | if (ret > 0) |
4336 | ret = 0; | |
4337 | } | |
4338 | ||
4339 | return ret; | |
4340 | } | |
4341 | ||
4342 | #ifdef CONFIG_COMPAT | |
4343 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4344 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4345 | { |
4346 | struct compat_msghdr __user *msg_compat; | |
4347 | struct io_sr_msg *sr = &req->sr_msg; | |
4348 | struct compat_iovec __user *uiov; | |
4349 | compat_uptr_t ptr; | |
4350 | compat_size_t len; | |
4351 | int ret; | |
4352 | ||
270a5940 | 4353 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4354 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4355 | &ptr, &len); |
4356 | if (ret) | |
4357 | return ret; | |
4358 | ||
4359 | uiov = compat_ptr(ptr); | |
4360 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4361 | compat_ssize_t clen; | |
4362 | ||
4363 | if (len > 1) | |
4364 | return -EINVAL; | |
4365 | if (!access_ok(uiov, sizeof(*uiov))) | |
4366 | return -EFAULT; | |
4367 | if (__get_user(clen, &uiov->iov_len)) | |
4368 | return -EFAULT; | |
4369 | if (clen < 0) | |
4370 | return -EINVAL; | |
1400e697 PB |
4371 | sr->len = iomsg->iov[0].iov_len; |
4372 | iomsg->iov = NULL; | |
52de1fe1 JA |
4373 | } else { |
4374 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, | |
1400e697 PB |
4375 | &iomsg->iov, |
4376 | &iomsg->msg.msg_iter); | |
52de1fe1 JA |
4377 | if (ret < 0) |
4378 | return ret; | |
4379 | } | |
4380 | ||
4381 | return 0; | |
4382 | } | |
4383 | #endif | |
4384 | ||
1400e697 PB |
4385 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4386 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4387 | { |
1400e697 PB |
4388 | iomsg->msg.msg_name = &iomsg->addr; |
4389 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4390 | |
4391 | #ifdef CONFIG_COMPAT | |
4392 | if (req->ctx->compat) | |
1400e697 | 4393 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4394 | #endif |
52de1fe1 | 4395 | |
1400e697 | 4396 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4397 | } |
4398 | ||
bcda7baa | 4399 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4400 | bool needs_lock) |
bcda7baa JA |
4401 | { |
4402 | struct io_sr_msg *sr = &req->sr_msg; | |
4403 | struct io_buffer *kbuf; | |
4404 | ||
bcda7baa JA |
4405 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4406 | if (IS_ERR(kbuf)) | |
4407 | return kbuf; | |
4408 | ||
4409 | sr->kbuf = kbuf; | |
4410 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4411 | return kbuf; |
fddaface JA |
4412 | } |
4413 | ||
7fbb1b54 PB |
4414 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4415 | { | |
4416 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4417 | } | |
4418 | ||
3529d8c2 JA |
4419 | static int io_recvmsg_prep(struct io_kiocb *req, |
4420 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4421 | { |
e8c2bc1f | 4422 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4423 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4424 | int ret; |
3529d8c2 | 4425 | |
d2b6f48b PB |
4426 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4427 | return -EINVAL; | |
4428 | ||
3529d8c2 | 4429 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4430 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4431 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4432 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4433 | |
d8768362 JA |
4434 | #ifdef CONFIG_COMPAT |
4435 | if (req->ctx->compat) | |
4436 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4437 | #endif | |
4438 | ||
e8c2bc1f | 4439 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
06b76d44 | 4440 | return 0; |
e8c2bc1f | 4441 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4442 | if (!ret) |
4443 | req->flags |= REQ_F_NEED_CLEANUP; | |
4444 | return ret; | |
aa1fa28f JA |
4445 | } |
4446 | ||
229a7b63 JA |
4447 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4448 | struct io_comp_state *cs) | |
aa1fa28f | 4449 | { |
6b754c8b | 4450 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4451 | struct socket *sock; |
7fbb1b54 | 4452 | struct io_buffer *kbuf; |
7a7cacba | 4453 | unsigned flags; |
52de1fe1 | 4454 | int ret, cflags = 0; |
03b1230c | 4455 | |
03b1230c | 4456 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4457 | if (unlikely(!sock)) |
4458 | return ret; | |
3529d8c2 | 4459 | |
e8c2bc1f JA |
4460 | if (req->async_data) { |
4461 | kmsg = req->async_data; | |
4462 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4463 | /* if iov is set, it's allocated already */ |
4464 | if (!kmsg->iov) | |
4465 | kmsg->iov = kmsg->fast_iov; | |
4466 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4467 | } else { | |
4468 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4469 | if (ret) | |
681fda8d | 4470 | return ret; |
7a7cacba PB |
4471 | kmsg = &iomsg; |
4472 | } | |
03b1230c | 4473 | |
bc02ef33 | 4474 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4475 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4476 | if (IS_ERR(kbuf)) |
52de1fe1 | 4477 | return PTR_ERR(kbuf); |
7a7cacba PB |
4478 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4479 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4480 | 1, req->sr_msg.len); | |
4481 | } | |
52de1fe1 | 4482 | |
7a7cacba PB |
4483 | flags = req->sr_msg.msg_flags; |
4484 | if (flags & MSG_DONTWAIT) | |
4485 | req->flags |= REQ_F_NOWAIT; | |
4486 | else if (force_nonblock) | |
4487 | flags |= MSG_DONTWAIT; | |
e47293fd | 4488 | |
7a7cacba PB |
4489 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4490 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4491 | if (force_nonblock && ret == -EAGAIN) |
4492 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4493 | if (ret == -ERESTARTSYS) |
4494 | ret = -EINTR; | |
03b1230c | 4495 | |
7fbb1b54 PB |
4496 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4497 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4498 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4499 | kfree(kmsg->iov); |
99bc4c38 | 4500 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4501 | if (ret < 0) |
4502 | req_set_fail_links(req); | |
229a7b63 | 4503 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4504 | return 0; |
0fa03c62 | 4505 | } |
5d17b4a4 | 4506 | |
229a7b63 JA |
4507 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4508 | struct io_comp_state *cs) | |
fddaface | 4509 | { |
6b754c8b | 4510 | struct io_buffer *kbuf; |
7a7cacba PB |
4511 | struct io_sr_msg *sr = &req->sr_msg; |
4512 | struct msghdr msg; | |
4513 | void __user *buf = sr->buf; | |
fddaface | 4514 | struct socket *sock; |
7a7cacba PB |
4515 | struct iovec iov; |
4516 | unsigned flags; | |
bcda7baa | 4517 | int ret, cflags = 0; |
fddaface | 4518 | |
fddaface | 4519 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4520 | if (unlikely(!sock)) |
4521 | return ret; | |
fddaface | 4522 | |
bc02ef33 | 4523 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4524 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4525 | if (IS_ERR(kbuf)) |
4526 | return PTR_ERR(kbuf); | |
7a7cacba | 4527 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4528 | } |
bcda7baa | 4529 | |
7a7cacba | 4530 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4531 | if (unlikely(ret)) |
4532 | goto out_free; | |
fddaface | 4533 | |
7a7cacba PB |
4534 | msg.msg_name = NULL; |
4535 | msg.msg_control = NULL; | |
4536 | msg.msg_controllen = 0; | |
4537 | msg.msg_namelen = 0; | |
4538 | msg.msg_iocb = NULL; | |
4539 | msg.msg_flags = 0; | |
fddaface | 4540 | |
7a7cacba PB |
4541 | flags = req->sr_msg.msg_flags; |
4542 | if (flags & MSG_DONTWAIT) | |
4543 | req->flags |= REQ_F_NOWAIT; | |
4544 | else if (force_nonblock) | |
4545 | flags |= MSG_DONTWAIT; | |
4546 | ||
4547 | ret = sock_recvmsg(sock, &msg, flags); | |
4548 | if (force_nonblock && ret == -EAGAIN) | |
4549 | return -EAGAIN; | |
4550 | if (ret == -ERESTARTSYS) | |
4551 | ret = -EINTR; | |
14c32eee | 4552 | out_free: |
7fbb1b54 PB |
4553 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4554 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4555 | if (ret < 0) |
4556 | req_set_fail_links(req); | |
229a7b63 | 4557 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4558 | return 0; |
fddaface JA |
4559 | } |
4560 | ||
3529d8c2 | 4561 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4562 | { |
8ed8d3c3 JA |
4563 | struct io_accept *accept = &req->accept; |
4564 | ||
17f2fe35 JA |
4565 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4566 | return -EINVAL; | |
8042d6ce | 4567 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4568 | return -EINVAL; |
4569 | ||
d55e5f5b JA |
4570 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4571 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4572 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4573 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4574 | return 0; |
8ed8d3c3 | 4575 | } |
17f2fe35 | 4576 | |
229a7b63 JA |
4577 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4578 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
4579 | { |
4580 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 4581 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4582 | int ret; |
4583 | ||
e697deed JX |
4584 | if (req->file->f_flags & O_NONBLOCK) |
4585 | req->flags |= REQ_F_NOWAIT; | |
4586 | ||
8ed8d3c3 | 4587 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4588 | accept->addr_len, accept->flags, |
4589 | accept->nofile); | |
8ed8d3c3 | 4590 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4591 | return -EAGAIN; |
ac45abc0 PB |
4592 | if (ret < 0) { |
4593 | if (ret == -ERESTARTSYS) | |
4594 | ret = -EINTR; | |
4e88d6e7 | 4595 | req_set_fail_links(req); |
ac45abc0 | 4596 | } |
229a7b63 | 4597 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 4598 | return 0; |
8ed8d3c3 JA |
4599 | } |
4600 | ||
3529d8c2 | 4601 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4602 | { |
3529d8c2 | 4603 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 4604 | struct io_async_connect *io = req->async_data; |
f499a021 | 4605 | |
3fbb51c1 JA |
4606 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
4607 | return -EINVAL; | |
4608 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4609 | return -EINVAL; | |
4610 | ||
3529d8c2 JA |
4611 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4612 | conn->addr_len = READ_ONCE(sqe->addr2); | |
4613 | ||
4614 | if (!io) | |
4615 | return 0; | |
4616 | ||
4617 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 4618 | &io->address); |
f499a021 JA |
4619 | } |
4620 | ||
229a7b63 JA |
4621 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4622 | struct io_comp_state *cs) | |
f8e85cf2 | 4623 | { |
e8c2bc1f | 4624 | struct io_async_connect __io, *io; |
f8e85cf2 | 4625 | unsigned file_flags; |
3fbb51c1 | 4626 | int ret; |
f8e85cf2 | 4627 | |
e8c2bc1f JA |
4628 | if (req->async_data) { |
4629 | io = req->async_data; | |
f499a021 | 4630 | } else { |
3529d8c2 JA |
4631 | ret = move_addr_to_kernel(req->connect.addr, |
4632 | req->connect.addr_len, | |
e8c2bc1f | 4633 | &__io.address); |
f499a021 JA |
4634 | if (ret) |
4635 | goto out; | |
4636 | io = &__io; | |
4637 | } | |
4638 | ||
3fbb51c1 JA |
4639 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4640 | ||
e8c2bc1f | 4641 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4642 | req->connect.addr_len, file_flags); |
87f80d62 | 4643 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4644 | if (req->async_data) |
b7bb4f7d | 4645 | return -EAGAIN; |
e8c2bc1f | 4646 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4647 | ret = -ENOMEM; |
4648 | goto out; | |
4649 | } | |
e8c2bc1f JA |
4650 | io = req->async_data; |
4651 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4652 | return -EAGAIN; |
f499a021 | 4653 | } |
f8e85cf2 JA |
4654 | if (ret == -ERESTARTSYS) |
4655 | ret = -EINTR; | |
f499a021 | 4656 | out: |
4e88d6e7 JA |
4657 | if (ret < 0) |
4658 | req_set_fail_links(req); | |
229a7b63 | 4659 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 4660 | return 0; |
469956e8 Y |
4661 | } |
4662 | #else /* !CONFIG_NET */ | |
4663 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4664 | { | |
f8e85cf2 | 4665 | return -EOPNOTSUPP; |
f8e85cf2 JA |
4666 | } |
4667 | ||
1e16c2f9 RD |
4668 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4669 | struct io_comp_state *cs) | |
469956e8 Y |
4670 | { |
4671 | return -EOPNOTSUPP; | |
4672 | } | |
4673 | ||
1e16c2f9 RD |
4674 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4675 | struct io_comp_state *cs) | |
469956e8 Y |
4676 | { |
4677 | return -EOPNOTSUPP; | |
4678 | } | |
4679 | ||
4680 | static int io_recvmsg_prep(struct io_kiocb *req, | |
4681 | const struct io_uring_sqe *sqe) | |
4682 | { | |
4683 | return -EOPNOTSUPP; | |
4684 | } | |
4685 | ||
1e16c2f9 RD |
4686 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4687 | struct io_comp_state *cs) | |
469956e8 Y |
4688 | { |
4689 | return -EOPNOTSUPP; | |
4690 | } | |
4691 | ||
1e16c2f9 RD |
4692 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4693 | struct io_comp_state *cs) | |
469956e8 Y |
4694 | { |
4695 | return -EOPNOTSUPP; | |
4696 | } | |
4697 | ||
4698 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4699 | { | |
4700 | return -EOPNOTSUPP; | |
4701 | } | |
4702 | ||
1e16c2f9 RD |
4703 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4704 | struct io_comp_state *cs) | |
469956e8 Y |
4705 | { |
4706 | return -EOPNOTSUPP; | |
4707 | } | |
ce593a6c | 4708 | |
469956e8 Y |
4709 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4710 | { | |
4711 | return -EOPNOTSUPP; | |
4712 | } | |
4713 | ||
1e16c2f9 RD |
4714 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4715 | struct io_comp_state *cs) | |
469956e8 | 4716 | { |
f8e85cf2 | 4717 | return -EOPNOTSUPP; |
ce593a6c | 4718 | } |
469956e8 | 4719 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4720 | |
d7718a9d JA |
4721 | struct io_poll_table { |
4722 | struct poll_table_struct pt; | |
4723 | struct io_kiocb *req; | |
4724 | int error; | |
4725 | }; | |
ce593a6c | 4726 | |
d7718a9d JA |
4727 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4728 | __poll_t mask, task_work_func_t func) | |
4729 | { | |
fd7d6de2 | 4730 | bool twa_signal_ok; |
aa96bf8a | 4731 | int ret; |
d7718a9d JA |
4732 | |
4733 | /* for instances that support it check for an event match first: */ | |
4734 | if (mask && !(mask & poll->events)) | |
4735 | return 0; | |
4736 | ||
4737 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4738 | ||
4739 | list_del_init(&poll->wait.entry); | |
4740 | ||
d7718a9d JA |
4741 | req->result = mask; |
4742 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
4743 | percpu_ref_get(&req->ctx->refs); |
4744 | ||
fd7d6de2 JA |
4745 | /* |
4746 | * If we using the signalfd wait_queue_head for this wakeup, then | |
4747 | * it's not safe to use TWA_SIGNAL as we could be recursing on the | |
4748 | * tsk->sighand->siglock on doing the wakeup. Should not be needed | |
4749 | * either, as the normal wakeup will suffice. | |
4750 | */ | |
4751 | twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh); | |
4752 | ||
d7718a9d | 4753 | /* |
e3aabf95 JA |
4754 | * If this fails, then the task is exiting. When a task exits, the |
4755 | * work gets canceled, so just cancel this request as well instead | |
4756 | * of executing it. We can't safely execute it anyway, as we may not | |
4757 | * have the needed state needed for it anyway. | |
d7718a9d | 4758 | */ |
fd7d6de2 | 4759 | ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok); |
aa96bf8a | 4760 | if (unlikely(ret)) { |
c2c4c83c JA |
4761 | struct task_struct *tsk; |
4762 | ||
e3aabf95 | 4763 | WRITE_ONCE(poll->canceled, true); |
aa96bf8a | 4764 | tsk = io_wq_get_task(req->ctx->io_wq); |
ce593a6c JA |
4765 | task_work_add(tsk, &req->task_work, 0); |
4766 | wake_up_process(tsk); | |
aa96bf8a | 4767 | } |
d7718a9d JA |
4768 | return 1; |
4769 | } | |
4770 | ||
74ce6ce4 JA |
4771 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4772 | __acquires(&req->ctx->completion_lock) | |
4773 | { | |
4774 | struct io_ring_ctx *ctx = req->ctx; | |
4775 | ||
4776 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4777 | struct poll_table_struct pt = { ._key = poll->events }; | |
4778 | ||
4779 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4780 | } | |
4781 | ||
4782 | spin_lock_irq(&ctx->completion_lock); | |
4783 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4784 | add_wait_queue(poll->head, &poll->wait); | |
4785 | return true; | |
4786 | } | |
4787 | ||
4788 | return false; | |
4789 | } | |
4790 | ||
d4e7cd36 | 4791 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4792 | { |
e8c2bc1f | 4793 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4794 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4795 | return req->async_data; |
d4e7cd36 JA |
4796 | return req->apoll->double_poll; |
4797 | } | |
4798 | ||
4799 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4800 | { | |
4801 | if (req->opcode == IORING_OP_POLL_ADD) | |
4802 | return &req->poll; | |
4803 | return &req->apoll->poll; | |
4804 | } | |
4805 | ||
4806 | static void io_poll_remove_double(struct io_kiocb *req) | |
4807 | { | |
4808 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4809 | |
4810 | lockdep_assert_held(&req->ctx->completion_lock); | |
4811 | ||
4812 | if (poll && poll->head) { | |
4813 | struct wait_queue_head *head = poll->head; | |
4814 | ||
4815 | spin_lock(&head->lock); | |
4816 | list_del_init(&poll->wait.entry); | |
4817 | if (poll->wait.private) | |
4818 | refcount_dec(&req->refs); | |
4819 | poll->head = NULL; | |
4820 | spin_unlock(&head->lock); | |
4821 | } | |
4822 | } | |
4823 | ||
4824 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4825 | { | |
4826 | struct io_ring_ctx *ctx = req->ctx; | |
4827 | ||
d4e7cd36 | 4828 | io_poll_remove_double(req); |
18bceab1 JA |
4829 | req->poll.done = true; |
4830 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4831 | io_commit_cqring(ctx); | |
4832 | } | |
4833 | ||
4834 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) | |
4835 | { | |
4836 | struct io_ring_ctx *ctx = req->ctx; | |
4837 | ||
4838 | if (io_poll_rewait(req, &req->poll)) { | |
4839 | spin_unlock_irq(&ctx->completion_lock); | |
4840 | return; | |
4841 | } | |
4842 | ||
4843 | hash_del(&req->hash_node); | |
4844 | io_poll_complete(req, req->result, 0); | |
4845 | req->flags |= REQ_F_COMP_LOCKED; | |
9b5f7bd9 | 4846 | *nxt = io_put_req_find_next(req); |
18bceab1 JA |
4847 | spin_unlock_irq(&ctx->completion_lock); |
4848 | ||
4849 | io_cqring_ev_posted(ctx); | |
4850 | } | |
4851 | ||
4852 | static void io_poll_task_func(struct callback_head *cb) | |
4853 | { | |
4854 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 4855 | struct io_ring_ctx *ctx = req->ctx; |
18bceab1 JA |
4856 | struct io_kiocb *nxt = NULL; |
4857 | ||
4858 | io_poll_task_handler(req, &nxt); | |
ea1164e5 PB |
4859 | if (nxt) |
4860 | __io_req_task_submit(nxt); | |
6d816e08 | 4861 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4862 | } |
4863 | ||
4864 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4865 | int sync, void *key) | |
4866 | { | |
4867 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4868 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4869 | __poll_t mask = key_to_poll(key); |
4870 | ||
4871 | /* for instances that support it check for an event match first: */ | |
4872 | if (mask && !(mask & poll->events)) | |
4873 | return 0; | |
4874 | ||
8706e04e JA |
4875 | list_del_init(&wait->entry); |
4876 | ||
807abcb0 | 4877 | if (poll && poll->head) { |
18bceab1 JA |
4878 | bool done; |
4879 | ||
807abcb0 JA |
4880 | spin_lock(&poll->head->lock); |
4881 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4882 | if (!done) |
807abcb0 | 4883 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4884 | /* make sure double remove sees this as being gone */ |
4885 | wait->private = NULL; | |
807abcb0 | 4886 | spin_unlock(&poll->head->lock); |
18bceab1 JA |
4887 | if (!done) |
4888 | __io_async_wake(req, poll, mask, io_poll_task_func); | |
4889 | } | |
4890 | refcount_dec(&req->refs); | |
4891 | return 1; | |
4892 | } | |
4893 | ||
4894 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4895 | wait_queue_func_t wake_func) | |
4896 | { | |
4897 | poll->head = NULL; | |
4898 | poll->done = false; | |
4899 | poll->canceled = false; | |
4900 | poll->events = events; | |
4901 | INIT_LIST_HEAD(&poll->wait.entry); | |
4902 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4903 | } | |
4904 | ||
4905 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4906 | struct wait_queue_head *head, |
4907 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4908 | { |
4909 | struct io_kiocb *req = pt->req; | |
4910 | ||
4911 | /* | |
4912 | * If poll->head is already set, it's because the file being polled | |
4913 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4914 | * for write). Setup a separate io_poll_iocb if this happens. | |
4915 | */ | |
4916 | if (unlikely(poll->head)) { | |
4917 | /* already have a 2nd entry, fail a third attempt */ | |
807abcb0 | 4918 | if (*poll_ptr) { |
18bceab1 JA |
4919 | pt->error = -EINVAL; |
4920 | return; | |
4921 | } | |
4922 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
4923 | if (!poll) { | |
4924 | pt->error = -ENOMEM; | |
4925 | return; | |
4926 | } | |
4927 | io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake); | |
4928 | refcount_inc(&req->refs); | |
4929 | poll->wait.private = req; | |
807abcb0 | 4930 | *poll_ptr = poll; |
18bceab1 JA |
4931 | } |
4932 | ||
4933 | pt->error = 0; | |
4934 | poll->head = head; | |
a31eb4a2 JX |
4935 | |
4936 | if (poll->events & EPOLLEXCLUSIVE) | |
4937 | add_wait_queue_exclusive(head, &poll->wait); | |
4938 | else | |
4939 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
4940 | } |
4941 | ||
4942 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
4943 | struct poll_table_struct *p) | |
4944 | { | |
4945 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 4946 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 4947 | |
807abcb0 | 4948 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
4949 | } |
4950 | ||
d7718a9d JA |
4951 | static void io_async_task_func(struct callback_head *cb) |
4952 | { | |
4953 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
4954 | struct async_poll *apoll = req->apoll; | |
4955 | struct io_ring_ctx *ctx = req->ctx; | |
4956 | ||
4957 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
4958 | ||
74ce6ce4 | 4959 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 4960 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 4961 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 4962 | return; |
d7718a9d JA |
4963 | } |
4964 | ||
31067255 | 4965 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 4966 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 4967 | hash_del(&req->hash_node); |
2bae047e | 4968 | |
d4e7cd36 | 4969 | io_poll_remove_double(req); |
74ce6ce4 JA |
4970 | spin_unlock_irq(&ctx->completion_lock); |
4971 | ||
0be0b0e3 PB |
4972 | if (!READ_ONCE(apoll->poll.canceled)) |
4973 | __io_req_task_submit(req); | |
4974 | else | |
4975 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 4976 | |
6d816e08 | 4977 | percpu_ref_put(&ctx->refs); |
807abcb0 | 4978 | kfree(apoll->double_poll); |
31067255 | 4979 | kfree(apoll); |
d7718a9d JA |
4980 | } |
4981 | ||
4982 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
4983 | void *key) | |
4984 | { | |
4985 | struct io_kiocb *req = wait->private; | |
4986 | struct io_poll_iocb *poll = &req->apoll->poll; | |
4987 | ||
4988 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
4989 | key_to_poll(key)); | |
4990 | ||
4991 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
4992 | } | |
4993 | ||
4994 | static void io_poll_req_insert(struct io_kiocb *req) | |
4995 | { | |
4996 | struct io_ring_ctx *ctx = req->ctx; | |
4997 | struct hlist_head *list; | |
4998 | ||
4999 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5000 | hlist_add_head(&req->hash_node, list); | |
5001 | } | |
5002 | ||
5003 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5004 | struct io_poll_iocb *poll, | |
5005 | struct io_poll_table *ipt, __poll_t mask, | |
5006 | wait_queue_func_t wake_func) | |
5007 | __acquires(&ctx->completion_lock) | |
5008 | { | |
5009 | struct io_ring_ctx *ctx = req->ctx; | |
5010 | bool cancel = false; | |
5011 | ||
18bceab1 | 5012 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5013 | poll->file = req->file; |
18bceab1 | 5014 | poll->wait.private = req; |
d7718a9d JA |
5015 | |
5016 | ipt->pt._key = mask; | |
5017 | ipt->req = req; | |
5018 | ipt->error = -EINVAL; | |
5019 | ||
d7718a9d JA |
5020 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5021 | ||
5022 | spin_lock_irq(&ctx->completion_lock); | |
5023 | if (likely(poll->head)) { | |
5024 | spin_lock(&poll->head->lock); | |
5025 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5026 | if (ipt->error) | |
5027 | cancel = true; | |
5028 | ipt->error = 0; | |
5029 | mask = 0; | |
5030 | } | |
5031 | if (mask || ipt->error) | |
5032 | list_del_init(&poll->wait.entry); | |
5033 | else if (cancel) | |
5034 | WRITE_ONCE(poll->canceled, true); | |
5035 | else if (!poll->done) /* actually waiting for an event */ | |
5036 | io_poll_req_insert(req); | |
5037 | spin_unlock(&poll->head->lock); | |
5038 | } | |
5039 | ||
5040 | return mask; | |
5041 | } | |
5042 | ||
5043 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5044 | { | |
5045 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5046 | struct io_ring_ctx *ctx = req->ctx; | |
5047 | struct async_poll *apoll; | |
5048 | struct io_poll_table ipt; | |
5049 | __poll_t mask, ret; | |
9dab14b8 | 5050 | int rw; |
d7718a9d JA |
5051 | |
5052 | if (!req->file || !file_can_poll(req->file)) | |
5053 | return false; | |
24c74678 | 5054 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5055 | return false; |
9dab14b8 JA |
5056 | if (def->pollin) |
5057 | rw = READ; | |
5058 | else if (def->pollout) | |
5059 | rw = WRITE; | |
5060 | else | |
5061 | return false; | |
5062 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5063 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5064 | return false; |
5065 | ||
5066 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5067 | if (unlikely(!apoll)) | |
5068 | return false; | |
807abcb0 | 5069 | apoll->double_poll = NULL; |
d7718a9d JA |
5070 | |
5071 | req->flags |= REQ_F_POLLED; | |
d7718a9d JA |
5072 | req->apoll = apoll; |
5073 | INIT_HLIST_NODE(&req->hash_node); | |
5074 | ||
8755d97a | 5075 | mask = 0; |
d7718a9d | 5076 | if (def->pollin) |
8755d97a | 5077 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5078 | if (def->pollout) |
5079 | mask |= POLLOUT | POLLWRNORM; | |
5080 | mask |= POLLERR | POLLPRI; | |
5081 | ||
5082 | ipt.pt._qproc = io_async_queue_proc; | |
5083 | ||
5084 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5085 | io_async_wake); | |
a36da65c | 5086 | if (ret || ipt.error) { |
d4e7cd36 | 5087 | io_poll_remove_double(req); |
d7718a9d | 5088 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5089 | kfree(apoll->double_poll); |
d7718a9d JA |
5090 | kfree(apoll); |
5091 | return false; | |
5092 | } | |
5093 | spin_unlock_irq(&ctx->completion_lock); | |
5094 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5095 | apoll->poll.events); | |
5096 | return true; | |
5097 | } | |
5098 | ||
5099 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5100 | struct io_poll_iocb *poll) | |
221c5eb2 | 5101 | { |
b41e9852 | 5102 | bool do_complete = false; |
221c5eb2 JA |
5103 | |
5104 | spin_lock(&poll->head->lock); | |
5105 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5106 | if (!list_empty(&poll->wait.entry)) { |
5107 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5108 | do_complete = true; |
221c5eb2 JA |
5109 | } |
5110 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5111 | hash_del(&req->hash_node); |
d7718a9d JA |
5112 | return do_complete; |
5113 | } | |
5114 | ||
5115 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5116 | { | |
5117 | bool do_complete; | |
5118 | ||
d4e7cd36 JA |
5119 | io_poll_remove_double(req); |
5120 | ||
d7718a9d JA |
5121 | if (req->opcode == IORING_OP_POLL_ADD) { |
5122 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5123 | } else { | |
3bfa5bcb JA |
5124 | struct async_poll *apoll = req->apoll; |
5125 | ||
d7718a9d | 5126 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5127 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5128 | if (do_complete) { | |
d7718a9d | 5129 | io_put_req(req); |
807abcb0 | 5130 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5131 | kfree(apoll); |
5132 | } | |
b1f573bd XW |
5133 | } |
5134 | ||
b41e9852 JA |
5135 | if (do_complete) { |
5136 | io_cqring_fill_event(req, -ECANCELED); | |
5137 | io_commit_cqring(req->ctx); | |
5138 | req->flags |= REQ_F_COMP_LOCKED; | |
f254ac04 | 5139 | req_set_fail_links(req); |
b41e9852 JA |
5140 | io_put_req(req); |
5141 | } | |
5142 | ||
5143 | return do_complete; | |
221c5eb2 JA |
5144 | } |
5145 | ||
76e1b642 JA |
5146 | /* |
5147 | * Returns true if we found and killed one or more poll requests | |
5148 | */ | |
5149 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk) | |
221c5eb2 | 5150 | { |
78076bb6 | 5151 | struct hlist_node *tmp; |
221c5eb2 | 5152 | struct io_kiocb *req; |
8e2e1faf | 5153 | int posted = 0, i; |
221c5eb2 JA |
5154 | |
5155 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5156 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5157 | struct hlist_head *list; | |
5158 | ||
5159 | list = &ctx->cancel_hash[i]; | |
f3606e3a JA |
5160 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
5161 | if (io_task_match(req, tsk)) | |
5162 | posted += io_poll_remove_one(req); | |
5163 | } | |
221c5eb2 JA |
5164 | } |
5165 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5166 | |
8e2e1faf JA |
5167 | if (posted) |
5168 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5169 | |
5170 | return posted != 0; | |
221c5eb2 JA |
5171 | } |
5172 | ||
47f46768 JA |
5173 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5174 | { | |
78076bb6 | 5175 | struct hlist_head *list; |
47f46768 JA |
5176 | struct io_kiocb *req; |
5177 | ||
78076bb6 JA |
5178 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5179 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5180 | if (sqe_addr != req->user_data) |
5181 | continue; | |
5182 | if (io_poll_remove_one(req)) | |
eac406c6 | 5183 | return 0; |
b41e9852 | 5184 | return -EALREADY; |
47f46768 JA |
5185 | } |
5186 | ||
5187 | return -ENOENT; | |
5188 | } | |
5189 | ||
3529d8c2 JA |
5190 | static int io_poll_remove_prep(struct io_kiocb *req, |
5191 | const struct io_uring_sqe *sqe) | |
0969e783 | 5192 | { |
0969e783 JA |
5193 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5194 | return -EINVAL; | |
5195 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5196 | sqe->poll_events) | |
5197 | return -EINVAL; | |
5198 | ||
5199 | req->poll.addr = READ_ONCE(sqe->addr); | |
0969e783 JA |
5200 | return 0; |
5201 | } | |
5202 | ||
221c5eb2 JA |
5203 | /* |
5204 | * Find a running poll command that matches one specified in sqe->addr, | |
5205 | * and remove it if found. | |
5206 | */ | |
fc4df999 | 5207 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5208 | { |
5209 | struct io_ring_ctx *ctx = req->ctx; | |
0969e783 | 5210 | u64 addr; |
47f46768 | 5211 | int ret; |
221c5eb2 | 5212 | |
0969e783 | 5213 | addr = req->poll.addr; |
221c5eb2 | 5214 | spin_lock_irq(&ctx->completion_lock); |
0969e783 | 5215 | ret = io_poll_cancel(ctx, addr); |
221c5eb2 JA |
5216 | spin_unlock_irq(&ctx->completion_lock); |
5217 | ||
4e88d6e7 JA |
5218 | if (ret < 0) |
5219 | req_set_fail_links(req); | |
e1e16097 | 5220 | io_req_complete(req, ret); |
221c5eb2 JA |
5221 | return 0; |
5222 | } | |
5223 | ||
221c5eb2 JA |
5224 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5225 | void *key) | |
5226 | { | |
c2f2eb7d JA |
5227 | struct io_kiocb *req = wait->private; |
5228 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5229 | |
d7718a9d | 5230 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5231 | } |
5232 | ||
221c5eb2 JA |
5233 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5234 | struct poll_table_struct *p) | |
5235 | { | |
5236 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5237 | ||
e8c2bc1f | 5238 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5239 | } |
5240 | ||
3529d8c2 | 5241 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5242 | { |
5243 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5244 | u32 events; |
221c5eb2 JA |
5245 | |
5246 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5247 | return -EINVAL; | |
5248 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5249 | return -EINVAL; | |
09bb8394 JA |
5250 | if (!poll->file) |
5251 | return -EBADF; | |
221c5eb2 | 5252 | |
5769a351 JX |
5253 | events = READ_ONCE(sqe->poll32_events); |
5254 | #ifdef __BIG_ENDIAN | |
5255 | events = swahw32(events); | |
5256 | #endif | |
a31eb4a2 JX |
5257 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5258 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5259 | return 0; |
5260 | } | |
5261 | ||
014db007 | 5262 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5263 | { |
5264 | struct io_poll_iocb *poll = &req->poll; | |
5265 | struct io_ring_ctx *ctx = req->ctx; | |
5266 | struct io_poll_table ipt; | |
0969e783 | 5267 | __poll_t mask; |
0969e783 | 5268 | |
78076bb6 | 5269 | INIT_HLIST_NODE(&req->hash_node); |
d7718a9d | 5270 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5271 | |
d7718a9d JA |
5272 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5273 | io_poll_wake); | |
221c5eb2 | 5274 | |
8c838788 | 5275 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5276 | ipt.error = 0; |
b0dd8a41 | 5277 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5278 | } |
221c5eb2 JA |
5279 | spin_unlock_irq(&ctx->completion_lock); |
5280 | ||
8c838788 JA |
5281 | if (mask) { |
5282 | io_cqring_ev_posted(ctx); | |
014db007 | 5283 | io_put_req(req); |
221c5eb2 | 5284 | } |
8c838788 | 5285 | return ipt.error; |
221c5eb2 JA |
5286 | } |
5287 | ||
5262f567 JA |
5288 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5289 | { | |
ad8a48ac JA |
5290 | struct io_timeout_data *data = container_of(timer, |
5291 | struct io_timeout_data, timer); | |
5292 | struct io_kiocb *req = data->req; | |
5293 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5294 | unsigned long flags; |
5295 | ||
5262f567 | 5296 | spin_lock_irqsave(&ctx->completion_lock, flags); |
01cec8c1 PB |
5297 | atomic_set(&req->ctx->cq_timeouts, |
5298 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5299 | ||
ef03681a | 5300 | /* |
11365043 JA |
5301 | * We could be racing with timeout deletion. If the list is empty, |
5302 | * then timeout lookup already found it and will be handling it. | |
ef03681a | 5303 | */ |
135fcde8 PB |
5304 | if (!list_empty(&req->timeout.list)) |
5305 | list_del_init(&req->timeout.list); | |
5262f567 | 5306 | |
78e19bbe | 5307 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5308 | io_commit_cqring(ctx); |
5309 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5310 | ||
5311 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5312 | req_set_fail_links(req); |
5262f567 JA |
5313 | io_put_req(req); |
5314 | return HRTIMER_NORESTART; | |
5315 | } | |
5316 | ||
f254ac04 JA |
5317 | static int __io_timeout_cancel(struct io_kiocb *req) |
5318 | { | |
e8c2bc1f | 5319 | struct io_timeout_data *io = req->async_data; |
f254ac04 JA |
5320 | int ret; |
5321 | ||
5322 | list_del_init(&req->timeout.list); | |
5323 | ||
e8c2bc1f | 5324 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 JA |
5325 | if (ret == -1) |
5326 | return -EALREADY; | |
5327 | ||
5328 | req_set_fail_links(req); | |
5329 | req->flags |= REQ_F_COMP_LOCKED; | |
5330 | io_cqring_fill_event(req, -ECANCELED); | |
5331 | io_put_req(req); | |
5332 | return 0; | |
5333 | } | |
5334 | ||
47f46768 JA |
5335 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5336 | { | |
5337 | struct io_kiocb *req; | |
5338 | int ret = -ENOENT; | |
5339 | ||
135fcde8 | 5340 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5341 | if (user_data == req->user_data) { |
47f46768 JA |
5342 | ret = 0; |
5343 | break; | |
5344 | } | |
5345 | } | |
5346 | ||
5347 | if (ret == -ENOENT) | |
5348 | return ret; | |
5349 | ||
f254ac04 | 5350 | return __io_timeout_cancel(req); |
47f46768 JA |
5351 | } |
5352 | ||
3529d8c2 JA |
5353 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5354 | const struct io_uring_sqe *sqe) | |
b29472ee | 5355 | { |
b29472ee JA |
5356 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5357 | return -EINVAL; | |
61710e43 DA |
5358 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5359 | return -EINVAL; | |
5360 | if (sqe->ioprio || sqe->buf_index || sqe->len) | |
b29472ee JA |
5361 | return -EINVAL; |
5362 | ||
5363 | req->timeout.addr = READ_ONCE(sqe->addr); | |
5364 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); | |
5365 | if (req->timeout.flags) | |
5366 | return -EINVAL; | |
5367 | ||
b29472ee JA |
5368 | return 0; |
5369 | } | |
5370 | ||
11365043 JA |
5371 | /* |
5372 | * Remove or update an existing timeout command | |
5373 | */ | |
fc4df999 | 5374 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 JA |
5375 | { |
5376 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5377 | int ret; |
11365043 | 5378 | |
11365043 | 5379 | spin_lock_irq(&ctx->completion_lock); |
b29472ee | 5380 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
11365043 | 5381 | |
47f46768 | 5382 | io_cqring_fill_event(req, ret); |
11365043 JA |
5383 | io_commit_cqring(ctx); |
5384 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5385 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5386 | if (ret < 0) |
5387 | req_set_fail_links(req); | |
ec9c02ad | 5388 | io_put_req(req); |
11365043 | 5389 | return 0; |
5262f567 JA |
5390 | } |
5391 | ||
3529d8c2 | 5392 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5393 | bool is_timeout_link) |
5262f567 | 5394 | { |
ad8a48ac | 5395 | struct io_timeout_data *data; |
a41525ab | 5396 | unsigned flags; |
56080b02 | 5397 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5398 | |
ad8a48ac | 5399 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5400 | return -EINVAL; |
ad8a48ac | 5401 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5402 | return -EINVAL; |
56080b02 | 5403 | if (off && is_timeout_link) |
2d28390a | 5404 | return -EINVAL; |
a41525ab JA |
5405 | flags = READ_ONCE(sqe->timeout_flags); |
5406 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5407 | return -EINVAL; |
bdf20073 | 5408 | |
bfe68a22 | 5409 | req->timeout.off = off; |
26a61679 | 5410 | |
e8c2bc1f | 5411 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5412 | return -ENOMEM; |
5413 | ||
e8c2bc1f | 5414 | data = req->async_data; |
ad8a48ac | 5415 | data->req = req; |
ad8a48ac JA |
5416 | |
5417 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5418 | return -EFAULT; |
5419 | ||
11365043 | 5420 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 5421 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 5422 | else |
ad8a48ac | 5423 | data->mode = HRTIMER_MODE_REL; |
11365043 | 5424 | |
ad8a48ac JA |
5425 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5426 | return 0; | |
5427 | } | |
5428 | ||
fc4df999 | 5429 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5430 | { |
ad8a48ac | 5431 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5432 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5433 | struct list_head *entry; |
bfe68a22 | 5434 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5435 | |
733f5c95 | 5436 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5437 | |
5262f567 JA |
5438 | /* |
5439 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5440 | * timeout event to be satisfied. If it isn't set, then this is |
5441 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5442 | */ |
8eb7e2d0 | 5443 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5444 | entry = ctx->timeout_list.prev; |
5445 | goto add; | |
5446 | } | |
5262f567 | 5447 | |
bfe68a22 PB |
5448 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5449 | req->timeout.target_seq = tail + off; | |
5262f567 JA |
5450 | |
5451 | /* | |
5452 | * Insertion sort, ensuring the first entry in the list is always | |
5453 | * the one we need first. | |
5454 | */ | |
5262f567 | 5455 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5456 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5457 | timeout.list); | |
5262f567 | 5458 | |
8eb7e2d0 | 5459 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5460 | continue; |
bfe68a22 PB |
5461 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5462 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5463 | break; |
5464 | } | |
93bd25bb | 5465 | add: |
135fcde8 | 5466 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5467 | data->timer.function = io_timeout_fn; |
5468 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5469 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5470 | return 0; |
5471 | } | |
5262f567 | 5472 | |
62755e35 JA |
5473 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5474 | { | |
5475 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5476 | ||
5477 | return req->user_data == (unsigned long) data; | |
5478 | } | |
5479 | ||
e977d6d3 | 5480 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5481 | { |
62755e35 | 5482 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5483 | int ret = 0; |
5484 | ||
4f26bda1 | 5485 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5486 | switch (cancel_ret) { |
5487 | case IO_WQ_CANCEL_OK: | |
5488 | ret = 0; | |
5489 | break; | |
5490 | case IO_WQ_CANCEL_RUNNING: | |
5491 | ret = -EALREADY; | |
5492 | break; | |
5493 | case IO_WQ_CANCEL_NOTFOUND: | |
5494 | ret = -ENOENT; | |
5495 | break; | |
5496 | } | |
5497 | ||
e977d6d3 JA |
5498 | return ret; |
5499 | } | |
5500 | ||
47f46768 JA |
5501 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5502 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5503 | int success_ret) |
47f46768 JA |
5504 | { |
5505 | unsigned long flags; | |
5506 | int ret; | |
5507 | ||
5508 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5509 | if (ret != -ENOENT) { | |
5510 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5511 | goto done; | |
5512 | } | |
5513 | ||
5514 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5515 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5516 | if (ret != -ENOENT) | |
5517 | goto done; | |
5518 | ret = io_poll_cancel(ctx, sqe_addr); | |
5519 | done: | |
b0dd8a41 JA |
5520 | if (!ret) |
5521 | ret = success_ret; | |
47f46768 JA |
5522 | io_cqring_fill_event(req, ret); |
5523 | io_commit_cqring(ctx); | |
5524 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5525 | io_cqring_ev_posted(ctx); | |
5526 | ||
4e88d6e7 JA |
5527 | if (ret < 0) |
5528 | req_set_fail_links(req); | |
014db007 | 5529 | io_put_req(req); |
47f46768 JA |
5530 | } |
5531 | ||
3529d8c2 JA |
5532 | static int io_async_cancel_prep(struct io_kiocb *req, |
5533 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5534 | { |
fbf23849 | 5535 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5536 | return -EINVAL; |
61710e43 DA |
5537 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5538 | return -EINVAL; | |
5539 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5540 | return -EINVAL; |
5541 | ||
fbf23849 JA |
5542 | req->cancel.addr = READ_ONCE(sqe->addr); |
5543 | return 0; | |
5544 | } | |
5545 | ||
014db007 | 5546 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
5547 | { |
5548 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5549 | |
014db007 | 5550 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5551 | return 0; |
5552 | } | |
5553 | ||
05f3fb3c JA |
5554 | static int io_files_update_prep(struct io_kiocb *req, |
5555 | const struct io_uring_sqe *sqe) | |
5556 | { | |
6ca56f84 JA |
5557 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5558 | return -EINVAL; | |
61710e43 DA |
5559 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5560 | return -EINVAL; | |
5561 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5562 | return -EINVAL; |
5563 | ||
5564 | req->files_update.offset = READ_ONCE(sqe->off); | |
5565 | req->files_update.nr_args = READ_ONCE(sqe->len); | |
5566 | if (!req->files_update.nr_args) | |
5567 | return -EINVAL; | |
5568 | req->files_update.arg = READ_ONCE(sqe->addr); | |
5569 | return 0; | |
5570 | } | |
5571 | ||
229a7b63 JA |
5572 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
5573 | struct io_comp_state *cs) | |
fbf23849 JA |
5574 | { |
5575 | struct io_ring_ctx *ctx = req->ctx; | |
05f3fb3c JA |
5576 | struct io_uring_files_update up; |
5577 | int ret; | |
fbf23849 | 5578 | |
f86cd20c | 5579 | if (force_nonblock) |
05f3fb3c | 5580 | return -EAGAIN; |
05f3fb3c JA |
5581 | |
5582 | up.offset = req->files_update.offset; | |
5583 | up.fds = req->files_update.arg; | |
5584 | ||
5585 | mutex_lock(&ctx->uring_lock); | |
5586 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); | |
5587 | mutex_unlock(&ctx->uring_lock); | |
5588 | ||
5589 | if (ret < 0) | |
5590 | req_set_fail_links(req); | |
229a7b63 | 5591 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
5592 | return 0; |
5593 | } | |
5594 | ||
3529d8c2 JA |
5595 | static int io_req_defer_prep(struct io_kiocb *req, |
5596 | const struct io_uring_sqe *sqe) | |
f67676d1 | 5597 | { |
e781573e | 5598 | ssize_t ret = 0; |
f67676d1 | 5599 | |
f1d96a8f PB |
5600 | if (!sqe) |
5601 | return 0; | |
5602 | ||
e8c2bc1f | 5603 | if (io_alloc_async_data(req)) |
327d6d96 | 5604 | return -EAGAIN; |
f56040b8 PB |
5605 | ret = io_prep_work_files(req); |
5606 | if (unlikely(ret)) | |
5607 | return ret; | |
cccf0ee8 | 5608 | |
202700e1 JA |
5609 | io_prep_async_work(req); |
5610 | ||
d625c6ee | 5611 | switch (req->opcode) { |
e781573e JA |
5612 | case IORING_OP_NOP: |
5613 | break; | |
f67676d1 JA |
5614 | case IORING_OP_READV: |
5615 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5616 | case IORING_OP_READ: |
3529d8c2 | 5617 | ret = io_read_prep(req, sqe, true); |
f67676d1 JA |
5618 | break; |
5619 | case IORING_OP_WRITEV: | |
5620 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5621 | case IORING_OP_WRITE: |
3529d8c2 | 5622 | ret = io_write_prep(req, sqe, true); |
f67676d1 | 5623 | break; |
0969e783 | 5624 | case IORING_OP_POLL_ADD: |
3529d8c2 | 5625 | ret = io_poll_add_prep(req, sqe); |
0969e783 JA |
5626 | break; |
5627 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 | 5628 | ret = io_poll_remove_prep(req, sqe); |
0969e783 | 5629 | break; |
8ed8d3c3 | 5630 | case IORING_OP_FSYNC: |
3529d8c2 | 5631 | ret = io_prep_fsync(req, sqe); |
8ed8d3c3 JA |
5632 | break; |
5633 | case IORING_OP_SYNC_FILE_RANGE: | |
3529d8c2 | 5634 | ret = io_prep_sfr(req, sqe); |
8ed8d3c3 | 5635 | break; |
03b1230c | 5636 | case IORING_OP_SENDMSG: |
fddaface | 5637 | case IORING_OP_SEND: |
3529d8c2 | 5638 | ret = io_sendmsg_prep(req, sqe); |
03b1230c JA |
5639 | break; |
5640 | case IORING_OP_RECVMSG: | |
fddaface | 5641 | case IORING_OP_RECV: |
3529d8c2 | 5642 | ret = io_recvmsg_prep(req, sqe); |
03b1230c | 5643 | break; |
f499a021 | 5644 | case IORING_OP_CONNECT: |
3529d8c2 | 5645 | ret = io_connect_prep(req, sqe); |
f499a021 | 5646 | break; |
2d28390a | 5647 | case IORING_OP_TIMEOUT: |
3529d8c2 | 5648 | ret = io_timeout_prep(req, sqe, false); |
b7bb4f7d | 5649 | break; |
b29472ee | 5650 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 | 5651 | ret = io_timeout_remove_prep(req, sqe); |
b29472ee | 5652 | break; |
fbf23849 | 5653 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 | 5654 | ret = io_async_cancel_prep(req, sqe); |
fbf23849 | 5655 | break; |
2d28390a | 5656 | case IORING_OP_LINK_TIMEOUT: |
3529d8c2 | 5657 | ret = io_timeout_prep(req, sqe, true); |
b7bb4f7d | 5658 | break; |
8ed8d3c3 | 5659 | case IORING_OP_ACCEPT: |
3529d8c2 | 5660 | ret = io_accept_prep(req, sqe); |
8ed8d3c3 | 5661 | break; |
d63d1b5e JA |
5662 | case IORING_OP_FALLOCATE: |
5663 | ret = io_fallocate_prep(req, sqe); | |
5664 | break; | |
15b71abe JA |
5665 | case IORING_OP_OPENAT: |
5666 | ret = io_openat_prep(req, sqe); | |
5667 | break; | |
b5dba59e JA |
5668 | case IORING_OP_CLOSE: |
5669 | ret = io_close_prep(req, sqe); | |
5670 | break; | |
05f3fb3c JA |
5671 | case IORING_OP_FILES_UPDATE: |
5672 | ret = io_files_update_prep(req, sqe); | |
5673 | break; | |
eddc7ef5 JA |
5674 | case IORING_OP_STATX: |
5675 | ret = io_statx_prep(req, sqe); | |
5676 | break; | |
4840e418 JA |
5677 | case IORING_OP_FADVISE: |
5678 | ret = io_fadvise_prep(req, sqe); | |
5679 | break; | |
c1ca757b JA |
5680 | case IORING_OP_MADVISE: |
5681 | ret = io_madvise_prep(req, sqe); | |
5682 | break; | |
cebdb986 JA |
5683 | case IORING_OP_OPENAT2: |
5684 | ret = io_openat2_prep(req, sqe); | |
5685 | break; | |
3e4827b0 JA |
5686 | case IORING_OP_EPOLL_CTL: |
5687 | ret = io_epoll_ctl_prep(req, sqe); | |
5688 | break; | |
7d67af2c PB |
5689 | case IORING_OP_SPLICE: |
5690 | ret = io_splice_prep(req, sqe); | |
5691 | break; | |
ddf0322d JA |
5692 | case IORING_OP_PROVIDE_BUFFERS: |
5693 | ret = io_provide_buffers_prep(req, sqe); | |
5694 | break; | |
067524e9 JA |
5695 | case IORING_OP_REMOVE_BUFFERS: |
5696 | ret = io_remove_buffers_prep(req, sqe); | |
5697 | break; | |
f2a8d5c7 PB |
5698 | case IORING_OP_TEE: |
5699 | ret = io_tee_prep(req, sqe); | |
5700 | break; | |
f67676d1 | 5701 | default: |
e781573e JA |
5702 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5703 | req->opcode); | |
5704 | ret = -EINVAL; | |
b7bb4f7d | 5705 | break; |
f67676d1 JA |
5706 | } |
5707 | ||
b7bb4f7d | 5708 | return ret; |
f67676d1 JA |
5709 | } |
5710 | ||
9cf7c104 PB |
5711 | static u32 io_get_sequence(struct io_kiocb *req) |
5712 | { | |
5713 | struct io_kiocb *pos; | |
5714 | struct io_ring_ctx *ctx = req->ctx; | |
5715 | u32 total_submitted, nr_reqs = 1; | |
5716 | ||
5717 | if (req->flags & REQ_F_LINK_HEAD) | |
5718 | list_for_each_entry(pos, &req->link_list, link_list) | |
5719 | nr_reqs++; | |
5720 | ||
5721 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5722 | return total_submitted - nr_reqs; | |
5723 | } | |
5724 | ||
3529d8c2 | 5725 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 5726 | { |
a197f664 | 5727 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5728 | struct io_defer_entry *de; |
f67676d1 | 5729 | int ret; |
9cf7c104 | 5730 | u32 seq; |
de0617e4 | 5731 | |
9d858b21 | 5732 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5733 | if (likely(list_empty_careful(&ctx->defer_list) && |
5734 | !(req->flags & REQ_F_IO_DRAIN))) | |
5735 | return 0; | |
5736 | ||
5737 | seq = io_get_sequence(req); | |
5738 | /* Still a chance to pass the sequence check */ | |
5739 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5740 | return 0; |
5741 | ||
e8c2bc1f | 5742 | if (!req->async_data) { |
650b5481 | 5743 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 5744 | if (ret) |
650b5481 PB |
5745 | return ret; |
5746 | } | |
cbdcb435 | 5747 | io_prep_async_link(req); |
27dc8338 PB |
5748 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5749 | if (!de) | |
5750 | return -ENOMEM; | |
2d28390a | 5751 | |
de0617e4 | 5752 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5753 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5754 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5755 | kfree(de); |
ae34817b PB |
5756 | io_queue_async_work(req); |
5757 | return -EIOCBQUEUED; | |
de0617e4 JA |
5758 | } |
5759 | ||
915967f6 | 5760 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5761 | de->req = req; |
9cf7c104 | 5762 | de->seq = seq; |
27dc8338 | 5763 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5764 | spin_unlock_irq(&ctx->completion_lock); |
5765 | return -EIOCBQUEUED; | |
5766 | } | |
5767 | ||
f573d384 JA |
5768 | static void io_req_drop_files(struct io_kiocb *req) |
5769 | { | |
5770 | struct io_ring_ctx *ctx = req->ctx; | |
5771 | unsigned long flags; | |
5772 | ||
5773 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
5774 | list_del(&req->inflight_entry); | |
5775 | if (waitqueue_active(&ctx->inflight_wait)) | |
5776 | wake_up(&ctx->inflight_wait); | |
5777 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
5778 | req->flags &= ~REQ_F_INFLIGHT; | |
0f212204 | 5779 | put_files_struct(req->work.files); |
9b828492 | 5780 | put_nsproxy(req->work.nsproxy); |
f573d384 JA |
5781 | req->work.files = NULL; |
5782 | } | |
5783 | ||
3ca405eb | 5784 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5785 | { |
0e1b6fe3 PB |
5786 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5787 | switch (req->opcode) { | |
5788 | case IORING_OP_READV: | |
5789 | case IORING_OP_READ_FIXED: | |
5790 | case IORING_OP_READ: | |
bcda7baa | 5791 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5792 | break; |
5793 | case IORING_OP_RECVMSG: | |
5794 | case IORING_OP_RECV: | |
bcda7baa | 5795 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5796 | break; |
5797 | } | |
5798 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5799 | } |
5800 | ||
0e1b6fe3 PB |
5801 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5802 | switch (req->opcode) { | |
5803 | case IORING_OP_READV: | |
5804 | case IORING_OP_READ_FIXED: | |
5805 | case IORING_OP_READ: | |
5806 | case IORING_OP_WRITEV: | |
5807 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5808 | case IORING_OP_WRITE: { |
5809 | struct io_async_rw *io = req->async_data; | |
5810 | if (io->free_iovec) | |
5811 | kfree(io->free_iovec); | |
0e1b6fe3 | 5812 | break; |
e8c2bc1f | 5813 | } |
0e1b6fe3 | 5814 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5815 | case IORING_OP_SENDMSG: { |
5816 | struct io_async_msghdr *io = req->async_data; | |
5817 | if (io->iov != io->fast_iov) | |
5818 | kfree(io->iov); | |
0e1b6fe3 | 5819 | break; |
e8c2bc1f | 5820 | } |
0e1b6fe3 PB |
5821 | case IORING_OP_SPLICE: |
5822 | case IORING_OP_TEE: | |
5823 | io_put_file(req, req->splice.file_in, | |
5824 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5825 | break; | |
f3cd4850 JA |
5826 | case IORING_OP_OPENAT: |
5827 | case IORING_OP_OPENAT2: | |
5828 | if (req->open.filename) | |
5829 | putname(req->open.filename); | |
5830 | break; | |
0e1b6fe3 PB |
5831 | } |
5832 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5833 | } |
bb175342 | 5834 | |
f573d384 JA |
5835 | if (req->flags & REQ_F_INFLIGHT) |
5836 | io_req_drop_files(req); | |
99bc4c38 PB |
5837 | } |
5838 | ||
3529d8c2 | 5839 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
f13fad7b | 5840 | bool force_nonblock, struct io_comp_state *cs) |
2b188cc1 | 5841 | { |
a197f664 | 5842 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 5843 | int ret; |
2b188cc1 | 5844 | |
d625c6ee | 5845 | switch (req->opcode) { |
2b188cc1 | 5846 | case IORING_OP_NOP: |
229a7b63 | 5847 | ret = io_nop(req, cs); |
2b188cc1 JA |
5848 | break; |
5849 | case IORING_OP_READV: | |
edafccee | 5850 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5851 | case IORING_OP_READ: |
3529d8c2 JA |
5852 | if (sqe) { |
5853 | ret = io_read_prep(req, sqe, force_nonblock); | |
5854 | if (ret < 0) | |
5855 | break; | |
5856 | } | |
a1d7c393 | 5857 | ret = io_read(req, force_nonblock, cs); |
edafccee | 5858 | break; |
3529d8c2 | 5859 | case IORING_OP_WRITEV: |
edafccee | 5860 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5861 | case IORING_OP_WRITE: |
3529d8c2 JA |
5862 | if (sqe) { |
5863 | ret = io_write_prep(req, sqe, force_nonblock); | |
5864 | if (ret < 0) | |
5865 | break; | |
5866 | } | |
a1d7c393 | 5867 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 5868 | break; |
c992fe29 | 5869 | case IORING_OP_FSYNC: |
3529d8c2 JA |
5870 | if (sqe) { |
5871 | ret = io_prep_fsync(req, sqe); | |
5872 | if (ret < 0) | |
5873 | break; | |
5874 | } | |
014db007 | 5875 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 5876 | break; |
221c5eb2 | 5877 | case IORING_OP_POLL_ADD: |
3529d8c2 JA |
5878 | if (sqe) { |
5879 | ret = io_poll_add_prep(req, sqe); | |
5880 | if (ret) | |
5881 | break; | |
5882 | } | |
014db007 | 5883 | ret = io_poll_add(req); |
221c5eb2 JA |
5884 | break; |
5885 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 JA |
5886 | if (sqe) { |
5887 | ret = io_poll_remove_prep(req, sqe); | |
5888 | if (ret < 0) | |
5889 | break; | |
5890 | } | |
fc4df999 | 5891 | ret = io_poll_remove(req); |
221c5eb2 | 5892 | break; |
5d17b4a4 | 5893 | case IORING_OP_SYNC_FILE_RANGE: |
3529d8c2 JA |
5894 | if (sqe) { |
5895 | ret = io_prep_sfr(req, sqe); | |
5896 | if (ret < 0) | |
5897 | break; | |
5898 | } | |
014db007 | 5899 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 5900 | break; |
0fa03c62 | 5901 | case IORING_OP_SENDMSG: |
fddaface | 5902 | case IORING_OP_SEND: |
3529d8c2 JA |
5903 | if (sqe) { |
5904 | ret = io_sendmsg_prep(req, sqe); | |
5905 | if (ret < 0) | |
5906 | break; | |
5907 | } | |
fddaface | 5908 | if (req->opcode == IORING_OP_SENDMSG) |
229a7b63 | 5909 | ret = io_sendmsg(req, force_nonblock, cs); |
fddaface | 5910 | else |
229a7b63 | 5911 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 5912 | break; |
aa1fa28f | 5913 | case IORING_OP_RECVMSG: |
fddaface | 5914 | case IORING_OP_RECV: |
3529d8c2 JA |
5915 | if (sqe) { |
5916 | ret = io_recvmsg_prep(req, sqe); | |
5917 | if (ret) | |
5918 | break; | |
5919 | } | |
fddaface | 5920 | if (req->opcode == IORING_OP_RECVMSG) |
229a7b63 | 5921 | ret = io_recvmsg(req, force_nonblock, cs); |
fddaface | 5922 | else |
229a7b63 | 5923 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 5924 | break; |
5262f567 | 5925 | case IORING_OP_TIMEOUT: |
3529d8c2 JA |
5926 | if (sqe) { |
5927 | ret = io_timeout_prep(req, sqe, false); | |
5928 | if (ret) | |
5929 | break; | |
5930 | } | |
fc4df999 | 5931 | ret = io_timeout(req); |
5262f567 | 5932 | break; |
11365043 | 5933 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 JA |
5934 | if (sqe) { |
5935 | ret = io_timeout_remove_prep(req, sqe); | |
5936 | if (ret) | |
5937 | break; | |
5938 | } | |
fc4df999 | 5939 | ret = io_timeout_remove(req); |
11365043 | 5940 | break; |
17f2fe35 | 5941 | case IORING_OP_ACCEPT: |
3529d8c2 JA |
5942 | if (sqe) { |
5943 | ret = io_accept_prep(req, sqe); | |
5944 | if (ret) | |
5945 | break; | |
5946 | } | |
229a7b63 | 5947 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 5948 | break; |
f8e85cf2 | 5949 | case IORING_OP_CONNECT: |
3529d8c2 JA |
5950 | if (sqe) { |
5951 | ret = io_connect_prep(req, sqe); | |
5952 | if (ret) | |
5953 | break; | |
5954 | } | |
229a7b63 | 5955 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 5956 | break; |
62755e35 | 5957 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 JA |
5958 | if (sqe) { |
5959 | ret = io_async_cancel_prep(req, sqe); | |
5960 | if (ret) | |
5961 | break; | |
5962 | } | |
014db007 | 5963 | ret = io_async_cancel(req); |
62755e35 | 5964 | break; |
d63d1b5e JA |
5965 | case IORING_OP_FALLOCATE: |
5966 | if (sqe) { | |
5967 | ret = io_fallocate_prep(req, sqe); | |
5968 | if (ret) | |
5969 | break; | |
5970 | } | |
014db007 | 5971 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 5972 | break; |
15b71abe JA |
5973 | case IORING_OP_OPENAT: |
5974 | if (sqe) { | |
5975 | ret = io_openat_prep(req, sqe); | |
5976 | if (ret) | |
5977 | break; | |
5978 | } | |
014db007 | 5979 | ret = io_openat(req, force_nonblock); |
15b71abe | 5980 | break; |
b5dba59e JA |
5981 | case IORING_OP_CLOSE: |
5982 | if (sqe) { | |
5983 | ret = io_close_prep(req, sqe); | |
5984 | if (ret) | |
5985 | break; | |
5986 | } | |
229a7b63 | 5987 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 5988 | break; |
05f3fb3c JA |
5989 | case IORING_OP_FILES_UPDATE: |
5990 | if (sqe) { | |
5991 | ret = io_files_update_prep(req, sqe); | |
5992 | if (ret) | |
5993 | break; | |
5994 | } | |
229a7b63 | 5995 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 5996 | break; |
eddc7ef5 JA |
5997 | case IORING_OP_STATX: |
5998 | if (sqe) { | |
5999 | ret = io_statx_prep(req, sqe); | |
6000 | if (ret) | |
6001 | break; | |
6002 | } | |
014db007 | 6003 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 6004 | break; |
4840e418 JA |
6005 | case IORING_OP_FADVISE: |
6006 | if (sqe) { | |
6007 | ret = io_fadvise_prep(req, sqe); | |
6008 | if (ret) | |
6009 | break; | |
6010 | } | |
014db007 | 6011 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 6012 | break; |
c1ca757b JA |
6013 | case IORING_OP_MADVISE: |
6014 | if (sqe) { | |
6015 | ret = io_madvise_prep(req, sqe); | |
6016 | if (ret) | |
6017 | break; | |
6018 | } | |
014db007 | 6019 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 6020 | break; |
cebdb986 JA |
6021 | case IORING_OP_OPENAT2: |
6022 | if (sqe) { | |
6023 | ret = io_openat2_prep(req, sqe); | |
6024 | if (ret) | |
6025 | break; | |
6026 | } | |
014db007 | 6027 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 6028 | break; |
3e4827b0 JA |
6029 | case IORING_OP_EPOLL_CTL: |
6030 | if (sqe) { | |
6031 | ret = io_epoll_ctl_prep(req, sqe); | |
6032 | if (ret) | |
6033 | break; | |
6034 | } | |
229a7b63 | 6035 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 6036 | break; |
7d67af2c PB |
6037 | case IORING_OP_SPLICE: |
6038 | if (sqe) { | |
6039 | ret = io_splice_prep(req, sqe); | |
6040 | if (ret < 0) | |
6041 | break; | |
6042 | } | |
014db007 | 6043 | ret = io_splice(req, force_nonblock); |
7d67af2c | 6044 | break; |
ddf0322d JA |
6045 | case IORING_OP_PROVIDE_BUFFERS: |
6046 | if (sqe) { | |
6047 | ret = io_provide_buffers_prep(req, sqe); | |
6048 | if (ret) | |
6049 | break; | |
6050 | } | |
229a7b63 | 6051 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 6052 | break; |
067524e9 JA |
6053 | case IORING_OP_REMOVE_BUFFERS: |
6054 | if (sqe) { | |
6055 | ret = io_remove_buffers_prep(req, sqe); | |
6056 | if (ret) | |
6057 | break; | |
6058 | } | |
229a7b63 | 6059 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 6060 | break; |
f2a8d5c7 PB |
6061 | case IORING_OP_TEE: |
6062 | if (sqe) { | |
6063 | ret = io_tee_prep(req, sqe); | |
6064 | if (ret < 0) | |
6065 | break; | |
6066 | } | |
6067 | ret = io_tee(req, force_nonblock); | |
6068 | break; | |
2b188cc1 JA |
6069 | default: |
6070 | ret = -EINVAL; | |
6071 | break; | |
6072 | } | |
6073 | ||
def596e9 JA |
6074 | if (ret) |
6075 | return ret; | |
6076 | ||
b532576e JA |
6077 | /* If the op doesn't have a file, we're not polling for it */ |
6078 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6079 | const bool in_async = io_wq_current_is_worker(); |
6080 | ||
11ba820b JA |
6081 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6082 | if (in_async) | |
6083 | mutex_lock(&ctx->uring_lock); | |
6084 | ||
def596e9 | 6085 | io_iopoll_req_issued(req); |
11ba820b JA |
6086 | |
6087 | if (in_async) | |
6088 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6089 | } |
6090 | ||
6091 | return 0; | |
2b188cc1 JA |
6092 | } |
6093 | ||
f4db7182 | 6094 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6095 | { |
6096 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6097 | struct io_kiocb *timeout; |
561fb04a | 6098 | int ret = 0; |
2b188cc1 | 6099 | |
6df1db6b PB |
6100 | timeout = io_prep_linked_timeout(req); |
6101 | if (timeout) | |
6102 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6103 | |
0c9d5ccd JA |
6104 | /* if NO_CANCEL is set, we must still run the work */ |
6105 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
6106 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 6107 | ret = -ECANCELED; |
0c9d5ccd | 6108 | } |
31b51510 | 6109 | |
561fb04a | 6110 | if (!ret) { |
561fb04a | 6111 | do { |
f13fad7b | 6112 | ret = io_issue_sqe(req, NULL, false, NULL); |
561fb04a JA |
6113 | /* |
6114 | * We can get EAGAIN for polled IO even though we're | |
6115 | * forcing a sync submission from here, since we can't | |
6116 | * wait for request slots on the block side. | |
6117 | */ | |
6118 | if (ret != -EAGAIN) | |
6119 | break; | |
6120 | cond_resched(); | |
6121 | } while (1); | |
6122 | } | |
31b51510 | 6123 | |
561fb04a | 6124 | if (ret) { |
4e88d6e7 | 6125 | req_set_fail_links(req); |
e1e16097 | 6126 | io_req_complete(req, ret); |
edafccee | 6127 | } |
2b188cc1 | 6128 | |
f4db7182 | 6129 | return io_steal_work(req); |
2b188cc1 JA |
6130 | } |
6131 | ||
65e19f54 JA |
6132 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6133 | int index) | |
6134 | { | |
6135 | struct fixed_file_table *table; | |
6136 | ||
05f3fb3c | 6137 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6138 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6139 | } |
6140 | ||
8da11c19 PB |
6141 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
6142 | int fd, struct file **out_file, bool fixed) | |
09bb8394 | 6143 | { |
a197f664 | 6144 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6145 | struct file *file; |
09bb8394 | 6146 | |
8da11c19 | 6147 | if (fixed) { |
05f3fb3c | 6148 | if (unlikely(!ctx->file_data || |
09bb8394 JA |
6149 | (unsigned) fd >= ctx->nr_user_files)) |
6150 | return -EBADF; | |
b7620121 | 6151 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6152 | file = io_file_from_index(ctx, fd); |
fd2206e4 JA |
6153 | if (file) { |
6154 | req->fixed_file_refs = ctx->file_data->cur_refs; | |
6155 | percpu_ref_get(req->fixed_file_refs); | |
6156 | } | |
09bb8394 | 6157 | } else { |
c826bd7a | 6158 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6159 | file = __io_file_get(state, fd); |
09bb8394 JA |
6160 | } |
6161 | ||
fd2206e4 JA |
6162 | if (file || io_op_defs[req->opcode].needs_file_no_error) { |
6163 | *out_file = file; | |
6164 | return 0; | |
6165 | } | |
6166 | return -EBADF; | |
09bb8394 JA |
6167 | } |
6168 | ||
8da11c19 | 6169 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
63ff8223 | 6170 | int fd) |
8da11c19 | 6171 | { |
8da11c19 PB |
6172 | bool fixed; |
6173 | ||
63ff8223 | 6174 | fixed = (req->flags & REQ_F_FIXED_FILE) != 0; |
0cdaf760 | 6175 | if (unlikely(!fixed && io_async_submit(req->ctx))) |
8da11c19 PB |
6176 | return -EBADF; |
6177 | ||
6178 | return io_file_get(state, req, fd, &req->file, fixed); | |
6179 | } | |
6180 | ||
a197f664 | 6181 | static int io_grab_files(struct io_kiocb *req) |
fcb323cc | 6182 | { |
a197f664 | 6183 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc | 6184 | |
f56040b8 PB |
6185 | io_req_init_async(req); |
6186 | ||
5b0bbee4 | 6187 | if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE)) |
f86cd20c | 6188 | return 0; |
b5dba59e | 6189 | |
0f212204 | 6190 | req->work.files = get_files_struct(current); |
9b828492 JA |
6191 | get_nsproxy(current->nsproxy); |
6192 | req->work.nsproxy = current->nsproxy; | |
0f212204 JA |
6193 | req->flags |= REQ_F_INFLIGHT; |
6194 | ||
fcb323cc | 6195 | spin_lock_irq(&ctx->inflight_lock); |
0f212204 | 6196 | list_add(&req->inflight_entry, &ctx->inflight_list); |
fcb323cc | 6197 | spin_unlock_irq(&ctx->inflight_lock); |
0f212204 | 6198 | return 0; |
fcb323cc JA |
6199 | } |
6200 | ||
f56040b8 PB |
6201 | static inline int io_prep_work_files(struct io_kiocb *req) |
6202 | { | |
6203 | if (!io_op_defs[req->opcode].file_table) | |
6204 | return 0; | |
6205 | return io_grab_files(req); | |
6206 | } | |
6207 | ||
2665abfd | 6208 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6209 | { |
ad8a48ac JA |
6210 | struct io_timeout_data *data = container_of(timer, |
6211 | struct io_timeout_data, timer); | |
6212 | struct io_kiocb *req = data->req; | |
2665abfd JA |
6213 | struct io_ring_ctx *ctx = req->ctx; |
6214 | struct io_kiocb *prev = NULL; | |
6215 | unsigned long flags; | |
2665abfd JA |
6216 | |
6217 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
6218 | ||
6219 | /* | |
6220 | * We don't expect the list to be empty, that will only happen if we | |
6221 | * race with the completion of the linked work. | |
6222 | */ | |
4493233e PB |
6223 | if (!list_empty(&req->link_list)) { |
6224 | prev = list_entry(req->link_list.prev, struct io_kiocb, | |
6225 | link_list); | |
5d960724 | 6226 | if (refcount_inc_not_zero(&prev->refs)) { |
4493233e | 6227 | list_del_init(&req->link_list); |
5d960724 JA |
6228 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
6229 | } else | |
76a46e06 | 6230 | prev = NULL; |
2665abfd JA |
6231 | } |
6232 | ||
6233 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6234 | ||
6235 | if (prev) { | |
4e88d6e7 | 6236 | req_set_fail_links(prev); |
014db007 | 6237 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
76a46e06 | 6238 | io_put_req(prev); |
47f46768 | 6239 | } else { |
e1e16097 | 6240 | io_req_complete(req, -ETIME); |
2665abfd | 6241 | } |
2665abfd JA |
6242 | return HRTIMER_NORESTART; |
6243 | } | |
6244 | ||
7271ef3a | 6245 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6246 | { |
76a46e06 JA |
6247 | /* |
6248 | * If the list is now empty, then our linked request finished before | |
6249 | * we got a chance to setup the timer | |
6250 | */ | |
4493233e | 6251 | if (!list_empty(&req->link_list)) { |
e8c2bc1f | 6252 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6253 | |
ad8a48ac JA |
6254 | data->timer.function = io_link_timeout_fn; |
6255 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6256 | data->mode); | |
2665abfd | 6257 | } |
7271ef3a JA |
6258 | } |
6259 | ||
6260 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6261 | { | |
6262 | struct io_ring_ctx *ctx = req->ctx; | |
6263 | ||
6264 | spin_lock_irq(&ctx->completion_lock); | |
6265 | __io_queue_linked_timeout(req); | |
76a46e06 | 6266 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6267 | |
2665abfd | 6268 | /* drop submission reference */ |
76a46e06 JA |
6269 | io_put_req(req); |
6270 | } | |
2665abfd | 6271 | |
ad8a48ac | 6272 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd JA |
6273 | { |
6274 | struct io_kiocb *nxt; | |
6275 | ||
dea3b49c | 6276 | if (!(req->flags & REQ_F_LINK_HEAD)) |
2665abfd | 6277 | return NULL; |
6df1db6b | 6278 | if (req->flags & REQ_F_LINK_TIMEOUT) |
d7718a9d | 6279 | return NULL; |
2665abfd | 6280 | |
4493233e PB |
6281 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
6282 | link_list); | |
d625c6ee | 6283 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
76a46e06 | 6284 | return NULL; |
2665abfd | 6285 | |
76a46e06 | 6286 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6287 | return nxt; |
2665abfd JA |
6288 | } |
6289 | ||
f13fad7b JA |
6290 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6291 | struct io_comp_state *cs) | |
2b188cc1 | 6292 | { |
4a0a7a18 | 6293 | struct io_kiocb *linked_timeout; |
4bc4494e | 6294 | struct io_kiocb *nxt; |
193155c8 | 6295 | const struct cred *old_creds = NULL; |
e0c5c576 | 6296 | int ret; |
2b188cc1 | 6297 | |
4a0a7a18 JA |
6298 | again: |
6299 | linked_timeout = io_prep_linked_timeout(req); | |
6300 | ||
7cdaf587 XW |
6301 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
6302 | req->work.creds != current_cred()) { | |
193155c8 JA |
6303 | if (old_creds) |
6304 | revert_creds(old_creds); | |
6305 | if (old_creds == req->work.creds) | |
6306 | old_creds = NULL; /* restored original creds */ | |
6307 | else | |
6308 | old_creds = override_creds(req->work.creds); | |
6309 | } | |
6310 | ||
f13fad7b | 6311 | ret = io_issue_sqe(req, sqe, true, cs); |
491381ce JA |
6312 | |
6313 | /* | |
6314 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6315 | * doesn't support non-blocking read/write attempts | |
6316 | */ | |
24c74678 | 6317 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6318 | if (!io_arm_poll_handler(req)) { |
86a761f8 | 6319 | punt: |
f063c547 PB |
6320 | ret = io_prep_work_files(req); |
6321 | if (unlikely(ret)) | |
bbad27b2 | 6322 | goto err; |
f063c547 PB |
6323 | /* |
6324 | * Queued up for async execution, worker will release | |
6325 | * submit reference when the iocb is actually submitted. | |
6326 | */ | |
6327 | io_queue_async_work(req); | |
2b188cc1 | 6328 | } |
bbad27b2 | 6329 | |
f063c547 PB |
6330 | if (linked_timeout) |
6331 | io_queue_linked_timeout(linked_timeout); | |
4bc4494e | 6332 | goto exit; |
2b188cc1 | 6333 | } |
e65ef56d | 6334 | |
652532ad | 6335 | if (unlikely(ret)) { |
fcb323cc | 6336 | err: |
652532ad PB |
6337 | /* un-prep timeout, so it'll be killed as any other linked */ |
6338 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6339 | req_set_fail_links(req); |
e65ef56d | 6340 | io_put_req(req); |
e1e16097 | 6341 | io_req_complete(req, ret); |
652532ad | 6342 | goto exit; |
9e645e11 | 6343 | } |
652532ad PB |
6344 | |
6345 | /* drop submission reference */ | |
6346 | nxt = io_put_req_find_next(req); | |
6347 | if (linked_timeout) | |
6348 | io_queue_linked_timeout(linked_timeout); | |
6349 | ||
4a0a7a18 JA |
6350 | if (nxt) { |
6351 | req = nxt; | |
86a761f8 PB |
6352 | |
6353 | if (req->flags & REQ_F_FORCE_ASYNC) | |
6354 | goto punt; | |
4a0a7a18 JA |
6355 | goto again; |
6356 | } | |
4bc4494e | 6357 | exit: |
193155c8 JA |
6358 | if (old_creds) |
6359 | revert_creds(old_creds); | |
2b188cc1 JA |
6360 | } |
6361 | ||
f13fad7b JA |
6362 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6363 | struct io_comp_state *cs) | |
4fe2c963 JL |
6364 | { |
6365 | int ret; | |
6366 | ||
3529d8c2 | 6367 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6368 | if (ret) { |
6369 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6370 | fail_req: |
4e88d6e7 | 6371 | req_set_fail_links(req); |
e1e16097 JA |
6372 | io_put_req(req); |
6373 | io_req_complete(req, ret); | |
4fe2c963 | 6374 | } |
2550878f | 6375 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6376 | if (!req->async_data) { |
bd2ab18a | 6377 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6378 | if (unlikely(ret)) |
bd2ab18a PB |
6379 | goto fail_req; |
6380 | } | |
6381 | ||
ce35a47a JA |
6382 | /* |
6383 | * Never try inline submit of IOSQE_ASYNC is set, go straight | |
6384 | * to async execution. | |
6385 | */ | |
3e863ea3 | 6386 | io_req_init_async(req); |
ce35a47a JA |
6387 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
6388 | io_queue_async_work(req); | |
6389 | } else { | |
f13fad7b | 6390 | __io_queue_sqe(req, sqe, cs); |
ce35a47a | 6391 | } |
4fe2c963 JL |
6392 | } |
6393 | ||
f13fad7b JA |
6394 | static inline void io_queue_link_head(struct io_kiocb *req, |
6395 | struct io_comp_state *cs) | |
4fe2c963 | 6396 | { |
94ae5e77 | 6397 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6398 | io_put_req(req); |
6399 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6400 | } else |
f13fad7b | 6401 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6402 | } |
6403 | ||
1d4240cc | 6404 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
f13fad7b | 6405 | struct io_kiocb **link, struct io_comp_state *cs) |
9e645e11 | 6406 | { |
a197f664 | 6407 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6408 | int ret; |
9e645e11 | 6409 | |
9e645e11 JA |
6410 | /* |
6411 | * If we already have a head request, queue this one for async | |
6412 | * submittal once the head completes. If we don't have a head but | |
6413 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6414 | * submitted sync once the chain is complete. If none of those | |
6415 | * conditions are true (normal request), then just queue it. | |
6416 | */ | |
6417 | if (*link) { | |
9d76377f | 6418 | struct io_kiocb *head = *link; |
4e88d6e7 | 6419 | |
8cdf2193 PB |
6420 | /* |
6421 | * Taking sequential execution of a link, draining both sides | |
6422 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6423 | * requests in the link. So, it drains the head and the | |
6424 | * next after the link request. The last one is done via | |
6425 | * drain_next flag to persist the effect across calls. | |
6426 | */ | |
ef4ff581 | 6427 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6428 | head->flags |= REQ_F_IO_DRAIN; |
6429 | ctx->drain_next = 1; | |
6430 | } | |
3529d8c2 | 6431 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6432 | if (unlikely(ret)) { |
4e88d6e7 | 6433 | /* fail even hard links since we don't submit */ |
9d76377f | 6434 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6435 | return ret; |
2d28390a | 6436 | } |
9d76377f PB |
6437 | trace_io_uring_link(ctx, req, head); |
6438 | list_add_tail(&req->link_list, &head->link_list); | |
32fe525b PB |
6439 | |
6440 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6441 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6442 | io_queue_link_head(head, cs); |
32fe525b PB |
6443 | *link = NULL; |
6444 | } | |
9e645e11 | 6445 | } else { |
711be031 PB |
6446 | if (unlikely(ctx->drain_next)) { |
6447 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6448 | ctx->drain_next = 0; |
711be031 | 6449 | } |
ef4ff581 | 6450 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
dea3b49c | 6451 | req->flags |= REQ_F_LINK_HEAD; |
711be031 | 6452 | INIT_LIST_HEAD(&req->link_list); |
f1d96a8f | 6453 | |
711be031 | 6454 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6455 | if (unlikely(ret)) |
711be031 PB |
6456 | req->flags |= REQ_F_FAIL_LINK; |
6457 | *link = req; | |
6458 | } else { | |
f13fad7b | 6459 | io_queue_sqe(req, sqe, cs); |
711be031 | 6460 | } |
9e645e11 | 6461 | } |
2e6e1fde | 6462 | |
1d4240cc | 6463 | return 0; |
9e645e11 JA |
6464 | } |
6465 | ||
9a56a232 JA |
6466 | /* |
6467 | * Batched submission is done, ensure local IO is flushed out. | |
6468 | */ | |
6469 | static void io_submit_state_end(struct io_submit_state *state) | |
6470 | { | |
f13fad7b JA |
6471 | if (!list_empty(&state->comp.list)) |
6472 | io_submit_flush_completions(&state->comp); | |
9a56a232 | 6473 | blk_finish_plug(&state->plug); |
9f13c35b | 6474 | io_state_file_put(state); |
2579f913 | 6475 | if (state->free_reqs) |
6c8a3134 | 6476 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6477 | } |
6478 | ||
6479 | /* | |
6480 | * Start submission side cache. | |
6481 | */ | |
6482 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6483 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 JA |
6484 | { |
6485 | blk_start_plug(&state->plug); | |
013538bd JA |
6486 | state->comp.nr = 0; |
6487 | INIT_LIST_HEAD(&state->comp.list); | |
6488 | state->comp.ctx = ctx; | |
2579f913 | 6489 | state->free_reqs = 0; |
9a56a232 JA |
6490 | state->file = NULL; |
6491 | state->ios_left = max_ios; | |
6492 | } | |
6493 | ||
2b188cc1 JA |
6494 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6495 | { | |
75b28aff | 6496 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6497 | |
caf582c6 PB |
6498 | /* |
6499 | * Ensure any loads from the SQEs are done at this point, | |
6500 | * since once we write the new head, the application could | |
6501 | * write new data to them. | |
6502 | */ | |
6503 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6504 | } |
6505 | ||
2b188cc1 | 6506 | /* |
3529d8c2 | 6507 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6508 | * that is mapped by userspace. This means that care needs to be taken to |
6509 | * ensure that reads are stable, as we cannot rely on userspace always | |
6510 | * being a good citizen. If members of the sqe are validated and then later | |
6511 | * used, it's important that those reads are done through READ_ONCE() to | |
6512 | * prevent a re-load down the line. | |
6513 | */ | |
709b302f | 6514 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6515 | { |
75b28aff | 6516 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6517 | unsigned head; |
6518 | ||
6519 | /* | |
6520 | * The cached sq head (or cq tail) serves two purposes: | |
6521 | * | |
6522 | * 1) allows us to batch the cost of updating the user visible | |
6523 | * head updates. | |
6524 | * 2) allows the kernel side to track the head on its own, even | |
6525 | * though the application is the one updating it. | |
6526 | */ | |
ee7d46d9 | 6527 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6528 | if (likely(head < ctx->sq_entries)) |
6529 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6530 | |
6531 | /* drop invalid entries */ | |
498ccd9e | 6532 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6533 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6534 | return NULL; |
6535 | } | |
6536 | ||
6537 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6538 | { | |
6539 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6540 | } |
6541 | ||
21b55dbc SG |
6542 | /* |
6543 | * Check SQE restrictions (opcode and flags). | |
6544 | * | |
6545 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6546 | */ | |
6547 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6548 | struct io_kiocb *req, | |
6549 | unsigned int sqe_flags) | |
6550 | { | |
6551 | if (!ctx->restricted) | |
6552 | return true; | |
6553 | ||
6554 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6555 | return false; | |
6556 | ||
6557 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6558 | ctx->restrictions.sqe_flags_required) | |
6559 | return false; | |
6560 | ||
6561 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6562 | ctx->restrictions.sqe_flags_required)) | |
6563 | return false; | |
6564 | ||
6565 | return true; | |
6566 | } | |
6567 | ||
ef4ff581 PB |
6568 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6569 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6570 | IOSQE_BUFFER_SELECT) | |
6571 | ||
6572 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6573 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6574 | struct io_submit_state *state) |
0553b8bd | 6575 | { |
ef4ff581 | 6576 | unsigned int sqe_flags; |
63ff8223 | 6577 | int id; |
ef4ff581 | 6578 | |
0553b8bd PB |
6579 | req->opcode = READ_ONCE(sqe->opcode); |
6580 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6581 | req->async_data = NULL; |
0553b8bd PB |
6582 | req->file = NULL; |
6583 | req->ctx = ctx; | |
6584 | req->flags = 0; | |
6585 | /* one is dropped after submission, the other at completion */ | |
6586 | refcount_set(&req->refs, 2); | |
4dd2824d | 6587 | req->task = current; |
e3bc8e9d | 6588 | get_task_struct(req->task); |
0f212204 | 6589 | atomic_long_inc(&req->task->io_uring->req_issue); |
0553b8bd | 6590 | req->result = 0; |
ef4ff581 PB |
6591 | |
6592 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6593 | return -EINVAL; | |
6594 | ||
9d8426a0 JA |
6595 | if (unlikely(io_sq_thread_acquire_mm(ctx, req))) |
6596 | return -EFAULT; | |
ef4ff581 PB |
6597 | |
6598 | sqe_flags = READ_ONCE(sqe->flags); | |
6599 | /* enforce forwards compatibility on users */ | |
6600 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6601 | return -EINVAL; | |
6602 | ||
21b55dbc SG |
6603 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6604 | return -EACCES; | |
6605 | ||
ef4ff581 PB |
6606 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6607 | !io_op_defs[req->opcode].buffer_select) | |
6608 | return -EOPNOTSUPP; | |
6609 | ||
6610 | id = READ_ONCE(sqe->personality); | |
6611 | if (id) { | |
7cdaf587 | 6612 | io_req_init_async(req); |
ef4ff581 PB |
6613 | req->work.creds = idr_find(&ctx->personality_idr, id); |
6614 | if (unlikely(!req->work.creds)) | |
6615 | return -EINVAL; | |
6616 | get_cred(req->work.creds); | |
6617 | } | |
6618 | ||
6619 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6620 | req->flags |= sqe_flags; |
ef4ff581 | 6621 | |
63ff8223 JA |
6622 | if (!io_op_defs[req->opcode].needs_file) |
6623 | return 0; | |
6624 | ||
6625 | return io_req_set_file(state, req, READ_ONCE(sqe->fd)); | |
0553b8bd PB |
6626 | } |
6627 | ||
0f212204 | 6628 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6629 | { |
ac8691c4 | 6630 | struct io_submit_state state; |
9e645e11 | 6631 | struct io_kiocb *link = NULL; |
9e645e11 | 6632 | int i, submitted = 0; |
6c271ce2 | 6633 | |
c4a2ed72 | 6634 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 JA |
6635 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6636 | if (!list_empty(&ctx->cq_overflow_list) && | |
e6c8aa9a | 6637 | !io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6638 | return -EBUSY; |
6639 | } | |
6c271ce2 | 6640 | |
ee7d46d9 PB |
6641 | /* make sure SQ entry isn't read before tail */ |
6642 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6643 | |
2b85edfc PB |
6644 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6645 | return -EAGAIN; | |
6c271ce2 | 6646 | |
013538bd | 6647 | io_submit_state_start(&state, ctx, nr); |
6c271ce2 JA |
6648 | |
6649 | for (i = 0; i < nr; i++) { | |
3529d8c2 | 6650 | const struct io_uring_sqe *sqe; |
196be95c | 6651 | struct io_kiocb *req; |
1cb1edb2 | 6652 | int err; |
fb5ccc98 | 6653 | |
b1e50e54 PB |
6654 | sqe = io_get_sqe(ctx); |
6655 | if (unlikely(!sqe)) { | |
6656 | io_consume_sqe(ctx); | |
6657 | break; | |
6658 | } | |
ac8691c4 | 6659 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6660 | if (unlikely(!req)) { |
6661 | if (!submitted) | |
6662 | submitted = -EAGAIN; | |
fb5ccc98 | 6663 | break; |
196be95c | 6664 | } |
fb5ccc98 | 6665 | |
ac8691c4 | 6666 | err = io_init_req(ctx, req, sqe, &state); |
709b302f | 6667 | io_consume_sqe(ctx); |
d3656344 JA |
6668 | /* will complete beyond this point, count as submitted */ |
6669 | submitted++; | |
6670 | ||
ef4ff581 | 6671 | if (unlikely(err)) { |
1cb1edb2 | 6672 | fail_req: |
e1e16097 JA |
6673 | io_put_req(req); |
6674 | io_req_complete(req, err); | |
196be95c PB |
6675 | break; |
6676 | } | |
fb5ccc98 | 6677 | |
354420f7 | 6678 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
0cdaf760 | 6679 | true, io_async_submit(ctx)); |
f13fad7b | 6680 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6681 | if (err) |
6682 | goto fail_req; | |
6c271ce2 JA |
6683 | } |
6684 | ||
9466f437 PB |
6685 | if (unlikely(submitted != nr)) { |
6686 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
6687 | ||
6688 | percpu_ref_put_many(&ctx->refs, nr - ref_used); | |
6689 | } | |
9e645e11 | 6690 | if (link) |
f13fad7b | 6691 | io_queue_link_head(link, &state.comp); |
ac8691c4 | 6692 | io_submit_state_end(&state); |
6c271ce2 | 6693 | |
ae9428ca PB |
6694 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6695 | io_commit_sqring(ctx); | |
6696 | ||
6c271ce2 JA |
6697 | return submitted; |
6698 | } | |
6699 | ||
23b3628e XW |
6700 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6701 | { | |
6702 | /* Tell userspace we may need a wakeup call */ | |
6703 | spin_lock_irq(&ctx->completion_lock); | |
6704 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6705 | spin_unlock_irq(&ctx->completion_lock); | |
6706 | } | |
6707 | ||
6708 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6709 | { | |
6710 | spin_lock_irq(&ctx->completion_lock); | |
6711 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6712 | spin_unlock_irq(&ctx->completion_lock); | |
6713 | } | |
6714 | ||
3f0e64d0 JA |
6715 | static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode, |
6716 | int sync, void *key) | |
6717 | { | |
6718 | struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry); | |
6719 | int ret; | |
6720 | ||
6721 | ret = autoremove_wake_function(wqe, mode, sync, key); | |
6722 | if (ret) { | |
6723 | unsigned long flags; | |
6724 | ||
6725 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
6726 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6727 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
6728 | } | |
6729 | return ret; | |
6730 | } | |
6731 | ||
c8d1ba58 JA |
6732 | enum sq_ret { |
6733 | SQT_IDLE = 1, | |
6734 | SQT_SPIN = 2, | |
6735 | SQT_DID_WORK = 4, | |
6736 | }; | |
6737 | ||
6738 | static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx, | |
e95eee2d | 6739 | unsigned long start_jiffies, bool cap_entries) |
6c271ce2 | 6740 | { |
c8d1ba58 | 6741 | unsigned long timeout = start_jiffies + ctx->sq_thread_idle; |
534ca6d6 | 6742 | struct io_sq_data *sqd = ctx->sq_data; |
c8d1ba58 | 6743 | unsigned int to_submit; |
bdcd3eab | 6744 | int ret = 0; |
6c271ce2 | 6745 | |
c8d1ba58 JA |
6746 | again: |
6747 | if (!list_empty(&ctx->iopoll_list)) { | |
6748 | unsigned nr_events = 0; | |
6a779382 | 6749 | |
c8d1ba58 JA |
6750 | mutex_lock(&ctx->uring_lock); |
6751 | if (!list_empty(&ctx->iopoll_list) && !need_resched()) | |
6752 | io_do_iopoll(ctx, &nr_events, 0); | |
6753 | mutex_unlock(&ctx->uring_lock); | |
6754 | } | |
a4c0b3de | 6755 | |
c8d1ba58 | 6756 | to_submit = io_sqring_entries(ctx); |
6c271ce2 | 6757 | |
c8d1ba58 JA |
6758 | /* |
6759 | * If submit got -EBUSY, flag us as needing the application | |
6760 | * to enter the kernel to reap and flush events. | |
6761 | */ | |
6762 | if (!to_submit || ret == -EBUSY || need_resched()) { | |
6763 | /* | |
6764 | * Drop cur_mm before scheduling, we can't hold it for | |
6765 | * long periods (or over schedule()). Do this before | |
6766 | * adding ourselves to the waitqueue, as the unuse/drop | |
6767 | * may sleep. | |
6768 | */ | |
6769 | io_sq_thread_drop_mm(); | |
6770 | ||
6771 | /* | |
6772 | * We're polling. If we're within the defined idle | |
6773 | * period, then let us spin without work before going | |
6774 | * to sleep. The exception is if we got EBUSY doing | |
6775 | * more IO, we should wait for the application to | |
6776 | * reap events and wake us up. | |
6777 | */ | |
6778 | if (!list_empty(&ctx->iopoll_list) || need_resched() || | |
6779 | (!time_after(jiffies, timeout) && ret != -EBUSY && | |
6780 | !percpu_ref_is_dying(&ctx->refs))) | |
6781 | return SQT_SPIN; | |
6c271ce2 | 6782 | |
534ca6d6 | 6783 | prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry, |
c8d1ba58 | 6784 | TASK_INTERRUPTIBLE); |
6c271ce2 | 6785 | |
c8d1ba58 JA |
6786 | /* |
6787 | * While doing polled IO, before going to sleep, we need | |
6788 | * to check if there are new reqs added to iopoll_list, | |
6789 | * it is because reqs may have been punted to io worker | |
6790 | * and will be added to iopoll_list later, hence check | |
6791 | * the iopoll_list again. | |
6792 | */ | |
6793 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6794 | !list_empty_careful(&ctx->iopoll_list)) { | |
534ca6d6 | 6795 | finish_wait(&sqd->wait, &ctx->sqo_wait_entry); |
c8d1ba58 | 6796 | goto again; |
6c271ce2 JA |
6797 | } |
6798 | ||
fb5ccc98 | 6799 | to_submit = io_sqring_entries(ctx); |
c8d1ba58 JA |
6800 | if (!to_submit || ret == -EBUSY) |
6801 | return SQT_IDLE; | |
6802 | } | |
c1edbf5f | 6803 | |
534ca6d6 | 6804 | finish_wait(&sqd->wait, &ctx->sqo_wait_entry); |
c8d1ba58 | 6805 | io_ring_clear_wakeup_flag(ctx); |
7143b5ac | 6806 | |
e95eee2d JA |
6807 | /* if we're handling multiple rings, cap submit size for fairness */ |
6808 | if (cap_entries && to_submit > 8) | |
6809 | to_submit = 8; | |
6810 | ||
c8d1ba58 JA |
6811 | mutex_lock(&ctx->uring_lock); |
6812 | if (likely(!percpu_ref_is_dying(&ctx->refs))) | |
6813 | ret = io_submit_sqes(ctx, to_submit); | |
6814 | mutex_unlock(&ctx->uring_lock); | |
90554200 JA |
6815 | |
6816 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) | |
6817 | wake_up(&ctx->sqo_sq_wait); | |
6818 | ||
c8d1ba58 JA |
6819 | return SQT_DID_WORK; |
6820 | } | |
6c271ce2 | 6821 | |
69fb2131 JA |
6822 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6823 | { | |
6824 | struct io_ring_ctx *ctx; | |
6825 | ||
6826 | while (!list_empty(&sqd->ctx_new_list)) { | |
6827 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
6828 | init_wait(&ctx->sqo_wait_entry); | |
6829 | ctx->sqo_wait_entry.func = io_sq_wake_function; | |
6830 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); | |
6831 | complete(&ctx->sq_thread_comp); | |
6832 | } | |
6833 | } | |
6834 | ||
c8d1ba58 JA |
6835 | static int io_sq_thread(void *data) |
6836 | { | |
91d8f519 | 6837 | struct cgroup_subsys_state *cur_css = NULL; |
69fb2131 JA |
6838 | const struct cred *old_cred = NULL; |
6839 | struct io_sq_data *sqd = data; | |
6840 | struct io_ring_ctx *ctx; | |
c8d1ba58 | 6841 | unsigned long start_jiffies; |
6c271ce2 | 6842 | |
69fb2131 JA |
6843 | start_jiffies = jiffies; |
6844 | while (!kthread_should_stop()) { | |
6845 | enum sq_ret ret = 0; | |
e95eee2d | 6846 | bool cap_entries; |
6c271ce2 | 6847 | |
69fb2131 JA |
6848 | /* |
6849 | * Any changes to the sqd lists are synchronized through the | |
6850 | * kthread parking. This synchronizes the thread vs users, | |
6851 | * the users are synchronized on the sqd->ctx_lock. | |
6852 | */ | |
6853 | if (kthread_should_park()) | |
6854 | kthread_parkme(); | |
6c271ce2 | 6855 | |
69fb2131 JA |
6856 | if (unlikely(!list_empty(&sqd->ctx_new_list))) |
6857 | io_sqd_init_new(sqd); | |
c8d1ba58 | 6858 | |
e95eee2d JA |
6859 | cap_entries = !list_is_singular(&sqd->ctx_list); |
6860 | ||
69fb2131 JA |
6861 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6862 | if (current->cred != ctx->creds) { | |
6863 | if (old_cred) | |
6864 | revert_creds(old_cred); | |
6865 | old_cred = override_creds(ctx->creds); | |
6866 | } | |
91d8f519 | 6867 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
c8d1ba58 | 6868 | |
e95eee2d | 6869 | ret |= __io_sq_thread(ctx, start_jiffies, cap_entries); |
69fb2131 JA |
6870 | |
6871 | io_sq_thread_drop_mm(); | |
6872 | } | |
6873 | ||
6874 | if (ret & SQT_SPIN) { | |
c8d1ba58 JA |
6875 | io_run_task_work(); |
6876 | cond_resched(); | |
69fb2131 JA |
6877 | } else if (ret == SQT_IDLE) { |
6878 | if (kthread_should_park()) | |
6879 | continue; | |
6880 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
6881 | io_ring_set_wakeup_flag(ctx); | |
6882 | schedule(); | |
6883 | start_jiffies = jiffies; | |
6884 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
6885 | io_ring_clear_wakeup_flag(ctx); | |
c8d1ba58 | 6886 | } |
6c271ce2 JA |
6887 | } |
6888 | ||
4c6e277c | 6889 | io_run_task_work(); |
b41e9852 | 6890 | |
91d8f519 DZ |
6891 | if (cur_css) |
6892 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
6893 | if (old_cred) |
6894 | revert_creds(old_cred); | |
06058632 | 6895 | |
2bbcd6d3 | 6896 | kthread_parkme(); |
06058632 | 6897 | |
6c271ce2 JA |
6898 | return 0; |
6899 | } | |
6900 | ||
bda52162 JA |
6901 | struct io_wait_queue { |
6902 | struct wait_queue_entry wq; | |
6903 | struct io_ring_ctx *ctx; | |
6904 | unsigned to_wait; | |
6905 | unsigned nr_timeouts; | |
6906 | }; | |
6907 | ||
1d7bb1d5 | 6908 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
6909 | { |
6910 | struct io_ring_ctx *ctx = iowq->ctx; | |
6911 | ||
6912 | /* | |
d195a66e | 6913 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6914 | * started waiting. For timeouts, we always want to return to userspace, |
6915 | * regardless of event count. | |
6916 | */ | |
1d7bb1d5 | 6917 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
6918 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6919 | } | |
6920 | ||
6921 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6922 | int wake_flags, void *key) | |
6923 | { | |
6924 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6925 | wq); | |
6926 | ||
1d7bb1d5 JA |
6927 | /* use noflush == true, as we can't safely rely on locking context */ |
6928 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
6929 | return -1; |
6930 | ||
6931 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6932 | } | |
6933 | ||
af9c1a44 JA |
6934 | static int io_run_task_work_sig(void) |
6935 | { | |
6936 | if (io_run_task_work()) | |
6937 | return 1; | |
6938 | if (!signal_pending(current)) | |
6939 | return 0; | |
6940 | if (current->jobctl & JOBCTL_TASK_WORK) { | |
6941 | spin_lock_irq(¤t->sighand->siglock); | |
6942 | current->jobctl &= ~JOBCTL_TASK_WORK; | |
6943 | recalc_sigpending(); | |
6944 | spin_unlock_irq(¤t->sighand->siglock); | |
6945 | return 1; | |
6946 | } | |
6947 | return -EINTR; | |
6948 | } | |
6949 | ||
2b188cc1 JA |
6950 | /* |
6951 | * Wait until events become available, if we don't already have some. The | |
6952 | * application must reap them itself, as they reside on the shared cq ring. | |
6953 | */ | |
6954 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
6955 | const sigset_t __user *sig, size_t sigsz) | |
6956 | { | |
bda52162 JA |
6957 | struct io_wait_queue iowq = { |
6958 | .wq = { | |
6959 | .private = current, | |
6960 | .func = io_wake_function, | |
6961 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6962 | }, | |
6963 | .ctx = ctx, | |
6964 | .to_wait = min_events, | |
6965 | }; | |
75b28aff | 6966 | struct io_rings *rings = ctx->rings; |
e9ffa5c2 | 6967 | int ret = 0; |
2b188cc1 | 6968 | |
b41e9852 JA |
6969 | do { |
6970 | if (io_cqring_events(ctx, false) >= min_events) | |
6971 | return 0; | |
4c6e277c | 6972 | if (!io_run_task_work()) |
b41e9852 | 6973 | break; |
b41e9852 | 6974 | } while (1); |
2b188cc1 JA |
6975 | |
6976 | if (sig) { | |
9e75ad5d AB |
6977 | #ifdef CONFIG_COMPAT |
6978 | if (in_compat_syscall()) | |
6979 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6980 | sigsz); |
9e75ad5d AB |
6981 | else |
6982 | #endif | |
b772434b | 6983 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6984 | |
2b188cc1 JA |
6985 | if (ret) |
6986 | return ret; | |
6987 | } | |
6988 | ||
bda52162 | 6989 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6990 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
6991 | do { |
6992 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
6993 | TASK_INTERRUPTIBLE); | |
ce593a6c | 6994 | /* make sure we run task_work before checking for signals */ |
af9c1a44 JA |
6995 | ret = io_run_task_work_sig(); |
6996 | if (ret > 0) | |
4c6e277c | 6997 | continue; |
af9c1a44 | 6998 | else if (ret < 0) |
bda52162 | 6999 | break; |
ce593a6c JA |
7000 | if (io_should_wake(&iowq, false)) |
7001 | break; | |
7002 | schedule(); | |
bda52162 JA |
7003 | } while (1); |
7004 | finish_wait(&ctx->wait, &iowq.wq); | |
7005 | ||
b7db41c9 | 7006 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7007 | |
75b28aff | 7008 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7009 | } |
7010 | ||
6b06314c JA |
7011 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7012 | { | |
7013 | #if defined(CONFIG_UNIX) | |
7014 | if (ctx->ring_sock) { | |
7015 | struct sock *sock = ctx->ring_sock->sk; | |
7016 | struct sk_buff *skb; | |
7017 | ||
7018 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7019 | kfree_skb(skb); | |
7020 | } | |
7021 | #else | |
7022 | int i; | |
7023 | ||
65e19f54 JA |
7024 | for (i = 0; i < ctx->nr_user_files; i++) { |
7025 | struct file *file; | |
7026 | ||
7027 | file = io_file_from_index(ctx, i); | |
7028 | if (file) | |
7029 | fput(file); | |
7030 | } | |
6b06314c JA |
7031 | #endif |
7032 | } | |
7033 | ||
05f3fb3c JA |
7034 | static void io_file_ref_kill(struct percpu_ref *ref) |
7035 | { | |
7036 | struct fixed_file_data *data; | |
7037 | ||
7038 | data = container_of(ref, struct fixed_file_data, refs); | |
7039 | complete(&data->done); | |
7040 | } | |
7041 | ||
6b06314c JA |
7042 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7043 | { | |
05f3fb3c | 7044 | struct fixed_file_data *data = ctx->file_data; |
05589553 | 7045 | struct fixed_file_ref_node *ref_node = NULL; |
65e19f54 JA |
7046 | unsigned nr_tables, i; |
7047 | ||
05f3fb3c | 7048 | if (!data) |
6b06314c JA |
7049 | return -ENXIO; |
7050 | ||
6a4d07cd | 7051 | spin_lock(&data->lock); |
05589553 XW |
7052 | if (!list_empty(&data->ref_list)) |
7053 | ref_node = list_first_entry(&data->ref_list, | |
7054 | struct fixed_file_ref_node, node); | |
6a4d07cd | 7055 | spin_unlock(&data->lock); |
05589553 XW |
7056 | if (ref_node) |
7057 | percpu_ref_kill(&ref_node->refs); | |
7058 | ||
7059 | percpu_ref_kill(&data->refs); | |
7060 | ||
7061 | /* wait for all refs nodes to complete */ | |
4a38aed2 | 7062 | flush_delayed_work(&ctx->file_put_work); |
2faf852d | 7063 | wait_for_completion(&data->done); |
05f3fb3c | 7064 | |
6b06314c | 7065 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7066 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7067 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
7068 | kfree(data->table[i].files); |
7069 | kfree(data->table); | |
05589553 XW |
7070 | percpu_ref_exit(&data->refs); |
7071 | kfree(data); | |
05f3fb3c | 7072 | ctx->file_data = NULL; |
6b06314c JA |
7073 | ctx->nr_user_files = 0; |
7074 | return 0; | |
7075 | } | |
7076 | ||
534ca6d6 | 7077 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7078 | { |
534ca6d6 | 7079 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7080 | /* |
7081 | * The park is a bit of a work-around, without it we get | |
7082 | * warning spews on shutdown with SQPOLL set and affinity | |
7083 | * set to a single CPU. | |
7084 | */ | |
534ca6d6 JA |
7085 | if (sqd->thread) { |
7086 | kthread_park(sqd->thread); | |
7087 | kthread_stop(sqd->thread); | |
7088 | } | |
7089 | ||
7090 | kfree(sqd); | |
7091 | } | |
7092 | } | |
7093 | ||
aa06165d JA |
7094 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7095 | { | |
7096 | struct io_ring_ctx *ctx_attach; | |
7097 | struct io_sq_data *sqd; | |
7098 | struct fd f; | |
7099 | ||
7100 | f = fdget(p->wq_fd); | |
7101 | if (!f.file) | |
7102 | return ERR_PTR(-ENXIO); | |
7103 | if (f.file->f_op != &io_uring_fops) { | |
7104 | fdput(f); | |
7105 | return ERR_PTR(-EINVAL); | |
7106 | } | |
7107 | ||
7108 | ctx_attach = f.file->private_data; | |
7109 | sqd = ctx_attach->sq_data; | |
7110 | if (!sqd) { | |
7111 | fdput(f); | |
7112 | return ERR_PTR(-EINVAL); | |
7113 | } | |
7114 | ||
7115 | refcount_inc(&sqd->refs); | |
7116 | fdput(f); | |
7117 | return sqd; | |
7118 | } | |
7119 | ||
534ca6d6 JA |
7120 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7121 | { | |
7122 | struct io_sq_data *sqd; | |
7123 | ||
aa06165d JA |
7124 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7125 | return io_attach_sq_data(p); | |
7126 | ||
534ca6d6 JA |
7127 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7128 | if (!sqd) | |
7129 | return ERR_PTR(-ENOMEM); | |
7130 | ||
7131 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7132 | INIT_LIST_HEAD(&sqd->ctx_list); |
7133 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7134 | mutex_init(&sqd->ctx_lock); | |
7135 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7136 | init_waitqueue_head(&sqd->wait); |
7137 | return sqd; | |
7138 | } | |
7139 | ||
69fb2131 JA |
7140 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7141 | __releases(&sqd->lock) | |
7142 | { | |
7143 | if (!sqd->thread) | |
7144 | return; | |
7145 | kthread_unpark(sqd->thread); | |
7146 | mutex_unlock(&sqd->lock); | |
7147 | } | |
7148 | ||
7149 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7150 | __acquires(&sqd->lock) | |
7151 | { | |
7152 | if (!sqd->thread) | |
7153 | return; | |
7154 | mutex_lock(&sqd->lock); | |
7155 | kthread_park(sqd->thread); | |
7156 | } | |
7157 | ||
534ca6d6 JA |
7158 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7159 | { | |
7160 | struct io_sq_data *sqd = ctx->sq_data; | |
7161 | ||
7162 | if (sqd) { | |
7163 | if (sqd->thread) { | |
7164 | /* | |
7165 | * We may arrive here from the error branch in | |
7166 | * io_sq_offload_create() where the kthread is created | |
7167 | * without being waked up, thus wake it up now to make | |
7168 | * sure the wait will complete. | |
7169 | */ | |
7170 | wake_up_process(sqd->thread); | |
7171 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7172 | |
7173 | io_sq_thread_park(sqd); | |
7174 | } | |
7175 | ||
7176 | mutex_lock(&sqd->ctx_lock); | |
7177 | list_del(&ctx->sqd_list); | |
7178 | mutex_unlock(&sqd->ctx_lock); | |
7179 | ||
7180 | if (sqd->thread) { | |
7181 | finish_wait(&sqd->wait, &ctx->sqo_wait_entry); | |
7182 | io_sq_thread_unpark(sqd); | |
534ca6d6 JA |
7183 | } |
7184 | ||
7185 | io_put_sq_data(sqd); | |
7186 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7187 | } |
7188 | } | |
7189 | ||
6b06314c JA |
7190 | static void io_finish_async(struct io_ring_ctx *ctx) |
7191 | { | |
6c271ce2 JA |
7192 | io_sq_thread_stop(ctx); |
7193 | ||
561fb04a JA |
7194 | if (ctx->io_wq) { |
7195 | io_wq_destroy(ctx->io_wq); | |
7196 | ctx->io_wq = NULL; | |
6b06314c JA |
7197 | } |
7198 | } | |
7199 | ||
7200 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7201 | /* |
7202 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7203 | * the io_uring can be safely unregistered on process exit, even if we have | |
7204 | * loops in the file referencing. | |
7205 | */ | |
7206 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7207 | { | |
7208 | struct sock *sk = ctx->ring_sock->sk; | |
7209 | struct scm_fp_list *fpl; | |
7210 | struct sk_buff *skb; | |
08a45173 | 7211 | int i, nr_files; |
6b06314c | 7212 | |
6b06314c JA |
7213 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7214 | if (!fpl) | |
7215 | return -ENOMEM; | |
7216 | ||
7217 | skb = alloc_skb(0, GFP_KERNEL); | |
7218 | if (!skb) { | |
7219 | kfree(fpl); | |
7220 | return -ENOMEM; | |
7221 | } | |
7222 | ||
7223 | skb->sk = sk; | |
6b06314c | 7224 | |
08a45173 | 7225 | nr_files = 0; |
6b06314c JA |
7226 | fpl->user = get_uid(ctx->user); |
7227 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7228 | struct file *file = io_file_from_index(ctx, i + offset); |
7229 | ||
7230 | if (!file) | |
08a45173 | 7231 | continue; |
65e19f54 | 7232 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7233 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7234 | nr_files++; | |
6b06314c JA |
7235 | } |
7236 | ||
08a45173 JA |
7237 | if (nr_files) { |
7238 | fpl->max = SCM_MAX_FD; | |
7239 | fpl->count = nr_files; | |
7240 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7241 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7242 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7243 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7244 | |
08a45173 JA |
7245 | for (i = 0; i < nr_files; i++) |
7246 | fput(fpl->fp[i]); | |
7247 | } else { | |
7248 | kfree_skb(skb); | |
7249 | kfree(fpl); | |
7250 | } | |
6b06314c JA |
7251 | |
7252 | return 0; | |
7253 | } | |
7254 | ||
7255 | /* | |
7256 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7257 | * causes regular reference counting to break down. We rely on the UNIX | |
7258 | * garbage collection to take care of this problem for us. | |
7259 | */ | |
7260 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7261 | { | |
7262 | unsigned left, total; | |
7263 | int ret = 0; | |
7264 | ||
7265 | total = 0; | |
7266 | left = ctx->nr_user_files; | |
7267 | while (left) { | |
7268 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7269 | |
7270 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7271 | if (ret) | |
7272 | break; | |
7273 | left -= this_files; | |
7274 | total += this_files; | |
7275 | } | |
7276 | ||
7277 | if (!ret) | |
7278 | return 0; | |
7279 | ||
7280 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7281 | struct file *file = io_file_from_index(ctx, total); |
7282 | ||
7283 | if (file) | |
7284 | fput(file); | |
6b06314c JA |
7285 | total++; |
7286 | } | |
7287 | ||
7288 | return ret; | |
7289 | } | |
7290 | #else | |
7291 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7292 | { | |
7293 | return 0; | |
7294 | } | |
7295 | #endif | |
7296 | ||
65e19f54 JA |
7297 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
7298 | unsigned nr_files) | |
7299 | { | |
7300 | int i; | |
7301 | ||
7302 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 7303 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
7304 | unsigned this_files; |
7305 | ||
7306 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7307 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7308 | GFP_KERNEL); | |
7309 | if (!table->files) | |
7310 | break; | |
7311 | nr_files -= this_files; | |
7312 | } | |
7313 | ||
7314 | if (i == nr_tables) | |
7315 | return 0; | |
7316 | ||
7317 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 7318 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
7319 | kfree(table->files); |
7320 | } | |
7321 | return 1; | |
7322 | } | |
7323 | ||
05f3fb3c JA |
7324 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
7325 | { | |
7326 | #if defined(CONFIG_UNIX) | |
7327 | struct sock *sock = ctx->ring_sock->sk; | |
7328 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7329 | struct sk_buff *skb; | |
7330 | int i; | |
7331 | ||
7332 | __skb_queue_head_init(&list); | |
7333 | ||
7334 | /* | |
7335 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7336 | * remove this entry and rearrange the file array. | |
7337 | */ | |
7338 | skb = skb_dequeue(head); | |
7339 | while (skb) { | |
7340 | struct scm_fp_list *fp; | |
7341 | ||
7342 | fp = UNIXCB(skb).fp; | |
7343 | for (i = 0; i < fp->count; i++) { | |
7344 | int left; | |
7345 | ||
7346 | if (fp->fp[i] != file) | |
7347 | continue; | |
7348 | ||
7349 | unix_notinflight(fp->user, fp->fp[i]); | |
7350 | left = fp->count - 1 - i; | |
7351 | if (left) { | |
7352 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7353 | left * sizeof(struct file *)); | |
7354 | } | |
7355 | fp->count--; | |
7356 | if (!fp->count) { | |
7357 | kfree_skb(skb); | |
7358 | skb = NULL; | |
7359 | } else { | |
7360 | __skb_queue_tail(&list, skb); | |
7361 | } | |
7362 | fput(file); | |
7363 | file = NULL; | |
7364 | break; | |
7365 | } | |
7366 | ||
7367 | if (!file) | |
7368 | break; | |
7369 | ||
7370 | __skb_queue_tail(&list, skb); | |
7371 | ||
7372 | skb = skb_dequeue(head); | |
7373 | } | |
7374 | ||
7375 | if (skb_peek(&list)) { | |
7376 | spin_lock_irq(&head->lock); | |
7377 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7378 | __skb_queue_tail(head, skb); | |
7379 | spin_unlock_irq(&head->lock); | |
7380 | } | |
7381 | #else | |
7382 | fput(file); | |
7383 | #endif | |
7384 | } | |
7385 | ||
7386 | struct io_file_put { | |
05589553 | 7387 | struct list_head list; |
05f3fb3c | 7388 | struct file *file; |
05f3fb3c JA |
7389 | }; |
7390 | ||
4a38aed2 | 7391 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
65e19f54 | 7392 | { |
4a38aed2 JA |
7393 | struct fixed_file_data *file_data = ref_node->file_data; |
7394 | struct io_ring_ctx *ctx = file_data->ctx; | |
05f3fb3c | 7395 | struct io_file_put *pfile, *tmp; |
05589553 XW |
7396 | |
7397 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { | |
6a4d07cd | 7398 | list_del(&pfile->list); |
05589553 XW |
7399 | io_ring_file_put(ctx, pfile->file); |
7400 | kfree(pfile); | |
65e19f54 | 7401 | } |
05589553 | 7402 | |
6a4d07cd JA |
7403 | spin_lock(&file_data->lock); |
7404 | list_del(&ref_node->node); | |
7405 | spin_unlock(&file_data->lock); | |
05589553 XW |
7406 | |
7407 | percpu_ref_exit(&ref_node->refs); | |
7408 | kfree(ref_node); | |
7409 | percpu_ref_put(&file_data->refs); | |
2faf852d | 7410 | } |
65e19f54 | 7411 | |
4a38aed2 JA |
7412 | static void io_file_put_work(struct work_struct *work) |
7413 | { | |
7414 | struct io_ring_ctx *ctx; | |
7415 | struct llist_node *node; | |
7416 | ||
7417 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); | |
7418 | node = llist_del_all(&ctx->file_put_llist); | |
7419 | ||
7420 | while (node) { | |
7421 | struct fixed_file_ref_node *ref_node; | |
7422 | struct llist_node *next = node->next; | |
7423 | ||
7424 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); | |
7425 | __io_file_put_work(ref_node); | |
7426 | node = next; | |
7427 | } | |
7428 | } | |
7429 | ||
05589553 | 7430 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
2faf852d | 7431 | { |
05589553 | 7432 | struct fixed_file_ref_node *ref_node; |
4a38aed2 JA |
7433 | struct io_ring_ctx *ctx; |
7434 | bool first_add; | |
7435 | int delay = HZ; | |
65e19f54 | 7436 | |
05589553 | 7437 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
4a38aed2 | 7438 | ctx = ref_node->file_data->ctx; |
05589553 | 7439 | |
4a38aed2 JA |
7440 | if (percpu_ref_is_dying(&ctx->file_data->refs)) |
7441 | delay = 0; | |
05589553 | 7442 | |
4a38aed2 JA |
7443 | first_add = llist_add(&ref_node->llist, &ctx->file_put_llist); |
7444 | if (!delay) | |
7445 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); | |
7446 | else if (first_add) | |
7447 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); | |
05f3fb3c | 7448 | } |
65e19f54 | 7449 | |
05589553 XW |
7450 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
7451 | struct io_ring_ctx *ctx) | |
05f3fb3c | 7452 | { |
05589553 | 7453 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7454 | |
05589553 XW |
7455 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7456 | if (!ref_node) | |
7457 | return ERR_PTR(-ENOMEM); | |
05f3fb3c | 7458 | |
05589553 XW |
7459 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
7460 | 0, GFP_KERNEL)) { | |
7461 | kfree(ref_node); | |
7462 | return ERR_PTR(-ENOMEM); | |
7463 | } | |
7464 | INIT_LIST_HEAD(&ref_node->node); | |
7465 | INIT_LIST_HEAD(&ref_node->file_list); | |
05589553 XW |
7466 | ref_node->file_data = ctx->file_data; |
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; | |
65e19f54 | 7480 | unsigned nr_tables; |
05f3fb3c | 7481 | struct file *file; |
6b06314c JA |
7482 | int fd, ret = 0; |
7483 | unsigned i; | |
05589553 | 7484 | struct fixed_file_ref_node *ref_node; |
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 | ||
05f3fb3c JA |
7493 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
7494 | if (!ctx->file_data) | |
7495 | return -ENOMEM; | |
7496 | ctx->file_data->ctx = ctx; | |
7497 | init_completion(&ctx->file_data->done); | |
05589553 | 7498 | INIT_LIST_HEAD(&ctx->file_data->ref_list); |
f7fe9346 | 7499 | spin_lock_init(&ctx->file_data->lock); |
05f3fb3c | 7500 | |
65e19f54 | 7501 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
05f3fb3c JA |
7502 | ctx->file_data->table = kcalloc(nr_tables, |
7503 | sizeof(struct fixed_file_table), | |
65e19f54 | 7504 | GFP_KERNEL); |
05f3fb3c JA |
7505 | if (!ctx->file_data->table) { |
7506 | kfree(ctx->file_data); | |
7507 | ctx->file_data = NULL; | |
6b06314c | 7508 | return -ENOMEM; |
05f3fb3c JA |
7509 | } |
7510 | ||
05589553 | 7511 | if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill, |
05f3fb3c JA |
7512 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7513 | kfree(ctx->file_data->table); | |
7514 | kfree(ctx->file_data); | |
7515 | ctx->file_data = NULL; | |
6b06314c | 7516 | return -ENOMEM; |
05f3fb3c | 7517 | } |
6b06314c | 7518 | |
65e19f54 | 7519 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
05f3fb3c JA |
7520 | percpu_ref_exit(&ctx->file_data->refs); |
7521 | kfree(ctx->file_data->table); | |
7522 | kfree(ctx->file_data); | |
7523 | ctx->file_data = NULL; | |
65e19f54 JA |
7524 | return -ENOMEM; |
7525 | } | |
7526 | ||
08a45173 | 7527 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
7528 | struct fixed_file_table *table; |
7529 | unsigned index; | |
7530 | ||
6b06314c JA |
7531 | ret = -EFAULT; |
7532 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
7533 | break; | |
08a45173 JA |
7534 | /* allow sparse sets */ |
7535 | if (fd == -1) { | |
7536 | ret = 0; | |
7537 | continue; | |
7538 | } | |
6b06314c | 7539 | |
05f3fb3c | 7540 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
65e19f54 | 7541 | index = i & IORING_FILE_TABLE_MASK; |
05f3fb3c | 7542 | file = fget(fd); |
6b06314c JA |
7543 | |
7544 | ret = -EBADF; | |
05f3fb3c | 7545 | if (!file) |
6b06314c | 7546 | break; |
05f3fb3c | 7547 | |
6b06314c JA |
7548 | /* |
7549 | * Don't allow io_uring instances to be registered. If UNIX | |
7550 | * isn't enabled, then this causes a reference cycle and this | |
7551 | * instance can never get freed. If UNIX is enabled we'll | |
7552 | * handle it just fine, but there's still no point in allowing | |
7553 | * a ring fd as it doesn't support regular read/write anyway. | |
7554 | */ | |
05f3fb3c JA |
7555 | if (file->f_op == &io_uring_fops) { |
7556 | fput(file); | |
6b06314c JA |
7557 | break; |
7558 | } | |
6b06314c | 7559 | ret = 0; |
05f3fb3c | 7560 | table->files[index] = file; |
6b06314c JA |
7561 | } |
7562 | ||
7563 | if (ret) { | |
65e19f54 | 7564 | for (i = 0; i < ctx->nr_user_files; i++) { |
65e19f54 JA |
7565 | file = io_file_from_index(ctx, i); |
7566 | if (file) | |
7567 | fput(file); | |
7568 | } | |
7569 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7570 | kfree(ctx->file_data->table[i].files); |
6b06314c | 7571 | |
667e57da | 7572 | percpu_ref_exit(&ctx->file_data->refs); |
05f3fb3c JA |
7573 | kfree(ctx->file_data->table); |
7574 | kfree(ctx->file_data); | |
7575 | ctx->file_data = NULL; | |
6b06314c JA |
7576 | ctx->nr_user_files = 0; |
7577 | return ret; | |
7578 | } | |
7579 | ||
7580 | ret = io_sqe_files_scm(ctx); | |
05589553 | 7581 | if (ret) { |
6b06314c | 7582 | io_sqe_files_unregister(ctx); |
05589553 XW |
7583 | return ret; |
7584 | } | |
6b06314c | 7585 | |
05589553 XW |
7586 | ref_node = alloc_fixed_file_ref_node(ctx); |
7587 | if (IS_ERR(ref_node)) { | |
7588 | io_sqe_files_unregister(ctx); | |
7589 | return PTR_ERR(ref_node); | |
7590 | } | |
7591 | ||
7592 | ctx->file_data->cur_refs = &ref_node->refs; | |
6a4d07cd | 7593 | spin_lock(&ctx->file_data->lock); |
05589553 | 7594 | list_add(&ref_node->node, &ctx->file_data->ref_list); |
6a4d07cd | 7595 | spin_unlock(&ctx->file_data->lock); |
05589553 | 7596 | percpu_ref_get(&ctx->file_data->refs); |
6b06314c JA |
7597 | return ret; |
7598 | } | |
7599 | ||
c3a31e60 JA |
7600 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7601 | int index) | |
7602 | { | |
7603 | #if defined(CONFIG_UNIX) | |
7604 | struct sock *sock = ctx->ring_sock->sk; | |
7605 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7606 | struct sk_buff *skb; | |
7607 | ||
7608 | /* | |
7609 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7610 | * file set. If there's no room, fall back to allocating a new skb | |
7611 | * and filling it in. | |
7612 | */ | |
7613 | spin_lock_irq(&head->lock); | |
7614 | skb = skb_peek(head); | |
7615 | if (skb) { | |
7616 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7617 | ||
7618 | if (fpl->count < SCM_MAX_FD) { | |
7619 | __skb_unlink(skb, head); | |
7620 | spin_unlock_irq(&head->lock); | |
7621 | fpl->fp[fpl->count] = get_file(file); | |
7622 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7623 | fpl->count++; | |
7624 | spin_lock_irq(&head->lock); | |
7625 | __skb_queue_head(head, skb); | |
7626 | } else { | |
7627 | skb = NULL; | |
7628 | } | |
7629 | } | |
7630 | spin_unlock_irq(&head->lock); | |
7631 | ||
7632 | if (skb) { | |
7633 | fput(file); | |
7634 | return 0; | |
7635 | } | |
7636 | ||
7637 | return __io_sqe_files_scm(ctx, 1, index); | |
7638 | #else | |
7639 | return 0; | |
7640 | #endif | |
7641 | } | |
7642 | ||
a5318d3c | 7643 | static int io_queue_file_removal(struct fixed_file_data *data, |
05589553 | 7644 | struct file *file) |
05f3fb3c | 7645 | { |
a5318d3c | 7646 | struct io_file_put *pfile; |
05589553 XW |
7647 | struct percpu_ref *refs = data->cur_refs; |
7648 | struct fixed_file_ref_node *ref_node; | |
05f3fb3c | 7649 | |
05f3fb3c | 7650 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
a5318d3c HD |
7651 | if (!pfile) |
7652 | return -ENOMEM; | |
05f3fb3c | 7653 | |
05589553 | 7654 | ref_node = container_of(refs, struct fixed_file_ref_node, refs); |
05f3fb3c | 7655 | pfile->file = file; |
05589553 XW |
7656 | list_add(&pfile->list, &ref_node->file_list); |
7657 | ||
a5318d3c | 7658 | return 0; |
05f3fb3c JA |
7659 | } |
7660 | ||
7661 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
7662 | struct io_uring_files_update *up, | |
7663 | unsigned nr_args) | |
7664 | { | |
7665 | struct fixed_file_data *data = ctx->file_data; | |
05589553 | 7666 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7667 | struct file *file; |
c3a31e60 JA |
7668 | __s32 __user *fds; |
7669 | int fd, i, err; | |
7670 | __u32 done; | |
05589553 | 7671 | bool needs_switch = false; |
c3a31e60 | 7672 | |
05f3fb3c | 7673 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7674 | return -EOVERFLOW; |
7675 | if (done > ctx->nr_user_files) | |
7676 | return -EINVAL; | |
7677 | ||
05589553 XW |
7678 | ref_node = alloc_fixed_file_ref_node(ctx); |
7679 | if (IS_ERR(ref_node)) | |
7680 | return PTR_ERR(ref_node); | |
7681 | ||
c3a31e60 | 7682 | done = 0; |
05f3fb3c | 7683 | fds = u64_to_user_ptr(up->fds); |
c3a31e60 | 7684 | while (nr_args) { |
65e19f54 JA |
7685 | struct fixed_file_table *table; |
7686 | unsigned index; | |
7687 | ||
c3a31e60 JA |
7688 | err = 0; |
7689 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7690 | err = -EFAULT; | |
7691 | break; | |
7692 | } | |
05f3fb3c JA |
7693 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
7694 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
7695 | index = i & IORING_FILE_TABLE_MASK; |
7696 | if (table->files[index]) { | |
98dfd502 | 7697 | file = table->files[index]; |
a5318d3c HD |
7698 | err = io_queue_file_removal(data, file); |
7699 | if (err) | |
7700 | break; | |
65e19f54 | 7701 | table->files[index] = NULL; |
05589553 | 7702 | needs_switch = true; |
c3a31e60 JA |
7703 | } |
7704 | if (fd != -1) { | |
c3a31e60 JA |
7705 | file = fget(fd); |
7706 | if (!file) { | |
7707 | err = -EBADF; | |
7708 | break; | |
7709 | } | |
7710 | /* | |
7711 | * Don't allow io_uring instances to be registered. If | |
7712 | * UNIX isn't enabled, then this causes a reference | |
7713 | * cycle and this instance can never get freed. If UNIX | |
7714 | * is enabled we'll handle it just fine, but there's | |
7715 | * still no point in allowing a ring fd as it doesn't | |
7716 | * support regular read/write anyway. | |
7717 | */ | |
7718 | if (file->f_op == &io_uring_fops) { | |
7719 | fput(file); | |
7720 | err = -EBADF; | |
7721 | break; | |
7722 | } | |
65e19f54 | 7723 | table->files[index] = file; |
c3a31e60 | 7724 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7725 | if (err) { |
95d1c8e5 | 7726 | table->files[index] = NULL; |
f3bd9dae | 7727 | fput(file); |
c3a31e60 | 7728 | break; |
f3bd9dae | 7729 | } |
c3a31e60 JA |
7730 | } |
7731 | nr_args--; | |
7732 | done++; | |
05f3fb3c JA |
7733 | up->offset++; |
7734 | } | |
7735 | ||
05589553 XW |
7736 | if (needs_switch) { |
7737 | percpu_ref_kill(data->cur_refs); | |
6a4d07cd | 7738 | spin_lock(&data->lock); |
05589553 XW |
7739 | list_add(&ref_node->node, &data->ref_list); |
7740 | data->cur_refs = &ref_node->refs; | |
6a4d07cd | 7741 | spin_unlock(&data->lock); |
05589553 XW |
7742 | percpu_ref_get(&ctx->file_data->refs); |
7743 | } else | |
7744 | destroy_fixed_file_ref_node(ref_node); | |
c3a31e60 JA |
7745 | |
7746 | return done ? done : err; | |
7747 | } | |
05589553 | 7748 | |
05f3fb3c JA |
7749 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7750 | unsigned nr_args) | |
7751 | { | |
7752 | struct io_uring_files_update up; | |
7753 | ||
7754 | if (!ctx->file_data) | |
7755 | return -ENXIO; | |
7756 | if (!nr_args) | |
7757 | return -EINVAL; | |
7758 | if (copy_from_user(&up, arg, sizeof(up))) | |
7759 | return -EFAULT; | |
7760 | if (up.resv) | |
7761 | return -EINVAL; | |
7762 | ||
7763 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7764 | } | |
c3a31e60 | 7765 | |
e9fd9396 | 7766 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
7767 | { |
7768 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7769 | ||
e9fd9396 | 7770 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
7771 | io_put_req(req); |
7772 | } | |
7773 | ||
24369c2e PB |
7774 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
7775 | struct io_uring_params *p) | |
7776 | { | |
7777 | struct io_wq_data data; | |
7778 | struct fd f; | |
7779 | struct io_ring_ctx *ctx_attach; | |
7780 | unsigned int concurrency; | |
7781 | int ret = 0; | |
7782 | ||
7783 | data.user = ctx->user; | |
e9fd9396 | 7784 | data.free_work = io_free_work; |
f5fa38c5 | 7785 | data.do_work = io_wq_submit_work; |
24369c2e PB |
7786 | |
7787 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
7788 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
7789 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
7790 | ||
7791 | ctx->io_wq = io_wq_create(concurrency, &data); | |
7792 | if (IS_ERR(ctx->io_wq)) { | |
7793 | ret = PTR_ERR(ctx->io_wq); | |
7794 | ctx->io_wq = NULL; | |
7795 | } | |
7796 | return ret; | |
7797 | } | |
7798 | ||
7799 | f = fdget(p->wq_fd); | |
7800 | if (!f.file) | |
7801 | return -EBADF; | |
7802 | ||
7803 | if (f.file->f_op != &io_uring_fops) { | |
7804 | ret = -EINVAL; | |
7805 | goto out_fput; | |
7806 | } | |
7807 | ||
7808 | ctx_attach = f.file->private_data; | |
7809 | /* @io_wq is protected by holding the fd */ | |
7810 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
7811 | ret = -EINVAL; | |
7812 | goto out_fput; | |
7813 | } | |
7814 | ||
7815 | ctx->io_wq = ctx_attach->io_wq; | |
7816 | out_fput: | |
7817 | fdput(f); | |
7818 | return ret; | |
7819 | } | |
7820 | ||
0f212204 JA |
7821 | static int io_uring_alloc_task_context(struct task_struct *task) |
7822 | { | |
7823 | struct io_uring_task *tctx; | |
7824 | ||
7825 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7826 | if (unlikely(!tctx)) | |
7827 | return -ENOMEM; | |
7828 | ||
7829 | xa_init(&tctx->xa); | |
7830 | init_waitqueue_head(&tctx->wait); | |
7831 | tctx->last = NULL; | |
7832 | tctx->in_idle = 0; | |
7833 | atomic_long_set(&tctx->req_issue, 0); | |
7834 | atomic_long_set(&tctx->req_complete, 0); | |
7835 | task->io_uring = tctx; | |
7836 | return 0; | |
7837 | } | |
7838 | ||
7839 | void __io_uring_free(struct task_struct *tsk) | |
7840 | { | |
7841 | struct io_uring_task *tctx = tsk->io_uring; | |
7842 | ||
7843 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
7844 | xa_destroy(&tctx->xa); | |
7845 | kfree(tctx); | |
7846 | tsk->io_uring = NULL; | |
7847 | } | |
7848 | ||
7e84e1c7 SG |
7849 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7850 | struct io_uring_params *p) | |
2b188cc1 JA |
7851 | { |
7852 | int ret; | |
7853 | ||
6c271ce2 | 7854 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
7855 | struct io_sq_data *sqd; |
7856 | ||
3ec482d1 JA |
7857 | ret = -EPERM; |
7858 | if (!capable(CAP_SYS_ADMIN)) | |
7859 | goto err; | |
7860 | ||
534ca6d6 JA |
7861 | sqd = io_get_sq_data(p); |
7862 | if (IS_ERR(sqd)) { | |
7863 | ret = PTR_ERR(sqd); | |
7864 | goto err; | |
7865 | } | |
69fb2131 | 7866 | |
534ca6d6 | 7867 | ctx->sq_data = sqd; |
69fb2131 JA |
7868 | io_sq_thread_park(sqd); |
7869 | mutex_lock(&sqd->ctx_lock); | |
7870 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
7871 | mutex_unlock(&sqd->ctx_lock); | |
7872 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 7873 | |
917257da JA |
7874 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7875 | if (!ctx->sq_thread_idle) | |
7876 | ctx->sq_thread_idle = HZ; | |
7877 | ||
aa06165d JA |
7878 | if (sqd->thread) |
7879 | goto done; | |
7880 | ||
6c271ce2 | 7881 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7882 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7883 | |
917257da | 7884 | ret = -EINVAL; |
44a9bd18 JA |
7885 | if (cpu >= nr_cpu_ids) |
7886 | goto err; | |
7889f44d | 7887 | if (!cpu_online(cpu)) |
917257da JA |
7888 | goto err; |
7889 | ||
69fb2131 | 7890 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 7891 | cpu, "io_uring-sq"); |
6c271ce2 | 7892 | } else { |
69fb2131 | 7893 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
7894 | "io_uring-sq"); |
7895 | } | |
534ca6d6 JA |
7896 | if (IS_ERR(sqd->thread)) { |
7897 | ret = PTR_ERR(sqd->thread); | |
7898 | sqd->thread = NULL; | |
6c271ce2 JA |
7899 | goto err; |
7900 | } | |
534ca6d6 | 7901 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
7902 | if (ret) |
7903 | goto err; | |
6c271ce2 JA |
7904 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7905 | /* Can't have SQ_AFF without SQPOLL */ | |
7906 | ret = -EINVAL; | |
7907 | goto err; | |
7908 | } | |
7909 | ||
aa06165d | 7910 | done: |
24369c2e PB |
7911 | ret = io_init_wq_offload(ctx, p); |
7912 | if (ret) | |
2b188cc1 | 7913 | goto err; |
2b188cc1 JA |
7914 | |
7915 | return 0; | |
7916 | err: | |
54a91f3b | 7917 | io_finish_async(ctx); |
2b188cc1 JA |
7918 | return ret; |
7919 | } | |
7920 | ||
7e84e1c7 SG |
7921 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7922 | { | |
534ca6d6 JA |
7923 | struct io_sq_data *sqd = ctx->sq_data; |
7924 | ||
7925 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
7926 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
7927 | } |
7928 | ||
a087e2b5 BM |
7929 | static inline void __io_unaccount_mem(struct user_struct *user, |
7930 | unsigned long nr_pages) | |
2b188cc1 JA |
7931 | { |
7932 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7933 | } | |
7934 | ||
a087e2b5 BM |
7935 | static inline int __io_account_mem(struct user_struct *user, |
7936 | unsigned long nr_pages) | |
2b188cc1 JA |
7937 | { |
7938 | unsigned long page_limit, cur_pages, new_pages; | |
7939 | ||
7940 | /* Don't allow more pages than we can safely lock */ | |
7941 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7942 | ||
7943 | do { | |
7944 | cur_pages = atomic_long_read(&user->locked_vm); | |
7945 | new_pages = cur_pages + nr_pages; | |
7946 | if (new_pages > page_limit) | |
7947 | return -ENOMEM; | |
7948 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7949 | new_pages) != cur_pages); | |
7950 | ||
7951 | return 0; | |
7952 | } | |
7953 | ||
2e0464d4 BM |
7954 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
7955 | enum io_mem_account acct) | |
a087e2b5 | 7956 | { |
aad5d8da | 7957 | if (ctx->limit_mem) |
a087e2b5 | 7958 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7959 | |
2aede0e4 | 7960 | if (ctx->mm_account) { |
2e0464d4 | 7961 | if (acct == ACCT_LOCKED) |
2aede0e4 | 7962 | ctx->mm_account->locked_vm -= nr_pages; |
2e0464d4 | 7963 | else if (acct == ACCT_PINNED) |
2aede0e4 | 7964 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
2e0464d4 | 7965 | } |
a087e2b5 BM |
7966 | } |
7967 | ||
2e0464d4 BM |
7968 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
7969 | enum io_mem_account acct) | |
a087e2b5 | 7970 | { |
30975825 BM |
7971 | int ret; |
7972 | ||
7973 | if (ctx->limit_mem) { | |
7974 | ret = __io_account_mem(ctx->user, nr_pages); | |
7975 | if (ret) | |
7976 | return ret; | |
7977 | } | |
7978 | ||
2aede0e4 | 7979 | if (ctx->mm_account) { |
2e0464d4 | 7980 | if (acct == ACCT_LOCKED) |
2aede0e4 | 7981 | ctx->mm_account->locked_vm += nr_pages; |
2e0464d4 | 7982 | else if (acct == ACCT_PINNED) |
2aede0e4 | 7983 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
2e0464d4 | 7984 | } |
a087e2b5 BM |
7985 | |
7986 | return 0; | |
7987 | } | |
7988 | ||
2b188cc1 JA |
7989 | static void io_mem_free(void *ptr) |
7990 | { | |
52e04ef4 MR |
7991 | struct page *page; |
7992 | ||
7993 | if (!ptr) | |
7994 | return; | |
2b188cc1 | 7995 | |
52e04ef4 | 7996 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7997 | if (put_page_testzero(page)) |
7998 | free_compound_page(page); | |
7999 | } | |
8000 | ||
8001 | static void *io_mem_alloc(size_t size) | |
8002 | { | |
8003 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
8004 | __GFP_NORETRY; | |
8005 | ||
8006 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8007 | } | |
8008 | ||
75b28aff HV |
8009 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8010 | size_t *sq_offset) | |
8011 | { | |
8012 | struct io_rings *rings; | |
8013 | size_t off, sq_array_size; | |
8014 | ||
8015 | off = struct_size(rings, cqes, cq_entries); | |
8016 | if (off == SIZE_MAX) | |
8017 | return SIZE_MAX; | |
8018 | ||
8019 | #ifdef CONFIG_SMP | |
8020 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8021 | if (off == 0) | |
8022 | return SIZE_MAX; | |
8023 | #endif | |
8024 | ||
b36200f5 DV |
8025 | if (sq_offset) |
8026 | *sq_offset = off; | |
8027 | ||
75b28aff HV |
8028 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8029 | if (sq_array_size == SIZE_MAX) | |
8030 | return SIZE_MAX; | |
8031 | ||
8032 | if (check_add_overflow(off, sq_array_size, &off)) | |
8033 | return SIZE_MAX; | |
8034 | ||
75b28aff HV |
8035 | return off; |
8036 | } | |
8037 | ||
2b188cc1 JA |
8038 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8039 | { | |
75b28aff | 8040 | size_t pages; |
2b188cc1 | 8041 | |
75b28aff HV |
8042 | pages = (size_t)1 << get_order( |
8043 | rings_size(sq_entries, cq_entries, NULL)); | |
8044 | pages += (size_t)1 << get_order( | |
8045 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8046 | |
75b28aff | 8047 | return pages; |
2b188cc1 JA |
8048 | } |
8049 | ||
edafccee JA |
8050 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
8051 | { | |
8052 | int i, j; | |
8053 | ||
8054 | if (!ctx->user_bufs) | |
8055 | return -ENXIO; | |
8056 | ||
8057 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8058 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8059 | ||
8060 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8061 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8062 | |
de293938 JA |
8063 | if (imu->acct_pages) |
8064 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8065 | kvfree(imu->bvec); |
edafccee JA |
8066 | imu->nr_bvecs = 0; |
8067 | } | |
8068 | ||
8069 | kfree(ctx->user_bufs); | |
8070 | ctx->user_bufs = NULL; | |
8071 | ctx->nr_user_bufs = 0; | |
8072 | return 0; | |
8073 | } | |
8074 | ||
8075 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8076 | void __user *arg, unsigned index) | |
8077 | { | |
8078 | struct iovec __user *src; | |
8079 | ||
8080 | #ifdef CONFIG_COMPAT | |
8081 | if (ctx->compat) { | |
8082 | struct compat_iovec __user *ciovs; | |
8083 | struct compat_iovec ciov; | |
8084 | ||
8085 | ciovs = (struct compat_iovec __user *) arg; | |
8086 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8087 | return -EFAULT; | |
8088 | ||
d55e5f5b | 8089 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8090 | dst->iov_len = ciov.iov_len; |
8091 | return 0; | |
8092 | } | |
8093 | #endif | |
8094 | src = (struct iovec __user *) arg; | |
8095 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8096 | return -EFAULT; | |
8097 | return 0; | |
8098 | } | |
8099 | ||
de293938 JA |
8100 | /* |
8101 | * Not super efficient, but this is just a registration time. And we do cache | |
8102 | * the last compound head, so generally we'll only do a full search if we don't | |
8103 | * match that one. | |
8104 | * | |
8105 | * We check if the given compound head page has already been accounted, to | |
8106 | * avoid double accounting it. This allows us to account the full size of the | |
8107 | * page, not just the constituent pages of a huge page. | |
8108 | */ | |
8109 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8110 | int nr_pages, struct page *hpage) | |
8111 | { | |
8112 | int i, j; | |
8113 | ||
8114 | /* check current page array */ | |
8115 | for (i = 0; i < nr_pages; i++) { | |
8116 | if (!PageCompound(pages[i])) | |
8117 | continue; | |
8118 | if (compound_head(pages[i]) == hpage) | |
8119 | return true; | |
8120 | } | |
8121 | ||
8122 | /* check previously registered pages */ | |
8123 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8124 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8125 | ||
8126 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8127 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8128 | continue; | |
8129 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8130 | return true; | |
8131 | } | |
8132 | } | |
8133 | ||
8134 | return false; | |
8135 | } | |
8136 | ||
8137 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8138 | int nr_pages, struct io_mapped_ubuf *imu, | |
8139 | struct page **last_hpage) | |
8140 | { | |
8141 | int i, ret; | |
8142 | ||
8143 | for (i = 0; i < nr_pages; i++) { | |
8144 | if (!PageCompound(pages[i])) { | |
8145 | imu->acct_pages++; | |
8146 | } else { | |
8147 | struct page *hpage; | |
8148 | ||
8149 | hpage = compound_head(pages[i]); | |
8150 | if (hpage == *last_hpage) | |
8151 | continue; | |
8152 | *last_hpage = hpage; | |
8153 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8154 | continue; | |
8155 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8156 | } | |
8157 | } | |
8158 | ||
8159 | if (!imu->acct_pages) | |
8160 | return 0; | |
8161 | ||
8162 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8163 | if (ret) | |
8164 | imu->acct_pages = 0; | |
8165 | return ret; | |
8166 | } | |
8167 | ||
edafccee JA |
8168 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
8169 | unsigned nr_args) | |
8170 | { | |
8171 | struct vm_area_struct **vmas = NULL; | |
8172 | struct page **pages = NULL; | |
de293938 | 8173 | struct page *last_hpage = NULL; |
edafccee JA |
8174 | int i, j, got_pages = 0; |
8175 | int ret = -EINVAL; | |
8176 | ||
8177 | if (ctx->user_bufs) | |
8178 | return -EBUSY; | |
8179 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8180 | return -EINVAL; | |
8181 | ||
8182 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8183 | GFP_KERNEL); | |
8184 | if (!ctx->user_bufs) | |
8185 | return -ENOMEM; | |
8186 | ||
8187 | for (i = 0; i < nr_args; i++) { | |
8188 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8189 | unsigned long off, start, end, ubuf; | |
8190 | int pret, nr_pages; | |
8191 | struct iovec iov; | |
8192 | size_t size; | |
8193 | ||
8194 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8195 | if (ret) | |
a278682d | 8196 | goto err; |
edafccee JA |
8197 | |
8198 | /* | |
8199 | * Don't impose further limits on the size and buffer | |
8200 | * constraints here, we'll -EINVAL later when IO is | |
8201 | * submitted if they are wrong. | |
8202 | */ | |
8203 | ret = -EFAULT; | |
8204 | if (!iov.iov_base || !iov.iov_len) | |
8205 | goto err; | |
8206 | ||
8207 | /* arbitrary limit, but we need something */ | |
8208 | if (iov.iov_len > SZ_1G) | |
8209 | goto err; | |
8210 | ||
8211 | ubuf = (unsigned long) iov.iov_base; | |
8212 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8213 | start = ubuf >> PAGE_SHIFT; | |
8214 | nr_pages = end - start; | |
8215 | ||
edafccee JA |
8216 | ret = 0; |
8217 | if (!pages || nr_pages > got_pages) { | |
a8c73c1a DE |
8218 | kvfree(vmas); |
8219 | kvfree(pages); | |
d4ef6475 | 8220 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 8221 | GFP_KERNEL); |
d4ef6475 | 8222 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
8223 | sizeof(struct vm_area_struct *), |
8224 | GFP_KERNEL); | |
8225 | if (!pages || !vmas) { | |
8226 | ret = -ENOMEM; | |
edafccee JA |
8227 | goto err; |
8228 | } | |
8229 | got_pages = nr_pages; | |
8230 | } | |
8231 | ||
d4ef6475 | 8232 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
8233 | GFP_KERNEL); |
8234 | ret = -ENOMEM; | |
de293938 | 8235 | if (!imu->bvec) |
edafccee | 8236 | goto err; |
edafccee JA |
8237 | |
8238 | ret = 0; | |
d8ed45c5 | 8239 | mmap_read_lock(current->mm); |
2113b05d | 8240 | pret = pin_user_pages(ubuf, nr_pages, |
932f4a63 IW |
8241 | FOLL_WRITE | FOLL_LONGTERM, |
8242 | pages, vmas); | |
edafccee JA |
8243 | if (pret == nr_pages) { |
8244 | /* don't support file backed memory */ | |
8245 | for (j = 0; j < nr_pages; j++) { | |
8246 | struct vm_area_struct *vma = vmas[j]; | |
8247 | ||
8248 | if (vma->vm_file && | |
8249 | !is_file_hugepages(vma->vm_file)) { | |
8250 | ret = -EOPNOTSUPP; | |
8251 | break; | |
8252 | } | |
8253 | } | |
8254 | } else { | |
8255 | ret = pret < 0 ? pret : -EFAULT; | |
8256 | } | |
d8ed45c5 | 8257 | mmap_read_unlock(current->mm); |
edafccee JA |
8258 | if (ret) { |
8259 | /* | |
8260 | * if we did partial map, or found file backed vmas, | |
8261 | * release any pages we did get | |
8262 | */ | |
27c4d3a3 | 8263 | if (pret > 0) |
f1f6a7dd | 8264 | unpin_user_pages(pages, pret); |
de293938 JA |
8265 | kvfree(imu->bvec); |
8266 | goto err; | |
8267 | } | |
8268 | ||
8269 | ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage); | |
8270 | if (ret) { | |
8271 | unpin_user_pages(pages, pret); | |
d4ef6475 | 8272 | kvfree(imu->bvec); |
edafccee JA |
8273 | goto err; |
8274 | } | |
8275 | ||
8276 | off = ubuf & ~PAGE_MASK; | |
8277 | size = iov.iov_len; | |
8278 | for (j = 0; j < nr_pages; j++) { | |
8279 | size_t vec_len; | |
8280 | ||
8281 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8282 | imu->bvec[j].bv_page = pages[j]; | |
8283 | imu->bvec[j].bv_len = vec_len; | |
8284 | imu->bvec[j].bv_offset = off; | |
8285 | off = 0; | |
8286 | size -= vec_len; | |
8287 | } | |
8288 | /* store original address for later verification */ | |
8289 | imu->ubuf = ubuf; | |
8290 | imu->len = iov.iov_len; | |
8291 | imu->nr_bvecs = nr_pages; | |
8292 | ||
8293 | ctx->nr_user_bufs++; | |
8294 | } | |
d4ef6475 MR |
8295 | kvfree(pages); |
8296 | kvfree(vmas); | |
edafccee JA |
8297 | return 0; |
8298 | err: | |
d4ef6475 MR |
8299 | kvfree(pages); |
8300 | kvfree(vmas); | |
edafccee JA |
8301 | io_sqe_buffer_unregister(ctx); |
8302 | return ret; | |
8303 | } | |
8304 | ||
9b402849 JA |
8305 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8306 | { | |
8307 | __s32 __user *fds = arg; | |
8308 | int fd; | |
8309 | ||
8310 | if (ctx->cq_ev_fd) | |
8311 | return -EBUSY; | |
8312 | ||
8313 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8314 | return -EFAULT; | |
8315 | ||
8316 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8317 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8318 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8319 | ctx->cq_ev_fd = NULL; | |
8320 | return ret; | |
8321 | } | |
8322 | ||
8323 | return 0; | |
8324 | } | |
8325 | ||
8326 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8327 | { | |
8328 | if (ctx->cq_ev_fd) { | |
8329 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8330 | ctx->cq_ev_fd = NULL; | |
8331 | return 0; | |
8332 | } | |
8333 | ||
8334 | return -ENXIO; | |
8335 | } | |
8336 | ||
5a2e745d JA |
8337 | static int __io_destroy_buffers(int id, void *p, void *data) |
8338 | { | |
8339 | struct io_ring_ctx *ctx = data; | |
8340 | struct io_buffer *buf = p; | |
8341 | ||
067524e9 | 8342 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8343 | return 0; |
8344 | } | |
8345 | ||
8346 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8347 | { | |
8348 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8349 | idr_destroy(&ctx->io_buffer_idr); | |
8350 | } | |
8351 | ||
2b188cc1 JA |
8352 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8353 | { | |
6b06314c | 8354 | io_finish_async(ctx); |
5dbcad51 | 8355 | io_sqe_buffer_unregister(ctx); |
2aede0e4 JA |
8356 | |
8357 | if (ctx->sqo_task) { | |
8358 | put_task_struct(ctx->sqo_task); | |
8359 | ctx->sqo_task = NULL; | |
8360 | mmdrop(ctx->mm_account); | |
8361 | ctx->mm_account = NULL; | |
30975825 | 8362 | } |
def596e9 | 8363 | |
91d8f519 DZ |
8364 | #ifdef CONFIG_BLK_CGROUP |
8365 | if (ctx->sqo_blkcg_css) | |
8366 | css_put(ctx->sqo_blkcg_css); | |
8367 | #endif | |
8368 | ||
6b06314c | 8369 | io_sqe_files_unregister(ctx); |
9b402849 | 8370 | io_eventfd_unregister(ctx); |
5a2e745d | 8371 | io_destroy_buffers(ctx); |
41726c9a | 8372 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8373 | |
2b188cc1 | 8374 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8375 | if (ctx->ring_sock) { |
8376 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8377 | sock_release(ctx->ring_sock); |
355e8d26 | 8378 | } |
2b188cc1 JA |
8379 | #endif |
8380 | ||
75b28aff | 8381 | io_mem_free(ctx->rings); |
2b188cc1 | 8382 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8383 | |
8384 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8385 | free_uid(ctx->user); |
181e448d | 8386 | put_cred(ctx->creds); |
78076bb6 | 8387 | kfree(ctx->cancel_hash); |
0ddf92e8 | 8388 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
8389 | kfree(ctx); |
8390 | } | |
8391 | ||
8392 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8393 | { | |
8394 | struct io_ring_ctx *ctx = file->private_data; | |
8395 | __poll_t mask = 0; | |
8396 | ||
8397 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8398 | /* |
8399 | * synchronizes with barrier from wq_has_sleeper call in | |
8400 | * io_commit_cqring | |
8401 | */ | |
2b188cc1 | 8402 | smp_rmb(); |
90554200 | 8403 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8404 | mask |= EPOLLOUT | EPOLLWRNORM; |
63e5d81f | 8405 | if (io_cqring_events(ctx, false)) |
2b188cc1 JA |
8406 | mask |= EPOLLIN | EPOLLRDNORM; |
8407 | ||
8408 | return mask; | |
8409 | } | |
8410 | ||
8411 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8412 | { | |
8413 | struct io_ring_ctx *ctx = file->private_data; | |
8414 | ||
8415 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8416 | } | |
8417 | ||
071698e1 JA |
8418 | static int io_remove_personalities(int id, void *p, void *data) |
8419 | { | |
8420 | struct io_ring_ctx *ctx = data; | |
8421 | const struct cred *cred; | |
8422 | ||
8423 | cred = idr_remove(&ctx->personality_idr, id); | |
8424 | if (cred) | |
8425 | put_cred(cred); | |
8426 | return 0; | |
8427 | } | |
8428 | ||
85faa7b8 JA |
8429 | static void io_ring_exit_work(struct work_struct *work) |
8430 | { | |
b2edc0a7 PB |
8431 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8432 | exit_work); | |
85faa7b8 | 8433 | |
56952e91 JA |
8434 | /* |
8435 | * If we're doing polled IO and end up having requests being | |
8436 | * submitted async (out-of-line), then completions can come in while | |
8437 | * we're waiting for refs to drop. We need to reap these manually, | |
8438 | * as nobody else will be looking for them. | |
8439 | */ | |
b2edc0a7 | 8440 | do { |
56952e91 | 8441 | if (ctx->rings) |
e6c8aa9a | 8442 | io_cqring_overflow_flush(ctx, true, NULL, NULL); |
b2edc0a7 PB |
8443 | io_iopoll_try_reap_events(ctx); |
8444 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); | |
85faa7b8 JA |
8445 | io_ring_ctx_free(ctx); |
8446 | } | |
8447 | ||
2b188cc1 JA |
8448 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8449 | { | |
8450 | mutex_lock(&ctx->uring_lock); | |
8451 | percpu_ref_kill(&ctx->refs); | |
8452 | mutex_unlock(&ctx->uring_lock); | |
8453 | ||
f3606e3a JA |
8454 | io_kill_timeouts(ctx, NULL); |
8455 | io_poll_remove_all(ctx, NULL); | |
561fb04a JA |
8456 | |
8457 | if (ctx->io_wq) | |
8458 | io_wq_cancel_all(ctx->io_wq); | |
8459 | ||
15dff286 JA |
8460 | /* if we failed setting up the ctx, we might not have any rings */ |
8461 | if (ctx->rings) | |
e6c8aa9a | 8462 | io_cqring_overflow_flush(ctx, true, NULL, NULL); |
b2edc0a7 | 8463 | io_iopoll_try_reap_events(ctx); |
071698e1 | 8464 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
309fc03a JA |
8465 | |
8466 | /* | |
8467 | * Do this upfront, so we won't have a grace period where the ring | |
8468 | * is closed but resources aren't reaped yet. This can cause | |
8469 | * spurious failure in setting up a new ring. | |
8470 | */ | |
760618f7 JA |
8471 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8472 | ACCT_LOCKED); | |
309fc03a | 8473 | |
85faa7b8 | 8474 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8475 | /* |
8476 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8477 | * if we're exiting a ton of rings at the same time. It just adds | |
8478 | * noise and overhead, there's no discernable change in runtime | |
8479 | * over using system_wq. | |
8480 | */ | |
8481 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8482 | } |
8483 | ||
8484 | static int io_uring_release(struct inode *inode, struct file *file) | |
8485 | { | |
8486 | struct io_ring_ctx *ctx = file->private_data; | |
8487 | ||
8488 | file->private_data = NULL; | |
8489 | io_ring_ctx_wait_and_kill(ctx); | |
8490 | return 0; | |
8491 | } | |
8492 | ||
67c4d9e6 PB |
8493 | static bool io_wq_files_match(struct io_wq_work *work, void *data) |
8494 | { | |
8495 | struct files_struct *files = data; | |
8496 | ||
0f212204 | 8497 | return !files || work->files == files; |
67c4d9e6 PB |
8498 | } |
8499 | ||
f254ac04 JA |
8500 | /* |
8501 | * Returns true if 'preq' is the link parent of 'req' | |
8502 | */ | |
8503 | static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req) | |
8504 | { | |
8505 | struct io_kiocb *link; | |
8506 | ||
8507 | if (!(preq->flags & REQ_F_LINK_HEAD)) | |
8508 | return false; | |
8509 | ||
8510 | list_for_each_entry(link, &preq->link_list, link_list) { | |
8511 | if (link == req) | |
8512 | return true; | |
8513 | } | |
8514 | ||
8515 | return false; | |
8516 | } | |
8517 | ||
c127a2a1 PB |
8518 | static bool io_match_link_files(struct io_kiocb *req, |
8519 | struct files_struct *files) | |
8520 | { | |
8521 | struct io_kiocb *link; | |
8522 | ||
8523 | if (io_match_files(req, files)) | |
8524 | return true; | |
8525 | if (req->flags & REQ_F_LINK_HEAD) { | |
8526 | list_for_each_entry(link, &req->link_list, link_list) { | |
8527 | if (io_match_files(link, files)) | |
8528 | return true; | |
8529 | } | |
8530 | } | |
8531 | return false; | |
8532 | } | |
8533 | ||
f254ac04 JA |
8534 | /* |
8535 | * We're looking to cancel 'req' because it's holding on to our files, but | |
8536 | * 'req' could be a link to another request. See if it is, and cancel that | |
8537 | * parent request if so. | |
8538 | */ | |
8539 | static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req) | |
8540 | { | |
8541 | struct hlist_node *tmp; | |
8542 | struct io_kiocb *preq; | |
8543 | bool found = false; | |
8544 | int i; | |
8545 | ||
8546 | spin_lock_irq(&ctx->completion_lock); | |
8547 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
8548 | struct hlist_head *list; | |
8549 | ||
8550 | list = &ctx->cancel_hash[i]; | |
8551 | hlist_for_each_entry_safe(preq, tmp, list, hash_node) { | |
8552 | found = io_match_link(preq, req); | |
8553 | if (found) { | |
8554 | io_poll_remove_one(preq); | |
8555 | break; | |
8556 | } | |
8557 | } | |
8558 | } | |
8559 | spin_unlock_irq(&ctx->completion_lock); | |
8560 | return found; | |
8561 | } | |
8562 | ||
8563 | static bool io_timeout_remove_link(struct io_ring_ctx *ctx, | |
8564 | struct io_kiocb *req) | |
8565 | { | |
8566 | struct io_kiocb *preq; | |
8567 | bool found = false; | |
8568 | ||
8569 | spin_lock_irq(&ctx->completion_lock); | |
8570 | list_for_each_entry(preq, &ctx->timeout_list, timeout.list) { | |
8571 | found = io_match_link(preq, req); | |
8572 | if (found) { | |
8573 | __io_timeout_cancel(preq); | |
8574 | break; | |
8575 | } | |
8576 | } | |
8577 | spin_unlock_irq(&ctx->completion_lock); | |
8578 | return found; | |
8579 | } | |
8580 | ||
b711d4ea JA |
8581 | static bool io_cancel_link_cb(struct io_wq_work *work, void *data) |
8582 | { | |
8583 | return io_match_link(container_of(work, struct io_kiocb, work), data); | |
8584 | } | |
8585 | ||
8586 | static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req) | |
8587 | { | |
8588 | enum io_wq_cancel cret; | |
8589 | ||
8590 | /* cancel this particular work, if it's running */ | |
8591 | cret = io_wq_cancel_work(ctx->io_wq, &req->work); | |
8592 | if (cret != IO_WQ_CANCEL_NOTFOUND) | |
8593 | return; | |
8594 | ||
8595 | /* find links that hold this pending, cancel those */ | |
8596 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true); | |
8597 | if (cret != IO_WQ_CANCEL_NOTFOUND) | |
8598 | return; | |
8599 | ||
8600 | /* if we have a poll link holding this pending, cancel that */ | |
8601 | if (io_poll_remove_link(ctx, req)) | |
8602 | return; | |
8603 | ||
8604 | /* final option, timeout link is holding this req pending */ | |
8605 | io_timeout_remove_link(ctx, req); | |
8606 | } | |
8607 | ||
b7ddce3c PB |
8608 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
8609 | struct files_struct *files) | |
8610 | { | |
8611 | struct io_defer_entry *de = NULL; | |
8612 | LIST_HEAD(list); | |
8613 | ||
8614 | spin_lock_irq(&ctx->completion_lock); | |
8615 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
c127a2a1 | 8616 | if (io_match_link_files(de->req, files)) { |
b7ddce3c PB |
8617 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8618 | break; | |
8619 | } | |
8620 | } | |
8621 | spin_unlock_irq(&ctx->completion_lock); | |
8622 | ||
8623 | while (!list_empty(&list)) { | |
8624 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8625 | list_del_init(&de->list); | |
8626 | req_set_fail_links(de->req); | |
8627 | io_put_req(de->req); | |
8628 | io_req_complete(de->req, -ECANCELED); | |
8629 | kfree(de); | |
8630 | } | |
8631 | } | |
8632 | ||
76e1b642 JA |
8633 | /* |
8634 | * Returns true if we found and killed one or more files pinning requests | |
8635 | */ | |
8636 | static bool io_uring_cancel_files(struct io_ring_ctx *ctx, | |
fcb323cc JA |
8637 | struct files_struct *files) |
8638 | { | |
67c4d9e6 | 8639 | if (list_empty_careful(&ctx->inflight_list)) |
76e1b642 | 8640 | return false; |
67c4d9e6 | 8641 | |
b7ddce3c | 8642 | io_cancel_defer_files(ctx, files); |
67c4d9e6 PB |
8643 | /* cancel all at once, should be faster than doing it one by one*/ |
8644 | io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true); | |
8645 | ||
fcb323cc | 8646 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 XW |
8647 | struct io_kiocb *cancel_req = NULL, *req; |
8648 | DEFINE_WAIT(wait); | |
fcb323cc JA |
8649 | |
8650 | spin_lock_irq(&ctx->inflight_lock); | |
8651 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
0f212204 | 8652 | if (files && req->work.files != files) |
768134d4 JA |
8653 | continue; |
8654 | /* req is being completed, ignore */ | |
8655 | if (!refcount_inc_not_zero(&req->refs)) | |
8656 | continue; | |
8657 | cancel_req = req; | |
8658 | break; | |
fcb323cc | 8659 | } |
768134d4 | 8660 | if (cancel_req) |
fcb323cc | 8661 | prepare_to_wait(&ctx->inflight_wait, &wait, |
768134d4 | 8662 | TASK_UNINTERRUPTIBLE); |
fcb323cc JA |
8663 | spin_unlock_irq(&ctx->inflight_lock); |
8664 | ||
768134d4 JA |
8665 | /* We need to keep going until we don't find a matching req */ |
8666 | if (!cancel_req) | |
fcb323cc | 8667 | break; |
bb175342 PB |
8668 | /* cancel this request, or head link requests */ |
8669 | io_attempt_cancel(ctx, cancel_req); | |
8670 | io_put_req(cancel_req); | |
6200b0ae JA |
8671 | /* cancellations _may_ trigger task work */ |
8672 | io_run_task_work(); | |
fcb323cc | 8673 | schedule(); |
d8f1b971 | 8674 | finish_wait(&ctx->inflight_wait, &wait); |
fcb323cc | 8675 | } |
76e1b642 JA |
8676 | |
8677 | return true; | |
fcb323cc JA |
8678 | } |
8679 | ||
801dd57b | 8680 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
44e728b8 | 8681 | { |
801dd57b PB |
8682 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
8683 | struct task_struct *task = data; | |
44e728b8 | 8684 | |
f3606e3a | 8685 | return io_task_match(req, task); |
44e728b8 PB |
8686 | } |
8687 | ||
0f212204 JA |
8688 | static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
8689 | struct task_struct *task, | |
8690 | struct files_struct *files) | |
8691 | { | |
8692 | bool ret; | |
8693 | ||
8694 | ret = io_uring_cancel_files(ctx, files); | |
8695 | if (!files) { | |
8696 | enum io_wq_cancel cret; | |
8697 | ||
8698 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true); | |
8699 | if (cret != IO_WQ_CANCEL_NOTFOUND) | |
8700 | ret = true; | |
8701 | ||
8702 | /* SQPOLL thread does its own polling */ | |
8703 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8704 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8705 | io_iopoll_try_reap_events(ctx); | |
8706 | ret = true; | |
8707 | } | |
8708 | } | |
8709 | ||
8710 | ret |= io_poll_remove_all(ctx, task); | |
8711 | ret |= io_kill_timeouts(ctx, task); | |
8712 | } | |
8713 | ||
8714 | return ret; | |
8715 | } | |
8716 | ||
8717 | /* | |
8718 | * We need to iteratively cancel requests, in case a request has dependent | |
8719 | * hard links. These persist even for failure of cancelations, hence keep | |
8720 | * looping until none are found. | |
8721 | */ | |
8722 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8723 | struct files_struct *files) | |
8724 | { | |
8725 | struct task_struct *task = current; | |
8726 | ||
534ca6d6 JA |
8727 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) |
8728 | task = ctx->sq_data->thread; | |
0f212204 JA |
8729 | |
8730 | io_cqring_overflow_flush(ctx, true, task, files); | |
8731 | ||
8732 | while (__io_uring_cancel_task_requests(ctx, task, files)) { | |
8733 | io_run_task_work(); | |
8734 | cond_resched(); | |
8735 | } | |
8736 | } | |
8737 | ||
8738 | /* | |
8739 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8740 | */ | |
8741 | static int io_uring_add_task_file(struct file *file) | |
8742 | { | |
8743 | if (unlikely(!current->io_uring)) { | |
8744 | int ret; | |
8745 | ||
8746 | ret = io_uring_alloc_task_context(current); | |
8747 | if (unlikely(ret)) | |
8748 | return ret; | |
8749 | } | |
8750 | if (current->io_uring->last != file) { | |
8751 | XA_STATE(xas, ¤t->io_uring->xa, (unsigned long) file); | |
8752 | void *old; | |
8753 | ||
8754 | rcu_read_lock(); | |
8755 | old = xas_load(&xas); | |
8756 | if (old != file) { | |
8757 | get_file(file); | |
8758 | xas_lock(&xas); | |
8759 | xas_store(&xas, file); | |
8760 | xas_unlock(&xas); | |
8761 | } | |
8762 | rcu_read_unlock(); | |
8763 | current->io_uring->last = file; | |
8764 | } | |
8765 | ||
8766 | return 0; | |
8767 | } | |
8768 | ||
8769 | /* | |
8770 | * Remove this io_uring_file -> task mapping. | |
8771 | */ | |
8772 | static void io_uring_del_task_file(struct file *file) | |
8773 | { | |
8774 | struct io_uring_task *tctx = current->io_uring; | |
8775 | XA_STATE(xas, &tctx->xa, (unsigned long) file); | |
8776 | ||
8777 | if (tctx->last == file) | |
8778 | tctx->last = NULL; | |
8779 | ||
8780 | xas_lock(&xas); | |
8781 | file = xas_store(&xas, NULL); | |
8782 | xas_unlock(&xas); | |
8783 | ||
8784 | if (file) | |
8785 | fput(file); | |
8786 | } | |
8787 | ||
8788 | static void __io_uring_attempt_task_drop(struct file *file) | |
8789 | { | |
8790 | XA_STATE(xas, ¤t->io_uring->xa, (unsigned long) file); | |
8791 | struct file *old; | |
8792 | ||
8793 | rcu_read_lock(); | |
8794 | old = xas_load(&xas); | |
8795 | rcu_read_unlock(); | |
8796 | ||
8797 | if (old == file) | |
8798 | io_uring_del_task_file(file); | |
8799 | } | |
8800 | ||
8801 | /* | |
8802 | * Drop task note for this file if we're the only ones that hold it after | |
8803 | * pending fput() | |
8804 | */ | |
8805 | static void io_uring_attempt_task_drop(struct file *file, bool exiting) | |
8806 | { | |
8807 | if (!current->io_uring) | |
8808 | return; | |
8809 | /* | |
8810 | * fput() is pending, will be 2 if the only other ref is our potential | |
8811 | * task file note. If the task is exiting, drop regardless of count. | |
8812 | */ | |
8813 | if (!exiting && atomic_long_read(&file->f_count) != 2) | |
8814 | return; | |
8815 | ||
8816 | __io_uring_attempt_task_drop(file); | |
8817 | } | |
8818 | ||
8819 | void __io_uring_files_cancel(struct files_struct *files) | |
8820 | { | |
8821 | struct io_uring_task *tctx = current->io_uring; | |
8822 | XA_STATE(xas, &tctx->xa, 0); | |
8823 | ||
8824 | /* make sure overflow events are dropped */ | |
8825 | tctx->in_idle = true; | |
8826 | ||
8827 | do { | |
8828 | struct io_ring_ctx *ctx; | |
8829 | struct file *file; | |
8830 | ||
8831 | xas_lock(&xas); | |
8832 | file = xas_next_entry(&xas, ULONG_MAX); | |
8833 | xas_unlock(&xas); | |
8834 | ||
8835 | if (!file) | |
8836 | break; | |
8837 | ||
8838 | ctx = file->private_data; | |
8839 | ||
8840 | io_uring_cancel_task_requests(ctx, files); | |
8841 | if (files) | |
8842 | io_uring_del_task_file(file); | |
8843 | } while (1); | |
8844 | } | |
8845 | ||
8846 | static inline bool io_uring_task_idle(struct io_uring_task *tctx) | |
8847 | { | |
8848 | return atomic_long_read(&tctx->req_issue) == | |
8849 | atomic_long_read(&tctx->req_complete); | |
8850 | } | |
8851 | ||
8852 | /* | |
8853 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8854 | * requests. | |
8855 | */ | |
8856 | void __io_uring_task_cancel(void) | |
8857 | { | |
8858 | struct io_uring_task *tctx = current->io_uring; | |
8859 | DEFINE_WAIT(wait); | |
8860 | long completions; | |
8861 | ||
8862 | /* make sure overflow events are dropped */ | |
8863 | tctx->in_idle = true; | |
8864 | ||
8865 | while (!io_uring_task_idle(tctx)) { | |
8866 | /* read completions before cancelations */ | |
8867 | completions = atomic_long_read(&tctx->req_complete); | |
8868 | __io_uring_files_cancel(NULL); | |
8869 | ||
8870 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8871 | ||
8872 | /* | |
8873 | * If we've seen completions, retry. This avoids a race where | |
8874 | * a completion comes in before we did prepare_to_wait(). | |
8875 | */ | |
8876 | if (completions != atomic_long_read(&tctx->req_complete)) | |
8877 | continue; | |
8878 | if (io_uring_task_idle(tctx)) | |
8879 | break; | |
8880 | schedule(); | |
8881 | } | |
8882 | ||
8883 | finish_wait(&tctx->wait, &wait); | |
8884 | tctx->in_idle = false; | |
8885 | } | |
8886 | ||
fcb323cc JA |
8887 | static int io_uring_flush(struct file *file, void *data) |
8888 | { | |
8889 | struct io_ring_ctx *ctx = file->private_data; | |
8890 | ||
6ab23144 JA |
8891 | /* |
8892 | * If the task is going away, cancel work it may have pending | |
8893 | */ | |
801dd57b | 8894 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
0f212204 | 8895 | data = NULL; |
6ab23144 | 8896 | |
0f212204 JA |
8897 | io_uring_cancel_task_requests(ctx, data); |
8898 | io_uring_attempt_task_drop(file, !data); | |
fcb323cc JA |
8899 | return 0; |
8900 | } | |
8901 | ||
6c5c240e RP |
8902 | static void *io_uring_validate_mmap_request(struct file *file, |
8903 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8904 | { |
2b188cc1 | 8905 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8906 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8907 | struct page *page; |
8908 | void *ptr; | |
8909 | ||
8910 | switch (offset) { | |
8911 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8912 | case IORING_OFF_CQ_RING: |
8913 | ptr = ctx->rings; | |
2b188cc1 JA |
8914 | break; |
8915 | case IORING_OFF_SQES: | |
8916 | ptr = ctx->sq_sqes; | |
8917 | break; | |
2b188cc1 | 8918 | default: |
6c5c240e | 8919 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8920 | } |
8921 | ||
8922 | page = virt_to_head_page(ptr); | |
a50b854e | 8923 | if (sz > page_size(page)) |
6c5c240e RP |
8924 | return ERR_PTR(-EINVAL); |
8925 | ||
8926 | return ptr; | |
8927 | } | |
8928 | ||
8929 | #ifdef CONFIG_MMU | |
8930 | ||
8931 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8932 | { | |
8933 | size_t sz = vma->vm_end - vma->vm_start; | |
8934 | unsigned long pfn; | |
8935 | void *ptr; | |
8936 | ||
8937 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8938 | if (IS_ERR(ptr)) | |
8939 | return PTR_ERR(ptr); | |
2b188cc1 JA |
8940 | |
8941 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
8942 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
8943 | } | |
8944 | ||
6c5c240e RP |
8945 | #else /* !CONFIG_MMU */ |
8946 | ||
8947 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8948 | { | |
8949 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
8950 | } | |
8951 | ||
8952 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
8953 | { | |
8954 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
8955 | } | |
8956 | ||
8957 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
8958 | unsigned long addr, unsigned long len, | |
8959 | unsigned long pgoff, unsigned long flags) | |
8960 | { | |
8961 | void *ptr; | |
8962 | ||
8963 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
8964 | if (IS_ERR(ptr)) | |
8965 | return PTR_ERR(ptr); | |
8966 | ||
8967 | return (unsigned long) ptr; | |
8968 | } | |
8969 | ||
8970 | #endif /* !CONFIG_MMU */ | |
8971 | ||
90554200 JA |
8972 | static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
8973 | { | |
8974 | DEFINE_WAIT(wait); | |
8975 | ||
8976 | do { | |
8977 | if (!io_sqring_full(ctx)) | |
8978 | break; | |
8979 | ||
8980 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
8981 | ||
8982 | if (!io_sqring_full(ctx)) | |
8983 | break; | |
8984 | ||
8985 | schedule(); | |
8986 | } while (!signal_pending(current)); | |
8987 | ||
8988 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
8989 | } | |
8990 | ||
2b188cc1 JA |
8991 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
8992 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
8993 | size_t, sigsz) | |
8994 | { | |
8995 | struct io_ring_ctx *ctx; | |
8996 | long ret = -EBADF; | |
8997 | int submitted = 0; | |
8998 | struct fd f; | |
8999 | ||
4c6e277c | 9000 | io_run_task_work(); |
b41e9852 | 9001 | |
90554200 JA |
9002 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9003 | IORING_ENTER_SQ_WAIT)) | |
2b188cc1 JA |
9004 | return -EINVAL; |
9005 | ||
9006 | f = fdget(fd); | |
9007 | if (!f.file) | |
9008 | return -EBADF; | |
9009 | ||
9010 | ret = -EOPNOTSUPP; | |
9011 | if (f.file->f_op != &io_uring_fops) | |
9012 | goto out_fput; | |
9013 | ||
9014 | ret = -ENXIO; | |
9015 | ctx = f.file->private_data; | |
9016 | if (!percpu_ref_tryget(&ctx->refs)) | |
9017 | goto out_fput; | |
9018 | ||
7e84e1c7 SG |
9019 | ret = -EBADFD; |
9020 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9021 | goto out; | |
9022 | ||
6c271ce2 JA |
9023 | /* |
9024 | * For SQ polling, the thread will do all submissions and completions. | |
9025 | * Just return the requested submit count, and wake the thread if | |
9026 | * we were asked to. | |
9027 | */ | |
b2a9eada | 9028 | ret = 0; |
6c271ce2 | 9029 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c1edbf5f | 9030 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
e6c8aa9a | 9031 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6c271ce2 | 9032 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9033 | wake_up(&ctx->sq_data->wait); |
90554200 JA |
9034 | if (flags & IORING_ENTER_SQ_WAIT) |
9035 | io_sqpoll_wait_sq(ctx); | |
6c271ce2 | 9036 | submitted = to_submit; |
b2a9eada | 9037 | } else if (to_submit) { |
0f212204 JA |
9038 | ret = io_uring_add_task_file(f.file); |
9039 | if (unlikely(ret)) | |
9040 | goto out; | |
2b188cc1 | 9041 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9042 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9043 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9044 | |
9045 | if (submitted != to_submit) | |
9046 | goto out; | |
2b188cc1 JA |
9047 | } |
9048 | if (flags & IORING_ENTER_GETEVENTS) { | |
9049 | min_complete = min(min_complete, ctx->cq_entries); | |
9050 | ||
32b2244a XW |
9051 | /* |
9052 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9053 | * space applications don't need to do io completion events | |
9054 | * polling again, they can rely on io_sq_thread to do polling | |
9055 | * work, which can reduce cpu usage and uring_lock contention. | |
9056 | */ | |
9057 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9058 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9059 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 JA |
9060 | } else { |
9061 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
9062 | } | |
2b188cc1 JA |
9063 | } |
9064 | ||
7c504e65 | 9065 | out: |
6805b32e | 9066 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9067 | out_fput: |
9068 | fdput(f); | |
9069 | return submitted ? submitted : ret; | |
9070 | } | |
9071 | ||
bebdb65e | 9072 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9073 | static int io_uring_show_cred(int id, void *p, void *data) |
9074 | { | |
9075 | const struct cred *cred = p; | |
9076 | struct seq_file *m = data; | |
9077 | struct user_namespace *uns = seq_user_ns(m); | |
9078 | struct group_info *gi; | |
9079 | kernel_cap_t cap; | |
9080 | unsigned __capi; | |
9081 | int g; | |
9082 | ||
9083 | seq_printf(m, "%5d\n", id); | |
9084 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9085 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9086 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9087 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9088 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9089 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9090 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9091 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9092 | seq_puts(m, "\n\tGroups:\t"); | |
9093 | gi = cred->group_info; | |
9094 | for (g = 0; g < gi->ngroups; g++) { | |
9095 | seq_put_decimal_ull(m, g ? " " : "", | |
9096 | from_kgid_munged(uns, gi->gid[g])); | |
9097 | } | |
9098 | seq_puts(m, "\n\tCapEff:\t"); | |
9099 | cap = cred->cap_effective; | |
9100 | CAP_FOR_EACH_U32(__capi) | |
9101 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9102 | seq_putc(m, '\n'); | |
9103 | return 0; | |
9104 | } | |
9105 | ||
9106 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9107 | { | |
dbbe9c64 | 9108 | struct io_sq_data *sq = NULL; |
fad8e0de | 9109 | bool has_lock; |
87ce955b JA |
9110 | int i; |
9111 | ||
fad8e0de JA |
9112 | /* |
9113 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9114 | * since fdinfo case grabs it in the opposite direction of normal use | |
9115 | * cases. If we fail to get the lock, we just don't iterate any | |
9116 | * structures that could be going away outside the io_uring mutex. | |
9117 | */ | |
9118 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9119 | ||
dbbe9c64 JQ |
9120 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9121 | sq = ctx->sq_data; | |
9122 | ||
9123 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9124 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9125 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9126 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
87ce955b JA |
9127 | struct fixed_file_table *table; |
9128 | struct file *f; | |
9129 | ||
9130 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
9131 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
9132 | if (f) | |
9133 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9134 | else | |
9135 | seq_printf(m, "%5u: <none>\n", i); | |
9136 | } | |
9137 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9138 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9139 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9140 | ||
9141 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9142 | (unsigned int) buf->len); | |
9143 | } | |
fad8e0de | 9144 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9145 | seq_printf(m, "Personalities:\n"); |
9146 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9147 | } | |
d7718a9d JA |
9148 | seq_printf(m, "PollList:\n"); |
9149 | spin_lock_irq(&ctx->completion_lock); | |
9150 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9151 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9152 | struct io_kiocb *req; | |
9153 | ||
9154 | hlist_for_each_entry(req, list, hash_node) | |
9155 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9156 | req->task->task_works != NULL); | |
9157 | } | |
9158 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9159 | if (has_lock) |
9160 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9161 | } |
9162 | ||
9163 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9164 | { | |
9165 | struct io_ring_ctx *ctx = f->private_data; | |
9166 | ||
9167 | if (percpu_ref_tryget(&ctx->refs)) { | |
9168 | __io_uring_show_fdinfo(ctx, m); | |
9169 | percpu_ref_put(&ctx->refs); | |
9170 | } | |
9171 | } | |
bebdb65e | 9172 | #endif |
87ce955b | 9173 | |
2b188cc1 JA |
9174 | static const struct file_operations io_uring_fops = { |
9175 | .release = io_uring_release, | |
fcb323cc | 9176 | .flush = io_uring_flush, |
2b188cc1 | 9177 | .mmap = io_uring_mmap, |
6c5c240e RP |
9178 | #ifndef CONFIG_MMU |
9179 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9180 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9181 | #endif | |
2b188cc1 JA |
9182 | .poll = io_uring_poll, |
9183 | .fasync = io_uring_fasync, | |
bebdb65e | 9184 | #ifdef CONFIG_PROC_FS |
87ce955b | 9185 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9186 | #endif |
2b188cc1 JA |
9187 | }; |
9188 | ||
9189 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9190 | struct io_uring_params *p) | |
9191 | { | |
75b28aff HV |
9192 | struct io_rings *rings; |
9193 | size_t size, sq_array_offset; | |
2b188cc1 | 9194 | |
bd740481 JA |
9195 | /* make sure these are sane, as we already accounted them */ |
9196 | ctx->sq_entries = p->sq_entries; | |
9197 | ctx->cq_entries = p->cq_entries; | |
9198 | ||
75b28aff HV |
9199 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9200 | if (size == SIZE_MAX) | |
9201 | return -EOVERFLOW; | |
9202 | ||
9203 | rings = io_mem_alloc(size); | |
9204 | if (!rings) | |
2b188cc1 JA |
9205 | return -ENOMEM; |
9206 | ||
75b28aff HV |
9207 | ctx->rings = rings; |
9208 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9209 | rings->sq_ring_mask = p->sq_entries - 1; | |
9210 | rings->cq_ring_mask = p->cq_entries - 1; | |
9211 | rings->sq_ring_entries = p->sq_entries; | |
9212 | rings->cq_ring_entries = p->cq_entries; | |
9213 | ctx->sq_mask = rings->sq_ring_mask; | |
9214 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9215 | |
9216 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9217 | if (size == SIZE_MAX) { |
9218 | io_mem_free(ctx->rings); | |
9219 | ctx->rings = NULL; | |
2b188cc1 | 9220 | return -EOVERFLOW; |
eb065d30 | 9221 | } |
2b188cc1 JA |
9222 | |
9223 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9224 | if (!ctx->sq_sqes) { |
9225 | io_mem_free(ctx->rings); | |
9226 | ctx->rings = NULL; | |
2b188cc1 | 9227 | return -ENOMEM; |
eb065d30 | 9228 | } |
2b188cc1 | 9229 | |
2b188cc1 JA |
9230 | return 0; |
9231 | } | |
9232 | ||
9233 | /* | |
9234 | * Allocate an anonymous fd, this is what constitutes the application | |
9235 | * visible backing of an io_uring instance. The application mmaps this | |
9236 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9237 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9238 | */ | |
9239 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
9240 | { | |
9241 | struct file *file; | |
9242 | int ret; | |
9243 | ||
9244 | #if defined(CONFIG_UNIX) | |
9245 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
9246 | &ctx->ring_sock); | |
9247 | if (ret) | |
9248 | return ret; | |
9249 | #endif | |
9250 | ||
9251 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9252 | if (ret < 0) | |
9253 | goto err; | |
9254 | ||
9255 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
9256 | O_RDWR | O_CLOEXEC); | |
9257 | if (IS_ERR(file)) { | |
0f212204 | 9258 | err_fd: |
2b188cc1 JA |
9259 | put_unused_fd(ret); |
9260 | ret = PTR_ERR(file); | |
9261 | goto err; | |
9262 | } | |
9263 | ||
9264 | #if defined(CONFIG_UNIX) | |
9265 | ctx->ring_sock->file = file; | |
9266 | #endif | |
0f212204 JA |
9267 | if (unlikely(io_uring_add_task_file(file))) { |
9268 | file = ERR_PTR(-ENOMEM); | |
9269 | goto err_fd; | |
9270 | } | |
2b188cc1 JA |
9271 | fd_install(ret, file); |
9272 | return ret; | |
9273 | err: | |
9274 | #if defined(CONFIG_UNIX) | |
9275 | sock_release(ctx->ring_sock); | |
9276 | ctx->ring_sock = NULL; | |
9277 | #endif | |
9278 | return ret; | |
9279 | } | |
9280 | ||
7f13657d XW |
9281 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9282 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9283 | { |
9284 | struct user_struct *user = NULL; | |
9285 | struct io_ring_ctx *ctx; | |
aad5d8da | 9286 | bool limit_mem; |
2b188cc1 JA |
9287 | int ret; |
9288 | ||
8110c1a6 | 9289 | if (!entries) |
2b188cc1 | 9290 | return -EINVAL; |
8110c1a6 JA |
9291 | if (entries > IORING_MAX_ENTRIES) { |
9292 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9293 | return -EINVAL; | |
9294 | entries = IORING_MAX_ENTRIES; | |
9295 | } | |
2b188cc1 JA |
9296 | |
9297 | /* | |
9298 | * Use twice as many entries for the CQ ring. It's possible for the | |
9299 | * application to drive a higher depth than the size of the SQ ring, | |
9300 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9301 | * some flexibility in overcommitting a bit. If the application has |
9302 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9303 | * of CQ ring entries manually. | |
2b188cc1 JA |
9304 | */ |
9305 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9306 | if (p->flags & IORING_SETUP_CQSIZE) { |
9307 | /* | |
9308 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9309 | * to a power-of-two, if it isn't already. We do NOT impose | |
9310 | * any cq vs sq ring sizing. | |
9311 | */ | |
8110c1a6 | 9312 | if (p->cq_entries < p->sq_entries) |
33a107f0 | 9313 | return -EINVAL; |
8110c1a6 JA |
9314 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9315 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9316 | return -EINVAL; | |
9317 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9318 | } | |
33a107f0 JA |
9319 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9320 | } else { | |
9321 | p->cq_entries = 2 * p->sq_entries; | |
9322 | } | |
2b188cc1 JA |
9323 | |
9324 | user = get_uid(current_user()); | |
aad5d8da | 9325 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9326 | |
aad5d8da | 9327 | if (limit_mem) { |
a087e2b5 | 9328 | ret = __io_account_mem(user, |
2b188cc1 JA |
9329 | ring_pages(p->sq_entries, p->cq_entries)); |
9330 | if (ret) { | |
9331 | free_uid(user); | |
9332 | return ret; | |
9333 | } | |
9334 | } | |
9335 | ||
9336 | ctx = io_ring_ctx_alloc(p); | |
9337 | if (!ctx) { | |
aad5d8da | 9338 | if (limit_mem) |
a087e2b5 | 9339 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9340 | p->cq_entries)); |
9341 | free_uid(user); | |
9342 | return -ENOMEM; | |
9343 | } | |
9344 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9345 | ctx->user = user; |
0b8c0ec7 | 9346 | ctx->creds = get_current_cred(); |
2b188cc1 | 9347 | |
2aede0e4 JA |
9348 | ctx->sqo_task = get_task_struct(current); |
9349 | ||
9350 | /* | |
9351 | * This is just grabbed for accounting purposes. When a process exits, | |
9352 | * the mm is exited and dropped before the files, hence we need to hang | |
9353 | * on to this mm purely for the purposes of being able to unaccount | |
9354 | * memory (locked/pinned vm). It's not used for anything else. | |
9355 | */ | |
6b7898eb | 9356 | mmgrab(current->mm); |
2aede0e4 | 9357 | ctx->mm_account = current->mm; |
6b7898eb | 9358 | |
91d8f519 DZ |
9359 | #ifdef CONFIG_BLK_CGROUP |
9360 | /* | |
9361 | * The sq thread will belong to the original cgroup it was inited in. | |
9362 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9363 | * issued bios will be associated with the closest cgroup later in the | |
9364 | * block layer. | |
9365 | */ | |
9366 | rcu_read_lock(); | |
9367 | ctx->sqo_blkcg_css = blkcg_css(); | |
9368 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9369 | rcu_read_unlock(); | |
9370 | if (!ret) { | |
9371 | /* don't init against a dying cgroup, have the user try again */ | |
9372 | ctx->sqo_blkcg_css = NULL; | |
9373 | ret = -ENODEV; | |
9374 | goto err; | |
9375 | } | |
9376 | #endif | |
9377 | ||
f74441e6 JA |
9378 | /* |
9379 | * Account memory _before_ installing the file descriptor. Once | |
9380 | * the descriptor is installed, it can get closed at any time. Also | |
9381 | * do this before hitting the general error path, as ring freeing | |
9382 | * will un-account as well. | |
9383 | */ | |
9384 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9385 | ACCT_LOCKED); | |
9386 | ctx->limit_mem = limit_mem; | |
9387 | ||
2b188cc1 JA |
9388 | ret = io_allocate_scq_urings(ctx, p); |
9389 | if (ret) | |
9390 | goto err; | |
9391 | ||
7e84e1c7 | 9392 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9393 | if (ret) |
9394 | goto err; | |
9395 | ||
7e84e1c7 SG |
9396 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9397 | io_sq_offload_start(ctx); | |
9398 | ||
2b188cc1 | 9399 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9400 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9401 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9402 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9403 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9404 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9405 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9406 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9407 | |
9408 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9409 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9410 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9411 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9412 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9413 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9414 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9415 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9416 | |
7f13657d XW |
9417 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9418 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 JX |
9419 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
9420 | IORING_FEAT_POLL_32BITS; | |
7f13657d XW |
9421 | |
9422 | if (copy_to_user(params, p, sizeof(*p))) { | |
9423 | ret = -EFAULT; | |
9424 | goto err; | |
9425 | } | |
d1719f70 | 9426 | |
044c1ab3 JA |
9427 | /* |
9428 | * Install ring fd as the very last thing, so we don't risk someone | |
9429 | * having closed it before we finish setup | |
9430 | */ | |
9431 | ret = io_uring_get_fd(ctx); | |
9432 | if (ret < 0) | |
9433 | goto err; | |
9434 | ||
c826bd7a | 9435 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9436 | return ret; |
9437 | err: | |
9438 | io_ring_ctx_wait_and_kill(ctx); | |
9439 | return ret; | |
9440 | } | |
9441 | ||
9442 | /* | |
9443 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9444 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9445 | * params structure passed in. | |
9446 | */ | |
9447 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9448 | { | |
9449 | struct io_uring_params p; | |
2b188cc1 JA |
9450 | int i; |
9451 | ||
9452 | if (copy_from_user(&p, params, sizeof(p))) | |
9453 | return -EFAULT; | |
9454 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9455 | if (p.resv[i]) | |
9456 | return -EINVAL; | |
9457 | } | |
9458 | ||
6c271ce2 | 9459 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9460 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9461 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9462 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9463 | return -EINVAL; |
9464 | ||
7f13657d | 9465 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9466 | } |
9467 | ||
9468 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9469 | struct io_uring_params __user *, params) | |
9470 | { | |
9471 | return io_uring_setup(entries, params); | |
9472 | } | |
9473 | ||
66f4af93 JA |
9474 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9475 | { | |
9476 | struct io_uring_probe *p; | |
9477 | size_t size; | |
9478 | int i, ret; | |
9479 | ||
9480 | size = struct_size(p, ops, nr_args); | |
9481 | if (size == SIZE_MAX) | |
9482 | return -EOVERFLOW; | |
9483 | p = kzalloc(size, GFP_KERNEL); | |
9484 | if (!p) | |
9485 | return -ENOMEM; | |
9486 | ||
9487 | ret = -EFAULT; | |
9488 | if (copy_from_user(p, arg, size)) | |
9489 | goto out; | |
9490 | ret = -EINVAL; | |
9491 | if (memchr_inv(p, 0, size)) | |
9492 | goto out; | |
9493 | ||
9494 | p->last_op = IORING_OP_LAST - 1; | |
9495 | if (nr_args > IORING_OP_LAST) | |
9496 | nr_args = IORING_OP_LAST; | |
9497 | ||
9498 | for (i = 0; i < nr_args; i++) { | |
9499 | p->ops[i].op = i; | |
9500 | if (!io_op_defs[i].not_supported) | |
9501 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9502 | } | |
9503 | p->ops_len = i; | |
9504 | ||
9505 | ret = 0; | |
9506 | if (copy_to_user(arg, p, size)) | |
9507 | ret = -EFAULT; | |
9508 | out: | |
9509 | kfree(p); | |
9510 | return ret; | |
9511 | } | |
9512 | ||
071698e1 JA |
9513 | static int io_register_personality(struct io_ring_ctx *ctx) |
9514 | { | |
9515 | const struct cred *creds = get_current_cred(); | |
9516 | int id; | |
9517 | ||
9518 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, | |
9519 | USHRT_MAX, GFP_KERNEL); | |
9520 | if (id < 0) | |
9521 | put_cred(creds); | |
9522 | return id; | |
9523 | } | |
9524 | ||
9525 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
9526 | { | |
9527 | const struct cred *old_creds; | |
9528 | ||
9529 | old_creds = idr_remove(&ctx->personality_idr, id); | |
9530 | if (old_creds) { | |
9531 | put_cred(old_creds); | |
9532 | return 0; | |
9533 | } | |
9534 | ||
9535 | return -EINVAL; | |
9536 | } | |
9537 | ||
21b55dbc SG |
9538 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9539 | unsigned int nr_args) | |
9540 | { | |
9541 | struct io_uring_restriction *res; | |
9542 | size_t size; | |
9543 | int i, ret; | |
9544 | ||
7e84e1c7 SG |
9545 | /* Restrictions allowed only if rings started disabled */ |
9546 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9547 | return -EBADFD; | |
9548 | ||
21b55dbc | 9549 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9550 | if (ctx->restrictions.registered) |
21b55dbc SG |
9551 | return -EBUSY; |
9552 | ||
9553 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9554 | return -EINVAL; | |
9555 | ||
9556 | size = array_size(nr_args, sizeof(*res)); | |
9557 | if (size == SIZE_MAX) | |
9558 | return -EOVERFLOW; | |
9559 | ||
9560 | res = memdup_user(arg, size); | |
9561 | if (IS_ERR(res)) | |
9562 | return PTR_ERR(res); | |
9563 | ||
9564 | ret = 0; | |
9565 | ||
9566 | for (i = 0; i < nr_args; i++) { | |
9567 | switch (res[i].opcode) { | |
9568 | case IORING_RESTRICTION_REGISTER_OP: | |
9569 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9570 | ret = -EINVAL; | |
9571 | goto out; | |
9572 | } | |
9573 | ||
9574 | __set_bit(res[i].register_op, | |
9575 | ctx->restrictions.register_op); | |
9576 | break; | |
9577 | case IORING_RESTRICTION_SQE_OP: | |
9578 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9579 | ret = -EINVAL; | |
9580 | goto out; | |
9581 | } | |
9582 | ||
9583 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9584 | break; | |
9585 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9586 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9587 | break; | |
9588 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9589 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9590 | break; | |
9591 | default: | |
9592 | ret = -EINVAL; | |
9593 | goto out; | |
9594 | } | |
9595 | } | |
9596 | ||
9597 | out: | |
9598 | /* Reset all restrictions if an error happened */ | |
9599 | if (ret != 0) | |
9600 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9601 | else | |
7e84e1c7 | 9602 | ctx->restrictions.registered = true; |
21b55dbc SG |
9603 | |
9604 | kfree(res); | |
9605 | return ret; | |
9606 | } | |
9607 | ||
7e84e1c7 SG |
9608 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9609 | { | |
9610 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9611 | return -EBADFD; | |
9612 | ||
9613 | if (ctx->restrictions.registered) | |
9614 | ctx->restricted = 1; | |
9615 | ||
9616 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9617 | ||
9618 | io_sq_offload_start(ctx); | |
9619 | ||
9620 | return 0; | |
9621 | } | |
9622 | ||
071698e1 JA |
9623 | static bool io_register_op_must_quiesce(int op) |
9624 | { | |
9625 | switch (op) { | |
9626 | case IORING_UNREGISTER_FILES: | |
9627 | case IORING_REGISTER_FILES_UPDATE: | |
9628 | case IORING_REGISTER_PROBE: | |
9629 | case IORING_REGISTER_PERSONALITY: | |
9630 | case IORING_UNREGISTER_PERSONALITY: | |
9631 | return false; | |
9632 | default: | |
9633 | return true; | |
9634 | } | |
9635 | } | |
9636 | ||
edafccee JA |
9637 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9638 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9639 | __releases(ctx->uring_lock) |
9640 | __acquires(ctx->uring_lock) | |
edafccee JA |
9641 | { |
9642 | int ret; | |
9643 | ||
35fa71a0 JA |
9644 | /* |
9645 | * We're inside the ring mutex, if the ref is already dying, then | |
9646 | * someone else killed the ctx or is already going through | |
9647 | * io_uring_register(). | |
9648 | */ | |
9649 | if (percpu_ref_is_dying(&ctx->refs)) | |
9650 | return -ENXIO; | |
9651 | ||
071698e1 | 9652 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9653 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9654 | |
05f3fb3c JA |
9655 | /* |
9656 | * Drop uring mutex before waiting for references to exit. If | |
9657 | * another thread is currently inside io_uring_enter() it might | |
9658 | * need to grab the uring_lock to make progress. If we hold it | |
9659 | * here across the drain wait, then we can deadlock. It's safe | |
9660 | * to drop the mutex here, since no new references will come in | |
9661 | * after we've killed the percpu ref. | |
9662 | */ | |
9663 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9664 | do { |
9665 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9666 | if (!ret) | |
9667 | break; | |
9668 | if (io_run_task_work_sig() > 0) | |
9669 | continue; | |
9670 | } while (1); | |
9671 | ||
05f3fb3c | 9672 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9673 | |
c150368b JA |
9674 | if (ret) { |
9675 | percpu_ref_resurrect(&ctx->refs); | |
9676 | ret = -EINTR; | |
21b55dbc SG |
9677 | goto out_quiesce; |
9678 | } | |
9679 | } | |
9680 | ||
9681 | if (ctx->restricted) { | |
9682 | if (opcode >= IORING_REGISTER_LAST) { | |
9683 | ret = -EINVAL; | |
9684 | goto out; | |
9685 | } | |
9686 | ||
9687 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9688 | ret = -EACCES; | |
c150368b JA |
9689 | goto out; |
9690 | } | |
05f3fb3c | 9691 | } |
edafccee JA |
9692 | |
9693 | switch (opcode) { | |
9694 | case IORING_REGISTER_BUFFERS: | |
9695 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
9696 | break; | |
9697 | case IORING_UNREGISTER_BUFFERS: | |
9698 | ret = -EINVAL; | |
9699 | if (arg || nr_args) | |
9700 | break; | |
9701 | ret = io_sqe_buffer_unregister(ctx); | |
9702 | break; | |
6b06314c JA |
9703 | case IORING_REGISTER_FILES: |
9704 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9705 | break; | |
9706 | case IORING_UNREGISTER_FILES: | |
9707 | ret = -EINVAL; | |
9708 | if (arg || nr_args) | |
9709 | break; | |
9710 | ret = io_sqe_files_unregister(ctx); | |
9711 | break; | |
c3a31e60 JA |
9712 | case IORING_REGISTER_FILES_UPDATE: |
9713 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9714 | break; | |
9b402849 | 9715 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9716 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9717 | ret = -EINVAL; |
9718 | if (nr_args != 1) | |
9719 | break; | |
9720 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9721 | if (ret) |
9722 | break; | |
9723 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9724 | ctx->eventfd_async = 1; | |
9725 | else | |
9726 | ctx->eventfd_async = 0; | |
9b402849 JA |
9727 | break; |
9728 | case IORING_UNREGISTER_EVENTFD: | |
9729 | ret = -EINVAL; | |
9730 | if (arg || nr_args) | |
9731 | break; | |
9732 | ret = io_eventfd_unregister(ctx); | |
9733 | break; | |
66f4af93 JA |
9734 | case IORING_REGISTER_PROBE: |
9735 | ret = -EINVAL; | |
9736 | if (!arg || nr_args > 256) | |
9737 | break; | |
9738 | ret = io_probe(ctx, arg, nr_args); | |
9739 | break; | |
071698e1 JA |
9740 | case IORING_REGISTER_PERSONALITY: |
9741 | ret = -EINVAL; | |
9742 | if (arg || nr_args) | |
9743 | break; | |
9744 | ret = io_register_personality(ctx); | |
9745 | break; | |
9746 | case IORING_UNREGISTER_PERSONALITY: | |
9747 | ret = -EINVAL; | |
9748 | if (arg) | |
9749 | break; | |
9750 | ret = io_unregister_personality(ctx, nr_args); | |
9751 | break; | |
7e84e1c7 SG |
9752 | case IORING_REGISTER_ENABLE_RINGS: |
9753 | ret = -EINVAL; | |
9754 | if (arg || nr_args) | |
9755 | break; | |
9756 | ret = io_register_enable_rings(ctx); | |
9757 | break; | |
21b55dbc SG |
9758 | case IORING_REGISTER_RESTRICTIONS: |
9759 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9760 | break; | |
edafccee JA |
9761 | default: |
9762 | ret = -EINVAL; | |
9763 | break; | |
9764 | } | |
9765 | ||
21b55dbc | 9766 | out: |
071698e1 | 9767 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9768 | /* bring the ctx back to life */ |
05f3fb3c | 9769 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 9770 | out_quiesce: |
0f158b4c | 9771 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9772 | } |
edafccee JA |
9773 | return ret; |
9774 | } | |
9775 | ||
9776 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9777 | void __user *, arg, unsigned int, nr_args) | |
9778 | { | |
9779 | struct io_ring_ctx *ctx; | |
9780 | long ret = -EBADF; | |
9781 | struct fd f; | |
9782 | ||
9783 | f = fdget(fd); | |
9784 | if (!f.file) | |
9785 | return -EBADF; | |
9786 | ||
9787 | ret = -EOPNOTSUPP; | |
9788 | if (f.file->f_op != &io_uring_fops) | |
9789 | goto out_fput; | |
9790 | ||
9791 | ctx = f.file->private_data; | |
9792 | ||
9793 | mutex_lock(&ctx->uring_lock); | |
9794 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9795 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9796 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9797 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9798 | out_fput: |
9799 | fdput(f); | |
9800 | return ret; | |
9801 | } | |
9802 | ||
2b188cc1 JA |
9803 | static int __init io_uring_init(void) |
9804 | { | |
d7f62e82 SM |
9805 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9806 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9807 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9808 | } while (0) | |
9809 | ||
9810 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9811 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9812 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9813 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9814 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9815 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9816 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9817 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9818 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9819 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9820 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9821 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9822 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9823 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9824 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9825 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9826 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9827 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9828 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9829 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9830 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9831 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9832 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9833 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9834 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9835 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9836 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9837 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9838 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9839 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9840 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9841 | |
d3656344 | 9842 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9843 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
9844 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
9845 | return 0; | |
9846 | }; | |
9847 | __initcall(io_uring_init); |