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; | |
b5dba59e JA |
426 | int fd; |
427 | }; | |
428 | ||
ad8a48ac JA |
429 | struct io_timeout_data { |
430 | struct io_kiocb *req; | |
431 | struct hrtimer timer; | |
432 | struct timespec64 ts; | |
433 | enum hrtimer_mode mode; | |
434 | }; | |
435 | ||
8ed8d3c3 JA |
436 | struct io_accept { |
437 | struct file *file; | |
438 | struct sockaddr __user *addr; | |
439 | int __user *addr_len; | |
440 | int flags; | |
09952e3e | 441 | unsigned long nofile; |
8ed8d3c3 JA |
442 | }; |
443 | ||
444 | struct io_sync { | |
445 | struct file *file; | |
446 | loff_t len; | |
447 | loff_t off; | |
448 | int flags; | |
d63d1b5e | 449 | int mode; |
8ed8d3c3 JA |
450 | }; |
451 | ||
fbf23849 JA |
452 | struct io_cancel { |
453 | struct file *file; | |
454 | u64 addr; | |
455 | }; | |
456 | ||
b29472ee JA |
457 | struct io_timeout { |
458 | struct file *file; | |
bfe68a22 PB |
459 | u32 off; |
460 | u32 target_seq; | |
135fcde8 | 461 | struct list_head list; |
90cd7e42 PB |
462 | /* head of the link, used by linked timeouts only */ |
463 | struct io_kiocb *head; | |
b29472ee JA |
464 | }; |
465 | ||
0bdf7a2d PB |
466 | struct io_timeout_rem { |
467 | struct file *file; | |
468 | u64 addr; | |
9c8e11b3 PB |
469 | |
470 | /* timeout update */ | |
471 | struct timespec64 ts; | |
472 | u32 flags; | |
0bdf7a2d PB |
473 | }; |
474 | ||
9adbd45d JA |
475 | struct io_rw { |
476 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
477 | struct kiocb kiocb; | |
478 | u64 addr; | |
479 | u64 len; | |
480 | }; | |
481 | ||
3fbb51c1 JA |
482 | struct io_connect { |
483 | struct file *file; | |
484 | struct sockaddr __user *addr; | |
485 | int addr_len; | |
486 | }; | |
487 | ||
e47293fd JA |
488 | struct io_sr_msg { |
489 | struct file *file; | |
fddaface | 490 | union { |
270a5940 | 491 | struct user_msghdr __user *umsg; |
fddaface JA |
492 | void __user *buf; |
493 | }; | |
e47293fd | 494 | int msg_flags; |
bcda7baa | 495 | int bgid; |
fddaface | 496 | size_t len; |
bcda7baa | 497 | struct io_buffer *kbuf; |
e47293fd JA |
498 | }; |
499 | ||
15b71abe JA |
500 | struct io_open { |
501 | struct file *file; | |
502 | int dfd; | |
15b71abe | 503 | struct filename *filename; |
c12cedf2 | 504 | struct open_how how; |
4022e7af | 505 | unsigned long nofile; |
15b71abe JA |
506 | }; |
507 | ||
269bbe5f | 508 | struct io_rsrc_update { |
05f3fb3c JA |
509 | struct file *file; |
510 | u64 arg; | |
511 | u32 nr_args; | |
512 | u32 offset; | |
513 | }; | |
514 | ||
4840e418 JA |
515 | struct io_fadvise { |
516 | struct file *file; | |
517 | u64 offset; | |
518 | u32 len; | |
519 | u32 advice; | |
520 | }; | |
521 | ||
c1ca757b JA |
522 | struct io_madvise { |
523 | struct file *file; | |
524 | u64 addr; | |
525 | u32 len; | |
526 | u32 advice; | |
527 | }; | |
528 | ||
3e4827b0 JA |
529 | struct io_epoll { |
530 | struct file *file; | |
531 | int epfd; | |
532 | int op; | |
533 | int fd; | |
534 | struct epoll_event event; | |
e47293fd JA |
535 | }; |
536 | ||
7d67af2c PB |
537 | struct io_splice { |
538 | struct file *file_out; | |
539 | struct file *file_in; | |
540 | loff_t off_out; | |
541 | loff_t off_in; | |
542 | u64 len; | |
543 | unsigned int flags; | |
544 | }; | |
545 | ||
ddf0322d JA |
546 | struct io_provide_buf { |
547 | struct file *file; | |
548 | __u64 addr; | |
549 | __s32 len; | |
550 | __u32 bgid; | |
551 | __u16 nbufs; | |
552 | __u16 bid; | |
553 | }; | |
554 | ||
1d9e1288 BM |
555 | struct io_statx { |
556 | struct file *file; | |
557 | int dfd; | |
558 | unsigned int mask; | |
559 | unsigned int flags; | |
e62753e4 | 560 | const char __user *filename; |
1d9e1288 BM |
561 | struct statx __user *buffer; |
562 | }; | |
563 | ||
36f4fa68 JA |
564 | struct io_shutdown { |
565 | struct file *file; | |
566 | int how; | |
567 | }; | |
568 | ||
80a261fd JA |
569 | struct io_rename { |
570 | struct file *file; | |
571 | int old_dfd; | |
572 | int new_dfd; | |
573 | struct filename *oldpath; | |
574 | struct filename *newpath; | |
575 | int flags; | |
576 | }; | |
577 | ||
14a1143b JA |
578 | struct io_unlink { |
579 | struct file *file; | |
580 | int dfd; | |
581 | int flags; | |
582 | struct filename *filename; | |
583 | }; | |
584 | ||
3ca405eb PB |
585 | struct io_completion { |
586 | struct file *file; | |
587 | struct list_head list; | |
0f7e466b | 588 | int cflags; |
3ca405eb PB |
589 | }; |
590 | ||
f499a021 JA |
591 | struct io_async_connect { |
592 | struct sockaddr_storage address; | |
593 | }; | |
594 | ||
03b1230c JA |
595 | struct io_async_msghdr { |
596 | struct iovec fast_iov[UIO_FASTIOV]; | |
597 | struct iovec *iov; | |
598 | struct sockaddr __user *uaddr; | |
599 | struct msghdr msg; | |
b537916c | 600 | struct sockaddr_storage addr; |
03b1230c JA |
601 | }; |
602 | ||
f67676d1 JA |
603 | struct io_async_rw { |
604 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
605 | const struct iovec *free_iovec; |
606 | struct iov_iter iter; | |
227c0c96 | 607 | size_t bytes_done; |
bcf5a063 | 608 | struct wait_page_queue wpq; |
f67676d1 JA |
609 | }; |
610 | ||
6b47ee6e PB |
611 | enum { |
612 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
613 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
614 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
615 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
616 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 617 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 618 | |
6b47ee6e PB |
619 | REQ_F_FAIL_LINK_BIT, |
620 | REQ_F_INFLIGHT_BIT, | |
621 | REQ_F_CUR_POS_BIT, | |
622 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 623 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 624 | REQ_F_ISREG_BIT, |
99bc4c38 | 625 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 626 | REQ_F_POLLED_BIT, |
bcda7baa | 627 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 628 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 629 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 630 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 631 | REQ_F_COMPLETE_INLINE_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), | |
e342c807 PB |
675 | /* completion is deferred through io_comp_state */ |
676 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
677 | }; |
678 | ||
679 | struct async_poll { | |
680 | struct io_poll_iocb poll; | |
807abcb0 | 681 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
682 | }; |
683 | ||
09bb8394 JA |
684 | /* |
685 | * NOTE! Each of the iocb union members has the file pointer | |
686 | * as the first entry in their struct definition. So you can | |
687 | * access the file pointer through any of the sub-structs, | |
688 | * or directly as just 'ki_filp' in this struct. | |
689 | */ | |
2b188cc1 | 690 | struct io_kiocb { |
221c5eb2 | 691 | union { |
09bb8394 | 692 | struct file *file; |
9adbd45d | 693 | struct io_rw rw; |
221c5eb2 | 694 | struct io_poll_iocb poll; |
018043be | 695 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
696 | struct io_accept accept; |
697 | struct io_sync sync; | |
fbf23849 | 698 | struct io_cancel cancel; |
b29472ee | 699 | struct io_timeout timeout; |
0bdf7a2d | 700 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 701 | struct io_connect connect; |
e47293fd | 702 | struct io_sr_msg sr_msg; |
15b71abe | 703 | struct io_open open; |
b5dba59e | 704 | struct io_close close; |
269bbe5f | 705 | struct io_rsrc_update rsrc_update; |
4840e418 | 706 | struct io_fadvise fadvise; |
c1ca757b | 707 | struct io_madvise madvise; |
3e4827b0 | 708 | struct io_epoll epoll; |
7d67af2c | 709 | struct io_splice splice; |
ddf0322d | 710 | struct io_provide_buf pbuf; |
1d9e1288 | 711 | struct io_statx statx; |
36f4fa68 | 712 | struct io_shutdown shutdown; |
80a261fd | 713 | struct io_rename rename; |
14a1143b | 714 | struct io_unlink unlink; |
3ca405eb PB |
715 | /* use only after cleaning per-op data, see io_clean_op() */ |
716 | struct io_completion compl; | |
221c5eb2 | 717 | }; |
2b188cc1 | 718 | |
e8c2bc1f JA |
719 | /* opcode allocated if it needs to store data for async defer */ |
720 | void *async_data; | |
d625c6ee | 721 | u8 opcode; |
65a6543d XW |
722 | /* polled IO has completed */ |
723 | u8 iopoll_completed; | |
2b188cc1 | 724 | |
4f4eeba8 | 725 | u16 buf_index; |
9cf7c104 | 726 | u32 result; |
4f4eeba8 | 727 | |
010e8e6b PB |
728 | struct io_ring_ctx *ctx; |
729 | unsigned int flags; | |
730 | refcount_t refs; | |
731 | struct task_struct *task; | |
732 | u64 user_data; | |
d7718a9d | 733 | |
f2f87370 | 734 | struct io_kiocb *link; |
269bbe5f | 735 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 736 | |
d21ffe7e PB |
737 | /* |
738 | * 1. used with ctx->iopoll_list with reads/writes | |
739 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
740 | */ | |
010e8e6b | 741 | struct list_head inflight_entry; |
010e8e6b PB |
742 | struct callback_head task_work; |
743 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ | |
744 | struct hlist_node hash_node; | |
745 | struct async_poll *apoll; | |
746 | struct io_wq_work work; | |
2b188cc1 | 747 | }; |
05589553 | 748 | |
27dc8338 PB |
749 | struct io_defer_entry { |
750 | struct list_head list; | |
751 | struct io_kiocb *req; | |
9cf7c104 | 752 | u32 seq; |
2b188cc1 JA |
753 | }; |
754 | ||
def596e9 | 755 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 756 | |
013538bd JA |
757 | struct io_comp_state { |
758 | unsigned int nr; | |
759 | struct list_head list; | |
760 | struct io_ring_ctx *ctx; | |
761 | }; | |
762 | ||
9a56a232 JA |
763 | struct io_submit_state { |
764 | struct blk_plug plug; | |
765 | ||
2579f913 JA |
766 | /* |
767 | * io_kiocb alloc cache | |
768 | */ | |
769 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 770 | unsigned int free_reqs; |
2579f913 | 771 | |
27926b68 JA |
772 | bool plug_started; |
773 | ||
013538bd JA |
774 | /* |
775 | * Batch completion logic | |
776 | */ | |
777 | struct io_comp_state comp; | |
778 | ||
9a56a232 JA |
779 | /* |
780 | * File reference cache | |
781 | */ | |
782 | struct file *file; | |
783 | unsigned int fd; | |
6e1271e6 | 784 | unsigned int file_refs; |
9a56a232 JA |
785 | unsigned int ios_left; |
786 | }; | |
787 | ||
d3656344 | 788 | struct io_op_def { |
d3656344 JA |
789 | /* needs req->file assigned */ |
790 | unsigned needs_file : 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] = { |
0f203765 | 920 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
d3656344 | 921 | }, |
0463b6c5 | 922 | [IORING_OP_FILES_UPDATE] = { |
0f203765 | 923 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
d3656344 | 924 | }, |
0463b6c5 | 925 | [IORING_OP_STATX] = { |
0f203765 JA |
926 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
927 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
d3656344 | 928 | }, |
0463b6c5 | 929 | [IORING_OP_READ] = { |
3a6820f2 JA |
930 | .needs_file = 1, |
931 | .unbound_nonreg_file = 1, | |
8a72758c | 932 | .pollin = 1, |
bcda7baa | 933 | .buffer_select = 1, |
27926b68 | 934 | .plug = 1, |
e8c2bc1f | 935 | .async_size = sizeof(struct io_async_rw), |
0f203765 | 936 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
3a6820f2 | 937 | }, |
0463b6c5 | 938 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
939 | .needs_file = 1, |
940 | .unbound_nonreg_file = 1, | |
8a72758c | 941 | .pollout = 1, |
27926b68 | 942 | .plug = 1, |
e8c2bc1f | 943 | .async_size = sizeof(struct io_async_rw), |
69228338 JA |
944 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
945 | IO_WQ_WORK_FSIZE, | |
3a6820f2 | 946 | }, |
0463b6c5 | 947 | [IORING_OP_FADVISE] = { |
4840e418 | 948 | .needs_file = 1, |
0f203765 | 949 | .work_flags = IO_WQ_WORK_BLKCG, |
4840e418 | 950 | }, |
0463b6c5 | 951 | [IORING_OP_MADVISE] = { |
0f203765 | 952 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
c1ca757b | 953 | }, |
0463b6c5 | 954 | [IORING_OP_SEND] = { |
fddaface JA |
955 | .needs_file = 1, |
956 | .unbound_nonreg_file = 1, | |
8a72758c | 957 | .pollout = 1, |
0f203765 | 958 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 959 | }, |
0463b6c5 | 960 | [IORING_OP_RECV] = { |
fddaface JA |
961 | .needs_file = 1, |
962 | .unbound_nonreg_file = 1, | |
8a72758c | 963 | .pollin = 1, |
bcda7baa | 964 | .buffer_select = 1, |
0f203765 | 965 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
fddaface | 966 | }, |
0463b6c5 | 967 | [IORING_OP_OPENAT2] = { |
0f203765 | 968 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
14587a46 | 969 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
cebdb986 | 970 | }, |
3e4827b0 JA |
971 | [IORING_OP_EPOLL_CTL] = { |
972 | .unbound_nonreg_file = 1, | |
0f203765 | 973 | .work_flags = IO_WQ_WORK_FILES, |
3e4827b0 | 974 | }, |
7d67af2c PB |
975 | [IORING_OP_SPLICE] = { |
976 | .needs_file = 1, | |
977 | .hash_reg_file = 1, | |
978 | .unbound_nonreg_file = 1, | |
0f203765 | 979 | .work_flags = IO_WQ_WORK_BLKCG, |
ddf0322d JA |
980 | }, |
981 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 982 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
983 | [IORING_OP_TEE] = { |
984 | .needs_file = 1, | |
985 | .hash_reg_file = 1, | |
986 | .unbound_nonreg_file = 1, | |
987 | }, | |
36f4fa68 JA |
988 | [IORING_OP_SHUTDOWN] = { |
989 | .needs_file = 1, | |
990 | }, | |
80a261fd JA |
991 | [IORING_OP_RENAMEAT] = { |
992 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
993 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
994 | }, | |
14a1143b JA |
995 | [IORING_OP_UNLINKAT] = { |
996 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | | |
997 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, | |
998 | }, | |
d3656344 JA |
999 | }; |
1000 | ||
2e0464d4 BM |
1001 | enum io_mem_account { |
1002 | ACCT_LOCKED, | |
1003 | ACCT_PINNED, | |
1004 | }; | |
1005 | ||
90df0853 PB |
1006 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
1007 | struct task_struct *task); | |
1008 | ||
269bbe5f | 1009 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 1010 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 1011 | struct io_ring_ctx *ctx); |
bc9744cd PB |
1012 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
1013 | struct fixed_rsrc_ref_node *ref_node); | |
1ffc5422 | 1014 | |
81b68a5c PB |
1015 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
1016 | struct io_comp_state *cs); | |
78e19bbe | 1017 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1018 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1019 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1020 | static void io_double_put_req(struct io_kiocb *req); |
94ae5e77 | 1021 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1022 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1023 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1024 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1025 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1026 | unsigned nr_args); |
3ca405eb | 1027 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1028 | static struct file *io_file_get(struct io_submit_state *state, |
1029 | struct io_kiocb *req, int fd, bool fixed); | |
c1379e24 | 1030 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
269bbe5f | 1031 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1032 | |
b63534c4 JA |
1033 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
1034 | struct iovec **iovec, struct iov_iter *iter, | |
1035 | bool needs_lock); | |
ff6165b2 JA |
1036 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1037 | const struct iovec *fast_iov, | |
227c0c96 | 1038 | struct iov_iter *iter, bool force); |
907d1df3 | 1039 | static void io_req_task_queue(struct io_kiocb *req); |
de0617e4 | 1040 | |
2b188cc1 JA |
1041 | static struct kmem_cache *req_cachep; |
1042 | ||
0918682b | 1043 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1044 | |
1045 | struct sock *io_uring_get_socket(struct file *file) | |
1046 | { | |
1047 | #if defined(CONFIG_UNIX) | |
1048 | if (file->f_op == &io_uring_fops) { | |
1049 | struct io_ring_ctx *ctx = file->private_data; | |
1050 | ||
1051 | return ctx->ring_sock->sk; | |
1052 | } | |
1053 | #endif | |
1054 | return NULL; | |
1055 | } | |
1056 | EXPORT_SYMBOL(io_uring_get_socket); | |
1057 | ||
f2f87370 PB |
1058 | #define io_for_each_link(pos, head) \ |
1059 | for (pos = (head); pos; pos = pos->link) | |
1060 | ||
3ca405eb PB |
1061 | static inline void io_clean_op(struct io_kiocb *req) |
1062 | { | |
9d5c8190 | 1063 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1064 | __io_clean_op(req); |
1065 | } | |
1066 | ||
36f72fe2 PB |
1067 | static inline void io_set_resource_node(struct io_kiocb *req) |
1068 | { | |
1069 | struct io_ring_ctx *ctx = req->ctx; | |
1070 | ||
269bbe5f BM |
1071 | if (!req->fixed_rsrc_refs) { |
1072 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1073 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1074 | } |
1075 | } | |
1076 | ||
08d23634 PB |
1077 | static bool io_match_task(struct io_kiocb *head, |
1078 | struct task_struct *task, | |
1079 | struct files_struct *files) | |
1080 | { | |
1081 | struct io_kiocb *req; | |
1082 | ||
84965ff8 JA |
1083 | if (task && head->task != task) { |
1084 | /* in terms of cancelation, always match if req task is dead */ | |
1085 | if (head->task->flags & PF_EXITING) | |
1086 | return true; | |
08d23634 | 1087 | return false; |
84965ff8 | 1088 | } |
08d23634 PB |
1089 | if (!files) |
1090 | return true; | |
1091 | ||
1092 | io_for_each_link(req, head) { | |
02a13674 JA |
1093 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
1094 | continue; | |
1095 | if (req->file && req->file->f_op == &io_uring_fops) | |
1096 | return true; | |
1097 | if ((req->work.flags & IO_WQ_WORK_FILES) && | |
08d23634 PB |
1098 | req->work.identity->files == files) |
1099 | return true; | |
1100 | } | |
1101 | return false; | |
1102 | } | |
1103 | ||
28cea78a | 1104 | static void io_sq_thread_drop_mm_files(void) |
c40f6379 | 1105 | { |
28cea78a | 1106 | struct files_struct *files = current->files; |
c40f6379 JA |
1107 | struct mm_struct *mm = current->mm; |
1108 | ||
1109 | if (mm) { | |
1110 | kthread_unuse_mm(mm); | |
1111 | mmput(mm); | |
4b70cf9d | 1112 | current->mm = NULL; |
c40f6379 | 1113 | } |
28cea78a JA |
1114 | if (files) { |
1115 | struct nsproxy *nsproxy = current->nsproxy; | |
1116 | ||
1117 | task_lock(current); | |
1118 | current->files = NULL; | |
1119 | current->nsproxy = NULL; | |
1120 | task_unlock(current); | |
1121 | put_files_struct(files); | |
1122 | put_nsproxy(nsproxy); | |
1123 | } | |
1124 | } | |
1125 | ||
1a38ffc9 | 1126 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
28cea78a | 1127 | { |
621fadc2 PB |
1128 | if (current->flags & PF_EXITING) |
1129 | return -EFAULT; | |
1130 | ||
28cea78a JA |
1131 | if (!current->files) { |
1132 | struct files_struct *files; | |
1133 | struct nsproxy *nsproxy; | |
1134 | ||
1135 | task_lock(ctx->sqo_task); | |
1136 | files = ctx->sqo_task->files; | |
1137 | if (!files) { | |
1138 | task_unlock(ctx->sqo_task); | |
1a38ffc9 | 1139 | return -EOWNERDEAD; |
28cea78a JA |
1140 | } |
1141 | atomic_inc(&files->count); | |
1142 | get_nsproxy(ctx->sqo_task->nsproxy); | |
1143 | nsproxy = ctx->sqo_task->nsproxy; | |
1144 | task_unlock(ctx->sqo_task); | |
1145 | ||
1146 | task_lock(current); | |
1147 | current->files = files; | |
1148 | current->nsproxy = nsproxy; | |
1149 | task_unlock(current); | |
1150 | } | |
1a38ffc9 | 1151 | return 0; |
c40f6379 JA |
1152 | } |
1153 | ||
1154 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) | |
1155 | { | |
4b70cf9d JA |
1156 | struct mm_struct *mm; |
1157 | ||
621fadc2 PB |
1158 | if (current->flags & PF_EXITING) |
1159 | return -EFAULT; | |
4b70cf9d JA |
1160 | if (current->mm) |
1161 | return 0; | |
1162 | ||
1163 | /* Should never happen */ | |
1164 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) | |
1165 | return -EFAULT; | |
1166 | ||
1167 | task_lock(ctx->sqo_task); | |
1168 | mm = ctx->sqo_task->mm; | |
1169 | if (unlikely(!mm || !mmget_not_zero(mm))) | |
1170 | mm = NULL; | |
1171 | task_unlock(ctx->sqo_task); | |
1172 | ||
1173 | if (mm) { | |
1174 | kthread_use_mm(mm); | |
1175 | return 0; | |
c40f6379 JA |
1176 | } |
1177 | ||
4b70cf9d | 1178 | return -EFAULT; |
c40f6379 JA |
1179 | } |
1180 | ||
28cea78a JA |
1181 | static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
1182 | struct io_kiocb *req) | |
c40f6379 | 1183 | { |
28cea78a | 1184 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
1a38ffc9 | 1185 | int ret; |
28cea78a JA |
1186 | |
1187 | if (def->work_flags & IO_WQ_WORK_MM) { | |
1a38ffc9 | 1188 | ret = __io_sq_thread_acquire_mm(ctx); |
28cea78a JA |
1189 | if (unlikely(ret)) |
1190 | return ret; | |
1191 | } | |
1192 | ||
1a38ffc9 PB |
1193 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
1194 | ret = __io_sq_thread_acquire_files(ctx); | |
1195 | if (unlikely(ret)) | |
1196 | return ret; | |
1197 | } | |
28cea78a JA |
1198 | |
1199 | return 0; | |
c40f6379 JA |
1200 | } |
1201 | ||
91d8f519 DZ |
1202 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
1203 | struct cgroup_subsys_state **cur_css) | |
1204 | ||
1205 | { | |
1206 | #ifdef CONFIG_BLK_CGROUP | |
1207 | /* puts the old one when swapping */ | |
1208 | if (*cur_css != ctx->sqo_blkcg_css) { | |
1209 | kthread_associate_blkcg(ctx->sqo_blkcg_css); | |
1210 | *cur_css = ctx->sqo_blkcg_css; | |
1211 | } | |
1212 | #endif | |
1213 | } | |
1214 | ||
1215 | static void io_sq_thread_unassociate_blkcg(void) | |
1216 | { | |
1217 | #ifdef CONFIG_BLK_CGROUP | |
1218 | kthread_associate_blkcg(NULL); | |
1219 | #endif | |
1220 | } | |
1221 | ||
c40f6379 JA |
1222 | static inline void req_set_fail_links(struct io_kiocb *req) |
1223 | { | |
1224 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1225 | req->flags |= REQ_F_FAIL_LINK; | |
1226 | } | |
4a38aed2 | 1227 | |
1e6fa521 JA |
1228 | /* |
1229 | * None of these are dereferenced, they are simply used to check if any of | |
1230 | * them have changed. If we're under current and check they are still the | |
1231 | * same, we're fine to grab references to them for actual out-of-line use. | |
1232 | */ | |
1233 | static void io_init_identity(struct io_identity *id) | |
1234 | { | |
1235 | id->files = current->files; | |
1236 | id->mm = current->mm; | |
1237 | #ifdef CONFIG_BLK_CGROUP | |
1238 | rcu_read_lock(); | |
1239 | id->blkcg_css = blkcg_css(); | |
1240 | rcu_read_unlock(); | |
1241 | #endif | |
1242 | id->creds = current_cred(); | |
1243 | id->nsproxy = current->nsproxy; | |
1244 | id->fs = current->fs; | |
1245 | id->fsize = rlimit(RLIMIT_FSIZE); | |
4ea33a97 JA |
1246 | #ifdef CONFIG_AUDIT |
1247 | id->loginuid = current->loginuid; | |
1248 | id->sessionid = current->sessionid; | |
1249 | #endif | |
1e6fa521 JA |
1250 | refcount_set(&id->count, 1); |
1251 | } | |
1252 | ||
ec99ca6c PB |
1253 | static inline void __io_req_init_async(struct io_kiocb *req) |
1254 | { | |
1255 | memset(&req->work, 0, sizeof(req->work)); | |
1256 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1257 | } | |
1258 | ||
7cdaf587 XW |
1259 | /* |
1260 | * Note: must call io_req_init_async() for the first time you | |
1261 | * touch any members of io_wq_work. | |
1262 | */ | |
1263 | static inline void io_req_init_async(struct io_kiocb *req) | |
1264 | { | |
500a373d JA |
1265 | struct io_uring_task *tctx = current->io_uring; |
1266 | ||
7cdaf587 XW |
1267 | if (req->flags & REQ_F_WORK_INITIALIZED) |
1268 | return; | |
1269 | ||
ec99ca6c | 1270 | __io_req_init_async(req); |
500a373d JA |
1271 | |
1272 | /* Grab a ref if this isn't our static identity */ | |
1273 | req->work.identity = tctx->identity; | |
1274 | if (tctx->identity != &tctx->__identity) | |
1275 | refcount_inc(&req->work.identity->count); | |
7cdaf587 XW |
1276 | } |
1277 | ||
2b188cc1 JA |
1278 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1279 | { | |
1280 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1281 | ||
0f158b4c | 1282 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1283 | } |
1284 | ||
8eb7e2d0 PB |
1285 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1286 | { | |
1287 | return !req->timeout.off; | |
1288 | } | |
1289 | ||
2b188cc1 JA |
1290 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1291 | { | |
1292 | struct io_ring_ctx *ctx; | |
78076bb6 | 1293 | int hash_bits; |
2b188cc1 JA |
1294 | |
1295 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1296 | if (!ctx) | |
1297 | return NULL; | |
1298 | ||
0ddf92e8 JA |
1299 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
1300 | if (!ctx->fallback_req) | |
1301 | goto err; | |
1302 | ||
78076bb6 JA |
1303 | /* |
1304 | * Use 5 bits less than the max cq entries, that should give us around | |
1305 | * 32 entries per hash list if totally full and uniformly spread. | |
1306 | */ | |
1307 | hash_bits = ilog2(p->cq_entries); | |
1308 | hash_bits -= 5; | |
1309 | if (hash_bits <= 0) | |
1310 | hash_bits = 1; | |
1311 | ctx->cancel_hash_bits = hash_bits; | |
1312 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1313 | GFP_KERNEL); | |
1314 | if (!ctx->cancel_hash) | |
1315 | goto err; | |
1316 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1317 | ||
21482896 | 1318 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1319 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1320 | goto err; | |
2b188cc1 JA |
1321 | |
1322 | ctx->flags = p->flags; | |
90554200 | 1323 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1324 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1325 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1326 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1327 | init_completion(&ctx->ref_comp); |
1328 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1329 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1330 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1331 | mutex_init(&ctx->uring_lock); |
1332 | init_waitqueue_head(&ctx->wait); | |
1333 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1334 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1335 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1336 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1337 | spin_lock_init(&ctx->inflight_lock); |
1338 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1339 | spin_lock_init(&ctx->rsrc_ref_lock); |
1340 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1341 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1342 | init_llist_head(&ctx->rsrc_put_llist); | |
2b188cc1 | 1343 | return ctx; |
206aefde | 1344 | err: |
0ddf92e8 JA |
1345 | if (ctx->fallback_req) |
1346 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
78076bb6 | 1347 | kfree(ctx->cancel_hash); |
206aefde JA |
1348 | kfree(ctx); |
1349 | return NULL; | |
2b188cc1 JA |
1350 | } |
1351 | ||
9cf7c104 | 1352 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1353 | { |
2bc9930e JA |
1354 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1355 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1356 | |
9cf7c104 | 1357 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1358 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1359 | } |
de0617e4 | 1360 | |
9d858b21 | 1361 | return false; |
de0617e4 JA |
1362 | } |
1363 | ||
5c3462cf | 1364 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
1e6fa521 | 1365 | { |
500a373d | 1366 | if (req->work.identity == &tctx->__identity) |
1e6fa521 JA |
1367 | return; |
1368 | if (refcount_dec_and_test(&req->work.identity->count)) | |
1369 | kfree(req->work.identity); | |
1370 | } | |
1371 | ||
4edf20f9 | 1372 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1373 | { |
7cdaf587 | 1374 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1375 | return; |
51a4cc11 | 1376 | |
e86d0047 | 1377 | if (req->work.flags & IO_WQ_WORK_MM) |
98447d65 | 1378 | mmdrop(req->work.identity->mm); |
91d8f519 | 1379 | #ifdef CONFIG_BLK_CGROUP |
e86d0047 | 1380 | if (req->work.flags & IO_WQ_WORK_BLKCG) |
98447d65 | 1381 | css_put(req->work.identity->blkcg_css); |
91d8f519 | 1382 | #endif |
e86d0047 | 1383 | if (req->work.flags & IO_WQ_WORK_CREDS) |
98447d65 | 1384 | put_cred(req->work.identity->creds); |
dfead8a8 | 1385 | if (req->work.flags & IO_WQ_WORK_FS) { |
98447d65 | 1386 | struct fs_struct *fs = req->work.identity->fs; |
51a4cc11 | 1387 | |
98447d65 | 1388 | spin_lock(&req->work.identity->fs->lock); |
ff002b30 JA |
1389 | if (--fs->users) |
1390 | fs = NULL; | |
98447d65 | 1391 | spin_unlock(&req->work.identity->fs->lock); |
ff002b30 JA |
1392 | if (fs) |
1393 | free_fs_struct(fs); | |
1394 | } | |
34e08fed PB |
1395 | if (req->work.flags & IO_WQ_WORK_FILES) { |
1396 | put_files_struct(req->work.identity->files); | |
1397 | put_nsproxy(req->work.identity->nsproxy); | |
34e08fed PB |
1398 | } |
1399 | if (req->flags & REQ_F_INFLIGHT) { | |
1400 | struct io_ring_ctx *ctx = req->ctx; | |
1401 | struct io_uring_task *tctx = req->task->io_uring; | |
1402 | unsigned long flags; | |
1403 | ||
1404 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1405 | list_del(&req->inflight_entry); | |
1406 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1407 | req->flags &= ~REQ_F_INFLIGHT; | |
1408 | if (atomic_read(&tctx->in_idle)) | |
1409 | wake_up(&tctx->wait); | |
1410 | } | |
51a4cc11 | 1411 | |
e86d0047 PB |
1412 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
1413 | req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS | | |
1414 | IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES); | |
5c3462cf | 1415 | io_put_identity(req->task->io_uring, req); |
561fb04a JA |
1416 | } |
1417 | ||
1e6fa521 JA |
1418 | /* |
1419 | * Create a private copy of io_identity, since some fields don't match | |
1420 | * the current context. | |
1421 | */ | |
1422 | static bool io_identity_cow(struct io_kiocb *req) | |
1423 | { | |
5c3462cf | 1424 | struct io_uring_task *tctx = current->io_uring; |
1e6fa521 JA |
1425 | const struct cred *creds = NULL; |
1426 | struct io_identity *id; | |
1427 | ||
1428 | if (req->work.flags & IO_WQ_WORK_CREDS) | |
1429 | creds = req->work.identity->creds; | |
1430 | ||
1431 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); | |
1432 | if (unlikely(!id)) { | |
1433 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1434 | return false; | |
1435 | } | |
1436 | ||
1437 | /* | |
1438 | * We can safely just re-init the creds we copied Either the field | |
1439 | * matches the current one, or we haven't grabbed it yet. The only | |
1440 | * exception is ->creds, through registered personalities, so handle | |
1441 | * that one separately. | |
1442 | */ | |
1443 | io_init_identity(id); | |
1444 | if (creds) | |
e8c954df | 1445 | id->creds = creds; |
1e6fa521 JA |
1446 | |
1447 | /* add one for this request */ | |
1448 | refcount_inc(&id->count); | |
1449 | ||
cb8a8ae3 JA |
1450 | /* drop tctx and req identity references, if needed */ |
1451 | if (tctx->identity != &tctx->__identity && | |
1452 | refcount_dec_and_test(&tctx->identity->count)) | |
1453 | kfree(tctx->identity); | |
1454 | if (req->work.identity != &tctx->__identity && | |
1455 | refcount_dec_and_test(&req->work.identity->count)) | |
1e6fa521 JA |
1456 | kfree(req->work.identity); |
1457 | ||
1458 | req->work.identity = id; | |
500a373d | 1459 | tctx->identity = id; |
1e6fa521 JA |
1460 | return true; |
1461 | } | |
1462 | ||
1463 | static bool io_grab_identity(struct io_kiocb *req) | |
18d9be1a | 1464 | { |
d3656344 | 1465 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
5c3462cf | 1466 | struct io_identity *id = req->work.identity; |
23329513 | 1467 | struct io_ring_ctx *ctx = req->ctx; |
54a91f3b | 1468 | |
69228338 JA |
1469 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
1470 | if (id->fsize != rlimit(RLIMIT_FSIZE)) | |
1471 | return false; | |
1472 | req->work.flags |= IO_WQ_WORK_FSIZE; | |
1473 | } | |
91d8f519 | 1474 | #ifdef CONFIG_BLK_CGROUP |
dfead8a8 JA |
1475 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
1476 | (def->work_flags & IO_WQ_WORK_BLKCG)) { | |
91d8f519 | 1477 | rcu_read_lock(); |
1e6fa521 JA |
1478 | if (id->blkcg_css != blkcg_css()) { |
1479 | rcu_read_unlock(); | |
1480 | return false; | |
1481 | } | |
91d8f519 DZ |
1482 | /* |
1483 | * This should be rare, either the cgroup is dying or the task | |
1484 | * is moving cgroups. Just punt to root for the handful of ios. | |
1485 | */ | |
1e6fa521 | 1486 | if (css_tryget_online(id->blkcg_css)) |
dfead8a8 | 1487 | req->work.flags |= IO_WQ_WORK_BLKCG; |
91d8f519 DZ |
1488 | rcu_read_unlock(); |
1489 | } | |
1490 | #endif | |
dfead8a8 | 1491 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
1e6fa521 JA |
1492 | if (id->creds != current_cred()) |
1493 | return false; | |
1494 | get_cred(id->creds); | |
dfead8a8 JA |
1495 | req->work.flags |= IO_WQ_WORK_CREDS; |
1496 | } | |
4ea33a97 JA |
1497 | #ifdef CONFIG_AUDIT |
1498 | if (!uid_eq(current->loginuid, id->loginuid) || | |
1499 | current->sessionid != id->sessionid) | |
1500 | return false; | |
1501 | #endif | |
dfead8a8 JA |
1502 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
1503 | (def->work_flags & IO_WQ_WORK_FS)) { | |
1e6fa521 JA |
1504 | if (current->fs != id->fs) |
1505 | return false; | |
1506 | spin_lock(&id->fs->lock); | |
1507 | if (!id->fs->in_exec) { | |
1508 | id->fs->users++; | |
dfead8a8 | 1509 | req->work.flags |= IO_WQ_WORK_FS; |
dca9cf8b PB |
1510 | } else { |
1511 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1512 | } | |
1513 | spin_unlock(¤t->fs->lock); | |
1514 | } | |
af604703 PB |
1515 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
1516 | (def->work_flags & IO_WQ_WORK_FILES) && | |
1517 | !(req->flags & REQ_F_NO_FILE_TABLE)) { | |
1518 | if (id->files != current->files || | |
1519 | id->nsproxy != current->nsproxy) | |
1520 | return false; | |
1521 | atomic_inc(&id->files->count); | |
1522 | get_nsproxy(id->nsproxy); | |
af604703 | 1523 | |
02a13674 JA |
1524 | if (!(req->flags & REQ_F_INFLIGHT)) { |
1525 | req->flags |= REQ_F_INFLIGHT; | |
1526 | ||
1527 | spin_lock_irq(&ctx->inflight_lock); | |
1528 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1529 | spin_unlock_irq(&ctx->inflight_lock); | |
1530 | } | |
af604703 PB |
1531 | req->work.flags |= IO_WQ_WORK_FILES; |
1532 | } | |
77788775 JA |
1533 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
1534 | (def->work_flags & IO_WQ_WORK_MM)) { | |
1535 | if (id->mm != current->mm) | |
1536 | return false; | |
1537 | mmgrab(id->mm); | |
1538 | req->work.flags |= IO_WQ_WORK_MM; | |
1539 | } | |
1e6fa521 JA |
1540 | |
1541 | return true; | |
1542 | } | |
1543 | ||
1544 | static void io_prep_async_work(struct io_kiocb *req) | |
1545 | { | |
1546 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1547 | struct io_ring_ctx *ctx = req->ctx; |
1548 | ||
1549 | io_req_init_async(req); | |
1550 | ||
feaadc4f PB |
1551 | if (req->flags & REQ_F_FORCE_ASYNC) |
1552 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1553 | ||
1e6fa521 JA |
1554 | if (req->flags & REQ_F_ISREG) { |
1555 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1556 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1557 | } else { | |
1558 | if (def->unbound_nonreg_file) | |
1559 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1560 | } | |
1561 | ||
1e6fa521 JA |
1562 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
1563 | if (io_grab_identity(req)) | |
1564 | return; | |
1565 | ||
1566 | if (!io_identity_cow(req)) | |
1567 | return; | |
1568 | ||
1569 | /* can't fail at this point */ | |
1570 | if (!io_grab_identity(req)) | |
1571 | WARN_ON(1); | |
561fb04a | 1572 | } |
cccf0ee8 | 1573 | |
cbdcb435 | 1574 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1575 | { |
cbdcb435 | 1576 | struct io_kiocb *cur; |
54a91f3b | 1577 | |
f2f87370 PB |
1578 | io_for_each_link(cur, req) |
1579 | io_prep_async_work(cur); | |
561fb04a JA |
1580 | } |
1581 | ||
7271ef3a | 1582 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1583 | { |
a197f664 | 1584 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1585 | struct io_kiocb *link = io_prep_linked_timeout(req); |
561fb04a | 1586 | |
8766dd51 PB |
1587 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1588 | &req->work, req->flags); | |
1589 | io_wq_enqueue(ctx->io_wq, &req->work); | |
7271ef3a | 1590 | return link; |
18d9be1a JA |
1591 | } |
1592 | ||
cbdcb435 PB |
1593 | static void io_queue_async_work(struct io_kiocb *req) |
1594 | { | |
7271ef3a JA |
1595 | struct io_kiocb *link; |
1596 | ||
cbdcb435 PB |
1597 | /* init ->work of the whole link before punting */ |
1598 | io_prep_async_link(req); | |
7271ef3a JA |
1599 | link = __io_queue_async_work(req); |
1600 | ||
1601 | if (link) | |
1602 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1603 | } |
1604 | ||
5262f567 JA |
1605 | static void io_kill_timeout(struct io_kiocb *req) |
1606 | { | |
e8c2bc1f | 1607 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1608 | int ret; |
1609 | ||
e8c2bc1f | 1610 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1611 | if (ret != -1) { |
01cec8c1 PB |
1612 | atomic_set(&req->ctx->cq_timeouts, |
1613 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1614 | list_del_init(&req->timeout.list); |
78e19bbe | 1615 | io_cqring_fill_event(req, 0); |
216578e5 | 1616 | io_put_req_deferred(req, 1); |
5262f567 JA |
1617 | } |
1618 | } | |
1619 | ||
76e1b642 JA |
1620 | /* |
1621 | * Returns true if we found and killed one or more timeouts | |
1622 | */ | |
6b81928d PB |
1623 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1624 | struct files_struct *files) | |
5262f567 JA |
1625 | { |
1626 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1627 | int canceled = 0; |
5262f567 JA |
1628 | |
1629 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1630 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1631 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1632 | io_kill_timeout(req); |
76e1b642 JA |
1633 | canceled++; |
1634 | } | |
f3606e3a | 1635 | } |
5262f567 | 1636 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1637 | return canceled != 0; |
5262f567 JA |
1638 | } |
1639 | ||
04518945 | 1640 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1641 | { |
04518945 | 1642 | do { |
27dc8338 PB |
1643 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1644 | struct io_defer_entry, list); | |
de0617e4 | 1645 | |
9cf7c104 | 1646 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1647 | break; |
27dc8338 | 1648 | list_del_init(&de->list); |
907d1df3 | 1649 | io_req_task_queue(de->req); |
27dc8338 | 1650 | kfree(de); |
04518945 PB |
1651 | } while (!list_empty(&ctx->defer_list)); |
1652 | } | |
1653 | ||
360428f8 | 1654 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1655 | { |
f010505b MDG |
1656 | u32 seq; |
1657 | ||
1658 | if (list_empty(&ctx->timeout_list)) | |
1659 | return; | |
1660 | ||
1661 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1662 | ||
1663 | do { | |
1664 | u32 events_needed, events_got; | |
360428f8 | 1665 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1666 | struct io_kiocb, timeout.list); |
de0617e4 | 1667 | |
8eb7e2d0 | 1668 | if (io_is_timeout_noseq(req)) |
360428f8 | 1669 | break; |
f010505b MDG |
1670 | |
1671 | /* | |
1672 | * Since seq can easily wrap around over time, subtract | |
1673 | * the last seq at which timeouts were flushed before comparing. | |
1674 | * Assuming not more than 2^31-1 events have happened since, | |
1675 | * these subtractions won't have wrapped, so we can check if | |
1676 | * target is in [last_seq, current_seq] by comparing the two. | |
1677 | */ | |
1678 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1679 | events_got = seq - ctx->cq_last_tm_flush; | |
1680 | if (events_got < events_needed) | |
360428f8 | 1681 | break; |
bfe68a22 | 1682 | |
135fcde8 | 1683 | list_del_init(&req->timeout.list); |
5262f567 | 1684 | io_kill_timeout(req); |
f010505b MDG |
1685 | } while (!list_empty(&ctx->timeout_list)); |
1686 | ||
1687 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1688 | } |
5262f567 | 1689 | |
360428f8 PB |
1690 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1691 | { | |
1692 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1693 | |
1694 | /* order cqe stores with ring update */ | |
1695 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1696 | |
04518945 PB |
1697 | if (unlikely(!list_empty(&ctx->defer_list))) |
1698 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1699 | } |
1700 | ||
90554200 JA |
1701 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1702 | { | |
1703 | struct io_rings *r = ctx->rings; | |
1704 | ||
1705 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1706 | } | |
1707 | ||
888aae2e PB |
1708 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1709 | { | |
1710 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1711 | } | |
1712 | ||
2b188cc1 JA |
1713 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1714 | { | |
75b28aff | 1715 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1716 | unsigned tail; |
1717 | ||
115e12e5 SB |
1718 | /* |
1719 | * writes to the cq entry need to come after reading head; the | |
1720 | * control dependency is enough as we're using WRITE_ONCE to | |
1721 | * fill the cq entry | |
1722 | */ | |
888aae2e | 1723 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1724 | return NULL; |
1725 | ||
888aae2e | 1726 | tail = ctx->cached_cq_tail++; |
75b28aff | 1727 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1728 | } |
1729 | ||
f2842ab5 JA |
1730 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1731 | { | |
f0b493e6 JA |
1732 | if (!ctx->cq_ev_fd) |
1733 | return false; | |
7e55a19c SG |
1734 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1735 | return false; | |
f2842ab5 JA |
1736 | if (!ctx->eventfd_async) |
1737 | return true; | |
b41e9852 | 1738 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1739 | } |
1740 | ||
b41e9852 | 1741 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1742 | { |
b1445e59 PB |
1743 | /* see waitqueue_active() comment */ |
1744 | smp_mb(); | |
1745 | ||
1d7bb1d5 JA |
1746 | if (waitqueue_active(&ctx->wait)) |
1747 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1748 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1749 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1750 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1751 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1752 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1753 | wake_up_interruptible(&ctx->cq_wait); |
1754 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1755 | } | |
1d7bb1d5 JA |
1756 | } |
1757 | ||
80c18e4a PB |
1758 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1759 | { | |
b1445e59 PB |
1760 | /* see waitqueue_active() comment */ |
1761 | smp_mb(); | |
1762 | ||
80c18e4a PB |
1763 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1764 | if (waitqueue_active(&ctx->wait)) | |
1765 | wake_up(&ctx->wait); | |
1766 | } | |
1767 | if (io_should_trigger_evfd(ctx)) | |
1768 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1769 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1770 | wake_up_interruptible(&ctx->cq_wait); |
1771 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1772 | } | |
80c18e4a PB |
1773 | } |
1774 | ||
c4a2ed72 | 1775 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1776 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1777 | struct task_struct *tsk, | |
1778 | struct files_struct *files) | |
1d7bb1d5 JA |
1779 | { |
1780 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1781 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1782 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1783 | unsigned long flags; |
b18032bb | 1784 | bool all_flushed, posted; |
1d7bb1d5 JA |
1785 | LIST_HEAD(list); |
1786 | ||
e23de15f PB |
1787 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1788 | return false; | |
1d7bb1d5 | 1789 | |
b18032bb | 1790 | posted = false; |
1d7bb1d5 | 1791 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1792 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1793 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1794 | continue; |
1795 | ||
1d7bb1d5 JA |
1796 | cqe = io_get_cqring(ctx); |
1797 | if (!cqe && !force) | |
1798 | break; | |
1799 | ||
40d8ddd4 | 1800 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1801 | if (cqe) { |
1802 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1803 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1804 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1805 | } else { |
2c3bac6d | 1806 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1807 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1808 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1809 | } |
b18032bb | 1810 | posted = true; |
1d7bb1d5 JA |
1811 | } |
1812 | ||
09e88404 PB |
1813 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1814 | if (all_flushed) { | |
1815 | clear_bit(0, &ctx->sq_check_overflow); | |
1816 | clear_bit(0, &ctx->cq_check_overflow); | |
1817 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1818 | } | |
46930143 | 1819 | |
b18032bb JA |
1820 | if (posted) |
1821 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1822 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1823 | if (posted) |
1824 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1825 | |
1826 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1827 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1828 | list_del(&req->compl.list); | |
ec9c02ad | 1829 | io_put_req(req); |
1d7bb1d5 | 1830 | } |
c4a2ed72 | 1831 | |
09e88404 | 1832 | return all_flushed; |
1d7bb1d5 JA |
1833 | } |
1834 | ||
6c503150 PB |
1835 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1836 | struct task_struct *tsk, | |
1837 | struct files_struct *files) | |
1838 | { | |
1839 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1840 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1841 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1842 | mutex_lock(&ctx->uring_lock); | |
1843 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1844 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1845 | mutex_unlock(&ctx->uring_lock); | |
1846 | } | |
1847 | } | |
1848 | ||
bcda7baa | 1849 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1850 | { |
78e19bbe | 1851 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1852 | struct io_uring_cqe *cqe; |
1853 | ||
78e19bbe | 1854 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1855 | |
2b188cc1 JA |
1856 | /* |
1857 | * If we can't get a cq entry, userspace overflowed the | |
1858 | * submission (by quite a lot). Increment the overflow count in | |
1859 | * the ring. | |
1860 | */ | |
1861 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1862 | if (likely(cqe)) { |
78e19bbe | 1863 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1864 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1865 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1866 | } else if (ctx->cq_overflow_flushed || |
1867 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1868 | /* |
1869 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1870 | * then we cannot store the request for later flushing, we need | |
1871 | * to drop it on the floor. | |
1872 | */ | |
2c3bac6d PB |
1873 | ctx->cached_cq_overflow++; |
1874 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1875 | } else { |
ad3eb2c8 JA |
1876 | if (list_empty(&ctx->cq_overflow_list)) { |
1877 | set_bit(0, &ctx->sq_check_overflow); | |
1878 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1879 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1880 | } |
40d8ddd4 | 1881 | io_clean_op(req); |
1d7bb1d5 | 1882 | req->result = res; |
0f7e466b | 1883 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1884 | refcount_inc(&req->refs); |
1885 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1886 | } |
1887 | } | |
1888 | ||
bcda7baa JA |
1889 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1890 | { | |
1891 | __io_cqring_fill_event(req, res, 0); | |
1892 | } | |
1893 | ||
9ae1f8dd PB |
1894 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1895 | unsigned int cflags) | |
2b188cc1 | 1896 | { |
78e19bbe | 1897 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1898 | unsigned long flags; |
1899 | ||
1900 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1901 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 JA |
1902 | io_commit_cqring(ctx); |
1903 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1904 | ||
8c838788 | 1905 | io_cqring_ev_posted(ctx); |
9ae1f8dd PB |
1906 | } |
1907 | ||
1908 | static inline void io_req_complete_nostate(struct io_kiocb *req, long res, | |
1909 | unsigned int cflags) | |
1910 | { | |
1911 | io_req_complete_post(req, res, cflags); | |
a38d68db | 1912 | io_put_req(req); |
2b188cc1 JA |
1913 | } |
1914 | ||
229a7b63 | 1915 | static void io_submit_flush_completions(struct io_comp_state *cs) |
bcda7baa | 1916 | { |
229a7b63 JA |
1917 | struct io_ring_ctx *ctx = cs->ctx; |
1918 | ||
1919 | spin_lock_irq(&ctx->completion_lock); | |
1920 | while (!list_empty(&cs->list)) { | |
1921 | struct io_kiocb *req; | |
1922 | ||
3ca405eb PB |
1923 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
1924 | list_del(&req->compl.list); | |
0f7e466b | 1925 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
216578e5 PB |
1926 | |
1927 | /* | |
1928 | * io_free_req() doesn't care about completion_lock unless one | |
1929 | * of these flags is set. REQ_F_WORK_INITIALIZED is in the list | |
1930 | * because of a potential deadlock with req->work.fs->lock | |
e342c807 | 1931 | * We defer both, completion and submission refs. |
216578e5 PB |
1932 | */ |
1933 | if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT | |
1934 | |REQ_F_WORK_INITIALIZED)) { | |
229a7b63 | 1935 | spin_unlock_irq(&ctx->completion_lock); |
e342c807 | 1936 | io_double_put_req(req); |
229a7b63 | 1937 | spin_lock_irq(&ctx->completion_lock); |
216578e5 | 1938 | } else { |
e342c807 | 1939 | io_double_put_req(req); |
229a7b63 JA |
1940 | } |
1941 | } | |
1942 | io_commit_cqring(ctx); | |
1943 | spin_unlock_irq(&ctx->completion_lock); | |
1944 | ||
1945 | io_cqring_ev_posted(ctx); | |
1946 | cs->nr = 0; | |
1947 | } | |
1948 | ||
a38d68db PB |
1949 | static void io_req_complete_state(struct io_kiocb *req, long res, |
1950 | unsigned int cflags, struct io_comp_state *cs) | |
229a7b63 | 1951 | { |
a38d68db PB |
1952 | io_clean_op(req); |
1953 | req->result = res; | |
1954 | req->compl.cflags = cflags; | |
e342c807 | 1955 | req->flags |= REQ_F_COMPLETE_INLINE; |
a38d68db PB |
1956 | } |
1957 | ||
1958 | static inline void __io_req_complete(struct io_kiocb *req, long res, | |
1959 | unsigned cflags, struct io_comp_state *cs) | |
1960 | { | |
1961 | if (!cs) | |
1962 | io_req_complete_nostate(req, res, cflags); | |
1963 | else | |
1964 | io_req_complete_state(req, res, cflags, cs); | |
e1e16097 JA |
1965 | } |
1966 | ||
a38d68db | 1967 | static inline void io_req_complete(struct io_kiocb *req, long res) |
bcda7baa | 1968 | { |
229a7b63 | 1969 | __io_req_complete(req, res, 0, NULL); |
bcda7baa JA |
1970 | } |
1971 | ||
0ddf92e8 JA |
1972 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1973 | { | |
1974 | return req == (struct io_kiocb *) | |
1975 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1976 | } | |
1977 | ||
1978 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1979 | { | |
1980 | struct io_kiocb *req; | |
1981 | ||
1982 | req = ctx->fallback_req; | |
dd461af6 | 1983 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
0ddf92e8 JA |
1984 | return req; |
1985 | ||
1986 | return NULL; | |
1987 | } | |
1988 | ||
0553b8bd PB |
1989 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
1990 | struct io_submit_state *state) | |
2b188cc1 | 1991 | { |
f6b6c7d6 | 1992 | if (!state->free_reqs) { |
291b2821 | 1993 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1994 | size_t sz; |
1995 | int ret; | |
1996 | ||
1997 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1998 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1999 | ||
2000 | /* | |
2001 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2002 | * retry single alloc to be on the safe side. | |
2003 | */ | |
2004 | if (unlikely(ret <= 0)) { | |
2005 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
2006 | if (!state->reqs[0]) | |
85bcb6c6 | 2007 | return io_get_fallback_req(ctx); |
fd6fab2c JA |
2008 | ret = 1; |
2009 | } | |
291b2821 | 2010 | state->free_reqs = ret; |
2b188cc1 JA |
2011 | } |
2012 | ||
291b2821 PB |
2013 | state->free_reqs--; |
2014 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
2015 | } |
2016 | ||
8da11c19 PB |
2017 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
2018 | bool fixed) | |
2019 | { | |
36f72fe2 | 2020 | if (!fixed) |
8da11c19 PB |
2021 | fput(file); |
2022 | } | |
2023 | ||
4edf20f9 | 2024 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2025 | { |
3ca405eb | 2026 | io_clean_op(req); |
929a3af9 | 2027 | |
e8c2bc1f JA |
2028 | if (req->async_data) |
2029 | kfree(req->async_data); | |
8da11c19 PB |
2030 | if (req->file) |
2031 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
2032 | if (req->fixed_rsrc_refs) |
2033 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 2034 | io_req_clean_work(req); |
e65ef56d JA |
2035 | } |
2036 | ||
7c660731 PB |
2037 | static inline void io_put_task(struct task_struct *task, int nr) |
2038 | { | |
2039 | struct io_uring_task *tctx = task->io_uring; | |
2040 | ||
2041 | percpu_counter_sub(&tctx->inflight, nr); | |
2042 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2043 | wake_up(&tctx->wait); | |
2044 | put_task_struct_many(task, nr); | |
2045 | } | |
2046 | ||
216578e5 | 2047 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2048 | { |
51a4cc11 | 2049 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2050 | |
216578e5 | 2051 | io_dismantle_req(req); |
7c660731 | 2052 | io_put_task(req->task, 1); |
e3bc8e9d | 2053 | |
b1e50e54 PB |
2054 | if (likely(!io_is_fallback_req(req))) |
2055 | kmem_cache_free(req_cachep, req); | |
2056 | else | |
ecfc5177 PB |
2057 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
2058 | percpu_ref_put(&ctx->refs); | |
e65ef56d JA |
2059 | } |
2060 | ||
f2f87370 PB |
2061 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2062 | { | |
2063 | struct io_kiocb *nxt = req->link; | |
2064 | ||
2065 | req->link = nxt->link; | |
2066 | nxt->link = NULL; | |
2067 | } | |
2068 | ||
c9abd7ad | 2069 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 2070 | { |
a197f664 | 2071 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 2072 | struct io_kiocb *link; |
c9abd7ad PB |
2073 | bool cancelled = false; |
2074 | unsigned long flags; | |
7c86ffee | 2075 | |
c9abd7ad | 2076 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2077 | link = req->link; |
2078 | ||
900fad45 PB |
2079 | /* |
2080 | * Can happen if a linked timeout fired and link had been like | |
2081 | * req -> link t-out -> link t-out [-> ...] | |
2082 | */ | |
c9abd7ad PB |
2083 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
2084 | struct io_timeout_data *io = link->async_data; | |
2085 | int ret; | |
7c86ffee | 2086 | |
f2f87370 | 2087 | io_remove_next_linked(req); |
90cd7e42 | 2088 | link->timeout.head = NULL; |
c9abd7ad PB |
2089 | ret = hrtimer_try_to_cancel(&io->timer); |
2090 | if (ret != -1) { | |
2091 | io_cqring_fill_event(link, -ECANCELED); | |
2092 | io_commit_cqring(ctx); | |
2093 | cancelled = true; | |
2094 | } | |
2095 | } | |
7c86ffee | 2096 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 2097 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 2098 | |
c9abd7ad | 2099 | if (cancelled) { |
7c86ffee | 2100 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
2101 | io_put_req(link); |
2102 | } | |
7c86ffee PB |
2103 | } |
2104 | ||
9e645e11 | 2105 | |
d148ca4b | 2106 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 2107 | { |
f2f87370 | 2108 | struct io_kiocb *link, *nxt; |
2665abfd | 2109 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 2110 | unsigned long flags; |
9e645e11 | 2111 | |
d148ca4b | 2112 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
2113 | link = req->link; |
2114 | req->link = NULL; | |
9e645e11 | 2115 | |
f2f87370 PB |
2116 | while (link) { |
2117 | nxt = link->link; | |
2118 | link->link = NULL; | |
2665abfd | 2119 | |
f2f87370 | 2120 | trace_io_uring_fail_link(req, link); |
7c86ffee | 2121 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
2122 | |
2123 | /* | |
2124 | * It's ok to free under spinlock as they're not linked anymore, | |
2125 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
2126 | * work.fs->lock. | |
2127 | */ | |
2128 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
2129 | io_put_req_deferred(link, 2); | |
2130 | else | |
2131 | io_double_put_req(link); | |
f2f87370 | 2132 | link = nxt; |
9e645e11 | 2133 | } |
2665abfd | 2134 | io_commit_cqring(ctx); |
216578e5 | 2135 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 2136 | |
2665abfd | 2137 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
2138 | } |
2139 | ||
3fa5e0f3 | 2140 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2141 | { |
7c86ffee PB |
2142 | if (req->flags & REQ_F_LINK_TIMEOUT) |
2143 | io_kill_linked_timeout(req); | |
944e58bf | 2144 | |
9e645e11 JA |
2145 | /* |
2146 | * If LINK is set, we have dependent requests in this chain. If we | |
2147 | * didn't fail this request, queue the first one up, moving any other | |
2148 | * dependencies to the next request. In case of failure, fail the rest | |
2149 | * of the chain. | |
2150 | */ | |
f2f87370 PB |
2151 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
2152 | struct io_kiocb *nxt = req->link; | |
2153 | ||
2154 | req->link = NULL; | |
2155 | return nxt; | |
2156 | } | |
9b5f7bd9 PB |
2157 | io_fail_links(req); |
2158 | return NULL; | |
4d7dd462 | 2159 | } |
9e645e11 | 2160 | |
f2f87370 | 2161 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2162 | { |
f2f87370 | 2163 | if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT))) |
3fa5e0f3 PB |
2164 | return NULL; |
2165 | return __io_req_find_next(req); | |
2166 | } | |
2167 | ||
355fb9e2 | 2168 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
2169 | { |
2170 | struct task_struct *tsk = req->task; | |
2171 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
2172 | enum task_work_notify_mode notify; |
2173 | int ret; | |
c2c4c83c | 2174 | |
6200b0ae JA |
2175 | if (tsk->flags & PF_EXITING) |
2176 | return -ESRCH; | |
2177 | ||
c2c4c83c | 2178 | /* |
0ba9c9ed JA |
2179 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
2180 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2181 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2182 | * will do the job. | |
c2c4c83c | 2183 | */ |
91989c70 | 2184 | notify = TWA_NONE; |
355fb9e2 | 2185 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
2186 | notify = TWA_SIGNAL; |
2187 | ||
87c4311f | 2188 | ret = task_work_add(tsk, &req->task_work, notify); |
c2c4c83c JA |
2189 | if (!ret) |
2190 | wake_up_process(tsk); | |
0ba9c9ed | 2191 | |
c2c4c83c JA |
2192 | return ret; |
2193 | } | |
2194 | ||
eab30c4d PB |
2195 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
2196 | void (*cb)(struct callback_head *)) | |
2197 | { | |
2198 | struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq); | |
2199 | ||
2200 | init_task_work(&req->task_work, cb); | |
2201 | task_work_add(tsk, &req->task_work, TWA_NONE); | |
2202 | wake_up_process(tsk); | |
2203 | } | |
2204 | ||
c40f6379 JA |
2205 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
2206 | { | |
2207 | struct io_ring_ctx *ctx = req->ctx; | |
2208 | ||
2209 | spin_lock_irq(&ctx->completion_lock); | |
2210 | io_cqring_fill_event(req, error); | |
2211 | io_commit_cqring(ctx); | |
2212 | spin_unlock_irq(&ctx->completion_lock); | |
2213 | ||
2214 | io_cqring_ev_posted(ctx); | |
2215 | req_set_fail_links(req); | |
2216 | io_double_put_req(req); | |
2217 | } | |
2218 | ||
2219 | static void io_req_task_cancel(struct callback_head *cb) | |
2220 | { | |
2221 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2222 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2223 | |
2224 | __io_req_task_cancel(req, -ECANCELED); | |
87ceb6a6 | 2225 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2226 | } |
2227 | ||
2228 | static void __io_req_task_submit(struct io_kiocb *req) | |
2229 | { | |
2230 | struct io_ring_ctx *ctx = req->ctx; | |
2231 | ||
81b6d05c | 2232 | mutex_lock(&ctx->uring_lock); |
d9d05217 PB |
2233 | if (!ctx->sqo_dead && |
2234 | !__io_sq_thread_acquire_mm(ctx) && | |
2235 | !__io_sq_thread_acquire_files(ctx)) | |
c1379e24 | 2236 | __io_queue_sqe(req, NULL); |
81b6d05c | 2237 | else |
c40f6379 | 2238 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2239 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2240 | } |
2241 | ||
2242 | static void io_req_task_submit(struct callback_head *cb) | |
2243 | { | |
2244 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
6d816e08 | 2245 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 JA |
2246 | |
2247 | __io_req_task_submit(req); | |
6d816e08 | 2248 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2249 | } |
2250 | ||
2251 | static void io_req_task_queue(struct io_kiocb *req) | |
2252 | { | |
c40f6379 JA |
2253 | int ret; |
2254 | ||
2255 | init_task_work(&req->task_work, io_req_task_submit); | |
6d816e08 | 2256 | percpu_ref_get(&req->ctx->refs); |
c40f6379 | 2257 | |
355fb9e2 | 2258 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2259 | if (unlikely(ret)) |
2260 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
c40f6379 JA |
2261 | } |
2262 | ||
f2f87370 | 2263 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2264 | { |
9b5f7bd9 | 2265 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2266 | |
2267 | if (nxt) | |
906a8c3f | 2268 | io_req_task_queue(nxt); |
c69f8dbe JL |
2269 | } |
2270 | ||
c3524383 | 2271 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2272 | { |
c3524383 PB |
2273 | io_queue_next(req); |
2274 | __io_free_req(req); | |
2275 | } | |
8766dd51 | 2276 | |
2d6500d4 PB |
2277 | struct req_batch { |
2278 | void *reqs[IO_IOPOLL_BATCH]; | |
2279 | int to_free; | |
7a743e22 | 2280 | |
5af1d13e PB |
2281 | struct task_struct *task; |
2282 | int task_refs; | |
2d6500d4 PB |
2283 | }; |
2284 | ||
5af1d13e PB |
2285 | static inline void io_init_req_batch(struct req_batch *rb) |
2286 | { | |
2287 | rb->to_free = 0; | |
2288 | rb->task_refs = 0; | |
2289 | rb->task = NULL; | |
2290 | } | |
2291 | ||
2d6500d4 PB |
2292 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
2293 | struct req_batch *rb) | |
2294 | { | |
2295 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
2296 | percpu_ref_put_many(&ctx->refs, rb->to_free); | |
2297 | rb->to_free = 0; | |
2298 | } | |
2299 | ||
2300 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, | |
2301 | struct req_batch *rb) | |
2302 | { | |
2303 | if (rb->to_free) | |
2304 | __io_req_free_batch_flush(ctx, rb); | |
5af1d13e | 2305 | if (rb->task) { |
7c660731 | 2306 | io_put_task(rb->task, rb->task_refs); |
5af1d13e PB |
2307 | rb->task = NULL; |
2308 | } | |
2d6500d4 PB |
2309 | } |
2310 | ||
2311 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) | |
2312 | { | |
2313 | if (unlikely(io_is_fallback_req(req))) { | |
2314 | io_free_req(req); | |
2315 | return; | |
2316 | } | |
f2f87370 | 2317 | io_queue_next(req); |
2d6500d4 | 2318 | |
e3bc8e9d | 2319 | if (req->task != rb->task) { |
7c660731 PB |
2320 | if (rb->task) |
2321 | io_put_task(rb->task, rb->task_refs); | |
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); |
eab30c4d PB |
2367 | if (unlikely(ret)) |
2368 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2369 | } |
2370 | ||
2371 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2372 | { | |
2373 | if (refcount_sub_and_test(refs, &req->refs)) | |
2374 | io_free_req_deferred(req); | |
2375 | } | |
2376 | ||
f4db7182 | 2377 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
7a743e22 | 2378 | { |
6df1db6b | 2379 | struct io_kiocb *nxt; |
f4db7182 | 2380 | |
7a743e22 | 2381 | /* |
f4db7182 PB |
2382 | * A ref is owned by io-wq in which context we're. So, if that's the |
2383 | * last one, it's safe to steal next work. False negatives are Ok, | |
2384 | * it just will be re-punted async in io_put_work() | |
7a743e22 | 2385 | */ |
f4db7182 PB |
2386 | if (refcount_read(&req->refs) != 1) |
2387 | return NULL; | |
7a743e22 | 2388 | |
9b5f7bd9 | 2389 | nxt = io_req_find_next(req); |
6df1db6b | 2390 | return nxt ? &nxt->work : NULL; |
7a743e22 PB |
2391 | } |
2392 | ||
978db57e JA |
2393 | static void io_double_put_req(struct io_kiocb *req) |
2394 | { | |
2395 | /* drop both submit and complete references */ | |
2396 | if (refcount_sub_and_test(2, &req->refs)) | |
2397 | io_free_req(req); | |
2398 | } | |
2399 | ||
6c503150 | 2400 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2401 | { |
2402 | /* See comment at the top of this file */ | |
2403 | smp_rmb(); | |
e23de15f | 2404 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2405 | } |
2406 | ||
fb5ccc98 PB |
2407 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2408 | { | |
2409 | struct io_rings *rings = ctx->rings; | |
2410 | ||
2411 | /* make sure SQ entry isn't read before tail */ | |
2412 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2413 | } | |
2414 | ||
8ff069bf | 2415 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2416 | { |
8ff069bf | 2417 | unsigned int cflags; |
e94f141b | 2418 | |
bcda7baa JA |
2419 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2420 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2421 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2422 | kfree(kbuf); |
2423 | return cflags; | |
e94f141b JA |
2424 | } |
2425 | ||
8ff069bf | 2426 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2427 | { |
4d954c25 | 2428 | struct io_buffer *kbuf; |
bcda7baa | 2429 | |
4d954c25 | 2430 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2431 | return io_put_kbuf(req, kbuf); |
2432 | } | |
2433 | ||
4c6e277c JA |
2434 | static inline bool io_run_task_work(void) |
2435 | { | |
6200b0ae JA |
2436 | /* |
2437 | * Not safe to run on exiting task, and the task_work handling will | |
2438 | * not add work to such a task. | |
2439 | */ | |
2440 | if (unlikely(current->flags & PF_EXITING)) | |
2441 | return false; | |
4c6e277c JA |
2442 | if (current->task_works) { |
2443 | __set_current_state(TASK_RUNNING); | |
2444 | task_work_run(); | |
2445 | return true; | |
2446 | } | |
2447 | ||
2448 | return false; | |
bcda7baa JA |
2449 | } |
2450 | ||
bbde017a XW |
2451 | static void io_iopoll_queue(struct list_head *again) |
2452 | { | |
2453 | struct io_kiocb *req; | |
2454 | ||
2455 | do { | |
d21ffe7e PB |
2456 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
2457 | list_del(&req->inflight_entry); | |
81b68a5c | 2458 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
bbde017a XW |
2459 | } while (!list_empty(again)); |
2460 | } | |
2461 | ||
def596e9 JA |
2462 | /* |
2463 | * Find and free completed poll iocbs | |
2464 | */ | |
2465 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2466 | struct list_head *done) | |
2467 | { | |
8237e045 | 2468 | struct req_batch rb; |
def596e9 | 2469 | struct io_kiocb *req; |
bbde017a XW |
2470 | LIST_HEAD(again); |
2471 | ||
2472 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2473 | smp_rmb(); | |
def596e9 | 2474 | |
5af1d13e | 2475 | io_init_req_batch(&rb); |
def596e9 | 2476 | while (!list_empty(done)) { |
bcda7baa JA |
2477 | int cflags = 0; |
2478 | ||
d21ffe7e | 2479 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
bbde017a | 2480 | if (READ_ONCE(req->result) == -EAGAIN) { |
56450c20 | 2481 | req->result = 0; |
bbde017a | 2482 | req->iopoll_completed = 0; |
d21ffe7e | 2483 | list_move_tail(&req->inflight_entry, &again); |
bbde017a XW |
2484 | continue; |
2485 | } | |
d21ffe7e | 2486 | list_del(&req->inflight_entry); |
def596e9 | 2487 | |
bcda7baa | 2488 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2489 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2490 | |
2491 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2492 | (*nr_events)++; |
2493 | ||
c3524383 | 2494 | if (refcount_dec_and_test(&req->refs)) |
2d6500d4 | 2495 | io_req_free_batch(&rb, req); |
def596e9 | 2496 | } |
def596e9 | 2497 | |
09bb8394 | 2498 | io_commit_cqring(ctx); |
80c18e4a | 2499 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2500 | io_req_free_batch_finish(ctx, &rb); |
581f9810 | 2501 | |
bbde017a XW |
2502 | if (!list_empty(&again)) |
2503 | io_iopoll_queue(&again); | |
581f9810 BM |
2504 | } |
2505 | ||
def596e9 JA |
2506 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2507 | long min) | |
2508 | { | |
2509 | struct io_kiocb *req, *tmp; | |
2510 | LIST_HEAD(done); | |
2511 | bool spin; | |
2512 | int ret; | |
2513 | ||
2514 | /* | |
2515 | * Only spin for completions if we don't have multiple devices hanging | |
2516 | * off our complete list, and we're under the requested amount. | |
2517 | */ | |
2518 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2519 | ||
2520 | ret = 0; | |
d21ffe7e | 2521 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2522 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2523 | |
2524 | /* | |
581f9810 BM |
2525 | * Move completed and retryable entries to our local lists. |
2526 | * If we find a request that requires polling, break out | |
2527 | * and complete those lists first, if we have entries there. | |
def596e9 | 2528 | */ |
65a6543d | 2529 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2530 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2531 | continue; |
2532 | } | |
2533 | if (!list_empty(&done)) | |
2534 | break; | |
2535 | ||
2536 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2537 | if (ret < 0) | |
2538 | break; | |
2539 | ||
3aadc23e PB |
2540 | /* iopoll may have completed current req */ |
2541 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2542 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2543 | |
def596e9 JA |
2544 | if (ret && spin) |
2545 | spin = false; | |
2546 | ret = 0; | |
2547 | } | |
2548 | ||
2549 | if (!list_empty(&done)) | |
2550 | io_iopoll_complete(ctx, nr_events, &done); | |
2551 | ||
2552 | return ret; | |
2553 | } | |
2554 | ||
2555 | /* | |
d195a66e | 2556 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2557 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2558 | * as a non-spinning completion check. | |
2559 | */ | |
2560 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2561 | long min) | |
2562 | { | |
540e32a0 | 2563 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2564 | int ret; |
2565 | ||
2566 | ret = io_do_iopoll(ctx, nr_events, min); | |
2567 | if (ret < 0) | |
2568 | return ret; | |
eba0a4dd | 2569 | if (*nr_events >= min) |
def596e9 JA |
2570 | return 0; |
2571 | } | |
2572 | ||
2573 | return 1; | |
2574 | } | |
2575 | ||
2576 | /* | |
2577 | * We can't just wait for polled events to come to us, we have to actively | |
2578 | * find and complete them. | |
2579 | */ | |
b2edc0a7 | 2580 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2581 | { |
2582 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2583 | return; | |
2584 | ||
2585 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2586 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2587 | unsigned int nr_events = 0; |
2588 | ||
b2edc0a7 | 2589 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2590 | |
b2edc0a7 PB |
2591 | /* let it sleep and repeat later if can't complete a request */ |
2592 | if (nr_events == 0) | |
2593 | break; | |
08f5439f JA |
2594 | /* |
2595 | * Ensure we allow local-to-the-cpu processing to take place, | |
2596 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2597 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2598 | */ |
3fcee5a6 PB |
2599 | if (need_resched()) { |
2600 | mutex_unlock(&ctx->uring_lock); | |
2601 | cond_resched(); | |
2602 | mutex_lock(&ctx->uring_lock); | |
2603 | } | |
def596e9 JA |
2604 | } |
2605 | mutex_unlock(&ctx->uring_lock); | |
2606 | } | |
2607 | ||
7668b92a | 2608 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2609 | { |
7668b92a | 2610 | unsigned int nr_events = 0; |
2b2ed975 | 2611 | int iters = 0, ret = 0; |
500f9fba | 2612 | |
c7849be9 XW |
2613 | /* |
2614 | * We disallow the app entering submit/complete with polling, but we | |
2615 | * still need to lock the ring to prevent racing with polled issue | |
2616 | * that got punted to a workqueue. | |
2617 | */ | |
2618 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2619 | do { |
a3a0e43f JA |
2620 | /* |
2621 | * Don't enter poll loop if we already have events pending. | |
2622 | * If we do, we can potentially be spinning for commands that | |
2623 | * already triggered a CQE (eg in error). | |
2624 | */ | |
6c503150 PB |
2625 | if (test_bit(0, &ctx->cq_check_overflow)) |
2626 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2627 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2628 | break; |
2629 | ||
500f9fba JA |
2630 | /* |
2631 | * If a submit got punted to a workqueue, we can have the | |
2632 | * application entering polling for a command before it gets | |
2633 | * issued. That app will hold the uring_lock for the duration | |
2634 | * of the poll right here, so we need to take a breather every | |
2635 | * now and then to ensure that the issue has a chance to add | |
2636 | * the poll to the issued list. Otherwise we can spin here | |
2637 | * forever, while the workqueue is stuck trying to acquire the | |
2638 | * very same mutex. | |
2639 | */ | |
2640 | if (!(++iters & 7)) { | |
2641 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2642 | io_run_task_work(); |
500f9fba JA |
2643 | mutex_lock(&ctx->uring_lock); |
2644 | } | |
2645 | ||
7668b92a | 2646 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2647 | if (ret <= 0) |
2648 | break; | |
2649 | ret = 0; | |
7668b92a | 2650 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2651 | |
500f9fba | 2652 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2653 | return ret; |
2654 | } | |
2655 | ||
491381ce | 2656 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2657 | { |
491381ce JA |
2658 | /* |
2659 | * Tell lockdep we inherited freeze protection from submission | |
2660 | * thread. | |
2661 | */ | |
2662 | if (req->flags & REQ_F_ISREG) { | |
2663 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2664 | |
491381ce | 2665 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2666 | } |
491381ce | 2667 | file_end_write(req->file); |
2b188cc1 JA |
2668 | } |
2669 | ||
a1d7c393 JA |
2670 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
2671 | struct io_comp_state *cs) | |
2b188cc1 | 2672 | { |
9adbd45d | 2673 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
bcda7baa | 2674 | int cflags = 0; |
2b188cc1 | 2675 | |
491381ce JA |
2676 | if (kiocb->ki_flags & IOCB_WRITE) |
2677 | kiocb_end_write(req); | |
2b188cc1 | 2678 | |
4e88d6e7 JA |
2679 | if (res != req->result) |
2680 | req_set_fail_links(req); | |
bcda7baa | 2681 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2682 | cflags = io_put_rw_kbuf(req); |
a1d7c393 | 2683 | __io_req_complete(req, res, cflags, cs); |
ba816ad6 JA |
2684 | } |
2685 | ||
b63534c4 | 2686 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2687 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2688 | { |
2689 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
2690 | ssize_t ret = -ECANCELED; | |
2691 | struct iov_iter iter; | |
2692 | int rw; | |
2693 | ||
dc2a6e9a PB |
2694 | /* already prepared */ |
2695 | if (req->async_data) | |
2696 | return true; | |
b63534c4 JA |
2697 | |
2698 | switch (req->opcode) { | |
2699 | case IORING_OP_READV: | |
2700 | case IORING_OP_READ_FIXED: | |
2701 | case IORING_OP_READ: | |
2702 | rw = READ; | |
2703 | break; | |
2704 | case IORING_OP_WRITEV: | |
2705 | case IORING_OP_WRITE_FIXED: | |
2706 | case IORING_OP_WRITE: | |
2707 | rw = WRITE; | |
2708 | break; | |
2709 | default: | |
2710 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2711 | req->opcode); | |
dc2a6e9a | 2712 | return false; |
b63534c4 JA |
2713 | } |
2714 | ||
dc2a6e9a PB |
2715 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2716 | if (ret < 0) | |
2717 | return false; | |
2718 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); | |
2719 | if (!ret) | |
b63534c4 | 2720 | return true; |
dc2a6e9a | 2721 | kfree(iovec); |
b63534c4 JA |
2722 | return false; |
2723 | } | |
b63534c4 JA |
2724 | #endif |
2725 | ||
2726 | static bool io_rw_reissue(struct io_kiocb *req, long res) | |
2727 | { | |
2728 | #ifdef CONFIG_BLOCK | |
bf6182b6 | 2729 | umode_t mode; |
b63534c4 JA |
2730 | int ret; |
2731 | ||
bf6182b6 | 2732 | if (res != -EAGAIN && res != -EOPNOTSUPP) |
355afaeb | 2733 | return false; |
bf6182b6 PB |
2734 | mode = file_inode(req->file)->i_mode; |
2735 | if ((!S_ISBLK(mode) && !S_ISREG(mode)) || io_wq_current_is_worker()) | |
b63534c4 JA |
2736 | return false; |
2737 | ||
55e6ac1e PB |
2738 | lockdep_assert_held(&req->ctx->uring_lock); |
2739 | ||
28cea78a | 2740 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
6d816e08 | 2741 | |
dc2a6e9a | 2742 | if (!ret && io_resubmit_prep(req)) { |
fdee946d JA |
2743 | refcount_inc(&req->refs); |
2744 | io_queue_async_work(req); | |
b63534c4 | 2745 | return true; |
fdee946d | 2746 | } |
dc2a6e9a | 2747 | req_set_fail_links(req); |
b63534c4 JA |
2748 | #endif |
2749 | return false; | |
2750 | } | |
2751 | ||
a1d7c393 JA |
2752 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
2753 | struct io_comp_state *cs) | |
2754 | { | |
2755 | if (!io_rw_reissue(req, res)) | |
2756 | io_complete_rw_common(&req->rw.kiocb, res, cs); | |
ba816ad6 JA |
2757 | } |
2758 | ||
2759 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2760 | { | |
9adbd45d | 2761 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2762 | |
a1d7c393 | 2763 | __io_complete_rw(req, res, res2, NULL); |
2b188cc1 JA |
2764 | } |
2765 | ||
def596e9 JA |
2766 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2767 | { | |
9adbd45d | 2768 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2769 | |
491381ce JA |
2770 | if (kiocb->ki_flags & IOCB_WRITE) |
2771 | kiocb_end_write(req); | |
def596e9 | 2772 | |
2d7d6792 | 2773 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2774 | req_set_fail_links(req); |
bbde017a XW |
2775 | |
2776 | WRITE_ONCE(req->result, res); | |
2777 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2778 | smp_wmb(); |
2779 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2780 | } |
2781 | ||
2782 | /* | |
2783 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2784 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2785 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2786 | * accessing the kiocb cookie. | |
2787 | */ | |
2e9dbe90 | 2788 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2789 | { |
2790 | struct io_ring_ctx *ctx = req->ctx; | |
2791 | ||
2792 | /* | |
2793 | * Track whether we have multiple files in our lists. This will impact | |
2794 | * how we do polling eventually, not spinning if we're on potentially | |
2795 | * different devices. | |
2796 | */ | |
540e32a0 | 2797 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2798 | ctx->poll_multi_file = false; |
2799 | } else if (!ctx->poll_multi_file) { | |
2800 | struct io_kiocb *list_req; | |
2801 | ||
540e32a0 | 2802 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2803 | inflight_entry); |
9adbd45d | 2804 | if (list_req->file != req->file) |
def596e9 JA |
2805 | ctx->poll_multi_file = true; |
2806 | } | |
2807 | ||
2808 | /* | |
2809 | * For fast devices, IO may have already completed. If it has, add | |
2810 | * it to the front so we find it first. | |
2811 | */ | |
65a6543d | 2812 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2813 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2814 | else |
d21ffe7e | 2815 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2816 | |
2e9dbe90 XW |
2817 | /* |
2818 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2819 | * task context or in io worker task context. If current task context is | |
2820 | * sq thread, we don't need to check whether should wake up sq thread. | |
2821 | */ | |
2822 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2823 | wq_has_sleeper(&ctx->sq_data->wait)) |
2824 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2825 | } |
2826 | ||
9f13c35b PB |
2827 | static inline void io_state_file_put(struct io_submit_state *state) |
2828 | { | |
02b23a9a PB |
2829 | if (state->file_refs) { |
2830 | fput_many(state->file, state->file_refs); | |
2831 | state->file_refs = 0; | |
2832 | } | |
9a56a232 JA |
2833 | } |
2834 | ||
2835 | /* | |
2836 | * Get as many references to a file as we have IOs left in this submission, | |
2837 | * assuming most submissions are for one file, or at least that each file | |
2838 | * has more than one submission. | |
2839 | */ | |
8da11c19 | 2840 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2841 | { |
2842 | if (!state) | |
2843 | return fget(fd); | |
2844 | ||
6e1271e6 | 2845 | if (state->file_refs) { |
9a56a232 | 2846 | if (state->fd == fd) { |
6e1271e6 | 2847 | state->file_refs--; |
9a56a232 JA |
2848 | return state->file; |
2849 | } | |
02b23a9a | 2850 | io_state_file_put(state); |
9a56a232 JA |
2851 | } |
2852 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2853 | if (unlikely(!state->file)) |
9a56a232 JA |
2854 | return NULL; |
2855 | ||
2856 | state->fd = fd; | |
6e1271e6 | 2857 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2858 | return state->file; |
2859 | } | |
2860 | ||
4503b767 JA |
2861 | static bool io_bdev_nowait(struct block_device *bdev) |
2862 | { | |
9ba0d0c8 | 2863 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2864 | } |
2865 | ||
2b188cc1 JA |
2866 | /* |
2867 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2868 | * any file. For now, just ensure that anything potentially problematic is done | |
2869 | * inline. | |
2870 | */ | |
af197f50 | 2871 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2872 | { |
2873 | umode_t mode = file_inode(file)->i_mode; | |
2874 | ||
4503b767 | 2875 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2876 | if (IS_ENABLED(CONFIG_BLOCK) && |
2877 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2878 | return true; |
2879 | return false; | |
2880 | } | |
2881 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2882 | return true; |
4503b767 | 2883 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2884 | if (IS_ENABLED(CONFIG_BLOCK) && |
2885 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2886 | file->f_op != &io_uring_fops) |
2887 | return true; | |
2888 | return false; | |
2889 | } | |
2b188cc1 | 2890 | |
c5b85625 JA |
2891 | /* any ->read/write should understand O_NONBLOCK */ |
2892 | if (file->f_flags & O_NONBLOCK) | |
2893 | return true; | |
2894 | ||
af197f50 JA |
2895 | if (!(file->f_mode & FMODE_NOWAIT)) |
2896 | return false; | |
2897 | ||
2898 | if (rw == READ) | |
2899 | return file->f_op->read_iter != NULL; | |
2900 | ||
2901 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2902 | } |
2903 | ||
a88fc400 | 2904 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2905 | { |
def596e9 | 2906 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2907 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
2908 | unsigned ioprio; |
2909 | int ret; | |
2b188cc1 | 2910 | |
491381ce JA |
2911 | if (S_ISREG(file_inode(req->file)->i_mode)) |
2912 | req->flags |= REQ_F_ISREG; | |
2913 | ||
2b188cc1 | 2914 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
2915 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
2916 | req->flags |= REQ_F_CUR_POS; | |
2917 | kiocb->ki_pos = req->file->f_pos; | |
2918 | } | |
2b188cc1 | 2919 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2920 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2921 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2922 | if (unlikely(ret)) | |
2923 | return ret; | |
2b188cc1 JA |
2924 | |
2925 | ioprio = READ_ONCE(sqe->ioprio); | |
2926 | if (ioprio) { | |
2927 | ret = ioprio_check_cap(ioprio); | |
2928 | if (ret) | |
09bb8394 | 2929 | return ret; |
2b188cc1 JA |
2930 | |
2931 | kiocb->ki_ioprio = ioprio; | |
2932 | } else | |
2933 | kiocb->ki_ioprio = get_current_ioprio(); | |
2934 | ||
8449eeda | 2935 | /* don't allow async punt if RWF_NOWAIT was requested */ |
c5b85625 | 2936 | if (kiocb->ki_flags & IOCB_NOWAIT) |
8449eeda SB |
2937 | req->flags |= REQ_F_NOWAIT; |
2938 | ||
def596e9 | 2939 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2940 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2941 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2942 | return -EOPNOTSUPP; |
2b188cc1 | 2943 | |
def596e9 JA |
2944 | kiocb->ki_flags |= IOCB_HIPRI; |
2945 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2946 | req->iopoll_completed = 0; |
def596e9 | 2947 | } else { |
09bb8394 JA |
2948 | if (kiocb->ki_flags & IOCB_HIPRI) |
2949 | return -EINVAL; | |
def596e9 JA |
2950 | kiocb->ki_complete = io_complete_rw; |
2951 | } | |
9adbd45d | 2952 | |
3529d8c2 JA |
2953 | req->rw.addr = READ_ONCE(sqe->addr); |
2954 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2955 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2956 | return 0; |
2b188cc1 JA |
2957 | } |
2958 | ||
2959 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2960 | { | |
2961 | switch (ret) { | |
2962 | case -EIOCBQUEUED: | |
2963 | break; | |
2964 | case -ERESTARTSYS: | |
2965 | case -ERESTARTNOINTR: | |
2966 | case -ERESTARTNOHAND: | |
2967 | case -ERESTART_RESTARTBLOCK: | |
2968 | /* | |
2969 | * We can't just restart the syscall, since previously | |
2970 | * submitted sqes may already be in progress. Just fail this | |
2971 | * IO with EINTR. | |
2972 | */ | |
2973 | ret = -EINTR; | |
df561f66 | 2974 | fallthrough; |
2b188cc1 JA |
2975 | default: |
2976 | kiocb->ki_complete(kiocb, ret, 0); | |
2977 | } | |
2978 | } | |
2979 | ||
a1d7c393 JA |
2980 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
2981 | struct io_comp_state *cs) | |
ba816ad6 | 2982 | { |
ba04291e | 2983 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2984 | struct io_async_rw *io = req->async_data; |
ba04291e | 2985 | |
227c0c96 | 2986 | /* add previously done IO, if any */ |
e8c2bc1f | 2987 | if (io && io->bytes_done > 0) { |
227c0c96 | 2988 | if (ret < 0) |
e8c2bc1f | 2989 | ret = io->bytes_done; |
227c0c96 | 2990 | else |
e8c2bc1f | 2991 | ret += io->bytes_done; |
227c0c96 JA |
2992 | } |
2993 | ||
ba04291e JA |
2994 | if (req->flags & REQ_F_CUR_POS) |
2995 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2996 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
a1d7c393 | 2997 | __io_complete_rw(req, ret, 0, cs); |
ba816ad6 JA |
2998 | else |
2999 | io_rw_done(kiocb, ret); | |
3000 | } | |
3001 | ||
9adbd45d | 3002 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 3003 | struct iov_iter *iter) |
edafccee | 3004 | { |
9adbd45d JA |
3005 | struct io_ring_ctx *ctx = req->ctx; |
3006 | size_t len = req->rw.len; | |
edafccee | 3007 | struct io_mapped_ubuf *imu; |
4be1c615 | 3008 | u16 index, buf_index = req->buf_index; |
edafccee JA |
3009 | size_t offset; |
3010 | u64 buf_addr; | |
3011 | ||
edafccee JA |
3012 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3013 | return -EFAULT; | |
edafccee JA |
3014 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3015 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 3016 | buf_addr = req->rw.addr; |
edafccee JA |
3017 | |
3018 | /* overflow */ | |
3019 | if (buf_addr + len < buf_addr) | |
3020 | return -EFAULT; | |
3021 | /* not inside the mapped region */ | |
3022 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
3023 | return -EFAULT; | |
3024 | ||
3025 | /* | |
3026 | * May not be a start of buffer, set size appropriately | |
3027 | * and advance us to the beginning. | |
3028 | */ | |
3029 | offset = buf_addr - imu->ubuf; | |
3030 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3031 | |
3032 | if (offset) { | |
3033 | /* | |
3034 | * Don't use iov_iter_advance() here, as it's really slow for | |
3035 | * using the latter parts of a big fixed buffer - it iterates | |
3036 | * over each segment manually. We can cheat a bit here, because | |
3037 | * we know that: | |
3038 | * | |
3039 | * 1) it's a BVEC iter, we set it up | |
3040 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3041 | * first and last bvec | |
3042 | * | |
3043 | * So just find our index, and adjust the iterator afterwards. | |
3044 | * If the offset is within the first bvec (or the whole first | |
3045 | * bvec, just use iov_iter_advance(). This makes it easier | |
3046 | * since we can just skip the first segment, which may not | |
3047 | * be PAGE_SIZE aligned. | |
3048 | */ | |
3049 | const struct bio_vec *bvec = imu->bvec; | |
3050 | ||
3051 | if (offset <= bvec->bv_len) { | |
3052 | iov_iter_advance(iter, offset); | |
3053 | } else { | |
3054 | unsigned long seg_skip; | |
3055 | ||
3056 | /* skip first vec */ | |
3057 | offset -= bvec->bv_len; | |
3058 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3059 | ||
3060 | iter->bvec = bvec + seg_skip; | |
3061 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3062 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3063 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3064 | } |
3065 | } | |
3066 | ||
5e559561 | 3067 | return len; |
edafccee JA |
3068 | } |
3069 | ||
bcda7baa JA |
3070 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3071 | { | |
3072 | if (needs_lock) | |
3073 | mutex_unlock(&ctx->uring_lock); | |
3074 | } | |
3075 | ||
3076 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3077 | { | |
3078 | /* | |
3079 | * "Normal" inline submissions always hold the uring_lock, since we | |
3080 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3081 | * The only exception is when we've detached the request and issue it | |
3082 | * from an async worker thread, grab the lock for that case. | |
3083 | */ | |
3084 | if (needs_lock) | |
3085 | mutex_lock(&ctx->uring_lock); | |
3086 | } | |
3087 | ||
3088 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3089 | int bgid, struct io_buffer *kbuf, | |
3090 | bool needs_lock) | |
3091 | { | |
3092 | struct io_buffer *head; | |
3093 | ||
3094 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3095 | return kbuf; | |
3096 | ||
3097 | io_ring_submit_lock(req->ctx, needs_lock); | |
3098 | ||
3099 | lockdep_assert_held(&req->ctx->uring_lock); | |
3100 | ||
3101 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
3102 | if (head) { | |
3103 | if (!list_empty(&head->list)) { | |
3104 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3105 | list); | |
3106 | list_del(&kbuf->list); | |
3107 | } else { | |
3108 | kbuf = head; | |
3109 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
3110 | } | |
3111 | if (*len > kbuf->len) | |
3112 | *len = kbuf->len; | |
3113 | } else { | |
3114 | kbuf = ERR_PTR(-ENOBUFS); | |
3115 | } | |
3116 | ||
3117 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3118 | ||
3119 | return kbuf; | |
3120 | } | |
3121 | ||
4d954c25 JA |
3122 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3123 | bool needs_lock) | |
3124 | { | |
3125 | struct io_buffer *kbuf; | |
4f4eeba8 | 3126 | u16 bgid; |
4d954c25 JA |
3127 | |
3128 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3129 | bgid = req->buf_index; |
4d954c25 JA |
3130 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3131 | if (IS_ERR(kbuf)) | |
3132 | return kbuf; | |
3133 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3134 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3135 | return u64_to_user_ptr(kbuf->addr); | |
3136 | } | |
3137 | ||
3138 | #ifdef CONFIG_COMPAT | |
3139 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3140 | bool needs_lock) | |
3141 | { | |
3142 | struct compat_iovec __user *uiov; | |
3143 | compat_ssize_t clen; | |
3144 | void __user *buf; | |
3145 | ssize_t len; | |
3146 | ||
3147 | uiov = u64_to_user_ptr(req->rw.addr); | |
3148 | if (!access_ok(uiov, sizeof(*uiov))) | |
3149 | return -EFAULT; | |
3150 | if (__get_user(clen, &uiov->iov_len)) | |
3151 | return -EFAULT; | |
3152 | if (clen < 0) | |
3153 | return -EINVAL; | |
3154 | ||
3155 | len = clen; | |
3156 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3157 | if (IS_ERR(buf)) | |
3158 | return PTR_ERR(buf); | |
3159 | iov[0].iov_base = buf; | |
3160 | iov[0].iov_len = (compat_size_t) len; | |
3161 | return 0; | |
3162 | } | |
3163 | #endif | |
3164 | ||
3165 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3166 | bool needs_lock) | |
3167 | { | |
3168 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3169 | void __user *buf; | |
3170 | ssize_t len; | |
3171 | ||
3172 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3173 | return -EFAULT; | |
3174 | ||
3175 | len = iov[0].iov_len; | |
3176 | if (len < 0) | |
3177 | return -EINVAL; | |
3178 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3179 | if (IS_ERR(buf)) | |
3180 | return PTR_ERR(buf); | |
3181 | iov[0].iov_base = buf; | |
3182 | iov[0].iov_len = len; | |
3183 | return 0; | |
3184 | } | |
3185 | ||
3186 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3187 | bool needs_lock) | |
3188 | { | |
dddb3e26 JA |
3189 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3190 | struct io_buffer *kbuf; | |
3191 | ||
3192 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3193 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3194 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3195 | return 0; |
dddb3e26 | 3196 | } |
dd201662 | 3197 | if (req->rw.len != 1) |
4d954c25 JA |
3198 | return -EINVAL; |
3199 | ||
3200 | #ifdef CONFIG_COMPAT | |
3201 | if (req->ctx->compat) | |
3202 | return io_compat_import(req, iov, needs_lock); | |
3203 | #endif | |
3204 | ||
3205 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3206 | } | |
3207 | ||
2846c481 | 3208 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
8452fd0c JA |
3209 | struct iovec **iovec, struct iov_iter *iter, |
3210 | bool needs_lock) | |
2b188cc1 | 3211 | { |
9adbd45d JA |
3212 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3213 | size_t sqe_len = req->rw.len; | |
4d954c25 | 3214 | ssize_t ret; |
edafccee JA |
3215 | u8 opcode; |
3216 | ||
d625c6ee | 3217 | opcode = req->opcode; |
7d009165 | 3218 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3219 | *iovec = NULL; |
9adbd45d | 3220 | return io_import_fixed(req, rw, iter); |
edafccee | 3221 | } |
2b188cc1 | 3222 | |
bcda7baa | 3223 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3224 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3225 | return -EINVAL; |
3226 | ||
3a6820f2 | 3227 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3228 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3229 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3230 | if (IS_ERR(buf)) |
4d954c25 | 3231 | return PTR_ERR(buf); |
3f9d6441 | 3232 | req->rw.len = sqe_len; |
bcda7baa JA |
3233 | } |
3234 | ||
3a6820f2 JA |
3235 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3236 | *iovec = NULL; | |
10fc72e4 | 3237 | return ret; |
3a6820f2 JA |
3238 | } |
3239 | ||
4d954c25 JA |
3240 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3241 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
3f9d6441 JA |
3242 | if (!ret) { |
3243 | ret = (*iovec)->iov_len; | |
3244 | iov_iter_init(iter, rw, *iovec, 1, ret); | |
3245 | } | |
4d954c25 JA |
3246 | *iovec = NULL; |
3247 | return ret; | |
3248 | } | |
3249 | ||
89cd35c5 CH |
3250 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3251 | req->ctx->compat); | |
2b188cc1 JA |
3252 | } |
3253 | ||
0fef9483 JA |
3254 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3255 | { | |
5b09e37e | 3256 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3257 | } |
3258 | ||
31b51510 | 3259 | /* |
32960613 JA |
3260 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3261 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3262 | */ |
4017eb91 | 3263 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3264 | { |
4017eb91 JA |
3265 | struct kiocb *kiocb = &req->rw.kiocb; |
3266 | struct file *file = req->file; | |
32960613 JA |
3267 | ssize_t ret = 0; |
3268 | ||
3269 | /* | |
3270 | * Don't support polled IO through this interface, and we can't | |
3271 | * support non-blocking either. For the latter, this just causes | |
3272 | * the kiocb to be handled from an async context. | |
3273 | */ | |
3274 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3275 | return -EOPNOTSUPP; | |
3276 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3277 | return -EAGAIN; | |
3278 | ||
3279 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3280 | struct iovec iovec; |
32960613 JA |
3281 | ssize_t nr; |
3282 | ||
311ae9e1 PB |
3283 | if (!iov_iter_is_bvec(iter)) { |
3284 | iovec = iov_iter_iovec(iter); | |
3285 | } else { | |
4017eb91 JA |
3286 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3287 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3288 | } |
3289 | ||
32960613 JA |
3290 | if (rw == READ) { |
3291 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3292 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3293 | } else { |
3294 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3295 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3296 | } |
3297 | ||
3298 | if (nr < 0) { | |
3299 | if (!ret) | |
3300 | ret = nr; | |
3301 | break; | |
3302 | } | |
3303 | ret += nr; | |
3304 | if (nr != iovec.iov_len) | |
3305 | break; | |
4017eb91 JA |
3306 | req->rw.len -= nr; |
3307 | req->rw.addr += nr; | |
32960613 JA |
3308 | iov_iter_advance(iter, nr); |
3309 | } | |
3310 | ||
3311 | return ret; | |
3312 | } | |
3313 | ||
ff6165b2 JA |
3314 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3315 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3316 | { |
e8c2bc1f | 3317 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3318 | |
ff6165b2 | 3319 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3320 | rw->free_iovec = iovec; |
227c0c96 | 3321 | rw->bytes_done = 0; |
ff6165b2 | 3322 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3323 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3324 | return; |
b64e3444 | 3325 | if (!iovec) { |
ff6165b2 JA |
3326 | unsigned iov_off = 0; |
3327 | ||
3328 | rw->iter.iov = rw->fast_iov; | |
3329 | if (iter->iov != fast_iov) { | |
3330 | iov_off = iter->iov - fast_iov; | |
3331 | rw->iter.iov += iov_off; | |
3332 | } | |
3333 | if (rw->fast_iov != fast_iov) | |
3334 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3335 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3336 | } else { |
3337 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3338 | } |
3339 | } | |
3340 | ||
e8c2bc1f | 3341 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3342 | { |
e8c2bc1f JA |
3343 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3344 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3345 | return req->async_data == NULL; | |
3d9932a8 XW |
3346 | } |
3347 | ||
e8c2bc1f | 3348 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3349 | { |
e8c2bc1f | 3350 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3351 | return 0; |
3d9932a8 | 3352 | |
e8c2bc1f | 3353 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3354 | } |
3355 | ||
ff6165b2 JA |
3356 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3357 | const struct iovec *fast_iov, | |
227c0c96 | 3358 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3359 | { |
e8c2bc1f | 3360 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3361 | return 0; |
e8c2bc1f JA |
3362 | if (!req->async_data) { |
3363 | if (__io_alloc_async_data(req)) | |
5d204bcf | 3364 | return -ENOMEM; |
b7bb4f7d | 3365 | |
ff6165b2 | 3366 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3367 | } |
b7bb4f7d | 3368 | return 0; |
f67676d1 JA |
3369 | } |
3370 | ||
73debe68 | 3371 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3372 | { |
e8c2bc1f | 3373 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3374 | struct iovec *iov = iorw->fast_iov; |
c3e330a4 PB |
3375 | ssize_t ret; |
3376 | ||
2846c481 | 3377 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3378 | if (unlikely(ret < 0)) |
3379 | return ret; | |
3380 | ||
ab0b196c PB |
3381 | iorw->bytes_done = 0; |
3382 | iorw->free_iovec = iov; | |
3383 | if (iov) | |
3384 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3385 | return 0; |
3386 | } | |
3387 | ||
73debe68 | 3388 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3389 | { |
3390 | ssize_t ret; | |
3391 | ||
a88fc400 | 3392 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3393 | if (ret) |
3394 | return ret; | |
f67676d1 | 3395 | |
3529d8c2 JA |
3396 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3397 | return -EBADF; | |
f67676d1 | 3398 | |
5f798bea | 3399 | /* either don't need iovec imported or already have it */ |
2d199895 | 3400 | if (!req->async_data) |
3529d8c2 | 3401 | return 0; |
73debe68 | 3402 | return io_rw_prep_async(req, READ); |
f67676d1 JA |
3403 | } |
3404 | ||
c1dd91d1 JA |
3405 | /* |
3406 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3407 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3408 | * This gets called when the page is unlocked, and we generally expect that to | |
3409 | * happen when the page IO is completed and the page is now uptodate. This will | |
3410 | * queue a task_work based retry of the operation, attempting to copy the data | |
3411 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3412 | * do a thread based blocking retry of the operation. That's the unexpected | |
3413 | * slow path. | |
3414 | */ | |
bcf5a063 JA |
3415 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3416 | int sync, void *arg) | |
3417 | { | |
3418 | struct wait_page_queue *wpq; | |
3419 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3420 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3421 | int ret; |
3422 | ||
3423 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3424 | ||
cdc8fcb4 LT |
3425 | if (!wake_page_match(wpq, key)) |
3426 | return 0; | |
3427 | ||
c8d317aa | 3428 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3429 | list_del_init(&wait->entry); |
3430 | ||
e7375122 | 3431 | init_task_work(&req->task_work, io_req_task_submit); |
6d816e08 JA |
3432 | percpu_ref_get(&req->ctx->refs); |
3433 | ||
bcf5a063 JA |
3434 | /* submit ref gets dropped, acquire a new one */ |
3435 | refcount_inc(&req->refs); | |
355fb9e2 | 3436 | ret = io_req_task_work_add(req); |
eab30c4d PB |
3437 | if (unlikely(ret)) |
3438 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
bcf5a063 JA |
3439 | return 1; |
3440 | } | |
3441 | ||
c1dd91d1 JA |
3442 | /* |
3443 | * This controls whether a given IO request should be armed for async page | |
3444 | * based retry. If we return false here, the request is handed to the async | |
3445 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3446 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3447 | * will either succeed because the page is now uptodate and unlocked, or it | |
3448 | * will register a callback when the page is unlocked at IO completion. Through | |
3449 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3450 | * That retry will attempt the buffered read again. The retry will generally | |
3451 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3452 | * async worker threads for a blocking retry. | |
3453 | */ | |
227c0c96 | 3454 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3455 | { |
e8c2bc1f JA |
3456 | struct io_async_rw *rw = req->async_data; |
3457 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3458 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3459 | |
bcf5a063 JA |
3460 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3461 | if (req->flags & REQ_F_NOWAIT) | |
3462 | return false; | |
f67676d1 | 3463 | |
227c0c96 | 3464 | /* Only for buffered IO */ |
3b2a4439 | 3465 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3466 | return false; |
3b2a4439 | 3467 | |
bcf5a063 JA |
3468 | /* |
3469 | * just use poll if we can, and don't attempt if the fs doesn't | |
3470 | * support callback based unlocks | |
3471 | */ | |
3472 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3473 | return false; | |
f67676d1 | 3474 | |
3b2a4439 JA |
3475 | wait->wait.func = io_async_buf_func; |
3476 | wait->wait.private = req; | |
3477 | wait->wait.flags = 0; | |
3478 | INIT_LIST_HEAD(&wait->wait.entry); | |
3479 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3480 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3481 | kiocb->ki_waitq = wait; |
3b2a4439 | 3482 | return true; |
bcf5a063 JA |
3483 | } |
3484 | ||
3485 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3486 | { | |
3487 | if (req->file->f_op->read_iter) | |
3488 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3489 | else if (req->file->f_op->read) |
4017eb91 | 3490 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3491 | else |
3492 | return -EINVAL; | |
f67676d1 JA |
3493 | } |
3494 | ||
a1d7c393 JA |
3495 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
3496 | struct io_comp_state *cs) | |
2b188cc1 JA |
3497 | { |
3498 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3499 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3500 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3501 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3502 | ssize_t io_size, ret, ret2; |
f5cac8b1 | 3503 | bool no_async; |
ff6165b2 | 3504 | |
2846c481 | 3505 | if (rw) { |
e8c2bc1f | 3506 | iter = &rw->iter; |
2846c481 PB |
3507 | iovec = NULL; |
3508 | } else { | |
3509 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3510 | if (ret < 0) | |
3511 | return ret; | |
3512 | } | |
632546c4 | 3513 | io_size = iov_iter_count(iter); |
fa15bafb | 3514 | req->result = io_size; |
227c0c96 | 3515 | ret = 0; |
2b188cc1 | 3516 | |
fd6c2e4c JA |
3517 | /* Ensure we clear previously set non-block flag */ |
3518 | if (!force_nonblock) | |
29de5f6a | 3519 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3520 | else |
3521 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3522 | ||
fd6c2e4c | 3523 | |
24c74678 | 3524 | /* If the file doesn't support async, just async punt */ |
f5cac8b1 JA |
3525 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
3526 | if (no_async) | |
f67676d1 | 3527 | goto copy_iov; |
9e645e11 | 3528 | |
632546c4 | 3529 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3530 | if (unlikely(ret)) |
3531 | goto out_free; | |
2b188cc1 | 3532 | |
227c0c96 | 3533 | ret = io_iter_do_read(req, iter); |
32960613 | 3534 | |
227c0c96 JA |
3535 | if (!ret) { |
3536 | goto done; | |
3537 | } else if (ret == -EIOCBQUEUED) { | |
3538 | ret = 0; | |
3539 | goto out_free; | |
3540 | } else if (ret == -EAGAIN) { | |
eefdf30f JA |
3541 | /* IOPOLL retry should happen for io-wq threads */ |
3542 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3543 | goto done; |
355afaeb JA |
3544 | /* no retry on NONBLOCK marked file */ |
3545 | if (req->file->f_flags & O_NONBLOCK) | |
3546 | goto done; | |
84216315 | 3547 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3548 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a JA |
3549 | ret = 0; |
3550 | goto copy_iov; | |
227c0c96 | 3551 | } else if (ret < 0) { |
00d23d51 JA |
3552 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
3553 | goto done; | |
227c0c96 JA |
3554 | } |
3555 | ||
3556 | /* read it all, or we did blocking attempt. no retry. */ | |
f91daf56 | 3557 | if (!iov_iter_count(iter) || !force_nonblock || |
9a173346 | 3558 | (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG)) |
227c0c96 JA |
3559 | goto done; |
3560 | ||
3561 | io_size -= ret; | |
3562 | copy_iov: | |
3563 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
3564 | if (ret2) { | |
3565 | ret = ret2; | |
3566 | goto out_free; | |
3567 | } | |
f5cac8b1 JA |
3568 | if (no_async) |
3569 | return -EAGAIN; | |
e8c2bc1f | 3570 | rw = req->async_data; |
227c0c96 JA |
3571 | /* it's copied and will be cleaned with ->io */ |
3572 | iovec = NULL; | |
3573 | /* now use our persistent iterator, if we aren't already */ | |
e8c2bc1f | 3574 | iter = &rw->iter; |
227c0c96 | 3575 | retry: |
e8c2bc1f | 3576 | rw->bytes_done += ret; |
227c0c96 JA |
3577 | /* if we can retry, do so with the callbacks armed */ |
3578 | if (!io_rw_should_retry(req)) { | |
fa15bafb PB |
3579 | kiocb->ki_flags &= ~IOCB_WAITQ; |
3580 | return -EAGAIN; | |
2b188cc1 | 3581 | } |
227c0c96 JA |
3582 | |
3583 | /* | |
3584 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we | |
3585 | * get -EIOCBQUEUED, then we'll get a notification when the desired | |
3586 | * page gets unlocked. We can also get a partial read here, and if we | |
3587 | * do, then just retry at the new offset. | |
3588 | */ | |
3589 | ret = io_iter_do_read(req, iter); | |
3590 | if (ret == -EIOCBQUEUED) { | |
3591 | ret = 0; | |
3592 | goto out_free; | |
3593 | } else if (ret > 0 && ret < io_size) { | |
3594 | /* we got some bytes, but not all. retry. */ | |
3595 | goto retry; | |
3596 | } | |
3597 | done: | |
3598 | kiocb_done(kiocb, ret, cs); | |
3599 | ret = 0; | |
f67676d1 | 3600 | out_free: |
f261c168 | 3601 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3602 | if (iovec) |
6f2cc166 | 3603 | kfree(iovec); |
2b188cc1 JA |
3604 | return ret; |
3605 | } | |
3606 | ||
73debe68 | 3607 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 JA |
3608 | { |
3609 | ssize_t ret; | |
3610 | ||
a88fc400 | 3611 | ret = io_prep_rw(req, sqe); |
3529d8c2 JA |
3612 | if (ret) |
3613 | return ret; | |
f67676d1 | 3614 | |
3529d8c2 JA |
3615 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3616 | return -EBADF; | |
f67676d1 | 3617 | |
5f798bea | 3618 | /* either don't need iovec imported or already have it */ |
2d199895 | 3619 | if (!req->async_data) |
3529d8c2 | 3620 | return 0; |
73debe68 | 3621 | return io_rw_prep_async(req, WRITE); |
f67676d1 JA |
3622 | } |
3623 | ||
a1d7c393 JA |
3624 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
3625 | struct io_comp_state *cs) | |
2b188cc1 JA |
3626 | { |
3627 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3628 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3629 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3630 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3631 | ssize_t ret, ret2, io_size; |
2b188cc1 | 3632 | |
2846c481 | 3633 | if (rw) { |
e8c2bc1f | 3634 | iter = &rw->iter; |
2846c481 PB |
3635 | iovec = NULL; |
3636 | } else { | |
3637 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3638 | if (ret < 0) | |
3639 | return ret; | |
3640 | } | |
632546c4 | 3641 | io_size = iov_iter_count(iter); |
fa15bafb | 3642 | req->result = io_size; |
2b188cc1 | 3643 | |
fd6c2e4c JA |
3644 | /* Ensure we clear previously set non-block flag */ |
3645 | if (!force_nonblock) | |
a88fc400 PB |
3646 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3647 | else | |
3648 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3649 | |
24c74678 | 3650 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3651 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3652 | goto copy_iov; |
31b51510 | 3653 | |
10d59345 JA |
3654 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3655 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3656 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3657 | goto copy_iov; |
31b51510 | 3658 | |
632546c4 | 3659 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3660 | if (unlikely(ret)) |
3661 | goto out_free; | |
4ed734b0 | 3662 | |
fa15bafb PB |
3663 | /* |
3664 | * Open-code file_start_write here to grab freeze protection, | |
3665 | * which will be released by another thread in | |
3666 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3667 | * released so that it doesn't complain about the held lock when | |
3668 | * we return to userspace. | |
3669 | */ | |
3670 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3671 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3672 | __sb_writers_release(file_inode(req->file)->i_sb, |
3673 | SB_FREEZE_WRITE); | |
3674 | } | |
3675 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3676 | |
fa15bafb | 3677 | if (req->file->f_op->write_iter) |
ff6165b2 | 3678 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3679 | else if (req->file->f_op->write) |
4017eb91 | 3680 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3681 | else |
3682 | ret2 = -EINVAL; | |
4ed734b0 | 3683 | |
fa15bafb PB |
3684 | /* |
3685 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3686 | * retry them without IOCB_NOWAIT. | |
3687 | */ | |
3688 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3689 | ret2 = -EAGAIN; | |
355afaeb JA |
3690 | /* no retry on NONBLOCK marked file */ |
3691 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) | |
3692 | goto done; | |
fa15bafb | 3693 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3694 | /* IOPOLL retry should happen for io-wq threads */ |
3695 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3696 | goto copy_iov; | |
355afaeb | 3697 | done: |
fa15bafb PB |
3698 | kiocb_done(kiocb, ret2, cs); |
3699 | } else { | |
f67676d1 | 3700 | copy_iov: |
84216315 | 3701 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3702 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3703 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
ff6165b2 JA |
3704 | if (!ret) |
3705 | return -EAGAIN; | |
2b188cc1 | 3706 | } |
31b51510 | 3707 | out_free: |
f261c168 | 3708 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3709 | if (iovec) |
6f2cc166 | 3710 | kfree(iovec); |
2b188cc1 JA |
3711 | return ret; |
3712 | } | |
3713 | ||
80a261fd JA |
3714 | static int io_renameat_prep(struct io_kiocb *req, |
3715 | const struct io_uring_sqe *sqe) | |
3716 | { | |
3717 | struct io_rename *ren = &req->rename; | |
3718 | const char __user *oldf, *newf; | |
3719 | ||
3720 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3721 | return -EBADF; | |
3722 | ||
3723 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3724 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3725 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3726 | ren->new_dfd = READ_ONCE(sqe->len); | |
3727 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3728 | ||
3729 | ren->oldpath = getname(oldf); | |
3730 | if (IS_ERR(ren->oldpath)) | |
3731 | return PTR_ERR(ren->oldpath); | |
3732 | ||
3733 | ren->newpath = getname(newf); | |
3734 | if (IS_ERR(ren->newpath)) { | |
3735 | putname(ren->oldpath); | |
3736 | return PTR_ERR(ren->newpath); | |
3737 | } | |
3738 | ||
3739 | req->flags |= REQ_F_NEED_CLEANUP; | |
3740 | return 0; | |
3741 | } | |
3742 | ||
3743 | static int io_renameat(struct io_kiocb *req, bool force_nonblock) | |
3744 | { | |
3745 | struct io_rename *ren = &req->rename; | |
3746 | int ret; | |
3747 | ||
3748 | if (force_nonblock) | |
3749 | return -EAGAIN; | |
3750 | ||
3751 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3752 | ren->newpath, ren->flags); | |
3753 | ||
3754 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3755 | if (ret < 0) | |
3756 | req_set_fail_links(req); | |
3757 | io_req_complete(req, ret); | |
3758 | return 0; | |
3759 | } | |
3760 | ||
14a1143b JA |
3761 | static int io_unlinkat_prep(struct io_kiocb *req, |
3762 | const struct io_uring_sqe *sqe) | |
3763 | { | |
3764 | struct io_unlink *un = &req->unlink; | |
3765 | const char __user *fname; | |
3766 | ||
3767 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3768 | return -EBADF; | |
3769 | ||
3770 | un->dfd = READ_ONCE(sqe->fd); | |
3771 | ||
3772 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3773 | if (un->flags & ~AT_REMOVEDIR) | |
3774 | return -EINVAL; | |
3775 | ||
3776 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3777 | un->filename = getname(fname); | |
3778 | if (IS_ERR(un->filename)) | |
3779 | return PTR_ERR(un->filename); | |
3780 | ||
3781 | req->flags |= REQ_F_NEED_CLEANUP; | |
3782 | return 0; | |
3783 | } | |
3784 | ||
3785 | static int io_unlinkat(struct io_kiocb *req, bool force_nonblock) | |
3786 | { | |
3787 | struct io_unlink *un = &req->unlink; | |
3788 | int ret; | |
3789 | ||
3790 | if (force_nonblock) | |
3791 | return -EAGAIN; | |
3792 | ||
3793 | if (un->flags & AT_REMOVEDIR) | |
3794 | ret = do_rmdir(un->dfd, un->filename); | |
3795 | else | |
3796 | ret = do_unlinkat(un->dfd, un->filename); | |
3797 | ||
3798 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3799 | if (ret < 0) | |
3800 | req_set_fail_links(req); | |
3801 | io_req_complete(req, ret); | |
3802 | return 0; | |
3803 | } | |
3804 | ||
36f4fa68 JA |
3805 | static int io_shutdown_prep(struct io_kiocb *req, |
3806 | const struct io_uring_sqe *sqe) | |
3807 | { | |
3808 | #if defined(CONFIG_NET) | |
3809 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3810 | return -EINVAL; | |
3811 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3812 | sqe->buf_index) | |
3813 | return -EINVAL; | |
3814 | ||
3815 | req->shutdown.how = READ_ONCE(sqe->len); | |
3816 | return 0; | |
3817 | #else | |
3818 | return -EOPNOTSUPP; | |
3819 | #endif | |
3820 | } | |
3821 | ||
3822 | static int io_shutdown(struct io_kiocb *req, bool force_nonblock) | |
3823 | { | |
3824 | #if defined(CONFIG_NET) | |
3825 | struct socket *sock; | |
3826 | int ret; | |
3827 | ||
3828 | if (force_nonblock) | |
3829 | return -EAGAIN; | |
3830 | ||
48aba79b | 3831 | sock = sock_from_file(req->file); |
36f4fa68 | 3832 | if (unlikely(!sock)) |
48aba79b | 3833 | return -ENOTSOCK; |
36f4fa68 JA |
3834 | |
3835 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3836 | if (ret < 0) |
3837 | req_set_fail_links(req); | |
36f4fa68 JA |
3838 | io_req_complete(req, ret); |
3839 | return 0; | |
3840 | #else | |
3841 | return -EOPNOTSUPP; | |
3842 | #endif | |
3843 | } | |
3844 | ||
f2a8d5c7 PB |
3845 | static int __io_splice_prep(struct io_kiocb *req, |
3846 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3847 | { |
3848 | struct io_splice* sp = &req->splice; | |
3849 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3850 | |
3232dd02 PB |
3851 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3852 | return -EINVAL; | |
7d67af2c PB |
3853 | |
3854 | sp->file_in = NULL; | |
7d67af2c PB |
3855 | sp->len = READ_ONCE(sqe->len); |
3856 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3857 | ||
3858 | if (unlikely(sp->flags & ~valid_flags)) | |
3859 | return -EINVAL; | |
3860 | ||
8371adf5 PB |
3861 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3862 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3863 | if (!sp->file_in) | |
3864 | return -EBADF; | |
7d67af2c PB |
3865 | req->flags |= REQ_F_NEED_CLEANUP; |
3866 | ||
7cdaf587 XW |
3867 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3868 | /* | |
3869 | * Splice operation will be punted aync, and here need to | |
3870 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3871 | */ | |
3872 | io_req_init_async(req); | |
7d67af2c | 3873 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3874 | } |
7d67af2c PB |
3875 | |
3876 | return 0; | |
3877 | } | |
3878 | ||
f2a8d5c7 PB |
3879 | static int io_tee_prep(struct io_kiocb *req, |
3880 | const struct io_uring_sqe *sqe) | |
3881 | { | |
3882 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3883 | return -EINVAL; | |
3884 | return __io_splice_prep(req, sqe); | |
3885 | } | |
3886 | ||
3887 | static int io_tee(struct io_kiocb *req, bool force_nonblock) | |
3888 | { | |
3889 | struct io_splice *sp = &req->splice; | |
3890 | struct file *in = sp->file_in; | |
3891 | struct file *out = sp->file_out; | |
3892 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3893 | long ret = 0; | |
3894 | ||
3895 | if (force_nonblock) | |
3896 | return -EAGAIN; | |
3897 | if (sp->len) | |
3898 | ret = do_tee(in, out, sp->len, flags); | |
3899 | ||
3900 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3901 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3902 | ||
f2a8d5c7 PB |
3903 | if (ret != sp->len) |
3904 | req_set_fail_links(req); | |
e1e16097 | 3905 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3906 | return 0; |
3907 | } | |
3908 | ||
3909 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3910 | { | |
3911 | struct io_splice* sp = &req->splice; | |
3912 | ||
3913 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3914 | sp->off_out = READ_ONCE(sqe->off); | |
3915 | return __io_splice_prep(req, sqe); | |
3916 | } | |
3917 | ||
014db007 | 3918 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
7d67af2c PB |
3919 | { |
3920 | struct io_splice *sp = &req->splice; | |
3921 | struct file *in = sp->file_in; | |
3922 | struct file *out = sp->file_out; | |
3923 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3924 | loff_t *poff_in, *poff_out; | |
c9687426 | 3925 | long ret = 0; |
7d67af2c | 3926 | |
2fb3e822 PB |
3927 | if (force_nonblock) |
3928 | return -EAGAIN; | |
7d67af2c PB |
3929 | |
3930 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3931 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3932 | |
948a7749 | 3933 | if (sp->len) |
c9687426 | 3934 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3935 | |
3936 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3937 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3938 | ||
7d67af2c PB |
3939 | if (ret != sp->len) |
3940 | req_set_fail_links(req); | |
e1e16097 | 3941 | io_req_complete(req, ret); |
7d67af2c PB |
3942 | return 0; |
3943 | } | |
3944 | ||
2b188cc1 JA |
3945 | /* |
3946 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3947 | */ | |
229a7b63 | 3948 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 JA |
3949 | { |
3950 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3951 | |
def596e9 JA |
3952 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3953 | return -EINVAL; | |
3954 | ||
229a7b63 | 3955 | __io_req_complete(req, 0, 0, cs); |
2b188cc1 JA |
3956 | return 0; |
3957 | } | |
3958 | ||
3529d8c2 | 3959 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3960 | { |
6b06314c | 3961 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3962 | |
09bb8394 JA |
3963 | if (!req->file) |
3964 | return -EBADF; | |
c992fe29 | 3965 | |
6b06314c | 3966 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3967 | return -EINVAL; |
edafccee | 3968 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3969 | return -EINVAL; |
3970 | ||
8ed8d3c3 JA |
3971 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3972 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3973 | return -EINVAL; | |
3974 | ||
3975 | req->sync.off = READ_ONCE(sqe->off); | |
3976 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3977 | return 0; |
3978 | } | |
3979 | ||
ac45abc0 | 3980 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 3981 | { |
8ed8d3c3 | 3982 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3983 | int ret; |
3984 | ||
ac45abc0 PB |
3985 | /* fsync always requires a blocking context */ |
3986 | if (force_nonblock) | |
3987 | return -EAGAIN; | |
3988 | ||
9adbd45d | 3989 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3990 | end > 0 ? end : LLONG_MAX, |
3991 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3992 | if (ret < 0) | |
3993 | req_set_fail_links(req); | |
e1e16097 | 3994 | io_req_complete(req, ret); |
c992fe29 CH |
3995 | return 0; |
3996 | } | |
3997 | ||
d63d1b5e JA |
3998 | static int io_fallocate_prep(struct io_kiocb *req, |
3999 | const struct io_uring_sqe *sqe) | |
4000 | { | |
4001 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
4002 | return -EINVAL; | |
3232dd02 PB |
4003 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4004 | return -EINVAL; | |
d63d1b5e JA |
4005 | |
4006 | req->sync.off = READ_ONCE(sqe->off); | |
4007 | req->sync.len = READ_ONCE(sqe->addr); | |
4008 | req->sync.mode = READ_ONCE(sqe->len); | |
4009 | return 0; | |
4010 | } | |
4011 | ||
014db007 | 4012 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
5d17b4a4 | 4013 | { |
ac45abc0 PB |
4014 | int ret; |
4015 | ||
d63d1b5e | 4016 | /* fallocate always requiring blocking context */ |
ac45abc0 | 4017 | if (force_nonblock) |
5d17b4a4 | 4018 | return -EAGAIN; |
ac45abc0 PB |
4019 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4020 | req->sync.len); | |
ac45abc0 PB |
4021 | if (ret < 0) |
4022 | req_set_fail_links(req); | |
e1e16097 | 4023 | io_req_complete(req, ret); |
5d17b4a4 JA |
4024 | return 0; |
4025 | } | |
4026 | ||
ec65fea5 | 4027 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4028 | { |
f8748881 | 4029 | const char __user *fname; |
15b71abe | 4030 | int ret; |
b7bb4f7d | 4031 | |
ec65fea5 | 4032 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4033 | return -EINVAL; |
ec65fea5 | 4034 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4035 | return -EBADF; |
03b1230c | 4036 | |
ec65fea5 PB |
4037 | /* open.how should be already initialised */ |
4038 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4039 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4040 | |
25e72d10 PB |
4041 | req->open.dfd = READ_ONCE(sqe->fd); |
4042 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4043 | req->open.filename = getname(fname); |
15b71abe JA |
4044 | if (IS_ERR(req->open.filename)) { |
4045 | ret = PTR_ERR(req->open.filename); | |
4046 | req->open.filename = NULL; | |
4047 | return ret; | |
4048 | } | |
4022e7af | 4049 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4050 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4051 | return 0; |
03b1230c JA |
4052 | } |
4053 | ||
ec65fea5 PB |
4054 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4055 | { | |
4056 | u64 flags, mode; | |
4057 | ||
14587a46 | 4058 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4059 | return -EINVAL; |
ec65fea5 PB |
4060 | mode = READ_ONCE(sqe->len); |
4061 | flags = READ_ONCE(sqe->open_flags); | |
4062 | req->open.how = build_open_how(flags, mode); | |
4063 | return __io_openat_prep(req, sqe); | |
4064 | } | |
4065 | ||
cebdb986 | 4066 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4067 | { |
cebdb986 | 4068 | struct open_how __user *how; |
cebdb986 | 4069 | size_t len; |
0fa03c62 JA |
4070 | int ret; |
4071 | ||
14587a46 | 4072 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 4073 | return -EINVAL; |
cebdb986 JA |
4074 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4075 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4076 | if (len < OPEN_HOW_SIZE_VER0) |
4077 | return -EINVAL; | |
3529d8c2 | 4078 | |
cebdb986 JA |
4079 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4080 | len); | |
4081 | if (ret) | |
4082 | return ret; | |
3529d8c2 | 4083 | |
ec65fea5 | 4084 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4085 | } |
4086 | ||
014db007 | 4087 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
15b71abe JA |
4088 | { |
4089 | struct open_flags op; | |
15b71abe | 4090 | struct file *file; |
3a81fd02 JA |
4091 | bool nonblock_set; |
4092 | bool resolve_nonblock; | |
15b71abe JA |
4093 | int ret; |
4094 | ||
cebdb986 | 4095 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4096 | if (ret) |
4097 | goto err; | |
3a81fd02 JA |
4098 | nonblock_set = op.open_flag & O_NONBLOCK; |
4099 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
4100 | if (force_nonblock) { | |
4101 | /* | |
4102 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4103 | * it'll always -EAGAIN | |
4104 | */ | |
4105 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4106 | return -EAGAIN; | |
4107 | op.lookup_flags |= LOOKUP_CACHED; | |
4108 | op.open_flag |= O_NONBLOCK; | |
4109 | } | |
15b71abe | 4110 | |
4022e7af | 4111 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
4112 | if (ret < 0) |
4113 | goto err; | |
4114 | ||
4115 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 JA |
4116 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4117 | if ((!resolve_nonblock && force_nonblock) && file == ERR_PTR(-EAGAIN)) { | |
4118 | /* | |
4119 | * We could hang on to this 'fd', but seems like marginal | |
4120 | * gain for something that is now known to be a slower path. | |
4121 | * So just put it, and we'll get a new one when we retry. | |
4122 | */ | |
4123 | put_unused_fd(ret); | |
4124 | return -EAGAIN; | |
4125 | } | |
4126 | ||
15b71abe JA |
4127 | if (IS_ERR(file)) { |
4128 | put_unused_fd(ret); | |
4129 | ret = PTR_ERR(file); | |
4130 | } else { | |
3a81fd02 JA |
4131 | if (force_nonblock && !nonblock_set) |
4132 | file->f_flags &= ~O_NONBLOCK; | |
15b71abe JA |
4133 | fsnotify_open(file); |
4134 | fd_install(ret, file); | |
4135 | } | |
4136 | err: | |
4137 | putname(req->open.filename); | |
8fef80bf | 4138 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
4139 | if (ret < 0) |
4140 | req_set_fail_links(req); | |
e1e16097 | 4141 | io_req_complete(req, ret); |
15b71abe JA |
4142 | return 0; |
4143 | } | |
4144 | ||
014db007 | 4145 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
cebdb986 | 4146 | { |
014db007 | 4147 | return io_openat2(req, force_nonblock); |
cebdb986 JA |
4148 | } |
4149 | ||
067524e9 JA |
4150 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4151 | const struct io_uring_sqe *sqe) | |
4152 | { | |
4153 | struct io_provide_buf *p = &req->pbuf; | |
4154 | u64 tmp; | |
4155 | ||
4156 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
4157 | return -EINVAL; | |
4158 | ||
4159 | tmp = READ_ONCE(sqe->fd); | |
4160 | if (!tmp || tmp > USHRT_MAX) | |
4161 | return -EINVAL; | |
4162 | ||
4163 | memset(p, 0, sizeof(*p)); | |
4164 | p->nbufs = tmp; | |
4165 | p->bgid = READ_ONCE(sqe->buf_group); | |
4166 | return 0; | |
4167 | } | |
4168 | ||
4169 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4170 | int bgid, unsigned nbufs) | |
4171 | { | |
4172 | unsigned i = 0; | |
4173 | ||
4174 | /* shouldn't happen */ | |
4175 | if (!nbufs) | |
4176 | return 0; | |
4177 | ||
4178 | /* the head kbuf is the list itself */ | |
4179 | while (!list_empty(&buf->list)) { | |
4180 | struct io_buffer *nxt; | |
4181 | ||
4182 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4183 | list_del(&nxt->list); | |
4184 | kfree(nxt); | |
4185 | if (++i == nbufs) | |
4186 | return i; | |
4187 | } | |
4188 | i++; | |
4189 | kfree(buf); | |
4190 | idr_remove(&ctx->io_buffer_idr, bgid); | |
4191 | ||
4192 | return i; | |
4193 | } | |
4194 | ||
229a7b63 JA |
4195 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
4196 | struct io_comp_state *cs) | |
067524e9 JA |
4197 | { |
4198 | struct io_provide_buf *p = &req->pbuf; | |
4199 | struct io_ring_ctx *ctx = req->ctx; | |
4200 | struct io_buffer *head; | |
4201 | int ret = 0; | |
4202 | ||
4203 | io_ring_submit_lock(ctx, !force_nonblock); | |
4204 | ||
4205 | lockdep_assert_held(&ctx->uring_lock); | |
4206 | ||
4207 | ret = -ENOENT; | |
4208 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4209 | if (head) | |
4210 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
4211 | if (ret < 0) |
4212 | req_set_fail_links(req); | |
067524e9 | 4213 | |
31bff9a5 PB |
4214 | /* need to hold the lock to complete IOPOLL requests */ |
4215 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4216 | __io_req_complete(req, ret, 0, cs); | |
4217 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4218 | } else { | |
4219 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4220 | __io_req_complete(req, ret, 0, cs); | |
4221 | } | |
067524e9 JA |
4222 | return 0; |
4223 | } | |
4224 | ||
ddf0322d JA |
4225 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4226 | const struct io_uring_sqe *sqe) | |
4227 | { | |
4228 | struct io_provide_buf *p = &req->pbuf; | |
4229 | u64 tmp; | |
4230 | ||
4231 | if (sqe->ioprio || sqe->rw_flags) | |
4232 | return -EINVAL; | |
4233 | ||
4234 | tmp = READ_ONCE(sqe->fd); | |
4235 | if (!tmp || tmp > USHRT_MAX) | |
4236 | return -E2BIG; | |
4237 | p->nbufs = tmp; | |
4238 | p->addr = READ_ONCE(sqe->addr); | |
4239 | p->len = READ_ONCE(sqe->len); | |
4240 | ||
efe68c1c | 4241 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
4242 | return -EFAULT; |
4243 | ||
4244 | p->bgid = READ_ONCE(sqe->buf_group); | |
4245 | tmp = READ_ONCE(sqe->off); | |
4246 | if (tmp > USHRT_MAX) | |
4247 | return -E2BIG; | |
4248 | p->bid = tmp; | |
4249 | return 0; | |
4250 | } | |
4251 | ||
4252 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4253 | { | |
4254 | struct io_buffer *buf; | |
4255 | u64 addr = pbuf->addr; | |
4256 | int i, bid = pbuf->bid; | |
4257 | ||
4258 | for (i = 0; i < pbuf->nbufs; i++) { | |
4259 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4260 | if (!buf) | |
4261 | break; | |
4262 | ||
4263 | buf->addr = addr; | |
4264 | buf->len = pbuf->len; | |
4265 | buf->bid = bid; | |
4266 | addr += pbuf->len; | |
4267 | bid++; | |
4268 | if (!*head) { | |
4269 | INIT_LIST_HEAD(&buf->list); | |
4270 | *head = buf; | |
4271 | } else { | |
4272 | list_add_tail(&buf->list, &(*head)->list); | |
4273 | } | |
4274 | } | |
4275 | ||
4276 | return i ? i : -ENOMEM; | |
4277 | } | |
4278 | ||
229a7b63 JA |
4279 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
4280 | struct io_comp_state *cs) | |
ddf0322d JA |
4281 | { |
4282 | struct io_provide_buf *p = &req->pbuf; | |
4283 | struct io_ring_ctx *ctx = req->ctx; | |
4284 | struct io_buffer *head, *list; | |
4285 | int ret = 0; | |
4286 | ||
4287 | io_ring_submit_lock(ctx, !force_nonblock); | |
4288 | ||
4289 | lockdep_assert_held(&ctx->uring_lock); | |
4290 | ||
4291 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4292 | ||
4293 | ret = io_add_buffers(p, &head); | |
4294 | if (ret < 0) | |
4295 | goto out; | |
4296 | ||
4297 | if (!list) { | |
4298 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4299 | GFP_KERNEL); | |
4300 | if (ret < 0) { | |
067524e9 | 4301 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4302 | goto out; |
4303 | } | |
4304 | } | |
4305 | out: | |
ddf0322d JA |
4306 | if (ret < 0) |
4307 | req_set_fail_links(req); | |
31bff9a5 PB |
4308 | |
4309 | /* need to hold the lock to complete IOPOLL requests */ | |
4310 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
4311 | __io_req_complete(req, ret, 0, cs); | |
4312 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4313 | } else { | |
4314 | io_ring_submit_unlock(ctx, !force_nonblock); | |
4315 | __io_req_complete(req, ret, 0, cs); | |
4316 | } | |
ddf0322d | 4317 | return 0; |
cebdb986 JA |
4318 | } |
4319 | ||
3e4827b0 JA |
4320 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4321 | const struct io_uring_sqe *sqe) | |
4322 | { | |
4323 | #if defined(CONFIG_EPOLL) | |
4324 | if (sqe->ioprio || sqe->buf_index) | |
4325 | return -EINVAL; | |
6ca56f84 | 4326 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4327 | return -EINVAL; |
3e4827b0 JA |
4328 | |
4329 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4330 | req->epoll.op = READ_ONCE(sqe->len); | |
4331 | req->epoll.fd = READ_ONCE(sqe->off); | |
4332 | ||
4333 | if (ep_op_has_event(req->epoll.op)) { | |
4334 | struct epoll_event __user *ev; | |
4335 | ||
4336 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4337 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4338 | return -EFAULT; | |
4339 | } | |
4340 | ||
4341 | return 0; | |
4342 | #else | |
4343 | return -EOPNOTSUPP; | |
4344 | #endif | |
4345 | } | |
4346 | ||
229a7b63 JA |
4347 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
4348 | struct io_comp_state *cs) | |
3e4827b0 JA |
4349 | { |
4350 | #if defined(CONFIG_EPOLL) | |
4351 | struct io_epoll *ie = &req->epoll; | |
4352 | int ret; | |
4353 | ||
4354 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4355 | if (force_nonblock && ret == -EAGAIN) | |
4356 | return -EAGAIN; | |
4357 | ||
4358 | if (ret < 0) | |
4359 | req_set_fail_links(req); | |
229a7b63 | 4360 | __io_req_complete(req, ret, 0, cs); |
3e4827b0 JA |
4361 | return 0; |
4362 | #else | |
4363 | return -EOPNOTSUPP; | |
4364 | #endif | |
4365 | } | |
4366 | ||
c1ca757b JA |
4367 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4368 | { | |
4369 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4370 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4371 | return -EINVAL; | |
3232dd02 PB |
4372 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4373 | return -EINVAL; | |
c1ca757b JA |
4374 | |
4375 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4376 | req->madvise.len = READ_ONCE(sqe->len); | |
4377 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4378 | return 0; | |
4379 | #else | |
4380 | return -EOPNOTSUPP; | |
4381 | #endif | |
4382 | } | |
4383 | ||
014db007 | 4384 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
c1ca757b JA |
4385 | { |
4386 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4387 | struct io_madvise *ma = &req->madvise; | |
4388 | int ret; | |
4389 | ||
4390 | if (force_nonblock) | |
4391 | return -EAGAIN; | |
4392 | ||
0726b01e | 4393 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4394 | if (ret < 0) |
4395 | req_set_fail_links(req); | |
e1e16097 | 4396 | io_req_complete(req, ret); |
c1ca757b JA |
4397 | return 0; |
4398 | #else | |
4399 | return -EOPNOTSUPP; | |
4400 | #endif | |
4401 | } | |
4402 | ||
4840e418 JA |
4403 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4404 | { | |
4405 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4406 | return -EINVAL; | |
3232dd02 PB |
4407 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4408 | return -EINVAL; | |
4840e418 JA |
4409 | |
4410 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4411 | req->fadvise.len = READ_ONCE(sqe->len); | |
4412 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4413 | return 0; | |
4414 | } | |
4415 | ||
014db007 | 4416 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
4840e418 JA |
4417 | { |
4418 | struct io_fadvise *fa = &req->fadvise; | |
4419 | int ret; | |
4420 | ||
3e69426d JA |
4421 | if (force_nonblock) { |
4422 | switch (fa->advice) { | |
4423 | case POSIX_FADV_NORMAL: | |
4424 | case POSIX_FADV_RANDOM: | |
4425 | case POSIX_FADV_SEQUENTIAL: | |
4426 | break; | |
4427 | default: | |
4428 | return -EAGAIN; | |
4429 | } | |
4430 | } | |
4840e418 JA |
4431 | |
4432 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4433 | if (ret < 0) | |
4434 | req_set_fail_links(req); | |
e1e16097 | 4435 | io_req_complete(req, ret); |
4840e418 JA |
4436 | return 0; |
4437 | } | |
4438 | ||
eddc7ef5 JA |
4439 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4440 | { | |
6ca56f84 | 4441 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4442 | return -EINVAL; |
eddc7ef5 JA |
4443 | if (sqe->ioprio || sqe->buf_index) |
4444 | return -EINVAL; | |
9c280f90 | 4445 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4446 | return -EBADF; |
eddc7ef5 | 4447 | |
1d9e1288 BM |
4448 | req->statx.dfd = READ_ONCE(sqe->fd); |
4449 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4450 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4451 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4452 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4453 | |
4454 | return 0; | |
4455 | } | |
4456 | ||
014db007 | 4457 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
eddc7ef5 | 4458 | { |
1d9e1288 | 4459 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4460 | int ret; |
4461 | ||
5b0bbee4 JA |
4462 | if (force_nonblock) { |
4463 | /* only need file table for an actual valid fd */ | |
4464 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4465 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4466 | return -EAGAIN; |
5b0bbee4 | 4467 | } |
eddc7ef5 | 4468 | |
e62753e4 BM |
4469 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4470 | ctx->buffer); | |
eddc7ef5 | 4471 | |
eddc7ef5 JA |
4472 | if (ret < 0) |
4473 | req_set_fail_links(req); | |
e1e16097 | 4474 | io_req_complete(req, ret); |
eddc7ef5 JA |
4475 | return 0; |
4476 | } | |
4477 | ||
b5dba59e JA |
4478 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4479 | { | |
14587a46 | 4480 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4481 | return -EINVAL; |
b5dba59e JA |
4482 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4483 | sqe->rw_flags || sqe->buf_index) | |
4484 | return -EINVAL; | |
9c280f90 | 4485 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4486 | return -EBADF; |
b5dba59e JA |
4487 | |
4488 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4489 | return 0; |
b5dba59e JA |
4490 | } |
4491 | ||
229a7b63 JA |
4492 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
4493 | struct io_comp_state *cs) | |
b5dba59e | 4494 | { |
9eac1904 | 4495 | struct files_struct *files = current->files; |
3af73b28 | 4496 | struct io_close *close = &req->close; |
9eac1904 JA |
4497 | struct fdtable *fdt; |
4498 | struct file *file; | |
b5dba59e JA |
4499 | int ret; |
4500 | ||
9eac1904 JA |
4501 | file = NULL; |
4502 | ret = -EBADF; | |
4503 | spin_lock(&files->file_lock); | |
4504 | fdt = files_fdtable(files); | |
4505 | if (close->fd >= fdt->max_fds) { | |
4506 | spin_unlock(&files->file_lock); | |
4507 | goto err; | |
4508 | } | |
4509 | file = fdt->fd[close->fd]; | |
4510 | if (!file) { | |
4511 | spin_unlock(&files->file_lock); | |
4512 | goto err; | |
4513 | } | |
4514 | ||
4515 | if (file->f_op == &io_uring_fops) { | |
4516 | spin_unlock(&files->file_lock); | |
4517 | file = NULL; | |
4518 | goto err; | |
3af73b28 | 4519 | } |
b5dba59e JA |
4520 | |
4521 | /* if the file has a flush method, be safe and punt to async */ | |
9eac1904 JA |
4522 | if (file->f_op->flush && force_nonblock) { |
4523 | spin_unlock(&files->file_lock); | |
0bf0eefd | 4524 | return -EAGAIN; |
a2100672 | 4525 | } |
b5dba59e | 4526 | |
9eac1904 JA |
4527 | ret = __close_fd_get_file(close->fd, &file); |
4528 | spin_unlock(&files->file_lock); | |
4529 | if (ret < 0) { | |
4530 | if (ret == -ENOENT) | |
4531 | ret = -EBADF; | |
4532 | goto err; | |
4533 | } | |
4534 | ||
3af73b28 | 4535 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4536 | ret = filp_close(file, current->files); |
4537 | err: | |
3af73b28 PB |
4538 | if (ret < 0) |
4539 | req_set_fail_links(req); | |
9eac1904 JA |
4540 | if (file) |
4541 | fput(file); | |
229a7b63 | 4542 | __io_req_complete(req, ret, 0, cs); |
1a417f4e | 4543 | return 0; |
b5dba59e JA |
4544 | } |
4545 | ||
3529d8c2 | 4546 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4547 | { |
4548 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
4549 | |
4550 | if (!req->file) | |
4551 | return -EBADF; | |
5d17b4a4 JA |
4552 | |
4553 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
4554 | return -EINVAL; | |
4555 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4556 | return -EINVAL; | |
4557 | ||
8ed8d3c3 JA |
4558 | req->sync.off = READ_ONCE(sqe->off); |
4559 | req->sync.len = READ_ONCE(sqe->len); | |
4560 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4561 | return 0; |
4562 | } | |
4563 | ||
ac45abc0 | 4564 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
8ed8d3c3 | 4565 | { |
8ed8d3c3 JA |
4566 | int ret; |
4567 | ||
ac45abc0 PB |
4568 | /* sync_file_range always requires a blocking context */ |
4569 | if (force_nonblock) | |
4570 | return -EAGAIN; | |
4571 | ||
9adbd45d | 4572 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4573 | req->sync.flags); |
4574 | if (ret < 0) | |
4575 | req_set_fail_links(req); | |
e1e16097 | 4576 | io_req_complete(req, ret); |
5d17b4a4 JA |
4577 | return 0; |
4578 | } | |
4579 | ||
469956e8 | 4580 | #if defined(CONFIG_NET) |
02d27d89 PB |
4581 | static int io_setup_async_msg(struct io_kiocb *req, |
4582 | struct io_async_msghdr *kmsg) | |
4583 | { | |
e8c2bc1f JA |
4584 | struct io_async_msghdr *async_msg = req->async_data; |
4585 | ||
4586 | if (async_msg) | |
02d27d89 | 4587 | return -EAGAIN; |
e8c2bc1f | 4588 | if (io_alloc_async_data(req)) { |
02d27d89 PB |
4589 | if (kmsg->iov != kmsg->fast_iov) |
4590 | kfree(kmsg->iov); | |
4591 | return -ENOMEM; | |
4592 | } | |
e8c2bc1f | 4593 | async_msg = req->async_data; |
02d27d89 | 4594 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4595 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
02d27d89 PB |
4596 | return -EAGAIN; |
4597 | } | |
4598 | ||
2ae523ed PB |
4599 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4600 | struct io_async_msghdr *iomsg) | |
4601 | { | |
4602 | iomsg->iov = iomsg->fast_iov; | |
4603 | iomsg->msg.msg_name = &iomsg->addr; | |
4604 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, | |
4605 | req->sr_msg.msg_flags, &iomsg->iov); | |
4606 | } | |
4607 | ||
3529d8c2 | 4608 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4609 | { |
e8c2bc1f | 4610 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4611 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4612 | int ret; |
03b1230c | 4613 | |
d2b6f48b PB |
4614 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4615 | return -EINVAL; | |
4616 | ||
e47293fd | 4617 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4618 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4619 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4620 | |
d8768362 JA |
4621 | #ifdef CONFIG_COMPAT |
4622 | if (req->ctx->compat) | |
4623 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4624 | #endif | |
4625 | ||
e8c2bc1f | 4626 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
3529d8c2 | 4627 | return 0; |
e8c2bc1f | 4628 | ret = io_sendmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4629 | if (!ret) |
4630 | req->flags |= REQ_F_NEED_CLEANUP; | |
4631 | return ret; | |
03b1230c JA |
4632 | } |
4633 | ||
229a7b63 JA |
4634 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
4635 | struct io_comp_state *cs) | |
aa1fa28f | 4636 | { |
6b754c8b | 4637 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4638 | struct socket *sock; |
7a7cacba | 4639 | unsigned flags; |
0fa03c62 JA |
4640 | int ret; |
4641 | ||
dba4a925 | 4642 | sock = sock_from_file(req->file); |
7a7cacba | 4643 | if (unlikely(!sock)) |
dba4a925 | 4644 | return -ENOTSOCK; |
3529d8c2 | 4645 | |
e8c2bc1f JA |
4646 | if (req->async_data) { |
4647 | kmsg = req->async_data; | |
4648 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4649 | /* if iov is set, it's allocated already */ |
4650 | if (!kmsg->iov) | |
4651 | kmsg->iov = kmsg->fast_iov; | |
4652 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4653 | } else { | |
4654 | ret = io_sendmsg_copy_hdr(req, &iomsg); | |
4655 | if (ret) | |
4656 | return ret; | |
4657 | kmsg = &iomsg; | |
0fa03c62 | 4658 | } |
0fa03c62 | 4659 | |
7a7cacba PB |
4660 | flags = req->sr_msg.msg_flags; |
4661 | if (flags & MSG_DONTWAIT) | |
4662 | req->flags |= REQ_F_NOWAIT; | |
4663 | else if (force_nonblock) | |
4664 | flags |= MSG_DONTWAIT; | |
e47293fd | 4665 | |
7a7cacba PB |
4666 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
4667 | if (force_nonblock && ret == -EAGAIN) | |
4668 | return io_setup_async_msg(req, kmsg); | |
4669 | if (ret == -ERESTARTSYS) | |
4670 | ret = -EINTR; | |
0fa03c62 | 4671 | |
6b754c8b | 4672 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4673 | kfree(kmsg->iov); |
99bc4c38 | 4674 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4675 | if (ret < 0) |
4676 | req_set_fail_links(req); | |
229a7b63 | 4677 | __io_req_complete(req, ret, 0, cs); |
5d17b4a4 | 4678 | return 0; |
03b1230c | 4679 | } |
aa1fa28f | 4680 | |
229a7b63 JA |
4681 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
4682 | struct io_comp_state *cs) | |
fddaface | 4683 | { |
7a7cacba PB |
4684 | struct io_sr_msg *sr = &req->sr_msg; |
4685 | struct msghdr msg; | |
4686 | struct iovec iov; | |
fddaface | 4687 | struct socket *sock; |
7a7cacba | 4688 | unsigned flags; |
fddaface JA |
4689 | int ret; |
4690 | ||
dba4a925 | 4691 | sock = sock_from_file(req->file); |
7a7cacba | 4692 | if (unlikely(!sock)) |
dba4a925 | 4693 | return -ENOTSOCK; |
fddaface | 4694 | |
7a7cacba PB |
4695 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4696 | if (unlikely(ret)) | |
14db8411 | 4697 | return ret; |
fddaface | 4698 | |
7a7cacba PB |
4699 | msg.msg_name = NULL; |
4700 | msg.msg_control = NULL; | |
4701 | msg.msg_controllen = 0; | |
4702 | msg.msg_namelen = 0; | |
fddaface | 4703 | |
7a7cacba PB |
4704 | flags = req->sr_msg.msg_flags; |
4705 | if (flags & MSG_DONTWAIT) | |
4706 | req->flags |= REQ_F_NOWAIT; | |
4707 | else if (force_nonblock) | |
4708 | flags |= MSG_DONTWAIT; | |
fddaface | 4709 | |
7a7cacba PB |
4710 | msg.msg_flags = flags; |
4711 | ret = sock_sendmsg(sock, &msg); | |
4712 | if (force_nonblock && ret == -EAGAIN) | |
4713 | return -EAGAIN; | |
4714 | if (ret == -ERESTARTSYS) | |
4715 | ret = -EINTR; | |
fddaface | 4716 | |
fddaface JA |
4717 | if (ret < 0) |
4718 | req_set_fail_links(req); | |
229a7b63 | 4719 | __io_req_complete(req, ret, 0, cs); |
fddaface | 4720 | return 0; |
fddaface JA |
4721 | } |
4722 | ||
1400e697 PB |
4723 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4724 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4725 | { |
4726 | struct io_sr_msg *sr = &req->sr_msg; | |
4727 | struct iovec __user *uiov; | |
4728 | size_t iov_len; | |
4729 | int ret; | |
4730 | ||
1400e697 PB |
4731 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4732 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4733 | if (ret) |
4734 | return ret; | |
4735 | ||
4736 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4737 | if (iov_len > 1) | |
4738 | return -EINVAL; | |
1400e697 | 4739 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4740 | return -EFAULT; |
1400e697 PB |
4741 | sr->len = iomsg->iov[0].iov_len; |
4742 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, | |
52de1fe1 | 4743 | sr->len); |
1400e697 | 4744 | iomsg->iov = NULL; |
52de1fe1 | 4745 | } else { |
89cd35c5 CH |
4746 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
4747 | &iomsg->iov, &iomsg->msg.msg_iter, | |
4748 | false); | |
52de1fe1 JA |
4749 | if (ret > 0) |
4750 | ret = 0; | |
4751 | } | |
4752 | ||
4753 | return ret; | |
4754 | } | |
4755 | ||
4756 | #ifdef CONFIG_COMPAT | |
4757 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4758 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4759 | { |
4760 | struct compat_msghdr __user *msg_compat; | |
4761 | struct io_sr_msg *sr = &req->sr_msg; | |
4762 | struct compat_iovec __user *uiov; | |
4763 | compat_uptr_t ptr; | |
4764 | compat_size_t len; | |
4765 | int ret; | |
4766 | ||
270a5940 | 4767 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4768 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4769 | &ptr, &len); |
4770 | if (ret) | |
4771 | return ret; | |
4772 | ||
4773 | uiov = compat_ptr(ptr); | |
4774 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4775 | compat_ssize_t clen; | |
4776 | ||
4777 | if (len > 1) | |
4778 | return -EINVAL; | |
4779 | if (!access_ok(uiov, sizeof(*uiov))) | |
4780 | return -EFAULT; | |
4781 | if (__get_user(clen, &uiov->iov_len)) | |
4782 | return -EFAULT; | |
4783 | if (clen < 0) | |
4784 | return -EINVAL; | |
2d280bc8 PB |
4785 | sr->len = clen; |
4786 | iomsg->iov[0].iov_len = clen; | |
1400e697 | 4787 | iomsg->iov = NULL; |
52de1fe1 | 4788 | } else { |
89cd35c5 CH |
4789 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
4790 | UIO_FASTIOV, &iomsg->iov, | |
4791 | &iomsg->msg.msg_iter, true); | |
52de1fe1 JA |
4792 | if (ret < 0) |
4793 | return ret; | |
4794 | } | |
4795 | ||
4796 | return 0; | |
4797 | } | |
4798 | #endif | |
4799 | ||
1400e697 PB |
4800 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4801 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4802 | { |
1400e697 PB |
4803 | iomsg->msg.msg_name = &iomsg->addr; |
4804 | iomsg->iov = iomsg->fast_iov; | |
52de1fe1 JA |
4805 | |
4806 | #ifdef CONFIG_COMPAT | |
4807 | if (req->ctx->compat) | |
1400e697 | 4808 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4809 | #endif |
52de1fe1 | 4810 | |
1400e697 | 4811 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4812 | } |
4813 | ||
bcda7baa | 4814 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4815 | bool needs_lock) |
bcda7baa JA |
4816 | { |
4817 | struct io_sr_msg *sr = &req->sr_msg; | |
4818 | struct io_buffer *kbuf; | |
4819 | ||
bcda7baa JA |
4820 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4821 | if (IS_ERR(kbuf)) | |
4822 | return kbuf; | |
4823 | ||
4824 | sr->kbuf = kbuf; | |
4825 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4826 | return kbuf; |
fddaface JA |
4827 | } |
4828 | ||
7fbb1b54 PB |
4829 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4830 | { | |
4831 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4832 | } | |
4833 | ||
3529d8c2 JA |
4834 | static int io_recvmsg_prep(struct io_kiocb *req, |
4835 | const struct io_uring_sqe *sqe) | |
aa1fa28f | 4836 | { |
e8c2bc1f | 4837 | struct io_async_msghdr *async_msg = req->async_data; |
e47293fd | 4838 | struct io_sr_msg *sr = &req->sr_msg; |
99bc4c38 | 4839 | int ret; |
3529d8c2 | 4840 | |
d2b6f48b PB |
4841 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4842 | return -EINVAL; | |
4843 | ||
3529d8c2 | 4844 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4845 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4846 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4847 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4848 | |
d8768362 JA |
4849 | #ifdef CONFIG_COMPAT |
4850 | if (req->ctx->compat) | |
4851 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4852 | #endif | |
4853 | ||
e8c2bc1f | 4854 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
5f798bea | 4855 | return 0; |
e8c2bc1f | 4856 | ret = io_recvmsg_copy_hdr(req, async_msg); |
99bc4c38 PB |
4857 | if (!ret) |
4858 | req->flags |= REQ_F_NEED_CLEANUP; | |
4859 | return ret; | |
aa1fa28f JA |
4860 | } |
4861 | ||
229a7b63 JA |
4862 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
4863 | struct io_comp_state *cs) | |
aa1fa28f | 4864 | { |
6b754c8b | 4865 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4866 | struct socket *sock; |
7fbb1b54 | 4867 | struct io_buffer *kbuf; |
7a7cacba | 4868 | unsigned flags; |
52de1fe1 | 4869 | int ret, cflags = 0; |
03b1230c | 4870 | |
dba4a925 | 4871 | sock = sock_from_file(req->file); |
7a7cacba | 4872 | if (unlikely(!sock)) |
dba4a925 | 4873 | return -ENOTSOCK; |
3529d8c2 | 4874 | |
e8c2bc1f JA |
4875 | if (req->async_data) { |
4876 | kmsg = req->async_data; | |
4877 | kmsg->msg.msg_name = &kmsg->addr; | |
7a7cacba PB |
4878 | /* if iov is set, it's allocated already */ |
4879 | if (!kmsg->iov) | |
4880 | kmsg->iov = kmsg->fast_iov; | |
4881 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
4882 | } else { | |
4883 | ret = io_recvmsg_copy_hdr(req, &iomsg); | |
4884 | if (ret) | |
681fda8d | 4885 | return ret; |
7a7cacba PB |
4886 | kmsg = &iomsg; |
4887 | } | |
03b1230c | 4888 | |
bc02ef33 | 4889 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4890 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4891 | if (IS_ERR(kbuf)) |
52de1fe1 | 4892 | return PTR_ERR(kbuf); |
7a7cacba PB |
4893 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
4894 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, | |
4895 | 1, req->sr_msg.len); | |
4896 | } | |
52de1fe1 | 4897 | |
7a7cacba PB |
4898 | flags = req->sr_msg.msg_flags; |
4899 | if (flags & MSG_DONTWAIT) | |
4900 | req->flags |= REQ_F_NOWAIT; | |
4901 | else if (force_nonblock) | |
4902 | flags |= MSG_DONTWAIT; | |
e47293fd | 4903 | |
7a7cacba PB |
4904 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4905 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4906 | if (force_nonblock && ret == -EAGAIN) |
4907 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4908 | if (ret == -ERESTARTSYS) |
4909 | ret = -EINTR; | |
03b1230c | 4910 | |
7fbb1b54 PB |
4911 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4912 | cflags = io_put_recv_kbuf(req); | |
6b754c8b | 4913 | if (kmsg->iov != kmsg->fast_iov) |
0b416c3e | 4914 | kfree(kmsg->iov); |
99bc4c38 | 4915 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4916 | if (ret < 0) |
4917 | req_set_fail_links(req); | |
229a7b63 | 4918 | __io_req_complete(req, ret, cflags, cs); |
03b1230c | 4919 | return 0; |
0fa03c62 | 4920 | } |
5d17b4a4 | 4921 | |
229a7b63 JA |
4922 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
4923 | struct io_comp_state *cs) | |
fddaface | 4924 | { |
6b754c8b | 4925 | struct io_buffer *kbuf; |
7a7cacba PB |
4926 | struct io_sr_msg *sr = &req->sr_msg; |
4927 | struct msghdr msg; | |
4928 | void __user *buf = sr->buf; | |
fddaface | 4929 | struct socket *sock; |
7a7cacba PB |
4930 | struct iovec iov; |
4931 | unsigned flags; | |
bcda7baa | 4932 | int ret, cflags = 0; |
fddaface | 4933 | |
dba4a925 | 4934 | sock = sock_from_file(req->file); |
7a7cacba | 4935 | if (unlikely(!sock)) |
dba4a925 | 4936 | return -ENOTSOCK; |
fddaface | 4937 | |
bc02ef33 | 4938 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4939 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4940 | if (IS_ERR(kbuf)) |
4941 | return PTR_ERR(kbuf); | |
7a7cacba | 4942 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4943 | } |
bcda7baa | 4944 | |
7a7cacba | 4945 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4946 | if (unlikely(ret)) |
4947 | goto out_free; | |
fddaface | 4948 | |
7a7cacba PB |
4949 | msg.msg_name = NULL; |
4950 | msg.msg_control = NULL; | |
4951 | msg.msg_controllen = 0; | |
4952 | msg.msg_namelen = 0; | |
4953 | msg.msg_iocb = NULL; | |
4954 | msg.msg_flags = 0; | |
fddaface | 4955 | |
7a7cacba PB |
4956 | flags = req->sr_msg.msg_flags; |
4957 | if (flags & MSG_DONTWAIT) | |
4958 | req->flags |= REQ_F_NOWAIT; | |
4959 | else if (force_nonblock) | |
4960 | flags |= MSG_DONTWAIT; | |
4961 | ||
4962 | ret = sock_recvmsg(sock, &msg, flags); | |
4963 | if (force_nonblock && ret == -EAGAIN) | |
4964 | return -EAGAIN; | |
4965 | if (ret == -ERESTARTSYS) | |
4966 | ret = -EINTR; | |
14c32eee | 4967 | out_free: |
7fbb1b54 PB |
4968 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4969 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4970 | if (ret < 0) |
4971 | req_set_fail_links(req); | |
229a7b63 | 4972 | __io_req_complete(req, ret, cflags, cs); |
fddaface | 4973 | return 0; |
fddaface JA |
4974 | } |
4975 | ||
3529d8c2 | 4976 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4977 | { |
8ed8d3c3 JA |
4978 | struct io_accept *accept = &req->accept; |
4979 | ||
14587a46 | 4980 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4981 | return -EINVAL; |
8042d6ce | 4982 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4983 | return -EINVAL; |
4984 | ||
d55e5f5b JA |
4985 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4986 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4987 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4988 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4989 | return 0; |
8ed8d3c3 | 4990 | } |
17f2fe35 | 4991 | |
229a7b63 JA |
4992 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
4993 | struct io_comp_state *cs) | |
8ed8d3c3 JA |
4994 | { |
4995 | struct io_accept *accept = &req->accept; | |
ac45abc0 | 4996 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4997 | int ret; |
4998 | ||
e697deed JX |
4999 | if (req->file->f_flags & O_NONBLOCK) |
5000 | req->flags |= REQ_F_NOWAIT; | |
5001 | ||
8ed8d3c3 | 5002 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
5003 | accept->addr_len, accept->flags, |
5004 | accept->nofile); | |
8ed8d3c3 | 5005 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 5006 | return -EAGAIN; |
ac45abc0 PB |
5007 | if (ret < 0) { |
5008 | if (ret == -ERESTARTSYS) | |
5009 | ret = -EINTR; | |
4e88d6e7 | 5010 | req_set_fail_links(req); |
ac45abc0 | 5011 | } |
229a7b63 | 5012 | __io_req_complete(req, ret, 0, cs); |
17f2fe35 | 5013 | return 0; |
8ed8d3c3 JA |
5014 | } |
5015 | ||
3529d8c2 | 5016 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5017 | { |
3529d8c2 | 5018 | struct io_connect *conn = &req->connect; |
e8c2bc1f | 5019 | struct io_async_connect *io = req->async_data; |
f499a021 | 5020 | |
14587a46 | 5021 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
5022 | return -EINVAL; |
5023 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
5024 | return -EINVAL; | |
5025 | ||
3529d8c2 JA |
5026 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5027 | conn->addr_len = READ_ONCE(sqe->addr2); | |
5028 | ||
5029 | if (!io) | |
5030 | return 0; | |
5031 | ||
5032 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
e8c2bc1f | 5033 | &io->address); |
f499a021 JA |
5034 | } |
5035 | ||
229a7b63 JA |
5036 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5037 | struct io_comp_state *cs) | |
f8e85cf2 | 5038 | { |
e8c2bc1f | 5039 | struct io_async_connect __io, *io; |
f8e85cf2 | 5040 | unsigned file_flags; |
3fbb51c1 | 5041 | int ret; |
f8e85cf2 | 5042 | |
e8c2bc1f JA |
5043 | if (req->async_data) { |
5044 | io = req->async_data; | |
f499a021 | 5045 | } else { |
3529d8c2 JA |
5046 | ret = move_addr_to_kernel(req->connect.addr, |
5047 | req->connect.addr_len, | |
e8c2bc1f | 5048 | &__io.address); |
f499a021 JA |
5049 | if (ret) |
5050 | goto out; | |
5051 | io = &__io; | |
5052 | } | |
5053 | ||
3fbb51c1 JA |
5054 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5055 | ||
e8c2bc1f | 5056 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5057 | req->connect.addr_len, file_flags); |
87f80d62 | 5058 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 5059 | if (req->async_data) |
b7bb4f7d | 5060 | return -EAGAIN; |
e8c2bc1f | 5061 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5062 | ret = -ENOMEM; |
5063 | goto out; | |
5064 | } | |
e8c2bc1f JA |
5065 | io = req->async_data; |
5066 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 5067 | return -EAGAIN; |
f499a021 | 5068 | } |
f8e85cf2 JA |
5069 | if (ret == -ERESTARTSYS) |
5070 | ret = -EINTR; | |
f499a021 | 5071 | out: |
4e88d6e7 JA |
5072 | if (ret < 0) |
5073 | req_set_fail_links(req); | |
229a7b63 | 5074 | __io_req_complete(req, ret, 0, cs); |
f8e85cf2 | 5075 | return 0; |
469956e8 Y |
5076 | } |
5077 | #else /* !CONFIG_NET */ | |
5078 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5079 | { | |
f8e85cf2 | 5080 | return -EOPNOTSUPP; |
f8e85cf2 JA |
5081 | } |
5082 | ||
1e16c2f9 RD |
5083 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
5084 | struct io_comp_state *cs) | |
469956e8 Y |
5085 | { |
5086 | return -EOPNOTSUPP; | |
5087 | } | |
5088 | ||
1e16c2f9 RD |
5089 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
5090 | struct io_comp_state *cs) | |
469956e8 Y |
5091 | { |
5092 | return -EOPNOTSUPP; | |
5093 | } | |
5094 | ||
5095 | static int io_recvmsg_prep(struct io_kiocb *req, | |
5096 | const struct io_uring_sqe *sqe) | |
5097 | { | |
5098 | return -EOPNOTSUPP; | |
5099 | } | |
5100 | ||
1e16c2f9 RD |
5101 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
5102 | struct io_comp_state *cs) | |
469956e8 Y |
5103 | { |
5104 | return -EOPNOTSUPP; | |
5105 | } | |
5106 | ||
1e16c2f9 RD |
5107 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
5108 | struct io_comp_state *cs) | |
469956e8 Y |
5109 | { |
5110 | return -EOPNOTSUPP; | |
5111 | } | |
5112 | ||
5113 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5114 | { | |
5115 | return -EOPNOTSUPP; | |
5116 | } | |
5117 | ||
1e16c2f9 RD |
5118 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
5119 | struct io_comp_state *cs) | |
469956e8 Y |
5120 | { |
5121 | return -EOPNOTSUPP; | |
5122 | } | |
ce593a6c | 5123 | |
469956e8 Y |
5124 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5125 | { | |
5126 | return -EOPNOTSUPP; | |
5127 | } | |
5128 | ||
1e16c2f9 RD |
5129 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
5130 | struct io_comp_state *cs) | |
469956e8 | 5131 | { |
f8e85cf2 | 5132 | return -EOPNOTSUPP; |
ce593a6c | 5133 | } |
469956e8 | 5134 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5135 | |
d7718a9d JA |
5136 | struct io_poll_table { |
5137 | struct poll_table_struct pt; | |
5138 | struct io_kiocb *req; | |
5139 | int error; | |
5140 | }; | |
ce593a6c | 5141 | |
d7718a9d JA |
5142 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5143 | __poll_t mask, task_work_func_t func) | |
5144 | { | |
aa96bf8a | 5145 | int ret; |
d7718a9d JA |
5146 | |
5147 | /* for instances that support it check for an event match first: */ | |
5148 | if (mask && !(mask & poll->events)) | |
5149 | return 0; | |
5150 | ||
5151 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5152 | ||
5153 | list_del_init(&poll->wait.entry); | |
5154 | ||
d7718a9d JA |
5155 | req->result = mask; |
5156 | init_task_work(&req->task_work, func); | |
6d816e08 JA |
5157 | percpu_ref_get(&req->ctx->refs); |
5158 | ||
d7718a9d | 5159 | /* |
e3aabf95 JA |
5160 | * If this fails, then the task is exiting. When a task exits, the |
5161 | * work gets canceled, so just cancel this request as well instead | |
5162 | * of executing it. We can't safely execute it anyway, as we may not | |
5163 | * have the needed state needed for it anyway. | |
d7718a9d | 5164 | */ |
355fb9e2 | 5165 | ret = io_req_task_work_add(req); |
aa96bf8a | 5166 | if (unlikely(ret)) { |
e3aabf95 | 5167 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 5168 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 5169 | } |
d7718a9d JA |
5170 | return 1; |
5171 | } | |
5172 | ||
74ce6ce4 JA |
5173 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5174 | __acquires(&req->ctx->completion_lock) | |
5175 | { | |
5176 | struct io_ring_ctx *ctx = req->ctx; | |
5177 | ||
5178 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5179 | struct poll_table_struct pt = { ._key = poll->events }; | |
5180 | ||
5181 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5182 | } | |
5183 | ||
5184 | spin_lock_irq(&ctx->completion_lock); | |
5185 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
5186 | add_wait_queue(poll->head, &poll->wait); | |
5187 | return true; | |
5188 | } | |
5189 | ||
5190 | return false; | |
5191 | } | |
5192 | ||
d4e7cd36 | 5193 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5194 | { |
e8c2bc1f | 5195 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5196 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5197 | return req->async_data; |
d4e7cd36 JA |
5198 | return req->apoll->double_poll; |
5199 | } | |
5200 | ||
5201 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5202 | { | |
5203 | if (req->opcode == IORING_OP_POLL_ADD) | |
5204 | return &req->poll; | |
5205 | return &req->apoll->poll; | |
5206 | } | |
5207 | ||
5208 | static void io_poll_remove_double(struct io_kiocb *req) | |
5209 | { | |
5210 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5211 | |
5212 | lockdep_assert_held(&req->ctx->completion_lock); | |
5213 | ||
5214 | if (poll && poll->head) { | |
5215 | struct wait_queue_head *head = poll->head; | |
5216 | ||
5217 | spin_lock(&head->lock); | |
5218 | list_del_init(&poll->wait.entry); | |
5219 | if (poll->wait.private) | |
5220 | refcount_dec(&req->refs); | |
5221 | poll->head = NULL; | |
5222 | spin_unlock(&head->lock); | |
5223 | } | |
5224 | } | |
5225 | ||
5226 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
5227 | { | |
5228 | struct io_ring_ctx *ctx = req->ctx; | |
5229 | ||
d4e7cd36 | 5230 | io_poll_remove_double(req); |
18bceab1 JA |
5231 | req->poll.done = true; |
5232 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
5233 | io_commit_cqring(ctx); | |
5234 | } | |
5235 | ||
dd221f46 | 5236 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 5237 | { |
dd221f46 | 5238 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 5239 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 5240 | struct io_kiocb *nxt; |
18bceab1 JA |
5241 | |
5242 | if (io_poll_rewait(req, &req->poll)) { | |
5243 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
5244 | } else { |
5245 | hash_del(&req->hash_node); | |
5246 | io_poll_complete(req, req->result, 0); | |
5247 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 5248 | |
dd221f46 PB |
5249 | nxt = io_put_req_find_next(req); |
5250 | io_cqring_ev_posted(ctx); | |
5251 | if (nxt) | |
5252 | __io_req_task_submit(nxt); | |
5253 | } | |
18bceab1 | 5254 | |
6d816e08 | 5255 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
5256 | } |
5257 | ||
5258 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5259 | int sync, void *key) | |
5260 | { | |
5261 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5262 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
5263 | __poll_t mask = key_to_poll(key); |
5264 | ||
5265 | /* for instances that support it check for an event match first: */ | |
5266 | if (mask && !(mask & poll->events)) | |
5267 | return 0; | |
5268 | ||
8706e04e JA |
5269 | list_del_init(&wait->entry); |
5270 | ||
807abcb0 | 5271 | if (poll && poll->head) { |
18bceab1 JA |
5272 | bool done; |
5273 | ||
807abcb0 JA |
5274 | spin_lock(&poll->head->lock); |
5275 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5276 | if (!done) |
807abcb0 | 5277 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5278 | /* make sure double remove sees this as being gone */ |
5279 | wait->private = NULL; | |
807abcb0 | 5280 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5281 | if (!done) { |
5282 | /* use wait func handler, so it matches the rq type */ | |
5283 | poll->wait.func(&poll->wait, mode, sync, key); | |
5284 | } | |
18bceab1 JA |
5285 | } |
5286 | refcount_dec(&req->refs); | |
5287 | return 1; | |
5288 | } | |
5289 | ||
5290 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5291 | wait_queue_func_t wake_func) | |
5292 | { | |
5293 | poll->head = NULL; | |
5294 | poll->done = false; | |
5295 | poll->canceled = false; | |
5296 | poll->events = events; | |
5297 | INIT_LIST_HEAD(&poll->wait.entry); | |
5298 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5299 | } | |
5300 | ||
5301 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5302 | struct wait_queue_head *head, |
5303 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5304 | { |
5305 | struct io_kiocb *req = pt->req; | |
5306 | ||
5307 | /* | |
5308 | * If poll->head is already set, it's because the file being polled | |
5309 | * uses multiple waitqueues for poll handling (eg one for read, one | |
5310 | * for write). Setup a separate io_poll_iocb if this happens. | |
5311 | */ | |
5312 | if (unlikely(poll->head)) { | |
58852d4d PB |
5313 | struct io_poll_iocb *poll_one = poll; |
5314 | ||
18bceab1 | 5315 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5316 | if (*poll_ptr) { |
18bceab1 JA |
5317 | pt->error = -EINVAL; |
5318 | return; | |
5319 | } | |
5320 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5321 | if (!poll) { | |
5322 | pt->error = -ENOMEM; | |
5323 | return; | |
5324 | } | |
58852d4d | 5325 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5326 | refcount_inc(&req->refs); |
5327 | poll->wait.private = req; | |
807abcb0 | 5328 | *poll_ptr = poll; |
18bceab1 JA |
5329 | } |
5330 | ||
5331 | pt->error = 0; | |
5332 | poll->head = head; | |
a31eb4a2 JX |
5333 | |
5334 | if (poll->events & EPOLLEXCLUSIVE) | |
5335 | add_wait_queue_exclusive(head, &poll->wait); | |
5336 | else | |
5337 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5338 | } |
5339 | ||
5340 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5341 | struct poll_table_struct *p) | |
5342 | { | |
5343 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5344 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5345 | |
807abcb0 | 5346 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5347 | } |
5348 | ||
d7718a9d JA |
5349 | static void io_async_task_func(struct callback_head *cb) |
5350 | { | |
5351 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5352 | struct async_poll *apoll = req->apoll; | |
5353 | struct io_ring_ctx *ctx = req->ctx; | |
5354 | ||
5355 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5356 | ||
74ce6ce4 | 5357 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5358 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5359 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5360 | return; |
d7718a9d JA |
5361 | } |
5362 | ||
31067255 | 5363 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5364 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5365 | hash_del(&req->hash_node); |
2bae047e | 5366 | |
d4e7cd36 | 5367 | io_poll_remove_double(req); |
74ce6ce4 JA |
5368 | spin_unlock_irq(&ctx->completion_lock); |
5369 | ||
0be0b0e3 PB |
5370 | if (!READ_ONCE(apoll->poll.canceled)) |
5371 | __io_req_task_submit(req); | |
5372 | else | |
5373 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5374 | |
6d816e08 | 5375 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5376 | kfree(apoll->double_poll); |
31067255 | 5377 | kfree(apoll); |
d7718a9d JA |
5378 | } |
5379 | ||
5380 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5381 | void *key) | |
5382 | { | |
5383 | struct io_kiocb *req = wait->private; | |
5384 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5385 | ||
5386 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5387 | key_to_poll(key)); | |
5388 | ||
5389 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5390 | } | |
5391 | ||
5392 | static void io_poll_req_insert(struct io_kiocb *req) | |
5393 | { | |
5394 | struct io_ring_ctx *ctx = req->ctx; | |
5395 | struct hlist_head *list; | |
5396 | ||
5397 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5398 | hlist_add_head(&req->hash_node, list); | |
5399 | } | |
5400 | ||
5401 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5402 | struct io_poll_iocb *poll, | |
5403 | struct io_poll_table *ipt, __poll_t mask, | |
5404 | wait_queue_func_t wake_func) | |
5405 | __acquires(&ctx->completion_lock) | |
5406 | { | |
5407 | struct io_ring_ctx *ctx = req->ctx; | |
5408 | bool cancel = false; | |
5409 | ||
4d52f338 | 5410 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5411 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5412 | poll->file = req->file; |
18bceab1 | 5413 | poll->wait.private = req; |
d7718a9d JA |
5414 | |
5415 | ipt->pt._key = mask; | |
5416 | ipt->req = req; | |
5417 | ipt->error = -EINVAL; | |
5418 | ||
d7718a9d JA |
5419 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5420 | ||
5421 | spin_lock_irq(&ctx->completion_lock); | |
5422 | if (likely(poll->head)) { | |
5423 | spin_lock(&poll->head->lock); | |
5424 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5425 | if (ipt->error) | |
5426 | cancel = true; | |
5427 | ipt->error = 0; | |
5428 | mask = 0; | |
5429 | } | |
5430 | if (mask || ipt->error) | |
5431 | list_del_init(&poll->wait.entry); | |
5432 | else if (cancel) | |
5433 | WRITE_ONCE(poll->canceled, true); | |
5434 | else if (!poll->done) /* actually waiting for an event */ | |
5435 | io_poll_req_insert(req); | |
5436 | spin_unlock(&poll->head->lock); | |
5437 | } | |
5438 | ||
5439 | return mask; | |
5440 | } | |
5441 | ||
5442 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5443 | { | |
5444 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5445 | struct io_ring_ctx *ctx = req->ctx; | |
5446 | struct async_poll *apoll; | |
5447 | struct io_poll_table ipt; | |
5448 | __poll_t mask, ret; | |
9dab14b8 | 5449 | int rw; |
d7718a9d JA |
5450 | |
5451 | if (!req->file || !file_can_poll(req->file)) | |
5452 | return false; | |
24c74678 | 5453 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5454 | return false; |
9dab14b8 JA |
5455 | if (def->pollin) |
5456 | rw = READ; | |
5457 | else if (def->pollout) | |
5458 | rw = WRITE; | |
5459 | else | |
5460 | return false; | |
5461 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5462 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5463 | return false; |
5464 | ||
5465 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5466 | if (unlikely(!apoll)) | |
5467 | return false; | |
807abcb0 | 5468 | apoll->double_poll = NULL; |
d7718a9d JA |
5469 | |
5470 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5471 | req->apoll = apoll; |
d7718a9d | 5472 | |
8755d97a | 5473 | mask = 0; |
d7718a9d | 5474 | if (def->pollin) |
8755d97a | 5475 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5476 | if (def->pollout) |
5477 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5478 | |
5479 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5480 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5481 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5482 | mask &= ~POLLIN; | |
5483 | ||
d7718a9d JA |
5484 | mask |= POLLERR | POLLPRI; |
5485 | ||
5486 | ipt.pt._qproc = io_async_queue_proc; | |
5487 | ||
5488 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5489 | io_async_wake); | |
a36da65c | 5490 | if (ret || ipt.error) { |
d4e7cd36 | 5491 | io_poll_remove_double(req); |
d7718a9d | 5492 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5493 | kfree(apoll->double_poll); |
d7718a9d JA |
5494 | kfree(apoll); |
5495 | return false; | |
5496 | } | |
5497 | spin_unlock_irq(&ctx->completion_lock); | |
5498 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5499 | apoll->poll.events); | |
5500 | return true; | |
5501 | } | |
5502 | ||
5503 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5504 | struct io_poll_iocb *poll) | |
221c5eb2 | 5505 | { |
b41e9852 | 5506 | bool do_complete = false; |
221c5eb2 JA |
5507 | |
5508 | spin_lock(&poll->head->lock); | |
5509 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5510 | if (!list_empty(&poll->wait.entry)) { |
5511 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5512 | do_complete = true; |
221c5eb2 JA |
5513 | } |
5514 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5515 | hash_del(&req->hash_node); |
d7718a9d JA |
5516 | return do_complete; |
5517 | } | |
5518 | ||
5519 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5520 | { | |
5521 | bool do_complete; | |
5522 | ||
d4e7cd36 JA |
5523 | io_poll_remove_double(req); |
5524 | ||
d7718a9d JA |
5525 | if (req->opcode == IORING_OP_POLL_ADD) { |
5526 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5527 | } else { | |
3bfa5bcb JA |
5528 | struct async_poll *apoll = req->apoll; |
5529 | ||
d7718a9d | 5530 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5531 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5532 | if (do_complete) { | |
d7718a9d | 5533 | io_put_req(req); |
807abcb0 | 5534 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5535 | kfree(apoll); |
5536 | } | |
b1f573bd XW |
5537 | } |
5538 | ||
b41e9852 JA |
5539 | if (do_complete) { |
5540 | io_cqring_fill_event(req, -ECANCELED); | |
5541 | io_commit_cqring(req->ctx); | |
f254ac04 | 5542 | req_set_fail_links(req); |
216578e5 | 5543 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5544 | } |
5545 | ||
5546 | return do_complete; | |
221c5eb2 JA |
5547 | } |
5548 | ||
76e1b642 JA |
5549 | /* |
5550 | * Returns true if we found and killed one or more poll requests | |
5551 | */ | |
6b81928d PB |
5552 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5553 | struct files_struct *files) | |
221c5eb2 | 5554 | { |
78076bb6 | 5555 | struct hlist_node *tmp; |
221c5eb2 | 5556 | struct io_kiocb *req; |
8e2e1faf | 5557 | int posted = 0, i; |
221c5eb2 JA |
5558 | |
5559 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5560 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5561 | struct hlist_head *list; | |
5562 | ||
5563 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5564 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5565 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5566 | posted += io_poll_remove_one(req); |
5567 | } | |
221c5eb2 JA |
5568 | } |
5569 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5570 | |
8e2e1faf JA |
5571 | if (posted) |
5572 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5573 | |
5574 | return posted != 0; | |
221c5eb2 JA |
5575 | } |
5576 | ||
47f46768 JA |
5577 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5578 | { | |
78076bb6 | 5579 | struct hlist_head *list; |
47f46768 JA |
5580 | struct io_kiocb *req; |
5581 | ||
78076bb6 JA |
5582 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5583 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5584 | if (sqe_addr != req->user_data) |
5585 | continue; | |
5586 | if (io_poll_remove_one(req)) | |
eac406c6 | 5587 | return 0; |
b41e9852 | 5588 | return -EALREADY; |
47f46768 JA |
5589 | } |
5590 | ||
5591 | return -ENOENT; | |
5592 | } | |
5593 | ||
3529d8c2 JA |
5594 | static int io_poll_remove_prep(struct io_kiocb *req, |
5595 | const struct io_uring_sqe *sqe) | |
0969e783 | 5596 | { |
0969e783 JA |
5597 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5598 | return -EINVAL; | |
5599 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5600 | sqe->poll_events) | |
5601 | return -EINVAL; | |
5602 | ||
018043be | 5603 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5604 | return 0; |
5605 | } | |
5606 | ||
221c5eb2 JA |
5607 | /* |
5608 | * Find a running poll command that matches one specified in sqe->addr, | |
5609 | * and remove it if found. | |
5610 | */ | |
fc4df999 | 5611 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
5612 | { |
5613 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5614 | int ret; |
221c5eb2 | 5615 | |
221c5eb2 | 5616 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5617 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5618 | spin_unlock_irq(&ctx->completion_lock); |
5619 | ||
4e88d6e7 JA |
5620 | if (ret < 0) |
5621 | req_set_fail_links(req); | |
e1e16097 | 5622 | io_req_complete(req, ret); |
221c5eb2 JA |
5623 | return 0; |
5624 | } | |
5625 | ||
221c5eb2 JA |
5626 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5627 | void *key) | |
5628 | { | |
c2f2eb7d JA |
5629 | struct io_kiocb *req = wait->private; |
5630 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5631 | |
d7718a9d | 5632 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5633 | } |
5634 | ||
221c5eb2 JA |
5635 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5636 | struct poll_table_struct *p) | |
5637 | { | |
5638 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5639 | ||
e8c2bc1f | 5640 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5641 | } |
5642 | ||
3529d8c2 | 5643 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5644 | { |
5645 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5646 | u32 events; |
221c5eb2 JA |
5647 | |
5648 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5649 | return -EINVAL; | |
5650 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5651 | return -EINVAL; | |
5652 | ||
5769a351 JX |
5653 | events = READ_ONCE(sqe->poll32_events); |
5654 | #ifdef __BIG_ENDIAN | |
5655 | events = swahw32(events); | |
5656 | #endif | |
a31eb4a2 JX |
5657 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5658 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5659 | return 0; |
5660 | } | |
5661 | ||
014db007 | 5662 | static int io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5663 | { |
5664 | struct io_poll_iocb *poll = &req->poll; | |
5665 | struct io_ring_ctx *ctx = req->ctx; | |
5666 | struct io_poll_table ipt; | |
0969e783 | 5667 | __poll_t mask; |
0969e783 | 5668 | |
d7718a9d | 5669 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5670 | |
d7718a9d JA |
5671 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5672 | io_poll_wake); | |
221c5eb2 | 5673 | |
8c838788 | 5674 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5675 | ipt.error = 0; |
b0dd8a41 | 5676 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5677 | } |
221c5eb2 JA |
5678 | spin_unlock_irq(&ctx->completion_lock); |
5679 | ||
8c838788 JA |
5680 | if (mask) { |
5681 | io_cqring_ev_posted(ctx); | |
014db007 | 5682 | io_put_req(req); |
221c5eb2 | 5683 | } |
8c838788 | 5684 | return ipt.error; |
221c5eb2 JA |
5685 | } |
5686 | ||
5262f567 JA |
5687 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5688 | { | |
ad8a48ac JA |
5689 | struct io_timeout_data *data = container_of(timer, |
5690 | struct io_timeout_data, timer); | |
5691 | struct io_kiocb *req = data->req; | |
5692 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5693 | unsigned long flags; |
5694 | ||
5262f567 | 5695 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5696 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5697 | atomic_set(&req->ctx->cq_timeouts, |
5698 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5699 | ||
78e19bbe | 5700 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5701 | io_commit_cqring(ctx); |
5702 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5703 | ||
5704 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5705 | req_set_fail_links(req); |
5262f567 JA |
5706 | io_put_req(req); |
5707 | return HRTIMER_NORESTART; | |
5708 | } | |
5709 | ||
fbd15848 PB |
5710 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5711 | __u64 user_data) | |
f254ac04 | 5712 | { |
fbd15848 | 5713 | struct io_timeout_data *io; |
47f46768 JA |
5714 | struct io_kiocb *req; |
5715 | int ret = -ENOENT; | |
f254ac04 | 5716 | |
135fcde8 | 5717 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5718 | if (user_data == req->user_data) { |
47f46768 JA |
5719 | ret = 0; |
5720 | break; | |
5721 | } | |
5722 | } | |
5723 | ||
5724 | if (ret == -ENOENT) | |
fbd15848 PB |
5725 | return ERR_PTR(ret); |
5726 | ||
5727 | io = req->async_data; | |
e8c2bc1f | 5728 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5729 | if (ret == -1) |
fbd15848 | 5730 | return ERR_PTR(-EALREADY); |
a71976f3 | 5731 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5732 | return req; |
5733 | } | |
47f46768 | 5734 | |
fbd15848 PB |
5735 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5736 | { | |
5737 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5738 | ||
5739 | if (IS_ERR(req)) | |
5740 | return PTR_ERR(req); | |
f254ac04 JA |
5741 | |
5742 | req_set_fail_links(req); | |
f254ac04 | 5743 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5744 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5745 | return 0; |
5746 | } | |
5747 | ||
9c8e11b3 PB |
5748 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5749 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5750 | { |
9c8e11b3 PB |
5751 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5752 | struct io_timeout_data *data; | |
47f46768 | 5753 | |
9c8e11b3 PB |
5754 | if (IS_ERR(req)) |
5755 | return PTR_ERR(req); | |
47f46768 | 5756 | |
9c8e11b3 PB |
5757 | req->timeout.off = 0; /* noseq */ |
5758 | data = req->async_data; | |
5759 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5760 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5761 | data->timer.function = io_timeout_fn; | |
5762 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5763 | return 0; | |
47f46768 JA |
5764 | } |
5765 | ||
3529d8c2 JA |
5766 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5767 | const struct io_uring_sqe *sqe) | |
b29472ee | 5768 | { |
9c8e11b3 PB |
5769 | struct io_timeout_rem *tr = &req->timeout_rem; |
5770 | ||
b29472ee JA |
5771 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5772 | return -EINVAL; | |
61710e43 DA |
5773 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5774 | return -EINVAL; | |
9c8e11b3 | 5775 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5776 | return -EINVAL; |
5777 | ||
9c8e11b3 PB |
5778 | tr->addr = READ_ONCE(sqe->addr); |
5779 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5780 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5781 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5782 | return -EINVAL; | |
5783 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5784 | return -EFAULT; | |
5785 | } else if (tr->flags) { | |
5786 | /* timeout removal doesn't support flags */ | |
b29472ee | 5787 | return -EINVAL; |
9c8e11b3 | 5788 | } |
b29472ee | 5789 | |
b29472ee JA |
5790 | return 0; |
5791 | } | |
5792 | ||
8662daec PB |
5793 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5794 | { | |
5795 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5796 | : HRTIMER_MODE_REL; | |
5797 | } | |
5798 | ||
11365043 JA |
5799 | /* |
5800 | * Remove or update an existing timeout command | |
5801 | */ | |
fc4df999 | 5802 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 | 5803 | { |
9c8e11b3 | 5804 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5805 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5806 | int ret; |
11365043 | 5807 | |
11365043 | 5808 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5809 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5810 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5811 | else |
5812 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5813 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5814 | |
47f46768 | 5815 | io_cqring_fill_event(req, ret); |
11365043 JA |
5816 | io_commit_cqring(ctx); |
5817 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5818 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5819 | if (ret < 0) |
5820 | req_set_fail_links(req); | |
ec9c02ad | 5821 | io_put_req(req); |
11365043 | 5822 | return 0; |
5262f567 JA |
5823 | } |
5824 | ||
3529d8c2 | 5825 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5826 | bool is_timeout_link) |
5262f567 | 5827 | { |
ad8a48ac | 5828 | struct io_timeout_data *data; |
a41525ab | 5829 | unsigned flags; |
56080b02 | 5830 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5831 | |
ad8a48ac | 5832 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5833 | return -EINVAL; |
ad8a48ac | 5834 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5835 | return -EINVAL; |
56080b02 | 5836 | if (off && is_timeout_link) |
2d28390a | 5837 | return -EINVAL; |
a41525ab JA |
5838 | flags = READ_ONCE(sqe->timeout_flags); |
5839 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5840 | return -EINVAL; |
bdf20073 | 5841 | |
bfe68a22 | 5842 | req->timeout.off = off; |
26a61679 | 5843 | |
e8c2bc1f | 5844 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5845 | return -ENOMEM; |
5846 | ||
e8c2bc1f | 5847 | data = req->async_data; |
ad8a48ac | 5848 | data->req = req; |
ad8a48ac JA |
5849 | |
5850 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5851 | return -EFAULT; |
5852 | ||
8662daec | 5853 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5854 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5855 | return 0; | |
5856 | } | |
5857 | ||
fc4df999 | 5858 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac | 5859 | { |
ad8a48ac | 5860 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5861 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5862 | struct list_head *entry; |
bfe68a22 | 5863 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5864 | |
733f5c95 | 5865 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5866 | |
5262f567 JA |
5867 | /* |
5868 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5869 | * timeout event to be satisfied. If it isn't set, then this is |
5870 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5871 | */ |
8eb7e2d0 | 5872 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5873 | entry = ctx->timeout_list.prev; |
5874 | goto add; | |
5875 | } | |
5262f567 | 5876 | |
bfe68a22 PB |
5877 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5878 | req->timeout.target_seq = tail + off; | |
5262f567 | 5879 | |
f010505b MDG |
5880 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5881 | * This is safe because ->completion_lock is held, and submissions | |
5882 | * and completions are never mixed in the same ->completion_lock section. | |
5883 | */ | |
5884 | ctx->cq_last_tm_flush = tail; | |
5885 | ||
5262f567 JA |
5886 | /* |
5887 | * Insertion sort, ensuring the first entry in the list is always | |
5888 | * the one we need first. | |
5889 | */ | |
5262f567 | 5890 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5891 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5892 | timeout.list); | |
5262f567 | 5893 | |
8eb7e2d0 | 5894 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5895 | continue; |
bfe68a22 PB |
5896 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5897 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5898 | break; |
5899 | } | |
93bd25bb | 5900 | add: |
135fcde8 | 5901 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5902 | data->timer.function = io_timeout_fn; |
5903 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5904 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5905 | return 0; |
5906 | } | |
5262f567 | 5907 | |
62755e35 JA |
5908 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5909 | { | |
5910 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5911 | ||
5912 | return req->user_data == (unsigned long) data; | |
5913 | } | |
5914 | ||
e977d6d3 | 5915 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 5916 | { |
62755e35 | 5917 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5918 | int ret = 0; |
5919 | ||
4f26bda1 | 5920 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
62755e35 JA |
5921 | switch (cancel_ret) { |
5922 | case IO_WQ_CANCEL_OK: | |
5923 | ret = 0; | |
5924 | break; | |
5925 | case IO_WQ_CANCEL_RUNNING: | |
5926 | ret = -EALREADY; | |
5927 | break; | |
5928 | case IO_WQ_CANCEL_NOTFOUND: | |
5929 | ret = -ENOENT; | |
5930 | break; | |
5931 | } | |
5932 | ||
e977d6d3 JA |
5933 | return ret; |
5934 | } | |
5935 | ||
47f46768 JA |
5936 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5937 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5938 | int success_ret) |
47f46768 JA |
5939 | { |
5940 | unsigned long flags; | |
5941 | int ret; | |
5942 | ||
5943 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
5944 | if (ret != -ENOENT) { | |
5945 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5946 | goto done; | |
5947 | } | |
5948 | ||
5949 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5950 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5951 | if (ret != -ENOENT) | |
5952 | goto done; | |
5953 | ret = io_poll_cancel(ctx, sqe_addr); | |
5954 | done: | |
b0dd8a41 JA |
5955 | if (!ret) |
5956 | ret = success_ret; | |
47f46768 JA |
5957 | io_cqring_fill_event(req, ret); |
5958 | io_commit_cqring(ctx); | |
5959 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5960 | io_cqring_ev_posted(ctx); | |
5961 | ||
4e88d6e7 JA |
5962 | if (ret < 0) |
5963 | req_set_fail_links(req); | |
014db007 | 5964 | io_put_req(req); |
47f46768 JA |
5965 | } |
5966 | ||
3529d8c2 JA |
5967 | static int io_async_cancel_prep(struct io_kiocb *req, |
5968 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5969 | { |
fbf23849 | 5970 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5971 | return -EINVAL; |
61710e43 DA |
5972 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5973 | return -EINVAL; | |
5974 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5975 | return -EINVAL; |
5976 | ||
fbf23849 JA |
5977 | req->cancel.addr = READ_ONCE(sqe->addr); |
5978 | return 0; | |
5979 | } | |
5980 | ||
014db007 | 5981 | static int io_async_cancel(struct io_kiocb *req) |
fbf23849 JA |
5982 | { |
5983 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5984 | |
014db007 | 5985 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5986 | return 0; |
5987 | } | |
5988 | ||
269bbe5f | 5989 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5990 | const struct io_uring_sqe *sqe) |
5991 | { | |
6ca56f84 JA |
5992 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5993 | return -EINVAL; | |
61710e43 DA |
5994 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5995 | return -EINVAL; | |
5996 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5997 | return -EINVAL; |
5998 | ||
269bbe5f BM |
5999 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6000 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6001 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6002 | return -EINVAL; |
269bbe5f | 6003 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6004 | return 0; |
6005 | } | |
6006 | ||
229a7b63 JA |
6007 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
6008 | struct io_comp_state *cs) | |
fbf23849 JA |
6009 | { |
6010 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 6011 | struct io_uring_rsrc_update up; |
05f3fb3c | 6012 | int ret; |
fbf23849 | 6013 | |
f86cd20c | 6014 | if (force_nonblock) |
05f3fb3c | 6015 | return -EAGAIN; |
05f3fb3c | 6016 | |
269bbe5f BM |
6017 | up.offset = req->rsrc_update.offset; |
6018 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
6019 | |
6020 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 6021 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6022 | mutex_unlock(&ctx->uring_lock); |
6023 | ||
6024 | if (ret < 0) | |
6025 | req_set_fail_links(req); | |
229a7b63 | 6026 | __io_req_complete(req, ret, 0, cs); |
5262f567 JA |
6027 | return 0; |
6028 | } | |
6029 | ||
bfe76559 | 6030 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6031 | { |
d625c6ee | 6032 | switch (req->opcode) { |
e781573e | 6033 | case IORING_OP_NOP: |
bfe76559 | 6034 | return 0; |
f67676d1 JA |
6035 | case IORING_OP_READV: |
6036 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6037 | case IORING_OP_READ: |
bfe76559 | 6038 | return io_read_prep(req, sqe); |
f67676d1 JA |
6039 | case IORING_OP_WRITEV: |
6040 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6041 | case IORING_OP_WRITE: |
bfe76559 | 6042 | return io_write_prep(req, sqe); |
0969e783 | 6043 | case IORING_OP_POLL_ADD: |
bfe76559 | 6044 | return io_poll_add_prep(req, sqe); |
0969e783 | 6045 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 6046 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 6047 | case IORING_OP_FSYNC: |
bfe76559 | 6048 | return io_prep_fsync(req, sqe); |
8ed8d3c3 | 6049 | case IORING_OP_SYNC_FILE_RANGE: |
bfe76559 | 6050 | return io_prep_sfr(req, sqe); |
03b1230c | 6051 | case IORING_OP_SENDMSG: |
fddaface | 6052 | case IORING_OP_SEND: |
bfe76559 | 6053 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6054 | case IORING_OP_RECVMSG: |
fddaface | 6055 | case IORING_OP_RECV: |
bfe76559 | 6056 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6057 | case IORING_OP_CONNECT: |
bfe76559 | 6058 | return io_connect_prep(req, sqe); |
2d28390a | 6059 | case IORING_OP_TIMEOUT: |
bfe76559 | 6060 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6061 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6062 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6063 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6064 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6065 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6066 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6067 | case IORING_OP_ACCEPT: |
bfe76559 | 6068 | return io_accept_prep(req, sqe); |
d63d1b5e | 6069 | case IORING_OP_FALLOCATE: |
bfe76559 | 6070 | return io_fallocate_prep(req, sqe); |
15b71abe | 6071 | case IORING_OP_OPENAT: |
bfe76559 | 6072 | return io_openat_prep(req, sqe); |
b5dba59e | 6073 | case IORING_OP_CLOSE: |
bfe76559 | 6074 | return io_close_prep(req, sqe); |
05f3fb3c | 6075 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6076 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6077 | case IORING_OP_STATX: |
bfe76559 | 6078 | return io_statx_prep(req, sqe); |
4840e418 | 6079 | case IORING_OP_FADVISE: |
bfe76559 | 6080 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6081 | case IORING_OP_MADVISE: |
bfe76559 | 6082 | return io_madvise_prep(req, sqe); |
cebdb986 | 6083 | case IORING_OP_OPENAT2: |
bfe76559 | 6084 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6085 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6086 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6087 | case IORING_OP_SPLICE: |
bfe76559 | 6088 | return io_splice_prep(req, sqe); |
ddf0322d | 6089 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6090 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6091 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6092 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6093 | case IORING_OP_TEE: |
bfe76559 | 6094 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6095 | case IORING_OP_SHUTDOWN: |
6096 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6097 | case IORING_OP_RENAMEAT: |
6098 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6099 | case IORING_OP_UNLINKAT: |
6100 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6101 | } |
6102 | ||
bfe76559 PB |
6103 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6104 | req->opcode); | |
6105 | return-EINVAL; | |
6106 | } | |
6107 | ||
6108 | static int io_req_defer_prep(struct io_kiocb *req, | |
6109 | const struct io_uring_sqe *sqe) | |
6110 | { | |
bfe76559 PB |
6111 | if (!sqe) |
6112 | return 0; | |
6113 | if (io_alloc_async_data(req)) | |
6114 | return -EAGAIN; | |
bfe76559 | 6115 | return io_req_prep(req, sqe); |
f67676d1 JA |
6116 | } |
6117 | ||
9cf7c104 PB |
6118 | static u32 io_get_sequence(struct io_kiocb *req) |
6119 | { | |
6120 | struct io_kiocb *pos; | |
6121 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 6122 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 6123 | |
f2f87370 PB |
6124 | io_for_each_link(pos, req) |
6125 | nr_reqs++; | |
9cf7c104 PB |
6126 | |
6127 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
6128 | return total_submitted - nr_reqs; | |
6129 | } | |
6130 | ||
3529d8c2 | 6131 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 6132 | { |
a197f664 | 6133 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6134 | struct io_defer_entry *de; |
f67676d1 | 6135 | int ret; |
9cf7c104 | 6136 | u32 seq; |
de0617e4 | 6137 | |
9d858b21 | 6138 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
6139 | if (likely(list_empty_careful(&ctx->defer_list) && |
6140 | !(req->flags & REQ_F_IO_DRAIN))) | |
6141 | return 0; | |
6142 | ||
6143 | seq = io_get_sequence(req); | |
6144 | /* Still a chance to pass the sequence check */ | |
6145 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6146 | return 0; |
6147 | ||
e8c2bc1f | 6148 | if (!req->async_data) { |
650b5481 | 6149 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6150 | if (ret) |
650b5481 PB |
6151 | return ret; |
6152 | } | |
cbdcb435 | 6153 | io_prep_async_link(req); |
27dc8338 PB |
6154 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6155 | if (!de) | |
6156 | return -ENOMEM; | |
2d28390a | 6157 | |
de0617e4 | 6158 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6159 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6160 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6161 | kfree(de); |
ae34817b PB |
6162 | io_queue_async_work(req); |
6163 | return -EIOCBQUEUED; | |
de0617e4 JA |
6164 | } |
6165 | ||
915967f6 | 6166 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6167 | de->req = req; |
9cf7c104 | 6168 | de->seq = seq; |
27dc8338 | 6169 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6170 | spin_unlock_irq(&ctx->completion_lock); |
6171 | return -EIOCBQUEUED; | |
6172 | } | |
6173 | ||
3ca405eb | 6174 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6175 | { |
0e1b6fe3 PB |
6176 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6177 | switch (req->opcode) { | |
6178 | case IORING_OP_READV: | |
6179 | case IORING_OP_READ_FIXED: | |
6180 | case IORING_OP_READ: | |
bcda7baa | 6181 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6182 | break; |
6183 | case IORING_OP_RECVMSG: | |
6184 | case IORING_OP_RECV: | |
bcda7baa | 6185 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6186 | break; |
6187 | } | |
6188 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6189 | } |
6190 | ||
0e1b6fe3 PB |
6191 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6192 | switch (req->opcode) { | |
6193 | case IORING_OP_READV: | |
6194 | case IORING_OP_READ_FIXED: | |
6195 | case IORING_OP_READ: | |
6196 | case IORING_OP_WRITEV: | |
6197 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6198 | case IORING_OP_WRITE: { |
6199 | struct io_async_rw *io = req->async_data; | |
6200 | if (io->free_iovec) | |
6201 | kfree(io->free_iovec); | |
0e1b6fe3 | 6202 | break; |
e8c2bc1f | 6203 | } |
0e1b6fe3 | 6204 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6205 | case IORING_OP_SENDMSG: { |
6206 | struct io_async_msghdr *io = req->async_data; | |
6207 | if (io->iov != io->fast_iov) | |
6208 | kfree(io->iov); | |
0e1b6fe3 | 6209 | break; |
e8c2bc1f | 6210 | } |
0e1b6fe3 PB |
6211 | case IORING_OP_SPLICE: |
6212 | case IORING_OP_TEE: | |
6213 | io_put_file(req, req->splice.file_in, | |
6214 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
6215 | break; | |
f3cd4850 JA |
6216 | case IORING_OP_OPENAT: |
6217 | case IORING_OP_OPENAT2: | |
6218 | if (req->open.filename) | |
6219 | putname(req->open.filename); | |
6220 | break; | |
80a261fd JA |
6221 | case IORING_OP_RENAMEAT: |
6222 | putname(req->rename.oldpath); | |
6223 | putname(req->rename.newpath); | |
6224 | break; | |
14a1143b JA |
6225 | case IORING_OP_UNLINKAT: |
6226 | putname(req->unlink.filename); | |
6227 | break; | |
0e1b6fe3 PB |
6228 | } |
6229 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6230 | } |
99bc4c38 PB |
6231 | } |
6232 | ||
c1379e24 PB |
6233 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
6234 | struct io_comp_state *cs) | |
2b188cc1 | 6235 | { |
a197f664 | 6236 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 6237 | int ret; |
2b188cc1 | 6238 | |
d625c6ee | 6239 | switch (req->opcode) { |
2b188cc1 | 6240 | case IORING_OP_NOP: |
229a7b63 | 6241 | ret = io_nop(req, cs); |
2b188cc1 JA |
6242 | break; |
6243 | case IORING_OP_READV: | |
edafccee | 6244 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6245 | case IORING_OP_READ: |
a1d7c393 | 6246 | ret = io_read(req, force_nonblock, cs); |
edafccee | 6247 | break; |
3529d8c2 | 6248 | case IORING_OP_WRITEV: |
edafccee | 6249 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6250 | case IORING_OP_WRITE: |
a1d7c393 | 6251 | ret = io_write(req, force_nonblock, cs); |
2b188cc1 | 6252 | break; |
c992fe29 | 6253 | case IORING_OP_FSYNC: |
014db007 | 6254 | ret = io_fsync(req, force_nonblock); |
c992fe29 | 6255 | break; |
221c5eb2 | 6256 | case IORING_OP_POLL_ADD: |
014db007 | 6257 | ret = io_poll_add(req); |
221c5eb2 JA |
6258 | break; |
6259 | case IORING_OP_POLL_REMOVE: | |
fc4df999 | 6260 | ret = io_poll_remove(req); |
221c5eb2 | 6261 | break; |
5d17b4a4 | 6262 | case IORING_OP_SYNC_FILE_RANGE: |
014db007 | 6263 | ret = io_sync_file_range(req, force_nonblock); |
5d17b4a4 | 6264 | break; |
0fa03c62 | 6265 | case IORING_OP_SENDMSG: |
062d04d7 PB |
6266 | ret = io_sendmsg(req, force_nonblock, cs); |
6267 | break; | |
fddaface | 6268 | case IORING_OP_SEND: |
062d04d7 | 6269 | ret = io_send(req, force_nonblock, cs); |
0fa03c62 | 6270 | break; |
aa1fa28f | 6271 | case IORING_OP_RECVMSG: |
062d04d7 PB |
6272 | ret = io_recvmsg(req, force_nonblock, cs); |
6273 | break; | |
fddaface | 6274 | case IORING_OP_RECV: |
062d04d7 | 6275 | ret = io_recv(req, force_nonblock, cs); |
aa1fa28f | 6276 | break; |
5262f567 | 6277 | case IORING_OP_TIMEOUT: |
fc4df999 | 6278 | ret = io_timeout(req); |
5262f567 | 6279 | break; |
11365043 | 6280 | case IORING_OP_TIMEOUT_REMOVE: |
fc4df999 | 6281 | ret = io_timeout_remove(req); |
11365043 | 6282 | break; |
17f2fe35 | 6283 | case IORING_OP_ACCEPT: |
229a7b63 | 6284 | ret = io_accept(req, force_nonblock, cs); |
17f2fe35 | 6285 | break; |
f8e85cf2 | 6286 | case IORING_OP_CONNECT: |
229a7b63 | 6287 | ret = io_connect(req, force_nonblock, cs); |
f8e85cf2 | 6288 | break; |
62755e35 | 6289 | case IORING_OP_ASYNC_CANCEL: |
014db007 | 6290 | ret = io_async_cancel(req); |
62755e35 | 6291 | break; |
d63d1b5e | 6292 | case IORING_OP_FALLOCATE: |
014db007 | 6293 | ret = io_fallocate(req, force_nonblock); |
d63d1b5e | 6294 | break; |
15b71abe | 6295 | case IORING_OP_OPENAT: |
014db007 | 6296 | ret = io_openat(req, force_nonblock); |
15b71abe | 6297 | break; |
b5dba59e | 6298 | case IORING_OP_CLOSE: |
229a7b63 | 6299 | ret = io_close(req, force_nonblock, cs); |
b5dba59e | 6300 | break; |
05f3fb3c | 6301 | case IORING_OP_FILES_UPDATE: |
229a7b63 | 6302 | ret = io_files_update(req, force_nonblock, cs); |
05f3fb3c | 6303 | break; |
eddc7ef5 | 6304 | case IORING_OP_STATX: |
014db007 | 6305 | ret = io_statx(req, force_nonblock); |
eddc7ef5 | 6306 | break; |
4840e418 | 6307 | case IORING_OP_FADVISE: |
014db007 | 6308 | ret = io_fadvise(req, force_nonblock); |
4840e418 | 6309 | break; |
c1ca757b | 6310 | case IORING_OP_MADVISE: |
014db007 | 6311 | ret = io_madvise(req, force_nonblock); |
c1ca757b | 6312 | break; |
cebdb986 | 6313 | case IORING_OP_OPENAT2: |
014db007 | 6314 | ret = io_openat2(req, force_nonblock); |
cebdb986 | 6315 | break; |
3e4827b0 | 6316 | case IORING_OP_EPOLL_CTL: |
229a7b63 | 6317 | ret = io_epoll_ctl(req, force_nonblock, cs); |
3e4827b0 | 6318 | break; |
7d67af2c | 6319 | case IORING_OP_SPLICE: |
014db007 | 6320 | ret = io_splice(req, force_nonblock); |
7d67af2c | 6321 | break; |
ddf0322d | 6322 | case IORING_OP_PROVIDE_BUFFERS: |
229a7b63 | 6323 | ret = io_provide_buffers(req, force_nonblock, cs); |
ddf0322d | 6324 | break; |
067524e9 | 6325 | case IORING_OP_REMOVE_BUFFERS: |
229a7b63 | 6326 | ret = io_remove_buffers(req, force_nonblock, cs); |
3e4827b0 | 6327 | break; |
f2a8d5c7 | 6328 | case IORING_OP_TEE: |
f2a8d5c7 PB |
6329 | ret = io_tee(req, force_nonblock); |
6330 | break; | |
36f4fa68 JA |
6331 | case IORING_OP_SHUTDOWN: |
6332 | ret = io_shutdown(req, force_nonblock); | |
6333 | break; | |
80a261fd JA |
6334 | case IORING_OP_RENAMEAT: |
6335 | ret = io_renameat(req, force_nonblock); | |
6336 | break; | |
14a1143b JA |
6337 | case IORING_OP_UNLINKAT: |
6338 | ret = io_unlinkat(req, force_nonblock); | |
6339 | break; | |
2b188cc1 JA |
6340 | default: |
6341 | ret = -EINVAL; | |
6342 | break; | |
6343 | } | |
6344 | ||
def596e9 JA |
6345 | if (ret) |
6346 | return ret; | |
6347 | ||
b532576e JA |
6348 | /* If the op doesn't have a file, we're not polling for it */ |
6349 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6350 | const bool in_async = io_wq_current_is_worker(); |
6351 | ||
11ba820b JA |
6352 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6353 | if (in_async) | |
6354 | mutex_lock(&ctx->uring_lock); | |
6355 | ||
2e9dbe90 | 6356 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6357 | |
6358 | if (in_async) | |
6359 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6360 | } |
6361 | ||
6362 | return 0; | |
2b188cc1 JA |
6363 | } |
6364 | ||
f4db7182 | 6365 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6366 | { |
6367 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6368 | struct io_kiocb *timeout; |
561fb04a | 6369 | int ret = 0; |
2b188cc1 | 6370 | |
6df1db6b PB |
6371 | timeout = io_prep_linked_timeout(req); |
6372 | if (timeout) | |
6373 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6374 | |
4014d943 | 6375 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6376 | ret = -ECANCELED; |
31b51510 | 6377 | |
561fb04a | 6378 | if (!ret) { |
561fb04a | 6379 | do { |
c1379e24 | 6380 | ret = io_issue_sqe(req, false, NULL); |
561fb04a JA |
6381 | /* |
6382 | * We can get EAGAIN for polled IO even though we're | |
6383 | * forcing a sync submission from here, since we can't | |
6384 | * wait for request slots on the block side. | |
6385 | */ | |
6386 | if (ret != -EAGAIN) | |
6387 | break; | |
6388 | cond_resched(); | |
6389 | } while (1); | |
6390 | } | |
31b51510 | 6391 | |
561fb04a | 6392 | if (ret) { |
c07e6719 XW |
6393 | struct io_ring_ctx *lock_ctx = NULL; |
6394 | ||
6395 | if (req->ctx->flags & IORING_SETUP_IOPOLL) | |
6396 | lock_ctx = req->ctx; | |
6397 | ||
dad1b124 | 6398 | /* |
c07e6719 XW |
6399 | * io_iopoll_complete() does not hold completion_lock to |
6400 | * complete polled io, so here for polled io, we can not call | |
6401 | * io_req_complete() directly, otherwise there maybe concurrent | |
6402 | * access to cqring, defer_list, etc, which is not safe. Given | |
6403 | * that io_iopoll_complete() is always called under uring_lock, | |
6404 | * so here for polled io, we also get uring_lock to complete | |
6405 | * it. | |
dad1b124 | 6406 | */ |
c07e6719 XW |
6407 | if (lock_ctx) |
6408 | mutex_lock(&lock_ctx->uring_lock); | |
dad1b124 | 6409 | |
c07e6719 XW |
6410 | req_set_fail_links(req); |
6411 | io_req_complete(req, ret); | |
6412 | ||
6413 | if (lock_ctx) | |
6414 | mutex_unlock(&lock_ctx->uring_lock); | |
edafccee | 6415 | } |
2b188cc1 | 6416 | |
f4db7182 | 6417 | return io_steal_work(req); |
2b188cc1 JA |
6418 | } |
6419 | ||
65e19f54 JA |
6420 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6421 | int index) | |
6422 | { | |
269bbe5f | 6423 | struct fixed_rsrc_table *table; |
65e19f54 | 6424 | |
05f3fb3c | 6425 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6426 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6427 | } |
6428 | ||
8371adf5 PB |
6429 | static struct file *io_file_get(struct io_submit_state *state, |
6430 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6431 | { |
a197f664 | 6432 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6433 | struct file *file; |
09bb8394 | 6434 | |
8da11c19 | 6435 | if (fixed) { |
479f517b | 6436 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6437 | return NULL; |
b7620121 | 6438 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6439 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6440 | io_set_resource_node(req); |
09bb8394 | 6441 | } else { |
c826bd7a | 6442 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6443 | file = __io_file_get(state, fd); |
09bb8394 JA |
6444 | } |
6445 | ||
f609cbb8 PB |
6446 | if (file && file->f_op == &io_uring_fops && |
6447 | !(req->flags & REQ_F_INFLIGHT)) { | |
02a13674 JA |
6448 | io_req_init_async(req); |
6449 | req->flags |= REQ_F_INFLIGHT; | |
6450 | ||
6451 | spin_lock_irq(&ctx->inflight_lock); | |
6452 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
6453 | spin_unlock_irq(&ctx->inflight_lock); | |
6454 | } | |
6455 | ||
8371adf5 | 6456 | return file; |
09bb8394 JA |
6457 | } |
6458 | ||
2665abfd | 6459 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6460 | { |
ad8a48ac JA |
6461 | struct io_timeout_data *data = container_of(timer, |
6462 | struct io_timeout_data, timer); | |
90cd7e42 | 6463 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6464 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6465 | unsigned long flags; |
2665abfd JA |
6466 | |
6467 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6468 | prev = req->timeout.head; |
6469 | req->timeout.head = NULL; | |
2665abfd JA |
6470 | |
6471 | /* | |
6472 | * We don't expect the list to be empty, that will only happen if we | |
6473 | * race with the completion of the linked work. | |
6474 | */ | |
90cd7e42 | 6475 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6476 | io_remove_next_linked(prev); |
90cd7e42 PB |
6477 | else |
6478 | prev = NULL; | |
2665abfd JA |
6479 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6480 | ||
6481 | if (prev) { | |
4e88d6e7 | 6482 | req_set_fail_links(prev); |
014db007 | 6483 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6484 | io_put_req_deferred(prev, 1); |
47f46768 | 6485 | } else { |
9ae1f8dd PB |
6486 | io_req_complete_post(req, -ETIME, 0); |
6487 | io_put_req_deferred(req, 1); | |
2665abfd | 6488 | } |
2665abfd JA |
6489 | return HRTIMER_NORESTART; |
6490 | } | |
6491 | ||
7271ef3a | 6492 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6493 | { |
76a46e06 | 6494 | /* |
f2f87370 PB |
6495 | * If the back reference is NULL, then our linked request finished |
6496 | * before we got a chance to setup the timer | |
76a46e06 | 6497 | */ |
90cd7e42 | 6498 | if (req->timeout.head) { |
e8c2bc1f | 6499 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6500 | |
ad8a48ac JA |
6501 | data->timer.function = io_link_timeout_fn; |
6502 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6503 | data->mode); | |
2665abfd | 6504 | } |
7271ef3a JA |
6505 | } |
6506 | ||
6507 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6508 | { | |
6509 | struct io_ring_ctx *ctx = req->ctx; | |
6510 | ||
6511 | spin_lock_irq(&ctx->completion_lock); | |
6512 | __io_queue_linked_timeout(req); | |
76a46e06 | 6513 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6514 | |
2665abfd | 6515 | /* drop submission reference */ |
76a46e06 JA |
6516 | io_put_req(req); |
6517 | } | |
2665abfd | 6518 | |
ad8a48ac | 6519 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6520 | { |
f2f87370 | 6521 | struct io_kiocb *nxt = req->link; |
2665abfd | 6522 | |
f2f87370 PB |
6523 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6524 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6525 | return NULL; |
2665abfd | 6526 | |
90cd7e42 | 6527 | nxt->timeout.head = req; |
900fad45 | 6528 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6529 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6530 | return nxt; |
2665abfd JA |
6531 | } |
6532 | ||
c1379e24 | 6533 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
2b188cc1 | 6534 | { |
4a0a7a18 | 6535 | struct io_kiocb *linked_timeout; |
193155c8 | 6536 | const struct cred *old_creds = NULL; |
e0c5c576 | 6537 | int ret; |
2b188cc1 | 6538 | |
4a0a7a18 JA |
6539 | again: |
6540 | linked_timeout = io_prep_linked_timeout(req); | |
6541 | ||
2e5aa6cb PB |
6542 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
6543 | (req->work.flags & IO_WQ_WORK_CREDS) && | |
98447d65 | 6544 | req->work.identity->creds != current_cred()) { |
193155c8 JA |
6545 | if (old_creds) |
6546 | revert_creds(old_creds); | |
98447d65 | 6547 | if (old_creds == req->work.identity->creds) |
193155c8 JA |
6548 | old_creds = NULL; /* restored original creds */ |
6549 | else | |
98447d65 | 6550 | old_creds = override_creds(req->work.identity->creds); |
193155c8 JA |
6551 | } |
6552 | ||
c1379e24 | 6553 | ret = io_issue_sqe(req, true, cs); |
491381ce JA |
6554 | |
6555 | /* | |
6556 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6557 | * doesn't support non-blocking read/write attempts | |
6558 | */ | |
24c74678 | 6559 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6560 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6561 | /* |
6562 | * Queued up for async execution, worker will release | |
6563 | * submit reference when the iocb is actually submitted. | |
6564 | */ | |
6565 | io_queue_async_work(req); | |
2b188cc1 | 6566 | } |
bbad27b2 | 6567 | |
f063c547 PB |
6568 | if (linked_timeout) |
6569 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 PB |
6570 | } else if (likely(!ret)) { |
6571 | /* drop submission reference */ | |
e342c807 PB |
6572 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
6573 | list_add_tail(&req->compl.list, &cs->list); | |
6574 | if (++cs->nr >= 32) | |
9affd664 PB |
6575 | io_submit_flush_completions(cs); |
6576 | req = NULL; | |
6577 | } else { | |
6578 | req = io_put_req_find_next(req); | |
6579 | } | |
6580 | ||
0d63c148 PB |
6581 | if (linked_timeout) |
6582 | io_queue_linked_timeout(linked_timeout); | |
e65ef56d | 6583 | |
0d63c148 PB |
6584 | if (req) { |
6585 | if (!(req->flags & REQ_F_FORCE_ASYNC)) | |
6586 | goto again; | |
6587 | io_queue_async_work(req); | |
6588 | } | |
6589 | } else { | |
652532ad PB |
6590 | /* un-prep timeout, so it'll be killed as any other linked */ |
6591 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
4e88d6e7 | 6592 | req_set_fail_links(req); |
e65ef56d | 6593 | io_put_req(req); |
e1e16097 | 6594 | io_req_complete(req, ret); |
9e645e11 | 6595 | } |
652532ad | 6596 | |
193155c8 JA |
6597 | if (old_creds) |
6598 | revert_creds(old_creds); | |
2b188cc1 JA |
6599 | } |
6600 | ||
f13fad7b JA |
6601 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
6602 | struct io_comp_state *cs) | |
4fe2c963 JL |
6603 | { |
6604 | int ret; | |
6605 | ||
3529d8c2 | 6606 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
6607 | if (ret) { |
6608 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6609 | fail_req: |
4e88d6e7 | 6610 | req_set_fail_links(req); |
e1e16097 JA |
6611 | io_put_req(req); |
6612 | io_req_complete(req, ret); | |
4fe2c963 | 6613 | } |
2550878f | 6614 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
e8c2bc1f | 6615 | if (!req->async_data) { |
bd2ab18a | 6616 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6617 | if (unlikely(ret)) |
bd2ab18a PB |
6618 | goto fail_req; |
6619 | } | |
ce35a47a JA |
6620 | io_queue_async_work(req); |
6621 | } else { | |
c1379e24 PB |
6622 | if (sqe) { |
6623 | ret = io_req_prep(req, sqe); | |
6624 | if (unlikely(ret)) | |
6625 | goto fail_req; | |
6626 | } | |
6627 | __io_queue_sqe(req, cs); | |
ce35a47a | 6628 | } |
4fe2c963 JL |
6629 | } |
6630 | ||
f13fad7b JA |
6631 | static inline void io_queue_link_head(struct io_kiocb *req, |
6632 | struct io_comp_state *cs) | |
4fe2c963 | 6633 | { |
94ae5e77 | 6634 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
e1e16097 JA |
6635 | io_put_req(req); |
6636 | io_req_complete(req, -ECANCELED); | |
1b4a51b6 | 6637 | } else |
f13fad7b | 6638 | io_queue_sqe(req, NULL, cs); |
4fe2c963 JL |
6639 | } |
6640 | ||
863e0560 PB |
6641 | struct io_submit_link { |
6642 | struct io_kiocb *head; | |
6643 | struct io_kiocb *last; | |
6644 | }; | |
6645 | ||
1d4240cc | 6646 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
863e0560 | 6647 | struct io_submit_link *link, struct io_comp_state *cs) |
9e645e11 | 6648 | { |
a197f664 | 6649 | struct io_ring_ctx *ctx = req->ctx; |
ef4ff581 | 6650 | int ret; |
9e645e11 | 6651 | |
9e645e11 JA |
6652 | /* |
6653 | * If we already have a head request, queue this one for async | |
6654 | * submittal once the head completes. If we don't have a head but | |
6655 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6656 | * submitted sync once the chain is complete. If none of those | |
6657 | * conditions are true (normal request), then just queue it. | |
6658 | */ | |
863e0560 PB |
6659 | if (link->head) { |
6660 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6661 | |
8cdf2193 PB |
6662 | /* |
6663 | * Taking sequential execution of a link, draining both sides | |
6664 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6665 | * requests in the link. So, it drains the head and the | |
6666 | * next after the link request. The last one is done via | |
6667 | * drain_next flag to persist the effect across calls. | |
6668 | */ | |
ef4ff581 | 6669 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6670 | head->flags |= REQ_F_IO_DRAIN; |
6671 | ctx->drain_next = 1; | |
6672 | } | |
3529d8c2 | 6673 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6674 | if (unlikely(ret)) { |
4e88d6e7 | 6675 | /* fail even hard links since we don't submit */ |
9d76377f | 6676 | head->flags |= REQ_F_FAIL_LINK; |
1d4240cc | 6677 | return ret; |
2d28390a | 6678 | } |
9d76377f | 6679 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6680 | link->last->link = req; |
863e0560 | 6681 | link->last = req; |
32fe525b PB |
6682 | |
6683 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6684 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
f13fad7b | 6685 | io_queue_link_head(head, cs); |
863e0560 | 6686 | link->head = NULL; |
32fe525b | 6687 | } |
9e645e11 | 6688 | } else { |
711be031 PB |
6689 | if (unlikely(ctx->drain_next)) { |
6690 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6691 | ctx->drain_next = 0; |
711be031 | 6692 | } |
ef4ff581 | 6693 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
711be031 | 6694 | ret = io_req_defer_prep(req, sqe); |
327d6d96 | 6695 | if (unlikely(ret)) |
711be031 | 6696 | req->flags |= REQ_F_FAIL_LINK; |
863e0560 PB |
6697 | link->head = req; |
6698 | link->last = req; | |
711be031 | 6699 | } else { |
f13fad7b | 6700 | io_queue_sqe(req, sqe, cs); |
711be031 | 6701 | } |
9e645e11 | 6702 | } |
2e6e1fde | 6703 | |
1d4240cc | 6704 | return 0; |
9e645e11 JA |
6705 | } |
6706 | ||
9a56a232 JA |
6707 | /* |
6708 | * Batched submission is done, ensure local IO is flushed out. | |
6709 | */ | |
6710 | static void io_submit_state_end(struct io_submit_state *state) | |
6711 | { | |
f13fad7b JA |
6712 | if (!list_empty(&state->comp.list)) |
6713 | io_submit_flush_completions(&state->comp); | |
27926b68 JA |
6714 | if (state->plug_started) |
6715 | blk_finish_plug(&state->plug); | |
9f13c35b | 6716 | io_state_file_put(state); |
2579f913 | 6717 | if (state->free_reqs) |
6c8a3134 | 6718 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
6719 | } |
6720 | ||
6721 | /* | |
6722 | * Start submission side cache. | |
6723 | */ | |
6724 | static void io_submit_state_start(struct io_submit_state *state, | |
013538bd | 6725 | struct io_ring_ctx *ctx, unsigned int max_ios) |
9a56a232 | 6726 | { |
27926b68 | 6727 | state->plug_started = false; |
013538bd JA |
6728 | state->comp.nr = 0; |
6729 | INIT_LIST_HEAD(&state->comp.list); | |
6730 | state->comp.ctx = ctx; | |
2579f913 | 6731 | state->free_reqs = 0; |
6e1271e6 | 6732 | state->file_refs = 0; |
9a56a232 JA |
6733 | state->ios_left = max_ios; |
6734 | } | |
6735 | ||
2b188cc1 JA |
6736 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6737 | { | |
75b28aff | 6738 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6739 | |
caf582c6 PB |
6740 | /* |
6741 | * Ensure any loads from the SQEs are done at this point, | |
6742 | * since once we write the new head, the application could | |
6743 | * write new data to them. | |
6744 | */ | |
6745 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6746 | } |
6747 | ||
2b188cc1 | 6748 | /* |
3529d8c2 | 6749 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6750 | * that is mapped by userspace. This means that care needs to be taken to |
6751 | * ensure that reads are stable, as we cannot rely on userspace always | |
6752 | * being a good citizen. If members of the sqe are validated and then later | |
6753 | * used, it's important that those reads are done through READ_ONCE() to | |
6754 | * prevent a re-load down the line. | |
6755 | */ | |
709b302f | 6756 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6757 | { |
75b28aff | 6758 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6759 | unsigned head; |
6760 | ||
6761 | /* | |
6762 | * The cached sq head (or cq tail) serves two purposes: | |
6763 | * | |
6764 | * 1) allows us to batch the cost of updating the user visible | |
6765 | * head updates. | |
6766 | * 2) allows the kernel side to track the head on its own, even | |
6767 | * though the application is the one updating it. | |
6768 | */ | |
ee7d46d9 | 6769 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
709b302f PB |
6770 | if (likely(head < ctx->sq_entries)) |
6771 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6772 | |
6773 | /* drop invalid entries */ | |
498ccd9e | 6774 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6775 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6776 | return NULL; |
6777 | } | |
6778 | ||
6779 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) | |
6780 | { | |
6781 | ctx->cached_sq_head++; | |
2b188cc1 JA |
6782 | } |
6783 | ||
21b55dbc SG |
6784 | /* |
6785 | * Check SQE restrictions (opcode and flags). | |
6786 | * | |
6787 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6788 | */ | |
6789 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6790 | struct io_kiocb *req, | |
6791 | unsigned int sqe_flags) | |
6792 | { | |
6793 | if (!ctx->restricted) | |
6794 | return true; | |
6795 | ||
6796 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6797 | return false; | |
6798 | ||
6799 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6800 | ctx->restrictions.sqe_flags_required) | |
6801 | return false; | |
6802 | ||
6803 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6804 | ctx->restrictions.sqe_flags_required)) | |
6805 | return false; | |
6806 | ||
6807 | return true; | |
6808 | } | |
6809 | ||
ef4ff581 PB |
6810 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
6811 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
6812 | IOSQE_BUFFER_SELECT) | |
6813 | ||
6814 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
6815 | const struct io_uring_sqe *sqe, | |
0cdaf760 | 6816 | struct io_submit_state *state) |
0553b8bd | 6817 | { |
ef4ff581 | 6818 | unsigned int sqe_flags; |
71b547c0 | 6819 | int id, ret; |
ef4ff581 | 6820 | |
0553b8bd PB |
6821 | req->opcode = READ_ONCE(sqe->opcode); |
6822 | req->user_data = READ_ONCE(sqe->user_data); | |
e8c2bc1f | 6823 | req->async_data = NULL; |
0553b8bd PB |
6824 | req->file = NULL; |
6825 | req->ctx = ctx; | |
6826 | req->flags = 0; | |
f2f87370 | 6827 | req->link = NULL; |
269bbe5f | 6828 | req->fixed_rsrc_refs = NULL; |
0553b8bd PB |
6829 | /* one is dropped after submission, the other at completion */ |
6830 | refcount_set(&req->refs, 2); | |
4dd2824d | 6831 | req->task = current; |
0553b8bd | 6832 | req->result = 0; |
ef4ff581 PB |
6833 | |
6834 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6835 | return -EINVAL; | |
6836 | ||
28cea78a | 6837 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
9d8426a0 | 6838 | return -EFAULT; |
ef4ff581 PB |
6839 | |
6840 | sqe_flags = READ_ONCE(sqe->flags); | |
6841 | /* enforce forwards compatibility on users */ | |
6842 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) | |
6843 | return -EINVAL; | |
6844 | ||
21b55dbc SG |
6845 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6846 | return -EACCES; | |
6847 | ||
ef4ff581 PB |
6848 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
6849 | !io_op_defs[req->opcode].buffer_select) | |
6850 | return -EOPNOTSUPP; | |
6851 | ||
6852 | id = READ_ONCE(sqe->personality); | |
6853 | if (id) { | |
1e6fa521 JA |
6854 | struct io_identity *iod; |
6855 | ||
1e6fa521 JA |
6856 | iod = idr_find(&ctx->personality_idr, id); |
6857 | if (unlikely(!iod)) | |
ef4ff581 | 6858 | return -EINVAL; |
1e6fa521 | 6859 | refcount_inc(&iod->count); |
ec99ca6c PB |
6860 | |
6861 | __io_req_init_async(req); | |
1e6fa521 JA |
6862 | get_cred(iod->creds); |
6863 | req->work.identity = iod; | |
dfead8a8 | 6864 | req->work.flags |= IO_WQ_WORK_CREDS; |
ef4ff581 PB |
6865 | } |
6866 | ||
6867 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
c11368a5 | 6868 | req->flags |= sqe_flags; |
ef4ff581 | 6869 | |
27926b68 JA |
6870 | /* |
6871 | * Plug now if we have more than 1 IO left after this, and the target | |
6872 | * is potentially a read/write to block based storage. | |
6873 | */ | |
6874 | if (!state->plug_started && state->ios_left > 1 && | |
6875 | io_op_defs[req->opcode].plug) { | |
6876 | blk_start_plug(&state->plug); | |
6877 | state->plug_started = true; | |
6878 | } | |
6879 | ||
bd5bbda7 PB |
6880 | ret = 0; |
6881 | if (io_op_defs[req->opcode].needs_file) { | |
6882 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6883 | ||
6884 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
ba13e23f | 6885 | if (unlikely(!req->file)) |
bd5bbda7 PB |
6886 | ret = -EBADF; |
6887 | } | |
63ff8223 | 6888 | |
71b547c0 PB |
6889 | state->ios_left--; |
6890 | return ret; | |
0553b8bd PB |
6891 | } |
6892 | ||
0f212204 | 6893 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6894 | { |
ac8691c4 | 6895 | struct io_submit_state state; |
863e0560 | 6896 | struct io_submit_link link; |
9e645e11 | 6897 | int i, submitted = 0; |
6c271ce2 | 6898 | |
c4a2ed72 | 6899 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6900 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6901 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6902 | return -EBUSY; |
6903 | } | |
6c271ce2 | 6904 | |
ee7d46d9 PB |
6905 | /* make sure SQ entry isn't read before tail */ |
6906 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6907 | |
2b85edfc PB |
6908 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6909 | return -EAGAIN; | |
6c271ce2 | 6910 | |
d8a6df10 | 6911 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6912 | refcount_add(nr, ¤t->usage); |
6c271ce2 | 6913 | |
013538bd | 6914 | io_submit_state_start(&state, ctx, nr); |
863e0560 | 6915 | link.head = NULL; |
b14cca0c | 6916 | |
6c271ce2 | 6917 | for (i = 0; i < nr; i++) { |
3529d8c2 | 6918 | const struct io_uring_sqe *sqe; |
196be95c | 6919 | struct io_kiocb *req; |
1cb1edb2 | 6920 | int err; |
fb5ccc98 | 6921 | |
b1e50e54 PB |
6922 | sqe = io_get_sqe(ctx); |
6923 | if (unlikely(!sqe)) { | |
6924 | io_consume_sqe(ctx); | |
6925 | break; | |
6926 | } | |
ac8691c4 | 6927 | req = io_alloc_req(ctx, &state); |
196be95c PB |
6928 | if (unlikely(!req)) { |
6929 | if (!submitted) | |
6930 | submitted = -EAGAIN; | |
fb5ccc98 | 6931 | break; |
196be95c | 6932 | } |
709b302f | 6933 | io_consume_sqe(ctx); |
d3656344 JA |
6934 | /* will complete beyond this point, count as submitted */ |
6935 | submitted++; | |
6936 | ||
692d8363 | 6937 | err = io_init_req(ctx, req, sqe, &state); |
ef4ff581 | 6938 | if (unlikely(err)) { |
1cb1edb2 | 6939 | fail_req: |
e1e16097 JA |
6940 | io_put_req(req); |
6941 | io_req_complete(req, err); | |
196be95c PB |
6942 | break; |
6943 | } | |
fb5ccc98 | 6944 | |
354420f7 | 6945 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
2d7e9358 | 6946 | true, ctx->flags & IORING_SETUP_SQPOLL); |
f13fad7b | 6947 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
1d4240cc PB |
6948 | if (err) |
6949 | goto fail_req; | |
6c271ce2 JA |
6950 | } |
6951 | ||
9466f437 PB |
6952 | if (unlikely(submitted != nr)) { |
6953 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6954 | struct io_uring_task *tctx = current->io_uring; |
6955 | int unused = nr - ref_used; | |
9466f437 | 6956 | |
d8a6df10 JA |
6957 | percpu_ref_put_many(&ctx->refs, unused); |
6958 | percpu_counter_sub(&tctx->inflight, unused); | |
6959 | put_task_struct_many(current, unused); | |
9466f437 | 6960 | } |
863e0560 PB |
6961 | if (link.head) |
6962 | io_queue_link_head(link.head, &state.comp); | |
ac8691c4 | 6963 | io_submit_state_end(&state); |
6c271ce2 | 6964 | |
ae9428ca PB |
6965 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6966 | io_commit_sqring(ctx); | |
6967 | ||
6c271ce2 JA |
6968 | return submitted; |
6969 | } | |
6970 | ||
23b3628e XW |
6971 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6972 | { | |
6973 | /* Tell userspace we may need a wakeup call */ | |
6974 | spin_lock_irq(&ctx->completion_lock); | |
6975 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6976 | spin_unlock_irq(&ctx->completion_lock); | |
6977 | } | |
6978 | ||
6979 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6980 | { | |
6981 | spin_lock_irq(&ctx->completion_lock); | |
6982 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6983 | spin_unlock_irq(&ctx->completion_lock); | |
6984 | } | |
6985 | ||
08369246 | 6986 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6987 | { |
c8d1ba58 | 6988 | unsigned int to_submit; |
bdcd3eab | 6989 | int ret = 0; |
6c271ce2 | 6990 | |
c8d1ba58 | 6991 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6992 | /* if we're handling multiple rings, cap submit size for fairness */ |
6993 | if (cap_entries && to_submit > 8) | |
6994 | to_submit = 8; | |
6995 | ||
906a3c6f | 6996 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6997 | unsigned nr_events = 0; |
a4c0b3de | 6998 | |
c8d1ba58 | 6999 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7000 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 7001 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7002 | |
d9d05217 PB |
7003 | if (to_submit && !ctx->sqo_dead && |
7004 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 7005 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
7006 | mutex_unlock(&ctx->uring_lock); |
7007 | } | |
6c271ce2 | 7008 | |
90554200 JA |
7009 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7010 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 7011 | |
08369246 XW |
7012 | return ret; |
7013 | } | |
6c271ce2 | 7014 | |
08369246 XW |
7015 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7016 | { | |
7017 | struct io_ring_ctx *ctx; | |
7018 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7019 | |
08369246 XW |
7020 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7021 | if (sq_thread_idle < ctx->sq_thread_idle) | |
7022 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 7023 | } |
c1edbf5f | 7024 | |
08369246 | 7025 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7026 | } |
6c271ce2 | 7027 | |
69fb2131 JA |
7028 | static void io_sqd_init_new(struct io_sq_data *sqd) |
7029 | { | |
7030 | struct io_ring_ctx *ctx; | |
7031 | ||
7032 | while (!list_empty(&sqd->ctx_new_list)) { | |
7033 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
7034 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
7035 | complete(&ctx->sq_thread_comp); | |
7036 | } | |
08369246 XW |
7037 | |
7038 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
7039 | } |
7040 | ||
c8d1ba58 JA |
7041 | static int io_sq_thread(void *data) |
7042 | { | |
91d8f519 | 7043 | struct cgroup_subsys_state *cur_css = NULL; |
28cea78a JA |
7044 | struct files_struct *old_files = current->files; |
7045 | struct nsproxy *old_nsproxy = current->nsproxy; | |
69fb2131 JA |
7046 | const struct cred *old_cred = NULL; |
7047 | struct io_sq_data *sqd = data; | |
7048 | struct io_ring_ctx *ctx; | |
a0d9205f | 7049 | unsigned long timeout = 0; |
08369246 | 7050 | DEFINE_WAIT(wait); |
6c271ce2 | 7051 | |
28cea78a JA |
7052 | task_lock(current); |
7053 | current->files = NULL; | |
7054 | current->nsproxy = NULL; | |
7055 | task_unlock(current); | |
6c271ce2 | 7056 | |
69fb2131 | 7057 | while (!kthread_should_stop()) { |
08369246 XW |
7058 | int ret; |
7059 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
7060 | |
7061 | /* | |
69fb2131 JA |
7062 | * Any changes to the sqd lists are synchronized through the |
7063 | * kthread parking. This synchronizes the thread vs users, | |
7064 | * the users are synchronized on the sqd->ctx_lock. | |
c1edbf5f | 7065 | */ |
65b2b213 | 7066 | if (kthread_should_park()) { |
69fb2131 | 7067 | kthread_parkme(); |
65b2b213 XW |
7068 | /* |
7069 | * When sq thread is unparked, in case the previous park operation | |
7070 | * comes from io_put_sq_data(), which means that sq thread is going | |
7071 | * to be stopped, so here needs to have a check. | |
7072 | */ | |
7073 | if (kthread_should_stop()) | |
7074 | break; | |
7075 | } | |
7143b5ac | 7076 | |
08369246 | 7077 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 7078 | io_sqd_init_new(sqd); |
08369246 XW |
7079 | timeout = jiffies + sqd->sq_thread_idle; |
7080 | } | |
6c271ce2 | 7081 | |
08369246 | 7082 | sqt_spin = false; |
e95eee2d | 7083 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 JA |
7084 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7085 | if (current->cred != ctx->creds) { | |
7086 | if (old_cred) | |
7087 | revert_creds(old_cred); | |
7088 | old_cred = override_creds(ctx->creds); | |
bdcd3eab | 7089 | } |
91d8f519 | 7090 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
4ea33a97 JA |
7091 | #ifdef CONFIG_AUDIT |
7092 | current->loginuid = ctx->loginuid; | |
7093 | current->sessionid = ctx->sessionid; | |
7094 | #endif | |
bdcd3eab | 7095 | |
08369246 XW |
7096 | ret = __io_sq_thread(ctx, cap_entries); |
7097 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
7098 | sqt_spin = true; | |
6c271ce2 | 7099 | |
28cea78a | 7100 | io_sq_thread_drop_mm_files(); |
69fb2131 | 7101 | } |
6c271ce2 | 7102 | |
08369246 | 7103 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7104 | io_run_task_work(); |
d434ab6d | 7105 | io_sq_thread_drop_mm_files(); |
c8d1ba58 | 7106 | cond_resched(); |
08369246 XW |
7107 | if (sqt_spin) |
7108 | timeout = jiffies + sqd->sq_thread_idle; | |
7109 | continue; | |
7110 | } | |
7111 | ||
08369246 XW |
7112 | needs_sched = true; |
7113 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
7114 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
7115 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
7116 | !list_empty_careful(&ctx->iopoll_list)) { | |
7117 | needs_sched = false; | |
7118 | break; | |
7119 | } | |
7120 | if (io_sqring_entries(ctx)) { | |
7121 | needs_sched = false; | |
7122 | break; | |
7123 | } | |
7124 | } | |
7125 | ||
8b28fdf2 | 7126 | if (needs_sched && !kthread_should_park()) { |
69fb2131 JA |
7127 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7128 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 7129 | |
69fb2131 | 7130 | schedule(); |
69fb2131 JA |
7131 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7132 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7133 | } |
08369246 XW |
7134 | |
7135 | finish_wait(&sqd->wait, &wait); | |
7136 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
7137 | } |
7138 | ||
4c6e277c | 7139 | io_run_task_work(); |
d434ab6d | 7140 | io_sq_thread_drop_mm_files(); |
b41e9852 | 7141 | |
91d8f519 DZ |
7142 | if (cur_css) |
7143 | io_sq_thread_unassociate_blkcg(); | |
69fb2131 JA |
7144 | if (old_cred) |
7145 | revert_creds(old_cred); | |
06058632 | 7146 | |
28cea78a JA |
7147 | task_lock(current); |
7148 | current->files = old_files; | |
7149 | current->nsproxy = old_nsproxy; | |
7150 | task_unlock(current); | |
7151 | ||
2bbcd6d3 | 7152 | kthread_parkme(); |
06058632 | 7153 | |
6c271ce2 JA |
7154 | return 0; |
7155 | } | |
7156 | ||
bda52162 JA |
7157 | struct io_wait_queue { |
7158 | struct wait_queue_entry wq; | |
7159 | struct io_ring_ctx *ctx; | |
7160 | unsigned to_wait; | |
7161 | unsigned nr_timeouts; | |
7162 | }; | |
7163 | ||
6c503150 | 7164 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7165 | { |
7166 | struct io_ring_ctx *ctx = iowq->ctx; | |
7167 | ||
7168 | /* | |
d195a66e | 7169 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7170 | * started waiting. For timeouts, we always want to return to userspace, |
7171 | * regardless of event count. | |
7172 | */ | |
6c503150 | 7173 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
7174 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
7175 | } | |
7176 | ||
7177 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7178 | int wake_flags, void *key) | |
7179 | { | |
7180 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7181 | wq); | |
7182 | ||
6c503150 PB |
7183 | /* |
7184 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7185 | * the task, and the next invocation will do it. | |
7186 | */ | |
7187 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
7188 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
7189 | return -1; | |
bda52162 JA |
7190 | } |
7191 | ||
af9c1a44 JA |
7192 | static int io_run_task_work_sig(void) |
7193 | { | |
7194 | if (io_run_task_work()) | |
7195 | return 1; | |
7196 | if (!signal_pending(current)) | |
7197 | return 0; | |
792ee0f6 JA |
7198 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
7199 | return -ERESTARTSYS; | |
af9c1a44 JA |
7200 | return -EINTR; |
7201 | } | |
7202 | ||
2b188cc1 JA |
7203 | /* |
7204 | * Wait until events become available, if we don't already have some. The | |
7205 | * application must reap them itself, as they reside on the shared cq ring. | |
7206 | */ | |
7207 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7208 | const sigset_t __user *sig, size_t sigsz, |
7209 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7210 | { |
bda52162 JA |
7211 | struct io_wait_queue iowq = { |
7212 | .wq = { | |
7213 | .private = current, | |
7214 | .func = io_wake_function, | |
7215 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7216 | }, | |
7217 | .ctx = ctx, | |
7218 | .to_wait = min_events, | |
7219 | }; | |
75b28aff | 7220 | struct io_rings *rings = ctx->rings; |
c73ebb68 HX |
7221 | struct timespec64 ts; |
7222 | signed long timeout = 0; | |
e9ffa5c2 | 7223 | int ret = 0; |
2b188cc1 | 7224 | |
b41e9852 | 7225 | do { |
6c503150 PB |
7226 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
7227 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 7228 | return 0; |
4c6e277c | 7229 | if (!io_run_task_work()) |
b41e9852 | 7230 | break; |
b41e9852 | 7231 | } while (1); |
2b188cc1 JA |
7232 | |
7233 | if (sig) { | |
9e75ad5d AB |
7234 | #ifdef CONFIG_COMPAT |
7235 | if (in_compat_syscall()) | |
7236 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7237 | sigsz); |
9e75ad5d AB |
7238 | else |
7239 | #endif | |
b772434b | 7240 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7241 | |
2b188cc1 JA |
7242 | if (ret) |
7243 | return ret; | |
7244 | } | |
7245 | ||
c73ebb68 HX |
7246 | if (uts) { |
7247 | if (get_timespec64(&ts, uts)) | |
7248 | return -EFAULT; | |
7249 | timeout = timespec64_to_jiffies(&ts); | |
7250 | } | |
7251 | ||
bda52162 | 7252 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7253 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7254 | do { |
6c503150 | 7255 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
7256 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7257 | TASK_INTERRUPTIBLE); | |
ce593a6c | 7258 | /* make sure we run task_work before checking for signals */ |
af9c1a44 | 7259 | ret = io_run_task_work_sig(); |
6195ba09 HX |
7260 | if (ret > 0) { |
7261 | finish_wait(&ctx->wait, &iowq.wq); | |
4c6e277c | 7262 | continue; |
6195ba09 | 7263 | } |
af9c1a44 | 7264 | else if (ret < 0) |
bda52162 | 7265 | break; |
6c503150 | 7266 | if (io_should_wake(&iowq)) |
ce593a6c | 7267 | break; |
6195ba09 HX |
7268 | if (test_bit(0, &ctx->cq_check_overflow)) { |
7269 | finish_wait(&ctx->wait, &iowq.wq); | |
6c503150 | 7270 | continue; |
6195ba09 | 7271 | } |
c73ebb68 HX |
7272 | if (uts) { |
7273 | timeout = schedule_timeout(timeout); | |
7274 | if (timeout == 0) { | |
7275 | ret = -ETIME; | |
7276 | break; | |
7277 | } | |
7278 | } else { | |
7279 | schedule(); | |
7280 | } | |
bda52162 JA |
7281 | } while (1); |
7282 | finish_wait(&ctx->wait, &iowq.wq); | |
7283 | ||
b7db41c9 | 7284 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7285 | |
75b28aff | 7286 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7287 | } |
7288 | ||
6b06314c JA |
7289 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7290 | { | |
7291 | #if defined(CONFIG_UNIX) | |
7292 | if (ctx->ring_sock) { | |
7293 | struct sock *sock = ctx->ring_sock->sk; | |
7294 | struct sk_buff *skb; | |
7295 | ||
7296 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7297 | kfree_skb(skb); | |
7298 | } | |
7299 | #else | |
7300 | int i; | |
7301 | ||
65e19f54 JA |
7302 | for (i = 0; i < ctx->nr_user_files; i++) { |
7303 | struct file *file; | |
7304 | ||
7305 | file = io_file_from_index(ctx, i); | |
7306 | if (file) | |
7307 | fput(file); | |
7308 | } | |
6b06314c JA |
7309 | #endif |
7310 | } | |
7311 | ||
00835dce | 7312 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7313 | { |
269bbe5f | 7314 | struct fixed_rsrc_data *data; |
05f3fb3c | 7315 | |
269bbe5f | 7316 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
7317 | complete(&data->done); |
7318 | } | |
7319 | ||
2a63b2d9 BM |
7320 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
7321 | { | |
7322 | spin_lock_bh(&ctx->rsrc_ref_lock); | |
7323 | } | |
7324 | ||
7325 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) | |
7326 | { | |
7327 | spin_unlock_bh(&ctx->rsrc_ref_lock); | |
7328 | } | |
7329 | ||
d67d2263 BM |
7330 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
7331 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 7332 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 7333 | { |
2a63b2d9 | 7334 | io_rsrc_ref_lock(ctx); |
269bbe5f | 7335 | rsrc_data->node = ref_node; |
d67d2263 | 7336 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 7337 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 7338 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
7339 | } |
7340 | ||
d7954b2b BM |
7341 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
7342 | struct io_ring_ctx *ctx, | |
7343 | struct fixed_rsrc_ref_node *backup_node) | |
6b06314c | 7344 | { |
d7954b2b | 7345 | struct fixed_rsrc_ref_node *ref_node; |
1ffc5422 | 7346 | int ret; |
65e19f54 | 7347 | |
2a63b2d9 | 7348 | io_rsrc_ref_lock(ctx); |
1e5d770b | 7349 | ref_node = data->node; |
2a63b2d9 | 7350 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
7351 | if (ref_node) |
7352 | percpu_ref_kill(&ref_node->refs); | |
7353 | ||
7354 | percpu_ref_kill(&data->refs); | |
7355 | ||
7356 | /* wait for all refs nodes to complete */ | |
269bbe5f | 7357 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 PB |
7358 | do { |
7359 | ret = wait_for_completion_interruptible(&data->done); | |
7360 | if (!ret) | |
7361 | break; | |
7362 | ret = io_run_task_work_sig(); | |
7363 | if (ret < 0) { | |
7364 | percpu_ref_resurrect(&data->refs); | |
7365 | reinit_completion(&data->done); | |
d67d2263 | 7366 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
1ffc5422 PB |
7367 | return ret; |
7368 | } | |
7369 | } while (1); | |
05f3fb3c | 7370 | |
d7954b2b BM |
7371 | destroy_fixed_rsrc_ref_node(backup_node); |
7372 | return 0; | |
7373 | } | |
7374 | ||
1ad555c6 BM |
7375 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7376 | { | |
7377 | struct fixed_rsrc_data *data; | |
7378 | ||
7379 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7380 | if (!data) | |
7381 | return NULL; | |
7382 | ||
00835dce | 7383 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7384 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7385 | kfree(data); | |
7386 | return NULL; | |
7387 | } | |
7388 | data->ctx = ctx; | |
7389 | init_completion(&data->done); | |
7390 | return data; | |
7391 | } | |
7392 | ||
7393 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7394 | { | |
7395 | percpu_ref_exit(&data->refs); | |
7396 | kfree(data->table); | |
7397 | kfree(data); | |
7398 | } | |
7399 | ||
d7954b2b BM |
7400 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7401 | { | |
7402 | struct fixed_rsrc_data *data = ctx->file_data; | |
7403 | struct fixed_rsrc_ref_node *backup_node; | |
7404 | unsigned nr_tables, i; | |
7405 | int ret; | |
7406 | ||
7407 | if (!data) | |
7408 | return -ENXIO; | |
7409 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7410 | if (!backup_node) | |
7411 | return -ENOMEM; | |
7412 | init_fixed_file_ref_node(ctx, backup_node); | |
7413 | ||
7414 | ret = io_rsrc_ref_quiesce(data, ctx, backup_node); | |
7415 | if (ret) | |
7416 | return ret; | |
7417 | ||
6b06314c | 7418 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7419 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7420 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7421 | kfree(data->table[i].files); |
1ad555c6 | 7422 | free_fixed_rsrc_data(data); |
05f3fb3c | 7423 | ctx->file_data = NULL; |
6b06314c JA |
7424 | ctx->nr_user_files = 0; |
7425 | return 0; | |
7426 | } | |
7427 | ||
534ca6d6 | 7428 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7429 | { |
534ca6d6 | 7430 | if (refcount_dec_and_test(&sqd->refs)) { |
2bbcd6d3 RP |
7431 | /* |
7432 | * The park is a bit of a work-around, without it we get | |
7433 | * warning spews on shutdown with SQPOLL set and affinity | |
7434 | * set to a single CPU. | |
7435 | */ | |
534ca6d6 JA |
7436 | if (sqd->thread) { |
7437 | kthread_park(sqd->thread); | |
7438 | kthread_stop(sqd->thread); | |
7439 | } | |
7440 | ||
7441 | kfree(sqd); | |
7442 | } | |
7443 | } | |
7444 | ||
aa06165d JA |
7445 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7446 | { | |
7447 | struct io_ring_ctx *ctx_attach; | |
7448 | struct io_sq_data *sqd; | |
7449 | struct fd f; | |
7450 | ||
7451 | f = fdget(p->wq_fd); | |
7452 | if (!f.file) | |
7453 | return ERR_PTR(-ENXIO); | |
7454 | if (f.file->f_op != &io_uring_fops) { | |
7455 | fdput(f); | |
7456 | return ERR_PTR(-EINVAL); | |
7457 | } | |
7458 | ||
7459 | ctx_attach = f.file->private_data; | |
7460 | sqd = ctx_attach->sq_data; | |
7461 | if (!sqd) { | |
7462 | fdput(f); | |
7463 | return ERR_PTR(-EINVAL); | |
7464 | } | |
7465 | ||
7466 | refcount_inc(&sqd->refs); | |
7467 | fdput(f); | |
7468 | return sqd; | |
7469 | } | |
7470 | ||
534ca6d6 JA |
7471 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7472 | { | |
7473 | struct io_sq_data *sqd; | |
7474 | ||
aa06165d JA |
7475 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7476 | return io_attach_sq_data(p); | |
7477 | ||
534ca6d6 JA |
7478 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7479 | if (!sqd) | |
7480 | return ERR_PTR(-ENOMEM); | |
7481 | ||
7482 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7483 | INIT_LIST_HEAD(&sqd->ctx_list); |
7484 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7485 | mutex_init(&sqd->ctx_lock); | |
7486 | mutex_init(&sqd->lock); | |
534ca6d6 JA |
7487 | init_waitqueue_head(&sqd->wait); |
7488 | return sqd; | |
7489 | } | |
7490 | ||
69fb2131 JA |
7491 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7492 | __releases(&sqd->lock) | |
7493 | { | |
7494 | if (!sqd->thread) | |
7495 | return; | |
7496 | kthread_unpark(sqd->thread); | |
7497 | mutex_unlock(&sqd->lock); | |
7498 | } | |
7499 | ||
7500 | static void io_sq_thread_park(struct io_sq_data *sqd) | |
7501 | __acquires(&sqd->lock) | |
7502 | { | |
7503 | if (!sqd->thread) | |
7504 | return; | |
7505 | mutex_lock(&sqd->lock); | |
7506 | kthread_park(sqd->thread); | |
7507 | } | |
7508 | ||
534ca6d6 JA |
7509 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
7510 | { | |
7511 | struct io_sq_data *sqd = ctx->sq_data; | |
7512 | ||
7513 | if (sqd) { | |
7514 | if (sqd->thread) { | |
7515 | /* | |
7516 | * We may arrive here from the error branch in | |
7517 | * io_sq_offload_create() where the kthread is created | |
7518 | * without being waked up, thus wake it up now to make | |
7519 | * sure the wait will complete. | |
7520 | */ | |
7521 | wake_up_process(sqd->thread); | |
7522 | wait_for_completion(&ctx->sq_thread_comp); | |
69fb2131 JA |
7523 | |
7524 | io_sq_thread_park(sqd); | |
7525 | } | |
7526 | ||
7527 | mutex_lock(&sqd->ctx_lock); | |
7528 | list_del(&ctx->sqd_list); | |
08369246 | 7529 | io_sqd_update_thread_idle(sqd); |
69fb2131 JA |
7530 | mutex_unlock(&sqd->ctx_lock); |
7531 | ||
08369246 | 7532 | if (sqd->thread) |
69fb2131 | 7533 | io_sq_thread_unpark(sqd); |
534ca6d6 JA |
7534 | |
7535 | io_put_sq_data(sqd); | |
7536 | ctx->sq_data = NULL; | |
6c271ce2 JA |
7537 | } |
7538 | } | |
7539 | ||
6b06314c JA |
7540 | static void io_finish_async(struct io_ring_ctx *ctx) |
7541 | { | |
6c271ce2 JA |
7542 | io_sq_thread_stop(ctx); |
7543 | ||
561fb04a JA |
7544 | if (ctx->io_wq) { |
7545 | io_wq_destroy(ctx->io_wq); | |
7546 | ctx->io_wq = NULL; | |
6b06314c JA |
7547 | } |
7548 | } | |
7549 | ||
7550 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
7551 | /* |
7552 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7553 | * the io_uring can be safely unregistered on process exit, even if we have | |
7554 | * loops in the file referencing. | |
7555 | */ | |
7556 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7557 | { | |
7558 | struct sock *sk = ctx->ring_sock->sk; | |
7559 | struct scm_fp_list *fpl; | |
7560 | struct sk_buff *skb; | |
08a45173 | 7561 | int i, nr_files; |
6b06314c | 7562 | |
6b06314c JA |
7563 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7564 | if (!fpl) | |
7565 | return -ENOMEM; | |
7566 | ||
7567 | skb = alloc_skb(0, GFP_KERNEL); | |
7568 | if (!skb) { | |
7569 | kfree(fpl); | |
7570 | return -ENOMEM; | |
7571 | } | |
7572 | ||
7573 | skb->sk = sk; | |
6b06314c | 7574 | |
08a45173 | 7575 | nr_files = 0; |
6b06314c JA |
7576 | fpl->user = get_uid(ctx->user); |
7577 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7578 | struct file *file = io_file_from_index(ctx, i + offset); |
7579 | ||
7580 | if (!file) | |
08a45173 | 7581 | continue; |
65e19f54 | 7582 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7583 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7584 | nr_files++; | |
6b06314c JA |
7585 | } |
7586 | ||
08a45173 JA |
7587 | if (nr_files) { |
7588 | fpl->max = SCM_MAX_FD; | |
7589 | fpl->count = nr_files; | |
7590 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7591 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7592 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7593 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7594 | |
08a45173 JA |
7595 | for (i = 0; i < nr_files; i++) |
7596 | fput(fpl->fp[i]); | |
7597 | } else { | |
7598 | kfree_skb(skb); | |
7599 | kfree(fpl); | |
7600 | } | |
6b06314c JA |
7601 | |
7602 | return 0; | |
7603 | } | |
7604 | ||
7605 | /* | |
7606 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7607 | * causes regular reference counting to break down. We rely on the UNIX | |
7608 | * garbage collection to take care of this problem for us. | |
7609 | */ | |
7610 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7611 | { | |
7612 | unsigned left, total; | |
7613 | int ret = 0; | |
7614 | ||
7615 | total = 0; | |
7616 | left = ctx->nr_user_files; | |
7617 | while (left) { | |
7618 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7619 | |
7620 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7621 | if (ret) | |
7622 | break; | |
7623 | left -= this_files; | |
7624 | total += this_files; | |
7625 | } | |
7626 | ||
7627 | if (!ret) | |
7628 | return 0; | |
7629 | ||
7630 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7631 | struct file *file = io_file_from_index(ctx, total); |
7632 | ||
7633 | if (file) | |
7634 | fput(file); | |
6b06314c JA |
7635 | total++; |
7636 | } | |
7637 | ||
7638 | return ret; | |
7639 | } | |
7640 | #else | |
7641 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7642 | { | |
7643 | return 0; | |
7644 | } | |
7645 | #endif | |
7646 | ||
269bbe5f | 7647 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7648 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7649 | { |
7650 | int i; | |
7651 | ||
7652 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7653 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7654 | unsigned this_files; |
7655 | ||
7656 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7657 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7658 | GFP_KERNEL); | |
7659 | if (!table->files) | |
7660 | break; | |
7661 | nr_files -= this_files; | |
7662 | } | |
7663 | ||
7664 | if (i == nr_tables) | |
7665 | return 0; | |
7666 | ||
7667 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7668 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7669 | kfree(table->files); |
7670 | } | |
7671 | return 1; | |
7672 | } | |
7673 | ||
50238531 | 7674 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7675 | { |
50238531 | 7676 | struct file *file = prsrc->file; |
05f3fb3c JA |
7677 | #if defined(CONFIG_UNIX) |
7678 | struct sock *sock = ctx->ring_sock->sk; | |
7679 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7680 | struct sk_buff *skb; | |
7681 | int i; | |
7682 | ||
7683 | __skb_queue_head_init(&list); | |
7684 | ||
7685 | /* | |
7686 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7687 | * remove this entry and rearrange the file array. | |
7688 | */ | |
7689 | skb = skb_dequeue(head); | |
7690 | while (skb) { | |
7691 | struct scm_fp_list *fp; | |
7692 | ||
7693 | fp = UNIXCB(skb).fp; | |
7694 | for (i = 0; i < fp->count; i++) { | |
7695 | int left; | |
7696 | ||
7697 | if (fp->fp[i] != file) | |
7698 | continue; | |
7699 | ||
7700 | unix_notinflight(fp->user, fp->fp[i]); | |
7701 | left = fp->count - 1 - i; | |
7702 | if (left) { | |
7703 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7704 | left * sizeof(struct file *)); | |
7705 | } | |
7706 | fp->count--; | |
7707 | if (!fp->count) { | |
7708 | kfree_skb(skb); | |
7709 | skb = NULL; | |
7710 | } else { | |
7711 | __skb_queue_tail(&list, skb); | |
7712 | } | |
7713 | fput(file); | |
7714 | file = NULL; | |
7715 | break; | |
7716 | } | |
7717 | ||
7718 | if (!file) | |
7719 | break; | |
7720 | ||
7721 | __skb_queue_tail(&list, skb); | |
7722 | ||
7723 | skb = skb_dequeue(head); | |
7724 | } | |
7725 | ||
7726 | if (skb_peek(&list)) { | |
7727 | spin_lock_irq(&head->lock); | |
7728 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7729 | __skb_queue_tail(head, skb); | |
7730 | spin_unlock_irq(&head->lock); | |
7731 | } | |
7732 | #else | |
7733 | fput(file); | |
7734 | #endif | |
7735 | } | |
7736 | ||
269bbe5f | 7737 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7738 | { |
269bbe5f BM |
7739 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7740 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7741 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7742 | |
269bbe5f BM |
7743 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7744 | list_del(&prsrc->list); | |
50238531 | 7745 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7746 | kfree(prsrc); |
65e19f54 | 7747 | } |
05589553 | 7748 | |
05589553 XW |
7749 | percpu_ref_exit(&ref_node->refs); |
7750 | kfree(ref_node); | |
269bbe5f | 7751 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7752 | } |
65e19f54 | 7753 | |
269bbe5f | 7754 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7755 | { |
7756 | struct io_ring_ctx *ctx; | |
7757 | struct llist_node *node; | |
7758 | ||
269bbe5f BM |
7759 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7760 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7761 | |
7762 | while (node) { | |
269bbe5f | 7763 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7764 | struct llist_node *next = node->next; |
7765 | ||
269bbe5f BM |
7766 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7767 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7768 | node = next; |
7769 | } | |
7770 | } | |
7771 | ||
00835dce | 7772 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7773 | { |
269bbe5f BM |
7774 | struct fixed_rsrc_ref_node *ref_node; |
7775 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7776 | struct io_ring_ctx *ctx; |
e297822b | 7777 | bool first_add = false; |
4a38aed2 | 7778 | int delay = HZ; |
65e19f54 | 7779 | |
269bbe5f BM |
7780 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7781 | data = ref_node->rsrc_data; | |
e297822b PB |
7782 | ctx = data->ctx; |
7783 | ||
2a63b2d9 | 7784 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7785 | ref_node->done = true; |
7786 | ||
d67d2263 BM |
7787 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7788 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7789 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7790 | /* recycle ref nodes in order */ |
7791 | if (!ref_node->done) | |
7792 | break; | |
7793 | list_del(&ref_node->node); | |
269bbe5f | 7794 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7795 | } |
2a63b2d9 | 7796 | io_rsrc_ref_unlock(ctx); |
05589553 | 7797 | |
e297822b | 7798 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7799 | delay = 0; |
05589553 | 7800 | |
4a38aed2 | 7801 | if (!delay) |
269bbe5f | 7802 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7803 | else if (first_add) |
269bbe5f | 7804 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7805 | } |
65e19f54 | 7806 | |
6802535d | 7807 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7808 | struct io_ring_ctx *ctx) |
05f3fb3c | 7809 | { |
269bbe5f | 7810 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7811 | |
05589553 XW |
7812 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7813 | if (!ref_node) | |
3e2224c5 | 7814 | return NULL; |
05f3fb3c | 7815 | |
00835dce | 7816 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7817 | 0, GFP_KERNEL)) { |
7818 | kfree(ref_node); | |
3e2224c5 | 7819 | return NULL; |
05589553 XW |
7820 | } |
7821 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7822 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
6802535d BM |
7823 | ref_node->done = false; |
7824 | return ref_node; | |
7825 | } | |
7826 | ||
bc9744cd PB |
7827 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7828 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7829 | { |
269bbe5f | 7830 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7831 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7832 | } |
7833 | ||
269bbe5f | 7834 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7835 | { |
7836 | percpu_ref_exit(&ref_node->refs); | |
7837 | kfree(ref_node); | |
65e19f54 JA |
7838 | } |
7839 | ||
6b06314c JA |
7840 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7841 | unsigned nr_args) | |
7842 | { | |
7843 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7844 | unsigned nr_tables, i; |
05f3fb3c | 7845 | struct file *file; |
600cf3f8 | 7846 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7847 | struct fixed_rsrc_ref_node *ref_node; |
7848 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7849 | |
05f3fb3c | 7850 | if (ctx->file_data) |
6b06314c JA |
7851 | return -EBUSY; |
7852 | if (!nr_args) | |
7853 | return -EINVAL; | |
7854 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7855 | return -EMFILE; | |
7856 | ||
1ad555c6 | 7857 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7858 | if (!file_data) |
05f3fb3c | 7859 | return -ENOMEM; |
13770a71 | 7860 | ctx->file_data = file_data; |
05f3fb3c | 7861 | |
65e19f54 | 7862 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7863 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7864 | GFP_KERNEL); |
600cf3f8 PB |
7865 | if (!file_data->table) |
7866 | goto out_free; | |
05f3fb3c | 7867 | |
600cf3f8 | 7868 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7869 | goto out_free; |
65e19f54 | 7870 | |
08a45173 | 7871 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
269bbe5f | 7872 | struct fixed_rsrc_table *table; |
65e19f54 JA |
7873 | unsigned index; |
7874 | ||
600cf3f8 PB |
7875 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7876 | ret = -EFAULT; | |
7877 | goto out_fput; | |
7878 | } | |
08a45173 | 7879 | /* allow sparse sets */ |
600cf3f8 | 7880 | if (fd == -1) |
08a45173 | 7881 | continue; |
6b06314c | 7882 | |
05f3fb3c | 7883 | file = fget(fd); |
6b06314c | 7884 | ret = -EBADF; |
05f3fb3c | 7885 | if (!file) |
600cf3f8 | 7886 | goto out_fput; |
05f3fb3c | 7887 | |
6b06314c JA |
7888 | /* |
7889 | * Don't allow io_uring instances to be registered. If UNIX | |
7890 | * isn't enabled, then this causes a reference cycle and this | |
7891 | * instance can never get freed. If UNIX is enabled we'll | |
7892 | * handle it just fine, but there's still no point in allowing | |
7893 | * a ring fd as it doesn't support regular read/write anyway. | |
7894 | */ | |
05f3fb3c JA |
7895 | if (file->f_op == &io_uring_fops) { |
7896 | fput(file); | |
600cf3f8 | 7897 | goto out_fput; |
6b06314c | 7898 | } |
600cf3f8 PB |
7899 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
7900 | index = i & IORING_FILE_TABLE_MASK; | |
05f3fb3c | 7901 | table->files[index] = file; |
6b06314c JA |
7902 | } |
7903 | ||
6b06314c | 7904 | ret = io_sqe_files_scm(ctx); |
05589553 | 7905 | if (ret) { |
6b06314c | 7906 | io_sqe_files_unregister(ctx); |
05589553 XW |
7907 | return ret; |
7908 | } | |
6b06314c | 7909 | |
bc9744cd | 7910 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7911 | if (!ref_node) { |
05589553 | 7912 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7913 | return -ENOMEM; |
05589553 | 7914 | } |
bc9744cd | 7915 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7916 | |
d67d2263 | 7917 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7918 | return ret; |
600cf3f8 PB |
7919 | out_fput: |
7920 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7921 | file = io_file_from_index(ctx, i); | |
7922 | if (file) | |
7923 | fput(file); | |
7924 | } | |
7925 | for (i = 0; i < nr_tables; i++) | |
7926 | kfree(file_data->table[i].files); | |
7927 | ctx->nr_user_files = 0; | |
600cf3f8 | 7928 | out_free: |
1ad555c6 | 7929 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7930 | ctx->file_data = NULL; |
6b06314c JA |
7931 | return ret; |
7932 | } | |
7933 | ||
c3a31e60 JA |
7934 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7935 | int index) | |
7936 | { | |
7937 | #if defined(CONFIG_UNIX) | |
7938 | struct sock *sock = ctx->ring_sock->sk; | |
7939 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7940 | struct sk_buff *skb; | |
7941 | ||
7942 | /* | |
7943 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7944 | * file set. If there's no room, fall back to allocating a new skb | |
7945 | * and filling it in. | |
7946 | */ | |
7947 | spin_lock_irq(&head->lock); | |
7948 | skb = skb_peek(head); | |
7949 | if (skb) { | |
7950 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7951 | ||
7952 | if (fpl->count < SCM_MAX_FD) { | |
7953 | __skb_unlink(skb, head); | |
7954 | spin_unlock_irq(&head->lock); | |
7955 | fpl->fp[fpl->count] = get_file(file); | |
7956 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7957 | fpl->count++; | |
7958 | spin_lock_irq(&head->lock); | |
7959 | __skb_queue_head(head, skb); | |
7960 | } else { | |
7961 | skb = NULL; | |
7962 | } | |
7963 | } | |
7964 | spin_unlock_irq(&head->lock); | |
7965 | ||
7966 | if (skb) { | |
7967 | fput(file); | |
7968 | return 0; | |
7969 | } | |
7970 | ||
7971 | return __io_sqe_files_scm(ctx, 1, index); | |
7972 | #else | |
7973 | return 0; | |
7974 | #endif | |
7975 | } | |
7976 | ||
50238531 | 7977 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7978 | { |
269bbe5f BM |
7979 | struct io_rsrc_put *prsrc; |
7980 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7981 | |
269bbe5f BM |
7982 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7983 | if (!prsrc) | |
a5318d3c | 7984 | return -ENOMEM; |
05f3fb3c | 7985 | |
50238531 | 7986 | prsrc->rsrc = rsrc; |
269bbe5f | 7987 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7988 | |
a5318d3c | 7989 | return 0; |
05f3fb3c JA |
7990 | } |
7991 | ||
269bbe5f BM |
7992 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7993 | struct file *file) | |
7994 | { | |
50238531 | 7995 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7996 | } |
7997 | ||
05f3fb3c | 7998 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7999 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
8000 | unsigned nr_args) |
8001 | { | |
269bbe5f BM |
8002 | struct fixed_rsrc_data *data = ctx->file_data; |
8003 | struct fixed_rsrc_ref_node *ref_node; | |
05f3fb3c | 8004 | struct file *file; |
c3a31e60 JA |
8005 | __s32 __user *fds; |
8006 | int fd, i, err; | |
8007 | __u32 done; | |
05589553 | 8008 | bool needs_switch = false; |
c3a31e60 | 8009 | |
05f3fb3c | 8010 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
8011 | return -EOVERFLOW; |
8012 | if (done > ctx->nr_user_files) | |
8013 | return -EINVAL; | |
8014 | ||
bc9744cd | 8015 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
8016 | if (!ref_node) |
8017 | return -ENOMEM; | |
bc9744cd | 8018 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 8019 | |
269bbe5f | 8020 | fds = u64_to_user_ptr(up->data); |
67973b93 | 8021 | for (done = 0; done < nr_args; done++) { |
269bbe5f | 8022 | struct fixed_rsrc_table *table; |
65e19f54 JA |
8023 | unsigned index; |
8024 | ||
c3a31e60 JA |
8025 | err = 0; |
8026 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
8027 | err = -EFAULT; | |
8028 | break; | |
8029 | } | |
4e0377a1 | 8030 | if (fd == IORING_REGISTER_FILES_SKIP) |
8031 | continue; | |
8032 | ||
67973b93 | 8033 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
05f3fb3c | 8034 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
65e19f54 JA |
8035 | index = i & IORING_FILE_TABLE_MASK; |
8036 | if (table->files[index]) { | |
98dfd502 | 8037 | file = table->files[index]; |
a5318d3c HD |
8038 | err = io_queue_file_removal(data, file); |
8039 | if (err) | |
8040 | break; | |
65e19f54 | 8041 | table->files[index] = NULL; |
05589553 | 8042 | needs_switch = true; |
c3a31e60 JA |
8043 | } |
8044 | if (fd != -1) { | |
c3a31e60 JA |
8045 | file = fget(fd); |
8046 | if (!file) { | |
8047 | err = -EBADF; | |
8048 | break; | |
8049 | } | |
8050 | /* | |
8051 | * Don't allow io_uring instances to be registered. If | |
8052 | * UNIX isn't enabled, then this causes a reference | |
8053 | * cycle and this instance can never get freed. If UNIX | |
8054 | * is enabled we'll handle it just fine, but there's | |
8055 | * still no point in allowing a ring fd as it doesn't | |
8056 | * support regular read/write anyway. | |
8057 | */ | |
8058 | if (file->f_op == &io_uring_fops) { | |
8059 | fput(file); | |
8060 | err = -EBADF; | |
8061 | break; | |
8062 | } | |
65e19f54 | 8063 | table->files[index] = file; |
c3a31e60 | 8064 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8065 | if (err) { |
95d1c8e5 | 8066 | table->files[index] = NULL; |
f3bd9dae | 8067 | fput(file); |
c3a31e60 | 8068 | break; |
f3bd9dae | 8069 | } |
c3a31e60 | 8070 | } |
05f3fb3c JA |
8071 | } |
8072 | ||
05589553 | 8073 | if (needs_switch) { |
b2e96852 | 8074 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 8075 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 8076 | } else |
269bbe5f | 8077 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
8078 | |
8079 | return done ? done : err; | |
8080 | } | |
05589553 | 8081 | |
05f3fb3c JA |
8082 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
8083 | unsigned nr_args) | |
8084 | { | |
269bbe5f | 8085 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
8086 | |
8087 | if (!ctx->file_data) | |
8088 | return -ENXIO; | |
8089 | if (!nr_args) | |
8090 | return -EINVAL; | |
8091 | if (copy_from_user(&up, arg, sizeof(up))) | |
8092 | return -EFAULT; | |
8093 | if (up.resv) | |
8094 | return -EINVAL; | |
8095 | ||
8096 | return __io_sqe_files_update(ctx, &up, nr_args); | |
8097 | } | |
c3a31e60 | 8098 | |
e9fd9396 | 8099 | static void io_free_work(struct io_wq_work *work) |
7d723065 JA |
8100 | { |
8101 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8102 | ||
e9fd9396 | 8103 | /* Consider that io_steal_work() relies on this ref */ |
7d723065 JA |
8104 | io_put_req(req); |
8105 | } | |
8106 | ||
24369c2e PB |
8107 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
8108 | struct io_uring_params *p) | |
8109 | { | |
8110 | struct io_wq_data data; | |
8111 | struct fd f; | |
8112 | struct io_ring_ctx *ctx_attach; | |
8113 | unsigned int concurrency; | |
8114 | int ret = 0; | |
8115 | ||
8116 | data.user = ctx->user; | |
e9fd9396 | 8117 | data.free_work = io_free_work; |
f5fa38c5 | 8118 | data.do_work = io_wq_submit_work; |
24369c2e PB |
8119 | |
8120 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
8121 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
8122 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
8123 | ||
8124 | ctx->io_wq = io_wq_create(concurrency, &data); | |
8125 | if (IS_ERR(ctx->io_wq)) { | |
8126 | ret = PTR_ERR(ctx->io_wq); | |
8127 | ctx->io_wq = NULL; | |
8128 | } | |
8129 | return ret; | |
8130 | } | |
8131 | ||
8132 | f = fdget(p->wq_fd); | |
8133 | if (!f.file) | |
8134 | return -EBADF; | |
8135 | ||
8136 | if (f.file->f_op != &io_uring_fops) { | |
8137 | ret = -EINVAL; | |
8138 | goto out_fput; | |
8139 | } | |
8140 | ||
8141 | ctx_attach = f.file->private_data; | |
8142 | /* @io_wq is protected by holding the fd */ | |
8143 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
8144 | ret = -EINVAL; | |
8145 | goto out_fput; | |
8146 | } | |
8147 | ||
8148 | ctx->io_wq = ctx_attach->io_wq; | |
8149 | out_fput: | |
8150 | fdput(f); | |
8151 | return ret; | |
8152 | } | |
8153 | ||
0f212204 JA |
8154 | static int io_uring_alloc_task_context(struct task_struct *task) |
8155 | { | |
8156 | struct io_uring_task *tctx; | |
d8a6df10 | 8157 | int ret; |
0f212204 JA |
8158 | |
8159 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
8160 | if (unlikely(!tctx)) | |
8161 | return -ENOMEM; | |
8162 | ||
d8a6df10 JA |
8163 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8164 | if (unlikely(ret)) { | |
8165 | kfree(tctx); | |
8166 | return ret; | |
8167 | } | |
8168 | ||
0f212204 JA |
8169 | xa_init(&tctx->xa); |
8170 | init_waitqueue_head(&tctx->wait); | |
8171 | tctx->last = NULL; | |
fdaf083c JA |
8172 | atomic_set(&tctx->in_idle, 0); |
8173 | tctx->sqpoll = false; | |
500a373d JA |
8174 | io_init_identity(&tctx->__identity); |
8175 | tctx->identity = &tctx->__identity; | |
0f212204 JA |
8176 | task->io_uring = tctx; |
8177 | return 0; | |
8178 | } | |
8179 | ||
8180 | void __io_uring_free(struct task_struct *tsk) | |
8181 | { | |
8182 | struct io_uring_task *tctx = tsk->io_uring; | |
8183 | ||
8184 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
500a373d JA |
8185 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
8186 | if (tctx->identity != &tctx->__identity) | |
8187 | kfree(tctx->identity); | |
d8a6df10 | 8188 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8189 | kfree(tctx); |
8190 | tsk->io_uring = NULL; | |
8191 | } | |
8192 | ||
7e84e1c7 SG |
8193 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8194 | struct io_uring_params *p) | |
2b188cc1 JA |
8195 | { |
8196 | int ret; | |
8197 | ||
6c271ce2 | 8198 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
8199 | struct io_sq_data *sqd; |
8200 | ||
3ec482d1 | 8201 | ret = -EPERM; |
ce59fc69 | 8202 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
8203 | goto err; |
8204 | ||
534ca6d6 JA |
8205 | sqd = io_get_sq_data(p); |
8206 | if (IS_ERR(sqd)) { | |
8207 | ret = PTR_ERR(sqd); | |
8208 | goto err; | |
8209 | } | |
69fb2131 | 8210 | |
534ca6d6 | 8211 | ctx->sq_data = sqd; |
69fb2131 JA |
8212 | io_sq_thread_park(sqd); |
8213 | mutex_lock(&sqd->ctx_lock); | |
8214 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
8215 | mutex_unlock(&sqd->ctx_lock); | |
8216 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 8217 | |
917257da JA |
8218 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8219 | if (!ctx->sq_thread_idle) | |
8220 | ctx->sq_thread_idle = HZ; | |
8221 | ||
aa06165d JA |
8222 | if (sqd->thread) |
8223 | goto done; | |
8224 | ||
6c271ce2 | 8225 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8226 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8227 | |
917257da | 8228 | ret = -EINVAL; |
44a9bd18 JA |
8229 | if (cpu >= nr_cpu_ids) |
8230 | goto err; | |
7889f44d | 8231 | if (!cpu_online(cpu)) |
917257da JA |
8232 | goto err; |
8233 | ||
69fb2131 | 8234 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
534ca6d6 | 8235 | cpu, "io_uring-sq"); |
6c271ce2 | 8236 | } else { |
69fb2131 | 8237 | sqd->thread = kthread_create(io_sq_thread, sqd, |
6c271ce2 JA |
8238 | "io_uring-sq"); |
8239 | } | |
534ca6d6 JA |
8240 | if (IS_ERR(sqd->thread)) { |
8241 | ret = PTR_ERR(sqd->thread); | |
8242 | sqd->thread = NULL; | |
6c271ce2 JA |
8243 | goto err; |
8244 | } | |
534ca6d6 | 8245 | ret = io_uring_alloc_task_context(sqd->thread); |
0f212204 JA |
8246 | if (ret) |
8247 | goto err; | |
6c271ce2 JA |
8248 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8249 | /* Can't have SQ_AFF without SQPOLL */ | |
8250 | ret = -EINVAL; | |
8251 | goto err; | |
8252 | } | |
8253 | ||
aa06165d | 8254 | done: |
24369c2e PB |
8255 | ret = io_init_wq_offload(ctx, p); |
8256 | if (ret) | |
2b188cc1 | 8257 | goto err; |
2b188cc1 JA |
8258 | |
8259 | return 0; | |
8260 | err: | |
54a91f3b | 8261 | io_finish_async(ctx); |
2b188cc1 JA |
8262 | return ret; |
8263 | } | |
8264 | ||
7e84e1c7 SG |
8265 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
8266 | { | |
534ca6d6 JA |
8267 | struct io_sq_data *sqd = ctx->sq_data; |
8268 | ||
8269 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
8270 | wake_up_process(sqd->thread); | |
7e84e1c7 SG |
8271 | } |
8272 | ||
a087e2b5 BM |
8273 | static inline void __io_unaccount_mem(struct user_struct *user, |
8274 | unsigned long nr_pages) | |
2b188cc1 JA |
8275 | { |
8276 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8277 | } | |
8278 | ||
a087e2b5 BM |
8279 | static inline int __io_account_mem(struct user_struct *user, |
8280 | unsigned long nr_pages) | |
2b188cc1 JA |
8281 | { |
8282 | unsigned long page_limit, cur_pages, new_pages; | |
8283 | ||
8284 | /* Don't allow more pages than we can safely lock */ | |
8285 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8286 | ||
8287 | do { | |
8288 | cur_pages = atomic_long_read(&user->locked_vm); | |
8289 | new_pages = cur_pages + nr_pages; | |
8290 | if (new_pages > page_limit) | |
8291 | return -ENOMEM; | |
8292 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8293 | new_pages) != cur_pages); | |
8294 | ||
8295 | return 0; | |
8296 | } | |
8297 | ||
2e0464d4 BM |
8298 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8299 | enum io_mem_account acct) | |
a087e2b5 | 8300 | { |
aad5d8da | 8301 | if (ctx->limit_mem) |
a087e2b5 | 8302 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8303 | |
2aede0e4 | 8304 | if (ctx->mm_account) { |
4bc4a912 JA |
8305 | if (acct == ACCT_LOCKED) { |
8306 | mmap_write_lock(ctx->mm_account); | |
2aede0e4 | 8307 | ctx->mm_account->locked_vm -= nr_pages; |
4bc4a912 JA |
8308 | mmap_write_unlock(ctx->mm_account); |
8309 | }else if (acct == ACCT_PINNED) { | |
2aede0e4 | 8310 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8311 | } |
2e0464d4 | 8312 | } |
a087e2b5 BM |
8313 | } |
8314 | ||
2e0464d4 BM |
8315 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
8316 | enum io_mem_account acct) | |
a087e2b5 | 8317 | { |
30975825 BM |
8318 | int ret; |
8319 | ||
8320 | if (ctx->limit_mem) { | |
8321 | ret = __io_account_mem(ctx->user, nr_pages); | |
8322 | if (ret) | |
8323 | return ret; | |
8324 | } | |
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_add(nr_pages, &ctx->mm_account->pinned_vm); |
4bc4a912 | 8333 | } |
2e0464d4 | 8334 | } |
a087e2b5 BM |
8335 | |
8336 | return 0; | |
8337 | } | |
8338 | ||
2b188cc1 JA |
8339 | static void io_mem_free(void *ptr) |
8340 | { | |
52e04ef4 MR |
8341 | struct page *page; |
8342 | ||
8343 | if (!ptr) | |
8344 | return; | |
2b188cc1 | 8345 | |
52e04ef4 | 8346 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8347 | if (put_page_testzero(page)) |
8348 | free_compound_page(page); | |
8349 | } | |
8350 | ||
8351 | static void *io_mem_alloc(size_t size) | |
8352 | { | |
8353 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
8354 | __GFP_NORETRY; | |
8355 | ||
8356 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8357 | } | |
8358 | ||
75b28aff HV |
8359 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8360 | size_t *sq_offset) | |
8361 | { | |
8362 | struct io_rings *rings; | |
8363 | size_t off, sq_array_size; | |
8364 | ||
8365 | off = struct_size(rings, cqes, cq_entries); | |
8366 | if (off == SIZE_MAX) | |
8367 | return SIZE_MAX; | |
8368 | ||
8369 | #ifdef CONFIG_SMP | |
8370 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8371 | if (off == 0) | |
8372 | return SIZE_MAX; | |
8373 | #endif | |
8374 | ||
b36200f5 DV |
8375 | if (sq_offset) |
8376 | *sq_offset = off; | |
8377 | ||
75b28aff HV |
8378 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8379 | if (sq_array_size == SIZE_MAX) | |
8380 | return SIZE_MAX; | |
8381 | ||
8382 | if (check_add_overflow(off, sq_array_size, &off)) | |
8383 | return SIZE_MAX; | |
8384 | ||
75b28aff HV |
8385 | return off; |
8386 | } | |
8387 | ||
2b188cc1 JA |
8388 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
8389 | { | |
75b28aff | 8390 | size_t pages; |
2b188cc1 | 8391 | |
75b28aff HV |
8392 | pages = (size_t)1 << get_order( |
8393 | rings_size(sq_entries, cq_entries, NULL)); | |
8394 | pages += (size_t)1 << get_order( | |
8395 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 8396 | |
75b28aff | 8397 | return pages; |
2b188cc1 JA |
8398 | } |
8399 | ||
0a96bbe4 | 8400 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8401 | { |
8402 | int i, j; | |
8403 | ||
8404 | if (!ctx->user_bufs) | |
8405 | return -ENXIO; | |
8406 | ||
8407 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8408 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8409 | ||
8410 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8411 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8412 | |
de293938 JA |
8413 | if (imu->acct_pages) |
8414 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
d4ef6475 | 8415 | kvfree(imu->bvec); |
edafccee JA |
8416 | imu->nr_bvecs = 0; |
8417 | } | |
8418 | ||
8419 | kfree(ctx->user_bufs); | |
8420 | ctx->user_bufs = NULL; | |
8421 | ctx->nr_user_bufs = 0; | |
8422 | return 0; | |
8423 | } | |
8424 | ||
8425 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8426 | void __user *arg, unsigned index) | |
8427 | { | |
8428 | struct iovec __user *src; | |
8429 | ||
8430 | #ifdef CONFIG_COMPAT | |
8431 | if (ctx->compat) { | |
8432 | struct compat_iovec __user *ciovs; | |
8433 | struct compat_iovec ciov; | |
8434 | ||
8435 | ciovs = (struct compat_iovec __user *) arg; | |
8436 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8437 | return -EFAULT; | |
8438 | ||
d55e5f5b | 8439 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8440 | dst->iov_len = ciov.iov_len; |
8441 | return 0; | |
8442 | } | |
8443 | #endif | |
8444 | src = (struct iovec __user *) arg; | |
8445 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8446 | return -EFAULT; | |
8447 | return 0; | |
8448 | } | |
8449 | ||
de293938 JA |
8450 | /* |
8451 | * Not super efficient, but this is just a registration time. And we do cache | |
8452 | * the last compound head, so generally we'll only do a full search if we don't | |
8453 | * match that one. | |
8454 | * | |
8455 | * We check if the given compound head page has already been accounted, to | |
8456 | * avoid double accounting it. This allows us to account the full size of the | |
8457 | * page, not just the constituent pages of a huge page. | |
8458 | */ | |
8459 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8460 | int nr_pages, struct page *hpage) | |
8461 | { | |
8462 | int i, j; | |
8463 | ||
8464 | /* check current page array */ | |
8465 | for (i = 0; i < nr_pages; i++) { | |
8466 | if (!PageCompound(pages[i])) | |
8467 | continue; | |
8468 | if (compound_head(pages[i]) == hpage) | |
8469 | return true; | |
8470 | } | |
8471 | ||
8472 | /* check previously registered pages */ | |
8473 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8474 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8475 | ||
8476 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8477 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8478 | continue; | |
8479 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8480 | return true; | |
8481 | } | |
8482 | } | |
8483 | ||
8484 | return false; | |
8485 | } | |
8486 | ||
8487 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8488 | int nr_pages, struct io_mapped_ubuf *imu, | |
8489 | struct page **last_hpage) | |
8490 | { | |
8491 | int i, ret; | |
8492 | ||
8493 | for (i = 0; i < nr_pages; i++) { | |
8494 | if (!PageCompound(pages[i])) { | |
8495 | imu->acct_pages++; | |
8496 | } else { | |
8497 | struct page *hpage; | |
8498 | ||
8499 | hpage = compound_head(pages[i]); | |
8500 | if (hpage == *last_hpage) | |
8501 | continue; | |
8502 | *last_hpage = hpage; | |
8503 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8504 | continue; | |
8505 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8506 | } | |
8507 | } | |
8508 | ||
8509 | if (!imu->acct_pages) | |
8510 | return 0; | |
8511 | ||
8512 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); | |
8513 | if (ret) | |
8514 | imu->acct_pages = 0; | |
8515 | return ret; | |
8516 | } | |
8517 | ||
0a96bbe4 BM |
8518 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8519 | struct io_mapped_ubuf *imu, | |
8520 | struct page **last_hpage) | |
edafccee JA |
8521 | { |
8522 | struct vm_area_struct **vmas = NULL; | |
8523 | struct page **pages = NULL; | |
0a96bbe4 BM |
8524 | unsigned long off, start, end, ubuf; |
8525 | size_t size; | |
8526 | int ret, pret, nr_pages, i; | |
8527 | ||
8528 | ubuf = (unsigned long) iov->iov_base; | |
8529 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8530 | start = ubuf >> PAGE_SHIFT; | |
8531 | nr_pages = end - start; | |
8532 | ||
8533 | ret = -ENOMEM; | |
8534 | ||
8535 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8536 | if (!pages) | |
8537 | goto done; | |
8538 | ||
8539 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8540 | GFP_KERNEL); | |
8541 | if (!vmas) | |
8542 | goto done; | |
8543 | ||
8544 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), | |
8545 | GFP_KERNEL); | |
8546 | if (!imu->bvec) | |
8547 | goto done; | |
8548 | ||
8549 | ret = 0; | |
8550 | mmap_read_lock(current->mm); | |
8551 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8552 | pages, vmas); | |
8553 | if (pret == nr_pages) { | |
8554 | /* don't support file backed memory */ | |
8555 | for (i = 0; i < nr_pages; i++) { | |
8556 | struct vm_area_struct *vma = vmas[i]; | |
8557 | ||
8558 | if (vma->vm_file && | |
8559 | !is_file_hugepages(vma->vm_file)) { | |
8560 | ret = -EOPNOTSUPP; | |
8561 | break; | |
8562 | } | |
8563 | } | |
8564 | } else { | |
8565 | ret = pret < 0 ? pret : -EFAULT; | |
8566 | } | |
8567 | mmap_read_unlock(current->mm); | |
8568 | if (ret) { | |
8569 | /* | |
8570 | * if we did partial map, or found file backed vmas, | |
8571 | * release any pages we did get | |
8572 | */ | |
8573 | if (pret > 0) | |
8574 | unpin_user_pages(pages, pret); | |
8575 | kvfree(imu->bvec); | |
8576 | goto done; | |
8577 | } | |
8578 | ||
8579 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8580 | if (ret) { | |
8581 | unpin_user_pages(pages, pret); | |
8582 | kvfree(imu->bvec); | |
8583 | goto done; | |
8584 | } | |
8585 | ||
8586 | off = ubuf & ~PAGE_MASK; | |
8587 | size = iov->iov_len; | |
8588 | for (i = 0; i < nr_pages; i++) { | |
8589 | size_t vec_len; | |
8590 | ||
8591 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8592 | imu->bvec[i].bv_page = pages[i]; | |
8593 | imu->bvec[i].bv_len = vec_len; | |
8594 | imu->bvec[i].bv_offset = off; | |
8595 | off = 0; | |
8596 | size -= vec_len; | |
8597 | } | |
8598 | /* store original address for later verification */ | |
8599 | imu->ubuf = ubuf; | |
8600 | imu->len = iov->iov_len; | |
8601 | imu->nr_bvecs = nr_pages; | |
8602 | ret = 0; | |
8603 | done: | |
8604 | kvfree(pages); | |
8605 | kvfree(vmas); | |
8606 | return ret; | |
8607 | } | |
8608 | ||
2b358604 | 8609 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8610 | { |
edafccee JA |
8611 | if (ctx->user_bufs) |
8612 | return -EBUSY; | |
8613 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8614 | return -EINVAL; | |
8615 | ||
8616 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8617 | GFP_KERNEL); | |
8618 | if (!ctx->user_bufs) | |
8619 | return -ENOMEM; | |
8620 | ||
2b358604 BM |
8621 | return 0; |
8622 | } | |
8623 | ||
8624 | static int io_buffer_validate(struct iovec *iov) | |
8625 | { | |
8626 | /* | |
8627 | * Don't impose further limits on the size and buffer | |
8628 | * constraints here, we'll -EINVAL later when IO is | |
8629 | * submitted if they are wrong. | |
8630 | */ | |
8631 | if (!iov->iov_base || !iov->iov_len) | |
8632 | return -EFAULT; | |
8633 | ||
8634 | /* arbitrary limit, but we need something */ | |
8635 | if (iov->iov_len > SZ_1G) | |
8636 | return -EFAULT; | |
8637 | ||
8638 | return 0; | |
8639 | } | |
8640 | ||
8641 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
8642 | unsigned int nr_args) | |
8643 | { | |
8644 | int i, ret; | |
8645 | struct iovec iov; | |
8646 | struct page *last_hpage = NULL; | |
8647 | ||
8648 | ret = io_buffers_map_alloc(ctx, nr_args); | |
8649 | if (ret) | |
8650 | return ret; | |
8651 | ||
edafccee JA |
8652 | for (i = 0; i < nr_args; i++) { |
8653 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee JA |
8654 | |
8655 | ret = io_copy_iov(ctx, &iov, arg, i); | |
8656 | if (ret) | |
0a96bbe4 | 8657 | break; |
edafccee | 8658 | |
2b358604 BM |
8659 | ret = io_buffer_validate(&iov); |
8660 | if (ret) | |
0a96bbe4 | 8661 | break; |
edafccee | 8662 | |
0a96bbe4 BM |
8663 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8664 | if (ret) | |
8665 | break; | |
edafccee JA |
8666 | |
8667 | ctx->nr_user_bufs++; | |
8668 | } | |
0a96bbe4 BM |
8669 | |
8670 | if (ret) | |
8671 | io_sqe_buffers_unregister(ctx); | |
8672 | ||
edafccee JA |
8673 | return ret; |
8674 | } | |
8675 | ||
9b402849 JA |
8676 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8677 | { | |
8678 | __s32 __user *fds = arg; | |
8679 | int fd; | |
8680 | ||
8681 | if (ctx->cq_ev_fd) | |
8682 | return -EBUSY; | |
8683 | ||
8684 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8685 | return -EFAULT; | |
8686 | ||
8687 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8688 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8689 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8690 | ctx->cq_ev_fd = NULL; | |
8691 | return ret; | |
8692 | } | |
8693 | ||
8694 | return 0; | |
8695 | } | |
8696 | ||
8697 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8698 | { | |
8699 | if (ctx->cq_ev_fd) { | |
8700 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8701 | ctx->cq_ev_fd = NULL; | |
8702 | return 0; | |
8703 | } | |
8704 | ||
8705 | return -ENXIO; | |
8706 | } | |
8707 | ||
5a2e745d JA |
8708 | static int __io_destroy_buffers(int id, void *p, void *data) |
8709 | { | |
8710 | struct io_ring_ctx *ctx = data; | |
8711 | struct io_buffer *buf = p; | |
8712 | ||
067524e9 | 8713 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8714 | return 0; |
8715 | } | |
8716 | ||
8717 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8718 | { | |
8719 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8720 | idr_destroy(&ctx->io_buffer_idr); | |
8721 | } | |
8722 | ||
2b188cc1 JA |
8723 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8724 | { | |
6b06314c | 8725 | io_finish_async(ctx); |
0a96bbe4 | 8726 | io_sqe_buffers_unregister(ctx); |
2aede0e4 JA |
8727 | |
8728 | if (ctx->sqo_task) { | |
8729 | put_task_struct(ctx->sqo_task); | |
8730 | ctx->sqo_task = NULL; | |
8731 | mmdrop(ctx->mm_account); | |
8732 | ctx->mm_account = NULL; | |
30975825 | 8733 | } |
def596e9 | 8734 | |
91d8f519 DZ |
8735 | #ifdef CONFIG_BLK_CGROUP |
8736 | if (ctx->sqo_blkcg_css) | |
8737 | css_put(ctx->sqo_blkcg_css); | |
8738 | #endif | |
8739 | ||
6b06314c | 8740 | io_sqe_files_unregister(ctx); |
9b402849 | 8741 | io_eventfd_unregister(ctx); |
5a2e745d | 8742 | io_destroy_buffers(ctx); |
41726c9a | 8743 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8744 | |
2b188cc1 | 8745 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8746 | if (ctx->ring_sock) { |
8747 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8748 | sock_release(ctx->ring_sock); |
355e8d26 | 8749 | } |
2b188cc1 JA |
8750 | #endif |
8751 | ||
75b28aff | 8752 | io_mem_free(ctx->rings); |
2b188cc1 | 8753 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8754 | |
8755 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8756 | free_uid(ctx->user); |
181e448d | 8757 | put_cred(ctx->creds); |
78076bb6 | 8758 | kfree(ctx->cancel_hash); |
0ddf92e8 | 8759 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
8760 | kfree(ctx); |
8761 | } | |
8762 | ||
8763 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8764 | { | |
8765 | struct io_ring_ctx *ctx = file->private_data; | |
8766 | __poll_t mask = 0; | |
8767 | ||
8768 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8769 | /* |
8770 | * synchronizes with barrier from wq_has_sleeper call in | |
8771 | * io_commit_cqring | |
8772 | */ | |
2b188cc1 | 8773 | smp_rmb(); |
90554200 | 8774 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8775 | mask |= EPOLLOUT | EPOLLWRNORM; |
6c503150 PB |
8776 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
8777 | if (io_cqring_events(ctx)) | |
2b188cc1 JA |
8778 | mask |= EPOLLIN | EPOLLRDNORM; |
8779 | ||
8780 | return mask; | |
8781 | } | |
8782 | ||
8783 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8784 | { | |
8785 | struct io_ring_ctx *ctx = file->private_data; | |
8786 | ||
8787 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8788 | } | |
8789 | ||
0bead8cd | 8790 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8791 | { |
1e6fa521 | 8792 | struct io_identity *iod; |
071698e1 | 8793 | |
1e6fa521 JA |
8794 | iod = idr_remove(&ctx->personality_idr, id); |
8795 | if (iod) { | |
8796 | put_cred(iod->creds); | |
8797 | if (refcount_dec_and_test(&iod->count)) | |
8798 | kfree(iod); | |
0bead8cd | 8799 | return 0; |
1e6fa521 | 8800 | } |
0bead8cd YD |
8801 | |
8802 | return -EINVAL; | |
8803 | } | |
8804 | ||
8805 | static int io_remove_personalities(int id, void *p, void *data) | |
8806 | { | |
8807 | struct io_ring_ctx *ctx = data; | |
8808 | ||
8809 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8810 | return 0; |
8811 | } | |
8812 | ||
85faa7b8 JA |
8813 | static void io_ring_exit_work(struct work_struct *work) |
8814 | { | |
b2edc0a7 PB |
8815 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8816 | exit_work); | |
85faa7b8 | 8817 | |
56952e91 JA |
8818 | /* |
8819 | * If we're doing polled IO and end up having requests being | |
8820 | * submitted async (out-of-line), then completions can come in while | |
8821 | * we're waiting for refs to drop. We need to reap these manually, | |
8822 | * as nobody else will be looking for them. | |
8823 | */ | |
b2edc0a7 | 8824 | do { |
90df0853 | 8825 | __io_uring_cancel_task_requests(ctx, NULL); |
b2edc0a7 | 8826 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8827 | io_ring_ctx_free(ctx); |
8828 | } | |
8829 | ||
00c18640 JA |
8830 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8831 | { | |
8832 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8833 | ||
8834 | return req->ctx == data; | |
8835 | } | |
8836 | ||
2b188cc1 JA |
8837 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8838 | { | |
8839 | mutex_lock(&ctx->uring_lock); | |
8840 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8841 | |
8842 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8843 | ctx->sqo_dead = 1; | |
8844 | ||
cda286f0 PB |
8845 | /* if force is set, the ring is going away. always drop after that */ |
8846 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8847 | if (ctx->rings) |
6c503150 | 8848 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8849 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8850 | mutex_unlock(&ctx->uring_lock); |
8851 | ||
6b81928d PB |
8852 | io_kill_timeouts(ctx, NULL, NULL); |
8853 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a JA |
8854 | |
8855 | if (ctx->io_wq) | |
00c18640 | 8856 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
561fb04a | 8857 | |
15dff286 | 8858 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8859 | io_iopoll_try_reap_events(ctx); |
309fc03a JA |
8860 | |
8861 | /* | |
8862 | * Do this upfront, so we won't have a grace period where the ring | |
8863 | * is closed but resources aren't reaped yet. This can cause | |
8864 | * spurious failure in setting up a new ring. | |
8865 | */ | |
760618f7 JA |
8866 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
8867 | ACCT_LOCKED); | |
309fc03a | 8868 | |
85faa7b8 | 8869 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8870 | /* |
8871 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8872 | * if we're exiting a ton of rings at the same time. It just adds | |
8873 | * noise and overhead, there's no discernable change in runtime | |
8874 | * over using system_wq. | |
8875 | */ | |
8876 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8877 | } |
8878 | ||
8879 | static int io_uring_release(struct inode *inode, struct file *file) | |
8880 | { | |
8881 | struct io_ring_ctx *ctx = file->private_data; | |
8882 | ||
8883 | file->private_data = NULL; | |
8884 | io_ring_ctx_wait_and_kill(ctx); | |
8885 | return 0; | |
8886 | } | |
8887 | ||
f6edbabb PB |
8888 | struct io_task_cancel { |
8889 | struct task_struct *task; | |
8890 | struct files_struct *files; | |
8891 | }; | |
f254ac04 | 8892 | |
f6edbabb | 8893 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8894 | { |
9a472ef7 | 8895 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8896 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8897 | bool ret; |
8898 | ||
f6edbabb | 8899 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8900 | unsigned long flags; |
8901 | struct io_ring_ctx *ctx = req->ctx; | |
8902 | ||
8903 | /* protect against races with linked timeouts */ | |
8904 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8905 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8906 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8907 | } else { | |
f6edbabb | 8908 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8909 | } |
8910 | return ret; | |
b711d4ea JA |
8911 | } |
8912 | ||
b7ddce3c | 8913 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8914 | struct task_struct *task, |
b7ddce3c PB |
8915 | struct files_struct *files) |
8916 | { | |
8917 | struct io_defer_entry *de = NULL; | |
8918 | LIST_HEAD(list); | |
8919 | ||
8920 | spin_lock_irq(&ctx->completion_lock); | |
8921 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8922 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8923 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8924 | break; | |
8925 | } | |
8926 | } | |
8927 | spin_unlock_irq(&ctx->completion_lock); | |
8928 | ||
8929 | while (!list_empty(&list)) { | |
8930 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8931 | list_del_init(&de->list); | |
8932 | req_set_fail_links(de->req); | |
8933 | io_put_req(de->req); | |
8934 | io_req_complete(de->req, -ECANCELED); | |
8935 | kfree(de); | |
8936 | } | |
8937 | } | |
8938 | ||
ca70f00b PB |
8939 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8940 | struct task_struct *task, | |
8941 | struct files_struct *files) | |
8942 | { | |
8943 | struct io_kiocb *req; | |
8944 | int cnt = 0; | |
8945 | ||
8946 | spin_lock_irq(&ctx->inflight_lock); | |
8947 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8948 | cnt += io_match_task(req, task, files); | |
8949 | spin_unlock_irq(&ctx->inflight_lock); | |
8950 | return cnt; | |
8951 | } | |
8952 | ||
b52fda00 | 8953 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8954 | struct task_struct *task, |
fcb323cc JA |
8955 | struct files_struct *files) |
8956 | { | |
fcb323cc | 8957 | while (!list_empty_careful(&ctx->inflight_list)) { |
bee749b1 | 8958 | struct io_task_cancel cancel = { .task = task, .files = files }; |
d8f1b971 | 8959 | DEFINE_WAIT(wait); |
ca70f00b | 8960 | int inflight; |
fcb323cc | 8961 | |
ca70f00b PB |
8962 | inflight = io_uring_count_inflight(ctx, task, files); |
8963 | if (!inflight) | |
fcb323cc | 8964 | break; |
f6edbabb PB |
8965 | |
8966 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); | |
8967 | io_poll_remove_all(ctx, task, files); | |
8968 | io_kill_timeouts(ctx, task, files); | |
9d5c8190 | 8969 | io_cqring_overflow_flush(ctx, true, task, files); |
6200b0ae JA |
8970 | /* cancellations _may_ trigger task work */ |
8971 | io_run_task_work(); | |
ca70f00b PB |
8972 | |
8973 | prepare_to_wait(&task->io_uring->wait, &wait, | |
8974 | TASK_UNINTERRUPTIBLE); | |
8975 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8976 | schedule(); | |
c98de08c | 8977 | finish_wait(&task->io_uring->wait, &wait); |
fcb323cc | 8978 | } |
44e728b8 PB |
8979 | } |
8980 | ||
b52fda00 PB |
8981 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
8982 | struct task_struct *task) | |
0f212204 | 8983 | { |
b52fda00 | 8984 | while (1) { |
f6edbabb | 8985 | struct io_task_cancel cancel = { .task = task, .files = NULL, }; |
0f212204 | 8986 | enum io_wq_cancel cret; |
b52fda00 | 8987 | bool ret = false; |
0f212204 | 8988 | |
90df0853 PB |
8989 | if (ctx->io_wq) { |
8990 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, | |
8991 | &cancel, true); | |
8992 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8993 | } | |
0f212204 JA |
8994 | |
8995 | /* SQPOLL thread does its own polling */ | |
8996 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8997 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8998 | io_iopoll_try_reap_events(ctx); | |
8999 | ret = true; | |
9000 | } | |
9001 | } | |
9002 | ||
6b81928d PB |
9003 | ret |= io_poll_remove_all(ctx, task, NULL); |
9004 | ret |= io_kill_timeouts(ctx, task, NULL); | |
55583d72 | 9005 | ret |= io_run_task_work(); |
b52fda00 PB |
9006 | if (!ret) |
9007 | break; | |
b52fda00 | 9008 | cond_resched(); |
0f212204 | 9009 | } |
0f212204 JA |
9010 | } |
9011 | ||
d9d05217 PB |
9012 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
9013 | { | |
d9d05217 PB |
9014 | mutex_lock(&ctx->uring_lock); |
9015 | ctx->sqo_dead = 1; | |
9016 | mutex_unlock(&ctx->uring_lock); | |
9017 | ||
9018 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
9019 | if (ctx->rings) |
9020 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
9021 | } |
9022 | ||
0f212204 JA |
9023 | /* |
9024 | * We need to iteratively cancel requests, in case a request has dependent | |
9025 | * hard links. These persist even for failure of cancelations, hence keep | |
9026 | * looping until none are found. | |
9027 | */ | |
9028 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
9029 | struct files_struct *files) | |
9030 | { | |
9031 | struct task_struct *task = current; | |
9032 | ||
fdaf083c | 9033 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 9034 | io_disable_sqo_submit(ctx); |
534ca6d6 | 9035 | task = ctx->sq_data->thread; |
fdaf083c JA |
9036 | atomic_inc(&task->io_uring->in_idle); |
9037 | io_sq_thread_park(ctx->sq_data); | |
9038 | } | |
0f212204 | 9039 | |
df9923f9 | 9040 | io_cancel_defer_files(ctx, task, files); |
0f212204 JA |
9041 | io_cqring_overflow_flush(ctx, true, task, files); |
9042 | ||
3a7efd1a | 9043 | io_uring_cancel_files(ctx, task, files); |
b52fda00 PB |
9044 | if (!files) |
9045 | __io_uring_cancel_task_requests(ctx, task); | |
fdaf083c JA |
9046 | |
9047 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { | |
9048 | atomic_dec(&task->io_uring->in_idle); | |
9049 | /* | |
9050 | * If the files that are going away are the ones in the thread | |
9051 | * identity, clear them out. | |
9052 | */ | |
9053 | if (task->io_uring->identity->files == files) | |
9054 | task->io_uring->identity->files = NULL; | |
9055 | io_sq_thread_unpark(ctx->sq_data); | |
9056 | } | |
0f212204 JA |
9057 | } |
9058 | ||
9059 | /* | |
9060 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9061 | */ | |
fdaf083c | 9062 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 9063 | { |
236434c3 | 9064 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 9065 | int ret; |
236434c3 MWO |
9066 | |
9067 | if (unlikely(!tctx)) { | |
0f212204 JA |
9068 | ret = io_uring_alloc_task_context(current); |
9069 | if (unlikely(ret)) | |
9070 | return ret; | |
236434c3 | 9071 | tctx = current->io_uring; |
0f212204 | 9072 | } |
236434c3 MWO |
9073 | if (tctx->last != file) { |
9074 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 9075 | |
236434c3 | 9076 | if (!old) { |
0f212204 | 9077 | get_file(file); |
a528b04e PB |
9078 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
9079 | file, GFP_KERNEL)); | |
9080 | if (ret) { | |
9081 | fput(file); | |
9082 | return ret; | |
9083 | } | |
ecfc8492 PB |
9084 | |
9085 | /* one and only SQPOLL file note, held by sqo_task */ | |
9086 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
9087 | current != ctx->sqo_task); | |
0f212204 | 9088 | } |
236434c3 | 9089 | tctx->last = file; |
0f212204 JA |
9090 | } |
9091 | ||
fdaf083c JA |
9092 | /* |
9093 | * This is race safe in that the task itself is doing this, hence it | |
9094 | * cannot be going through the exit/cancel paths at the same time. | |
9095 | * This cannot be modified while exit/cancel is running. | |
9096 | */ | |
9097 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
9098 | tctx->sqpoll = true; | |
9099 | ||
0f212204 JA |
9100 | return 0; |
9101 | } | |
9102 | ||
9103 | /* | |
9104 | * Remove this io_uring_file -> task mapping. | |
9105 | */ | |
9106 | static void io_uring_del_task_file(struct file *file) | |
9107 | { | |
9108 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
9109 | |
9110 | if (tctx->last == file) | |
9111 | tctx->last = NULL; | |
5e2ed8c4 | 9112 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
9113 | if (file) |
9114 | fput(file); | |
9115 | } | |
9116 | ||
de7f1d9e PB |
9117 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
9118 | { | |
9119 | struct file *file; | |
9120 | unsigned long index; | |
9121 | ||
9122 | xa_for_each(&tctx->xa, index, file) | |
9123 | io_uring_del_task_file(file); | |
9124 | } | |
9125 | ||
0f212204 JA |
9126 | void __io_uring_files_cancel(struct files_struct *files) |
9127 | { | |
9128 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
9129 | struct file *file; |
9130 | unsigned long index; | |
0f212204 JA |
9131 | |
9132 | /* make sure overflow events are dropped */ | |
fdaf083c | 9133 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
9134 | xa_for_each(&tctx->xa, index, file) |
9135 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 9136 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9137 | |
9138 | if (files) | |
9139 | io_uring_remove_task_files(tctx); | |
fdaf083c JA |
9140 | } |
9141 | ||
9142 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
9143 | { | |
9144 | unsigned long index; | |
9145 | struct file *file; | |
9146 | s64 inflight; | |
9147 | ||
9148 | inflight = percpu_counter_sum(&tctx->inflight); | |
9149 | if (!tctx->sqpoll) | |
9150 | return inflight; | |
9151 | ||
9152 | /* | |
9153 | * If we have SQPOLL rings, then we need to iterate and find them, and | |
9154 | * add the pending count for those. | |
9155 | */ | |
9156 | xa_for_each(&tctx->xa, index, file) { | |
9157 | struct io_ring_ctx *ctx = file->private_data; | |
9158 | ||
9159 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
9160 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; | |
9161 | ||
9162 | inflight += percpu_counter_sum(&__tctx->inflight); | |
9163 | } | |
9164 | } | |
9165 | ||
9166 | return inflight; | |
0f212204 JA |
9167 | } |
9168 | ||
0f212204 JA |
9169 | /* |
9170 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9171 | * requests. | |
9172 | */ | |
9173 | void __io_uring_task_cancel(void) | |
9174 | { | |
9175 | struct io_uring_task *tctx = current->io_uring; | |
9176 | DEFINE_WAIT(wait); | |
d8a6df10 | 9177 | s64 inflight; |
0f212204 JA |
9178 | |
9179 | /* make sure overflow events are dropped */ | |
fdaf083c | 9180 | atomic_inc(&tctx->in_idle); |
0f212204 | 9181 | |
0b5cd6c3 PB |
9182 | /* trigger io_disable_sqo_submit() */ |
9183 | if (tctx->sqpoll) | |
9184 | __io_uring_files_cancel(NULL); | |
9185 | ||
d8a6df10 | 9186 | do { |
0f212204 | 9187 | /* read completions before cancelations */ |
fdaf083c | 9188 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9189 | if (!inflight) |
9190 | break; | |
0f212204 JA |
9191 | __io_uring_files_cancel(NULL); |
9192 | ||
9193 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9194 | ||
9195 | /* | |
a1bb3cd5 PB |
9196 | * If we've seen completions, retry without waiting. This |
9197 | * avoids a race where a completion comes in before we did | |
9198 | * prepare_to_wait(). | |
0f212204 | 9199 | */ |
a1bb3cd5 PB |
9200 | if (inflight == tctx_inflight(tctx)) |
9201 | schedule(); | |
f57555ed | 9202 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9203 | } while (1); |
0f212204 | 9204 | |
fdaf083c | 9205 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
9206 | |
9207 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
9208 | } |
9209 | ||
fcb323cc JA |
9210 | static int io_uring_flush(struct file *file, void *data) |
9211 | { | |
6b5733eb | 9212 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 9213 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 9214 | |
84965ff8 JA |
9215 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
9216 | io_uring_cancel_task_requests(ctx, NULL); | |
9217 | ||
6b5733eb | 9218 | if (!tctx) |
4f793dc4 PB |
9219 | return 0; |
9220 | ||
6b5733eb PB |
9221 | /* we should have cancelled and erased it before PF_EXITING */ |
9222 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
9223 | xa_load(&tctx->xa, (unsigned long)file)); | |
9224 | ||
4f793dc4 PB |
9225 | /* |
9226 | * fput() is pending, will be 2 if the only other ref is our potential | |
9227 | * task file note. If the task is exiting, drop regardless of count. | |
9228 | */ | |
6b5733eb PB |
9229 | if (atomic_long_read(&file->f_count) != 2) |
9230 | return 0; | |
4f793dc4 | 9231 | |
d9d05217 PB |
9232 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
9233 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
9234 | WARN_ON_ONCE(ctx->sqo_task != current && |
9235 | xa_load(&tctx->xa, (unsigned long)file)); | |
9236 | /* sqo_dead check is for when this happens after cancellation */ | |
9237 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
9238 | !xa_load(&tctx->xa, (unsigned long)file)); |
9239 | ||
9240 | io_disable_sqo_submit(ctx); | |
9241 | } | |
9242 | ||
9243 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
9244 | io_uring_del_task_file(file); | |
fcb323cc JA |
9245 | return 0; |
9246 | } | |
9247 | ||
6c5c240e RP |
9248 | static void *io_uring_validate_mmap_request(struct file *file, |
9249 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9250 | { |
2b188cc1 | 9251 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9252 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9253 | struct page *page; |
9254 | void *ptr; | |
9255 | ||
9256 | switch (offset) { | |
9257 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9258 | case IORING_OFF_CQ_RING: |
9259 | ptr = ctx->rings; | |
2b188cc1 JA |
9260 | break; |
9261 | case IORING_OFF_SQES: | |
9262 | ptr = ctx->sq_sqes; | |
9263 | break; | |
2b188cc1 | 9264 | default: |
6c5c240e | 9265 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9266 | } |
9267 | ||
9268 | page = virt_to_head_page(ptr); | |
a50b854e | 9269 | if (sz > page_size(page)) |
6c5c240e RP |
9270 | return ERR_PTR(-EINVAL); |
9271 | ||
9272 | return ptr; | |
9273 | } | |
9274 | ||
9275 | #ifdef CONFIG_MMU | |
9276 | ||
9277 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9278 | { | |
9279 | size_t sz = vma->vm_end - vma->vm_start; | |
9280 | unsigned long pfn; | |
9281 | void *ptr; | |
9282 | ||
9283 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9284 | if (IS_ERR(ptr)) | |
9285 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9286 | |
9287 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9288 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9289 | } | |
9290 | ||
6c5c240e RP |
9291 | #else /* !CONFIG_MMU */ |
9292 | ||
9293 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9294 | { | |
9295 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9296 | } | |
9297 | ||
9298 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9299 | { | |
9300 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9301 | } | |
9302 | ||
9303 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9304 | unsigned long addr, unsigned long len, | |
9305 | unsigned long pgoff, unsigned long flags) | |
9306 | { | |
9307 | void *ptr; | |
9308 | ||
9309 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9310 | if (IS_ERR(ptr)) | |
9311 | return PTR_ERR(ptr); | |
9312 | ||
9313 | return (unsigned long) ptr; | |
9314 | } | |
9315 | ||
9316 | #endif /* !CONFIG_MMU */ | |
9317 | ||
d9d05217 | 9318 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9319 | { |
d9d05217 | 9320 | int ret = 0; |
90554200 JA |
9321 | DEFINE_WAIT(wait); |
9322 | ||
9323 | do { | |
9324 | if (!io_sqring_full(ctx)) | |
9325 | break; | |
9326 | ||
9327 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9328 | ||
d9d05217 PB |
9329 | if (unlikely(ctx->sqo_dead)) { |
9330 | ret = -EOWNERDEAD; | |
9331 | goto out; | |
9332 | } | |
9333 | ||
90554200 JA |
9334 | if (!io_sqring_full(ctx)) |
9335 | break; | |
9336 | ||
9337 | schedule(); | |
9338 | } while (!signal_pending(current)); | |
9339 | ||
9340 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9341 | out: |
9342 | return ret; | |
90554200 JA |
9343 | } |
9344 | ||
c73ebb68 HX |
9345 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9346 | struct __kernel_timespec __user **ts, | |
9347 | const sigset_t __user **sig) | |
9348 | { | |
9349 | struct io_uring_getevents_arg arg; | |
9350 | ||
9351 | /* | |
9352 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9353 | * is just a pointer to the sigset_t. | |
9354 | */ | |
9355 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9356 | *sig = (const sigset_t __user *) argp; | |
9357 | *ts = NULL; | |
9358 | return 0; | |
9359 | } | |
9360 | ||
9361 | /* | |
9362 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9363 | * timespec and sigset_t pointers if good. | |
9364 | */ | |
9365 | if (*argsz != sizeof(arg)) | |
9366 | return -EINVAL; | |
9367 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9368 | return -EFAULT; | |
9369 | *sig = u64_to_user_ptr(arg.sigmask); | |
9370 | *argsz = arg.sigmask_sz; | |
9371 | *ts = u64_to_user_ptr(arg.ts); | |
9372 | return 0; | |
9373 | } | |
9374 | ||
2b188cc1 | 9375 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9376 | u32, min_complete, u32, flags, const void __user *, argp, |
9377 | size_t, argsz) | |
2b188cc1 JA |
9378 | { |
9379 | struct io_ring_ctx *ctx; | |
9380 | long ret = -EBADF; | |
9381 | int submitted = 0; | |
9382 | struct fd f; | |
9383 | ||
4c6e277c | 9384 | io_run_task_work(); |
b41e9852 | 9385 | |
90554200 | 9386 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9387 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9388 | return -EINVAL; |
9389 | ||
9390 | f = fdget(fd); | |
9391 | if (!f.file) | |
9392 | return -EBADF; | |
9393 | ||
9394 | ret = -EOPNOTSUPP; | |
9395 | if (f.file->f_op != &io_uring_fops) | |
9396 | goto out_fput; | |
9397 | ||
9398 | ret = -ENXIO; | |
9399 | ctx = f.file->private_data; | |
9400 | if (!percpu_ref_tryget(&ctx->refs)) | |
9401 | goto out_fput; | |
9402 | ||
7e84e1c7 SG |
9403 | ret = -EBADFD; |
9404 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9405 | goto out; | |
9406 | ||
6c271ce2 JA |
9407 | /* |
9408 | * For SQ polling, the thread will do all submissions and completions. | |
9409 | * Just return the requested submit count, and wake the thread if | |
9410 | * we were asked to. | |
9411 | */ | |
b2a9eada | 9412 | ret = 0; |
6c271ce2 | 9413 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9414 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9415 | |
d9d05217 PB |
9416 | ret = -EOWNERDEAD; |
9417 | if (unlikely(ctx->sqo_dead)) | |
9418 | goto out; | |
6c271ce2 | 9419 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9420 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9421 | if (flags & IORING_ENTER_SQ_WAIT) { |
9422 | ret = io_sqpoll_wait_sq(ctx); | |
9423 | if (ret) | |
9424 | goto out; | |
9425 | } | |
6c271ce2 | 9426 | submitted = to_submit; |
b2a9eada | 9427 | } else if (to_submit) { |
fdaf083c | 9428 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9429 | if (unlikely(ret)) |
9430 | goto out; | |
2b188cc1 | 9431 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9432 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9433 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9434 | |
9435 | if (submitted != to_submit) | |
9436 | goto out; | |
2b188cc1 JA |
9437 | } |
9438 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9439 | const sigset_t __user *sig; |
9440 | struct __kernel_timespec __user *ts; | |
9441 | ||
9442 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9443 | if (unlikely(ret)) | |
9444 | goto out; | |
9445 | ||
2b188cc1 JA |
9446 | min_complete = min(min_complete, ctx->cq_entries); |
9447 | ||
32b2244a XW |
9448 | /* |
9449 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9450 | * space applications don't need to do io completion events | |
9451 | * polling again, they can rely on io_sq_thread to do polling | |
9452 | * work, which can reduce cpu usage and uring_lock contention. | |
9453 | */ | |
9454 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9455 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9456 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9457 | } else { |
c73ebb68 | 9458 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9459 | } |
2b188cc1 JA |
9460 | } |
9461 | ||
7c504e65 | 9462 | out: |
6805b32e | 9463 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9464 | out_fput: |
9465 | fdput(f); | |
9466 | return submitted ? submitted : ret; | |
9467 | } | |
9468 | ||
bebdb65e | 9469 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9470 | static int io_uring_show_cred(int id, void *p, void *data) |
9471 | { | |
6b47ab81 JA |
9472 | struct io_identity *iod = p; |
9473 | const struct cred *cred = iod->creds; | |
87ce955b JA |
9474 | struct seq_file *m = data; |
9475 | struct user_namespace *uns = seq_user_ns(m); | |
9476 | struct group_info *gi; | |
9477 | kernel_cap_t cap; | |
9478 | unsigned __capi; | |
9479 | int g; | |
9480 | ||
9481 | seq_printf(m, "%5d\n", id); | |
9482 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9483 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9484 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9485 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9486 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9487 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9488 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9489 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9490 | seq_puts(m, "\n\tGroups:\t"); | |
9491 | gi = cred->group_info; | |
9492 | for (g = 0; g < gi->ngroups; g++) { | |
9493 | seq_put_decimal_ull(m, g ? " " : "", | |
9494 | from_kgid_munged(uns, gi->gid[g])); | |
9495 | } | |
9496 | seq_puts(m, "\n\tCapEff:\t"); | |
9497 | cap = cred->cap_effective; | |
9498 | CAP_FOR_EACH_U32(__capi) | |
9499 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9500 | seq_putc(m, '\n'); | |
9501 | return 0; | |
9502 | } | |
9503 | ||
9504 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9505 | { | |
dbbe9c64 | 9506 | struct io_sq_data *sq = NULL; |
fad8e0de | 9507 | bool has_lock; |
87ce955b JA |
9508 | int i; |
9509 | ||
fad8e0de JA |
9510 | /* |
9511 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9512 | * since fdinfo case grabs it in the opposite direction of normal use | |
9513 | * cases. If we fail to get the lock, we just don't iterate any | |
9514 | * structures that could be going away outside the io_uring mutex. | |
9515 | */ | |
9516 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9517 | ||
dbbe9c64 JQ |
9518 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9519 | sq = ctx->sq_data; | |
9520 | ||
9521 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9522 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9523 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9524 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
269bbe5f | 9525 | struct fixed_rsrc_table *table; |
87ce955b JA |
9526 | struct file *f; |
9527 | ||
9528 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
9529 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
9530 | if (f) | |
9531 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9532 | else | |
9533 | seq_printf(m, "%5u: <none>\n", i); | |
9534 | } | |
9535 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9536 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9537 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9538 | ||
9539 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9540 | (unsigned int) buf->len); | |
9541 | } | |
fad8e0de | 9542 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9543 | seq_printf(m, "Personalities:\n"); |
9544 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9545 | } | |
d7718a9d JA |
9546 | seq_printf(m, "PollList:\n"); |
9547 | spin_lock_irq(&ctx->completion_lock); | |
9548 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9549 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9550 | struct io_kiocb *req; | |
9551 | ||
9552 | hlist_for_each_entry(req, list, hash_node) | |
9553 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9554 | req->task->task_works != NULL); | |
9555 | } | |
9556 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9557 | if (has_lock) |
9558 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9559 | } |
9560 | ||
9561 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9562 | { | |
9563 | struct io_ring_ctx *ctx = f->private_data; | |
9564 | ||
9565 | if (percpu_ref_tryget(&ctx->refs)) { | |
9566 | __io_uring_show_fdinfo(ctx, m); | |
9567 | percpu_ref_put(&ctx->refs); | |
9568 | } | |
9569 | } | |
bebdb65e | 9570 | #endif |
87ce955b | 9571 | |
2b188cc1 JA |
9572 | static const struct file_operations io_uring_fops = { |
9573 | .release = io_uring_release, | |
fcb323cc | 9574 | .flush = io_uring_flush, |
2b188cc1 | 9575 | .mmap = io_uring_mmap, |
6c5c240e RP |
9576 | #ifndef CONFIG_MMU |
9577 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9578 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9579 | #endif | |
2b188cc1 JA |
9580 | .poll = io_uring_poll, |
9581 | .fasync = io_uring_fasync, | |
bebdb65e | 9582 | #ifdef CONFIG_PROC_FS |
87ce955b | 9583 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9584 | #endif |
2b188cc1 JA |
9585 | }; |
9586 | ||
9587 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9588 | struct io_uring_params *p) | |
9589 | { | |
75b28aff HV |
9590 | struct io_rings *rings; |
9591 | size_t size, sq_array_offset; | |
2b188cc1 | 9592 | |
bd740481 JA |
9593 | /* make sure these are sane, as we already accounted them */ |
9594 | ctx->sq_entries = p->sq_entries; | |
9595 | ctx->cq_entries = p->cq_entries; | |
9596 | ||
75b28aff HV |
9597 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9598 | if (size == SIZE_MAX) | |
9599 | return -EOVERFLOW; | |
9600 | ||
9601 | rings = io_mem_alloc(size); | |
9602 | if (!rings) | |
2b188cc1 JA |
9603 | return -ENOMEM; |
9604 | ||
75b28aff HV |
9605 | ctx->rings = rings; |
9606 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9607 | rings->sq_ring_mask = p->sq_entries - 1; | |
9608 | rings->cq_ring_mask = p->cq_entries - 1; | |
9609 | rings->sq_ring_entries = p->sq_entries; | |
9610 | rings->cq_ring_entries = p->cq_entries; | |
9611 | ctx->sq_mask = rings->sq_ring_mask; | |
9612 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9613 | |
9614 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9615 | if (size == SIZE_MAX) { |
9616 | io_mem_free(ctx->rings); | |
9617 | ctx->rings = NULL; | |
2b188cc1 | 9618 | return -EOVERFLOW; |
eb065d30 | 9619 | } |
2b188cc1 JA |
9620 | |
9621 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9622 | if (!ctx->sq_sqes) { |
9623 | io_mem_free(ctx->rings); | |
9624 | ctx->rings = NULL; | |
2b188cc1 | 9625 | return -ENOMEM; |
eb065d30 | 9626 | } |
2b188cc1 | 9627 | |
2b188cc1 JA |
9628 | return 0; |
9629 | } | |
9630 | ||
9faadcc8 PB |
9631 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9632 | { | |
9633 | int ret, fd; | |
9634 | ||
9635 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9636 | if (fd < 0) | |
9637 | return fd; | |
9638 | ||
9639 | ret = io_uring_add_task_file(ctx, file); | |
9640 | if (ret) { | |
9641 | put_unused_fd(fd); | |
9642 | return ret; | |
9643 | } | |
9644 | fd_install(fd, file); | |
9645 | return fd; | |
9646 | } | |
9647 | ||
2b188cc1 JA |
9648 | /* |
9649 | * Allocate an anonymous fd, this is what constitutes the application | |
9650 | * visible backing of an io_uring instance. The application mmaps this | |
9651 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9652 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9653 | */ | |
9faadcc8 | 9654 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9655 | { |
9656 | struct file *file; | |
9faadcc8 | 9657 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9658 | int ret; |
9659 | ||
2b188cc1 JA |
9660 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9661 | &ctx->ring_sock); | |
9662 | if (ret) | |
9faadcc8 | 9663 | return ERR_PTR(ret); |
2b188cc1 JA |
9664 | #endif |
9665 | ||
2b188cc1 JA |
9666 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9667 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9668 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9669 | if (IS_ERR(file)) { |
9670 | sock_release(ctx->ring_sock); | |
9671 | ctx->ring_sock = NULL; | |
9672 | } else { | |
9673 | ctx->ring_sock->file = file; | |
0f212204 | 9674 | } |
2b188cc1 | 9675 | #endif |
9faadcc8 | 9676 | return file; |
2b188cc1 JA |
9677 | } |
9678 | ||
7f13657d XW |
9679 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9680 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9681 | { |
9682 | struct user_struct *user = NULL; | |
9683 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9684 | struct file *file; |
aad5d8da | 9685 | bool limit_mem; |
2b188cc1 JA |
9686 | int ret; |
9687 | ||
8110c1a6 | 9688 | if (!entries) |
2b188cc1 | 9689 | return -EINVAL; |
8110c1a6 JA |
9690 | if (entries > IORING_MAX_ENTRIES) { |
9691 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9692 | return -EINVAL; | |
9693 | entries = IORING_MAX_ENTRIES; | |
9694 | } | |
2b188cc1 JA |
9695 | |
9696 | /* | |
9697 | * Use twice as many entries for the CQ ring. It's possible for the | |
9698 | * application to drive a higher depth than the size of the SQ ring, | |
9699 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9700 | * some flexibility in overcommitting a bit. If the application has |
9701 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9702 | * of CQ ring entries manually. | |
2b188cc1 JA |
9703 | */ |
9704 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9705 | if (p->flags & IORING_SETUP_CQSIZE) { |
9706 | /* | |
9707 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9708 | * to a power-of-two, if it isn't already. We do NOT impose | |
9709 | * any cq vs sq ring sizing. | |
9710 | */ | |
eb2667b3 | 9711 | if (!p->cq_entries) |
33a107f0 | 9712 | return -EINVAL; |
8110c1a6 JA |
9713 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9714 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9715 | return -EINVAL; | |
9716 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9717 | } | |
eb2667b3 JQ |
9718 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9719 | if (p->cq_entries < p->sq_entries) | |
9720 | return -EINVAL; | |
33a107f0 JA |
9721 | } else { |
9722 | p->cq_entries = 2 * p->sq_entries; | |
9723 | } | |
2b188cc1 JA |
9724 | |
9725 | user = get_uid(current_user()); | |
aad5d8da | 9726 | limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9727 | |
aad5d8da | 9728 | if (limit_mem) { |
a087e2b5 | 9729 | ret = __io_account_mem(user, |
2b188cc1 JA |
9730 | ring_pages(p->sq_entries, p->cq_entries)); |
9731 | if (ret) { | |
9732 | free_uid(user); | |
9733 | return ret; | |
9734 | } | |
9735 | } | |
9736 | ||
9737 | ctx = io_ring_ctx_alloc(p); | |
9738 | if (!ctx) { | |
aad5d8da | 9739 | if (limit_mem) |
a087e2b5 | 9740 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
2b188cc1 JA |
9741 | p->cq_entries)); |
9742 | free_uid(user); | |
9743 | return -ENOMEM; | |
9744 | } | |
9745 | ctx->compat = in_compat_syscall(); | |
2b188cc1 | 9746 | ctx->user = user; |
0b8c0ec7 | 9747 | ctx->creds = get_current_cred(); |
4ea33a97 JA |
9748 | #ifdef CONFIG_AUDIT |
9749 | ctx->loginuid = current->loginuid; | |
9750 | ctx->sessionid = current->sessionid; | |
9751 | #endif | |
2aede0e4 JA |
9752 | ctx->sqo_task = get_task_struct(current); |
9753 | ||
9754 | /* | |
9755 | * This is just grabbed for accounting purposes. When a process exits, | |
9756 | * the mm is exited and dropped before the files, hence we need to hang | |
9757 | * on to this mm purely for the purposes of being able to unaccount | |
9758 | * memory (locked/pinned vm). It's not used for anything else. | |
9759 | */ | |
6b7898eb | 9760 | mmgrab(current->mm); |
2aede0e4 | 9761 | ctx->mm_account = current->mm; |
6b7898eb | 9762 | |
91d8f519 DZ |
9763 | #ifdef CONFIG_BLK_CGROUP |
9764 | /* | |
9765 | * The sq thread will belong to the original cgroup it was inited in. | |
9766 | * If the cgroup goes offline (e.g. disabling the io controller), then | |
9767 | * issued bios will be associated with the closest cgroup later in the | |
9768 | * block layer. | |
9769 | */ | |
9770 | rcu_read_lock(); | |
9771 | ctx->sqo_blkcg_css = blkcg_css(); | |
9772 | ret = css_tryget_online(ctx->sqo_blkcg_css); | |
9773 | rcu_read_unlock(); | |
9774 | if (!ret) { | |
9775 | /* don't init against a dying cgroup, have the user try again */ | |
9776 | ctx->sqo_blkcg_css = NULL; | |
9777 | ret = -ENODEV; | |
9778 | goto err; | |
9779 | } | |
9780 | #endif | |
6b7898eb | 9781 | |
f74441e6 JA |
9782 | /* |
9783 | * Account memory _before_ installing the file descriptor. Once | |
9784 | * the descriptor is installed, it can get closed at any time. Also | |
9785 | * do this before hitting the general error path, as ring freeing | |
9786 | * will un-account as well. | |
9787 | */ | |
9788 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), | |
9789 | ACCT_LOCKED); | |
9790 | ctx->limit_mem = limit_mem; | |
9791 | ||
2b188cc1 JA |
9792 | ret = io_allocate_scq_urings(ctx, p); |
9793 | if (ret) | |
9794 | goto err; | |
9795 | ||
7e84e1c7 | 9796 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9797 | if (ret) |
9798 | goto err; | |
9799 | ||
7e84e1c7 SG |
9800 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9801 | io_sq_offload_start(ctx); | |
9802 | ||
2b188cc1 | 9803 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9804 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9805 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9806 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9807 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9808 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9809 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9810 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9811 | |
9812 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9813 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9814 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9815 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9816 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9817 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9818 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9819 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9820 | |
7f13657d XW |
9821 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9822 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9823 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 HX |
9824 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9825 | IORING_FEAT_EXT_ARG; | |
7f13657d XW |
9826 | |
9827 | if (copy_to_user(params, p, sizeof(*p))) { | |
9828 | ret = -EFAULT; | |
9829 | goto err; | |
9830 | } | |
d1719f70 | 9831 | |
9faadcc8 PB |
9832 | file = io_uring_get_file(ctx); |
9833 | if (IS_ERR(file)) { | |
9834 | ret = PTR_ERR(file); | |
9835 | goto err; | |
9836 | } | |
9837 | ||
044c1ab3 JA |
9838 | /* |
9839 | * Install ring fd as the very last thing, so we don't risk someone | |
9840 | * having closed it before we finish setup | |
9841 | */ | |
9faadcc8 PB |
9842 | ret = io_uring_install_fd(ctx, file); |
9843 | if (ret < 0) { | |
06585c49 | 9844 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9845 | /* fput will clean it up */ |
9846 | fput(file); | |
9847 | return ret; | |
9848 | } | |
044c1ab3 | 9849 | |
c826bd7a | 9850 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9851 | return ret; |
9852 | err: | |
d9d05217 | 9853 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9854 | io_ring_ctx_wait_and_kill(ctx); |
9855 | return ret; | |
9856 | } | |
9857 | ||
9858 | /* | |
9859 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9860 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9861 | * params structure passed in. | |
9862 | */ | |
9863 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9864 | { | |
9865 | struct io_uring_params p; | |
2b188cc1 JA |
9866 | int i; |
9867 | ||
9868 | if (copy_from_user(&p, params, sizeof(p))) | |
9869 | return -EFAULT; | |
9870 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9871 | if (p.resv[i]) | |
9872 | return -EINVAL; | |
9873 | } | |
9874 | ||
6c271ce2 | 9875 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9876 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9877 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9878 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9879 | return -EINVAL; |
9880 | ||
7f13657d | 9881 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9882 | } |
9883 | ||
9884 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9885 | struct io_uring_params __user *, params) | |
9886 | { | |
9887 | return io_uring_setup(entries, params); | |
9888 | } | |
9889 | ||
66f4af93 JA |
9890 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9891 | { | |
9892 | struct io_uring_probe *p; | |
9893 | size_t size; | |
9894 | int i, ret; | |
9895 | ||
9896 | size = struct_size(p, ops, nr_args); | |
9897 | if (size == SIZE_MAX) | |
9898 | return -EOVERFLOW; | |
9899 | p = kzalloc(size, GFP_KERNEL); | |
9900 | if (!p) | |
9901 | return -ENOMEM; | |
9902 | ||
9903 | ret = -EFAULT; | |
9904 | if (copy_from_user(p, arg, size)) | |
9905 | goto out; | |
9906 | ret = -EINVAL; | |
9907 | if (memchr_inv(p, 0, size)) | |
9908 | goto out; | |
9909 | ||
9910 | p->last_op = IORING_OP_LAST - 1; | |
9911 | if (nr_args > IORING_OP_LAST) | |
9912 | nr_args = IORING_OP_LAST; | |
9913 | ||
9914 | for (i = 0; i < nr_args; i++) { | |
9915 | p->ops[i].op = i; | |
9916 | if (!io_op_defs[i].not_supported) | |
9917 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9918 | } | |
9919 | p->ops_len = i; | |
9920 | ||
9921 | ret = 0; | |
9922 | if (copy_to_user(arg, p, size)) | |
9923 | ret = -EFAULT; | |
9924 | out: | |
9925 | kfree(p); | |
9926 | return ret; | |
9927 | } | |
9928 | ||
071698e1 JA |
9929 | static int io_register_personality(struct io_ring_ctx *ctx) |
9930 | { | |
1e6fa521 JA |
9931 | struct io_identity *id; |
9932 | int ret; | |
071698e1 | 9933 | |
1e6fa521 JA |
9934 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
9935 | if (unlikely(!id)) | |
9936 | return -ENOMEM; | |
9937 | ||
9938 | io_init_identity(id); | |
9939 | id->creds = get_current_cred(); | |
9940 | ||
9941 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); | |
9942 | if (ret < 0) { | |
9943 | put_cred(id->creds); | |
9944 | kfree(id); | |
9945 | } | |
9946 | return ret; | |
071698e1 JA |
9947 | } |
9948 | ||
21b55dbc SG |
9949 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9950 | unsigned int nr_args) | |
9951 | { | |
9952 | struct io_uring_restriction *res; | |
9953 | size_t size; | |
9954 | int i, ret; | |
9955 | ||
7e84e1c7 SG |
9956 | /* Restrictions allowed only if rings started disabled */ |
9957 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9958 | return -EBADFD; | |
9959 | ||
21b55dbc | 9960 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9961 | if (ctx->restrictions.registered) |
21b55dbc SG |
9962 | return -EBUSY; |
9963 | ||
9964 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9965 | return -EINVAL; | |
9966 | ||
9967 | size = array_size(nr_args, sizeof(*res)); | |
9968 | if (size == SIZE_MAX) | |
9969 | return -EOVERFLOW; | |
9970 | ||
9971 | res = memdup_user(arg, size); | |
9972 | if (IS_ERR(res)) | |
9973 | return PTR_ERR(res); | |
9974 | ||
9975 | ret = 0; | |
9976 | ||
9977 | for (i = 0; i < nr_args; i++) { | |
9978 | switch (res[i].opcode) { | |
9979 | case IORING_RESTRICTION_REGISTER_OP: | |
9980 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9981 | ret = -EINVAL; | |
9982 | goto out; | |
9983 | } | |
9984 | ||
9985 | __set_bit(res[i].register_op, | |
9986 | ctx->restrictions.register_op); | |
9987 | break; | |
9988 | case IORING_RESTRICTION_SQE_OP: | |
9989 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9990 | ret = -EINVAL; | |
9991 | goto out; | |
9992 | } | |
9993 | ||
9994 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9995 | break; | |
9996 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9997 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9998 | break; | |
9999 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10000 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10001 | break; | |
10002 | default: | |
10003 | ret = -EINVAL; | |
10004 | goto out; | |
10005 | } | |
10006 | } | |
10007 | ||
10008 | out: | |
10009 | /* Reset all restrictions if an error happened */ | |
10010 | if (ret != 0) | |
10011 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10012 | else | |
7e84e1c7 | 10013 | ctx->restrictions.registered = true; |
21b55dbc SG |
10014 | |
10015 | kfree(res); | |
10016 | return ret; | |
10017 | } | |
10018 | ||
7e84e1c7 SG |
10019 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10020 | { | |
10021 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10022 | return -EBADFD; | |
10023 | ||
10024 | if (ctx->restrictions.registered) | |
10025 | ctx->restricted = 1; | |
10026 | ||
10027 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
10028 | ||
10029 | io_sq_offload_start(ctx); | |
10030 | ||
10031 | return 0; | |
10032 | } | |
10033 | ||
071698e1 JA |
10034 | static bool io_register_op_must_quiesce(int op) |
10035 | { | |
10036 | switch (op) { | |
10037 | case IORING_UNREGISTER_FILES: | |
10038 | case IORING_REGISTER_FILES_UPDATE: | |
10039 | case IORING_REGISTER_PROBE: | |
10040 | case IORING_REGISTER_PERSONALITY: | |
10041 | case IORING_UNREGISTER_PERSONALITY: | |
10042 | return false; | |
10043 | default: | |
10044 | return true; | |
10045 | } | |
10046 | } | |
10047 | ||
edafccee JA |
10048 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10049 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10050 | __releases(ctx->uring_lock) |
10051 | __acquires(ctx->uring_lock) | |
edafccee JA |
10052 | { |
10053 | int ret; | |
10054 | ||
35fa71a0 JA |
10055 | /* |
10056 | * We're inside the ring mutex, if the ref is already dying, then | |
10057 | * someone else killed the ctx or is already going through | |
10058 | * io_uring_register(). | |
10059 | */ | |
10060 | if (percpu_ref_is_dying(&ctx->refs)) | |
10061 | return -ENXIO; | |
10062 | ||
071698e1 | 10063 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10064 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10065 | |
05f3fb3c JA |
10066 | /* |
10067 | * Drop uring mutex before waiting for references to exit. If | |
10068 | * another thread is currently inside io_uring_enter() it might | |
10069 | * need to grab the uring_lock to make progress. If we hold it | |
10070 | * here across the drain wait, then we can deadlock. It's safe | |
10071 | * to drop the mutex here, since no new references will come in | |
10072 | * after we've killed the percpu ref. | |
10073 | */ | |
10074 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10075 | do { |
10076 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10077 | if (!ret) | |
10078 | break; | |
ed6930c9 JA |
10079 | ret = io_run_task_work_sig(); |
10080 | if (ret < 0) | |
10081 | break; | |
af9c1a44 JA |
10082 | } while (1); |
10083 | ||
05f3fb3c | 10084 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10085 | |
c150368b JA |
10086 | if (ret) { |
10087 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
10088 | goto out_quiesce; |
10089 | } | |
10090 | } | |
10091 | ||
10092 | if (ctx->restricted) { | |
10093 | if (opcode >= IORING_REGISTER_LAST) { | |
10094 | ret = -EINVAL; | |
10095 | goto out; | |
10096 | } | |
10097 | ||
10098 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
10099 | ret = -EACCES; | |
c150368b JA |
10100 | goto out; |
10101 | } | |
05f3fb3c | 10102 | } |
edafccee JA |
10103 | |
10104 | switch (opcode) { | |
10105 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 10106 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
10107 | break; |
10108 | case IORING_UNREGISTER_BUFFERS: | |
10109 | ret = -EINVAL; | |
10110 | if (arg || nr_args) | |
10111 | break; | |
0a96bbe4 | 10112 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10113 | break; |
6b06314c JA |
10114 | case IORING_REGISTER_FILES: |
10115 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
10116 | break; | |
10117 | case IORING_UNREGISTER_FILES: | |
10118 | ret = -EINVAL; | |
10119 | if (arg || nr_args) | |
10120 | break; | |
10121 | ret = io_sqe_files_unregister(ctx); | |
10122 | break; | |
c3a31e60 JA |
10123 | case IORING_REGISTER_FILES_UPDATE: |
10124 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
10125 | break; | |
9b402849 | 10126 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10127 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10128 | ret = -EINVAL; |
10129 | if (nr_args != 1) | |
10130 | break; | |
10131 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10132 | if (ret) |
10133 | break; | |
10134 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10135 | ctx->eventfd_async = 1; | |
10136 | else | |
10137 | ctx->eventfd_async = 0; | |
9b402849 JA |
10138 | break; |
10139 | case IORING_UNREGISTER_EVENTFD: | |
10140 | ret = -EINVAL; | |
10141 | if (arg || nr_args) | |
10142 | break; | |
10143 | ret = io_eventfd_unregister(ctx); | |
10144 | break; | |
66f4af93 JA |
10145 | case IORING_REGISTER_PROBE: |
10146 | ret = -EINVAL; | |
10147 | if (!arg || nr_args > 256) | |
10148 | break; | |
10149 | ret = io_probe(ctx, arg, nr_args); | |
10150 | break; | |
071698e1 JA |
10151 | case IORING_REGISTER_PERSONALITY: |
10152 | ret = -EINVAL; | |
10153 | if (arg || nr_args) | |
10154 | break; | |
10155 | ret = io_register_personality(ctx); | |
10156 | break; | |
10157 | case IORING_UNREGISTER_PERSONALITY: | |
10158 | ret = -EINVAL; | |
10159 | if (arg) | |
10160 | break; | |
10161 | ret = io_unregister_personality(ctx, nr_args); | |
10162 | break; | |
7e84e1c7 SG |
10163 | case IORING_REGISTER_ENABLE_RINGS: |
10164 | ret = -EINVAL; | |
10165 | if (arg || nr_args) | |
10166 | break; | |
10167 | ret = io_register_enable_rings(ctx); | |
10168 | break; | |
21b55dbc SG |
10169 | case IORING_REGISTER_RESTRICTIONS: |
10170 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10171 | break; | |
edafccee JA |
10172 | default: |
10173 | ret = -EINVAL; | |
10174 | break; | |
10175 | } | |
10176 | ||
21b55dbc | 10177 | out: |
071698e1 | 10178 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10179 | /* bring the ctx back to life */ |
05f3fb3c | 10180 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 10181 | out_quiesce: |
0f158b4c | 10182 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10183 | } |
edafccee JA |
10184 | return ret; |
10185 | } | |
10186 | ||
10187 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10188 | void __user *, arg, unsigned int, nr_args) | |
10189 | { | |
10190 | struct io_ring_ctx *ctx; | |
10191 | long ret = -EBADF; | |
10192 | struct fd f; | |
10193 | ||
10194 | f = fdget(fd); | |
10195 | if (!f.file) | |
10196 | return -EBADF; | |
10197 | ||
10198 | ret = -EOPNOTSUPP; | |
10199 | if (f.file->f_op != &io_uring_fops) | |
10200 | goto out_fput; | |
10201 | ||
10202 | ctx = f.file->private_data; | |
10203 | ||
10204 | mutex_lock(&ctx->uring_lock); | |
10205 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10206 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10207 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10208 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10209 | out_fput: |
10210 | fdput(f); | |
10211 | return ret; | |
10212 | } | |
10213 | ||
2b188cc1 JA |
10214 | static int __init io_uring_init(void) |
10215 | { | |
d7f62e82 SM |
10216 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10217 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10218 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10219 | } while (0) | |
10220 | ||
10221 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10222 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10223 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10224 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10225 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10226 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10227 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10228 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10229 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10230 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10231 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10232 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10233 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10234 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10235 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10236 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10237 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10238 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10239 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10240 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10241 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10242 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10243 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10244 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10245 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10246 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10247 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10248 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10249 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10250 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10251 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10252 | |
d3656344 | 10253 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10254 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
2b188cc1 JA |
10255 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
10256 | return 0; | |
10257 | }; | |
10258 | __initcall(io_uring_init); |