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> |
7d67af2c | 77 | #include <linux/splice.h> |
b41e9852 | 78 | #include <linux/task_work.h> |
bcf5a063 | 79 | #include <linux/pagemap.h> |
0f212204 | 80 | #include <linux/io_uring.h> |
e4b4a13f | 81 | #include <linux/freezer.h> |
2b188cc1 | 82 | |
c826bd7a DD |
83 | #define CREATE_TRACE_POINTS |
84 | #include <trace/events/io_uring.h> | |
85 | ||
2b188cc1 JA |
86 | #include <uapi/linux/io_uring.h> |
87 | ||
88 | #include "internal.h" | |
561fb04a | 89 | #include "io-wq.h" |
2b188cc1 | 90 | |
5277deaa | 91 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
93 | |
94 | /* | |
95 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
96 | */ | |
97 | #define IORING_FILE_TABLE_SHIFT 9 | |
98 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
99 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
100 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
101 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
102 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 103 | |
b16fed66 PB |
104 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
105 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
106 | IOSQE_BUFFER_SELECT) | |
107 | ||
2b188cc1 JA |
108 | struct io_uring { |
109 | u32 head ____cacheline_aligned_in_smp; | |
110 | u32 tail ____cacheline_aligned_in_smp; | |
111 | }; | |
112 | ||
1e84b97b | 113 | /* |
75b28aff HV |
114 | * This data is shared with the application through the mmap at offsets |
115 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
116 | * |
117 | * The offsets to the member fields are published through struct | |
118 | * io_sqring_offsets when calling io_uring_setup. | |
119 | */ | |
75b28aff | 120 | struct io_rings { |
1e84b97b SB |
121 | /* |
122 | * Head and tail offsets into the ring; the offsets need to be | |
123 | * masked to get valid indices. | |
124 | * | |
75b28aff HV |
125 | * The kernel controls head of the sq ring and the tail of the cq ring, |
126 | * and the application controls tail of the sq ring and the head of the | |
127 | * cq ring. | |
1e84b97b | 128 | */ |
75b28aff | 129 | struct io_uring sq, cq; |
1e84b97b | 130 | /* |
75b28aff | 131 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
132 | * ring_entries - 1) |
133 | */ | |
75b28aff HV |
134 | u32 sq_ring_mask, cq_ring_mask; |
135 | /* Ring sizes (constant, power of 2) */ | |
136 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
137 | /* |
138 | * Number of invalid entries dropped by the kernel due to | |
139 | * invalid index stored in array | |
140 | * | |
141 | * Written by the kernel, shouldn't be modified by the | |
142 | * application (i.e. get number of "new events" by comparing to | |
143 | * cached value). | |
144 | * | |
145 | * After a new SQ head value was read by the application this | |
146 | * counter includes all submissions that were dropped reaching | |
147 | * the new SQ head (and possibly more). | |
148 | */ | |
75b28aff | 149 | u32 sq_dropped; |
1e84b97b | 150 | /* |
0d9b5b3a | 151 | * Runtime SQ flags |
1e84b97b SB |
152 | * |
153 | * Written by the kernel, shouldn't be modified by the | |
154 | * application. | |
155 | * | |
156 | * The application needs a full memory barrier before checking | |
157 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
158 | */ | |
75b28aff | 159 | u32 sq_flags; |
0d9b5b3a SG |
160 | /* |
161 | * Runtime CQ flags | |
162 | * | |
163 | * Written by the application, shouldn't be modified by the | |
164 | * kernel. | |
165 | */ | |
166 | u32 cq_flags; | |
1e84b97b SB |
167 | /* |
168 | * Number of completion events lost because the queue was full; | |
169 | * this should be avoided by the application by making sure | |
0b4295b5 | 170 | * there are not more requests pending than there is space in |
1e84b97b SB |
171 | * the completion queue. |
172 | * | |
173 | * Written by the kernel, shouldn't be modified by the | |
174 | * application (i.e. get number of "new events" by comparing to | |
175 | * cached value). | |
176 | * | |
177 | * As completion events come in out of order this counter is not | |
178 | * ordered with any other data. | |
179 | */ | |
75b28aff | 180 | u32 cq_overflow; |
1e84b97b SB |
181 | /* |
182 | * Ring buffer of completion events. | |
183 | * | |
184 | * The kernel writes completion events fresh every time they are | |
185 | * produced, so the application is allowed to modify pending | |
186 | * entries. | |
187 | */ | |
75b28aff | 188 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
189 | }; |
190 | ||
45d189c6 PB |
191 | enum io_uring_cmd_flags { |
192 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 193 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
194 | }; |
195 | ||
edafccee JA |
196 | struct io_mapped_ubuf { |
197 | u64 ubuf; | |
198 | size_t len; | |
199 | struct bio_vec *bvec; | |
200 | unsigned int nr_bvecs; | |
de293938 | 201 | unsigned long acct_pages; |
edafccee JA |
202 | }; |
203 | ||
50238531 BM |
204 | struct io_ring_ctx; |
205 | ||
269bbe5f BM |
206 | struct io_rsrc_put { |
207 | struct list_head list; | |
50238531 BM |
208 | union { |
209 | void *rsrc; | |
210 | struct file *file; | |
211 | }; | |
269bbe5f BM |
212 | }; |
213 | ||
214 | struct fixed_rsrc_table { | |
65e19f54 | 215 | struct file **files; |
31b51510 JA |
216 | }; |
217 | ||
269bbe5f | 218 | struct fixed_rsrc_ref_node { |
05589553 XW |
219 | struct percpu_ref refs; |
220 | struct list_head node; | |
269bbe5f BM |
221 | struct list_head rsrc_list; |
222 | struct fixed_rsrc_data *rsrc_data; | |
50238531 BM |
223 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
224 | struct io_rsrc_put *prsrc); | |
4a38aed2 | 225 | struct llist_node llist; |
e297822b | 226 | bool done; |
05589553 XW |
227 | }; |
228 | ||
269bbe5f BM |
229 | struct fixed_rsrc_data { |
230 | struct fixed_rsrc_table *table; | |
05f3fb3c JA |
231 | struct io_ring_ctx *ctx; |
232 | ||
269bbe5f | 233 | struct fixed_rsrc_ref_node *node; |
05f3fb3c | 234 | struct percpu_ref refs; |
05f3fb3c | 235 | struct completion done; |
8bad28d8 | 236 | bool quiesce; |
05f3fb3c JA |
237 | }; |
238 | ||
5a2e745d JA |
239 | struct io_buffer { |
240 | struct list_head list; | |
241 | __u64 addr; | |
242 | __s32 len; | |
243 | __u16 bid; | |
244 | }; | |
245 | ||
21b55dbc SG |
246 | struct io_restriction { |
247 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
248 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
249 | u8 sqe_flags_allowed; | |
250 | u8 sqe_flags_required; | |
7e84e1c7 | 251 | bool registered; |
21b55dbc SG |
252 | }; |
253 | ||
37d1e2e3 JA |
254 | enum { |
255 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
256 | IO_SQ_THREAD_SHOULD_PARK, | |
257 | }; | |
258 | ||
534ca6d6 JA |
259 | struct io_sq_data { |
260 | refcount_t refs; | |
05962f95 | 261 | struct rw_semaphore rw_lock; |
69fb2131 JA |
262 | |
263 | /* ctx's that are using this sqd */ | |
264 | struct list_head ctx_list; | |
265 | struct list_head ctx_new_list; | |
69fb2131 | 266 | |
534ca6d6 JA |
267 | struct task_struct *thread; |
268 | struct wait_queue_head wait; | |
08369246 XW |
269 | |
270 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
271 | int sq_cpu; |
272 | pid_t task_pid; | |
273 | ||
274 | unsigned long state; | |
275 | struct completion startup; | |
37d1e2e3 | 276 | struct completion exited; |
534ca6d6 JA |
277 | }; |
278 | ||
258b29a9 | 279 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 280 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 281 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 282 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
283 | |
284 | struct io_comp_state { | |
6dd0be1e | 285 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 286 | unsigned int nr; |
c7dae4ba JA |
287 | unsigned int locked_free_nr; |
288 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 289 | struct list_head free_list; |
c7dae4ba JA |
290 | /* IRQ completion list, under ->completion_lock */ |
291 | struct list_head locked_free_list; | |
258b29a9 PB |
292 | }; |
293 | ||
a1ab7b35 PB |
294 | struct io_submit_link { |
295 | struct io_kiocb *head; | |
296 | struct io_kiocb *last; | |
297 | }; | |
298 | ||
258b29a9 PB |
299 | struct io_submit_state { |
300 | struct blk_plug plug; | |
a1ab7b35 | 301 | struct io_submit_link link; |
258b29a9 PB |
302 | |
303 | /* | |
304 | * io_kiocb alloc cache | |
305 | */ | |
bf019da7 | 306 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
307 | unsigned int free_reqs; |
308 | ||
309 | bool plug_started; | |
310 | ||
311 | /* | |
312 | * Batch completion logic | |
313 | */ | |
314 | struct io_comp_state comp; | |
315 | ||
316 | /* | |
317 | * File reference cache | |
318 | */ | |
319 | struct file *file; | |
320 | unsigned int fd; | |
321 | unsigned int file_refs; | |
322 | unsigned int ios_left; | |
323 | }; | |
324 | ||
2b188cc1 JA |
325 | struct io_ring_ctx { |
326 | struct { | |
327 | struct percpu_ref refs; | |
328 | } ____cacheline_aligned_in_smp; | |
329 | ||
330 | struct { | |
331 | unsigned int flags; | |
e1d85334 | 332 | unsigned int compat: 1; |
e1d85334 RD |
333 | unsigned int cq_overflow_flushed: 1; |
334 | unsigned int drain_next: 1; | |
335 | unsigned int eventfd_async: 1; | |
21b55dbc | 336 | unsigned int restricted: 1; |
2b188cc1 | 337 | |
75b28aff HV |
338 | /* |
339 | * Ring buffer of indices into array of io_uring_sqe, which is | |
340 | * mmapped by the application using the IORING_OFF_SQES offset. | |
341 | * | |
342 | * This indirection could e.g. be used to assign fixed | |
343 | * io_uring_sqe entries to operations and only submit them to | |
344 | * the queue when needed. | |
345 | * | |
346 | * The kernel modifies neither the indices array nor the entries | |
347 | * array. | |
348 | */ | |
349 | u32 *sq_array; | |
2b188cc1 JA |
350 | unsigned cached_sq_head; |
351 | unsigned sq_entries; | |
352 | unsigned sq_mask; | |
6c271ce2 | 353 | unsigned sq_thread_idle; |
498ccd9e | 354 | unsigned cached_sq_dropped; |
2c3bac6d | 355 | unsigned cached_cq_overflow; |
ad3eb2c8 | 356 | unsigned long sq_check_overflow; |
de0617e4 | 357 | |
e941894e JA |
358 | /* hashed buffered write serialization */ |
359 | struct io_wq_hash *hash_map; | |
360 | ||
de0617e4 | 361 | struct list_head defer_list; |
5262f567 | 362 | struct list_head timeout_list; |
1d7bb1d5 | 363 | struct list_head cq_overflow_list; |
fcb323cc | 364 | |
ad3eb2c8 | 365 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
366 | } ____cacheline_aligned_in_smp; |
367 | ||
3c1a2ead JA |
368 | struct { |
369 | struct mutex uring_lock; | |
370 | wait_queue_head_t wait; | |
371 | } ____cacheline_aligned_in_smp; | |
372 | ||
373 | struct io_submit_state submit_state; | |
374 | ||
206aefde JA |
375 | struct io_rings *rings; |
376 | ||
2aede0e4 JA |
377 | /* Only used for accounting purposes */ |
378 | struct mm_struct *mm_account; | |
379 | ||
7c30f36a | 380 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
381 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
382 | ||
90554200 | 383 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 384 | struct list_head sqd_list; |
75b28aff | 385 | |
6b06314c JA |
386 | /* |
387 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
388 | * readers must ensure that ->refs is alive as long as the file* is | |
389 | * used. Only updated through io_uring_register(2). | |
390 | */ | |
269bbe5f | 391 | struct fixed_rsrc_data *file_data; |
6b06314c JA |
392 | unsigned nr_user_files; |
393 | ||
edafccee JA |
394 | /* if used, fixed mapped user buffers */ |
395 | unsigned nr_user_bufs; | |
396 | struct io_mapped_ubuf *user_bufs; | |
397 | ||
2b188cc1 JA |
398 | struct user_struct *user; |
399 | ||
0f158b4c JA |
400 | struct completion ref_comp; |
401 | struct completion sq_thread_comp; | |
206aefde JA |
402 | |
403 | #if defined(CONFIG_UNIX) | |
404 | struct socket *ring_sock; | |
405 | #endif | |
406 | ||
5a2e745d JA |
407 | struct idr io_buffer_idr; |
408 | ||
071698e1 JA |
409 | struct idr personality_idr; |
410 | ||
206aefde JA |
411 | struct { |
412 | unsigned cached_cq_tail; | |
413 | unsigned cq_entries; | |
414 | unsigned cq_mask; | |
415 | atomic_t cq_timeouts; | |
f010505b | 416 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 417 | unsigned long cq_check_overflow; |
206aefde JA |
418 | struct wait_queue_head cq_wait; |
419 | struct fasync_struct *cq_fasync; | |
420 | struct eventfd_ctx *cq_ev_fd; | |
421 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 422 | |
2b188cc1 JA |
423 | struct { |
424 | spinlock_t completion_lock; | |
e94f141b | 425 | |
def596e9 | 426 | /* |
540e32a0 | 427 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
428 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
429 | * For SQPOLL, only the single threaded io_sq_thread() will | |
430 | * manipulate the list, hence no extra locking is needed there. | |
431 | */ | |
540e32a0 | 432 | struct list_head iopoll_list; |
78076bb6 JA |
433 | struct hlist_head *cancel_hash; |
434 | unsigned cancel_hash_bits; | |
e94f141b | 435 | bool poll_multi_file; |
31b51510 | 436 | |
fcb323cc JA |
437 | spinlock_t inflight_lock; |
438 | struct list_head inflight_list; | |
2b188cc1 | 439 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 440 | |
269bbe5f BM |
441 | struct delayed_work rsrc_put_work; |
442 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
443 | struct list_head rsrc_ref_list; |
444 | spinlock_t rsrc_ref_lock; | |
4a38aed2 | 445 | |
21b55dbc | 446 | struct io_restriction restrictions; |
3c1a2ead | 447 | |
7c25c0d1 JA |
448 | /* exit task_work */ |
449 | struct callback_head *exit_task_work; | |
450 | ||
e941894e JA |
451 | struct wait_queue_head hash_wait; |
452 | ||
3c1a2ead JA |
453 | /* Keep this last, we don't need it for the fast path */ |
454 | struct work_struct exit_work; | |
13bf43f5 | 455 | struct list_head tctx_list; |
2b188cc1 JA |
456 | }; |
457 | ||
09bb8394 JA |
458 | /* |
459 | * First field must be the file pointer in all the | |
460 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
461 | */ | |
221c5eb2 JA |
462 | struct io_poll_iocb { |
463 | struct file *file; | |
018043be | 464 | struct wait_queue_head *head; |
221c5eb2 | 465 | __poll_t events; |
8c838788 | 466 | bool done; |
221c5eb2 | 467 | bool canceled; |
392edb45 | 468 | struct wait_queue_entry wait; |
221c5eb2 JA |
469 | }; |
470 | ||
018043be PB |
471 | struct io_poll_remove { |
472 | struct file *file; | |
473 | u64 addr; | |
474 | }; | |
475 | ||
b5dba59e JA |
476 | struct io_close { |
477 | struct file *file; | |
b5dba59e JA |
478 | int fd; |
479 | }; | |
480 | ||
ad8a48ac JA |
481 | struct io_timeout_data { |
482 | struct io_kiocb *req; | |
483 | struct hrtimer timer; | |
484 | struct timespec64 ts; | |
485 | enum hrtimer_mode mode; | |
486 | }; | |
487 | ||
8ed8d3c3 JA |
488 | struct io_accept { |
489 | struct file *file; | |
490 | struct sockaddr __user *addr; | |
491 | int __user *addr_len; | |
492 | int flags; | |
09952e3e | 493 | unsigned long nofile; |
8ed8d3c3 JA |
494 | }; |
495 | ||
496 | struct io_sync { | |
497 | struct file *file; | |
498 | loff_t len; | |
499 | loff_t off; | |
500 | int flags; | |
d63d1b5e | 501 | int mode; |
8ed8d3c3 JA |
502 | }; |
503 | ||
fbf23849 JA |
504 | struct io_cancel { |
505 | struct file *file; | |
506 | u64 addr; | |
507 | }; | |
508 | ||
b29472ee JA |
509 | struct io_timeout { |
510 | struct file *file; | |
bfe68a22 PB |
511 | u32 off; |
512 | u32 target_seq; | |
135fcde8 | 513 | struct list_head list; |
90cd7e42 PB |
514 | /* head of the link, used by linked timeouts only */ |
515 | struct io_kiocb *head; | |
b29472ee JA |
516 | }; |
517 | ||
0bdf7a2d PB |
518 | struct io_timeout_rem { |
519 | struct file *file; | |
520 | u64 addr; | |
9c8e11b3 PB |
521 | |
522 | /* timeout update */ | |
523 | struct timespec64 ts; | |
524 | u32 flags; | |
0bdf7a2d PB |
525 | }; |
526 | ||
9adbd45d JA |
527 | struct io_rw { |
528 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
529 | struct kiocb kiocb; | |
530 | u64 addr; | |
531 | u64 len; | |
532 | }; | |
533 | ||
3fbb51c1 JA |
534 | struct io_connect { |
535 | struct file *file; | |
536 | struct sockaddr __user *addr; | |
537 | int addr_len; | |
538 | }; | |
539 | ||
e47293fd JA |
540 | struct io_sr_msg { |
541 | struct file *file; | |
fddaface | 542 | union { |
270a5940 | 543 | struct user_msghdr __user *umsg; |
fddaface JA |
544 | void __user *buf; |
545 | }; | |
e47293fd | 546 | int msg_flags; |
bcda7baa | 547 | int bgid; |
fddaface | 548 | size_t len; |
bcda7baa | 549 | struct io_buffer *kbuf; |
e47293fd JA |
550 | }; |
551 | ||
15b71abe JA |
552 | struct io_open { |
553 | struct file *file; | |
554 | int dfd; | |
15b71abe | 555 | struct filename *filename; |
c12cedf2 | 556 | struct open_how how; |
4022e7af | 557 | unsigned long nofile; |
15b71abe JA |
558 | }; |
559 | ||
269bbe5f | 560 | struct io_rsrc_update { |
05f3fb3c JA |
561 | struct file *file; |
562 | u64 arg; | |
563 | u32 nr_args; | |
564 | u32 offset; | |
565 | }; | |
566 | ||
4840e418 JA |
567 | struct io_fadvise { |
568 | struct file *file; | |
569 | u64 offset; | |
570 | u32 len; | |
571 | u32 advice; | |
572 | }; | |
573 | ||
c1ca757b JA |
574 | struct io_madvise { |
575 | struct file *file; | |
576 | u64 addr; | |
577 | u32 len; | |
578 | u32 advice; | |
579 | }; | |
580 | ||
3e4827b0 JA |
581 | struct io_epoll { |
582 | struct file *file; | |
583 | int epfd; | |
584 | int op; | |
585 | int fd; | |
586 | struct epoll_event event; | |
e47293fd JA |
587 | }; |
588 | ||
7d67af2c PB |
589 | struct io_splice { |
590 | struct file *file_out; | |
591 | struct file *file_in; | |
592 | loff_t off_out; | |
593 | loff_t off_in; | |
594 | u64 len; | |
595 | unsigned int flags; | |
596 | }; | |
597 | ||
ddf0322d JA |
598 | struct io_provide_buf { |
599 | struct file *file; | |
600 | __u64 addr; | |
601 | __s32 len; | |
602 | __u32 bgid; | |
603 | __u16 nbufs; | |
604 | __u16 bid; | |
605 | }; | |
606 | ||
1d9e1288 BM |
607 | struct io_statx { |
608 | struct file *file; | |
609 | int dfd; | |
610 | unsigned int mask; | |
611 | unsigned int flags; | |
e62753e4 | 612 | const char __user *filename; |
1d9e1288 BM |
613 | struct statx __user *buffer; |
614 | }; | |
615 | ||
36f4fa68 JA |
616 | struct io_shutdown { |
617 | struct file *file; | |
618 | int how; | |
619 | }; | |
620 | ||
80a261fd JA |
621 | struct io_rename { |
622 | struct file *file; | |
623 | int old_dfd; | |
624 | int new_dfd; | |
625 | struct filename *oldpath; | |
626 | struct filename *newpath; | |
627 | int flags; | |
628 | }; | |
629 | ||
14a1143b JA |
630 | struct io_unlink { |
631 | struct file *file; | |
632 | int dfd; | |
633 | int flags; | |
634 | struct filename *filename; | |
635 | }; | |
636 | ||
3ca405eb PB |
637 | struct io_completion { |
638 | struct file *file; | |
639 | struct list_head list; | |
0f7e466b | 640 | int cflags; |
3ca405eb PB |
641 | }; |
642 | ||
f499a021 JA |
643 | struct io_async_connect { |
644 | struct sockaddr_storage address; | |
645 | }; | |
646 | ||
03b1230c JA |
647 | struct io_async_msghdr { |
648 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
649 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
650 | struct iovec *free_iov; | |
03b1230c JA |
651 | struct sockaddr __user *uaddr; |
652 | struct msghdr msg; | |
b537916c | 653 | struct sockaddr_storage addr; |
03b1230c JA |
654 | }; |
655 | ||
f67676d1 JA |
656 | struct io_async_rw { |
657 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
658 | const struct iovec *free_iovec; |
659 | struct iov_iter iter; | |
227c0c96 | 660 | size_t bytes_done; |
bcf5a063 | 661 | struct wait_page_queue wpq; |
f67676d1 JA |
662 | }; |
663 | ||
6b47ee6e PB |
664 | enum { |
665 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
666 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
667 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
668 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
669 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 670 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 671 | |
6b47ee6e PB |
672 | REQ_F_FAIL_LINK_BIT, |
673 | REQ_F_INFLIGHT_BIT, | |
674 | REQ_F_CUR_POS_BIT, | |
675 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 676 | REQ_F_LINK_TIMEOUT_BIT, |
6b47ee6e | 677 | REQ_F_ISREG_BIT, |
99bc4c38 | 678 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 679 | REQ_F_POLLED_BIT, |
bcda7baa | 680 | REQ_F_BUFFER_SELECTED_BIT, |
5b0bbee4 | 681 | REQ_F_NO_FILE_TABLE_BIT, |
900fad45 | 682 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 683 | REQ_F_COMPLETE_INLINE_BIT, |
84557871 JA |
684 | |
685 | /* not a real bit, just to check we're not overflowing the space */ | |
686 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
687 | }; |
688 | ||
689 | enum { | |
690 | /* ctx owns file */ | |
691 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
692 | /* drain existing IO first */ | |
693 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
694 | /* linked sqes */ | |
695 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
696 | /* doesn't sever on completion < 0 */ | |
697 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
698 | /* IOSQE_ASYNC */ | |
699 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
700 | /* IOSQE_BUFFER_SELECT */ |
701 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 702 | |
6b47ee6e PB |
703 | /* fail rest of links */ |
704 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
b05a1bcd | 705 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
706 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
707 | /* read/write uses file position */ | |
708 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
709 | /* must not punt to workers */ | |
710 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 711 | /* has or had linked timeout */ |
6b47ee6e | 712 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
6b47ee6e PB |
713 | /* regular file */ |
714 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
99bc4c38 PB |
715 | /* needs cleanup */ |
716 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
717 | /* already went through poll handler */ |
718 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
719 | /* buffer already selected */ |
720 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
5b0bbee4 JA |
721 | /* doesn't need file table for this request */ |
722 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), | |
900fad45 PB |
723 | /* linked timeout is active, i.e. prepared by link's head */ |
724 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
725 | /* completion is deferred through io_comp_state */ |
726 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
d7718a9d JA |
727 | }; |
728 | ||
729 | struct async_poll { | |
730 | struct io_poll_iocb poll; | |
807abcb0 | 731 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
732 | }; |
733 | ||
7cbf1722 JA |
734 | struct io_task_work { |
735 | struct io_wq_work_node node; | |
736 | task_work_func_t func; | |
737 | }; | |
738 | ||
09bb8394 JA |
739 | /* |
740 | * NOTE! Each of the iocb union members has the file pointer | |
741 | * as the first entry in their struct definition. So you can | |
742 | * access the file pointer through any of the sub-structs, | |
743 | * or directly as just 'ki_filp' in this struct. | |
744 | */ | |
2b188cc1 | 745 | struct io_kiocb { |
221c5eb2 | 746 | union { |
09bb8394 | 747 | struct file *file; |
9adbd45d | 748 | struct io_rw rw; |
221c5eb2 | 749 | struct io_poll_iocb poll; |
018043be | 750 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
751 | struct io_accept accept; |
752 | struct io_sync sync; | |
fbf23849 | 753 | struct io_cancel cancel; |
b29472ee | 754 | struct io_timeout timeout; |
0bdf7a2d | 755 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 756 | struct io_connect connect; |
e47293fd | 757 | struct io_sr_msg sr_msg; |
15b71abe | 758 | struct io_open open; |
b5dba59e | 759 | struct io_close close; |
269bbe5f | 760 | struct io_rsrc_update rsrc_update; |
4840e418 | 761 | struct io_fadvise fadvise; |
c1ca757b | 762 | struct io_madvise madvise; |
3e4827b0 | 763 | struct io_epoll epoll; |
7d67af2c | 764 | struct io_splice splice; |
ddf0322d | 765 | struct io_provide_buf pbuf; |
1d9e1288 | 766 | struct io_statx statx; |
36f4fa68 | 767 | struct io_shutdown shutdown; |
80a261fd | 768 | struct io_rename rename; |
14a1143b | 769 | struct io_unlink unlink; |
3ca405eb PB |
770 | /* use only after cleaning per-op data, see io_clean_op() */ |
771 | struct io_completion compl; | |
221c5eb2 | 772 | }; |
2b188cc1 | 773 | |
e8c2bc1f JA |
774 | /* opcode allocated if it needs to store data for async defer */ |
775 | void *async_data; | |
d625c6ee | 776 | u8 opcode; |
65a6543d XW |
777 | /* polled IO has completed */ |
778 | u8 iopoll_completed; | |
2b188cc1 | 779 | |
4f4eeba8 | 780 | u16 buf_index; |
9cf7c104 | 781 | u32 result; |
4f4eeba8 | 782 | |
010e8e6b PB |
783 | struct io_ring_ctx *ctx; |
784 | unsigned int flags; | |
785 | refcount_t refs; | |
786 | struct task_struct *task; | |
787 | u64 user_data; | |
d7718a9d | 788 | |
f2f87370 | 789 | struct io_kiocb *link; |
269bbe5f | 790 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 791 | |
d21ffe7e PB |
792 | /* |
793 | * 1. used with ctx->iopoll_list with reads/writes | |
794 | * 2. to track reqs with ->files (see io_op_def::file_table) | |
795 | */ | |
010e8e6b | 796 | struct list_head inflight_entry; |
7cbf1722 JA |
797 | union { |
798 | struct io_task_work io_task_work; | |
799 | struct callback_head task_work; | |
800 | }; | |
010e8e6b PB |
801 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
802 | struct hlist_node hash_node; | |
803 | struct async_poll *apoll; | |
804 | struct io_wq_work work; | |
2b188cc1 | 805 | }; |
05589553 | 806 | |
13bf43f5 PB |
807 | struct io_tctx_node { |
808 | struct list_head ctx_node; | |
809 | struct task_struct *task; | |
13bf43f5 PB |
810 | struct io_ring_ctx *ctx; |
811 | }; | |
812 | ||
27dc8338 PB |
813 | struct io_defer_entry { |
814 | struct list_head list; | |
815 | struct io_kiocb *req; | |
9cf7c104 | 816 | u32 seq; |
2b188cc1 JA |
817 | }; |
818 | ||
d3656344 | 819 | struct io_op_def { |
d3656344 JA |
820 | /* needs req->file assigned */ |
821 | unsigned needs_file : 1; | |
d3656344 JA |
822 | /* hash wq insertion if file is a regular file */ |
823 | unsigned hash_reg_file : 1; | |
824 | /* unbound wq insertion if file is a non-regular file */ | |
825 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
826 | /* opcode is not supported by this kernel */ |
827 | unsigned not_supported : 1; | |
8a72758c JA |
828 | /* set if opcode supports polled "wait" */ |
829 | unsigned pollin : 1; | |
830 | unsigned pollout : 1; | |
bcda7baa JA |
831 | /* op supports buffer selection */ |
832 | unsigned buffer_select : 1; | |
e8c2bc1f JA |
833 | /* must always have async data allocated */ |
834 | unsigned needs_async_data : 1; | |
27926b68 JA |
835 | /* should block plug */ |
836 | unsigned plug : 1; | |
e8c2bc1f JA |
837 | /* size of async data needed, if any */ |
838 | unsigned short async_size; | |
d3656344 JA |
839 | }; |
840 | ||
0918682b | 841 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
842 | [IORING_OP_NOP] = {}, |
843 | [IORING_OP_READV] = { | |
d3656344 JA |
844 | .needs_file = 1, |
845 | .unbound_nonreg_file = 1, | |
8a72758c | 846 | .pollin = 1, |
4d954c25 | 847 | .buffer_select = 1, |
e8c2bc1f | 848 | .needs_async_data = 1, |
27926b68 | 849 | .plug = 1, |
e8c2bc1f | 850 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 851 | }, |
0463b6c5 | 852 | [IORING_OP_WRITEV] = { |
d3656344 JA |
853 | .needs_file = 1, |
854 | .hash_reg_file = 1, | |
855 | .unbound_nonreg_file = 1, | |
8a72758c | 856 | .pollout = 1, |
e8c2bc1f | 857 | .needs_async_data = 1, |
27926b68 | 858 | .plug = 1, |
e8c2bc1f | 859 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 860 | }, |
0463b6c5 | 861 | [IORING_OP_FSYNC] = { |
d3656344 JA |
862 | .needs_file = 1, |
863 | }, | |
0463b6c5 | 864 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
865 | .needs_file = 1, |
866 | .unbound_nonreg_file = 1, | |
8a72758c | 867 | .pollin = 1, |
27926b68 | 868 | .plug = 1, |
e8c2bc1f | 869 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 870 | }, |
0463b6c5 | 871 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
872 | .needs_file = 1, |
873 | .hash_reg_file = 1, | |
874 | .unbound_nonreg_file = 1, | |
8a72758c | 875 | .pollout = 1, |
27926b68 | 876 | .plug = 1, |
e8c2bc1f | 877 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 878 | }, |
0463b6c5 | 879 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
880 | .needs_file = 1, |
881 | .unbound_nonreg_file = 1, | |
882 | }, | |
0463b6c5 PB |
883 | [IORING_OP_POLL_REMOVE] = {}, |
884 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
885 | .needs_file = 1, |
886 | }, | |
0463b6c5 | 887 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
888 | .needs_file = 1, |
889 | .unbound_nonreg_file = 1, | |
8a72758c | 890 | .pollout = 1, |
e8c2bc1f JA |
891 | .needs_async_data = 1, |
892 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 893 | }, |
0463b6c5 | 894 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
895 | .needs_file = 1, |
896 | .unbound_nonreg_file = 1, | |
8a72758c | 897 | .pollin = 1, |
52de1fe1 | 898 | .buffer_select = 1, |
e8c2bc1f JA |
899 | .needs_async_data = 1, |
900 | .async_size = sizeof(struct io_async_msghdr), | |
d3656344 | 901 | }, |
0463b6c5 | 902 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f JA |
903 | .needs_async_data = 1, |
904 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 905 | }, |
9c8e11b3 PB |
906 | [IORING_OP_TIMEOUT_REMOVE] = { |
907 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 908 | }, |
0463b6c5 | 909 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
910 | .needs_file = 1, |
911 | .unbound_nonreg_file = 1, | |
8a72758c | 912 | .pollin = 1, |
d3656344 | 913 | }, |
0463b6c5 PB |
914 | [IORING_OP_ASYNC_CANCEL] = {}, |
915 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f JA |
916 | .needs_async_data = 1, |
917 | .async_size = sizeof(struct io_timeout_data), | |
d3656344 | 918 | }, |
0463b6c5 | 919 | [IORING_OP_CONNECT] = { |
d3656344 JA |
920 | .needs_file = 1, |
921 | .unbound_nonreg_file = 1, | |
8a72758c | 922 | .pollout = 1, |
e8c2bc1f JA |
923 | .needs_async_data = 1, |
924 | .async_size = sizeof(struct io_async_connect), | |
d3656344 | 925 | }, |
0463b6c5 | 926 | [IORING_OP_FALLOCATE] = { |
d3656344 | 927 | .needs_file = 1, |
d3656344 | 928 | }, |
44526bed JA |
929 | [IORING_OP_OPENAT] = {}, |
930 | [IORING_OP_CLOSE] = {}, | |
931 | [IORING_OP_FILES_UPDATE] = {}, | |
932 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 933 | [IORING_OP_READ] = { |
3a6820f2 JA |
934 | .needs_file = 1, |
935 | .unbound_nonreg_file = 1, | |
8a72758c | 936 | .pollin = 1, |
bcda7baa | 937 | .buffer_select = 1, |
27926b68 | 938 | .plug = 1, |
e8c2bc1f | 939 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 940 | }, |
0463b6c5 | 941 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
942 | .needs_file = 1, |
943 | .unbound_nonreg_file = 1, | |
8a72758c | 944 | .pollout = 1, |
27926b68 | 945 | .plug = 1, |
e8c2bc1f | 946 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 947 | }, |
0463b6c5 | 948 | [IORING_OP_FADVISE] = { |
4840e418 | 949 | .needs_file = 1, |
c1ca757b | 950 | }, |
44526bed | 951 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 952 | [IORING_OP_SEND] = { |
fddaface JA |
953 | .needs_file = 1, |
954 | .unbound_nonreg_file = 1, | |
8a72758c | 955 | .pollout = 1, |
fddaface | 956 | }, |
0463b6c5 | 957 | [IORING_OP_RECV] = { |
fddaface JA |
958 | .needs_file = 1, |
959 | .unbound_nonreg_file = 1, | |
8a72758c | 960 | .pollin = 1, |
bcda7baa | 961 | .buffer_select = 1, |
fddaface | 962 | }, |
0463b6c5 | 963 | [IORING_OP_OPENAT2] = { |
cebdb986 | 964 | }, |
3e4827b0 JA |
965 | [IORING_OP_EPOLL_CTL] = { |
966 | .unbound_nonreg_file = 1, | |
3e4827b0 | 967 | }, |
7d67af2c PB |
968 | [IORING_OP_SPLICE] = { |
969 | .needs_file = 1, | |
970 | .hash_reg_file = 1, | |
971 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
972 | }, |
973 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 974 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
975 | [IORING_OP_TEE] = { |
976 | .needs_file = 1, | |
977 | .hash_reg_file = 1, | |
978 | .unbound_nonreg_file = 1, | |
979 | }, | |
36f4fa68 JA |
980 | [IORING_OP_SHUTDOWN] = { |
981 | .needs_file = 1, | |
982 | }, | |
44526bed JA |
983 | [IORING_OP_RENAMEAT] = {}, |
984 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
985 | }; |
986 | ||
d56d938b | 987 | static void io_uring_del_task_file(unsigned long index); |
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 | ||
08d23634 PB |
1062 | static bool io_match_task(struct io_kiocb *head, |
1063 | struct task_struct *task, | |
1064 | struct files_struct *files) | |
1065 | { | |
1066 | struct io_kiocb *req; | |
1067 | ||
84965ff8 JA |
1068 | if (task && head->task != task) { |
1069 | /* in terms of cancelation, always match if req task is dead */ | |
1070 | if (head->task->flags & PF_EXITING) | |
1071 | return true; | |
08d23634 | 1072 | return false; |
84965ff8 | 1073 | } |
08d23634 PB |
1074 | if (!files) |
1075 | return true; | |
1076 | ||
1077 | io_for_each_link(req, head) { | |
b05a1bcd | 1078 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1079 | return true; |
4379bf8b | 1080 | if (req->task->files == files) |
08d23634 PB |
1081 | return true; |
1082 | } | |
1083 | return false; | |
1084 | } | |
1085 | ||
c40f6379 JA |
1086 | static inline void req_set_fail_links(struct io_kiocb *req) |
1087 | { | |
1088 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1089 | req->flags |= REQ_F_FAIL_LINK; | |
1090 | } | |
4a38aed2 | 1091 | |
2b188cc1 JA |
1092 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1093 | { | |
1094 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1095 | ||
0f158b4c | 1096 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1097 | } |
1098 | ||
8eb7e2d0 PB |
1099 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1100 | { | |
1101 | return !req->timeout.off; | |
1102 | } | |
1103 | ||
2b188cc1 JA |
1104 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1105 | { | |
1106 | struct io_ring_ctx *ctx; | |
78076bb6 | 1107 | int hash_bits; |
2b188cc1 JA |
1108 | |
1109 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1110 | if (!ctx) | |
1111 | return NULL; | |
1112 | ||
78076bb6 JA |
1113 | /* |
1114 | * Use 5 bits less than the max cq entries, that should give us around | |
1115 | * 32 entries per hash list if totally full and uniformly spread. | |
1116 | */ | |
1117 | hash_bits = ilog2(p->cq_entries); | |
1118 | hash_bits -= 5; | |
1119 | if (hash_bits <= 0) | |
1120 | hash_bits = 1; | |
1121 | ctx->cancel_hash_bits = hash_bits; | |
1122 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1123 | GFP_KERNEL); | |
1124 | if (!ctx->cancel_hash) | |
1125 | goto err; | |
1126 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1127 | ||
21482896 | 1128 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1129 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1130 | goto err; | |
2b188cc1 JA |
1131 | |
1132 | ctx->flags = p->flags; | |
90554200 | 1133 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1134 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1135 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1136 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c JA |
1137 | init_completion(&ctx->ref_comp); |
1138 | init_completion(&ctx->sq_thread_comp); | |
5a2e745d | 1139 | idr_init(&ctx->io_buffer_idr); |
071698e1 | 1140 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
1141 | mutex_init(&ctx->uring_lock); |
1142 | init_waitqueue_head(&ctx->wait); | |
1143 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1144 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1145 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1146 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
1147 | spin_lock_init(&ctx->inflight_lock); |
1148 | INIT_LIST_HEAD(&ctx->inflight_list); | |
d67d2263 BM |
1149 | spin_lock_init(&ctx->rsrc_ref_lock); |
1150 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1151 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1152 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1153 | INIT_LIST_HEAD(&ctx->tctx_list); |
1b4c351f | 1154 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1155 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1156 | return ctx; |
206aefde | 1157 | err: |
78076bb6 | 1158 | kfree(ctx->cancel_hash); |
206aefde JA |
1159 | kfree(ctx); |
1160 | return NULL; | |
2b188cc1 JA |
1161 | } |
1162 | ||
9cf7c104 | 1163 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1164 | { |
2bc9930e JA |
1165 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1166 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1167 | |
9cf7c104 | 1168 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1169 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1170 | } |
de0617e4 | 1171 | |
9d858b21 | 1172 | return false; |
de0617e4 JA |
1173 | } |
1174 | ||
ce3d5aae PB |
1175 | static void io_req_track_inflight(struct io_kiocb *req) |
1176 | { | |
1177 | struct io_ring_ctx *ctx = req->ctx; | |
1178 | ||
1179 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
ce3d5aae PB |
1180 | req->flags |= REQ_F_INFLIGHT; |
1181 | ||
1182 | spin_lock_irq(&ctx->inflight_lock); | |
1183 | list_add(&req->inflight_entry, &ctx->inflight_list); | |
1184 | spin_unlock_irq(&ctx->inflight_lock); | |
1185 | } | |
1186 | } | |
1187 | ||
1e6fa521 JA |
1188 | static void io_prep_async_work(struct io_kiocb *req) |
1189 | { | |
1190 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1191 | struct io_ring_ctx *ctx = req->ctx; |
1192 | ||
003e8dcc JA |
1193 | if (!req->work.creds) |
1194 | req->work.creds = get_current_cred(); | |
1195 | ||
feaadc4f PB |
1196 | if (req->flags & REQ_F_FORCE_ASYNC) |
1197 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1198 | ||
1e6fa521 JA |
1199 | if (req->flags & REQ_F_ISREG) { |
1200 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1201 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
1202 | } else { | |
1203 | if (def->unbound_nonreg_file) | |
1204 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1205 | } | |
561fb04a | 1206 | } |
cccf0ee8 | 1207 | |
cbdcb435 | 1208 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1209 | { |
cbdcb435 | 1210 | struct io_kiocb *cur; |
54a91f3b | 1211 | |
f2f87370 PB |
1212 | io_for_each_link(cur, req) |
1213 | io_prep_async_work(cur); | |
561fb04a JA |
1214 | } |
1215 | ||
ebf93667 | 1216 | static void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1217 | { |
a197f664 | 1218 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1219 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1220 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1221 | |
3bfe6106 JA |
1222 | BUG_ON(!tctx); |
1223 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1224 | |
8766dd51 PB |
1225 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1226 | &req->work, req->flags); | |
cbdcb435 PB |
1227 | /* init ->work of the whole link before punting */ |
1228 | io_prep_async_link(req); | |
ebf93667 | 1229 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1230 | if (link) |
1231 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1232 | } |
1233 | ||
5262f567 JA |
1234 | static void io_kill_timeout(struct io_kiocb *req) |
1235 | { | |
e8c2bc1f | 1236 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1237 | int ret; |
1238 | ||
e8c2bc1f | 1239 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1240 | if (ret != -1) { |
01cec8c1 PB |
1241 | atomic_set(&req->ctx->cq_timeouts, |
1242 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1243 | list_del_init(&req->timeout.list); |
78e19bbe | 1244 | io_cqring_fill_event(req, 0); |
216578e5 | 1245 | io_put_req_deferred(req, 1); |
5262f567 JA |
1246 | } |
1247 | } | |
1248 | ||
76e1b642 JA |
1249 | /* |
1250 | * Returns true if we found and killed one or more timeouts | |
1251 | */ | |
6b81928d PB |
1252 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
1253 | struct files_struct *files) | |
5262f567 JA |
1254 | { |
1255 | struct io_kiocb *req, *tmp; | |
76e1b642 | 1256 | int canceled = 0; |
5262f567 JA |
1257 | |
1258 | spin_lock_irq(&ctx->completion_lock); | |
f3606e3a | 1259 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
6b81928d | 1260 | if (io_match_task(req, tsk, files)) { |
f3606e3a | 1261 | io_kill_timeout(req); |
76e1b642 JA |
1262 | canceled++; |
1263 | } | |
f3606e3a | 1264 | } |
5262f567 | 1265 | spin_unlock_irq(&ctx->completion_lock); |
76e1b642 | 1266 | return canceled != 0; |
5262f567 JA |
1267 | } |
1268 | ||
04518945 | 1269 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1270 | { |
04518945 | 1271 | do { |
27dc8338 PB |
1272 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1273 | struct io_defer_entry, list); | |
de0617e4 | 1274 | |
9cf7c104 | 1275 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1276 | break; |
27dc8338 | 1277 | list_del_init(&de->list); |
907d1df3 | 1278 | io_req_task_queue(de->req); |
27dc8338 | 1279 | kfree(de); |
04518945 PB |
1280 | } while (!list_empty(&ctx->defer_list)); |
1281 | } | |
1282 | ||
360428f8 | 1283 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1284 | { |
f010505b MDG |
1285 | u32 seq; |
1286 | ||
1287 | if (list_empty(&ctx->timeout_list)) | |
1288 | return; | |
1289 | ||
1290 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1291 | ||
1292 | do { | |
1293 | u32 events_needed, events_got; | |
360428f8 | 1294 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1295 | struct io_kiocb, timeout.list); |
de0617e4 | 1296 | |
8eb7e2d0 | 1297 | if (io_is_timeout_noseq(req)) |
360428f8 | 1298 | break; |
f010505b MDG |
1299 | |
1300 | /* | |
1301 | * Since seq can easily wrap around over time, subtract | |
1302 | * the last seq at which timeouts were flushed before comparing. | |
1303 | * Assuming not more than 2^31-1 events have happened since, | |
1304 | * these subtractions won't have wrapped, so we can check if | |
1305 | * target is in [last_seq, current_seq] by comparing the two. | |
1306 | */ | |
1307 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1308 | events_got = seq - ctx->cq_last_tm_flush; | |
1309 | if (events_got < events_needed) | |
360428f8 | 1310 | break; |
bfe68a22 | 1311 | |
135fcde8 | 1312 | list_del_init(&req->timeout.list); |
5262f567 | 1313 | io_kill_timeout(req); |
f010505b MDG |
1314 | } while (!list_empty(&ctx->timeout_list)); |
1315 | ||
1316 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1317 | } |
5262f567 | 1318 | |
360428f8 PB |
1319 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1320 | { | |
1321 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1322 | |
1323 | /* order cqe stores with ring update */ | |
1324 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1325 | |
04518945 PB |
1326 | if (unlikely(!list_empty(&ctx->defer_list))) |
1327 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1328 | } |
1329 | ||
90554200 JA |
1330 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1331 | { | |
1332 | struct io_rings *r = ctx->rings; | |
1333 | ||
1334 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1335 | } | |
1336 | ||
888aae2e PB |
1337 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1338 | { | |
1339 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1340 | } | |
1341 | ||
2b188cc1 JA |
1342 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1343 | { | |
75b28aff | 1344 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1345 | unsigned tail; |
1346 | ||
115e12e5 SB |
1347 | /* |
1348 | * writes to the cq entry need to come after reading head; the | |
1349 | * control dependency is enough as we're using WRITE_ONCE to | |
1350 | * fill the cq entry | |
1351 | */ | |
888aae2e | 1352 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1353 | return NULL; |
1354 | ||
888aae2e | 1355 | tail = ctx->cached_cq_tail++; |
75b28aff | 1356 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1357 | } |
1358 | ||
f2842ab5 JA |
1359 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1360 | { | |
f0b493e6 JA |
1361 | if (!ctx->cq_ev_fd) |
1362 | return false; | |
7e55a19c SG |
1363 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1364 | return false; | |
f2842ab5 JA |
1365 | if (!ctx->eventfd_async) |
1366 | return true; | |
b41e9852 | 1367 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1368 | } |
1369 | ||
b41e9852 | 1370 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1371 | { |
b1445e59 PB |
1372 | /* see waitqueue_active() comment */ |
1373 | smp_mb(); | |
1374 | ||
1d7bb1d5 JA |
1375 | if (waitqueue_active(&ctx->wait)) |
1376 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1377 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1378 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1379 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1380 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1381 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1382 | wake_up_interruptible(&ctx->cq_wait); |
1383 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1384 | } | |
1d7bb1d5 JA |
1385 | } |
1386 | ||
80c18e4a PB |
1387 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1388 | { | |
b1445e59 PB |
1389 | /* see waitqueue_active() comment */ |
1390 | smp_mb(); | |
1391 | ||
80c18e4a PB |
1392 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1393 | if (waitqueue_active(&ctx->wait)) | |
1394 | wake_up(&ctx->wait); | |
1395 | } | |
1396 | if (io_should_trigger_evfd(ctx)) | |
1397 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1398 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1399 | wake_up_interruptible(&ctx->cq_wait); |
1400 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1401 | } | |
80c18e4a PB |
1402 | } |
1403 | ||
c4a2ed72 | 1404 | /* Returns true if there are no backlogged entries after the flush */ |
6c503150 PB |
1405 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
1406 | struct task_struct *tsk, | |
1407 | struct files_struct *files) | |
1d7bb1d5 JA |
1408 | { |
1409 | struct io_rings *rings = ctx->rings; | |
e6c8aa9a | 1410 | struct io_kiocb *req, *tmp; |
1d7bb1d5 | 1411 | struct io_uring_cqe *cqe; |
1d7bb1d5 | 1412 | unsigned long flags; |
b18032bb | 1413 | bool all_flushed, posted; |
1d7bb1d5 JA |
1414 | LIST_HEAD(list); |
1415 | ||
e23de15f PB |
1416 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1417 | return false; | |
1d7bb1d5 | 1418 | |
b18032bb | 1419 | posted = false; |
1d7bb1d5 | 1420 | spin_lock_irqsave(&ctx->completion_lock, flags); |
e6c8aa9a | 1421 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
08d23634 | 1422 | if (!io_match_task(req, tsk, files)) |
e6c8aa9a JA |
1423 | continue; |
1424 | ||
1d7bb1d5 JA |
1425 | cqe = io_get_cqring(ctx); |
1426 | if (!cqe && !force) | |
1427 | break; | |
1428 | ||
40d8ddd4 | 1429 | list_move(&req->compl.list, &list); |
1d7bb1d5 JA |
1430 | if (cqe) { |
1431 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1432 | WRITE_ONCE(cqe->res, req->result); | |
0f7e466b | 1433 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
1d7bb1d5 | 1434 | } else { |
2c3bac6d | 1435 | ctx->cached_cq_overflow++; |
1d7bb1d5 | 1436 | WRITE_ONCE(ctx->rings->cq_overflow, |
2c3bac6d | 1437 | ctx->cached_cq_overflow); |
1d7bb1d5 | 1438 | } |
b18032bb | 1439 | posted = true; |
1d7bb1d5 JA |
1440 | } |
1441 | ||
09e88404 PB |
1442 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1443 | if (all_flushed) { | |
1444 | clear_bit(0, &ctx->sq_check_overflow); | |
1445 | clear_bit(0, &ctx->cq_check_overflow); | |
1446 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1447 | } | |
46930143 | 1448 | |
b18032bb JA |
1449 | if (posted) |
1450 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1451 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1452 | if (posted) |
1453 | io_cqring_ev_posted(ctx); | |
1d7bb1d5 JA |
1454 | |
1455 | while (!list_empty(&list)) { | |
40d8ddd4 PB |
1456 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
1457 | list_del(&req->compl.list); | |
ec9c02ad | 1458 | io_put_req(req); |
1d7bb1d5 | 1459 | } |
c4a2ed72 | 1460 | |
09e88404 | 1461 | return all_flushed; |
1d7bb1d5 JA |
1462 | } |
1463 | ||
ca0a2651 | 1464 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
6c503150 PB |
1465 | struct task_struct *tsk, |
1466 | struct files_struct *files) | |
1467 | { | |
ca0a2651 JA |
1468 | bool ret = true; |
1469 | ||
6c503150 PB |
1470 | if (test_bit(0, &ctx->cq_check_overflow)) { |
1471 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1472 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1473 | mutex_lock(&ctx->uring_lock); | |
ca0a2651 | 1474 | ret = __io_cqring_overflow_flush(ctx, force, tsk, files); |
6c503150 PB |
1475 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1476 | mutex_unlock(&ctx->uring_lock); | |
1477 | } | |
ca0a2651 JA |
1478 | |
1479 | return ret; | |
6c503150 PB |
1480 | } |
1481 | ||
bcda7baa | 1482 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
2b188cc1 | 1483 | { |
78e19bbe | 1484 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1485 | struct io_uring_cqe *cqe; |
1486 | ||
78e19bbe | 1487 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1488 | |
2b188cc1 JA |
1489 | /* |
1490 | * If we can't get a cq entry, userspace overflowed the | |
1491 | * submission (by quite a lot). Increment the overflow count in | |
1492 | * the ring. | |
1493 | */ | |
1494 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1495 | if (likely(cqe)) { |
78e19bbe | 1496 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1497 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1498 | WRITE_ONCE(cqe->flags, cflags); |
fdaf083c JA |
1499 | } else if (ctx->cq_overflow_flushed || |
1500 | atomic_read(&req->task->io_uring->in_idle)) { | |
0f212204 JA |
1501 | /* |
1502 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1503 | * then we cannot store the request for later flushing, we need | |
1504 | * to drop it on the floor. | |
1505 | */ | |
2c3bac6d PB |
1506 | ctx->cached_cq_overflow++; |
1507 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); | |
1d7bb1d5 | 1508 | } else { |
ad3eb2c8 JA |
1509 | if (list_empty(&ctx->cq_overflow_list)) { |
1510 | set_bit(0, &ctx->sq_check_overflow); | |
1511 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1512 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1513 | } |
40d8ddd4 | 1514 | io_clean_op(req); |
1d7bb1d5 | 1515 | req->result = res; |
0f7e466b | 1516 | req->compl.cflags = cflags; |
40d8ddd4 PB |
1517 | refcount_inc(&req->refs); |
1518 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1519 | } |
1520 | } | |
1521 | ||
bcda7baa JA |
1522 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1523 | { | |
1524 | __io_cqring_fill_event(req, res, 0); | |
1525 | } | |
1526 | ||
c7dae4ba JA |
1527 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
1528 | unsigned int cflags) | |
2b188cc1 | 1529 | { |
78e19bbe | 1530 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1531 | unsigned long flags; |
1532 | ||
1533 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1534 | __io_cqring_fill_event(req, res, cflags); |
2b188cc1 | 1535 | io_commit_cqring(ctx); |
c7dae4ba JA |
1536 | /* |
1537 | * If we're the last reference to this request, add to our locked | |
1538 | * free_list cache. | |
1539 | */ | |
1540 | if (refcount_dec_and_test(&req->refs)) { | |
1541 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
1542 | ||
1543 | io_dismantle_req(req); | |
1544 | io_put_task(req->task, 1); | |
1545 | list_add(&req->compl.list, &cs->locked_free_list); | |
1546 | cs->locked_free_nr++; | |
1547 | } else | |
1548 | req = NULL; | |
2b188cc1 JA |
1549 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1550 | ||
8c838788 | 1551 | io_cqring_ev_posted(ctx); |
c7dae4ba JA |
1552 | if (req) { |
1553 | io_queue_next(req); | |
1554 | percpu_ref_put(&ctx->refs); | |
229a7b63 | 1555 | } |
229a7b63 JA |
1556 | } |
1557 | ||
a38d68db | 1558 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1559 | unsigned int cflags) |
229a7b63 | 1560 | { |
a38d68db PB |
1561 | io_clean_op(req); |
1562 | req->result = res; | |
1563 | req->compl.cflags = cflags; | |
e342c807 | 1564 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1565 | } |
1566 | ||
889fca73 PB |
1567 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1568 | long res, unsigned cflags) | |
bcda7baa | 1569 | { |
889fca73 PB |
1570 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1571 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1572 | else |
c7dae4ba | 1573 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1574 | } |
1575 | ||
a38d68db | 1576 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1577 | { |
889fca73 | 1578 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1579 | } |
1580 | ||
c7dae4ba | 1581 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1582 | { |
c7dae4ba JA |
1583 | struct io_submit_state *state = &ctx->submit_state; |
1584 | struct io_comp_state *cs = &state->comp; | |
e5d1bc0a | 1585 | struct io_kiocb *req = NULL; |
0ddf92e8 | 1586 | |
c7dae4ba JA |
1587 | /* |
1588 | * If we have more than a batch's worth of requests in our IRQ side | |
1589 | * locked cache, grab the lock and move them over to our submission | |
1590 | * side cache. | |
1591 | */ | |
1592 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { | |
1593 | spin_lock_irq(&ctx->completion_lock); | |
1594 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1595 | cs->locked_free_nr = 0; | |
1596 | spin_unlock_irq(&ctx->completion_lock); | |
1597 | } | |
0ddf92e8 | 1598 | |
c7dae4ba JA |
1599 | while (!list_empty(&cs->free_list)) { |
1600 | req = list_first_entry(&cs->free_list, struct io_kiocb, | |
1b4c351f JA |
1601 | compl.list); |
1602 | list_del(&req->compl.list); | |
e5d1bc0a PB |
1603 | state->reqs[state->free_reqs++] = req; |
1604 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) | |
1605 | break; | |
1b4c351f JA |
1606 | } |
1607 | ||
e5d1bc0a | 1608 | return req != NULL; |
0ddf92e8 JA |
1609 | } |
1610 | ||
e5d1bc0a | 1611 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1612 | { |
e5d1bc0a PB |
1613 | struct io_submit_state *state = &ctx->submit_state; |
1614 | ||
1615 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1616 | ||
f6b6c7d6 | 1617 | if (!state->free_reqs) { |
291b2821 | 1618 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1619 | int ret; |
1620 | ||
c7dae4ba | 1621 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1622 | goto got_req; |
1623 | ||
bf019da7 PB |
1624 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1625 | state->reqs); | |
fd6fab2c JA |
1626 | |
1627 | /* | |
1628 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1629 | * retry single alloc to be on the safe side. | |
1630 | */ | |
1631 | if (unlikely(ret <= 0)) { | |
1632 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1633 | if (!state->reqs[0]) | |
3893f39f | 1634 | return NULL; |
fd6fab2c JA |
1635 | ret = 1; |
1636 | } | |
291b2821 | 1637 | state->free_reqs = ret; |
2b188cc1 | 1638 | } |
e5d1bc0a | 1639 | got_req: |
291b2821 PB |
1640 | state->free_reqs--; |
1641 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1642 | } |
1643 | ||
8da11c19 PB |
1644 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
1645 | bool fixed) | |
1646 | { | |
36f72fe2 | 1647 | if (!fixed) |
8da11c19 PB |
1648 | fput(file); |
1649 | } | |
1650 | ||
4edf20f9 | 1651 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1652 | { |
3ca405eb | 1653 | io_clean_op(req); |
929a3af9 | 1654 | |
e8c2bc1f JA |
1655 | if (req->async_data) |
1656 | kfree(req->async_data); | |
8da11c19 PB |
1657 | if (req->file) |
1658 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); | |
269bbe5f BM |
1659 | if (req->fixed_rsrc_refs) |
1660 | percpu_ref_put(req->fixed_rsrc_refs); | |
003e8dcc JA |
1661 | if (req->work.creds) { |
1662 | put_cred(req->work.creds); | |
1663 | req->work.creds = NULL; | |
1664 | } | |
f85c310a PB |
1665 | |
1666 | if (req->flags & REQ_F_INFLIGHT) { | |
1667 | struct io_ring_ctx *ctx = req->ctx; | |
f85c310a PB |
1668 | unsigned long flags; |
1669 | ||
1670 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1671 | list_del(&req->inflight_entry); | |
1672 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1673 | req->flags &= ~REQ_F_INFLIGHT; | |
f85c310a | 1674 | } |
e65ef56d JA |
1675 | } |
1676 | ||
b23fcf47 | 1677 | /* must to be called somewhat shortly after putting a request */ |
7c660731 PB |
1678 | static inline void io_put_task(struct task_struct *task, int nr) |
1679 | { | |
1680 | struct io_uring_task *tctx = task->io_uring; | |
1681 | ||
1682 | percpu_counter_sub(&tctx->inflight, nr); | |
1683 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1684 | wake_up(&tctx->wait); | |
1685 | put_task_struct_many(task, nr); | |
1686 | } | |
1687 | ||
216578e5 | 1688 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1689 | { |
51a4cc11 | 1690 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1691 | |
216578e5 | 1692 | io_dismantle_req(req); |
7c660731 | 1693 | io_put_task(req->task, 1); |
c6ca97b3 | 1694 | |
3893f39f | 1695 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1696 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1697 | } |
1698 | ||
f2f87370 PB |
1699 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1700 | { | |
1701 | struct io_kiocb *nxt = req->link; | |
1702 | ||
1703 | req->link = nxt->link; | |
1704 | nxt->link = NULL; | |
1705 | } | |
1706 | ||
c9abd7ad | 1707 | static void io_kill_linked_timeout(struct io_kiocb *req) |
2665abfd | 1708 | { |
a197f664 | 1709 | struct io_ring_ctx *ctx = req->ctx; |
7c86ffee | 1710 | struct io_kiocb *link; |
c9abd7ad PB |
1711 | bool cancelled = false; |
1712 | unsigned long flags; | |
7c86ffee | 1713 | |
c9abd7ad | 1714 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1715 | link = req->link; |
1716 | ||
900fad45 PB |
1717 | /* |
1718 | * Can happen if a linked timeout fired and link had been like | |
1719 | * req -> link t-out -> link t-out [-> ...] | |
1720 | */ | |
c9abd7ad PB |
1721 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1722 | struct io_timeout_data *io = link->async_data; | |
1723 | int ret; | |
7c86ffee | 1724 | |
f2f87370 | 1725 | io_remove_next_linked(req); |
90cd7e42 | 1726 | link->timeout.head = NULL; |
c9abd7ad PB |
1727 | ret = hrtimer_try_to_cancel(&io->timer); |
1728 | if (ret != -1) { | |
1729 | io_cqring_fill_event(link, -ECANCELED); | |
1730 | io_commit_cqring(ctx); | |
1731 | cancelled = true; | |
1732 | } | |
1733 | } | |
7c86ffee | 1734 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
216578e5 | 1735 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
ab0b6451 | 1736 | |
c9abd7ad | 1737 | if (cancelled) { |
7c86ffee | 1738 | io_cqring_ev_posted(ctx); |
c9abd7ad PB |
1739 | io_put_req(link); |
1740 | } | |
7c86ffee PB |
1741 | } |
1742 | ||
9e645e11 | 1743 | |
d148ca4b | 1744 | static void io_fail_links(struct io_kiocb *req) |
9e645e11 | 1745 | { |
f2f87370 | 1746 | struct io_kiocb *link, *nxt; |
2665abfd | 1747 | struct io_ring_ctx *ctx = req->ctx; |
d148ca4b | 1748 | unsigned long flags; |
9e645e11 | 1749 | |
d148ca4b | 1750 | spin_lock_irqsave(&ctx->completion_lock, flags); |
f2f87370 PB |
1751 | link = req->link; |
1752 | req->link = NULL; | |
9e645e11 | 1753 | |
f2f87370 PB |
1754 | while (link) { |
1755 | nxt = link->link; | |
1756 | link->link = NULL; | |
2665abfd | 1757 | |
f2f87370 | 1758 | trace_io_uring_fail_link(req, link); |
7c86ffee | 1759 | io_cqring_fill_event(link, -ECANCELED); |
216578e5 | 1760 | |
1575f21a | 1761 | io_put_req_deferred(link, 2); |
f2f87370 | 1762 | link = nxt; |
9e645e11 | 1763 | } |
2665abfd | 1764 | io_commit_cqring(ctx); |
216578e5 | 1765 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
9e645e11 | 1766 | |
2665abfd | 1767 | io_cqring_ev_posted(ctx); |
9e645e11 JA |
1768 | } |
1769 | ||
3fa5e0f3 | 1770 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1771 | { |
7c86ffee PB |
1772 | if (req->flags & REQ_F_LINK_TIMEOUT) |
1773 | io_kill_linked_timeout(req); | |
944e58bf | 1774 | |
9e645e11 JA |
1775 | /* |
1776 | * If LINK is set, we have dependent requests in this chain. If we | |
1777 | * didn't fail this request, queue the first one up, moving any other | |
1778 | * dependencies to the next request. In case of failure, fail the rest | |
1779 | * of the chain. | |
1780 | */ | |
f2f87370 PB |
1781 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
1782 | struct io_kiocb *nxt = req->link; | |
1783 | ||
1784 | req->link = NULL; | |
1785 | return nxt; | |
1786 | } | |
9b5f7bd9 PB |
1787 | io_fail_links(req); |
1788 | return NULL; | |
4d7dd462 | 1789 | } |
9e645e11 | 1790 | |
f2f87370 | 1791 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1792 | { |
cdbff982 | 1793 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1794 | return NULL; |
1795 | return __io_req_find_next(req); | |
1796 | } | |
1797 | ||
2c32395d PB |
1798 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
1799 | { | |
1800 | if (!ctx) | |
1801 | return; | |
1802 | if (ctx->submit_state.comp.nr) { | |
1803 | mutex_lock(&ctx->uring_lock); | |
1804 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1805 | mutex_unlock(&ctx->uring_lock); | |
1806 | } | |
1807 | percpu_ref_put(&ctx->refs); | |
1808 | } | |
1809 | ||
7cbf1722 | 1810 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1811 | { |
65453d1e | 1812 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1813 | struct io_wq_work_list list; |
1814 | struct io_wq_work_node *node; | |
c2c4c83c | 1815 | |
7cbf1722 JA |
1816 | if (wq_list_empty(&tctx->task_list)) |
1817 | return false; | |
6200b0ae | 1818 | |
0b81e80c | 1819 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1820 | list = tctx->task_list; |
1821 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1822 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1823 | |
7cbf1722 JA |
1824 | node = list.first; |
1825 | while (node) { | |
1826 | struct io_wq_work_node *next = node->next; | |
1827 | struct io_kiocb *req; | |
0ba9c9ed | 1828 | |
7cbf1722 | 1829 | req = container_of(node, struct io_kiocb, io_task_work.node); |
2c32395d PB |
1830 | if (req->ctx != ctx) { |
1831 | ctx_flush_and_put(ctx); | |
1832 | ctx = req->ctx; | |
1833 | percpu_ref_get(&ctx->refs); | |
65453d1e | 1834 | } |
65453d1e | 1835 | |
2c32395d PB |
1836 | req->task_work.func(&req->task_work); |
1837 | node = next; | |
7cbf1722 JA |
1838 | } |
1839 | ||
2c32395d | 1840 | ctx_flush_and_put(ctx); |
7cbf1722 | 1841 | return list.first != NULL; |
c2c4c83c JA |
1842 | } |
1843 | ||
7cbf1722 | 1844 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1845 | { |
7cbf1722 | 1846 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1847 | |
1d5f360d JA |
1848 | clear_bit(0, &tctx->task_state); |
1849 | ||
7cbf1722 JA |
1850 | while (__tctx_task_work(tctx)) |
1851 | cond_resched(); | |
7cbf1722 JA |
1852 | } |
1853 | ||
1854 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, | |
1855 | enum task_work_notify_mode notify) | |
1856 | { | |
1857 | struct io_uring_task *tctx = tsk->io_uring; | |
1858 | struct io_wq_work_node *node, *prev; | |
0b81e80c | 1859 | unsigned long flags; |
7cbf1722 JA |
1860 | int ret; |
1861 | ||
1862 | WARN_ON_ONCE(!tctx); | |
1863 | ||
0b81e80c | 1864 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1865 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1866 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1867 | |
1868 | /* task_work already pending, we're done */ | |
1869 | if (test_bit(0, &tctx->task_state) || | |
1870 | test_and_set_bit(0, &tctx->task_state)) | |
1871 | return 0; | |
1872 | ||
1873 | if (!task_work_add(tsk, &tctx->task_work, notify)) | |
1874 | return 0; | |
1875 | ||
1876 | /* | |
1877 | * Slow path - we failed, find and delete work. if the work is not | |
1878 | * in the list, it got run and we're fine. | |
1879 | */ | |
1880 | ret = 0; | |
0b81e80c | 1881 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1882 | wq_list_for_each(node, prev, &tctx->task_list) { |
1883 | if (&req->io_task_work.node == node) { | |
1884 | wq_list_del(&tctx->task_list, node, prev); | |
1885 | ret = 1; | |
1886 | break; | |
1887 | } | |
1888 | } | |
0b81e80c | 1889 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1890 | clear_bit(0, &tctx->task_state); |
1891 | return ret; | |
1892 | } | |
1893 | ||
355fb9e2 | 1894 | static int io_req_task_work_add(struct io_kiocb *req) |
c2c4c83c JA |
1895 | { |
1896 | struct task_struct *tsk = req->task; | |
1897 | struct io_ring_ctx *ctx = req->ctx; | |
91989c70 JA |
1898 | enum task_work_notify_mode notify; |
1899 | int ret; | |
c2c4c83c | 1900 | |
6200b0ae JA |
1901 | if (tsk->flags & PF_EXITING) |
1902 | return -ESRCH; | |
1903 | ||
c2c4c83c | 1904 | /* |
0ba9c9ed JA |
1905 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
1906 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1907 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1908 | * will do the job. | |
c2c4c83c | 1909 | */ |
91989c70 | 1910 | notify = TWA_NONE; |
355fb9e2 | 1911 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
c2c4c83c JA |
1912 | notify = TWA_SIGNAL; |
1913 | ||
7cbf1722 | 1914 | ret = io_task_work_add(tsk, req, notify); |
c2c4c83c JA |
1915 | if (!ret) |
1916 | wake_up_process(tsk); | |
0ba9c9ed | 1917 | |
c2c4c83c JA |
1918 | return ret; |
1919 | } | |
1920 | ||
eab30c4d | 1921 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
7cbf1722 | 1922 | task_work_func_t cb) |
eab30c4d | 1923 | { |
7c25c0d1 JA |
1924 | struct io_ring_ctx *ctx = req->ctx; |
1925 | struct callback_head *head; | |
eab30c4d PB |
1926 | |
1927 | init_task_work(&req->task_work, cb); | |
7c25c0d1 JA |
1928 | do { |
1929 | head = READ_ONCE(ctx->exit_task_work); | |
1930 | req->task_work.next = head; | |
1931 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); | |
eab30c4d PB |
1932 | } |
1933 | ||
c40f6379 JA |
1934 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
1935 | { | |
1936 | struct io_ring_ctx *ctx = req->ctx; | |
1937 | ||
1938 | spin_lock_irq(&ctx->completion_lock); | |
1939 | io_cqring_fill_event(req, error); | |
1940 | io_commit_cqring(ctx); | |
1941 | spin_unlock_irq(&ctx->completion_lock); | |
1942 | ||
1943 | io_cqring_ev_posted(ctx); | |
1944 | req_set_fail_links(req); | |
1945 | io_double_put_req(req); | |
1946 | } | |
1947 | ||
1948 | static void io_req_task_cancel(struct callback_head *cb) | |
1949 | { | |
1950 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 1951 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 1952 | |
792bb6eb | 1953 | mutex_lock(&ctx->uring_lock); |
a3df7698 | 1954 | __io_req_task_cancel(req, req->result); |
792bb6eb | 1955 | mutex_unlock(&ctx->uring_lock); |
87ceb6a6 | 1956 | percpu_ref_put(&ctx->refs); |
c40f6379 JA |
1957 | } |
1958 | ||
1959 | static void __io_req_task_submit(struct io_kiocb *req) | |
1960 | { | |
1961 | struct io_ring_ctx *ctx = req->ctx; | |
1962 | ||
04fc6c80 | 1963 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 1964 | mutex_lock(&ctx->uring_lock); |
70aacfe6 | 1965 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
c5eef2b9 | 1966 | __io_queue_sqe(req); |
81b6d05c | 1967 | else |
c40f6379 | 1968 | __io_req_task_cancel(req, -EFAULT); |
81b6d05c | 1969 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
1970 | } |
1971 | ||
1972 | static void io_req_task_submit(struct callback_head *cb) | |
1973 | { | |
1974 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
1975 | ||
1976 | __io_req_task_submit(req); | |
1977 | } | |
1978 | ||
1979 | static void io_req_task_queue(struct io_kiocb *req) | |
1980 | { | |
c40f6379 JA |
1981 | int ret; |
1982 | ||
7cbf1722 | 1983 | req->task_work.func = io_req_task_submit; |
355fb9e2 | 1984 | ret = io_req_task_work_add(req); |
c40f6379 | 1985 | if (unlikely(ret)) { |
a3df7698 | 1986 | req->result = -ECANCELED; |
04fc6c80 | 1987 | percpu_ref_get(&req->ctx->refs); |
eab30c4d | 1988 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 | 1989 | } |
c40f6379 JA |
1990 | } |
1991 | ||
a3df7698 PB |
1992 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
1993 | { | |
1994 | percpu_ref_get(&req->ctx->refs); | |
1995 | req->result = ret; | |
1996 | req->task_work.func = io_req_task_cancel; | |
1997 | ||
1998 | if (unlikely(io_req_task_work_add(req))) | |
1999 | io_req_task_work_add_fallback(req, io_req_task_cancel); | |
2000 | } | |
2001 | ||
f2f87370 | 2002 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2003 | { |
9b5f7bd9 | 2004 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2005 | |
2006 | if (nxt) | |
906a8c3f | 2007 | io_req_task_queue(nxt); |
c69f8dbe JL |
2008 | } |
2009 | ||
c3524383 | 2010 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2011 | { |
c3524383 PB |
2012 | io_queue_next(req); |
2013 | __io_free_req(req); | |
2014 | } | |
8766dd51 | 2015 | |
2d6500d4 | 2016 | struct req_batch { |
5af1d13e PB |
2017 | struct task_struct *task; |
2018 | int task_refs; | |
1b4c351f | 2019 | int ctx_refs; |
2d6500d4 PB |
2020 | }; |
2021 | ||
5af1d13e PB |
2022 | static inline void io_init_req_batch(struct req_batch *rb) |
2023 | { | |
5af1d13e | 2024 | rb->task_refs = 0; |
9ae72463 | 2025 | rb->ctx_refs = 0; |
5af1d13e PB |
2026 | rb->task = NULL; |
2027 | } | |
2028 | ||
2d6500d4 PB |
2029 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2030 | struct req_batch *rb) | |
2031 | { | |
6e833d53 | 2032 | if (rb->task) |
7c660731 | 2033 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2034 | if (rb->ctx_refs) |
2035 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2036 | } |
2037 | ||
6ff119a6 PB |
2038 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2039 | struct io_submit_state *state) | |
2d6500d4 | 2040 | { |
f2f87370 | 2041 | io_queue_next(req); |
2d6500d4 | 2042 | |
e3bc8e9d | 2043 | if (req->task != rb->task) { |
7c660731 PB |
2044 | if (rb->task) |
2045 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2046 | rb->task = req->task; |
2047 | rb->task_refs = 0; | |
5af1d13e | 2048 | } |
e3bc8e9d | 2049 | rb->task_refs++; |
9ae72463 | 2050 | rb->ctx_refs++; |
5af1d13e | 2051 | |
4edf20f9 | 2052 | io_dismantle_req(req); |
bd759045 | 2053 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2054 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2055 | else |
2056 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2057 | } |
2058 | ||
905c172f PB |
2059 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2060 | struct io_ring_ctx *ctx) | |
2061 | { | |
2062 | int i, nr = cs->nr; | |
2063 | struct io_kiocb *req; | |
2064 | struct req_batch rb; | |
2065 | ||
2066 | io_init_req_batch(&rb); | |
2067 | spin_lock_irq(&ctx->completion_lock); | |
2068 | for (i = 0; i < nr; i++) { | |
2069 | req = cs->reqs[i]; | |
2070 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2071 | } | |
2072 | io_commit_cqring(ctx); | |
2073 | spin_unlock_irq(&ctx->completion_lock); | |
2074 | ||
2075 | io_cqring_ev_posted(ctx); | |
2076 | for (i = 0; i < nr; i++) { | |
2077 | req = cs->reqs[i]; | |
2078 | ||
2079 | /* submission and completion refs */ | |
2080 | if (refcount_sub_and_test(2, &req->refs)) | |
6ff119a6 | 2081 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2082 | } |
2083 | ||
2084 | io_req_free_batch_finish(ctx, &rb); | |
2085 | cs->nr = 0; | |
7a743e22 PB |
2086 | } |
2087 | ||
ba816ad6 JA |
2088 | /* |
2089 | * Drop reference to request, return next in chain (if there is one) if this | |
2090 | * was the last reference to this request. | |
2091 | */ | |
9b5f7bd9 | 2092 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2093 | { |
9b5f7bd9 PB |
2094 | struct io_kiocb *nxt = NULL; |
2095 | ||
2a44f467 | 2096 | if (refcount_dec_and_test(&req->refs)) { |
9b5f7bd9 | 2097 | nxt = io_req_find_next(req); |
4d7dd462 | 2098 | __io_free_req(req); |
2a44f467 | 2099 | } |
9b5f7bd9 | 2100 | return nxt; |
2b188cc1 JA |
2101 | } |
2102 | ||
e65ef56d JA |
2103 | static void io_put_req(struct io_kiocb *req) |
2104 | { | |
2105 | if (refcount_dec_and_test(&req->refs)) | |
2106 | io_free_req(req); | |
2b188cc1 JA |
2107 | } |
2108 | ||
216578e5 PB |
2109 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2110 | { | |
2111 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2112 | ||
2113 | io_free_req(req); | |
2114 | } | |
2115 | ||
2116 | static void io_free_req_deferred(struct io_kiocb *req) | |
2117 | { | |
2118 | int ret; | |
2119 | ||
7cbf1722 | 2120 | req->task_work.func = io_put_req_deferred_cb; |
355fb9e2 | 2121 | ret = io_req_task_work_add(req); |
eab30c4d PB |
2122 | if (unlikely(ret)) |
2123 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); | |
216578e5 PB |
2124 | } |
2125 | ||
2126 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2127 | { | |
2128 | if (refcount_sub_and_test(refs, &req->refs)) | |
2129 | io_free_req_deferred(req); | |
2130 | } | |
2131 | ||
978db57e JA |
2132 | static void io_double_put_req(struct io_kiocb *req) |
2133 | { | |
2134 | /* drop both submit and complete references */ | |
2135 | if (refcount_sub_and_test(2, &req->refs)) | |
2136 | io_free_req(req); | |
2137 | } | |
2138 | ||
6c503150 | 2139 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2140 | { |
2141 | /* See comment at the top of this file */ | |
2142 | smp_rmb(); | |
e23de15f | 2143 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2144 | } |
2145 | ||
fb5ccc98 PB |
2146 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2147 | { | |
2148 | struct io_rings *rings = ctx->rings; | |
2149 | ||
2150 | /* make sure SQ entry isn't read before tail */ | |
2151 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2152 | } | |
2153 | ||
8ff069bf | 2154 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2155 | { |
8ff069bf | 2156 | unsigned int cflags; |
e94f141b | 2157 | |
bcda7baa JA |
2158 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2159 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2160 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2161 | kfree(kbuf); |
2162 | return cflags; | |
e94f141b JA |
2163 | } |
2164 | ||
8ff069bf | 2165 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2166 | { |
4d954c25 | 2167 | struct io_buffer *kbuf; |
bcda7baa | 2168 | |
4d954c25 | 2169 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2170 | return io_put_kbuf(req, kbuf); |
2171 | } | |
2172 | ||
4c6e277c JA |
2173 | static inline bool io_run_task_work(void) |
2174 | { | |
6200b0ae JA |
2175 | /* |
2176 | * Not safe to run on exiting task, and the task_work handling will | |
2177 | * not add work to such a task. | |
2178 | */ | |
2179 | if (unlikely(current->flags & PF_EXITING)) | |
2180 | return false; | |
4c6e277c JA |
2181 | if (current->task_works) { |
2182 | __set_current_state(TASK_RUNNING); | |
2183 | task_work_run(); | |
2184 | return true; | |
2185 | } | |
2186 | ||
2187 | return false; | |
bcda7baa JA |
2188 | } |
2189 | ||
def596e9 JA |
2190 | /* |
2191 | * Find and free completed poll iocbs | |
2192 | */ | |
2193 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2194 | struct list_head *done) | |
2195 | { | |
8237e045 | 2196 | struct req_batch rb; |
def596e9 | 2197 | struct io_kiocb *req; |
bbde017a XW |
2198 | |
2199 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2200 | smp_rmb(); | |
def596e9 | 2201 | |
5af1d13e | 2202 | io_init_req_batch(&rb); |
def596e9 | 2203 | while (!list_empty(done)) { |
bcda7baa JA |
2204 | int cflags = 0; |
2205 | ||
d21ffe7e | 2206 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2207 | list_del(&req->inflight_entry); |
2208 | ||
bbde017a XW |
2209 | if (READ_ONCE(req->result) == -EAGAIN) { |
2210 | req->iopoll_completed = 0; | |
23faba36 | 2211 | if (io_rw_reissue(req)) |
f161340d | 2212 | continue; |
bbde017a | 2213 | } |
def596e9 | 2214 | |
bcda7baa | 2215 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2216 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2217 | |
2218 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2219 | (*nr_events)++; |
2220 | ||
c3524383 | 2221 | if (refcount_dec_and_test(&req->refs)) |
6ff119a6 | 2222 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2223 | } |
def596e9 | 2224 | |
09bb8394 | 2225 | io_commit_cqring(ctx); |
80c18e4a | 2226 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2227 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2228 | } |
2229 | ||
def596e9 JA |
2230 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2231 | long min) | |
2232 | { | |
2233 | struct io_kiocb *req, *tmp; | |
2234 | LIST_HEAD(done); | |
2235 | bool spin; | |
2236 | int ret; | |
2237 | ||
2238 | /* | |
2239 | * Only spin for completions if we don't have multiple devices hanging | |
2240 | * off our complete list, and we're under the requested amount. | |
2241 | */ | |
2242 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2243 | ||
2244 | ret = 0; | |
d21ffe7e | 2245 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2246 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2247 | |
2248 | /* | |
581f9810 BM |
2249 | * Move completed and retryable entries to our local lists. |
2250 | * If we find a request that requires polling, break out | |
2251 | * and complete those lists first, if we have entries there. | |
def596e9 | 2252 | */ |
65a6543d | 2253 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2254 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2255 | continue; |
2256 | } | |
2257 | if (!list_empty(&done)) | |
2258 | break; | |
2259 | ||
2260 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2261 | if (ret < 0) | |
2262 | break; | |
2263 | ||
3aadc23e PB |
2264 | /* iopoll may have completed current req */ |
2265 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2266 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2267 | |
def596e9 JA |
2268 | if (ret && spin) |
2269 | spin = false; | |
2270 | ret = 0; | |
2271 | } | |
2272 | ||
2273 | if (!list_empty(&done)) | |
2274 | io_iopoll_complete(ctx, nr_events, &done); | |
2275 | ||
2276 | return ret; | |
2277 | } | |
2278 | ||
2279 | /* | |
d195a66e | 2280 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2281 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2282 | * as a non-spinning completion check. | |
2283 | */ | |
2284 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2285 | long min) | |
2286 | { | |
540e32a0 | 2287 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2288 | int ret; |
2289 | ||
2290 | ret = io_do_iopoll(ctx, nr_events, min); | |
2291 | if (ret < 0) | |
2292 | return ret; | |
eba0a4dd | 2293 | if (*nr_events >= min) |
def596e9 JA |
2294 | return 0; |
2295 | } | |
2296 | ||
2297 | return 1; | |
2298 | } | |
2299 | ||
2300 | /* | |
2301 | * We can't just wait for polled events to come to us, we have to actively | |
2302 | * find and complete them. | |
2303 | */ | |
b2edc0a7 | 2304 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2305 | { |
2306 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2307 | return; | |
2308 | ||
2309 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2310 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2311 | unsigned int nr_events = 0; |
2312 | ||
b2edc0a7 | 2313 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2314 | |
b2edc0a7 PB |
2315 | /* let it sleep and repeat later if can't complete a request */ |
2316 | if (nr_events == 0) | |
2317 | break; | |
08f5439f JA |
2318 | /* |
2319 | * Ensure we allow local-to-the-cpu processing to take place, | |
2320 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2321 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2322 | */ |
3fcee5a6 PB |
2323 | if (need_resched()) { |
2324 | mutex_unlock(&ctx->uring_lock); | |
2325 | cond_resched(); | |
2326 | mutex_lock(&ctx->uring_lock); | |
2327 | } | |
def596e9 JA |
2328 | } |
2329 | mutex_unlock(&ctx->uring_lock); | |
2330 | } | |
2331 | ||
7668b92a | 2332 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2333 | { |
7668b92a | 2334 | unsigned int nr_events = 0; |
2b2ed975 | 2335 | int iters = 0, ret = 0; |
500f9fba | 2336 | |
c7849be9 XW |
2337 | /* |
2338 | * We disallow the app entering submit/complete with polling, but we | |
2339 | * still need to lock the ring to prevent racing with polled issue | |
2340 | * that got punted to a workqueue. | |
2341 | */ | |
2342 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2343 | do { |
a3a0e43f JA |
2344 | /* |
2345 | * Don't enter poll loop if we already have events pending. | |
2346 | * If we do, we can potentially be spinning for commands that | |
2347 | * already triggered a CQE (eg in error). | |
2348 | */ | |
6c503150 PB |
2349 | if (test_bit(0, &ctx->cq_check_overflow)) |
2350 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); | |
2351 | if (io_cqring_events(ctx)) | |
a3a0e43f JA |
2352 | break; |
2353 | ||
500f9fba JA |
2354 | /* |
2355 | * If a submit got punted to a workqueue, we can have the | |
2356 | * application entering polling for a command before it gets | |
2357 | * issued. That app will hold the uring_lock for the duration | |
2358 | * of the poll right here, so we need to take a breather every | |
2359 | * now and then to ensure that the issue has a chance to add | |
2360 | * the poll to the issued list. Otherwise we can spin here | |
2361 | * forever, while the workqueue is stuck trying to acquire the | |
2362 | * very same mutex. | |
2363 | */ | |
2364 | if (!(++iters & 7)) { | |
2365 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2366 | io_run_task_work(); |
500f9fba JA |
2367 | mutex_lock(&ctx->uring_lock); |
2368 | } | |
2369 | ||
7668b92a | 2370 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2371 | if (ret <= 0) |
2372 | break; | |
2373 | ret = 0; | |
7668b92a | 2374 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2375 | |
500f9fba | 2376 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2377 | return ret; |
2378 | } | |
2379 | ||
491381ce | 2380 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2381 | { |
491381ce JA |
2382 | /* |
2383 | * Tell lockdep we inherited freeze protection from submission | |
2384 | * thread. | |
2385 | */ | |
2386 | if (req->flags & REQ_F_ISREG) { | |
2387 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 2388 | |
491381ce | 2389 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 2390 | } |
491381ce | 2391 | file_end_write(req->file); |
2b188cc1 JA |
2392 | } |
2393 | ||
b63534c4 | 2394 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2395 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 JA |
2396 | { |
2397 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
4a245479 | 2398 | int rw, ret; |
b63534c4 | 2399 | struct iov_iter iter; |
b63534c4 | 2400 | |
dc2a6e9a PB |
2401 | /* already prepared */ |
2402 | if (req->async_data) | |
2403 | return true; | |
b63534c4 JA |
2404 | |
2405 | switch (req->opcode) { | |
2406 | case IORING_OP_READV: | |
2407 | case IORING_OP_READ_FIXED: | |
2408 | case IORING_OP_READ: | |
2409 | rw = READ; | |
2410 | break; | |
2411 | case IORING_OP_WRITEV: | |
2412 | case IORING_OP_WRITE_FIXED: | |
2413 | case IORING_OP_WRITE: | |
2414 | rw = WRITE; | |
2415 | break; | |
2416 | default: | |
2417 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", | |
2418 | req->opcode); | |
dc2a6e9a | 2419 | return false; |
b63534c4 JA |
2420 | } |
2421 | ||
dc2a6e9a PB |
2422 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
2423 | if (ret < 0) | |
2424 | return false; | |
6bf985dc | 2425 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
b63534c4 | 2426 | } |
b63534c4 | 2427 | |
3e6a0d3c | 2428 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2429 | { |
355afaeb | 2430 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2431 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2432 | |
355afaeb JA |
2433 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2434 | return false; | |
3e6a0d3c JA |
2435 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2436 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2437 | return false; |
7c977a58 JA |
2438 | /* |
2439 | * If ref is dying, we might be running poll reap from the exit work. | |
2440 | * Don't attempt to reissue from that path, just let it fail with | |
2441 | * -EAGAIN. | |
2442 | */ | |
3e6a0d3c JA |
2443 | if (percpu_ref_is_dying(&ctx->refs)) |
2444 | return false; | |
2445 | return true; | |
2446 | } | |
2447 | #endif | |
2448 | ||
2449 | static bool io_rw_reissue(struct io_kiocb *req) | |
2450 | { | |
2451 | #ifdef CONFIG_BLOCK | |
2452 | if (!io_rw_should_reissue(req)) | |
7c977a58 | 2453 | return false; |
b63534c4 | 2454 | |
55e6ac1e PB |
2455 | lockdep_assert_held(&req->ctx->uring_lock); |
2456 | ||
37d1e2e3 | 2457 | if (io_resubmit_prep(req)) { |
fdee946d JA |
2458 | refcount_inc(&req->refs); |
2459 | io_queue_async_work(req); | |
b63534c4 | 2460 | return true; |
fdee946d | 2461 | } |
dc2a6e9a | 2462 | req_set_fail_links(req); |
b63534c4 JA |
2463 | #endif |
2464 | return false; | |
2465 | } | |
2466 | ||
a1d7c393 | 2467 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2468 | unsigned int issue_flags) |
a1d7c393 | 2469 | { |
2f8e45f1 PB |
2470 | int cflags = 0; |
2471 | ||
23faba36 PB |
2472 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
2473 | return; | |
2f8e45f1 PB |
2474 | if (res != req->result) |
2475 | req_set_fail_links(req); | |
23faba36 | 2476 | |
2f8e45f1 PB |
2477 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2478 | kiocb_end_write(req); | |
2479 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2480 | cflags = io_put_rw_kbuf(req); | |
2481 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2482 | } |
2483 | ||
2484 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2485 | { | |
9adbd45d | 2486 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2487 | |
889fca73 | 2488 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2489 | } |
2490 | ||
def596e9 JA |
2491 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2492 | { | |
9adbd45d | 2493 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2494 | |
3e6a0d3c JA |
2495 | #ifdef CONFIG_BLOCK |
2496 | /* Rewind iter, if we have one. iopoll path resubmits as usual */ | |
2497 | if (res == -EAGAIN && io_rw_should_reissue(req)) { | |
2498 | struct io_async_rw *rw = req->async_data; | |
2499 | ||
2500 | if (rw) | |
2501 | iov_iter_revert(&rw->iter, | |
2502 | req->result - iov_iter_count(&rw->iter)); | |
2503 | else if (!io_resubmit_prep(req)) | |
2504 | res = -EIO; | |
2505 | } | |
2506 | #endif | |
2507 | ||
491381ce JA |
2508 | if (kiocb->ki_flags & IOCB_WRITE) |
2509 | kiocb_end_write(req); | |
def596e9 | 2510 | |
2d7d6792 | 2511 | if (res != -EAGAIN && res != req->result) |
4e88d6e7 | 2512 | req_set_fail_links(req); |
bbde017a XW |
2513 | |
2514 | WRITE_ONCE(req->result, res); | |
2515 | /* order with io_poll_complete() checking ->result */ | |
cd664b0e PB |
2516 | smp_wmb(); |
2517 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2518 | } |
2519 | ||
2520 | /* | |
2521 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2522 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2523 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2524 | * accessing the kiocb cookie. | |
2525 | */ | |
2e9dbe90 | 2526 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2527 | { |
2528 | struct io_ring_ctx *ctx = req->ctx; | |
2529 | ||
2530 | /* | |
2531 | * Track whether we have multiple files in our lists. This will impact | |
2532 | * how we do polling eventually, not spinning if we're on potentially | |
2533 | * different devices. | |
2534 | */ | |
540e32a0 | 2535 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2536 | ctx->poll_multi_file = false; |
2537 | } else if (!ctx->poll_multi_file) { | |
2538 | struct io_kiocb *list_req; | |
2539 | ||
540e32a0 | 2540 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2541 | inflight_entry); |
9adbd45d | 2542 | if (list_req->file != req->file) |
def596e9 JA |
2543 | ctx->poll_multi_file = true; |
2544 | } | |
2545 | ||
2546 | /* | |
2547 | * For fast devices, IO may have already completed. If it has, add | |
2548 | * it to the front so we find it first. | |
2549 | */ | |
65a6543d | 2550 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2551 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2552 | else |
d21ffe7e | 2553 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2554 | |
2e9dbe90 XW |
2555 | /* |
2556 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2557 | * task context or in io worker task context. If current task context is | |
2558 | * sq thread, we don't need to check whether should wake up sq thread. | |
2559 | */ | |
2560 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2561 | wq_has_sleeper(&ctx->sq_data->wait)) |
2562 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2563 | } |
2564 | ||
9f13c35b PB |
2565 | static inline void io_state_file_put(struct io_submit_state *state) |
2566 | { | |
02b23a9a PB |
2567 | if (state->file_refs) { |
2568 | fput_many(state->file, state->file_refs); | |
2569 | state->file_refs = 0; | |
2570 | } | |
9a56a232 JA |
2571 | } |
2572 | ||
2573 | /* | |
2574 | * Get as many references to a file as we have IOs left in this submission, | |
2575 | * assuming most submissions are for one file, or at least that each file | |
2576 | * has more than one submission. | |
2577 | */ | |
8da11c19 | 2578 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2579 | { |
2580 | if (!state) | |
2581 | return fget(fd); | |
2582 | ||
6e1271e6 | 2583 | if (state->file_refs) { |
9a56a232 | 2584 | if (state->fd == fd) { |
6e1271e6 | 2585 | state->file_refs--; |
9a56a232 JA |
2586 | return state->file; |
2587 | } | |
02b23a9a | 2588 | io_state_file_put(state); |
9a56a232 JA |
2589 | } |
2590 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2591 | if (unlikely(!state->file)) |
9a56a232 JA |
2592 | return NULL; |
2593 | ||
2594 | state->fd = fd; | |
6e1271e6 | 2595 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2596 | return state->file; |
2597 | } | |
2598 | ||
4503b767 JA |
2599 | static bool io_bdev_nowait(struct block_device *bdev) |
2600 | { | |
9ba0d0c8 | 2601 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2602 | } |
2603 | ||
2b188cc1 JA |
2604 | /* |
2605 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2606 | * any file. For now, just ensure that anything potentially problematic is done | |
2607 | * inline. | |
2608 | */ | |
af197f50 | 2609 | static bool io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2610 | { |
2611 | umode_t mode = file_inode(file)->i_mode; | |
2612 | ||
4503b767 | 2613 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2614 | if (IS_ENABLED(CONFIG_BLOCK) && |
2615 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2616 | return true; |
2617 | return false; | |
2618 | } | |
2619 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2620 | return true; |
4503b767 | 2621 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2622 | if (IS_ENABLED(CONFIG_BLOCK) && |
2623 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2624 | file->f_op != &io_uring_fops) |
2625 | return true; | |
2626 | return false; | |
2627 | } | |
2b188cc1 | 2628 | |
c5b85625 JA |
2629 | /* any ->read/write should understand O_NONBLOCK */ |
2630 | if (file->f_flags & O_NONBLOCK) | |
2631 | return true; | |
2632 | ||
af197f50 JA |
2633 | if (!(file->f_mode & FMODE_NOWAIT)) |
2634 | return false; | |
2635 | ||
2636 | if (rw == READ) | |
2637 | return file->f_op->read_iter != NULL; | |
2638 | ||
2639 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2640 | } |
2641 | ||
a88fc400 | 2642 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2643 | { |
def596e9 | 2644 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2645 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2646 | struct file *file = req->file; |
09bb8394 JA |
2647 | unsigned ioprio; |
2648 | int ret; | |
2b188cc1 | 2649 | |
75c668cd | 2650 | if (S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2651 | req->flags |= REQ_F_ISREG; |
2652 | ||
2b188cc1 | 2653 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2654 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2655 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2656 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2657 | } |
2b188cc1 | 2658 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2659 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2660 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2661 | if (unlikely(ret)) | |
2662 | return ret; | |
2b188cc1 | 2663 | |
75c668cd PB |
2664 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2665 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2666 | req->flags |= REQ_F_NOWAIT; | |
2667 | ||
2b188cc1 JA |
2668 | ioprio = READ_ONCE(sqe->ioprio); |
2669 | if (ioprio) { | |
2670 | ret = ioprio_check_cap(ioprio); | |
2671 | if (ret) | |
09bb8394 | 2672 | return ret; |
2b188cc1 JA |
2673 | |
2674 | kiocb->ki_ioprio = ioprio; | |
2675 | } else | |
2676 | kiocb->ki_ioprio = get_current_ioprio(); | |
2677 | ||
def596e9 | 2678 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2679 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2680 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2681 | return -EOPNOTSUPP; |
2b188cc1 | 2682 | |
def596e9 JA |
2683 | kiocb->ki_flags |= IOCB_HIPRI; |
2684 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2685 | req->iopoll_completed = 0; |
def596e9 | 2686 | } else { |
09bb8394 JA |
2687 | if (kiocb->ki_flags & IOCB_HIPRI) |
2688 | return -EINVAL; | |
def596e9 JA |
2689 | kiocb->ki_complete = io_complete_rw; |
2690 | } | |
9adbd45d | 2691 | |
3529d8c2 JA |
2692 | req->rw.addr = READ_ONCE(sqe->addr); |
2693 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2694 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2695 | return 0; |
2b188cc1 JA |
2696 | } |
2697 | ||
2698 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2699 | { | |
2700 | switch (ret) { | |
2701 | case -EIOCBQUEUED: | |
2702 | break; | |
2703 | case -ERESTARTSYS: | |
2704 | case -ERESTARTNOINTR: | |
2705 | case -ERESTARTNOHAND: | |
2706 | case -ERESTART_RESTARTBLOCK: | |
2707 | /* | |
2708 | * We can't just restart the syscall, since previously | |
2709 | * submitted sqes may already be in progress. Just fail this | |
2710 | * IO with EINTR. | |
2711 | */ | |
2712 | ret = -EINTR; | |
df561f66 | 2713 | fallthrough; |
2b188cc1 JA |
2714 | default: |
2715 | kiocb->ki_complete(kiocb, ret, 0); | |
2716 | } | |
2717 | } | |
2718 | ||
a1d7c393 | 2719 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2720 | unsigned int issue_flags) |
ba816ad6 | 2721 | { |
ba04291e | 2722 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2723 | struct io_async_rw *io = req->async_data; |
ba04291e | 2724 | |
227c0c96 | 2725 | /* add previously done IO, if any */ |
e8c2bc1f | 2726 | if (io && io->bytes_done > 0) { |
227c0c96 | 2727 | if (ret < 0) |
e8c2bc1f | 2728 | ret = io->bytes_done; |
227c0c96 | 2729 | else |
e8c2bc1f | 2730 | ret += io->bytes_done; |
227c0c96 JA |
2731 | } |
2732 | ||
ba04291e JA |
2733 | if (req->flags & REQ_F_CUR_POS) |
2734 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2735 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2736 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2737 | else |
2738 | io_rw_done(kiocb, ret); | |
2739 | } | |
2740 | ||
847595de | 2741 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2742 | { |
9adbd45d JA |
2743 | struct io_ring_ctx *ctx = req->ctx; |
2744 | size_t len = req->rw.len; | |
edafccee | 2745 | struct io_mapped_ubuf *imu; |
4be1c615 | 2746 | u16 index, buf_index = req->buf_index; |
edafccee JA |
2747 | size_t offset; |
2748 | u64 buf_addr; | |
2749 | ||
edafccee JA |
2750 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2751 | return -EFAULT; | |
edafccee JA |
2752 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2753 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2754 | buf_addr = req->rw.addr; |
edafccee JA |
2755 | |
2756 | /* overflow */ | |
2757 | if (buf_addr + len < buf_addr) | |
2758 | return -EFAULT; | |
2759 | /* not inside the mapped region */ | |
2760 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
2761 | return -EFAULT; | |
2762 | ||
2763 | /* | |
2764 | * May not be a start of buffer, set size appropriately | |
2765 | * and advance us to the beginning. | |
2766 | */ | |
2767 | offset = buf_addr - imu->ubuf; | |
2768 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2769 | |
2770 | if (offset) { | |
2771 | /* | |
2772 | * Don't use iov_iter_advance() here, as it's really slow for | |
2773 | * using the latter parts of a big fixed buffer - it iterates | |
2774 | * over each segment manually. We can cheat a bit here, because | |
2775 | * we know that: | |
2776 | * | |
2777 | * 1) it's a BVEC iter, we set it up | |
2778 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2779 | * first and last bvec | |
2780 | * | |
2781 | * So just find our index, and adjust the iterator afterwards. | |
2782 | * If the offset is within the first bvec (or the whole first | |
2783 | * bvec, just use iov_iter_advance(). This makes it easier | |
2784 | * since we can just skip the first segment, which may not | |
2785 | * be PAGE_SIZE aligned. | |
2786 | */ | |
2787 | const struct bio_vec *bvec = imu->bvec; | |
2788 | ||
2789 | if (offset <= bvec->bv_len) { | |
2790 | iov_iter_advance(iter, offset); | |
2791 | } else { | |
2792 | unsigned long seg_skip; | |
2793 | ||
2794 | /* skip first vec */ | |
2795 | offset -= bvec->bv_len; | |
2796 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2797 | ||
2798 | iter->bvec = bvec + seg_skip; | |
2799 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2800 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2801 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2802 | } |
2803 | } | |
2804 | ||
847595de | 2805 | return 0; |
edafccee JA |
2806 | } |
2807 | ||
bcda7baa JA |
2808 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2809 | { | |
2810 | if (needs_lock) | |
2811 | mutex_unlock(&ctx->uring_lock); | |
2812 | } | |
2813 | ||
2814 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2815 | { | |
2816 | /* | |
2817 | * "Normal" inline submissions always hold the uring_lock, since we | |
2818 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2819 | * The only exception is when we've detached the request and issue it | |
2820 | * from an async worker thread, grab the lock for that case. | |
2821 | */ | |
2822 | if (needs_lock) | |
2823 | mutex_lock(&ctx->uring_lock); | |
2824 | } | |
2825 | ||
2826 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2827 | int bgid, struct io_buffer *kbuf, | |
2828 | bool needs_lock) | |
2829 | { | |
2830 | struct io_buffer *head; | |
2831 | ||
2832 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2833 | return kbuf; | |
2834 | ||
2835 | io_ring_submit_lock(req->ctx, needs_lock); | |
2836 | ||
2837 | lockdep_assert_held(&req->ctx->uring_lock); | |
2838 | ||
2839 | head = idr_find(&req->ctx->io_buffer_idr, bgid); | |
2840 | if (head) { | |
2841 | if (!list_empty(&head->list)) { | |
2842 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2843 | list); | |
2844 | list_del(&kbuf->list); | |
2845 | } else { | |
2846 | kbuf = head; | |
2847 | idr_remove(&req->ctx->io_buffer_idr, bgid); | |
2848 | } | |
2849 | if (*len > kbuf->len) | |
2850 | *len = kbuf->len; | |
2851 | } else { | |
2852 | kbuf = ERR_PTR(-ENOBUFS); | |
2853 | } | |
2854 | ||
2855 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2856 | ||
2857 | return kbuf; | |
2858 | } | |
2859 | ||
4d954c25 JA |
2860 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2861 | bool needs_lock) | |
2862 | { | |
2863 | struct io_buffer *kbuf; | |
4f4eeba8 | 2864 | u16 bgid; |
4d954c25 JA |
2865 | |
2866 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2867 | bgid = req->buf_index; |
4d954c25 JA |
2868 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2869 | if (IS_ERR(kbuf)) | |
2870 | return kbuf; | |
2871 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2872 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2873 | return u64_to_user_ptr(kbuf->addr); | |
2874 | } | |
2875 | ||
2876 | #ifdef CONFIG_COMPAT | |
2877 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2878 | bool needs_lock) | |
2879 | { | |
2880 | struct compat_iovec __user *uiov; | |
2881 | compat_ssize_t clen; | |
2882 | void __user *buf; | |
2883 | ssize_t len; | |
2884 | ||
2885 | uiov = u64_to_user_ptr(req->rw.addr); | |
2886 | if (!access_ok(uiov, sizeof(*uiov))) | |
2887 | return -EFAULT; | |
2888 | if (__get_user(clen, &uiov->iov_len)) | |
2889 | return -EFAULT; | |
2890 | if (clen < 0) | |
2891 | return -EINVAL; | |
2892 | ||
2893 | len = clen; | |
2894 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2895 | if (IS_ERR(buf)) | |
2896 | return PTR_ERR(buf); | |
2897 | iov[0].iov_base = buf; | |
2898 | iov[0].iov_len = (compat_size_t) len; | |
2899 | return 0; | |
2900 | } | |
2901 | #endif | |
2902 | ||
2903 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2904 | bool needs_lock) | |
2905 | { | |
2906 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2907 | void __user *buf; | |
2908 | ssize_t len; | |
2909 | ||
2910 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2911 | return -EFAULT; | |
2912 | ||
2913 | len = iov[0].iov_len; | |
2914 | if (len < 0) | |
2915 | return -EINVAL; | |
2916 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2917 | if (IS_ERR(buf)) | |
2918 | return PTR_ERR(buf); | |
2919 | iov[0].iov_base = buf; | |
2920 | iov[0].iov_len = len; | |
2921 | return 0; | |
2922 | } | |
2923 | ||
2924 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2925 | bool needs_lock) | |
2926 | { | |
dddb3e26 JA |
2927 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2928 | struct io_buffer *kbuf; | |
2929 | ||
2930 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2931 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2932 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2933 | return 0; |
dddb3e26 | 2934 | } |
dd201662 | 2935 | if (req->rw.len != 1) |
4d954c25 JA |
2936 | return -EINVAL; |
2937 | ||
2938 | #ifdef CONFIG_COMPAT | |
2939 | if (req->ctx->compat) | |
2940 | return io_compat_import(req, iov, needs_lock); | |
2941 | #endif | |
2942 | ||
2943 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2944 | } | |
2945 | ||
847595de PB |
2946 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2947 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2948 | { |
9adbd45d JA |
2949 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2950 | size_t sqe_len = req->rw.len; | |
847595de | 2951 | u8 opcode = req->opcode; |
4d954c25 | 2952 | ssize_t ret; |
edafccee | 2953 | |
7d009165 | 2954 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2955 | *iovec = NULL; |
9adbd45d | 2956 | return io_import_fixed(req, rw, iter); |
edafccee | 2957 | } |
2b188cc1 | 2958 | |
bcda7baa | 2959 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2960 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2961 | return -EINVAL; |
2962 | ||
3a6820f2 | 2963 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2964 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2965 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2966 | if (IS_ERR(buf)) |
4d954c25 | 2967 | return PTR_ERR(buf); |
3f9d6441 | 2968 | req->rw.len = sqe_len; |
bcda7baa JA |
2969 | } |
2970 | ||
3a6820f2 JA |
2971 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
2972 | *iovec = NULL; | |
10fc72e4 | 2973 | return ret; |
3a6820f2 JA |
2974 | } |
2975 | ||
4d954c25 JA |
2976 | if (req->flags & REQ_F_BUFFER_SELECT) { |
2977 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
2978 | if (!ret) |
2979 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
2980 | *iovec = NULL; |
2981 | return ret; | |
2982 | } | |
2983 | ||
89cd35c5 CH |
2984 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
2985 | req->ctx->compat); | |
2b188cc1 JA |
2986 | } |
2987 | ||
0fef9483 JA |
2988 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
2989 | { | |
5b09e37e | 2990 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
2991 | } |
2992 | ||
31b51510 | 2993 | /* |
32960613 JA |
2994 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
2995 | * by looping over ->read() or ->write() manually. | |
31b51510 | 2996 | */ |
4017eb91 | 2997 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 2998 | { |
4017eb91 JA |
2999 | struct kiocb *kiocb = &req->rw.kiocb; |
3000 | struct file *file = req->file; | |
32960613 JA |
3001 | ssize_t ret = 0; |
3002 | ||
3003 | /* | |
3004 | * Don't support polled IO through this interface, and we can't | |
3005 | * support non-blocking either. For the latter, this just causes | |
3006 | * the kiocb to be handled from an async context. | |
3007 | */ | |
3008 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3009 | return -EOPNOTSUPP; | |
3010 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3011 | return -EAGAIN; | |
3012 | ||
3013 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3014 | struct iovec iovec; |
32960613 JA |
3015 | ssize_t nr; |
3016 | ||
311ae9e1 PB |
3017 | if (!iov_iter_is_bvec(iter)) { |
3018 | iovec = iov_iter_iovec(iter); | |
3019 | } else { | |
4017eb91 JA |
3020 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3021 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3022 | } |
3023 | ||
32960613 JA |
3024 | if (rw == READ) { |
3025 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3026 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3027 | } else { |
3028 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3029 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3030 | } |
3031 | ||
3032 | if (nr < 0) { | |
3033 | if (!ret) | |
3034 | ret = nr; | |
3035 | break; | |
3036 | } | |
3037 | ret += nr; | |
3038 | if (nr != iovec.iov_len) | |
3039 | break; | |
4017eb91 JA |
3040 | req->rw.len -= nr; |
3041 | req->rw.addr += nr; | |
32960613 JA |
3042 | iov_iter_advance(iter, nr); |
3043 | } | |
3044 | ||
3045 | return ret; | |
3046 | } | |
3047 | ||
ff6165b2 JA |
3048 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3049 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3050 | { |
e8c2bc1f | 3051 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3052 | |
ff6165b2 | 3053 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3054 | rw->free_iovec = iovec; |
227c0c96 | 3055 | rw->bytes_done = 0; |
ff6165b2 | 3056 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3057 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3058 | return; |
b64e3444 | 3059 | if (!iovec) { |
ff6165b2 JA |
3060 | unsigned iov_off = 0; |
3061 | ||
3062 | rw->iter.iov = rw->fast_iov; | |
3063 | if (iter->iov != fast_iov) { | |
3064 | iov_off = iter->iov - fast_iov; | |
3065 | rw->iter.iov += iov_off; | |
3066 | } | |
3067 | if (rw->fast_iov != fast_iov) | |
3068 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3069 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3070 | } else { |
3071 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3072 | } |
3073 | } | |
3074 | ||
e8c2bc1f | 3075 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3076 | { |
e8c2bc1f JA |
3077 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3078 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3079 | return req->async_data == NULL; | |
3d9932a8 XW |
3080 | } |
3081 | ||
e8c2bc1f | 3082 | static int io_alloc_async_data(struct io_kiocb *req) |
f67676d1 | 3083 | { |
e8c2bc1f | 3084 | if (!io_op_defs[req->opcode].needs_async_data) |
d3656344 | 3085 | return 0; |
3d9932a8 | 3086 | |
e8c2bc1f | 3087 | return __io_alloc_async_data(req); |
b7bb4f7d JA |
3088 | } |
3089 | ||
ff6165b2 JA |
3090 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3091 | const struct iovec *fast_iov, | |
227c0c96 | 3092 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3093 | { |
e8c2bc1f | 3094 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
74566df3 | 3095 | return 0; |
e8c2bc1f | 3096 | if (!req->async_data) { |
6bf985dc PB |
3097 | if (__io_alloc_async_data(req)) { |
3098 | kfree(iovec); | |
5d204bcf | 3099 | return -ENOMEM; |
6bf985dc | 3100 | } |
b7bb4f7d | 3101 | |
ff6165b2 | 3102 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3103 | } |
b7bb4f7d | 3104 | return 0; |
f67676d1 JA |
3105 | } |
3106 | ||
73debe68 | 3107 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3108 | { |
e8c2bc1f | 3109 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3110 | struct iovec *iov = iorw->fast_iov; |
847595de | 3111 | int ret; |
c3e330a4 | 3112 | |
2846c481 | 3113 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3114 | if (unlikely(ret < 0)) |
3115 | return ret; | |
3116 | ||
ab0b196c PB |
3117 | iorw->bytes_done = 0; |
3118 | iorw->free_iovec = iov; | |
3119 | if (iov) | |
3120 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3121 | return 0; |
3122 | } | |
3123 | ||
73debe68 | 3124 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3125 | { |
3529d8c2 JA |
3126 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3127 | return -EBADF; | |
93642ef8 | 3128 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3129 | } |
3130 | ||
c1dd91d1 JA |
3131 | /* |
3132 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3133 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3134 | * This gets called when the page is unlocked, and we generally expect that to | |
3135 | * happen when the page IO is completed and the page is now uptodate. This will | |
3136 | * queue a task_work based retry of the operation, attempting to copy the data | |
3137 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3138 | * do a thread based blocking retry of the operation. That's the unexpected | |
3139 | * slow path. | |
3140 | */ | |
bcf5a063 JA |
3141 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3142 | int sync, void *arg) | |
3143 | { | |
3144 | struct wait_page_queue *wpq; | |
3145 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3146 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3147 | |
3148 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3149 | ||
cdc8fcb4 LT |
3150 | if (!wake_page_match(wpq, key)) |
3151 | return 0; | |
3152 | ||
c8d317aa | 3153 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3154 | list_del_init(&wait->entry); |
3155 | ||
bcf5a063 JA |
3156 | /* submit ref gets dropped, acquire a new one */ |
3157 | refcount_inc(&req->refs); | |
921b9054 | 3158 | io_req_task_queue(req); |
bcf5a063 JA |
3159 | return 1; |
3160 | } | |
3161 | ||
c1dd91d1 JA |
3162 | /* |
3163 | * This controls whether a given IO request should be armed for async page | |
3164 | * based retry. If we return false here, the request is handed to the async | |
3165 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3166 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3167 | * will either succeed because the page is now uptodate and unlocked, or it | |
3168 | * will register a callback when the page is unlocked at IO completion. Through | |
3169 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3170 | * That retry will attempt the buffered read again. The retry will generally | |
3171 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3172 | * async worker threads for a blocking retry. | |
3173 | */ | |
227c0c96 | 3174 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3175 | { |
e8c2bc1f JA |
3176 | struct io_async_rw *rw = req->async_data; |
3177 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3178 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3179 | |
bcf5a063 JA |
3180 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3181 | if (req->flags & REQ_F_NOWAIT) | |
3182 | return false; | |
f67676d1 | 3183 | |
227c0c96 | 3184 | /* Only for buffered IO */ |
3b2a4439 | 3185 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3186 | return false; |
3b2a4439 | 3187 | |
bcf5a063 JA |
3188 | /* |
3189 | * just use poll if we can, and don't attempt if the fs doesn't | |
3190 | * support callback based unlocks | |
3191 | */ | |
3192 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3193 | return false; | |
f67676d1 | 3194 | |
3b2a4439 JA |
3195 | wait->wait.func = io_async_buf_func; |
3196 | wait->wait.private = req; | |
3197 | wait->wait.flags = 0; | |
3198 | INIT_LIST_HEAD(&wait->wait.entry); | |
3199 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3200 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3201 | kiocb->ki_waitq = wait; |
3b2a4439 | 3202 | return true; |
bcf5a063 JA |
3203 | } |
3204 | ||
3205 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3206 | { | |
3207 | if (req->file->f_op->read_iter) | |
3208 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3209 | else if (req->file->f_op->read) |
4017eb91 | 3210 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3211 | else |
3212 | return -EINVAL; | |
f67676d1 JA |
3213 | } |
3214 | ||
889fca73 | 3215 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3216 | { |
3217 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3218 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3219 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3220 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3221 | ssize_t io_size, ret, ret2; |
45d189c6 | 3222 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3223 | |
2846c481 | 3224 | if (rw) { |
e8c2bc1f | 3225 | iter = &rw->iter; |
2846c481 PB |
3226 | iovec = NULL; |
3227 | } else { | |
3228 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3229 | if (ret < 0) | |
3230 | return ret; | |
3231 | } | |
632546c4 | 3232 | io_size = iov_iter_count(iter); |
fa15bafb | 3233 | req->result = io_size; |
2b188cc1 | 3234 | |
fd6c2e4c JA |
3235 | /* Ensure we clear previously set non-block flag */ |
3236 | if (!force_nonblock) | |
29de5f6a | 3237 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3238 | else |
3239 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3240 | ||
24c74678 | 3241 | /* If the file doesn't support async, just async punt */ |
6713e7a6 PB |
3242 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
3243 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); | |
6bf985dc | 3244 | return ret ?: -EAGAIN; |
6713e7a6 | 3245 | } |
9e645e11 | 3246 | |
632546c4 | 3247 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3248 | if (unlikely(ret)) { |
3249 | kfree(iovec); | |
3250 | return ret; | |
3251 | } | |
2b188cc1 | 3252 | |
227c0c96 | 3253 | ret = io_iter_do_read(req, iter); |
32960613 | 3254 | |
57cd657b | 3255 | if (ret == -EIOCBQUEUED) { |
3e6a0d3c JA |
3256 | if (req->async_data) |
3257 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); | |
fe1cdd55 | 3258 | goto out_free; |
227c0c96 | 3259 | } else if (ret == -EAGAIN) { |
eefdf30f JA |
3260 | /* IOPOLL retry should happen for io-wq threads */ |
3261 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3262 | goto done; |
75c668cd PB |
3263 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3264 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3265 | goto done; |
84216315 | 3266 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3267 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3268 | ret = 0; |
7335e3bf | 3269 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3270 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3271 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3272 | goto done; |
227c0c96 JA |
3273 | } |
3274 | ||
227c0c96 | 3275 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3276 | if (ret2) |
3277 | return ret2; | |
3278 | ||
fe1cdd55 | 3279 | iovec = NULL; |
e8c2bc1f | 3280 | rw = req->async_data; |
227c0c96 | 3281 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3282 | iter = &rw->iter; |
227c0c96 | 3283 | |
b23df91b PB |
3284 | do { |
3285 | io_size -= ret; | |
3286 | rw->bytes_done += ret; | |
3287 | /* if we can retry, do so with the callbacks armed */ | |
3288 | if (!io_rw_should_retry(req)) { | |
3289 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3290 | return -EAGAIN; | |
3291 | } | |
3292 | ||
3293 | /* | |
3294 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3295 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3296 | * desired page gets unlocked. We can also get a partial read | |
3297 | * here, and if we do, then just retry at the new offset. | |
3298 | */ | |
3299 | ret = io_iter_do_read(req, iter); | |
3300 | if (ret == -EIOCBQUEUED) | |
3301 | return 0; | |
227c0c96 | 3302 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3303 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3304 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3305 | done: |
889fca73 | 3306 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3307 | out_free: |
3308 | /* it's faster to check here then delegate to kfree */ | |
3309 | if (iovec) | |
3310 | kfree(iovec); | |
5ea5dd45 | 3311 | return 0; |
2b188cc1 JA |
3312 | } |
3313 | ||
73debe68 | 3314 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3315 | { |
3529d8c2 JA |
3316 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3317 | return -EBADF; | |
93642ef8 | 3318 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3319 | } |
3320 | ||
889fca73 | 3321 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3322 | { |
3323 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3324 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3325 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3326 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3327 | ssize_t ret, ret2, io_size; |
45d189c6 | 3328 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3329 | |
2846c481 | 3330 | if (rw) { |
e8c2bc1f | 3331 | iter = &rw->iter; |
2846c481 PB |
3332 | iovec = NULL; |
3333 | } else { | |
3334 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3335 | if (ret < 0) | |
3336 | return ret; | |
3337 | } | |
632546c4 | 3338 | io_size = iov_iter_count(iter); |
fa15bafb | 3339 | req->result = io_size; |
2b188cc1 | 3340 | |
fd6c2e4c JA |
3341 | /* Ensure we clear previously set non-block flag */ |
3342 | if (!force_nonblock) | |
a88fc400 PB |
3343 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3344 | else | |
3345 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3346 | |
24c74678 | 3347 | /* If the file doesn't support async, just async punt */ |
af197f50 | 3348 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
f67676d1 | 3349 | goto copy_iov; |
31b51510 | 3350 | |
10d59345 JA |
3351 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3352 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3353 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3354 | goto copy_iov; |
31b51510 | 3355 | |
632546c4 | 3356 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3357 | if (unlikely(ret)) |
3358 | goto out_free; | |
4ed734b0 | 3359 | |
fa15bafb PB |
3360 | /* |
3361 | * Open-code file_start_write here to grab freeze protection, | |
3362 | * which will be released by another thread in | |
3363 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3364 | * released so that it doesn't complain about the held lock when | |
3365 | * we return to userspace. | |
3366 | */ | |
3367 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3368 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3369 | __sb_writers_release(file_inode(req->file)->i_sb, |
3370 | SB_FREEZE_WRITE); | |
3371 | } | |
3372 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3373 | |
fa15bafb | 3374 | if (req->file->f_op->write_iter) |
ff6165b2 | 3375 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3376 | else if (req->file->f_op->write) |
4017eb91 | 3377 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3378 | else |
3379 | ret2 = -EINVAL; | |
4ed734b0 | 3380 | |
fa15bafb PB |
3381 | /* |
3382 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3383 | * retry them without IOCB_NOWAIT. | |
3384 | */ | |
3385 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3386 | ret2 = -EAGAIN; | |
75c668cd PB |
3387 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3388 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3389 | goto done; |
3e6a0d3c JA |
3390 | if (ret2 == -EIOCBQUEUED && req->async_data) |
3391 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); | |
fa15bafb | 3392 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3393 | /* IOPOLL retry should happen for io-wq threads */ |
3394 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3395 | goto copy_iov; | |
355afaeb | 3396 | done: |
889fca73 | 3397 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3398 | } else { |
f67676d1 | 3399 | copy_iov: |
84216315 | 3400 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3401 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3402 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3403 | return ret ?: -EAGAIN; |
2b188cc1 | 3404 | } |
31b51510 | 3405 | out_free: |
f261c168 | 3406 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3407 | if (iovec) |
6f2cc166 | 3408 | kfree(iovec); |
2b188cc1 JA |
3409 | return ret; |
3410 | } | |
3411 | ||
80a261fd JA |
3412 | static int io_renameat_prep(struct io_kiocb *req, |
3413 | const struct io_uring_sqe *sqe) | |
3414 | { | |
3415 | struct io_rename *ren = &req->rename; | |
3416 | const char __user *oldf, *newf; | |
3417 | ||
3418 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3419 | return -EBADF; | |
3420 | ||
3421 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3422 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3423 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3424 | ren->new_dfd = READ_ONCE(sqe->len); | |
3425 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3426 | ||
3427 | ren->oldpath = getname(oldf); | |
3428 | if (IS_ERR(ren->oldpath)) | |
3429 | return PTR_ERR(ren->oldpath); | |
3430 | ||
3431 | ren->newpath = getname(newf); | |
3432 | if (IS_ERR(ren->newpath)) { | |
3433 | putname(ren->oldpath); | |
3434 | return PTR_ERR(ren->newpath); | |
3435 | } | |
3436 | ||
3437 | req->flags |= REQ_F_NEED_CLEANUP; | |
3438 | return 0; | |
3439 | } | |
3440 | ||
45d189c6 | 3441 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3442 | { |
3443 | struct io_rename *ren = &req->rename; | |
3444 | int ret; | |
3445 | ||
45d189c6 | 3446 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3447 | return -EAGAIN; |
3448 | ||
3449 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3450 | ren->newpath, ren->flags); | |
3451 | ||
3452 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3453 | if (ret < 0) | |
3454 | req_set_fail_links(req); | |
3455 | io_req_complete(req, ret); | |
3456 | return 0; | |
3457 | } | |
3458 | ||
14a1143b JA |
3459 | static int io_unlinkat_prep(struct io_kiocb *req, |
3460 | const struct io_uring_sqe *sqe) | |
3461 | { | |
3462 | struct io_unlink *un = &req->unlink; | |
3463 | const char __user *fname; | |
3464 | ||
3465 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3466 | return -EBADF; | |
3467 | ||
3468 | un->dfd = READ_ONCE(sqe->fd); | |
3469 | ||
3470 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3471 | if (un->flags & ~AT_REMOVEDIR) | |
3472 | return -EINVAL; | |
3473 | ||
3474 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3475 | un->filename = getname(fname); | |
3476 | if (IS_ERR(un->filename)) | |
3477 | return PTR_ERR(un->filename); | |
3478 | ||
3479 | req->flags |= REQ_F_NEED_CLEANUP; | |
3480 | return 0; | |
3481 | } | |
3482 | ||
45d189c6 | 3483 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3484 | { |
3485 | struct io_unlink *un = &req->unlink; | |
3486 | int ret; | |
3487 | ||
45d189c6 | 3488 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3489 | return -EAGAIN; |
3490 | ||
3491 | if (un->flags & AT_REMOVEDIR) | |
3492 | ret = do_rmdir(un->dfd, un->filename); | |
3493 | else | |
3494 | ret = do_unlinkat(un->dfd, un->filename); | |
3495 | ||
3496 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3497 | if (ret < 0) | |
3498 | req_set_fail_links(req); | |
3499 | io_req_complete(req, ret); | |
3500 | return 0; | |
3501 | } | |
3502 | ||
36f4fa68 JA |
3503 | static int io_shutdown_prep(struct io_kiocb *req, |
3504 | const struct io_uring_sqe *sqe) | |
3505 | { | |
3506 | #if defined(CONFIG_NET) | |
3507 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3508 | return -EINVAL; | |
3509 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3510 | sqe->buf_index) | |
3511 | return -EINVAL; | |
3512 | ||
3513 | req->shutdown.how = READ_ONCE(sqe->len); | |
3514 | return 0; | |
3515 | #else | |
3516 | return -EOPNOTSUPP; | |
3517 | #endif | |
3518 | } | |
3519 | ||
45d189c6 | 3520 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3521 | { |
3522 | #if defined(CONFIG_NET) | |
3523 | struct socket *sock; | |
3524 | int ret; | |
3525 | ||
45d189c6 | 3526 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3527 | return -EAGAIN; |
3528 | ||
48aba79b | 3529 | sock = sock_from_file(req->file); |
36f4fa68 | 3530 | if (unlikely(!sock)) |
48aba79b | 3531 | return -ENOTSOCK; |
36f4fa68 JA |
3532 | |
3533 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3534 | if (ret < 0) |
3535 | req_set_fail_links(req); | |
36f4fa68 JA |
3536 | io_req_complete(req, ret); |
3537 | return 0; | |
3538 | #else | |
3539 | return -EOPNOTSUPP; | |
3540 | #endif | |
3541 | } | |
3542 | ||
f2a8d5c7 PB |
3543 | static int __io_splice_prep(struct io_kiocb *req, |
3544 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3545 | { |
3546 | struct io_splice* sp = &req->splice; | |
3547 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3548 | |
3232dd02 PB |
3549 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3550 | return -EINVAL; | |
7d67af2c PB |
3551 | |
3552 | sp->file_in = NULL; | |
7d67af2c PB |
3553 | sp->len = READ_ONCE(sqe->len); |
3554 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3555 | ||
3556 | if (unlikely(sp->flags & ~valid_flags)) | |
3557 | return -EINVAL; | |
3558 | ||
8371adf5 PB |
3559 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3560 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3561 | if (!sp->file_in) | |
3562 | return -EBADF; | |
7d67af2c PB |
3563 | req->flags |= REQ_F_NEED_CLEANUP; |
3564 | ||
7cdaf587 XW |
3565 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
3566 | /* | |
3567 | * Splice operation will be punted aync, and here need to | |
3568 | * modify io_wq_work.flags, so initialize io_wq_work firstly. | |
3569 | */ | |
7d67af2c | 3570 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
7cdaf587 | 3571 | } |
7d67af2c PB |
3572 | |
3573 | return 0; | |
3574 | } | |
3575 | ||
f2a8d5c7 PB |
3576 | static int io_tee_prep(struct io_kiocb *req, |
3577 | const struct io_uring_sqe *sqe) | |
3578 | { | |
3579 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3580 | return -EINVAL; | |
3581 | return __io_splice_prep(req, sqe); | |
3582 | } | |
3583 | ||
45d189c6 | 3584 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3585 | { |
3586 | struct io_splice *sp = &req->splice; | |
3587 | struct file *in = sp->file_in; | |
3588 | struct file *out = sp->file_out; | |
3589 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3590 | long ret = 0; | |
3591 | ||
45d189c6 | 3592 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3593 | return -EAGAIN; |
3594 | if (sp->len) | |
3595 | ret = do_tee(in, out, sp->len, flags); | |
3596 | ||
3597 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3598 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3599 | ||
f2a8d5c7 PB |
3600 | if (ret != sp->len) |
3601 | req_set_fail_links(req); | |
e1e16097 | 3602 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3603 | return 0; |
3604 | } | |
3605 | ||
3606 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3607 | { | |
3608 | struct io_splice* sp = &req->splice; | |
3609 | ||
3610 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3611 | sp->off_out = READ_ONCE(sqe->off); | |
3612 | return __io_splice_prep(req, sqe); | |
3613 | } | |
3614 | ||
45d189c6 | 3615 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3616 | { |
3617 | struct io_splice *sp = &req->splice; | |
3618 | struct file *in = sp->file_in; | |
3619 | struct file *out = sp->file_out; | |
3620 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3621 | loff_t *poff_in, *poff_out; | |
c9687426 | 3622 | long ret = 0; |
7d67af2c | 3623 | |
45d189c6 | 3624 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3625 | return -EAGAIN; |
7d67af2c PB |
3626 | |
3627 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3628 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3629 | |
948a7749 | 3630 | if (sp->len) |
c9687426 | 3631 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c PB |
3632 | |
3633 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3634 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3635 | ||
7d67af2c PB |
3636 | if (ret != sp->len) |
3637 | req_set_fail_links(req); | |
e1e16097 | 3638 | io_req_complete(req, ret); |
7d67af2c PB |
3639 | return 0; |
3640 | } | |
3641 | ||
2b188cc1 JA |
3642 | /* |
3643 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3644 | */ | |
889fca73 | 3645 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3646 | { |
3647 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3648 | |
def596e9 JA |
3649 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3650 | return -EINVAL; | |
3651 | ||
889fca73 | 3652 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3653 | return 0; |
3654 | } | |
3655 | ||
1155c76a | 3656 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3657 | { |
6b06314c | 3658 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3659 | |
09bb8394 JA |
3660 | if (!req->file) |
3661 | return -EBADF; | |
c992fe29 | 3662 | |
6b06314c | 3663 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3664 | return -EINVAL; |
edafccee | 3665 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3666 | return -EINVAL; |
3667 | ||
8ed8d3c3 JA |
3668 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3669 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3670 | return -EINVAL; | |
3671 | ||
3672 | req->sync.off = READ_ONCE(sqe->off); | |
3673 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3674 | return 0; |
3675 | } | |
3676 | ||
45d189c6 | 3677 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3678 | { |
8ed8d3c3 | 3679 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3680 | int ret; |
3681 | ||
ac45abc0 | 3682 | /* fsync always requires a blocking context */ |
45d189c6 | 3683 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3684 | return -EAGAIN; |
3685 | ||
9adbd45d | 3686 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3687 | end > 0 ? end : LLONG_MAX, |
3688 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3689 | if (ret < 0) | |
3690 | req_set_fail_links(req); | |
e1e16097 | 3691 | io_req_complete(req, ret); |
c992fe29 CH |
3692 | return 0; |
3693 | } | |
3694 | ||
d63d1b5e JA |
3695 | static int io_fallocate_prep(struct io_kiocb *req, |
3696 | const struct io_uring_sqe *sqe) | |
3697 | { | |
3698 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3699 | return -EINVAL; | |
3232dd02 PB |
3700 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3701 | return -EINVAL; | |
d63d1b5e JA |
3702 | |
3703 | req->sync.off = READ_ONCE(sqe->off); | |
3704 | req->sync.len = READ_ONCE(sqe->addr); | |
3705 | req->sync.mode = READ_ONCE(sqe->len); | |
3706 | return 0; | |
3707 | } | |
3708 | ||
45d189c6 | 3709 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3710 | { |
ac45abc0 PB |
3711 | int ret; |
3712 | ||
d63d1b5e | 3713 | /* fallocate always requiring blocking context */ |
45d189c6 | 3714 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3715 | return -EAGAIN; |
ac45abc0 PB |
3716 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3717 | req->sync.len); | |
ac45abc0 PB |
3718 | if (ret < 0) |
3719 | req_set_fail_links(req); | |
e1e16097 | 3720 | io_req_complete(req, ret); |
5d17b4a4 JA |
3721 | return 0; |
3722 | } | |
3723 | ||
ec65fea5 | 3724 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3725 | { |
f8748881 | 3726 | const char __user *fname; |
15b71abe | 3727 | int ret; |
b7bb4f7d | 3728 | |
ec65fea5 | 3729 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3730 | return -EINVAL; |
ec65fea5 | 3731 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3732 | return -EBADF; |
03b1230c | 3733 | |
ec65fea5 PB |
3734 | /* open.how should be already initialised */ |
3735 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3736 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3737 | |
25e72d10 PB |
3738 | req->open.dfd = READ_ONCE(sqe->fd); |
3739 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3740 | req->open.filename = getname(fname); |
15b71abe JA |
3741 | if (IS_ERR(req->open.filename)) { |
3742 | ret = PTR_ERR(req->open.filename); | |
3743 | req->open.filename = NULL; | |
3744 | return ret; | |
3745 | } | |
4022e7af | 3746 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3747 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3748 | return 0; |
03b1230c JA |
3749 | } |
3750 | ||
ec65fea5 PB |
3751 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3752 | { | |
3753 | u64 flags, mode; | |
3754 | ||
14587a46 | 3755 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3756 | return -EINVAL; |
ec65fea5 PB |
3757 | mode = READ_ONCE(sqe->len); |
3758 | flags = READ_ONCE(sqe->open_flags); | |
3759 | req->open.how = build_open_how(flags, mode); | |
3760 | return __io_openat_prep(req, sqe); | |
3761 | } | |
3762 | ||
cebdb986 | 3763 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3764 | { |
cebdb986 | 3765 | struct open_how __user *how; |
cebdb986 | 3766 | size_t len; |
0fa03c62 JA |
3767 | int ret; |
3768 | ||
14587a46 | 3769 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3770 | return -EINVAL; |
cebdb986 JA |
3771 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3772 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3773 | if (len < OPEN_HOW_SIZE_VER0) |
3774 | return -EINVAL; | |
3529d8c2 | 3775 | |
cebdb986 JA |
3776 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3777 | len); | |
3778 | if (ret) | |
3779 | return ret; | |
3529d8c2 | 3780 | |
ec65fea5 | 3781 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3782 | } |
3783 | ||
45d189c6 | 3784 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3785 | { |
3786 | struct open_flags op; | |
15b71abe | 3787 | struct file *file; |
3a81fd02 JA |
3788 | bool nonblock_set; |
3789 | bool resolve_nonblock; | |
15b71abe JA |
3790 | int ret; |
3791 | ||
cebdb986 | 3792 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3793 | if (ret) |
3794 | goto err; | |
3a81fd02 JA |
3795 | nonblock_set = op.open_flag & O_NONBLOCK; |
3796 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3797 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3798 | /* |
3799 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3800 | * it'll always -EAGAIN | |
3801 | */ | |
3802 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3803 | return -EAGAIN; | |
3804 | op.lookup_flags |= LOOKUP_CACHED; | |
3805 | op.open_flag |= O_NONBLOCK; | |
3806 | } | |
15b71abe | 3807 | |
4022e7af | 3808 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3809 | if (ret < 0) |
3810 | goto err; | |
3811 | ||
3812 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3813 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3814 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3815 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3816 | /* |
3a81fd02 JA |
3817 | * We could hang on to this 'fd', but seems like marginal |
3818 | * gain for something that is now known to be a slower path. | |
3819 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3820 | */ |
3a81fd02 JA |
3821 | put_unused_fd(ret); |
3822 | return -EAGAIN; | |
3823 | } | |
3824 | ||
15b71abe JA |
3825 | if (IS_ERR(file)) { |
3826 | put_unused_fd(ret); | |
3827 | ret = PTR_ERR(file); | |
3828 | } else { | |
45d189c6 | 3829 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3830 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3831 | fsnotify_open(file); |
3832 | fd_install(ret, file); | |
3833 | } | |
3834 | err: | |
3835 | putname(req->open.filename); | |
8fef80bf | 3836 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3837 | if (ret < 0) |
3838 | req_set_fail_links(req); | |
e1e16097 | 3839 | io_req_complete(req, ret); |
15b71abe JA |
3840 | return 0; |
3841 | } | |
3842 | ||
45d189c6 | 3843 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3844 | { |
e45cff58 | 3845 | return io_openat2(req, issue_flags); |
cebdb986 JA |
3846 | } |
3847 | ||
067524e9 JA |
3848 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3849 | const struct io_uring_sqe *sqe) | |
3850 | { | |
3851 | struct io_provide_buf *p = &req->pbuf; | |
3852 | u64 tmp; | |
3853 | ||
3854 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3855 | return -EINVAL; | |
3856 | ||
3857 | tmp = READ_ONCE(sqe->fd); | |
3858 | if (!tmp || tmp > USHRT_MAX) | |
3859 | return -EINVAL; | |
3860 | ||
3861 | memset(p, 0, sizeof(*p)); | |
3862 | p->nbufs = tmp; | |
3863 | p->bgid = READ_ONCE(sqe->buf_group); | |
3864 | return 0; | |
3865 | } | |
3866 | ||
3867 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3868 | int bgid, unsigned nbufs) | |
3869 | { | |
3870 | unsigned i = 0; | |
3871 | ||
3872 | /* shouldn't happen */ | |
3873 | if (!nbufs) | |
3874 | return 0; | |
3875 | ||
3876 | /* the head kbuf is the list itself */ | |
3877 | while (!list_empty(&buf->list)) { | |
3878 | struct io_buffer *nxt; | |
3879 | ||
3880 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3881 | list_del(&nxt->list); | |
3882 | kfree(nxt); | |
3883 | if (++i == nbufs) | |
3884 | return i; | |
3885 | } | |
3886 | i++; | |
3887 | kfree(buf); | |
3888 | idr_remove(&ctx->io_buffer_idr, bgid); | |
3889 | ||
3890 | return i; | |
3891 | } | |
3892 | ||
889fca73 | 3893 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3894 | { |
3895 | struct io_provide_buf *p = &req->pbuf; | |
3896 | struct io_ring_ctx *ctx = req->ctx; | |
3897 | struct io_buffer *head; | |
3898 | int ret = 0; | |
45d189c6 | 3899 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3900 | |
3901 | io_ring_submit_lock(ctx, !force_nonblock); | |
3902 | ||
3903 | lockdep_assert_held(&ctx->uring_lock); | |
3904 | ||
3905 | ret = -ENOENT; | |
3906 | head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3907 | if (head) | |
3908 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3909 | if (ret < 0) |
3910 | req_set_fail_links(req); | |
067524e9 | 3911 | |
31bff9a5 PB |
3912 | /* need to hold the lock to complete IOPOLL requests */ |
3913 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 3914 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
3915 | io_ring_submit_unlock(ctx, !force_nonblock); |
3916 | } else { | |
3917 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 3918 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 3919 | } |
067524e9 JA |
3920 | return 0; |
3921 | } | |
3922 | ||
ddf0322d JA |
3923 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3924 | const struct io_uring_sqe *sqe) | |
3925 | { | |
3926 | struct io_provide_buf *p = &req->pbuf; | |
3927 | u64 tmp; | |
3928 | ||
3929 | if (sqe->ioprio || sqe->rw_flags) | |
3930 | return -EINVAL; | |
3931 | ||
3932 | tmp = READ_ONCE(sqe->fd); | |
3933 | if (!tmp || tmp > USHRT_MAX) | |
3934 | return -E2BIG; | |
3935 | p->nbufs = tmp; | |
3936 | p->addr = READ_ONCE(sqe->addr); | |
3937 | p->len = READ_ONCE(sqe->len); | |
3938 | ||
efe68c1c | 3939 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
ddf0322d JA |
3940 | return -EFAULT; |
3941 | ||
3942 | p->bgid = READ_ONCE(sqe->buf_group); | |
3943 | tmp = READ_ONCE(sqe->off); | |
3944 | if (tmp > USHRT_MAX) | |
3945 | return -E2BIG; | |
3946 | p->bid = tmp; | |
3947 | return 0; | |
3948 | } | |
3949 | ||
3950 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3951 | { | |
3952 | struct io_buffer *buf; | |
3953 | u64 addr = pbuf->addr; | |
3954 | int i, bid = pbuf->bid; | |
3955 | ||
3956 | for (i = 0; i < pbuf->nbufs; i++) { | |
3957 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3958 | if (!buf) | |
3959 | break; | |
3960 | ||
3961 | buf->addr = addr; | |
3962 | buf->len = pbuf->len; | |
3963 | buf->bid = bid; | |
3964 | addr += pbuf->len; | |
3965 | bid++; | |
3966 | if (!*head) { | |
3967 | INIT_LIST_HEAD(&buf->list); | |
3968 | *head = buf; | |
3969 | } else { | |
3970 | list_add_tail(&buf->list, &(*head)->list); | |
3971 | } | |
3972 | } | |
3973 | ||
3974 | return i ? i : -ENOMEM; | |
3975 | } | |
3976 | ||
889fca73 | 3977 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
3978 | { |
3979 | struct io_provide_buf *p = &req->pbuf; | |
3980 | struct io_ring_ctx *ctx = req->ctx; | |
3981 | struct io_buffer *head, *list; | |
3982 | int ret = 0; | |
45d189c6 | 3983 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
3984 | |
3985 | io_ring_submit_lock(ctx, !force_nonblock); | |
3986 | ||
3987 | lockdep_assert_held(&ctx->uring_lock); | |
3988 | ||
3989 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); | |
3990 | ||
3991 | ret = io_add_buffers(p, &head); | |
3992 | if (ret < 0) | |
3993 | goto out; | |
3994 | ||
3995 | if (!list) { | |
3996 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, | |
3997 | GFP_KERNEL); | |
3998 | if (ret < 0) { | |
067524e9 | 3999 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d JA |
4000 | goto out; |
4001 | } | |
4002 | } | |
4003 | out: | |
ddf0322d JA |
4004 | if (ret < 0) |
4005 | req_set_fail_links(req); | |
31bff9a5 PB |
4006 | |
4007 | /* need to hold the lock to complete IOPOLL requests */ | |
4008 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
889fca73 | 4009 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 PB |
4010 | io_ring_submit_unlock(ctx, !force_nonblock); |
4011 | } else { | |
4012 | io_ring_submit_unlock(ctx, !force_nonblock); | |
889fca73 | 4013 | __io_req_complete(req, issue_flags, ret, 0); |
31bff9a5 | 4014 | } |
ddf0322d | 4015 | return 0; |
cebdb986 JA |
4016 | } |
4017 | ||
3e4827b0 JA |
4018 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4019 | const struct io_uring_sqe *sqe) | |
4020 | { | |
4021 | #if defined(CONFIG_EPOLL) | |
4022 | if (sqe->ioprio || sqe->buf_index) | |
4023 | return -EINVAL; | |
6ca56f84 | 4024 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4025 | return -EINVAL; |
3e4827b0 JA |
4026 | |
4027 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4028 | req->epoll.op = READ_ONCE(sqe->len); | |
4029 | req->epoll.fd = READ_ONCE(sqe->off); | |
4030 | ||
4031 | if (ep_op_has_event(req->epoll.op)) { | |
4032 | struct epoll_event __user *ev; | |
4033 | ||
4034 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4035 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4036 | return -EFAULT; | |
4037 | } | |
4038 | ||
4039 | return 0; | |
4040 | #else | |
4041 | return -EOPNOTSUPP; | |
4042 | #endif | |
4043 | } | |
4044 | ||
889fca73 | 4045 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4046 | { |
4047 | #if defined(CONFIG_EPOLL) | |
4048 | struct io_epoll *ie = &req->epoll; | |
4049 | int ret; | |
45d189c6 | 4050 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4051 | |
4052 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4053 | if (force_nonblock && ret == -EAGAIN) | |
4054 | return -EAGAIN; | |
4055 | ||
4056 | if (ret < 0) | |
4057 | req_set_fail_links(req); | |
889fca73 | 4058 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4059 | return 0; |
4060 | #else | |
4061 | return -EOPNOTSUPP; | |
4062 | #endif | |
4063 | } | |
4064 | ||
c1ca757b JA |
4065 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4066 | { | |
4067 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4068 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4069 | return -EINVAL; | |
3232dd02 PB |
4070 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4071 | return -EINVAL; | |
c1ca757b JA |
4072 | |
4073 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4074 | req->madvise.len = READ_ONCE(sqe->len); | |
4075 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4076 | return 0; | |
4077 | #else | |
4078 | return -EOPNOTSUPP; | |
4079 | #endif | |
4080 | } | |
4081 | ||
45d189c6 | 4082 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4083 | { |
4084 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4085 | struct io_madvise *ma = &req->madvise; | |
4086 | int ret; | |
4087 | ||
45d189c6 | 4088 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4089 | return -EAGAIN; |
4090 | ||
0726b01e | 4091 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4092 | if (ret < 0) |
4093 | req_set_fail_links(req); | |
e1e16097 | 4094 | io_req_complete(req, ret); |
c1ca757b JA |
4095 | return 0; |
4096 | #else | |
4097 | return -EOPNOTSUPP; | |
4098 | #endif | |
4099 | } | |
4100 | ||
4840e418 JA |
4101 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4102 | { | |
4103 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4104 | return -EINVAL; | |
3232dd02 PB |
4105 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4106 | return -EINVAL; | |
4840e418 JA |
4107 | |
4108 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4109 | req->fadvise.len = READ_ONCE(sqe->len); | |
4110 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4111 | return 0; | |
4112 | } | |
4113 | ||
45d189c6 | 4114 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4115 | { |
4116 | struct io_fadvise *fa = &req->fadvise; | |
4117 | int ret; | |
4118 | ||
45d189c6 | 4119 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4120 | switch (fa->advice) { |
4121 | case POSIX_FADV_NORMAL: | |
4122 | case POSIX_FADV_RANDOM: | |
4123 | case POSIX_FADV_SEQUENTIAL: | |
4124 | break; | |
4125 | default: | |
4126 | return -EAGAIN; | |
4127 | } | |
4128 | } | |
4840e418 JA |
4129 | |
4130 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4131 | if (ret < 0) | |
4132 | req_set_fail_links(req); | |
e1e16097 | 4133 | io_req_complete(req, ret); |
4840e418 JA |
4134 | return 0; |
4135 | } | |
4136 | ||
eddc7ef5 JA |
4137 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4138 | { | |
6ca56f84 | 4139 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4140 | return -EINVAL; |
eddc7ef5 JA |
4141 | if (sqe->ioprio || sqe->buf_index) |
4142 | return -EINVAL; | |
9c280f90 | 4143 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4144 | return -EBADF; |
eddc7ef5 | 4145 | |
1d9e1288 BM |
4146 | req->statx.dfd = READ_ONCE(sqe->fd); |
4147 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4148 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4149 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4150 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4151 | |
4152 | return 0; | |
4153 | } | |
4154 | ||
45d189c6 | 4155 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4156 | { |
1d9e1288 | 4157 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4158 | int ret; |
4159 | ||
45d189c6 | 4160 | if (issue_flags & IO_URING_F_NONBLOCK) { |
5b0bbee4 JA |
4161 | /* only need file table for an actual valid fd */ |
4162 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) | |
4163 | req->flags |= REQ_F_NO_FILE_TABLE; | |
eddc7ef5 | 4164 | return -EAGAIN; |
5b0bbee4 | 4165 | } |
eddc7ef5 | 4166 | |
e62753e4 BM |
4167 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4168 | ctx->buffer); | |
eddc7ef5 | 4169 | |
eddc7ef5 JA |
4170 | if (ret < 0) |
4171 | req_set_fail_links(req); | |
e1e16097 | 4172 | io_req_complete(req, ret); |
eddc7ef5 JA |
4173 | return 0; |
4174 | } | |
4175 | ||
b5dba59e JA |
4176 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4177 | { | |
14587a46 | 4178 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4179 | return -EINVAL; |
b5dba59e JA |
4180 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4181 | sqe->rw_flags || sqe->buf_index) | |
4182 | return -EINVAL; | |
9c280f90 | 4183 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4184 | return -EBADF; |
b5dba59e JA |
4185 | |
4186 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4187 | return 0; |
b5dba59e JA |
4188 | } |
4189 | ||
889fca73 | 4190 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4191 | { |
9eac1904 | 4192 | struct files_struct *files = current->files; |
3af73b28 | 4193 | struct io_close *close = &req->close; |
9eac1904 JA |
4194 | struct fdtable *fdt; |
4195 | struct file *file; | |
b5dba59e JA |
4196 | int ret; |
4197 | ||
9eac1904 JA |
4198 | file = NULL; |
4199 | ret = -EBADF; | |
4200 | spin_lock(&files->file_lock); | |
4201 | fdt = files_fdtable(files); | |
4202 | if (close->fd >= fdt->max_fds) { | |
4203 | spin_unlock(&files->file_lock); | |
4204 | goto err; | |
4205 | } | |
4206 | file = fdt->fd[close->fd]; | |
4207 | if (!file) { | |
4208 | spin_unlock(&files->file_lock); | |
4209 | goto err; | |
4210 | } | |
4211 | ||
4212 | if (file->f_op == &io_uring_fops) { | |
4213 | spin_unlock(&files->file_lock); | |
4214 | file = NULL; | |
4215 | goto err; | |
3af73b28 | 4216 | } |
b5dba59e JA |
4217 | |
4218 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4219 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4220 | spin_unlock(&files->file_lock); |
0bf0eefd | 4221 | return -EAGAIN; |
a2100672 | 4222 | } |
b5dba59e | 4223 | |
9eac1904 JA |
4224 | ret = __close_fd_get_file(close->fd, &file); |
4225 | spin_unlock(&files->file_lock); | |
4226 | if (ret < 0) { | |
4227 | if (ret == -ENOENT) | |
4228 | ret = -EBADF; | |
4229 | goto err; | |
4230 | } | |
4231 | ||
3af73b28 | 4232 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4233 | ret = filp_close(file, current->files); |
4234 | err: | |
3af73b28 PB |
4235 | if (ret < 0) |
4236 | req_set_fail_links(req); | |
9eac1904 JA |
4237 | if (file) |
4238 | fput(file); | |
889fca73 | 4239 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4240 | return 0; |
b5dba59e JA |
4241 | } |
4242 | ||
1155c76a | 4243 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4244 | { |
4245 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4246 | |
5d17b4a4 JA |
4247 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4248 | return -EINVAL; | |
4249 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4250 | return -EINVAL; | |
4251 | ||
8ed8d3c3 JA |
4252 | req->sync.off = READ_ONCE(sqe->off); |
4253 | req->sync.len = READ_ONCE(sqe->len); | |
4254 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4255 | return 0; |
4256 | } | |
4257 | ||
45d189c6 | 4258 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4259 | { |
8ed8d3c3 JA |
4260 | int ret; |
4261 | ||
ac45abc0 | 4262 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4263 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4264 | return -EAGAIN; |
4265 | ||
9adbd45d | 4266 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4267 | req->sync.flags); |
4268 | if (ret < 0) | |
4269 | req_set_fail_links(req); | |
e1e16097 | 4270 | io_req_complete(req, ret); |
5d17b4a4 JA |
4271 | return 0; |
4272 | } | |
4273 | ||
469956e8 | 4274 | #if defined(CONFIG_NET) |
02d27d89 PB |
4275 | static int io_setup_async_msg(struct io_kiocb *req, |
4276 | struct io_async_msghdr *kmsg) | |
4277 | { | |
e8c2bc1f JA |
4278 | struct io_async_msghdr *async_msg = req->async_data; |
4279 | ||
4280 | if (async_msg) | |
02d27d89 | 4281 | return -EAGAIN; |
e8c2bc1f | 4282 | if (io_alloc_async_data(req)) { |
257e84a5 | 4283 | kfree(kmsg->free_iov); |
02d27d89 PB |
4284 | return -ENOMEM; |
4285 | } | |
e8c2bc1f | 4286 | async_msg = req->async_data; |
02d27d89 | 4287 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4288 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4289 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4290 | /* if were using fast_iov, set it to the new one */ |
4291 | if (!async_msg->free_iov) | |
4292 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4293 | ||
02d27d89 PB |
4294 | return -EAGAIN; |
4295 | } | |
4296 | ||
2ae523ed PB |
4297 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4298 | struct io_async_msghdr *iomsg) | |
4299 | { | |
2ae523ed | 4300 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4301 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4302 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4303 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4304 | } |
4305 | ||
93642ef8 PB |
4306 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4307 | { | |
4308 | int ret; | |
4309 | ||
4310 | if (!io_op_defs[req->opcode].needs_async_data) | |
4311 | return 0; | |
4312 | ret = io_sendmsg_copy_hdr(req, req->async_data); | |
4313 | if (!ret) | |
4314 | req->flags |= REQ_F_NEED_CLEANUP; | |
4315 | return ret; | |
4316 | } | |
4317 | ||
3529d8c2 | 4318 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4319 | { |
e47293fd | 4320 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4321 | |
d2b6f48b PB |
4322 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4323 | return -EINVAL; | |
4324 | ||
e47293fd | 4325 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4326 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4327 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 4328 | |
d8768362 JA |
4329 | #ifdef CONFIG_COMPAT |
4330 | if (req->ctx->compat) | |
4331 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4332 | #endif | |
93642ef8 | 4333 | return 0; |
03b1230c JA |
4334 | } |
4335 | ||
889fca73 | 4336 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4337 | { |
6b754c8b | 4338 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4339 | struct socket *sock; |
7a7cacba | 4340 | unsigned flags; |
0fa03c62 JA |
4341 | int ret; |
4342 | ||
dba4a925 | 4343 | sock = sock_from_file(req->file); |
7a7cacba | 4344 | if (unlikely(!sock)) |
dba4a925 | 4345 | return -ENOTSOCK; |
3529d8c2 | 4346 | |
257e84a5 PB |
4347 | kmsg = req->async_data; |
4348 | if (!kmsg) { | |
7a7cacba PB |
4349 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4350 | if (ret) | |
4351 | return ret; | |
4352 | kmsg = &iomsg; | |
0fa03c62 | 4353 | } |
0fa03c62 | 4354 | |
7a7cacba PB |
4355 | flags = req->sr_msg.msg_flags; |
4356 | if (flags & MSG_DONTWAIT) | |
4357 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4358 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4359 | flags |= MSG_DONTWAIT; |
e47293fd | 4360 | |
7a7cacba | 4361 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4362 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4363 | return io_setup_async_msg(req, kmsg); |
4364 | if (ret == -ERESTARTSYS) | |
4365 | ret = -EINTR; | |
0fa03c62 | 4366 | |
257e84a5 PB |
4367 | /* fast path, check for non-NULL to avoid function call */ |
4368 | if (kmsg->free_iov) | |
4369 | kfree(kmsg->free_iov); | |
99bc4c38 | 4370 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4371 | if (ret < 0) |
4372 | req_set_fail_links(req); | |
889fca73 | 4373 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4374 | return 0; |
03b1230c | 4375 | } |
aa1fa28f | 4376 | |
889fca73 | 4377 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4378 | { |
7a7cacba PB |
4379 | struct io_sr_msg *sr = &req->sr_msg; |
4380 | struct msghdr msg; | |
4381 | struct iovec iov; | |
fddaface | 4382 | struct socket *sock; |
7a7cacba | 4383 | unsigned flags; |
fddaface JA |
4384 | int ret; |
4385 | ||
dba4a925 | 4386 | sock = sock_from_file(req->file); |
7a7cacba | 4387 | if (unlikely(!sock)) |
dba4a925 | 4388 | return -ENOTSOCK; |
fddaface | 4389 | |
7a7cacba PB |
4390 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4391 | if (unlikely(ret)) | |
14db8411 | 4392 | return ret; |
fddaface | 4393 | |
7a7cacba PB |
4394 | msg.msg_name = NULL; |
4395 | msg.msg_control = NULL; | |
4396 | msg.msg_controllen = 0; | |
4397 | msg.msg_namelen = 0; | |
fddaface | 4398 | |
7a7cacba PB |
4399 | flags = req->sr_msg.msg_flags; |
4400 | if (flags & MSG_DONTWAIT) | |
4401 | req->flags |= REQ_F_NOWAIT; | |
45d189c6 | 4402 | else if (issue_flags & IO_URING_F_NONBLOCK) |
7a7cacba | 4403 | flags |= MSG_DONTWAIT; |
fddaface | 4404 | |
7a7cacba PB |
4405 | msg.msg_flags = flags; |
4406 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4407 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4408 | return -EAGAIN; |
4409 | if (ret == -ERESTARTSYS) | |
4410 | ret = -EINTR; | |
fddaface | 4411 | |
fddaface JA |
4412 | if (ret < 0) |
4413 | req_set_fail_links(req); | |
889fca73 | 4414 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4415 | return 0; |
fddaface JA |
4416 | } |
4417 | ||
1400e697 PB |
4418 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4419 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4420 | { |
4421 | struct io_sr_msg *sr = &req->sr_msg; | |
4422 | struct iovec __user *uiov; | |
4423 | size_t iov_len; | |
4424 | int ret; | |
4425 | ||
1400e697 PB |
4426 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4427 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4428 | if (ret) |
4429 | return ret; | |
4430 | ||
4431 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4432 | if (iov_len > 1) | |
4433 | return -EINVAL; | |
5476dfed | 4434 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4435 | return -EFAULT; |
5476dfed | 4436 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4437 | iomsg->free_iov = NULL; |
52de1fe1 | 4438 | } else { |
257e84a5 | 4439 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4440 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4441 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4442 | false); |
52de1fe1 JA |
4443 | if (ret > 0) |
4444 | ret = 0; | |
4445 | } | |
4446 | ||
4447 | return ret; | |
4448 | } | |
4449 | ||
4450 | #ifdef CONFIG_COMPAT | |
4451 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4452 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4453 | { |
4454 | struct compat_msghdr __user *msg_compat; | |
4455 | struct io_sr_msg *sr = &req->sr_msg; | |
4456 | struct compat_iovec __user *uiov; | |
4457 | compat_uptr_t ptr; | |
4458 | compat_size_t len; | |
4459 | int ret; | |
4460 | ||
270a5940 | 4461 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4462 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4463 | &ptr, &len); |
4464 | if (ret) | |
4465 | return ret; | |
4466 | ||
4467 | uiov = compat_ptr(ptr); | |
4468 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4469 | compat_ssize_t clen; | |
4470 | ||
4471 | if (len > 1) | |
4472 | return -EINVAL; | |
4473 | if (!access_ok(uiov, sizeof(*uiov))) | |
4474 | return -EFAULT; | |
4475 | if (__get_user(clen, &uiov->iov_len)) | |
4476 | return -EFAULT; | |
4477 | if (clen < 0) | |
4478 | return -EINVAL; | |
2d280bc8 | 4479 | sr->len = clen; |
257e84a5 | 4480 | iomsg->free_iov = NULL; |
52de1fe1 | 4481 | } else { |
257e84a5 | 4482 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4483 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4484 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4485 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4486 | if (ret < 0) |
4487 | return ret; | |
4488 | } | |
4489 | ||
4490 | return 0; | |
4491 | } | |
4492 | #endif | |
4493 | ||
1400e697 PB |
4494 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4495 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4496 | { |
1400e697 | 4497 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4498 | |
4499 | #ifdef CONFIG_COMPAT | |
4500 | if (req->ctx->compat) | |
1400e697 | 4501 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4502 | #endif |
52de1fe1 | 4503 | |
1400e697 | 4504 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4505 | } |
4506 | ||
bcda7baa | 4507 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4508 | bool needs_lock) |
bcda7baa JA |
4509 | { |
4510 | struct io_sr_msg *sr = &req->sr_msg; | |
4511 | struct io_buffer *kbuf; | |
4512 | ||
bcda7baa JA |
4513 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4514 | if (IS_ERR(kbuf)) | |
4515 | return kbuf; | |
4516 | ||
4517 | sr->kbuf = kbuf; | |
4518 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4519 | return kbuf; |
fddaface JA |
4520 | } |
4521 | ||
7fbb1b54 PB |
4522 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4523 | { | |
4524 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4525 | } | |
4526 | ||
93642ef8 | 4527 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4528 | { |
99bc4c38 | 4529 | int ret; |
3529d8c2 | 4530 | |
93642ef8 PB |
4531 | if (!io_op_defs[req->opcode].needs_async_data) |
4532 | return 0; | |
4533 | ret = io_recvmsg_copy_hdr(req, req->async_data); | |
4534 | if (!ret) | |
4535 | req->flags |= REQ_F_NEED_CLEANUP; | |
4536 | return ret; | |
4537 | } | |
4538 | ||
4539 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4540 | { | |
4541 | struct io_sr_msg *sr = &req->sr_msg; | |
4542 | ||
d2b6f48b PB |
4543 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4544 | return -EINVAL; | |
4545 | ||
3529d8c2 | 4546 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
270a5940 | 4547 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4548 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4549 | sr->bgid = READ_ONCE(sqe->buf_group); |
06b76d44 | 4550 | |
d8768362 JA |
4551 | #ifdef CONFIG_COMPAT |
4552 | if (req->ctx->compat) | |
4553 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4554 | #endif | |
93642ef8 | 4555 | return 0; |
aa1fa28f JA |
4556 | } |
4557 | ||
889fca73 | 4558 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4559 | { |
6b754c8b | 4560 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4561 | struct socket *sock; |
7fbb1b54 | 4562 | struct io_buffer *kbuf; |
7a7cacba | 4563 | unsigned flags; |
52de1fe1 | 4564 | int ret, cflags = 0; |
45d189c6 | 4565 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4566 | |
dba4a925 | 4567 | sock = sock_from_file(req->file); |
7a7cacba | 4568 | if (unlikely(!sock)) |
dba4a925 | 4569 | return -ENOTSOCK; |
3529d8c2 | 4570 | |
257e84a5 PB |
4571 | kmsg = req->async_data; |
4572 | if (!kmsg) { | |
7a7cacba PB |
4573 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4574 | if (ret) | |
681fda8d | 4575 | return ret; |
7a7cacba PB |
4576 | kmsg = &iomsg; |
4577 | } | |
03b1230c | 4578 | |
bc02ef33 | 4579 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4580 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4581 | if (IS_ERR(kbuf)) |
52de1fe1 | 4582 | return PTR_ERR(kbuf); |
7a7cacba | 4583 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4584 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4585 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4586 | 1, req->sr_msg.len); |
4587 | } | |
52de1fe1 | 4588 | |
7a7cacba PB |
4589 | flags = req->sr_msg.msg_flags; |
4590 | if (flags & MSG_DONTWAIT) | |
4591 | req->flags |= REQ_F_NOWAIT; | |
4592 | else if (force_nonblock) | |
4593 | flags |= MSG_DONTWAIT; | |
e47293fd | 4594 | |
7a7cacba PB |
4595 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4596 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4597 | if (force_nonblock && ret == -EAGAIN) |
4598 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4599 | if (ret == -ERESTARTSYS) |
4600 | ret = -EINTR; | |
03b1230c | 4601 | |
7fbb1b54 PB |
4602 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4603 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4604 | /* fast path, check for non-NULL to avoid function call */ |
4605 | if (kmsg->free_iov) | |
4606 | kfree(kmsg->free_iov); | |
99bc4c38 | 4607 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4e88d6e7 JA |
4608 | if (ret < 0) |
4609 | req_set_fail_links(req); | |
889fca73 | 4610 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4611 | return 0; |
0fa03c62 | 4612 | } |
5d17b4a4 | 4613 | |
889fca73 | 4614 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4615 | { |
6b754c8b | 4616 | struct io_buffer *kbuf; |
7a7cacba PB |
4617 | struct io_sr_msg *sr = &req->sr_msg; |
4618 | struct msghdr msg; | |
4619 | void __user *buf = sr->buf; | |
fddaface | 4620 | struct socket *sock; |
7a7cacba PB |
4621 | struct iovec iov; |
4622 | unsigned flags; | |
bcda7baa | 4623 | int ret, cflags = 0; |
45d189c6 | 4624 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4625 | |
dba4a925 | 4626 | sock = sock_from_file(req->file); |
7a7cacba | 4627 | if (unlikely(!sock)) |
dba4a925 | 4628 | return -ENOTSOCK; |
fddaface | 4629 | |
bc02ef33 | 4630 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4631 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4632 | if (IS_ERR(kbuf)) |
4633 | return PTR_ERR(kbuf); | |
7a7cacba | 4634 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4635 | } |
bcda7baa | 4636 | |
7a7cacba | 4637 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4638 | if (unlikely(ret)) |
4639 | goto out_free; | |
fddaface | 4640 | |
7a7cacba PB |
4641 | msg.msg_name = NULL; |
4642 | msg.msg_control = NULL; | |
4643 | msg.msg_controllen = 0; | |
4644 | msg.msg_namelen = 0; | |
4645 | msg.msg_iocb = NULL; | |
4646 | msg.msg_flags = 0; | |
fddaface | 4647 | |
7a7cacba PB |
4648 | flags = req->sr_msg.msg_flags; |
4649 | if (flags & MSG_DONTWAIT) | |
4650 | req->flags |= REQ_F_NOWAIT; | |
4651 | else if (force_nonblock) | |
4652 | flags |= MSG_DONTWAIT; | |
4653 | ||
4654 | ret = sock_recvmsg(sock, &msg, flags); | |
4655 | if (force_nonblock && ret == -EAGAIN) | |
4656 | return -EAGAIN; | |
4657 | if (ret == -ERESTARTSYS) | |
4658 | ret = -EINTR; | |
14c32eee | 4659 | out_free: |
7fbb1b54 PB |
4660 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4661 | cflags = io_put_recv_kbuf(req); | |
fddaface JA |
4662 | if (ret < 0) |
4663 | req_set_fail_links(req); | |
889fca73 | 4664 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4665 | return 0; |
fddaface JA |
4666 | } |
4667 | ||
3529d8c2 | 4668 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4669 | { |
8ed8d3c3 JA |
4670 | struct io_accept *accept = &req->accept; |
4671 | ||
14587a46 | 4672 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4673 | return -EINVAL; |
8042d6ce | 4674 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4675 | return -EINVAL; |
4676 | ||
d55e5f5b JA |
4677 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4678 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4679 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4680 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4681 | return 0; |
8ed8d3c3 | 4682 | } |
17f2fe35 | 4683 | |
889fca73 | 4684 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4685 | { |
4686 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4687 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4688 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4689 | int ret; |
4690 | ||
e697deed JX |
4691 | if (req->file->f_flags & O_NONBLOCK) |
4692 | req->flags |= REQ_F_NOWAIT; | |
4693 | ||
8ed8d3c3 | 4694 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4695 | accept->addr_len, accept->flags, |
4696 | accept->nofile); | |
8ed8d3c3 | 4697 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4698 | return -EAGAIN; |
ac45abc0 PB |
4699 | if (ret < 0) { |
4700 | if (ret == -ERESTARTSYS) | |
4701 | ret = -EINTR; | |
4e88d6e7 | 4702 | req_set_fail_links(req); |
ac45abc0 | 4703 | } |
889fca73 | 4704 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4705 | return 0; |
8ed8d3c3 JA |
4706 | } |
4707 | ||
93642ef8 PB |
4708 | static int io_connect_prep_async(struct io_kiocb *req) |
4709 | { | |
4710 | struct io_async_connect *io = req->async_data; | |
4711 | struct io_connect *conn = &req->connect; | |
4712 | ||
4713 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4714 | } | |
4715 | ||
3529d8c2 | 4716 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4717 | { |
3529d8c2 | 4718 | struct io_connect *conn = &req->connect; |
f499a021 | 4719 | |
14587a46 | 4720 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4721 | return -EINVAL; |
4722 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4723 | return -EINVAL; | |
4724 | ||
3529d8c2 JA |
4725 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4726 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4727 | return 0; |
f499a021 JA |
4728 | } |
4729 | ||
889fca73 | 4730 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4731 | { |
e8c2bc1f | 4732 | struct io_async_connect __io, *io; |
f8e85cf2 | 4733 | unsigned file_flags; |
3fbb51c1 | 4734 | int ret; |
45d189c6 | 4735 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4736 | |
e8c2bc1f JA |
4737 | if (req->async_data) { |
4738 | io = req->async_data; | |
f499a021 | 4739 | } else { |
3529d8c2 JA |
4740 | ret = move_addr_to_kernel(req->connect.addr, |
4741 | req->connect.addr_len, | |
e8c2bc1f | 4742 | &__io.address); |
f499a021 JA |
4743 | if (ret) |
4744 | goto out; | |
4745 | io = &__io; | |
4746 | } | |
4747 | ||
3fbb51c1 JA |
4748 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4749 | ||
e8c2bc1f | 4750 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4751 | req->connect.addr_len, file_flags); |
87f80d62 | 4752 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4753 | if (req->async_data) |
b7bb4f7d | 4754 | return -EAGAIN; |
e8c2bc1f | 4755 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4756 | ret = -ENOMEM; |
4757 | goto out; | |
4758 | } | |
e8c2bc1f JA |
4759 | io = req->async_data; |
4760 | memcpy(req->async_data, &__io, sizeof(__io)); | |
f8e85cf2 | 4761 | return -EAGAIN; |
f499a021 | 4762 | } |
f8e85cf2 JA |
4763 | if (ret == -ERESTARTSYS) |
4764 | ret = -EINTR; | |
f499a021 | 4765 | out: |
4e88d6e7 JA |
4766 | if (ret < 0) |
4767 | req_set_fail_links(req); | |
889fca73 | 4768 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4769 | return 0; |
469956e8 Y |
4770 | } |
4771 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4772 | #define IO_NETOP_FN(op) \ |
4773 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4774 | { \ | |
4775 | return -EOPNOTSUPP; \ | |
4776 | } | |
4777 | ||
4778 | #define IO_NETOP_PREP(op) \ | |
4779 | IO_NETOP_FN(op) \ | |
4780 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4781 | { \ | |
4782 | return -EOPNOTSUPP; \ | |
4783 | } \ | |
4784 | ||
4785 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4786 | IO_NETOP_PREP(op) \ | |
4787 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4788 | { \ | |
4789 | return -EOPNOTSUPP; \ | |
4790 | } | |
4791 | ||
4792 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4793 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4794 | IO_NETOP_PREP_ASYNC(connect); | |
4795 | IO_NETOP_PREP(accept); | |
4796 | IO_NETOP_FN(send); | |
4797 | IO_NETOP_FN(recv); | |
469956e8 | 4798 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4799 | |
d7718a9d JA |
4800 | struct io_poll_table { |
4801 | struct poll_table_struct pt; | |
4802 | struct io_kiocb *req; | |
4803 | int error; | |
4804 | }; | |
ce593a6c | 4805 | |
d7718a9d JA |
4806 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4807 | __poll_t mask, task_work_func_t func) | |
4808 | { | |
aa96bf8a | 4809 | int ret; |
d7718a9d JA |
4810 | |
4811 | /* for instances that support it check for an event match first: */ | |
4812 | if (mask && !(mask & poll->events)) | |
4813 | return 0; | |
4814 | ||
4815 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4816 | ||
4817 | list_del_init(&poll->wait.entry); | |
4818 | ||
d7718a9d | 4819 | req->result = mask; |
7cbf1722 | 4820 | req->task_work.func = func; |
6d816e08 JA |
4821 | percpu_ref_get(&req->ctx->refs); |
4822 | ||
d7718a9d | 4823 | /* |
e3aabf95 JA |
4824 | * If this fails, then the task is exiting. When a task exits, the |
4825 | * work gets canceled, so just cancel this request as well instead | |
4826 | * of executing it. We can't safely execute it anyway, as we may not | |
4827 | * have the needed state needed for it anyway. | |
d7718a9d | 4828 | */ |
355fb9e2 | 4829 | ret = io_req_task_work_add(req); |
aa96bf8a | 4830 | if (unlikely(ret)) { |
e3aabf95 | 4831 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4832 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4833 | } |
d7718a9d JA |
4834 | return 1; |
4835 | } | |
4836 | ||
74ce6ce4 JA |
4837 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4838 | __acquires(&req->ctx->completion_lock) | |
4839 | { | |
4840 | struct io_ring_ctx *ctx = req->ctx; | |
4841 | ||
4842 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4843 | struct poll_table_struct pt = { ._key = poll->events }; | |
4844 | ||
4845 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4846 | } | |
4847 | ||
4848 | spin_lock_irq(&ctx->completion_lock); | |
4849 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4850 | add_wait_queue(poll->head, &poll->wait); | |
4851 | return true; | |
4852 | } | |
4853 | ||
4854 | return false; | |
4855 | } | |
4856 | ||
d4e7cd36 | 4857 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4858 | { |
e8c2bc1f | 4859 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4860 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4861 | return req->async_data; |
d4e7cd36 JA |
4862 | return req->apoll->double_poll; |
4863 | } | |
4864 | ||
4865 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4866 | { | |
4867 | if (req->opcode == IORING_OP_POLL_ADD) | |
4868 | return &req->poll; | |
4869 | return &req->apoll->poll; | |
4870 | } | |
4871 | ||
4872 | static void io_poll_remove_double(struct io_kiocb *req) | |
4873 | { | |
4874 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4875 | |
4876 | lockdep_assert_held(&req->ctx->completion_lock); | |
4877 | ||
4878 | if (poll && poll->head) { | |
4879 | struct wait_queue_head *head = poll->head; | |
4880 | ||
4881 | spin_lock(&head->lock); | |
4882 | list_del_init(&poll->wait.entry); | |
4883 | if (poll->wait.private) | |
4884 | refcount_dec(&req->refs); | |
4885 | poll->head = NULL; | |
4886 | spin_unlock(&head->lock); | |
4887 | } | |
4888 | } | |
4889 | ||
4890 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) | |
4891 | { | |
4892 | struct io_ring_ctx *ctx = req->ctx; | |
4893 | ||
d4e7cd36 | 4894 | io_poll_remove_double(req); |
18bceab1 JA |
4895 | req->poll.done = true; |
4896 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); | |
4897 | io_commit_cqring(ctx); | |
4898 | } | |
4899 | ||
dd221f46 | 4900 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4901 | { |
dd221f46 | 4902 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4903 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4904 | struct io_kiocb *nxt; |
18bceab1 JA |
4905 | |
4906 | if (io_poll_rewait(req, &req->poll)) { | |
4907 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 PB |
4908 | } else { |
4909 | hash_del(&req->hash_node); | |
4910 | io_poll_complete(req, req->result, 0); | |
4911 | spin_unlock_irq(&ctx->completion_lock); | |
18bceab1 | 4912 | |
dd221f46 PB |
4913 | nxt = io_put_req_find_next(req); |
4914 | io_cqring_ev_posted(ctx); | |
4915 | if (nxt) | |
4916 | __io_req_task_submit(nxt); | |
4917 | } | |
18bceab1 | 4918 | |
6d816e08 | 4919 | percpu_ref_put(&ctx->refs); |
18bceab1 JA |
4920 | } |
4921 | ||
4922 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4923 | int sync, void *key) | |
4924 | { | |
4925 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4926 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4927 | __poll_t mask = key_to_poll(key); |
4928 | ||
4929 | /* for instances that support it check for an event match first: */ | |
4930 | if (mask && !(mask & poll->events)) | |
4931 | return 0; | |
4932 | ||
8706e04e JA |
4933 | list_del_init(&wait->entry); |
4934 | ||
807abcb0 | 4935 | if (poll && poll->head) { |
18bceab1 JA |
4936 | bool done; |
4937 | ||
807abcb0 JA |
4938 | spin_lock(&poll->head->lock); |
4939 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4940 | if (!done) |
807abcb0 | 4941 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4942 | /* make sure double remove sees this as being gone */ |
4943 | wait->private = NULL; | |
807abcb0 | 4944 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4945 | if (!done) { |
4946 | /* use wait func handler, so it matches the rq type */ | |
4947 | poll->wait.func(&poll->wait, mode, sync, key); | |
4948 | } | |
18bceab1 JA |
4949 | } |
4950 | refcount_dec(&req->refs); | |
4951 | return 1; | |
4952 | } | |
4953 | ||
4954 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4955 | wait_queue_func_t wake_func) | |
4956 | { | |
4957 | poll->head = NULL; | |
4958 | poll->done = false; | |
4959 | poll->canceled = false; | |
4960 | poll->events = events; | |
4961 | INIT_LIST_HEAD(&poll->wait.entry); | |
4962 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4963 | } | |
4964 | ||
4965 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4966 | struct wait_queue_head *head, |
4967 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4968 | { |
4969 | struct io_kiocb *req = pt->req; | |
4970 | ||
4971 | /* | |
4972 | * If poll->head is already set, it's because the file being polled | |
4973 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4974 | * for write). Setup a separate io_poll_iocb if this happens. | |
4975 | */ | |
4976 | if (unlikely(poll->head)) { | |
58852d4d PB |
4977 | struct io_poll_iocb *poll_one = poll; |
4978 | ||
18bceab1 | 4979 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 4980 | if (*poll_ptr) { |
18bceab1 JA |
4981 | pt->error = -EINVAL; |
4982 | return; | |
4983 | } | |
1c3b3e65 JA |
4984 | /* double add on the same waitqueue head, ignore */ |
4985 | if (poll->head == head) | |
4986 | return; | |
18bceab1 JA |
4987 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
4988 | if (!poll) { | |
4989 | pt->error = -ENOMEM; | |
4990 | return; | |
4991 | } | |
58852d4d | 4992 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
18bceab1 JA |
4993 | refcount_inc(&req->refs); |
4994 | poll->wait.private = req; | |
807abcb0 | 4995 | *poll_ptr = poll; |
18bceab1 JA |
4996 | } |
4997 | ||
4998 | pt->error = 0; | |
4999 | poll->head = head; | |
a31eb4a2 JX |
5000 | |
5001 | if (poll->events & EPOLLEXCLUSIVE) | |
5002 | add_wait_queue_exclusive(head, &poll->wait); | |
5003 | else | |
5004 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5005 | } |
5006 | ||
5007 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5008 | struct poll_table_struct *p) | |
5009 | { | |
5010 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5011 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5012 | |
807abcb0 | 5013 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5014 | } |
5015 | ||
d7718a9d JA |
5016 | static void io_async_task_func(struct callback_head *cb) |
5017 | { | |
5018 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5019 | struct async_poll *apoll = req->apoll; | |
5020 | struct io_ring_ctx *ctx = req->ctx; | |
5021 | ||
5022 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5023 | ||
74ce6ce4 | 5024 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5025 | spin_unlock_irq(&ctx->completion_lock); |
6d816e08 | 5026 | percpu_ref_put(&ctx->refs); |
74ce6ce4 | 5027 | return; |
d7718a9d JA |
5028 | } |
5029 | ||
31067255 | 5030 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
0be0b0e3 | 5031 | if (hash_hashed(&req->hash_node)) |
74ce6ce4 | 5032 | hash_del(&req->hash_node); |
2bae047e | 5033 | |
d4e7cd36 | 5034 | io_poll_remove_double(req); |
74ce6ce4 JA |
5035 | spin_unlock_irq(&ctx->completion_lock); |
5036 | ||
0be0b0e3 PB |
5037 | if (!READ_ONCE(apoll->poll.canceled)) |
5038 | __io_req_task_submit(req); | |
5039 | else | |
5040 | __io_req_task_cancel(req, -ECANCELED); | |
aa340845 | 5041 | |
6d816e08 | 5042 | percpu_ref_put(&ctx->refs); |
807abcb0 | 5043 | kfree(apoll->double_poll); |
31067255 | 5044 | kfree(apoll); |
d7718a9d JA |
5045 | } |
5046 | ||
5047 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5048 | void *key) | |
5049 | { | |
5050 | struct io_kiocb *req = wait->private; | |
5051 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5052 | ||
5053 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5054 | key_to_poll(key)); | |
5055 | ||
5056 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5057 | } | |
5058 | ||
5059 | static void io_poll_req_insert(struct io_kiocb *req) | |
5060 | { | |
5061 | struct io_ring_ctx *ctx = req->ctx; | |
5062 | struct hlist_head *list; | |
5063 | ||
5064 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5065 | hlist_add_head(&req->hash_node, list); | |
5066 | } | |
5067 | ||
5068 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5069 | struct io_poll_iocb *poll, | |
5070 | struct io_poll_table *ipt, __poll_t mask, | |
5071 | wait_queue_func_t wake_func) | |
5072 | __acquires(&ctx->completion_lock) | |
5073 | { | |
5074 | struct io_ring_ctx *ctx = req->ctx; | |
5075 | bool cancel = false; | |
5076 | ||
4d52f338 | 5077 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5078 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5079 | poll->file = req->file; |
18bceab1 | 5080 | poll->wait.private = req; |
d7718a9d JA |
5081 | |
5082 | ipt->pt._key = mask; | |
5083 | ipt->req = req; | |
5084 | ipt->error = -EINVAL; | |
5085 | ||
d7718a9d JA |
5086 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5087 | ||
5088 | spin_lock_irq(&ctx->completion_lock); | |
5089 | if (likely(poll->head)) { | |
5090 | spin_lock(&poll->head->lock); | |
5091 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5092 | if (ipt->error) | |
5093 | cancel = true; | |
5094 | ipt->error = 0; | |
5095 | mask = 0; | |
5096 | } | |
5097 | if (mask || ipt->error) | |
5098 | list_del_init(&poll->wait.entry); | |
5099 | else if (cancel) | |
5100 | WRITE_ONCE(poll->canceled, true); | |
5101 | else if (!poll->done) /* actually waiting for an event */ | |
5102 | io_poll_req_insert(req); | |
5103 | spin_unlock(&poll->head->lock); | |
5104 | } | |
5105 | ||
5106 | return mask; | |
5107 | } | |
5108 | ||
5109 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5110 | { | |
5111 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5112 | struct io_ring_ctx *ctx = req->ctx; | |
5113 | struct async_poll *apoll; | |
5114 | struct io_poll_table ipt; | |
5115 | __poll_t mask, ret; | |
9dab14b8 | 5116 | int rw; |
d7718a9d JA |
5117 | |
5118 | if (!req->file || !file_can_poll(req->file)) | |
5119 | return false; | |
24c74678 | 5120 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5121 | return false; |
9dab14b8 JA |
5122 | if (def->pollin) |
5123 | rw = READ; | |
5124 | else if (def->pollout) | |
5125 | rw = WRITE; | |
5126 | else | |
5127 | return false; | |
5128 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
5129 | if (!io_file_supports_async(req->file, rw)) | |
d7718a9d JA |
5130 | return false; |
5131 | ||
5132 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5133 | if (unlikely(!apoll)) | |
5134 | return false; | |
807abcb0 | 5135 | apoll->double_poll = NULL; |
d7718a9d JA |
5136 | |
5137 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5138 | req->apoll = apoll; |
d7718a9d | 5139 | |
8755d97a | 5140 | mask = 0; |
d7718a9d | 5141 | if (def->pollin) |
8755d97a | 5142 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5143 | if (def->pollout) |
5144 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5145 | |
5146 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5147 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5148 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5149 | mask &= ~POLLIN; | |
5150 | ||
d7718a9d JA |
5151 | mask |= POLLERR | POLLPRI; |
5152 | ||
5153 | ipt.pt._qproc = io_async_queue_proc; | |
5154 | ||
5155 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5156 | io_async_wake); | |
a36da65c | 5157 | if (ret || ipt.error) { |
d4e7cd36 | 5158 | io_poll_remove_double(req); |
d7718a9d | 5159 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5160 | kfree(apoll->double_poll); |
d7718a9d JA |
5161 | kfree(apoll); |
5162 | return false; | |
5163 | } | |
5164 | spin_unlock_irq(&ctx->completion_lock); | |
5165 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5166 | apoll->poll.events); | |
5167 | return true; | |
5168 | } | |
5169 | ||
5170 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
5171 | struct io_poll_iocb *poll) | |
221c5eb2 | 5172 | { |
b41e9852 | 5173 | bool do_complete = false; |
221c5eb2 JA |
5174 | |
5175 | spin_lock(&poll->head->lock); | |
5176 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5177 | if (!list_empty(&poll->wait.entry)) { |
5178 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5179 | do_complete = true; |
221c5eb2 JA |
5180 | } |
5181 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5182 | hash_del(&req->hash_node); |
d7718a9d JA |
5183 | return do_complete; |
5184 | } | |
5185 | ||
5186 | static bool io_poll_remove_one(struct io_kiocb *req) | |
5187 | { | |
5188 | bool do_complete; | |
5189 | ||
d4e7cd36 JA |
5190 | io_poll_remove_double(req); |
5191 | ||
d7718a9d JA |
5192 | if (req->opcode == IORING_OP_POLL_ADD) { |
5193 | do_complete = __io_poll_remove_one(req, &req->poll); | |
5194 | } else { | |
3bfa5bcb JA |
5195 | struct async_poll *apoll = req->apoll; |
5196 | ||
d7718a9d | 5197 | /* non-poll requests have submit ref still */ |
3bfa5bcb JA |
5198 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
5199 | if (do_complete) { | |
d7718a9d | 5200 | io_put_req(req); |
807abcb0 | 5201 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5202 | kfree(apoll); |
5203 | } | |
b1f573bd XW |
5204 | } |
5205 | ||
b41e9852 JA |
5206 | if (do_complete) { |
5207 | io_cqring_fill_event(req, -ECANCELED); | |
5208 | io_commit_cqring(req->ctx); | |
f254ac04 | 5209 | req_set_fail_links(req); |
216578e5 | 5210 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5211 | } |
5212 | ||
5213 | return do_complete; | |
221c5eb2 JA |
5214 | } |
5215 | ||
76e1b642 JA |
5216 | /* |
5217 | * Returns true if we found and killed one or more poll requests | |
5218 | */ | |
6b81928d PB |
5219 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5220 | struct files_struct *files) | |
221c5eb2 | 5221 | { |
78076bb6 | 5222 | struct hlist_node *tmp; |
221c5eb2 | 5223 | struct io_kiocb *req; |
8e2e1faf | 5224 | int posted = 0, i; |
221c5eb2 JA |
5225 | |
5226 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5227 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5228 | struct hlist_head *list; | |
5229 | ||
5230 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5231 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5232 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5233 | posted += io_poll_remove_one(req); |
5234 | } | |
221c5eb2 JA |
5235 | } |
5236 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5237 | |
8e2e1faf JA |
5238 | if (posted) |
5239 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5240 | |
5241 | return posted != 0; | |
221c5eb2 JA |
5242 | } |
5243 | ||
47f46768 JA |
5244 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
5245 | { | |
78076bb6 | 5246 | struct hlist_head *list; |
47f46768 JA |
5247 | struct io_kiocb *req; |
5248 | ||
78076bb6 JA |
5249 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5250 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5251 | if (sqe_addr != req->user_data) |
5252 | continue; | |
5253 | if (io_poll_remove_one(req)) | |
eac406c6 | 5254 | return 0; |
b41e9852 | 5255 | return -EALREADY; |
47f46768 JA |
5256 | } |
5257 | ||
5258 | return -ENOENT; | |
5259 | } | |
5260 | ||
3529d8c2 JA |
5261 | static int io_poll_remove_prep(struct io_kiocb *req, |
5262 | const struct io_uring_sqe *sqe) | |
0969e783 | 5263 | { |
0969e783 JA |
5264 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5265 | return -EINVAL; | |
5266 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5267 | sqe->poll_events) | |
5268 | return -EINVAL; | |
5269 | ||
018043be | 5270 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5271 | return 0; |
5272 | } | |
5273 | ||
221c5eb2 JA |
5274 | /* |
5275 | * Find a running poll command that matches one specified in sqe->addr, | |
5276 | * and remove it if found. | |
5277 | */ | |
61e98203 | 5278 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5279 | { |
5280 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5281 | int ret; |
221c5eb2 | 5282 | |
221c5eb2 | 5283 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5284 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5285 | spin_unlock_irq(&ctx->completion_lock); |
5286 | ||
4e88d6e7 JA |
5287 | if (ret < 0) |
5288 | req_set_fail_links(req); | |
e1e16097 | 5289 | io_req_complete(req, ret); |
221c5eb2 JA |
5290 | return 0; |
5291 | } | |
5292 | ||
221c5eb2 JA |
5293 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5294 | void *key) | |
5295 | { | |
c2f2eb7d JA |
5296 | struct io_kiocb *req = wait->private; |
5297 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5298 | |
d7718a9d | 5299 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5300 | } |
5301 | ||
221c5eb2 JA |
5302 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5303 | struct poll_table_struct *p) | |
5304 | { | |
5305 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5306 | ||
e8c2bc1f | 5307 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5308 | } |
5309 | ||
3529d8c2 | 5310 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5311 | { |
5312 | struct io_poll_iocb *poll = &req->poll; | |
5769a351 | 5313 | u32 events; |
221c5eb2 JA |
5314 | |
5315 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5316 | return -EINVAL; | |
5317 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
5318 | return -EINVAL; | |
5319 | ||
5769a351 JX |
5320 | events = READ_ONCE(sqe->poll32_events); |
5321 | #ifdef __BIG_ENDIAN | |
5322 | events = swahw32(events); | |
5323 | #endif | |
a31eb4a2 JX |
5324 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
5325 | (events & EPOLLEXCLUSIVE); | |
0969e783 JA |
5326 | return 0; |
5327 | } | |
5328 | ||
61e98203 | 5329 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5330 | { |
5331 | struct io_poll_iocb *poll = &req->poll; | |
5332 | struct io_ring_ctx *ctx = req->ctx; | |
5333 | struct io_poll_table ipt; | |
0969e783 | 5334 | __poll_t mask; |
0969e783 | 5335 | |
d7718a9d | 5336 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5337 | |
d7718a9d JA |
5338 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5339 | io_poll_wake); | |
221c5eb2 | 5340 | |
8c838788 | 5341 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5342 | ipt.error = 0; |
b0dd8a41 | 5343 | io_poll_complete(req, mask, 0); |
221c5eb2 | 5344 | } |
221c5eb2 JA |
5345 | spin_unlock_irq(&ctx->completion_lock); |
5346 | ||
8c838788 JA |
5347 | if (mask) { |
5348 | io_cqring_ev_posted(ctx); | |
014db007 | 5349 | io_put_req(req); |
221c5eb2 | 5350 | } |
8c838788 | 5351 | return ipt.error; |
221c5eb2 JA |
5352 | } |
5353 | ||
5262f567 JA |
5354 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5355 | { | |
ad8a48ac JA |
5356 | struct io_timeout_data *data = container_of(timer, |
5357 | struct io_timeout_data, timer); | |
5358 | struct io_kiocb *req = data->req; | |
5359 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5360 | unsigned long flags; |
5361 | ||
5262f567 | 5362 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5363 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5364 | atomic_set(&req->ctx->cq_timeouts, |
5365 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5366 | ||
78e19bbe | 5367 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5368 | io_commit_cqring(ctx); |
5369 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5370 | ||
5371 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5372 | req_set_fail_links(req); |
5262f567 JA |
5373 | io_put_req(req); |
5374 | return HRTIMER_NORESTART; | |
5375 | } | |
5376 | ||
fbd15848 PB |
5377 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5378 | __u64 user_data) | |
f254ac04 | 5379 | { |
fbd15848 | 5380 | struct io_timeout_data *io; |
47f46768 JA |
5381 | struct io_kiocb *req; |
5382 | int ret = -ENOENT; | |
f254ac04 | 5383 | |
135fcde8 | 5384 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5385 | if (user_data == req->user_data) { |
47f46768 JA |
5386 | ret = 0; |
5387 | break; | |
5388 | } | |
5389 | } | |
5390 | ||
5391 | if (ret == -ENOENT) | |
fbd15848 PB |
5392 | return ERR_PTR(ret); |
5393 | ||
5394 | io = req->async_data; | |
e8c2bc1f | 5395 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5396 | if (ret == -1) |
fbd15848 | 5397 | return ERR_PTR(-EALREADY); |
a71976f3 | 5398 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5399 | return req; |
5400 | } | |
47f46768 | 5401 | |
fbd15848 PB |
5402 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
5403 | { | |
5404 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5405 | ||
5406 | if (IS_ERR(req)) | |
5407 | return PTR_ERR(req); | |
f254ac04 JA |
5408 | |
5409 | req_set_fail_links(req); | |
f254ac04 | 5410 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5411 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5412 | return 0; |
5413 | } | |
5414 | ||
9c8e11b3 PB |
5415 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5416 | struct timespec64 *ts, enum hrtimer_mode mode) | |
47f46768 | 5417 | { |
9c8e11b3 PB |
5418 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5419 | struct io_timeout_data *data; | |
47f46768 | 5420 | |
9c8e11b3 PB |
5421 | if (IS_ERR(req)) |
5422 | return PTR_ERR(req); | |
47f46768 | 5423 | |
9c8e11b3 PB |
5424 | req->timeout.off = 0; /* noseq */ |
5425 | data = req->async_data; | |
5426 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5427 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5428 | data->timer.function = io_timeout_fn; | |
5429 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5430 | return 0; | |
47f46768 JA |
5431 | } |
5432 | ||
3529d8c2 JA |
5433 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5434 | const struct io_uring_sqe *sqe) | |
b29472ee | 5435 | { |
9c8e11b3 PB |
5436 | struct io_timeout_rem *tr = &req->timeout_rem; |
5437 | ||
b29472ee JA |
5438 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5439 | return -EINVAL; | |
61710e43 DA |
5440 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5441 | return -EINVAL; | |
9c8e11b3 | 5442 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5443 | return -EINVAL; |
5444 | ||
9c8e11b3 PB |
5445 | tr->addr = READ_ONCE(sqe->addr); |
5446 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5447 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5448 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5449 | return -EINVAL; | |
5450 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5451 | return -EFAULT; | |
5452 | } else if (tr->flags) { | |
5453 | /* timeout removal doesn't support flags */ | |
b29472ee | 5454 | return -EINVAL; |
9c8e11b3 | 5455 | } |
b29472ee | 5456 | |
b29472ee JA |
5457 | return 0; |
5458 | } | |
5459 | ||
8662daec PB |
5460 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5461 | { | |
5462 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5463 | : HRTIMER_MODE_REL; | |
5464 | } | |
5465 | ||
11365043 JA |
5466 | /* |
5467 | * Remove or update an existing timeout command | |
5468 | */ | |
61e98203 | 5469 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5470 | { |
9c8e11b3 | 5471 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5472 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5473 | int ret; |
11365043 | 5474 | |
11365043 | 5475 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5476 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5477 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5478 | else |
5479 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5480 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5481 | |
47f46768 | 5482 | io_cqring_fill_event(req, ret); |
11365043 JA |
5483 | io_commit_cqring(ctx); |
5484 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5485 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5486 | if (ret < 0) |
5487 | req_set_fail_links(req); | |
ec9c02ad | 5488 | io_put_req(req); |
11365043 | 5489 | return 0; |
5262f567 JA |
5490 | } |
5491 | ||
3529d8c2 | 5492 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5493 | bool is_timeout_link) |
5262f567 | 5494 | { |
ad8a48ac | 5495 | struct io_timeout_data *data; |
a41525ab | 5496 | unsigned flags; |
56080b02 | 5497 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5498 | |
ad8a48ac | 5499 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5500 | return -EINVAL; |
ad8a48ac | 5501 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5502 | return -EINVAL; |
56080b02 | 5503 | if (off && is_timeout_link) |
2d28390a | 5504 | return -EINVAL; |
a41525ab JA |
5505 | flags = READ_ONCE(sqe->timeout_flags); |
5506 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5507 | return -EINVAL; |
bdf20073 | 5508 | |
bfe68a22 | 5509 | req->timeout.off = off; |
26a61679 | 5510 | |
e8c2bc1f | 5511 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5512 | return -ENOMEM; |
5513 | ||
e8c2bc1f | 5514 | data = req->async_data; |
ad8a48ac | 5515 | data->req = req; |
ad8a48ac JA |
5516 | |
5517 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5518 | return -EFAULT; |
5519 | ||
8662daec | 5520 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac | 5521 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
dd59a3d5 | 5522 | io_req_track_inflight(req); |
ad8a48ac JA |
5523 | return 0; |
5524 | } | |
5525 | ||
61e98203 | 5526 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5527 | { |
ad8a48ac | 5528 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5529 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5530 | struct list_head *entry; |
bfe68a22 | 5531 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5532 | |
733f5c95 | 5533 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5534 | |
5262f567 JA |
5535 | /* |
5536 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5537 | * timeout event to be satisfied. If it isn't set, then this is |
5538 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5539 | */ |
8eb7e2d0 | 5540 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5541 | entry = ctx->timeout_list.prev; |
5542 | goto add; | |
5543 | } | |
5262f567 | 5544 | |
bfe68a22 PB |
5545 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5546 | req->timeout.target_seq = tail + off; | |
5262f567 | 5547 | |
f010505b MDG |
5548 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5549 | * This is safe because ->completion_lock is held, and submissions | |
5550 | * and completions are never mixed in the same ->completion_lock section. | |
5551 | */ | |
5552 | ctx->cq_last_tm_flush = tail; | |
5553 | ||
5262f567 JA |
5554 | /* |
5555 | * Insertion sort, ensuring the first entry in the list is always | |
5556 | * the one we need first. | |
5557 | */ | |
5262f567 | 5558 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5559 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5560 | timeout.list); | |
5262f567 | 5561 | |
8eb7e2d0 | 5562 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5563 | continue; |
bfe68a22 PB |
5564 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5565 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5566 | break; |
5567 | } | |
93bd25bb | 5568 | add: |
135fcde8 | 5569 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5570 | data->timer.function = io_timeout_fn; |
5571 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5572 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5573 | return 0; |
5574 | } | |
5262f567 | 5575 | |
f458dd84 PB |
5576 | struct io_cancel_data { |
5577 | struct io_ring_ctx *ctx; | |
5578 | u64 user_data; | |
5579 | }; | |
5580 | ||
62755e35 JA |
5581 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5582 | { | |
5583 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5584 | struct io_cancel_data *cd = data; |
62755e35 | 5585 | |
f458dd84 | 5586 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5587 | } |
5588 | ||
f458dd84 PB |
5589 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5590 | struct io_ring_ctx *ctx) | |
62755e35 | 5591 | { |
f458dd84 | 5592 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5593 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5594 | int ret = 0; |
5595 | ||
f458dd84 | 5596 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5597 | return -ENOENT; |
5598 | ||
f458dd84 | 5599 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5600 | switch (cancel_ret) { |
5601 | case IO_WQ_CANCEL_OK: | |
5602 | ret = 0; | |
5603 | break; | |
5604 | case IO_WQ_CANCEL_RUNNING: | |
5605 | ret = -EALREADY; | |
5606 | break; | |
5607 | case IO_WQ_CANCEL_NOTFOUND: | |
5608 | ret = -ENOENT; | |
5609 | break; | |
5610 | } | |
5611 | ||
e977d6d3 JA |
5612 | return ret; |
5613 | } | |
5614 | ||
47f46768 JA |
5615 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5616 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5617 | int success_ret) |
47f46768 JA |
5618 | { |
5619 | unsigned long flags; | |
5620 | int ret; | |
5621 | ||
f458dd84 | 5622 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
47f46768 JA |
5623 | if (ret != -ENOENT) { |
5624 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5625 | goto done; | |
5626 | } | |
5627 | ||
5628 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
5629 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5630 | if (ret != -ENOENT) | |
5631 | goto done; | |
5632 | ret = io_poll_cancel(ctx, sqe_addr); | |
5633 | done: | |
b0dd8a41 JA |
5634 | if (!ret) |
5635 | ret = success_ret; | |
47f46768 JA |
5636 | io_cqring_fill_event(req, ret); |
5637 | io_commit_cqring(ctx); | |
5638 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5639 | io_cqring_ev_posted(ctx); | |
5640 | ||
4e88d6e7 JA |
5641 | if (ret < 0) |
5642 | req_set_fail_links(req); | |
014db007 | 5643 | io_put_req(req); |
47f46768 JA |
5644 | } |
5645 | ||
3529d8c2 JA |
5646 | static int io_async_cancel_prep(struct io_kiocb *req, |
5647 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5648 | { |
fbf23849 | 5649 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5650 | return -EINVAL; |
61710e43 DA |
5651 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5652 | return -EINVAL; | |
5653 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5654 | return -EINVAL; |
5655 | ||
fbf23849 JA |
5656 | req->cancel.addr = READ_ONCE(sqe->addr); |
5657 | return 0; | |
5658 | } | |
5659 | ||
61e98203 | 5660 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5661 | { |
5662 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 | 5663 | |
014db007 | 5664 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
5262f567 JA |
5665 | return 0; |
5666 | } | |
5667 | ||
269bbe5f | 5668 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5669 | const struct io_uring_sqe *sqe) |
5670 | { | |
6ca56f84 JA |
5671 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5672 | return -EINVAL; | |
61710e43 DA |
5673 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5674 | return -EINVAL; | |
5675 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5676 | return -EINVAL; |
5677 | ||
269bbe5f BM |
5678 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5679 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5680 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5681 | return -EINVAL; |
269bbe5f | 5682 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5683 | return 0; |
5684 | } | |
5685 | ||
889fca73 | 5686 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5687 | { |
5688 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5689 | struct io_uring_rsrc_update up; |
05f3fb3c | 5690 | int ret; |
fbf23849 | 5691 | |
45d189c6 | 5692 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5693 | return -EAGAIN; |
05f3fb3c | 5694 | |
269bbe5f BM |
5695 | up.offset = req->rsrc_update.offset; |
5696 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5697 | |
5698 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5699 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5700 | mutex_unlock(&ctx->uring_lock); |
5701 | ||
5702 | if (ret < 0) | |
5703 | req_set_fail_links(req); | |
889fca73 | 5704 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5705 | return 0; |
5706 | } | |
5707 | ||
bfe76559 | 5708 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5709 | { |
d625c6ee | 5710 | switch (req->opcode) { |
e781573e | 5711 | case IORING_OP_NOP: |
bfe76559 | 5712 | return 0; |
f67676d1 JA |
5713 | case IORING_OP_READV: |
5714 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5715 | case IORING_OP_READ: |
bfe76559 | 5716 | return io_read_prep(req, sqe); |
f67676d1 JA |
5717 | case IORING_OP_WRITEV: |
5718 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5719 | case IORING_OP_WRITE: |
bfe76559 | 5720 | return io_write_prep(req, sqe); |
0969e783 | 5721 | case IORING_OP_POLL_ADD: |
bfe76559 | 5722 | return io_poll_add_prep(req, sqe); |
0969e783 | 5723 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5724 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5725 | case IORING_OP_FSYNC: |
1155c76a | 5726 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5727 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5728 | return io_sfr_prep(req, sqe); |
03b1230c | 5729 | case IORING_OP_SENDMSG: |
fddaface | 5730 | case IORING_OP_SEND: |
bfe76559 | 5731 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5732 | case IORING_OP_RECVMSG: |
fddaface | 5733 | case IORING_OP_RECV: |
bfe76559 | 5734 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5735 | case IORING_OP_CONNECT: |
bfe76559 | 5736 | return io_connect_prep(req, sqe); |
2d28390a | 5737 | case IORING_OP_TIMEOUT: |
bfe76559 | 5738 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5739 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5740 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5741 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5742 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5743 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5744 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5745 | case IORING_OP_ACCEPT: |
bfe76559 | 5746 | return io_accept_prep(req, sqe); |
d63d1b5e | 5747 | case IORING_OP_FALLOCATE: |
bfe76559 | 5748 | return io_fallocate_prep(req, sqe); |
15b71abe | 5749 | case IORING_OP_OPENAT: |
bfe76559 | 5750 | return io_openat_prep(req, sqe); |
b5dba59e | 5751 | case IORING_OP_CLOSE: |
bfe76559 | 5752 | return io_close_prep(req, sqe); |
05f3fb3c | 5753 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5754 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5755 | case IORING_OP_STATX: |
bfe76559 | 5756 | return io_statx_prep(req, sqe); |
4840e418 | 5757 | case IORING_OP_FADVISE: |
bfe76559 | 5758 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5759 | case IORING_OP_MADVISE: |
bfe76559 | 5760 | return io_madvise_prep(req, sqe); |
cebdb986 | 5761 | case IORING_OP_OPENAT2: |
bfe76559 | 5762 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5763 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5764 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5765 | case IORING_OP_SPLICE: |
bfe76559 | 5766 | return io_splice_prep(req, sqe); |
ddf0322d | 5767 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5768 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5769 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5770 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5771 | case IORING_OP_TEE: |
bfe76559 | 5772 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5773 | case IORING_OP_SHUTDOWN: |
5774 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5775 | case IORING_OP_RENAMEAT: |
5776 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5777 | case IORING_OP_UNLINKAT: |
5778 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5779 | } |
5780 | ||
bfe76559 PB |
5781 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5782 | req->opcode); | |
5783 | return-EINVAL; | |
5784 | } | |
5785 | ||
93642ef8 | 5786 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5787 | { |
93642ef8 PB |
5788 | switch (req->opcode) { |
5789 | case IORING_OP_READV: | |
5790 | case IORING_OP_READ_FIXED: | |
5791 | case IORING_OP_READ: | |
5792 | return io_rw_prep_async(req, READ); | |
5793 | case IORING_OP_WRITEV: | |
5794 | case IORING_OP_WRITE_FIXED: | |
5795 | case IORING_OP_WRITE: | |
5796 | return io_rw_prep_async(req, WRITE); | |
5797 | case IORING_OP_SENDMSG: | |
5798 | case IORING_OP_SEND: | |
5799 | return io_sendmsg_prep_async(req); | |
5800 | case IORING_OP_RECVMSG: | |
5801 | case IORING_OP_RECV: | |
5802 | return io_recvmsg_prep_async(req); | |
5803 | case IORING_OP_CONNECT: | |
5804 | return io_connect_prep_async(req); | |
5805 | } | |
5806 | return 0; | |
5807 | } | |
5808 | ||
be7053b7 | 5809 | static int io_req_defer_prep(struct io_kiocb *req) |
bfe76559 | 5810 | { |
be7053b7 | 5811 | if (!io_op_defs[req->opcode].needs_async_data) |
bfe76559 | 5812 | return 0; |
be7053b7 | 5813 | /* some opcodes init it during the inital prep */ |
93642ef8 | 5814 | if (req->async_data) |
be7053b7 PB |
5815 | return 0; |
5816 | if (__io_alloc_async_data(req)) | |
bfe76559 | 5817 | return -EAGAIN; |
be7053b7 | 5818 | return io_req_prep_async(req); |
f67676d1 JA |
5819 | } |
5820 | ||
9cf7c104 PB |
5821 | static u32 io_get_sequence(struct io_kiocb *req) |
5822 | { | |
5823 | struct io_kiocb *pos; | |
5824 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 5825 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 5826 | |
f2f87370 PB |
5827 | io_for_each_link(pos, req) |
5828 | nr_reqs++; | |
9cf7c104 PB |
5829 | |
5830 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5831 | return total_submitted - nr_reqs; | |
5832 | } | |
5833 | ||
be7053b7 | 5834 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 5835 | { |
a197f664 | 5836 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5837 | struct io_defer_entry *de; |
f67676d1 | 5838 | int ret; |
9cf7c104 | 5839 | u32 seq; |
de0617e4 | 5840 | |
9d858b21 | 5841 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5842 | if (likely(list_empty_careful(&ctx->defer_list) && |
5843 | !(req->flags & REQ_F_IO_DRAIN))) | |
5844 | return 0; | |
5845 | ||
5846 | seq = io_get_sequence(req); | |
5847 | /* Still a chance to pass the sequence check */ | |
5848 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
5849 | return 0; |
5850 | ||
be7053b7 PB |
5851 | ret = io_req_defer_prep(req); |
5852 | if (ret) | |
5853 | return ret; | |
cbdcb435 | 5854 | io_prep_async_link(req); |
27dc8338 PB |
5855 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
5856 | if (!de) | |
5857 | return -ENOMEM; | |
2d28390a | 5858 | |
de0617e4 | 5859 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 5860 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 5861 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 5862 | kfree(de); |
ae34817b PB |
5863 | io_queue_async_work(req); |
5864 | return -EIOCBQUEUED; | |
de0617e4 JA |
5865 | } |
5866 | ||
915967f6 | 5867 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 5868 | de->req = req; |
9cf7c104 | 5869 | de->seq = seq; |
27dc8338 | 5870 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
5871 | spin_unlock_irq(&ctx->completion_lock); |
5872 | return -EIOCBQUEUED; | |
5873 | } | |
5874 | ||
3ca405eb | 5875 | static void __io_clean_op(struct io_kiocb *req) |
99bc4c38 | 5876 | { |
0e1b6fe3 PB |
5877 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
5878 | switch (req->opcode) { | |
5879 | case IORING_OP_READV: | |
5880 | case IORING_OP_READ_FIXED: | |
5881 | case IORING_OP_READ: | |
bcda7baa | 5882 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
5883 | break; |
5884 | case IORING_OP_RECVMSG: | |
5885 | case IORING_OP_RECV: | |
bcda7baa | 5886 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
5887 | break; |
5888 | } | |
5889 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
5890 | } |
5891 | ||
0e1b6fe3 PB |
5892 | if (req->flags & REQ_F_NEED_CLEANUP) { |
5893 | switch (req->opcode) { | |
5894 | case IORING_OP_READV: | |
5895 | case IORING_OP_READ_FIXED: | |
5896 | case IORING_OP_READ: | |
5897 | case IORING_OP_WRITEV: | |
5898 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
5899 | case IORING_OP_WRITE: { |
5900 | struct io_async_rw *io = req->async_data; | |
5901 | if (io->free_iovec) | |
5902 | kfree(io->free_iovec); | |
0e1b6fe3 | 5903 | break; |
e8c2bc1f | 5904 | } |
0e1b6fe3 | 5905 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
5906 | case IORING_OP_SENDMSG: { |
5907 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
5908 | |
5909 | kfree(io->free_iov); | |
0e1b6fe3 | 5910 | break; |
e8c2bc1f | 5911 | } |
0e1b6fe3 PB |
5912 | case IORING_OP_SPLICE: |
5913 | case IORING_OP_TEE: | |
5914 | io_put_file(req, req->splice.file_in, | |
5915 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); | |
5916 | break; | |
f3cd4850 JA |
5917 | case IORING_OP_OPENAT: |
5918 | case IORING_OP_OPENAT2: | |
5919 | if (req->open.filename) | |
5920 | putname(req->open.filename); | |
5921 | break; | |
80a261fd JA |
5922 | case IORING_OP_RENAMEAT: |
5923 | putname(req->rename.oldpath); | |
5924 | putname(req->rename.newpath); | |
5925 | break; | |
14a1143b JA |
5926 | case IORING_OP_UNLINKAT: |
5927 | putname(req->unlink.filename); | |
5928 | break; | |
0e1b6fe3 PB |
5929 | } |
5930 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 5931 | } |
99bc4c38 PB |
5932 | } |
5933 | ||
889fca73 | 5934 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 5935 | { |
a197f664 | 5936 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 5937 | const struct cred *creds = NULL; |
d625c6ee | 5938 | int ret; |
2b188cc1 | 5939 | |
003e8dcc JA |
5940 | if (req->work.creds && req->work.creds != current_cred()) |
5941 | creds = override_creds(req->work.creds); | |
5730b27e | 5942 | |
d625c6ee | 5943 | switch (req->opcode) { |
2b188cc1 | 5944 | case IORING_OP_NOP: |
889fca73 | 5945 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
5946 | break; |
5947 | case IORING_OP_READV: | |
edafccee | 5948 | case IORING_OP_READ_FIXED: |
3a6820f2 | 5949 | case IORING_OP_READ: |
889fca73 | 5950 | ret = io_read(req, issue_flags); |
edafccee | 5951 | break; |
3529d8c2 | 5952 | case IORING_OP_WRITEV: |
edafccee | 5953 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 5954 | case IORING_OP_WRITE: |
889fca73 | 5955 | ret = io_write(req, issue_flags); |
2b188cc1 | 5956 | break; |
c992fe29 | 5957 | case IORING_OP_FSYNC: |
45d189c6 | 5958 | ret = io_fsync(req, issue_flags); |
c992fe29 | 5959 | break; |
221c5eb2 | 5960 | case IORING_OP_POLL_ADD: |
61e98203 | 5961 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
5962 | break; |
5963 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 5964 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 5965 | break; |
5d17b4a4 | 5966 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 5967 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 5968 | break; |
0fa03c62 | 5969 | case IORING_OP_SENDMSG: |
889fca73 | 5970 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 5971 | break; |
fddaface | 5972 | case IORING_OP_SEND: |
889fca73 | 5973 | ret = io_send(req, issue_flags); |
0fa03c62 | 5974 | break; |
aa1fa28f | 5975 | case IORING_OP_RECVMSG: |
889fca73 | 5976 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 5977 | break; |
fddaface | 5978 | case IORING_OP_RECV: |
889fca73 | 5979 | ret = io_recv(req, issue_flags); |
aa1fa28f | 5980 | break; |
5262f567 | 5981 | case IORING_OP_TIMEOUT: |
61e98203 | 5982 | ret = io_timeout(req, issue_flags); |
5262f567 | 5983 | break; |
11365043 | 5984 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 5985 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 5986 | break; |
17f2fe35 | 5987 | case IORING_OP_ACCEPT: |
889fca73 | 5988 | ret = io_accept(req, issue_flags); |
17f2fe35 | 5989 | break; |
f8e85cf2 | 5990 | case IORING_OP_CONNECT: |
889fca73 | 5991 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 5992 | break; |
62755e35 | 5993 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 5994 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 5995 | break; |
d63d1b5e | 5996 | case IORING_OP_FALLOCATE: |
45d189c6 | 5997 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 5998 | break; |
15b71abe | 5999 | case IORING_OP_OPENAT: |
45d189c6 | 6000 | ret = io_openat(req, issue_flags); |
15b71abe | 6001 | break; |
b5dba59e | 6002 | case IORING_OP_CLOSE: |
889fca73 | 6003 | ret = io_close(req, issue_flags); |
b5dba59e | 6004 | break; |
05f3fb3c | 6005 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6006 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6007 | break; |
eddc7ef5 | 6008 | case IORING_OP_STATX: |
45d189c6 | 6009 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6010 | break; |
4840e418 | 6011 | case IORING_OP_FADVISE: |
45d189c6 | 6012 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6013 | break; |
c1ca757b | 6014 | case IORING_OP_MADVISE: |
45d189c6 | 6015 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6016 | break; |
cebdb986 | 6017 | case IORING_OP_OPENAT2: |
45d189c6 | 6018 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6019 | break; |
3e4827b0 | 6020 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6021 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6022 | break; |
7d67af2c | 6023 | case IORING_OP_SPLICE: |
45d189c6 | 6024 | ret = io_splice(req, issue_flags); |
7d67af2c | 6025 | break; |
ddf0322d | 6026 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6027 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6028 | break; |
067524e9 | 6029 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6030 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6031 | break; |
f2a8d5c7 | 6032 | case IORING_OP_TEE: |
45d189c6 | 6033 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6034 | break; |
36f4fa68 | 6035 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6036 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6037 | break; |
80a261fd | 6038 | case IORING_OP_RENAMEAT: |
45d189c6 | 6039 | ret = io_renameat(req, issue_flags); |
80a261fd | 6040 | break; |
14a1143b | 6041 | case IORING_OP_UNLINKAT: |
45d189c6 | 6042 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6043 | break; |
2b188cc1 JA |
6044 | default: |
6045 | ret = -EINVAL; | |
6046 | break; | |
6047 | } | |
6048 | ||
5730b27e JA |
6049 | if (creds) |
6050 | revert_creds(creds); | |
6051 | ||
def596e9 JA |
6052 | if (ret) |
6053 | return ret; | |
6054 | ||
b532576e JA |
6055 | /* If the op doesn't have a file, we're not polling for it */ |
6056 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6057 | const bool in_async = io_wq_current_is_worker(); |
6058 | ||
11ba820b JA |
6059 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6060 | if (in_async) | |
6061 | mutex_lock(&ctx->uring_lock); | |
6062 | ||
2e9dbe90 | 6063 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6064 | |
6065 | if (in_async) | |
6066 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6067 | } |
6068 | ||
6069 | return 0; | |
2b188cc1 JA |
6070 | } |
6071 | ||
5280f7e5 | 6072 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6073 | { |
6074 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6075 | struct io_kiocb *timeout; |
561fb04a | 6076 | int ret = 0; |
2b188cc1 | 6077 | |
6df1db6b PB |
6078 | timeout = io_prep_linked_timeout(req); |
6079 | if (timeout) | |
6080 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6081 | |
4014d943 | 6082 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6083 | ret = -ECANCELED; |
31b51510 | 6084 | |
561fb04a | 6085 | if (!ret) { |
561fb04a | 6086 | do { |
889fca73 | 6087 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6088 | /* |
6089 | * We can get EAGAIN for polled IO even though we're | |
6090 | * forcing a sync submission from here, since we can't | |
6091 | * wait for request slots on the block side. | |
6092 | */ | |
6093 | if (ret != -EAGAIN) | |
6094 | break; | |
6095 | cond_resched(); | |
6096 | } while (1); | |
6097 | } | |
31b51510 | 6098 | |
a3df7698 | 6099 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6100 | if (ret) { |
a3df7698 PB |
6101 | /* io-wq is going to take one down */ |
6102 | refcount_inc(&req->refs); | |
6103 | io_req_task_queue_fail(req, ret); | |
edafccee | 6104 | } |
2b188cc1 JA |
6105 | } |
6106 | ||
65e19f54 JA |
6107 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6108 | int index) | |
6109 | { | |
269bbe5f | 6110 | struct fixed_rsrc_table *table; |
65e19f54 | 6111 | |
05f3fb3c | 6112 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
84695089 | 6113 | return table->files[index & IORING_FILE_TABLE_MASK]; |
65e19f54 JA |
6114 | } |
6115 | ||
8371adf5 PB |
6116 | static struct file *io_file_get(struct io_submit_state *state, |
6117 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6118 | { |
a197f664 | 6119 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6120 | struct file *file; |
09bb8394 | 6121 | |
8da11c19 | 6122 | if (fixed) { |
479f517b | 6123 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6124 | return NULL; |
b7620121 | 6125 | fd = array_index_nospec(fd, ctx->nr_user_files); |
8da11c19 | 6126 | file = io_file_from_index(ctx, fd); |
36f72fe2 | 6127 | io_set_resource_node(req); |
09bb8394 | 6128 | } else { |
c826bd7a | 6129 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6130 | file = __io_file_get(state, fd); |
09bb8394 JA |
6131 | } |
6132 | ||
ce3d5aae PB |
6133 | if (file && unlikely(file->f_op == &io_uring_fops)) |
6134 | io_req_track_inflight(req); | |
8371adf5 | 6135 | return file; |
09bb8394 JA |
6136 | } |
6137 | ||
2665abfd | 6138 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6139 | { |
ad8a48ac JA |
6140 | struct io_timeout_data *data = container_of(timer, |
6141 | struct io_timeout_data, timer); | |
90cd7e42 | 6142 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6143 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6144 | unsigned long flags; |
2665abfd JA |
6145 | |
6146 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6147 | prev = req->timeout.head; |
6148 | req->timeout.head = NULL; | |
2665abfd JA |
6149 | |
6150 | /* | |
6151 | * We don't expect the list to be empty, that will only happen if we | |
6152 | * race with the completion of the linked work. | |
6153 | */ | |
90cd7e42 | 6154 | if (prev && refcount_inc_not_zero(&prev->refs)) |
f2f87370 | 6155 | io_remove_next_linked(prev); |
90cd7e42 PB |
6156 | else |
6157 | prev = NULL; | |
2665abfd JA |
6158 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6159 | ||
6160 | if (prev) { | |
4e88d6e7 | 6161 | req_set_fail_links(prev); |
014db007 | 6162 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6163 | io_put_req_deferred(prev, 1); |
47f46768 | 6164 | } else { |
9ae1f8dd PB |
6165 | io_req_complete_post(req, -ETIME, 0); |
6166 | io_put_req_deferred(req, 1); | |
2665abfd | 6167 | } |
2665abfd JA |
6168 | return HRTIMER_NORESTART; |
6169 | } | |
6170 | ||
7271ef3a | 6171 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6172 | { |
76a46e06 | 6173 | /* |
f2f87370 PB |
6174 | * If the back reference is NULL, then our linked request finished |
6175 | * before we got a chance to setup the timer | |
76a46e06 | 6176 | */ |
90cd7e42 | 6177 | if (req->timeout.head) { |
e8c2bc1f | 6178 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6179 | |
ad8a48ac JA |
6180 | data->timer.function = io_link_timeout_fn; |
6181 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6182 | data->mode); | |
2665abfd | 6183 | } |
7271ef3a JA |
6184 | } |
6185 | ||
6186 | static void io_queue_linked_timeout(struct io_kiocb *req) | |
6187 | { | |
6188 | struct io_ring_ctx *ctx = req->ctx; | |
6189 | ||
6190 | spin_lock_irq(&ctx->completion_lock); | |
6191 | __io_queue_linked_timeout(req); | |
76a46e06 | 6192 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6193 | |
2665abfd | 6194 | /* drop submission reference */ |
76a46e06 JA |
6195 | io_put_req(req); |
6196 | } | |
2665abfd | 6197 | |
ad8a48ac | 6198 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6199 | { |
f2f87370 | 6200 | struct io_kiocb *nxt = req->link; |
2665abfd | 6201 | |
f2f87370 PB |
6202 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6203 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6204 | return NULL; |
2665abfd | 6205 | |
90cd7e42 | 6206 | nxt->timeout.head = req; |
900fad45 | 6207 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6208 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6209 | return nxt; |
2665abfd JA |
6210 | } |
6211 | ||
c5eef2b9 | 6212 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6213 | { |
d3d7298d | 6214 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
e0c5c576 | 6215 | int ret; |
2b188cc1 | 6216 | |
c5eef2b9 | 6217 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6218 | |
491381ce JA |
6219 | /* |
6220 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6221 | * doesn't support non-blocking read/write attempts | |
6222 | */ | |
24c74678 | 6223 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
f063c547 | 6224 | if (!io_arm_poll_handler(req)) { |
f063c547 PB |
6225 | /* |
6226 | * Queued up for async execution, worker will release | |
6227 | * submit reference when the iocb is actually submitted. | |
6228 | */ | |
6229 | io_queue_async_work(req); | |
2b188cc1 | 6230 | } |
0d63c148 PB |
6231 | } else if (likely(!ret)) { |
6232 | /* drop submission reference */ | |
e342c807 | 6233 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6234 | struct io_ring_ctx *ctx = req->ctx; |
6235 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6236 | |
6dd0be1e | 6237 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6238 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6239 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6240 | } else { |
d3d7298d | 6241 | io_put_req(req); |
0d63c148 PB |
6242 | } |
6243 | } else { | |
4e88d6e7 | 6244 | req_set_fail_links(req); |
e65ef56d | 6245 | io_put_req(req); |
e1e16097 | 6246 | io_req_complete(req, ret); |
9e645e11 | 6247 | } |
d3d7298d PB |
6248 | if (linked_timeout) |
6249 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6250 | } |
6251 | ||
be7053b7 | 6252 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6253 | { |
6254 | int ret; | |
6255 | ||
be7053b7 | 6256 | ret = io_req_defer(req); |
4fe2c963 JL |
6257 | if (ret) { |
6258 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6259 | fail_req: |
4e88d6e7 | 6260 | req_set_fail_links(req); |
e1e16097 JA |
6261 | io_put_req(req); |
6262 | io_req_complete(req, ret); | |
4fe2c963 | 6263 | } |
2550878f | 6264 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
be7053b7 PB |
6265 | ret = io_req_defer_prep(req); |
6266 | if (unlikely(ret)) | |
6267 | goto fail_req; | |
ce35a47a JA |
6268 | io_queue_async_work(req); |
6269 | } else { | |
c5eef2b9 | 6270 | __io_queue_sqe(req); |
ce35a47a | 6271 | } |
4fe2c963 JL |
6272 | } |
6273 | ||
b16fed66 PB |
6274 | /* |
6275 | * Check SQE restrictions (opcode and flags). | |
6276 | * | |
6277 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6278 | */ | |
6279 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6280 | struct io_kiocb *req, | |
6281 | unsigned int sqe_flags) | |
4fe2c963 | 6282 | { |
b16fed66 PB |
6283 | if (!ctx->restricted) |
6284 | return true; | |
6285 | ||
6286 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6287 | return false; | |
6288 | ||
6289 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6290 | ctx->restrictions.sqe_flags_required) | |
6291 | return false; | |
6292 | ||
6293 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6294 | ctx->restrictions.sqe_flags_required)) | |
6295 | return false; | |
6296 | ||
6297 | return true; | |
4fe2c963 JL |
6298 | } |
6299 | ||
b16fed66 PB |
6300 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6301 | const struct io_uring_sqe *sqe) | |
6302 | { | |
6303 | struct io_submit_state *state; | |
6304 | unsigned int sqe_flags; | |
003e8dcc | 6305 | int personality, ret = 0; |
b16fed66 PB |
6306 | |
6307 | req->opcode = READ_ONCE(sqe->opcode); | |
6308 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6309 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6310 | req->user_data = READ_ONCE(sqe->user_data); | |
6311 | req->async_data = NULL; | |
6312 | req->file = NULL; | |
6313 | req->ctx = ctx; | |
6314 | req->link = NULL; | |
6315 | req->fixed_rsrc_refs = NULL; | |
6316 | /* one is dropped after submission, the other at completion */ | |
6317 | refcount_set(&req->refs, 2); | |
6318 | req->task = current; | |
6319 | req->result = 0; | |
6320 | ||
6321 | /* enforce forwards compatibility on users */ | |
ebf4a5db PB |
6322 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6323 | req->flags = 0; | |
b16fed66 | 6324 | return -EINVAL; |
ebf4a5db | 6325 | } |
b16fed66 PB |
6326 | |
6327 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6328 | return -EINVAL; | |
6329 | ||
b16fed66 PB |
6330 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6331 | return -EACCES; | |
6332 | ||
6333 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6334 | !io_op_defs[req->opcode].buffer_select) | |
6335 | return -EOPNOTSUPP; | |
863e0560 | 6336 | |
5730b27e | 6337 | req->work.list.next = NULL; |
003e8dcc JA |
6338 | personality = READ_ONCE(sqe->personality); |
6339 | if (personality) { | |
6340 | req->work.creds = idr_find(&ctx->personality_idr, personality); | |
6341 | if (!req->work.creds) | |
6342 | return -EINVAL; | |
6343 | get_cred(req->work.creds); | |
6344 | } else { | |
6345 | req->work.creds = NULL; | |
6346 | } | |
5730b27e | 6347 | req->work.flags = 0; |
b16fed66 PB |
6348 | state = &ctx->submit_state; |
6349 | ||
6350 | /* | |
6351 | * Plug now if we have more than 1 IO left after this, and the target | |
6352 | * is potentially a read/write to block based storage. | |
6353 | */ | |
6354 | if (!state->plug_started && state->ios_left > 1 && | |
6355 | io_op_defs[req->opcode].plug) { | |
6356 | blk_start_plug(&state->plug); | |
6357 | state->plug_started = true; | |
6358 | } | |
6359 | ||
6360 | if (io_op_defs[req->opcode].needs_file) { | |
6361 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6362 | ||
6363 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6364 | if (unlikely(!req->file)) | |
6365 | ret = -EBADF; | |
6366 | } | |
6367 | ||
6368 | state->ios_left--; | |
6369 | return ret; | |
6370 | } | |
6371 | ||
a6b8cadc | 6372 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6373 | const struct io_uring_sqe *sqe) |
9e645e11 | 6374 | { |
a1ab7b35 | 6375 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6376 | int ret; |
9e645e11 | 6377 | |
a6b8cadc PB |
6378 | ret = io_init_req(ctx, req, sqe); |
6379 | if (unlikely(ret)) { | |
6380 | fail_req: | |
6381 | io_put_req(req); | |
6382 | io_req_complete(req, ret); | |
de59bc10 PB |
6383 | if (link->head) { |
6384 | /* fail even hard links since we don't submit */ | |
cf109604 | 6385 | link->head->flags |= REQ_F_FAIL_LINK; |
de59bc10 PB |
6386 | io_put_req(link->head); |
6387 | io_req_complete(link->head, -ECANCELED); | |
6388 | link->head = NULL; | |
6389 | } | |
a6b8cadc PB |
6390 | return ret; |
6391 | } | |
be7053b7 PB |
6392 | ret = io_req_prep(req, sqe); |
6393 | if (unlikely(ret)) | |
6394 | goto fail_req; | |
a6b8cadc | 6395 | |
be7053b7 | 6396 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6397 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6398 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6399 | ||
9e645e11 JA |
6400 | /* |
6401 | * If we already have a head request, queue this one for async | |
6402 | * submittal once the head completes. If we don't have a head but | |
6403 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6404 | * submitted sync once the chain is complete. If none of those | |
6405 | * conditions are true (normal request), then just queue it. | |
6406 | */ | |
863e0560 PB |
6407 | if (link->head) { |
6408 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6409 | |
8cdf2193 PB |
6410 | /* |
6411 | * Taking sequential execution of a link, draining both sides | |
6412 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6413 | * requests in the link. So, it drains the head and the | |
6414 | * next after the link request. The last one is done via | |
6415 | * drain_next flag to persist the effect across calls. | |
6416 | */ | |
ef4ff581 | 6417 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6418 | head->flags |= REQ_F_IO_DRAIN; |
6419 | ctx->drain_next = 1; | |
6420 | } | |
be7053b7 | 6421 | ret = io_req_defer_prep(req); |
cf109604 | 6422 | if (unlikely(ret)) |
a6b8cadc | 6423 | goto fail_req; |
9d76377f | 6424 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6425 | link->last->link = req; |
863e0560 | 6426 | link->last = req; |
32fe525b PB |
6427 | |
6428 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6429 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6430 | io_queue_sqe(head); |
863e0560 | 6431 | link->head = NULL; |
32fe525b | 6432 | } |
9e645e11 | 6433 | } else { |
711be031 PB |
6434 | if (unlikely(ctx->drain_next)) { |
6435 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6436 | ctx->drain_next = 0; |
711be031 | 6437 | } |
ef4ff581 | 6438 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6439 | link->head = req; |
6440 | link->last = req; | |
711be031 | 6441 | } else { |
be7053b7 | 6442 | io_queue_sqe(req); |
711be031 | 6443 | } |
9e645e11 | 6444 | } |
2e6e1fde | 6445 | |
1d4240cc | 6446 | return 0; |
9e645e11 JA |
6447 | } |
6448 | ||
9a56a232 JA |
6449 | /* |
6450 | * Batched submission is done, ensure local IO is flushed out. | |
6451 | */ | |
ba88ff11 PB |
6452 | static void io_submit_state_end(struct io_submit_state *state, |
6453 | struct io_ring_ctx *ctx) | |
9a56a232 | 6454 | { |
a1ab7b35 | 6455 | if (state->link.head) |
de59bc10 | 6456 | io_queue_sqe(state->link.head); |
6dd0be1e | 6457 | if (state->comp.nr) |
ba88ff11 | 6458 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6459 | if (state->plug_started) |
6460 | blk_finish_plug(&state->plug); | |
9f13c35b | 6461 | io_state_file_put(state); |
9a56a232 JA |
6462 | } |
6463 | ||
6464 | /* | |
6465 | * Start submission side cache. | |
6466 | */ | |
6467 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6468 | unsigned int max_ios) |
9a56a232 | 6469 | { |
27926b68 | 6470 | state->plug_started = false; |
9a56a232 | 6471 | state->ios_left = max_ios; |
a1ab7b35 PB |
6472 | /* set only head, no need to init link_last in advance */ |
6473 | state->link.head = NULL; | |
9a56a232 JA |
6474 | } |
6475 | ||
2b188cc1 JA |
6476 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6477 | { | |
75b28aff | 6478 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6479 | |
caf582c6 PB |
6480 | /* |
6481 | * Ensure any loads from the SQEs are done at this point, | |
6482 | * since once we write the new head, the application could | |
6483 | * write new data to them. | |
6484 | */ | |
6485 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6486 | } |
6487 | ||
2b188cc1 | 6488 | /* |
3529d8c2 | 6489 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6490 | * that is mapped by userspace. This means that care needs to be taken to |
6491 | * ensure that reads are stable, as we cannot rely on userspace always | |
6492 | * being a good citizen. If members of the sqe are validated and then later | |
6493 | * used, it's important that those reads are done through READ_ONCE() to | |
6494 | * prevent a re-load down the line. | |
6495 | */ | |
709b302f | 6496 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6497 | { |
75b28aff | 6498 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6499 | unsigned head; |
6500 | ||
6501 | /* | |
6502 | * The cached sq head (or cq tail) serves two purposes: | |
6503 | * | |
6504 | * 1) allows us to batch the cost of updating the user visible | |
6505 | * head updates. | |
6506 | * 2) allows the kernel side to track the head on its own, even | |
6507 | * though the application is the one updating it. | |
6508 | */ | |
4fccfcbb | 6509 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6510 | if (likely(head < ctx->sq_entries)) |
6511 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6512 | |
6513 | /* drop invalid entries */ | |
498ccd9e | 6514 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6515 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6516 | return NULL; |
6517 | } | |
6518 | ||
0f212204 | 6519 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6520 | { |
46c4e16a | 6521 | int submitted = 0; |
6c271ce2 | 6522 | |
c4a2ed72 | 6523 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6524 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c503150 | 6525 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
ad3eb2c8 JA |
6526 | return -EBUSY; |
6527 | } | |
6c271ce2 | 6528 | |
ee7d46d9 PB |
6529 | /* make sure SQ entry isn't read before tail */ |
6530 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6531 | |
2b85edfc PB |
6532 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6533 | return -EAGAIN; | |
6c271ce2 | 6534 | |
d8a6df10 | 6535 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6536 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6537 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6538 | |
46c4e16a | 6539 | while (submitted < nr) { |
3529d8c2 | 6540 | const struct io_uring_sqe *sqe; |
196be95c | 6541 | struct io_kiocb *req; |
fb5ccc98 | 6542 | |
258b29a9 | 6543 | req = io_alloc_req(ctx); |
196be95c PB |
6544 | if (unlikely(!req)) { |
6545 | if (!submitted) | |
6546 | submitted = -EAGAIN; | |
fb5ccc98 | 6547 | break; |
196be95c | 6548 | } |
4fccfcbb PB |
6549 | sqe = io_get_sqe(ctx); |
6550 | if (unlikely(!sqe)) { | |
6551 | kmem_cache_free(req_cachep, req); | |
6552 | break; | |
6553 | } | |
d3656344 JA |
6554 | /* will complete beyond this point, count as submitted */ |
6555 | submitted++; | |
a1ab7b35 | 6556 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6557 | break; |
6c271ce2 JA |
6558 | } |
6559 | ||
9466f437 PB |
6560 | if (unlikely(submitted != nr)) { |
6561 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6562 | struct io_uring_task *tctx = current->io_uring; |
6563 | int unused = nr - ref_used; | |
9466f437 | 6564 | |
d8a6df10 JA |
6565 | percpu_ref_put_many(&ctx->refs, unused); |
6566 | percpu_counter_sub(&tctx->inflight, unused); | |
6567 | put_task_struct_many(current, unused); | |
9466f437 | 6568 | } |
6c271ce2 | 6569 | |
a1ab7b35 | 6570 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6571 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6572 | io_commit_sqring(ctx); | |
6573 | ||
6c271ce2 JA |
6574 | return submitted; |
6575 | } | |
6576 | ||
23b3628e XW |
6577 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6578 | { | |
6579 | /* Tell userspace we may need a wakeup call */ | |
6580 | spin_lock_irq(&ctx->completion_lock); | |
6581 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6582 | spin_unlock_irq(&ctx->completion_lock); | |
6583 | } | |
6584 | ||
6585 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6586 | { | |
6587 | spin_lock_irq(&ctx->completion_lock); | |
6588 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6589 | spin_unlock_irq(&ctx->completion_lock); | |
6590 | } | |
6591 | ||
08369246 | 6592 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6593 | { |
c8d1ba58 | 6594 | unsigned int to_submit; |
bdcd3eab | 6595 | int ret = 0; |
6c271ce2 | 6596 | |
c8d1ba58 | 6597 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6598 | /* if we're handling multiple rings, cap submit size for fairness */ |
6599 | if (cap_entries && to_submit > 8) | |
6600 | to_submit = 8; | |
6601 | ||
906a3c6f | 6602 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6603 | unsigned nr_events = 0; |
a4c0b3de | 6604 | |
c8d1ba58 | 6605 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6606 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6607 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6608 | |
70aacfe6 | 6609 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs))) |
08369246 | 6610 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6611 | mutex_unlock(&ctx->uring_lock); |
6612 | } | |
6c271ce2 | 6613 | |
90554200 JA |
6614 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6615 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6616 | |
08369246 XW |
6617 | return ret; |
6618 | } | |
6c271ce2 | 6619 | |
08369246 XW |
6620 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6621 | { | |
6622 | struct io_ring_ctx *ctx; | |
6623 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6624 | |
08369246 XW |
6625 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
6626 | if (sq_thread_idle < ctx->sq_thread_idle) | |
6627 | sq_thread_idle = ctx->sq_thread_idle; | |
c8d1ba58 | 6628 | } |
c1edbf5f | 6629 | |
08369246 | 6630 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6631 | } |
6c271ce2 | 6632 | |
69fb2131 JA |
6633 | static void io_sqd_init_new(struct io_sq_data *sqd) |
6634 | { | |
6635 | struct io_ring_ctx *ctx; | |
6636 | ||
6637 | while (!list_empty(&sqd->ctx_new_list)) { | |
6638 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); | |
69fb2131 JA |
6639 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
6640 | complete(&ctx->sq_thread_comp); | |
6641 | } | |
08369246 XW |
6642 | |
6643 | io_sqd_update_thread_idle(sqd); | |
69fb2131 JA |
6644 | } |
6645 | ||
c8d1ba58 JA |
6646 | static int io_sq_thread(void *data) |
6647 | { | |
69fb2131 JA |
6648 | struct io_sq_data *sqd = data; |
6649 | struct io_ring_ctx *ctx; | |
a0d9205f | 6650 | unsigned long timeout = 0; |
37d1e2e3 | 6651 | char buf[TASK_COMM_LEN]; |
08369246 | 6652 | DEFINE_WAIT(wait); |
6c271ce2 | 6653 | |
37d1e2e3 JA |
6654 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
6655 | set_task_comm(current, buf); | |
37d1e2e3 JA |
6656 | current->pf_io_worker = NULL; |
6657 | ||
6658 | if (sqd->sq_cpu != -1) | |
6659 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6660 | else | |
6661 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6662 | current->flags |= PF_NO_SETAFFINITY; | |
6663 | ||
37d1e2e3 JA |
6664 | wait_for_completion(&sqd->startup); |
6665 | ||
05962f95 JA |
6666 | down_read(&sqd->rw_lock); |
6667 | ||
6668 | while (!test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) { | |
08369246 XW |
6669 | int ret; |
6670 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f | 6671 | |
05962f95 JA |
6672 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) { |
6673 | up_read(&sqd->rw_lock); | |
6674 | cond_resched(); | |
6675 | down_read(&sqd->rw_lock); | |
37d1e2e3 | 6676 | continue; |
65b2b213 | 6677 | } |
08369246 | 6678 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
69fb2131 | 6679 | io_sqd_init_new(sqd); |
08369246 XW |
6680 | timeout = jiffies + sqd->sq_thread_idle; |
6681 | } | |
37d1e2e3 JA |
6682 | if (fatal_signal_pending(current)) |
6683 | break; | |
08369246 | 6684 | sqt_spin = false; |
e95eee2d | 6685 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6686 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7c30f36a SM |
6687 | const struct cred *creds = NULL; |
6688 | ||
6689 | if (ctx->sq_creds != current_cred()) | |
6690 | creds = override_creds(ctx->sq_creds); | |
08369246 | 6691 | ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a SM |
6692 | if (creds) |
6693 | revert_creds(creds); | |
08369246 XW |
6694 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
6695 | sqt_spin = true; | |
69fb2131 | 6696 | } |
6c271ce2 | 6697 | |
08369246 | 6698 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6699 | io_run_task_work(); |
6700 | cond_resched(); | |
08369246 XW |
6701 | if (sqt_spin) |
6702 | timeout = jiffies + sqd->sq_thread_idle; | |
6703 | continue; | |
6704 | } | |
6705 | ||
08369246 XW |
6706 | needs_sched = true; |
6707 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6708 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6709 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6710 | !list_empty_careful(&ctx->iopoll_list)) { | |
6711 | needs_sched = false; | |
6712 | break; | |
6713 | } | |
6714 | if (io_sqring_entries(ctx)) { | |
6715 | needs_sched = false; | |
6716 | break; | |
6717 | } | |
6718 | } | |
6719 | ||
05962f95 | 6720 | if (needs_sched && !test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) { |
69fb2131 JA |
6721 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6722 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6723 | |
05962f95 | 6724 | up_read(&sqd->rw_lock); |
69fb2131 | 6725 | schedule(); |
e4b4a13f | 6726 | try_to_freeze(); |
05962f95 | 6727 | down_read(&sqd->rw_lock); |
69fb2131 JA |
6728 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6729 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6730 | } |
08369246 XW |
6731 | |
6732 | finish_wait(&sqd->wait, &wait); | |
6733 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 JA |
6734 | } |
6735 | ||
37d1e2e3 JA |
6736 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6737 | io_uring_cancel_sqpoll(ctx); | |
05962f95 | 6738 | up_read(&sqd->rw_lock); |
06058632 | 6739 | |
37d1e2e3 | 6740 | io_run_task_work(); |
28cea78a | 6741 | |
05962f95 | 6742 | down_write(&sqd->rw_lock); |
37d1e2e3 | 6743 | sqd->thread = NULL; |
05962f95 | 6744 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 6745 | io_ring_set_wakeup_flag(ctx); |
05962f95 | 6746 | up_write(&sqd->rw_lock); |
37d1e2e3 JA |
6747 | complete(&sqd->exited); |
6748 | do_exit(0); | |
6c271ce2 JA |
6749 | } |
6750 | ||
bda52162 JA |
6751 | struct io_wait_queue { |
6752 | struct wait_queue_entry wq; | |
6753 | struct io_ring_ctx *ctx; | |
6754 | unsigned to_wait; | |
6755 | unsigned nr_timeouts; | |
6756 | }; | |
6757 | ||
6c503150 | 6758 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6759 | { |
6760 | struct io_ring_ctx *ctx = iowq->ctx; | |
6761 | ||
6762 | /* | |
d195a66e | 6763 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6764 | * started waiting. For timeouts, we always want to return to userspace, |
6765 | * regardless of event count. | |
6766 | */ | |
6c503150 | 6767 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6768 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6769 | } | |
6770 | ||
6771 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6772 | int wake_flags, void *key) | |
6773 | { | |
6774 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6775 | wq); | |
6776 | ||
6c503150 PB |
6777 | /* |
6778 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6779 | * the task, and the next invocation will do it. | |
6780 | */ | |
6781 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6782 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6783 | return -1; | |
bda52162 JA |
6784 | } |
6785 | ||
af9c1a44 JA |
6786 | static int io_run_task_work_sig(void) |
6787 | { | |
6788 | if (io_run_task_work()) | |
6789 | return 1; | |
6790 | if (!signal_pending(current)) | |
6791 | return 0; | |
792ee0f6 JA |
6792 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
6793 | return -ERESTARTSYS; | |
af9c1a44 JA |
6794 | return -EINTR; |
6795 | } | |
6796 | ||
eeb60b9a PB |
6797 | /* when returns >0, the caller should retry */ |
6798 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6799 | struct io_wait_queue *iowq, | |
6800 | signed long *timeout) | |
6801 | { | |
6802 | int ret; | |
6803 | ||
6804 | /* make sure we run task_work before checking for signals */ | |
6805 | ret = io_run_task_work_sig(); | |
6806 | if (ret || io_should_wake(iowq)) | |
6807 | return ret; | |
6808 | /* let the caller flush overflows, retry */ | |
6809 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6810 | return 1; | |
6811 | ||
6812 | *timeout = schedule_timeout(*timeout); | |
6813 | return !*timeout ? -ETIME : 1; | |
6814 | } | |
6815 | ||
2b188cc1 JA |
6816 | /* |
6817 | * Wait until events become available, if we don't already have some. The | |
6818 | * application must reap them itself, as they reside on the shared cq ring. | |
6819 | */ | |
6820 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
6821 | const sigset_t __user *sig, size_t sigsz, |
6822 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 6823 | { |
bda52162 JA |
6824 | struct io_wait_queue iowq = { |
6825 | .wq = { | |
6826 | .private = current, | |
6827 | .func = io_wake_function, | |
6828 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6829 | }, | |
6830 | .ctx = ctx, | |
6831 | .to_wait = min_events, | |
6832 | }; | |
75b28aff | 6833 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
6834 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
6835 | int ret; | |
2b188cc1 | 6836 | |
b41e9852 | 6837 | do { |
6c503150 PB |
6838 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
6839 | if (io_cqring_events(ctx) >= min_events) | |
b41e9852 | 6840 | return 0; |
4c6e277c | 6841 | if (!io_run_task_work()) |
b41e9852 | 6842 | break; |
b41e9852 | 6843 | } while (1); |
2b188cc1 JA |
6844 | |
6845 | if (sig) { | |
9e75ad5d AB |
6846 | #ifdef CONFIG_COMPAT |
6847 | if (in_compat_syscall()) | |
6848 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 6849 | sigsz); |
9e75ad5d AB |
6850 | else |
6851 | #endif | |
b772434b | 6852 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 6853 | |
2b188cc1 JA |
6854 | if (ret) |
6855 | return ret; | |
6856 | } | |
6857 | ||
c73ebb68 | 6858 | if (uts) { |
c1d5a224 PB |
6859 | struct timespec64 ts; |
6860 | ||
c73ebb68 HX |
6861 | if (get_timespec64(&ts, uts)) |
6862 | return -EFAULT; | |
6863 | timeout = timespec64_to_jiffies(&ts); | |
6864 | } | |
6865 | ||
bda52162 | 6866 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 6867 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 6868 | do { |
ca0a2651 JA |
6869 | /* if we can't even flush overflow, don't wait for more */ |
6870 | if (!io_cqring_overflow_flush(ctx, false, NULL, NULL)) { | |
6871 | ret = -EBUSY; | |
6872 | break; | |
6873 | } | |
bda52162 JA |
6874 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
6875 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
6876 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
6877 | finish_wait(&ctx->wait, &iowq.wq); | |
ca0a2651 | 6878 | cond_resched(); |
eeb60b9a | 6879 | } while (ret > 0); |
bda52162 | 6880 | |
b7db41c9 | 6881 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 6882 | |
75b28aff | 6883 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
6884 | } |
6885 | ||
6b06314c JA |
6886 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
6887 | { | |
6888 | #if defined(CONFIG_UNIX) | |
6889 | if (ctx->ring_sock) { | |
6890 | struct sock *sock = ctx->ring_sock->sk; | |
6891 | struct sk_buff *skb; | |
6892 | ||
6893 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
6894 | kfree_skb(skb); | |
6895 | } | |
6896 | #else | |
6897 | int i; | |
6898 | ||
65e19f54 JA |
6899 | for (i = 0; i < ctx->nr_user_files; i++) { |
6900 | struct file *file; | |
6901 | ||
6902 | file = io_file_from_index(ctx, i); | |
6903 | if (file) | |
6904 | fput(file); | |
6905 | } | |
6b06314c JA |
6906 | #endif |
6907 | } | |
6908 | ||
00835dce | 6909 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 6910 | { |
269bbe5f | 6911 | struct fixed_rsrc_data *data; |
05f3fb3c | 6912 | |
269bbe5f | 6913 | data = container_of(ref, struct fixed_rsrc_data, refs); |
05f3fb3c JA |
6914 | complete(&data->done); |
6915 | } | |
6916 | ||
2a63b2d9 | 6917 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 6918 | { |
2a63b2d9 | 6919 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
6920 | } |
6921 | ||
2a63b2d9 | 6922 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 6923 | { |
2a63b2d9 BM |
6924 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
6925 | } | |
65e19f54 | 6926 | |
d67d2263 BM |
6927 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
6928 | struct fixed_rsrc_data *rsrc_data, | |
269bbe5f | 6929 | struct fixed_rsrc_ref_node *ref_node) |
1642b445 | 6930 | { |
2a63b2d9 | 6931 | io_rsrc_ref_lock(ctx); |
269bbe5f | 6932 | rsrc_data->node = ref_node; |
d67d2263 | 6933 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
2a63b2d9 | 6934 | io_rsrc_ref_unlock(ctx); |
269bbe5f | 6935 | percpu_ref_get(&rsrc_data->refs); |
1642b445 PB |
6936 | } |
6937 | ||
8bad28d8 | 6938 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
6b06314c | 6939 | { |
8bad28d8 | 6940 | struct fixed_rsrc_ref_node *ref_node = NULL; |
6b06314c | 6941 | |
2a63b2d9 | 6942 | io_rsrc_ref_lock(ctx); |
1e5d770b | 6943 | ref_node = data->node; |
e6cb007c | 6944 | data->node = NULL; |
2a63b2d9 | 6945 | io_rsrc_ref_unlock(ctx); |
05589553 XW |
6946 | if (ref_node) |
6947 | percpu_ref_kill(&ref_node->refs); | |
8bad28d8 HX |
6948 | } |
6949 | ||
6950 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, | |
6951 | struct io_ring_ctx *ctx, | |
f2303b1f PB |
6952 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
6953 | struct io_rsrc_put *prsrc)) | |
8bad28d8 | 6954 | { |
f2303b1f | 6955 | struct fixed_rsrc_ref_node *backup_node; |
8bad28d8 | 6956 | int ret; |
05589553 | 6957 | |
8bad28d8 HX |
6958 | if (data->quiesce) |
6959 | return -ENXIO; | |
05589553 | 6960 | |
8bad28d8 | 6961 | data->quiesce = true; |
1ffc5422 | 6962 | do { |
f2303b1f PB |
6963 | ret = -ENOMEM; |
6964 | backup_node = alloc_fixed_rsrc_ref_node(ctx); | |
6965 | if (!backup_node) | |
6966 | break; | |
6967 | backup_node->rsrc_data = data; | |
6968 | backup_node->rsrc_put = rsrc_put; | |
6969 | ||
8bad28d8 HX |
6970 | io_sqe_rsrc_kill_node(ctx, data); |
6971 | percpu_ref_kill(&data->refs); | |
6972 | flush_delayed_work(&ctx->rsrc_put_work); | |
6973 | ||
1ffc5422 PB |
6974 | ret = wait_for_completion_interruptible(&data->done); |
6975 | if (!ret) | |
6976 | break; | |
8bad28d8 | 6977 | |
cb5e1b81 | 6978 | percpu_ref_resurrect(&data->refs); |
8bad28d8 HX |
6979 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
6980 | backup_node = NULL; | |
cb5e1b81 | 6981 | reinit_completion(&data->done); |
8bad28d8 | 6982 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 6983 | ret = io_run_task_work_sig(); |
8bad28d8 | 6984 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 6985 | } while (ret >= 0); |
8bad28d8 | 6986 | data->quiesce = false; |
05f3fb3c | 6987 | |
8bad28d8 HX |
6988 | if (backup_node) |
6989 | destroy_fixed_rsrc_ref_node(backup_node); | |
6990 | return ret; | |
d7954b2b BM |
6991 | } |
6992 | ||
1ad555c6 BM |
6993 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
6994 | { | |
6995 | struct fixed_rsrc_data *data; | |
6996 | ||
6997 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
6998 | if (!data) | |
6999 | return NULL; | |
7000 | ||
00835dce | 7001 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7002 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7003 | kfree(data); | |
7004 | return NULL; | |
7005 | } | |
7006 | data->ctx = ctx; | |
7007 | init_completion(&data->done); | |
7008 | return data; | |
7009 | } | |
7010 | ||
7011 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) | |
7012 | { | |
7013 | percpu_ref_exit(&data->refs); | |
7014 | kfree(data->table); | |
7015 | kfree(data); | |
7016 | } | |
7017 | ||
d7954b2b BM |
7018 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7019 | { | |
7020 | struct fixed_rsrc_data *data = ctx->file_data; | |
d7954b2b BM |
7021 | unsigned nr_tables, i; |
7022 | int ret; | |
7023 | ||
8bad28d8 HX |
7024 | /* |
7025 | * percpu_ref_is_dying() is to stop parallel files unregister | |
7026 | * Since we possibly drop uring lock later in this function to | |
7027 | * run task work. | |
7028 | */ | |
7029 | if (!data || percpu_ref_is_dying(&data->refs)) | |
d7954b2b | 7030 | return -ENXIO; |
f2303b1f | 7031 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
d7954b2b BM |
7032 | if (ret) |
7033 | return ret; | |
7034 | ||
6b06314c | 7035 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
7036 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
7037 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 7038 | kfree(data->table[i].files); |
1ad555c6 | 7039 | free_fixed_rsrc_data(data); |
05f3fb3c | 7040 | ctx->file_data = NULL; |
6b06314c JA |
7041 | ctx->nr_user_files = 0; |
7042 | return 0; | |
7043 | } | |
7044 | ||
37d1e2e3 | 7045 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
05962f95 | 7046 | __releases(&sqd->rw_lock) |
37d1e2e3 | 7047 | { |
37d1e2e3 JA |
7048 | if (sqd->thread == current) |
7049 | return; | |
7050 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
05962f95 | 7051 | up_write(&sqd->rw_lock); |
37d1e2e3 JA |
7052 | } |
7053 | ||
86e0d676 | 7054 | static void io_sq_thread_park(struct io_sq_data *sqd) |
05962f95 | 7055 | __acquires(&sqd->rw_lock) |
37d1e2e3 JA |
7056 | { |
7057 | if (sqd->thread == current) | |
86e0d676 JA |
7058 | return; |
7059 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
05962f95 JA |
7060 | down_write(&sqd->rw_lock); |
7061 | /* set again for consistency, in case concurrent parks are happening */ | |
7062 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
7063 | if (sqd->thread) | |
86e0d676 | 7064 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7065 | } |
7066 | ||
7067 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7068 | { | |
e54945ae | 7069 | if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) |
37d1e2e3 | 7070 | return; |
05962f95 JA |
7071 | down_write(&sqd->rw_lock); |
7072 | if (!sqd->thread) { | |
7073 | up_write(&sqd->rw_lock); | |
7074 | return; | |
e54945ae | 7075 | } |
05962f95 JA |
7076 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7077 | wake_up_process(sqd->thread); | |
7078 | up_write(&sqd->rw_lock); | |
7079 | wait_for_completion(&sqd->exited); | |
37d1e2e3 JA |
7080 | } |
7081 | ||
534ca6d6 | 7082 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7083 | { |
534ca6d6 | 7084 | if (refcount_dec_and_test(&sqd->refs)) { |
37d1e2e3 JA |
7085 | io_sq_thread_stop(sqd); |
7086 | kfree(sqd); | |
7087 | } | |
7088 | } | |
7089 | ||
7090 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7091 | { | |
7092 | struct io_sq_data *sqd = ctx->sq_data; | |
7093 | ||
7094 | if (sqd) { | |
eb85890b | 7095 | complete(&sqd->startup); |
05962f95 | 7096 | if (sqd->thread) |
37d1e2e3 | 7097 | wait_for_completion(&ctx->sq_thread_comp); |
534ca6d6 | 7098 | |
05962f95 | 7099 | io_sq_thread_park(sqd); |
37d1e2e3 JA |
7100 | list_del(&ctx->sqd_list); |
7101 | io_sqd_update_thread_idle(sqd); | |
05962f95 | 7102 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7103 | |
7104 | io_put_sq_data(sqd); | |
7105 | ctx->sq_data = NULL; | |
7c30f36a SM |
7106 | if (ctx->sq_creds) |
7107 | put_cred(ctx->sq_creds); | |
534ca6d6 JA |
7108 | } |
7109 | } | |
7110 | ||
aa06165d JA |
7111 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7112 | { | |
7113 | struct io_ring_ctx *ctx_attach; | |
7114 | struct io_sq_data *sqd; | |
7115 | struct fd f; | |
7116 | ||
7117 | f = fdget(p->wq_fd); | |
7118 | if (!f.file) | |
7119 | return ERR_PTR(-ENXIO); | |
7120 | if (f.file->f_op != &io_uring_fops) { | |
7121 | fdput(f); | |
7122 | return ERR_PTR(-EINVAL); | |
7123 | } | |
7124 | ||
7125 | ctx_attach = f.file->private_data; | |
7126 | sqd = ctx_attach->sq_data; | |
7127 | if (!sqd) { | |
7128 | fdput(f); | |
7129 | return ERR_PTR(-EINVAL); | |
7130 | } | |
7131 | ||
7132 | refcount_inc(&sqd->refs); | |
7133 | fdput(f); | |
7134 | return sqd; | |
7135 | } | |
7136 | ||
534ca6d6 JA |
7137 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
7138 | { | |
7139 | struct io_sq_data *sqd; | |
7140 | ||
aa06165d JA |
7141 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
7142 | return io_attach_sq_data(p); | |
7143 | ||
534ca6d6 JA |
7144 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7145 | if (!sqd) | |
7146 | return ERR_PTR(-ENOMEM); | |
7147 | ||
7148 | refcount_set(&sqd->refs, 1); | |
69fb2131 JA |
7149 | INIT_LIST_HEAD(&sqd->ctx_list); |
7150 | INIT_LIST_HEAD(&sqd->ctx_new_list); | |
05962f95 | 7151 | init_rwsem(&sqd->rw_lock); |
534ca6d6 | 7152 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7153 | init_completion(&sqd->startup); |
37d1e2e3 | 7154 | init_completion(&sqd->exited); |
534ca6d6 JA |
7155 | return sqd; |
7156 | } | |
7157 | ||
6b06314c | 7158 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7159 | /* |
7160 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7161 | * the io_uring can be safely unregistered on process exit, even if we have | |
7162 | * loops in the file referencing. | |
7163 | */ | |
7164 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7165 | { | |
7166 | struct sock *sk = ctx->ring_sock->sk; | |
7167 | struct scm_fp_list *fpl; | |
7168 | struct sk_buff *skb; | |
08a45173 | 7169 | int i, nr_files; |
6b06314c | 7170 | |
6b06314c JA |
7171 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7172 | if (!fpl) | |
7173 | return -ENOMEM; | |
7174 | ||
7175 | skb = alloc_skb(0, GFP_KERNEL); | |
7176 | if (!skb) { | |
7177 | kfree(fpl); | |
7178 | return -ENOMEM; | |
7179 | } | |
7180 | ||
7181 | skb->sk = sk; | |
6b06314c | 7182 | |
08a45173 | 7183 | nr_files = 0; |
62e398be | 7184 | fpl->user = get_uid(current_user()); |
6b06314c | 7185 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7186 | struct file *file = io_file_from_index(ctx, i + offset); |
7187 | ||
7188 | if (!file) | |
08a45173 | 7189 | continue; |
65e19f54 | 7190 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7191 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7192 | nr_files++; | |
6b06314c JA |
7193 | } |
7194 | ||
08a45173 JA |
7195 | if (nr_files) { |
7196 | fpl->max = SCM_MAX_FD; | |
7197 | fpl->count = nr_files; | |
7198 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7199 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7200 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7201 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7202 | |
08a45173 JA |
7203 | for (i = 0; i < nr_files; i++) |
7204 | fput(fpl->fp[i]); | |
7205 | } else { | |
7206 | kfree_skb(skb); | |
7207 | kfree(fpl); | |
7208 | } | |
6b06314c JA |
7209 | |
7210 | return 0; | |
7211 | } | |
7212 | ||
7213 | /* | |
7214 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7215 | * causes regular reference counting to break down. We rely on the UNIX | |
7216 | * garbage collection to take care of this problem for us. | |
7217 | */ | |
7218 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7219 | { | |
7220 | unsigned left, total; | |
7221 | int ret = 0; | |
7222 | ||
7223 | total = 0; | |
7224 | left = ctx->nr_user_files; | |
7225 | while (left) { | |
7226 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7227 | |
7228 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7229 | if (ret) | |
7230 | break; | |
7231 | left -= this_files; | |
7232 | total += this_files; | |
7233 | } | |
7234 | ||
7235 | if (!ret) | |
7236 | return 0; | |
7237 | ||
7238 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7239 | struct file *file = io_file_from_index(ctx, total); |
7240 | ||
7241 | if (file) | |
7242 | fput(file); | |
6b06314c JA |
7243 | total++; |
7244 | } | |
7245 | ||
7246 | return ret; | |
7247 | } | |
7248 | #else | |
7249 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7250 | { | |
7251 | return 0; | |
7252 | } | |
7253 | #endif | |
7254 | ||
269bbe5f | 7255 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
5398ae69 | 7256 | unsigned nr_tables, unsigned nr_files) |
65e19f54 JA |
7257 | { |
7258 | int i; | |
7259 | ||
7260 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7261 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7262 | unsigned this_files; |
7263 | ||
7264 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
7265 | table->files = kcalloc(this_files, sizeof(struct file *), | |
7266 | GFP_KERNEL); | |
7267 | if (!table->files) | |
7268 | break; | |
7269 | nr_files -= this_files; | |
7270 | } | |
7271 | ||
7272 | if (i == nr_tables) | |
7273 | return 0; | |
7274 | ||
7275 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7276 | struct fixed_rsrc_table *table = &file_data->table[i]; |
65e19f54 JA |
7277 | kfree(table->files); |
7278 | } | |
7279 | return 1; | |
7280 | } | |
7281 | ||
50238531 | 7282 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7283 | { |
50238531 | 7284 | struct file *file = prsrc->file; |
05f3fb3c JA |
7285 | #if defined(CONFIG_UNIX) |
7286 | struct sock *sock = ctx->ring_sock->sk; | |
7287 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7288 | struct sk_buff *skb; | |
7289 | int i; | |
7290 | ||
7291 | __skb_queue_head_init(&list); | |
7292 | ||
7293 | /* | |
7294 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7295 | * remove this entry and rearrange the file array. | |
7296 | */ | |
7297 | skb = skb_dequeue(head); | |
7298 | while (skb) { | |
7299 | struct scm_fp_list *fp; | |
7300 | ||
7301 | fp = UNIXCB(skb).fp; | |
7302 | for (i = 0; i < fp->count; i++) { | |
7303 | int left; | |
7304 | ||
7305 | if (fp->fp[i] != file) | |
7306 | continue; | |
7307 | ||
7308 | unix_notinflight(fp->user, fp->fp[i]); | |
7309 | left = fp->count - 1 - i; | |
7310 | if (left) { | |
7311 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7312 | left * sizeof(struct file *)); | |
7313 | } | |
7314 | fp->count--; | |
7315 | if (!fp->count) { | |
7316 | kfree_skb(skb); | |
7317 | skb = NULL; | |
7318 | } else { | |
7319 | __skb_queue_tail(&list, skb); | |
7320 | } | |
7321 | fput(file); | |
7322 | file = NULL; | |
7323 | break; | |
7324 | } | |
7325 | ||
7326 | if (!file) | |
7327 | break; | |
7328 | ||
7329 | __skb_queue_tail(&list, skb); | |
7330 | ||
7331 | skb = skb_dequeue(head); | |
7332 | } | |
7333 | ||
7334 | if (skb_peek(&list)) { | |
7335 | spin_lock_irq(&head->lock); | |
7336 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7337 | __skb_queue_tail(head, skb); | |
7338 | spin_unlock_irq(&head->lock); | |
7339 | } | |
7340 | #else | |
7341 | fput(file); | |
7342 | #endif | |
7343 | } | |
7344 | ||
269bbe5f | 7345 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
65e19f54 | 7346 | { |
269bbe5f BM |
7347 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
7348 | struct io_ring_ctx *ctx = rsrc_data->ctx; | |
7349 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7350 | |
269bbe5f BM |
7351 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7352 | list_del(&prsrc->list); | |
50238531 | 7353 | ref_node->rsrc_put(ctx, prsrc); |
269bbe5f | 7354 | kfree(prsrc); |
65e19f54 | 7355 | } |
05589553 | 7356 | |
05589553 XW |
7357 | percpu_ref_exit(&ref_node->refs); |
7358 | kfree(ref_node); | |
269bbe5f | 7359 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7360 | } |
65e19f54 | 7361 | |
269bbe5f | 7362 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7363 | { |
7364 | struct io_ring_ctx *ctx; | |
7365 | struct llist_node *node; | |
7366 | ||
269bbe5f BM |
7367 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7368 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7369 | |
7370 | while (node) { | |
269bbe5f | 7371 | struct fixed_rsrc_ref_node *ref_node; |
4a38aed2 JA |
7372 | struct llist_node *next = node->next; |
7373 | ||
269bbe5f BM |
7374 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
7375 | __io_rsrc_put_work(ref_node); | |
4a38aed2 JA |
7376 | node = next; |
7377 | } | |
7378 | } | |
7379 | ||
ea64ec02 PB |
7380 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
7381 | unsigned i) | |
2faf852d | 7382 | { |
ea64ec02 PB |
7383 | struct fixed_rsrc_table *table; |
7384 | ||
7385 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
7386 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
7387 | } | |
7388 | ||
00835dce | 7389 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7390 | { |
269bbe5f BM |
7391 | struct fixed_rsrc_ref_node *ref_node; |
7392 | struct fixed_rsrc_data *data; | |
4a38aed2 | 7393 | struct io_ring_ctx *ctx; |
e297822b | 7394 | bool first_add = false; |
4a38aed2 | 7395 | int delay = HZ; |
65e19f54 | 7396 | |
269bbe5f BM |
7397 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
7398 | data = ref_node->rsrc_data; | |
e297822b PB |
7399 | ctx = data->ctx; |
7400 | ||
2a63b2d9 | 7401 | io_rsrc_ref_lock(ctx); |
e297822b PB |
7402 | ref_node->done = true; |
7403 | ||
d67d2263 BM |
7404 | while (!list_empty(&ctx->rsrc_ref_list)) { |
7405 | ref_node = list_first_entry(&ctx->rsrc_ref_list, | |
269bbe5f | 7406 | struct fixed_rsrc_ref_node, node); |
e297822b PB |
7407 | /* recycle ref nodes in order */ |
7408 | if (!ref_node->done) | |
7409 | break; | |
7410 | list_del(&ref_node->node); | |
269bbe5f | 7411 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
e297822b | 7412 | } |
2a63b2d9 | 7413 | io_rsrc_ref_unlock(ctx); |
05589553 | 7414 | |
e297822b | 7415 | if (percpu_ref_is_dying(&data->refs)) |
4a38aed2 | 7416 | delay = 0; |
05589553 | 7417 | |
4a38aed2 | 7418 | if (!delay) |
269bbe5f | 7419 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
4a38aed2 | 7420 | else if (first_add) |
269bbe5f | 7421 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
05f3fb3c | 7422 | } |
65e19f54 | 7423 | |
6802535d | 7424 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
05589553 | 7425 | struct io_ring_ctx *ctx) |
05f3fb3c | 7426 | { |
269bbe5f | 7427 | struct fixed_rsrc_ref_node *ref_node; |
05f3fb3c | 7428 | |
05589553 XW |
7429 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7430 | if (!ref_node) | |
3e2224c5 | 7431 | return NULL; |
05f3fb3c | 7432 | |
00835dce | 7433 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7434 | 0, GFP_KERNEL)) { |
7435 | kfree(ref_node); | |
3e2224c5 | 7436 | return NULL; |
05589553 XW |
7437 | } |
7438 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7439 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7440 | ref_node->done = false; |
05589553 | 7441 | return ref_node; |
05589553 XW |
7442 | } |
7443 | ||
bc9744cd PB |
7444 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
7445 | struct fixed_rsrc_ref_node *ref_node) | |
6802535d | 7446 | { |
269bbe5f | 7447 | ref_node->rsrc_data = ctx->file_data; |
50238531 | 7448 | ref_node->rsrc_put = io_ring_file_put; |
05589553 XW |
7449 | } |
7450 | ||
269bbe5f | 7451 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
05589553 XW |
7452 | { |
7453 | percpu_ref_exit(&ref_node->refs); | |
7454 | kfree(ref_node); | |
65e19f54 JA |
7455 | } |
7456 | ||
ea64ec02 | 7457 | |
6b06314c JA |
7458 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7459 | unsigned nr_args) | |
7460 | { | |
7461 | __s32 __user *fds = (__s32 __user *) arg; | |
600cf3f8 | 7462 | unsigned nr_tables, i; |
05f3fb3c | 7463 | struct file *file; |
600cf3f8 | 7464 | int fd, ret = -ENOMEM; |
269bbe5f BM |
7465 | struct fixed_rsrc_ref_node *ref_node; |
7466 | struct fixed_rsrc_data *file_data; | |
6b06314c | 7467 | |
05f3fb3c | 7468 | if (ctx->file_data) |
6b06314c JA |
7469 | return -EBUSY; |
7470 | if (!nr_args) | |
7471 | return -EINVAL; | |
7472 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7473 | return -EMFILE; | |
7474 | ||
1ad555c6 | 7475 | file_data = alloc_fixed_rsrc_data(ctx); |
5398ae69 | 7476 | if (!file_data) |
05f3fb3c | 7477 | return -ENOMEM; |
13770a71 | 7478 | ctx->file_data = file_data; |
05f3fb3c | 7479 | |
65e19f54 | 7480 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
035fbafc | 7481 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
5398ae69 | 7482 | GFP_KERNEL); |
600cf3f8 PB |
7483 | if (!file_data->table) |
7484 | goto out_free; | |
05f3fb3c | 7485 | |
600cf3f8 | 7486 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
1ad555c6 | 7487 | goto out_free; |
65e19f54 | 7488 | |
08a45173 | 7489 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7490 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7491 | ret = -EFAULT; | |
7492 | goto out_fput; | |
7493 | } | |
08a45173 | 7494 | /* allow sparse sets */ |
600cf3f8 | 7495 | if (fd == -1) |
08a45173 | 7496 | continue; |
6b06314c | 7497 | |
05f3fb3c | 7498 | file = fget(fd); |
6b06314c | 7499 | ret = -EBADF; |
05f3fb3c | 7500 | if (!file) |
600cf3f8 | 7501 | goto out_fput; |
05f3fb3c | 7502 | |
6b06314c JA |
7503 | /* |
7504 | * Don't allow io_uring instances to be registered. If UNIX | |
7505 | * isn't enabled, then this causes a reference cycle and this | |
7506 | * instance can never get freed. If UNIX is enabled we'll | |
7507 | * handle it just fine, but there's still no point in allowing | |
7508 | * a ring fd as it doesn't support regular read/write anyway. | |
7509 | */ | |
05f3fb3c JA |
7510 | if (file->f_op == &io_uring_fops) { |
7511 | fput(file); | |
600cf3f8 | 7512 | goto out_fput; |
6b06314c | 7513 | } |
ea64ec02 | 7514 | *io_fixed_file_slot(file_data, i) = file; |
6b06314c JA |
7515 | } |
7516 | ||
6b06314c | 7517 | ret = io_sqe_files_scm(ctx); |
05589553 | 7518 | if (ret) { |
6b06314c | 7519 | io_sqe_files_unregister(ctx); |
05589553 XW |
7520 | return ret; |
7521 | } | |
6b06314c | 7522 | |
bc9744cd | 7523 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 | 7524 | if (!ref_node) { |
05589553 | 7525 | io_sqe_files_unregister(ctx); |
3e2224c5 | 7526 | return -ENOMEM; |
05589553 | 7527 | } |
bc9744cd | 7528 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7529 | |
d67d2263 | 7530 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
6b06314c | 7531 | return ret; |
600cf3f8 PB |
7532 | out_fput: |
7533 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7534 | file = io_file_from_index(ctx, i); | |
7535 | if (file) | |
7536 | fput(file); | |
7537 | } | |
7538 | for (i = 0; i < nr_tables; i++) | |
7539 | kfree(file_data->table[i].files); | |
7540 | ctx->nr_user_files = 0; | |
600cf3f8 | 7541 | out_free: |
1ad555c6 | 7542 | free_fixed_rsrc_data(ctx->file_data); |
55cbc256 | 7543 | ctx->file_data = NULL; |
6b06314c JA |
7544 | return ret; |
7545 | } | |
7546 | ||
c3a31e60 JA |
7547 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7548 | int index) | |
7549 | { | |
7550 | #if defined(CONFIG_UNIX) | |
7551 | struct sock *sock = ctx->ring_sock->sk; | |
7552 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7553 | struct sk_buff *skb; | |
7554 | ||
7555 | /* | |
7556 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7557 | * file set. If there's no room, fall back to allocating a new skb | |
7558 | * and filling it in. | |
7559 | */ | |
7560 | spin_lock_irq(&head->lock); | |
7561 | skb = skb_peek(head); | |
7562 | if (skb) { | |
7563 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7564 | ||
7565 | if (fpl->count < SCM_MAX_FD) { | |
7566 | __skb_unlink(skb, head); | |
7567 | spin_unlock_irq(&head->lock); | |
7568 | fpl->fp[fpl->count] = get_file(file); | |
7569 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7570 | fpl->count++; | |
7571 | spin_lock_irq(&head->lock); | |
7572 | __skb_queue_head(head, skb); | |
7573 | } else { | |
7574 | skb = NULL; | |
7575 | } | |
7576 | } | |
7577 | spin_unlock_irq(&head->lock); | |
7578 | ||
7579 | if (skb) { | |
7580 | fput(file); | |
7581 | return 0; | |
7582 | } | |
7583 | ||
7584 | return __io_sqe_files_scm(ctx, 1, index); | |
7585 | #else | |
7586 | return 0; | |
7587 | #endif | |
7588 | } | |
7589 | ||
50238531 | 7590 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
05f3fb3c | 7591 | { |
269bbe5f BM |
7592 | struct io_rsrc_put *prsrc; |
7593 | struct fixed_rsrc_ref_node *ref_node = data->node; | |
05f3fb3c | 7594 | |
269bbe5f BM |
7595 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7596 | if (!prsrc) | |
a5318d3c | 7597 | return -ENOMEM; |
05f3fb3c | 7598 | |
50238531 | 7599 | prsrc->rsrc = rsrc; |
269bbe5f | 7600 | list_add(&prsrc->list, &ref_node->rsrc_list); |
05589553 | 7601 | |
a5318d3c | 7602 | return 0; |
05f3fb3c JA |
7603 | } |
7604 | ||
269bbe5f BM |
7605 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
7606 | struct file *file) | |
7607 | { | |
50238531 | 7608 | return io_queue_rsrc_removal(data, (void *)file); |
269bbe5f BM |
7609 | } |
7610 | ||
05f3fb3c | 7611 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 7612 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7613 | unsigned nr_args) |
7614 | { | |
269bbe5f BM |
7615 | struct fixed_rsrc_data *data = ctx->file_data; |
7616 | struct fixed_rsrc_ref_node *ref_node; | |
ea64ec02 | 7617 | struct file *file, **file_slot; |
c3a31e60 JA |
7618 | __s32 __user *fds; |
7619 | int fd, i, err; | |
7620 | __u32 done; | |
05589553 | 7621 | bool needs_switch = false; |
c3a31e60 | 7622 | |
05f3fb3c | 7623 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7624 | return -EOVERFLOW; |
7625 | if (done > ctx->nr_user_files) | |
7626 | return -EINVAL; | |
7627 | ||
bc9744cd | 7628 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
3e2224c5 MWO |
7629 | if (!ref_node) |
7630 | return -ENOMEM; | |
bc9744cd | 7631 | init_fixed_file_ref_node(ctx, ref_node); |
05589553 | 7632 | |
269bbe5f | 7633 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7634 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7635 | err = 0; |
7636 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7637 | err = -EFAULT; | |
7638 | break; | |
7639 | } | |
4e0377a1 | 7640 | if (fd == IORING_REGISTER_FILES_SKIP) |
7641 | continue; | |
7642 | ||
67973b93 | 7643 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7644 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7645 | ||
7646 | if (*file_slot) { | |
7647 | err = io_queue_file_removal(data, *file_slot); | |
a5318d3c HD |
7648 | if (err) |
7649 | break; | |
ea64ec02 | 7650 | *file_slot = NULL; |
05589553 | 7651 | needs_switch = true; |
c3a31e60 JA |
7652 | } |
7653 | if (fd != -1) { | |
c3a31e60 JA |
7654 | file = fget(fd); |
7655 | if (!file) { | |
7656 | err = -EBADF; | |
7657 | break; | |
7658 | } | |
7659 | /* | |
7660 | * Don't allow io_uring instances to be registered. If | |
7661 | * UNIX isn't enabled, then this causes a reference | |
7662 | * cycle and this instance can never get freed. If UNIX | |
7663 | * is enabled we'll handle it just fine, but there's | |
7664 | * still no point in allowing a ring fd as it doesn't | |
7665 | * support regular read/write anyway. | |
7666 | */ | |
7667 | if (file->f_op == &io_uring_fops) { | |
7668 | fput(file); | |
7669 | err = -EBADF; | |
7670 | break; | |
7671 | } | |
e68a3ff8 | 7672 | *file_slot = file; |
c3a31e60 | 7673 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7674 | if (err) { |
e68a3ff8 | 7675 | *file_slot = NULL; |
f3bd9dae | 7676 | fput(file); |
c3a31e60 | 7677 | break; |
f3bd9dae | 7678 | } |
c3a31e60 | 7679 | } |
05f3fb3c JA |
7680 | } |
7681 | ||
05589553 | 7682 | if (needs_switch) { |
b2e96852 | 7683 | percpu_ref_kill(&data->node->refs); |
d67d2263 | 7684 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
05589553 | 7685 | } else |
269bbe5f | 7686 | destroy_fixed_rsrc_ref_node(ref_node); |
c3a31e60 JA |
7687 | |
7688 | return done ? done : err; | |
7689 | } | |
05589553 | 7690 | |
05f3fb3c JA |
7691 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7692 | unsigned nr_args) | |
7693 | { | |
269bbe5f | 7694 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7695 | |
7696 | if (!ctx->file_data) | |
7697 | return -ENXIO; | |
7698 | if (!nr_args) | |
7699 | return -EINVAL; | |
7700 | if (copy_from_user(&up, arg, sizeof(up))) | |
7701 | return -EFAULT; | |
7702 | if (up.resv) | |
7703 | return -EINVAL; | |
7704 | ||
7705 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7706 | } | |
c3a31e60 | 7707 | |
5280f7e5 | 7708 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7709 | { |
7710 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7711 | ||
5280f7e5 PB |
7712 | req = io_put_req_find_next(req); |
7713 | return req ? &req->work : NULL; | |
7d723065 JA |
7714 | } |
7715 | ||
5aa75ed5 | 7716 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
24369c2e | 7717 | { |
e941894e | 7718 | struct io_wq_hash *hash; |
24369c2e | 7719 | struct io_wq_data data; |
24369c2e | 7720 | unsigned int concurrency; |
24369c2e | 7721 | |
e941894e JA |
7722 | hash = ctx->hash_map; |
7723 | if (!hash) { | |
7724 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
7725 | if (!hash) | |
7726 | return ERR_PTR(-ENOMEM); | |
7727 | refcount_set(&hash->refs, 1); | |
7728 | init_waitqueue_head(&hash->wait); | |
7729 | ctx->hash_map = hash; | |
24369c2e PB |
7730 | } |
7731 | ||
e941894e | 7732 | data.hash = hash; |
e9fd9396 | 7733 | data.free_work = io_free_work; |
f5fa38c5 | 7734 | data.do_work = io_wq_submit_work; |
24369c2e | 7735 | |
d25e3a3d JA |
7736 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7737 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7738 | |
5aa75ed5 | 7739 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7740 | } |
7741 | ||
5aa75ed5 JA |
7742 | static int io_uring_alloc_task_context(struct task_struct *task, |
7743 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7744 | { |
7745 | struct io_uring_task *tctx; | |
d8a6df10 | 7746 | int ret; |
0f212204 JA |
7747 | |
7748 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7749 | if (unlikely(!tctx)) | |
7750 | return -ENOMEM; | |
7751 | ||
d8a6df10 JA |
7752 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7753 | if (unlikely(ret)) { | |
7754 | kfree(tctx); | |
7755 | return ret; | |
7756 | } | |
7757 | ||
5aa75ed5 JA |
7758 | tctx->io_wq = io_init_wq_offload(ctx); |
7759 | if (IS_ERR(tctx->io_wq)) { | |
7760 | ret = PTR_ERR(tctx->io_wq); | |
7761 | percpu_counter_destroy(&tctx->inflight); | |
7762 | kfree(tctx); | |
7763 | return ret; | |
7764 | } | |
7765 | ||
0f212204 JA |
7766 | xa_init(&tctx->xa); |
7767 | init_waitqueue_head(&tctx->wait); | |
7768 | tctx->last = NULL; | |
fdaf083c JA |
7769 | atomic_set(&tctx->in_idle, 0); |
7770 | tctx->sqpoll = false; | |
0f212204 | 7771 | task->io_uring = tctx; |
7cbf1722 JA |
7772 | spin_lock_init(&tctx->task_lock); |
7773 | INIT_WQ_LIST(&tctx->task_list); | |
7774 | tctx->task_state = 0; | |
7775 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7776 | return 0; |
7777 | } | |
7778 | ||
7779 | void __io_uring_free(struct task_struct *tsk) | |
7780 | { | |
7781 | struct io_uring_task *tctx = tsk->io_uring; | |
7782 | ||
7783 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e PB |
7784 | WARN_ON_ONCE(tctx->io_wq); |
7785 | ||
d8a6df10 | 7786 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7787 | kfree(tctx); |
7788 | tsk->io_uring = NULL; | |
7789 | } | |
7790 | ||
7e84e1c7 SG |
7791 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7792 | struct io_uring_params *p) | |
2b188cc1 JA |
7793 | { |
7794 | int ret; | |
7795 | ||
d25e3a3d JA |
7796 | /* Retain compatibility with failing for an invalid attach attempt */ |
7797 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7798 | IORING_SETUP_ATTACH_WQ) { | |
7799 | struct fd f; | |
7800 | ||
7801 | f = fdget(p->wq_fd); | |
7802 | if (!f.file) | |
7803 | return -ENXIO; | |
7804 | if (f.file->f_op != &io_uring_fops) { | |
7805 | fdput(f); | |
7806 | return -EINVAL; | |
7807 | } | |
7808 | fdput(f); | |
7809 | } | |
6c271ce2 | 7810 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 7811 | struct task_struct *tsk; |
534ca6d6 JA |
7812 | struct io_sq_data *sqd; |
7813 | ||
3ec482d1 | 7814 | ret = -EPERM; |
ce59fc69 | 7815 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
3ec482d1 JA |
7816 | goto err; |
7817 | ||
534ca6d6 JA |
7818 | sqd = io_get_sq_data(p); |
7819 | if (IS_ERR(sqd)) { | |
7820 | ret = PTR_ERR(sqd); | |
7821 | goto err; | |
7822 | } | |
69fb2131 | 7823 | |
7c30f36a | 7824 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 7825 | ctx->sq_data = sqd; |
69fb2131 | 7826 | io_sq_thread_park(sqd); |
69fb2131 | 7827 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
69fb2131 | 7828 | io_sq_thread_unpark(sqd); |
534ca6d6 | 7829 | |
917257da JA |
7830 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7831 | if (!ctx->sq_thread_idle) | |
7832 | ctx->sq_thread_idle = HZ; | |
7833 | ||
aa06165d | 7834 | if (sqd->thread) |
5aa75ed5 | 7835 | return 0; |
aa06165d | 7836 | |
6c271ce2 | 7837 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7838 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7839 | |
917257da | 7840 | ret = -EINVAL; |
44a9bd18 JA |
7841 | if (cpu >= nr_cpu_ids) |
7842 | goto err; | |
7889f44d | 7843 | if (!cpu_online(cpu)) |
917257da JA |
7844 | goto err; |
7845 | ||
37d1e2e3 | 7846 | sqd->sq_cpu = cpu; |
6c271ce2 | 7847 | } else { |
37d1e2e3 | 7848 | sqd->sq_cpu = -1; |
6c271ce2 | 7849 | } |
37d1e2e3 JA |
7850 | |
7851 | sqd->task_pid = current->pid; | |
46fe18b1 JA |
7852 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
7853 | if (IS_ERR(tsk)) { | |
7854 | ret = PTR_ERR(tsk); | |
6c271ce2 JA |
7855 | goto err; |
7856 | } | |
46fe18b1 JA |
7857 | ret = io_uring_alloc_task_context(tsk, ctx); |
7858 | if (ret) | |
7859 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); | |
7860 | sqd->thread = tsk; | |
7861 | wake_up_new_task(tsk); | |
0f212204 JA |
7862 | if (ret) |
7863 | goto err; | |
6c271ce2 JA |
7864 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
7865 | /* Can't have SQ_AFF without SQPOLL */ | |
7866 | ret = -EINVAL; | |
7867 | goto err; | |
7868 | } | |
7869 | ||
2b188cc1 JA |
7870 | return 0; |
7871 | err: | |
37d1e2e3 | 7872 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
7873 | return ret; |
7874 | } | |
7875 | ||
7e84e1c7 SG |
7876 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
7877 | { | |
534ca6d6 JA |
7878 | struct io_sq_data *sqd = ctx->sq_data; |
7879 | ||
3ebba796 | 7880 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
eb85890b | 7881 | if (ctx->flags & IORING_SETUP_SQPOLL) |
37d1e2e3 | 7882 | complete(&sqd->startup); |
7e84e1c7 SG |
7883 | } |
7884 | ||
a087e2b5 BM |
7885 | static inline void __io_unaccount_mem(struct user_struct *user, |
7886 | unsigned long nr_pages) | |
2b188cc1 JA |
7887 | { |
7888 | atomic_long_sub(nr_pages, &user->locked_vm); | |
7889 | } | |
7890 | ||
a087e2b5 BM |
7891 | static inline int __io_account_mem(struct user_struct *user, |
7892 | unsigned long nr_pages) | |
2b188cc1 JA |
7893 | { |
7894 | unsigned long page_limit, cur_pages, new_pages; | |
7895 | ||
7896 | /* Don't allow more pages than we can safely lock */ | |
7897 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
7898 | ||
7899 | do { | |
7900 | cur_pages = atomic_long_read(&user->locked_vm); | |
7901 | new_pages = cur_pages + nr_pages; | |
7902 | if (new_pages > page_limit) | |
7903 | return -ENOMEM; | |
7904 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
7905 | new_pages) != cur_pages); | |
7906 | ||
7907 | return 0; | |
7908 | } | |
7909 | ||
26bfa89e | 7910 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7911 | { |
62e398be | 7912 | if (ctx->user) |
a087e2b5 | 7913 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 7914 | |
26bfa89e JA |
7915 | if (ctx->mm_account) |
7916 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7917 | } |
7918 | ||
26bfa89e | 7919 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 7920 | { |
30975825 BM |
7921 | int ret; |
7922 | ||
62e398be | 7923 | if (ctx->user) { |
30975825 BM |
7924 | ret = __io_account_mem(ctx->user, nr_pages); |
7925 | if (ret) | |
7926 | return ret; | |
7927 | } | |
7928 | ||
26bfa89e JA |
7929 | if (ctx->mm_account) |
7930 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
7931 | |
7932 | return 0; | |
7933 | } | |
7934 | ||
2b188cc1 JA |
7935 | static void io_mem_free(void *ptr) |
7936 | { | |
52e04ef4 MR |
7937 | struct page *page; |
7938 | ||
7939 | if (!ptr) | |
7940 | return; | |
2b188cc1 | 7941 | |
52e04ef4 | 7942 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
7943 | if (put_page_testzero(page)) |
7944 | free_compound_page(page); | |
7945 | } | |
7946 | ||
7947 | static void *io_mem_alloc(size_t size) | |
7948 | { | |
7949 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 7950 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
7951 | |
7952 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
7953 | } | |
7954 | ||
75b28aff HV |
7955 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
7956 | size_t *sq_offset) | |
7957 | { | |
7958 | struct io_rings *rings; | |
7959 | size_t off, sq_array_size; | |
7960 | ||
7961 | off = struct_size(rings, cqes, cq_entries); | |
7962 | if (off == SIZE_MAX) | |
7963 | return SIZE_MAX; | |
7964 | ||
7965 | #ifdef CONFIG_SMP | |
7966 | off = ALIGN(off, SMP_CACHE_BYTES); | |
7967 | if (off == 0) | |
7968 | return SIZE_MAX; | |
7969 | #endif | |
7970 | ||
b36200f5 DV |
7971 | if (sq_offset) |
7972 | *sq_offset = off; | |
7973 | ||
75b28aff HV |
7974 | sq_array_size = array_size(sizeof(u32), sq_entries); |
7975 | if (sq_array_size == SIZE_MAX) | |
7976 | return SIZE_MAX; | |
7977 | ||
7978 | if (check_add_overflow(off, sq_array_size, &off)) | |
7979 | return SIZE_MAX; | |
7980 | ||
75b28aff HV |
7981 | return off; |
7982 | } | |
7983 | ||
0a96bbe4 | 7984 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
7985 | { |
7986 | int i, j; | |
7987 | ||
7988 | if (!ctx->user_bufs) | |
7989 | return -ENXIO; | |
7990 | ||
7991 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
7992 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
7993 | ||
7994 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 7995 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 7996 | |
de293938 | 7997 | if (imu->acct_pages) |
26bfa89e | 7998 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 7999 | kvfree(imu->bvec); |
edafccee JA |
8000 | imu->nr_bvecs = 0; |
8001 | } | |
8002 | ||
8003 | kfree(ctx->user_bufs); | |
8004 | ctx->user_bufs = NULL; | |
8005 | ctx->nr_user_bufs = 0; | |
8006 | return 0; | |
8007 | } | |
8008 | ||
8009 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8010 | void __user *arg, unsigned index) | |
8011 | { | |
8012 | struct iovec __user *src; | |
8013 | ||
8014 | #ifdef CONFIG_COMPAT | |
8015 | if (ctx->compat) { | |
8016 | struct compat_iovec __user *ciovs; | |
8017 | struct compat_iovec ciov; | |
8018 | ||
8019 | ciovs = (struct compat_iovec __user *) arg; | |
8020 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8021 | return -EFAULT; | |
8022 | ||
d55e5f5b | 8023 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8024 | dst->iov_len = ciov.iov_len; |
8025 | return 0; | |
8026 | } | |
8027 | #endif | |
8028 | src = (struct iovec __user *) arg; | |
8029 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8030 | return -EFAULT; | |
8031 | return 0; | |
8032 | } | |
8033 | ||
de293938 JA |
8034 | /* |
8035 | * Not super efficient, but this is just a registration time. And we do cache | |
8036 | * the last compound head, so generally we'll only do a full search if we don't | |
8037 | * match that one. | |
8038 | * | |
8039 | * We check if the given compound head page has already been accounted, to | |
8040 | * avoid double accounting it. This allows us to account the full size of the | |
8041 | * page, not just the constituent pages of a huge page. | |
8042 | */ | |
8043 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8044 | int nr_pages, struct page *hpage) | |
8045 | { | |
8046 | int i, j; | |
8047 | ||
8048 | /* check current page array */ | |
8049 | for (i = 0; i < nr_pages; i++) { | |
8050 | if (!PageCompound(pages[i])) | |
8051 | continue; | |
8052 | if (compound_head(pages[i]) == hpage) | |
8053 | return true; | |
8054 | } | |
8055 | ||
8056 | /* check previously registered pages */ | |
8057 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8058 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8059 | ||
8060 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8061 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8062 | continue; | |
8063 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8064 | return true; | |
8065 | } | |
8066 | } | |
8067 | ||
8068 | return false; | |
8069 | } | |
8070 | ||
8071 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8072 | int nr_pages, struct io_mapped_ubuf *imu, | |
8073 | struct page **last_hpage) | |
8074 | { | |
8075 | int i, ret; | |
8076 | ||
8077 | for (i = 0; i < nr_pages; i++) { | |
8078 | if (!PageCompound(pages[i])) { | |
8079 | imu->acct_pages++; | |
8080 | } else { | |
8081 | struct page *hpage; | |
8082 | ||
8083 | hpage = compound_head(pages[i]); | |
8084 | if (hpage == *last_hpage) | |
8085 | continue; | |
8086 | *last_hpage = hpage; | |
8087 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8088 | continue; | |
8089 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8090 | } | |
8091 | } | |
8092 | ||
8093 | if (!imu->acct_pages) | |
8094 | return 0; | |
8095 | ||
26bfa89e | 8096 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8097 | if (ret) |
8098 | imu->acct_pages = 0; | |
8099 | return ret; | |
8100 | } | |
8101 | ||
0a96bbe4 BM |
8102 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8103 | struct io_mapped_ubuf *imu, | |
8104 | struct page **last_hpage) | |
edafccee JA |
8105 | { |
8106 | struct vm_area_struct **vmas = NULL; | |
8107 | struct page **pages = NULL; | |
0a96bbe4 BM |
8108 | unsigned long off, start, end, ubuf; |
8109 | size_t size; | |
8110 | int ret, pret, nr_pages, i; | |
8111 | ||
8112 | ubuf = (unsigned long) iov->iov_base; | |
8113 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8114 | start = ubuf >> PAGE_SHIFT; | |
8115 | nr_pages = end - start; | |
8116 | ||
8117 | ret = -ENOMEM; | |
8118 | ||
8119 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8120 | if (!pages) | |
8121 | goto done; | |
8122 | ||
8123 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8124 | GFP_KERNEL); | |
8125 | if (!vmas) | |
8126 | goto done; | |
edafccee | 8127 | |
0a96bbe4 BM |
8128 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8129 | GFP_KERNEL); | |
8130 | if (!imu->bvec) | |
8131 | goto done; | |
8132 | ||
8133 | ret = 0; | |
8134 | mmap_read_lock(current->mm); | |
8135 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8136 | pages, vmas); | |
8137 | if (pret == nr_pages) { | |
8138 | /* don't support file backed memory */ | |
8139 | for (i = 0; i < nr_pages; i++) { | |
8140 | struct vm_area_struct *vma = vmas[i]; | |
8141 | ||
8142 | if (vma->vm_file && | |
8143 | !is_file_hugepages(vma->vm_file)) { | |
8144 | ret = -EOPNOTSUPP; | |
8145 | break; | |
8146 | } | |
8147 | } | |
8148 | } else { | |
8149 | ret = pret < 0 ? pret : -EFAULT; | |
8150 | } | |
8151 | mmap_read_unlock(current->mm); | |
8152 | if (ret) { | |
8153 | /* | |
8154 | * if we did partial map, or found file backed vmas, | |
8155 | * release any pages we did get | |
8156 | */ | |
8157 | if (pret > 0) | |
8158 | unpin_user_pages(pages, pret); | |
8159 | kvfree(imu->bvec); | |
8160 | goto done; | |
8161 | } | |
8162 | ||
8163 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8164 | if (ret) { | |
8165 | unpin_user_pages(pages, pret); | |
8166 | kvfree(imu->bvec); | |
8167 | goto done; | |
8168 | } | |
8169 | ||
8170 | off = ubuf & ~PAGE_MASK; | |
8171 | size = iov->iov_len; | |
8172 | for (i = 0; i < nr_pages; i++) { | |
8173 | size_t vec_len; | |
8174 | ||
8175 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8176 | imu->bvec[i].bv_page = pages[i]; | |
8177 | imu->bvec[i].bv_len = vec_len; | |
8178 | imu->bvec[i].bv_offset = off; | |
8179 | off = 0; | |
8180 | size -= vec_len; | |
8181 | } | |
8182 | /* store original address for later verification */ | |
8183 | imu->ubuf = ubuf; | |
8184 | imu->len = iov->iov_len; | |
8185 | imu->nr_bvecs = nr_pages; | |
8186 | ret = 0; | |
8187 | done: | |
8188 | kvfree(pages); | |
8189 | kvfree(vmas); | |
8190 | return ret; | |
8191 | } | |
8192 | ||
2b358604 | 8193 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8194 | { |
edafccee JA |
8195 | if (ctx->user_bufs) |
8196 | return -EBUSY; | |
8197 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8198 | return -EINVAL; | |
8199 | ||
8200 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8201 | GFP_KERNEL); | |
8202 | if (!ctx->user_bufs) | |
8203 | return -ENOMEM; | |
8204 | ||
2b358604 BM |
8205 | return 0; |
8206 | } | |
edafccee | 8207 | |
2b358604 BM |
8208 | static int io_buffer_validate(struct iovec *iov) |
8209 | { | |
8210 | /* | |
8211 | * Don't impose further limits on the size and buffer | |
8212 | * constraints here, we'll -EINVAL later when IO is | |
8213 | * submitted if they are wrong. | |
8214 | */ | |
8215 | if (!iov->iov_base || !iov->iov_len) | |
8216 | return -EFAULT; | |
edafccee | 8217 | |
2b358604 BM |
8218 | /* arbitrary limit, but we need something */ |
8219 | if (iov->iov_len > SZ_1G) | |
8220 | return -EFAULT; | |
edafccee | 8221 | |
2b358604 BM |
8222 | return 0; |
8223 | } | |
edafccee | 8224 | |
2b358604 BM |
8225 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8226 | unsigned int nr_args) | |
8227 | { | |
8228 | int i, ret; | |
8229 | struct iovec iov; | |
8230 | struct page *last_hpage = NULL; | |
edafccee | 8231 | |
2b358604 BM |
8232 | ret = io_buffers_map_alloc(ctx, nr_args); |
8233 | if (ret) | |
8234 | return ret; | |
edafccee | 8235 | |
edafccee JA |
8236 | for (i = 0; i < nr_args; i++) { |
8237 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8238 | |
edafccee JA |
8239 | ret = io_copy_iov(ctx, &iov, arg, i); |
8240 | if (ret) | |
0a96bbe4 | 8241 | break; |
de293938 | 8242 | |
2b358604 BM |
8243 | ret = io_buffer_validate(&iov); |
8244 | if (ret) | |
0a96bbe4 | 8245 | break; |
edafccee | 8246 | |
0a96bbe4 BM |
8247 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8248 | if (ret) | |
8249 | break; | |
edafccee JA |
8250 | |
8251 | ctx->nr_user_bufs++; | |
8252 | } | |
0a96bbe4 BM |
8253 | |
8254 | if (ret) | |
8255 | io_sqe_buffers_unregister(ctx); | |
8256 | ||
edafccee JA |
8257 | return ret; |
8258 | } | |
8259 | ||
9b402849 JA |
8260 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8261 | { | |
8262 | __s32 __user *fds = arg; | |
8263 | int fd; | |
8264 | ||
8265 | if (ctx->cq_ev_fd) | |
8266 | return -EBUSY; | |
8267 | ||
8268 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8269 | return -EFAULT; | |
8270 | ||
8271 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8272 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8273 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8274 | ctx->cq_ev_fd = NULL; | |
8275 | return ret; | |
8276 | } | |
8277 | ||
8278 | return 0; | |
8279 | } | |
8280 | ||
8281 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8282 | { | |
8283 | if (ctx->cq_ev_fd) { | |
8284 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8285 | ctx->cq_ev_fd = NULL; | |
8286 | return 0; | |
8287 | } | |
8288 | ||
8289 | return -ENXIO; | |
8290 | } | |
8291 | ||
5a2e745d JA |
8292 | static int __io_destroy_buffers(int id, void *p, void *data) |
8293 | { | |
8294 | struct io_ring_ctx *ctx = data; | |
8295 | struct io_buffer *buf = p; | |
8296 | ||
067524e9 | 8297 | __io_remove_buffers(ctx, buf, id, -1U); |
5a2e745d JA |
8298 | return 0; |
8299 | } | |
8300 | ||
8301 | static void io_destroy_buffers(struct io_ring_ctx *ctx) | |
8302 | { | |
8303 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); | |
8304 | idr_destroy(&ctx->io_buffer_idr); | |
8305 | } | |
8306 | ||
68e68ee6 | 8307 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8308 | { |
68e68ee6 | 8309 | struct io_kiocb *req, *nxt; |
1b4c351f | 8310 | |
68e68ee6 JA |
8311 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8312 | if (tsk && req->task != tsk) | |
8313 | continue; | |
1b4c351f JA |
8314 | list_del(&req->compl.list); |
8315 | kmem_cache_free(req_cachep, req); | |
8316 | } | |
8317 | } | |
8318 | ||
4010fec4 | 8319 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8320 | { |
bf019da7 | 8321 | struct io_submit_state *submit_state = &ctx->submit_state; |
e5547d2c | 8322 | struct io_comp_state *cs = &ctx->submit_state.comp; |
bf019da7 | 8323 | |
9a4fdbd8 JA |
8324 | mutex_lock(&ctx->uring_lock); |
8325 | ||
8e5c66c4 | 8326 | if (submit_state->free_reqs) { |
9a4fdbd8 JA |
8327 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
8328 | submit_state->reqs); | |
8e5c66c4 PB |
8329 | submit_state->free_reqs = 0; |
8330 | } | |
9a4fdbd8 JA |
8331 | |
8332 | spin_lock_irq(&ctx->completion_lock); | |
e5547d2c PB |
8333 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
8334 | cs->locked_free_nr = 0; | |
9a4fdbd8 JA |
8335 | spin_unlock_irq(&ctx->completion_lock); |
8336 | ||
e5547d2c PB |
8337 | io_req_cache_free(&cs->free_list, NULL); |
8338 | ||
9a4fdbd8 JA |
8339 | mutex_unlock(&ctx->uring_lock); |
8340 | } | |
8341 | ||
2b188cc1 JA |
8342 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8343 | { | |
04fc6c80 PB |
8344 | /* |
8345 | * Some may use context even when all refs and requests have been put, | |
8346 | * and they are free to do so while still holding uring_lock, see | |
8347 | * __io_req_task_submit(). Wait for them to finish. | |
8348 | */ | |
8349 | mutex_lock(&ctx->uring_lock); | |
8350 | mutex_unlock(&ctx->uring_lock); | |
8351 | ||
37d1e2e3 | 8352 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8353 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8354 | |
37d1e2e3 | 8355 | if (ctx->mm_account) { |
2aede0e4 JA |
8356 | mmdrop(ctx->mm_account); |
8357 | ctx->mm_account = NULL; | |
30975825 | 8358 | } |
def596e9 | 8359 | |
8bad28d8 | 8360 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8361 | io_sqe_files_unregister(ctx); |
8bad28d8 | 8362 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8363 | io_eventfd_unregister(ctx); |
5a2e745d | 8364 | io_destroy_buffers(ctx); |
41726c9a | 8365 | idr_destroy(&ctx->personality_idr); |
def596e9 | 8366 | |
2b188cc1 | 8367 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8368 | if (ctx->ring_sock) { |
8369 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8370 | sock_release(ctx->ring_sock); |
355e8d26 | 8371 | } |
2b188cc1 JA |
8372 | #endif |
8373 | ||
75b28aff | 8374 | io_mem_free(ctx->rings); |
2b188cc1 | 8375 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8376 | |
8377 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8378 | free_uid(ctx->user); |
4010fec4 | 8379 | io_req_caches_free(ctx); |
e941894e JA |
8380 | if (ctx->hash_map) |
8381 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8382 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8383 | kfree(ctx); |
8384 | } | |
8385 | ||
8386 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8387 | { | |
8388 | struct io_ring_ctx *ctx = file->private_data; | |
8389 | __poll_t mask = 0; | |
8390 | ||
8391 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8392 | /* |
8393 | * synchronizes with barrier from wq_has_sleeper call in | |
8394 | * io_commit_cqring | |
8395 | */ | |
2b188cc1 | 8396 | smp_rmb(); |
90554200 | 8397 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8398 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8399 | |
8400 | /* | |
8401 | * Don't flush cqring overflow list here, just do a simple check. | |
8402 | * Otherwise there could possible be ABBA deadlock: | |
8403 | * CPU0 CPU1 | |
8404 | * ---- ---- | |
8405 | * lock(&ctx->uring_lock); | |
8406 | * lock(&ep->mtx); | |
8407 | * lock(&ctx->uring_lock); | |
8408 | * lock(&ep->mtx); | |
8409 | * | |
8410 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8411 | * pushs them to do the flush. | |
8412 | */ | |
8413 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8414 | mask |= EPOLLIN | EPOLLRDNORM; |
8415 | ||
8416 | return mask; | |
8417 | } | |
8418 | ||
8419 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8420 | { | |
8421 | struct io_ring_ctx *ctx = file->private_data; | |
8422 | ||
8423 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8424 | } | |
8425 | ||
0bead8cd | 8426 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8427 | { |
4379bf8b | 8428 | const struct cred *creds; |
071698e1 | 8429 | |
4379bf8b JA |
8430 | creds = idr_remove(&ctx->personality_idr, id); |
8431 | if (creds) { | |
8432 | put_cred(creds); | |
0bead8cd | 8433 | return 0; |
1e6fa521 | 8434 | } |
0bead8cd YD |
8435 | |
8436 | return -EINVAL; | |
8437 | } | |
8438 | ||
8439 | static int io_remove_personalities(int id, void *p, void *data) | |
8440 | { | |
8441 | struct io_ring_ctx *ctx = data; | |
8442 | ||
8443 | io_unregister_personality(ctx, id); | |
071698e1 JA |
8444 | return 0; |
8445 | } | |
8446 | ||
ba50a036 | 8447 | static bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
7c25c0d1 | 8448 | { |
28c4721b | 8449 | struct callback_head *work, *next; |
ba50a036 | 8450 | bool executed = false; |
7c25c0d1 JA |
8451 | |
8452 | do { | |
28c4721b | 8453 | work = xchg(&ctx->exit_task_work, NULL); |
7c25c0d1 JA |
8454 | if (!work) |
8455 | break; | |
8456 | ||
8457 | do { | |
8458 | next = work->next; | |
8459 | work->func(work); | |
8460 | work = next; | |
8461 | cond_resched(); | |
8462 | } while (work); | |
ba50a036 | 8463 | executed = true; |
7c25c0d1 | 8464 | } while (1); |
ba50a036 PB |
8465 | |
8466 | return executed; | |
7c25c0d1 JA |
8467 | } |
8468 | ||
d56d938b PB |
8469 | struct io_tctx_exit { |
8470 | struct callback_head task_work; | |
8471 | struct completion completion; | |
baf186c4 | 8472 | struct io_ring_ctx *ctx; |
d56d938b PB |
8473 | }; |
8474 | ||
8475 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8476 | { | |
8477 | struct io_uring_task *tctx = current->io_uring; | |
8478 | struct io_tctx_exit *work; | |
8479 | ||
8480 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8481 | /* | |
8482 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8483 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8484 | */ | |
8485 | if (!atomic_read(&tctx->in_idle)) | |
baf186c4 | 8486 | io_uring_del_task_file((unsigned long)work->ctx); |
d56d938b PB |
8487 | complete(&work->completion); |
8488 | } | |
8489 | ||
85faa7b8 JA |
8490 | static void io_ring_exit_work(struct work_struct *work) |
8491 | { | |
d56d938b | 8492 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8493 | unsigned long timeout = jiffies + HZ * 60 * 5; |
d56d938b PB |
8494 | struct io_tctx_exit exit; |
8495 | struct io_tctx_node *node; | |
8496 | int ret; | |
85faa7b8 | 8497 | |
56952e91 JA |
8498 | /* |
8499 | * If we're doing polled IO and end up having requests being | |
8500 | * submitted async (out-of-line), then completions can come in while | |
8501 | * we're waiting for refs to drop. We need to reap these manually, | |
8502 | * as nobody else will be looking for them. | |
8503 | */ | |
b2edc0a7 | 8504 | do { |
9936c7c2 | 8505 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b5bb3a24 PB |
8506 | |
8507 | WARN_ON_ONCE(time_after(jiffies, timeout)); | |
b2edc0a7 | 8508 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
d56d938b PB |
8509 | |
8510 | mutex_lock(&ctx->uring_lock); | |
8511 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
8512 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
8513 | ||
d56d938b PB |
8514 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
8515 | ctx_node); | |
baf186c4 | 8516 | exit.ctx = ctx; |
d56d938b PB |
8517 | init_completion(&exit.completion); |
8518 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
8519 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); | |
8520 | if (WARN_ON_ONCE(ret)) | |
8521 | continue; | |
8522 | wake_up_process(node->task); | |
8523 | ||
8524 | mutex_unlock(&ctx->uring_lock); | |
8525 | wait_for_completion(&exit.completion); | |
8526 | cond_resched(); | |
8527 | mutex_lock(&ctx->uring_lock); | |
8528 | } | |
8529 | mutex_unlock(&ctx->uring_lock); | |
8530 | ||
85faa7b8 JA |
8531 | io_ring_ctx_free(ctx); |
8532 | } | |
8533 | ||
2b188cc1 JA |
8534 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8535 | { | |
8536 | mutex_lock(&ctx->uring_lock); | |
8537 | percpu_ref_kill(&ctx->refs); | |
cda286f0 PB |
8538 | /* if force is set, the ring is going away. always drop after that */ |
8539 | ctx->cq_overflow_flushed = 1; | |
634578f8 | 8540 | if (ctx->rings) |
6c503150 | 8541 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
5c766a90 | 8542 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
2b188cc1 JA |
8543 | mutex_unlock(&ctx->uring_lock); |
8544 | ||
6b81928d PB |
8545 | io_kill_timeouts(ctx, NULL, NULL); |
8546 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8547 | |
15dff286 | 8548 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8549 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8550 | |
85faa7b8 | 8551 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8552 | /* |
8553 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8554 | * if we're exiting a ton of rings at the same time. It just adds | |
8555 | * noise and overhead, there's no discernable change in runtime | |
8556 | * over using system_wq. | |
8557 | */ | |
8558 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8559 | } |
8560 | ||
8561 | static int io_uring_release(struct inode *inode, struct file *file) | |
8562 | { | |
8563 | struct io_ring_ctx *ctx = file->private_data; | |
8564 | ||
8565 | file->private_data = NULL; | |
8566 | io_ring_ctx_wait_and_kill(ctx); | |
8567 | return 0; | |
8568 | } | |
8569 | ||
f6edbabb PB |
8570 | struct io_task_cancel { |
8571 | struct task_struct *task; | |
8572 | struct files_struct *files; | |
8573 | }; | |
f254ac04 | 8574 | |
f6edbabb | 8575 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8576 | { |
9a472ef7 | 8577 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8578 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8579 | bool ret; |
8580 | ||
f6edbabb | 8581 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8582 | unsigned long flags; |
8583 | struct io_ring_ctx *ctx = req->ctx; | |
8584 | ||
8585 | /* protect against races with linked timeouts */ | |
8586 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8587 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8588 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8589 | } else { | |
f6edbabb | 8590 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8591 | } |
8592 | return ret; | |
b711d4ea JA |
8593 | } |
8594 | ||
b7ddce3c | 8595 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8596 | struct task_struct *task, |
b7ddce3c PB |
8597 | struct files_struct *files) |
8598 | { | |
8599 | struct io_defer_entry *de = NULL; | |
8600 | LIST_HEAD(list); | |
8601 | ||
8602 | spin_lock_irq(&ctx->completion_lock); | |
8603 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8604 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8605 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8606 | break; | |
8607 | } | |
8608 | } | |
8609 | spin_unlock_irq(&ctx->completion_lock); | |
8610 | ||
8611 | while (!list_empty(&list)) { | |
8612 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8613 | list_del_init(&de->list); | |
8614 | req_set_fail_links(de->req); | |
8615 | io_put_req(de->req); | |
8616 | io_req_complete(de->req, -ECANCELED); | |
8617 | kfree(de); | |
8618 | } | |
8619 | } | |
8620 | ||
1b00764f PB |
8621 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8622 | { | |
8623 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8624 | ||
8625 | return req->ctx == data; | |
8626 | } | |
8627 | ||
8628 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) | |
8629 | { | |
8630 | struct io_tctx_node *node; | |
8631 | enum io_wq_cancel cret; | |
8632 | bool ret = false; | |
8633 | ||
8634 | mutex_lock(&ctx->uring_lock); | |
8635 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
8636 | struct io_uring_task *tctx = node->task->io_uring; | |
8637 | ||
8638 | /* | |
8639 | * io_wq will stay alive while we hold uring_lock, because it's | |
8640 | * killed after ctx nodes, which requires to take the lock. | |
8641 | */ | |
8642 | if (!tctx || !tctx->io_wq) | |
8643 | continue; | |
8644 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
8645 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8646 | } | |
8647 | mutex_unlock(&ctx->uring_lock); | |
8648 | ||
8649 | return ret; | |
8650 | } | |
8651 | ||
9936c7c2 PB |
8652 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8653 | struct task_struct *task, | |
8654 | struct files_struct *files) | |
8655 | { | |
8656 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
1b00764f | 8657 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
8658 | |
8659 | while (1) { | |
8660 | enum io_wq_cancel cret; | |
8661 | bool ret = false; | |
8662 | ||
1b00764f PB |
8663 | if (!task) { |
8664 | ret |= io_uring_try_cancel_iowq(ctx); | |
8665 | } else if (tctx && tctx->io_wq) { | |
8666 | /* | |
8667 | * Cancels requests of all rings, not only @ctx, but | |
8668 | * it's fine as the task is in exit/exec. | |
8669 | */ | |
5aa75ed5 | 8670 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
8671 | &cancel, true); |
8672 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8673 | } | |
8674 | ||
8675 | /* SQPOLL thread does its own polling */ | |
8676 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { | |
8677 | while (!list_empty_careful(&ctx->iopoll_list)) { | |
8678 | io_iopoll_try_reap_events(ctx); | |
8679 | ret = true; | |
8680 | } | |
8681 | } | |
8682 | ||
8683 | ret |= io_poll_remove_all(ctx, task, files); | |
8684 | ret |= io_kill_timeouts(ctx, task, files); | |
8685 | ret |= io_run_task_work(); | |
ba50a036 | 8686 | ret |= io_run_ctx_fallback(ctx); |
9936c7c2 PB |
8687 | io_cqring_overflow_flush(ctx, true, task, files); |
8688 | if (!ret) | |
8689 | break; | |
8690 | cond_resched(); | |
8691 | } | |
8692 | } | |
8693 | ||
ca70f00b PB |
8694 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
8695 | struct task_struct *task, | |
8696 | struct files_struct *files) | |
8697 | { | |
8698 | struct io_kiocb *req; | |
8699 | int cnt = 0; | |
8700 | ||
8701 | spin_lock_irq(&ctx->inflight_lock); | |
8702 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) | |
8703 | cnt += io_match_task(req, task, files); | |
8704 | spin_unlock_irq(&ctx->inflight_lock); | |
8705 | return cnt; | |
8706 | } | |
8707 | ||
b52fda00 | 8708 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
df9923f9 | 8709 | struct task_struct *task, |
fcb323cc JA |
8710 | struct files_struct *files) |
8711 | { | |
fcb323cc | 8712 | while (!list_empty_careful(&ctx->inflight_list)) { |
d8f1b971 | 8713 | DEFINE_WAIT(wait); |
ca70f00b | 8714 | int inflight; |
fcb323cc | 8715 | |
ca70f00b PB |
8716 | inflight = io_uring_count_inflight(ctx, task, files); |
8717 | if (!inflight) | |
fcb323cc | 8718 | break; |
f6edbabb | 8719 | |
9936c7c2 | 8720 | io_uring_try_cancel_requests(ctx, task, files); |
ca70f00b | 8721 | |
34343786 PB |
8722 | if (ctx->sq_data) |
8723 | io_sq_thread_unpark(ctx->sq_data); | |
ca70f00b PB |
8724 | prepare_to_wait(&task->io_uring->wait, &wait, |
8725 | TASK_UNINTERRUPTIBLE); | |
8726 | if (inflight == io_uring_count_inflight(ctx, task, files)) | |
8727 | schedule(); | |
c98de08c | 8728 | finish_wait(&task->io_uring->wait, &wait); |
34343786 PB |
8729 | if (ctx->sq_data) |
8730 | io_sq_thread_park(ctx->sq_data); | |
0f212204 | 8731 | } |
0f212204 JA |
8732 | } |
8733 | ||
8734 | /* | |
8735 | * We need to iteratively cancel requests, in case a request has dependent | |
8736 | * hard links. These persist even for failure of cancelations, hence keep | |
8737 | * looping until none are found. | |
8738 | */ | |
8739 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, | |
8740 | struct files_struct *files) | |
8741 | { | |
8742 | struct task_struct *task = current; | |
8743 | ||
fdaf083c | 8744 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
70aacfe6 PB |
8745 | /* never started, nothing to cancel */ |
8746 | if (ctx->flags & IORING_SETUP_R_DISABLED) { | |
8747 | io_sq_offload_start(ctx); | |
8748 | return; | |
8749 | } | |
86e0d676 JA |
8750 | io_sq_thread_park(ctx->sq_data); |
8751 | task = ctx->sq_data->thread; | |
8752 | if (task) | |
37d1e2e3 | 8753 | atomic_inc(&task->io_uring->in_idle); |
fdaf083c | 8754 | } |
0f212204 | 8755 | |
df9923f9 | 8756 | io_cancel_defer_files(ctx, task, files); |
0f212204 | 8757 | |
3a7efd1a | 8758 | io_uring_cancel_files(ctx, task, files); |
b52fda00 | 8759 | if (!files) |
9936c7c2 | 8760 | io_uring_try_cancel_requests(ctx, task, NULL); |
fdaf083c | 8761 | |
86e0d676 | 8762 | if (task) |
fdaf083c | 8763 | atomic_dec(&task->io_uring->in_idle); |
86e0d676 | 8764 | if (ctx->sq_data) |
fdaf083c | 8765 | io_sq_thread_unpark(ctx->sq_data); |
0f212204 JA |
8766 | } |
8767 | ||
8768 | /* | |
8769 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8770 | */ | |
baf186c4 | 8771 | static int io_uring_add_task_file(struct io_ring_ctx *ctx) |
0f212204 | 8772 | { |
236434c3 | 8773 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 8774 | struct io_tctx_node *node; |
a528b04e | 8775 | int ret; |
236434c3 MWO |
8776 | |
8777 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8778 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8779 | if (unlikely(ret)) |
8780 | return ret; | |
236434c3 | 8781 | tctx = current->io_uring; |
0f212204 | 8782 | } |
baf186c4 PB |
8783 | if (tctx->last != ctx) { |
8784 | void *old = xa_load(&tctx->xa, (unsigned long)ctx); | |
0f212204 | 8785 | |
236434c3 | 8786 | if (!old) { |
13bf43f5 PB |
8787 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
8788 | if (!node) | |
8789 | return -ENOMEM; | |
8790 | node->ctx = ctx; | |
13bf43f5 PB |
8791 | node->task = current; |
8792 | ||
baf186c4 | 8793 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
13bf43f5 | 8794 | node, GFP_KERNEL)); |
a528b04e | 8795 | if (ret) { |
13bf43f5 | 8796 | kfree(node); |
a528b04e PB |
8797 | return ret; |
8798 | } | |
13bf43f5 PB |
8799 | |
8800 | mutex_lock(&ctx->uring_lock); | |
8801 | list_add(&node->ctx_node, &ctx->tctx_list); | |
8802 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 8803 | } |
baf186c4 | 8804 | tctx->last = ctx; |
0f212204 JA |
8805 | } |
8806 | ||
fdaf083c JA |
8807 | /* |
8808 | * This is race safe in that the task itself is doing this, hence it | |
8809 | * cannot be going through the exit/cancel paths at the same time. | |
8810 | * This cannot be modified while exit/cancel is running. | |
8811 | */ | |
8812 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) | |
8813 | tctx->sqpoll = true; | |
8814 | ||
0f212204 JA |
8815 | return 0; |
8816 | } | |
8817 | ||
8818 | /* | |
8819 | * Remove this io_uring_file -> task mapping. | |
8820 | */ | |
2941267b | 8821 | static void io_uring_del_task_file(unsigned long index) |
0f212204 JA |
8822 | { |
8823 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 8824 | struct io_tctx_node *node; |
2941267b | 8825 | |
eebd2e37 PB |
8826 | if (!tctx) |
8827 | return; | |
13bf43f5 PB |
8828 | node = xa_erase(&tctx->xa, index); |
8829 | if (!node) | |
2941267b | 8830 | return; |
0f212204 | 8831 | |
13bf43f5 PB |
8832 | WARN_ON_ONCE(current != node->task); |
8833 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
8834 | ||
8835 | mutex_lock(&node->ctx->uring_lock); | |
8836 | list_del(&node->ctx_node); | |
8837 | mutex_unlock(&node->ctx->uring_lock); | |
8838 | ||
baf186c4 | 8839 | if (tctx->last == node->ctx) |
0f212204 | 8840 | tctx->last = NULL; |
13bf43f5 | 8841 | kfree(node); |
0f212204 JA |
8842 | } |
8843 | ||
8452d4a6 | 8844 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 8845 | { |
13bf43f5 | 8846 | struct io_tctx_node *node; |
de7f1d9e PB |
8847 | unsigned long index; |
8848 | ||
13bf43f5 | 8849 | xa_for_each(&tctx->xa, index, node) |
2941267b | 8850 | io_uring_del_task_file(index); |
8452d4a6 PB |
8851 | if (tctx->io_wq) { |
8852 | io_wq_put_and_exit(tctx->io_wq); | |
8853 | tctx->io_wq = NULL; | |
8854 | } | |
de7f1d9e PB |
8855 | } |
8856 | ||
0f212204 JA |
8857 | void __io_uring_files_cancel(struct files_struct *files) |
8858 | { | |
8859 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 8860 | struct io_tctx_node *node; |
ce765372 | 8861 | unsigned long index; |
0f212204 JA |
8862 | |
8863 | /* make sure overflow events are dropped */ | |
fdaf083c | 8864 | atomic_inc(&tctx->in_idle); |
13bf43f5 PB |
8865 | xa_for_each(&tctx->xa, index, node) |
8866 | io_uring_cancel_task_requests(node->ctx, files); | |
fdaf083c | 8867 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8868 | |
8452d4a6 PB |
8869 | if (files) |
8870 | io_uring_clean_tctx(tctx); | |
fdaf083c JA |
8871 | } |
8872 | ||
8873 | static s64 tctx_inflight(struct io_uring_task *tctx) | |
8874 | { | |
0e9ddb39 PB |
8875 | return percpu_counter_sum(&tctx->inflight); |
8876 | } | |
fdaf083c | 8877 | |
0e9ddb39 PB |
8878 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8879 | { | |
37d1e2e3 | 8880 | struct io_sq_data *sqd = ctx->sq_data; |
0e9ddb39 PB |
8881 | struct io_uring_task *tctx; |
8882 | s64 inflight; | |
8883 | DEFINE_WAIT(wait); | |
fdaf083c | 8884 | |
37d1e2e3 | 8885 | if (!sqd) |
0e9ddb39 | 8886 | return; |
86e0d676 JA |
8887 | io_sq_thread_park(sqd); |
8888 | if (!sqd->thread || !sqd->thread->io_uring) { | |
e54945ae JA |
8889 | io_sq_thread_unpark(sqd); |
8890 | return; | |
8891 | } | |
86e0d676 | 8892 | tctx = ctx->sq_data->thread->io_uring; |
0e9ddb39 PB |
8893 | atomic_inc(&tctx->in_idle); |
8894 | do { | |
8895 | /* read completions before cancelations */ | |
8896 | inflight = tctx_inflight(tctx); | |
8897 | if (!inflight) | |
8898 | break; | |
8899 | io_uring_cancel_task_requests(ctx, NULL); | |
fdaf083c | 8900 | |
0e9ddb39 PB |
8901 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
8902 | /* | |
8903 | * If we've seen completions, retry without waiting. This | |
8904 | * avoids a race where a completion comes in before we did | |
8905 | * prepare_to_wait(). | |
8906 | */ | |
8907 | if (inflight == tctx_inflight(tctx)) | |
8908 | schedule(); | |
8909 | finish_wait(&tctx->wait, &wait); | |
8910 | } while (1); | |
8911 | atomic_dec(&tctx->in_idle); | |
37d1e2e3 | 8912 | io_sq_thread_unpark(sqd); |
0f212204 JA |
8913 | } |
8914 | ||
0f212204 JA |
8915 | /* |
8916 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
8917 | * requests. | |
8918 | */ | |
8919 | void __io_uring_task_cancel(void) | |
8920 | { | |
8921 | struct io_uring_task *tctx = current->io_uring; | |
8922 | DEFINE_WAIT(wait); | |
d8a6df10 | 8923 | s64 inflight; |
0f212204 JA |
8924 | |
8925 | /* make sure overflow events are dropped */ | |
fdaf083c | 8926 | atomic_inc(&tctx->in_idle); |
0f212204 | 8927 | |
0e9ddb39 | 8928 | if (tctx->sqpoll) { |
13bf43f5 | 8929 | struct io_tctx_node *node; |
0e9ddb39 PB |
8930 | unsigned long index; |
8931 | ||
13bf43f5 PB |
8932 | xa_for_each(&tctx->xa, index, node) |
8933 | io_uring_cancel_sqpoll(node->ctx); | |
0e9ddb39 | 8934 | } |
0b5cd6c3 | 8935 | |
d8a6df10 | 8936 | do { |
0f212204 | 8937 | /* read completions before cancelations */ |
fdaf083c | 8938 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
8939 | if (!inflight) |
8940 | break; | |
0f212204 JA |
8941 | __io_uring_files_cancel(NULL); |
8942 | ||
8943 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
8944 | ||
8945 | /* | |
a1bb3cd5 PB |
8946 | * If we've seen completions, retry without waiting. This |
8947 | * avoids a race where a completion comes in before we did | |
8948 | * prepare_to_wait(). | |
0f212204 | 8949 | */ |
a1bb3cd5 PB |
8950 | if (inflight == tctx_inflight(tctx)) |
8951 | schedule(); | |
f57555ed | 8952 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 8953 | } while (1); |
0f212204 | 8954 | |
fdaf083c | 8955 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 8956 | |
8452d4a6 PB |
8957 | io_uring_clean_tctx(tctx); |
8958 | /* all current's requests should be gone, we can kill tctx */ | |
8959 | __io_uring_free(current); | |
44e728b8 PB |
8960 | } |
8961 | ||
6c5c240e RP |
8962 | static void *io_uring_validate_mmap_request(struct file *file, |
8963 | loff_t pgoff, size_t sz) | |
2b188cc1 | 8964 | { |
2b188cc1 | 8965 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 8966 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
8967 | struct page *page; |
8968 | void *ptr; | |
8969 | ||
8970 | switch (offset) { | |
8971 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
8972 | case IORING_OFF_CQ_RING: |
8973 | ptr = ctx->rings; | |
2b188cc1 JA |
8974 | break; |
8975 | case IORING_OFF_SQES: | |
8976 | ptr = ctx->sq_sqes; | |
8977 | break; | |
2b188cc1 | 8978 | default: |
6c5c240e | 8979 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
8980 | } |
8981 | ||
8982 | page = virt_to_head_page(ptr); | |
a50b854e | 8983 | if (sz > page_size(page)) |
6c5c240e RP |
8984 | return ERR_PTR(-EINVAL); |
8985 | ||
8986 | return ptr; | |
8987 | } | |
8988 | ||
8989 | #ifdef CONFIG_MMU | |
8990 | ||
8991 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
8992 | { | |
8993 | size_t sz = vma->vm_end - vma->vm_start; | |
8994 | unsigned long pfn; | |
8995 | void *ptr; | |
8996 | ||
8997 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
8998 | if (IS_ERR(ptr)) | |
8999 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9000 | |
9001 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9002 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9003 | } | |
9004 | ||
6c5c240e RP |
9005 | #else /* !CONFIG_MMU */ |
9006 | ||
9007 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9008 | { | |
9009 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9010 | } | |
9011 | ||
9012 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9013 | { | |
9014 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9015 | } | |
9016 | ||
9017 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9018 | unsigned long addr, unsigned long len, | |
9019 | unsigned long pgoff, unsigned long flags) | |
9020 | { | |
9021 | void *ptr; | |
9022 | ||
9023 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9024 | if (IS_ERR(ptr)) | |
9025 | return PTR_ERR(ptr); | |
9026 | ||
9027 | return (unsigned long) ptr; | |
9028 | } | |
9029 | ||
9030 | #endif /* !CONFIG_MMU */ | |
9031 | ||
d9d05217 | 9032 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 | 9033 | { |
d9d05217 | 9034 | int ret = 0; |
90554200 JA |
9035 | DEFINE_WAIT(wait); |
9036 | ||
9037 | do { | |
9038 | if (!io_sqring_full(ctx)) | |
9039 | break; | |
90554200 JA |
9040 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9041 | ||
9042 | if (!io_sqring_full(ctx)) | |
9043 | break; | |
90554200 JA |
9044 | schedule(); |
9045 | } while (!signal_pending(current)); | |
9046 | ||
9047 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
d9d05217 | 9048 | return ret; |
90554200 JA |
9049 | } |
9050 | ||
c73ebb68 HX |
9051 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9052 | struct __kernel_timespec __user **ts, | |
9053 | const sigset_t __user **sig) | |
9054 | { | |
9055 | struct io_uring_getevents_arg arg; | |
9056 | ||
9057 | /* | |
9058 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9059 | * is just a pointer to the sigset_t. | |
9060 | */ | |
9061 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9062 | *sig = (const sigset_t __user *) argp; | |
9063 | *ts = NULL; | |
9064 | return 0; | |
9065 | } | |
9066 | ||
9067 | /* | |
9068 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9069 | * timespec and sigset_t pointers if good. | |
9070 | */ | |
9071 | if (*argsz != sizeof(arg)) | |
9072 | return -EINVAL; | |
9073 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9074 | return -EFAULT; | |
9075 | *sig = u64_to_user_ptr(arg.sigmask); | |
9076 | *argsz = arg.sigmask_sz; | |
9077 | *ts = u64_to_user_ptr(arg.ts); | |
9078 | return 0; | |
9079 | } | |
9080 | ||
2b188cc1 | 9081 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9082 | u32, min_complete, u32, flags, const void __user *, argp, |
9083 | size_t, argsz) | |
2b188cc1 JA |
9084 | { |
9085 | struct io_ring_ctx *ctx; | |
9086 | long ret = -EBADF; | |
9087 | int submitted = 0; | |
9088 | struct fd f; | |
9089 | ||
4c6e277c | 9090 | io_run_task_work(); |
b41e9852 | 9091 | |
90554200 | 9092 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
c73ebb68 | 9093 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
2b188cc1 JA |
9094 | return -EINVAL; |
9095 | ||
9096 | f = fdget(fd); | |
9097 | if (!f.file) | |
9098 | return -EBADF; | |
9099 | ||
9100 | ret = -EOPNOTSUPP; | |
9101 | if (f.file->f_op != &io_uring_fops) | |
9102 | goto out_fput; | |
9103 | ||
9104 | ret = -ENXIO; | |
9105 | ctx = f.file->private_data; | |
9106 | if (!percpu_ref_tryget(&ctx->refs)) | |
9107 | goto out_fput; | |
9108 | ||
7e84e1c7 SG |
9109 | ret = -EBADFD; |
9110 | if (ctx->flags & IORING_SETUP_R_DISABLED) | |
9111 | goto out; | |
9112 | ||
6c271ce2 JA |
9113 | /* |
9114 | * For SQ polling, the thread will do all submissions and completions. | |
9115 | * Just return the requested submit count, and wake the thread if | |
9116 | * we were asked to. | |
9117 | */ | |
b2a9eada | 9118 | ret = 0; |
6c271ce2 | 9119 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c503150 | 9120 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
89448c47 | 9121 | |
d9d05217 | 9122 | ret = -EOWNERDEAD; |
04147488 SM |
9123 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9124 | goto out; | |
9125 | } | |
6c271ce2 | 9126 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9127 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9128 | if (flags & IORING_ENTER_SQ_WAIT) { |
9129 | ret = io_sqpoll_wait_sq(ctx); | |
9130 | if (ret) | |
9131 | goto out; | |
9132 | } | |
6c271ce2 | 9133 | submitted = to_submit; |
b2a9eada | 9134 | } else if (to_submit) { |
baf186c4 | 9135 | ret = io_uring_add_task_file(ctx); |
0f212204 JA |
9136 | if (unlikely(ret)) |
9137 | goto out; | |
2b188cc1 | 9138 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9139 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9140 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9141 | |
9142 | if (submitted != to_submit) | |
9143 | goto out; | |
2b188cc1 JA |
9144 | } |
9145 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9146 | const sigset_t __user *sig; |
9147 | struct __kernel_timespec __user *ts; | |
9148 | ||
9149 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9150 | if (unlikely(ret)) | |
9151 | goto out; | |
9152 | ||
2b188cc1 JA |
9153 | min_complete = min(min_complete, ctx->cq_entries); |
9154 | ||
32b2244a XW |
9155 | /* |
9156 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9157 | * space applications don't need to do io completion events | |
9158 | * polling again, they can rely on io_sq_thread to do polling | |
9159 | * work, which can reduce cpu usage and uring_lock contention. | |
9160 | */ | |
9161 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9162 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9163 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9164 | } else { |
c73ebb68 | 9165 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9166 | } |
2b188cc1 JA |
9167 | } |
9168 | ||
7c504e65 | 9169 | out: |
6805b32e | 9170 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9171 | out_fput: |
9172 | fdput(f); | |
9173 | return submitted ? submitted : ret; | |
9174 | } | |
9175 | ||
bebdb65e | 9176 | #ifdef CONFIG_PROC_FS |
87ce955b JA |
9177 | static int io_uring_show_cred(int id, void *p, void *data) |
9178 | { | |
4379bf8b | 9179 | const struct cred *cred = p; |
87ce955b JA |
9180 | struct seq_file *m = data; |
9181 | struct user_namespace *uns = seq_user_ns(m); | |
9182 | struct group_info *gi; | |
9183 | kernel_cap_t cap; | |
9184 | unsigned __capi; | |
9185 | int g; | |
9186 | ||
9187 | seq_printf(m, "%5d\n", id); | |
9188 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9189 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9190 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9191 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9192 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9193 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9194 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9195 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9196 | seq_puts(m, "\n\tGroups:\t"); | |
9197 | gi = cred->group_info; | |
9198 | for (g = 0; g < gi->ngroups; g++) { | |
9199 | seq_put_decimal_ull(m, g ? " " : "", | |
9200 | from_kgid_munged(uns, gi->gid[g])); | |
9201 | } | |
9202 | seq_puts(m, "\n\tCapEff:\t"); | |
9203 | cap = cred->cap_effective; | |
9204 | CAP_FOR_EACH_U32(__capi) | |
9205 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9206 | seq_putc(m, '\n'); | |
9207 | return 0; | |
9208 | } | |
9209 | ||
9210 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9211 | { | |
dbbe9c64 | 9212 | struct io_sq_data *sq = NULL; |
fad8e0de | 9213 | bool has_lock; |
87ce955b JA |
9214 | int i; |
9215 | ||
fad8e0de JA |
9216 | /* |
9217 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9218 | * since fdinfo case grabs it in the opposite direction of normal use | |
9219 | * cases. If we fail to get the lock, we just don't iterate any | |
9220 | * structures that could be going away outside the io_uring mutex. | |
9221 | */ | |
9222 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9223 | ||
5f3f26f9 | 9224 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9225 | sq = ctx->sq_data; |
5f3f26f9 JA |
9226 | if (!sq->thread) |
9227 | sq = NULL; | |
9228 | } | |
dbbe9c64 JQ |
9229 | |
9230 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9231 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9232 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9233 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
ea64ec02 | 9234 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
87ce955b | 9235 | |
87ce955b JA |
9236 | if (f) |
9237 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9238 | else | |
9239 | seq_printf(m, "%5u: <none>\n", i); | |
9240 | } | |
9241 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9242 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b JA |
9243 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
9244 | ||
9245 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
9246 | (unsigned int) buf->len); | |
9247 | } | |
fad8e0de | 9248 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
87ce955b JA |
9249 | seq_printf(m, "Personalities:\n"); |
9250 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
9251 | } | |
d7718a9d JA |
9252 | seq_printf(m, "PollList:\n"); |
9253 | spin_lock_irq(&ctx->completion_lock); | |
9254 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9255 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9256 | struct io_kiocb *req; | |
9257 | ||
9258 | hlist_for_each_entry(req, list, hash_node) | |
9259 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9260 | req->task->task_works != NULL); | |
9261 | } | |
9262 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9263 | if (has_lock) |
9264 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9265 | } |
9266 | ||
9267 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9268 | { | |
9269 | struct io_ring_ctx *ctx = f->private_data; | |
9270 | ||
9271 | if (percpu_ref_tryget(&ctx->refs)) { | |
9272 | __io_uring_show_fdinfo(ctx, m); | |
9273 | percpu_ref_put(&ctx->refs); | |
9274 | } | |
9275 | } | |
bebdb65e | 9276 | #endif |
87ce955b | 9277 | |
2b188cc1 JA |
9278 | static const struct file_operations io_uring_fops = { |
9279 | .release = io_uring_release, | |
9280 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9281 | #ifndef CONFIG_MMU |
9282 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9283 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9284 | #endif | |
2b188cc1 JA |
9285 | .poll = io_uring_poll, |
9286 | .fasync = io_uring_fasync, | |
bebdb65e | 9287 | #ifdef CONFIG_PROC_FS |
87ce955b | 9288 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9289 | #endif |
2b188cc1 JA |
9290 | }; |
9291 | ||
9292 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9293 | struct io_uring_params *p) | |
9294 | { | |
75b28aff HV |
9295 | struct io_rings *rings; |
9296 | size_t size, sq_array_offset; | |
2b188cc1 | 9297 | |
bd740481 JA |
9298 | /* make sure these are sane, as we already accounted them */ |
9299 | ctx->sq_entries = p->sq_entries; | |
9300 | ctx->cq_entries = p->cq_entries; | |
9301 | ||
75b28aff HV |
9302 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9303 | if (size == SIZE_MAX) | |
9304 | return -EOVERFLOW; | |
9305 | ||
9306 | rings = io_mem_alloc(size); | |
9307 | if (!rings) | |
2b188cc1 JA |
9308 | return -ENOMEM; |
9309 | ||
75b28aff HV |
9310 | ctx->rings = rings; |
9311 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9312 | rings->sq_ring_mask = p->sq_entries - 1; | |
9313 | rings->cq_ring_mask = p->cq_entries - 1; | |
9314 | rings->sq_ring_entries = p->sq_entries; | |
9315 | rings->cq_ring_entries = p->cq_entries; | |
9316 | ctx->sq_mask = rings->sq_ring_mask; | |
9317 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9318 | |
9319 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9320 | if (size == SIZE_MAX) { |
9321 | io_mem_free(ctx->rings); | |
9322 | ctx->rings = NULL; | |
2b188cc1 | 9323 | return -EOVERFLOW; |
eb065d30 | 9324 | } |
2b188cc1 JA |
9325 | |
9326 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9327 | if (!ctx->sq_sqes) { |
9328 | io_mem_free(ctx->rings); | |
9329 | ctx->rings = NULL; | |
2b188cc1 | 9330 | return -ENOMEM; |
eb065d30 | 9331 | } |
2b188cc1 | 9332 | |
2b188cc1 JA |
9333 | return 0; |
9334 | } | |
9335 | ||
9faadcc8 PB |
9336 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9337 | { | |
9338 | int ret, fd; | |
9339 | ||
9340 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9341 | if (fd < 0) | |
9342 | return fd; | |
9343 | ||
baf186c4 | 9344 | ret = io_uring_add_task_file(ctx); |
9faadcc8 PB |
9345 | if (ret) { |
9346 | put_unused_fd(fd); | |
9347 | return ret; | |
9348 | } | |
9349 | fd_install(fd, file); | |
9350 | return fd; | |
9351 | } | |
9352 | ||
2b188cc1 JA |
9353 | /* |
9354 | * Allocate an anonymous fd, this is what constitutes the application | |
9355 | * visible backing of an io_uring instance. The application mmaps this | |
9356 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9357 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9358 | */ | |
9faadcc8 | 9359 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9360 | { |
9361 | struct file *file; | |
9faadcc8 | 9362 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9363 | int ret; |
9364 | ||
2b188cc1 JA |
9365 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9366 | &ctx->ring_sock); | |
9367 | if (ret) | |
9faadcc8 | 9368 | return ERR_PTR(ret); |
2b188cc1 JA |
9369 | #endif |
9370 | ||
2b188cc1 JA |
9371 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9372 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9373 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9374 | if (IS_ERR(file)) { |
9375 | sock_release(ctx->ring_sock); | |
9376 | ctx->ring_sock = NULL; | |
9377 | } else { | |
9378 | ctx->ring_sock->file = file; | |
0f212204 | 9379 | } |
2b188cc1 | 9380 | #endif |
9faadcc8 | 9381 | return file; |
2b188cc1 JA |
9382 | } |
9383 | ||
7f13657d XW |
9384 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9385 | struct io_uring_params __user *params) | |
2b188cc1 | 9386 | { |
2b188cc1 | 9387 | struct io_ring_ctx *ctx; |
9faadcc8 | 9388 | struct file *file; |
2b188cc1 JA |
9389 | int ret; |
9390 | ||
8110c1a6 | 9391 | if (!entries) |
2b188cc1 | 9392 | return -EINVAL; |
8110c1a6 JA |
9393 | if (entries > IORING_MAX_ENTRIES) { |
9394 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9395 | return -EINVAL; | |
9396 | entries = IORING_MAX_ENTRIES; | |
9397 | } | |
2b188cc1 JA |
9398 | |
9399 | /* | |
9400 | * Use twice as many entries for the CQ ring. It's possible for the | |
9401 | * application to drive a higher depth than the size of the SQ ring, | |
9402 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9403 | * some flexibility in overcommitting a bit. If the application has |
9404 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9405 | * of CQ ring entries manually. | |
2b188cc1 JA |
9406 | */ |
9407 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9408 | if (p->flags & IORING_SETUP_CQSIZE) { |
9409 | /* | |
9410 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9411 | * to a power-of-two, if it isn't already. We do NOT impose | |
9412 | * any cq vs sq ring sizing. | |
9413 | */ | |
eb2667b3 | 9414 | if (!p->cq_entries) |
33a107f0 | 9415 | return -EINVAL; |
8110c1a6 JA |
9416 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9417 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9418 | return -EINVAL; | |
9419 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9420 | } | |
eb2667b3 JQ |
9421 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9422 | if (p->cq_entries < p->sq_entries) | |
9423 | return -EINVAL; | |
33a107f0 JA |
9424 | } else { |
9425 | p->cq_entries = 2 * p->sq_entries; | |
9426 | } | |
2b188cc1 | 9427 | |
2b188cc1 | 9428 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9429 | if (!ctx) |
2b188cc1 | 9430 | return -ENOMEM; |
2b188cc1 | 9431 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9432 | if (!capable(CAP_IPC_LOCK)) |
9433 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9434 | |
9435 | /* | |
9436 | * This is just grabbed for accounting purposes. When a process exits, | |
9437 | * the mm is exited and dropped before the files, hence we need to hang | |
9438 | * on to this mm purely for the purposes of being able to unaccount | |
9439 | * memory (locked/pinned vm). It's not used for anything else. | |
9440 | */ | |
6b7898eb | 9441 | mmgrab(current->mm); |
2aede0e4 | 9442 | ctx->mm_account = current->mm; |
6b7898eb | 9443 | |
2b188cc1 JA |
9444 | ret = io_allocate_scq_urings(ctx, p); |
9445 | if (ret) | |
9446 | goto err; | |
9447 | ||
7e84e1c7 | 9448 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9449 | if (ret) |
9450 | goto err; | |
9451 | ||
7e84e1c7 SG |
9452 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
9453 | io_sq_offload_start(ctx); | |
9454 | ||
2b188cc1 | 9455 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9456 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9457 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9458 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9459 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9460 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9461 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9462 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9463 | |
9464 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9465 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9466 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9467 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9468 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9469 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9470 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9471 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9472 | |
7f13657d XW |
9473 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9474 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9475 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9476 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9477 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9478 | |
9479 | if (copy_to_user(params, p, sizeof(*p))) { | |
9480 | ret = -EFAULT; | |
9481 | goto err; | |
9482 | } | |
d1719f70 | 9483 | |
9faadcc8 PB |
9484 | file = io_uring_get_file(ctx); |
9485 | if (IS_ERR(file)) { | |
9486 | ret = PTR_ERR(file); | |
9487 | goto err; | |
9488 | } | |
9489 | ||
044c1ab3 JA |
9490 | /* |
9491 | * Install ring fd as the very last thing, so we don't risk someone | |
9492 | * having closed it before we finish setup | |
9493 | */ | |
9faadcc8 PB |
9494 | ret = io_uring_install_fd(ctx, file); |
9495 | if (ret < 0) { | |
9496 | /* fput will clean it up */ | |
9497 | fput(file); | |
9498 | return ret; | |
9499 | } | |
044c1ab3 | 9500 | |
c826bd7a | 9501 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9502 | return ret; |
9503 | err: | |
9504 | io_ring_ctx_wait_and_kill(ctx); | |
9505 | return ret; | |
9506 | } | |
9507 | ||
9508 | /* | |
9509 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9510 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9511 | * params structure passed in. | |
9512 | */ | |
9513 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9514 | { | |
9515 | struct io_uring_params p; | |
2b188cc1 JA |
9516 | int i; |
9517 | ||
9518 | if (copy_from_user(&p, params, sizeof(p))) | |
9519 | return -EFAULT; | |
9520 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9521 | if (p.resv[i]) | |
9522 | return -EINVAL; | |
9523 | } | |
9524 | ||
6c271ce2 | 9525 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9526 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9527 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9528 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9529 | return -EINVAL; |
9530 | ||
7f13657d | 9531 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9532 | } |
9533 | ||
9534 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9535 | struct io_uring_params __user *, params) | |
9536 | { | |
9537 | return io_uring_setup(entries, params); | |
9538 | } | |
9539 | ||
66f4af93 JA |
9540 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9541 | { | |
9542 | struct io_uring_probe *p; | |
9543 | size_t size; | |
9544 | int i, ret; | |
9545 | ||
9546 | size = struct_size(p, ops, nr_args); | |
9547 | if (size == SIZE_MAX) | |
9548 | return -EOVERFLOW; | |
9549 | p = kzalloc(size, GFP_KERNEL); | |
9550 | if (!p) | |
9551 | return -ENOMEM; | |
9552 | ||
9553 | ret = -EFAULT; | |
9554 | if (copy_from_user(p, arg, size)) | |
9555 | goto out; | |
9556 | ret = -EINVAL; | |
9557 | if (memchr_inv(p, 0, size)) | |
9558 | goto out; | |
9559 | ||
9560 | p->last_op = IORING_OP_LAST - 1; | |
9561 | if (nr_args > IORING_OP_LAST) | |
9562 | nr_args = IORING_OP_LAST; | |
9563 | ||
9564 | for (i = 0; i < nr_args; i++) { | |
9565 | p->ops[i].op = i; | |
9566 | if (!io_op_defs[i].not_supported) | |
9567 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9568 | } | |
9569 | p->ops_len = i; | |
9570 | ||
9571 | ret = 0; | |
9572 | if (copy_to_user(arg, p, size)) | |
9573 | ret = -EFAULT; | |
9574 | out: | |
9575 | kfree(p); | |
9576 | return ret; | |
9577 | } | |
9578 | ||
071698e1 JA |
9579 | static int io_register_personality(struct io_ring_ctx *ctx) |
9580 | { | |
4379bf8b | 9581 | const struct cred *creds; |
1e6fa521 | 9582 | int ret; |
071698e1 | 9583 | |
4379bf8b | 9584 | creds = get_current_cred(); |
1e6fa521 | 9585 | |
4379bf8b JA |
9586 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
9587 | USHRT_MAX, GFP_KERNEL); | |
9588 | if (ret < 0) | |
9589 | put_cred(creds); | |
1e6fa521 | 9590 | return ret; |
071698e1 JA |
9591 | } |
9592 | ||
21b55dbc SG |
9593 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9594 | unsigned int nr_args) | |
9595 | { | |
9596 | struct io_uring_restriction *res; | |
9597 | size_t size; | |
9598 | int i, ret; | |
9599 | ||
7e84e1c7 SG |
9600 | /* Restrictions allowed only if rings started disabled */ |
9601 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9602 | return -EBADFD; | |
9603 | ||
21b55dbc | 9604 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9605 | if (ctx->restrictions.registered) |
21b55dbc SG |
9606 | return -EBUSY; |
9607 | ||
9608 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9609 | return -EINVAL; | |
9610 | ||
9611 | size = array_size(nr_args, sizeof(*res)); | |
9612 | if (size == SIZE_MAX) | |
9613 | return -EOVERFLOW; | |
9614 | ||
9615 | res = memdup_user(arg, size); | |
9616 | if (IS_ERR(res)) | |
9617 | return PTR_ERR(res); | |
9618 | ||
9619 | ret = 0; | |
9620 | ||
9621 | for (i = 0; i < nr_args; i++) { | |
9622 | switch (res[i].opcode) { | |
9623 | case IORING_RESTRICTION_REGISTER_OP: | |
9624 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9625 | ret = -EINVAL; | |
9626 | goto out; | |
9627 | } | |
9628 | ||
9629 | __set_bit(res[i].register_op, | |
9630 | ctx->restrictions.register_op); | |
9631 | break; | |
9632 | case IORING_RESTRICTION_SQE_OP: | |
9633 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9634 | ret = -EINVAL; | |
9635 | goto out; | |
9636 | } | |
9637 | ||
9638 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9639 | break; | |
9640 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9641 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9642 | break; | |
9643 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9644 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9645 | break; | |
9646 | default: | |
9647 | ret = -EINVAL; | |
9648 | goto out; | |
9649 | } | |
9650 | } | |
9651 | ||
9652 | out: | |
9653 | /* Reset all restrictions if an error happened */ | |
9654 | if (ret != 0) | |
9655 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9656 | else | |
7e84e1c7 | 9657 | ctx->restrictions.registered = true; |
21b55dbc SG |
9658 | |
9659 | kfree(res); | |
9660 | return ret; | |
9661 | } | |
9662 | ||
7e84e1c7 SG |
9663 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9664 | { | |
9665 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9666 | return -EBADFD; | |
9667 | ||
9668 | if (ctx->restrictions.registered) | |
9669 | ctx->restricted = 1; | |
9670 | ||
7e84e1c7 | 9671 | io_sq_offload_start(ctx); |
7e84e1c7 SG |
9672 | return 0; |
9673 | } | |
9674 | ||
071698e1 JA |
9675 | static bool io_register_op_must_quiesce(int op) |
9676 | { | |
9677 | switch (op) { | |
9678 | case IORING_UNREGISTER_FILES: | |
9679 | case IORING_REGISTER_FILES_UPDATE: | |
9680 | case IORING_REGISTER_PROBE: | |
9681 | case IORING_REGISTER_PERSONALITY: | |
9682 | case IORING_UNREGISTER_PERSONALITY: | |
9683 | return false; | |
9684 | default: | |
9685 | return true; | |
9686 | } | |
9687 | } | |
9688 | ||
edafccee JA |
9689 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9690 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9691 | __releases(ctx->uring_lock) |
9692 | __acquires(ctx->uring_lock) | |
edafccee JA |
9693 | { |
9694 | int ret; | |
9695 | ||
35fa71a0 JA |
9696 | /* |
9697 | * We're inside the ring mutex, if the ref is already dying, then | |
9698 | * someone else killed the ctx or is already going through | |
9699 | * io_uring_register(). | |
9700 | */ | |
9701 | if (percpu_ref_is_dying(&ctx->refs)) | |
9702 | return -ENXIO; | |
9703 | ||
071698e1 | 9704 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9705 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9706 | |
05f3fb3c JA |
9707 | /* |
9708 | * Drop uring mutex before waiting for references to exit. If | |
9709 | * another thread is currently inside io_uring_enter() it might | |
9710 | * need to grab the uring_lock to make progress. If we hold it | |
9711 | * here across the drain wait, then we can deadlock. It's safe | |
9712 | * to drop the mutex here, since no new references will come in | |
9713 | * after we've killed the percpu ref. | |
9714 | */ | |
9715 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9716 | do { |
9717 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9718 | if (!ret) | |
9719 | break; | |
ed6930c9 JA |
9720 | ret = io_run_task_work_sig(); |
9721 | if (ret < 0) | |
9722 | break; | |
af9c1a44 JA |
9723 | } while (1); |
9724 | ||
05f3fb3c | 9725 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9726 | |
c150368b JA |
9727 | if (ret) { |
9728 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
9729 | goto out_quiesce; |
9730 | } | |
9731 | } | |
9732 | ||
9733 | if (ctx->restricted) { | |
9734 | if (opcode >= IORING_REGISTER_LAST) { | |
9735 | ret = -EINVAL; | |
9736 | goto out; | |
9737 | } | |
9738 | ||
9739 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9740 | ret = -EACCES; | |
c150368b JA |
9741 | goto out; |
9742 | } | |
05f3fb3c | 9743 | } |
edafccee JA |
9744 | |
9745 | switch (opcode) { | |
9746 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9747 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9748 | break; |
9749 | case IORING_UNREGISTER_BUFFERS: | |
9750 | ret = -EINVAL; | |
9751 | if (arg || nr_args) | |
9752 | break; | |
0a96bbe4 | 9753 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9754 | break; |
6b06314c JA |
9755 | case IORING_REGISTER_FILES: |
9756 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9757 | break; | |
9758 | case IORING_UNREGISTER_FILES: | |
9759 | ret = -EINVAL; | |
9760 | if (arg || nr_args) | |
9761 | break; | |
9762 | ret = io_sqe_files_unregister(ctx); | |
9763 | break; | |
c3a31e60 JA |
9764 | case IORING_REGISTER_FILES_UPDATE: |
9765 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9766 | break; | |
9b402849 | 9767 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9768 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9769 | ret = -EINVAL; |
9770 | if (nr_args != 1) | |
9771 | break; | |
9772 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9773 | if (ret) |
9774 | break; | |
9775 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9776 | ctx->eventfd_async = 1; | |
9777 | else | |
9778 | ctx->eventfd_async = 0; | |
9b402849 JA |
9779 | break; |
9780 | case IORING_UNREGISTER_EVENTFD: | |
9781 | ret = -EINVAL; | |
9782 | if (arg || nr_args) | |
9783 | break; | |
9784 | ret = io_eventfd_unregister(ctx); | |
9785 | break; | |
66f4af93 JA |
9786 | case IORING_REGISTER_PROBE: |
9787 | ret = -EINVAL; | |
9788 | if (!arg || nr_args > 256) | |
9789 | break; | |
9790 | ret = io_probe(ctx, arg, nr_args); | |
9791 | break; | |
071698e1 JA |
9792 | case IORING_REGISTER_PERSONALITY: |
9793 | ret = -EINVAL; | |
9794 | if (arg || nr_args) | |
9795 | break; | |
9796 | ret = io_register_personality(ctx); | |
9797 | break; | |
9798 | case IORING_UNREGISTER_PERSONALITY: | |
9799 | ret = -EINVAL; | |
9800 | if (arg) | |
9801 | break; | |
9802 | ret = io_unregister_personality(ctx, nr_args); | |
9803 | break; | |
7e84e1c7 SG |
9804 | case IORING_REGISTER_ENABLE_RINGS: |
9805 | ret = -EINVAL; | |
9806 | if (arg || nr_args) | |
9807 | break; | |
9808 | ret = io_register_enable_rings(ctx); | |
9809 | break; | |
21b55dbc SG |
9810 | case IORING_REGISTER_RESTRICTIONS: |
9811 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9812 | break; | |
edafccee JA |
9813 | default: |
9814 | ret = -EINVAL; | |
9815 | break; | |
9816 | } | |
9817 | ||
21b55dbc | 9818 | out: |
071698e1 | 9819 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9820 | /* bring the ctx back to life */ |
05f3fb3c | 9821 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 9822 | out_quiesce: |
0f158b4c | 9823 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9824 | } |
edafccee JA |
9825 | return ret; |
9826 | } | |
9827 | ||
9828 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9829 | void __user *, arg, unsigned int, nr_args) | |
9830 | { | |
9831 | struct io_ring_ctx *ctx; | |
9832 | long ret = -EBADF; | |
9833 | struct fd f; | |
9834 | ||
9835 | f = fdget(fd); | |
9836 | if (!f.file) | |
9837 | return -EBADF; | |
9838 | ||
9839 | ret = -EOPNOTSUPP; | |
9840 | if (f.file->f_op != &io_uring_fops) | |
9841 | goto out_fput; | |
9842 | ||
9843 | ctx = f.file->private_data; | |
9844 | ||
b6c23dd5 PB |
9845 | io_run_task_work(); |
9846 | ||
edafccee JA |
9847 | mutex_lock(&ctx->uring_lock); |
9848 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9849 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9850 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9851 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9852 | out_fput: |
9853 | fdput(f); | |
9854 | return ret; | |
9855 | } | |
9856 | ||
2b188cc1 JA |
9857 | static int __init io_uring_init(void) |
9858 | { | |
d7f62e82 SM |
9859 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9860 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9861 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9862 | } while (0) | |
9863 | ||
9864 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9865 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9866 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9867 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9868 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9869 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9870 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9871 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9872 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9873 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9874 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9875 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9876 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9877 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9878 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
9879 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
9880 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
9881 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
9882 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
9883 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
9884 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
9885 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
9886 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
9887 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
9888 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
9889 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 9890 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
9891 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
9892 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
9893 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 9894 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 9895 | |
d3656344 | 9896 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 9897 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
9898 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
9899 | SLAB_ACCOUNT); | |
2b188cc1 JA |
9900 | return 0; |
9901 | }; | |
9902 | __initcall(io_uring_init); |