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 | ||
50238531 BM |
198 | struct io_ring_ctx; |
199 | ||
269bbe5f BM |
200 | struct io_rsrc_put { |
201 | struct list_head list; | |
50238531 BM |
202 | union { |
203 | void *rsrc; | |
204 | struct file *file; | |
205 | }; | |
269bbe5f BM |
206 | }; |
207 | ||
208 | struct fixed_rsrc_table { | |
65e19f54 | 209 | struct file **files; |
31b51510 JA |
210 | }; |
211 | ||
269bbe5f | 212 | struct fixed_rsrc_ref_node { |
05589553 XW |
213 | struct percpu_ref refs; |
214 | struct list_head node; | |
269bbe5f BM |
215 | struct list_head rsrc_list; |
216 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
217 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
218 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 219 | struct llist_node llist; |
e297822b | 220 | bool done; |
05589553 XW |
221 | }; |
222 | ||
269bbe5f BM |
223 | struct fixed_rsrc_data { |
224 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
225 | struct io_ring_ctx *ctx; |
226 | ||
269bbe5f | 227 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 228 | struct percpu_ref refs; |
05f3fb3c JA |
229 | struct completion done; |
230 | }; | |
231 | ||
5a2e745d JA |
232 | struct io_buffer { |
233 | struct list_head list; | |
234 | __u64 addr; | |
235 | __s32 len; | |
236 | __u16 bid; | |
237 | }; | |
238 | ||
21b55dbc SG |
239 | struct io_restriction { |
240 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
241 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
242 | u8 sqe_flags_allowed; | |
243 | u8 sqe_flags_required; | |
7e84e1c7 | 244 | bool registered; |
21b55dbc SG |
245 | }; |
246 | ||
534ca6d6 JA |
247 | struct io_sq_data { |
248 | refcount_t refs; | |
69fb2131 JA |
249 | struct mutex lock; |
250 | ||
251 | /* ctx's that are using this sqd */ | |
252 | struct list_head ctx_list; | |
253 | struct list_head ctx_new_list; | |
254 | struct mutex ctx_lock; | |
255 | ||
534ca6d6 JA |
256 | struct task_struct *thread; |
257 | struct wait_queue_head wait; | |
08369246 XW |
258 | |
259 | unsigned sq_thread_idle; | |
534ca6d6 JA |
260 | }; |
261 | ||
2b188cc1 JA |
262 | struct io_ring_ctx { |
263 | struct { | |
264 | struct percpu_ref refs; | |
265 | } ____cacheline_aligned_in_smp; | |
266 | ||
267 | struct { | |
268 | unsigned int flags; | |
e1d85334 | 269 | unsigned int compat: 1; |
aad5d8da | 270 | unsigned int limit_mem: 1; |
e1d85334 RD |
271 | unsigned int cq_overflow_flushed: 1; |
272 | unsigned int drain_next: 1; | |
273 | unsigned int eventfd_async: 1; | |
21b55dbc | 274 | unsigned int restricted: 1; |
d9d05217 | 275 | unsigned int sqo_dead: 1; |
2b188cc1 | 276 | |
75b28aff HV |
277 | /* |
278 | * Ring buffer of indices into array of io_uring_sqe, which is | |
279 | * mmapped by the application using the IORING_OFF_SQES offset. | |
280 | * | |
281 | * This indirection could e.g. be used to assign fixed | |
282 | * io_uring_sqe entries to operations and only submit them to | |
283 | * the queue when needed. | |
284 | * | |
285 | * The kernel modifies neither the indices array nor the entries | |
286 | * array. | |
287 | */ | |
288 | u32 *sq_array; | |
2b188cc1 JA |
289 | unsigned cached_sq_head; |
290 | unsigned sq_entries; | |
291 | unsigned sq_mask; | |
6c271ce2 | 292 | unsigned sq_thread_idle; |
498ccd9e | 293 | unsigned cached_sq_dropped; |
2c3bac6d | 294 | unsigned cached_cq_overflow; |
ad3eb2c8 | 295 | unsigned long sq_check_overflow; |
de0617e4 JA |
296 | |
297 | struct list_head defer_list; | |
5262f567 | 298 | struct list_head timeout_list; |
1d7bb1d5 | 299 | struct list_head cq_overflow_list; |
fcb323cc | 300 | |
ad3eb2c8 | 301 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
302 | } ____cacheline_aligned_in_smp; |
303 | ||
206aefde JA |
304 | struct io_rings *rings; |
305 | ||
2b188cc1 | 306 | /* IO offload */ |
561fb04a | 307 | struct io_wq *io_wq; |
2aede0e4 JA |
308 | |
309 | /* | |
310 | * For SQPOLL usage - we hold a reference to the parent task, so we | |
311 | * have access to the ->files | |
312 | */ | |
313 | struct task_struct *sqo_task; | |
314 | ||
315 | /* Only used for accounting purposes */ | |
316 | struct mm_struct *mm_account; | |
317 | ||
91d8f519 DZ |
318 | #ifdef CONFIG_BLK_CGROUP |
319 | struct cgroup_subsys_state *sqo_blkcg_css; | |
320 | #endif | |
321 | ||
534ca6d6 JA |
322 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
323 | ||
90554200 | 324 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 325 | struct list_head sqd_list; |
75b28aff | 326 | |
6b06314c JA |
327 | /* |
328 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
329 | * readers must ensure that ->refs is alive as long as the file* is | |
330 | * used. Only updated through io_uring_register(2). | |
331 | */ | |
269bbe5f | 332 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
333 | unsigned nr_user_files; |
334 | ||
edafccee JA |
335 | /* if used, fixed mapped user buffers */ |
336 | unsigned nr_user_bufs; | |
337 | struct io_mapped_ubuf *user_bufs; | |
338 | ||
2b188cc1 JA |
339 | struct user_struct *user; |
340 | ||
0b8c0ec7 | 341 | const struct cred *creds; |
181e448d | 342 | |
4ea33a97 JA |
343 | #ifdef CONFIG_AUDIT |
344 | kuid_t loginuid; | |
345 | unsigned int sessionid; | |
346 | #endif | |
347 | ||
0f158b4c JA |
348 | struct completion ref_comp; |
349 | struct completion sq_thread_comp; | |
206aefde | 350 | |
0ddf92e8 JA |
351 | /* if all else fails... */ |
352 | struct io_kiocb *fallback_req; | |
353 | ||
206aefde JA |
354 | #if defined(CONFIG_UNIX) |
355 | struct socket *ring_sock; | |
356 | #endif | |
357 | ||
5a2e745d JA |
358 | struct idr io_buffer_idr; |
359 | ||
071698e1 JA |
360 | struct idr personality_idr; |
361 | ||
206aefde JA |
362 | struct { |
363 | unsigned cached_cq_tail; | |
364 | unsigned cq_entries; | |
365 | unsigned cq_mask; | |
366 | atomic_t cq_timeouts; | |
f010505b | 367 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 368 | unsigned long cq_check_overflow; |
206aefde JA |
369 | struct wait_queue_head cq_wait; |
370 | struct fasync_struct *cq_fasync; | |
371 | struct eventfd_ctx *cq_ev_fd; | |
372 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
373 | |
374 | struct { | |
375 | struct mutex uring_lock; | |
376 | wait_queue_head_t wait; | |
377 | } ____cacheline_aligned_in_smp; | |
378 | ||
379 | struct { | |
380 | spinlock_t completion_lock; | |
e94f141b | 381 | |
def596e9 | 382 | /* |
540e32a0 | 383 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
384 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
385 | * For SQPOLL, only the single threaded io_sq_thread() will | |
386 | * manipulate the list, hence no extra locking is needed there. | |
387 | */ | |
540e32a0 | 388 | struct list_head iopoll_list; |
78076bb6 JA |
389 | struct hlist_head *cancel_hash; |
390 | unsigned cancel_hash_bits; | |
e94f141b | 391 | bool poll_multi_file; |
31b51510 | 392 | |
fcb323cc JA |
393 | spinlock_t inflight_lock; |
394 | struct list_head inflight_list; | |
2b188cc1 | 395 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 396 | |
269bbe5f BM |
397 | struct delayed_work rsrc_put_work; |
398 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
399 | struct list_head rsrc_ref_list; |
400 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 401 | |
85faa7b8 | 402 | struct work_struct exit_work; |
21b55dbc | 403 | struct io_restriction restrictions; |
2b188cc1 JA |
404 | }; |
405 | ||
09bb8394 JA |
406 | /* |
407 | * First field must be the file pointer in all the | |
408 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
409 | */ | |
221c5eb2 JA |
410 | struct io_poll_iocb { |
411 | struct file *file; | |
018043be | 412 | struct wait_queue_head *head; |
221c5eb2 | 413 | __poll_t events; |
8c838788 | 414 | bool done; |
221c5eb2 | 415 | bool canceled; |
392edb45 | 416 | struct wait_queue_entry wait; |
221c5eb2 JA |
417 | }; |
418 | ||
018043be PB |
419 | struct io_poll_remove { |
420 | struct file *file; | |
421 | u64 addr; | |
422 | }; | |
423 | ||
b5dba59e JA |
424 | struct io_close { |
425 | struct file *file; | |
426 | struct file *put_file; | |
427 | int fd; | |
428 | }; | |
429 | ||
ad8a48ac JA |
430 | struct io_timeout_data { |
431 | struct io_kiocb *req; | |
432 | struct hrtimer timer; | |
433 | struct timespec64 ts; | |
434 | enum hrtimer_mode mode; | |
435 | }; | |
436 | ||
8ed8d3c3 JA |
437 | struct io_accept { |
438 | struct file *file; | |
439 | struct sockaddr __user *addr; | |
440 | int __user *addr_len; | |
441 | int flags; | |
09952e3e | 442 | unsigned long nofile; |
8ed8d3c3 JA |
443 | }; |
444 | ||
445 | struct io_sync { | |
446 | struct file *file; | |
447 | loff_t len; | |
448 | loff_t off; | |
449 | int flags; | |
d63d1b5e | 450 | int mode; |
8ed8d3c3 JA |
451 | }; |
452 | ||
fbf23849 JA |
453 | struct io_cancel { |
454 | struct file *file; | |
455 | u64 addr; | |
456 | }; | |
457 | ||
b29472ee JA |
458 | struct io_timeout { |
459 | struct file *file; | |
bfe68a22 PB |
460 | u32 off; |
461 | u32 target_seq; | |
135fcde8 | 462 | struct list_head list; |
90cd7e42 PB |
463 | /* head of the link, used by linked timeouts only */ |
464 | struct io_kiocb *head; | |
b29472ee JA |
465 | }; |
466 | ||
0bdf7a2d PB |
467 | struct io_timeout_rem { |
468 | struct file *file; | |
469 | u64 addr; | |
9c8e11b3 PB |
470 | |
471 | /* timeout update */ | |
472 | struct timespec64 ts; | |
473 | u32 flags; | |
0bdf7a2d PB |
474 | }; |
475 | ||
9adbd45d JA |
476 | struct io_rw { |
477 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
478 | struct kiocb kiocb; | |
479 | u64 addr; | |
480 | u64 len; | |
481 | }; | |
482 | ||
3fbb51c1 JA |
483 | struct io_connect { |
484 | struct file *file; | |
485 | struct sockaddr __user *addr; | |
486 | int addr_len; | |
487 | }; | |
488 | ||
e47293fd JA |
489 | struct io_sr_msg { |
490 | struct file *file; | |
fddaface | 491 | union { |
270a5940 | 492 | struct user_msghdr __user *umsg; |
fddaface JA |
493 | void __user *buf; |
494 | }; | |
e47293fd | 495 | int msg_flags; |
bcda7baa | 496 | int bgid; |
fddaface | 497 | size_t len; |
bcda7baa | 498 | struct io_buffer *kbuf; |
e47293fd JA |
499 | }; |
500 | ||
15b71abe JA |
501 | struct io_open { |
502 | struct file *file; | |
503 | int dfd; | |
15b71abe | 504 | struct filename *filename; |
c12cedf2 | 505 | struct open_how how; |
4022e7af | 506 | unsigned long nofile; |
15b71abe JA |
507 | }; |
508 | ||
269bbe5f | 509 | struct io_rsrc_update { |
05f3fb3c JA |
510 | struct file *file; |
511 | u64 arg; | |
512 | u32 nr_args; | |
513 | u32 offset; | |
514 | }; | |
515 | ||
4840e418 JA |
516 | struct io_fadvise { |
517 | struct file *file; | |
518 | u64 offset; | |
519 | u32 len; | |
520 | u32 advice; | |
521 | }; | |
522 | ||
c1ca757b JA |
523 | struct io_madvise { |
524 | struct file *file; | |
525 | u64 addr; | |
526 | u32 len; | |
527 | u32 advice; | |
528 | }; | |
529 | ||
3e4827b0 JA |
530 | struct io_epoll { |
531 | struct file *file; | |
532 | int epfd; | |
533 | int op; | |
534 | int fd; | |
535 | struct epoll_event event; | |
e47293fd JA |
536 | }; |
537 | ||
7d67af2c PB |
538 | struct io_splice { |
539 | struct file *file_out; | |
540 | struct file *file_in; | |
541 | loff_t off_out; | |
542 | loff_t off_in; | |
543 | u64 len; | |
544 | unsigned int flags; | |
545 | }; | |
546 | ||
ddf0322d JA |
547 | struct io_provide_buf { |
548 | struct file *file; | |
549 | __u64 addr; | |
550 | __s32 len; | |
551 | __u32 bgid; | |
552 | __u16 nbufs; | |
553 | __u16 bid; | |
554 | }; | |
555 | ||
1d9e1288 BM |
556 | struct io_statx { |
557 | struct file *file; | |
558 | int dfd; | |
559 | unsigned int mask; | |
560 | unsigned int flags; | |
e62753e4 | 561 | const char __user *filename; |
1d9e1288 BM |
562 | struct statx __user *buffer; |
563 | }; | |
564 | ||
36f4fa68 JA |
565 | struct io_shutdown { |
566 | struct file *file; | |
567 | int how; | |
568 | }; | |
569 | ||
80a261fd JA |
570 | struct io_rename { |
571 | struct file *file; | |
572 | int old_dfd; | |
573 | int new_dfd; | |
574 | struct filename *oldpath; | |
575 | struct filename *newpath; | |
576 | int flags; | |
577 | }; | |
578 | ||
14a1143b JA |
579 | struct io_unlink { |
580 | struct file *file; | |
581 | int dfd; | |
582 | int flags; | |
583 | struct filename *filename; | |
584 | }; | |
585 | ||
3ca405eb PB |
586 | struct io_completion { |
587 | struct file *file; | |
588 | struct list_head list; | |
0f7e466b | 589 | int cflags; |
3ca405eb PB |
590 | }; |
591 | ||
f499a021 JA |
592 | struct io_async_connect { |
593 | struct sockaddr_storage address; | |
594 | }; | |
595 | ||
03b1230c JA |
596 | struct io_async_msghdr { |
597 | struct iovec fast_iov[UIO_FASTIOV]; | |
598 | struct iovec *iov; | |
599 | struct sockaddr __user *uaddr; | |
600 | struct msghdr msg; | |
b537916c | 601 | struct sockaddr_storage addr; |
03b1230c JA |
602 | }; |
603 | ||
f67676d1 JA |
604 | struct io_async_rw { |
605 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
606 | const struct iovec *free_iovec; |
607 | struct iov_iter iter; | |
227c0c96 | 608 | size_t bytes_done; |
bcf5a063 | 609 | struct wait_page_queue wpq; |
f67676d1 JA |
610 | }; |
611 | ||
6b47ee6e PB |
612 | enum { |
613 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
614 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
615 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
616 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
617 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 618 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 619 | |
6b47ee6e PB |
620 | REQ_F_FAIL_LINK_BIT, |
621 | REQ_F_INFLIGHT_BIT, | |
622 | REQ_F_CUR_POS_BIT, | |
623 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 624 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 625 | REQ_F_ISREG_BIT, |
99bc4c38 | 626 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 627 | REQ_F_POLLED_BIT, |
bcda7baa | 628 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 629 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 630 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 631 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
84557871 JA |
632 | |
633 | /* not a real bit, just to check we're not overflowing the space */ | |
634 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
635 | }; |
636 | ||
637 | enum { | |
638 | /* ctx owns file */ | |
639 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
640 | /* drain existing IO first */ | |
641 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
642 | /* linked sqes */ | |
643 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
644 | /* doesn't sever on completion < 0 */ | |
645 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
646 | /* IOSQE_ASYNC */ | |
647 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
648 | /* IOSQE_BUFFER_SELECT */ |
649 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 650 | |
6b47ee6e PB |
651 | /* fail rest of links */ |
652 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
653 | /* on inflight list */ | |
654 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
655 | /* read/write uses file position */ | |
656 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
657 | /* must not punt to workers */ | |
658 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 659 | /* has or had linked timeout */ |
6b47ee6e | 660 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
661 | /* regular file */ |
662 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
663 | /* needs cleanup */ |
664 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
665 | /* already went through poll handler */ |
666 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
667 | /* buffer already selected */ |
668 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
669 | /* doesn't need file table for this request */ |
670 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
671 | /* io_wq_work is initialized */ |
672 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
673 | /* linked timeout is active, i.e. prepared by link's head */ |
674 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
d7718a9d JA |
675 | }; |
676 | ||
677 | struct async_poll { | |
678 | struct io_poll_iocb poll; | |
807abcb0 | 679 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
680 | }; |
681 | ||
09bb8394 JA |
682 | /* |
683 | * NOTE! Each of the iocb union members has the file pointer | |
684 | * as the first entry in their struct definition. So you can | |
685 | * access the file pointer through any of the sub-structs, | |
686 | * or directly as just 'ki_filp' in this struct. | |
687 | */ | |
2b188cc1 | 688 | struct io_kiocb { |
221c5eb2 | 689 | union { |
09bb8394 | 690 | struct file *file; |
9adbd45d | 691 | struct io_rw rw; |
221c5eb2 | 692 | struct io_poll_iocb poll; |
018043be | 693 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
694 | struct io_accept accept; |
695 | struct io_sync sync; | |
fbf23849 | 696 | struct io_cancel cancel; |
b29472ee | 697 | struct io_timeout timeout; |
0bdf7a2d | 698 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 699 | struct io_connect connect; |
e47293fd | 700 | struct io_sr_msg sr_msg; |
15b71abe | 701 | struct io_open open; |
b5dba59e | 702 | struct io_close close; |
269bbe5f | 703 | struct io_rsrc_update rsrc_update; |
4840e418 | 704 | struct io_fadvise fadvise; |
c1ca757b | 705 | struct io_madvise madvise; |
3e4827b0 | 706 | struct io_epoll epoll; |
7d67af2c | 707 | struct io_splice splice; |
ddf0322d | 708 | struct io_provide_buf pbuf; |
1d9e1288 | 709 | struct io_statx statx; |
36f4fa68 | 710 | struct io_shutdown shutdown; |
80a261fd | 711 | struct io_rename rename; |
14a1143b | 712 | struct io_unlink unlink; |
3ca405eb PB |
713 | /* use only after cleaning per-op data, see io_clean_op() */ |
714 | struct io_completion compl; | |
221c5eb2 | 715 | }; |
2b188cc1 | 716 | |
e8c2bc1f JA |
717 | /* opcode allocated if it needs to store data for async defer */ |
718 | void *async_data; | |
d625c6ee | 719 | u8 opcode; |
65a6543d XW |
720 | /* polled IO has completed */ |
721 | u8 iopoll_completed; | |
2b188cc1 | 722 | |
4f4eeba8 | 723 | u16 buf_index; |
9cf7c104 | 724 | u32 result; |
4f4eeba8 | 725 | |
010e8e6b PB |
726 | struct io_ring_ctx *ctx; |
727 | unsigned int flags; | |
728 | refcount_t refs; | |
729 | struct task_struct *task; | |
730 | u64 user_data; | |
d7718a9d | 731 | |
f2f87370 | 732 | struct io_kiocb *link; |
269bbe5f | 733 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 734 | |
d21ffe7e PB |
735 | /* |
736 | * 1. used with ctx->iopoll_list with reads/writes | |
737 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
738 | */ | |
010e8e6b | 739 | struct list_head inflight_entry; |
010e8e6b PB |
740 | struct callback_head task_work; |
741 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
742 | struct hlist_node hash_node; | |
743 | struct async_poll *apoll; | |
744 | struct io_wq_work work; | |
2b188cc1 | 745 | }; |
05589553 | 746 | |
27dc8338 PB |
747 | struct io_defer_entry { |
748 | struct list_head list; | |
749 | struct io_kiocb *req; | |
9cf7c104 | 750 | u32 seq; |
2b188cc1 JA |
751 | }; |
752 | ||
def596e9 | 753 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 754 | |
013538bd JA |
755 | struct io_comp_state { |
756 | unsigned int nr; | |
757 | struct list_head list; | |
758 | struct io_ring_ctx *ctx; | |
759 | }; | |
760 | ||
9a56a232 JA |
761 | struct io_submit_state { |
762 | struct blk_plug plug; | |
763 | ||
2579f913 JA |
764 | /* |
765 | * io_kiocb alloc cache | |
766 | */ | |
767 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 768 | unsigned int free_reqs; |
2579f913 | 769 | |
27926b68 JA |
770 | bool plug_started; |
771 | ||
013538bd JA |
772 | /* |
773 | * Batch completion logic | |
774 | */ | |
775 | struct io_comp_state comp; | |
776 | ||
9a56a232 JA |
777 | /* |
778 | * File reference cache | |
779 | */ | |
780 | struct file *file; | |
781 | unsigned int fd; | |
6e1271e6 | 782 | unsigned int file_refs; |
9a56a232 JA |
783 | unsigned int ios_left; |
784 | }; | |
785 | ||
d3656344 | 786 | struct io_op_def { |
d3656344 JA |
787 | /* needs req->file assigned */ |
788 | unsigned needs_file : 1; | |
fd2206e4 JA |
789 | /* don't fail if file grab fails */ |
790 | unsigned needs_file_no_error : 1; | |
d3656344 JA |
791 | /* hash wq insertion if file is a regular file */ |
792 | unsigned hash_reg_file : 1; | |
793 | /* unbound wq insertion if file is a non-regular file */ | |
794 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
795 | /* opcode is not supported by this kernel */ |
796 | unsigned not_supported : 1; | |
8a72758c JA |
797 | /* set if opcode supports polled "wait" */ |
798 | unsigned pollin : 1; | |
799 | unsigned pollout : 1; | |
bcda7baa JA |
800 | /* op supports buffer selection */ |
801 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
802 | /* must always have async data allocated */ |
803 | unsigned needs_async_data : 1; | |
27926b68 JA |
804 | /* should block plug */ |
805 | unsigned plug : 1; | |
e8c2bc1f JA |
806 | /* size of async data needed, if any */ |
807 | unsigned short async_size; | |
0f203765 | 808 | unsigned work_flags; |
d3656344 JA |
809 | }; |
810 | ||
0918682b | 811 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
812 | [IORING_OP_NOP] = {}, |
813 | [IORING_OP_READV] = { | |
d3656344 JA |
814 | .needs_file = 1, |
815 | .unbound_nonreg_file = 1, | |
8a72758c | 816 | .pollin = 1, |
4d954c25 | 817 | .buffer_select = 1, |
e8c2bc1f | 818 | .needs_async_data = 1, |
27926b68 | 819 | .plug = 1, |
e8c2bc1f | 820 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 821 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 822 | }, |
0463b6c5 | 823 | [IORING_OP_WRITEV] = { |
d3656344 JA |
824 | .needs_file = 1, |
825 | .hash_reg_file = 1, | |
826 | .unbound_nonreg_file = 1, | |
8a72758c | 827 | .pollout = 1, |
e8c2bc1f | 828 | .needs_async_data = 1, |
27926b68 | 829 | .plug = 1, |
e8c2bc1f | 830 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
831 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
832 | IO_WQ_WORK_FSIZE, | |
d3656344 | 833 | }, |
0463b6c5 | 834 | [IORING_OP_FSYNC] = { |
d3656344 | 835 | .needs_file = 1, |
0f203765 | 836 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 837 | }, |
0463b6c5 | 838 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
839 | .needs_file = 1, |
840 | .unbound_nonreg_file = 1, | |
8a72758c | 841 | .pollin = 1, |
27926b68 | 842 | .plug = 1, |
e8c2bc1f | 843 | .async_size = sizeof(struct io_async_rw), |
4017eb91 | 844 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
d3656344 | 845 | }, |
0463b6c5 | 846 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
847 | .needs_file = 1, |
848 | .hash_reg_file = 1, | |
849 | .unbound_nonreg_file = 1, | |
8a72758c | 850 | .pollout = 1, |
27926b68 | 851 | .plug = 1, |
e8c2bc1f | 852 | .async_size = sizeof(struct io_async_rw), |
4017eb91 JA |
853 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
854 | IO_WQ_WORK_MM, | |
d3656344 | 855 | }, |
0463b6c5 | 856 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
857 | .needs_file = 1, |
858 | .unbound_nonreg_file = 1, | |
859 | }, | |
0463b6c5 PB |
860 | [IORING_OP_POLL_REMOVE] = {}, |
861 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 | 862 | .needs_file = 1, |
0f203765 | 863 | .work_flags = IO_WQ_WORK_BLKCG, |
d3656344 | 864 | }, |
0463b6c5 | 865 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
866 | .needs_file = 1, |
867 | .unbound_nonreg_file = 1, | |
8a72758c | 868 | .pollout = 1, |
e8c2bc1f JA |
869 | .needs_async_data = 1, |
870 | .async_size = sizeof(struct io_async_msghdr), | |
10cad2c4 | 871 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 872 | }, |
0463b6c5 | 873 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
874 | .needs_file = 1, |
875 | .unbound_nonreg_file = 1, | |
8a72758c | 876 | .pollin = 1, |
52de1fe1 | 877 | .buffer_select = 1, |
e8c2bc1f JA |
878 | .needs_async_data = 1, |
879 | .async_size = sizeof(struct io_async_msghdr), | |
10cad2c4 | 880 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
d3656344 | 881 | }, |
0463b6c5 | 882 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
883 | .needs_async_data = 1, |
884 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 885 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 886 | }, |
9c8e11b3 PB |
887 | [IORING_OP_TIMEOUT_REMOVE] = { |
888 | /* used by timeout updates' prep() */ | |
889 | .work_flags = IO_WQ_WORK_MM, | |
890 | }, | |
0463b6c5 | 891 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
892 | .needs_file = 1, |
893 | .unbound_nonreg_file = 1, | |
8a72758c | 894 | .pollin = 1, |
0f203765 | 895 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
d3656344 | 896 | }, |
0463b6c5 PB |
897 | [IORING_OP_ASYNC_CANCEL] = {}, |
898 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
899 | .needs_async_data = 1, |
900 | .async_size = sizeof(struct io_timeout_data), | |
0f203765 | 901 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 902 | }, |
0463b6c5 | 903 | [IORING_OP_CONNECT] = { |
d3656344 JA |
904 | .needs_file = 1, |
905 | .unbound_nonreg_file = 1, | |
8a72758c | 906 | .pollout = 1, |
e8c2bc1f JA |
907 | .needs_async_data = 1, |
908 | .async_size = sizeof(struct io_async_connect), | |
0f203765 | 909 | .work_flags = IO_WQ_WORK_MM, |
d3656344 | 910 | }, |
0463b6c5 | 911 | [IORING_OP_FALLOCATE] = { |
d3656344 | 912 | .needs_file = 1, |
69228338 | 913 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
d3656344 | 914 | }, |
0463b6c5 | 915 | [IORING_OP_OPENAT] = { |
0f203765 | 916 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
14587a46 | 917 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
d3656344 | 918 | }, |
0463b6c5 | 919 | [IORING_OP_CLOSE] = { |
fd2206e4 JA |
920 | .needs_file = 1, |
921 | .needs_file_no_error = 1, | |
0f203765 | 922 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 923 | }, |
0463b6c5 | 924 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 925 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 926 | }, |
0463b6c5 | 927 | [IORING_OP_STATX] = { |
0f203765 JA |
928 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
929 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 930 | }, |
0463b6c5 | 931 | [IORING_OP_READ] = { |
3a6820f2 JA |
932 | .needs_file = 1, |
933 | .unbound_nonreg_file = 1, | |
8a72758c | 934 | .pollin = 1, |
bcda7baa | 935 | .buffer_select = 1, |
27926b68 | 936 | .plug = 1, |
e8c2bc1f | 937 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 938 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 939 | }, |
0463b6c5 | 940 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
941 | .needs_file = 1, |
942 | .unbound_nonreg_file = 1, | |
8a72758c | 943 | .pollout = 1, |
27926b68 | 944 | .plug = 1, |
e8c2bc1f | 945 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
946 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
947 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 948 | }, |
0463b6c5 | 949 | [IORING_OP_FADVISE] = { |
4840e418 | 950 | .needs_file = 1, |
0f203765 | 951 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 952 | }, |
0463b6c5 | 953 | [IORING_OP_MADVISE] = { |
0f203765 | 954 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 955 | }, |
0463b6c5 | 956 | [IORING_OP_SEND] = { |
fddaface JA |
957 | .needs_file = 1, |
958 | .unbound_nonreg_file = 1, | |
8a72758c | 959 | .pollout = 1, |
0f203765 | 960 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 961 | }, |
0463b6c5 | 962 | [IORING_OP_RECV] = { |
fddaface JA |
963 | .needs_file = 1, |
964 | .unbound_nonreg_file = 1, | |
8a72758c | 965 | .pollin = 1, |
bcda7baa | 966 | .buffer_select = 1, |
0f203765 | 967 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 968 | }, |
0463b6c5 | 969 | [IORING_OP_OPENAT2] = { |
0f203765 | 970 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
14587a46 | 971 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
cebdb986 | 972 | }, |
3e4827b0 JA |
973 | [IORING_OP_EPOLL_CTL] = { |
974 | .unbound_nonreg_file = 1, | |
0f203765 | 975 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 976 | }, |
7d67af2c PB |
977 | [IORING_OP_SPLICE] = { |
978 | .needs_file = 1, | |
979 | .hash_reg_file = 1, | |
980 | .unbound_nonreg_file = 1, | |
0f203765 | 981 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
982 | }, |
983 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 984 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
985 | [IORING_OP_TEE] = { |
986 | .needs_file = 1, | |
987 | .hash_reg_file = 1, | |
988 | .unbound_nonreg_file = 1, | |
989 | }, | |
36f4fa68 JA |
990 | [IORING_OP_SHUTDOWN] = { |
991 | .needs_file = 1, | |
992 | }, | |
80a261fd JA |
993 | [IORING_OP_RENAMEAT] = { |
994 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
995 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
996 | }, | |
14a1143b JA |
997 | [IORING_OP_UNLINKAT] = { |
998 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
999 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
1000 | }, | |
d3656344 JA |
1001 | }; |
1002 | ||
2e0464d4 BM |
1003 | enum io_mem_account { |
1004 | ACCT_LOCKED, | |
1005 | ACCT_PINNED, | |
1006 | }; | |
1007 | ||
90df0853 PB |
1008 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
1009 | struct task_struct *task); | |
1010 | ||
269bbe5f | 1011 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 1012 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 1013 | struct io_ring_ctx *ctx); |
bc9744cd PB |
1014 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
1015 | struct fixed_rsrc_ref_node *ref_node); | |
1ffc5422 | 1016 | |
81b68a5c PB |
1017 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
1018 | struct io_comp_state *cs); | |
78e19bbe | 1019 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1020 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1021 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1022 | static void io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 1023 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1024 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1025 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1026 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1027 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1028 | unsigned nr_args); |
3ca405eb | 1029 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1030 | static struct file *io_file_get(struct io_submit_state *state, |
1031 | struct io_kiocb *req, int fd, bool fixed); | |
c1379e24 | 1032 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
269bbe5f | 1033 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1034 | |
b63534c4 JA |
1035 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
1036 | struct iovec **iovec, struct iov_iter *iter, | |
1037 | bool needs_lock); | |
ff6165b2 JA |
1038 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1039 | const struct iovec *fast_iov, | |
227c0c96 | 1040 | struct iov_iter *iter, bool force); |
9d5c8190 | 1041 | static void io_req_drop_files(struct io_kiocb *req); |
907d1df3 | 1042 | static void io_req_task_queue(struct io_kiocb *req); |
de0617e4 | 1043 | |
2b188cc1 JA |
1044 | static struct kmem_cache *req_cachep; |
1045 | ||
0918682b | 1046 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1047 | |
1048 | struct sock *io_uring_get_socket(struct file *file) | |
1049 | { | |
1050 | #if defined(CONFIG_UNIX) | |
1051 | if (file->f_op == &io_uring_fops) { | |
1052 | struct io_ring_ctx *ctx = file->private_data; | |
1053 | ||
1054 | return ctx->ring_sock->sk; | |
1055 | } | |
1056 | #endif | |
1057 | return NULL; | |
1058 | } | |
1059 | EXPORT_SYMBOL(io_uring_get_socket); | |
1060 | ||
f2f87370 PB |
1061 | #define io_for_each_link(pos, head) \ |
1062 | for (pos = (head); pos; pos = pos->link) | |
1063 | ||
3ca405eb PB |
1064 | static inline void io_clean_op(struct io_kiocb *req) |
1065 | { | |
9d5c8190 | 1066 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1067 | __io_clean_op(req); |
1068 | } | |
1069 | ||
36f72fe2 PB |
1070 | static inline void io_set_resource_node(struct io_kiocb *req) |
1071 | { | |
1072 | struct io_ring_ctx *ctx = req->ctx; | |
1073 | ||
269bbe5f BM |
1074 | if (!req->fixed_rsrc_refs) { |
1075 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1076 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1077 | } |
1078 | } | |
1079 | ||
08d23634 PB |
1080 | static bool io_match_task(struct io_kiocb *head, |
1081 | struct task_struct *task, | |
1082 | struct files_struct *files) | |
1083 | { | |
1084 | struct io_kiocb *req; | |
1085 | ||
84965ff8 JA |
1086 | if (task && head->task != task) { |
1087 | /* in terms of cancelation, always match if req task is dead */ | |
1088 | if (head->task->flags & PF_EXITING) | |
1089 | return true; | |
08d23634 | 1090 | return false; |
84965ff8 | 1091 | } |
08d23634 PB |
1092 | if (!files) |
1093 | return true; | |
1094 | ||
1095 | io_for_each_link(req, head) { | |
02a13674 JA |
1096 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1097 | continue; | |
1098 | if (req->file && req->file->f_op == &io_uring_fops) | |
1099 | return true; | |
1100 | if ((req->work.flags & IO_WQ_WORK_FILES) && | |
08d23634 PB |
1101 | req->work.identity->files == files) |
1102 | return true; | |
1103 | } | |
1104 | return false; | |
1105 | } | |
1106 | ||
28cea78a | 1107 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1108 | { |
28cea78a | 1109 | struct files_struct *files = current->files; |
c40f6379 JA |
1110 | struct mm_struct *mm = current->mm; |
1111 | ||
1112 | if (mm) { | |
1113 | kthread_unuse_mm(mm); | |
1114 | mmput(mm); | |
4b70cf9d | 1115 | current->mm = NULL; |
c40f6379 | 1116 | } |
28cea78a JA |
1117 | if (files) { |
1118 | struct nsproxy *nsproxy = current->nsproxy; | |
1119 | ||
1120 | task_lock(current); | |
1121 | current->files = NULL; | |
1122 | current->nsproxy = NULL; | |
1123 | task_unlock(current); | |
1124 | put_files_struct(files); | |
1125 | put_nsproxy(nsproxy); | |
1126 | } | |
1127 | } | |
1128 | ||
1a38ffc9 | 1129 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a | 1130 | { |
621fadc2 PB |
1131 | if (current->flags & PF_EXITING) |
1132 | return -EFAULT; | |
1133 | ||
28cea78a JA |
1134 | if (!current->files) { |
1135 | struct files_struct *files; | |
1136 | struct nsproxy *nsproxy; | |
1137 | ||
1138 | task_lock(ctx->sqo_task); | |
1139 | files = ctx->sqo_task->files; | |
1140 | if (!files) { | |
1141 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1142 | return -EOWNERDEAD; |
28cea78a JA |
1143 | } |
1144 | atomic_inc(&files->count); | |
1145 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1146 | nsproxy = ctx->sqo_task->nsproxy; | |
1147 | task_unlock(ctx->sqo_task); | |
1148 | ||
1149 | task_lock(current); | |
1150 | current->files = files; | |
1151 | current->nsproxy = nsproxy; | |
1152 | task_unlock(current); | |
1153 | } | |
1a38ffc9 | 1154 | return 0; |
c40f6379 JA |
1155 | } |
1156 | ||
1157 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1158 | { | |
4b70cf9d JA |
1159 | struct mm_struct *mm; |
1160 | ||
621fadc2 PB |
1161 | if (current->flags & PF_EXITING) |
1162 | return -EFAULT; | |
4b70cf9d JA |
1163 | if (current->mm) |
1164 | return 0; | |
1165 | ||
1166 | /* Should never happen */ | |
1167 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) | |
1168 | return -EFAULT; | |
1169 | ||
1170 | task_lock(ctx->sqo_task); | |
1171 | mm = ctx->sqo_task->mm; | |
1172 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1173 | mm = NULL; | |
1174 | task_unlock(ctx->sqo_task); | |
1175 | ||
1176 | if (mm) { | |
1177 | kthread_use_mm(mm); | |
1178 | return 0; | |
c40f6379 JA |
1179 | } |
1180 | ||
4b70cf9d | 1181 | return -EFAULT; |
c40f6379 JA |
1182 | } |
1183 | ||
28cea78a JA |
1184 | static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1185 | struct io_kiocb *req) | |
c40f6379 | 1186 | { |
28cea78a | 1187 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1188 | int ret; |
28cea78a JA |
1189 | |
1190 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1191 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1192 | if (unlikely(ret)) |
1193 | return ret; | |
1194 | } | |
1195 | ||
1a38ffc9 PB |
1196 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1197 | ret = __io_sq_thread_acquire_files(ctx); | |
1198 | if (unlikely(ret)) | |
1199 | return ret; | |
1200 | } | |
28cea78a JA |
1201 | |
1202 | return 0; | |
c40f6379 JA |
1203 | } |
1204 | ||
91d8f519 DZ |
1205 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1206 | struct cgroup_subsys_state **cur_css) | |
1207 | ||
1208 | { | |
1209 | #ifdef CONFIG_BLK_CGROUP | |
1210 | /* puts the old one when swapping */ | |
1211 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1212 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1213 | *cur_css = ctx->sqo_blkcg_css; | |
1214 | } | |
1215 | #endif | |
1216 | } | |
1217 | ||
1218 | static void io_sq_thread_unassociate_blkcg(void) | |
1219 | { | |
1220 | #ifdef CONFIG_BLK_CGROUP | |
1221 | kthread_associate_blkcg(NULL); | |
1222 | #endif | |
1223 | } | |
1224 | ||
c40f6379 JA |
1225 | static inline void req_set_fail_links(struct io_kiocb *req) |
1226 | { | |
1227 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1228 | req->flags |= REQ_F_FAIL_LINK; | |
1229 | } | |
4a38aed2 | 1230 | |
1e6fa521 JA |
1231 | /* |
1232 | * None of these are dereferenced, they are simply used to check if any of | |
1233 | * them have changed. If we're under current and check they are still the | |
1234 | * same, we're fine to grab references to them for actual out-of-line use. | |
1235 | */ | |
1236 | static void io_init_identity(struct io_identity *id) | |
1237 | { | |
1238 | id->files = current->files; | |
1239 | id->mm = current->mm; | |
1240 | #ifdef CONFIG_BLK_CGROUP | |
1241 | rcu_read_lock(); | |
1242 | id->blkcg_css = blkcg_css(); | |
1243 | rcu_read_unlock(); | |
1244 | #endif | |
1245 | id->creds = current_cred(); | |
1246 | id->nsproxy = current->nsproxy; | |
1247 | id->fs = current->fs; | |
1248 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1249 | #ifdef CONFIG_AUDIT |
1250 | id->loginuid = current->loginuid; | |
1251 | id->sessionid = current->sessionid; | |
1252 | #endif | |
1e6fa521 JA |
1253 | refcount_set(&id->count, 1); |
1254 | } | |
1255 | ||
ec99ca6c PB |
1256 | static inline void __io_req_init_async(struct io_kiocb *req) |
1257 | { | |
1258 | memset(&req->work, 0, sizeof(req->work)); | |
1259 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1260 | } | |
1261 | ||
7cdaf587 XW |
1262 | /* |
1263 | * Note: must call io_req_init_async() for the first time you | |
1264 | * touch any members of io_wq_work. | |
1265 | */ | |
1266 | static inline void io_req_init_async(struct io_kiocb *req) | |
1267 | { | |
500a373d JA |
1268 | struct io_uring_task *tctx = current->io_uring; |
1269 | ||
7cdaf587 XW |
1270 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1271 | return; | |
1272 | ||
ec99ca6c | 1273 | __io_req_init_async(req); |
500a373d JA |
1274 | |
1275 | /* Grab a ref if this isn't our static identity */ | |
1276 | req->work.identity = tctx->identity; | |
1277 | if (tctx->identity != &tctx->__identity) | |
1278 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1279 | } |
1280 | ||
0cdaf760 PB |
1281 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
1282 | { | |
1283 | return ctx->flags & IORING_SETUP_SQPOLL; | |
1284 | } | |
1285 | ||
2b188cc1 JA |
1286 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1287 | { | |
1288 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1289 | ||
0f158b4c | 1290 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1291 | } |
1292 | ||
8eb7e2d0 PB |
1293 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1294 | { | |
1295 | return !req->timeout.off; | |
1296 | } | |
1297 | ||
2b188cc1 JA |
1298 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1299 | { | |
1300 | struct io_ring_ctx *ctx; | |
78076bb6 | 1301 | int hash_bits; |
2b188cc1 JA |
1302 | |
1303 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1304 | if (!ctx) | |
1305 | return NULL; | |
1306 | ||
0ddf92e8 JA |
1307 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
1308 | if (!ctx->fallback_req) | |
1309 | goto err; | |
1310 | ||
78076bb6 JA |
1311 | /* |
1312 | * Use 5 bits less than the max cq entries, that should give us around | |
1313 | * 32 entries per hash list if totally full and uniformly spread. | |
1314 | */ | |
1315 | hash_bits = ilog2(p->cq_entries); | |
1316 | hash_bits -= 5; | |
1317 | if (hash_bits <= 0) | |
1318 | hash_bits = 1; | |
1319 | ctx->cancel_hash_bits = hash_bits; | |
1320 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1321 | GFP_KERNEL); | |
1322 | if (!ctx->cancel_hash) | |
1323 | goto err; | |
1324 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1325 | ||
21482896 | 1326 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1327 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1328 | goto err; | |
2b188cc1 JA |
1329 | |
1330 | ctx->flags = p->flags; | |
90554200 | 1331 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1332 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1333 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1334 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1335 | init_completion(&ctx->ref_comp); |
1336 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1337 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1338 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1339 | mutex_init(&ctx->uring_lock); |
1340 | init_waitqueue_head(&ctx->wait); | |
1341 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1342 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1343 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1344 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1345 | spin_lock_init(&ctx->inflight_lock); |
1346 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1347 | spin_lock_init(&ctx->rsrc_ref_lock); |
1348 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1349 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1350 | init_llist_head(&ctx->rsrc_put_llist); | |
2b188cc1 | 1351 | return ctx; |
206aefde | 1352 | err: |
0ddf92e8 JA |
1353 | if (ctx->fallback_req) |
1354 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
78076bb6 | 1355 | kfree(ctx->cancel_hash); |
206aefde JA |
1356 | kfree(ctx); |
1357 | return NULL; | |
2b188cc1 JA |
1358 | } |
1359 | ||
9cf7c104 | 1360 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1361 | { |
2bc9930e JA |
1362 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1363 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1364 | |
9cf7c104 | 1365 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1366 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1367 | } |
de0617e4 | 1368 | |
9d858b21 | 1369 | return false; |
de0617e4 JA |
1370 | } |
1371 | ||
de0617e4 | 1372 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 1373 | { |
75b28aff | 1374 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 1375 | |
07910158 PB |
1376 | /* order cqe stores with ring update */ |
1377 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 JA |
1378 | } |
1379 | ||
5c3462cf | 1380 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1381 | { |
500a373d | 1382 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1383 | return; |
1384 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1385 | kfree(req->work.identity); | |
1386 | } | |
1387 | ||
4edf20f9 | 1388 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1389 | { |
7cdaf587 | 1390 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1391 | return; |
51a4cc11 JA |
1392 | |
1393 | req->flags &= ~REQ_F_WORK_INITIALIZED; | |
7cdaf587 | 1394 | |
dfead8a8 | 1395 | if (req->work.flags & IO_WQ_WORK_MM) { |
98447d65 | 1396 | mmdrop(req->work.identity->mm); |
dfead8a8 | 1397 | req->work.flags &= ~IO_WQ_WORK_MM; |
cccf0ee8 | 1398 | } |
91d8f519 | 1399 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 | 1400 | if (req->work.flags & IO_WQ_WORK_BLKCG) { |
98447d65 | 1401 | css_put(req->work.identity->blkcg_css); |
dfead8a8 JA |
1402 | req->work.flags &= ~IO_WQ_WORK_BLKCG; |
1403 | } | |
91d8f519 | 1404 | #endif |
dfead8a8 | 1405 | if (req->work.flags & IO_WQ_WORK_CREDS) { |
98447d65 | 1406 | put_cred(req->work.identity->creds); |
dfead8a8 | 1407 | req->work.flags &= ~IO_WQ_WORK_CREDS; |
cccf0ee8 | 1408 | } |
dfead8a8 | 1409 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1410 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1411 | |
98447d65 | 1412 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1413 | if (--fs->users) |
1414 | fs = NULL; | |
98447d65 | 1415 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1416 | if (fs) |
1417 | free_fs_struct(fs); | |
dfead8a8 | 1418 | req->work.flags &= ~IO_WQ_WORK_FS; |
ff002b30 | 1419 | } |
9d5c8190 PB |
1420 | if (req->flags & REQ_F_INFLIGHT) |
1421 | io_req_drop_files(req); | |
51a4cc11 | 1422 | |
5c3462cf | 1423 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1424 | } |
1425 | ||
1e6fa521 JA |
1426 | /* |
1427 | * Create a private copy of io_identity, since some fields don't match | |
1428 | * the current context. | |
1429 | */ | |
1430 | static bool io_identity_cow(struct io_kiocb *req) | |
1431 | { | |
5c3462cf | 1432 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1433 | const struct cred *creds = NULL; |
1434 | struct io_identity *id; | |
1435 | ||
1436 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1437 | creds = req->work.identity->creds; | |
1438 | ||
1439 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1440 | if (unlikely(!id)) { | |
1441 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1442 | return false; | |
1443 | } | |
1444 | ||
1445 | /* | |
1446 | * We can safely just re-init the creds we copied Either the field | |
1447 | * matches the current one, or we haven't grabbed it yet. The only | |
1448 | * exception is ->creds, through registered personalities, so handle | |
1449 | * that one separately. | |
1450 | */ | |
1451 | io_init_identity(id); | |
1452 | if (creds) | |
e8c954df | 1453 | id->creds = creds; |
1e6fa521 JA |
1454 | |
1455 | /* add one for this request */ | |
1456 | refcount_inc(&id->count); | |
1457 | ||
cb8a8ae3 JA |
1458 | /* drop tctx and req identity references, if needed */ |
1459 | if (tctx->identity != &tctx->__identity && | |
1460 | refcount_dec_and_test(&tctx->identity->count)) | |
1461 | kfree(tctx->identity); | |
1462 | if (req->work.identity != &tctx->__identity && | |
1463 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1464 | kfree(req->work.identity); |
1465 | ||
1466 | req->work.identity = id; | |
500a373d | 1467 | tctx->identity = id; |
1e6fa521 JA |
1468 | return true; |
1469 | } | |
1470 | ||
1471 | static bool io_grab_identity(struct io_kiocb *req) | |
18d9be1a | 1472 | { |
d3656344 | 1473 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1474 | struct io_identity *id = req->work.identity; |
23329513 | 1475 | struct io_ring_ctx *ctx = req->ctx; |
54a91f3b | 1476 | |
69228338 JA |
1477 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1478 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1479 | return false; | |
1480 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1481 | } | |
91d8f519 | 1482 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1483 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1484 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1485 | rcu_read_lock(); |
1e6fa521 JA |
1486 | if (id->blkcg_css != blkcg_css()) { |
1487 | rcu_read_unlock(); | |
1488 | return false; | |
1489 | } | |
91d8f519 DZ |
1490 | /* |
1491 | * This should be rare, either the cgroup is dying or the task | |
1492 | * is moving cgroups. Just punt to root for the handful of ios. | |
1493 | */ | |
1e6fa521 | 1494 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1495 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1496 | rcu_read_unlock(); |
1497 | } | |
1498 | #endif | |
dfead8a8 | 1499 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1500 | if (id->creds != current_cred()) |
1501 | return false; | |
1502 | get_cred(id->creds); | |
dfead8a8 JA |
1503 | req->work.flags |= IO_WQ_WORK_CREDS; |
1504 | } | |
4ea33a97 JA |
1505 | #ifdef CONFIG_AUDIT |
1506 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1507 | current->sessionid != id->sessionid) | |
1508 | return false; | |
1509 | #endif | |
dfead8a8 JA |
1510 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1511 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1512 | if (current->fs != id->fs) |
1513 | return false; | |
1514 | spin_lock(&id->fs->lock); | |
1515 | if (!id->fs->in_exec) { | |
1516 | id->fs->users++; | |
dfead8a8 | 1517 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1518 | } else { |
1519 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1520 | } | |
1521 | spin_unlock(¤t->fs->lock); | |
1522 | } | |
af604703 PB |
1523 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1524 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1525 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1526 | if (id->files != current->files || | |
1527 | id->nsproxy != current->nsproxy) | |
1528 | return false; | |
1529 | atomic_inc(&id->files->count); | |
1530 | get_nsproxy(id->nsproxy); | |
af604703 | 1531 | |
02a13674 JA |
1532 | if (!(req->flags & REQ_F_INFLIGHT)) { |
1533 | req->flags |= REQ_F_INFLIGHT; | |
1534 | ||
1535 | spin_lock_irq(&ctx->inflight_lock); | |
1536 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1537 | spin_unlock_irq(&ctx->inflight_lock); | |
1538 | } | |
af604703 PB |
1539 | req->work.flags |= IO_WQ_WORK_FILES; |
1540 | } | |
77788775 JA |
1541 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1542 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1543 | if (id->mm != current->mm) | |
1544 | return false; | |
1545 | mmgrab(id->mm); | |
1546 | req->work.flags |= IO_WQ_WORK_MM; | |
1547 | } | |
1e6fa521 JA |
1548 | |
1549 | return true; | |
1550 | } | |
1551 | ||
1552 | static void io_prep_async_work(struct io_kiocb *req) | |
1553 | { | |
1554 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1555 | struct io_ring_ctx *ctx = req->ctx; |
1556 | ||
1557 | io_req_init_async(req); | |
1558 | ||
feaadc4f PB |
1559 | if (req->flags & REQ_F_FORCE_ASYNC) |
1560 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1561 | ||
1e6fa521 JA |
1562 | if (req->flags & REQ_F_ISREG) { |
1563 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1564 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1565 | } else { | |
1566 | if (def->unbound_nonreg_file) | |
1567 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1568 | } | |
1569 | ||
1e6fa521 JA |
1570 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1571 | if (io_grab_identity(req)) | |
1572 | return; | |
1573 | ||
1574 | if (!io_identity_cow(req)) | |
1575 | return; | |
1576 | ||
1577 | /* can't fail at this point */ | |
1578 | if (!io_grab_identity(req)) | |
1579 | WARN_ON(1); | |
561fb04a | 1580 | } |
cccf0ee8 | 1581 | |
cbdcb435 | 1582 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1583 | { |
cbdcb435 | 1584 | struct io_kiocb *cur; |
54a91f3b | 1585 | |
f2f87370 PB |
1586 | io_for_each_link(cur, req) |
1587 | io_prep_async_work(cur); | |
561fb04a JA |
1588 | } |
1589 | ||
7271ef3a | 1590 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1591 | { |
a197f664 | 1592 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1593 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1594 | |
8766dd51 PB |
1595 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1596 | &req->work, req->flags); | |
1597 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1598 | return link; |
18d9be1a JA |
1599 | } |
1600 | ||
cbdcb435 PB |
1601 | static void io_queue_async_work(struct io_kiocb *req) |
1602 | { | |
7271ef3a JA |
1603 | struct io_kiocb *link; |
1604 | ||
cbdcb435 PB |
1605 | /* init ->work of the whole link before punting */ |
1606 | io_prep_async_link(req); | |
7271ef3a JA |
1607 | link = __io_queue_async_work(req); |
1608 | ||
1609 | if (link) | |
1610 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1611 | } |
1612 | ||
5262f567 JA |
1613 | static void io_kill_timeout(struct io_kiocb *req) |
1614 | { | |
e8c2bc1f | 1615 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1616 | int ret; |
1617 | ||
e8c2bc1f | 1618 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1619 | if (ret != -1) { |
01cec8c1 PB |
1620 | atomic_set(&req->ctx->cq_timeouts, |
1621 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1622 | list_del_init(&req->timeout.list); |
78e19bbe | 1623 | io_cqring_fill_event(req, 0); |
216578e5 | 1624 | io_put_req_deferred(req, 1); |
5262f567 JA |
1625 | } |
1626 | } | |
1627 | ||
76e1b642 JA |
1628 | /* |
1629 | * Returns true if we found and killed one or more timeouts | |
1630 | */ | |
6b81928d PB |
1631 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1632 | struct files_struct *files) | |
5262f567 JA |
1633 | { |
1634 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1635 | int canceled = 0; |
5262f567 JA |
1636 | |
1637 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1638 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1639 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1640 | io_kill_timeout(req); |
76e1b642 JA |
1641 | canceled++; |
1642 | } | |
f3606e3a | 1643 | } |
5262f567 | 1644 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1645 | return canceled != 0; |
5262f567 JA |
1646 | } |
1647 | ||
04518945 | 1648 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1649 | { |
04518945 | 1650 | do { |
27dc8338 PB |
1651 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1652 | struct io_defer_entry, list); | |
de0617e4 | 1653 | |
9cf7c104 | 1654 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1655 | break; |
27dc8338 | 1656 | list_del_init(&de->list); |
907d1df3 | 1657 | io_req_task_queue(de->req); |
27dc8338 | 1658 | kfree(de); |
04518945 PB |
1659 | } while (!list_empty(&ctx->defer_list)); |
1660 | } | |
1661 | ||
360428f8 | 1662 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1663 | { |
f010505b MDG |
1664 | u32 seq; |
1665 | ||
1666 | if (list_empty(&ctx->timeout_list)) | |
1667 | return; | |
1668 | ||
1669 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1670 | ||
1671 | do { | |
1672 | u32 events_needed, events_got; | |
360428f8 | 1673 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1674 | struct io_kiocb, timeout.list); |
de0617e4 | 1675 | |
8eb7e2d0 | 1676 | if (io_is_timeout_noseq(req)) |
360428f8 | 1677 | break; |
f010505b MDG |
1678 | |
1679 | /* | |
1680 | * Since seq can easily wrap around over time, subtract | |
1681 | * the last seq at which timeouts were flushed before comparing. | |
1682 | * Assuming not more than 2^31-1 events have happened since, | |
1683 | * these subtractions won't have wrapped, so we can check if | |
1684 | * target is in [last_seq, current_seq] by comparing the two. | |
1685 | */ | |
1686 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1687 | events_got = seq - ctx->cq_last_tm_flush; | |
1688 | if (events_got < events_needed) | |
360428f8 | 1689 | break; |
bfe68a22 | 1690 | |
135fcde8 | 1691 | list_del_init(&req->timeout.list); |
5262f567 | 1692 | io_kill_timeout(req); |
f010505b MDG |
1693 | } while (!list_empty(&ctx->timeout_list)); |
1694 | ||
1695 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1696 | } |
5262f567 | 1697 | |
360428f8 PB |
1698 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1699 | { | |
1700 | io_flush_timeouts(ctx); | |
de0617e4 JA |
1701 | __io_commit_cqring(ctx); |
1702 | ||
04518945 PB |
1703 | if (unlikely(!list_empty(&ctx->defer_list))) |
1704 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1705 | } |
1706 | ||
90554200 JA |
1707 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1708 | { | |
1709 | struct io_rings *r = ctx->rings; | |
1710 | ||
1711 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1712 | } | |
1713 | ||
2b188cc1 JA |
1714 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1715 | { | |
75b28aff | 1716 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1717 | unsigned tail; |
1718 | ||
1719 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
1720 | /* |
1721 | * writes to the cq entry need to come after reading head; the | |
1722 | * control dependency is enough as we're using WRITE_ONCE to | |
1723 | * fill the cq entry | |
1724 | */ | |
75b28aff | 1725 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
1726 | return NULL; |
1727 | ||
1728 | ctx->cached_cq_tail++; | |
75b28aff | 1729 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1730 | } |
1731 | ||
f2842ab5 JA |
1732 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1733 | { | |
f0b493e6 JA |
1734 | if (!ctx->cq_ev_fd) |
1735 | return false; | |
7e55a19c SG |
1736 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1737 | return false; | |
f2842ab5 JA |
1738 | if (!ctx->eventfd_async) |
1739 | return true; | |
b41e9852 | 1740 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1741 | } |
1742 | ||
e23de15f PB |
1743 | static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx) |
1744 | { | |
1745 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1746 | } | |
1747 | ||
b41e9852 | 1748 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1749 | { |
b1445e59 PB |
1750 | /* see waitqueue_active() comment */ |
1751 | smp_mb(); | |
1752 | ||
1d7bb1d5 JA |
1753 | if (waitqueue_active(&ctx->wait)) |
1754 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1755 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1756 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1757 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1758 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1759 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1760 | wake_up_interruptible(&ctx->cq_wait); |
1761 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1762 | } | |
1d7bb1d5 JA |
1763 | } |
1764 | ||
80c18e4a PB |
1765 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1766 | { | |
b1445e59 PB |
1767 | /* see waitqueue_active() comment */ |
1768 | smp_mb(); | |
1769 | ||
80c18e4a PB |
1770 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1771 | if (waitqueue_active(&ctx->wait)) | |
1772 | wake_up(&ctx->wait); | |
1773 | } | |
1774 | if (io_should_trigger_evfd(ctx)) | |
1775 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1776 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1777 | wake_up_interruptible(&ctx->cq_wait); |
1778 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1779 | } | |
80c18e4a PB |
1780 | } |
1781 | ||
c4a2ed72 | 1782 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1783 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1784 | struct task_struct *tsk, | |
1785 | struct files_struct *files) | |
1d7bb1d5 JA |
1786 | { |
1787 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1788 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1789 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1790 | unsigned long flags; |
b18032bb | 1791 | bool all_flushed, posted; |
1d7bb1d5 JA |
1792 | LIST_HEAD(list); |
1793 | ||
e23de15f PB |
1794 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1795 | return false; | |
1d7bb1d5 | 1796 | |
b18032bb | 1797 | posted = false; |
1d7bb1d5 | 1798 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1799 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1800 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1801 | continue; |
1802 | ||
1d7bb1d5 JA |
1803 | cqe = io_get_cqring(ctx); |
1804 | if (!cqe && !force) | |
1805 | break; | |
1806 | ||
40d8ddd4 | 1807 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1808 | if (cqe) { |
1809 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1810 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1811 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1812 | } else { |
2c3bac6d | 1813 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1814 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1815 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1816 | } |
b18032bb | 1817 | posted = true; |
1d7bb1d5 JA |
1818 | } |
1819 | ||
09e88404 PB |
1820 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1821 | if (all_flushed) { | |
1822 | clear_bit(0, &ctx->sq_check_overflow); | |
1823 | clear_bit(0, &ctx->cq_check_overflow); | |
1824 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1825 | } | |
46930143 | 1826 | |
b18032bb JA |
1827 | if (posted) |
1828 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1829 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1830 | if (posted) |
1831 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1832 | |
1833 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1834 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1835 | list_del(&req->compl.list); | |
ec9c02ad | 1836 | io_put_req(req); |
1d7bb1d5 | 1837 | } |
c4a2ed72 | 1838 | |
09e88404 | 1839 | return all_flushed; |
1d7bb1d5 JA |
1840 | } |
1841 | ||
6c503150 PB |
1842 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1843 | struct task_struct *tsk, | |
1844 | struct files_struct *files) | |
1845 | { | |
1846 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1847 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1848 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1849 | mutex_lock(&ctx->uring_lock); | |
1850 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1851 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1852 | mutex_unlock(&ctx->uring_lock); | |
1853 | } | |
1854 | } | |
1855 | ||
bcda7baa | 1856 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1857 | { |
78e19bbe | 1858 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1859 | struct io_uring_cqe *cqe; |
1860 | ||
78e19bbe | 1861 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1862 | |
2b188cc1 JA |
1863 | /* |
1864 | * If we can't get a cq entry, userspace overflowed the | |
1865 | * submission (by quite a lot). Increment the overflow count in | |
1866 | * the ring. | |
1867 | */ | |
1868 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1869 | if (likely(cqe)) { |
78e19bbe | 1870 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1871 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1872 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1873 | } else if (ctx->cq_overflow_flushed || |
1874 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1875 | /* |
1876 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1877 | * then we cannot store the request for later flushing, we need | |
1878 | * to drop it on the floor. | |
1879 | */ | |
2c3bac6d PB |
1880 | ctx->cached_cq_overflow++; |
1881 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1882 | } else { |
ad3eb2c8 JA |
1883 | if (list_empty(&ctx->cq_overflow_list)) { |
1884 | set_bit(0, &ctx->sq_check_overflow); | |
1885 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1886 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1887 | } |
40d8ddd4 | 1888 | io_clean_op(req); |
1d7bb1d5 | 1889 | req->result = res; |
0f7e466b | 1890 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1891 | refcount_inc(&req->refs); |
1892 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1893 | } |
1894 | } | |
1895 | ||
bcda7baa JA |
1896 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1897 | { | |
1898 | __io_cqring_fill_event(req, res, 0); | |
1899 | } | |
1900 | ||
e1e16097 | 1901 | static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1902 | { |
78e19bbe | 1903 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1904 | unsigned long flags; |
1905 | ||
1906 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1907 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1908 | io_commit_cqring(ctx); |
1909 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1910 | ||
8c838788 | 1911 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1912 | } |
1913 | ||
229a7b63 | 1914 | static void io_submit_flush_completions(struct io_comp_state *cs) |
bcda7baa | 1915 | { |
229a7b63 JA |
1916 | struct io_ring_ctx *ctx = cs->ctx; |
1917 | ||
1918 | spin_lock_irq(&ctx->completion_lock); | |
1919 | while (!list_empty(&cs->list)) { | |
1920 | struct io_kiocb *req; | |
1921 | ||
3ca405eb PB |
1922 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
1923 | list_del(&req->compl.list); | |
0f7e466b | 1924 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
216578e5 PB |
1925 | |
1926 | /* | |
1927 | * io_free_req() doesn't care about completion_lock unless one | |
1928 | * of these flags is set. REQ_F_WORK_INITIALIZED is in the list | |
1929 | * because of a potential deadlock with req->work.fs->lock | |
1930 | */ | |
1931 | if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT | |
1932 | |REQ_F_WORK_INITIALIZED)) { | |
229a7b63 JA |
1933 | spin_unlock_irq(&ctx->completion_lock); |
1934 | io_put_req(req); | |
1935 | spin_lock_irq(&ctx->completion_lock); | |
216578e5 PB |
1936 | } else { |
1937 | io_put_req(req); | |
229a7b63 JA |
1938 | } |
1939 | } | |
1940 | io_commit_cqring(ctx); | |
1941 | spin_unlock_irq(&ctx->completion_lock); | |
1942 | ||
1943 | io_cqring_ev_posted(ctx); | |
1944 | cs->nr = 0; | |
1945 | } | |
1946 | ||
1947 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, | |
1948 | struct io_comp_state *cs) | |
1949 | { | |
1950 | if (!cs) { | |
1951 | io_cqring_add_event(req, res, cflags); | |
1952 | io_put_req(req); | |
1953 | } else { | |
3ca405eb | 1954 | io_clean_op(req); |
229a7b63 | 1955 | req->result = res; |
0f7e466b | 1956 | req->compl.cflags = cflags; |
3ca405eb | 1957 | list_add_tail(&req->compl.list, &cs->list); |
229a7b63 JA |
1958 | if (++cs->nr >= 32) |
1959 | io_submit_flush_completions(cs); | |
1960 | } | |
e1e16097 JA |
1961 | } |
1962 | ||
1963 | static void io_req_complete(struct io_kiocb *req, long res) | |
bcda7baa | 1964 | { |
229a7b63 | 1965 | __io_req_complete(req, res, 0, NULL); |
bcda7baa JA |
1966 | } |
1967 | ||
0ddf92e8 JA |
1968 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1969 | { | |
1970 | return req == (struct io_kiocb *) | |
1971 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1972 | } | |
1973 | ||
1974 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1975 | { | |
1976 | struct io_kiocb *req; | |
1977 | ||
1978 | req = ctx->fallback_req; | |
dd461af6 | 1979 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
0ddf92e8 JA |
1980 | return req; |
1981 | ||
1982 | return NULL; | |
1983 | } | |
1984 | ||
0553b8bd PB |
1985 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
1986 | struct io_submit_state *state) | |
2b188cc1 | 1987 | { |
f6b6c7d6 | 1988 | if (!state->free_reqs) { |
291b2821 | 1989 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1990 | size_t sz; |
1991 | int ret; | |
1992 | ||
1993 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1994 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1995 | ||
1996 | /* | |
1997 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1998 | * retry single alloc to be on the safe side. | |
1999 | */ | |
2000 | if (unlikely(ret <= 0)) { | |
2001 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
2002 | if (!state->reqs[0]) | |
0ddf92e8 | 2003 | goto fallback; |
fd6fab2c JA |
2004 | ret = 1; |
2005 | } | |
291b2821 | 2006 | state->free_reqs = ret; |
2b188cc1 JA |
2007 | } |
2008 | ||
291b2821 PB |
2009 | state->free_reqs--; |
2010 | return state->reqs[state->free_reqs]; | |
0ddf92e8 | 2011 | fallback: |
0553b8bd | 2012 | return io_get_fallback_req(ctx); |
2b188cc1 JA |
2013 | } |
2014 | ||
8da11c19 PB |
2015 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
2016 | bool fixed) | |
2017 | { | |
36f72fe2 | 2018 | if (!fixed) |
8da11c19 PB |
2019 | fput(file); |
2020 | } | |
2021 | ||
4edf20f9 | 2022 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2023 | { |
3ca405eb | 2024 | io_clean_op(req); |
929a3af9 | 2025 | |
e8c2bc1f JA |
2026 | if (req->async_data) |
2027 | kfree(req->async_data); | |
8da11c19 PB |
2028 | if (req->file) |
2029 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
2030 | if (req->fixed_rsrc_refs) |
2031 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 2032 | io_req_clean_work(req); |
e65ef56d JA |
2033 | } |
2034 | ||
216578e5 | 2035 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2036 | { |
0f212204 | 2037 | struct io_uring_task *tctx = req->task->io_uring; |
51a4cc11 | 2038 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2039 | |
216578e5 | 2040 | io_dismantle_req(req); |
c6ca97b3 | 2041 | |
d8a6df10 | 2042 | percpu_counter_dec(&tctx->inflight); |
fdaf083c | 2043 | if (atomic_read(&tctx->in_idle)) |
0f212204 | 2044 | wake_up(&tctx->wait); |
e3bc8e9d JA |
2045 | put_task_struct(req->task); |
2046 | ||
b1e50e54 PB |
2047 | if (likely(!io_is_fallback_req(req))) |
2048 | kmem_cache_free(req_cachep, req); | |
2049 | else | |
ecfc5177 PB |
2050 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
2051 | percpu_ref_put(&ctx->refs); | |
e65ef56d JA |
2052 | } |
2053 | ||
f2f87370 PB |
2054 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2055 | { | |
2056 | struct io_kiocb *nxt = req->link; | |
2057 | ||
2058 | req->link = nxt->link; | |
2059 | nxt->link = NULL; | |
2060 | } | |
2061 | ||
c9abd7ad | 2062 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 2063 | { |
a197f664 | 2064 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 2065 | struct io_kiocb *link; |
c9abd7ad PB |
2066 | bool cancelled = false; |
2067 | unsigned long flags; | |
7c86ffee | 2068 | |
c9abd7ad | 2069 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2070 | link = req->link; |
2071 | ||
900fad45 PB |
2072 | /* |
2073 | * Can happen if a linked timeout fired and link had been like | |
2074 | * req -> link t-out -> link t-out [-> ...] | |
2075 | */ | |
c9abd7ad PB |
2076 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
2077 | struct io_timeout_data *io = link->async_data; | |
2078 | int ret; | |
7c86ffee | 2079 | |
f2f87370 | 2080 | io_remove_next_linked(req); |
90cd7e42 | 2081 | link->timeout.head = NULL; |
c9abd7ad PB |
2082 | ret = hrtimer_try_to_cancel(&io->timer); |
2083 | if (ret != -1) { | |
2084 | io_cqring_fill_event(link, -ECANCELED); | |
2085 | io_commit_cqring(ctx); | |
2086 | cancelled = true; | |
2087 | } | |
2088 | } | |
7c86ffee | 2089 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2090 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2091 | |
c9abd7ad | 2092 | if (cancelled) { |
7c86ffee | 2093 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2094 | io_put_req(link); |
2095 | } | |
7c86ffee PB |
2096 | } |
2097 | ||
9e645e11 | 2098 | |
d148ca4b | 2099 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2100 | { |
f2f87370 | 2101 | struct io_kiocb *link, *nxt; |
2665abfd | 2102 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2103 | unsigned long flags; |
9e645e11 | 2104 | |
d148ca4b | 2105 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2106 | link = req->link; |
2107 | req->link = NULL; | |
9e645e11 | 2108 | |
f2f87370 PB |
2109 | while (link) { |
2110 | nxt = link->link; | |
2111 | link->link = NULL; | |
2665abfd | 2112 | |
f2f87370 | 2113 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2114 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2115 | |
2116 | /* | |
2117 | * It's ok to free under spinlock as they're not linked anymore, | |
2118 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2119 | * work.fs->lock. | |
2120 | */ | |
2121 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2122 | io_put_req_deferred(link, 2); | |
2123 | else | |
2124 | io_double_put_req(link); | |
f2f87370 | 2125 | link = nxt; |
9e645e11 | 2126 | } |
2665abfd | 2127 | io_commit_cqring(ctx); |
216578e5 | 2128 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2129 | |
2665abfd | 2130 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2131 | } |
2132 | ||
3fa5e0f3 | 2133 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2134 | { |
7c86ffee PB |
2135 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2136 | io_kill_linked_timeout(req); | |
944e58bf | 2137 | |
9e645e11 JA |
2138 | /* |
2139 | * If LINK is set, we have dependent requests in this chain. If we | |
2140 | * didn't fail this request, queue the first one up, moving any other | |
2141 | * dependencies to the next request. In case of failure, fail the rest | |
2142 | * of the chain. | |
2143 | */ | |
f2f87370 PB |
2144 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2145 | struct io_kiocb *nxt = req->link; | |
2146 | ||
2147 | req->link = NULL; | |
2148 | return nxt; | |
2149 | } | |
9b5f7bd9 PB |
2150 | io_fail_links(req); |
2151 | return NULL; | |
4d7dd462 | 2152 | } |
9e645e11 | 2153 | |
f2f87370 | 2154 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2155 | { |
f2f87370 | 2156 | if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT))) |
3fa5e0f3 PB |
2157 | return NULL; |
2158 | return __io_req_find_next(req); | |
2159 | } | |
2160 | ||
355fb9e2 | 2161 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
2162 | { |
2163 | struct task_struct *tsk = req->task; | |
2164 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2165 | enum task_work_notify_mode notify; |
2166 | int ret; | |
c2c4c83c | 2167 | |
6200b0ae JA |
2168 | if (tsk->flags & PF_EXITING) |
2169 | return -ESRCH; | |
2170 | ||
c2c4c83c | 2171 | /* |
0ba9c9ed JA |
2172 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2173 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2174 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2175 | * will do the job. | |
c2c4c83c | 2176 | */ |
91989c70 | 2177 | notify = TWA_NONE; |
355fb9e2 | 2178 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
2179 | notify = TWA_SIGNAL; |
2180 | ||
87c4311f | 2181 | ret = task_work_add(tsk, &req->task_work, notify); |
c2c4c83c JA |
2182 | if (!ret) |
2183 | wake_up_process(tsk); | |
0ba9c9ed | 2184 | |
c2c4c83c JA |
2185 | return ret; |
2186 | } | |
2187 | ||
c40f6379 JA |
2188 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2189 | { | |
2190 | struct io_ring_ctx *ctx = req->ctx; | |
2191 | ||
2192 | spin_lock_irq(&ctx->completion_lock); | |
2193 | io_cqring_fill_event(req, error); | |
2194 | io_commit_cqring(ctx); | |
2195 | spin_unlock_irq(&ctx->completion_lock); | |
2196 | ||
2197 | io_cqring_ev_posted(ctx); | |
2198 | req_set_fail_links(req); | |
2199 | io_double_put_req(req); | |
2200 | } | |
2201 | ||
2202 | static void io_req_task_cancel(struct callback_head *cb) | |
2203 | { | |
2204 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2205 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2206 | |
2207 | __io_req_task_cancel(req, -ECANCELED); | |
87ceb6a6 | 2208 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2209 | } |
2210 | ||
2211 | static void __io_req_task_submit(struct io_kiocb *req) | |
2212 | { | |
2213 | struct io_ring_ctx *ctx = req->ctx; | |
2214 | ||
81b6d05c | 2215 | mutex_lock(&ctx->uring_lock); |
d9d05217 PB |
2216 | if (!ctx->sqo_dead && |
2217 | !__io_sq_thread_acquire_mm(ctx) && | |
2218 | !__io_sq_thread_acquire_files(ctx)) | |
c1379e24 | 2219 | __io_queue_sqe(req, NULL); |
81b6d05c | 2220 | else |
c40f6379 | 2221 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2222 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2223 | } |
2224 | ||
2225 | static void io_req_task_submit(struct callback_head *cb) | |
2226 | { | |
2227 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 2228 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2229 | |
2230 | __io_req_task_submit(req); | |
6d816e08 | 2231 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2232 | } |
2233 | ||
2234 | static void io_req_task_queue(struct io_kiocb *req) | |
2235 | { | |
c40f6379 JA |
2236 | int ret; |
2237 | ||
2238 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 2239 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 2240 | |
355fb9e2 | 2241 | ret = io_req_task_work_add(req); |
c40f6379 | 2242 | if (unlikely(ret)) { |
c2c4c83c JA |
2243 | struct task_struct *tsk; |
2244 | ||
c40f6379 JA |
2245 | init_task_work(&req->task_work, io_req_task_cancel); |
2246 | tsk = io_wq_get_task(req->ctx->io_wq); | |
91989c70 | 2247 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 2248 | wake_up_process(tsk); |
c40f6379 | 2249 | } |
c40f6379 JA |
2250 | } |
2251 | ||
f2f87370 | 2252 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2253 | { |
9b5f7bd9 | 2254 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2255 | |
2256 | if (nxt) | |
906a8c3f | 2257 | io_req_task_queue(nxt); |
c69f8dbe JL |
2258 | } |
2259 | ||
c3524383 | 2260 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2261 | { |
c3524383 PB |
2262 | io_queue_next(req); |
2263 | __io_free_req(req); | |
2264 | } | |
8766dd51 | 2265 | |
2d6500d4 PB |
2266 | struct req_batch { |
2267 | void *reqs[IO_IOPOLL_BATCH]; | |
2268 | int to_free; | |
7a743e22 | 2269 | |
5af1d13e PB |
2270 | struct task_struct *task; |
2271 | int task_refs; | |
2d6500d4 PB |
2272 | }; |
2273 | ||
5af1d13e PB |
2274 | static inline void io_init_req_batch(struct req_batch *rb) |
2275 | { | |
2276 | rb->to_free = 0; | |
2277 | rb->task_refs = 0; | |
2278 | rb->task = NULL; | |
2279 | } | |
2280 | ||
2d6500d4 PB |
2281 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
2282 | struct req_batch *rb) | |
2283 | { | |
2284 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
2285 | percpu_ref_put_many(&ctx->refs, rb->to_free); | |
2286 | rb->to_free = 0; | |
2287 | } | |
2288 | ||
2289 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
2290 | struct req_batch *rb) | |
2291 | { | |
2292 | if (rb->to_free) | |
2293 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e | 2294 | if (rb->task) { |
d8a6df10 JA |
2295 | struct io_uring_task *tctx = rb->task->io_uring; |
2296 | ||
2297 | percpu_counter_sub(&tctx->inflight, rb->task_refs); | |
c93cc9e1 JA |
2298 | if (atomic_read(&tctx->in_idle)) |
2299 | wake_up(&tctx->wait); | |
5af1d13e PB |
2300 | put_task_struct_many(rb->task, rb->task_refs); |
2301 | rb->task = NULL; | |
2302 | } | |
2d6500d4 PB |
2303 | } |
2304 | ||
2305 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
2306 | { | |
2307 | if (unlikely(io_is_fallback_req(req))) { | |
2308 | io_free_req(req); | |
2309 | return; | |
2310 | } | |
f2f87370 | 2311 | io_queue_next(req); |
2d6500d4 | 2312 | |
e3bc8e9d | 2313 | if (req->task != rb->task) { |
0f212204 | 2314 | if (rb->task) { |
d8a6df10 JA |
2315 | struct io_uring_task *tctx = rb->task->io_uring; |
2316 | ||
2317 | percpu_counter_sub(&tctx->inflight, rb->task_refs); | |
c93cc9e1 JA |
2318 | if (atomic_read(&tctx->in_idle)) |
2319 | wake_up(&tctx->wait); | |
e3bc8e9d | 2320 | put_task_struct_many(rb->task, rb->task_refs); |
5af1d13e | 2321 | } |
e3bc8e9d JA |
2322 | rb->task = req->task; |
2323 | rb->task_refs = 0; | |
5af1d13e | 2324 | } |
e3bc8e9d | 2325 | rb->task_refs++; |
5af1d13e | 2326 | |
4edf20f9 | 2327 | io_dismantle_req(req); |
2d6500d4 PB |
2328 | rb->reqs[rb->to_free++] = req; |
2329 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
2330 | __io_req_free_batch_flush(req->ctx, rb); | |
7a743e22 PB |
2331 | } |
2332 | ||
ba816ad6 JA |
2333 | /* |
2334 | * Drop reference to request, return next in chain (if there is one) if this | |
2335 | * was the last reference to this request. | |
2336 | */ | |
9b5f7bd9 | 2337 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2338 | { |
9b5f7bd9 PB |
2339 | struct io_kiocb *nxt = NULL; |
2340 | ||
2a44f467 | 2341 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2342 | nxt = io_req_find_next(req); |
4d7dd462 | 2343 | __io_free_req(req); |
2a44f467 | 2344 | } |
9b5f7bd9 | 2345 | return nxt; |
2b188cc1 JA |
2346 | } |
2347 | ||
e65ef56d JA |
2348 | static void io_put_req(struct io_kiocb *req) |
2349 | { | |
2350 | if (refcount_dec_and_test(&req->refs)) | |
2351 | io_free_req(req); | |
2b188cc1 JA |
2352 | } |
2353 | ||
216578e5 PB |
2354 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2355 | { | |
2356 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2357 | ||
2358 | io_free_req(req); | |
2359 | } | |
2360 | ||
2361 | static void io_free_req_deferred(struct io_kiocb *req) | |
2362 | { | |
2363 | int ret; | |
2364 | ||
2365 | init_task_work(&req->task_work, io_put_req_deferred_cb); | |
355fb9e2 | 2366 | ret = io_req_task_work_add(req); |
216578e5 PB |
2367 | if (unlikely(ret)) { |
2368 | struct task_struct *tsk; | |
2369 | ||
2370 | tsk = io_wq_get_task(req->ctx->io_wq); | |
91989c70 | 2371 | task_work_add(tsk, &req->task_work, TWA_NONE); |
216578e5 PB |
2372 | wake_up_process(tsk); |
2373 | } | |
2374 | } | |
2375 | ||
2376 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2377 | { | |
2378 | if (refcount_sub_and_test(refs, &req->refs)) | |
2379 | io_free_req_deferred(req); | |
2380 | } | |
2381 | ||
f4db7182 | 2382 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
7a743e22 | 2383 | { |
6df1db6b | 2384 | struct io_kiocb *nxt; |
f4db7182 | 2385 | |
7a743e22 | 2386 | /* |
f4db7182 PB |
2387 | * A ref is owned by io-wq in which context we're. So, if that's the |
2388 | * last one, it's safe to steal next work. False negatives are Ok, | |
2389 | * it just will be re-punted async in io_put_work() | |
7a743e22 | 2390 | */ |
f4db7182 PB |
2391 | if (refcount_read(&req->refs) != 1) |
2392 | return NULL; | |
7a743e22 | 2393 | |
9b5f7bd9 | 2394 | nxt = io_req_find_next(req); |
6df1db6b | 2395 | return nxt ? &nxt->work : NULL; |
7a743e22 PB |
2396 | } |
2397 | ||
978db57e JA |
2398 | static void io_double_put_req(struct io_kiocb *req) |
2399 | { | |
2400 | /* drop both submit and complete references */ | |
2401 | if (refcount_sub_and_test(2, &req->refs)) | |
2402 | io_free_req(req); | |
2403 | } | |
2404 | ||
6c503150 | 2405 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2406 | { |
2407 | /* See comment at the top of this file */ | |
2408 | smp_rmb(); | |
e23de15f | 2409 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2410 | } |
2411 | ||
fb5ccc98 PB |
2412 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2413 | { | |
2414 | struct io_rings *rings = ctx->rings; | |
2415 | ||
2416 | /* make sure SQ entry isn't read before tail */ | |
2417 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2418 | } | |
2419 | ||
8ff069bf | 2420 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2421 | { |
8ff069bf | 2422 | unsigned int cflags; |
e94f141b | 2423 | |
bcda7baa JA |
2424 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2425 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2426 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2427 | kfree(kbuf); |
2428 | return cflags; | |
e94f141b JA |
2429 | } |
2430 | ||
8ff069bf | 2431 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2432 | { |
4d954c25 | 2433 | struct io_buffer *kbuf; |
bcda7baa | 2434 | |
4d954c25 | 2435 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2436 | return io_put_kbuf(req, kbuf); |
2437 | } | |
2438 | ||
4c6e277c JA |
2439 | static inline bool io_run_task_work(void) |
2440 | { | |
6200b0ae JA |
2441 | /* |
2442 | * Not safe to run on exiting task, and the task_work handling will | |
2443 | * not add work to such a task. | |
2444 | */ | |
2445 | if (unlikely(current->flags & PF_EXITING)) | |
2446 | return false; | |
4c6e277c JA |
2447 | if (current->task_works) { |
2448 | __set_current_state(TASK_RUNNING); | |
2449 | task_work_run(); | |
2450 | return true; | |
2451 | } | |
2452 | ||
2453 | return false; | |
bcda7baa JA |
2454 | } |
2455 | ||
bbde017a XW |
2456 | static void io_iopoll_queue(struct list_head *again) |
2457 | { | |
2458 | struct io_kiocb *req; | |
2459 | ||
2460 | do { | |
d21ffe7e PB |
2461 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2462 | list_del(&req->inflight_entry); | |
81b68a5c | 2463 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
bbde017a XW |
2464 | } while (!list_empty(again)); |
2465 | } | |
2466 | ||
def596e9 JA |
2467 | /* |
2468 | * Find and free completed poll iocbs | |
2469 | */ | |
2470 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2471 | struct list_head *done) | |
2472 | { | |
8237e045 | 2473 | struct req_batch rb; |
def596e9 | 2474 | struct io_kiocb *req; |
bbde017a XW |
2475 | LIST_HEAD(again); |
2476 | ||
2477 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2478 | smp_rmb(); | |
def596e9 | 2479 | |
5af1d13e | 2480 | io_init_req_batch(&rb); |
def596e9 | 2481 | while (!list_empty(done)) { |
bcda7baa JA |
2482 | int cflags = 0; |
2483 | ||
d21ffe7e | 2484 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a | 2485 | if (READ_ONCE(req->result) == -EAGAIN) { |
56450c20 | 2486 | req->result = 0; |
bbde017a | 2487 | req->iopoll_completed = 0; |
d21ffe7e | 2488 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2489 | continue; |
2490 | } | |
d21ffe7e | 2491 | list_del(&req->inflight_entry); |
def596e9 | 2492 | |
bcda7baa | 2493 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2494 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2495 | |
2496 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2497 | (*nr_events)++; |
2498 | ||
c3524383 | 2499 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2500 | io_req_free_batch(&rb, req); |
def596e9 | 2501 | } |
def596e9 | 2502 | |
09bb8394 | 2503 | io_commit_cqring(ctx); |
80c18e4a | 2504 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2505 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2506 | |
bbde017a XW |
2507 | if (!list_empty(&again)) |
2508 | io_iopoll_queue(&again); | |
581f9810 BM |
2509 | } |
2510 | ||
def596e9 JA |
2511 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2512 | long min) | |
2513 | { | |
2514 | struct io_kiocb *req, *tmp; | |
2515 | LIST_HEAD(done); | |
2516 | bool spin; | |
2517 | int ret; | |
2518 | ||
2519 | /* | |
2520 | * Only spin for completions if we don't have multiple devices hanging | |
2521 | * off our complete list, and we're under the requested amount. | |
2522 | */ | |
2523 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2524 | ||
2525 | ret = 0; | |
d21ffe7e | 2526 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2527 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2528 | |
2529 | /* | |
581f9810 BM |
2530 | * Move completed and retryable entries to our local lists. |
2531 | * If we find a request that requires polling, break out | |
2532 | * and complete those lists first, if we have entries there. | |
def596e9 | 2533 | */ |
65a6543d | 2534 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2535 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2536 | continue; |
2537 | } | |
2538 | if (!list_empty(&done)) | |
2539 | break; | |
2540 | ||
2541 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2542 | if (ret < 0) | |
2543 | break; | |
2544 | ||
3aadc23e PB |
2545 | /* iopoll may have completed current req */ |
2546 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2547 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2548 | |
def596e9 JA |
2549 | if (ret && spin) |
2550 | spin = false; | |
2551 | ret = 0; | |
2552 | } | |
2553 | ||
2554 | if (!list_empty(&done)) | |
2555 | io_iopoll_complete(ctx, nr_events, &done); | |
2556 | ||
2557 | return ret; | |
2558 | } | |
2559 | ||
2560 | /* | |
d195a66e | 2561 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2562 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2563 | * as a non-spinning completion check. | |
2564 | */ | |
2565 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2566 | long min) | |
2567 | { | |
540e32a0 | 2568 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2569 | int ret; |
2570 | ||
2571 | ret = io_do_iopoll(ctx, nr_events, min); | |
2572 | if (ret < 0) | |
2573 | return ret; | |
eba0a4dd | 2574 | if (*nr_events >= min) |
def596e9 JA |
2575 | return 0; |
2576 | } | |
2577 | ||
2578 | return 1; | |
2579 | } | |
2580 | ||
2581 | /* | |
2582 | * We can't just wait for polled events to come to us, we have to actively | |
2583 | * find and complete them. | |
2584 | */ | |
b2edc0a7 | 2585 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2586 | { |
2587 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2588 | return; | |
2589 | ||
2590 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2591 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2592 | unsigned int nr_events = 0; |
2593 | ||
b2edc0a7 | 2594 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2595 | |
b2edc0a7 PB |
2596 | /* let it sleep and repeat later if can't complete a request */ |
2597 | if (nr_events == 0) | |
2598 | break; | |
08f5439f JA |
2599 | /* |
2600 | * Ensure we allow local-to-the-cpu processing to take place, | |
2601 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2602 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2603 | */ |
3fcee5a6 PB |
2604 | if (need_resched()) { |
2605 | mutex_unlock(&ctx->uring_lock); | |
2606 | cond_resched(); | |
2607 | mutex_lock(&ctx->uring_lock); | |
2608 | } | |
def596e9 JA |
2609 | } |
2610 | mutex_unlock(&ctx->uring_lock); | |
2611 | } | |
2612 | ||
7668b92a | 2613 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2614 | { |
7668b92a | 2615 | unsigned int nr_events = 0; |
2b2ed975 | 2616 | int iters = 0, ret = 0; |
500f9fba | 2617 | |
c7849be9 XW |
2618 | /* |
2619 | * We disallow the app entering submit/complete with polling, but we | |
2620 | * still need to lock the ring to prevent racing with polled issue | |
2621 | * that got punted to a workqueue. | |
2622 | */ | |
2623 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2624 | do { |
a3a0e43f JA |
2625 | /* |
2626 | * Don't enter poll loop if we already have events pending. | |
2627 | * If we do, we can potentially be spinning for commands that | |
2628 | * already triggered a CQE (eg in error). | |
2629 | */ | |
6c503150 PB |
2630 | if (test_bit(0, &ctx->cq_check_overflow)) |
2631 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2632 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2633 | break; |
2634 | ||
500f9fba JA |
2635 | /* |
2636 | * If a submit got punted to a workqueue, we can have the | |
2637 | * application entering polling for a command before it gets | |
2638 | * issued. That app will hold the uring_lock for the duration | |
2639 | * of the poll right here, so we need to take a breather every | |
2640 | * now and then to ensure that the issue has a chance to add | |
2641 | * the poll to the issued list. Otherwise we can spin here | |
2642 | * forever, while the workqueue is stuck trying to acquire the | |
2643 | * very same mutex. | |
2644 | */ | |
2645 | if (!(++iters & 7)) { | |
2646 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2647 | io_run_task_work(); |
500f9fba JA |
2648 | mutex_lock(&ctx->uring_lock); |
2649 | } | |
2650 | ||
7668b92a | 2651 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2652 | if (ret <= 0) |
2653 | break; | |
2654 | ret = 0; | |
7668b92a | 2655 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2656 | |
500f9fba | 2657 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2658 | return ret; |
2659 | } | |
2660 | ||
491381ce | 2661 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2662 | { |
491381ce JA |
2663 | /* |
2664 | * Tell lockdep we inherited freeze protection from submission | |
2665 | * thread. | |
2666 | */ | |
2667 | if (req->flags & REQ_F_ISREG) { | |
2668 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2669 | |
491381ce | 2670 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2671 | } |
491381ce | 2672 | file_end_write(req->file); |
2b188cc1 JA |
2673 | } |
2674 | ||
a1d7c393 JA |
2675 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
2676 | struct io_comp_state *cs) | |
2b188cc1 | 2677 | { |
9adbd45d | 2678 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2679 | int cflags = 0; |
2b188cc1 | 2680 | |
491381ce JA |
2681 | if (kiocb->ki_flags & IOCB_WRITE) |
2682 | kiocb_end_write(req); | |
2b188cc1 | 2683 | |
4e88d6e7 JA |
2684 | if (res != req->result) |
2685 | req_set_fail_links(req); | |
bcda7baa | 2686 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2687 | cflags = io_put_rw_kbuf(req); |
a1d7c393 | 2688 | __io_req_complete(req, res, cflags, cs); |
ba816ad6 JA |
2689 | } |
2690 | ||
b63534c4 | 2691 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2692 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2693 | { |
2694 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
2695 | ssize_t ret = -ECANCELED; | |
2696 | struct iov_iter iter; | |
2697 | int rw; | |
2698 | ||
dc2a6e9a PB |
2699 | /* already prepared */ |
2700 | if (req->async_data) | |
2701 | return true; | |
b63534c4 JA |
2702 | |
2703 | switch (req->opcode) { | |
2704 | case IORING_OP_READV: | |
2705 | case IORING_OP_READ_FIXED: | |
2706 | case IORING_OP_READ: | |
2707 | rw = READ; | |
2708 | break; | |
2709 | case IORING_OP_WRITEV: | |
2710 | case IORING_OP_WRITE_FIXED: | |
2711 | case IORING_OP_WRITE: | |
2712 | rw = WRITE; | |
2713 | break; | |
2714 | default: | |
2715 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2716 | req->opcode); | |
dc2a6e9a | 2717 | return false; |
b63534c4 JA |
2718 | } |
2719 | ||
dc2a6e9a PB |
2720 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2721 | if (ret < 0) | |
2722 | return false; | |
2723 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); | |
2724 | if (!ret) | |
b63534c4 | 2725 | return true; |
dc2a6e9a | 2726 | kfree(iovec); |
b63534c4 JA |
2727 | return false; |
2728 | } | |
b63534c4 JA |
2729 | #endif |
2730 | ||
2731 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2732 | { | |
2733 | #ifdef CONFIG_BLOCK | |
bf6182b6 | 2734 | umode_t mode; |
b63534c4 JA |
2735 | int ret; |
2736 | ||
bf6182b6 | 2737 | if (res != -EAGAIN && res != -EOPNOTSUPP) |
355afaeb | 2738 | return false; |
bf6182b6 PB |
2739 | mode = file_inode(req->file)->i_mode; |
2740 | if ((!S_ISBLK(mode) && !S_ISREG(mode)) || io_wq_current_is_worker()) | |
b63534c4 JA |
2741 | return false; |
2742 | ||
55e6ac1e PB |
2743 | lockdep_assert_held(&req->ctx->uring_lock); |
2744 | ||
28cea78a | 2745 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2746 | |
dc2a6e9a | 2747 | if (!ret && io_resubmit_prep(req)) { |
fdee946d JA |
2748 | refcount_inc(&req->refs); |
2749 | io_queue_async_work(req); | |
b63534c4 | 2750 | return true; |
fdee946d | 2751 | } |
dc2a6e9a | 2752 | req_set_fail_links(req); |
b63534c4 JA |
2753 | #endif |
2754 | return false; | |
2755 | } | |
2756 | ||
a1d7c393 JA |
2757 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2758 | struct io_comp_state *cs) | |
2759 | { | |
2760 | if (!io_rw_reissue(req, res)) | |
2761 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2762 | } |
2763 | ||
2764 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2765 | { | |
9adbd45d | 2766 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2767 | |
a1d7c393 | 2768 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2769 | } |
2770 | ||
def596e9 JA |
2771 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2772 | { | |
9adbd45d | 2773 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2774 | |
491381ce JA |
2775 | if (kiocb->ki_flags & IOCB_WRITE) |
2776 | kiocb_end_write(req); | |
def596e9 | 2777 | |
2d7d6792 | 2778 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2779 | req_set_fail_links(req); |
bbde017a XW |
2780 | |
2781 | WRITE_ONCE(req->result, res); | |
2782 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2783 | smp_wmb(); |
2784 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2785 | } |
2786 | ||
2787 | /* | |
2788 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2789 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2790 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2791 | * accessing the kiocb cookie. | |
2792 | */ | |
2e9dbe90 | 2793 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2794 | { |
2795 | struct io_ring_ctx *ctx = req->ctx; | |
2796 | ||
2797 | /* | |
2798 | * Track whether we have multiple files in our lists. This will impact | |
2799 | * how we do polling eventually, not spinning if we're on potentially | |
2800 | * different devices. | |
2801 | */ | |
540e32a0 | 2802 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2803 | ctx->poll_multi_file = false; |
2804 | } else if (!ctx->poll_multi_file) { | |
2805 | struct io_kiocb *list_req; | |
2806 | ||
540e32a0 | 2807 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2808 | inflight_entry); |
9adbd45d | 2809 | if (list_req->file != req->file) |
def596e9 JA |
2810 | ctx->poll_multi_file = true; |
2811 | } | |
2812 | ||
2813 | /* | |
2814 | * For fast devices, IO may have already completed. If it has, add | |
2815 | * it to the front so we find it first. | |
2816 | */ | |
65a6543d | 2817 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2818 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2819 | else |
d21ffe7e | 2820 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2821 | |
2e9dbe90 XW |
2822 | /* |
2823 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2824 | * task context or in io worker task context. If current task context is | |
2825 | * sq thread, we don't need to check whether should wake up sq thread. | |
2826 | */ | |
2827 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2828 | wq_has_sleeper(&ctx->sq_data->wait)) |
2829 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2830 | } |
2831 | ||
6e1271e6 | 2832 | static inline void __io_state_file_put(struct io_submit_state *state) |
9a56a232 | 2833 | { |
6e1271e6 PB |
2834 | fput_many(state->file, state->file_refs); |
2835 | state->file_refs = 0; | |
9f13c35b PB |
2836 | } |
2837 | ||
2838 | static inline void io_state_file_put(struct io_submit_state *state) | |
2839 | { | |
6e1271e6 | 2840 | if (state->file_refs) |
9f13c35b | 2841 | __io_state_file_put(state); |
9a56a232 JA |
2842 | } |
2843 | ||
2844 | /* | |
2845 | * Get as many references to a file as we have IOs left in this submission, | |
2846 | * assuming most submissions are for one file, or at least that each file | |
2847 | * has more than one submission. | |
2848 | */ | |
8da11c19 | 2849 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2850 | { |
2851 | if (!state) | |
2852 | return fget(fd); | |
2853 | ||
6e1271e6 | 2854 | if (state->file_refs) { |
9a56a232 | 2855 | if (state->fd == fd) { |
6e1271e6 | 2856 | state->file_refs--; |
9a56a232 JA |
2857 | return state->file; |
2858 | } | |
9f13c35b | 2859 | __io_state_file_put(state); |
9a56a232 JA |
2860 | } |
2861 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2862 | if (unlikely(!state->file)) |
9a56a232 JA |
2863 | return NULL; |
2864 | ||
2865 | state->fd = fd; | |
6e1271e6 | 2866 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2867 | return state->file; |
2868 | } | |
2869 | ||
4503b767 JA |
2870 | static bool io_bdev_nowait(struct block_device *bdev) |
2871 | { | |
9ba0d0c8 | 2872 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2873 | } |
2874 | ||
2b188cc1 JA |
2875 | /* |
2876 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2877 | * any file. For now, just ensure that anything potentially problematic is done | |
2878 | * inline. | |
2879 | */ | |
af197f50 | 2880 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2881 | { |
2882 | umode_t mode = file_inode(file)->i_mode; | |
2883 | ||
4503b767 | 2884 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2885 | if (IS_ENABLED(CONFIG_BLOCK) && |
2886 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2887 | return true; |
2888 | return false; | |
2889 | } | |
2890 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2891 | return true; |
4503b767 | 2892 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2893 | if (IS_ENABLED(CONFIG_BLOCK) && |
2894 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2895 | file->f_op != &io_uring_fops) |
2896 | return true; | |
2897 | return false; | |
2898 | } | |
2b188cc1 | 2899 | |
c5b85625 JA |
2900 | /* any ->read/write should understand O_NONBLOCK */ |
2901 | if (file->f_flags & O_NONBLOCK) | |
2902 | return true; | |
2903 | ||
af197f50 JA |
2904 | if (!(file->f_mode & FMODE_NOWAIT)) |
2905 | return false; | |
2906 | ||
2907 | if (rw == READ) | |
2908 | return file->f_op->read_iter != NULL; | |
2909 | ||
2910 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2911 | } |
2912 | ||
a88fc400 | 2913 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2914 | { |
def596e9 | 2915 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2916 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2917 | unsigned ioprio; |
2918 | int ret; | |
2b188cc1 | 2919 | |
491381ce JA |
2920 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2921 | req->flags |= REQ_F_ISREG; | |
2922 | ||
2b188cc1 | 2923 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2924 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2925 | req->flags |= REQ_F_CUR_POS; | |
2926 | kiocb->ki_pos = req->file->f_pos; | |
2927 | } | |
2b188cc1 | 2928 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2929 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2930 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2931 | if (unlikely(ret)) | |
2932 | return ret; | |
2b188cc1 JA |
2933 | |
2934 | ioprio = READ_ONCE(sqe->ioprio); | |
2935 | if (ioprio) { | |
2936 | ret = ioprio_check_cap(ioprio); | |
2937 | if (ret) | |
09bb8394 | 2938 | return ret; |
2b188cc1 JA |
2939 | |
2940 | kiocb->ki_ioprio = ioprio; | |
2941 | } else | |
2942 | kiocb->ki_ioprio = get_current_ioprio(); | |
2943 | ||
8449eeda | 2944 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2945 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2946 | req->flags |= REQ_F_NOWAIT; |
2947 | ||
def596e9 | 2948 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2949 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2950 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2951 | return -EOPNOTSUPP; |
2b188cc1 | 2952 | |
def596e9 JA |
2953 | kiocb->ki_flags |= IOCB_HIPRI; |
2954 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2955 | req->iopoll_completed = 0; |
def596e9 | 2956 | } else { |
09bb8394 JA |
2957 | if (kiocb->ki_flags & IOCB_HIPRI) |
2958 | return -EINVAL; | |
def596e9 JA |
2959 | kiocb->ki_complete = io_complete_rw; |
2960 | } | |
9adbd45d | 2961 | |
3529d8c2 JA |
2962 | req->rw.addr = READ_ONCE(sqe->addr); |
2963 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2964 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2965 | return 0; |
2b188cc1 JA |
2966 | } |
2967 | ||
2968 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2969 | { | |
2970 | switch (ret) { | |
2971 | case -EIOCBQUEUED: | |
2972 | break; | |
2973 | case -ERESTARTSYS: | |
2974 | case -ERESTARTNOINTR: | |
2975 | case -ERESTARTNOHAND: | |
2976 | case -ERESTART_RESTARTBLOCK: | |
2977 | /* | |
2978 | * We can't just restart the syscall, since previously | |
2979 | * submitted sqes may already be in progress. Just fail this | |
2980 | * IO with EINTR. | |
2981 | */ | |
2982 | ret = -EINTR; | |
df561f66 | 2983 | fallthrough; |
2b188cc1 JA |
2984 | default: |
2985 | kiocb->ki_complete(kiocb, ret, 0); | |
2986 | } | |
2987 | } | |
2988 | ||
a1d7c393 JA |
2989 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2990 | struct io_comp_state *cs) | |
ba816ad6 | 2991 | { |
ba04291e | 2992 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2993 | struct io_async_rw *io = req->async_data; |
ba04291e | 2994 | |
227c0c96 | 2995 | /* add previously done IO, if any */ |
e8c2bc1f | 2996 | if (io && io->bytes_done > 0) { |
227c0c96 | 2997 | if (ret < 0) |
e8c2bc1f | 2998 | ret = io->bytes_done; |
227c0c96 | 2999 | else |
e8c2bc1f | 3000 | ret += io->bytes_done; |
227c0c96 JA |
3001 | } |
3002 | ||
ba04291e JA |
3003 | if (req->flags & REQ_F_CUR_POS) |
3004 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 3005 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 3006 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
3007 | else |
3008 | io_rw_done(kiocb, ret); | |
3009 | } | |
3010 | ||
9adbd45d | 3011 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 3012 | struct iov_iter *iter) |
edafccee | 3013 | { |
9adbd45d JA |
3014 | struct io_ring_ctx *ctx = req->ctx; |
3015 | size_t len = req->rw.len; | |
edafccee | 3016 | struct io_mapped_ubuf *imu; |
4be1c615 | 3017 | u16 index, buf_index = req->buf_index; |
edafccee JA |
3018 | size_t offset; |
3019 | u64 buf_addr; | |
3020 | ||
edafccee JA |
3021 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3022 | return -EFAULT; | |
edafccee JA |
3023 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3024 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 3025 | buf_addr = req->rw.addr; |
edafccee JA |
3026 | |
3027 | /* overflow */ | |
3028 | if (buf_addr + len < buf_addr) | |
3029 | return -EFAULT; | |
3030 | /* not inside the mapped region */ | |
3031 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
3032 | return -EFAULT; | |
3033 | ||
3034 | /* | |
3035 | * May not be a start of buffer, set size appropriately | |
3036 | * and advance us to the beginning. | |
3037 | */ | |
3038 | offset = buf_addr - imu->ubuf; | |
3039 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3040 | |
3041 | if (offset) { | |
3042 | /* | |
3043 | * Don't use iov_iter_advance() here, as it's really slow for | |
3044 | * using the latter parts of a big fixed buffer - it iterates | |
3045 | * over each segment manually. We can cheat a bit here, because | |
3046 | * we know that: | |
3047 | * | |
3048 | * 1) it's a BVEC iter, we set it up | |
3049 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3050 | * first and last bvec | |
3051 | * | |
3052 | * So just find our index, and adjust the iterator afterwards. | |
3053 | * If the offset is within the first bvec (or the whole first | |
3054 | * bvec, just use iov_iter_advance(). This makes it easier | |
3055 | * since we can just skip the first segment, which may not | |
3056 | * be PAGE_SIZE aligned. | |
3057 | */ | |
3058 | const struct bio_vec *bvec = imu->bvec; | |
3059 | ||
3060 | if (offset <= bvec->bv_len) { | |
3061 | iov_iter_advance(iter, offset); | |
3062 | } else { | |
3063 | unsigned long seg_skip; | |
3064 | ||
3065 | /* skip first vec */ | |
3066 | offset -= bvec->bv_len; | |
3067 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3068 | ||
3069 | iter->bvec = bvec + seg_skip; | |
3070 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3071 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3072 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3073 | } |
3074 | } | |
3075 | ||
5e559561 | 3076 | return len; |
edafccee JA |
3077 | } |
3078 | ||
bcda7baa JA |
3079 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3080 | { | |
3081 | if (needs_lock) | |
3082 | mutex_unlock(&ctx->uring_lock); | |
3083 | } | |
3084 | ||
3085 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3086 | { | |
3087 | /* | |
3088 | * "Normal" inline submissions always hold the uring_lock, since we | |
3089 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3090 | * The only exception is when we've detached the request and issue it | |
3091 | * from an async worker thread, grab the lock for that case. | |
3092 | */ | |
3093 | if (needs_lock) | |
3094 | mutex_lock(&ctx->uring_lock); | |
3095 | } | |
3096 | ||
3097 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3098 | int bgid, struct io_buffer *kbuf, | |
3099 | bool needs_lock) | |
3100 | { | |
3101 | struct io_buffer *head; | |
3102 | ||
3103 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3104 | return kbuf; | |
3105 | ||
3106 | io_ring_submit_lock(req->ctx, needs_lock); | |
3107 | ||
3108 | lockdep_assert_held(&req->ctx->uring_lock); | |
3109 | ||
3110 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3111 | if (head) { | |
3112 | if (!list_empty(&head->list)) { | |
3113 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3114 | list); | |
3115 | list_del(&kbuf->list); | |
3116 | } else { | |
3117 | kbuf = head; | |
3118 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3119 | } | |
3120 | if (*len > kbuf->len) | |
3121 | *len = kbuf->len; | |
3122 | } else { | |
3123 | kbuf = ERR_PTR(-ENOBUFS); | |
3124 | } | |
3125 | ||
3126 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3127 | ||
3128 | return kbuf; | |
3129 | } | |
3130 | ||
4d954c25 JA |
3131 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3132 | bool needs_lock) | |
3133 | { | |
3134 | struct io_buffer *kbuf; | |
4f4eeba8 | 3135 | u16 bgid; |
4d954c25 JA |
3136 | |
3137 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3138 | bgid = req->buf_index; |
4d954c25 JA |
3139 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3140 | if (IS_ERR(kbuf)) | |
3141 | return kbuf; | |
3142 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3143 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3144 | return u64_to_user_ptr(kbuf->addr); | |
3145 | } | |
3146 | ||
3147 | #ifdef CONFIG_COMPAT | |
3148 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3149 | bool needs_lock) | |
3150 | { | |
3151 | struct compat_iovec __user *uiov; | |
3152 | compat_ssize_t clen; | |
3153 | void __user *buf; | |
3154 | ssize_t len; | |
3155 | ||
3156 | uiov = u64_to_user_ptr(req->rw.addr); | |
3157 | if (!access_ok(uiov, sizeof(*uiov))) | |
3158 | return -EFAULT; | |
3159 | if (__get_user(clen, &uiov->iov_len)) | |
3160 | return -EFAULT; | |
3161 | if (clen < 0) | |
3162 | return -EINVAL; | |
3163 | ||
3164 | len = clen; | |
3165 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3166 | if (IS_ERR(buf)) | |
3167 | return PTR_ERR(buf); | |
3168 | iov[0].iov_base = buf; | |
3169 | iov[0].iov_len = (compat_size_t) len; | |
3170 | return 0; | |
3171 | } | |
3172 | #endif | |
3173 | ||
3174 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3175 | bool needs_lock) | |
3176 | { | |
3177 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3178 | void __user *buf; | |
3179 | ssize_t len; | |
3180 | ||
3181 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3182 | return -EFAULT; | |
3183 | ||
3184 | len = iov[0].iov_len; | |
3185 | if (len < 0) | |
3186 | return -EINVAL; | |
3187 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3188 | if (IS_ERR(buf)) | |
3189 | return PTR_ERR(buf); | |
3190 | iov[0].iov_base = buf; | |
3191 | iov[0].iov_len = len; | |
3192 | return 0; | |
3193 | } | |
3194 | ||
3195 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3196 | bool needs_lock) | |
3197 | { | |
dddb3e26 JA |
3198 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3199 | struct io_buffer *kbuf; | |
3200 | ||
3201 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3202 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3203 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3204 | return 0; |
dddb3e26 | 3205 | } |
dd201662 | 3206 | if (req->rw.len != 1) |
4d954c25 JA |
3207 | return -EINVAL; |
3208 | ||
3209 | #ifdef CONFIG_COMPAT | |
3210 | if (req->ctx->compat) | |
3211 | return io_compat_import(req, iov, needs_lock); | |
3212 | #endif | |
3213 | ||
3214 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3215 | } | |
3216 | ||
2846c481 | 3217 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
8452fd0c JA |
3218 | struct iovec **iovec, struct iov_iter *iter, |
3219 | bool needs_lock) | |
2b188cc1 | 3220 | { |
9adbd45d JA |
3221 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3222 | size_t sqe_len = req->rw.len; | |
4d954c25 | 3223 | ssize_t ret; |
edafccee JA |
3224 | u8 opcode; |
3225 | ||
d625c6ee | 3226 | opcode = req->opcode; |
7d009165 | 3227 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3228 | *iovec = NULL; |
9adbd45d | 3229 | return io_import_fixed(req, rw, iter); |
edafccee | 3230 | } |
2b188cc1 | 3231 | |
bcda7baa | 3232 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3233 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3234 | return -EINVAL; |
3235 | ||
3a6820f2 | 3236 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3237 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3238 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3239 | if (IS_ERR(buf)) |
4d954c25 | 3240 | return PTR_ERR(buf); |
3f9d6441 | 3241 | req->rw.len = sqe_len; |
bcda7baa JA |
3242 | } |
3243 | ||
3a6820f2 JA |
3244 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3245 | *iovec = NULL; | |
10fc72e4 | 3246 | return ret; |
3a6820f2 JA |
3247 | } |
3248 | ||
4d954c25 JA |
3249 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3250 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
3251 | if (!ret) { |
3252 | ret = (*iovec)->iov_len; | |
3253 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
3254 | } | |
4d954c25 JA |
3255 | *iovec = NULL; |
3256 | return ret; | |
3257 | } | |
3258 | ||
89cd35c5 CH |
3259 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3260 | req->ctx->compat); | |
2b188cc1 JA |
3261 | } |
3262 | ||
0fef9483 JA |
3263 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3264 | { | |
5b09e37e | 3265 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3266 | } |
3267 | ||
31b51510 | 3268 | /* |
32960613 JA |
3269 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3270 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3271 | */ |
4017eb91 | 3272 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3273 | { |
4017eb91 JA |
3274 | struct kiocb *kiocb = &req->rw.kiocb; |
3275 | struct file *file = req->file; | |
32960613 JA |
3276 | ssize_t ret = 0; |
3277 | ||
3278 | /* | |
3279 | * Don't support polled IO through this interface, and we can't | |
3280 | * support non-blocking either. For the latter, this just causes | |
3281 | * the kiocb to be handled from an async context. | |
3282 | */ | |
3283 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3284 | return -EOPNOTSUPP; | |
3285 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3286 | return -EAGAIN; | |
3287 | ||
3288 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3289 | struct iovec iovec; |
32960613 JA |
3290 | ssize_t nr; |
3291 | ||
311ae9e1 PB |
3292 | if (!iov_iter_is_bvec(iter)) { |
3293 | iovec = iov_iter_iovec(iter); | |
3294 | } else { | |
4017eb91 JA |
3295 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3296 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3297 | } |
3298 | ||
32960613 JA |
3299 | if (rw == READ) { |
3300 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3301 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3302 | } else { |
3303 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3304 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3305 | } |
3306 | ||
3307 | if (nr < 0) { | |
3308 | if (!ret) | |
3309 | ret = nr; | |
3310 | break; | |
3311 | } | |
3312 | ret += nr; | |
3313 | if (nr != iovec.iov_len) | |
3314 | break; | |
4017eb91 JA |
3315 | req->rw.len -= nr; |
3316 | req->rw.addr += nr; | |
32960613 JA |
3317 | iov_iter_advance(iter, nr); |
3318 | } | |
3319 | ||
3320 | return ret; | |
3321 | } | |
3322 | ||
ff6165b2 JA |
3323 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3324 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3325 | { |
e8c2bc1f | 3326 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3327 | |
ff6165b2 | 3328 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3329 | rw->free_iovec = iovec; |
227c0c96 | 3330 | rw->bytes_done = 0; |
ff6165b2 | 3331 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3332 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3333 | return; |
b64e3444 | 3334 | if (!iovec) { |
ff6165b2 JA |
3335 | unsigned iov_off = 0; |
3336 | ||
3337 | rw->iter.iov = rw->fast_iov; | |
3338 | if (iter->iov != fast_iov) { | |
3339 | iov_off = iter->iov - fast_iov; | |
3340 | rw->iter.iov += iov_off; | |
3341 | } | |
3342 | if (rw->fast_iov != fast_iov) | |
3343 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3344 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3345 | } else { |
3346 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3347 | } |
3348 | } | |
3349 | ||
e8c2bc1f | 3350 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3351 | { |
e8c2bc1f JA |
3352 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3353 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3354 | return req->async_data == NULL; | |
3d9932a8 XW |
3355 | } |
3356 | ||
e8c2bc1f | 3357 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3358 | { |
e8c2bc1f | 3359 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3360 | return 0; |
3d9932a8 | 3361 | |
e8c2bc1f | 3362 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3363 | } |
3364 | ||
ff6165b2 JA |
3365 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3366 | const struct iovec *fast_iov, | |
227c0c96 | 3367 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3368 | { |
e8c2bc1f | 3369 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3370 | return 0; |
e8c2bc1f JA |
3371 | if (!req->async_data) { |
3372 | if (__io_alloc_async_data(req)) | |
5d204bcf | 3373 | return -ENOMEM; |
b7bb4f7d | 3374 | |
ff6165b2 | 3375 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3376 | } |
b7bb4f7d | 3377 | return 0; |
f67676d1 JA |
3378 | } |
3379 | ||
73debe68 | 3380 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3381 | { |
e8c2bc1f | 3382 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3383 | struct iovec *iov = iorw->fast_iov; |
c3e330a4 PB |
3384 | ssize_t ret; |
3385 | ||
2846c481 | 3386 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3387 | if (unlikely(ret < 0)) |
3388 | return ret; | |
3389 | ||
ab0b196c PB |
3390 | iorw->bytes_done = 0; |
3391 | iorw->free_iovec = iov; | |
3392 | if (iov) | |
3393 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3394 | return 0; |
3395 | } | |
3396 | ||
73debe68 | 3397 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3398 | { |
3399 | ssize_t ret; | |
3400 | ||
a88fc400 | 3401 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3402 | if (ret) |
3403 | return ret; | |
f67676d1 | 3404 | |
3529d8c2 JA |
3405 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3406 | return -EBADF; | |
f67676d1 | 3407 | |
5f798bea | 3408 | /* either don't need iovec imported or already have it */ |
2d199895 | 3409 | if (!req->async_data) |
3529d8c2 | 3410 | return 0; |
73debe68 | 3411 | return io_rw_prep_async(req, READ); |
f67676d1 JA |
3412 | } |
3413 | ||
c1dd91d1 JA |
3414 | /* |
3415 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3416 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3417 | * This gets called when the page is unlocked, and we generally expect that to | |
3418 | * happen when the page IO is completed and the page is now uptodate. This will | |
3419 | * queue a task_work based retry of the operation, attempting to copy the data | |
3420 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3421 | * do a thread based blocking retry of the operation. That's the unexpected | |
3422 | * slow path. | |
3423 | */ | |
bcf5a063 JA |
3424 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3425 | int sync, void *arg) | |
3426 | { | |
3427 | struct wait_page_queue *wpq; | |
3428 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3429 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3430 | int ret; |
3431 | ||
3432 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3433 | ||
cdc8fcb4 LT |
3434 | if (!wake_page_match(wpq, key)) |
3435 | return 0; | |
3436 | ||
c8d317aa | 3437 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3438 | list_del_init(&wait->entry); |
3439 | ||
e7375122 | 3440 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3441 | percpu_ref_get(&req->ctx->refs); |
3442 | ||
bcf5a063 JA |
3443 | /* submit ref gets dropped, acquire a new one */ |
3444 | refcount_inc(&req->refs); | |
355fb9e2 | 3445 | ret = io_req_task_work_add(req); |
bcf5a063 | 3446 | if (unlikely(ret)) { |
c2c4c83c JA |
3447 | struct task_struct *tsk; |
3448 | ||
bcf5a063 | 3449 | /* queue just for cancelation */ |
e7375122 | 3450 | init_task_work(&req->task_work, io_req_task_cancel); |
bcf5a063 | 3451 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 3452 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 3453 | wake_up_process(tsk); |
bcf5a063 | 3454 | } |
bcf5a063 JA |
3455 | return 1; |
3456 | } | |
3457 | ||
c1dd91d1 JA |
3458 | /* |
3459 | * This controls whether a given IO request should be armed for async page | |
3460 | * based retry. If we return false here, the request is handed to the async | |
3461 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3462 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3463 | * will either succeed because the page is now uptodate and unlocked, or it | |
3464 | * will register a callback when the page is unlocked at IO completion. Through | |
3465 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3466 | * That retry will attempt the buffered read again. The retry will generally | |
3467 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3468 | * async worker threads for a blocking retry. | |
3469 | */ | |
227c0c96 | 3470 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3471 | { |
e8c2bc1f JA |
3472 | struct io_async_rw *rw = req->async_data; |
3473 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3474 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3475 | |
bcf5a063 JA |
3476 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3477 | if (req->flags & REQ_F_NOWAIT) | |
3478 | return false; | |
f67676d1 | 3479 | |
227c0c96 | 3480 | /* Only for buffered IO */ |
3b2a4439 | 3481 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3482 | return false; |
3b2a4439 | 3483 | |
bcf5a063 JA |
3484 | /* |
3485 | * just use poll if we can, and don't attempt if the fs doesn't | |
3486 | * support callback based unlocks | |
3487 | */ | |
3488 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3489 | return false; | |
f67676d1 | 3490 | |
3b2a4439 JA |
3491 | wait->wait.func = io_async_buf_func; |
3492 | wait->wait.private = req; | |
3493 | wait->wait.flags = 0; | |
3494 | INIT_LIST_HEAD(&wait->wait.entry); | |
3495 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3496 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3497 | kiocb->ki_waitq = wait; |
3b2a4439 | 3498 | return true; |
bcf5a063 JA |
3499 | } |
3500 | ||
3501 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3502 | { | |
3503 | if (req->file->f_op->read_iter) | |
3504 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3505 | else if (req->file->f_op->read) |
4017eb91 | 3506 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3507 | else |
3508 | return -EINVAL; | |
f67676d1 JA |
3509 | } |
3510 | ||
a1d7c393 JA |
3511 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3512 | struct io_comp_state *cs) | |
2b188cc1 JA |
3513 | { |
3514 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3515 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3516 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3517 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3518 | ssize_t io_size, ret, ret2; |
f5cac8b1 | 3519 | bool no_async; |
ff6165b2 | 3520 | |
2846c481 | 3521 | if (rw) { |
e8c2bc1f | 3522 | iter = &rw->iter; |
2846c481 PB |
3523 | iovec = NULL; |
3524 | } else { | |
3525 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3526 | if (ret < 0) | |
3527 | return ret; | |
3528 | } | |
632546c4 | 3529 | io_size = iov_iter_count(iter); |
fa15bafb | 3530 | req->result = io_size; |
227c0c96 | 3531 | ret = 0; |
2b188cc1 | 3532 | |
fd6c2e4c JA |
3533 | /* Ensure we clear previously set non-block flag */ |
3534 | if (!force_nonblock) | |
29de5f6a | 3535 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3536 | else |
3537 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3538 | ||
fd6c2e4c | 3539 | |
24c74678 | 3540 | /* If the file doesn't support async, just async punt */ |
f5cac8b1 JA |
3541 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
3542 | if (no_async) | |
f67676d1 | 3543 | goto copy_iov; |
9e645e11 | 3544 | |
632546c4 | 3545 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3546 | if (unlikely(ret)) |
3547 | goto out_free; | |
2b188cc1 | 3548 | |
227c0c96 | 3549 | ret = io_iter_do_read(req, iter); |
32960613 | 3550 | |
227c0c96 JA |
3551 | if (!ret) { |
3552 | goto done; | |
3553 | } else if (ret == -EIOCBQUEUED) { | |
3554 | ret = 0; | |
3555 | goto out_free; | |
3556 | } else if (ret == -EAGAIN) { | |
eefdf30f JA |
3557 | /* IOPOLL retry should happen for io-wq threads */ |
3558 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3559 | goto done; |
355afaeb JA |
3560 | /* no retry on NONBLOCK marked file */ |
3561 | if (req->file->f_flags & O_NONBLOCK) | |
3562 | goto done; | |
84216315 | 3563 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3564 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a JA |
3565 | ret = 0; |
3566 | goto copy_iov; | |
227c0c96 | 3567 | } else if (ret < 0) { |
00d23d51 JA |
3568 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
3569 | goto done; | |
227c0c96 JA |
3570 | } |
3571 | ||
3572 | /* read it all, or we did blocking attempt. no retry. */ | |
f91daf56 | 3573 | if (!iov_iter_count(iter) || !force_nonblock || |
9a173346 | 3574 | (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG)) |
227c0c96 JA |
3575 | goto done; |
3576 | ||
3577 | io_size -= ret; | |
3578 | copy_iov: | |
3579 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
3580 | if (ret2) { | |
3581 | ret = ret2; | |
3582 | goto out_free; | |
3583 | } | |
f5cac8b1 JA |
3584 | if (no_async) |
3585 | return -EAGAIN; | |
e8c2bc1f | 3586 | rw = req->async_data; |
227c0c96 JA |
3587 | /* it's copied and will be cleaned with ->io */ |
3588 | iovec = NULL; | |
3589 | /* now use our persistent iterator, if we aren't already */ | |
e8c2bc1f | 3590 | iter = &rw->iter; |
227c0c96 | 3591 | retry: |
e8c2bc1f | 3592 | rw->bytes_done += ret; |
227c0c96 JA |
3593 | /* if we can retry, do so with the callbacks armed */ |
3594 | if (!io_rw_should_retry(req)) { | |
fa15bafb PB |
3595 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3596 | return -EAGAIN; | |
2b188cc1 | 3597 | } |
227c0c96 JA |
3598 | |
3599 | /* | |
3600 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we | |
3601 | * get -EIOCBQUEUED, then we'll get a notification when the desired | |
3602 | * page gets unlocked. We can also get a partial read here, and if we | |
3603 | * do, then just retry at the new offset. | |
3604 | */ | |
3605 | ret = io_iter_do_read(req, iter); | |
3606 | if (ret == -EIOCBQUEUED) { | |
3607 | ret = 0; | |
3608 | goto out_free; | |
3609 | } else if (ret > 0 && ret < io_size) { | |
3610 | /* we got some bytes, but not all. retry. */ | |
3611 | goto retry; | |
3612 | } | |
3613 | done: | |
3614 | kiocb_done(kiocb, ret, cs); | |
3615 | ret = 0; | |
f67676d1 | 3616 | out_free: |
f261c168 | 3617 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3618 | if (iovec) |
6f2cc166 | 3619 | kfree(iovec); |
2b188cc1 JA |
3620 | return ret; |
3621 | } | |
3622 | ||
73debe68 | 3623 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3624 | { |
3625 | ssize_t ret; | |
3626 | ||
a88fc400 | 3627 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3628 | if (ret) |
3629 | return ret; | |
f67676d1 | 3630 | |
3529d8c2 JA |
3631 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3632 | return -EBADF; | |
f67676d1 | 3633 | |
5f798bea | 3634 | /* either don't need iovec imported or already have it */ |
2d199895 | 3635 | if (!req->async_data) |
3529d8c2 | 3636 | return 0; |
73debe68 | 3637 | return io_rw_prep_async(req, WRITE); |
f67676d1 JA |
3638 | } |
3639 | ||
a1d7c393 JA |
3640 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3641 | struct io_comp_state *cs) | |
2b188cc1 JA |
3642 | { |
3643 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3644 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3645 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3646 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3647 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3648 | |
2846c481 | 3649 | if (rw) { |
e8c2bc1f | 3650 | iter = &rw->iter; |
2846c481 PB |
3651 | iovec = NULL; |
3652 | } else { | |
3653 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3654 | if (ret < 0) | |
3655 | return ret; | |
3656 | } | |
632546c4 | 3657 | io_size = iov_iter_count(iter); |
fa15bafb | 3658 | req->result = io_size; |
2b188cc1 | 3659 | |
fd6c2e4c JA |
3660 | /* Ensure we clear previously set non-block flag */ |
3661 | if (!force_nonblock) | |
a88fc400 PB |
3662 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3663 | else | |
3664 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3665 | |
24c74678 | 3666 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3667 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3668 | goto copy_iov; |
31b51510 | 3669 | |
10d59345 JA |
3670 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3671 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3672 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3673 | goto copy_iov; |
31b51510 | 3674 | |
632546c4 | 3675 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3676 | if (unlikely(ret)) |
3677 | goto out_free; | |
4ed734b0 | 3678 | |
fa15bafb PB |
3679 | /* |
3680 | * Open-code file_start_write here to grab freeze protection, | |
3681 | * which will be released by another thread in | |
3682 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3683 | * released so that it doesn't complain about the held lock when | |
3684 | * we return to userspace. | |
3685 | */ | |
3686 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3687 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3688 | __sb_writers_release(file_inode(req->file)->i_sb, |
3689 | SB_FREEZE_WRITE); | |
3690 | } | |
3691 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3692 | |
fa15bafb | 3693 | if (req->file->f_op->write_iter) |
ff6165b2 | 3694 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3695 | else if (req->file->f_op->write) |
4017eb91 | 3696 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3697 | else |
3698 | ret2 = -EINVAL; | |
4ed734b0 | 3699 | |
fa15bafb PB |
3700 | /* |
3701 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3702 | * retry them without IOCB_NOWAIT. | |
3703 | */ | |
3704 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3705 | ret2 = -EAGAIN; | |
355afaeb JA |
3706 | /* no retry on NONBLOCK marked file */ |
3707 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) | |
3708 | goto done; | |
fa15bafb | 3709 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3710 | /* IOPOLL retry should happen for io-wq threads */ |
3711 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3712 | goto copy_iov; | |
355afaeb | 3713 | done: |
fa15bafb PB |
3714 | kiocb_done(kiocb, ret2, cs); |
3715 | } else { | |
f67676d1 | 3716 | copy_iov: |
84216315 | 3717 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3718 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3719 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
ff6165b2 JA |
3720 | if (!ret) |
3721 | return -EAGAIN; | |
2b188cc1 | 3722 | } |
31b51510 | 3723 | out_free: |
f261c168 | 3724 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3725 | if (iovec) |
6f2cc166 | 3726 | kfree(iovec); |
2b188cc1 JA |
3727 | return ret; |
3728 | } | |
3729 | ||
80a261fd JA |
3730 | static int io_renameat_prep(struct io_kiocb *req, |
3731 | const struct io_uring_sqe *sqe) | |
3732 | { | |
3733 | struct io_rename *ren = &req->rename; | |
3734 | const char __user *oldf, *newf; | |
3735 | ||
3736 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3737 | return -EBADF; | |
3738 | ||
3739 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3740 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3741 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3742 | ren->new_dfd = READ_ONCE(sqe->len); | |
3743 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3744 | ||
3745 | ren->oldpath = getname(oldf); | |
3746 | if (IS_ERR(ren->oldpath)) | |
3747 | return PTR_ERR(ren->oldpath); | |
3748 | ||
3749 | ren->newpath = getname(newf); | |
3750 | if (IS_ERR(ren->newpath)) { | |
3751 | putname(ren->oldpath); | |
3752 | return PTR_ERR(ren->newpath); | |
3753 | } | |
3754 | ||
3755 | req->flags |= REQ_F_NEED_CLEANUP; | |
3756 | return 0; | |
3757 | } | |
3758 | ||
3759 | static int io_renameat(struct io_kiocb *req, bool force_nonblock) | |
3760 | { | |
3761 | struct io_rename *ren = &req->rename; | |
3762 | int ret; | |
3763 | ||
3764 | if (force_nonblock) | |
3765 | return -EAGAIN; | |
3766 | ||
3767 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3768 | ren->newpath, ren->flags); | |
3769 | ||
3770 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3771 | if (ret < 0) | |
3772 | req_set_fail_links(req); | |
3773 | io_req_complete(req, ret); | |
3774 | return 0; | |
3775 | } | |
3776 | ||
14a1143b JA |
3777 | static int io_unlinkat_prep(struct io_kiocb *req, |
3778 | const struct io_uring_sqe *sqe) | |
3779 | { | |
3780 | struct io_unlink *un = &req->unlink; | |
3781 | const char __user *fname; | |
3782 | ||
3783 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3784 | return -EBADF; | |
3785 | ||
3786 | un->dfd = READ_ONCE(sqe->fd); | |
3787 | ||
3788 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3789 | if (un->flags & ~AT_REMOVEDIR) | |
3790 | return -EINVAL; | |
3791 | ||
3792 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3793 | un->filename = getname(fname); | |
3794 | if (IS_ERR(un->filename)) | |
3795 | return PTR_ERR(un->filename); | |
3796 | ||
3797 | req->flags |= REQ_F_NEED_CLEANUP; | |
3798 | return 0; | |
3799 | } | |
3800 | ||
3801 | static int io_unlinkat(struct io_kiocb *req, bool force_nonblock) | |
3802 | { | |
3803 | struct io_unlink *un = &req->unlink; | |
3804 | int ret; | |
3805 | ||
3806 | if (force_nonblock) | |
3807 | return -EAGAIN; | |
3808 | ||
3809 | if (un->flags & AT_REMOVEDIR) | |
3810 | ret = do_rmdir(un->dfd, un->filename); | |
3811 | else | |
3812 | ret = do_unlinkat(un->dfd, un->filename); | |
3813 | ||
3814 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3815 | if (ret < 0) | |
3816 | req_set_fail_links(req); | |
3817 | io_req_complete(req, ret); | |
3818 | return 0; | |
3819 | } | |
3820 | ||
36f4fa68 JA |
3821 | static int io_shutdown_prep(struct io_kiocb *req, |
3822 | const struct io_uring_sqe *sqe) | |
3823 | { | |
3824 | #if defined(CONFIG_NET) | |
3825 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3826 | return -EINVAL; | |
3827 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3828 | sqe->buf_index) | |
3829 | return -EINVAL; | |
3830 | ||
3831 | req->shutdown.how = READ_ONCE(sqe->len); | |
3832 | return 0; | |
3833 | #else | |
3834 | return -EOPNOTSUPP; | |
3835 | #endif | |
3836 | } | |
3837 | ||
3838 | static int io_shutdown(struct io_kiocb *req, bool force_nonblock) | |
3839 | { | |
3840 | #if defined(CONFIG_NET) | |
3841 | struct socket *sock; | |
3842 | int ret; | |
3843 | ||
3844 | if (force_nonblock) | |
3845 | return -EAGAIN; | |
3846 | ||
48aba79b | 3847 | sock = sock_from_file(req->file); |
36f4fa68 | 3848 | if (unlikely(!sock)) |
48aba79b | 3849 | return -ENOTSOCK; |
36f4fa68 JA |
3850 | |
3851 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3852 | if (ret < 0) |
3853 | req_set_fail_links(req); | |
36f4fa68 JA |
3854 | io_req_complete(req, ret); |
3855 | return 0; | |
3856 | #else | |
3857 | return -EOPNOTSUPP; | |
3858 | #endif | |
3859 | } | |
3860 | ||
f2a8d5c7 PB |
3861 | static int __io_splice_prep(struct io_kiocb *req, |
3862 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3863 | { |
3864 | struct io_splice* sp = &req->splice; | |
3865 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3866 | |
3232dd02 PB |
3867 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3868 | return -EINVAL; | |
7d67af2c PB |
3869 | |
3870 | sp->file_in = NULL; | |
7d67af2c PB |
3871 | sp->len = READ_ONCE(sqe->len); |
3872 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3873 | ||
3874 | if (unlikely(sp->flags & ~valid_flags)) | |
3875 | return -EINVAL; | |
3876 | ||
8371adf5 PB |
3877 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3878 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3879 | if (!sp->file_in) | |
3880 | return -EBADF; | |
7d67af2c PB |
3881 | req->flags |= REQ_F_NEED_CLEANUP; |
3882 | ||
7cdaf587 XW |
3883 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3884 | /* | |
3885 | * Splice operation will be punted aync, and here need to | |
3886 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3887 | */ | |
3888 | io_req_init_async(req); | |
7d67af2c | 3889 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3890 | } |
7d67af2c PB |
3891 | |
3892 | return 0; | |
3893 | } | |
3894 | ||
f2a8d5c7 PB |
3895 | static int io_tee_prep(struct io_kiocb *req, |
3896 | const struct io_uring_sqe *sqe) | |
3897 | { | |
3898 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3899 | return -EINVAL; | |
3900 | return __io_splice_prep(req, sqe); | |
3901 | } | |
3902 | ||
3903 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3904 | { | |
3905 | struct io_splice *sp = &req->splice; | |
3906 | struct file *in = sp->file_in; | |
3907 | struct file *out = sp->file_out; | |
3908 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3909 | long ret = 0; | |
3910 | ||
3911 | if (force_nonblock) | |
3912 | return -EAGAIN; | |
3913 | if (sp->len) | |
3914 | ret = do_tee(in, out, sp->len, flags); | |
3915 | ||
3916 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3917 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3918 | ||
f2a8d5c7 PB |
3919 | if (ret != sp->len) |
3920 | req_set_fail_links(req); | |
e1e16097 | 3921 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3922 | return 0; |
3923 | } | |
3924 | ||
3925 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3926 | { | |
3927 | struct io_splice* sp = &req->splice; | |
3928 | ||
3929 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3930 | sp->off_out = READ_ONCE(sqe->off); | |
3931 | return __io_splice_prep(req, sqe); | |
3932 | } | |
3933 | ||
014db007 | 3934 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3935 | { |
3936 | struct io_splice *sp = &req->splice; | |
3937 | struct file *in = sp->file_in; | |
3938 | struct file *out = sp->file_out; | |
3939 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3940 | loff_t *poff_in, *poff_out; | |
c9687426 | 3941 | long ret = 0; |
7d67af2c | 3942 | |
2fb3e822 PB |
3943 | if (force_nonblock) |
3944 | return -EAGAIN; | |
7d67af2c PB |
3945 | |
3946 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3947 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3948 | |
948a7749 | 3949 | if (sp->len) |
c9687426 | 3950 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3951 | |
3952 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3953 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3954 | ||
7d67af2c PB |
3955 | if (ret != sp->len) |
3956 | req_set_fail_links(req); | |
e1e16097 | 3957 | io_req_complete(req, ret); |
7d67af2c PB |
3958 | return 0; |
3959 | } | |
3960 | ||
2b188cc1 JA |
3961 | /* |
3962 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3963 | */ | |
229a7b63 | 3964 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3965 | { |
3966 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3967 | |
def596e9 JA |
3968 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3969 | return -EINVAL; | |
3970 | ||
229a7b63 | 3971 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3972 | return 0; |
3973 | } | |
3974 | ||
3529d8c2 | 3975 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3976 | { |
6b06314c | 3977 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3978 | |
09bb8394 JA |
3979 | if (!req->file) |
3980 | return -EBADF; | |
c992fe29 | 3981 | |
6b06314c | 3982 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3983 | return -EINVAL; |
edafccee | 3984 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3985 | return -EINVAL; |
3986 | ||
8ed8d3c3 JA |
3987 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3988 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3989 | return -EINVAL; | |
3990 | ||
3991 | req->sync.off = READ_ONCE(sqe->off); | |
3992 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3993 | return 0; |
3994 | } | |
3995 | ||
ac45abc0 | 3996 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3997 | { |
8ed8d3c3 | 3998 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3999 | int ret; |
4000 | ||
ac45abc0 PB |
4001 | /* fsync always requires a blocking context */ |
4002 | if (force_nonblock) | |
4003 | return -EAGAIN; | |
4004 | ||
9adbd45d | 4005 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4006 | end > 0 ? end : LLONG_MAX, |
4007 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4008 | if (ret < 0) | |
4009 | req_set_fail_links(req); | |
e1e16097 | 4010 | io_req_complete(req, ret); |
c992fe29 CH |
4011 | return 0; |
4012 | } | |
4013 | ||
d63d1b5e JA |
4014 | static int io_fallocate_prep(struct io_kiocb *req, |
4015 | const struct io_uring_sqe *sqe) | |
4016 | { | |
4017 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
4018 | return -EINVAL; | |
3232dd02 PB |
4019 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4020 | return -EINVAL; | |
d63d1b5e JA |
4021 | |
4022 | req->sync.off = READ_ONCE(sqe->off); | |
4023 | req->sync.len = READ_ONCE(sqe->addr); | |
4024 | req->sync.mode = READ_ONCE(sqe->len); | |
4025 | return 0; | |
4026 | } | |
4027 | ||
014db007 | 4028 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 4029 | { |
ac45abc0 PB |
4030 | int ret; |
4031 | ||
d63d1b5e | 4032 | /* fallocate always requiring blocking context */ |
ac45abc0 | 4033 | if (force_nonblock) |
5d17b4a4 | 4034 | return -EAGAIN; |
ac45abc0 PB |
4035 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4036 | req->sync.len); | |
ac45abc0 PB |
4037 | if (ret < 0) |
4038 | req_set_fail_links(req); | |
e1e16097 | 4039 | io_req_complete(req, ret); |
5d17b4a4 JA |
4040 | return 0; |
4041 | } | |
4042 | ||
ec65fea5 | 4043 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4044 | { |
f8748881 | 4045 | const char __user *fname; |
15b71abe | 4046 | int ret; |
b7bb4f7d | 4047 | |
ec65fea5 | 4048 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4049 | return -EINVAL; |
ec65fea5 | 4050 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4051 | return -EBADF; |
03b1230c | 4052 | |
ec65fea5 PB |
4053 | /* open.how should be already initialised */ |
4054 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4055 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4056 | |
25e72d10 PB |
4057 | req->open.dfd = READ_ONCE(sqe->fd); |
4058 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4059 | req->open.filename = getname(fname); |
15b71abe JA |
4060 | if (IS_ERR(req->open.filename)) { |
4061 | ret = PTR_ERR(req->open.filename); | |
4062 | req->open.filename = NULL; | |
4063 | return ret; | |
4064 | } | |
4022e7af | 4065 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4066 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4067 | return 0; |
03b1230c JA |
4068 | } |
4069 | ||
ec65fea5 PB |
4070 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4071 | { | |
4072 | u64 flags, mode; | |
4073 | ||
14587a46 | 4074 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4075 | return -EINVAL; |
ec65fea5 PB |
4076 | mode = READ_ONCE(sqe->len); |
4077 | flags = READ_ONCE(sqe->open_flags); | |
4078 | req->open.how = build_open_how(flags, mode); | |
4079 | return __io_openat_prep(req, sqe); | |
4080 | } | |
4081 | ||
cebdb986 | 4082 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4083 | { |
cebdb986 | 4084 | struct open_how __user *how; |
cebdb986 | 4085 | size_t len; |
0fa03c62 JA |
4086 | int ret; |
4087 | ||
14587a46 | 4088 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4089 | return -EINVAL; |
cebdb986 JA |
4090 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4091 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4092 | if (len < OPEN_HOW_SIZE_VER0) |
4093 | return -EINVAL; | |
3529d8c2 | 4094 | |
cebdb986 JA |
4095 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4096 | len); | |
4097 | if (ret) | |
4098 | return ret; | |
3529d8c2 | 4099 | |
ec65fea5 | 4100 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4101 | } |
4102 | ||
014db007 | 4103 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
4104 | { |
4105 | struct open_flags op; | |
15b71abe | 4106 | struct file *file; |
3a81fd02 JA |
4107 | bool nonblock_set; |
4108 | bool resolve_nonblock; | |
15b71abe JA |
4109 | int ret; |
4110 | ||
cebdb986 | 4111 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4112 | if (ret) |
4113 | goto err; | |
3a81fd02 JA |
4114 | nonblock_set = op.open_flag & O_NONBLOCK; |
4115 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
4116 | if (force_nonblock) { | |
4117 | /* | |
4118 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4119 | * it'll always -EAGAIN | |
4120 | */ | |
4121 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4122 | return -EAGAIN; | |
4123 | op.lookup_flags |= LOOKUP_CACHED; | |
4124 | op.open_flag |= O_NONBLOCK; | |
4125 | } | |
15b71abe | 4126 | |
4022e7af | 4127 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4128 | if (ret < 0) |
4129 | goto err; | |
4130 | ||
4131 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 JA |
4132 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4133 | if ((!resolve_nonblock && force_nonblock) && file == ERR_PTR(-EAGAIN)) { | |
4134 | /* | |
4135 | * We could hang on to this 'fd', but seems like marginal | |
4136 | * gain for something that is now known to be a slower path. | |
4137 | * So just put it, and we'll get a new one when we retry. | |
4138 | */ | |
4139 | put_unused_fd(ret); | |
4140 | return -EAGAIN; | |
4141 | } | |
4142 | ||
15b71abe JA |
4143 | if (IS_ERR(file)) { |
4144 | put_unused_fd(ret); | |
4145 | ret = PTR_ERR(file); | |
4146 | } else { | |
3a81fd02 JA |
4147 | if (force_nonblock && !nonblock_set) |
4148 | file->f_flags &= ~O_NONBLOCK; | |
15b71abe JA |
4149 | fsnotify_open(file); |
4150 | fd_install(ret, file); | |
4151 | } | |
4152 | err: | |
4153 | putname(req->open.filename); | |
8fef80bf | 4154 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4155 | if (ret < 0) |
4156 | req_set_fail_links(req); | |
e1e16097 | 4157 | io_req_complete(req, ret); |
15b71abe JA |
4158 | return 0; |
4159 | } | |
4160 | ||
014db007 | 4161 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 4162 | { |
014db007 | 4163 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
4164 | } |
4165 | ||
067524e9 JA |
4166 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4167 | const struct io_uring_sqe *sqe) | |
4168 | { | |
4169 | struct io_provide_buf *p = &req->pbuf; | |
4170 | u64 tmp; | |
4171 | ||
4172 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4173 | return -EINVAL; | |
4174 | ||
4175 | tmp = READ_ONCE(sqe->fd); | |
4176 | if (!tmp || tmp > USHRT_MAX) | |
4177 | return -EINVAL; | |
4178 | ||
4179 | memset(p, 0, sizeof(*p)); | |
4180 | p->nbufs = tmp; | |
4181 | p->bgid = READ_ONCE(sqe->buf_group); | |
4182 | return 0; | |
4183 | } | |
4184 | ||
4185 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4186 | int bgid, unsigned nbufs) | |
4187 | { | |
4188 | unsigned i = 0; | |
4189 | ||
4190 | /* shouldn't happen */ | |
4191 | if (!nbufs) | |
4192 | return 0; | |
4193 | ||
4194 | /* the head kbuf is the list itself */ | |
4195 | while (!list_empty(&buf->list)) { | |
4196 | struct io_buffer *nxt; | |
4197 | ||
4198 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4199 | list_del(&nxt->list); | |
4200 | kfree(nxt); | |
4201 | if (++i == nbufs) | |
4202 | return i; | |
4203 | } | |
4204 | i++; | |
4205 | kfree(buf); | |
4206 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4207 | ||
4208 | return i; | |
4209 | } | |
4210 | ||
229a7b63 JA |
4211 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
4212 | struct io_comp_state *cs) | |
067524e9 JA |
4213 | { |
4214 | struct io_provide_buf *p = &req->pbuf; | |
4215 | struct io_ring_ctx *ctx = req->ctx; | |
4216 | struct io_buffer *head; | |
4217 | int ret = 0; | |
4218 | ||
4219 | io_ring_submit_lock(ctx, !force_nonblock); | |
4220 | ||
4221 | lockdep_assert_held(&ctx->uring_lock); | |
4222 | ||
4223 | ret = -ENOENT; | |
4224 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4225 | if (head) | |
4226 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4227 | if (ret < 0) |
4228 | req_set_fail_links(req); | |
067524e9 | 4229 | |
31bff9a5 PB |
4230 | /* need to hold the lock to complete IOPOLL requests */ |
4231 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4232 | __io_req_complete(req, ret, 0, cs); | |
4233 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4234 | } else { | |
4235 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4236 | __io_req_complete(req, ret, 0, cs); | |
4237 | } | |
067524e9 JA |
4238 | return 0; |
4239 | } | |
4240 | ||
ddf0322d JA |
4241 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4242 | const struct io_uring_sqe *sqe) | |
4243 | { | |
4244 | struct io_provide_buf *p = &req->pbuf; | |
4245 | u64 tmp; | |
4246 | ||
4247 | if (sqe->ioprio || sqe->rw_flags) | |
4248 | return -EINVAL; | |
4249 | ||
4250 | tmp = READ_ONCE(sqe->fd); | |
4251 | if (!tmp || tmp > USHRT_MAX) | |
4252 | return -E2BIG; | |
4253 | p->nbufs = tmp; | |
4254 | p->addr = READ_ONCE(sqe->addr); | |
4255 | p->len = READ_ONCE(sqe->len); | |
4256 | ||
efe68c1c | 4257 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4258 | return -EFAULT; |
4259 | ||
4260 | p->bgid = READ_ONCE(sqe->buf_group); | |
4261 | tmp = READ_ONCE(sqe->off); | |
4262 | if (tmp > USHRT_MAX) | |
4263 | return -E2BIG; | |
4264 | p->bid = tmp; | |
4265 | return 0; | |
4266 | } | |
4267 | ||
4268 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4269 | { | |
4270 | struct io_buffer *buf; | |
4271 | u64 addr = pbuf->addr; | |
4272 | int i, bid = pbuf->bid; | |
4273 | ||
4274 | for (i = 0; i < pbuf->nbufs; i++) { | |
4275 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4276 | if (!buf) | |
4277 | break; | |
4278 | ||
4279 | buf->addr = addr; | |
4280 | buf->len = pbuf->len; | |
4281 | buf->bid = bid; | |
4282 | addr += pbuf->len; | |
4283 | bid++; | |
4284 | if (!*head) { | |
4285 | INIT_LIST_HEAD(&buf->list); | |
4286 | *head = buf; | |
4287 | } else { | |
4288 | list_add_tail(&buf->list, &(*head)->list); | |
4289 | } | |
4290 | } | |
4291 | ||
4292 | return i ? i : -ENOMEM; | |
4293 | } | |
4294 | ||
229a7b63 JA |
4295 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
4296 | struct io_comp_state *cs) | |
ddf0322d JA |
4297 | { |
4298 | struct io_provide_buf *p = &req->pbuf; | |
4299 | struct io_ring_ctx *ctx = req->ctx; | |
4300 | struct io_buffer *head, *list; | |
4301 | int ret = 0; | |
4302 | ||
4303 | io_ring_submit_lock(ctx, !force_nonblock); | |
4304 | ||
4305 | lockdep_assert_held(&ctx->uring_lock); | |
4306 | ||
4307 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4308 | ||
4309 | ret = io_add_buffers(p, &head); | |
4310 | if (ret < 0) | |
4311 | goto out; | |
4312 | ||
4313 | if (!list) { | |
4314 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4315 | GFP_KERNEL); | |
4316 | if (ret < 0) { | |
067524e9 | 4317 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4318 | goto out; |
4319 | } | |
4320 | } | |
4321 | out: | |
ddf0322d JA |
4322 | if (ret < 0) |
4323 | req_set_fail_links(req); | |
31bff9a5 PB |
4324 | |
4325 | /* need to hold the lock to complete IOPOLL requests */ | |
4326 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4327 | __io_req_complete(req, ret, 0, cs); | |
4328 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4329 | } else { | |
4330 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4331 | __io_req_complete(req, ret, 0, cs); | |
4332 | } | |
ddf0322d | 4333 | return 0; |
cebdb986 JA |
4334 | } |
4335 | ||
3e4827b0 JA |
4336 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4337 | const struct io_uring_sqe *sqe) | |
4338 | { | |
4339 | #if defined(CONFIG_EPOLL) | |
4340 | if (sqe->ioprio || sqe->buf_index) | |
4341 | return -EINVAL; | |
6ca56f84 | 4342 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4343 | return -EINVAL; |
3e4827b0 JA |
4344 | |
4345 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4346 | req->epoll.op = READ_ONCE(sqe->len); | |
4347 | req->epoll.fd = READ_ONCE(sqe->off); | |
4348 | ||
4349 | if (ep_op_has_event(req->epoll.op)) { | |
4350 | struct epoll_event __user *ev; | |
4351 | ||
4352 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4353 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4354 | return -EFAULT; | |
4355 | } | |
4356 | ||
4357 | return 0; | |
4358 | #else | |
4359 | return -EOPNOTSUPP; | |
4360 | #endif | |
4361 | } | |
4362 | ||
229a7b63 JA |
4363 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
4364 | struct io_comp_state *cs) | |
3e4827b0 JA |
4365 | { |
4366 | #if defined(CONFIG_EPOLL) | |
4367 | struct io_epoll *ie = &req->epoll; | |
4368 | int ret; | |
4369 | ||
4370 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4371 | if (force_nonblock && ret == -EAGAIN) | |
4372 | return -EAGAIN; | |
4373 | ||
4374 | if (ret < 0) | |
4375 | req_set_fail_links(req); | |
229a7b63 | 4376 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
4377 | return 0; |
4378 | #else | |
4379 | return -EOPNOTSUPP; | |
4380 | #endif | |
4381 | } | |
4382 | ||
c1ca757b JA |
4383 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4384 | { | |
4385 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4386 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4387 | return -EINVAL; | |
3232dd02 PB |
4388 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4389 | return -EINVAL; | |
c1ca757b JA |
4390 | |
4391 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4392 | req->madvise.len = READ_ONCE(sqe->len); | |
4393 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4394 | return 0; | |
4395 | #else | |
4396 | return -EOPNOTSUPP; | |
4397 | #endif | |
4398 | } | |
4399 | ||
014db007 | 4400 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
4401 | { |
4402 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4403 | struct io_madvise *ma = &req->madvise; | |
4404 | int ret; | |
4405 | ||
4406 | if (force_nonblock) | |
4407 | return -EAGAIN; | |
4408 | ||
0726b01e | 4409 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4410 | if (ret < 0) |
4411 | req_set_fail_links(req); | |
e1e16097 | 4412 | io_req_complete(req, ret); |
c1ca757b JA |
4413 | return 0; |
4414 | #else | |
4415 | return -EOPNOTSUPP; | |
4416 | #endif | |
4417 | } | |
4418 | ||
4840e418 JA |
4419 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4420 | { | |
4421 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4422 | return -EINVAL; | |
3232dd02 PB |
4423 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4424 | return -EINVAL; | |
4840e418 JA |
4425 | |
4426 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4427 | req->fadvise.len = READ_ONCE(sqe->len); | |
4428 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4429 | return 0; | |
4430 | } | |
4431 | ||
014db007 | 4432 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
4433 | { |
4434 | struct io_fadvise *fa = &req->fadvise; | |
4435 | int ret; | |
4436 | ||
3e69426d JA |
4437 | if (force_nonblock) { |
4438 | switch (fa->advice) { | |
4439 | case POSIX_FADV_NORMAL: | |
4440 | case POSIX_FADV_RANDOM: | |
4441 | case POSIX_FADV_SEQUENTIAL: | |
4442 | break; | |
4443 | default: | |
4444 | return -EAGAIN; | |
4445 | } | |
4446 | } | |
4840e418 JA |
4447 | |
4448 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4449 | if (ret < 0) | |
4450 | req_set_fail_links(req); | |
e1e16097 | 4451 | io_req_complete(req, ret); |
4840e418 JA |
4452 | return 0; |
4453 | } | |
4454 | ||
eddc7ef5 JA |
4455 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4456 | { | |
6ca56f84 | 4457 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4458 | return -EINVAL; |
eddc7ef5 JA |
4459 | if (sqe->ioprio || sqe->buf_index) |
4460 | return -EINVAL; | |
9c280f90 | 4461 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4462 | return -EBADF; |
eddc7ef5 | 4463 | |
1d9e1288 BM |
4464 | req->statx.dfd = READ_ONCE(sqe->fd); |
4465 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4466 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4467 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4468 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4469 | |
4470 | return 0; | |
4471 | } | |
4472 | ||
014db007 | 4473 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 4474 | { |
1d9e1288 | 4475 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4476 | int ret; |
4477 | ||
5b0bbee4 JA |
4478 | if (force_nonblock) { |
4479 | /* only need file table for an actual valid fd */ | |
4480 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4481 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4482 | return -EAGAIN; |
5b0bbee4 | 4483 | } |
eddc7ef5 | 4484 | |
e62753e4 BM |
4485 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4486 | ctx->buffer); | |
eddc7ef5 | 4487 | |
eddc7ef5 JA |
4488 | if (ret < 0) |
4489 | req_set_fail_links(req); | |
e1e16097 | 4490 | io_req_complete(req, ret); |
eddc7ef5 JA |
4491 | return 0; |
4492 | } | |
4493 | ||
b5dba59e JA |
4494 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4495 | { | |
4496 | /* | |
4497 | * If we queue this for async, it must not be cancellable. That would | |
7cdaf587 XW |
4498 | * leave the 'file' in an undeterminate state, and here need to modify |
4499 | * io_wq_work.flags, so initialize io_wq_work firstly. | |
b5dba59e | 4500 | */ |
7cdaf587 | 4501 | io_req_init_async(req); |
b5dba59e | 4502 | |
14587a46 | 4503 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4504 | return -EINVAL; |
b5dba59e JA |
4505 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4506 | sqe->rw_flags || sqe->buf_index) | |
4507 | return -EINVAL; | |
9c280f90 | 4508 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4509 | return -EBADF; |
b5dba59e JA |
4510 | |
4511 | req->close.fd = READ_ONCE(sqe->fd); | |
0f212204 | 4512 | if ((req->file && req->file->f_op == &io_uring_fops)) |
fd2206e4 | 4513 | return -EBADF; |
b5dba59e | 4514 | |
3af73b28 | 4515 | req->close.put_file = NULL; |
b5dba59e | 4516 | return 0; |
b5dba59e JA |
4517 | } |
4518 | ||
229a7b63 JA |
4519 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
4520 | struct io_comp_state *cs) | |
b5dba59e | 4521 | { |
3af73b28 | 4522 | struct io_close *close = &req->close; |
b5dba59e JA |
4523 | int ret; |
4524 | ||
3af73b28 PB |
4525 | /* might be already done during nonblock submission */ |
4526 | if (!close->put_file) { | |
9fe83c43 | 4527 | ret = close_fd_get_file(close->fd, &close->put_file); |
3af73b28 PB |
4528 | if (ret < 0) |
4529 | return (ret == -ENOENT) ? -EBADF : ret; | |
4530 | } | |
b5dba59e JA |
4531 | |
4532 | /* if the file has a flush method, be safe and punt to async */ | |
3af73b28 | 4533 | if (close->put_file->f_op->flush && force_nonblock) { |
607ec89e JA |
4534 | /* not safe to cancel at this point */ |
4535 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; | |
24c74678 PB |
4536 | /* was never set, but play safe */ |
4537 | req->flags &= ~REQ_F_NOWAIT; | |
0bf0eefd | 4538 | /* avoid grabbing files - we don't need the files */ |
24c74678 | 4539 | req->flags |= REQ_F_NO_FILE_TABLE; |
0bf0eefd | 4540 | return -EAGAIN; |
a2100672 | 4541 | } |
b5dba59e | 4542 | |
3af73b28 | 4543 | /* No ->flush() or already async, safely close from here */ |
98447d65 | 4544 | ret = filp_close(close->put_file, req->work.identity->files); |
3af73b28 PB |
4545 | if (ret < 0) |
4546 | req_set_fail_links(req); | |
3af73b28 PB |
4547 | fput(close->put_file); |
4548 | close->put_file = NULL; | |
229a7b63 | 4549 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 4550 | return 0; |
b5dba59e JA |
4551 | } |
4552 | ||
3529d8c2 | 4553 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4554 | { |
4555 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4556 | |
4557 | if (!req->file) | |
4558 | return -EBADF; | |
5d17b4a4 JA |
4559 | |
4560 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4561 | return -EINVAL; | |
4562 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4563 | return -EINVAL; | |
4564 | ||
8ed8d3c3 JA |
4565 | req->sync.off = READ_ONCE(sqe->off); |
4566 | req->sync.len = READ_ONCE(sqe->len); | |
4567 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4568 | return 0; |
4569 | } | |
4570 | ||
ac45abc0 | 4571 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4572 | { |
8ed8d3c3 JA |
4573 | int ret; |
4574 | ||
ac45abc0 PB |
4575 | /* sync_file_range always requires a blocking context */ |
4576 | if (force_nonblock) | |
4577 | return -EAGAIN; | |
4578 | ||
9adbd45d | 4579 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4580 | req->sync.flags); |
4581 | if (ret < 0) | |
4582 | req_set_fail_links(req); | |
e1e16097 | 4583 | io_req_complete(req, ret); |
5d17b4a4 JA |
4584 | return 0; |
4585 | } | |
4586 | ||
469956e8 | 4587 | #if defined(CONFIG_NET) |
02d27d89 PB |
4588 | static int io_setup_async_msg(struct io_kiocb *req, |
4589 | struct io_async_msghdr *kmsg) | |
4590 | { | |
e8c2bc1f JA |
4591 | struct io_async_msghdr *async_msg = req->async_data; |
4592 | ||
4593 | if (async_msg) | |
02d27d89 | 4594 | return -EAGAIN; |
e8c2bc1f | 4595 | if (io_alloc_async_data(req)) { |
02d27d89 PB |
4596 | if (kmsg->iov != kmsg->fast_iov) |
4597 | kfree(kmsg->iov); | |
4598 | return -ENOMEM; | |
4599 | } | |
e8c2bc1f | 4600 | async_msg = req->async_data; |
02d27d89 | 4601 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4602 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
02d27d89 PB |
4603 | return -EAGAIN; |
4604 | } | |
4605 | ||
2ae523ed PB |
4606 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4607 | struct io_async_msghdr *iomsg) | |
4608 | { | |
4609 | iomsg->iov = iomsg->fast_iov; | |
4610 | iomsg->msg.msg_name = &iomsg->addr; | |
4611 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4612 | req->sr_msg.msg_flags, &iomsg->iov); | |
4613 | } | |
4614 | ||
3529d8c2 | 4615 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4616 | { |
e8c2bc1f | 4617 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4618 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4619 | int ret; |
03b1230c | 4620 | |
d2b6f48b PB |
4621 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4622 | return -EINVAL; | |
4623 | ||
e47293fd | 4624 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4625 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4626 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4627 | |
d8768362 JA |
4628 | #ifdef CONFIG_COMPAT |
4629 | if (req->ctx->compat) | |
4630 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4631 | #endif | |
4632 | ||
e8c2bc1f | 4633 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4634 | return 0; |
e8c2bc1f | 4635 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4636 | if (!ret) |
4637 | req->flags |= REQ_F_NEED_CLEANUP; | |
4638 | return ret; | |
03b1230c JA |
4639 | } |
4640 | ||
229a7b63 JA |
4641 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4642 | struct io_comp_state *cs) | |
aa1fa28f | 4643 | { |
6b754c8b | 4644 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4645 | struct socket *sock; |
7a7cacba | 4646 | unsigned flags; |
0fa03c62 JA |
4647 | int ret; |
4648 | ||
dba4a925 | 4649 | sock = sock_from_file(req->file); |
7a7cacba | 4650 | if (unlikely(!sock)) |
dba4a925 | 4651 | return -ENOTSOCK; |
3529d8c2 | 4652 | |
e8c2bc1f JA |
4653 | if (req->async_data) { |
4654 | kmsg = req->async_data; | |
4655 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4656 | /* if iov is set, it's allocated already */ |
4657 | if (!kmsg->iov) | |
4658 | kmsg->iov = kmsg->fast_iov; | |
4659 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4660 | } else { | |
4661 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4662 | if (ret) | |
4663 | return ret; | |
4664 | kmsg = &iomsg; | |
0fa03c62 | 4665 | } |
0fa03c62 | 4666 | |
7a7cacba PB |
4667 | flags = req->sr_msg.msg_flags; |
4668 | if (flags & MSG_DONTWAIT) | |
4669 | req->flags |= REQ_F_NOWAIT; | |
4670 | else if (force_nonblock) | |
4671 | flags |= MSG_DONTWAIT; | |
e47293fd | 4672 | |
7a7cacba PB |
4673 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4674 | if (force_nonblock && ret == -EAGAIN) | |
4675 | return io_setup_async_msg(req, kmsg); | |
4676 | if (ret == -ERESTARTSYS) | |
4677 | ret = -EINTR; | |
0fa03c62 | 4678 | |
6b754c8b | 4679 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4680 | kfree(kmsg->iov); |
99bc4c38 | 4681 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4682 | if (ret < 0) |
4683 | req_set_fail_links(req); | |
229a7b63 | 4684 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4685 | return 0; |
03b1230c | 4686 | } |
aa1fa28f | 4687 | |
229a7b63 JA |
4688 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4689 | struct io_comp_state *cs) | |
fddaface | 4690 | { |
7a7cacba PB |
4691 | struct io_sr_msg *sr = &req->sr_msg; |
4692 | struct msghdr msg; | |
4693 | struct iovec iov; | |
fddaface | 4694 | struct socket *sock; |
7a7cacba | 4695 | unsigned flags; |
fddaface JA |
4696 | int ret; |
4697 | ||
dba4a925 | 4698 | sock = sock_from_file(req->file); |
7a7cacba | 4699 | if (unlikely(!sock)) |
dba4a925 | 4700 | return -ENOTSOCK; |
fddaface | 4701 | |
7a7cacba PB |
4702 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4703 | if (unlikely(ret)) | |
14db8411 | 4704 | return ret; |
fddaface | 4705 | |
7a7cacba PB |
4706 | msg.msg_name = NULL; |
4707 | msg.msg_control = NULL; | |
4708 | msg.msg_controllen = 0; | |
4709 | msg.msg_namelen = 0; | |
fddaface | 4710 | |
7a7cacba PB |
4711 | flags = req->sr_msg.msg_flags; |
4712 | if (flags & MSG_DONTWAIT) | |
4713 | req->flags |= REQ_F_NOWAIT; | |
4714 | else if (force_nonblock) | |
4715 | flags |= MSG_DONTWAIT; | |
fddaface | 4716 | |
7a7cacba PB |
4717 | msg.msg_flags = flags; |
4718 | ret = sock_sendmsg(sock, &msg); | |
4719 | if (force_nonblock && ret == -EAGAIN) | |
4720 | return -EAGAIN; | |
4721 | if (ret == -ERESTARTSYS) | |
4722 | ret = -EINTR; | |
fddaface | 4723 | |
fddaface JA |
4724 | if (ret < 0) |
4725 | req_set_fail_links(req); | |
229a7b63 | 4726 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4727 | return 0; |
fddaface JA |
4728 | } |
4729 | ||
1400e697 PB |
4730 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4731 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4732 | { |
4733 | struct io_sr_msg *sr = &req->sr_msg; | |
4734 | struct iovec __user *uiov; | |
4735 | size_t iov_len; | |
4736 | int ret; | |
4737 | ||
1400e697 PB |
4738 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4739 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4740 | if (ret) |
4741 | return ret; | |
4742 | ||
4743 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4744 | if (iov_len > 1) | |
4745 | return -EINVAL; | |
1400e697 | 4746 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4747 | return -EFAULT; |
1400e697 PB |
4748 | sr->len = iomsg->iov[0].iov_len; |
4749 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4750 | sr->len); |
1400e697 | 4751 | iomsg->iov = NULL; |
52de1fe1 | 4752 | } else { |
89cd35c5 CH |
4753 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
4754 | &iomsg->iov, &iomsg->msg.msg_iter, | |
4755 | false); | |
52de1fe1 JA |
4756 | if (ret > 0) |
4757 | ret = 0; | |
4758 | } | |
4759 | ||
4760 | return ret; | |
4761 | } | |
4762 | ||
4763 | #ifdef CONFIG_COMPAT | |
4764 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4765 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4766 | { |
4767 | struct compat_msghdr __user *msg_compat; | |
4768 | struct io_sr_msg *sr = &req->sr_msg; | |
4769 | struct compat_iovec __user *uiov; | |
4770 | compat_uptr_t ptr; | |
4771 | compat_size_t len; | |
4772 | int ret; | |
4773 | ||
270a5940 | 4774 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4775 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4776 | &ptr, &len); |
4777 | if (ret) | |
4778 | return ret; | |
4779 | ||
4780 | uiov = compat_ptr(ptr); | |
4781 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4782 | compat_ssize_t clen; | |
4783 | ||
4784 | if (len > 1) | |
4785 | return -EINVAL; | |
4786 | if (!access_ok(uiov, sizeof(*uiov))) | |
4787 | return -EFAULT; | |
4788 | if (__get_user(clen, &uiov->iov_len)) | |
4789 | return -EFAULT; | |
4790 | if (clen < 0) | |
4791 | return -EINVAL; | |
2d280bc8 PB |
4792 | sr->len = clen; |
4793 | iomsg->iov[0].iov_len = clen; | |
1400e697 | 4794 | iomsg->iov = NULL; |
52de1fe1 | 4795 | } else { |
89cd35c5 CH |
4796 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
4797 | UIO_FASTIOV, &iomsg->iov, | |
4798 | &iomsg->msg.msg_iter, true); | |
52de1fe1 JA |
4799 | if (ret < 0) |
4800 | return ret; | |
4801 | } | |
4802 | ||
4803 | return 0; | |
4804 | } | |
4805 | #endif | |
4806 | ||
1400e697 PB |
4807 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4808 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4809 | { |
1400e697 PB |
4810 | iomsg->msg.msg_name = &iomsg->addr; |
4811 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4812 | |
4813 | #ifdef CONFIG_COMPAT | |
4814 | if (req->ctx->compat) | |
1400e697 | 4815 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4816 | #endif |
52de1fe1 | 4817 | |
1400e697 | 4818 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4819 | } |
4820 | ||
bcda7baa | 4821 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4822 | bool needs_lock) |
bcda7baa JA |
4823 | { |
4824 | struct io_sr_msg *sr = &req->sr_msg; | |
4825 | struct io_buffer *kbuf; | |
4826 | ||
bcda7baa JA |
4827 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4828 | if (IS_ERR(kbuf)) | |
4829 | return kbuf; | |
4830 | ||
4831 | sr->kbuf = kbuf; | |
4832 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4833 | return kbuf; |
fddaface JA |
4834 | } |
4835 | ||
7fbb1b54 PB |
4836 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4837 | { | |
4838 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4839 | } | |
4840 | ||
3529d8c2 JA |
4841 | static int io_recvmsg_prep(struct io_kiocb *req, |
4842 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4843 | { |
e8c2bc1f | 4844 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4845 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4846 | int ret; |
3529d8c2 | 4847 | |
d2b6f48b PB |
4848 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4849 | return -EINVAL; | |
4850 | ||
3529d8c2 | 4851 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4852 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4853 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4854 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4855 | |
d8768362 JA |
4856 | #ifdef CONFIG_COMPAT |
4857 | if (req->ctx->compat) | |
4858 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4859 | #endif | |
4860 | ||
e8c2bc1f | 4861 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
5f798bea | 4862 | return 0; |
e8c2bc1f | 4863 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4864 | if (!ret) |
4865 | req->flags |= REQ_F_NEED_CLEANUP; | |
4866 | return ret; | |
aa1fa28f JA |
4867 | } |
4868 | ||
229a7b63 JA |
4869 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4870 | struct io_comp_state *cs) | |
aa1fa28f | 4871 | { |
6b754c8b | 4872 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4873 | struct socket *sock; |
7fbb1b54 | 4874 | struct io_buffer *kbuf; |
7a7cacba | 4875 | unsigned flags; |
52de1fe1 | 4876 | int ret, cflags = 0; |
03b1230c | 4877 | |
dba4a925 | 4878 | sock = sock_from_file(req->file); |
7a7cacba | 4879 | if (unlikely(!sock)) |
dba4a925 | 4880 | return -ENOTSOCK; |
3529d8c2 | 4881 | |
e8c2bc1f JA |
4882 | if (req->async_data) { |
4883 | kmsg = req->async_data; | |
4884 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4885 | /* if iov is set, it's allocated already */ |
4886 | if (!kmsg->iov) | |
4887 | kmsg->iov = kmsg->fast_iov; | |
4888 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4889 | } else { | |
4890 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4891 | if (ret) | |
681fda8d | 4892 | return ret; |
7a7cacba PB |
4893 | kmsg = &iomsg; |
4894 | } | |
03b1230c | 4895 | |
bc02ef33 | 4896 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4897 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4898 | if (IS_ERR(kbuf)) |
52de1fe1 | 4899 | return PTR_ERR(kbuf); |
7a7cacba PB |
4900 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4901 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4902 | 1, req->sr_msg.len); | |
4903 | } | |
52de1fe1 | 4904 | |
7a7cacba PB |
4905 | flags = req->sr_msg.msg_flags; |
4906 | if (flags & MSG_DONTWAIT) | |
4907 | req->flags |= REQ_F_NOWAIT; | |
4908 | else if (force_nonblock) | |
4909 | flags |= MSG_DONTWAIT; | |
e47293fd | 4910 | |
7a7cacba PB |
4911 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4912 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4913 | if (force_nonblock && ret == -EAGAIN) |
4914 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4915 | if (ret == -ERESTARTSYS) |
4916 | ret = -EINTR; | |
03b1230c | 4917 | |
7fbb1b54 PB |
4918 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4919 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4920 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4921 | kfree(kmsg->iov); |
99bc4c38 | 4922 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4923 | if (ret < 0) |
4924 | req_set_fail_links(req); | |
229a7b63 | 4925 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4926 | return 0; |
0fa03c62 | 4927 | } |
5d17b4a4 | 4928 | |
229a7b63 JA |
4929 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4930 | struct io_comp_state *cs) | |
fddaface | 4931 | { |
6b754c8b | 4932 | struct io_buffer *kbuf; |
7a7cacba PB |
4933 | struct io_sr_msg *sr = &req->sr_msg; |
4934 | struct msghdr msg; | |
4935 | void __user *buf = sr->buf; | |
fddaface | 4936 | struct socket *sock; |
7a7cacba PB |
4937 | struct iovec iov; |
4938 | unsigned flags; | |
bcda7baa | 4939 | int ret, cflags = 0; |
fddaface | 4940 | |
dba4a925 | 4941 | sock = sock_from_file(req->file); |
7a7cacba | 4942 | if (unlikely(!sock)) |
dba4a925 | 4943 | return -ENOTSOCK; |
fddaface | 4944 | |
bc02ef33 | 4945 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4946 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4947 | if (IS_ERR(kbuf)) |
4948 | return PTR_ERR(kbuf); | |
7a7cacba | 4949 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4950 | } |
bcda7baa | 4951 | |
7a7cacba | 4952 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4953 | if (unlikely(ret)) |
4954 | goto out_free; | |
fddaface | 4955 | |
7a7cacba PB |
4956 | msg.msg_name = NULL; |
4957 | msg.msg_control = NULL; | |
4958 | msg.msg_controllen = 0; | |
4959 | msg.msg_namelen = 0; | |
4960 | msg.msg_iocb = NULL; | |
4961 | msg.msg_flags = 0; | |
fddaface | 4962 | |
7a7cacba PB |
4963 | flags = req->sr_msg.msg_flags; |
4964 | if (flags & MSG_DONTWAIT) | |
4965 | req->flags |= REQ_F_NOWAIT; | |
4966 | else if (force_nonblock) | |
4967 | flags |= MSG_DONTWAIT; | |
4968 | ||
4969 | ret = sock_recvmsg(sock, &msg, flags); | |
4970 | if (force_nonblock && ret == -EAGAIN) | |
4971 | return -EAGAIN; | |
4972 | if (ret == -ERESTARTSYS) | |
4973 | ret = -EINTR; | |
14c32eee | 4974 | out_free: |
7fbb1b54 PB |
4975 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4976 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4977 | if (ret < 0) |
4978 | req_set_fail_links(req); | |
229a7b63 | 4979 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4980 | return 0; |
fddaface JA |
4981 | } |
4982 | ||
3529d8c2 | 4983 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4984 | { |
8ed8d3c3 JA |
4985 | struct io_accept *accept = &req->accept; |
4986 | ||
14587a46 | 4987 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4988 | return -EINVAL; |
8042d6ce | 4989 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4990 | return -EINVAL; |
4991 | ||
d55e5f5b JA |
4992 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4993 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4994 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4995 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4996 | return 0; |
8ed8d3c3 | 4997 | } |
17f2fe35 | 4998 | |
229a7b63 JA |
4999 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
5000 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
5001 | { |
5002 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 5003 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
5004 | int ret; |
5005 | ||
e697deed JX |
5006 | if (req->file->f_flags & O_NONBLOCK) |
5007 | req->flags |= REQ_F_NOWAIT; | |
5008 | ||
8ed8d3c3 | 5009 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
5010 | accept->addr_len, accept->flags, |
5011 | accept->nofile); | |
8ed8d3c3 | 5012 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 5013 | return -EAGAIN; |
ac45abc0 PB |
5014 | if (ret < 0) { |
5015 | if (ret == -ERESTARTSYS) | |
5016 | ret = -EINTR; | |
4e88d6e7 | 5017 | req_set_fail_links(req); |
ac45abc0 | 5018 | } |
229a7b63 | 5019 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 5020 | return 0; |
8ed8d3c3 JA |
5021 | } |
5022 | ||
3529d8c2 | 5023 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5024 | { |
3529d8c2 | 5025 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 5026 | struct io_async_connect *io = req->async_data; |
f499a021 | 5027 | |
14587a46 | 5028 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
5029 | return -EINVAL; |
5030 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
5031 | return -EINVAL; | |
5032 | ||
3529d8c2 JA |
5033 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5034 | conn->addr_len = READ_ONCE(sqe->addr2); | |
5035 | ||
5036 | if (!io) | |
5037 | return 0; | |
5038 | ||
5039 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 5040 | &io->address); |
f499a021 JA |
5041 | } |
5042 | ||
229a7b63 JA |
5043 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5044 | struct io_comp_state *cs) | |
f8e85cf2 | 5045 | { |
e8c2bc1f | 5046 | struct io_async_connect __io, *io; |
f8e85cf2 | 5047 | unsigned file_flags; |
3fbb51c1 | 5048 | int ret; |
f8e85cf2 | 5049 | |
e8c2bc1f JA |
5050 | if (req->async_data) { |
5051 | io = req->async_data; | |
f499a021 | 5052 | } else { |
3529d8c2 JA |
5053 | ret = move_addr_to_kernel(req->connect.addr, |
5054 | req->connect.addr_len, | |
e8c2bc1f | 5055 | &__io.address); |
f499a021 JA |
5056 | if (ret) |
5057 | goto out; | |
5058 | io = &__io; | |
5059 | } | |
5060 | ||
3fbb51c1 JA |
5061 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5062 | ||
e8c2bc1f | 5063 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5064 | req->connect.addr_len, file_flags); |
87f80d62 | 5065 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5066 | if (req->async_data) |
b7bb4f7d | 5067 | return -EAGAIN; |
e8c2bc1f | 5068 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5069 | ret = -ENOMEM; |
5070 | goto out; | |
5071 | } | |
e8c2bc1f JA |
5072 | io = req->async_data; |
5073 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5074 | return -EAGAIN; |
f499a021 | 5075 | } |
f8e85cf2 JA |
5076 | if (ret == -ERESTARTSYS) |
5077 | ret = -EINTR; | |
f499a021 | 5078 | out: |
4e88d6e7 JA |
5079 | if (ret < 0) |
5080 | req_set_fail_links(req); | |
229a7b63 | 5081 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 5082 | return 0; |
469956e8 Y |
5083 | } |
5084 | #else /* !CONFIG_NET */ | |
5085 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5086 | { | |
f8e85cf2 | 5087 | return -EOPNOTSUPP; |
f8e85cf2 JA |
5088 | } |
5089 | ||
1e16c2f9 RD |
5090 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
5091 | struct io_comp_state *cs) | |
469956e8 Y |
5092 | { |
5093 | return -EOPNOTSUPP; | |
5094 | } | |
5095 | ||
1e16c2f9 RD |
5096 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
5097 | struct io_comp_state *cs) | |
469956e8 Y |
5098 | { |
5099 | return -EOPNOTSUPP; | |
5100 | } | |
5101 | ||
5102 | static int io_recvmsg_prep(struct io_kiocb *req, | |
5103 | const struct io_uring_sqe *sqe) | |
5104 | { | |
5105 | return -EOPNOTSUPP; | |
5106 | } | |
5107 | ||
1e16c2f9 RD |
5108 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
5109 | struct io_comp_state *cs) | |
469956e8 Y |
5110 | { |
5111 | return -EOPNOTSUPP; | |
5112 | } | |
5113 | ||
1e16c2f9 RD |
5114 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
5115 | struct io_comp_state *cs) | |
469956e8 Y |
5116 | { |
5117 | return -EOPNOTSUPP; | |
5118 | } | |
5119 | ||
5120 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5121 | { | |
5122 | return -EOPNOTSUPP; | |
5123 | } | |
5124 | ||
1e16c2f9 RD |
5125 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
5126 | struct io_comp_state *cs) | |
469956e8 Y |
5127 | { |
5128 | return -EOPNOTSUPP; | |
5129 | } | |
ce593a6c | 5130 | |
469956e8 Y |
5131 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5132 | { | |
5133 | return -EOPNOTSUPP; | |
5134 | } | |
5135 | ||
1e16c2f9 RD |
5136 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5137 | struct io_comp_state *cs) | |
469956e8 | 5138 | { |
f8e85cf2 | 5139 | return -EOPNOTSUPP; |
ce593a6c | 5140 | } |
469956e8 | 5141 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5142 | |
d7718a9d JA |
5143 | struct io_poll_table { |
5144 | struct poll_table_struct pt; | |
5145 | struct io_kiocb *req; | |
5146 | int error; | |
5147 | }; | |
ce593a6c | 5148 | |
d7718a9d JA |
5149 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5150 | __poll_t mask, task_work_func_t func) | |
5151 | { | |
aa96bf8a | 5152 | int ret; |
d7718a9d JA |
5153 | |
5154 | /* for instances that support it check for an event match first: */ | |
5155 | if (mask && !(mask & poll->events)) | |
5156 | return 0; | |
5157 | ||
5158 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5159 | ||
5160 | list_del_init(&poll->wait.entry); | |
5161 | ||
d7718a9d JA |
5162 | req->result = mask; |
5163 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
5164 | percpu_ref_get(&req->ctx->refs); |
5165 | ||
d7718a9d | 5166 | /* |
e3aabf95 JA |
5167 | * If this fails, then the task is exiting. When a task exits, the |
5168 | * work gets canceled, so just cancel this request as well instead | |
5169 | * of executing it. We can't safely execute it anyway, as we may not | |
5170 | * have the needed state needed for it anyway. | |
d7718a9d | 5171 | */ |
355fb9e2 | 5172 | ret = io_req_task_work_add(req); |
aa96bf8a | 5173 | if (unlikely(ret)) { |
c2c4c83c JA |
5174 | struct task_struct *tsk; |
5175 | ||
e3aabf95 | 5176 | WRITE_ONCE(poll->canceled, true); |
aa96bf8a | 5177 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 5178 | task_work_add(tsk, &req->task_work, TWA_NONE); |
ce593a6c | 5179 | wake_up_process(tsk); |
aa96bf8a | 5180 | } |
d7718a9d JA |
5181 | return 1; |
5182 | } | |
5183 | ||
74ce6ce4 JA |
5184 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5185 | __acquires(&req->ctx->completion_lock) | |
5186 | { | |
5187 | struct io_ring_ctx *ctx = req->ctx; | |
5188 | ||
5189 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5190 | struct poll_table_struct pt = { ._key = poll->events }; | |
5191 | ||
5192 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5193 | } | |
5194 | ||
5195 | spin_lock_irq(&ctx->completion_lock); | |
5196 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5197 | add_wait_queue(poll->head, &poll->wait); | |
5198 | return true; | |
5199 | } | |
5200 | ||
5201 | return false; | |
5202 | } | |
5203 | ||
d4e7cd36 | 5204 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5205 | { |
e8c2bc1f | 5206 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5207 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5208 | return req->async_data; |
d4e7cd36 JA |
5209 | return req->apoll->double_poll; |
5210 | } | |
5211 | ||
5212 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5213 | { | |
5214 | if (req->opcode == IORING_OP_POLL_ADD) | |
5215 | return &req->poll; | |
5216 | return &req->apoll->poll; | |
5217 | } | |
5218 | ||
5219 | static void io_poll_remove_double(struct io_kiocb *req) | |
5220 | { | |
5221 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5222 | |
5223 | lockdep_assert_held(&req->ctx->completion_lock); | |
5224 | ||
5225 | if (poll && poll->head) { | |
5226 | struct wait_queue_head *head = poll->head; | |
5227 | ||
5228 | spin_lock(&head->lock); | |
5229 | list_del_init(&poll->wait.entry); | |
5230 | if (poll->wait.private) | |
5231 | refcount_dec(&req->refs); | |
5232 | poll->head = NULL; | |
5233 | spin_unlock(&head->lock); | |
5234 | } | |
5235 | } | |
5236 | ||
5237 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5238 | { | |
5239 | struct io_ring_ctx *ctx = req->ctx; | |
5240 | ||
d4e7cd36 | 5241 | io_poll_remove_double(req); |
18bceab1 JA |
5242 | req->poll.done = true; |
5243 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5244 | io_commit_cqring(ctx); | |
5245 | } | |
5246 | ||
dd221f46 | 5247 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5248 | { |
dd221f46 | 5249 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5250 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5251 | struct io_kiocb *nxt; |
18bceab1 JA |
5252 | |
5253 | if (io_poll_rewait(req, &req->poll)) { | |
5254 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5255 | } else { |
5256 | hash_del(&req->hash_node); | |
5257 | io_poll_complete(req, req->result, 0); | |
5258 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5259 | |
dd221f46 PB |
5260 | nxt = io_put_req_find_next(req); |
5261 | io_cqring_ev_posted(ctx); | |
5262 | if (nxt) | |
5263 | __io_req_task_submit(nxt); | |
5264 | } | |
18bceab1 | 5265 | |
6d816e08 | 5266 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5267 | } |
5268 | ||
5269 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5270 | int sync, void *key) | |
5271 | { | |
5272 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5273 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5274 | __poll_t mask = key_to_poll(key); |
5275 | ||
5276 | /* for instances that support it check for an event match first: */ | |
5277 | if (mask && !(mask & poll->events)) | |
5278 | return 0; | |
5279 | ||
8706e04e JA |
5280 | list_del_init(&wait->entry); |
5281 | ||
807abcb0 | 5282 | if (poll && poll->head) { |
18bceab1 JA |
5283 | bool done; |
5284 | ||
807abcb0 JA |
5285 | spin_lock(&poll->head->lock); |
5286 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5287 | if (!done) |
807abcb0 | 5288 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5289 | /* make sure double remove sees this as being gone */ |
5290 | wait->private = NULL; | |
807abcb0 | 5291 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5292 | if (!done) { |
5293 | /* use wait func handler, so it matches the rq type */ | |
5294 | poll->wait.func(&poll->wait, mode, sync, key); | |
5295 | } | |
18bceab1 JA |
5296 | } |
5297 | refcount_dec(&req->refs); | |
5298 | return 1; | |
5299 | } | |
5300 | ||
5301 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5302 | wait_queue_func_t wake_func) | |
5303 | { | |
5304 | poll->head = NULL; | |
5305 | poll->done = false; | |
5306 | poll->canceled = false; | |
5307 | poll->events = events; | |
5308 | INIT_LIST_HEAD(&poll->wait.entry); | |
5309 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5310 | } | |
5311 | ||
5312 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5313 | struct wait_queue_head *head, |
5314 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5315 | { |
5316 | struct io_kiocb *req = pt->req; | |
5317 | ||
5318 | /* | |
5319 | * If poll->head is already set, it's because the file being polled | |
5320 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5321 | * for write). Setup a separate io_poll_iocb if this happens. | |
5322 | */ | |
5323 | if (unlikely(poll->head)) { | |
58852d4d PB |
5324 | struct io_poll_iocb *poll_one = poll; |
5325 | ||
18bceab1 | 5326 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5327 | if (*poll_ptr) { |
18bceab1 JA |
5328 | pt->error = -EINVAL; |
5329 | return; | |
5330 | } | |
5331 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5332 | if (!poll) { | |
5333 | pt->error = -ENOMEM; | |
5334 | return; | |
5335 | } | |
58852d4d | 5336 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5337 | refcount_inc(&req->refs); |
5338 | poll->wait.private = req; | |
807abcb0 | 5339 | *poll_ptr = poll; |
18bceab1 JA |
5340 | } |
5341 | ||
5342 | pt->error = 0; | |
5343 | poll->head = head; | |
a31eb4a2 JX |
5344 | |
5345 | if (poll->events & EPOLLEXCLUSIVE) | |
5346 | add_wait_queue_exclusive(head, &poll->wait); | |
5347 | else | |
5348 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5349 | } |
5350 | ||
5351 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5352 | struct poll_table_struct *p) | |
5353 | { | |
5354 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5355 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5356 | |
807abcb0 | 5357 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5358 | } |
5359 | ||
d7718a9d JA |
5360 | static void io_async_task_func(struct callback_head *cb) |
5361 | { | |
5362 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5363 | struct async_poll *apoll = req->apoll; | |
5364 | struct io_ring_ctx *ctx = req->ctx; | |
5365 | ||
5366 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5367 | ||
74ce6ce4 | 5368 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5369 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5370 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5371 | return; |
d7718a9d JA |
5372 | } |
5373 | ||
31067255 | 5374 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5375 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5376 | hash_del(&req->hash_node); |
2bae047e | 5377 | |
d4e7cd36 | 5378 | io_poll_remove_double(req); |
74ce6ce4 JA |
5379 | spin_unlock_irq(&ctx->completion_lock); |
5380 | ||
0be0b0e3 PB |
5381 | if (!READ_ONCE(apoll->poll.canceled)) |
5382 | __io_req_task_submit(req); | |
5383 | else | |
5384 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5385 | |
6d816e08 | 5386 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5387 | kfree(apoll->double_poll); |
31067255 | 5388 | kfree(apoll); |
d7718a9d JA |
5389 | } |
5390 | ||
5391 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5392 | void *key) | |
5393 | { | |
5394 | struct io_kiocb *req = wait->private; | |
5395 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5396 | ||
5397 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5398 | key_to_poll(key)); | |
5399 | ||
5400 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5401 | } | |
5402 | ||
5403 | static void io_poll_req_insert(struct io_kiocb *req) | |
5404 | { | |
5405 | struct io_ring_ctx *ctx = req->ctx; | |
5406 | struct hlist_head *list; | |
5407 | ||
5408 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5409 | hlist_add_head(&req->hash_node, list); | |
5410 | } | |
5411 | ||
5412 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5413 | struct io_poll_iocb *poll, | |
5414 | struct io_poll_table *ipt, __poll_t mask, | |
5415 | wait_queue_func_t wake_func) | |
5416 | __acquires(&ctx->completion_lock) | |
5417 | { | |
5418 | struct io_ring_ctx *ctx = req->ctx; | |
5419 | bool cancel = false; | |
5420 | ||
4d52f338 | 5421 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5422 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5423 | poll->file = req->file; |
18bceab1 | 5424 | poll->wait.private = req; |
d7718a9d JA |
5425 | |
5426 | ipt->pt._key = mask; | |
5427 | ipt->req = req; | |
5428 | ipt->error = -EINVAL; | |
5429 | ||
d7718a9d JA |
5430 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5431 | ||
5432 | spin_lock_irq(&ctx->completion_lock); | |
5433 | if (likely(poll->head)) { | |
5434 | spin_lock(&poll->head->lock); | |
5435 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5436 | if (ipt->error) | |
5437 | cancel = true; | |
5438 | ipt->error = 0; | |
5439 | mask = 0; | |
5440 | } | |
5441 | if (mask || ipt->error) | |
5442 | list_del_init(&poll->wait.entry); | |
5443 | else if (cancel) | |
5444 | WRITE_ONCE(poll->canceled, true); | |
5445 | else if (!poll->done) /* actually waiting for an event */ | |
5446 | io_poll_req_insert(req); | |
5447 | spin_unlock(&poll->head->lock); | |
5448 | } | |
5449 | ||
5450 | return mask; | |
5451 | } | |
5452 | ||
5453 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5454 | { | |
5455 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5456 | struct io_ring_ctx *ctx = req->ctx; | |
5457 | struct async_poll *apoll; | |
5458 | struct io_poll_table ipt; | |
5459 | __poll_t mask, ret; | |
9dab14b8 | 5460 | int rw; |
d7718a9d JA |
5461 | |
5462 | if (!req->file || !file_can_poll(req->file)) | |
5463 | return false; | |
24c74678 | 5464 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5465 | return false; |
9dab14b8 JA |
5466 | if (def->pollin) |
5467 | rw = READ; | |
5468 | else if (def->pollout) | |
5469 | rw = WRITE; | |
5470 | else | |
5471 | return false; | |
5472 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5473 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5474 | return false; |
5475 | ||
5476 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5477 | if (unlikely(!apoll)) | |
5478 | return false; | |
807abcb0 | 5479 | apoll->double_poll = NULL; |
d7718a9d JA |
5480 | |
5481 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5482 | req->apoll = apoll; |
d7718a9d | 5483 | |
8755d97a | 5484 | mask = 0; |
d7718a9d | 5485 | if (def->pollin) |
8755d97a | 5486 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5487 | if (def->pollout) |
5488 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5489 | |
5490 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5491 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5492 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5493 | mask &= ~POLLIN; | |
5494 | ||
d7718a9d JA |
5495 | mask |= POLLERR | POLLPRI; |
5496 | ||
5497 | ipt.pt._qproc = io_async_queue_proc; | |
5498 | ||
5499 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5500 | io_async_wake); | |
a36da65c | 5501 | if (ret || ipt.error) { |
d4e7cd36 | 5502 | io_poll_remove_double(req); |
d7718a9d | 5503 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5504 | kfree(apoll->double_poll); |
d7718a9d JA |
5505 | kfree(apoll); |
5506 | return false; | |
5507 | } | |
5508 | spin_unlock_irq(&ctx->completion_lock); | |
5509 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5510 | apoll->poll.events); | |
5511 | return true; | |
5512 | } | |
5513 | ||
5514 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5515 | struct io_poll_iocb *poll) | |
221c5eb2 | 5516 | { |
b41e9852 | 5517 | bool do_complete = false; |
221c5eb2 JA |
5518 | |
5519 | spin_lock(&poll->head->lock); | |
5520 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5521 | if (!list_empty(&poll->wait.entry)) { |
5522 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5523 | do_complete = true; |
221c5eb2 JA |
5524 | } |
5525 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5526 | hash_del(&req->hash_node); |
d7718a9d JA |
5527 | return do_complete; |
5528 | } | |
5529 | ||
5530 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5531 | { | |
5532 | bool do_complete; | |
5533 | ||
d4e7cd36 JA |
5534 | io_poll_remove_double(req); |
5535 | ||
d7718a9d JA |
5536 | if (req->opcode == IORING_OP_POLL_ADD) { |
5537 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5538 | } else { | |
3bfa5bcb JA |
5539 | struct async_poll *apoll = req->apoll; |
5540 | ||
d7718a9d | 5541 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5542 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5543 | if (do_complete) { | |
d7718a9d | 5544 | io_put_req(req); |
807abcb0 | 5545 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5546 | kfree(apoll); |
5547 | } | |
b1f573bd XW |
5548 | } |
5549 | ||
b41e9852 JA |
5550 | if (do_complete) { |
5551 | io_cqring_fill_event(req, -ECANCELED); | |
5552 | io_commit_cqring(req->ctx); | |
f254ac04 | 5553 | req_set_fail_links(req); |
216578e5 | 5554 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5555 | } |
5556 | ||
5557 | return do_complete; | |
221c5eb2 JA |
5558 | } |
5559 | ||
76e1b642 JA |
5560 | /* |
5561 | * Returns true if we found and killed one or more poll requests | |
5562 | */ | |
6b81928d PB |
5563 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5564 | struct files_struct *files) | |
221c5eb2 | 5565 | { |
78076bb6 | 5566 | struct hlist_node *tmp; |
221c5eb2 | 5567 | struct io_kiocb *req; |
8e2e1faf | 5568 | int posted = 0, i; |
221c5eb2 JA |
5569 | |
5570 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5571 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5572 | struct hlist_head *list; | |
5573 | ||
5574 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5575 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5576 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5577 | posted += io_poll_remove_one(req); |
5578 | } | |
221c5eb2 JA |
5579 | } |
5580 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5581 | |
8e2e1faf JA |
5582 | if (posted) |
5583 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5584 | |
5585 | return posted != 0; | |
221c5eb2 JA |
5586 | } |
5587 | ||
47f46768 JA |
5588 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5589 | { | |
78076bb6 | 5590 | struct hlist_head *list; |
47f46768 JA |
5591 | struct io_kiocb *req; |
5592 | ||
78076bb6 JA |
5593 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5594 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5595 | if (sqe_addr != req->user_data) |
5596 | continue; | |
5597 | if (io_poll_remove_one(req)) | |
eac406c6 | 5598 | return 0; |
b41e9852 | 5599 | return -EALREADY; |
47f46768 JA |
5600 | } |
5601 | ||
5602 | return -ENOENT; | |
5603 | } | |
5604 | ||
3529d8c2 JA |
5605 | static int io_poll_remove_prep(struct io_kiocb *req, |
5606 | const struct io_uring_sqe *sqe) | |
0969e783 | 5607 | { |
0969e783 JA |
5608 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5609 | return -EINVAL; | |
5610 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5611 | sqe->poll_events) | |
5612 | return -EINVAL; | |
5613 | ||
018043be | 5614 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5615 | return 0; |
5616 | } | |
5617 | ||
221c5eb2 JA |
5618 | /* |
5619 | * Find a running poll command that matches one specified in sqe->addr, | |
5620 | * and remove it if found. | |
5621 | */ | |
fc4df999 | 5622 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5623 | { |
5624 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5625 | int ret; |
221c5eb2 | 5626 | |
221c5eb2 | 5627 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5628 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5629 | spin_unlock_irq(&ctx->completion_lock); |
5630 | ||
4e88d6e7 JA |
5631 | if (ret < 0) |
5632 | req_set_fail_links(req); | |
e1e16097 | 5633 | io_req_complete(req, ret); |
221c5eb2 JA |
5634 | return 0; |
5635 | } | |
5636 | ||
221c5eb2 JA |
5637 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5638 | void *key) | |
5639 | { | |
c2f2eb7d JA |
5640 | struct io_kiocb *req = wait->private; |
5641 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5642 | |
d7718a9d | 5643 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5644 | } |
5645 | ||
221c5eb2 JA |
5646 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5647 | struct poll_table_struct *p) | |
5648 | { | |
5649 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5650 | ||
e8c2bc1f | 5651 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5652 | } |
5653 | ||
3529d8c2 | 5654 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5655 | { |
5656 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5657 | u32 events; |
221c5eb2 JA |
5658 | |
5659 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5660 | return -EINVAL; | |
5661 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5662 | return -EINVAL; | |
5663 | ||
5769a351 JX |
5664 | events = READ_ONCE(sqe->poll32_events); |
5665 | #ifdef __BIG_ENDIAN | |
5666 | events = swahw32(events); | |
5667 | #endif | |
a31eb4a2 JX |
5668 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5669 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5670 | return 0; |
5671 | } | |
5672 | ||
014db007 | 5673 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5674 | { |
5675 | struct io_poll_iocb *poll = &req->poll; | |
5676 | struct io_ring_ctx *ctx = req->ctx; | |
5677 | struct io_poll_table ipt; | |
0969e783 | 5678 | __poll_t mask; |
0969e783 | 5679 | |
d7718a9d | 5680 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5681 | |
d7718a9d JA |
5682 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5683 | io_poll_wake); | |
221c5eb2 | 5684 | |
8c838788 | 5685 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5686 | ipt.error = 0; |
b0dd8a41 | 5687 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5688 | } |
221c5eb2 JA |
5689 | spin_unlock_irq(&ctx->completion_lock); |
5690 | ||
8c838788 JA |
5691 | if (mask) { |
5692 | io_cqring_ev_posted(ctx); | |
014db007 | 5693 | io_put_req(req); |
221c5eb2 | 5694 | } |
8c838788 | 5695 | return ipt.error; |
221c5eb2 JA |
5696 | } |
5697 | ||
5262f567 JA |
5698 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5699 | { | |
ad8a48ac JA |
5700 | struct io_timeout_data *data = container_of(timer, |
5701 | struct io_timeout_data, timer); | |
5702 | struct io_kiocb *req = data->req; | |
5703 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5704 | unsigned long flags; |
5705 | ||
5262f567 | 5706 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5707 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5708 | atomic_set(&req->ctx->cq_timeouts, |
5709 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5710 | ||
78e19bbe | 5711 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5712 | io_commit_cqring(ctx); |
5713 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5714 | ||
5715 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5716 | req_set_fail_links(req); |
5262f567 JA |
5717 | io_put_req(req); |
5718 | return HRTIMER_NORESTART; | |
5719 | } | |
5720 | ||
fbd15848 PB |
5721 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5722 | __u64 user_data) | |
f254ac04 | 5723 | { |
fbd15848 | 5724 | struct io_timeout_data *io; |
47f46768 JA |
5725 | struct io_kiocb *req; |
5726 | int ret = -ENOENT; | |
f254ac04 | 5727 | |
135fcde8 | 5728 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5729 | if (user_data == req->user_data) { |
47f46768 JA |
5730 | ret = 0; |
5731 | break; | |
5732 | } | |
5733 | } | |
5734 | ||
5735 | if (ret == -ENOENT) | |
fbd15848 PB |
5736 | return ERR_PTR(ret); |
5737 | ||
5738 | io = req->async_data; | |
e8c2bc1f | 5739 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5740 | if (ret == -1) |
fbd15848 | 5741 | return ERR_PTR(-EALREADY); |
a71976f3 | 5742 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5743 | return req; |
5744 | } | |
47f46768 | 5745 | |
fbd15848 PB |
5746 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5747 | { | |
5748 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5749 | ||
5750 | if (IS_ERR(req)) | |
5751 | return PTR_ERR(req); | |
f254ac04 JA |
5752 | |
5753 | req_set_fail_links(req); | |
f254ac04 | 5754 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5755 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5756 | return 0; |
5757 | } | |
5758 | ||
9c8e11b3 PB |
5759 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5760 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5761 | { |
9c8e11b3 PB |
5762 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5763 | struct io_timeout_data *data; | |
47f46768 | 5764 | |
9c8e11b3 PB |
5765 | if (IS_ERR(req)) |
5766 | return PTR_ERR(req); | |
47f46768 | 5767 | |
9c8e11b3 PB |
5768 | req->timeout.off = 0; /* noseq */ |
5769 | data = req->async_data; | |
5770 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5771 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5772 | data->timer.function = io_timeout_fn; | |
5773 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5774 | return 0; | |
47f46768 JA |
5775 | } |
5776 | ||
3529d8c2 JA |
5777 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5778 | const struct io_uring_sqe *sqe) | |
b29472ee | 5779 | { |
9c8e11b3 PB |
5780 | struct io_timeout_rem *tr = &req->timeout_rem; |
5781 | ||
b29472ee JA |
5782 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5783 | return -EINVAL; | |
61710e43 DA |
5784 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5785 | return -EINVAL; | |
9c8e11b3 | 5786 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5787 | return -EINVAL; |
5788 | ||
9c8e11b3 PB |
5789 | tr->addr = READ_ONCE(sqe->addr); |
5790 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5791 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5792 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5793 | return -EINVAL; | |
5794 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5795 | return -EFAULT; | |
5796 | } else if (tr->flags) { | |
5797 | /* timeout removal doesn't support flags */ | |
b29472ee | 5798 | return -EINVAL; |
9c8e11b3 | 5799 | } |
b29472ee | 5800 | |
b29472ee JA |
5801 | return 0; |
5802 | } | |
5803 | ||
11365043 JA |
5804 | /* |
5805 | * Remove or update an existing timeout command | |
5806 | */ | |
fc4df999 | 5807 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 | 5808 | { |
9c8e11b3 | 5809 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5810 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5811 | int ret; |
11365043 | 5812 | |
11365043 | 5813 | spin_lock_irq(&ctx->completion_lock); |
9c8e11b3 PB |
5814 | if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) { |
5815 | enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS) | |
5816 | ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; | |
5817 | ||
5818 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
5819 | } else { | |
5820 | ret = io_timeout_cancel(ctx, tr->addr); | |
5821 | } | |
11365043 | 5822 | |
47f46768 | 5823 | io_cqring_fill_event(req, ret); |
11365043 JA |
5824 | io_commit_cqring(ctx); |
5825 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5826 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5827 | if (ret < 0) |
5828 | req_set_fail_links(req); | |
ec9c02ad | 5829 | io_put_req(req); |
11365043 | 5830 | return 0; |
5262f567 JA |
5831 | } |
5832 | ||
3529d8c2 | 5833 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5834 | bool is_timeout_link) |
5262f567 | 5835 | { |
ad8a48ac | 5836 | struct io_timeout_data *data; |
a41525ab | 5837 | unsigned flags; |
56080b02 | 5838 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5839 | |
ad8a48ac | 5840 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5841 | return -EINVAL; |
ad8a48ac | 5842 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5843 | return -EINVAL; |
56080b02 | 5844 | if (off && is_timeout_link) |
2d28390a | 5845 | return -EINVAL; |
a41525ab JA |
5846 | flags = READ_ONCE(sqe->timeout_flags); |
5847 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5848 | return -EINVAL; |
bdf20073 | 5849 | |
bfe68a22 | 5850 | req->timeout.off = off; |
26a61679 | 5851 | |
e8c2bc1f | 5852 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5853 | return -ENOMEM; |
5854 | ||
e8c2bc1f | 5855 | data = req->async_data; |
ad8a48ac | 5856 | data->req = req; |
ad8a48ac JA |
5857 | |
5858 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5859 | return -EFAULT; |
5860 | ||
11365043 | 5861 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 5862 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 5863 | else |
ad8a48ac | 5864 | data->mode = HRTIMER_MODE_REL; |
11365043 | 5865 | |
ad8a48ac JA |
5866 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5867 | return 0; | |
5868 | } | |
5869 | ||
fc4df999 | 5870 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5871 | { |
ad8a48ac | 5872 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5873 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5874 | struct list_head *entry; |
bfe68a22 | 5875 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5876 | |
733f5c95 | 5877 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5878 | |
5262f567 JA |
5879 | /* |
5880 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5881 | * timeout event to be satisfied. If it isn't set, then this is |
5882 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5883 | */ |
8eb7e2d0 | 5884 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5885 | entry = ctx->timeout_list.prev; |
5886 | goto add; | |
5887 | } | |
5262f567 | 5888 | |
bfe68a22 PB |
5889 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5890 | req->timeout.target_seq = tail + off; | |
5262f567 | 5891 | |
f010505b MDG |
5892 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5893 | * This is safe because ->completion_lock is held, and submissions | |
5894 | * and completions are never mixed in the same ->completion_lock section. | |
5895 | */ | |
5896 | ctx->cq_last_tm_flush = tail; | |
5897 | ||
5262f567 JA |
5898 | /* |
5899 | * Insertion sort, ensuring the first entry in the list is always | |
5900 | * the one we need first. | |
5901 | */ | |
5262f567 | 5902 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5903 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5904 | timeout.list); | |
5262f567 | 5905 | |
8eb7e2d0 | 5906 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5907 | continue; |
bfe68a22 PB |
5908 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5909 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5910 | break; |
5911 | } | |
93bd25bb | 5912 | add: |
135fcde8 | 5913 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5914 | data->timer.function = io_timeout_fn; |
5915 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5916 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5917 | return 0; |
5918 | } | |
5262f567 | 5919 | |
62755e35 JA |
5920 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5921 | { | |
5922 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5923 | ||
5924 | return req->user_data == (unsigned long) data; | |
5925 | } | |
5926 | ||
e977d6d3 | 5927 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5928 | { |
62755e35 | 5929 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5930 | int ret = 0; |
5931 | ||
4f26bda1 | 5932 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5933 | switch (cancel_ret) { |
5934 | case IO_WQ_CANCEL_OK: | |
5935 | ret = 0; | |
5936 | break; | |
5937 | case IO_WQ_CANCEL_RUNNING: | |
5938 | ret = -EALREADY; | |
5939 | break; | |
5940 | case IO_WQ_CANCEL_NOTFOUND: | |
5941 | ret = -ENOENT; | |
5942 | break; | |
5943 | } | |
5944 | ||
e977d6d3 JA |
5945 | return ret; |
5946 | } | |
5947 | ||
47f46768 JA |
5948 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5949 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5950 | int success_ret) |
47f46768 JA |
5951 | { |
5952 | unsigned long flags; | |
5953 | int ret; | |
5954 | ||
5955 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5956 | if (ret != -ENOENT) { | |
5957 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5958 | goto done; | |
5959 | } | |
5960 | ||
5961 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5962 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5963 | if (ret != -ENOENT) | |
5964 | goto done; | |
5965 | ret = io_poll_cancel(ctx, sqe_addr); | |
5966 | done: | |
b0dd8a41 JA |
5967 | if (!ret) |
5968 | ret = success_ret; | |
47f46768 JA |
5969 | io_cqring_fill_event(req, ret); |
5970 | io_commit_cqring(ctx); | |
5971 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5972 | io_cqring_ev_posted(ctx); | |
5973 | ||
4e88d6e7 JA |
5974 | if (ret < 0) |
5975 | req_set_fail_links(req); | |
014db007 | 5976 | io_put_req(req); |
47f46768 JA |
5977 | } |
5978 | ||
3529d8c2 JA |
5979 | static int io_async_cancel_prep(struct io_kiocb *req, |
5980 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5981 | { |
fbf23849 | 5982 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5983 | return -EINVAL; |
61710e43 DA |
5984 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5985 | return -EINVAL; | |
5986 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5987 | return -EINVAL; |
5988 | ||
fbf23849 JA |
5989 | req->cancel.addr = READ_ONCE(sqe->addr); |
5990 | return 0; | |
5991 | } | |
5992 | ||
014db007 | 5993 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
5994 | { |
5995 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5996 | |
014db007 | 5997 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5998 | return 0; |
5999 | } | |
6000 | ||
269bbe5f | 6001 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6002 | const struct io_uring_sqe *sqe) |
6003 | { | |
6ca56f84 JA |
6004 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
6005 | return -EINVAL; | |
61710e43 DA |
6006 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6007 | return -EINVAL; | |
6008 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
6009 | return -EINVAL; |
6010 | ||
269bbe5f BM |
6011 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6012 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6013 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6014 | return -EINVAL; |
269bbe5f | 6015 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6016 | return 0; |
6017 | } | |
6018 | ||
229a7b63 JA |
6019 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
6020 | struct io_comp_state *cs) | |
fbf23849 JA |
6021 | { |
6022 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 6023 | struct io_uring_rsrc_update up; |
05f3fb3c | 6024 | int ret; |
fbf23849 | 6025 | |
f86cd20c | 6026 | if (force_nonblock) |
05f3fb3c | 6027 | return -EAGAIN; |
05f3fb3c | 6028 | |
269bbe5f BM |
6029 | up.offset = req->rsrc_update.offset; |
6030 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
6031 | |
6032 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 6033 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6034 | mutex_unlock(&ctx->uring_lock); |
6035 | ||
6036 | if (ret < 0) | |
6037 | req_set_fail_links(req); | |
229a7b63 | 6038 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
6039 | return 0; |
6040 | } | |
6041 | ||
bfe76559 | 6042 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6043 | { |
d625c6ee | 6044 | switch (req->opcode) { |
e781573e | 6045 | case IORING_OP_NOP: |
bfe76559 | 6046 | return 0; |
f67676d1 JA |
6047 | case IORING_OP_READV: |
6048 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6049 | case IORING_OP_READ: |
bfe76559 | 6050 | return io_read_prep(req, sqe); |
f67676d1 JA |
6051 | case IORING_OP_WRITEV: |
6052 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6053 | case IORING_OP_WRITE: |
bfe76559 | 6054 | return io_write_prep(req, sqe); |
0969e783 | 6055 | case IORING_OP_POLL_ADD: |
bfe76559 | 6056 | return io_poll_add_prep(req, sqe); |
0969e783 | 6057 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 6058 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 6059 | case IORING_OP_FSYNC: |
bfe76559 | 6060 | return io_prep_fsync(req, sqe); |
8ed8d3c3 | 6061 | case IORING_OP_SYNC_FILE_RANGE: |
bfe76559 | 6062 | return io_prep_sfr(req, sqe); |
03b1230c | 6063 | case IORING_OP_SENDMSG: |
fddaface | 6064 | case IORING_OP_SEND: |
bfe76559 | 6065 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6066 | case IORING_OP_RECVMSG: |
fddaface | 6067 | case IORING_OP_RECV: |
bfe76559 | 6068 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6069 | case IORING_OP_CONNECT: |
bfe76559 | 6070 | return io_connect_prep(req, sqe); |
2d28390a | 6071 | case IORING_OP_TIMEOUT: |
bfe76559 | 6072 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6073 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6074 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6075 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6076 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6077 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6078 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6079 | case IORING_OP_ACCEPT: |
bfe76559 | 6080 | return io_accept_prep(req, sqe); |
d63d1b5e | 6081 | case IORING_OP_FALLOCATE: |
bfe76559 | 6082 | return io_fallocate_prep(req, sqe); |
15b71abe | 6083 | case IORING_OP_OPENAT: |
bfe76559 | 6084 | return io_openat_prep(req, sqe); |
b5dba59e | 6085 | case IORING_OP_CLOSE: |
bfe76559 | 6086 | return io_close_prep(req, sqe); |
05f3fb3c | 6087 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6088 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6089 | case IORING_OP_STATX: |
bfe76559 | 6090 | return io_statx_prep(req, sqe); |
4840e418 | 6091 | case IORING_OP_FADVISE: |
bfe76559 | 6092 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6093 | case IORING_OP_MADVISE: |
bfe76559 | 6094 | return io_madvise_prep(req, sqe); |
cebdb986 | 6095 | case IORING_OP_OPENAT2: |
bfe76559 | 6096 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6097 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6098 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6099 | case IORING_OP_SPLICE: |
bfe76559 | 6100 | return io_splice_prep(req, sqe); |
ddf0322d | 6101 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6102 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6103 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6104 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6105 | case IORING_OP_TEE: |
bfe76559 | 6106 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6107 | case IORING_OP_SHUTDOWN: |
6108 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6109 | case IORING_OP_RENAMEAT: |
6110 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6111 | case IORING_OP_UNLINKAT: |
6112 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6113 | } |
6114 | ||
bfe76559 PB |
6115 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6116 | req->opcode); | |
6117 | return-EINVAL; | |
6118 | } | |
6119 | ||
6120 | static int io_req_defer_prep(struct io_kiocb *req, | |
6121 | const struct io_uring_sqe *sqe) | |
6122 | { | |
bfe76559 PB |
6123 | if (!sqe) |
6124 | return 0; | |
6125 | if (io_alloc_async_data(req)) | |
6126 | return -EAGAIN; | |
bfe76559 | 6127 | return io_req_prep(req, sqe); |
f67676d1 JA |
6128 | } |
6129 | ||
9cf7c104 PB |
6130 | static u32 io_get_sequence(struct io_kiocb *req) |
6131 | { | |
6132 | struct io_kiocb *pos; | |
6133 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6134 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6135 | |
f2f87370 PB |
6136 | io_for_each_link(pos, req) |
6137 | nr_reqs++; | |
9cf7c104 PB |
6138 | |
6139 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6140 | return total_submitted - nr_reqs; | |
6141 | } | |
6142 | ||
3529d8c2 | 6143 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 6144 | { |
a197f664 | 6145 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6146 | struct io_defer_entry *de; |
f67676d1 | 6147 | int ret; |
9cf7c104 | 6148 | u32 seq; |
de0617e4 | 6149 | |
9d858b21 | 6150 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6151 | if (likely(list_empty_careful(&ctx->defer_list) && |
6152 | !(req->flags & REQ_F_IO_DRAIN))) | |
6153 | return 0; | |
6154 | ||
6155 | seq = io_get_sequence(req); | |
6156 | /* Still a chance to pass the sequence check */ | |
6157 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6158 | return 0; |
6159 | ||
e8c2bc1f | 6160 | if (!req->async_data) { |
650b5481 | 6161 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6162 | if (ret) |
650b5481 PB |
6163 | return ret; |
6164 | } | |
cbdcb435 | 6165 | io_prep_async_link(req); |
27dc8338 PB |
6166 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6167 | if (!de) | |
6168 | return -ENOMEM; | |
2d28390a | 6169 | |
de0617e4 | 6170 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6171 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6172 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6173 | kfree(de); |
ae34817b PB |
6174 | io_queue_async_work(req); |
6175 | return -EIOCBQUEUED; | |
de0617e4 JA |
6176 | } |
6177 | ||
915967f6 | 6178 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6179 | de->req = req; |
9cf7c104 | 6180 | de->seq = seq; |
27dc8338 | 6181 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6182 | spin_unlock_irq(&ctx->completion_lock); |
6183 | return -EIOCBQUEUED; | |
6184 | } | |
6185 | ||
f573d384 | 6186 | static void io_req_drop_files(struct io_kiocb *req) |
99bc4c38 | 6187 | { |
f573d384 | 6188 | struct io_ring_ctx *ctx = req->ctx; |
c98de08c | 6189 | struct io_uring_task *tctx = req->task->io_uring; |
f573d384 JA |
6190 | unsigned long flags; |
6191 | ||
02a13674 JA |
6192 | if (req->work.flags & IO_WQ_WORK_FILES) { |
6193 | put_files_struct(req->work.identity->files); | |
6194 | put_nsproxy(req->work.identity->nsproxy); | |
6195 | } | |
f573d384 JA |
6196 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
6197 | list_del(&req->inflight_entry); | |
f573d384 JA |
6198 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
6199 | req->flags &= ~REQ_F_INFLIGHT; | |
dfead8a8 | 6200 | req->work.flags &= ~IO_WQ_WORK_FILES; |
dfea9fce PB |
6201 | if (atomic_read(&tctx->in_idle)) |
6202 | wake_up(&tctx->wait); | |
f573d384 | 6203 | } |
99bc4c38 | 6204 | |
3ca405eb | 6205 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6206 | { |
0e1b6fe3 PB |
6207 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6208 | switch (req->opcode) { | |
6209 | case IORING_OP_READV: | |
6210 | case IORING_OP_READ_FIXED: | |
6211 | case IORING_OP_READ: | |
bcda7baa | 6212 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6213 | break; |
6214 | case IORING_OP_RECVMSG: | |
6215 | case IORING_OP_RECV: | |
bcda7baa | 6216 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6217 | break; |
6218 | } | |
6219 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6220 | } |
6221 | ||
0e1b6fe3 PB |
6222 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6223 | switch (req->opcode) { | |
6224 | case IORING_OP_READV: | |
6225 | case IORING_OP_READ_FIXED: | |
6226 | case IORING_OP_READ: | |
6227 | case IORING_OP_WRITEV: | |
6228 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6229 | case IORING_OP_WRITE: { |
6230 | struct io_async_rw *io = req->async_data; | |
6231 | if (io->free_iovec) | |
6232 | kfree(io->free_iovec); | |
0e1b6fe3 | 6233 | break; |
e8c2bc1f | 6234 | } |
0e1b6fe3 | 6235 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6236 | case IORING_OP_SENDMSG: { |
6237 | struct io_async_msghdr *io = req->async_data; | |
6238 | if (io->iov != io->fast_iov) | |
6239 | kfree(io->iov); | |
0e1b6fe3 | 6240 | break; |
e8c2bc1f | 6241 | } |
0e1b6fe3 PB |
6242 | case IORING_OP_SPLICE: |
6243 | case IORING_OP_TEE: | |
6244 | io_put_file(req, req->splice.file_in, | |
6245 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6246 | break; | |
f3cd4850 JA |
6247 | case IORING_OP_OPENAT: |
6248 | case IORING_OP_OPENAT2: | |
6249 | if (req->open.filename) | |
6250 | putname(req->open.filename); | |
6251 | break; | |
80a261fd JA |
6252 | case IORING_OP_RENAMEAT: |
6253 | putname(req->rename.oldpath); | |
6254 | putname(req->rename.newpath); | |
6255 | break; | |
14a1143b JA |
6256 | case IORING_OP_UNLINKAT: |
6257 | putname(req->unlink.filename); | |
6258 | break; | |
0e1b6fe3 PB |
6259 | } |
6260 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6261 | } |
99bc4c38 PB |
6262 | } |
6263 | ||
c1379e24 PB |
6264 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
6265 | struct io_comp_state *cs) | |
2b188cc1 | 6266 | { |
a197f664 | 6267 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6268 | int ret; |
2b188cc1 | 6269 | |
d625c6ee | 6270 | switch (req->opcode) { |
2b188cc1 | 6271 | case IORING_OP_NOP: |
229a7b63 | 6272 | ret = io_nop(req, cs); |
2b188cc1 JA |
6273 | break; |
6274 | case IORING_OP_READV: | |
edafccee | 6275 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6276 | case IORING_OP_READ: |
a1d7c393 | 6277 | ret = io_read(req, force_nonblock, cs); |
edafccee | 6278 | break; |
3529d8c2 | 6279 | case IORING_OP_WRITEV: |
edafccee | 6280 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6281 | case IORING_OP_WRITE: |
a1d7c393 | 6282 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 6283 | break; |
c992fe29 | 6284 | case IORING_OP_FSYNC: |
014db007 | 6285 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 6286 | break; |
221c5eb2 | 6287 | case IORING_OP_POLL_ADD: |
014db007 | 6288 | ret = io_poll_add(req); |
221c5eb2 JA |
6289 | break; |
6290 | case IORING_OP_POLL_REMOVE: | |
fc4df999 | 6291 | ret = io_poll_remove(req); |
221c5eb2 | 6292 | break; |
5d17b4a4 | 6293 | case IORING_OP_SYNC_FILE_RANGE: |
014db007 | 6294 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 6295 | break; |
0fa03c62 | 6296 | case IORING_OP_SENDMSG: |
062d04d7 PB |
6297 | ret = io_sendmsg(req, force_nonblock, cs); |
6298 | break; | |
fddaface | 6299 | case IORING_OP_SEND: |
062d04d7 | 6300 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 6301 | break; |
aa1fa28f | 6302 | case IORING_OP_RECVMSG: |
062d04d7 PB |
6303 | ret = io_recvmsg(req, force_nonblock, cs); |
6304 | break; | |
fddaface | 6305 | case IORING_OP_RECV: |
062d04d7 | 6306 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 6307 | break; |
5262f567 | 6308 | case IORING_OP_TIMEOUT: |
fc4df999 | 6309 | ret = io_timeout(req); |
5262f567 | 6310 | break; |
11365043 | 6311 | case IORING_OP_TIMEOUT_REMOVE: |
fc4df999 | 6312 | ret = io_timeout_remove(req); |
11365043 | 6313 | break; |
17f2fe35 | 6314 | case IORING_OP_ACCEPT: |
229a7b63 | 6315 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 6316 | break; |
f8e85cf2 | 6317 | case IORING_OP_CONNECT: |
229a7b63 | 6318 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 6319 | break; |
62755e35 | 6320 | case IORING_OP_ASYNC_CANCEL: |
014db007 | 6321 | ret = io_async_cancel(req); |
62755e35 | 6322 | break; |
d63d1b5e | 6323 | case IORING_OP_FALLOCATE: |
014db007 | 6324 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 6325 | break; |
15b71abe | 6326 | case IORING_OP_OPENAT: |
014db007 | 6327 | ret = io_openat(req, force_nonblock); |
15b71abe | 6328 | break; |
b5dba59e | 6329 | case IORING_OP_CLOSE: |
229a7b63 | 6330 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 6331 | break; |
05f3fb3c | 6332 | case IORING_OP_FILES_UPDATE: |
229a7b63 | 6333 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 6334 | break; |
eddc7ef5 | 6335 | case IORING_OP_STATX: |
014db007 | 6336 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 6337 | break; |
4840e418 | 6338 | case IORING_OP_FADVISE: |
014db007 | 6339 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 6340 | break; |
c1ca757b | 6341 | case IORING_OP_MADVISE: |
014db007 | 6342 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 6343 | break; |
cebdb986 | 6344 | case IORING_OP_OPENAT2: |
014db007 | 6345 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 6346 | break; |
3e4827b0 | 6347 | case IORING_OP_EPOLL_CTL: |
229a7b63 | 6348 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 6349 | break; |
7d67af2c | 6350 | case IORING_OP_SPLICE: |
014db007 | 6351 | ret = io_splice(req, force_nonblock); |
7d67af2c | 6352 | break; |
ddf0322d | 6353 | case IORING_OP_PROVIDE_BUFFERS: |
229a7b63 | 6354 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 6355 | break; |
067524e9 | 6356 | case IORING_OP_REMOVE_BUFFERS: |
229a7b63 | 6357 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 6358 | break; |
f2a8d5c7 | 6359 | case IORING_OP_TEE: |
f2a8d5c7 PB |
6360 | ret = io_tee(req, force_nonblock); |
6361 | break; | |
36f4fa68 JA |
6362 | case IORING_OP_SHUTDOWN: |
6363 | ret = io_shutdown(req, force_nonblock); | |
6364 | break; | |
80a261fd JA |
6365 | case IORING_OP_RENAMEAT: |
6366 | ret = io_renameat(req, force_nonblock); | |
6367 | break; | |
14a1143b JA |
6368 | case IORING_OP_UNLINKAT: |
6369 | ret = io_unlinkat(req, force_nonblock); | |
6370 | break; | |
2b188cc1 JA |
6371 | default: |
6372 | ret = -EINVAL; | |
6373 | break; | |
6374 | } | |
6375 | ||
def596e9 JA |
6376 | if (ret) |
6377 | return ret; | |
6378 | ||
b532576e JA |
6379 | /* If the op doesn't have a file, we're not polling for it */ |
6380 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6381 | const bool in_async = io_wq_current_is_worker(); |
6382 | ||
11ba820b JA |
6383 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6384 | if (in_async) | |
6385 | mutex_lock(&ctx->uring_lock); | |
6386 | ||
2e9dbe90 | 6387 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6388 | |
6389 | if (in_async) | |
6390 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6391 | } |
6392 | ||
6393 | return 0; | |
2b188cc1 JA |
6394 | } |
6395 | ||
f4db7182 | 6396 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6397 | { |
6398 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6399 | struct io_kiocb *timeout; |
561fb04a | 6400 | int ret = 0; |
2b188cc1 | 6401 | |
6df1db6b PB |
6402 | timeout = io_prep_linked_timeout(req); |
6403 | if (timeout) | |
6404 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6405 | |
0c9d5ccd JA |
6406 | /* if NO_CANCEL is set, we must still run the work */ |
6407 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
6408 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 6409 | ret = -ECANCELED; |
0c9d5ccd | 6410 | } |
31b51510 | 6411 | |
561fb04a | 6412 | if (!ret) { |
561fb04a | 6413 | do { |
c1379e24 | 6414 | ret = io_issue_sqe(req, false, NULL); |
561fb04a JA |
6415 | /* |
6416 | * We can get EAGAIN for polled IO even though we're | |
6417 | * forcing a sync submission from here, since we can't | |
6418 | * wait for request slots on the block side. | |
6419 | */ | |
6420 | if (ret != -EAGAIN) | |
6421 | break; | |
6422 | cond_resched(); | |
6423 | } while (1); | |
6424 | } | |
31b51510 | 6425 | |
561fb04a | 6426 | if (ret) { |
c07e6719 XW |
6427 | struct io_ring_ctx *lock_ctx = NULL; |
6428 | ||
6429 | if (req->ctx->flags & IORING_SETUP_IOPOLL) | |
6430 | lock_ctx = req->ctx; | |
6431 | ||
dad1b124 | 6432 | /* |
c07e6719 XW |
6433 | * io_iopoll_complete() does not hold completion_lock to |
6434 | * complete polled io, so here for polled io, we can not call | |
6435 | * io_req_complete() directly, otherwise there maybe concurrent | |
6436 | * access to cqring, defer_list, etc, which is not safe. Given | |
6437 | * that io_iopoll_complete() is always called under uring_lock, | |
6438 | * so here for polled io, we also get uring_lock to complete | |
6439 | * it. | |
dad1b124 | 6440 | */ |
c07e6719 XW |
6441 | if (lock_ctx) |
6442 | mutex_lock(&lock_ctx->uring_lock); | |
dad1b124 | 6443 | |
c07e6719 XW |
6444 | req_set_fail_links(req); |
6445 | io_req_complete(req, ret); | |
6446 | ||
6447 | if (lock_ctx) | |
6448 | mutex_unlock(&lock_ctx->uring_lock); | |
edafccee | 6449 | } |
2b188cc1 | 6450 | |
f4db7182 | 6451 | return io_steal_work(req); |
2b188cc1 JA |
6452 | } |
6453 | ||
65e19f54 JA |
6454 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6455 | int index) | |
6456 | { | |
269bbe5f | 6457 | struct fixed_rsrc_table *table; |
65e19f54 | 6458 | |
05f3fb3c | 6459 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6460 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6461 | } |
6462 | ||
8371adf5 PB |
6463 | static struct file *io_file_get(struct io_submit_state *state, |
6464 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6465 | { |
a197f664 | 6466 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6467 | struct file *file; |
09bb8394 | 6468 | |
8da11c19 | 6469 | if (fixed) { |
479f517b | 6470 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6471 | return NULL; |
b7620121 | 6472 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6473 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6474 | io_set_resource_node(req); |
09bb8394 | 6475 | } else { |
c826bd7a | 6476 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6477 | file = __io_file_get(state, fd); |
09bb8394 JA |
6478 | } |
6479 | ||
f609cbb8 PB |
6480 | if (file && file->f_op == &io_uring_fops && |
6481 | !(req->flags & REQ_F_INFLIGHT)) { | |
02a13674 JA |
6482 | io_req_init_async(req); |
6483 | req->flags |= REQ_F_INFLIGHT; | |
6484 | ||
6485 | spin_lock_irq(&ctx->inflight_lock); | |
6486 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
6487 | spin_unlock_irq(&ctx->inflight_lock); | |
6488 | } | |
6489 | ||
8371adf5 | 6490 | return file; |
09bb8394 JA |
6491 | } |
6492 | ||
2665abfd | 6493 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6494 | { |
ad8a48ac JA |
6495 | struct io_timeout_data *data = container_of(timer, |
6496 | struct io_timeout_data, timer); | |
90cd7e42 | 6497 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6498 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6499 | unsigned long flags; |
2665abfd JA |
6500 | |
6501 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6502 | prev = req->timeout.head; |
6503 | req->timeout.head = NULL; | |
2665abfd JA |
6504 | |
6505 | /* | |
6506 | * We don't expect the list to be empty, that will only happen if we | |
6507 | * race with the completion of the linked work. | |
6508 | */ | |
90cd7e42 | 6509 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6510 | io_remove_next_linked(prev); |
90cd7e42 PB |
6511 | else |
6512 | prev = NULL; | |
2665abfd JA |
6513 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6514 | ||
6515 | if (prev) { | |
4e88d6e7 | 6516 | req_set_fail_links(prev); |
014db007 | 6517 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
76a46e06 | 6518 | io_put_req(prev); |
47f46768 | 6519 | } else { |
e1e16097 | 6520 | io_req_complete(req, -ETIME); |
2665abfd | 6521 | } |
2665abfd JA |
6522 | return HRTIMER_NORESTART; |
6523 | } | |
6524 | ||
7271ef3a | 6525 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6526 | { |
76a46e06 | 6527 | /* |
f2f87370 PB |
6528 | * If the back reference is NULL, then our linked request finished |
6529 | * before we got a chance to setup the timer | |
76a46e06 | 6530 | */ |
90cd7e42 | 6531 | if (req->timeout.head) { |
e8c2bc1f | 6532 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6533 | |
ad8a48ac JA |
6534 | data->timer.function = io_link_timeout_fn; |
6535 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6536 | data->mode); | |
2665abfd | 6537 | } |
7271ef3a JA |
6538 | } |
6539 | ||
6540 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6541 | { | |
6542 | struct io_ring_ctx *ctx = req->ctx; | |
6543 | ||
6544 | spin_lock_irq(&ctx->completion_lock); | |
6545 | __io_queue_linked_timeout(req); | |
76a46e06 | 6546 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6547 | |
2665abfd | 6548 | /* drop submission reference */ |
76a46e06 JA |
6549 | io_put_req(req); |
6550 | } | |
2665abfd | 6551 | |
ad8a48ac | 6552 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6553 | { |
f2f87370 | 6554 | struct io_kiocb *nxt = req->link; |
2665abfd | 6555 | |
f2f87370 PB |
6556 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6557 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6558 | return NULL; |
2665abfd | 6559 | |
90cd7e42 | 6560 | nxt->timeout.head = req; |
900fad45 | 6561 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6562 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6563 | return nxt; |
2665abfd JA |
6564 | } |
6565 | ||
c1379e24 | 6566 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 | 6567 | { |
4a0a7a18 | 6568 | struct io_kiocb *linked_timeout; |
193155c8 | 6569 | const struct cred *old_creds = NULL; |
e0c5c576 | 6570 | int ret; |
2b188cc1 | 6571 | |
4a0a7a18 JA |
6572 | again: |
6573 | linked_timeout = io_prep_linked_timeout(req); | |
6574 | ||
2e5aa6cb PB |
6575 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6576 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
98447d65 | 6577 | req->work.identity->creds != current_cred()) { |
193155c8 JA |
6578 | if (old_creds) |
6579 | revert_creds(old_creds); | |
98447d65 | 6580 | if (old_creds == req->work.identity->creds) |
193155c8 JA |
6581 | old_creds = NULL; /* restored original creds */ |
6582 | else | |
98447d65 | 6583 | old_creds = override_creds(req->work.identity->creds); |
193155c8 JA |
6584 | } |
6585 | ||
c1379e24 | 6586 | ret = io_issue_sqe(req, true, cs); |
491381ce JA |
6587 | |
6588 | /* | |
6589 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6590 | * doesn't support non-blocking read/write attempts | |
6591 | */ | |
24c74678 | 6592 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6593 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6594 | /* |
6595 | * Queued up for async execution, worker will release | |
6596 | * submit reference when the iocb is actually submitted. | |
6597 | */ | |
6598 | io_queue_async_work(req); | |
2b188cc1 | 6599 | } |
bbad27b2 | 6600 | |
f063c547 PB |
6601 | if (linked_timeout) |
6602 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 PB |
6603 | } else if (likely(!ret)) { |
6604 | /* drop submission reference */ | |
6605 | req = io_put_req_find_next(req); | |
6606 | if (linked_timeout) | |
6607 | io_queue_linked_timeout(linked_timeout); | |
e65ef56d | 6608 | |
0d63c148 PB |
6609 | if (req) { |
6610 | if (!(req->flags & REQ_F_FORCE_ASYNC)) | |
6611 | goto again; | |
6612 | io_queue_async_work(req); | |
6613 | } | |
6614 | } else { | |
652532ad PB |
6615 | /* un-prep timeout, so it'll be killed as any other linked */ |
6616 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6617 | req_set_fail_links(req); |
e65ef56d | 6618 | io_put_req(req); |
e1e16097 | 6619 | io_req_complete(req, ret); |
9e645e11 | 6620 | } |
652532ad | 6621 | |
193155c8 JA |
6622 | if (old_creds) |
6623 | revert_creds(old_creds); | |
2b188cc1 JA |
6624 | } |
6625 | ||
f13fad7b JA |
6626 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6627 | struct io_comp_state *cs) | |
4fe2c963 JL |
6628 | { |
6629 | int ret; | |
6630 | ||
3529d8c2 | 6631 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6632 | if (ret) { |
6633 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6634 | fail_req: |
4e88d6e7 | 6635 | req_set_fail_links(req); |
e1e16097 JA |
6636 | io_put_req(req); |
6637 | io_req_complete(req, ret); | |
4fe2c963 | 6638 | } |
2550878f | 6639 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6640 | if (!req->async_data) { |
bd2ab18a | 6641 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6642 | if (unlikely(ret)) |
bd2ab18a PB |
6643 | goto fail_req; |
6644 | } | |
ce35a47a JA |
6645 | io_queue_async_work(req); |
6646 | } else { | |
c1379e24 PB |
6647 | if (sqe) { |
6648 | ret = io_req_prep(req, sqe); | |
6649 | if (unlikely(ret)) | |
6650 | goto fail_req; | |
6651 | } | |
6652 | __io_queue_sqe(req, cs); | |
ce35a47a | 6653 | } |
4fe2c963 JL |
6654 | } |
6655 | ||
f13fad7b JA |
6656 | static inline void io_queue_link_head(struct io_kiocb *req, |
6657 | struct io_comp_state *cs) | |
4fe2c963 | 6658 | { |
94ae5e77 | 6659 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6660 | io_put_req(req); |
6661 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6662 | } else |
f13fad7b | 6663 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6664 | } |
6665 | ||
863e0560 PB |
6666 | struct io_submit_link { |
6667 | struct io_kiocb *head; | |
6668 | struct io_kiocb *last; | |
6669 | }; | |
6670 | ||
1d4240cc | 6671 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
863e0560 | 6672 | struct io_submit_link *link, struct io_comp_state *cs) |
9e645e11 | 6673 | { |
a197f664 | 6674 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6675 | int ret; |
9e645e11 | 6676 | |
9e645e11 JA |
6677 | /* |
6678 | * If we already have a head request, queue this one for async | |
6679 | * submittal once the head completes. If we don't have a head but | |
6680 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6681 | * submitted sync once the chain is complete. If none of those | |
6682 | * conditions are true (normal request), then just queue it. | |
6683 | */ | |
863e0560 PB |
6684 | if (link->head) { |
6685 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6686 | |
8cdf2193 PB |
6687 | /* |
6688 | * Taking sequential execution of a link, draining both sides | |
6689 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6690 | * requests in the link. So, it drains the head and the | |
6691 | * next after the link request. The last one is done via | |
6692 | * drain_next flag to persist the effect across calls. | |
6693 | */ | |
ef4ff581 | 6694 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6695 | head->flags |= REQ_F_IO_DRAIN; |
6696 | ctx->drain_next = 1; | |
6697 | } | |
3529d8c2 | 6698 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6699 | if (unlikely(ret)) { |
4e88d6e7 | 6700 | /* fail even hard links since we don't submit */ |
9d76377f | 6701 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6702 | return ret; |
2d28390a | 6703 | } |
9d76377f | 6704 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6705 | link->last->link = req; |
863e0560 | 6706 | link->last = req; |
32fe525b PB |
6707 | |
6708 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6709 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6710 | io_queue_link_head(head, cs); |
863e0560 | 6711 | link->head = NULL; |
32fe525b | 6712 | } |
9e645e11 | 6713 | } else { |
711be031 PB |
6714 | if (unlikely(ctx->drain_next)) { |
6715 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6716 | ctx->drain_next = 0; |
711be031 | 6717 | } |
ef4ff581 | 6718 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
711be031 | 6719 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6720 | if (unlikely(ret)) |
711be031 | 6721 | req->flags |= REQ_F_FAIL_LINK; |
863e0560 PB |
6722 | link->head = req; |
6723 | link->last = req; | |
711be031 | 6724 | } else { |
f13fad7b | 6725 | io_queue_sqe(req, sqe, cs); |
711be031 | 6726 | } |
9e645e11 | 6727 | } |
2e6e1fde | 6728 | |
1d4240cc | 6729 | return 0; |
9e645e11 JA |
6730 | } |
6731 | ||
9a56a232 JA |
6732 | /* |
6733 | * Batched submission is done, ensure local IO is flushed out. | |
6734 | */ | |
6735 | static void io_submit_state_end(struct io_submit_state *state) | |
6736 | { | |
f13fad7b JA |
6737 | if (!list_empty(&state->comp.list)) |
6738 | io_submit_flush_completions(&state->comp); | |
27926b68 JA |
6739 | if (state->plug_started) |
6740 | blk_finish_plug(&state->plug); | |
9f13c35b | 6741 | io_state_file_put(state); |
2579f913 | 6742 | if (state->free_reqs) |
6c8a3134 | 6743 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6744 | } |
6745 | ||
6746 | /* | |
6747 | * Start submission side cache. | |
6748 | */ | |
6749 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6750 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 | 6751 | { |
27926b68 | 6752 | state->plug_started = false; |
013538bd JA |
6753 | state->comp.nr = 0; |
6754 | INIT_LIST_HEAD(&state->comp.list); | |
6755 | state->comp.ctx = ctx; | |
2579f913 | 6756 | state->free_reqs = 0; |
6e1271e6 | 6757 | state->file_refs = 0; |
9a56a232 JA |
6758 | state->ios_left = max_ios; |
6759 | } | |
6760 | ||
2b188cc1 JA |
6761 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6762 | { | |
75b28aff | 6763 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6764 | |
caf582c6 PB |
6765 | /* |
6766 | * Ensure any loads from the SQEs are done at this point, | |
6767 | * since once we write the new head, the application could | |
6768 | * write new data to them. | |
6769 | */ | |
6770 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6771 | } |
6772 | ||
2b188cc1 | 6773 | /* |
3529d8c2 | 6774 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6775 | * that is mapped by userspace. This means that care needs to be taken to |
6776 | * ensure that reads are stable, as we cannot rely on userspace always | |
6777 | * being a good citizen. If members of the sqe are validated and then later | |
6778 | * used, it's important that those reads are done through READ_ONCE() to | |
6779 | * prevent a re-load down the line. | |
6780 | */ | |
709b302f | 6781 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6782 | { |
75b28aff | 6783 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6784 | unsigned head; |
6785 | ||
6786 | /* | |
6787 | * The cached sq head (or cq tail) serves two purposes: | |
6788 | * | |
6789 | * 1) allows us to batch the cost of updating the user visible | |
6790 | * head updates. | |
6791 | * 2) allows the kernel side to track the head on its own, even | |
6792 | * though the application is the one updating it. | |
6793 | */ | |
ee7d46d9 | 6794 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6795 | if (likely(head < ctx->sq_entries)) |
6796 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6797 | |
6798 | /* drop invalid entries */ | |
498ccd9e | 6799 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6800 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6801 | return NULL; |
6802 | } | |
6803 | ||
6804 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6805 | { | |
6806 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6807 | } |
6808 | ||
21b55dbc SG |
6809 | /* |
6810 | * Check SQE restrictions (opcode and flags). | |
6811 | * | |
6812 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6813 | */ | |
6814 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6815 | struct io_kiocb *req, | |
6816 | unsigned int sqe_flags) | |
6817 | { | |
6818 | if (!ctx->restricted) | |
6819 | return true; | |
6820 | ||
6821 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6822 | return false; | |
6823 | ||
6824 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6825 | ctx->restrictions.sqe_flags_required) | |
6826 | return false; | |
6827 | ||
6828 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6829 | ctx->restrictions.sqe_flags_required)) | |
6830 | return false; | |
6831 | ||
6832 | return true; | |
6833 | } | |
6834 | ||
ef4ff581 PB |
6835 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6836 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6837 | IOSQE_BUFFER_SELECT) | |
6838 | ||
6839 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6840 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6841 | struct io_submit_state *state) |
0553b8bd | 6842 | { |
ef4ff581 | 6843 | unsigned int sqe_flags; |
71b547c0 | 6844 | int id, ret; |
ef4ff581 | 6845 | |
0553b8bd PB |
6846 | req->opcode = READ_ONCE(sqe->opcode); |
6847 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6848 | req->async_data = NULL; |
0553b8bd PB |
6849 | req->file = NULL; |
6850 | req->ctx = ctx; | |
6851 | req->flags = 0; | |
f2f87370 | 6852 | req->link = NULL; |
269bbe5f | 6853 | req->fixed_rsrc_refs = NULL; |
0553b8bd PB |
6854 | /* one is dropped after submission, the other at completion */ |
6855 | refcount_set(&req->refs, 2); | |
4dd2824d | 6856 | req->task = current; |
0553b8bd | 6857 | req->result = 0; |
ef4ff581 PB |
6858 | |
6859 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6860 | return -EINVAL; | |
6861 | ||
28cea78a | 6862 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
9d8426a0 | 6863 | return -EFAULT; |
ef4ff581 PB |
6864 | |
6865 | sqe_flags = READ_ONCE(sqe->flags); | |
6866 | /* enforce forwards compatibility on users */ | |
6867 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6868 | return -EINVAL; | |
6869 | ||
21b55dbc SG |
6870 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6871 | return -EACCES; | |
6872 | ||
ef4ff581 PB |
6873 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6874 | !io_op_defs[req->opcode].buffer_select) | |
6875 | return -EOPNOTSUPP; | |
6876 | ||
6877 | id = READ_ONCE(sqe->personality); | |
6878 | if (id) { | |
1e6fa521 JA |
6879 | struct io_identity *iod; |
6880 | ||
1e6fa521 JA |
6881 | iod = idr_find(&ctx->personality_idr, id); |
6882 | if (unlikely(!iod)) | |
ef4ff581 | 6883 | return -EINVAL; |
1e6fa521 | 6884 | refcount_inc(&iod->count); |
ec99ca6c PB |
6885 | |
6886 | __io_req_init_async(req); | |
1e6fa521 JA |
6887 | get_cred(iod->creds); |
6888 | req->work.identity = iod; | |
dfead8a8 | 6889 | req->work.flags |= IO_WQ_WORK_CREDS; |
ef4ff581 PB |
6890 | } |
6891 | ||
6892 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6893 | req->flags |= sqe_flags; |
ef4ff581 | 6894 | |
27926b68 JA |
6895 | /* |
6896 | * Plug now if we have more than 1 IO left after this, and the target | |
6897 | * is potentially a read/write to block based storage. | |
6898 | */ | |
6899 | if (!state->plug_started && state->ios_left > 1 && | |
6900 | io_op_defs[req->opcode].plug) { | |
6901 | blk_start_plug(&state->plug); | |
6902 | state->plug_started = true; | |
6903 | } | |
6904 | ||
bd5bbda7 PB |
6905 | ret = 0; |
6906 | if (io_op_defs[req->opcode].needs_file) { | |
6907 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6908 | ||
6909 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6910 | if (unlikely(!req->file && | |
6911 | !io_op_defs[req->opcode].needs_file_no_error)) | |
6912 | ret = -EBADF; | |
6913 | } | |
63ff8223 | 6914 | |
71b547c0 PB |
6915 | state->ios_left--; |
6916 | return ret; | |
0553b8bd PB |
6917 | } |
6918 | ||
0f212204 | 6919 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6920 | { |
ac8691c4 | 6921 | struct io_submit_state state; |
863e0560 | 6922 | struct io_submit_link link; |
9e645e11 | 6923 | int i, submitted = 0; |
6c271ce2 | 6924 | |
c4a2ed72 | 6925 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6926 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6927 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6928 | return -EBUSY; |
6929 | } | |
6c271ce2 | 6930 | |
ee7d46d9 PB |
6931 | /* make sure SQ entry isn't read before tail */ |
6932 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6933 | |
2b85edfc PB |
6934 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6935 | return -EAGAIN; | |
6c271ce2 | 6936 | |
d8a6df10 | 6937 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6938 | refcount_add(nr, ¤t->usage); |
6c271ce2 | 6939 | |
013538bd | 6940 | io_submit_state_start(&state, ctx, nr); |
863e0560 | 6941 | link.head = NULL; |
b14cca0c | 6942 | |
6c271ce2 | 6943 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6944 | const struct io_uring_sqe *sqe; |
196be95c | 6945 | struct io_kiocb *req; |
1cb1edb2 | 6946 | int err; |
fb5ccc98 | 6947 | |
b1e50e54 PB |
6948 | sqe = io_get_sqe(ctx); |
6949 | if (unlikely(!sqe)) { | |
6950 | io_consume_sqe(ctx); | |
6951 | break; | |
6952 | } | |
ac8691c4 | 6953 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6954 | if (unlikely(!req)) { |
6955 | if (!submitted) | |
6956 | submitted = -EAGAIN; | |
fb5ccc98 | 6957 | break; |
196be95c | 6958 | } |
709b302f | 6959 | io_consume_sqe(ctx); |
d3656344 JA |
6960 | /* will complete beyond this point, count as submitted */ |
6961 | submitted++; | |
6962 | ||
692d8363 | 6963 | err = io_init_req(ctx, req, sqe, &state); |
ef4ff581 | 6964 | if (unlikely(err)) { |
1cb1edb2 | 6965 | fail_req: |
e1e16097 JA |
6966 | io_put_req(req); |
6967 | io_req_complete(req, err); | |
196be95c PB |
6968 | break; |
6969 | } | |
fb5ccc98 | 6970 | |
354420f7 | 6971 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
0cdaf760 | 6972 | true, io_async_submit(ctx)); |
f13fad7b | 6973 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6974 | if (err) |
6975 | goto fail_req; | |
6c271ce2 JA |
6976 | } |
6977 | ||
9466f437 PB |
6978 | if (unlikely(submitted != nr)) { |
6979 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6980 | struct io_uring_task *tctx = current->io_uring; |
6981 | int unused = nr - ref_used; | |
9466f437 | 6982 | |
d8a6df10 JA |
6983 | percpu_ref_put_many(&ctx->refs, unused); |
6984 | percpu_counter_sub(&tctx->inflight, unused); | |
6985 | put_task_struct_many(current, unused); | |
9466f437 | 6986 | } |
863e0560 PB |
6987 | if (link.head) |
6988 | io_queue_link_head(link.head, &state.comp); | |
ac8691c4 | 6989 | io_submit_state_end(&state); |
6c271ce2 | 6990 | |
ae9428ca PB |
6991 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6992 | io_commit_sqring(ctx); | |
6993 | ||
6c271ce2 JA |
6994 | return submitted; |
6995 | } | |
6996 | ||
23b3628e XW |
6997 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6998 | { | |
6999 | /* Tell userspace we may need a wakeup call */ | |
7000 | spin_lock_irq(&ctx->completion_lock); | |
7001 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
7002 | spin_unlock_irq(&ctx->completion_lock); | |
7003 | } | |
7004 | ||
7005 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7006 | { | |
7007 | spin_lock_irq(&ctx->completion_lock); | |
7008 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
7009 | spin_unlock_irq(&ctx->completion_lock); | |
7010 | } | |
7011 | ||
08369246 | 7012 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7013 | { |
c8d1ba58 | 7014 | unsigned int to_submit; |
bdcd3eab | 7015 | int ret = 0; |
6c271ce2 | 7016 | |
c8d1ba58 | 7017 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
7018 | /* if we're handling multiple rings, cap submit size for fairness */ |
7019 | if (cap_entries && to_submit > 8) | |
7020 | to_submit = 8; | |
7021 | ||
906a3c6f | 7022 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 7023 | unsigned nr_events = 0; |
a4c0b3de | 7024 | |
c8d1ba58 | 7025 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7026 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 7027 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7028 | |
d9d05217 PB |
7029 | if (to_submit && !ctx->sqo_dead && |
7030 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 7031 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
7032 | mutex_unlock(&ctx->uring_lock); |
7033 | } | |
6c271ce2 | 7034 | |
90554200 JA |
7035 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7036 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 7037 | |
08369246 XW |
7038 | return ret; |
7039 | } | |
6c271ce2 | 7040 | |
08369246 XW |
7041 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7042 | { | |
7043 | struct io_ring_ctx *ctx; | |
7044 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7045 | |
08369246 XW |
7046 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7047 | if (sq_thread_idle < ctx->sq_thread_idle) | |
7048 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 7049 | } |
c1edbf5f | 7050 | |
08369246 | 7051 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7052 | } |
6c271ce2 | 7053 | |
69fb2131 JA |
7054 | static void io_sqd_init_new(struct io_sq_data *sqd) |
7055 | { | |
7056 | struct io_ring_ctx *ctx; | |
7057 | ||
7058 | while (!list_empty(&sqd->ctx_new_list)) { | |
7059 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
7060 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
7061 | complete(&ctx->sq_thread_comp); | |
7062 | } | |
08369246 XW |
7063 | |
7064 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
7065 | } |
7066 | ||
c8d1ba58 JA |
7067 | static int io_sq_thread(void *data) |
7068 | { | |
91d8f519 | 7069 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
7070 | struct files_struct *old_files = current->files; |
7071 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
7072 | const struct cred *old_cred = NULL; |
7073 | struct io_sq_data *sqd = data; | |
7074 | struct io_ring_ctx *ctx; | |
a0d9205f | 7075 | unsigned long timeout = 0; |
08369246 | 7076 | DEFINE_WAIT(wait); |
6c271ce2 | 7077 | |
28cea78a JA |
7078 | task_lock(current); |
7079 | current->files = NULL; | |
7080 | current->nsproxy = NULL; | |
7081 | task_unlock(current); | |
6c271ce2 | 7082 | |
69fb2131 | 7083 | while (!kthread_should_stop()) { |
08369246 XW |
7084 | int ret; |
7085 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
7086 | |
7087 | /* | |
69fb2131 JA |
7088 | * Any changes to the sqd lists are synchronized through the |
7089 | * kthread parking. This synchronizes the thread vs users, | |
7090 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7091 | */ |
65b2b213 | 7092 | if (kthread_should_park()) { |
69fb2131 | 7093 | kthread_parkme(); |
65b2b213 XW |
7094 | /* |
7095 | * When sq thread is unparked, in case the previous park operation | |
7096 | * comes from io_put_sq_data(), which means that sq thread is going | |
7097 | * to be stopped, so here needs to have a check. | |
7098 | */ | |
7099 | if (kthread_should_stop()) | |
7100 | break; | |
7101 | } | |
7143b5ac | 7102 | |
08369246 | 7103 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7104 | io_sqd_init_new(sqd); |
08369246 XW |
7105 | timeout = jiffies + sqd->sq_thread_idle; |
7106 | } | |
6c271ce2 | 7107 | |
08369246 | 7108 | sqt_spin = false; |
e95eee2d | 7109 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7110 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7111 | if (current->cred != ctx->creds) { | |
7112 | if (old_cred) | |
7113 | revert_creds(old_cred); | |
7114 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7115 | } |
91d8f519 | 7116 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7117 | #ifdef CONFIG_AUDIT |
7118 | current->loginuid = ctx->loginuid; | |
7119 | current->sessionid = ctx->sessionid; | |
7120 | #endif | |
bdcd3eab | 7121 | |
08369246 XW |
7122 | ret = __io_sq_thread(ctx, cap_entries); |
7123 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7124 | sqt_spin = true; | |
6c271ce2 | 7125 | |
28cea78a | 7126 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7127 | } |
6c271ce2 | 7128 | |
08369246 | 7129 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7130 | io_run_task_work(); |
d434ab6d | 7131 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7132 | cond_resched(); |
08369246 XW |
7133 | if (sqt_spin) |
7134 | timeout = jiffies + sqd->sq_thread_idle; | |
7135 | continue; | |
7136 | } | |
7137 | ||
7138 | if (kthread_should_park()) | |
7139 | continue; | |
7140 | ||
7141 | needs_sched = true; | |
7142 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7143 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7144 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7145 | !list_empty_careful(&ctx->iopoll_list)) { | |
7146 | needs_sched = false; | |
7147 | break; | |
7148 | } | |
7149 | if (io_sqring_entries(ctx)) { | |
7150 | needs_sched = false; | |
7151 | break; | |
7152 | } | |
7153 | } | |
7154 | ||
7155 | if (needs_sched) { | |
69fb2131 JA |
7156 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7157 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7158 | |
69fb2131 | 7159 | schedule(); |
69fb2131 JA |
7160 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7161 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7162 | } |
08369246 XW |
7163 | |
7164 | finish_wait(&sqd->wait, &wait); | |
7165 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7166 | } |
7167 | ||
4c6e277c | 7168 | io_run_task_work(); |
d434ab6d | 7169 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7170 | |
91d8f519 DZ |
7171 | if (cur_css) |
7172 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7173 | if (old_cred) |
7174 | revert_creds(old_cred); | |
06058632 | 7175 | |
28cea78a JA |
7176 | task_lock(current); |
7177 | current->files = old_files; | |
7178 | current->nsproxy = old_nsproxy; | |
7179 | task_unlock(current); | |
7180 | ||
2bbcd6d3 | 7181 | kthread_parkme(); |
06058632 | 7182 | |
6c271ce2 JA |
7183 | return 0; |
7184 | } | |
7185 | ||
bda52162 JA |
7186 | struct io_wait_queue { |
7187 | struct wait_queue_entry wq; | |
7188 | struct io_ring_ctx *ctx; | |
7189 | unsigned to_wait; | |
7190 | unsigned nr_timeouts; | |
7191 | }; | |
7192 | ||
6c503150 | 7193 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7194 | { |
7195 | struct io_ring_ctx *ctx = iowq->ctx; | |
7196 | ||
7197 | /* | |
d195a66e | 7198 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7199 | * started waiting. For timeouts, we always want to return to userspace, |
7200 | * regardless of event count. | |
7201 | */ | |
6c503150 | 7202 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7203 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7204 | } | |
7205 | ||
7206 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7207 | int wake_flags, void *key) | |
7208 | { | |
7209 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7210 | wq); | |
7211 | ||
6c503150 PB |
7212 | /* |
7213 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7214 | * the task, and the next invocation will do it. | |
7215 | */ | |
7216 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7217 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7218 | return -1; | |
bda52162 JA |
7219 | } |
7220 | ||
af9c1a44 JA |
7221 | static int io_run_task_work_sig(void) |
7222 | { | |
7223 | if (io_run_task_work()) | |
7224 | return 1; | |
7225 | if (!signal_pending(current)) | |
7226 | return 0; | |
792ee0f6 JA |
7227 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7228 | return -ERESTARTSYS; | |
af9c1a44 JA |
7229 | return -EINTR; |
7230 | } | |
7231 | ||
2b188cc1 JA |
7232 | /* |
7233 | * Wait until events become available, if we don't already have some. The | |
7234 | * application must reap them itself, as they reside on the shared cq ring. | |
7235 | */ | |
7236 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7237 | const sigset_t __user *sig, size_t sigsz, |
7238 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7239 | { |
bda52162 JA |
7240 | struct io_wait_queue iowq = { |
7241 | .wq = { | |
7242 | .private = current, | |
7243 | .func = io_wake_function, | |
7244 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7245 | }, | |
7246 | .ctx = ctx, | |
7247 | .to_wait = min_events, | |
7248 | }; | |
75b28aff | 7249 | struct io_rings *rings = ctx->rings; |
c73ebb68 HX |
7250 | struct timespec64 ts; |
7251 | signed long timeout = 0; | |
e9ffa5c2 | 7252 | int ret = 0; |
2b188cc1 | 7253 | |
b41e9852 | 7254 | do { |
6c503150 PB |
7255 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7256 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7257 | return 0; |
4c6e277c | 7258 | if (!io_run_task_work()) |
b41e9852 | 7259 | break; |
b41e9852 | 7260 | } while (1); |
2b188cc1 JA |
7261 | |
7262 | if (sig) { | |
9e75ad5d AB |
7263 | #ifdef CONFIG_COMPAT |
7264 | if (in_compat_syscall()) | |
7265 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7266 | sigsz); |
9e75ad5d AB |
7267 | else |
7268 | #endif | |
b772434b | 7269 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7270 | |
2b188cc1 JA |
7271 | if (ret) |
7272 | return ret; | |
7273 | } | |
7274 | ||
c73ebb68 HX |
7275 | if (uts) { |
7276 | if (get_timespec64(&ts, uts)) | |
7277 | return -EFAULT; | |
7278 | timeout = timespec64_to_jiffies(&ts); | |
7279 | } | |
7280 | ||
bda52162 | 7281 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7282 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7283 | do { |
6c503150 | 7284 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7285 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7286 | TASK_INTERRUPTIBLE); | |
ce593a6c | 7287 | /* make sure we run task_work before checking for signals */ |
af9c1a44 | 7288 | ret = io_run_task_work_sig(); |
6195ba09 HX |
7289 | if (ret > 0) { |
7290 | finish_wait(&ctx->wait, &iowq.wq); | |
4c6e277c | 7291 | continue; |
6195ba09 | 7292 | } |
af9c1a44 | 7293 | else if (ret < 0) |
bda52162 | 7294 | break; |
6c503150 | 7295 | if (io_should_wake(&iowq)) |
ce593a6c | 7296 | break; |
6195ba09 HX |
7297 | if (test_bit(0, &ctx->cq_check_overflow)) { |
7298 | finish_wait(&ctx->wait, &iowq.wq); | |
6c503150 | 7299 | continue; |
6195ba09 | 7300 | } |
c73ebb68 HX |
7301 | if (uts) { |
7302 | timeout = schedule_timeout(timeout); | |
7303 | if (timeout == 0) { | |
7304 | ret = -ETIME; | |
7305 | break; | |
7306 | } | |
7307 | } else { | |
7308 | schedule(); | |
7309 | } | |
bda52162 JA |
7310 | } while (1); |
7311 | finish_wait(&ctx->wait, &iowq.wq); | |
7312 | ||
b7db41c9 | 7313 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7314 | |
75b28aff | 7315 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7316 | } |
7317 | ||
6b06314c JA |
7318 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7319 | { | |
7320 | #if defined(CONFIG_UNIX) | |
7321 | if (ctx->ring_sock) { | |
7322 | struct sock *sock = ctx->ring_sock->sk; | |
7323 | struct sk_buff *skb; | |
7324 | ||
7325 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7326 | kfree_skb(skb); | |
7327 | } | |
7328 | #else | |
7329 | int i; | |
7330 | ||
65e19f54 JA |
7331 | for (i = 0; i < ctx->nr_user_files; i++) { |
7332 | struct file *file; | |
7333 | ||
7334 | file = io_file_from_index(ctx, i); | |
7335 | if (file) | |
7336 | fput(file); | |
7337 | } | |
6b06314c JA |
7338 | #endif |
7339 | } | |
7340 | ||
00835dce | 7341 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7342 | { |
269bbe5f | 7343 | struct fixed_rsrc_data *data; |
05f3fb3c | 7344 | |
269bbe5f | 7345 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7346 | complete(&data->done); |
7347 | } | |
7348 | ||
2a63b2d9 BM |
7349 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
7350 | { | |
7351 | spin_lock_bh(&ctx->rsrc_ref_lock); | |
7352 | } | |
7353 | ||
7354 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) | |
7355 | { | |
7356 | spin_unlock_bh(&ctx->rsrc_ref_lock); | |
7357 | } | |
7358 | ||
d67d2263 BM |
7359 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7360 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7361 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7362 | { |
2a63b2d9 | 7363 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7364 | rsrc_data->node = ref_node; |
d67d2263 | 7365 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7366 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7367 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7368 | } |
7369 | ||
d7954b2b BM |
7370 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7371 | struct io_ring_ctx *ctx, | |
7372 | struct fixed_rsrc_ref_node *backup_node) | |
6b06314c | 7373 | { |
d7954b2b | 7374 | struct fixed_rsrc_ref_node *ref_node; |
1ffc5422 | 7375 | int ret; |
65e19f54 | 7376 | |
2a63b2d9 | 7377 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7378 | ref_node = data->node; |
2a63b2d9 | 7379 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7380 | if (ref_node) |
7381 | percpu_ref_kill(&ref_node->refs); | |
7382 | ||
7383 | percpu_ref_kill(&data->refs); | |
7384 | ||
7385 | /* wait for all refs nodes to complete */ | |
269bbe5f | 7386 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 PB |
7387 | do { |
7388 | ret = wait_for_completion_interruptible(&data->done); | |
7389 | if (!ret) | |
7390 | break; | |
7391 | ret = io_run_task_work_sig(); | |
7392 | if (ret < 0) { | |
7393 | percpu_ref_resurrect(&data->refs); | |
7394 | reinit_completion(&data->done); | |
d67d2263 | 7395 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
1ffc5422 PB |
7396 | return ret; |
7397 | } | |
7398 | } while (1); | |
05f3fb3c | 7399 | |
d7954b2b BM |
7400 | destroy_fixed_rsrc_ref_node(backup_node); |
7401 | return 0; | |
7402 | } | |
7403 | ||
1ad555c6 BM |
7404 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7405 | { | |
7406 | struct fixed_rsrc_data *data; | |
7407 | ||
7408 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7409 | if (!data) | |
7410 | return NULL; | |
7411 | ||
00835dce | 7412 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7413 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7414 | kfree(data); | |
7415 | return NULL; | |
7416 | } | |
7417 | data->ctx = ctx; | |
7418 | init_completion(&data->done); | |
7419 | return data; | |
7420 | } | |
7421 | ||
7422 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7423 | { | |
7424 | percpu_ref_exit(&data->refs); | |
7425 | kfree(data->table); | |
7426 | kfree(data); | |
7427 | } | |
7428 | ||
d7954b2b BM |
7429 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7430 | { | |
7431 | struct fixed_rsrc_data *data = ctx->file_data; | |
7432 | struct fixed_rsrc_ref_node *backup_node; | |
7433 | unsigned nr_tables, i; | |
7434 | int ret; | |
7435 | ||
7436 | if (!data) | |
7437 | return -ENXIO; | |
7438 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7439 | if (!backup_node) | |
7440 | return -ENOMEM; | |
7441 | init_fixed_file_ref_node(ctx, backup_node); | |
7442 | ||
7443 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); | |
7444 | if (ret) | |
7445 | return ret; | |
7446 | ||
6b06314c | 7447 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7448 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7449 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7450 | kfree(data->table[i].files); |
1ad555c6 | 7451 | free_fixed_rsrc_data(data); |
05f3fb3c | 7452 | ctx->file_data = NULL; |
6b06314c JA |
7453 | ctx->nr_user_files = 0; |
7454 | return 0; | |
7455 | } | |
7456 | ||
534ca6d6 | 7457 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7458 | { |
534ca6d6 | 7459 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7460 | /* |
7461 | * The park is a bit of a work-around, without it we get | |
7462 | * warning spews on shutdown with SQPOLL set and affinity | |
7463 | * set to a single CPU. | |
7464 | */ | |
534ca6d6 JA |
7465 | if (sqd->thread) { |
7466 | kthread_park(sqd->thread); | |
7467 | kthread_stop(sqd->thread); | |
7468 | } | |
7469 | ||
7470 | kfree(sqd); | |
7471 | } | |
7472 | } | |
7473 | ||
aa06165d JA |
7474 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7475 | { | |
7476 | struct io_ring_ctx *ctx_attach; | |
7477 | struct io_sq_data *sqd; | |
7478 | struct fd f; | |
7479 | ||
7480 | f = fdget(p->wq_fd); | |
7481 | if (!f.file) | |
7482 | return ERR_PTR(-ENXIO); | |
7483 | if (f.file->f_op != &io_uring_fops) { | |
7484 | fdput(f); | |
7485 | return ERR_PTR(-EINVAL); | |
7486 | } | |
7487 | ||
7488 | ctx_attach = f.file->private_data; | |
7489 | sqd = ctx_attach->sq_data; | |
7490 | if (!sqd) { | |
7491 | fdput(f); | |
7492 | return ERR_PTR(-EINVAL); | |
7493 | } | |
7494 | ||
7495 | refcount_inc(&sqd->refs); | |
7496 | fdput(f); | |
7497 | return sqd; | |
7498 | } | |
7499 | ||
534ca6d6 JA |
7500 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7501 | { | |
7502 | struct io_sq_data *sqd; | |
7503 | ||
aa06165d JA |
7504 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7505 | return io_attach_sq_data(p); | |
7506 | ||
534ca6d6 JA |
7507 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7508 | if (!sqd) | |
7509 | return ERR_PTR(-ENOMEM); | |
7510 | ||
7511 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7512 | INIT_LIST_HEAD(&sqd->ctx_list); |
7513 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7514 | mutex_init(&sqd->ctx_lock); | |
7515 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7516 | init_waitqueue_head(&sqd->wait); |
7517 | return sqd; | |
7518 | } | |
7519 | ||
69fb2131 JA |
7520 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7521 | __releases(&sqd->lock) | |
7522 | { | |
7523 | if (!sqd->thread) | |
7524 | return; | |
7525 | kthread_unpark(sqd->thread); | |
7526 | mutex_unlock(&sqd->lock); | |
7527 | } | |
7528 | ||
7529 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7530 | __acquires(&sqd->lock) | |
7531 | { | |
7532 | if (!sqd->thread) | |
7533 | return; | |
7534 | mutex_lock(&sqd->lock); | |
7535 | kthread_park(sqd->thread); | |
7536 | } | |
7537 | ||
534ca6d6 JA |
7538 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7539 | { | |
7540 | struct io_sq_data *sqd = ctx->sq_data; | |
7541 | ||
7542 | if (sqd) { | |
7543 | if (sqd->thread) { | |
7544 | /* | |
7545 | * We may arrive here from the error branch in | |
7546 | * io_sq_offload_create() where the kthread is created | |
7547 | * without being waked up, thus wake it up now to make | |
7548 | * sure the wait will complete. | |
7549 | */ | |
7550 | wake_up_process(sqd->thread); | |
7551 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7552 | |
7553 | io_sq_thread_park(sqd); | |
7554 | } | |
7555 | ||
7556 | mutex_lock(&sqd->ctx_lock); | |
7557 | list_del(&ctx->sqd_list); | |
08369246 | 7558 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7559 | mutex_unlock(&sqd->ctx_lock); |
7560 | ||
08369246 | 7561 | if (sqd->thread) |
69fb2131 | 7562 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7563 | |
7564 | io_put_sq_data(sqd); | |
7565 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7566 | } |
7567 | } | |
7568 | ||
6b06314c JA |
7569 | static void io_finish_async(struct io_ring_ctx *ctx) |
7570 | { | |
6c271ce2 JA |
7571 | io_sq_thread_stop(ctx); |
7572 | ||
561fb04a JA |
7573 | if (ctx->io_wq) { |
7574 | io_wq_destroy(ctx->io_wq); | |
7575 | ctx->io_wq = NULL; | |
6b06314c JA |
7576 | } |
7577 | } | |
7578 | ||
7579 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7580 | /* |
7581 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7582 | * the io_uring can be safely unregistered on process exit, even if we have | |
7583 | * loops in the file referencing. | |
7584 | */ | |
7585 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7586 | { | |
7587 | struct sock *sk = ctx->ring_sock->sk; | |
7588 | struct scm_fp_list *fpl; | |
7589 | struct sk_buff *skb; | |
08a45173 | 7590 | int i, nr_files; |
6b06314c | 7591 | |
6b06314c JA |
7592 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7593 | if (!fpl) | |
7594 | return -ENOMEM; | |
7595 | ||
7596 | skb = alloc_skb(0, GFP_KERNEL); | |
7597 | if (!skb) { | |
7598 | kfree(fpl); | |
7599 | return -ENOMEM; | |
7600 | } | |
7601 | ||
7602 | skb->sk = sk; | |
6b06314c | 7603 | |
08a45173 | 7604 | nr_files = 0; |
6b06314c JA |
7605 | fpl->user = get_uid(ctx->user); |
7606 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7607 | struct file *file = io_file_from_index(ctx, i + offset); |
7608 | ||
7609 | if (!file) | |
08a45173 | 7610 | continue; |
65e19f54 | 7611 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7612 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7613 | nr_files++; | |
6b06314c JA |
7614 | } |
7615 | ||
08a45173 JA |
7616 | if (nr_files) { |
7617 | fpl->max = SCM_MAX_FD; | |
7618 | fpl->count = nr_files; | |
7619 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7620 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7621 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7622 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7623 | |
08a45173 JA |
7624 | for (i = 0; i < nr_files; i++) |
7625 | fput(fpl->fp[i]); | |
7626 | } else { | |
7627 | kfree_skb(skb); | |
7628 | kfree(fpl); | |
7629 | } | |
6b06314c JA |
7630 | |
7631 | return 0; | |
7632 | } | |
7633 | ||
7634 | /* | |
7635 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7636 | * causes regular reference counting to break down. We rely on the UNIX | |
7637 | * garbage collection to take care of this problem for us. | |
7638 | */ | |
7639 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7640 | { | |
7641 | unsigned left, total; | |
7642 | int ret = 0; | |
7643 | ||
7644 | total = 0; | |
7645 | left = ctx->nr_user_files; | |
7646 | while (left) { | |
7647 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7648 | |
7649 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7650 | if (ret) | |
7651 | break; | |
7652 | left -= this_files; | |
7653 | total += this_files; | |
7654 | } | |
7655 | ||
7656 | if (!ret) | |
7657 | return 0; | |
7658 | ||
7659 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7660 | struct file *file = io_file_from_index(ctx, total); |
7661 | ||
7662 | if (file) | |
7663 | fput(file); | |
6b06314c JA |
7664 | total++; |
7665 | } | |
7666 | ||
7667 | return ret; | |
7668 | } | |
7669 | #else | |
7670 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7671 | { | |
7672 | return 0; | |
7673 | } | |
7674 | #endif | |
7675 | ||
269bbe5f | 7676 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7677 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7678 | { |
7679 | int i; | |
7680 | ||
7681 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7682 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7683 | unsigned this_files; |
7684 | ||
7685 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7686 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7687 | GFP_KERNEL); | |
7688 | if (!table->files) | |
7689 | break; | |
7690 | nr_files -= this_files; | |
7691 | } | |
7692 | ||
7693 | if (i == nr_tables) | |
7694 | return 0; | |
7695 | ||
7696 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7697 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7698 | kfree(table->files); |
7699 | } | |
7700 | return 1; | |
7701 | } | |
7702 | ||
50238531 | 7703 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7704 | { |
50238531 | 7705 | struct file *file = prsrc->file; |
05f3fb3c JA |
7706 | #if defined(CONFIG_UNIX) |
7707 | struct sock *sock = ctx->ring_sock->sk; | |
7708 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7709 | struct sk_buff *skb; | |
7710 | int i; | |
7711 | ||
7712 | __skb_queue_head_init(&list); | |
7713 | ||
7714 | /* | |
7715 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7716 | * remove this entry and rearrange the file array. | |
7717 | */ | |
7718 | skb = skb_dequeue(head); | |
7719 | while (skb) { | |
7720 | struct scm_fp_list *fp; | |
7721 | ||
7722 | fp = UNIXCB(skb).fp; | |
7723 | for (i = 0; i < fp->count; i++) { | |
7724 | int left; | |
7725 | ||
7726 | if (fp->fp[i] != file) | |
7727 | continue; | |
7728 | ||
7729 | unix_notinflight(fp->user, fp->fp[i]); | |
7730 | left = fp->count - 1 - i; | |
7731 | if (left) { | |
7732 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7733 | left * sizeof(struct file *)); | |
7734 | } | |
7735 | fp->count--; | |
7736 | if (!fp->count) { | |
7737 | kfree_skb(skb); | |
7738 | skb = NULL; | |
7739 | } else { | |
7740 | __skb_queue_tail(&list, skb); | |
7741 | } | |
7742 | fput(file); | |
7743 | file = NULL; | |
7744 | break; | |
7745 | } | |
7746 | ||
7747 | if (!file) | |
7748 | break; | |
7749 | ||
7750 | __skb_queue_tail(&list, skb); | |
7751 | ||
7752 | skb = skb_dequeue(head); | |
7753 | } | |
7754 | ||
7755 | if (skb_peek(&list)) { | |
7756 | spin_lock_irq(&head->lock); | |
7757 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7758 | __skb_queue_tail(head, skb); | |
7759 | spin_unlock_irq(&head->lock); | |
7760 | } | |
7761 | #else | |
7762 | fput(file); | |
7763 | #endif | |
7764 | } | |
7765 | ||
269bbe5f | 7766 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7767 | { |
269bbe5f BM |
7768 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7769 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7770 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7771 | |
269bbe5f BM |
7772 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7773 | list_del(&prsrc->list); | |
50238531 | 7774 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7775 | kfree(prsrc); |
65e19f54 | 7776 | } |
05589553 | 7777 | |
05589553 XW |
7778 | percpu_ref_exit(&ref_node->refs); |
7779 | kfree(ref_node); | |
269bbe5f | 7780 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7781 | } |
65e19f54 | 7782 | |
269bbe5f | 7783 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7784 | { |
7785 | struct io_ring_ctx *ctx; | |
7786 | struct llist_node *node; | |
7787 | ||
269bbe5f BM |
7788 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7789 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7790 | |
7791 | while (node) { | |
269bbe5f | 7792 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7793 | struct llist_node *next = node->next; |
7794 | ||
269bbe5f BM |
7795 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7796 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7797 | node = next; |
7798 | } | |
7799 | } | |
7800 | ||
00835dce | 7801 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7802 | { |
269bbe5f BM |
7803 | struct fixed_rsrc_ref_node *ref_node; |
7804 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7805 | struct io_ring_ctx *ctx; |
e297822b | 7806 | bool first_add = false; |
4a38aed2 | 7807 | int delay = HZ; |
65e19f54 | 7808 | |
269bbe5f BM |
7809 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7810 | data = ref_node->rsrc_data; | |
e297822b PB |
7811 | ctx = data->ctx; |
7812 | ||
2a63b2d9 | 7813 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7814 | ref_node->done = true; |
7815 | ||
d67d2263 BM |
7816 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7817 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7818 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7819 | /* recycle ref nodes in order */ |
7820 | if (!ref_node->done) | |
7821 | break; | |
7822 | list_del(&ref_node->node); | |
269bbe5f | 7823 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7824 | } |
2a63b2d9 | 7825 | io_rsrc_ref_unlock(ctx); |
05589553 | 7826 | |
e297822b | 7827 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7828 | delay = 0; |
05589553 | 7829 | |
4a38aed2 | 7830 | if (!delay) |
269bbe5f | 7831 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7832 | else if (first_add) |
269bbe5f | 7833 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7834 | } |
65e19f54 | 7835 | |
6802535d | 7836 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7837 | struct io_ring_ctx *ctx) |
05f3fb3c | 7838 | { |
269bbe5f | 7839 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7840 | |
05589553 XW |
7841 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7842 | if (!ref_node) | |
3e2224c5 | 7843 | return NULL; |
05f3fb3c | 7844 | |
00835dce | 7845 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7846 | 0, GFP_KERNEL)) { |
7847 | kfree(ref_node); | |
3e2224c5 | 7848 | return NULL; |
05589553 XW |
7849 | } |
7850 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7851 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
6802535d BM |
7852 | ref_node->done = false; |
7853 | return ref_node; | |
7854 | } | |
7855 | ||
bc9744cd PB |
7856 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7857 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7858 | { |
269bbe5f | 7859 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7860 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7861 | } |
7862 | ||
269bbe5f | 7863 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7864 | { |
7865 | percpu_ref_exit(&ref_node->refs); | |
7866 | kfree(ref_node); | |
65e19f54 JA |
7867 | } |
7868 | ||
6b06314c JA |
7869 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7870 | unsigned nr_args) | |
7871 | { | |
7872 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7873 | unsigned nr_tables, i; |
05f3fb3c | 7874 | struct file *file; |
600cf3f8 | 7875 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7876 | struct fixed_rsrc_ref_node *ref_node; |
7877 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7878 | |
05f3fb3c | 7879 | if (ctx->file_data) |
6b06314c JA |
7880 | return -EBUSY; |
7881 | if (!nr_args) | |
7882 | return -EINVAL; | |
7883 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7884 | return -EMFILE; | |
7885 | ||
1ad555c6 | 7886 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7887 | if (!file_data) |
05f3fb3c | 7888 | return -ENOMEM; |
05f3fb3c | 7889 | |
65e19f54 | 7890 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7891 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7892 | GFP_KERNEL); |
600cf3f8 PB |
7893 | if (!file_data->table) |
7894 | goto out_free; | |
05f3fb3c | 7895 | |
600cf3f8 | 7896 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7897 | goto out_free; |
55cbc256 | 7898 | ctx->file_data = file_data; |
65e19f54 | 7899 | |
08a45173 | 7900 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
269bbe5f | 7901 | struct fixed_rsrc_table *table; |
65e19f54 JA |
7902 | unsigned index; |
7903 | ||
600cf3f8 PB |
7904 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7905 | ret = -EFAULT; | |
7906 | goto out_fput; | |
7907 | } | |
08a45173 | 7908 | /* allow sparse sets */ |
600cf3f8 | 7909 | if (fd == -1) |
08a45173 | 7910 | continue; |
6b06314c | 7911 | |
05f3fb3c | 7912 | file = fget(fd); |
6b06314c | 7913 | ret = -EBADF; |
05f3fb3c | 7914 | if (!file) |
600cf3f8 | 7915 | goto out_fput; |
05f3fb3c | 7916 | |
6b06314c JA |
7917 | /* |
7918 | * Don't allow io_uring instances to be registered. If UNIX | |
7919 | * isn't enabled, then this causes a reference cycle and this | |
7920 | * instance can never get freed. If UNIX is enabled we'll | |
7921 | * handle it just fine, but there's still no point in allowing | |
7922 | * a ring fd as it doesn't support regular read/write anyway. | |
7923 | */ | |
05f3fb3c JA |
7924 | if (file->f_op == &io_uring_fops) { |
7925 | fput(file); | |
600cf3f8 | 7926 | goto out_fput; |
6b06314c | 7927 | } |
600cf3f8 PB |
7928 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
7929 | index = i & IORING_FILE_TABLE_MASK; | |
05f3fb3c | 7930 | table->files[index] = file; |
6b06314c JA |
7931 | } |
7932 | ||
6b06314c | 7933 | ret = io_sqe_files_scm(ctx); |
05589553 | 7934 | if (ret) { |
6b06314c | 7935 | io_sqe_files_unregister(ctx); |
05589553 XW |
7936 | return ret; |
7937 | } | |
6b06314c | 7938 | |
bc9744cd | 7939 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7940 | if (!ref_node) { |
05589553 | 7941 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7942 | return -ENOMEM; |
05589553 | 7943 | } |
bc9744cd | 7944 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7945 | |
d67d2263 | 7946 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7947 | return ret; |
600cf3f8 PB |
7948 | out_fput: |
7949 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7950 | file = io_file_from_index(ctx, i); | |
7951 | if (file) | |
7952 | fput(file); | |
7953 | } | |
7954 | for (i = 0; i < nr_tables; i++) | |
7955 | kfree(file_data->table[i].files); | |
7956 | ctx->nr_user_files = 0; | |
600cf3f8 | 7957 | out_free: |
1ad555c6 | 7958 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7959 | ctx->file_data = NULL; |
6b06314c JA |
7960 | return ret; |
7961 | } | |
7962 | ||
c3a31e60 JA |
7963 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7964 | int index) | |
7965 | { | |
7966 | #if defined(CONFIG_UNIX) | |
7967 | struct sock *sock = ctx->ring_sock->sk; | |
7968 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7969 | struct sk_buff *skb; | |
7970 | ||
7971 | /* | |
7972 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7973 | * file set. If there's no room, fall back to allocating a new skb | |
7974 | * and filling it in. | |
7975 | */ | |
7976 | spin_lock_irq(&head->lock); | |
7977 | skb = skb_peek(head); | |
7978 | if (skb) { | |
7979 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7980 | ||
7981 | if (fpl->count < SCM_MAX_FD) { | |
7982 | __skb_unlink(skb, head); | |
7983 | spin_unlock_irq(&head->lock); | |
7984 | fpl->fp[fpl->count] = get_file(file); | |
7985 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7986 | fpl->count++; | |
7987 | spin_lock_irq(&head->lock); | |
7988 | __skb_queue_head(head, skb); | |
7989 | } else { | |
7990 | skb = NULL; | |
7991 | } | |
7992 | } | |
7993 | spin_unlock_irq(&head->lock); | |
7994 | ||
7995 | if (skb) { | |
7996 | fput(file); | |
7997 | return 0; | |
7998 | } | |
7999 | ||
8000 | return __io_sqe_files_scm(ctx, 1, index); | |
8001 | #else | |
8002 | return 0; | |
8003 | #endif | |
8004 | } | |
8005 | ||
50238531 | 8006 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 8007 | { |
269bbe5f BM |
8008 | struct io_rsrc_put *prsrc; |
8009 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 8010 | |
269bbe5f BM |
8011 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
8012 | if (!prsrc) | |
a5318d3c | 8013 | return -ENOMEM; |
05f3fb3c | 8014 | |
50238531 | 8015 | prsrc->rsrc = rsrc; |
269bbe5f | 8016 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 8017 | |
a5318d3c | 8018 | return 0; |
05f3fb3c JA |
8019 | } |
8020 | ||
269bbe5f BM |
8021 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
8022 | struct file *file) | |
8023 | { | |
50238531 | 8024 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
8025 | } |
8026 | ||
05f3fb3c | 8027 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 8028 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
8029 | unsigned nr_args) |
8030 | { | |
269bbe5f BM |
8031 | struct fixed_rsrc_data *data = ctx->file_data; |
8032 | struct fixed_rsrc_ref_node *ref_node; | |
05f3fb3c | 8033 | struct file *file; |
c3a31e60 JA |
8034 | __s32 __user *fds; |
8035 | int fd, i, err; | |
8036 | __u32 done; | |
05589553 | 8037 | bool needs_switch = false; |
c3a31e60 | 8038 | |
05f3fb3c | 8039 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
8040 | return -EOVERFLOW; |
8041 | if (done > ctx->nr_user_files) | |
8042 | return -EINVAL; | |
8043 | ||
bc9744cd | 8044 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
8045 | if (!ref_node) |
8046 | return -ENOMEM; | |
bc9744cd | 8047 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 8048 | |
c3a31e60 | 8049 | done = 0; |
269bbe5f | 8050 | fds = u64_to_user_ptr(up->data); |
c3a31e60 | 8051 | while (nr_args) { |
269bbe5f | 8052 | struct fixed_rsrc_table *table; |
65e19f54 JA |
8053 | unsigned index; |
8054 | ||
c3a31e60 JA |
8055 | err = 0; |
8056 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
8057 | err = -EFAULT; | |
8058 | break; | |
8059 | } | |
05f3fb3c JA |
8060 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
8061 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
8062 | index = i & IORING_FILE_TABLE_MASK; |
8063 | if (table->files[index]) { | |
98dfd502 | 8064 | file = table->files[index]; |
a5318d3c HD |
8065 | err = io_queue_file_removal(data, file); |
8066 | if (err) | |
8067 | break; | |
65e19f54 | 8068 | table->files[index] = NULL; |
05589553 | 8069 | needs_switch = true; |
c3a31e60 JA |
8070 | } |
8071 | if (fd != -1) { | |
c3a31e60 JA |
8072 | file = fget(fd); |
8073 | if (!file) { | |
8074 | err = -EBADF; | |
8075 | break; | |
8076 | } | |
8077 | /* | |
8078 | * Don't allow io_uring instances to be registered. If | |
8079 | * UNIX isn't enabled, then this causes a reference | |
8080 | * cycle and this instance can never get freed. If UNIX | |
8081 | * is enabled we'll handle it just fine, but there's | |
8082 | * still no point in allowing a ring fd as it doesn't | |
8083 | * support regular read/write anyway. | |
8084 | */ | |
8085 | if (file->f_op == &io_uring_fops) { | |
8086 | fput(file); | |
8087 | err = -EBADF; | |
8088 | break; | |
8089 | } | |
65e19f54 | 8090 | table->files[index] = file; |
c3a31e60 | 8091 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8092 | if (err) { |
95d1c8e5 | 8093 | table->files[index] = NULL; |
f3bd9dae | 8094 | fput(file); |
c3a31e60 | 8095 | break; |
f3bd9dae | 8096 | } |
c3a31e60 JA |
8097 | } |
8098 | nr_args--; | |
8099 | done++; | |
05f3fb3c JA |
8100 | up->offset++; |
8101 | } | |
8102 | ||
05589553 | 8103 | if (needs_switch) { |
b2e96852 | 8104 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 8105 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 8106 | } else |
269bbe5f | 8107 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
8108 | |
8109 | return done ? done : err; | |
8110 | } | |
05589553 | 8111 | |
05f3fb3c JA |
8112 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
8113 | unsigned nr_args) | |
8114 | { | |
269bbe5f | 8115 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
8116 | |
8117 | if (!ctx->file_data) | |
8118 | return -ENXIO; | |
8119 | if (!nr_args) | |
8120 | return -EINVAL; | |
8121 | if (copy_from_user(&up, arg, sizeof(up))) | |
8122 | return -EFAULT; | |
8123 | if (up.resv) | |
8124 | return -EINVAL; | |
8125 | ||
8126 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8127 | } | |
c3a31e60 | 8128 | |
e9fd9396 | 8129 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
8130 | { |
8131 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8132 | ||
e9fd9396 | 8133 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
8134 | io_put_req(req); |
8135 | } | |
8136 | ||
24369c2e PB |
8137 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
8138 | struct io_uring_params *p) | |
8139 | { | |
8140 | struct io_wq_data data; | |
8141 | struct fd f; | |
8142 | struct io_ring_ctx *ctx_attach; | |
8143 | unsigned int concurrency; | |
8144 | int ret = 0; | |
8145 | ||
8146 | data.user = ctx->user; | |
e9fd9396 | 8147 | data.free_work = io_free_work; |
f5fa38c5 | 8148 | data.do_work = io_wq_submit_work; |
24369c2e PB |
8149 | |
8150 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
8151 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
8152 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
8153 | ||
8154 | ctx->io_wq = io_wq_create(concurrency, &data); | |
8155 | if (IS_ERR(ctx->io_wq)) { | |
8156 | ret = PTR_ERR(ctx->io_wq); | |
8157 | ctx->io_wq = NULL; | |
8158 | } | |
8159 | return ret; | |
8160 | } | |
8161 | ||
8162 | f = fdget(p->wq_fd); | |
8163 | if (!f.file) | |
8164 | return -EBADF; | |
8165 | ||
8166 | if (f.file->f_op != &io_uring_fops) { | |
8167 | ret = -EINVAL; | |
8168 | goto out_fput; | |
8169 | } | |
8170 | ||
8171 | ctx_attach = f.file->private_data; | |
8172 | /* @io_wq is protected by holding the fd */ | |
8173 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8174 | ret = -EINVAL; | |
8175 | goto out_fput; | |
8176 | } | |
8177 | ||
8178 | ctx->io_wq = ctx_attach->io_wq; | |
8179 | out_fput: | |
8180 | fdput(f); | |
8181 | return ret; | |
8182 | } | |
8183 | ||
0f212204 JA |
8184 | static int io_uring_alloc_task_context(struct task_struct *task) |
8185 | { | |
8186 | struct io_uring_task *tctx; | |
d8a6df10 | 8187 | int ret; |
0f212204 JA |
8188 | |
8189 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8190 | if (unlikely(!tctx)) | |
8191 | return -ENOMEM; | |
8192 | ||
d8a6df10 JA |
8193 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8194 | if (unlikely(ret)) { | |
8195 | kfree(tctx); | |
8196 | return ret; | |
8197 | } | |
8198 | ||
0f212204 JA |
8199 | xa_init(&tctx->xa); |
8200 | init_waitqueue_head(&tctx->wait); | |
8201 | tctx->last = NULL; | |
fdaf083c JA |
8202 | atomic_set(&tctx->in_idle, 0); |
8203 | tctx->sqpoll = false; | |
500a373d JA |
8204 | io_init_identity(&tctx->__identity); |
8205 | tctx->identity = &tctx->__identity; | |
0f212204 JA |
8206 | task->io_uring = tctx; |
8207 | return 0; | |
8208 | } | |
8209 | ||
8210 | void __io_uring_free(struct task_struct *tsk) | |
8211 | { | |
8212 | struct io_uring_task *tctx = tsk->io_uring; | |
8213 | ||
8214 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8215 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8216 | if (tctx->identity != &tctx->__identity) | |
8217 | kfree(tctx->identity); | |
d8a6df10 | 8218 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8219 | kfree(tctx); |
8220 | tsk->io_uring = NULL; | |
8221 | } | |
8222 | ||
7e84e1c7 SG |
8223 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8224 | struct io_uring_params *p) | |
2b188cc1 JA |
8225 | { |
8226 | int ret; | |
8227 | ||
6c271ce2 | 8228 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8229 | struct io_sq_data *sqd; |
8230 | ||
3ec482d1 | 8231 | ret = -EPERM; |
ce59fc69 | 8232 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8233 | goto err; |
8234 | ||
534ca6d6 JA |
8235 | sqd = io_get_sq_data(p); |
8236 | if (IS_ERR(sqd)) { | |
8237 | ret = PTR_ERR(sqd); | |
8238 | goto err; | |
8239 | } | |
69fb2131 | 8240 | |
534ca6d6 | 8241 | ctx->sq_data = sqd; |
69fb2131 JA |
8242 | io_sq_thread_park(sqd); |
8243 | mutex_lock(&sqd->ctx_lock); | |
8244 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8245 | mutex_unlock(&sqd->ctx_lock); | |
8246 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8247 | |
917257da JA |
8248 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8249 | if (!ctx->sq_thread_idle) | |
8250 | ctx->sq_thread_idle = HZ; | |
8251 | ||
aa06165d JA |
8252 | if (sqd->thread) |
8253 | goto done; | |
8254 | ||
6c271ce2 | 8255 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8256 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8257 | |
917257da | 8258 | ret = -EINVAL; |
44a9bd18 JA |
8259 | if (cpu >= nr_cpu_ids) |
8260 | goto err; | |
7889f44d | 8261 | if (!cpu_online(cpu)) |
917257da JA |
8262 | goto err; |
8263 | ||
69fb2131 | 8264 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8265 | cpu, "io_uring-sq"); |
6c271ce2 | 8266 | } else { |
69fb2131 | 8267 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8268 | "io_uring-sq"); |
8269 | } | |
534ca6d6 JA |
8270 | if (IS_ERR(sqd->thread)) { |
8271 | ret = PTR_ERR(sqd->thread); | |
8272 | sqd->thread = NULL; | |
6c271ce2 JA |
8273 | goto err; |
8274 | } | |
534ca6d6 | 8275 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8276 | if (ret) |
8277 | goto err; | |
6c271ce2 JA |
8278 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8279 | /* Can't have SQ_AFF without SQPOLL */ | |
8280 | ret = -EINVAL; | |
8281 | goto err; | |
8282 | } | |
8283 | ||
aa06165d | 8284 | done: |
24369c2e PB |
8285 | ret = io_init_wq_offload(ctx, p); |
8286 | if (ret) | |
2b188cc1 | 8287 | goto err; |
2b188cc1 JA |
8288 | |
8289 | return 0; | |
8290 | err: | |
54a91f3b | 8291 | io_finish_async(ctx); |
2b188cc1 JA |
8292 | return ret; |
8293 | } | |
8294 | ||
7e84e1c7 SG |
8295 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8296 | { | |
534ca6d6 JA |
8297 | struct io_sq_data *sqd = ctx->sq_data; |
8298 | ||
8299 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8300 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8301 | } |
8302 | ||
a087e2b5 BM |
8303 | static inline void __io_unaccount_mem(struct user_struct *user, |
8304 | unsigned long nr_pages) | |
2b188cc1 JA |
8305 | { |
8306 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8307 | } | |
8308 | ||
a087e2b5 BM |
8309 | static inline int __io_account_mem(struct user_struct *user, |
8310 | unsigned long nr_pages) | |
2b188cc1 JA |
8311 | { |
8312 | unsigned long page_limit, cur_pages, new_pages; | |
8313 | ||
8314 | /* Don't allow more pages than we can safely lock */ | |
8315 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8316 | ||
8317 | do { | |
8318 | cur_pages = atomic_long_read(&user->locked_vm); | |
8319 | new_pages = cur_pages + nr_pages; | |
8320 | if (new_pages > page_limit) | |
8321 | return -ENOMEM; | |
8322 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8323 | new_pages) != cur_pages); | |
8324 | ||
8325 | return 0; | |
8326 | } | |
8327 | ||
2e0464d4 BM |
8328 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8329 | enum io_mem_account acct) | |
a087e2b5 | 8330 | { |
aad5d8da | 8331 | if (ctx->limit_mem) |
a087e2b5 | 8332 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8333 | |
2aede0e4 | 8334 | if (ctx->mm_account) { |
4bc4a912 JA |
8335 | if (acct == ACCT_LOCKED) { |
8336 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8337 | ctx->mm_account->locked_vm -= nr_pages; |
4bc4a912 JA |
8338 | mmap_write_unlock(ctx->mm_account); |
8339 | }else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8340 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8341 | } |
2e0464d4 | 8342 | } |
a087e2b5 BM |
8343 | } |
8344 | ||
2e0464d4 BM |
8345 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8346 | enum io_mem_account acct) | |
a087e2b5 | 8347 | { |
30975825 BM |
8348 | int ret; |
8349 | ||
8350 | if (ctx->limit_mem) { | |
8351 | ret = __io_account_mem(ctx->user, nr_pages); | |
8352 | if (ret) | |
8353 | return ret; | |
8354 | } | |
8355 | ||
2aede0e4 | 8356 | if (ctx->mm_account) { |
4bc4a912 JA |
8357 | if (acct == ACCT_LOCKED) { |
8358 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8359 | ctx->mm_account->locked_vm += nr_pages; |
4bc4a912 JA |
8360 | mmap_write_unlock(ctx->mm_account); |
8361 | } else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8362 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8363 | } |
2e0464d4 | 8364 | } |
a087e2b5 BM |
8365 | |
8366 | return 0; | |
8367 | } | |
8368 | ||
2b188cc1 JA |
8369 | static void io_mem_free(void *ptr) |
8370 | { | |
52e04ef4 MR |
8371 | struct page *page; |
8372 | ||
8373 | if (!ptr) | |
8374 | return; | |
2b188cc1 | 8375 | |
52e04ef4 | 8376 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8377 | if (put_page_testzero(page)) |
8378 | free_compound_page(page); | |
8379 | } | |
8380 | ||
8381 | static void *io_mem_alloc(size_t size) | |
8382 | { | |
8383 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
8384 | __GFP_NORETRY; | |
8385 | ||
8386 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8387 | } | |
8388 | ||
75b28aff HV |
8389 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8390 | size_t *sq_offset) | |
8391 | { | |
8392 | struct io_rings *rings; | |
8393 | size_t off, sq_array_size; | |
8394 | ||
8395 | off = struct_size(rings, cqes, cq_entries); | |
8396 | if (off == SIZE_MAX) | |
8397 | return SIZE_MAX; | |
8398 | ||
8399 | #ifdef CONFIG_SMP | |
8400 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8401 | if (off == 0) | |
8402 | return SIZE_MAX; | |
8403 | #endif | |
8404 | ||
b36200f5 DV |
8405 | if (sq_offset) |
8406 | *sq_offset = off; | |
8407 | ||
75b28aff HV |
8408 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8409 | if (sq_array_size == SIZE_MAX) | |
8410 | return SIZE_MAX; | |
8411 | ||
8412 | if (check_add_overflow(off, sq_array_size, &off)) | |
8413 | return SIZE_MAX; | |
8414 | ||
75b28aff HV |
8415 | return off; |
8416 | } | |
8417 | ||
2b188cc1 JA |
8418 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8419 | { | |
75b28aff | 8420 | size_t pages; |
2b188cc1 | 8421 | |
75b28aff HV |
8422 | pages = (size_t)1 << get_order( |
8423 | rings_size(sq_entries, cq_entries, NULL)); | |
8424 | pages += (size_t)1 << get_order( | |
8425 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8426 | |
75b28aff | 8427 | return pages; |
2b188cc1 JA |
8428 | } |
8429 | ||
0a96bbe4 | 8430 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8431 | { |
8432 | int i, j; | |
8433 | ||
8434 | if (!ctx->user_bufs) | |
8435 | return -ENXIO; | |
8436 | ||
8437 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8438 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8439 | ||
8440 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8441 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8442 | |
de293938 JA |
8443 | if (imu->acct_pages) |
8444 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8445 | kvfree(imu->bvec); |
edafccee JA |
8446 | imu->nr_bvecs = 0; |
8447 | } | |
8448 | ||
8449 | kfree(ctx->user_bufs); | |
8450 | ctx->user_bufs = NULL; | |
8451 | ctx->nr_user_bufs = 0; | |
8452 | return 0; | |
8453 | } | |
8454 | ||
8455 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8456 | void __user *arg, unsigned index) | |
8457 | { | |
8458 | struct iovec __user *src; | |
8459 | ||
8460 | #ifdef CONFIG_COMPAT | |
8461 | if (ctx->compat) { | |
8462 | struct compat_iovec __user *ciovs; | |
8463 | struct compat_iovec ciov; | |
8464 | ||
8465 | ciovs = (struct compat_iovec __user *) arg; | |
8466 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8467 | return -EFAULT; | |
8468 | ||
d55e5f5b | 8469 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8470 | dst->iov_len = ciov.iov_len; |
8471 | return 0; | |
8472 | } | |
8473 | #endif | |
8474 | src = (struct iovec __user *) arg; | |
8475 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8476 | return -EFAULT; | |
8477 | return 0; | |
8478 | } | |
8479 | ||
de293938 JA |
8480 | /* |
8481 | * Not super efficient, but this is just a registration time. And we do cache | |
8482 | * the last compound head, so generally we'll only do a full search if we don't | |
8483 | * match that one. | |
8484 | * | |
8485 | * We check if the given compound head page has already been accounted, to | |
8486 | * avoid double accounting it. This allows us to account the full size of the | |
8487 | * page, not just the constituent pages of a huge page. | |
8488 | */ | |
8489 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8490 | int nr_pages, struct page *hpage) | |
8491 | { | |
8492 | int i, j; | |
8493 | ||
8494 | /* check current page array */ | |
8495 | for (i = 0; i < nr_pages; i++) { | |
8496 | if (!PageCompound(pages[i])) | |
8497 | continue; | |
8498 | if (compound_head(pages[i]) == hpage) | |
8499 | return true; | |
8500 | } | |
8501 | ||
8502 | /* check previously registered pages */ | |
8503 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8504 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8505 | ||
8506 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8507 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8508 | continue; | |
8509 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8510 | return true; | |
8511 | } | |
8512 | } | |
8513 | ||
8514 | return false; | |
8515 | } | |
8516 | ||
8517 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8518 | int nr_pages, struct io_mapped_ubuf *imu, | |
8519 | struct page **last_hpage) | |
8520 | { | |
8521 | int i, ret; | |
8522 | ||
8523 | for (i = 0; i < nr_pages; i++) { | |
8524 | if (!PageCompound(pages[i])) { | |
8525 | imu->acct_pages++; | |
8526 | } else { | |
8527 | struct page *hpage; | |
8528 | ||
8529 | hpage = compound_head(pages[i]); | |
8530 | if (hpage == *last_hpage) | |
8531 | continue; | |
8532 | *last_hpage = hpage; | |
8533 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8534 | continue; | |
8535 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8536 | } | |
8537 | } | |
8538 | ||
8539 | if (!imu->acct_pages) | |
8540 | return 0; | |
8541 | ||
8542 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8543 | if (ret) | |
8544 | imu->acct_pages = 0; | |
8545 | return ret; | |
8546 | } | |
8547 | ||
0a96bbe4 BM |
8548 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8549 | struct io_mapped_ubuf *imu, | |
8550 | struct page **last_hpage) | |
edafccee JA |
8551 | { |
8552 | struct vm_area_struct **vmas = NULL; | |
8553 | struct page **pages = NULL; | |
0a96bbe4 BM |
8554 | unsigned long off, start, end, ubuf; |
8555 | size_t size; | |
8556 | int ret, pret, nr_pages, i; | |
8557 | ||
8558 | ubuf = (unsigned long) iov->iov_base; | |
8559 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8560 | start = ubuf >> PAGE_SHIFT; | |
8561 | nr_pages = end - start; | |
8562 | ||
8563 | ret = -ENOMEM; | |
8564 | ||
8565 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8566 | if (!pages) | |
8567 | goto done; | |
8568 | ||
8569 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8570 | GFP_KERNEL); | |
8571 | if (!vmas) | |
8572 | goto done; | |
8573 | ||
8574 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), | |
8575 | GFP_KERNEL); | |
8576 | if (!imu->bvec) | |
8577 | goto done; | |
8578 | ||
8579 | ret = 0; | |
8580 | mmap_read_lock(current->mm); | |
8581 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8582 | pages, vmas); | |
8583 | if (pret == nr_pages) { | |
8584 | /* don't support file backed memory */ | |
8585 | for (i = 0; i < nr_pages; i++) { | |
8586 | struct vm_area_struct *vma = vmas[i]; | |
8587 | ||
8588 | if (vma->vm_file && | |
8589 | !is_file_hugepages(vma->vm_file)) { | |
8590 | ret = -EOPNOTSUPP; | |
8591 | break; | |
8592 | } | |
8593 | } | |
8594 | } else { | |
8595 | ret = pret < 0 ? pret : -EFAULT; | |
8596 | } | |
8597 | mmap_read_unlock(current->mm); | |
8598 | if (ret) { | |
8599 | /* | |
8600 | * if we did partial map, or found file backed vmas, | |
8601 | * release any pages we did get | |
8602 | */ | |
8603 | if (pret > 0) | |
8604 | unpin_user_pages(pages, pret); | |
8605 | kvfree(imu->bvec); | |
8606 | goto done; | |
8607 | } | |
8608 | ||
8609 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8610 | if (ret) { | |
8611 | unpin_user_pages(pages, pret); | |
8612 | kvfree(imu->bvec); | |
8613 | goto done; | |
8614 | } | |
8615 | ||
8616 | off = ubuf & ~PAGE_MASK; | |
8617 | size = iov->iov_len; | |
8618 | for (i = 0; i < nr_pages; i++) { | |
8619 | size_t vec_len; | |
8620 | ||
8621 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8622 | imu->bvec[i].bv_page = pages[i]; | |
8623 | imu->bvec[i].bv_len = vec_len; | |
8624 | imu->bvec[i].bv_offset = off; | |
8625 | off = 0; | |
8626 | size -= vec_len; | |
8627 | } | |
8628 | /* store original address for later verification */ | |
8629 | imu->ubuf = ubuf; | |
8630 | imu->len = iov->iov_len; | |
8631 | imu->nr_bvecs = nr_pages; | |
8632 | ret = 0; | |
8633 | done: | |
8634 | kvfree(pages); | |
8635 | kvfree(vmas); | |
8636 | return ret; | |
8637 | } | |
8638 | ||
2b358604 | 8639 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8640 | { |
edafccee JA |
8641 | if (ctx->user_bufs) |
8642 | return -EBUSY; | |
8643 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8644 | return -EINVAL; | |
8645 | ||
8646 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8647 | GFP_KERNEL); | |
8648 | if (!ctx->user_bufs) | |
8649 | return -ENOMEM; | |
8650 | ||
2b358604 BM |
8651 | return 0; |
8652 | } | |
8653 | ||
8654 | static int io_buffer_validate(struct iovec *iov) | |
8655 | { | |
8656 | /* | |
8657 | * Don't impose further limits on the size and buffer | |
8658 | * constraints here, we'll -EINVAL later when IO is | |
8659 | * submitted if they are wrong. | |
8660 | */ | |
8661 | if (!iov->iov_base || !iov->iov_len) | |
8662 | return -EFAULT; | |
8663 | ||
8664 | /* arbitrary limit, but we need something */ | |
8665 | if (iov->iov_len > SZ_1G) | |
8666 | return -EFAULT; | |
8667 | ||
8668 | return 0; | |
8669 | } | |
8670 | ||
8671 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
8672 | unsigned int nr_args) | |
8673 | { | |
8674 | int i, ret; | |
8675 | struct iovec iov; | |
8676 | struct page *last_hpage = NULL; | |
8677 | ||
8678 | ret = io_buffers_map_alloc(ctx, nr_args); | |
8679 | if (ret) | |
8680 | return ret; | |
8681 | ||
edafccee JA |
8682 | for (i = 0; i < nr_args; i++) { |
8683 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee JA |
8684 | |
8685 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8686 | if (ret) | |
0a96bbe4 | 8687 | break; |
edafccee | 8688 | |
2b358604 BM |
8689 | ret = io_buffer_validate(&iov); |
8690 | if (ret) | |
0a96bbe4 | 8691 | break; |
edafccee | 8692 | |
0a96bbe4 BM |
8693 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8694 | if (ret) | |
8695 | break; | |
edafccee JA |
8696 | |
8697 | ctx->nr_user_bufs++; | |
8698 | } | |
0a96bbe4 BM |
8699 | |
8700 | if (ret) | |
8701 | io_sqe_buffers_unregister(ctx); | |
8702 | ||
edafccee JA |
8703 | return ret; |
8704 | } | |
8705 | ||
9b402849 JA |
8706 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8707 | { | |
8708 | __s32 __user *fds = arg; | |
8709 | int fd; | |
8710 | ||
8711 | if (ctx->cq_ev_fd) | |
8712 | return -EBUSY; | |
8713 | ||
8714 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8715 | return -EFAULT; | |
8716 | ||
8717 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8718 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8719 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8720 | ctx->cq_ev_fd = NULL; | |
8721 | return ret; | |
8722 | } | |
8723 | ||
8724 | return 0; | |
8725 | } | |
8726 | ||
8727 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8728 | { | |
8729 | if (ctx->cq_ev_fd) { | |
8730 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8731 | ctx->cq_ev_fd = NULL; | |
8732 | return 0; | |
8733 | } | |
8734 | ||
8735 | return -ENXIO; | |
8736 | } | |
8737 | ||
5a2e745d JA |
8738 | static int __io_destroy_buffers(int id, void *p, void *data) |
8739 | { | |
8740 | struct io_ring_ctx *ctx = data; | |
8741 | struct io_buffer *buf = p; | |
8742 | ||
067524e9 | 8743 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8744 | return 0; |
8745 | } | |
8746 | ||
8747 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8748 | { | |
8749 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8750 | idr_destroy(&ctx->io_buffer_idr); | |
8751 | } | |
8752 | ||
2b188cc1 JA |
8753 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8754 | { | |
6b06314c | 8755 | io_finish_async(ctx); |
0a96bbe4 | 8756 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8757 | |
8758 | if (ctx->sqo_task) { | |
8759 | put_task_struct(ctx->sqo_task); | |
8760 | ctx->sqo_task = NULL; | |
8761 | mmdrop(ctx->mm_account); | |
8762 | ctx->mm_account = NULL; | |
30975825 | 8763 | } |
def596e9 | 8764 | |
91d8f519 DZ |
8765 | #ifdef CONFIG_BLK_CGROUP |
8766 | if (ctx->sqo_blkcg_css) | |
8767 | css_put(ctx->sqo_blkcg_css); | |
8768 | #endif | |
8769 | ||
6b06314c | 8770 | io_sqe_files_unregister(ctx); |
9b402849 | 8771 | io_eventfd_unregister(ctx); |
5a2e745d | 8772 | io_destroy_buffers(ctx); |
41726c9a | 8773 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8774 | |
2b188cc1 | 8775 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8776 | if (ctx->ring_sock) { |
8777 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8778 | sock_release(ctx->ring_sock); |
355e8d26 | 8779 | } |
2b188cc1 JA |
8780 | #endif |
8781 | ||
75b28aff | 8782 | io_mem_free(ctx->rings); |
2b188cc1 | 8783 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8784 | |
8785 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8786 | free_uid(ctx->user); |
181e448d | 8787 | put_cred(ctx->creds); |
78076bb6 | 8788 | kfree(ctx->cancel_hash); |
0ddf92e8 | 8789 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
8790 | kfree(ctx); |
8791 | } | |
8792 | ||
8793 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8794 | { | |
8795 | struct io_ring_ctx *ctx = file->private_data; | |
8796 | __poll_t mask = 0; | |
8797 | ||
8798 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8799 | /* |
8800 | * synchronizes with barrier from wq_has_sleeper call in | |
8801 | * io_commit_cqring | |
8802 | */ | |
2b188cc1 | 8803 | smp_rmb(); |
90554200 | 8804 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8805 | mask |= EPOLLOUT | EPOLLWRNORM; |
6c503150 PB |
8806 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
8807 | if (io_cqring_events(ctx)) | |
2b188cc1 JA |
8808 | mask |= EPOLLIN | EPOLLRDNORM; |
8809 | ||
8810 | return mask; | |
8811 | } | |
8812 | ||
8813 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8814 | { | |
8815 | struct io_ring_ctx *ctx = file->private_data; | |
8816 | ||
8817 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8818 | } | |
8819 | ||
071698e1 JA |
8820 | static int io_remove_personalities(int id, void *p, void *data) |
8821 | { | |
8822 | struct io_ring_ctx *ctx = data; | |
1e6fa521 | 8823 | struct io_identity *iod; |
071698e1 | 8824 | |
1e6fa521 JA |
8825 | iod = idr_remove(&ctx->personality_idr, id); |
8826 | if (iod) { | |
8827 | put_cred(iod->creds); | |
8828 | if (refcount_dec_and_test(&iod->count)) | |
8829 | kfree(iod); | |
8830 | } | |
071698e1 JA |
8831 | return 0; |
8832 | } | |
8833 | ||
85faa7b8 JA |
8834 | static void io_ring_exit_work(struct work_struct *work) |
8835 | { | |
b2edc0a7 PB |
8836 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8837 | exit_work); | |
85faa7b8 | 8838 | |
56952e91 JA |
8839 | /* |
8840 | * If we're doing polled IO and end up having requests being | |
8841 | * submitted async (out-of-line), then completions can come in while | |
8842 | * we're waiting for refs to drop. We need to reap these manually, | |
8843 | * as nobody else will be looking for them. | |
8844 | */ | |
b2edc0a7 | 8845 | do { |
90df0853 | 8846 | __io_uring_cancel_task_requests(ctx, NULL); |
b2edc0a7 | 8847 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8848 | io_ring_ctx_free(ctx); |
8849 | } | |
8850 | ||
00c18640 JA |
8851 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8852 | { | |
8853 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8854 | ||
8855 | return req->ctx == data; | |
8856 | } | |
8857 | ||
2b188cc1 JA |
8858 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8859 | { | |
8860 | mutex_lock(&ctx->uring_lock); | |
8861 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8862 | |
8863 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8864 | ctx->sqo_dead = 1; | |
8865 | ||
cda286f0 PB |
8866 | /* if force is set, the ring is going away. always drop after that */ |
8867 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8868 | if (ctx->rings) |
6c503150 | 8869 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
2b188cc1 JA |
8870 | mutex_unlock(&ctx->uring_lock); |
8871 | ||
6b81928d PB |
8872 | io_kill_timeouts(ctx, NULL, NULL); |
8873 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8874 | |
8875 | if (ctx->io_wq) | |
00c18640 | 8876 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8877 | |
15dff286 | 8878 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8879 | io_iopoll_try_reap_events(ctx); |
071698e1 | 8880 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
309fc03a JA |
8881 | |
8882 | /* | |
8883 | * Do this upfront, so we won't have a grace period where the ring | |
8884 | * is closed but resources aren't reaped yet. This can cause | |
8885 | * spurious failure in setting up a new ring. | |
8886 | */ | |
760618f7 JA |
8887 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8888 | ACCT_LOCKED); | |
309fc03a | 8889 | |
85faa7b8 | 8890 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8891 | /* |
8892 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8893 | * if we're exiting a ton of rings at the same time. It just adds | |
8894 | * noise and overhead, there's no discernable change in runtime | |
8895 | * over using system_wq. | |
8896 | */ | |
8897 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8898 | } |
8899 | ||
8900 | static int io_uring_release(struct inode *inode, struct file *file) | |
8901 | { | |
8902 | struct io_ring_ctx *ctx = file->private_data; | |
8903 | ||
8904 | file->private_data = NULL; | |
8905 | io_ring_ctx_wait_and_kill(ctx); | |
8906 | return 0; | |
8907 | } | |
8908 | ||
f6edbabb PB |
8909 | struct io_task_cancel { |
8910 | struct task_struct *task; | |
8911 | struct files_struct *files; | |
8912 | }; | |
f254ac04 | 8913 | |
f6edbabb | 8914 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8915 | { |
9a472ef7 | 8916 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8917 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8918 | bool ret; |
8919 | ||
f6edbabb | 8920 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8921 | unsigned long flags; |
8922 | struct io_ring_ctx *ctx = req->ctx; | |
8923 | ||
8924 | /* protect against races with linked timeouts */ | |
8925 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8926 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8927 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8928 | } else { | |
f6edbabb | 8929 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8930 | } |
8931 | return ret; | |
b711d4ea JA |
8932 | } |
8933 | ||
b7ddce3c | 8934 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8935 | struct task_struct *task, |
b7ddce3c PB |
8936 | struct files_struct *files) |
8937 | { | |
8938 | struct io_defer_entry *de = NULL; | |
8939 | LIST_HEAD(list); | |
8940 | ||
8941 | spin_lock_irq(&ctx->completion_lock); | |
8942 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8943 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8944 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8945 | break; | |
8946 | } | |
8947 | } | |
8948 | spin_unlock_irq(&ctx->completion_lock); | |
8949 | ||
8950 | while (!list_empty(&list)) { | |
8951 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8952 | list_del_init(&de->list); | |
8953 | req_set_fail_links(de->req); | |
8954 | io_put_req(de->req); | |
8955 | io_req_complete(de->req, -ECANCELED); | |
8956 | kfree(de); | |
8957 | } | |
8958 | } | |
8959 | ||
ca70f00b PB |
8960 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8961 | struct task_struct *task, | |
8962 | struct files_struct *files) | |
8963 | { | |
8964 | struct io_kiocb *req; | |
8965 | int cnt = 0; | |
8966 | ||
8967 | spin_lock_irq(&ctx->inflight_lock); | |
8968 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8969 | cnt += io_match_task(req, task, files); | |
8970 | spin_unlock_irq(&ctx->inflight_lock); | |
8971 | return cnt; | |
8972 | } | |
8973 | ||
b52fda00 | 8974 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8975 | struct task_struct *task, |
fcb323cc JA |
8976 | struct files_struct *files) |
8977 | { | |
fcb323cc | 8978 | while (!list_empty_careful(&ctx->inflight_list)) { |
bee749b1 | 8979 | struct io_task_cancel cancel = { .task = task, .files = files }; |
d8f1b971 | 8980 | DEFINE_WAIT(wait); |
ca70f00b | 8981 | int inflight; |
fcb323cc | 8982 | |
ca70f00b PB |
8983 | inflight = io_uring_count_inflight(ctx, task, files); |
8984 | if (!inflight) | |
fcb323cc | 8985 | break; |
f6edbabb PB |
8986 | |
8987 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); | |
8988 | io_poll_remove_all(ctx, task, files); | |
8989 | io_kill_timeouts(ctx, task, files); | |
9d5c8190 | 8990 | io_cqring_overflow_flush(ctx, true, task, files); |
6200b0ae JA |
8991 | /* cancellations _may_ trigger task work */ |
8992 | io_run_task_work(); | |
ca70f00b PB |
8993 | |
8994 | prepare_to_wait(&task->io_uring->wait, &wait, | |
8995 | TASK_UNINTERRUPTIBLE); | |
8996 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8997 | schedule(); | |
c98de08c | 8998 | finish_wait(&task->io_uring->wait, &wait); |
fcb323cc | 8999 | } |
44e728b8 PB |
9000 | } |
9001 | ||
b52fda00 PB |
9002 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
9003 | struct task_struct *task) | |
0f212204 | 9004 | { |
b52fda00 | 9005 | while (1) { |
f6edbabb | 9006 | struct io_task_cancel cancel = { .task = task, .files = NULL, }; |
0f212204 | 9007 | enum io_wq_cancel cret; |
b52fda00 | 9008 | bool ret = false; |
0f212204 | 9009 | |
90df0853 PB |
9010 | if (ctx->io_wq) { |
9011 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, | |
9012 | &cancel, true); | |
9013 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9014 | } | |
0f212204 JA |
9015 | |
9016 | /* SQPOLL thread does its own polling */ | |
9017 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
9018 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
9019 | io_iopoll_try_reap_events(ctx); | |
9020 | ret = true; | |
9021 | } | |
9022 | } | |
9023 | ||
6b81928d PB |
9024 | ret |= io_poll_remove_all(ctx, task, NULL); |
9025 | ret |= io_kill_timeouts(ctx, task, NULL); | |
55583d72 | 9026 | ret |= io_run_task_work(); |
b52fda00 PB |
9027 | if (!ret) |
9028 | break; | |
b52fda00 | 9029 | cond_resched(); |
0f212204 | 9030 | } |
0f212204 JA |
9031 | } |
9032 | ||
d9d05217 PB |
9033 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
9034 | { | |
d9d05217 PB |
9035 | mutex_lock(&ctx->uring_lock); |
9036 | ctx->sqo_dead = 1; | |
9037 | mutex_unlock(&ctx->uring_lock); | |
9038 | ||
9039 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
9040 | if (ctx->rings) |
9041 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
9042 | } |
9043 | ||
0f212204 JA |
9044 | /* |
9045 | * We need to iteratively cancel requests, in case a request has dependent | |
9046 | * hard links. These persist even for failure of cancelations, hence keep | |
9047 | * looping until none are found. | |
9048 | */ | |
9049 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
9050 | struct files_struct *files) | |
9051 | { | |
9052 | struct task_struct *task = current; | |
9053 | ||
fdaf083c | 9054 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 9055 | io_disable_sqo_submit(ctx); |
534ca6d6 | 9056 | task = ctx->sq_data->thread; |
fdaf083c JA |
9057 | atomic_inc(&task->io_uring->in_idle); |
9058 | io_sq_thread_park(ctx->sq_data); | |
9059 | } | |
0f212204 | 9060 | |
df9923f9 | 9061 | io_cancel_defer_files(ctx, task, files); |
0f212204 JA |
9062 | io_cqring_overflow_flush(ctx, true, task, files); |
9063 | ||
3a7efd1a | 9064 | io_uring_cancel_files(ctx, task, files); |
b52fda00 PB |
9065 | if (!files) |
9066 | __io_uring_cancel_task_requests(ctx, task); | |
fdaf083c JA |
9067 | |
9068 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
9069 | atomic_dec(&task->io_uring->in_idle); | |
9070 | /* | |
9071 | * If the files that are going away are the ones in the thread | |
9072 | * identity, clear them out. | |
9073 | */ | |
9074 | if (task->io_uring->identity->files == files) | |
9075 | task->io_uring->identity->files = NULL; | |
9076 | io_sq_thread_unpark(ctx->sq_data); | |
9077 | } | |
0f212204 JA |
9078 | } |
9079 | ||
9080 | /* | |
9081 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9082 | */ | |
fdaf083c | 9083 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 9084 | { |
236434c3 | 9085 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 9086 | int ret; |
236434c3 MWO |
9087 | |
9088 | if (unlikely(!tctx)) { | |
0f212204 JA |
9089 | ret = io_uring_alloc_task_context(current); |
9090 | if (unlikely(ret)) | |
9091 | return ret; | |
236434c3 | 9092 | tctx = current->io_uring; |
0f212204 | 9093 | } |
236434c3 MWO |
9094 | if (tctx->last != file) { |
9095 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 9096 | |
236434c3 | 9097 | if (!old) { |
0f212204 | 9098 | get_file(file); |
a528b04e PB |
9099 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
9100 | file, GFP_KERNEL)); | |
9101 | if (ret) { | |
9102 | fput(file); | |
9103 | return ret; | |
9104 | } | |
0f212204 | 9105 | } |
236434c3 | 9106 | tctx->last = file; |
0f212204 JA |
9107 | } |
9108 | ||
fdaf083c JA |
9109 | /* |
9110 | * This is race safe in that the task itself is doing this, hence it | |
9111 | * cannot be going through the exit/cancel paths at the same time. | |
9112 | * This cannot be modified while exit/cancel is running. | |
9113 | */ | |
9114 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9115 | tctx->sqpoll = true; | |
9116 | ||
0f212204 JA |
9117 | return 0; |
9118 | } | |
9119 | ||
9120 | /* | |
9121 | * Remove this io_uring_file -> task mapping. | |
9122 | */ | |
9123 | static void io_uring_del_task_file(struct file *file) | |
9124 | { | |
9125 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9126 | |
9127 | if (tctx->last == file) | |
9128 | tctx->last = NULL; | |
5e2ed8c4 | 9129 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9130 | if (file) |
9131 | fput(file); | |
9132 | } | |
9133 | ||
de7f1d9e PB |
9134 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9135 | { | |
9136 | struct file *file; | |
9137 | unsigned long index; | |
9138 | ||
9139 | xa_for_each(&tctx->xa, index, file) | |
9140 | io_uring_del_task_file(file); | |
9141 | } | |
9142 | ||
0f212204 JA |
9143 | void __io_uring_files_cancel(struct files_struct *files) |
9144 | { | |
9145 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9146 | struct file *file; |
9147 | unsigned long index; | |
0f212204 JA |
9148 | |
9149 | /* make sure overflow events are dropped */ | |
fdaf083c | 9150 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9151 | xa_for_each(&tctx->xa, index, file) |
9152 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9153 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9154 | |
9155 | if (files) | |
9156 | io_uring_remove_task_files(tctx); | |
fdaf083c JA |
9157 | } |
9158 | ||
9159 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9160 | { | |
9161 | unsigned long index; | |
9162 | struct file *file; | |
9163 | s64 inflight; | |
9164 | ||
9165 | inflight = percpu_counter_sum(&tctx->inflight); | |
9166 | if (!tctx->sqpoll) | |
9167 | return inflight; | |
9168 | ||
9169 | /* | |
9170 | * If we have SQPOLL rings, then we need to iterate and find them, and | |
9171 | * add the pending count for those. | |
9172 | */ | |
9173 | xa_for_each(&tctx->xa, index, file) { | |
9174 | struct io_ring_ctx *ctx = file->private_data; | |
9175 | ||
9176 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
9177 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; | |
9178 | ||
9179 | inflight += percpu_counter_sum(&__tctx->inflight); | |
9180 | } | |
9181 | } | |
9182 | ||
9183 | return inflight; | |
0f212204 JA |
9184 | } |
9185 | ||
0f212204 JA |
9186 | /* |
9187 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9188 | * requests. | |
9189 | */ | |
9190 | void __io_uring_task_cancel(void) | |
9191 | { | |
9192 | struct io_uring_task *tctx = current->io_uring; | |
9193 | DEFINE_WAIT(wait); | |
d8a6df10 | 9194 | s64 inflight; |
0f212204 JA |
9195 | |
9196 | /* make sure overflow events are dropped */ | |
fdaf083c | 9197 | atomic_inc(&tctx->in_idle); |
0f212204 | 9198 | |
0b5cd6c3 PB |
9199 | /* trigger io_disable_sqo_submit() */ |
9200 | if (tctx->sqpoll) | |
9201 | __io_uring_files_cancel(NULL); | |
9202 | ||
d8a6df10 | 9203 | do { |
0f212204 | 9204 | /* read completions before cancelations */ |
fdaf083c | 9205 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9206 | if (!inflight) |
9207 | break; | |
0f212204 JA |
9208 | __io_uring_files_cancel(NULL); |
9209 | ||
9210 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9211 | ||
9212 | /* | |
a1bb3cd5 PB |
9213 | * If we've seen completions, retry without waiting. This |
9214 | * avoids a race where a completion comes in before we did | |
9215 | * prepare_to_wait(). | |
0f212204 | 9216 | */ |
a1bb3cd5 PB |
9217 | if (inflight == tctx_inflight(tctx)) |
9218 | schedule(); | |
f57555ed | 9219 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9220 | } while (1); |
0f212204 | 9221 | |
fdaf083c | 9222 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9223 | |
9224 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9225 | } |
9226 | ||
fcb323cc JA |
9227 | static int io_uring_flush(struct file *file, void *data) |
9228 | { | |
6b5733eb | 9229 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9230 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9231 | |
84965ff8 JA |
9232 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
9233 | io_uring_cancel_task_requests(ctx, NULL); | |
9234 | ||
6b5733eb | 9235 | if (!tctx) |
4f793dc4 PB |
9236 | return 0; |
9237 | ||
6b5733eb PB |
9238 | /* we should have cancelled and erased it before PF_EXITING */ |
9239 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9240 | xa_load(&tctx->xa, (unsigned long)file)); | |
9241 | ||
4f793dc4 PB |
9242 | /* |
9243 | * fput() is pending, will be 2 if the only other ref is our potential | |
9244 | * task file note. If the task is exiting, drop regardless of count. | |
9245 | */ | |
6b5733eb PB |
9246 | if (atomic_long_read(&file->f_count) != 2) |
9247 | return 0; | |
4f793dc4 | 9248 | |
d9d05217 PB |
9249 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9250 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9251 | WARN_ON_ONCE(ctx->sqo_task != current && |
9252 | xa_load(&tctx->xa, (unsigned long)file)); | |
9253 | /* sqo_dead check is for when this happens after cancellation */ | |
9254 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9255 | !xa_load(&tctx->xa, (unsigned long)file)); |
9256 | ||
9257 | io_disable_sqo_submit(ctx); | |
9258 | } | |
9259 | ||
9260 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9261 | io_uring_del_task_file(file); | |
fcb323cc JA |
9262 | return 0; |
9263 | } | |
9264 | ||
6c5c240e RP |
9265 | static void *io_uring_validate_mmap_request(struct file *file, |
9266 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9267 | { |
2b188cc1 | 9268 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9269 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9270 | struct page *page; |
9271 | void *ptr; | |
9272 | ||
9273 | switch (offset) { | |
9274 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9275 | case IORING_OFF_CQ_RING: |
9276 | ptr = ctx->rings; | |
2b188cc1 JA |
9277 | break; |
9278 | case IORING_OFF_SQES: | |
9279 | ptr = ctx->sq_sqes; | |
9280 | break; | |
2b188cc1 | 9281 | default: |
6c5c240e | 9282 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9283 | } |
9284 | ||
9285 | page = virt_to_head_page(ptr); | |
a50b854e | 9286 | if (sz > page_size(page)) |
6c5c240e RP |
9287 | return ERR_PTR(-EINVAL); |
9288 | ||
9289 | return ptr; | |
9290 | } | |
9291 | ||
9292 | #ifdef CONFIG_MMU | |
9293 | ||
9294 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9295 | { | |
9296 | size_t sz = vma->vm_end - vma->vm_start; | |
9297 | unsigned long pfn; | |
9298 | void *ptr; | |
9299 | ||
9300 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9301 | if (IS_ERR(ptr)) | |
9302 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9303 | |
9304 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9305 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9306 | } | |
9307 | ||
6c5c240e RP |
9308 | #else /* !CONFIG_MMU */ |
9309 | ||
9310 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9311 | { | |
9312 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9313 | } | |
9314 | ||
9315 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9316 | { | |
9317 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9318 | } | |
9319 | ||
9320 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9321 | unsigned long addr, unsigned long len, | |
9322 | unsigned long pgoff, unsigned long flags) | |
9323 | { | |
9324 | void *ptr; | |
9325 | ||
9326 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9327 | if (IS_ERR(ptr)) | |
9328 | return PTR_ERR(ptr); | |
9329 | ||
9330 | return (unsigned long) ptr; | |
9331 | } | |
9332 | ||
9333 | #endif /* !CONFIG_MMU */ | |
9334 | ||
d9d05217 | 9335 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9336 | { |
d9d05217 | 9337 | int ret = 0; |
90554200 JA |
9338 | DEFINE_WAIT(wait); |
9339 | ||
9340 | do { | |
9341 | if (!io_sqring_full(ctx)) | |
9342 | break; | |
9343 | ||
9344 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9345 | ||
d9d05217 PB |
9346 | if (unlikely(ctx->sqo_dead)) { |
9347 | ret = -EOWNERDEAD; | |
9348 | goto out; | |
9349 | } | |
9350 | ||
90554200 JA |
9351 | if (!io_sqring_full(ctx)) |
9352 | break; | |
9353 | ||
9354 | schedule(); | |
9355 | } while (!signal_pending(current)); | |
9356 | ||
9357 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9358 | out: |
9359 | return ret; | |
90554200 JA |
9360 | } |
9361 | ||
c73ebb68 HX |
9362 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9363 | struct __kernel_timespec __user **ts, | |
9364 | const sigset_t __user **sig) | |
9365 | { | |
9366 | struct io_uring_getevents_arg arg; | |
9367 | ||
9368 | /* | |
9369 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9370 | * is just a pointer to the sigset_t. | |
9371 | */ | |
9372 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9373 | *sig = (const sigset_t __user *) argp; | |
9374 | *ts = NULL; | |
9375 | return 0; | |
9376 | } | |
9377 | ||
9378 | /* | |
9379 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9380 | * timespec and sigset_t pointers if good. | |
9381 | */ | |
9382 | if (*argsz != sizeof(arg)) | |
9383 | return -EINVAL; | |
9384 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9385 | return -EFAULT; | |
9386 | *sig = u64_to_user_ptr(arg.sigmask); | |
9387 | *argsz = arg.sigmask_sz; | |
9388 | *ts = u64_to_user_ptr(arg.ts); | |
9389 | return 0; | |
9390 | } | |
9391 | ||
2b188cc1 | 9392 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9393 | u32, min_complete, u32, flags, const void __user *, argp, |
9394 | size_t, argsz) | |
2b188cc1 JA |
9395 | { |
9396 | struct io_ring_ctx *ctx; | |
9397 | long ret = -EBADF; | |
9398 | int submitted = 0; | |
9399 | struct fd f; | |
9400 | ||
4c6e277c | 9401 | io_run_task_work(); |
b41e9852 | 9402 | |
90554200 | 9403 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9404 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9405 | return -EINVAL; |
9406 | ||
9407 | f = fdget(fd); | |
9408 | if (!f.file) | |
9409 | return -EBADF; | |
9410 | ||
9411 | ret = -EOPNOTSUPP; | |
9412 | if (f.file->f_op != &io_uring_fops) | |
9413 | goto out_fput; | |
9414 | ||
9415 | ret = -ENXIO; | |
9416 | ctx = f.file->private_data; | |
9417 | if (!percpu_ref_tryget(&ctx->refs)) | |
9418 | goto out_fput; | |
9419 | ||
7e84e1c7 SG |
9420 | ret = -EBADFD; |
9421 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9422 | goto out; | |
9423 | ||
6c271ce2 JA |
9424 | /* |
9425 | * For SQ polling, the thread will do all submissions and completions. | |
9426 | * Just return the requested submit count, and wake the thread if | |
9427 | * we were asked to. | |
9428 | */ | |
b2a9eada | 9429 | ret = 0; |
6c271ce2 | 9430 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9431 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9432 | |
d9d05217 PB |
9433 | ret = -EOWNERDEAD; |
9434 | if (unlikely(ctx->sqo_dead)) | |
9435 | goto out; | |
6c271ce2 | 9436 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9437 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9438 | if (flags & IORING_ENTER_SQ_WAIT) { |
9439 | ret = io_sqpoll_wait_sq(ctx); | |
9440 | if (ret) | |
9441 | goto out; | |
9442 | } | |
6c271ce2 | 9443 | submitted = to_submit; |
b2a9eada | 9444 | } else if (to_submit) { |
fdaf083c | 9445 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9446 | if (unlikely(ret)) |
9447 | goto out; | |
2b188cc1 | 9448 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9449 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9450 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9451 | |
9452 | if (submitted != to_submit) | |
9453 | goto out; | |
2b188cc1 JA |
9454 | } |
9455 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9456 | const sigset_t __user *sig; |
9457 | struct __kernel_timespec __user *ts; | |
9458 | ||
9459 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9460 | if (unlikely(ret)) | |
9461 | goto out; | |
9462 | ||
2b188cc1 JA |
9463 | min_complete = min(min_complete, ctx->cq_entries); |
9464 | ||
32b2244a XW |
9465 | /* |
9466 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9467 | * space applications don't need to do io completion events | |
9468 | * polling again, they can rely on io_sq_thread to do polling | |
9469 | * work, which can reduce cpu usage and uring_lock contention. | |
9470 | */ | |
9471 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9472 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9473 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9474 | } else { |
c73ebb68 | 9475 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9476 | } |
2b188cc1 JA |
9477 | } |
9478 | ||
7c504e65 | 9479 | out: |
6805b32e | 9480 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9481 | out_fput: |
9482 | fdput(f); | |
9483 | return submitted ? submitted : ret; | |
9484 | } | |
9485 | ||
bebdb65e | 9486 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9487 | static int io_uring_show_cred(int id, void *p, void *data) |
9488 | { | |
6b47ab81 JA |
9489 | struct io_identity *iod = p; |
9490 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9491 | struct seq_file *m = data; |
9492 | struct user_namespace *uns = seq_user_ns(m); | |
9493 | struct group_info *gi; | |
9494 | kernel_cap_t cap; | |
9495 | unsigned __capi; | |
9496 | int g; | |
9497 | ||
9498 | seq_printf(m, "%5d\n", id); | |
9499 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9500 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9501 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9502 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9503 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9504 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9505 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9506 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9507 | seq_puts(m, "\n\tGroups:\t"); | |
9508 | gi = cred->group_info; | |
9509 | for (g = 0; g < gi->ngroups; g++) { | |
9510 | seq_put_decimal_ull(m, g ? " " : "", | |
9511 | from_kgid_munged(uns, gi->gid[g])); | |
9512 | } | |
9513 | seq_puts(m, "\n\tCapEff:\t"); | |
9514 | cap = cred->cap_effective; | |
9515 | CAP_FOR_EACH_U32(__capi) | |
9516 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9517 | seq_putc(m, '\n'); | |
9518 | return 0; | |
9519 | } | |
9520 | ||
9521 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9522 | { | |
dbbe9c64 | 9523 | struct io_sq_data *sq = NULL; |
fad8e0de | 9524 | bool has_lock; |
87ce955b JA |
9525 | int i; |
9526 | ||
fad8e0de JA |
9527 | /* |
9528 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9529 | * since fdinfo case grabs it in the opposite direction of normal use | |
9530 | * cases. If we fail to get the lock, we just don't iterate any | |
9531 | * structures that could be going away outside the io_uring mutex. | |
9532 | */ | |
9533 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9534 | ||
dbbe9c64 JQ |
9535 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9536 | sq = ctx->sq_data; | |
9537 | ||
9538 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9539 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9540 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9541 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
269bbe5f | 9542 | struct fixed_rsrc_table *table; |
87ce955b JA |
9543 | struct file *f; |
9544 | ||
9545 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
9546 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
9547 | if (f) | |
9548 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9549 | else | |
9550 | seq_printf(m, "%5u: <none>\n", i); | |
9551 | } | |
9552 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9553 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9554 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9555 | ||
9556 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9557 | (unsigned int) buf->len); | |
9558 | } | |
fad8e0de | 9559 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9560 | seq_printf(m, "Personalities:\n"); |
9561 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9562 | } | |
d7718a9d JA |
9563 | seq_printf(m, "PollList:\n"); |
9564 | spin_lock_irq(&ctx->completion_lock); | |
9565 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9566 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9567 | struct io_kiocb *req; | |
9568 | ||
9569 | hlist_for_each_entry(req, list, hash_node) | |
9570 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9571 | req->task->task_works != NULL); | |
9572 | } | |
9573 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9574 | if (has_lock) |
9575 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9576 | } |
9577 | ||
9578 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9579 | { | |
9580 | struct io_ring_ctx *ctx = f->private_data; | |
9581 | ||
9582 | if (percpu_ref_tryget(&ctx->refs)) { | |
9583 | __io_uring_show_fdinfo(ctx, m); | |
9584 | percpu_ref_put(&ctx->refs); | |
9585 | } | |
9586 | } | |
bebdb65e | 9587 | #endif |
87ce955b | 9588 | |
2b188cc1 JA |
9589 | static const struct file_operations io_uring_fops = { |
9590 | .release = io_uring_release, | |
fcb323cc | 9591 | .flush = io_uring_flush, |
2b188cc1 | 9592 | .mmap = io_uring_mmap, |
6c5c240e RP |
9593 | #ifndef CONFIG_MMU |
9594 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9595 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9596 | #endif | |
2b188cc1 JA |
9597 | .poll = io_uring_poll, |
9598 | .fasync = io_uring_fasync, | |
bebdb65e | 9599 | #ifdef CONFIG_PROC_FS |
87ce955b | 9600 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9601 | #endif |
2b188cc1 JA |
9602 | }; |
9603 | ||
9604 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9605 | struct io_uring_params *p) | |
9606 | { | |
75b28aff HV |
9607 | struct io_rings *rings; |
9608 | size_t size, sq_array_offset; | |
2b188cc1 | 9609 | |
bd740481 JA |
9610 | /* make sure these are sane, as we already accounted them */ |
9611 | ctx->sq_entries = p->sq_entries; | |
9612 | ctx->cq_entries = p->cq_entries; | |
9613 | ||
75b28aff HV |
9614 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9615 | if (size == SIZE_MAX) | |
9616 | return -EOVERFLOW; | |
9617 | ||
9618 | rings = io_mem_alloc(size); | |
9619 | if (!rings) | |
2b188cc1 JA |
9620 | return -ENOMEM; |
9621 | ||
75b28aff HV |
9622 | ctx->rings = rings; |
9623 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9624 | rings->sq_ring_mask = p->sq_entries - 1; | |
9625 | rings->cq_ring_mask = p->cq_entries - 1; | |
9626 | rings->sq_ring_entries = p->sq_entries; | |
9627 | rings->cq_ring_entries = p->cq_entries; | |
9628 | ctx->sq_mask = rings->sq_ring_mask; | |
9629 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9630 | |
9631 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9632 | if (size == SIZE_MAX) { |
9633 | io_mem_free(ctx->rings); | |
9634 | ctx->rings = NULL; | |
2b188cc1 | 9635 | return -EOVERFLOW; |
eb065d30 | 9636 | } |
2b188cc1 JA |
9637 | |
9638 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9639 | if (!ctx->sq_sqes) { |
9640 | io_mem_free(ctx->rings); | |
9641 | ctx->rings = NULL; | |
2b188cc1 | 9642 | return -ENOMEM; |
eb065d30 | 9643 | } |
2b188cc1 | 9644 | |
2b188cc1 JA |
9645 | return 0; |
9646 | } | |
9647 | ||
9faadcc8 PB |
9648 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9649 | { | |
9650 | int ret, fd; | |
9651 | ||
9652 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9653 | if (fd < 0) | |
9654 | return fd; | |
9655 | ||
9656 | ret = io_uring_add_task_file(ctx, file); | |
9657 | if (ret) { | |
9658 | put_unused_fd(fd); | |
9659 | return ret; | |
9660 | } | |
9661 | fd_install(fd, file); | |
9662 | return fd; | |
9663 | } | |
9664 | ||
2b188cc1 JA |
9665 | /* |
9666 | * Allocate an anonymous fd, this is what constitutes the application | |
9667 | * visible backing of an io_uring instance. The application mmaps this | |
9668 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9669 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9670 | */ | |
9faadcc8 | 9671 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9672 | { |
9673 | struct file *file; | |
9faadcc8 | 9674 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9675 | int ret; |
9676 | ||
2b188cc1 JA |
9677 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9678 | &ctx->ring_sock); | |
9679 | if (ret) | |
9faadcc8 | 9680 | return ERR_PTR(ret); |
2b188cc1 JA |
9681 | #endif |
9682 | ||
2b188cc1 JA |
9683 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9684 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9685 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9686 | if (IS_ERR(file)) { |
9687 | sock_release(ctx->ring_sock); | |
9688 | ctx->ring_sock = NULL; | |
9689 | } else { | |
9690 | ctx->ring_sock->file = file; | |
0f212204 | 9691 | } |
2b188cc1 | 9692 | #endif |
9faadcc8 | 9693 | return file; |
2b188cc1 JA |
9694 | } |
9695 | ||
7f13657d XW |
9696 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9697 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9698 | { |
9699 | struct user_struct *user = NULL; | |
9700 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9701 | struct file *file; |
aad5d8da | 9702 | bool limit_mem; |
2b188cc1 JA |
9703 | int ret; |
9704 | ||
8110c1a6 | 9705 | if (!entries) |
2b188cc1 | 9706 | return -EINVAL; |
8110c1a6 JA |
9707 | if (entries > IORING_MAX_ENTRIES) { |
9708 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9709 | return -EINVAL; | |
9710 | entries = IORING_MAX_ENTRIES; | |
9711 | } | |
2b188cc1 JA |
9712 | |
9713 | /* | |
9714 | * Use twice as many entries for the CQ ring. It's possible for the | |
9715 | * application to drive a higher depth than the size of the SQ ring, | |
9716 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9717 | * some flexibility in overcommitting a bit. If the application has |
9718 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9719 | * of CQ ring entries manually. | |
2b188cc1 JA |
9720 | */ |
9721 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9722 | if (p->flags & IORING_SETUP_CQSIZE) { |
9723 | /* | |
9724 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9725 | * to a power-of-two, if it isn't already. We do NOT impose | |
9726 | * any cq vs sq ring sizing. | |
9727 | */ | |
eb2667b3 | 9728 | if (!p->cq_entries) |
33a107f0 | 9729 | return -EINVAL; |
8110c1a6 JA |
9730 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9731 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9732 | return -EINVAL; | |
9733 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9734 | } | |
eb2667b3 JQ |
9735 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9736 | if (p->cq_entries < p->sq_entries) | |
9737 | return -EINVAL; | |
33a107f0 JA |
9738 | } else { |
9739 | p->cq_entries = 2 * p->sq_entries; | |
9740 | } | |
2b188cc1 JA |
9741 | |
9742 | user = get_uid(current_user()); | |
aad5d8da | 9743 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9744 | |
aad5d8da | 9745 | if (limit_mem) { |
a087e2b5 | 9746 | ret = __io_account_mem(user, |
2b188cc1 JA |
9747 | ring_pages(p->sq_entries, p->cq_entries)); |
9748 | if (ret) { | |
9749 | free_uid(user); | |
9750 | return ret; | |
9751 | } | |
9752 | } | |
9753 | ||
9754 | ctx = io_ring_ctx_alloc(p); | |
9755 | if (!ctx) { | |
aad5d8da | 9756 | if (limit_mem) |
a087e2b5 | 9757 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9758 | p->cq_entries)); |
9759 | free_uid(user); | |
9760 | return -ENOMEM; | |
9761 | } | |
9762 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9763 | ctx->user = user; |
0b8c0ec7 | 9764 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9765 | #ifdef CONFIG_AUDIT |
9766 | ctx->loginuid = current->loginuid; | |
9767 | ctx->sessionid = current->sessionid; | |
9768 | #endif | |
2aede0e4 JA |
9769 | ctx->sqo_task = get_task_struct(current); |
9770 | ||
9771 | /* | |
9772 | * This is just grabbed for accounting purposes. When a process exits, | |
9773 | * the mm is exited and dropped before the files, hence we need to hang | |
9774 | * on to this mm purely for the purposes of being able to unaccount | |
9775 | * memory (locked/pinned vm). It's not used for anything else. | |
9776 | */ | |
6b7898eb | 9777 | mmgrab(current->mm); |
2aede0e4 | 9778 | ctx->mm_account = current->mm; |
6b7898eb | 9779 | |
91d8f519 DZ |
9780 | #ifdef CONFIG_BLK_CGROUP |
9781 | /* | |
9782 | * The sq thread will belong to the original cgroup it was inited in. | |
9783 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9784 | * issued bios will be associated with the closest cgroup later in the | |
9785 | * block layer. | |
9786 | */ | |
9787 | rcu_read_lock(); | |
9788 | ctx->sqo_blkcg_css = blkcg_css(); | |
9789 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9790 | rcu_read_unlock(); | |
9791 | if (!ret) { | |
9792 | /* don't init against a dying cgroup, have the user try again */ | |
9793 | ctx->sqo_blkcg_css = NULL; | |
9794 | ret = -ENODEV; | |
9795 | goto err; | |
9796 | } | |
9797 | #endif | |
6b7898eb | 9798 | |
f74441e6 JA |
9799 | /* |
9800 | * Account memory _before_ installing the file descriptor. Once | |
9801 | * the descriptor is installed, it can get closed at any time. Also | |
9802 | * do this before hitting the general error path, as ring freeing | |
9803 | * will un-account as well. | |
9804 | */ | |
9805 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9806 | ACCT_LOCKED); | |
9807 | ctx->limit_mem = limit_mem; | |
9808 | ||
2b188cc1 JA |
9809 | ret = io_allocate_scq_urings(ctx, p); |
9810 | if (ret) | |
9811 | goto err; | |
9812 | ||
7e84e1c7 | 9813 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9814 | if (ret) |
9815 | goto err; | |
9816 | ||
7e84e1c7 SG |
9817 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9818 | io_sq_offload_start(ctx); | |
9819 | ||
2b188cc1 | 9820 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9821 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9822 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9823 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9824 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9825 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9826 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9827 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9828 | |
9829 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9830 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9831 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9832 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9833 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9834 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9835 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9836 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9837 | |
7f13657d XW |
9838 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9839 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9840 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9841 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9842 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9843 | |
9844 | if (copy_to_user(params, p, sizeof(*p))) { | |
9845 | ret = -EFAULT; | |
9846 | goto err; | |
9847 | } | |
d1719f70 | 9848 | |
9faadcc8 PB |
9849 | file = io_uring_get_file(ctx); |
9850 | if (IS_ERR(file)) { | |
9851 | ret = PTR_ERR(file); | |
9852 | goto err; | |
9853 | } | |
9854 | ||
044c1ab3 JA |
9855 | /* |
9856 | * Install ring fd as the very last thing, so we don't risk someone | |
9857 | * having closed it before we finish setup | |
9858 | */ | |
9faadcc8 PB |
9859 | ret = io_uring_install_fd(ctx, file); |
9860 | if (ret < 0) { | |
06585c49 | 9861 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9862 | /* fput will clean it up */ |
9863 | fput(file); | |
9864 | return ret; | |
9865 | } | |
044c1ab3 | 9866 | |
c826bd7a | 9867 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9868 | return ret; |
9869 | err: | |
d9d05217 | 9870 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9871 | io_ring_ctx_wait_and_kill(ctx); |
9872 | return ret; | |
9873 | } | |
9874 | ||
9875 | /* | |
9876 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9877 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9878 | * params structure passed in. | |
9879 | */ | |
9880 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9881 | { | |
9882 | struct io_uring_params p; | |
2b188cc1 JA |
9883 | int i; |
9884 | ||
9885 | if (copy_from_user(&p, params, sizeof(p))) | |
9886 | return -EFAULT; | |
9887 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9888 | if (p.resv[i]) | |
9889 | return -EINVAL; | |
9890 | } | |
9891 | ||
6c271ce2 | 9892 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9893 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9894 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9895 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9896 | return -EINVAL; |
9897 | ||
7f13657d | 9898 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9899 | } |
9900 | ||
9901 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9902 | struct io_uring_params __user *, params) | |
9903 | { | |
9904 | return io_uring_setup(entries, params); | |
9905 | } | |
9906 | ||
66f4af93 JA |
9907 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9908 | { | |
9909 | struct io_uring_probe *p; | |
9910 | size_t size; | |
9911 | int i, ret; | |
9912 | ||
9913 | size = struct_size(p, ops, nr_args); | |
9914 | if (size == SIZE_MAX) | |
9915 | return -EOVERFLOW; | |
9916 | p = kzalloc(size, GFP_KERNEL); | |
9917 | if (!p) | |
9918 | return -ENOMEM; | |
9919 | ||
9920 | ret = -EFAULT; | |
9921 | if (copy_from_user(p, arg, size)) | |
9922 | goto out; | |
9923 | ret = -EINVAL; | |
9924 | if (memchr_inv(p, 0, size)) | |
9925 | goto out; | |
9926 | ||
9927 | p->last_op = IORING_OP_LAST - 1; | |
9928 | if (nr_args > IORING_OP_LAST) | |
9929 | nr_args = IORING_OP_LAST; | |
9930 | ||
9931 | for (i = 0; i < nr_args; i++) { | |
9932 | p->ops[i].op = i; | |
9933 | if (!io_op_defs[i].not_supported) | |
9934 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9935 | } | |
9936 | p->ops_len = i; | |
9937 | ||
9938 | ret = 0; | |
9939 | if (copy_to_user(arg, p, size)) | |
9940 | ret = -EFAULT; | |
9941 | out: | |
9942 | kfree(p); | |
9943 | return ret; | |
9944 | } | |
9945 | ||
071698e1 JA |
9946 | static int io_register_personality(struct io_ring_ctx *ctx) |
9947 | { | |
1e6fa521 JA |
9948 | struct io_identity *id; |
9949 | int ret; | |
071698e1 | 9950 | |
1e6fa521 JA |
9951 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9952 | if (unlikely(!id)) | |
9953 | return -ENOMEM; | |
9954 | ||
9955 | io_init_identity(id); | |
9956 | id->creds = get_current_cred(); | |
9957 | ||
9958 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9959 | if (ret < 0) { | |
9960 | put_cred(id->creds); | |
9961 | kfree(id); | |
9962 | } | |
9963 | return ret; | |
071698e1 JA |
9964 | } |
9965 | ||
9966 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
9967 | { | |
1e6fa521 | 9968 | struct io_identity *iod; |
071698e1 | 9969 | |
1e6fa521 JA |
9970 | iod = idr_remove(&ctx->personality_idr, id); |
9971 | if (iod) { | |
9972 | put_cred(iod->creds); | |
9973 | if (refcount_dec_and_test(&iod->count)) | |
9974 | kfree(iod); | |
071698e1 JA |
9975 | return 0; |
9976 | } | |
9977 | ||
9978 | return -EINVAL; | |
9979 | } | |
9980 | ||
21b55dbc SG |
9981 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9982 | unsigned int nr_args) | |
9983 | { | |
9984 | struct io_uring_restriction *res; | |
9985 | size_t size; | |
9986 | int i, ret; | |
9987 | ||
7e84e1c7 SG |
9988 | /* Restrictions allowed only if rings started disabled */ |
9989 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9990 | return -EBADFD; | |
9991 | ||
21b55dbc | 9992 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9993 | if (ctx->restrictions.registered) |
21b55dbc SG |
9994 | return -EBUSY; |
9995 | ||
9996 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9997 | return -EINVAL; | |
9998 | ||
9999 | size = array_size(nr_args, sizeof(*res)); | |
10000 | if (size == SIZE_MAX) | |
10001 | return -EOVERFLOW; | |
10002 | ||
10003 | res = memdup_user(arg, size); | |
10004 | if (IS_ERR(res)) | |
10005 | return PTR_ERR(res); | |
10006 | ||
10007 | ret = 0; | |
10008 | ||
10009 | for (i = 0; i < nr_args; i++) { | |
10010 | switch (res[i].opcode) { | |
10011 | case IORING_RESTRICTION_REGISTER_OP: | |
10012 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10013 | ret = -EINVAL; | |
10014 | goto out; | |
10015 | } | |
10016 | ||
10017 | __set_bit(res[i].register_op, | |
10018 | ctx->restrictions.register_op); | |
10019 | break; | |
10020 | case IORING_RESTRICTION_SQE_OP: | |
10021 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10022 | ret = -EINVAL; | |
10023 | goto out; | |
10024 | } | |
10025 | ||
10026 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10027 | break; | |
10028 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10029 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10030 | break; | |
10031 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10032 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10033 | break; | |
10034 | default: | |
10035 | ret = -EINVAL; | |
10036 | goto out; | |
10037 | } | |
10038 | } | |
10039 | ||
10040 | out: | |
10041 | /* Reset all restrictions if an error happened */ | |
10042 | if (ret != 0) | |
10043 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10044 | else | |
7e84e1c7 | 10045 | ctx->restrictions.registered = true; |
21b55dbc SG |
10046 | |
10047 | kfree(res); | |
10048 | return ret; | |
10049 | } | |
10050 | ||
7e84e1c7 SG |
10051 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10052 | { | |
10053 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10054 | return -EBADFD; | |
10055 | ||
10056 | if (ctx->restrictions.registered) | |
10057 | ctx->restricted = 1; | |
10058 | ||
10059 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
10060 | ||
10061 | io_sq_offload_start(ctx); | |
10062 | ||
10063 | return 0; | |
10064 | } | |
10065 | ||
071698e1 JA |
10066 | static bool io_register_op_must_quiesce(int op) |
10067 | { | |
10068 | switch (op) { | |
10069 | case IORING_UNREGISTER_FILES: | |
10070 | case IORING_REGISTER_FILES_UPDATE: | |
10071 | case IORING_REGISTER_PROBE: | |
10072 | case IORING_REGISTER_PERSONALITY: | |
10073 | case IORING_UNREGISTER_PERSONALITY: | |
10074 | return false; | |
10075 | default: | |
10076 | return true; | |
10077 | } | |
10078 | } | |
10079 | ||
edafccee JA |
10080 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10081 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10082 | __releases(ctx->uring_lock) |
10083 | __acquires(ctx->uring_lock) | |
edafccee JA |
10084 | { |
10085 | int ret; | |
10086 | ||
35fa71a0 JA |
10087 | /* |
10088 | * We're inside the ring mutex, if the ref is already dying, then | |
10089 | * someone else killed the ctx or is already going through | |
10090 | * io_uring_register(). | |
10091 | */ | |
10092 | if (percpu_ref_is_dying(&ctx->refs)) | |
10093 | return -ENXIO; | |
10094 | ||
071698e1 | 10095 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10096 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10097 | |
05f3fb3c JA |
10098 | /* |
10099 | * Drop uring mutex before waiting for references to exit. If | |
10100 | * another thread is currently inside io_uring_enter() it might | |
10101 | * need to grab the uring_lock to make progress. If we hold it | |
10102 | * here across the drain wait, then we can deadlock. It's safe | |
10103 | * to drop the mutex here, since no new references will come in | |
10104 | * after we've killed the percpu ref. | |
10105 | */ | |
10106 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10107 | do { |
10108 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10109 | if (!ret) | |
10110 | break; | |
ed6930c9 JA |
10111 | ret = io_run_task_work_sig(); |
10112 | if (ret < 0) | |
10113 | break; | |
af9c1a44 JA |
10114 | } while (1); |
10115 | ||
05f3fb3c | 10116 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10117 | |
c150368b JA |
10118 | if (ret) { |
10119 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
10120 | goto out_quiesce; |
10121 | } | |
10122 | } | |
10123 | ||
10124 | if (ctx->restricted) { | |
10125 | if (opcode >= IORING_REGISTER_LAST) { | |
10126 | ret = -EINVAL; | |
10127 | goto out; | |
10128 | } | |
10129 | ||
10130 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
10131 | ret = -EACCES; | |
c150368b JA |
10132 | goto out; |
10133 | } | |
05f3fb3c | 10134 | } |
edafccee JA |
10135 | |
10136 | switch (opcode) { | |
10137 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 10138 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
10139 | break; |
10140 | case IORING_UNREGISTER_BUFFERS: | |
10141 | ret = -EINVAL; | |
10142 | if (arg || nr_args) | |
10143 | break; | |
0a96bbe4 | 10144 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10145 | break; |
6b06314c JA |
10146 | case IORING_REGISTER_FILES: |
10147 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
10148 | break; | |
10149 | case IORING_UNREGISTER_FILES: | |
10150 | ret = -EINVAL; | |
10151 | if (arg || nr_args) | |
10152 | break; | |
10153 | ret = io_sqe_files_unregister(ctx); | |
10154 | break; | |
c3a31e60 JA |
10155 | case IORING_REGISTER_FILES_UPDATE: |
10156 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
10157 | break; | |
9b402849 | 10158 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10159 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10160 | ret = -EINVAL; |
10161 | if (nr_args != 1) | |
10162 | break; | |
10163 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10164 | if (ret) |
10165 | break; | |
10166 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10167 | ctx->eventfd_async = 1; | |
10168 | else | |
10169 | ctx->eventfd_async = 0; | |
9b402849 JA |
10170 | break; |
10171 | case IORING_UNREGISTER_EVENTFD: | |
10172 | ret = -EINVAL; | |
10173 | if (arg || nr_args) | |
10174 | break; | |
10175 | ret = io_eventfd_unregister(ctx); | |
10176 | break; | |
66f4af93 JA |
10177 | case IORING_REGISTER_PROBE: |
10178 | ret = -EINVAL; | |
10179 | if (!arg || nr_args > 256) | |
10180 | break; | |
10181 | ret = io_probe(ctx, arg, nr_args); | |
10182 | break; | |
071698e1 JA |
10183 | case IORING_REGISTER_PERSONALITY: |
10184 | ret = -EINVAL; | |
10185 | if (arg || nr_args) | |
10186 | break; | |
10187 | ret = io_register_personality(ctx); | |
10188 | break; | |
10189 | case IORING_UNREGISTER_PERSONALITY: | |
10190 | ret = -EINVAL; | |
10191 | if (arg) | |
10192 | break; | |
10193 | ret = io_unregister_personality(ctx, nr_args); | |
10194 | break; | |
7e84e1c7 SG |
10195 | case IORING_REGISTER_ENABLE_RINGS: |
10196 | ret = -EINVAL; | |
10197 | if (arg || nr_args) | |
10198 | break; | |
10199 | ret = io_register_enable_rings(ctx); | |
10200 | break; | |
21b55dbc SG |
10201 | case IORING_REGISTER_RESTRICTIONS: |
10202 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10203 | break; | |
edafccee JA |
10204 | default: |
10205 | ret = -EINVAL; | |
10206 | break; | |
10207 | } | |
10208 | ||
21b55dbc | 10209 | out: |
071698e1 | 10210 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10211 | /* bring the ctx back to life */ |
05f3fb3c | 10212 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 10213 | out_quiesce: |
0f158b4c | 10214 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10215 | } |
edafccee JA |
10216 | return ret; |
10217 | } | |
10218 | ||
10219 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10220 | void __user *, arg, unsigned int, nr_args) | |
10221 | { | |
10222 | struct io_ring_ctx *ctx; | |
10223 | long ret = -EBADF; | |
10224 | struct fd f; | |
10225 | ||
10226 | f = fdget(fd); | |
10227 | if (!f.file) | |
10228 | return -EBADF; | |
10229 | ||
10230 | ret = -EOPNOTSUPP; | |
10231 | if (f.file->f_op != &io_uring_fops) | |
10232 | goto out_fput; | |
10233 | ||
10234 | ctx = f.file->private_data; | |
10235 | ||
10236 | mutex_lock(&ctx->uring_lock); | |
10237 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10238 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10239 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10240 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10241 | out_fput: |
10242 | fdput(f); | |
10243 | return ret; | |
10244 | } | |
10245 | ||
2b188cc1 JA |
10246 | static int __init io_uring_init(void) |
10247 | { | |
d7f62e82 SM |
10248 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10249 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10250 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10251 | } while (0) | |
10252 | ||
10253 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10254 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10255 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10256 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10257 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10258 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10259 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10260 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10261 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10262 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10263 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10264 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10265 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10266 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10267 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10268 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10269 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10270 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10271 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10272 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10273 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10274 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10275 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10276 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10277 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10278 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10279 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10280 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10281 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10282 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10283 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10284 | |
d3656344 | 10285 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10286 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
10287 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
10288 | return 0; | |
10289 | }; | |
10290 | __initcall(io_uring_init); |