]>
Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
14 | * through a control-dependency in io_get_cqring (smp_store_release to | |
15 | * store head will do). Failure to do so could lead to reading invalid | |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
6c271ce2 | 60 | #include <linux/kthread.h> |
2b188cc1 | 61 | #include <linux/blkdev.h> |
edafccee | 62 | #include <linux/bvec.h> |
2b188cc1 JA |
63 | #include <linux/net.h> |
64 | #include <net/sock.h> | |
65 | #include <net/af_unix.h> | |
6b06314c | 66 | #include <net/scm.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
ff002b30 | 78 | #include <linux/fs_struct.h> |
7d67af2c | 79 | #include <linux/splice.h> |
b41e9852 | 80 | #include <linux/task_work.h> |
bcf5a063 | 81 | #include <linux/pagemap.h> |
0f212204 | 82 | #include <linux/io_uring.h> |
91d8f519 | 83 | #include <linux/blk-cgroup.h> |
4ea33a97 | 84 | #include <linux/audit.h> |
2b188cc1 | 85 | |
c826bd7a DD |
86 | #define CREATE_TRACE_POINTS |
87 | #include <trace/events/io_uring.h> | |
88 | ||
2b188cc1 JA |
89 | #include <uapi/linux/io_uring.h> |
90 | ||
91 | #include "internal.h" | |
561fb04a | 92 | #include "io-wq.h" |
2b188cc1 | 93 | |
5277deaa | 94 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
96 | |
97 | /* | |
98 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
99 | */ | |
100 | #define IORING_FILE_TABLE_SHIFT 9 | |
101 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
102 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
103 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
104 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
105 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 JA |
106 | |
107 | struct io_uring { | |
108 | u32 head ____cacheline_aligned_in_smp; | |
109 | u32 tail ____cacheline_aligned_in_smp; | |
110 | }; | |
111 | ||
1e84b97b | 112 | /* |
75b28aff HV |
113 | * This data is shared with the application through the mmap at offsets |
114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
115 | * |
116 | * The offsets to the member fields are published through struct | |
117 | * io_sqring_offsets when calling io_uring_setup. | |
118 | */ | |
75b28aff | 119 | struct io_rings { |
1e84b97b SB |
120 | /* |
121 | * Head and tail offsets into the ring; the offsets need to be | |
122 | * masked to get valid indices. | |
123 | * | |
75b28aff HV |
124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
125 | * and the application controls tail of the sq ring and the head of the | |
126 | * cq ring. | |
1e84b97b | 127 | */ |
75b28aff | 128 | struct io_uring sq, cq; |
1e84b97b | 129 | /* |
75b28aff | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
131 | * ring_entries - 1) |
132 | */ | |
75b28aff HV |
133 | u32 sq_ring_mask, cq_ring_mask; |
134 | /* Ring sizes (constant, power of 2) */ | |
135 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
136 | /* |
137 | * Number of invalid entries dropped by the kernel due to | |
138 | * invalid index stored in array | |
139 | * | |
140 | * Written by the kernel, shouldn't be modified by the | |
141 | * application (i.e. get number of "new events" by comparing to | |
142 | * cached value). | |
143 | * | |
144 | * After a new SQ head value was read by the application this | |
145 | * counter includes all submissions that were dropped reaching | |
146 | * the new SQ head (and possibly more). | |
147 | */ | |
75b28aff | 148 | u32 sq_dropped; |
1e84b97b | 149 | /* |
0d9b5b3a | 150 | * Runtime SQ flags |
1e84b97b SB |
151 | * |
152 | * Written by the kernel, shouldn't be modified by the | |
153 | * application. | |
154 | * | |
155 | * The application needs a full memory barrier before checking | |
156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
157 | */ | |
75b28aff | 158 | u32 sq_flags; |
0d9b5b3a SG |
159 | /* |
160 | * Runtime CQ flags | |
161 | * | |
162 | * Written by the application, shouldn't be modified by the | |
163 | * kernel. | |
164 | */ | |
165 | u32 cq_flags; | |
1e84b97b SB |
166 | /* |
167 | * Number of completion events lost because the queue was full; | |
168 | * this should be avoided by the application by making sure | |
0b4295b5 | 169 | * there are not more requests pending than there is space in |
1e84b97b SB |
170 | * the completion queue. |
171 | * | |
172 | * Written by the kernel, shouldn't be modified by the | |
173 | * application (i.e. get number of "new events" by comparing to | |
174 | * cached value). | |
175 | * | |
176 | * As completion events come in out of order this counter is not | |
177 | * ordered with any other data. | |
178 | */ | |
75b28aff | 179 | u32 cq_overflow; |
1e84b97b SB |
180 | /* |
181 | * Ring buffer of completion events. | |
182 | * | |
183 | * The kernel writes completion events fresh every time they are | |
184 | * produced, so the application is allowed to modify pending | |
185 | * entries. | |
186 | */ | |
75b28aff | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
188 | }; |
189 | ||
edafccee JA |
190 | struct io_mapped_ubuf { |
191 | u64 ubuf; | |
192 | size_t len; | |
193 | struct bio_vec *bvec; | |
194 | unsigned int nr_bvecs; | |
de293938 | 195 | unsigned long acct_pages; |
edafccee JA |
196 | }; |
197 | ||
65e19f54 JA |
198 | struct fixed_file_table { |
199 | struct file **files; | |
31b51510 JA |
200 | }; |
201 | ||
05589553 XW |
202 | struct fixed_file_ref_node { |
203 | struct percpu_ref refs; | |
204 | struct list_head node; | |
205 | struct list_head file_list; | |
206 | struct fixed_file_data *file_data; | |
4a38aed2 | 207 | struct llist_node llist; |
e297822b | 208 | bool done; |
05589553 XW |
209 | }; |
210 | ||
05f3fb3c JA |
211 | struct fixed_file_data { |
212 | struct fixed_file_table *table; | |
213 | struct io_ring_ctx *ctx; | |
214 | ||
b2e96852 | 215 | struct fixed_file_ref_node *node; |
05f3fb3c | 216 | struct percpu_ref refs; |
05f3fb3c | 217 | struct completion done; |
05589553 XW |
218 | struct list_head ref_list; |
219 | spinlock_t lock; | |
05f3fb3c JA |
220 | }; |
221 | ||
5a2e745d JA |
222 | struct io_buffer { |
223 | struct list_head list; | |
224 | __u64 addr; | |
225 | __s32 len; | |
226 | __u16 bid; | |
227 | }; | |
228 | ||
21b55dbc SG |
229 | struct io_restriction { |
230 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
231 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
232 | u8 sqe_flags_allowed; | |
233 | u8 sqe_flags_required; | |
7e84e1c7 | 234 | bool registered; |
21b55dbc SG |
235 | }; |
236 | ||
534ca6d6 JA |
237 | struct io_sq_data { |
238 | refcount_t refs; | |
69fb2131 JA |
239 | struct mutex lock; |
240 | ||
241 | /* ctx's that are using this sqd */ | |
242 | struct list_head ctx_list; | |
243 | struct list_head ctx_new_list; | |
244 | struct mutex ctx_lock; | |
245 | ||
534ca6d6 JA |
246 | struct task_struct *thread; |
247 | struct wait_queue_head wait; | |
08369246 XW |
248 | |
249 | unsigned sq_thread_idle; | |
534ca6d6 JA |
250 | }; |
251 | ||
2b188cc1 JA |
252 | struct io_ring_ctx { |
253 | struct { | |
254 | struct percpu_ref refs; | |
255 | } ____cacheline_aligned_in_smp; | |
256 | ||
257 | struct { | |
258 | unsigned int flags; | |
e1d85334 | 259 | unsigned int compat: 1; |
aad5d8da | 260 | unsigned int limit_mem: 1; |
e1d85334 RD |
261 | unsigned int cq_overflow_flushed: 1; |
262 | unsigned int drain_next: 1; | |
263 | unsigned int eventfd_async: 1; | |
21b55dbc | 264 | unsigned int restricted: 1; |
2b188cc1 | 265 | |
75b28aff HV |
266 | /* |
267 | * Ring buffer of indices into array of io_uring_sqe, which is | |
268 | * mmapped by the application using the IORING_OFF_SQES offset. | |
269 | * | |
270 | * This indirection could e.g. be used to assign fixed | |
271 | * io_uring_sqe entries to operations and only submit them to | |
272 | * the queue when needed. | |
273 | * | |
274 | * The kernel modifies neither the indices array nor the entries | |
275 | * array. | |
276 | */ | |
277 | u32 *sq_array; | |
2b188cc1 JA |
278 | unsigned cached_sq_head; |
279 | unsigned sq_entries; | |
280 | unsigned sq_mask; | |
6c271ce2 | 281 | unsigned sq_thread_idle; |
498ccd9e | 282 | unsigned cached_sq_dropped; |
2c3bac6d | 283 | unsigned cached_cq_overflow; |
ad3eb2c8 | 284 | unsigned long sq_check_overflow; |
de0617e4 JA |
285 | |
286 | struct list_head defer_list; | |
5262f567 | 287 | struct list_head timeout_list; |
1d7bb1d5 | 288 | struct list_head cq_overflow_list; |
fcb323cc | 289 | |
ad3eb2c8 | 290 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
291 | } ____cacheline_aligned_in_smp; |
292 | ||
206aefde JA |
293 | struct io_rings *rings; |
294 | ||
2b188cc1 | 295 | /* IO offload */ |
561fb04a | 296 | struct io_wq *io_wq; |
2aede0e4 JA |
297 | |
298 | /* | |
299 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
300 | * have access to the ->files | |
301 | */ | |
302 | struct task_struct *sqo_task; | |
303 | ||
304 | /* Only used for accounting purposes */ | |
305 | struct mm_struct *mm_account; | |
306 | ||
91d8f519 DZ |
307 | #ifdef CONFIG_BLK_CGROUP |
308 | struct cgroup_subsys_state *sqo_blkcg_css; | |
309 | #endif | |
310 | ||
534ca6d6 JA |
311 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
312 | ||
90554200 | 313 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 314 | struct list_head sqd_list; |
75b28aff | 315 | |
6b06314c JA |
316 | /* |
317 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
318 | * readers must ensure that ->refs is alive as long as the file* is | |
319 | * used. Only updated through io_uring_register(2). | |
320 | */ | |
05f3fb3c | 321 | struct fixed_file_data *file_data; |
6b06314c JA |
322 | unsigned nr_user_files; |
323 | ||
edafccee JA |
324 | /* if used, fixed mapped user buffers */ |
325 | unsigned nr_user_bufs; | |
326 | struct io_mapped_ubuf *user_bufs; | |
327 | ||
2b188cc1 JA |
328 | struct user_struct *user; |
329 | ||
0b8c0ec7 | 330 | const struct cred *creds; |
181e448d | 331 | |
4ea33a97 JA |
332 | #ifdef CONFIG_AUDIT |
333 | kuid_t loginuid; | |
334 | unsigned int sessionid; | |
335 | #endif | |
336 | ||
0f158b4c JA |
337 | struct completion ref_comp; |
338 | struct completion sq_thread_comp; | |
206aefde | 339 | |
0ddf92e8 JA |
340 | /* if all else fails... */ |
341 | struct io_kiocb *fallback_req; | |
342 | ||
206aefde JA |
343 | #if defined(CONFIG_UNIX) |
344 | struct socket *ring_sock; | |
345 | #endif | |
346 | ||
5a2e745d JA |
347 | struct idr io_buffer_idr; |
348 | ||
071698e1 JA |
349 | struct idr personality_idr; |
350 | ||
206aefde JA |
351 | struct { |
352 | unsigned cached_cq_tail; | |
353 | unsigned cq_entries; | |
354 | unsigned cq_mask; | |
355 | atomic_t cq_timeouts; | |
ad3eb2c8 | 356 | unsigned long cq_check_overflow; |
206aefde JA |
357 | struct wait_queue_head cq_wait; |
358 | struct fasync_struct *cq_fasync; | |
359 | struct eventfd_ctx *cq_ev_fd; | |
360 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
361 | |
362 | struct { | |
363 | struct mutex uring_lock; | |
364 | wait_queue_head_t wait; | |
365 | } ____cacheline_aligned_in_smp; | |
366 | ||
367 | struct { | |
368 | spinlock_t completion_lock; | |
e94f141b | 369 | |
def596e9 | 370 | /* |
540e32a0 | 371 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
372 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
373 | * For SQPOLL, only the single threaded io_sq_thread() will | |
374 | * manipulate the list, hence no extra locking is needed there. | |
375 | */ | |
540e32a0 | 376 | struct list_head iopoll_list; |
78076bb6 JA |
377 | struct hlist_head *cancel_hash; |
378 | unsigned cancel_hash_bits; | |
e94f141b | 379 | bool poll_multi_file; |
31b51510 | 380 | |
fcb323cc JA |
381 | spinlock_t inflight_lock; |
382 | struct list_head inflight_list; | |
2b188cc1 | 383 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 384 | |
4a38aed2 JA |
385 | struct delayed_work file_put_work; |
386 | struct llist_head file_put_llist; | |
387 | ||
85faa7b8 | 388 | struct work_struct exit_work; |
21b55dbc | 389 | struct io_restriction restrictions; |
2b188cc1 JA |
390 | }; |
391 | ||
09bb8394 JA |
392 | /* |
393 | * First field must be the file pointer in all the | |
394 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
395 | */ | |
221c5eb2 JA |
396 | struct io_poll_iocb { |
397 | struct file *file; | |
018043be | 398 | struct wait_queue_head *head; |
221c5eb2 | 399 | __poll_t events; |
8c838788 | 400 | bool done; |
221c5eb2 | 401 | bool canceled; |
392edb45 | 402 | struct wait_queue_entry wait; |
221c5eb2 JA |
403 | }; |
404 | ||
018043be PB |
405 | struct io_poll_remove { |
406 | struct file *file; | |
407 | u64 addr; | |
408 | }; | |
409 | ||
b5dba59e JA |
410 | struct io_close { |
411 | struct file *file; | |
412 | struct file *put_file; | |
413 | int fd; | |
414 | }; | |
415 | ||
ad8a48ac JA |
416 | struct io_timeout_data { |
417 | struct io_kiocb *req; | |
418 | struct hrtimer timer; | |
419 | struct timespec64 ts; | |
420 | enum hrtimer_mode mode; | |
421 | }; | |
422 | ||
8ed8d3c3 JA |
423 | struct io_accept { |
424 | struct file *file; | |
425 | struct sockaddr __user *addr; | |
426 | int __user *addr_len; | |
427 | int flags; | |
09952e3e | 428 | unsigned long nofile; |
8ed8d3c3 JA |
429 | }; |
430 | ||
431 | struct io_sync { | |
432 | struct file *file; | |
433 | loff_t len; | |
434 | loff_t off; | |
435 | int flags; | |
d63d1b5e | 436 | int mode; |
8ed8d3c3 JA |
437 | }; |
438 | ||
fbf23849 JA |
439 | struct io_cancel { |
440 | struct file *file; | |
441 | u64 addr; | |
442 | }; | |
443 | ||
b29472ee JA |
444 | struct io_timeout { |
445 | struct file *file; | |
bfe68a22 PB |
446 | u32 off; |
447 | u32 target_seq; | |
135fcde8 | 448 | struct list_head list; |
90cd7e42 PB |
449 | /* head of the link, used by linked timeouts only */ |
450 | struct io_kiocb *head; | |
b29472ee JA |
451 | }; |
452 | ||
0bdf7a2d PB |
453 | struct io_timeout_rem { |
454 | struct file *file; | |
455 | u64 addr; | |
9c8e11b3 PB |
456 | |
457 | /* timeout update */ | |
458 | struct timespec64 ts; | |
459 | u32 flags; | |
0bdf7a2d PB |
460 | }; |
461 | ||
9adbd45d JA |
462 | struct io_rw { |
463 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
464 | struct kiocb kiocb; | |
465 | u64 addr; | |
466 | u64 len; | |
467 | }; | |
468 | ||
3fbb51c1 JA |
469 | struct io_connect { |
470 | struct file *file; | |
471 | struct sockaddr __user *addr; | |
472 | int addr_len; | |
473 | }; | |
474 | ||
e47293fd JA |
475 | struct io_sr_msg { |
476 | struct file *file; | |
fddaface | 477 | union { |
270a5940 | 478 | struct user_msghdr __user *umsg; |
fddaface JA |
479 | void __user *buf; |
480 | }; | |
e47293fd | 481 | int msg_flags; |
bcda7baa | 482 | int bgid; |
fddaface | 483 | size_t len; |
bcda7baa | 484 | struct io_buffer *kbuf; |
e47293fd JA |
485 | }; |
486 | ||
15b71abe JA |
487 | struct io_open { |
488 | struct file *file; | |
489 | int dfd; | |
944d1444 | 490 | bool ignore_nonblock; |
15b71abe | 491 | struct filename *filename; |
c12cedf2 | 492 | struct open_how how; |
4022e7af | 493 | unsigned long nofile; |
15b71abe JA |
494 | }; |
495 | ||
05f3fb3c JA |
496 | struct io_files_update { |
497 | struct file *file; | |
498 | u64 arg; | |
499 | u32 nr_args; | |
500 | u32 offset; | |
501 | }; | |
502 | ||
4840e418 JA |
503 | struct io_fadvise { |
504 | struct file *file; | |
505 | u64 offset; | |
506 | u32 len; | |
507 | u32 advice; | |
508 | }; | |
509 | ||
c1ca757b JA |
510 | struct io_madvise { |
511 | struct file *file; | |
512 | u64 addr; | |
513 | u32 len; | |
514 | u32 advice; | |
515 | }; | |
516 | ||
3e4827b0 JA |
517 | struct io_epoll { |
518 | struct file *file; | |
519 | int epfd; | |
520 | int op; | |
521 | int fd; | |
522 | struct epoll_event event; | |
e47293fd JA |
523 | }; |
524 | ||
7d67af2c PB |
525 | struct io_splice { |
526 | struct file *file_out; | |
527 | struct file *file_in; | |
528 | loff_t off_out; | |
529 | loff_t off_in; | |
530 | u64 len; | |
531 | unsigned int flags; | |
532 | }; | |
533 | ||
ddf0322d JA |
534 | struct io_provide_buf { |
535 | struct file *file; | |
536 | __u64 addr; | |
537 | __s32 len; | |
538 | __u32 bgid; | |
539 | __u16 nbufs; | |
540 | __u16 bid; | |
541 | }; | |
542 | ||
1d9e1288 BM |
543 | struct io_statx { |
544 | struct file *file; | |
545 | int dfd; | |
546 | unsigned int mask; | |
547 | unsigned int flags; | |
e62753e4 | 548 | const char __user *filename; |
1d9e1288 BM |
549 | struct statx __user *buffer; |
550 | }; | |
551 | ||
36f4fa68 JA |
552 | struct io_shutdown { |
553 | struct file *file; | |
554 | int how; | |
555 | }; | |
556 | ||
80a261fd JA |
557 | struct io_rename { |
558 | struct file *file; | |
559 | int old_dfd; | |
560 | int new_dfd; | |
561 | struct filename *oldpath; | |
562 | struct filename *newpath; | |
563 | int flags; | |
564 | }; | |
565 | ||
14a1143b JA |
566 | struct io_unlink { |
567 | struct file *file; | |
568 | int dfd; | |
569 | int flags; | |
570 | struct filename *filename; | |
571 | }; | |
572 | ||
3ca405eb PB |
573 | struct io_completion { |
574 | struct file *file; | |
575 | struct list_head list; | |
0f7e466b | 576 | int cflags; |
3ca405eb PB |
577 | }; |
578 | ||
f499a021 JA |
579 | struct io_async_connect { |
580 | struct sockaddr_storage address; | |
581 | }; | |
582 | ||
03b1230c JA |
583 | struct io_async_msghdr { |
584 | struct iovec fast_iov[UIO_FASTIOV]; | |
585 | struct iovec *iov; | |
586 | struct sockaddr __user *uaddr; | |
587 | struct msghdr msg; | |
b537916c | 588 | struct sockaddr_storage addr; |
03b1230c JA |
589 | }; |
590 | ||
f67676d1 JA |
591 | struct io_async_rw { |
592 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
593 | const struct iovec *free_iovec; |
594 | struct iov_iter iter; | |
227c0c96 | 595 | size_t bytes_done; |
bcf5a063 | 596 | struct wait_page_queue wpq; |
f67676d1 JA |
597 | }; |
598 | ||
6b47ee6e PB |
599 | enum { |
600 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
601 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
602 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
603 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
604 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 605 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 606 | |
6b47ee6e PB |
607 | REQ_F_FAIL_LINK_BIT, |
608 | REQ_F_INFLIGHT_BIT, | |
609 | REQ_F_CUR_POS_BIT, | |
610 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 611 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 612 | REQ_F_ISREG_BIT, |
99bc4c38 | 613 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 614 | REQ_F_POLLED_BIT, |
bcda7baa | 615 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 616 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 617 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 618 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
84557871 JA |
619 | |
620 | /* not a real bit, just to check we're not overflowing the space */ | |
621 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
622 | }; |
623 | ||
624 | enum { | |
625 | /* ctx owns file */ | |
626 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
627 | /* drain existing IO first */ | |
628 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
629 | /* linked sqes */ | |
630 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
631 | /* doesn't sever on completion < 0 */ | |
632 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
633 | /* IOSQE_ASYNC */ | |
634 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
635 | /* IOSQE_BUFFER_SELECT */ |
636 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 637 | |
6b47ee6e PB |
638 | /* fail rest of links */ |
639 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
640 | /* on inflight list */ | |
641 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
642 | /* read/write uses file position */ | |
643 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
644 | /* must not punt to workers */ | |
645 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 646 | /* has or had linked timeout */ |
6b47ee6e | 647 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
648 | /* regular file */ |
649 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
650 | /* needs cleanup */ |
651 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
652 | /* already went through poll handler */ |
653 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
654 | /* buffer already selected */ |
655 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
656 | /* doesn't need file table for this request */ |
657 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
658 | /* io_wq_work is initialized */ |
659 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
660 | /* linked timeout is active, i.e. prepared by link's head */ |
661 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
d7718a9d JA |
662 | }; |
663 | ||
664 | struct async_poll { | |
665 | struct io_poll_iocb poll; | |
807abcb0 | 666 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
667 | }; |
668 | ||
09bb8394 JA |
669 | /* |
670 | * NOTE! Each of the iocb union members has the file pointer | |
671 | * as the first entry in their struct definition. So you can | |
672 | * access the file pointer through any of the sub-structs, | |
673 | * or directly as just 'ki_filp' in this struct. | |
674 | */ | |
2b188cc1 | 675 | struct io_kiocb { |
221c5eb2 | 676 | union { |
09bb8394 | 677 | struct file *file; |
9adbd45d | 678 | struct io_rw rw; |
221c5eb2 | 679 | struct io_poll_iocb poll; |
018043be | 680 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
681 | struct io_accept accept; |
682 | struct io_sync sync; | |
fbf23849 | 683 | struct io_cancel cancel; |
b29472ee | 684 | struct io_timeout timeout; |
0bdf7a2d | 685 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 686 | struct io_connect connect; |
e47293fd | 687 | struct io_sr_msg sr_msg; |
15b71abe | 688 | struct io_open open; |
b5dba59e | 689 | struct io_close close; |
05f3fb3c | 690 | struct io_files_update files_update; |
4840e418 | 691 | struct io_fadvise fadvise; |
c1ca757b | 692 | struct io_madvise madvise; |
3e4827b0 | 693 | struct io_epoll epoll; |
7d67af2c | 694 | struct io_splice splice; |
ddf0322d | 695 | struct io_provide_buf pbuf; |
1d9e1288 | 696 | struct io_statx statx; |
36f4fa68 | 697 | struct io_shutdown shutdown; |
80a261fd | 698 | struct io_rename rename; |
14a1143b | 699 | struct io_unlink unlink; |
3ca405eb PB |
700 | /* use only after cleaning per-op data, see io_clean_op() */ |
701 | struct io_completion compl; | |
221c5eb2 | 702 | }; |
2b188cc1 | 703 | |
e8c2bc1f JA |
704 | /* opcode allocated if it needs to store data for async defer */ |
705 | void *async_data; | |
d625c6ee | 706 | u8 opcode; |
65a6543d XW |
707 | /* polled IO has completed */ |
708 | u8 iopoll_completed; | |
2b188cc1 | 709 | |
4f4eeba8 | 710 | u16 buf_index; |
9cf7c104 | 711 | u32 result; |
4f4eeba8 | 712 | |
010e8e6b PB |
713 | struct io_ring_ctx *ctx; |
714 | unsigned int flags; | |
715 | refcount_t refs; | |
716 | struct task_struct *task; | |
717 | u64 user_data; | |
d7718a9d | 718 | |
f2f87370 | 719 | struct io_kiocb *link; |
0415767e | 720 | struct percpu_ref *fixed_file_refs; |
fcb323cc | 721 | |
d21ffe7e PB |
722 | /* |
723 | * 1. used with ctx->iopoll_list with reads/writes | |
724 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
725 | */ | |
010e8e6b | 726 | struct list_head inflight_entry; |
010e8e6b PB |
727 | struct callback_head task_work; |
728 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
729 | struct hlist_node hash_node; | |
730 | struct async_poll *apoll; | |
731 | struct io_wq_work work; | |
2b188cc1 | 732 | }; |
05589553 | 733 | |
27dc8338 PB |
734 | struct io_defer_entry { |
735 | struct list_head list; | |
736 | struct io_kiocb *req; | |
9cf7c104 | 737 | u32 seq; |
2b188cc1 JA |
738 | }; |
739 | ||
def596e9 | 740 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 741 | |
013538bd JA |
742 | struct io_comp_state { |
743 | unsigned int nr; | |
744 | struct list_head list; | |
745 | struct io_ring_ctx *ctx; | |
746 | }; | |
747 | ||
9a56a232 JA |
748 | struct io_submit_state { |
749 | struct blk_plug plug; | |
750 | ||
2579f913 JA |
751 | /* |
752 | * io_kiocb alloc cache | |
753 | */ | |
754 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 755 | unsigned int free_reqs; |
2579f913 | 756 | |
27926b68 JA |
757 | bool plug_started; |
758 | ||
013538bd JA |
759 | /* |
760 | * Batch completion logic | |
761 | */ | |
762 | struct io_comp_state comp; | |
763 | ||
9a56a232 JA |
764 | /* |
765 | * File reference cache | |
766 | */ | |
767 | struct file *file; | |
768 | unsigned int fd; | |
6e1271e6 | 769 | unsigned int file_refs; |
9a56a232 JA |
770 | unsigned int ios_left; |
771 | }; | |
772 | ||
d3656344 | 773 | struct io_op_def { |
d3656344 JA |
774 | /* needs req->file assigned */ |
775 | unsigned needs_file : 1; | |
fd2206e4 JA |
776 | /* don't fail if file grab fails */ |
777 | unsigned needs_file_no_error : 1; | |
d3656344 JA |
778 | /* hash wq insertion if file is a regular file */ |
779 | unsigned hash_reg_file : 1; | |
780 | /* unbound wq insertion if file is a non-regular file */ | |
781 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
782 | /* opcode is not supported by this kernel */ |
783 | unsigned not_supported : 1; | |
8a72758c JA |
784 | /* set if opcode supports polled "wait" */ |
785 | unsigned pollin : 1; | |
786 | unsigned pollout : 1; | |
bcda7baa JA |
787 | /* op supports buffer selection */ |
788 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
789 | /* must always have async data allocated */ |
790 | unsigned needs_async_data : 1; | |
27926b68 JA |
791 | /* should block plug */ |
792 | unsigned plug : 1; | |
e8c2bc1f JA |
793 | /* size of async data needed, if any */ |
794 | unsigned short async_size; | |
0f203765 | 795 | unsigned work_flags; |
d3656344 JA |
796 | }; |
797 | ||
0918682b | 798 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
799 | [IORING_OP_NOP] = {}, |
800 | [IORING_OP_READV] = { | |
d3656344 JA |
801 | .needs_file = 1, |
802 | .unbound_nonreg_file = 1, | |
8a72758c | 803 | .pollin = 1, |
4d954c25 | 804 | .buffer_select = 1, |
e8c2bc1f | 805 | .needs_async_data = 1, |
27926b68 | 806 | .plug = 1, |
e8c2bc1f | 807 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 808 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 809 | }, |
0463b6c5 | 810 | [IORING_OP_WRITEV] = { |
d3656344 JA |
811 | .needs_file = 1, |
812 | .hash_reg_file = 1, | |
813 | .unbound_nonreg_file = 1, | |
8a72758c | 814 | .pollout = 1, |
e8c2bc1f | 815 | .needs_async_data = 1, |
27926b68 | 816 | .plug = 1, |
e8c2bc1f | 817 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
818 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
819 | IO_WQ_WORK_FSIZE, | |
d3656344 | 820 | }, |
0463b6c5 | 821 | [IORING_OP_FSYNC] = { |
d3656344 | 822 | .needs_file = 1, |
0f203765 | 823 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 824 | }, |
0463b6c5 | 825 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
826 | .needs_file = 1, |
827 | .unbound_nonreg_file = 1, | |
8a72758c | 828 | .pollin = 1, |
27926b68 | 829 | .plug = 1, |
e8c2bc1f | 830 | .async_size = sizeof(struct io_async_rw), |
4017eb91 | 831 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
d3656344 | 832 | }, |
0463b6c5 | 833 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
834 | .needs_file = 1, |
835 | .hash_reg_file = 1, | |
836 | .unbound_nonreg_file = 1, | |
8a72758c | 837 | .pollout = 1, |
27926b68 | 838 | .plug = 1, |
e8c2bc1f | 839 | .async_size = sizeof(struct io_async_rw), |
4017eb91 JA |
840 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
841 | IO_WQ_WORK_MM, | |
d3656344 | 842 | }, |
0463b6c5 | 843 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
844 | .needs_file = 1, |
845 | .unbound_nonreg_file = 1, | |
846 | }, | |
0463b6c5 PB |
847 | [IORING_OP_POLL_REMOVE] = {}, |
848 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 849 | .needs_file = 1, |
0f203765 | 850 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 851 | }, |
0463b6c5 | 852 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
853 | .needs_file = 1, |
854 | .unbound_nonreg_file = 1, | |
8a72758c | 855 | .pollout = 1, |
e8c2bc1f JA |
856 | .needs_async_data = 1, |
857 | .async_size = sizeof(struct io_async_msghdr), | |
10cad2c4 | 858 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 859 | }, |
0463b6c5 | 860 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
861 | .needs_file = 1, |
862 | .unbound_nonreg_file = 1, | |
8a72758c | 863 | .pollin = 1, |
52de1fe1 | 864 | .buffer_select = 1, |
e8c2bc1f JA |
865 | .needs_async_data = 1, |
866 | .async_size = sizeof(struct io_async_msghdr), | |
10cad2c4 | 867 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 868 | }, |
0463b6c5 | 869 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
870 | .needs_async_data = 1, |
871 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 872 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 873 | }, |
9c8e11b3 PB |
874 | [IORING_OP_TIMEOUT_REMOVE] = { |
875 | /* used by timeout updates' prep() */ | |
876 | .work_flags = IO_WQ_WORK_MM, | |
877 | }, | |
0463b6c5 | 878 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
879 | .needs_file = 1, |
880 | .unbound_nonreg_file = 1, | |
8a72758c | 881 | .pollin = 1, |
0f203765 | 882 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
d3656344 | 883 | }, |
0463b6c5 PB |
884 | [IORING_OP_ASYNC_CANCEL] = {}, |
885 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
886 | .needs_async_data = 1, |
887 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 888 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 889 | }, |
0463b6c5 | 890 | [IORING_OP_CONNECT] = { |
d3656344 JA |
891 | .needs_file = 1, |
892 | .unbound_nonreg_file = 1, | |
8a72758c | 893 | .pollout = 1, |
e8c2bc1f JA |
894 | .needs_async_data = 1, |
895 | .async_size = sizeof(struct io_async_connect), | |
0f203765 | 896 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 897 | }, |
0463b6c5 | 898 | [IORING_OP_FALLOCATE] = { |
d3656344 | 899 | .needs_file = 1, |
69228338 | 900 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
d3656344 | 901 | }, |
0463b6c5 | 902 | [IORING_OP_OPENAT] = { |
0f203765 | 903 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
14587a46 | 904 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
d3656344 | 905 | }, |
0463b6c5 | 906 | [IORING_OP_CLOSE] = { |
fd2206e4 JA |
907 | .needs_file = 1, |
908 | .needs_file_no_error = 1, | |
0f203765 | 909 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 910 | }, |
0463b6c5 | 911 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 912 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 913 | }, |
0463b6c5 | 914 | [IORING_OP_STATX] = { |
0f203765 JA |
915 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
916 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 917 | }, |
0463b6c5 | 918 | [IORING_OP_READ] = { |
3a6820f2 JA |
919 | .needs_file = 1, |
920 | .unbound_nonreg_file = 1, | |
8a72758c | 921 | .pollin = 1, |
bcda7baa | 922 | .buffer_select = 1, |
27926b68 | 923 | .plug = 1, |
e8c2bc1f | 924 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 925 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 926 | }, |
0463b6c5 | 927 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
928 | .needs_file = 1, |
929 | .unbound_nonreg_file = 1, | |
8a72758c | 930 | .pollout = 1, |
27926b68 | 931 | .plug = 1, |
e8c2bc1f | 932 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
933 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
934 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 935 | }, |
0463b6c5 | 936 | [IORING_OP_FADVISE] = { |
4840e418 | 937 | .needs_file = 1, |
0f203765 | 938 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 939 | }, |
0463b6c5 | 940 | [IORING_OP_MADVISE] = { |
0f203765 | 941 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 942 | }, |
0463b6c5 | 943 | [IORING_OP_SEND] = { |
fddaface JA |
944 | .needs_file = 1, |
945 | .unbound_nonreg_file = 1, | |
8a72758c | 946 | .pollout = 1, |
0f203765 | 947 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 948 | }, |
0463b6c5 | 949 | [IORING_OP_RECV] = { |
fddaface JA |
950 | .needs_file = 1, |
951 | .unbound_nonreg_file = 1, | |
8a72758c | 952 | .pollin = 1, |
bcda7baa | 953 | .buffer_select = 1, |
0f203765 | 954 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 955 | }, |
0463b6c5 | 956 | [IORING_OP_OPENAT2] = { |
0f203765 | 957 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
14587a46 | 958 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
cebdb986 | 959 | }, |
3e4827b0 JA |
960 | [IORING_OP_EPOLL_CTL] = { |
961 | .unbound_nonreg_file = 1, | |
0f203765 | 962 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 963 | }, |
7d67af2c PB |
964 | [IORING_OP_SPLICE] = { |
965 | .needs_file = 1, | |
966 | .hash_reg_file = 1, | |
967 | .unbound_nonreg_file = 1, | |
0f203765 | 968 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
969 | }, |
970 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 971 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
972 | [IORING_OP_TEE] = { |
973 | .needs_file = 1, | |
974 | .hash_reg_file = 1, | |
975 | .unbound_nonreg_file = 1, | |
976 | }, | |
36f4fa68 JA |
977 | [IORING_OP_SHUTDOWN] = { |
978 | .needs_file = 1, | |
979 | }, | |
80a261fd JA |
980 | [IORING_OP_RENAMEAT] = { |
981 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
982 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
983 | }, | |
14a1143b JA |
984 | [IORING_OP_UNLINKAT] = { |
985 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
986 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
987 | }, | |
d3656344 JA |
988 | }; |
989 | ||
2e0464d4 BM |
990 | enum io_mem_account { |
991 | ACCT_LOCKED, | |
992 | ACCT_PINNED, | |
993 | }; | |
994 | ||
81b68a5c PB |
995 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
996 | struct io_comp_state *cs); | |
78e19bbe | 997 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 998 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 999 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1000 | static void io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 1001 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1002 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1003 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c JA |
1004 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
1005 | struct io_uring_files_update *ip, | |
1006 | unsigned nr_args); | |
3ca405eb | 1007 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1008 | static struct file *io_file_get(struct io_submit_state *state, |
1009 | struct io_kiocb *req, int fd, bool fixed); | |
c1379e24 | 1010 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
4349f30e | 1011 | static void io_file_put_work(struct work_struct *work); |
de0617e4 | 1012 | |
b63534c4 JA |
1013 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
1014 | struct iovec **iovec, struct iov_iter *iter, | |
1015 | bool needs_lock); | |
ff6165b2 JA |
1016 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1017 | const struct iovec *fast_iov, | |
227c0c96 | 1018 | struct iov_iter *iter, bool force); |
de0617e4 | 1019 | |
2b188cc1 JA |
1020 | static struct kmem_cache *req_cachep; |
1021 | ||
0918682b | 1022 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1023 | |
1024 | struct sock *io_uring_get_socket(struct file *file) | |
1025 | { | |
1026 | #if defined(CONFIG_UNIX) | |
1027 | if (file->f_op == &io_uring_fops) { | |
1028 | struct io_ring_ctx *ctx = file->private_data; | |
1029 | ||
1030 | return ctx->ring_sock->sk; | |
1031 | } | |
1032 | #endif | |
1033 | return NULL; | |
1034 | } | |
1035 | EXPORT_SYMBOL(io_uring_get_socket); | |
1036 | ||
f2f87370 PB |
1037 | #define io_for_each_link(pos, head) \ |
1038 | for (pos = (head); pos; pos = pos->link) | |
1039 | ||
3ca405eb PB |
1040 | static inline void io_clean_op(struct io_kiocb *req) |
1041 | { | |
bb175342 PB |
1042 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
1043 | REQ_F_INFLIGHT)) | |
3ca405eb PB |
1044 | __io_clean_op(req); |
1045 | } | |
1046 | ||
36f72fe2 PB |
1047 | static inline void io_set_resource_node(struct io_kiocb *req) |
1048 | { | |
1049 | struct io_ring_ctx *ctx = req->ctx; | |
1050 | ||
1051 | if (!req->fixed_file_refs) { | |
1052 | req->fixed_file_refs = &ctx->file_data->node->refs; | |
1053 | percpu_ref_get(req->fixed_file_refs); | |
1054 | } | |
1055 | } | |
1056 | ||
08d23634 PB |
1057 | static bool io_match_task(struct io_kiocb *head, |
1058 | struct task_struct *task, | |
1059 | struct files_struct *files) | |
1060 | { | |
1061 | struct io_kiocb *req; | |
1062 | ||
1063 | if (task && head->task != task) | |
1064 | return false; | |
1065 | if (!files) | |
1066 | return true; | |
1067 | ||
1068 | io_for_each_link(req, head) { | |
1069 | if ((req->flags & REQ_F_WORK_INITIALIZED) && | |
1070 | (req->work.flags & IO_WQ_WORK_FILES) && | |
1071 | req->work.identity->files == files) | |
1072 | return true; | |
1073 | } | |
1074 | return false; | |
1075 | } | |
1076 | ||
28cea78a | 1077 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1078 | { |
28cea78a | 1079 | struct files_struct *files = current->files; |
c40f6379 JA |
1080 | struct mm_struct *mm = current->mm; |
1081 | ||
1082 | if (mm) { | |
1083 | kthread_unuse_mm(mm); | |
1084 | mmput(mm); | |
4b70cf9d | 1085 | current->mm = NULL; |
c40f6379 | 1086 | } |
28cea78a JA |
1087 | if (files) { |
1088 | struct nsproxy *nsproxy = current->nsproxy; | |
1089 | ||
1090 | task_lock(current); | |
1091 | current->files = NULL; | |
1092 | current->nsproxy = NULL; | |
1093 | task_unlock(current); | |
1094 | put_files_struct(files); | |
1095 | put_nsproxy(nsproxy); | |
1096 | } | |
1097 | } | |
1098 | ||
1a38ffc9 | 1099 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a JA |
1100 | { |
1101 | if (!current->files) { | |
1102 | struct files_struct *files; | |
1103 | struct nsproxy *nsproxy; | |
1104 | ||
1105 | task_lock(ctx->sqo_task); | |
1106 | files = ctx->sqo_task->files; | |
1107 | if (!files) { | |
1108 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1109 | return -EOWNERDEAD; |
28cea78a JA |
1110 | } |
1111 | atomic_inc(&files->count); | |
1112 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1113 | nsproxy = ctx->sqo_task->nsproxy; | |
1114 | task_unlock(ctx->sqo_task); | |
1115 | ||
1116 | task_lock(current); | |
1117 | current->files = files; | |
1118 | current->nsproxy = nsproxy; | |
1119 | task_unlock(current); | |
1120 | } | |
1a38ffc9 | 1121 | return 0; |
c40f6379 JA |
1122 | } |
1123 | ||
1124 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1125 | { | |
4b70cf9d JA |
1126 | struct mm_struct *mm; |
1127 | ||
1128 | if (current->mm) | |
1129 | return 0; | |
1130 | ||
1131 | /* Should never happen */ | |
1132 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) | |
1133 | return -EFAULT; | |
1134 | ||
1135 | task_lock(ctx->sqo_task); | |
1136 | mm = ctx->sqo_task->mm; | |
1137 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1138 | mm = NULL; | |
1139 | task_unlock(ctx->sqo_task); | |
1140 | ||
1141 | if (mm) { | |
1142 | kthread_use_mm(mm); | |
1143 | return 0; | |
c40f6379 JA |
1144 | } |
1145 | ||
4b70cf9d | 1146 | return -EFAULT; |
c40f6379 JA |
1147 | } |
1148 | ||
28cea78a JA |
1149 | static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1150 | struct io_kiocb *req) | |
c40f6379 | 1151 | { |
28cea78a | 1152 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1153 | int ret; |
28cea78a JA |
1154 | |
1155 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1156 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1157 | if (unlikely(ret)) |
1158 | return ret; | |
1159 | } | |
1160 | ||
1a38ffc9 PB |
1161 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1162 | ret = __io_sq_thread_acquire_files(ctx); | |
1163 | if (unlikely(ret)) | |
1164 | return ret; | |
1165 | } | |
28cea78a JA |
1166 | |
1167 | return 0; | |
c40f6379 JA |
1168 | } |
1169 | ||
91d8f519 DZ |
1170 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1171 | struct cgroup_subsys_state **cur_css) | |
1172 | ||
1173 | { | |
1174 | #ifdef CONFIG_BLK_CGROUP | |
1175 | /* puts the old one when swapping */ | |
1176 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1177 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1178 | *cur_css = ctx->sqo_blkcg_css; | |
1179 | } | |
1180 | #endif | |
1181 | } | |
1182 | ||
1183 | static void io_sq_thread_unassociate_blkcg(void) | |
1184 | { | |
1185 | #ifdef CONFIG_BLK_CGROUP | |
1186 | kthread_associate_blkcg(NULL); | |
1187 | #endif | |
1188 | } | |
1189 | ||
c40f6379 JA |
1190 | static inline void req_set_fail_links(struct io_kiocb *req) |
1191 | { | |
1192 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1193 | req->flags |= REQ_F_FAIL_LINK; | |
1194 | } | |
4a38aed2 | 1195 | |
1e6fa521 JA |
1196 | /* |
1197 | * None of these are dereferenced, they are simply used to check if any of | |
1198 | * them have changed. If we're under current and check they are still the | |
1199 | * same, we're fine to grab references to them for actual out-of-line use. | |
1200 | */ | |
1201 | static void io_init_identity(struct io_identity *id) | |
1202 | { | |
1203 | id->files = current->files; | |
1204 | id->mm = current->mm; | |
1205 | #ifdef CONFIG_BLK_CGROUP | |
1206 | rcu_read_lock(); | |
1207 | id->blkcg_css = blkcg_css(); | |
1208 | rcu_read_unlock(); | |
1209 | #endif | |
1210 | id->creds = current_cred(); | |
1211 | id->nsproxy = current->nsproxy; | |
1212 | id->fs = current->fs; | |
1213 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1214 | #ifdef CONFIG_AUDIT |
1215 | id->loginuid = current->loginuid; | |
1216 | id->sessionid = current->sessionid; | |
1217 | #endif | |
1e6fa521 JA |
1218 | refcount_set(&id->count, 1); |
1219 | } | |
1220 | ||
ec99ca6c PB |
1221 | static inline void __io_req_init_async(struct io_kiocb *req) |
1222 | { | |
1223 | memset(&req->work, 0, sizeof(req->work)); | |
1224 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1225 | } | |
1226 | ||
7cdaf587 XW |
1227 | /* |
1228 | * Note: must call io_req_init_async() for the first time you | |
1229 | * touch any members of io_wq_work. | |
1230 | */ | |
1231 | static inline void io_req_init_async(struct io_kiocb *req) | |
1232 | { | |
500a373d JA |
1233 | struct io_uring_task *tctx = current->io_uring; |
1234 | ||
7cdaf587 XW |
1235 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1236 | return; | |
1237 | ||
ec99ca6c | 1238 | __io_req_init_async(req); |
500a373d JA |
1239 | |
1240 | /* Grab a ref if this isn't our static identity */ | |
1241 | req->work.identity = tctx->identity; | |
1242 | if (tctx->identity != &tctx->__identity) | |
1243 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1244 | } |
1245 | ||
0cdaf760 PB |
1246 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
1247 | { | |
1248 | return ctx->flags & IORING_SETUP_SQPOLL; | |
1249 | } | |
1250 | ||
2b188cc1 JA |
1251 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1252 | { | |
1253 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1254 | ||
0f158b4c | 1255 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1256 | } |
1257 | ||
8eb7e2d0 PB |
1258 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1259 | { | |
1260 | return !req->timeout.off; | |
1261 | } | |
1262 | ||
2b188cc1 JA |
1263 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1264 | { | |
1265 | struct io_ring_ctx *ctx; | |
78076bb6 | 1266 | int hash_bits; |
2b188cc1 JA |
1267 | |
1268 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1269 | if (!ctx) | |
1270 | return NULL; | |
1271 | ||
0ddf92e8 JA |
1272 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
1273 | if (!ctx->fallback_req) | |
1274 | goto err; | |
1275 | ||
78076bb6 JA |
1276 | /* |
1277 | * Use 5 bits less than the max cq entries, that should give us around | |
1278 | * 32 entries per hash list if totally full and uniformly spread. | |
1279 | */ | |
1280 | hash_bits = ilog2(p->cq_entries); | |
1281 | hash_bits -= 5; | |
1282 | if (hash_bits <= 0) | |
1283 | hash_bits = 1; | |
1284 | ctx->cancel_hash_bits = hash_bits; | |
1285 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1286 | GFP_KERNEL); | |
1287 | if (!ctx->cancel_hash) | |
1288 | goto err; | |
1289 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1290 | ||
21482896 | 1291 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1292 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1293 | goto err; | |
2b188cc1 JA |
1294 | |
1295 | ctx->flags = p->flags; | |
90554200 | 1296 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1297 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1298 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1299 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1300 | init_completion(&ctx->ref_comp); |
1301 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1302 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1303 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1304 | mutex_init(&ctx->uring_lock); |
1305 | init_waitqueue_head(&ctx->wait); | |
1306 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1307 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1308 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1309 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1310 | spin_lock_init(&ctx->inflight_lock); |
1311 | INIT_LIST_HEAD(&ctx->inflight_list); | |
4a38aed2 JA |
1312 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
1313 | init_llist_head(&ctx->file_put_llist); | |
2b188cc1 | 1314 | return ctx; |
206aefde | 1315 | err: |
0ddf92e8 JA |
1316 | if (ctx->fallback_req) |
1317 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
78076bb6 | 1318 | kfree(ctx->cancel_hash); |
206aefde JA |
1319 | kfree(ctx); |
1320 | return NULL; | |
2b188cc1 JA |
1321 | } |
1322 | ||
9cf7c104 | 1323 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1324 | { |
2bc9930e JA |
1325 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1326 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1327 | |
9cf7c104 | 1328 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1329 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1330 | } |
de0617e4 | 1331 | |
9d858b21 | 1332 | return false; |
de0617e4 JA |
1333 | } |
1334 | ||
de0617e4 | 1335 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 1336 | { |
75b28aff | 1337 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 1338 | |
07910158 PB |
1339 | /* order cqe stores with ring update */ |
1340 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 | 1341 | |
07910158 PB |
1342 | if (wq_has_sleeper(&ctx->cq_wait)) { |
1343 | wake_up_interruptible(&ctx->cq_wait); | |
1344 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
2b188cc1 JA |
1345 | } |
1346 | } | |
1347 | ||
5c3462cf | 1348 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1349 | { |
500a373d | 1350 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1351 | return; |
1352 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1353 | kfree(req->work.identity); | |
1354 | } | |
1355 | ||
4edf20f9 | 1356 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1357 | { |
7cdaf587 | 1358 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1359 | return; |
51a4cc11 JA |
1360 | |
1361 | req->flags &= ~REQ_F_WORK_INITIALIZED; | |
7cdaf587 | 1362 | |
dfead8a8 | 1363 | if (req->work.flags & IO_WQ_WORK_MM) { |
98447d65 | 1364 | mmdrop(req->work.identity->mm); |
dfead8a8 | 1365 | req->work.flags &= ~IO_WQ_WORK_MM; |
cccf0ee8 | 1366 | } |
91d8f519 | 1367 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 | 1368 | if (req->work.flags & IO_WQ_WORK_BLKCG) { |
98447d65 | 1369 | css_put(req->work.identity->blkcg_css); |
dfead8a8 JA |
1370 | req->work.flags &= ~IO_WQ_WORK_BLKCG; |
1371 | } | |
91d8f519 | 1372 | #endif |
dfead8a8 | 1373 | if (req->work.flags & IO_WQ_WORK_CREDS) { |
98447d65 | 1374 | put_cred(req->work.identity->creds); |
dfead8a8 | 1375 | req->work.flags &= ~IO_WQ_WORK_CREDS; |
cccf0ee8 | 1376 | } |
dfead8a8 | 1377 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1378 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1379 | |
98447d65 | 1380 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1381 | if (--fs->users) |
1382 | fs = NULL; | |
98447d65 | 1383 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1384 | if (fs) |
1385 | free_fs_struct(fs); | |
dfead8a8 | 1386 | req->work.flags &= ~IO_WQ_WORK_FS; |
ff002b30 | 1387 | } |
51a4cc11 | 1388 | |
5c3462cf | 1389 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1390 | } |
1391 | ||
1e6fa521 JA |
1392 | /* |
1393 | * Create a private copy of io_identity, since some fields don't match | |
1394 | * the current context. | |
1395 | */ | |
1396 | static bool io_identity_cow(struct io_kiocb *req) | |
1397 | { | |
5c3462cf | 1398 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1399 | const struct cred *creds = NULL; |
1400 | struct io_identity *id; | |
1401 | ||
1402 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1403 | creds = req->work.identity->creds; | |
1404 | ||
1405 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1406 | if (unlikely(!id)) { | |
1407 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1408 | return false; | |
1409 | } | |
1410 | ||
1411 | /* | |
1412 | * We can safely just re-init the creds we copied Either the field | |
1413 | * matches the current one, or we haven't grabbed it yet. The only | |
1414 | * exception is ->creds, through registered personalities, so handle | |
1415 | * that one separately. | |
1416 | */ | |
1417 | io_init_identity(id); | |
1418 | if (creds) | |
1419 | req->work.identity->creds = creds; | |
1420 | ||
1421 | /* add one for this request */ | |
1422 | refcount_inc(&id->count); | |
1423 | ||
cb8a8ae3 JA |
1424 | /* drop tctx and req identity references, if needed */ |
1425 | if (tctx->identity != &tctx->__identity && | |
1426 | refcount_dec_and_test(&tctx->identity->count)) | |
1427 | kfree(tctx->identity); | |
1428 | if (req->work.identity != &tctx->__identity && | |
1429 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1430 | kfree(req->work.identity); |
1431 | ||
1432 | req->work.identity = id; | |
500a373d | 1433 | tctx->identity = id; |
1e6fa521 JA |
1434 | return true; |
1435 | } | |
1436 | ||
1437 | static bool io_grab_identity(struct io_kiocb *req) | |
18d9be1a | 1438 | { |
d3656344 | 1439 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1440 | struct io_identity *id = req->work.identity; |
23329513 | 1441 | struct io_ring_ctx *ctx = req->ctx; |
54a91f3b | 1442 | |
69228338 JA |
1443 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1444 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1445 | return false; | |
1446 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1447 | } | |
16d59803 | 1448 | |
dfead8a8 | 1449 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1e6fa521 | 1450 | (def->work_flags & IO_WQ_WORK_FILES) && |
23329513 | 1451 | !(req->flags & REQ_F_NO_FILE_TABLE)) { |
1e6fa521 JA |
1452 | if (id->files != current->files || |
1453 | id->nsproxy != current->nsproxy) | |
1454 | return false; | |
1455 | atomic_inc(&id->files->count); | |
1456 | get_nsproxy(id->nsproxy); | |
23329513 PB |
1457 | req->flags |= REQ_F_INFLIGHT; |
1458 | ||
1459 | spin_lock_irq(&ctx->inflight_lock); | |
1460 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1461 | spin_unlock_irq(&ctx->inflight_lock); | |
dfead8a8 | 1462 | req->work.flags |= IO_WQ_WORK_FILES; |
dca9cf8b | 1463 | } |
91d8f519 | 1464 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1465 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1466 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1467 | rcu_read_lock(); |
1e6fa521 JA |
1468 | if (id->blkcg_css != blkcg_css()) { |
1469 | rcu_read_unlock(); | |
1470 | return false; | |
1471 | } | |
91d8f519 DZ |
1472 | /* |
1473 | * This should be rare, either the cgroup is dying or the task | |
1474 | * is moving cgroups. Just punt to root for the handful of ios. | |
1475 | */ | |
1e6fa521 | 1476 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1477 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1478 | rcu_read_unlock(); |
1479 | } | |
1480 | #endif | |
dfead8a8 | 1481 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1482 | if (id->creds != current_cred()) |
1483 | return false; | |
1484 | get_cred(id->creds); | |
dfead8a8 JA |
1485 | req->work.flags |= IO_WQ_WORK_CREDS; |
1486 | } | |
4ea33a97 JA |
1487 | #ifdef CONFIG_AUDIT |
1488 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1489 | current->sessionid != id->sessionid) | |
1490 | return false; | |
1491 | #endif | |
dfead8a8 JA |
1492 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1493 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1494 | if (current->fs != id->fs) |
1495 | return false; | |
1496 | spin_lock(&id->fs->lock); | |
1497 | if (!id->fs->in_exec) { | |
1498 | id->fs->users++; | |
dfead8a8 | 1499 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1500 | } else { |
1501 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1502 | } | |
1503 | spin_unlock(¤t->fs->lock); | |
1504 | } | |
1e6fa521 JA |
1505 | |
1506 | return true; | |
1507 | } | |
1508 | ||
1509 | static void io_prep_async_work(struct io_kiocb *req) | |
1510 | { | |
1511 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 | 1512 | struct io_ring_ctx *ctx = req->ctx; |
5c3462cf | 1513 | struct io_identity *id; |
1e6fa521 JA |
1514 | |
1515 | io_req_init_async(req); | |
5c3462cf | 1516 | id = req->work.identity; |
1e6fa521 | 1517 | |
feaadc4f PB |
1518 | if (req->flags & REQ_F_FORCE_ASYNC) |
1519 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1520 | ||
1e6fa521 JA |
1521 | if (req->flags & REQ_F_ISREG) { |
1522 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1523 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1524 | } else { | |
1525 | if (def->unbound_nonreg_file) | |
1526 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1527 | } | |
1528 | ||
1529 | /* ->mm can never change on us */ | |
1530 | if (!(req->work.flags & IO_WQ_WORK_MM) && | |
1531 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1532 | mmgrab(id->mm); | |
1533 | req->work.flags |= IO_WQ_WORK_MM; | |
1534 | } | |
1535 | ||
1536 | /* if we fail grabbing identity, we must COW, regrab, and retry */ | |
1537 | if (io_grab_identity(req)) | |
1538 | return; | |
1539 | ||
1540 | if (!io_identity_cow(req)) | |
1541 | return; | |
1542 | ||
1543 | /* can't fail at this point */ | |
1544 | if (!io_grab_identity(req)) | |
1545 | WARN_ON(1); | |
561fb04a | 1546 | } |
cccf0ee8 | 1547 | |
cbdcb435 | 1548 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1549 | { |
cbdcb435 | 1550 | struct io_kiocb *cur; |
54a91f3b | 1551 | |
f2f87370 PB |
1552 | io_for_each_link(cur, req) |
1553 | io_prep_async_work(cur); | |
561fb04a JA |
1554 | } |
1555 | ||
7271ef3a | 1556 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1557 | { |
a197f664 | 1558 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1559 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1560 | |
8766dd51 PB |
1561 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1562 | &req->work, req->flags); | |
1563 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1564 | return link; |
18d9be1a JA |
1565 | } |
1566 | ||
cbdcb435 PB |
1567 | static void io_queue_async_work(struct io_kiocb *req) |
1568 | { | |
7271ef3a JA |
1569 | struct io_kiocb *link; |
1570 | ||
cbdcb435 PB |
1571 | /* init ->work of the whole link before punting */ |
1572 | io_prep_async_link(req); | |
7271ef3a JA |
1573 | link = __io_queue_async_work(req); |
1574 | ||
1575 | if (link) | |
1576 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1577 | } |
1578 | ||
5262f567 JA |
1579 | static void io_kill_timeout(struct io_kiocb *req) |
1580 | { | |
e8c2bc1f | 1581 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1582 | int ret; |
1583 | ||
e8c2bc1f | 1584 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1585 | if (ret != -1) { |
01cec8c1 PB |
1586 | atomic_set(&req->ctx->cq_timeouts, |
1587 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1588 | list_del_init(&req->timeout.list); |
78e19bbe | 1589 | io_cqring_fill_event(req, 0); |
216578e5 | 1590 | io_put_req_deferred(req, 1); |
5262f567 JA |
1591 | } |
1592 | } | |
1593 | ||
76e1b642 JA |
1594 | /* |
1595 | * Returns true if we found and killed one or more timeouts | |
1596 | */ | |
6b81928d PB |
1597 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1598 | struct files_struct *files) | |
5262f567 JA |
1599 | { |
1600 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1601 | int canceled = 0; |
5262f567 JA |
1602 | |
1603 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1604 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1605 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1606 | io_kill_timeout(req); |
76e1b642 JA |
1607 | canceled++; |
1608 | } | |
f3606e3a | 1609 | } |
5262f567 | 1610 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1611 | return canceled != 0; |
5262f567 JA |
1612 | } |
1613 | ||
04518945 | 1614 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1615 | { |
04518945 | 1616 | do { |
27dc8338 PB |
1617 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1618 | struct io_defer_entry, list); | |
7271ef3a | 1619 | struct io_kiocb *link; |
de0617e4 | 1620 | |
9cf7c104 | 1621 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1622 | break; |
27dc8338 | 1623 | list_del_init(&de->list); |
cbdcb435 | 1624 | /* punt-init is done before queueing for defer */ |
7271ef3a JA |
1625 | link = __io_queue_async_work(de->req); |
1626 | if (link) { | |
1627 | __io_queue_linked_timeout(link); | |
1628 | /* drop submission reference */ | |
216578e5 | 1629 | io_put_req_deferred(link, 1); |
7271ef3a | 1630 | } |
27dc8338 | 1631 | kfree(de); |
04518945 PB |
1632 | } while (!list_empty(&ctx->defer_list)); |
1633 | } | |
1634 | ||
360428f8 | 1635 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1636 | { |
360428f8 PB |
1637 | while (!list_empty(&ctx->timeout_list)) { |
1638 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, | |
135fcde8 | 1639 | struct io_kiocb, timeout.list); |
de0617e4 | 1640 | |
8eb7e2d0 | 1641 | if (io_is_timeout_noseq(req)) |
360428f8 | 1642 | break; |
bfe68a22 PB |
1643 | if (req->timeout.target_seq != ctx->cached_cq_tail |
1644 | - atomic_read(&ctx->cq_timeouts)) | |
360428f8 | 1645 | break; |
bfe68a22 | 1646 | |
135fcde8 | 1647 | list_del_init(&req->timeout.list); |
5262f567 | 1648 | io_kill_timeout(req); |
360428f8 PB |
1649 | } |
1650 | } | |
5262f567 | 1651 | |
360428f8 PB |
1652 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1653 | { | |
1654 | io_flush_timeouts(ctx); | |
de0617e4 JA |
1655 | __io_commit_cqring(ctx); |
1656 | ||
04518945 PB |
1657 | if (unlikely(!list_empty(&ctx->defer_list))) |
1658 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1659 | } |
1660 | ||
90554200 JA |
1661 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1662 | { | |
1663 | struct io_rings *r = ctx->rings; | |
1664 | ||
1665 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1666 | } | |
1667 | ||
2b188cc1 JA |
1668 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1669 | { | |
75b28aff | 1670 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1671 | unsigned tail; |
1672 | ||
1673 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
1674 | /* |
1675 | * writes to the cq entry need to come after reading head; the | |
1676 | * control dependency is enough as we're using WRITE_ONCE to | |
1677 | * fill the cq entry | |
1678 | */ | |
75b28aff | 1679 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
1680 | return NULL; |
1681 | ||
1682 | ctx->cached_cq_tail++; | |
75b28aff | 1683 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1684 | } |
1685 | ||
f2842ab5 JA |
1686 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1687 | { | |
f0b493e6 JA |
1688 | if (!ctx->cq_ev_fd) |
1689 | return false; | |
7e55a19c SG |
1690 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1691 | return false; | |
f2842ab5 JA |
1692 | if (!ctx->eventfd_async) |
1693 | return true; | |
b41e9852 | 1694 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1695 | } |
1696 | ||
b41e9852 | 1697 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 JA |
1698 | { |
1699 | if (waitqueue_active(&ctx->wait)) | |
1700 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1701 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1702 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1703 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 JA |
1704 | eventfd_signal(ctx->cq_ev_fd, 1); |
1705 | } | |
1706 | ||
46930143 PB |
1707 | static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
1708 | { | |
1709 | if (list_empty(&ctx->cq_overflow_list)) { | |
1710 | clear_bit(0, &ctx->sq_check_overflow); | |
1711 | clear_bit(0, &ctx->cq_check_overflow); | |
1712 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1713 | } | |
1714 | } | |
1715 | ||
c4a2ed72 | 1716 | /* Returns true if there are no backlogged entries after the flush */ |
e6c8aa9a JA |
1717 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1718 | struct task_struct *tsk, | |
1719 | struct files_struct *files) | |
1d7bb1d5 JA |
1720 | { |
1721 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1722 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1723 | struct io_uring_cqe *cqe; |
1d7bb1d5 JA |
1724 | unsigned long flags; |
1725 | LIST_HEAD(list); | |
1726 | ||
1727 | if (!force) { | |
1728 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
c4a2ed72 | 1729 | return true; |
1d7bb1d5 JA |
1730 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
1731 | rings->cq_ring_entries)) | |
c4a2ed72 | 1732 | return false; |
1d7bb1d5 JA |
1733 | } |
1734 | ||
1735 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1736 | ||
1737 | /* if force is set, the ring is going away. always drop after that */ | |
1738 | if (force) | |
69b3e546 | 1739 | ctx->cq_overflow_flushed = 1; |
1d7bb1d5 | 1740 | |
c4a2ed72 | 1741 | cqe = NULL; |
e6c8aa9a | 1742 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1743 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1744 | continue; |
1745 | ||
1d7bb1d5 JA |
1746 | cqe = io_get_cqring(ctx); |
1747 | if (!cqe && !force) | |
1748 | break; | |
1749 | ||
40d8ddd4 | 1750 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1751 | if (cqe) { |
1752 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1753 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1754 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1755 | } else { |
2c3bac6d | 1756 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1757 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1758 | ctx->cached_cq_overflow); |
1d7bb1d5 JA |
1759 | } |
1760 | } | |
1761 | ||
1762 | io_commit_cqring(ctx); | |
46930143 PB |
1763 | io_cqring_mark_overflow(ctx); |
1764 | ||
1d7bb1d5 JA |
1765 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1766 | io_cqring_ev_posted(ctx); | |
1767 | ||
1768 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1769 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1770 | list_del(&req->compl.list); | |
ec9c02ad | 1771 | io_put_req(req); |
1d7bb1d5 | 1772 | } |
c4a2ed72 JA |
1773 | |
1774 | return cqe != NULL; | |
1d7bb1d5 JA |
1775 | } |
1776 | ||
bcda7baa | 1777 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1778 | { |
78e19bbe | 1779 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1780 | struct io_uring_cqe *cqe; |
1781 | ||
78e19bbe | 1782 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1783 | |
2b188cc1 JA |
1784 | /* |
1785 | * If we can't get a cq entry, userspace overflowed the | |
1786 | * submission (by quite a lot). Increment the overflow count in | |
1787 | * the ring. | |
1788 | */ | |
1789 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1790 | if (likely(cqe)) { |
78e19bbe | 1791 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1792 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1793 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1794 | } else if (ctx->cq_overflow_flushed || |
1795 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1796 | /* |
1797 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1798 | * then we cannot store the request for later flushing, we need | |
1799 | * to drop it on the floor. | |
1800 | */ | |
2c3bac6d PB |
1801 | ctx->cached_cq_overflow++; |
1802 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1803 | } else { |
ad3eb2c8 JA |
1804 | if (list_empty(&ctx->cq_overflow_list)) { |
1805 | set_bit(0, &ctx->sq_check_overflow); | |
1806 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1807 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1808 | } |
40d8ddd4 | 1809 | io_clean_op(req); |
1d7bb1d5 | 1810 | req->result = res; |
0f7e466b | 1811 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1812 | refcount_inc(&req->refs); |
1813 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1814 | } |
1815 | } | |
1816 | ||
bcda7baa JA |
1817 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1818 | { | |
1819 | __io_cqring_fill_event(req, res, 0); | |
1820 | } | |
1821 | ||
e1e16097 | 1822 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1823 | { |
78e19bbe | 1824 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1825 | unsigned long flags; |
1826 | ||
1827 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1828 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1829 | io_commit_cqring(ctx); |
1830 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1831 | ||
8c838788 | 1832 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1833 | } |
1834 | ||
229a7b63 | 1835 | static void io_submit_flush_completions(struct io_comp_state *cs) |
bcda7baa | 1836 | { |
229a7b63 JA |
1837 | struct io_ring_ctx *ctx = cs->ctx; |
1838 | ||
1839 | spin_lock_irq(&ctx->completion_lock); | |
1840 | while (!list_empty(&cs->list)) { | |
1841 | struct io_kiocb *req; | |
1842 | ||
3ca405eb PB |
1843 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
1844 | list_del(&req->compl.list); | |
0f7e466b | 1845 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
216578e5 PB |
1846 | |
1847 | /* | |
1848 | * io_free_req() doesn't care about completion_lock unless one | |
1849 | * of these flags is set. REQ_F_WORK_INITIALIZED is in the list | |
1850 | * because of a potential deadlock with req->work.fs->lock | |
1851 | */ | |
1852 | if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT | |
1853 | |REQ_F_WORK_INITIALIZED)) { | |
229a7b63 JA |
1854 | spin_unlock_irq(&ctx->completion_lock); |
1855 | io_put_req(req); | |
1856 | spin_lock_irq(&ctx->completion_lock); | |
216578e5 PB |
1857 | } else { |
1858 | io_put_req(req); | |
229a7b63 JA |
1859 | } |
1860 | } | |
1861 | io_commit_cqring(ctx); | |
1862 | spin_unlock_irq(&ctx->completion_lock); | |
1863 | ||
1864 | io_cqring_ev_posted(ctx); | |
1865 | cs->nr = 0; | |
1866 | } | |
1867 | ||
1868 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, | |
1869 | struct io_comp_state *cs) | |
1870 | { | |
1871 | if (!cs) { | |
1872 | io_cqring_add_event(req, res, cflags); | |
1873 | io_put_req(req); | |
1874 | } else { | |
3ca405eb | 1875 | io_clean_op(req); |
229a7b63 | 1876 | req->result = res; |
0f7e466b | 1877 | req->compl.cflags = cflags; |
3ca405eb | 1878 | list_add_tail(&req->compl.list, &cs->list); |
229a7b63 JA |
1879 | if (++cs->nr >= 32) |
1880 | io_submit_flush_completions(cs); | |
1881 | } | |
e1e16097 JA |
1882 | } |
1883 | ||
1884 | static void io_req_complete(struct io_kiocb *req, long res) | |
bcda7baa | 1885 | { |
229a7b63 | 1886 | __io_req_complete(req, res, 0, NULL); |
bcda7baa JA |
1887 | } |
1888 | ||
0ddf92e8 JA |
1889 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1890 | { | |
1891 | return req == (struct io_kiocb *) | |
1892 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1893 | } | |
1894 | ||
1895 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1896 | { | |
1897 | struct io_kiocb *req; | |
1898 | ||
1899 | req = ctx->fallback_req; | |
dd461af6 | 1900 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
0ddf92e8 JA |
1901 | return req; |
1902 | ||
1903 | return NULL; | |
1904 | } | |
1905 | ||
0553b8bd PB |
1906 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
1907 | struct io_submit_state *state) | |
2b188cc1 | 1908 | { |
f6b6c7d6 | 1909 | if (!state->free_reqs) { |
291b2821 | 1910 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1911 | size_t sz; |
1912 | int ret; | |
1913 | ||
1914 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1915 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1916 | ||
1917 | /* | |
1918 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1919 | * retry single alloc to be on the safe side. | |
1920 | */ | |
1921 | if (unlikely(ret <= 0)) { | |
1922 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1923 | if (!state->reqs[0]) | |
0ddf92e8 | 1924 | goto fallback; |
fd6fab2c JA |
1925 | ret = 1; |
1926 | } | |
291b2821 | 1927 | state->free_reqs = ret; |
2b188cc1 JA |
1928 | } |
1929 | ||
291b2821 PB |
1930 | state->free_reqs--; |
1931 | return state->reqs[state->free_reqs]; | |
0ddf92e8 | 1932 | fallback: |
0553b8bd | 1933 | return io_get_fallback_req(ctx); |
2b188cc1 JA |
1934 | } |
1935 | ||
8da11c19 PB |
1936 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1937 | bool fixed) | |
1938 | { | |
36f72fe2 | 1939 | if (!fixed) |
8da11c19 PB |
1940 | fput(file); |
1941 | } | |
1942 | ||
4edf20f9 | 1943 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1944 | { |
3ca405eb | 1945 | io_clean_op(req); |
929a3af9 | 1946 | |
e8c2bc1f JA |
1947 | if (req->async_data) |
1948 | kfree(req->async_data); | |
8da11c19 PB |
1949 | if (req->file) |
1950 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
36f72fe2 PB |
1951 | if (req->fixed_file_refs) |
1952 | percpu_ref_put(req->fixed_file_refs); | |
4edf20f9 | 1953 | io_req_clean_work(req); |
e65ef56d JA |
1954 | } |
1955 | ||
216578e5 | 1956 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1957 | { |
0f212204 | 1958 | struct io_uring_task *tctx = req->task->io_uring; |
51a4cc11 | 1959 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1960 | |
216578e5 | 1961 | io_dismantle_req(req); |
c6ca97b3 | 1962 | |
d8a6df10 | 1963 | percpu_counter_dec(&tctx->inflight); |
fdaf083c | 1964 | if (atomic_read(&tctx->in_idle)) |
0f212204 | 1965 | wake_up(&tctx->wait); |
e3bc8e9d JA |
1966 | put_task_struct(req->task); |
1967 | ||
b1e50e54 PB |
1968 | if (likely(!io_is_fallback_req(req))) |
1969 | kmem_cache_free(req_cachep, req); | |
1970 | else | |
ecfc5177 PB |
1971 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
1972 | percpu_ref_put(&ctx->refs); | |
e65ef56d JA |
1973 | } |
1974 | ||
f2f87370 PB |
1975 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1976 | { | |
1977 | struct io_kiocb *nxt = req->link; | |
1978 | ||
1979 | req->link = nxt->link; | |
1980 | nxt->link = NULL; | |
1981 | } | |
1982 | ||
c9abd7ad | 1983 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1984 | { |
a197f664 | 1985 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1986 | struct io_kiocb *link; |
c9abd7ad PB |
1987 | bool cancelled = false; |
1988 | unsigned long flags; | |
7c86ffee | 1989 | |
c9abd7ad | 1990 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1991 | link = req->link; |
1992 | ||
900fad45 PB |
1993 | /* |
1994 | * Can happen if a linked timeout fired and link had been like | |
1995 | * req -> link t-out -> link t-out [-> ...] | |
1996 | */ | |
c9abd7ad PB |
1997 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1998 | struct io_timeout_data *io = link->async_data; | |
1999 | int ret; | |
7c86ffee | 2000 | |
f2f87370 | 2001 | io_remove_next_linked(req); |
90cd7e42 | 2002 | link->timeout.head = NULL; |
c9abd7ad PB |
2003 | ret = hrtimer_try_to_cancel(&io->timer); |
2004 | if (ret != -1) { | |
2005 | io_cqring_fill_event(link, -ECANCELED); | |
2006 | io_commit_cqring(ctx); | |
2007 | cancelled = true; | |
2008 | } | |
2009 | } | |
7c86ffee | 2010 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2011 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2012 | |
c9abd7ad | 2013 | if (cancelled) { |
7c86ffee | 2014 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2015 | io_put_req(link); |
2016 | } | |
7c86ffee PB |
2017 | } |
2018 | ||
9e645e11 | 2019 | |
d148ca4b | 2020 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2021 | { |
f2f87370 | 2022 | struct io_kiocb *link, *nxt; |
2665abfd | 2023 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2024 | unsigned long flags; |
9e645e11 | 2025 | |
d148ca4b | 2026 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2027 | link = req->link; |
2028 | req->link = NULL; | |
9e645e11 | 2029 | |
f2f87370 PB |
2030 | while (link) { |
2031 | nxt = link->link; | |
2032 | link->link = NULL; | |
2665abfd | 2033 | |
f2f87370 | 2034 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2035 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2036 | |
2037 | /* | |
2038 | * It's ok to free under spinlock as they're not linked anymore, | |
2039 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2040 | * work.fs->lock. | |
2041 | */ | |
2042 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2043 | io_put_req_deferred(link, 2); | |
2044 | else | |
2045 | io_double_put_req(link); | |
f2f87370 | 2046 | link = nxt; |
9e645e11 | 2047 | } |
2665abfd | 2048 | io_commit_cqring(ctx); |
216578e5 | 2049 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2050 | |
2665abfd | 2051 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2052 | } |
2053 | ||
3fa5e0f3 | 2054 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2055 | { |
7c86ffee PB |
2056 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2057 | io_kill_linked_timeout(req); | |
944e58bf | 2058 | |
9e645e11 JA |
2059 | /* |
2060 | * If LINK is set, we have dependent requests in this chain. If we | |
2061 | * didn't fail this request, queue the first one up, moving any other | |
2062 | * dependencies to the next request. In case of failure, fail the rest | |
2063 | * of the chain. | |
2064 | */ | |
f2f87370 PB |
2065 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2066 | struct io_kiocb *nxt = req->link; | |
2067 | ||
2068 | req->link = NULL; | |
2069 | return nxt; | |
2070 | } | |
9b5f7bd9 PB |
2071 | io_fail_links(req); |
2072 | return NULL; | |
4d7dd462 | 2073 | } |
9e645e11 | 2074 | |
f2f87370 | 2075 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2076 | { |
f2f87370 | 2077 | if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT))) |
3fa5e0f3 PB |
2078 | return NULL; |
2079 | return __io_req_find_next(req); | |
2080 | } | |
2081 | ||
87c4311f | 2082 | static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok) |
c2c4c83c JA |
2083 | { |
2084 | struct task_struct *tsk = req->task; | |
2085 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2086 | enum task_work_notify_mode notify; |
2087 | int ret; | |
c2c4c83c | 2088 | |
6200b0ae JA |
2089 | if (tsk->flags & PF_EXITING) |
2090 | return -ESRCH; | |
2091 | ||
c2c4c83c | 2092 | /* |
0ba9c9ed JA |
2093 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2094 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2095 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2096 | * will do the job. | |
c2c4c83c | 2097 | */ |
91989c70 | 2098 | notify = TWA_NONE; |
fd7d6de2 | 2099 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok) |
c2c4c83c JA |
2100 | notify = TWA_SIGNAL; |
2101 | ||
87c4311f | 2102 | ret = task_work_add(tsk, &req->task_work, notify); |
c2c4c83c JA |
2103 | if (!ret) |
2104 | wake_up_process(tsk); | |
0ba9c9ed | 2105 | |
c2c4c83c JA |
2106 | return ret; |
2107 | } | |
2108 | ||
c40f6379 JA |
2109 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2110 | { | |
2111 | struct io_ring_ctx *ctx = req->ctx; | |
2112 | ||
2113 | spin_lock_irq(&ctx->completion_lock); | |
2114 | io_cqring_fill_event(req, error); | |
2115 | io_commit_cqring(ctx); | |
2116 | spin_unlock_irq(&ctx->completion_lock); | |
2117 | ||
2118 | io_cqring_ev_posted(ctx); | |
2119 | req_set_fail_links(req); | |
2120 | io_double_put_req(req); | |
2121 | } | |
2122 | ||
2123 | static void io_req_task_cancel(struct callback_head *cb) | |
2124 | { | |
2125 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2126 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2127 | |
2128 | __io_req_task_cancel(req, -ECANCELED); | |
87ceb6a6 | 2129 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2130 | } |
2131 | ||
2132 | static void __io_req_task_submit(struct io_kiocb *req) | |
2133 | { | |
2134 | struct io_ring_ctx *ctx = req->ctx; | |
2135 | ||
1a38ffc9 PB |
2136 | if (!__io_sq_thread_acquire_mm(ctx) && |
2137 | !__io_sq_thread_acquire_files(ctx)) { | |
c40f6379 | 2138 | mutex_lock(&ctx->uring_lock); |
c1379e24 | 2139 | __io_queue_sqe(req, NULL); |
c40f6379 JA |
2140 | mutex_unlock(&ctx->uring_lock); |
2141 | } else { | |
2142 | __io_req_task_cancel(req, -EFAULT); | |
2143 | } | |
2144 | } | |
2145 | ||
2146 | static void io_req_task_submit(struct callback_head *cb) | |
2147 | { | |
2148 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 2149 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2150 | |
2151 | __io_req_task_submit(req); | |
6d816e08 | 2152 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2153 | } |
2154 | ||
2155 | static void io_req_task_queue(struct io_kiocb *req) | |
2156 | { | |
c40f6379 JA |
2157 | int ret; |
2158 | ||
2159 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 2160 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 2161 | |
87c4311f | 2162 | ret = io_req_task_work_add(req, true); |
c40f6379 | 2163 | if (unlikely(ret)) { |
c2c4c83c JA |
2164 | struct task_struct *tsk; |
2165 | ||
c40f6379 JA |
2166 | init_task_work(&req->task_work, io_req_task_cancel); |
2167 | tsk = io_wq_get_task(req->ctx->io_wq); | |
91989c70 | 2168 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 2169 | wake_up_process(tsk); |
c40f6379 | 2170 | } |
c40f6379 JA |
2171 | } |
2172 | ||
f2f87370 | 2173 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2174 | { |
9b5f7bd9 | 2175 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2176 | |
2177 | if (nxt) | |
906a8c3f | 2178 | io_req_task_queue(nxt); |
c69f8dbe JL |
2179 | } |
2180 | ||
c3524383 | 2181 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2182 | { |
c3524383 PB |
2183 | io_queue_next(req); |
2184 | __io_free_req(req); | |
2185 | } | |
8766dd51 | 2186 | |
2d6500d4 PB |
2187 | struct req_batch { |
2188 | void *reqs[IO_IOPOLL_BATCH]; | |
2189 | int to_free; | |
7a743e22 | 2190 | |
5af1d13e PB |
2191 | struct task_struct *task; |
2192 | int task_refs; | |
2d6500d4 PB |
2193 | }; |
2194 | ||
5af1d13e PB |
2195 | static inline void io_init_req_batch(struct req_batch *rb) |
2196 | { | |
2197 | rb->to_free = 0; | |
2198 | rb->task_refs = 0; | |
2199 | rb->task = NULL; | |
2200 | } | |
2201 | ||
2d6500d4 PB |
2202 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
2203 | struct req_batch *rb) | |
2204 | { | |
2205 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
2206 | percpu_ref_put_many(&ctx->refs, rb->to_free); | |
2207 | rb->to_free = 0; | |
2208 | } | |
2209 | ||
2210 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
2211 | struct req_batch *rb) | |
2212 | { | |
2213 | if (rb->to_free) | |
2214 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e | 2215 | if (rb->task) { |
d8a6df10 JA |
2216 | struct io_uring_task *tctx = rb->task->io_uring; |
2217 | ||
2218 | percpu_counter_sub(&tctx->inflight, rb->task_refs); | |
5af1d13e PB |
2219 | put_task_struct_many(rb->task, rb->task_refs); |
2220 | rb->task = NULL; | |
2221 | } | |
2d6500d4 PB |
2222 | } |
2223 | ||
2224 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
2225 | { | |
2226 | if (unlikely(io_is_fallback_req(req))) { | |
2227 | io_free_req(req); | |
2228 | return; | |
2229 | } | |
f2f87370 | 2230 | io_queue_next(req); |
2d6500d4 | 2231 | |
e3bc8e9d | 2232 | if (req->task != rb->task) { |
0f212204 | 2233 | if (rb->task) { |
d8a6df10 JA |
2234 | struct io_uring_task *tctx = rb->task->io_uring; |
2235 | ||
2236 | percpu_counter_sub(&tctx->inflight, rb->task_refs); | |
e3bc8e9d | 2237 | put_task_struct_many(rb->task, rb->task_refs); |
5af1d13e | 2238 | } |
e3bc8e9d JA |
2239 | rb->task = req->task; |
2240 | rb->task_refs = 0; | |
5af1d13e | 2241 | } |
e3bc8e9d | 2242 | rb->task_refs++; |
5af1d13e | 2243 | |
4edf20f9 | 2244 | io_dismantle_req(req); |
2d6500d4 PB |
2245 | rb->reqs[rb->to_free++] = req; |
2246 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
2247 | __io_req_free_batch_flush(req->ctx, rb); | |
7a743e22 PB |
2248 | } |
2249 | ||
ba816ad6 JA |
2250 | /* |
2251 | * Drop reference to request, return next in chain (if there is one) if this | |
2252 | * was the last reference to this request. | |
2253 | */ | |
9b5f7bd9 | 2254 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2255 | { |
9b5f7bd9 PB |
2256 | struct io_kiocb *nxt = NULL; |
2257 | ||
2a44f467 | 2258 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2259 | nxt = io_req_find_next(req); |
4d7dd462 | 2260 | __io_free_req(req); |
2a44f467 | 2261 | } |
9b5f7bd9 | 2262 | return nxt; |
2b188cc1 JA |
2263 | } |
2264 | ||
e65ef56d JA |
2265 | static void io_put_req(struct io_kiocb *req) |
2266 | { | |
2267 | if (refcount_dec_and_test(&req->refs)) | |
2268 | io_free_req(req); | |
2b188cc1 JA |
2269 | } |
2270 | ||
216578e5 PB |
2271 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2272 | { | |
2273 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2274 | ||
2275 | io_free_req(req); | |
2276 | } | |
2277 | ||
2278 | static void io_free_req_deferred(struct io_kiocb *req) | |
2279 | { | |
2280 | int ret; | |
2281 | ||
2282 | init_task_work(&req->task_work, io_put_req_deferred_cb); | |
2283 | ret = io_req_task_work_add(req, true); | |
2284 | if (unlikely(ret)) { | |
2285 | struct task_struct *tsk; | |
2286 | ||
2287 | tsk = io_wq_get_task(req->ctx->io_wq); | |
91989c70 | 2288 | task_work_add(tsk, &req->task_work, TWA_NONE); |
216578e5 PB |
2289 | wake_up_process(tsk); |
2290 | } | |
2291 | } | |
2292 | ||
2293 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2294 | { | |
2295 | if (refcount_sub_and_test(refs, &req->refs)) | |
2296 | io_free_req_deferred(req); | |
2297 | } | |
2298 | ||
f4db7182 | 2299 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
7a743e22 | 2300 | { |
6df1db6b | 2301 | struct io_kiocb *nxt; |
f4db7182 | 2302 | |
7a743e22 | 2303 | /* |
f4db7182 PB |
2304 | * A ref is owned by io-wq in which context we're. So, if that's the |
2305 | * last one, it's safe to steal next work. False negatives are Ok, | |
2306 | * it just will be re-punted async in io_put_work() | |
7a743e22 | 2307 | */ |
f4db7182 PB |
2308 | if (refcount_read(&req->refs) != 1) |
2309 | return NULL; | |
7a743e22 | 2310 | |
9b5f7bd9 | 2311 | nxt = io_req_find_next(req); |
6df1db6b | 2312 | return nxt ? &nxt->work : NULL; |
7a743e22 PB |
2313 | } |
2314 | ||
978db57e JA |
2315 | static void io_double_put_req(struct io_kiocb *req) |
2316 | { | |
2317 | /* drop both submit and complete references */ | |
2318 | if (refcount_sub_and_test(2, &req->refs)) | |
2319 | io_free_req(req); | |
2320 | } | |
2321 | ||
1d7bb1d5 | 2322 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 2323 | { |
84f97dc2 JA |
2324 | struct io_rings *rings = ctx->rings; |
2325 | ||
ad3eb2c8 JA |
2326 | if (test_bit(0, &ctx->cq_check_overflow)) { |
2327 | /* | |
2328 | * noflush == true is from the waitqueue handler, just ensure | |
2329 | * we wake up the task, and the next invocation will flush the | |
2330 | * entries. We cannot safely to it from here. | |
2331 | */ | |
2332 | if (noflush && !list_empty(&ctx->cq_overflow_list)) | |
2333 | return -1U; | |
1d7bb1d5 | 2334 | |
e6c8aa9a | 2335 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
ad3eb2c8 | 2336 | } |
1d7bb1d5 | 2337 | |
a3a0e43f JA |
2338 | /* See comment at the top of this file */ |
2339 | smp_rmb(); | |
ad3eb2c8 | 2340 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
2341 | } |
2342 | ||
fb5ccc98 PB |
2343 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2344 | { | |
2345 | struct io_rings *rings = ctx->rings; | |
2346 | ||
2347 | /* make sure SQ entry isn't read before tail */ | |
2348 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2349 | } | |
2350 | ||
8ff069bf | 2351 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2352 | { |
8ff069bf | 2353 | unsigned int cflags; |
e94f141b | 2354 | |
bcda7baa JA |
2355 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2356 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2357 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2358 | kfree(kbuf); |
2359 | return cflags; | |
e94f141b JA |
2360 | } |
2361 | ||
8ff069bf | 2362 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2363 | { |
4d954c25 | 2364 | struct io_buffer *kbuf; |
bcda7baa | 2365 | |
4d954c25 | 2366 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2367 | return io_put_kbuf(req, kbuf); |
2368 | } | |
2369 | ||
4c6e277c JA |
2370 | static inline bool io_run_task_work(void) |
2371 | { | |
6200b0ae JA |
2372 | /* |
2373 | * Not safe to run on exiting task, and the task_work handling will | |
2374 | * not add work to such a task. | |
2375 | */ | |
2376 | if (unlikely(current->flags & PF_EXITING)) | |
2377 | return false; | |
4c6e277c JA |
2378 | if (current->task_works) { |
2379 | __set_current_state(TASK_RUNNING); | |
2380 | task_work_run(); | |
2381 | return true; | |
2382 | } | |
2383 | ||
2384 | return false; | |
bcda7baa JA |
2385 | } |
2386 | ||
bbde017a XW |
2387 | static void io_iopoll_queue(struct list_head *again) |
2388 | { | |
2389 | struct io_kiocb *req; | |
2390 | ||
2391 | do { | |
d21ffe7e PB |
2392 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2393 | list_del(&req->inflight_entry); | |
81b68a5c | 2394 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
bbde017a XW |
2395 | } while (!list_empty(again)); |
2396 | } | |
2397 | ||
def596e9 JA |
2398 | /* |
2399 | * Find and free completed poll iocbs | |
2400 | */ | |
2401 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2402 | struct list_head *done) | |
2403 | { | |
8237e045 | 2404 | struct req_batch rb; |
def596e9 | 2405 | struct io_kiocb *req; |
bbde017a XW |
2406 | LIST_HEAD(again); |
2407 | ||
2408 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2409 | smp_rmb(); | |
def596e9 | 2410 | |
5af1d13e | 2411 | io_init_req_batch(&rb); |
def596e9 | 2412 | while (!list_empty(done)) { |
bcda7baa JA |
2413 | int cflags = 0; |
2414 | ||
d21ffe7e | 2415 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a | 2416 | if (READ_ONCE(req->result) == -EAGAIN) { |
56450c20 | 2417 | req->result = 0; |
bbde017a | 2418 | req->iopoll_completed = 0; |
d21ffe7e | 2419 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2420 | continue; |
2421 | } | |
d21ffe7e | 2422 | list_del(&req->inflight_entry); |
def596e9 | 2423 | |
bcda7baa | 2424 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2425 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2426 | |
2427 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2428 | (*nr_events)++; |
2429 | ||
c3524383 | 2430 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2431 | io_req_free_batch(&rb, req); |
def596e9 | 2432 | } |
def596e9 | 2433 | |
09bb8394 | 2434 | io_commit_cqring(ctx); |
32b2244a XW |
2435 | if (ctx->flags & IORING_SETUP_SQPOLL) |
2436 | io_cqring_ev_posted(ctx); | |
2d6500d4 | 2437 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2438 | |
bbde017a XW |
2439 | if (!list_empty(&again)) |
2440 | io_iopoll_queue(&again); | |
581f9810 BM |
2441 | } |
2442 | ||
def596e9 JA |
2443 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2444 | long min) | |
2445 | { | |
2446 | struct io_kiocb *req, *tmp; | |
2447 | LIST_HEAD(done); | |
2448 | bool spin; | |
2449 | int ret; | |
2450 | ||
2451 | /* | |
2452 | * Only spin for completions if we don't have multiple devices hanging | |
2453 | * off our complete list, and we're under the requested amount. | |
2454 | */ | |
2455 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2456 | ||
2457 | ret = 0; | |
d21ffe7e | 2458 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2459 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2460 | |
2461 | /* | |
581f9810 BM |
2462 | * Move completed and retryable entries to our local lists. |
2463 | * If we find a request that requires polling, break out | |
2464 | * and complete those lists first, if we have entries there. | |
def596e9 | 2465 | */ |
65a6543d | 2466 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2467 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2468 | continue; |
2469 | } | |
2470 | if (!list_empty(&done)) | |
2471 | break; | |
2472 | ||
2473 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2474 | if (ret < 0) | |
2475 | break; | |
2476 | ||
3aadc23e PB |
2477 | /* iopoll may have completed current req */ |
2478 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2479 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2480 | |
def596e9 JA |
2481 | if (ret && spin) |
2482 | spin = false; | |
2483 | ret = 0; | |
2484 | } | |
2485 | ||
2486 | if (!list_empty(&done)) | |
2487 | io_iopoll_complete(ctx, nr_events, &done); | |
2488 | ||
2489 | return ret; | |
2490 | } | |
2491 | ||
2492 | /* | |
d195a66e | 2493 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2494 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2495 | * as a non-spinning completion check. | |
2496 | */ | |
2497 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2498 | long min) | |
2499 | { | |
540e32a0 | 2500 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2501 | int ret; |
2502 | ||
2503 | ret = io_do_iopoll(ctx, nr_events, min); | |
2504 | if (ret < 0) | |
2505 | return ret; | |
eba0a4dd | 2506 | if (*nr_events >= min) |
def596e9 JA |
2507 | return 0; |
2508 | } | |
2509 | ||
2510 | return 1; | |
2511 | } | |
2512 | ||
2513 | /* | |
2514 | * We can't just wait for polled events to come to us, we have to actively | |
2515 | * find and complete them. | |
2516 | */ | |
b2edc0a7 | 2517 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2518 | { |
2519 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2520 | return; | |
2521 | ||
2522 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2523 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2524 | unsigned int nr_events = 0; |
2525 | ||
b2edc0a7 | 2526 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2527 | |
b2edc0a7 PB |
2528 | /* let it sleep and repeat later if can't complete a request */ |
2529 | if (nr_events == 0) | |
2530 | break; | |
08f5439f JA |
2531 | /* |
2532 | * Ensure we allow local-to-the-cpu processing to take place, | |
2533 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2534 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2535 | */ |
3fcee5a6 PB |
2536 | if (need_resched()) { |
2537 | mutex_unlock(&ctx->uring_lock); | |
2538 | cond_resched(); | |
2539 | mutex_lock(&ctx->uring_lock); | |
2540 | } | |
def596e9 JA |
2541 | } |
2542 | mutex_unlock(&ctx->uring_lock); | |
2543 | } | |
2544 | ||
7668b92a | 2545 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2546 | { |
7668b92a | 2547 | unsigned int nr_events = 0; |
2b2ed975 | 2548 | int iters = 0, ret = 0; |
500f9fba | 2549 | |
c7849be9 XW |
2550 | /* |
2551 | * We disallow the app entering submit/complete with polling, but we | |
2552 | * still need to lock the ring to prevent racing with polled issue | |
2553 | * that got punted to a workqueue. | |
2554 | */ | |
2555 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2556 | do { |
a3a0e43f JA |
2557 | /* |
2558 | * Don't enter poll loop if we already have events pending. | |
2559 | * If we do, we can potentially be spinning for commands that | |
2560 | * already triggered a CQE (eg in error). | |
2561 | */ | |
1d7bb1d5 | 2562 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
2563 | break; |
2564 | ||
500f9fba JA |
2565 | /* |
2566 | * If a submit got punted to a workqueue, we can have the | |
2567 | * application entering polling for a command before it gets | |
2568 | * issued. That app will hold the uring_lock for the duration | |
2569 | * of the poll right here, so we need to take a breather every | |
2570 | * now and then to ensure that the issue has a chance to add | |
2571 | * the poll to the issued list. Otherwise we can spin here | |
2572 | * forever, while the workqueue is stuck trying to acquire the | |
2573 | * very same mutex. | |
2574 | */ | |
2575 | if (!(++iters & 7)) { | |
2576 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2577 | io_run_task_work(); |
500f9fba JA |
2578 | mutex_lock(&ctx->uring_lock); |
2579 | } | |
2580 | ||
7668b92a | 2581 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2582 | if (ret <= 0) |
2583 | break; | |
2584 | ret = 0; | |
7668b92a | 2585 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2586 | |
500f9fba | 2587 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2588 | return ret; |
2589 | } | |
2590 | ||
491381ce | 2591 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2592 | { |
491381ce JA |
2593 | /* |
2594 | * Tell lockdep we inherited freeze protection from submission | |
2595 | * thread. | |
2596 | */ | |
2597 | if (req->flags & REQ_F_ISREG) { | |
2598 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2599 | |
491381ce | 2600 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2601 | } |
491381ce | 2602 | file_end_write(req->file); |
2b188cc1 JA |
2603 | } |
2604 | ||
a1d7c393 JA |
2605 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
2606 | struct io_comp_state *cs) | |
2b188cc1 | 2607 | { |
9adbd45d | 2608 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2609 | int cflags = 0; |
2b188cc1 | 2610 | |
491381ce JA |
2611 | if (kiocb->ki_flags & IOCB_WRITE) |
2612 | kiocb_end_write(req); | |
2b188cc1 | 2613 | |
4e88d6e7 JA |
2614 | if (res != req->result) |
2615 | req_set_fail_links(req); | |
bcda7baa | 2616 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2617 | cflags = io_put_rw_kbuf(req); |
a1d7c393 | 2618 | __io_req_complete(req, res, cflags, cs); |
ba816ad6 JA |
2619 | } |
2620 | ||
b63534c4 JA |
2621 | #ifdef CONFIG_BLOCK |
2622 | static bool io_resubmit_prep(struct io_kiocb *req, int error) | |
2623 | { | |
2624 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
2625 | ssize_t ret = -ECANCELED; | |
2626 | struct iov_iter iter; | |
2627 | int rw; | |
2628 | ||
2629 | if (error) { | |
2630 | ret = error; | |
2631 | goto end_req; | |
2632 | } | |
2633 | ||
2634 | switch (req->opcode) { | |
2635 | case IORING_OP_READV: | |
2636 | case IORING_OP_READ_FIXED: | |
2637 | case IORING_OP_READ: | |
2638 | rw = READ; | |
2639 | break; | |
2640 | case IORING_OP_WRITEV: | |
2641 | case IORING_OP_WRITE_FIXED: | |
2642 | case IORING_OP_WRITE: | |
2643 | rw = WRITE; | |
2644 | break; | |
2645 | default: | |
2646 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2647 | req->opcode); | |
2648 | goto end_req; | |
2649 | } | |
2650 | ||
e8c2bc1f | 2651 | if (!req->async_data) { |
8f3d7496 JA |
2652 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2653 | if (ret < 0) | |
2654 | goto end_req; | |
2655 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); | |
2656 | if (!ret) | |
2657 | return true; | |
2658 | kfree(iovec); | |
2659 | } else { | |
b63534c4 | 2660 | return true; |
8f3d7496 | 2661 | } |
b63534c4 | 2662 | end_req: |
b63534c4 | 2663 | req_set_fail_links(req); |
b63534c4 JA |
2664 | return false; |
2665 | } | |
b63534c4 JA |
2666 | #endif |
2667 | ||
2668 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2669 | { | |
2670 | #ifdef CONFIG_BLOCK | |
355afaeb | 2671 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2672 | int ret; |
2673 | ||
355afaeb JA |
2674 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2675 | return false; | |
b63534c4 JA |
2676 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
2677 | return false; | |
2678 | ||
28cea78a | 2679 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2680 | |
fdee946d JA |
2681 | if (io_resubmit_prep(req, ret)) { |
2682 | refcount_inc(&req->refs); | |
2683 | io_queue_async_work(req); | |
b63534c4 | 2684 | return true; |
fdee946d JA |
2685 | } |
2686 | ||
b63534c4 JA |
2687 | #endif |
2688 | return false; | |
2689 | } | |
2690 | ||
a1d7c393 JA |
2691 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2692 | struct io_comp_state *cs) | |
2693 | { | |
2694 | if (!io_rw_reissue(req, res)) | |
2695 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2696 | } |
2697 | ||
2698 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2699 | { | |
9adbd45d | 2700 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2701 | |
a1d7c393 | 2702 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2703 | } |
2704 | ||
def596e9 JA |
2705 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2706 | { | |
9adbd45d | 2707 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2708 | |
491381ce JA |
2709 | if (kiocb->ki_flags & IOCB_WRITE) |
2710 | kiocb_end_write(req); | |
def596e9 | 2711 | |
2d7d6792 | 2712 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2713 | req_set_fail_links(req); |
bbde017a XW |
2714 | |
2715 | WRITE_ONCE(req->result, res); | |
2716 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2717 | smp_wmb(); |
2718 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2719 | } |
2720 | ||
2721 | /* | |
2722 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2723 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2724 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2725 | * accessing the kiocb cookie. | |
2726 | */ | |
2e9dbe90 | 2727 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2728 | { |
2729 | struct io_ring_ctx *ctx = req->ctx; | |
2730 | ||
2731 | /* | |
2732 | * Track whether we have multiple files in our lists. This will impact | |
2733 | * how we do polling eventually, not spinning if we're on potentially | |
2734 | * different devices. | |
2735 | */ | |
540e32a0 | 2736 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2737 | ctx->poll_multi_file = false; |
2738 | } else if (!ctx->poll_multi_file) { | |
2739 | struct io_kiocb *list_req; | |
2740 | ||
540e32a0 | 2741 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2742 | inflight_entry); |
9adbd45d | 2743 | if (list_req->file != req->file) |
def596e9 JA |
2744 | ctx->poll_multi_file = true; |
2745 | } | |
2746 | ||
2747 | /* | |
2748 | * For fast devices, IO may have already completed. If it has, add | |
2749 | * it to the front so we find it first. | |
2750 | */ | |
65a6543d | 2751 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2752 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2753 | else |
d21ffe7e | 2754 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2755 | |
2e9dbe90 XW |
2756 | /* |
2757 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2758 | * task context or in io worker task context. If current task context is | |
2759 | * sq thread, we don't need to check whether should wake up sq thread. | |
2760 | */ | |
2761 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2762 | wq_has_sleeper(&ctx->sq_data->wait)) |
2763 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2764 | } |
2765 | ||
6e1271e6 | 2766 | static inline void __io_state_file_put(struct io_submit_state *state) |
9a56a232 | 2767 | { |
6e1271e6 PB |
2768 | fput_many(state->file, state->file_refs); |
2769 | state->file_refs = 0; | |
9f13c35b PB |
2770 | } |
2771 | ||
2772 | static inline void io_state_file_put(struct io_submit_state *state) | |
2773 | { | |
6e1271e6 | 2774 | if (state->file_refs) |
9f13c35b | 2775 | __io_state_file_put(state); |
9a56a232 JA |
2776 | } |
2777 | ||
2778 | /* | |
2779 | * Get as many references to a file as we have IOs left in this submission, | |
2780 | * assuming most submissions are for one file, or at least that each file | |
2781 | * has more than one submission. | |
2782 | */ | |
8da11c19 | 2783 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2784 | { |
2785 | if (!state) | |
2786 | return fget(fd); | |
2787 | ||
6e1271e6 | 2788 | if (state->file_refs) { |
9a56a232 | 2789 | if (state->fd == fd) { |
6e1271e6 | 2790 | state->file_refs--; |
9a56a232 JA |
2791 | return state->file; |
2792 | } | |
9f13c35b | 2793 | __io_state_file_put(state); |
9a56a232 JA |
2794 | } |
2795 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2796 | if (unlikely(!state->file)) |
9a56a232 JA |
2797 | return NULL; |
2798 | ||
2799 | state->fd = fd; | |
6e1271e6 | 2800 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2801 | return state->file; |
2802 | } | |
2803 | ||
4503b767 JA |
2804 | static bool io_bdev_nowait(struct block_device *bdev) |
2805 | { | |
2806 | #ifdef CONFIG_BLOCK | |
9ba0d0c8 | 2807 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2808 | #else |
2809 | return true; | |
2810 | #endif | |
2811 | } | |
2812 | ||
2b188cc1 JA |
2813 | /* |
2814 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2815 | * any file. For now, just ensure that anything potentially problematic is done | |
2816 | * inline. | |
2817 | */ | |
af197f50 | 2818 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2819 | { |
2820 | umode_t mode = file_inode(file)->i_mode; | |
2821 | ||
4503b767 JA |
2822 | if (S_ISBLK(mode)) { |
2823 | if (io_bdev_nowait(file->f_inode->i_bdev)) | |
2824 | return true; | |
2825 | return false; | |
2826 | } | |
2827 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2828 | return true; |
4503b767 JA |
2829 | if (S_ISREG(mode)) { |
2830 | if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
2831 | file->f_op != &io_uring_fops) | |
2832 | return true; | |
2833 | return false; | |
2834 | } | |
2b188cc1 | 2835 | |
c5b85625 JA |
2836 | /* any ->read/write should understand O_NONBLOCK */ |
2837 | if (file->f_flags & O_NONBLOCK) | |
2838 | return true; | |
2839 | ||
af197f50 JA |
2840 | if (!(file->f_mode & FMODE_NOWAIT)) |
2841 | return false; | |
2842 | ||
2843 | if (rw == READ) | |
2844 | return file->f_op->read_iter != NULL; | |
2845 | ||
2846 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2847 | } |
2848 | ||
a88fc400 | 2849 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2850 | { |
def596e9 | 2851 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2852 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2853 | unsigned ioprio; |
2854 | int ret; | |
2b188cc1 | 2855 | |
491381ce JA |
2856 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2857 | req->flags |= REQ_F_ISREG; | |
2858 | ||
2b188cc1 | 2859 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2860 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2861 | req->flags |= REQ_F_CUR_POS; | |
2862 | kiocb->ki_pos = req->file->f_pos; | |
2863 | } | |
2b188cc1 | 2864 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2865 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2866 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2867 | if (unlikely(ret)) | |
2868 | return ret; | |
2b188cc1 JA |
2869 | |
2870 | ioprio = READ_ONCE(sqe->ioprio); | |
2871 | if (ioprio) { | |
2872 | ret = ioprio_check_cap(ioprio); | |
2873 | if (ret) | |
09bb8394 | 2874 | return ret; |
2b188cc1 JA |
2875 | |
2876 | kiocb->ki_ioprio = ioprio; | |
2877 | } else | |
2878 | kiocb->ki_ioprio = get_current_ioprio(); | |
2879 | ||
8449eeda | 2880 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2881 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2882 | req->flags |= REQ_F_NOWAIT; |
2883 | ||
def596e9 | 2884 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2885 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2886 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2887 | return -EOPNOTSUPP; |
2b188cc1 | 2888 | |
def596e9 JA |
2889 | kiocb->ki_flags |= IOCB_HIPRI; |
2890 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2891 | req->iopoll_completed = 0; |
def596e9 | 2892 | } else { |
09bb8394 JA |
2893 | if (kiocb->ki_flags & IOCB_HIPRI) |
2894 | return -EINVAL; | |
def596e9 JA |
2895 | kiocb->ki_complete = io_complete_rw; |
2896 | } | |
9adbd45d | 2897 | |
3529d8c2 JA |
2898 | req->rw.addr = READ_ONCE(sqe->addr); |
2899 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2900 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2901 | return 0; |
2b188cc1 JA |
2902 | } |
2903 | ||
2904 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2905 | { | |
2906 | switch (ret) { | |
2907 | case -EIOCBQUEUED: | |
2908 | break; | |
2909 | case -ERESTARTSYS: | |
2910 | case -ERESTARTNOINTR: | |
2911 | case -ERESTARTNOHAND: | |
2912 | case -ERESTART_RESTARTBLOCK: | |
2913 | /* | |
2914 | * We can't just restart the syscall, since previously | |
2915 | * submitted sqes may already be in progress. Just fail this | |
2916 | * IO with EINTR. | |
2917 | */ | |
2918 | ret = -EINTR; | |
df561f66 | 2919 | fallthrough; |
2b188cc1 JA |
2920 | default: |
2921 | kiocb->ki_complete(kiocb, ret, 0); | |
2922 | } | |
2923 | } | |
2924 | ||
a1d7c393 JA |
2925 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2926 | struct io_comp_state *cs) | |
ba816ad6 | 2927 | { |
ba04291e | 2928 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2929 | struct io_async_rw *io = req->async_data; |
ba04291e | 2930 | |
227c0c96 | 2931 | /* add previously done IO, if any */ |
e8c2bc1f | 2932 | if (io && io->bytes_done > 0) { |
227c0c96 | 2933 | if (ret < 0) |
e8c2bc1f | 2934 | ret = io->bytes_done; |
227c0c96 | 2935 | else |
e8c2bc1f | 2936 | ret += io->bytes_done; |
227c0c96 JA |
2937 | } |
2938 | ||
ba04291e JA |
2939 | if (req->flags & REQ_F_CUR_POS) |
2940 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2941 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 2942 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
2943 | else |
2944 | io_rw_done(kiocb, ret); | |
2945 | } | |
2946 | ||
9adbd45d | 2947 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 2948 | struct iov_iter *iter) |
edafccee | 2949 | { |
9adbd45d JA |
2950 | struct io_ring_ctx *ctx = req->ctx; |
2951 | size_t len = req->rw.len; | |
edafccee | 2952 | struct io_mapped_ubuf *imu; |
4be1c615 | 2953 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2954 | size_t offset; |
2955 | u64 buf_addr; | |
2956 | ||
edafccee JA |
2957 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2958 | return -EFAULT; | |
edafccee JA |
2959 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2960 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2961 | buf_addr = req->rw.addr; |
edafccee JA |
2962 | |
2963 | /* overflow */ | |
2964 | if (buf_addr + len < buf_addr) | |
2965 | return -EFAULT; | |
2966 | /* not inside the mapped region */ | |
2967 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2968 | return -EFAULT; | |
2969 | ||
2970 | /* | |
2971 | * May not be a start of buffer, set size appropriately | |
2972 | * and advance us to the beginning. | |
2973 | */ | |
2974 | offset = buf_addr - imu->ubuf; | |
2975 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2976 | |
2977 | if (offset) { | |
2978 | /* | |
2979 | * Don't use iov_iter_advance() here, as it's really slow for | |
2980 | * using the latter parts of a big fixed buffer - it iterates | |
2981 | * over each segment manually. We can cheat a bit here, because | |
2982 | * we know that: | |
2983 | * | |
2984 | * 1) it's a BVEC iter, we set it up | |
2985 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2986 | * first and last bvec | |
2987 | * | |
2988 | * So just find our index, and adjust the iterator afterwards. | |
2989 | * If the offset is within the first bvec (or the whole first | |
2990 | * bvec, just use iov_iter_advance(). This makes it easier | |
2991 | * since we can just skip the first segment, which may not | |
2992 | * be PAGE_SIZE aligned. | |
2993 | */ | |
2994 | const struct bio_vec *bvec = imu->bvec; | |
2995 | ||
2996 | if (offset <= bvec->bv_len) { | |
2997 | iov_iter_advance(iter, offset); | |
2998 | } else { | |
2999 | unsigned long seg_skip; | |
3000 | ||
3001 | /* skip first vec */ | |
3002 | offset -= bvec->bv_len; | |
3003 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3004 | ||
3005 | iter->bvec = bvec + seg_skip; | |
3006 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3007 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3008 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3009 | } |
3010 | } | |
3011 | ||
5e559561 | 3012 | return len; |
edafccee JA |
3013 | } |
3014 | ||
bcda7baa JA |
3015 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3016 | { | |
3017 | if (needs_lock) | |
3018 | mutex_unlock(&ctx->uring_lock); | |
3019 | } | |
3020 | ||
3021 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3022 | { | |
3023 | /* | |
3024 | * "Normal" inline submissions always hold the uring_lock, since we | |
3025 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3026 | * The only exception is when we've detached the request and issue it | |
3027 | * from an async worker thread, grab the lock for that case. | |
3028 | */ | |
3029 | if (needs_lock) | |
3030 | mutex_lock(&ctx->uring_lock); | |
3031 | } | |
3032 | ||
3033 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3034 | int bgid, struct io_buffer *kbuf, | |
3035 | bool needs_lock) | |
3036 | { | |
3037 | struct io_buffer *head; | |
3038 | ||
3039 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3040 | return kbuf; | |
3041 | ||
3042 | io_ring_submit_lock(req->ctx, needs_lock); | |
3043 | ||
3044 | lockdep_assert_held(&req->ctx->uring_lock); | |
3045 | ||
3046 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3047 | if (head) { | |
3048 | if (!list_empty(&head->list)) { | |
3049 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3050 | list); | |
3051 | list_del(&kbuf->list); | |
3052 | } else { | |
3053 | kbuf = head; | |
3054 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3055 | } | |
3056 | if (*len > kbuf->len) | |
3057 | *len = kbuf->len; | |
3058 | } else { | |
3059 | kbuf = ERR_PTR(-ENOBUFS); | |
3060 | } | |
3061 | ||
3062 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3063 | ||
3064 | return kbuf; | |
3065 | } | |
3066 | ||
4d954c25 JA |
3067 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3068 | bool needs_lock) | |
3069 | { | |
3070 | struct io_buffer *kbuf; | |
4f4eeba8 | 3071 | u16 bgid; |
4d954c25 JA |
3072 | |
3073 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3074 | bgid = req->buf_index; |
4d954c25 JA |
3075 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3076 | if (IS_ERR(kbuf)) | |
3077 | return kbuf; | |
3078 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3079 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3080 | return u64_to_user_ptr(kbuf->addr); | |
3081 | } | |
3082 | ||
3083 | #ifdef CONFIG_COMPAT | |
3084 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3085 | bool needs_lock) | |
3086 | { | |
3087 | struct compat_iovec __user *uiov; | |
3088 | compat_ssize_t clen; | |
3089 | void __user *buf; | |
3090 | ssize_t len; | |
3091 | ||
3092 | uiov = u64_to_user_ptr(req->rw.addr); | |
3093 | if (!access_ok(uiov, sizeof(*uiov))) | |
3094 | return -EFAULT; | |
3095 | if (__get_user(clen, &uiov->iov_len)) | |
3096 | return -EFAULT; | |
3097 | if (clen < 0) | |
3098 | return -EINVAL; | |
3099 | ||
3100 | len = clen; | |
3101 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3102 | if (IS_ERR(buf)) | |
3103 | return PTR_ERR(buf); | |
3104 | iov[0].iov_base = buf; | |
3105 | iov[0].iov_len = (compat_size_t) len; | |
3106 | return 0; | |
3107 | } | |
3108 | #endif | |
3109 | ||
3110 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3111 | bool needs_lock) | |
3112 | { | |
3113 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3114 | void __user *buf; | |
3115 | ssize_t len; | |
3116 | ||
3117 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3118 | return -EFAULT; | |
3119 | ||
3120 | len = iov[0].iov_len; | |
3121 | if (len < 0) | |
3122 | return -EINVAL; | |
3123 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3124 | if (IS_ERR(buf)) | |
3125 | return PTR_ERR(buf); | |
3126 | iov[0].iov_base = buf; | |
3127 | iov[0].iov_len = len; | |
3128 | return 0; | |
3129 | } | |
3130 | ||
3131 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3132 | bool needs_lock) | |
3133 | { | |
dddb3e26 JA |
3134 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3135 | struct io_buffer *kbuf; | |
3136 | ||
3137 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3138 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3139 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3140 | return 0; |
dddb3e26 | 3141 | } |
4d954c25 JA |
3142 | if (!req->rw.len) |
3143 | return 0; | |
3144 | else if (req->rw.len > 1) | |
3145 | return -EINVAL; | |
3146 | ||
3147 | #ifdef CONFIG_COMPAT | |
3148 | if (req->ctx->compat) | |
3149 | return io_compat_import(req, iov, needs_lock); | |
3150 | #endif | |
3151 | ||
3152 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3153 | } | |
3154 | ||
2846c481 | 3155 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
8452fd0c JA |
3156 | struct iovec **iovec, struct iov_iter *iter, |
3157 | bool needs_lock) | |
2b188cc1 | 3158 | { |
9adbd45d JA |
3159 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3160 | size_t sqe_len = req->rw.len; | |
4d954c25 | 3161 | ssize_t ret; |
edafccee JA |
3162 | u8 opcode; |
3163 | ||
d625c6ee | 3164 | opcode = req->opcode; |
7d009165 | 3165 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3166 | *iovec = NULL; |
9adbd45d | 3167 | return io_import_fixed(req, rw, iter); |
edafccee | 3168 | } |
2b188cc1 | 3169 | |
bcda7baa | 3170 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3171 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3172 | return -EINVAL; |
3173 | ||
3a6820f2 | 3174 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3175 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3176 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3177 | if (IS_ERR(buf)) |
4d954c25 | 3178 | return PTR_ERR(buf); |
3f9d6441 | 3179 | req->rw.len = sqe_len; |
bcda7baa JA |
3180 | } |
3181 | ||
3a6820f2 JA |
3182 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3183 | *iovec = NULL; | |
10fc72e4 | 3184 | return ret; |
3a6820f2 JA |
3185 | } |
3186 | ||
4d954c25 JA |
3187 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3188 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
3189 | if (!ret) { |
3190 | ret = (*iovec)->iov_len; | |
3191 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
3192 | } | |
4d954c25 JA |
3193 | *iovec = NULL; |
3194 | return ret; | |
3195 | } | |
3196 | ||
89cd35c5 CH |
3197 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3198 | req->ctx->compat); | |
2b188cc1 JA |
3199 | } |
3200 | ||
0fef9483 JA |
3201 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3202 | { | |
5b09e37e | 3203 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3204 | } |
3205 | ||
31b51510 | 3206 | /* |
32960613 JA |
3207 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3208 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3209 | */ |
4017eb91 | 3210 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3211 | { |
4017eb91 JA |
3212 | struct kiocb *kiocb = &req->rw.kiocb; |
3213 | struct file *file = req->file; | |
32960613 JA |
3214 | ssize_t ret = 0; |
3215 | ||
3216 | /* | |
3217 | * Don't support polled IO through this interface, and we can't | |
3218 | * support non-blocking either. For the latter, this just causes | |
3219 | * the kiocb to be handled from an async context. | |
3220 | */ | |
3221 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3222 | return -EOPNOTSUPP; | |
3223 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3224 | return -EAGAIN; | |
3225 | ||
3226 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3227 | struct iovec iovec; |
32960613 JA |
3228 | ssize_t nr; |
3229 | ||
311ae9e1 PB |
3230 | if (!iov_iter_is_bvec(iter)) { |
3231 | iovec = iov_iter_iovec(iter); | |
3232 | } else { | |
4017eb91 JA |
3233 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3234 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3235 | } |
3236 | ||
32960613 JA |
3237 | if (rw == READ) { |
3238 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3239 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3240 | } else { |
3241 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3242 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3243 | } |
3244 | ||
3245 | if (nr < 0) { | |
3246 | if (!ret) | |
3247 | ret = nr; | |
3248 | break; | |
3249 | } | |
3250 | ret += nr; | |
3251 | if (nr != iovec.iov_len) | |
3252 | break; | |
4017eb91 JA |
3253 | req->rw.len -= nr; |
3254 | req->rw.addr += nr; | |
32960613 JA |
3255 | iov_iter_advance(iter, nr); |
3256 | } | |
3257 | ||
3258 | return ret; | |
3259 | } | |
3260 | ||
ff6165b2 JA |
3261 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3262 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3263 | { |
e8c2bc1f | 3264 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3265 | |
ff6165b2 | 3266 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3267 | rw->free_iovec = iovec; |
227c0c96 | 3268 | rw->bytes_done = 0; |
ff6165b2 JA |
3269 | /* can only be fixed buffers, no need to do anything */ |
3270 | if (iter->type == ITER_BVEC) | |
3271 | return; | |
b64e3444 | 3272 | if (!iovec) { |
ff6165b2 JA |
3273 | unsigned iov_off = 0; |
3274 | ||
3275 | rw->iter.iov = rw->fast_iov; | |
3276 | if (iter->iov != fast_iov) { | |
3277 | iov_off = iter->iov - fast_iov; | |
3278 | rw->iter.iov += iov_off; | |
3279 | } | |
3280 | if (rw->fast_iov != fast_iov) | |
3281 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3282 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3283 | } else { |
3284 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3285 | } |
3286 | } | |
3287 | ||
e8c2bc1f | 3288 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3289 | { |
e8c2bc1f JA |
3290 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3291 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3292 | return req->async_data == NULL; | |
3d9932a8 XW |
3293 | } |
3294 | ||
e8c2bc1f | 3295 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3296 | { |
e8c2bc1f | 3297 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3298 | return 0; |
3d9932a8 | 3299 | |
e8c2bc1f | 3300 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3301 | } |
3302 | ||
ff6165b2 JA |
3303 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3304 | const struct iovec *fast_iov, | |
227c0c96 | 3305 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3306 | { |
e8c2bc1f | 3307 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3308 | return 0; |
e8c2bc1f JA |
3309 | if (!req->async_data) { |
3310 | if (__io_alloc_async_data(req)) | |
5d204bcf | 3311 | return -ENOMEM; |
b7bb4f7d | 3312 | |
ff6165b2 | 3313 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3314 | } |
b7bb4f7d | 3315 | return 0; |
f67676d1 JA |
3316 | } |
3317 | ||
73debe68 | 3318 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3319 | { |
e8c2bc1f | 3320 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3321 | struct iovec *iov = iorw->fast_iov; |
c3e330a4 PB |
3322 | ssize_t ret; |
3323 | ||
2846c481 | 3324 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3325 | if (unlikely(ret < 0)) |
3326 | return ret; | |
3327 | ||
ab0b196c PB |
3328 | iorw->bytes_done = 0; |
3329 | iorw->free_iovec = iov; | |
3330 | if (iov) | |
3331 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3332 | return 0; |
3333 | } | |
3334 | ||
73debe68 | 3335 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3336 | { |
3337 | ssize_t ret; | |
3338 | ||
a88fc400 | 3339 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3340 | if (ret) |
3341 | return ret; | |
f67676d1 | 3342 | |
3529d8c2 JA |
3343 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3344 | return -EBADF; | |
f67676d1 | 3345 | |
5f798bea | 3346 | /* either don't need iovec imported or already have it */ |
2d199895 | 3347 | if (!req->async_data) |
3529d8c2 | 3348 | return 0; |
73debe68 | 3349 | return io_rw_prep_async(req, READ); |
f67676d1 JA |
3350 | } |
3351 | ||
c1dd91d1 JA |
3352 | /* |
3353 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3354 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3355 | * This gets called when the page is unlocked, and we generally expect that to | |
3356 | * happen when the page IO is completed and the page is now uptodate. This will | |
3357 | * queue a task_work based retry of the operation, attempting to copy the data | |
3358 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3359 | * do a thread based blocking retry of the operation. That's the unexpected | |
3360 | * slow path. | |
3361 | */ | |
bcf5a063 JA |
3362 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3363 | int sync, void *arg) | |
3364 | { | |
3365 | struct wait_page_queue *wpq; | |
3366 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3367 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3368 | int ret; |
3369 | ||
3370 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3371 | ||
cdc8fcb4 LT |
3372 | if (!wake_page_match(wpq, key)) |
3373 | return 0; | |
3374 | ||
c8d317aa | 3375 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3376 | list_del_init(&wait->entry); |
3377 | ||
e7375122 | 3378 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3379 | percpu_ref_get(&req->ctx->refs); |
3380 | ||
bcf5a063 JA |
3381 | /* submit ref gets dropped, acquire a new one */ |
3382 | refcount_inc(&req->refs); | |
87c4311f | 3383 | ret = io_req_task_work_add(req, true); |
bcf5a063 | 3384 | if (unlikely(ret)) { |
c2c4c83c JA |
3385 | struct task_struct *tsk; |
3386 | ||
bcf5a063 | 3387 | /* queue just for cancelation */ |
e7375122 | 3388 | init_task_work(&req->task_work, io_req_task_cancel); |
bcf5a063 | 3389 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 3390 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 3391 | wake_up_process(tsk); |
bcf5a063 | 3392 | } |
bcf5a063 JA |
3393 | return 1; |
3394 | } | |
3395 | ||
c1dd91d1 JA |
3396 | /* |
3397 | * This controls whether a given IO request should be armed for async page | |
3398 | * based retry. If we return false here, the request is handed to the async | |
3399 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3400 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3401 | * will either succeed because the page is now uptodate and unlocked, or it | |
3402 | * will register a callback when the page is unlocked at IO completion. Through | |
3403 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3404 | * That retry will attempt the buffered read again. The retry will generally | |
3405 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3406 | * async worker threads for a blocking retry. | |
3407 | */ | |
227c0c96 | 3408 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3409 | { |
e8c2bc1f JA |
3410 | struct io_async_rw *rw = req->async_data; |
3411 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3412 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3413 | |
bcf5a063 JA |
3414 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3415 | if (req->flags & REQ_F_NOWAIT) | |
3416 | return false; | |
f67676d1 | 3417 | |
227c0c96 | 3418 | /* Only for buffered IO */ |
3b2a4439 | 3419 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3420 | return false; |
3b2a4439 | 3421 | |
bcf5a063 JA |
3422 | /* |
3423 | * just use poll if we can, and don't attempt if the fs doesn't | |
3424 | * support callback based unlocks | |
3425 | */ | |
3426 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3427 | return false; | |
f67676d1 | 3428 | |
3b2a4439 JA |
3429 | wait->wait.func = io_async_buf_func; |
3430 | wait->wait.private = req; | |
3431 | wait->wait.flags = 0; | |
3432 | INIT_LIST_HEAD(&wait->wait.entry); | |
3433 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3434 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3435 | kiocb->ki_waitq = wait; |
3b2a4439 | 3436 | return true; |
bcf5a063 JA |
3437 | } |
3438 | ||
3439 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3440 | { | |
3441 | if (req->file->f_op->read_iter) | |
3442 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3443 | else if (req->file->f_op->read) |
4017eb91 | 3444 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3445 | else |
3446 | return -EINVAL; | |
f67676d1 JA |
3447 | } |
3448 | ||
a1d7c393 JA |
3449 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3450 | struct io_comp_state *cs) | |
2b188cc1 JA |
3451 | { |
3452 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3453 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3454 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3455 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3456 | ssize_t io_size, ret, ret2; |
f5cac8b1 | 3457 | bool no_async; |
ff6165b2 | 3458 | |
2846c481 | 3459 | if (rw) { |
e8c2bc1f | 3460 | iter = &rw->iter; |
2846c481 PB |
3461 | iovec = NULL; |
3462 | } else { | |
3463 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3464 | if (ret < 0) | |
3465 | return ret; | |
3466 | } | |
632546c4 | 3467 | io_size = iov_iter_count(iter); |
fa15bafb | 3468 | req->result = io_size; |
227c0c96 | 3469 | ret = 0; |
2b188cc1 | 3470 | |
fd6c2e4c JA |
3471 | /* Ensure we clear previously set non-block flag */ |
3472 | if (!force_nonblock) | |
29de5f6a | 3473 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3474 | else |
3475 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3476 | ||
fd6c2e4c | 3477 | |
24c74678 | 3478 | /* If the file doesn't support async, just async punt */ |
f5cac8b1 JA |
3479 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
3480 | if (no_async) | |
f67676d1 | 3481 | goto copy_iov; |
9e645e11 | 3482 | |
632546c4 | 3483 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3484 | if (unlikely(ret)) |
3485 | goto out_free; | |
2b188cc1 | 3486 | |
227c0c96 | 3487 | ret = io_iter_do_read(req, iter); |
32960613 | 3488 | |
227c0c96 JA |
3489 | if (!ret) { |
3490 | goto done; | |
3491 | } else if (ret == -EIOCBQUEUED) { | |
3492 | ret = 0; | |
3493 | goto out_free; | |
3494 | } else if (ret == -EAGAIN) { | |
eefdf30f JA |
3495 | /* IOPOLL retry should happen for io-wq threads */ |
3496 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3497 | goto done; |
355afaeb JA |
3498 | /* no retry on NONBLOCK marked file */ |
3499 | if (req->file->f_flags & O_NONBLOCK) | |
3500 | goto done; | |
84216315 | 3501 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3502 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a JA |
3503 | ret = 0; |
3504 | goto copy_iov; | |
227c0c96 | 3505 | } else if (ret < 0) { |
00d23d51 JA |
3506 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
3507 | goto done; | |
227c0c96 JA |
3508 | } |
3509 | ||
3510 | /* read it all, or we did blocking attempt. no retry. */ | |
f91daf56 JA |
3511 | if (!iov_iter_count(iter) || !force_nonblock || |
3512 | (req->file->f_flags & O_NONBLOCK)) | |
227c0c96 JA |
3513 | goto done; |
3514 | ||
3515 | io_size -= ret; | |
3516 | copy_iov: | |
3517 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
3518 | if (ret2) { | |
3519 | ret = ret2; | |
3520 | goto out_free; | |
3521 | } | |
f5cac8b1 JA |
3522 | if (no_async) |
3523 | return -EAGAIN; | |
e8c2bc1f | 3524 | rw = req->async_data; |
227c0c96 JA |
3525 | /* it's copied and will be cleaned with ->io */ |
3526 | iovec = NULL; | |
3527 | /* now use our persistent iterator, if we aren't already */ | |
e8c2bc1f | 3528 | iter = &rw->iter; |
227c0c96 | 3529 | retry: |
e8c2bc1f | 3530 | rw->bytes_done += ret; |
227c0c96 JA |
3531 | /* if we can retry, do so with the callbacks armed */ |
3532 | if (!io_rw_should_retry(req)) { | |
fa15bafb PB |
3533 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3534 | return -EAGAIN; | |
2b188cc1 | 3535 | } |
227c0c96 JA |
3536 | |
3537 | /* | |
3538 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we | |
3539 | * get -EIOCBQUEUED, then we'll get a notification when the desired | |
3540 | * page gets unlocked. We can also get a partial read here, and if we | |
3541 | * do, then just retry at the new offset. | |
3542 | */ | |
3543 | ret = io_iter_do_read(req, iter); | |
3544 | if (ret == -EIOCBQUEUED) { | |
3545 | ret = 0; | |
3546 | goto out_free; | |
3547 | } else if (ret > 0 && ret < io_size) { | |
3548 | /* we got some bytes, but not all. retry. */ | |
3549 | goto retry; | |
3550 | } | |
3551 | done: | |
3552 | kiocb_done(kiocb, ret, cs); | |
3553 | ret = 0; | |
f67676d1 | 3554 | out_free: |
f261c168 | 3555 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3556 | if (iovec) |
6f2cc166 | 3557 | kfree(iovec); |
2b188cc1 JA |
3558 | return ret; |
3559 | } | |
3560 | ||
73debe68 | 3561 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3562 | { |
3563 | ssize_t ret; | |
3564 | ||
a88fc400 | 3565 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3566 | if (ret) |
3567 | return ret; | |
f67676d1 | 3568 | |
3529d8c2 JA |
3569 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3570 | return -EBADF; | |
f67676d1 | 3571 | |
5f798bea | 3572 | /* either don't need iovec imported or already have it */ |
2d199895 | 3573 | if (!req->async_data) |
3529d8c2 | 3574 | return 0; |
73debe68 | 3575 | return io_rw_prep_async(req, WRITE); |
f67676d1 JA |
3576 | } |
3577 | ||
a1d7c393 JA |
3578 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3579 | struct io_comp_state *cs) | |
2b188cc1 JA |
3580 | { |
3581 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3582 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3583 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3584 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3585 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3586 | |
2846c481 | 3587 | if (rw) { |
e8c2bc1f | 3588 | iter = &rw->iter; |
2846c481 PB |
3589 | iovec = NULL; |
3590 | } else { | |
3591 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3592 | if (ret < 0) | |
3593 | return ret; | |
3594 | } | |
632546c4 | 3595 | io_size = iov_iter_count(iter); |
fa15bafb | 3596 | req->result = io_size; |
2b188cc1 | 3597 | |
fd6c2e4c JA |
3598 | /* Ensure we clear previously set non-block flag */ |
3599 | if (!force_nonblock) | |
a88fc400 PB |
3600 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3601 | else | |
3602 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3603 | |
24c74678 | 3604 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3605 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3606 | goto copy_iov; |
31b51510 | 3607 | |
10d59345 JA |
3608 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3609 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3610 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3611 | goto copy_iov; |
31b51510 | 3612 | |
632546c4 | 3613 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3614 | if (unlikely(ret)) |
3615 | goto out_free; | |
4ed734b0 | 3616 | |
fa15bafb PB |
3617 | /* |
3618 | * Open-code file_start_write here to grab freeze protection, | |
3619 | * which will be released by another thread in | |
3620 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3621 | * released so that it doesn't complain about the held lock when | |
3622 | * we return to userspace. | |
3623 | */ | |
3624 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3625 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3626 | __sb_writers_release(file_inode(req->file)->i_sb, |
3627 | SB_FREEZE_WRITE); | |
3628 | } | |
3629 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3630 | |
fa15bafb | 3631 | if (req->file->f_op->write_iter) |
ff6165b2 | 3632 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3633 | else if (req->file->f_op->write) |
4017eb91 | 3634 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3635 | else |
3636 | ret2 = -EINVAL; | |
4ed734b0 | 3637 | |
fa15bafb PB |
3638 | /* |
3639 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3640 | * retry them without IOCB_NOWAIT. | |
3641 | */ | |
3642 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3643 | ret2 = -EAGAIN; | |
355afaeb JA |
3644 | /* no retry on NONBLOCK marked file */ |
3645 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) | |
3646 | goto done; | |
fa15bafb | 3647 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3648 | /* IOPOLL retry should happen for io-wq threads */ |
3649 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3650 | goto copy_iov; | |
355afaeb | 3651 | done: |
fa15bafb PB |
3652 | kiocb_done(kiocb, ret2, cs); |
3653 | } else { | |
f67676d1 | 3654 | copy_iov: |
84216315 | 3655 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3656 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3657 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
ff6165b2 JA |
3658 | if (!ret) |
3659 | return -EAGAIN; | |
2b188cc1 | 3660 | } |
31b51510 | 3661 | out_free: |
f261c168 | 3662 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3663 | if (iovec) |
6f2cc166 | 3664 | kfree(iovec); |
2b188cc1 JA |
3665 | return ret; |
3666 | } | |
3667 | ||
80a261fd JA |
3668 | static int io_renameat_prep(struct io_kiocb *req, |
3669 | const struct io_uring_sqe *sqe) | |
3670 | { | |
3671 | struct io_rename *ren = &req->rename; | |
3672 | const char __user *oldf, *newf; | |
3673 | ||
3674 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3675 | return -EBADF; | |
3676 | ||
3677 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3678 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3679 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3680 | ren->new_dfd = READ_ONCE(sqe->len); | |
3681 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3682 | ||
3683 | ren->oldpath = getname(oldf); | |
3684 | if (IS_ERR(ren->oldpath)) | |
3685 | return PTR_ERR(ren->oldpath); | |
3686 | ||
3687 | ren->newpath = getname(newf); | |
3688 | if (IS_ERR(ren->newpath)) { | |
3689 | putname(ren->oldpath); | |
3690 | return PTR_ERR(ren->newpath); | |
3691 | } | |
3692 | ||
3693 | req->flags |= REQ_F_NEED_CLEANUP; | |
3694 | return 0; | |
3695 | } | |
3696 | ||
3697 | static int io_renameat(struct io_kiocb *req, bool force_nonblock) | |
3698 | { | |
3699 | struct io_rename *ren = &req->rename; | |
3700 | int ret; | |
3701 | ||
3702 | if (force_nonblock) | |
3703 | return -EAGAIN; | |
3704 | ||
3705 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3706 | ren->newpath, ren->flags); | |
3707 | ||
3708 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3709 | if (ret < 0) | |
3710 | req_set_fail_links(req); | |
3711 | io_req_complete(req, ret); | |
3712 | return 0; | |
3713 | } | |
3714 | ||
14a1143b JA |
3715 | static int io_unlinkat_prep(struct io_kiocb *req, |
3716 | const struct io_uring_sqe *sqe) | |
3717 | { | |
3718 | struct io_unlink *un = &req->unlink; | |
3719 | const char __user *fname; | |
3720 | ||
3721 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3722 | return -EBADF; | |
3723 | ||
3724 | un->dfd = READ_ONCE(sqe->fd); | |
3725 | ||
3726 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3727 | if (un->flags & ~AT_REMOVEDIR) | |
3728 | return -EINVAL; | |
3729 | ||
3730 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3731 | un->filename = getname(fname); | |
3732 | if (IS_ERR(un->filename)) | |
3733 | return PTR_ERR(un->filename); | |
3734 | ||
3735 | req->flags |= REQ_F_NEED_CLEANUP; | |
3736 | return 0; | |
3737 | } | |
3738 | ||
3739 | static int io_unlinkat(struct io_kiocb *req, bool force_nonblock) | |
3740 | { | |
3741 | struct io_unlink *un = &req->unlink; | |
3742 | int ret; | |
3743 | ||
3744 | if (force_nonblock) | |
3745 | return -EAGAIN; | |
3746 | ||
3747 | if (un->flags & AT_REMOVEDIR) | |
3748 | ret = do_rmdir(un->dfd, un->filename); | |
3749 | else | |
3750 | ret = do_unlinkat(un->dfd, un->filename); | |
3751 | ||
3752 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3753 | if (ret < 0) | |
3754 | req_set_fail_links(req); | |
3755 | io_req_complete(req, ret); | |
3756 | return 0; | |
3757 | } | |
3758 | ||
36f4fa68 JA |
3759 | static int io_shutdown_prep(struct io_kiocb *req, |
3760 | const struct io_uring_sqe *sqe) | |
3761 | { | |
3762 | #if defined(CONFIG_NET) | |
3763 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3764 | return -EINVAL; | |
3765 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3766 | sqe->buf_index) | |
3767 | return -EINVAL; | |
3768 | ||
3769 | req->shutdown.how = READ_ONCE(sqe->len); | |
3770 | return 0; | |
3771 | #else | |
3772 | return -EOPNOTSUPP; | |
3773 | #endif | |
3774 | } | |
3775 | ||
3776 | static int io_shutdown(struct io_kiocb *req, bool force_nonblock) | |
3777 | { | |
3778 | #if defined(CONFIG_NET) | |
3779 | struct socket *sock; | |
3780 | int ret; | |
3781 | ||
3782 | if (force_nonblock) | |
3783 | return -EAGAIN; | |
3784 | ||
3785 | sock = sock_from_file(req->file, &ret); | |
3786 | if (unlikely(!sock)) | |
3787 | return ret; | |
3788 | ||
3789 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
3790 | io_req_complete(req, ret); | |
3791 | return 0; | |
3792 | #else | |
3793 | return -EOPNOTSUPP; | |
3794 | #endif | |
3795 | } | |
3796 | ||
f2a8d5c7 PB |
3797 | static int __io_splice_prep(struct io_kiocb *req, |
3798 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3799 | { |
3800 | struct io_splice* sp = &req->splice; | |
3801 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3802 | |
3232dd02 PB |
3803 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3804 | return -EINVAL; | |
7d67af2c PB |
3805 | |
3806 | sp->file_in = NULL; | |
7d67af2c PB |
3807 | sp->len = READ_ONCE(sqe->len); |
3808 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3809 | ||
3810 | if (unlikely(sp->flags & ~valid_flags)) | |
3811 | return -EINVAL; | |
3812 | ||
8371adf5 PB |
3813 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3814 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3815 | if (!sp->file_in) | |
3816 | return -EBADF; | |
7d67af2c PB |
3817 | req->flags |= REQ_F_NEED_CLEANUP; |
3818 | ||
7cdaf587 XW |
3819 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3820 | /* | |
3821 | * Splice operation will be punted aync, and here need to | |
3822 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3823 | */ | |
3824 | io_req_init_async(req); | |
7d67af2c | 3825 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3826 | } |
7d67af2c PB |
3827 | |
3828 | return 0; | |
3829 | } | |
3830 | ||
f2a8d5c7 PB |
3831 | static int io_tee_prep(struct io_kiocb *req, |
3832 | const struct io_uring_sqe *sqe) | |
3833 | { | |
3834 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3835 | return -EINVAL; | |
3836 | return __io_splice_prep(req, sqe); | |
3837 | } | |
3838 | ||
3839 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3840 | { | |
3841 | struct io_splice *sp = &req->splice; | |
3842 | struct file *in = sp->file_in; | |
3843 | struct file *out = sp->file_out; | |
3844 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3845 | long ret = 0; | |
3846 | ||
3847 | if (force_nonblock) | |
3848 | return -EAGAIN; | |
3849 | if (sp->len) | |
3850 | ret = do_tee(in, out, sp->len, flags); | |
3851 | ||
3852 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3853 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3854 | ||
f2a8d5c7 PB |
3855 | if (ret != sp->len) |
3856 | req_set_fail_links(req); | |
e1e16097 | 3857 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3858 | return 0; |
3859 | } | |
3860 | ||
3861 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3862 | { | |
3863 | struct io_splice* sp = &req->splice; | |
3864 | ||
3865 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3866 | sp->off_out = READ_ONCE(sqe->off); | |
3867 | return __io_splice_prep(req, sqe); | |
3868 | } | |
3869 | ||
014db007 | 3870 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3871 | { |
3872 | struct io_splice *sp = &req->splice; | |
3873 | struct file *in = sp->file_in; | |
3874 | struct file *out = sp->file_out; | |
3875 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3876 | loff_t *poff_in, *poff_out; | |
c9687426 | 3877 | long ret = 0; |
7d67af2c | 3878 | |
2fb3e822 PB |
3879 | if (force_nonblock) |
3880 | return -EAGAIN; | |
7d67af2c PB |
3881 | |
3882 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3883 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3884 | |
948a7749 | 3885 | if (sp->len) |
c9687426 | 3886 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3887 | |
3888 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3889 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3890 | ||
7d67af2c PB |
3891 | if (ret != sp->len) |
3892 | req_set_fail_links(req); | |
e1e16097 | 3893 | io_req_complete(req, ret); |
7d67af2c PB |
3894 | return 0; |
3895 | } | |
3896 | ||
2b188cc1 JA |
3897 | /* |
3898 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3899 | */ | |
229a7b63 | 3900 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3901 | { |
3902 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3903 | |
def596e9 JA |
3904 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3905 | return -EINVAL; | |
3906 | ||
229a7b63 | 3907 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3908 | return 0; |
3909 | } | |
3910 | ||
3529d8c2 | 3911 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3912 | { |
6b06314c | 3913 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3914 | |
09bb8394 JA |
3915 | if (!req->file) |
3916 | return -EBADF; | |
c992fe29 | 3917 | |
6b06314c | 3918 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3919 | return -EINVAL; |
edafccee | 3920 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3921 | return -EINVAL; |
3922 | ||
8ed8d3c3 JA |
3923 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3924 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3925 | return -EINVAL; | |
3926 | ||
3927 | req->sync.off = READ_ONCE(sqe->off); | |
3928 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3929 | return 0; |
3930 | } | |
3931 | ||
ac45abc0 | 3932 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3933 | { |
8ed8d3c3 | 3934 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3935 | int ret; |
3936 | ||
ac45abc0 PB |
3937 | /* fsync always requires a blocking context */ |
3938 | if (force_nonblock) | |
3939 | return -EAGAIN; | |
3940 | ||
9adbd45d | 3941 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3942 | end > 0 ? end : LLONG_MAX, |
3943 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3944 | if (ret < 0) | |
3945 | req_set_fail_links(req); | |
e1e16097 | 3946 | io_req_complete(req, ret); |
c992fe29 CH |
3947 | return 0; |
3948 | } | |
3949 | ||
d63d1b5e JA |
3950 | static int io_fallocate_prep(struct io_kiocb *req, |
3951 | const struct io_uring_sqe *sqe) | |
3952 | { | |
3953 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3954 | return -EINVAL; | |
3232dd02 PB |
3955 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3956 | return -EINVAL; | |
d63d1b5e JA |
3957 | |
3958 | req->sync.off = READ_ONCE(sqe->off); | |
3959 | req->sync.len = READ_ONCE(sqe->addr); | |
3960 | req->sync.mode = READ_ONCE(sqe->len); | |
3961 | return 0; | |
3962 | } | |
3963 | ||
014db007 | 3964 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 3965 | { |
ac45abc0 PB |
3966 | int ret; |
3967 | ||
d63d1b5e | 3968 | /* fallocate always requiring blocking context */ |
ac45abc0 | 3969 | if (force_nonblock) |
5d17b4a4 | 3970 | return -EAGAIN; |
ac45abc0 PB |
3971 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3972 | req->sync.len); | |
ac45abc0 PB |
3973 | if (ret < 0) |
3974 | req_set_fail_links(req); | |
e1e16097 | 3975 | io_req_complete(req, ret); |
5d17b4a4 JA |
3976 | return 0; |
3977 | } | |
3978 | ||
ec65fea5 | 3979 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3980 | { |
f8748881 | 3981 | const char __user *fname; |
15b71abe | 3982 | int ret; |
b7bb4f7d | 3983 | |
ec65fea5 | 3984 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3985 | return -EINVAL; |
ec65fea5 | 3986 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3987 | return -EBADF; |
03b1230c | 3988 | |
ec65fea5 PB |
3989 | /* open.how should be already initialised */ |
3990 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3991 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3992 | |
25e72d10 PB |
3993 | req->open.dfd = READ_ONCE(sqe->fd); |
3994 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3995 | req->open.filename = getname(fname); |
15b71abe JA |
3996 | if (IS_ERR(req->open.filename)) { |
3997 | ret = PTR_ERR(req->open.filename); | |
3998 | req->open.filename = NULL; | |
3999 | return ret; | |
4000 | } | |
4022e7af | 4001 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
944d1444 | 4002 | req->open.ignore_nonblock = false; |
8fef80bf | 4003 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4004 | return 0; |
03b1230c JA |
4005 | } |
4006 | ||
ec65fea5 PB |
4007 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4008 | { | |
4009 | u64 flags, mode; | |
4010 | ||
14587a46 | 4011 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4012 | return -EINVAL; |
ec65fea5 PB |
4013 | mode = READ_ONCE(sqe->len); |
4014 | flags = READ_ONCE(sqe->open_flags); | |
4015 | req->open.how = build_open_how(flags, mode); | |
4016 | return __io_openat_prep(req, sqe); | |
4017 | } | |
4018 | ||
cebdb986 | 4019 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4020 | { |
cebdb986 | 4021 | struct open_how __user *how; |
cebdb986 | 4022 | size_t len; |
0fa03c62 JA |
4023 | int ret; |
4024 | ||
14587a46 | 4025 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4026 | return -EINVAL; |
cebdb986 JA |
4027 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4028 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4029 | if (len < OPEN_HOW_SIZE_VER0) |
4030 | return -EINVAL; | |
3529d8c2 | 4031 | |
cebdb986 JA |
4032 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4033 | len); | |
4034 | if (ret) | |
4035 | return ret; | |
3529d8c2 | 4036 | |
ec65fea5 | 4037 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4038 | } |
4039 | ||
014db007 | 4040 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
4041 | { |
4042 | struct open_flags op; | |
15b71abe JA |
4043 | struct file *file; |
4044 | int ret; | |
4045 | ||
944d1444 | 4046 | if (force_nonblock && !req->open.ignore_nonblock) |
15b71abe | 4047 | return -EAGAIN; |
15b71abe | 4048 | |
cebdb986 | 4049 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4050 | if (ret) |
4051 | goto err; | |
4052 | ||
4022e7af | 4053 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4054 | if (ret < 0) |
4055 | goto err; | |
4056 | ||
4057 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
4058 | if (IS_ERR(file)) { | |
4059 | put_unused_fd(ret); | |
4060 | ret = PTR_ERR(file); | |
944d1444 JA |
4061 | /* |
4062 | * A work-around to ensure that /proc/self works that way | |
4063 | * that it should - if we get -EOPNOTSUPP back, then assume | |
4064 | * that proc_self_get_link() failed us because we're in async | |
4065 | * context. We should be safe to retry this from the task | |
4066 | * itself with force_nonblock == false set, as it should not | |
4067 | * block on lookup. Would be nice to know this upfront and | |
4068 | * avoid the async dance, but doesn't seem feasible. | |
4069 | */ | |
4070 | if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) { | |
4071 | req->open.ignore_nonblock = true; | |
4072 | refcount_inc(&req->refs); | |
4073 | io_req_task_queue(req); | |
4074 | return 0; | |
4075 | } | |
15b71abe JA |
4076 | } else { |
4077 | fsnotify_open(file); | |
4078 | fd_install(ret, file); | |
4079 | } | |
4080 | err: | |
4081 | putname(req->open.filename); | |
8fef80bf | 4082 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4083 | if (ret < 0) |
4084 | req_set_fail_links(req); | |
e1e16097 | 4085 | io_req_complete(req, ret); |
15b71abe JA |
4086 | return 0; |
4087 | } | |
4088 | ||
014db007 | 4089 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 4090 | { |
014db007 | 4091 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
4092 | } |
4093 | ||
067524e9 JA |
4094 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4095 | const struct io_uring_sqe *sqe) | |
4096 | { | |
4097 | struct io_provide_buf *p = &req->pbuf; | |
4098 | u64 tmp; | |
4099 | ||
4100 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4101 | return -EINVAL; | |
4102 | ||
4103 | tmp = READ_ONCE(sqe->fd); | |
4104 | if (!tmp || tmp > USHRT_MAX) | |
4105 | return -EINVAL; | |
4106 | ||
4107 | memset(p, 0, sizeof(*p)); | |
4108 | p->nbufs = tmp; | |
4109 | p->bgid = READ_ONCE(sqe->buf_group); | |
4110 | return 0; | |
4111 | } | |
4112 | ||
4113 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4114 | int bgid, unsigned nbufs) | |
4115 | { | |
4116 | unsigned i = 0; | |
4117 | ||
4118 | /* shouldn't happen */ | |
4119 | if (!nbufs) | |
4120 | return 0; | |
4121 | ||
4122 | /* the head kbuf is the list itself */ | |
4123 | while (!list_empty(&buf->list)) { | |
4124 | struct io_buffer *nxt; | |
4125 | ||
4126 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4127 | list_del(&nxt->list); | |
4128 | kfree(nxt); | |
4129 | if (++i == nbufs) | |
4130 | return i; | |
4131 | } | |
4132 | i++; | |
4133 | kfree(buf); | |
4134 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4135 | ||
4136 | return i; | |
4137 | } | |
4138 | ||
229a7b63 JA |
4139 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
4140 | struct io_comp_state *cs) | |
067524e9 JA |
4141 | { |
4142 | struct io_provide_buf *p = &req->pbuf; | |
4143 | struct io_ring_ctx *ctx = req->ctx; | |
4144 | struct io_buffer *head; | |
4145 | int ret = 0; | |
4146 | ||
4147 | io_ring_submit_lock(ctx, !force_nonblock); | |
4148 | ||
4149 | lockdep_assert_held(&ctx->uring_lock); | |
4150 | ||
4151 | ret = -ENOENT; | |
4152 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4153 | if (head) | |
4154 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4155 | if (ret < 0) |
4156 | req_set_fail_links(req); | |
31bff9a5 PB |
4157 | |
4158 | /* need to hold the lock to complete IOPOLL requests */ | |
4159 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4160 | __io_req_complete(req, ret, 0, cs); | |
4161 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4162 | } else { | |
4163 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4164 | __io_req_complete(req, ret, 0, cs); | |
4165 | } | |
067524e9 JA |
4166 | return 0; |
4167 | } | |
4168 | ||
ddf0322d JA |
4169 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4170 | const struct io_uring_sqe *sqe) | |
4171 | { | |
4172 | struct io_provide_buf *p = &req->pbuf; | |
4173 | u64 tmp; | |
4174 | ||
4175 | if (sqe->ioprio || sqe->rw_flags) | |
4176 | return -EINVAL; | |
4177 | ||
4178 | tmp = READ_ONCE(sqe->fd); | |
4179 | if (!tmp || tmp > USHRT_MAX) | |
4180 | return -E2BIG; | |
4181 | p->nbufs = tmp; | |
4182 | p->addr = READ_ONCE(sqe->addr); | |
4183 | p->len = READ_ONCE(sqe->len); | |
4184 | ||
efe68c1c | 4185 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4186 | return -EFAULT; |
4187 | ||
4188 | p->bgid = READ_ONCE(sqe->buf_group); | |
4189 | tmp = READ_ONCE(sqe->off); | |
4190 | if (tmp > USHRT_MAX) | |
4191 | return -E2BIG; | |
4192 | p->bid = tmp; | |
4193 | return 0; | |
4194 | } | |
4195 | ||
4196 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4197 | { | |
4198 | struct io_buffer *buf; | |
4199 | u64 addr = pbuf->addr; | |
4200 | int i, bid = pbuf->bid; | |
4201 | ||
4202 | for (i = 0; i < pbuf->nbufs; i++) { | |
4203 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4204 | if (!buf) | |
4205 | break; | |
4206 | ||
4207 | buf->addr = addr; | |
4208 | buf->len = pbuf->len; | |
4209 | buf->bid = bid; | |
4210 | addr += pbuf->len; | |
4211 | bid++; | |
4212 | if (!*head) { | |
4213 | INIT_LIST_HEAD(&buf->list); | |
4214 | *head = buf; | |
4215 | } else { | |
4216 | list_add_tail(&buf->list, &(*head)->list); | |
4217 | } | |
4218 | } | |
4219 | ||
4220 | return i ? i : -ENOMEM; | |
4221 | } | |
4222 | ||
229a7b63 JA |
4223 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
4224 | struct io_comp_state *cs) | |
ddf0322d JA |
4225 | { |
4226 | struct io_provide_buf *p = &req->pbuf; | |
4227 | struct io_ring_ctx *ctx = req->ctx; | |
4228 | struct io_buffer *head, *list; | |
4229 | int ret = 0; | |
4230 | ||
4231 | io_ring_submit_lock(ctx, !force_nonblock); | |
4232 | ||
4233 | lockdep_assert_held(&ctx->uring_lock); | |
4234 | ||
4235 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4236 | ||
4237 | ret = io_add_buffers(p, &head); | |
4238 | if (ret < 0) | |
4239 | goto out; | |
4240 | ||
4241 | if (!list) { | |
4242 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4243 | GFP_KERNEL); | |
4244 | if (ret < 0) { | |
067524e9 | 4245 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4246 | goto out; |
4247 | } | |
4248 | } | |
4249 | out: | |
ddf0322d JA |
4250 | if (ret < 0) |
4251 | req_set_fail_links(req); | |
31bff9a5 PB |
4252 | |
4253 | /* need to hold the lock to complete IOPOLL requests */ | |
4254 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4255 | __io_req_complete(req, ret, 0, cs); | |
4256 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4257 | } else { | |
4258 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4259 | __io_req_complete(req, ret, 0, cs); | |
4260 | } | |
ddf0322d | 4261 | return 0; |
cebdb986 JA |
4262 | } |
4263 | ||
3e4827b0 JA |
4264 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4265 | const struct io_uring_sqe *sqe) | |
4266 | { | |
4267 | #if defined(CONFIG_EPOLL) | |
4268 | if (sqe->ioprio || sqe->buf_index) | |
4269 | return -EINVAL; | |
6ca56f84 | 4270 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4271 | return -EINVAL; |
3e4827b0 JA |
4272 | |
4273 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4274 | req->epoll.op = READ_ONCE(sqe->len); | |
4275 | req->epoll.fd = READ_ONCE(sqe->off); | |
4276 | ||
4277 | if (ep_op_has_event(req->epoll.op)) { | |
4278 | struct epoll_event __user *ev; | |
4279 | ||
4280 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4281 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4282 | return -EFAULT; | |
4283 | } | |
4284 | ||
4285 | return 0; | |
4286 | #else | |
4287 | return -EOPNOTSUPP; | |
4288 | #endif | |
4289 | } | |
4290 | ||
229a7b63 JA |
4291 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
4292 | struct io_comp_state *cs) | |
3e4827b0 JA |
4293 | { |
4294 | #if defined(CONFIG_EPOLL) | |
4295 | struct io_epoll *ie = &req->epoll; | |
4296 | int ret; | |
4297 | ||
4298 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4299 | if (force_nonblock && ret == -EAGAIN) | |
4300 | return -EAGAIN; | |
4301 | ||
4302 | if (ret < 0) | |
4303 | req_set_fail_links(req); | |
229a7b63 | 4304 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
4305 | return 0; |
4306 | #else | |
4307 | return -EOPNOTSUPP; | |
4308 | #endif | |
4309 | } | |
4310 | ||
c1ca757b JA |
4311 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4312 | { | |
4313 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4314 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4315 | return -EINVAL; | |
3232dd02 PB |
4316 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4317 | return -EINVAL; | |
c1ca757b JA |
4318 | |
4319 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4320 | req->madvise.len = READ_ONCE(sqe->len); | |
4321 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4322 | return 0; | |
4323 | #else | |
4324 | return -EOPNOTSUPP; | |
4325 | #endif | |
4326 | } | |
4327 | ||
014db007 | 4328 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
4329 | { |
4330 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4331 | struct io_madvise *ma = &req->madvise; | |
4332 | int ret; | |
4333 | ||
4334 | if (force_nonblock) | |
4335 | return -EAGAIN; | |
4336 | ||
0726b01e | 4337 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4338 | if (ret < 0) |
4339 | req_set_fail_links(req); | |
e1e16097 | 4340 | io_req_complete(req, ret); |
c1ca757b JA |
4341 | return 0; |
4342 | #else | |
4343 | return -EOPNOTSUPP; | |
4344 | #endif | |
4345 | } | |
4346 | ||
4840e418 JA |
4347 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4348 | { | |
4349 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4350 | return -EINVAL; | |
3232dd02 PB |
4351 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4352 | return -EINVAL; | |
4840e418 JA |
4353 | |
4354 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4355 | req->fadvise.len = READ_ONCE(sqe->len); | |
4356 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4357 | return 0; | |
4358 | } | |
4359 | ||
014db007 | 4360 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
4361 | { |
4362 | struct io_fadvise *fa = &req->fadvise; | |
4363 | int ret; | |
4364 | ||
3e69426d JA |
4365 | if (force_nonblock) { |
4366 | switch (fa->advice) { | |
4367 | case POSIX_FADV_NORMAL: | |
4368 | case POSIX_FADV_RANDOM: | |
4369 | case POSIX_FADV_SEQUENTIAL: | |
4370 | break; | |
4371 | default: | |
4372 | return -EAGAIN; | |
4373 | } | |
4374 | } | |
4840e418 JA |
4375 | |
4376 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4377 | if (ret < 0) | |
4378 | req_set_fail_links(req); | |
e1e16097 | 4379 | io_req_complete(req, ret); |
4840e418 JA |
4380 | return 0; |
4381 | } | |
4382 | ||
eddc7ef5 JA |
4383 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4384 | { | |
6ca56f84 | 4385 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4386 | return -EINVAL; |
eddc7ef5 JA |
4387 | if (sqe->ioprio || sqe->buf_index) |
4388 | return -EINVAL; | |
9c280f90 | 4389 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4390 | return -EBADF; |
eddc7ef5 | 4391 | |
1d9e1288 BM |
4392 | req->statx.dfd = READ_ONCE(sqe->fd); |
4393 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4394 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4395 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4396 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4397 | |
4398 | return 0; | |
4399 | } | |
4400 | ||
014db007 | 4401 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 4402 | { |
1d9e1288 | 4403 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4404 | int ret; |
4405 | ||
5b0bbee4 JA |
4406 | if (force_nonblock) { |
4407 | /* only need file table for an actual valid fd */ | |
4408 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4409 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4410 | return -EAGAIN; |
5b0bbee4 | 4411 | } |
eddc7ef5 | 4412 | |
e62753e4 BM |
4413 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4414 | ctx->buffer); | |
eddc7ef5 | 4415 | |
eddc7ef5 JA |
4416 | if (ret < 0) |
4417 | req_set_fail_links(req); | |
e1e16097 | 4418 | io_req_complete(req, ret); |
eddc7ef5 JA |
4419 | return 0; |
4420 | } | |
4421 | ||
b5dba59e JA |
4422 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4423 | { | |
4424 | /* | |
4425 | * If we queue this for async, it must not be cancellable. That would | |
7cdaf587 XW |
4426 | * leave the 'file' in an undeterminate state, and here need to modify |
4427 | * io_wq_work.flags, so initialize io_wq_work firstly. | |
b5dba59e | 4428 | */ |
7cdaf587 | 4429 | io_req_init_async(req); |
b5dba59e JA |
4430 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
4431 | ||
14587a46 | 4432 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4433 | return -EINVAL; |
b5dba59e JA |
4434 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4435 | sqe->rw_flags || sqe->buf_index) | |
4436 | return -EINVAL; | |
9c280f90 | 4437 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4438 | return -EBADF; |
b5dba59e JA |
4439 | |
4440 | req->close.fd = READ_ONCE(sqe->fd); | |
0f212204 | 4441 | if ((req->file && req->file->f_op == &io_uring_fops)) |
fd2206e4 | 4442 | return -EBADF; |
b5dba59e | 4443 | |
3af73b28 | 4444 | req->close.put_file = NULL; |
b5dba59e | 4445 | return 0; |
b5dba59e JA |
4446 | } |
4447 | ||
229a7b63 JA |
4448 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
4449 | struct io_comp_state *cs) | |
b5dba59e | 4450 | { |
3af73b28 | 4451 | struct io_close *close = &req->close; |
b5dba59e JA |
4452 | int ret; |
4453 | ||
3af73b28 PB |
4454 | /* might be already done during nonblock submission */ |
4455 | if (!close->put_file) { | |
4456 | ret = __close_fd_get_file(close->fd, &close->put_file); | |
4457 | if (ret < 0) | |
4458 | return (ret == -ENOENT) ? -EBADF : ret; | |
4459 | } | |
b5dba59e JA |
4460 | |
4461 | /* if the file has a flush method, be safe and punt to async */ | |
3af73b28 | 4462 | if (close->put_file->f_op->flush && force_nonblock) { |
24c74678 PB |
4463 | /* was never set, but play safe */ |
4464 | req->flags &= ~REQ_F_NOWAIT; | |
0bf0eefd | 4465 | /* avoid grabbing files - we don't need the files */ |
24c74678 | 4466 | req->flags |= REQ_F_NO_FILE_TABLE; |
0bf0eefd | 4467 | return -EAGAIN; |
a2100672 | 4468 | } |
b5dba59e | 4469 | |
3af73b28 | 4470 | /* No ->flush() or already async, safely close from here */ |
98447d65 | 4471 | ret = filp_close(close->put_file, req->work.identity->files); |
3af73b28 PB |
4472 | if (ret < 0) |
4473 | req_set_fail_links(req); | |
3af73b28 PB |
4474 | fput(close->put_file); |
4475 | close->put_file = NULL; | |
229a7b63 | 4476 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 4477 | return 0; |
b5dba59e JA |
4478 | } |
4479 | ||
3529d8c2 | 4480 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4481 | { |
4482 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4483 | |
4484 | if (!req->file) | |
4485 | return -EBADF; | |
5d17b4a4 JA |
4486 | |
4487 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4488 | return -EINVAL; | |
4489 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4490 | return -EINVAL; | |
4491 | ||
8ed8d3c3 JA |
4492 | req->sync.off = READ_ONCE(sqe->off); |
4493 | req->sync.len = READ_ONCE(sqe->len); | |
4494 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4495 | return 0; |
4496 | } | |
4497 | ||
ac45abc0 | 4498 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4499 | { |
8ed8d3c3 JA |
4500 | int ret; |
4501 | ||
ac45abc0 PB |
4502 | /* sync_file_range always requires a blocking context */ |
4503 | if (force_nonblock) | |
4504 | return -EAGAIN; | |
4505 | ||
9adbd45d | 4506 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4507 | req->sync.flags); |
4508 | if (ret < 0) | |
4509 | req_set_fail_links(req); | |
e1e16097 | 4510 | io_req_complete(req, ret); |
5d17b4a4 JA |
4511 | return 0; |
4512 | } | |
4513 | ||
469956e8 | 4514 | #if defined(CONFIG_NET) |
02d27d89 PB |
4515 | static int io_setup_async_msg(struct io_kiocb *req, |
4516 | struct io_async_msghdr *kmsg) | |
4517 | { | |
e8c2bc1f JA |
4518 | struct io_async_msghdr *async_msg = req->async_data; |
4519 | ||
4520 | if (async_msg) | |
02d27d89 | 4521 | return -EAGAIN; |
e8c2bc1f | 4522 | if (io_alloc_async_data(req)) { |
02d27d89 PB |
4523 | if (kmsg->iov != kmsg->fast_iov) |
4524 | kfree(kmsg->iov); | |
4525 | return -ENOMEM; | |
4526 | } | |
e8c2bc1f | 4527 | async_msg = req->async_data; |
02d27d89 | 4528 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4529 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
02d27d89 PB |
4530 | return -EAGAIN; |
4531 | } | |
4532 | ||
2ae523ed PB |
4533 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4534 | struct io_async_msghdr *iomsg) | |
4535 | { | |
4536 | iomsg->iov = iomsg->fast_iov; | |
4537 | iomsg->msg.msg_name = &iomsg->addr; | |
4538 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4539 | req->sr_msg.msg_flags, &iomsg->iov); | |
4540 | } | |
4541 | ||
3529d8c2 | 4542 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4543 | { |
e8c2bc1f | 4544 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4545 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4546 | int ret; |
03b1230c | 4547 | |
d2b6f48b PB |
4548 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4549 | return -EINVAL; | |
4550 | ||
e47293fd | 4551 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4552 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4553 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4554 | |
d8768362 JA |
4555 | #ifdef CONFIG_COMPAT |
4556 | if (req->ctx->compat) | |
4557 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4558 | #endif | |
4559 | ||
e8c2bc1f | 4560 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4561 | return 0; |
e8c2bc1f | 4562 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4563 | if (!ret) |
4564 | req->flags |= REQ_F_NEED_CLEANUP; | |
4565 | return ret; | |
03b1230c JA |
4566 | } |
4567 | ||
229a7b63 JA |
4568 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4569 | struct io_comp_state *cs) | |
aa1fa28f | 4570 | { |
6b754c8b | 4571 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4572 | struct socket *sock; |
7a7cacba | 4573 | unsigned flags; |
0fa03c62 JA |
4574 | int ret; |
4575 | ||
0fa03c62 | 4576 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4577 | if (unlikely(!sock)) |
4578 | return ret; | |
3529d8c2 | 4579 | |
e8c2bc1f JA |
4580 | if (req->async_data) { |
4581 | kmsg = req->async_data; | |
4582 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4583 | /* if iov is set, it's allocated already */ |
4584 | if (!kmsg->iov) | |
4585 | kmsg->iov = kmsg->fast_iov; | |
4586 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4587 | } else { | |
4588 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4589 | if (ret) | |
4590 | return ret; | |
4591 | kmsg = &iomsg; | |
0fa03c62 | 4592 | } |
0fa03c62 | 4593 | |
7a7cacba PB |
4594 | flags = req->sr_msg.msg_flags; |
4595 | if (flags & MSG_DONTWAIT) | |
4596 | req->flags |= REQ_F_NOWAIT; | |
4597 | else if (force_nonblock) | |
4598 | flags |= MSG_DONTWAIT; | |
e47293fd | 4599 | |
7a7cacba PB |
4600 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4601 | if (force_nonblock && ret == -EAGAIN) | |
4602 | return io_setup_async_msg(req, kmsg); | |
4603 | if (ret == -ERESTARTSYS) | |
4604 | ret = -EINTR; | |
0fa03c62 | 4605 | |
6b754c8b | 4606 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4607 | kfree(kmsg->iov); |
99bc4c38 | 4608 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4609 | if (ret < 0) |
4610 | req_set_fail_links(req); | |
229a7b63 | 4611 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4612 | return 0; |
03b1230c | 4613 | } |
aa1fa28f | 4614 | |
229a7b63 JA |
4615 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4616 | struct io_comp_state *cs) | |
fddaface | 4617 | { |
7a7cacba PB |
4618 | struct io_sr_msg *sr = &req->sr_msg; |
4619 | struct msghdr msg; | |
4620 | struct iovec iov; | |
fddaface | 4621 | struct socket *sock; |
7a7cacba | 4622 | unsigned flags; |
fddaface JA |
4623 | int ret; |
4624 | ||
fddaface | 4625 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4626 | if (unlikely(!sock)) |
4627 | return ret; | |
fddaface | 4628 | |
7a7cacba PB |
4629 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4630 | if (unlikely(ret)) | |
14db8411 | 4631 | return ret; |
fddaface | 4632 | |
7a7cacba PB |
4633 | msg.msg_name = NULL; |
4634 | msg.msg_control = NULL; | |
4635 | msg.msg_controllen = 0; | |
4636 | msg.msg_namelen = 0; | |
fddaface | 4637 | |
7a7cacba PB |
4638 | flags = req->sr_msg.msg_flags; |
4639 | if (flags & MSG_DONTWAIT) | |
4640 | req->flags |= REQ_F_NOWAIT; | |
4641 | else if (force_nonblock) | |
4642 | flags |= MSG_DONTWAIT; | |
fddaface | 4643 | |
7a7cacba PB |
4644 | msg.msg_flags = flags; |
4645 | ret = sock_sendmsg(sock, &msg); | |
4646 | if (force_nonblock && ret == -EAGAIN) | |
4647 | return -EAGAIN; | |
4648 | if (ret == -ERESTARTSYS) | |
4649 | ret = -EINTR; | |
fddaface | 4650 | |
fddaface JA |
4651 | if (ret < 0) |
4652 | req_set_fail_links(req); | |
229a7b63 | 4653 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4654 | return 0; |
fddaface JA |
4655 | } |
4656 | ||
1400e697 PB |
4657 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4658 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4659 | { |
4660 | struct io_sr_msg *sr = &req->sr_msg; | |
4661 | struct iovec __user *uiov; | |
4662 | size_t iov_len; | |
4663 | int ret; | |
4664 | ||
1400e697 PB |
4665 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4666 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4667 | if (ret) |
4668 | return ret; | |
4669 | ||
4670 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4671 | if (iov_len > 1) | |
4672 | return -EINVAL; | |
1400e697 | 4673 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4674 | return -EFAULT; |
1400e697 PB |
4675 | sr->len = iomsg->iov[0].iov_len; |
4676 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4677 | sr->len); |
1400e697 | 4678 | iomsg->iov = NULL; |
52de1fe1 | 4679 | } else { |
89cd35c5 CH |
4680 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
4681 | &iomsg->iov, &iomsg->msg.msg_iter, | |
4682 | false); | |
52de1fe1 JA |
4683 | if (ret > 0) |
4684 | ret = 0; | |
4685 | } | |
4686 | ||
4687 | return ret; | |
4688 | } | |
4689 | ||
4690 | #ifdef CONFIG_COMPAT | |
4691 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4692 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4693 | { |
4694 | struct compat_msghdr __user *msg_compat; | |
4695 | struct io_sr_msg *sr = &req->sr_msg; | |
4696 | struct compat_iovec __user *uiov; | |
4697 | compat_uptr_t ptr; | |
4698 | compat_size_t len; | |
4699 | int ret; | |
4700 | ||
270a5940 | 4701 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4702 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4703 | &ptr, &len); |
4704 | if (ret) | |
4705 | return ret; | |
4706 | ||
4707 | uiov = compat_ptr(ptr); | |
4708 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4709 | compat_ssize_t clen; | |
4710 | ||
4711 | if (len > 1) | |
4712 | return -EINVAL; | |
4713 | if (!access_ok(uiov, sizeof(*uiov))) | |
4714 | return -EFAULT; | |
4715 | if (__get_user(clen, &uiov->iov_len)) | |
4716 | return -EFAULT; | |
4717 | if (clen < 0) | |
4718 | return -EINVAL; | |
1400e697 PB |
4719 | sr->len = iomsg->iov[0].iov_len; |
4720 | iomsg->iov = NULL; | |
52de1fe1 | 4721 | } else { |
89cd35c5 CH |
4722 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
4723 | UIO_FASTIOV, &iomsg->iov, | |
4724 | &iomsg->msg.msg_iter, true); | |
52de1fe1 JA |
4725 | if (ret < 0) |
4726 | return ret; | |
4727 | } | |
4728 | ||
4729 | return 0; | |
4730 | } | |
4731 | #endif | |
4732 | ||
1400e697 PB |
4733 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4734 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4735 | { |
1400e697 PB |
4736 | iomsg->msg.msg_name = &iomsg->addr; |
4737 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4738 | |
4739 | #ifdef CONFIG_COMPAT | |
4740 | if (req->ctx->compat) | |
1400e697 | 4741 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4742 | #endif |
52de1fe1 | 4743 | |
1400e697 | 4744 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4745 | } |
4746 | ||
bcda7baa | 4747 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4748 | bool needs_lock) |
bcda7baa JA |
4749 | { |
4750 | struct io_sr_msg *sr = &req->sr_msg; | |
4751 | struct io_buffer *kbuf; | |
4752 | ||
bcda7baa JA |
4753 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4754 | if (IS_ERR(kbuf)) | |
4755 | return kbuf; | |
4756 | ||
4757 | sr->kbuf = kbuf; | |
4758 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4759 | return kbuf; |
fddaface JA |
4760 | } |
4761 | ||
7fbb1b54 PB |
4762 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4763 | { | |
4764 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4765 | } | |
4766 | ||
3529d8c2 JA |
4767 | static int io_recvmsg_prep(struct io_kiocb *req, |
4768 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4769 | { |
e8c2bc1f | 4770 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4771 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4772 | int ret; |
3529d8c2 | 4773 | |
d2b6f48b PB |
4774 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4775 | return -EINVAL; | |
4776 | ||
3529d8c2 | 4777 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4778 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4779 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4780 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4781 | |
d8768362 JA |
4782 | #ifdef CONFIG_COMPAT |
4783 | if (req->ctx->compat) | |
4784 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4785 | #endif | |
4786 | ||
e8c2bc1f | 4787 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
5f798bea | 4788 | return 0; |
e8c2bc1f | 4789 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4790 | if (!ret) |
4791 | req->flags |= REQ_F_NEED_CLEANUP; | |
4792 | return ret; | |
aa1fa28f JA |
4793 | } |
4794 | ||
229a7b63 JA |
4795 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4796 | struct io_comp_state *cs) | |
aa1fa28f | 4797 | { |
6b754c8b | 4798 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4799 | struct socket *sock; |
7fbb1b54 | 4800 | struct io_buffer *kbuf; |
7a7cacba | 4801 | unsigned flags; |
52de1fe1 | 4802 | int ret, cflags = 0; |
03b1230c | 4803 | |
03b1230c | 4804 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4805 | if (unlikely(!sock)) |
4806 | return ret; | |
3529d8c2 | 4807 | |
e8c2bc1f JA |
4808 | if (req->async_data) { |
4809 | kmsg = req->async_data; | |
4810 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4811 | /* if iov is set, it's allocated already */ |
4812 | if (!kmsg->iov) | |
4813 | kmsg->iov = kmsg->fast_iov; | |
4814 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4815 | } else { | |
4816 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4817 | if (ret) | |
681fda8d | 4818 | return ret; |
7a7cacba PB |
4819 | kmsg = &iomsg; |
4820 | } | |
03b1230c | 4821 | |
bc02ef33 | 4822 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4823 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4824 | if (IS_ERR(kbuf)) |
52de1fe1 | 4825 | return PTR_ERR(kbuf); |
7a7cacba PB |
4826 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4827 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4828 | 1, req->sr_msg.len); | |
4829 | } | |
52de1fe1 | 4830 | |
7a7cacba PB |
4831 | flags = req->sr_msg.msg_flags; |
4832 | if (flags & MSG_DONTWAIT) | |
4833 | req->flags |= REQ_F_NOWAIT; | |
4834 | else if (force_nonblock) | |
4835 | flags |= MSG_DONTWAIT; | |
e47293fd | 4836 | |
7a7cacba PB |
4837 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4838 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4839 | if (force_nonblock && ret == -EAGAIN) |
4840 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4841 | if (ret == -ERESTARTSYS) |
4842 | ret = -EINTR; | |
03b1230c | 4843 | |
7fbb1b54 PB |
4844 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4845 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4846 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4847 | kfree(kmsg->iov); |
99bc4c38 | 4848 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4849 | if (ret < 0) |
4850 | req_set_fail_links(req); | |
229a7b63 | 4851 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4852 | return 0; |
0fa03c62 | 4853 | } |
5d17b4a4 | 4854 | |
229a7b63 JA |
4855 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4856 | struct io_comp_state *cs) | |
fddaface | 4857 | { |
6b754c8b | 4858 | struct io_buffer *kbuf; |
7a7cacba PB |
4859 | struct io_sr_msg *sr = &req->sr_msg; |
4860 | struct msghdr msg; | |
4861 | void __user *buf = sr->buf; | |
fddaface | 4862 | struct socket *sock; |
7a7cacba PB |
4863 | struct iovec iov; |
4864 | unsigned flags; | |
bcda7baa | 4865 | int ret, cflags = 0; |
fddaface | 4866 | |
fddaface | 4867 | sock = sock_from_file(req->file, &ret); |
7a7cacba PB |
4868 | if (unlikely(!sock)) |
4869 | return ret; | |
fddaface | 4870 | |
bc02ef33 | 4871 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4872 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4873 | if (IS_ERR(kbuf)) |
4874 | return PTR_ERR(kbuf); | |
7a7cacba | 4875 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4876 | } |
bcda7baa | 4877 | |
7a7cacba | 4878 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4879 | if (unlikely(ret)) |
4880 | goto out_free; | |
fddaface | 4881 | |
7a7cacba PB |
4882 | msg.msg_name = NULL; |
4883 | msg.msg_control = NULL; | |
4884 | msg.msg_controllen = 0; | |
4885 | msg.msg_namelen = 0; | |
4886 | msg.msg_iocb = NULL; | |
4887 | msg.msg_flags = 0; | |
fddaface | 4888 | |
7a7cacba PB |
4889 | flags = req->sr_msg.msg_flags; |
4890 | if (flags & MSG_DONTWAIT) | |
4891 | req->flags |= REQ_F_NOWAIT; | |
4892 | else if (force_nonblock) | |
4893 | flags |= MSG_DONTWAIT; | |
4894 | ||
4895 | ret = sock_recvmsg(sock, &msg, flags); | |
4896 | if (force_nonblock && ret == -EAGAIN) | |
4897 | return -EAGAIN; | |
4898 | if (ret == -ERESTARTSYS) | |
4899 | ret = -EINTR; | |
14c32eee | 4900 | out_free: |
7fbb1b54 PB |
4901 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4902 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4903 | if (ret < 0) |
4904 | req_set_fail_links(req); | |
229a7b63 | 4905 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4906 | return 0; |
fddaface JA |
4907 | } |
4908 | ||
3529d8c2 | 4909 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4910 | { |
8ed8d3c3 JA |
4911 | struct io_accept *accept = &req->accept; |
4912 | ||
14587a46 | 4913 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4914 | return -EINVAL; |
8042d6ce | 4915 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4916 | return -EINVAL; |
4917 | ||
d55e5f5b JA |
4918 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4919 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4920 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4921 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4922 | return 0; |
8ed8d3c3 | 4923 | } |
17f2fe35 | 4924 | |
229a7b63 JA |
4925 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4926 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
4927 | { |
4928 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 4929 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4930 | int ret; |
4931 | ||
e697deed JX |
4932 | if (req->file->f_flags & O_NONBLOCK) |
4933 | req->flags |= REQ_F_NOWAIT; | |
4934 | ||
8ed8d3c3 | 4935 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4936 | accept->addr_len, accept->flags, |
4937 | accept->nofile); | |
8ed8d3c3 | 4938 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4939 | return -EAGAIN; |
ac45abc0 PB |
4940 | if (ret < 0) { |
4941 | if (ret == -ERESTARTSYS) | |
4942 | ret = -EINTR; | |
4e88d6e7 | 4943 | req_set_fail_links(req); |
ac45abc0 | 4944 | } |
229a7b63 | 4945 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 4946 | return 0; |
8ed8d3c3 JA |
4947 | } |
4948 | ||
3529d8c2 | 4949 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4950 | { |
3529d8c2 | 4951 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 4952 | struct io_async_connect *io = req->async_data; |
f499a021 | 4953 | |
14587a46 | 4954 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4955 | return -EINVAL; |
4956 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4957 | return -EINVAL; | |
4958 | ||
3529d8c2 JA |
4959 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4960 | conn->addr_len = READ_ONCE(sqe->addr2); | |
4961 | ||
4962 | if (!io) | |
4963 | return 0; | |
4964 | ||
4965 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 4966 | &io->address); |
f499a021 JA |
4967 | } |
4968 | ||
229a7b63 JA |
4969 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
4970 | struct io_comp_state *cs) | |
f8e85cf2 | 4971 | { |
e8c2bc1f | 4972 | struct io_async_connect __io, *io; |
f8e85cf2 | 4973 | unsigned file_flags; |
3fbb51c1 | 4974 | int ret; |
f8e85cf2 | 4975 | |
e8c2bc1f JA |
4976 | if (req->async_data) { |
4977 | io = req->async_data; | |
f499a021 | 4978 | } else { |
3529d8c2 JA |
4979 | ret = move_addr_to_kernel(req->connect.addr, |
4980 | req->connect.addr_len, | |
e8c2bc1f | 4981 | &__io.address); |
f499a021 JA |
4982 | if (ret) |
4983 | goto out; | |
4984 | io = &__io; | |
4985 | } | |
4986 | ||
3fbb51c1 JA |
4987 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4988 | ||
e8c2bc1f | 4989 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4990 | req->connect.addr_len, file_flags); |
87f80d62 | 4991 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4992 | if (req->async_data) |
b7bb4f7d | 4993 | return -EAGAIN; |
e8c2bc1f | 4994 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4995 | ret = -ENOMEM; |
4996 | goto out; | |
4997 | } | |
e8c2bc1f JA |
4998 | io = req->async_data; |
4999 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5000 | return -EAGAIN; |
f499a021 | 5001 | } |
f8e85cf2 JA |
5002 | if (ret == -ERESTARTSYS) |
5003 | ret = -EINTR; | |
f499a021 | 5004 | out: |
4e88d6e7 JA |
5005 | if (ret < 0) |
5006 | req_set_fail_links(req); | |
229a7b63 | 5007 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 5008 | return 0; |
469956e8 Y |
5009 | } |
5010 | #else /* !CONFIG_NET */ | |
5011 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5012 | { | |
f8e85cf2 | 5013 | return -EOPNOTSUPP; |
f8e85cf2 JA |
5014 | } |
5015 | ||
1e16c2f9 RD |
5016 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
5017 | struct io_comp_state *cs) | |
469956e8 Y |
5018 | { |
5019 | return -EOPNOTSUPP; | |
5020 | } | |
5021 | ||
1e16c2f9 RD |
5022 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
5023 | struct io_comp_state *cs) | |
469956e8 Y |
5024 | { |
5025 | return -EOPNOTSUPP; | |
5026 | } | |
5027 | ||
5028 | static int io_recvmsg_prep(struct io_kiocb *req, | |
5029 | const struct io_uring_sqe *sqe) | |
5030 | { | |
5031 | return -EOPNOTSUPP; | |
5032 | } | |
5033 | ||
1e16c2f9 RD |
5034 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
5035 | struct io_comp_state *cs) | |
469956e8 Y |
5036 | { |
5037 | return -EOPNOTSUPP; | |
5038 | } | |
5039 | ||
1e16c2f9 RD |
5040 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
5041 | struct io_comp_state *cs) | |
469956e8 Y |
5042 | { |
5043 | return -EOPNOTSUPP; | |
5044 | } | |
5045 | ||
5046 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5047 | { | |
5048 | return -EOPNOTSUPP; | |
5049 | } | |
5050 | ||
1e16c2f9 RD |
5051 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
5052 | struct io_comp_state *cs) | |
469956e8 Y |
5053 | { |
5054 | return -EOPNOTSUPP; | |
5055 | } | |
ce593a6c | 5056 | |
469956e8 Y |
5057 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5058 | { | |
5059 | return -EOPNOTSUPP; | |
5060 | } | |
5061 | ||
1e16c2f9 RD |
5062 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5063 | struct io_comp_state *cs) | |
469956e8 | 5064 | { |
f8e85cf2 | 5065 | return -EOPNOTSUPP; |
ce593a6c | 5066 | } |
469956e8 | 5067 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5068 | |
d7718a9d JA |
5069 | struct io_poll_table { |
5070 | struct poll_table_struct pt; | |
5071 | struct io_kiocb *req; | |
5072 | int error; | |
5073 | }; | |
ce593a6c | 5074 | |
d7718a9d JA |
5075 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5076 | __poll_t mask, task_work_func_t func) | |
5077 | { | |
fd7d6de2 | 5078 | bool twa_signal_ok; |
aa96bf8a | 5079 | int ret; |
d7718a9d JA |
5080 | |
5081 | /* for instances that support it check for an event match first: */ | |
5082 | if (mask && !(mask & poll->events)) | |
5083 | return 0; | |
5084 | ||
5085 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5086 | ||
5087 | list_del_init(&poll->wait.entry); | |
5088 | ||
d7718a9d JA |
5089 | req->result = mask; |
5090 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
5091 | percpu_ref_get(&req->ctx->refs); |
5092 | ||
fd7d6de2 JA |
5093 | /* |
5094 | * If we using the signalfd wait_queue_head for this wakeup, then | |
5095 | * it's not safe to use TWA_SIGNAL as we could be recursing on the | |
5096 | * tsk->sighand->siglock on doing the wakeup. Should not be needed | |
5097 | * either, as the normal wakeup will suffice. | |
5098 | */ | |
5099 | twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh); | |
5100 | ||
d7718a9d | 5101 | /* |
e3aabf95 JA |
5102 | * If this fails, then the task is exiting. When a task exits, the |
5103 | * work gets canceled, so just cancel this request as well instead | |
5104 | * of executing it. We can't safely execute it anyway, as we may not | |
5105 | * have the needed state needed for it anyway. | |
d7718a9d | 5106 | */ |
87c4311f | 5107 | ret = io_req_task_work_add(req, twa_signal_ok); |
aa96bf8a | 5108 | if (unlikely(ret)) { |
c2c4c83c JA |
5109 | struct task_struct *tsk; |
5110 | ||
e3aabf95 | 5111 | WRITE_ONCE(poll->canceled, true); |
aa96bf8a | 5112 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 5113 | task_work_add(tsk, &req->task_work, TWA_NONE); |
ce593a6c | 5114 | wake_up_process(tsk); |
aa96bf8a | 5115 | } |
d7718a9d JA |
5116 | return 1; |
5117 | } | |
5118 | ||
74ce6ce4 JA |
5119 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5120 | __acquires(&req->ctx->completion_lock) | |
5121 | { | |
5122 | struct io_ring_ctx *ctx = req->ctx; | |
5123 | ||
5124 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5125 | struct poll_table_struct pt = { ._key = poll->events }; | |
5126 | ||
5127 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5128 | } | |
5129 | ||
5130 | spin_lock_irq(&ctx->completion_lock); | |
5131 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5132 | add_wait_queue(poll->head, &poll->wait); | |
5133 | return true; | |
5134 | } | |
5135 | ||
5136 | return false; | |
5137 | } | |
5138 | ||
d4e7cd36 | 5139 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5140 | { |
e8c2bc1f | 5141 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5142 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5143 | return req->async_data; |
d4e7cd36 JA |
5144 | return req->apoll->double_poll; |
5145 | } | |
5146 | ||
5147 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5148 | { | |
5149 | if (req->opcode == IORING_OP_POLL_ADD) | |
5150 | return &req->poll; | |
5151 | return &req->apoll->poll; | |
5152 | } | |
5153 | ||
5154 | static void io_poll_remove_double(struct io_kiocb *req) | |
5155 | { | |
5156 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5157 | |
5158 | lockdep_assert_held(&req->ctx->completion_lock); | |
5159 | ||
5160 | if (poll && poll->head) { | |
5161 | struct wait_queue_head *head = poll->head; | |
5162 | ||
5163 | spin_lock(&head->lock); | |
5164 | list_del_init(&poll->wait.entry); | |
5165 | if (poll->wait.private) | |
5166 | refcount_dec(&req->refs); | |
5167 | poll->head = NULL; | |
5168 | spin_unlock(&head->lock); | |
5169 | } | |
5170 | } | |
5171 | ||
5172 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5173 | { | |
5174 | struct io_ring_ctx *ctx = req->ctx; | |
5175 | ||
d4e7cd36 | 5176 | io_poll_remove_double(req); |
18bceab1 JA |
5177 | req->poll.done = true; |
5178 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5179 | io_commit_cqring(ctx); | |
5180 | } | |
5181 | ||
dd221f46 | 5182 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5183 | { |
dd221f46 | 5184 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5185 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5186 | struct io_kiocb *nxt; |
18bceab1 JA |
5187 | |
5188 | if (io_poll_rewait(req, &req->poll)) { | |
5189 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5190 | } else { |
5191 | hash_del(&req->hash_node); | |
5192 | io_poll_complete(req, req->result, 0); | |
5193 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5194 | |
dd221f46 PB |
5195 | nxt = io_put_req_find_next(req); |
5196 | io_cqring_ev_posted(ctx); | |
5197 | if (nxt) | |
5198 | __io_req_task_submit(nxt); | |
5199 | } | |
18bceab1 | 5200 | |
6d816e08 | 5201 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5202 | } |
5203 | ||
5204 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5205 | int sync, void *key) | |
5206 | { | |
5207 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5208 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5209 | __poll_t mask = key_to_poll(key); |
5210 | ||
5211 | /* for instances that support it check for an event match first: */ | |
5212 | if (mask && !(mask & poll->events)) | |
5213 | return 0; | |
5214 | ||
8706e04e JA |
5215 | list_del_init(&wait->entry); |
5216 | ||
807abcb0 | 5217 | if (poll && poll->head) { |
18bceab1 JA |
5218 | bool done; |
5219 | ||
807abcb0 JA |
5220 | spin_lock(&poll->head->lock); |
5221 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5222 | if (!done) |
807abcb0 | 5223 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5224 | /* make sure double remove sees this as being gone */ |
5225 | wait->private = NULL; | |
807abcb0 | 5226 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5227 | if (!done) { |
5228 | /* use wait func handler, so it matches the rq type */ | |
5229 | poll->wait.func(&poll->wait, mode, sync, key); | |
5230 | } | |
18bceab1 JA |
5231 | } |
5232 | refcount_dec(&req->refs); | |
5233 | return 1; | |
5234 | } | |
5235 | ||
5236 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5237 | wait_queue_func_t wake_func) | |
5238 | { | |
5239 | poll->head = NULL; | |
5240 | poll->done = false; | |
5241 | poll->canceled = false; | |
5242 | poll->events = events; | |
5243 | INIT_LIST_HEAD(&poll->wait.entry); | |
5244 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5245 | } | |
5246 | ||
5247 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5248 | struct wait_queue_head *head, |
5249 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5250 | { |
5251 | struct io_kiocb *req = pt->req; | |
5252 | ||
5253 | /* | |
5254 | * If poll->head is already set, it's because the file being polled | |
5255 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5256 | * for write). Setup a separate io_poll_iocb if this happens. | |
5257 | */ | |
5258 | if (unlikely(poll->head)) { | |
58852d4d PB |
5259 | struct io_poll_iocb *poll_one = poll; |
5260 | ||
18bceab1 | 5261 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5262 | if (*poll_ptr) { |
18bceab1 JA |
5263 | pt->error = -EINVAL; |
5264 | return; | |
5265 | } | |
5266 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5267 | if (!poll) { | |
5268 | pt->error = -ENOMEM; | |
5269 | return; | |
5270 | } | |
58852d4d | 5271 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5272 | refcount_inc(&req->refs); |
5273 | poll->wait.private = req; | |
807abcb0 | 5274 | *poll_ptr = poll; |
18bceab1 JA |
5275 | } |
5276 | ||
5277 | pt->error = 0; | |
5278 | poll->head = head; | |
a31eb4a2 JX |
5279 | |
5280 | if (poll->events & EPOLLEXCLUSIVE) | |
5281 | add_wait_queue_exclusive(head, &poll->wait); | |
5282 | else | |
5283 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5284 | } |
5285 | ||
5286 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5287 | struct poll_table_struct *p) | |
5288 | { | |
5289 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5290 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5291 | |
807abcb0 | 5292 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5293 | } |
5294 | ||
d7718a9d JA |
5295 | static void io_async_task_func(struct callback_head *cb) |
5296 | { | |
5297 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5298 | struct async_poll *apoll = req->apoll; | |
5299 | struct io_ring_ctx *ctx = req->ctx; | |
5300 | ||
5301 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5302 | ||
74ce6ce4 | 5303 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5304 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5305 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5306 | return; |
d7718a9d JA |
5307 | } |
5308 | ||
31067255 | 5309 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5310 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5311 | hash_del(&req->hash_node); |
2bae047e | 5312 | |
d4e7cd36 | 5313 | io_poll_remove_double(req); |
74ce6ce4 JA |
5314 | spin_unlock_irq(&ctx->completion_lock); |
5315 | ||
0be0b0e3 PB |
5316 | if (!READ_ONCE(apoll->poll.canceled)) |
5317 | __io_req_task_submit(req); | |
5318 | else | |
5319 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5320 | |
6d816e08 | 5321 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5322 | kfree(apoll->double_poll); |
31067255 | 5323 | kfree(apoll); |
d7718a9d JA |
5324 | } |
5325 | ||
5326 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5327 | void *key) | |
5328 | { | |
5329 | struct io_kiocb *req = wait->private; | |
5330 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5331 | ||
5332 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5333 | key_to_poll(key)); | |
5334 | ||
5335 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5336 | } | |
5337 | ||
5338 | static void io_poll_req_insert(struct io_kiocb *req) | |
5339 | { | |
5340 | struct io_ring_ctx *ctx = req->ctx; | |
5341 | struct hlist_head *list; | |
5342 | ||
5343 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5344 | hlist_add_head(&req->hash_node, list); | |
5345 | } | |
5346 | ||
5347 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5348 | struct io_poll_iocb *poll, | |
5349 | struct io_poll_table *ipt, __poll_t mask, | |
5350 | wait_queue_func_t wake_func) | |
5351 | __acquires(&ctx->completion_lock) | |
5352 | { | |
5353 | struct io_ring_ctx *ctx = req->ctx; | |
5354 | bool cancel = false; | |
5355 | ||
4d52f338 | 5356 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5357 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5358 | poll->file = req->file; |
18bceab1 | 5359 | poll->wait.private = req; |
d7718a9d JA |
5360 | |
5361 | ipt->pt._key = mask; | |
5362 | ipt->req = req; | |
5363 | ipt->error = -EINVAL; | |
5364 | ||
d7718a9d JA |
5365 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5366 | ||
5367 | spin_lock_irq(&ctx->completion_lock); | |
5368 | if (likely(poll->head)) { | |
5369 | spin_lock(&poll->head->lock); | |
5370 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5371 | if (ipt->error) | |
5372 | cancel = true; | |
5373 | ipt->error = 0; | |
5374 | mask = 0; | |
5375 | } | |
5376 | if (mask || ipt->error) | |
5377 | list_del_init(&poll->wait.entry); | |
5378 | else if (cancel) | |
5379 | WRITE_ONCE(poll->canceled, true); | |
5380 | else if (!poll->done) /* actually waiting for an event */ | |
5381 | io_poll_req_insert(req); | |
5382 | spin_unlock(&poll->head->lock); | |
5383 | } | |
5384 | ||
5385 | return mask; | |
5386 | } | |
5387 | ||
5388 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5389 | { | |
5390 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5391 | struct io_ring_ctx *ctx = req->ctx; | |
5392 | struct async_poll *apoll; | |
5393 | struct io_poll_table ipt; | |
5394 | __poll_t mask, ret; | |
9dab14b8 | 5395 | int rw; |
d7718a9d JA |
5396 | |
5397 | if (!req->file || !file_can_poll(req->file)) | |
5398 | return false; | |
24c74678 | 5399 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5400 | return false; |
9dab14b8 JA |
5401 | if (def->pollin) |
5402 | rw = READ; | |
5403 | else if (def->pollout) | |
5404 | rw = WRITE; | |
5405 | else | |
5406 | return false; | |
5407 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5408 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5409 | return false; |
5410 | ||
5411 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5412 | if (unlikely(!apoll)) | |
5413 | return false; | |
807abcb0 | 5414 | apoll->double_poll = NULL; |
d7718a9d JA |
5415 | |
5416 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5417 | req->apoll = apoll; |
d7718a9d | 5418 | |
8755d97a | 5419 | mask = 0; |
d7718a9d | 5420 | if (def->pollin) |
8755d97a | 5421 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5422 | if (def->pollout) |
5423 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5424 | |
5425 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5426 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5427 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5428 | mask &= ~POLLIN; | |
5429 | ||
d7718a9d JA |
5430 | mask |= POLLERR | POLLPRI; |
5431 | ||
5432 | ipt.pt._qproc = io_async_queue_proc; | |
5433 | ||
5434 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5435 | io_async_wake); | |
a36da65c | 5436 | if (ret || ipt.error) { |
d4e7cd36 | 5437 | io_poll_remove_double(req); |
d7718a9d | 5438 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5439 | kfree(apoll->double_poll); |
d7718a9d JA |
5440 | kfree(apoll); |
5441 | return false; | |
5442 | } | |
5443 | spin_unlock_irq(&ctx->completion_lock); | |
5444 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5445 | apoll->poll.events); | |
5446 | return true; | |
5447 | } | |
5448 | ||
5449 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5450 | struct io_poll_iocb *poll) | |
221c5eb2 | 5451 | { |
b41e9852 | 5452 | bool do_complete = false; |
221c5eb2 JA |
5453 | |
5454 | spin_lock(&poll->head->lock); | |
5455 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5456 | if (!list_empty(&poll->wait.entry)) { |
5457 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5458 | do_complete = true; |
221c5eb2 JA |
5459 | } |
5460 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5461 | hash_del(&req->hash_node); |
d7718a9d JA |
5462 | return do_complete; |
5463 | } | |
5464 | ||
5465 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5466 | { | |
5467 | bool do_complete; | |
5468 | ||
d4e7cd36 JA |
5469 | io_poll_remove_double(req); |
5470 | ||
d7718a9d JA |
5471 | if (req->opcode == IORING_OP_POLL_ADD) { |
5472 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5473 | } else { | |
3bfa5bcb JA |
5474 | struct async_poll *apoll = req->apoll; |
5475 | ||
d7718a9d | 5476 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5477 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5478 | if (do_complete) { | |
d7718a9d | 5479 | io_put_req(req); |
807abcb0 | 5480 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5481 | kfree(apoll); |
5482 | } | |
b1f573bd XW |
5483 | } |
5484 | ||
b41e9852 JA |
5485 | if (do_complete) { |
5486 | io_cqring_fill_event(req, -ECANCELED); | |
5487 | io_commit_cqring(req->ctx); | |
f254ac04 | 5488 | req_set_fail_links(req); |
216578e5 | 5489 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5490 | } |
5491 | ||
5492 | return do_complete; | |
221c5eb2 JA |
5493 | } |
5494 | ||
76e1b642 JA |
5495 | /* |
5496 | * Returns true if we found and killed one or more poll requests | |
5497 | */ | |
6b81928d PB |
5498 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5499 | struct files_struct *files) | |
221c5eb2 | 5500 | { |
78076bb6 | 5501 | struct hlist_node *tmp; |
221c5eb2 | 5502 | struct io_kiocb *req; |
8e2e1faf | 5503 | int posted = 0, i; |
221c5eb2 JA |
5504 | |
5505 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5506 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5507 | struct hlist_head *list; | |
5508 | ||
5509 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5510 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5511 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5512 | posted += io_poll_remove_one(req); |
5513 | } | |
221c5eb2 JA |
5514 | } |
5515 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5516 | |
8e2e1faf JA |
5517 | if (posted) |
5518 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5519 | |
5520 | return posted != 0; | |
221c5eb2 JA |
5521 | } |
5522 | ||
47f46768 JA |
5523 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5524 | { | |
78076bb6 | 5525 | struct hlist_head *list; |
47f46768 JA |
5526 | struct io_kiocb *req; |
5527 | ||
78076bb6 JA |
5528 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5529 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5530 | if (sqe_addr != req->user_data) |
5531 | continue; | |
5532 | if (io_poll_remove_one(req)) | |
eac406c6 | 5533 | return 0; |
b41e9852 | 5534 | return -EALREADY; |
47f46768 JA |
5535 | } |
5536 | ||
5537 | return -ENOENT; | |
5538 | } | |
5539 | ||
3529d8c2 JA |
5540 | static int io_poll_remove_prep(struct io_kiocb *req, |
5541 | const struct io_uring_sqe *sqe) | |
0969e783 | 5542 | { |
0969e783 JA |
5543 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5544 | return -EINVAL; | |
5545 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5546 | sqe->poll_events) | |
5547 | return -EINVAL; | |
5548 | ||
018043be | 5549 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5550 | return 0; |
5551 | } | |
5552 | ||
221c5eb2 JA |
5553 | /* |
5554 | * Find a running poll command that matches one specified in sqe->addr, | |
5555 | * and remove it if found. | |
5556 | */ | |
fc4df999 | 5557 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5558 | { |
5559 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5560 | int ret; |
221c5eb2 | 5561 | |
221c5eb2 | 5562 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5563 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5564 | spin_unlock_irq(&ctx->completion_lock); |
5565 | ||
4e88d6e7 JA |
5566 | if (ret < 0) |
5567 | req_set_fail_links(req); | |
e1e16097 | 5568 | io_req_complete(req, ret); |
221c5eb2 JA |
5569 | return 0; |
5570 | } | |
5571 | ||
221c5eb2 JA |
5572 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5573 | void *key) | |
5574 | { | |
c2f2eb7d JA |
5575 | struct io_kiocb *req = wait->private; |
5576 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5577 | |
d7718a9d | 5578 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5579 | } |
5580 | ||
221c5eb2 JA |
5581 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5582 | struct poll_table_struct *p) | |
5583 | { | |
5584 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5585 | ||
e8c2bc1f | 5586 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5587 | } |
5588 | ||
3529d8c2 | 5589 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5590 | { |
5591 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5592 | u32 events; |
221c5eb2 JA |
5593 | |
5594 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5595 | return -EINVAL; | |
5596 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5597 | return -EINVAL; | |
5598 | ||
5769a351 JX |
5599 | events = READ_ONCE(sqe->poll32_events); |
5600 | #ifdef __BIG_ENDIAN | |
5601 | events = swahw32(events); | |
5602 | #endif | |
a31eb4a2 JX |
5603 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5604 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5605 | return 0; |
5606 | } | |
5607 | ||
014db007 | 5608 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5609 | { |
5610 | struct io_poll_iocb *poll = &req->poll; | |
5611 | struct io_ring_ctx *ctx = req->ctx; | |
5612 | struct io_poll_table ipt; | |
0969e783 | 5613 | __poll_t mask; |
0969e783 | 5614 | |
d7718a9d | 5615 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5616 | |
d7718a9d JA |
5617 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5618 | io_poll_wake); | |
221c5eb2 | 5619 | |
8c838788 | 5620 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5621 | ipt.error = 0; |
b0dd8a41 | 5622 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5623 | } |
221c5eb2 JA |
5624 | spin_unlock_irq(&ctx->completion_lock); |
5625 | ||
8c838788 JA |
5626 | if (mask) { |
5627 | io_cqring_ev_posted(ctx); | |
014db007 | 5628 | io_put_req(req); |
221c5eb2 | 5629 | } |
8c838788 | 5630 | return ipt.error; |
221c5eb2 JA |
5631 | } |
5632 | ||
5262f567 JA |
5633 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5634 | { | |
ad8a48ac JA |
5635 | struct io_timeout_data *data = container_of(timer, |
5636 | struct io_timeout_data, timer); | |
5637 | struct io_kiocb *req = data->req; | |
5638 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5639 | unsigned long flags; |
5640 | ||
5262f567 | 5641 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5642 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5643 | atomic_set(&req->ctx->cq_timeouts, |
5644 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5645 | ||
78e19bbe | 5646 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5647 | io_commit_cqring(ctx); |
5648 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5649 | ||
5650 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5651 | req_set_fail_links(req); |
5262f567 JA |
5652 | io_put_req(req); |
5653 | return HRTIMER_NORESTART; | |
5654 | } | |
5655 | ||
fbd15848 PB |
5656 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5657 | __u64 user_data) | |
47f46768 | 5658 | { |
fbd15848 | 5659 | struct io_timeout_data *io; |
47f46768 JA |
5660 | struct io_kiocb *req; |
5661 | int ret = -ENOENT; | |
5662 | ||
135fcde8 | 5663 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5664 | if (user_data == req->user_data) { |
47f46768 JA |
5665 | ret = 0; |
5666 | break; | |
5667 | } | |
5668 | } | |
5669 | ||
5670 | if (ret == -ENOENT) | |
fbd15848 PB |
5671 | return ERR_PTR(ret); |
5672 | ||
5673 | io = req->async_data; | |
5674 | ret = hrtimer_try_to_cancel(&io->timer); | |
5675 | if (ret == -1) | |
5676 | return ERR_PTR(-EALREADY); | |
5677 | list_del_init(&req->timeout.list); | |
5678 | return req; | |
5679 | } | |
47f46768 | 5680 | |
fbd15848 PB |
5681 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5682 | { | |
5683 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5684 | ||
5685 | if (IS_ERR(req)) | |
5686 | return PTR_ERR(req); | |
5687 | ||
5688 | req_set_fail_links(req); | |
5689 | io_cqring_fill_event(req, -ECANCELED); | |
5690 | io_put_req_deferred(req, 1); | |
5691 | return 0; | |
47f46768 JA |
5692 | } |
5693 | ||
9c8e11b3 PB |
5694 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5695 | struct timespec64 *ts, enum hrtimer_mode mode) | |
5696 | { | |
5697 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5698 | struct io_timeout_data *data; | |
5699 | ||
5700 | if (IS_ERR(req)) | |
5701 | return PTR_ERR(req); | |
5702 | ||
5703 | req->timeout.off = 0; /* noseq */ | |
5704 | data = req->async_data; | |
5705 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5706 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5707 | data->timer.function = io_timeout_fn; | |
5708 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5709 | return 0; | |
5710 | } | |
5711 | ||
3529d8c2 JA |
5712 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5713 | const struct io_uring_sqe *sqe) | |
b29472ee | 5714 | { |
9c8e11b3 PB |
5715 | struct io_timeout_rem *tr = &req->timeout_rem; |
5716 | ||
b29472ee JA |
5717 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5718 | return -EINVAL; | |
61710e43 DA |
5719 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5720 | return -EINVAL; | |
9c8e11b3 PB |
5721 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
5722 | return -EINVAL; | |
5723 | ||
5724 | tr->addr = READ_ONCE(sqe->addr); | |
5725 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5726 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5727 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5728 | return -EINVAL; | |
5729 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5730 | return -EFAULT; | |
5731 | } else if (tr->flags) { | |
5732 | /* timeout removal doesn't support flags */ | |
b29472ee | 5733 | return -EINVAL; |
9c8e11b3 | 5734 | } |
b29472ee | 5735 | |
b29472ee JA |
5736 | return 0; |
5737 | } | |
5738 | ||
11365043 JA |
5739 | /* |
5740 | * Remove or update an existing timeout command | |
5741 | */ | |
fc4df999 | 5742 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 | 5743 | { |
9c8e11b3 | 5744 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5745 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5746 | int ret; |
11365043 | 5747 | |
11365043 | 5748 | spin_lock_irq(&ctx->completion_lock); |
9c8e11b3 PB |
5749 | if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) { |
5750 | enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS) | |
5751 | ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; | |
5752 | ||
5753 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
5754 | } else { | |
5755 | ret = io_timeout_cancel(ctx, tr->addr); | |
5756 | } | |
11365043 | 5757 | |
47f46768 | 5758 | io_cqring_fill_event(req, ret); |
11365043 JA |
5759 | io_commit_cqring(ctx); |
5760 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5761 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5762 | if (ret < 0) |
5763 | req_set_fail_links(req); | |
ec9c02ad | 5764 | io_put_req(req); |
11365043 | 5765 | return 0; |
5262f567 JA |
5766 | } |
5767 | ||
3529d8c2 | 5768 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5769 | bool is_timeout_link) |
5262f567 | 5770 | { |
ad8a48ac | 5771 | struct io_timeout_data *data; |
a41525ab | 5772 | unsigned flags; |
56080b02 | 5773 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5774 | |
ad8a48ac | 5775 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5776 | return -EINVAL; |
ad8a48ac | 5777 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5778 | return -EINVAL; |
56080b02 | 5779 | if (off && is_timeout_link) |
2d28390a | 5780 | return -EINVAL; |
a41525ab JA |
5781 | flags = READ_ONCE(sqe->timeout_flags); |
5782 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5783 | return -EINVAL; |
bdf20073 | 5784 | |
bfe68a22 | 5785 | req->timeout.off = off; |
26a61679 | 5786 | |
e8c2bc1f | 5787 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5788 | return -ENOMEM; |
5789 | ||
e8c2bc1f | 5790 | data = req->async_data; |
ad8a48ac | 5791 | data->req = req; |
ad8a48ac JA |
5792 | |
5793 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5794 | return -EFAULT; |
5795 | ||
11365043 | 5796 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 5797 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 5798 | else |
ad8a48ac | 5799 | data->mode = HRTIMER_MODE_REL; |
11365043 | 5800 | |
ad8a48ac JA |
5801 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5802 | return 0; | |
5803 | } | |
5804 | ||
fc4df999 | 5805 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5806 | { |
ad8a48ac | 5807 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5808 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5809 | struct list_head *entry; |
bfe68a22 | 5810 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5811 | |
733f5c95 | 5812 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5813 | |
5262f567 JA |
5814 | /* |
5815 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5816 | * timeout event to be satisfied. If it isn't set, then this is |
5817 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5818 | */ |
8eb7e2d0 | 5819 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5820 | entry = ctx->timeout_list.prev; |
5821 | goto add; | |
5822 | } | |
5262f567 | 5823 | |
bfe68a22 PB |
5824 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5825 | req->timeout.target_seq = tail + off; | |
5262f567 JA |
5826 | |
5827 | /* | |
5828 | * Insertion sort, ensuring the first entry in the list is always | |
5829 | * the one we need first. | |
5830 | */ | |
5262f567 | 5831 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5832 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5833 | timeout.list); | |
5262f567 | 5834 | |
8eb7e2d0 | 5835 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5836 | continue; |
bfe68a22 PB |
5837 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5838 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5839 | break; |
5840 | } | |
93bd25bb | 5841 | add: |
135fcde8 | 5842 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5843 | data->timer.function = io_timeout_fn; |
5844 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5845 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5846 | return 0; |
5847 | } | |
5262f567 | 5848 | |
62755e35 JA |
5849 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5850 | { | |
5851 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5852 | ||
5853 | return req->user_data == (unsigned long) data; | |
5854 | } | |
5855 | ||
e977d6d3 | 5856 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5857 | { |
62755e35 | 5858 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5859 | int ret = 0; |
5860 | ||
4f26bda1 | 5861 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5862 | switch (cancel_ret) { |
5863 | case IO_WQ_CANCEL_OK: | |
5864 | ret = 0; | |
5865 | break; | |
5866 | case IO_WQ_CANCEL_RUNNING: | |
5867 | ret = -EALREADY; | |
5868 | break; | |
5869 | case IO_WQ_CANCEL_NOTFOUND: | |
5870 | ret = -ENOENT; | |
5871 | break; | |
5872 | } | |
5873 | ||
e977d6d3 JA |
5874 | return ret; |
5875 | } | |
5876 | ||
47f46768 JA |
5877 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5878 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5879 | int success_ret) |
47f46768 JA |
5880 | { |
5881 | unsigned long flags; | |
5882 | int ret; | |
5883 | ||
5884 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5885 | if (ret != -ENOENT) { | |
5886 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5887 | goto done; | |
5888 | } | |
5889 | ||
5890 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5891 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5892 | if (ret != -ENOENT) | |
5893 | goto done; | |
5894 | ret = io_poll_cancel(ctx, sqe_addr); | |
5895 | done: | |
b0dd8a41 JA |
5896 | if (!ret) |
5897 | ret = success_ret; | |
47f46768 JA |
5898 | io_cqring_fill_event(req, ret); |
5899 | io_commit_cqring(ctx); | |
5900 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5901 | io_cqring_ev_posted(ctx); | |
5902 | ||
4e88d6e7 JA |
5903 | if (ret < 0) |
5904 | req_set_fail_links(req); | |
014db007 | 5905 | io_put_req(req); |
47f46768 JA |
5906 | } |
5907 | ||
3529d8c2 JA |
5908 | static int io_async_cancel_prep(struct io_kiocb *req, |
5909 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5910 | { |
fbf23849 | 5911 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5912 | return -EINVAL; |
61710e43 DA |
5913 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5914 | return -EINVAL; | |
5915 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5916 | return -EINVAL; |
5917 | ||
fbf23849 JA |
5918 | req->cancel.addr = READ_ONCE(sqe->addr); |
5919 | return 0; | |
5920 | } | |
5921 | ||
014db007 | 5922 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
5923 | { |
5924 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5925 | |
014db007 | 5926 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5927 | return 0; |
5928 | } | |
5929 | ||
05f3fb3c JA |
5930 | static int io_files_update_prep(struct io_kiocb *req, |
5931 | const struct io_uring_sqe *sqe) | |
5932 | { | |
6ca56f84 JA |
5933 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5934 | return -EINVAL; | |
61710e43 DA |
5935 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5936 | return -EINVAL; | |
5937 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5938 | return -EINVAL; |
5939 | ||
5940 | req->files_update.offset = READ_ONCE(sqe->off); | |
5941 | req->files_update.nr_args = READ_ONCE(sqe->len); | |
5942 | if (!req->files_update.nr_args) | |
5943 | return -EINVAL; | |
5944 | req->files_update.arg = READ_ONCE(sqe->addr); | |
5945 | return 0; | |
5946 | } | |
5947 | ||
229a7b63 JA |
5948 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
5949 | struct io_comp_state *cs) | |
fbf23849 JA |
5950 | { |
5951 | struct io_ring_ctx *ctx = req->ctx; | |
05f3fb3c JA |
5952 | struct io_uring_files_update up; |
5953 | int ret; | |
fbf23849 | 5954 | |
f86cd20c | 5955 | if (force_nonblock) |
05f3fb3c | 5956 | return -EAGAIN; |
05f3fb3c JA |
5957 | |
5958 | up.offset = req->files_update.offset; | |
5959 | up.fds = req->files_update.arg; | |
5960 | ||
5961 | mutex_lock(&ctx->uring_lock); | |
5962 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); | |
5963 | mutex_unlock(&ctx->uring_lock); | |
5964 | ||
5965 | if (ret < 0) | |
5966 | req_set_fail_links(req); | |
229a7b63 | 5967 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
5968 | return 0; |
5969 | } | |
5970 | ||
bfe76559 | 5971 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5972 | { |
d625c6ee | 5973 | switch (req->opcode) { |
e781573e | 5974 | case IORING_OP_NOP: |
bfe76559 | 5975 | return 0; |
f67676d1 JA |
5976 | case IORING_OP_READV: |
5977 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5978 | case IORING_OP_READ: |
bfe76559 | 5979 | return io_read_prep(req, sqe); |
f67676d1 JA |
5980 | case IORING_OP_WRITEV: |
5981 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5982 | case IORING_OP_WRITE: |
bfe76559 | 5983 | return io_write_prep(req, sqe); |
0969e783 | 5984 | case IORING_OP_POLL_ADD: |
bfe76559 | 5985 | return io_poll_add_prep(req, sqe); |
0969e783 | 5986 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5987 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5988 | case IORING_OP_FSYNC: |
bfe76559 | 5989 | return io_prep_fsync(req, sqe); |
8ed8d3c3 | 5990 | case IORING_OP_SYNC_FILE_RANGE: |
bfe76559 | 5991 | return io_prep_sfr(req, sqe); |
03b1230c | 5992 | case IORING_OP_SENDMSG: |
fddaface | 5993 | case IORING_OP_SEND: |
bfe76559 | 5994 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5995 | case IORING_OP_RECVMSG: |
fddaface | 5996 | case IORING_OP_RECV: |
bfe76559 | 5997 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5998 | case IORING_OP_CONNECT: |
bfe76559 | 5999 | return io_connect_prep(req, sqe); |
2d28390a | 6000 | case IORING_OP_TIMEOUT: |
bfe76559 | 6001 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6002 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6003 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6004 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6005 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6006 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6007 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6008 | case IORING_OP_ACCEPT: |
bfe76559 | 6009 | return io_accept_prep(req, sqe); |
d63d1b5e | 6010 | case IORING_OP_FALLOCATE: |
bfe76559 | 6011 | return io_fallocate_prep(req, sqe); |
15b71abe | 6012 | case IORING_OP_OPENAT: |
bfe76559 | 6013 | return io_openat_prep(req, sqe); |
b5dba59e | 6014 | case IORING_OP_CLOSE: |
bfe76559 | 6015 | return io_close_prep(req, sqe); |
05f3fb3c | 6016 | case IORING_OP_FILES_UPDATE: |
bfe76559 | 6017 | return io_files_update_prep(req, sqe); |
eddc7ef5 | 6018 | case IORING_OP_STATX: |
bfe76559 | 6019 | return io_statx_prep(req, sqe); |
4840e418 | 6020 | case IORING_OP_FADVISE: |
bfe76559 | 6021 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6022 | case IORING_OP_MADVISE: |
bfe76559 | 6023 | return io_madvise_prep(req, sqe); |
cebdb986 | 6024 | case IORING_OP_OPENAT2: |
bfe76559 | 6025 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6026 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6027 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6028 | case IORING_OP_SPLICE: |
bfe76559 | 6029 | return io_splice_prep(req, sqe); |
ddf0322d | 6030 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6031 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6032 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6033 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6034 | case IORING_OP_TEE: |
bfe76559 | 6035 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6036 | case IORING_OP_SHUTDOWN: |
6037 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6038 | case IORING_OP_RENAMEAT: |
6039 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6040 | case IORING_OP_UNLINKAT: |
6041 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6042 | } |
6043 | ||
bfe76559 PB |
6044 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6045 | req->opcode); | |
6046 | return-EINVAL; | |
6047 | } | |
6048 | ||
6049 | static int io_req_defer_prep(struct io_kiocb *req, | |
6050 | const struct io_uring_sqe *sqe) | |
6051 | { | |
bfe76559 PB |
6052 | if (!sqe) |
6053 | return 0; | |
6054 | if (io_alloc_async_data(req)) | |
6055 | return -EAGAIN; | |
bfe76559 | 6056 | return io_req_prep(req, sqe); |
f67676d1 JA |
6057 | } |
6058 | ||
9cf7c104 PB |
6059 | static u32 io_get_sequence(struct io_kiocb *req) |
6060 | { | |
6061 | struct io_kiocb *pos; | |
6062 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6063 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6064 | |
f2f87370 PB |
6065 | io_for_each_link(pos, req) |
6066 | nr_reqs++; | |
9cf7c104 PB |
6067 | |
6068 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6069 | return total_submitted - nr_reqs; | |
6070 | } | |
6071 | ||
3529d8c2 | 6072 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 6073 | { |
a197f664 | 6074 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6075 | struct io_defer_entry *de; |
f67676d1 | 6076 | int ret; |
9cf7c104 | 6077 | u32 seq; |
de0617e4 | 6078 | |
9d858b21 | 6079 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6080 | if (likely(list_empty_careful(&ctx->defer_list) && |
6081 | !(req->flags & REQ_F_IO_DRAIN))) | |
6082 | return 0; | |
6083 | ||
6084 | seq = io_get_sequence(req); | |
6085 | /* Still a chance to pass the sequence check */ | |
6086 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6087 | return 0; |
6088 | ||
e8c2bc1f | 6089 | if (!req->async_data) { |
650b5481 | 6090 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6091 | if (ret) |
650b5481 PB |
6092 | return ret; |
6093 | } | |
cbdcb435 | 6094 | io_prep_async_link(req); |
27dc8338 PB |
6095 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6096 | if (!de) | |
6097 | return -ENOMEM; | |
2d28390a | 6098 | |
de0617e4 | 6099 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6100 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6101 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6102 | kfree(de); |
ae34817b PB |
6103 | io_queue_async_work(req); |
6104 | return -EIOCBQUEUED; | |
de0617e4 JA |
6105 | } |
6106 | ||
915967f6 | 6107 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6108 | de->req = req; |
9cf7c104 | 6109 | de->seq = seq; |
27dc8338 | 6110 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6111 | spin_unlock_irq(&ctx->completion_lock); |
6112 | return -EIOCBQUEUED; | |
6113 | } | |
6114 | ||
f573d384 | 6115 | static void io_req_drop_files(struct io_kiocb *req) |
99bc4c38 | 6116 | { |
f573d384 | 6117 | struct io_ring_ctx *ctx = req->ctx; |
c98de08c | 6118 | struct io_uring_task *tctx = req->task->io_uring; |
f573d384 JA |
6119 | unsigned long flags; |
6120 | ||
6121 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
6122 | list_del(&req->inflight_entry); | |
c98de08c PB |
6123 | if (atomic_read(&tctx->in_idle)) |
6124 | wake_up(&tctx->wait); | |
f573d384 JA |
6125 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
6126 | req->flags &= ~REQ_F_INFLIGHT; | |
98447d65 JA |
6127 | put_files_struct(req->work.identity->files); |
6128 | put_nsproxy(req->work.identity->nsproxy); | |
dfead8a8 | 6129 | req->work.flags &= ~IO_WQ_WORK_FILES; |
f573d384 | 6130 | } |
99bc4c38 | 6131 | |
3ca405eb | 6132 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6133 | { |
0e1b6fe3 PB |
6134 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6135 | switch (req->opcode) { | |
6136 | case IORING_OP_READV: | |
6137 | case IORING_OP_READ_FIXED: | |
6138 | case IORING_OP_READ: | |
bcda7baa | 6139 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6140 | break; |
6141 | case IORING_OP_RECVMSG: | |
6142 | case IORING_OP_RECV: | |
bcda7baa | 6143 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6144 | break; |
6145 | } | |
6146 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6147 | } |
6148 | ||
0e1b6fe3 PB |
6149 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6150 | switch (req->opcode) { | |
6151 | case IORING_OP_READV: | |
6152 | case IORING_OP_READ_FIXED: | |
6153 | case IORING_OP_READ: | |
6154 | case IORING_OP_WRITEV: | |
6155 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6156 | case IORING_OP_WRITE: { |
6157 | struct io_async_rw *io = req->async_data; | |
6158 | if (io->free_iovec) | |
6159 | kfree(io->free_iovec); | |
0e1b6fe3 | 6160 | break; |
e8c2bc1f | 6161 | } |
0e1b6fe3 | 6162 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6163 | case IORING_OP_SENDMSG: { |
6164 | struct io_async_msghdr *io = req->async_data; | |
6165 | if (io->iov != io->fast_iov) | |
6166 | kfree(io->iov); | |
0e1b6fe3 | 6167 | break; |
e8c2bc1f | 6168 | } |
0e1b6fe3 PB |
6169 | case IORING_OP_SPLICE: |
6170 | case IORING_OP_TEE: | |
6171 | io_put_file(req, req->splice.file_in, | |
6172 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6173 | break; | |
f3cd4850 JA |
6174 | case IORING_OP_OPENAT: |
6175 | case IORING_OP_OPENAT2: | |
6176 | if (req->open.filename) | |
6177 | putname(req->open.filename); | |
6178 | break; | |
80a261fd JA |
6179 | case IORING_OP_RENAMEAT: |
6180 | putname(req->rename.oldpath); | |
6181 | putname(req->rename.newpath); | |
6182 | break; | |
14a1143b JA |
6183 | case IORING_OP_UNLINKAT: |
6184 | putname(req->unlink.filename); | |
6185 | break; | |
0e1b6fe3 PB |
6186 | } |
6187 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6188 | } |
bb175342 | 6189 | |
f573d384 JA |
6190 | if (req->flags & REQ_F_INFLIGHT) |
6191 | io_req_drop_files(req); | |
99bc4c38 PB |
6192 | } |
6193 | ||
c1379e24 PB |
6194 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
6195 | struct io_comp_state *cs) | |
2b188cc1 | 6196 | { |
a197f664 | 6197 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6198 | int ret; |
2b188cc1 | 6199 | |
d625c6ee | 6200 | switch (req->opcode) { |
2b188cc1 | 6201 | case IORING_OP_NOP: |
229a7b63 | 6202 | ret = io_nop(req, cs); |
2b188cc1 JA |
6203 | break; |
6204 | case IORING_OP_READV: | |
edafccee | 6205 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6206 | case IORING_OP_READ: |
a1d7c393 | 6207 | ret = io_read(req, force_nonblock, cs); |
edafccee | 6208 | break; |
3529d8c2 | 6209 | case IORING_OP_WRITEV: |
edafccee | 6210 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6211 | case IORING_OP_WRITE: |
a1d7c393 | 6212 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 6213 | break; |
c992fe29 | 6214 | case IORING_OP_FSYNC: |
014db007 | 6215 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 6216 | break; |
221c5eb2 | 6217 | case IORING_OP_POLL_ADD: |
014db007 | 6218 | ret = io_poll_add(req); |
221c5eb2 JA |
6219 | break; |
6220 | case IORING_OP_POLL_REMOVE: | |
fc4df999 | 6221 | ret = io_poll_remove(req); |
221c5eb2 | 6222 | break; |
5d17b4a4 | 6223 | case IORING_OP_SYNC_FILE_RANGE: |
014db007 | 6224 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 6225 | break; |
0fa03c62 | 6226 | case IORING_OP_SENDMSG: |
062d04d7 PB |
6227 | ret = io_sendmsg(req, force_nonblock, cs); |
6228 | break; | |
fddaface | 6229 | case IORING_OP_SEND: |
062d04d7 | 6230 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 6231 | break; |
aa1fa28f | 6232 | case IORING_OP_RECVMSG: |
062d04d7 PB |
6233 | ret = io_recvmsg(req, force_nonblock, cs); |
6234 | break; | |
fddaface | 6235 | case IORING_OP_RECV: |
062d04d7 | 6236 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 6237 | break; |
5262f567 | 6238 | case IORING_OP_TIMEOUT: |
fc4df999 | 6239 | ret = io_timeout(req); |
5262f567 | 6240 | break; |
11365043 | 6241 | case IORING_OP_TIMEOUT_REMOVE: |
fc4df999 | 6242 | ret = io_timeout_remove(req); |
11365043 | 6243 | break; |
17f2fe35 | 6244 | case IORING_OP_ACCEPT: |
229a7b63 | 6245 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 6246 | break; |
f8e85cf2 | 6247 | case IORING_OP_CONNECT: |
229a7b63 | 6248 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 6249 | break; |
62755e35 | 6250 | case IORING_OP_ASYNC_CANCEL: |
014db007 | 6251 | ret = io_async_cancel(req); |
62755e35 | 6252 | break; |
d63d1b5e | 6253 | case IORING_OP_FALLOCATE: |
014db007 | 6254 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 6255 | break; |
15b71abe | 6256 | case IORING_OP_OPENAT: |
014db007 | 6257 | ret = io_openat(req, force_nonblock); |
15b71abe | 6258 | break; |
b5dba59e | 6259 | case IORING_OP_CLOSE: |
229a7b63 | 6260 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 6261 | break; |
05f3fb3c | 6262 | case IORING_OP_FILES_UPDATE: |
229a7b63 | 6263 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 6264 | break; |
eddc7ef5 | 6265 | case IORING_OP_STATX: |
014db007 | 6266 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 6267 | break; |
4840e418 | 6268 | case IORING_OP_FADVISE: |
014db007 | 6269 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 6270 | break; |
c1ca757b | 6271 | case IORING_OP_MADVISE: |
014db007 | 6272 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 6273 | break; |
cebdb986 | 6274 | case IORING_OP_OPENAT2: |
014db007 | 6275 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 6276 | break; |
3e4827b0 | 6277 | case IORING_OP_EPOLL_CTL: |
229a7b63 | 6278 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 6279 | break; |
7d67af2c | 6280 | case IORING_OP_SPLICE: |
014db007 | 6281 | ret = io_splice(req, force_nonblock); |
7d67af2c | 6282 | break; |
ddf0322d | 6283 | case IORING_OP_PROVIDE_BUFFERS: |
229a7b63 | 6284 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 6285 | break; |
067524e9 | 6286 | case IORING_OP_REMOVE_BUFFERS: |
229a7b63 | 6287 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 6288 | break; |
f2a8d5c7 | 6289 | case IORING_OP_TEE: |
f2a8d5c7 PB |
6290 | ret = io_tee(req, force_nonblock); |
6291 | break; | |
36f4fa68 JA |
6292 | case IORING_OP_SHUTDOWN: |
6293 | ret = io_shutdown(req, force_nonblock); | |
6294 | break; | |
80a261fd JA |
6295 | case IORING_OP_RENAMEAT: |
6296 | ret = io_renameat(req, force_nonblock); | |
6297 | break; | |
14a1143b JA |
6298 | case IORING_OP_UNLINKAT: |
6299 | ret = io_unlinkat(req, force_nonblock); | |
6300 | break; | |
2b188cc1 JA |
6301 | default: |
6302 | ret = -EINVAL; | |
6303 | break; | |
6304 | } | |
6305 | ||
def596e9 JA |
6306 | if (ret) |
6307 | return ret; | |
6308 | ||
b532576e JA |
6309 | /* If the op doesn't have a file, we're not polling for it */ |
6310 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6311 | const bool in_async = io_wq_current_is_worker(); |
6312 | ||
11ba820b JA |
6313 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6314 | if (in_async) | |
6315 | mutex_lock(&ctx->uring_lock); | |
6316 | ||
2e9dbe90 | 6317 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6318 | |
6319 | if (in_async) | |
6320 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6321 | } |
6322 | ||
6323 | return 0; | |
2b188cc1 JA |
6324 | } |
6325 | ||
f4db7182 | 6326 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6327 | { |
6328 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6329 | struct io_kiocb *timeout; |
561fb04a | 6330 | int ret = 0; |
2b188cc1 | 6331 | |
6df1db6b PB |
6332 | timeout = io_prep_linked_timeout(req); |
6333 | if (timeout) | |
6334 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6335 | |
0c9d5ccd JA |
6336 | /* if NO_CANCEL is set, we must still run the work */ |
6337 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
6338 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 6339 | ret = -ECANCELED; |
0c9d5ccd | 6340 | } |
31b51510 | 6341 | |
561fb04a | 6342 | if (!ret) { |
561fb04a | 6343 | do { |
c1379e24 | 6344 | ret = io_issue_sqe(req, false, NULL); |
561fb04a JA |
6345 | /* |
6346 | * We can get EAGAIN for polled IO even though we're | |
6347 | * forcing a sync submission from here, since we can't | |
6348 | * wait for request slots on the block side. | |
6349 | */ | |
6350 | if (ret != -EAGAIN) | |
6351 | break; | |
6352 | cond_resched(); | |
6353 | } while (1); | |
6354 | } | |
31b51510 | 6355 | |
561fb04a | 6356 | if (ret) { |
dad1b124 XW |
6357 | /* |
6358 | * io_iopoll_complete() does not hold completion_lock to complete | |
6359 | * polled io, so here for polled io, just mark it done and still let | |
6360 | * io_iopoll_complete() complete it. | |
6361 | */ | |
6362 | if (req->ctx->flags & IORING_SETUP_IOPOLL) { | |
6363 | struct kiocb *kiocb = &req->rw.kiocb; | |
6364 | ||
6365 | kiocb_done(kiocb, ret, NULL); | |
6366 | } else { | |
6367 | req_set_fail_links(req); | |
6368 | io_req_complete(req, ret); | |
6369 | } | |
edafccee | 6370 | } |
2b188cc1 | 6371 | |
f4db7182 | 6372 | return io_steal_work(req); |
2b188cc1 JA |
6373 | } |
6374 | ||
65e19f54 JA |
6375 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6376 | int index) | |
6377 | { | |
6378 | struct fixed_file_table *table; | |
6379 | ||
05f3fb3c | 6380 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6381 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6382 | } |
6383 | ||
8371adf5 PB |
6384 | static struct file *io_file_get(struct io_submit_state *state, |
6385 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6386 | { |
a197f664 | 6387 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6388 | struct file *file; |
09bb8394 | 6389 | |
8da11c19 | 6390 | if (fixed) { |
479f517b | 6391 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6392 | return NULL; |
b7620121 | 6393 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6394 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6395 | io_set_resource_node(req); |
09bb8394 | 6396 | } else { |
c826bd7a | 6397 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6398 | file = __io_file_get(state, fd); |
09bb8394 JA |
6399 | } |
6400 | ||
8371adf5 | 6401 | return file; |
09bb8394 JA |
6402 | } |
6403 | ||
2665abfd | 6404 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6405 | { |
ad8a48ac JA |
6406 | struct io_timeout_data *data = container_of(timer, |
6407 | struct io_timeout_data, timer); | |
90cd7e42 | 6408 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6409 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6410 | unsigned long flags; |
2665abfd JA |
6411 | |
6412 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6413 | prev = req->timeout.head; |
6414 | req->timeout.head = NULL; | |
2665abfd JA |
6415 | |
6416 | /* | |
6417 | * We don't expect the list to be empty, that will only happen if we | |
6418 | * race with the completion of the linked work. | |
6419 | */ | |
90cd7e42 | 6420 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6421 | io_remove_next_linked(prev); |
90cd7e42 PB |
6422 | else |
6423 | prev = NULL; | |
2665abfd JA |
6424 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6425 | ||
6426 | if (prev) { | |
4e88d6e7 | 6427 | req_set_fail_links(prev); |
014db007 | 6428 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
76a46e06 | 6429 | io_put_req(prev); |
47f46768 | 6430 | } else { |
e1e16097 | 6431 | io_req_complete(req, -ETIME); |
2665abfd | 6432 | } |
2665abfd JA |
6433 | return HRTIMER_NORESTART; |
6434 | } | |
6435 | ||
7271ef3a | 6436 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6437 | { |
76a46e06 | 6438 | /* |
f2f87370 PB |
6439 | * If the back reference is NULL, then our linked request finished |
6440 | * before we got a chance to setup the timer | |
76a46e06 | 6441 | */ |
90cd7e42 | 6442 | if (req->timeout.head) { |
e8c2bc1f | 6443 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6444 | |
ad8a48ac JA |
6445 | data->timer.function = io_link_timeout_fn; |
6446 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6447 | data->mode); | |
2665abfd | 6448 | } |
7271ef3a JA |
6449 | } |
6450 | ||
6451 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6452 | { | |
6453 | struct io_ring_ctx *ctx = req->ctx; | |
6454 | ||
6455 | spin_lock_irq(&ctx->completion_lock); | |
6456 | __io_queue_linked_timeout(req); | |
76a46e06 | 6457 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6458 | |
2665abfd | 6459 | /* drop submission reference */ |
76a46e06 JA |
6460 | io_put_req(req); |
6461 | } | |
2665abfd | 6462 | |
ad8a48ac | 6463 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6464 | { |
f2f87370 | 6465 | struct io_kiocb *nxt = req->link; |
2665abfd | 6466 | |
f2f87370 PB |
6467 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6468 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6469 | return NULL; |
2665abfd | 6470 | |
90cd7e42 | 6471 | nxt->timeout.head = req; |
900fad45 | 6472 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6473 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6474 | return nxt; |
2665abfd JA |
6475 | } |
6476 | ||
c1379e24 | 6477 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 | 6478 | { |
4a0a7a18 | 6479 | struct io_kiocb *linked_timeout; |
193155c8 | 6480 | const struct cred *old_creds = NULL; |
e0c5c576 | 6481 | int ret; |
2b188cc1 | 6482 | |
4a0a7a18 JA |
6483 | again: |
6484 | linked_timeout = io_prep_linked_timeout(req); | |
6485 | ||
2e5aa6cb PB |
6486 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6487 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
98447d65 | 6488 | req->work.identity->creds != current_cred()) { |
193155c8 JA |
6489 | if (old_creds) |
6490 | revert_creds(old_creds); | |
98447d65 | 6491 | if (old_creds == req->work.identity->creds) |
193155c8 JA |
6492 | old_creds = NULL; /* restored original creds */ |
6493 | else | |
98447d65 | 6494 | old_creds = override_creds(req->work.identity->creds); |
193155c8 JA |
6495 | } |
6496 | ||
c1379e24 | 6497 | ret = io_issue_sqe(req, true, cs); |
491381ce JA |
6498 | |
6499 | /* | |
6500 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6501 | * doesn't support non-blocking read/write attempts | |
6502 | */ | |
24c74678 | 6503 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6504 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6505 | /* |
6506 | * Queued up for async execution, worker will release | |
6507 | * submit reference when the iocb is actually submitted. | |
6508 | */ | |
6509 | io_queue_async_work(req); | |
2b188cc1 | 6510 | } |
bbad27b2 | 6511 | |
f063c547 PB |
6512 | if (linked_timeout) |
6513 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 PB |
6514 | } else if (likely(!ret)) { |
6515 | /* drop submission reference */ | |
6516 | req = io_put_req_find_next(req); | |
6517 | if (linked_timeout) | |
6518 | io_queue_linked_timeout(linked_timeout); | |
e65ef56d | 6519 | |
0d63c148 PB |
6520 | if (req) { |
6521 | if (!(req->flags & REQ_F_FORCE_ASYNC)) | |
6522 | goto again; | |
6523 | io_queue_async_work(req); | |
6524 | } | |
6525 | } else { | |
652532ad PB |
6526 | /* un-prep timeout, so it'll be killed as any other linked */ |
6527 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6528 | req_set_fail_links(req); |
e65ef56d | 6529 | io_put_req(req); |
e1e16097 | 6530 | io_req_complete(req, ret); |
9e645e11 | 6531 | } |
652532ad | 6532 | |
193155c8 JA |
6533 | if (old_creds) |
6534 | revert_creds(old_creds); | |
2b188cc1 JA |
6535 | } |
6536 | ||
f13fad7b JA |
6537 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6538 | struct io_comp_state *cs) | |
4fe2c963 JL |
6539 | { |
6540 | int ret; | |
6541 | ||
3529d8c2 | 6542 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6543 | if (ret) { |
6544 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6545 | fail_req: |
4e88d6e7 | 6546 | req_set_fail_links(req); |
e1e16097 JA |
6547 | io_put_req(req); |
6548 | io_req_complete(req, ret); | |
4fe2c963 | 6549 | } |
2550878f | 6550 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6551 | if (!req->async_data) { |
bd2ab18a | 6552 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6553 | if (unlikely(ret)) |
bd2ab18a PB |
6554 | goto fail_req; |
6555 | } | |
ce35a47a JA |
6556 | io_queue_async_work(req); |
6557 | } else { | |
c1379e24 PB |
6558 | if (sqe) { |
6559 | ret = io_req_prep(req, sqe); | |
6560 | if (unlikely(ret)) | |
6561 | goto fail_req; | |
6562 | } | |
6563 | __io_queue_sqe(req, cs); | |
ce35a47a | 6564 | } |
4fe2c963 JL |
6565 | } |
6566 | ||
f13fad7b JA |
6567 | static inline void io_queue_link_head(struct io_kiocb *req, |
6568 | struct io_comp_state *cs) | |
4fe2c963 | 6569 | { |
94ae5e77 | 6570 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6571 | io_put_req(req); |
6572 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6573 | } else |
f13fad7b | 6574 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6575 | } |
6576 | ||
863e0560 PB |
6577 | struct io_submit_link { |
6578 | struct io_kiocb *head; | |
6579 | struct io_kiocb *last; | |
6580 | }; | |
6581 | ||
1d4240cc | 6582 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
863e0560 | 6583 | struct io_submit_link *link, struct io_comp_state *cs) |
9e645e11 | 6584 | { |
a197f664 | 6585 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6586 | int ret; |
9e645e11 | 6587 | |
9e645e11 JA |
6588 | /* |
6589 | * If we already have a head request, queue this one for async | |
6590 | * submittal once the head completes. If we don't have a head but | |
6591 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6592 | * submitted sync once the chain is complete. If none of those | |
6593 | * conditions are true (normal request), then just queue it. | |
6594 | */ | |
863e0560 PB |
6595 | if (link->head) { |
6596 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6597 | |
8cdf2193 PB |
6598 | /* |
6599 | * Taking sequential execution of a link, draining both sides | |
6600 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6601 | * requests in the link. So, it drains the head and the | |
6602 | * next after the link request. The last one is done via | |
6603 | * drain_next flag to persist the effect across calls. | |
6604 | */ | |
ef4ff581 | 6605 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6606 | head->flags |= REQ_F_IO_DRAIN; |
6607 | ctx->drain_next = 1; | |
6608 | } | |
3529d8c2 | 6609 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6610 | if (unlikely(ret)) { |
4e88d6e7 | 6611 | /* fail even hard links since we don't submit */ |
9d76377f | 6612 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6613 | return ret; |
2d28390a | 6614 | } |
9d76377f | 6615 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6616 | link->last->link = req; |
863e0560 | 6617 | link->last = req; |
32fe525b PB |
6618 | |
6619 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6620 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6621 | io_queue_link_head(head, cs); |
863e0560 | 6622 | link->head = NULL; |
32fe525b | 6623 | } |
9e645e11 | 6624 | } else { |
711be031 PB |
6625 | if (unlikely(ctx->drain_next)) { |
6626 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6627 | ctx->drain_next = 0; |
711be031 | 6628 | } |
ef4ff581 | 6629 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
711be031 | 6630 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6631 | if (unlikely(ret)) |
711be031 | 6632 | req->flags |= REQ_F_FAIL_LINK; |
863e0560 PB |
6633 | link->head = req; |
6634 | link->last = req; | |
711be031 | 6635 | } else { |
f13fad7b | 6636 | io_queue_sqe(req, sqe, cs); |
711be031 | 6637 | } |
9e645e11 | 6638 | } |
2e6e1fde | 6639 | |
1d4240cc | 6640 | return 0; |
9e645e11 JA |
6641 | } |
6642 | ||
9a56a232 JA |
6643 | /* |
6644 | * Batched submission is done, ensure local IO is flushed out. | |
6645 | */ | |
6646 | static void io_submit_state_end(struct io_submit_state *state) | |
6647 | { | |
f13fad7b JA |
6648 | if (!list_empty(&state->comp.list)) |
6649 | io_submit_flush_completions(&state->comp); | |
27926b68 JA |
6650 | if (state->plug_started) |
6651 | blk_finish_plug(&state->plug); | |
9f13c35b | 6652 | io_state_file_put(state); |
2579f913 | 6653 | if (state->free_reqs) |
6c8a3134 | 6654 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6655 | } |
6656 | ||
6657 | /* | |
6658 | * Start submission side cache. | |
6659 | */ | |
6660 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6661 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 | 6662 | { |
27926b68 | 6663 | state->plug_started = false; |
013538bd JA |
6664 | state->comp.nr = 0; |
6665 | INIT_LIST_HEAD(&state->comp.list); | |
6666 | state->comp.ctx = ctx; | |
2579f913 | 6667 | state->free_reqs = 0; |
6e1271e6 | 6668 | state->file_refs = 0; |
9a56a232 JA |
6669 | state->ios_left = max_ios; |
6670 | } | |
6671 | ||
2b188cc1 JA |
6672 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6673 | { | |
75b28aff | 6674 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6675 | |
caf582c6 PB |
6676 | /* |
6677 | * Ensure any loads from the SQEs are done at this point, | |
6678 | * since once we write the new head, the application could | |
6679 | * write new data to them. | |
6680 | */ | |
6681 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6682 | } |
6683 | ||
2b188cc1 | 6684 | /* |
3529d8c2 | 6685 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6686 | * that is mapped by userspace. This means that care needs to be taken to |
6687 | * ensure that reads are stable, as we cannot rely on userspace always | |
6688 | * being a good citizen. If members of the sqe are validated and then later | |
6689 | * used, it's important that those reads are done through READ_ONCE() to | |
6690 | * prevent a re-load down the line. | |
6691 | */ | |
709b302f | 6692 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6693 | { |
75b28aff | 6694 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6695 | unsigned head; |
6696 | ||
6697 | /* | |
6698 | * The cached sq head (or cq tail) serves two purposes: | |
6699 | * | |
6700 | * 1) allows us to batch the cost of updating the user visible | |
6701 | * head updates. | |
6702 | * 2) allows the kernel side to track the head on its own, even | |
6703 | * though the application is the one updating it. | |
6704 | */ | |
ee7d46d9 | 6705 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6706 | if (likely(head < ctx->sq_entries)) |
6707 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6708 | |
6709 | /* drop invalid entries */ | |
498ccd9e | 6710 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6711 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6712 | return NULL; |
6713 | } | |
6714 | ||
6715 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6716 | { | |
6717 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6718 | } |
6719 | ||
21b55dbc SG |
6720 | /* |
6721 | * Check SQE restrictions (opcode and flags). | |
6722 | * | |
6723 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6724 | */ | |
6725 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6726 | struct io_kiocb *req, | |
6727 | unsigned int sqe_flags) | |
6728 | { | |
6729 | if (!ctx->restricted) | |
6730 | return true; | |
6731 | ||
6732 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6733 | return false; | |
6734 | ||
6735 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6736 | ctx->restrictions.sqe_flags_required) | |
6737 | return false; | |
6738 | ||
6739 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6740 | ctx->restrictions.sqe_flags_required)) | |
6741 | return false; | |
6742 | ||
6743 | return true; | |
6744 | } | |
6745 | ||
ef4ff581 PB |
6746 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6747 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6748 | IOSQE_BUFFER_SELECT) | |
6749 | ||
6750 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6751 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6752 | struct io_submit_state *state) |
0553b8bd | 6753 | { |
ef4ff581 | 6754 | unsigned int sqe_flags; |
71b547c0 | 6755 | int id, ret; |
ef4ff581 | 6756 | |
0553b8bd PB |
6757 | req->opcode = READ_ONCE(sqe->opcode); |
6758 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6759 | req->async_data = NULL; |
0553b8bd PB |
6760 | req->file = NULL; |
6761 | req->ctx = ctx; | |
6762 | req->flags = 0; | |
f2f87370 | 6763 | req->link = NULL; |
36f72fe2 | 6764 | req->fixed_file_refs = NULL; |
0553b8bd PB |
6765 | /* one is dropped after submission, the other at completion */ |
6766 | refcount_set(&req->refs, 2); | |
4dd2824d | 6767 | req->task = current; |
0553b8bd | 6768 | req->result = 0; |
ef4ff581 PB |
6769 | |
6770 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6771 | return -EINVAL; | |
6772 | ||
28cea78a | 6773 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
9d8426a0 | 6774 | return -EFAULT; |
ef4ff581 PB |
6775 | |
6776 | sqe_flags = READ_ONCE(sqe->flags); | |
6777 | /* enforce forwards compatibility on users */ | |
6778 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6779 | return -EINVAL; | |
6780 | ||
21b55dbc SG |
6781 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6782 | return -EACCES; | |
6783 | ||
ef4ff581 PB |
6784 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6785 | !io_op_defs[req->opcode].buffer_select) | |
6786 | return -EOPNOTSUPP; | |
6787 | ||
6788 | id = READ_ONCE(sqe->personality); | |
6789 | if (id) { | |
1e6fa521 JA |
6790 | struct io_identity *iod; |
6791 | ||
1e6fa521 JA |
6792 | iod = idr_find(&ctx->personality_idr, id); |
6793 | if (unlikely(!iod)) | |
ef4ff581 | 6794 | return -EINVAL; |
1e6fa521 | 6795 | refcount_inc(&iod->count); |
ec99ca6c PB |
6796 | |
6797 | __io_req_init_async(req); | |
1e6fa521 JA |
6798 | get_cred(iod->creds); |
6799 | req->work.identity = iod; | |
dfead8a8 | 6800 | req->work.flags |= IO_WQ_WORK_CREDS; |
ef4ff581 PB |
6801 | } |
6802 | ||
6803 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6804 | req->flags |= sqe_flags; |
ef4ff581 | 6805 | |
27926b68 JA |
6806 | /* |
6807 | * Plug now if we have more than 1 IO left after this, and the target | |
6808 | * is potentially a read/write to block based storage. | |
6809 | */ | |
6810 | if (!state->plug_started && state->ios_left > 1 && | |
6811 | io_op_defs[req->opcode].plug) { | |
6812 | blk_start_plug(&state->plug); | |
6813 | state->plug_started = true; | |
6814 | } | |
6815 | ||
bd5bbda7 PB |
6816 | ret = 0; |
6817 | if (io_op_defs[req->opcode].needs_file) { | |
6818 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6819 | ||
6820 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6821 | if (unlikely(!req->file && | |
6822 | !io_op_defs[req->opcode].needs_file_no_error)) | |
6823 | ret = -EBADF; | |
6824 | } | |
63ff8223 | 6825 | |
71b547c0 PB |
6826 | state->ios_left--; |
6827 | return ret; | |
0553b8bd PB |
6828 | } |
6829 | ||
0f212204 | 6830 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6831 | { |
ac8691c4 | 6832 | struct io_submit_state state; |
863e0560 | 6833 | struct io_submit_link link; |
9e645e11 | 6834 | int i, submitted = 0; |
6c271ce2 | 6835 | |
c4a2ed72 | 6836 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 JA |
6837 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6838 | if (!list_empty(&ctx->cq_overflow_list) && | |
e6c8aa9a | 6839 | !io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6840 | return -EBUSY; |
6841 | } | |
6c271ce2 | 6842 | |
ee7d46d9 PB |
6843 | /* make sure SQ entry isn't read before tail */ |
6844 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6845 | |
2b85edfc PB |
6846 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6847 | return -EAGAIN; | |
6c271ce2 | 6848 | |
d8a6df10 | 6849 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6850 | refcount_add(nr, ¤t->usage); |
6c271ce2 | 6851 | |
013538bd | 6852 | io_submit_state_start(&state, ctx, nr); |
863e0560 | 6853 | link.head = NULL; |
b14cca0c | 6854 | |
6c271ce2 | 6855 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6856 | const struct io_uring_sqe *sqe; |
196be95c | 6857 | struct io_kiocb *req; |
1cb1edb2 | 6858 | int err; |
fb5ccc98 | 6859 | |
b1e50e54 PB |
6860 | sqe = io_get_sqe(ctx); |
6861 | if (unlikely(!sqe)) { | |
6862 | io_consume_sqe(ctx); | |
6863 | break; | |
6864 | } | |
ac8691c4 | 6865 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6866 | if (unlikely(!req)) { |
6867 | if (!submitted) | |
6868 | submitted = -EAGAIN; | |
fb5ccc98 | 6869 | break; |
196be95c | 6870 | } |
709b302f | 6871 | io_consume_sqe(ctx); |
d3656344 JA |
6872 | /* will complete beyond this point, count as submitted */ |
6873 | submitted++; | |
6874 | ||
692d8363 | 6875 | err = io_init_req(ctx, req, sqe, &state); |
ef4ff581 | 6876 | if (unlikely(err)) { |
1cb1edb2 | 6877 | fail_req: |
e1e16097 JA |
6878 | io_put_req(req); |
6879 | io_req_complete(req, err); | |
196be95c PB |
6880 | break; |
6881 | } | |
fb5ccc98 | 6882 | |
354420f7 | 6883 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
0cdaf760 | 6884 | true, io_async_submit(ctx)); |
f13fad7b | 6885 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6886 | if (err) |
6887 | goto fail_req; | |
6c271ce2 JA |
6888 | } |
6889 | ||
9466f437 PB |
6890 | if (unlikely(submitted != nr)) { |
6891 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6892 | struct io_uring_task *tctx = current->io_uring; |
6893 | int unused = nr - ref_used; | |
9466f437 | 6894 | |
d8a6df10 JA |
6895 | percpu_ref_put_many(&ctx->refs, unused); |
6896 | percpu_counter_sub(&tctx->inflight, unused); | |
6897 | put_task_struct_many(current, unused); | |
9466f437 | 6898 | } |
863e0560 PB |
6899 | if (link.head) |
6900 | io_queue_link_head(link.head, &state.comp); | |
ac8691c4 | 6901 | io_submit_state_end(&state); |
6c271ce2 | 6902 | |
ae9428ca PB |
6903 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6904 | io_commit_sqring(ctx); | |
6905 | ||
6c271ce2 JA |
6906 | return submitted; |
6907 | } | |
6908 | ||
23b3628e XW |
6909 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6910 | { | |
6911 | /* Tell userspace we may need a wakeup call */ | |
6912 | spin_lock_irq(&ctx->completion_lock); | |
6913 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6914 | spin_unlock_irq(&ctx->completion_lock); | |
6915 | } | |
6916 | ||
6917 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6918 | { | |
6919 | spin_lock_irq(&ctx->completion_lock); | |
6920 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6921 | spin_unlock_irq(&ctx->completion_lock); | |
6922 | } | |
6923 | ||
08369246 | 6924 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6925 | { |
c8d1ba58 | 6926 | unsigned int to_submit; |
bdcd3eab | 6927 | int ret = 0; |
6c271ce2 | 6928 | |
c8d1ba58 | 6929 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6930 | /* if we're handling multiple rings, cap submit size for fairness */ |
6931 | if (cap_entries && to_submit > 8) | |
6932 | to_submit = 8; | |
6933 | ||
906a3c6f XW |
6934 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
6935 | unsigned nr_events = 0; | |
6936 | ||
08369246 | 6937 | mutex_lock(&ctx->uring_lock); |
906a3c6f XW |
6938 | if (!list_empty(&ctx->iopoll_list)) |
6939 | io_do_iopoll(ctx, &nr_events, 0); | |
6940 | ||
6941 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 XW |
6942 | ret = io_submit_sqes(ctx, to_submit); |
6943 | mutex_unlock(&ctx->uring_lock); | |
6944 | } | |
90554200 JA |
6945 | |
6946 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) | |
6947 | wake_up(&ctx->sqo_sq_wait); | |
6948 | ||
08369246 XW |
6949 | return ret; |
6950 | } | |
6951 | ||
6952 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) | |
6953 | { | |
6954 | struct io_ring_ctx *ctx; | |
6955 | unsigned sq_thread_idle = 0; | |
6956 | ||
6957 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6958 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6959 | sq_thread_idle = ctx->sq_thread_idle; | |
6960 | } | |
6961 | ||
6962 | sqd->sq_thread_idle = sq_thread_idle; | |
c8d1ba58 | 6963 | } |
6c271ce2 | 6964 | |
69fb2131 JA |
6965 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6966 | { | |
6967 | struct io_ring_ctx *ctx; | |
6968 | ||
6969 | while (!list_empty(&sqd->ctx_new_list)) { | |
6970 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6971 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6972 | complete(&ctx->sq_thread_comp); | |
6973 | } | |
08369246 XW |
6974 | |
6975 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6976 | } |
6977 | ||
c8d1ba58 JA |
6978 | static int io_sq_thread(void *data) |
6979 | { | |
91d8f519 | 6980 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
6981 | struct files_struct *old_files = current->files; |
6982 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
6983 | const struct cred *old_cred = NULL; |
6984 | struct io_sq_data *sqd = data; | |
6985 | struct io_ring_ctx *ctx; | |
a0d9205f | 6986 | unsigned long timeout = 0; |
08369246 | 6987 | DEFINE_WAIT(wait); |
6c271ce2 | 6988 | |
28cea78a JA |
6989 | task_lock(current); |
6990 | current->files = NULL; | |
6991 | current->nsproxy = NULL; | |
6992 | task_unlock(current); | |
6993 | ||
69fb2131 | 6994 | while (!kthread_should_stop()) { |
08369246 XW |
6995 | int ret; |
6996 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
6997 | |
6998 | /* | |
69fb2131 JA |
6999 | * Any changes to the sqd lists are synchronized through the |
7000 | * kthread parking. This synchronizes the thread vs users, | |
7001 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7002 | */ |
65b2b213 | 7003 | if (kthread_should_park()) { |
69fb2131 | 7004 | kthread_parkme(); |
65b2b213 XW |
7005 | /* |
7006 | * When sq thread is unparked, in case the previous park operation | |
7007 | * comes from io_put_sq_data(), which means that sq thread is going | |
7008 | * to be stopped, so here needs to have a check. | |
7009 | */ | |
7010 | if (kthread_should_stop()) | |
7011 | break; | |
7012 | } | |
7143b5ac | 7013 | |
08369246 | 7014 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7015 | io_sqd_init_new(sqd); |
08369246 XW |
7016 | timeout = jiffies + sqd->sq_thread_idle; |
7017 | } | |
6c271ce2 | 7018 | |
08369246 | 7019 | sqt_spin = false; |
e95eee2d | 7020 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7021 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7022 | if (current->cred != ctx->creds) { | |
7023 | if (old_cred) | |
7024 | revert_creds(old_cred); | |
7025 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7026 | } |
91d8f519 | 7027 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7028 | #ifdef CONFIG_AUDIT |
7029 | current->loginuid = ctx->loginuid; | |
7030 | current->sessionid = ctx->sessionid; | |
7031 | #endif | |
bdcd3eab | 7032 | |
08369246 XW |
7033 | ret = __io_sq_thread(ctx, cap_entries); |
7034 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7035 | sqt_spin = true; | |
6c271ce2 | 7036 | |
28cea78a | 7037 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7038 | } |
6c271ce2 | 7039 | |
08369246 | 7040 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
7041 | io_run_task_work(); |
7042 | cond_resched(); | |
08369246 XW |
7043 | if (sqt_spin) |
7044 | timeout = jiffies + sqd->sq_thread_idle; | |
7045 | continue; | |
7046 | } | |
7047 | ||
7048 | if (kthread_should_park()) | |
7049 | continue; | |
7050 | ||
7051 | needs_sched = true; | |
7052 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7053 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7054 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7055 | !list_empty_careful(&ctx->iopoll_list)) { | |
7056 | needs_sched = false; | |
7057 | break; | |
7058 | } | |
7059 | if (io_sqring_entries(ctx)) { | |
7060 | needs_sched = false; | |
7061 | break; | |
7062 | } | |
7063 | } | |
7064 | ||
7065 | if (needs_sched) { | |
69fb2131 JA |
7066 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7067 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7068 | |
69fb2131 | 7069 | schedule(); |
69fb2131 JA |
7070 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7071 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7072 | } |
08369246 XW |
7073 | |
7074 | finish_wait(&sqd->wait, &wait); | |
7075 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7076 | } |
7077 | ||
4c6e277c | 7078 | io_run_task_work(); |
b41e9852 | 7079 | |
91d8f519 DZ |
7080 | if (cur_css) |
7081 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7082 | if (old_cred) |
7083 | revert_creds(old_cred); | |
06058632 | 7084 | |
28cea78a JA |
7085 | task_lock(current); |
7086 | current->files = old_files; | |
7087 | current->nsproxy = old_nsproxy; | |
7088 | task_unlock(current); | |
7089 | ||
2bbcd6d3 | 7090 | kthread_parkme(); |
06058632 | 7091 | |
6c271ce2 JA |
7092 | return 0; |
7093 | } | |
7094 | ||
bda52162 JA |
7095 | struct io_wait_queue { |
7096 | struct wait_queue_entry wq; | |
7097 | struct io_ring_ctx *ctx; | |
7098 | unsigned to_wait; | |
7099 | unsigned nr_timeouts; | |
7100 | }; | |
7101 | ||
1d7bb1d5 | 7102 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
7103 | { |
7104 | struct io_ring_ctx *ctx = iowq->ctx; | |
7105 | ||
7106 | /* | |
d195a66e | 7107 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7108 | * started waiting. For timeouts, we always want to return to userspace, |
7109 | * regardless of event count. | |
7110 | */ | |
1d7bb1d5 | 7111 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
7112 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7113 | } | |
7114 | ||
7115 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7116 | int wake_flags, void *key) | |
7117 | { | |
7118 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7119 | wq); | |
7120 | ||
1d7bb1d5 JA |
7121 | /* use noflush == true, as we can't safely rely on locking context */ |
7122 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
7123 | return -1; |
7124 | ||
7125 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7126 | } | |
7127 | ||
af9c1a44 JA |
7128 | static int io_run_task_work_sig(void) |
7129 | { | |
7130 | if (io_run_task_work()) | |
7131 | return 1; | |
7132 | if (!signal_pending(current)) | |
7133 | return 0; | |
7134 | if (current->jobctl & JOBCTL_TASK_WORK) { | |
7135 | spin_lock_irq(¤t->sighand->siglock); | |
7136 | current->jobctl &= ~JOBCTL_TASK_WORK; | |
7137 | recalc_sigpending(); | |
7138 | spin_unlock_irq(¤t->sighand->siglock); | |
7139 | return 1; | |
7140 | } | |
7141 | return -EINTR; | |
7142 | } | |
7143 | ||
2b188cc1 JA |
7144 | /* |
7145 | * Wait until events become available, if we don't already have some. The | |
7146 | * application must reap them itself, as they reside on the shared cq ring. | |
7147 | */ | |
7148 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7149 | const sigset_t __user *sig, size_t sigsz, |
7150 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7151 | { |
bda52162 JA |
7152 | struct io_wait_queue iowq = { |
7153 | .wq = { | |
7154 | .private = current, | |
7155 | .func = io_wake_function, | |
7156 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7157 | }, | |
7158 | .ctx = ctx, | |
7159 | .to_wait = min_events, | |
7160 | }; | |
75b28aff | 7161 | struct io_rings *rings = ctx->rings; |
c73ebb68 HX |
7162 | struct timespec64 ts; |
7163 | signed long timeout = 0; | |
e9ffa5c2 | 7164 | int ret = 0; |
2b188cc1 | 7165 | |
b41e9852 JA |
7166 | do { |
7167 | if (io_cqring_events(ctx, false) >= min_events) | |
7168 | return 0; | |
4c6e277c | 7169 | if (!io_run_task_work()) |
b41e9852 | 7170 | break; |
b41e9852 | 7171 | } while (1); |
2b188cc1 JA |
7172 | |
7173 | if (sig) { | |
9e75ad5d AB |
7174 | #ifdef CONFIG_COMPAT |
7175 | if (in_compat_syscall()) | |
7176 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7177 | sigsz); |
9e75ad5d AB |
7178 | else |
7179 | #endif | |
b772434b | 7180 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7181 | |
2b188cc1 JA |
7182 | if (ret) |
7183 | return ret; | |
7184 | } | |
7185 | ||
c73ebb68 HX |
7186 | if (uts) { |
7187 | if (get_timespec64(&ts, uts)) | |
7188 | return -EFAULT; | |
7189 | timeout = timespec64_to_jiffies(&ts); | |
7190 | } | |
7191 | ||
bda52162 | 7192 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7193 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
7194 | do { |
7195 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
7196 | TASK_INTERRUPTIBLE); | |
ce593a6c | 7197 | /* make sure we run task_work before checking for signals */ |
af9c1a44 JA |
7198 | ret = io_run_task_work_sig(); |
7199 | if (ret > 0) | |
4c6e277c | 7200 | continue; |
af9c1a44 | 7201 | else if (ret < 0) |
bda52162 | 7202 | break; |
ce593a6c JA |
7203 | if (io_should_wake(&iowq, false)) |
7204 | break; | |
c73ebb68 HX |
7205 | if (uts) { |
7206 | timeout = schedule_timeout(timeout); | |
7207 | if (timeout == 0) { | |
7208 | ret = -ETIME; | |
7209 | break; | |
7210 | } | |
7211 | } else { | |
7212 | schedule(); | |
7213 | } | |
bda52162 JA |
7214 | } while (1); |
7215 | finish_wait(&ctx->wait, &iowq.wq); | |
7216 | ||
b7db41c9 | 7217 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7218 | |
75b28aff | 7219 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7220 | } |
7221 | ||
6b06314c JA |
7222 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7223 | { | |
7224 | #if defined(CONFIG_UNIX) | |
7225 | if (ctx->ring_sock) { | |
7226 | struct sock *sock = ctx->ring_sock->sk; | |
7227 | struct sk_buff *skb; | |
7228 | ||
7229 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7230 | kfree_skb(skb); | |
7231 | } | |
7232 | #else | |
7233 | int i; | |
7234 | ||
65e19f54 JA |
7235 | for (i = 0; i < ctx->nr_user_files; i++) { |
7236 | struct file *file; | |
7237 | ||
7238 | file = io_file_from_index(ctx, i); | |
7239 | if (file) | |
7240 | fput(file); | |
7241 | } | |
6b06314c JA |
7242 | #endif |
7243 | } | |
7244 | ||
05f3fb3c JA |
7245 | static void io_file_ref_kill(struct percpu_ref *ref) |
7246 | { | |
7247 | struct fixed_file_data *data; | |
7248 | ||
7249 | data = container_of(ref, struct fixed_file_data, refs); | |
7250 | complete(&data->done); | |
7251 | } | |
7252 | ||
6b06314c JA |
7253 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7254 | { | |
05f3fb3c | 7255 | struct fixed_file_data *data = ctx->file_data; |
05589553 | 7256 | struct fixed_file_ref_node *ref_node = NULL; |
65e19f54 JA |
7257 | unsigned nr_tables, i; |
7258 | ||
05f3fb3c | 7259 | if (!data) |
6b06314c JA |
7260 | return -ENXIO; |
7261 | ||
ac0648a5 | 7262 | spin_lock_bh(&data->lock); |
1e5d770b | 7263 | ref_node = data->node; |
ac0648a5 | 7264 | spin_unlock_bh(&data->lock); |
05589553 XW |
7265 | if (ref_node) |
7266 | percpu_ref_kill(&ref_node->refs); | |
7267 | ||
7268 | percpu_ref_kill(&data->refs); | |
7269 | ||
7270 | /* wait for all refs nodes to complete */ | |
4a38aed2 | 7271 | flush_delayed_work(&ctx->file_put_work); |
2faf852d | 7272 | wait_for_completion(&data->done); |
05f3fb3c | 7273 | |
6b06314c | 7274 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7275 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7276 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
7277 | kfree(data->table[i].files); |
7278 | kfree(data->table); | |
05589553 XW |
7279 | percpu_ref_exit(&data->refs); |
7280 | kfree(data); | |
05f3fb3c | 7281 | ctx->file_data = NULL; |
6b06314c JA |
7282 | ctx->nr_user_files = 0; |
7283 | return 0; | |
7284 | } | |
7285 | ||
534ca6d6 | 7286 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7287 | { |
534ca6d6 | 7288 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7289 | /* |
7290 | * The park is a bit of a work-around, without it we get | |
7291 | * warning spews on shutdown with SQPOLL set and affinity | |
7292 | * set to a single CPU. | |
7293 | */ | |
534ca6d6 JA |
7294 | if (sqd->thread) { |
7295 | kthread_park(sqd->thread); | |
7296 | kthread_stop(sqd->thread); | |
7297 | } | |
7298 | ||
7299 | kfree(sqd); | |
7300 | } | |
7301 | } | |
7302 | ||
aa06165d JA |
7303 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7304 | { | |
7305 | struct io_ring_ctx *ctx_attach; | |
7306 | struct io_sq_data *sqd; | |
7307 | struct fd f; | |
7308 | ||
7309 | f = fdget(p->wq_fd); | |
7310 | if (!f.file) | |
7311 | return ERR_PTR(-ENXIO); | |
7312 | if (f.file->f_op != &io_uring_fops) { | |
7313 | fdput(f); | |
7314 | return ERR_PTR(-EINVAL); | |
7315 | } | |
7316 | ||
7317 | ctx_attach = f.file->private_data; | |
7318 | sqd = ctx_attach->sq_data; | |
7319 | if (!sqd) { | |
7320 | fdput(f); | |
7321 | return ERR_PTR(-EINVAL); | |
7322 | } | |
7323 | ||
7324 | refcount_inc(&sqd->refs); | |
7325 | fdput(f); | |
7326 | return sqd; | |
7327 | } | |
7328 | ||
534ca6d6 JA |
7329 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7330 | { | |
7331 | struct io_sq_data *sqd; | |
7332 | ||
aa06165d JA |
7333 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7334 | return io_attach_sq_data(p); | |
7335 | ||
534ca6d6 JA |
7336 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7337 | if (!sqd) | |
7338 | return ERR_PTR(-ENOMEM); | |
7339 | ||
7340 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7341 | INIT_LIST_HEAD(&sqd->ctx_list); |
7342 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7343 | mutex_init(&sqd->ctx_lock); | |
7344 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7345 | init_waitqueue_head(&sqd->wait); |
7346 | return sqd; | |
7347 | } | |
7348 | ||
69fb2131 JA |
7349 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7350 | __releases(&sqd->lock) | |
7351 | { | |
7352 | if (!sqd->thread) | |
7353 | return; | |
7354 | kthread_unpark(sqd->thread); | |
7355 | mutex_unlock(&sqd->lock); | |
7356 | } | |
7357 | ||
7358 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7359 | __acquires(&sqd->lock) | |
7360 | { | |
7361 | if (!sqd->thread) | |
7362 | return; | |
7363 | mutex_lock(&sqd->lock); | |
7364 | kthread_park(sqd->thread); | |
7365 | } | |
7366 | ||
534ca6d6 JA |
7367 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7368 | { | |
7369 | struct io_sq_data *sqd = ctx->sq_data; | |
7370 | ||
7371 | if (sqd) { | |
7372 | if (sqd->thread) { | |
7373 | /* | |
7374 | * We may arrive here from the error branch in | |
7375 | * io_sq_offload_create() where the kthread is created | |
7376 | * without being waked up, thus wake it up now to make | |
7377 | * sure the wait will complete. | |
7378 | */ | |
7379 | wake_up_process(sqd->thread); | |
7380 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7381 | |
7382 | io_sq_thread_park(sqd); | |
7383 | } | |
7384 | ||
7385 | mutex_lock(&sqd->ctx_lock); | |
7386 | list_del(&ctx->sqd_list); | |
08369246 | 7387 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7388 | mutex_unlock(&sqd->ctx_lock); |
7389 | ||
08369246 | 7390 | if (sqd->thread) |
69fb2131 | 7391 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7392 | |
7393 | io_put_sq_data(sqd); | |
7394 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7395 | } |
7396 | } | |
7397 | ||
6b06314c JA |
7398 | static void io_finish_async(struct io_ring_ctx *ctx) |
7399 | { | |
6c271ce2 JA |
7400 | io_sq_thread_stop(ctx); |
7401 | ||
561fb04a JA |
7402 | if (ctx->io_wq) { |
7403 | io_wq_destroy(ctx->io_wq); | |
7404 | ctx->io_wq = NULL; | |
6b06314c JA |
7405 | } |
7406 | } | |
7407 | ||
7408 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7409 | /* |
7410 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7411 | * the io_uring can be safely unregistered on process exit, even if we have | |
7412 | * loops in the file referencing. | |
7413 | */ | |
7414 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7415 | { | |
7416 | struct sock *sk = ctx->ring_sock->sk; | |
7417 | struct scm_fp_list *fpl; | |
7418 | struct sk_buff *skb; | |
08a45173 | 7419 | int i, nr_files; |
6b06314c | 7420 | |
6b06314c JA |
7421 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7422 | if (!fpl) | |
7423 | return -ENOMEM; | |
7424 | ||
7425 | skb = alloc_skb(0, GFP_KERNEL); | |
7426 | if (!skb) { | |
7427 | kfree(fpl); | |
7428 | return -ENOMEM; | |
7429 | } | |
7430 | ||
7431 | skb->sk = sk; | |
6b06314c | 7432 | |
08a45173 | 7433 | nr_files = 0; |
6b06314c JA |
7434 | fpl->user = get_uid(ctx->user); |
7435 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7436 | struct file *file = io_file_from_index(ctx, i + offset); |
7437 | ||
7438 | if (!file) | |
08a45173 | 7439 | continue; |
65e19f54 | 7440 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7441 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7442 | nr_files++; | |
6b06314c JA |
7443 | } |
7444 | ||
08a45173 JA |
7445 | if (nr_files) { |
7446 | fpl->max = SCM_MAX_FD; | |
7447 | fpl->count = nr_files; | |
7448 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7449 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7450 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7451 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7452 | |
08a45173 JA |
7453 | for (i = 0; i < nr_files; i++) |
7454 | fput(fpl->fp[i]); | |
7455 | } else { | |
7456 | kfree_skb(skb); | |
7457 | kfree(fpl); | |
7458 | } | |
6b06314c JA |
7459 | |
7460 | return 0; | |
7461 | } | |
7462 | ||
7463 | /* | |
7464 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7465 | * causes regular reference counting to break down. We rely on the UNIX | |
7466 | * garbage collection to take care of this problem for us. | |
7467 | */ | |
7468 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7469 | { | |
7470 | unsigned left, total; | |
7471 | int ret = 0; | |
7472 | ||
7473 | total = 0; | |
7474 | left = ctx->nr_user_files; | |
7475 | while (left) { | |
7476 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7477 | |
7478 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7479 | if (ret) | |
7480 | break; | |
7481 | left -= this_files; | |
7482 | total += this_files; | |
7483 | } | |
7484 | ||
7485 | if (!ret) | |
7486 | return 0; | |
7487 | ||
7488 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7489 | struct file *file = io_file_from_index(ctx, total); |
7490 | ||
7491 | if (file) | |
7492 | fput(file); | |
6b06314c JA |
7493 | total++; |
7494 | } | |
7495 | ||
7496 | return ret; | |
7497 | } | |
7498 | #else | |
7499 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7500 | { | |
7501 | return 0; | |
7502 | } | |
7503 | #endif | |
7504 | ||
5398ae69 PB |
7505 | static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data, |
7506 | unsigned nr_tables, unsigned nr_files) | |
65e19f54 JA |
7507 | { |
7508 | int i; | |
7509 | ||
7510 | for (i = 0; i < nr_tables; i++) { | |
5398ae69 | 7511 | struct fixed_file_table *table = &file_data->table[i]; |
65e19f54 JA |
7512 | unsigned this_files; |
7513 | ||
7514 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7515 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7516 | GFP_KERNEL); | |
7517 | if (!table->files) | |
7518 | break; | |
7519 | nr_files -= this_files; | |
7520 | } | |
7521 | ||
7522 | if (i == nr_tables) | |
7523 | return 0; | |
7524 | ||
7525 | for (i = 0; i < nr_tables; i++) { | |
5398ae69 | 7526 | struct fixed_file_table *table = &file_data->table[i]; |
65e19f54 JA |
7527 | kfree(table->files); |
7528 | } | |
7529 | return 1; | |
7530 | } | |
7531 | ||
05f3fb3c JA |
7532 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
7533 | { | |
7534 | #if defined(CONFIG_UNIX) | |
7535 | struct sock *sock = ctx->ring_sock->sk; | |
7536 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7537 | struct sk_buff *skb; | |
7538 | int i; | |
7539 | ||
7540 | __skb_queue_head_init(&list); | |
7541 | ||
7542 | /* | |
7543 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7544 | * remove this entry and rearrange the file array. | |
7545 | */ | |
7546 | skb = skb_dequeue(head); | |
7547 | while (skb) { | |
7548 | struct scm_fp_list *fp; | |
7549 | ||
7550 | fp = UNIXCB(skb).fp; | |
7551 | for (i = 0; i < fp->count; i++) { | |
7552 | int left; | |
7553 | ||
7554 | if (fp->fp[i] != file) | |
7555 | continue; | |
7556 | ||
7557 | unix_notinflight(fp->user, fp->fp[i]); | |
7558 | left = fp->count - 1 - i; | |
7559 | if (left) { | |
7560 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7561 | left * sizeof(struct file *)); | |
7562 | } | |
7563 | fp->count--; | |
7564 | if (!fp->count) { | |
7565 | kfree_skb(skb); | |
7566 | skb = NULL; | |
7567 | } else { | |
7568 | __skb_queue_tail(&list, skb); | |
7569 | } | |
7570 | fput(file); | |
7571 | file = NULL; | |
7572 | break; | |
7573 | } | |
7574 | ||
7575 | if (!file) | |
7576 | break; | |
7577 | ||
7578 | __skb_queue_tail(&list, skb); | |
7579 | ||
7580 | skb = skb_dequeue(head); | |
7581 | } | |
7582 | ||
7583 | if (skb_peek(&list)) { | |
7584 | spin_lock_irq(&head->lock); | |
7585 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7586 | __skb_queue_tail(head, skb); | |
7587 | spin_unlock_irq(&head->lock); | |
7588 | } | |
7589 | #else | |
7590 | fput(file); | |
7591 | #endif | |
7592 | } | |
7593 | ||
7594 | struct io_file_put { | |
05589553 | 7595 | struct list_head list; |
05f3fb3c | 7596 | struct file *file; |
05f3fb3c JA |
7597 | }; |
7598 | ||
4a38aed2 | 7599 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
65e19f54 | 7600 | { |
4a38aed2 JA |
7601 | struct fixed_file_data *file_data = ref_node->file_data; |
7602 | struct io_ring_ctx *ctx = file_data->ctx; | |
05f3fb3c | 7603 | struct io_file_put *pfile, *tmp; |
05589553 XW |
7604 | |
7605 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { | |
6a4d07cd | 7606 | list_del(&pfile->list); |
05589553 XW |
7607 | io_ring_file_put(ctx, pfile->file); |
7608 | kfree(pfile); | |
65e19f54 | 7609 | } |
05589553 | 7610 | |
05589553 XW |
7611 | percpu_ref_exit(&ref_node->refs); |
7612 | kfree(ref_node); | |
7613 | percpu_ref_put(&file_data->refs); | |
2faf852d | 7614 | } |
65e19f54 | 7615 | |
4a38aed2 JA |
7616 | static void io_file_put_work(struct work_struct *work) |
7617 | { | |
7618 | struct io_ring_ctx *ctx; | |
7619 | struct llist_node *node; | |
7620 | ||
7621 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); | |
7622 | node = llist_del_all(&ctx->file_put_llist); | |
7623 | ||
7624 | while (node) { | |
7625 | struct fixed_file_ref_node *ref_node; | |
7626 | struct llist_node *next = node->next; | |
7627 | ||
7628 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); | |
7629 | __io_file_put_work(ref_node); | |
7630 | node = next; | |
7631 | } | |
7632 | } | |
7633 | ||
05589553 | 7634 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
2faf852d | 7635 | { |
05589553 | 7636 | struct fixed_file_ref_node *ref_node; |
e297822b | 7637 | struct fixed_file_data *data; |
4a38aed2 | 7638 | struct io_ring_ctx *ctx; |
e297822b | 7639 | bool first_add = false; |
4a38aed2 | 7640 | int delay = HZ; |
65e19f54 | 7641 | |
05589553 | 7642 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
e297822b PB |
7643 | data = ref_node->file_data; |
7644 | ctx = data->ctx; | |
7645 | ||
ac0648a5 | 7646 | spin_lock_bh(&data->lock); |
e297822b PB |
7647 | ref_node->done = true; |
7648 | ||
7649 | while (!list_empty(&data->ref_list)) { | |
7650 | ref_node = list_first_entry(&data->ref_list, | |
7651 | struct fixed_file_ref_node, node); | |
7652 | /* recycle ref nodes in order */ | |
7653 | if (!ref_node->done) | |
7654 | break; | |
7655 | list_del(&ref_node->node); | |
7656 | first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist); | |
7657 | } | |
ac0648a5 | 7658 | spin_unlock_bh(&data->lock); |
05589553 | 7659 | |
e297822b | 7660 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7661 | delay = 0; |
05589553 | 7662 | |
4a38aed2 JA |
7663 | if (!delay) |
7664 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); | |
7665 | else if (first_add) | |
7666 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); | |
05f3fb3c | 7667 | } |
65e19f54 | 7668 | |
05589553 XW |
7669 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
7670 | struct io_ring_ctx *ctx) | |
05f3fb3c | 7671 | { |
05589553 | 7672 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7673 | |
05589553 XW |
7674 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7675 | if (!ref_node) | |
7676 | return ERR_PTR(-ENOMEM); | |
05f3fb3c | 7677 | |
05589553 XW |
7678 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
7679 | 0, GFP_KERNEL)) { | |
7680 | kfree(ref_node); | |
7681 | return ERR_PTR(-ENOMEM); | |
7682 | } | |
7683 | INIT_LIST_HEAD(&ref_node->node); | |
7684 | INIT_LIST_HEAD(&ref_node->file_list); | |
05589553 | 7685 | ref_node->file_data = ctx->file_data; |
e297822b | 7686 | ref_node->done = false; |
05589553 | 7687 | return ref_node; |
05589553 XW |
7688 | } |
7689 | ||
7690 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) | |
7691 | { | |
7692 | percpu_ref_exit(&ref_node->refs); | |
7693 | kfree(ref_node); | |
65e19f54 JA |
7694 | } |
7695 | ||
6b06314c JA |
7696 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7697 | unsigned nr_args) | |
7698 | { | |
7699 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7700 | unsigned nr_tables, i; |
05f3fb3c | 7701 | struct file *file; |
600cf3f8 | 7702 | int fd, ret = -ENOMEM; |
05589553 | 7703 | struct fixed_file_ref_node *ref_node; |
5398ae69 | 7704 | struct fixed_file_data *file_data; |
6b06314c | 7705 | |
05f3fb3c | 7706 | if (ctx->file_data) |
6b06314c JA |
7707 | return -EBUSY; |
7708 | if (!nr_args) | |
7709 | return -EINVAL; | |
7710 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7711 | return -EMFILE; | |
7712 | ||
5398ae69 PB |
7713 | file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
7714 | if (!file_data) | |
05f3fb3c | 7715 | return -ENOMEM; |
5398ae69 PB |
7716 | file_data->ctx = ctx; |
7717 | init_completion(&file_data->done); | |
7718 | INIT_LIST_HEAD(&file_data->ref_list); | |
7719 | spin_lock_init(&file_data->lock); | |
05f3fb3c | 7720 | |
65e19f54 | 7721 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7722 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7723 | GFP_KERNEL); |
600cf3f8 PB |
7724 | if (!file_data->table) |
7725 | goto out_free; | |
05f3fb3c | 7726 | |
5398ae69 | 7727 | if (percpu_ref_init(&file_data->refs, io_file_ref_kill, |
600cf3f8 PB |
7728 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
7729 | goto out_free; | |
6b06314c | 7730 | |
600cf3f8 PB |
7731 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
7732 | goto out_ref; | |
55cbc256 | 7733 | ctx->file_data = file_data; |
65e19f54 | 7734 | |
08a45173 | 7735 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
7736 | struct fixed_file_table *table; |
7737 | unsigned index; | |
7738 | ||
600cf3f8 PB |
7739 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7740 | ret = -EFAULT; | |
7741 | goto out_fput; | |
7742 | } | |
08a45173 | 7743 | /* allow sparse sets */ |
600cf3f8 | 7744 | if (fd == -1) |
08a45173 | 7745 | continue; |
6b06314c | 7746 | |
05f3fb3c | 7747 | file = fget(fd); |
6b06314c | 7748 | ret = -EBADF; |
05f3fb3c | 7749 | if (!file) |
600cf3f8 | 7750 | goto out_fput; |
05f3fb3c | 7751 | |
6b06314c JA |
7752 | /* |
7753 | * Don't allow io_uring instances to be registered. If UNIX | |
7754 | * isn't enabled, then this causes a reference cycle and this | |
7755 | * instance can never get freed. If UNIX is enabled we'll | |
7756 | * handle it just fine, but there's still no point in allowing | |
7757 | * a ring fd as it doesn't support regular read/write anyway. | |
7758 | */ | |
05f3fb3c JA |
7759 | if (file->f_op == &io_uring_fops) { |
7760 | fput(file); | |
600cf3f8 | 7761 | goto out_fput; |
6b06314c | 7762 | } |
600cf3f8 PB |
7763 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
7764 | index = i & IORING_FILE_TABLE_MASK; | |
05f3fb3c | 7765 | table->files[index] = file; |
6b06314c JA |
7766 | } |
7767 | ||
6b06314c | 7768 | ret = io_sqe_files_scm(ctx); |
05589553 | 7769 | if (ret) { |
6b06314c | 7770 | io_sqe_files_unregister(ctx); |
05589553 XW |
7771 | return ret; |
7772 | } | |
6b06314c | 7773 | |
05589553 XW |
7774 | ref_node = alloc_fixed_file_ref_node(ctx); |
7775 | if (IS_ERR(ref_node)) { | |
7776 | io_sqe_files_unregister(ctx); | |
7777 | return PTR_ERR(ref_node); | |
7778 | } | |
7779 | ||
b2e96852 | 7780 | file_data->node = ref_node; |
ac0648a5 | 7781 | spin_lock_bh(&file_data->lock); |
e297822b | 7782 | list_add_tail(&ref_node->node, &file_data->ref_list); |
ac0648a5 | 7783 | spin_unlock_bh(&file_data->lock); |
5398ae69 | 7784 | percpu_ref_get(&file_data->refs); |
6b06314c | 7785 | return ret; |
600cf3f8 PB |
7786 | out_fput: |
7787 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7788 | file = io_file_from_index(ctx, i); | |
7789 | if (file) | |
7790 | fput(file); | |
7791 | } | |
7792 | for (i = 0; i < nr_tables; i++) | |
7793 | kfree(file_data->table[i].files); | |
7794 | ctx->nr_user_files = 0; | |
7795 | out_ref: | |
7796 | percpu_ref_exit(&file_data->refs); | |
7797 | out_free: | |
7798 | kfree(file_data->table); | |
7799 | kfree(file_data); | |
55cbc256 | 7800 | ctx->file_data = NULL; |
6b06314c JA |
7801 | return ret; |
7802 | } | |
7803 | ||
c3a31e60 JA |
7804 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7805 | int index) | |
7806 | { | |
7807 | #if defined(CONFIG_UNIX) | |
7808 | struct sock *sock = ctx->ring_sock->sk; | |
7809 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7810 | struct sk_buff *skb; | |
7811 | ||
7812 | /* | |
7813 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7814 | * file set. If there's no room, fall back to allocating a new skb | |
7815 | * and filling it in. | |
7816 | */ | |
7817 | spin_lock_irq(&head->lock); | |
7818 | skb = skb_peek(head); | |
7819 | if (skb) { | |
7820 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7821 | ||
7822 | if (fpl->count < SCM_MAX_FD) { | |
7823 | __skb_unlink(skb, head); | |
7824 | spin_unlock_irq(&head->lock); | |
7825 | fpl->fp[fpl->count] = get_file(file); | |
7826 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7827 | fpl->count++; | |
7828 | spin_lock_irq(&head->lock); | |
7829 | __skb_queue_head(head, skb); | |
7830 | } else { | |
7831 | skb = NULL; | |
7832 | } | |
7833 | } | |
7834 | spin_unlock_irq(&head->lock); | |
7835 | ||
7836 | if (skb) { | |
7837 | fput(file); | |
7838 | return 0; | |
7839 | } | |
7840 | ||
7841 | return __io_sqe_files_scm(ctx, 1, index); | |
7842 | #else | |
7843 | return 0; | |
7844 | #endif | |
7845 | } | |
7846 | ||
a5318d3c | 7847 | static int io_queue_file_removal(struct fixed_file_data *data, |
05589553 | 7848 | struct file *file) |
05f3fb3c | 7849 | { |
a5318d3c | 7850 | struct io_file_put *pfile; |
b2e96852 | 7851 | struct fixed_file_ref_node *ref_node = data->node; |
05f3fb3c | 7852 | |
05f3fb3c | 7853 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
a5318d3c HD |
7854 | if (!pfile) |
7855 | return -ENOMEM; | |
05f3fb3c JA |
7856 | |
7857 | pfile->file = file; | |
05589553 XW |
7858 | list_add(&pfile->list, &ref_node->file_list); |
7859 | ||
a5318d3c | 7860 | return 0; |
05f3fb3c JA |
7861 | } |
7862 | ||
7863 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
7864 | struct io_uring_files_update *up, | |
7865 | unsigned nr_args) | |
7866 | { | |
7867 | struct fixed_file_data *data = ctx->file_data; | |
05589553 | 7868 | struct fixed_file_ref_node *ref_node; |
05f3fb3c | 7869 | struct file *file; |
c3a31e60 JA |
7870 | __s32 __user *fds; |
7871 | int fd, i, err; | |
7872 | __u32 done; | |
05589553 | 7873 | bool needs_switch = false; |
c3a31e60 | 7874 | |
05f3fb3c | 7875 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7876 | return -EOVERFLOW; |
7877 | if (done > ctx->nr_user_files) | |
7878 | return -EINVAL; | |
7879 | ||
05589553 XW |
7880 | ref_node = alloc_fixed_file_ref_node(ctx); |
7881 | if (IS_ERR(ref_node)) | |
7882 | return PTR_ERR(ref_node); | |
7883 | ||
c3a31e60 | 7884 | done = 0; |
05f3fb3c | 7885 | fds = u64_to_user_ptr(up->fds); |
c3a31e60 | 7886 | while (nr_args) { |
65e19f54 JA |
7887 | struct fixed_file_table *table; |
7888 | unsigned index; | |
7889 | ||
c3a31e60 JA |
7890 | err = 0; |
7891 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7892 | err = -EFAULT; | |
7893 | break; | |
7894 | } | |
05f3fb3c JA |
7895 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
7896 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
7897 | index = i & IORING_FILE_TABLE_MASK; |
7898 | if (table->files[index]) { | |
98dfd502 | 7899 | file = table->files[index]; |
a5318d3c HD |
7900 | err = io_queue_file_removal(data, file); |
7901 | if (err) | |
7902 | break; | |
65e19f54 | 7903 | table->files[index] = NULL; |
05589553 | 7904 | needs_switch = true; |
c3a31e60 JA |
7905 | } |
7906 | if (fd != -1) { | |
c3a31e60 JA |
7907 | file = fget(fd); |
7908 | if (!file) { | |
7909 | err = -EBADF; | |
7910 | break; | |
7911 | } | |
7912 | /* | |
7913 | * Don't allow io_uring instances to be registered. If | |
7914 | * UNIX isn't enabled, then this causes a reference | |
7915 | * cycle and this instance can never get freed. If UNIX | |
7916 | * is enabled we'll handle it just fine, but there's | |
7917 | * still no point in allowing a ring fd as it doesn't | |
7918 | * support regular read/write anyway. | |
7919 | */ | |
7920 | if (file->f_op == &io_uring_fops) { | |
7921 | fput(file); | |
7922 | err = -EBADF; | |
7923 | break; | |
7924 | } | |
65e19f54 | 7925 | table->files[index] = file; |
c3a31e60 | 7926 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7927 | if (err) { |
95d1c8e5 | 7928 | table->files[index] = NULL; |
f3bd9dae | 7929 | fput(file); |
c3a31e60 | 7930 | break; |
f3bd9dae | 7931 | } |
c3a31e60 JA |
7932 | } |
7933 | nr_args--; | |
7934 | done++; | |
05f3fb3c JA |
7935 | up->offset++; |
7936 | } | |
7937 | ||
05589553 | 7938 | if (needs_switch) { |
b2e96852 | 7939 | percpu_ref_kill(&data->node->refs); |
ac0648a5 | 7940 | spin_lock_bh(&data->lock); |
e297822b | 7941 | list_add_tail(&ref_node->node, &data->ref_list); |
b2e96852 | 7942 | data->node = ref_node; |
ac0648a5 | 7943 | spin_unlock_bh(&data->lock); |
05589553 XW |
7944 | percpu_ref_get(&ctx->file_data->refs); |
7945 | } else | |
7946 | destroy_fixed_file_ref_node(ref_node); | |
c3a31e60 JA |
7947 | |
7948 | return done ? done : err; | |
7949 | } | |
05589553 | 7950 | |
05f3fb3c JA |
7951 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7952 | unsigned nr_args) | |
7953 | { | |
7954 | struct io_uring_files_update up; | |
7955 | ||
7956 | if (!ctx->file_data) | |
7957 | return -ENXIO; | |
7958 | if (!nr_args) | |
7959 | return -EINVAL; | |
7960 | if (copy_from_user(&up, arg, sizeof(up))) | |
7961 | return -EFAULT; | |
7962 | if (up.resv) | |
7963 | return -EINVAL; | |
7964 | ||
7965 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7966 | } | |
c3a31e60 | 7967 | |
e9fd9396 | 7968 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
7969 | { |
7970 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7971 | ||
e9fd9396 | 7972 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
7973 | io_put_req(req); |
7974 | } | |
7975 | ||
24369c2e PB |
7976 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
7977 | struct io_uring_params *p) | |
7978 | { | |
7979 | struct io_wq_data data; | |
7980 | struct fd f; | |
7981 | struct io_ring_ctx *ctx_attach; | |
7982 | unsigned int concurrency; | |
7983 | int ret = 0; | |
7984 | ||
7985 | data.user = ctx->user; | |
e9fd9396 | 7986 | data.free_work = io_free_work; |
f5fa38c5 | 7987 | data.do_work = io_wq_submit_work; |
24369c2e PB |
7988 | |
7989 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
7990 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
7991 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
7992 | ||
7993 | ctx->io_wq = io_wq_create(concurrency, &data); | |
7994 | if (IS_ERR(ctx->io_wq)) { | |
7995 | ret = PTR_ERR(ctx->io_wq); | |
7996 | ctx->io_wq = NULL; | |
7997 | } | |
7998 | return ret; | |
7999 | } | |
8000 | ||
8001 | f = fdget(p->wq_fd); | |
8002 | if (!f.file) | |
8003 | return -EBADF; | |
8004 | ||
8005 | if (f.file->f_op != &io_uring_fops) { | |
8006 | ret = -EINVAL; | |
8007 | goto out_fput; | |
8008 | } | |
8009 | ||
8010 | ctx_attach = f.file->private_data; | |
8011 | /* @io_wq is protected by holding the fd */ | |
8012 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8013 | ret = -EINVAL; | |
8014 | goto out_fput; | |
8015 | } | |
8016 | ||
8017 | ctx->io_wq = ctx_attach->io_wq; | |
8018 | out_fput: | |
8019 | fdput(f); | |
8020 | return ret; | |
8021 | } | |
8022 | ||
0f212204 JA |
8023 | static int io_uring_alloc_task_context(struct task_struct *task) |
8024 | { | |
8025 | struct io_uring_task *tctx; | |
d8a6df10 | 8026 | int ret; |
0f212204 JA |
8027 | |
8028 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8029 | if (unlikely(!tctx)) | |
8030 | return -ENOMEM; | |
8031 | ||
d8a6df10 JA |
8032 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8033 | if (unlikely(ret)) { | |
8034 | kfree(tctx); | |
8035 | return ret; | |
8036 | } | |
8037 | ||
0f212204 JA |
8038 | xa_init(&tctx->xa); |
8039 | init_waitqueue_head(&tctx->wait); | |
8040 | tctx->last = NULL; | |
fdaf083c JA |
8041 | atomic_set(&tctx->in_idle, 0); |
8042 | tctx->sqpoll = false; | |
500a373d JA |
8043 | io_init_identity(&tctx->__identity); |
8044 | tctx->identity = &tctx->__identity; | |
0f212204 JA |
8045 | task->io_uring = tctx; |
8046 | return 0; | |
8047 | } | |
8048 | ||
8049 | void __io_uring_free(struct task_struct *tsk) | |
8050 | { | |
8051 | struct io_uring_task *tctx = tsk->io_uring; | |
8052 | ||
8053 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8054 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8055 | if (tctx->identity != &tctx->__identity) | |
8056 | kfree(tctx->identity); | |
d8a6df10 | 8057 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8058 | kfree(tctx); |
8059 | tsk->io_uring = NULL; | |
8060 | } | |
8061 | ||
7e84e1c7 SG |
8062 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8063 | struct io_uring_params *p) | |
2b188cc1 JA |
8064 | { |
8065 | int ret; | |
8066 | ||
6c271ce2 | 8067 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8068 | struct io_sq_data *sqd; |
8069 | ||
3ec482d1 | 8070 | ret = -EPERM; |
ce59fc69 | 8071 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8072 | goto err; |
8073 | ||
534ca6d6 JA |
8074 | sqd = io_get_sq_data(p); |
8075 | if (IS_ERR(sqd)) { | |
8076 | ret = PTR_ERR(sqd); | |
8077 | goto err; | |
8078 | } | |
69fb2131 | 8079 | |
534ca6d6 | 8080 | ctx->sq_data = sqd; |
69fb2131 JA |
8081 | io_sq_thread_park(sqd); |
8082 | mutex_lock(&sqd->ctx_lock); | |
8083 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8084 | mutex_unlock(&sqd->ctx_lock); | |
8085 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8086 | |
917257da JA |
8087 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8088 | if (!ctx->sq_thread_idle) | |
8089 | ctx->sq_thread_idle = HZ; | |
8090 | ||
aa06165d JA |
8091 | if (sqd->thread) |
8092 | goto done; | |
8093 | ||
6c271ce2 | 8094 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8095 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8096 | |
917257da | 8097 | ret = -EINVAL; |
44a9bd18 JA |
8098 | if (cpu >= nr_cpu_ids) |
8099 | goto err; | |
7889f44d | 8100 | if (!cpu_online(cpu)) |
917257da JA |
8101 | goto err; |
8102 | ||
69fb2131 | 8103 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8104 | cpu, "io_uring-sq"); |
6c271ce2 | 8105 | } else { |
69fb2131 | 8106 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8107 | "io_uring-sq"); |
8108 | } | |
534ca6d6 JA |
8109 | if (IS_ERR(sqd->thread)) { |
8110 | ret = PTR_ERR(sqd->thread); | |
8111 | sqd->thread = NULL; | |
6c271ce2 JA |
8112 | goto err; |
8113 | } | |
534ca6d6 | 8114 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8115 | if (ret) |
8116 | goto err; | |
6c271ce2 JA |
8117 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8118 | /* Can't have SQ_AFF without SQPOLL */ | |
8119 | ret = -EINVAL; | |
8120 | goto err; | |
8121 | } | |
8122 | ||
aa06165d | 8123 | done: |
24369c2e PB |
8124 | ret = io_init_wq_offload(ctx, p); |
8125 | if (ret) | |
2b188cc1 | 8126 | goto err; |
2b188cc1 JA |
8127 | |
8128 | return 0; | |
8129 | err: | |
54a91f3b | 8130 | io_finish_async(ctx); |
2b188cc1 JA |
8131 | return ret; |
8132 | } | |
8133 | ||
7e84e1c7 SG |
8134 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8135 | { | |
534ca6d6 JA |
8136 | struct io_sq_data *sqd = ctx->sq_data; |
8137 | ||
8138 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8139 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8140 | } |
8141 | ||
a087e2b5 BM |
8142 | static inline void __io_unaccount_mem(struct user_struct *user, |
8143 | unsigned long nr_pages) | |
2b188cc1 JA |
8144 | { |
8145 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8146 | } | |
8147 | ||
a087e2b5 BM |
8148 | static inline int __io_account_mem(struct user_struct *user, |
8149 | unsigned long nr_pages) | |
2b188cc1 JA |
8150 | { |
8151 | unsigned long page_limit, cur_pages, new_pages; | |
8152 | ||
8153 | /* Don't allow more pages than we can safely lock */ | |
8154 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8155 | ||
8156 | do { | |
8157 | cur_pages = atomic_long_read(&user->locked_vm); | |
8158 | new_pages = cur_pages + nr_pages; | |
8159 | if (new_pages > page_limit) | |
8160 | return -ENOMEM; | |
8161 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8162 | new_pages) != cur_pages); | |
8163 | ||
8164 | return 0; | |
8165 | } | |
8166 | ||
2e0464d4 BM |
8167 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8168 | enum io_mem_account acct) | |
a087e2b5 | 8169 | { |
aad5d8da | 8170 | if (ctx->limit_mem) |
a087e2b5 | 8171 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8172 | |
2aede0e4 | 8173 | if (ctx->mm_account) { |
2e0464d4 | 8174 | if (acct == ACCT_LOCKED) |
2aede0e4 | 8175 | ctx->mm_account->locked_vm -= nr_pages; |
2e0464d4 | 8176 | else if (acct == ACCT_PINNED) |
2aede0e4 | 8177 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
2e0464d4 | 8178 | } |
a087e2b5 BM |
8179 | } |
8180 | ||
2e0464d4 BM |
8181 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8182 | enum io_mem_account acct) | |
a087e2b5 | 8183 | { |
30975825 BM |
8184 | int ret; |
8185 | ||
8186 | if (ctx->limit_mem) { | |
8187 | ret = __io_account_mem(ctx->user, nr_pages); | |
8188 | if (ret) | |
8189 | return ret; | |
8190 | } | |
8191 | ||
2aede0e4 | 8192 | if (ctx->mm_account) { |
2e0464d4 | 8193 | if (acct == ACCT_LOCKED) |
2aede0e4 | 8194 | ctx->mm_account->locked_vm += nr_pages; |
2e0464d4 | 8195 | else if (acct == ACCT_PINNED) |
2aede0e4 | 8196 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
2e0464d4 | 8197 | } |
a087e2b5 BM |
8198 | |
8199 | return 0; | |
8200 | } | |
8201 | ||
2b188cc1 JA |
8202 | static void io_mem_free(void *ptr) |
8203 | { | |
52e04ef4 MR |
8204 | struct page *page; |
8205 | ||
8206 | if (!ptr) | |
8207 | return; | |
2b188cc1 | 8208 | |
52e04ef4 | 8209 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8210 | if (put_page_testzero(page)) |
8211 | free_compound_page(page); | |
8212 | } | |
8213 | ||
8214 | static void *io_mem_alloc(size_t size) | |
8215 | { | |
8216 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
8217 | __GFP_NORETRY; | |
8218 | ||
8219 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8220 | } | |
8221 | ||
75b28aff HV |
8222 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8223 | size_t *sq_offset) | |
8224 | { | |
8225 | struct io_rings *rings; | |
8226 | size_t off, sq_array_size; | |
8227 | ||
8228 | off = struct_size(rings, cqes, cq_entries); | |
8229 | if (off == SIZE_MAX) | |
8230 | return SIZE_MAX; | |
8231 | ||
8232 | #ifdef CONFIG_SMP | |
8233 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8234 | if (off == 0) | |
8235 | return SIZE_MAX; | |
8236 | #endif | |
8237 | ||
b36200f5 DV |
8238 | if (sq_offset) |
8239 | *sq_offset = off; | |
8240 | ||
75b28aff HV |
8241 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8242 | if (sq_array_size == SIZE_MAX) | |
8243 | return SIZE_MAX; | |
8244 | ||
8245 | if (check_add_overflow(off, sq_array_size, &off)) | |
8246 | return SIZE_MAX; | |
8247 | ||
75b28aff HV |
8248 | return off; |
8249 | } | |
8250 | ||
2b188cc1 JA |
8251 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8252 | { | |
75b28aff | 8253 | size_t pages; |
2b188cc1 | 8254 | |
75b28aff HV |
8255 | pages = (size_t)1 << get_order( |
8256 | rings_size(sq_entries, cq_entries, NULL)); | |
8257 | pages += (size_t)1 << get_order( | |
8258 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8259 | |
75b28aff | 8260 | return pages; |
2b188cc1 JA |
8261 | } |
8262 | ||
edafccee JA |
8263 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
8264 | { | |
8265 | int i, j; | |
8266 | ||
8267 | if (!ctx->user_bufs) | |
8268 | return -ENXIO; | |
8269 | ||
8270 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8271 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8272 | ||
8273 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8274 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8275 | |
de293938 JA |
8276 | if (imu->acct_pages) |
8277 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8278 | kvfree(imu->bvec); |
edafccee JA |
8279 | imu->nr_bvecs = 0; |
8280 | } | |
8281 | ||
8282 | kfree(ctx->user_bufs); | |
8283 | ctx->user_bufs = NULL; | |
8284 | ctx->nr_user_bufs = 0; | |
8285 | return 0; | |
8286 | } | |
8287 | ||
8288 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8289 | void __user *arg, unsigned index) | |
8290 | { | |
8291 | struct iovec __user *src; | |
8292 | ||
8293 | #ifdef CONFIG_COMPAT | |
8294 | if (ctx->compat) { | |
8295 | struct compat_iovec __user *ciovs; | |
8296 | struct compat_iovec ciov; | |
8297 | ||
8298 | ciovs = (struct compat_iovec __user *) arg; | |
8299 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8300 | return -EFAULT; | |
8301 | ||
d55e5f5b | 8302 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8303 | dst->iov_len = ciov.iov_len; |
8304 | return 0; | |
8305 | } | |
8306 | #endif | |
8307 | src = (struct iovec __user *) arg; | |
8308 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8309 | return -EFAULT; | |
8310 | return 0; | |
8311 | } | |
8312 | ||
de293938 JA |
8313 | /* |
8314 | * Not super efficient, but this is just a registration time. And we do cache | |
8315 | * the last compound head, so generally we'll only do a full search if we don't | |
8316 | * match that one. | |
8317 | * | |
8318 | * We check if the given compound head page has already been accounted, to | |
8319 | * avoid double accounting it. This allows us to account the full size of the | |
8320 | * page, not just the constituent pages of a huge page. | |
8321 | */ | |
8322 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8323 | int nr_pages, struct page *hpage) | |
8324 | { | |
8325 | int i, j; | |
8326 | ||
8327 | /* check current page array */ | |
8328 | for (i = 0; i < nr_pages; i++) { | |
8329 | if (!PageCompound(pages[i])) | |
8330 | continue; | |
8331 | if (compound_head(pages[i]) == hpage) | |
8332 | return true; | |
8333 | } | |
8334 | ||
8335 | /* check previously registered pages */ | |
8336 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8337 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8338 | ||
8339 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8340 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8341 | continue; | |
8342 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8343 | return true; | |
8344 | } | |
8345 | } | |
8346 | ||
8347 | return false; | |
8348 | } | |
8349 | ||
8350 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8351 | int nr_pages, struct io_mapped_ubuf *imu, | |
8352 | struct page **last_hpage) | |
8353 | { | |
8354 | int i, ret; | |
8355 | ||
8356 | for (i = 0; i < nr_pages; i++) { | |
8357 | if (!PageCompound(pages[i])) { | |
8358 | imu->acct_pages++; | |
8359 | } else { | |
8360 | struct page *hpage; | |
8361 | ||
8362 | hpage = compound_head(pages[i]); | |
8363 | if (hpage == *last_hpage) | |
8364 | continue; | |
8365 | *last_hpage = hpage; | |
8366 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8367 | continue; | |
8368 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8369 | } | |
8370 | } | |
8371 | ||
8372 | if (!imu->acct_pages) | |
8373 | return 0; | |
8374 | ||
8375 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8376 | if (ret) | |
8377 | imu->acct_pages = 0; | |
8378 | return ret; | |
8379 | } | |
8380 | ||
edafccee JA |
8381 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
8382 | unsigned nr_args) | |
8383 | { | |
8384 | struct vm_area_struct **vmas = NULL; | |
8385 | struct page **pages = NULL; | |
de293938 | 8386 | struct page *last_hpage = NULL; |
edafccee JA |
8387 | int i, j, got_pages = 0; |
8388 | int ret = -EINVAL; | |
8389 | ||
8390 | if (ctx->user_bufs) | |
8391 | return -EBUSY; | |
8392 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8393 | return -EINVAL; | |
8394 | ||
8395 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8396 | GFP_KERNEL); | |
8397 | if (!ctx->user_bufs) | |
8398 | return -ENOMEM; | |
8399 | ||
8400 | for (i = 0; i < nr_args; i++) { | |
8401 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8402 | unsigned long off, start, end, ubuf; | |
8403 | int pret, nr_pages; | |
8404 | struct iovec iov; | |
8405 | size_t size; | |
8406 | ||
8407 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8408 | if (ret) | |
a278682d | 8409 | goto err; |
edafccee JA |
8410 | |
8411 | /* | |
8412 | * Don't impose further limits on the size and buffer | |
8413 | * constraints here, we'll -EINVAL later when IO is | |
8414 | * submitted if they are wrong. | |
8415 | */ | |
8416 | ret = -EFAULT; | |
8417 | if (!iov.iov_base || !iov.iov_len) | |
8418 | goto err; | |
8419 | ||
8420 | /* arbitrary limit, but we need something */ | |
8421 | if (iov.iov_len > SZ_1G) | |
8422 | goto err; | |
8423 | ||
8424 | ubuf = (unsigned long) iov.iov_base; | |
8425 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8426 | start = ubuf >> PAGE_SHIFT; | |
8427 | nr_pages = end - start; | |
8428 | ||
edafccee JA |
8429 | ret = 0; |
8430 | if (!pages || nr_pages > got_pages) { | |
a8c73c1a DE |
8431 | kvfree(vmas); |
8432 | kvfree(pages); | |
d4ef6475 | 8433 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 8434 | GFP_KERNEL); |
d4ef6475 | 8435 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
8436 | sizeof(struct vm_area_struct *), |
8437 | GFP_KERNEL); | |
8438 | if (!pages || !vmas) { | |
8439 | ret = -ENOMEM; | |
edafccee JA |
8440 | goto err; |
8441 | } | |
8442 | got_pages = nr_pages; | |
8443 | } | |
8444 | ||
d4ef6475 | 8445 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
8446 | GFP_KERNEL); |
8447 | ret = -ENOMEM; | |
de293938 | 8448 | if (!imu->bvec) |
edafccee | 8449 | goto err; |
edafccee JA |
8450 | |
8451 | ret = 0; | |
d8ed45c5 | 8452 | mmap_read_lock(current->mm); |
2113b05d | 8453 | pret = pin_user_pages(ubuf, nr_pages, |
932f4a63 IW |
8454 | FOLL_WRITE | FOLL_LONGTERM, |
8455 | pages, vmas); | |
edafccee JA |
8456 | if (pret == nr_pages) { |
8457 | /* don't support file backed memory */ | |
8458 | for (j = 0; j < nr_pages; j++) { | |
8459 | struct vm_area_struct *vma = vmas[j]; | |
8460 | ||
8461 | if (vma->vm_file && | |
8462 | !is_file_hugepages(vma->vm_file)) { | |
8463 | ret = -EOPNOTSUPP; | |
8464 | break; | |
8465 | } | |
8466 | } | |
8467 | } else { | |
8468 | ret = pret < 0 ? pret : -EFAULT; | |
8469 | } | |
d8ed45c5 | 8470 | mmap_read_unlock(current->mm); |
edafccee JA |
8471 | if (ret) { |
8472 | /* | |
8473 | * if we did partial map, or found file backed vmas, | |
8474 | * release any pages we did get | |
8475 | */ | |
27c4d3a3 | 8476 | if (pret > 0) |
f1f6a7dd | 8477 | unpin_user_pages(pages, pret); |
de293938 JA |
8478 | kvfree(imu->bvec); |
8479 | goto err; | |
8480 | } | |
8481 | ||
8482 | ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage); | |
8483 | if (ret) { | |
8484 | unpin_user_pages(pages, pret); | |
d4ef6475 | 8485 | kvfree(imu->bvec); |
edafccee JA |
8486 | goto err; |
8487 | } | |
8488 | ||
8489 | off = ubuf & ~PAGE_MASK; | |
8490 | size = iov.iov_len; | |
8491 | for (j = 0; j < nr_pages; j++) { | |
8492 | size_t vec_len; | |
8493 | ||
8494 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8495 | imu->bvec[j].bv_page = pages[j]; | |
8496 | imu->bvec[j].bv_len = vec_len; | |
8497 | imu->bvec[j].bv_offset = off; | |
8498 | off = 0; | |
8499 | size -= vec_len; | |
8500 | } | |
8501 | /* store original address for later verification */ | |
8502 | imu->ubuf = ubuf; | |
8503 | imu->len = iov.iov_len; | |
8504 | imu->nr_bvecs = nr_pages; | |
8505 | ||
8506 | ctx->nr_user_bufs++; | |
8507 | } | |
d4ef6475 MR |
8508 | kvfree(pages); |
8509 | kvfree(vmas); | |
edafccee JA |
8510 | return 0; |
8511 | err: | |
d4ef6475 MR |
8512 | kvfree(pages); |
8513 | kvfree(vmas); | |
edafccee JA |
8514 | io_sqe_buffer_unregister(ctx); |
8515 | return ret; | |
8516 | } | |
8517 | ||
9b402849 JA |
8518 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8519 | { | |
8520 | __s32 __user *fds = arg; | |
8521 | int fd; | |
8522 | ||
8523 | if (ctx->cq_ev_fd) | |
8524 | return -EBUSY; | |
8525 | ||
8526 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8527 | return -EFAULT; | |
8528 | ||
8529 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8530 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8531 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8532 | ctx->cq_ev_fd = NULL; | |
8533 | return ret; | |
8534 | } | |
8535 | ||
8536 | return 0; | |
8537 | } | |
8538 | ||
8539 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8540 | { | |
8541 | if (ctx->cq_ev_fd) { | |
8542 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8543 | ctx->cq_ev_fd = NULL; | |
8544 | return 0; | |
8545 | } | |
8546 | ||
8547 | return -ENXIO; | |
8548 | } | |
8549 | ||
5a2e745d JA |
8550 | static int __io_destroy_buffers(int id, void *p, void *data) |
8551 | { | |
8552 | struct io_ring_ctx *ctx = data; | |
8553 | struct io_buffer *buf = p; | |
8554 | ||
067524e9 | 8555 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8556 | return 0; |
8557 | } | |
8558 | ||
8559 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8560 | { | |
8561 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8562 | idr_destroy(&ctx->io_buffer_idr); | |
8563 | } | |
8564 | ||
2b188cc1 JA |
8565 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8566 | { | |
6b06314c | 8567 | io_finish_async(ctx); |
5dbcad51 | 8568 | io_sqe_buffer_unregister(ctx); |
2aede0e4 JA |
8569 | |
8570 | if (ctx->sqo_task) { | |
8571 | put_task_struct(ctx->sqo_task); | |
8572 | ctx->sqo_task = NULL; | |
8573 | mmdrop(ctx->mm_account); | |
8574 | ctx->mm_account = NULL; | |
30975825 | 8575 | } |
def596e9 | 8576 | |
91d8f519 DZ |
8577 | #ifdef CONFIG_BLK_CGROUP |
8578 | if (ctx->sqo_blkcg_css) | |
8579 | css_put(ctx->sqo_blkcg_css); | |
8580 | #endif | |
8581 | ||
6b06314c | 8582 | io_sqe_files_unregister(ctx); |
9b402849 | 8583 | io_eventfd_unregister(ctx); |
5a2e745d | 8584 | io_destroy_buffers(ctx); |
41726c9a | 8585 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8586 | |
2b188cc1 | 8587 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8588 | if (ctx->ring_sock) { |
8589 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8590 | sock_release(ctx->ring_sock); |
355e8d26 | 8591 | } |
2b188cc1 JA |
8592 | #endif |
8593 | ||
75b28aff | 8594 | io_mem_free(ctx->rings); |
2b188cc1 | 8595 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8596 | |
8597 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8598 | free_uid(ctx->user); |
181e448d | 8599 | put_cred(ctx->creds); |
78076bb6 | 8600 | kfree(ctx->cancel_hash); |
0ddf92e8 | 8601 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
8602 | kfree(ctx); |
8603 | } | |
8604 | ||
8605 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8606 | { | |
8607 | struct io_ring_ctx *ctx = file->private_data; | |
8608 | __poll_t mask = 0; | |
8609 | ||
8610 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8611 | /* |
8612 | * synchronizes with barrier from wq_has_sleeper call in | |
8613 | * io_commit_cqring | |
8614 | */ | |
2b188cc1 | 8615 | smp_rmb(); |
90554200 | 8616 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8617 | mask |= EPOLLOUT | EPOLLWRNORM; |
63e5d81f | 8618 | if (io_cqring_events(ctx, false)) |
2b188cc1 JA |
8619 | mask |= EPOLLIN | EPOLLRDNORM; |
8620 | ||
8621 | return mask; | |
8622 | } | |
8623 | ||
8624 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8625 | { | |
8626 | struct io_ring_ctx *ctx = file->private_data; | |
8627 | ||
8628 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8629 | } | |
8630 | ||
071698e1 JA |
8631 | static int io_remove_personalities(int id, void *p, void *data) |
8632 | { | |
8633 | struct io_ring_ctx *ctx = data; | |
1e6fa521 | 8634 | struct io_identity *iod; |
071698e1 | 8635 | |
1e6fa521 JA |
8636 | iod = idr_remove(&ctx->personality_idr, id); |
8637 | if (iod) { | |
8638 | put_cred(iod->creds); | |
8639 | if (refcount_dec_and_test(&iod->count)) | |
8640 | kfree(iod); | |
8641 | } | |
071698e1 JA |
8642 | return 0; |
8643 | } | |
8644 | ||
85faa7b8 JA |
8645 | static void io_ring_exit_work(struct work_struct *work) |
8646 | { | |
b2edc0a7 PB |
8647 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8648 | exit_work); | |
85faa7b8 | 8649 | |
56952e91 JA |
8650 | /* |
8651 | * If we're doing polled IO and end up having requests being | |
8652 | * submitted async (out-of-line), then completions can come in while | |
8653 | * we're waiting for refs to drop. We need to reap these manually, | |
8654 | * as nobody else will be looking for them. | |
8655 | */ | |
b2edc0a7 | 8656 | do { |
56952e91 | 8657 | if (ctx->rings) |
e6c8aa9a | 8658 | io_cqring_overflow_flush(ctx, true, NULL, NULL); |
b2edc0a7 PB |
8659 | io_iopoll_try_reap_events(ctx); |
8660 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); | |
85faa7b8 JA |
8661 | io_ring_ctx_free(ctx); |
8662 | } | |
8663 | ||
2b188cc1 JA |
8664 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8665 | { | |
8666 | mutex_lock(&ctx->uring_lock); | |
8667 | percpu_ref_kill(&ctx->refs); | |
8668 | mutex_unlock(&ctx->uring_lock); | |
8669 | ||
6b81928d PB |
8670 | io_kill_timeouts(ctx, NULL, NULL); |
8671 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8672 | |
8673 | if (ctx->io_wq) | |
8674 | io_wq_cancel_all(ctx->io_wq); | |
8675 | ||
15dff286 JA |
8676 | /* if we failed setting up the ctx, we might not have any rings */ |
8677 | if (ctx->rings) | |
e6c8aa9a | 8678 | io_cqring_overflow_flush(ctx, true, NULL, NULL); |
b2edc0a7 | 8679 | io_iopoll_try_reap_events(ctx); |
071698e1 | 8680 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
309fc03a JA |
8681 | |
8682 | /* | |
8683 | * Do this upfront, so we won't have a grace period where the ring | |
8684 | * is closed but resources aren't reaped yet. This can cause | |
8685 | * spurious failure in setting up a new ring. | |
8686 | */ | |
760618f7 JA |
8687 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8688 | ACCT_LOCKED); | |
309fc03a | 8689 | |
85faa7b8 | 8690 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8691 | /* |
8692 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8693 | * if we're exiting a ton of rings at the same time. It just adds | |
8694 | * noise and overhead, there's no discernable change in runtime | |
8695 | * over using system_wq. | |
8696 | */ | |
8697 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8698 | } |
8699 | ||
8700 | static int io_uring_release(struct inode *inode, struct file *file) | |
8701 | { | |
8702 | struct io_ring_ctx *ctx = file->private_data; | |
8703 | ||
8704 | file->private_data = NULL; | |
8705 | io_ring_ctx_wait_and_kill(ctx); | |
8706 | return 0; | |
8707 | } | |
8708 | ||
f6edbabb PB |
8709 | struct io_task_cancel { |
8710 | struct task_struct *task; | |
8711 | struct files_struct *files; | |
8712 | }; | |
f254ac04 | 8713 | |
f6edbabb | 8714 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8715 | { |
9a472ef7 | 8716 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8717 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8718 | bool ret; |
8719 | ||
f6edbabb | 8720 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8721 | unsigned long flags; |
8722 | struct io_ring_ctx *ctx = req->ctx; | |
8723 | ||
8724 | /* protect against races with linked timeouts */ | |
8725 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8726 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8727 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8728 | } else { | |
f6edbabb | 8729 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8730 | } |
8731 | return ret; | |
b711d4ea JA |
8732 | } |
8733 | ||
b7ddce3c | 8734 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8735 | struct task_struct *task, |
b7ddce3c PB |
8736 | struct files_struct *files) |
8737 | { | |
8738 | struct io_defer_entry *de = NULL; | |
8739 | LIST_HEAD(list); | |
8740 | ||
8741 | spin_lock_irq(&ctx->completion_lock); | |
8742 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8743 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8744 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8745 | break; | |
8746 | } | |
8747 | } | |
8748 | spin_unlock_irq(&ctx->completion_lock); | |
8749 | ||
8750 | while (!list_empty(&list)) { | |
8751 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8752 | list_del_init(&de->list); | |
8753 | req_set_fail_links(de->req); | |
8754 | io_put_req(de->req); | |
8755 | io_req_complete(de->req, -ECANCELED); | |
8756 | kfree(de); | |
8757 | } | |
8758 | } | |
8759 | ||
b52fda00 | 8760 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8761 | struct task_struct *task, |
fcb323cc JA |
8762 | struct files_struct *files) |
8763 | { | |
fcb323cc | 8764 | while (!list_empty_careful(&ctx->inflight_list)) { |
bee749b1 | 8765 | struct io_task_cancel cancel = { .task = task, .files = files }; |
f6edbabb | 8766 | struct io_kiocb *req; |
d8f1b971 | 8767 | DEFINE_WAIT(wait); |
f6edbabb | 8768 | bool found = false; |
fcb323cc JA |
8769 | |
8770 | spin_lock_irq(&ctx->inflight_lock); | |
8771 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
bee749b1 | 8772 | if (req->task != task || |
98447d65 | 8773 | req->work.identity->files != files) |
768134d4 | 8774 | continue; |
f6edbabb | 8775 | found = true; |
768134d4 | 8776 | break; |
fcb323cc | 8777 | } |
f6edbabb | 8778 | if (found) |
c98de08c PB |
8779 | prepare_to_wait(&task->io_uring->wait, &wait, |
8780 | TASK_UNINTERRUPTIBLE); | |
fcb323cc JA |
8781 | spin_unlock_irq(&ctx->inflight_lock); |
8782 | ||
768134d4 | 8783 | /* We need to keep going until we don't find a matching req */ |
f6edbabb | 8784 | if (!found) |
fcb323cc | 8785 | break; |
f6edbabb PB |
8786 | |
8787 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); | |
8788 | io_poll_remove_all(ctx, task, files); | |
8789 | io_kill_timeouts(ctx, task, files); | |
6200b0ae JA |
8790 | /* cancellations _may_ trigger task work */ |
8791 | io_run_task_work(); | |
fcb323cc | 8792 | schedule(); |
c98de08c | 8793 | finish_wait(&task->io_uring->wait, &wait); |
fcb323cc JA |
8794 | } |
8795 | } | |
8796 | ||
b52fda00 PB |
8797 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
8798 | struct task_struct *task) | |
0f212204 | 8799 | { |
b52fda00 | 8800 | while (1) { |
f6edbabb | 8801 | struct io_task_cancel cancel = { .task = task, .files = NULL, }; |
0f212204 | 8802 | enum io_wq_cancel cret; |
b52fda00 | 8803 | bool ret = false; |
0f212204 | 8804 | |
f6edbabb | 8805 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); |
0f212204 JA |
8806 | if (cret != IO_WQ_CANCEL_NOTFOUND) |
8807 | ret = true; | |
8808 | ||
8809 | /* SQPOLL thread does its own polling */ | |
8810 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8811 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8812 | io_iopoll_try_reap_events(ctx); | |
8813 | ret = true; | |
8814 | } | |
8815 | } | |
8816 | ||
6b81928d PB |
8817 | ret |= io_poll_remove_all(ctx, task, NULL); |
8818 | ret |= io_kill_timeouts(ctx, task, NULL); | |
b52fda00 PB |
8819 | if (!ret) |
8820 | break; | |
8821 | io_run_task_work(); | |
8822 | cond_resched(); | |
0f212204 | 8823 | } |
0f212204 JA |
8824 | } |
8825 | ||
8826 | /* | |
8827 | * We need to iteratively cancel requests, in case a request has dependent | |
8828 | * hard links. These persist even for failure of cancelations, hence keep | |
8829 | * looping until none are found. | |
8830 | */ | |
8831 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8832 | struct files_struct *files) | |
8833 | { | |
8834 | struct task_struct *task = current; | |
8835 | ||
fdaf083c | 8836 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
534ca6d6 | 8837 | task = ctx->sq_data->thread; |
fdaf083c JA |
8838 | atomic_inc(&task->io_uring->in_idle); |
8839 | io_sq_thread_park(ctx->sq_data); | |
8840 | } | |
0f212204 | 8841 | |
df9923f9 | 8842 | io_cancel_defer_files(ctx, task, files); |
0f212204 JA |
8843 | io_cqring_overflow_flush(ctx, true, task, files); |
8844 | ||
b52fda00 PB |
8845 | if (!files) |
8846 | __io_uring_cancel_task_requests(ctx, task); | |
bee749b1 PB |
8847 | else |
8848 | io_uring_cancel_files(ctx, task, files); | |
fdaf083c JA |
8849 | |
8850 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
8851 | atomic_dec(&task->io_uring->in_idle); | |
8852 | /* | |
8853 | * If the files that are going away are the ones in the thread | |
8854 | * identity, clear them out. | |
8855 | */ | |
8856 | if (task->io_uring->identity->files == files) | |
8857 | task->io_uring->identity->files = NULL; | |
8858 | io_sq_thread_unpark(ctx->sq_data); | |
8859 | } | |
0f212204 JA |
8860 | } |
8861 | ||
8862 | /* | |
8863 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8864 | */ | |
fdaf083c | 8865 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8866 | { |
236434c3 MWO |
8867 | struct io_uring_task *tctx = current->io_uring; |
8868 | ||
8869 | if (unlikely(!tctx)) { | |
0f212204 JA |
8870 | int ret; |
8871 | ||
8872 | ret = io_uring_alloc_task_context(current); | |
8873 | if (unlikely(ret)) | |
8874 | return ret; | |
236434c3 | 8875 | tctx = current->io_uring; |
0f212204 | 8876 | } |
236434c3 MWO |
8877 | if (tctx->last != file) { |
8878 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8879 | |
236434c3 | 8880 | if (!old) { |
0f212204 | 8881 | get_file(file); |
236434c3 | 8882 | xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL); |
0f212204 | 8883 | } |
236434c3 | 8884 | tctx->last = file; |
0f212204 JA |
8885 | } |
8886 | ||
fdaf083c JA |
8887 | /* |
8888 | * This is race safe in that the task itself is doing this, hence it | |
8889 | * cannot be going through the exit/cancel paths at the same time. | |
8890 | * This cannot be modified while exit/cancel is running. | |
8891 | */ | |
8892 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8893 | tctx->sqpoll = true; | |
8894 | ||
0f212204 JA |
8895 | return 0; |
8896 | } | |
8897 | ||
8898 | /* | |
8899 | * Remove this io_uring_file -> task mapping. | |
8900 | */ | |
8901 | static void io_uring_del_task_file(struct file *file) | |
8902 | { | |
8903 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
8904 | |
8905 | if (tctx->last == file) | |
8906 | tctx->last = NULL; | |
5e2ed8c4 | 8907 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
8908 | if (file) |
8909 | fput(file); | |
8910 | } | |
8911 | ||
0f212204 JA |
8912 | /* |
8913 | * Drop task note for this file if we're the only ones that hold it after | |
8914 | * pending fput() | |
8915 | */ | |
c8fb20b5 | 8916 | static void io_uring_attempt_task_drop(struct file *file) |
0f212204 JA |
8917 | { |
8918 | if (!current->io_uring) | |
8919 | return; | |
8920 | /* | |
8921 | * fput() is pending, will be 2 if the only other ref is our potential | |
8922 | * task file note. If the task is exiting, drop regardless of count. | |
8923 | */ | |
c8fb20b5 PB |
8924 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING) || |
8925 | atomic_long_read(&file->f_count) == 2) | |
8926 | io_uring_del_task_file(file); | |
0f212204 JA |
8927 | } |
8928 | ||
8929 | void __io_uring_files_cancel(struct files_struct *files) | |
8930 | { | |
8931 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
8932 | struct file *file; |
8933 | unsigned long index; | |
0f212204 JA |
8934 | |
8935 | /* make sure overflow events are dropped */ | |
fdaf083c | 8936 | atomic_inc(&tctx->in_idle); |
0f212204 | 8937 | |
ce765372 MWO |
8938 | xa_for_each(&tctx->xa, index, file) { |
8939 | struct io_ring_ctx *ctx = file->private_data; | |
0f212204 JA |
8940 | |
8941 | io_uring_cancel_task_requests(ctx, files); | |
8942 | if (files) | |
8943 | io_uring_del_task_file(file); | |
ce765372 | 8944 | } |
fdaf083c JA |
8945 | |
8946 | atomic_dec(&tctx->in_idle); | |
8947 | } | |
8948 | ||
8949 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8950 | { | |
8951 | unsigned long index; | |
8952 | struct file *file; | |
8953 | s64 inflight; | |
8954 | ||
8955 | inflight = percpu_counter_sum(&tctx->inflight); | |
8956 | if (!tctx->sqpoll) | |
8957 | return inflight; | |
8958 | ||
8959 | /* | |
8960 | * If we have SQPOLL rings, then we need to iterate and find them, and | |
8961 | * add the pending count for those. | |
8962 | */ | |
8963 | xa_for_each(&tctx->xa, index, file) { | |
8964 | struct io_ring_ctx *ctx = file->private_data; | |
8965 | ||
8966 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
8967 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; | |
8968 | ||
8969 | inflight += percpu_counter_sum(&__tctx->inflight); | |
8970 | } | |
8971 | } | |
8972 | ||
8973 | return inflight; | |
0f212204 JA |
8974 | } |
8975 | ||
0f212204 JA |
8976 | /* |
8977 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8978 | * requests. | |
8979 | */ | |
8980 | void __io_uring_task_cancel(void) | |
8981 | { | |
8982 | struct io_uring_task *tctx = current->io_uring; | |
8983 | DEFINE_WAIT(wait); | |
d8a6df10 | 8984 | s64 inflight; |
0f212204 JA |
8985 | |
8986 | /* make sure overflow events are dropped */ | |
fdaf083c | 8987 | atomic_inc(&tctx->in_idle); |
0f212204 | 8988 | |
d8a6df10 | 8989 | do { |
0f212204 | 8990 | /* read completions before cancelations */ |
fdaf083c | 8991 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
8992 | if (!inflight) |
8993 | break; | |
0f212204 JA |
8994 | __io_uring_files_cancel(NULL); |
8995 | ||
8996 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8997 | ||
8998 | /* | |
8999 | * If we've seen completions, retry. This avoids a race where | |
9000 | * a completion comes in before we did prepare_to_wait(). | |
9001 | */ | |
fdaf083c | 9002 | if (inflight != tctx_inflight(tctx)) |
0f212204 | 9003 | continue; |
0f212204 | 9004 | schedule(); |
d8a6df10 | 9005 | } while (1); |
0f212204 JA |
9006 | |
9007 | finish_wait(&tctx->wait, &wait); | |
fdaf083c | 9008 | atomic_dec(&tctx->in_idle); |
44e728b8 PB |
9009 | } |
9010 | ||
fcb323cc JA |
9011 | static int io_uring_flush(struct file *file, void *data) |
9012 | { | |
c8fb20b5 | 9013 | io_uring_attempt_task_drop(file); |
fcb323cc JA |
9014 | return 0; |
9015 | } | |
9016 | ||
6c5c240e RP |
9017 | static void *io_uring_validate_mmap_request(struct file *file, |
9018 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9019 | { |
2b188cc1 | 9020 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9021 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9022 | struct page *page; |
9023 | void *ptr; | |
9024 | ||
9025 | switch (offset) { | |
9026 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9027 | case IORING_OFF_CQ_RING: |
9028 | ptr = ctx->rings; | |
2b188cc1 JA |
9029 | break; |
9030 | case IORING_OFF_SQES: | |
9031 | ptr = ctx->sq_sqes; | |
9032 | break; | |
2b188cc1 | 9033 | default: |
6c5c240e | 9034 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9035 | } |
9036 | ||
9037 | page = virt_to_head_page(ptr); | |
a50b854e | 9038 | if (sz > page_size(page)) |
6c5c240e RP |
9039 | return ERR_PTR(-EINVAL); |
9040 | ||
9041 | return ptr; | |
9042 | } | |
9043 | ||
9044 | #ifdef CONFIG_MMU | |
9045 | ||
9046 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9047 | { | |
9048 | size_t sz = vma->vm_end - vma->vm_start; | |
9049 | unsigned long pfn; | |
9050 | void *ptr; | |
9051 | ||
9052 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9053 | if (IS_ERR(ptr)) | |
9054 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9055 | |
9056 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9057 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9058 | } | |
9059 | ||
6c5c240e RP |
9060 | #else /* !CONFIG_MMU */ |
9061 | ||
9062 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9063 | { | |
9064 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9065 | } | |
9066 | ||
9067 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9068 | { | |
9069 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9070 | } | |
9071 | ||
9072 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9073 | unsigned long addr, unsigned long len, | |
9074 | unsigned long pgoff, unsigned long flags) | |
9075 | { | |
9076 | void *ptr; | |
9077 | ||
9078 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9079 | if (IS_ERR(ptr)) | |
9080 | return PTR_ERR(ptr); | |
9081 | ||
9082 | return (unsigned long) ptr; | |
9083 | } | |
9084 | ||
9085 | #endif /* !CONFIG_MMU */ | |
9086 | ||
90554200 JA |
9087 | static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
9088 | { | |
9089 | DEFINE_WAIT(wait); | |
9090 | ||
9091 | do { | |
9092 | if (!io_sqring_full(ctx)) | |
9093 | break; | |
9094 | ||
9095 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9096 | ||
9097 | if (!io_sqring_full(ctx)) | |
9098 | break; | |
9099 | ||
9100 | schedule(); | |
9101 | } while (!signal_pending(current)); | |
9102 | ||
9103 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
9104 | } | |
9105 | ||
c73ebb68 HX |
9106 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9107 | struct __kernel_timespec __user **ts, | |
9108 | const sigset_t __user **sig) | |
9109 | { | |
9110 | struct io_uring_getevents_arg arg; | |
9111 | ||
9112 | /* | |
9113 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9114 | * is just a pointer to the sigset_t. | |
9115 | */ | |
9116 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9117 | *sig = (const sigset_t __user *) argp; | |
9118 | *ts = NULL; | |
9119 | return 0; | |
9120 | } | |
9121 | ||
9122 | /* | |
9123 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9124 | * timespec and sigset_t pointers if good. | |
9125 | */ | |
9126 | if (*argsz != sizeof(arg)) | |
9127 | return -EINVAL; | |
9128 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9129 | return -EFAULT; | |
9130 | *sig = u64_to_user_ptr(arg.sigmask); | |
9131 | *argsz = arg.sigmask_sz; | |
9132 | *ts = u64_to_user_ptr(arg.ts); | |
9133 | return 0; | |
9134 | } | |
9135 | ||
2b188cc1 | 9136 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9137 | u32, min_complete, u32, flags, const void __user *, argp, |
9138 | size_t, argsz) | |
2b188cc1 JA |
9139 | { |
9140 | struct io_ring_ctx *ctx; | |
9141 | long ret = -EBADF; | |
9142 | int submitted = 0; | |
9143 | struct fd f; | |
9144 | ||
4c6e277c | 9145 | io_run_task_work(); |
b41e9852 | 9146 | |
90554200 | 9147 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9148 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9149 | return -EINVAL; |
9150 | ||
9151 | f = fdget(fd); | |
9152 | if (!f.file) | |
9153 | return -EBADF; | |
9154 | ||
9155 | ret = -EOPNOTSUPP; | |
9156 | if (f.file->f_op != &io_uring_fops) | |
9157 | goto out_fput; | |
9158 | ||
9159 | ret = -ENXIO; | |
9160 | ctx = f.file->private_data; | |
9161 | if (!percpu_ref_tryget(&ctx->refs)) | |
9162 | goto out_fput; | |
9163 | ||
7e84e1c7 SG |
9164 | ret = -EBADFD; |
9165 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9166 | goto out; | |
9167 | ||
6c271ce2 JA |
9168 | /* |
9169 | * For SQ polling, the thread will do all submissions and completions. | |
9170 | * Just return the requested submit count, and wake the thread if | |
9171 | * we were asked to. | |
9172 | */ | |
b2a9eada | 9173 | ret = 0; |
6c271ce2 | 9174 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c1edbf5f | 9175 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
e6c8aa9a | 9176 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6c271ce2 | 9177 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9178 | wake_up(&ctx->sq_data->wait); |
90554200 JA |
9179 | if (flags & IORING_ENTER_SQ_WAIT) |
9180 | io_sqpoll_wait_sq(ctx); | |
6c271ce2 | 9181 | submitted = to_submit; |
b2a9eada | 9182 | } else if (to_submit) { |
fdaf083c | 9183 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9184 | if (unlikely(ret)) |
9185 | goto out; | |
2b188cc1 | 9186 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9187 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9188 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9189 | |
9190 | if (submitted != to_submit) | |
9191 | goto out; | |
2b188cc1 JA |
9192 | } |
9193 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9194 | const sigset_t __user *sig; |
9195 | struct __kernel_timespec __user *ts; | |
9196 | ||
9197 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9198 | if (unlikely(ret)) | |
9199 | goto out; | |
9200 | ||
2b188cc1 JA |
9201 | min_complete = min(min_complete, ctx->cq_entries); |
9202 | ||
32b2244a XW |
9203 | /* |
9204 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9205 | * space applications don't need to do io completion events | |
9206 | * polling again, they can rely on io_sq_thread to do polling | |
9207 | * work, which can reduce cpu usage and uring_lock contention. | |
9208 | */ | |
9209 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9210 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9211 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9212 | } else { |
c73ebb68 | 9213 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9214 | } |
2b188cc1 JA |
9215 | } |
9216 | ||
7c504e65 | 9217 | out: |
6805b32e | 9218 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9219 | out_fput: |
9220 | fdput(f); | |
9221 | return submitted ? submitted : ret; | |
9222 | } | |
9223 | ||
bebdb65e | 9224 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9225 | static int io_uring_show_cred(int id, void *p, void *data) |
9226 | { | |
6b47ab81 JA |
9227 | struct io_identity *iod = p; |
9228 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9229 | struct seq_file *m = data; |
9230 | struct user_namespace *uns = seq_user_ns(m); | |
9231 | struct group_info *gi; | |
9232 | kernel_cap_t cap; | |
9233 | unsigned __capi; | |
9234 | int g; | |
9235 | ||
9236 | seq_printf(m, "%5d\n", id); | |
9237 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9238 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9239 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9240 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9241 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9242 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9243 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9244 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9245 | seq_puts(m, "\n\tGroups:\t"); | |
9246 | gi = cred->group_info; | |
9247 | for (g = 0; g < gi->ngroups; g++) { | |
9248 | seq_put_decimal_ull(m, g ? " " : "", | |
9249 | from_kgid_munged(uns, gi->gid[g])); | |
9250 | } | |
9251 | seq_puts(m, "\n\tCapEff:\t"); | |
9252 | cap = cred->cap_effective; | |
9253 | CAP_FOR_EACH_U32(__capi) | |
9254 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9255 | seq_putc(m, '\n'); | |
9256 | return 0; | |
9257 | } | |
9258 | ||
9259 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9260 | { | |
dbbe9c64 | 9261 | struct io_sq_data *sq = NULL; |
fad8e0de | 9262 | bool has_lock; |
87ce955b JA |
9263 | int i; |
9264 | ||
fad8e0de JA |
9265 | /* |
9266 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9267 | * since fdinfo case grabs it in the opposite direction of normal use | |
9268 | * cases. If we fail to get the lock, we just don't iterate any | |
9269 | * structures that could be going away outside the io_uring mutex. | |
9270 | */ | |
9271 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9272 | ||
dbbe9c64 JQ |
9273 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9274 | sq = ctx->sq_data; | |
9275 | ||
9276 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9277 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9278 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9279 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
87ce955b JA |
9280 | struct fixed_file_table *table; |
9281 | struct file *f; | |
9282 | ||
9283 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
9284 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
9285 | if (f) | |
9286 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9287 | else | |
9288 | seq_printf(m, "%5u: <none>\n", i); | |
9289 | } | |
9290 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9291 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9292 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9293 | ||
9294 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9295 | (unsigned int) buf->len); | |
9296 | } | |
fad8e0de | 9297 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9298 | seq_printf(m, "Personalities:\n"); |
9299 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9300 | } | |
d7718a9d JA |
9301 | seq_printf(m, "PollList:\n"); |
9302 | spin_lock_irq(&ctx->completion_lock); | |
9303 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9304 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9305 | struct io_kiocb *req; | |
9306 | ||
9307 | hlist_for_each_entry(req, list, hash_node) | |
9308 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9309 | req->task->task_works != NULL); | |
9310 | } | |
9311 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9312 | if (has_lock) |
9313 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9314 | } |
9315 | ||
9316 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9317 | { | |
9318 | struct io_ring_ctx *ctx = f->private_data; | |
9319 | ||
9320 | if (percpu_ref_tryget(&ctx->refs)) { | |
9321 | __io_uring_show_fdinfo(ctx, m); | |
9322 | percpu_ref_put(&ctx->refs); | |
9323 | } | |
9324 | } | |
bebdb65e | 9325 | #endif |
87ce955b | 9326 | |
2b188cc1 JA |
9327 | static const struct file_operations io_uring_fops = { |
9328 | .release = io_uring_release, | |
fcb323cc | 9329 | .flush = io_uring_flush, |
2b188cc1 | 9330 | .mmap = io_uring_mmap, |
6c5c240e RP |
9331 | #ifndef CONFIG_MMU |
9332 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9333 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9334 | #endif | |
2b188cc1 JA |
9335 | .poll = io_uring_poll, |
9336 | .fasync = io_uring_fasync, | |
bebdb65e | 9337 | #ifdef CONFIG_PROC_FS |
87ce955b | 9338 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9339 | #endif |
2b188cc1 JA |
9340 | }; |
9341 | ||
9342 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9343 | struct io_uring_params *p) | |
9344 | { | |
75b28aff HV |
9345 | struct io_rings *rings; |
9346 | size_t size, sq_array_offset; | |
2b188cc1 | 9347 | |
bd740481 JA |
9348 | /* make sure these are sane, as we already accounted them */ |
9349 | ctx->sq_entries = p->sq_entries; | |
9350 | ctx->cq_entries = p->cq_entries; | |
9351 | ||
75b28aff HV |
9352 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9353 | if (size == SIZE_MAX) | |
9354 | return -EOVERFLOW; | |
9355 | ||
9356 | rings = io_mem_alloc(size); | |
9357 | if (!rings) | |
2b188cc1 JA |
9358 | return -ENOMEM; |
9359 | ||
75b28aff HV |
9360 | ctx->rings = rings; |
9361 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9362 | rings->sq_ring_mask = p->sq_entries - 1; | |
9363 | rings->cq_ring_mask = p->cq_entries - 1; | |
9364 | rings->sq_ring_entries = p->sq_entries; | |
9365 | rings->cq_ring_entries = p->cq_entries; | |
9366 | ctx->sq_mask = rings->sq_ring_mask; | |
9367 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9368 | |
9369 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9370 | if (size == SIZE_MAX) { |
9371 | io_mem_free(ctx->rings); | |
9372 | ctx->rings = NULL; | |
2b188cc1 | 9373 | return -EOVERFLOW; |
eb065d30 | 9374 | } |
2b188cc1 JA |
9375 | |
9376 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9377 | if (!ctx->sq_sqes) { |
9378 | io_mem_free(ctx->rings); | |
9379 | ctx->rings = NULL; | |
2b188cc1 | 9380 | return -ENOMEM; |
eb065d30 | 9381 | } |
2b188cc1 | 9382 | |
2b188cc1 JA |
9383 | return 0; |
9384 | } | |
9385 | ||
9386 | /* | |
9387 | * Allocate an anonymous fd, this is what constitutes the application | |
9388 | * visible backing of an io_uring instance. The application mmaps this | |
9389 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9390 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9391 | */ | |
9392 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
9393 | { | |
9394 | struct file *file; | |
9395 | int ret; | |
9396 | ||
9397 | #if defined(CONFIG_UNIX) | |
9398 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
9399 | &ctx->ring_sock); | |
9400 | if (ret) | |
9401 | return ret; | |
9402 | #endif | |
9403 | ||
9404 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9405 | if (ret < 0) | |
9406 | goto err; | |
9407 | ||
9408 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
9409 | O_RDWR | O_CLOEXEC); | |
9410 | if (IS_ERR(file)) { | |
0f212204 | 9411 | err_fd: |
2b188cc1 JA |
9412 | put_unused_fd(ret); |
9413 | ret = PTR_ERR(file); | |
9414 | goto err; | |
9415 | } | |
9416 | ||
9417 | #if defined(CONFIG_UNIX) | |
9418 | ctx->ring_sock->file = file; | |
9419 | #endif | |
fdaf083c | 9420 | if (unlikely(io_uring_add_task_file(ctx, file))) { |
0f212204 JA |
9421 | file = ERR_PTR(-ENOMEM); |
9422 | goto err_fd; | |
9423 | } | |
2b188cc1 JA |
9424 | fd_install(ret, file); |
9425 | return ret; | |
9426 | err: | |
9427 | #if defined(CONFIG_UNIX) | |
9428 | sock_release(ctx->ring_sock); | |
9429 | ctx->ring_sock = NULL; | |
9430 | #endif | |
9431 | return ret; | |
9432 | } | |
9433 | ||
7f13657d XW |
9434 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9435 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9436 | { |
9437 | struct user_struct *user = NULL; | |
9438 | struct io_ring_ctx *ctx; | |
aad5d8da | 9439 | bool limit_mem; |
2b188cc1 JA |
9440 | int ret; |
9441 | ||
8110c1a6 | 9442 | if (!entries) |
2b188cc1 | 9443 | return -EINVAL; |
8110c1a6 JA |
9444 | if (entries > IORING_MAX_ENTRIES) { |
9445 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9446 | return -EINVAL; | |
9447 | entries = IORING_MAX_ENTRIES; | |
9448 | } | |
2b188cc1 JA |
9449 | |
9450 | /* | |
9451 | * Use twice as many entries for the CQ ring. It's possible for the | |
9452 | * application to drive a higher depth than the size of the SQ ring, | |
9453 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9454 | * some flexibility in overcommitting a bit. If the application has |
9455 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9456 | * of CQ ring entries manually. | |
2b188cc1 JA |
9457 | */ |
9458 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9459 | if (p->flags & IORING_SETUP_CQSIZE) { |
9460 | /* | |
9461 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9462 | * to a power-of-two, if it isn't already. We do NOT impose | |
9463 | * any cq vs sq ring sizing. | |
9464 | */ | |
88ec3211 | 9465 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
8110c1a6 | 9466 | if (p->cq_entries < p->sq_entries) |
33a107f0 | 9467 | return -EINVAL; |
8110c1a6 JA |
9468 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9469 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9470 | return -EINVAL; | |
9471 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9472 | } | |
33a107f0 JA |
9473 | } else { |
9474 | p->cq_entries = 2 * p->sq_entries; | |
9475 | } | |
2b188cc1 JA |
9476 | |
9477 | user = get_uid(current_user()); | |
aad5d8da | 9478 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9479 | |
aad5d8da | 9480 | if (limit_mem) { |
a087e2b5 | 9481 | ret = __io_account_mem(user, |
2b188cc1 JA |
9482 | ring_pages(p->sq_entries, p->cq_entries)); |
9483 | if (ret) { | |
9484 | free_uid(user); | |
9485 | return ret; | |
9486 | } | |
9487 | } | |
9488 | ||
9489 | ctx = io_ring_ctx_alloc(p); | |
9490 | if (!ctx) { | |
aad5d8da | 9491 | if (limit_mem) |
a087e2b5 | 9492 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9493 | p->cq_entries)); |
9494 | free_uid(user); | |
9495 | return -ENOMEM; | |
9496 | } | |
9497 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9498 | ctx->user = user; |
0b8c0ec7 | 9499 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9500 | #ifdef CONFIG_AUDIT |
9501 | ctx->loginuid = current->loginuid; | |
9502 | ctx->sessionid = current->sessionid; | |
9503 | #endif | |
2aede0e4 JA |
9504 | ctx->sqo_task = get_task_struct(current); |
9505 | ||
9506 | /* | |
9507 | * This is just grabbed for accounting purposes. When a process exits, | |
9508 | * the mm is exited and dropped before the files, hence we need to hang | |
9509 | * on to this mm purely for the purposes of being able to unaccount | |
9510 | * memory (locked/pinned vm). It's not used for anything else. | |
9511 | */ | |
6b7898eb | 9512 | mmgrab(current->mm); |
2aede0e4 | 9513 | ctx->mm_account = current->mm; |
6b7898eb | 9514 | |
91d8f519 DZ |
9515 | #ifdef CONFIG_BLK_CGROUP |
9516 | /* | |
9517 | * The sq thread will belong to the original cgroup it was inited in. | |
9518 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9519 | * issued bios will be associated with the closest cgroup later in the | |
9520 | * block layer. | |
9521 | */ | |
9522 | rcu_read_lock(); | |
9523 | ctx->sqo_blkcg_css = blkcg_css(); | |
9524 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9525 | rcu_read_unlock(); | |
9526 | if (!ret) { | |
9527 | /* don't init against a dying cgroup, have the user try again */ | |
9528 | ctx->sqo_blkcg_css = NULL; | |
9529 | ret = -ENODEV; | |
9530 | goto err; | |
9531 | } | |
9532 | #endif | |
6b7898eb | 9533 | |
f74441e6 JA |
9534 | /* |
9535 | * Account memory _before_ installing the file descriptor. Once | |
9536 | * the descriptor is installed, it can get closed at any time. Also | |
9537 | * do this before hitting the general error path, as ring freeing | |
9538 | * will un-account as well. | |
9539 | */ | |
9540 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9541 | ACCT_LOCKED); | |
9542 | ctx->limit_mem = limit_mem; | |
9543 | ||
2b188cc1 JA |
9544 | ret = io_allocate_scq_urings(ctx, p); |
9545 | if (ret) | |
9546 | goto err; | |
9547 | ||
7e84e1c7 | 9548 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9549 | if (ret) |
9550 | goto err; | |
9551 | ||
7e84e1c7 SG |
9552 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9553 | io_sq_offload_start(ctx); | |
9554 | ||
2b188cc1 | 9555 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9556 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9557 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9558 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9559 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9560 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9561 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9562 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9563 | |
9564 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9565 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9566 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9567 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9568 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9569 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9570 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9571 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9572 | |
7f13657d XW |
9573 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9574 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9575 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9576 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9577 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9578 | |
9579 | if (copy_to_user(params, p, sizeof(*p))) { | |
9580 | ret = -EFAULT; | |
9581 | goto err; | |
9582 | } | |
d1719f70 | 9583 | |
044c1ab3 JA |
9584 | /* |
9585 | * Install ring fd as the very last thing, so we don't risk someone | |
9586 | * having closed it before we finish setup | |
9587 | */ | |
9588 | ret = io_uring_get_fd(ctx); | |
9589 | if (ret < 0) | |
9590 | goto err; | |
9591 | ||
c826bd7a | 9592 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9593 | return ret; |
9594 | err: | |
9595 | io_ring_ctx_wait_and_kill(ctx); | |
9596 | return ret; | |
9597 | } | |
9598 | ||
9599 | /* | |
9600 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9601 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9602 | * params structure passed in. | |
9603 | */ | |
9604 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9605 | { | |
9606 | struct io_uring_params p; | |
2b188cc1 JA |
9607 | int i; |
9608 | ||
9609 | if (copy_from_user(&p, params, sizeof(p))) | |
9610 | return -EFAULT; | |
9611 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9612 | if (p.resv[i]) | |
9613 | return -EINVAL; | |
9614 | } | |
9615 | ||
6c271ce2 | 9616 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9617 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9618 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9619 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9620 | return -EINVAL; |
9621 | ||
7f13657d | 9622 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9623 | } |
9624 | ||
9625 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9626 | struct io_uring_params __user *, params) | |
9627 | { | |
9628 | return io_uring_setup(entries, params); | |
9629 | } | |
9630 | ||
66f4af93 JA |
9631 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9632 | { | |
9633 | struct io_uring_probe *p; | |
9634 | size_t size; | |
9635 | int i, ret; | |
9636 | ||
9637 | size = struct_size(p, ops, nr_args); | |
9638 | if (size == SIZE_MAX) | |
9639 | return -EOVERFLOW; | |
9640 | p = kzalloc(size, GFP_KERNEL); | |
9641 | if (!p) | |
9642 | return -ENOMEM; | |
9643 | ||
9644 | ret = -EFAULT; | |
9645 | if (copy_from_user(p, arg, size)) | |
9646 | goto out; | |
9647 | ret = -EINVAL; | |
9648 | if (memchr_inv(p, 0, size)) | |
9649 | goto out; | |
9650 | ||
9651 | p->last_op = IORING_OP_LAST - 1; | |
9652 | if (nr_args > IORING_OP_LAST) | |
9653 | nr_args = IORING_OP_LAST; | |
9654 | ||
9655 | for (i = 0; i < nr_args; i++) { | |
9656 | p->ops[i].op = i; | |
9657 | if (!io_op_defs[i].not_supported) | |
9658 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9659 | } | |
9660 | p->ops_len = i; | |
9661 | ||
9662 | ret = 0; | |
9663 | if (copy_to_user(arg, p, size)) | |
9664 | ret = -EFAULT; | |
9665 | out: | |
9666 | kfree(p); | |
9667 | return ret; | |
9668 | } | |
9669 | ||
071698e1 JA |
9670 | static int io_register_personality(struct io_ring_ctx *ctx) |
9671 | { | |
1e6fa521 JA |
9672 | struct io_identity *id; |
9673 | int ret; | |
071698e1 | 9674 | |
1e6fa521 JA |
9675 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9676 | if (unlikely(!id)) | |
9677 | return -ENOMEM; | |
9678 | ||
9679 | io_init_identity(id); | |
9680 | id->creds = get_current_cred(); | |
9681 | ||
9682 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9683 | if (ret < 0) { | |
9684 | put_cred(id->creds); | |
9685 | kfree(id); | |
9686 | } | |
9687 | return ret; | |
071698e1 JA |
9688 | } |
9689 | ||
9690 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
9691 | { | |
1e6fa521 | 9692 | struct io_identity *iod; |
071698e1 | 9693 | |
1e6fa521 JA |
9694 | iod = idr_remove(&ctx->personality_idr, id); |
9695 | if (iod) { | |
9696 | put_cred(iod->creds); | |
9697 | if (refcount_dec_and_test(&iod->count)) | |
9698 | kfree(iod); | |
071698e1 JA |
9699 | return 0; |
9700 | } | |
9701 | ||
9702 | return -EINVAL; | |
9703 | } | |
9704 | ||
21b55dbc SG |
9705 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9706 | unsigned int nr_args) | |
9707 | { | |
9708 | struct io_uring_restriction *res; | |
9709 | size_t size; | |
9710 | int i, ret; | |
9711 | ||
7e84e1c7 SG |
9712 | /* Restrictions allowed only if rings started disabled */ |
9713 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9714 | return -EBADFD; | |
9715 | ||
21b55dbc | 9716 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9717 | if (ctx->restrictions.registered) |
21b55dbc SG |
9718 | return -EBUSY; |
9719 | ||
9720 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9721 | return -EINVAL; | |
9722 | ||
9723 | size = array_size(nr_args, sizeof(*res)); | |
9724 | if (size == SIZE_MAX) | |
9725 | return -EOVERFLOW; | |
9726 | ||
9727 | res = memdup_user(arg, size); | |
9728 | if (IS_ERR(res)) | |
9729 | return PTR_ERR(res); | |
9730 | ||
9731 | ret = 0; | |
9732 | ||
9733 | for (i = 0; i < nr_args; i++) { | |
9734 | switch (res[i].opcode) { | |
9735 | case IORING_RESTRICTION_REGISTER_OP: | |
9736 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9737 | ret = -EINVAL; | |
9738 | goto out; | |
9739 | } | |
9740 | ||
9741 | __set_bit(res[i].register_op, | |
9742 | ctx->restrictions.register_op); | |
9743 | break; | |
9744 | case IORING_RESTRICTION_SQE_OP: | |
9745 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9746 | ret = -EINVAL; | |
9747 | goto out; | |
9748 | } | |
9749 | ||
9750 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9751 | break; | |
9752 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9753 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9754 | break; | |
9755 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9756 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9757 | break; | |
9758 | default: | |
9759 | ret = -EINVAL; | |
9760 | goto out; | |
9761 | } | |
9762 | } | |
9763 | ||
9764 | out: | |
9765 | /* Reset all restrictions if an error happened */ | |
9766 | if (ret != 0) | |
9767 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9768 | else | |
7e84e1c7 | 9769 | ctx->restrictions.registered = true; |
21b55dbc SG |
9770 | |
9771 | kfree(res); | |
9772 | return ret; | |
9773 | } | |
9774 | ||
7e84e1c7 SG |
9775 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9776 | { | |
9777 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9778 | return -EBADFD; | |
9779 | ||
9780 | if (ctx->restrictions.registered) | |
9781 | ctx->restricted = 1; | |
9782 | ||
9783 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9784 | ||
9785 | io_sq_offload_start(ctx); | |
9786 | ||
9787 | return 0; | |
9788 | } | |
9789 | ||
071698e1 JA |
9790 | static bool io_register_op_must_quiesce(int op) |
9791 | { | |
9792 | switch (op) { | |
9793 | case IORING_UNREGISTER_FILES: | |
9794 | case IORING_REGISTER_FILES_UPDATE: | |
9795 | case IORING_REGISTER_PROBE: | |
9796 | case IORING_REGISTER_PERSONALITY: | |
9797 | case IORING_UNREGISTER_PERSONALITY: | |
9798 | return false; | |
9799 | default: | |
9800 | return true; | |
9801 | } | |
9802 | } | |
9803 | ||
edafccee JA |
9804 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9805 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9806 | __releases(ctx->uring_lock) |
9807 | __acquires(ctx->uring_lock) | |
edafccee JA |
9808 | { |
9809 | int ret; | |
9810 | ||
35fa71a0 JA |
9811 | /* |
9812 | * We're inside the ring mutex, if the ref is already dying, then | |
9813 | * someone else killed the ctx or is already going through | |
9814 | * io_uring_register(). | |
9815 | */ | |
9816 | if (percpu_ref_is_dying(&ctx->refs)) | |
9817 | return -ENXIO; | |
9818 | ||
071698e1 | 9819 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9820 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9821 | |
05f3fb3c JA |
9822 | /* |
9823 | * Drop uring mutex before waiting for references to exit. If | |
9824 | * another thread is currently inside io_uring_enter() it might | |
9825 | * need to grab the uring_lock to make progress. If we hold it | |
9826 | * here across the drain wait, then we can deadlock. It's safe | |
9827 | * to drop the mutex here, since no new references will come in | |
9828 | * after we've killed the percpu ref. | |
9829 | */ | |
9830 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9831 | do { |
9832 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9833 | if (!ret) | |
9834 | break; | |
ed6930c9 JA |
9835 | ret = io_run_task_work_sig(); |
9836 | if (ret < 0) | |
9837 | break; | |
af9c1a44 JA |
9838 | } while (1); |
9839 | ||
05f3fb3c | 9840 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9841 | |
c150368b JA |
9842 | if (ret) { |
9843 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
9844 | goto out_quiesce; |
9845 | } | |
9846 | } | |
9847 | ||
9848 | if (ctx->restricted) { | |
9849 | if (opcode >= IORING_REGISTER_LAST) { | |
9850 | ret = -EINVAL; | |
9851 | goto out; | |
9852 | } | |
9853 | ||
9854 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9855 | ret = -EACCES; | |
c150368b JA |
9856 | goto out; |
9857 | } | |
05f3fb3c | 9858 | } |
edafccee JA |
9859 | |
9860 | switch (opcode) { | |
9861 | case IORING_REGISTER_BUFFERS: | |
9862 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
9863 | break; | |
9864 | case IORING_UNREGISTER_BUFFERS: | |
9865 | ret = -EINVAL; | |
9866 | if (arg || nr_args) | |
9867 | break; | |
9868 | ret = io_sqe_buffer_unregister(ctx); | |
9869 | break; | |
6b06314c JA |
9870 | case IORING_REGISTER_FILES: |
9871 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9872 | break; | |
9873 | case IORING_UNREGISTER_FILES: | |
9874 | ret = -EINVAL; | |
9875 | if (arg || nr_args) | |
9876 | break; | |
9877 | ret = io_sqe_files_unregister(ctx); | |
9878 | break; | |
c3a31e60 JA |
9879 | case IORING_REGISTER_FILES_UPDATE: |
9880 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9881 | break; | |
9b402849 | 9882 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9883 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9884 | ret = -EINVAL; |
9885 | if (nr_args != 1) | |
9886 | break; | |
9887 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9888 | if (ret) |
9889 | break; | |
9890 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9891 | ctx->eventfd_async = 1; | |
9892 | else | |
9893 | ctx->eventfd_async = 0; | |
9b402849 JA |
9894 | break; |
9895 | case IORING_UNREGISTER_EVENTFD: | |
9896 | ret = -EINVAL; | |
9897 | if (arg || nr_args) | |
9898 | break; | |
9899 | ret = io_eventfd_unregister(ctx); | |
9900 | break; | |
66f4af93 JA |
9901 | case IORING_REGISTER_PROBE: |
9902 | ret = -EINVAL; | |
9903 | if (!arg || nr_args > 256) | |
9904 | break; | |
9905 | ret = io_probe(ctx, arg, nr_args); | |
9906 | break; | |
071698e1 JA |
9907 | case IORING_REGISTER_PERSONALITY: |
9908 | ret = -EINVAL; | |
9909 | if (arg || nr_args) | |
9910 | break; | |
9911 | ret = io_register_personality(ctx); | |
9912 | break; | |
9913 | case IORING_UNREGISTER_PERSONALITY: | |
9914 | ret = -EINVAL; | |
9915 | if (arg) | |
9916 | break; | |
9917 | ret = io_unregister_personality(ctx, nr_args); | |
9918 | break; | |
7e84e1c7 SG |
9919 | case IORING_REGISTER_ENABLE_RINGS: |
9920 | ret = -EINVAL; | |
9921 | if (arg || nr_args) | |
9922 | break; | |
9923 | ret = io_register_enable_rings(ctx); | |
9924 | break; | |
21b55dbc SG |
9925 | case IORING_REGISTER_RESTRICTIONS: |
9926 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9927 | break; | |
edafccee JA |
9928 | default: |
9929 | ret = -EINVAL; | |
9930 | break; | |
9931 | } | |
9932 | ||
21b55dbc | 9933 | out: |
071698e1 | 9934 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9935 | /* bring the ctx back to life */ |
05f3fb3c | 9936 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 9937 | out_quiesce: |
0f158b4c | 9938 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9939 | } |
edafccee JA |
9940 | return ret; |
9941 | } | |
9942 | ||
9943 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9944 | void __user *, arg, unsigned int, nr_args) | |
9945 | { | |
9946 | struct io_ring_ctx *ctx; | |
9947 | long ret = -EBADF; | |
9948 | struct fd f; | |
9949 | ||
9950 | f = fdget(fd); | |
9951 | if (!f.file) | |
9952 | return -EBADF; | |
9953 | ||
9954 | ret = -EOPNOTSUPP; | |
9955 | if (f.file->f_op != &io_uring_fops) | |
9956 | goto out_fput; | |
9957 | ||
9958 | ctx = f.file->private_data; | |
9959 | ||
9960 | mutex_lock(&ctx->uring_lock); | |
9961 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9962 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9963 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9964 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9965 | out_fput: |
9966 | fdput(f); | |
9967 | return ret; | |
9968 | } | |
9969 | ||
2b188cc1 JA |
9970 | static int __init io_uring_init(void) |
9971 | { | |
d7f62e82 SM |
9972 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9973 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9974 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9975 | } while (0) | |
9976 | ||
9977 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9978 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9979 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9980 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9981 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9982 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9983 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9984 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9985 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9986 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9987 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9988 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9989 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9990 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9991 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9992 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9993 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9994 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9995 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9996 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9997 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9998 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9999 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10000 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10001 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10002 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10003 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10004 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10005 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10006 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10007 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10008 | |
d3656344 | 10009 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10010 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
10011 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
10012 | return 0; | |
10013 | }; | |
10014 | __initcall(io_uring_init); |