]>
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> | |
2b188cc1 | 60 | #include <linux/blkdev.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
2b188cc1 JA |
66 | #include <linux/anon_inodes.h> |
67 | #include <linux/sched/mm.h> | |
68 | #include <linux/uaccess.h> | |
69 | #include <linux/nospec.h> | |
edafccee JA |
70 | #include <linux/sizes.h> |
71 | #include <linux/hugetlb.h> | |
aa4c3967 | 72 | #include <linux/highmem.h> |
15b71abe JA |
73 | #include <linux/namei.h> |
74 | #include <linux/fsnotify.h> | |
4840e418 | 75 | #include <linux/fadvise.h> |
3e4827b0 | 76 | #include <linux/eventpoll.h> |
ff002b30 | 77 | #include <linux/fs_struct.h> |
7d67af2c | 78 | #include <linux/splice.h> |
b41e9852 | 79 | #include <linux/task_work.h> |
bcf5a063 | 80 | #include <linux/pagemap.h> |
0f212204 | 81 | #include <linux/io_uring.h> |
91d8f519 | 82 | #include <linux/blk-cgroup.h> |
4ea33a97 | 83 | #include <linux/audit.h> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
95 | |
96 | /* | |
97 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
98 | */ | |
99 | #define IORING_FILE_TABLE_SHIFT 9 | |
100 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
101 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
102 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
103 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
104 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 105 | |
b16fed66 PB |
106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
108 | IOSQE_BUFFER_SELECT) | |
109 | ||
2b188cc1 JA |
110 | struct io_uring { |
111 | u32 head ____cacheline_aligned_in_smp; | |
112 | u32 tail ____cacheline_aligned_in_smp; | |
113 | }; | |
114 | ||
1e84b97b | 115 | /* |
75b28aff HV |
116 | * This data is shared with the application through the mmap at offsets |
117 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
118 | * |
119 | * The offsets to the member fields are published through struct | |
120 | * io_sqring_offsets when calling io_uring_setup. | |
121 | */ | |
75b28aff | 122 | struct io_rings { |
1e84b97b SB |
123 | /* |
124 | * Head and tail offsets into the ring; the offsets need to be | |
125 | * masked to get valid indices. | |
126 | * | |
75b28aff HV |
127 | * The kernel controls head of the sq ring and the tail of the cq ring, |
128 | * and the application controls tail of the sq ring and the head of the | |
129 | * cq ring. | |
1e84b97b | 130 | */ |
75b28aff | 131 | struct io_uring sq, cq; |
1e84b97b | 132 | /* |
75b28aff | 133 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
134 | * ring_entries - 1) |
135 | */ | |
75b28aff HV |
136 | u32 sq_ring_mask, cq_ring_mask; |
137 | /* Ring sizes (constant, power of 2) */ | |
138 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
139 | /* |
140 | * Number of invalid entries dropped by the kernel due to | |
141 | * invalid index stored in array | |
142 | * | |
143 | * Written by the kernel, shouldn't be modified by the | |
144 | * application (i.e. get number of "new events" by comparing to | |
145 | * cached value). | |
146 | * | |
147 | * After a new SQ head value was read by the application this | |
148 | * counter includes all submissions that were dropped reaching | |
149 | * the new SQ head (and possibly more). | |
150 | */ | |
75b28aff | 151 | u32 sq_dropped; |
1e84b97b | 152 | /* |
0d9b5b3a | 153 | * Runtime SQ flags |
1e84b97b SB |
154 | * |
155 | * Written by the kernel, shouldn't be modified by the | |
156 | * application. | |
157 | * | |
158 | * The application needs a full memory barrier before checking | |
159 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
160 | */ | |
75b28aff | 161 | u32 sq_flags; |
0d9b5b3a SG |
162 | /* |
163 | * Runtime CQ flags | |
164 | * | |
165 | * Written by the application, shouldn't be modified by the | |
166 | * kernel. | |
167 | */ | |
168 | u32 cq_flags; | |
1e84b97b SB |
169 | /* |
170 | * Number of completion events lost because the queue was full; | |
171 | * this should be avoided by the application by making sure | |
0b4295b5 | 172 | * there are not more requests pending than there is space in |
1e84b97b SB |
173 | * the completion queue. |
174 | * | |
175 | * Written by the kernel, shouldn't be modified by the | |
176 | * application (i.e. get number of "new events" by comparing to | |
177 | * cached value). | |
178 | * | |
179 | * As completion events come in out of order this counter is not | |
180 | * ordered with any other data. | |
181 | */ | |
75b28aff | 182 | u32 cq_overflow; |
1e84b97b SB |
183 | /* |
184 | * Ring buffer of completion events. | |
185 | * | |
186 | * The kernel writes completion events fresh every time they are | |
187 | * produced, so the application is allowed to modify pending | |
188 | * entries. | |
189 | */ | |
75b28aff | 190 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
191 | }; |
192 | ||
45d189c6 PB |
193 | enum io_uring_cmd_flags { |
194 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 195 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
196 | }; |
197 | ||
edafccee JA |
198 | struct io_mapped_ubuf { |
199 | u64 ubuf; | |
200 | size_t len; | |
201 | struct bio_vec *bvec; | |
202 | unsigned int nr_bvecs; | |
de293938 | 203 | unsigned long acct_pages; |
edafccee JA |
204 | }; |
205 | ||
50238531 BM |
206 | struct io_ring_ctx; |
207 | ||
269bbe5f BM |
208 | struct io_rsrc_put { |
209 | struct list_head list; | |
50238531 BM |
210 | union { |
211 | void *rsrc; | |
212 | struct file *file; | |
213 | }; | |
269bbe5f BM |
214 | }; |
215 | ||
216 | struct fixed_rsrc_table { | |
65e19f54 | 217 | struct file **files; |
31b51510 JA |
218 | }; |
219 | ||
269bbe5f | 220 | struct fixed_rsrc_ref_node { |
05589553 XW |
221 | struct percpu_ref refs; |
222 | struct list_head node; | |
269bbe5f BM |
223 | struct list_head rsrc_list; |
224 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
225 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
226 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 227 | struct llist_node llist; |
e297822b | 228 | bool done; |
05589553 XW |
229 | }; |
230 | ||
269bbe5f BM |
231 | struct fixed_rsrc_data { |
232 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
233 | struct io_ring_ctx *ctx; |
234 | ||
269bbe5f | 235 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 236 | struct percpu_ref refs; |
05f3fb3c | 237 | struct completion done; |
8bad28d8 | 238 | bool quiesce; |
05f3fb3c JA |
239 | }; |
240 | ||
5a2e745d JA |
241 | struct io_buffer { |
242 | struct list_head list; | |
243 | __u64 addr; | |
244 | __s32 len; | |
245 | __u16 bid; | |
246 | }; | |
247 | ||
21b55dbc SG |
248 | struct io_restriction { |
249 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
250 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
251 | u8 sqe_flags_allowed; | |
252 | u8 sqe_flags_required; | |
7e84e1c7 | 253 | bool registered; |
21b55dbc SG |
254 | }; |
255 | ||
37d1e2e3 JA |
256 | enum { |
257 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
258 | IO_SQ_THREAD_SHOULD_PARK, | |
259 | }; | |
260 | ||
534ca6d6 JA |
261 | struct io_sq_data { |
262 | refcount_t refs; | |
69fb2131 JA |
263 | struct mutex lock; |
264 | ||
265 | /* ctx's that are using this sqd */ | |
266 | struct list_head ctx_list; | |
267 | struct list_head ctx_new_list; | |
268 | struct mutex ctx_lock; | |
269 | ||
534ca6d6 JA |
270 | struct task_struct *thread; |
271 | struct wait_queue_head wait; | |
08369246 XW |
272 | |
273 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
274 | int sq_cpu; |
275 | pid_t task_pid; | |
276 | ||
277 | unsigned long state; | |
278 | struct completion startup; | |
279 | struct completion completion; | |
280 | struct completion exited; | |
534ca6d6 JA |
281 | }; |
282 | ||
258b29a9 | 283 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 284 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 285 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 286 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
287 | |
288 | struct io_comp_state { | |
6dd0be1e | 289 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 290 | unsigned int nr; |
c7dae4ba JA |
291 | unsigned int locked_free_nr; |
292 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 293 | struct list_head free_list; |
c7dae4ba JA |
294 | /* IRQ completion list, under ->completion_lock */ |
295 | struct list_head locked_free_list; | |
258b29a9 PB |
296 | }; |
297 | ||
a1ab7b35 PB |
298 | struct io_submit_link { |
299 | struct io_kiocb *head; | |
300 | struct io_kiocb *last; | |
301 | }; | |
302 | ||
258b29a9 PB |
303 | struct io_submit_state { |
304 | struct blk_plug plug; | |
a1ab7b35 | 305 | struct io_submit_link link; |
258b29a9 PB |
306 | |
307 | /* | |
308 | * io_kiocb alloc cache | |
309 | */ | |
bf019da7 | 310 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
311 | unsigned int free_reqs; |
312 | ||
313 | bool plug_started; | |
314 | ||
315 | /* | |
316 | * Batch completion logic | |
317 | */ | |
318 | struct io_comp_state comp; | |
319 | ||
320 | /* | |
321 | * File reference cache | |
322 | */ | |
323 | struct file *file; | |
324 | unsigned int fd; | |
325 | unsigned int file_refs; | |
326 | unsigned int ios_left; | |
327 | }; | |
328 | ||
2b188cc1 JA |
329 | struct io_ring_ctx { |
330 | struct { | |
331 | struct percpu_ref refs; | |
332 | } ____cacheline_aligned_in_smp; | |
333 | ||
334 | struct { | |
335 | unsigned int flags; | |
e1d85334 | 336 | unsigned int compat: 1; |
aad5d8da | 337 | unsigned int limit_mem: 1; |
e1d85334 RD |
338 | unsigned int cq_overflow_flushed: 1; |
339 | unsigned int drain_next: 1; | |
340 | unsigned int eventfd_async: 1; | |
21b55dbc | 341 | unsigned int restricted: 1; |
d9d05217 | 342 | unsigned int sqo_dead: 1; |
2b188cc1 | 343 | |
75b28aff HV |
344 | /* |
345 | * Ring buffer of indices into array of io_uring_sqe, which is | |
346 | * mmapped by the application using the IORING_OFF_SQES offset. | |
347 | * | |
348 | * This indirection could e.g. be used to assign fixed | |
349 | * io_uring_sqe entries to operations and only submit them to | |
350 | * the queue when needed. | |
351 | * | |
352 | * The kernel modifies neither the indices array nor the entries | |
353 | * array. | |
354 | */ | |
355 | u32 *sq_array; | |
2b188cc1 JA |
356 | unsigned cached_sq_head; |
357 | unsigned sq_entries; | |
358 | unsigned sq_mask; | |
6c271ce2 | 359 | unsigned sq_thread_idle; |
498ccd9e | 360 | unsigned cached_sq_dropped; |
2c3bac6d | 361 | unsigned cached_cq_overflow; |
ad3eb2c8 | 362 | unsigned long sq_check_overflow; |
de0617e4 JA |
363 | |
364 | struct list_head defer_list; | |
5262f567 | 365 | struct list_head timeout_list; |
1d7bb1d5 | 366 | struct list_head cq_overflow_list; |
fcb323cc | 367 | |
ad3eb2c8 | 368 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
369 | } ____cacheline_aligned_in_smp; |
370 | ||
3c1a2ead JA |
371 | struct { |
372 | struct mutex uring_lock; | |
373 | wait_queue_head_t wait; | |
374 | } ____cacheline_aligned_in_smp; | |
375 | ||
376 | struct io_submit_state submit_state; | |
377 | ||
206aefde JA |
378 | struct io_rings *rings; |
379 | ||
2aede0e4 | 380 | /* |
37d1e2e3 | 381 | * For SQPOLL usage |
2aede0e4 JA |
382 | */ |
383 | struct task_struct *sqo_task; | |
384 | ||
385 | /* Only used for accounting purposes */ | |
386 | struct mm_struct *mm_account; | |
387 | ||
534ca6d6 JA |
388 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
389 | ||
90554200 | 390 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 391 | struct list_head sqd_list; |
75b28aff | 392 | |
6b06314c JA |
393 | /* |
394 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
395 | * readers must ensure that ->refs is alive as long as the file* is | |
396 | * used. Only updated through io_uring_register(2). | |
397 | */ | |
269bbe5f | 398 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
399 | unsigned nr_user_files; |
400 | ||
edafccee JA |
401 | /* if used, fixed mapped user buffers */ |
402 | unsigned nr_user_bufs; | |
403 | struct io_mapped_ubuf *user_bufs; | |
404 | ||
2b188cc1 JA |
405 | struct user_struct *user; |
406 | ||
0f158b4c JA |
407 | struct completion ref_comp; |
408 | struct completion sq_thread_comp; | |
206aefde JA |
409 | |
410 | #if defined(CONFIG_UNIX) | |
411 | struct socket *ring_sock; | |
412 | #endif | |
413 | ||
5a2e745d JA |
414 | struct idr io_buffer_idr; |
415 | ||
071698e1 JA |
416 | struct idr personality_idr; |
417 | ||
206aefde JA |
418 | struct { |
419 | unsigned cached_cq_tail; | |
420 | unsigned cq_entries; | |
421 | unsigned cq_mask; | |
422 | atomic_t cq_timeouts; | |
f010505b | 423 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 424 | unsigned long cq_check_overflow; |
206aefde JA |
425 | struct wait_queue_head cq_wait; |
426 | struct fasync_struct *cq_fasync; | |
427 | struct eventfd_ctx *cq_ev_fd; | |
428 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 429 | |
2b188cc1 JA |
430 | struct { |
431 | spinlock_t completion_lock; | |
e94f141b | 432 | |
def596e9 | 433 | /* |
540e32a0 | 434 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
435 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
436 | * For SQPOLL, only the single threaded io_sq_thread() will | |
437 | * manipulate the list, hence no extra locking is needed there. | |
438 | */ | |
540e32a0 | 439 | struct list_head iopoll_list; |
78076bb6 JA |
440 | struct hlist_head *cancel_hash; |
441 | unsigned cancel_hash_bits; | |
e94f141b | 442 | bool poll_multi_file; |
31b51510 | 443 | |
fcb323cc JA |
444 | spinlock_t inflight_lock; |
445 | struct list_head inflight_list; | |
2b188cc1 | 446 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 447 | |
269bbe5f BM |
448 | struct delayed_work rsrc_put_work; |
449 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
450 | struct list_head rsrc_ref_list; |
451 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 452 | |
21b55dbc | 453 | struct io_restriction restrictions; |
3c1a2ead | 454 | |
7c25c0d1 JA |
455 | /* exit task_work */ |
456 | struct callback_head *exit_task_work; | |
457 | ||
3c1a2ead JA |
458 | /* Keep this last, we don't need it for the fast path */ |
459 | struct work_struct exit_work; | |
2b188cc1 JA |
460 | }; |
461 | ||
09bb8394 JA |
462 | /* |
463 | * First field must be the file pointer in all the | |
464 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
465 | */ | |
221c5eb2 JA |
466 | struct io_poll_iocb { |
467 | struct file *file; | |
018043be | 468 | struct wait_queue_head *head; |
221c5eb2 | 469 | __poll_t events; |
8c838788 | 470 | bool done; |
221c5eb2 | 471 | bool canceled; |
392edb45 | 472 | struct wait_queue_entry wait; |
221c5eb2 JA |
473 | }; |
474 | ||
018043be PB |
475 | struct io_poll_remove { |
476 | struct file *file; | |
477 | u64 addr; | |
478 | }; | |
479 | ||
b5dba59e JA |
480 | struct io_close { |
481 | struct file *file; | |
b5dba59e JA |
482 | int fd; |
483 | }; | |
484 | ||
ad8a48ac JA |
485 | struct io_timeout_data { |
486 | struct io_kiocb *req; | |
487 | struct hrtimer timer; | |
488 | struct timespec64 ts; | |
489 | enum hrtimer_mode mode; | |
490 | }; | |
491 | ||
8ed8d3c3 JA |
492 | struct io_accept { |
493 | struct file *file; | |
494 | struct sockaddr __user *addr; | |
495 | int __user *addr_len; | |
496 | int flags; | |
09952e3e | 497 | unsigned long nofile; |
8ed8d3c3 JA |
498 | }; |
499 | ||
500 | struct io_sync { | |
501 | struct file *file; | |
502 | loff_t len; | |
503 | loff_t off; | |
504 | int flags; | |
d63d1b5e | 505 | int mode; |
8ed8d3c3 JA |
506 | }; |
507 | ||
fbf23849 JA |
508 | struct io_cancel { |
509 | struct file *file; | |
510 | u64 addr; | |
511 | }; | |
512 | ||
b29472ee JA |
513 | struct io_timeout { |
514 | struct file *file; | |
bfe68a22 PB |
515 | u32 off; |
516 | u32 target_seq; | |
135fcde8 | 517 | struct list_head list; |
90cd7e42 PB |
518 | /* head of the link, used by linked timeouts only */ |
519 | struct io_kiocb *head; | |
b29472ee JA |
520 | }; |
521 | ||
0bdf7a2d PB |
522 | struct io_timeout_rem { |
523 | struct file *file; | |
524 | u64 addr; | |
9c8e11b3 PB |
525 | |
526 | /* timeout update */ | |
527 | struct timespec64 ts; | |
528 | u32 flags; | |
0bdf7a2d PB |
529 | }; |
530 | ||
9adbd45d JA |
531 | struct io_rw { |
532 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
533 | struct kiocb kiocb; | |
534 | u64 addr; | |
535 | u64 len; | |
536 | }; | |
537 | ||
3fbb51c1 JA |
538 | struct io_connect { |
539 | struct file *file; | |
540 | struct sockaddr __user *addr; | |
541 | int addr_len; | |
542 | }; | |
543 | ||
e47293fd JA |
544 | struct io_sr_msg { |
545 | struct file *file; | |
fddaface | 546 | union { |
270a5940 | 547 | struct user_msghdr __user *umsg; |
fddaface JA |
548 | void __user *buf; |
549 | }; | |
e47293fd | 550 | int msg_flags; |
bcda7baa | 551 | int bgid; |
fddaface | 552 | size_t len; |
bcda7baa | 553 | struct io_buffer *kbuf; |
e47293fd JA |
554 | }; |
555 | ||
15b71abe JA |
556 | struct io_open { |
557 | struct file *file; | |
558 | int dfd; | |
15b71abe | 559 | struct filename *filename; |
c12cedf2 | 560 | struct open_how how; |
4022e7af | 561 | unsigned long nofile; |
15b71abe JA |
562 | }; |
563 | ||
269bbe5f | 564 | struct io_rsrc_update { |
05f3fb3c JA |
565 | struct file *file; |
566 | u64 arg; | |
567 | u32 nr_args; | |
568 | u32 offset; | |
569 | }; | |
570 | ||
4840e418 JA |
571 | struct io_fadvise { |
572 | struct file *file; | |
573 | u64 offset; | |
574 | u32 len; | |
575 | u32 advice; | |
576 | }; | |
577 | ||
c1ca757b JA |
578 | struct io_madvise { |
579 | struct file *file; | |
580 | u64 addr; | |
581 | u32 len; | |
582 | u32 advice; | |
583 | }; | |
584 | ||
3e4827b0 JA |
585 | struct io_epoll { |
586 | struct file *file; | |
587 | int epfd; | |
588 | int op; | |
589 | int fd; | |
590 | struct epoll_event event; | |
e47293fd JA |
591 | }; |
592 | ||
7d67af2c PB |
593 | struct io_splice { |
594 | struct file *file_out; | |
595 | struct file *file_in; | |
596 | loff_t off_out; | |
597 | loff_t off_in; | |
598 | u64 len; | |
599 | unsigned int flags; | |
600 | }; | |
601 | ||
ddf0322d JA |
602 | struct io_provide_buf { |
603 | struct file *file; | |
604 | __u64 addr; | |
605 | __s32 len; | |
606 | __u32 bgid; | |
607 | __u16 nbufs; | |
608 | __u16 bid; | |
609 | }; | |
610 | ||
1d9e1288 BM |
611 | struct io_statx { |
612 | struct file *file; | |
613 | int dfd; | |
614 | unsigned int mask; | |
615 | unsigned int flags; | |
e62753e4 | 616 | const char __user *filename; |
1d9e1288 BM |
617 | struct statx __user *buffer; |
618 | }; | |
619 | ||
36f4fa68 JA |
620 | struct io_shutdown { |
621 | struct file *file; | |
622 | int how; | |
623 | }; | |
624 | ||
80a261fd JA |
625 | struct io_rename { |
626 | struct file *file; | |
627 | int old_dfd; | |
628 | int new_dfd; | |
629 | struct filename *oldpath; | |
630 | struct filename *newpath; | |
631 | int flags; | |
632 | }; | |
633 | ||
14a1143b JA |
634 | struct io_unlink { |
635 | struct file *file; | |
636 | int dfd; | |
637 | int flags; | |
638 | struct filename *filename; | |
639 | }; | |
640 | ||
3ca405eb PB |
641 | struct io_completion { |
642 | struct file *file; | |
643 | struct list_head list; | |
0f7e466b | 644 | int cflags; |
3ca405eb PB |
645 | }; |
646 | ||
f499a021 JA |
647 | struct io_async_connect { |
648 | struct sockaddr_storage address; | |
649 | }; | |
650 | ||
03b1230c JA |
651 | struct io_async_msghdr { |
652 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
653 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
654 | struct iovec *free_iov; | |
03b1230c JA |
655 | struct sockaddr __user *uaddr; |
656 | struct msghdr msg; | |
b537916c | 657 | struct sockaddr_storage addr; |
03b1230c JA |
658 | }; |
659 | ||
f67676d1 JA |
660 | struct io_async_rw { |
661 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
662 | const struct iovec *free_iovec; |
663 | struct iov_iter iter; | |
227c0c96 | 664 | size_t bytes_done; |
bcf5a063 | 665 | struct wait_page_queue wpq; |
f67676d1 JA |
666 | }; |
667 | ||
6b47ee6e PB |
668 | enum { |
669 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
670 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
671 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
672 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
673 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 674 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 675 | |
6b47ee6e PB |
676 | REQ_F_FAIL_LINK_BIT, |
677 | REQ_F_INFLIGHT_BIT, | |
678 | REQ_F_CUR_POS_BIT, | |
679 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 680 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 681 | REQ_F_ISREG_BIT, |
99bc4c38 | 682 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 683 | REQ_F_POLLED_BIT, |
bcda7baa | 684 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 685 | REQ_F_NO_FILE_TABLE_BIT, |
7cdaf587 | 686 | REQ_F_WORK_INITIALIZED_BIT, |
900fad45 | 687 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 688 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
689 | |
690 | /* not a real bit, just to check we're not overflowing the space */ | |
691 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
692 | }; |
693 | ||
694 | enum { | |
695 | /* ctx owns file */ | |
696 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
697 | /* drain existing IO first */ | |
698 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
699 | /* linked sqes */ | |
700 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
701 | /* doesn't sever on completion < 0 */ | |
702 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
703 | /* IOSQE_ASYNC */ | |
704 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
705 | /* IOSQE_BUFFER_SELECT */ |
706 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 707 | |
6b47ee6e PB |
708 | /* fail rest of links */ |
709 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
710 | /* on inflight list */ | |
711 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
712 | /* read/write uses file position */ | |
713 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
714 | /* must not punt to workers */ | |
715 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 716 | /* has or had linked timeout */ |
6b47ee6e | 717 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
718 | /* regular file */ |
719 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
720 | /* needs cleanup */ |
721 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
722 | /* already went through poll handler */ |
723 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
724 | /* buffer already selected */ |
725 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
726 | /* doesn't need file table for this request */ |
727 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
7cdaf587 XW |
728 | /* io_wq_work is initialized */ |
729 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), | |
900fad45 PB |
730 | /* linked timeout is active, i.e. prepared by link's head */ |
731 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
732 | /* completion is deferred through io_comp_state */ |
733 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
734 | }; |
735 | ||
736 | struct async_poll { | |
737 | struct io_poll_iocb poll; | |
807abcb0 | 738 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
739 | }; |
740 | ||
7cbf1722 JA |
741 | struct io_task_work { |
742 | struct io_wq_work_node node; | |
743 | task_work_func_t func; | |
744 | }; | |
745 | ||
09bb8394 JA |
746 | /* |
747 | * NOTE! Each of the iocb union members has the file pointer | |
748 | * as the first entry in their struct definition. So you can | |
749 | * access the file pointer through any of the sub-structs, | |
750 | * or directly as just 'ki_filp' in this struct. | |
751 | */ | |
2b188cc1 | 752 | struct io_kiocb { |
221c5eb2 | 753 | union { |
09bb8394 | 754 | struct file *file; |
9adbd45d | 755 | struct io_rw rw; |
221c5eb2 | 756 | struct io_poll_iocb poll; |
018043be | 757 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
758 | struct io_accept accept; |
759 | struct io_sync sync; | |
fbf23849 | 760 | struct io_cancel cancel; |
b29472ee | 761 | struct io_timeout timeout; |
0bdf7a2d | 762 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 763 | struct io_connect connect; |
e47293fd | 764 | struct io_sr_msg sr_msg; |
15b71abe | 765 | struct io_open open; |
b5dba59e | 766 | struct io_close close; |
269bbe5f | 767 | struct io_rsrc_update rsrc_update; |
4840e418 | 768 | struct io_fadvise fadvise; |
c1ca757b | 769 | struct io_madvise madvise; |
3e4827b0 | 770 | struct io_epoll epoll; |
7d67af2c | 771 | struct io_splice splice; |
ddf0322d | 772 | struct io_provide_buf pbuf; |
1d9e1288 | 773 | struct io_statx statx; |
36f4fa68 | 774 | struct io_shutdown shutdown; |
80a261fd | 775 | struct io_rename rename; |
14a1143b | 776 | struct io_unlink unlink; |
3ca405eb PB |
777 | /* use only after cleaning per-op data, see io_clean_op() */ |
778 | struct io_completion compl; | |
221c5eb2 | 779 | }; |
2b188cc1 | 780 | |
e8c2bc1f JA |
781 | /* opcode allocated if it needs to store data for async defer */ |
782 | void *async_data; | |
d625c6ee | 783 | u8 opcode; |
65a6543d XW |
784 | /* polled IO has completed */ |
785 | u8 iopoll_completed; | |
2b188cc1 | 786 | |
4f4eeba8 | 787 | u16 buf_index; |
9cf7c104 | 788 | u32 result; |
4f4eeba8 | 789 | |
010e8e6b PB |
790 | struct io_ring_ctx *ctx; |
791 | unsigned int flags; | |
792 | refcount_t refs; | |
793 | struct task_struct *task; | |
794 | u64 user_data; | |
d7718a9d | 795 | |
f2f87370 | 796 | struct io_kiocb *link; |
269bbe5f | 797 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 798 | |
d21ffe7e PB |
799 | /* |
800 | * 1. used with ctx->iopoll_list with reads/writes | |
801 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
802 | */ | |
010e8e6b | 803 | struct list_head inflight_entry; |
7cbf1722 JA |
804 | union { |
805 | struct io_task_work io_task_work; | |
806 | struct callback_head task_work; | |
807 | }; | |
010e8e6b PB |
808 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
809 | struct hlist_node hash_node; | |
810 | struct async_poll *apoll; | |
811 | struct io_wq_work work; | |
2b188cc1 | 812 | }; |
05589553 | 813 | |
27dc8338 PB |
814 | struct io_defer_entry { |
815 | struct list_head list; | |
816 | struct io_kiocb *req; | |
9cf7c104 | 817 | u32 seq; |
2b188cc1 JA |
818 | }; |
819 | ||
d3656344 | 820 | struct io_op_def { |
d3656344 JA |
821 | /* needs req->file assigned */ |
822 | unsigned needs_file : 1; | |
d3656344 JA |
823 | /* hash wq insertion if file is a regular file */ |
824 | unsigned hash_reg_file : 1; | |
825 | /* unbound wq insertion if file is a non-regular file */ | |
826 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
827 | /* opcode is not supported by this kernel */ |
828 | unsigned not_supported : 1; | |
8a72758c JA |
829 | /* set if opcode supports polled "wait" */ |
830 | unsigned pollin : 1; | |
831 | unsigned pollout : 1; | |
bcda7baa JA |
832 | /* op supports buffer selection */ |
833 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
834 | /* must always have async data allocated */ |
835 | unsigned needs_async_data : 1; | |
27926b68 JA |
836 | /* should block plug */ |
837 | unsigned plug : 1; | |
e8c2bc1f JA |
838 | /* size of async data needed, if any */ |
839 | unsigned short async_size; | |
d3656344 JA |
840 | }; |
841 | ||
0918682b | 842 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
843 | [IORING_OP_NOP] = {}, |
844 | [IORING_OP_READV] = { | |
d3656344 JA |
845 | .needs_file = 1, |
846 | .unbound_nonreg_file = 1, | |
8a72758c | 847 | .pollin = 1, |
4d954c25 | 848 | .buffer_select = 1, |
e8c2bc1f | 849 | .needs_async_data = 1, |
27926b68 | 850 | .plug = 1, |
e8c2bc1f | 851 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 852 | }, |
0463b6c5 | 853 | [IORING_OP_WRITEV] = { |
d3656344 JA |
854 | .needs_file = 1, |
855 | .hash_reg_file = 1, | |
856 | .unbound_nonreg_file = 1, | |
8a72758c | 857 | .pollout = 1, |
e8c2bc1f | 858 | .needs_async_data = 1, |
27926b68 | 859 | .plug = 1, |
e8c2bc1f | 860 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 861 | }, |
0463b6c5 | 862 | [IORING_OP_FSYNC] = { |
d3656344 JA |
863 | .needs_file = 1, |
864 | }, | |
0463b6c5 | 865 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
866 | .needs_file = 1, |
867 | .unbound_nonreg_file = 1, | |
8a72758c | 868 | .pollin = 1, |
27926b68 | 869 | .plug = 1, |
e8c2bc1f | 870 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 871 | }, |
0463b6c5 | 872 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
873 | .needs_file = 1, |
874 | .hash_reg_file = 1, | |
875 | .unbound_nonreg_file = 1, | |
8a72758c | 876 | .pollout = 1, |
27926b68 | 877 | .plug = 1, |
e8c2bc1f | 878 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 879 | }, |
0463b6c5 | 880 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
881 | .needs_file = 1, |
882 | .unbound_nonreg_file = 1, | |
883 | }, | |
0463b6c5 PB |
884 | [IORING_OP_POLL_REMOVE] = {}, |
885 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
886 | .needs_file = 1, |
887 | }, | |
0463b6c5 | 888 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
889 | .needs_file = 1, |
890 | .unbound_nonreg_file = 1, | |
8a72758c | 891 | .pollout = 1, |
e8c2bc1f JA |
892 | .needs_async_data = 1, |
893 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 894 | }, |
0463b6c5 | 895 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
896 | .needs_file = 1, |
897 | .unbound_nonreg_file = 1, | |
8a72758c | 898 | .pollin = 1, |
52de1fe1 | 899 | .buffer_select = 1, |
e8c2bc1f JA |
900 | .needs_async_data = 1, |
901 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 902 | }, |
0463b6c5 | 903 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
904 | .needs_async_data = 1, |
905 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 906 | }, |
9c8e11b3 PB |
907 | [IORING_OP_TIMEOUT_REMOVE] = { |
908 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 909 | }, |
0463b6c5 | 910 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
911 | .needs_file = 1, |
912 | .unbound_nonreg_file = 1, | |
8a72758c | 913 | .pollin = 1, |
d3656344 | 914 | }, |
0463b6c5 PB |
915 | [IORING_OP_ASYNC_CANCEL] = {}, |
916 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
917 | .needs_async_data = 1, |
918 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 919 | }, |
0463b6c5 | 920 | [IORING_OP_CONNECT] = { |
d3656344 JA |
921 | .needs_file = 1, |
922 | .unbound_nonreg_file = 1, | |
8a72758c | 923 | .pollout = 1, |
e8c2bc1f JA |
924 | .needs_async_data = 1, |
925 | .async_size = sizeof(struct io_async_connect), | |
d3656344 | 926 | }, |
0463b6c5 | 927 | [IORING_OP_FALLOCATE] = { |
d3656344 | 928 | .needs_file = 1, |
d3656344 | 929 | }, |
44526bed JA |
930 | [IORING_OP_OPENAT] = {}, |
931 | [IORING_OP_CLOSE] = {}, | |
932 | [IORING_OP_FILES_UPDATE] = {}, | |
933 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 934 | [IORING_OP_READ] = { |
3a6820f2 JA |
935 | .needs_file = 1, |
936 | .unbound_nonreg_file = 1, | |
8a72758c | 937 | .pollin = 1, |
bcda7baa | 938 | .buffer_select = 1, |
27926b68 | 939 | .plug = 1, |
e8c2bc1f | 940 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 941 | }, |
0463b6c5 | 942 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
943 | .needs_file = 1, |
944 | .unbound_nonreg_file = 1, | |
8a72758c | 945 | .pollout = 1, |
27926b68 | 946 | .plug = 1, |
e8c2bc1f | 947 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 948 | }, |
0463b6c5 | 949 | [IORING_OP_FADVISE] = { |
4840e418 | 950 | .needs_file = 1, |
c1ca757b | 951 | }, |
44526bed | 952 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 953 | [IORING_OP_SEND] = { |
fddaface JA |
954 | .needs_file = 1, |
955 | .unbound_nonreg_file = 1, | |
8a72758c | 956 | .pollout = 1, |
fddaface | 957 | }, |
0463b6c5 | 958 | [IORING_OP_RECV] = { |
fddaface JA |
959 | .needs_file = 1, |
960 | .unbound_nonreg_file = 1, | |
8a72758c | 961 | .pollin = 1, |
bcda7baa | 962 | .buffer_select = 1, |
fddaface | 963 | }, |
0463b6c5 | 964 | [IORING_OP_OPENAT2] = { |
cebdb986 | 965 | }, |
3e4827b0 JA |
966 | [IORING_OP_EPOLL_CTL] = { |
967 | .unbound_nonreg_file = 1, | |
3e4827b0 | 968 | }, |
7d67af2c PB |
969 | [IORING_OP_SPLICE] = { |
970 | .needs_file = 1, | |
971 | .hash_reg_file = 1, | |
972 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
973 | }, |
974 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 975 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
976 | [IORING_OP_TEE] = { |
977 | .needs_file = 1, | |
978 | .hash_reg_file = 1, | |
979 | .unbound_nonreg_file = 1, | |
980 | }, | |
36f4fa68 JA |
981 | [IORING_OP_SHUTDOWN] = { |
982 | .needs_file = 1, | |
983 | }, | |
44526bed JA |
984 | [IORING_OP_RENAMEAT] = {}, |
985 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
986 | }; |
987 | ||
9936c7c2 PB |
988 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
989 | struct task_struct *task, | |
990 | struct files_struct *files); | |
37d1e2e3 | 991 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
269bbe5f | 992 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
bc9744cd | 993 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
1ffc5422 | 994 | struct io_ring_ctx *ctx); |
f2303b1f | 995 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
1ffc5422 | 996 | |
23faba36 | 997 | static bool io_rw_reissue(struct io_kiocb *req); |
78e19bbe | 998 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 999 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1000 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c40f6379 | 1001 | static void io_double_put_req(struct io_kiocb *req); |
c7dae4ba JA |
1002 | static void io_dismantle_req(struct io_kiocb *req); |
1003 | static void io_put_task(struct task_struct *task, int nr); | |
1004 | static void io_queue_next(struct io_kiocb *req); | |
94ae5e77 | 1005 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
7271ef3a | 1006 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
94ae5e77 | 1007 | static void io_queue_linked_timeout(struct io_kiocb *req); |
05f3fb3c | 1008 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1009 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1010 | unsigned nr_args); |
3ca405eb | 1011 | static void __io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1012 | static struct file *io_file_get(struct io_submit_state *state, |
1013 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1014 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1015 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1016 | |
847595de PB |
1017 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
1018 | struct iov_iter *iter, bool needs_lock); | |
ff6165b2 JA |
1019 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
1020 | const struct iovec *fast_iov, | |
227c0c96 | 1021 | struct iov_iter *iter, bool force); |
907d1df3 | 1022 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1023 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1024 | struct io_ring_ctx *ctx); | |
de0617e4 | 1025 | |
2b188cc1 JA |
1026 | static struct kmem_cache *req_cachep; |
1027 | ||
0918682b | 1028 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1029 | |
1030 | struct sock *io_uring_get_socket(struct file *file) | |
1031 | { | |
1032 | #if defined(CONFIG_UNIX) | |
1033 | if (file->f_op == &io_uring_fops) { | |
1034 | struct io_ring_ctx *ctx = file->private_data; | |
1035 | ||
1036 | return ctx->ring_sock->sk; | |
1037 | } | |
1038 | #endif | |
1039 | return NULL; | |
1040 | } | |
1041 | EXPORT_SYMBOL(io_uring_get_socket); | |
1042 | ||
f2f87370 PB |
1043 | #define io_for_each_link(pos, head) \ |
1044 | for (pos = (head); pos; pos = pos->link) | |
1045 | ||
3ca405eb PB |
1046 | static inline void io_clean_op(struct io_kiocb *req) |
1047 | { | |
9d5c8190 | 1048 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
3ca405eb PB |
1049 | __io_clean_op(req); |
1050 | } | |
1051 | ||
36f72fe2 PB |
1052 | static inline void io_set_resource_node(struct io_kiocb *req) |
1053 | { | |
1054 | struct io_ring_ctx *ctx = req->ctx; | |
1055 | ||
269bbe5f BM |
1056 | if (!req->fixed_rsrc_refs) { |
1057 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; | |
1058 | percpu_ref_get(req->fixed_rsrc_refs); | |
36f72fe2 PB |
1059 | } |
1060 | } | |
1061 | ||
88f171ab PB |
1062 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1063 | { | |
1064 | if (!percpu_ref_tryget(ref)) { | |
1065 | /* already at zero, wait for ->release() */ | |
1066 | if (!try_wait_for_completion(compl)) | |
1067 | synchronize_rcu(); | |
1068 | return false; | |
1069 | } | |
1070 | ||
1071 | percpu_ref_resurrect(ref); | |
1072 | reinit_completion(compl); | |
1073 | percpu_ref_put(ref); | |
1074 | return true; | |
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; | |
4379bf8b | 1097 | if (req->task->files == files) |
08d23634 PB |
1098 | return true; |
1099 | } | |
1100 | return false; | |
1101 | } | |
1102 | ||
c40f6379 JA |
1103 | static inline void req_set_fail_links(struct io_kiocb *req) |
1104 | { | |
1105 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1106 | req->flags |= REQ_F_FAIL_LINK; | |
1107 | } | |
4a38aed2 | 1108 | |
ec99ca6c PB |
1109 | static inline void __io_req_init_async(struct io_kiocb *req) |
1110 | { | |
1111 | memset(&req->work, 0, sizeof(req->work)); | |
1112 | req->flags |= REQ_F_WORK_INITIALIZED; | |
1113 | } | |
1114 | ||
7cdaf587 XW |
1115 | /* |
1116 | * Note: must call io_req_init_async() for the first time you | |
1117 | * touch any members of io_wq_work. | |
1118 | */ | |
1119 | static inline void io_req_init_async(struct io_kiocb *req) | |
1120 | { | |
1121 | if (req->flags & REQ_F_WORK_INITIALIZED) | |
1122 | return; | |
1123 | ||
ec99ca6c | 1124 | __io_req_init_async(req); |
7cdaf587 XW |
1125 | } |
1126 | ||
2b188cc1 JA |
1127 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1128 | { | |
1129 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1130 | ||
0f158b4c | 1131 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1132 | } |
1133 | ||
8eb7e2d0 PB |
1134 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1135 | { | |
1136 | return !req->timeout.off; | |
1137 | } | |
1138 | ||
2b188cc1 JA |
1139 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1140 | { | |
1141 | struct io_ring_ctx *ctx; | |
78076bb6 | 1142 | int hash_bits; |
2b188cc1 JA |
1143 | |
1144 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1145 | if (!ctx) | |
1146 | return NULL; | |
1147 | ||
78076bb6 JA |
1148 | /* |
1149 | * Use 5 bits less than the max cq entries, that should give us around | |
1150 | * 32 entries per hash list if totally full and uniformly spread. | |
1151 | */ | |
1152 | hash_bits = ilog2(p->cq_entries); | |
1153 | hash_bits -= 5; | |
1154 | if (hash_bits <= 0) | |
1155 | hash_bits = 1; | |
1156 | ctx->cancel_hash_bits = hash_bits; | |
1157 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1158 | GFP_KERNEL); | |
1159 | if (!ctx->cancel_hash) | |
1160 | goto err; | |
1161 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1162 | ||
21482896 | 1163 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1164 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1165 | goto err; | |
2b188cc1 JA |
1166 | |
1167 | ctx->flags = p->flags; | |
90554200 | 1168 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1169 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1170 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1171 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1172 | init_completion(&ctx->ref_comp); |
1173 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1174 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1175 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1176 | mutex_init(&ctx->uring_lock); |
1177 | init_waitqueue_head(&ctx->wait); | |
1178 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1179 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1180 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1181 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1182 | spin_lock_init(&ctx->inflight_lock); |
1183 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1184 | spin_lock_init(&ctx->rsrc_ref_lock); |
1185 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1186 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1187 | init_llist_head(&ctx->rsrc_put_llist); | |
1b4c351f | 1188 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1189 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1190 | return ctx; |
206aefde | 1191 | err: |
78076bb6 | 1192 | kfree(ctx->cancel_hash); |
206aefde JA |
1193 | kfree(ctx); |
1194 | return NULL; | |
2b188cc1 JA |
1195 | } |
1196 | ||
9cf7c104 | 1197 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1198 | { |
2bc9930e JA |
1199 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1200 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1201 | |
9cf7c104 | 1202 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1203 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1204 | } |
de0617e4 | 1205 | |
9d858b21 | 1206 | return false; |
de0617e4 JA |
1207 | } |
1208 | ||
4edf20f9 | 1209 | static void io_req_clean_work(struct io_kiocb *req) |
18d9be1a | 1210 | { |
7cdaf587 | 1211 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
4edf20f9 | 1212 | return; |
51a4cc11 | 1213 | |
4379bf8b JA |
1214 | if (req->work.creds) { |
1215 | put_cred(req->work.creds); | |
1216 | req->work.creds = NULL; | |
1217 | } | |
34e08fed PB |
1218 | if (req->flags & REQ_F_INFLIGHT) { |
1219 | struct io_ring_ctx *ctx = req->ctx; | |
1220 | struct io_uring_task *tctx = req->task->io_uring; | |
1221 | unsigned long flags; | |
1222 | ||
1223 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1224 | list_del(&req->inflight_entry); | |
1225 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1226 | req->flags &= ~REQ_F_INFLIGHT; | |
1227 | if (atomic_read(&tctx->in_idle)) | |
1228 | wake_up(&tctx->wait); | |
1229 | } | |
51a4cc11 | 1230 | |
e86d0047 | 1231 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
561fb04a JA |
1232 | } |
1233 | ||
ce3d5aae PB |
1234 | static void io_req_track_inflight(struct io_kiocb *req) |
1235 | { | |
1236 | struct io_ring_ctx *ctx = req->ctx; | |
1237 | ||
1238 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
1239 | io_req_init_async(req); | |
1240 | req->flags |= REQ_F_INFLIGHT; | |
1241 | ||
1242 | spin_lock_irq(&ctx->inflight_lock); | |
1243 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1244 | spin_unlock_irq(&ctx->inflight_lock); | |
1245 | } | |
1246 | } | |
1247 | ||
1e6fa521 JA |
1248 | static void io_prep_async_work(struct io_kiocb *req) |
1249 | { | |
1250 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1251 | struct io_ring_ctx *ctx = req->ctx; |
1252 | ||
1253 | io_req_init_async(req); | |
1254 | ||
feaadc4f PB |
1255 | if (req->flags & REQ_F_FORCE_ASYNC) |
1256 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1257 | ||
1e6fa521 JA |
1258 | if (req->flags & REQ_F_ISREG) { |
1259 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1260 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1261 | } else { | |
1262 | if (def->unbound_nonreg_file) | |
1263 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1264 | } | |
4379bf8b JA |
1265 | if (!req->work.creds) |
1266 | req->work.creds = get_current_cred(); | |
561fb04a | 1267 | } |
cccf0ee8 | 1268 | |
cbdcb435 | 1269 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1270 | { |
cbdcb435 | 1271 | struct io_kiocb *cur; |
54a91f3b | 1272 | |
f2f87370 PB |
1273 | io_for_each_link(cur, req) |
1274 | io_prep_async_work(cur); | |
561fb04a JA |
1275 | } |
1276 | ||
7271ef3a | 1277 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1278 | { |
a197f664 | 1279 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1280 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1281 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1282 | |
3bfe6106 JA |
1283 | BUG_ON(!tctx); |
1284 | BUG_ON(!tctx->io_wq); | |
1285 | ||
8766dd51 PB |
1286 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1287 | &req->work, req->flags); | |
5aa75ed5 | 1288 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a | 1289 | return link; |
18d9be1a JA |
1290 | } |
1291 | ||
cbdcb435 PB |
1292 | static void io_queue_async_work(struct io_kiocb *req) |
1293 | { | |
7271ef3a JA |
1294 | struct io_kiocb *link; |
1295 | ||
cbdcb435 PB |
1296 | /* init ->work of the whole link before punting */ |
1297 | io_prep_async_link(req); | |
7271ef3a JA |
1298 | link = __io_queue_async_work(req); |
1299 | ||
1300 | if (link) | |
1301 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1302 | } |
1303 | ||
5262f567 JA |
1304 | static void io_kill_timeout(struct io_kiocb *req) |
1305 | { | |
e8c2bc1f | 1306 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1307 | int ret; |
1308 | ||
e8c2bc1f | 1309 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1310 | if (ret != -1) { |
01cec8c1 PB |
1311 | atomic_set(&req->ctx->cq_timeouts, |
1312 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1313 | list_del_init(&req->timeout.list); |
78e19bbe | 1314 | io_cqring_fill_event(req, 0); |
216578e5 | 1315 | io_put_req_deferred(req, 1); |
5262f567 JA |
1316 | } |
1317 | } | |
1318 | ||
76e1b642 JA |
1319 | /* |
1320 | * Returns true if we found and killed one or more timeouts | |
1321 | */ | |
6b81928d PB |
1322 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1323 | struct files_struct *files) | |
5262f567 JA |
1324 | { |
1325 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1326 | int canceled = 0; |
5262f567 JA |
1327 | |
1328 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1329 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1330 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1331 | io_kill_timeout(req); |
76e1b642 JA |
1332 | canceled++; |
1333 | } | |
f3606e3a | 1334 | } |
5262f567 | 1335 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1336 | return canceled != 0; |
5262f567 JA |
1337 | } |
1338 | ||
04518945 | 1339 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1340 | { |
04518945 | 1341 | do { |
27dc8338 PB |
1342 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1343 | struct io_defer_entry, list); | |
de0617e4 | 1344 | |
9cf7c104 | 1345 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1346 | break; |
27dc8338 | 1347 | list_del_init(&de->list); |
907d1df3 | 1348 | io_req_task_queue(de->req); |
27dc8338 | 1349 | kfree(de); |
04518945 PB |
1350 | } while (!list_empty(&ctx->defer_list)); |
1351 | } | |
1352 | ||
360428f8 | 1353 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1354 | { |
f010505b MDG |
1355 | u32 seq; |
1356 | ||
1357 | if (list_empty(&ctx->timeout_list)) | |
1358 | return; | |
1359 | ||
1360 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1361 | ||
1362 | do { | |
1363 | u32 events_needed, events_got; | |
360428f8 | 1364 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1365 | struct io_kiocb, timeout.list); |
de0617e4 | 1366 | |
8eb7e2d0 | 1367 | if (io_is_timeout_noseq(req)) |
360428f8 | 1368 | break; |
f010505b MDG |
1369 | |
1370 | /* | |
1371 | * Since seq can easily wrap around over time, subtract | |
1372 | * the last seq at which timeouts were flushed before comparing. | |
1373 | * Assuming not more than 2^31-1 events have happened since, | |
1374 | * these subtractions won't have wrapped, so we can check if | |
1375 | * target is in [last_seq, current_seq] by comparing the two. | |
1376 | */ | |
1377 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1378 | events_got = seq - ctx->cq_last_tm_flush; | |
1379 | if (events_got < events_needed) | |
360428f8 | 1380 | break; |
bfe68a22 | 1381 | |
135fcde8 | 1382 | list_del_init(&req->timeout.list); |
5262f567 | 1383 | io_kill_timeout(req); |
f010505b MDG |
1384 | } while (!list_empty(&ctx->timeout_list)); |
1385 | ||
1386 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1387 | } |
5262f567 | 1388 | |
360428f8 PB |
1389 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1390 | { | |
1391 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1392 | |
1393 | /* order cqe stores with ring update */ | |
1394 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1395 | |
04518945 PB |
1396 | if (unlikely(!list_empty(&ctx->defer_list))) |
1397 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1398 | } |
1399 | ||
90554200 JA |
1400 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1401 | { | |
1402 | struct io_rings *r = ctx->rings; | |
1403 | ||
1404 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1405 | } | |
1406 | ||
888aae2e PB |
1407 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1408 | { | |
1409 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1410 | } | |
1411 | ||
2b188cc1 JA |
1412 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1413 | { | |
75b28aff | 1414 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1415 | unsigned tail; |
1416 | ||
115e12e5 SB |
1417 | /* |
1418 | * writes to the cq entry need to come after reading head; the | |
1419 | * control dependency is enough as we're using WRITE_ONCE to | |
1420 | * fill the cq entry | |
1421 | */ | |
888aae2e | 1422 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1423 | return NULL; |
1424 | ||
888aae2e | 1425 | tail = ctx->cached_cq_tail++; |
75b28aff | 1426 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1427 | } |
1428 | ||
f2842ab5 JA |
1429 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1430 | { | |
f0b493e6 JA |
1431 | if (!ctx->cq_ev_fd) |
1432 | return false; | |
7e55a19c SG |
1433 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1434 | return false; | |
f2842ab5 JA |
1435 | if (!ctx->eventfd_async) |
1436 | return true; | |
b41e9852 | 1437 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1438 | } |
1439 | ||
b41e9852 | 1440 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1441 | { |
b1445e59 PB |
1442 | /* see waitqueue_active() comment */ |
1443 | smp_mb(); | |
1444 | ||
1d7bb1d5 JA |
1445 | if (waitqueue_active(&ctx->wait)) |
1446 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1447 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1448 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1449 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1450 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1451 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1452 | wake_up_interruptible(&ctx->cq_wait); |
1453 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1454 | } | |
1d7bb1d5 JA |
1455 | } |
1456 | ||
80c18e4a PB |
1457 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1458 | { | |
b1445e59 PB |
1459 | /* see waitqueue_active() comment */ |
1460 | smp_mb(); | |
1461 | ||
80c18e4a PB |
1462 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1463 | if (waitqueue_active(&ctx->wait)) | |
1464 | wake_up(&ctx->wait); | |
1465 | } | |
1466 | if (io_should_trigger_evfd(ctx)) | |
1467 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1468 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1469 | wake_up_interruptible(&ctx->cq_wait); |
1470 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1471 | } | |
80c18e4a PB |
1472 | } |
1473 | ||
c4a2ed72 | 1474 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1475 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1476 | struct task_struct *tsk, | |
1477 | struct files_struct *files) | |
1d7bb1d5 JA |
1478 | { |
1479 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1480 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1481 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1482 | unsigned long flags; |
b18032bb | 1483 | bool all_flushed, posted; |
1d7bb1d5 JA |
1484 | LIST_HEAD(list); |
1485 | ||
e23de15f PB |
1486 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1487 | return false; | |
1d7bb1d5 | 1488 | |
b18032bb | 1489 | posted = false; |
1d7bb1d5 | 1490 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1491 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1492 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1493 | continue; |
1494 | ||
1d7bb1d5 JA |
1495 | cqe = io_get_cqring(ctx); |
1496 | if (!cqe && !force) | |
1497 | break; | |
1498 | ||
40d8ddd4 | 1499 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1500 | if (cqe) { |
1501 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1502 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1503 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1504 | } else { |
2c3bac6d | 1505 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1506 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1507 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1508 | } |
b18032bb | 1509 | posted = true; |
1d7bb1d5 JA |
1510 | } |
1511 | ||
09e88404 PB |
1512 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1513 | if (all_flushed) { | |
1514 | clear_bit(0, &ctx->sq_check_overflow); | |
1515 | clear_bit(0, &ctx->cq_check_overflow); | |
1516 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1517 | } | |
46930143 | 1518 | |
b18032bb JA |
1519 | if (posted) |
1520 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1521 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1522 | if (posted) |
1523 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1524 | |
1525 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1526 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1527 | list_del(&req->compl.list); | |
ec9c02ad | 1528 | io_put_req(req); |
1d7bb1d5 | 1529 | } |
c4a2ed72 | 1530 | |
09e88404 | 1531 | return all_flushed; |
1d7bb1d5 JA |
1532 | } |
1533 | ||
6c503150 PB |
1534 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1535 | struct task_struct *tsk, | |
1536 | struct files_struct *files) | |
1537 | { | |
1538 | if (test_bit(0, &ctx->cq_check_overflow)) { | |
1539 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1540 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1541 | mutex_lock(&ctx->uring_lock); | |
1542 | __io_cqring_overflow_flush(ctx, force, tsk, files); | |
1543 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1544 | mutex_unlock(&ctx->uring_lock); | |
1545 | } | |
1546 | } | |
1547 | ||
bcda7baa | 1548 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1549 | { |
78e19bbe | 1550 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1551 | struct io_uring_cqe *cqe; |
1552 | ||
78e19bbe | 1553 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1554 | |
2b188cc1 JA |
1555 | /* |
1556 | * If we can't get a cq entry, userspace overflowed the | |
1557 | * submission (by quite a lot). Increment the overflow count in | |
1558 | * the ring. | |
1559 | */ | |
1560 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1561 | if (likely(cqe)) { |
78e19bbe | 1562 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1563 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1564 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1565 | } else if (ctx->cq_overflow_flushed || |
1566 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1567 | /* |
1568 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1569 | * then we cannot store the request for later flushing, we need | |
1570 | * to drop it on the floor. | |
1571 | */ | |
2c3bac6d PB |
1572 | ctx->cached_cq_overflow++; |
1573 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1574 | } else { |
ad3eb2c8 JA |
1575 | if (list_empty(&ctx->cq_overflow_list)) { |
1576 | set_bit(0, &ctx->sq_check_overflow); | |
1577 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1578 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1579 | } |
40d8ddd4 | 1580 | io_clean_op(req); |
1d7bb1d5 | 1581 | req->result = res; |
0f7e466b | 1582 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1583 | refcount_inc(&req->refs); |
1584 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1585 | } |
1586 | } | |
1587 | ||
bcda7baa JA |
1588 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1589 | { | |
1590 | __io_cqring_fill_event(req, res, 0); | |
1591 | } | |
1592 | ||
c7dae4ba JA |
1593 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1594 | unsigned int cflags) | |
2b188cc1 | 1595 | { |
78e19bbe | 1596 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1597 | unsigned long flags; |
1598 | ||
1599 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1600 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1601 | io_commit_cqring(ctx); |
c7dae4ba JA |
1602 | /* |
1603 | * If we're the last reference to this request, add to our locked | |
1604 | * free_list cache. | |
1605 | */ | |
1606 | if (refcount_dec_and_test(&req->refs)) { | |
1607 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1608 | ||
1609 | io_dismantle_req(req); | |
1610 | io_put_task(req->task, 1); | |
1611 | list_add(&req->compl.list, &cs->locked_free_list); | |
1612 | cs->locked_free_nr++; | |
1613 | } else | |
1614 | req = NULL; | |
2b188cc1 JA |
1615 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1616 | ||
8c838788 | 1617 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1618 | if (req) { |
1619 | io_queue_next(req); | |
1620 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1621 | } |
229a7b63 JA |
1622 | } |
1623 | ||
a38d68db | 1624 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1625 | unsigned int cflags) |
229a7b63 | 1626 | { |
a38d68db PB |
1627 | io_clean_op(req); |
1628 | req->result = res; | |
1629 | req->compl.cflags = cflags; | |
e342c807 | 1630 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1631 | } |
1632 | ||
889fca73 PB |
1633 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1634 | long res, unsigned cflags) | |
bcda7baa | 1635 | { |
889fca73 PB |
1636 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1637 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1638 | else |
c7dae4ba | 1639 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1640 | } |
1641 | ||
a38d68db | 1642 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1643 | { |
889fca73 | 1644 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1645 | } |
1646 | ||
c7dae4ba | 1647 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1648 | { |
c7dae4ba JA |
1649 | struct io_submit_state *state = &ctx->submit_state; |
1650 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 1651 | struct io_kiocb *req = NULL; |
0ddf92e8 | 1652 | |
c7dae4ba JA |
1653 | /* |
1654 | * If we have more than a batch's worth of requests in our IRQ side | |
1655 | * locked cache, grab the lock and move them over to our submission | |
1656 | * side cache. | |
1657 | */ | |
1658 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
1659 | spin_lock_irq(&ctx->completion_lock); | |
1660 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1661 | cs->locked_free_nr = 0; | |
1662 | spin_unlock_irq(&ctx->completion_lock); | |
1663 | } | |
0ddf92e8 | 1664 | |
c7dae4ba JA |
1665 | while (!list_empty(&cs->free_list)) { |
1666 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
1667 | compl.list); |
1668 | list_del(&req->compl.list); | |
e5d1bc0a PB |
1669 | state->reqs[state->free_reqs++] = req; |
1670 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
1671 | break; | |
1b4c351f JA |
1672 | } |
1673 | ||
e5d1bc0a | 1674 | return req != NULL; |
0ddf92e8 JA |
1675 | } |
1676 | ||
e5d1bc0a | 1677 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1678 | { |
e5d1bc0a PB |
1679 | struct io_submit_state *state = &ctx->submit_state; |
1680 | ||
1681 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1682 | ||
f6b6c7d6 | 1683 | if (!state->free_reqs) { |
291b2821 | 1684 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1685 | int ret; |
1686 | ||
c7dae4ba | 1687 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1688 | goto got_req; |
1689 | ||
bf019da7 PB |
1690 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1691 | state->reqs); | |
fd6fab2c JA |
1692 | |
1693 | /* | |
1694 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1695 | * retry single alloc to be on the safe side. | |
1696 | */ | |
1697 | if (unlikely(ret <= 0)) { | |
1698 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1699 | if (!state->reqs[0]) | |
3893f39f | 1700 | return NULL; |
fd6fab2c JA |
1701 | ret = 1; |
1702 | } | |
291b2821 | 1703 | state->free_reqs = ret; |
2b188cc1 | 1704 | } |
e5d1bc0a | 1705 | got_req: |
291b2821 PB |
1706 | state->free_reqs--; |
1707 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1708 | } |
1709 | ||
8da11c19 PB |
1710 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1711 | bool fixed) | |
1712 | { | |
36f72fe2 | 1713 | if (!fixed) |
8da11c19 PB |
1714 | fput(file); |
1715 | } | |
1716 | ||
4edf20f9 | 1717 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1718 | { |
3ca405eb | 1719 | io_clean_op(req); |
929a3af9 | 1720 | |
e8c2bc1f JA |
1721 | if (req->async_data) |
1722 | kfree(req->async_data); | |
8da11c19 PB |
1723 | if (req->file) |
1724 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
1725 | if (req->fixed_rsrc_refs) |
1726 | percpu_ref_put(req->fixed_rsrc_refs); | |
4edf20f9 | 1727 | io_req_clean_work(req); |
e65ef56d JA |
1728 | } |
1729 | ||
7c660731 PB |
1730 | static inline void io_put_task(struct task_struct *task, int nr) |
1731 | { | |
1732 | struct io_uring_task *tctx = task->io_uring; | |
1733 | ||
1734 | percpu_counter_sub(&tctx->inflight, nr); | |
1735 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1736 | wake_up(&tctx->wait); | |
1737 | put_task_struct_many(task, nr); | |
1738 | } | |
1739 | ||
216578e5 | 1740 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1741 | { |
51a4cc11 | 1742 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1743 | |
216578e5 | 1744 | io_dismantle_req(req); |
7c660731 | 1745 | io_put_task(req->task, 1); |
c6ca97b3 | 1746 | |
3893f39f | 1747 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1748 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1749 | } |
1750 | ||
f2f87370 PB |
1751 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1752 | { | |
1753 | struct io_kiocb *nxt = req->link; | |
1754 | ||
1755 | req->link = nxt->link; | |
1756 | nxt->link = NULL; | |
1757 | } | |
1758 | ||
c9abd7ad | 1759 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1760 | { |
a197f664 | 1761 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1762 | struct io_kiocb *link; |
c9abd7ad PB |
1763 | bool cancelled = false; |
1764 | unsigned long flags; | |
7c86ffee | 1765 | |
c9abd7ad | 1766 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1767 | link = req->link; |
1768 | ||
900fad45 PB |
1769 | /* |
1770 | * Can happen if a linked timeout fired and link had been like | |
1771 | * req -> link t-out -> link t-out [-> ...] | |
1772 | */ | |
c9abd7ad PB |
1773 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1774 | struct io_timeout_data *io = link->async_data; | |
1775 | int ret; | |
7c86ffee | 1776 | |
f2f87370 | 1777 | io_remove_next_linked(req); |
90cd7e42 | 1778 | link->timeout.head = NULL; |
c9abd7ad PB |
1779 | ret = hrtimer_try_to_cancel(&io->timer); |
1780 | if (ret != -1) { | |
1781 | io_cqring_fill_event(link, -ECANCELED); | |
1782 | io_commit_cqring(ctx); | |
1783 | cancelled = true; | |
1784 | } | |
1785 | } | |
7c86ffee | 1786 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 1787 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 1788 | |
c9abd7ad | 1789 | if (cancelled) { |
7c86ffee | 1790 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
1791 | io_put_req(link); |
1792 | } | |
7c86ffee PB |
1793 | } |
1794 | ||
9e645e11 | 1795 | |
d148ca4b | 1796 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1797 | { |
f2f87370 | 1798 | struct io_kiocb *link, *nxt; |
2665abfd | 1799 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 1800 | unsigned long flags; |
9e645e11 | 1801 | |
d148ca4b | 1802 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1803 | link = req->link; |
1804 | req->link = NULL; | |
9e645e11 | 1805 | |
f2f87370 PB |
1806 | while (link) { |
1807 | nxt = link->link; | |
1808 | link->link = NULL; | |
2665abfd | 1809 | |
f2f87370 | 1810 | trace_io_uring_fail_link(req, link); |
7c86ffee | 1811 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 PB |
1812 | |
1813 | /* | |
1814 | * It's ok to free under spinlock as they're not linked anymore, | |
1815 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on | |
1816 | * work.fs->lock. | |
1817 | */ | |
1818 | if (link->flags & REQ_F_WORK_INITIALIZED) | |
1819 | io_put_req_deferred(link, 2); | |
1820 | else | |
1821 | io_double_put_req(link); | |
f2f87370 | 1822 | link = nxt; |
9e645e11 | 1823 | } |
2665abfd | 1824 | io_commit_cqring(ctx); |
216578e5 | 1825 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 1826 | |
2665abfd | 1827 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1828 | } |
1829 | ||
3fa5e0f3 | 1830 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1831 | { |
7c86ffee PB |
1832 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1833 | io_kill_linked_timeout(req); | |
944e58bf | 1834 | |
9e645e11 JA |
1835 | /* |
1836 | * If LINK is set, we have dependent requests in this chain. If we | |
1837 | * didn't fail this request, queue the first one up, moving any other | |
1838 | * dependencies to the next request. In case of failure, fail the rest | |
1839 | * of the chain. | |
1840 | */ | |
f2f87370 PB |
1841 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
1842 | struct io_kiocb *nxt = req->link; | |
1843 | ||
1844 | req->link = NULL; | |
1845 | return nxt; | |
1846 | } | |
9b5f7bd9 PB |
1847 | io_fail_links(req); |
1848 | return NULL; | |
4d7dd462 | 1849 | } |
9e645e11 | 1850 | |
f2f87370 | 1851 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1852 | { |
cdbff982 | 1853 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1854 | return NULL; |
1855 | return __io_req_find_next(req); | |
1856 | } | |
1857 | ||
7cbf1722 | 1858 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1859 | { |
65453d1e | 1860 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1861 | struct io_wq_work_list list; |
1862 | struct io_wq_work_node *node; | |
c2c4c83c | 1863 | |
7cbf1722 JA |
1864 | if (wq_list_empty(&tctx->task_list)) |
1865 | return false; | |
6200b0ae | 1866 | |
0b81e80c | 1867 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1868 | list = tctx->task_list; |
1869 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1870 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1871 | |
7cbf1722 JA |
1872 | node = list.first; |
1873 | while (node) { | |
1874 | struct io_wq_work_node *next = node->next; | |
65453d1e | 1875 | struct io_ring_ctx *this_ctx; |
7cbf1722 | 1876 | struct io_kiocb *req; |
0ba9c9ed | 1877 | |
7cbf1722 | 1878 | req = container_of(node, struct io_kiocb, io_task_work.node); |
65453d1e | 1879 | this_ctx = req->ctx; |
7cbf1722 JA |
1880 | req->task_work.func(&req->task_work); |
1881 | node = next; | |
65453d1e JA |
1882 | |
1883 | if (!ctx) { | |
1884 | ctx = this_ctx; | |
1885 | } else if (ctx != this_ctx) { | |
1886 | mutex_lock(&ctx->uring_lock); | |
1887 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1888 | mutex_unlock(&ctx->uring_lock); | |
1889 | ctx = this_ctx; | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | if (ctx && ctx->submit_state.comp.nr) { | |
1894 | mutex_lock(&ctx->uring_lock); | |
1895 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1896 | mutex_unlock(&ctx->uring_lock); | |
7cbf1722 JA |
1897 | } |
1898 | ||
1899 | return list.first != NULL; | |
c2c4c83c JA |
1900 | } |
1901 | ||
7cbf1722 | 1902 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1903 | { |
7cbf1722 | 1904 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1905 | |
7cbf1722 JA |
1906 | while (__tctx_task_work(tctx)) |
1907 | cond_resched(); | |
1908 | ||
1909 | clear_bit(0, &tctx->task_state); | |
1910 | } | |
1911 | ||
1912 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
1913 | enum task_work_notify_mode notify) | |
1914 | { | |
1915 | struct io_uring_task *tctx = tsk->io_uring; | |
1916 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 1917 | unsigned long flags; |
7cbf1722 JA |
1918 | int ret; |
1919 | ||
1920 | WARN_ON_ONCE(!tctx); | |
1921 | ||
0b81e80c | 1922 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1923 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1924 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1925 | |
1926 | /* task_work already pending, we're done */ | |
1927 | if (test_bit(0, &tctx->task_state) || | |
1928 | test_and_set_bit(0, &tctx->task_state)) | |
1929 | return 0; | |
1930 | ||
1931 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
1932 | return 0; | |
1933 | ||
1934 | /* | |
1935 | * Slow path - we failed, find and delete work. if the work is not | |
1936 | * in the list, it got run and we're fine. | |
1937 | */ | |
1938 | ret = 0; | |
0b81e80c | 1939 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1940 | wq_list_for_each(node, prev, &tctx->task_list) { |
1941 | if (&req->io_task_work.node == node) { | |
1942 | wq_list_del(&tctx->task_list, node, prev); | |
1943 | ret = 1; | |
1944 | break; | |
1945 | } | |
1946 | } | |
0b81e80c | 1947 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1948 | clear_bit(0, &tctx->task_state); |
1949 | return ret; | |
1950 | } | |
1951 | ||
355fb9e2 | 1952 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
1953 | { |
1954 | struct task_struct *tsk = req->task; | |
1955 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
1956 | enum task_work_notify_mode notify; |
1957 | int ret; | |
c2c4c83c | 1958 | |
6200b0ae JA |
1959 | if (tsk->flags & PF_EXITING) |
1960 | return -ESRCH; | |
1961 | ||
c2c4c83c | 1962 | /* |
0ba9c9ed JA |
1963 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1964 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1965 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1966 | * will do the job. | |
c2c4c83c | 1967 | */ |
91989c70 | 1968 | notify = TWA_NONE; |
355fb9e2 | 1969 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
1970 | notify = TWA_SIGNAL; |
1971 | ||
7cbf1722 | 1972 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
1973 | if (!ret) |
1974 | wake_up_process(tsk); | |
0ba9c9ed | 1975 | |
c2c4c83c JA |
1976 | return ret; |
1977 | } | |
1978 | ||
eab30c4d | 1979 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 1980 | task_work_func_t cb) |
eab30c4d | 1981 | { |
7c25c0d1 JA |
1982 | struct io_ring_ctx *ctx = req->ctx; |
1983 | struct callback_head *head; | |
eab30c4d PB |
1984 | |
1985 | init_task_work(&req->task_work, cb); | |
7c25c0d1 JA |
1986 | do { |
1987 | head = READ_ONCE(ctx->exit_task_work); | |
1988 | req->task_work.next = head; | |
1989 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); | |
eab30c4d PB |
1990 | } |
1991 | ||
c40f6379 JA |
1992 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1993 | { | |
1994 | struct io_ring_ctx *ctx = req->ctx; | |
1995 | ||
1996 | spin_lock_irq(&ctx->completion_lock); | |
1997 | io_cqring_fill_event(req, error); | |
1998 | io_commit_cqring(ctx); | |
1999 | spin_unlock_irq(&ctx->completion_lock); | |
2000 | ||
2001 | io_cqring_ev_posted(ctx); | |
2002 | req_set_fail_links(req); | |
2003 | io_double_put_req(req); | |
2004 | } | |
2005 | ||
2006 | static void io_req_task_cancel(struct callback_head *cb) | |
2007 | { | |
2008 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 2009 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2010 | |
792bb6eb | 2011 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 2012 | __io_req_task_cancel(req, req->result); |
792bb6eb | 2013 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 2014 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
2015 | } |
2016 | ||
2017 | static void __io_req_task_submit(struct io_kiocb *req) | |
2018 | { | |
2019 | struct io_ring_ctx *ctx = req->ctx; | |
2020 | ||
04fc6c80 | 2021 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2022 | mutex_lock(&ctx->uring_lock); |
37d1e2e3 | 2023 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING)) |
c5eef2b9 | 2024 | __io_queue_sqe(req); |
81b6d05c | 2025 | else |
c40f6379 | 2026 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 2027 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2028 | } |
2029 | ||
2030 | static void io_req_task_submit(struct callback_head *cb) | |
2031 | { | |
2032 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2033 | ||
2034 | __io_req_task_submit(req); | |
2035 | } | |
2036 | ||
2037 | static void io_req_task_queue(struct io_kiocb *req) | |
2038 | { | |
c40f6379 JA |
2039 | int ret; |
2040 | ||
7cbf1722 | 2041 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 2042 | ret = io_req_task_work_add(req); |
c40f6379 | 2043 | if (unlikely(ret)) { |
a3df7698 | 2044 | req->result = -ECANCELED; |
04fc6c80 | 2045 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 2046 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 2047 | } |
c40f6379 JA |
2048 | } |
2049 | ||
a3df7698 PB |
2050 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
2051 | { | |
2052 | percpu_ref_get(&req->ctx->refs); | |
2053 | req->result = ret; | |
2054 | req->task_work.func = io_req_task_cancel; | |
2055 | ||
2056 | if (unlikely(io_req_task_work_add(req))) | |
2057 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2058 | } | |
2059 | ||
f2f87370 | 2060 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2061 | { |
9b5f7bd9 | 2062 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2063 | |
2064 | if (nxt) | |
906a8c3f | 2065 | io_req_task_queue(nxt); |
c69f8dbe JL |
2066 | } |
2067 | ||
c3524383 | 2068 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2069 | { |
c3524383 PB |
2070 | io_queue_next(req); |
2071 | __io_free_req(req); | |
2072 | } | |
8766dd51 | 2073 | |
2d6500d4 | 2074 | struct req_batch { |
5af1d13e PB |
2075 | struct task_struct *task; |
2076 | int task_refs; | |
1b4c351f | 2077 | int ctx_refs; |
2d6500d4 PB |
2078 | }; |
2079 | ||
5af1d13e PB |
2080 | static inline void io_init_req_batch(struct req_batch *rb) |
2081 | { | |
5af1d13e | 2082 | rb->task_refs = 0; |
9ae72463 | 2083 | rb->ctx_refs = 0; |
5af1d13e PB |
2084 | rb->task = NULL; |
2085 | } | |
2086 | ||
2d6500d4 PB |
2087 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2088 | struct req_batch *rb) | |
2089 | { | |
6e833d53 | 2090 | if (rb->task) |
7c660731 | 2091 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2092 | if (rb->ctx_refs) |
2093 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2094 | } |
2095 | ||
6ff119a6 PB |
2096 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2097 | struct io_submit_state *state) | |
2d6500d4 | 2098 | { |
f2f87370 | 2099 | io_queue_next(req); |
2d6500d4 | 2100 | |
e3bc8e9d | 2101 | if (req->task != rb->task) { |
7c660731 PB |
2102 | if (rb->task) |
2103 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2104 | rb->task = req->task; |
2105 | rb->task_refs = 0; | |
5af1d13e | 2106 | } |
e3bc8e9d | 2107 | rb->task_refs++; |
9ae72463 | 2108 | rb->ctx_refs++; |
5af1d13e | 2109 | |
4edf20f9 | 2110 | io_dismantle_req(req); |
bd759045 | 2111 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2112 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2113 | else |
2114 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2115 | } |
2116 | ||
905c172f PB |
2117 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2118 | struct io_ring_ctx *ctx) | |
2119 | { | |
2120 | int i, nr = cs->nr; | |
2121 | struct io_kiocb *req; | |
2122 | struct req_batch rb; | |
2123 | ||
2124 | io_init_req_batch(&rb); | |
2125 | spin_lock_irq(&ctx->completion_lock); | |
2126 | for (i = 0; i < nr; i++) { | |
2127 | req = cs->reqs[i]; | |
2128 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2129 | } | |
2130 | io_commit_cqring(ctx); | |
2131 | spin_unlock_irq(&ctx->completion_lock); | |
2132 | ||
2133 | io_cqring_ev_posted(ctx); | |
2134 | for (i = 0; i < nr; i++) { | |
2135 | req = cs->reqs[i]; | |
2136 | ||
2137 | /* submission and completion refs */ | |
2138 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2139 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2140 | } |
2141 | ||
2142 | io_req_free_batch_finish(ctx, &rb); | |
2143 | cs->nr = 0; | |
7a743e22 PB |
2144 | } |
2145 | ||
ba816ad6 JA |
2146 | /* |
2147 | * Drop reference to request, return next in chain (if there is one) if this | |
2148 | * was the last reference to this request. | |
2149 | */ | |
9b5f7bd9 | 2150 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2151 | { |
9b5f7bd9 PB |
2152 | struct io_kiocb *nxt = NULL; |
2153 | ||
2a44f467 | 2154 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2155 | nxt = io_req_find_next(req); |
4d7dd462 | 2156 | __io_free_req(req); |
2a44f467 | 2157 | } |
9b5f7bd9 | 2158 | return nxt; |
2b188cc1 JA |
2159 | } |
2160 | ||
e65ef56d JA |
2161 | static void io_put_req(struct io_kiocb *req) |
2162 | { | |
2163 | if (refcount_dec_and_test(&req->refs)) | |
2164 | io_free_req(req); | |
2b188cc1 JA |
2165 | } |
2166 | ||
216578e5 PB |
2167 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2168 | { | |
2169 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2170 | ||
2171 | io_free_req(req); | |
2172 | } | |
2173 | ||
2174 | static void io_free_req_deferred(struct io_kiocb *req) | |
2175 | { | |
2176 | int ret; | |
2177 | ||
7cbf1722 | 2178 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2179 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2180 | if (unlikely(ret)) |
2181 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2182 | } |
2183 | ||
2184 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2185 | { | |
2186 | if (refcount_sub_and_test(refs, &req->refs)) | |
2187 | io_free_req_deferred(req); | |
2188 | } | |
2189 | ||
978db57e JA |
2190 | static void io_double_put_req(struct io_kiocb *req) |
2191 | { | |
2192 | /* drop both submit and complete references */ | |
2193 | if (refcount_sub_and_test(2, &req->refs)) | |
2194 | io_free_req(req); | |
2195 | } | |
2196 | ||
6c503150 | 2197 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2198 | { |
2199 | /* See comment at the top of this file */ | |
2200 | smp_rmb(); | |
e23de15f | 2201 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2202 | } |
2203 | ||
fb5ccc98 PB |
2204 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2205 | { | |
2206 | struct io_rings *rings = ctx->rings; | |
2207 | ||
2208 | /* make sure SQ entry isn't read before tail */ | |
2209 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2210 | } | |
2211 | ||
8ff069bf | 2212 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2213 | { |
8ff069bf | 2214 | unsigned int cflags; |
e94f141b | 2215 | |
bcda7baa JA |
2216 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2217 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2218 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2219 | kfree(kbuf); |
2220 | return cflags; | |
e94f141b JA |
2221 | } |
2222 | ||
8ff069bf | 2223 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2224 | { |
4d954c25 | 2225 | struct io_buffer *kbuf; |
bcda7baa | 2226 | |
4d954c25 | 2227 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2228 | return io_put_kbuf(req, kbuf); |
2229 | } | |
2230 | ||
4c6e277c JA |
2231 | static inline bool io_run_task_work(void) |
2232 | { | |
6200b0ae JA |
2233 | /* |
2234 | * Not safe to run on exiting task, and the task_work handling will | |
2235 | * not add work to such a task. | |
2236 | */ | |
2237 | if (unlikely(current->flags & PF_EXITING)) | |
2238 | return false; | |
4c6e277c JA |
2239 | if (current->task_works) { |
2240 | __set_current_state(TASK_RUNNING); | |
2241 | task_work_run(); | |
2242 | return true; | |
2243 | } | |
2244 | ||
2245 | return false; | |
bcda7baa JA |
2246 | } |
2247 | ||
def596e9 JA |
2248 | /* |
2249 | * Find and free completed poll iocbs | |
2250 | */ | |
2251 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2252 | struct list_head *done) | |
2253 | { | |
8237e045 | 2254 | struct req_batch rb; |
def596e9 | 2255 | struct io_kiocb *req; |
bbde017a XW |
2256 | |
2257 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2258 | smp_rmb(); | |
def596e9 | 2259 | |
5af1d13e | 2260 | io_init_req_batch(&rb); |
def596e9 | 2261 | while (!list_empty(done)) { |
bcda7baa JA |
2262 | int cflags = 0; |
2263 | ||
d21ffe7e | 2264 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2265 | list_del(&req->inflight_entry); |
2266 | ||
bbde017a XW |
2267 | if (READ_ONCE(req->result) == -EAGAIN) { |
2268 | req->iopoll_completed = 0; | |
23faba36 | 2269 | if (io_rw_reissue(req)) |
f161340d | 2270 | continue; |
bbde017a | 2271 | } |
def596e9 | 2272 | |
bcda7baa | 2273 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2274 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2275 | |
2276 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2277 | (*nr_events)++; |
2278 | ||
c3524383 | 2279 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2280 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2281 | } |
def596e9 | 2282 | |
09bb8394 | 2283 | io_commit_cqring(ctx); |
80c18e4a | 2284 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2285 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2286 | } |
2287 | ||
def596e9 JA |
2288 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2289 | long min) | |
2290 | { | |
2291 | struct io_kiocb *req, *tmp; | |
2292 | LIST_HEAD(done); | |
2293 | bool spin; | |
2294 | int ret; | |
2295 | ||
2296 | /* | |
2297 | * Only spin for completions if we don't have multiple devices hanging | |
2298 | * off our complete list, and we're under the requested amount. | |
2299 | */ | |
2300 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2301 | ||
2302 | ret = 0; | |
d21ffe7e | 2303 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2304 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2305 | |
2306 | /* | |
581f9810 BM |
2307 | * Move completed and retryable entries to our local lists. |
2308 | * If we find a request that requires polling, break out | |
2309 | * and complete those lists first, if we have entries there. | |
def596e9 | 2310 | */ |
65a6543d | 2311 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2312 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2313 | continue; |
2314 | } | |
2315 | if (!list_empty(&done)) | |
2316 | break; | |
2317 | ||
2318 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2319 | if (ret < 0) | |
2320 | break; | |
2321 | ||
3aadc23e PB |
2322 | /* iopoll may have completed current req */ |
2323 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2324 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2325 | |
def596e9 JA |
2326 | if (ret && spin) |
2327 | spin = false; | |
2328 | ret = 0; | |
2329 | } | |
2330 | ||
2331 | if (!list_empty(&done)) | |
2332 | io_iopoll_complete(ctx, nr_events, &done); | |
2333 | ||
2334 | return ret; | |
2335 | } | |
2336 | ||
2337 | /* | |
d195a66e | 2338 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2339 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2340 | * as a non-spinning completion check. | |
2341 | */ | |
2342 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2343 | long min) | |
2344 | { | |
540e32a0 | 2345 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2346 | int ret; |
2347 | ||
2348 | ret = io_do_iopoll(ctx, nr_events, min); | |
2349 | if (ret < 0) | |
2350 | return ret; | |
eba0a4dd | 2351 | if (*nr_events >= min) |
def596e9 JA |
2352 | return 0; |
2353 | } | |
2354 | ||
2355 | return 1; | |
2356 | } | |
2357 | ||
2358 | /* | |
2359 | * We can't just wait for polled events to come to us, we have to actively | |
2360 | * find and complete them. | |
2361 | */ | |
b2edc0a7 | 2362 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2363 | { |
2364 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2365 | return; | |
2366 | ||
2367 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2368 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2369 | unsigned int nr_events = 0; |
2370 | ||
b2edc0a7 | 2371 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2372 | |
b2edc0a7 PB |
2373 | /* let it sleep and repeat later if can't complete a request */ |
2374 | if (nr_events == 0) | |
2375 | break; | |
08f5439f JA |
2376 | /* |
2377 | * Ensure we allow local-to-the-cpu processing to take place, | |
2378 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2379 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2380 | */ |
3fcee5a6 PB |
2381 | if (need_resched()) { |
2382 | mutex_unlock(&ctx->uring_lock); | |
2383 | cond_resched(); | |
2384 | mutex_lock(&ctx->uring_lock); | |
2385 | } | |
def596e9 JA |
2386 | } |
2387 | mutex_unlock(&ctx->uring_lock); | |
2388 | } | |
2389 | ||
7668b92a | 2390 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2391 | { |
7668b92a | 2392 | unsigned int nr_events = 0; |
2b2ed975 | 2393 | int iters = 0, ret = 0; |
500f9fba | 2394 | |
c7849be9 XW |
2395 | /* |
2396 | * We disallow the app entering submit/complete with polling, but we | |
2397 | * still need to lock the ring to prevent racing with polled issue | |
2398 | * that got punted to a workqueue. | |
2399 | */ | |
2400 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2401 | do { |
a3a0e43f JA |
2402 | /* |
2403 | * Don't enter poll loop if we already have events pending. | |
2404 | * If we do, we can potentially be spinning for commands that | |
2405 | * already triggered a CQE (eg in error). | |
2406 | */ | |
6c503150 PB |
2407 | if (test_bit(0, &ctx->cq_check_overflow)) |
2408 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2409 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2410 | break; |
2411 | ||
500f9fba JA |
2412 | /* |
2413 | * If a submit got punted to a workqueue, we can have the | |
2414 | * application entering polling for a command before it gets | |
2415 | * issued. That app will hold the uring_lock for the duration | |
2416 | * of the poll right here, so we need to take a breather every | |
2417 | * now and then to ensure that the issue has a chance to add | |
2418 | * the poll to the issued list. Otherwise we can spin here | |
2419 | * forever, while the workqueue is stuck trying to acquire the | |
2420 | * very same mutex. | |
2421 | */ | |
2422 | if (!(++iters & 7)) { | |
2423 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2424 | io_run_task_work(); |
500f9fba JA |
2425 | mutex_lock(&ctx->uring_lock); |
2426 | } | |
2427 | ||
7668b92a | 2428 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2429 | if (ret <= 0) |
2430 | break; | |
2431 | ret = 0; | |
7668b92a | 2432 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2433 | |
500f9fba | 2434 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2435 | return ret; |
2436 | } | |
2437 | ||
491381ce | 2438 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2439 | { |
491381ce JA |
2440 | /* |
2441 | * Tell lockdep we inherited freeze protection from submission | |
2442 | * thread. | |
2443 | */ | |
2444 | if (req->flags & REQ_F_ISREG) { | |
2445 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2446 | |
491381ce | 2447 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2448 | } |
491381ce | 2449 | file_end_write(req->file); |
2b188cc1 JA |
2450 | } |
2451 | ||
b63534c4 | 2452 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2453 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2454 | { |
2455 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2456 | int rw, ret; |
b63534c4 | 2457 | struct iov_iter iter; |
b63534c4 | 2458 | |
dc2a6e9a PB |
2459 | /* already prepared */ |
2460 | if (req->async_data) | |
2461 | return true; | |
b63534c4 JA |
2462 | |
2463 | switch (req->opcode) { | |
2464 | case IORING_OP_READV: | |
2465 | case IORING_OP_READ_FIXED: | |
2466 | case IORING_OP_READ: | |
2467 | rw = READ; | |
2468 | break; | |
2469 | case IORING_OP_WRITEV: | |
2470 | case IORING_OP_WRITE_FIXED: | |
2471 | case IORING_OP_WRITE: | |
2472 | rw = WRITE; | |
2473 | break; | |
2474 | default: | |
2475 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2476 | req->opcode); | |
dc2a6e9a | 2477 | return false; |
b63534c4 JA |
2478 | } |
2479 | ||
dc2a6e9a PB |
2480 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2481 | if (ret < 0) | |
2482 | return false; | |
6bf985dc | 2483 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2484 | } |
b63534c4 JA |
2485 | #endif |
2486 | ||
23faba36 | 2487 | static bool io_rw_reissue(struct io_kiocb *req) |
b63534c4 JA |
2488 | { |
2489 | #ifdef CONFIG_BLOCK | |
355afaeb | 2490 | umode_t mode = file_inode(req->file)->i_mode; |
b63534c4 | 2491 | |
355afaeb JA |
2492 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2493 | return false; | |
75c668cd | 2494 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
b63534c4 JA |
2495 | return false; |
2496 | ||
55e6ac1e PB |
2497 | lockdep_assert_held(&req->ctx->uring_lock); |
2498 | ||
37d1e2e3 | 2499 | if (io_resubmit_prep(req)) { |
fdee946d JA |
2500 | refcount_inc(&req->refs); |
2501 | io_queue_async_work(req); | |
b63534c4 | 2502 | return true; |
fdee946d | 2503 | } |
dc2a6e9a | 2504 | req_set_fail_links(req); |
b63534c4 JA |
2505 | #endif |
2506 | return false; | |
2507 | } | |
2508 | ||
a1d7c393 | 2509 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2510 | unsigned int issue_flags) |
a1d7c393 | 2511 | { |
2f8e45f1 PB |
2512 | int cflags = 0; |
2513 | ||
23faba36 PB |
2514 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2515 | return; | |
2f8e45f1 PB |
2516 | if (res != req->result) |
2517 | req_set_fail_links(req); | |
23faba36 | 2518 | |
2f8e45f1 PB |
2519 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2520 | kiocb_end_write(req); | |
2521 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2522 | cflags = io_put_rw_kbuf(req); | |
2523 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2524 | } |
2525 | ||
2526 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2527 | { | |
9adbd45d | 2528 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2529 | |
889fca73 | 2530 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2531 | } |
2532 | ||
def596e9 JA |
2533 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2534 | { | |
9adbd45d | 2535 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2536 | |
491381ce JA |
2537 | if (kiocb->ki_flags & IOCB_WRITE) |
2538 | kiocb_end_write(req); | |
def596e9 | 2539 | |
2d7d6792 | 2540 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2541 | req_set_fail_links(req); |
bbde017a XW |
2542 | |
2543 | WRITE_ONCE(req->result, res); | |
2544 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2545 | smp_wmb(); |
2546 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2547 | } |
2548 | ||
2549 | /* | |
2550 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2551 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2552 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2553 | * accessing the kiocb cookie. | |
2554 | */ | |
2e9dbe90 | 2555 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2556 | { |
2557 | struct io_ring_ctx *ctx = req->ctx; | |
2558 | ||
2559 | /* | |
2560 | * Track whether we have multiple files in our lists. This will impact | |
2561 | * how we do polling eventually, not spinning if we're on potentially | |
2562 | * different devices. | |
2563 | */ | |
540e32a0 | 2564 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2565 | ctx->poll_multi_file = false; |
2566 | } else if (!ctx->poll_multi_file) { | |
2567 | struct io_kiocb *list_req; | |
2568 | ||
540e32a0 | 2569 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2570 | inflight_entry); |
9adbd45d | 2571 | if (list_req->file != req->file) |
def596e9 JA |
2572 | ctx->poll_multi_file = true; |
2573 | } | |
2574 | ||
2575 | /* | |
2576 | * For fast devices, IO may have already completed. If it has, add | |
2577 | * it to the front so we find it first. | |
2578 | */ | |
65a6543d | 2579 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2580 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2581 | else |
d21ffe7e | 2582 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2583 | |
2e9dbe90 XW |
2584 | /* |
2585 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2586 | * task context or in io worker task context. If current task context is | |
2587 | * sq thread, we don't need to check whether should wake up sq thread. | |
2588 | */ | |
2589 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2590 | wq_has_sleeper(&ctx->sq_data->wait)) |
2591 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2592 | } |
2593 | ||
9f13c35b PB |
2594 | static inline void io_state_file_put(struct io_submit_state *state) |
2595 | { | |
02b23a9a PB |
2596 | if (state->file_refs) { |
2597 | fput_many(state->file, state->file_refs); | |
2598 | state->file_refs = 0; | |
2599 | } | |
9a56a232 JA |
2600 | } |
2601 | ||
2602 | /* | |
2603 | * Get as many references to a file as we have IOs left in this submission, | |
2604 | * assuming most submissions are for one file, or at least that each file | |
2605 | * has more than one submission. | |
2606 | */ | |
8da11c19 | 2607 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2608 | { |
2609 | if (!state) | |
2610 | return fget(fd); | |
2611 | ||
6e1271e6 | 2612 | if (state->file_refs) { |
9a56a232 | 2613 | if (state->fd == fd) { |
6e1271e6 | 2614 | state->file_refs--; |
9a56a232 JA |
2615 | return state->file; |
2616 | } | |
02b23a9a | 2617 | io_state_file_put(state); |
9a56a232 JA |
2618 | } |
2619 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2620 | if (unlikely(!state->file)) |
9a56a232 JA |
2621 | return NULL; |
2622 | ||
2623 | state->fd = fd; | |
6e1271e6 | 2624 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2625 | return state->file; |
2626 | } | |
2627 | ||
4503b767 JA |
2628 | static bool io_bdev_nowait(struct block_device *bdev) |
2629 | { | |
9ba0d0c8 | 2630 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2631 | } |
2632 | ||
2b188cc1 JA |
2633 | /* |
2634 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2635 | * any file. For now, just ensure that anything potentially problematic is done | |
2636 | * inline. | |
2637 | */ | |
af197f50 | 2638 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2639 | { |
2640 | umode_t mode = file_inode(file)->i_mode; | |
2641 | ||
4503b767 | 2642 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2643 | if (IS_ENABLED(CONFIG_BLOCK) && |
2644 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2645 | return true; |
2646 | return false; | |
2647 | } | |
2648 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2649 | return true; |
4503b767 | 2650 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2651 | if (IS_ENABLED(CONFIG_BLOCK) && |
2652 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2653 | file->f_op != &io_uring_fops) |
2654 | return true; | |
2655 | return false; | |
2656 | } | |
2b188cc1 | 2657 | |
c5b85625 JA |
2658 | /* any ->read/write should understand O_NONBLOCK */ |
2659 | if (file->f_flags & O_NONBLOCK) | |
2660 | return true; | |
2661 | ||
af197f50 JA |
2662 | if (!(file->f_mode & FMODE_NOWAIT)) |
2663 | return false; | |
2664 | ||
2665 | if (rw == READ) | |
2666 | return file->f_op->read_iter != NULL; | |
2667 | ||
2668 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2669 | } |
2670 | ||
a88fc400 | 2671 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2672 | { |
def596e9 | 2673 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2674 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2675 | struct file *file = req->file; |
09bb8394 JA |
2676 | unsigned ioprio; |
2677 | int ret; | |
2b188cc1 | 2678 | |
75c668cd | 2679 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2680 | req->flags |= REQ_F_ISREG; |
2681 | ||
2b188cc1 | 2682 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2683 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2684 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2685 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2686 | } |
2b188cc1 | 2687 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2688 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2689 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2690 | if (unlikely(ret)) | |
2691 | return ret; | |
2b188cc1 | 2692 | |
75c668cd PB |
2693 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2694 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2695 | req->flags |= REQ_F_NOWAIT; | |
2696 | ||
2b188cc1 JA |
2697 | ioprio = READ_ONCE(sqe->ioprio); |
2698 | if (ioprio) { | |
2699 | ret = ioprio_check_cap(ioprio); | |
2700 | if (ret) | |
09bb8394 | 2701 | return ret; |
2b188cc1 JA |
2702 | |
2703 | kiocb->ki_ioprio = ioprio; | |
2704 | } else | |
2705 | kiocb->ki_ioprio = get_current_ioprio(); | |
2706 | ||
def596e9 | 2707 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2708 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2709 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2710 | return -EOPNOTSUPP; |
2b188cc1 | 2711 | |
def596e9 JA |
2712 | kiocb->ki_flags |= IOCB_HIPRI; |
2713 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2714 | req->iopoll_completed = 0; |
def596e9 | 2715 | } else { |
09bb8394 JA |
2716 | if (kiocb->ki_flags & IOCB_HIPRI) |
2717 | return -EINVAL; | |
def596e9 JA |
2718 | kiocb->ki_complete = io_complete_rw; |
2719 | } | |
9adbd45d | 2720 | |
3529d8c2 JA |
2721 | req->rw.addr = READ_ONCE(sqe->addr); |
2722 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2723 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2724 | return 0; |
2b188cc1 JA |
2725 | } |
2726 | ||
2727 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2728 | { | |
2729 | switch (ret) { | |
2730 | case -EIOCBQUEUED: | |
2731 | break; | |
2732 | case -ERESTARTSYS: | |
2733 | case -ERESTARTNOINTR: | |
2734 | case -ERESTARTNOHAND: | |
2735 | case -ERESTART_RESTARTBLOCK: | |
2736 | /* | |
2737 | * We can't just restart the syscall, since previously | |
2738 | * submitted sqes may already be in progress. Just fail this | |
2739 | * IO with EINTR. | |
2740 | */ | |
2741 | ret = -EINTR; | |
df561f66 | 2742 | fallthrough; |
2b188cc1 JA |
2743 | default: |
2744 | kiocb->ki_complete(kiocb, ret, 0); | |
2745 | } | |
2746 | } | |
2747 | ||
a1d7c393 | 2748 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2749 | unsigned int issue_flags) |
ba816ad6 | 2750 | { |
ba04291e | 2751 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2752 | struct io_async_rw *io = req->async_data; |
ba04291e | 2753 | |
227c0c96 | 2754 | /* add previously done IO, if any */ |
e8c2bc1f | 2755 | if (io && io->bytes_done > 0) { |
227c0c96 | 2756 | if (ret < 0) |
e8c2bc1f | 2757 | ret = io->bytes_done; |
227c0c96 | 2758 | else |
e8c2bc1f | 2759 | ret += io->bytes_done; |
227c0c96 JA |
2760 | } |
2761 | ||
ba04291e JA |
2762 | if (req->flags & REQ_F_CUR_POS) |
2763 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2764 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2765 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2766 | else |
2767 | io_rw_done(kiocb, ret); | |
2768 | } | |
2769 | ||
847595de | 2770 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2771 | { |
9adbd45d JA |
2772 | struct io_ring_ctx *ctx = req->ctx; |
2773 | size_t len = req->rw.len; | |
edafccee | 2774 | struct io_mapped_ubuf *imu; |
4be1c615 | 2775 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2776 | size_t offset; |
2777 | u64 buf_addr; | |
2778 | ||
edafccee JA |
2779 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2780 | return -EFAULT; | |
edafccee JA |
2781 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2782 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2783 | buf_addr = req->rw.addr; |
edafccee JA |
2784 | |
2785 | /* overflow */ | |
2786 | if (buf_addr + len < buf_addr) | |
2787 | return -EFAULT; | |
2788 | /* not inside the mapped region */ | |
2789 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2790 | return -EFAULT; | |
2791 | ||
2792 | /* | |
2793 | * May not be a start of buffer, set size appropriately | |
2794 | * and advance us to the beginning. | |
2795 | */ | |
2796 | offset = buf_addr - imu->ubuf; | |
2797 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2798 | |
2799 | if (offset) { | |
2800 | /* | |
2801 | * Don't use iov_iter_advance() here, as it's really slow for | |
2802 | * using the latter parts of a big fixed buffer - it iterates | |
2803 | * over each segment manually. We can cheat a bit here, because | |
2804 | * we know that: | |
2805 | * | |
2806 | * 1) it's a BVEC iter, we set it up | |
2807 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2808 | * first and last bvec | |
2809 | * | |
2810 | * So just find our index, and adjust the iterator afterwards. | |
2811 | * If the offset is within the first bvec (or the whole first | |
2812 | * bvec, just use iov_iter_advance(). This makes it easier | |
2813 | * since we can just skip the first segment, which may not | |
2814 | * be PAGE_SIZE aligned. | |
2815 | */ | |
2816 | const struct bio_vec *bvec = imu->bvec; | |
2817 | ||
2818 | if (offset <= bvec->bv_len) { | |
2819 | iov_iter_advance(iter, offset); | |
2820 | } else { | |
2821 | unsigned long seg_skip; | |
2822 | ||
2823 | /* skip first vec */ | |
2824 | offset -= bvec->bv_len; | |
2825 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2826 | ||
2827 | iter->bvec = bvec + seg_skip; | |
2828 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2829 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2830 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2831 | } |
2832 | } | |
2833 | ||
847595de | 2834 | return 0; |
edafccee JA |
2835 | } |
2836 | ||
bcda7baa JA |
2837 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2838 | { | |
2839 | if (needs_lock) | |
2840 | mutex_unlock(&ctx->uring_lock); | |
2841 | } | |
2842 | ||
2843 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2844 | { | |
2845 | /* | |
2846 | * "Normal" inline submissions always hold the uring_lock, since we | |
2847 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2848 | * The only exception is when we've detached the request and issue it | |
2849 | * from an async worker thread, grab the lock for that case. | |
2850 | */ | |
2851 | if (needs_lock) | |
2852 | mutex_lock(&ctx->uring_lock); | |
2853 | } | |
2854 | ||
2855 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2856 | int bgid, struct io_buffer *kbuf, | |
2857 | bool needs_lock) | |
2858 | { | |
2859 | struct io_buffer *head; | |
2860 | ||
2861 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2862 | return kbuf; | |
2863 | ||
2864 | io_ring_submit_lock(req->ctx, needs_lock); | |
2865 | ||
2866 | lockdep_assert_held(&req->ctx->uring_lock); | |
2867 | ||
2868 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2869 | if (head) { | |
2870 | if (!list_empty(&head->list)) { | |
2871 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2872 | list); | |
2873 | list_del(&kbuf->list); | |
2874 | } else { | |
2875 | kbuf = head; | |
2876 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2877 | } | |
2878 | if (*len > kbuf->len) | |
2879 | *len = kbuf->len; | |
2880 | } else { | |
2881 | kbuf = ERR_PTR(-ENOBUFS); | |
2882 | } | |
2883 | ||
2884 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2885 | ||
2886 | return kbuf; | |
2887 | } | |
2888 | ||
4d954c25 JA |
2889 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2890 | bool needs_lock) | |
2891 | { | |
2892 | struct io_buffer *kbuf; | |
4f4eeba8 | 2893 | u16 bgid; |
4d954c25 JA |
2894 | |
2895 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2896 | bgid = req->buf_index; |
4d954c25 JA |
2897 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2898 | if (IS_ERR(kbuf)) | |
2899 | return kbuf; | |
2900 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2901 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2902 | return u64_to_user_ptr(kbuf->addr); | |
2903 | } | |
2904 | ||
2905 | #ifdef CONFIG_COMPAT | |
2906 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2907 | bool needs_lock) | |
2908 | { | |
2909 | struct compat_iovec __user *uiov; | |
2910 | compat_ssize_t clen; | |
2911 | void __user *buf; | |
2912 | ssize_t len; | |
2913 | ||
2914 | uiov = u64_to_user_ptr(req->rw.addr); | |
2915 | if (!access_ok(uiov, sizeof(*uiov))) | |
2916 | return -EFAULT; | |
2917 | if (__get_user(clen, &uiov->iov_len)) | |
2918 | return -EFAULT; | |
2919 | if (clen < 0) | |
2920 | return -EINVAL; | |
2921 | ||
2922 | len = clen; | |
2923 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2924 | if (IS_ERR(buf)) | |
2925 | return PTR_ERR(buf); | |
2926 | iov[0].iov_base = buf; | |
2927 | iov[0].iov_len = (compat_size_t) len; | |
2928 | return 0; | |
2929 | } | |
2930 | #endif | |
2931 | ||
2932 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2933 | bool needs_lock) | |
2934 | { | |
2935 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2936 | void __user *buf; | |
2937 | ssize_t len; | |
2938 | ||
2939 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2940 | return -EFAULT; | |
2941 | ||
2942 | len = iov[0].iov_len; | |
2943 | if (len < 0) | |
2944 | return -EINVAL; | |
2945 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2946 | if (IS_ERR(buf)) | |
2947 | return PTR_ERR(buf); | |
2948 | iov[0].iov_base = buf; | |
2949 | iov[0].iov_len = len; | |
2950 | return 0; | |
2951 | } | |
2952 | ||
2953 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2954 | bool needs_lock) | |
2955 | { | |
dddb3e26 JA |
2956 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2957 | struct io_buffer *kbuf; | |
2958 | ||
2959 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2960 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2961 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2962 | return 0; |
dddb3e26 | 2963 | } |
dd201662 | 2964 | if (req->rw.len != 1) |
4d954c25 JA |
2965 | return -EINVAL; |
2966 | ||
2967 | #ifdef CONFIG_COMPAT | |
2968 | if (req->ctx->compat) | |
2969 | return io_compat_import(req, iov, needs_lock); | |
2970 | #endif | |
2971 | ||
2972 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2973 | } | |
2974 | ||
847595de PB |
2975 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2976 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2977 | { |
9adbd45d JA |
2978 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2979 | size_t sqe_len = req->rw.len; | |
847595de | 2980 | u8 opcode = req->opcode; |
4d954c25 | 2981 | ssize_t ret; |
edafccee | 2982 | |
7d009165 | 2983 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2984 | *iovec = NULL; |
9adbd45d | 2985 | return io_import_fixed(req, rw, iter); |
edafccee | 2986 | } |
2b188cc1 | 2987 | |
bcda7baa | 2988 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2989 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2990 | return -EINVAL; |
2991 | ||
3a6820f2 | 2992 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2993 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2994 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2995 | if (IS_ERR(buf)) |
4d954c25 | 2996 | return PTR_ERR(buf); |
3f9d6441 | 2997 | req->rw.len = sqe_len; |
bcda7baa JA |
2998 | } |
2999 | ||
3a6820f2 JA |
3000 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3001 | *iovec = NULL; | |
10fc72e4 | 3002 | return ret; |
3a6820f2 JA |
3003 | } |
3004 | ||
4d954c25 JA |
3005 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3006 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3007 | if (!ret) |
3008 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3009 | *iovec = NULL; |
3010 | return ret; | |
3011 | } | |
3012 | ||
89cd35c5 CH |
3013 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3014 | req->ctx->compat); | |
2b188cc1 JA |
3015 | } |
3016 | ||
0fef9483 JA |
3017 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3018 | { | |
5b09e37e | 3019 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3020 | } |
3021 | ||
31b51510 | 3022 | /* |
32960613 JA |
3023 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3024 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3025 | */ |
4017eb91 | 3026 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3027 | { |
4017eb91 JA |
3028 | struct kiocb *kiocb = &req->rw.kiocb; |
3029 | struct file *file = req->file; | |
32960613 JA |
3030 | ssize_t ret = 0; |
3031 | ||
3032 | /* | |
3033 | * Don't support polled IO through this interface, and we can't | |
3034 | * support non-blocking either. For the latter, this just causes | |
3035 | * the kiocb to be handled from an async context. | |
3036 | */ | |
3037 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3038 | return -EOPNOTSUPP; | |
3039 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3040 | return -EAGAIN; | |
3041 | ||
3042 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3043 | struct iovec iovec; |
32960613 JA |
3044 | ssize_t nr; |
3045 | ||
311ae9e1 PB |
3046 | if (!iov_iter_is_bvec(iter)) { |
3047 | iovec = iov_iter_iovec(iter); | |
3048 | } else { | |
4017eb91 JA |
3049 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3050 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3051 | } |
3052 | ||
32960613 JA |
3053 | if (rw == READ) { |
3054 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3055 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3056 | } else { |
3057 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3058 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3059 | } |
3060 | ||
3061 | if (nr < 0) { | |
3062 | if (!ret) | |
3063 | ret = nr; | |
3064 | break; | |
3065 | } | |
3066 | ret += nr; | |
3067 | if (nr != iovec.iov_len) | |
3068 | break; | |
4017eb91 JA |
3069 | req->rw.len -= nr; |
3070 | req->rw.addr += nr; | |
32960613 JA |
3071 | iov_iter_advance(iter, nr); |
3072 | } | |
3073 | ||
3074 | return ret; | |
3075 | } | |
3076 | ||
ff6165b2 JA |
3077 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3078 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3079 | { |
e8c2bc1f | 3080 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3081 | |
ff6165b2 | 3082 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3083 | rw->free_iovec = iovec; |
227c0c96 | 3084 | rw->bytes_done = 0; |
ff6165b2 | 3085 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3086 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3087 | return; |
b64e3444 | 3088 | if (!iovec) { |
ff6165b2 JA |
3089 | unsigned iov_off = 0; |
3090 | ||
3091 | rw->iter.iov = rw->fast_iov; | |
3092 | if (iter->iov != fast_iov) { | |
3093 | iov_off = iter->iov - fast_iov; | |
3094 | rw->iter.iov += iov_off; | |
3095 | } | |
3096 | if (rw->fast_iov != fast_iov) | |
3097 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3098 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3099 | } else { |
3100 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3101 | } |
3102 | } | |
3103 | ||
e8c2bc1f | 3104 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3105 | { |
e8c2bc1f JA |
3106 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3107 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3108 | return req->async_data == NULL; | |
3d9932a8 XW |
3109 | } |
3110 | ||
e8c2bc1f | 3111 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3112 | { |
e8c2bc1f | 3113 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3114 | return 0; |
3d9932a8 | 3115 | |
e8c2bc1f | 3116 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3117 | } |
3118 | ||
ff6165b2 JA |
3119 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3120 | const struct iovec *fast_iov, | |
227c0c96 | 3121 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3122 | { |
e8c2bc1f | 3123 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3124 | return 0; |
e8c2bc1f | 3125 | if (!req->async_data) { |
6bf985dc PB |
3126 | if (__io_alloc_async_data(req)) { |
3127 | kfree(iovec); | |
5d204bcf | 3128 | return -ENOMEM; |
6bf985dc | 3129 | } |
b7bb4f7d | 3130 | |
ff6165b2 | 3131 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3132 | } |
b7bb4f7d | 3133 | return 0; |
f67676d1 JA |
3134 | } |
3135 | ||
73debe68 | 3136 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3137 | { |
e8c2bc1f | 3138 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3139 | struct iovec *iov = iorw->fast_iov; |
847595de | 3140 | int ret; |
c3e330a4 | 3141 | |
2846c481 | 3142 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3143 | if (unlikely(ret < 0)) |
3144 | return ret; | |
3145 | ||
ab0b196c PB |
3146 | iorw->bytes_done = 0; |
3147 | iorw->free_iovec = iov; | |
3148 | if (iov) | |
3149 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3150 | return 0; |
3151 | } | |
3152 | ||
73debe68 | 3153 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3154 | { |
3529d8c2 JA |
3155 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3156 | return -EBADF; | |
93642ef8 | 3157 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3158 | } |
3159 | ||
c1dd91d1 JA |
3160 | /* |
3161 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3162 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3163 | * This gets called when the page is unlocked, and we generally expect that to | |
3164 | * happen when the page IO is completed and the page is now uptodate. This will | |
3165 | * queue a task_work based retry of the operation, attempting to copy the data | |
3166 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3167 | * do a thread based blocking retry of the operation. That's the unexpected | |
3168 | * slow path. | |
3169 | */ | |
bcf5a063 JA |
3170 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3171 | int sync, void *arg) | |
3172 | { | |
3173 | struct wait_page_queue *wpq; | |
3174 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3175 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3176 | |
3177 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3178 | ||
cdc8fcb4 LT |
3179 | if (!wake_page_match(wpq, key)) |
3180 | return 0; | |
3181 | ||
c8d317aa | 3182 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3183 | list_del_init(&wait->entry); |
3184 | ||
bcf5a063 JA |
3185 | /* submit ref gets dropped, acquire a new one */ |
3186 | refcount_inc(&req->refs); | |
921b9054 | 3187 | io_req_task_queue(req); |
bcf5a063 JA |
3188 | return 1; |
3189 | } | |
3190 | ||
c1dd91d1 JA |
3191 | /* |
3192 | * This controls whether a given IO request should be armed for async page | |
3193 | * based retry. If we return false here, the request is handed to the async | |
3194 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3195 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3196 | * will either succeed because the page is now uptodate and unlocked, or it | |
3197 | * will register a callback when the page is unlocked at IO completion. Through | |
3198 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3199 | * That retry will attempt the buffered read again. The retry will generally | |
3200 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3201 | * async worker threads for a blocking retry. | |
3202 | */ | |
227c0c96 | 3203 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3204 | { |
e8c2bc1f JA |
3205 | struct io_async_rw *rw = req->async_data; |
3206 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3207 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3208 | |
bcf5a063 JA |
3209 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3210 | if (req->flags & REQ_F_NOWAIT) | |
3211 | return false; | |
f67676d1 | 3212 | |
227c0c96 | 3213 | /* Only for buffered IO */ |
3b2a4439 | 3214 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3215 | return false; |
3b2a4439 | 3216 | |
bcf5a063 JA |
3217 | /* |
3218 | * just use poll if we can, and don't attempt if the fs doesn't | |
3219 | * support callback based unlocks | |
3220 | */ | |
3221 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3222 | return false; | |
f67676d1 | 3223 | |
3b2a4439 JA |
3224 | wait->wait.func = io_async_buf_func; |
3225 | wait->wait.private = req; | |
3226 | wait->wait.flags = 0; | |
3227 | INIT_LIST_HEAD(&wait->wait.entry); | |
3228 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3229 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3230 | kiocb->ki_waitq = wait; |
3b2a4439 | 3231 | return true; |
bcf5a063 JA |
3232 | } |
3233 | ||
3234 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3235 | { | |
3236 | if (req->file->f_op->read_iter) | |
3237 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3238 | else if (req->file->f_op->read) |
4017eb91 | 3239 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3240 | else |
3241 | return -EINVAL; | |
f67676d1 JA |
3242 | } |
3243 | ||
889fca73 | 3244 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3245 | { |
3246 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3247 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3248 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3249 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3250 | ssize_t io_size, ret, ret2; |
45d189c6 | 3251 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3252 | |
2846c481 | 3253 | if (rw) { |
e8c2bc1f | 3254 | iter = &rw->iter; |
2846c481 PB |
3255 | iovec = NULL; |
3256 | } else { | |
3257 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3258 | if (ret < 0) | |
3259 | return ret; | |
3260 | } | |
632546c4 | 3261 | io_size = iov_iter_count(iter); |
fa15bafb | 3262 | req->result = io_size; |
2b188cc1 | 3263 | |
fd6c2e4c JA |
3264 | /* Ensure we clear previously set non-block flag */ |
3265 | if (!force_nonblock) | |
29de5f6a | 3266 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3267 | else |
3268 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3269 | ||
24c74678 | 3270 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3271 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3272 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3273 | return ret ?: -EAGAIN; |
6713e7a6 | 3274 | } |
9e645e11 | 3275 | |
632546c4 | 3276 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3277 | if (unlikely(ret)) { |
3278 | kfree(iovec); | |
3279 | return ret; | |
3280 | } | |
2b188cc1 | 3281 | |
227c0c96 | 3282 | ret = io_iter_do_read(req, iter); |
32960613 | 3283 | |
57cd657b | 3284 | if (ret == -EIOCBQUEUED) { |
fe1cdd55 | 3285 | goto out_free; |
227c0c96 | 3286 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3287 | /* IOPOLL retry should happen for io-wq threads */ |
3288 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3289 | goto done; |
75c668cd PB |
3290 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3291 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3292 | goto done; |
84216315 | 3293 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3294 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3295 | ret = 0; |
7335e3bf | 3296 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3297 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3298 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3299 | goto done; |
227c0c96 JA |
3300 | } |
3301 | ||
227c0c96 | 3302 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3303 | if (ret2) |
3304 | return ret2; | |
3305 | ||
fe1cdd55 | 3306 | iovec = NULL; |
e8c2bc1f | 3307 | rw = req->async_data; |
227c0c96 | 3308 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3309 | iter = &rw->iter; |
227c0c96 | 3310 | |
b23df91b PB |
3311 | do { |
3312 | io_size -= ret; | |
3313 | rw->bytes_done += ret; | |
3314 | /* if we can retry, do so with the callbacks armed */ | |
3315 | if (!io_rw_should_retry(req)) { | |
3316 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3317 | return -EAGAIN; | |
3318 | } | |
3319 | ||
3320 | /* | |
3321 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3322 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3323 | * desired page gets unlocked. We can also get a partial read | |
3324 | * here, and if we do, then just retry at the new offset. | |
3325 | */ | |
3326 | ret = io_iter_do_read(req, iter); | |
3327 | if (ret == -EIOCBQUEUED) | |
3328 | return 0; | |
227c0c96 | 3329 | /* we got some bytes, but not all. retry. */ |
b23df91b | 3330 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3331 | done: |
889fca73 | 3332 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3333 | out_free: |
3334 | /* it's faster to check here then delegate to kfree */ | |
3335 | if (iovec) | |
3336 | kfree(iovec); | |
5ea5dd45 | 3337 | return 0; |
2b188cc1 JA |
3338 | } |
3339 | ||
73debe68 | 3340 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3341 | { |
3529d8c2 JA |
3342 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3343 | return -EBADF; | |
93642ef8 | 3344 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3345 | } |
3346 | ||
889fca73 | 3347 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3348 | { |
3349 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3350 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3351 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3352 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3353 | ssize_t ret, ret2, io_size; |
45d189c6 | 3354 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3355 | |
2846c481 | 3356 | if (rw) { |
e8c2bc1f | 3357 | iter = &rw->iter; |
2846c481 PB |
3358 | iovec = NULL; |
3359 | } else { | |
3360 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3361 | if (ret < 0) | |
3362 | return ret; | |
3363 | } | |
632546c4 | 3364 | io_size = iov_iter_count(iter); |
fa15bafb | 3365 | req->result = io_size; |
2b188cc1 | 3366 | |
fd6c2e4c JA |
3367 | /* Ensure we clear previously set non-block flag */ |
3368 | if (!force_nonblock) | |
a88fc400 PB |
3369 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3370 | else | |
3371 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3372 | |
24c74678 | 3373 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3374 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3375 | goto copy_iov; |
31b51510 | 3376 | |
10d59345 JA |
3377 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3378 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3379 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3380 | goto copy_iov; |
31b51510 | 3381 | |
632546c4 | 3382 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3383 | if (unlikely(ret)) |
3384 | goto out_free; | |
4ed734b0 | 3385 | |
fa15bafb PB |
3386 | /* |
3387 | * Open-code file_start_write here to grab freeze protection, | |
3388 | * which will be released by another thread in | |
3389 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3390 | * released so that it doesn't complain about the held lock when | |
3391 | * we return to userspace. | |
3392 | */ | |
3393 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3394 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3395 | __sb_writers_release(file_inode(req->file)->i_sb, |
3396 | SB_FREEZE_WRITE); | |
3397 | } | |
3398 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3399 | |
fa15bafb | 3400 | if (req->file->f_op->write_iter) |
ff6165b2 | 3401 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3402 | else if (req->file->f_op->write) |
4017eb91 | 3403 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3404 | else |
3405 | ret2 = -EINVAL; | |
4ed734b0 | 3406 | |
fa15bafb PB |
3407 | /* |
3408 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3409 | * retry them without IOCB_NOWAIT. | |
3410 | */ | |
3411 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3412 | ret2 = -EAGAIN; | |
75c668cd PB |
3413 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3414 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3415 | goto done; |
fa15bafb | 3416 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3417 | /* IOPOLL retry should happen for io-wq threads */ |
3418 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3419 | goto copy_iov; | |
355afaeb | 3420 | done: |
889fca73 | 3421 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3422 | } else { |
f67676d1 | 3423 | copy_iov: |
84216315 | 3424 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3425 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3426 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3427 | return ret ?: -EAGAIN; |
2b188cc1 | 3428 | } |
31b51510 | 3429 | out_free: |
f261c168 | 3430 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3431 | if (iovec) |
6f2cc166 | 3432 | kfree(iovec); |
2b188cc1 JA |
3433 | return ret; |
3434 | } | |
3435 | ||
80a261fd JA |
3436 | static int io_renameat_prep(struct io_kiocb *req, |
3437 | const struct io_uring_sqe *sqe) | |
3438 | { | |
3439 | struct io_rename *ren = &req->rename; | |
3440 | const char __user *oldf, *newf; | |
3441 | ||
3442 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3443 | return -EBADF; | |
3444 | ||
3445 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3446 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3447 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3448 | ren->new_dfd = READ_ONCE(sqe->len); | |
3449 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3450 | ||
3451 | ren->oldpath = getname(oldf); | |
3452 | if (IS_ERR(ren->oldpath)) | |
3453 | return PTR_ERR(ren->oldpath); | |
3454 | ||
3455 | ren->newpath = getname(newf); | |
3456 | if (IS_ERR(ren->newpath)) { | |
3457 | putname(ren->oldpath); | |
3458 | return PTR_ERR(ren->newpath); | |
3459 | } | |
3460 | ||
3461 | req->flags |= REQ_F_NEED_CLEANUP; | |
3462 | return 0; | |
3463 | } | |
3464 | ||
45d189c6 | 3465 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3466 | { |
3467 | struct io_rename *ren = &req->rename; | |
3468 | int ret; | |
3469 | ||
45d189c6 | 3470 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3471 | return -EAGAIN; |
3472 | ||
3473 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3474 | ren->newpath, ren->flags); | |
3475 | ||
3476 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3477 | if (ret < 0) | |
3478 | req_set_fail_links(req); | |
3479 | io_req_complete(req, ret); | |
3480 | return 0; | |
3481 | } | |
3482 | ||
14a1143b JA |
3483 | static int io_unlinkat_prep(struct io_kiocb *req, |
3484 | const struct io_uring_sqe *sqe) | |
3485 | { | |
3486 | struct io_unlink *un = &req->unlink; | |
3487 | const char __user *fname; | |
3488 | ||
3489 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3490 | return -EBADF; | |
3491 | ||
3492 | un->dfd = READ_ONCE(sqe->fd); | |
3493 | ||
3494 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3495 | if (un->flags & ~AT_REMOVEDIR) | |
3496 | return -EINVAL; | |
3497 | ||
3498 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3499 | un->filename = getname(fname); | |
3500 | if (IS_ERR(un->filename)) | |
3501 | return PTR_ERR(un->filename); | |
3502 | ||
3503 | req->flags |= REQ_F_NEED_CLEANUP; | |
3504 | return 0; | |
3505 | } | |
3506 | ||
45d189c6 | 3507 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3508 | { |
3509 | struct io_unlink *un = &req->unlink; | |
3510 | int ret; | |
3511 | ||
45d189c6 | 3512 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3513 | return -EAGAIN; |
3514 | ||
3515 | if (un->flags & AT_REMOVEDIR) | |
3516 | ret = do_rmdir(un->dfd, un->filename); | |
3517 | else | |
3518 | ret = do_unlinkat(un->dfd, un->filename); | |
3519 | ||
3520 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3521 | if (ret < 0) | |
3522 | req_set_fail_links(req); | |
3523 | io_req_complete(req, ret); | |
3524 | return 0; | |
3525 | } | |
3526 | ||
36f4fa68 JA |
3527 | static int io_shutdown_prep(struct io_kiocb *req, |
3528 | const struct io_uring_sqe *sqe) | |
3529 | { | |
3530 | #if defined(CONFIG_NET) | |
3531 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3532 | return -EINVAL; | |
3533 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3534 | sqe->buf_index) | |
3535 | return -EINVAL; | |
3536 | ||
3537 | req->shutdown.how = READ_ONCE(sqe->len); | |
3538 | return 0; | |
3539 | #else | |
3540 | return -EOPNOTSUPP; | |
3541 | #endif | |
3542 | } | |
3543 | ||
45d189c6 | 3544 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3545 | { |
3546 | #if defined(CONFIG_NET) | |
3547 | struct socket *sock; | |
3548 | int ret; | |
3549 | ||
45d189c6 | 3550 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3551 | return -EAGAIN; |
3552 | ||
48aba79b | 3553 | sock = sock_from_file(req->file); |
36f4fa68 | 3554 | if (unlikely(!sock)) |
48aba79b | 3555 | return -ENOTSOCK; |
36f4fa68 JA |
3556 | |
3557 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3558 | if (ret < 0) |
3559 | req_set_fail_links(req); | |
36f4fa68 JA |
3560 | io_req_complete(req, ret); |
3561 | return 0; | |
3562 | #else | |
3563 | return -EOPNOTSUPP; | |
3564 | #endif | |
3565 | } | |
3566 | ||
f2a8d5c7 PB |
3567 | static int __io_splice_prep(struct io_kiocb *req, |
3568 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3569 | { |
3570 | struct io_splice* sp = &req->splice; | |
3571 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3572 | |
3232dd02 PB |
3573 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3574 | return -EINVAL; | |
7d67af2c PB |
3575 | |
3576 | sp->file_in = NULL; | |
7d67af2c PB |
3577 | sp->len = READ_ONCE(sqe->len); |
3578 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3579 | ||
3580 | if (unlikely(sp->flags & ~valid_flags)) | |
3581 | return -EINVAL; | |
3582 | ||
8371adf5 PB |
3583 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3584 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3585 | if (!sp->file_in) | |
3586 | return -EBADF; | |
7d67af2c PB |
3587 | req->flags |= REQ_F_NEED_CLEANUP; |
3588 | ||
7cdaf587 XW |
3589 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3590 | /* | |
3591 | * Splice operation will be punted aync, and here need to | |
3592 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3593 | */ | |
3594 | io_req_init_async(req); | |
7d67af2c | 3595 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3596 | } |
7d67af2c PB |
3597 | |
3598 | return 0; | |
3599 | } | |
3600 | ||
f2a8d5c7 PB |
3601 | static int io_tee_prep(struct io_kiocb *req, |
3602 | const struct io_uring_sqe *sqe) | |
3603 | { | |
3604 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3605 | return -EINVAL; | |
3606 | return __io_splice_prep(req, sqe); | |
3607 | } | |
3608 | ||
45d189c6 | 3609 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3610 | { |
3611 | struct io_splice *sp = &req->splice; | |
3612 | struct file *in = sp->file_in; | |
3613 | struct file *out = sp->file_out; | |
3614 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3615 | long ret = 0; | |
3616 | ||
45d189c6 | 3617 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3618 | return -EAGAIN; |
3619 | if (sp->len) | |
3620 | ret = do_tee(in, out, sp->len, flags); | |
3621 | ||
3622 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3623 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3624 | ||
f2a8d5c7 PB |
3625 | if (ret != sp->len) |
3626 | req_set_fail_links(req); | |
e1e16097 | 3627 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3628 | return 0; |
3629 | } | |
3630 | ||
3631 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3632 | { | |
3633 | struct io_splice* sp = &req->splice; | |
3634 | ||
3635 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3636 | sp->off_out = READ_ONCE(sqe->off); | |
3637 | return __io_splice_prep(req, sqe); | |
3638 | } | |
3639 | ||
45d189c6 | 3640 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3641 | { |
3642 | struct io_splice *sp = &req->splice; | |
3643 | struct file *in = sp->file_in; | |
3644 | struct file *out = sp->file_out; | |
3645 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3646 | loff_t *poff_in, *poff_out; | |
c9687426 | 3647 | long ret = 0; |
7d67af2c | 3648 | |
45d189c6 | 3649 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3650 | return -EAGAIN; |
7d67af2c PB |
3651 | |
3652 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3653 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3654 | |
948a7749 | 3655 | if (sp->len) |
c9687426 | 3656 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3657 | |
3658 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3659 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3660 | ||
7d67af2c PB |
3661 | if (ret != sp->len) |
3662 | req_set_fail_links(req); | |
e1e16097 | 3663 | io_req_complete(req, ret); |
7d67af2c PB |
3664 | return 0; |
3665 | } | |
3666 | ||
2b188cc1 JA |
3667 | /* |
3668 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3669 | */ | |
889fca73 | 3670 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3671 | { |
3672 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3673 | |
def596e9 JA |
3674 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3675 | return -EINVAL; | |
3676 | ||
889fca73 | 3677 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3678 | return 0; |
3679 | } | |
3680 | ||
1155c76a | 3681 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3682 | { |
6b06314c | 3683 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3684 | |
09bb8394 JA |
3685 | if (!req->file) |
3686 | return -EBADF; | |
c992fe29 | 3687 | |
6b06314c | 3688 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3689 | return -EINVAL; |
edafccee | 3690 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3691 | return -EINVAL; |
3692 | ||
8ed8d3c3 JA |
3693 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3694 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3695 | return -EINVAL; | |
3696 | ||
3697 | req->sync.off = READ_ONCE(sqe->off); | |
3698 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3699 | return 0; |
3700 | } | |
3701 | ||
45d189c6 | 3702 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3703 | { |
8ed8d3c3 | 3704 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3705 | int ret; |
3706 | ||
ac45abc0 | 3707 | /* fsync always requires a blocking context */ |
45d189c6 | 3708 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3709 | return -EAGAIN; |
3710 | ||
9adbd45d | 3711 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3712 | end > 0 ? end : LLONG_MAX, |
3713 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3714 | if (ret < 0) | |
3715 | req_set_fail_links(req); | |
e1e16097 | 3716 | io_req_complete(req, ret); |
c992fe29 CH |
3717 | return 0; |
3718 | } | |
3719 | ||
d63d1b5e JA |
3720 | static int io_fallocate_prep(struct io_kiocb *req, |
3721 | const struct io_uring_sqe *sqe) | |
3722 | { | |
3723 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3724 | return -EINVAL; | |
3232dd02 PB |
3725 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3726 | return -EINVAL; | |
d63d1b5e JA |
3727 | |
3728 | req->sync.off = READ_ONCE(sqe->off); | |
3729 | req->sync.len = READ_ONCE(sqe->addr); | |
3730 | req->sync.mode = READ_ONCE(sqe->len); | |
3731 | return 0; | |
3732 | } | |
3733 | ||
45d189c6 | 3734 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3735 | { |
ac45abc0 PB |
3736 | int ret; |
3737 | ||
d63d1b5e | 3738 | /* fallocate always requiring blocking context */ |
45d189c6 | 3739 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3740 | return -EAGAIN; |
ac45abc0 PB |
3741 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3742 | req->sync.len); | |
ac45abc0 PB |
3743 | if (ret < 0) |
3744 | req_set_fail_links(req); | |
e1e16097 | 3745 | io_req_complete(req, ret); |
5d17b4a4 JA |
3746 | return 0; |
3747 | } | |
3748 | ||
ec65fea5 | 3749 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3750 | { |
f8748881 | 3751 | const char __user *fname; |
15b71abe | 3752 | int ret; |
b7bb4f7d | 3753 | |
ec65fea5 | 3754 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3755 | return -EINVAL; |
ec65fea5 | 3756 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3757 | return -EBADF; |
03b1230c | 3758 | |
ec65fea5 PB |
3759 | /* open.how should be already initialised */ |
3760 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3761 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3762 | |
25e72d10 PB |
3763 | req->open.dfd = READ_ONCE(sqe->fd); |
3764 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3765 | req->open.filename = getname(fname); |
15b71abe JA |
3766 | if (IS_ERR(req->open.filename)) { |
3767 | ret = PTR_ERR(req->open.filename); | |
3768 | req->open.filename = NULL; | |
3769 | return ret; | |
3770 | } | |
4022e7af | 3771 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3772 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3773 | return 0; |
03b1230c JA |
3774 | } |
3775 | ||
ec65fea5 PB |
3776 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3777 | { | |
3778 | u64 flags, mode; | |
3779 | ||
14587a46 | 3780 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3781 | return -EINVAL; |
ec65fea5 PB |
3782 | mode = READ_ONCE(sqe->len); |
3783 | flags = READ_ONCE(sqe->open_flags); | |
3784 | req->open.how = build_open_how(flags, mode); | |
3785 | return __io_openat_prep(req, sqe); | |
3786 | } | |
3787 | ||
cebdb986 | 3788 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3789 | { |
cebdb986 | 3790 | struct open_how __user *how; |
cebdb986 | 3791 | size_t len; |
0fa03c62 JA |
3792 | int ret; |
3793 | ||
14587a46 | 3794 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3795 | return -EINVAL; |
cebdb986 JA |
3796 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3797 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3798 | if (len < OPEN_HOW_SIZE_VER0) |
3799 | return -EINVAL; | |
3529d8c2 | 3800 | |
cebdb986 JA |
3801 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3802 | len); | |
3803 | if (ret) | |
3804 | return ret; | |
3529d8c2 | 3805 | |
ec65fea5 | 3806 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3807 | } |
3808 | ||
45d189c6 | 3809 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3810 | { |
3811 | struct open_flags op; | |
15b71abe | 3812 | struct file *file; |
3a81fd02 JA |
3813 | bool nonblock_set; |
3814 | bool resolve_nonblock; | |
15b71abe JA |
3815 | int ret; |
3816 | ||
cebdb986 | 3817 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3818 | if (ret) |
3819 | goto err; | |
3a81fd02 JA |
3820 | nonblock_set = op.open_flag & O_NONBLOCK; |
3821 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3822 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3823 | /* |
3824 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3825 | * it'll always -EAGAIN | |
3826 | */ | |
3827 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3828 | return -EAGAIN; | |
3829 | op.lookup_flags |= LOOKUP_CACHED; | |
3830 | op.open_flag |= O_NONBLOCK; | |
3831 | } | |
15b71abe | 3832 | |
4022e7af | 3833 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3834 | if (ret < 0) |
3835 | goto err; | |
3836 | ||
3837 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3838 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3839 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3840 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3841 | /* |
3a81fd02 JA |
3842 | * We could hang on to this 'fd', but seems like marginal |
3843 | * gain for something that is now known to be a slower path. | |
3844 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3845 | */ |
3a81fd02 JA |
3846 | put_unused_fd(ret); |
3847 | return -EAGAIN; | |
3848 | } | |
3849 | ||
15b71abe JA |
3850 | if (IS_ERR(file)) { |
3851 | put_unused_fd(ret); | |
3852 | ret = PTR_ERR(file); | |
3853 | } else { | |
45d189c6 | 3854 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3855 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3856 | fsnotify_open(file); |
3857 | fd_install(ret, file); | |
3858 | } | |
3859 | err: | |
3860 | putname(req->open.filename); | |
8fef80bf | 3861 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3862 | if (ret < 0) |
3863 | req_set_fail_links(req); | |
e1e16097 | 3864 | io_req_complete(req, ret); |
15b71abe JA |
3865 | return 0; |
3866 | } | |
3867 | ||
45d189c6 | 3868 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3869 | { |
45d189c6 | 3870 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
cebdb986 JA |
3871 | } |
3872 | ||
067524e9 JA |
3873 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3874 | const struct io_uring_sqe *sqe) | |
3875 | { | |
3876 | struct io_provide_buf *p = &req->pbuf; | |
3877 | u64 tmp; | |
3878 | ||
3879 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3880 | return -EINVAL; | |
3881 | ||
3882 | tmp = READ_ONCE(sqe->fd); | |
3883 | if (!tmp || tmp > USHRT_MAX) | |
3884 | return -EINVAL; | |
3885 | ||
3886 | memset(p, 0, sizeof(*p)); | |
3887 | p->nbufs = tmp; | |
3888 | p->bgid = READ_ONCE(sqe->buf_group); | |
3889 | return 0; | |
3890 | } | |
3891 | ||
3892 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3893 | int bgid, unsigned nbufs) | |
3894 | { | |
3895 | unsigned i = 0; | |
3896 | ||
3897 | /* shouldn't happen */ | |
3898 | if (!nbufs) | |
3899 | return 0; | |
3900 | ||
3901 | /* the head kbuf is the list itself */ | |
3902 | while (!list_empty(&buf->list)) { | |
3903 | struct io_buffer *nxt; | |
3904 | ||
3905 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3906 | list_del(&nxt->list); | |
3907 | kfree(nxt); | |
3908 | if (++i == nbufs) | |
3909 | return i; | |
3910 | } | |
3911 | i++; | |
3912 | kfree(buf); | |
3913 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3914 | ||
3915 | return i; | |
3916 | } | |
3917 | ||
889fca73 | 3918 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3919 | { |
3920 | struct io_provide_buf *p = &req->pbuf; | |
3921 | struct io_ring_ctx *ctx = req->ctx; | |
3922 | struct io_buffer *head; | |
3923 | int ret = 0; | |
45d189c6 | 3924 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3925 | |
3926 | io_ring_submit_lock(ctx, !force_nonblock); | |
3927 | ||
3928 | lockdep_assert_held(&ctx->uring_lock); | |
3929 | ||
3930 | ret = -ENOENT; | |
3931 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3932 | if (head) | |
3933 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3934 | if (ret < 0) |
3935 | req_set_fail_links(req); | |
067524e9 | 3936 | |
31bff9a5 PB |
3937 | /* need to hold the lock to complete IOPOLL requests */ |
3938 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 3939 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
3940 | io_ring_submit_unlock(ctx, !force_nonblock); |
3941 | } else { | |
3942 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 3943 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 3944 | } |
067524e9 JA |
3945 | return 0; |
3946 | } | |
3947 | ||
ddf0322d JA |
3948 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3949 | const struct io_uring_sqe *sqe) | |
3950 | { | |
3951 | struct io_provide_buf *p = &req->pbuf; | |
3952 | u64 tmp; | |
3953 | ||
3954 | if (sqe->ioprio || sqe->rw_flags) | |
3955 | return -EINVAL; | |
3956 | ||
3957 | tmp = READ_ONCE(sqe->fd); | |
3958 | if (!tmp || tmp > USHRT_MAX) | |
3959 | return -E2BIG; | |
3960 | p->nbufs = tmp; | |
3961 | p->addr = READ_ONCE(sqe->addr); | |
3962 | p->len = READ_ONCE(sqe->len); | |
3963 | ||
efe68c1c | 3964 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3965 | return -EFAULT; |
3966 | ||
3967 | p->bgid = READ_ONCE(sqe->buf_group); | |
3968 | tmp = READ_ONCE(sqe->off); | |
3969 | if (tmp > USHRT_MAX) | |
3970 | return -E2BIG; | |
3971 | p->bid = tmp; | |
3972 | return 0; | |
3973 | } | |
3974 | ||
3975 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3976 | { | |
3977 | struct io_buffer *buf; | |
3978 | u64 addr = pbuf->addr; | |
3979 | int i, bid = pbuf->bid; | |
3980 | ||
3981 | for (i = 0; i < pbuf->nbufs; i++) { | |
3982 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3983 | if (!buf) | |
3984 | break; | |
3985 | ||
3986 | buf->addr = addr; | |
3987 | buf->len = pbuf->len; | |
3988 | buf->bid = bid; | |
3989 | addr += pbuf->len; | |
3990 | bid++; | |
3991 | if (!*head) { | |
3992 | INIT_LIST_HEAD(&buf->list); | |
3993 | *head = buf; | |
3994 | } else { | |
3995 | list_add_tail(&buf->list, &(*head)->list); | |
3996 | } | |
3997 | } | |
3998 | ||
3999 | return i ? i : -ENOMEM; | |
4000 | } | |
4001 | ||
889fca73 | 4002 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4003 | { |
4004 | struct io_provide_buf *p = &req->pbuf; | |
4005 | struct io_ring_ctx *ctx = req->ctx; | |
4006 | struct io_buffer *head, *list; | |
4007 | int ret = 0; | |
45d189c6 | 4008 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4009 | |
4010 | io_ring_submit_lock(ctx, !force_nonblock); | |
4011 | ||
4012 | lockdep_assert_held(&ctx->uring_lock); | |
4013 | ||
4014 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
4015 | ||
4016 | ret = io_add_buffers(p, &head); | |
4017 | if (ret < 0) | |
4018 | goto out; | |
4019 | ||
4020 | if (!list) { | |
4021 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
4022 | GFP_KERNEL); | |
4023 | if (ret < 0) { | |
067524e9 | 4024 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4025 | goto out; |
4026 | } | |
4027 | } | |
4028 | out: | |
ddf0322d JA |
4029 | if (ret < 0) |
4030 | req_set_fail_links(req); | |
31bff9a5 PB |
4031 | |
4032 | /* need to hold the lock to complete IOPOLL requests */ | |
4033 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4034 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4035 | io_ring_submit_unlock(ctx, !force_nonblock); |
4036 | } else { | |
4037 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4038 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4039 | } |
ddf0322d | 4040 | return 0; |
cebdb986 JA |
4041 | } |
4042 | ||
3e4827b0 JA |
4043 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4044 | const struct io_uring_sqe *sqe) | |
4045 | { | |
4046 | #if defined(CONFIG_EPOLL) | |
4047 | if (sqe->ioprio || sqe->buf_index) | |
4048 | return -EINVAL; | |
6ca56f84 | 4049 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4050 | return -EINVAL; |
3e4827b0 JA |
4051 | |
4052 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4053 | req->epoll.op = READ_ONCE(sqe->len); | |
4054 | req->epoll.fd = READ_ONCE(sqe->off); | |
4055 | ||
4056 | if (ep_op_has_event(req->epoll.op)) { | |
4057 | struct epoll_event __user *ev; | |
4058 | ||
4059 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4060 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4061 | return -EFAULT; | |
4062 | } | |
4063 | ||
4064 | return 0; | |
4065 | #else | |
4066 | return -EOPNOTSUPP; | |
4067 | #endif | |
4068 | } | |
4069 | ||
889fca73 | 4070 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4071 | { |
4072 | #if defined(CONFIG_EPOLL) | |
4073 | struct io_epoll *ie = &req->epoll; | |
4074 | int ret; | |
45d189c6 | 4075 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4076 | |
4077 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4078 | if (force_nonblock && ret == -EAGAIN) | |
4079 | return -EAGAIN; | |
4080 | ||
4081 | if (ret < 0) | |
4082 | req_set_fail_links(req); | |
889fca73 | 4083 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4084 | return 0; |
4085 | #else | |
4086 | return -EOPNOTSUPP; | |
4087 | #endif | |
4088 | } | |
4089 | ||
c1ca757b JA |
4090 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4091 | { | |
4092 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4093 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4094 | return -EINVAL; | |
3232dd02 PB |
4095 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4096 | return -EINVAL; | |
c1ca757b JA |
4097 | |
4098 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4099 | req->madvise.len = READ_ONCE(sqe->len); | |
4100 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4101 | return 0; | |
4102 | #else | |
4103 | return -EOPNOTSUPP; | |
4104 | #endif | |
4105 | } | |
4106 | ||
45d189c6 | 4107 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4108 | { |
4109 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4110 | struct io_madvise *ma = &req->madvise; | |
4111 | int ret; | |
4112 | ||
45d189c6 | 4113 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4114 | return -EAGAIN; |
4115 | ||
0726b01e | 4116 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4117 | if (ret < 0) |
4118 | req_set_fail_links(req); | |
e1e16097 | 4119 | io_req_complete(req, ret); |
c1ca757b JA |
4120 | return 0; |
4121 | #else | |
4122 | return -EOPNOTSUPP; | |
4123 | #endif | |
4124 | } | |
4125 | ||
4840e418 JA |
4126 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4127 | { | |
4128 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4129 | return -EINVAL; | |
3232dd02 PB |
4130 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4131 | return -EINVAL; | |
4840e418 JA |
4132 | |
4133 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4134 | req->fadvise.len = READ_ONCE(sqe->len); | |
4135 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4136 | return 0; | |
4137 | } | |
4138 | ||
45d189c6 | 4139 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4140 | { |
4141 | struct io_fadvise *fa = &req->fadvise; | |
4142 | int ret; | |
4143 | ||
45d189c6 | 4144 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4145 | switch (fa->advice) { |
4146 | case POSIX_FADV_NORMAL: | |
4147 | case POSIX_FADV_RANDOM: | |
4148 | case POSIX_FADV_SEQUENTIAL: | |
4149 | break; | |
4150 | default: | |
4151 | return -EAGAIN; | |
4152 | } | |
4153 | } | |
4840e418 JA |
4154 | |
4155 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4156 | if (ret < 0) | |
4157 | req_set_fail_links(req); | |
e1e16097 | 4158 | io_req_complete(req, ret); |
4840e418 JA |
4159 | return 0; |
4160 | } | |
4161 | ||
eddc7ef5 JA |
4162 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4163 | { | |
6ca56f84 | 4164 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4165 | return -EINVAL; |
eddc7ef5 JA |
4166 | if (sqe->ioprio || sqe->buf_index) |
4167 | return -EINVAL; | |
9c280f90 | 4168 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4169 | return -EBADF; |
eddc7ef5 | 4170 | |
1d9e1288 BM |
4171 | req->statx.dfd = READ_ONCE(sqe->fd); |
4172 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4173 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4174 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4175 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4176 | |
4177 | return 0; | |
4178 | } | |
4179 | ||
45d189c6 | 4180 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4181 | { |
1d9e1288 | 4182 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4183 | int ret; |
4184 | ||
45d189c6 | 4185 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4186 | /* only need file table for an actual valid fd */ |
4187 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4188 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4189 | return -EAGAIN; |
5b0bbee4 | 4190 | } |
eddc7ef5 | 4191 | |
e62753e4 BM |
4192 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4193 | ctx->buffer); | |
eddc7ef5 | 4194 | |
eddc7ef5 JA |
4195 | if (ret < 0) |
4196 | req_set_fail_links(req); | |
e1e16097 | 4197 | io_req_complete(req, ret); |
eddc7ef5 JA |
4198 | return 0; |
4199 | } | |
4200 | ||
b5dba59e JA |
4201 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4202 | { | |
14587a46 | 4203 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4204 | return -EINVAL; |
b5dba59e JA |
4205 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4206 | sqe->rw_flags || sqe->buf_index) | |
4207 | return -EINVAL; | |
9c280f90 | 4208 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4209 | return -EBADF; |
b5dba59e JA |
4210 | |
4211 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4212 | return 0; |
b5dba59e JA |
4213 | } |
4214 | ||
889fca73 | 4215 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4216 | { |
9eac1904 | 4217 | struct files_struct *files = current->files; |
3af73b28 | 4218 | struct io_close *close = &req->close; |
9eac1904 JA |
4219 | struct fdtable *fdt; |
4220 | struct file *file; | |
b5dba59e JA |
4221 | int ret; |
4222 | ||
9eac1904 JA |
4223 | file = NULL; |
4224 | ret = -EBADF; | |
4225 | spin_lock(&files->file_lock); | |
4226 | fdt = files_fdtable(files); | |
4227 | if (close->fd >= fdt->max_fds) { | |
4228 | spin_unlock(&files->file_lock); | |
4229 | goto err; | |
4230 | } | |
4231 | file = fdt->fd[close->fd]; | |
4232 | if (!file) { | |
4233 | spin_unlock(&files->file_lock); | |
4234 | goto err; | |
4235 | } | |
4236 | ||
4237 | if (file->f_op == &io_uring_fops) { | |
4238 | spin_unlock(&files->file_lock); | |
4239 | file = NULL; | |
4240 | goto err; | |
3af73b28 | 4241 | } |
b5dba59e JA |
4242 | |
4243 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4244 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4245 | spin_unlock(&files->file_lock); |
0bf0eefd | 4246 | return -EAGAIN; |
a2100672 | 4247 | } |
b5dba59e | 4248 | |
9eac1904 JA |
4249 | ret = __close_fd_get_file(close->fd, &file); |
4250 | spin_unlock(&files->file_lock); | |
4251 | if (ret < 0) { | |
4252 | if (ret == -ENOENT) | |
4253 | ret = -EBADF; | |
4254 | goto err; | |
4255 | } | |
4256 | ||
3af73b28 | 4257 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4258 | ret = filp_close(file, current->files); |
4259 | err: | |
3af73b28 PB |
4260 | if (ret < 0) |
4261 | req_set_fail_links(req); | |
9eac1904 JA |
4262 | if (file) |
4263 | fput(file); | |
889fca73 | 4264 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4265 | return 0; |
b5dba59e JA |
4266 | } |
4267 | ||
1155c76a | 4268 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4269 | { |
4270 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4271 | |
5d17b4a4 JA |
4272 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4273 | return -EINVAL; | |
4274 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4275 | return -EINVAL; | |
4276 | ||
8ed8d3c3 JA |
4277 | req->sync.off = READ_ONCE(sqe->off); |
4278 | req->sync.len = READ_ONCE(sqe->len); | |
4279 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4280 | return 0; |
4281 | } | |
4282 | ||
45d189c6 | 4283 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4284 | { |
8ed8d3c3 JA |
4285 | int ret; |
4286 | ||
ac45abc0 | 4287 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4288 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4289 | return -EAGAIN; |
4290 | ||
9adbd45d | 4291 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4292 | req->sync.flags); |
4293 | if (ret < 0) | |
4294 | req_set_fail_links(req); | |
e1e16097 | 4295 | io_req_complete(req, ret); |
5d17b4a4 JA |
4296 | return 0; |
4297 | } | |
4298 | ||
469956e8 | 4299 | #if defined(CONFIG_NET) |
02d27d89 PB |
4300 | static int io_setup_async_msg(struct io_kiocb *req, |
4301 | struct io_async_msghdr *kmsg) | |
4302 | { | |
e8c2bc1f JA |
4303 | struct io_async_msghdr *async_msg = req->async_data; |
4304 | ||
4305 | if (async_msg) | |
02d27d89 | 4306 | return -EAGAIN; |
e8c2bc1f | 4307 | if (io_alloc_async_data(req)) { |
257e84a5 | 4308 | kfree(kmsg->free_iov); |
02d27d89 PB |
4309 | return -ENOMEM; |
4310 | } | |
e8c2bc1f | 4311 | async_msg = req->async_data; |
02d27d89 | 4312 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4313 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4314 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4315 | /* if were using fast_iov, set it to the new one */ |
4316 | if (!async_msg->free_iov) | |
4317 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4318 | ||
02d27d89 PB |
4319 | return -EAGAIN; |
4320 | } | |
4321 | ||
2ae523ed PB |
4322 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4323 | struct io_async_msghdr *iomsg) | |
4324 | { | |
2ae523ed | 4325 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4326 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4327 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4328 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4329 | } |
4330 | ||
93642ef8 PB |
4331 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4332 | { | |
4333 | int ret; | |
4334 | ||
4335 | if (!io_op_defs[req->opcode].needs_async_data) | |
4336 | return 0; | |
4337 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4338 | if (!ret) | |
4339 | req->flags |= REQ_F_NEED_CLEANUP; | |
4340 | return ret; | |
4341 | } | |
4342 | ||
3529d8c2 | 4343 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4344 | { |
e47293fd | 4345 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4346 | |
d2b6f48b PB |
4347 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4348 | return -EINVAL; | |
4349 | ||
e47293fd | 4350 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4351 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4352 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4353 | |
d8768362 JA |
4354 | #ifdef CONFIG_COMPAT |
4355 | if (req->ctx->compat) | |
4356 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4357 | #endif | |
93642ef8 | 4358 | return 0; |
03b1230c JA |
4359 | } |
4360 | ||
889fca73 | 4361 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4362 | { |
6b754c8b | 4363 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4364 | struct socket *sock; |
7a7cacba | 4365 | unsigned flags; |
0fa03c62 JA |
4366 | int ret; |
4367 | ||
dba4a925 | 4368 | sock = sock_from_file(req->file); |
7a7cacba | 4369 | if (unlikely(!sock)) |
dba4a925 | 4370 | return -ENOTSOCK; |
3529d8c2 | 4371 | |
257e84a5 PB |
4372 | kmsg = req->async_data; |
4373 | if (!kmsg) { | |
7a7cacba PB |
4374 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4375 | if (ret) | |
4376 | return ret; | |
4377 | kmsg = &iomsg; | |
0fa03c62 | 4378 | } |
0fa03c62 | 4379 | |
7a7cacba PB |
4380 | flags = req->sr_msg.msg_flags; |
4381 | if (flags & MSG_DONTWAIT) | |
4382 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4383 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4384 | flags |= MSG_DONTWAIT; |
e47293fd | 4385 | |
7a7cacba | 4386 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4387 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4388 | return io_setup_async_msg(req, kmsg); |
4389 | if (ret == -ERESTARTSYS) | |
4390 | ret = -EINTR; | |
0fa03c62 | 4391 | |
257e84a5 PB |
4392 | /* fast path, check for non-NULL to avoid function call */ |
4393 | if (kmsg->free_iov) | |
4394 | kfree(kmsg->free_iov); | |
99bc4c38 | 4395 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4396 | if (ret < 0) |
4397 | req_set_fail_links(req); | |
889fca73 | 4398 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4399 | return 0; |
03b1230c | 4400 | } |
aa1fa28f | 4401 | |
889fca73 | 4402 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4403 | { |
7a7cacba PB |
4404 | struct io_sr_msg *sr = &req->sr_msg; |
4405 | struct msghdr msg; | |
4406 | struct iovec iov; | |
fddaface | 4407 | struct socket *sock; |
7a7cacba | 4408 | unsigned flags; |
fddaface JA |
4409 | int ret; |
4410 | ||
dba4a925 | 4411 | sock = sock_from_file(req->file); |
7a7cacba | 4412 | if (unlikely(!sock)) |
dba4a925 | 4413 | return -ENOTSOCK; |
fddaface | 4414 | |
7a7cacba PB |
4415 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4416 | if (unlikely(ret)) | |
14db8411 | 4417 | return ret; |
fddaface | 4418 | |
7a7cacba PB |
4419 | msg.msg_name = NULL; |
4420 | msg.msg_control = NULL; | |
4421 | msg.msg_controllen = 0; | |
4422 | msg.msg_namelen = 0; | |
fddaface | 4423 | |
7a7cacba PB |
4424 | flags = req->sr_msg.msg_flags; |
4425 | if (flags & MSG_DONTWAIT) | |
4426 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4427 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4428 | flags |= MSG_DONTWAIT; |
fddaface | 4429 | |
7a7cacba PB |
4430 | msg.msg_flags = flags; |
4431 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4432 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4433 | return -EAGAIN; |
4434 | if (ret == -ERESTARTSYS) | |
4435 | ret = -EINTR; | |
fddaface | 4436 | |
fddaface JA |
4437 | if (ret < 0) |
4438 | req_set_fail_links(req); | |
889fca73 | 4439 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4440 | return 0; |
fddaface JA |
4441 | } |
4442 | ||
1400e697 PB |
4443 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4444 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4445 | { |
4446 | struct io_sr_msg *sr = &req->sr_msg; | |
4447 | struct iovec __user *uiov; | |
4448 | size_t iov_len; | |
4449 | int ret; | |
4450 | ||
1400e697 PB |
4451 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4452 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4453 | if (ret) |
4454 | return ret; | |
4455 | ||
4456 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4457 | if (iov_len > 1) | |
4458 | return -EINVAL; | |
5476dfed | 4459 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4460 | return -EFAULT; |
5476dfed | 4461 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4462 | iomsg->free_iov = NULL; |
52de1fe1 | 4463 | } else { |
257e84a5 | 4464 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4465 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4466 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4467 | false); |
52de1fe1 JA |
4468 | if (ret > 0) |
4469 | ret = 0; | |
4470 | } | |
4471 | ||
4472 | return ret; | |
4473 | } | |
4474 | ||
4475 | #ifdef CONFIG_COMPAT | |
4476 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4477 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4478 | { |
4479 | struct compat_msghdr __user *msg_compat; | |
4480 | struct io_sr_msg *sr = &req->sr_msg; | |
4481 | struct compat_iovec __user *uiov; | |
4482 | compat_uptr_t ptr; | |
4483 | compat_size_t len; | |
4484 | int ret; | |
4485 | ||
270a5940 | 4486 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4487 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4488 | &ptr, &len); |
4489 | if (ret) | |
4490 | return ret; | |
4491 | ||
4492 | uiov = compat_ptr(ptr); | |
4493 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4494 | compat_ssize_t clen; | |
4495 | ||
4496 | if (len > 1) | |
4497 | return -EINVAL; | |
4498 | if (!access_ok(uiov, sizeof(*uiov))) | |
4499 | return -EFAULT; | |
4500 | if (__get_user(clen, &uiov->iov_len)) | |
4501 | return -EFAULT; | |
4502 | if (clen < 0) | |
4503 | return -EINVAL; | |
2d280bc8 | 4504 | sr->len = clen; |
257e84a5 | 4505 | iomsg->free_iov = NULL; |
52de1fe1 | 4506 | } else { |
257e84a5 | 4507 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4508 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4509 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4510 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4511 | if (ret < 0) |
4512 | return ret; | |
4513 | } | |
4514 | ||
4515 | return 0; | |
4516 | } | |
4517 | #endif | |
4518 | ||
1400e697 PB |
4519 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4520 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4521 | { |
1400e697 | 4522 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4523 | |
4524 | #ifdef CONFIG_COMPAT | |
4525 | if (req->ctx->compat) | |
1400e697 | 4526 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4527 | #endif |
52de1fe1 | 4528 | |
1400e697 | 4529 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4530 | } |
4531 | ||
bcda7baa | 4532 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4533 | bool needs_lock) |
bcda7baa JA |
4534 | { |
4535 | struct io_sr_msg *sr = &req->sr_msg; | |
4536 | struct io_buffer *kbuf; | |
4537 | ||
bcda7baa JA |
4538 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4539 | if (IS_ERR(kbuf)) | |
4540 | return kbuf; | |
4541 | ||
4542 | sr->kbuf = kbuf; | |
4543 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4544 | return kbuf; |
fddaface JA |
4545 | } |
4546 | ||
7fbb1b54 PB |
4547 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4548 | { | |
4549 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4550 | } | |
4551 | ||
93642ef8 | 4552 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4553 | { |
99bc4c38 | 4554 | int ret; |
3529d8c2 | 4555 | |
93642ef8 PB |
4556 | if (!io_op_defs[req->opcode].needs_async_data) |
4557 | return 0; | |
4558 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4559 | if (!ret) | |
4560 | req->flags |= REQ_F_NEED_CLEANUP; | |
4561 | return ret; | |
4562 | } | |
4563 | ||
4564 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4565 | { | |
4566 | struct io_sr_msg *sr = &req->sr_msg; | |
4567 | ||
d2b6f48b PB |
4568 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4569 | return -EINVAL; | |
4570 | ||
3529d8c2 | 4571 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4572 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4573 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4574 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4575 | |
d8768362 JA |
4576 | #ifdef CONFIG_COMPAT |
4577 | if (req->ctx->compat) | |
4578 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4579 | #endif | |
93642ef8 | 4580 | return 0; |
aa1fa28f JA |
4581 | } |
4582 | ||
889fca73 | 4583 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4584 | { |
6b754c8b | 4585 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4586 | struct socket *sock; |
7fbb1b54 | 4587 | struct io_buffer *kbuf; |
7a7cacba | 4588 | unsigned flags; |
52de1fe1 | 4589 | int ret, cflags = 0; |
45d189c6 | 4590 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4591 | |
dba4a925 | 4592 | sock = sock_from_file(req->file); |
7a7cacba | 4593 | if (unlikely(!sock)) |
dba4a925 | 4594 | return -ENOTSOCK; |
3529d8c2 | 4595 | |
257e84a5 PB |
4596 | kmsg = req->async_data; |
4597 | if (!kmsg) { | |
7a7cacba PB |
4598 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4599 | if (ret) | |
681fda8d | 4600 | return ret; |
7a7cacba PB |
4601 | kmsg = &iomsg; |
4602 | } | |
03b1230c | 4603 | |
bc02ef33 | 4604 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4605 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4606 | if (IS_ERR(kbuf)) |
52de1fe1 | 4607 | return PTR_ERR(kbuf); |
7a7cacba | 4608 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4609 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4610 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4611 | 1, req->sr_msg.len); |
4612 | } | |
52de1fe1 | 4613 | |
7a7cacba PB |
4614 | flags = req->sr_msg.msg_flags; |
4615 | if (flags & MSG_DONTWAIT) | |
4616 | req->flags |= REQ_F_NOWAIT; | |
4617 | else if (force_nonblock) | |
4618 | flags |= MSG_DONTWAIT; | |
e47293fd | 4619 | |
7a7cacba PB |
4620 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4621 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4622 | if (force_nonblock && ret == -EAGAIN) |
4623 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4624 | if (ret == -ERESTARTSYS) |
4625 | ret = -EINTR; | |
03b1230c | 4626 | |
7fbb1b54 PB |
4627 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4628 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4629 | /* fast path, check for non-NULL to avoid function call */ |
4630 | if (kmsg->free_iov) | |
4631 | kfree(kmsg->free_iov); | |
99bc4c38 | 4632 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4633 | if (ret < 0) |
4634 | req_set_fail_links(req); | |
889fca73 | 4635 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4636 | return 0; |
0fa03c62 | 4637 | } |
5d17b4a4 | 4638 | |
889fca73 | 4639 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4640 | { |
6b754c8b | 4641 | struct io_buffer *kbuf; |
7a7cacba PB |
4642 | struct io_sr_msg *sr = &req->sr_msg; |
4643 | struct msghdr msg; | |
4644 | void __user *buf = sr->buf; | |
fddaface | 4645 | struct socket *sock; |
7a7cacba PB |
4646 | struct iovec iov; |
4647 | unsigned flags; | |
bcda7baa | 4648 | int ret, cflags = 0; |
45d189c6 | 4649 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4650 | |
dba4a925 | 4651 | sock = sock_from_file(req->file); |
7a7cacba | 4652 | if (unlikely(!sock)) |
dba4a925 | 4653 | return -ENOTSOCK; |
fddaface | 4654 | |
bc02ef33 | 4655 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4656 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4657 | if (IS_ERR(kbuf)) |
4658 | return PTR_ERR(kbuf); | |
7a7cacba | 4659 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4660 | } |
bcda7baa | 4661 | |
7a7cacba | 4662 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4663 | if (unlikely(ret)) |
4664 | goto out_free; | |
fddaface | 4665 | |
7a7cacba PB |
4666 | msg.msg_name = NULL; |
4667 | msg.msg_control = NULL; | |
4668 | msg.msg_controllen = 0; | |
4669 | msg.msg_namelen = 0; | |
4670 | msg.msg_iocb = NULL; | |
4671 | msg.msg_flags = 0; | |
fddaface | 4672 | |
7a7cacba PB |
4673 | flags = req->sr_msg.msg_flags; |
4674 | if (flags & MSG_DONTWAIT) | |
4675 | req->flags |= REQ_F_NOWAIT; | |
4676 | else if (force_nonblock) | |
4677 | flags |= MSG_DONTWAIT; | |
4678 | ||
4679 | ret = sock_recvmsg(sock, &msg, flags); | |
4680 | if (force_nonblock && ret == -EAGAIN) | |
4681 | return -EAGAIN; | |
4682 | if (ret == -ERESTARTSYS) | |
4683 | ret = -EINTR; | |
14c32eee | 4684 | out_free: |
7fbb1b54 PB |
4685 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4686 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4687 | if (ret < 0) |
4688 | req_set_fail_links(req); | |
889fca73 | 4689 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4690 | return 0; |
fddaface JA |
4691 | } |
4692 | ||
3529d8c2 | 4693 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4694 | { |
8ed8d3c3 JA |
4695 | struct io_accept *accept = &req->accept; |
4696 | ||
14587a46 | 4697 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4698 | return -EINVAL; |
8042d6ce | 4699 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4700 | return -EINVAL; |
4701 | ||
d55e5f5b JA |
4702 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4703 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4704 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4705 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4706 | return 0; |
8ed8d3c3 | 4707 | } |
17f2fe35 | 4708 | |
889fca73 | 4709 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4710 | { |
4711 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4712 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4713 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4714 | int ret; |
4715 | ||
e697deed JX |
4716 | if (req->file->f_flags & O_NONBLOCK) |
4717 | req->flags |= REQ_F_NOWAIT; | |
4718 | ||
8ed8d3c3 | 4719 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4720 | accept->addr_len, accept->flags, |
4721 | accept->nofile); | |
8ed8d3c3 | 4722 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4723 | return -EAGAIN; |
ac45abc0 PB |
4724 | if (ret < 0) { |
4725 | if (ret == -ERESTARTSYS) | |
4726 | ret = -EINTR; | |
4e88d6e7 | 4727 | req_set_fail_links(req); |
ac45abc0 | 4728 | } |
889fca73 | 4729 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4730 | return 0; |
8ed8d3c3 JA |
4731 | } |
4732 | ||
93642ef8 PB |
4733 | static int io_connect_prep_async(struct io_kiocb *req) |
4734 | { | |
4735 | struct io_async_connect *io = req->async_data; | |
4736 | struct io_connect *conn = &req->connect; | |
4737 | ||
4738 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4739 | } | |
4740 | ||
3529d8c2 | 4741 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4742 | { |
3529d8c2 | 4743 | struct io_connect *conn = &req->connect; |
f499a021 | 4744 | |
14587a46 | 4745 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4746 | return -EINVAL; |
4747 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4748 | return -EINVAL; | |
4749 | ||
3529d8c2 JA |
4750 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4751 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4752 | return 0; |
f499a021 JA |
4753 | } |
4754 | ||
889fca73 | 4755 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4756 | { |
e8c2bc1f | 4757 | struct io_async_connect __io, *io; |
f8e85cf2 | 4758 | unsigned file_flags; |
3fbb51c1 | 4759 | int ret; |
45d189c6 | 4760 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4761 | |
e8c2bc1f JA |
4762 | if (req->async_data) { |
4763 | io = req->async_data; | |
f499a021 | 4764 | } else { |
3529d8c2 JA |
4765 | ret = move_addr_to_kernel(req->connect.addr, |
4766 | req->connect.addr_len, | |
e8c2bc1f | 4767 | &__io.address); |
f499a021 JA |
4768 | if (ret) |
4769 | goto out; | |
4770 | io = &__io; | |
4771 | } | |
4772 | ||
3fbb51c1 JA |
4773 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4774 | ||
e8c2bc1f | 4775 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4776 | req->connect.addr_len, file_flags); |
87f80d62 | 4777 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4778 | if (req->async_data) |
b7bb4f7d | 4779 | return -EAGAIN; |
e8c2bc1f | 4780 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4781 | ret = -ENOMEM; |
4782 | goto out; | |
4783 | } | |
e8c2bc1f JA |
4784 | io = req->async_data; |
4785 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4786 | return -EAGAIN; |
f499a021 | 4787 | } |
f8e85cf2 JA |
4788 | if (ret == -ERESTARTSYS) |
4789 | ret = -EINTR; | |
f499a021 | 4790 | out: |
4e88d6e7 JA |
4791 | if (ret < 0) |
4792 | req_set_fail_links(req); | |
889fca73 | 4793 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4794 | return 0; |
469956e8 Y |
4795 | } |
4796 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4797 | #define IO_NETOP_FN(op) \ |
4798 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4799 | { \ | |
4800 | return -EOPNOTSUPP; \ | |
4801 | } | |
4802 | ||
4803 | #define IO_NETOP_PREP(op) \ | |
4804 | IO_NETOP_FN(op) \ | |
4805 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4806 | { \ | |
4807 | return -EOPNOTSUPP; \ | |
4808 | } \ | |
4809 | ||
4810 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4811 | IO_NETOP_PREP(op) \ | |
4812 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4813 | { \ | |
4814 | return -EOPNOTSUPP; \ | |
4815 | } | |
4816 | ||
4817 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4818 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4819 | IO_NETOP_PREP_ASYNC(connect); | |
4820 | IO_NETOP_PREP(accept); | |
4821 | IO_NETOP_FN(send); | |
4822 | IO_NETOP_FN(recv); | |
469956e8 | 4823 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4824 | |
d7718a9d JA |
4825 | struct io_poll_table { |
4826 | struct poll_table_struct pt; | |
4827 | struct io_kiocb *req; | |
4828 | int error; | |
4829 | }; | |
ce593a6c | 4830 | |
d7718a9d JA |
4831 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4832 | __poll_t mask, task_work_func_t func) | |
4833 | { | |
aa96bf8a | 4834 | int ret; |
d7718a9d JA |
4835 | |
4836 | /* for instances that support it check for an event match first: */ | |
4837 | if (mask && !(mask & poll->events)) | |
4838 | return 0; | |
4839 | ||
4840 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4841 | ||
4842 | list_del_init(&poll->wait.entry); | |
4843 | ||
d7718a9d | 4844 | req->result = mask; |
7cbf1722 | 4845 | req->task_work.func = func; |
6d816e08 JA |
4846 | percpu_ref_get(&req->ctx->refs); |
4847 | ||
d7718a9d | 4848 | /* |
e3aabf95 JA |
4849 | * If this fails, then the task is exiting. When a task exits, the |
4850 | * work gets canceled, so just cancel this request as well instead | |
4851 | * of executing it. We can't safely execute it anyway, as we may not | |
4852 | * have the needed state needed for it anyway. | |
d7718a9d | 4853 | */ |
355fb9e2 | 4854 | ret = io_req_task_work_add(req); |
aa96bf8a | 4855 | if (unlikely(ret)) { |
e3aabf95 | 4856 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4857 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4858 | } |
d7718a9d JA |
4859 | return 1; |
4860 | } | |
4861 | ||
74ce6ce4 JA |
4862 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4863 | __acquires(&req->ctx->completion_lock) | |
4864 | { | |
4865 | struct io_ring_ctx *ctx = req->ctx; | |
4866 | ||
4867 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4868 | struct poll_table_struct pt = { ._key = poll->events }; | |
4869 | ||
4870 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4871 | } | |
4872 | ||
4873 | spin_lock_irq(&ctx->completion_lock); | |
4874 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4875 | add_wait_queue(poll->head, &poll->wait); | |
4876 | return true; | |
4877 | } | |
4878 | ||
4879 | return false; | |
4880 | } | |
4881 | ||
d4e7cd36 | 4882 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4883 | { |
e8c2bc1f | 4884 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4885 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4886 | return req->async_data; |
d4e7cd36 JA |
4887 | return req->apoll->double_poll; |
4888 | } | |
4889 | ||
4890 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4891 | { | |
4892 | if (req->opcode == IORING_OP_POLL_ADD) | |
4893 | return &req->poll; | |
4894 | return &req->apoll->poll; | |
4895 | } | |
4896 | ||
4897 | static void io_poll_remove_double(struct io_kiocb *req) | |
4898 | { | |
4899 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4900 | |
4901 | lockdep_assert_held(&req->ctx->completion_lock); | |
4902 | ||
4903 | if (poll && poll->head) { | |
4904 | struct wait_queue_head *head = poll->head; | |
4905 | ||
4906 | spin_lock(&head->lock); | |
4907 | list_del_init(&poll->wait.entry); | |
4908 | if (poll->wait.private) | |
4909 | refcount_dec(&req->refs); | |
4910 | poll->head = NULL; | |
4911 | spin_unlock(&head->lock); | |
4912 | } | |
4913 | } | |
4914 | ||
4915 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4916 | { | |
4917 | struct io_ring_ctx *ctx = req->ctx; | |
4918 | ||
d4e7cd36 | 4919 | io_poll_remove_double(req); |
18bceab1 JA |
4920 | req->poll.done = true; |
4921 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4922 | io_commit_cqring(ctx); | |
4923 | } | |
4924 | ||
dd221f46 | 4925 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4926 | { |
dd221f46 | 4927 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4928 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4929 | struct io_kiocb *nxt; |
18bceab1 JA |
4930 | |
4931 | if (io_poll_rewait(req, &req->poll)) { | |
4932 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
4933 | } else { |
4934 | hash_del(&req->hash_node); | |
4935 | io_poll_complete(req, req->result, 0); | |
4936 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 4937 | |
dd221f46 PB |
4938 | nxt = io_put_req_find_next(req); |
4939 | io_cqring_ev_posted(ctx); | |
4940 | if (nxt) | |
4941 | __io_req_task_submit(nxt); | |
4942 | } | |
18bceab1 | 4943 | |
6d816e08 | 4944 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4945 | } |
4946 | ||
4947 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4948 | int sync, void *key) | |
4949 | { | |
4950 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4951 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4952 | __poll_t mask = key_to_poll(key); |
4953 | ||
4954 | /* for instances that support it check for an event match first: */ | |
4955 | if (mask && !(mask & poll->events)) | |
4956 | return 0; | |
4957 | ||
8706e04e JA |
4958 | list_del_init(&wait->entry); |
4959 | ||
807abcb0 | 4960 | if (poll && poll->head) { |
18bceab1 JA |
4961 | bool done; |
4962 | ||
807abcb0 JA |
4963 | spin_lock(&poll->head->lock); |
4964 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4965 | if (!done) |
807abcb0 | 4966 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4967 | /* make sure double remove sees this as being gone */ |
4968 | wait->private = NULL; | |
807abcb0 | 4969 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4970 | if (!done) { |
4971 | /* use wait func handler, so it matches the rq type */ | |
4972 | poll->wait.func(&poll->wait, mode, sync, key); | |
4973 | } | |
18bceab1 JA |
4974 | } |
4975 | refcount_dec(&req->refs); | |
4976 | return 1; | |
4977 | } | |
4978 | ||
4979 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4980 | wait_queue_func_t wake_func) | |
4981 | { | |
4982 | poll->head = NULL; | |
4983 | poll->done = false; | |
4984 | poll->canceled = false; | |
4985 | poll->events = events; | |
4986 | INIT_LIST_HEAD(&poll->wait.entry); | |
4987 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4988 | } | |
4989 | ||
4990 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4991 | struct wait_queue_head *head, |
4992 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4993 | { |
4994 | struct io_kiocb *req = pt->req; | |
4995 | ||
4996 | /* | |
4997 | * If poll->head is already set, it's because the file being polled | |
4998 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4999 | * for write). Setup a separate io_poll_iocb if this happens. | |
5000 | */ | |
5001 | if (unlikely(poll->head)) { | |
58852d4d PB |
5002 | struct io_poll_iocb *poll_one = poll; |
5003 | ||
18bceab1 | 5004 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5005 | if (*poll_ptr) { |
18bceab1 JA |
5006 | pt->error = -EINVAL; |
5007 | return; | |
5008 | } | |
5009 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); | |
5010 | if (!poll) { | |
5011 | pt->error = -ENOMEM; | |
5012 | return; | |
5013 | } | |
58852d4d | 5014 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
5015 | refcount_inc(&req->refs); |
5016 | poll->wait.private = req; | |
807abcb0 | 5017 | *poll_ptr = poll; |
18bceab1 JA |
5018 | } |
5019 | ||
5020 | pt->error = 0; | |
5021 | poll->head = head; | |
a31eb4a2 JX |
5022 | |
5023 | if (poll->events & EPOLLEXCLUSIVE) | |
5024 | add_wait_queue_exclusive(head, &poll->wait); | |
5025 | else | |
5026 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5027 | } |
5028 | ||
5029 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5030 | struct poll_table_struct *p) | |
5031 | { | |
5032 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5033 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5034 | |
807abcb0 | 5035 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5036 | } |
5037 | ||
d7718a9d JA |
5038 | static void io_async_task_func(struct callback_head *cb) |
5039 | { | |
5040 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5041 | struct async_poll *apoll = req->apoll; | |
5042 | struct io_ring_ctx *ctx = req->ctx; | |
5043 | ||
5044 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5045 | ||
74ce6ce4 | 5046 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5047 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5048 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5049 | return; |
d7718a9d JA |
5050 | } |
5051 | ||
31067255 | 5052 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5053 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5054 | hash_del(&req->hash_node); |
2bae047e | 5055 | |
d4e7cd36 | 5056 | io_poll_remove_double(req); |
74ce6ce4 JA |
5057 | spin_unlock_irq(&ctx->completion_lock); |
5058 | ||
0be0b0e3 PB |
5059 | if (!READ_ONCE(apoll->poll.canceled)) |
5060 | __io_req_task_submit(req); | |
5061 | else | |
5062 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5063 | |
6d816e08 | 5064 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5065 | kfree(apoll->double_poll); |
31067255 | 5066 | kfree(apoll); |
d7718a9d JA |
5067 | } |
5068 | ||
5069 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5070 | void *key) | |
5071 | { | |
5072 | struct io_kiocb *req = wait->private; | |
5073 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5074 | ||
5075 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5076 | key_to_poll(key)); | |
5077 | ||
5078 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5079 | } | |
5080 | ||
5081 | static void io_poll_req_insert(struct io_kiocb *req) | |
5082 | { | |
5083 | struct io_ring_ctx *ctx = req->ctx; | |
5084 | struct hlist_head *list; | |
5085 | ||
5086 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5087 | hlist_add_head(&req->hash_node, list); | |
5088 | } | |
5089 | ||
5090 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5091 | struct io_poll_iocb *poll, | |
5092 | struct io_poll_table *ipt, __poll_t mask, | |
5093 | wait_queue_func_t wake_func) | |
5094 | __acquires(&ctx->completion_lock) | |
5095 | { | |
5096 | struct io_ring_ctx *ctx = req->ctx; | |
5097 | bool cancel = false; | |
5098 | ||
4d52f338 | 5099 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5100 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5101 | poll->file = req->file; |
18bceab1 | 5102 | poll->wait.private = req; |
d7718a9d JA |
5103 | |
5104 | ipt->pt._key = mask; | |
5105 | ipt->req = req; | |
5106 | ipt->error = -EINVAL; | |
5107 | ||
d7718a9d JA |
5108 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5109 | ||
5110 | spin_lock_irq(&ctx->completion_lock); | |
5111 | if (likely(poll->head)) { | |
5112 | spin_lock(&poll->head->lock); | |
5113 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5114 | if (ipt->error) | |
5115 | cancel = true; | |
5116 | ipt->error = 0; | |
5117 | mask = 0; | |
5118 | } | |
5119 | if (mask || ipt->error) | |
5120 | list_del_init(&poll->wait.entry); | |
5121 | else if (cancel) | |
5122 | WRITE_ONCE(poll->canceled, true); | |
5123 | else if (!poll->done) /* actually waiting for an event */ | |
5124 | io_poll_req_insert(req); | |
5125 | spin_unlock(&poll->head->lock); | |
5126 | } | |
5127 | ||
5128 | return mask; | |
5129 | } | |
5130 | ||
5131 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5132 | { | |
5133 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5134 | struct io_ring_ctx *ctx = req->ctx; | |
5135 | struct async_poll *apoll; | |
5136 | struct io_poll_table ipt; | |
5137 | __poll_t mask, ret; | |
9dab14b8 | 5138 | int rw; |
d7718a9d JA |
5139 | |
5140 | if (!req->file || !file_can_poll(req->file)) | |
5141 | return false; | |
24c74678 | 5142 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5143 | return false; |
9dab14b8 JA |
5144 | if (def->pollin) |
5145 | rw = READ; | |
5146 | else if (def->pollout) | |
5147 | rw = WRITE; | |
5148 | else | |
5149 | return false; | |
5150 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5151 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5152 | return false; |
5153 | ||
5154 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5155 | if (unlikely(!apoll)) | |
5156 | return false; | |
807abcb0 | 5157 | apoll->double_poll = NULL; |
d7718a9d JA |
5158 | |
5159 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5160 | req->apoll = apoll; |
d7718a9d | 5161 | |
8755d97a | 5162 | mask = 0; |
d7718a9d | 5163 | if (def->pollin) |
8755d97a | 5164 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5165 | if (def->pollout) |
5166 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5167 | |
5168 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5169 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5170 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5171 | mask &= ~POLLIN; | |
5172 | ||
d7718a9d JA |
5173 | mask |= POLLERR | POLLPRI; |
5174 | ||
5175 | ipt.pt._qproc = io_async_queue_proc; | |
5176 | ||
5177 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5178 | io_async_wake); | |
a36da65c | 5179 | if (ret || ipt.error) { |
d4e7cd36 | 5180 | io_poll_remove_double(req); |
d7718a9d | 5181 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5182 | kfree(apoll->double_poll); |
d7718a9d JA |
5183 | kfree(apoll); |
5184 | return false; | |
5185 | } | |
5186 | spin_unlock_irq(&ctx->completion_lock); | |
5187 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5188 | apoll->poll.events); | |
5189 | return true; | |
5190 | } | |
5191 | ||
5192 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5193 | struct io_poll_iocb *poll) | |
221c5eb2 | 5194 | { |
b41e9852 | 5195 | bool do_complete = false; |
221c5eb2 JA |
5196 | |
5197 | spin_lock(&poll->head->lock); | |
5198 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5199 | if (!list_empty(&poll->wait.entry)) { |
5200 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5201 | do_complete = true; |
221c5eb2 JA |
5202 | } |
5203 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5204 | hash_del(&req->hash_node); |
d7718a9d JA |
5205 | return do_complete; |
5206 | } | |
5207 | ||
5208 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5209 | { | |
5210 | bool do_complete; | |
5211 | ||
d4e7cd36 JA |
5212 | io_poll_remove_double(req); |
5213 | ||
d7718a9d JA |
5214 | if (req->opcode == IORING_OP_POLL_ADD) { |
5215 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5216 | } else { | |
3bfa5bcb JA |
5217 | struct async_poll *apoll = req->apoll; |
5218 | ||
d7718a9d | 5219 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5220 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5221 | if (do_complete) { | |
d7718a9d | 5222 | io_put_req(req); |
807abcb0 | 5223 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5224 | kfree(apoll); |
5225 | } | |
b1f573bd XW |
5226 | } |
5227 | ||
b41e9852 JA |
5228 | if (do_complete) { |
5229 | io_cqring_fill_event(req, -ECANCELED); | |
5230 | io_commit_cqring(req->ctx); | |
f254ac04 | 5231 | req_set_fail_links(req); |
216578e5 | 5232 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5233 | } |
5234 | ||
5235 | return do_complete; | |
221c5eb2 JA |
5236 | } |
5237 | ||
76e1b642 JA |
5238 | /* |
5239 | * Returns true if we found and killed one or more poll requests | |
5240 | */ | |
6b81928d PB |
5241 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5242 | struct files_struct *files) | |
221c5eb2 | 5243 | { |
78076bb6 | 5244 | struct hlist_node *tmp; |
221c5eb2 | 5245 | struct io_kiocb *req; |
8e2e1faf | 5246 | int posted = 0, i; |
221c5eb2 JA |
5247 | |
5248 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5249 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5250 | struct hlist_head *list; | |
5251 | ||
5252 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5253 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5254 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5255 | posted += io_poll_remove_one(req); |
5256 | } | |
221c5eb2 JA |
5257 | } |
5258 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5259 | |
8e2e1faf JA |
5260 | if (posted) |
5261 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5262 | |
5263 | return posted != 0; | |
221c5eb2 JA |
5264 | } |
5265 | ||
47f46768 JA |
5266 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5267 | { | |
78076bb6 | 5268 | struct hlist_head *list; |
47f46768 JA |
5269 | struct io_kiocb *req; |
5270 | ||
78076bb6 JA |
5271 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5272 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5273 | if (sqe_addr != req->user_data) |
5274 | continue; | |
5275 | if (io_poll_remove_one(req)) | |
eac406c6 | 5276 | return 0; |
b41e9852 | 5277 | return -EALREADY; |
47f46768 JA |
5278 | } |
5279 | ||
5280 | return -ENOENT; | |
5281 | } | |
5282 | ||
3529d8c2 JA |
5283 | static int io_poll_remove_prep(struct io_kiocb *req, |
5284 | const struct io_uring_sqe *sqe) | |
0969e783 | 5285 | { |
0969e783 JA |
5286 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5287 | return -EINVAL; | |
5288 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5289 | sqe->poll_events) | |
5290 | return -EINVAL; | |
5291 | ||
018043be | 5292 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5293 | return 0; |
5294 | } | |
5295 | ||
221c5eb2 JA |
5296 | /* |
5297 | * Find a running poll command that matches one specified in sqe->addr, | |
5298 | * and remove it if found. | |
5299 | */ | |
61e98203 | 5300 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5301 | { |
5302 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5303 | int ret; |
221c5eb2 | 5304 | |
221c5eb2 | 5305 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5306 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5307 | spin_unlock_irq(&ctx->completion_lock); |
5308 | ||
4e88d6e7 JA |
5309 | if (ret < 0) |
5310 | req_set_fail_links(req); | |
e1e16097 | 5311 | io_req_complete(req, ret); |
221c5eb2 JA |
5312 | return 0; |
5313 | } | |
5314 | ||
221c5eb2 JA |
5315 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5316 | void *key) | |
5317 | { | |
c2f2eb7d JA |
5318 | struct io_kiocb *req = wait->private; |
5319 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5320 | |
d7718a9d | 5321 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5322 | } |
5323 | ||
221c5eb2 JA |
5324 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5325 | struct poll_table_struct *p) | |
5326 | { | |
5327 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5328 | ||
e8c2bc1f | 5329 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5330 | } |
5331 | ||
3529d8c2 | 5332 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5333 | { |
5334 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5335 | u32 events; |
221c5eb2 JA |
5336 | |
5337 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5338 | return -EINVAL; | |
5339 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5340 | return -EINVAL; | |
5341 | ||
5769a351 JX |
5342 | events = READ_ONCE(sqe->poll32_events); |
5343 | #ifdef __BIG_ENDIAN | |
5344 | events = swahw32(events); | |
5345 | #endif | |
a31eb4a2 JX |
5346 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5347 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5348 | return 0; |
5349 | } | |
5350 | ||
61e98203 | 5351 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5352 | { |
5353 | struct io_poll_iocb *poll = &req->poll; | |
5354 | struct io_ring_ctx *ctx = req->ctx; | |
5355 | struct io_poll_table ipt; | |
0969e783 | 5356 | __poll_t mask; |
0969e783 | 5357 | |
d7718a9d | 5358 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5359 | |
d7718a9d JA |
5360 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5361 | io_poll_wake); | |
221c5eb2 | 5362 | |
8c838788 | 5363 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5364 | ipt.error = 0; |
b0dd8a41 | 5365 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5366 | } |
221c5eb2 JA |
5367 | spin_unlock_irq(&ctx->completion_lock); |
5368 | ||
8c838788 JA |
5369 | if (mask) { |
5370 | io_cqring_ev_posted(ctx); | |
014db007 | 5371 | io_put_req(req); |
221c5eb2 | 5372 | } |
8c838788 | 5373 | return ipt.error; |
221c5eb2 JA |
5374 | } |
5375 | ||
5262f567 JA |
5376 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5377 | { | |
ad8a48ac JA |
5378 | struct io_timeout_data *data = container_of(timer, |
5379 | struct io_timeout_data, timer); | |
5380 | struct io_kiocb *req = data->req; | |
5381 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5382 | unsigned long flags; |
5383 | ||
5262f567 | 5384 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5385 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5386 | atomic_set(&req->ctx->cq_timeouts, |
5387 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5388 | ||
78e19bbe | 5389 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5390 | io_commit_cqring(ctx); |
5391 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5392 | ||
5393 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5394 | req_set_fail_links(req); |
5262f567 JA |
5395 | io_put_req(req); |
5396 | return HRTIMER_NORESTART; | |
5397 | } | |
5398 | ||
fbd15848 PB |
5399 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5400 | __u64 user_data) | |
f254ac04 | 5401 | { |
fbd15848 | 5402 | struct io_timeout_data *io; |
47f46768 JA |
5403 | struct io_kiocb *req; |
5404 | int ret = -ENOENT; | |
f254ac04 | 5405 | |
135fcde8 | 5406 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5407 | if (user_data == req->user_data) { |
47f46768 JA |
5408 | ret = 0; |
5409 | break; | |
5410 | } | |
5411 | } | |
5412 | ||
5413 | if (ret == -ENOENT) | |
fbd15848 PB |
5414 | return ERR_PTR(ret); |
5415 | ||
5416 | io = req->async_data; | |
e8c2bc1f | 5417 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5418 | if (ret == -1) |
fbd15848 | 5419 | return ERR_PTR(-EALREADY); |
a71976f3 | 5420 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5421 | return req; |
5422 | } | |
47f46768 | 5423 | |
fbd15848 PB |
5424 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5425 | { | |
5426 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5427 | ||
5428 | if (IS_ERR(req)) | |
5429 | return PTR_ERR(req); | |
f254ac04 JA |
5430 | |
5431 | req_set_fail_links(req); | |
f254ac04 | 5432 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5433 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5434 | return 0; |
5435 | } | |
5436 | ||
9c8e11b3 PB |
5437 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5438 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5439 | { |
9c8e11b3 PB |
5440 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5441 | struct io_timeout_data *data; | |
47f46768 | 5442 | |
9c8e11b3 PB |
5443 | if (IS_ERR(req)) |
5444 | return PTR_ERR(req); | |
47f46768 | 5445 | |
9c8e11b3 PB |
5446 | req->timeout.off = 0; /* noseq */ |
5447 | data = req->async_data; | |
5448 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5449 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5450 | data->timer.function = io_timeout_fn; | |
5451 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5452 | return 0; | |
47f46768 JA |
5453 | } |
5454 | ||
3529d8c2 JA |
5455 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5456 | const struct io_uring_sqe *sqe) | |
b29472ee | 5457 | { |
9c8e11b3 PB |
5458 | struct io_timeout_rem *tr = &req->timeout_rem; |
5459 | ||
b29472ee JA |
5460 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5461 | return -EINVAL; | |
61710e43 DA |
5462 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5463 | return -EINVAL; | |
9c8e11b3 | 5464 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5465 | return -EINVAL; |
5466 | ||
9c8e11b3 PB |
5467 | tr->addr = READ_ONCE(sqe->addr); |
5468 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5469 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5470 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5471 | return -EINVAL; | |
5472 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5473 | return -EFAULT; | |
5474 | } else if (tr->flags) { | |
5475 | /* timeout removal doesn't support flags */ | |
b29472ee | 5476 | return -EINVAL; |
9c8e11b3 | 5477 | } |
b29472ee | 5478 | |
b29472ee JA |
5479 | return 0; |
5480 | } | |
5481 | ||
8662daec PB |
5482 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5483 | { | |
5484 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5485 | : HRTIMER_MODE_REL; | |
5486 | } | |
5487 | ||
11365043 JA |
5488 | /* |
5489 | * Remove or update an existing timeout command | |
5490 | */ | |
61e98203 | 5491 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5492 | { |
9c8e11b3 | 5493 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5494 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5495 | int ret; |
11365043 | 5496 | |
11365043 | 5497 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5498 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5499 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5500 | else |
5501 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5502 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5503 | |
47f46768 | 5504 | io_cqring_fill_event(req, ret); |
11365043 JA |
5505 | io_commit_cqring(ctx); |
5506 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5507 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5508 | if (ret < 0) |
5509 | req_set_fail_links(req); | |
ec9c02ad | 5510 | io_put_req(req); |
11365043 | 5511 | return 0; |
5262f567 JA |
5512 | } |
5513 | ||
3529d8c2 | 5514 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5515 | bool is_timeout_link) |
5262f567 | 5516 | { |
ad8a48ac | 5517 | struct io_timeout_data *data; |
a41525ab | 5518 | unsigned flags; |
56080b02 | 5519 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5520 | |
ad8a48ac | 5521 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5522 | return -EINVAL; |
ad8a48ac | 5523 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5524 | return -EINVAL; |
56080b02 | 5525 | if (off && is_timeout_link) |
2d28390a | 5526 | return -EINVAL; |
a41525ab JA |
5527 | flags = READ_ONCE(sqe->timeout_flags); |
5528 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5529 | return -EINVAL; |
bdf20073 | 5530 | |
bfe68a22 | 5531 | req->timeout.off = off; |
26a61679 | 5532 | |
e8c2bc1f | 5533 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5534 | return -ENOMEM; |
5535 | ||
e8c2bc1f | 5536 | data = req->async_data; |
ad8a48ac | 5537 | data->req = req; |
ad8a48ac JA |
5538 | |
5539 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5540 | return -EFAULT; |
5541 | ||
8662daec | 5542 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5543 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5544 | return 0; | |
5545 | } | |
5546 | ||
61e98203 | 5547 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5548 | { |
ad8a48ac | 5549 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5550 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5551 | struct list_head *entry; |
bfe68a22 | 5552 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5553 | |
733f5c95 | 5554 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5555 | |
5262f567 JA |
5556 | /* |
5557 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5558 | * timeout event to be satisfied. If it isn't set, then this is |
5559 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5560 | */ |
8eb7e2d0 | 5561 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5562 | entry = ctx->timeout_list.prev; |
5563 | goto add; | |
5564 | } | |
5262f567 | 5565 | |
bfe68a22 PB |
5566 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5567 | req->timeout.target_seq = tail + off; | |
5262f567 | 5568 | |
f010505b MDG |
5569 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5570 | * This is safe because ->completion_lock is held, and submissions | |
5571 | * and completions are never mixed in the same ->completion_lock section. | |
5572 | */ | |
5573 | ctx->cq_last_tm_flush = tail; | |
5574 | ||
5262f567 JA |
5575 | /* |
5576 | * Insertion sort, ensuring the first entry in the list is always | |
5577 | * the one we need first. | |
5578 | */ | |
5262f567 | 5579 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5580 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5581 | timeout.list); | |
5262f567 | 5582 | |
8eb7e2d0 | 5583 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5584 | continue; |
bfe68a22 PB |
5585 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5586 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5587 | break; |
5588 | } | |
93bd25bb | 5589 | add: |
135fcde8 | 5590 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5591 | data->timer.function = io_timeout_fn; |
5592 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5593 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5594 | return 0; |
5595 | } | |
5262f567 | 5596 | |
62755e35 JA |
5597 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5598 | { | |
5599 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5600 | ||
5601 | return req->user_data == (unsigned long) data; | |
5602 | } | |
5603 | ||
5aa75ed5 | 5604 | static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr) |
62755e35 | 5605 | { |
62755e35 | 5606 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5607 | int ret = 0; |
5608 | ||
5aa75ed5 JA |
5609 | if (!tctx->io_wq) |
5610 | return -ENOENT; | |
5611 | ||
5612 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false); | |
62755e35 JA |
5613 | switch (cancel_ret) { |
5614 | case IO_WQ_CANCEL_OK: | |
5615 | ret = 0; | |
5616 | break; | |
5617 | case IO_WQ_CANCEL_RUNNING: | |
5618 | ret = -EALREADY; | |
5619 | break; | |
5620 | case IO_WQ_CANCEL_NOTFOUND: | |
5621 | ret = -ENOENT; | |
5622 | break; | |
5623 | } | |
5624 | ||
e977d6d3 JA |
5625 | return ret; |
5626 | } | |
5627 | ||
47f46768 JA |
5628 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5629 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5630 | int success_ret) |
47f46768 JA |
5631 | { |
5632 | unsigned long flags; | |
5633 | int ret; | |
5634 | ||
5aa75ed5 JA |
5635 | ret = io_async_cancel_one(req->task->io_uring, |
5636 | (void *) (unsigned long) sqe_addr); | |
47f46768 JA |
5637 | if (ret != -ENOENT) { |
5638 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5639 | goto done; | |
5640 | } | |
5641 | ||
5642 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5643 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5644 | if (ret != -ENOENT) | |
5645 | goto done; | |
5646 | ret = io_poll_cancel(ctx, sqe_addr); | |
5647 | done: | |
b0dd8a41 JA |
5648 | if (!ret) |
5649 | ret = success_ret; | |
47f46768 JA |
5650 | io_cqring_fill_event(req, ret); |
5651 | io_commit_cqring(ctx); | |
5652 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5653 | io_cqring_ev_posted(ctx); | |
5654 | ||
4e88d6e7 JA |
5655 | if (ret < 0) |
5656 | req_set_fail_links(req); | |
014db007 | 5657 | io_put_req(req); |
47f46768 JA |
5658 | } |
5659 | ||
3529d8c2 JA |
5660 | static int io_async_cancel_prep(struct io_kiocb *req, |
5661 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5662 | { |
fbf23849 | 5663 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5664 | return -EINVAL; |
61710e43 DA |
5665 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5666 | return -EINVAL; | |
5667 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5668 | return -EINVAL; |
5669 | ||
fbf23849 JA |
5670 | req->cancel.addr = READ_ONCE(sqe->addr); |
5671 | return 0; | |
5672 | } | |
5673 | ||
61e98203 | 5674 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5675 | { |
5676 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5677 | |
014db007 | 5678 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5679 | return 0; |
5680 | } | |
5681 | ||
269bbe5f | 5682 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5683 | const struct io_uring_sqe *sqe) |
5684 | { | |
6ca56f84 JA |
5685 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5686 | return -EINVAL; | |
61710e43 DA |
5687 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5688 | return -EINVAL; | |
5689 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5690 | return -EINVAL; |
5691 | ||
269bbe5f BM |
5692 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5693 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5694 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5695 | return -EINVAL; |
269bbe5f | 5696 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5697 | return 0; |
5698 | } | |
5699 | ||
889fca73 | 5700 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5701 | { |
5702 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5703 | struct io_uring_rsrc_update up; |
05f3fb3c | 5704 | int ret; |
fbf23849 | 5705 | |
45d189c6 | 5706 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5707 | return -EAGAIN; |
05f3fb3c | 5708 | |
269bbe5f BM |
5709 | up.offset = req->rsrc_update.offset; |
5710 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5711 | |
5712 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5713 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5714 | mutex_unlock(&ctx->uring_lock); |
5715 | ||
5716 | if (ret < 0) | |
5717 | req_set_fail_links(req); | |
889fca73 | 5718 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5719 | return 0; |
5720 | } | |
5721 | ||
bfe76559 | 5722 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5723 | { |
d625c6ee | 5724 | switch (req->opcode) { |
e781573e | 5725 | case IORING_OP_NOP: |
bfe76559 | 5726 | return 0; |
f67676d1 JA |
5727 | case IORING_OP_READV: |
5728 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5729 | case IORING_OP_READ: |
bfe76559 | 5730 | return io_read_prep(req, sqe); |
f67676d1 JA |
5731 | case IORING_OP_WRITEV: |
5732 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5733 | case IORING_OP_WRITE: |
bfe76559 | 5734 | return io_write_prep(req, sqe); |
0969e783 | 5735 | case IORING_OP_POLL_ADD: |
bfe76559 | 5736 | return io_poll_add_prep(req, sqe); |
0969e783 | 5737 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5738 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5739 | case IORING_OP_FSYNC: |
1155c76a | 5740 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5741 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5742 | return io_sfr_prep(req, sqe); |
03b1230c | 5743 | case IORING_OP_SENDMSG: |
fddaface | 5744 | case IORING_OP_SEND: |
bfe76559 | 5745 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5746 | case IORING_OP_RECVMSG: |
fddaface | 5747 | case IORING_OP_RECV: |
bfe76559 | 5748 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5749 | case IORING_OP_CONNECT: |
bfe76559 | 5750 | return io_connect_prep(req, sqe); |
2d28390a | 5751 | case IORING_OP_TIMEOUT: |
bfe76559 | 5752 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5753 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5754 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5755 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5756 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5757 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5758 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5759 | case IORING_OP_ACCEPT: |
bfe76559 | 5760 | return io_accept_prep(req, sqe); |
d63d1b5e | 5761 | case IORING_OP_FALLOCATE: |
bfe76559 | 5762 | return io_fallocate_prep(req, sqe); |
15b71abe | 5763 | case IORING_OP_OPENAT: |
bfe76559 | 5764 | return io_openat_prep(req, sqe); |
b5dba59e | 5765 | case IORING_OP_CLOSE: |
bfe76559 | 5766 | return io_close_prep(req, sqe); |
05f3fb3c | 5767 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5768 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5769 | case IORING_OP_STATX: |
bfe76559 | 5770 | return io_statx_prep(req, sqe); |
4840e418 | 5771 | case IORING_OP_FADVISE: |
bfe76559 | 5772 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5773 | case IORING_OP_MADVISE: |
bfe76559 | 5774 | return io_madvise_prep(req, sqe); |
cebdb986 | 5775 | case IORING_OP_OPENAT2: |
bfe76559 | 5776 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5777 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5778 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5779 | case IORING_OP_SPLICE: |
bfe76559 | 5780 | return io_splice_prep(req, sqe); |
ddf0322d | 5781 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5782 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5783 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5784 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5785 | case IORING_OP_TEE: |
bfe76559 | 5786 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5787 | case IORING_OP_SHUTDOWN: |
5788 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5789 | case IORING_OP_RENAMEAT: |
5790 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5791 | case IORING_OP_UNLINKAT: |
5792 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5793 | } |
5794 | ||
bfe76559 PB |
5795 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5796 | req->opcode); | |
5797 | return-EINVAL; | |
5798 | } | |
5799 | ||
93642ef8 | 5800 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5801 | { |
93642ef8 PB |
5802 | switch (req->opcode) { |
5803 | case IORING_OP_READV: | |
5804 | case IORING_OP_READ_FIXED: | |
5805 | case IORING_OP_READ: | |
5806 | return io_rw_prep_async(req, READ); | |
5807 | case IORING_OP_WRITEV: | |
5808 | case IORING_OP_WRITE_FIXED: | |
5809 | case IORING_OP_WRITE: | |
5810 | return io_rw_prep_async(req, WRITE); | |
5811 | case IORING_OP_SENDMSG: | |
5812 | case IORING_OP_SEND: | |
5813 | return io_sendmsg_prep_async(req); | |
5814 | case IORING_OP_RECVMSG: | |
5815 | case IORING_OP_RECV: | |
5816 | return io_recvmsg_prep_async(req); | |
5817 | case IORING_OP_CONNECT: | |
5818 | return io_connect_prep_async(req); | |
5819 | } | |
5820 | return 0; | |
5821 | } | |
5822 | ||
be7053b7 | 5823 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 5824 | { |
be7053b7 | 5825 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 5826 | return 0; |
be7053b7 | 5827 | /* some opcodes init it during the inital prep */ |
93642ef8 | 5828 | if (req->async_data) |
be7053b7 PB |
5829 | return 0; |
5830 | if (__io_alloc_async_data(req)) | |
bfe76559 | 5831 | return -EAGAIN; |
be7053b7 | 5832 | return io_req_prep_async(req); |
f67676d1 JA |
5833 | } |
5834 | ||
9cf7c104 PB |
5835 | static u32 io_get_sequence(struct io_kiocb *req) |
5836 | { | |
5837 | struct io_kiocb *pos; | |
5838 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 5839 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 5840 | |
f2f87370 PB |
5841 | io_for_each_link(pos, req) |
5842 | nr_reqs++; | |
9cf7c104 PB |
5843 | |
5844 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5845 | return total_submitted - nr_reqs; | |
5846 | } | |
5847 | ||
be7053b7 | 5848 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 5849 | { |
a197f664 | 5850 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5851 | struct io_defer_entry *de; |
f67676d1 | 5852 | int ret; |
9cf7c104 | 5853 | u32 seq; |
de0617e4 | 5854 | |
9d858b21 | 5855 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5856 | if (likely(list_empty_careful(&ctx->defer_list) && |
5857 | !(req->flags & REQ_F_IO_DRAIN))) | |
5858 | return 0; | |
5859 | ||
5860 | seq = io_get_sequence(req); | |
5861 | /* Still a chance to pass the sequence check */ | |
5862 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5863 | return 0; |
5864 | ||
be7053b7 PB |
5865 | ret = io_req_defer_prep(req); |
5866 | if (ret) | |
5867 | return ret; | |
cbdcb435 | 5868 | io_prep_async_link(req); |
27dc8338 PB |
5869 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5870 | if (!de) | |
5871 | return -ENOMEM; | |
2d28390a | 5872 | |
de0617e4 | 5873 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5874 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5875 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5876 | kfree(de); |
ae34817b PB |
5877 | io_queue_async_work(req); |
5878 | return -EIOCBQUEUED; | |
de0617e4 JA |
5879 | } |
5880 | ||
915967f6 | 5881 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5882 | de->req = req; |
9cf7c104 | 5883 | de->seq = seq; |
27dc8338 | 5884 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5885 | spin_unlock_irq(&ctx->completion_lock); |
5886 | return -EIOCBQUEUED; | |
5887 | } | |
5888 | ||
3ca405eb | 5889 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5890 | { |
0e1b6fe3 PB |
5891 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5892 | switch (req->opcode) { | |
5893 | case IORING_OP_READV: | |
5894 | case IORING_OP_READ_FIXED: | |
5895 | case IORING_OP_READ: | |
bcda7baa | 5896 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5897 | break; |
5898 | case IORING_OP_RECVMSG: | |
5899 | case IORING_OP_RECV: | |
bcda7baa | 5900 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5901 | break; |
5902 | } | |
5903 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5904 | } |
5905 | ||
0e1b6fe3 PB |
5906 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5907 | switch (req->opcode) { | |
5908 | case IORING_OP_READV: | |
5909 | case IORING_OP_READ_FIXED: | |
5910 | case IORING_OP_READ: | |
5911 | case IORING_OP_WRITEV: | |
5912 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5913 | case IORING_OP_WRITE: { |
5914 | struct io_async_rw *io = req->async_data; | |
5915 | if (io->free_iovec) | |
5916 | kfree(io->free_iovec); | |
0e1b6fe3 | 5917 | break; |
e8c2bc1f | 5918 | } |
0e1b6fe3 | 5919 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5920 | case IORING_OP_SENDMSG: { |
5921 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
5922 | |
5923 | kfree(io->free_iov); | |
0e1b6fe3 | 5924 | break; |
e8c2bc1f | 5925 | } |
0e1b6fe3 PB |
5926 | case IORING_OP_SPLICE: |
5927 | case IORING_OP_TEE: | |
5928 | io_put_file(req, req->splice.file_in, | |
5929 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5930 | break; | |
f3cd4850 JA |
5931 | case IORING_OP_OPENAT: |
5932 | case IORING_OP_OPENAT2: | |
5933 | if (req->open.filename) | |
5934 | putname(req->open.filename); | |
5935 | break; | |
80a261fd JA |
5936 | case IORING_OP_RENAMEAT: |
5937 | putname(req->rename.oldpath); | |
5938 | putname(req->rename.newpath); | |
5939 | break; | |
14a1143b JA |
5940 | case IORING_OP_UNLINKAT: |
5941 | putname(req->unlink.filename); | |
5942 | break; | |
0e1b6fe3 PB |
5943 | } |
5944 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5945 | } |
99bc4c38 PB |
5946 | } |
5947 | ||
889fca73 | 5948 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 5949 | { |
a197f664 | 5950 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 5951 | int ret; |
2b188cc1 | 5952 | |
d625c6ee | 5953 | switch (req->opcode) { |
2b188cc1 | 5954 | case IORING_OP_NOP: |
889fca73 | 5955 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
5956 | break; |
5957 | case IORING_OP_READV: | |
edafccee | 5958 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5959 | case IORING_OP_READ: |
889fca73 | 5960 | ret = io_read(req, issue_flags); |
edafccee | 5961 | break; |
3529d8c2 | 5962 | case IORING_OP_WRITEV: |
edafccee | 5963 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5964 | case IORING_OP_WRITE: |
889fca73 | 5965 | ret = io_write(req, issue_flags); |
2b188cc1 | 5966 | break; |
c992fe29 | 5967 | case IORING_OP_FSYNC: |
45d189c6 | 5968 | ret = io_fsync(req, issue_flags); |
c992fe29 | 5969 | break; |
221c5eb2 | 5970 | case IORING_OP_POLL_ADD: |
61e98203 | 5971 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
5972 | break; |
5973 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 5974 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 5975 | break; |
5d17b4a4 | 5976 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 5977 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 5978 | break; |
0fa03c62 | 5979 | case IORING_OP_SENDMSG: |
889fca73 | 5980 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 5981 | break; |
fddaface | 5982 | case IORING_OP_SEND: |
889fca73 | 5983 | ret = io_send(req, issue_flags); |
0fa03c62 | 5984 | break; |
aa1fa28f | 5985 | case IORING_OP_RECVMSG: |
889fca73 | 5986 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 5987 | break; |
fddaface | 5988 | case IORING_OP_RECV: |
889fca73 | 5989 | ret = io_recv(req, issue_flags); |
aa1fa28f | 5990 | break; |
5262f567 | 5991 | case IORING_OP_TIMEOUT: |
61e98203 | 5992 | ret = io_timeout(req, issue_flags); |
5262f567 | 5993 | break; |
11365043 | 5994 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 5995 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 5996 | break; |
17f2fe35 | 5997 | case IORING_OP_ACCEPT: |
889fca73 | 5998 | ret = io_accept(req, issue_flags); |
17f2fe35 | 5999 | break; |
f8e85cf2 | 6000 | case IORING_OP_CONNECT: |
889fca73 | 6001 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6002 | break; |
62755e35 | 6003 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6004 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6005 | break; |
d63d1b5e | 6006 | case IORING_OP_FALLOCATE: |
45d189c6 | 6007 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6008 | break; |
15b71abe | 6009 | case IORING_OP_OPENAT: |
45d189c6 | 6010 | ret = io_openat(req, issue_flags); |
15b71abe | 6011 | break; |
b5dba59e | 6012 | case IORING_OP_CLOSE: |
889fca73 | 6013 | ret = io_close(req, issue_flags); |
b5dba59e | 6014 | break; |
05f3fb3c | 6015 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6016 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6017 | break; |
eddc7ef5 | 6018 | case IORING_OP_STATX: |
45d189c6 | 6019 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6020 | break; |
4840e418 | 6021 | case IORING_OP_FADVISE: |
45d189c6 | 6022 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6023 | break; |
c1ca757b | 6024 | case IORING_OP_MADVISE: |
45d189c6 | 6025 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6026 | break; |
cebdb986 | 6027 | case IORING_OP_OPENAT2: |
45d189c6 | 6028 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6029 | break; |
3e4827b0 | 6030 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6031 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6032 | break; |
7d67af2c | 6033 | case IORING_OP_SPLICE: |
45d189c6 | 6034 | ret = io_splice(req, issue_flags); |
7d67af2c | 6035 | break; |
ddf0322d | 6036 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6037 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6038 | break; |
067524e9 | 6039 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6040 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6041 | break; |
f2a8d5c7 | 6042 | case IORING_OP_TEE: |
45d189c6 | 6043 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6044 | break; |
36f4fa68 | 6045 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6046 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6047 | break; |
80a261fd | 6048 | case IORING_OP_RENAMEAT: |
45d189c6 | 6049 | ret = io_renameat(req, issue_flags); |
80a261fd | 6050 | break; |
14a1143b | 6051 | case IORING_OP_UNLINKAT: |
45d189c6 | 6052 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6053 | break; |
2b188cc1 JA |
6054 | default: |
6055 | ret = -EINVAL; | |
6056 | break; | |
6057 | } | |
6058 | ||
def596e9 JA |
6059 | if (ret) |
6060 | return ret; | |
6061 | ||
b532576e JA |
6062 | /* If the op doesn't have a file, we're not polling for it */ |
6063 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6064 | const bool in_async = io_wq_current_is_worker(); |
6065 | ||
11ba820b JA |
6066 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6067 | if (in_async) | |
6068 | mutex_lock(&ctx->uring_lock); | |
6069 | ||
2e9dbe90 | 6070 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6071 | |
6072 | if (in_async) | |
6073 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6074 | } |
6075 | ||
6076 | return 0; | |
2b188cc1 JA |
6077 | } |
6078 | ||
5280f7e5 | 6079 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6080 | { |
6081 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6082 | struct io_kiocb *timeout; |
561fb04a | 6083 | int ret = 0; |
2b188cc1 | 6084 | |
6df1db6b PB |
6085 | timeout = io_prep_linked_timeout(req); |
6086 | if (timeout) | |
6087 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6088 | |
4014d943 | 6089 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6090 | ret = -ECANCELED; |
31b51510 | 6091 | |
561fb04a | 6092 | if (!ret) { |
561fb04a | 6093 | do { |
889fca73 | 6094 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6095 | /* |
6096 | * We can get EAGAIN for polled IO even though we're | |
6097 | * forcing a sync submission from here, since we can't | |
6098 | * wait for request slots on the block side. | |
6099 | */ | |
6100 | if (ret != -EAGAIN) | |
6101 | break; | |
6102 | cond_resched(); | |
6103 | } while (1); | |
6104 | } | |
31b51510 | 6105 | |
a3df7698 | 6106 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6107 | if (ret) { |
a3df7698 PB |
6108 | /* io-wq is going to take one down */ |
6109 | refcount_inc(&req->refs); | |
6110 | io_req_task_queue_fail(req, ret); | |
edafccee | 6111 | } |
2b188cc1 JA |
6112 | } |
6113 | ||
65e19f54 JA |
6114 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6115 | int index) | |
6116 | { | |
269bbe5f | 6117 | struct fixed_rsrc_table *table; |
65e19f54 | 6118 | |
05f3fb3c | 6119 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6120 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6121 | } |
6122 | ||
8371adf5 PB |
6123 | static struct file *io_file_get(struct io_submit_state *state, |
6124 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6125 | { |
a197f664 | 6126 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6127 | struct file *file; |
09bb8394 | 6128 | |
8da11c19 | 6129 | if (fixed) { |
479f517b | 6130 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6131 | return NULL; |
b7620121 | 6132 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6133 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6134 | io_set_resource_node(req); |
09bb8394 | 6135 | } else { |
c826bd7a | 6136 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6137 | file = __io_file_get(state, fd); |
09bb8394 JA |
6138 | } |
6139 | ||
ce3d5aae PB |
6140 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6141 | io_req_track_inflight(req); | |
8371adf5 | 6142 | return file; |
09bb8394 JA |
6143 | } |
6144 | ||
2665abfd | 6145 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6146 | { |
ad8a48ac JA |
6147 | struct io_timeout_data *data = container_of(timer, |
6148 | struct io_timeout_data, timer); | |
90cd7e42 | 6149 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6150 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6151 | unsigned long flags; |
2665abfd JA |
6152 | |
6153 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6154 | prev = req->timeout.head; |
6155 | req->timeout.head = NULL; | |
2665abfd JA |
6156 | |
6157 | /* | |
6158 | * We don't expect the list to be empty, that will only happen if we | |
6159 | * race with the completion of the linked work. | |
6160 | */ | |
90cd7e42 | 6161 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6162 | io_remove_next_linked(prev); |
90cd7e42 PB |
6163 | else |
6164 | prev = NULL; | |
2665abfd JA |
6165 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6166 | ||
6167 | if (prev) { | |
4e88d6e7 | 6168 | req_set_fail_links(prev); |
014db007 | 6169 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6170 | io_put_req_deferred(prev, 1); |
47f46768 | 6171 | } else { |
9ae1f8dd PB |
6172 | io_req_complete_post(req, -ETIME, 0); |
6173 | io_put_req_deferred(req, 1); | |
2665abfd | 6174 | } |
2665abfd JA |
6175 | return HRTIMER_NORESTART; |
6176 | } | |
6177 | ||
7271ef3a | 6178 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6179 | { |
76a46e06 | 6180 | /* |
f2f87370 PB |
6181 | * If the back reference is NULL, then our linked request finished |
6182 | * before we got a chance to setup the timer | |
76a46e06 | 6183 | */ |
90cd7e42 | 6184 | if (req->timeout.head) { |
e8c2bc1f | 6185 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6186 | |
ad8a48ac JA |
6187 | data->timer.function = io_link_timeout_fn; |
6188 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6189 | data->mode); | |
2665abfd | 6190 | } |
7271ef3a JA |
6191 | } |
6192 | ||
6193 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6194 | { | |
6195 | struct io_ring_ctx *ctx = req->ctx; | |
6196 | ||
6197 | spin_lock_irq(&ctx->completion_lock); | |
6198 | __io_queue_linked_timeout(req); | |
76a46e06 | 6199 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6200 | |
2665abfd | 6201 | /* drop submission reference */ |
76a46e06 JA |
6202 | io_put_req(req); |
6203 | } | |
2665abfd | 6204 | |
ad8a48ac | 6205 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6206 | { |
f2f87370 | 6207 | struct io_kiocb *nxt = req->link; |
2665abfd | 6208 | |
f2f87370 PB |
6209 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6210 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6211 | return NULL; |
2665abfd | 6212 | |
90cd7e42 | 6213 | nxt->timeout.head = req; |
900fad45 | 6214 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6215 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6216 | return nxt; |
2665abfd JA |
6217 | } |
6218 | ||
c5eef2b9 | 6219 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6220 | { |
d3d7298d | 6221 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
193155c8 | 6222 | const struct cred *old_creds = NULL; |
e0c5c576 | 6223 | int ret; |
2b188cc1 | 6224 | |
4379bf8b JA |
6225 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
6226 | req->work.creds != current_cred()) | |
6227 | old_creds = override_creds(req->work.creds); | |
193155c8 | 6228 | |
c5eef2b9 | 6229 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6230 | |
d3d7298d PB |
6231 | if (old_creds) |
6232 | revert_creds(old_creds); | |
491381ce JA |
6233 | |
6234 | /* | |
6235 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6236 | * doesn't support non-blocking read/write attempts | |
6237 | */ | |
24c74678 | 6238 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6239 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6240 | /* |
6241 | * Queued up for async execution, worker will release | |
6242 | * submit reference when the iocb is actually submitted. | |
6243 | */ | |
6244 | io_queue_async_work(req); | |
2b188cc1 | 6245 | } |
0d63c148 PB |
6246 | } else if (likely(!ret)) { |
6247 | /* drop submission reference */ | |
e342c807 | 6248 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6249 | struct io_ring_ctx *ctx = req->ctx; |
6250 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6251 | |
6dd0be1e | 6252 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6253 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6254 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6255 | } else { |
d3d7298d | 6256 | io_put_req(req); |
0d63c148 PB |
6257 | } |
6258 | } else { | |
4e88d6e7 | 6259 | req_set_fail_links(req); |
e65ef56d | 6260 | io_put_req(req); |
e1e16097 | 6261 | io_req_complete(req, ret); |
9e645e11 | 6262 | } |
d3d7298d PB |
6263 | if (linked_timeout) |
6264 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6265 | } |
6266 | ||
be7053b7 | 6267 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6268 | { |
6269 | int ret; | |
6270 | ||
be7053b7 | 6271 | ret = io_req_defer(req); |
4fe2c963 JL |
6272 | if (ret) { |
6273 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6274 | fail_req: |
4e88d6e7 | 6275 | req_set_fail_links(req); |
e1e16097 JA |
6276 | io_put_req(req); |
6277 | io_req_complete(req, ret); | |
4fe2c963 | 6278 | } |
2550878f | 6279 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6280 | ret = io_req_defer_prep(req); |
6281 | if (unlikely(ret)) | |
6282 | goto fail_req; | |
ce35a47a JA |
6283 | io_queue_async_work(req); |
6284 | } else { | |
c5eef2b9 | 6285 | __io_queue_sqe(req); |
ce35a47a | 6286 | } |
4fe2c963 JL |
6287 | } |
6288 | ||
b16fed66 PB |
6289 | /* |
6290 | * Check SQE restrictions (opcode and flags). | |
6291 | * | |
6292 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6293 | */ | |
6294 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6295 | struct io_kiocb *req, | |
6296 | unsigned int sqe_flags) | |
4fe2c963 | 6297 | { |
b16fed66 PB |
6298 | if (!ctx->restricted) |
6299 | return true; | |
6300 | ||
6301 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6302 | return false; | |
6303 | ||
6304 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6305 | ctx->restrictions.sqe_flags_required) | |
6306 | return false; | |
6307 | ||
6308 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6309 | ctx->restrictions.sqe_flags_required)) | |
6310 | return false; | |
6311 | ||
6312 | return true; | |
4fe2c963 JL |
6313 | } |
6314 | ||
b16fed66 PB |
6315 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6316 | const struct io_uring_sqe *sqe) | |
6317 | { | |
6318 | struct io_submit_state *state; | |
6319 | unsigned int sqe_flags; | |
6320 | int id, ret = 0; | |
6321 | ||
6322 | req->opcode = READ_ONCE(sqe->opcode); | |
6323 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6324 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6325 | req->user_data = READ_ONCE(sqe->user_data); | |
6326 | req->async_data = NULL; | |
6327 | req->file = NULL; | |
6328 | req->ctx = ctx; | |
6329 | req->link = NULL; | |
6330 | req->fixed_rsrc_refs = NULL; | |
6331 | /* one is dropped after submission, the other at completion */ | |
6332 | refcount_set(&req->refs, 2); | |
6333 | req->task = current; | |
6334 | req->result = 0; | |
863e0560 | 6335 | |
b16fed66 | 6336 | /* enforce forwards compatibility on users */ |
ebf4a5db PB |
6337 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6338 | req->flags = 0; | |
b16fed66 | 6339 | return -EINVAL; |
ebf4a5db | 6340 | } |
b16fed66 PB |
6341 | |
6342 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6343 | return -EINVAL; | |
6344 | ||
b16fed66 PB |
6345 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6346 | return -EACCES; | |
6347 | ||
6348 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6349 | !io_op_defs[req->opcode].buffer_select) | |
6350 | return -EOPNOTSUPP; | |
6351 | ||
6352 | id = READ_ONCE(sqe->personality); | |
6353 | if (id) { | |
b16fed66 | 6354 | __io_req_init_async(req); |
4379bf8b JA |
6355 | req->work.creds = idr_find(&ctx->personality_idr, id); |
6356 | if (unlikely(!req->work.creds)) | |
6357 | return -EINVAL; | |
6358 | get_cred(req->work.creds); | |
b16fed66 PB |
6359 | } |
6360 | ||
6361 | state = &ctx->submit_state; | |
6362 | ||
6363 | /* | |
6364 | * Plug now if we have more than 1 IO left after this, and the target | |
6365 | * is potentially a read/write to block based storage. | |
6366 | */ | |
6367 | if (!state->plug_started && state->ios_left > 1 && | |
6368 | io_op_defs[req->opcode].plug) { | |
6369 | blk_start_plug(&state->plug); | |
6370 | state->plug_started = true; | |
6371 | } | |
6372 | ||
6373 | if (io_op_defs[req->opcode].needs_file) { | |
6374 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6375 | ||
6376 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6377 | if (unlikely(!req->file)) | |
6378 | ret = -EBADF; | |
6379 | } | |
6380 | ||
6381 | state->ios_left--; | |
6382 | return ret; | |
6383 | } | |
6384 | ||
a6b8cadc | 6385 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6386 | const struct io_uring_sqe *sqe) |
9e645e11 | 6387 | { |
a1ab7b35 | 6388 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6389 | int ret; |
9e645e11 | 6390 | |
a6b8cadc PB |
6391 | ret = io_init_req(ctx, req, sqe); |
6392 | if (unlikely(ret)) { | |
6393 | fail_req: | |
6394 | io_put_req(req); | |
6395 | io_req_complete(req, ret); | |
de59bc10 PB |
6396 | if (link->head) { |
6397 | /* fail even hard links since we don't submit */ | |
cf109604 | 6398 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6399 | io_put_req(link->head); |
6400 | io_req_complete(link->head, -ECANCELED); | |
6401 | link->head = NULL; | |
6402 | } | |
a6b8cadc PB |
6403 | return ret; |
6404 | } | |
be7053b7 PB |
6405 | ret = io_req_prep(req, sqe); |
6406 | if (unlikely(ret)) | |
6407 | goto fail_req; | |
a6b8cadc | 6408 | |
be7053b7 | 6409 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6410 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6411 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6412 | ||
9e645e11 JA |
6413 | /* |
6414 | * If we already have a head request, queue this one for async | |
6415 | * submittal once the head completes. If we don't have a head but | |
6416 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6417 | * submitted sync once the chain is complete. If none of those | |
6418 | * conditions are true (normal request), then just queue it. | |
6419 | */ | |
863e0560 PB |
6420 | if (link->head) { |
6421 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6422 | |
8cdf2193 PB |
6423 | /* |
6424 | * Taking sequential execution of a link, draining both sides | |
6425 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6426 | * requests in the link. So, it drains the head and the | |
6427 | * next after the link request. The last one is done via | |
6428 | * drain_next flag to persist the effect across calls. | |
6429 | */ | |
ef4ff581 | 6430 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6431 | head->flags |= REQ_F_IO_DRAIN; |
6432 | ctx->drain_next = 1; | |
6433 | } | |
be7053b7 | 6434 | ret = io_req_defer_prep(req); |
cf109604 | 6435 | if (unlikely(ret)) |
a6b8cadc | 6436 | goto fail_req; |
9d76377f | 6437 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6438 | link->last->link = req; |
863e0560 | 6439 | link->last = req; |
32fe525b PB |
6440 | |
6441 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6442 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6443 | io_queue_sqe(head); |
863e0560 | 6444 | link->head = NULL; |
32fe525b | 6445 | } |
9e645e11 | 6446 | } else { |
711be031 PB |
6447 | if (unlikely(ctx->drain_next)) { |
6448 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6449 | ctx->drain_next = 0; |
711be031 | 6450 | } |
ef4ff581 | 6451 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6452 | link->head = req; |
6453 | link->last = req; | |
711be031 | 6454 | } else { |
be7053b7 | 6455 | io_queue_sqe(req); |
711be031 | 6456 | } |
9e645e11 | 6457 | } |
2e6e1fde | 6458 | |
1d4240cc | 6459 | return 0; |
9e645e11 JA |
6460 | } |
6461 | ||
9a56a232 JA |
6462 | /* |
6463 | * Batched submission is done, ensure local IO is flushed out. | |
6464 | */ | |
ba88ff11 PB |
6465 | static void io_submit_state_end(struct io_submit_state *state, |
6466 | struct io_ring_ctx *ctx) | |
9a56a232 | 6467 | { |
a1ab7b35 | 6468 | if (state->link.head) |
de59bc10 | 6469 | io_queue_sqe(state->link.head); |
6dd0be1e | 6470 | if (state->comp.nr) |
ba88ff11 | 6471 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6472 | if (state->plug_started) |
6473 | blk_finish_plug(&state->plug); | |
9f13c35b | 6474 | io_state_file_put(state); |
9a56a232 JA |
6475 | } |
6476 | ||
6477 | /* | |
6478 | * Start submission side cache. | |
6479 | */ | |
6480 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6481 | unsigned int max_ios) |
9a56a232 | 6482 | { |
27926b68 | 6483 | state->plug_started = false; |
9a56a232 | 6484 | state->ios_left = max_ios; |
a1ab7b35 PB |
6485 | /* set only head, no need to init link_last in advance */ |
6486 | state->link.head = NULL; | |
9a56a232 JA |
6487 | } |
6488 | ||
2b188cc1 JA |
6489 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6490 | { | |
75b28aff | 6491 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6492 | |
caf582c6 PB |
6493 | /* |
6494 | * Ensure any loads from the SQEs are done at this point, | |
6495 | * since once we write the new head, the application could | |
6496 | * write new data to them. | |
6497 | */ | |
6498 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6499 | } |
6500 | ||
2b188cc1 | 6501 | /* |
3529d8c2 | 6502 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6503 | * that is mapped by userspace. This means that care needs to be taken to |
6504 | * ensure that reads are stable, as we cannot rely on userspace always | |
6505 | * being a good citizen. If members of the sqe are validated and then later | |
6506 | * used, it's important that those reads are done through READ_ONCE() to | |
6507 | * prevent a re-load down the line. | |
6508 | */ | |
709b302f | 6509 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6510 | { |
75b28aff | 6511 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6512 | unsigned head; |
6513 | ||
6514 | /* | |
6515 | * The cached sq head (or cq tail) serves two purposes: | |
6516 | * | |
6517 | * 1) allows us to batch the cost of updating the user visible | |
6518 | * head updates. | |
6519 | * 2) allows the kernel side to track the head on its own, even | |
6520 | * though the application is the one updating it. | |
6521 | */ | |
4fccfcbb | 6522 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6523 | if (likely(head < ctx->sq_entries)) |
6524 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6525 | |
6526 | /* drop invalid entries */ | |
498ccd9e | 6527 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6528 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6529 | return NULL; |
6530 | } | |
6531 | ||
0f212204 | 6532 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6533 | { |
46c4e16a | 6534 | int submitted = 0; |
6c271ce2 | 6535 | |
c4a2ed72 | 6536 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6537 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6538 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6539 | return -EBUSY; |
6540 | } | |
6c271ce2 | 6541 | |
ee7d46d9 PB |
6542 | /* make sure SQ entry isn't read before tail */ |
6543 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6544 | |
2b85edfc PB |
6545 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6546 | return -EAGAIN; | |
6c271ce2 | 6547 | |
d8a6df10 | 6548 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6549 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6550 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6551 | |
46c4e16a | 6552 | while (submitted < nr) { |
3529d8c2 | 6553 | const struct io_uring_sqe *sqe; |
196be95c | 6554 | struct io_kiocb *req; |
fb5ccc98 | 6555 | |
258b29a9 | 6556 | req = io_alloc_req(ctx); |
196be95c PB |
6557 | if (unlikely(!req)) { |
6558 | if (!submitted) | |
6559 | submitted = -EAGAIN; | |
fb5ccc98 | 6560 | break; |
196be95c | 6561 | } |
4fccfcbb PB |
6562 | sqe = io_get_sqe(ctx); |
6563 | if (unlikely(!sqe)) { | |
6564 | kmem_cache_free(req_cachep, req); | |
6565 | break; | |
6566 | } | |
d3656344 JA |
6567 | /* will complete beyond this point, count as submitted */ |
6568 | submitted++; | |
a1ab7b35 | 6569 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6570 | break; |
6c271ce2 JA |
6571 | } |
6572 | ||
9466f437 PB |
6573 | if (unlikely(submitted != nr)) { |
6574 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6575 | struct io_uring_task *tctx = current->io_uring; |
6576 | int unused = nr - ref_used; | |
9466f437 | 6577 | |
d8a6df10 JA |
6578 | percpu_ref_put_many(&ctx->refs, unused); |
6579 | percpu_counter_sub(&tctx->inflight, unused); | |
6580 | put_task_struct_many(current, unused); | |
9466f437 | 6581 | } |
6c271ce2 | 6582 | |
a1ab7b35 | 6583 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6584 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6585 | io_commit_sqring(ctx); | |
6586 | ||
6c271ce2 JA |
6587 | return submitted; |
6588 | } | |
6589 | ||
23b3628e XW |
6590 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6591 | { | |
6592 | /* Tell userspace we may need a wakeup call */ | |
6593 | spin_lock_irq(&ctx->completion_lock); | |
6594 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6595 | spin_unlock_irq(&ctx->completion_lock); | |
6596 | } | |
6597 | ||
6598 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6599 | { | |
6600 | spin_lock_irq(&ctx->completion_lock); | |
6601 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6602 | spin_unlock_irq(&ctx->completion_lock); | |
6603 | } | |
6604 | ||
08369246 | 6605 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6606 | { |
c8d1ba58 | 6607 | unsigned int to_submit; |
bdcd3eab | 6608 | int ret = 0; |
6c271ce2 | 6609 | |
c8d1ba58 | 6610 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6611 | /* if we're handling multiple rings, cap submit size for fairness */ |
6612 | if (cap_entries && to_submit > 8) | |
6613 | to_submit = 8; | |
6614 | ||
906a3c6f | 6615 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6616 | unsigned nr_events = 0; |
a4c0b3de | 6617 | |
c8d1ba58 | 6618 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6619 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6620 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6621 | |
d9d05217 PB |
6622 | if (to_submit && !ctx->sqo_dead && |
6623 | likely(!percpu_ref_is_dying(&ctx->refs))) | |
08369246 | 6624 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6625 | mutex_unlock(&ctx->uring_lock); |
6626 | } | |
6c271ce2 | 6627 | |
90554200 JA |
6628 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6629 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6630 | |
08369246 XW |
6631 | return ret; |
6632 | } | |
6c271ce2 | 6633 | |
08369246 XW |
6634 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6635 | { | |
6636 | struct io_ring_ctx *ctx; | |
6637 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6638 | |
08369246 XW |
6639 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6640 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6641 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6642 | } |
c1edbf5f | 6643 | |
08369246 | 6644 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6645 | } |
6c271ce2 | 6646 | |
69fb2131 JA |
6647 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6648 | { | |
6649 | struct io_ring_ctx *ctx; | |
6650 | ||
6651 | while (!list_empty(&sqd->ctx_new_list)) { | |
6652 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6653 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6654 | complete(&ctx->sq_thread_comp); | |
6655 | } | |
08369246 XW |
6656 | |
6657 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6658 | } |
6659 | ||
37d1e2e3 JA |
6660 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
6661 | { | |
6662 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
6663 | } | |
6664 | ||
6665 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) | |
6666 | { | |
6667 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
6668 | } | |
6669 | ||
6670 | static void io_sq_thread_parkme(struct io_sq_data *sqd) | |
6671 | { | |
6672 | for (;;) { | |
6673 | /* | |
6674 | * TASK_PARKED is a special state; we must serialize against | |
6675 | * possible pending wakeups to avoid store-store collisions on | |
6676 | * task->state. | |
6677 | * | |
6678 | * Such a collision might possibly result in the task state | |
6679 | * changin from TASK_PARKED and us failing the | |
6680 | * wait_task_inactive() in kthread_park(). | |
6681 | */ | |
6682 | set_special_state(TASK_PARKED); | |
6683 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) | |
6684 | break; | |
6685 | ||
6686 | /* | |
6687 | * Thread is going to call schedule(), do not preempt it, | |
6688 | * or the caller of kthread_park() may spend more time in | |
6689 | * wait_task_inactive(). | |
6690 | */ | |
6691 | preempt_disable(); | |
6692 | complete(&sqd->completion); | |
6693 | schedule_preempt_disabled(); | |
6694 | preempt_enable(); | |
6695 | } | |
6696 | __set_current_state(TASK_RUNNING); | |
6697 | } | |
6698 | ||
c8d1ba58 JA |
6699 | static int io_sq_thread(void *data) |
6700 | { | |
69fb2131 JA |
6701 | struct io_sq_data *sqd = data; |
6702 | struct io_ring_ctx *ctx; | |
a0d9205f | 6703 | unsigned long timeout = 0; |
37d1e2e3 | 6704 | char buf[TASK_COMM_LEN]; |
08369246 | 6705 | DEFINE_WAIT(wait); |
6c271ce2 | 6706 | |
37d1e2e3 JA |
6707 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
6708 | set_task_comm(current, buf); | |
6709 | sqd->thread = current; | |
6710 | current->pf_io_worker = NULL; | |
6711 | ||
6712 | if (sqd->sq_cpu != -1) | |
6713 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6714 | else | |
6715 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6716 | current->flags |= PF_NO_SETAFFINITY; | |
6717 | ||
6718 | complete(&sqd->completion); | |
6c271ce2 | 6719 | |
37d1e2e3 JA |
6720 | wait_for_completion(&sqd->startup); |
6721 | ||
6722 | while (!io_sq_thread_should_stop(sqd)) { | |
08369246 XW |
6723 | int ret; |
6724 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f JA |
6725 | |
6726 | /* | |
69fb2131 | 6727 | * Any changes to the sqd lists are synchronized through the |
37d1e2e3 | 6728 | * thread parking. This synchronizes the thread vs users, |
69fb2131 | 6729 | * the users are synchronized on the sqd->ctx_lock. |
c1edbf5f | 6730 | */ |
37d1e2e3 JA |
6731 | if (io_sq_thread_should_park(sqd)) { |
6732 | io_sq_thread_parkme(sqd); | |
6733 | continue; | |
65b2b213 | 6734 | } |
08369246 | 6735 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 6736 | io_sqd_init_new(sqd); |
08369246 XW |
6737 | timeout = jiffies + sqd->sq_thread_idle; |
6738 | } | |
37d1e2e3 JA |
6739 | if (fatal_signal_pending(current)) |
6740 | break; | |
08369246 | 6741 | sqt_spin = false; |
e95eee2d | 6742 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6743 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
08369246 XW |
6744 | ret = __io_sq_thread(ctx, cap_entries); |
6745 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) | |
6746 | sqt_spin = true; | |
69fb2131 | 6747 | } |
6c271ce2 | 6748 | |
08369246 | 6749 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6750 | io_run_task_work(); |
6751 | cond_resched(); | |
08369246 XW |
6752 | if (sqt_spin) |
6753 | timeout = jiffies + sqd->sq_thread_idle; | |
6754 | continue; | |
6755 | } | |
6756 | ||
08369246 XW |
6757 | needs_sched = true; |
6758 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6759 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6760 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6761 | !list_empty_careful(&ctx->iopoll_list)) { | |
6762 | needs_sched = false; | |
6763 | break; | |
6764 | } | |
6765 | if (io_sqring_entries(ctx)) { | |
6766 | needs_sched = false; | |
6767 | break; | |
6768 | } | |
6769 | } | |
6770 | ||
37d1e2e3 | 6771 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
69fb2131 JA |
6772 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6773 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6774 | |
69fb2131 | 6775 | schedule(); |
69fb2131 JA |
6776 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6777 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6778 | } |
08369246 XW |
6779 | |
6780 | finish_wait(&sqd->wait, &wait); | |
6781 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
6782 | } |
6783 | ||
37d1e2e3 JA |
6784 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6785 | io_uring_cancel_sqpoll(ctx); | |
b41e9852 | 6786 | |
37d1e2e3 | 6787 | io_run_task_work(); |
28cea78a | 6788 | |
37d1e2e3 JA |
6789 | /* |
6790 | * Clear thread under lock so that concurrent parks work correctly | |
6791 | */ | |
6792 | complete_all(&sqd->completion); | |
6793 | mutex_lock(&sqd->lock); | |
6794 | sqd->thread = NULL; | |
6795 | mutex_unlock(&sqd->lock); | |
06058632 | 6796 | |
37d1e2e3 JA |
6797 | complete(&sqd->exited); |
6798 | do_exit(0); | |
6c271ce2 JA |
6799 | } |
6800 | ||
bda52162 JA |
6801 | struct io_wait_queue { |
6802 | struct wait_queue_entry wq; | |
6803 | struct io_ring_ctx *ctx; | |
6804 | unsigned to_wait; | |
6805 | unsigned nr_timeouts; | |
6806 | }; | |
6807 | ||
6c503150 | 6808 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6809 | { |
6810 | struct io_ring_ctx *ctx = iowq->ctx; | |
6811 | ||
6812 | /* | |
d195a66e | 6813 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6814 | * started waiting. For timeouts, we always want to return to userspace, |
6815 | * regardless of event count. | |
6816 | */ | |
6c503150 | 6817 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6818 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6819 | } | |
6820 | ||
6821 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6822 | int wake_flags, void *key) | |
6823 | { | |
6824 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6825 | wq); | |
6826 | ||
6c503150 PB |
6827 | /* |
6828 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6829 | * the task, and the next invocation will do it. | |
6830 | */ | |
6831 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6832 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6833 | return -1; | |
bda52162 JA |
6834 | } |
6835 | ||
af9c1a44 JA |
6836 | static int io_run_task_work_sig(void) |
6837 | { | |
6838 | if (io_run_task_work()) | |
6839 | return 1; | |
6840 | if (!signal_pending(current)) | |
6841 | return 0; | |
792ee0f6 JA |
6842 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
6843 | return -ERESTARTSYS; | |
af9c1a44 JA |
6844 | return -EINTR; |
6845 | } | |
6846 | ||
eeb60b9a PB |
6847 | /* when returns >0, the caller should retry */ |
6848 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6849 | struct io_wait_queue *iowq, | |
6850 | signed long *timeout) | |
6851 | { | |
6852 | int ret; | |
6853 | ||
6854 | /* make sure we run task_work before checking for signals */ | |
6855 | ret = io_run_task_work_sig(); | |
6856 | if (ret || io_should_wake(iowq)) | |
6857 | return ret; | |
6858 | /* let the caller flush overflows, retry */ | |
6859 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6860 | return 1; | |
6861 | ||
6862 | *timeout = schedule_timeout(*timeout); | |
6863 | return !*timeout ? -ETIME : 1; | |
6864 | } | |
6865 | ||
2b188cc1 JA |
6866 | /* |
6867 | * Wait until events become available, if we don't already have some. The | |
6868 | * application must reap them itself, as they reside on the shared cq ring. | |
6869 | */ | |
6870 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
6871 | const sigset_t __user *sig, size_t sigsz, |
6872 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 6873 | { |
bda52162 JA |
6874 | struct io_wait_queue iowq = { |
6875 | .wq = { | |
6876 | .private = current, | |
6877 | .func = io_wake_function, | |
6878 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6879 | }, | |
6880 | .ctx = ctx, | |
6881 | .to_wait = min_events, | |
6882 | }; | |
75b28aff | 6883 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
6884 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
6885 | int ret; | |
2b188cc1 | 6886 | |
b41e9852 | 6887 | do { |
6c503150 PB |
6888 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6889 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 6890 | return 0; |
4c6e277c | 6891 | if (!io_run_task_work()) |
b41e9852 | 6892 | break; |
b41e9852 | 6893 | } while (1); |
2b188cc1 JA |
6894 | |
6895 | if (sig) { | |
9e75ad5d AB |
6896 | #ifdef CONFIG_COMPAT |
6897 | if (in_compat_syscall()) | |
6898 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6899 | sigsz); |
9e75ad5d AB |
6900 | else |
6901 | #endif | |
b772434b | 6902 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6903 | |
2b188cc1 JA |
6904 | if (ret) |
6905 | return ret; | |
6906 | } | |
6907 | ||
c73ebb68 | 6908 | if (uts) { |
c1d5a224 PB |
6909 | struct timespec64 ts; |
6910 | ||
c73ebb68 HX |
6911 | if (get_timespec64(&ts, uts)) |
6912 | return -EFAULT; | |
6913 | timeout = timespec64_to_jiffies(&ts); | |
6914 | } | |
6915 | ||
bda52162 | 6916 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6917 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 6918 | do { |
6c503150 | 6919 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
bda52162 JA |
6920 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
6921 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
6922 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
6923 | finish_wait(&ctx->wait, &iowq.wq); | |
6924 | } while (ret > 0); | |
bda52162 | 6925 | |
b7db41c9 | 6926 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6927 | |
75b28aff | 6928 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6929 | } |
6930 | ||
6b06314c JA |
6931 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6932 | { | |
6933 | #if defined(CONFIG_UNIX) | |
6934 | if (ctx->ring_sock) { | |
6935 | struct sock *sock = ctx->ring_sock->sk; | |
6936 | struct sk_buff *skb; | |
6937 | ||
6938 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6939 | kfree_skb(skb); | |
6940 | } | |
6941 | #else | |
6942 | int i; | |
6943 | ||
65e19f54 JA |
6944 | for (i = 0; i < ctx->nr_user_files; i++) { |
6945 | struct file *file; | |
6946 | ||
6947 | file = io_file_from_index(ctx, i); | |
6948 | if (file) | |
6949 | fput(file); | |
6950 | } | |
6b06314c JA |
6951 | #endif |
6952 | } | |
6953 | ||
00835dce | 6954 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 6955 | { |
269bbe5f | 6956 | struct fixed_rsrc_data *data; |
05f3fb3c | 6957 | |
269bbe5f | 6958 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
6959 | complete(&data->done); |
6960 | } | |
6961 | ||
2a63b2d9 | 6962 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 6963 | { |
2a63b2d9 | 6964 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
6965 | } |
6966 | ||
2a63b2d9 | 6967 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 6968 | { |
2a63b2d9 BM |
6969 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
6970 | } | |
65e19f54 | 6971 | |
d67d2263 BM |
6972 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
6973 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 6974 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 6975 | { |
2a63b2d9 | 6976 | io_rsrc_ref_lock(ctx); |
269bbe5f | 6977 | rsrc_data->node = ref_node; |
d67d2263 | 6978 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 6979 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 6980 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
6981 | } |
6982 | ||
8bad28d8 | 6983 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 6984 | { |
8bad28d8 | 6985 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 6986 | |
2a63b2d9 | 6987 | io_rsrc_ref_lock(ctx); |
1e5d770b | 6988 | ref_node = data->node; |
e6cb007c | 6989 | data->node = NULL; |
2a63b2d9 | 6990 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
6991 | if (ref_node) |
6992 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 | 6993 | } |
05589553 | 6994 | |
8bad28d8 HX |
6995 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
6996 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
6997 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
6998 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 6999 | { |
f2303b1f | 7000 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 7001 | int ret; |
05589553 | 7002 | |
8bad28d8 HX |
7003 | if (data->quiesce) |
7004 | return -ENXIO; | |
05589553 | 7005 | |
8bad28d8 | 7006 | data->quiesce = true; |
1ffc5422 | 7007 | do { |
f2303b1f PB |
7008 | ret = -ENOMEM; |
7009 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
7010 | if (!backup_node) | |
7011 | break; | |
7012 | backup_node->rsrc_data = data; | |
7013 | backup_node->rsrc_put = rsrc_put; | |
7014 | ||
8bad28d8 HX |
7015 | io_sqe_rsrc_kill_node(ctx, data); |
7016 | percpu_ref_kill(&data->refs); | |
7017 | flush_delayed_work(&ctx->rsrc_put_work); | |
7018 | ||
1ffc5422 | 7019 | ret = wait_for_completion_interruptible(&data->done); |
88f171ab | 7020 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
1ffc5422 | 7021 | break; |
8bad28d8 | 7022 | |
8bad28d8 HX |
7023 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
7024 | backup_node = NULL; | |
8bad28d8 | 7025 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7026 | ret = io_run_task_work_sig(); |
8bad28d8 | 7027 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7028 | } while (ret >= 0); |
8bad28d8 | 7029 | data->quiesce = false; |
05f3fb3c | 7030 | |
8bad28d8 HX |
7031 | if (backup_node) |
7032 | destroy_fixed_rsrc_ref_node(backup_node); | |
7033 | return ret; | |
d7954b2b BM |
7034 | } |
7035 | ||
1ad555c6 BM |
7036 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
7037 | { | |
7038 | struct fixed_rsrc_data *data; | |
7039 | ||
7040 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7041 | if (!data) | |
7042 | return NULL; | |
7043 | ||
00835dce | 7044 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7045 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7046 | kfree(data); | |
7047 | return NULL; | |
7048 | } | |
7049 | data->ctx = ctx; | |
7050 | init_completion(&data->done); | |
7051 | return data; | |
7052 | } | |
7053 | ||
7054 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7055 | { | |
7056 | percpu_ref_exit(&data->refs); | |
7057 | kfree(data->table); | |
7058 | kfree(data); | |
7059 | } | |
7060 | ||
d7954b2b BM |
7061 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7062 | { | |
7063 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7064 | unsigned nr_tables, i; |
7065 | int ret; | |
7066 | ||
8bad28d8 HX |
7067 | /* |
7068 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7069 | * Since we possibly drop uring lock later in this function to | |
7070 | * run task work. | |
7071 | */ | |
7072 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7073 | return -ENXIO; |
f2303b1f | 7074 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7075 | if (ret) |
7076 | return ret; | |
7077 | ||
6b06314c | 7078 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7079 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7080 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7081 | kfree(data->table[i].files); |
1ad555c6 | 7082 | free_fixed_rsrc_data(data); |
05f3fb3c | 7083 | ctx->file_data = NULL; |
6b06314c JA |
7084 | ctx->nr_user_files = 0; |
7085 | return 0; | |
7086 | } | |
7087 | ||
37d1e2e3 JA |
7088 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
7089 | __releases(&sqd->lock) | |
7090 | { | |
7091 | if (!sqd->thread) | |
7092 | return; | |
7093 | if (sqd->thread == current) | |
7094 | return; | |
7095 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7096 | wake_up_state(sqd->thread, TASK_PARKED); | |
7097 | mutex_unlock(&sqd->lock); | |
7098 | } | |
7099 | ||
7100 | static bool io_sq_thread_park(struct io_sq_data *sqd) | |
7101 | __acquires(&sqd->lock) | |
7102 | { | |
7103 | if (sqd->thread == current) | |
7104 | return true; | |
7105 | mutex_lock(&sqd->lock); | |
7106 | if (!sqd->thread) { | |
7107 | mutex_unlock(&sqd->lock); | |
7108 | return false; | |
7109 | } | |
7110 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7111 | wake_up_process(sqd->thread); | |
7112 | wait_for_completion(&sqd->completion); | |
7113 | return true; | |
7114 | } | |
7115 | ||
7116 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7117 | { | |
7118 | if (!sqd->thread) | |
7119 | return; | |
7120 | ||
7121 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
7122 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); | |
7123 | wake_up_process(sqd->thread); | |
7124 | wait_for_completion(&sqd->exited); | |
7125 | } | |
7126 | ||
534ca6d6 | 7127 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7128 | { |
534ca6d6 | 7129 | if (refcount_dec_and_test(&sqd->refs)) { |
37d1e2e3 JA |
7130 | io_sq_thread_stop(sqd); |
7131 | kfree(sqd); | |
7132 | } | |
7133 | } | |
7134 | ||
7135 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7136 | { | |
7137 | struct io_sq_data *sqd = ctx->sq_data; | |
7138 | ||
7139 | if (sqd) { | |
534ca6d6 | 7140 | if (sqd->thread) { |
37d1e2e3 JA |
7141 | wait_for_completion(&ctx->sq_thread_comp); |
7142 | io_sq_thread_park(sqd); | |
534ca6d6 JA |
7143 | } |
7144 | ||
37d1e2e3 JA |
7145 | mutex_lock(&sqd->ctx_lock); |
7146 | list_del(&ctx->sqd_list); | |
7147 | io_sqd_update_thread_idle(sqd); | |
7148 | mutex_unlock(&sqd->ctx_lock); | |
7149 | ||
7150 | if (sqd->thread) | |
7151 | io_sq_thread_unpark(sqd); | |
7152 | ||
7153 | io_put_sq_data(sqd); | |
7154 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7155 | } |
7156 | } | |
7157 | ||
aa06165d JA |
7158 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7159 | { | |
7160 | struct io_ring_ctx *ctx_attach; | |
7161 | struct io_sq_data *sqd; | |
7162 | struct fd f; | |
7163 | ||
7164 | f = fdget(p->wq_fd); | |
7165 | if (!f.file) | |
7166 | return ERR_PTR(-ENXIO); | |
7167 | if (f.file->f_op != &io_uring_fops) { | |
7168 | fdput(f); | |
7169 | return ERR_PTR(-EINVAL); | |
7170 | } | |
7171 | ||
7172 | ctx_attach = f.file->private_data; | |
7173 | sqd = ctx_attach->sq_data; | |
7174 | if (!sqd) { | |
7175 | fdput(f); | |
7176 | return ERR_PTR(-EINVAL); | |
7177 | } | |
7178 | ||
7179 | refcount_inc(&sqd->refs); | |
7180 | fdput(f); | |
7181 | return sqd; | |
7182 | } | |
7183 | ||
534ca6d6 JA |
7184 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7185 | { | |
7186 | struct io_sq_data *sqd; | |
7187 | ||
aa06165d JA |
7188 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7189 | return io_attach_sq_data(p); | |
7190 | ||
534ca6d6 JA |
7191 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7192 | if (!sqd) | |
7193 | return ERR_PTR(-ENOMEM); | |
7194 | ||
7195 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7196 | INIT_LIST_HEAD(&sqd->ctx_list); |
7197 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
7198 | mutex_init(&sqd->ctx_lock); | |
7199 | mutex_init(&sqd->lock); | |
534ca6d6 | 7200 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 JA |
7201 | init_completion(&sqd->startup); |
7202 | init_completion(&sqd->completion); | |
7203 | init_completion(&sqd->exited); | |
534ca6d6 JA |
7204 | return sqd; |
7205 | } | |
7206 | ||
6b06314c | 7207 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7208 | /* |
7209 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7210 | * the io_uring can be safely unregistered on process exit, even if we have | |
7211 | * loops in the file referencing. | |
7212 | */ | |
7213 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7214 | { | |
7215 | struct sock *sk = ctx->ring_sock->sk; | |
7216 | struct scm_fp_list *fpl; | |
7217 | struct sk_buff *skb; | |
08a45173 | 7218 | int i, nr_files; |
6b06314c | 7219 | |
6b06314c JA |
7220 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7221 | if (!fpl) | |
7222 | return -ENOMEM; | |
7223 | ||
7224 | skb = alloc_skb(0, GFP_KERNEL); | |
7225 | if (!skb) { | |
7226 | kfree(fpl); | |
7227 | return -ENOMEM; | |
7228 | } | |
7229 | ||
7230 | skb->sk = sk; | |
6b06314c | 7231 | |
08a45173 | 7232 | nr_files = 0; |
6b06314c JA |
7233 | fpl->user = get_uid(ctx->user); |
7234 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
7235 | struct file *file = io_file_from_index(ctx, i + offset); |
7236 | ||
7237 | if (!file) | |
08a45173 | 7238 | continue; |
65e19f54 | 7239 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7240 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7241 | nr_files++; | |
6b06314c JA |
7242 | } |
7243 | ||
08a45173 JA |
7244 | if (nr_files) { |
7245 | fpl->max = SCM_MAX_FD; | |
7246 | fpl->count = nr_files; | |
7247 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7248 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7249 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7250 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7251 | |
08a45173 JA |
7252 | for (i = 0; i < nr_files; i++) |
7253 | fput(fpl->fp[i]); | |
7254 | } else { | |
7255 | kfree_skb(skb); | |
7256 | kfree(fpl); | |
7257 | } | |
6b06314c JA |
7258 | |
7259 | return 0; | |
7260 | } | |
7261 | ||
7262 | /* | |
7263 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7264 | * causes regular reference counting to break down. We rely on the UNIX | |
7265 | * garbage collection to take care of this problem for us. | |
7266 | */ | |
7267 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7268 | { | |
7269 | unsigned left, total; | |
7270 | int ret = 0; | |
7271 | ||
7272 | total = 0; | |
7273 | left = ctx->nr_user_files; | |
7274 | while (left) { | |
7275 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7276 | |
7277 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7278 | if (ret) | |
7279 | break; | |
7280 | left -= this_files; | |
7281 | total += this_files; | |
7282 | } | |
7283 | ||
7284 | if (!ret) | |
7285 | return 0; | |
7286 | ||
7287 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7288 | struct file *file = io_file_from_index(ctx, total); |
7289 | ||
7290 | if (file) | |
7291 | fput(file); | |
6b06314c JA |
7292 | total++; |
7293 | } | |
7294 | ||
7295 | return ret; | |
7296 | } | |
7297 | #else | |
7298 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7299 | { | |
7300 | return 0; | |
7301 | } | |
7302 | #endif | |
7303 | ||
269bbe5f | 7304 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7305 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7306 | { |
7307 | int i; | |
7308 | ||
7309 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7310 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7311 | unsigned this_files; |
7312 | ||
7313 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7314 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7315 | GFP_KERNEL); | |
7316 | if (!table->files) | |
7317 | break; | |
7318 | nr_files -= this_files; | |
7319 | } | |
7320 | ||
7321 | if (i == nr_tables) | |
7322 | return 0; | |
7323 | ||
7324 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7325 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7326 | kfree(table->files); |
7327 | } | |
7328 | return 1; | |
7329 | } | |
7330 | ||
50238531 | 7331 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7332 | { |
50238531 | 7333 | struct file *file = prsrc->file; |
05f3fb3c JA |
7334 | #if defined(CONFIG_UNIX) |
7335 | struct sock *sock = ctx->ring_sock->sk; | |
7336 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7337 | struct sk_buff *skb; | |
7338 | int i; | |
7339 | ||
7340 | __skb_queue_head_init(&list); | |
7341 | ||
7342 | /* | |
7343 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7344 | * remove this entry and rearrange the file array. | |
7345 | */ | |
7346 | skb = skb_dequeue(head); | |
7347 | while (skb) { | |
7348 | struct scm_fp_list *fp; | |
7349 | ||
7350 | fp = UNIXCB(skb).fp; | |
7351 | for (i = 0; i < fp->count; i++) { | |
7352 | int left; | |
7353 | ||
7354 | if (fp->fp[i] != file) | |
7355 | continue; | |
7356 | ||
7357 | unix_notinflight(fp->user, fp->fp[i]); | |
7358 | left = fp->count - 1 - i; | |
7359 | if (left) { | |
7360 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7361 | left * sizeof(struct file *)); | |
7362 | } | |
7363 | fp->count--; | |
7364 | if (!fp->count) { | |
7365 | kfree_skb(skb); | |
7366 | skb = NULL; | |
7367 | } else { | |
7368 | __skb_queue_tail(&list, skb); | |
7369 | } | |
7370 | fput(file); | |
7371 | file = NULL; | |
7372 | break; | |
7373 | } | |
7374 | ||
7375 | if (!file) | |
7376 | break; | |
7377 | ||
7378 | __skb_queue_tail(&list, skb); | |
7379 | ||
7380 | skb = skb_dequeue(head); | |
7381 | } | |
7382 | ||
7383 | if (skb_peek(&list)) { | |
7384 | spin_lock_irq(&head->lock); | |
7385 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7386 | __skb_queue_tail(head, skb); | |
7387 | spin_unlock_irq(&head->lock); | |
7388 | } | |
7389 | #else | |
7390 | fput(file); | |
7391 | #endif | |
7392 | } | |
7393 | ||
269bbe5f | 7394 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7395 | { |
269bbe5f BM |
7396 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7397 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7398 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7399 | |
269bbe5f BM |
7400 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7401 | list_del(&prsrc->list); | |
50238531 | 7402 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7403 | kfree(prsrc); |
65e19f54 | 7404 | } |
05589553 | 7405 | |
05589553 XW |
7406 | percpu_ref_exit(&ref_node->refs); |
7407 | kfree(ref_node); | |
269bbe5f | 7408 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7409 | } |
65e19f54 | 7410 | |
269bbe5f | 7411 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7412 | { |
7413 | struct io_ring_ctx *ctx; | |
7414 | struct llist_node *node; | |
7415 | ||
269bbe5f BM |
7416 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7417 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7418 | |
7419 | while (node) { | |
269bbe5f | 7420 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7421 | struct llist_node *next = node->next; |
7422 | ||
269bbe5f BM |
7423 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7424 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7425 | node = next; |
7426 | } | |
7427 | } | |
7428 | ||
ea64ec02 PB |
7429 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7430 | unsigned i) | |
2faf852d | 7431 | { |
ea64ec02 PB |
7432 | struct fixed_rsrc_table *table; |
7433 | ||
7434 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7435 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7436 | } | |
7437 | ||
00835dce | 7438 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7439 | { |
269bbe5f BM |
7440 | struct fixed_rsrc_ref_node *ref_node; |
7441 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7442 | struct io_ring_ctx *ctx; |
e297822b | 7443 | bool first_add = false; |
4a38aed2 | 7444 | int delay = HZ; |
65e19f54 | 7445 | |
269bbe5f BM |
7446 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7447 | data = ref_node->rsrc_data; | |
e297822b PB |
7448 | ctx = data->ctx; |
7449 | ||
2a63b2d9 | 7450 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7451 | ref_node->done = true; |
7452 | ||
d67d2263 BM |
7453 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7454 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7455 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7456 | /* recycle ref nodes in order */ |
7457 | if (!ref_node->done) | |
7458 | break; | |
7459 | list_del(&ref_node->node); | |
269bbe5f | 7460 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7461 | } |
2a63b2d9 | 7462 | io_rsrc_ref_unlock(ctx); |
05589553 | 7463 | |
e297822b | 7464 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7465 | delay = 0; |
05589553 | 7466 | |
4a38aed2 | 7467 | if (!delay) |
269bbe5f | 7468 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7469 | else if (first_add) |
269bbe5f | 7470 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7471 | } |
65e19f54 | 7472 | |
6802535d | 7473 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7474 | struct io_ring_ctx *ctx) |
05f3fb3c | 7475 | { |
269bbe5f | 7476 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7477 | |
05589553 XW |
7478 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7479 | if (!ref_node) | |
3e2224c5 | 7480 | return NULL; |
05f3fb3c | 7481 | |
00835dce | 7482 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7483 | 0, GFP_KERNEL)) { |
7484 | kfree(ref_node); | |
3e2224c5 | 7485 | return NULL; |
05589553 XW |
7486 | } |
7487 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7488 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7489 | ref_node->done = false; |
05589553 | 7490 | return ref_node; |
05589553 XW |
7491 | } |
7492 | ||
bc9744cd PB |
7493 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7494 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7495 | { |
269bbe5f | 7496 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7497 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7498 | } |
7499 | ||
269bbe5f | 7500 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7501 | { |
7502 | percpu_ref_exit(&ref_node->refs); | |
7503 | kfree(ref_node); | |
65e19f54 JA |
7504 | } |
7505 | ||
ea64ec02 | 7506 | |
6b06314c JA |
7507 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7508 | unsigned nr_args) | |
7509 | { | |
7510 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7511 | unsigned nr_tables, i; |
05f3fb3c | 7512 | struct file *file; |
600cf3f8 | 7513 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7514 | struct fixed_rsrc_ref_node *ref_node; |
7515 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7516 | |
05f3fb3c | 7517 | if (ctx->file_data) |
6b06314c JA |
7518 | return -EBUSY; |
7519 | if (!nr_args) | |
7520 | return -EINVAL; | |
7521 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7522 | return -EMFILE; | |
7523 | ||
1ad555c6 | 7524 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7525 | if (!file_data) |
05f3fb3c | 7526 | return -ENOMEM; |
13770a71 | 7527 | ctx->file_data = file_data; |
05f3fb3c | 7528 | |
65e19f54 | 7529 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7530 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7531 | GFP_KERNEL); |
600cf3f8 PB |
7532 | if (!file_data->table) |
7533 | goto out_free; | |
05f3fb3c | 7534 | |
600cf3f8 | 7535 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7536 | goto out_free; |
65e19f54 | 7537 | |
08a45173 | 7538 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7539 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7540 | ret = -EFAULT; | |
7541 | goto out_fput; | |
7542 | } | |
08a45173 | 7543 | /* allow sparse sets */ |
600cf3f8 | 7544 | if (fd == -1) |
08a45173 | 7545 | continue; |
6b06314c | 7546 | |
05f3fb3c | 7547 | file = fget(fd); |
6b06314c | 7548 | ret = -EBADF; |
05f3fb3c | 7549 | if (!file) |
600cf3f8 | 7550 | goto out_fput; |
05f3fb3c | 7551 | |
6b06314c JA |
7552 | /* |
7553 | * Don't allow io_uring instances to be registered. If UNIX | |
7554 | * isn't enabled, then this causes a reference cycle and this | |
7555 | * instance can never get freed. If UNIX is enabled we'll | |
7556 | * handle it just fine, but there's still no point in allowing | |
7557 | * a ring fd as it doesn't support regular read/write anyway. | |
7558 | */ | |
05f3fb3c JA |
7559 | if (file->f_op == &io_uring_fops) { |
7560 | fput(file); | |
600cf3f8 | 7561 | goto out_fput; |
6b06314c | 7562 | } |
ea64ec02 | 7563 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7564 | } |
7565 | ||
6b06314c | 7566 | ret = io_sqe_files_scm(ctx); |
05589553 | 7567 | if (ret) { |
6b06314c | 7568 | io_sqe_files_unregister(ctx); |
05589553 XW |
7569 | return ret; |
7570 | } | |
6b06314c | 7571 | |
bc9744cd | 7572 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7573 | if (!ref_node) { |
05589553 | 7574 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7575 | return -ENOMEM; |
05589553 | 7576 | } |
bc9744cd | 7577 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7578 | |
d67d2263 | 7579 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7580 | return ret; |
600cf3f8 PB |
7581 | out_fput: |
7582 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7583 | file = io_file_from_index(ctx, i); | |
7584 | if (file) | |
7585 | fput(file); | |
7586 | } | |
7587 | for (i = 0; i < nr_tables; i++) | |
7588 | kfree(file_data->table[i].files); | |
7589 | ctx->nr_user_files = 0; | |
600cf3f8 | 7590 | out_free: |
1ad555c6 | 7591 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7592 | ctx->file_data = NULL; |
6b06314c JA |
7593 | return ret; |
7594 | } | |
7595 | ||
c3a31e60 JA |
7596 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7597 | int index) | |
7598 | { | |
7599 | #if defined(CONFIG_UNIX) | |
7600 | struct sock *sock = ctx->ring_sock->sk; | |
7601 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7602 | struct sk_buff *skb; | |
7603 | ||
7604 | /* | |
7605 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7606 | * file set. If there's no room, fall back to allocating a new skb | |
7607 | * and filling it in. | |
7608 | */ | |
7609 | spin_lock_irq(&head->lock); | |
7610 | skb = skb_peek(head); | |
7611 | if (skb) { | |
7612 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7613 | ||
7614 | if (fpl->count < SCM_MAX_FD) { | |
7615 | __skb_unlink(skb, head); | |
7616 | spin_unlock_irq(&head->lock); | |
7617 | fpl->fp[fpl->count] = get_file(file); | |
7618 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7619 | fpl->count++; | |
7620 | spin_lock_irq(&head->lock); | |
7621 | __skb_queue_head(head, skb); | |
7622 | } else { | |
7623 | skb = NULL; | |
7624 | } | |
7625 | } | |
7626 | spin_unlock_irq(&head->lock); | |
7627 | ||
7628 | if (skb) { | |
7629 | fput(file); | |
7630 | return 0; | |
7631 | } | |
7632 | ||
7633 | return __io_sqe_files_scm(ctx, 1, index); | |
7634 | #else | |
7635 | return 0; | |
7636 | #endif | |
7637 | } | |
7638 | ||
50238531 | 7639 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7640 | { |
269bbe5f BM |
7641 | struct io_rsrc_put *prsrc; |
7642 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7643 | |
269bbe5f BM |
7644 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7645 | if (!prsrc) | |
a5318d3c | 7646 | return -ENOMEM; |
05f3fb3c | 7647 | |
50238531 | 7648 | prsrc->rsrc = rsrc; |
269bbe5f | 7649 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7650 | |
a5318d3c | 7651 | return 0; |
05f3fb3c JA |
7652 | } |
7653 | ||
269bbe5f BM |
7654 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7655 | struct file *file) | |
7656 | { | |
50238531 | 7657 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7658 | } |
7659 | ||
05f3fb3c | 7660 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7661 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7662 | unsigned nr_args) |
7663 | { | |
269bbe5f BM |
7664 | struct fixed_rsrc_data *data = ctx->file_data; |
7665 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7666 | struct file *file, **file_slot; |
c3a31e60 JA |
7667 | __s32 __user *fds; |
7668 | int fd, i, err; | |
7669 | __u32 done; | |
05589553 | 7670 | bool needs_switch = false; |
c3a31e60 | 7671 | |
05f3fb3c | 7672 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7673 | return -EOVERFLOW; |
7674 | if (done > ctx->nr_user_files) | |
7675 | return -EINVAL; | |
7676 | ||
bc9744cd | 7677 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
7678 | if (!ref_node) |
7679 | return -ENOMEM; | |
bc9744cd | 7680 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7681 | |
269bbe5f | 7682 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7683 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7684 | err = 0; |
7685 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7686 | err = -EFAULT; | |
7687 | break; | |
7688 | } | |
4e0377a1 | 7689 | if (fd == IORING_REGISTER_FILES_SKIP) |
7690 | continue; | |
7691 | ||
67973b93 | 7692 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7693 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7694 | ||
7695 | if (*file_slot) { | |
7696 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
7697 | if (err) |
7698 | break; | |
ea64ec02 | 7699 | *file_slot = NULL; |
05589553 | 7700 | needs_switch = true; |
c3a31e60 JA |
7701 | } |
7702 | if (fd != -1) { | |
c3a31e60 JA |
7703 | file = fget(fd); |
7704 | if (!file) { | |
7705 | err = -EBADF; | |
7706 | break; | |
7707 | } | |
7708 | /* | |
7709 | * Don't allow io_uring instances to be registered. If | |
7710 | * UNIX isn't enabled, then this causes a reference | |
7711 | * cycle and this instance can never get freed. If UNIX | |
7712 | * is enabled we'll handle it just fine, but there's | |
7713 | * still no point in allowing a ring fd as it doesn't | |
7714 | * support regular read/write anyway. | |
7715 | */ | |
7716 | if (file->f_op == &io_uring_fops) { | |
7717 | fput(file); | |
7718 | err = -EBADF; | |
7719 | break; | |
7720 | } | |
e68a3ff8 | 7721 | *file_slot = file; |
c3a31e60 | 7722 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7723 | if (err) { |
e68a3ff8 | 7724 | *file_slot = NULL; |
f3bd9dae | 7725 | fput(file); |
c3a31e60 | 7726 | break; |
f3bd9dae | 7727 | } |
c3a31e60 | 7728 | } |
05f3fb3c JA |
7729 | } |
7730 | ||
05589553 | 7731 | if (needs_switch) { |
b2e96852 | 7732 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 7733 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 7734 | } else |
269bbe5f | 7735 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
7736 | |
7737 | return done ? done : err; | |
7738 | } | |
05589553 | 7739 | |
05f3fb3c JA |
7740 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7741 | unsigned nr_args) | |
7742 | { | |
269bbe5f | 7743 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7744 | |
7745 | if (!ctx->file_data) | |
7746 | return -ENXIO; | |
7747 | if (!nr_args) | |
7748 | return -EINVAL; | |
7749 | if (copy_from_user(&up, arg, sizeof(up))) | |
7750 | return -EFAULT; | |
7751 | if (up.resv) | |
7752 | return -EINVAL; | |
7753 | ||
7754 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7755 | } | |
c3a31e60 | 7756 | |
5280f7e5 | 7757 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7758 | { |
7759 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7760 | ||
5280f7e5 PB |
7761 | req = io_put_req_find_next(req); |
7762 | return req ? &req->work : NULL; | |
7d723065 JA |
7763 | } |
7764 | ||
5aa75ed5 | 7765 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
24369c2e PB |
7766 | { |
7767 | struct io_wq_data data; | |
24369c2e | 7768 | unsigned int concurrency; |
24369c2e | 7769 | |
e9fd9396 | 7770 | data.free_work = io_free_work; |
f5fa38c5 | 7771 | data.do_work = io_wq_submit_work; |
24369c2e | 7772 | |
d25e3a3d JA |
7773 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7774 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7775 | |
5aa75ed5 | 7776 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7777 | } |
7778 | ||
5aa75ed5 JA |
7779 | static int io_uring_alloc_task_context(struct task_struct *task, |
7780 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7781 | { |
7782 | struct io_uring_task *tctx; | |
d8a6df10 | 7783 | int ret; |
0f212204 JA |
7784 | |
7785 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7786 | if (unlikely(!tctx)) | |
7787 | return -ENOMEM; | |
7788 | ||
d8a6df10 JA |
7789 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7790 | if (unlikely(ret)) { | |
7791 | kfree(tctx); | |
7792 | return ret; | |
7793 | } | |
7794 | ||
5aa75ed5 JA |
7795 | tctx->io_wq = io_init_wq_offload(ctx); |
7796 | if (IS_ERR(tctx->io_wq)) { | |
7797 | ret = PTR_ERR(tctx->io_wq); | |
7798 | percpu_counter_destroy(&tctx->inflight); | |
7799 | kfree(tctx); | |
7800 | return ret; | |
7801 | } | |
7802 | ||
0f212204 JA |
7803 | xa_init(&tctx->xa); |
7804 | init_waitqueue_head(&tctx->wait); | |
7805 | tctx->last = NULL; | |
fdaf083c JA |
7806 | atomic_set(&tctx->in_idle, 0); |
7807 | tctx->sqpoll = false; | |
0f212204 | 7808 | task->io_uring = tctx; |
7cbf1722 JA |
7809 | spin_lock_init(&tctx->task_lock); |
7810 | INIT_WQ_LIST(&tctx->task_list); | |
7811 | tctx->task_state = 0; | |
7812 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7813 | return 0; |
7814 | } | |
7815 | ||
7816 | void __io_uring_free(struct task_struct *tsk) | |
7817 | { | |
7818 | struct io_uring_task *tctx = tsk->io_uring; | |
7819 | ||
7820 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
d8a6df10 | 7821 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7822 | kfree(tctx); |
7823 | tsk->io_uring = NULL; | |
7824 | } | |
7825 | ||
7e84e1c7 SG |
7826 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7827 | struct io_uring_params *p) | |
2b188cc1 JA |
7828 | { |
7829 | int ret; | |
7830 | ||
d25e3a3d JA |
7831 | /* Retain compatibility with failing for an invalid attach attempt */ |
7832 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7833 | IORING_SETUP_ATTACH_WQ) { | |
7834 | struct fd f; | |
7835 | ||
7836 | f = fdget(p->wq_fd); | |
7837 | if (!f.file) | |
7838 | return -ENXIO; | |
7839 | if (f.file->f_op != &io_uring_fops) { | |
7840 | fdput(f); | |
7841 | return -EINVAL; | |
7842 | } | |
7843 | fdput(f); | |
7844 | } | |
6c271ce2 | 7845 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
534ca6d6 JA |
7846 | struct io_sq_data *sqd; |
7847 | ||
3ec482d1 | 7848 | ret = -EPERM; |
ce59fc69 | 7849 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
7850 | goto err; |
7851 | ||
534ca6d6 JA |
7852 | sqd = io_get_sq_data(p); |
7853 | if (IS_ERR(sqd)) { | |
7854 | ret = PTR_ERR(sqd); | |
7855 | goto err; | |
7856 | } | |
69fb2131 | 7857 | |
534ca6d6 | 7858 | ctx->sq_data = sqd; |
69fb2131 JA |
7859 | io_sq_thread_park(sqd); |
7860 | mutex_lock(&sqd->ctx_lock); | |
7861 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); | |
7862 | mutex_unlock(&sqd->ctx_lock); | |
7863 | io_sq_thread_unpark(sqd); | |
534ca6d6 | 7864 | |
917257da JA |
7865 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7866 | if (!ctx->sq_thread_idle) | |
7867 | ctx->sq_thread_idle = HZ; | |
7868 | ||
aa06165d | 7869 | if (sqd->thread) |
5aa75ed5 | 7870 | return 0; |
aa06165d | 7871 | |
6c271ce2 | 7872 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7873 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7874 | |
917257da | 7875 | ret = -EINVAL; |
44a9bd18 JA |
7876 | if (cpu >= nr_cpu_ids) |
7877 | goto err; | |
7889f44d | 7878 | if (!cpu_online(cpu)) |
917257da JA |
7879 | goto err; |
7880 | ||
37d1e2e3 | 7881 | sqd->sq_cpu = cpu; |
6c271ce2 | 7882 | } else { |
37d1e2e3 | 7883 | sqd->sq_cpu = -1; |
6c271ce2 | 7884 | } |
37d1e2e3 JA |
7885 | |
7886 | sqd->task_pid = current->pid; | |
7887 | current->flags |= PF_IO_WORKER; | |
7888 | ret = io_wq_fork_thread(io_sq_thread, sqd); | |
7889 | current->flags &= ~PF_IO_WORKER; | |
7890 | if (ret < 0) { | |
534ca6d6 | 7891 | sqd->thread = NULL; |
6c271ce2 JA |
7892 | goto err; |
7893 | } | |
37d1e2e3 | 7894 | wait_for_completion(&sqd->completion); |
5aa75ed5 | 7895 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
0f212204 JA |
7896 | if (ret) |
7897 | goto err; | |
6c271ce2 JA |
7898 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7899 | /* Can't have SQ_AFF without SQPOLL */ | |
7900 | ret = -EINVAL; | |
7901 | goto err; | |
7902 | } | |
7903 | ||
2b188cc1 JA |
7904 | return 0; |
7905 | err: | |
37d1e2e3 | 7906 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
7907 | return ret; |
7908 | } | |
7909 | ||
7e84e1c7 SG |
7910 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7911 | { | |
534ca6d6 JA |
7912 | struct io_sq_data *sqd = ctx->sq_data; |
7913 | ||
7914 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) | |
37d1e2e3 | 7915 | complete(&sqd->startup); |
7e84e1c7 SG |
7916 | } |
7917 | ||
a087e2b5 BM |
7918 | static inline void __io_unaccount_mem(struct user_struct *user, |
7919 | unsigned long nr_pages) | |
2b188cc1 JA |
7920 | { |
7921 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7922 | } | |
7923 | ||
a087e2b5 BM |
7924 | static inline int __io_account_mem(struct user_struct *user, |
7925 | unsigned long nr_pages) | |
2b188cc1 JA |
7926 | { |
7927 | unsigned long page_limit, cur_pages, new_pages; | |
7928 | ||
7929 | /* Don't allow more pages than we can safely lock */ | |
7930 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7931 | ||
7932 | do { | |
7933 | cur_pages = atomic_long_read(&user->locked_vm); | |
7934 | new_pages = cur_pages + nr_pages; | |
7935 | if (new_pages > page_limit) | |
7936 | return -ENOMEM; | |
7937 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7938 | new_pages) != cur_pages); | |
7939 | ||
7940 | return 0; | |
7941 | } | |
7942 | ||
26bfa89e | 7943 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7944 | { |
aad5d8da | 7945 | if (ctx->limit_mem) |
a087e2b5 | 7946 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7947 | |
26bfa89e JA |
7948 | if (ctx->mm_account) |
7949 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7950 | } |
7951 | ||
26bfa89e | 7952 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7953 | { |
30975825 BM |
7954 | int ret; |
7955 | ||
7956 | if (ctx->limit_mem) { | |
7957 | ret = __io_account_mem(ctx->user, nr_pages); | |
7958 | if (ret) | |
7959 | return ret; | |
7960 | } | |
7961 | ||
26bfa89e JA |
7962 | if (ctx->mm_account) |
7963 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7964 | |
7965 | return 0; | |
7966 | } | |
7967 | ||
2b188cc1 JA |
7968 | static void io_mem_free(void *ptr) |
7969 | { | |
52e04ef4 MR |
7970 | struct page *page; |
7971 | ||
7972 | if (!ptr) | |
7973 | return; | |
2b188cc1 | 7974 | |
52e04ef4 | 7975 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7976 | if (put_page_testzero(page)) |
7977 | free_compound_page(page); | |
7978 | } | |
7979 | ||
7980 | static void *io_mem_alloc(size_t size) | |
7981 | { | |
7982 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 7983 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
7984 | |
7985 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
7986 | } | |
7987 | ||
75b28aff HV |
7988 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
7989 | size_t *sq_offset) | |
7990 | { | |
7991 | struct io_rings *rings; | |
7992 | size_t off, sq_array_size; | |
7993 | ||
7994 | off = struct_size(rings, cqes, cq_entries); | |
7995 | if (off == SIZE_MAX) | |
7996 | return SIZE_MAX; | |
7997 | ||
7998 | #ifdef CONFIG_SMP | |
7999 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8000 | if (off == 0) | |
8001 | return SIZE_MAX; | |
8002 | #endif | |
8003 | ||
b36200f5 DV |
8004 | if (sq_offset) |
8005 | *sq_offset = off; | |
8006 | ||
75b28aff HV |
8007 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8008 | if (sq_array_size == SIZE_MAX) | |
8009 | return SIZE_MAX; | |
8010 | ||
8011 | if (check_add_overflow(off, sq_array_size, &off)) | |
8012 | return SIZE_MAX; | |
8013 | ||
75b28aff HV |
8014 | return off; |
8015 | } | |
8016 | ||
0a96bbe4 | 8017 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8018 | { |
8019 | int i, j; | |
8020 | ||
8021 | if (!ctx->user_bufs) | |
8022 | return -ENXIO; | |
8023 | ||
8024 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8025 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8026 | ||
8027 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8028 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8029 | |
de293938 | 8030 | if (imu->acct_pages) |
26bfa89e | 8031 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8032 | kvfree(imu->bvec); |
edafccee JA |
8033 | imu->nr_bvecs = 0; |
8034 | } | |
8035 | ||
8036 | kfree(ctx->user_bufs); | |
8037 | ctx->user_bufs = NULL; | |
8038 | ctx->nr_user_bufs = 0; | |
8039 | return 0; | |
8040 | } | |
8041 | ||
8042 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8043 | void __user *arg, unsigned index) | |
8044 | { | |
8045 | struct iovec __user *src; | |
8046 | ||
8047 | #ifdef CONFIG_COMPAT | |
8048 | if (ctx->compat) { | |
8049 | struct compat_iovec __user *ciovs; | |
8050 | struct compat_iovec ciov; | |
8051 | ||
8052 | ciovs = (struct compat_iovec __user *) arg; | |
8053 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8054 | return -EFAULT; | |
8055 | ||
d55e5f5b | 8056 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8057 | dst->iov_len = ciov.iov_len; |
8058 | return 0; | |
8059 | } | |
8060 | #endif | |
8061 | src = (struct iovec __user *) arg; | |
8062 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8063 | return -EFAULT; | |
8064 | return 0; | |
8065 | } | |
8066 | ||
de293938 JA |
8067 | /* |
8068 | * Not super efficient, but this is just a registration time. And we do cache | |
8069 | * the last compound head, so generally we'll only do a full search if we don't | |
8070 | * match that one. | |
8071 | * | |
8072 | * We check if the given compound head page has already been accounted, to | |
8073 | * avoid double accounting it. This allows us to account the full size of the | |
8074 | * page, not just the constituent pages of a huge page. | |
8075 | */ | |
8076 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8077 | int nr_pages, struct page *hpage) | |
8078 | { | |
8079 | int i, j; | |
8080 | ||
8081 | /* check current page array */ | |
8082 | for (i = 0; i < nr_pages; i++) { | |
8083 | if (!PageCompound(pages[i])) | |
8084 | continue; | |
8085 | if (compound_head(pages[i]) == hpage) | |
8086 | return true; | |
8087 | } | |
8088 | ||
8089 | /* check previously registered pages */ | |
8090 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8091 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8092 | ||
8093 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8094 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8095 | continue; | |
8096 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8097 | return true; | |
8098 | } | |
8099 | } | |
8100 | ||
8101 | return false; | |
8102 | } | |
8103 | ||
8104 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8105 | int nr_pages, struct io_mapped_ubuf *imu, | |
8106 | struct page **last_hpage) | |
8107 | { | |
8108 | int i, ret; | |
8109 | ||
8110 | for (i = 0; i < nr_pages; i++) { | |
8111 | if (!PageCompound(pages[i])) { | |
8112 | imu->acct_pages++; | |
8113 | } else { | |
8114 | struct page *hpage; | |
8115 | ||
8116 | hpage = compound_head(pages[i]); | |
8117 | if (hpage == *last_hpage) | |
8118 | continue; | |
8119 | *last_hpage = hpage; | |
8120 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8121 | continue; | |
8122 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8123 | } | |
8124 | } | |
8125 | ||
8126 | if (!imu->acct_pages) | |
8127 | return 0; | |
8128 | ||
26bfa89e | 8129 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8130 | if (ret) |
8131 | imu->acct_pages = 0; | |
8132 | return ret; | |
8133 | } | |
8134 | ||
0a96bbe4 BM |
8135 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8136 | struct io_mapped_ubuf *imu, | |
8137 | struct page **last_hpage) | |
edafccee JA |
8138 | { |
8139 | struct vm_area_struct **vmas = NULL; | |
8140 | struct page **pages = NULL; | |
0a96bbe4 BM |
8141 | unsigned long off, start, end, ubuf; |
8142 | size_t size; | |
8143 | int ret, pret, nr_pages, i; | |
8144 | ||
8145 | ubuf = (unsigned long) iov->iov_base; | |
8146 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8147 | start = ubuf >> PAGE_SHIFT; | |
8148 | nr_pages = end - start; | |
8149 | ||
8150 | ret = -ENOMEM; | |
8151 | ||
8152 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8153 | if (!pages) | |
8154 | goto done; | |
8155 | ||
8156 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8157 | GFP_KERNEL); | |
8158 | if (!vmas) | |
8159 | goto done; | |
edafccee | 8160 | |
0a96bbe4 BM |
8161 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8162 | GFP_KERNEL); | |
8163 | if (!imu->bvec) | |
8164 | goto done; | |
8165 | ||
8166 | ret = 0; | |
8167 | mmap_read_lock(current->mm); | |
8168 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8169 | pages, vmas); | |
8170 | if (pret == nr_pages) { | |
8171 | /* don't support file backed memory */ | |
8172 | for (i = 0; i < nr_pages; i++) { | |
8173 | struct vm_area_struct *vma = vmas[i]; | |
8174 | ||
8175 | if (vma->vm_file && | |
8176 | !is_file_hugepages(vma->vm_file)) { | |
8177 | ret = -EOPNOTSUPP; | |
8178 | break; | |
8179 | } | |
8180 | } | |
8181 | } else { | |
8182 | ret = pret < 0 ? pret : -EFAULT; | |
8183 | } | |
8184 | mmap_read_unlock(current->mm); | |
8185 | if (ret) { | |
8186 | /* | |
8187 | * if we did partial map, or found file backed vmas, | |
8188 | * release any pages we did get | |
8189 | */ | |
8190 | if (pret > 0) | |
8191 | unpin_user_pages(pages, pret); | |
8192 | kvfree(imu->bvec); | |
8193 | goto done; | |
8194 | } | |
8195 | ||
8196 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8197 | if (ret) { | |
8198 | unpin_user_pages(pages, pret); | |
8199 | kvfree(imu->bvec); | |
8200 | goto done; | |
8201 | } | |
8202 | ||
8203 | off = ubuf & ~PAGE_MASK; | |
8204 | size = iov->iov_len; | |
8205 | for (i = 0; i < nr_pages; i++) { | |
8206 | size_t vec_len; | |
8207 | ||
8208 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8209 | imu->bvec[i].bv_page = pages[i]; | |
8210 | imu->bvec[i].bv_len = vec_len; | |
8211 | imu->bvec[i].bv_offset = off; | |
8212 | off = 0; | |
8213 | size -= vec_len; | |
8214 | } | |
8215 | /* store original address for later verification */ | |
8216 | imu->ubuf = ubuf; | |
8217 | imu->len = iov->iov_len; | |
8218 | imu->nr_bvecs = nr_pages; | |
8219 | ret = 0; | |
8220 | done: | |
8221 | kvfree(pages); | |
8222 | kvfree(vmas); | |
8223 | return ret; | |
8224 | } | |
8225 | ||
2b358604 | 8226 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8227 | { |
edafccee JA |
8228 | if (ctx->user_bufs) |
8229 | return -EBUSY; | |
8230 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8231 | return -EINVAL; | |
8232 | ||
8233 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8234 | GFP_KERNEL); | |
8235 | if (!ctx->user_bufs) | |
8236 | return -ENOMEM; | |
8237 | ||
2b358604 BM |
8238 | return 0; |
8239 | } | |
edafccee | 8240 | |
2b358604 BM |
8241 | static int io_buffer_validate(struct iovec *iov) |
8242 | { | |
8243 | /* | |
8244 | * Don't impose further limits on the size and buffer | |
8245 | * constraints here, we'll -EINVAL later when IO is | |
8246 | * submitted if they are wrong. | |
8247 | */ | |
8248 | if (!iov->iov_base || !iov->iov_len) | |
8249 | return -EFAULT; | |
edafccee | 8250 | |
2b358604 BM |
8251 | /* arbitrary limit, but we need something */ |
8252 | if (iov->iov_len > SZ_1G) | |
8253 | return -EFAULT; | |
edafccee | 8254 | |
2b358604 BM |
8255 | return 0; |
8256 | } | |
edafccee | 8257 | |
2b358604 BM |
8258 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8259 | unsigned int nr_args) | |
8260 | { | |
8261 | int i, ret; | |
8262 | struct iovec iov; | |
8263 | struct page *last_hpage = NULL; | |
edafccee | 8264 | |
2b358604 BM |
8265 | ret = io_buffers_map_alloc(ctx, nr_args); |
8266 | if (ret) | |
8267 | return ret; | |
edafccee | 8268 | |
edafccee JA |
8269 | for (i = 0; i < nr_args; i++) { |
8270 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8271 | |
edafccee JA |
8272 | ret = io_copy_iov(ctx, &iov, arg, i); |
8273 | if (ret) | |
0a96bbe4 | 8274 | break; |
de293938 | 8275 | |
2b358604 BM |
8276 | ret = io_buffer_validate(&iov); |
8277 | if (ret) | |
0a96bbe4 | 8278 | break; |
edafccee | 8279 | |
0a96bbe4 BM |
8280 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8281 | if (ret) | |
8282 | break; | |
edafccee JA |
8283 | |
8284 | ctx->nr_user_bufs++; | |
8285 | } | |
0a96bbe4 BM |
8286 | |
8287 | if (ret) | |
8288 | io_sqe_buffers_unregister(ctx); | |
8289 | ||
edafccee JA |
8290 | return ret; |
8291 | } | |
8292 | ||
9b402849 JA |
8293 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8294 | { | |
8295 | __s32 __user *fds = arg; | |
8296 | int fd; | |
8297 | ||
8298 | if (ctx->cq_ev_fd) | |
8299 | return -EBUSY; | |
8300 | ||
8301 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8302 | return -EFAULT; | |
8303 | ||
8304 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8305 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8306 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8307 | ctx->cq_ev_fd = NULL; | |
8308 | return ret; | |
8309 | } | |
8310 | ||
8311 | return 0; | |
8312 | } | |
8313 | ||
8314 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8315 | { | |
8316 | if (ctx->cq_ev_fd) { | |
8317 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8318 | ctx->cq_ev_fd = NULL; | |
8319 | return 0; | |
8320 | } | |
8321 | ||
8322 | return -ENXIO; | |
8323 | } | |
8324 | ||
5a2e745d JA |
8325 | static int __io_destroy_buffers(int id, void *p, void *data) |
8326 | { | |
8327 | struct io_ring_ctx *ctx = data; | |
8328 | struct io_buffer *buf = p; | |
8329 | ||
067524e9 | 8330 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8331 | return 0; |
8332 | } | |
8333 | ||
8334 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8335 | { | |
8336 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8337 | idr_destroy(&ctx->io_buffer_idr); | |
8338 | } | |
8339 | ||
68e68ee6 | 8340 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8341 | { |
68e68ee6 | 8342 | struct io_kiocb *req, *nxt; |
1b4c351f | 8343 | |
68e68ee6 JA |
8344 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8345 | if (tsk && req->task != tsk) | |
8346 | continue; | |
1b4c351f JA |
8347 | list_del(&req->compl.list); |
8348 | kmem_cache_free(req_cachep, req); | |
8349 | } | |
8350 | } | |
8351 | ||
9a4fdbd8 | 8352 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
2b188cc1 | 8353 | { |
bf019da7 PB |
8354 | struct io_submit_state *submit_state = &ctx->submit_state; |
8355 | ||
9a4fdbd8 JA |
8356 | mutex_lock(&ctx->uring_lock); |
8357 | ||
8358 | if (submit_state->free_reqs) | |
8359 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, | |
8360 | submit_state->reqs); | |
8361 | ||
8362 | io_req_cache_free(&submit_state->comp.free_list, NULL); | |
8363 | ||
8364 | spin_lock_irq(&ctx->completion_lock); | |
8365 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); | |
8366 | spin_unlock_irq(&ctx->completion_lock); | |
8367 | ||
8368 | mutex_unlock(&ctx->uring_lock); | |
8369 | } | |
8370 | ||
2b188cc1 JA |
8371 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8372 | { | |
04fc6c80 PB |
8373 | /* |
8374 | * Some may use context even when all refs and requests have been put, | |
8375 | * and they are free to do so while still holding uring_lock, see | |
8376 | * __io_req_task_submit(). Wait for them to finish. | |
8377 | */ | |
8378 | mutex_lock(&ctx->uring_lock); | |
8379 | mutex_unlock(&ctx->uring_lock); | |
8380 | ||
37d1e2e3 | 8381 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8382 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8383 | |
37d1e2e3 | 8384 | if (ctx->mm_account) { |
2aede0e4 JA |
8385 | mmdrop(ctx->mm_account); |
8386 | ctx->mm_account = NULL; | |
30975825 | 8387 | } |
def596e9 | 8388 | |
8bad28d8 | 8389 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8390 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8391 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8392 | io_eventfd_unregister(ctx); |
5a2e745d | 8393 | io_destroy_buffers(ctx); |
41726c9a | 8394 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8395 | |
2b188cc1 | 8396 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8397 | if (ctx->ring_sock) { |
8398 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8399 | sock_release(ctx->ring_sock); |
355e8d26 | 8400 | } |
2b188cc1 JA |
8401 | #endif |
8402 | ||
75b28aff | 8403 | io_mem_free(ctx->rings); |
2b188cc1 | 8404 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8405 | |
8406 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8407 | free_uid(ctx->user); |
9a4fdbd8 | 8408 | io_req_caches_free(ctx, NULL); |
78076bb6 | 8409 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8410 | kfree(ctx); |
8411 | } | |
8412 | ||
8413 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8414 | { | |
8415 | struct io_ring_ctx *ctx = file->private_data; | |
8416 | __poll_t mask = 0; | |
8417 | ||
8418 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8419 | /* |
8420 | * synchronizes with barrier from wq_has_sleeper call in | |
8421 | * io_commit_cqring | |
8422 | */ | |
2b188cc1 | 8423 | smp_rmb(); |
90554200 | 8424 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8425 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8426 | |
8427 | /* | |
8428 | * Don't flush cqring overflow list here, just do a simple check. | |
8429 | * Otherwise there could possible be ABBA deadlock: | |
8430 | * CPU0 CPU1 | |
8431 | * ---- ---- | |
8432 | * lock(&ctx->uring_lock); | |
8433 | * lock(&ep->mtx); | |
8434 | * lock(&ctx->uring_lock); | |
8435 | * lock(&ep->mtx); | |
8436 | * | |
8437 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8438 | * pushs them to do the flush. | |
8439 | */ | |
8440 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8441 | mask |= EPOLLIN | EPOLLRDNORM; |
8442 | ||
8443 | return mask; | |
8444 | } | |
8445 | ||
8446 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8447 | { | |
8448 | struct io_ring_ctx *ctx = file->private_data; | |
8449 | ||
8450 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8451 | } | |
8452 | ||
0bead8cd | 8453 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8454 | { |
4379bf8b | 8455 | const struct cred *creds; |
071698e1 | 8456 | |
4379bf8b JA |
8457 | creds = idr_remove(&ctx->personality_idr, id); |
8458 | if (creds) { | |
8459 | put_cred(creds); | |
0bead8cd | 8460 | return 0; |
1e6fa521 | 8461 | } |
0bead8cd YD |
8462 | |
8463 | return -EINVAL; | |
8464 | } | |
8465 | ||
8466 | static int io_remove_personalities(int id, void *p, void *data) | |
8467 | { | |
8468 | struct io_ring_ctx *ctx = data; | |
8469 | ||
8470 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8471 | return 0; |
8472 | } | |
8473 | ||
7c25c0d1 JA |
8474 | static void io_run_ctx_fallback(struct io_ring_ctx *ctx) |
8475 | { | |
8476 | struct callback_head *work, *head, *next; | |
8477 | ||
8478 | do { | |
8479 | do { | |
8480 | head = NULL; | |
8481 | work = READ_ONCE(ctx->exit_task_work); | |
8482 | } while (cmpxchg(&ctx->exit_task_work, work, head) != work); | |
8483 | ||
8484 | if (!work) | |
8485 | break; | |
8486 | ||
8487 | do { | |
8488 | next = work->next; | |
8489 | work->func(work); | |
8490 | work = next; | |
8491 | cond_resched(); | |
8492 | } while (work); | |
8493 | } while (1); | |
8494 | } | |
8495 | ||
85faa7b8 JA |
8496 | static void io_ring_exit_work(struct work_struct *work) |
8497 | { | |
b2edc0a7 PB |
8498 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
8499 | exit_work); | |
85faa7b8 | 8500 | |
56952e91 JA |
8501 | /* |
8502 | * If we're doing polled IO and end up having requests being | |
8503 | * submitted async (out-of-line), then completions can come in while | |
8504 | * we're waiting for refs to drop. We need to reap these manually, | |
8505 | * as nobody else will be looking for them. | |
8506 | */ | |
b2edc0a7 | 8507 | do { |
9936c7c2 | 8508 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
7c25c0d1 | 8509 | io_run_ctx_fallback(ctx); |
b2edc0a7 | 8510 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
85faa7b8 JA |
8511 | io_ring_ctx_free(ctx); |
8512 | } | |
8513 | ||
2b188cc1 JA |
8514 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8515 | { | |
8516 | mutex_lock(&ctx->uring_lock); | |
8517 | percpu_ref_kill(&ctx->refs); | |
d9d05217 PB |
8518 | |
8519 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) | |
8520 | ctx->sqo_dead = 1; | |
8521 | ||
cda286f0 PB |
8522 | /* if force is set, the ring is going away. always drop after that */ |
8523 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8524 | if (ctx->rings) |
6c503150 | 8525 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8526 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8527 | mutex_unlock(&ctx->uring_lock); |
8528 | ||
6b81928d PB |
8529 | io_kill_timeouts(ctx, NULL, NULL); |
8530 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8531 | |
15dff286 | 8532 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8533 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8534 | |
85faa7b8 | 8535 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8536 | /* |
8537 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8538 | * if we're exiting a ton of rings at the same time. It just adds | |
8539 | * noise and overhead, there's no discernable change in runtime | |
8540 | * over using system_wq. | |
8541 | */ | |
8542 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8543 | } |
8544 | ||
8545 | static int io_uring_release(struct inode *inode, struct file *file) | |
8546 | { | |
8547 | struct io_ring_ctx *ctx = file->private_data; | |
8548 | ||
8549 | file->private_data = NULL; | |
8550 | io_ring_ctx_wait_and_kill(ctx); | |
8551 | return 0; | |
8552 | } | |
8553 | ||
f6edbabb PB |
8554 | struct io_task_cancel { |
8555 | struct task_struct *task; | |
8556 | struct files_struct *files; | |
8557 | }; | |
f254ac04 | 8558 | |
f6edbabb | 8559 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8560 | { |
9a472ef7 | 8561 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8562 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8563 | bool ret; |
8564 | ||
f6edbabb | 8565 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8566 | unsigned long flags; |
8567 | struct io_ring_ctx *ctx = req->ctx; | |
8568 | ||
8569 | /* protect against races with linked timeouts */ | |
8570 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8571 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8572 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8573 | } else { | |
f6edbabb | 8574 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8575 | } |
8576 | return ret; | |
b711d4ea JA |
8577 | } |
8578 | ||
b7ddce3c | 8579 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8580 | struct task_struct *task, |
b7ddce3c PB |
8581 | struct files_struct *files) |
8582 | { | |
8583 | struct io_defer_entry *de = NULL; | |
8584 | LIST_HEAD(list); | |
8585 | ||
8586 | spin_lock_irq(&ctx->completion_lock); | |
8587 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8588 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8589 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8590 | break; | |
8591 | } | |
8592 | } | |
8593 | spin_unlock_irq(&ctx->completion_lock); | |
8594 | ||
8595 | while (!list_empty(&list)) { | |
8596 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8597 | list_del_init(&de->list); | |
8598 | req_set_fail_links(de->req); | |
8599 | io_put_req(de->req); | |
8600 | io_req_complete(de->req, -ECANCELED); | |
8601 | kfree(de); | |
8602 | } | |
8603 | } | |
8604 | ||
9936c7c2 PB |
8605 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8606 | struct task_struct *task, | |
8607 | struct files_struct *files) | |
8608 | { | |
8609 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
5aa75ed5 | 8610 | struct io_uring_task *tctx = current->io_uring; |
9936c7c2 PB |
8611 | |
8612 | while (1) { | |
8613 | enum io_wq_cancel cret; | |
8614 | bool ret = false; | |
8615 | ||
5aa75ed5 JA |
8616 | if (tctx && tctx->io_wq) { |
8617 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, | |
9936c7c2 PB |
8618 | &cancel, true); |
8619 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8620 | } | |
8621 | ||
8622 | /* SQPOLL thread does its own polling */ | |
8623 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8624 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8625 | io_iopoll_try_reap_events(ctx); | |
8626 | ret = true; | |
8627 | } | |
8628 | } | |
8629 | ||
8630 | ret |= io_poll_remove_all(ctx, task, files); | |
8631 | ret |= io_kill_timeouts(ctx, task, files); | |
8632 | ret |= io_run_task_work(); | |
8633 | io_cqring_overflow_flush(ctx, true, task, files); | |
8634 | if (!ret) | |
8635 | break; | |
8636 | cond_resched(); | |
8637 | } | |
8638 | } | |
8639 | ||
ca70f00b PB |
8640 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8641 | struct task_struct *task, | |
8642 | struct files_struct *files) | |
8643 | { | |
8644 | struct io_kiocb *req; | |
8645 | int cnt = 0; | |
8646 | ||
8647 | spin_lock_irq(&ctx->inflight_lock); | |
8648 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8649 | cnt += io_match_task(req, task, files); | |
8650 | spin_unlock_irq(&ctx->inflight_lock); | |
8651 | return cnt; | |
8652 | } | |
8653 | ||
b52fda00 | 8654 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8655 | struct task_struct *task, |
fcb323cc JA |
8656 | struct files_struct *files) |
8657 | { | |
fcb323cc | 8658 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 8659 | DEFINE_WAIT(wait); |
ca70f00b | 8660 | int inflight; |
fcb323cc | 8661 | |
ca70f00b PB |
8662 | inflight = io_uring_count_inflight(ctx, task, files); |
8663 | if (!inflight) | |
fcb323cc | 8664 | break; |
f6edbabb | 8665 | |
9936c7c2 | 8666 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 8667 | |
34343786 PB |
8668 | if (ctx->sq_data) |
8669 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
8670 | prepare_to_wait(&task->io_uring->wait, &wait, |
8671 | TASK_UNINTERRUPTIBLE); | |
8672 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8673 | schedule(); | |
c98de08c | 8674 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
8675 | if (ctx->sq_data) |
8676 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 8677 | } |
0f212204 JA |
8678 | } |
8679 | ||
d9d05217 PB |
8680 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
8681 | { | |
d9d05217 PB |
8682 | mutex_lock(&ctx->uring_lock); |
8683 | ctx->sqo_dead = 1; | |
8684 | mutex_unlock(&ctx->uring_lock); | |
8685 | ||
8686 | /* make sure callers enter the ring to get error */ | |
b4411616 PB |
8687 | if (ctx->rings) |
8688 | io_ring_set_wakeup_flag(ctx); | |
d9d05217 PB |
8689 | } |
8690 | ||
0f212204 JA |
8691 | /* |
8692 | * We need to iteratively cancel requests, in case a request has dependent | |
8693 | * hard links. These persist even for failure of cancelations, hence keep | |
8694 | * looping until none are found. | |
8695 | */ | |
8696 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8697 | struct files_struct *files) | |
8698 | { | |
8699 | struct task_struct *task = current; | |
37d1e2e3 | 8700 | bool did_park = false; |
0f212204 | 8701 | |
fdaf083c | 8702 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
d9d05217 | 8703 | io_disable_sqo_submit(ctx); |
37d1e2e3 JA |
8704 | did_park = io_sq_thread_park(ctx->sq_data); |
8705 | if (did_park) { | |
8706 | task = ctx->sq_data->thread; | |
8707 | atomic_inc(&task->io_uring->in_idle); | |
8708 | } | |
fdaf083c | 8709 | } |
0f212204 | 8710 | |
df9923f9 | 8711 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 8712 | |
3a7efd1a | 8713 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 8714 | if (!files) |
9936c7c2 | 8715 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c | 8716 | |
37d1e2e3 | 8717 | if (did_park) { |
fdaf083c | 8718 | atomic_dec(&task->io_uring->in_idle); |
fdaf083c JA |
8719 | io_sq_thread_unpark(ctx->sq_data); |
8720 | } | |
0f212204 JA |
8721 | } |
8722 | ||
8723 | /* | |
8724 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8725 | */ | |
fdaf083c | 8726 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
0f212204 | 8727 | { |
236434c3 | 8728 | struct io_uring_task *tctx = current->io_uring; |
a528b04e | 8729 | int ret; |
236434c3 MWO |
8730 | |
8731 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8732 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8733 | if (unlikely(ret)) |
8734 | return ret; | |
236434c3 | 8735 | tctx = current->io_uring; |
0f212204 | 8736 | } |
236434c3 MWO |
8737 | if (tctx->last != file) { |
8738 | void *old = xa_load(&tctx->xa, (unsigned long)file); | |
0f212204 | 8739 | |
236434c3 | 8740 | if (!old) { |
0f212204 | 8741 | get_file(file); |
a528b04e PB |
8742 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
8743 | file, GFP_KERNEL)); | |
8744 | if (ret) { | |
8745 | fput(file); | |
8746 | return ret; | |
8747 | } | |
ecfc8492 PB |
8748 | |
8749 | /* one and only SQPOLL file note, held by sqo_task */ | |
8750 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && | |
8751 | current != ctx->sqo_task); | |
0f212204 | 8752 | } |
236434c3 | 8753 | tctx->last = file; |
0f212204 JA |
8754 | } |
8755 | ||
fdaf083c JA |
8756 | /* |
8757 | * This is race safe in that the task itself is doing this, hence it | |
8758 | * cannot be going through the exit/cancel paths at the same time. | |
8759 | * This cannot be modified while exit/cancel is running. | |
8760 | */ | |
8761 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8762 | tctx->sqpoll = true; | |
8763 | ||
0f212204 JA |
8764 | return 0; |
8765 | } | |
8766 | ||
8767 | /* | |
8768 | * Remove this io_uring_file -> task mapping. | |
8769 | */ | |
8770 | static void io_uring_del_task_file(struct file *file) | |
8771 | { | |
8772 | struct io_uring_task *tctx = current->io_uring; | |
0f212204 JA |
8773 | |
8774 | if (tctx->last == file) | |
8775 | tctx->last = NULL; | |
5e2ed8c4 | 8776 | file = xa_erase(&tctx->xa, (unsigned long)file); |
0f212204 JA |
8777 | if (file) |
8778 | fput(file); | |
8779 | } | |
8780 | ||
de7f1d9e PB |
8781 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
8782 | { | |
8783 | struct file *file; | |
8784 | unsigned long index; | |
8785 | ||
8786 | xa_for_each(&tctx->xa, index, file) | |
8787 | io_uring_del_task_file(file); | |
8788 | } | |
8789 | ||
0f212204 JA |
8790 | void __io_uring_files_cancel(struct files_struct *files) |
8791 | { | |
8792 | struct io_uring_task *tctx = current->io_uring; | |
ce765372 MWO |
8793 | struct file *file; |
8794 | unsigned long index; | |
0f212204 JA |
8795 | |
8796 | /* make sure overflow events are dropped */ | |
fdaf083c | 8797 | atomic_inc(&tctx->in_idle); |
de7f1d9e PB |
8798 | xa_for_each(&tctx->xa, index, file) |
8799 | io_uring_cancel_task_requests(file->private_data, files); | |
fdaf083c | 8800 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8801 | |
5aa75ed5 | 8802 | if (files) { |
de7f1d9e | 8803 | io_uring_remove_task_files(tctx); |
5aa75ed5 JA |
8804 | } else if (tctx->io_wq && current->flags & PF_EXITING) { |
8805 | io_wq_destroy(tctx->io_wq); | |
8806 | tctx->io_wq = NULL; | |
8807 | } | |
fdaf083c JA |
8808 | } |
8809 | ||
8810 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8811 | { | |
0e9ddb39 PB |
8812 | return percpu_counter_sum(&tctx->inflight); |
8813 | } | |
fdaf083c | 8814 | |
0e9ddb39 PB |
8815 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8816 | { | |
37d1e2e3 | 8817 | struct io_sq_data *sqd = ctx->sq_data; |
0e9ddb39 PB |
8818 | struct io_uring_task *tctx; |
8819 | s64 inflight; | |
8820 | DEFINE_WAIT(wait); | |
fdaf083c | 8821 | |
37d1e2e3 | 8822 | if (!sqd) |
0e9ddb39 | 8823 | return; |
0e9ddb39 | 8824 | io_disable_sqo_submit(ctx); |
37d1e2e3 JA |
8825 | if (!io_sq_thread_park(sqd)) |
8826 | return; | |
8827 | tctx = ctx->sq_data->thread->io_uring; | |
fdaf083c | 8828 | |
0e9ddb39 PB |
8829 | atomic_inc(&tctx->in_idle); |
8830 | do { | |
8831 | /* read completions before cancelations */ | |
8832 | inflight = tctx_inflight(tctx); | |
8833 | if (!inflight) | |
8834 | break; | |
8835 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 8836 | |
0e9ddb39 PB |
8837 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
8838 | /* | |
8839 | * If we've seen completions, retry without waiting. This | |
8840 | * avoids a race where a completion comes in before we did | |
8841 | * prepare_to_wait(). | |
8842 | */ | |
8843 | if (inflight == tctx_inflight(tctx)) | |
8844 | schedule(); | |
8845 | finish_wait(&tctx->wait, &wait); | |
8846 | } while (1); | |
8847 | atomic_dec(&tctx->in_idle); | |
37d1e2e3 | 8848 | io_sq_thread_unpark(sqd); |
0f212204 JA |
8849 | } |
8850 | ||
0f212204 JA |
8851 | /* |
8852 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8853 | * requests. | |
8854 | */ | |
8855 | void __io_uring_task_cancel(void) | |
8856 | { | |
8857 | struct io_uring_task *tctx = current->io_uring; | |
8858 | DEFINE_WAIT(wait); | |
d8a6df10 | 8859 | s64 inflight; |
0f212204 JA |
8860 | |
8861 | /* make sure overflow events are dropped */ | |
fdaf083c | 8862 | atomic_inc(&tctx->in_idle); |
0f212204 | 8863 | |
0b5cd6c3 | 8864 | /* trigger io_disable_sqo_submit() */ |
0e9ddb39 PB |
8865 | if (tctx->sqpoll) { |
8866 | struct file *file; | |
8867 | unsigned long index; | |
8868 | ||
8869 | xa_for_each(&tctx->xa, index, file) | |
8870 | io_uring_cancel_sqpoll(file->private_data); | |
8871 | } | |
0b5cd6c3 | 8872 | |
d8a6df10 | 8873 | do { |
0f212204 | 8874 | /* read completions before cancelations */ |
fdaf083c | 8875 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
8876 | if (!inflight) |
8877 | break; | |
0f212204 JA |
8878 | __io_uring_files_cancel(NULL); |
8879 | ||
8880 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8881 | ||
8882 | /* | |
a1bb3cd5 PB |
8883 | * If we've seen completions, retry without waiting. This |
8884 | * avoids a race where a completion comes in before we did | |
8885 | * prepare_to_wait(). | |
0f212204 | 8886 | */ |
a1bb3cd5 PB |
8887 | if (inflight == tctx_inflight(tctx)) |
8888 | schedule(); | |
f57555ed | 8889 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 8890 | } while (1); |
0f212204 | 8891 | |
fdaf083c | 8892 | atomic_dec(&tctx->in_idle); |
de7f1d9e PB |
8893 | |
8894 | io_uring_remove_task_files(tctx); | |
44e728b8 PB |
8895 | } |
8896 | ||
fcb323cc JA |
8897 | static int io_uring_flush(struct file *file, void *data) |
8898 | { | |
6b5733eb | 8899 | struct io_uring_task *tctx = current->io_uring; |
d9d05217 | 8900 | struct io_ring_ctx *ctx = file->private_data; |
6b5733eb | 8901 | |
3bfe6106 JA |
8902 | /* Ignore helper thread files exit */ |
8903 | if (current->flags & PF_IO_WORKER) | |
8904 | return 0; | |
8905 | ||
41be53e9 | 8906 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
84965ff8 | 8907 | io_uring_cancel_task_requests(ctx, NULL); |
41be53e9 JA |
8908 | io_req_caches_free(ctx, current); |
8909 | } | |
84965ff8 | 8910 | |
7c25c0d1 JA |
8911 | io_run_ctx_fallback(ctx); |
8912 | ||
6b5733eb | 8913 | if (!tctx) |
4f793dc4 PB |
8914 | return 0; |
8915 | ||
6b5733eb PB |
8916 | /* we should have cancelled and erased it before PF_EXITING */ |
8917 | WARN_ON_ONCE((current->flags & PF_EXITING) && | |
8918 | xa_load(&tctx->xa, (unsigned long)file)); | |
8919 | ||
4f793dc4 PB |
8920 | /* |
8921 | * fput() is pending, will be 2 if the only other ref is our potential | |
8922 | * task file note. If the task is exiting, drop regardless of count. | |
8923 | */ | |
6b5733eb PB |
8924 | if (atomic_long_read(&file->f_count) != 2) |
8925 | return 0; | |
4f793dc4 | 8926 | |
d9d05217 PB |
8927 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
8928 | /* there is only one file note, which is owned by sqo_task */ | |
4325cb49 PB |
8929 | WARN_ON_ONCE(ctx->sqo_task != current && |
8930 | xa_load(&tctx->xa, (unsigned long)file)); | |
8931 | /* sqo_dead check is for when this happens after cancellation */ | |
8932 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && | |
d9d05217 PB |
8933 | !xa_load(&tctx->xa, (unsigned long)file)); |
8934 | ||
8935 | io_disable_sqo_submit(ctx); | |
8936 | } | |
8937 | ||
8938 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) | |
8939 | io_uring_del_task_file(file); | |
fcb323cc JA |
8940 | return 0; |
8941 | } | |
8942 | ||
6c5c240e RP |
8943 | static void *io_uring_validate_mmap_request(struct file *file, |
8944 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8945 | { |
2b188cc1 | 8946 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8947 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8948 | struct page *page; |
8949 | void *ptr; | |
8950 | ||
8951 | switch (offset) { | |
8952 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8953 | case IORING_OFF_CQ_RING: |
8954 | ptr = ctx->rings; | |
2b188cc1 JA |
8955 | break; |
8956 | case IORING_OFF_SQES: | |
8957 | ptr = ctx->sq_sqes; | |
8958 | break; | |
2b188cc1 | 8959 | default: |
6c5c240e | 8960 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8961 | } |
8962 | ||
8963 | page = virt_to_head_page(ptr); | |
a50b854e | 8964 | if (sz > page_size(page)) |
6c5c240e RP |
8965 | return ERR_PTR(-EINVAL); |
8966 | ||
8967 | return ptr; | |
8968 | } | |
8969 | ||
8970 | #ifdef CONFIG_MMU | |
8971 | ||
8972 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8973 | { | |
8974 | size_t sz = vma->vm_end - vma->vm_start; | |
8975 | unsigned long pfn; | |
8976 | void *ptr; | |
8977 | ||
8978 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8979 | if (IS_ERR(ptr)) | |
8980 | return PTR_ERR(ptr); | |
2b188cc1 JA |
8981 | |
8982 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
8983 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
8984 | } | |
8985 | ||
6c5c240e RP |
8986 | #else /* !CONFIG_MMU */ |
8987 | ||
8988 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8989 | { | |
8990 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
8991 | } | |
8992 | ||
8993 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
8994 | { | |
8995 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
8996 | } | |
8997 | ||
8998 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
8999 | unsigned long addr, unsigned long len, | |
9000 | unsigned long pgoff, unsigned long flags) | |
9001 | { | |
9002 | void *ptr; | |
9003 | ||
9004 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9005 | if (IS_ERR(ptr)) | |
9006 | return PTR_ERR(ptr); | |
9007 | ||
9008 | return (unsigned long) ptr; | |
9009 | } | |
9010 | ||
9011 | #endif /* !CONFIG_MMU */ | |
9012 | ||
d9d05217 | 9013 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9014 | { |
d9d05217 | 9015 | int ret = 0; |
90554200 JA |
9016 | DEFINE_WAIT(wait); |
9017 | ||
9018 | do { | |
9019 | if (!io_sqring_full(ctx)) | |
9020 | break; | |
9021 | ||
9022 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); | |
9023 | ||
d9d05217 PB |
9024 | if (unlikely(ctx->sqo_dead)) { |
9025 | ret = -EOWNERDEAD; | |
9026 | goto out; | |
9027 | } | |
9028 | ||
90554200 JA |
9029 | if (!io_sqring_full(ctx)) |
9030 | break; | |
9031 | ||
9032 | schedule(); | |
9033 | } while (!signal_pending(current)); | |
9034 | ||
9035 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 PB |
9036 | out: |
9037 | return ret; | |
90554200 JA |
9038 | } |
9039 | ||
c73ebb68 HX |
9040 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9041 | struct __kernel_timespec __user **ts, | |
9042 | const sigset_t __user **sig) | |
9043 | { | |
9044 | struct io_uring_getevents_arg arg; | |
9045 | ||
9046 | /* | |
9047 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9048 | * is just a pointer to the sigset_t. | |
9049 | */ | |
9050 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9051 | *sig = (const sigset_t __user *) argp; | |
9052 | *ts = NULL; | |
9053 | return 0; | |
9054 | } | |
9055 | ||
9056 | /* | |
9057 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9058 | * timespec and sigset_t pointers if good. | |
9059 | */ | |
9060 | if (*argsz != sizeof(arg)) | |
9061 | return -EINVAL; | |
9062 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9063 | return -EFAULT; | |
9064 | *sig = u64_to_user_ptr(arg.sigmask); | |
9065 | *argsz = arg.sigmask_sz; | |
9066 | *ts = u64_to_user_ptr(arg.ts); | |
9067 | return 0; | |
9068 | } | |
9069 | ||
2b188cc1 | 9070 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9071 | u32, min_complete, u32, flags, const void __user *, argp, |
9072 | size_t, argsz) | |
2b188cc1 JA |
9073 | { |
9074 | struct io_ring_ctx *ctx; | |
9075 | long ret = -EBADF; | |
9076 | int submitted = 0; | |
9077 | struct fd f; | |
9078 | ||
4c6e277c | 9079 | io_run_task_work(); |
b41e9852 | 9080 | |
90554200 | 9081 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9082 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9083 | return -EINVAL; |
9084 | ||
9085 | f = fdget(fd); | |
9086 | if (!f.file) | |
9087 | return -EBADF; | |
9088 | ||
9089 | ret = -EOPNOTSUPP; | |
9090 | if (f.file->f_op != &io_uring_fops) | |
9091 | goto out_fput; | |
9092 | ||
9093 | ret = -ENXIO; | |
9094 | ctx = f.file->private_data; | |
9095 | if (!percpu_ref_tryget(&ctx->refs)) | |
9096 | goto out_fput; | |
9097 | ||
7e84e1c7 SG |
9098 | ret = -EBADFD; |
9099 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9100 | goto out; | |
9101 | ||
6c271ce2 JA |
9102 | /* |
9103 | * For SQ polling, the thread will do all submissions and completions. | |
9104 | * Just return the requested submit count, and wake the thread if | |
9105 | * we were asked to. | |
9106 | */ | |
b2a9eada | 9107 | ret = 0; |
6c271ce2 | 9108 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9109 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9110 | |
d9d05217 PB |
9111 | ret = -EOWNERDEAD; |
9112 | if (unlikely(ctx->sqo_dead)) | |
9113 | goto out; | |
6c271ce2 | 9114 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9115 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9116 | if (flags & IORING_ENTER_SQ_WAIT) { |
9117 | ret = io_sqpoll_wait_sq(ctx); | |
9118 | if (ret) | |
9119 | goto out; | |
9120 | } | |
6c271ce2 | 9121 | submitted = to_submit; |
b2a9eada | 9122 | } else if (to_submit) { |
fdaf083c | 9123 | ret = io_uring_add_task_file(ctx, f.file); |
0f212204 JA |
9124 | if (unlikely(ret)) |
9125 | goto out; | |
2b188cc1 | 9126 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9127 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9128 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9129 | |
9130 | if (submitted != to_submit) | |
9131 | goto out; | |
2b188cc1 JA |
9132 | } |
9133 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9134 | const sigset_t __user *sig; |
9135 | struct __kernel_timespec __user *ts; | |
9136 | ||
9137 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9138 | if (unlikely(ret)) | |
9139 | goto out; | |
9140 | ||
2b188cc1 JA |
9141 | min_complete = min(min_complete, ctx->cq_entries); |
9142 | ||
32b2244a XW |
9143 | /* |
9144 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9145 | * space applications don't need to do io completion events | |
9146 | * polling again, they can rely on io_sq_thread to do polling | |
9147 | * work, which can reduce cpu usage and uring_lock contention. | |
9148 | */ | |
9149 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9150 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9151 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9152 | } else { |
c73ebb68 | 9153 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9154 | } |
2b188cc1 JA |
9155 | } |
9156 | ||
7c504e65 | 9157 | out: |
6805b32e | 9158 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9159 | out_fput: |
9160 | fdput(f); | |
9161 | return submitted ? submitted : ret; | |
9162 | } | |
9163 | ||
bebdb65e | 9164 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9165 | static int io_uring_show_cred(int id, void *p, void *data) |
9166 | { | |
4379bf8b | 9167 | const struct cred *cred = p; |
87ce955b JA |
9168 | struct seq_file *m = data; |
9169 | struct user_namespace *uns = seq_user_ns(m); | |
9170 | struct group_info *gi; | |
9171 | kernel_cap_t cap; | |
9172 | unsigned __capi; | |
9173 | int g; | |
9174 | ||
9175 | seq_printf(m, "%5d\n", id); | |
9176 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9177 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9178 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9179 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9180 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9181 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9182 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9183 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9184 | seq_puts(m, "\n\tGroups:\t"); | |
9185 | gi = cred->group_info; | |
9186 | for (g = 0; g < gi->ngroups; g++) { | |
9187 | seq_put_decimal_ull(m, g ? " " : "", | |
9188 | from_kgid_munged(uns, gi->gid[g])); | |
9189 | } | |
9190 | seq_puts(m, "\n\tCapEff:\t"); | |
9191 | cap = cred->cap_effective; | |
9192 | CAP_FOR_EACH_U32(__capi) | |
9193 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9194 | seq_putc(m, '\n'); | |
9195 | return 0; | |
9196 | } | |
9197 | ||
9198 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9199 | { | |
dbbe9c64 | 9200 | struct io_sq_data *sq = NULL; |
fad8e0de | 9201 | bool has_lock; |
87ce955b JA |
9202 | int i; |
9203 | ||
fad8e0de JA |
9204 | /* |
9205 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9206 | * since fdinfo case grabs it in the opposite direction of normal use | |
9207 | * cases. If we fail to get the lock, we just don't iterate any | |
9208 | * structures that could be going away outside the io_uring mutex. | |
9209 | */ | |
9210 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9211 | ||
dbbe9c64 JQ |
9212 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
9213 | sq = ctx->sq_data; | |
9214 | ||
9215 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9216 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9217 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9218 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9219 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9220 | |
87ce955b JA |
9221 | if (f) |
9222 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9223 | else | |
9224 | seq_printf(m, "%5u: <none>\n", i); | |
9225 | } | |
9226 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9227 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9228 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9229 | ||
9230 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9231 | (unsigned int) buf->len); | |
9232 | } | |
fad8e0de | 9233 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9234 | seq_printf(m, "Personalities:\n"); |
9235 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9236 | } | |
d7718a9d JA |
9237 | seq_printf(m, "PollList:\n"); |
9238 | spin_lock_irq(&ctx->completion_lock); | |
9239 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9240 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9241 | struct io_kiocb *req; | |
9242 | ||
9243 | hlist_for_each_entry(req, list, hash_node) | |
9244 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9245 | req->task->task_works != NULL); | |
9246 | } | |
9247 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9248 | if (has_lock) |
9249 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9250 | } |
9251 | ||
9252 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9253 | { | |
9254 | struct io_ring_ctx *ctx = f->private_data; | |
9255 | ||
9256 | if (percpu_ref_tryget(&ctx->refs)) { | |
9257 | __io_uring_show_fdinfo(ctx, m); | |
9258 | percpu_ref_put(&ctx->refs); | |
9259 | } | |
9260 | } | |
bebdb65e | 9261 | #endif |
87ce955b | 9262 | |
2b188cc1 JA |
9263 | static const struct file_operations io_uring_fops = { |
9264 | .release = io_uring_release, | |
fcb323cc | 9265 | .flush = io_uring_flush, |
2b188cc1 | 9266 | .mmap = io_uring_mmap, |
6c5c240e RP |
9267 | #ifndef CONFIG_MMU |
9268 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9269 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9270 | #endif | |
2b188cc1 JA |
9271 | .poll = io_uring_poll, |
9272 | .fasync = io_uring_fasync, | |
bebdb65e | 9273 | #ifdef CONFIG_PROC_FS |
87ce955b | 9274 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9275 | #endif |
2b188cc1 JA |
9276 | }; |
9277 | ||
9278 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9279 | struct io_uring_params *p) | |
9280 | { | |
75b28aff HV |
9281 | struct io_rings *rings; |
9282 | size_t size, sq_array_offset; | |
2b188cc1 | 9283 | |
bd740481 JA |
9284 | /* make sure these are sane, as we already accounted them */ |
9285 | ctx->sq_entries = p->sq_entries; | |
9286 | ctx->cq_entries = p->cq_entries; | |
9287 | ||
75b28aff HV |
9288 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9289 | if (size == SIZE_MAX) | |
9290 | return -EOVERFLOW; | |
9291 | ||
9292 | rings = io_mem_alloc(size); | |
9293 | if (!rings) | |
2b188cc1 JA |
9294 | return -ENOMEM; |
9295 | ||
75b28aff HV |
9296 | ctx->rings = rings; |
9297 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9298 | rings->sq_ring_mask = p->sq_entries - 1; | |
9299 | rings->cq_ring_mask = p->cq_entries - 1; | |
9300 | rings->sq_ring_entries = p->sq_entries; | |
9301 | rings->cq_ring_entries = p->cq_entries; | |
9302 | ctx->sq_mask = rings->sq_ring_mask; | |
9303 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9304 | |
9305 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9306 | if (size == SIZE_MAX) { |
9307 | io_mem_free(ctx->rings); | |
9308 | ctx->rings = NULL; | |
2b188cc1 | 9309 | return -EOVERFLOW; |
eb065d30 | 9310 | } |
2b188cc1 JA |
9311 | |
9312 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9313 | if (!ctx->sq_sqes) { |
9314 | io_mem_free(ctx->rings); | |
9315 | ctx->rings = NULL; | |
2b188cc1 | 9316 | return -ENOMEM; |
eb065d30 | 9317 | } |
2b188cc1 | 9318 | |
2b188cc1 JA |
9319 | return 0; |
9320 | } | |
9321 | ||
9faadcc8 PB |
9322 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9323 | { | |
9324 | int ret, fd; | |
9325 | ||
9326 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9327 | if (fd < 0) | |
9328 | return fd; | |
9329 | ||
9330 | ret = io_uring_add_task_file(ctx, file); | |
9331 | if (ret) { | |
9332 | put_unused_fd(fd); | |
9333 | return ret; | |
9334 | } | |
9335 | fd_install(fd, file); | |
9336 | return fd; | |
9337 | } | |
9338 | ||
2b188cc1 JA |
9339 | /* |
9340 | * Allocate an anonymous fd, this is what constitutes the application | |
9341 | * visible backing of an io_uring instance. The application mmaps this | |
9342 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9343 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9344 | */ | |
9faadcc8 | 9345 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9346 | { |
9347 | struct file *file; | |
9faadcc8 | 9348 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9349 | int ret; |
9350 | ||
2b188cc1 JA |
9351 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9352 | &ctx->ring_sock); | |
9353 | if (ret) | |
9faadcc8 | 9354 | return ERR_PTR(ret); |
2b188cc1 JA |
9355 | #endif |
9356 | ||
2b188cc1 JA |
9357 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9358 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9359 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9360 | if (IS_ERR(file)) { |
9361 | sock_release(ctx->ring_sock); | |
9362 | ctx->ring_sock = NULL; | |
9363 | } else { | |
9364 | ctx->ring_sock->file = file; | |
0f212204 | 9365 | } |
2b188cc1 | 9366 | #endif |
9faadcc8 | 9367 | return file; |
2b188cc1 JA |
9368 | } |
9369 | ||
7f13657d XW |
9370 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9371 | struct io_uring_params __user *params) | |
2b188cc1 JA |
9372 | { |
9373 | struct user_struct *user = NULL; | |
9374 | struct io_ring_ctx *ctx; | |
9faadcc8 | 9375 | struct file *file; |
2b188cc1 JA |
9376 | int ret; |
9377 | ||
8110c1a6 | 9378 | if (!entries) |
2b188cc1 | 9379 | return -EINVAL; |
8110c1a6 JA |
9380 | if (entries > IORING_MAX_ENTRIES) { |
9381 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9382 | return -EINVAL; | |
9383 | entries = IORING_MAX_ENTRIES; | |
9384 | } | |
2b188cc1 JA |
9385 | |
9386 | /* | |
9387 | * Use twice as many entries for the CQ ring. It's possible for the | |
9388 | * application to drive a higher depth than the size of the SQ ring, | |
9389 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9390 | * some flexibility in overcommitting a bit. If the application has |
9391 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9392 | * of CQ ring entries manually. | |
2b188cc1 JA |
9393 | */ |
9394 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9395 | if (p->flags & IORING_SETUP_CQSIZE) { |
9396 | /* | |
9397 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9398 | * to a power-of-two, if it isn't already. We do NOT impose | |
9399 | * any cq vs sq ring sizing. | |
9400 | */ | |
eb2667b3 | 9401 | if (!p->cq_entries) |
33a107f0 | 9402 | return -EINVAL; |
8110c1a6 JA |
9403 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9404 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9405 | return -EINVAL; | |
9406 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9407 | } | |
eb2667b3 JQ |
9408 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9409 | if (p->cq_entries < p->sq_entries) | |
9410 | return -EINVAL; | |
33a107f0 JA |
9411 | } else { |
9412 | p->cq_entries = 2 * p->sq_entries; | |
9413 | } | |
2b188cc1 JA |
9414 | |
9415 | user = get_uid(current_user()); | |
2b188cc1 JA |
9416 | |
9417 | ctx = io_ring_ctx_alloc(p); | |
9418 | if (!ctx) { | |
2b188cc1 JA |
9419 | free_uid(user); |
9420 | return -ENOMEM; | |
9421 | } | |
9422 | ctx->compat = in_compat_syscall(); | |
26bfa89e | 9423 | ctx->limit_mem = !capable(CAP_IPC_LOCK); |
2b188cc1 | 9424 | ctx->user = user; |
37d1e2e3 | 9425 | ctx->sqo_task = current; |
2aede0e4 JA |
9426 | |
9427 | /* | |
9428 | * This is just grabbed for accounting purposes. When a process exits, | |
9429 | * the mm is exited and dropped before the files, hence we need to hang | |
9430 | * on to this mm purely for the purposes of being able to unaccount | |
9431 | * memory (locked/pinned vm). It's not used for anything else. | |
9432 | */ | |
6b7898eb | 9433 | mmgrab(current->mm); |
2aede0e4 | 9434 | ctx->mm_account = current->mm; |
6b7898eb | 9435 | |
2b188cc1 JA |
9436 | ret = io_allocate_scq_urings(ctx, p); |
9437 | if (ret) | |
9438 | goto err; | |
9439 | ||
7e84e1c7 | 9440 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9441 | if (ret) |
9442 | goto err; | |
9443 | ||
7e84e1c7 SG |
9444 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9445 | io_sq_offload_start(ctx); | |
9446 | ||
2b188cc1 | 9447 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9448 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9449 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9450 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9451 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9452 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9453 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9454 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9455 | |
9456 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9457 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9458 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9459 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9460 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9461 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9462 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9463 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9464 | |
7f13657d XW |
9465 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9466 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9467 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9468 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9469 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9470 | |
9471 | if (copy_to_user(params, p, sizeof(*p))) { | |
9472 | ret = -EFAULT; | |
9473 | goto err; | |
9474 | } | |
d1719f70 | 9475 | |
9faadcc8 PB |
9476 | file = io_uring_get_file(ctx); |
9477 | if (IS_ERR(file)) { | |
9478 | ret = PTR_ERR(file); | |
9479 | goto err; | |
9480 | } | |
9481 | ||
044c1ab3 JA |
9482 | /* |
9483 | * Install ring fd as the very last thing, so we don't risk someone | |
9484 | * having closed it before we finish setup | |
9485 | */ | |
9faadcc8 PB |
9486 | ret = io_uring_install_fd(ctx, file); |
9487 | if (ret < 0) { | |
06585c49 | 9488 | io_disable_sqo_submit(ctx); |
9faadcc8 PB |
9489 | /* fput will clean it up */ |
9490 | fput(file); | |
9491 | return ret; | |
9492 | } | |
044c1ab3 | 9493 | |
c826bd7a | 9494 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9495 | return ret; |
9496 | err: | |
d9d05217 | 9497 | io_disable_sqo_submit(ctx); |
2b188cc1 JA |
9498 | io_ring_ctx_wait_and_kill(ctx); |
9499 | return ret; | |
9500 | } | |
9501 | ||
9502 | /* | |
9503 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9504 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9505 | * params structure passed in. | |
9506 | */ | |
9507 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9508 | { | |
9509 | struct io_uring_params p; | |
2b188cc1 JA |
9510 | int i; |
9511 | ||
9512 | if (copy_from_user(&p, params, sizeof(p))) | |
9513 | return -EFAULT; | |
9514 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9515 | if (p.resv[i]) | |
9516 | return -EINVAL; | |
9517 | } | |
9518 | ||
6c271ce2 | 9519 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9520 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9521 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9522 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9523 | return -EINVAL; |
9524 | ||
7f13657d | 9525 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9526 | } |
9527 | ||
9528 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9529 | struct io_uring_params __user *, params) | |
9530 | { | |
9531 | return io_uring_setup(entries, params); | |
9532 | } | |
9533 | ||
66f4af93 JA |
9534 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9535 | { | |
9536 | struct io_uring_probe *p; | |
9537 | size_t size; | |
9538 | int i, ret; | |
9539 | ||
9540 | size = struct_size(p, ops, nr_args); | |
9541 | if (size == SIZE_MAX) | |
9542 | return -EOVERFLOW; | |
9543 | p = kzalloc(size, GFP_KERNEL); | |
9544 | if (!p) | |
9545 | return -ENOMEM; | |
9546 | ||
9547 | ret = -EFAULT; | |
9548 | if (copy_from_user(p, arg, size)) | |
9549 | goto out; | |
9550 | ret = -EINVAL; | |
9551 | if (memchr_inv(p, 0, size)) | |
9552 | goto out; | |
9553 | ||
9554 | p->last_op = IORING_OP_LAST - 1; | |
9555 | if (nr_args > IORING_OP_LAST) | |
9556 | nr_args = IORING_OP_LAST; | |
9557 | ||
9558 | for (i = 0; i < nr_args; i++) { | |
9559 | p->ops[i].op = i; | |
9560 | if (!io_op_defs[i].not_supported) | |
9561 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9562 | } | |
9563 | p->ops_len = i; | |
9564 | ||
9565 | ret = 0; | |
9566 | if (copy_to_user(arg, p, size)) | |
9567 | ret = -EFAULT; | |
9568 | out: | |
9569 | kfree(p); | |
9570 | return ret; | |
9571 | } | |
9572 | ||
071698e1 JA |
9573 | static int io_register_personality(struct io_ring_ctx *ctx) |
9574 | { | |
4379bf8b | 9575 | const struct cred *creds; |
1e6fa521 | 9576 | int ret; |
071698e1 | 9577 | |
4379bf8b | 9578 | creds = get_current_cred(); |
1e6fa521 | 9579 | |
4379bf8b JA |
9580 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
9581 | USHRT_MAX, GFP_KERNEL); | |
9582 | if (ret < 0) | |
9583 | put_cred(creds); | |
1e6fa521 | 9584 | return ret; |
071698e1 JA |
9585 | } |
9586 | ||
21b55dbc SG |
9587 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9588 | unsigned int nr_args) | |
9589 | { | |
9590 | struct io_uring_restriction *res; | |
9591 | size_t size; | |
9592 | int i, ret; | |
9593 | ||
7e84e1c7 SG |
9594 | /* Restrictions allowed only if rings started disabled */ |
9595 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9596 | return -EBADFD; | |
9597 | ||
21b55dbc | 9598 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9599 | if (ctx->restrictions.registered) |
21b55dbc SG |
9600 | return -EBUSY; |
9601 | ||
9602 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9603 | return -EINVAL; | |
9604 | ||
9605 | size = array_size(nr_args, sizeof(*res)); | |
9606 | if (size == SIZE_MAX) | |
9607 | return -EOVERFLOW; | |
9608 | ||
9609 | res = memdup_user(arg, size); | |
9610 | if (IS_ERR(res)) | |
9611 | return PTR_ERR(res); | |
9612 | ||
9613 | ret = 0; | |
9614 | ||
9615 | for (i = 0; i < nr_args; i++) { | |
9616 | switch (res[i].opcode) { | |
9617 | case IORING_RESTRICTION_REGISTER_OP: | |
9618 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9619 | ret = -EINVAL; | |
9620 | goto out; | |
9621 | } | |
9622 | ||
9623 | __set_bit(res[i].register_op, | |
9624 | ctx->restrictions.register_op); | |
9625 | break; | |
9626 | case IORING_RESTRICTION_SQE_OP: | |
9627 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9628 | ret = -EINVAL; | |
9629 | goto out; | |
9630 | } | |
9631 | ||
9632 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9633 | break; | |
9634 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9635 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9636 | break; | |
9637 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9638 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9639 | break; | |
9640 | default: | |
9641 | ret = -EINVAL; | |
9642 | goto out; | |
9643 | } | |
9644 | } | |
9645 | ||
9646 | out: | |
9647 | /* Reset all restrictions if an error happened */ | |
9648 | if (ret != 0) | |
9649 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9650 | else | |
7e84e1c7 | 9651 | ctx->restrictions.registered = true; |
21b55dbc SG |
9652 | |
9653 | kfree(res); | |
9654 | return ret; | |
9655 | } | |
9656 | ||
7e84e1c7 SG |
9657 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9658 | { | |
9659 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9660 | return -EBADFD; | |
9661 | ||
9662 | if (ctx->restrictions.registered) | |
9663 | ctx->restricted = 1; | |
9664 | ||
9665 | ctx->flags &= ~IORING_SETUP_R_DISABLED; | |
9666 | ||
9667 | io_sq_offload_start(ctx); | |
9668 | ||
9669 | return 0; | |
9670 | } | |
9671 | ||
071698e1 JA |
9672 | static bool io_register_op_must_quiesce(int op) |
9673 | { | |
9674 | switch (op) { | |
9675 | case IORING_UNREGISTER_FILES: | |
9676 | case IORING_REGISTER_FILES_UPDATE: | |
9677 | case IORING_REGISTER_PROBE: | |
9678 | case IORING_REGISTER_PERSONALITY: | |
9679 | case IORING_UNREGISTER_PERSONALITY: | |
9680 | return false; | |
9681 | default: | |
9682 | return true; | |
9683 | } | |
9684 | } | |
9685 | ||
edafccee JA |
9686 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9687 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9688 | __releases(ctx->uring_lock) |
9689 | __acquires(ctx->uring_lock) | |
edafccee JA |
9690 | { |
9691 | int ret; | |
9692 | ||
35fa71a0 JA |
9693 | /* |
9694 | * We're inside the ring mutex, if the ref is already dying, then | |
9695 | * someone else killed the ctx or is already going through | |
9696 | * io_uring_register(). | |
9697 | */ | |
9698 | if (percpu_ref_is_dying(&ctx->refs)) | |
9699 | return -ENXIO; | |
9700 | ||
071698e1 | 9701 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9702 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9703 | |
05f3fb3c JA |
9704 | /* |
9705 | * Drop uring mutex before waiting for references to exit. If | |
9706 | * another thread is currently inside io_uring_enter() it might | |
9707 | * need to grab the uring_lock to make progress. If we hold it | |
9708 | * here across the drain wait, then we can deadlock. It's safe | |
9709 | * to drop the mutex here, since no new references will come in | |
9710 | * after we've killed the percpu ref. | |
9711 | */ | |
9712 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9713 | do { |
9714 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9715 | if (!ret) | |
9716 | break; | |
ed6930c9 JA |
9717 | ret = io_run_task_work_sig(); |
9718 | if (ret < 0) | |
9719 | break; | |
af9c1a44 JA |
9720 | } while (1); |
9721 | ||
05f3fb3c | 9722 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9723 | |
88f171ab PB |
9724 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
9725 | return ret; | |
21b55dbc SG |
9726 | } |
9727 | ||
9728 | if (ctx->restricted) { | |
9729 | if (opcode >= IORING_REGISTER_LAST) { | |
9730 | ret = -EINVAL; | |
9731 | goto out; | |
9732 | } | |
9733 | ||
9734 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9735 | ret = -EACCES; | |
c150368b JA |
9736 | goto out; |
9737 | } | |
05f3fb3c | 9738 | } |
edafccee JA |
9739 | |
9740 | switch (opcode) { | |
9741 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9742 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9743 | break; |
9744 | case IORING_UNREGISTER_BUFFERS: | |
9745 | ret = -EINVAL; | |
9746 | if (arg || nr_args) | |
9747 | break; | |
0a96bbe4 | 9748 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9749 | break; |
6b06314c JA |
9750 | case IORING_REGISTER_FILES: |
9751 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9752 | break; | |
9753 | case IORING_UNREGISTER_FILES: | |
9754 | ret = -EINVAL; | |
9755 | if (arg || nr_args) | |
9756 | break; | |
9757 | ret = io_sqe_files_unregister(ctx); | |
9758 | break; | |
c3a31e60 JA |
9759 | case IORING_REGISTER_FILES_UPDATE: |
9760 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9761 | break; | |
9b402849 | 9762 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9763 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9764 | ret = -EINVAL; |
9765 | if (nr_args != 1) | |
9766 | break; | |
9767 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9768 | if (ret) |
9769 | break; | |
9770 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9771 | ctx->eventfd_async = 1; | |
9772 | else | |
9773 | ctx->eventfd_async = 0; | |
9b402849 JA |
9774 | break; |
9775 | case IORING_UNREGISTER_EVENTFD: | |
9776 | ret = -EINVAL; | |
9777 | if (arg || nr_args) | |
9778 | break; | |
9779 | ret = io_eventfd_unregister(ctx); | |
9780 | break; | |
66f4af93 JA |
9781 | case IORING_REGISTER_PROBE: |
9782 | ret = -EINVAL; | |
9783 | if (!arg || nr_args > 256) | |
9784 | break; | |
9785 | ret = io_probe(ctx, arg, nr_args); | |
9786 | break; | |
071698e1 JA |
9787 | case IORING_REGISTER_PERSONALITY: |
9788 | ret = -EINVAL; | |
9789 | if (arg || nr_args) | |
9790 | break; | |
9791 | ret = io_register_personality(ctx); | |
9792 | break; | |
9793 | case IORING_UNREGISTER_PERSONALITY: | |
9794 | ret = -EINVAL; | |
9795 | if (arg) | |
9796 | break; | |
9797 | ret = io_unregister_personality(ctx, nr_args); | |
9798 | break; | |
7e84e1c7 SG |
9799 | case IORING_REGISTER_ENABLE_RINGS: |
9800 | ret = -EINVAL; | |
9801 | if (arg || nr_args) | |
9802 | break; | |
9803 | ret = io_register_enable_rings(ctx); | |
9804 | break; | |
21b55dbc SG |
9805 | case IORING_REGISTER_RESTRICTIONS: |
9806 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9807 | break; | |
edafccee JA |
9808 | default: |
9809 | ret = -EINVAL; | |
9810 | break; | |
9811 | } | |
9812 | ||
21b55dbc | 9813 | out: |
071698e1 | 9814 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9815 | /* bring the ctx back to life */ |
05f3fb3c | 9816 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 9817 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9818 | } |
edafccee JA |
9819 | return ret; |
9820 | } | |
9821 | ||
9822 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9823 | void __user *, arg, unsigned int, nr_args) | |
9824 | { | |
9825 | struct io_ring_ctx *ctx; | |
9826 | long ret = -EBADF; | |
9827 | struct fd f; | |
9828 | ||
9829 | f = fdget(fd); | |
9830 | if (!f.file) | |
9831 | return -EBADF; | |
9832 | ||
9833 | ret = -EOPNOTSUPP; | |
9834 | if (f.file->f_op != &io_uring_fops) | |
9835 | goto out_fput; | |
9836 | ||
9837 | ctx = f.file->private_data; | |
9838 | ||
b6c23dd5 PB |
9839 | io_run_task_work(); |
9840 | ||
edafccee JA |
9841 | mutex_lock(&ctx->uring_lock); |
9842 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9843 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9844 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9845 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9846 | out_fput: |
9847 | fdput(f); | |
9848 | return ret; | |
9849 | } | |
9850 | ||
2b188cc1 JA |
9851 | static int __init io_uring_init(void) |
9852 | { | |
d7f62e82 SM |
9853 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9854 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9855 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9856 | } while (0) | |
9857 | ||
9858 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9859 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9860 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9861 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9862 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9863 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9864 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9865 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9866 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9867 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9868 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9869 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9870 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9871 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9872 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9873 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9874 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9875 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9876 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9877 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9878 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9879 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9880 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9881 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9882 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9883 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9884 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9885 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9886 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9887 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9888 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9889 | |
d3656344 | 9890 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9891 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
9892 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
9893 | SLAB_ACCOUNT); | |
2b188cc1 JA |
9894 | return 0; |
9895 | }; | |
9896 | __initcall(io_uring_init); |