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 JA |
2691 | #ifdef CONFIG_BLOCK |
2692 | static bool io_resubmit_prep(struct io_kiocb *req, int error) | |
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 | ||
2699 | if (error) { | |
2700 | ret = error; | |
2701 | goto end_req; | |
2702 | } | |
2703 | ||
2704 | switch (req->opcode) { | |
2705 | case IORING_OP_READV: | |
2706 | case IORING_OP_READ_FIXED: | |
2707 | case IORING_OP_READ: | |
2708 | rw = READ; | |
2709 | break; | |
2710 | case IORING_OP_WRITEV: | |
2711 | case IORING_OP_WRITE_FIXED: | |
2712 | case IORING_OP_WRITE: | |
2713 | rw = WRITE; | |
2714 | break; | |
2715 | default: | |
2716 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2717 | req->opcode); | |
2718 | goto end_req; | |
2719 | } | |
2720 | ||
e8c2bc1f | 2721 | if (!req->async_data) { |
8f3d7496 JA |
2722 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2723 | if (ret < 0) | |
2724 | goto end_req; | |
2725 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); | |
2726 | if (!ret) | |
2727 | return true; | |
2728 | kfree(iovec); | |
2729 | } else { | |
b63534c4 | 2730 | return true; |
8f3d7496 | 2731 | } |
b63534c4 | 2732 | end_req: |
b63534c4 | 2733 | req_set_fail_links(req); |
b63534c4 JA |
2734 | return false; |
2735 | } | |
b63534c4 JA |
2736 | #endif |
2737 | ||
2738 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2739 | { | |
2740 | #ifdef CONFIG_BLOCK | |
355afaeb | 2741 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 JA |
2742 | int ret; |
2743 | ||
355afaeb JA |
2744 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2745 | return false; | |
b63534c4 JA |
2746 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
2747 | return false; | |
2748 | ||
55e6ac1e PB |
2749 | lockdep_assert_held(&req->ctx->uring_lock); |
2750 | ||
28cea78a | 2751 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2752 | |
fdee946d JA |
2753 | if (io_resubmit_prep(req, ret)) { |
2754 | refcount_inc(&req->refs); | |
2755 | io_queue_async_work(req); | |
b63534c4 | 2756 | return true; |
fdee946d JA |
2757 | } |
2758 | ||
b63534c4 JA |
2759 | #endif |
2760 | return false; | |
2761 | } | |
2762 | ||
a1d7c393 JA |
2763 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2764 | struct io_comp_state *cs) | |
2765 | { | |
2766 | if (!io_rw_reissue(req, res)) | |
2767 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2768 | } |
2769 | ||
2770 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2771 | { | |
9adbd45d | 2772 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2773 | |
a1d7c393 | 2774 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2775 | } |
2776 | ||
def596e9 JA |
2777 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2778 | { | |
9adbd45d | 2779 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2780 | |
491381ce JA |
2781 | if (kiocb->ki_flags & IOCB_WRITE) |
2782 | kiocb_end_write(req); | |
def596e9 | 2783 | |
2d7d6792 | 2784 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2785 | req_set_fail_links(req); |
bbde017a XW |
2786 | |
2787 | WRITE_ONCE(req->result, res); | |
2788 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2789 | smp_wmb(); |
2790 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2791 | } |
2792 | ||
2793 | /* | |
2794 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2795 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2796 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2797 | * accessing the kiocb cookie. | |
2798 | */ | |
2e9dbe90 | 2799 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2800 | { |
2801 | struct io_ring_ctx *ctx = req->ctx; | |
2802 | ||
2803 | /* | |
2804 | * Track whether we have multiple files in our lists. This will impact | |
2805 | * how we do polling eventually, not spinning if we're on potentially | |
2806 | * different devices. | |
2807 | */ | |
540e32a0 | 2808 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2809 | ctx->poll_multi_file = false; |
2810 | } else if (!ctx->poll_multi_file) { | |
2811 | struct io_kiocb *list_req; | |
2812 | ||
540e32a0 | 2813 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2814 | inflight_entry); |
9adbd45d | 2815 | if (list_req->file != req->file) |
def596e9 JA |
2816 | ctx->poll_multi_file = true; |
2817 | } | |
2818 | ||
2819 | /* | |
2820 | * For fast devices, IO may have already completed. If it has, add | |
2821 | * it to the front so we find it first. | |
2822 | */ | |
65a6543d | 2823 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2824 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2825 | else |
d21ffe7e | 2826 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2827 | |
2e9dbe90 XW |
2828 | /* |
2829 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2830 | * task context or in io worker task context. If current task context is | |
2831 | * sq thread, we don't need to check whether should wake up sq thread. | |
2832 | */ | |
2833 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2834 | wq_has_sleeper(&ctx->sq_data->wait)) |
2835 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2836 | } |
2837 | ||
6e1271e6 | 2838 | static inline void __io_state_file_put(struct io_submit_state *state) |
9a56a232 | 2839 | { |
6e1271e6 PB |
2840 | fput_many(state->file, state->file_refs); |
2841 | state->file_refs = 0; | |
9f13c35b PB |
2842 | } |
2843 | ||
2844 | static inline void io_state_file_put(struct io_submit_state *state) | |
2845 | { | |
6e1271e6 | 2846 | if (state->file_refs) |
9f13c35b | 2847 | __io_state_file_put(state); |
9a56a232 JA |
2848 | } |
2849 | ||
2850 | /* | |
2851 | * Get as many references to a file as we have IOs left in this submission, | |
2852 | * assuming most submissions are for one file, or at least that each file | |
2853 | * has more than one submission. | |
2854 | */ | |
8da11c19 | 2855 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2856 | { |
2857 | if (!state) | |
2858 | return fget(fd); | |
2859 | ||
6e1271e6 | 2860 | if (state->file_refs) { |
9a56a232 | 2861 | if (state->fd == fd) { |
6e1271e6 | 2862 | state->file_refs--; |
9a56a232 JA |
2863 | return state->file; |
2864 | } | |
9f13c35b | 2865 | __io_state_file_put(state); |
9a56a232 JA |
2866 | } |
2867 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2868 | if (unlikely(!state->file)) |
9a56a232 JA |
2869 | return NULL; |
2870 | ||
2871 | state->fd = fd; | |
6e1271e6 | 2872 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2873 | return state->file; |
2874 | } | |
2875 | ||
4503b767 JA |
2876 | static bool io_bdev_nowait(struct block_device *bdev) |
2877 | { | |
9ba0d0c8 | 2878 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2879 | } |
2880 | ||
2b188cc1 JA |
2881 | /* |
2882 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2883 | * any file. For now, just ensure that anything potentially problematic is done | |
2884 | * inline. | |
2885 | */ | |
af197f50 | 2886 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2887 | { |
2888 | umode_t mode = file_inode(file)->i_mode; | |
2889 | ||
4503b767 | 2890 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2891 | if (IS_ENABLED(CONFIG_BLOCK) && |
2892 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2893 | return true; |
2894 | return false; | |
2895 | } | |
2896 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2897 | return true; |
4503b767 | 2898 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2899 | if (IS_ENABLED(CONFIG_BLOCK) && |
2900 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2901 | file->f_op != &io_uring_fops) |
2902 | return true; | |
2903 | return false; | |
2904 | } | |
2b188cc1 | 2905 | |
c5b85625 JA |
2906 | /* any ->read/write should understand O_NONBLOCK */ |
2907 | if (file->f_flags & O_NONBLOCK) | |
2908 | return true; | |
2909 | ||
af197f50 JA |
2910 | if (!(file->f_mode & FMODE_NOWAIT)) |
2911 | return false; | |
2912 | ||
2913 | if (rw == READ) | |
2914 | return file->f_op->read_iter != NULL; | |
2915 | ||
2916 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2917 | } |
2918 | ||
a88fc400 | 2919 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2920 | { |
def596e9 | 2921 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2922 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2923 | unsigned ioprio; |
2924 | int ret; | |
2b188cc1 | 2925 | |
491381ce JA |
2926 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2927 | req->flags |= REQ_F_ISREG; | |
2928 | ||
2b188cc1 | 2929 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2930 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2931 | req->flags |= REQ_F_CUR_POS; | |
2932 | kiocb->ki_pos = req->file->f_pos; | |
2933 | } | |
2b188cc1 | 2934 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2935 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2936 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2937 | if (unlikely(ret)) | |
2938 | return ret; | |
2b188cc1 JA |
2939 | |
2940 | ioprio = READ_ONCE(sqe->ioprio); | |
2941 | if (ioprio) { | |
2942 | ret = ioprio_check_cap(ioprio); | |
2943 | if (ret) | |
09bb8394 | 2944 | return ret; |
2b188cc1 JA |
2945 | |
2946 | kiocb->ki_ioprio = ioprio; | |
2947 | } else | |
2948 | kiocb->ki_ioprio = get_current_ioprio(); | |
2949 | ||
8449eeda | 2950 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2951 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2952 | req->flags |= REQ_F_NOWAIT; |
2953 | ||
def596e9 | 2954 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2955 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2956 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2957 | return -EOPNOTSUPP; |
2b188cc1 | 2958 | |
def596e9 JA |
2959 | kiocb->ki_flags |= IOCB_HIPRI; |
2960 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2961 | req->iopoll_completed = 0; |
def596e9 | 2962 | } else { |
09bb8394 JA |
2963 | if (kiocb->ki_flags & IOCB_HIPRI) |
2964 | return -EINVAL; | |
def596e9 JA |
2965 | kiocb->ki_complete = io_complete_rw; |
2966 | } | |
9adbd45d | 2967 | |
3529d8c2 JA |
2968 | req->rw.addr = READ_ONCE(sqe->addr); |
2969 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2970 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2971 | return 0; |
2b188cc1 JA |
2972 | } |
2973 | ||
2974 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2975 | { | |
2976 | switch (ret) { | |
2977 | case -EIOCBQUEUED: | |
2978 | break; | |
2979 | case -ERESTARTSYS: | |
2980 | case -ERESTARTNOINTR: | |
2981 | case -ERESTARTNOHAND: | |
2982 | case -ERESTART_RESTARTBLOCK: | |
2983 | /* | |
2984 | * We can't just restart the syscall, since previously | |
2985 | * submitted sqes may already be in progress. Just fail this | |
2986 | * IO with EINTR. | |
2987 | */ | |
2988 | ret = -EINTR; | |
df561f66 | 2989 | fallthrough; |
2b188cc1 JA |
2990 | default: |
2991 | kiocb->ki_complete(kiocb, ret, 0); | |
2992 | } | |
2993 | } | |
2994 | ||
a1d7c393 JA |
2995 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2996 | struct io_comp_state *cs) | |
ba816ad6 | 2997 | { |
ba04291e | 2998 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2999 | struct io_async_rw *io = req->async_data; |
ba04291e | 3000 | |
227c0c96 | 3001 | /* add previously done IO, if any */ |
e8c2bc1f | 3002 | if (io && io->bytes_done > 0) { |
227c0c96 | 3003 | if (ret < 0) |
e8c2bc1f | 3004 | ret = io->bytes_done; |
227c0c96 | 3005 | else |
e8c2bc1f | 3006 | ret += io->bytes_done; |
227c0c96 JA |
3007 | } |
3008 | ||
ba04291e JA |
3009 | if (req->flags & REQ_F_CUR_POS) |
3010 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 3011 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 3012 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
3013 | else |
3014 | io_rw_done(kiocb, ret); | |
3015 | } | |
3016 | ||
9adbd45d | 3017 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 3018 | struct iov_iter *iter) |
edafccee | 3019 | { |
9adbd45d JA |
3020 | struct io_ring_ctx *ctx = req->ctx; |
3021 | size_t len = req->rw.len; | |
edafccee | 3022 | struct io_mapped_ubuf *imu; |
4be1c615 | 3023 | u16 index, buf_index = req->buf_index; |
edafccee JA |
3024 | size_t offset; |
3025 | u64 buf_addr; | |
3026 | ||
edafccee JA |
3027 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3028 | return -EFAULT; | |
edafccee JA |
3029 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3030 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 3031 | buf_addr = req->rw.addr; |
edafccee JA |
3032 | |
3033 | /* overflow */ | |
3034 | if (buf_addr + len < buf_addr) | |
3035 | return -EFAULT; | |
3036 | /* not inside the mapped region */ | |
3037 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
3038 | return -EFAULT; | |
3039 | ||
3040 | /* | |
3041 | * May not be a start of buffer, set size appropriately | |
3042 | * and advance us to the beginning. | |
3043 | */ | |
3044 | offset = buf_addr - imu->ubuf; | |
3045 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3046 | |
3047 | if (offset) { | |
3048 | /* | |
3049 | * Don't use iov_iter_advance() here, as it's really slow for | |
3050 | * using the latter parts of a big fixed buffer - it iterates | |
3051 | * over each segment manually. We can cheat a bit here, because | |
3052 | * we know that: | |
3053 | * | |
3054 | * 1) it's a BVEC iter, we set it up | |
3055 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3056 | * first and last bvec | |
3057 | * | |
3058 | * So just find our index, and adjust the iterator afterwards. | |
3059 | * If the offset is within the first bvec (or the whole first | |
3060 | * bvec, just use iov_iter_advance(). This makes it easier | |
3061 | * since we can just skip the first segment, which may not | |
3062 | * be PAGE_SIZE aligned. | |
3063 | */ | |
3064 | const struct bio_vec *bvec = imu->bvec; | |
3065 | ||
3066 | if (offset <= bvec->bv_len) { | |
3067 | iov_iter_advance(iter, offset); | |
3068 | } else { | |
3069 | unsigned long seg_skip; | |
3070 | ||
3071 | /* skip first vec */ | |
3072 | offset -= bvec->bv_len; | |
3073 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3074 | ||
3075 | iter->bvec = bvec + seg_skip; | |
3076 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3077 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3078 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3079 | } |
3080 | } | |
3081 | ||
5e559561 | 3082 | return len; |
edafccee JA |
3083 | } |
3084 | ||
bcda7baa JA |
3085 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3086 | { | |
3087 | if (needs_lock) | |
3088 | mutex_unlock(&ctx->uring_lock); | |
3089 | } | |
3090 | ||
3091 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3092 | { | |
3093 | /* | |
3094 | * "Normal" inline submissions always hold the uring_lock, since we | |
3095 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3096 | * The only exception is when we've detached the request and issue it | |
3097 | * from an async worker thread, grab the lock for that case. | |
3098 | */ | |
3099 | if (needs_lock) | |
3100 | mutex_lock(&ctx->uring_lock); | |
3101 | } | |
3102 | ||
3103 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3104 | int bgid, struct io_buffer *kbuf, | |
3105 | bool needs_lock) | |
3106 | { | |
3107 | struct io_buffer *head; | |
3108 | ||
3109 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3110 | return kbuf; | |
3111 | ||
3112 | io_ring_submit_lock(req->ctx, needs_lock); | |
3113 | ||
3114 | lockdep_assert_held(&req->ctx->uring_lock); | |
3115 | ||
3116 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3117 | if (head) { | |
3118 | if (!list_empty(&head->list)) { | |
3119 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3120 | list); | |
3121 | list_del(&kbuf->list); | |
3122 | } else { | |
3123 | kbuf = head; | |
3124 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3125 | } | |
3126 | if (*len > kbuf->len) | |
3127 | *len = kbuf->len; | |
3128 | } else { | |
3129 | kbuf = ERR_PTR(-ENOBUFS); | |
3130 | } | |
3131 | ||
3132 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3133 | ||
3134 | return kbuf; | |
3135 | } | |
3136 | ||
4d954c25 JA |
3137 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3138 | bool needs_lock) | |
3139 | { | |
3140 | struct io_buffer *kbuf; | |
4f4eeba8 | 3141 | u16 bgid; |
4d954c25 JA |
3142 | |
3143 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3144 | bgid = req->buf_index; |
4d954c25 JA |
3145 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3146 | if (IS_ERR(kbuf)) | |
3147 | return kbuf; | |
3148 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3149 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3150 | return u64_to_user_ptr(kbuf->addr); | |
3151 | } | |
3152 | ||
3153 | #ifdef CONFIG_COMPAT | |
3154 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3155 | bool needs_lock) | |
3156 | { | |
3157 | struct compat_iovec __user *uiov; | |
3158 | compat_ssize_t clen; | |
3159 | void __user *buf; | |
3160 | ssize_t len; | |
3161 | ||
3162 | uiov = u64_to_user_ptr(req->rw.addr); | |
3163 | if (!access_ok(uiov, sizeof(*uiov))) | |
3164 | return -EFAULT; | |
3165 | if (__get_user(clen, &uiov->iov_len)) | |
3166 | return -EFAULT; | |
3167 | if (clen < 0) | |
3168 | return -EINVAL; | |
3169 | ||
3170 | len = clen; | |
3171 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3172 | if (IS_ERR(buf)) | |
3173 | return PTR_ERR(buf); | |
3174 | iov[0].iov_base = buf; | |
3175 | iov[0].iov_len = (compat_size_t) len; | |
3176 | return 0; | |
3177 | } | |
3178 | #endif | |
3179 | ||
3180 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3181 | bool needs_lock) | |
3182 | { | |
3183 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3184 | void __user *buf; | |
3185 | ssize_t len; | |
3186 | ||
3187 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3188 | return -EFAULT; | |
3189 | ||
3190 | len = iov[0].iov_len; | |
3191 | if (len < 0) | |
3192 | return -EINVAL; | |
3193 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3194 | if (IS_ERR(buf)) | |
3195 | return PTR_ERR(buf); | |
3196 | iov[0].iov_base = buf; | |
3197 | iov[0].iov_len = len; | |
3198 | return 0; | |
3199 | } | |
3200 | ||
3201 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3202 | bool needs_lock) | |
3203 | { | |
dddb3e26 JA |
3204 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3205 | struct io_buffer *kbuf; | |
3206 | ||
3207 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3208 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3209 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3210 | return 0; |
dddb3e26 | 3211 | } |
dd201662 | 3212 | if (req->rw.len != 1) |
4d954c25 JA |
3213 | return -EINVAL; |
3214 | ||
3215 | #ifdef CONFIG_COMPAT | |
3216 | if (req->ctx->compat) | |
3217 | return io_compat_import(req, iov, needs_lock); | |
3218 | #endif | |
3219 | ||
3220 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3221 | } | |
3222 | ||
2846c481 | 3223 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
8452fd0c JA |
3224 | struct iovec **iovec, struct iov_iter *iter, |
3225 | bool needs_lock) | |
2b188cc1 | 3226 | { |
9adbd45d JA |
3227 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3228 | size_t sqe_len = req->rw.len; | |
4d954c25 | 3229 | ssize_t ret; |
edafccee JA |
3230 | u8 opcode; |
3231 | ||
d625c6ee | 3232 | opcode = req->opcode; |
7d009165 | 3233 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3234 | *iovec = NULL; |
9adbd45d | 3235 | return io_import_fixed(req, rw, iter); |
edafccee | 3236 | } |
2b188cc1 | 3237 | |
bcda7baa | 3238 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3239 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3240 | return -EINVAL; |
3241 | ||
3a6820f2 | 3242 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3243 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3244 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3245 | if (IS_ERR(buf)) |
4d954c25 | 3246 | return PTR_ERR(buf); |
3f9d6441 | 3247 | req->rw.len = sqe_len; |
bcda7baa JA |
3248 | } |
3249 | ||
3a6820f2 JA |
3250 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3251 | *iovec = NULL; | |
10fc72e4 | 3252 | return ret; |
3a6820f2 JA |
3253 | } |
3254 | ||
4d954c25 JA |
3255 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3256 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
3257 | if (!ret) { |
3258 | ret = (*iovec)->iov_len; | |
3259 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
3260 | } | |
4d954c25 JA |
3261 | *iovec = NULL; |
3262 | return ret; | |
3263 | } | |
3264 | ||
89cd35c5 CH |
3265 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3266 | req->ctx->compat); | |
2b188cc1 JA |
3267 | } |
3268 | ||
0fef9483 JA |
3269 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3270 | { | |
5b09e37e | 3271 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3272 | } |
3273 | ||
31b51510 | 3274 | /* |
32960613 JA |
3275 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3276 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3277 | */ |
4017eb91 | 3278 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3279 | { |
4017eb91 JA |
3280 | struct kiocb *kiocb = &req->rw.kiocb; |
3281 | struct file *file = req->file; | |
32960613 JA |
3282 | ssize_t ret = 0; |
3283 | ||
3284 | /* | |
3285 | * Don't support polled IO through this interface, and we can't | |
3286 | * support non-blocking either. For the latter, this just causes | |
3287 | * the kiocb to be handled from an async context. | |
3288 | */ | |
3289 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3290 | return -EOPNOTSUPP; | |
3291 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3292 | return -EAGAIN; | |
3293 | ||
3294 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3295 | struct iovec iovec; |
32960613 JA |
3296 | ssize_t nr; |
3297 | ||
311ae9e1 PB |
3298 | if (!iov_iter_is_bvec(iter)) { |
3299 | iovec = iov_iter_iovec(iter); | |
3300 | } else { | |
4017eb91 JA |
3301 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3302 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3303 | } |
3304 | ||
32960613 JA |
3305 | if (rw == READ) { |
3306 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3307 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3308 | } else { |
3309 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3310 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3311 | } |
3312 | ||
3313 | if (nr < 0) { | |
3314 | if (!ret) | |
3315 | ret = nr; | |
3316 | break; | |
3317 | } | |
3318 | ret += nr; | |
3319 | if (nr != iovec.iov_len) | |
3320 | break; | |
4017eb91 JA |
3321 | req->rw.len -= nr; |
3322 | req->rw.addr += nr; | |
32960613 JA |
3323 | iov_iter_advance(iter, nr); |
3324 | } | |
3325 | ||
3326 | return ret; | |
3327 | } | |
3328 | ||
ff6165b2 JA |
3329 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3330 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3331 | { |
e8c2bc1f | 3332 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3333 | |
ff6165b2 | 3334 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3335 | rw->free_iovec = iovec; |
227c0c96 | 3336 | rw->bytes_done = 0; |
ff6165b2 | 3337 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3338 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3339 | return; |
b64e3444 | 3340 | if (!iovec) { |
ff6165b2 JA |
3341 | unsigned iov_off = 0; |
3342 | ||
3343 | rw->iter.iov = rw->fast_iov; | |
3344 | if (iter->iov != fast_iov) { | |
3345 | iov_off = iter->iov - fast_iov; | |
3346 | rw->iter.iov += iov_off; | |
3347 | } | |
3348 | if (rw->fast_iov != fast_iov) | |
3349 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3350 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3351 | } else { |
3352 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3353 | } |
3354 | } | |
3355 | ||
e8c2bc1f | 3356 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3357 | { |
e8c2bc1f JA |
3358 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3359 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3360 | return req->async_data == NULL; | |
3d9932a8 XW |
3361 | } |
3362 | ||
e8c2bc1f | 3363 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3364 | { |
e8c2bc1f | 3365 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3366 | return 0; |
3d9932a8 | 3367 | |
e8c2bc1f | 3368 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3369 | } |
3370 | ||
ff6165b2 JA |
3371 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3372 | const struct iovec *fast_iov, | |
227c0c96 | 3373 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3374 | { |
e8c2bc1f | 3375 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3376 | return 0; |
e8c2bc1f JA |
3377 | if (!req->async_data) { |
3378 | if (__io_alloc_async_data(req)) | |
5d204bcf | 3379 | return -ENOMEM; |
b7bb4f7d | 3380 | |
ff6165b2 | 3381 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3382 | } |
b7bb4f7d | 3383 | return 0; |
f67676d1 JA |
3384 | } |
3385 | ||
73debe68 | 3386 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3387 | { |
e8c2bc1f | 3388 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3389 | struct iovec *iov = iorw->fast_iov; |
c3e330a4 PB |
3390 | ssize_t ret; |
3391 | ||
2846c481 | 3392 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3393 | if (unlikely(ret < 0)) |
3394 | return ret; | |
3395 | ||
ab0b196c PB |
3396 | iorw->bytes_done = 0; |
3397 | iorw->free_iovec = iov; | |
3398 | if (iov) | |
3399 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3400 | return 0; |
3401 | } | |
3402 | ||
73debe68 | 3403 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3404 | { |
3405 | ssize_t ret; | |
3406 | ||
a88fc400 | 3407 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3408 | if (ret) |
3409 | return ret; | |
f67676d1 | 3410 | |
3529d8c2 JA |
3411 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3412 | return -EBADF; | |
f67676d1 | 3413 | |
5f798bea | 3414 | /* either don't need iovec imported or already have it */ |
2d199895 | 3415 | if (!req->async_data) |
3529d8c2 | 3416 | return 0; |
73debe68 | 3417 | return io_rw_prep_async(req, READ); |
f67676d1 JA |
3418 | } |
3419 | ||
c1dd91d1 JA |
3420 | /* |
3421 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3422 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3423 | * This gets called when the page is unlocked, and we generally expect that to | |
3424 | * happen when the page IO is completed and the page is now uptodate. This will | |
3425 | * queue a task_work based retry of the operation, attempting to copy the data | |
3426 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3427 | * do a thread based blocking retry of the operation. That's the unexpected | |
3428 | * slow path. | |
3429 | */ | |
bcf5a063 JA |
3430 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3431 | int sync, void *arg) | |
3432 | { | |
3433 | struct wait_page_queue *wpq; | |
3434 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3435 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3436 | int ret; |
3437 | ||
3438 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3439 | ||
cdc8fcb4 LT |
3440 | if (!wake_page_match(wpq, key)) |
3441 | return 0; | |
3442 | ||
c8d317aa | 3443 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3444 | list_del_init(&wait->entry); |
3445 | ||
e7375122 | 3446 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3447 | percpu_ref_get(&req->ctx->refs); |
3448 | ||
bcf5a063 JA |
3449 | /* submit ref gets dropped, acquire a new one */ |
3450 | refcount_inc(&req->refs); | |
355fb9e2 | 3451 | ret = io_req_task_work_add(req); |
bcf5a063 | 3452 | if (unlikely(ret)) { |
c2c4c83c JA |
3453 | struct task_struct *tsk; |
3454 | ||
bcf5a063 | 3455 | /* queue just for cancelation */ |
e7375122 | 3456 | init_task_work(&req->task_work, io_req_task_cancel); |
bcf5a063 | 3457 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 3458 | task_work_add(tsk, &req->task_work, TWA_NONE); |
c2c4c83c | 3459 | wake_up_process(tsk); |
bcf5a063 | 3460 | } |
bcf5a063 JA |
3461 | return 1; |
3462 | } | |
3463 | ||
c1dd91d1 JA |
3464 | /* |
3465 | * This controls whether a given IO request should be armed for async page | |
3466 | * based retry. If we return false here, the request is handed to the async | |
3467 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3468 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3469 | * will either succeed because the page is now uptodate and unlocked, or it | |
3470 | * will register a callback when the page is unlocked at IO completion. Through | |
3471 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3472 | * That retry will attempt the buffered read again. The retry will generally | |
3473 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3474 | * async worker threads for a blocking retry. | |
3475 | */ | |
227c0c96 | 3476 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3477 | { |
e8c2bc1f JA |
3478 | struct io_async_rw *rw = req->async_data; |
3479 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3480 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3481 | |
bcf5a063 JA |
3482 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3483 | if (req->flags & REQ_F_NOWAIT) | |
3484 | return false; | |
f67676d1 | 3485 | |
227c0c96 | 3486 | /* Only for buffered IO */ |
3b2a4439 | 3487 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3488 | return false; |
3b2a4439 | 3489 | |
bcf5a063 JA |
3490 | /* |
3491 | * just use poll if we can, and don't attempt if the fs doesn't | |
3492 | * support callback based unlocks | |
3493 | */ | |
3494 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3495 | return false; | |
f67676d1 | 3496 | |
3b2a4439 JA |
3497 | wait->wait.func = io_async_buf_func; |
3498 | wait->wait.private = req; | |
3499 | wait->wait.flags = 0; | |
3500 | INIT_LIST_HEAD(&wait->wait.entry); | |
3501 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3502 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3503 | kiocb->ki_waitq = wait; |
3b2a4439 | 3504 | return true; |
bcf5a063 JA |
3505 | } |
3506 | ||
3507 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3508 | { | |
3509 | if (req->file->f_op->read_iter) | |
3510 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3511 | else if (req->file->f_op->read) |
4017eb91 | 3512 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3513 | else |
3514 | return -EINVAL; | |
f67676d1 JA |
3515 | } |
3516 | ||
a1d7c393 JA |
3517 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3518 | struct io_comp_state *cs) | |
2b188cc1 JA |
3519 | { |
3520 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3521 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3522 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3523 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3524 | ssize_t io_size, ret, ret2; |
f5cac8b1 | 3525 | bool no_async; |
ff6165b2 | 3526 | |
2846c481 | 3527 | if (rw) { |
e8c2bc1f | 3528 | iter = &rw->iter; |
2846c481 PB |
3529 | iovec = NULL; |
3530 | } else { | |
3531 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3532 | if (ret < 0) | |
3533 | return ret; | |
3534 | } | |
632546c4 | 3535 | io_size = iov_iter_count(iter); |
fa15bafb | 3536 | req->result = io_size; |
227c0c96 | 3537 | ret = 0; |
2b188cc1 | 3538 | |
fd6c2e4c JA |
3539 | /* Ensure we clear previously set non-block flag */ |
3540 | if (!force_nonblock) | |
29de5f6a | 3541 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3542 | else |
3543 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3544 | ||
fd6c2e4c | 3545 | |
24c74678 | 3546 | /* If the file doesn't support async, just async punt */ |
f5cac8b1 JA |
3547 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
3548 | if (no_async) | |
f67676d1 | 3549 | goto copy_iov; |
9e645e11 | 3550 | |
632546c4 | 3551 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3552 | if (unlikely(ret)) |
3553 | goto out_free; | |
2b188cc1 | 3554 | |
227c0c96 | 3555 | ret = io_iter_do_read(req, iter); |
32960613 | 3556 | |
227c0c96 JA |
3557 | if (!ret) { |
3558 | goto done; | |
3559 | } else if (ret == -EIOCBQUEUED) { | |
3560 | ret = 0; | |
3561 | goto out_free; | |
3562 | } else if (ret == -EAGAIN) { | |
eefdf30f JA |
3563 | /* IOPOLL retry should happen for io-wq threads */ |
3564 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3565 | goto done; |
355afaeb JA |
3566 | /* no retry on NONBLOCK marked file */ |
3567 | if (req->file->f_flags & O_NONBLOCK) | |
3568 | goto done; | |
84216315 | 3569 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3570 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a JA |
3571 | ret = 0; |
3572 | goto copy_iov; | |
227c0c96 | 3573 | } else if (ret < 0) { |
00d23d51 JA |
3574 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
3575 | goto done; | |
227c0c96 JA |
3576 | } |
3577 | ||
3578 | /* read it all, or we did blocking attempt. no retry. */ | |
f91daf56 | 3579 | if (!iov_iter_count(iter) || !force_nonblock || |
9a173346 | 3580 | (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG)) |
227c0c96 JA |
3581 | goto done; |
3582 | ||
3583 | io_size -= ret; | |
3584 | copy_iov: | |
3585 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
3586 | if (ret2) { | |
3587 | ret = ret2; | |
3588 | goto out_free; | |
3589 | } | |
f5cac8b1 JA |
3590 | if (no_async) |
3591 | return -EAGAIN; | |
e8c2bc1f | 3592 | rw = req->async_data; |
227c0c96 JA |
3593 | /* it's copied and will be cleaned with ->io */ |
3594 | iovec = NULL; | |
3595 | /* now use our persistent iterator, if we aren't already */ | |
e8c2bc1f | 3596 | iter = &rw->iter; |
227c0c96 | 3597 | retry: |
e8c2bc1f | 3598 | rw->bytes_done += ret; |
227c0c96 JA |
3599 | /* if we can retry, do so with the callbacks armed */ |
3600 | if (!io_rw_should_retry(req)) { | |
fa15bafb PB |
3601 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3602 | return -EAGAIN; | |
2b188cc1 | 3603 | } |
227c0c96 JA |
3604 | |
3605 | /* | |
3606 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we | |
3607 | * get -EIOCBQUEUED, then we'll get a notification when the desired | |
3608 | * page gets unlocked. We can also get a partial read here, and if we | |
3609 | * do, then just retry at the new offset. | |
3610 | */ | |
3611 | ret = io_iter_do_read(req, iter); | |
3612 | if (ret == -EIOCBQUEUED) { | |
3613 | ret = 0; | |
3614 | goto out_free; | |
3615 | } else if (ret > 0 && ret < io_size) { | |
3616 | /* we got some bytes, but not all. retry. */ | |
3617 | goto retry; | |
3618 | } | |
3619 | done: | |
3620 | kiocb_done(kiocb, ret, cs); | |
3621 | ret = 0; | |
f67676d1 | 3622 | out_free: |
f261c168 | 3623 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3624 | if (iovec) |
6f2cc166 | 3625 | kfree(iovec); |
2b188cc1 JA |
3626 | return ret; |
3627 | } | |
3628 | ||
73debe68 | 3629 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3630 | { |
3631 | ssize_t ret; | |
3632 | ||
a88fc400 | 3633 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3634 | if (ret) |
3635 | return ret; | |
f67676d1 | 3636 | |
3529d8c2 JA |
3637 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3638 | return -EBADF; | |
f67676d1 | 3639 | |
5f798bea | 3640 | /* either don't need iovec imported or already have it */ |
2d199895 | 3641 | if (!req->async_data) |
3529d8c2 | 3642 | return 0; |
73debe68 | 3643 | return io_rw_prep_async(req, WRITE); |
f67676d1 JA |
3644 | } |
3645 | ||
a1d7c393 JA |
3646 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3647 | struct io_comp_state *cs) | |
2b188cc1 JA |
3648 | { |
3649 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3650 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3651 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3652 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3653 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3654 | |
2846c481 | 3655 | if (rw) { |
e8c2bc1f | 3656 | iter = &rw->iter; |
2846c481 PB |
3657 | iovec = NULL; |
3658 | } else { | |
3659 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3660 | if (ret < 0) | |
3661 | return ret; | |
3662 | } | |
632546c4 | 3663 | io_size = iov_iter_count(iter); |
fa15bafb | 3664 | req->result = io_size; |
2b188cc1 | 3665 | |
fd6c2e4c JA |
3666 | /* Ensure we clear previously set non-block flag */ |
3667 | if (!force_nonblock) | |
a88fc400 PB |
3668 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3669 | else | |
3670 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3671 | |
24c74678 | 3672 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3673 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3674 | goto copy_iov; |
31b51510 | 3675 | |
10d59345 JA |
3676 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3677 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3678 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3679 | goto copy_iov; |
31b51510 | 3680 | |
632546c4 | 3681 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3682 | if (unlikely(ret)) |
3683 | goto out_free; | |
4ed734b0 | 3684 | |
fa15bafb PB |
3685 | /* |
3686 | * Open-code file_start_write here to grab freeze protection, | |
3687 | * which will be released by another thread in | |
3688 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3689 | * released so that it doesn't complain about the held lock when | |
3690 | * we return to userspace. | |
3691 | */ | |
3692 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3693 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3694 | __sb_writers_release(file_inode(req->file)->i_sb, |
3695 | SB_FREEZE_WRITE); | |
3696 | } | |
3697 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3698 | |
fa15bafb | 3699 | if (req->file->f_op->write_iter) |
ff6165b2 | 3700 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3701 | else if (req->file->f_op->write) |
4017eb91 | 3702 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3703 | else |
3704 | ret2 = -EINVAL; | |
4ed734b0 | 3705 | |
fa15bafb PB |
3706 | /* |
3707 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3708 | * retry them without IOCB_NOWAIT. | |
3709 | */ | |
3710 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3711 | ret2 = -EAGAIN; | |
355afaeb JA |
3712 | /* no retry on NONBLOCK marked file */ |
3713 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) | |
3714 | goto done; | |
fa15bafb | 3715 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3716 | /* IOPOLL retry should happen for io-wq threads */ |
3717 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3718 | goto copy_iov; | |
355afaeb | 3719 | done: |
fa15bafb PB |
3720 | kiocb_done(kiocb, ret2, cs); |
3721 | } else { | |
f67676d1 | 3722 | copy_iov: |
84216315 | 3723 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3724 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3725 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
ff6165b2 JA |
3726 | if (!ret) |
3727 | return -EAGAIN; | |
2b188cc1 | 3728 | } |
31b51510 | 3729 | out_free: |
f261c168 | 3730 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3731 | if (iovec) |
6f2cc166 | 3732 | kfree(iovec); |
2b188cc1 JA |
3733 | return ret; |
3734 | } | |
3735 | ||
80a261fd JA |
3736 | static int io_renameat_prep(struct io_kiocb *req, |
3737 | const struct io_uring_sqe *sqe) | |
3738 | { | |
3739 | struct io_rename *ren = &req->rename; | |
3740 | const char __user *oldf, *newf; | |
3741 | ||
3742 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3743 | return -EBADF; | |
3744 | ||
3745 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3746 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3747 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3748 | ren->new_dfd = READ_ONCE(sqe->len); | |
3749 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3750 | ||
3751 | ren->oldpath = getname(oldf); | |
3752 | if (IS_ERR(ren->oldpath)) | |
3753 | return PTR_ERR(ren->oldpath); | |
3754 | ||
3755 | ren->newpath = getname(newf); | |
3756 | if (IS_ERR(ren->newpath)) { | |
3757 | putname(ren->oldpath); | |
3758 | return PTR_ERR(ren->newpath); | |
3759 | } | |
3760 | ||
3761 | req->flags |= REQ_F_NEED_CLEANUP; | |
3762 | return 0; | |
3763 | } | |
3764 | ||
3765 | static int io_renameat(struct io_kiocb *req, bool force_nonblock) | |
3766 | { | |
3767 | struct io_rename *ren = &req->rename; | |
3768 | int ret; | |
3769 | ||
3770 | if (force_nonblock) | |
3771 | return -EAGAIN; | |
3772 | ||
3773 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3774 | ren->newpath, ren->flags); | |
3775 | ||
3776 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3777 | if (ret < 0) | |
3778 | req_set_fail_links(req); | |
3779 | io_req_complete(req, ret); | |
3780 | return 0; | |
3781 | } | |
3782 | ||
14a1143b JA |
3783 | static int io_unlinkat_prep(struct io_kiocb *req, |
3784 | const struct io_uring_sqe *sqe) | |
3785 | { | |
3786 | struct io_unlink *un = &req->unlink; | |
3787 | const char __user *fname; | |
3788 | ||
3789 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3790 | return -EBADF; | |
3791 | ||
3792 | un->dfd = READ_ONCE(sqe->fd); | |
3793 | ||
3794 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3795 | if (un->flags & ~AT_REMOVEDIR) | |
3796 | return -EINVAL; | |
3797 | ||
3798 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3799 | un->filename = getname(fname); | |
3800 | if (IS_ERR(un->filename)) | |
3801 | return PTR_ERR(un->filename); | |
3802 | ||
3803 | req->flags |= REQ_F_NEED_CLEANUP; | |
3804 | return 0; | |
3805 | } | |
3806 | ||
3807 | static int io_unlinkat(struct io_kiocb *req, bool force_nonblock) | |
3808 | { | |
3809 | struct io_unlink *un = &req->unlink; | |
3810 | int ret; | |
3811 | ||
3812 | if (force_nonblock) | |
3813 | return -EAGAIN; | |
3814 | ||
3815 | if (un->flags & AT_REMOVEDIR) | |
3816 | ret = do_rmdir(un->dfd, un->filename); | |
3817 | else | |
3818 | ret = do_unlinkat(un->dfd, un->filename); | |
3819 | ||
3820 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3821 | if (ret < 0) | |
3822 | req_set_fail_links(req); | |
3823 | io_req_complete(req, ret); | |
3824 | return 0; | |
3825 | } | |
3826 | ||
36f4fa68 JA |
3827 | static int io_shutdown_prep(struct io_kiocb *req, |
3828 | const struct io_uring_sqe *sqe) | |
3829 | { | |
3830 | #if defined(CONFIG_NET) | |
3831 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3832 | return -EINVAL; | |
3833 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3834 | sqe->buf_index) | |
3835 | return -EINVAL; | |
3836 | ||
3837 | req->shutdown.how = READ_ONCE(sqe->len); | |
3838 | return 0; | |
3839 | #else | |
3840 | return -EOPNOTSUPP; | |
3841 | #endif | |
3842 | } | |
3843 | ||
3844 | static int io_shutdown(struct io_kiocb *req, bool force_nonblock) | |
3845 | { | |
3846 | #if defined(CONFIG_NET) | |
3847 | struct socket *sock; | |
3848 | int ret; | |
3849 | ||
3850 | if (force_nonblock) | |
3851 | return -EAGAIN; | |
3852 | ||
48aba79b | 3853 | sock = sock_from_file(req->file); |
36f4fa68 | 3854 | if (unlikely(!sock)) |
48aba79b | 3855 | return -ENOTSOCK; |
36f4fa68 JA |
3856 | |
3857 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3858 | if (ret < 0) |
3859 | req_set_fail_links(req); | |
36f4fa68 JA |
3860 | io_req_complete(req, ret); |
3861 | return 0; | |
3862 | #else | |
3863 | return -EOPNOTSUPP; | |
3864 | #endif | |
3865 | } | |
3866 | ||
f2a8d5c7 PB |
3867 | static int __io_splice_prep(struct io_kiocb *req, |
3868 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3869 | { |
3870 | struct io_splice* sp = &req->splice; | |
3871 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3872 | |
3232dd02 PB |
3873 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3874 | return -EINVAL; | |
7d67af2c PB |
3875 | |
3876 | sp->file_in = NULL; | |
7d67af2c PB |
3877 | sp->len = READ_ONCE(sqe->len); |
3878 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3879 | ||
3880 | if (unlikely(sp->flags & ~valid_flags)) | |
3881 | return -EINVAL; | |
3882 | ||
8371adf5 PB |
3883 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3884 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3885 | if (!sp->file_in) | |
3886 | return -EBADF; | |
7d67af2c PB |
3887 | req->flags |= REQ_F_NEED_CLEANUP; |
3888 | ||
7cdaf587 XW |
3889 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3890 | /* | |
3891 | * Splice operation will be punted aync, and here need to | |
3892 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3893 | */ | |
3894 | io_req_init_async(req); | |
7d67af2c | 3895 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3896 | } |
7d67af2c PB |
3897 | |
3898 | return 0; | |
3899 | } | |
3900 | ||
f2a8d5c7 PB |
3901 | static int io_tee_prep(struct io_kiocb *req, |
3902 | const struct io_uring_sqe *sqe) | |
3903 | { | |
3904 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3905 | return -EINVAL; | |
3906 | return __io_splice_prep(req, sqe); | |
3907 | } | |
3908 | ||
3909 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3910 | { | |
3911 | struct io_splice *sp = &req->splice; | |
3912 | struct file *in = sp->file_in; | |
3913 | struct file *out = sp->file_out; | |
3914 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3915 | long ret = 0; | |
3916 | ||
3917 | if (force_nonblock) | |
3918 | return -EAGAIN; | |
3919 | if (sp->len) | |
3920 | ret = do_tee(in, out, sp->len, flags); | |
3921 | ||
3922 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3923 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3924 | ||
f2a8d5c7 PB |
3925 | if (ret != sp->len) |
3926 | req_set_fail_links(req); | |
e1e16097 | 3927 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3928 | return 0; |
3929 | } | |
3930 | ||
3931 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3932 | { | |
3933 | struct io_splice* sp = &req->splice; | |
3934 | ||
3935 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3936 | sp->off_out = READ_ONCE(sqe->off); | |
3937 | return __io_splice_prep(req, sqe); | |
3938 | } | |
3939 | ||
014db007 | 3940 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3941 | { |
3942 | struct io_splice *sp = &req->splice; | |
3943 | struct file *in = sp->file_in; | |
3944 | struct file *out = sp->file_out; | |
3945 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3946 | loff_t *poff_in, *poff_out; | |
c9687426 | 3947 | long ret = 0; |
7d67af2c | 3948 | |
2fb3e822 PB |
3949 | if (force_nonblock) |
3950 | return -EAGAIN; | |
7d67af2c PB |
3951 | |
3952 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3953 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3954 | |
948a7749 | 3955 | if (sp->len) |
c9687426 | 3956 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3957 | |
3958 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3959 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3960 | ||
7d67af2c PB |
3961 | if (ret != sp->len) |
3962 | req_set_fail_links(req); | |
e1e16097 | 3963 | io_req_complete(req, ret); |
7d67af2c PB |
3964 | return 0; |
3965 | } | |
3966 | ||
2b188cc1 JA |
3967 | /* |
3968 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3969 | */ | |
229a7b63 | 3970 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3971 | { |
3972 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3973 | |
def596e9 JA |
3974 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3975 | return -EINVAL; | |
3976 | ||
229a7b63 | 3977 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3978 | return 0; |
3979 | } | |
3980 | ||
3529d8c2 | 3981 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3982 | { |
6b06314c | 3983 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3984 | |
09bb8394 JA |
3985 | if (!req->file) |
3986 | return -EBADF; | |
c992fe29 | 3987 | |
6b06314c | 3988 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3989 | return -EINVAL; |
edafccee | 3990 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3991 | return -EINVAL; |
3992 | ||
8ed8d3c3 JA |
3993 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3994 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3995 | return -EINVAL; | |
3996 | ||
3997 | req->sync.off = READ_ONCE(sqe->off); | |
3998 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3999 | return 0; |
4000 | } | |
4001 | ||
ac45abc0 | 4002 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4003 | { |
8ed8d3c3 | 4004 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4005 | int ret; |
4006 | ||
ac45abc0 PB |
4007 | /* fsync always requires a blocking context */ |
4008 | if (force_nonblock) | |
4009 | return -EAGAIN; | |
4010 | ||
9adbd45d | 4011 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4012 | end > 0 ? end : LLONG_MAX, |
4013 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4014 | if (ret < 0) | |
4015 | req_set_fail_links(req); | |
e1e16097 | 4016 | io_req_complete(req, ret); |
c992fe29 CH |
4017 | return 0; |
4018 | } | |
4019 | ||
d63d1b5e JA |
4020 | static int io_fallocate_prep(struct io_kiocb *req, |
4021 | const struct io_uring_sqe *sqe) | |
4022 | { | |
4023 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
4024 | return -EINVAL; | |
3232dd02 PB |
4025 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4026 | return -EINVAL; | |
d63d1b5e JA |
4027 | |
4028 | req->sync.off = READ_ONCE(sqe->off); | |
4029 | req->sync.len = READ_ONCE(sqe->addr); | |
4030 | req->sync.mode = READ_ONCE(sqe->len); | |
4031 | return 0; | |
4032 | } | |
4033 | ||
014db007 | 4034 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 4035 | { |
ac45abc0 PB |
4036 | int ret; |
4037 | ||
d63d1b5e | 4038 | /* fallocate always requiring blocking context */ |
ac45abc0 | 4039 | if (force_nonblock) |
5d17b4a4 | 4040 | return -EAGAIN; |
ac45abc0 PB |
4041 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4042 | req->sync.len); | |
ac45abc0 PB |
4043 | if (ret < 0) |
4044 | req_set_fail_links(req); | |
e1e16097 | 4045 | io_req_complete(req, ret); |
5d17b4a4 JA |
4046 | return 0; |
4047 | } | |
4048 | ||
ec65fea5 | 4049 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4050 | { |
f8748881 | 4051 | const char __user *fname; |
15b71abe | 4052 | int ret; |
b7bb4f7d | 4053 | |
ec65fea5 | 4054 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4055 | return -EINVAL; |
ec65fea5 | 4056 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4057 | return -EBADF; |
03b1230c | 4058 | |
ec65fea5 PB |
4059 | /* open.how should be already initialised */ |
4060 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4061 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4062 | |
25e72d10 PB |
4063 | req->open.dfd = READ_ONCE(sqe->fd); |
4064 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4065 | req->open.filename = getname(fname); |
15b71abe JA |
4066 | if (IS_ERR(req->open.filename)) { |
4067 | ret = PTR_ERR(req->open.filename); | |
4068 | req->open.filename = NULL; | |
4069 | return ret; | |
4070 | } | |
4022e7af | 4071 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4072 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4073 | return 0; |
03b1230c JA |
4074 | } |
4075 | ||
ec65fea5 PB |
4076 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4077 | { | |
4078 | u64 flags, mode; | |
4079 | ||
14587a46 | 4080 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4081 | return -EINVAL; |
ec65fea5 PB |
4082 | mode = READ_ONCE(sqe->len); |
4083 | flags = READ_ONCE(sqe->open_flags); | |
4084 | req->open.how = build_open_how(flags, mode); | |
4085 | return __io_openat_prep(req, sqe); | |
4086 | } | |
4087 | ||
cebdb986 | 4088 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4089 | { |
cebdb986 | 4090 | struct open_how __user *how; |
cebdb986 | 4091 | size_t len; |
0fa03c62 JA |
4092 | int ret; |
4093 | ||
14587a46 | 4094 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4095 | return -EINVAL; |
cebdb986 JA |
4096 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4097 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4098 | if (len < OPEN_HOW_SIZE_VER0) |
4099 | return -EINVAL; | |
3529d8c2 | 4100 | |
cebdb986 JA |
4101 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4102 | len); | |
4103 | if (ret) | |
4104 | return ret; | |
3529d8c2 | 4105 | |
ec65fea5 | 4106 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4107 | } |
4108 | ||
014db007 | 4109 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
4110 | { |
4111 | struct open_flags op; | |
15b71abe | 4112 | struct file *file; |
3a81fd02 JA |
4113 | bool nonblock_set; |
4114 | bool resolve_nonblock; | |
15b71abe JA |
4115 | int ret; |
4116 | ||
cebdb986 | 4117 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4118 | if (ret) |
4119 | goto err; | |
3a81fd02 JA |
4120 | nonblock_set = op.open_flag & O_NONBLOCK; |
4121 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
4122 | if (force_nonblock) { | |
4123 | /* | |
4124 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4125 | * it'll always -EAGAIN | |
4126 | */ | |
4127 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4128 | return -EAGAIN; | |
4129 | op.lookup_flags |= LOOKUP_CACHED; | |
4130 | op.open_flag |= O_NONBLOCK; | |
4131 | } | |
15b71abe | 4132 | |
4022e7af | 4133 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4134 | if (ret < 0) |
4135 | goto err; | |
4136 | ||
4137 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 JA |
4138 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4139 | if ((!resolve_nonblock && force_nonblock) && file == ERR_PTR(-EAGAIN)) { | |
4140 | /* | |
4141 | * We could hang on to this 'fd', but seems like marginal | |
4142 | * gain for something that is now known to be a slower path. | |
4143 | * So just put it, and we'll get a new one when we retry. | |
4144 | */ | |
4145 | put_unused_fd(ret); | |
4146 | return -EAGAIN; | |
4147 | } | |
4148 | ||
15b71abe JA |
4149 | if (IS_ERR(file)) { |
4150 | put_unused_fd(ret); | |
4151 | ret = PTR_ERR(file); | |
4152 | } else { | |
3a81fd02 JA |
4153 | if (force_nonblock && !nonblock_set) |
4154 | file->f_flags &= ~O_NONBLOCK; | |
15b71abe JA |
4155 | fsnotify_open(file); |
4156 | fd_install(ret, file); | |
4157 | } | |
4158 | err: | |
4159 | putname(req->open.filename); | |
8fef80bf | 4160 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4161 | if (ret < 0) |
4162 | req_set_fail_links(req); | |
e1e16097 | 4163 | io_req_complete(req, ret); |
15b71abe JA |
4164 | return 0; |
4165 | } | |
4166 | ||
014db007 | 4167 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 4168 | { |
014db007 | 4169 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
4170 | } |
4171 | ||
067524e9 JA |
4172 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4173 | const struct io_uring_sqe *sqe) | |
4174 | { | |
4175 | struct io_provide_buf *p = &req->pbuf; | |
4176 | u64 tmp; | |
4177 | ||
4178 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4179 | return -EINVAL; | |
4180 | ||
4181 | tmp = READ_ONCE(sqe->fd); | |
4182 | if (!tmp || tmp > USHRT_MAX) | |
4183 | return -EINVAL; | |
4184 | ||
4185 | memset(p, 0, sizeof(*p)); | |
4186 | p->nbufs = tmp; | |
4187 | p->bgid = READ_ONCE(sqe->buf_group); | |
4188 | return 0; | |
4189 | } | |
4190 | ||
4191 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4192 | int bgid, unsigned nbufs) | |
4193 | { | |
4194 | unsigned i = 0; | |
4195 | ||
4196 | /* shouldn't happen */ | |
4197 | if (!nbufs) | |
4198 | return 0; | |
4199 | ||
4200 | /* the head kbuf is the list itself */ | |
4201 | while (!list_empty(&buf->list)) { | |
4202 | struct io_buffer *nxt; | |
4203 | ||
4204 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4205 | list_del(&nxt->list); | |
4206 | kfree(nxt); | |
4207 | if (++i == nbufs) | |
4208 | return i; | |
4209 | } | |
4210 | i++; | |
4211 | kfree(buf); | |
4212 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4213 | ||
4214 | return i; | |
4215 | } | |
4216 | ||
229a7b63 JA |
4217 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
4218 | struct io_comp_state *cs) | |
067524e9 JA |
4219 | { |
4220 | struct io_provide_buf *p = &req->pbuf; | |
4221 | struct io_ring_ctx *ctx = req->ctx; | |
4222 | struct io_buffer *head; | |
4223 | int ret = 0; | |
4224 | ||
4225 | io_ring_submit_lock(ctx, !force_nonblock); | |
4226 | ||
4227 | lockdep_assert_held(&ctx->uring_lock); | |
4228 | ||
4229 | ret = -ENOENT; | |
4230 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4231 | if (head) | |
4232 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4233 | if (ret < 0) |
4234 | req_set_fail_links(req); | |
067524e9 | 4235 | |
31bff9a5 PB |
4236 | /* need to hold the lock to complete IOPOLL requests */ |
4237 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4238 | __io_req_complete(req, ret, 0, cs); | |
4239 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4240 | } else { | |
4241 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4242 | __io_req_complete(req, ret, 0, cs); | |
4243 | } | |
067524e9 JA |
4244 | return 0; |
4245 | } | |
4246 | ||
ddf0322d JA |
4247 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4248 | const struct io_uring_sqe *sqe) | |
4249 | { | |
4250 | struct io_provide_buf *p = &req->pbuf; | |
4251 | u64 tmp; | |
4252 | ||
4253 | if (sqe->ioprio || sqe->rw_flags) | |
4254 | return -EINVAL; | |
4255 | ||
4256 | tmp = READ_ONCE(sqe->fd); | |
4257 | if (!tmp || tmp > USHRT_MAX) | |
4258 | return -E2BIG; | |
4259 | p->nbufs = tmp; | |
4260 | p->addr = READ_ONCE(sqe->addr); | |
4261 | p->len = READ_ONCE(sqe->len); | |
4262 | ||
efe68c1c | 4263 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4264 | return -EFAULT; |
4265 | ||
4266 | p->bgid = READ_ONCE(sqe->buf_group); | |
4267 | tmp = READ_ONCE(sqe->off); | |
4268 | if (tmp > USHRT_MAX) | |
4269 | return -E2BIG; | |
4270 | p->bid = tmp; | |
4271 | return 0; | |
4272 | } | |
4273 | ||
4274 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4275 | { | |
4276 | struct io_buffer *buf; | |
4277 | u64 addr = pbuf->addr; | |
4278 | int i, bid = pbuf->bid; | |
4279 | ||
4280 | for (i = 0; i < pbuf->nbufs; i++) { | |
4281 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4282 | if (!buf) | |
4283 | break; | |
4284 | ||
4285 | buf->addr = addr; | |
4286 | buf->len = pbuf->len; | |
4287 | buf->bid = bid; | |
4288 | addr += pbuf->len; | |
4289 | bid++; | |
4290 | if (!*head) { | |
4291 | INIT_LIST_HEAD(&buf->list); | |
4292 | *head = buf; | |
4293 | } else { | |
4294 | list_add_tail(&buf->list, &(*head)->list); | |
4295 | } | |
4296 | } | |
4297 | ||
4298 | return i ? i : -ENOMEM; | |
4299 | } | |
4300 | ||
229a7b63 JA |
4301 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
4302 | struct io_comp_state *cs) | |
ddf0322d JA |
4303 | { |
4304 | struct io_provide_buf *p = &req->pbuf; | |
4305 | struct io_ring_ctx *ctx = req->ctx; | |
4306 | struct io_buffer *head, *list; | |
4307 | int ret = 0; | |
4308 | ||
4309 | io_ring_submit_lock(ctx, !force_nonblock); | |
4310 | ||
4311 | lockdep_assert_held(&ctx->uring_lock); | |
4312 | ||
4313 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4314 | ||
4315 | ret = io_add_buffers(p, &head); | |
4316 | if (ret < 0) | |
4317 | goto out; | |
4318 | ||
4319 | if (!list) { | |
4320 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4321 | GFP_KERNEL); | |
4322 | if (ret < 0) { | |
067524e9 | 4323 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4324 | goto out; |
4325 | } | |
4326 | } | |
4327 | out: | |
ddf0322d JA |
4328 | if (ret < 0) |
4329 | req_set_fail_links(req); | |
31bff9a5 PB |
4330 | |
4331 | /* need to hold the lock to complete IOPOLL requests */ | |
4332 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4333 | __io_req_complete(req, ret, 0, cs); | |
4334 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4335 | } else { | |
4336 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4337 | __io_req_complete(req, ret, 0, cs); | |
4338 | } | |
ddf0322d | 4339 | return 0; |
cebdb986 JA |
4340 | } |
4341 | ||
3e4827b0 JA |
4342 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4343 | const struct io_uring_sqe *sqe) | |
4344 | { | |
4345 | #if defined(CONFIG_EPOLL) | |
4346 | if (sqe->ioprio || sqe->buf_index) | |
4347 | return -EINVAL; | |
6ca56f84 | 4348 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4349 | return -EINVAL; |
3e4827b0 JA |
4350 | |
4351 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4352 | req->epoll.op = READ_ONCE(sqe->len); | |
4353 | req->epoll.fd = READ_ONCE(sqe->off); | |
4354 | ||
4355 | if (ep_op_has_event(req->epoll.op)) { | |
4356 | struct epoll_event __user *ev; | |
4357 | ||
4358 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4359 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4360 | return -EFAULT; | |
4361 | } | |
4362 | ||
4363 | return 0; | |
4364 | #else | |
4365 | return -EOPNOTSUPP; | |
4366 | #endif | |
4367 | } | |
4368 | ||
229a7b63 JA |
4369 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
4370 | struct io_comp_state *cs) | |
3e4827b0 JA |
4371 | { |
4372 | #if defined(CONFIG_EPOLL) | |
4373 | struct io_epoll *ie = &req->epoll; | |
4374 | int ret; | |
4375 | ||
4376 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4377 | if (force_nonblock && ret == -EAGAIN) | |
4378 | return -EAGAIN; | |
4379 | ||
4380 | if (ret < 0) | |
4381 | req_set_fail_links(req); | |
229a7b63 | 4382 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
4383 | return 0; |
4384 | #else | |
4385 | return -EOPNOTSUPP; | |
4386 | #endif | |
4387 | } | |
4388 | ||
c1ca757b JA |
4389 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4390 | { | |
4391 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4392 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4393 | return -EINVAL; | |
3232dd02 PB |
4394 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4395 | return -EINVAL; | |
c1ca757b JA |
4396 | |
4397 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4398 | req->madvise.len = READ_ONCE(sqe->len); | |
4399 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4400 | return 0; | |
4401 | #else | |
4402 | return -EOPNOTSUPP; | |
4403 | #endif | |
4404 | } | |
4405 | ||
014db007 | 4406 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
4407 | { |
4408 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4409 | struct io_madvise *ma = &req->madvise; | |
4410 | int ret; | |
4411 | ||
4412 | if (force_nonblock) | |
4413 | return -EAGAIN; | |
4414 | ||
0726b01e | 4415 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4416 | if (ret < 0) |
4417 | req_set_fail_links(req); | |
e1e16097 | 4418 | io_req_complete(req, ret); |
c1ca757b JA |
4419 | return 0; |
4420 | #else | |
4421 | return -EOPNOTSUPP; | |
4422 | #endif | |
4423 | } | |
4424 | ||
4840e418 JA |
4425 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4426 | { | |
4427 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4428 | return -EINVAL; | |
3232dd02 PB |
4429 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4430 | return -EINVAL; | |
4840e418 JA |
4431 | |
4432 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4433 | req->fadvise.len = READ_ONCE(sqe->len); | |
4434 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4435 | return 0; | |
4436 | } | |
4437 | ||
014db007 | 4438 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
4439 | { |
4440 | struct io_fadvise *fa = &req->fadvise; | |
4441 | int ret; | |
4442 | ||
3e69426d JA |
4443 | if (force_nonblock) { |
4444 | switch (fa->advice) { | |
4445 | case POSIX_FADV_NORMAL: | |
4446 | case POSIX_FADV_RANDOM: | |
4447 | case POSIX_FADV_SEQUENTIAL: | |
4448 | break; | |
4449 | default: | |
4450 | return -EAGAIN; | |
4451 | } | |
4452 | } | |
4840e418 JA |
4453 | |
4454 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4455 | if (ret < 0) | |
4456 | req_set_fail_links(req); | |
e1e16097 | 4457 | io_req_complete(req, ret); |
4840e418 JA |
4458 | return 0; |
4459 | } | |
4460 | ||
eddc7ef5 JA |
4461 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4462 | { | |
6ca56f84 | 4463 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4464 | return -EINVAL; |
eddc7ef5 JA |
4465 | if (sqe->ioprio || sqe->buf_index) |
4466 | return -EINVAL; | |
9c280f90 | 4467 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4468 | return -EBADF; |
eddc7ef5 | 4469 | |
1d9e1288 BM |
4470 | req->statx.dfd = READ_ONCE(sqe->fd); |
4471 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4472 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4473 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4474 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4475 | |
4476 | return 0; | |
4477 | } | |
4478 | ||
014db007 | 4479 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 4480 | { |
1d9e1288 | 4481 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4482 | int ret; |
4483 | ||
5b0bbee4 JA |
4484 | if (force_nonblock) { |
4485 | /* only need file table for an actual valid fd */ | |
4486 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4487 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4488 | return -EAGAIN; |
5b0bbee4 | 4489 | } |
eddc7ef5 | 4490 | |
e62753e4 BM |
4491 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4492 | ctx->buffer); | |
eddc7ef5 | 4493 | |
eddc7ef5 JA |
4494 | if (ret < 0) |
4495 | req_set_fail_links(req); | |
e1e16097 | 4496 | io_req_complete(req, ret); |
eddc7ef5 JA |
4497 | return 0; |
4498 | } | |
4499 | ||
b5dba59e JA |
4500 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4501 | { | |
4502 | /* | |
4503 | * If we queue this for async, it must not be cancellable. That would | |
7cdaf587 XW |
4504 | * leave the 'file' in an undeterminate state, and here need to modify |
4505 | * io_wq_work.flags, so initialize io_wq_work firstly. | |
b5dba59e | 4506 | */ |
7cdaf587 | 4507 | io_req_init_async(req); |
b5dba59e | 4508 | |
14587a46 | 4509 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4510 | return -EINVAL; |
b5dba59e JA |
4511 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4512 | sqe->rw_flags || sqe->buf_index) | |
4513 | return -EINVAL; | |
9c280f90 | 4514 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4515 | return -EBADF; |
b5dba59e JA |
4516 | |
4517 | req->close.fd = READ_ONCE(sqe->fd); | |
0f212204 | 4518 | if ((req->file && req->file->f_op == &io_uring_fops)) |
fd2206e4 | 4519 | return -EBADF; |
b5dba59e | 4520 | |
3af73b28 | 4521 | req->close.put_file = NULL; |
b5dba59e | 4522 | return 0; |
b5dba59e JA |
4523 | } |
4524 | ||
229a7b63 JA |
4525 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
4526 | struct io_comp_state *cs) | |
b5dba59e | 4527 | { |
3af73b28 | 4528 | struct io_close *close = &req->close; |
b5dba59e JA |
4529 | int ret; |
4530 | ||
3af73b28 PB |
4531 | /* might be already done during nonblock submission */ |
4532 | if (!close->put_file) { | |
9fe83c43 | 4533 | ret = close_fd_get_file(close->fd, &close->put_file); |
3af73b28 PB |
4534 | if (ret < 0) |
4535 | return (ret == -ENOENT) ? -EBADF : ret; | |
4536 | } | |
b5dba59e JA |
4537 | |
4538 | /* if the file has a flush method, be safe and punt to async */ | |
3af73b28 | 4539 | if (close->put_file->f_op->flush && force_nonblock) { |
607ec89e JA |
4540 | /* not safe to cancel at this point */ |
4541 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; | |
24c74678 PB |
4542 | /* was never set, but play safe */ |
4543 | req->flags &= ~REQ_F_NOWAIT; | |
0bf0eefd | 4544 | /* avoid grabbing files - we don't need the files */ |
24c74678 | 4545 | req->flags |= REQ_F_NO_FILE_TABLE; |
0bf0eefd | 4546 | return -EAGAIN; |
a2100672 | 4547 | } |
b5dba59e | 4548 | |
3af73b28 | 4549 | /* No ->flush() or already async, safely close from here */ |
98447d65 | 4550 | ret = filp_close(close->put_file, req->work.identity->files); |
3af73b28 PB |
4551 | if (ret < 0) |
4552 | req_set_fail_links(req); | |
3af73b28 PB |
4553 | fput(close->put_file); |
4554 | close->put_file = NULL; | |
229a7b63 | 4555 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 4556 | return 0; |
b5dba59e JA |
4557 | } |
4558 | ||
3529d8c2 | 4559 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4560 | { |
4561 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4562 | |
4563 | if (!req->file) | |
4564 | return -EBADF; | |
5d17b4a4 JA |
4565 | |
4566 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4567 | return -EINVAL; | |
4568 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4569 | return -EINVAL; | |
4570 | ||
8ed8d3c3 JA |
4571 | req->sync.off = READ_ONCE(sqe->off); |
4572 | req->sync.len = READ_ONCE(sqe->len); | |
4573 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4574 | return 0; |
4575 | } | |
4576 | ||
ac45abc0 | 4577 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4578 | { |
8ed8d3c3 JA |
4579 | int ret; |
4580 | ||
ac45abc0 PB |
4581 | /* sync_file_range always requires a blocking context */ |
4582 | if (force_nonblock) | |
4583 | return -EAGAIN; | |
4584 | ||
9adbd45d | 4585 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4586 | req->sync.flags); |
4587 | if (ret < 0) | |
4588 | req_set_fail_links(req); | |
e1e16097 | 4589 | io_req_complete(req, ret); |
5d17b4a4 JA |
4590 | return 0; |
4591 | } | |
4592 | ||
469956e8 | 4593 | #if defined(CONFIG_NET) |
02d27d89 PB |
4594 | static int io_setup_async_msg(struct io_kiocb *req, |
4595 | struct io_async_msghdr *kmsg) | |
4596 | { | |
e8c2bc1f JA |
4597 | struct io_async_msghdr *async_msg = req->async_data; |
4598 | ||
4599 | if (async_msg) | |
02d27d89 | 4600 | return -EAGAIN; |
e8c2bc1f | 4601 | if (io_alloc_async_data(req)) { |
02d27d89 PB |
4602 | if (kmsg->iov != kmsg->fast_iov) |
4603 | kfree(kmsg->iov); | |
4604 | return -ENOMEM; | |
4605 | } | |
e8c2bc1f | 4606 | async_msg = req->async_data; |
02d27d89 | 4607 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4608 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
02d27d89 PB |
4609 | return -EAGAIN; |
4610 | } | |
4611 | ||
2ae523ed PB |
4612 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4613 | struct io_async_msghdr *iomsg) | |
4614 | { | |
4615 | iomsg->iov = iomsg->fast_iov; | |
4616 | iomsg->msg.msg_name = &iomsg->addr; | |
4617 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4618 | req->sr_msg.msg_flags, &iomsg->iov); | |
4619 | } | |
4620 | ||
3529d8c2 | 4621 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4622 | { |
e8c2bc1f | 4623 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4624 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4625 | int ret; |
03b1230c | 4626 | |
d2b6f48b PB |
4627 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4628 | return -EINVAL; | |
4629 | ||
e47293fd | 4630 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4631 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4632 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4633 | |
d8768362 JA |
4634 | #ifdef CONFIG_COMPAT |
4635 | if (req->ctx->compat) | |
4636 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4637 | #endif | |
4638 | ||
e8c2bc1f | 4639 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4640 | return 0; |
e8c2bc1f | 4641 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4642 | if (!ret) |
4643 | req->flags |= REQ_F_NEED_CLEANUP; | |
4644 | return ret; | |
03b1230c JA |
4645 | } |
4646 | ||
229a7b63 JA |
4647 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4648 | struct io_comp_state *cs) | |
aa1fa28f | 4649 | { |
6b754c8b | 4650 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4651 | struct socket *sock; |
7a7cacba | 4652 | unsigned flags; |
0fa03c62 JA |
4653 | int ret; |
4654 | ||
dba4a925 | 4655 | sock = sock_from_file(req->file); |
7a7cacba | 4656 | if (unlikely(!sock)) |
dba4a925 | 4657 | return -ENOTSOCK; |
3529d8c2 | 4658 | |
e8c2bc1f JA |
4659 | if (req->async_data) { |
4660 | kmsg = req->async_data; | |
4661 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4662 | /* if iov is set, it's allocated already */ |
4663 | if (!kmsg->iov) | |
4664 | kmsg->iov = kmsg->fast_iov; | |
4665 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4666 | } else { | |
4667 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4668 | if (ret) | |
4669 | return ret; | |
4670 | kmsg = &iomsg; | |
0fa03c62 | 4671 | } |
0fa03c62 | 4672 | |
7a7cacba PB |
4673 | flags = req->sr_msg.msg_flags; |
4674 | if (flags & MSG_DONTWAIT) | |
4675 | req->flags |= REQ_F_NOWAIT; | |
4676 | else if (force_nonblock) | |
4677 | flags |= MSG_DONTWAIT; | |
e47293fd | 4678 | |
7a7cacba PB |
4679 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4680 | if (force_nonblock && ret == -EAGAIN) | |
4681 | return io_setup_async_msg(req, kmsg); | |
4682 | if (ret == -ERESTARTSYS) | |
4683 | ret = -EINTR; | |
0fa03c62 | 4684 | |
6b754c8b | 4685 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4686 | kfree(kmsg->iov); |
99bc4c38 | 4687 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4688 | if (ret < 0) |
4689 | req_set_fail_links(req); | |
229a7b63 | 4690 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4691 | return 0; |
03b1230c | 4692 | } |
aa1fa28f | 4693 | |
229a7b63 JA |
4694 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4695 | struct io_comp_state *cs) | |
fddaface | 4696 | { |
7a7cacba PB |
4697 | struct io_sr_msg *sr = &req->sr_msg; |
4698 | struct msghdr msg; | |
4699 | struct iovec iov; | |
fddaface | 4700 | struct socket *sock; |
7a7cacba | 4701 | unsigned flags; |
fddaface JA |
4702 | int ret; |
4703 | ||
dba4a925 | 4704 | sock = sock_from_file(req->file); |
7a7cacba | 4705 | if (unlikely(!sock)) |
dba4a925 | 4706 | return -ENOTSOCK; |
fddaface | 4707 | |
7a7cacba PB |
4708 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4709 | if (unlikely(ret)) | |
14db8411 | 4710 | return ret; |
fddaface | 4711 | |
7a7cacba PB |
4712 | msg.msg_name = NULL; |
4713 | msg.msg_control = NULL; | |
4714 | msg.msg_controllen = 0; | |
4715 | msg.msg_namelen = 0; | |
fddaface | 4716 | |
7a7cacba PB |
4717 | flags = req->sr_msg.msg_flags; |
4718 | if (flags & MSG_DONTWAIT) | |
4719 | req->flags |= REQ_F_NOWAIT; | |
4720 | else if (force_nonblock) | |
4721 | flags |= MSG_DONTWAIT; | |
fddaface | 4722 | |
7a7cacba PB |
4723 | msg.msg_flags = flags; |
4724 | ret = sock_sendmsg(sock, &msg); | |
4725 | if (force_nonblock && ret == -EAGAIN) | |
4726 | return -EAGAIN; | |
4727 | if (ret == -ERESTARTSYS) | |
4728 | ret = -EINTR; | |
fddaface | 4729 | |
fddaface JA |
4730 | if (ret < 0) |
4731 | req_set_fail_links(req); | |
229a7b63 | 4732 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4733 | return 0; |
fddaface JA |
4734 | } |
4735 | ||
1400e697 PB |
4736 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4737 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4738 | { |
4739 | struct io_sr_msg *sr = &req->sr_msg; | |
4740 | struct iovec __user *uiov; | |
4741 | size_t iov_len; | |
4742 | int ret; | |
4743 | ||
1400e697 PB |
4744 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4745 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4746 | if (ret) |
4747 | return ret; | |
4748 | ||
4749 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4750 | if (iov_len > 1) | |
4751 | return -EINVAL; | |
1400e697 | 4752 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4753 | return -EFAULT; |
1400e697 PB |
4754 | sr->len = iomsg->iov[0].iov_len; |
4755 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4756 | sr->len); |
1400e697 | 4757 | iomsg->iov = NULL; |
52de1fe1 | 4758 | } else { |
89cd35c5 CH |
4759 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
4760 | &iomsg->iov, &iomsg->msg.msg_iter, | |
4761 | false); | |
52de1fe1 JA |
4762 | if (ret > 0) |
4763 | ret = 0; | |
4764 | } | |
4765 | ||
4766 | return ret; | |
4767 | } | |
4768 | ||
4769 | #ifdef CONFIG_COMPAT | |
4770 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4771 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4772 | { |
4773 | struct compat_msghdr __user *msg_compat; | |
4774 | struct io_sr_msg *sr = &req->sr_msg; | |
4775 | struct compat_iovec __user *uiov; | |
4776 | compat_uptr_t ptr; | |
4777 | compat_size_t len; | |
4778 | int ret; | |
4779 | ||
270a5940 | 4780 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4781 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4782 | &ptr, &len); |
4783 | if (ret) | |
4784 | return ret; | |
4785 | ||
4786 | uiov = compat_ptr(ptr); | |
4787 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4788 | compat_ssize_t clen; | |
4789 | ||
4790 | if (len > 1) | |
4791 | return -EINVAL; | |
4792 | if (!access_ok(uiov, sizeof(*uiov))) | |
4793 | return -EFAULT; | |
4794 | if (__get_user(clen, &uiov->iov_len)) | |
4795 | return -EFAULT; | |
4796 | if (clen < 0) | |
4797 | return -EINVAL; | |
2d280bc8 PB |
4798 | sr->len = clen; |
4799 | iomsg->iov[0].iov_len = clen; | |
1400e697 | 4800 | iomsg->iov = NULL; |
52de1fe1 | 4801 | } else { |
89cd35c5 CH |
4802 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
4803 | UIO_FASTIOV, &iomsg->iov, | |
4804 | &iomsg->msg.msg_iter, true); | |
52de1fe1 JA |
4805 | if (ret < 0) |
4806 | return ret; | |
4807 | } | |
4808 | ||
4809 | return 0; | |
4810 | } | |
4811 | #endif | |
4812 | ||
1400e697 PB |
4813 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4814 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4815 | { |
1400e697 PB |
4816 | iomsg->msg.msg_name = &iomsg->addr; |
4817 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4818 | |
4819 | #ifdef CONFIG_COMPAT | |
4820 | if (req->ctx->compat) | |
1400e697 | 4821 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4822 | #endif |
52de1fe1 | 4823 | |
1400e697 | 4824 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4825 | } |
4826 | ||
bcda7baa | 4827 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4828 | bool needs_lock) |
bcda7baa JA |
4829 | { |
4830 | struct io_sr_msg *sr = &req->sr_msg; | |
4831 | struct io_buffer *kbuf; | |
4832 | ||
bcda7baa JA |
4833 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4834 | if (IS_ERR(kbuf)) | |
4835 | return kbuf; | |
4836 | ||
4837 | sr->kbuf = kbuf; | |
4838 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4839 | return kbuf; |
fddaface JA |
4840 | } |
4841 | ||
7fbb1b54 PB |
4842 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4843 | { | |
4844 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4845 | } | |
4846 | ||
3529d8c2 JA |
4847 | static int io_recvmsg_prep(struct io_kiocb *req, |
4848 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4849 | { |
e8c2bc1f | 4850 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4851 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4852 | int ret; |
3529d8c2 | 4853 | |
d2b6f48b PB |
4854 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4855 | return -EINVAL; | |
4856 | ||
3529d8c2 | 4857 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4858 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4859 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4860 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4861 | |
d8768362 JA |
4862 | #ifdef CONFIG_COMPAT |
4863 | if (req->ctx->compat) | |
4864 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4865 | #endif | |
4866 | ||
e8c2bc1f | 4867 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
5f798bea | 4868 | return 0; |
e8c2bc1f | 4869 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4870 | if (!ret) |
4871 | req->flags |= REQ_F_NEED_CLEANUP; | |
4872 | return ret; | |
aa1fa28f JA |
4873 | } |
4874 | ||
229a7b63 JA |
4875 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4876 | struct io_comp_state *cs) | |
aa1fa28f | 4877 | { |
6b754c8b | 4878 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4879 | struct socket *sock; |
7fbb1b54 | 4880 | struct io_buffer *kbuf; |
7a7cacba | 4881 | unsigned flags; |
52de1fe1 | 4882 | int ret, cflags = 0; |
03b1230c | 4883 | |
dba4a925 | 4884 | sock = sock_from_file(req->file); |
7a7cacba | 4885 | if (unlikely(!sock)) |
dba4a925 | 4886 | return -ENOTSOCK; |
3529d8c2 | 4887 | |
e8c2bc1f JA |
4888 | if (req->async_data) { |
4889 | kmsg = req->async_data; | |
4890 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4891 | /* if iov is set, it's allocated already */ |
4892 | if (!kmsg->iov) | |
4893 | kmsg->iov = kmsg->fast_iov; | |
4894 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4895 | } else { | |
4896 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4897 | if (ret) | |
681fda8d | 4898 | return ret; |
7a7cacba PB |
4899 | kmsg = &iomsg; |
4900 | } | |
03b1230c | 4901 | |
bc02ef33 | 4902 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4903 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4904 | if (IS_ERR(kbuf)) |
52de1fe1 | 4905 | return PTR_ERR(kbuf); |
7a7cacba PB |
4906 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4907 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4908 | 1, req->sr_msg.len); | |
4909 | } | |
52de1fe1 | 4910 | |
7a7cacba PB |
4911 | flags = req->sr_msg.msg_flags; |
4912 | if (flags & MSG_DONTWAIT) | |
4913 | req->flags |= REQ_F_NOWAIT; | |
4914 | else if (force_nonblock) | |
4915 | flags |= MSG_DONTWAIT; | |
e47293fd | 4916 | |
7a7cacba PB |
4917 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4918 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4919 | if (force_nonblock && ret == -EAGAIN) |
4920 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4921 | if (ret == -ERESTARTSYS) |
4922 | ret = -EINTR; | |
03b1230c | 4923 | |
7fbb1b54 PB |
4924 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4925 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4926 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4927 | kfree(kmsg->iov); |
99bc4c38 | 4928 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4929 | if (ret < 0) |
4930 | req_set_fail_links(req); | |
229a7b63 | 4931 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4932 | return 0; |
0fa03c62 | 4933 | } |
5d17b4a4 | 4934 | |
229a7b63 JA |
4935 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4936 | struct io_comp_state *cs) | |
fddaface | 4937 | { |
6b754c8b | 4938 | struct io_buffer *kbuf; |
7a7cacba PB |
4939 | struct io_sr_msg *sr = &req->sr_msg; |
4940 | struct msghdr msg; | |
4941 | void __user *buf = sr->buf; | |
fddaface | 4942 | struct socket *sock; |
7a7cacba PB |
4943 | struct iovec iov; |
4944 | unsigned flags; | |
bcda7baa | 4945 | int ret, cflags = 0; |
fddaface | 4946 | |
dba4a925 | 4947 | sock = sock_from_file(req->file); |
7a7cacba | 4948 | if (unlikely(!sock)) |
dba4a925 | 4949 | return -ENOTSOCK; |
fddaface | 4950 | |
bc02ef33 | 4951 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4952 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4953 | if (IS_ERR(kbuf)) |
4954 | return PTR_ERR(kbuf); | |
7a7cacba | 4955 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4956 | } |
bcda7baa | 4957 | |
7a7cacba | 4958 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4959 | if (unlikely(ret)) |
4960 | goto out_free; | |
fddaface | 4961 | |
7a7cacba PB |
4962 | msg.msg_name = NULL; |
4963 | msg.msg_control = NULL; | |
4964 | msg.msg_controllen = 0; | |
4965 | msg.msg_namelen = 0; | |
4966 | msg.msg_iocb = NULL; | |
4967 | msg.msg_flags = 0; | |
fddaface | 4968 | |
7a7cacba PB |
4969 | flags = req->sr_msg.msg_flags; |
4970 | if (flags & MSG_DONTWAIT) | |
4971 | req->flags |= REQ_F_NOWAIT; | |
4972 | else if (force_nonblock) | |
4973 | flags |= MSG_DONTWAIT; | |
4974 | ||
4975 | ret = sock_recvmsg(sock, &msg, flags); | |
4976 | if (force_nonblock && ret == -EAGAIN) | |
4977 | return -EAGAIN; | |
4978 | if (ret == -ERESTARTSYS) | |
4979 | ret = -EINTR; | |
14c32eee | 4980 | out_free: |
7fbb1b54 PB |
4981 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4982 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4983 | if (ret < 0) |
4984 | req_set_fail_links(req); | |
229a7b63 | 4985 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4986 | return 0; |
fddaface JA |
4987 | } |
4988 | ||
3529d8c2 | 4989 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4990 | { |
8ed8d3c3 JA |
4991 | struct io_accept *accept = &req->accept; |
4992 | ||
14587a46 | 4993 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4994 | return -EINVAL; |
8042d6ce | 4995 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4996 | return -EINVAL; |
4997 | ||
d55e5f5b JA |
4998 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4999 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5000 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5001 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 5002 | return 0; |
8ed8d3c3 | 5003 | } |
17f2fe35 | 5004 | |
229a7b63 JA |
5005 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
5006 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
5007 | { |
5008 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 5009 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
5010 | int ret; |
5011 | ||
e697deed JX |
5012 | if (req->file->f_flags & O_NONBLOCK) |
5013 | req->flags |= REQ_F_NOWAIT; | |
5014 | ||
8ed8d3c3 | 5015 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
5016 | accept->addr_len, accept->flags, |
5017 | accept->nofile); | |
8ed8d3c3 | 5018 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 5019 | return -EAGAIN; |
ac45abc0 PB |
5020 | if (ret < 0) { |
5021 | if (ret == -ERESTARTSYS) | |
5022 | ret = -EINTR; | |
4e88d6e7 | 5023 | req_set_fail_links(req); |
ac45abc0 | 5024 | } |
229a7b63 | 5025 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 5026 | return 0; |
8ed8d3c3 JA |
5027 | } |
5028 | ||
3529d8c2 | 5029 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5030 | { |
3529d8c2 | 5031 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 5032 | struct io_async_connect *io = req->async_data; |
f499a021 | 5033 | |
14587a46 | 5034 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
5035 | return -EINVAL; |
5036 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
5037 | return -EINVAL; | |
5038 | ||
3529d8c2 JA |
5039 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5040 | conn->addr_len = READ_ONCE(sqe->addr2); | |
5041 | ||
5042 | if (!io) | |
5043 | return 0; | |
5044 | ||
5045 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 5046 | &io->address); |
f499a021 JA |
5047 | } |
5048 | ||
229a7b63 JA |
5049 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5050 | struct io_comp_state *cs) | |
f8e85cf2 | 5051 | { |
e8c2bc1f | 5052 | struct io_async_connect __io, *io; |
f8e85cf2 | 5053 | unsigned file_flags; |
3fbb51c1 | 5054 | int ret; |
f8e85cf2 | 5055 | |
e8c2bc1f JA |
5056 | if (req->async_data) { |
5057 | io = req->async_data; | |
f499a021 | 5058 | } else { |
3529d8c2 JA |
5059 | ret = move_addr_to_kernel(req->connect.addr, |
5060 | req->connect.addr_len, | |
e8c2bc1f | 5061 | &__io.address); |
f499a021 JA |
5062 | if (ret) |
5063 | goto out; | |
5064 | io = &__io; | |
5065 | } | |
5066 | ||
3fbb51c1 JA |
5067 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5068 | ||
e8c2bc1f | 5069 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5070 | req->connect.addr_len, file_flags); |
87f80d62 | 5071 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5072 | if (req->async_data) |
b7bb4f7d | 5073 | return -EAGAIN; |
e8c2bc1f | 5074 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5075 | ret = -ENOMEM; |
5076 | goto out; | |
5077 | } | |
e8c2bc1f JA |
5078 | io = req->async_data; |
5079 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5080 | return -EAGAIN; |
f499a021 | 5081 | } |
f8e85cf2 JA |
5082 | if (ret == -ERESTARTSYS) |
5083 | ret = -EINTR; | |
f499a021 | 5084 | out: |
4e88d6e7 JA |
5085 | if (ret < 0) |
5086 | req_set_fail_links(req); | |
229a7b63 | 5087 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 5088 | return 0; |
469956e8 Y |
5089 | } |
5090 | #else /* !CONFIG_NET */ | |
5091 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5092 | { | |
f8e85cf2 | 5093 | return -EOPNOTSUPP; |
f8e85cf2 JA |
5094 | } |
5095 | ||
1e16c2f9 RD |
5096 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
5097 | struct io_comp_state *cs) | |
469956e8 Y |
5098 | { |
5099 | return -EOPNOTSUPP; | |
5100 | } | |
5101 | ||
1e16c2f9 RD |
5102 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
5103 | struct io_comp_state *cs) | |
469956e8 Y |
5104 | { |
5105 | return -EOPNOTSUPP; | |
5106 | } | |
5107 | ||
5108 | static int io_recvmsg_prep(struct io_kiocb *req, | |
5109 | const struct io_uring_sqe *sqe) | |
5110 | { | |
5111 | return -EOPNOTSUPP; | |
5112 | } | |
5113 | ||
1e16c2f9 RD |
5114 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
5115 | struct io_comp_state *cs) | |
469956e8 Y |
5116 | { |
5117 | return -EOPNOTSUPP; | |
5118 | } | |
5119 | ||
1e16c2f9 RD |
5120 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
5121 | struct io_comp_state *cs) | |
469956e8 Y |
5122 | { |
5123 | return -EOPNOTSUPP; | |
5124 | } | |
5125 | ||
5126 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5127 | { | |
5128 | return -EOPNOTSUPP; | |
5129 | } | |
5130 | ||
1e16c2f9 RD |
5131 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
5132 | struct io_comp_state *cs) | |
469956e8 Y |
5133 | { |
5134 | return -EOPNOTSUPP; | |
5135 | } | |
ce593a6c | 5136 | |
469956e8 Y |
5137 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5138 | { | |
5139 | return -EOPNOTSUPP; | |
5140 | } | |
5141 | ||
1e16c2f9 RD |
5142 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5143 | struct io_comp_state *cs) | |
469956e8 | 5144 | { |
f8e85cf2 | 5145 | return -EOPNOTSUPP; |
ce593a6c | 5146 | } |
469956e8 | 5147 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5148 | |
d7718a9d JA |
5149 | struct io_poll_table { |
5150 | struct poll_table_struct pt; | |
5151 | struct io_kiocb *req; | |
5152 | int error; | |
5153 | }; | |
ce593a6c | 5154 | |
d7718a9d JA |
5155 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5156 | __poll_t mask, task_work_func_t func) | |
5157 | { | |
aa96bf8a | 5158 | int ret; |
d7718a9d JA |
5159 | |
5160 | /* for instances that support it check for an event match first: */ | |
5161 | if (mask && !(mask & poll->events)) | |
5162 | return 0; | |
5163 | ||
5164 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5165 | ||
5166 | list_del_init(&poll->wait.entry); | |
5167 | ||
d7718a9d JA |
5168 | req->result = mask; |
5169 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
5170 | percpu_ref_get(&req->ctx->refs); |
5171 | ||
d7718a9d | 5172 | /* |
e3aabf95 JA |
5173 | * If this fails, then the task is exiting. When a task exits, the |
5174 | * work gets canceled, so just cancel this request as well instead | |
5175 | * of executing it. We can't safely execute it anyway, as we may not | |
5176 | * have the needed state needed for it anyway. | |
d7718a9d | 5177 | */ |
355fb9e2 | 5178 | ret = io_req_task_work_add(req); |
aa96bf8a | 5179 | if (unlikely(ret)) { |
c2c4c83c JA |
5180 | struct task_struct *tsk; |
5181 | ||
e3aabf95 | 5182 | WRITE_ONCE(poll->canceled, true); |
aa96bf8a | 5183 | tsk = io_wq_get_task(req->ctx->io_wq); |
91989c70 | 5184 | task_work_add(tsk, &req->task_work, TWA_NONE); |
ce593a6c | 5185 | wake_up_process(tsk); |
aa96bf8a | 5186 | } |
d7718a9d JA |
5187 | return 1; |
5188 | } | |
5189 | ||
74ce6ce4 JA |
5190 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5191 | __acquires(&req->ctx->completion_lock) | |
5192 | { | |
5193 | struct io_ring_ctx *ctx = req->ctx; | |
5194 | ||
5195 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5196 | struct poll_table_struct pt = { ._key = poll->events }; | |
5197 | ||
5198 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5199 | } | |
5200 | ||
5201 | spin_lock_irq(&ctx->completion_lock); | |
5202 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5203 | add_wait_queue(poll->head, &poll->wait); | |
5204 | return true; | |
5205 | } | |
5206 | ||
5207 | return false; | |
5208 | } | |
5209 | ||
d4e7cd36 | 5210 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5211 | { |
e8c2bc1f | 5212 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5213 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5214 | return req->async_data; |
d4e7cd36 JA |
5215 | return req->apoll->double_poll; |
5216 | } | |
5217 | ||
5218 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5219 | { | |
5220 | if (req->opcode == IORING_OP_POLL_ADD) | |
5221 | return &req->poll; | |
5222 | return &req->apoll->poll; | |
5223 | } | |
5224 | ||
5225 | static void io_poll_remove_double(struct io_kiocb *req) | |
5226 | { | |
5227 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5228 | |
5229 | lockdep_assert_held(&req->ctx->completion_lock); | |
5230 | ||
5231 | if (poll && poll->head) { | |
5232 | struct wait_queue_head *head = poll->head; | |
5233 | ||
5234 | spin_lock(&head->lock); | |
5235 | list_del_init(&poll->wait.entry); | |
5236 | if (poll->wait.private) | |
5237 | refcount_dec(&req->refs); | |
5238 | poll->head = NULL; | |
5239 | spin_unlock(&head->lock); | |
5240 | } | |
5241 | } | |
5242 | ||
5243 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5244 | { | |
5245 | struct io_ring_ctx *ctx = req->ctx; | |
5246 | ||
d4e7cd36 | 5247 | io_poll_remove_double(req); |
18bceab1 JA |
5248 | req->poll.done = true; |
5249 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5250 | io_commit_cqring(ctx); | |
5251 | } | |
5252 | ||
dd221f46 | 5253 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5254 | { |
dd221f46 | 5255 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5256 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5257 | struct io_kiocb *nxt; |
18bceab1 JA |
5258 | |
5259 | if (io_poll_rewait(req, &req->poll)) { | |
5260 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5261 | } else { |
5262 | hash_del(&req->hash_node); | |
5263 | io_poll_complete(req, req->result, 0); | |
5264 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5265 | |
dd221f46 PB |
5266 | nxt = io_put_req_find_next(req); |
5267 | io_cqring_ev_posted(ctx); | |
5268 | if (nxt) | |
5269 | __io_req_task_submit(nxt); | |
5270 | } | |
18bceab1 | 5271 | |
6d816e08 | 5272 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5273 | } |
5274 | ||
5275 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5276 | int sync, void *key) | |
5277 | { | |
5278 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5279 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5280 | __poll_t mask = key_to_poll(key); |
5281 | ||
5282 | /* for instances that support it check for an event match first: */ | |
5283 | if (mask && !(mask & poll->events)) | |
5284 | return 0; | |
5285 | ||
8706e04e JA |
5286 | list_del_init(&wait->entry); |
5287 | ||
807abcb0 | 5288 | if (poll && poll->head) { |
18bceab1 JA |
5289 | bool done; |
5290 | ||
807abcb0 JA |
5291 | spin_lock(&poll->head->lock); |
5292 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5293 | if (!done) |
807abcb0 | 5294 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5295 | /* make sure double remove sees this as being gone */ |
5296 | wait->private = NULL; | |
807abcb0 | 5297 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5298 | if (!done) { |
5299 | /* use wait func handler, so it matches the rq type */ | |
5300 | poll->wait.func(&poll->wait, mode, sync, key); | |
5301 | } | |
18bceab1 JA |
5302 | } |
5303 | refcount_dec(&req->refs); | |
5304 | return 1; | |
5305 | } | |
5306 | ||
5307 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5308 | wait_queue_func_t wake_func) | |
5309 | { | |
5310 | poll->head = NULL; | |
5311 | poll->done = false; | |
5312 | poll->canceled = false; | |
5313 | poll->events = events; | |
5314 | INIT_LIST_HEAD(&poll->wait.entry); | |
5315 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5316 | } | |
5317 | ||
5318 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5319 | struct wait_queue_head *head, |
5320 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5321 | { |
5322 | struct io_kiocb *req = pt->req; | |
5323 | ||
5324 | /* | |
5325 | * If poll->head is already set, it's because the file being polled | |
5326 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5327 | * for write). Setup a separate io_poll_iocb if this happens. | |
5328 | */ | |
5329 | if (unlikely(poll->head)) { | |
58852d4d PB |
5330 | struct io_poll_iocb *poll_one = poll; |
5331 | ||
18bceab1 | 5332 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5333 | if (*poll_ptr) { |
18bceab1 JA |
5334 | pt->error = -EINVAL; |
5335 | return; | |
5336 | } | |
5337 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5338 | if (!poll) { | |
5339 | pt->error = -ENOMEM; | |
5340 | return; | |
5341 | } | |
58852d4d | 5342 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5343 | refcount_inc(&req->refs); |
5344 | poll->wait.private = req; | |
807abcb0 | 5345 | *poll_ptr = poll; |
18bceab1 JA |
5346 | } |
5347 | ||
5348 | pt->error = 0; | |
5349 | poll->head = head; | |
a31eb4a2 JX |
5350 | |
5351 | if (poll->events & EPOLLEXCLUSIVE) | |
5352 | add_wait_queue_exclusive(head, &poll->wait); | |
5353 | else | |
5354 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5355 | } |
5356 | ||
5357 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5358 | struct poll_table_struct *p) | |
5359 | { | |
5360 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5361 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5362 | |
807abcb0 | 5363 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5364 | } |
5365 | ||
d7718a9d JA |
5366 | static void io_async_task_func(struct callback_head *cb) |
5367 | { | |
5368 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5369 | struct async_poll *apoll = req->apoll; | |
5370 | struct io_ring_ctx *ctx = req->ctx; | |
5371 | ||
5372 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5373 | ||
74ce6ce4 | 5374 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5375 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5376 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5377 | return; |
d7718a9d JA |
5378 | } |
5379 | ||
31067255 | 5380 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5381 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5382 | hash_del(&req->hash_node); |
2bae047e | 5383 | |
d4e7cd36 | 5384 | io_poll_remove_double(req); |
74ce6ce4 JA |
5385 | spin_unlock_irq(&ctx->completion_lock); |
5386 | ||
0be0b0e3 PB |
5387 | if (!READ_ONCE(apoll->poll.canceled)) |
5388 | __io_req_task_submit(req); | |
5389 | else | |
5390 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5391 | |
6d816e08 | 5392 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5393 | kfree(apoll->double_poll); |
31067255 | 5394 | kfree(apoll); |
d7718a9d JA |
5395 | } |
5396 | ||
5397 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5398 | void *key) | |
5399 | { | |
5400 | struct io_kiocb *req = wait->private; | |
5401 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5402 | ||
5403 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5404 | key_to_poll(key)); | |
5405 | ||
5406 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5407 | } | |
5408 | ||
5409 | static void io_poll_req_insert(struct io_kiocb *req) | |
5410 | { | |
5411 | struct io_ring_ctx *ctx = req->ctx; | |
5412 | struct hlist_head *list; | |
5413 | ||
5414 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5415 | hlist_add_head(&req->hash_node, list); | |
5416 | } | |
5417 | ||
5418 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5419 | struct io_poll_iocb *poll, | |
5420 | struct io_poll_table *ipt, __poll_t mask, | |
5421 | wait_queue_func_t wake_func) | |
5422 | __acquires(&ctx->completion_lock) | |
5423 | { | |
5424 | struct io_ring_ctx *ctx = req->ctx; | |
5425 | bool cancel = false; | |
5426 | ||
4d52f338 | 5427 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5428 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5429 | poll->file = req->file; |
18bceab1 | 5430 | poll->wait.private = req; |
d7718a9d JA |
5431 | |
5432 | ipt->pt._key = mask; | |
5433 | ipt->req = req; | |
5434 | ipt->error = -EINVAL; | |
5435 | ||
d7718a9d JA |
5436 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5437 | ||
5438 | spin_lock_irq(&ctx->completion_lock); | |
5439 | if (likely(poll->head)) { | |
5440 | spin_lock(&poll->head->lock); | |
5441 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5442 | if (ipt->error) | |
5443 | cancel = true; | |
5444 | ipt->error = 0; | |
5445 | mask = 0; | |
5446 | } | |
5447 | if (mask || ipt->error) | |
5448 | list_del_init(&poll->wait.entry); | |
5449 | else if (cancel) | |
5450 | WRITE_ONCE(poll->canceled, true); | |
5451 | else if (!poll->done) /* actually waiting for an event */ | |
5452 | io_poll_req_insert(req); | |
5453 | spin_unlock(&poll->head->lock); | |
5454 | } | |
5455 | ||
5456 | return mask; | |
5457 | } | |
5458 | ||
5459 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5460 | { | |
5461 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5462 | struct io_ring_ctx *ctx = req->ctx; | |
5463 | struct async_poll *apoll; | |
5464 | struct io_poll_table ipt; | |
5465 | __poll_t mask, ret; | |
9dab14b8 | 5466 | int rw; |
d7718a9d JA |
5467 | |
5468 | if (!req->file || !file_can_poll(req->file)) | |
5469 | return false; | |
24c74678 | 5470 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5471 | return false; |
9dab14b8 JA |
5472 | if (def->pollin) |
5473 | rw = READ; | |
5474 | else if (def->pollout) | |
5475 | rw = WRITE; | |
5476 | else | |
5477 | return false; | |
5478 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5479 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5480 | return false; |
5481 | ||
5482 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5483 | if (unlikely(!apoll)) | |
5484 | return false; | |
807abcb0 | 5485 | apoll->double_poll = NULL; |
d7718a9d JA |
5486 | |
5487 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5488 | req->apoll = apoll; |
d7718a9d | 5489 | |
8755d97a | 5490 | mask = 0; |
d7718a9d | 5491 | if (def->pollin) |
8755d97a | 5492 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5493 | if (def->pollout) |
5494 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5495 | |
5496 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5497 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5498 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5499 | mask &= ~POLLIN; | |
5500 | ||
d7718a9d JA |
5501 | mask |= POLLERR | POLLPRI; |
5502 | ||
5503 | ipt.pt._qproc = io_async_queue_proc; | |
5504 | ||
5505 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5506 | io_async_wake); | |
a36da65c | 5507 | if (ret || ipt.error) { |
d4e7cd36 | 5508 | io_poll_remove_double(req); |
d7718a9d | 5509 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5510 | kfree(apoll->double_poll); |
d7718a9d JA |
5511 | kfree(apoll); |
5512 | return false; | |
5513 | } | |
5514 | spin_unlock_irq(&ctx->completion_lock); | |
5515 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5516 | apoll->poll.events); | |
5517 | return true; | |
5518 | } | |
5519 | ||
5520 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5521 | struct io_poll_iocb *poll) | |
221c5eb2 | 5522 | { |
b41e9852 | 5523 | bool do_complete = false; |
221c5eb2 JA |
5524 | |
5525 | spin_lock(&poll->head->lock); | |
5526 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5527 | if (!list_empty(&poll->wait.entry)) { |
5528 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5529 | do_complete = true; |
221c5eb2 JA |
5530 | } |
5531 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5532 | hash_del(&req->hash_node); |
d7718a9d JA |
5533 | return do_complete; |
5534 | } | |
5535 | ||
5536 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5537 | { | |
5538 | bool do_complete; | |
5539 | ||
d4e7cd36 JA |
5540 | io_poll_remove_double(req); |
5541 | ||
d7718a9d JA |
5542 | if (req->opcode == IORING_OP_POLL_ADD) { |
5543 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5544 | } else { | |
3bfa5bcb JA |
5545 | struct async_poll *apoll = req->apoll; |
5546 | ||
d7718a9d | 5547 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5548 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5549 | if (do_complete) { | |
d7718a9d | 5550 | io_put_req(req); |
807abcb0 | 5551 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5552 | kfree(apoll); |
5553 | } | |
b1f573bd XW |
5554 | } |
5555 | ||
b41e9852 JA |
5556 | if (do_complete) { |
5557 | io_cqring_fill_event(req, -ECANCELED); | |
5558 | io_commit_cqring(req->ctx); | |
f254ac04 | 5559 | req_set_fail_links(req); |
216578e5 | 5560 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5561 | } |
5562 | ||
5563 | return do_complete; | |
221c5eb2 JA |
5564 | } |
5565 | ||
76e1b642 JA |
5566 | /* |
5567 | * Returns true if we found and killed one or more poll requests | |
5568 | */ | |
6b81928d PB |
5569 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5570 | struct files_struct *files) | |
221c5eb2 | 5571 | { |
78076bb6 | 5572 | struct hlist_node *tmp; |
221c5eb2 | 5573 | struct io_kiocb *req; |
8e2e1faf | 5574 | int posted = 0, i; |
221c5eb2 JA |
5575 | |
5576 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5577 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5578 | struct hlist_head *list; | |
5579 | ||
5580 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5581 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5582 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5583 | posted += io_poll_remove_one(req); |
5584 | } | |
221c5eb2 JA |
5585 | } |
5586 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5587 | |
8e2e1faf JA |
5588 | if (posted) |
5589 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5590 | |
5591 | return posted != 0; | |
221c5eb2 JA |
5592 | } |
5593 | ||
47f46768 JA |
5594 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5595 | { | |
78076bb6 | 5596 | struct hlist_head *list; |
47f46768 JA |
5597 | struct io_kiocb *req; |
5598 | ||
78076bb6 JA |
5599 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5600 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5601 | if (sqe_addr != req->user_data) |
5602 | continue; | |
5603 | if (io_poll_remove_one(req)) | |
eac406c6 | 5604 | return 0; |
b41e9852 | 5605 | return -EALREADY; |
47f46768 JA |
5606 | } |
5607 | ||
5608 | return -ENOENT; | |
5609 | } | |
5610 | ||
3529d8c2 JA |
5611 | static int io_poll_remove_prep(struct io_kiocb *req, |
5612 | const struct io_uring_sqe *sqe) | |
0969e783 | 5613 | { |
0969e783 JA |
5614 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5615 | return -EINVAL; | |
5616 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5617 | sqe->poll_events) | |
5618 | return -EINVAL; | |
5619 | ||
018043be | 5620 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5621 | return 0; |
5622 | } | |
5623 | ||
221c5eb2 JA |
5624 | /* |
5625 | * Find a running poll command that matches one specified in sqe->addr, | |
5626 | * and remove it if found. | |
5627 | */ | |
fc4df999 | 5628 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5629 | { |
5630 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5631 | int ret; |
221c5eb2 | 5632 | |
221c5eb2 | 5633 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5634 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5635 | spin_unlock_irq(&ctx->completion_lock); |
5636 | ||
4e88d6e7 JA |
5637 | if (ret < 0) |
5638 | req_set_fail_links(req); | |
e1e16097 | 5639 | io_req_complete(req, ret); |
221c5eb2 JA |
5640 | return 0; |
5641 | } | |
5642 | ||
221c5eb2 JA |
5643 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5644 | void *key) | |
5645 | { | |
c2f2eb7d JA |
5646 | struct io_kiocb *req = wait->private; |
5647 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5648 | |
d7718a9d | 5649 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5650 | } |
5651 | ||
221c5eb2 JA |
5652 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5653 | struct poll_table_struct *p) | |
5654 | { | |
5655 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5656 | ||
e8c2bc1f | 5657 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5658 | } |
5659 | ||
3529d8c2 | 5660 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5661 | { |
5662 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5663 | u32 events; |
221c5eb2 JA |
5664 | |
5665 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5666 | return -EINVAL; | |
5667 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5668 | return -EINVAL; | |
5669 | ||
5769a351 JX |
5670 | events = READ_ONCE(sqe->poll32_events); |
5671 | #ifdef __BIG_ENDIAN | |
5672 | events = swahw32(events); | |
5673 | #endif | |
a31eb4a2 JX |
5674 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5675 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5676 | return 0; |
5677 | } | |
5678 | ||
014db007 | 5679 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5680 | { |
5681 | struct io_poll_iocb *poll = &req->poll; | |
5682 | struct io_ring_ctx *ctx = req->ctx; | |
5683 | struct io_poll_table ipt; | |
0969e783 | 5684 | __poll_t mask; |
0969e783 | 5685 | |
d7718a9d | 5686 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5687 | |
d7718a9d JA |
5688 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5689 | io_poll_wake); | |
221c5eb2 | 5690 | |
8c838788 | 5691 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5692 | ipt.error = 0; |
b0dd8a41 | 5693 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5694 | } |
221c5eb2 JA |
5695 | spin_unlock_irq(&ctx->completion_lock); |
5696 | ||
8c838788 JA |
5697 | if (mask) { |
5698 | io_cqring_ev_posted(ctx); | |
014db007 | 5699 | io_put_req(req); |
221c5eb2 | 5700 | } |
8c838788 | 5701 | return ipt.error; |
221c5eb2 JA |
5702 | } |
5703 | ||
5262f567 JA |
5704 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5705 | { | |
ad8a48ac JA |
5706 | struct io_timeout_data *data = container_of(timer, |
5707 | struct io_timeout_data, timer); | |
5708 | struct io_kiocb *req = data->req; | |
5709 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5710 | unsigned long flags; |
5711 | ||
5262f567 | 5712 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5713 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5714 | atomic_set(&req->ctx->cq_timeouts, |
5715 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5716 | ||
78e19bbe | 5717 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5718 | io_commit_cqring(ctx); |
5719 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5720 | ||
5721 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5722 | req_set_fail_links(req); |
5262f567 JA |
5723 | io_put_req(req); |
5724 | return HRTIMER_NORESTART; | |
5725 | } | |
5726 | ||
fbd15848 PB |
5727 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5728 | __u64 user_data) | |
f254ac04 | 5729 | { |
fbd15848 | 5730 | struct io_timeout_data *io; |
47f46768 JA |
5731 | struct io_kiocb *req; |
5732 | int ret = -ENOENT; | |
f254ac04 | 5733 | |
135fcde8 | 5734 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5735 | if (user_data == req->user_data) { |
47f46768 JA |
5736 | ret = 0; |
5737 | break; | |
5738 | } | |
5739 | } | |
5740 | ||
5741 | if (ret == -ENOENT) | |
fbd15848 PB |
5742 | return ERR_PTR(ret); |
5743 | ||
5744 | io = req->async_data; | |
e8c2bc1f | 5745 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5746 | if (ret == -1) |
fbd15848 | 5747 | return ERR_PTR(-EALREADY); |
a71976f3 | 5748 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5749 | return req; |
5750 | } | |
47f46768 | 5751 | |
fbd15848 PB |
5752 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5753 | { | |
5754 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5755 | ||
5756 | if (IS_ERR(req)) | |
5757 | return PTR_ERR(req); | |
f254ac04 JA |
5758 | |
5759 | req_set_fail_links(req); | |
f254ac04 | 5760 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5761 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5762 | return 0; |
5763 | } | |
5764 | ||
9c8e11b3 PB |
5765 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5766 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5767 | { |
9c8e11b3 PB |
5768 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5769 | struct io_timeout_data *data; | |
47f46768 | 5770 | |
9c8e11b3 PB |
5771 | if (IS_ERR(req)) |
5772 | return PTR_ERR(req); | |
47f46768 | 5773 | |
9c8e11b3 PB |
5774 | req->timeout.off = 0; /* noseq */ |
5775 | data = req->async_data; | |
5776 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5777 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5778 | data->timer.function = io_timeout_fn; | |
5779 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5780 | return 0; | |
47f46768 JA |
5781 | } |
5782 | ||
3529d8c2 JA |
5783 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5784 | const struct io_uring_sqe *sqe) | |
b29472ee | 5785 | { |
9c8e11b3 PB |
5786 | struct io_timeout_rem *tr = &req->timeout_rem; |
5787 | ||
b29472ee JA |
5788 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5789 | return -EINVAL; | |
61710e43 DA |
5790 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5791 | return -EINVAL; | |
9c8e11b3 | 5792 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5793 | return -EINVAL; |
5794 | ||
9c8e11b3 PB |
5795 | tr->addr = READ_ONCE(sqe->addr); |
5796 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5797 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5798 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5799 | return -EINVAL; | |
5800 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5801 | return -EFAULT; | |
5802 | } else if (tr->flags) { | |
5803 | /* timeout removal doesn't support flags */ | |
b29472ee | 5804 | return -EINVAL; |
9c8e11b3 | 5805 | } |
b29472ee | 5806 | |
b29472ee JA |
5807 | return 0; |
5808 | } | |
5809 | ||
11365043 JA |
5810 | /* |
5811 | * Remove or update an existing timeout command | |
5812 | */ | |
fc4df999 | 5813 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 | 5814 | { |
9c8e11b3 | 5815 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5816 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5817 | int ret; |
11365043 | 5818 | |
11365043 | 5819 | spin_lock_irq(&ctx->completion_lock); |
9c8e11b3 PB |
5820 | if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) { |
5821 | enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS) | |
5822 | ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; | |
5823 | ||
5824 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
5825 | } else { | |
5826 | ret = io_timeout_cancel(ctx, tr->addr); | |
5827 | } | |
11365043 | 5828 | |
47f46768 | 5829 | io_cqring_fill_event(req, ret); |
11365043 JA |
5830 | io_commit_cqring(ctx); |
5831 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5832 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5833 | if (ret < 0) |
5834 | req_set_fail_links(req); | |
ec9c02ad | 5835 | io_put_req(req); |
11365043 | 5836 | return 0; |
5262f567 JA |
5837 | } |
5838 | ||
3529d8c2 | 5839 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5840 | bool is_timeout_link) |
5262f567 | 5841 | { |
ad8a48ac | 5842 | struct io_timeout_data *data; |
a41525ab | 5843 | unsigned flags; |
56080b02 | 5844 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5845 | |
ad8a48ac | 5846 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5847 | return -EINVAL; |
ad8a48ac | 5848 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5849 | return -EINVAL; |
56080b02 | 5850 | if (off && is_timeout_link) |
2d28390a | 5851 | return -EINVAL; |
a41525ab JA |
5852 | flags = READ_ONCE(sqe->timeout_flags); |
5853 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5854 | return -EINVAL; |
bdf20073 | 5855 | |
bfe68a22 | 5856 | req->timeout.off = off; |
26a61679 | 5857 | |
e8c2bc1f | 5858 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5859 | return -ENOMEM; |
5860 | ||
e8c2bc1f | 5861 | data = req->async_data; |
ad8a48ac | 5862 | data->req = req; |
ad8a48ac JA |
5863 | |
5864 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5865 | return -EFAULT; |
5866 | ||
11365043 | 5867 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 5868 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 5869 | else |
ad8a48ac | 5870 | data->mode = HRTIMER_MODE_REL; |
11365043 | 5871 | |
ad8a48ac JA |
5872 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5873 | return 0; | |
5874 | } | |
5875 | ||
fc4df999 | 5876 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5877 | { |
ad8a48ac | 5878 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5879 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5880 | struct list_head *entry; |
bfe68a22 | 5881 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5882 | |
733f5c95 | 5883 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5884 | |
5262f567 JA |
5885 | /* |
5886 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5887 | * timeout event to be satisfied. If it isn't set, then this is |
5888 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5889 | */ |
8eb7e2d0 | 5890 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5891 | entry = ctx->timeout_list.prev; |
5892 | goto add; | |
5893 | } | |
5262f567 | 5894 | |
bfe68a22 PB |
5895 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5896 | req->timeout.target_seq = tail + off; | |
5262f567 | 5897 | |
f010505b MDG |
5898 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5899 | * This is safe because ->completion_lock is held, and submissions | |
5900 | * and completions are never mixed in the same ->completion_lock section. | |
5901 | */ | |
5902 | ctx->cq_last_tm_flush = tail; | |
5903 | ||
5262f567 JA |
5904 | /* |
5905 | * Insertion sort, ensuring the first entry in the list is always | |
5906 | * the one we need first. | |
5907 | */ | |
5262f567 | 5908 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5909 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5910 | timeout.list); | |
5262f567 | 5911 | |
8eb7e2d0 | 5912 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5913 | continue; |
bfe68a22 PB |
5914 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5915 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5916 | break; |
5917 | } | |
93bd25bb | 5918 | add: |
135fcde8 | 5919 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5920 | data->timer.function = io_timeout_fn; |
5921 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5922 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5923 | return 0; |
5924 | } | |
5262f567 | 5925 | |
62755e35 JA |
5926 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5927 | { | |
5928 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5929 | ||
5930 | return req->user_data == (unsigned long) data; | |
5931 | } | |
5932 | ||
e977d6d3 | 5933 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5934 | { |
62755e35 | 5935 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5936 | int ret = 0; |
5937 | ||
4f26bda1 | 5938 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5939 | switch (cancel_ret) { |
5940 | case IO_WQ_CANCEL_OK: | |
5941 | ret = 0; | |
5942 | break; | |
5943 | case IO_WQ_CANCEL_RUNNING: | |
5944 | ret = -EALREADY; | |
5945 | break; | |
5946 | case IO_WQ_CANCEL_NOTFOUND: | |
5947 | ret = -ENOENT; | |
5948 | break; | |
5949 | } | |
5950 | ||
e977d6d3 JA |
5951 | return ret; |
5952 | } | |
5953 | ||
47f46768 JA |
5954 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5955 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5956 | int success_ret) |
47f46768 JA |
5957 | { |
5958 | unsigned long flags; | |
5959 | int ret; | |
5960 | ||
5961 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5962 | if (ret != -ENOENT) { | |
5963 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5964 | goto done; | |
5965 | } | |
5966 | ||
5967 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5968 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5969 | if (ret != -ENOENT) | |
5970 | goto done; | |
5971 | ret = io_poll_cancel(ctx, sqe_addr); | |
5972 | done: | |
b0dd8a41 JA |
5973 | if (!ret) |
5974 | ret = success_ret; | |
47f46768 JA |
5975 | io_cqring_fill_event(req, ret); |
5976 | io_commit_cqring(ctx); | |
5977 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5978 | io_cqring_ev_posted(ctx); | |
5979 | ||
4e88d6e7 JA |
5980 | if (ret < 0) |
5981 | req_set_fail_links(req); | |
014db007 | 5982 | io_put_req(req); |
47f46768 JA |
5983 | } |
5984 | ||
3529d8c2 JA |
5985 | static int io_async_cancel_prep(struct io_kiocb *req, |
5986 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5987 | { |
fbf23849 | 5988 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5989 | return -EINVAL; |
61710e43 DA |
5990 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5991 | return -EINVAL; | |
5992 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5993 | return -EINVAL; |
5994 | ||
fbf23849 JA |
5995 | req->cancel.addr = READ_ONCE(sqe->addr); |
5996 | return 0; | |
5997 | } | |
5998 | ||
014db007 | 5999 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
6000 | { |
6001 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 6002 | |
014db007 | 6003 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
6004 | return 0; |
6005 | } | |
6006 | ||
269bbe5f | 6007 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6008 | const struct io_uring_sqe *sqe) |
6009 | { | |
6ca56f84 JA |
6010 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
6011 | return -EINVAL; | |
61710e43 DA |
6012 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6013 | return -EINVAL; | |
6014 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
6015 | return -EINVAL; |
6016 | ||
269bbe5f BM |
6017 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6018 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6019 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6020 | return -EINVAL; |
269bbe5f | 6021 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6022 | return 0; |
6023 | } | |
6024 | ||
229a7b63 JA |
6025 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
6026 | struct io_comp_state *cs) | |
fbf23849 JA |
6027 | { |
6028 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 6029 | struct io_uring_rsrc_update up; |
05f3fb3c | 6030 | int ret; |
fbf23849 | 6031 | |
f86cd20c | 6032 | if (force_nonblock) |
05f3fb3c | 6033 | return -EAGAIN; |
05f3fb3c | 6034 | |
269bbe5f BM |
6035 | up.offset = req->rsrc_update.offset; |
6036 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
6037 | |
6038 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 6039 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6040 | mutex_unlock(&ctx->uring_lock); |
6041 | ||
6042 | if (ret < 0) | |
6043 | req_set_fail_links(req); | |
229a7b63 | 6044 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
6045 | return 0; |
6046 | } | |
6047 | ||
bfe76559 | 6048 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6049 | { |
d625c6ee | 6050 | switch (req->opcode) { |
e781573e | 6051 | case IORING_OP_NOP: |
bfe76559 | 6052 | return 0; |
f67676d1 JA |
6053 | case IORING_OP_READV: |
6054 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6055 | case IORING_OP_READ: |
bfe76559 | 6056 | return io_read_prep(req, sqe); |
f67676d1 JA |
6057 | case IORING_OP_WRITEV: |
6058 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6059 | case IORING_OP_WRITE: |
bfe76559 | 6060 | return io_write_prep(req, sqe); |
0969e783 | 6061 | case IORING_OP_POLL_ADD: |
bfe76559 | 6062 | return io_poll_add_prep(req, sqe); |
0969e783 | 6063 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 6064 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 6065 | case IORING_OP_FSYNC: |
bfe76559 | 6066 | return io_prep_fsync(req, sqe); |
8ed8d3c3 | 6067 | case IORING_OP_SYNC_FILE_RANGE: |
bfe76559 | 6068 | return io_prep_sfr(req, sqe); |
03b1230c | 6069 | case IORING_OP_SENDMSG: |
fddaface | 6070 | case IORING_OP_SEND: |
bfe76559 | 6071 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6072 | case IORING_OP_RECVMSG: |
fddaface | 6073 | case IORING_OP_RECV: |
bfe76559 | 6074 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6075 | case IORING_OP_CONNECT: |
bfe76559 | 6076 | return io_connect_prep(req, sqe); |
2d28390a | 6077 | case IORING_OP_TIMEOUT: |
bfe76559 | 6078 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6079 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6080 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6081 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6082 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6083 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6084 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6085 | case IORING_OP_ACCEPT: |
bfe76559 | 6086 | return io_accept_prep(req, sqe); |
d63d1b5e | 6087 | case IORING_OP_FALLOCATE: |
bfe76559 | 6088 | return io_fallocate_prep(req, sqe); |
15b71abe | 6089 | case IORING_OP_OPENAT: |
bfe76559 | 6090 | return io_openat_prep(req, sqe); |
b5dba59e | 6091 | case IORING_OP_CLOSE: |
bfe76559 | 6092 | return io_close_prep(req, sqe); |
05f3fb3c | 6093 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6094 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6095 | case IORING_OP_STATX: |
bfe76559 | 6096 | return io_statx_prep(req, sqe); |
4840e418 | 6097 | case IORING_OP_FADVISE: |
bfe76559 | 6098 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6099 | case IORING_OP_MADVISE: |
bfe76559 | 6100 | return io_madvise_prep(req, sqe); |
cebdb986 | 6101 | case IORING_OP_OPENAT2: |
bfe76559 | 6102 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6103 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6104 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6105 | case IORING_OP_SPLICE: |
bfe76559 | 6106 | return io_splice_prep(req, sqe); |
ddf0322d | 6107 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6108 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6109 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6110 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6111 | case IORING_OP_TEE: |
bfe76559 | 6112 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6113 | case IORING_OP_SHUTDOWN: |
6114 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6115 | case IORING_OP_RENAMEAT: |
6116 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6117 | case IORING_OP_UNLINKAT: |
6118 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6119 | } |
6120 | ||
bfe76559 PB |
6121 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6122 | req->opcode); | |
6123 | return-EINVAL; | |
6124 | } | |
6125 | ||
6126 | static int io_req_defer_prep(struct io_kiocb *req, | |
6127 | const struct io_uring_sqe *sqe) | |
6128 | { | |
bfe76559 PB |
6129 | if (!sqe) |
6130 | return 0; | |
6131 | if (io_alloc_async_data(req)) | |
6132 | return -EAGAIN; | |
bfe76559 | 6133 | return io_req_prep(req, sqe); |
f67676d1 JA |
6134 | } |
6135 | ||
9cf7c104 PB |
6136 | static u32 io_get_sequence(struct io_kiocb *req) |
6137 | { | |
6138 | struct io_kiocb *pos; | |
6139 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6140 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6141 | |
f2f87370 PB |
6142 | io_for_each_link(pos, req) |
6143 | nr_reqs++; | |
9cf7c104 PB |
6144 | |
6145 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6146 | return total_submitted - nr_reqs; | |
6147 | } | |
6148 | ||
3529d8c2 | 6149 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 6150 | { |
a197f664 | 6151 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6152 | struct io_defer_entry *de; |
f67676d1 | 6153 | int ret; |
9cf7c104 | 6154 | u32 seq; |
de0617e4 | 6155 | |
9d858b21 | 6156 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6157 | if (likely(list_empty_careful(&ctx->defer_list) && |
6158 | !(req->flags & REQ_F_IO_DRAIN))) | |
6159 | return 0; | |
6160 | ||
6161 | seq = io_get_sequence(req); | |
6162 | /* Still a chance to pass the sequence check */ | |
6163 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6164 | return 0; |
6165 | ||
e8c2bc1f | 6166 | if (!req->async_data) { |
650b5481 | 6167 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6168 | if (ret) |
650b5481 PB |
6169 | return ret; |
6170 | } | |
cbdcb435 | 6171 | io_prep_async_link(req); |
27dc8338 PB |
6172 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6173 | if (!de) | |
6174 | return -ENOMEM; | |
2d28390a | 6175 | |
de0617e4 | 6176 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6177 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6178 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6179 | kfree(de); |
ae34817b PB |
6180 | io_queue_async_work(req); |
6181 | return -EIOCBQUEUED; | |
de0617e4 JA |
6182 | } |
6183 | ||
915967f6 | 6184 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6185 | de->req = req; |
9cf7c104 | 6186 | de->seq = seq; |
27dc8338 | 6187 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6188 | spin_unlock_irq(&ctx->completion_lock); |
6189 | return -EIOCBQUEUED; | |
6190 | } | |
6191 | ||
f573d384 | 6192 | static void io_req_drop_files(struct io_kiocb *req) |
99bc4c38 | 6193 | { |
f573d384 | 6194 | struct io_ring_ctx *ctx = req->ctx; |
c98de08c | 6195 | struct io_uring_task *tctx = req->task->io_uring; |
f573d384 JA |
6196 | unsigned long flags; |
6197 | ||
02a13674 JA |
6198 | if (req->work.flags & IO_WQ_WORK_FILES) { |
6199 | put_files_struct(req->work.identity->files); | |
6200 | put_nsproxy(req->work.identity->nsproxy); | |
6201 | } | |
f573d384 JA |
6202 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
6203 | list_del(&req->inflight_entry); | |
f573d384 JA |
6204 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
6205 | req->flags &= ~REQ_F_INFLIGHT; | |
dfead8a8 | 6206 | req->work.flags &= ~IO_WQ_WORK_FILES; |
dfea9fce PB |
6207 | if (atomic_read(&tctx->in_idle)) |
6208 | wake_up(&tctx->wait); | |
f573d384 | 6209 | } |
99bc4c38 | 6210 | |
3ca405eb | 6211 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6212 | { |
0e1b6fe3 PB |
6213 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6214 | switch (req->opcode) { | |
6215 | case IORING_OP_READV: | |
6216 | case IORING_OP_READ_FIXED: | |
6217 | case IORING_OP_READ: | |
bcda7baa | 6218 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6219 | break; |
6220 | case IORING_OP_RECVMSG: | |
6221 | case IORING_OP_RECV: | |
bcda7baa | 6222 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6223 | break; |
6224 | } | |
6225 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6226 | } |
6227 | ||
0e1b6fe3 PB |
6228 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6229 | switch (req->opcode) { | |
6230 | case IORING_OP_READV: | |
6231 | case IORING_OP_READ_FIXED: | |
6232 | case IORING_OP_READ: | |
6233 | case IORING_OP_WRITEV: | |
6234 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6235 | case IORING_OP_WRITE: { |
6236 | struct io_async_rw *io = req->async_data; | |
6237 | if (io->free_iovec) | |
6238 | kfree(io->free_iovec); | |
0e1b6fe3 | 6239 | break; |
e8c2bc1f | 6240 | } |
0e1b6fe3 | 6241 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6242 | case IORING_OP_SENDMSG: { |
6243 | struct io_async_msghdr *io = req->async_data; | |
6244 | if (io->iov != io->fast_iov) | |
6245 | kfree(io->iov); | |
0e1b6fe3 | 6246 | break; |
e8c2bc1f | 6247 | } |
0e1b6fe3 PB |
6248 | case IORING_OP_SPLICE: |
6249 | case IORING_OP_TEE: | |
6250 | io_put_file(req, req->splice.file_in, | |
6251 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6252 | break; | |
f3cd4850 JA |
6253 | case IORING_OP_OPENAT: |
6254 | case IORING_OP_OPENAT2: | |
6255 | if (req->open.filename) | |
6256 | putname(req->open.filename); | |
6257 | break; | |
80a261fd JA |
6258 | case IORING_OP_RENAMEAT: |
6259 | putname(req->rename.oldpath); | |
6260 | putname(req->rename.newpath); | |
6261 | break; | |
14a1143b JA |
6262 | case IORING_OP_UNLINKAT: |
6263 | putname(req->unlink.filename); | |
6264 | break; | |
0e1b6fe3 PB |
6265 | } |
6266 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6267 | } |
99bc4c38 PB |
6268 | } |
6269 | ||
c1379e24 PB |
6270 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
6271 | struct io_comp_state *cs) | |
2b188cc1 | 6272 | { |
a197f664 | 6273 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6274 | int ret; |
2b188cc1 | 6275 | |
d625c6ee | 6276 | switch (req->opcode) { |
2b188cc1 | 6277 | case IORING_OP_NOP: |
229a7b63 | 6278 | ret = io_nop(req, cs); |
2b188cc1 JA |
6279 | break; |
6280 | case IORING_OP_READV: | |
edafccee | 6281 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6282 | case IORING_OP_READ: |
a1d7c393 | 6283 | ret = io_read(req, force_nonblock, cs); |
edafccee | 6284 | break; |
3529d8c2 | 6285 | case IORING_OP_WRITEV: |
edafccee | 6286 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6287 | case IORING_OP_WRITE: |
a1d7c393 | 6288 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 6289 | break; |
c992fe29 | 6290 | case IORING_OP_FSYNC: |
014db007 | 6291 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 6292 | break; |
221c5eb2 | 6293 | case IORING_OP_POLL_ADD: |
014db007 | 6294 | ret = io_poll_add(req); |
221c5eb2 JA |
6295 | break; |
6296 | case IORING_OP_POLL_REMOVE: | |
fc4df999 | 6297 | ret = io_poll_remove(req); |
221c5eb2 | 6298 | break; |
5d17b4a4 | 6299 | case IORING_OP_SYNC_FILE_RANGE: |
014db007 | 6300 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 6301 | break; |
0fa03c62 | 6302 | case IORING_OP_SENDMSG: |
062d04d7 PB |
6303 | ret = io_sendmsg(req, force_nonblock, cs); |
6304 | break; | |
fddaface | 6305 | case IORING_OP_SEND: |
062d04d7 | 6306 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 6307 | break; |
aa1fa28f | 6308 | case IORING_OP_RECVMSG: |
062d04d7 PB |
6309 | ret = io_recvmsg(req, force_nonblock, cs); |
6310 | break; | |
fddaface | 6311 | case IORING_OP_RECV: |
062d04d7 | 6312 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 6313 | break; |
5262f567 | 6314 | case IORING_OP_TIMEOUT: |
fc4df999 | 6315 | ret = io_timeout(req); |
5262f567 | 6316 | break; |
11365043 | 6317 | case IORING_OP_TIMEOUT_REMOVE: |
fc4df999 | 6318 | ret = io_timeout_remove(req); |
11365043 | 6319 | break; |
17f2fe35 | 6320 | case IORING_OP_ACCEPT: |
229a7b63 | 6321 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 6322 | break; |
f8e85cf2 | 6323 | case IORING_OP_CONNECT: |
229a7b63 | 6324 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 6325 | break; |
62755e35 | 6326 | case IORING_OP_ASYNC_CANCEL: |
014db007 | 6327 | ret = io_async_cancel(req); |
62755e35 | 6328 | break; |
d63d1b5e | 6329 | case IORING_OP_FALLOCATE: |
014db007 | 6330 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 6331 | break; |
15b71abe | 6332 | case IORING_OP_OPENAT: |
014db007 | 6333 | ret = io_openat(req, force_nonblock); |
15b71abe | 6334 | break; |
b5dba59e | 6335 | case IORING_OP_CLOSE: |
229a7b63 | 6336 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 6337 | break; |
05f3fb3c | 6338 | case IORING_OP_FILES_UPDATE: |
229a7b63 | 6339 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 6340 | break; |
eddc7ef5 | 6341 | case IORING_OP_STATX: |
014db007 | 6342 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 6343 | break; |
4840e418 | 6344 | case IORING_OP_FADVISE: |
014db007 | 6345 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 6346 | break; |
c1ca757b | 6347 | case IORING_OP_MADVISE: |
014db007 | 6348 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 6349 | break; |
cebdb986 | 6350 | case IORING_OP_OPENAT2: |
014db007 | 6351 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 6352 | break; |
3e4827b0 | 6353 | case IORING_OP_EPOLL_CTL: |
229a7b63 | 6354 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 6355 | break; |
7d67af2c | 6356 | case IORING_OP_SPLICE: |
014db007 | 6357 | ret = io_splice(req, force_nonblock); |
7d67af2c | 6358 | break; |
ddf0322d | 6359 | case IORING_OP_PROVIDE_BUFFERS: |
229a7b63 | 6360 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 6361 | break; |
067524e9 | 6362 | case IORING_OP_REMOVE_BUFFERS: |
229a7b63 | 6363 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 6364 | break; |
f2a8d5c7 | 6365 | case IORING_OP_TEE: |
f2a8d5c7 PB |
6366 | ret = io_tee(req, force_nonblock); |
6367 | break; | |
36f4fa68 JA |
6368 | case IORING_OP_SHUTDOWN: |
6369 | ret = io_shutdown(req, force_nonblock); | |
6370 | break; | |
80a261fd JA |
6371 | case IORING_OP_RENAMEAT: |
6372 | ret = io_renameat(req, force_nonblock); | |
6373 | break; | |
14a1143b JA |
6374 | case IORING_OP_UNLINKAT: |
6375 | ret = io_unlinkat(req, force_nonblock); | |
6376 | break; | |
2b188cc1 JA |
6377 | default: |
6378 | ret = -EINVAL; | |
6379 | break; | |
6380 | } | |
6381 | ||
def596e9 JA |
6382 | if (ret) |
6383 | return ret; | |
6384 | ||
b532576e JA |
6385 | /* If the op doesn't have a file, we're not polling for it */ |
6386 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6387 | const bool in_async = io_wq_current_is_worker(); |
6388 | ||
11ba820b JA |
6389 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6390 | if (in_async) | |
6391 | mutex_lock(&ctx->uring_lock); | |
6392 | ||
2e9dbe90 | 6393 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6394 | |
6395 | if (in_async) | |
6396 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6397 | } |
6398 | ||
6399 | return 0; | |
2b188cc1 JA |
6400 | } |
6401 | ||
f4db7182 | 6402 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6403 | { |
6404 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6405 | struct io_kiocb *timeout; |
561fb04a | 6406 | int ret = 0; |
2b188cc1 | 6407 | |
6df1db6b PB |
6408 | timeout = io_prep_linked_timeout(req); |
6409 | if (timeout) | |
6410 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6411 | |
0c9d5ccd JA |
6412 | /* if NO_CANCEL is set, we must still run the work */ |
6413 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
6414 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 6415 | ret = -ECANCELED; |
0c9d5ccd | 6416 | } |
31b51510 | 6417 | |
561fb04a | 6418 | if (!ret) { |
561fb04a | 6419 | do { |
c1379e24 | 6420 | ret = io_issue_sqe(req, false, NULL); |
561fb04a JA |
6421 | /* |
6422 | * We can get EAGAIN for polled IO even though we're | |
6423 | * forcing a sync submission from here, since we can't | |
6424 | * wait for request slots on the block side. | |
6425 | */ | |
6426 | if (ret != -EAGAIN) | |
6427 | break; | |
6428 | cond_resched(); | |
6429 | } while (1); | |
6430 | } | |
31b51510 | 6431 | |
561fb04a | 6432 | if (ret) { |
c07e6719 XW |
6433 | struct io_ring_ctx *lock_ctx = NULL; |
6434 | ||
6435 | if (req->ctx->flags & IORING_SETUP_IOPOLL) | |
6436 | lock_ctx = req->ctx; | |
6437 | ||
dad1b124 | 6438 | /* |
c07e6719 XW |
6439 | * io_iopoll_complete() does not hold completion_lock to |
6440 | * complete polled io, so here for polled io, we can not call | |
6441 | * io_req_complete() directly, otherwise there maybe concurrent | |
6442 | * access to cqring, defer_list, etc, which is not safe. Given | |
6443 | * that io_iopoll_complete() is always called under uring_lock, | |
6444 | * so here for polled io, we also get uring_lock to complete | |
6445 | * it. | |
dad1b124 | 6446 | */ |
c07e6719 XW |
6447 | if (lock_ctx) |
6448 | mutex_lock(&lock_ctx->uring_lock); | |
dad1b124 | 6449 | |
c07e6719 XW |
6450 | req_set_fail_links(req); |
6451 | io_req_complete(req, ret); | |
6452 | ||
6453 | if (lock_ctx) | |
6454 | mutex_unlock(&lock_ctx->uring_lock); | |
edafccee | 6455 | } |
2b188cc1 | 6456 | |
f4db7182 | 6457 | return io_steal_work(req); |
2b188cc1 JA |
6458 | } |
6459 | ||
65e19f54 JA |
6460 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6461 | int index) | |
6462 | { | |
269bbe5f | 6463 | struct fixed_rsrc_table *table; |
65e19f54 | 6464 | |
05f3fb3c | 6465 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6466 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6467 | } |
6468 | ||
8371adf5 PB |
6469 | static struct file *io_file_get(struct io_submit_state *state, |
6470 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6471 | { |
a197f664 | 6472 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6473 | struct file *file; |
09bb8394 | 6474 | |
8da11c19 | 6475 | if (fixed) { |
479f517b | 6476 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6477 | return NULL; |
b7620121 | 6478 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6479 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6480 | io_set_resource_node(req); |
09bb8394 | 6481 | } else { |
c826bd7a | 6482 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6483 | file = __io_file_get(state, fd); |
09bb8394 JA |
6484 | } |
6485 | ||
f609cbb8 PB |
6486 | if (file && file->f_op == &io_uring_fops && |
6487 | !(req->flags & REQ_F_INFLIGHT)) { | |
02a13674 JA |
6488 | io_req_init_async(req); |
6489 | req->flags |= REQ_F_INFLIGHT; | |
6490 | ||
6491 | spin_lock_irq(&ctx->inflight_lock); | |
6492 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
6493 | spin_unlock_irq(&ctx->inflight_lock); | |
6494 | } | |
6495 | ||
8371adf5 | 6496 | return file; |
09bb8394 JA |
6497 | } |
6498 | ||
2665abfd | 6499 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6500 | { |
ad8a48ac JA |
6501 | struct io_timeout_data *data = container_of(timer, |
6502 | struct io_timeout_data, timer); | |
90cd7e42 | 6503 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6504 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6505 | unsigned long flags; |
2665abfd JA |
6506 | |
6507 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6508 | prev = req->timeout.head; |
6509 | req->timeout.head = NULL; | |
2665abfd JA |
6510 | |
6511 | /* | |
6512 | * We don't expect the list to be empty, that will only happen if we | |
6513 | * race with the completion of the linked work. | |
6514 | */ | |
90cd7e42 | 6515 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6516 | io_remove_next_linked(prev); |
90cd7e42 PB |
6517 | else |
6518 | prev = NULL; | |
2665abfd JA |
6519 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6520 | ||
6521 | if (prev) { | |
4e88d6e7 | 6522 | req_set_fail_links(prev); |
014db007 | 6523 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
76a46e06 | 6524 | io_put_req(prev); |
47f46768 | 6525 | } else { |
e1e16097 | 6526 | io_req_complete(req, -ETIME); |
2665abfd | 6527 | } |
2665abfd JA |
6528 | return HRTIMER_NORESTART; |
6529 | } | |
6530 | ||
7271ef3a | 6531 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6532 | { |
76a46e06 | 6533 | /* |
f2f87370 PB |
6534 | * If the back reference is NULL, then our linked request finished |
6535 | * before we got a chance to setup the timer | |
76a46e06 | 6536 | */ |
90cd7e42 | 6537 | if (req->timeout.head) { |
e8c2bc1f | 6538 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6539 | |
ad8a48ac JA |
6540 | data->timer.function = io_link_timeout_fn; |
6541 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6542 | data->mode); | |
2665abfd | 6543 | } |
7271ef3a JA |
6544 | } |
6545 | ||
6546 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6547 | { | |
6548 | struct io_ring_ctx *ctx = req->ctx; | |
6549 | ||
6550 | spin_lock_irq(&ctx->completion_lock); | |
6551 | __io_queue_linked_timeout(req); | |
76a46e06 | 6552 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6553 | |
2665abfd | 6554 | /* drop submission reference */ |
76a46e06 JA |
6555 | io_put_req(req); |
6556 | } | |
2665abfd | 6557 | |
ad8a48ac | 6558 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6559 | { |
f2f87370 | 6560 | struct io_kiocb *nxt = req->link; |
2665abfd | 6561 | |
f2f87370 PB |
6562 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6563 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6564 | return NULL; |
2665abfd | 6565 | |
90cd7e42 | 6566 | nxt->timeout.head = req; |
900fad45 | 6567 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6568 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6569 | return nxt; |
2665abfd JA |
6570 | } |
6571 | ||
c1379e24 | 6572 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 | 6573 | { |
4a0a7a18 | 6574 | struct io_kiocb *linked_timeout; |
193155c8 | 6575 | const struct cred *old_creds = NULL; |
e0c5c576 | 6576 | int ret; |
2b188cc1 | 6577 | |
4a0a7a18 JA |
6578 | again: |
6579 | linked_timeout = io_prep_linked_timeout(req); | |
6580 | ||
2e5aa6cb PB |
6581 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6582 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
98447d65 | 6583 | req->work.identity->creds != current_cred()) { |
193155c8 JA |
6584 | if (old_creds) |
6585 | revert_creds(old_creds); | |
98447d65 | 6586 | if (old_creds == req->work.identity->creds) |
193155c8 JA |
6587 | old_creds = NULL; /* restored original creds */ |
6588 | else | |
98447d65 | 6589 | old_creds = override_creds(req->work.identity->creds); |
193155c8 JA |
6590 | } |
6591 | ||
c1379e24 | 6592 | ret = io_issue_sqe(req, true, cs); |
491381ce JA |
6593 | |
6594 | /* | |
6595 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6596 | * doesn't support non-blocking read/write attempts | |
6597 | */ | |
24c74678 | 6598 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6599 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6600 | /* |
6601 | * Queued up for async execution, worker will release | |
6602 | * submit reference when the iocb is actually submitted. | |
6603 | */ | |
6604 | io_queue_async_work(req); | |
2b188cc1 | 6605 | } |
bbad27b2 | 6606 | |
f063c547 PB |
6607 | if (linked_timeout) |
6608 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 PB |
6609 | } else if (likely(!ret)) { |
6610 | /* drop submission reference */ | |
6611 | req = io_put_req_find_next(req); | |
6612 | if (linked_timeout) | |
6613 | io_queue_linked_timeout(linked_timeout); | |
e65ef56d | 6614 | |
0d63c148 PB |
6615 | if (req) { |
6616 | if (!(req->flags & REQ_F_FORCE_ASYNC)) | |
6617 | goto again; | |
6618 | io_queue_async_work(req); | |
6619 | } | |
6620 | } else { | |
652532ad PB |
6621 | /* un-prep timeout, so it'll be killed as any other linked */ |
6622 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6623 | req_set_fail_links(req); |
e65ef56d | 6624 | io_put_req(req); |
e1e16097 | 6625 | io_req_complete(req, ret); |
9e645e11 | 6626 | } |
652532ad | 6627 | |
193155c8 JA |
6628 | if (old_creds) |
6629 | revert_creds(old_creds); | |
2b188cc1 JA |
6630 | } |
6631 | ||
f13fad7b JA |
6632 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6633 | struct io_comp_state *cs) | |
4fe2c963 JL |
6634 | { |
6635 | int ret; | |
6636 | ||
3529d8c2 | 6637 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6638 | if (ret) { |
6639 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6640 | fail_req: |
4e88d6e7 | 6641 | req_set_fail_links(req); |
e1e16097 JA |
6642 | io_put_req(req); |
6643 | io_req_complete(req, ret); | |
4fe2c963 | 6644 | } |
2550878f | 6645 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6646 | if (!req->async_data) { |
bd2ab18a | 6647 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6648 | if (unlikely(ret)) |
bd2ab18a PB |
6649 | goto fail_req; |
6650 | } | |
ce35a47a JA |
6651 | io_queue_async_work(req); |
6652 | } else { | |
c1379e24 PB |
6653 | if (sqe) { |
6654 | ret = io_req_prep(req, sqe); | |
6655 | if (unlikely(ret)) | |
6656 | goto fail_req; | |
6657 | } | |
6658 | __io_queue_sqe(req, cs); | |
ce35a47a | 6659 | } |
4fe2c963 JL |
6660 | } |
6661 | ||
f13fad7b JA |
6662 | static inline void io_queue_link_head(struct io_kiocb *req, |
6663 | struct io_comp_state *cs) | |
4fe2c963 | 6664 | { |
94ae5e77 | 6665 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6666 | io_put_req(req); |
6667 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6668 | } else |
f13fad7b | 6669 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6670 | } |
6671 | ||
863e0560 PB |
6672 | struct io_submit_link { |
6673 | struct io_kiocb *head; | |
6674 | struct io_kiocb *last; | |
6675 | }; | |
6676 | ||
1d4240cc | 6677 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
863e0560 | 6678 | struct io_submit_link *link, struct io_comp_state *cs) |
9e645e11 | 6679 | { |
a197f664 | 6680 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6681 | int ret; |
9e645e11 | 6682 | |
9e645e11 JA |
6683 | /* |
6684 | * If we already have a head request, queue this one for async | |
6685 | * submittal once the head completes. If we don't have a head but | |
6686 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6687 | * submitted sync once the chain is complete. If none of those | |
6688 | * conditions are true (normal request), then just queue it. | |
6689 | */ | |
863e0560 PB |
6690 | if (link->head) { |
6691 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6692 | |
8cdf2193 PB |
6693 | /* |
6694 | * Taking sequential execution of a link, draining both sides | |
6695 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6696 | * requests in the link. So, it drains the head and the | |
6697 | * next after the link request. The last one is done via | |
6698 | * drain_next flag to persist the effect across calls. | |
6699 | */ | |
ef4ff581 | 6700 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6701 | head->flags |= REQ_F_IO_DRAIN; |
6702 | ctx->drain_next = 1; | |
6703 | } | |
3529d8c2 | 6704 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6705 | if (unlikely(ret)) { |
4e88d6e7 | 6706 | /* fail even hard links since we don't submit */ |
9d76377f | 6707 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6708 | return ret; |
2d28390a | 6709 | } |
9d76377f | 6710 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6711 | link->last->link = req; |
863e0560 | 6712 | link->last = req; |
32fe525b PB |
6713 | |
6714 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6715 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6716 | io_queue_link_head(head, cs); |
863e0560 | 6717 | link->head = NULL; |
32fe525b | 6718 | } |
9e645e11 | 6719 | } else { |
711be031 PB |
6720 | if (unlikely(ctx->drain_next)) { |
6721 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6722 | ctx->drain_next = 0; |
711be031 | 6723 | } |
ef4ff581 | 6724 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
711be031 | 6725 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6726 | if (unlikely(ret)) |
711be031 | 6727 | req->flags |= REQ_F_FAIL_LINK; |
863e0560 PB |
6728 | link->head = req; |
6729 | link->last = req; | |
711be031 | 6730 | } else { |
f13fad7b | 6731 | io_queue_sqe(req, sqe, cs); |
711be031 | 6732 | } |
9e645e11 | 6733 | } |
2e6e1fde | 6734 | |
1d4240cc | 6735 | return 0; |
9e645e11 JA |
6736 | } |
6737 | ||
9a56a232 JA |
6738 | /* |
6739 | * Batched submission is done, ensure local IO is flushed out. | |
6740 | */ | |
6741 | static void io_submit_state_end(struct io_submit_state *state) | |
6742 | { | |
f13fad7b JA |
6743 | if (!list_empty(&state->comp.list)) |
6744 | io_submit_flush_completions(&state->comp); | |
27926b68 JA |
6745 | if (state->plug_started) |
6746 | blk_finish_plug(&state->plug); | |
9f13c35b | 6747 | io_state_file_put(state); |
2579f913 | 6748 | if (state->free_reqs) |
6c8a3134 | 6749 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6750 | } |
6751 | ||
6752 | /* | |
6753 | * Start submission side cache. | |
6754 | */ | |
6755 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6756 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 | 6757 | { |
27926b68 | 6758 | state->plug_started = false; |
013538bd JA |
6759 | state->comp.nr = 0; |
6760 | INIT_LIST_HEAD(&state->comp.list); | |
6761 | state->comp.ctx = ctx; | |
2579f913 | 6762 | state->free_reqs = 0; |
6e1271e6 | 6763 | state->file_refs = 0; |
9a56a232 JA |
6764 | state->ios_left = max_ios; |
6765 | } | |
6766 | ||
2b188cc1 JA |
6767 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6768 | { | |
75b28aff | 6769 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6770 | |
caf582c6 PB |
6771 | /* |
6772 | * Ensure any loads from the SQEs are done at this point, | |
6773 | * since once we write the new head, the application could | |
6774 | * write new data to them. | |
6775 | */ | |
6776 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6777 | } |
6778 | ||
2b188cc1 | 6779 | /* |
3529d8c2 | 6780 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6781 | * that is mapped by userspace. This means that care needs to be taken to |
6782 | * ensure that reads are stable, as we cannot rely on userspace always | |
6783 | * being a good citizen. If members of the sqe are validated and then later | |
6784 | * used, it's important that those reads are done through READ_ONCE() to | |
6785 | * prevent a re-load down the line. | |
6786 | */ | |
709b302f | 6787 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6788 | { |
75b28aff | 6789 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6790 | unsigned head; |
6791 | ||
6792 | /* | |
6793 | * The cached sq head (or cq tail) serves two purposes: | |
6794 | * | |
6795 | * 1) allows us to batch the cost of updating the user visible | |
6796 | * head updates. | |
6797 | * 2) allows the kernel side to track the head on its own, even | |
6798 | * though the application is the one updating it. | |
6799 | */ | |
ee7d46d9 | 6800 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6801 | if (likely(head < ctx->sq_entries)) |
6802 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6803 | |
6804 | /* drop invalid entries */ | |
498ccd9e | 6805 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6806 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6807 | return NULL; |
6808 | } | |
6809 | ||
6810 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6811 | { | |
6812 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6813 | } |
6814 | ||
21b55dbc SG |
6815 | /* |
6816 | * Check SQE restrictions (opcode and flags). | |
6817 | * | |
6818 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6819 | */ | |
6820 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6821 | struct io_kiocb *req, | |
6822 | unsigned int sqe_flags) | |
6823 | { | |
6824 | if (!ctx->restricted) | |
6825 | return true; | |
6826 | ||
6827 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6828 | return false; | |
6829 | ||
6830 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6831 | ctx->restrictions.sqe_flags_required) | |
6832 | return false; | |
6833 | ||
6834 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6835 | ctx->restrictions.sqe_flags_required)) | |
6836 | return false; | |
6837 | ||
6838 | return true; | |
6839 | } | |
6840 | ||
ef4ff581 PB |
6841 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6842 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6843 | IOSQE_BUFFER_SELECT) | |
6844 | ||
6845 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6846 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6847 | struct io_submit_state *state) |
0553b8bd | 6848 | { |
ef4ff581 | 6849 | unsigned int sqe_flags; |
71b547c0 | 6850 | int id, ret; |
ef4ff581 | 6851 | |
0553b8bd PB |
6852 | req->opcode = READ_ONCE(sqe->opcode); |
6853 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6854 | req->async_data = NULL; |
0553b8bd PB |
6855 | req->file = NULL; |
6856 | req->ctx = ctx; | |
6857 | req->flags = 0; | |
f2f87370 | 6858 | req->link = NULL; |
269bbe5f | 6859 | req->fixed_rsrc_refs = NULL; |
0553b8bd PB |
6860 | /* one is dropped after submission, the other at completion */ |
6861 | refcount_set(&req->refs, 2); | |
4dd2824d | 6862 | req->task = current; |
0553b8bd | 6863 | req->result = 0; |
ef4ff581 PB |
6864 | |
6865 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6866 | return -EINVAL; | |
6867 | ||
28cea78a | 6868 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
9d8426a0 | 6869 | return -EFAULT; |
ef4ff581 PB |
6870 | |
6871 | sqe_flags = READ_ONCE(sqe->flags); | |
6872 | /* enforce forwards compatibility on users */ | |
6873 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6874 | return -EINVAL; | |
6875 | ||
21b55dbc SG |
6876 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6877 | return -EACCES; | |
6878 | ||
ef4ff581 PB |
6879 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6880 | !io_op_defs[req->opcode].buffer_select) | |
6881 | return -EOPNOTSUPP; | |
6882 | ||
6883 | id = READ_ONCE(sqe->personality); | |
6884 | if (id) { | |
1e6fa521 JA |
6885 | struct io_identity *iod; |
6886 | ||
1e6fa521 JA |
6887 | iod = idr_find(&ctx->personality_idr, id); |
6888 | if (unlikely(!iod)) | |
ef4ff581 | 6889 | return -EINVAL; |
1e6fa521 | 6890 | refcount_inc(&iod->count); |
ec99ca6c PB |
6891 | |
6892 | __io_req_init_async(req); | |
1e6fa521 JA |
6893 | get_cred(iod->creds); |
6894 | req->work.identity = iod; | |
dfead8a8 | 6895 | req->work.flags |= IO_WQ_WORK_CREDS; |
ef4ff581 PB |
6896 | } |
6897 | ||
6898 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6899 | req->flags |= sqe_flags; |
ef4ff581 | 6900 | |
27926b68 JA |
6901 | /* |
6902 | * Plug now if we have more than 1 IO left after this, and the target | |
6903 | * is potentially a read/write to block based storage. | |
6904 | */ | |
6905 | if (!state->plug_started && state->ios_left > 1 && | |
6906 | io_op_defs[req->opcode].plug) { | |
6907 | blk_start_plug(&state->plug); | |
6908 | state->plug_started = true; | |
6909 | } | |
6910 | ||
bd5bbda7 PB |
6911 | ret = 0; |
6912 | if (io_op_defs[req->opcode].needs_file) { | |
6913 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6914 | ||
6915 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6916 | if (unlikely(!req->file && | |
6917 | !io_op_defs[req->opcode].needs_file_no_error)) | |
6918 | ret = -EBADF; | |
6919 | } | |
63ff8223 | 6920 | |
71b547c0 PB |
6921 | state->ios_left--; |
6922 | return ret; | |
0553b8bd PB |
6923 | } |
6924 | ||
0f212204 | 6925 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6926 | { |
ac8691c4 | 6927 | struct io_submit_state state; |
863e0560 | 6928 | struct io_submit_link link; |
9e645e11 | 6929 | int i, submitted = 0; |
6c271ce2 | 6930 | |
c4a2ed72 | 6931 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6932 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6933 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6934 | return -EBUSY; |
6935 | } | |
6c271ce2 | 6936 | |
ee7d46d9 PB |
6937 | /* make sure SQ entry isn't read before tail */ |
6938 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6939 | |
2b85edfc PB |
6940 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6941 | return -EAGAIN; | |
6c271ce2 | 6942 | |
d8a6df10 | 6943 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6944 | refcount_add(nr, ¤t->usage); |
6c271ce2 | 6945 | |
013538bd | 6946 | io_submit_state_start(&state, ctx, nr); |
863e0560 | 6947 | link.head = NULL; |
b14cca0c | 6948 | |
6c271ce2 | 6949 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6950 | const struct io_uring_sqe *sqe; |
196be95c | 6951 | struct io_kiocb *req; |
1cb1edb2 | 6952 | int err; |
fb5ccc98 | 6953 | |
b1e50e54 PB |
6954 | sqe = io_get_sqe(ctx); |
6955 | if (unlikely(!sqe)) { | |
6956 | io_consume_sqe(ctx); | |
6957 | break; | |
6958 | } | |
ac8691c4 | 6959 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6960 | if (unlikely(!req)) { |
6961 | if (!submitted) | |
6962 | submitted = -EAGAIN; | |
fb5ccc98 | 6963 | break; |
196be95c | 6964 | } |
709b302f | 6965 | io_consume_sqe(ctx); |
d3656344 JA |
6966 | /* will complete beyond this point, count as submitted */ |
6967 | submitted++; | |
6968 | ||
692d8363 | 6969 | err = io_init_req(ctx, req, sqe, &state); |
ef4ff581 | 6970 | if (unlikely(err)) { |
1cb1edb2 | 6971 | fail_req: |
e1e16097 JA |
6972 | io_put_req(req); |
6973 | io_req_complete(req, err); | |
196be95c PB |
6974 | break; |
6975 | } | |
fb5ccc98 | 6976 | |
354420f7 | 6977 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
0cdaf760 | 6978 | true, io_async_submit(ctx)); |
f13fad7b | 6979 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6980 | if (err) |
6981 | goto fail_req; | |
6c271ce2 JA |
6982 | } |
6983 | ||
9466f437 PB |
6984 | if (unlikely(submitted != nr)) { |
6985 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6986 | struct io_uring_task *tctx = current->io_uring; |
6987 | int unused = nr - ref_used; | |
9466f437 | 6988 | |
d8a6df10 JA |
6989 | percpu_ref_put_many(&ctx->refs, unused); |
6990 | percpu_counter_sub(&tctx->inflight, unused); | |
6991 | put_task_struct_many(current, unused); | |
9466f437 | 6992 | } |
863e0560 PB |
6993 | if (link.head) |
6994 | io_queue_link_head(link.head, &state.comp); | |
ac8691c4 | 6995 | io_submit_state_end(&state); |
6c271ce2 | 6996 | |
ae9428ca PB |
6997 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6998 | io_commit_sqring(ctx); | |
6999 | ||
6c271ce2 JA |
7000 | return submitted; |
7001 | } | |
7002 | ||
23b3628e XW |
7003 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7004 | { | |
7005 | /* Tell userspace we may need a wakeup call */ | |
7006 | spin_lock_irq(&ctx->completion_lock); | |
7007 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
7008 | spin_unlock_irq(&ctx->completion_lock); | |
7009 | } | |
7010 | ||
7011 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7012 | { | |
7013 | spin_lock_irq(&ctx->completion_lock); | |
7014 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
7015 | spin_unlock_irq(&ctx->completion_lock); | |
7016 | } | |
7017 | ||
08369246 | 7018 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 7019 | { |
c8d1ba58 | 7020 | unsigned int to_submit; |
bdcd3eab | 7021 | int ret = 0; |
6c271ce2 | 7022 | |
c8d1ba58 | 7023 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
7024 | /* if we're handling multiple rings, cap submit size for fairness */ |
7025 | if (cap_entries && to_submit > 8) | |
7026 | to_submit = 8; | |
7027 | ||
906a3c6f | 7028 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 7029 | unsigned nr_events = 0; |
a4c0b3de | 7030 | |
c8d1ba58 | 7031 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7032 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 7033 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7034 | |
d9d05217 PB |
7035 | if (to_submit && !ctx->sqo_dead && |
7036 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 7037 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
7038 | mutex_unlock(&ctx->uring_lock); |
7039 | } | |
6c271ce2 | 7040 | |
90554200 JA |
7041 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7042 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 7043 | |
08369246 XW |
7044 | return ret; |
7045 | } | |
6c271ce2 | 7046 | |
08369246 XW |
7047 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7048 | { | |
7049 | struct io_ring_ctx *ctx; | |
7050 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7051 | |
08369246 XW |
7052 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7053 | if (sq_thread_idle < ctx->sq_thread_idle) | |
7054 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 7055 | } |
c1edbf5f | 7056 | |
08369246 | 7057 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7058 | } |
6c271ce2 | 7059 | |
69fb2131 JA |
7060 | static void io_sqd_init_new(struct io_sq_data *sqd) |
7061 | { | |
7062 | struct io_ring_ctx *ctx; | |
7063 | ||
7064 | while (!list_empty(&sqd->ctx_new_list)) { | |
7065 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
7066 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
7067 | complete(&ctx->sq_thread_comp); | |
7068 | } | |
08369246 XW |
7069 | |
7070 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
7071 | } |
7072 | ||
c8d1ba58 JA |
7073 | static int io_sq_thread(void *data) |
7074 | { | |
91d8f519 | 7075 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
7076 | struct files_struct *old_files = current->files; |
7077 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
7078 | const struct cred *old_cred = NULL; |
7079 | struct io_sq_data *sqd = data; | |
7080 | struct io_ring_ctx *ctx; | |
a0d9205f | 7081 | unsigned long timeout = 0; |
08369246 | 7082 | DEFINE_WAIT(wait); |
6c271ce2 | 7083 | |
28cea78a JA |
7084 | task_lock(current); |
7085 | current->files = NULL; | |
7086 | current->nsproxy = NULL; | |
7087 | task_unlock(current); | |
6c271ce2 | 7088 | |
69fb2131 | 7089 | while (!kthread_should_stop()) { |
08369246 XW |
7090 | int ret; |
7091 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
7092 | |
7093 | /* | |
69fb2131 JA |
7094 | * Any changes to the sqd lists are synchronized through the |
7095 | * kthread parking. This synchronizes the thread vs users, | |
7096 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7097 | */ |
65b2b213 | 7098 | if (kthread_should_park()) { |
69fb2131 | 7099 | kthread_parkme(); |
65b2b213 XW |
7100 | /* |
7101 | * When sq thread is unparked, in case the previous park operation | |
7102 | * comes from io_put_sq_data(), which means that sq thread is going | |
7103 | * to be stopped, so here needs to have a check. | |
7104 | */ | |
7105 | if (kthread_should_stop()) | |
7106 | break; | |
7107 | } | |
7143b5ac | 7108 | |
08369246 | 7109 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7110 | io_sqd_init_new(sqd); |
08369246 XW |
7111 | timeout = jiffies + sqd->sq_thread_idle; |
7112 | } | |
6c271ce2 | 7113 | |
08369246 | 7114 | sqt_spin = false; |
e95eee2d | 7115 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7116 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7117 | if (current->cred != ctx->creds) { | |
7118 | if (old_cred) | |
7119 | revert_creds(old_cred); | |
7120 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7121 | } |
91d8f519 | 7122 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7123 | #ifdef CONFIG_AUDIT |
7124 | current->loginuid = ctx->loginuid; | |
7125 | current->sessionid = ctx->sessionid; | |
7126 | #endif | |
bdcd3eab | 7127 | |
08369246 XW |
7128 | ret = __io_sq_thread(ctx, cap_entries); |
7129 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7130 | sqt_spin = true; | |
6c271ce2 | 7131 | |
28cea78a | 7132 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7133 | } |
6c271ce2 | 7134 | |
08369246 | 7135 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7136 | io_run_task_work(); |
d434ab6d | 7137 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7138 | cond_resched(); |
08369246 XW |
7139 | if (sqt_spin) |
7140 | timeout = jiffies + sqd->sq_thread_idle; | |
7141 | continue; | |
7142 | } | |
7143 | ||
7144 | if (kthread_should_park()) | |
7145 | continue; | |
7146 | ||
7147 | needs_sched = true; | |
7148 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7149 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7150 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7151 | !list_empty_careful(&ctx->iopoll_list)) { | |
7152 | needs_sched = false; | |
7153 | break; | |
7154 | } | |
7155 | if (io_sqring_entries(ctx)) { | |
7156 | needs_sched = false; | |
7157 | break; | |
7158 | } | |
7159 | } | |
7160 | ||
7161 | if (needs_sched) { | |
69fb2131 JA |
7162 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7163 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7164 | |
69fb2131 | 7165 | schedule(); |
69fb2131 JA |
7166 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7167 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7168 | } |
08369246 XW |
7169 | |
7170 | finish_wait(&sqd->wait, &wait); | |
7171 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7172 | } |
7173 | ||
4c6e277c | 7174 | io_run_task_work(); |
d434ab6d | 7175 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7176 | |
91d8f519 DZ |
7177 | if (cur_css) |
7178 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7179 | if (old_cred) |
7180 | revert_creds(old_cred); | |
06058632 | 7181 | |
28cea78a JA |
7182 | task_lock(current); |
7183 | current->files = old_files; | |
7184 | current->nsproxy = old_nsproxy; | |
7185 | task_unlock(current); | |
7186 | ||
2bbcd6d3 | 7187 | kthread_parkme(); |
06058632 | 7188 | |
6c271ce2 JA |
7189 | return 0; |
7190 | } | |
7191 | ||
bda52162 JA |
7192 | struct io_wait_queue { |
7193 | struct wait_queue_entry wq; | |
7194 | struct io_ring_ctx *ctx; | |
7195 | unsigned to_wait; | |
7196 | unsigned nr_timeouts; | |
7197 | }; | |
7198 | ||
6c503150 | 7199 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7200 | { |
7201 | struct io_ring_ctx *ctx = iowq->ctx; | |
7202 | ||
7203 | /* | |
d195a66e | 7204 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7205 | * started waiting. For timeouts, we always want to return to userspace, |
7206 | * regardless of event count. | |
7207 | */ | |
6c503150 | 7208 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7209 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7210 | } | |
7211 | ||
7212 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7213 | int wake_flags, void *key) | |
7214 | { | |
7215 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7216 | wq); | |
7217 | ||
6c503150 PB |
7218 | /* |
7219 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7220 | * the task, and the next invocation will do it. | |
7221 | */ | |
7222 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7223 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7224 | return -1; | |
bda52162 JA |
7225 | } |
7226 | ||
af9c1a44 JA |
7227 | static int io_run_task_work_sig(void) |
7228 | { | |
7229 | if (io_run_task_work()) | |
7230 | return 1; | |
7231 | if (!signal_pending(current)) | |
7232 | return 0; | |
792ee0f6 JA |
7233 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7234 | return -ERESTARTSYS; | |
af9c1a44 JA |
7235 | return -EINTR; |
7236 | } | |
7237 | ||
2b188cc1 JA |
7238 | /* |
7239 | * Wait until events become available, if we don't already have some. The | |
7240 | * application must reap them itself, as they reside on the shared cq ring. | |
7241 | */ | |
7242 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7243 | const sigset_t __user *sig, size_t sigsz, |
7244 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7245 | { |
bda52162 JA |
7246 | struct io_wait_queue iowq = { |
7247 | .wq = { | |
7248 | .private = current, | |
7249 | .func = io_wake_function, | |
7250 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7251 | }, | |
7252 | .ctx = ctx, | |
7253 | .to_wait = min_events, | |
7254 | }; | |
75b28aff | 7255 | struct io_rings *rings = ctx->rings; |
c73ebb68 HX |
7256 | struct timespec64 ts; |
7257 | signed long timeout = 0; | |
e9ffa5c2 | 7258 | int ret = 0; |
2b188cc1 | 7259 | |
b41e9852 | 7260 | do { |
6c503150 PB |
7261 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7262 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7263 | return 0; |
4c6e277c | 7264 | if (!io_run_task_work()) |
b41e9852 | 7265 | break; |
b41e9852 | 7266 | } while (1); |
2b188cc1 JA |
7267 | |
7268 | if (sig) { | |
9e75ad5d AB |
7269 | #ifdef CONFIG_COMPAT |
7270 | if (in_compat_syscall()) | |
7271 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7272 | sigsz); |
9e75ad5d AB |
7273 | else |
7274 | #endif | |
b772434b | 7275 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7276 | |
2b188cc1 JA |
7277 | if (ret) |
7278 | return ret; | |
7279 | } | |
7280 | ||
c73ebb68 HX |
7281 | if (uts) { |
7282 | if (get_timespec64(&ts, uts)) | |
7283 | return -EFAULT; | |
7284 | timeout = timespec64_to_jiffies(&ts); | |
7285 | } | |
7286 | ||
bda52162 | 7287 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7288 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7289 | do { |
6c503150 | 7290 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7291 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7292 | TASK_INTERRUPTIBLE); | |
ce593a6c | 7293 | /* make sure we run task_work before checking for signals */ |
af9c1a44 | 7294 | ret = io_run_task_work_sig(); |
6195ba09 HX |
7295 | if (ret > 0) { |
7296 | finish_wait(&ctx->wait, &iowq.wq); | |
4c6e277c | 7297 | continue; |
6195ba09 | 7298 | } |
af9c1a44 | 7299 | else if (ret < 0) |
bda52162 | 7300 | break; |
6c503150 | 7301 | if (io_should_wake(&iowq)) |
ce593a6c | 7302 | break; |
6195ba09 HX |
7303 | if (test_bit(0, &ctx->cq_check_overflow)) { |
7304 | finish_wait(&ctx->wait, &iowq.wq); | |
6c503150 | 7305 | continue; |
6195ba09 | 7306 | } |
c73ebb68 HX |
7307 | if (uts) { |
7308 | timeout = schedule_timeout(timeout); | |
7309 | if (timeout == 0) { | |
7310 | ret = -ETIME; | |
7311 | break; | |
7312 | } | |
7313 | } else { | |
7314 | schedule(); | |
7315 | } | |
bda52162 JA |
7316 | } while (1); |
7317 | finish_wait(&ctx->wait, &iowq.wq); | |
7318 | ||
b7db41c9 | 7319 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7320 | |
75b28aff | 7321 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7322 | } |
7323 | ||
6b06314c JA |
7324 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7325 | { | |
7326 | #if defined(CONFIG_UNIX) | |
7327 | if (ctx->ring_sock) { | |
7328 | struct sock *sock = ctx->ring_sock->sk; | |
7329 | struct sk_buff *skb; | |
7330 | ||
7331 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7332 | kfree_skb(skb); | |
7333 | } | |
7334 | #else | |
7335 | int i; | |
7336 | ||
65e19f54 JA |
7337 | for (i = 0; i < ctx->nr_user_files; i++) { |
7338 | struct file *file; | |
7339 | ||
7340 | file = io_file_from_index(ctx, i); | |
7341 | if (file) | |
7342 | fput(file); | |
7343 | } | |
6b06314c JA |
7344 | #endif |
7345 | } | |
7346 | ||
269bbe5f | 7347 | static void io_rsrc_ref_kill(struct percpu_ref *ref) |
05f3fb3c | 7348 | { |
269bbe5f | 7349 | struct fixed_rsrc_data *data; |
05f3fb3c | 7350 | |
269bbe5f | 7351 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7352 | complete(&data->done); |
7353 | } | |
7354 | ||
2a63b2d9 BM |
7355 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
7356 | { | |
7357 | spin_lock_bh(&ctx->rsrc_ref_lock); | |
7358 | } | |
7359 | ||
7360 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) | |
7361 | { | |
7362 | spin_unlock_bh(&ctx->rsrc_ref_lock); | |
7363 | } | |
7364 | ||
d67d2263 BM |
7365 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7366 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7367 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7368 | { |
2a63b2d9 | 7369 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7370 | rsrc_data->node = ref_node; |
d67d2263 | 7371 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7372 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7373 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7374 | } |
7375 | ||
d7954b2b BM |
7376 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7377 | struct io_ring_ctx *ctx, | |
7378 | struct fixed_rsrc_ref_node *backup_node) | |
6b06314c | 7379 | { |
d7954b2b | 7380 | struct fixed_rsrc_ref_node *ref_node; |
1ffc5422 | 7381 | int ret; |
65e19f54 | 7382 | |
2a63b2d9 | 7383 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7384 | ref_node = data->node; |
2a63b2d9 | 7385 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7386 | if (ref_node) |
7387 | percpu_ref_kill(&ref_node->refs); | |
7388 | ||
7389 | percpu_ref_kill(&data->refs); | |
7390 | ||
7391 | /* wait for all refs nodes to complete */ | |
269bbe5f | 7392 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 PB |
7393 | do { |
7394 | ret = wait_for_completion_interruptible(&data->done); | |
7395 | if (!ret) | |
7396 | break; | |
7397 | ret = io_run_task_work_sig(); | |
7398 | if (ret < 0) { | |
7399 | percpu_ref_resurrect(&data->refs); | |
7400 | reinit_completion(&data->done); | |
d67d2263 | 7401 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
1ffc5422 PB |
7402 | return ret; |
7403 | } | |
7404 | } while (1); | |
05f3fb3c | 7405 | |
d7954b2b BM |
7406 | destroy_fixed_rsrc_ref_node(backup_node); |
7407 | return 0; | |
7408 | } | |
7409 | ||
7410 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) | |
7411 | { | |
7412 | struct fixed_rsrc_data *data = ctx->file_data; | |
7413 | struct fixed_rsrc_ref_node *backup_node; | |
7414 | unsigned nr_tables, i; | |
7415 | int ret; | |
7416 | ||
7417 | if (!data) | |
7418 | return -ENXIO; | |
7419 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7420 | if (!backup_node) | |
7421 | return -ENOMEM; | |
7422 | init_fixed_file_ref_node(ctx, backup_node); | |
7423 | ||
7424 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); | |
7425 | if (ret) | |
7426 | return ret; | |
7427 | ||
6b06314c | 7428 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7429 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7430 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
7431 | kfree(data->table[i].files); |
7432 | kfree(data->table); | |
05589553 XW |
7433 | percpu_ref_exit(&data->refs); |
7434 | kfree(data); | |
05f3fb3c | 7435 | ctx->file_data = NULL; |
6b06314c JA |
7436 | ctx->nr_user_files = 0; |
7437 | return 0; | |
7438 | } | |
7439 | ||
534ca6d6 | 7440 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7441 | { |
534ca6d6 | 7442 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7443 | /* |
7444 | * The park is a bit of a work-around, without it we get | |
7445 | * warning spews on shutdown with SQPOLL set and affinity | |
7446 | * set to a single CPU. | |
7447 | */ | |
534ca6d6 JA |
7448 | if (sqd->thread) { |
7449 | kthread_park(sqd->thread); | |
7450 | kthread_stop(sqd->thread); | |
7451 | } | |
7452 | ||
7453 | kfree(sqd); | |
7454 | } | |
7455 | } | |
7456 | ||
aa06165d JA |
7457 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7458 | { | |
7459 | struct io_ring_ctx *ctx_attach; | |
7460 | struct io_sq_data *sqd; | |
7461 | struct fd f; | |
7462 | ||
7463 | f = fdget(p->wq_fd); | |
7464 | if (!f.file) | |
7465 | return ERR_PTR(-ENXIO); | |
7466 | if (f.file->f_op != &io_uring_fops) { | |
7467 | fdput(f); | |
7468 | return ERR_PTR(-EINVAL); | |
7469 | } | |
7470 | ||
7471 | ctx_attach = f.file->private_data; | |
7472 | sqd = ctx_attach->sq_data; | |
7473 | if (!sqd) { | |
7474 | fdput(f); | |
7475 | return ERR_PTR(-EINVAL); | |
7476 | } | |
7477 | ||
7478 | refcount_inc(&sqd->refs); | |
7479 | fdput(f); | |
7480 | return sqd; | |
7481 | } | |
7482 | ||
534ca6d6 JA |
7483 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7484 | { | |
7485 | struct io_sq_data *sqd; | |
7486 | ||
aa06165d JA |
7487 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7488 | return io_attach_sq_data(p); | |
7489 | ||
534ca6d6 JA |
7490 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7491 | if (!sqd) | |
7492 | return ERR_PTR(-ENOMEM); | |
7493 | ||
7494 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7495 | INIT_LIST_HEAD(&sqd->ctx_list); |
7496 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7497 | mutex_init(&sqd->ctx_lock); | |
7498 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7499 | init_waitqueue_head(&sqd->wait); |
7500 | return sqd; | |
7501 | } | |
7502 | ||
69fb2131 JA |
7503 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7504 | __releases(&sqd->lock) | |
7505 | { | |
7506 | if (!sqd->thread) | |
7507 | return; | |
7508 | kthread_unpark(sqd->thread); | |
7509 | mutex_unlock(&sqd->lock); | |
7510 | } | |
7511 | ||
7512 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7513 | __acquires(&sqd->lock) | |
7514 | { | |
7515 | if (!sqd->thread) | |
7516 | return; | |
7517 | mutex_lock(&sqd->lock); | |
7518 | kthread_park(sqd->thread); | |
7519 | } | |
7520 | ||
534ca6d6 JA |
7521 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7522 | { | |
7523 | struct io_sq_data *sqd = ctx->sq_data; | |
7524 | ||
7525 | if (sqd) { | |
7526 | if (sqd->thread) { | |
7527 | /* | |
7528 | * We may arrive here from the error branch in | |
7529 | * io_sq_offload_create() where the kthread is created | |
7530 | * without being waked up, thus wake it up now to make | |
7531 | * sure the wait will complete. | |
7532 | */ | |
7533 | wake_up_process(sqd->thread); | |
7534 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7535 | |
7536 | io_sq_thread_park(sqd); | |
7537 | } | |
7538 | ||
7539 | mutex_lock(&sqd->ctx_lock); | |
7540 | list_del(&ctx->sqd_list); | |
08369246 | 7541 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7542 | mutex_unlock(&sqd->ctx_lock); |
7543 | ||
08369246 | 7544 | if (sqd->thread) |
69fb2131 | 7545 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7546 | |
7547 | io_put_sq_data(sqd); | |
7548 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7549 | } |
7550 | } | |
7551 | ||
6b06314c JA |
7552 | static void io_finish_async(struct io_ring_ctx *ctx) |
7553 | { | |
6c271ce2 JA |
7554 | io_sq_thread_stop(ctx); |
7555 | ||
561fb04a JA |
7556 | if (ctx->io_wq) { |
7557 | io_wq_destroy(ctx->io_wq); | |
7558 | ctx->io_wq = NULL; | |
6b06314c JA |
7559 | } |
7560 | } | |
7561 | ||
7562 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7563 | /* |
7564 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7565 | * the io_uring can be safely unregistered on process exit, even if we have | |
7566 | * loops in the file referencing. | |
7567 | */ | |
7568 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7569 | { | |
7570 | struct sock *sk = ctx->ring_sock->sk; | |
7571 | struct scm_fp_list *fpl; | |
7572 | struct sk_buff *skb; | |
08a45173 | 7573 | int i, nr_files; |
6b06314c | 7574 | |
6b06314c JA |
7575 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7576 | if (!fpl) | |
7577 | return -ENOMEM; | |
7578 | ||
7579 | skb = alloc_skb(0, GFP_KERNEL); | |
7580 | if (!skb) { | |
7581 | kfree(fpl); | |
7582 | return -ENOMEM; | |
7583 | } | |
7584 | ||
7585 | skb->sk = sk; | |
6b06314c | 7586 | |
08a45173 | 7587 | nr_files = 0; |
6b06314c JA |
7588 | fpl->user = get_uid(ctx->user); |
7589 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7590 | struct file *file = io_file_from_index(ctx, i + offset); |
7591 | ||
7592 | if (!file) | |
08a45173 | 7593 | continue; |
65e19f54 | 7594 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7595 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7596 | nr_files++; | |
6b06314c JA |
7597 | } |
7598 | ||
08a45173 JA |
7599 | if (nr_files) { |
7600 | fpl->max = SCM_MAX_FD; | |
7601 | fpl->count = nr_files; | |
7602 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7603 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7604 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7605 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7606 | |
08a45173 JA |
7607 | for (i = 0; i < nr_files; i++) |
7608 | fput(fpl->fp[i]); | |
7609 | } else { | |
7610 | kfree_skb(skb); | |
7611 | kfree(fpl); | |
7612 | } | |
6b06314c JA |
7613 | |
7614 | return 0; | |
7615 | } | |
7616 | ||
7617 | /* | |
7618 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7619 | * causes regular reference counting to break down. We rely on the UNIX | |
7620 | * garbage collection to take care of this problem for us. | |
7621 | */ | |
7622 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7623 | { | |
7624 | unsigned left, total; | |
7625 | int ret = 0; | |
7626 | ||
7627 | total = 0; | |
7628 | left = ctx->nr_user_files; | |
7629 | while (left) { | |
7630 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7631 | |
7632 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7633 | if (ret) | |
7634 | break; | |
7635 | left -= this_files; | |
7636 | total += this_files; | |
7637 | } | |
7638 | ||
7639 | if (!ret) | |
7640 | return 0; | |
7641 | ||
7642 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7643 | struct file *file = io_file_from_index(ctx, total); |
7644 | ||
7645 | if (file) | |
7646 | fput(file); | |
6b06314c JA |
7647 | total++; |
7648 | } | |
7649 | ||
7650 | return ret; | |
7651 | } | |
7652 | #else | |
7653 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7654 | { | |
7655 | return 0; | |
7656 | } | |
7657 | #endif | |
7658 | ||
269bbe5f | 7659 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7660 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7661 | { |
7662 | int i; | |
7663 | ||
7664 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7665 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7666 | unsigned this_files; |
7667 | ||
7668 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7669 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7670 | GFP_KERNEL); | |
7671 | if (!table->files) | |
7672 | break; | |
7673 | nr_files -= this_files; | |
7674 | } | |
7675 | ||
7676 | if (i == nr_tables) | |
7677 | return 0; | |
7678 | ||
7679 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7680 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7681 | kfree(table->files); |
7682 | } | |
7683 | return 1; | |
7684 | } | |
7685 | ||
50238531 | 7686 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7687 | { |
50238531 | 7688 | struct file *file = prsrc->file; |
05f3fb3c JA |
7689 | #if defined(CONFIG_UNIX) |
7690 | struct sock *sock = ctx->ring_sock->sk; | |
7691 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7692 | struct sk_buff *skb; | |
7693 | int i; | |
7694 | ||
7695 | __skb_queue_head_init(&list); | |
7696 | ||
7697 | /* | |
7698 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7699 | * remove this entry and rearrange the file array. | |
7700 | */ | |
7701 | skb = skb_dequeue(head); | |
7702 | while (skb) { | |
7703 | struct scm_fp_list *fp; | |
7704 | ||
7705 | fp = UNIXCB(skb).fp; | |
7706 | for (i = 0; i < fp->count; i++) { | |
7707 | int left; | |
7708 | ||
7709 | if (fp->fp[i] != file) | |
7710 | continue; | |
7711 | ||
7712 | unix_notinflight(fp->user, fp->fp[i]); | |
7713 | left = fp->count - 1 - i; | |
7714 | if (left) { | |
7715 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7716 | left * sizeof(struct file *)); | |
7717 | } | |
7718 | fp->count--; | |
7719 | if (!fp->count) { | |
7720 | kfree_skb(skb); | |
7721 | skb = NULL; | |
7722 | } else { | |
7723 | __skb_queue_tail(&list, skb); | |
7724 | } | |
7725 | fput(file); | |
7726 | file = NULL; | |
7727 | break; | |
7728 | } | |
7729 | ||
7730 | if (!file) | |
7731 | break; | |
7732 | ||
7733 | __skb_queue_tail(&list, skb); | |
7734 | ||
7735 | skb = skb_dequeue(head); | |
7736 | } | |
7737 | ||
7738 | if (skb_peek(&list)) { | |
7739 | spin_lock_irq(&head->lock); | |
7740 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7741 | __skb_queue_tail(head, skb); | |
7742 | spin_unlock_irq(&head->lock); | |
7743 | } | |
7744 | #else | |
7745 | fput(file); | |
7746 | #endif | |
7747 | } | |
7748 | ||
269bbe5f | 7749 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7750 | { |
269bbe5f BM |
7751 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7752 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7753 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7754 | |
269bbe5f BM |
7755 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7756 | list_del(&prsrc->list); | |
50238531 | 7757 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7758 | kfree(prsrc); |
65e19f54 | 7759 | } |
05589553 | 7760 | |
05589553 XW |
7761 | percpu_ref_exit(&ref_node->refs); |
7762 | kfree(ref_node); | |
269bbe5f | 7763 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7764 | } |
65e19f54 | 7765 | |
269bbe5f | 7766 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7767 | { |
7768 | struct io_ring_ctx *ctx; | |
7769 | struct llist_node *node; | |
7770 | ||
269bbe5f BM |
7771 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7772 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7773 | |
7774 | while (node) { | |
269bbe5f | 7775 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7776 | struct llist_node *next = node->next; |
7777 | ||
269bbe5f BM |
7778 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7779 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7780 | node = next; |
7781 | } | |
7782 | } | |
7783 | ||
269bbe5f | 7784 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
2faf852d | 7785 | { |
269bbe5f BM |
7786 | struct fixed_rsrc_ref_node *ref_node; |
7787 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7788 | struct io_ring_ctx *ctx; |
e297822b | 7789 | bool first_add = false; |
4a38aed2 | 7790 | int delay = HZ; |
65e19f54 | 7791 | |
269bbe5f BM |
7792 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7793 | data = ref_node->rsrc_data; | |
e297822b PB |
7794 | ctx = data->ctx; |
7795 | ||
2a63b2d9 | 7796 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7797 | ref_node->done = true; |
7798 | ||
d67d2263 BM |
7799 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7800 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7801 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7802 | /* recycle ref nodes in order */ |
7803 | if (!ref_node->done) | |
7804 | break; | |
7805 | list_del(&ref_node->node); | |
269bbe5f | 7806 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7807 | } |
2a63b2d9 | 7808 | io_rsrc_ref_unlock(ctx); |
05589553 | 7809 | |
e297822b | 7810 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7811 | delay = 0; |
05589553 | 7812 | |
4a38aed2 | 7813 | if (!delay) |
269bbe5f | 7814 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7815 | else if (first_add) |
269bbe5f | 7816 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7817 | } |
65e19f54 | 7818 | |
6802535d | 7819 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7820 | struct io_ring_ctx *ctx) |
05f3fb3c | 7821 | { |
269bbe5f | 7822 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7823 | |
05589553 XW |
7824 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7825 | if (!ref_node) | |
3e2224c5 | 7826 | return NULL; |
05f3fb3c | 7827 | |
269bbe5f | 7828 | if (percpu_ref_init(&ref_node->refs, io_rsrc_data_ref_zero, |
05589553 XW |
7829 | 0, GFP_KERNEL)) { |
7830 | kfree(ref_node); | |
3e2224c5 | 7831 | return NULL; |
05589553 XW |
7832 | } |
7833 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7834 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
6802535d BM |
7835 | ref_node->done = false; |
7836 | return ref_node; | |
7837 | } | |
7838 | ||
bc9744cd PB |
7839 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7840 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7841 | { |
269bbe5f | 7842 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7843 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7844 | } |
7845 | ||
269bbe5f | 7846 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7847 | { |
7848 | percpu_ref_exit(&ref_node->refs); | |
7849 | kfree(ref_node); | |
65e19f54 JA |
7850 | } |
7851 | ||
6b06314c JA |
7852 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7853 | unsigned nr_args) | |
7854 | { | |
7855 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7856 | unsigned nr_tables, i; |
05f3fb3c | 7857 | struct file *file; |
600cf3f8 | 7858 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7859 | struct fixed_rsrc_ref_node *ref_node; |
7860 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7861 | |
05f3fb3c | 7862 | if (ctx->file_data) |
6b06314c JA |
7863 | return -EBUSY; |
7864 | if (!nr_args) | |
7865 | return -EINVAL; | |
7866 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7867 | return -EMFILE; | |
7868 | ||
5398ae69 PB |
7869 | file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
7870 | if (!file_data) | |
05f3fb3c | 7871 | return -ENOMEM; |
5398ae69 PB |
7872 | file_data->ctx = ctx; |
7873 | init_completion(&file_data->done); | |
05f3fb3c | 7874 | |
65e19f54 | 7875 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7876 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7877 | GFP_KERNEL); |
600cf3f8 PB |
7878 | if (!file_data->table) |
7879 | goto out_free; | |
05f3fb3c | 7880 | |
269bbe5f | 7881 | if (percpu_ref_init(&file_data->refs, io_rsrc_ref_kill, |
600cf3f8 PB |
7882 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
7883 | goto out_free; | |
6b06314c | 7884 | |
600cf3f8 PB |
7885 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
7886 | goto out_ref; | |
55cbc256 | 7887 | ctx->file_data = file_data; |
65e19f54 | 7888 | |
08a45173 | 7889 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
269bbe5f | 7890 | struct fixed_rsrc_table *table; |
65e19f54 JA |
7891 | unsigned index; |
7892 | ||
600cf3f8 PB |
7893 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7894 | ret = -EFAULT; | |
7895 | goto out_fput; | |
7896 | } | |
08a45173 | 7897 | /* allow sparse sets */ |
600cf3f8 | 7898 | if (fd == -1) |
08a45173 | 7899 | continue; |
6b06314c | 7900 | |
05f3fb3c | 7901 | file = fget(fd); |
6b06314c | 7902 | ret = -EBADF; |
05f3fb3c | 7903 | if (!file) |
600cf3f8 | 7904 | goto out_fput; |
05f3fb3c | 7905 | |
6b06314c JA |
7906 | /* |
7907 | * Don't allow io_uring instances to be registered. If UNIX | |
7908 | * isn't enabled, then this causes a reference cycle and this | |
7909 | * instance can never get freed. If UNIX is enabled we'll | |
7910 | * handle it just fine, but there's still no point in allowing | |
7911 | * a ring fd as it doesn't support regular read/write anyway. | |
7912 | */ | |
05f3fb3c JA |
7913 | if (file->f_op == &io_uring_fops) { |
7914 | fput(file); | |
600cf3f8 | 7915 | goto out_fput; |
6b06314c | 7916 | } |
600cf3f8 PB |
7917 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
7918 | index = i & IORING_FILE_TABLE_MASK; | |
05f3fb3c | 7919 | table->files[index] = file; |
6b06314c JA |
7920 | } |
7921 | ||
6b06314c | 7922 | ret = io_sqe_files_scm(ctx); |
05589553 | 7923 | if (ret) { |
6b06314c | 7924 | io_sqe_files_unregister(ctx); |
05589553 XW |
7925 | return ret; |
7926 | } | |
6b06314c | 7927 | |
bc9744cd | 7928 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7929 | if (!ref_node) { |
05589553 | 7930 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7931 | return -ENOMEM; |
05589553 | 7932 | } |
bc9744cd | 7933 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7934 | |
d67d2263 | 7935 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7936 | return ret; |
600cf3f8 PB |
7937 | out_fput: |
7938 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7939 | file = io_file_from_index(ctx, i); | |
7940 | if (file) | |
7941 | fput(file); | |
7942 | } | |
7943 | for (i = 0; i < nr_tables; i++) | |
7944 | kfree(file_data->table[i].files); | |
7945 | ctx->nr_user_files = 0; | |
7946 | out_ref: | |
7947 | percpu_ref_exit(&file_data->refs); | |
7948 | out_free: | |
7949 | kfree(file_data->table); | |
7950 | kfree(file_data); | |
55cbc256 | 7951 | ctx->file_data = NULL; |
6b06314c JA |
7952 | return ret; |
7953 | } | |
7954 | ||
c3a31e60 JA |
7955 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7956 | int index) | |
7957 | { | |
7958 | #if defined(CONFIG_UNIX) | |
7959 | struct sock *sock = ctx->ring_sock->sk; | |
7960 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7961 | struct sk_buff *skb; | |
7962 | ||
7963 | /* | |
7964 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7965 | * file set. If there's no room, fall back to allocating a new skb | |
7966 | * and filling it in. | |
7967 | */ | |
7968 | spin_lock_irq(&head->lock); | |
7969 | skb = skb_peek(head); | |
7970 | if (skb) { | |
7971 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7972 | ||
7973 | if (fpl->count < SCM_MAX_FD) { | |
7974 | __skb_unlink(skb, head); | |
7975 | spin_unlock_irq(&head->lock); | |
7976 | fpl->fp[fpl->count] = get_file(file); | |
7977 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7978 | fpl->count++; | |
7979 | spin_lock_irq(&head->lock); | |
7980 | __skb_queue_head(head, skb); | |
7981 | } else { | |
7982 | skb = NULL; | |
7983 | } | |
7984 | } | |
7985 | spin_unlock_irq(&head->lock); | |
7986 | ||
7987 | if (skb) { | |
7988 | fput(file); | |
7989 | return 0; | |
7990 | } | |
7991 | ||
7992 | return __io_sqe_files_scm(ctx, 1, index); | |
7993 | #else | |
7994 | return 0; | |
7995 | #endif | |
7996 | } | |
7997 | ||
50238531 | 7998 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7999 | { |
269bbe5f BM |
8000 | struct io_rsrc_put *prsrc; |
8001 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 8002 | |
269bbe5f BM |
8003 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
8004 | if (!prsrc) | |
a5318d3c | 8005 | return -ENOMEM; |
05f3fb3c | 8006 | |
50238531 | 8007 | prsrc->rsrc = rsrc; |
269bbe5f | 8008 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 8009 | |
a5318d3c | 8010 | return 0; |
05f3fb3c JA |
8011 | } |
8012 | ||
269bbe5f BM |
8013 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
8014 | struct file *file) | |
8015 | { | |
50238531 | 8016 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
8017 | } |
8018 | ||
05f3fb3c | 8019 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 8020 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
8021 | unsigned nr_args) |
8022 | { | |
269bbe5f BM |
8023 | struct fixed_rsrc_data *data = ctx->file_data; |
8024 | struct fixed_rsrc_ref_node *ref_node; | |
05f3fb3c | 8025 | struct file *file; |
c3a31e60 JA |
8026 | __s32 __user *fds; |
8027 | int fd, i, err; | |
8028 | __u32 done; | |
05589553 | 8029 | bool needs_switch = false; |
c3a31e60 | 8030 | |
05f3fb3c | 8031 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
8032 | return -EOVERFLOW; |
8033 | if (done > ctx->nr_user_files) | |
8034 | return -EINVAL; | |
8035 | ||
bc9744cd | 8036 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
8037 | if (!ref_node) |
8038 | return -ENOMEM; | |
bc9744cd | 8039 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 8040 | |
c3a31e60 | 8041 | done = 0; |
269bbe5f | 8042 | fds = u64_to_user_ptr(up->data); |
c3a31e60 | 8043 | while (nr_args) { |
269bbe5f | 8044 | struct fixed_rsrc_table *table; |
65e19f54 JA |
8045 | unsigned index; |
8046 | ||
c3a31e60 JA |
8047 | err = 0; |
8048 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
8049 | err = -EFAULT; | |
8050 | break; | |
8051 | } | |
05f3fb3c JA |
8052 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
8053 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
8054 | index = i & IORING_FILE_TABLE_MASK; |
8055 | if (table->files[index]) { | |
98dfd502 | 8056 | file = table->files[index]; |
a5318d3c HD |
8057 | err = io_queue_file_removal(data, file); |
8058 | if (err) | |
8059 | break; | |
65e19f54 | 8060 | table->files[index] = NULL; |
05589553 | 8061 | needs_switch = true; |
c3a31e60 JA |
8062 | } |
8063 | if (fd != -1) { | |
c3a31e60 JA |
8064 | file = fget(fd); |
8065 | if (!file) { | |
8066 | err = -EBADF; | |
8067 | break; | |
8068 | } | |
8069 | /* | |
8070 | * Don't allow io_uring instances to be registered. If | |
8071 | * UNIX isn't enabled, then this causes a reference | |
8072 | * cycle and this instance can never get freed. If UNIX | |
8073 | * is enabled we'll handle it just fine, but there's | |
8074 | * still no point in allowing a ring fd as it doesn't | |
8075 | * support regular read/write anyway. | |
8076 | */ | |
8077 | if (file->f_op == &io_uring_fops) { | |
8078 | fput(file); | |
8079 | err = -EBADF; | |
8080 | break; | |
8081 | } | |
65e19f54 | 8082 | table->files[index] = file; |
c3a31e60 | 8083 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8084 | if (err) { |
95d1c8e5 | 8085 | table->files[index] = NULL; |
f3bd9dae | 8086 | fput(file); |
c3a31e60 | 8087 | break; |
f3bd9dae | 8088 | } |
c3a31e60 JA |
8089 | } |
8090 | nr_args--; | |
8091 | done++; | |
05f3fb3c JA |
8092 | up->offset++; |
8093 | } | |
8094 | ||
05589553 | 8095 | if (needs_switch) { |
b2e96852 | 8096 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 8097 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 8098 | } else |
269bbe5f | 8099 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
8100 | |
8101 | return done ? done : err; | |
8102 | } | |
05589553 | 8103 | |
05f3fb3c JA |
8104 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
8105 | unsigned nr_args) | |
8106 | { | |
269bbe5f | 8107 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
8108 | |
8109 | if (!ctx->file_data) | |
8110 | return -ENXIO; | |
8111 | if (!nr_args) | |
8112 | return -EINVAL; | |
8113 | if (copy_from_user(&up, arg, sizeof(up))) | |
8114 | return -EFAULT; | |
8115 | if (up.resv) | |
8116 | return -EINVAL; | |
8117 | ||
8118 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8119 | } | |
c3a31e60 | 8120 | |
e9fd9396 | 8121 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
8122 | { |
8123 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8124 | ||
e9fd9396 | 8125 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
8126 | io_put_req(req); |
8127 | } | |
8128 | ||
24369c2e PB |
8129 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
8130 | struct io_uring_params *p) | |
8131 | { | |
8132 | struct io_wq_data data; | |
8133 | struct fd f; | |
8134 | struct io_ring_ctx *ctx_attach; | |
8135 | unsigned int concurrency; | |
8136 | int ret = 0; | |
8137 | ||
8138 | data.user = ctx->user; | |
e9fd9396 | 8139 | data.free_work = io_free_work; |
f5fa38c5 | 8140 | data.do_work = io_wq_submit_work; |
24369c2e PB |
8141 | |
8142 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
8143 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
8144 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
8145 | ||
8146 | ctx->io_wq = io_wq_create(concurrency, &data); | |
8147 | if (IS_ERR(ctx->io_wq)) { | |
8148 | ret = PTR_ERR(ctx->io_wq); | |
8149 | ctx->io_wq = NULL; | |
8150 | } | |
8151 | return ret; | |
8152 | } | |
8153 | ||
8154 | f = fdget(p->wq_fd); | |
8155 | if (!f.file) | |
8156 | return -EBADF; | |
8157 | ||
8158 | if (f.file->f_op != &io_uring_fops) { | |
8159 | ret = -EINVAL; | |
8160 | goto out_fput; | |
8161 | } | |
8162 | ||
8163 | ctx_attach = f.file->private_data; | |
8164 | /* @io_wq is protected by holding the fd */ | |
8165 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8166 | ret = -EINVAL; | |
8167 | goto out_fput; | |
8168 | } | |
8169 | ||
8170 | ctx->io_wq = ctx_attach->io_wq; | |
8171 | out_fput: | |
8172 | fdput(f); | |
8173 | return ret; | |
8174 | } | |
8175 | ||
0f212204 JA |
8176 | static int io_uring_alloc_task_context(struct task_struct *task) |
8177 | { | |
8178 | struct io_uring_task *tctx; | |
d8a6df10 | 8179 | int ret; |
0f212204 JA |
8180 | |
8181 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8182 | if (unlikely(!tctx)) | |
8183 | return -ENOMEM; | |
8184 | ||
d8a6df10 JA |
8185 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8186 | if (unlikely(ret)) { | |
8187 | kfree(tctx); | |
8188 | return ret; | |
8189 | } | |
8190 | ||
0f212204 JA |
8191 | xa_init(&tctx->xa); |
8192 | init_waitqueue_head(&tctx->wait); | |
8193 | tctx->last = NULL; | |
fdaf083c JA |
8194 | atomic_set(&tctx->in_idle, 0); |
8195 | tctx->sqpoll = false; | |
500a373d JA |
8196 | io_init_identity(&tctx->__identity); |
8197 | tctx->identity = &tctx->__identity; | |
0f212204 JA |
8198 | task->io_uring = tctx; |
8199 | return 0; | |
8200 | } | |
8201 | ||
8202 | void __io_uring_free(struct task_struct *tsk) | |
8203 | { | |
8204 | struct io_uring_task *tctx = tsk->io_uring; | |
8205 | ||
8206 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8207 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8208 | if (tctx->identity != &tctx->__identity) | |
8209 | kfree(tctx->identity); | |
d8a6df10 | 8210 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8211 | kfree(tctx); |
8212 | tsk->io_uring = NULL; | |
8213 | } | |
8214 | ||
7e84e1c7 SG |
8215 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8216 | struct io_uring_params *p) | |
2b188cc1 JA |
8217 | { |
8218 | int ret; | |
8219 | ||
6c271ce2 | 8220 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8221 | struct io_sq_data *sqd; |
8222 | ||
3ec482d1 | 8223 | ret = -EPERM; |
ce59fc69 | 8224 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8225 | goto err; |
8226 | ||
534ca6d6 JA |
8227 | sqd = io_get_sq_data(p); |
8228 | if (IS_ERR(sqd)) { | |
8229 | ret = PTR_ERR(sqd); | |
8230 | goto err; | |
8231 | } | |
69fb2131 | 8232 | |
534ca6d6 | 8233 | ctx->sq_data = sqd; |
69fb2131 JA |
8234 | io_sq_thread_park(sqd); |
8235 | mutex_lock(&sqd->ctx_lock); | |
8236 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8237 | mutex_unlock(&sqd->ctx_lock); | |
8238 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8239 | |
917257da JA |
8240 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8241 | if (!ctx->sq_thread_idle) | |
8242 | ctx->sq_thread_idle = HZ; | |
8243 | ||
aa06165d JA |
8244 | if (sqd->thread) |
8245 | goto done; | |
8246 | ||
6c271ce2 | 8247 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8248 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8249 | |
917257da | 8250 | ret = -EINVAL; |
44a9bd18 JA |
8251 | if (cpu >= nr_cpu_ids) |
8252 | goto err; | |
7889f44d | 8253 | if (!cpu_online(cpu)) |
917257da JA |
8254 | goto err; |
8255 | ||
69fb2131 | 8256 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8257 | cpu, "io_uring-sq"); |
6c271ce2 | 8258 | } else { |
69fb2131 | 8259 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8260 | "io_uring-sq"); |
8261 | } | |
534ca6d6 JA |
8262 | if (IS_ERR(sqd->thread)) { |
8263 | ret = PTR_ERR(sqd->thread); | |
8264 | sqd->thread = NULL; | |
6c271ce2 JA |
8265 | goto err; |
8266 | } | |
534ca6d6 | 8267 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8268 | if (ret) |
8269 | goto err; | |
6c271ce2 JA |
8270 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8271 | /* Can't have SQ_AFF without SQPOLL */ | |
8272 | ret = -EINVAL; | |
8273 | goto err; | |
8274 | } | |
8275 | ||
aa06165d | 8276 | done: |
24369c2e PB |
8277 | ret = io_init_wq_offload(ctx, p); |
8278 | if (ret) | |
2b188cc1 | 8279 | goto err; |
2b188cc1 JA |
8280 | |
8281 | return 0; | |
8282 | err: | |
54a91f3b | 8283 | io_finish_async(ctx); |
2b188cc1 JA |
8284 | return ret; |
8285 | } | |
8286 | ||
7e84e1c7 SG |
8287 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8288 | { | |
534ca6d6 JA |
8289 | struct io_sq_data *sqd = ctx->sq_data; |
8290 | ||
8291 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8292 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8293 | } |
8294 | ||
a087e2b5 BM |
8295 | static inline void __io_unaccount_mem(struct user_struct *user, |
8296 | unsigned long nr_pages) | |
2b188cc1 JA |
8297 | { |
8298 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8299 | } | |
8300 | ||
a087e2b5 BM |
8301 | static inline int __io_account_mem(struct user_struct *user, |
8302 | unsigned long nr_pages) | |
2b188cc1 JA |
8303 | { |
8304 | unsigned long page_limit, cur_pages, new_pages; | |
8305 | ||
8306 | /* Don't allow more pages than we can safely lock */ | |
8307 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8308 | ||
8309 | do { | |
8310 | cur_pages = atomic_long_read(&user->locked_vm); | |
8311 | new_pages = cur_pages + nr_pages; | |
8312 | if (new_pages > page_limit) | |
8313 | return -ENOMEM; | |
8314 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8315 | new_pages) != cur_pages); | |
8316 | ||
8317 | return 0; | |
8318 | } | |
8319 | ||
2e0464d4 BM |
8320 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8321 | enum io_mem_account acct) | |
a087e2b5 | 8322 | { |
aad5d8da | 8323 | if (ctx->limit_mem) |
a087e2b5 | 8324 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8325 | |
2aede0e4 | 8326 | if (ctx->mm_account) { |
4bc4a912 JA |
8327 | if (acct == ACCT_LOCKED) { |
8328 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8329 | ctx->mm_account->locked_vm -= nr_pages; |
4bc4a912 JA |
8330 | mmap_write_unlock(ctx->mm_account); |
8331 | }else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8332 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8333 | } |
2e0464d4 | 8334 | } |
a087e2b5 BM |
8335 | } |
8336 | ||
2e0464d4 BM |
8337 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8338 | enum io_mem_account acct) | |
a087e2b5 | 8339 | { |
30975825 BM |
8340 | int ret; |
8341 | ||
8342 | if (ctx->limit_mem) { | |
8343 | ret = __io_account_mem(ctx->user, nr_pages); | |
8344 | if (ret) | |
8345 | return ret; | |
8346 | } | |
8347 | ||
2aede0e4 | 8348 | if (ctx->mm_account) { |
4bc4a912 JA |
8349 | if (acct == ACCT_LOCKED) { |
8350 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8351 | ctx->mm_account->locked_vm += nr_pages; |
4bc4a912 JA |
8352 | mmap_write_unlock(ctx->mm_account); |
8353 | } else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8354 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8355 | } |
2e0464d4 | 8356 | } |
a087e2b5 BM |
8357 | |
8358 | return 0; | |
8359 | } | |
8360 | ||
2b188cc1 JA |
8361 | static void io_mem_free(void *ptr) |
8362 | { | |
52e04ef4 MR |
8363 | struct page *page; |
8364 | ||
8365 | if (!ptr) | |
8366 | return; | |
2b188cc1 | 8367 | |
52e04ef4 | 8368 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8369 | if (put_page_testzero(page)) |
8370 | free_compound_page(page); | |
8371 | } | |
8372 | ||
8373 | static void *io_mem_alloc(size_t size) | |
8374 | { | |
8375 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
8376 | __GFP_NORETRY; | |
8377 | ||
8378 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8379 | } | |
8380 | ||
75b28aff HV |
8381 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8382 | size_t *sq_offset) | |
8383 | { | |
8384 | struct io_rings *rings; | |
8385 | size_t off, sq_array_size; | |
8386 | ||
8387 | off = struct_size(rings, cqes, cq_entries); | |
8388 | if (off == SIZE_MAX) | |
8389 | return SIZE_MAX; | |
8390 | ||
8391 | #ifdef CONFIG_SMP | |
8392 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8393 | if (off == 0) | |
8394 | return SIZE_MAX; | |
8395 | #endif | |
8396 | ||
b36200f5 DV |
8397 | if (sq_offset) |
8398 | *sq_offset = off; | |
8399 | ||
75b28aff HV |
8400 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8401 | if (sq_array_size == SIZE_MAX) | |
8402 | return SIZE_MAX; | |
8403 | ||
8404 | if (check_add_overflow(off, sq_array_size, &off)) | |
8405 | return SIZE_MAX; | |
8406 | ||
75b28aff HV |
8407 | return off; |
8408 | } | |
8409 | ||
2b188cc1 JA |
8410 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8411 | { | |
75b28aff | 8412 | size_t pages; |
2b188cc1 | 8413 | |
75b28aff HV |
8414 | pages = (size_t)1 << get_order( |
8415 | rings_size(sq_entries, cq_entries, NULL)); | |
8416 | pages += (size_t)1 << get_order( | |
8417 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8418 | |
75b28aff | 8419 | return pages; |
2b188cc1 JA |
8420 | } |
8421 | ||
0a96bbe4 | 8422 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8423 | { |
8424 | int i, j; | |
8425 | ||
8426 | if (!ctx->user_bufs) | |
8427 | return -ENXIO; | |
8428 | ||
8429 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8430 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8431 | ||
8432 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8433 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8434 | |
de293938 JA |
8435 | if (imu->acct_pages) |
8436 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8437 | kvfree(imu->bvec); |
edafccee JA |
8438 | imu->nr_bvecs = 0; |
8439 | } | |
8440 | ||
8441 | kfree(ctx->user_bufs); | |
8442 | ctx->user_bufs = NULL; | |
8443 | ctx->nr_user_bufs = 0; | |
8444 | return 0; | |
8445 | } | |
8446 | ||
8447 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8448 | void __user *arg, unsigned index) | |
8449 | { | |
8450 | struct iovec __user *src; | |
8451 | ||
8452 | #ifdef CONFIG_COMPAT | |
8453 | if (ctx->compat) { | |
8454 | struct compat_iovec __user *ciovs; | |
8455 | struct compat_iovec ciov; | |
8456 | ||
8457 | ciovs = (struct compat_iovec __user *) arg; | |
8458 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8459 | return -EFAULT; | |
8460 | ||
d55e5f5b | 8461 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8462 | dst->iov_len = ciov.iov_len; |
8463 | return 0; | |
8464 | } | |
8465 | #endif | |
8466 | src = (struct iovec __user *) arg; | |
8467 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8468 | return -EFAULT; | |
8469 | return 0; | |
8470 | } | |
8471 | ||
de293938 JA |
8472 | /* |
8473 | * Not super efficient, but this is just a registration time. And we do cache | |
8474 | * the last compound head, so generally we'll only do a full search if we don't | |
8475 | * match that one. | |
8476 | * | |
8477 | * We check if the given compound head page has already been accounted, to | |
8478 | * avoid double accounting it. This allows us to account the full size of the | |
8479 | * page, not just the constituent pages of a huge page. | |
8480 | */ | |
8481 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8482 | int nr_pages, struct page *hpage) | |
8483 | { | |
8484 | int i, j; | |
8485 | ||
8486 | /* check current page array */ | |
8487 | for (i = 0; i < nr_pages; i++) { | |
8488 | if (!PageCompound(pages[i])) | |
8489 | continue; | |
8490 | if (compound_head(pages[i]) == hpage) | |
8491 | return true; | |
8492 | } | |
8493 | ||
8494 | /* check previously registered pages */ | |
8495 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8496 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8497 | ||
8498 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8499 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8500 | continue; | |
8501 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8502 | return true; | |
8503 | } | |
8504 | } | |
8505 | ||
8506 | return false; | |
8507 | } | |
8508 | ||
8509 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8510 | int nr_pages, struct io_mapped_ubuf *imu, | |
8511 | struct page **last_hpage) | |
8512 | { | |
8513 | int i, ret; | |
8514 | ||
8515 | for (i = 0; i < nr_pages; i++) { | |
8516 | if (!PageCompound(pages[i])) { | |
8517 | imu->acct_pages++; | |
8518 | } else { | |
8519 | struct page *hpage; | |
8520 | ||
8521 | hpage = compound_head(pages[i]); | |
8522 | if (hpage == *last_hpage) | |
8523 | continue; | |
8524 | *last_hpage = hpage; | |
8525 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8526 | continue; | |
8527 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8528 | } | |
8529 | } | |
8530 | ||
8531 | if (!imu->acct_pages) | |
8532 | return 0; | |
8533 | ||
8534 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8535 | if (ret) | |
8536 | imu->acct_pages = 0; | |
8537 | return ret; | |
8538 | } | |
8539 | ||
0a96bbe4 BM |
8540 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8541 | struct io_mapped_ubuf *imu, | |
8542 | struct page **last_hpage) | |
edafccee JA |
8543 | { |
8544 | struct vm_area_struct **vmas = NULL; | |
8545 | struct page **pages = NULL; | |
0a96bbe4 BM |
8546 | unsigned long off, start, end, ubuf; |
8547 | size_t size; | |
8548 | int ret, pret, nr_pages, i; | |
8549 | ||
8550 | ubuf = (unsigned long) iov->iov_base; | |
8551 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8552 | start = ubuf >> PAGE_SHIFT; | |
8553 | nr_pages = end - start; | |
8554 | ||
8555 | ret = -ENOMEM; | |
8556 | ||
8557 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8558 | if (!pages) | |
8559 | goto done; | |
8560 | ||
8561 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8562 | GFP_KERNEL); | |
8563 | if (!vmas) | |
8564 | goto done; | |
8565 | ||
8566 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), | |
8567 | GFP_KERNEL); | |
8568 | if (!imu->bvec) | |
8569 | goto done; | |
8570 | ||
8571 | ret = 0; | |
8572 | mmap_read_lock(current->mm); | |
8573 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8574 | pages, vmas); | |
8575 | if (pret == nr_pages) { | |
8576 | /* don't support file backed memory */ | |
8577 | for (i = 0; i < nr_pages; i++) { | |
8578 | struct vm_area_struct *vma = vmas[i]; | |
8579 | ||
8580 | if (vma->vm_file && | |
8581 | !is_file_hugepages(vma->vm_file)) { | |
8582 | ret = -EOPNOTSUPP; | |
8583 | break; | |
8584 | } | |
8585 | } | |
8586 | } else { | |
8587 | ret = pret < 0 ? pret : -EFAULT; | |
8588 | } | |
8589 | mmap_read_unlock(current->mm); | |
8590 | if (ret) { | |
8591 | /* | |
8592 | * if we did partial map, or found file backed vmas, | |
8593 | * release any pages we did get | |
8594 | */ | |
8595 | if (pret > 0) | |
8596 | unpin_user_pages(pages, pret); | |
8597 | kvfree(imu->bvec); | |
8598 | goto done; | |
8599 | } | |
8600 | ||
8601 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8602 | if (ret) { | |
8603 | unpin_user_pages(pages, pret); | |
8604 | kvfree(imu->bvec); | |
8605 | goto done; | |
8606 | } | |
8607 | ||
8608 | off = ubuf & ~PAGE_MASK; | |
8609 | size = iov->iov_len; | |
8610 | for (i = 0; i < nr_pages; i++) { | |
8611 | size_t vec_len; | |
8612 | ||
8613 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8614 | imu->bvec[i].bv_page = pages[i]; | |
8615 | imu->bvec[i].bv_len = vec_len; | |
8616 | imu->bvec[i].bv_offset = off; | |
8617 | off = 0; | |
8618 | size -= vec_len; | |
8619 | } | |
8620 | /* store original address for later verification */ | |
8621 | imu->ubuf = ubuf; | |
8622 | imu->len = iov->iov_len; | |
8623 | imu->nr_bvecs = nr_pages; | |
8624 | ret = 0; | |
8625 | done: | |
8626 | kvfree(pages); | |
8627 | kvfree(vmas); | |
8628 | return ret; | |
8629 | } | |
8630 | ||
2b358604 | 8631 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8632 | { |
edafccee JA |
8633 | if (ctx->user_bufs) |
8634 | return -EBUSY; | |
8635 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8636 | return -EINVAL; | |
8637 | ||
8638 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8639 | GFP_KERNEL); | |
8640 | if (!ctx->user_bufs) | |
8641 | return -ENOMEM; | |
8642 | ||
2b358604 BM |
8643 | return 0; |
8644 | } | |
8645 | ||
8646 | static int io_buffer_validate(struct iovec *iov) | |
8647 | { | |
8648 | /* | |
8649 | * Don't impose further limits on the size and buffer | |
8650 | * constraints here, we'll -EINVAL later when IO is | |
8651 | * submitted if they are wrong. | |
8652 | */ | |
8653 | if (!iov->iov_base || !iov->iov_len) | |
8654 | return -EFAULT; | |
8655 | ||
8656 | /* arbitrary limit, but we need something */ | |
8657 | if (iov->iov_len > SZ_1G) | |
8658 | return -EFAULT; | |
8659 | ||
8660 | return 0; | |
8661 | } | |
8662 | ||
8663 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
8664 | unsigned int nr_args) | |
8665 | { | |
8666 | int i, ret; | |
8667 | struct iovec iov; | |
8668 | struct page *last_hpage = NULL; | |
8669 | ||
8670 | ret = io_buffers_map_alloc(ctx, nr_args); | |
8671 | if (ret) | |
8672 | return ret; | |
8673 | ||
edafccee JA |
8674 | for (i = 0; i < nr_args; i++) { |
8675 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee JA |
8676 | |
8677 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8678 | if (ret) | |
0a96bbe4 | 8679 | break; |
edafccee | 8680 | |
2b358604 BM |
8681 | ret = io_buffer_validate(&iov); |
8682 | if (ret) | |
0a96bbe4 | 8683 | break; |
edafccee | 8684 | |
0a96bbe4 BM |
8685 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8686 | if (ret) | |
8687 | break; | |
edafccee JA |
8688 | |
8689 | ctx->nr_user_bufs++; | |
8690 | } | |
0a96bbe4 BM |
8691 | |
8692 | if (ret) | |
8693 | io_sqe_buffers_unregister(ctx); | |
8694 | ||
edafccee JA |
8695 | return ret; |
8696 | } | |
8697 | ||
9b402849 JA |
8698 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8699 | { | |
8700 | __s32 __user *fds = arg; | |
8701 | int fd; | |
8702 | ||
8703 | if (ctx->cq_ev_fd) | |
8704 | return -EBUSY; | |
8705 | ||
8706 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8707 | return -EFAULT; | |
8708 | ||
8709 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8710 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8711 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8712 | ctx->cq_ev_fd = NULL; | |
8713 | return ret; | |
8714 | } | |
8715 | ||
8716 | return 0; | |
8717 | } | |
8718 | ||
8719 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8720 | { | |
8721 | if (ctx->cq_ev_fd) { | |
8722 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8723 | ctx->cq_ev_fd = NULL; | |
8724 | return 0; | |
8725 | } | |
8726 | ||
8727 | return -ENXIO; | |
8728 | } | |
8729 | ||
5a2e745d JA |
8730 | static int __io_destroy_buffers(int id, void *p, void *data) |
8731 | { | |
8732 | struct io_ring_ctx *ctx = data; | |
8733 | struct io_buffer *buf = p; | |
8734 | ||
067524e9 | 8735 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8736 | return 0; |
8737 | } | |
8738 | ||
8739 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8740 | { | |
8741 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8742 | idr_destroy(&ctx->io_buffer_idr); | |
8743 | } | |
8744 | ||
2b188cc1 JA |
8745 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8746 | { | |
6b06314c | 8747 | io_finish_async(ctx); |
0a96bbe4 | 8748 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8749 | |
8750 | if (ctx->sqo_task) { | |
8751 | put_task_struct(ctx->sqo_task); | |
8752 | ctx->sqo_task = NULL; | |
8753 | mmdrop(ctx->mm_account); | |
8754 | ctx->mm_account = NULL; | |
30975825 | 8755 | } |
def596e9 | 8756 | |
91d8f519 DZ |
8757 | #ifdef CONFIG_BLK_CGROUP |
8758 | if (ctx->sqo_blkcg_css) | |
8759 | css_put(ctx->sqo_blkcg_css); | |
8760 | #endif | |
8761 | ||
6b06314c | 8762 | io_sqe_files_unregister(ctx); |
9b402849 | 8763 | io_eventfd_unregister(ctx); |
5a2e745d | 8764 | io_destroy_buffers(ctx); |
41726c9a | 8765 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8766 | |
2b188cc1 | 8767 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8768 | if (ctx->ring_sock) { |
8769 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8770 | sock_release(ctx->ring_sock); |
355e8d26 | 8771 | } |
2b188cc1 JA |
8772 | #endif |
8773 | ||
75b28aff | 8774 | io_mem_free(ctx->rings); |
2b188cc1 | 8775 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8776 | |
8777 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8778 | free_uid(ctx->user); |
181e448d | 8779 | put_cred(ctx->creds); |
78076bb6 | 8780 | kfree(ctx->cancel_hash); |
0ddf92e8 | 8781 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
8782 | kfree(ctx); |
8783 | } | |
8784 | ||
8785 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8786 | { | |
8787 | struct io_ring_ctx *ctx = file->private_data; | |
8788 | __poll_t mask = 0; | |
8789 | ||
8790 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8791 | /* |
8792 | * synchronizes with barrier from wq_has_sleeper call in | |
8793 | * io_commit_cqring | |
8794 | */ | |
2b188cc1 | 8795 | smp_rmb(); |
90554200 | 8796 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8797 | mask |= EPOLLOUT | EPOLLWRNORM; |
6c503150 PB |
8798 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
8799 | if (io_cqring_events(ctx)) | |
2b188cc1 JA |
8800 | mask |= EPOLLIN | EPOLLRDNORM; |
8801 | ||
8802 | return mask; | |
8803 | } | |
8804 | ||
8805 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8806 | { | |
8807 | struct io_ring_ctx *ctx = file->private_data; | |
8808 | ||
8809 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8810 | } | |
8811 | ||
071698e1 JA |
8812 | static int io_remove_personalities(int id, void *p, void *data) |
8813 | { | |
8814 | struct io_ring_ctx *ctx = data; | |
1e6fa521 | 8815 | struct io_identity *iod; |
071698e1 | 8816 | |
1e6fa521 JA |
8817 | iod = idr_remove(&ctx->personality_idr, id); |
8818 | if (iod) { | |
8819 | put_cred(iod->creds); | |
8820 | if (refcount_dec_and_test(&iod->count)) | |
8821 | kfree(iod); | |
8822 | } | |
071698e1 JA |
8823 | return 0; |
8824 | } | |
8825 | ||
85faa7b8 JA |
8826 | static void io_ring_exit_work(struct work_struct *work) |
8827 | { | |
b2edc0a7 PB |
8828 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8829 | exit_work); | |
85faa7b8 | 8830 | |
56952e91 JA |
8831 | /* |
8832 | * If we're doing polled IO and end up having requests being | |
8833 | * submitted async (out-of-line), then completions can come in while | |
8834 | * we're waiting for refs to drop. We need to reap these manually, | |
8835 | * as nobody else will be looking for them. | |
8836 | */ | |
b2edc0a7 | 8837 | do { |
90df0853 | 8838 | __io_uring_cancel_task_requests(ctx, NULL); |
b2edc0a7 | 8839 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8840 | io_ring_ctx_free(ctx); |
8841 | } | |
8842 | ||
00c18640 JA |
8843 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8844 | { | |
8845 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8846 | ||
8847 | return req->ctx == data; | |
8848 | } | |
8849 | ||
2b188cc1 JA |
8850 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8851 | { | |
8852 | mutex_lock(&ctx->uring_lock); | |
8853 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8854 | |
8855 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8856 | ctx->sqo_dead = 1; | |
8857 | ||
cda286f0 PB |
8858 | /* if force is set, the ring is going away. always drop after that */ |
8859 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8860 | if (ctx->rings) |
6c503150 | 8861 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
2b188cc1 JA |
8862 | mutex_unlock(&ctx->uring_lock); |
8863 | ||
6b81928d PB |
8864 | io_kill_timeouts(ctx, NULL, NULL); |
8865 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8866 | |
8867 | if (ctx->io_wq) | |
00c18640 | 8868 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8869 | |
15dff286 | 8870 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8871 | io_iopoll_try_reap_events(ctx); |
071698e1 | 8872 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
309fc03a JA |
8873 | |
8874 | /* | |
8875 | * Do this upfront, so we won't have a grace period where the ring | |
8876 | * is closed but resources aren't reaped yet. This can cause | |
8877 | * spurious failure in setting up a new ring. | |
8878 | */ | |
760618f7 JA |
8879 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8880 | ACCT_LOCKED); | |
309fc03a | 8881 | |
85faa7b8 | 8882 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8883 | /* |
8884 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8885 | * if we're exiting a ton of rings at the same time. It just adds | |
8886 | * noise and overhead, there's no discernable change in runtime | |
8887 | * over using system_wq. | |
8888 | */ | |
8889 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8890 | } |
8891 | ||
8892 | static int io_uring_release(struct inode *inode, struct file *file) | |
8893 | { | |
8894 | struct io_ring_ctx *ctx = file->private_data; | |
8895 | ||
8896 | file->private_data = NULL; | |
8897 | io_ring_ctx_wait_and_kill(ctx); | |
8898 | return 0; | |
8899 | } | |
8900 | ||
f6edbabb PB |
8901 | struct io_task_cancel { |
8902 | struct task_struct *task; | |
8903 | struct files_struct *files; | |
8904 | }; | |
f254ac04 | 8905 | |
f6edbabb | 8906 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8907 | { |
9a472ef7 | 8908 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8909 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8910 | bool ret; |
8911 | ||
f6edbabb | 8912 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8913 | unsigned long flags; |
8914 | struct io_ring_ctx *ctx = req->ctx; | |
8915 | ||
8916 | /* protect against races with linked timeouts */ | |
8917 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8918 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8919 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8920 | } else { | |
f6edbabb | 8921 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8922 | } |
8923 | return ret; | |
b711d4ea JA |
8924 | } |
8925 | ||
b7ddce3c | 8926 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8927 | struct task_struct *task, |
b7ddce3c PB |
8928 | struct files_struct *files) |
8929 | { | |
8930 | struct io_defer_entry *de = NULL; | |
8931 | LIST_HEAD(list); | |
8932 | ||
8933 | spin_lock_irq(&ctx->completion_lock); | |
8934 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8935 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8936 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8937 | break; | |
8938 | } | |
8939 | } | |
8940 | spin_unlock_irq(&ctx->completion_lock); | |
8941 | ||
8942 | while (!list_empty(&list)) { | |
8943 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8944 | list_del_init(&de->list); | |
8945 | req_set_fail_links(de->req); | |
8946 | io_put_req(de->req); | |
8947 | io_req_complete(de->req, -ECANCELED); | |
8948 | kfree(de); | |
8949 | } | |
8950 | } | |
8951 | ||
ca70f00b PB |
8952 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8953 | struct task_struct *task, | |
8954 | struct files_struct *files) | |
8955 | { | |
8956 | struct io_kiocb *req; | |
8957 | int cnt = 0; | |
8958 | ||
8959 | spin_lock_irq(&ctx->inflight_lock); | |
8960 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8961 | cnt += io_match_task(req, task, files); | |
8962 | spin_unlock_irq(&ctx->inflight_lock); | |
8963 | return cnt; | |
8964 | } | |
8965 | ||
b52fda00 | 8966 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8967 | struct task_struct *task, |
fcb323cc JA |
8968 | struct files_struct *files) |
8969 | { | |
fcb323cc | 8970 | while (!list_empty_careful(&ctx->inflight_list)) { |
bee749b1 | 8971 | struct io_task_cancel cancel = { .task = task, .files = files }; |
d8f1b971 | 8972 | DEFINE_WAIT(wait); |
ca70f00b | 8973 | int inflight; |
fcb323cc | 8974 | |
ca70f00b PB |
8975 | inflight = io_uring_count_inflight(ctx, task, files); |
8976 | if (!inflight) | |
fcb323cc | 8977 | break; |
f6edbabb PB |
8978 | |
8979 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); | |
8980 | io_poll_remove_all(ctx, task, files); | |
8981 | io_kill_timeouts(ctx, task, files); | |
9d5c8190 | 8982 | io_cqring_overflow_flush(ctx, true, task, files); |
6200b0ae JA |
8983 | /* cancellations _may_ trigger task work */ |
8984 | io_run_task_work(); | |
ca70f00b PB |
8985 | |
8986 | prepare_to_wait(&task->io_uring->wait, &wait, | |
8987 | TASK_UNINTERRUPTIBLE); | |
8988 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8989 | schedule(); | |
c98de08c | 8990 | finish_wait(&task->io_uring->wait, &wait); |
fcb323cc | 8991 | } |
44e728b8 PB |
8992 | } |
8993 | ||
b52fda00 PB |
8994 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
8995 | struct task_struct *task) | |
0f212204 | 8996 | { |
b52fda00 | 8997 | while (1) { |
f6edbabb | 8998 | struct io_task_cancel cancel = { .task = task, .files = NULL, }; |
0f212204 | 8999 | enum io_wq_cancel cret; |
b52fda00 | 9000 | bool ret = false; |
0f212204 | 9001 | |
90df0853 PB |
9002 | if (ctx->io_wq) { |
9003 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, | |
9004 | &cancel, true); | |
9005 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9006 | } | |
0f212204 JA |
9007 | |
9008 | /* SQPOLL thread does its own polling */ | |
9009 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
9010 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
9011 | io_iopoll_try_reap_events(ctx); | |
9012 | ret = true; | |
9013 | } | |
9014 | } | |
9015 | ||
6b81928d PB |
9016 | ret |= io_poll_remove_all(ctx, task, NULL); |
9017 | ret |= io_kill_timeouts(ctx, task, NULL); | |
55583d72 | 9018 | ret |= io_run_task_work(); |
b52fda00 PB |
9019 | if (!ret) |
9020 | break; | |
b52fda00 | 9021 | cond_resched(); |
0f212204 | 9022 | } |
0f212204 JA |
9023 | } |
9024 | ||
d9d05217 PB |
9025 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
9026 | { | |
d9d05217 PB |
9027 | mutex_lock(&ctx->uring_lock); |
9028 | ctx->sqo_dead = 1; | |
9029 | mutex_unlock(&ctx->uring_lock); | |
9030 | ||
9031 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
9032 | if (ctx->rings) |
9033 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
9034 | } |
9035 | ||
0f212204 JA |
9036 | /* |
9037 | * We need to iteratively cancel requests, in case a request has dependent | |
9038 | * hard links. These persist even for failure of cancelations, hence keep | |
9039 | * looping until none are found. | |
9040 | */ | |
9041 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
9042 | struct files_struct *files) | |
9043 | { | |
9044 | struct task_struct *task = current; | |
9045 | ||
fdaf083c | 9046 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 9047 | io_disable_sqo_submit(ctx); |
534ca6d6 | 9048 | task = ctx->sq_data->thread; |
fdaf083c JA |
9049 | atomic_inc(&task->io_uring->in_idle); |
9050 | io_sq_thread_park(ctx->sq_data); | |
9051 | } | |
0f212204 | 9052 | |
df9923f9 | 9053 | io_cancel_defer_files(ctx, task, files); |
0f212204 JA |
9054 | io_cqring_overflow_flush(ctx, true, task, files); |
9055 | ||
3a7efd1a | 9056 | io_uring_cancel_files(ctx, task, files); |
b52fda00 PB |
9057 | if (!files) |
9058 | __io_uring_cancel_task_requests(ctx, task); | |
fdaf083c JA |
9059 | |
9060 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
9061 | atomic_dec(&task->io_uring->in_idle); | |
9062 | /* | |
9063 | * If the files that are going away are the ones in the thread | |
9064 | * identity, clear them out. | |
9065 | */ | |
9066 | if (task->io_uring->identity->files == files) | |
9067 | task->io_uring->identity->files = NULL; | |
9068 | io_sq_thread_unpark(ctx->sq_data); | |
9069 | } | |
0f212204 JA |
9070 | } |
9071 | ||
9072 | /* | |
9073 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9074 | */ | |
fdaf083c | 9075 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 9076 | { |
236434c3 | 9077 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 9078 | int ret; |
236434c3 MWO |
9079 | |
9080 | if (unlikely(!tctx)) { | |
0f212204 JA |
9081 | ret = io_uring_alloc_task_context(current); |
9082 | if (unlikely(ret)) | |
9083 | return ret; | |
236434c3 | 9084 | tctx = current->io_uring; |
0f212204 | 9085 | } |
236434c3 MWO |
9086 | if (tctx->last != file) { |
9087 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 9088 | |
236434c3 | 9089 | if (!old) { |
0f212204 | 9090 | get_file(file); |
a528b04e PB |
9091 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
9092 | file, GFP_KERNEL)); | |
9093 | if (ret) { | |
9094 | fput(file); | |
9095 | return ret; | |
9096 | } | |
0f212204 | 9097 | } |
236434c3 | 9098 | tctx->last = file; |
0f212204 JA |
9099 | } |
9100 | ||
fdaf083c JA |
9101 | /* |
9102 | * This is race safe in that the task itself is doing this, hence it | |
9103 | * cannot be going through the exit/cancel paths at the same time. | |
9104 | * This cannot be modified while exit/cancel is running. | |
9105 | */ | |
9106 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9107 | tctx->sqpoll = true; | |
9108 | ||
0f212204 JA |
9109 | return 0; |
9110 | } | |
9111 | ||
9112 | /* | |
9113 | * Remove this io_uring_file -> task mapping. | |
9114 | */ | |
9115 | static void io_uring_del_task_file(struct file *file) | |
9116 | { | |
9117 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9118 | |
9119 | if (tctx->last == file) | |
9120 | tctx->last = NULL; | |
5e2ed8c4 | 9121 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9122 | if (file) |
9123 | fput(file); | |
9124 | } | |
9125 | ||
de7f1d9e PB |
9126 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9127 | { | |
9128 | struct file *file; | |
9129 | unsigned long index; | |
9130 | ||
9131 | xa_for_each(&tctx->xa, index, file) | |
9132 | io_uring_del_task_file(file); | |
9133 | } | |
9134 | ||
0f212204 JA |
9135 | void __io_uring_files_cancel(struct files_struct *files) |
9136 | { | |
9137 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9138 | struct file *file; |
9139 | unsigned long index; | |
0f212204 JA |
9140 | |
9141 | /* make sure overflow events are dropped */ | |
fdaf083c | 9142 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9143 | xa_for_each(&tctx->xa, index, file) |
9144 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9145 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9146 | |
9147 | if (files) | |
9148 | io_uring_remove_task_files(tctx); | |
fdaf083c JA |
9149 | } |
9150 | ||
9151 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9152 | { | |
9153 | unsigned long index; | |
9154 | struct file *file; | |
9155 | s64 inflight; | |
9156 | ||
9157 | inflight = percpu_counter_sum(&tctx->inflight); | |
9158 | if (!tctx->sqpoll) | |
9159 | return inflight; | |
9160 | ||
9161 | /* | |
9162 | * If we have SQPOLL rings, then we need to iterate and find them, and | |
9163 | * add the pending count for those. | |
9164 | */ | |
9165 | xa_for_each(&tctx->xa, index, file) { | |
9166 | struct io_ring_ctx *ctx = file->private_data; | |
9167 | ||
9168 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
9169 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; | |
9170 | ||
9171 | inflight += percpu_counter_sum(&__tctx->inflight); | |
9172 | } | |
9173 | } | |
9174 | ||
9175 | return inflight; | |
0f212204 JA |
9176 | } |
9177 | ||
0f212204 JA |
9178 | /* |
9179 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9180 | * requests. | |
9181 | */ | |
9182 | void __io_uring_task_cancel(void) | |
9183 | { | |
9184 | struct io_uring_task *tctx = current->io_uring; | |
9185 | DEFINE_WAIT(wait); | |
d8a6df10 | 9186 | s64 inflight; |
0f212204 JA |
9187 | |
9188 | /* make sure overflow events are dropped */ | |
fdaf083c | 9189 | atomic_inc(&tctx->in_idle); |
0f212204 | 9190 | |
0b5cd6c3 PB |
9191 | /* trigger io_disable_sqo_submit() */ |
9192 | if (tctx->sqpoll) | |
9193 | __io_uring_files_cancel(NULL); | |
9194 | ||
d8a6df10 | 9195 | do { |
0f212204 | 9196 | /* read completions before cancelations */ |
fdaf083c | 9197 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9198 | if (!inflight) |
9199 | break; | |
0f212204 JA |
9200 | __io_uring_files_cancel(NULL); |
9201 | ||
9202 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9203 | ||
9204 | /* | |
a1bb3cd5 PB |
9205 | * If we've seen completions, retry without waiting. This |
9206 | * avoids a race where a completion comes in before we did | |
9207 | * prepare_to_wait(). | |
0f212204 | 9208 | */ |
a1bb3cd5 PB |
9209 | if (inflight == tctx_inflight(tctx)) |
9210 | schedule(); | |
f57555ed | 9211 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9212 | } while (1); |
0f212204 | 9213 | |
fdaf083c | 9214 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9215 | |
9216 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9217 | } |
9218 | ||
fcb323cc JA |
9219 | static int io_uring_flush(struct file *file, void *data) |
9220 | { | |
6b5733eb | 9221 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9222 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9223 | |
84965ff8 JA |
9224 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
9225 | io_uring_cancel_task_requests(ctx, NULL); | |
9226 | ||
6b5733eb | 9227 | if (!tctx) |
4f793dc4 PB |
9228 | return 0; |
9229 | ||
6b5733eb PB |
9230 | /* we should have cancelled and erased it before PF_EXITING */ |
9231 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9232 | xa_load(&tctx->xa, (unsigned long)file)); | |
9233 | ||
4f793dc4 PB |
9234 | /* |
9235 | * fput() is pending, will be 2 if the only other ref is our potential | |
9236 | * task file note. If the task is exiting, drop regardless of count. | |
9237 | */ | |
6b5733eb PB |
9238 | if (atomic_long_read(&file->f_count) != 2) |
9239 | return 0; | |
4f793dc4 | 9240 | |
d9d05217 PB |
9241 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9242 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9243 | WARN_ON_ONCE(ctx->sqo_task != current && |
9244 | xa_load(&tctx->xa, (unsigned long)file)); | |
9245 | /* sqo_dead check is for when this happens after cancellation */ | |
9246 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9247 | !xa_load(&tctx->xa, (unsigned long)file)); |
9248 | ||
9249 | io_disable_sqo_submit(ctx); | |
9250 | } | |
9251 | ||
9252 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9253 | io_uring_del_task_file(file); | |
fcb323cc JA |
9254 | return 0; |
9255 | } | |
9256 | ||
6c5c240e RP |
9257 | static void *io_uring_validate_mmap_request(struct file *file, |
9258 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9259 | { |
2b188cc1 | 9260 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9261 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9262 | struct page *page; |
9263 | void *ptr; | |
9264 | ||
9265 | switch (offset) { | |
9266 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9267 | case IORING_OFF_CQ_RING: |
9268 | ptr = ctx->rings; | |
2b188cc1 JA |
9269 | break; |
9270 | case IORING_OFF_SQES: | |
9271 | ptr = ctx->sq_sqes; | |
9272 | break; | |
2b188cc1 | 9273 | default: |
6c5c240e | 9274 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9275 | } |
9276 | ||
9277 | page = virt_to_head_page(ptr); | |
a50b854e | 9278 | if (sz > page_size(page)) |
6c5c240e RP |
9279 | return ERR_PTR(-EINVAL); |
9280 | ||
9281 | return ptr; | |
9282 | } | |
9283 | ||
9284 | #ifdef CONFIG_MMU | |
9285 | ||
9286 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9287 | { | |
9288 | size_t sz = vma->vm_end - vma->vm_start; | |
9289 | unsigned long pfn; | |
9290 | void *ptr; | |
9291 | ||
9292 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9293 | if (IS_ERR(ptr)) | |
9294 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9295 | |
9296 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9297 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9298 | } | |
9299 | ||
6c5c240e RP |
9300 | #else /* !CONFIG_MMU */ |
9301 | ||
9302 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9303 | { | |
9304 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9305 | } | |
9306 | ||
9307 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9308 | { | |
9309 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9310 | } | |
9311 | ||
9312 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9313 | unsigned long addr, unsigned long len, | |
9314 | unsigned long pgoff, unsigned long flags) | |
9315 | { | |
9316 | void *ptr; | |
9317 | ||
9318 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9319 | if (IS_ERR(ptr)) | |
9320 | return PTR_ERR(ptr); | |
9321 | ||
9322 | return (unsigned long) ptr; | |
9323 | } | |
9324 | ||
9325 | #endif /* !CONFIG_MMU */ | |
9326 | ||
d9d05217 | 9327 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9328 | { |
d9d05217 | 9329 | int ret = 0; |
90554200 JA |
9330 | DEFINE_WAIT(wait); |
9331 | ||
9332 | do { | |
9333 | if (!io_sqring_full(ctx)) | |
9334 | break; | |
9335 | ||
9336 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9337 | ||
d9d05217 PB |
9338 | if (unlikely(ctx->sqo_dead)) { |
9339 | ret = -EOWNERDEAD; | |
9340 | goto out; | |
9341 | } | |
9342 | ||
90554200 JA |
9343 | if (!io_sqring_full(ctx)) |
9344 | break; | |
9345 | ||
9346 | schedule(); | |
9347 | } while (!signal_pending(current)); | |
9348 | ||
9349 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9350 | out: |
9351 | return ret; | |
90554200 JA |
9352 | } |
9353 | ||
c73ebb68 HX |
9354 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9355 | struct __kernel_timespec __user **ts, | |
9356 | const sigset_t __user **sig) | |
9357 | { | |
9358 | struct io_uring_getevents_arg arg; | |
9359 | ||
9360 | /* | |
9361 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9362 | * is just a pointer to the sigset_t. | |
9363 | */ | |
9364 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9365 | *sig = (const sigset_t __user *) argp; | |
9366 | *ts = NULL; | |
9367 | return 0; | |
9368 | } | |
9369 | ||
9370 | /* | |
9371 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9372 | * timespec and sigset_t pointers if good. | |
9373 | */ | |
9374 | if (*argsz != sizeof(arg)) | |
9375 | return -EINVAL; | |
9376 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9377 | return -EFAULT; | |
9378 | *sig = u64_to_user_ptr(arg.sigmask); | |
9379 | *argsz = arg.sigmask_sz; | |
9380 | *ts = u64_to_user_ptr(arg.ts); | |
9381 | return 0; | |
9382 | } | |
9383 | ||
2b188cc1 | 9384 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9385 | u32, min_complete, u32, flags, const void __user *, argp, |
9386 | size_t, argsz) | |
2b188cc1 JA |
9387 | { |
9388 | struct io_ring_ctx *ctx; | |
9389 | long ret = -EBADF; | |
9390 | int submitted = 0; | |
9391 | struct fd f; | |
9392 | ||
4c6e277c | 9393 | io_run_task_work(); |
b41e9852 | 9394 | |
90554200 | 9395 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9396 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9397 | return -EINVAL; |
9398 | ||
9399 | f = fdget(fd); | |
9400 | if (!f.file) | |
9401 | return -EBADF; | |
9402 | ||
9403 | ret = -EOPNOTSUPP; | |
9404 | if (f.file->f_op != &io_uring_fops) | |
9405 | goto out_fput; | |
9406 | ||
9407 | ret = -ENXIO; | |
9408 | ctx = f.file->private_data; | |
9409 | if (!percpu_ref_tryget(&ctx->refs)) | |
9410 | goto out_fput; | |
9411 | ||
7e84e1c7 SG |
9412 | ret = -EBADFD; |
9413 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9414 | goto out; | |
9415 | ||
6c271ce2 JA |
9416 | /* |
9417 | * For SQ polling, the thread will do all submissions and completions. | |
9418 | * Just return the requested submit count, and wake the thread if | |
9419 | * we were asked to. | |
9420 | */ | |
b2a9eada | 9421 | ret = 0; |
6c271ce2 | 9422 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9423 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9424 | |
d9d05217 PB |
9425 | ret = -EOWNERDEAD; |
9426 | if (unlikely(ctx->sqo_dead)) | |
9427 | goto out; | |
6c271ce2 | 9428 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9429 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9430 | if (flags & IORING_ENTER_SQ_WAIT) { |
9431 | ret = io_sqpoll_wait_sq(ctx); | |
9432 | if (ret) | |
9433 | goto out; | |
9434 | } | |
6c271ce2 | 9435 | submitted = to_submit; |
b2a9eada | 9436 | } else if (to_submit) { |
fdaf083c | 9437 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9438 | if (unlikely(ret)) |
9439 | goto out; | |
2b188cc1 | 9440 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9441 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9442 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9443 | |
9444 | if (submitted != to_submit) | |
9445 | goto out; | |
2b188cc1 JA |
9446 | } |
9447 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9448 | const sigset_t __user *sig; |
9449 | struct __kernel_timespec __user *ts; | |
9450 | ||
9451 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9452 | if (unlikely(ret)) | |
9453 | goto out; | |
9454 | ||
2b188cc1 JA |
9455 | min_complete = min(min_complete, ctx->cq_entries); |
9456 | ||
32b2244a XW |
9457 | /* |
9458 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9459 | * space applications don't need to do io completion events | |
9460 | * polling again, they can rely on io_sq_thread to do polling | |
9461 | * work, which can reduce cpu usage and uring_lock contention. | |
9462 | */ | |
9463 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9464 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9465 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9466 | } else { |
c73ebb68 | 9467 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9468 | } |
2b188cc1 JA |
9469 | } |
9470 | ||
7c504e65 | 9471 | out: |
6805b32e | 9472 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9473 | out_fput: |
9474 | fdput(f); | |
9475 | return submitted ? submitted : ret; | |
9476 | } | |
9477 | ||
bebdb65e | 9478 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9479 | static int io_uring_show_cred(int id, void *p, void *data) |
9480 | { | |
6b47ab81 JA |
9481 | struct io_identity *iod = p; |
9482 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9483 | struct seq_file *m = data; |
9484 | struct user_namespace *uns = seq_user_ns(m); | |
9485 | struct group_info *gi; | |
9486 | kernel_cap_t cap; | |
9487 | unsigned __capi; | |
9488 | int g; | |
9489 | ||
9490 | seq_printf(m, "%5d\n", id); | |
9491 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9492 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9493 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9494 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9495 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9496 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9497 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9498 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9499 | seq_puts(m, "\n\tGroups:\t"); | |
9500 | gi = cred->group_info; | |
9501 | for (g = 0; g < gi->ngroups; g++) { | |
9502 | seq_put_decimal_ull(m, g ? " " : "", | |
9503 | from_kgid_munged(uns, gi->gid[g])); | |
9504 | } | |
9505 | seq_puts(m, "\n\tCapEff:\t"); | |
9506 | cap = cred->cap_effective; | |
9507 | CAP_FOR_EACH_U32(__capi) | |
9508 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9509 | seq_putc(m, '\n'); | |
9510 | return 0; | |
9511 | } | |
9512 | ||
9513 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9514 | { | |
dbbe9c64 | 9515 | struct io_sq_data *sq = NULL; |
fad8e0de | 9516 | bool has_lock; |
87ce955b JA |
9517 | int i; |
9518 | ||
fad8e0de JA |
9519 | /* |
9520 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9521 | * since fdinfo case grabs it in the opposite direction of normal use | |
9522 | * cases. If we fail to get the lock, we just don't iterate any | |
9523 | * structures that could be going away outside the io_uring mutex. | |
9524 | */ | |
9525 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9526 | ||
dbbe9c64 JQ |
9527 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9528 | sq = ctx->sq_data; | |
9529 | ||
9530 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9531 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9532 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9533 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
269bbe5f | 9534 | struct fixed_rsrc_table *table; |
87ce955b JA |
9535 | struct file *f; |
9536 | ||
9537 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
9538 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
9539 | if (f) | |
9540 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9541 | else | |
9542 | seq_printf(m, "%5u: <none>\n", i); | |
9543 | } | |
9544 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9545 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9546 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9547 | ||
9548 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9549 | (unsigned int) buf->len); | |
9550 | } | |
fad8e0de | 9551 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9552 | seq_printf(m, "Personalities:\n"); |
9553 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9554 | } | |
d7718a9d JA |
9555 | seq_printf(m, "PollList:\n"); |
9556 | spin_lock_irq(&ctx->completion_lock); | |
9557 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9558 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9559 | struct io_kiocb *req; | |
9560 | ||
9561 | hlist_for_each_entry(req, list, hash_node) | |
9562 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9563 | req->task->task_works != NULL); | |
9564 | } | |
9565 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9566 | if (has_lock) |
9567 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9568 | } |
9569 | ||
9570 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9571 | { | |
9572 | struct io_ring_ctx *ctx = f->private_data; | |
9573 | ||
9574 | if (percpu_ref_tryget(&ctx->refs)) { | |
9575 | __io_uring_show_fdinfo(ctx, m); | |
9576 | percpu_ref_put(&ctx->refs); | |
9577 | } | |
9578 | } | |
bebdb65e | 9579 | #endif |
87ce955b | 9580 | |
2b188cc1 JA |
9581 | static const struct file_operations io_uring_fops = { |
9582 | .release = io_uring_release, | |
fcb323cc | 9583 | .flush = io_uring_flush, |
2b188cc1 | 9584 | .mmap = io_uring_mmap, |
6c5c240e RP |
9585 | #ifndef CONFIG_MMU |
9586 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9587 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9588 | #endif | |
2b188cc1 JA |
9589 | .poll = io_uring_poll, |
9590 | .fasync = io_uring_fasync, | |
bebdb65e | 9591 | #ifdef CONFIG_PROC_FS |
87ce955b | 9592 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9593 | #endif |
2b188cc1 JA |
9594 | }; |
9595 | ||
9596 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9597 | struct io_uring_params *p) | |
9598 | { | |
75b28aff HV |
9599 | struct io_rings *rings; |
9600 | size_t size, sq_array_offset; | |
2b188cc1 | 9601 | |
bd740481 JA |
9602 | /* make sure these are sane, as we already accounted them */ |
9603 | ctx->sq_entries = p->sq_entries; | |
9604 | ctx->cq_entries = p->cq_entries; | |
9605 | ||
75b28aff HV |
9606 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9607 | if (size == SIZE_MAX) | |
9608 | return -EOVERFLOW; | |
9609 | ||
9610 | rings = io_mem_alloc(size); | |
9611 | if (!rings) | |
2b188cc1 JA |
9612 | return -ENOMEM; |
9613 | ||
75b28aff HV |
9614 | ctx->rings = rings; |
9615 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9616 | rings->sq_ring_mask = p->sq_entries - 1; | |
9617 | rings->cq_ring_mask = p->cq_entries - 1; | |
9618 | rings->sq_ring_entries = p->sq_entries; | |
9619 | rings->cq_ring_entries = p->cq_entries; | |
9620 | ctx->sq_mask = rings->sq_ring_mask; | |
9621 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9622 | |
9623 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9624 | if (size == SIZE_MAX) { |
9625 | io_mem_free(ctx->rings); | |
9626 | ctx->rings = NULL; | |
2b188cc1 | 9627 | return -EOVERFLOW; |
eb065d30 | 9628 | } |
2b188cc1 JA |
9629 | |
9630 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9631 | if (!ctx->sq_sqes) { |
9632 | io_mem_free(ctx->rings); | |
9633 | ctx->rings = NULL; | |
2b188cc1 | 9634 | return -ENOMEM; |
eb065d30 | 9635 | } |
2b188cc1 | 9636 | |
2b188cc1 JA |
9637 | return 0; |
9638 | } | |
9639 | ||
9faadcc8 PB |
9640 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9641 | { | |
9642 | int ret, fd; | |
9643 | ||
9644 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9645 | if (fd < 0) | |
9646 | return fd; | |
9647 | ||
9648 | ret = io_uring_add_task_file(ctx, file); | |
9649 | if (ret) { | |
9650 | put_unused_fd(fd); | |
9651 | return ret; | |
9652 | } | |
9653 | fd_install(fd, file); | |
9654 | return fd; | |
9655 | } | |
9656 | ||
2b188cc1 JA |
9657 | /* |
9658 | * Allocate an anonymous fd, this is what constitutes the application | |
9659 | * visible backing of an io_uring instance. The application mmaps this | |
9660 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9661 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9662 | */ | |
9faadcc8 | 9663 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9664 | { |
9665 | struct file *file; | |
9faadcc8 | 9666 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9667 | int ret; |
9668 | ||
2b188cc1 JA |
9669 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9670 | &ctx->ring_sock); | |
9671 | if (ret) | |
9faadcc8 | 9672 | return ERR_PTR(ret); |
2b188cc1 JA |
9673 | #endif |
9674 | ||
2b188cc1 JA |
9675 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9676 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9677 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9678 | if (IS_ERR(file)) { |
9679 | sock_release(ctx->ring_sock); | |
9680 | ctx->ring_sock = NULL; | |
9681 | } else { | |
9682 | ctx->ring_sock->file = file; | |
0f212204 | 9683 | } |
2b188cc1 | 9684 | #endif |
9faadcc8 | 9685 | return file; |
2b188cc1 JA |
9686 | } |
9687 | ||
7f13657d XW |
9688 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9689 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9690 | { |
9691 | struct user_struct *user = NULL; | |
9692 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9693 | struct file *file; |
aad5d8da | 9694 | bool limit_mem; |
2b188cc1 JA |
9695 | int ret; |
9696 | ||
8110c1a6 | 9697 | if (!entries) |
2b188cc1 | 9698 | return -EINVAL; |
8110c1a6 JA |
9699 | if (entries > IORING_MAX_ENTRIES) { |
9700 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9701 | return -EINVAL; | |
9702 | entries = IORING_MAX_ENTRIES; | |
9703 | } | |
2b188cc1 JA |
9704 | |
9705 | /* | |
9706 | * Use twice as many entries for the CQ ring. It's possible for the | |
9707 | * application to drive a higher depth than the size of the SQ ring, | |
9708 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9709 | * some flexibility in overcommitting a bit. If the application has |
9710 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9711 | * of CQ ring entries manually. | |
2b188cc1 JA |
9712 | */ |
9713 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9714 | if (p->flags & IORING_SETUP_CQSIZE) { |
9715 | /* | |
9716 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9717 | * to a power-of-two, if it isn't already. We do NOT impose | |
9718 | * any cq vs sq ring sizing. | |
9719 | */ | |
eb2667b3 | 9720 | if (!p->cq_entries) |
33a107f0 | 9721 | return -EINVAL; |
8110c1a6 JA |
9722 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9723 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9724 | return -EINVAL; | |
9725 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9726 | } | |
eb2667b3 JQ |
9727 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9728 | if (p->cq_entries < p->sq_entries) | |
9729 | return -EINVAL; | |
33a107f0 JA |
9730 | } else { |
9731 | p->cq_entries = 2 * p->sq_entries; | |
9732 | } | |
2b188cc1 JA |
9733 | |
9734 | user = get_uid(current_user()); | |
aad5d8da | 9735 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9736 | |
aad5d8da | 9737 | if (limit_mem) { |
a087e2b5 | 9738 | ret = __io_account_mem(user, |
2b188cc1 JA |
9739 | ring_pages(p->sq_entries, p->cq_entries)); |
9740 | if (ret) { | |
9741 | free_uid(user); | |
9742 | return ret; | |
9743 | } | |
9744 | } | |
9745 | ||
9746 | ctx = io_ring_ctx_alloc(p); | |
9747 | if (!ctx) { | |
aad5d8da | 9748 | if (limit_mem) |
a087e2b5 | 9749 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9750 | p->cq_entries)); |
9751 | free_uid(user); | |
9752 | return -ENOMEM; | |
9753 | } | |
9754 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9755 | ctx->user = user; |
0b8c0ec7 | 9756 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9757 | #ifdef CONFIG_AUDIT |
9758 | ctx->loginuid = current->loginuid; | |
9759 | ctx->sessionid = current->sessionid; | |
9760 | #endif | |
2aede0e4 JA |
9761 | ctx->sqo_task = get_task_struct(current); |
9762 | ||
9763 | /* | |
9764 | * This is just grabbed for accounting purposes. When a process exits, | |
9765 | * the mm is exited and dropped before the files, hence we need to hang | |
9766 | * on to this mm purely for the purposes of being able to unaccount | |
9767 | * memory (locked/pinned vm). It's not used for anything else. | |
9768 | */ | |
6b7898eb | 9769 | mmgrab(current->mm); |
2aede0e4 | 9770 | ctx->mm_account = current->mm; |
6b7898eb | 9771 | |
91d8f519 DZ |
9772 | #ifdef CONFIG_BLK_CGROUP |
9773 | /* | |
9774 | * The sq thread will belong to the original cgroup it was inited in. | |
9775 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9776 | * issued bios will be associated with the closest cgroup later in the | |
9777 | * block layer. | |
9778 | */ | |
9779 | rcu_read_lock(); | |
9780 | ctx->sqo_blkcg_css = blkcg_css(); | |
9781 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9782 | rcu_read_unlock(); | |
9783 | if (!ret) { | |
9784 | /* don't init against a dying cgroup, have the user try again */ | |
9785 | ctx->sqo_blkcg_css = NULL; | |
9786 | ret = -ENODEV; | |
9787 | goto err; | |
9788 | } | |
9789 | #endif | |
6b7898eb | 9790 | |
f74441e6 JA |
9791 | /* |
9792 | * Account memory _before_ installing the file descriptor. Once | |
9793 | * the descriptor is installed, it can get closed at any time. Also | |
9794 | * do this before hitting the general error path, as ring freeing | |
9795 | * will un-account as well. | |
9796 | */ | |
9797 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9798 | ACCT_LOCKED); | |
9799 | ctx->limit_mem = limit_mem; | |
9800 | ||
2b188cc1 JA |
9801 | ret = io_allocate_scq_urings(ctx, p); |
9802 | if (ret) | |
9803 | goto err; | |
9804 | ||
7e84e1c7 | 9805 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9806 | if (ret) |
9807 | goto err; | |
9808 | ||
7e84e1c7 SG |
9809 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9810 | io_sq_offload_start(ctx); | |
9811 | ||
2b188cc1 | 9812 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9813 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9814 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9815 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9816 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9817 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9818 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9819 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9820 | |
9821 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9822 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9823 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9824 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9825 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9826 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9827 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9828 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9829 | |
7f13657d XW |
9830 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9831 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9832 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9833 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9834 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9835 | |
9836 | if (copy_to_user(params, p, sizeof(*p))) { | |
9837 | ret = -EFAULT; | |
9838 | goto err; | |
9839 | } | |
d1719f70 | 9840 | |
9faadcc8 PB |
9841 | file = io_uring_get_file(ctx); |
9842 | if (IS_ERR(file)) { | |
9843 | ret = PTR_ERR(file); | |
9844 | goto err; | |
9845 | } | |
9846 | ||
044c1ab3 JA |
9847 | /* |
9848 | * Install ring fd as the very last thing, so we don't risk someone | |
9849 | * having closed it before we finish setup | |
9850 | */ | |
9faadcc8 PB |
9851 | ret = io_uring_install_fd(ctx, file); |
9852 | if (ret < 0) { | |
06585c49 | 9853 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9854 | /* fput will clean it up */ |
9855 | fput(file); | |
9856 | return ret; | |
9857 | } | |
044c1ab3 | 9858 | |
c826bd7a | 9859 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9860 | return ret; |
9861 | err: | |
d9d05217 | 9862 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9863 | io_ring_ctx_wait_and_kill(ctx); |
9864 | return ret; | |
9865 | } | |
9866 | ||
9867 | /* | |
9868 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9869 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9870 | * params structure passed in. | |
9871 | */ | |
9872 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9873 | { | |
9874 | struct io_uring_params p; | |
2b188cc1 JA |
9875 | int i; |
9876 | ||
9877 | if (copy_from_user(&p, params, sizeof(p))) | |
9878 | return -EFAULT; | |
9879 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9880 | if (p.resv[i]) | |
9881 | return -EINVAL; | |
9882 | } | |
9883 | ||
6c271ce2 | 9884 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9885 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9886 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9887 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9888 | return -EINVAL; |
9889 | ||
7f13657d | 9890 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9891 | } |
9892 | ||
9893 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9894 | struct io_uring_params __user *, params) | |
9895 | { | |
9896 | return io_uring_setup(entries, params); | |
9897 | } | |
9898 | ||
66f4af93 JA |
9899 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9900 | { | |
9901 | struct io_uring_probe *p; | |
9902 | size_t size; | |
9903 | int i, ret; | |
9904 | ||
9905 | size = struct_size(p, ops, nr_args); | |
9906 | if (size == SIZE_MAX) | |
9907 | return -EOVERFLOW; | |
9908 | p = kzalloc(size, GFP_KERNEL); | |
9909 | if (!p) | |
9910 | return -ENOMEM; | |
9911 | ||
9912 | ret = -EFAULT; | |
9913 | if (copy_from_user(p, arg, size)) | |
9914 | goto out; | |
9915 | ret = -EINVAL; | |
9916 | if (memchr_inv(p, 0, size)) | |
9917 | goto out; | |
9918 | ||
9919 | p->last_op = IORING_OP_LAST - 1; | |
9920 | if (nr_args > IORING_OP_LAST) | |
9921 | nr_args = IORING_OP_LAST; | |
9922 | ||
9923 | for (i = 0; i < nr_args; i++) { | |
9924 | p->ops[i].op = i; | |
9925 | if (!io_op_defs[i].not_supported) | |
9926 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9927 | } | |
9928 | p->ops_len = i; | |
9929 | ||
9930 | ret = 0; | |
9931 | if (copy_to_user(arg, p, size)) | |
9932 | ret = -EFAULT; | |
9933 | out: | |
9934 | kfree(p); | |
9935 | return ret; | |
9936 | } | |
9937 | ||
071698e1 JA |
9938 | static int io_register_personality(struct io_ring_ctx *ctx) |
9939 | { | |
1e6fa521 JA |
9940 | struct io_identity *id; |
9941 | int ret; | |
071698e1 | 9942 | |
1e6fa521 JA |
9943 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9944 | if (unlikely(!id)) | |
9945 | return -ENOMEM; | |
9946 | ||
9947 | io_init_identity(id); | |
9948 | id->creds = get_current_cred(); | |
9949 | ||
9950 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9951 | if (ret < 0) { | |
9952 | put_cred(id->creds); | |
9953 | kfree(id); | |
9954 | } | |
9955 | return ret; | |
071698e1 JA |
9956 | } |
9957 | ||
9958 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
9959 | { | |
1e6fa521 | 9960 | struct io_identity *iod; |
071698e1 | 9961 | |
1e6fa521 JA |
9962 | iod = idr_remove(&ctx->personality_idr, id); |
9963 | if (iod) { | |
9964 | put_cred(iod->creds); | |
9965 | if (refcount_dec_and_test(&iod->count)) | |
9966 | kfree(iod); | |
071698e1 JA |
9967 | return 0; |
9968 | } | |
9969 | ||
9970 | return -EINVAL; | |
9971 | } | |
9972 | ||
21b55dbc SG |
9973 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9974 | unsigned int nr_args) | |
9975 | { | |
9976 | struct io_uring_restriction *res; | |
9977 | size_t size; | |
9978 | int i, ret; | |
9979 | ||
7e84e1c7 SG |
9980 | /* Restrictions allowed only if rings started disabled */ |
9981 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9982 | return -EBADFD; | |
9983 | ||
21b55dbc | 9984 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9985 | if (ctx->restrictions.registered) |
21b55dbc SG |
9986 | return -EBUSY; |
9987 | ||
9988 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9989 | return -EINVAL; | |
9990 | ||
9991 | size = array_size(nr_args, sizeof(*res)); | |
9992 | if (size == SIZE_MAX) | |
9993 | return -EOVERFLOW; | |
9994 | ||
9995 | res = memdup_user(arg, size); | |
9996 | if (IS_ERR(res)) | |
9997 | return PTR_ERR(res); | |
9998 | ||
9999 | ret = 0; | |
10000 | ||
10001 | for (i = 0; i < nr_args; i++) { | |
10002 | switch (res[i].opcode) { | |
10003 | case IORING_RESTRICTION_REGISTER_OP: | |
10004 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10005 | ret = -EINVAL; | |
10006 | goto out; | |
10007 | } | |
10008 | ||
10009 | __set_bit(res[i].register_op, | |
10010 | ctx->restrictions.register_op); | |
10011 | break; | |
10012 | case IORING_RESTRICTION_SQE_OP: | |
10013 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10014 | ret = -EINVAL; | |
10015 | goto out; | |
10016 | } | |
10017 | ||
10018 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10019 | break; | |
10020 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10021 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10022 | break; | |
10023 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10024 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10025 | break; | |
10026 | default: | |
10027 | ret = -EINVAL; | |
10028 | goto out; | |
10029 | } | |
10030 | } | |
10031 | ||
10032 | out: | |
10033 | /* Reset all restrictions if an error happened */ | |
10034 | if (ret != 0) | |
10035 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10036 | else | |
7e84e1c7 | 10037 | ctx->restrictions.registered = true; |
21b55dbc SG |
10038 | |
10039 | kfree(res); | |
10040 | return ret; | |
10041 | } | |
10042 | ||
7e84e1c7 SG |
10043 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10044 | { | |
10045 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10046 | return -EBADFD; | |
10047 | ||
10048 | if (ctx->restrictions.registered) | |
10049 | ctx->restricted = 1; | |
10050 | ||
10051 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
10052 | ||
10053 | io_sq_offload_start(ctx); | |
10054 | ||
10055 | return 0; | |
10056 | } | |
10057 | ||
071698e1 JA |
10058 | static bool io_register_op_must_quiesce(int op) |
10059 | { | |
10060 | switch (op) { | |
10061 | case IORING_UNREGISTER_FILES: | |
10062 | case IORING_REGISTER_FILES_UPDATE: | |
10063 | case IORING_REGISTER_PROBE: | |
10064 | case IORING_REGISTER_PERSONALITY: | |
10065 | case IORING_UNREGISTER_PERSONALITY: | |
10066 | return false; | |
10067 | default: | |
10068 | return true; | |
10069 | } | |
10070 | } | |
10071 | ||
edafccee JA |
10072 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10073 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10074 | __releases(ctx->uring_lock) |
10075 | __acquires(ctx->uring_lock) | |
edafccee JA |
10076 | { |
10077 | int ret; | |
10078 | ||
35fa71a0 JA |
10079 | /* |
10080 | * We're inside the ring mutex, if the ref is already dying, then | |
10081 | * someone else killed the ctx or is already going through | |
10082 | * io_uring_register(). | |
10083 | */ | |
10084 | if (percpu_ref_is_dying(&ctx->refs)) | |
10085 | return -ENXIO; | |
10086 | ||
071698e1 | 10087 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10088 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10089 | |
05f3fb3c JA |
10090 | /* |
10091 | * Drop uring mutex before waiting for references to exit. If | |
10092 | * another thread is currently inside io_uring_enter() it might | |
10093 | * need to grab the uring_lock to make progress. If we hold it | |
10094 | * here across the drain wait, then we can deadlock. It's safe | |
10095 | * to drop the mutex here, since no new references will come in | |
10096 | * after we've killed the percpu ref. | |
10097 | */ | |
10098 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10099 | do { |
10100 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10101 | if (!ret) | |
10102 | break; | |
ed6930c9 JA |
10103 | ret = io_run_task_work_sig(); |
10104 | if (ret < 0) | |
10105 | break; | |
af9c1a44 JA |
10106 | } while (1); |
10107 | ||
05f3fb3c | 10108 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10109 | |
c150368b JA |
10110 | if (ret) { |
10111 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
10112 | goto out_quiesce; |
10113 | } | |
10114 | } | |
10115 | ||
10116 | if (ctx->restricted) { | |
10117 | if (opcode >= IORING_REGISTER_LAST) { | |
10118 | ret = -EINVAL; | |
10119 | goto out; | |
10120 | } | |
10121 | ||
10122 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
10123 | ret = -EACCES; | |
c150368b JA |
10124 | goto out; |
10125 | } | |
05f3fb3c | 10126 | } |
edafccee JA |
10127 | |
10128 | switch (opcode) { | |
10129 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 10130 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
10131 | break; |
10132 | case IORING_UNREGISTER_BUFFERS: | |
10133 | ret = -EINVAL; | |
10134 | if (arg || nr_args) | |
10135 | break; | |
0a96bbe4 | 10136 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10137 | break; |
6b06314c JA |
10138 | case IORING_REGISTER_FILES: |
10139 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
10140 | break; | |
10141 | case IORING_UNREGISTER_FILES: | |
10142 | ret = -EINVAL; | |
10143 | if (arg || nr_args) | |
10144 | break; | |
10145 | ret = io_sqe_files_unregister(ctx); | |
10146 | break; | |
c3a31e60 JA |
10147 | case IORING_REGISTER_FILES_UPDATE: |
10148 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
10149 | break; | |
9b402849 | 10150 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10151 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10152 | ret = -EINVAL; |
10153 | if (nr_args != 1) | |
10154 | break; | |
10155 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10156 | if (ret) |
10157 | break; | |
10158 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10159 | ctx->eventfd_async = 1; | |
10160 | else | |
10161 | ctx->eventfd_async = 0; | |
9b402849 JA |
10162 | break; |
10163 | case IORING_UNREGISTER_EVENTFD: | |
10164 | ret = -EINVAL; | |
10165 | if (arg || nr_args) | |
10166 | break; | |
10167 | ret = io_eventfd_unregister(ctx); | |
10168 | break; | |
66f4af93 JA |
10169 | case IORING_REGISTER_PROBE: |
10170 | ret = -EINVAL; | |
10171 | if (!arg || nr_args > 256) | |
10172 | break; | |
10173 | ret = io_probe(ctx, arg, nr_args); | |
10174 | break; | |
071698e1 JA |
10175 | case IORING_REGISTER_PERSONALITY: |
10176 | ret = -EINVAL; | |
10177 | if (arg || nr_args) | |
10178 | break; | |
10179 | ret = io_register_personality(ctx); | |
10180 | break; | |
10181 | case IORING_UNREGISTER_PERSONALITY: | |
10182 | ret = -EINVAL; | |
10183 | if (arg) | |
10184 | break; | |
10185 | ret = io_unregister_personality(ctx, nr_args); | |
10186 | break; | |
7e84e1c7 SG |
10187 | case IORING_REGISTER_ENABLE_RINGS: |
10188 | ret = -EINVAL; | |
10189 | if (arg || nr_args) | |
10190 | break; | |
10191 | ret = io_register_enable_rings(ctx); | |
10192 | break; | |
21b55dbc SG |
10193 | case IORING_REGISTER_RESTRICTIONS: |
10194 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10195 | break; | |
edafccee JA |
10196 | default: |
10197 | ret = -EINVAL; | |
10198 | break; | |
10199 | } | |
10200 | ||
21b55dbc | 10201 | out: |
071698e1 | 10202 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10203 | /* bring the ctx back to life */ |
05f3fb3c | 10204 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 10205 | out_quiesce: |
0f158b4c | 10206 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10207 | } |
edafccee JA |
10208 | return ret; |
10209 | } | |
10210 | ||
10211 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10212 | void __user *, arg, unsigned int, nr_args) | |
10213 | { | |
10214 | struct io_ring_ctx *ctx; | |
10215 | long ret = -EBADF; | |
10216 | struct fd f; | |
10217 | ||
10218 | f = fdget(fd); | |
10219 | if (!f.file) | |
10220 | return -EBADF; | |
10221 | ||
10222 | ret = -EOPNOTSUPP; | |
10223 | if (f.file->f_op != &io_uring_fops) | |
10224 | goto out_fput; | |
10225 | ||
10226 | ctx = f.file->private_data; | |
10227 | ||
10228 | mutex_lock(&ctx->uring_lock); | |
10229 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10230 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10231 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10232 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10233 | out_fput: |
10234 | fdput(f); | |
10235 | return ret; | |
10236 | } | |
10237 | ||
2b188cc1 JA |
10238 | static int __init io_uring_init(void) |
10239 | { | |
d7f62e82 SM |
10240 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10241 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10242 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10243 | } while (0) | |
10244 | ||
10245 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10246 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10247 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10248 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10249 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10250 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10251 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10252 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10253 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10254 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10255 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10256 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10257 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10258 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10259 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10260 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10261 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10262 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10263 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10264 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10265 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10266 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10267 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10268 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10269 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10270 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10271 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10272 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10273 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10274 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10275 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10276 | |
d3656344 | 10277 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10278 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
10279 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
10280 | return 0; | |
10281 | }; | |
10282 | __initcall(io_uring_init); |