]>
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> |
2b188cc1 | 81 | |
c826bd7a DD |
82 | #define CREATE_TRACE_POINTS |
83 | #include <trace/events/io_uring.h> | |
84 | ||
2b188cc1 JA |
85 | #include <uapi/linux/io_uring.h> |
86 | ||
87 | #include "internal.h" | |
561fb04a | 88 | #include "io-wq.h" |
2b188cc1 | 89 | |
5277deaa | 90 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 91 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
92 | |
93 | /* | |
94 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
95 | */ | |
96 | #define IORING_FILE_TABLE_SHIFT 9 | |
97 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
98 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
99 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
21b55dbc SG |
100 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
101 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 102 | |
b16fed66 PB |
103 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
104 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
105 | IOSQE_BUFFER_SELECT) | |
106 | ||
2b188cc1 JA |
107 | struct io_uring { |
108 | u32 head ____cacheline_aligned_in_smp; | |
109 | u32 tail ____cacheline_aligned_in_smp; | |
110 | }; | |
111 | ||
1e84b97b | 112 | /* |
75b28aff HV |
113 | * This data is shared with the application through the mmap at offsets |
114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
115 | * |
116 | * The offsets to the member fields are published through struct | |
117 | * io_sqring_offsets when calling io_uring_setup. | |
118 | */ | |
75b28aff | 119 | struct io_rings { |
1e84b97b SB |
120 | /* |
121 | * Head and tail offsets into the ring; the offsets need to be | |
122 | * masked to get valid indices. | |
123 | * | |
75b28aff HV |
124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
125 | * and the application controls tail of the sq ring and the head of the | |
126 | * cq ring. | |
1e84b97b | 127 | */ |
75b28aff | 128 | struct io_uring sq, cq; |
1e84b97b | 129 | /* |
75b28aff | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
131 | * ring_entries - 1) |
132 | */ | |
75b28aff HV |
133 | u32 sq_ring_mask, cq_ring_mask; |
134 | /* Ring sizes (constant, power of 2) */ | |
135 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
136 | /* |
137 | * Number of invalid entries dropped by the kernel due to | |
138 | * invalid index stored in array | |
139 | * | |
140 | * Written by the kernel, shouldn't be modified by the | |
141 | * application (i.e. get number of "new events" by comparing to | |
142 | * cached value). | |
143 | * | |
144 | * After a new SQ head value was read by the application this | |
145 | * counter includes all submissions that were dropped reaching | |
146 | * the new SQ head (and possibly more). | |
147 | */ | |
75b28aff | 148 | u32 sq_dropped; |
1e84b97b | 149 | /* |
0d9b5b3a | 150 | * Runtime SQ flags |
1e84b97b SB |
151 | * |
152 | * Written by the kernel, shouldn't be modified by the | |
153 | * application. | |
154 | * | |
155 | * The application needs a full memory barrier before checking | |
156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
157 | */ | |
75b28aff | 158 | u32 sq_flags; |
0d9b5b3a SG |
159 | /* |
160 | * Runtime CQ flags | |
161 | * | |
162 | * Written by the application, shouldn't be modified by the | |
163 | * kernel. | |
164 | */ | |
165 | u32 cq_flags; | |
1e84b97b SB |
166 | /* |
167 | * Number of completion events lost because the queue was full; | |
168 | * this should be avoided by the application by making sure | |
0b4295b5 | 169 | * there are not more requests pending than there is space in |
1e84b97b SB |
170 | * the completion queue. |
171 | * | |
172 | * Written by the kernel, shouldn't be modified by the | |
173 | * application (i.e. get number of "new events" by comparing to | |
174 | * cached value). | |
175 | * | |
176 | * As completion events come in out of order this counter is not | |
177 | * ordered with any other data. | |
178 | */ | |
75b28aff | 179 | u32 cq_overflow; |
1e84b97b SB |
180 | /* |
181 | * Ring buffer of completion events. | |
182 | * | |
183 | * The kernel writes completion events fresh every time they are | |
184 | * produced, so the application is allowed to modify pending | |
185 | * entries. | |
186 | */ | |
75b28aff | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
188 | }; |
189 | ||
45d189c6 PB |
190 | enum io_uring_cmd_flags { |
191 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 192 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
193 | }; |
194 | ||
edafccee JA |
195 | struct io_mapped_ubuf { |
196 | u64 ubuf; | |
4751f53d | 197 | u64 ubuf_end; |
edafccee JA |
198 | struct bio_vec *bvec; |
199 | unsigned int nr_bvecs; | |
de293938 | 200 | unsigned long acct_pages; |
edafccee JA |
201 | }; |
202 | ||
50238531 BM |
203 | struct io_ring_ctx; |
204 | ||
6c2450ae PB |
205 | struct io_overflow_cqe { |
206 | struct io_uring_cqe cqe; | |
207 | struct list_head list; | |
208 | }; | |
209 | ||
a04b0ac0 PB |
210 | struct io_fixed_file { |
211 | /* file * with additional FFS_* flags */ | |
212 | unsigned long file_ptr; | |
213 | }; | |
214 | ||
269bbe5f BM |
215 | struct io_rsrc_put { |
216 | struct list_head list; | |
50238531 BM |
217 | union { |
218 | void *rsrc; | |
219 | struct file *file; | |
220 | }; | |
269bbe5f BM |
221 | }; |
222 | ||
223 | struct fixed_rsrc_table { | |
a04b0ac0 | 224 | struct io_fixed_file *files; |
31b51510 JA |
225 | }; |
226 | ||
b895c9a6 | 227 | struct io_rsrc_node { |
05589553 XW |
228 | struct percpu_ref refs; |
229 | struct list_head node; | |
269bbe5f | 230 | struct list_head rsrc_list; |
b895c9a6 | 231 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 232 | struct llist_node llist; |
e297822b | 233 | bool done; |
05589553 XW |
234 | }; |
235 | ||
40ae0ff7 PB |
236 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
237 | ||
b895c9a6 | 238 | struct io_rsrc_data { |
269bbe5f | 239 | struct fixed_rsrc_table *table; |
05f3fb3c JA |
240 | struct io_ring_ctx *ctx; |
241 | ||
40ae0ff7 | 242 | rsrc_put_fn *do_put; |
05f3fb3c | 243 | struct percpu_ref refs; |
05f3fb3c | 244 | struct completion done; |
8bad28d8 | 245 | bool quiesce; |
05f3fb3c JA |
246 | }; |
247 | ||
5a2e745d JA |
248 | struct io_buffer { |
249 | struct list_head list; | |
250 | __u64 addr; | |
251 | __s32 len; | |
252 | __u16 bid; | |
253 | }; | |
254 | ||
21b55dbc SG |
255 | struct io_restriction { |
256 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
257 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
258 | u8 sqe_flags_allowed; | |
259 | u8 sqe_flags_required; | |
7e84e1c7 | 260 | bool registered; |
21b55dbc SG |
261 | }; |
262 | ||
37d1e2e3 JA |
263 | enum { |
264 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
265 | IO_SQ_THREAD_SHOULD_PARK, | |
266 | }; | |
267 | ||
534ca6d6 JA |
268 | struct io_sq_data { |
269 | refcount_t refs; | |
9e138a48 | 270 | atomic_t park_pending; |
09a6f4ef | 271 | struct mutex lock; |
69fb2131 JA |
272 | |
273 | /* ctx's that are using this sqd */ | |
274 | struct list_head ctx_list; | |
69fb2131 | 275 | |
534ca6d6 JA |
276 | struct task_struct *thread; |
277 | struct wait_queue_head wait; | |
08369246 XW |
278 | |
279 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
280 | int sq_cpu; |
281 | pid_t task_pid; | |
5c2469e0 | 282 | pid_t task_tgid; |
37d1e2e3 JA |
283 | |
284 | unsigned long state; | |
37d1e2e3 | 285 | struct completion exited; |
b7f5a0bf | 286 | struct callback_head *park_task_work; |
534ca6d6 JA |
287 | }; |
288 | ||
258b29a9 | 289 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 290 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 291 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 292 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
293 | |
294 | struct io_comp_state { | |
6dd0be1e | 295 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 296 | unsigned int nr; |
c7dae4ba JA |
297 | unsigned int locked_free_nr; |
298 | /* inline/task_work completion list, under ->uring_lock */ | |
1b4c351f | 299 | struct list_head free_list; |
c7dae4ba JA |
300 | /* IRQ completion list, under ->completion_lock */ |
301 | struct list_head locked_free_list; | |
258b29a9 PB |
302 | }; |
303 | ||
a1ab7b35 PB |
304 | struct io_submit_link { |
305 | struct io_kiocb *head; | |
306 | struct io_kiocb *last; | |
307 | }; | |
308 | ||
258b29a9 PB |
309 | struct io_submit_state { |
310 | struct blk_plug plug; | |
a1ab7b35 | 311 | struct io_submit_link link; |
258b29a9 PB |
312 | |
313 | /* | |
314 | * io_kiocb alloc cache | |
315 | */ | |
bf019da7 | 316 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
317 | unsigned int free_reqs; |
318 | ||
319 | bool plug_started; | |
320 | ||
321 | /* | |
322 | * Batch completion logic | |
323 | */ | |
324 | struct io_comp_state comp; | |
325 | ||
326 | /* | |
327 | * File reference cache | |
328 | */ | |
329 | struct file *file; | |
330 | unsigned int fd; | |
331 | unsigned int file_refs; | |
332 | unsigned int ios_left; | |
333 | }; | |
334 | ||
2b188cc1 JA |
335 | struct io_ring_ctx { |
336 | struct { | |
337 | struct percpu_ref refs; | |
338 | } ____cacheline_aligned_in_smp; | |
339 | ||
340 | struct { | |
341 | unsigned int flags; | |
e1d85334 | 342 | unsigned int compat: 1; |
e1d85334 RD |
343 | unsigned int drain_next: 1; |
344 | unsigned int eventfd_async: 1; | |
21b55dbc | 345 | unsigned int restricted: 1; |
2b188cc1 | 346 | |
75b28aff HV |
347 | /* |
348 | * Ring buffer of indices into array of io_uring_sqe, which is | |
349 | * mmapped by the application using the IORING_OFF_SQES offset. | |
350 | * | |
351 | * This indirection could e.g. be used to assign fixed | |
352 | * io_uring_sqe entries to operations and only submit them to | |
353 | * the queue when needed. | |
354 | * | |
355 | * The kernel modifies neither the indices array nor the entries | |
356 | * array. | |
357 | */ | |
358 | u32 *sq_array; | |
2b188cc1 JA |
359 | unsigned cached_sq_head; |
360 | unsigned sq_entries; | |
361 | unsigned sq_mask; | |
6c271ce2 | 362 | unsigned sq_thread_idle; |
498ccd9e | 363 | unsigned cached_sq_dropped; |
2c3bac6d | 364 | unsigned cached_cq_overflow; |
ad3eb2c8 | 365 | unsigned long sq_check_overflow; |
de0617e4 | 366 | |
e941894e JA |
367 | /* hashed buffered write serialization */ |
368 | struct io_wq_hash *hash_map; | |
369 | ||
de0617e4 | 370 | struct list_head defer_list; |
5262f567 | 371 | struct list_head timeout_list; |
1d7bb1d5 | 372 | struct list_head cq_overflow_list; |
fcb323cc | 373 | |
ad3eb2c8 | 374 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
375 | } ____cacheline_aligned_in_smp; |
376 | ||
3c1a2ead JA |
377 | struct { |
378 | struct mutex uring_lock; | |
379 | wait_queue_head_t wait; | |
380 | } ____cacheline_aligned_in_smp; | |
381 | ||
382 | struct io_submit_state submit_state; | |
383 | ||
206aefde JA |
384 | struct io_rings *rings; |
385 | ||
2aede0e4 JA |
386 | /* Only used for accounting purposes */ |
387 | struct mm_struct *mm_account; | |
388 | ||
7c30f36a | 389 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
391 | ||
90554200 | 392 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 393 | struct list_head sqd_list; |
75b28aff | 394 | |
6b06314c JA |
395 | /* |
396 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
397 | * readers must ensure that ->refs is alive as long as the file* is | |
398 | * used. Only updated through io_uring_register(2). | |
399 | */ | |
b895c9a6 | 400 | struct io_rsrc_data *file_data; |
6b06314c JA |
401 | unsigned nr_user_files; |
402 | ||
edafccee JA |
403 | /* if used, fixed mapped user buffers */ |
404 | unsigned nr_user_bufs; | |
405 | struct io_mapped_ubuf *user_bufs; | |
406 | ||
2b188cc1 JA |
407 | struct user_struct *user; |
408 | ||
0f158b4c | 409 | struct completion ref_comp; |
206aefde JA |
410 | |
411 | #if defined(CONFIG_UNIX) | |
412 | struct socket *ring_sock; | |
413 | #endif | |
414 | ||
9e15c3a0 | 415 | struct xarray io_buffers; |
5a2e745d | 416 | |
61cf9370 MWO |
417 | struct xarray personalities; |
418 | u32 pers_next; | |
071698e1 | 419 | |
206aefde JA |
420 | struct { |
421 | unsigned cached_cq_tail; | |
422 | unsigned cq_entries; | |
423 | unsigned cq_mask; | |
424 | atomic_t cq_timeouts; | |
f010505b | 425 | unsigned cq_last_tm_flush; |
ad3eb2c8 | 426 | unsigned long cq_check_overflow; |
206aefde JA |
427 | struct wait_queue_head cq_wait; |
428 | struct fasync_struct *cq_fasync; | |
429 | struct eventfd_ctx *cq_ev_fd; | |
430 | } ____cacheline_aligned_in_smp; | |
2b188cc1 | 431 | |
2b188cc1 JA |
432 | struct { |
433 | spinlock_t completion_lock; | |
e94f141b | 434 | |
def596e9 | 435 | /* |
540e32a0 | 436 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
437 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
438 | * For SQPOLL, only the single threaded io_sq_thread() will | |
439 | * manipulate the list, hence no extra locking is needed there. | |
440 | */ | |
540e32a0 | 441 | struct list_head iopoll_list; |
78076bb6 JA |
442 | struct hlist_head *cancel_hash; |
443 | unsigned cancel_hash_bits; | |
e94f141b | 444 | bool poll_multi_file; |
2b188cc1 | 445 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 446 | |
269bbe5f BM |
447 | struct delayed_work rsrc_put_work; |
448 | struct llist_head rsrc_put_llist; | |
d67d2263 BM |
449 | struct list_head rsrc_ref_list; |
450 | spinlock_t rsrc_ref_lock; | |
a7f0ed5a | 451 | struct io_rsrc_node *rsrc_node; |
b895c9a6 | 452 | struct io_rsrc_node *rsrc_backup_node; |
4a38aed2 | 453 | |
21b55dbc | 454 | struct io_restriction restrictions; |
3c1a2ead | 455 | |
7c25c0d1 JA |
456 | /* exit task_work */ |
457 | struct callback_head *exit_task_work; | |
458 | ||
3c1a2ead JA |
459 | /* Keep this last, we don't need it for the fast path */ |
460 | struct work_struct exit_work; | |
13bf43f5 | 461 | struct list_head tctx_list; |
2b188cc1 JA |
462 | }; |
463 | ||
53e043b2 SM |
464 | struct io_uring_task { |
465 | /* submission side */ | |
466 | struct xarray xa; | |
467 | struct wait_queue_head wait; | |
ee53fb2b SM |
468 | const struct io_ring_ctx *last; |
469 | struct io_wq *io_wq; | |
53e043b2 | 470 | struct percpu_counter inflight; |
b303fe2e | 471 | atomic_t inflight_tracked; |
53e043b2 | 472 | atomic_t in_idle; |
53e043b2 SM |
473 | |
474 | spinlock_t task_lock; | |
475 | struct io_wq_work_list task_list; | |
476 | unsigned long task_state; | |
477 | struct callback_head task_work; | |
478 | }; | |
479 | ||
09bb8394 JA |
480 | /* |
481 | * First field must be the file pointer in all the | |
482 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
483 | */ | |
221c5eb2 JA |
484 | struct io_poll_iocb { |
485 | struct file *file; | |
018043be | 486 | struct wait_queue_head *head; |
221c5eb2 | 487 | __poll_t events; |
8c838788 | 488 | bool done; |
221c5eb2 | 489 | bool canceled; |
b69de288 JA |
490 | bool update_events; |
491 | bool update_user_data; | |
492 | union { | |
493 | struct wait_queue_entry wait; | |
494 | struct { | |
495 | u64 old_user_data; | |
496 | u64 new_user_data; | |
497 | }; | |
498 | }; | |
221c5eb2 JA |
499 | }; |
500 | ||
018043be PB |
501 | struct io_poll_remove { |
502 | struct file *file; | |
503 | u64 addr; | |
504 | }; | |
505 | ||
b5dba59e JA |
506 | struct io_close { |
507 | struct file *file; | |
b5dba59e JA |
508 | int fd; |
509 | }; | |
510 | ||
ad8a48ac JA |
511 | struct io_timeout_data { |
512 | struct io_kiocb *req; | |
513 | struct hrtimer timer; | |
514 | struct timespec64 ts; | |
515 | enum hrtimer_mode mode; | |
516 | }; | |
517 | ||
8ed8d3c3 JA |
518 | struct io_accept { |
519 | struct file *file; | |
520 | struct sockaddr __user *addr; | |
521 | int __user *addr_len; | |
522 | int flags; | |
09952e3e | 523 | unsigned long nofile; |
8ed8d3c3 JA |
524 | }; |
525 | ||
526 | struct io_sync { | |
527 | struct file *file; | |
528 | loff_t len; | |
529 | loff_t off; | |
530 | int flags; | |
d63d1b5e | 531 | int mode; |
8ed8d3c3 JA |
532 | }; |
533 | ||
fbf23849 JA |
534 | struct io_cancel { |
535 | struct file *file; | |
536 | u64 addr; | |
537 | }; | |
538 | ||
b29472ee JA |
539 | struct io_timeout { |
540 | struct file *file; | |
bfe68a22 PB |
541 | u32 off; |
542 | u32 target_seq; | |
135fcde8 | 543 | struct list_head list; |
90cd7e42 PB |
544 | /* head of the link, used by linked timeouts only */ |
545 | struct io_kiocb *head; | |
b29472ee JA |
546 | }; |
547 | ||
0bdf7a2d PB |
548 | struct io_timeout_rem { |
549 | struct file *file; | |
550 | u64 addr; | |
9c8e11b3 PB |
551 | |
552 | /* timeout update */ | |
553 | struct timespec64 ts; | |
554 | u32 flags; | |
0bdf7a2d PB |
555 | }; |
556 | ||
9adbd45d JA |
557 | struct io_rw { |
558 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
559 | struct kiocb kiocb; | |
560 | u64 addr; | |
561 | u64 len; | |
562 | }; | |
563 | ||
3fbb51c1 JA |
564 | struct io_connect { |
565 | struct file *file; | |
566 | struct sockaddr __user *addr; | |
567 | int addr_len; | |
568 | }; | |
569 | ||
e47293fd JA |
570 | struct io_sr_msg { |
571 | struct file *file; | |
fddaface | 572 | union { |
270a5940 | 573 | struct user_msghdr __user *umsg; |
fddaface JA |
574 | void __user *buf; |
575 | }; | |
e47293fd | 576 | int msg_flags; |
bcda7baa | 577 | int bgid; |
fddaface | 578 | size_t len; |
bcda7baa | 579 | struct io_buffer *kbuf; |
e47293fd JA |
580 | }; |
581 | ||
15b71abe JA |
582 | struct io_open { |
583 | struct file *file; | |
584 | int dfd; | |
15b71abe | 585 | struct filename *filename; |
c12cedf2 | 586 | struct open_how how; |
4022e7af | 587 | unsigned long nofile; |
15b71abe JA |
588 | }; |
589 | ||
269bbe5f | 590 | struct io_rsrc_update { |
05f3fb3c JA |
591 | struct file *file; |
592 | u64 arg; | |
593 | u32 nr_args; | |
594 | u32 offset; | |
595 | }; | |
596 | ||
4840e418 JA |
597 | struct io_fadvise { |
598 | struct file *file; | |
599 | u64 offset; | |
600 | u32 len; | |
601 | u32 advice; | |
602 | }; | |
603 | ||
c1ca757b JA |
604 | struct io_madvise { |
605 | struct file *file; | |
606 | u64 addr; | |
607 | u32 len; | |
608 | u32 advice; | |
609 | }; | |
610 | ||
3e4827b0 JA |
611 | struct io_epoll { |
612 | struct file *file; | |
613 | int epfd; | |
614 | int op; | |
615 | int fd; | |
616 | struct epoll_event event; | |
e47293fd JA |
617 | }; |
618 | ||
7d67af2c PB |
619 | struct io_splice { |
620 | struct file *file_out; | |
621 | struct file *file_in; | |
622 | loff_t off_out; | |
623 | loff_t off_in; | |
624 | u64 len; | |
625 | unsigned int flags; | |
626 | }; | |
627 | ||
ddf0322d JA |
628 | struct io_provide_buf { |
629 | struct file *file; | |
630 | __u64 addr; | |
631 | __s32 len; | |
632 | __u32 bgid; | |
633 | __u16 nbufs; | |
634 | __u16 bid; | |
635 | }; | |
636 | ||
1d9e1288 BM |
637 | struct io_statx { |
638 | struct file *file; | |
639 | int dfd; | |
640 | unsigned int mask; | |
641 | unsigned int flags; | |
e62753e4 | 642 | const char __user *filename; |
1d9e1288 BM |
643 | struct statx __user *buffer; |
644 | }; | |
645 | ||
36f4fa68 JA |
646 | struct io_shutdown { |
647 | struct file *file; | |
648 | int how; | |
649 | }; | |
650 | ||
80a261fd JA |
651 | struct io_rename { |
652 | struct file *file; | |
653 | int old_dfd; | |
654 | int new_dfd; | |
655 | struct filename *oldpath; | |
656 | struct filename *newpath; | |
657 | int flags; | |
658 | }; | |
659 | ||
14a1143b JA |
660 | struct io_unlink { |
661 | struct file *file; | |
662 | int dfd; | |
663 | int flags; | |
664 | struct filename *filename; | |
665 | }; | |
666 | ||
3ca405eb PB |
667 | struct io_completion { |
668 | struct file *file; | |
669 | struct list_head list; | |
8c3f9cd1 | 670 | u32 cflags; |
3ca405eb PB |
671 | }; |
672 | ||
f499a021 JA |
673 | struct io_async_connect { |
674 | struct sockaddr_storage address; | |
675 | }; | |
676 | ||
03b1230c JA |
677 | struct io_async_msghdr { |
678 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
679 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
680 | struct iovec *free_iov; | |
03b1230c JA |
681 | struct sockaddr __user *uaddr; |
682 | struct msghdr msg; | |
b537916c | 683 | struct sockaddr_storage addr; |
03b1230c JA |
684 | }; |
685 | ||
f67676d1 JA |
686 | struct io_async_rw { |
687 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
688 | const struct iovec *free_iovec; |
689 | struct iov_iter iter; | |
227c0c96 | 690 | size_t bytes_done; |
bcf5a063 | 691 | struct wait_page_queue wpq; |
f67676d1 JA |
692 | }; |
693 | ||
6b47ee6e PB |
694 | enum { |
695 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
696 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
697 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
698 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
699 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 700 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 701 | |
6b47ee6e PB |
702 | REQ_F_FAIL_LINK_BIT, |
703 | REQ_F_INFLIGHT_BIT, | |
704 | REQ_F_CUR_POS_BIT, | |
705 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 706 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 707 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 708 | REQ_F_POLLED_BIT, |
bcda7baa | 709 | REQ_F_BUFFER_SELECTED_BIT, |
900fad45 | 710 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 711 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 712 | REQ_F_REISSUE_BIT, |
8c130827 | 713 | REQ_F_DONT_REISSUE_BIT, |
7b29f92d JA |
714 | /* keep async read/write and isreg together and in order */ |
715 | REQ_F_ASYNC_READ_BIT, | |
716 | REQ_F_ASYNC_WRITE_BIT, | |
717 | REQ_F_ISREG_BIT, | |
84557871 JA |
718 | |
719 | /* not a real bit, just to check we're not overflowing the space */ | |
720 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
721 | }; |
722 | ||
723 | enum { | |
724 | /* ctx owns file */ | |
725 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
726 | /* drain existing IO first */ | |
727 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
728 | /* linked sqes */ | |
729 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
730 | /* doesn't sever on completion < 0 */ | |
731 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
732 | /* IOSQE_ASYNC */ | |
733 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
734 | /* IOSQE_BUFFER_SELECT */ |
735 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 736 | |
6b47ee6e PB |
737 | /* fail rest of links */ |
738 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
b05a1bcd | 739 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
740 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
741 | /* read/write uses file position */ | |
742 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
743 | /* must not punt to workers */ | |
744 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 745 | /* has or had linked timeout */ |
6b47ee6e | 746 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
747 | /* needs cleanup */ |
748 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
749 | /* already went through poll handler */ |
750 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
751 | /* buffer already selected */ |
752 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
900fad45 PB |
753 | /* linked timeout is active, i.e. prepared by link's head */ |
754 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
755 | /* completion is deferred through io_comp_state */ |
756 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
757 | /* caller should reissue async */ |
758 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
759 | /* don't attempt request reissue, see io_rw_reissue() */ |
760 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
7b29f92d JA |
761 | /* supports async reads */ |
762 | REQ_F_ASYNC_READ = BIT(REQ_F_ASYNC_READ_BIT), | |
763 | /* supports async writes */ | |
764 | REQ_F_ASYNC_WRITE = BIT(REQ_F_ASYNC_WRITE_BIT), | |
765 | /* regular file */ | |
766 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
d7718a9d JA |
767 | }; |
768 | ||
769 | struct async_poll { | |
770 | struct io_poll_iocb poll; | |
807abcb0 | 771 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
772 | }; |
773 | ||
7cbf1722 JA |
774 | struct io_task_work { |
775 | struct io_wq_work_node node; | |
776 | task_work_func_t func; | |
777 | }; | |
778 | ||
09bb8394 JA |
779 | /* |
780 | * NOTE! Each of the iocb union members has the file pointer | |
781 | * as the first entry in their struct definition. So you can | |
782 | * access the file pointer through any of the sub-structs, | |
783 | * or directly as just 'ki_filp' in this struct. | |
784 | */ | |
2b188cc1 | 785 | struct io_kiocb { |
221c5eb2 | 786 | union { |
09bb8394 | 787 | struct file *file; |
9adbd45d | 788 | struct io_rw rw; |
221c5eb2 | 789 | struct io_poll_iocb poll; |
018043be | 790 | struct io_poll_remove poll_remove; |
8ed8d3c3 JA |
791 | struct io_accept accept; |
792 | struct io_sync sync; | |
fbf23849 | 793 | struct io_cancel cancel; |
b29472ee | 794 | struct io_timeout timeout; |
0bdf7a2d | 795 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 796 | struct io_connect connect; |
e47293fd | 797 | struct io_sr_msg sr_msg; |
15b71abe | 798 | struct io_open open; |
b5dba59e | 799 | struct io_close close; |
269bbe5f | 800 | struct io_rsrc_update rsrc_update; |
4840e418 | 801 | struct io_fadvise fadvise; |
c1ca757b | 802 | struct io_madvise madvise; |
3e4827b0 | 803 | struct io_epoll epoll; |
7d67af2c | 804 | struct io_splice splice; |
ddf0322d | 805 | struct io_provide_buf pbuf; |
1d9e1288 | 806 | struct io_statx statx; |
36f4fa68 | 807 | struct io_shutdown shutdown; |
80a261fd | 808 | struct io_rename rename; |
14a1143b | 809 | struct io_unlink unlink; |
3ca405eb PB |
810 | /* use only after cleaning per-op data, see io_clean_op() */ |
811 | struct io_completion compl; | |
221c5eb2 | 812 | }; |
2b188cc1 | 813 | |
e8c2bc1f JA |
814 | /* opcode allocated if it needs to store data for async defer */ |
815 | void *async_data; | |
d625c6ee | 816 | u8 opcode; |
65a6543d XW |
817 | /* polled IO has completed */ |
818 | u8 iopoll_completed; | |
2b188cc1 | 819 | |
4f4eeba8 | 820 | u16 buf_index; |
9cf7c104 | 821 | u32 result; |
4f4eeba8 | 822 | |
010e8e6b PB |
823 | struct io_ring_ctx *ctx; |
824 | unsigned int flags; | |
abc54d63 | 825 | atomic_t refs; |
010e8e6b PB |
826 | struct task_struct *task; |
827 | u64 user_data; | |
d7718a9d | 828 | |
f2f87370 | 829 | struct io_kiocb *link; |
269bbe5f | 830 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 831 | |
b303fe2e | 832 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 833 | struct list_head inflight_entry; |
7cbf1722 JA |
834 | union { |
835 | struct io_task_work io_task_work; | |
836 | struct callback_head task_work; | |
837 | }; | |
010e8e6b PB |
838 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
839 | struct hlist_node hash_node; | |
840 | struct async_poll *apoll; | |
841 | struct io_wq_work work; | |
2b188cc1 | 842 | }; |
05589553 | 843 | |
13bf43f5 PB |
844 | struct io_tctx_node { |
845 | struct list_head ctx_node; | |
846 | struct task_struct *task; | |
13bf43f5 PB |
847 | struct io_ring_ctx *ctx; |
848 | }; | |
849 | ||
27dc8338 PB |
850 | struct io_defer_entry { |
851 | struct list_head list; | |
852 | struct io_kiocb *req; | |
9cf7c104 | 853 | u32 seq; |
2b188cc1 JA |
854 | }; |
855 | ||
d3656344 | 856 | struct io_op_def { |
d3656344 JA |
857 | /* needs req->file assigned */ |
858 | unsigned needs_file : 1; | |
d3656344 JA |
859 | /* hash wq insertion if file is a regular file */ |
860 | unsigned hash_reg_file : 1; | |
861 | /* unbound wq insertion if file is a non-regular file */ | |
862 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
863 | /* opcode is not supported by this kernel */ |
864 | unsigned not_supported : 1; | |
8a72758c JA |
865 | /* set if opcode supports polled "wait" */ |
866 | unsigned pollin : 1; | |
867 | unsigned pollout : 1; | |
bcda7baa JA |
868 | /* op supports buffer selection */ |
869 | unsigned buffer_select : 1; | |
26f0505a PB |
870 | /* do prep async if is going to be punted */ |
871 | unsigned needs_async_setup : 1; | |
27926b68 JA |
872 | /* should block plug */ |
873 | unsigned plug : 1; | |
e8c2bc1f JA |
874 | /* size of async data needed, if any */ |
875 | unsigned short async_size; | |
d3656344 JA |
876 | }; |
877 | ||
0918682b | 878 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
879 | [IORING_OP_NOP] = {}, |
880 | [IORING_OP_READV] = { | |
d3656344 JA |
881 | .needs_file = 1, |
882 | .unbound_nonreg_file = 1, | |
8a72758c | 883 | .pollin = 1, |
4d954c25 | 884 | .buffer_select = 1, |
26f0505a | 885 | .needs_async_setup = 1, |
27926b68 | 886 | .plug = 1, |
e8c2bc1f | 887 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 888 | }, |
0463b6c5 | 889 | [IORING_OP_WRITEV] = { |
d3656344 JA |
890 | .needs_file = 1, |
891 | .hash_reg_file = 1, | |
892 | .unbound_nonreg_file = 1, | |
8a72758c | 893 | .pollout = 1, |
26f0505a | 894 | .needs_async_setup = 1, |
27926b68 | 895 | .plug = 1, |
e8c2bc1f | 896 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 897 | }, |
0463b6c5 | 898 | [IORING_OP_FSYNC] = { |
d3656344 JA |
899 | .needs_file = 1, |
900 | }, | |
0463b6c5 | 901 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
902 | .needs_file = 1, |
903 | .unbound_nonreg_file = 1, | |
8a72758c | 904 | .pollin = 1, |
27926b68 | 905 | .plug = 1, |
e8c2bc1f | 906 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 907 | }, |
0463b6c5 | 908 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
909 | .needs_file = 1, |
910 | .hash_reg_file = 1, | |
911 | .unbound_nonreg_file = 1, | |
8a72758c | 912 | .pollout = 1, |
27926b68 | 913 | .plug = 1, |
e8c2bc1f | 914 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 915 | }, |
0463b6c5 | 916 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
917 | .needs_file = 1, |
918 | .unbound_nonreg_file = 1, | |
919 | }, | |
0463b6c5 PB |
920 | [IORING_OP_POLL_REMOVE] = {}, |
921 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
922 | .needs_file = 1, |
923 | }, | |
0463b6c5 | 924 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
925 | .needs_file = 1, |
926 | .unbound_nonreg_file = 1, | |
8a72758c | 927 | .pollout = 1, |
26f0505a | 928 | .needs_async_setup = 1, |
e8c2bc1f | 929 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 930 | }, |
0463b6c5 | 931 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
932 | .needs_file = 1, |
933 | .unbound_nonreg_file = 1, | |
8a72758c | 934 | .pollin = 1, |
52de1fe1 | 935 | .buffer_select = 1, |
26f0505a | 936 | .needs_async_setup = 1, |
e8c2bc1f | 937 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 938 | }, |
0463b6c5 | 939 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 940 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 941 | }, |
9c8e11b3 PB |
942 | [IORING_OP_TIMEOUT_REMOVE] = { |
943 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 944 | }, |
0463b6c5 | 945 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
946 | .needs_file = 1, |
947 | .unbound_nonreg_file = 1, | |
8a72758c | 948 | .pollin = 1, |
d3656344 | 949 | }, |
0463b6c5 PB |
950 | [IORING_OP_ASYNC_CANCEL] = {}, |
951 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 952 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 953 | }, |
0463b6c5 | 954 | [IORING_OP_CONNECT] = { |
d3656344 JA |
955 | .needs_file = 1, |
956 | .unbound_nonreg_file = 1, | |
8a72758c | 957 | .pollout = 1, |
26f0505a | 958 | .needs_async_setup = 1, |
e8c2bc1f | 959 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 960 | }, |
0463b6c5 | 961 | [IORING_OP_FALLOCATE] = { |
d3656344 | 962 | .needs_file = 1, |
d3656344 | 963 | }, |
44526bed JA |
964 | [IORING_OP_OPENAT] = {}, |
965 | [IORING_OP_CLOSE] = {}, | |
966 | [IORING_OP_FILES_UPDATE] = {}, | |
967 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 968 | [IORING_OP_READ] = { |
3a6820f2 JA |
969 | .needs_file = 1, |
970 | .unbound_nonreg_file = 1, | |
8a72758c | 971 | .pollin = 1, |
bcda7baa | 972 | .buffer_select = 1, |
27926b68 | 973 | .plug = 1, |
e8c2bc1f | 974 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 975 | }, |
0463b6c5 | 976 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
977 | .needs_file = 1, |
978 | .unbound_nonreg_file = 1, | |
8a72758c | 979 | .pollout = 1, |
27926b68 | 980 | .plug = 1, |
e8c2bc1f | 981 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 982 | }, |
0463b6c5 | 983 | [IORING_OP_FADVISE] = { |
4840e418 | 984 | .needs_file = 1, |
c1ca757b | 985 | }, |
44526bed | 986 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 987 | [IORING_OP_SEND] = { |
fddaface JA |
988 | .needs_file = 1, |
989 | .unbound_nonreg_file = 1, | |
8a72758c | 990 | .pollout = 1, |
fddaface | 991 | }, |
0463b6c5 | 992 | [IORING_OP_RECV] = { |
fddaface JA |
993 | .needs_file = 1, |
994 | .unbound_nonreg_file = 1, | |
8a72758c | 995 | .pollin = 1, |
bcda7baa | 996 | .buffer_select = 1, |
fddaface | 997 | }, |
0463b6c5 | 998 | [IORING_OP_OPENAT2] = { |
cebdb986 | 999 | }, |
3e4827b0 JA |
1000 | [IORING_OP_EPOLL_CTL] = { |
1001 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1002 | }, |
7d67af2c PB |
1003 | [IORING_OP_SPLICE] = { |
1004 | .needs_file = 1, | |
1005 | .hash_reg_file = 1, | |
1006 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1007 | }, |
1008 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1009 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1010 | [IORING_OP_TEE] = { |
1011 | .needs_file = 1, | |
1012 | .hash_reg_file = 1, | |
1013 | .unbound_nonreg_file = 1, | |
1014 | }, | |
36f4fa68 JA |
1015 | [IORING_OP_SHUTDOWN] = { |
1016 | .needs_file = 1, | |
1017 | }, | |
44526bed JA |
1018 | [IORING_OP_RENAMEAT] = {}, |
1019 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
1020 | }; |
1021 | ||
7a612350 | 1022 | static bool io_disarm_next(struct io_kiocb *req); |
d56d938b | 1023 | static void io_uring_del_task_file(unsigned long index); |
9936c7c2 PB |
1024 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1025 | struct task_struct *task, | |
1026 | struct files_struct *files); | |
37d1e2e3 | 1027 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
b895c9a6 | 1028 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx); |
1ffc5422 | 1029 | |
78e19bbe | 1030 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 1031 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1032 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c7dae4ba JA |
1033 | static void io_dismantle_req(struct io_kiocb *req); |
1034 | static void io_put_task(struct task_struct *task, int nr); | |
94ae5e77 JA |
1035 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
1036 | static void io_queue_linked_timeout(struct io_kiocb *req); | |
05f3fb3c | 1037 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
269bbe5f | 1038 | struct io_uring_rsrc_update *ip, |
05f3fb3c | 1039 | unsigned nr_args); |
68fb8979 | 1040 | static void io_clean_op(struct io_kiocb *req); |
8371adf5 PB |
1041 | static struct file *io_file_get(struct io_submit_state *state, |
1042 | struct io_kiocb *req, int fd, bool fixed); | |
c5eef2b9 | 1043 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1044 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1045 | |
907d1df3 | 1046 | static void io_req_task_queue(struct io_kiocb *req); |
65453d1e JA |
1047 | static void io_submit_flush_completions(struct io_comp_state *cs, |
1048 | struct io_ring_ctx *ctx); | |
5082620f | 1049 | static bool io_poll_remove_waitqs(struct io_kiocb *req); |
179ae0d1 | 1050 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1051 | |
2b188cc1 JA |
1052 | static struct kmem_cache *req_cachep; |
1053 | ||
0918682b | 1054 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1055 | |
1056 | struct sock *io_uring_get_socket(struct file *file) | |
1057 | { | |
1058 | #if defined(CONFIG_UNIX) | |
1059 | if (file->f_op == &io_uring_fops) { | |
1060 | struct io_ring_ctx *ctx = file->private_data; | |
1061 | ||
1062 | return ctx->ring_sock->sk; | |
1063 | } | |
1064 | #endif | |
1065 | return NULL; | |
1066 | } | |
1067 | EXPORT_SYMBOL(io_uring_get_socket); | |
1068 | ||
f2f87370 PB |
1069 | #define io_for_each_link(pos, head) \ |
1070 | for (pos = (head); pos; pos = pos->link) | |
1071 | ||
b895c9a6 | 1072 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1073 | { |
1074 | struct io_ring_ctx *ctx = req->ctx; | |
1075 | ||
269bbe5f | 1076 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1077 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1078 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1079 | } |
1080 | } | |
1081 | ||
08d23634 PB |
1082 | static bool io_match_task(struct io_kiocb *head, |
1083 | struct task_struct *task, | |
1084 | struct files_struct *files) | |
1085 | { | |
1086 | struct io_kiocb *req; | |
1087 | ||
68207680 | 1088 | if (task && head->task != task) |
08d23634 PB |
1089 | return false; |
1090 | if (!files) | |
1091 | return true; | |
1092 | ||
1093 | io_for_each_link(req, head) { | |
b05a1bcd | 1094 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1095 | return true; |
08d23634 PB |
1096 | } |
1097 | return false; | |
1098 | } | |
1099 | ||
c40f6379 JA |
1100 | static inline void req_set_fail_links(struct io_kiocb *req) |
1101 | { | |
1102 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1103 | req->flags |= REQ_F_FAIL_LINK; | |
1104 | } | |
4a38aed2 | 1105 | |
2b188cc1 JA |
1106 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1107 | { | |
1108 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1109 | ||
0f158b4c | 1110 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1111 | } |
1112 | ||
8eb7e2d0 PB |
1113 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1114 | { | |
1115 | return !req->timeout.off; | |
1116 | } | |
1117 | ||
2b188cc1 JA |
1118 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1119 | { | |
1120 | struct io_ring_ctx *ctx; | |
78076bb6 | 1121 | int hash_bits; |
2b188cc1 JA |
1122 | |
1123 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1124 | if (!ctx) | |
1125 | return NULL; | |
1126 | ||
78076bb6 JA |
1127 | /* |
1128 | * Use 5 bits less than the max cq entries, that should give us around | |
1129 | * 32 entries per hash list if totally full and uniformly spread. | |
1130 | */ | |
1131 | hash_bits = ilog2(p->cq_entries); | |
1132 | hash_bits -= 5; | |
1133 | if (hash_bits <= 0) | |
1134 | hash_bits = 1; | |
1135 | ctx->cancel_hash_bits = hash_bits; | |
1136 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1137 | GFP_KERNEL); | |
1138 | if (!ctx->cancel_hash) | |
1139 | goto err; | |
1140 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1141 | ||
21482896 | 1142 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1143 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1144 | goto err; | |
2b188cc1 JA |
1145 | |
1146 | ctx->flags = p->flags; | |
90554200 | 1147 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1148 | INIT_LIST_HEAD(&ctx->sqd_list); |
2b188cc1 | 1149 | init_waitqueue_head(&ctx->cq_wait); |
1d7bb1d5 | 1150 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1151 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1152 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1153 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 JA |
1154 | mutex_init(&ctx->uring_lock); |
1155 | init_waitqueue_head(&ctx->wait); | |
1156 | spin_lock_init(&ctx->completion_lock); | |
540e32a0 | 1157 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1158 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1159 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1160 | spin_lock_init(&ctx->rsrc_ref_lock); |
1161 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1162 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1163 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1164 | INIT_LIST_HEAD(&ctx->tctx_list); |
1b4c351f | 1165 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
c7dae4ba | 1166 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
2b188cc1 | 1167 | return ctx; |
206aefde | 1168 | err: |
78076bb6 | 1169 | kfree(ctx->cancel_hash); |
206aefde JA |
1170 | kfree(ctx); |
1171 | return NULL; | |
2b188cc1 JA |
1172 | } |
1173 | ||
9cf7c104 | 1174 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1175 | { |
2bc9930e JA |
1176 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1177 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1178 | |
9cf7c104 | 1179 | return seq != ctx->cached_cq_tail |
2c3bac6d | 1180 | + READ_ONCE(ctx->cached_cq_overflow); |
2bc9930e | 1181 | } |
de0617e4 | 1182 | |
9d858b21 | 1183 | return false; |
de0617e4 JA |
1184 | } |
1185 | ||
ce3d5aae PB |
1186 | static void io_req_track_inflight(struct io_kiocb *req) |
1187 | { | |
ce3d5aae | 1188 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1189 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1190 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1191 | } |
1192 | } | |
1193 | ||
1e6fa521 JA |
1194 | static void io_prep_async_work(struct io_kiocb *req) |
1195 | { | |
1196 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1197 | struct io_ring_ctx *ctx = req->ctx; |
1198 | ||
003e8dcc JA |
1199 | if (!req->work.creds) |
1200 | req->work.creds = get_current_cred(); | |
1201 | ||
e1d675df PB |
1202 | req->work.list.next = NULL; |
1203 | req->work.flags = 0; | |
feaadc4f PB |
1204 | if (req->flags & REQ_F_FORCE_ASYNC) |
1205 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1206 | ||
1e6fa521 JA |
1207 | if (req->flags & REQ_F_ISREG) { |
1208 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1209 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1210 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1211 | if (def->unbound_nonreg_file) |
1212 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1213 | } | |
e1d675df PB |
1214 | |
1215 | switch (req->opcode) { | |
1216 | case IORING_OP_SPLICE: | |
1217 | case IORING_OP_TEE: | |
e1d675df PB |
1218 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1219 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1220 | break; | |
1221 | } | |
561fb04a | 1222 | } |
cccf0ee8 | 1223 | |
cbdcb435 | 1224 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1225 | { |
cbdcb435 | 1226 | struct io_kiocb *cur; |
54a91f3b | 1227 | |
f2f87370 PB |
1228 | io_for_each_link(cur, req) |
1229 | io_prep_async_work(cur); | |
561fb04a JA |
1230 | } |
1231 | ||
ebf93667 | 1232 | static void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1233 | { |
a197f664 | 1234 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1235 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1236 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1237 | |
3bfe6106 JA |
1238 | BUG_ON(!tctx); |
1239 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1240 | |
cbdcb435 PB |
1241 | /* init ->work of the whole link before punting */ |
1242 | io_prep_async_link(req); | |
d07f1e8a PB |
1243 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1244 | &req->work, req->flags); | |
ebf93667 | 1245 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1246 | if (link) |
1247 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1248 | } |
1249 | ||
1ee4160c | 1250 | static void io_kill_timeout(struct io_kiocb *req, int status) |
5262f567 | 1251 | { |
e8c2bc1f | 1252 | struct io_timeout_data *io = req->async_data; |
5262f567 JA |
1253 | int ret; |
1254 | ||
e8c2bc1f | 1255 | ret = hrtimer_try_to_cancel(&io->timer); |
5262f567 | 1256 | if (ret != -1) { |
01cec8c1 PB |
1257 | atomic_set(&req->ctx->cq_timeouts, |
1258 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1259 | list_del_init(&req->timeout.list); |
1ee4160c | 1260 | io_cqring_fill_event(req, status); |
216578e5 | 1261 | io_put_req_deferred(req, 1); |
5262f567 JA |
1262 | } |
1263 | } | |
1264 | ||
04518945 | 1265 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1266 | { |
04518945 | 1267 | do { |
27dc8338 PB |
1268 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1269 | struct io_defer_entry, list); | |
de0617e4 | 1270 | |
9cf7c104 | 1271 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1272 | break; |
27dc8338 | 1273 | list_del_init(&de->list); |
907d1df3 | 1274 | io_req_task_queue(de->req); |
27dc8338 | 1275 | kfree(de); |
04518945 PB |
1276 | } while (!list_empty(&ctx->defer_list)); |
1277 | } | |
1278 | ||
360428f8 | 1279 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1280 | { |
f010505b MDG |
1281 | u32 seq; |
1282 | ||
1283 | if (list_empty(&ctx->timeout_list)) | |
1284 | return; | |
1285 | ||
1286 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); | |
1287 | ||
1288 | do { | |
1289 | u32 events_needed, events_got; | |
360428f8 | 1290 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1291 | struct io_kiocb, timeout.list); |
de0617e4 | 1292 | |
8eb7e2d0 | 1293 | if (io_is_timeout_noseq(req)) |
360428f8 | 1294 | break; |
f010505b MDG |
1295 | |
1296 | /* | |
1297 | * Since seq can easily wrap around over time, subtract | |
1298 | * the last seq at which timeouts were flushed before comparing. | |
1299 | * Assuming not more than 2^31-1 events have happened since, | |
1300 | * these subtractions won't have wrapped, so we can check if | |
1301 | * target is in [last_seq, current_seq] by comparing the two. | |
1302 | */ | |
1303 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1304 | events_got = seq - ctx->cq_last_tm_flush; | |
1305 | if (events_got < events_needed) | |
360428f8 | 1306 | break; |
bfe68a22 | 1307 | |
135fcde8 | 1308 | list_del_init(&req->timeout.list); |
1ee4160c | 1309 | io_kill_timeout(req, 0); |
f010505b MDG |
1310 | } while (!list_empty(&ctx->timeout_list)); |
1311 | ||
1312 | ctx->cq_last_tm_flush = seq; | |
360428f8 | 1313 | } |
5262f567 | 1314 | |
360428f8 PB |
1315 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
1316 | { | |
1317 | io_flush_timeouts(ctx); | |
ec30e04b PB |
1318 | |
1319 | /* order cqe stores with ring update */ | |
1320 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 | 1321 | |
04518945 PB |
1322 | if (unlikely(!list_empty(&ctx->defer_list))) |
1323 | __io_queue_deferred(ctx); | |
de0617e4 JA |
1324 | } |
1325 | ||
90554200 JA |
1326 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1327 | { | |
1328 | struct io_rings *r = ctx->rings; | |
1329 | ||
1330 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; | |
1331 | } | |
1332 | ||
888aae2e PB |
1333 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1334 | { | |
1335 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1336 | } | |
1337 | ||
2b188cc1 JA |
1338 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1339 | { | |
75b28aff | 1340 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1341 | unsigned tail; |
1342 | ||
115e12e5 SB |
1343 | /* |
1344 | * writes to the cq entry need to come after reading head; the | |
1345 | * control dependency is enough as we're using WRITE_ONCE to | |
1346 | * fill the cq entry | |
1347 | */ | |
888aae2e | 1348 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
2b188cc1 JA |
1349 | return NULL; |
1350 | ||
888aae2e | 1351 | tail = ctx->cached_cq_tail++; |
75b28aff | 1352 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1353 | } |
1354 | ||
f2842ab5 JA |
1355 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1356 | { | |
f0b493e6 JA |
1357 | if (!ctx->cq_ev_fd) |
1358 | return false; | |
7e55a19c SG |
1359 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1360 | return false; | |
f2842ab5 JA |
1361 | if (!ctx->eventfd_async) |
1362 | return true; | |
b41e9852 | 1363 | return io_wq_current_is_worker(); |
f2842ab5 JA |
1364 | } |
1365 | ||
b41e9852 | 1366 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1367 | { |
b1445e59 PB |
1368 | /* see waitqueue_active() comment */ |
1369 | smp_mb(); | |
1370 | ||
1d7bb1d5 JA |
1371 | if (waitqueue_active(&ctx->wait)) |
1372 | wake_up(&ctx->wait); | |
534ca6d6 JA |
1373 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1374 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1375 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1376 | eventfd_signal(ctx->cq_ev_fd, 1); |
b1445e59 | 1377 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1378 | wake_up_interruptible(&ctx->cq_wait); |
1379 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1380 | } | |
1d7bb1d5 JA |
1381 | } |
1382 | ||
80c18e4a PB |
1383 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1384 | { | |
b1445e59 PB |
1385 | /* see waitqueue_active() comment */ |
1386 | smp_mb(); | |
1387 | ||
80c18e4a PB |
1388 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
1389 | if (waitqueue_active(&ctx->wait)) | |
1390 | wake_up(&ctx->wait); | |
1391 | } | |
1392 | if (io_should_trigger_evfd(ctx)) | |
1393 | eventfd_signal(ctx->cq_ev_fd, 1); | |
b1445e59 | 1394 | if (waitqueue_active(&ctx->cq_wait)) { |
4aa84f2f PB |
1395 | wake_up_interruptible(&ctx->cq_wait); |
1396 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
1397 | } | |
80c18e4a PB |
1398 | } |
1399 | ||
c4a2ed72 | 1400 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1401 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 JA |
1402 | { |
1403 | struct io_rings *rings = ctx->rings; | |
1d7bb1d5 | 1404 | unsigned long flags; |
b18032bb | 1405 | bool all_flushed, posted; |
1d7bb1d5 | 1406 | |
e23de15f PB |
1407 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
1408 | return false; | |
1d7bb1d5 | 1409 | |
b18032bb | 1410 | posted = false; |
1d7bb1d5 | 1411 | spin_lock_irqsave(&ctx->completion_lock, flags); |
6c2450ae PB |
1412 | while (!list_empty(&ctx->cq_overflow_list)) { |
1413 | struct io_uring_cqe *cqe = io_get_cqring(ctx); | |
1414 | struct io_overflow_cqe *ocqe; | |
e6c8aa9a | 1415 | |
1d7bb1d5 JA |
1416 | if (!cqe && !force) |
1417 | break; | |
6c2450ae PB |
1418 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1419 | struct io_overflow_cqe, list); | |
1420 | if (cqe) | |
1421 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1422 | else | |
1d7bb1d5 | 1423 | WRITE_ONCE(ctx->rings->cq_overflow, |
6c2450ae | 1424 | ++ctx->cached_cq_overflow); |
b18032bb | 1425 | posted = true; |
6c2450ae PB |
1426 | list_del(&ocqe->list); |
1427 | kfree(ocqe); | |
1d7bb1d5 JA |
1428 | } |
1429 | ||
09e88404 PB |
1430 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1431 | if (all_flushed) { | |
1432 | clear_bit(0, &ctx->sq_check_overflow); | |
1433 | clear_bit(0, &ctx->cq_check_overflow); | |
1434 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; | |
1435 | } | |
46930143 | 1436 | |
b18032bb JA |
1437 | if (posted) |
1438 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1439 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1440 | if (posted) |
1441 | io_cqring_ev_posted(ctx); | |
09e88404 | 1442 | return all_flushed; |
1d7bb1d5 JA |
1443 | } |
1444 | ||
6c2450ae | 1445 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
6c503150 | 1446 | { |
ca0a2651 JA |
1447 | bool ret = true; |
1448 | ||
6c503150 PB |
1449 | if (test_bit(0, &ctx->cq_check_overflow)) { |
1450 | /* iopoll syncs against uring_lock, not completion_lock */ | |
1451 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1452 | mutex_lock(&ctx->uring_lock); | |
6c2450ae | 1453 | ret = __io_cqring_overflow_flush(ctx, force); |
6c503150 PB |
1454 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1455 | mutex_unlock(&ctx->uring_lock); | |
1456 | } | |
ca0a2651 JA |
1457 | |
1458 | return ret; | |
6c503150 PB |
1459 | } |
1460 | ||
abc54d63 JA |
1461 | /* |
1462 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1463 | * see commit f958d7b528b1 for details. | |
1464 | */ | |
1465 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1466 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1467 | ||
de9b4cca JA |
1468 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
1469 | { | |
abc54d63 | 1470 | return atomic_inc_not_zero(&req->refs); |
de9b4cca JA |
1471 | } |
1472 | ||
1473 | static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs) | |
1474 | { | |
abc54d63 JA |
1475 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1476 | return atomic_sub_and_test(refs, &req->refs); | |
de9b4cca JA |
1477 | } |
1478 | ||
1479 | static inline bool req_ref_put_and_test(struct io_kiocb *req) | |
1480 | { | |
abc54d63 JA |
1481 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1482 | return atomic_dec_and_test(&req->refs); | |
de9b4cca JA |
1483 | } |
1484 | ||
1485 | static inline void req_ref_put(struct io_kiocb *req) | |
1486 | { | |
abc54d63 | 1487 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
de9b4cca JA |
1488 | } |
1489 | ||
1490 | static inline void req_ref_get(struct io_kiocb *req) | |
1491 | { | |
abc54d63 JA |
1492 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1493 | atomic_inc(&req->refs); | |
de9b4cca JA |
1494 | } |
1495 | ||
5082620f | 1496 | static bool __io_cqring_fill_event(struct io_kiocb *req, long res, |
8c3f9cd1 | 1497 | unsigned int cflags) |
2b188cc1 | 1498 | { |
78e19bbe | 1499 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1500 | struct io_uring_cqe *cqe; |
1501 | ||
7471e1af | 1502 | trace_io_uring_complete(ctx, req->user_data, res, cflags); |
51c3ff62 | 1503 | |
2b188cc1 JA |
1504 | /* |
1505 | * If we can't get a cq entry, userspace overflowed the | |
1506 | * submission (by quite a lot). Increment the overflow count in | |
1507 | * the ring. | |
1508 | */ | |
1509 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1510 | if (likely(cqe)) { |
78e19bbe | 1511 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1512 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1513 | WRITE_ONCE(cqe->flags, cflags); |
5082620f | 1514 | return true; |
6c2450ae | 1515 | } |
c4ea060e | 1516 | if (!atomic_read(&req->task->io_uring->in_idle)) { |
6c2450ae PB |
1517 | struct io_overflow_cqe *ocqe; |
1518 | ||
1519 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); | |
1520 | if (!ocqe) | |
1521 | goto overflow; | |
ad3eb2c8 JA |
1522 | if (list_empty(&ctx->cq_overflow_list)) { |
1523 | set_bit(0, &ctx->sq_check_overflow); | |
1524 | set_bit(0, &ctx->cq_check_overflow); | |
6d5f9049 | 1525 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
ad3eb2c8 | 1526 | } |
6c2450ae PB |
1527 | ocqe->cqe.user_data = req->user_data; |
1528 | ocqe->cqe.res = res; | |
1529 | ocqe->cqe.flags = cflags; | |
1530 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
5082620f | 1531 | return true; |
2b188cc1 | 1532 | } |
6c2450ae PB |
1533 | overflow: |
1534 | /* | |
1535 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1536 | * or cannot allocate an overflow entry, then we need to drop it | |
1537 | * on the floor. | |
1538 | */ | |
1539 | WRITE_ONCE(ctx->rings->cq_overflow, ++ctx->cached_cq_overflow); | |
5082620f | 1540 | return false; |
2b188cc1 JA |
1541 | } |
1542 | ||
bcda7baa JA |
1543 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
1544 | { | |
1545 | __io_cqring_fill_event(req, res, 0); | |
1546 | } | |
1547 | ||
7a612350 PB |
1548 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1549 | unsigned int cflags) | |
2b188cc1 | 1550 | { |
78e19bbe | 1551 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1552 | unsigned long flags; |
1553 | ||
1554 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
bcda7baa | 1555 | __io_cqring_fill_event(req, res, cflags); |
c7dae4ba JA |
1556 | /* |
1557 | * If we're the last reference to this request, add to our locked | |
1558 | * free_list cache. | |
1559 | */ | |
de9b4cca | 1560 | if (req_ref_put_and_test(req)) { |
c7dae4ba JA |
1561 | struct io_comp_state *cs = &ctx->submit_state.comp; |
1562 | ||
7a612350 PB |
1563 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
1564 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) | |
1565 | io_disarm_next(req); | |
1566 | if (req->link) { | |
1567 | io_req_task_queue(req->link); | |
1568 | req->link = NULL; | |
1569 | } | |
1570 | } | |
c7dae4ba JA |
1571 | io_dismantle_req(req); |
1572 | io_put_task(req->task, 1); | |
1573 | list_add(&req->compl.list, &cs->locked_free_list); | |
1574 | cs->locked_free_nr++; | |
180f829f PB |
1575 | } else { |
1576 | if (!percpu_ref_tryget(&ctx->refs)) | |
1577 | req = NULL; | |
1578 | } | |
7a612350 | 1579 | io_commit_cqring(ctx); |
2b188cc1 | 1580 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
7a612350 | 1581 | |
180f829f PB |
1582 | if (req) { |
1583 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1584 | percpu_ref_put(&ctx->refs); |
180f829f | 1585 | } |
229a7b63 JA |
1586 | } |
1587 | ||
a38d68db | 1588 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1589 | unsigned int cflags) |
229a7b63 | 1590 | { |
68fb8979 PB |
1591 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
1592 | io_clean_op(req); | |
a38d68db PB |
1593 | req->result = res; |
1594 | req->compl.cflags = cflags; | |
e342c807 | 1595 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1596 | } |
1597 | ||
889fca73 PB |
1598 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1599 | long res, unsigned cflags) | |
bcda7baa | 1600 | { |
889fca73 PB |
1601 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1602 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1603 | else |
c7dae4ba | 1604 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1605 | } |
1606 | ||
a38d68db | 1607 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1608 | { |
889fca73 | 1609 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1610 | } |
1611 | ||
f41db273 PB |
1612 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1613 | { | |
1614 | req_set_fail_links(req); | |
1615 | io_put_req(req); | |
1616 | io_req_complete_post(req, res, 0); | |
1617 | } | |
1618 | ||
dac7a098 PB |
1619 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
1620 | struct io_comp_state *cs) | |
1621 | { | |
1622 | spin_lock_irq(&ctx->completion_lock); | |
1623 | list_splice_init(&cs->locked_free_list, &cs->free_list); | |
1624 | cs->locked_free_nr = 0; | |
1625 | spin_unlock_irq(&ctx->completion_lock); | |
1626 | } | |
1627 | ||
dd78f492 | 1628 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1629 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1630 | { |
c7dae4ba JA |
1631 | struct io_submit_state *state = &ctx->submit_state; |
1632 | struct io_comp_state *cs = &state->comp; | |
dd78f492 | 1633 | int nr; |
0ddf92e8 | 1634 | |
c7dae4ba JA |
1635 | /* |
1636 | * If we have more than a batch's worth of requests in our IRQ side | |
1637 | * locked cache, grab the lock and move them over to our submission | |
1638 | * side cache. | |
1639 | */ | |
dac7a098 PB |
1640 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) |
1641 | io_flush_cached_locked_reqs(ctx, cs); | |
0ddf92e8 | 1642 | |
dd78f492 | 1643 | nr = state->free_reqs; |
c7dae4ba | 1644 | while (!list_empty(&cs->free_list)) { |
dd78f492 PB |
1645 | struct io_kiocb *req = list_first_entry(&cs->free_list, |
1646 | struct io_kiocb, compl.list); | |
1647 | ||
1b4c351f | 1648 | list_del(&req->compl.list); |
dd78f492 PB |
1649 | state->reqs[nr++] = req; |
1650 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1651 | break; |
1b4c351f JA |
1652 | } |
1653 | ||
dd78f492 PB |
1654 | state->free_reqs = nr; |
1655 | return nr != 0; | |
0ddf92e8 JA |
1656 | } |
1657 | ||
e5d1bc0a | 1658 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1659 | { |
e5d1bc0a PB |
1660 | struct io_submit_state *state = &ctx->submit_state; |
1661 | ||
1662 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); | |
1663 | ||
f6b6c7d6 | 1664 | if (!state->free_reqs) { |
291b2821 | 1665 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2579f913 JA |
1666 | int ret; |
1667 | ||
c7dae4ba | 1668 | if (io_flush_cached_reqs(ctx)) |
e5d1bc0a PB |
1669 | goto got_req; |
1670 | ||
bf019da7 PB |
1671 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1672 | state->reqs); | |
fd6fab2c JA |
1673 | |
1674 | /* | |
1675 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1676 | * retry single alloc to be on the safe side. | |
1677 | */ | |
1678 | if (unlikely(ret <= 0)) { | |
1679 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1680 | if (!state->reqs[0]) | |
3893f39f | 1681 | return NULL; |
fd6fab2c JA |
1682 | ret = 1; |
1683 | } | |
291b2821 | 1684 | state->free_reqs = ret; |
2b188cc1 | 1685 | } |
e5d1bc0a | 1686 | got_req: |
291b2821 PB |
1687 | state->free_reqs--; |
1688 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1689 | } |
1690 | ||
e1d767f0 | 1691 | static inline void io_put_file(struct file *file) |
8da11c19 | 1692 | { |
e1d767f0 | 1693 | if (file) |
8da11c19 PB |
1694 | fput(file); |
1695 | } | |
1696 | ||
4edf20f9 | 1697 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1698 | { |
094bae49 PB |
1699 | unsigned int flags = req->flags; |
1700 | ||
e1d767f0 PB |
1701 | if (!(flags & REQ_F_FIXED_FILE)) |
1702 | io_put_file(req->file); | |
094bae49 PB |
1703 | if (flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
1704 | REQ_F_INFLIGHT)) { | |
1705 | io_clean_op(req); | |
1706 | ||
1707 | if (req->flags & REQ_F_INFLIGHT) { | |
b303fe2e | 1708 | struct io_uring_task *tctx = req->task->io_uring; |
094bae49 | 1709 | |
b303fe2e | 1710 | atomic_dec(&tctx->inflight_tracked); |
094bae49 PB |
1711 | req->flags &= ~REQ_F_INFLIGHT; |
1712 | } | |
1713 | } | |
269bbe5f BM |
1714 | if (req->fixed_rsrc_refs) |
1715 | percpu_ref_put(req->fixed_rsrc_refs); | |
094bae49 PB |
1716 | if (req->async_data) |
1717 | kfree(req->async_data); | |
003e8dcc JA |
1718 | if (req->work.creds) { |
1719 | put_cred(req->work.creds); | |
1720 | req->work.creds = NULL; | |
1721 | } | |
e65ef56d JA |
1722 | } |
1723 | ||
b23fcf47 | 1724 | /* must to be called somewhat shortly after putting a request */ |
7c660731 PB |
1725 | static inline void io_put_task(struct task_struct *task, int nr) |
1726 | { | |
1727 | struct io_uring_task *tctx = task->io_uring; | |
1728 | ||
1729 | percpu_counter_sub(&tctx->inflight, nr); | |
1730 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1731 | wake_up(&tctx->wait); | |
1732 | put_task_struct_many(task, nr); | |
1733 | } | |
1734 | ||
216578e5 | 1735 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1736 | { |
51a4cc11 | 1737 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1738 | |
216578e5 | 1739 | io_dismantle_req(req); |
7c660731 | 1740 | io_put_task(req->task, 1); |
c6ca97b3 | 1741 | |
3893f39f | 1742 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1743 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1744 | } |
1745 | ||
f2f87370 PB |
1746 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1747 | { | |
1748 | struct io_kiocb *nxt = req->link; | |
1749 | ||
1750 | req->link = nxt->link; | |
1751 | nxt->link = NULL; | |
1752 | } | |
1753 | ||
33cc89a9 PB |
1754 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1755 | __must_hold(&req->ctx->completion_lock) | |
2665abfd | 1756 | { |
33cc89a9 | 1757 | struct io_kiocb *link = req->link; |
f2f87370 | 1758 | |
900fad45 PB |
1759 | /* |
1760 | * Can happen if a linked timeout fired and link had been like | |
1761 | * req -> link t-out -> link t-out [-> ...] | |
1762 | */ | |
c9abd7ad PB |
1763 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1764 | struct io_timeout_data *io = link->async_data; | |
1765 | int ret; | |
7c86ffee | 1766 | |
f2f87370 | 1767 | io_remove_next_linked(req); |
90cd7e42 | 1768 | link->timeout.head = NULL; |
c9abd7ad PB |
1769 | ret = hrtimer_try_to_cancel(&io->timer); |
1770 | if (ret != -1) { | |
1771 | io_cqring_fill_event(link, -ECANCELED); | |
33cc89a9 | 1772 | io_put_req_deferred(link, 1); |
d4729fbd | 1773 | return true; |
c9abd7ad PB |
1774 | } |
1775 | } | |
d4729fbd | 1776 | return false; |
7c86ffee PB |
1777 | } |
1778 | ||
d148ca4b | 1779 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1780 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1781 | { |
33cc89a9 | 1782 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1783 | |
f2f87370 | 1784 | req->link = NULL; |
f2f87370 PB |
1785 | while (link) { |
1786 | nxt = link->link; | |
1787 | link->link = NULL; | |
2665abfd | 1788 | |
f2f87370 | 1789 | trace_io_uring_fail_link(req, link); |
7c86ffee | 1790 | io_cqring_fill_event(link, -ECANCELED); |
1575f21a | 1791 | io_put_req_deferred(link, 2); |
f2f87370 | 1792 | link = nxt; |
9e645e11 | 1793 | } |
33cc89a9 | 1794 | } |
9e645e11 | 1795 | |
33cc89a9 PB |
1796 | static bool io_disarm_next(struct io_kiocb *req) |
1797 | __must_hold(&req->ctx->completion_lock) | |
1798 | { | |
1799 | bool posted = false; | |
1800 | ||
1801 | if (likely(req->flags & REQ_F_LINK_TIMEOUT)) | |
1802 | posted = io_kill_linked_timeout(req); | |
1803 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { | |
1804 | posted |= (req->link != NULL); | |
1805 | io_fail_links(req); | |
1806 | } | |
1807 | return posted; | |
9e645e11 JA |
1808 | } |
1809 | ||
3fa5e0f3 | 1810 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1811 | { |
33cc89a9 | 1812 | struct io_kiocb *nxt; |
944e58bf | 1813 | |
9e645e11 JA |
1814 | /* |
1815 | * If LINK is set, we have dependent requests in this chain. If we | |
1816 | * didn't fail this request, queue the first one up, moving any other | |
1817 | * dependencies to the next request. In case of failure, fail the rest | |
1818 | * of the chain. | |
1819 | */ | |
33cc89a9 PB |
1820 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) { |
1821 | struct io_ring_ctx *ctx = req->ctx; | |
1822 | unsigned long flags; | |
1823 | bool posted; | |
1824 | ||
1825 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1826 | posted = io_disarm_next(req); | |
1827 | if (posted) | |
1828 | io_commit_cqring(req->ctx); | |
1829 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1830 | if (posted) | |
1831 | io_cqring_ev_posted(ctx); | |
f2f87370 | 1832 | } |
33cc89a9 PB |
1833 | nxt = req->link; |
1834 | req->link = NULL; | |
1835 | return nxt; | |
4d7dd462 | 1836 | } |
9e645e11 | 1837 | |
f2f87370 | 1838 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1839 | { |
cdbff982 | 1840 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1841 | return NULL; |
1842 | return __io_req_find_next(req); | |
1843 | } | |
1844 | ||
2c32395d PB |
1845 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
1846 | { | |
1847 | if (!ctx) | |
1848 | return; | |
1849 | if (ctx->submit_state.comp.nr) { | |
1850 | mutex_lock(&ctx->uring_lock); | |
1851 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); | |
1852 | mutex_unlock(&ctx->uring_lock); | |
1853 | } | |
1854 | percpu_ref_put(&ctx->refs); | |
1855 | } | |
1856 | ||
7cbf1722 | 1857 | static bool __tctx_task_work(struct io_uring_task *tctx) |
c2c4c83c | 1858 | { |
65453d1e | 1859 | struct io_ring_ctx *ctx = NULL; |
7cbf1722 JA |
1860 | struct io_wq_work_list list; |
1861 | struct io_wq_work_node *node; | |
c2c4c83c | 1862 | |
7cbf1722 JA |
1863 | if (wq_list_empty(&tctx->task_list)) |
1864 | return false; | |
6200b0ae | 1865 | |
0b81e80c | 1866 | spin_lock_irq(&tctx->task_lock); |
7cbf1722 JA |
1867 | list = tctx->task_list; |
1868 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 1869 | spin_unlock_irq(&tctx->task_lock); |
c2c4c83c | 1870 | |
7cbf1722 JA |
1871 | node = list.first; |
1872 | while (node) { | |
1873 | struct io_wq_work_node *next = node->next; | |
1874 | struct io_kiocb *req; | |
0ba9c9ed | 1875 | |
7cbf1722 | 1876 | req = container_of(node, struct io_kiocb, io_task_work.node); |
2c32395d PB |
1877 | if (req->ctx != ctx) { |
1878 | ctx_flush_and_put(ctx); | |
1879 | ctx = req->ctx; | |
1880 | percpu_ref_get(&ctx->refs); | |
65453d1e | 1881 | } |
65453d1e | 1882 | |
2c32395d PB |
1883 | req->task_work.func(&req->task_work); |
1884 | node = next; | |
7cbf1722 JA |
1885 | } |
1886 | ||
2c32395d | 1887 | ctx_flush_and_put(ctx); |
7cbf1722 | 1888 | return list.first != NULL; |
c2c4c83c JA |
1889 | } |
1890 | ||
7cbf1722 | 1891 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1892 | { |
7cbf1722 | 1893 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
c40f6379 | 1894 | |
1d5f360d JA |
1895 | clear_bit(0, &tctx->task_state); |
1896 | ||
7cbf1722 JA |
1897 | while (__tctx_task_work(tctx)) |
1898 | cond_resched(); | |
7cbf1722 JA |
1899 | } |
1900 | ||
c15b79de | 1901 | static int io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 1902 | { |
c15b79de | 1903 | struct task_struct *tsk = req->task; |
7cbf1722 | 1904 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 1905 | enum task_work_notify_mode notify; |
7cbf1722 | 1906 | struct io_wq_work_node *node, *prev; |
0b81e80c | 1907 | unsigned long flags; |
c15b79de PB |
1908 | int ret = 0; |
1909 | ||
1910 | if (unlikely(tsk->flags & PF_EXITING)) | |
1911 | return -ESRCH; | |
7cbf1722 JA |
1912 | |
1913 | WARN_ON_ONCE(!tctx); | |
1914 | ||
0b81e80c | 1915 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 1916 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 1917 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1918 | |
1919 | /* task_work already pending, we're done */ | |
1920 | if (test_bit(0, &tctx->task_state) || | |
1921 | test_and_set_bit(0, &tctx->task_state)) | |
1922 | return 0; | |
1923 | ||
c15b79de PB |
1924 | /* |
1925 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
1926 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
1927 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
1928 | * will do the job. | |
1929 | */ | |
1930 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
1931 | ||
1932 | if (!task_work_add(tsk, &tctx->task_work, notify)) { | |
1933 | wake_up_process(tsk); | |
7cbf1722 | 1934 | return 0; |
c15b79de | 1935 | } |
7cbf1722 JA |
1936 | |
1937 | /* | |
1938 | * Slow path - we failed, find and delete work. if the work is not | |
1939 | * in the list, it got run and we're fine. | |
1940 | */ | |
0b81e80c | 1941 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 JA |
1942 | wq_list_for_each(node, prev, &tctx->task_list) { |
1943 | if (&req->io_task_work.node == node) { | |
1944 | wq_list_del(&tctx->task_list, node, prev); | |
1945 | ret = 1; | |
1946 | break; | |
1947 | } | |
1948 | } | |
0b81e80c | 1949 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
1950 | clear_bit(0, &tctx->task_state); |
1951 | return ret; | |
1952 | } | |
1953 | ||
9b465711 PB |
1954 | static bool io_run_task_work_head(struct callback_head **work_head) |
1955 | { | |
1956 | struct callback_head *work, *next; | |
1957 | bool executed = false; | |
1958 | ||
1959 | do { | |
1960 | work = xchg(work_head, NULL); | |
1961 | if (!work) | |
1962 | break; | |
1963 | ||
1964 | do { | |
1965 | next = work->next; | |
1966 | work->func(work); | |
1967 | work = next; | |
1968 | cond_resched(); | |
1969 | } while (work); | |
1970 | executed = true; | |
1971 | } while (1); | |
1972 | ||
1973 | return executed; | |
1974 | } | |
1975 | ||
1976 | static void io_task_work_add_head(struct callback_head **work_head, | |
1977 | struct callback_head *task_work) | |
eab30c4d | 1978 | { |
7c25c0d1 | 1979 | struct callback_head *head; |
eab30c4d | 1980 | |
7c25c0d1 | 1981 | do { |
9b465711 PB |
1982 | head = READ_ONCE(*work_head); |
1983 | task_work->next = head; | |
1984 | } while (cmpxchg(work_head, head, task_work) != head); | |
1985 | } | |
1986 | ||
1987 | static void io_req_task_work_add_fallback(struct io_kiocb *req, | |
1988 | task_work_func_t cb) | |
1989 | { | |
1990 | init_task_work(&req->task_work, cb); | |
1991 | io_task_work_add_head(&req->ctx->exit_task_work, &req->task_work); | |
eab30c4d PB |
1992 | } |
1993 | ||
c40f6379 JA |
1994 | static void io_req_task_cancel(struct callback_head *cb) |
1995 | { | |
1996 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
87ceb6a6 | 1997 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 1998 | |
e83acd7d | 1999 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
792bb6eb | 2000 | mutex_lock(&ctx->uring_lock); |
2593553a | 2001 | io_req_complete_failed(req, req->result); |
792bb6eb | 2002 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2003 | } |
2004 | ||
2005 | static void __io_req_task_submit(struct io_kiocb *req) | |
2006 | { | |
2007 | struct io_ring_ctx *ctx = req->ctx; | |
2008 | ||
04fc6c80 | 2009 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2010 | mutex_lock(&ctx->uring_lock); |
70aacfe6 | 2011 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
c5eef2b9 | 2012 | __io_queue_sqe(req); |
81b6d05c | 2013 | else |
2593553a | 2014 | io_req_complete_failed(req, -EFAULT); |
81b6d05c | 2015 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2016 | } |
2017 | ||
2018 | static void io_req_task_submit(struct callback_head *cb) | |
2019 | { | |
2020 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2021 | ||
2022 | __io_req_task_submit(req); | |
2023 | } | |
2024 | ||
2c4b8eb6 | 2025 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2026 | { |
2c4b8eb6 PB |
2027 | req->result = ret; |
2028 | req->task_work.func = io_req_task_cancel; | |
c40f6379 | 2029 | |
2c4b8eb6 | 2030 | if (unlikely(io_req_task_work_add(req))) |
eab30c4d | 2031 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
c40f6379 JA |
2032 | } |
2033 | ||
2c4b8eb6 | 2034 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2035 | { |
2c4b8eb6 | 2036 | req->task_work.func = io_req_task_submit; |
a3df7698 PB |
2037 | |
2038 | if (unlikely(io_req_task_work_add(req))) | |
2c4b8eb6 | 2039 | io_req_task_queue_fail(req, -ECANCELED); |
a3df7698 PB |
2040 | } |
2041 | ||
f2f87370 | 2042 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2043 | { |
9b5f7bd9 | 2044 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2045 | |
2046 | if (nxt) | |
906a8c3f | 2047 | io_req_task_queue(nxt); |
c69f8dbe JL |
2048 | } |
2049 | ||
c3524383 | 2050 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2051 | { |
c3524383 PB |
2052 | io_queue_next(req); |
2053 | __io_free_req(req); | |
2054 | } | |
8766dd51 | 2055 | |
2d6500d4 | 2056 | struct req_batch { |
5af1d13e PB |
2057 | struct task_struct *task; |
2058 | int task_refs; | |
1b4c351f | 2059 | int ctx_refs; |
2d6500d4 PB |
2060 | }; |
2061 | ||
5af1d13e PB |
2062 | static inline void io_init_req_batch(struct req_batch *rb) |
2063 | { | |
5af1d13e | 2064 | rb->task_refs = 0; |
9ae72463 | 2065 | rb->ctx_refs = 0; |
5af1d13e PB |
2066 | rb->task = NULL; |
2067 | } | |
2068 | ||
2d6500d4 PB |
2069 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2070 | struct req_batch *rb) | |
2071 | { | |
6e833d53 | 2072 | if (rb->task) |
7c660731 | 2073 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2074 | if (rb->ctx_refs) |
2075 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2076 | } |
2077 | ||
6ff119a6 PB |
2078 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2079 | struct io_submit_state *state) | |
2d6500d4 | 2080 | { |
f2f87370 | 2081 | io_queue_next(req); |
96670657 | 2082 | io_dismantle_req(req); |
2d6500d4 | 2083 | |
e3bc8e9d | 2084 | if (req->task != rb->task) { |
7c660731 PB |
2085 | if (rb->task) |
2086 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2087 | rb->task = req->task; |
2088 | rb->task_refs = 0; | |
5af1d13e | 2089 | } |
e3bc8e9d | 2090 | rb->task_refs++; |
9ae72463 | 2091 | rb->ctx_refs++; |
5af1d13e | 2092 | |
bd759045 | 2093 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2094 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2095 | else |
2096 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2097 | } |
2098 | ||
905c172f PB |
2099 | static void io_submit_flush_completions(struct io_comp_state *cs, |
2100 | struct io_ring_ctx *ctx) | |
2101 | { | |
2102 | int i, nr = cs->nr; | |
2103 | struct io_kiocb *req; | |
2104 | struct req_batch rb; | |
2105 | ||
2106 | io_init_req_batch(&rb); | |
2107 | spin_lock_irq(&ctx->completion_lock); | |
2108 | for (i = 0; i < nr; i++) { | |
2109 | req = cs->reqs[i]; | |
2110 | __io_cqring_fill_event(req, req->result, req->compl.cflags); | |
2111 | } | |
2112 | io_commit_cqring(ctx); | |
2113 | spin_unlock_irq(&ctx->completion_lock); | |
2114 | ||
2115 | io_cqring_ev_posted(ctx); | |
2116 | for (i = 0; i < nr; i++) { | |
2117 | req = cs->reqs[i]; | |
2118 | ||
2119 | /* submission and completion refs */ | |
de9b4cca | 2120 | if (req_ref_sub_and_test(req, 2)) |
6ff119a6 | 2121 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2122 | } |
2123 | ||
2124 | io_req_free_batch_finish(ctx, &rb); | |
2125 | cs->nr = 0; | |
7a743e22 PB |
2126 | } |
2127 | ||
ba816ad6 JA |
2128 | /* |
2129 | * Drop reference to request, return next in chain (if there is one) if this | |
2130 | * was the last reference to this request. | |
2131 | */ | |
0d85035a | 2132 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2133 | { |
9b5f7bd9 PB |
2134 | struct io_kiocb *nxt = NULL; |
2135 | ||
de9b4cca | 2136 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2137 | nxt = io_req_find_next(req); |
4d7dd462 | 2138 | __io_free_req(req); |
2a44f467 | 2139 | } |
9b5f7bd9 | 2140 | return nxt; |
2b188cc1 JA |
2141 | } |
2142 | ||
0d85035a | 2143 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2144 | { |
de9b4cca | 2145 | if (req_ref_put_and_test(req)) |
e65ef56d | 2146 | io_free_req(req); |
2b188cc1 JA |
2147 | } |
2148 | ||
216578e5 PB |
2149 | static void io_put_req_deferred_cb(struct callback_head *cb) |
2150 | { | |
2151 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
2152 | ||
2153 | io_free_req(req); | |
2154 | } | |
2155 | ||
2156 | static void io_free_req_deferred(struct io_kiocb *req) | |
2157 | { | |
7cbf1722 | 2158 | req->task_work.func = io_put_req_deferred_cb; |
a05432fb | 2159 | if (unlikely(io_req_task_work_add(req))) |
eab30c4d | 2160 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
216578e5 PB |
2161 | } |
2162 | ||
2163 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2164 | { | |
de9b4cca | 2165 | if (req_ref_sub_and_test(req, refs)) |
216578e5 PB |
2166 | io_free_req_deferred(req); |
2167 | } | |
2168 | ||
6c503150 | 2169 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2170 | { |
2171 | /* See comment at the top of this file */ | |
2172 | smp_rmb(); | |
e23de15f | 2173 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2174 | } |
2175 | ||
fb5ccc98 PB |
2176 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2177 | { | |
2178 | struct io_rings *rings = ctx->rings; | |
2179 | ||
2180 | /* make sure SQ entry isn't read before tail */ | |
2181 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2182 | } | |
2183 | ||
8ff069bf | 2184 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2185 | { |
8ff069bf | 2186 | unsigned int cflags; |
e94f141b | 2187 | |
bcda7baa JA |
2188 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2189 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2190 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2191 | kfree(kbuf); |
2192 | return cflags; | |
e94f141b JA |
2193 | } |
2194 | ||
8ff069bf | 2195 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2196 | { |
4d954c25 | 2197 | struct io_buffer *kbuf; |
bcda7baa | 2198 | |
4d954c25 | 2199 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2200 | return io_put_kbuf(req, kbuf); |
2201 | } | |
2202 | ||
4c6e277c JA |
2203 | static inline bool io_run_task_work(void) |
2204 | { | |
6200b0ae JA |
2205 | /* |
2206 | * Not safe to run on exiting task, and the task_work handling will | |
2207 | * not add work to such a task. | |
2208 | */ | |
2209 | if (unlikely(current->flags & PF_EXITING)) | |
2210 | return false; | |
4c6e277c JA |
2211 | if (current->task_works) { |
2212 | __set_current_state(TASK_RUNNING); | |
2213 | task_work_run(); | |
2214 | return true; | |
2215 | } | |
2216 | ||
2217 | return false; | |
bcda7baa JA |
2218 | } |
2219 | ||
def596e9 JA |
2220 | /* |
2221 | * Find and free completed poll iocbs | |
2222 | */ | |
2223 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2224 | struct list_head *done) | |
2225 | { | |
8237e045 | 2226 | struct req_batch rb; |
def596e9 | 2227 | struct io_kiocb *req; |
bbde017a XW |
2228 | |
2229 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2230 | smp_rmb(); | |
def596e9 | 2231 | |
5af1d13e | 2232 | io_init_req_batch(&rb); |
def596e9 | 2233 | while (!list_empty(done)) { |
bcda7baa JA |
2234 | int cflags = 0; |
2235 | ||
d21ffe7e | 2236 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2237 | list_del(&req->inflight_entry); |
2238 | ||
8c130827 PB |
2239 | if (READ_ONCE(req->result) == -EAGAIN && |
2240 | !(req->flags & REQ_F_DONT_REISSUE)) { | |
bbde017a | 2241 | req->iopoll_completed = 0; |
8c130827 PB |
2242 | req_ref_get(req); |
2243 | io_queue_async_work(req); | |
2244 | continue; | |
bbde017a | 2245 | } |
def596e9 | 2246 | |
bcda7baa | 2247 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2248 | cflags = io_put_rw_kbuf(req); |
bcda7baa JA |
2249 | |
2250 | __io_cqring_fill_event(req, req->result, cflags); | |
def596e9 JA |
2251 | (*nr_events)++; |
2252 | ||
de9b4cca | 2253 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2254 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2255 | } |
def596e9 | 2256 | |
09bb8394 | 2257 | io_commit_cqring(ctx); |
80c18e4a | 2258 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2259 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2260 | } |
2261 | ||
def596e9 JA |
2262 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
2263 | long min) | |
2264 | { | |
2265 | struct io_kiocb *req, *tmp; | |
2266 | LIST_HEAD(done); | |
2267 | bool spin; | |
2268 | int ret; | |
2269 | ||
2270 | /* | |
2271 | * Only spin for completions if we don't have multiple devices hanging | |
2272 | * off our complete list, and we're under the requested amount. | |
2273 | */ | |
2274 | spin = !ctx->poll_multi_file && *nr_events < min; | |
2275 | ||
2276 | ret = 0; | |
d21ffe7e | 2277 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2278 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
2279 | |
2280 | /* | |
581f9810 BM |
2281 | * Move completed and retryable entries to our local lists. |
2282 | * If we find a request that requires polling, break out | |
2283 | * and complete those lists first, if we have entries there. | |
def596e9 | 2284 | */ |
65a6543d | 2285 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2286 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2287 | continue; |
2288 | } | |
2289 | if (!list_empty(&done)) | |
2290 | break; | |
2291 | ||
2292 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
2293 | if (ret < 0) | |
2294 | break; | |
2295 | ||
3aadc23e PB |
2296 | /* iopoll may have completed current req */ |
2297 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2298 | list_move_tail(&req->inflight_entry, &done); |
3aadc23e | 2299 | |
def596e9 JA |
2300 | if (ret && spin) |
2301 | spin = false; | |
2302 | ret = 0; | |
2303 | } | |
2304 | ||
2305 | if (!list_empty(&done)) | |
2306 | io_iopoll_complete(ctx, nr_events, &done); | |
2307 | ||
2308 | return ret; | |
2309 | } | |
2310 | ||
2311 | /* | |
d195a66e | 2312 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
2313 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
2314 | * as a non-spinning completion check. | |
2315 | */ | |
2316 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
2317 | long min) | |
2318 | { | |
540e32a0 | 2319 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
def596e9 JA |
2320 | int ret; |
2321 | ||
2322 | ret = io_do_iopoll(ctx, nr_events, min); | |
2323 | if (ret < 0) | |
2324 | return ret; | |
eba0a4dd | 2325 | if (*nr_events >= min) |
def596e9 JA |
2326 | return 0; |
2327 | } | |
2328 | ||
2329 | return 1; | |
2330 | } | |
2331 | ||
2332 | /* | |
2333 | * We can't just wait for polled events to come to us, we have to actively | |
2334 | * find and complete them. | |
2335 | */ | |
b2edc0a7 | 2336 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2337 | { |
2338 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2339 | return; | |
2340 | ||
2341 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2342 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2343 | unsigned int nr_events = 0; |
2344 | ||
b2edc0a7 | 2345 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2346 | |
b2edc0a7 PB |
2347 | /* let it sleep and repeat later if can't complete a request */ |
2348 | if (nr_events == 0) | |
2349 | break; | |
08f5439f JA |
2350 | /* |
2351 | * Ensure we allow local-to-the-cpu processing to take place, | |
2352 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2353 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2354 | */ |
3fcee5a6 PB |
2355 | if (need_resched()) { |
2356 | mutex_unlock(&ctx->uring_lock); | |
2357 | cond_resched(); | |
2358 | mutex_lock(&ctx->uring_lock); | |
2359 | } | |
def596e9 JA |
2360 | } |
2361 | mutex_unlock(&ctx->uring_lock); | |
2362 | } | |
2363 | ||
7668b92a | 2364 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2365 | { |
7668b92a | 2366 | unsigned int nr_events = 0; |
2b2ed975 | 2367 | int iters = 0, ret = 0; |
500f9fba | 2368 | |
c7849be9 XW |
2369 | /* |
2370 | * We disallow the app entering submit/complete with polling, but we | |
2371 | * still need to lock the ring to prevent racing with polled issue | |
2372 | * that got punted to a workqueue. | |
2373 | */ | |
2374 | mutex_lock(&ctx->uring_lock); | |
def596e9 | 2375 | do { |
a3a0e43f JA |
2376 | /* |
2377 | * Don't enter poll loop if we already have events pending. | |
2378 | * If we do, we can potentially be spinning for commands that | |
2379 | * already triggered a CQE (eg in error). | |
2380 | */ | |
6c503150 | 2381 | if (test_bit(0, &ctx->cq_check_overflow)) |
6c2450ae | 2382 | __io_cqring_overflow_flush(ctx, false); |
6c503150 | 2383 | if (io_cqring_events(ctx)) |
a3a0e43f JA |
2384 | break; |
2385 | ||
500f9fba JA |
2386 | /* |
2387 | * If a submit got punted to a workqueue, we can have the | |
2388 | * application entering polling for a command before it gets | |
2389 | * issued. That app will hold the uring_lock for the duration | |
2390 | * of the poll right here, so we need to take a breather every | |
2391 | * now and then to ensure that the issue has a chance to add | |
2392 | * the poll to the issued list. Otherwise we can spin here | |
2393 | * forever, while the workqueue is stuck trying to acquire the | |
2394 | * very same mutex. | |
2395 | */ | |
2396 | if (!(++iters & 7)) { | |
2397 | mutex_unlock(&ctx->uring_lock); | |
4c6e277c | 2398 | io_run_task_work(); |
500f9fba JA |
2399 | mutex_lock(&ctx->uring_lock); |
2400 | } | |
2401 | ||
7668b92a | 2402 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
def596e9 JA |
2403 | if (ret <= 0) |
2404 | break; | |
2405 | ret = 0; | |
7668b92a | 2406 | } while (min && !nr_events && !need_resched()); |
def596e9 | 2407 | |
500f9fba | 2408 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2409 | return ret; |
2410 | } | |
2411 | ||
491381ce | 2412 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2413 | { |
491381ce JA |
2414 | /* |
2415 | * Tell lockdep we inherited freeze protection from submission | |
2416 | * thread. | |
2417 | */ | |
2418 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2419 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2420 | |
1c98679d PB |
2421 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2422 | sb_end_write(sb); | |
2b188cc1 JA |
2423 | } |
2424 | } | |
2425 | ||
b63534c4 | 2426 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2427 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2428 | { |
ab454438 PB |
2429 | struct io_async_rw *rw = req->async_data; |
2430 | ||
2431 | if (!rw) | |
2432 | return !io_req_prep_async(req); | |
2433 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2434 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2435 | return true; | |
b63534c4 | 2436 | } |
b63534c4 | 2437 | |
3e6a0d3c | 2438 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2439 | { |
355afaeb | 2440 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2441 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2442 | |
355afaeb JA |
2443 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2444 | return false; | |
3e6a0d3c JA |
2445 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2446 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2447 | return false; |
7c977a58 JA |
2448 | /* |
2449 | * If ref is dying, we might be running poll reap from the exit work. | |
2450 | * Don't attempt to reissue from that path, just let it fail with | |
2451 | * -EAGAIN. | |
2452 | */ | |
3e6a0d3c JA |
2453 | if (percpu_ref_is_dying(&ctx->refs)) |
2454 | return false; | |
2455 | return true; | |
2456 | } | |
e82ad485 JA |
2457 | #else |
2458 | static bool io_rw_should_reissue(struct io_kiocb *req) | |
2459 | { | |
2460 | return false; | |
2461 | } | |
3e6a0d3c JA |
2462 | #endif |
2463 | ||
a1d7c393 | 2464 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2465 | unsigned int issue_flags) |
a1d7c393 | 2466 | { |
2f8e45f1 PB |
2467 | int cflags = 0; |
2468 | ||
b65c128f PB |
2469 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2470 | kiocb_end_write(req); | |
9532b99b PB |
2471 | if (res != req->result) { |
2472 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2473 | io_rw_should_reissue(req)) { | |
2474 | req->flags |= REQ_F_REISSUE; | |
2475 | return; | |
2476 | } | |
2f8e45f1 | 2477 | req_set_fail_links(req); |
9532b99b | 2478 | } |
2f8e45f1 PB |
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 | |
491381ce JA |
2495 | if (kiocb->ki_flags & IOCB_WRITE) |
2496 | kiocb_end_write(req); | |
9532b99b PB |
2497 | if (unlikely(res != req->result)) { |
2498 | bool fail = true; | |
def596e9 | 2499 | |
9532b99b PB |
2500 | #ifdef CONFIG_BLOCK |
2501 | if (res == -EAGAIN && io_rw_should_reissue(req) && | |
2502 | io_resubmit_prep(req)) | |
2503 | fail = false; | |
2504 | #endif | |
2505 | if (fail) { | |
2506 | req_set_fail_links(req); | |
2507 | req->flags |= REQ_F_DONT_REISSUE; | |
2508 | } | |
8c130827 | 2509 | } |
bbde017a XW |
2510 | |
2511 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2512 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2513 | smp_wmb(); |
2514 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2515 | } |
2516 | ||
2517 | /* | |
2518 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2519 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
2520 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
2521 | * accessing the kiocb cookie. | |
2522 | */ | |
2e9dbe90 | 2523 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
def596e9 JA |
2524 | { |
2525 | struct io_ring_ctx *ctx = req->ctx; | |
2526 | ||
2527 | /* | |
2528 | * Track whether we have multiple files in our lists. This will impact | |
2529 | * how we do polling eventually, not spinning if we're on potentially | |
2530 | * different devices. | |
2531 | */ | |
540e32a0 | 2532 | if (list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2533 | ctx->poll_multi_file = false; |
2534 | } else if (!ctx->poll_multi_file) { | |
2535 | struct io_kiocb *list_req; | |
2536 | ||
540e32a0 | 2537 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2538 | inflight_entry); |
9adbd45d | 2539 | if (list_req->file != req->file) |
def596e9 JA |
2540 | ctx->poll_multi_file = true; |
2541 | } | |
2542 | ||
2543 | /* | |
2544 | * For fast devices, IO may have already completed. If it has, add | |
2545 | * it to the front so we find it first. | |
2546 | */ | |
65a6543d | 2547 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2548 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2549 | else |
d21ffe7e | 2550 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2551 | |
2e9dbe90 XW |
2552 | /* |
2553 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread | |
2554 | * task context or in io worker task context. If current task context is | |
2555 | * sq thread, we don't need to check whether should wake up sq thread. | |
2556 | */ | |
2557 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && | |
534ca6d6 JA |
2558 | wq_has_sleeper(&ctx->sq_data->wait)) |
2559 | wake_up(&ctx->sq_data->wait); | |
def596e9 JA |
2560 | } |
2561 | ||
9f13c35b PB |
2562 | static inline void io_state_file_put(struct io_submit_state *state) |
2563 | { | |
02b23a9a PB |
2564 | if (state->file_refs) { |
2565 | fput_many(state->file, state->file_refs); | |
2566 | state->file_refs = 0; | |
2567 | } | |
9a56a232 JA |
2568 | } |
2569 | ||
2570 | /* | |
2571 | * Get as many references to a file as we have IOs left in this submission, | |
2572 | * assuming most submissions are for one file, or at least that each file | |
2573 | * has more than one submission. | |
2574 | */ | |
8da11c19 | 2575 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2576 | { |
2577 | if (!state) | |
2578 | return fget(fd); | |
2579 | ||
6e1271e6 | 2580 | if (state->file_refs) { |
9a56a232 | 2581 | if (state->fd == fd) { |
6e1271e6 | 2582 | state->file_refs--; |
9a56a232 JA |
2583 | return state->file; |
2584 | } | |
02b23a9a | 2585 | io_state_file_put(state); |
9a56a232 JA |
2586 | } |
2587 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2588 | if (unlikely(!state->file)) |
9a56a232 JA |
2589 | return NULL; |
2590 | ||
2591 | state->fd = fd; | |
6e1271e6 | 2592 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2593 | return state->file; |
2594 | } | |
2595 | ||
4503b767 JA |
2596 | static bool io_bdev_nowait(struct block_device *bdev) |
2597 | { | |
9ba0d0c8 | 2598 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2599 | } |
2600 | ||
2b188cc1 JA |
2601 | /* |
2602 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2603 | * any file. For now, just ensure that anything potentially problematic is done | |
2604 | * inline. | |
2605 | */ | |
7b29f92d | 2606 | static bool __io_file_supports_async(struct file *file, int rw) |
2b188cc1 JA |
2607 | { |
2608 | umode_t mode = file_inode(file)->i_mode; | |
2609 | ||
4503b767 | 2610 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2611 | if (IS_ENABLED(CONFIG_BLOCK) && |
2612 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2613 | return true; |
2614 | return false; | |
2615 | } | |
2616 | if (S_ISCHR(mode) || S_ISSOCK(mode)) | |
2b188cc1 | 2617 | return true; |
4503b767 | 2618 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2619 | if (IS_ENABLED(CONFIG_BLOCK) && |
2620 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2621 | file->f_op != &io_uring_fops) |
2622 | return true; | |
2623 | return false; | |
2624 | } | |
2b188cc1 | 2625 | |
c5b85625 JA |
2626 | /* any ->read/write should understand O_NONBLOCK */ |
2627 | if (file->f_flags & O_NONBLOCK) | |
2628 | return true; | |
2629 | ||
af197f50 JA |
2630 | if (!(file->f_mode & FMODE_NOWAIT)) |
2631 | return false; | |
2632 | ||
2633 | if (rw == READ) | |
2634 | return file->f_op->read_iter != NULL; | |
2635 | ||
2636 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2637 | } |
2638 | ||
7b29f92d JA |
2639 | static bool io_file_supports_async(struct io_kiocb *req, int rw) |
2640 | { | |
2641 | if (rw == READ && (req->flags & REQ_F_ASYNC_READ)) | |
2642 | return true; | |
2643 | else if (rw == WRITE && (req->flags & REQ_F_ASYNC_WRITE)) | |
2644 | return true; | |
2645 | ||
2646 | return __io_file_supports_async(req->file, rw); | |
2647 | } | |
2648 | ||
a88fc400 | 2649 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2650 | { |
def596e9 | 2651 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2652 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2653 | struct file *file = req->file; |
09bb8394 JA |
2654 | unsigned ioprio; |
2655 | int ret; | |
2b188cc1 | 2656 | |
7b29f92d | 2657 | if (!(req->flags & REQ_F_ISREG) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2658 | req->flags |= REQ_F_ISREG; |
2659 | ||
2b188cc1 | 2660 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2661 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2662 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2663 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2664 | } |
2b188cc1 | 2665 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2666 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2667 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2668 | if (unlikely(ret)) | |
2669 | return ret; | |
2b188cc1 | 2670 | |
75c668cd PB |
2671 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2672 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2673 | req->flags |= REQ_F_NOWAIT; | |
2674 | ||
2b188cc1 JA |
2675 | ioprio = READ_ONCE(sqe->ioprio); |
2676 | if (ioprio) { | |
2677 | ret = ioprio_check_cap(ioprio); | |
2678 | if (ret) | |
09bb8394 | 2679 | return ret; |
2b188cc1 JA |
2680 | |
2681 | kiocb->ki_ioprio = ioprio; | |
2682 | } else | |
2683 | kiocb->ki_ioprio = get_current_ioprio(); | |
2684 | ||
def596e9 | 2685 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2686 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2687 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2688 | return -EOPNOTSUPP; |
2b188cc1 | 2689 | |
def596e9 JA |
2690 | kiocb->ki_flags |= IOCB_HIPRI; |
2691 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2692 | req->iopoll_completed = 0; |
def596e9 | 2693 | } else { |
09bb8394 JA |
2694 | if (kiocb->ki_flags & IOCB_HIPRI) |
2695 | return -EINVAL; | |
def596e9 JA |
2696 | kiocb->ki_complete = io_complete_rw; |
2697 | } | |
9adbd45d | 2698 | |
3529d8c2 JA |
2699 | req->rw.addr = READ_ONCE(sqe->addr); |
2700 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2701 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2702 | return 0; |
2b188cc1 JA |
2703 | } |
2704 | ||
2705 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2706 | { | |
2707 | switch (ret) { | |
2708 | case -EIOCBQUEUED: | |
2709 | break; | |
2710 | case -ERESTARTSYS: | |
2711 | case -ERESTARTNOINTR: | |
2712 | case -ERESTARTNOHAND: | |
2713 | case -ERESTART_RESTARTBLOCK: | |
2714 | /* | |
2715 | * We can't just restart the syscall, since previously | |
2716 | * submitted sqes may already be in progress. Just fail this | |
2717 | * IO with EINTR. | |
2718 | */ | |
2719 | ret = -EINTR; | |
df561f66 | 2720 | fallthrough; |
2b188cc1 JA |
2721 | default: |
2722 | kiocb->ki_complete(kiocb, ret, 0); | |
2723 | } | |
2724 | } | |
2725 | ||
a1d7c393 | 2726 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2727 | unsigned int issue_flags) |
ba816ad6 | 2728 | { |
ba04291e | 2729 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2730 | struct io_async_rw *io = req->async_data; |
97284637 | 2731 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2732 | |
227c0c96 | 2733 | /* add previously done IO, if any */ |
e8c2bc1f | 2734 | if (io && io->bytes_done > 0) { |
227c0c96 | 2735 | if (ret < 0) |
e8c2bc1f | 2736 | ret = io->bytes_done; |
227c0c96 | 2737 | else |
e8c2bc1f | 2738 | ret += io->bytes_done; |
227c0c96 JA |
2739 | } |
2740 | ||
ba04291e JA |
2741 | if (req->flags & REQ_F_CUR_POS) |
2742 | req->file->f_pos = kiocb->ki_pos; | |
bcaec089 | 2743 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
889fca73 | 2744 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2745 | else |
2746 | io_rw_done(kiocb, ret); | |
97284637 PB |
2747 | |
2748 | if (check_reissue && req->flags & REQ_F_REISSUE) { | |
2749 | req->flags &= ~REQ_F_REISSUE; | |
8c130827 PB |
2750 | if (!io_resubmit_prep(req)) { |
2751 | req_ref_get(req); | |
2752 | io_queue_async_work(req); | |
2753 | } else { | |
97284637 PB |
2754 | int cflags = 0; |
2755 | ||
2756 | req_set_fail_links(req); | |
2757 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2758 | cflags = io_put_rw_kbuf(req); | |
2759 | __io_req_complete(req, issue_flags, ret, cflags); | |
2760 | } | |
2761 | } | |
ba816ad6 JA |
2762 | } |
2763 | ||
847595de | 2764 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
edafccee | 2765 | { |
9adbd45d JA |
2766 | struct io_ring_ctx *ctx = req->ctx; |
2767 | size_t len = req->rw.len; | |
edafccee | 2768 | struct io_mapped_ubuf *imu; |
4be1c615 | 2769 | u16 index, buf_index = req->buf_index; |
75769e3f | 2770 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2771 | size_t offset; |
edafccee | 2772 | |
edafccee JA |
2773 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
2774 | return -EFAULT; | |
edafccee JA |
2775 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
2776 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 2777 | buf_addr = req->rw.addr; |
edafccee | 2778 | |
75769e3f | 2779 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2780 | return -EFAULT; |
2781 | /* not inside the mapped region */ | |
4751f53d | 2782 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2783 | return -EFAULT; |
2784 | ||
2785 | /* | |
2786 | * May not be a start of buffer, set size appropriately | |
2787 | * and advance us to the beginning. | |
2788 | */ | |
2789 | offset = buf_addr - imu->ubuf; | |
2790 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2791 | |
2792 | if (offset) { | |
2793 | /* | |
2794 | * Don't use iov_iter_advance() here, as it's really slow for | |
2795 | * using the latter parts of a big fixed buffer - it iterates | |
2796 | * over each segment manually. We can cheat a bit here, because | |
2797 | * we know that: | |
2798 | * | |
2799 | * 1) it's a BVEC iter, we set it up | |
2800 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2801 | * first and last bvec | |
2802 | * | |
2803 | * So just find our index, and adjust the iterator afterwards. | |
2804 | * If the offset is within the first bvec (or the whole first | |
2805 | * bvec, just use iov_iter_advance(). This makes it easier | |
2806 | * since we can just skip the first segment, which may not | |
2807 | * be PAGE_SIZE aligned. | |
2808 | */ | |
2809 | const struct bio_vec *bvec = imu->bvec; | |
2810 | ||
2811 | if (offset <= bvec->bv_len) { | |
2812 | iov_iter_advance(iter, offset); | |
2813 | } else { | |
2814 | unsigned long seg_skip; | |
2815 | ||
2816 | /* skip first vec */ | |
2817 | offset -= bvec->bv_len; | |
2818 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2819 | ||
2820 | iter->bvec = bvec + seg_skip; | |
2821 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2822 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2823 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2824 | } |
2825 | } | |
2826 | ||
847595de | 2827 | return 0; |
edafccee JA |
2828 | } |
2829 | ||
bcda7baa JA |
2830 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2831 | { | |
2832 | if (needs_lock) | |
2833 | mutex_unlock(&ctx->uring_lock); | |
2834 | } | |
2835 | ||
2836 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2837 | { | |
2838 | /* | |
2839 | * "Normal" inline submissions always hold the uring_lock, since we | |
2840 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2841 | * The only exception is when we've detached the request and issue it | |
2842 | * from an async worker thread, grab the lock for that case. | |
2843 | */ | |
2844 | if (needs_lock) | |
2845 | mutex_lock(&ctx->uring_lock); | |
2846 | } | |
2847 | ||
2848 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2849 | int bgid, struct io_buffer *kbuf, | |
2850 | bool needs_lock) | |
2851 | { | |
2852 | struct io_buffer *head; | |
2853 | ||
2854 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2855 | return kbuf; | |
2856 | ||
2857 | io_ring_submit_lock(req->ctx, needs_lock); | |
2858 | ||
2859 | lockdep_assert_held(&req->ctx->uring_lock); | |
2860 | ||
9e15c3a0 | 2861 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2862 | if (head) { |
2863 | if (!list_empty(&head->list)) { | |
2864 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2865 | list); | |
2866 | list_del(&kbuf->list); | |
2867 | } else { | |
2868 | kbuf = head; | |
9e15c3a0 | 2869 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2870 | } |
2871 | if (*len > kbuf->len) | |
2872 | *len = kbuf->len; | |
2873 | } else { | |
2874 | kbuf = ERR_PTR(-ENOBUFS); | |
2875 | } | |
2876 | ||
2877 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2878 | ||
2879 | return kbuf; | |
2880 | } | |
2881 | ||
4d954c25 JA |
2882 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2883 | bool needs_lock) | |
2884 | { | |
2885 | struct io_buffer *kbuf; | |
4f4eeba8 | 2886 | u16 bgid; |
4d954c25 JA |
2887 | |
2888 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2889 | bgid = req->buf_index; |
4d954c25 JA |
2890 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2891 | if (IS_ERR(kbuf)) | |
2892 | return kbuf; | |
2893 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2894 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2895 | return u64_to_user_ptr(kbuf->addr); | |
2896 | } | |
2897 | ||
2898 | #ifdef CONFIG_COMPAT | |
2899 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2900 | bool needs_lock) | |
2901 | { | |
2902 | struct compat_iovec __user *uiov; | |
2903 | compat_ssize_t clen; | |
2904 | void __user *buf; | |
2905 | ssize_t len; | |
2906 | ||
2907 | uiov = u64_to_user_ptr(req->rw.addr); | |
2908 | if (!access_ok(uiov, sizeof(*uiov))) | |
2909 | return -EFAULT; | |
2910 | if (__get_user(clen, &uiov->iov_len)) | |
2911 | return -EFAULT; | |
2912 | if (clen < 0) | |
2913 | return -EINVAL; | |
2914 | ||
2915 | len = clen; | |
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 = (compat_size_t) len; | |
2921 | return 0; | |
2922 | } | |
2923 | #endif | |
2924 | ||
2925 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2926 | bool needs_lock) | |
2927 | { | |
2928 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2929 | void __user *buf; | |
2930 | ssize_t len; | |
2931 | ||
2932 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2933 | return -EFAULT; | |
2934 | ||
2935 | len = iov[0].iov_len; | |
2936 | if (len < 0) | |
2937 | return -EINVAL; | |
2938 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2939 | if (IS_ERR(buf)) | |
2940 | return PTR_ERR(buf); | |
2941 | iov[0].iov_base = buf; | |
2942 | iov[0].iov_len = len; | |
2943 | return 0; | |
2944 | } | |
2945 | ||
2946 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2947 | bool needs_lock) | |
2948 | { | |
dddb3e26 JA |
2949 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
2950 | struct io_buffer *kbuf; | |
2951 | ||
2952 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
2953 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
2954 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 2955 | return 0; |
dddb3e26 | 2956 | } |
dd201662 | 2957 | if (req->rw.len != 1) |
4d954c25 JA |
2958 | return -EINVAL; |
2959 | ||
2960 | #ifdef CONFIG_COMPAT | |
2961 | if (req->ctx->compat) | |
2962 | return io_compat_import(req, iov, needs_lock); | |
2963 | #endif | |
2964 | ||
2965 | return __io_iov_buffer_select(req, iov, needs_lock); | |
2966 | } | |
2967 | ||
847595de PB |
2968 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
2969 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 2970 | { |
9adbd45d JA |
2971 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2972 | size_t sqe_len = req->rw.len; | |
847595de | 2973 | u8 opcode = req->opcode; |
4d954c25 | 2974 | ssize_t ret; |
edafccee | 2975 | |
7d009165 | 2976 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2977 | *iovec = NULL; |
9adbd45d | 2978 | return io_import_fixed(req, rw, iter); |
edafccee | 2979 | } |
2b188cc1 | 2980 | |
bcda7baa | 2981 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 2982 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
2983 | return -EINVAL; |
2984 | ||
3a6820f2 | 2985 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 2986 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 2987 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 2988 | if (IS_ERR(buf)) |
4d954c25 | 2989 | return PTR_ERR(buf); |
3f9d6441 | 2990 | req->rw.len = sqe_len; |
bcda7baa JA |
2991 | } |
2992 | ||
3a6820f2 JA |
2993 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
2994 | *iovec = NULL; | |
10fc72e4 | 2995 | return ret; |
3a6820f2 JA |
2996 | } |
2997 | ||
4d954c25 JA |
2998 | if (req->flags & REQ_F_BUFFER_SELECT) { |
2999 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3000 | if (!ret) |
3001 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3002 | *iovec = NULL; |
3003 | return ret; | |
3004 | } | |
3005 | ||
89cd35c5 CH |
3006 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3007 | req->ctx->compat); | |
2b188cc1 JA |
3008 | } |
3009 | ||
0fef9483 JA |
3010 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3011 | { | |
5b09e37e | 3012 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3013 | } |
3014 | ||
31b51510 | 3015 | /* |
32960613 JA |
3016 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3017 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3018 | */ |
4017eb91 | 3019 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3020 | { |
4017eb91 JA |
3021 | struct kiocb *kiocb = &req->rw.kiocb; |
3022 | struct file *file = req->file; | |
32960613 JA |
3023 | ssize_t ret = 0; |
3024 | ||
3025 | /* | |
3026 | * Don't support polled IO through this interface, and we can't | |
3027 | * support non-blocking either. For the latter, this just causes | |
3028 | * the kiocb to be handled from an async context. | |
3029 | */ | |
3030 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3031 | return -EOPNOTSUPP; | |
3032 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3033 | return -EAGAIN; | |
3034 | ||
3035 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3036 | struct iovec iovec; |
32960613 JA |
3037 | ssize_t nr; |
3038 | ||
311ae9e1 PB |
3039 | if (!iov_iter_is_bvec(iter)) { |
3040 | iovec = iov_iter_iovec(iter); | |
3041 | } else { | |
4017eb91 JA |
3042 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3043 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3044 | } |
3045 | ||
32960613 JA |
3046 | if (rw == READ) { |
3047 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3048 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3049 | } else { |
3050 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3051 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3052 | } |
3053 | ||
3054 | if (nr < 0) { | |
3055 | if (!ret) | |
3056 | ret = nr; | |
3057 | break; | |
3058 | } | |
3059 | ret += nr; | |
3060 | if (nr != iovec.iov_len) | |
3061 | break; | |
4017eb91 JA |
3062 | req->rw.len -= nr; |
3063 | req->rw.addr += nr; | |
32960613 JA |
3064 | iov_iter_advance(iter, nr); |
3065 | } | |
3066 | ||
3067 | return ret; | |
3068 | } | |
3069 | ||
ff6165b2 JA |
3070 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3071 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3072 | { |
e8c2bc1f | 3073 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3074 | |
ff6165b2 | 3075 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3076 | rw->free_iovec = iovec; |
227c0c96 | 3077 | rw->bytes_done = 0; |
ff6165b2 | 3078 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3079 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3080 | return; |
b64e3444 | 3081 | if (!iovec) { |
ff6165b2 JA |
3082 | unsigned iov_off = 0; |
3083 | ||
3084 | rw->iter.iov = rw->fast_iov; | |
3085 | if (iter->iov != fast_iov) { | |
3086 | iov_off = iter->iov - fast_iov; | |
3087 | rw->iter.iov += iov_off; | |
3088 | } | |
3089 | if (rw->fast_iov != fast_iov) | |
3090 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3091 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3092 | } else { |
3093 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3094 | } |
3095 | } | |
3096 | ||
6cb78689 | 3097 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3098 | { |
e8c2bc1f JA |
3099 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3100 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3101 | return req->async_data == NULL; | |
3d9932a8 XW |
3102 | } |
3103 | ||
ff6165b2 JA |
3104 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3105 | const struct iovec *fast_iov, | |
227c0c96 | 3106 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3107 | { |
26f0505a | 3108 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3109 | return 0; |
e8c2bc1f | 3110 | if (!req->async_data) { |
6cb78689 | 3111 | if (io_alloc_async_data(req)) { |
6bf985dc | 3112 | kfree(iovec); |
5d204bcf | 3113 | return -ENOMEM; |
6bf985dc | 3114 | } |
b7bb4f7d | 3115 | |
ff6165b2 | 3116 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3117 | } |
b7bb4f7d | 3118 | return 0; |
f67676d1 JA |
3119 | } |
3120 | ||
73debe68 | 3121 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3122 | { |
e8c2bc1f | 3123 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3124 | struct iovec *iov = iorw->fast_iov; |
847595de | 3125 | int ret; |
c3e330a4 | 3126 | |
2846c481 | 3127 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3128 | if (unlikely(ret < 0)) |
3129 | return ret; | |
3130 | ||
ab0b196c PB |
3131 | iorw->bytes_done = 0; |
3132 | iorw->free_iovec = iov; | |
3133 | if (iov) | |
3134 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3135 | return 0; |
3136 | } | |
3137 | ||
73debe68 | 3138 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3139 | { |
3529d8c2 JA |
3140 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3141 | return -EBADF; | |
93642ef8 | 3142 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3143 | } |
3144 | ||
c1dd91d1 JA |
3145 | /* |
3146 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3147 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3148 | * This gets called when the page is unlocked, and we generally expect that to | |
3149 | * happen when the page IO is completed and the page is now uptodate. This will | |
3150 | * queue a task_work based retry of the operation, attempting to copy the data | |
3151 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3152 | * do a thread based blocking retry of the operation. That's the unexpected | |
3153 | * slow path. | |
3154 | */ | |
bcf5a063 JA |
3155 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3156 | int sync, void *arg) | |
3157 | { | |
3158 | struct wait_page_queue *wpq; | |
3159 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3160 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3161 | |
3162 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3163 | ||
cdc8fcb4 LT |
3164 | if (!wake_page_match(wpq, key)) |
3165 | return 0; | |
3166 | ||
c8d317aa | 3167 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3168 | list_del_init(&wait->entry); |
3169 | ||
bcf5a063 | 3170 | /* submit ref gets dropped, acquire a new one */ |
de9b4cca | 3171 | req_ref_get(req); |
921b9054 | 3172 | io_req_task_queue(req); |
bcf5a063 JA |
3173 | return 1; |
3174 | } | |
3175 | ||
c1dd91d1 JA |
3176 | /* |
3177 | * This controls whether a given IO request should be armed for async page | |
3178 | * based retry. If we return false here, the request is handed to the async | |
3179 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3180 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3181 | * will either succeed because the page is now uptodate and unlocked, or it | |
3182 | * will register a callback when the page is unlocked at IO completion. Through | |
3183 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3184 | * That retry will attempt the buffered read again. The retry will generally | |
3185 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3186 | * async worker threads for a blocking retry. | |
3187 | */ | |
227c0c96 | 3188 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3189 | { |
e8c2bc1f JA |
3190 | struct io_async_rw *rw = req->async_data; |
3191 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3192 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3193 | |
bcf5a063 JA |
3194 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3195 | if (req->flags & REQ_F_NOWAIT) | |
3196 | return false; | |
f67676d1 | 3197 | |
227c0c96 | 3198 | /* Only for buffered IO */ |
3b2a4439 | 3199 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3200 | return false; |
3b2a4439 | 3201 | |
bcf5a063 JA |
3202 | /* |
3203 | * just use poll if we can, and don't attempt if the fs doesn't | |
3204 | * support callback based unlocks | |
3205 | */ | |
3206 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3207 | return false; | |
f67676d1 | 3208 | |
3b2a4439 JA |
3209 | wait->wait.func = io_async_buf_func; |
3210 | wait->wait.private = req; | |
3211 | wait->wait.flags = 0; | |
3212 | INIT_LIST_HEAD(&wait->wait.entry); | |
3213 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3214 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3215 | kiocb->ki_waitq = wait; |
3b2a4439 | 3216 | return true; |
bcf5a063 JA |
3217 | } |
3218 | ||
3219 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) | |
3220 | { | |
3221 | if (req->file->f_op->read_iter) | |
3222 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3223 | else if (req->file->f_op->read) |
4017eb91 | 3224 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3225 | else |
3226 | return -EINVAL; | |
f67676d1 JA |
3227 | } |
3228 | ||
889fca73 | 3229 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3230 | { |
3231 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3232 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3233 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3234 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3235 | ssize_t io_size, ret, ret2; |
45d189c6 | 3236 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3237 | |
2846c481 | 3238 | if (rw) { |
e8c2bc1f | 3239 | iter = &rw->iter; |
2846c481 PB |
3240 | iovec = NULL; |
3241 | } else { | |
3242 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3243 | if (ret < 0) | |
3244 | return ret; | |
3245 | } | |
632546c4 | 3246 | io_size = iov_iter_count(iter); |
fa15bafb | 3247 | req->result = io_size; |
2b188cc1 | 3248 | |
fd6c2e4c JA |
3249 | /* Ensure we clear previously set non-block flag */ |
3250 | if (!force_nonblock) | |
29de5f6a | 3251 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3252 | else |
3253 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3254 | ||
24c74678 | 3255 | /* If the file doesn't support async, just async punt */ |
7b29f92d | 3256 | if (force_nonblock && !io_file_supports_async(req, READ)) { |
6713e7a6 | 3257 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3258 | return ret ?: -EAGAIN; |
6713e7a6 | 3259 | } |
9e645e11 | 3260 | |
632546c4 | 3261 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3262 | if (unlikely(ret)) { |
3263 | kfree(iovec); | |
3264 | return ret; | |
3265 | } | |
2b188cc1 | 3266 | |
227c0c96 | 3267 | ret = io_iter_do_read(req, iter); |
32960613 | 3268 | |
230d50d4 | 3269 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3270 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3271 | /* IOPOLL retry should happen for io-wq threads */ |
3272 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3273 | goto done; |
75c668cd PB |
3274 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3275 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3276 | goto done; |
84216315 | 3277 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3278 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3279 | ret = 0; |
230d50d4 JA |
3280 | } else if (ret == -EIOCBQUEUED) { |
3281 | goto out_free; | |
7335e3bf | 3282 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3283 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3284 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3285 | goto done; |
227c0c96 JA |
3286 | } |
3287 | ||
227c0c96 | 3288 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3289 | if (ret2) |
3290 | return ret2; | |
3291 | ||
fe1cdd55 | 3292 | iovec = NULL; |
e8c2bc1f | 3293 | rw = req->async_data; |
227c0c96 | 3294 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3295 | iter = &rw->iter; |
227c0c96 | 3296 | |
b23df91b PB |
3297 | do { |
3298 | io_size -= ret; | |
3299 | rw->bytes_done += ret; | |
3300 | /* if we can retry, do so with the callbacks armed */ | |
3301 | if (!io_rw_should_retry(req)) { | |
3302 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3303 | return -EAGAIN; | |
3304 | } | |
3305 | ||
3306 | /* | |
3307 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3308 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3309 | * desired page gets unlocked. We can also get a partial read | |
3310 | * here, and if we do, then just retry at the new offset. | |
3311 | */ | |
3312 | ret = io_iter_do_read(req, iter); | |
3313 | if (ret == -EIOCBQUEUED) | |
3314 | return 0; | |
227c0c96 | 3315 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3316 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3317 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3318 | done: |
889fca73 | 3319 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3320 | out_free: |
3321 | /* it's faster to check here then delegate to kfree */ | |
3322 | if (iovec) | |
3323 | kfree(iovec); | |
5ea5dd45 | 3324 | return 0; |
2b188cc1 JA |
3325 | } |
3326 | ||
73debe68 | 3327 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3328 | { |
3529d8c2 JA |
3329 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3330 | return -EBADF; | |
93642ef8 | 3331 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3332 | } |
3333 | ||
889fca73 | 3334 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3335 | { |
3336 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3337 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3338 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3339 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3340 | ssize_t ret, ret2, io_size; |
45d189c6 | 3341 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3342 | |
2846c481 | 3343 | if (rw) { |
e8c2bc1f | 3344 | iter = &rw->iter; |
2846c481 PB |
3345 | iovec = NULL; |
3346 | } else { | |
3347 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3348 | if (ret < 0) | |
3349 | return ret; | |
3350 | } | |
632546c4 | 3351 | io_size = iov_iter_count(iter); |
fa15bafb | 3352 | req->result = io_size; |
2b188cc1 | 3353 | |
fd6c2e4c JA |
3354 | /* Ensure we clear previously set non-block flag */ |
3355 | if (!force_nonblock) | |
a88fc400 PB |
3356 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3357 | else | |
3358 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3359 | |
24c74678 | 3360 | /* If the file doesn't support async, just async punt */ |
7b29f92d | 3361 | if (force_nonblock && !io_file_supports_async(req, WRITE)) |
f67676d1 | 3362 | goto copy_iov; |
31b51510 | 3363 | |
10d59345 JA |
3364 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3365 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3366 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3367 | goto copy_iov; |
31b51510 | 3368 | |
632546c4 | 3369 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3370 | if (unlikely(ret)) |
3371 | goto out_free; | |
4ed734b0 | 3372 | |
fa15bafb PB |
3373 | /* |
3374 | * Open-code file_start_write here to grab freeze protection, | |
3375 | * which will be released by another thread in | |
3376 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3377 | * released so that it doesn't complain about the held lock when | |
3378 | * we return to userspace. | |
3379 | */ | |
3380 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3381 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3382 | __sb_writers_release(file_inode(req->file)->i_sb, |
3383 | SB_FREEZE_WRITE); | |
3384 | } | |
3385 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3386 | |
fa15bafb | 3387 | if (req->file->f_op->write_iter) |
ff6165b2 | 3388 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3389 | else if (req->file->f_op->write) |
4017eb91 | 3390 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3391 | else |
3392 | ret2 = -EINVAL; | |
4ed734b0 | 3393 | |
6ad7f233 PB |
3394 | if (req->flags & REQ_F_REISSUE) { |
3395 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3396 | ret2 = -EAGAIN; |
6ad7f233 | 3397 | } |
230d50d4 | 3398 | |
fa15bafb PB |
3399 | /* |
3400 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3401 | * retry them without IOCB_NOWAIT. | |
3402 | */ | |
3403 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3404 | ret2 = -EAGAIN; | |
75c668cd PB |
3405 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3406 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3407 | goto done; |
fa15bafb | 3408 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3409 | /* IOPOLL retry should happen for io-wq threads */ |
3410 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3411 | goto copy_iov; | |
355afaeb | 3412 | done: |
889fca73 | 3413 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3414 | } else { |
f67676d1 | 3415 | copy_iov: |
84216315 | 3416 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3417 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3418 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3419 | return ret ?: -EAGAIN; |
2b188cc1 | 3420 | } |
31b51510 | 3421 | out_free: |
f261c168 | 3422 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3423 | if (iovec) |
6f2cc166 | 3424 | kfree(iovec); |
2b188cc1 JA |
3425 | return ret; |
3426 | } | |
3427 | ||
80a261fd JA |
3428 | static int io_renameat_prep(struct io_kiocb *req, |
3429 | const struct io_uring_sqe *sqe) | |
3430 | { | |
3431 | struct io_rename *ren = &req->rename; | |
3432 | const char __user *oldf, *newf; | |
3433 | ||
3434 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3435 | return -EBADF; | |
3436 | ||
3437 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3438 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3439 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3440 | ren->new_dfd = READ_ONCE(sqe->len); | |
3441 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3442 | ||
3443 | ren->oldpath = getname(oldf); | |
3444 | if (IS_ERR(ren->oldpath)) | |
3445 | return PTR_ERR(ren->oldpath); | |
3446 | ||
3447 | ren->newpath = getname(newf); | |
3448 | if (IS_ERR(ren->newpath)) { | |
3449 | putname(ren->oldpath); | |
3450 | return PTR_ERR(ren->newpath); | |
3451 | } | |
3452 | ||
3453 | req->flags |= REQ_F_NEED_CLEANUP; | |
3454 | return 0; | |
3455 | } | |
3456 | ||
45d189c6 | 3457 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3458 | { |
3459 | struct io_rename *ren = &req->rename; | |
3460 | int ret; | |
3461 | ||
45d189c6 | 3462 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3463 | return -EAGAIN; |
3464 | ||
3465 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3466 | ren->newpath, ren->flags); | |
3467 | ||
3468 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3469 | if (ret < 0) | |
3470 | req_set_fail_links(req); | |
3471 | io_req_complete(req, ret); | |
3472 | return 0; | |
3473 | } | |
3474 | ||
14a1143b JA |
3475 | static int io_unlinkat_prep(struct io_kiocb *req, |
3476 | const struct io_uring_sqe *sqe) | |
3477 | { | |
3478 | struct io_unlink *un = &req->unlink; | |
3479 | const char __user *fname; | |
3480 | ||
3481 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
3482 | return -EBADF; | |
3483 | ||
3484 | un->dfd = READ_ONCE(sqe->fd); | |
3485 | ||
3486 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3487 | if (un->flags & ~AT_REMOVEDIR) | |
3488 | return -EINVAL; | |
3489 | ||
3490 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3491 | un->filename = getname(fname); | |
3492 | if (IS_ERR(un->filename)) | |
3493 | return PTR_ERR(un->filename); | |
3494 | ||
3495 | req->flags |= REQ_F_NEED_CLEANUP; | |
3496 | return 0; | |
3497 | } | |
3498 | ||
45d189c6 | 3499 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3500 | { |
3501 | struct io_unlink *un = &req->unlink; | |
3502 | int ret; | |
3503 | ||
45d189c6 | 3504 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3505 | return -EAGAIN; |
3506 | ||
3507 | if (un->flags & AT_REMOVEDIR) | |
3508 | ret = do_rmdir(un->dfd, un->filename); | |
3509 | else | |
3510 | ret = do_unlinkat(un->dfd, un->filename); | |
3511 | ||
3512 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3513 | if (ret < 0) | |
3514 | req_set_fail_links(req); | |
3515 | io_req_complete(req, ret); | |
3516 | return 0; | |
3517 | } | |
3518 | ||
36f4fa68 JA |
3519 | static int io_shutdown_prep(struct io_kiocb *req, |
3520 | const struct io_uring_sqe *sqe) | |
3521 | { | |
3522 | #if defined(CONFIG_NET) | |
3523 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3524 | return -EINVAL; | |
3525 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3526 | sqe->buf_index) | |
3527 | return -EINVAL; | |
3528 | ||
3529 | req->shutdown.how = READ_ONCE(sqe->len); | |
3530 | return 0; | |
3531 | #else | |
3532 | return -EOPNOTSUPP; | |
3533 | #endif | |
3534 | } | |
3535 | ||
45d189c6 | 3536 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3537 | { |
3538 | #if defined(CONFIG_NET) | |
3539 | struct socket *sock; | |
3540 | int ret; | |
3541 | ||
45d189c6 | 3542 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3543 | return -EAGAIN; |
3544 | ||
48aba79b | 3545 | sock = sock_from_file(req->file); |
36f4fa68 | 3546 | if (unlikely(!sock)) |
48aba79b | 3547 | return -ENOTSOCK; |
36f4fa68 JA |
3548 | |
3549 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d JA |
3550 | if (ret < 0) |
3551 | req_set_fail_links(req); | |
36f4fa68 JA |
3552 | io_req_complete(req, ret); |
3553 | return 0; | |
3554 | #else | |
3555 | return -EOPNOTSUPP; | |
3556 | #endif | |
3557 | } | |
3558 | ||
f2a8d5c7 PB |
3559 | static int __io_splice_prep(struct io_kiocb *req, |
3560 | const struct io_uring_sqe *sqe) | |
7d67af2c PB |
3561 | { |
3562 | struct io_splice* sp = &req->splice; | |
3563 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; | |
7d67af2c | 3564 | |
3232dd02 PB |
3565 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3566 | return -EINVAL; | |
7d67af2c PB |
3567 | |
3568 | sp->file_in = NULL; | |
7d67af2c PB |
3569 | sp->len = READ_ONCE(sqe->len); |
3570 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3571 | ||
3572 | if (unlikely(sp->flags & ~valid_flags)) | |
3573 | return -EINVAL; | |
3574 | ||
8371adf5 PB |
3575 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
3576 | (sp->flags & SPLICE_F_FD_IN_FIXED)); | |
3577 | if (!sp->file_in) | |
3578 | return -EBADF; | |
7d67af2c | 3579 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3580 | return 0; |
3581 | } | |
3582 | ||
f2a8d5c7 PB |
3583 | static int io_tee_prep(struct io_kiocb *req, |
3584 | const struct io_uring_sqe *sqe) | |
3585 | { | |
3586 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3587 | return -EINVAL; | |
3588 | return __io_splice_prep(req, sqe); | |
3589 | } | |
3590 | ||
45d189c6 | 3591 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3592 | { |
3593 | struct io_splice *sp = &req->splice; | |
3594 | struct file *in = sp->file_in; | |
3595 | struct file *out = sp->file_out; | |
3596 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3597 | long ret = 0; | |
3598 | ||
45d189c6 | 3599 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3600 | return -EAGAIN; |
3601 | if (sp->len) | |
3602 | ret = do_tee(in, out, sp->len, flags); | |
3603 | ||
e1d767f0 PB |
3604 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3605 | io_put_file(in); | |
f2a8d5c7 PB |
3606 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3607 | ||
f2a8d5c7 PB |
3608 | if (ret != sp->len) |
3609 | req_set_fail_links(req); | |
e1e16097 | 3610 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3611 | return 0; |
3612 | } | |
3613 | ||
3614 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3615 | { | |
3616 | struct io_splice* sp = &req->splice; | |
3617 | ||
3618 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3619 | sp->off_out = READ_ONCE(sqe->off); | |
3620 | return __io_splice_prep(req, sqe); | |
3621 | } | |
3622 | ||
45d189c6 | 3623 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3624 | { |
3625 | struct io_splice *sp = &req->splice; | |
3626 | struct file *in = sp->file_in; | |
3627 | struct file *out = sp->file_out; | |
3628 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3629 | loff_t *poff_in, *poff_out; | |
c9687426 | 3630 | long ret = 0; |
7d67af2c | 3631 | |
45d189c6 | 3632 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3633 | return -EAGAIN; |
7d67af2c PB |
3634 | |
3635 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3636 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3637 | |
948a7749 | 3638 | if (sp->len) |
c9687426 | 3639 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3640 | |
e1d767f0 PB |
3641 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3642 | io_put_file(in); | |
7d67af2c PB |
3643 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3644 | ||
7d67af2c PB |
3645 | if (ret != sp->len) |
3646 | req_set_fail_links(req); | |
e1e16097 | 3647 | io_req_complete(req, ret); |
7d67af2c PB |
3648 | return 0; |
3649 | } | |
3650 | ||
2b188cc1 JA |
3651 | /* |
3652 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3653 | */ | |
889fca73 | 3654 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3655 | { |
3656 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3657 | |
def596e9 JA |
3658 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3659 | return -EINVAL; | |
3660 | ||
889fca73 | 3661 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3662 | return 0; |
3663 | } | |
3664 | ||
1155c76a | 3665 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3666 | { |
6b06314c | 3667 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3668 | |
09bb8394 JA |
3669 | if (!req->file) |
3670 | return -EBADF; | |
c992fe29 | 3671 | |
6b06314c | 3672 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3673 | return -EINVAL; |
edafccee | 3674 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3675 | return -EINVAL; |
3676 | ||
8ed8d3c3 JA |
3677 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3678 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3679 | return -EINVAL; | |
3680 | ||
3681 | req->sync.off = READ_ONCE(sqe->off); | |
3682 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3683 | return 0; |
3684 | } | |
3685 | ||
45d189c6 | 3686 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3687 | { |
8ed8d3c3 | 3688 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3689 | int ret; |
3690 | ||
ac45abc0 | 3691 | /* fsync always requires a blocking context */ |
45d189c6 | 3692 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3693 | return -EAGAIN; |
3694 | ||
9adbd45d | 3695 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3696 | end > 0 ? end : LLONG_MAX, |
3697 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3698 | if (ret < 0) | |
3699 | req_set_fail_links(req); | |
e1e16097 | 3700 | io_req_complete(req, ret); |
c992fe29 CH |
3701 | return 0; |
3702 | } | |
3703 | ||
d63d1b5e JA |
3704 | static int io_fallocate_prep(struct io_kiocb *req, |
3705 | const struct io_uring_sqe *sqe) | |
3706 | { | |
3707 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3708 | return -EINVAL; | |
3232dd02 PB |
3709 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3710 | return -EINVAL; | |
d63d1b5e JA |
3711 | |
3712 | req->sync.off = READ_ONCE(sqe->off); | |
3713 | req->sync.len = READ_ONCE(sqe->addr); | |
3714 | req->sync.mode = READ_ONCE(sqe->len); | |
3715 | return 0; | |
3716 | } | |
3717 | ||
45d189c6 | 3718 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3719 | { |
ac45abc0 PB |
3720 | int ret; |
3721 | ||
d63d1b5e | 3722 | /* fallocate always requiring blocking context */ |
45d189c6 | 3723 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3724 | return -EAGAIN; |
ac45abc0 PB |
3725 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3726 | req->sync.len); | |
ac45abc0 PB |
3727 | if (ret < 0) |
3728 | req_set_fail_links(req); | |
e1e16097 | 3729 | io_req_complete(req, ret); |
5d17b4a4 JA |
3730 | return 0; |
3731 | } | |
3732 | ||
ec65fea5 | 3733 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3734 | { |
f8748881 | 3735 | const char __user *fname; |
15b71abe | 3736 | int ret; |
b7bb4f7d | 3737 | |
ec65fea5 | 3738 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3739 | return -EINVAL; |
ec65fea5 | 3740 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3741 | return -EBADF; |
03b1230c | 3742 | |
ec65fea5 PB |
3743 | /* open.how should be already initialised */ |
3744 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3745 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3746 | |
25e72d10 PB |
3747 | req->open.dfd = READ_ONCE(sqe->fd); |
3748 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3749 | req->open.filename = getname(fname); |
15b71abe JA |
3750 | if (IS_ERR(req->open.filename)) { |
3751 | ret = PTR_ERR(req->open.filename); | |
3752 | req->open.filename = NULL; | |
3753 | return ret; | |
3754 | } | |
4022e7af | 3755 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3756 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3757 | return 0; |
03b1230c JA |
3758 | } |
3759 | ||
ec65fea5 PB |
3760 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3761 | { | |
3762 | u64 flags, mode; | |
3763 | ||
14587a46 | 3764 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3765 | return -EINVAL; |
ec65fea5 PB |
3766 | mode = READ_ONCE(sqe->len); |
3767 | flags = READ_ONCE(sqe->open_flags); | |
3768 | req->open.how = build_open_how(flags, mode); | |
3769 | return __io_openat_prep(req, sqe); | |
3770 | } | |
3771 | ||
cebdb986 | 3772 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3773 | { |
cebdb986 | 3774 | struct open_how __user *how; |
cebdb986 | 3775 | size_t len; |
0fa03c62 JA |
3776 | int ret; |
3777 | ||
14587a46 | 3778 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3779 | return -EINVAL; |
cebdb986 JA |
3780 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3781 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3782 | if (len < OPEN_HOW_SIZE_VER0) |
3783 | return -EINVAL; | |
3529d8c2 | 3784 | |
cebdb986 JA |
3785 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3786 | len); | |
3787 | if (ret) | |
3788 | return ret; | |
3529d8c2 | 3789 | |
ec65fea5 | 3790 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3791 | } |
3792 | ||
45d189c6 | 3793 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3794 | { |
3795 | struct open_flags op; | |
15b71abe | 3796 | struct file *file; |
3a81fd02 JA |
3797 | bool nonblock_set; |
3798 | bool resolve_nonblock; | |
15b71abe JA |
3799 | int ret; |
3800 | ||
cebdb986 | 3801 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3802 | if (ret) |
3803 | goto err; | |
3a81fd02 JA |
3804 | nonblock_set = op.open_flag & O_NONBLOCK; |
3805 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3806 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3807 | /* |
3808 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3809 | * it'll always -EAGAIN | |
3810 | */ | |
3811 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3812 | return -EAGAIN; | |
3813 | op.lookup_flags |= LOOKUP_CACHED; | |
3814 | op.open_flag |= O_NONBLOCK; | |
3815 | } | |
15b71abe | 3816 | |
4022e7af | 3817 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3818 | if (ret < 0) |
3819 | goto err; | |
3820 | ||
3821 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
3a81fd02 | 3822 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
45d189c6 PB |
3823 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
3824 | file == ERR_PTR(-EAGAIN)) { | |
944d1444 | 3825 | /* |
3a81fd02 JA |
3826 | * We could hang on to this 'fd', but seems like marginal |
3827 | * gain for something that is now known to be a slower path. | |
3828 | * So just put it, and we'll get a new one when we retry. | |
944d1444 | 3829 | */ |
3a81fd02 JA |
3830 | put_unused_fd(ret); |
3831 | return -EAGAIN; | |
3832 | } | |
3833 | ||
15b71abe JA |
3834 | if (IS_ERR(file)) { |
3835 | put_unused_fd(ret); | |
3836 | ret = PTR_ERR(file); | |
3837 | } else { | |
45d189c6 | 3838 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
3a81fd02 | 3839 | file->f_flags &= ~O_NONBLOCK; |
15b71abe JA |
3840 | fsnotify_open(file); |
3841 | fd_install(ret, file); | |
3842 | } | |
3843 | err: | |
3844 | putname(req->open.filename); | |
8fef80bf | 3845 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe JA |
3846 | if (ret < 0) |
3847 | req_set_fail_links(req); | |
e1e16097 | 3848 | io_req_complete(req, ret); |
15b71abe JA |
3849 | return 0; |
3850 | } | |
3851 | ||
45d189c6 | 3852 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3853 | { |
e45cff58 | 3854 | return io_openat2(req, issue_flags); |
cebdb986 JA |
3855 | } |
3856 | ||
067524e9 JA |
3857 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3858 | const struct io_uring_sqe *sqe) | |
3859 | { | |
3860 | struct io_provide_buf *p = &req->pbuf; | |
3861 | u64 tmp; | |
3862 | ||
3863 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3864 | return -EINVAL; | |
3865 | ||
3866 | tmp = READ_ONCE(sqe->fd); | |
3867 | if (!tmp || tmp > USHRT_MAX) | |
3868 | return -EINVAL; | |
3869 | ||
3870 | memset(p, 0, sizeof(*p)); | |
3871 | p->nbufs = tmp; | |
3872 | p->bgid = READ_ONCE(sqe->buf_group); | |
3873 | return 0; | |
3874 | } | |
3875 | ||
3876 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3877 | int bgid, unsigned nbufs) | |
3878 | { | |
3879 | unsigned i = 0; | |
3880 | ||
3881 | /* shouldn't happen */ | |
3882 | if (!nbufs) | |
3883 | return 0; | |
3884 | ||
3885 | /* the head kbuf is the list itself */ | |
3886 | while (!list_empty(&buf->list)) { | |
3887 | struct io_buffer *nxt; | |
3888 | ||
3889 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3890 | list_del(&nxt->list); | |
3891 | kfree(nxt); | |
3892 | if (++i == nbufs) | |
3893 | return i; | |
3894 | } | |
3895 | i++; | |
3896 | kfree(buf); | |
9e15c3a0 | 3897 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
3898 | |
3899 | return i; | |
3900 | } | |
3901 | ||
889fca73 | 3902 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3903 | { |
3904 | struct io_provide_buf *p = &req->pbuf; | |
3905 | struct io_ring_ctx *ctx = req->ctx; | |
3906 | struct io_buffer *head; | |
3907 | int ret = 0; | |
45d189c6 | 3908 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3909 | |
3910 | io_ring_submit_lock(ctx, !force_nonblock); | |
3911 | ||
3912 | lockdep_assert_held(&ctx->uring_lock); | |
3913 | ||
3914 | ret = -ENOENT; | |
9e15c3a0 | 3915 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
3916 | if (head) |
3917 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 JA |
3918 | if (ret < 0) |
3919 | req_set_fail_links(req); | |
067524e9 | 3920 | |
9fb8cb49 PB |
3921 | /* complete before unlock, IOPOLL may need the lock */ |
3922 | __io_req_complete(req, issue_flags, ret, 0); | |
3923 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
3924 | return 0; |
3925 | } | |
3926 | ||
ddf0322d JA |
3927 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3928 | const struct io_uring_sqe *sqe) | |
3929 | { | |
d81269fe | 3930 | unsigned long size; |
ddf0322d JA |
3931 | struct io_provide_buf *p = &req->pbuf; |
3932 | u64 tmp; | |
3933 | ||
3934 | if (sqe->ioprio || sqe->rw_flags) | |
3935 | return -EINVAL; | |
3936 | ||
3937 | tmp = READ_ONCE(sqe->fd); | |
3938 | if (!tmp || tmp > USHRT_MAX) | |
3939 | return -E2BIG; | |
3940 | p->nbufs = tmp; | |
3941 | p->addr = READ_ONCE(sqe->addr); | |
3942 | p->len = READ_ONCE(sqe->len); | |
3943 | ||
d81269fe PB |
3944 | size = (unsigned long)p->len * p->nbufs; |
3945 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
3946 | return -EFAULT; |
3947 | ||
3948 | p->bgid = READ_ONCE(sqe->buf_group); | |
3949 | tmp = READ_ONCE(sqe->off); | |
3950 | if (tmp > USHRT_MAX) | |
3951 | return -E2BIG; | |
3952 | p->bid = tmp; | |
3953 | return 0; | |
3954 | } | |
3955 | ||
3956 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
3957 | { | |
3958 | struct io_buffer *buf; | |
3959 | u64 addr = pbuf->addr; | |
3960 | int i, bid = pbuf->bid; | |
3961 | ||
3962 | for (i = 0; i < pbuf->nbufs; i++) { | |
3963 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
3964 | if (!buf) | |
3965 | break; | |
3966 | ||
3967 | buf->addr = addr; | |
3968 | buf->len = pbuf->len; | |
3969 | buf->bid = bid; | |
3970 | addr += pbuf->len; | |
3971 | bid++; | |
3972 | if (!*head) { | |
3973 | INIT_LIST_HEAD(&buf->list); | |
3974 | *head = buf; | |
3975 | } else { | |
3976 | list_add_tail(&buf->list, &(*head)->list); | |
3977 | } | |
3978 | } | |
3979 | ||
3980 | return i ? i : -ENOMEM; | |
3981 | } | |
3982 | ||
889fca73 | 3983 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
3984 | { |
3985 | struct io_provide_buf *p = &req->pbuf; | |
3986 | struct io_ring_ctx *ctx = req->ctx; | |
3987 | struct io_buffer *head, *list; | |
3988 | int ret = 0; | |
45d189c6 | 3989 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
3990 | |
3991 | io_ring_submit_lock(ctx, !force_nonblock); | |
3992 | ||
3993 | lockdep_assert_held(&ctx->uring_lock); | |
3994 | ||
9e15c3a0 | 3995 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
3996 | |
3997 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
3998 | if (ret >= 0 && !list) { |
3999 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4000 | if (ret < 0) | |
067524e9 | 4001 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4002 | } |
ddf0322d JA |
4003 | if (ret < 0) |
4004 | req_set_fail_links(req); | |
9fb8cb49 PB |
4005 | /* complete before unlock, IOPOLL may need the lock */ |
4006 | __io_req_complete(req, issue_flags, ret, 0); | |
4007 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4008 | return 0; |
cebdb986 JA |
4009 | } |
4010 | ||
3e4827b0 JA |
4011 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4012 | const struct io_uring_sqe *sqe) | |
4013 | { | |
4014 | #if defined(CONFIG_EPOLL) | |
4015 | if (sqe->ioprio || sqe->buf_index) | |
4016 | return -EINVAL; | |
6ca56f84 | 4017 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4018 | return -EINVAL; |
3e4827b0 JA |
4019 | |
4020 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4021 | req->epoll.op = READ_ONCE(sqe->len); | |
4022 | req->epoll.fd = READ_ONCE(sqe->off); | |
4023 | ||
4024 | if (ep_op_has_event(req->epoll.op)) { | |
4025 | struct epoll_event __user *ev; | |
4026 | ||
4027 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4028 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4029 | return -EFAULT; | |
4030 | } | |
4031 | ||
4032 | return 0; | |
4033 | #else | |
4034 | return -EOPNOTSUPP; | |
4035 | #endif | |
4036 | } | |
4037 | ||
889fca73 | 4038 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4039 | { |
4040 | #if defined(CONFIG_EPOLL) | |
4041 | struct io_epoll *ie = &req->epoll; | |
4042 | int ret; | |
45d189c6 | 4043 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4044 | |
4045 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4046 | if (force_nonblock && ret == -EAGAIN) | |
4047 | return -EAGAIN; | |
4048 | ||
4049 | if (ret < 0) | |
4050 | req_set_fail_links(req); | |
889fca73 | 4051 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4052 | return 0; |
4053 | #else | |
4054 | return -EOPNOTSUPP; | |
4055 | #endif | |
4056 | } | |
4057 | ||
c1ca757b JA |
4058 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4059 | { | |
4060 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4061 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4062 | return -EINVAL; | |
3232dd02 PB |
4063 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4064 | return -EINVAL; | |
c1ca757b JA |
4065 | |
4066 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4067 | req->madvise.len = READ_ONCE(sqe->len); | |
4068 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4069 | return 0; | |
4070 | #else | |
4071 | return -EOPNOTSUPP; | |
4072 | #endif | |
4073 | } | |
4074 | ||
45d189c6 | 4075 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4076 | { |
4077 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4078 | struct io_madvise *ma = &req->madvise; | |
4079 | int ret; | |
4080 | ||
45d189c6 | 4081 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4082 | return -EAGAIN; |
4083 | ||
0726b01e | 4084 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b JA |
4085 | if (ret < 0) |
4086 | req_set_fail_links(req); | |
e1e16097 | 4087 | io_req_complete(req, ret); |
c1ca757b JA |
4088 | return 0; |
4089 | #else | |
4090 | return -EOPNOTSUPP; | |
4091 | #endif | |
4092 | } | |
4093 | ||
4840e418 JA |
4094 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4095 | { | |
4096 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4097 | return -EINVAL; | |
3232dd02 PB |
4098 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4099 | return -EINVAL; | |
4840e418 JA |
4100 | |
4101 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4102 | req->fadvise.len = READ_ONCE(sqe->len); | |
4103 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4104 | return 0; | |
4105 | } | |
4106 | ||
45d189c6 | 4107 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4108 | { |
4109 | struct io_fadvise *fa = &req->fadvise; | |
4110 | int ret; | |
4111 | ||
45d189c6 | 4112 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4113 | switch (fa->advice) { |
4114 | case POSIX_FADV_NORMAL: | |
4115 | case POSIX_FADV_RANDOM: | |
4116 | case POSIX_FADV_SEQUENTIAL: | |
4117 | break; | |
4118 | default: | |
4119 | return -EAGAIN; | |
4120 | } | |
4121 | } | |
4840e418 JA |
4122 | |
4123 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4124 | if (ret < 0) | |
4125 | req_set_fail_links(req); | |
e1e16097 | 4126 | io_req_complete(req, ret); |
4840e418 JA |
4127 | return 0; |
4128 | } | |
4129 | ||
eddc7ef5 JA |
4130 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4131 | { | |
6ca56f84 | 4132 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
3232dd02 | 4133 | return -EINVAL; |
eddc7ef5 JA |
4134 | if (sqe->ioprio || sqe->buf_index) |
4135 | return -EINVAL; | |
9c280f90 | 4136 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4137 | return -EBADF; |
eddc7ef5 | 4138 | |
1d9e1288 BM |
4139 | req->statx.dfd = READ_ONCE(sqe->fd); |
4140 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4141 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4142 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4143 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4144 | |
4145 | return 0; | |
4146 | } | |
4147 | ||
45d189c6 | 4148 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4149 | { |
1d9e1288 | 4150 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4151 | int ret; |
4152 | ||
59d70013 | 4153 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4154 | return -EAGAIN; |
4155 | ||
e62753e4 BM |
4156 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4157 | ctx->buffer); | |
eddc7ef5 | 4158 | |
eddc7ef5 JA |
4159 | if (ret < 0) |
4160 | req_set_fail_links(req); | |
e1e16097 | 4161 | io_req_complete(req, ret); |
eddc7ef5 JA |
4162 | return 0; |
4163 | } | |
4164 | ||
b5dba59e JA |
4165 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4166 | { | |
14587a46 | 4167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4168 | return -EINVAL; |
b5dba59e JA |
4169 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4170 | sqe->rw_flags || sqe->buf_index) | |
4171 | return -EINVAL; | |
9c280f90 | 4172 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4173 | return -EBADF; |
b5dba59e JA |
4174 | |
4175 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4176 | return 0; |
b5dba59e JA |
4177 | } |
4178 | ||
889fca73 | 4179 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4180 | { |
9eac1904 | 4181 | struct files_struct *files = current->files; |
3af73b28 | 4182 | struct io_close *close = &req->close; |
9eac1904 JA |
4183 | struct fdtable *fdt; |
4184 | struct file *file; | |
b5dba59e JA |
4185 | int ret; |
4186 | ||
9eac1904 JA |
4187 | file = NULL; |
4188 | ret = -EBADF; | |
4189 | spin_lock(&files->file_lock); | |
4190 | fdt = files_fdtable(files); | |
4191 | if (close->fd >= fdt->max_fds) { | |
4192 | spin_unlock(&files->file_lock); | |
4193 | goto err; | |
4194 | } | |
4195 | file = fdt->fd[close->fd]; | |
4196 | if (!file) { | |
4197 | spin_unlock(&files->file_lock); | |
4198 | goto err; | |
4199 | } | |
4200 | ||
4201 | if (file->f_op == &io_uring_fops) { | |
4202 | spin_unlock(&files->file_lock); | |
4203 | file = NULL; | |
4204 | goto err; | |
3af73b28 | 4205 | } |
b5dba59e JA |
4206 | |
4207 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4208 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4209 | spin_unlock(&files->file_lock); |
0bf0eefd | 4210 | return -EAGAIN; |
a2100672 | 4211 | } |
b5dba59e | 4212 | |
9eac1904 JA |
4213 | ret = __close_fd_get_file(close->fd, &file); |
4214 | spin_unlock(&files->file_lock); | |
4215 | if (ret < 0) { | |
4216 | if (ret == -ENOENT) | |
4217 | ret = -EBADF; | |
4218 | goto err; | |
4219 | } | |
4220 | ||
3af73b28 | 4221 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4222 | ret = filp_close(file, current->files); |
4223 | err: | |
3af73b28 PB |
4224 | if (ret < 0) |
4225 | req_set_fail_links(req); | |
9eac1904 JA |
4226 | if (file) |
4227 | fput(file); | |
889fca73 | 4228 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4229 | return 0; |
b5dba59e JA |
4230 | } |
4231 | ||
1155c76a | 4232 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4233 | { |
4234 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4235 | |
5d17b4a4 JA |
4236 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4237 | return -EINVAL; | |
4238 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4239 | return -EINVAL; | |
4240 | ||
8ed8d3c3 JA |
4241 | req->sync.off = READ_ONCE(sqe->off); |
4242 | req->sync.len = READ_ONCE(sqe->len); | |
4243 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4244 | return 0; |
4245 | } | |
4246 | ||
45d189c6 | 4247 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4248 | { |
8ed8d3c3 JA |
4249 | int ret; |
4250 | ||
ac45abc0 | 4251 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4252 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4253 | return -EAGAIN; |
4254 | ||
9adbd45d | 4255 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4256 | req->sync.flags); |
4257 | if (ret < 0) | |
4258 | req_set_fail_links(req); | |
e1e16097 | 4259 | io_req_complete(req, ret); |
5d17b4a4 JA |
4260 | return 0; |
4261 | } | |
4262 | ||
469956e8 | 4263 | #if defined(CONFIG_NET) |
02d27d89 PB |
4264 | static int io_setup_async_msg(struct io_kiocb *req, |
4265 | struct io_async_msghdr *kmsg) | |
4266 | { | |
e8c2bc1f JA |
4267 | struct io_async_msghdr *async_msg = req->async_data; |
4268 | ||
4269 | if (async_msg) | |
02d27d89 | 4270 | return -EAGAIN; |
e8c2bc1f | 4271 | if (io_alloc_async_data(req)) { |
257e84a5 | 4272 | kfree(kmsg->free_iov); |
02d27d89 PB |
4273 | return -ENOMEM; |
4274 | } | |
e8c2bc1f | 4275 | async_msg = req->async_data; |
02d27d89 | 4276 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4277 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4278 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4279 | /* if were using fast_iov, set it to the new one */ |
4280 | if (!async_msg->free_iov) | |
4281 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4282 | ||
02d27d89 PB |
4283 | return -EAGAIN; |
4284 | } | |
4285 | ||
2ae523ed PB |
4286 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4287 | struct io_async_msghdr *iomsg) | |
4288 | { | |
2ae523ed | 4289 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4290 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4291 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4292 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4293 | } |
4294 | ||
93642ef8 PB |
4295 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4296 | { | |
4297 | int ret; | |
4298 | ||
93642ef8 PB |
4299 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4300 | if (!ret) | |
4301 | req->flags |= REQ_F_NEED_CLEANUP; | |
4302 | return ret; | |
4303 | } | |
4304 | ||
3529d8c2 | 4305 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4306 | { |
e47293fd | 4307 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4308 | |
d2b6f48b PB |
4309 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4310 | return -EINVAL; | |
4311 | ||
270a5940 | 4312 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4313 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4314 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4315 | if (sr->msg_flags & MSG_DONTWAIT) | |
4316 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4317 | |
d8768362 JA |
4318 | #ifdef CONFIG_COMPAT |
4319 | if (req->ctx->compat) | |
4320 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4321 | #endif | |
93642ef8 | 4322 | return 0; |
03b1230c JA |
4323 | } |
4324 | ||
889fca73 | 4325 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4326 | { |
6b754c8b | 4327 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4328 | struct socket *sock; |
7a7cacba | 4329 | unsigned flags; |
0031275d | 4330 | int min_ret = 0; |
0fa03c62 JA |
4331 | int ret; |
4332 | ||
dba4a925 | 4333 | sock = sock_from_file(req->file); |
7a7cacba | 4334 | if (unlikely(!sock)) |
dba4a925 | 4335 | return -ENOTSOCK; |
3529d8c2 | 4336 | |
257e84a5 PB |
4337 | kmsg = req->async_data; |
4338 | if (!kmsg) { | |
7a7cacba PB |
4339 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4340 | if (ret) | |
4341 | return ret; | |
4342 | kmsg = &iomsg; | |
0fa03c62 | 4343 | } |
0fa03c62 | 4344 | |
04411806 PB |
4345 | flags = req->sr_msg.msg_flags; |
4346 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4347 | flags |= MSG_DONTWAIT; |
0031275d SM |
4348 | if (flags & MSG_WAITALL) |
4349 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4350 | ||
7a7cacba | 4351 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4352 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4353 | return io_setup_async_msg(req, kmsg); |
4354 | if (ret == -ERESTARTSYS) | |
4355 | ret = -EINTR; | |
0fa03c62 | 4356 | |
257e84a5 PB |
4357 | /* fast path, check for non-NULL to avoid function call */ |
4358 | if (kmsg->free_iov) | |
4359 | kfree(kmsg->free_iov); | |
99bc4c38 | 4360 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4361 | if (ret < min_ret) |
4e88d6e7 | 4362 | req_set_fail_links(req); |
889fca73 | 4363 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4364 | return 0; |
03b1230c | 4365 | } |
aa1fa28f | 4366 | |
889fca73 | 4367 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4368 | { |
7a7cacba PB |
4369 | struct io_sr_msg *sr = &req->sr_msg; |
4370 | struct msghdr msg; | |
4371 | struct iovec iov; | |
fddaface | 4372 | struct socket *sock; |
7a7cacba | 4373 | unsigned flags; |
0031275d | 4374 | int min_ret = 0; |
fddaface JA |
4375 | int ret; |
4376 | ||
dba4a925 | 4377 | sock = sock_from_file(req->file); |
7a7cacba | 4378 | if (unlikely(!sock)) |
dba4a925 | 4379 | return -ENOTSOCK; |
fddaface | 4380 | |
7a7cacba PB |
4381 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4382 | if (unlikely(ret)) | |
14db8411 | 4383 | return ret; |
fddaface | 4384 | |
7a7cacba PB |
4385 | msg.msg_name = NULL; |
4386 | msg.msg_control = NULL; | |
4387 | msg.msg_controllen = 0; | |
4388 | msg.msg_namelen = 0; | |
fddaface | 4389 | |
04411806 PB |
4390 | flags = req->sr_msg.msg_flags; |
4391 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4392 | flags |= MSG_DONTWAIT; |
0031275d SM |
4393 | if (flags & MSG_WAITALL) |
4394 | min_ret = iov_iter_count(&msg.msg_iter); | |
4395 | ||
7a7cacba PB |
4396 | msg.msg_flags = flags; |
4397 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4398 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4399 | return -EAGAIN; |
4400 | if (ret == -ERESTARTSYS) | |
4401 | ret = -EINTR; | |
fddaface | 4402 | |
0031275d | 4403 | if (ret < min_ret) |
fddaface | 4404 | req_set_fail_links(req); |
889fca73 | 4405 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4406 | return 0; |
fddaface JA |
4407 | } |
4408 | ||
1400e697 PB |
4409 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4410 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4411 | { |
4412 | struct io_sr_msg *sr = &req->sr_msg; | |
4413 | struct iovec __user *uiov; | |
4414 | size_t iov_len; | |
4415 | int ret; | |
4416 | ||
1400e697 PB |
4417 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4418 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4419 | if (ret) |
4420 | return ret; | |
4421 | ||
4422 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4423 | if (iov_len > 1) | |
4424 | return -EINVAL; | |
5476dfed | 4425 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4426 | return -EFAULT; |
5476dfed | 4427 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4428 | iomsg->free_iov = NULL; |
52de1fe1 | 4429 | } else { |
257e84a5 | 4430 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4431 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4432 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4433 | false); |
52de1fe1 JA |
4434 | if (ret > 0) |
4435 | ret = 0; | |
4436 | } | |
4437 | ||
4438 | return ret; | |
4439 | } | |
4440 | ||
4441 | #ifdef CONFIG_COMPAT | |
4442 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4443 | struct io_async_msghdr *iomsg) |
52de1fe1 JA |
4444 | { |
4445 | struct compat_msghdr __user *msg_compat; | |
4446 | struct io_sr_msg *sr = &req->sr_msg; | |
4447 | struct compat_iovec __user *uiov; | |
4448 | compat_uptr_t ptr; | |
4449 | compat_size_t len; | |
4450 | int ret; | |
4451 | ||
270a5940 | 4452 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
1400e697 | 4453 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
52de1fe1 JA |
4454 | &ptr, &len); |
4455 | if (ret) | |
4456 | return ret; | |
4457 | ||
4458 | uiov = compat_ptr(ptr); | |
4459 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4460 | compat_ssize_t clen; | |
4461 | ||
4462 | if (len > 1) | |
4463 | return -EINVAL; | |
4464 | if (!access_ok(uiov, sizeof(*uiov))) | |
4465 | return -EFAULT; | |
4466 | if (__get_user(clen, &uiov->iov_len)) | |
4467 | return -EFAULT; | |
4468 | if (clen < 0) | |
4469 | return -EINVAL; | |
2d280bc8 | 4470 | sr->len = clen; |
257e84a5 | 4471 | iomsg->free_iov = NULL; |
52de1fe1 | 4472 | } else { |
257e84a5 | 4473 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4474 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4475 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4476 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4477 | if (ret < 0) |
4478 | return ret; | |
4479 | } | |
4480 | ||
4481 | return 0; | |
4482 | } | |
4483 | #endif | |
4484 | ||
1400e697 PB |
4485 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4486 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4487 | { |
1400e697 | 4488 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4489 | |
4490 | #ifdef CONFIG_COMPAT | |
4491 | if (req->ctx->compat) | |
1400e697 | 4492 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4493 | #endif |
52de1fe1 | 4494 | |
1400e697 | 4495 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4496 | } |
4497 | ||
bcda7baa | 4498 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4499 | bool needs_lock) |
bcda7baa JA |
4500 | { |
4501 | struct io_sr_msg *sr = &req->sr_msg; | |
4502 | struct io_buffer *kbuf; | |
4503 | ||
bcda7baa JA |
4504 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4505 | if (IS_ERR(kbuf)) | |
4506 | return kbuf; | |
4507 | ||
4508 | sr->kbuf = kbuf; | |
4509 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4510 | return kbuf; |
fddaface JA |
4511 | } |
4512 | ||
7fbb1b54 PB |
4513 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4514 | { | |
4515 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4516 | } | |
4517 | ||
93642ef8 | 4518 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4519 | { |
99bc4c38 | 4520 | int ret; |
3529d8c2 | 4521 | |
93642ef8 PB |
4522 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4523 | if (!ret) | |
4524 | req->flags |= REQ_F_NEED_CLEANUP; | |
4525 | return ret; | |
4526 | } | |
4527 | ||
4528 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4529 | { | |
4530 | struct io_sr_msg *sr = &req->sr_msg; | |
4531 | ||
d2b6f48b PB |
4532 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4533 | return -EINVAL; | |
4534 | ||
270a5940 | 4535 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4536 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4537 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4538 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4539 | if (sr->msg_flags & MSG_DONTWAIT) | |
4540 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4541 | |
d8768362 JA |
4542 | #ifdef CONFIG_COMPAT |
4543 | if (req->ctx->compat) | |
4544 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4545 | #endif | |
93642ef8 | 4546 | return 0; |
aa1fa28f JA |
4547 | } |
4548 | ||
889fca73 | 4549 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4550 | { |
6b754c8b | 4551 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4552 | struct socket *sock; |
7fbb1b54 | 4553 | struct io_buffer *kbuf; |
7a7cacba | 4554 | unsigned flags; |
0031275d | 4555 | int min_ret = 0; |
52de1fe1 | 4556 | int ret, cflags = 0; |
45d189c6 | 4557 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4558 | |
dba4a925 | 4559 | sock = sock_from_file(req->file); |
7a7cacba | 4560 | if (unlikely(!sock)) |
dba4a925 | 4561 | return -ENOTSOCK; |
3529d8c2 | 4562 | |
257e84a5 PB |
4563 | kmsg = req->async_data; |
4564 | if (!kmsg) { | |
7a7cacba PB |
4565 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4566 | if (ret) | |
681fda8d | 4567 | return ret; |
7a7cacba PB |
4568 | kmsg = &iomsg; |
4569 | } | |
03b1230c | 4570 | |
bc02ef33 | 4571 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4572 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4573 | if (IS_ERR(kbuf)) |
52de1fe1 | 4574 | return PTR_ERR(kbuf); |
7a7cacba | 4575 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4576 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4577 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4578 | 1, req->sr_msg.len); |
4579 | } | |
52de1fe1 | 4580 | |
04411806 PB |
4581 | flags = req->sr_msg.msg_flags; |
4582 | if (force_nonblock) | |
7a7cacba | 4583 | flags |= MSG_DONTWAIT; |
0031275d SM |
4584 | if (flags & MSG_WAITALL) |
4585 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4586 | ||
7a7cacba PB |
4587 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4588 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4589 | if (force_nonblock && ret == -EAGAIN) |
4590 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4591 | if (ret == -ERESTARTSYS) |
4592 | ret = -EINTR; | |
03b1230c | 4593 | |
7fbb1b54 PB |
4594 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4595 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4596 | /* fast path, check for non-NULL to avoid function call */ |
4597 | if (kmsg->free_iov) | |
4598 | kfree(kmsg->free_iov); | |
99bc4c38 | 4599 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4600 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
4e88d6e7 | 4601 | req_set_fail_links(req); |
889fca73 | 4602 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4603 | return 0; |
0fa03c62 | 4604 | } |
5d17b4a4 | 4605 | |
889fca73 | 4606 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4607 | { |
6b754c8b | 4608 | struct io_buffer *kbuf; |
7a7cacba PB |
4609 | struct io_sr_msg *sr = &req->sr_msg; |
4610 | struct msghdr msg; | |
4611 | void __user *buf = sr->buf; | |
fddaface | 4612 | struct socket *sock; |
7a7cacba PB |
4613 | struct iovec iov; |
4614 | unsigned flags; | |
0031275d | 4615 | int min_ret = 0; |
bcda7baa | 4616 | int ret, cflags = 0; |
45d189c6 | 4617 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4618 | |
dba4a925 | 4619 | sock = sock_from_file(req->file); |
7a7cacba | 4620 | if (unlikely(!sock)) |
dba4a925 | 4621 | return -ENOTSOCK; |
fddaface | 4622 | |
bc02ef33 | 4623 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4624 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4625 | if (IS_ERR(kbuf)) |
4626 | return PTR_ERR(kbuf); | |
7a7cacba | 4627 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4628 | } |
bcda7baa | 4629 | |
7a7cacba | 4630 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4631 | if (unlikely(ret)) |
4632 | goto out_free; | |
fddaface | 4633 | |
7a7cacba PB |
4634 | msg.msg_name = NULL; |
4635 | msg.msg_control = NULL; | |
4636 | msg.msg_controllen = 0; | |
4637 | msg.msg_namelen = 0; | |
4638 | msg.msg_iocb = NULL; | |
4639 | msg.msg_flags = 0; | |
fddaface | 4640 | |
04411806 PB |
4641 | flags = req->sr_msg.msg_flags; |
4642 | if (force_nonblock) | |
7a7cacba | 4643 | flags |= MSG_DONTWAIT; |
0031275d SM |
4644 | if (flags & MSG_WAITALL) |
4645 | min_ret = iov_iter_count(&msg.msg_iter); | |
4646 | ||
7a7cacba PB |
4647 | ret = sock_recvmsg(sock, &msg, flags); |
4648 | if (force_nonblock && ret == -EAGAIN) | |
4649 | return -EAGAIN; | |
4650 | if (ret == -ERESTARTSYS) | |
4651 | ret = -EINTR; | |
14c32eee | 4652 | out_free: |
7fbb1b54 PB |
4653 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4654 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4655 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
fddaface | 4656 | req_set_fail_links(req); |
889fca73 | 4657 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4658 | return 0; |
fddaface JA |
4659 | } |
4660 | ||
3529d8c2 | 4661 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4662 | { |
8ed8d3c3 JA |
4663 | struct io_accept *accept = &req->accept; |
4664 | ||
14587a46 | 4665 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4666 | return -EINVAL; |
8042d6ce | 4667 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4668 | return -EINVAL; |
4669 | ||
d55e5f5b JA |
4670 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4671 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4672 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4673 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4674 | return 0; |
8ed8d3c3 | 4675 | } |
17f2fe35 | 4676 | |
889fca73 | 4677 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4678 | { |
4679 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4680 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4681 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4682 | int ret; |
4683 | ||
e697deed JX |
4684 | if (req->file->f_flags & O_NONBLOCK) |
4685 | req->flags |= REQ_F_NOWAIT; | |
4686 | ||
8ed8d3c3 | 4687 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4688 | accept->addr_len, accept->flags, |
4689 | accept->nofile); | |
8ed8d3c3 | 4690 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4691 | return -EAGAIN; |
ac45abc0 PB |
4692 | if (ret < 0) { |
4693 | if (ret == -ERESTARTSYS) | |
4694 | ret = -EINTR; | |
4e88d6e7 | 4695 | req_set_fail_links(req); |
ac45abc0 | 4696 | } |
889fca73 | 4697 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4698 | return 0; |
8ed8d3c3 JA |
4699 | } |
4700 | ||
93642ef8 PB |
4701 | static int io_connect_prep_async(struct io_kiocb *req) |
4702 | { | |
4703 | struct io_async_connect *io = req->async_data; | |
4704 | struct io_connect *conn = &req->connect; | |
4705 | ||
4706 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4707 | } | |
4708 | ||
3529d8c2 | 4709 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4710 | { |
3529d8c2 | 4711 | struct io_connect *conn = &req->connect; |
f499a021 | 4712 | |
14587a46 | 4713 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4714 | return -EINVAL; |
4715 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4716 | return -EINVAL; | |
4717 | ||
3529d8c2 JA |
4718 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4719 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4720 | return 0; |
f499a021 JA |
4721 | } |
4722 | ||
889fca73 | 4723 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4724 | { |
e8c2bc1f | 4725 | struct io_async_connect __io, *io; |
f8e85cf2 | 4726 | unsigned file_flags; |
3fbb51c1 | 4727 | int ret; |
45d189c6 | 4728 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4729 | |
e8c2bc1f JA |
4730 | if (req->async_data) { |
4731 | io = req->async_data; | |
f499a021 | 4732 | } else { |
3529d8c2 JA |
4733 | ret = move_addr_to_kernel(req->connect.addr, |
4734 | req->connect.addr_len, | |
e8c2bc1f | 4735 | &__io.address); |
f499a021 JA |
4736 | if (ret) |
4737 | goto out; | |
4738 | io = &__io; | |
4739 | } | |
4740 | ||
3fbb51c1 JA |
4741 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4742 | ||
e8c2bc1f | 4743 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4744 | req->connect.addr_len, file_flags); |
87f80d62 | 4745 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4746 | if (req->async_data) |
b7bb4f7d | 4747 | return -EAGAIN; |
e8c2bc1f | 4748 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4749 | ret = -ENOMEM; |
4750 | goto out; | |
4751 | } | |
e8c2bc1f | 4752 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4753 | return -EAGAIN; |
f499a021 | 4754 | } |
f8e85cf2 JA |
4755 | if (ret == -ERESTARTSYS) |
4756 | ret = -EINTR; | |
f499a021 | 4757 | out: |
4e88d6e7 JA |
4758 | if (ret < 0) |
4759 | req_set_fail_links(req); | |
889fca73 | 4760 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4761 | return 0; |
469956e8 Y |
4762 | } |
4763 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4764 | #define IO_NETOP_FN(op) \ |
4765 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4766 | { \ | |
4767 | return -EOPNOTSUPP; \ | |
4768 | } | |
4769 | ||
4770 | #define IO_NETOP_PREP(op) \ | |
4771 | IO_NETOP_FN(op) \ | |
4772 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4773 | { \ | |
4774 | return -EOPNOTSUPP; \ | |
4775 | } \ | |
4776 | ||
4777 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4778 | IO_NETOP_PREP(op) \ | |
4779 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4780 | { \ | |
4781 | return -EOPNOTSUPP; \ | |
4782 | } | |
4783 | ||
4784 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4785 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4786 | IO_NETOP_PREP_ASYNC(connect); | |
4787 | IO_NETOP_PREP(accept); | |
4788 | IO_NETOP_FN(send); | |
4789 | IO_NETOP_FN(recv); | |
469956e8 | 4790 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4791 | |
d7718a9d JA |
4792 | struct io_poll_table { |
4793 | struct poll_table_struct pt; | |
4794 | struct io_kiocb *req; | |
4795 | int error; | |
4796 | }; | |
ce593a6c | 4797 | |
d7718a9d JA |
4798 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
4799 | __poll_t mask, task_work_func_t func) | |
4800 | { | |
aa96bf8a | 4801 | int ret; |
d7718a9d JA |
4802 | |
4803 | /* for instances that support it check for an event match first: */ | |
4804 | if (mask && !(mask & poll->events)) | |
4805 | return 0; | |
4806 | ||
4807 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4808 | ||
4809 | list_del_init(&poll->wait.entry); | |
4810 | ||
d7718a9d | 4811 | req->result = mask; |
7cbf1722 | 4812 | req->task_work.func = func; |
6d816e08 | 4813 | |
d7718a9d | 4814 | /* |
e3aabf95 JA |
4815 | * If this fails, then the task is exiting. When a task exits, the |
4816 | * work gets canceled, so just cancel this request as well instead | |
4817 | * of executing it. We can't safely execute it anyway, as we may not | |
4818 | * have the needed state needed for it anyway. | |
d7718a9d | 4819 | */ |
355fb9e2 | 4820 | ret = io_req_task_work_add(req); |
aa96bf8a | 4821 | if (unlikely(ret)) { |
e3aabf95 | 4822 | WRITE_ONCE(poll->canceled, true); |
eab30c4d | 4823 | io_req_task_work_add_fallback(req, func); |
aa96bf8a | 4824 | } |
d7718a9d JA |
4825 | return 1; |
4826 | } | |
4827 | ||
74ce6ce4 JA |
4828 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4829 | __acquires(&req->ctx->completion_lock) | |
4830 | { | |
4831 | struct io_ring_ctx *ctx = req->ctx; | |
4832 | ||
4833 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4834 | struct poll_table_struct pt = { ._key = poll->events }; | |
4835 | ||
4836 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4837 | } | |
4838 | ||
4839 | spin_lock_irq(&ctx->completion_lock); | |
4840 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4841 | add_wait_queue(poll->head, &poll->wait); | |
4842 | return true; | |
4843 | } | |
4844 | ||
4845 | return false; | |
4846 | } | |
4847 | ||
d4e7cd36 | 4848 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4849 | { |
e8c2bc1f | 4850 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4851 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4852 | return req->async_data; |
d4e7cd36 JA |
4853 | return req->apoll->double_poll; |
4854 | } | |
4855 | ||
4856 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4857 | { | |
4858 | if (req->opcode == IORING_OP_POLL_ADD) | |
4859 | return &req->poll; | |
4860 | return &req->apoll->poll; | |
4861 | } | |
4862 | ||
4863 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 4864 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
4865 | { |
4866 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4867 | |
4868 | lockdep_assert_held(&req->ctx->completion_lock); | |
4869 | ||
4870 | if (poll && poll->head) { | |
4871 | struct wait_queue_head *head = poll->head; | |
4872 | ||
4873 | spin_lock(&head->lock); | |
4874 | list_del_init(&poll->wait.entry); | |
4875 | if (poll->wait.private) | |
de9b4cca | 4876 | req_ref_put(req); |
18bceab1 JA |
4877 | poll->head = NULL; |
4878 | spin_unlock(&head->lock); | |
4879 | } | |
4880 | } | |
4881 | ||
e27414be | 4882 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 4883 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
4884 | { |
4885 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 4886 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 4887 | int error; |
18bceab1 | 4888 | |
e27414be | 4889 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 4890 | error = -ECANCELED; |
88e41cf9 | 4891 | req->poll.events |= EPOLLONESHOT; |
e27414be | 4892 | } else { |
5082620f | 4893 | error = mangle_poll(mask); |
e27414be | 4894 | } |
b69de288 JA |
4895 | if (req->poll.events & EPOLLONESHOT) |
4896 | flags = 0; | |
4897 | if (!__io_cqring_fill_event(req, error, flags)) { | |
5082620f | 4898 | io_poll_remove_waitqs(req); |
88e41cf9 JA |
4899 | req->poll.done = true; |
4900 | flags = 0; | |
4901 | } | |
18bceab1 | 4902 | io_commit_cqring(ctx); |
88e41cf9 | 4903 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
4904 | } |
4905 | ||
dd221f46 | 4906 | static void io_poll_task_func(struct callback_head *cb) |
18bceab1 | 4907 | { |
dd221f46 | 4908 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
18bceab1 | 4909 | struct io_ring_ctx *ctx = req->ctx; |
dd221f46 | 4910 | struct io_kiocb *nxt; |
18bceab1 JA |
4911 | |
4912 | if (io_poll_rewait(req, &req->poll)) { | |
4913 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 | 4914 | } else { |
f40b964a | 4915 | bool done; |
88e41cf9 | 4916 | |
e27414be | 4917 | done = io_poll_complete(req, req->result); |
88e41cf9 JA |
4918 | if (done) { |
4919 | hash_del(&req->hash_node); | |
f40b964a | 4920 | } else { |
88e41cf9 JA |
4921 | req->result = 0; |
4922 | add_wait_queue(req->poll.head, &req->poll.wait); | |
4923 | } | |
dd221f46 | 4924 | spin_unlock_irq(&ctx->completion_lock); |
f40b964a | 4925 | io_cqring_ev_posted(ctx); |
18bceab1 | 4926 | |
88e41cf9 JA |
4927 | if (done) { |
4928 | nxt = io_put_req_find_next(req); | |
4929 | if (nxt) | |
4930 | __io_req_task_submit(nxt); | |
4931 | } | |
dd221f46 | 4932 | } |
18bceab1 JA |
4933 | } |
4934 | ||
4935 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4936 | int sync, void *key) | |
4937 | { | |
4938 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4939 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4940 | __poll_t mask = key_to_poll(key); |
4941 | ||
4942 | /* for instances that support it check for an event match first: */ | |
4943 | if (mask && !(mask & poll->events)) | |
4944 | return 0; | |
88e41cf9 JA |
4945 | if (!(poll->events & EPOLLONESHOT)) |
4946 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 4947 | |
8706e04e JA |
4948 | list_del_init(&wait->entry); |
4949 | ||
807abcb0 | 4950 | if (poll && poll->head) { |
18bceab1 JA |
4951 | bool done; |
4952 | ||
807abcb0 JA |
4953 | spin_lock(&poll->head->lock); |
4954 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 4955 | if (!done) |
807abcb0 | 4956 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
4957 | /* make sure double remove sees this as being gone */ |
4958 | wait->private = NULL; | |
807abcb0 | 4959 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
4960 | if (!done) { |
4961 | /* use wait func handler, so it matches the rq type */ | |
4962 | poll->wait.func(&poll->wait, mode, sync, key); | |
4963 | } | |
18bceab1 | 4964 | } |
de9b4cca | 4965 | req_ref_put(req); |
18bceab1 JA |
4966 | return 1; |
4967 | } | |
4968 | ||
4969 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
4970 | wait_queue_func_t wake_func) | |
4971 | { | |
4972 | poll->head = NULL; | |
4973 | poll->done = false; | |
4974 | poll->canceled = false; | |
b69de288 | 4975 | poll->update_events = poll->update_user_data = false; |
464dca61 JA |
4976 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
4977 | /* mask in events that we always want/need */ | |
4978 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
4979 | INIT_LIST_HEAD(&poll->wait.entry); |
4980 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
4981 | } | |
4982 | ||
4983 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
4984 | struct wait_queue_head *head, |
4985 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
4986 | { |
4987 | struct io_kiocb *req = pt->req; | |
4988 | ||
4989 | /* | |
4990 | * If poll->head is already set, it's because the file being polled | |
4991 | * uses multiple waitqueues for poll handling (eg one for read, one | |
4992 | * for write). Setup a separate io_poll_iocb if this happens. | |
4993 | */ | |
4994 | if (unlikely(poll->head)) { | |
58852d4d PB |
4995 | struct io_poll_iocb *poll_one = poll; |
4996 | ||
18bceab1 | 4997 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 4998 | if (*poll_ptr) { |
18bceab1 JA |
4999 | pt->error = -EINVAL; |
5000 | return; | |
5001 | } | |
1c3b3e65 JA |
5002 | /* double add on the same waitqueue head, ignore */ |
5003 | if (poll->head == head) | |
5004 | return; | |
18bceab1 JA |
5005 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5006 | if (!poll) { | |
5007 | pt->error = -ENOMEM; | |
5008 | return; | |
5009 | } | |
58852d4d | 5010 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5011 | req_ref_get(req); |
18bceab1 | 5012 | poll->wait.private = req; |
807abcb0 | 5013 | *poll_ptr = poll; |
18bceab1 JA |
5014 | } |
5015 | ||
5016 | pt->error = 0; | |
5017 | poll->head = head; | |
a31eb4a2 JX |
5018 | |
5019 | if (poll->events & EPOLLEXCLUSIVE) | |
5020 | add_wait_queue_exclusive(head, &poll->wait); | |
5021 | else | |
5022 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5023 | } |
5024 | ||
5025 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5026 | struct poll_table_struct *p) | |
5027 | { | |
5028 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5029 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5030 | |
807abcb0 | 5031 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5032 | } |
5033 | ||
d7718a9d JA |
5034 | static void io_async_task_func(struct callback_head *cb) |
5035 | { | |
5036 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); | |
5037 | struct async_poll *apoll = req->apoll; | |
5038 | struct io_ring_ctx *ctx = req->ctx; | |
5039 | ||
5040 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); | |
5041 | ||
74ce6ce4 | 5042 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5043 | spin_unlock_irq(&ctx->completion_lock); |
74ce6ce4 | 5044 | return; |
d7718a9d JA |
5045 | } |
5046 | ||
0ea13b44 | 5047 | hash_del(&req->hash_node); |
d4e7cd36 | 5048 | io_poll_remove_double(req); |
74ce6ce4 JA |
5049 | spin_unlock_irq(&ctx->completion_lock); |
5050 | ||
0be0b0e3 PB |
5051 | if (!READ_ONCE(apoll->poll.canceled)) |
5052 | __io_req_task_submit(req); | |
5053 | else | |
2593553a | 5054 | io_req_complete_failed(req, -ECANCELED); |
aa340845 | 5055 | |
807abcb0 | 5056 | kfree(apoll->double_poll); |
31067255 | 5057 | kfree(apoll); |
d7718a9d JA |
5058 | } |
5059 | ||
5060 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5061 | void *key) | |
5062 | { | |
5063 | struct io_kiocb *req = wait->private; | |
5064 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5065 | ||
5066 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5067 | key_to_poll(key)); | |
5068 | ||
5069 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5070 | } | |
5071 | ||
5072 | static void io_poll_req_insert(struct io_kiocb *req) | |
5073 | { | |
5074 | struct io_ring_ctx *ctx = req->ctx; | |
5075 | struct hlist_head *list; | |
5076 | ||
5077 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5078 | hlist_add_head(&req->hash_node, list); | |
5079 | } | |
5080 | ||
5081 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5082 | struct io_poll_iocb *poll, | |
5083 | struct io_poll_table *ipt, __poll_t mask, | |
5084 | wait_queue_func_t wake_func) | |
5085 | __acquires(&ctx->completion_lock) | |
5086 | { | |
5087 | struct io_ring_ctx *ctx = req->ctx; | |
5088 | bool cancel = false; | |
5089 | ||
4d52f338 | 5090 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5091 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5092 | poll->file = req->file; |
18bceab1 | 5093 | poll->wait.private = req; |
d7718a9d JA |
5094 | |
5095 | ipt->pt._key = mask; | |
5096 | ipt->req = req; | |
5097 | ipt->error = -EINVAL; | |
5098 | ||
d7718a9d JA |
5099 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
5100 | ||
5101 | spin_lock_irq(&ctx->completion_lock); | |
5102 | if (likely(poll->head)) { | |
5103 | spin_lock(&poll->head->lock); | |
5104 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5105 | if (ipt->error) | |
5106 | cancel = true; | |
5107 | ipt->error = 0; | |
5108 | mask = 0; | |
5109 | } | |
88e41cf9 | 5110 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5111 | list_del_init(&poll->wait.entry); |
5112 | else if (cancel) | |
5113 | WRITE_ONCE(poll->canceled, true); | |
5114 | else if (!poll->done) /* actually waiting for an event */ | |
5115 | io_poll_req_insert(req); | |
5116 | spin_unlock(&poll->head->lock); | |
5117 | } | |
5118 | ||
5119 | return mask; | |
5120 | } | |
5121 | ||
5122 | static bool io_arm_poll_handler(struct io_kiocb *req) | |
5123 | { | |
5124 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5125 | struct io_ring_ctx *ctx = req->ctx; | |
5126 | struct async_poll *apoll; | |
5127 | struct io_poll_table ipt; | |
5128 | __poll_t mask, ret; | |
9dab14b8 | 5129 | int rw; |
d7718a9d JA |
5130 | |
5131 | if (!req->file || !file_can_poll(req->file)) | |
5132 | return false; | |
24c74678 | 5133 | if (req->flags & REQ_F_POLLED) |
d7718a9d | 5134 | return false; |
9dab14b8 JA |
5135 | if (def->pollin) |
5136 | rw = READ; | |
5137 | else if (def->pollout) | |
5138 | rw = WRITE; | |
5139 | else | |
5140 | return false; | |
5141 | /* if we can't nonblock try, then no point in arming a poll handler */ | |
7b29f92d | 5142 | if (!io_file_supports_async(req, rw)) |
d7718a9d JA |
5143 | return false; |
5144 | ||
5145 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5146 | if (unlikely(!apoll)) | |
5147 | return false; | |
807abcb0 | 5148 | apoll->double_poll = NULL; |
d7718a9d JA |
5149 | |
5150 | req->flags |= REQ_F_POLLED; | |
d7718a9d | 5151 | req->apoll = apoll; |
d7718a9d | 5152 | |
88e41cf9 | 5153 | mask = EPOLLONESHOT; |
d7718a9d | 5154 | if (def->pollin) |
8755d97a | 5155 | mask |= POLLIN | POLLRDNORM; |
d7718a9d JA |
5156 | if (def->pollout) |
5157 | mask |= POLLOUT | POLLWRNORM; | |
901341bb LH |
5158 | |
5159 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5160 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5161 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5162 | mask &= ~POLLIN; | |
5163 | ||
d7718a9d JA |
5164 | mask |= POLLERR | POLLPRI; |
5165 | ||
5166 | ipt.pt._qproc = io_async_queue_proc; | |
5167 | ||
5168 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5169 | io_async_wake); | |
a36da65c | 5170 | if (ret || ipt.error) { |
d4e7cd36 | 5171 | io_poll_remove_double(req); |
d7718a9d | 5172 | spin_unlock_irq(&ctx->completion_lock); |
807abcb0 | 5173 | kfree(apoll->double_poll); |
d7718a9d JA |
5174 | kfree(apoll); |
5175 | return false; | |
5176 | } | |
5177 | spin_unlock_irq(&ctx->completion_lock); | |
5178 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, | |
5179 | apoll->poll.events); | |
5180 | return true; | |
5181 | } | |
5182 | ||
5183 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5184 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5185 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5186 | { |
b41e9852 | 5187 | bool do_complete = false; |
221c5eb2 | 5188 | |
5082620f JA |
5189 | if (!poll->head) |
5190 | return false; | |
221c5eb2 | 5191 | spin_lock(&poll->head->lock); |
b2e720ac JA |
5192 | if (do_cancel) |
5193 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5194 | if (!list_empty(&poll->wait.entry)) { |
5195 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5196 | do_complete = true; |
221c5eb2 JA |
5197 | } |
5198 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5199 | hash_del(&req->hash_node); |
d7718a9d JA |
5200 | return do_complete; |
5201 | } | |
5202 | ||
b2c3f7e1 | 5203 | static bool io_poll_remove_waitqs(struct io_kiocb *req) |
e07785b0 | 5204 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5205 | { |
5206 | bool do_complete; | |
5207 | ||
d4e7cd36 JA |
5208 | io_poll_remove_double(req); |
5209 | ||
d7718a9d | 5210 | if (req->opcode == IORING_OP_POLL_ADD) { |
b2e720ac | 5211 | do_complete = __io_poll_remove_one(req, &req->poll, true); |
d7718a9d | 5212 | } else { |
3bfa5bcb JA |
5213 | struct async_poll *apoll = req->apoll; |
5214 | ||
d7718a9d | 5215 | /* non-poll requests have submit ref still */ |
b2e720ac | 5216 | do_complete = __io_poll_remove_one(req, &apoll->poll, true); |
3bfa5bcb | 5217 | if (do_complete) { |
7394161c | 5218 | req_ref_put(req); |
807abcb0 | 5219 | kfree(apoll->double_poll); |
3bfa5bcb JA |
5220 | kfree(apoll); |
5221 | } | |
b1f573bd XW |
5222 | } |
5223 | ||
b2c3f7e1 JA |
5224 | return do_complete; |
5225 | } | |
5226 | ||
5227 | static bool io_poll_remove_one(struct io_kiocb *req) | |
e07785b0 | 5228 | __must_hold(&req->ctx->completion_lock) |
b2c3f7e1 JA |
5229 | { |
5230 | bool do_complete; | |
5231 | ||
5232 | do_complete = io_poll_remove_waitqs(req); | |
b41e9852 JA |
5233 | if (do_complete) { |
5234 | io_cqring_fill_event(req, -ECANCELED); | |
5235 | io_commit_cqring(req->ctx); | |
f254ac04 | 5236 | req_set_fail_links(req); |
216578e5 | 5237 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5238 | } |
5239 | ||
5240 | return do_complete; | |
221c5eb2 JA |
5241 | } |
5242 | ||
76e1b642 JA |
5243 | /* |
5244 | * Returns true if we found and killed one or more poll requests | |
5245 | */ | |
6b81928d PB |
5246 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
5247 | struct files_struct *files) | |
221c5eb2 | 5248 | { |
78076bb6 | 5249 | struct hlist_node *tmp; |
221c5eb2 | 5250 | struct io_kiocb *req; |
8e2e1faf | 5251 | int posted = 0, i; |
221c5eb2 JA |
5252 | |
5253 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5254 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5255 | struct hlist_head *list; | |
5256 | ||
5257 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5258 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
6b81928d | 5259 | if (io_match_task(req, tsk, files)) |
f3606e3a JA |
5260 | posted += io_poll_remove_one(req); |
5261 | } | |
221c5eb2 JA |
5262 | } |
5263 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5264 | |
8e2e1faf JA |
5265 | if (posted) |
5266 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5267 | |
5268 | return posted != 0; | |
221c5eb2 JA |
5269 | } |
5270 | ||
b2cb805f | 5271 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr) |
e07785b0 | 5272 | __must_hold(&ctx->completion_lock) |
47f46768 | 5273 | { |
78076bb6 | 5274 | struct hlist_head *list; |
47f46768 JA |
5275 | struct io_kiocb *req; |
5276 | ||
78076bb6 JA |
5277 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5278 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5279 | if (sqe_addr != req->user_data) |
5280 | continue; | |
b2cb805f | 5281 | return req; |
47f46768 JA |
5282 | } |
5283 | ||
b2cb805f JA |
5284 | return NULL; |
5285 | } | |
5286 | ||
5287 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) | |
e07785b0 | 5288 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5289 | { |
5290 | struct io_kiocb *req; | |
5291 | ||
5292 | req = io_poll_find(ctx, sqe_addr); | |
5293 | if (!req) | |
5294 | return -ENOENT; | |
5295 | if (io_poll_remove_one(req)) | |
5296 | return 0; | |
5297 | ||
5298 | return -EALREADY; | |
47f46768 JA |
5299 | } |
5300 | ||
3529d8c2 JA |
5301 | static int io_poll_remove_prep(struct io_kiocb *req, |
5302 | const struct io_uring_sqe *sqe) | |
0969e783 | 5303 | { |
0969e783 JA |
5304 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5305 | return -EINVAL; | |
5306 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
5307 | sqe->poll_events) | |
5308 | return -EINVAL; | |
5309 | ||
018043be | 5310 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
0969e783 JA |
5311 | return 0; |
5312 | } | |
5313 | ||
221c5eb2 JA |
5314 | /* |
5315 | * Find a running poll command that matches one specified in sqe->addr, | |
5316 | * and remove it if found. | |
5317 | */ | |
61e98203 | 5318 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
221c5eb2 JA |
5319 | { |
5320 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 5321 | int ret; |
221c5eb2 | 5322 | |
221c5eb2 | 5323 | spin_lock_irq(&ctx->completion_lock); |
018043be | 5324 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
221c5eb2 JA |
5325 | spin_unlock_irq(&ctx->completion_lock); |
5326 | ||
4e88d6e7 JA |
5327 | if (ret < 0) |
5328 | req_set_fail_links(req); | |
e1e16097 | 5329 | io_req_complete(req, ret); |
221c5eb2 JA |
5330 | return 0; |
5331 | } | |
5332 | ||
221c5eb2 JA |
5333 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5334 | void *key) | |
5335 | { | |
c2f2eb7d JA |
5336 | struct io_kiocb *req = wait->private; |
5337 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5338 | |
d7718a9d | 5339 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5340 | } |
5341 | ||
221c5eb2 JA |
5342 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5343 | struct poll_table_struct *p) | |
5344 | { | |
5345 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5346 | ||
e8c2bc1f | 5347 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5348 | } |
5349 | ||
3529d8c2 | 5350 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5351 | { |
5352 | struct io_poll_iocb *poll = &req->poll; | |
88e41cf9 | 5353 | u32 events, flags; |
221c5eb2 JA |
5354 | |
5355 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5356 | return -EINVAL; | |
b69de288 | 5357 | if (sqe->ioprio || sqe->buf_index) |
88e41cf9 JA |
5358 | return -EINVAL; |
5359 | flags = READ_ONCE(sqe->len); | |
b69de288 JA |
5360 | if (flags & ~(IORING_POLL_ADD_MULTI | IORING_POLL_UPDATE_EVENTS | |
5361 | IORING_POLL_UPDATE_USER_DATA)) | |
221c5eb2 | 5362 | return -EINVAL; |
5769a351 JX |
5363 | events = READ_ONCE(sqe->poll32_events); |
5364 | #ifdef __BIG_ENDIAN | |
5365 | events = swahw32(events); | |
5366 | #endif | |
b69de288 | 5367 | if (!(flags & IORING_POLL_ADD_MULTI)) |
88e41cf9 | 5368 | events |= EPOLLONESHOT; |
b69de288 JA |
5369 | poll->update_events = poll->update_user_data = false; |
5370 | if (flags & IORING_POLL_UPDATE_EVENTS) { | |
5371 | poll->update_events = true; | |
5372 | poll->old_user_data = READ_ONCE(sqe->addr); | |
5373 | } | |
5374 | if (flags & IORING_POLL_UPDATE_USER_DATA) { | |
5375 | poll->update_user_data = true; | |
5376 | poll->new_user_data = READ_ONCE(sqe->off); | |
5377 | } | |
5378 | if (!(poll->update_events || poll->update_user_data) && | |
5379 | (sqe->off || sqe->addr)) | |
5380 | return -EINVAL; | |
88e41cf9 JA |
5381 | poll->events = demangle_poll(events) | |
5382 | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
0969e783 JA |
5383 | return 0; |
5384 | } | |
5385 | ||
b69de288 | 5386 | static int __io_poll_add(struct io_kiocb *req) |
0969e783 JA |
5387 | { |
5388 | struct io_poll_iocb *poll = &req->poll; | |
5389 | struct io_ring_ctx *ctx = req->ctx; | |
5390 | struct io_poll_table ipt; | |
0969e783 | 5391 | __poll_t mask; |
0969e783 | 5392 | |
d7718a9d | 5393 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5394 | |
d7718a9d JA |
5395 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5396 | io_poll_wake); | |
221c5eb2 | 5397 | |
8c838788 | 5398 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5399 | ipt.error = 0; |
e27414be | 5400 | io_poll_complete(req, mask); |
221c5eb2 | 5401 | } |
221c5eb2 JA |
5402 | spin_unlock_irq(&ctx->completion_lock); |
5403 | ||
8c838788 JA |
5404 | if (mask) { |
5405 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5406 | if (poll->events & EPOLLONESHOT) |
5407 | io_put_req(req); | |
221c5eb2 | 5408 | } |
8c838788 | 5409 | return ipt.error; |
221c5eb2 JA |
5410 | } |
5411 | ||
b69de288 JA |
5412 | static int io_poll_update(struct io_kiocb *req) |
5413 | { | |
5414 | struct io_ring_ctx *ctx = req->ctx; | |
5415 | struct io_kiocb *preq; | |
cb3b200e | 5416 | bool completing; |
b69de288 JA |
5417 | int ret; |
5418 | ||
5419 | spin_lock_irq(&ctx->completion_lock); | |
5420 | preq = io_poll_find(ctx, req->poll.old_user_data); | |
5421 | if (!preq) { | |
5422 | ret = -ENOENT; | |
5423 | goto err; | |
5424 | } else if (preq->opcode != IORING_OP_POLL_ADD) { | |
5425 | /* don't allow internal poll updates */ | |
5426 | ret = -EACCES; | |
5427 | goto err; | |
5428 | } | |
cb3b200e JA |
5429 | |
5430 | /* | |
5431 | * Don't allow racy completion with singleshot, as we cannot safely | |
5432 | * update those. For multishot, if we're racing with completion, just | |
5433 | * let completion re-add it. | |
5434 | */ | |
5435 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5436 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5437 | ret = -EALREADY; | |
5438 | goto err; | |
b69de288 JA |
5439 | } |
5440 | /* we now have a detached poll request. reissue. */ | |
5441 | ret = 0; | |
5442 | err: | |
b69de288 | 5443 | if (ret < 0) { |
cb3b200e | 5444 | spin_unlock_irq(&ctx->completion_lock); |
b69de288 JA |
5445 | req_set_fail_links(req); |
5446 | io_req_complete(req, ret); | |
5447 | return 0; | |
5448 | } | |
5449 | /* only mask one event flags, keep behavior flags */ | |
5450 | if (req->poll.update_events) { | |
5451 | preq->poll.events &= ~0xffff; | |
5452 | preq->poll.events |= req->poll.events & 0xffff; | |
5453 | preq->poll.events |= IO_POLL_UNMASK; | |
5454 | } | |
5455 | if (req->poll.update_user_data) | |
5456 | preq->user_data = req->poll.new_user_data; | |
5457 | ||
cb3b200e JA |
5458 | spin_unlock_irq(&ctx->completion_lock); |
5459 | ||
b69de288 JA |
5460 | /* complete update request, we're done with it */ |
5461 | io_req_complete(req, ret); | |
5462 | ||
cb3b200e JA |
5463 | if (!completing) { |
5464 | ret = __io_poll_add(preq); | |
5465 | if (ret < 0) { | |
5466 | req_set_fail_links(preq); | |
5467 | io_req_complete(preq, ret); | |
5468 | } | |
b69de288 JA |
5469 | } |
5470 | return 0; | |
5471 | } | |
5472 | ||
5473 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) | |
5474 | { | |
5475 | if (!req->poll.update_events && !req->poll.update_user_data) | |
5476 | return __io_poll_add(req); | |
5477 | return io_poll_update(req); | |
5478 | } | |
5479 | ||
5262f567 JA |
5480 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5481 | { | |
ad8a48ac JA |
5482 | struct io_timeout_data *data = container_of(timer, |
5483 | struct io_timeout_data, timer); | |
5484 | struct io_kiocb *req = data->req; | |
5485 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5486 | unsigned long flags; |
5487 | ||
5262f567 | 5488 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5489 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5490 | atomic_set(&req->ctx->cq_timeouts, |
5491 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5492 | ||
78e19bbe | 5493 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
5494 | io_commit_cqring(ctx); |
5495 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5496 | ||
5497 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 5498 | req_set_fail_links(req); |
5262f567 JA |
5499 | io_put_req(req); |
5500 | return HRTIMER_NORESTART; | |
5501 | } | |
5502 | ||
fbd15848 PB |
5503 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5504 | __u64 user_data) | |
e07785b0 | 5505 | __must_hold(&ctx->completion_lock) |
f254ac04 | 5506 | { |
fbd15848 | 5507 | struct io_timeout_data *io; |
47f46768 JA |
5508 | struct io_kiocb *req; |
5509 | int ret = -ENOENT; | |
f254ac04 | 5510 | |
135fcde8 | 5511 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
47f46768 | 5512 | if (user_data == req->user_data) { |
47f46768 JA |
5513 | ret = 0; |
5514 | break; | |
5515 | } | |
5516 | } | |
5517 | ||
5518 | if (ret == -ENOENT) | |
fbd15848 PB |
5519 | return ERR_PTR(ret); |
5520 | ||
5521 | io = req->async_data; | |
e8c2bc1f | 5522 | ret = hrtimer_try_to_cancel(&io->timer); |
f254ac04 | 5523 | if (ret == -1) |
fbd15848 | 5524 | return ERR_PTR(-EALREADY); |
a71976f3 | 5525 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5526 | return req; |
5527 | } | |
47f46768 | 5528 | |
fbd15848 | 5529 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
e07785b0 | 5530 | __must_hold(&ctx->completion_lock) |
fbd15848 PB |
5531 | { |
5532 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5533 | ||
5534 | if (IS_ERR(req)) | |
5535 | return PTR_ERR(req); | |
f254ac04 JA |
5536 | |
5537 | req_set_fail_links(req); | |
f254ac04 | 5538 | io_cqring_fill_event(req, -ECANCELED); |
216578e5 | 5539 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5540 | return 0; |
5541 | } | |
5542 | ||
9c8e11b3 PB |
5543 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5544 | struct timespec64 *ts, enum hrtimer_mode mode) | |
e07785b0 | 5545 | __must_hold(&ctx->completion_lock) |
47f46768 | 5546 | { |
9c8e11b3 PB |
5547 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5548 | struct io_timeout_data *data; | |
47f46768 | 5549 | |
9c8e11b3 PB |
5550 | if (IS_ERR(req)) |
5551 | return PTR_ERR(req); | |
47f46768 | 5552 | |
9c8e11b3 PB |
5553 | req->timeout.off = 0; /* noseq */ |
5554 | data = req->async_data; | |
5555 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5556 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5557 | data->timer.function = io_timeout_fn; | |
5558 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5559 | return 0; | |
47f46768 JA |
5560 | } |
5561 | ||
3529d8c2 JA |
5562 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5563 | const struct io_uring_sqe *sqe) | |
b29472ee | 5564 | { |
9c8e11b3 PB |
5565 | struct io_timeout_rem *tr = &req->timeout_rem; |
5566 | ||
b29472ee JA |
5567 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5568 | return -EINVAL; | |
61710e43 DA |
5569 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5570 | return -EINVAL; | |
9c8e11b3 | 5571 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5572 | return -EINVAL; |
5573 | ||
9c8e11b3 PB |
5574 | tr->addr = READ_ONCE(sqe->addr); |
5575 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5576 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5577 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5578 | return -EINVAL; | |
5579 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5580 | return -EFAULT; | |
5581 | } else if (tr->flags) { | |
5582 | /* timeout removal doesn't support flags */ | |
b29472ee | 5583 | return -EINVAL; |
9c8e11b3 | 5584 | } |
b29472ee | 5585 | |
b29472ee JA |
5586 | return 0; |
5587 | } | |
5588 | ||
8662daec PB |
5589 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5590 | { | |
5591 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5592 | : HRTIMER_MODE_REL; | |
5593 | } | |
5594 | ||
11365043 JA |
5595 | /* |
5596 | * Remove or update an existing timeout command | |
5597 | */ | |
61e98203 | 5598 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5599 | { |
9c8e11b3 | 5600 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5601 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5602 | int ret; |
11365043 | 5603 | |
11365043 | 5604 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5605 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5606 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5607 | else |
5608 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5609 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5610 | |
47f46768 | 5611 | io_cqring_fill_event(req, ret); |
11365043 JA |
5612 | io_commit_cqring(ctx); |
5613 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5614 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
5615 | if (ret < 0) |
5616 | req_set_fail_links(req); | |
ec9c02ad | 5617 | io_put_req(req); |
11365043 | 5618 | return 0; |
5262f567 JA |
5619 | } |
5620 | ||
3529d8c2 | 5621 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5622 | bool is_timeout_link) |
5262f567 | 5623 | { |
ad8a48ac | 5624 | struct io_timeout_data *data; |
a41525ab | 5625 | unsigned flags; |
56080b02 | 5626 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5627 | |
ad8a48ac | 5628 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5629 | return -EINVAL; |
ad8a48ac | 5630 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5631 | return -EINVAL; |
56080b02 | 5632 | if (off && is_timeout_link) |
2d28390a | 5633 | return -EINVAL; |
a41525ab JA |
5634 | flags = READ_ONCE(sqe->timeout_flags); |
5635 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5636 | return -EINVAL; |
bdf20073 | 5637 | |
bfe68a22 | 5638 | req->timeout.off = off; |
26a61679 | 5639 | |
e8c2bc1f | 5640 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5641 | return -ENOMEM; |
5642 | ||
e8c2bc1f | 5643 | data = req->async_data; |
ad8a48ac | 5644 | data->req = req; |
ad8a48ac JA |
5645 | |
5646 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5647 | return -EFAULT; |
5648 | ||
8662daec | 5649 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac | 5650 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
2482b58f PB |
5651 | if (is_timeout_link) |
5652 | io_req_track_inflight(req); | |
ad8a48ac JA |
5653 | return 0; |
5654 | } | |
5655 | ||
61e98203 | 5656 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5657 | { |
ad8a48ac | 5658 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5659 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5660 | struct list_head *entry; |
bfe68a22 | 5661 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5662 | |
733f5c95 | 5663 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5664 | |
5262f567 JA |
5665 | /* |
5666 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5667 | * timeout event to be satisfied. If it isn't set, then this is |
5668 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5669 | */ |
8eb7e2d0 | 5670 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5671 | entry = ctx->timeout_list.prev; |
5672 | goto add; | |
5673 | } | |
5262f567 | 5674 | |
bfe68a22 PB |
5675 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5676 | req->timeout.target_seq = tail + off; | |
5262f567 | 5677 | |
f010505b MDG |
5678 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5679 | * This is safe because ->completion_lock is held, and submissions | |
5680 | * and completions are never mixed in the same ->completion_lock section. | |
5681 | */ | |
5682 | ctx->cq_last_tm_flush = tail; | |
5683 | ||
5262f567 JA |
5684 | /* |
5685 | * Insertion sort, ensuring the first entry in the list is always | |
5686 | * the one we need first. | |
5687 | */ | |
5262f567 | 5688 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5689 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5690 | timeout.list); | |
5262f567 | 5691 | |
8eb7e2d0 | 5692 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5693 | continue; |
bfe68a22 PB |
5694 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5695 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5696 | break; |
5697 | } | |
93bd25bb | 5698 | add: |
135fcde8 | 5699 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5700 | data->timer.function = io_timeout_fn; |
5701 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5702 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5703 | return 0; |
5704 | } | |
5262f567 | 5705 | |
f458dd84 PB |
5706 | struct io_cancel_data { |
5707 | struct io_ring_ctx *ctx; | |
5708 | u64 user_data; | |
5709 | }; | |
5710 | ||
62755e35 JA |
5711 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5712 | { | |
5713 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5714 | struct io_cancel_data *cd = data; |
62755e35 | 5715 | |
f458dd84 | 5716 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5717 | } |
5718 | ||
f458dd84 PB |
5719 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5720 | struct io_ring_ctx *ctx) | |
62755e35 | 5721 | { |
f458dd84 | 5722 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5723 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5724 | int ret = 0; |
5725 | ||
f458dd84 | 5726 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5727 | return -ENOENT; |
5728 | ||
f458dd84 | 5729 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5730 | switch (cancel_ret) { |
5731 | case IO_WQ_CANCEL_OK: | |
5732 | ret = 0; | |
5733 | break; | |
5734 | case IO_WQ_CANCEL_RUNNING: | |
5735 | ret = -EALREADY; | |
5736 | break; | |
5737 | case IO_WQ_CANCEL_NOTFOUND: | |
5738 | ret = -ENOENT; | |
5739 | break; | |
5740 | } | |
5741 | ||
e977d6d3 JA |
5742 | return ret; |
5743 | } | |
5744 | ||
47f46768 JA |
5745 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5746 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5747 | int success_ret) |
47f46768 JA |
5748 | { |
5749 | unsigned long flags; | |
5750 | int ret; | |
5751 | ||
f458dd84 | 5752 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
47f46768 | 5753 | spin_lock_irqsave(&ctx->completion_lock, flags); |
df9727af PB |
5754 | if (ret != -ENOENT) |
5755 | goto done; | |
47f46768 JA |
5756 | ret = io_timeout_cancel(ctx, sqe_addr); |
5757 | if (ret != -ENOENT) | |
5758 | goto done; | |
5759 | ret = io_poll_cancel(ctx, sqe_addr); | |
5760 | done: | |
b0dd8a41 JA |
5761 | if (!ret) |
5762 | ret = success_ret; | |
47f46768 JA |
5763 | io_cqring_fill_event(req, ret); |
5764 | io_commit_cqring(ctx); | |
5765 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5766 | io_cqring_ev_posted(ctx); | |
5767 | ||
4e88d6e7 JA |
5768 | if (ret < 0) |
5769 | req_set_fail_links(req); | |
47f46768 JA |
5770 | } |
5771 | ||
3529d8c2 JA |
5772 | static int io_async_cancel_prep(struct io_kiocb *req, |
5773 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5774 | { |
fbf23849 | 5775 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5776 | return -EINVAL; |
61710e43 DA |
5777 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5778 | return -EINVAL; | |
5779 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5780 | return -EINVAL; |
5781 | ||
fbf23849 JA |
5782 | req->cancel.addr = READ_ONCE(sqe->addr); |
5783 | return 0; | |
5784 | } | |
5785 | ||
61e98203 | 5786 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5787 | { |
5788 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5789 | u64 sqe_addr = req->cancel.addr; |
5790 | struct io_tctx_node *node; | |
5791 | int ret; | |
5792 | ||
5793 | /* tasks should wait for their io-wq threads, so safe w/o sync */ | |
5794 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); | |
5795 | spin_lock_irq(&ctx->completion_lock); | |
5796 | if (ret != -ENOENT) | |
5797 | goto done; | |
5798 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5799 | if (ret != -ENOENT) | |
5800 | goto done; | |
5801 | ret = io_poll_cancel(ctx, sqe_addr); | |
5802 | if (ret != -ENOENT) | |
5803 | goto done; | |
5804 | spin_unlock_irq(&ctx->completion_lock); | |
5805 | ||
5806 | /* slow path, try all io-wq's */ | |
5807 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5808 | ret = -ENOENT; | |
5809 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
5810 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 5811 | |
58f99373 PB |
5812 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
5813 | if (ret != -ENOENT) | |
5814 | break; | |
5815 | } | |
5816 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5817 | ||
5818 | spin_lock_irq(&ctx->completion_lock); | |
5819 | done: | |
5820 | io_cqring_fill_event(req, ret); | |
5821 | io_commit_cqring(ctx); | |
5822 | spin_unlock_irq(&ctx->completion_lock); | |
5823 | io_cqring_ev_posted(ctx); | |
5824 | ||
5825 | if (ret < 0) | |
5826 | req_set_fail_links(req); | |
5827 | io_put_req(req); | |
5262f567 JA |
5828 | return 0; |
5829 | } | |
5830 | ||
269bbe5f | 5831 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5832 | const struct io_uring_sqe *sqe) |
5833 | { | |
6ca56f84 JA |
5834 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
5835 | return -EINVAL; | |
61710e43 DA |
5836 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5837 | return -EINVAL; | |
5838 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5839 | return -EINVAL; |
5840 | ||
269bbe5f BM |
5841 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5842 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5843 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5844 | return -EINVAL; |
269bbe5f | 5845 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5846 | return 0; |
5847 | } | |
5848 | ||
889fca73 | 5849 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5850 | { |
5851 | struct io_ring_ctx *ctx = req->ctx; | |
269bbe5f | 5852 | struct io_uring_rsrc_update up; |
05f3fb3c | 5853 | int ret; |
fbf23849 | 5854 | |
45d189c6 | 5855 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5856 | return -EAGAIN; |
05f3fb3c | 5857 | |
269bbe5f BM |
5858 | up.offset = req->rsrc_update.offset; |
5859 | up.data = req->rsrc_update.arg; | |
05f3fb3c JA |
5860 | |
5861 | mutex_lock(&ctx->uring_lock); | |
269bbe5f | 5862 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5863 | mutex_unlock(&ctx->uring_lock); |
5864 | ||
5865 | if (ret < 0) | |
5866 | req_set_fail_links(req); | |
889fca73 | 5867 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5868 | return 0; |
5869 | } | |
5870 | ||
bfe76559 | 5871 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5872 | { |
d625c6ee | 5873 | switch (req->opcode) { |
e781573e | 5874 | case IORING_OP_NOP: |
bfe76559 | 5875 | return 0; |
f67676d1 JA |
5876 | case IORING_OP_READV: |
5877 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5878 | case IORING_OP_READ: |
bfe76559 | 5879 | return io_read_prep(req, sqe); |
f67676d1 JA |
5880 | case IORING_OP_WRITEV: |
5881 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5882 | case IORING_OP_WRITE: |
bfe76559 | 5883 | return io_write_prep(req, sqe); |
0969e783 | 5884 | case IORING_OP_POLL_ADD: |
bfe76559 | 5885 | return io_poll_add_prep(req, sqe); |
0969e783 | 5886 | case IORING_OP_POLL_REMOVE: |
bfe76559 | 5887 | return io_poll_remove_prep(req, sqe); |
8ed8d3c3 | 5888 | case IORING_OP_FSYNC: |
1155c76a | 5889 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5890 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5891 | return io_sfr_prep(req, sqe); |
03b1230c | 5892 | case IORING_OP_SENDMSG: |
fddaface | 5893 | case IORING_OP_SEND: |
bfe76559 | 5894 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5895 | case IORING_OP_RECVMSG: |
fddaface | 5896 | case IORING_OP_RECV: |
bfe76559 | 5897 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5898 | case IORING_OP_CONNECT: |
bfe76559 | 5899 | return io_connect_prep(req, sqe); |
2d28390a | 5900 | case IORING_OP_TIMEOUT: |
bfe76559 | 5901 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5902 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5903 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5904 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5905 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5906 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5907 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5908 | case IORING_OP_ACCEPT: |
bfe76559 | 5909 | return io_accept_prep(req, sqe); |
d63d1b5e | 5910 | case IORING_OP_FALLOCATE: |
bfe76559 | 5911 | return io_fallocate_prep(req, sqe); |
15b71abe | 5912 | case IORING_OP_OPENAT: |
bfe76559 | 5913 | return io_openat_prep(req, sqe); |
b5dba59e | 5914 | case IORING_OP_CLOSE: |
bfe76559 | 5915 | return io_close_prep(req, sqe); |
05f3fb3c | 5916 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5917 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5918 | case IORING_OP_STATX: |
bfe76559 | 5919 | return io_statx_prep(req, sqe); |
4840e418 | 5920 | case IORING_OP_FADVISE: |
bfe76559 | 5921 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5922 | case IORING_OP_MADVISE: |
bfe76559 | 5923 | return io_madvise_prep(req, sqe); |
cebdb986 | 5924 | case IORING_OP_OPENAT2: |
bfe76559 | 5925 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5926 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5927 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5928 | case IORING_OP_SPLICE: |
bfe76559 | 5929 | return io_splice_prep(req, sqe); |
ddf0322d | 5930 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5931 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5932 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5933 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5934 | case IORING_OP_TEE: |
bfe76559 | 5935 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5936 | case IORING_OP_SHUTDOWN: |
5937 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5938 | case IORING_OP_RENAMEAT: |
5939 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5940 | case IORING_OP_UNLINKAT: |
5941 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5942 | } |
5943 | ||
bfe76559 PB |
5944 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5945 | req->opcode); | |
5946 | return-EINVAL; | |
5947 | } | |
5948 | ||
93642ef8 | 5949 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5950 | { |
b7e298d2 PB |
5951 | if (!io_op_defs[req->opcode].needs_async_setup) |
5952 | return 0; | |
5953 | if (WARN_ON_ONCE(req->async_data)) | |
5954 | return -EFAULT; | |
5955 | if (io_alloc_async_data(req)) | |
5956 | return -EAGAIN; | |
5957 | ||
93642ef8 PB |
5958 | switch (req->opcode) { |
5959 | case IORING_OP_READV: | |
93642ef8 PB |
5960 | return io_rw_prep_async(req, READ); |
5961 | case IORING_OP_WRITEV: | |
93642ef8 PB |
5962 | return io_rw_prep_async(req, WRITE); |
5963 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
5964 | return io_sendmsg_prep_async(req); |
5965 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
5966 | return io_recvmsg_prep_async(req); |
5967 | case IORING_OP_CONNECT: | |
5968 | return io_connect_prep_async(req); | |
5969 | } | |
b7e298d2 PB |
5970 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
5971 | req->opcode); | |
5972 | return -EFAULT; | |
f67676d1 JA |
5973 | } |
5974 | ||
9cf7c104 PB |
5975 | static u32 io_get_sequence(struct io_kiocb *req) |
5976 | { | |
5977 | struct io_kiocb *pos; | |
5978 | struct io_ring_ctx *ctx = req->ctx; | |
f2f87370 | 5979 | u32 total_submitted, nr_reqs = 0; |
9cf7c104 | 5980 | |
f2f87370 PB |
5981 | io_for_each_link(pos, req) |
5982 | nr_reqs++; | |
9cf7c104 PB |
5983 | |
5984 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; | |
5985 | return total_submitted - nr_reqs; | |
5986 | } | |
5987 | ||
be7053b7 | 5988 | static int io_req_defer(struct io_kiocb *req) |
de0617e4 | 5989 | { |
a197f664 | 5990 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 5991 | struct io_defer_entry *de; |
f67676d1 | 5992 | int ret; |
9cf7c104 | 5993 | u32 seq; |
de0617e4 | 5994 | |
9d858b21 | 5995 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 PB |
5996 | if (likely(list_empty_careful(&ctx->defer_list) && |
5997 | !(req->flags & REQ_F_IO_DRAIN))) | |
5998 | return 0; | |
5999 | ||
6000 | seq = io_get_sequence(req); | |
6001 | /* Still a chance to pass the sequence check */ | |
6002 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
de0617e4 JA |
6003 | return 0; |
6004 | ||
b7e298d2 | 6005 | ret = io_req_prep_async(req); |
be7053b7 PB |
6006 | if (ret) |
6007 | return ret; | |
cbdcb435 | 6008 | io_prep_async_link(req); |
27dc8338 PB |
6009 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
6010 | if (!de) | |
6011 | return -ENOMEM; | |
2d28390a | 6012 | |
de0617e4 | 6013 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6014 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6015 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6016 | kfree(de); |
ae34817b PB |
6017 | io_queue_async_work(req); |
6018 | return -EIOCBQUEUED; | |
de0617e4 JA |
6019 | } |
6020 | ||
915967f6 | 6021 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6022 | de->req = req; |
9cf7c104 | 6023 | de->seq = seq; |
27dc8338 | 6024 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 JA |
6025 | spin_unlock_irq(&ctx->completion_lock); |
6026 | return -EIOCBQUEUED; | |
6027 | } | |
6028 | ||
68fb8979 | 6029 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6030 | { |
0e1b6fe3 PB |
6031 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6032 | switch (req->opcode) { | |
6033 | case IORING_OP_READV: | |
6034 | case IORING_OP_READ_FIXED: | |
6035 | case IORING_OP_READ: | |
bcda7baa | 6036 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6037 | break; |
6038 | case IORING_OP_RECVMSG: | |
6039 | case IORING_OP_RECV: | |
bcda7baa | 6040 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6041 | break; |
6042 | } | |
6043 | req->flags &= ~REQ_F_BUFFER_SELECTED; | |
99bc4c38 PB |
6044 | } |
6045 | ||
0e1b6fe3 PB |
6046 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6047 | switch (req->opcode) { | |
6048 | case IORING_OP_READV: | |
6049 | case IORING_OP_READ_FIXED: | |
6050 | case IORING_OP_READ: | |
6051 | case IORING_OP_WRITEV: | |
6052 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6053 | case IORING_OP_WRITE: { |
6054 | struct io_async_rw *io = req->async_data; | |
6055 | if (io->free_iovec) | |
6056 | kfree(io->free_iovec); | |
0e1b6fe3 | 6057 | break; |
e8c2bc1f | 6058 | } |
0e1b6fe3 | 6059 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6060 | case IORING_OP_SENDMSG: { |
6061 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6062 | |
6063 | kfree(io->free_iov); | |
0e1b6fe3 | 6064 | break; |
e8c2bc1f | 6065 | } |
0e1b6fe3 PB |
6066 | case IORING_OP_SPLICE: |
6067 | case IORING_OP_TEE: | |
e1d767f0 PB |
6068 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6069 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6070 | break; |
f3cd4850 JA |
6071 | case IORING_OP_OPENAT: |
6072 | case IORING_OP_OPENAT2: | |
6073 | if (req->open.filename) | |
6074 | putname(req->open.filename); | |
6075 | break; | |
80a261fd JA |
6076 | case IORING_OP_RENAMEAT: |
6077 | putname(req->rename.oldpath); | |
6078 | putname(req->rename.newpath); | |
6079 | break; | |
14a1143b JA |
6080 | case IORING_OP_UNLINKAT: |
6081 | putname(req->unlink.filename); | |
6082 | break; | |
0e1b6fe3 PB |
6083 | } |
6084 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
99bc4c38 | 6085 | } |
99bc4c38 PB |
6086 | } |
6087 | ||
889fca73 | 6088 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6089 | { |
a197f664 | 6090 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6091 | const struct cred *creds = NULL; |
d625c6ee | 6092 | int ret; |
2b188cc1 | 6093 | |
003e8dcc JA |
6094 | if (req->work.creds && req->work.creds != current_cred()) |
6095 | creds = override_creds(req->work.creds); | |
5730b27e | 6096 | |
d625c6ee | 6097 | switch (req->opcode) { |
2b188cc1 | 6098 | case IORING_OP_NOP: |
889fca73 | 6099 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6100 | break; |
6101 | case IORING_OP_READV: | |
edafccee | 6102 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6103 | case IORING_OP_READ: |
889fca73 | 6104 | ret = io_read(req, issue_flags); |
edafccee | 6105 | break; |
3529d8c2 | 6106 | case IORING_OP_WRITEV: |
edafccee | 6107 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6108 | case IORING_OP_WRITE: |
889fca73 | 6109 | ret = io_write(req, issue_flags); |
2b188cc1 | 6110 | break; |
c992fe29 | 6111 | case IORING_OP_FSYNC: |
45d189c6 | 6112 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6113 | break; |
221c5eb2 | 6114 | case IORING_OP_POLL_ADD: |
61e98203 | 6115 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6116 | break; |
6117 | case IORING_OP_POLL_REMOVE: | |
61e98203 | 6118 | ret = io_poll_remove(req, issue_flags); |
221c5eb2 | 6119 | break; |
5d17b4a4 | 6120 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6121 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6122 | break; |
0fa03c62 | 6123 | case IORING_OP_SENDMSG: |
889fca73 | 6124 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6125 | break; |
fddaface | 6126 | case IORING_OP_SEND: |
889fca73 | 6127 | ret = io_send(req, issue_flags); |
0fa03c62 | 6128 | break; |
aa1fa28f | 6129 | case IORING_OP_RECVMSG: |
889fca73 | 6130 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6131 | break; |
fddaface | 6132 | case IORING_OP_RECV: |
889fca73 | 6133 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6134 | break; |
5262f567 | 6135 | case IORING_OP_TIMEOUT: |
61e98203 | 6136 | ret = io_timeout(req, issue_flags); |
5262f567 | 6137 | break; |
11365043 | 6138 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6139 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6140 | break; |
17f2fe35 | 6141 | case IORING_OP_ACCEPT: |
889fca73 | 6142 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6143 | break; |
f8e85cf2 | 6144 | case IORING_OP_CONNECT: |
889fca73 | 6145 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6146 | break; |
62755e35 | 6147 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6148 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6149 | break; |
d63d1b5e | 6150 | case IORING_OP_FALLOCATE: |
45d189c6 | 6151 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6152 | break; |
15b71abe | 6153 | case IORING_OP_OPENAT: |
45d189c6 | 6154 | ret = io_openat(req, issue_flags); |
15b71abe | 6155 | break; |
b5dba59e | 6156 | case IORING_OP_CLOSE: |
889fca73 | 6157 | ret = io_close(req, issue_flags); |
b5dba59e | 6158 | break; |
05f3fb3c | 6159 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6160 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6161 | break; |
eddc7ef5 | 6162 | case IORING_OP_STATX: |
45d189c6 | 6163 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6164 | break; |
4840e418 | 6165 | case IORING_OP_FADVISE: |
45d189c6 | 6166 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6167 | break; |
c1ca757b | 6168 | case IORING_OP_MADVISE: |
45d189c6 | 6169 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6170 | break; |
cebdb986 | 6171 | case IORING_OP_OPENAT2: |
45d189c6 | 6172 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6173 | break; |
3e4827b0 | 6174 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6175 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6176 | break; |
7d67af2c | 6177 | case IORING_OP_SPLICE: |
45d189c6 | 6178 | ret = io_splice(req, issue_flags); |
7d67af2c | 6179 | break; |
ddf0322d | 6180 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6181 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6182 | break; |
067524e9 | 6183 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6184 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6185 | break; |
f2a8d5c7 | 6186 | case IORING_OP_TEE: |
45d189c6 | 6187 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6188 | break; |
36f4fa68 | 6189 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6190 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6191 | break; |
80a261fd | 6192 | case IORING_OP_RENAMEAT: |
45d189c6 | 6193 | ret = io_renameat(req, issue_flags); |
80a261fd | 6194 | break; |
14a1143b | 6195 | case IORING_OP_UNLINKAT: |
45d189c6 | 6196 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6197 | break; |
2b188cc1 JA |
6198 | default: |
6199 | ret = -EINVAL; | |
6200 | break; | |
6201 | } | |
6202 | ||
5730b27e JA |
6203 | if (creds) |
6204 | revert_creds(creds); | |
6205 | ||
def596e9 JA |
6206 | if (ret) |
6207 | return ret; | |
6208 | ||
b532576e JA |
6209 | /* If the op doesn't have a file, we're not polling for it */ |
6210 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { | |
11ba820b JA |
6211 | const bool in_async = io_wq_current_is_worker(); |
6212 | ||
11ba820b JA |
6213 | /* workqueue context doesn't hold uring_lock, grab it now */ |
6214 | if (in_async) | |
6215 | mutex_lock(&ctx->uring_lock); | |
6216 | ||
2e9dbe90 | 6217 | io_iopoll_req_issued(req, in_async); |
11ba820b JA |
6218 | |
6219 | if (in_async) | |
6220 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
6221 | } |
6222 | ||
6223 | return 0; | |
2b188cc1 JA |
6224 | } |
6225 | ||
5280f7e5 | 6226 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6227 | { |
6228 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6229 | struct io_kiocb *timeout; |
561fb04a | 6230 | int ret = 0; |
2b188cc1 | 6231 | |
6df1db6b PB |
6232 | timeout = io_prep_linked_timeout(req); |
6233 | if (timeout) | |
6234 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6235 | |
4014d943 | 6236 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6237 | ret = -ECANCELED; |
31b51510 | 6238 | |
561fb04a | 6239 | if (!ret) { |
561fb04a | 6240 | do { |
889fca73 | 6241 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6242 | /* |
6243 | * We can get EAGAIN for polled IO even though we're | |
6244 | * forcing a sync submission from here, since we can't | |
6245 | * wait for request slots on the block side. | |
6246 | */ | |
6247 | if (ret != -EAGAIN) | |
6248 | break; | |
6249 | cond_resched(); | |
6250 | } while (1); | |
6251 | } | |
31b51510 | 6252 | |
a3df7698 | 6253 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6254 | if (ret) { |
a3df7698 | 6255 | /* io-wq is going to take one down */ |
de9b4cca | 6256 | req_ref_get(req); |
a3df7698 | 6257 | io_req_task_queue_fail(req, ret); |
edafccee | 6258 | } |
2b188cc1 JA |
6259 | } |
6260 | ||
7b29f92d JA |
6261 | #define FFS_ASYNC_READ 0x1UL |
6262 | #define FFS_ASYNC_WRITE 0x2UL | |
6263 | #ifdef CONFIG_64BIT | |
6264 | #define FFS_ISREG 0x4UL | |
6265 | #else | |
6266 | #define FFS_ISREG 0x0UL | |
6267 | #endif | |
6268 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
6269 | ||
a04b0ac0 PB |
6270 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_rsrc_data *file_data, |
6271 | unsigned i) | |
65e19f54 | 6272 | { |
269bbe5f | 6273 | struct fixed_rsrc_table *table; |
65e19f54 | 6274 | |
dafecf19 PB |
6275 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
6276 | return &table->files[i & IORING_FILE_TABLE_MASK]; | |
6277 | } | |
6278 | ||
6279 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, | |
6280 | int index) | |
6281 | { | |
a04b0ac0 | 6282 | struct io_fixed_file *slot = io_fixed_file_slot(ctx->file_data, index); |
7b29f92d | 6283 | |
a04b0ac0 | 6284 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6285 | } |
6286 | ||
a04b0ac0 | 6287 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6288 | { |
6289 | unsigned long file_ptr = (unsigned long) file; | |
6290 | ||
6291 | if (__io_file_supports_async(file, READ)) | |
6292 | file_ptr |= FFS_ASYNC_READ; | |
6293 | if (__io_file_supports_async(file, WRITE)) | |
6294 | file_ptr |= FFS_ASYNC_WRITE; | |
6295 | if (S_ISREG(file_inode(file)->i_mode)) | |
6296 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6297 | file_slot->file_ptr = file_ptr; |
9a321c98 PB |
6298 | } |
6299 | ||
8371adf5 PB |
6300 | static struct file *io_file_get(struct io_submit_state *state, |
6301 | struct io_kiocb *req, int fd, bool fixed) | |
09bb8394 | 6302 | { |
a197f664 | 6303 | struct io_ring_ctx *ctx = req->ctx; |
8da11c19 | 6304 | struct file *file; |
09bb8394 | 6305 | |
8da11c19 | 6306 | if (fixed) { |
7b29f92d JA |
6307 | unsigned long file_ptr; |
6308 | ||
479f517b | 6309 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
8371adf5 | 6310 | return NULL; |
b7620121 | 6311 | fd = array_index_nospec(fd, ctx->nr_user_files); |
a04b0ac0 | 6312 | file_ptr = io_fixed_file_slot(ctx->file_data, fd)->file_ptr; |
7b29f92d JA |
6313 | file = (struct file *) (file_ptr & FFS_MASK); |
6314 | file_ptr &= ~FFS_MASK; | |
6315 | /* mask in overlapping REQ_F and FFS bits */ | |
6316 | req->flags |= (file_ptr << REQ_F_ASYNC_READ_BIT); | |
b895c9a6 | 6317 | io_req_set_rsrc_node(req); |
09bb8394 | 6318 | } else { |
c826bd7a | 6319 | trace_io_uring_file_get(ctx, fd); |
8da11c19 | 6320 | file = __io_file_get(state, fd); |
d44f554e JA |
6321 | |
6322 | /* we don't allow fixed io_uring files */ | |
6323 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6324 | io_req_track_inflight(req); | |
09bb8394 JA |
6325 | } |
6326 | ||
8371adf5 | 6327 | return file; |
09bb8394 JA |
6328 | } |
6329 | ||
2665abfd | 6330 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6331 | { |
ad8a48ac JA |
6332 | struct io_timeout_data *data = container_of(timer, |
6333 | struct io_timeout_data, timer); | |
90cd7e42 | 6334 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6335 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6336 | unsigned long flags; |
2665abfd JA |
6337 | |
6338 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6339 | prev = req->timeout.head; |
6340 | req->timeout.head = NULL; | |
2665abfd JA |
6341 | |
6342 | /* | |
6343 | * We don't expect the list to be empty, that will only happen if we | |
6344 | * race with the completion of the linked work. | |
6345 | */ | |
de9b4cca | 6346 | if (prev && req_ref_inc_not_zero(prev)) |
f2f87370 | 6347 | io_remove_next_linked(prev); |
90cd7e42 PB |
6348 | else |
6349 | prev = NULL; | |
2665abfd JA |
6350 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6351 | ||
6352 | if (prev) { | |
014db007 | 6353 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6354 | io_put_req_deferred(prev, 1); |
47f46768 | 6355 | } else { |
9ae1f8dd | 6356 | io_req_complete_post(req, -ETIME, 0); |
2665abfd | 6357 | } |
df9727af | 6358 | io_put_req_deferred(req, 1); |
2665abfd JA |
6359 | return HRTIMER_NORESTART; |
6360 | } | |
6361 | ||
de968c18 | 6362 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6363 | { |
de968c18 PB |
6364 | struct io_ring_ctx *ctx = req->ctx; |
6365 | ||
6366 | spin_lock_irq(&ctx->completion_lock); | |
76a46e06 | 6367 | /* |
f2f87370 PB |
6368 | * If the back reference is NULL, then our linked request finished |
6369 | * before we got a chance to setup the timer | |
76a46e06 | 6370 | */ |
90cd7e42 | 6371 | if (req->timeout.head) { |
e8c2bc1f | 6372 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6373 | |
ad8a48ac JA |
6374 | data->timer.function = io_link_timeout_fn; |
6375 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6376 | data->mode); | |
2665abfd | 6377 | } |
76a46e06 | 6378 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6379 | /* drop submission reference */ |
76a46e06 JA |
6380 | io_put_req(req); |
6381 | } | |
2665abfd | 6382 | |
ad8a48ac | 6383 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6384 | { |
f2f87370 | 6385 | struct io_kiocb *nxt = req->link; |
2665abfd | 6386 | |
f2f87370 PB |
6387 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6388 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6389 | return NULL; |
2665abfd | 6390 | |
90cd7e42 | 6391 | nxt->timeout.head = req; |
900fad45 | 6392 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6393 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6394 | return nxt; |
2665abfd JA |
6395 | } |
6396 | ||
c5eef2b9 | 6397 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6398 | { |
d3d7298d | 6399 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
e0c5c576 | 6400 | int ret; |
2b188cc1 | 6401 | |
c5eef2b9 | 6402 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6403 | |
491381ce JA |
6404 | /* |
6405 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6406 | * doesn't support non-blocking read/write attempts | |
6407 | */ | |
1840038e | 6408 | if (likely(!ret)) { |
0d63c148 | 6409 | /* drop submission reference */ |
e342c807 | 6410 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6411 | struct io_ring_ctx *ctx = req->ctx; |
6412 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6413 | |
6dd0be1e | 6414 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6415 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
c5eef2b9 | 6416 | io_submit_flush_completions(cs, ctx); |
9affd664 | 6417 | } else { |
d3d7298d | 6418 | io_put_req(req); |
0d63c148 | 6419 | } |
1840038e PB |
6420 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
6421 | if (!io_arm_poll_handler(req)) { | |
6422 | /* | |
6423 | * Queued up for async execution, worker will release | |
6424 | * submit reference when the iocb is actually submitted. | |
6425 | */ | |
6426 | io_queue_async_work(req); | |
6427 | } | |
0d63c148 | 6428 | } else { |
f41db273 | 6429 | io_req_complete_failed(req, ret); |
9e645e11 | 6430 | } |
d3d7298d PB |
6431 | if (linked_timeout) |
6432 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6433 | } |
6434 | ||
be7053b7 | 6435 | static void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 JL |
6436 | { |
6437 | int ret; | |
6438 | ||
be7053b7 | 6439 | ret = io_req_defer(req); |
4fe2c963 JL |
6440 | if (ret) { |
6441 | if (ret != -EIOCBQUEUED) { | |
1118591a | 6442 | fail_req: |
f41db273 | 6443 | io_req_complete_failed(req, ret); |
4fe2c963 | 6444 | } |
2550878f | 6445 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
b7e298d2 | 6446 | ret = io_req_prep_async(req); |
be7053b7 PB |
6447 | if (unlikely(ret)) |
6448 | goto fail_req; | |
ce35a47a JA |
6449 | io_queue_async_work(req); |
6450 | } else { | |
c5eef2b9 | 6451 | __io_queue_sqe(req); |
ce35a47a | 6452 | } |
4fe2c963 JL |
6453 | } |
6454 | ||
b16fed66 PB |
6455 | /* |
6456 | * Check SQE restrictions (opcode and flags). | |
6457 | * | |
6458 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6459 | */ | |
6460 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6461 | struct io_kiocb *req, | |
6462 | unsigned int sqe_flags) | |
4fe2c963 | 6463 | { |
b16fed66 PB |
6464 | if (!ctx->restricted) |
6465 | return true; | |
6466 | ||
6467 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6468 | return false; | |
6469 | ||
6470 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6471 | ctx->restrictions.sqe_flags_required) | |
6472 | return false; | |
6473 | ||
6474 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6475 | ctx->restrictions.sqe_flags_required)) | |
6476 | return false; | |
6477 | ||
6478 | return true; | |
4fe2c963 JL |
6479 | } |
6480 | ||
b16fed66 PB |
6481 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6482 | const struct io_uring_sqe *sqe) | |
6483 | { | |
6484 | struct io_submit_state *state; | |
6485 | unsigned int sqe_flags; | |
003e8dcc | 6486 | int personality, ret = 0; |
b16fed66 PB |
6487 | |
6488 | req->opcode = READ_ONCE(sqe->opcode); | |
6489 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6490 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6491 | req->user_data = READ_ONCE(sqe->user_data); | |
6492 | req->async_data = NULL; | |
6493 | req->file = NULL; | |
6494 | req->ctx = ctx; | |
6495 | req->link = NULL; | |
6496 | req->fixed_rsrc_refs = NULL; | |
6497 | /* one is dropped after submission, the other at completion */ | |
abc54d63 | 6498 | atomic_set(&req->refs, 2); |
b16fed66 PB |
6499 | req->task = current; |
6500 | req->result = 0; | |
93e68e03 | 6501 | req->work.creds = NULL; |
b16fed66 PB |
6502 | |
6503 | /* enforce forwards compatibility on users */ | |
ebf4a5db PB |
6504 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
6505 | req->flags = 0; | |
b16fed66 | 6506 | return -EINVAL; |
ebf4a5db | 6507 | } |
b16fed66 PB |
6508 | |
6509 | if (unlikely(req->opcode >= IORING_OP_LAST)) | |
6510 | return -EINVAL; | |
6511 | ||
b16fed66 PB |
6512 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
6513 | return -EACCES; | |
6514 | ||
6515 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6516 | !io_op_defs[req->opcode].buffer_select) | |
6517 | return -EOPNOTSUPP; | |
863e0560 | 6518 | |
003e8dcc JA |
6519 | personality = READ_ONCE(sqe->personality); |
6520 | if (personality) { | |
61cf9370 | 6521 | req->work.creds = xa_load(&ctx->personalities, personality); |
003e8dcc JA |
6522 | if (!req->work.creds) |
6523 | return -EINVAL; | |
6524 | get_cred(req->work.creds); | |
003e8dcc | 6525 | } |
b16fed66 PB |
6526 | state = &ctx->submit_state; |
6527 | ||
6528 | /* | |
6529 | * Plug now if we have more than 1 IO left after this, and the target | |
6530 | * is potentially a read/write to block based storage. | |
6531 | */ | |
6532 | if (!state->plug_started && state->ios_left > 1 && | |
6533 | io_op_defs[req->opcode].plug) { | |
6534 | blk_start_plug(&state->plug); | |
6535 | state->plug_started = true; | |
6536 | } | |
6537 | ||
6538 | if (io_op_defs[req->opcode].needs_file) { | |
6539 | bool fixed = req->flags & REQ_F_FIXED_FILE; | |
6540 | ||
6541 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); | |
6542 | if (unlikely(!req->file)) | |
6543 | ret = -EBADF; | |
6544 | } | |
6545 | ||
6546 | state->ios_left--; | |
6547 | return ret; | |
6548 | } | |
6549 | ||
a6b8cadc | 6550 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6551 | const struct io_uring_sqe *sqe) |
9e645e11 | 6552 | { |
a1ab7b35 | 6553 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6554 | int ret; |
9e645e11 | 6555 | |
a6b8cadc PB |
6556 | ret = io_init_req(ctx, req, sqe); |
6557 | if (unlikely(ret)) { | |
6558 | fail_req: | |
de59bc10 PB |
6559 | if (link->head) { |
6560 | /* fail even hard links since we don't submit */ | |
cf109604 | 6561 | link->head->flags |= REQ_F_FAIL_LINK; |
f41db273 | 6562 | io_req_complete_failed(link->head, -ECANCELED); |
de59bc10 PB |
6563 | link->head = NULL; |
6564 | } | |
f41db273 | 6565 | io_req_complete_failed(req, ret); |
a6b8cadc PB |
6566 | return ret; |
6567 | } | |
be7053b7 PB |
6568 | ret = io_req_prep(req, sqe); |
6569 | if (unlikely(ret)) | |
6570 | goto fail_req; | |
a6b8cadc | 6571 | |
be7053b7 | 6572 | /* don't need @sqe from now on */ |
a6b8cadc PB |
6573 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
6574 | true, ctx->flags & IORING_SETUP_SQPOLL); | |
6575 | ||
9e645e11 JA |
6576 | /* |
6577 | * If we already have a head request, queue this one for async | |
6578 | * submittal once the head completes. If we don't have a head but | |
6579 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6580 | * submitted sync once the chain is complete. If none of those | |
6581 | * conditions are true (normal request), then just queue it. | |
6582 | */ | |
863e0560 PB |
6583 | if (link->head) { |
6584 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6585 | |
8cdf2193 PB |
6586 | /* |
6587 | * Taking sequential execution of a link, draining both sides | |
6588 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
6589 | * requests in the link. So, it drains the head and the | |
6590 | * next after the link request. The last one is done via | |
6591 | * drain_next flag to persist the effect across calls. | |
6592 | */ | |
ef4ff581 | 6593 | if (req->flags & REQ_F_IO_DRAIN) { |
711be031 PB |
6594 | head->flags |= REQ_F_IO_DRAIN; |
6595 | ctx->drain_next = 1; | |
6596 | } | |
b7e298d2 | 6597 | ret = io_req_prep_async(req); |
cf109604 | 6598 | if (unlikely(ret)) |
a6b8cadc | 6599 | goto fail_req; |
9d76377f | 6600 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6601 | link->last->link = req; |
863e0560 | 6602 | link->last = req; |
32fe525b PB |
6603 | |
6604 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6605 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
de59bc10 | 6606 | io_queue_sqe(head); |
863e0560 | 6607 | link->head = NULL; |
32fe525b | 6608 | } |
9e645e11 | 6609 | } else { |
711be031 PB |
6610 | if (unlikely(ctx->drain_next)) { |
6611 | req->flags |= REQ_F_IO_DRAIN; | |
ef4ff581 | 6612 | ctx->drain_next = 0; |
711be031 | 6613 | } |
ef4ff581 | 6614 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6615 | link->head = req; |
6616 | link->last = req; | |
711be031 | 6617 | } else { |
be7053b7 | 6618 | io_queue_sqe(req); |
711be031 | 6619 | } |
9e645e11 | 6620 | } |
2e6e1fde | 6621 | |
1d4240cc | 6622 | return 0; |
9e645e11 JA |
6623 | } |
6624 | ||
9a56a232 JA |
6625 | /* |
6626 | * Batched submission is done, ensure local IO is flushed out. | |
6627 | */ | |
ba88ff11 PB |
6628 | static void io_submit_state_end(struct io_submit_state *state, |
6629 | struct io_ring_ctx *ctx) | |
9a56a232 | 6630 | { |
a1ab7b35 | 6631 | if (state->link.head) |
de59bc10 | 6632 | io_queue_sqe(state->link.head); |
6dd0be1e | 6633 | if (state->comp.nr) |
ba88ff11 | 6634 | io_submit_flush_completions(&state->comp, ctx); |
27926b68 JA |
6635 | if (state->plug_started) |
6636 | blk_finish_plug(&state->plug); | |
9f13c35b | 6637 | io_state_file_put(state); |
9a56a232 JA |
6638 | } |
6639 | ||
6640 | /* | |
6641 | * Start submission side cache. | |
6642 | */ | |
6643 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6644 | unsigned int max_ios) |
9a56a232 | 6645 | { |
27926b68 | 6646 | state->plug_started = false; |
9a56a232 | 6647 | state->ios_left = max_ios; |
a1ab7b35 PB |
6648 | /* set only head, no need to init link_last in advance */ |
6649 | state->link.head = NULL; | |
9a56a232 JA |
6650 | } |
6651 | ||
2b188cc1 JA |
6652 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6653 | { | |
75b28aff | 6654 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6655 | |
caf582c6 PB |
6656 | /* |
6657 | * Ensure any loads from the SQEs are done at this point, | |
6658 | * since once we write the new head, the application could | |
6659 | * write new data to them. | |
6660 | */ | |
6661 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6662 | } |
6663 | ||
2b188cc1 | 6664 | /* |
3529d8c2 | 6665 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
6666 | * that is mapped by userspace. This means that care needs to be taken to |
6667 | * ensure that reads are stable, as we cannot rely on userspace always | |
6668 | * being a good citizen. If members of the sqe are validated and then later | |
6669 | * used, it's important that those reads are done through READ_ONCE() to | |
6670 | * prevent a re-load down the line. | |
6671 | */ | |
709b302f | 6672 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6673 | { |
75b28aff | 6674 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
6675 | unsigned head; |
6676 | ||
6677 | /* | |
6678 | * The cached sq head (or cq tail) serves two purposes: | |
6679 | * | |
6680 | * 1) allows us to batch the cost of updating the user visible | |
6681 | * head updates. | |
6682 | * 2) allows the kernel side to track the head on its own, even | |
6683 | * though the application is the one updating it. | |
6684 | */ | |
4fccfcbb | 6685 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
709b302f PB |
6686 | if (likely(head < ctx->sq_entries)) |
6687 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6688 | |
6689 | /* drop invalid entries */ | |
498ccd9e | 6690 | ctx->cached_sq_dropped++; |
ee7d46d9 | 6691 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
709b302f PB |
6692 | return NULL; |
6693 | } | |
6694 | ||
0f212204 | 6695 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6696 | { |
46c4e16a | 6697 | int submitted = 0; |
6c271ce2 | 6698 | |
c4a2ed72 | 6699 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 | 6700 | if (test_bit(0, &ctx->sq_check_overflow)) { |
6c2450ae | 6701 | if (!__io_cqring_overflow_flush(ctx, false)) |
ad3eb2c8 JA |
6702 | return -EBUSY; |
6703 | } | |
6c271ce2 | 6704 | |
ee7d46d9 PB |
6705 | /* make sure SQ entry isn't read before tail */ |
6706 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 6707 | |
2b85edfc PB |
6708 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6709 | return -EAGAIN; | |
6c271ce2 | 6710 | |
d8a6df10 | 6711 | percpu_counter_add(¤t->io_uring->inflight, nr); |
faf7b51c | 6712 | refcount_add(nr, ¤t->usage); |
ba88ff11 | 6713 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6714 | |
46c4e16a | 6715 | while (submitted < nr) { |
3529d8c2 | 6716 | const struct io_uring_sqe *sqe; |
196be95c | 6717 | struct io_kiocb *req; |
fb5ccc98 | 6718 | |
258b29a9 | 6719 | req = io_alloc_req(ctx); |
196be95c PB |
6720 | if (unlikely(!req)) { |
6721 | if (!submitted) | |
6722 | submitted = -EAGAIN; | |
fb5ccc98 | 6723 | break; |
196be95c | 6724 | } |
4fccfcbb PB |
6725 | sqe = io_get_sqe(ctx); |
6726 | if (unlikely(!sqe)) { | |
6727 | kmem_cache_free(req_cachep, req); | |
6728 | break; | |
6729 | } | |
d3656344 JA |
6730 | /* will complete beyond this point, count as submitted */ |
6731 | submitted++; | |
a1ab7b35 | 6732 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6733 | break; |
6c271ce2 JA |
6734 | } |
6735 | ||
9466f437 PB |
6736 | if (unlikely(submitted != nr)) { |
6737 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 JA |
6738 | struct io_uring_task *tctx = current->io_uring; |
6739 | int unused = nr - ref_used; | |
9466f437 | 6740 | |
d8a6df10 JA |
6741 | percpu_ref_put_many(&ctx->refs, unused); |
6742 | percpu_counter_sub(&tctx->inflight, unused); | |
6743 | put_task_struct_many(current, unused); | |
9466f437 | 6744 | } |
6c271ce2 | 6745 | |
a1ab7b35 | 6746 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6747 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6748 | io_commit_sqring(ctx); | |
6749 | ||
6c271ce2 JA |
6750 | return submitted; |
6751 | } | |
6752 | ||
23b3628e XW |
6753 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6754 | { | |
6755 | /* Tell userspace we may need a wakeup call */ | |
6756 | spin_lock_irq(&ctx->completion_lock); | |
6757 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; | |
6758 | spin_unlock_irq(&ctx->completion_lock); | |
6759 | } | |
6760 | ||
6761 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6762 | { | |
6763 | spin_lock_irq(&ctx->completion_lock); | |
6764 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; | |
6765 | spin_unlock_irq(&ctx->completion_lock); | |
6766 | } | |
6767 | ||
08369246 | 6768 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6769 | { |
c8d1ba58 | 6770 | unsigned int to_submit; |
bdcd3eab | 6771 | int ret = 0; |
6c271ce2 | 6772 | |
c8d1ba58 | 6773 | to_submit = io_sqring_entries(ctx); |
e95eee2d JA |
6774 | /* if we're handling multiple rings, cap submit size for fairness */ |
6775 | if (cap_entries && to_submit > 8) | |
6776 | to_submit = 8; | |
6777 | ||
906a3c6f | 6778 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6779 | unsigned nr_events = 0; |
a4c0b3de | 6780 | |
c8d1ba58 | 6781 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6782 | if (!list_empty(&ctx->iopoll_list)) |
c8d1ba58 | 6783 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6784 | |
0298ef96 PB |
6785 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
6786 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 6787 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 JA |
6788 | mutex_unlock(&ctx->uring_lock); |
6789 | } | |
6c271ce2 | 6790 | |
90554200 JA |
6791 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6792 | wake_up(&ctx->sqo_sq_wait); | |
6c271ce2 | 6793 | |
08369246 XW |
6794 | return ret; |
6795 | } | |
6c271ce2 | 6796 | |
08369246 XW |
6797 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6798 | { | |
6799 | struct io_ring_ctx *ctx; | |
6800 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6801 | |
c9dca27d PB |
6802 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6803 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 6804 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6805 | } |
6c271ce2 | 6806 | |
c8d1ba58 JA |
6807 | static int io_sq_thread(void *data) |
6808 | { | |
69fb2131 JA |
6809 | struct io_sq_data *sqd = data; |
6810 | struct io_ring_ctx *ctx; | |
a0d9205f | 6811 | unsigned long timeout = 0; |
37d1e2e3 | 6812 | char buf[TASK_COMM_LEN]; |
08369246 | 6813 | DEFINE_WAIT(wait); |
6c271ce2 | 6814 | |
696ee88a | 6815 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 6816 | set_task_comm(current, buf); |
37d1e2e3 JA |
6817 | current->pf_io_worker = NULL; |
6818 | ||
6819 | if (sqd->sq_cpu != -1) | |
6820 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6821 | else | |
6822 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6823 | current->flags |= PF_NO_SETAFFINITY; | |
6824 | ||
09a6f4ef | 6825 | mutex_lock(&sqd->lock); |
05962f95 | 6826 | while (!test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) { |
08369246 XW |
6827 | int ret; |
6828 | bool cap_entries, sqt_spin, needs_sched; | |
c1edbf5f | 6829 | |
82734c5b JA |
6830 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
6831 | signal_pending(current)) { | |
6832 | bool did_sig = false; | |
6833 | ||
09a6f4ef | 6834 | mutex_unlock(&sqd->lock); |
82734c5b JA |
6835 | if (signal_pending(current)) { |
6836 | struct ksignal ksig; | |
6837 | ||
6838 | did_sig = get_signal(&ksig); | |
6839 | } | |
05962f95 | 6840 | cond_resched(); |
09a6f4ef | 6841 | mutex_lock(&sqd->lock); |
82734c5b JA |
6842 | if (did_sig) |
6843 | break; | |
521d6a73 | 6844 | io_run_task_work(); |
b7f5a0bf | 6845 | io_run_task_work_head(&sqd->park_task_work); |
08369246 | 6846 | timeout = jiffies + sqd->sq_thread_idle; |
7d41e854 | 6847 | continue; |
08369246 | 6848 | } |
08369246 | 6849 | sqt_spin = false; |
e95eee2d | 6850 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6851 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
7c30f36a SM |
6852 | const struct cred *creds = NULL; |
6853 | ||
6854 | if (ctx->sq_creds != current_cred()) | |
6855 | creds = override_creds(ctx->sq_creds); | |
08369246 | 6856 | ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a SM |
6857 | if (creds) |
6858 | revert_creds(creds); | |
08369246 XW |
6859 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
6860 | sqt_spin = true; | |
69fb2131 | 6861 | } |
6c271ce2 | 6862 | |
08369246 | 6863 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 JA |
6864 | io_run_task_work(); |
6865 | cond_resched(); | |
08369246 XW |
6866 | if (sqt_spin) |
6867 | timeout = jiffies + sqd->sq_thread_idle; | |
6868 | continue; | |
6869 | } | |
6870 | ||
08369246 XW |
6871 | needs_sched = true; |
6872 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); | |
6873 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { | |
6874 | if ((ctx->flags & IORING_SETUP_IOPOLL) && | |
6875 | !list_empty_careful(&ctx->iopoll_list)) { | |
6876 | needs_sched = false; | |
6877 | break; | |
6878 | } | |
6879 | if (io_sqring_entries(ctx)) { | |
6880 | needs_sched = false; | |
6881 | break; | |
6882 | } | |
6883 | } | |
6884 | ||
05962f95 | 6885 | if (needs_sched && !test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) { |
69fb2131 JA |
6886 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6887 | io_ring_set_wakeup_flag(ctx); | |
08369246 | 6888 | |
09a6f4ef | 6889 | mutex_unlock(&sqd->lock); |
69fb2131 | 6890 | schedule(); |
09a6f4ef | 6891 | mutex_lock(&sqd->lock); |
69fb2131 JA |
6892 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6893 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6894 | } |
08369246 XW |
6895 | |
6896 | finish_wait(&sqd->wait, &wait); | |
b7f5a0bf | 6897 | io_run_task_work_head(&sqd->park_task_work); |
08369246 | 6898 | timeout = jiffies + sqd->sq_thread_idle; |
6c271ce2 | 6899 | } |
28cea78a | 6900 | |
521d6a73 PB |
6901 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6902 | io_uring_cancel_sqpoll(ctx); | |
37d1e2e3 | 6903 | sqd->thread = NULL; |
05962f95 | 6904 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 6905 | io_ring_set_wakeup_flag(ctx); |
09a6f4ef | 6906 | mutex_unlock(&sqd->lock); |
521d6a73 PB |
6907 | |
6908 | io_run_task_work(); | |
b7f5a0bf | 6909 | io_run_task_work_head(&sqd->park_task_work); |
37d1e2e3 JA |
6910 | complete(&sqd->exited); |
6911 | do_exit(0); | |
6c271ce2 JA |
6912 | } |
6913 | ||
bda52162 JA |
6914 | struct io_wait_queue { |
6915 | struct wait_queue_entry wq; | |
6916 | struct io_ring_ctx *ctx; | |
6917 | unsigned to_wait; | |
6918 | unsigned nr_timeouts; | |
6919 | }; | |
6920 | ||
6c503150 | 6921 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6922 | { |
6923 | struct io_ring_ctx *ctx = iowq->ctx; | |
6924 | ||
6925 | /* | |
d195a66e | 6926 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
6927 | * started waiting. For timeouts, we always want to return to userspace, |
6928 | * regardless of event count. | |
6929 | */ | |
6c503150 | 6930 | return io_cqring_events(ctx) >= iowq->to_wait || |
bda52162 JA |
6931 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
6932 | } | |
6933 | ||
6934 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
6935 | int wake_flags, void *key) | |
6936 | { | |
6937 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
6938 | wq); | |
6939 | ||
6c503150 PB |
6940 | /* |
6941 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
6942 | * the task, and the next invocation will do it. | |
6943 | */ | |
6944 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) | |
6945 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
6946 | return -1; | |
bda52162 JA |
6947 | } |
6948 | ||
af9c1a44 JA |
6949 | static int io_run_task_work_sig(void) |
6950 | { | |
6951 | if (io_run_task_work()) | |
6952 | return 1; | |
6953 | if (!signal_pending(current)) | |
6954 | return 0; | |
0b8cfa97 | 6955 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 6956 | return -ERESTARTSYS; |
af9c1a44 JA |
6957 | return -EINTR; |
6958 | } | |
6959 | ||
eeb60b9a PB |
6960 | /* when returns >0, the caller should retry */ |
6961 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
6962 | struct io_wait_queue *iowq, | |
6963 | signed long *timeout) | |
6964 | { | |
6965 | int ret; | |
6966 | ||
6967 | /* make sure we run task_work before checking for signals */ | |
6968 | ret = io_run_task_work_sig(); | |
6969 | if (ret || io_should_wake(iowq)) | |
6970 | return ret; | |
6971 | /* let the caller flush overflows, retry */ | |
6972 | if (test_bit(0, &ctx->cq_check_overflow)) | |
6973 | return 1; | |
6974 | ||
6975 | *timeout = schedule_timeout(*timeout); | |
6976 | return !*timeout ? -ETIME : 1; | |
6977 | } | |
6978 | ||
2b188cc1 JA |
6979 | /* |
6980 | * Wait until events become available, if we don't already have some. The | |
6981 | * application must reap them itself, as they reside on the shared cq ring. | |
6982 | */ | |
6983 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
6984 | const sigset_t __user *sig, size_t sigsz, |
6985 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 6986 | { |
bda52162 JA |
6987 | struct io_wait_queue iowq = { |
6988 | .wq = { | |
6989 | .private = current, | |
6990 | .func = io_wake_function, | |
6991 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
6992 | }, | |
6993 | .ctx = ctx, | |
6994 | .to_wait = min_events, | |
6995 | }; | |
75b28aff | 6996 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
6997 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
6998 | int ret; | |
2b188cc1 | 6999 | |
b41e9852 | 7000 | do { |
6c2450ae | 7001 | io_cqring_overflow_flush(ctx, false); |
6c503150 | 7002 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7003 | return 0; |
4c6e277c | 7004 | if (!io_run_task_work()) |
b41e9852 | 7005 | break; |
b41e9852 | 7006 | } while (1); |
2b188cc1 JA |
7007 | |
7008 | if (sig) { | |
9e75ad5d AB |
7009 | #ifdef CONFIG_COMPAT |
7010 | if (in_compat_syscall()) | |
7011 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7012 | sigsz); |
9e75ad5d AB |
7013 | else |
7014 | #endif | |
b772434b | 7015 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7016 | |
2b188cc1 JA |
7017 | if (ret) |
7018 | return ret; | |
7019 | } | |
7020 | ||
c73ebb68 | 7021 | if (uts) { |
c1d5a224 PB |
7022 | struct timespec64 ts; |
7023 | ||
c73ebb68 HX |
7024 | if (get_timespec64(&ts, uts)) |
7025 | return -EFAULT; | |
7026 | timeout = timespec64_to_jiffies(&ts); | |
7027 | } | |
7028 | ||
bda52162 | 7029 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 7030 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7031 | do { |
ca0a2651 | 7032 | /* if we can't even flush overflow, don't wait for more */ |
6c2450ae | 7033 | if (!io_cqring_overflow_flush(ctx, false)) { |
ca0a2651 JA |
7034 | ret = -EBUSY; |
7035 | break; | |
7036 | } | |
bda52162 JA |
7037 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
7038 | TASK_INTERRUPTIBLE); | |
eeb60b9a PB |
7039 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
7040 | finish_wait(&ctx->wait, &iowq.wq); | |
ca0a2651 | 7041 | cond_resched(); |
eeb60b9a | 7042 | } while (ret > 0); |
bda52162 | 7043 | |
b7db41c9 | 7044 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7045 | |
75b28aff | 7046 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7047 | } |
7048 | ||
846a4ef2 PB |
7049 | static void io_free_file_tables(struct io_rsrc_data *data, unsigned nr_files) |
7050 | { | |
7051 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); | |
7052 | ||
7053 | for (i = 0; i < nr_tables; i++) | |
7054 | kfree(data->table[i].files); | |
7055 | kfree(data->table); | |
7056 | data->table = NULL; | |
7057 | } | |
7058 | ||
6b06314c JA |
7059 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7060 | { | |
7061 | #if defined(CONFIG_UNIX) | |
7062 | if (ctx->ring_sock) { | |
7063 | struct sock *sock = ctx->ring_sock->sk; | |
7064 | struct sk_buff *skb; | |
7065 | ||
7066 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7067 | kfree_skb(skb); | |
7068 | } | |
7069 | #else | |
7070 | int i; | |
7071 | ||
65e19f54 JA |
7072 | for (i = 0; i < ctx->nr_user_files; i++) { |
7073 | struct file *file; | |
7074 | ||
7075 | file = io_file_from_index(ctx, i); | |
7076 | if (file) | |
7077 | fput(file); | |
7078 | } | |
6b06314c JA |
7079 | #endif |
7080 | } | |
7081 | ||
00835dce | 7082 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
05f3fb3c | 7083 | { |
b895c9a6 | 7084 | struct io_rsrc_data *data = container_of(ref, struct io_rsrc_data, refs); |
05f3fb3c | 7085 | |
05f3fb3c JA |
7086 | complete(&data->done); |
7087 | } | |
7088 | ||
2a63b2d9 | 7089 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
1642b445 | 7090 | { |
2a63b2d9 | 7091 | spin_lock_bh(&ctx->rsrc_ref_lock); |
1642b445 PB |
7092 | } |
7093 | ||
2a63b2d9 | 7094 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
6b06314c | 7095 | { |
2a63b2d9 BM |
7096 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
7097 | } | |
65e19f54 | 7098 | |
28a9fe25 PB |
7099 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
7100 | { | |
7101 | percpu_ref_exit(&ref_node->refs); | |
7102 | kfree(ref_node); | |
7103 | } | |
7104 | ||
a7f0ed5a PB |
7105 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7106 | struct io_rsrc_data *data_to_kill) | |
1642b445 | 7107 | { |
a7f0ed5a PB |
7108 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7109 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
82fbcfa9 | 7110 | |
a7f0ed5a PB |
7111 | if (data_to_kill) { |
7112 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7113 | |
a7f0ed5a PB |
7114 | rsrc_node->rsrc_data = data_to_kill; |
7115 | io_rsrc_ref_lock(ctx); | |
7116 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); | |
7117 | io_rsrc_ref_unlock(ctx); | |
82fbcfa9 | 7118 | |
a7f0ed5a PB |
7119 | percpu_ref_get(&data_to_kill->refs); |
7120 | percpu_ref_kill(&rsrc_node->refs); | |
7121 | ctx->rsrc_node = NULL; | |
7122 | } | |
6b06314c | 7123 | |
a7f0ed5a PB |
7124 | if (!ctx->rsrc_node) { |
7125 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7126 | ctx->rsrc_backup_node = NULL; | |
7127 | } | |
8bad28d8 HX |
7128 | } |
7129 | ||
a7f0ed5a | 7130 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7131 | { |
7132 | if (ctx->rsrc_backup_node) | |
7133 | return 0; | |
b895c9a6 | 7134 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe PB |
7135 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
7136 | } | |
7137 | ||
40ae0ff7 | 7138 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7139 | { |
7140 | int ret; | |
05589553 | 7141 | |
215c3902 | 7142 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7143 | if (data->quiesce) |
7144 | return -ENXIO; | |
05589553 | 7145 | |
8bad28d8 | 7146 | data->quiesce = true; |
1ffc5422 | 7147 | do { |
a7f0ed5a | 7148 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7149 | if (ret) |
f2303b1f | 7150 | break; |
a7f0ed5a PB |
7151 | io_rsrc_node_switch(ctx, data); |
7152 | ||
8bad28d8 HX |
7153 | percpu_ref_kill(&data->refs); |
7154 | flush_delayed_work(&ctx->rsrc_put_work); | |
7155 | ||
1ffc5422 PB |
7156 | ret = wait_for_completion_interruptible(&data->done); |
7157 | if (!ret) | |
7158 | break; | |
8bad28d8 | 7159 | |
cb5e1b81 | 7160 | percpu_ref_resurrect(&data->refs); |
cb5e1b81 | 7161 | reinit_completion(&data->done); |
8dd03afe | 7162 | |
8bad28d8 | 7163 | mutex_unlock(&ctx->uring_lock); |
1ffc5422 | 7164 | ret = io_run_task_work_sig(); |
8bad28d8 | 7165 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7166 | } while (ret >= 0); |
8bad28d8 | 7167 | data->quiesce = false; |
05f3fb3c | 7168 | |
8bad28d8 | 7169 | return ret; |
d7954b2b BM |
7170 | } |
7171 | ||
40ae0ff7 PB |
7172 | static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx, |
7173 | rsrc_put_fn *do_put) | |
1ad555c6 | 7174 | { |
b895c9a6 | 7175 | struct io_rsrc_data *data; |
1ad555c6 BM |
7176 | |
7177 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7178 | if (!data) | |
7179 | return NULL; | |
7180 | ||
00835dce | 7181 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
1ad555c6 BM |
7182 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
7183 | kfree(data); | |
7184 | return NULL; | |
7185 | } | |
7186 | data->ctx = ctx; | |
40ae0ff7 | 7187 | data->do_put = do_put; |
1ad555c6 BM |
7188 | init_completion(&data->done); |
7189 | return data; | |
7190 | } | |
7191 | ||
b895c9a6 | 7192 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 BM |
7193 | { |
7194 | percpu_ref_exit(&data->refs); | |
1ad555c6 BM |
7195 | kfree(data); |
7196 | } | |
7197 | ||
d7954b2b BM |
7198 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7199 | { | |
b895c9a6 | 7200 | struct io_rsrc_data *data = ctx->file_data; |
d7954b2b BM |
7201 | int ret; |
7202 | ||
215c3902 | 7203 | if (!data) |
d7954b2b | 7204 | return -ENXIO; |
40ae0ff7 | 7205 | ret = io_rsrc_ref_quiesce(data, ctx); |
d7954b2b BM |
7206 | if (ret) |
7207 | return ret; | |
7208 | ||
6b06314c | 7209 | __io_sqe_files_unregister(ctx); |
846a4ef2 | 7210 | io_free_file_tables(data, ctx->nr_user_files); |
b895c9a6 | 7211 | io_rsrc_data_free(data); |
05f3fb3c | 7212 | ctx->file_data = NULL; |
6b06314c JA |
7213 | ctx->nr_user_files = 0; |
7214 | return 0; | |
7215 | } | |
7216 | ||
37d1e2e3 | 7217 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7218 | __releases(&sqd->lock) |
37d1e2e3 | 7219 | { |
521d6a73 PB |
7220 | WARN_ON_ONCE(sqd->thread == current); |
7221 | ||
9e138a48 PB |
7222 | /* |
7223 | * Do the dance but not conditional clear_bit() because it'd race with | |
7224 | * other threads incrementing park_pending and setting the bit. | |
7225 | */ | |
37d1e2e3 | 7226 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7227 | if (atomic_dec_return(&sqd->park_pending)) |
7228 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7229 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7230 | } |
7231 | ||
86e0d676 | 7232 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7233 | __acquires(&sqd->lock) |
37d1e2e3 | 7234 | { |
521d6a73 PB |
7235 | WARN_ON_ONCE(sqd->thread == current); |
7236 | ||
9e138a48 | 7237 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7238 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7239 | mutex_lock(&sqd->lock); |
05962f95 | 7240 | if (sqd->thread) |
86e0d676 | 7241 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7242 | } |
7243 | ||
7244 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7245 | { | |
521d6a73 PB |
7246 | WARN_ON_ONCE(sqd->thread == current); |
7247 | ||
09a6f4ef | 7248 | mutex_lock(&sqd->lock); |
05962f95 | 7249 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
e8f98f24 JA |
7250 | if (sqd->thread) |
7251 | wake_up_process(sqd->thread); | |
09a6f4ef | 7252 | mutex_unlock(&sqd->lock); |
05962f95 | 7253 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7254 | } |
7255 | ||
534ca6d6 | 7256 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7257 | { |
534ca6d6 | 7258 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7259 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7260 | ||
37d1e2e3 JA |
7261 | io_sq_thread_stop(sqd); |
7262 | kfree(sqd); | |
7263 | } | |
7264 | } | |
7265 | ||
7266 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7267 | { | |
7268 | struct io_sq_data *sqd = ctx->sq_data; | |
7269 | ||
7270 | if (sqd) { | |
05962f95 | 7271 | io_sq_thread_park(sqd); |
521d6a73 | 7272 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7273 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7274 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7275 | |
7276 | io_put_sq_data(sqd); | |
7277 | ctx->sq_data = NULL; | |
7c30f36a SM |
7278 | if (ctx->sq_creds) |
7279 | put_cred(ctx->sq_creds); | |
534ca6d6 JA |
7280 | } |
7281 | } | |
7282 | ||
aa06165d JA |
7283 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7284 | { | |
7285 | struct io_ring_ctx *ctx_attach; | |
7286 | struct io_sq_data *sqd; | |
7287 | struct fd f; | |
7288 | ||
7289 | f = fdget(p->wq_fd); | |
7290 | if (!f.file) | |
7291 | return ERR_PTR(-ENXIO); | |
7292 | if (f.file->f_op != &io_uring_fops) { | |
7293 | fdput(f); | |
7294 | return ERR_PTR(-EINVAL); | |
7295 | } | |
7296 | ||
7297 | ctx_attach = f.file->private_data; | |
7298 | sqd = ctx_attach->sq_data; | |
7299 | if (!sqd) { | |
7300 | fdput(f); | |
7301 | return ERR_PTR(-EINVAL); | |
7302 | } | |
5c2469e0 JA |
7303 | if (sqd->task_tgid != current->tgid) { |
7304 | fdput(f); | |
7305 | return ERR_PTR(-EPERM); | |
7306 | } | |
aa06165d JA |
7307 | |
7308 | refcount_inc(&sqd->refs); | |
7309 | fdput(f); | |
7310 | return sqd; | |
7311 | } | |
7312 | ||
26984fbf PB |
7313 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7314 | bool *attached) | |
534ca6d6 JA |
7315 | { |
7316 | struct io_sq_data *sqd; | |
7317 | ||
26984fbf | 7318 | *attached = false; |
5c2469e0 JA |
7319 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7320 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7321 | if (!IS_ERR(sqd)) { |
7322 | *attached = true; | |
5c2469e0 | 7323 | return sqd; |
26984fbf | 7324 | } |
5c2469e0 JA |
7325 | /* fall through for EPERM case, setup new sqd/task */ |
7326 | if (PTR_ERR(sqd) != -EPERM) | |
7327 | return sqd; | |
7328 | } | |
aa06165d | 7329 | |
534ca6d6 JA |
7330 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7331 | if (!sqd) | |
7332 | return ERR_PTR(-ENOMEM); | |
7333 | ||
9e138a48 | 7334 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7335 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7336 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7337 | mutex_init(&sqd->lock); |
534ca6d6 | 7338 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7339 | init_completion(&sqd->exited); |
534ca6d6 JA |
7340 | return sqd; |
7341 | } | |
7342 | ||
6b06314c | 7343 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7344 | /* |
7345 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7346 | * the io_uring can be safely unregistered on process exit, even if we have | |
7347 | * loops in the file referencing. | |
7348 | */ | |
7349 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7350 | { | |
7351 | struct sock *sk = ctx->ring_sock->sk; | |
7352 | struct scm_fp_list *fpl; | |
7353 | struct sk_buff *skb; | |
08a45173 | 7354 | int i, nr_files; |
6b06314c | 7355 | |
6b06314c JA |
7356 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7357 | if (!fpl) | |
7358 | return -ENOMEM; | |
7359 | ||
7360 | skb = alloc_skb(0, GFP_KERNEL); | |
7361 | if (!skb) { | |
7362 | kfree(fpl); | |
7363 | return -ENOMEM; | |
7364 | } | |
7365 | ||
7366 | skb->sk = sk; | |
6b06314c | 7367 | |
08a45173 | 7368 | nr_files = 0; |
62e398be | 7369 | fpl->user = get_uid(current_user()); |
6b06314c | 7370 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7371 | struct file *file = io_file_from_index(ctx, i + offset); |
7372 | ||
7373 | if (!file) | |
08a45173 | 7374 | continue; |
65e19f54 | 7375 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7376 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7377 | nr_files++; | |
6b06314c JA |
7378 | } |
7379 | ||
08a45173 JA |
7380 | if (nr_files) { |
7381 | fpl->max = SCM_MAX_FD; | |
7382 | fpl->count = nr_files; | |
7383 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7384 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7385 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7386 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7387 | |
08a45173 JA |
7388 | for (i = 0; i < nr_files; i++) |
7389 | fput(fpl->fp[i]); | |
7390 | } else { | |
7391 | kfree_skb(skb); | |
7392 | kfree(fpl); | |
7393 | } | |
6b06314c JA |
7394 | |
7395 | return 0; | |
7396 | } | |
7397 | ||
7398 | /* | |
7399 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7400 | * causes regular reference counting to break down. We rely on the UNIX | |
7401 | * garbage collection to take care of this problem for us. | |
7402 | */ | |
7403 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7404 | { | |
7405 | unsigned left, total; | |
7406 | int ret = 0; | |
7407 | ||
7408 | total = 0; | |
7409 | left = ctx->nr_user_files; | |
7410 | while (left) { | |
7411 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7412 | |
7413 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7414 | if (ret) | |
7415 | break; | |
7416 | left -= this_files; | |
7417 | total += this_files; | |
7418 | } | |
7419 | ||
7420 | if (!ret) | |
7421 | return 0; | |
7422 | ||
7423 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7424 | struct file *file = io_file_from_index(ctx, total); |
7425 | ||
7426 | if (file) | |
7427 | fput(file); | |
6b06314c JA |
7428 | total++; |
7429 | } | |
7430 | ||
7431 | return ret; | |
7432 | } | |
7433 | #else | |
7434 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7435 | { | |
7436 | return 0; | |
7437 | } | |
7438 | #endif | |
7439 | ||
846a4ef2 PB |
7440 | static bool io_alloc_file_tables(struct io_rsrc_data *file_data, |
7441 | unsigned nr_files) | |
65e19f54 | 7442 | { |
846a4ef2 PB |
7443 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); |
7444 | ||
7445 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), | |
7446 | GFP_KERNEL); | |
7447 | if (!file_data->table) | |
7448 | return false; | |
65e19f54 JA |
7449 | |
7450 | for (i = 0; i < nr_tables; i++) { | |
269bbe5f | 7451 | struct fixed_rsrc_table *table = &file_data->table[i]; |
846a4ef2 | 7452 | unsigned int this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
65e19f54 | 7453 | |
65e19f54 JA |
7454 | table->files = kcalloc(this_files, sizeof(struct file *), |
7455 | GFP_KERNEL); | |
7456 | if (!table->files) | |
7457 | break; | |
7458 | nr_files -= this_files; | |
7459 | } | |
7460 | ||
7461 | if (i == nr_tables) | |
846a4ef2 | 7462 | return true; |
65e19f54 | 7463 | |
846a4ef2 PB |
7464 | io_free_file_tables(file_data, nr_tables * IORING_MAX_FILES_TABLE); |
7465 | return false; | |
65e19f54 JA |
7466 | } |
7467 | ||
47e90392 | 7468 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7469 | { |
50238531 | 7470 | struct file *file = prsrc->file; |
05f3fb3c JA |
7471 | #if defined(CONFIG_UNIX) |
7472 | struct sock *sock = ctx->ring_sock->sk; | |
7473 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7474 | struct sk_buff *skb; | |
7475 | int i; | |
7476 | ||
7477 | __skb_queue_head_init(&list); | |
7478 | ||
7479 | /* | |
7480 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7481 | * remove this entry and rearrange the file array. | |
7482 | */ | |
7483 | skb = skb_dequeue(head); | |
7484 | while (skb) { | |
7485 | struct scm_fp_list *fp; | |
7486 | ||
7487 | fp = UNIXCB(skb).fp; | |
7488 | for (i = 0; i < fp->count; i++) { | |
7489 | int left; | |
7490 | ||
7491 | if (fp->fp[i] != file) | |
7492 | continue; | |
7493 | ||
7494 | unix_notinflight(fp->user, fp->fp[i]); | |
7495 | left = fp->count - 1 - i; | |
7496 | if (left) { | |
7497 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7498 | left * sizeof(struct file *)); | |
7499 | } | |
7500 | fp->count--; | |
7501 | if (!fp->count) { | |
7502 | kfree_skb(skb); | |
7503 | skb = NULL; | |
7504 | } else { | |
7505 | __skb_queue_tail(&list, skb); | |
7506 | } | |
7507 | fput(file); | |
7508 | file = NULL; | |
7509 | break; | |
7510 | } | |
7511 | ||
7512 | if (!file) | |
7513 | break; | |
7514 | ||
7515 | __skb_queue_tail(&list, skb); | |
7516 | ||
7517 | skb = skb_dequeue(head); | |
7518 | } | |
7519 | ||
7520 | if (skb_peek(&list)) { | |
7521 | spin_lock_irq(&head->lock); | |
7522 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7523 | __skb_queue_tail(head, skb); | |
7524 | spin_unlock_irq(&head->lock); | |
7525 | } | |
7526 | #else | |
7527 | fput(file); | |
7528 | #endif | |
7529 | } | |
7530 | ||
b895c9a6 | 7531 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7532 | { |
b895c9a6 | 7533 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7534 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7535 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7536 | |
269bbe5f BM |
7537 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7538 | list_del(&prsrc->list); | |
40ae0ff7 | 7539 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7540 | kfree(prsrc); |
65e19f54 | 7541 | } |
05589553 | 7542 | |
28a9fe25 | 7543 | io_rsrc_node_destroy(ref_node); |
269bbe5f | 7544 | percpu_ref_put(&rsrc_data->refs); |
2faf852d | 7545 | } |
65e19f54 | 7546 | |
269bbe5f | 7547 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7548 | { |
7549 | struct io_ring_ctx *ctx; | |
7550 | struct llist_node *node; | |
7551 | ||
269bbe5f BM |
7552 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7553 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7554 | |
7555 | while (node) { | |
b895c9a6 | 7556 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7557 | struct llist_node *next = node->next; |
7558 | ||
b895c9a6 | 7559 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7560 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7561 | node = next; |
7562 | } | |
7563 | } | |
7564 | ||
00835dce | 7565 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7566 | { |
b895c9a6 PB |
7567 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
7568 | struct io_rsrc_data *data = node->rsrc_data; | |
7569 | struct io_ring_ctx *ctx = data->ctx; | |
e297822b | 7570 | bool first_add = false; |
221aa924 | 7571 | int delay; |
65e19f54 | 7572 | |
2a63b2d9 | 7573 | io_rsrc_ref_lock(ctx); |
b895c9a6 | 7574 | node->done = true; |
e297822b | 7575 | |
d67d2263 | 7576 | while (!list_empty(&ctx->rsrc_ref_list)) { |
b895c9a6 PB |
7577 | node = list_first_entry(&ctx->rsrc_ref_list, |
7578 | struct io_rsrc_node, node); | |
e297822b | 7579 | /* recycle ref nodes in order */ |
b895c9a6 | 7580 | if (!node->done) |
e297822b | 7581 | break; |
b895c9a6 PB |
7582 | list_del(&node->node); |
7583 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
e297822b | 7584 | } |
2a63b2d9 | 7585 | io_rsrc_ref_unlock(ctx); |
05589553 | 7586 | |
221aa924 PB |
7587 | delay = percpu_ref_is_dying(&data->refs) ? 0 : HZ; |
7588 | if (first_add || !delay) | |
7589 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); | |
05f3fb3c | 7590 | } |
65e19f54 | 7591 | |
b895c9a6 | 7592 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
05f3fb3c | 7593 | { |
b895c9a6 | 7594 | struct io_rsrc_node *ref_node; |
05f3fb3c | 7595 | |
05589553 XW |
7596 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7597 | if (!ref_node) | |
3e2224c5 | 7598 | return NULL; |
05f3fb3c | 7599 | |
00835dce | 7600 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7601 | 0, GFP_KERNEL)) { |
7602 | kfree(ref_node); | |
3e2224c5 | 7603 | return NULL; |
05589553 XW |
7604 | } |
7605 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7606 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7607 | ref_node->done = false; |
05589553 | 7608 | return ref_node; |
05589553 XW |
7609 | } |
7610 | ||
6b06314c JA |
7611 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
7612 | unsigned nr_args) | |
7613 | { | |
7614 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7615 | struct file *file; |
f3baed39 | 7616 | int fd, ret; |
846a4ef2 | 7617 | unsigned i; |
b895c9a6 | 7618 | struct io_rsrc_data *file_data; |
6b06314c | 7619 | |
05f3fb3c | 7620 | if (ctx->file_data) |
6b06314c JA |
7621 | return -EBUSY; |
7622 | if (!nr_args) | |
7623 | return -EINVAL; | |
7624 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7625 | return -EMFILE; | |
a7f0ed5a | 7626 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7627 | if (ret) |
7628 | return ret; | |
6b06314c | 7629 | |
47e90392 | 7630 | file_data = io_rsrc_data_alloc(ctx, io_rsrc_file_put); |
5398ae69 | 7631 | if (!file_data) |
05f3fb3c | 7632 | return -ENOMEM; |
13770a71 | 7633 | ctx->file_data = file_data; |
05f3fb3c | 7634 | |
f3baed39 | 7635 | ret = -ENOMEM; |
846a4ef2 | 7636 | if (!io_alloc_file_tables(file_data, nr_args)) |
1ad555c6 | 7637 | goto out_free; |
65e19f54 | 7638 | |
08a45173 | 7639 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
600cf3f8 PB |
7640 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
7641 | ret = -EFAULT; | |
7642 | goto out_fput; | |
7643 | } | |
08a45173 | 7644 | /* allow sparse sets */ |
600cf3f8 | 7645 | if (fd == -1) |
08a45173 | 7646 | continue; |
6b06314c | 7647 | |
05f3fb3c | 7648 | file = fget(fd); |
6b06314c | 7649 | ret = -EBADF; |
05f3fb3c | 7650 | if (!file) |
600cf3f8 | 7651 | goto out_fput; |
05f3fb3c | 7652 | |
6b06314c JA |
7653 | /* |
7654 | * Don't allow io_uring instances to be registered. If UNIX | |
7655 | * isn't enabled, then this causes a reference cycle and this | |
7656 | * instance can never get freed. If UNIX is enabled we'll | |
7657 | * handle it just fine, but there's still no point in allowing | |
7658 | * a ring fd as it doesn't support regular read/write anyway. | |
7659 | */ | |
05f3fb3c JA |
7660 | if (file->f_op == &io_uring_fops) { |
7661 | fput(file); | |
600cf3f8 | 7662 | goto out_fput; |
6b06314c | 7663 | } |
9a321c98 | 7664 | io_fixed_file_set(io_fixed_file_slot(file_data, i), file); |
6b06314c JA |
7665 | } |
7666 | ||
6b06314c | 7667 | ret = io_sqe_files_scm(ctx); |
05589553 | 7668 | if (ret) { |
6b06314c | 7669 | io_sqe_files_unregister(ctx); |
05589553 XW |
7670 | return ret; |
7671 | } | |
6b06314c | 7672 | |
a7f0ed5a | 7673 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7674 | return ret; |
600cf3f8 PB |
7675 | out_fput: |
7676 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7677 | file = io_file_from_index(ctx, i); | |
7678 | if (file) | |
7679 | fput(file); | |
7680 | } | |
846a4ef2 | 7681 | io_free_file_tables(file_data, nr_args); |
600cf3f8 | 7682 | ctx->nr_user_files = 0; |
600cf3f8 | 7683 | out_free: |
b895c9a6 | 7684 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 7685 | ctx->file_data = NULL; |
6b06314c JA |
7686 | return ret; |
7687 | } | |
7688 | ||
c3a31e60 JA |
7689 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7690 | int index) | |
7691 | { | |
7692 | #if defined(CONFIG_UNIX) | |
7693 | struct sock *sock = ctx->ring_sock->sk; | |
7694 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7695 | struct sk_buff *skb; | |
7696 | ||
7697 | /* | |
7698 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7699 | * file set. If there's no room, fall back to allocating a new skb | |
7700 | * and filling it in. | |
7701 | */ | |
7702 | spin_lock_irq(&head->lock); | |
7703 | skb = skb_peek(head); | |
7704 | if (skb) { | |
7705 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7706 | ||
7707 | if (fpl->count < SCM_MAX_FD) { | |
7708 | __skb_unlink(skb, head); | |
7709 | spin_unlock_irq(&head->lock); | |
7710 | fpl->fp[fpl->count] = get_file(file); | |
7711 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7712 | fpl->count++; | |
7713 | spin_lock_irq(&head->lock); | |
7714 | __skb_queue_head(head, skb); | |
7715 | } else { | |
7716 | skb = NULL; | |
7717 | } | |
7718 | } | |
7719 | spin_unlock_irq(&head->lock); | |
7720 | ||
7721 | if (skb) { | |
7722 | fput(file); | |
7723 | return 0; | |
7724 | } | |
7725 | ||
7726 | return __io_sqe_files_scm(ctx, 1, index); | |
7727 | #else | |
7728 | return 0; | |
7729 | #endif | |
7730 | } | |
7731 | ||
e7c78371 PB |
7732 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, |
7733 | struct io_rsrc_node *node, void *rsrc) | |
05f3fb3c | 7734 | { |
269bbe5f | 7735 | struct io_rsrc_put *prsrc; |
05f3fb3c | 7736 | |
269bbe5f BM |
7737 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7738 | if (!prsrc) | |
a5318d3c | 7739 | return -ENOMEM; |
05f3fb3c | 7740 | |
50238531 | 7741 | prsrc->rsrc = rsrc; |
e7c78371 | 7742 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 7743 | return 0; |
05f3fb3c JA |
7744 | } |
7745 | ||
7746 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
269bbe5f | 7747 | struct io_uring_rsrc_update *up, |
05f3fb3c JA |
7748 | unsigned nr_args) |
7749 | { | |
b895c9a6 | 7750 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
7751 | struct io_fixed_file *file_slot; |
7752 | struct file *file; | |
c3a31e60 JA |
7753 | __s32 __user *fds; |
7754 | int fd, i, err; | |
7755 | __u32 done; | |
05589553 | 7756 | bool needs_switch = false; |
c3a31e60 | 7757 | |
05f3fb3c | 7758 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
7759 | return -EOVERFLOW; |
7760 | if (done > ctx->nr_user_files) | |
7761 | return -EINVAL; | |
a7f0ed5a | 7762 | err = io_rsrc_node_switch_start(ctx); |
8dd03afe PB |
7763 | if (err) |
7764 | return err; | |
05589553 | 7765 | |
269bbe5f | 7766 | fds = u64_to_user_ptr(up->data); |
67973b93 | 7767 | for (done = 0; done < nr_args; done++) { |
c3a31e60 JA |
7768 | err = 0; |
7769 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
7770 | err = -EFAULT; | |
7771 | break; | |
7772 | } | |
4e0377a1 | 7773 | if (fd == IORING_REGISTER_FILES_SKIP) |
7774 | continue; | |
7775 | ||
67973b93 | 7776 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
ea64ec02 PB |
7777 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
7778 | ||
a04b0ac0 PB |
7779 | if (file_slot->file_ptr) { |
7780 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
a7f0ed5a | 7781 | err = io_queue_rsrc_removal(data, ctx->rsrc_node, file); |
a5318d3c HD |
7782 | if (err) |
7783 | break; | |
a04b0ac0 | 7784 | file_slot->file_ptr = 0; |
05589553 | 7785 | needs_switch = true; |
c3a31e60 JA |
7786 | } |
7787 | if (fd != -1) { | |
c3a31e60 JA |
7788 | file = fget(fd); |
7789 | if (!file) { | |
7790 | err = -EBADF; | |
7791 | break; | |
7792 | } | |
7793 | /* | |
7794 | * Don't allow io_uring instances to be registered. If | |
7795 | * UNIX isn't enabled, then this causes a reference | |
7796 | * cycle and this instance can never get freed. If UNIX | |
7797 | * is enabled we'll handle it just fine, but there's | |
7798 | * still no point in allowing a ring fd as it doesn't | |
7799 | * support regular read/write anyway. | |
7800 | */ | |
7801 | if (file->f_op == &io_uring_fops) { | |
7802 | fput(file); | |
7803 | err = -EBADF; | |
7804 | break; | |
7805 | } | |
9a321c98 | 7806 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 7807 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7808 | if (err) { |
a04b0ac0 | 7809 | file_slot->file_ptr = 0; |
f3bd9dae | 7810 | fput(file); |
c3a31e60 | 7811 | break; |
f3bd9dae | 7812 | } |
c3a31e60 | 7813 | } |
05f3fb3c JA |
7814 | } |
7815 | ||
a7f0ed5a PB |
7816 | if (needs_switch) |
7817 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
7818 | return done ? done : err; |
7819 | } | |
05589553 | 7820 | |
05f3fb3c JA |
7821 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
7822 | unsigned nr_args) | |
7823 | { | |
269bbe5f | 7824 | struct io_uring_rsrc_update up; |
05f3fb3c JA |
7825 | |
7826 | if (!ctx->file_data) | |
7827 | return -ENXIO; | |
7828 | if (!nr_args) | |
7829 | return -EINVAL; | |
7830 | if (copy_from_user(&up, arg, sizeof(up))) | |
7831 | return -EFAULT; | |
7832 | if (up.resv) | |
7833 | return -EINVAL; | |
7834 | ||
7835 | return __io_sqe_files_update(ctx, &up, nr_args); | |
7836 | } | |
c3a31e60 | 7837 | |
5280f7e5 | 7838 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
7d723065 JA |
7839 | { |
7840 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7841 | ||
5280f7e5 PB |
7842 | req = io_put_req_find_next(req); |
7843 | return req ? &req->work : NULL; | |
7d723065 JA |
7844 | } |
7845 | ||
685fe7fe JA |
7846 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
7847 | struct task_struct *task) | |
24369c2e | 7848 | { |
e941894e | 7849 | struct io_wq_hash *hash; |
24369c2e | 7850 | struct io_wq_data data; |
24369c2e | 7851 | unsigned int concurrency; |
24369c2e | 7852 | |
e941894e JA |
7853 | hash = ctx->hash_map; |
7854 | if (!hash) { | |
7855 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
7856 | if (!hash) | |
7857 | return ERR_PTR(-ENOMEM); | |
7858 | refcount_set(&hash->refs, 1); | |
7859 | init_waitqueue_head(&hash->wait); | |
7860 | ctx->hash_map = hash; | |
24369c2e PB |
7861 | } |
7862 | ||
e941894e | 7863 | data.hash = hash; |
685fe7fe | 7864 | data.task = task; |
e9fd9396 | 7865 | data.free_work = io_free_work; |
f5fa38c5 | 7866 | data.do_work = io_wq_submit_work; |
24369c2e | 7867 | |
d25e3a3d JA |
7868 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7869 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7870 | |
5aa75ed5 | 7871 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7872 | } |
7873 | ||
5aa75ed5 JA |
7874 | static int io_uring_alloc_task_context(struct task_struct *task, |
7875 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7876 | { |
7877 | struct io_uring_task *tctx; | |
d8a6df10 | 7878 | int ret; |
0f212204 JA |
7879 | |
7880 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); | |
7881 | if (unlikely(!tctx)) | |
7882 | return -ENOMEM; | |
7883 | ||
d8a6df10 JA |
7884 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7885 | if (unlikely(ret)) { | |
7886 | kfree(tctx); | |
7887 | return ret; | |
7888 | } | |
7889 | ||
685fe7fe | 7890 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
7891 | if (IS_ERR(tctx->io_wq)) { |
7892 | ret = PTR_ERR(tctx->io_wq); | |
7893 | percpu_counter_destroy(&tctx->inflight); | |
7894 | kfree(tctx); | |
7895 | return ret; | |
7896 | } | |
7897 | ||
0f212204 JA |
7898 | xa_init(&tctx->xa); |
7899 | init_waitqueue_head(&tctx->wait); | |
7900 | tctx->last = NULL; | |
fdaf083c | 7901 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 7902 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 7903 | task->io_uring = tctx; |
7cbf1722 JA |
7904 | spin_lock_init(&tctx->task_lock); |
7905 | INIT_WQ_LIST(&tctx->task_list); | |
7906 | tctx->task_state = 0; | |
7907 | init_task_work(&tctx->task_work, tctx_task_work); | |
0f212204 JA |
7908 | return 0; |
7909 | } | |
7910 | ||
7911 | void __io_uring_free(struct task_struct *tsk) | |
7912 | { | |
7913 | struct io_uring_task *tctx = tsk->io_uring; | |
7914 | ||
7915 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e PB |
7916 | WARN_ON_ONCE(tctx->io_wq); |
7917 | ||
d8a6df10 | 7918 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
7919 | kfree(tctx); |
7920 | tsk->io_uring = NULL; | |
7921 | } | |
7922 | ||
7e84e1c7 SG |
7923 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
7924 | struct io_uring_params *p) | |
2b188cc1 JA |
7925 | { |
7926 | int ret; | |
7927 | ||
d25e3a3d JA |
7928 | /* Retain compatibility with failing for an invalid attach attempt */ |
7929 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
7930 | IORING_SETUP_ATTACH_WQ) { | |
7931 | struct fd f; | |
7932 | ||
7933 | f = fdget(p->wq_fd); | |
7934 | if (!f.file) | |
7935 | return -ENXIO; | |
7936 | if (f.file->f_op != &io_uring_fops) { | |
7937 | fdput(f); | |
7938 | return -EINVAL; | |
7939 | } | |
7940 | fdput(f); | |
7941 | } | |
6c271ce2 | 7942 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 7943 | struct task_struct *tsk; |
534ca6d6 | 7944 | struct io_sq_data *sqd; |
26984fbf | 7945 | bool attached; |
534ca6d6 | 7946 | |
26984fbf | 7947 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
7948 | if (IS_ERR(sqd)) { |
7949 | ret = PTR_ERR(sqd); | |
7950 | goto err; | |
7951 | } | |
69fb2131 | 7952 | |
7c30f36a | 7953 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 7954 | ctx->sq_data = sqd; |
917257da JA |
7955 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
7956 | if (!ctx->sq_thread_idle) | |
7957 | ctx->sq_thread_idle = HZ; | |
7958 | ||
26984fbf | 7959 | ret = 0; |
78d7f6ba | 7960 | io_sq_thread_park(sqd); |
de75a3d3 PB |
7961 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
7962 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 7963 | /* don't attach to a dying SQPOLL thread, would be racy */ |
de75a3d3 | 7964 | if (attached && !sqd->thread) |
26984fbf | 7965 | ret = -ENXIO; |
78d7f6ba PB |
7966 | io_sq_thread_unpark(sqd); |
7967 | ||
de75a3d3 PB |
7968 | if (ret < 0) |
7969 | goto err; | |
7970 | if (attached) | |
5aa75ed5 | 7971 | return 0; |
aa06165d | 7972 | |
6c271ce2 | 7973 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 7974 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 7975 | |
917257da | 7976 | ret = -EINVAL; |
44a9bd18 | 7977 | if (cpu >= nr_cpu_ids) |
e8f98f24 | 7978 | goto err_sqpoll; |
7889f44d | 7979 | if (!cpu_online(cpu)) |
e8f98f24 | 7980 | goto err_sqpoll; |
917257da | 7981 | |
37d1e2e3 | 7982 | sqd->sq_cpu = cpu; |
6c271ce2 | 7983 | } else { |
37d1e2e3 | 7984 | sqd->sq_cpu = -1; |
6c271ce2 | 7985 | } |
37d1e2e3 JA |
7986 | |
7987 | sqd->task_pid = current->pid; | |
5c2469e0 | 7988 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
7989 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
7990 | if (IS_ERR(tsk)) { | |
7991 | ret = PTR_ERR(tsk); | |
e8f98f24 | 7992 | goto err_sqpoll; |
6c271ce2 | 7993 | } |
97a73a0f | 7994 | |
46fe18b1 | 7995 | sqd->thread = tsk; |
97a73a0f | 7996 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 7997 | wake_up_new_task(tsk); |
0f212204 JA |
7998 | if (ret) |
7999 | goto err; | |
6c271ce2 JA |
8000 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8001 | /* Can't have SQ_AFF without SQPOLL */ | |
8002 | ret = -EINVAL; | |
8003 | goto err; | |
8004 | } | |
8005 | ||
2b188cc1 JA |
8006 | return 0; |
8007 | err: | |
37d1e2e3 | 8008 | io_sq_thread_finish(ctx); |
2b188cc1 | 8009 | return ret; |
e8f98f24 JA |
8010 | err_sqpoll: |
8011 | complete(&ctx->sq_data->exited); | |
8012 | goto err; | |
2b188cc1 JA |
8013 | } |
8014 | ||
a087e2b5 BM |
8015 | static inline void __io_unaccount_mem(struct user_struct *user, |
8016 | unsigned long nr_pages) | |
2b188cc1 JA |
8017 | { |
8018 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8019 | } | |
8020 | ||
a087e2b5 BM |
8021 | static inline int __io_account_mem(struct user_struct *user, |
8022 | unsigned long nr_pages) | |
2b188cc1 JA |
8023 | { |
8024 | unsigned long page_limit, cur_pages, new_pages; | |
8025 | ||
8026 | /* Don't allow more pages than we can safely lock */ | |
8027 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8028 | ||
8029 | do { | |
8030 | cur_pages = atomic_long_read(&user->locked_vm); | |
8031 | new_pages = cur_pages + nr_pages; | |
8032 | if (new_pages > page_limit) | |
8033 | return -ENOMEM; | |
8034 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8035 | new_pages) != cur_pages); | |
8036 | ||
8037 | return 0; | |
8038 | } | |
8039 | ||
26bfa89e | 8040 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8041 | { |
62e398be | 8042 | if (ctx->user) |
a087e2b5 | 8043 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8044 | |
26bfa89e JA |
8045 | if (ctx->mm_account) |
8046 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8047 | } |
8048 | ||
26bfa89e | 8049 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8050 | { |
30975825 BM |
8051 | int ret; |
8052 | ||
62e398be | 8053 | if (ctx->user) { |
30975825 BM |
8054 | ret = __io_account_mem(ctx->user, nr_pages); |
8055 | if (ret) | |
8056 | return ret; | |
8057 | } | |
8058 | ||
26bfa89e JA |
8059 | if (ctx->mm_account) |
8060 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8061 | |
8062 | return 0; | |
8063 | } | |
8064 | ||
2b188cc1 JA |
8065 | static void io_mem_free(void *ptr) |
8066 | { | |
52e04ef4 MR |
8067 | struct page *page; |
8068 | ||
8069 | if (!ptr) | |
8070 | return; | |
2b188cc1 | 8071 | |
52e04ef4 | 8072 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8073 | if (put_page_testzero(page)) |
8074 | free_compound_page(page); | |
8075 | } | |
8076 | ||
8077 | static void *io_mem_alloc(size_t size) | |
8078 | { | |
8079 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8080 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8081 | |
8082 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8083 | } | |
8084 | ||
75b28aff HV |
8085 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8086 | size_t *sq_offset) | |
8087 | { | |
8088 | struct io_rings *rings; | |
8089 | size_t off, sq_array_size; | |
8090 | ||
8091 | off = struct_size(rings, cqes, cq_entries); | |
8092 | if (off == SIZE_MAX) | |
8093 | return SIZE_MAX; | |
8094 | ||
8095 | #ifdef CONFIG_SMP | |
8096 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8097 | if (off == 0) | |
8098 | return SIZE_MAX; | |
8099 | #endif | |
8100 | ||
b36200f5 DV |
8101 | if (sq_offset) |
8102 | *sq_offset = off; | |
8103 | ||
75b28aff HV |
8104 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8105 | if (sq_array_size == SIZE_MAX) | |
8106 | return SIZE_MAX; | |
8107 | ||
8108 | if (check_add_overflow(off, sq_array_size, &off)) | |
8109 | return SIZE_MAX; | |
8110 | ||
75b28aff HV |
8111 | return off; |
8112 | } | |
8113 | ||
0a96bbe4 | 8114 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee JA |
8115 | { |
8116 | int i, j; | |
8117 | ||
8118 | if (!ctx->user_bufs) | |
8119 | return -ENXIO; | |
8120 | ||
8121 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8122 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8123 | ||
8124 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 8125 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee | 8126 | |
de293938 | 8127 | if (imu->acct_pages) |
26bfa89e | 8128 | io_unaccount_mem(ctx, imu->acct_pages); |
d4ef6475 | 8129 | kvfree(imu->bvec); |
edafccee JA |
8130 | imu->nr_bvecs = 0; |
8131 | } | |
8132 | ||
8133 | kfree(ctx->user_bufs); | |
8134 | ctx->user_bufs = NULL; | |
8135 | ctx->nr_user_bufs = 0; | |
8136 | return 0; | |
8137 | } | |
8138 | ||
8139 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8140 | void __user *arg, unsigned index) | |
8141 | { | |
8142 | struct iovec __user *src; | |
8143 | ||
8144 | #ifdef CONFIG_COMPAT | |
8145 | if (ctx->compat) { | |
8146 | struct compat_iovec __user *ciovs; | |
8147 | struct compat_iovec ciov; | |
8148 | ||
8149 | ciovs = (struct compat_iovec __user *) arg; | |
8150 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8151 | return -EFAULT; | |
8152 | ||
d55e5f5b | 8153 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8154 | dst->iov_len = ciov.iov_len; |
8155 | return 0; | |
8156 | } | |
8157 | #endif | |
8158 | src = (struct iovec __user *) arg; | |
8159 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8160 | return -EFAULT; | |
8161 | return 0; | |
8162 | } | |
8163 | ||
de293938 JA |
8164 | /* |
8165 | * Not super efficient, but this is just a registration time. And we do cache | |
8166 | * the last compound head, so generally we'll only do a full search if we don't | |
8167 | * match that one. | |
8168 | * | |
8169 | * We check if the given compound head page has already been accounted, to | |
8170 | * avoid double accounting it. This allows us to account the full size of the | |
8171 | * page, not just the constituent pages of a huge page. | |
8172 | */ | |
8173 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8174 | int nr_pages, struct page *hpage) | |
8175 | { | |
8176 | int i, j; | |
8177 | ||
8178 | /* check current page array */ | |
8179 | for (i = 0; i < nr_pages; i++) { | |
8180 | if (!PageCompound(pages[i])) | |
8181 | continue; | |
8182 | if (compound_head(pages[i]) == hpage) | |
8183 | return true; | |
8184 | } | |
8185 | ||
8186 | /* check previously registered pages */ | |
8187 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
8188 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
8189 | ||
8190 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8191 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8192 | continue; | |
8193 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8194 | return true; | |
8195 | } | |
8196 | } | |
8197 | ||
8198 | return false; | |
8199 | } | |
8200 | ||
8201 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8202 | int nr_pages, struct io_mapped_ubuf *imu, | |
8203 | struct page **last_hpage) | |
8204 | { | |
8205 | int i, ret; | |
8206 | ||
8207 | for (i = 0; i < nr_pages; i++) { | |
8208 | if (!PageCompound(pages[i])) { | |
8209 | imu->acct_pages++; | |
8210 | } else { | |
8211 | struct page *hpage; | |
8212 | ||
8213 | hpage = compound_head(pages[i]); | |
8214 | if (hpage == *last_hpage) | |
8215 | continue; | |
8216 | *last_hpage = hpage; | |
8217 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8218 | continue; | |
8219 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8220 | } | |
8221 | } | |
8222 | ||
8223 | if (!imu->acct_pages) | |
8224 | return 0; | |
8225 | ||
26bfa89e | 8226 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8227 | if (ret) |
8228 | imu->acct_pages = 0; | |
8229 | return ret; | |
8230 | } | |
8231 | ||
0a96bbe4 BM |
8232 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
8233 | struct io_mapped_ubuf *imu, | |
8234 | struct page **last_hpage) | |
edafccee JA |
8235 | { |
8236 | struct vm_area_struct **vmas = NULL; | |
8237 | struct page **pages = NULL; | |
0a96bbe4 BM |
8238 | unsigned long off, start, end, ubuf; |
8239 | size_t size; | |
8240 | int ret, pret, nr_pages, i; | |
8241 | ||
8242 | ubuf = (unsigned long) iov->iov_base; | |
8243 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8244 | start = ubuf >> PAGE_SHIFT; | |
8245 | nr_pages = end - start; | |
8246 | ||
8247 | ret = -ENOMEM; | |
8248 | ||
8249 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8250 | if (!pages) | |
8251 | goto done; | |
8252 | ||
8253 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8254 | GFP_KERNEL); | |
8255 | if (!vmas) | |
8256 | goto done; | |
edafccee | 8257 | |
0a96bbe4 BM |
8258 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
8259 | GFP_KERNEL); | |
8260 | if (!imu->bvec) | |
8261 | goto done; | |
8262 | ||
8263 | ret = 0; | |
8264 | mmap_read_lock(current->mm); | |
8265 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8266 | pages, vmas); | |
8267 | if (pret == nr_pages) { | |
8268 | /* don't support file backed memory */ | |
8269 | for (i = 0; i < nr_pages; i++) { | |
8270 | struct vm_area_struct *vma = vmas[i]; | |
8271 | ||
8272 | if (vma->vm_file && | |
8273 | !is_file_hugepages(vma->vm_file)) { | |
8274 | ret = -EOPNOTSUPP; | |
8275 | break; | |
8276 | } | |
8277 | } | |
8278 | } else { | |
8279 | ret = pret < 0 ? pret : -EFAULT; | |
8280 | } | |
8281 | mmap_read_unlock(current->mm); | |
8282 | if (ret) { | |
8283 | /* | |
8284 | * if we did partial map, or found file backed vmas, | |
8285 | * release any pages we did get | |
8286 | */ | |
8287 | if (pret > 0) | |
8288 | unpin_user_pages(pages, pret); | |
8289 | kvfree(imu->bvec); | |
8290 | goto done; | |
8291 | } | |
8292 | ||
8293 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8294 | if (ret) { | |
8295 | unpin_user_pages(pages, pret); | |
8296 | kvfree(imu->bvec); | |
8297 | goto done; | |
8298 | } | |
8299 | ||
8300 | off = ubuf & ~PAGE_MASK; | |
8301 | size = iov->iov_len; | |
8302 | for (i = 0; i < nr_pages; i++) { | |
8303 | size_t vec_len; | |
8304 | ||
8305 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8306 | imu->bvec[i].bv_page = pages[i]; | |
8307 | imu->bvec[i].bv_len = vec_len; | |
8308 | imu->bvec[i].bv_offset = off; | |
8309 | off = 0; | |
8310 | size -= vec_len; | |
8311 | } | |
8312 | /* store original address for later verification */ | |
8313 | imu->ubuf = ubuf; | |
4751f53d | 8314 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 BM |
8315 | imu->nr_bvecs = nr_pages; |
8316 | ret = 0; | |
8317 | done: | |
8318 | kvfree(pages); | |
8319 | kvfree(vmas); | |
8320 | return ret; | |
8321 | } | |
8322 | ||
2b358604 | 8323 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8324 | { |
edafccee JA |
8325 | if (ctx->user_bufs) |
8326 | return -EBUSY; | |
8327 | if (!nr_args || nr_args > UIO_MAXIOV) | |
8328 | return -EINVAL; | |
8329 | ||
8330 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
8331 | GFP_KERNEL); | |
8332 | if (!ctx->user_bufs) | |
8333 | return -ENOMEM; | |
8334 | ||
2b358604 BM |
8335 | return 0; |
8336 | } | |
edafccee | 8337 | |
2b358604 BM |
8338 | static int io_buffer_validate(struct iovec *iov) |
8339 | { | |
50e96989 PB |
8340 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8341 | ||
2b358604 BM |
8342 | /* |
8343 | * Don't impose further limits on the size and buffer | |
8344 | * constraints here, we'll -EINVAL later when IO is | |
8345 | * submitted if they are wrong. | |
8346 | */ | |
8347 | if (!iov->iov_base || !iov->iov_len) | |
8348 | return -EFAULT; | |
edafccee | 8349 | |
2b358604 BM |
8350 | /* arbitrary limit, but we need something */ |
8351 | if (iov->iov_len > SZ_1G) | |
8352 | return -EFAULT; | |
edafccee | 8353 | |
50e96989 PB |
8354 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8355 | return -EOVERFLOW; | |
8356 | ||
2b358604 BM |
8357 | return 0; |
8358 | } | |
edafccee | 8359 | |
2b358604 BM |
8360 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
8361 | unsigned int nr_args) | |
8362 | { | |
8363 | int i, ret; | |
8364 | struct iovec iov; | |
8365 | struct page *last_hpage = NULL; | |
edafccee | 8366 | |
2b358604 BM |
8367 | ret = io_buffers_map_alloc(ctx, nr_args); |
8368 | if (ret) | |
8369 | return ret; | |
edafccee | 8370 | |
edafccee JA |
8371 | for (i = 0; i < nr_args; i++) { |
8372 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
edafccee | 8373 | |
edafccee JA |
8374 | ret = io_copy_iov(ctx, &iov, arg, i); |
8375 | if (ret) | |
0a96bbe4 | 8376 | break; |
de293938 | 8377 | |
2b358604 BM |
8378 | ret = io_buffer_validate(&iov); |
8379 | if (ret) | |
0a96bbe4 | 8380 | break; |
edafccee | 8381 | |
0a96bbe4 BM |
8382 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
8383 | if (ret) | |
8384 | break; | |
edafccee JA |
8385 | |
8386 | ctx->nr_user_bufs++; | |
8387 | } | |
0a96bbe4 BM |
8388 | |
8389 | if (ret) | |
8390 | io_sqe_buffers_unregister(ctx); | |
8391 | ||
edafccee JA |
8392 | return ret; |
8393 | } | |
8394 | ||
9b402849 JA |
8395 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8396 | { | |
8397 | __s32 __user *fds = arg; | |
8398 | int fd; | |
8399 | ||
8400 | if (ctx->cq_ev_fd) | |
8401 | return -EBUSY; | |
8402 | ||
8403 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8404 | return -EFAULT; | |
8405 | ||
8406 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8407 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8408 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
8409 | ctx->cq_ev_fd = NULL; | |
8410 | return ret; | |
8411 | } | |
8412 | ||
8413 | return 0; | |
8414 | } | |
8415 | ||
8416 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8417 | { | |
8418 | if (ctx->cq_ev_fd) { | |
8419 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8420 | ctx->cq_ev_fd = NULL; | |
8421 | return 0; | |
8422 | } | |
8423 | ||
8424 | return -ENXIO; | |
8425 | } | |
8426 | ||
5a2e745d JA |
8427 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8428 | { | |
9e15c3a0 JA |
8429 | struct io_buffer *buf; |
8430 | unsigned long index; | |
8431 | ||
8432 | xa_for_each(&ctx->io_buffers, index, buf) | |
8433 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8434 | } |
8435 | ||
68e68ee6 | 8436 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8437 | { |
68e68ee6 | 8438 | struct io_kiocb *req, *nxt; |
1b4c351f | 8439 | |
68e68ee6 JA |
8440 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8441 | if (tsk && req->task != tsk) | |
8442 | continue; | |
1b4c351f JA |
8443 | list_del(&req->compl.list); |
8444 | kmem_cache_free(req_cachep, req); | |
8445 | } | |
8446 | } | |
8447 | ||
4010fec4 | 8448 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8449 | { |
bf019da7 | 8450 | struct io_submit_state *submit_state = &ctx->submit_state; |
e5547d2c | 8451 | struct io_comp_state *cs = &ctx->submit_state.comp; |
bf019da7 | 8452 | |
9a4fdbd8 JA |
8453 | mutex_lock(&ctx->uring_lock); |
8454 | ||
8e5c66c4 | 8455 | if (submit_state->free_reqs) { |
9a4fdbd8 JA |
8456 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
8457 | submit_state->reqs); | |
8e5c66c4 PB |
8458 | submit_state->free_reqs = 0; |
8459 | } | |
9a4fdbd8 | 8460 | |
dac7a098 | 8461 | io_flush_cached_locked_reqs(ctx, cs); |
e5547d2c | 8462 | io_req_cache_free(&cs->free_list, NULL); |
9a4fdbd8 JA |
8463 | mutex_unlock(&ctx->uring_lock); |
8464 | } | |
8465 | ||
2b188cc1 JA |
8466 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8467 | { | |
37d1e2e3 | 8468 | io_sq_thread_finish(ctx); |
0a96bbe4 | 8469 | io_sqe_buffers_unregister(ctx); |
2aede0e4 | 8470 | |
37d1e2e3 | 8471 | if (ctx->mm_account) { |
2aede0e4 JA |
8472 | mmdrop(ctx->mm_account); |
8473 | ctx->mm_account = NULL; | |
30975825 | 8474 | } |
def596e9 | 8475 | |
8bad28d8 | 8476 | mutex_lock(&ctx->uring_lock); |
6b06314c | 8477 | io_sqe_files_unregister(ctx); |
c4ea060e PB |
8478 | if (ctx->rings) |
8479 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8480 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8481 | io_eventfd_unregister(ctx); |
5a2e745d | 8482 | io_destroy_buffers(ctx); |
def596e9 | 8483 | |
a7f0ed5a PB |
8484 | /* there are no registered resources left, nobody uses it */ |
8485 | if (ctx->rsrc_node) | |
8486 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8487 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8488 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8489 | flush_delayed_work(&ctx->rsrc_put_work); |
8490 | ||
8491 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8492 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
8dd03afe | 8493 | |
2b188cc1 | 8494 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8495 | if (ctx->ring_sock) { |
8496 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8497 | sock_release(ctx->ring_sock); |
355e8d26 | 8498 | } |
2b188cc1 JA |
8499 | #endif |
8500 | ||
75b28aff | 8501 | io_mem_free(ctx->rings); |
2b188cc1 | 8502 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8503 | |
8504 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8505 | free_uid(ctx->user); |
4010fec4 | 8506 | io_req_caches_free(ctx); |
e941894e JA |
8507 | if (ctx->hash_map) |
8508 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8509 | kfree(ctx->cancel_hash); |
2b188cc1 JA |
8510 | kfree(ctx); |
8511 | } | |
8512 | ||
8513 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8514 | { | |
8515 | struct io_ring_ctx *ctx = file->private_data; | |
8516 | __poll_t mask = 0; | |
8517 | ||
8518 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
8519 | /* |
8520 | * synchronizes with barrier from wq_has_sleeper call in | |
8521 | * io_commit_cqring | |
8522 | */ | |
2b188cc1 | 8523 | smp_rmb(); |
90554200 | 8524 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8525 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8526 | |
8527 | /* | |
8528 | * Don't flush cqring overflow list here, just do a simple check. | |
8529 | * Otherwise there could possible be ABBA deadlock: | |
8530 | * CPU0 CPU1 | |
8531 | * ---- ---- | |
8532 | * lock(&ctx->uring_lock); | |
8533 | * lock(&ep->mtx); | |
8534 | * lock(&ctx->uring_lock); | |
8535 | * lock(&ep->mtx); | |
8536 | * | |
8537 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8538 | * pushs them to do the flush. | |
8539 | */ | |
8540 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) | |
2b188cc1 JA |
8541 | mask |= EPOLLIN | EPOLLRDNORM; |
8542 | ||
8543 | return mask; | |
8544 | } | |
8545 | ||
8546 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8547 | { | |
8548 | struct io_ring_ctx *ctx = file->private_data; | |
8549 | ||
8550 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8551 | } | |
8552 | ||
0bead8cd | 8553 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8554 | { |
4379bf8b | 8555 | const struct cred *creds; |
071698e1 | 8556 | |
61cf9370 | 8557 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8558 | if (creds) { |
8559 | put_cred(creds); | |
0bead8cd | 8560 | return 0; |
1e6fa521 | 8561 | } |
0bead8cd YD |
8562 | |
8563 | return -EINVAL; | |
8564 | } | |
8565 | ||
9b465711 | 8566 | static inline bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
7c25c0d1 | 8567 | { |
9b465711 | 8568 | return io_run_task_work_head(&ctx->exit_task_work); |
7c25c0d1 JA |
8569 | } |
8570 | ||
d56d938b PB |
8571 | struct io_tctx_exit { |
8572 | struct callback_head task_work; | |
8573 | struct completion completion; | |
baf186c4 | 8574 | struct io_ring_ctx *ctx; |
d56d938b PB |
8575 | }; |
8576 | ||
8577 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8578 | { | |
8579 | struct io_uring_task *tctx = current->io_uring; | |
8580 | struct io_tctx_exit *work; | |
8581 | ||
8582 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8583 | /* | |
8584 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8585 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8586 | */ | |
8587 | if (!atomic_read(&tctx->in_idle)) | |
baf186c4 | 8588 | io_uring_del_task_file((unsigned long)work->ctx); |
d56d938b PB |
8589 | complete(&work->completion); |
8590 | } | |
8591 | ||
85faa7b8 JA |
8592 | static void io_ring_exit_work(struct work_struct *work) |
8593 | { | |
d56d938b | 8594 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8595 | unsigned long timeout = jiffies + HZ * 60 * 5; |
d56d938b PB |
8596 | struct io_tctx_exit exit; |
8597 | struct io_tctx_node *node; | |
8598 | int ret; | |
85faa7b8 | 8599 | |
a185f1db PB |
8600 | /* prevent SQPOLL from submitting new requests */ |
8601 | if (ctx->sq_data) { | |
8602 | io_sq_thread_park(ctx->sq_data); | |
8603 | list_del_init(&ctx->sqd_list); | |
8604 | io_sqd_update_thread_idle(ctx->sq_data); | |
8605 | io_sq_thread_unpark(ctx->sq_data); | |
8606 | } | |
8607 | ||
56952e91 JA |
8608 | /* |
8609 | * If we're doing polled IO and end up having requests being | |
8610 | * submitted async (out-of-line), then completions can come in while | |
8611 | * we're waiting for refs to drop. We need to reap these manually, | |
8612 | * as nobody else will be looking for them. | |
8613 | */ | |
b2edc0a7 | 8614 | do { |
9936c7c2 | 8615 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
b5bb3a24 PB |
8616 | |
8617 | WARN_ON_ONCE(time_after(jiffies, timeout)); | |
b2edc0a7 | 8618 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
d56d938b | 8619 | |
89b5066e PB |
8620 | /* |
8621 | * Some may use context even when all refs and requests have been put, | |
8622 | * and they are free to do so while still holding uring_lock or | |
8623 | * completion_lock, see __io_req_task_submit(). Apart from other work, | |
8624 | * this lock/unlock section also waits them to finish. | |
8625 | */ | |
d56d938b PB |
8626 | mutex_lock(&ctx->uring_lock); |
8627 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
8628 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
8629 | ||
d56d938b PB |
8630 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
8631 | ctx_node); | |
baf186c4 | 8632 | exit.ctx = ctx; |
d56d938b PB |
8633 | init_completion(&exit.completion); |
8634 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
8635 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); | |
8636 | if (WARN_ON_ONCE(ret)) | |
8637 | continue; | |
8638 | wake_up_process(node->task); | |
8639 | ||
8640 | mutex_unlock(&ctx->uring_lock); | |
8641 | wait_for_completion(&exit.completion); | |
8642 | cond_resched(); | |
8643 | mutex_lock(&ctx->uring_lock); | |
8644 | } | |
8645 | mutex_unlock(&ctx->uring_lock); | |
89b5066e PB |
8646 | spin_lock_irq(&ctx->completion_lock); |
8647 | spin_unlock_irq(&ctx->completion_lock); | |
d56d938b | 8648 | |
85faa7b8 JA |
8649 | io_ring_ctx_free(ctx); |
8650 | } | |
8651 | ||
80c4cbdb PB |
8652 | /* Returns true if we found and killed one or more timeouts */ |
8653 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
8654 | struct files_struct *files) | |
8655 | { | |
8656 | struct io_kiocb *req, *tmp; | |
8657 | int canceled = 0; | |
8658 | ||
8659 | spin_lock_irq(&ctx->completion_lock); | |
8660 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { | |
8661 | if (io_match_task(req, tsk, files)) { | |
8662 | io_kill_timeout(req, -ECANCELED); | |
8663 | canceled++; | |
8664 | } | |
8665 | } | |
51520426 PB |
8666 | if (canceled != 0) |
8667 | io_commit_cqring(ctx); | |
80c4cbdb | 8668 | spin_unlock_irq(&ctx->completion_lock); |
80c4cbdb PB |
8669 | if (canceled != 0) |
8670 | io_cqring_ev_posted(ctx); | |
8671 | return canceled != 0; | |
8672 | } | |
8673 | ||
2b188cc1 JA |
8674 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8675 | { | |
61cf9370 MWO |
8676 | unsigned long index; |
8677 | struct creds *creds; | |
8678 | ||
2b188cc1 JA |
8679 | mutex_lock(&ctx->uring_lock); |
8680 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 8681 | if (ctx->rings) |
6c2450ae | 8682 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
8683 | xa_for_each(&ctx->personalities, index, creds) |
8684 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
8685 | mutex_unlock(&ctx->uring_lock); |
8686 | ||
6b81928d PB |
8687 | io_kill_timeouts(ctx, NULL, NULL); |
8688 | io_poll_remove_all(ctx, NULL, NULL); | |
561fb04a | 8689 | |
15dff286 | 8690 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8691 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8692 | |
85faa7b8 | 8693 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8694 | /* |
8695 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8696 | * if we're exiting a ton of rings at the same time. It just adds | |
8697 | * noise and overhead, there's no discernable change in runtime | |
8698 | * over using system_wq. | |
8699 | */ | |
8700 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8701 | } |
8702 | ||
8703 | static int io_uring_release(struct inode *inode, struct file *file) | |
8704 | { | |
8705 | struct io_ring_ctx *ctx = file->private_data; | |
8706 | ||
8707 | file->private_data = NULL; | |
8708 | io_ring_ctx_wait_and_kill(ctx); | |
8709 | return 0; | |
8710 | } | |
8711 | ||
f6edbabb PB |
8712 | struct io_task_cancel { |
8713 | struct task_struct *task; | |
8714 | struct files_struct *files; | |
8715 | }; | |
f254ac04 | 8716 | |
f6edbabb | 8717 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8718 | { |
9a472ef7 | 8719 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8720 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8721 | bool ret; |
8722 | ||
f6edbabb | 8723 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8724 | unsigned long flags; |
8725 | struct io_ring_ctx *ctx = req->ctx; | |
8726 | ||
8727 | /* protect against races with linked timeouts */ | |
8728 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
f6edbabb | 8729 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8730 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8731 | } else { | |
f6edbabb | 8732 | ret = io_match_task(req, cancel->task, cancel->files); |
9a472ef7 PB |
8733 | } |
8734 | return ret; | |
b711d4ea JA |
8735 | } |
8736 | ||
e1915f76 | 8737 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
ef9865a4 | 8738 | struct task_struct *task, |
b7ddce3c PB |
8739 | struct files_struct *files) |
8740 | { | |
e1915f76 | 8741 | struct io_defer_entry *de; |
b7ddce3c PB |
8742 | LIST_HEAD(list); |
8743 | ||
8744 | spin_lock_irq(&ctx->completion_lock); | |
8745 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
08d23634 | 8746 | if (io_match_task(de->req, task, files)) { |
b7ddce3c PB |
8747 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8748 | break; | |
8749 | } | |
8750 | } | |
8751 | spin_unlock_irq(&ctx->completion_lock); | |
e1915f76 PB |
8752 | if (list_empty(&list)) |
8753 | return false; | |
b7ddce3c PB |
8754 | |
8755 | while (!list_empty(&list)) { | |
8756 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8757 | list_del_init(&de->list); | |
f41db273 | 8758 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
8759 | kfree(de); |
8760 | } | |
e1915f76 | 8761 | return true; |
b7ddce3c PB |
8762 | } |
8763 | ||
1b00764f PB |
8764 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8765 | { | |
8766 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8767 | ||
8768 | return req->ctx == data; | |
8769 | } | |
8770 | ||
8771 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) | |
8772 | { | |
8773 | struct io_tctx_node *node; | |
8774 | enum io_wq_cancel cret; | |
8775 | bool ret = false; | |
8776 | ||
8777 | mutex_lock(&ctx->uring_lock); | |
8778 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
8779 | struct io_uring_task *tctx = node->task->io_uring; | |
8780 | ||
8781 | /* | |
8782 | * io_wq will stay alive while we hold uring_lock, because it's | |
8783 | * killed after ctx nodes, which requires to take the lock. | |
8784 | */ | |
8785 | if (!tctx || !tctx->io_wq) | |
8786 | continue; | |
8787 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
8788 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8789 | } | |
8790 | mutex_unlock(&ctx->uring_lock); | |
8791 | ||
8792 | return ret; | |
8793 | } | |
8794 | ||
9936c7c2 PB |
8795 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8796 | struct task_struct *task, | |
8797 | struct files_struct *files) | |
8798 | { | |
8799 | struct io_task_cancel cancel = { .task = task, .files = files, }; | |
1b00764f | 8800 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
8801 | |
8802 | while (1) { | |
8803 | enum io_wq_cancel cret; | |
8804 | bool ret = false; | |
8805 | ||
1b00764f PB |
8806 | if (!task) { |
8807 | ret |= io_uring_try_cancel_iowq(ctx); | |
8808 | } else if (tctx && tctx->io_wq) { | |
8809 | /* | |
8810 | * Cancels requests of all rings, not only @ctx, but | |
8811 | * it's fine as the task is in exit/exec. | |
8812 | */ | |
5aa75ed5 | 8813 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
8814 | &cancel, true); |
8815 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8816 | } | |
8817 | ||
8818 | /* SQPOLL thread does its own polling */ | |
d052d1d6 JA |
8819 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && !files) || |
8820 | (ctx->sq_data && ctx->sq_data->thread == current)) { | |
9936c7c2 PB |
8821 | while (!list_empty_careful(&ctx->iopoll_list)) { |
8822 | io_iopoll_try_reap_events(ctx); | |
8823 | ret = true; | |
8824 | } | |
8825 | } | |
8826 | ||
e1915f76 | 8827 | ret |= io_cancel_defer_files(ctx, task, files); |
9936c7c2 PB |
8828 | ret |= io_poll_remove_all(ctx, task, files); |
8829 | ret |= io_kill_timeouts(ctx, task, files); | |
8830 | ret |= io_run_task_work(); | |
ba50a036 | 8831 | ret |= io_run_ctx_fallback(ctx); |
9936c7c2 PB |
8832 | if (!ret) |
8833 | break; | |
8834 | cond_resched(); | |
8835 | } | |
8836 | } | |
8837 | ||
cf27f3b1 | 8838 | static int __io_uring_add_task_file(struct io_ring_ctx *ctx) |
0f212204 | 8839 | { |
236434c3 | 8840 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 8841 | struct io_tctx_node *node; |
a528b04e | 8842 | int ret; |
236434c3 MWO |
8843 | |
8844 | if (unlikely(!tctx)) { | |
5aa75ed5 | 8845 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
8846 | if (unlikely(ret)) |
8847 | return ret; | |
236434c3 | 8848 | tctx = current->io_uring; |
0f212204 | 8849 | } |
cf27f3b1 PB |
8850 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
8851 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
8852 | if (!node) | |
8853 | return -ENOMEM; | |
8854 | node->ctx = ctx; | |
8855 | node->task = current; | |
13bf43f5 | 8856 | |
cf27f3b1 PB |
8857 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
8858 | node, GFP_KERNEL)); | |
8859 | if (ret) { | |
8860 | kfree(node); | |
8861 | return ret; | |
0f212204 | 8862 | } |
cf27f3b1 PB |
8863 | |
8864 | mutex_lock(&ctx->uring_lock); | |
8865 | list_add(&node->ctx_node, &ctx->tctx_list); | |
8866 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 8867 | } |
cf27f3b1 | 8868 | tctx->last = ctx; |
0f212204 JA |
8869 | return 0; |
8870 | } | |
8871 | ||
cf27f3b1 PB |
8872 | /* |
8873 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
8874 | */ | |
8875 | static inline int io_uring_add_task_file(struct io_ring_ctx *ctx) | |
8876 | { | |
8877 | struct io_uring_task *tctx = current->io_uring; | |
8878 | ||
8879 | if (likely(tctx && tctx->last == ctx)) | |
8880 | return 0; | |
8881 | return __io_uring_add_task_file(ctx); | |
8882 | } | |
8883 | ||
0f212204 JA |
8884 | /* |
8885 | * Remove this io_uring_file -> task mapping. | |
8886 | */ | |
2941267b | 8887 | static void io_uring_del_task_file(unsigned long index) |
0f212204 JA |
8888 | { |
8889 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 8890 | struct io_tctx_node *node; |
2941267b | 8891 | |
eebd2e37 PB |
8892 | if (!tctx) |
8893 | return; | |
13bf43f5 PB |
8894 | node = xa_erase(&tctx->xa, index); |
8895 | if (!node) | |
2941267b | 8896 | return; |
0f212204 | 8897 | |
13bf43f5 PB |
8898 | WARN_ON_ONCE(current != node->task); |
8899 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
8900 | ||
8901 | mutex_lock(&node->ctx->uring_lock); | |
8902 | list_del(&node->ctx_node); | |
8903 | mutex_unlock(&node->ctx->uring_lock); | |
8904 | ||
baf186c4 | 8905 | if (tctx->last == node->ctx) |
0f212204 | 8906 | tctx->last = NULL; |
13bf43f5 | 8907 | kfree(node); |
0f212204 JA |
8908 | } |
8909 | ||
8452d4a6 | 8910 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 8911 | { |
13bf43f5 | 8912 | struct io_tctx_node *node; |
de7f1d9e PB |
8913 | unsigned long index; |
8914 | ||
13bf43f5 | 8915 | xa_for_each(&tctx->xa, index, node) |
2941267b | 8916 | io_uring_del_task_file(index); |
8452d4a6 PB |
8917 | if (tctx->io_wq) { |
8918 | io_wq_put_and_exit(tctx->io_wq); | |
8919 | tctx->io_wq = NULL; | |
8920 | } | |
de7f1d9e PB |
8921 | } |
8922 | ||
b303fe2e | 8923 | static s64 tctx_inflight_tracked(struct io_uring_task *tctx) |
368b2080 | 8924 | { |
b303fe2e | 8925 | return atomic_read(&tctx->inflight_tracked); |
368b2080 PB |
8926 | } |
8927 | ||
521d6a73 PB |
8928 | static s64 tctx_inflight(struct io_uring_task *tctx) |
8929 | { | |
8930 | return percpu_counter_sum(&tctx->inflight); | |
8931 | } | |
8932 | ||
8933 | static void io_sqpoll_cancel_cb(struct callback_head *cb) | |
8934 | { | |
8935 | struct io_tctx_exit *work = container_of(cb, struct io_tctx_exit, task_work); | |
8936 | struct io_ring_ctx *ctx = work->ctx; | |
8937 | struct io_sq_data *sqd = ctx->sq_data; | |
8938 | ||
8939 | if (sqd->thread) | |
8940 | io_uring_cancel_sqpoll(ctx); | |
8941 | complete(&work->completion); | |
8942 | } | |
8943 | ||
8944 | static void io_sqpoll_cancel_sync(struct io_ring_ctx *ctx) | |
8945 | { | |
8946 | struct io_sq_data *sqd = ctx->sq_data; | |
8947 | struct io_tctx_exit work = { .ctx = ctx, }; | |
8948 | struct task_struct *task; | |
8949 | ||
8950 | io_sq_thread_park(sqd); | |
8951 | list_del_init(&ctx->sqd_list); | |
8952 | io_sqd_update_thread_idle(sqd); | |
8953 | task = sqd->thread; | |
8954 | if (task) { | |
8955 | init_completion(&work.completion); | |
8956 | init_task_work(&work.task_work, io_sqpoll_cancel_cb); | |
b7f5a0bf | 8957 | io_task_work_add_head(&sqd->park_task_work, &work.task_work); |
521d6a73 PB |
8958 | wake_up_process(task); |
8959 | } | |
8960 | io_sq_thread_unpark(sqd); | |
8961 | ||
8962 | if (task) | |
8963 | wait_for_completion(&work.completion); | |
8964 | } | |
8965 | ||
368b2080 | 8966 | static void io_uring_try_cancel(struct files_struct *files) |
0f212204 JA |
8967 | { |
8968 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 8969 | struct io_tctx_node *node; |
ce765372 | 8970 | unsigned long index; |
0f212204 | 8971 | |
521d6a73 PB |
8972 | xa_for_each(&tctx->xa, index, node) { |
8973 | struct io_ring_ctx *ctx = node->ctx; | |
8974 | ||
8975 | if (ctx->sq_data) { | |
8976 | io_sqpoll_cancel_sync(ctx); | |
8977 | continue; | |
8978 | } | |
368b2080 | 8979 | io_uring_try_cancel_requests(ctx, current, files); |
521d6a73 | 8980 | } |
fdaf083c JA |
8981 | } |
8982 | ||
521d6a73 | 8983 | /* should only be called by SQPOLL task */ |
0e9ddb39 PB |
8984 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
8985 | { | |
37d1e2e3 | 8986 | struct io_sq_data *sqd = ctx->sq_data; |
521d6a73 | 8987 | struct io_uring_task *tctx = current->io_uring; |
0e9ddb39 PB |
8988 | s64 inflight; |
8989 | DEFINE_WAIT(wait); | |
fdaf083c | 8990 | |
521d6a73 PB |
8991 | WARN_ON_ONCE(!sqd || ctx->sq_data->thread != current); |
8992 | ||
0e9ddb39 PB |
8993 | atomic_inc(&tctx->in_idle); |
8994 | do { | |
8995 | /* read completions before cancelations */ | |
8996 | inflight = tctx_inflight(tctx); | |
8997 | if (!inflight) | |
8998 | break; | |
521d6a73 | 8999 | io_uring_try_cancel_requests(ctx, current, NULL); |
fdaf083c | 9000 | |
0e9ddb39 PB |
9001 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
9002 | /* | |
9003 | * If we've seen completions, retry without waiting. This | |
9004 | * avoids a race where a completion comes in before we did | |
9005 | * prepare_to_wait(). | |
9006 | */ | |
9007 | if (inflight == tctx_inflight(tctx)) | |
9008 | schedule(); | |
9009 | finish_wait(&tctx->wait, &wait); | |
9010 | } while (1); | |
9011 | atomic_dec(&tctx->in_idle); | |
0f212204 JA |
9012 | } |
9013 | ||
368b2080 PB |
9014 | void __io_uring_files_cancel(struct files_struct *files) |
9015 | { | |
9016 | struct io_uring_task *tctx = current->io_uring; | |
9017 | DEFINE_WAIT(wait); | |
9018 | s64 inflight; | |
9019 | ||
9020 | /* make sure overflow events are dropped */ | |
9021 | atomic_inc(&tctx->in_idle); | |
9022 | do { | |
9023 | /* read completions before cancelations */ | |
b303fe2e | 9024 | inflight = tctx_inflight_tracked(tctx); |
368b2080 PB |
9025 | if (!inflight) |
9026 | break; | |
9027 | io_uring_try_cancel(files); | |
9028 | ||
9029 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
b303fe2e | 9030 | if (inflight == tctx_inflight_tracked(tctx)) |
368b2080 PB |
9031 | schedule(); |
9032 | finish_wait(&tctx->wait, &wait); | |
9033 | } while (1); | |
9034 | atomic_dec(&tctx->in_idle); | |
9035 | ||
9036 | io_uring_clean_tctx(tctx); | |
9037 | } | |
9038 | ||
0f212204 JA |
9039 | /* |
9040 | * Find any io_uring fd that this task has registered or done IO on, and cancel | |
9041 | * requests. | |
9042 | */ | |
9043 | void __io_uring_task_cancel(void) | |
9044 | { | |
9045 | struct io_uring_task *tctx = current->io_uring; | |
9046 | DEFINE_WAIT(wait); | |
d8a6df10 | 9047 | s64 inflight; |
0f212204 JA |
9048 | |
9049 | /* make sure overflow events are dropped */ | |
fdaf083c | 9050 | atomic_inc(&tctx->in_idle); |
5a978dcf PB |
9051 | __io_uring_files_cancel(NULL); |
9052 | ||
d8a6df10 | 9053 | do { |
0f212204 | 9054 | /* read completions before cancelations */ |
fdaf083c | 9055 | inflight = tctx_inflight(tctx); |
d8a6df10 JA |
9056 | if (!inflight) |
9057 | break; | |
368b2080 | 9058 | io_uring_try_cancel(NULL); |
0f212204 JA |
9059 | |
9060 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); | |
9061 | ||
9062 | /* | |
a1bb3cd5 PB |
9063 | * If we've seen completions, retry without waiting. This |
9064 | * avoids a race where a completion comes in before we did | |
9065 | * prepare_to_wait(). | |
0f212204 | 9066 | */ |
a1bb3cd5 PB |
9067 | if (inflight == tctx_inflight(tctx)) |
9068 | schedule(); | |
f57555ed | 9069 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9070 | } while (1); |
0f212204 | 9071 | |
fdaf083c | 9072 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9073 | |
8452d4a6 PB |
9074 | io_uring_clean_tctx(tctx); |
9075 | /* all current's requests should be gone, we can kill tctx */ | |
9076 | __io_uring_free(current); | |
44e728b8 PB |
9077 | } |
9078 | ||
6c5c240e RP |
9079 | static void *io_uring_validate_mmap_request(struct file *file, |
9080 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9081 | { |
2b188cc1 | 9082 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9083 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9084 | struct page *page; |
9085 | void *ptr; | |
9086 | ||
9087 | switch (offset) { | |
9088 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9089 | case IORING_OFF_CQ_RING: |
9090 | ptr = ctx->rings; | |
2b188cc1 JA |
9091 | break; |
9092 | case IORING_OFF_SQES: | |
9093 | ptr = ctx->sq_sqes; | |
9094 | break; | |
2b188cc1 | 9095 | default: |
6c5c240e | 9096 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9097 | } |
9098 | ||
9099 | page = virt_to_head_page(ptr); | |
a50b854e | 9100 | if (sz > page_size(page)) |
6c5c240e RP |
9101 | return ERR_PTR(-EINVAL); |
9102 | ||
9103 | return ptr; | |
9104 | } | |
9105 | ||
9106 | #ifdef CONFIG_MMU | |
9107 | ||
9108 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9109 | { | |
9110 | size_t sz = vma->vm_end - vma->vm_start; | |
9111 | unsigned long pfn; | |
9112 | void *ptr; | |
9113 | ||
9114 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9115 | if (IS_ERR(ptr)) | |
9116 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9117 | |
9118 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9119 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9120 | } | |
9121 | ||
6c5c240e RP |
9122 | #else /* !CONFIG_MMU */ |
9123 | ||
9124 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9125 | { | |
9126 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9127 | } | |
9128 | ||
9129 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9130 | { | |
9131 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9132 | } | |
9133 | ||
9134 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9135 | unsigned long addr, unsigned long len, | |
9136 | unsigned long pgoff, unsigned long flags) | |
9137 | { | |
9138 | void *ptr; | |
9139 | ||
9140 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9141 | if (IS_ERR(ptr)) | |
9142 | return PTR_ERR(ptr); | |
9143 | ||
9144 | return (unsigned long) ptr; | |
9145 | } | |
9146 | ||
9147 | #endif /* !CONFIG_MMU */ | |
9148 | ||
d9d05217 | 9149 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9150 | { |
9151 | DEFINE_WAIT(wait); | |
9152 | ||
9153 | do { | |
9154 | if (!io_sqring_full(ctx)) | |
9155 | break; | |
90554200 JA |
9156 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9157 | ||
9158 | if (!io_sqring_full(ctx)) | |
9159 | break; | |
90554200 JA |
9160 | schedule(); |
9161 | } while (!signal_pending(current)); | |
9162 | ||
9163 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9164 | return 0; |
90554200 JA |
9165 | } |
9166 | ||
c73ebb68 HX |
9167 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9168 | struct __kernel_timespec __user **ts, | |
9169 | const sigset_t __user **sig) | |
9170 | { | |
9171 | struct io_uring_getevents_arg arg; | |
9172 | ||
9173 | /* | |
9174 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9175 | * is just a pointer to the sigset_t. | |
9176 | */ | |
9177 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9178 | *sig = (const sigset_t __user *) argp; | |
9179 | *ts = NULL; | |
9180 | return 0; | |
9181 | } | |
9182 | ||
9183 | /* | |
9184 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9185 | * timespec and sigset_t pointers if good. | |
9186 | */ | |
9187 | if (*argsz != sizeof(arg)) | |
9188 | return -EINVAL; | |
9189 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9190 | return -EFAULT; | |
9191 | *sig = u64_to_user_ptr(arg.sigmask); | |
9192 | *argsz = arg.sigmask_sz; | |
9193 | *ts = u64_to_user_ptr(arg.ts); | |
9194 | return 0; | |
9195 | } | |
9196 | ||
2b188cc1 | 9197 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9198 | u32, min_complete, u32, flags, const void __user *, argp, |
9199 | size_t, argsz) | |
2b188cc1 JA |
9200 | { |
9201 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9202 | int submitted = 0; |
9203 | struct fd f; | |
33f993da | 9204 | long ret; |
2b188cc1 | 9205 | |
4c6e277c | 9206 | io_run_task_work(); |
b41e9852 | 9207 | |
33f993da PB |
9208 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9209 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9210 | return -EINVAL; |
9211 | ||
9212 | f = fdget(fd); | |
33f993da | 9213 | if (unlikely(!f.file)) |
2b188cc1 JA |
9214 | return -EBADF; |
9215 | ||
9216 | ret = -EOPNOTSUPP; | |
33f993da | 9217 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9218 | goto out_fput; |
9219 | ||
9220 | ret = -ENXIO; | |
9221 | ctx = f.file->private_data; | |
33f993da | 9222 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9223 | goto out_fput; |
9224 | ||
7e84e1c7 | 9225 | ret = -EBADFD; |
33f993da | 9226 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9227 | goto out; |
9228 | ||
6c271ce2 JA |
9229 | /* |
9230 | * For SQ polling, the thread will do all submissions and completions. | |
9231 | * Just return the requested submit count, and wake the thread if | |
9232 | * we were asked to. | |
9233 | */ | |
b2a9eada | 9234 | ret = 0; |
6c271ce2 | 9235 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c2450ae | 9236 | io_cqring_overflow_flush(ctx, false); |
89448c47 | 9237 | |
d9d05217 | 9238 | ret = -EOWNERDEAD; |
04147488 SM |
9239 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9240 | goto out; | |
9241 | } | |
6c271ce2 | 9242 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9243 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9244 | if (flags & IORING_ENTER_SQ_WAIT) { |
9245 | ret = io_sqpoll_wait_sq(ctx); | |
9246 | if (ret) | |
9247 | goto out; | |
9248 | } | |
6c271ce2 | 9249 | submitted = to_submit; |
b2a9eada | 9250 | } else if (to_submit) { |
baf186c4 | 9251 | ret = io_uring_add_task_file(ctx); |
0f212204 JA |
9252 | if (unlikely(ret)) |
9253 | goto out; | |
2b188cc1 | 9254 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9255 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9256 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9257 | |
9258 | if (submitted != to_submit) | |
9259 | goto out; | |
2b188cc1 JA |
9260 | } |
9261 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9262 | const sigset_t __user *sig; |
9263 | struct __kernel_timespec __user *ts; | |
9264 | ||
9265 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9266 | if (unlikely(ret)) | |
9267 | goto out; | |
9268 | ||
2b188cc1 JA |
9269 | min_complete = min(min_complete, ctx->cq_entries); |
9270 | ||
32b2244a XW |
9271 | /* |
9272 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9273 | * space applications don't need to do io completion events | |
9274 | * polling again, they can rely on io_sq_thread to do polling | |
9275 | * work, which can reduce cpu usage and uring_lock contention. | |
9276 | */ | |
9277 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9278 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9279 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9280 | } else { |
c73ebb68 | 9281 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9282 | } |
2b188cc1 JA |
9283 | } |
9284 | ||
7c504e65 | 9285 | out: |
6805b32e | 9286 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9287 | out_fput: |
9288 | fdput(f); | |
9289 | return submitted ? submitted : ret; | |
9290 | } | |
9291 | ||
bebdb65e | 9292 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9293 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9294 | const struct cred *cred) | |
87ce955b | 9295 | { |
87ce955b JA |
9296 | struct user_namespace *uns = seq_user_ns(m); |
9297 | struct group_info *gi; | |
9298 | kernel_cap_t cap; | |
9299 | unsigned __capi; | |
9300 | int g; | |
9301 | ||
9302 | seq_printf(m, "%5d\n", id); | |
9303 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9304 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9305 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9306 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9307 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9308 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9309 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9310 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9311 | seq_puts(m, "\n\tGroups:\t"); | |
9312 | gi = cred->group_info; | |
9313 | for (g = 0; g < gi->ngroups; g++) { | |
9314 | seq_put_decimal_ull(m, g ? " " : "", | |
9315 | from_kgid_munged(uns, gi->gid[g])); | |
9316 | } | |
9317 | seq_puts(m, "\n\tCapEff:\t"); | |
9318 | cap = cred->cap_effective; | |
9319 | CAP_FOR_EACH_U32(__capi) | |
9320 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9321 | seq_putc(m, '\n'); | |
9322 | return 0; | |
9323 | } | |
9324 | ||
9325 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9326 | { | |
dbbe9c64 | 9327 | struct io_sq_data *sq = NULL; |
fad8e0de | 9328 | bool has_lock; |
87ce955b JA |
9329 | int i; |
9330 | ||
fad8e0de JA |
9331 | /* |
9332 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9333 | * since fdinfo case grabs it in the opposite direction of normal use | |
9334 | * cases. If we fail to get the lock, we just don't iterate any | |
9335 | * structures that could be going away outside the io_uring mutex. | |
9336 | */ | |
9337 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9338 | ||
5f3f26f9 | 9339 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9340 | sq = ctx->sq_data; |
5f3f26f9 JA |
9341 | if (!sq->thread) |
9342 | sq = NULL; | |
9343 | } | |
dbbe9c64 JQ |
9344 | |
9345 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9346 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9347 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9348 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9349 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9350 | |
87ce955b JA |
9351 | if (f) |
9352 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9353 | else | |
9354 | seq_printf(m, "%5u: <none>\n", i); | |
9355 | } | |
9356 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9357 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
87ce955b | 9358 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
4751f53d | 9359 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9360 | |
4751f53d | 9361 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9362 | } |
61cf9370 MWO |
9363 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9364 | unsigned long index; | |
9365 | const struct cred *cred; | |
9366 | ||
87ce955b | 9367 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9368 | xa_for_each(&ctx->personalities, index, cred) |
9369 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9370 | } |
d7718a9d JA |
9371 | seq_printf(m, "PollList:\n"); |
9372 | spin_lock_irq(&ctx->completion_lock); | |
9373 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9374 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9375 | struct io_kiocb *req; | |
9376 | ||
9377 | hlist_for_each_entry(req, list, hash_node) | |
9378 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9379 | req->task->task_works != NULL); | |
9380 | } | |
9381 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9382 | if (has_lock) |
9383 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9384 | } |
9385 | ||
9386 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9387 | { | |
9388 | struct io_ring_ctx *ctx = f->private_data; | |
9389 | ||
9390 | if (percpu_ref_tryget(&ctx->refs)) { | |
9391 | __io_uring_show_fdinfo(ctx, m); | |
9392 | percpu_ref_put(&ctx->refs); | |
9393 | } | |
9394 | } | |
bebdb65e | 9395 | #endif |
87ce955b | 9396 | |
2b188cc1 JA |
9397 | static const struct file_operations io_uring_fops = { |
9398 | .release = io_uring_release, | |
9399 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9400 | #ifndef CONFIG_MMU |
9401 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9402 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9403 | #endif | |
2b188cc1 JA |
9404 | .poll = io_uring_poll, |
9405 | .fasync = io_uring_fasync, | |
bebdb65e | 9406 | #ifdef CONFIG_PROC_FS |
87ce955b | 9407 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9408 | #endif |
2b188cc1 JA |
9409 | }; |
9410 | ||
9411 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9412 | struct io_uring_params *p) | |
9413 | { | |
75b28aff HV |
9414 | struct io_rings *rings; |
9415 | size_t size, sq_array_offset; | |
2b188cc1 | 9416 | |
bd740481 JA |
9417 | /* make sure these are sane, as we already accounted them */ |
9418 | ctx->sq_entries = p->sq_entries; | |
9419 | ctx->cq_entries = p->cq_entries; | |
9420 | ||
75b28aff HV |
9421 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9422 | if (size == SIZE_MAX) | |
9423 | return -EOVERFLOW; | |
9424 | ||
9425 | rings = io_mem_alloc(size); | |
9426 | if (!rings) | |
2b188cc1 JA |
9427 | return -ENOMEM; |
9428 | ||
75b28aff HV |
9429 | ctx->rings = rings; |
9430 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9431 | rings->sq_ring_mask = p->sq_entries - 1; | |
9432 | rings->cq_ring_mask = p->cq_entries - 1; | |
9433 | rings->sq_ring_entries = p->sq_entries; | |
9434 | rings->cq_ring_entries = p->cq_entries; | |
9435 | ctx->sq_mask = rings->sq_ring_mask; | |
9436 | ctx->cq_mask = rings->cq_ring_mask; | |
2b188cc1 JA |
9437 | |
9438 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9439 | if (size == SIZE_MAX) { |
9440 | io_mem_free(ctx->rings); | |
9441 | ctx->rings = NULL; | |
2b188cc1 | 9442 | return -EOVERFLOW; |
eb065d30 | 9443 | } |
2b188cc1 JA |
9444 | |
9445 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9446 | if (!ctx->sq_sqes) { |
9447 | io_mem_free(ctx->rings); | |
9448 | ctx->rings = NULL; | |
2b188cc1 | 9449 | return -ENOMEM; |
eb065d30 | 9450 | } |
2b188cc1 | 9451 | |
2b188cc1 JA |
9452 | return 0; |
9453 | } | |
9454 | ||
9faadcc8 PB |
9455 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9456 | { | |
9457 | int ret, fd; | |
9458 | ||
9459 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9460 | if (fd < 0) | |
9461 | return fd; | |
9462 | ||
baf186c4 | 9463 | ret = io_uring_add_task_file(ctx); |
9faadcc8 PB |
9464 | if (ret) { |
9465 | put_unused_fd(fd); | |
9466 | return ret; | |
9467 | } | |
9468 | fd_install(fd, file); | |
9469 | return fd; | |
9470 | } | |
9471 | ||
2b188cc1 JA |
9472 | /* |
9473 | * Allocate an anonymous fd, this is what constitutes the application | |
9474 | * visible backing of an io_uring instance. The application mmaps this | |
9475 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9476 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9477 | */ | |
9faadcc8 | 9478 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9479 | { |
9480 | struct file *file; | |
9faadcc8 | 9481 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9482 | int ret; |
9483 | ||
2b188cc1 JA |
9484 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9485 | &ctx->ring_sock); | |
9486 | if (ret) | |
9faadcc8 | 9487 | return ERR_PTR(ret); |
2b188cc1 JA |
9488 | #endif |
9489 | ||
2b188cc1 JA |
9490 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9491 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9492 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9493 | if (IS_ERR(file)) { |
9494 | sock_release(ctx->ring_sock); | |
9495 | ctx->ring_sock = NULL; | |
9496 | } else { | |
9497 | ctx->ring_sock->file = file; | |
0f212204 | 9498 | } |
2b188cc1 | 9499 | #endif |
9faadcc8 | 9500 | return file; |
2b188cc1 JA |
9501 | } |
9502 | ||
7f13657d XW |
9503 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9504 | struct io_uring_params __user *params) | |
2b188cc1 | 9505 | { |
2b188cc1 | 9506 | struct io_ring_ctx *ctx; |
9faadcc8 | 9507 | struct file *file; |
2b188cc1 JA |
9508 | int ret; |
9509 | ||
8110c1a6 | 9510 | if (!entries) |
2b188cc1 | 9511 | return -EINVAL; |
8110c1a6 JA |
9512 | if (entries > IORING_MAX_ENTRIES) { |
9513 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9514 | return -EINVAL; | |
9515 | entries = IORING_MAX_ENTRIES; | |
9516 | } | |
2b188cc1 JA |
9517 | |
9518 | /* | |
9519 | * Use twice as many entries for the CQ ring. It's possible for the | |
9520 | * application to drive a higher depth than the size of the SQ ring, | |
9521 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9522 | * some flexibility in overcommitting a bit. If the application has |
9523 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9524 | * of CQ ring entries manually. | |
2b188cc1 JA |
9525 | */ |
9526 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9527 | if (p->flags & IORING_SETUP_CQSIZE) { |
9528 | /* | |
9529 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9530 | * to a power-of-two, if it isn't already. We do NOT impose | |
9531 | * any cq vs sq ring sizing. | |
9532 | */ | |
eb2667b3 | 9533 | if (!p->cq_entries) |
33a107f0 | 9534 | return -EINVAL; |
8110c1a6 JA |
9535 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9536 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9537 | return -EINVAL; | |
9538 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9539 | } | |
eb2667b3 JQ |
9540 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9541 | if (p->cq_entries < p->sq_entries) | |
9542 | return -EINVAL; | |
33a107f0 JA |
9543 | } else { |
9544 | p->cq_entries = 2 * p->sq_entries; | |
9545 | } | |
2b188cc1 | 9546 | |
2b188cc1 | 9547 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9548 | if (!ctx) |
2b188cc1 | 9549 | return -ENOMEM; |
2b188cc1 | 9550 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9551 | if (!capable(CAP_IPC_LOCK)) |
9552 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9553 | |
9554 | /* | |
9555 | * This is just grabbed for accounting purposes. When a process exits, | |
9556 | * the mm is exited and dropped before the files, hence we need to hang | |
9557 | * on to this mm purely for the purposes of being able to unaccount | |
9558 | * memory (locked/pinned vm). It's not used for anything else. | |
9559 | */ | |
6b7898eb | 9560 | mmgrab(current->mm); |
2aede0e4 | 9561 | ctx->mm_account = current->mm; |
6b7898eb | 9562 | |
2b188cc1 JA |
9563 | ret = io_allocate_scq_urings(ctx, p); |
9564 | if (ret) | |
9565 | goto err; | |
9566 | ||
7e84e1c7 | 9567 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9568 | if (ret) |
9569 | goto err; | |
9570 | ||
2b188cc1 | 9571 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9572 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9573 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9574 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9575 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9576 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9577 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9578 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9579 | |
9580 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9581 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9582 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9583 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9584 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9585 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9586 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9587 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9588 | |
7f13657d XW |
9589 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9590 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9591 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9592 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
1c0aa1fa | 9593 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
7f13657d XW |
9594 | |
9595 | if (copy_to_user(params, p, sizeof(*p))) { | |
9596 | ret = -EFAULT; | |
9597 | goto err; | |
9598 | } | |
d1719f70 | 9599 | |
9faadcc8 PB |
9600 | file = io_uring_get_file(ctx); |
9601 | if (IS_ERR(file)) { | |
9602 | ret = PTR_ERR(file); | |
9603 | goto err; | |
9604 | } | |
9605 | ||
044c1ab3 JA |
9606 | /* |
9607 | * Install ring fd as the very last thing, so we don't risk someone | |
9608 | * having closed it before we finish setup | |
9609 | */ | |
9faadcc8 PB |
9610 | ret = io_uring_install_fd(ctx, file); |
9611 | if (ret < 0) { | |
9612 | /* fput will clean it up */ | |
9613 | fput(file); | |
9614 | return ret; | |
9615 | } | |
044c1ab3 | 9616 | |
c826bd7a | 9617 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9618 | return ret; |
9619 | err: | |
9620 | io_ring_ctx_wait_and_kill(ctx); | |
9621 | return ret; | |
9622 | } | |
9623 | ||
9624 | /* | |
9625 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9626 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9627 | * params structure passed in. | |
9628 | */ | |
9629 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9630 | { | |
9631 | struct io_uring_params p; | |
2b188cc1 JA |
9632 | int i; |
9633 | ||
9634 | if (copy_from_user(&p, params, sizeof(p))) | |
9635 | return -EFAULT; | |
9636 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9637 | if (p.resv[i]) | |
9638 | return -EINVAL; | |
9639 | } | |
9640 | ||
6c271ce2 | 9641 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9642 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9643 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9644 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9645 | return -EINVAL; |
9646 | ||
7f13657d | 9647 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9648 | } |
9649 | ||
9650 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9651 | struct io_uring_params __user *, params) | |
9652 | { | |
9653 | return io_uring_setup(entries, params); | |
9654 | } | |
9655 | ||
66f4af93 JA |
9656 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9657 | { | |
9658 | struct io_uring_probe *p; | |
9659 | size_t size; | |
9660 | int i, ret; | |
9661 | ||
9662 | size = struct_size(p, ops, nr_args); | |
9663 | if (size == SIZE_MAX) | |
9664 | return -EOVERFLOW; | |
9665 | p = kzalloc(size, GFP_KERNEL); | |
9666 | if (!p) | |
9667 | return -ENOMEM; | |
9668 | ||
9669 | ret = -EFAULT; | |
9670 | if (copy_from_user(p, arg, size)) | |
9671 | goto out; | |
9672 | ret = -EINVAL; | |
9673 | if (memchr_inv(p, 0, size)) | |
9674 | goto out; | |
9675 | ||
9676 | p->last_op = IORING_OP_LAST - 1; | |
9677 | if (nr_args > IORING_OP_LAST) | |
9678 | nr_args = IORING_OP_LAST; | |
9679 | ||
9680 | for (i = 0; i < nr_args; i++) { | |
9681 | p->ops[i].op = i; | |
9682 | if (!io_op_defs[i].not_supported) | |
9683 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9684 | } | |
9685 | p->ops_len = i; | |
9686 | ||
9687 | ret = 0; | |
9688 | if (copy_to_user(arg, p, size)) | |
9689 | ret = -EFAULT; | |
9690 | out: | |
9691 | kfree(p); | |
9692 | return ret; | |
9693 | } | |
9694 | ||
071698e1 JA |
9695 | static int io_register_personality(struct io_ring_ctx *ctx) |
9696 | { | |
4379bf8b | 9697 | const struct cred *creds; |
61cf9370 | 9698 | u32 id; |
1e6fa521 | 9699 | int ret; |
071698e1 | 9700 | |
4379bf8b | 9701 | creds = get_current_cred(); |
1e6fa521 | 9702 | |
61cf9370 MWO |
9703 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
9704 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
9705 | if (!ret) | |
9706 | return id; | |
9707 | put_cred(creds); | |
1e6fa521 | 9708 | return ret; |
071698e1 JA |
9709 | } |
9710 | ||
21b55dbc SG |
9711 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9712 | unsigned int nr_args) | |
9713 | { | |
9714 | struct io_uring_restriction *res; | |
9715 | size_t size; | |
9716 | int i, ret; | |
9717 | ||
7e84e1c7 SG |
9718 | /* Restrictions allowed only if rings started disabled */ |
9719 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9720 | return -EBADFD; | |
9721 | ||
21b55dbc | 9722 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9723 | if (ctx->restrictions.registered) |
21b55dbc SG |
9724 | return -EBUSY; |
9725 | ||
9726 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9727 | return -EINVAL; | |
9728 | ||
9729 | size = array_size(nr_args, sizeof(*res)); | |
9730 | if (size == SIZE_MAX) | |
9731 | return -EOVERFLOW; | |
9732 | ||
9733 | res = memdup_user(arg, size); | |
9734 | if (IS_ERR(res)) | |
9735 | return PTR_ERR(res); | |
9736 | ||
9737 | ret = 0; | |
9738 | ||
9739 | for (i = 0; i < nr_args; i++) { | |
9740 | switch (res[i].opcode) { | |
9741 | case IORING_RESTRICTION_REGISTER_OP: | |
9742 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9743 | ret = -EINVAL; | |
9744 | goto out; | |
9745 | } | |
9746 | ||
9747 | __set_bit(res[i].register_op, | |
9748 | ctx->restrictions.register_op); | |
9749 | break; | |
9750 | case IORING_RESTRICTION_SQE_OP: | |
9751 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9752 | ret = -EINVAL; | |
9753 | goto out; | |
9754 | } | |
9755 | ||
9756 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9757 | break; | |
9758 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9759 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9760 | break; | |
9761 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9762 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9763 | break; | |
9764 | default: | |
9765 | ret = -EINVAL; | |
9766 | goto out; | |
9767 | } | |
9768 | } | |
9769 | ||
9770 | out: | |
9771 | /* Reset all restrictions if an error happened */ | |
9772 | if (ret != 0) | |
9773 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9774 | else | |
7e84e1c7 | 9775 | ctx->restrictions.registered = true; |
21b55dbc SG |
9776 | |
9777 | kfree(res); | |
9778 | return ret; | |
9779 | } | |
9780 | ||
7e84e1c7 SG |
9781 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9782 | { | |
9783 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9784 | return -EBADFD; | |
9785 | ||
9786 | if (ctx->restrictions.registered) | |
9787 | ctx->restricted = 1; | |
9788 | ||
0298ef96 PB |
9789 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
9790 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
9791 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
9792 | return 0; |
9793 | } | |
9794 | ||
071698e1 JA |
9795 | static bool io_register_op_must_quiesce(int op) |
9796 | { | |
9797 | switch (op) { | |
f4f7d21c | 9798 | case IORING_REGISTER_FILES: |
071698e1 JA |
9799 | case IORING_UNREGISTER_FILES: |
9800 | case IORING_REGISTER_FILES_UPDATE: | |
9801 | case IORING_REGISTER_PROBE: | |
9802 | case IORING_REGISTER_PERSONALITY: | |
9803 | case IORING_UNREGISTER_PERSONALITY: | |
9804 | return false; | |
9805 | default: | |
9806 | return true; | |
9807 | } | |
9808 | } | |
9809 | ||
edafccee JA |
9810 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
9811 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
9812 | __releases(ctx->uring_lock) |
9813 | __acquires(ctx->uring_lock) | |
edafccee JA |
9814 | { |
9815 | int ret; | |
9816 | ||
35fa71a0 JA |
9817 | /* |
9818 | * We're inside the ring mutex, if the ref is already dying, then | |
9819 | * someone else killed the ctx or is already going through | |
9820 | * io_uring_register(). | |
9821 | */ | |
9822 | if (percpu_ref_is_dying(&ctx->refs)) | |
9823 | return -ENXIO; | |
9824 | ||
071698e1 | 9825 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9826 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 9827 | |
05f3fb3c JA |
9828 | /* |
9829 | * Drop uring mutex before waiting for references to exit. If | |
9830 | * another thread is currently inside io_uring_enter() it might | |
9831 | * need to grab the uring_lock to make progress. If we hold it | |
9832 | * here across the drain wait, then we can deadlock. It's safe | |
9833 | * to drop the mutex here, since no new references will come in | |
9834 | * after we've killed the percpu ref. | |
9835 | */ | |
9836 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
9837 | do { |
9838 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
9839 | if (!ret) | |
9840 | break; | |
ed6930c9 JA |
9841 | ret = io_run_task_work_sig(); |
9842 | if (ret < 0) | |
9843 | break; | |
af9c1a44 JA |
9844 | } while (1); |
9845 | ||
05f3fb3c | 9846 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 9847 | |
c150368b JA |
9848 | if (ret) { |
9849 | percpu_ref_resurrect(&ctx->refs); | |
21b55dbc SG |
9850 | goto out_quiesce; |
9851 | } | |
9852 | } | |
9853 | ||
9854 | if (ctx->restricted) { | |
9855 | if (opcode >= IORING_REGISTER_LAST) { | |
9856 | ret = -EINVAL; | |
9857 | goto out; | |
9858 | } | |
9859 | ||
9860 | if (!test_bit(opcode, ctx->restrictions.register_op)) { | |
9861 | ret = -EACCES; | |
c150368b JA |
9862 | goto out; |
9863 | } | |
05f3fb3c | 9864 | } |
edafccee JA |
9865 | |
9866 | switch (opcode) { | |
9867 | case IORING_REGISTER_BUFFERS: | |
0a96bbe4 | 9868 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
edafccee JA |
9869 | break; |
9870 | case IORING_UNREGISTER_BUFFERS: | |
9871 | ret = -EINVAL; | |
9872 | if (arg || nr_args) | |
9873 | break; | |
0a96bbe4 | 9874 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 9875 | break; |
6b06314c JA |
9876 | case IORING_REGISTER_FILES: |
9877 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
9878 | break; | |
9879 | case IORING_UNREGISTER_FILES: | |
9880 | ret = -EINVAL; | |
9881 | if (arg || nr_args) | |
9882 | break; | |
9883 | ret = io_sqe_files_unregister(ctx); | |
9884 | break; | |
c3a31e60 JA |
9885 | case IORING_REGISTER_FILES_UPDATE: |
9886 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
9887 | break; | |
9b402849 | 9888 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 9889 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
9890 | ret = -EINVAL; |
9891 | if (nr_args != 1) | |
9892 | break; | |
9893 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
9894 | if (ret) |
9895 | break; | |
9896 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
9897 | ctx->eventfd_async = 1; | |
9898 | else | |
9899 | ctx->eventfd_async = 0; | |
9b402849 JA |
9900 | break; |
9901 | case IORING_UNREGISTER_EVENTFD: | |
9902 | ret = -EINVAL; | |
9903 | if (arg || nr_args) | |
9904 | break; | |
9905 | ret = io_eventfd_unregister(ctx); | |
9906 | break; | |
66f4af93 JA |
9907 | case IORING_REGISTER_PROBE: |
9908 | ret = -EINVAL; | |
9909 | if (!arg || nr_args > 256) | |
9910 | break; | |
9911 | ret = io_probe(ctx, arg, nr_args); | |
9912 | break; | |
071698e1 JA |
9913 | case IORING_REGISTER_PERSONALITY: |
9914 | ret = -EINVAL; | |
9915 | if (arg || nr_args) | |
9916 | break; | |
9917 | ret = io_register_personality(ctx); | |
9918 | break; | |
9919 | case IORING_UNREGISTER_PERSONALITY: | |
9920 | ret = -EINVAL; | |
9921 | if (arg) | |
9922 | break; | |
9923 | ret = io_unregister_personality(ctx, nr_args); | |
9924 | break; | |
7e84e1c7 SG |
9925 | case IORING_REGISTER_ENABLE_RINGS: |
9926 | ret = -EINVAL; | |
9927 | if (arg || nr_args) | |
9928 | break; | |
9929 | ret = io_register_enable_rings(ctx); | |
9930 | break; | |
21b55dbc SG |
9931 | case IORING_REGISTER_RESTRICTIONS: |
9932 | ret = io_register_restrictions(ctx, arg, nr_args); | |
9933 | break; | |
edafccee JA |
9934 | default: |
9935 | ret = -EINVAL; | |
9936 | break; | |
9937 | } | |
9938 | ||
21b55dbc | 9939 | out: |
071698e1 | 9940 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 9941 | /* bring the ctx back to life */ |
05f3fb3c | 9942 | percpu_ref_reinit(&ctx->refs); |
21b55dbc | 9943 | out_quiesce: |
0f158b4c | 9944 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 9945 | } |
edafccee JA |
9946 | return ret; |
9947 | } | |
9948 | ||
9949 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
9950 | void __user *, arg, unsigned int, nr_args) | |
9951 | { | |
9952 | struct io_ring_ctx *ctx; | |
9953 | long ret = -EBADF; | |
9954 | struct fd f; | |
9955 | ||
9956 | f = fdget(fd); | |
9957 | if (!f.file) | |
9958 | return -EBADF; | |
9959 | ||
9960 | ret = -EOPNOTSUPP; | |
9961 | if (f.file->f_op != &io_uring_fops) | |
9962 | goto out_fput; | |
9963 | ||
9964 | ctx = f.file->private_data; | |
9965 | ||
b6c23dd5 PB |
9966 | io_run_task_work(); |
9967 | ||
edafccee JA |
9968 | mutex_lock(&ctx->uring_lock); |
9969 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
9970 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
9971 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
9972 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
9973 | out_fput: |
9974 | fdput(f); | |
9975 | return ret; | |
9976 | } | |
9977 | ||
2b188cc1 JA |
9978 | static int __init io_uring_init(void) |
9979 | { | |
d7f62e82 SM |
9980 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
9981 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
9982 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
9983 | } while (0) | |
9984 | ||
9985 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9986 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
9987 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
9988 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
9989 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
9990 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
9991 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
9992 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
9993 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9994 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 9995 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
9996 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
9997 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
9998 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
9999 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10000 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10001 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10002 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10003 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10004 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10005 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10006 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10007 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10008 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10009 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10010 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10011 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10012 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10013 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
10014 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7d67af2c | 10015 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10016 | |
d3656344 | 10017 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10018 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
91f245d5 JA |
10019 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10020 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10021 | return 0; |
10022 | }; | |
10023 | __initcall(io_uring_init); |