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 | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
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> |
ef98eb04 | 81 | #include <linux/tracehook.h> |
2b188cc1 | 82 | |
c826bd7a DD |
83 | #define CREATE_TRACE_POINTS |
84 | #include <trace/events/io_uring.h> | |
85 | ||
2b188cc1 JA |
86 | #include <uapi/linux/io_uring.h> |
87 | ||
88 | #include "internal.h" | |
561fb04a | 89 | #include "io-wq.h" |
2b188cc1 | 90 | |
5277deaa | 91 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 93 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 94 | |
042b0d85 PB |
95 | /* 512 entries per page on 64-bit archs, 64 pages max */ |
96 | #define IORING_MAX_FIXED_FILES (1U << 15) | |
21b55dbc SG |
97 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
98 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 99 | |
2d091d62 PB |
100 | #define IO_RSRC_TAG_TABLE_SHIFT 9 |
101 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) | |
102 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
103 | ||
489809e2 PB |
104 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
105 | ||
b16fed66 PB |
106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ | |
108 | IOSQE_BUFFER_SELECT) | |
c854357b PB |
109 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
110 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS) | |
b16fed66 | 111 | |
09899b19 PB |
112 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
113 | ||
2b188cc1 JA |
114 | struct io_uring { |
115 | u32 head ____cacheline_aligned_in_smp; | |
116 | u32 tail ____cacheline_aligned_in_smp; | |
117 | }; | |
118 | ||
1e84b97b | 119 | /* |
75b28aff HV |
120 | * This data is shared with the application through the mmap at offsets |
121 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
122 | * |
123 | * The offsets to the member fields are published through struct | |
124 | * io_sqring_offsets when calling io_uring_setup. | |
125 | */ | |
75b28aff | 126 | struct io_rings { |
1e84b97b SB |
127 | /* |
128 | * Head and tail offsets into the ring; the offsets need to be | |
129 | * masked to get valid indices. | |
130 | * | |
75b28aff HV |
131 | * The kernel controls head of the sq ring and the tail of the cq ring, |
132 | * and the application controls tail of the sq ring and the head of the | |
133 | * cq ring. | |
1e84b97b | 134 | */ |
75b28aff | 135 | struct io_uring sq, cq; |
1e84b97b | 136 | /* |
75b28aff | 137 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
138 | * ring_entries - 1) |
139 | */ | |
75b28aff HV |
140 | u32 sq_ring_mask, cq_ring_mask; |
141 | /* Ring sizes (constant, power of 2) */ | |
142 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
143 | /* |
144 | * Number of invalid entries dropped by the kernel due to | |
145 | * invalid index stored in array | |
146 | * | |
147 | * Written by the kernel, shouldn't be modified by the | |
148 | * application (i.e. get number of "new events" by comparing to | |
149 | * cached value). | |
150 | * | |
151 | * After a new SQ head value was read by the application this | |
152 | * counter includes all submissions that were dropped reaching | |
153 | * the new SQ head (and possibly more). | |
154 | */ | |
75b28aff | 155 | u32 sq_dropped; |
1e84b97b | 156 | /* |
0d9b5b3a | 157 | * Runtime SQ flags |
1e84b97b SB |
158 | * |
159 | * Written by the kernel, shouldn't be modified by the | |
160 | * application. | |
161 | * | |
162 | * The application needs a full memory barrier before checking | |
163 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
164 | */ | |
75b28aff | 165 | u32 sq_flags; |
0d9b5b3a SG |
166 | /* |
167 | * Runtime CQ flags | |
168 | * | |
169 | * Written by the application, shouldn't be modified by the | |
170 | * kernel. | |
171 | */ | |
fe7e3257 | 172 | u32 cq_flags; |
1e84b97b SB |
173 | /* |
174 | * Number of completion events lost because the queue was full; | |
175 | * this should be avoided by the application by making sure | |
0b4295b5 | 176 | * there are not more requests pending than there is space in |
1e84b97b SB |
177 | * the completion queue. |
178 | * | |
179 | * Written by the kernel, shouldn't be modified by the | |
180 | * application (i.e. get number of "new events" by comparing to | |
181 | * cached value). | |
182 | * | |
183 | * As completion events come in out of order this counter is not | |
184 | * ordered with any other data. | |
185 | */ | |
75b28aff | 186 | u32 cq_overflow; |
1e84b97b SB |
187 | /* |
188 | * Ring buffer of completion events. | |
189 | * | |
190 | * The kernel writes completion events fresh every time they are | |
191 | * produced, so the application is allowed to modify pending | |
192 | * entries. | |
193 | */ | |
75b28aff | 194 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
195 | }; |
196 | ||
45d189c6 PB |
197 | enum io_uring_cmd_flags { |
198 | IO_URING_F_NONBLOCK = 1, | |
889fca73 | 199 | IO_URING_F_COMPLETE_DEFER = 2, |
45d189c6 PB |
200 | }; |
201 | ||
edafccee JA |
202 | struct io_mapped_ubuf { |
203 | u64 ubuf; | |
4751f53d | 204 | u64 ubuf_end; |
edafccee | 205 | unsigned int nr_bvecs; |
de293938 | 206 | unsigned long acct_pages; |
41edf1a5 | 207 | struct bio_vec bvec[]; |
edafccee JA |
208 | }; |
209 | ||
50238531 BM |
210 | struct io_ring_ctx; |
211 | ||
6c2450ae PB |
212 | struct io_overflow_cqe { |
213 | struct io_uring_cqe cqe; | |
214 | struct list_head list; | |
215 | }; | |
216 | ||
a04b0ac0 PB |
217 | struct io_fixed_file { |
218 | /* file * with additional FFS_* flags */ | |
219 | unsigned long file_ptr; | |
220 | }; | |
221 | ||
269bbe5f BM |
222 | struct io_rsrc_put { |
223 | struct list_head list; | |
b60c8dce | 224 | u64 tag; |
50238531 BM |
225 | union { |
226 | void *rsrc; | |
227 | struct file *file; | |
bd54b6fe | 228 | struct io_mapped_ubuf *buf; |
50238531 | 229 | }; |
269bbe5f BM |
230 | }; |
231 | ||
aeca241b | 232 | struct io_file_table { |
042b0d85 | 233 | struct io_fixed_file *files; |
31b51510 JA |
234 | }; |
235 | ||
b895c9a6 | 236 | struct io_rsrc_node { |
05589553 XW |
237 | struct percpu_ref refs; |
238 | struct list_head node; | |
269bbe5f | 239 | struct list_head rsrc_list; |
b895c9a6 | 240 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 241 | struct llist_node llist; |
e297822b | 242 | bool done; |
05589553 XW |
243 | }; |
244 | ||
40ae0ff7 PB |
245 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
246 | ||
b895c9a6 | 247 | struct io_rsrc_data { |
05f3fb3c JA |
248 | struct io_ring_ctx *ctx; |
249 | ||
2d091d62 PB |
250 | u64 **tags; |
251 | unsigned int nr; | |
40ae0ff7 | 252 | rsrc_put_fn *do_put; |
3e942498 | 253 | atomic_t refs; |
05f3fb3c | 254 | struct completion done; |
8bad28d8 | 255 | bool quiesce; |
05f3fb3c JA |
256 | }; |
257 | ||
5a2e745d JA |
258 | struct io_buffer { |
259 | struct list_head list; | |
260 | __u64 addr; | |
d1f82808 | 261 | __u32 len; |
5a2e745d JA |
262 | __u16 bid; |
263 | }; | |
264 | ||
21b55dbc SG |
265 | struct io_restriction { |
266 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
267 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
268 | u8 sqe_flags_allowed; | |
269 | u8 sqe_flags_required; | |
7e84e1c7 | 270 | bool registered; |
21b55dbc SG |
271 | }; |
272 | ||
37d1e2e3 JA |
273 | enum { |
274 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
275 | IO_SQ_THREAD_SHOULD_PARK, | |
276 | }; | |
277 | ||
534ca6d6 JA |
278 | struct io_sq_data { |
279 | refcount_t refs; | |
9e138a48 | 280 | atomic_t park_pending; |
09a6f4ef | 281 | struct mutex lock; |
69fb2131 JA |
282 | |
283 | /* ctx's that are using this sqd */ | |
284 | struct list_head ctx_list; | |
69fb2131 | 285 | |
534ca6d6 JA |
286 | struct task_struct *thread; |
287 | struct wait_queue_head wait; | |
08369246 XW |
288 | |
289 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
290 | int sq_cpu; |
291 | pid_t task_pid; | |
5c2469e0 | 292 | pid_t task_tgid; |
37d1e2e3 JA |
293 | |
294 | unsigned long state; | |
37d1e2e3 | 295 | struct completion exited; |
534ca6d6 JA |
296 | }; |
297 | ||
258b29a9 | 298 | #define IO_IOPOLL_BATCH 8 |
6dd0be1e | 299 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 300 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 301 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 PB |
302 | |
303 | struct io_comp_state { | |
6dd0be1e | 304 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
1b4c351f | 305 | unsigned int nr; |
c7dae4ba | 306 | /* inline/task_work completion list, under ->uring_lock */ |
1b4c351f | 307 | struct list_head free_list; |
258b29a9 PB |
308 | }; |
309 | ||
a1ab7b35 PB |
310 | struct io_submit_link { |
311 | struct io_kiocb *head; | |
312 | struct io_kiocb *last; | |
313 | }; | |
314 | ||
258b29a9 PB |
315 | struct io_submit_state { |
316 | struct blk_plug plug; | |
a1ab7b35 | 317 | struct io_submit_link link; |
258b29a9 PB |
318 | |
319 | /* | |
320 | * io_kiocb alloc cache | |
321 | */ | |
bf019da7 | 322 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
323 | unsigned int free_reqs; |
324 | ||
325 | bool plug_started; | |
326 | ||
327 | /* | |
328 | * Batch completion logic | |
329 | */ | |
330 | struct io_comp_state comp; | |
331 | ||
332 | /* | |
333 | * File reference cache | |
334 | */ | |
335 | struct file *file; | |
336 | unsigned int fd; | |
337 | unsigned int file_refs; | |
338 | unsigned int ios_left; | |
339 | }; | |
340 | ||
2b188cc1 | 341 | struct io_ring_ctx { |
b52ecf8c | 342 | /* const or read-mostly hot data */ |
2b188cc1 JA |
343 | struct { |
344 | struct percpu_ref refs; | |
2b188cc1 | 345 | |
b52ecf8c | 346 | struct io_rings *rings; |
2b188cc1 | 347 | unsigned int flags; |
e1d85334 | 348 | unsigned int compat: 1; |
e1d85334 RD |
349 | unsigned int drain_next: 1; |
350 | unsigned int eventfd_async: 1; | |
21b55dbc | 351 | unsigned int restricted: 1; |
f18ee4cf | 352 | unsigned int off_timeout_used: 1; |
10c66904 | 353 | unsigned int drain_active: 1; |
b52ecf8c | 354 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 355 | |
7f1129d2 | 356 | /* submission data */ |
b52ecf8c | 357 | struct { |
0499e582 PB |
358 | struct mutex uring_lock; |
359 | ||
75b28aff HV |
360 | /* |
361 | * Ring buffer of indices into array of io_uring_sqe, which is | |
362 | * mmapped by the application using the IORING_OFF_SQES offset. | |
363 | * | |
364 | * This indirection could e.g. be used to assign fixed | |
365 | * io_uring_sqe entries to operations and only submit them to | |
366 | * the queue when needed. | |
367 | * | |
368 | * The kernel modifies neither the indices array nor the entries | |
369 | * array. | |
370 | */ | |
371 | u32 *sq_array; | |
c7af47cf | 372 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
373 | unsigned cached_sq_head; |
374 | unsigned sq_entries; | |
de0617e4 | 375 | struct list_head defer_list; |
7f1129d2 PB |
376 | |
377 | /* | |
378 | * Fixed resources fast path, should be accessed only under | |
379 | * uring_lock, and updated through io_uring_register(2) | |
380 | */ | |
381 | struct io_rsrc_node *rsrc_node; | |
382 | struct io_file_table file_table; | |
383 | unsigned nr_user_files; | |
384 | unsigned nr_user_bufs; | |
385 | struct io_mapped_ubuf **user_bufs; | |
386 | ||
387 | struct io_submit_state submit_state; | |
5262f567 | 388 | struct list_head timeout_list; |
1d7bb1d5 | 389 | struct list_head cq_overflow_list; |
7f1129d2 PB |
390 | struct xarray io_buffers; |
391 | struct xarray personalities; | |
392 | u32 pers_next; | |
393 | unsigned sq_thread_idle; | |
2b188cc1 JA |
394 | } ____cacheline_aligned_in_smp; |
395 | ||
d0acdee2 PB |
396 | /* IRQ completion list, under ->completion_lock */ |
397 | struct list_head locked_free_list; | |
398 | unsigned int locked_free_nr; | |
3c1a2ead | 399 | |
7c30f36a | 400 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
401 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
402 | ||
90554200 | 403 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 404 | struct list_head sqd_list; |
75b28aff | 405 | |
5ed7a37d PB |
406 | unsigned long check_cq_overflow; |
407 | ||
206aefde JA |
408 | struct { |
409 | unsigned cached_cq_tail; | |
410 | unsigned cq_entries; | |
0499e582 | 411 | struct eventfd_ctx *cq_ev_fd; |
311997b3 | 412 | struct wait_queue_head poll_wait; |
0499e582 PB |
413 | struct wait_queue_head cq_wait; |
414 | unsigned cq_extra; | |
415 | atomic_t cq_timeouts; | |
206aefde | 416 | struct fasync_struct *cq_fasync; |
0499e582 | 417 | unsigned cq_last_tm_flush; |
206aefde | 418 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 419 | |
2b188cc1 JA |
420 | struct { |
421 | spinlock_t completion_lock; | |
e94f141b | 422 | |
def596e9 | 423 | /* |
540e32a0 | 424 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
425 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
426 | * For SQPOLL, only the single threaded io_sq_thread() will | |
427 | * manipulate the list, hence no extra locking is needed there. | |
428 | */ | |
540e32a0 | 429 | struct list_head iopoll_list; |
78076bb6 JA |
430 | struct hlist_head *cancel_hash; |
431 | unsigned cancel_hash_bits; | |
915b3dde | 432 | bool poll_multi_queue; |
2b188cc1 | 433 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 434 | |
21b55dbc | 435 | struct io_restriction restrictions; |
3c1a2ead | 436 | |
b13a8918 PB |
437 | /* slow path rsrc auxilary data, used by update/register */ |
438 | struct { | |
439 | struct io_rsrc_node *rsrc_backup_node; | |
440 | struct io_mapped_ubuf *dummy_ubuf; | |
441 | struct io_rsrc_data *file_data; | |
442 | struct io_rsrc_data *buf_data; | |
443 | ||
444 | struct delayed_work rsrc_put_work; | |
445 | struct llist_head rsrc_put_llist; | |
446 | struct list_head rsrc_ref_list; | |
447 | spinlock_t rsrc_ref_lock; | |
448 | }; | |
449 | ||
3c1a2ead | 450 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
451 | struct { |
452 | #if defined(CONFIG_UNIX) | |
453 | struct socket *ring_sock; | |
454 | #endif | |
455 | /* hashed buffered write serialization */ | |
456 | struct io_wq_hash *hash_map; | |
457 | ||
458 | /* Only used for accounting purposes */ | |
459 | struct user_struct *user; | |
460 | struct mm_struct *mm_account; | |
461 | ||
462 | /* ctx exit and cancelation */ | |
9011bf9a PB |
463 | struct llist_head fallback_llist; |
464 | struct delayed_work fallback_work; | |
b986af7e PB |
465 | struct work_struct exit_work; |
466 | struct list_head tctx_list; | |
467 | struct completion ref_comp; | |
468 | }; | |
2b188cc1 JA |
469 | }; |
470 | ||
53e043b2 SM |
471 | struct io_uring_task { |
472 | /* submission side */ | |
09899b19 | 473 | int cached_refs; |
53e043b2 SM |
474 | struct xarray xa; |
475 | struct wait_queue_head wait; | |
ee53fb2b SM |
476 | const struct io_ring_ctx *last; |
477 | struct io_wq *io_wq; | |
53e043b2 | 478 | struct percpu_counter inflight; |
b303fe2e | 479 | atomic_t inflight_tracked; |
53e043b2 | 480 | atomic_t in_idle; |
53e043b2 SM |
481 | |
482 | spinlock_t task_lock; | |
483 | struct io_wq_work_list task_list; | |
484 | unsigned long task_state; | |
485 | struct callback_head task_work; | |
486 | }; | |
487 | ||
09bb8394 JA |
488 | /* |
489 | * First field must be the file pointer in all the | |
490 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
491 | */ | |
221c5eb2 JA |
492 | struct io_poll_iocb { |
493 | struct file *file; | |
018043be | 494 | struct wait_queue_head *head; |
221c5eb2 | 495 | __poll_t events; |
8c838788 | 496 | bool done; |
221c5eb2 | 497 | bool canceled; |
392edb45 | 498 | struct wait_queue_entry wait; |
221c5eb2 JA |
499 | }; |
500 | ||
9d805892 | 501 | struct io_poll_update { |
018043be | 502 | struct file *file; |
9d805892 PB |
503 | u64 old_user_data; |
504 | u64 new_user_data; | |
505 | __poll_t events; | |
b69de288 JA |
506 | bool update_events; |
507 | bool update_user_data; | |
018043be PB |
508 | }; |
509 | ||
b5dba59e JA |
510 | struct io_close { |
511 | struct file *file; | |
b5dba59e JA |
512 | int fd; |
513 | }; | |
514 | ||
ad8a48ac JA |
515 | struct io_timeout_data { |
516 | struct io_kiocb *req; | |
517 | struct hrtimer timer; | |
518 | struct timespec64 ts; | |
519 | enum hrtimer_mode mode; | |
520 | }; | |
521 | ||
8ed8d3c3 JA |
522 | struct io_accept { |
523 | struct file *file; | |
524 | struct sockaddr __user *addr; | |
525 | int __user *addr_len; | |
526 | int flags; | |
09952e3e | 527 | unsigned long nofile; |
8ed8d3c3 JA |
528 | }; |
529 | ||
530 | struct io_sync { | |
531 | struct file *file; | |
532 | loff_t len; | |
533 | loff_t off; | |
534 | int flags; | |
d63d1b5e | 535 | int mode; |
8ed8d3c3 JA |
536 | }; |
537 | ||
fbf23849 JA |
538 | struct io_cancel { |
539 | struct file *file; | |
540 | u64 addr; | |
541 | }; | |
542 | ||
b29472ee JA |
543 | struct io_timeout { |
544 | struct file *file; | |
bfe68a22 PB |
545 | u32 off; |
546 | u32 target_seq; | |
135fcde8 | 547 | struct list_head list; |
90cd7e42 PB |
548 | /* head of the link, used by linked timeouts only */ |
549 | struct io_kiocb *head; | |
b29472ee JA |
550 | }; |
551 | ||
0bdf7a2d PB |
552 | struct io_timeout_rem { |
553 | struct file *file; | |
554 | u64 addr; | |
9c8e11b3 PB |
555 | |
556 | /* timeout update */ | |
557 | struct timespec64 ts; | |
558 | u32 flags; | |
0bdf7a2d PB |
559 | }; |
560 | ||
9adbd45d JA |
561 | struct io_rw { |
562 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
563 | struct kiocb kiocb; | |
564 | u64 addr; | |
565 | u64 len; | |
566 | }; | |
567 | ||
3fbb51c1 JA |
568 | struct io_connect { |
569 | struct file *file; | |
570 | struct sockaddr __user *addr; | |
571 | int addr_len; | |
572 | }; | |
573 | ||
e47293fd JA |
574 | struct io_sr_msg { |
575 | struct file *file; | |
fddaface | 576 | union { |
4af3417a PB |
577 | struct compat_msghdr __user *umsg_compat; |
578 | struct user_msghdr __user *umsg; | |
579 | void __user *buf; | |
fddaface | 580 | }; |
e47293fd | 581 | int msg_flags; |
bcda7baa | 582 | int bgid; |
fddaface | 583 | size_t len; |
bcda7baa | 584 | struct io_buffer *kbuf; |
e47293fd JA |
585 | }; |
586 | ||
15b71abe JA |
587 | struct io_open { |
588 | struct file *file; | |
589 | int dfd; | |
15b71abe | 590 | struct filename *filename; |
c12cedf2 | 591 | struct open_how how; |
4022e7af | 592 | unsigned long nofile; |
15b71abe JA |
593 | }; |
594 | ||
269bbe5f | 595 | struct io_rsrc_update { |
05f3fb3c JA |
596 | struct file *file; |
597 | u64 arg; | |
598 | u32 nr_args; | |
599 | u32 offset; | |
600 | }; | |
601 | ||
4840e418 JA |
602 | struct io_fadvise { |
603 | struct file *file; | |
604 | u64 offset; | |
605 | u32 len; | |
606 | u32 advice; | |
607 | }; | |
608 | ||
c1ca757b JA |
609 | struct io_madvise { |
610 | struct file *file; | |
611 | u64 addr; | |
612 | u32 len; | |
613 | u32 advice; | |
614 | }; | |
615 | ||
3e4827b0 JA |
616 | struct io_epoll { |
617 | struct file *file; | |
618 | int epfd; | |
619 | int op; | |
620 | int fd; | |
621 | struct epoll_event event; | |
e47293fd JA |
622 | }; |
623 | ||
7d67af2c PB |
624 | struct io_splice { |
625 | struct file *file_out; | |
626 | struct file *file_in; | |
627 | loff_t off_out; | |
628 | loff_t off_in; | |
629 | u64 len; | |
630 | unsigned int flags; | |
631 | }; | |
632 | ||
ddf0322d JA |
633 | struct io_provide_buf { |
634 | struct file *file; | |
635 | __u64 addr; | |
38134ada | 636 | __u32 len; |
ddf0322d JA |
637 | __u32 bgid; |
638 | __u16 nbufs; | |
639 | __u16 bid; | |
640 | }; | |
641 | ||
1d9e1288 BM |
642 | struct io_statx { |
643 | struct file *file; | |
644 | int dfd; | |
645 | unsigned int mask; | |
646 | unsigned int flags; | |
e62753e4 | 647 | const char __user *filename; |
1d9e1288 BM |
648 | struct statx __user *buffer; |
649 | }; | |
650 | ||
36f4fa68 JA |
651 | struct io_shutdown { |
652 | struct file *file; | |
653 | int how; | |
654 | }; | |
655 | ||
80a261fd JA |
656 | struct io_rename { |
657 | struct file *file; | |
658 | int old_dfd; | |
659 | int new_dfd; | |
660 | struct filename *oldpath; | |
661 | struct filename *newpath; | |
662 | int flags; | |
663 | }; | |
664 | ||
14a1143b JA |
665 | struct io_unlink { |
666 | struct file *file; | |
667 | int dfd; | |
668 | int flags; | |
669 | struct filename *filename; | |
670 | }; | |
671 | ||
3ca405eb PB |
672 | struct io_completion { |
673 | struct file *file; | |
674 | struct list_head list; | |
8c3f9cd1 | 675 | u32 cflags; |
3ca405eb PB |
676 | }; |
677 | ||
f499a021 JA |
678 | struct io_async_connect { |
679 | struct sockaddr_storage address; | |
680 | }; | |
681 | ||
03b1230c JA |
682 | struct io_async_msghdr { |
683 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
684 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
685 | struct iovec *free_iov; | |
03b1230c JA |
686 | struct sockaddr __user *uaddr; |
687 | struct msghdr msg; | |
b537916c | 688 | struct sockaddr_storage addr; |
03b1230c JA |
689 | }; |
690 | ||
f67676d1 JA |
691 | struct io_async_rw { |
692 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
693 | const struct iovec *free_iovec; |
694 | struct iov_iter iter; | |
227c0c96 | 695 | size_t bytes_done; |
bcf5a063 | 696 | struct wait_page_queue wpq; |
f67676d1 JA |
697 | }; |
698 | ||
6b47ee6e PB |
699 | enum { |
700 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
701 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
702 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
703 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
704 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 705 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 706 | |
dddca226 | 707 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 708 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
709 | REQ_F_INFLIGHT_BIT, |
710 | REQ_F_CUR_POS_BIT, | |
711 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 712 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 713 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 714 | REQ_F_POLLED_BIT, |
bcda7baa | 715 | REQ_F_BUFFER_SELECTED_BIT, |
900fad45 | 716 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 717 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 718 | REQ_F_REISSUE_BIT, |
8c130827 | 719 | REQ_F_DONT_REISSUE_BIT, |
b8e64b53 | 720 | REQ_F_CREDS_BIT, |
7b29f92d | 721 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
722 | REQ_F_NOWAIT_READ_BIT, |
723 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 724 | REQ_F_ISREG_BIT, |
84557871 JA |
725 | |
726 | /* not a real bit, just to check we're not overflowing the space */ | |
727 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
728 | }; |
729 | ||
730 | enum { | |
731 | /* ctx owns file */ | |
732 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
733 | /* drain existing IO first */ | |
734 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
735 | /* linked sqes */ | |
736 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
737 | /* doesn't sever on completion < 0 */ | |
738 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
739 | /* IOSQE_ASYNC */ | |
740 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
741 | /* IOSQE_BUFFER_SELECT */ |
742 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 743 | |
6b47ee6e | 744 | /* fail rest of links */ |
93d2bcd2 | 745 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 746 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
747 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
748 | /* read/write uses file position */ | |
749 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
750 | /* must not punt to workers */ | |
751 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 752 | /* has or had linked timeout */ |
6b47ee6e | 753 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
754 | /* needs cleanup */ |
755 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
756 | /* already went through poll handler */ |
757 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
758 | /* buffer already selected */ |
759 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
900fad45 PB |
760 | /* linked timeout is active, i.e. prepared by link's head */ |
761 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
762 | /* completion is deferred through io_comp_state */ |
763 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
764 | /* caller should reissue async */ |
765 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
766 | /* don't attempt request reissue, see io_rw_reissue() */ |
767 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
7b29f92d | 768 | /* supports async reads */ |
b191e2df | 769 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 770 | /* supports async writes */ |
b191e2df | 771 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
772 | /* regular file */ |
773 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
774 | /* has creds assigned */ |
775 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
d7718a9d JA |
776 | }; |
777 | ||
778 | struct async_poll { | |
779 | struct io_poll_iocb poll; | |
807abcb0 | 780 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
781 | }; |
782 | ||
5b0a6acc PB |
783 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req); |
784 | ||
7cbf1722 | 785 | struct io_task_work { |
5b0a6acc PB |
786 | union { |
787 | struct io_wq_work_node node; | |
788 | struct llist_node fallback_node; | |
789 | }; | |
790 | io_req_tw_func_t func; | |
7cbf1722 JA |
791 | }; |
792 | ||
992da01a PB |
793 | enum { |
794 | IORING_RSRC_FILE = 0, | |
795 | IORING_RSRC_BUFFER = 1, | |
796 | }; | |
797 | ||
09bb8394 JA |
798 | /* |
799 | * NOTE! Each of the iocb union members has the file pointer | |
800 | * as the first entry in their struct definition. So you can | |
801 | * access the file pointer through any of the sub-structs, | |
802 | * or directly as just 'ki_filp' in this struct. | |
803 | */ | |
2b188cc1 | 804 | struct io_kiocb { |
221c5eb2 | 805 | union { |
09bb8394 | 806 | struct file *file; |
9adbd45d | 807 | struct io_rw rw; |
221c5eb2 | 808 | struct io_poll_iocb poll; |
9d805892 | 809 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
810 | struct io_accept accept; |
811 | struct io_sync sync; | |
fbf23849 | 812 | struct io_cancel cancel; |
b29472ee | 813 | struct io_timeout timeout; |
0bdf7a2d | 814 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 815 | struct io_connect connect; |
e47293fd | 816 | struct io_sr_msg sr_msg; |
15b71abe | 817 | struct io_open open; |
b5dba59e | 818 | struct io_close close; |
269bbe5f | 819 | struct io_rsrc_update rsrc_update; |
4840e418 | 820 | struct io_fadvise fadvise; |
c1ca757b | 821 | struct io_madvise madvise; |
3e4827b0 | 822 | struct io_epoll epoll; |
7d67af2c | 823 | struct io_splice splice; |
ddf0322d | 824 | struct io_provide_buf pbuf; |
1d9e1288 | 825 | struct io_statx statx; |
36f4fa68 | 826 | struct io_shutdown shutdown; |
80a261fd | 827 | struct io_rename rename; |
14a1143b | 828 | struct io_unlink unlink; |
3ca405eb PB |
829 | /* use only after cleaning per-op data, see io_clean_op() */ |
830 | struct io_completion compl; | |
221c5eb2 | 831 | }; |
2b188cc1 | 832 | |
e8c2bc1f JA |
833 | /* opcode allocated if it needs to store data for async defer */ |
834 | void *async_data; | |
d625c6ee | 835 | u8 opcode; |
65a6543d XW |
836 | /* polled IO has completed */ |
837 | u8 iopoll_completed; | |
2b188cc1 | 838 | |
4f4eeba8 | 839 | u16 buf_index; |
9cf7c104 | 840 | u32 result; |
4f4eeba8 | 841 | |
010e8e6b PB |
842 | struct io_ring_ctx *ctx; |
843 | unsigned int flags; | |
abc54d63 | 844 | atomic_t refs; |
010e8e6b PB |
845 | struct task_struct *task; |
846 | u64 user_data; | |
d7718a9d | 847 | |
f2f87370 | 848 | struct io_kiocb *link; |
269bbe5f | 849 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 850 | |
b303fe2e | 851 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 852 | struct list_head inflight_entry; |
5b0a6acc | 853 | struct io_task_work io_task_work; |
010e8e6b PB |
854 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
855 | struct hlist_node hash_node; | |
856 | struct async_poll *apoll; | |
857 | struct io_wq_work work; | |
fe7e3257 | 858 | const struct cred *creds; |
c10d1f98 | 859 | |
eae071c9 PB |
860 | /* store used ubuf, so we can prevent reloading */ |
861 | struct io_mapped_ubuf *imu; | |
2b188cc1 | 862 | }; |
05589553 | 863 | |
13bf43f5 PB |
864 | struct io_tctx_node { |
865 | struct list_head ctx_node; | |
866 | struct task_struct *task; | |
13bf43f5 PB |
867 | struct io_ring_ctx *ctx; |
868 | }; | |
869 | ||
27dc8338 PB |
870 | struct io_defer_entry { |
871 | struct list_head list; | |
872 | struct io_kiocb *req; | |
9cf7c104 | 873 | u32 seq; |
2b188cc1 JA |
874 | }; |
875 | ||
d3656344 | 876 | struct io_op_def { |
d3656344 JA |
877 | /* needs req->file assigned */ |
878 | unsigned needs_file : 1; | |
d3656344 JA |
879 | /* hash wq insertion if file is a regular file */ |
880 | unsigned hash_reg_file : 1; | |
881 | /* unbound wq insertion if file is a non-regular file */ | |
882 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
883 | /* opcode is not supported by this kernel */ |
884 | unsigned not_supported : 1; | |
8a72758c JA |
885 | /* set if opcode supports polled "wait" */ |
886 | unsigned pollin : 1; | |
887 | unsigned pollout : 1; | |
bcda7baa JA |
888 | /* op supports buffer selection */ |
889 | unsigned buffer_select : 1; | |
26f0505a PB |
890 | /* do prep async if is going to be punted */ |
891 | unsigned needs_async_setup : 1; | |
27926b68 JA |
892 | /* should block plug */ |
893 | unsigned plug : 1; | |
e8c2bc1f JA |
894 | /* size of async data needed, if any */ |
895 | unsigned short async_size; | |
d3656344 JA |
896 | }; |
897 | ||
0918682b | 898 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
899 | [IORING_OP_NOP] = {}, |
900 | [IORING_OP_READV] = { | |
d3656344 JA |
901 | .needs_file = 1, |
902 | .unbound_nonreg_file = 1, | |
8a72758c | 903 | .pollin = 1, |
4d954c25 | 904 | .buffer_select = 1, |
26f0505a | 905 | .needs_async_setup = 1, |
27926b68 | 906 | .plug = 1, |
e8c2bc1f | 907 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 908 | }, |
0463b6c5 | 909 | [IORING_OP_WRITEV] = { |
d3656344 JA |
910 | .needs_file = 1, |
911 | .hash_reg_file = 1, | |
912 | .unbound_nonreg_file = 1, | |
8a72758c | 913 | .pollout = 1, |
26f0505a | 914 | .needs_async_setup = 1, |
27926b68 | 915 | .plug = 1, |
e8c2bc1f | 916 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 917 | }, |
0463b6c5 | 918 | [IORING_OP_FSYNC] = { |
d3656344 JA |
919 | .needs_file = 1, |
920 | }, | |
0463b6c5 | 921 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
922 | .needs_file = 1, |
923 | .unbound_nonreg_file = 1, | |
8a72758c | 924 | .pollin = 1, |
27926b68 | 925 | .plug = 1, |
e8c2bc1f | 926 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 927 | }, |
0463b6c5 | 928 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
929 | .needs_file = 1, |
930 | .hash_reg_file = 1, | |
931 | .unbound_nonreg_file = 1, | |
8a72758c | 932 | .pollout = 1, |
27926b68 | 933 | .plug = 1, |
e8c2bc1f | 934 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 935 | }, |
0463b6c5 | 936 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
937 | .needs_file = 1, |
938 | .unbound_nonreg_file = 1, | |
939 | }, | |
0463b6c5 PB |
940 | [IORING_OP_POLL_REMOVE] = {}, |
941 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
942 | .needs_file = 1, |
943 | }, | |
0463b6c5 | 944 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
945 | .needs_file = 1, |
946 | .unbound_nonreg_file = 1, | |
8a72758c | 947 | .pollout = 1, |
26f0505a | 948 | .needs_async_setup = 1, |
e8c2bc1f | 949 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 950 | }, |
0463b6c5 | 951 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
952 | .needs_file = 1, |
953 | .unbound_nonreg_file = 1, | |
8a72758c | 954 | .pollin = 1, |
52de1fe1 | 955 | .buffer_select = 1, |
26f0505a | 956 | .needs_async_setup = 1, |
e8c2bc1f | 957 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 958 | }, |
0463b6c5 | 959 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 960 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 961 | }, |
9c8e11b3 PB |
962 | [IORING_OP_TIMEOUT_REMOVE] = { |
963 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 964 | }, |
0463b6c5 | 965 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
966 | .needs_file = 1, |
967 | .unbound_nonreg_file = 1, | |
8a72758c | 968 | .pollin = 1, |
d3656344 | 969 | }, |
0463b6c5 PB |
970 | [IORING_OP_ASYNC_CANCEL] = {}, |
971 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 972 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 973 | }, |
0463b6c5 | 974 | [IORING_OP_CONNECT] = { |
d3656344 JA |
975 | .needs_file = 1, |
976 | .unbound_nonreg_file = 1, | |
8a72758c | 977 | .pollout = 1, |
26f0505a | 978 | .needs_async_setup = 1, |
e8c2bc1f | 979 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 980 | }, |
0463b6c5 | 981 | [IORING_OP_FALLOCATE] = { |
d3656344 | 982 | .needs_file = 1, |
d3656344 | 983 | }, |
44526bed JA |
984 | [IORING_OP_OPENAT] = {}, |
985 | [IORING_OP_CLOSE] = {}, | |
986 | [IORING_OP_FILES_UPDATE] = {}, | |
987 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 988 | [IORING_OP_READ] = { |
3a6820f2 JA |
989 | .needs_file = 1, |
990 | .unbound_nonreg_file = 1, | |
8a72758c | 991 | .pollin = 1, |
bcda7baa | 992 | .buffer_select = 1, |
27926b68 | 993 | .plug = 1, |
e8c2bc1f | 994 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 995 | }, |
0463b6c5 | 996 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
997 | .needs_file = 1, |
998 | .unbound_nonreg_file = 1, | |
8a72758c | 999 | .pollout = 1, |
27926b68 | 1000 | .plug = 1, |
e8c2bc1f | 1001 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1002 | }, |
0463b6c5 | 1003 | [IORING_OP_FADVISE] = { |
4840e418 | 1004 | .needs_file = 1, |
c1ca757b | 1005 | }, |
44526bed | 1006 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1007 | [IORING_OP_SEND] = { |
fddaface JA |
1008 | .needs_file = 1, |
1009 | .unbound_nonreg_file = 1, | |
8a72758c | 1010 | .pollout = 1, |
fddaface | 1011 | }, |
0463b6c5 | 1012 | [IORING_OP_RECV] = { |
fddaface JA |
1013 | .needs_file = 1, |
1014 | .unbound_nonreg_file = 1, | |
8a72758c | 1015 | .pollin = 1, |
bcda7baa | 1016 | .buffer_select = 1, |
fddaface | 1017 | }, |
0463b6c5 | 1018 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1019 | }, |
3e4827b0 JA |
1020 | [IORING_OP_EPOLL_CTL] = { |
1021 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1022 | }, |
7d67af2c PB |
1023 | [IORING_OP_SPLICE] = { |
1024 | .needs_file = 1, | |
1025 | .hash_reg_file = 1, | |
1026 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1027 | }, |
1028 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1029 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1030 | [IORING_OP_TEE] = { |
1031 | .needs_file = 1, | |
1032 | .hash_reg_file = 1, | |
1033 | .unbound_nonreg_file = 1, | |
1034 | }, | |
36f4fa68 JA |
1035 | [IORING_OP_SHUTDOWN] = { |
1036 | .needs_file = 1, | |
1037 | }, | |
44526bed JA |
1038 | [IORING_OP_RENAMEAT] = {}, |
1039 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
1040 | }; |
1041 | ||
7a612350 | 1042 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1043 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1044 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1045 | struct task_struct *task, | |
3dd0c97a | 1046 | bool cancel_all); |
78cc687b | 1047 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
b895c9a6 | 1048 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx); |
1ffc5422 | 1049 | |
d4d19c19 PB |
1050 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1051 | long res, unsigned int cflags); | |
ec9c02ad | 1052 | static void io_put_req(struct io_kiocb *req); |
216578e5 | 1053 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
c7dae4ba JA |
1054 | static void io_dismantle_req(struct io_kiocb *req); |
1055 | static void io_put_task(struct task_struct *task, int nr); | |
94ae5e77 JA |
1056 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
1057 | static void io_queue_linked_timeout(struct io_kiocb *req); | |
fdecb662 | 1058 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1059 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1060 | unsigned nr_args); |
68fb8979 | 1061 | static void io_clean_op(struct io_kiocb *req); |
ac177053 PB |
1062 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
1063 | struct io_submit_state *state, | |
8371adf5 | 1064 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1065 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1066 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1067 | |
907d1df3 | 1068 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1069 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
5082620f | 1070 | static bool io_poll_remove_waitqs(struct io_kiocb *req); |
179ae0d1 | 1071 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1072 | |
9011bf9a PB |
1073 | static void io_fallback_req_func(struct work_struct *unused); |
1074 | ||
2b188cc1 JA |
1075 | static struct kmem_cache *req_cachep; |
1076 | ||
0918682b | 1077 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1078 | |
1079 | struct sock *io_uring_get_socket(struct file *file) | |
1080 | { | |
1081 | #if defined(CONFIG_UNIX) | |
1082 | if (file->f_op == &io_uring_fops) { | |
1083 | struct io_ring_ctx *ctx = file->private_data; | |
1084 | ||
1085 | return ctx->ring_sock->sk; | |
1086 | } | |
1087 | #endif | |
1088 | return NULL; | |
1089 | } | |
1090 | EXPORT_SYMBOL(io_uring_get_socket); | |
1091 | ||
f2f87370 PB |
1092 | #define io_for_each_link(pos, head) \ |
1093 | for (pos = (head); pos; pos = pos->link) | |
1094 | ||
b895c9a6 | 1095 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1096 | { |
1097 | struct io_ring_ctx *ctx = req->ctx; | |
1098 | ||
269bbe5f | 1099 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1100 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1101 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1102 | } |
1103 | } | |
1104 | ||
f70865db PB |
1105 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1106 | { | |
1107 | bool got = percpu_ref_tryget(ref); | |
1108 | ||
1109 | /* already at zero, wait for ->release() */ | |
1110 | if (!got) | |
1111 | wait_for_completion(compl); | |
1112 | percpu_ref_resurrect(ref); | |
1113 | if (got) | |
1114 | percpu_ref_put(ref); | |
1115 | } | |
1116 | ||
3dd0c97a PB |
1117 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1118 | bool cancel_all) | |
08d23634 PB |
1119 | { |
1120 | struct io_kiocb *req; | |
1121 | ||
68207680 | 1122 | if (task && head->task != task) |
08d23634 | 1123 | return false; |
3dd0c97a | 1124 | if (cancel_all) |
08d23634 PB |
1125 | return true; |
1126 | ||
1127 | io_for_each_link(req, head) { | |
b05a1bcd | 1128 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1129 | return true; |
08d23634 PB |
1130 | } |
1131 | return false; | |
1132 | } | |
1133 | ||
93d2bcd2 | 1134 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1135 | { |
93d2bcd2 | 1136 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1137 | } |
4a38aed2 | 1138 | |
2b188cc1 JA |
1139 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1140 | { | |
1141 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1142 | ||
0f158b4c | 1143 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1144 | } |
1145 | ||
8eb7e2d0 PB |
1146 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1147 | { | |
1148 | return !req->timeout.off; | |
1149 | } | |
1150 | ||
2b188cc1 JA |
1151 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1152 | { | |
1153 | struct io_ring_ctx *ctx; | |
78076bb6 | 1154 | int hash_bits; |
2b188cc1 JA |
1155 | |
1156 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1157 | if (!ctx) | |
1158 | return NULL; | |
1159 | ||
78076bb6 JA |
1160 | /* |
1161 | * Use 5 bits less than the max cq entries, that should give us around | |
1162 | * 32 entries per hash list if totally full and uniformly spread. | |
1163 | */ | |
1164 | hash_bits = ilog2(p->cq_entries); | |
1165 | hash_bits -= 5; | |
1166 | if (hash_bits <= 0) | |
1167 | hash_bits = 1; | |
1168 | ctx->cancel_hash_bits = hash_bits; | |
1169 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1170 | GFP_KERNEL); | |
1171 | if (!ctx->cancel_hash) | |
1172 | goto err; | |
1173 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1174 | ||
6224843d PB |
1175 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1176 | if (!ctx->dummy_ubuf) | |
1177 | goto err; | |
1178 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1179 | ctx->dummy_ubuf->ubuf = -1UL; | |
1180 | ||
21482896 | 1181 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1182 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1183 | goto err; | |
2b188cc1 JA |
1184 | |
1185 | ctx->flags = p->flags; | |
90554200 | 1186 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1187 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1188 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1189 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1190 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1191 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1192 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1193 | mutex_init(&ctx->uring_lock); |
311997b3 | 1194 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1195 | spin_lock_init(&ctx->completion_lock); |
540e32a0 | 1196 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1197 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1198 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1199 | spin_lock_init(&ctx->rsrc_ref_lock); |
1200 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1201 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1202 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1203 | INIT_LIST_HEAD(&ctx->tctx_list); |
1b4c351f | 1204 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
d0acdee2 | 1205 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1206 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1207 | return ctx; |
206aefde | 1208 | err: |
6224843d | 1209 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1210 | kfree(ctx->cancel_hash); |
206aefde JA |
1211 | kfree(ctx); |
1212 | return NULL; | |
2b188cc1 JA |
1213 | } |
1214 | ||
8f6ed49a PB |
1215 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1216 | { | |
1217 | struct io_rings *r = ctx->rings; | |
1218 | ||
1219 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1220 | ctx->cq_extra--; | |
1221 | } | |
1222 | ||
9cf7c104 | 1223 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1224 | { |
2bc9930e JA |
1225 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1226 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1227 | |
8f6ed49a | 1228 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1229 | } |
de0617e4 | 1230 | |
9d858b21 | 1231 | return false; |
de0617e4 JA |
1232 | } |
1233 | ||
c97d8a0f PB |
1234 | #define FFS_ASYNC_READ 0x1UL |
1235 | #define FFS_ASYNC_WRITE 0x2UL | |
1236 | #ifdef CONFIG_64BIT | |
1237 | #define FFS_ISREG 0x4UL | |
1238 | #else | |
1239 | #define FFS_ISREG 0x0UL | |
1240 | #endif | |
1241 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1242 | ||
1243 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1244 | { | |
1245 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1246 | } | |
1247 | ||
ce3d5aae PB |
1248 | static void io_req_track_inflight(struct io_kiocb *req) |
1249 | { | |
ce3d5aae | 1250 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1251 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1252 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1253 | } |
1254 | } | |
1255 | ||
1e6fa521 JA |
1256 | static void io_prep_async_work(struct io_kiocb *req) |
1257 | { | |
1258 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1259 | struct io_ring_ctx *ctx = req->ctx; |
1260 | ||
b8e64b53 PB |
1261 | if (!(req->flags & REQ_F_CREDS)) { |
1262 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1263 | req->creds = get_current_cred(); |
b8e64b53 | 1264 | } |
003e8dcc | 1265 | |
e1d675df PB |
1266 | req->work.list.next = NULL; |
1267 | req->work.flags = 0; | |
feaadc4f PB |
1268 | if (req->flags & REQ_F_FORCE_ASYNC) |
1269 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1270 | ||
1e6fa521 JA |
1271 | if (req->flags & REQ_F_ISREG) { |
1272 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1273 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1274 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1275 | if (def->unbound_nonreg_file) |
1276 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1277 | } | |
e1d675df PB |
1278 | |
1279 | switch (req->opcode) { | |
1280 | case IORING_OP_SPLICE: | |
1281 | case IORING_OP_TEE: | |
e1d675df PB |
1282 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1283 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1284 | break; | |
1285 | } | |
561fb04a | 1286 | } |
cccf0ee8 | 1287 | |
cbdcb435 | 1288 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1289 | { |
cbdcb435 | 1290 | struct io_kiocb *cur; |
54a91f3b | 1291 | |
44eff40a PB |
1292 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1293 | struct io_ring_ctx *ctx = req->ctx; | |
1294 | ||
1295 | spin_lock_irq(&ctx->completion_lock); | |
1296 | io_for_each_link(cur, req) | |
1297 | io_prep_async_work(cur); | |
1298 | spin_unlock_irq(&ctx->completion_lock); | |
1299 | } else { | |
1300 | io_for_each_link(cur, req) | |
1301 | io_prep_async_work(cur); | |
1302 | } | |
561fb04a JA |
1303 | } |
1304 | ||
ebf93667 | 1305 | static void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1306 | { |
a197f664 | 1307 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1308 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1309 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1310 | |
3bfe6106 JA |
1311 | BUG_ON(!tctx); |
1312 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1313 | |
cbdcb435 PB |
1314 | /* init ->work of the whole link before punting */ |
1315 | io_prep_async_link(req); | |
991468dc JA |
1316 | |
1317 | /* | |
1318 | * Not expected to happen, but if we do have a bug where this _can_ | |
1319 | * happen, catch it here and ensure the request is marked as | |
1320 | * canceled. That will make io-wq go through the usual work cancel | |
1321 | * procedure rather than attempt to run this request (or create a new | |
1322 | * worker for it). | |
1323 | */ | |
1324 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1325 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1326 | ||
d07f1e8a PB |
1327 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1328 | &req->work, req->flags); | |
ebf93667 | 1329 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1330 | if (link) |
1331 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1332 | } |
1333 | ||
1ee4160c | 1334 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1335 | __must_hold(&req->ctx->completion_lock) |
5262f567 | 1336 | { |
e8c2bc1f | 1337 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1338 | |
fd9c7bc5 | 1339 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
01cec8c1 PB |
1340 | atomic_set(&req->ctx->cq_timeouts, |
1341 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1342 | list_del_init(&req->timeout.list); |
d4d19c19 | 1343 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
216578e5 | 1344 | io_put_req_deferred(req, 1); |
5262f567 JA |
1345 | } |
1346 | } | |
1347 | ||
441b8a78 | 1348 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1349 | { |
441b8a78 | 1350 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1351 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1352 | struct io_defer_entry, list); | |
de0617e4 | 1353 | |
9cf7c104 | 1354 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1355 | break; |
27dc8338 | 1356 | list_del_init(&de->list); |
907d1df3 | 1357 | io_req_task_queue(de->req); |
27dc8338 | 1358 | kfree(de); |
441b8a78 | 1359 | } |
04518945 PB |
1360 | } |
1361 | ||
360428f8 | 1362 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
de0617e4 | 1363 | { |
441b8a78 | 1364 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1365 | |
f18ee4cf | 1366 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1367 | u32 events_needed, events_got; |
360428f8 | 1368 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1369 | struct io_kiocb, timeout.list); |
de0617e4 | 1370 | |
8eb7e2d0 | 1371 | if (io_is_timeout_noseq(req)) |
360428f8 | 1372 | break; |
f010505b MDG |
1373 | |
1374 | /* | |
1375 | * Since seq can easily wrap around over time, subtract | |
1376 | * the last seq at which timeouts were flushed before comparing. | |
1377 | * Assuming not more than 2^31-1 events have happened since, | |
1378 | * these subtractions won't have wrapped, so we can check if | |
1379 | * target is in [last_seq, current_seq] by comparing the two. | |
1380 | */ | |
1381 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1382 | events_got = seq - ctx->cq_last_tm_flush; | |
1383 | if (events_got < events_needed) | |
360428f8 | 1384 | break; |
bfe68a22 | 1385 | |
135fcde8 | 1386 | list_del_init(&req->timeout.list); |
1ee4160c | 1387 | io_kill_timeout(req, 0); |
f18ee4cf | 1388 | } |
f010505b | 1389 | ctx->cq_last_tm_flush = seq; |
360428f8 | 1390 | } |
5262f567 | 1391 | |
2335f6f5 | 1392 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1393 | { |
2335f6f5 PB |
1394 | if (ctx->off_timeout_used) |
1395 | io_flush_timeouts(ctx); | |
1396 | if (ctx->drain_active) | |
1397 | io_queue_deferred(ctx); | |
1398 | } | |
1399 | ||
1400 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1401 | { | |
1402 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1403 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1404 | /* order cqe stores with ring update */ |
1405 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1406 | } |
1407 | ||
90554200 JA |
1408 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1409 | { | |
1410 | struct io_rings *r = ctx->rings; | |
1411 | ||
a566c556 | 1412 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1413 | } |
1414 | ||
888aae2e PB |
1415 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1416 | { | |
1417 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1418 | } | |
1419 | ||
d068b506 | 1420 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1421 | { |
75b28aff | 1422 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1423 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1424 | |
115e12e5 SB |
1425 | /* |
1426 | * writes to the cq entry need to come after reading head; the | |
1427 | * control dependency is enough as we're using WRITE_ONCE to | |
1428 | * fill the cq entry | |
1429 | */ | |
a566c556 | 1430 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1431 | return NULL; |
1432 | ||
888aae2e | 1433 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1434 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1435 | } |
1436 | ||
f2842ab5 JA |
1437 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1438 | { | |
44c769de | 1439 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1440 | return false; |
7e55a19c SG |
1441 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1442 | return false; | |
44c769de | 1443 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1444 | } |
1445 | ||
b41e9852 | 1446 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1447 | { |
5fd46178 JA |
1448 | /* |
1449 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1450 | * io_should_wake() handle the termination of the loop and only | |
1451 | * wake as many waiters as we need to. | |
1452 | */ | |
1453 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1454 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1455 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1456 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1457 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1458 | eventfd_signal(ctx->cq_ev_fd, 1); |
311997b3 PB |
1459 | if (waitqueue_active(&ctx->poll_wait)) { |
1460 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1461 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1462 | } | |
1d7bb1d5 JA |
1463 | } |
1464 | ||
80c18e4a PB |
1465 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1466 | { | |
1467 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
5fd46178 JA |
1468 | if (wq_has_sleeper(&ctx->cq_wait)) |
1469 | wake_up_all(&ctx->cq_wait); | |
80c18e4a PB |
1470 | } |
1471 | if (io_should_trigger_evfd(ctx)) | |
1472 | eventfd_signal(ctx->cq_ev_fd, 1); | |
311997b3 PB |
1473 | if (waitqueue_active(&ctx->poll_wait)) { |
1474 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1475 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1476 | } | |
80c18e4a PB |
1477 | } |
1478 | ||
c4a2ed72 | 1479 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1480 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1481 | { |
1d7bb1d5 | 1482 | unsigned long flags; |
b18032bb | 1483 | bool all_flushed, posted; |
1d7bb1d5 | 1484 | |
a566c556 | 1485 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1486 | return false; |
1d7bb1d5 | 1487 | |
b18032bb | 1488 | posted = false; |
1d7bb1d5 | 1489 | spin_lock_irqsave(&ctx->completion_lock, flags); |
6c2450ae | 1490 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1491 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1492 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1493 | |
1d7bb1d5 JA |
1494 | if (!cqe && !force) |
1495 | break; | |
6c2450ae PB |
1496 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1497 | struct io_overflow_cqe, list); | |
1498 | if (cqe) | |
1499 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1500 | else | |
8f6ed49a PB |
1501 | io_account_cq_overflow(ctx); |
1502 | ||
b18032bb | 1503 | posted = true; |
6c2450ae PB |
1504 | list_del(&ocqe->list); |
1505 | kfree(ocqe); | |
1d7bb1d5 JA |
1506 | } |
1507 | ||
09e88404 PB |
1508 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1509 | if (all_flushed) { | |
5ed7a37d | 1510 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1511 | WRITE_ONCE(ctx->rings->sq_flags, |
1512 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1513 | } |
46930143 | 1514 | |
b18032bb JA |
1515 | if (posted) |
1516 | io_commit_cqring(ctx); | |
1d7bb1d5 | 1517 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
b18032bb JA |
1518 | if (posted) |
1519 | io_cqring_ev_posted(ctx); | |
09e88404 | 1520 | return all_flushed; |
1d7bb1d5 JA |
1521 | } |
1522 | ||
6c2450ae | 1523 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
6c503150 | 1524 | { |
ca0a2651 JA |
1525 | bool ret = true; |
1526 | ||
5ed7a37d | 1527 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1528 | /* iopoll syncs against uring_lock, not completion_lock */ |
1529 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1530 | mutex_lock(&ctx->uring_lock); | |
6c2450ae | 1531 | ret = __io_cqring_overflow_flush(ctx, force); |
6c503150 PB |
1532 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1533 | mutex_unlock(&ctx->uring_lock); | |
1534 | } | |
ca0a2651 JA |
1535 | |
1536 | return ret; | |
6c503150 PB |
1537 | } |
1538 | ||
abc54d63 JA |
1539 | /* |
1540 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1541 | * see commit f958d7b528b1 for details. | |
1542 | */ | |
1543 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1544 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1545 | ||
de9b4cca JA |
1546 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
1547 | { | |
abc54d63 | 1548 | return atomic_inc_not_zero(&req->refs); |
de9b4cca JA |
1549 | } |
1550 | ||
1551 | static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs) | |
1552 | { | |
abc54d63 JA |
1553 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1554 | return atomic_sub_and_test(refs, &req->refs); | |
de9b4cca JA |
1555 | } |
1556 | ||
1557 | static inline bool req_ref_put_and_test(struct io_kiocb *req) | |
1558 | { | |
abc54d63 JA |
1559 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1560 | return atomic_dec_and_test(&req->refs); | |
de9b4cca JA |
1561 | } |
1562 | ||
1563 | static inline void req_ref_put(struct io_kiocb *req) | |
1564 | { | |
abc54d63 | 1565 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
de9b4cca JA |
1566 | } |
1567 | ||
1568 | static inline void req_ref_get(struct io_kiocb *req) | |
1569 | { | |
abc54d63 JA |
1570 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1571 | atomic_inc(&req->refs); | |
de9b4cca JA |
1572 | } |
1573 | ||
d4d19c19 PB |
1574 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1575 | long res, unsigned int cflags) | |
2b188cc1 | 1576 | { |
cce4b8b0 | 1577 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1578 | |
cce4b8b0 PB |
1579 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1580 | if (!ocqe) { | |
1581 | /* | |
1582 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1583 | * or cannot allocate an overflow entry, then we need to drop it | |
1584 | * on the floor. | |
1585 | */ | |
8f6ed49a | 1586 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1587 | return false; |
2b188cc1 | 1588 | } |
cce4b8b0 | 1589 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1590 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1591 | WRITE_ONCE(ctx->rings->sq_flags, |
1592 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1593 | ||
cce4b8b0 | 1594 | } |
d4d19c19 | 1595 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1596 | ocqe->cqe.res = res; |
1597 | ocqe->cqe.flags = cflags; | |
1598 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1599 | return true; | |
2b188cc1 JA |
1600 | } |
1601 | ||
d4d19c19 PB |
1602 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1603 | long res, unsigned int cflags) | |
2b188cc1 JA |
1604 | { |
1605 | struct io_uring_cqe *cqe; | |
1606 | ||
d4d19c19 | 1607 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1608 | |
2b188cc1 JA |
1609 | /* |
1610 | * If we can't get a cq entry, userspace overflowed the | |
1611 | * submission (by quite a lot). Increment the overflow count in | |
1612 | * the ring. | |
1613 | */ | |
d068b506 | 1614 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1615 | if (likely(cqe)) { |
d4d19c19 | 1616 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1617 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1618 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1619 | return true; |
2b188cc1 | 1620 | } |
d4d19c19 | 1621 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1622 | } |
1623 | ||
8d13326e | 1624 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1625 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1626 | long res, unsigned int cflags) | |
bcda7baa | 1627 | { |
d4d19c19 | 1628 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1629 | } |
1630 | ||
7a612350 PB |
1631 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1632 | unsigned int cflags) | |
2b188cc1 | 1633 | { |
78e19bbe | 1634 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1635 | unsigned long flags; |
1636 | ||
1637 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
d4d19c19 | 1638 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1639 | /* |
1640 | * If we're the last reference to this request, add to our locked | |
1641 | * free_list cache. | |
1642 | */ | |
de9b4cca | 1643 | if (req_ref_put_and_test(req)) { |
7a612350 | 1644 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
93d2bcd2 | 1645 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) |
7a612350 PB |
1646 | io_disarm_next(req); |
1647 | if (req->link) { | |
1648 | io_req_task_queue(req->link); | |
1649 | req->link = NULL; | |
1650 | } | |
1651 | } | |
c7dae4ba JA |
1652 | io_dismantle_req(req); |
1653 | io_put_task(req->task, 1); | |
d0acdee2 PB |
1654 | list_add(&req->compl.list, &ctx->locked_free_list); |
1655 | ctx->locked_free_nr++; | |
180f829f PB |
1656 | } else { |
1657 | if (!percpu_ref_tryget(&ctx->refs)) | |
1658 | req = NULL; | |
1659 | } | |
7a612350 | 1660 | io_commit_cqring(ctx); |
2b188cc1 | 1661 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
7a612350 | 1662 | |
180f829f PB |
1663 | if (req) { |
1664 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1665 | percpu_ref_put(&ctx->refs); |
180f829f | 1666 | } |
229a7b63 JA |
1667 | } |
1668 | ||
4e3d9ff9 JA |
1669 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1670 | { | |
c854357b | 1671 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1672 | } |
1673 | ||
a38d68db | 1674 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1675 | unsigned int cflags) |
229a7b63 | 1676 | { |
4e3d9ff9 | 1677 | if (io_req_needs_clean(req)) |
68fb8979 | 1678 | io_clean_op(req); |
a38d68db PB |
1679 | req->result = res; |
1680 | req->compl.cflags = cflags; | |
e342c807 | 1681 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1682 | } |
1683 | ||
889fca73 PB |
1684 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1685 | long res, unsigned cflags) | |
bcda7baa | 1686 | { |
889fca73 PB |
1687 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1688 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1689 | else |
c7dae4ba | 1690 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1691 | } |
1692 | ||
a38d68db | 1693 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1694 | { |
889fca73 | 1695 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1696 | } |
1697 | ||
f41db273 PB |
1698 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1699 | { | |
93d2bcd2 | 1700 | req_set_fail(req); |
f41db273 PB |
1701 | io_put_req(req); |
1702 | io_req_complete_post(req, res, 0); | |
1703 | } | |
1704 | ||
864ea921 PB |
1705 | /* |
1706 | * Don't initialise the fields below on every allocation, but do that in | |
1707 | * advance and keep them valid across allocations. | |
1708 | */ | |
1709 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1710 | { | |
1711 | req->ctx = ctx; | |
1712 | req->link = NULL; | |
1713 | req->async_data = NULL; | |
1714 | /* not necessary, but safer to zero */ | |
1715 | req->result = 0; | |
1716 | } | |
1717 | ||
dac7a098 PB |
1718 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
1719 | struct io_comp_state *cs) | |
1720 | { | |
1721 | spin_lock_irq(&ctx->completion_lock); | |
d0acdee2 PB |
1722 | list_splice_init(&ctx->locked_free_list, &cs->free_list); |
1723 | ctx->locked_free_nr = 0; | |
dac7a098 PB |
1724 | spin_unlock_irq(&ctx->completion_lock); |
1725 | } | |
1726 | ||
dd78f492 | 1727 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1728 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1729 | { |
c7dae4ba JA |
1730 | struct io_submit_state *state = &ctx->submit_state; |
1731 | struct io_comp_state *cs = &state->comp; | |
dd78f492 | 1732 | int nr; |
0ddf92e8 | 1733 | |
c7dae4ba JA |
1734 | /* |
1735 | * If we have more than a batch's worth of requests in our IRQ side | |
1736 | * locked cache, grab the lock and move them over to our submission | |
1737 | * side cache. | |
1738 | */ | |
d0acdee2 | 1739 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
dac7a098 | 1740 | io_flush_cached_locked_reqs(ctx, cs); |
0ddf92e8 | 1741 | |
dd78f492 | 1742 | nr = state->free_reqs; |
c7dae4ba | 1743 | while (!list_empty(&cs->free_list)) { |
dd78f492 PB |
1744 | struct io_kiocb *req = list_first_entry(&cs->free_list, |
1745 | struct io_kiocb, compl.list); | |
1746 | ||
1b4c351f | 1747 | list_del(&req->compl.list); |
dd78f492 PB |
1748 | state->reqs[nr++] = req; |
1749 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1750 | break; |
1b4c351f JA |
1751 | } |
1752 | ||
dd78f492 PB |
1753 | state->free_reqs = nr; |
1754 | return nr != 0; | |
0ddf92e8 JA |
1755 | } |
1756 | ||
e5d1bc0a | 1757 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
2b188cc1 | 1758 | { |
e5d1bc0a | 1759 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1760 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1761 | int ret, i; | |
e5d1bc0a | 1762 | |
fe7e3257 | 1763 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1764 | |
864ea921 PB |
1765 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1766 | goto got_req; | |
e5d1bc0a | 1767 | |
864ea921 PB |
1768 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1769 | state->reqs); | |
fd6fab2c | 1770 | |
864ea921 PB |
1771 | /* |
1772 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1773 | * retry single alloc to be on the safe side. | |
1774 | */ | |
1775 | if (unlikely(ret <= 0)) { | |
1776 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1777 | if (!state->reqs[0]) | |
1778 | return NULL; | |
1779 | ret = 1; | |
2b188cc1 | 1780 | } |
864ea921 PB |
1781 | |
1782 | for (i = 0; i < ret; i++) | |
1783 | io_preinit_req(state->reqs[i], ctx); | |
1784 | state->free_reqs = ret; | |
e5d1bc0a | 1785 | got_req: |
291b2821 PB |
1786 | state->free_reqs--; |
1787 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1788 | } |
1789 | ||
e1d767f0 | 1790 | static inline void io_put_file(struct file *file) |
8da11c19 | 1791 | { |
e1d767f0 | 1792 | if (file) |
8da11c19 PB |
1793 | fput(file); |
1794 | } | |
1795 | ||
4edf20f9 | 1796 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1797 | { |
094bae49 | 1798 | unsigned int flags = req->flags; |
929a3af9 | 1799 | |
3a0a6902 PB |
1800 | if (io_req_needs_clean(req)) |
1801 | io_clean_op(req); | |
e1d767f0 PB |
1802 | if (!(flags & REQ_F_FIXED_FILE)) |
1803 | io_put_file(req->file); | |
269bbe5f BM |
1804 | if (req->fixed_rsrc_refs) |
1805 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1806 | if (req->async_data) { |
094bae49 | 1807 | kfree(req->async_data); |
99ebe4ef PB |
1808 | req->async_data = NULL; |
1809 | } | |
e65ef56d JA |
1810 | } |
1811 | ||
b23fcf47 | 1812 | /* must to be called somewhat shortly after putting a request */ |
7c660731 PB |
1813 | static inline void io_put_task(struct task_struct *task, int nr) |
1814 | { | |
1815 | struct io_uring_task *tctx = task->io_uring; | |
1816 | ||
1817 | percpu_counter_sub(&tctx->inflight, nr); | |
1818 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1819 | wake_up(&tctx->wait); | |
1820 | put_task_struct_many(task, nr); | |
1821 | } | |
1822 | ||
216578e5 | 1823 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1824 | { |
51a4cc11 | 1825 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1826 | |
216578e5 | 1827 | io_dismantle_req(req); |
7c660731 | 1828 | io_put_task(req->task, 1); |
c6ca97b3 | 1829 | |
3893f39f | 1830 | kmem_cache_free(req_cachep, req); |
ecfc5177 | 1831 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1832 | } |
1833 | ||
f2f87370 PB |
1834 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1835 | { | |
1836 | struct io_kiocb *nxt = req->link; | |
1837 | ||
1838 | req->link = nxt->link; | |
1839 | nxt->link = NULL; | |
1840 | } | |
1841 | ||
33cc89a9 PB |
1842 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1843 | __must_hold(&req->ctx->completion_lock) | |
2665abfd | 1844 | { |
33cc89a9 | 1845 | struct io_kiocb *link = req->link; |
f2f87370 | 1846 | |
900fad45 PB |
1847 | /* |
1848 | * Can happen if a linked timeout fired and link had been like | |
1849 | * req -> link t-out -> link t-out [-> ...] | |
1850 | */ | |
c9abd7ad PB |
1851 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1852 | struct io_timeout_data *io = link->async_data; | |
7c86ffee | 1853 | |
f2f87370 | 1854 | io_remove_next_linked(req); |
90cd7e42 | 1855 | link->timeout.head = NULL; |
fd9c7bc5 | 1856 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
d4d19c19 PB |
1857 | io_cqring_fill_event(link->ctx, link->user_data, |
1858 | -ECANCELED, 0); | |
33cc89a9 | 1859 | io_put_req_deferred(link, 1); |
d4729fbd | 1860 | return true; |
c9abd7ad PB |
1861 | } |
1862 | } | |
d4729fbd | 1863 | return false; |
7c86ffee PB |
1864 | } |
1865 | ||
d148ca4b | 1866 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1867 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1868 | { |
33cc89a9 | 1869 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1870 | |
f2f87370 | 1871 | req->link = NULL; |
f2f87370 PB |
1872 | while (link) { |
1873 | nxt = link->link; | |
1874 | link->link = NULL; | |
2665abfd | 1875 | |
f2f87370 | 1876 | trace_io_uring_fail_link(req, link); |
d4d19c19 | 1877 | io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0); |
1575f21a | 1878 | io_put_req_deferred(link, 2); |
f2f87370 | 1879 | link = nxt; |
9e645e11 | 1880 | } |
33cc89a9 | 1881 | } |
9e645e11 | 1882 | |
33cc89a9 PB |
1883 | static bool io_disarm_next(struct io_kiocb *req) |
1884 | __must_hold(&req->ctx->completion_lock) | |
1885 | { | |
1886 | bool posted = false; | |
1887 | ||
1888 | if (likely(req->flags & REQ_F_LINK_TIMEOUT)) | |
1889 | posted = io_kill_linked_timeout(req); | |
93d2bcd2 | 1890 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 1891 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
1892 | posted |= (req->link != NULL); |
1893 | io_fail_links(req); | |
1894 | } | |
1895 | return posted; | |
9e645e11 JA |
1896 | } |
1897 | ||
3fa5e0f3 | 1898 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1899 | { |
33cc89a9 | 1900 | struct io_kiocb *nxt; |
944e58bf | 1901 | |
9e645e11 JA |
1902 | /* |
1903 | * If LINK is set, we have dependent requests in this chain. If we | |
1904 | * didn't fail this request, queue the first one up, moving any other | |
1905 | * dependencies to the next request. In case of failure, fail the rest | |
1906 | * of the chain. | |
1907 | */ | |
93d2bcd2 | 1908 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) { |
33cc89a9 PB |
1909 | struct io_ring_ctx *ctx = req->ctx; |
1910 | unsigned long flags; | |
1911 | bool posted; | |
1912 | ||
1913 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1914 | posted = io_disarm_next(req); | |
1915 | if (posted) | |
1916 | io_commit_cqring(req->ctx); | |
1917 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1918 | if (posted) | |
1919 | io_cqring_ev_posted(ctx); | |
f2f87370 | 1920 | } |
33cc89a9 PB |
1921 | nxt = req->link; |
1922 | req->link = NULL; | |
1923 | return nxt; | |
4d7dd462 | 1924 | } |
9e645e11 | 1925 | |
f2f87370 | 1926 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1927 | { |
cdbff982 | 1928 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1929 | return NULL; |
1930 | return __io_req_find_next(req); | |
1931 | } | |
1932 | ||
2c32395d PB |
1933 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
1934 | { | |
1935 | if (!ctx) | |
1936 | return; | |
1937 | if (ctx->submit_state.comp.nr) { | |
1938 | mutex_lock(&ctx->uring_lock); | |
2a2758f2 | 1939 | io_submit_flush_completions(ctx); |
2c32395d PB |
1940 | mutex_unlock(&ctx->uring_lock); |
1941 | } | |
1942 | percpu_ref_put(&ctx->refs); | |
1943 | } | |
1944 | ||
7cbf1722 | 1945 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 1946 | { |
ebd0df2e | 1947 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
1948 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
1949 | task_work); | |
c40f6379 | 1950 | |
16f72070 | 1951 | while (1) { |
3f18407d PB |
1952 | struct io_wq_work_node *node; |
1953 | ||
1954 | spin_lock_irq(&tctx->task_lock); | |
c6538be9 | 1955 | node = tctx->task_list.first; |
3f18407d PB |
1956 | INIT_WQ_LIST(&tctx->task_list); |
1957 | spin_unlock_irq(&tctx->task_lock); | |
1958 | ||
3f18407d PB |
1959 | while (node) { |
1960 | struct io_wq_work_node *next = node->next; | |
1961 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
1962 | io_task_work.node); | |
1963 | ||
1964 | if (req->ctx != ctx) { | |
1965 | ctx_flush_and_put(ctx); | |
1966 | ctx = req->ctx; | |
1967 | percpu_ref_get(&ctx->refs); | |
1968 | } | |
5b0a6acc | 1969 | req->io_task_work.func(req); |
3f18407d PB |
1970 | node = next; |
1971 | } | |
7a778f9d | 1972 | if (wq_list_empty(&tctx->task_list)) { |
110aa25c | 1973 | spin_lock_irq(&tctx->task_lock); |
7a778f9d | 1974 | clear_bit(0, &tctx->task_state); |
110aa25c JA |
1975 | if (wq_list_empty(&tctx->task_list)) { |
1976 | spin_unlock_irq(&tctx->task_lock); | |
7a778f9d | 1977 | break; |
110aa25c JA |
1978 | } |
1979 | spin_unlock_irq(&tctx->task_lock); | |
7a778f9d PB |
1980 | /* another tctx_task_work() is enqueued, yield */ |
1981 | if (test_and_set_bit(0, &tctx->task_state)) | |
1982 | break; | |
1983 | } | |
7cbf1722 | 1984 | cond_resched(); |
3f18407d | 1985 | } |
ebd0df2e PB |
1986 | |
1987 | ctx_flush_and_put(ctx); | |
7cbf1722 JA |
1988 | } |
1989 | ||
e09ee510 | 1990 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 1991 | { |
c15b79de | 1992 | struct task_struct *tsk = req->task; |
7cbf1722 | 1993 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 1994 | enum task_work_notify_mode notify; |
e09ee510 | 1995 | struct io_wq_work_node *node; |
0b81e80c | 1996 | unsigned long flags; |
7cbf1722 JA |
1997 | |
1998 | WARN_ON_ONCE(!tctx); | |
1999 | ||
0b81e80c | 2000 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2001 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
0b81e80c | 2002 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2003 | |
2004 | /* task_work already pending, we're done */ | |
2005 | if (test_bit(0, &tctx->task_state) || | |
2006 | test_and_set_bit(0, &tctx->task_state)) | |
e09ee510 | 2007 | return; |
7cbf1722 | 2008 | |
c15b79de PB |
2009 | /* |
2010 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2011 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2012 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2013 | * will do the job. | |
2014 | */ | |
2015 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2016 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2017 | wake_up_process(tsk); | |
e09ee510 | 2018 | return; |
c15b79de | 2019 | } |
2215bed9 | 2020 | |
e09ee510 | 2021 | clear_bit(0, &tctx->task_state); |
0b81e80c | 2022 | spin_lock_irqsave(&tctx->task_lock, flags); |
e09ee510 PB |
2023 | node = tctx->task_list.first; |
2024 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2025 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2026 | |
e09ee510 PB |
2027 | while (node) { |
2028 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2029 | node = node->next; | |
2030 | if (llist_add(&req->io_task_work.fallback_node, | |
2031 | &req->ctx->fallback_llist)) | |
2032 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2033 | } | |
eab30c4d PB |
2034 | } |
2035 | ||
5b0a6acc | 2036 | static void io_req_task_cancel(struct io_kiocb *req) |
c40f6379 | 2037 | { |
87ceb6a6 | 2038 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2039 | |
e83acd7d | 2040 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
792bb6eb | 2041 | mutex_lock(&ctx->uring_lock); |
2593553a | 2042 | io_req_complete_failed(req, req->result); |
792bb6eb | 2043 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2044 | } |
2045 | ||
5b0a6acc | 2046 | static void io_req_task_submit(struct io_kiocb *req) |
c40f6379 JA |
2047 | { |
2048 | struct io_ring_ctx *ctx = req->ctx; | |
2049 | ||
04fc6c80 | 2050 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2051 | mutex_lock(&ctx->uring_lock); |
9c688260 | 2052 | if (!(req->task->flags & PF_EXITING) && !req->task->in_execve) |
c5eef2b9 | 2053 | __io_queue_sqe(req); |
81b6d05c | 2054 | else |
2593553a | 2055 | io_req_complete_failed(req, -EFAULT); |
81b6d05c | 2056 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2057 | } |
2058 | ||
2c4b8eb6 | 2059 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2060 | { |
2c4b8eb6 | 2061 | req->result = ret; |
5b0a6acc | 2062 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2063 | io_req_task_work_add(req); |
c40f6379 JA |
2064 | } |
2065 | ||
2c4b8eb6 | 2066 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2067 | { |
5b0a6acc | 2068 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2069 | io_req_task_work_add(req); |
a3df7698 PB |
2070 | } |
2071 | ||
773af691 JA |
2072 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2073 | { | |
2074 | req->io_task_work.func = io_queue_async_work; | |
2075 | io_req_task_work_add(req); | |
2076 | } | |
2077 | ||
f2f87370 | 2078 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2079 | { |
9b5f7bd9 | 2080 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2081 | |
2082 | if (nxt) | |
906a8c3f | 2083 | io_req_task_queue(nxt); |
c69f8dbe JL |
2084 | } |
2085 | ||
c3524383 | 2086 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2087 | { |
c3524383 PB |
2088 | io_queue_next(req); |
2089 | __io_free_req(req); | |
2090 | } | |
8766dd51 | 2091 | |
2d6500d4 | 2092 | struct req_batch { |
5af1d13e PB |
2093 | struct task_struct *task; |
2094 | int task_refs; | |
1b4c351f | 2095 | int ctx_refs; |
2d6500d4 PB |
2096 | }; |
2097 | ||
5af1d13e PB |
2098 | static inline void io_init_req_batch(struct req_batch *rb) |
2099 | { | |
5af1d13e | 2100 | rb->task_refs = 0; |
9ae72463 | 2101 | rb->ctx_refs = 0; |
5af1d13e PB |
2102 | rb->task = NULL; |
2103 | } | |
2104 | ||
2d6500d4 PB |
2105 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2106 | struct req_batch *rb) | |
2107 | { | |
6e833d53 | 2108 | if (rb->task) |
7c660731 | 2109 | io_put_task(rb->task, rb->task_refs); |
9ae72463 PB |
2110 | if (rb->ctx_refs) |
2111 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
2d6500d4 PB |
2112 | } |
2113 | ||
6ff119a6 PB |
2114 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2115 | struct io_submit_state *state) | |
2d6500d4 | 2116 | { |
f2f87370 | 2117 | io_queue_next(req); |
96670657 | 2118 | io_dismantle_req(req); |
2d6500d4 | 2119 | |
e3bc8e9d | 2120 | if (req->task != rb->task) { |
7c660731 PB |
2121 | if (rb->task) |
2122 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2123 | rb->task = req->task; |
2124 | rb->task_refs = 0; | |
5af1d13e | 2125 | } |
e3bc8e9d | 2126 | rb->task_refs++; |
9ae72463 | 2127 | rb->ctx_refs++; |
5af1d13e | 2128 | |
bd759045 | 2129 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2130 | state->reqs[state->free_reqs++] = req; |
bd759045 PB |
2131 | else |
2132 | list_add(&req->compl.list, &state->comp.free_list); | |
7a743e22 PB |
2133 | } |
2134 | ||
2a2758f2 | 2135 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
905c172f | 2136 | { |
2a2758f2 | 2137 | struct io_comp_state *cs = &ctx->submit_state.comp; |
905c172f | 2138 | int i, nr = cs->nr; |
905c172f PB |
2139 | struct req_batch rb; |
2140 | ||
905c172f PB |
2141 | spin_lock_irq(&ctx->completion_lock); |
2142 | for (i = 0; i < nr; i++) { | |
5182ed2e PB |
2143 | struct io_kiocb *req = cs->reqs[i]; |
2144 | ||
d4d19c19 PB |
2145 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2146 | req->compl.cflags); | |
905c172f PB |
2147 | } |
2148 | io_commit_cqring(ctx); | |
2149 | spin_unlock_irq(&ctx->completion_lock); | |
905c172f | 2150 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2151 | |
2152 | io_init_req_batch(&rb); | |
905c172f | 2153 | for (i = 0; i < nr; i++) { |
5182ed2e | 2154 | struct io_kiocb *req = cs->reqs[i]; |
905c172f PB |
2155 | |
2156 | /* submission and completion refs */ | |
de9b4cca | 2157 | if (req_ref_sub_and_test(req, 2)) |
6ff119a6 | 2158 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2159 | } |
2160 | ||
2161 | io_req_free_batch_finish(ctx, &rb); | |
2162 | cs->nr = 0; | |
7a743e22 PB |
2163 | } |
2164 | ||
ba816ad6 JA |
2165 | /* |
2166 | * Drop reference to request, return next in chain (if there is one) if this | |
2167 | * was the last reference to this request. | |
2168 | */ | |
0d85035a | 2169 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2170 | { |
9b5f7bd9 PB |
2171 | struct io_kiocb *nxt = NULL; |
2172 | ||
de9b4cca | 2173 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2174 | nxt = io_req_find_next(req); |
4d7dd462 | 2175 | __io_free_req(req); |
2a44f467 | 2176 | } |
9b5f7bd9 | 2177 | return nxt; |
2b188cc1 JA |
2178 | } |
2179 | ||
0d85035a | 2180 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2181 | { |
de9b4cca | 2182 | if (req_ref_put_and_test(req)) |
e65ef56d | 2183 | io_free_req(req); |
2b188cc1 JA |
2184 | } |
2185 | ||
216578e5 PB |
2186 | static void io_free_req_deferred(struct io_kiocb *req) |
2187 | { | |
5b0a6acc | 2188 | req->io_task_work.func = io_free_req; |
e09ee510 | 2189 | io_req_task_work_add(req); |
216578e5 PB |
2190 | } |
2191 | ||
2192 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) | |
2193 | { | |
de9b4cca | 2194 | if (req_ref_sub_and_test(req, refs)) |
216578e5 PB |
2195 | io_free_req_deferred(req); |
2196 | } | |
2197 | ||
6c503150 | 2198 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2199 | { |
2200 | /* See comment at the top of this file */ | |
2201 | smp_rmb(); | |
e23de15f | 2202 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2203 | } |
2204 | ||
fb5ccc98 PB |
2205 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2206 | { | |
2207 | struct io_rings *rings = ctx->rings; | |
2208 | ||
2209 | /* make sure SQ entry isn't read before tail */ | |
2210 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2211 | } | |
2212 | ||
8ff069bf | 2213 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2214 | { |
8ff069bf | 2215 | unsigned int cflags; |
e94f141b | 2216 | |
bcda7baa JA |
2217 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2218 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2219 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2220 | kfree(kbuf); |
2221 | return cflags; | |
e94f141b JA |
2222 | } |
2223 | ||
8ff069bf | 2224 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2225 | { |
4d954c25 | 2226 | struct io_buffer *kbuf; |
bcda7baa | 2227 | |
4d954c25 | 2228 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2229 | return io_put_kbuf(req, kbuf); |
2230 | } | |
2231 | ||
4c6e277c JA |
2232 | static inline bool io_run_task_work(void) |
2233 | { | |
ef98eb04 | 2234 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2235 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2236 | tracehook_notify_signal(); |
4c6e277c JA |
2237 | return true; |
2238 | } | |
2239 | ||
2240 | return false; | |
bcda7baa JA |
2241 | } |
2242 | ||
def596e9 JA |
2243 | /* |
2244 | * Find and free completed poll iocbs | |
2245 | */ | |
2246 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
3c30ef0f | 2247 | struct list_head *done, bool resubmit) |
def596e9 | 2248 | { |
8237e045 | 2249 | struct req_batch rb; |
def596e9 | 2250 | struct io_kiocb *req; |
bbde017a XW |
2251 | |
2252 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2253 | smp_rmb(); | |
def596e9 | 2254 | |
5af1d13e | 2255 | io_init_req_batch(&rb); |
def596e9 | 2256 | while (!list_empty(done)) { |
bcda7baa JA |
2257 | int cflags = 0; |
2258 | ||
d21ffe7e | 2259 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2260 | list_del(&req->inflight_entry); |
2261 | ||
3c30ef0f | 2262 | if (READ_ONCE(req->result) == -EAGAIN && resubmit && |
8c130827 | 2263 | !(req->flags & REQ_F_DONT_REISSUE)) { |
bbde017a | 2264 | req->iopoll_completed = 0; |
8c130827 | 2265 | req_ref_get(req); |
773af691 | 2266 | io_req_task_queue_reissue(req); |
8c130827 | 2267 | continue; |
bbde017a | 2268 | } |
def596e9 | 2269 | |
bcda7baa | 2270 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2271 | cflags = io_put_rw_kbuf(req); |
bcda7baa | 2272 | |
d4d19c19 | 2273 | __io_cqring_fill_event(ctx, req->user_data, req->result, cflags); |
def596e9 JA |
2274 | (*nr_events)++; |
2275 | ||
de9b4cca | 2276 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2277 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2278 | } |
def596e9 | 2279 | |
09bb8394 | 2280 | io_commit_cqring(ctx); |
80c18e4a | 2281 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2282 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2283 | } |
2284 | ||
def596e9 | 2285 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
3c30ef0f | 2286 | long min, bool resubmit) |
def596e9 JA |
2287 | { |
2288 | struct io_kiocb *req, *tmp; | |
2289 | LIST_HEAD(done); | |
2290 | bool spin; | |
def596e9 JA |
2291 | |
2292 | /* | |
2293 | * Only spin for completions if we don't have multiple devices hanging | |
2294 | * off our complete list, and we're under the requested amount. | |
2295 | */ | |
915b3dde | 2296 | spin = !ctx->poll_multi_queue && *nr_events < min; |
def596e9 | 2297 | |
d21ffe7e | 2298 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2299 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2300 | int ret; |
def596e9 JA |
2301 | |
2302 | /* | |
581f9810 BM |
2303 | * Move completed and retryable entries to our local lists. |
2304 | * If we find a request that requires polling, break out | |
2305 | * and complete those lists first, if we have entries there. | |
def596e9 | 2306 | */ |
65a6543d | 2307 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2308 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2309 | continue; |
2310 | } | |
2311 | if (!list_empty(&done)) | |
2312 | break; | |
2313 | ||
2314 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
a2416e1e PB |
2315 | if (unlikely(ret < 0)) |
2316 | return ret; | |
2317 | else if (ret) | |
2318 | spin = false; | |
def596e9 | 2319 | |
3aadc23e PB |
2320 | /* iopoll may have completed current req */ |
2321 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2322 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2323 | } |
2324 | ||
2325 | if (!list_empty(&done)) | |
3c30ef0f | 2326 | io_iopoll_complete(ctx, nr_events, &done, resubmit); |
def596e9 | 2327 | |
a2416e1e | 2328 | return 0; |
def596e9 JA |
2329 | } |
2330 | ||
def596e9 JA |
2331 | /* |
2332 | * We can't just wait for polled events to come to us, we have to actively | |
2333 | * find and complete them. | |
2334 | */ | |
b2edc0a7 | 2335 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2336 | { |
2337 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2338 | return; | |
2339 | ||
2340 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2341 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2342 | unsigned int nr_events = 0; |
2343 | ||
3c30ef0f | 2344 | io_do_iopoll(ctx, &nr_events, 0, false); |
08f5439f | 2345 | |
b2edc0a7 PB |
2346 | /* let it sleep and repeat later if can't complete a request */ |
2347 | if (nr_events == 0) | |
2348 | break; | |
08f5439f JA |
2349 | /* |
2350 | * Ensure we allow local-to-the-cpu processing to take place, | |
2351 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2352 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2353 | */ |
3fcee5a6 PB |
2354 | if (need_resched()) { |
2355 | mutex_unlock(&ctx->uring_lock); | |
2356 | cond_resched(); | |
2357 | mutex_lock(&ctx->uring_lock); | |
2358 | } | |
def596e9 JA |
2359 | } |
2360 | mutex_unlock(&ctx->uring_lock); | |
2361 | } | |
2362 | ||
7668b92a | 2363 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2364 | { |
7668b92a | 2365 | unsigned int nr_events = 0; |
e9979b36 | 2366 | int ret = 0; |
500f9fba | 2367 | |
c7849be9 XW |
2368 | /* |
2369 | * We disallow the app entering submit/complete with polling, but we | |
2370 | * still need to lock the ring to prevent racing with polled issue | |
2371 | * that got punted to a workqueue. | |
2372 | */ | |
2373 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2374 | /* |
2375 | * Don't enter poll loop if we already have events pending. | |
2376 | * If we do, we can potentially be spinning for commands that | |
2377 | * already triggered a CQE (eg in error). | |
2378 | */ | |
5ed7a37d | 2379 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2380 | __io_cqring_overflow_flush(ctx, false); |
2381 | if (io_cqring_events(ctx)) | |
2382 | goto out; | |
def596e9 | 2383 | do { |
500f9fba JA |
2384 | /* |
2385 | * If a submit got punted to a workqueue, we can have the | |
2386 | * application entering polling for a command before it gets | |
2387 | * issued. That app will hold the uring_lock for the duration | |
2388 | * of the poll right here, so we need to take a breather every | |
2389 | * now and then to ensure that the issue has a chance to add | |
2390 | * the poll to the issued list. Otherwise we can spin here | |
2391 | * forever, while the workqueue is stuck trying to acquire the | |
2392 | * very same mutex. | |
2393 | */ | |
e9979b36 | 2394 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2395 | u32 tail = ctx->cached_cq_tail; |
2396 | ||
500f9fba | 2397 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2398 | io_run_task_work(); |
500f9fba | 2399 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2400 | |
8f487ef2 PB |
2401 | /* some requests don't go through iopoll_list */ |
2402 | if (tail != ctx->cached_cq_tail || | |
2403 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2404 | break; |
500f9fba | 2405 | } |
3c30ef0f | 2406 | ret = io_do_iopoll(ctx, &nr_events, min, true); |
f39c8a5b PB |
2407 | } while (!ret && nr_events < min && !need_resched()); |
2408 | out: | |
500f9fba | 2409 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2410 | return ret; |
2411 | } | |
2412 | ||
491381ce | 2413 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2414 | { |
491381ce JA |
2415 | /* |
2416 | * Tell lockdep we inherited freeze protection from submission | |
2417 | * thread. | |
2418 | */ | |
2419 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2420 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2421 | |
1c98679d PB |
2422 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2423 | sb_end_write(sb); | |
2b188cc1 JA |
2424 | } |
2425 | } | |
2426 | ||
b63534c4 | 2427 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2428 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2429 | { |
ab454438 | 2430 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2431 | |
ab454438 PB |
2432 | if (!rw) |
2433 | return !io_req_prep_async(req); | |
2434 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2435 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2436 | return true; | |
b63534c4 | 2437 | } |
b63534c4 | 2438 | |
3e6a0d3c | 2439 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2440 | { |
355afaeb | 2441 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2442 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2443 | |
355afaeb JA |
2444 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2445 | return false; | |
3e6a0d3c JA |
2446 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2447 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2448 | return false; |
7c977a58 JA |
2449 | /* |
2450 | * If ref is dying, we might be running poll reap from the exit work. | |
2451 | * Don't attempt to reissue from that path, just let it fail with | |
2452 | * -EAGAIN. | |
2453 | */ | |
3e6a0d3c JA |
2454 | if (percpu_ref_is_dying(&ctx->refs)) |
2455 | return false; | |
ef046888 JA |
2456 | /* |
2457 | * Play it safe and assume not safe to re-import and reissue if we're | |
2458 | * not in the original thread group (or in task context). | |
2459 | */ | |
2460 | if (!same_thread_group(req->task, current) || !in_task()) | |
2461 | return false; | |
3e6a0d3c JA |
2462 | return true; |
2463 | } | |
e82ad485 | 2464 | #else |
a1ff1e3f | 2465 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2466 | { |
2467 | return false; | |
2468 | } | |
e82ad485 | 2469 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2470 | { |
b63534c4 JA |
2471 | return false; |
2472 | } | |
3e6a0d3c | 2473 | #endif |
b63534c4 | 2474 | |
9011bf9a PB |
2475 | static void io_fallback_req_func(struct work_struct *work) |
2476 | { | |
2477 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
2478 | fallback_work.work); | |
2479 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
2480 | struct io_kiocb *req, *tmp; | |
2481 | ||
9cb0073b | 2482 | percpu_ref_get(&ctx->refs); |
5b0a6acc PB |
2483 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) |
2484 | req->io_task_work.func(req); | |
9cb0073b | 2485 | percpu_ref_put(&ctx->refs); |
9011bf9a PB |
2486 | } |
2487 | ||
a1d7c393 | 2488 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
889fca73 | 2489 | unsigned int issue_flags) |
a1d7c393 | 2490 | { |
2f8e45f1 PB |
2491 | int cflags = 0; |
2492 | ||
b65c128f PB |
2493 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2494 | kiocb_end_write(req); | |
9532b99b PB |
2495 | if (res != req->result) { |
2496 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2497 | io_rw_should_reissue(req)) { | |
2498 | req->flags |= REQ_F_REISSUE; | |
2499 | return; | |
2500 | } | |
93d2bcd2 | 2501 | req_set_fail(req); |
9532b99b | 2502 | } |
2f8e45f1 PB |
2503 | if (req->flags & REQ_F_BUFFER_SELECTED) |
2504 | cflags = io_put_rw_kbuf(req); | |
2505 | __io_req_complete(req, issue_flags, res, cflags); | |
ba816ad6 JA |
2506 | } |
2507 | ||
2508 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2509 | { | |
9adbd45d | 2510 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2511 | |
889fca73 | 2512 | __io_complete_rw(req, res, res2, 0); |
2b188cc1 JA |
2513 | } |
2514 | ||
def596e9 JA |
2515 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2516 | { | |
9adbd45d | 2517 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2518 | |
491381ce JA |
2519 | if (kiocb->ki_flags & IOCB_WRITE) |
2520 | kiocb_end_write(req); | |
9532b99b | 2521 | if (unlikely(res != req->result)) { |
a1ff1e3f JA |
2522 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
2523 | io_resubmit_prep(req))) { | |
93d2bcd2 | 2524 | req_set_fail(req); |
9532b99b PB |
2525 | req->flags |= REQ_F_DONT_REISSUE; |
2526 | } | |
8c130827 | 2527 | } |
bbde017a XW |
2528 | |
2529 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2530 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2531 | smp_wmb(); |
2532 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2533 | } |
2534 | ||
2535 | /* | |
2536 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2537 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2538 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2539 | * accessing the kiocb cookie. |
2540 | */ | |
cb3d8972 | 2541 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2542 | { |
2543 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2544 | const bool in_async = io_wq_current_is_worker(); |
2545 | ||
2546 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2547 | if (unlikely(in_async)) | |
2548 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2549 | |
2550 | /* | |
2551 | * Track whether we have multiple files in our lists. This will impact | |
2552 | * how we do polling eventually, not spinning if we're on potentially | |
2553 | * different devices. | |
2554 | */ | |
540e32a0 | 2555 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2556 | ctx->poll_multi_queue = false; |
2557 | } else if (!ctx->poll_multi_queue) { | |
def596e9 | 2558 | struct io_kiocb *list_req; |
915b3dde | 2559 | unsigned int queue_num0, queue_num1; |
def596e9 | 2560 | |
540e32a0 | 2561 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2562 | inflight_entry); |
915b3dde HX |
2563 | |
2564 | if (list_req->file != req->file) { | |
2565 | ctx->poll_multi_queue = true; | |
2566 | } else { | |
2567 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); | |
2568 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); | |
2569 | if (queue_num0 != queue_num1) | |
2570 | ctx->poll_multi_queue = true; | |
2571 | } | |
def596e9 JA |
2572 | } |
2573 | ||
2574 | /* | |
2575 | * For fast devices, IO may have already completed. If it has, add | |
2576 | * it to the front so we find it first. | |
2577 | */ | |
65a6543d | 2578 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2579 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2580 | else |
d21ffe7e | 2581 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2582 | |
cb3d8972 PB |
2583 | if (unlikely(in_async)) { |
2584 | /* | |
2585 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2586 | * in sq thread task context or in io worker task context. If | |
2587 | * current task context is sq thread, we don't need to check | |
2588 | * whether should wake up sq thread. | |
2589 | */ | |
2590 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2591 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2592 | wake_up(&ctx->sq_data->wait); | |
2593 | ||
2594 | mutex_unlock(&ctx->uring_lock); | |
2595 | } | |
def596e9 JA |
2596 | } |
2597 | ||
9f13c35b PB |
2598 | static inline void io_state_file_put(struct io_submit_state *state) |
2599 | { | |
02b23a9a PB |
2600 | if (state->file_refs) { |
2601 | fput_many(state->file, state->file_refs); | |
2602 | state->file_refs = 0; | |
2603 | } | |
9a56a232 JA |
2604 | } |
2605 | ||
2606 | /* | |
2607 | * Get as many references to a file as we have IOs left in this submission, | |
2608 | * assuming most submissions are for one file, or at least that each file | |
2609 | * has more than one submission. | |
2610 | */ | |
8da11c19 | 2611 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
9a56a232 JA |
2612 | { |
2613 | if (!state) | |
2614 | return fget(fd); | |
2615 | ||
6e1271e6 | 2616 | if (state->file_refs) { |
9a56a232 | 2617 | if (state->fd == fd) { |
6e1271e6 | 2618 | state->file_refs--; |
9a56a232 JA |
2619 | return state->file; |
2620 | } | |
02b23a9a | 2621 | io_state_file_put(state); |
9a56a232 JA |
2622 | } |
2623 | state->file = fget_many(fd, state->ios_left); | |
6e1271e6 | 2624 | if (unlikely(!state->file)) |
9a56a232 JA |
2625 | return NULL; |
2626 | ||
2627 | state->fd = fd; | |
6e1271e6 | 2628 | state->file_refs = state->ios_left - 1; |
9a56a232 JA |
2629 | return state->file; |
2630 | } | |
2631 | ||
4503b767 JA |
2632 | static bool io_bdev_nowait(struct block_device *bdev) |
2633 | { | |
9ba0d0c8 | 2634 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2635 | } |
2636 | ||
2b188cc1 JA |
2637 | /* |
2638 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2639 | * any file. For now, just ensure that anything potentially problematic is done | |
2640 | * inline. | |
2641 | */ | |
b191e2df | 2642 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2643 | { |
2644 | umode_t mode = file_inode(file)->i_mode; | |
2645 | ||
4503b767 | 2646 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2647 | if (IS_ENABLED(CONFIG_BLOCK) && |
2648 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2649 | return true; |
2650 | return false; | |
2651 | } | |
976517f1 | 2652 | if (S_ISSOCK(mode)) |
2b188cc1 | 2653 | return true; |
4503b767 | 2654 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2655 | if (IS_ENABLED(CONFIG_BLOCK) && |
2656 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2657 | file->f_op != &io_uring_fops) |
2658 | return true; | |
2659 | return false; | |
2660 | } | |
2b188cc1 | 2661 | |
c5b85625 JA |
2662 | /* any ->read/write should understand O_NONBLOCK */ |
2663 | if (file->f_flags & O_NONBLOCK) | |
2664 | return true; | |
2665 | ||
af197f50 JA |
2666 | if (!(file->f_mode & FMODE_NOWAIT)) |
2667 | return false; | |
2668 | ||
2669 | if (rw == READ) | |
2670 | return file->f_op->read_iter != NULL; | |
2671 | ||
2672 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2673 | } |
2674 | ||
b191e2df | 2675 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2676 | { |
b191e2df | 2677 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2678 | return true; |
b191e2df | 2679 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2680 | return true; |
2681 | ||
b191e2df | 2682 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2683 | } |
2684 | ||
a88fc400 | 2685 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2686 | { |
def596e9 | 2687 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2688 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2689 | struct file *file = req->file; |
09bb8394 JA |
2690 | unsigned ioprio; |
2691 | int ret; | |
2b188cc1 | 2692 | |
c97d8a0f | 2693 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2694 | req->flags |= REQ_F_ISREG; |
2695 | ||
2b188cc1 | 2696 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2697 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2698 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2699 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2700 | } |
2b188cc1 | 2701 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2702 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2703 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2704 | if (unlikely(ret)) | |
2705 | return ret; | |
2b188cc1 | 2706 | |
75c668cd PB |
2707 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2708 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2709 | req->flags |= REQ_F_NOWAIT; | |
2710 | ||
2b188cc1 JA |
2711 | ioprio = READ_ONCE(sqe->ioprio); |
2712 | if (ioprio) { | |
2713 | ret = ioprio_check_cap(ioprio); | |
2714 | if (ret) | |
09bb8394 | 2715 | return ret; |
2b188cc1 JA |
2716 | |
2717 | kiocb->ki_ioprio = ioprio; | |
2718 | } else | |
2719 | kiocb->ki_ioprio = get_current_ioprio(); | |
2720 | ||
def596e9 | 2721 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2722 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2723 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2724 | return -EOPNOTSUPP; |
2b188cc1 | 2725 | |
def596e9 JA |
2726 | kiocb->ki_flags |= IOCB_HIPRI; |
2727 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2728 | req->iopoll_completed = 0; |
def596e9 | 2729 | } else { |
09bb8394 JA |
2730 | if (kiocb->ki_flags & IOCB_HIPRI) |
2731 | return -EINVAL; | |
def596e9 JA |
2732 | kiocb->ki_complete = io_complete_rw; |
2733 | } | |
9adbd45d | 2734 | |
eae071c9 PB |
2735 | if (req->opcode == IORING_OP_READ_FIXED || |
2736 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2737 | req->imu = NULL; | |
2738 | io_req_set_rsrc_node(req); | |
2739 | } | |
2740 | ||
3529d8c2 JA |
2741 | req->rw.addr = READ_ONCE(sqe->addr); |
2742 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2743 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2744 | return 0; |
2b188cc1 JA |
2745 | } |
2746 | ||
2747 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2748 | { | |
2749 | switch (ret) { | |
2750 | case -EIOCBQUEUED: | |
2751 | break; | |
2752 | case -ERESTARTSYS: | |
2753 | case -ERESTARTNOINTR: | |
2754 | case -ERESTARTNOHAND: | |
2755 | case -ERESTART_RESTARTBLOCK: | |
2756 | /* | |
2757 | * We can't just restart the syscall, since previously | |
2758 | * submitted sqes may already be in progress. Just fail this | |
2759 | * IO with EINTR. | |
2760 | */ | |
2761 | ret = -EINTR; | |
df561f66 | 2762 | fallthrough; |
2b188cc1 JA |
2763 | default: |
2764 | kiocb->ki_complete(kiocb, ret, 0); | |
2765 | } | |
2766 | } | |
2767 | ||
a1d7c393 | 2768 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2769 | unsigned int issue_flags) |
ba816ad6 | 2770 | { |
ba04291e | 2771 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2772 | struct io_async_rw *io = req->async_data; |
97284637 | 2773 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2774 | |
227c0c96 | 2775 | /* add previously done IO, if any */ |
e8c2bc1f | 2776 | if (io && io->bytes_done > 0) { |
227c0c96 | 2777 | if (ret < 0) |
e8c2bc1f | 2778 | ret = io->bytes_done; |
227c0c96 | 2779 | else |
e8c2bc1f | 2780 | ret += io->bytes_done; |
227c0c96 JA |
2781 | } |
2782 | ||
ba04291e JA |
2783 | if (req->flags & REQ_F_CUR_POS) |
2784 | req->file->f_pos = kiocb->ki_pos; | |
e149bd74 | 2785 | if (ret >= 0 && check_reissue) |
889fca73 | 2786 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2787 | else |
2788 | io_rw_done(kiocb, ret); | |
97284637 | 2789 | |
fe7e3257 | 2790 | if (check_reissue && (req->flags & REQ_F_REISSUE)) { |
97284637 | 2791 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2792 | if (io_resubmit_prep(req)) { |
8c130827 | 2793 | req_ref_get(req); |
773af691 | 2794 | io_req_task_queue_reissue(req); |
8c130827 | 2795 | } else { |
97284637 PB |
2796 | int cflags = 0; |
2797 | ||
93d2bcd2 | 2798 | req_set_fail(req); |
97284637 PB |
2799 | if (req->flags & REQ_F_BUFFER_SELECTED) |
2800 | cflags = io_put_rw_kbuf(req); | |
2801 | __io_req_complete(req, issue_flags, ret, cflags); | |
2802 | } | |
2803 | } | |
ba816ad6 JA |
2804 | } |
2805 | ||
eae071c9 PB |
2806 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2807 | struct io_mapped_ubuf *imu) | |
edafccee | 2808 | { |
9adbd45d | 2809 | size_t len = req->rw.len; |
75769e3f | 2810 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2811 | size_t offset; |
edafccee | 2812 | |
75769e3f | 2813 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2814 | return -EFAULT; |
2815 | /* not inside the mapped region */ | |
4751f53d | 2816 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2817 | return -EFAULT; |
2818 | ||
2819 | /* | |
2820 | * May not be a start of buffer, set size appropriately | |
2821 | * and advance us to the beginning. | |
2822 | */ | |
2823 | offset = buf_addr - imu->ubuf; | |
2824 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2825 | |
2826 | if (offset) { | |
2827 | /* | |
2828 | * Don't use iov_iter_advance() here, as it's really slow for | |
2829 | * using the latter parts of a big fixed buffer - it iterates | |
2830 | * over each segment manually. We can cheat a bit here, because | |
2831 | * we know that: | |
2832 | * | |
2833 | * 1) it's a BVEC iter, we set it up | |
2834 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2835 | * first and last bvec | |
2836 | * | |
2837 | * So just find our index, and adjust the iterator afterwards. | |
2838 | * If the offset is within the first bvec (or the whole first | |
2839 | * bvec, just use iov_iter_advance(). This makes it easier | |
2840 | * since we can just skip the first segment, which may not | |
2841 | * be PAGE_SIZE aligned. | |
2842 | */ | |
2843 | const struct bio_vec *bvec = imu->bvec; | |
2844 | ||
2845 | if (offset <= bvec->bv_len) { | |
2846 | iov_iter_advance(iter, offset); | |
2847 | } else { | |
2848 | unsigned long seg_skip; | |
2849 | ||
2850 | /* skip first vec */ | |
2851 | offset -= bvec->bv_len; | |
2852 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2853 | ||
2854 | iter->bvec = bvec + seg_skip; | |
2855 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2856 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2857 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2858 | } |
2859 | } | |
2860 | ||
847595de | 2861 | return 0; |
edafccee JA |
2862 | } |
2863 | ||
eae071c9 PB |
2864 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2865 | { | |
2866 | struct io_ring_ctx *ctx = req->ctx; | |
2867 | struct io_mapped_ubuf *imu = req->imu; | |
2868 | u16 index, buf_index = req->buf_index; | |
2869 | ||
2870 | if (likely(!imu)) { | |
2871 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2872 | return -EFAULT; | |
2873 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2874 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2875 | req->imu = imu; | |
2876 | } | |
2877 | return __io_import_fixed(req, rw, iter, imu); | |
2878 | } | |
2879 | ||
bcda7baa JA |
2880 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2881 | { | |
2882 | if (needs_lock) | |
2883 | mutex_unlock(&ctx->uring_lock); | |
2884 | } | |
2885 | ||
2886 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2887 | { | |
2888 | /* | |
2889 | * "Normal" inline submissions always hold the uring_lock, since we | |
2890 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2891 | * The only exception is when we've detached the request and issue it | |
2892 | * from an async worker thread, grab the lock for that case. | |
2893 | */ | |
2894 | if (needs_lock) | |
2895 | mutex_lock(&ctx->uring_lock); | |
2896 | } | |
2897 | ||
2898 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2899 | int bgid, struct io_buffer *kbuf, | |
2900 | bool needs_lock) | |
2901 | { | |
2902 | struct io_buffer *head; | |
2903 | ||
2904 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2905 | return kbuf; | |
2906 | ||
2907 | io_ring_submit_lock(req->ctx, needs_lock); | |
2908 | ||
2909 | lockdep_assert_held(&req->ctx->uring_lock); | |
2910 | ||
9e15c3a0 | 2911 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2912 | if (head) { |
2913 | if (!list_empty(&head->list)) { | |
2914 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2915 | list); | |
2916 | list_del(&kbuf->list); | |
2917 | } else { | |
2918 | kbuf = head; | |
9e15c3a0 | 2919 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2920 | } |
2921 | if (*len > kbuf->len) | |
2922 | *len = kbuf->len; | |
2923 | } else { | |
2924 | kbuf = ERR_PTR(-ENOBUFS); | |
2925 | } | |
2926 | ||
2927 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2928 | ||
2929 | return kbuf; | |
2930 | } | |
2931 | ||
4d954c25 JA |
2932 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2933 | bool needs_lock) | |
2934 | { | |
2935 | struct io_buffer *kbuf; | |
4f4eeba8 | 2936 | u16 bgid; |
4d954c25 JA |
2937 | |
2938 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2939 | bgid = req->buf_index; |
4d954c25 JA |
2940 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2941 | if (IS_ERR(kbuf)) | |
2942 | return kbuf; | |
2943 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2944 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2945 | return u64_to_user_ptr(kbuf->addr); | |
2946 | } | |
2947 | ||
2948 | #ifdef CONFIG_COMPAT | |
2949 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2950 | bool needs_lock) | |
2951 | { | |
2952 | struct compat_iovec __user *uiov; | |
2953 | compat_ssize_t clen; | |
2954 | void __user *buf; | |
2955 | ssize_t len; | |
2956 | ||
2957 | uiov = u64_to_user_ptr(req->rw.addr); | |
2958 | if (!access_ok(uiov, sizeof(*uiov))) | |
2959 | return -EFAULT; | |
2960 | if (__get_user(clen, &uiov->iov_len)) | |
2961 | return -EFAULT; | |
2962 | if (clen < 0) | |
2963 | return -EINVAL; | |
2964 | ||
2965 | len = clen; | |
2966 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2967 | if (IS_ERR(buf)) | |
2968 | return PTR_ERR(buf); | |
2969 | iov[0].iov_base = buf; | |
2970 | iov[0].iov_len = (compat_size_t) len; | |
2971 | return 0; | |
2972 | } | |
2973 | #endif | |
2974 | ||
2975 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2976 | bool needs_lock) | |
2977 | { | |
2978 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2979 | void __user *buf; | |
2980 | ssize_t len; | |
2981 | ||
2982 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
2983 | return -EFAULT; | |
2984 | ||
2985 | len = iov[0].iov_len; | |
2986 | if (len < 0) | |
2987 | return -EINVAL; | |
2988 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2989 | if (IS_ERR(buf)) | |
2990 | return PTR_ERR(buf); | |
2991 | iov[0].iov_base = buf; | |
2992 | iov[0].iov_len = len; | |
2993 | return 0; | |
2994 | } | |
2995 | ||
2996 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2997 | bool needs_lock) | |
2998 | { | |
dddb3e26 JA |
2999 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3000 | struct io_buffer *kbuf; | |
3001 | ||
3002 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3003 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3004 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3005 | return 0; |
dddb3e26 | 3006 | } |
dd201662 | 3007 | if (req->rw.len != 1) |
4d954c25 JA |
3008 | return -EINVAL; |
3009 | ||
3010 | #ifdef CONFIG_COMPAT | |
3011 | if (req->ctx->compat) | |
3012 | return io_compat_import(req, iov, needs_lock); | |
3013 | #endif | |
3014 | ||
3015 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3016 | } | |
3017 | ||
847595de PB |
3018 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3019 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3020 | { |
9adbd45d JA |
3021 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3022 | size_t sqe_len = req->rw.len; | |
847595de | 3023 | u8 opcode = req->opcode; |
4d954c25 | 3024 | ssize_t ret; |
edafccee | 3025 | |
7d009165 | 3026 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3027 | *iovec = NULL; |
9adbd45d | 3028 | return io_import_fixed(req, rw, iter); |
edafccee | 3029 | } |
2b188cc1 | 3030 | |
bcda7baa | 3031 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3032 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3033 | return -EINVAL; |
3034 | ||
3a6820f2 | 3035 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3036 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3037 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3038 | if (IS_ERR(buf)) |
4d954c25 | 3039 | return PTR_ERR(buf); |
3f9d6441 | 3040 | req->rw.len = sqe_len; |
bcda7baa JA |
3041 | } |
3042 | ||
3a6820f2 JA |
3043 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3044 | *iovec = NULL; | |
10fc72e4 | 3045 | return ret; |
3a6820f2 JA |
3046 | } |
3047 | ||
4d954c25 JA |
3048 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3049 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3050 | if (!ret) |
3051 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3052 | *iovec = NULL; |
3053 | return ret; | |
3054 | } | |
3055 | ||
89cd35c5 CH |
3056 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3057 | req->ctx->compat); | |
2b188cc1 JA |
3058 | } |
3059 | ||
0fef9483 JA |
3060 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3061 | { | |
5b09e37e | 3062 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3063 | } |
3064 | ||
31b51510 | 3065 | /* |
32960613 JA |
3066 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3067 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3068 | */ |
4017eb91 | 3069 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3070 | { |
4017eb91 JA |
3071 | struct kiocb *kiocb = &req->rw.kiocb; |
3072 | struct file *file = req->file; | |
32960613 JA |
3073 | ssize_t ret = 0; |
3074 | ||
3075 | /* | |
3076 | * Don't support polled IO through this interface, and we can't | |
3077 | * support non-blocking either. For the latter, this just causes | |
3078 | * the kiocb to be handled from an async context. | |
3079 | */ | |
3080 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3081 | return -EOPNOTSUPP; | |
3082 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3083 | return -EAGAIN; | |
3084 | ||
3085 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3086 | struct iovec iovec; |
32960613 JA |
3087 | ssize_t nr; |
3088 | ||
311ae9e1 PB |
3089 | if (!iov_iter_is_bvec(iter)) { |
3090 | iovec = iov_iter_iovec(iter); | |
3091 | } else { | |
4017eb91 JA |
3092 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3093 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3094 | } |
3095 | ||
32960613 JA |
3096 | if (rw == READ) { |
3097 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3098 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3099 | } else { |
3100 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3101 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3102 | } |
3103 | ||
3104 | if (nr < 0) { | |
3105 | if (!ret) | |
3106 | ret = nr; | |
3107 | break; | |
3108 | } | |
3109 | ret += nr; | |
3110 | if (nr != iovec.iov_len) | |
3111 | break; | |
4017eb91 JA |
3112 | req->rw.len -= nr; |
3113 | req->rw.addr += nr; | |
32960613 JA |
3114 | iov_iter_advance(iter, nr); |
3115 | } | |
3116 | ||
3117 | return ret; | |
3118 | } | |
3119 | ||
ff6165b2 JA |
3120 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3121 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3122 | { |
e8c2bc1f | 3123 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3124 | |
ff6165b2 | 3125 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3126 | rw->free_iovec = iovec; |
227c0c96 | 3127 | rw->bytes_done = 0; |
ff6165b2 | 3128 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3129 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3130 | return; |
b64e3444 | 3131 | if (!iovec) { |
ff6165b2 JA |
3132 | unsigned iov_off = 0; |
3133 | ||
3134 | rw->iter.iov = rw->fast_iov; | |
3135 | if (iter->iov != fast_iov) { | |
3136 | iov_off = iter->iov - fast_iov; | |
3137 | rw->iter.iov += iov_off; | |
3138 | } | |
3139 | if (rw->fast_iov != fast_iov) | |
3140 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3141 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3142 | } else { |
3143 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3144 | } |
3145 | } | |
3146 | ||
6cb78689 | 3147 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3148 | { |
e8c2bc1f JA |
3149 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3150 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3151 | return req->async_data == NULL; | |
3d9932a8 XW |
3152 | } |
3153 | ||
ff6165b2 JA |
3154 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3155 | const struct iovec *fast_iov, | |
227c0c96 | 3156 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3157 | { |
26f0505a | 3158 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3159 | return 0; |
e8c2bc1f | 3160 | if (!req->async_data) { |
6cb78689 | 3161 | if (io_alloc_async_data(req)) { |
6bf985dc | 3162 | kfree(iovec); |
5d204bcf | 3163 | return -ENOMEM; |
6bf985dc | 3164 | } |
b7bb4f7d | 3165 | |
ff6165b2 | 3166 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3167 | } |
b7bb4f7d | 3168 | return 0; |
f67676d1 JA |
3169 | } |
3170 | ||
73debe68 | 3171 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3172 | { |
e8c2bc1f | 3173 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3174 | struct iovec *iov = iorw->fast_iov; |
847595de | 3175 | int ret; |
c3e330a4 | 3176 | |
2846c481 | 3177 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3178 | if (unlikely(ret < 0)) |
3179 | return ret; | |
3180 | ||
ab0b196c PB |
3181 | iorw->bytes_done = 0; |
3182 | iorw->free_iovec = iov; | |
3183 | if (iov) | |
3184 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3185 | return 0; |
3186 | } | |
3187 | ||
73debe68 | 3188 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3189 | { |
3529d8c2 JA |
3190 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3191 | return -EBADF; | |
93642ef8 | 3192 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3193 | } |
3194 | ||
c1dd91d1 JA |
3195 | /* |
3196 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3197 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3198 | * This gets called when the page is unlocked, and we generally expect that to | |
3199 | * happen when the page IO is completed and the page is now uptodate. This will | |
3200 | * queue a task_work based retry of the operation, attempting to copy the data | |
3201 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3202 | * do a thread based blocking retry of the operation. That's the unexpected | |
3203 | * slow path. | |
3204 | */ | |
bcf5a063 JA |
3205 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3206 | int sync, void *arg) | |
3207 | { | |
3208 | struct wait_page_queue *wpq; | |
3209 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3210 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3211 | |
3212 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3213 | ||
cdc8fcb4 LT |
3214 | if (!wake_page_match(wpq, key)) |
3215 | return 0; | |
3216 | ||
c8d317aa | 3217 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 JA |
3218 | list_del_init(&wait->entry); |
3219 | ||
bcf5a063 | 3220 | /* submit ref gets dropped, acquire a new one */ |
de9b4cca | 3221 | req_ref_get(req); |
921b9054 | 3222 | io_req_task_queue(req); |
bcf5a063 JA |
3223 | return 1; |
3224 | } | |
3225 | ||
c1dd91d1 JA |
3226 | /* |
3227 | * This controls whether a given IO request should be armed for async page | |
3228 | * based retry. If we return false here, the request is handed to the async | |
3229 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3230 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3231 | * will either succeed because the page is now uptodate and unlocked, or it | |
3232 | * will register a callback when the page is unlocked at IO completion. Through | |
3233 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3234 | * That retry will attempt the buffered read again. The retry will generally | |
3235 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3236 | * async worker threads for a blocking retry. | |
3237 | */ | |
227c0c96 | 3238 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3239 | { |
e8c2bc1f JA |
3240 | struct io_async_rw *rw = req->async_data; |
3241 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3242 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3243 | |
bcf5a063 JA |
3244 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3245 | if (req->flags & REQ_F_NOWAIT) | |
3246 | return false; | |
f67676d1 | 3247 | |
227c0c96 | 3248 | /* Only for buffered IO */ |
3b2a4439 | 3249 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3250 | return false; |
3b2a4439 | 3251 | |
bcf5a063 JA |
3252 | /* |
3253 | * just use poll if we can, and don't attempt if the fs doesn't | |
3254 | * support callback based unlocks | |
3255 | */ | |
3256 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3257 | return false; | |
f67676d1 | 3258 | |
3b2a4439 JA |
3259 | wait->wait.func = io_async_buf_func; |
3260 | wait->wait.private = req; | |
3261 | wait->wait.flags = 0; | |
3262 | INIT_LIST_HEAD(&wait->wait.entry); | |
3263 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3264 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3265 | kiocb->ki_waitq = wait; |
3b2a4439 | 3266 | return true; |
bcf5a063 JA |
3267 | } |
3268 | ||
aeab9506 | 3269 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3270 | { |
3271 | if (req->file->f_op->read_iter) | |
3272 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3273 | else if (req->file->f_op->read) |
4017eb91 | 3274 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3275 | else |
3276 | return -EINVAL; | |
f67676d1 JA |
3277 | } |
3278 | ||
889fca73 | 3279 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3280 | { |
3281 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3282 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3283 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3284 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3285 | ssize_t io_size, ret, ret2; |
45d189c6 | 3286 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3287 | |
2846c481 | 3288 | if (rw) { |
e8c2bc1f | 3289 | iter = &rw->iter; |
2846c481 PB |
3290 | iovec = NULL; |
3291 | } else { | |
3292 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3293 | if (ret < 0) | |
3294 | return ret; | |
3295 | } | |
632546c4 | 3296 | io_size = iov_iter_count(iter); |
fa15bafb | 3297 | req->result = io_size; |
2b188cc1 | 3298 | |
fd6c2e4c JA |
3299 | /* Ensure we clear previously set non-block flag */ |
3300 | if (!force_nonblock) | |
29de5f6a | 3301 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3302 | else |
3303 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3304 | ||
24c74678 | 3305 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3306 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3307 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3308 | return ret ?: -EAGAIN; |
6713e7a6 | 3309 | } |
9e645e11 | 3310 | |
632546c4 | 3311 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3312 | if (unlikely(ret)) { |
3313 | kfree(iovec); | |
3314 | return ret; | |
3315 | } | |
2b188cc1 | 3316 | |
227c0c96 | 3317 | ret = io_iter_do_read(req, iter); |
32960613 | 3318 | |
230d50d4 | 3319 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3320 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3321 | /* IOPOLL retry should happen for io-wq threads */ |
3322 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3323 | goto done; |
75c668cd PB |
3324 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3325 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3326 | goto done; |
84216315 | 3327 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3328 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3329 | ret = 0; |
230d50d4 JA |
3330 | } else if (ret == -EIOCBQUEUED) { |
3331 | goto out_free; | |
7335e3bf | 3332 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3333 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3334 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3335 | goto done; |
227c0c96 JA |
3336 | } |
3337 | ||
227c0c96 | 3338 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3339 | if (ret2) |
3340 | return ret2; | |
3341 | ||
fe1cdd55 | 3342 | iovec = NULL; |
e8c2bc1f | 3343 | rw = req->async_data; |
227c0c96 | 3344 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3345 | iter = &rw->iter; |
227c0c96 | 3346 | |
b23df91b PB |
3347 | do { |
3348 | io_size -= ret; | |
3349 | rw->bytes_done += ret; | |
3350 | /* if we can retry, do so with the callbacks armed */ | |
3351 | if (!io_rw_should_retry(req)) { | |
3352 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3353 | return -EAGAIN; | |
3354 | } | |
3355 | ||
3356 | /* | |
3357 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3358 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3359 | * desired page gets unlocked. We can also get a partial read | |
3360 | * here, and if we do, then just retry at the new offset. | |
3361 | */ | |
3362 | ret = io_iter_do_read(req, iter); | |
3363 | if (ret == -EIOCBQUEUED) | |
3364 | return 0; | |
227c0c96 | 3365 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3366 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3367 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3368 | done: |
889fca73 | 3369 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3370 | out_free: |
3371 | /* it's faster to check here then delegate to kfree */ | |
3372 | if (iovec) | |
3373 | kfree(iovec); | |
5ea5dd45 | 3374 | return 0; |
2b188cc1 JA |
3375 | } |
3376 | ||
73debe68 | 3377 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3378 | { |
3529d8c2 JA |
3379 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3380 | return -EBADF; | |
93642ef8 | 3381 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3382 | } |
3383 | ||
889fca73 | 3384 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3385 | { |
3386 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3387 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3388 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3389 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3390 | ssize_t ret, ret2, io_size; |
45d189c6 | 3391 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3392 | |
2846c481 | 3393 | if (rw) { |
e8c2bc1f | 3394 | iter = &rw->iter; |
2846c481 PB |
3395 | iovec = NULL; |
3396 | } else { | |
3397 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3398 | if (ret < 0) | |
3399 | return ret; | |
3400 | } | |
632546c4 | 3401 | io_size = iov_iter_count(iter); |
fa15bafb | 3402 | req->result = io_size; |
2b188cc1 | 3403 | |
fd6c2e4c JA |
3404 | /* Ensure we clear previously set non-block flag */ |
3405 | if (!force_nonblock) | |
a88fc400 PB |
3406 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3407 | else | |
3408 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3409 | |
24c74678 | 3410 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3411 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3412 | goto copy_iov; |
31b51510 | 3413 | |
10d59345 JA |
3414 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3415 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3416 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3417 | goto copy_iov; |
31b51510 | 3418 | |
632546c4 | 3419 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3420 | if (unlikely(ret)) |
3421 | goto out_free; | |
4ed734b0 | 3422 | |
fa15bafb PB |
3423 | /* |
3424 | * Open-code file_start_write here to grab freeze protection, | |
3425 | * which will be released by another thread in | |
3426 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3427 | * released so that it doesn't complain about the held lock when | |
3428 | * we return to userspace. | |
3429 | */ | |
3430 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3431 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3432 | __sb_writers_release(file_inode(req->file)->i_sb, |
3433 | SB_FREEZE_WRITE); | |
3434 | } | |
3435 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3436 | |
fa15bafb | 3437 | if (req->file->f_op->write_iter) |
ff6165b2 | 3438 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3439 | else if (req->file->f_op->write) |
4017eb91 | 3440 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3441 | else |
3442 | ret2 = -EINVAL; | |
4ed734b0 | 3443 | |
6ad7f233 PB |
3444 | if (req->flags & REQ_F_REISSUE) { |
3445 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3446 | ret2 = -EAGAIN; |
6ad7f233 | 3447 | } |
230d50d4 | 3448 | |
fa15bafb PB |
3449 | /* |
3450 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3451 | * retry them without IOCB_NOWAIT. | |
3452 | */ | |
3453 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3454 | ret2 = -EAGAIN; | |
75c668cd PB |
3455 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3456 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3457 | goto done; |
fa15bafb | 3458 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3459 | /* IOPOLL retry should happen for io-wq threads */ |
3460 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3461 | goto copy_iov; | |
355afaeb | 3462 | done: |
889fca73 | 3463 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3464 | } else { |
f67676d1 | 3465 | copy_iov: |
84216315 | 3466 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3467 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3468 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3469 | return ret ?: -EAGAIN; |
2b188cc1 | 3470 | } |
31b51510 | 3471 | out_free: |
f261c168 | 3472 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3473 | if (iovec) |
6f2cc166 | 3474 | kfree(iovec); |
2b188cc1 JA |
3475 | return ret; |
3476 | } | |
3477 | ||
80a261fd JA |
3478 | static int io_renameat_prep(struct io_kiocb *req, |
3479 | const struct io_uring_sqe *sqe) | |
3480 | { | |
3481 | struct io_rename *ren = &req->rename; | |
3482 | const char __user *oldf, *newf; | |
3483 | ||
ed7eb259 JA |
3484 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3485 | return -EINVAL; | |
3486 | if (sqe->ioprio || sqe->buf_index) | |
3487 | return -EINVAL; | |
80a261fd JA |
3488 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3489 | return -EBADF; | |
3490 | ||
3491 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3492 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3493 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3494 | ren->new_dfd = READ_ONCE(sqe->len); | |
3495 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3496 | ||
3497 | ren->oldpath = getname(oldf); | |
3498 | if (IS_ERR(ren->oldpath)) | |
3499 | return PTR_ERR(ren->oldpath); | |
3500 | ||
3501 | ren->newpath = getname(newf); | |
3502 | if (IS_ERR(ren->newpath)) { | |
3503 | putname(ren->oldpath); | |
3504 | return PTR_ERR(ren->newpath); | |
3505 | } | |
3506 | ||
3507 | req->flags |= REQ_F_NEED_CLEANUP; | |
3508 | return 0; | |
3509 | } | |
3510 | ||
45d189c6 | 3511 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3512 | { |
3513 | struct io_rename *ren = &req->rename; | |
3514 | int ret; | |
3515 | ||
45d189c6 | 3516 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3517 | return -EAGAIN; |
3518 | ||
3519 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3520 | ren->newpath, ren->flags); | |
3521 | ||
3522 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3523 | if (ret < 0) | |
93d2bcd2 | 3524 | req_set_fail(req); |
80a261fd JA |
3525 | io_req_complete(req, ret); |
3526 | return 0; | |
3527 | } | |
3528 | ||
14a1143b JA |
3529 | static int io_unlinkat_prep(struct io_kiocb *req, |
3530 | const struct io_uring_sqe *sqe) | |
3531 | { | |
3532 | struct io_unlink *un = &req->unlink; | |
3533 | const char __user *fname; | |
3534 | ||
22634bc5 JA |
3535 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3536 | return -EINVAL; | |
3537 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
3538 | return -EINVAL; | |
14a1143b JA |
3539 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3540 | return -EBADF; | |
3541 | ||
3542 | un->dfd = READ_ONCE(sqe->fd); | |
3543 | ||
3544 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3545 | if (un->flags & ~AT_REMOVEDIR) | |
3546 | return -EINVAL; | |
3547 | ||
3548 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3549 | un->filename = getname(fname); | |
3550 | if (IS_ERR(un->filename)) | |
3551 | return PTR_ERR(un->filename); | |
3552 | ||
3553 | req->flags |= REQ_F_NEED_CLEANUP; | |
3554 | return 0; | |
3555 | } | |
3556 | ||
45d189c6 | 3557 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3558 | { |
3559 | struct io_unlink *un = &req->unlink; | |
3560 | int ret; | |
3561 | ||
45d189c6 | 3562 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3563 | return -EAGAIN; |
3564 | ||
3565 | if (un->flags & AT_REMOVEDIR) | |
3566 | ret = do_rmdir(un->dfd, un->filename); | |
3567 | else | |
3568 | ret = do_unlinkat(un->dfd, un->filename); | |
3569 | ||
3570 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3571 | if (ret < 0) | |
93d2bcd2 | 3572 | req_set_fail(req); |
14a1143b JA |
3573 | io_req_complete(req, ret); |
3574 | return 0; | |
3575 | } | |
3576 | ||
36f4fa68 JA |
3577 | static int io_shutdown_prep(struct io_kiocb *req, |
3578 | const struct io_uring_sqe *sqe) | |
3579 | { | |
3580 | #if defined(CONFIG_NET) | |
3581 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3582 | return -EINVAL; | |
3583 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3584 | sqe->buf_index) | |
3585 | return -EINVAL; | |
3586 | ||
3587 | req->shutdown.how = READ_ONCE(sqe->len); | |
3588 | return 0; | |
3589 | #else | |
3590 | return -EOPNOTSUPP; | |
3591 | #endif | |
3592 | } | |
3593 | ||
45d189c6 | 3594 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3595 | { |
3596 | #if defined(CONFIG_NET) | |
3597 | struct socket *sock; | |
3598 | int ret; | |
3599 | ||
45d189c6 | 3600 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3601 | return -EAGAIN; |
3602 | ||
48aba79b | 3603 | sock = sock_from_file(req->file); |
36f4fa68 | 3604 | if (unlikely(!sock)) |
48aba79b | 3605 | return -ENOTSOCK; |
36f4fa68 JA |
3606 | |
3607 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3608 | if (ret < 0) |
93d2bcd2 | 3609 | req_set_fail(req); |
36f4fa68 JA |
3610 | io_req_complete(req, ret); |
3611 | return 0; | |
3612 | #else | |
3613 | return -EOPNOTSUPP; | |
3614 | #endif | |
3615 | } | |
3616 | ||
f2a8d5c7 PB |
3617 | static int __io_splice_prep(struct io_kiocb *req, |
3618 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3619 | { |
fe7e3257 | 3620 | struct io_splice *sp = &req->splice; |
7d67af2c | 3621 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3622 | |
3232dd02 PB |
3623 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3624 | return -EINVAL; | |
7d67af2c PB |
3625 | |
3626 | sp->file_in = NULL; | |
7d67af2c PB |
3627 | sp->len = READ_ONCE(sqe->len); |
3628 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3629 | ||
3630 | if (unlikely(sp->flags & ~valid_flags)) | |
3631 | return -EINVAL; | |
3632 | ||
ac177053 PB |
3633 | sp->file_in = io_file_get(req->ctx, NULL, req, |
3634 | READ_ONCE(sqe->splice_fd_in), | |
8371adf5 PB |
3635 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3636 | if (!sp->file_in) | |
3637 | return -EBADF; | |
7d67af2c | 3638 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3639 | return 0; |
3640 | } | |
3641 | ||
f2a8d5c7 PB |
3642 | static int io_tee_prep(struct io_kiocb *req, |
3643 | const struct io_uring_sqe *sqe) | |
3644 | { | |
3645 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3646 | return -EINVAL; | |
3647 | return __io_splice_prep(req, sqe); | |
3648 | } | |
3649 | ||
45d189c6 | 3650 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3651 | { |
3652 | struct io_splice *sp = &req->splice; | |
3653 | struct file *in = sp->file_in; | |
3654 | struct file *out = sp->file_out; | |
3655 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3656 | long ret = 0; | |
3657 | ||
45d189c6 | 3658 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3659 | return -EAGAIN; |
3660 | if (sp->len) | |
3661 | ret = do_tee(in, out, sp->len, flags); | |
3662 | ||
e1d767f0 PB |
3663 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3664 | io_put_file(in); | |
f2a8d5c7 PB |
3665 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3666 | ||
f2a8d5c7 | 3667 | if (ret != sp->len) |
93d2bcd2 | 3668 | req_set_fail(req); |
e1e16097 | 3669 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3670 | return 0; |
3671 | } | |
3672 | ||
3673 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3674 | { | |
fe7e3257 | 3675 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3676 | |
3677 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3678 | sp->off_out = READ_ONCE(sqe->off); | |
3679 | return __io_splice_prep(req, sqe); | |
3680 | } | |
3681 | ||
45d189c6 | 3682 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3683 | { |
3684 | struct io_splice *sp = &req->splice; | |
3685 | struct file *in = sp->file_in; | |
3686 | struct file *out = sp->file_out; | |
3687 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3688 | loff_t *poff_in, *poff_out; | |
c9687426 | 3689 | long ret = 0; |
7d67af2c | 3690 | |
45d189c6 | 3691 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3692 | return -EAGAIN; |
7d67af2c PB |
3693 | |
3694 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3695 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3696 | |
948a7749 | 3697 | if (sp->len) |
c9687426 | 3698 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3699 | |
e1d767f0 PB |
3700 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3701 | io_put_file(in); | |
7d67af2c PB |
3702 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3703 | ||
7d67af2c | 3704 | if (ret != sp->len) |
93d2bcd2 | 3705 | req_set_fail(req); |
e1e16097 | 3706 | io_req_complete(req, ret); |
7d67af2c PB |
3707 | return 0; |
3708 | } | |
3709 | ||
2b188cc1 JA |
3710 | /* |
3711 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3712 | */ | |
889fca73 | 3713 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3714 | { |
3715 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3716 | |
def596e9 JA |
3717 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3718 | return -EINVAL; | |
3719 | ||
889fca73 | 3720 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3721 | return 0; |
3722 | } | |
3723 | ||
1155c76a | 3724 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3725 | { |
6b06314c | 3726 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3727 | |
09bb8394 JA |
3728 | if (!req->file) |
3729 | return -EBADF; | |
c992fe29 | 3730 | |
6b06314c | 3731 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3732 | return -EINVAL; |
edafccee | 3733 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3734 | return -EINVAL; |
3735 | ||
8ed8d3c3 JA |
3736 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3737 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3738 | return -EINVAL; | |
3739 | ||
3740 | req->sync.off = READ_ONCE(sqe->off); | |
3741 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3742 | return 0; |
3743 | } | |
3744 | ||
45d189c6 | 3745 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3746 | { |
8ed8d3c3 | 3747 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3748 | int ret; |
3749 | ||
ac45abc0 | 3750 | /* fsync always requires a blocking context */ |
45d189c6 | 3751 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3752 | return -EAGAIN; |
3753 | ||
9adbd45d | 3754 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3755 | end > 0 ? end : LLONG_MAX, |
3756 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3757 | if (ret < 0) | |
93d2bcd2 | 3758 | req_set_fail(req); |
e1e16097 | 3759 | io_req_complete(req, ret); |
c992fe29 CH |
3760 | return 0; |
3761 | } | |
3762 | ||
d63d1b5e JA |
3763 | static int io_fallocate_prep(struct io_kiocb *req, |
3764 | const struct io_uring_sqe *sqe) | |
3765 | { | |
3766 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3767 | return -EINVAL; | |
3232dd02 PB |
3768 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3769 | return -EINVAL; | |
d63d1b5e JA |
3770 | |
3771 | req->sync.off = READ_ONCE(sqe->off); | |
3772 | req->sync.len = READ_ONCE(sqe->addr); | |
3773 | req->sync.mode = READ_ONCE(sqe->len); | |
3774 | return 0; | |
3775 | } | |
3776 | ||
45d189c6 | 3777 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3778 | { |
ac45abc0 PB |
3779 | int ret; |
3780 | ||
d63d1b5e | 3781 | /* fallocate always requiring blocking context */ |
45d189c6 | 3782 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3783 | return -EAGAIN; |
ac45abc0 PB |
3784 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3785 | req->sync.len); | |
ac45abc0 | 3786 | if (ret < 0) |
93d2bcd2 | 3787 | req_set_fail(req); |
e1e16097 | 3788 | io_req_complete(req, ret); |
5d17b4a4 JA |
3789 | return 0; |
3790 | } | |
3791 | ||
ec65fea5 | 3792 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3793 | { |
f8748881 | 3794 | const char __user *fname; |
15b71abe | 3795 | int ret; |
b7bb4f7d | 3796 | |
ec65fea5 | 3797 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3798 | return -EINVAL; |
ec65fea5 | 3799 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3800 | return -EBADF; |
03b1230c | 3801 | |
ec65fea5 PB |
3802 | /* open.how should be already initialised */ |
3803 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3804 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3805 | |
25e72d10 PB |
3806 | req->open.dfd = READ_ONCE(sqe->fd); |
3807 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3808 | req->open.filename = getname(fname); |
15b71abe JA |
3809 | if (IS_ERR(req->open.filename)) { |
3810 | ret = PTR_ERR(req->open.filename); | |
3811 | req->open.filename = NULL; | |
3812 | return ret; | |
3813 | } | |
4022e7af | 3814 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3815 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3816 | return 0; |
03b1230c JA |
3817 | } |
3818 | ||
ec65fea5 PB |
3819 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3820 | { | |
3821 | u64 flags, mode; | |
3822 | ||
14587a46 | 3823 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3824 | return -EINVAL; |
ec65fea5 PB |
3825 | mode = READ_ONCE(sqe->len); |
3826 | flags = READ_ONCE(sqe->open_flags); | |
3827 | req->open.how = build_open_how(flags, mode); | |
3828 | return __io_openat_prep(req, sqe); | |
3829 | } | |
3830 | ||
cebdb986 | 3831 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3832 | { |
cebdb986 | 3833 | struct open_how __user *how; |
cebdb986 | 3834 | size_t len; |
0fa03c62 JA |
3835 | int ret; |
3836 | ||
14587a46 | 3837 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4eb8dded | 3838 | return -EINVAL; |
cebdb986 JA |
3839 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3840 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3841 | if (len < OPEN_HOW_SIZE_VER0) |
3842 | return -EINVAL; | |
3529d8c2 | 3843 | |
cebdb986 JA |
3844 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3845 | len); | |
3846 | if (ret) | |
3847 | return ret; | |
3529d8c2 | 3848 | |
ec65fea5 | 3849 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3850 | } |
3851 | ||
45d189c6 | 3852 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3853 | { |
3854 | struct open_flags op; | |
15b71abe | 3855 | struct file *file; |
3a81fd02 JA |
3856 | bool nonblock_set; |
3857 | bool resolve_nonblock; | |
15b71abe JA |
3858 | int ret; |
3859 | ||
cebdb986 | 3860 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3861 | if (ret) |
3862 | goto err; | |
3a81fd02 JA |
3863 | nonblock_set = op.open_flag & O_NONBLOCK; |
3864 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3865 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3866 | /* |
3867 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3868 | * it'll always -EAGAIN | |
3869 | */ | |
3870 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3871 | return -EAGAIN; | |
3872 | op.lookup_flags |= LOOKUP_CACHED; | |
3873 | op.open_flag |= O_NONBLOCK; | |
3874 | } | |
15b71abe | 3875 | |
4022e7af | 3876 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3877 | if (ret < 0) |
3878 | goto err; | |
3879 | ||
3880 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 3881 | if (IS_ERR(file)) { |
944d1444 | 3882 | /* |
12dcb58a PB |
3883 | * We could hang on to this 'fd' on retrying, but seems like |
3884 | * marginal gain for something that is now known to be a slower | |
3885 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 3886 | */ |
3a81fd02 | 3887 | put_unused_fd(ret); |
3a81fd02 | 3888 | |
15b71abe | 3889 | ret = PTR_ERR(file); |
12dcb58a PB |
3890 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
3891 | if (ret == -EAGAIN && | |
3892 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
3893 | return -EAGAIN; | |
3894 | goto err; | |
15b71abe | 3895 | } |
12dcb58a PB |
3896 | |
3897 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
3898 | file->f_flags &= ~O_NONBLOCK; | |
3899 | fsnotify_open(file); | |
3900 | fd_install(ret, file); | |
15b71abe JA |
3901 | err: |
3902 | putname(req->open.filename); | |
8fef80bf | 3903 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 3904 | if (ret < 0) |
93d2bcd2 | 3905 | req_set_fail(req); |
0bdf3398 | 3906 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
3907 | return 0; |
3908 | } | |
3909 | ||
45d189c6 | 3910 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3911 | { |
e45cff58 | 3912 | return io_openat2(req, issue_flags); |
cebdb986 JA |
3913 | } |
3914 | ||
067524e9 JA |
3915 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3916 | const struct io_uring_sqe *sqe) | |
3917 | { | |
3918 | struct io_provide_buf *p = &req->pbuf; | |
3919 | u64 tmp; | |
3920 | ||
3921 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3922 | return -EINVAL; | |
3923 | ||
3924 | tmp = READ_ONCE(sqe->fd); | |
3925 | if (!tmp || tmp > USHRT_MAX) | |
3926 | return -EINVAL; | |
3927 | ||
3928 | memset(p, 0, sizeof(*p)); | |
3929 | p->nbufs = tmp; | |
3930 | p->bgid = READ_ONCE(sqe->buf_group); | |
3931 | return 0; | |
3932 | } | |
3933 | ||
3934 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3935 | int bgid, unsigned nbufs) | |
3936 | { | |
3937 | unsigned i = 0; | |
3938 | ||
3939 | /* shouldn't happen */ | |
3940 | if (!nbufs) | |
3941 | return 0; | |
3942 | ||
3943 | /* the head kbuf is the list itself */ | |
3944 | while (!list_empty(&buf->list)) { | |
3945 | struct io_buffer *nxt; | |
3946 | ||
3947 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3948 | list_del(&nxt->list); | |
3949 | kfree(nxt); | |
3950 | if (++i == nbufs) | |
3951 | return i; | |
3952 | } | |
3953 | i++; | |
3954 | kfree(buf); | |
9e15c3a0 | 3955 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
3956 | |
3957 | return i; | |
3958 | } | |
3959 | ||
889fca73 | 3960 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3961 | { |
3962 | struct io_provide_buf *p = &req->pbuf; | |
3963 | struct io_ring_ctx *ctx = req->ctx; | |
3964 | struct io_buffer *head; | |
3965 | int ret = 0; | |
45d189c6 | 3966 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3967 | |
3968 | io_ring_submit_lock(ctx, !force_nonblock); | |
3969 | ||
3970 | lockdep_assert_held(&ctx->uring_lock); | |
3971 | ||
3972 | ret = -ENOENT; | |
9e15c3a0 | 3973 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
3974 | if (head) |
3975 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 3976 | if (ret < 0) |
93d2bcd2 | 3977 | req_set_fail(req); |
067524e9 | 3978 | |
9fb8cb49 PB |
3979 | /* complete before unlock, IOPOLL may need the lock */ |
3980 | __io_req_complete(req, issue_flags, ret, 0); | |
3981 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
3982 | return 0; |
3983 | } | |
3984 | ||
ddf0322d JA |
3985 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3986 | const struct io_uring_sqe *sqe) | |
3987 | { | |
38134ada | 3988 | unsigned long size, tmp_check; |
ddf0322d JA |
3989 | struct io_provide_buf *p = &req->pbuf; |
3990 | u64 tmp; | |
3991 | ||
3992 | if (sqe->ioprio || sqe->rw_flags) | |
3993 | return -EINVAL; | |
3994 | ||
3995 | tmp = READ_ONCE(sqe->fd); | |
3996 | if (!tmp || tmp > USHRT_MAX) | |
3997 | return -E2BIG; | |
3998 | p->nbufs = tmp; | |
3999 | p->addr = READ_ONCE(sqe->addr); | |
4000 | p->len = READ_ONCE(sqe->len); | |
4001 | ||
38134ada PB |
4002 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4003 | &size)) | |
4004 | return -EOVERFLOW; | |
4005 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4006 | return -EOVERFLOW; | |
4007 | ||
d81269fe PB |
4008 | size = (unsigned long)p->len * p->nbufs; |
4009 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4010 | return -EFAULT; |
4011 | ||
4012 | p->bgid = READ_ONCE(sqe->buf_group); | |
4013 | tmp = READ_ONCE(sqe->off); | |
4014 | if (tmp > USHRT_MAX) | |
4015 | return -E2BIG; | |
4016 | p->bid = tmp; | |
4017 | return 0; | |
4018 | } | |
4019 | ||
4020 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4021 | { | |
4022 | struct io_buffer *buf; | |
4023 | u64 addr = pbuf->addr; | |
4024 | int i, bid = pbuf->bid; | |
4025 | ||
4026 | for (i = 0; i < pbuf->nbufs; i++) { | |
4027 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4028 | if (!buf) | |
4029 | break; | |
4030 | ||
4031 | buf->addr = addr; | |
d1f82808 | 4032 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4033 | buf->bid = bid; |
4034 | addr += pbuf->len; | |
4035 | bid++; | |
4036 | if (!*head) { | |
4037 | INIT_LIST_HEAD(&buf->list); | |
4038 | *head = buf; | |
4039 | } else { | |
4040 | list_add_tail(&buf->list, &(*head)->list); | |
4041 | } | |
4042 | } | |
4043 | ||
4044 | return i ? i : -ENOMEM; | |
4045 | } | |
4046 | ||
889fca73 | 4047 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4048 | { |
4049 | struct io_provide_buf *p = &req->pbuf; | |
4050 | struct io_ring_ctx *ctx = req->ctx; | |
4051 | struct io_buffer *head, *list; | |
4052 | int ret = 0; | |
45d189c6 | 4053 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4054 | |
4055 | io_ring_submit_lock(ctx, !force_nonblock); | |
4056 | ||
4057 | lockdep_assert_held(&ctx->uring_lock); | |
4058 | ||
9e15c3a0 | 4059 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4060 | |
4061 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4062 | if (ret >= 0 && !list) { |
4063 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4064 | if (ret < 0) | |
067524e9 | 4065 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4066 | } |
ddf0322d | 4067 | if (ret < 0) |
93d2bcd2 | 4068 | req_set_fail(req); |
9fb8cb49 PB |
4069 | /* complete before unlock, IOPOLL may need the lock */ |
4070 | __io_req_complete(req, issue_flags, ret, 0); | |
4071 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4072 | return 0; |
cebdb986 JA |
4073 | } |
4074 | ||
3e4827b0 JA |
4075 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4076 | const struct io_uring_sqe *sqe) | |
4077 | { | |
4078 | #if defined(CONFIG_EPOLL) | |
4079 | if (sqe->ioprio || sqe->buf_index) | |
4080 | return -EINVAL; | |
2d74d042 | 4081 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4082 | return -EINVAL; |
3e4827b0 JA |
4083 | |
4084 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4085 | req->epoll.op = READ_ONCE(sqe->len); | |
4086 | req->epoll.fd = READ_ONCE(sqe->off); | |
4087 | ||
4088 | if (ep_op_has_event(req->epoll.op)) { | |
4089 | struct epoll_event __user *ev; | |
4090 | ||
4091 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4092 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4093 | return -EFAULT; | |
4094 | } | |
4095 | ||
4096 | return 0; | |
4097 | #else | |
4098 | return -EOPNOTSUPP; | |
4099 | #endif | |
4100 | } | |
4101 | ||
889fca73 | 4102 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4103 | { |
4104 | #if defined(CONFIG_EPOLL) | |
4105 | struct io_epoll *ie = &req->epoll; | |
4106 | int ret; | |
45d189c6 | 4107 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4108 | |
4109 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4110 | if (force_nonblock && ret == -EAGAIN) | |
4111 | return -EAGAIN; | |
4112 | ||
4113 | if (ret < 0) | |
93d2bcd2 | 4114 | req_set_fail(req); |
889fca73 | 4115 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4116 | return 0; |
4117 | #else | |
4118 | return -EOPNOTSUPP; | |
4119 | #endif | |
4120 | } | |
4121 | ||
c1ca757b JA |
4122 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4123 | { | |
4124 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4125 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4126 | return -EINVAL; | |
3232dd02 PB |
4127 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4128 | return -EINVAL; | |
c1ca757b JA |
4129 | |
4130 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4131 | req->madvise.len = READ_ONCE(sqe->len); | |
4132 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4133 | return 0; | |
4134 | #else | |
4135 | return -EOPNOTSUPP; | |
4136 | #endif | |
4137 | } | |
4138 | ||
45d189c6 | 4139 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4140 | { |
4141 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4142 | struct io_madvise *ma = &req->madvise; | |
4143 | int ret; | |
4144 | ||
45d189c6 | 4145 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4146 | return -EAGAIN; |
4147 | ||
0726b01e | 4148 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4149 | if (ret < 0) |
93d2bcd2 | 4150 | req_set_fail(req); |
e1e16097 | 4151 | io_req_complete(req, ret); |
c1ca757b JA |
4152 | return 0; |
4153 | #else | |
4154 | return -EOPNOTSUPP; | |
4155 | #endif | |
4156 | } | |
4157 | ||
4840e418 JA |
4158 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4159 | { | |
4160 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4161 | return -EINVAL; | |
3232dd02 PB |
4162 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4163 | return -EINVAL; | |
4840e418 JA |
4164 | |
4165 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4166 | req->fadvise.len = READ_ONCE(sqe->len); | |
4167 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4168 | return 0; | |
4169 | } | |
4170 | ||
45d189c6 | 4171 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4172 | { |
4173 | struct io_fadvise *fa = &req->fadvise; | |
4174 | int ret; | |
4175 | ||
45d189c6 | 4176 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4177 | switch (fa->advice) { |
4178 | case POSIX_FADV_NORMAL: | |
4179 | case POSIX_FADV_RANDOM: | |
4180 | case POSIX_FADV_SEQUENTIAL: | |
4181 | break; | |
4182 | default: | |
4183 | return -EAGAIN; | |
4184 | } | |
4185 | } | |
4840e418 JA |
4186 | |
4187 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4188 | if (ret < 0) | |
93d2bcd2 | 4189 | req_set_fail(req); |
0bdf3398 | 4190 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4191 | return 0; |
4192 | } | |
4193 | ||
eddc7ef5 JA |
4194 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4195 | { | |
2d74d042 | 4196 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4197 | return -EINVAL; |
eddc7ef5 JA |
4198 | if (sqe->ioprio || sqe->buf_index) |
4199 | return -EINVAL; | |
9c280f90 | 4200 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4201 | return -EBADF; |
eddc7ef5 | 4202 | |
1d9e1288 BM |
4203 | req->statx.dfd = READ_ONCE(sqe->fd); |
4204 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4205 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4206 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4207 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4208 | |
4209 | return 0; | |
4210 | } | |
4211 | ||
45d189c6 | 4212 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4213 | { |
1d9e1288 | 4214 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4215 | int ret; |
4216 | ||
59d70013 | 4217 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4218 | return -EAGAIN; |
4219 | ||
e62753e4 BM |
4220 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4221 | ctx->buffer); | |
eddc7ef5 | 4222 | |
eddc7ef5 | 4223 | if (ret < 0) |
93d2bcd2 | 4224 | req_set_fail(req); |
e1e16097 | 4225 | io_req_complete(req, ret); |
eddc7ef5 JA |
4226 | return 0; |
4227 | } | |
4228 | ||
b5dba59e JA |
4229 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4230 | { | |
14587a46 | 4231 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4232 | return -EINVAL; |
b5dba59e JA |
4233 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4234 | sqe->rw_flags || sqe->buf_index) | |
4235 | return -EINVAL; | |
9c280f90 | 4236 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4237 | return -EBADF; |
b5dba59e JA |
4238 | |
4239 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4240 | return 0; |
b5dba59e JA |
4241 | } |
4242 | ||
889fca73 | 4243 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4244 | { |
9eac1904 | 4245 | struct files_struct *files = current->files; |
3af73b28 | 4246 | struct io_close *close = &req->close; |
9eac1904 | 4247 | struct fdtable *fdt; |
a1fde923 PB |
4248 | struct file *file = NULL; |
4249 | int ret = -EBADF; | |
b5dba59e | 4250 | |
9eac1904 JA |
4251 | spin_lock(&files->file_lock); |
4252 | fdt = files_fdtable(files); | |
4253 | if (close->fd >= fdt->max_fds) { | |
4254 | spin_unlock(&files->file_lock); | |
4255 | goto err; | |
4256 | } | |
4257 | file = fdt->fd[close->fd]; | |
a1fde923 | 4258 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4259 | spin_unlock(&files->file_lock); |
4260 | file = NULL; | |
4261 | goto err; | |
3af73b28 | 4262 | } |
b5dba59e JA |
4263 | |
4264 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4265 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4266 | spin_unlock(&files->file_lock); |
0bf0eefd | 4267 | return -EAGAIN; |
a2100672 | 4268 | } |
b5dba59e | 4269 | |
9eac1904 JA |
4270 | ret = __close_fd_get_file(close->fd, &file); |
4271 | spin_unlock(&files->file_lock); | |
4272 | if (ret < 0) { | |
4273 | if (ret == -ENOENT) | |
4274 | ret = -EBADF; | |
4275 | goto err; | |
4276 | } | |
4277 | ||
3af73b28 | 4278 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4279 | ret = filp_close(file, current->files); |
4280 | err: | |
3af73b28 | 4281 | if (ret < 0) |
93d2bcd2 | 4282 | req_set_fail(req); |
9eac1904 JA |
4283 | if (file) |
4284 | fput(file); | |
889fca73 | 4285 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4286 | return 0; |
b5dba59e JA |
4287 | } |
4288 | ||
1155c76a | 4289 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4290 | { |
4291 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4292 | |
5d17b4a4 JA |
4293 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4294 | return -EINVAL; | |
4295 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4296 | return -EINVAL; | |
4297 | ||
8ed8d3c3 JA |
4298 | req->sync.off = READ_ONCE(sqe->off); |
4299 | req->sync.len = READ_ONCE(sqe->len); | |
4300 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4301 | return 0; |
4302 | } | |
4303 | ||
45d189c6 | 4304 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4305 | { |
8ed8d3c3 JA |
4306 | int ret; |
4307 | ||
ac45abc0 | 4308 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4309 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4310 | return -EAGAIN; |
4311 | ||
9adbd45d | 4312 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4313 | req->sync.flags); |
4314 | if (ret < 0) | |
93d2bcd2 | 4315 | req_set_fail(req); |
e1e16097 | 4316 | io_req_complete(req, ret); |
5d17b4a4 JA |
4317 | return 0; |
4318 | } | |
4319 | ||
469956e8 | 4320 | #if defined(CONFIG_NET) |
02d27d89 PB |
4321 | static int io_setup_async_msg(struct io_kiocb *req, |
4322 | struct io_async_msghdr *kmsg) | |
4323 | { | |
e8c2bc1f JA |
4324 | struct io_async_msghdr *async_msg = req->async_data; |
4325 | ||
4326 | if (async_msg) | |
02d27d89 | 4327 | return -EAGAIN; |
e8c2bc1f | 4328 | if (io_alloc_async_data(req)) { |
257e84a5 | 4329 | kfree(kmsg->free_iov); |
02d27d89 PB |
4330 | return -ENOMEM; |
4331 | } | |
e8c2bc1f | 4332 | async_msg = req->async_data; |
02d27d89 | 4333 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4334 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4335 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4336 | /* if were using fast_iov, set it to the new one */ |
4337 | if (!async_msg->free_iov) | |
4338 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4339 | ||
02d27d89 PB |
4340 | return -EAGAIN; |
4341 | } | |
4342 | ||
2ae523ed PB |
4343 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4344 | struct io_async_msghdr *iomsg) | |
4345 | { | |
2ae523ed | 4346 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4347 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4348 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4349 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4350 | } |
4351 | ||
93642ef8 PB |
4352 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4353 | { | |
4354 | int ret; | |
4355 | ||
93642ef8 PB |
4356 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4357 | if (!ret) | |
4358 | req->flags |= REQ_F_NEED_CLEANUP; | |
4359 | return ret; | |
4360 | } | |
4361 | ||
3529d8c2 | 4362 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4363 | { |
e47293fd | 4364 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4365 | |
d2b6f48b PB |
4366 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4367 | return -EINVAL; | |
4368 | ||
270a5940 | 4369 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4370 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4371 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4372 | if (sr->msg_flags & MSG_DONTWAIT) | |
4373 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4374 | |
d8768362 JA |
4375 | #ifdef CONFIG_COMPAT |
4376 | if (req->ctx->compat) | |
4377 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4378 | #endif | |
93642ef8 | 4379 | return 0; |
03b1230c JA |
4380 | } |
4381 | ||
889fca73 | 4382 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4383 | { |
6b754c8b | 4384 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4385 | struct socket *sock; |
7a7cacba | 4386 | unsigned flags; |
0031275d | 4387 | int min_ret = 0; |
0fa03c62 JA |
4388 | int ret; |
4389 | ||
dba4a925 | 4390 | sock = sock_from_file(req->file); |
7a7cacba | 4391 | if (unlikely(!sock)) |
dba4a925 | 4392 | return -ENOTSOCK; |
3529d8c2 | 4393 | |
257e84a5 PB |
4394 | kmsg = req->async_data; |
4395 | if (!kmsg) { | |
7a7cacba PB |
4396 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4397 | if (ret) | |
4398 | return ret; | |
4399 | kmsg = &iomsg; | |
0fa03c62 | 4400 | } |
0fa03c62 | 4401 | |
04411806 PB |
4402 | flags = req->sr_msg.msg_flags; |
4403 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4404 | flags |= MSG_DONTWAIT; |
0031275d SM |
4405 | if (flags & MSG_WAITALL) |
4406 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4407 | ||
7a7cacba | 4408 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4409 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4410 | return io_setup_async_msg(req, kmsg); |
4411 | if (ret == -ERESTARTSYS) | |
4412 | ret = -EINTR; | |
0fa03c62 | 4413 | |
257e84a5 PB |
4414 | /* fast path, check for non-NULL to avoid function call */ |
4415 | if (kmsg->free_iov) | |
4416 | kfree(kmsg->free_iov); | |
99bc4c38 | 4417 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4418 | if (ret < min_ret) |
93d2bcd2 | 4419 | req_set_fail(req); |
889fca73 | 4420 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4421 | return 0; |
03b1230c | 4422 | } |
aa1fa28f | 4423 | |
889fca73 | 4424 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4425 | { |
7a7cacba PB |
4426 | struct io_sr_msg *sr = &req->sr_msg; |
4427 | struct msghdr msg; | |
4428 | struct iovec iov; | |
fddaface | 4429 | struct socket *sock; |
7a7cacba | 4430 | unsigned flags; |
0031275d | 4431 | int min_ret = 0; |
fddaface JA |
4432 | int ret; |
4433 | ||
dba4a925 | 4434 | sock = sock_from_file(req->file); |
7a7cacba | 4435 | if (unlikely(!sock)) |
dba4a925 | 4436 | return -ENOTSOCK; |
fddaface | 4437 | |
7a7cacba PB |
4438 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4439 | if (unlikely(ret)) | |
14db8411 | 4440 | return ret; |
fddaface | 4441 | |
7a7cacba PB |
4442 | msg.msg_name = NULL; |
4443 | msg.msg_control = NULL; | |
4444 | msg.msg_controllen = 0; | |
4445 | msg.msg_namelen = 0; | |
fddaface | 4446 | |
04411806 PB |
4447 | flags = req->sr_msg.msg_flags; |
4448 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4449 | flags |= MSG_DONTWAIT; |
0031275d SM |
4450 | if (flags & MSG_WAITALL) |
4451 | min_ret = iov_iter_count(&msg.msg_iter); | |
4452 | ||
7a7cacba PB |
4453 | msg.msg_flags = flags; |
4454 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4455 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4456 | return -EAGAIN; |
4457 | if (ret == -ERESTARTSYS) | |
4458 | ret = -EINTR; | |
fddaface | 4459 | |
0031275d | 4460 | if (ret < min_ret) |
93d2bcd2 | 4461 | req_set_fail(req); |
889fca73 | 4462 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4463 | return 0; |
fddaface JA |
4464 | } |
4465 | ||
1400e697 PB |
4466 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4467 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4468 | { |
4469 | struct io_sr_msg *sr = &req->sr_msg; | |
4470 | struct iovec __user *uiov; | |
4471 | size_t iov_len; | |
4472 | int ret; | |
4473 | ||
1400e697 PB |
4474 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4475 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4476 | if (ret) |
4477 | return ret; | |
4478 | ||
4479 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4480 | if (iov_len > 1) | |
4481 | return -EINVAL; | |
5476dfed | 4482 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4483 | return -EFAULT; |
5476dfed | 4484 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4485 | iomsg->free_iov = NULL; |
52de1fe1 | 4486 | } else { |
257e84a5 | 4487 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4488 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4489 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4490 | false); |
52de1fe1 JA |
4491 | if (ret > 0) |
4492 | ret = 0; | |
4493 | } | |
4494 | ||
4495 | return ret; | |
4496 | } | |
4497 | ||
4498 | #ifdef CONFIG_COMPAT | |
4499 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4500 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4501 | { |
52de1fe1 JA |
4502 | struct io_sr_msg *sr = &req->sr_msg; |
4503 | struct compat_iovec __user *uiov; | |
4504 | compat_uptr_t ptr; | |
4505 | compat_size_t len; | |
4506 | int ret; | |
4507 | ||
4af3417a PB |
4508 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4509 | &ptr, &len); | |
52de1fe1 JA |
4510 | if (ret) |
4511 | return ret; | |
4512 | ||
4513 | uiov = compat_ptr(ptr); | |
4514 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4515 | compat_ssize_t clen; | |
4516 | ||
4517 | if (len > 1) | |
4518 | return -EINVAL; | |
4519 | if (!access_ok(uiov, sizeof(*uiov))) | |
4520 | return -EFAULT; | |
4521 | if (__get_user(clen, &uiov->iov_len)) | |
4522 | return -EFAULT; | |
4523 | if (clen < 0) | |
4524 | return -EINVAL; | |
2d280bc8 | 4525 | sr->len = clen; |
257e84a5 | 4526 | iomsg->free_iov = NULL; |
52de1fe1 | 4527 | } else { |
257e84a5 | 4528 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4529 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4530 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4531 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4532 | if (ret < 0) |
4533 | return ret; | |
4534 | } | |
4535 | ||
4536 | return 0; | |
4537 | } | |
4538 | #endif | |
4539 | ||
1400e697 PB |
4540 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4541 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4542 | { |
1400e697 | 4543 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4544 | |
4545 | #ifdef CONFIG_COMPAT | |
4546 | if (req->ctx->compat) | |
1400e697 | 4547 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4548 | #endif |
52de1fe1 | 4549 | |
1400e697 | 4550 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4551 | } |
4552 | ||
bcda7baa | 4553 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4554 | bool needs_lock) |
bcda7baa JA |
4555 | { |
4556 | struct io_sr_msg *sr = &req->sr_msg; | |
4557 | struct io_buffer *kbuf; | |
4558 | ||
bcda7baa JA |
4559 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4560 | if (IS_ERR(kbuf)) | |
4561 | return kbuf; | |
4562 | ||
4563 | sr->kbuf = kbuf; | |
4564 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4565 | return kbuf; |
fddaface JA |
4566 | } |
4567 | ||
7fbb1b54 PB |
4568 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4569 | { | |
4570 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4571 | } | |
4572 | ||
93642ef8 | 4573 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4574 | { |
99bc4c38 | 4575 | int ret; |
3529d8c2 | 4576 | |
93642ef8 PB |
4577 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4578 | if (!ret) | |
4579 | req->flags |= REQ_F_NEED_CLEANUP; | |
4580 | return ret; | |
4581 | } | |
4582 | ||
4583 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4584 | { | |
4585 | struct io_sr_msg *sr = &req->sr_msg; | |
4586 | ||
d2b6f48b PB |
4587 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4588 | return -EINVAL; | |
4589 | ||
270a5940 | 4590 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4591 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4592 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4593 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4594 | if (sr->msg_flags & MSG_DONTWAIT) | |
4595 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4596 | |
d8768362 JA |
4597 | #ifdef CONFIG_COMPAT |
4598 | if (req->ctx->compat) | |
4599 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4600 | #endif | |
93642ef8 | 4601 | return 0; |
aa1fa28f JA |
4602 | } |
4603 | ||
889fca73 | 4604 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4605 | { |
6b754c8b | 4606 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4607 | struct socket *sock; |
7fbb1b54 | 4608 | struct io_buffer *kbuf; |
7a7cacba | 4609 | unsigned flags; |
0031275d | 4610 | int min_ret = 0; |
52de1fe1 | 4611 | int ret, cflags = 0; |
45d189c6 | 4612 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4613 | |
dba4a925 | 4614 | sock = sock_from_file(req->file); |
7a7cacba | 4615 | if (unlikely(!sock)) |
dba4a925 | 4616 | return -ENOTSOCK; |
3529d8c2 | 4617 | |
257e84a5 PB |
4618 | kmsg = req->async_data; |
4619 | if (!kmsg) { | |
7a7cacba PB |
4620 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4621 | if (ret) | |
681fda8d | 4622 | return ret; |
7a7cacba PB |
4623 | kmsg = &iomsg; |
4624 | } | |
03b1230c | 4625 | |
bc02ef33 | 4626 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4627 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4628 | if (IS_ERR(kbuf)) |
52de1fe1 | 4629 | return PTR_ERR(kbuf); |
7a7cacba | 4630 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4631 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4632 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4633 | 1, req->sr_msg.len); |
4634 | } | |
52de1fe1 | 4635 | |
04411806 PB |
4636 | flags = req->sr_msg.msg_flags; |
4637 | if (force_nonblock) | |
7a7cacba | 4638 | flags |= MSG_DONTWAIT; |
0031275d SM |
4639 | if (flags & MSG_WAITALL) |
4640 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4641 | ||
7a7cacba PB |
4642 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4643 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4644 | if (force_nonblock && ret == -EAGAIN) |
4645 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4646 | if (ret == -ERESTARTSYS) |
4647 | ret = -EINTR; | |
03b1230c | 4648 | |
7fbb1b54 PB |
4649 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4650 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4651 | /* fast path, check for non-NULL to avoid function call */ |
4652 | if (kmsg->free_iov) | |
4653 | kfree(kmsg->free_iov); | |
99bc4c38 | 4654 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4655 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4656 | req_set_fail(req); |
889fca73 | 4657 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4658 | return 0; |
0fa03c62 | 4659 | } |
5d17b4a4 | 4660 | |
889fca73 | 4661 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4662 | { |
6b754c8b | 4663 | struct io_buffer *kbuf; |
7a7cacba PB |
4664 | struct io_sr_msg *sr = &req->sr_msg; |
4665 | struct msghdr msg; | |
4666 | void __user *buf = sr->buf; | |
fddaface | 4667 | struct socket *sock; |
7a7cacba PB |
4668 | struct iovec iov; |
4669 | unsigned flags; | |
0031275d | 4670 | int min_ret = 0; |
bcda7baa | 4671 | int ret, cflags = 0; |
45d189c6 | 4672 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4673 | |
dba4a925 | 4674 | sock = sock_from_file(req->file); |
7a7cacba | 4675 | if (unlikely(!sock)) |
dba4a925 | 4676 | return -ENOTSOCK; |
fddaface | 4677 | |
bc02ef33 | 4678 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4679 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4680 | if (IS_ERR(kbuf)) |
4681 | return PTR_ERR(kbuf); | |
7a7cacba | 4682 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4683 | } |
bcda7baa | 4684 | |
7a7cacba | 4685 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4686 | if (unlikely(ret)) |
4687 | goto out_free; | |
fddaface | 4688 | |
7a7cacba PB |
4689 | msg.msg_name = NULL; |
4690 | msg.msg_control = NULL; | |
4691 | msg.msg_controllen = 0; | |
4692 | msg.msg_namelen = 0; | |
4693 | msg.msg_iocb = NULL; | |
4694 | msg.msg_flags = 0; | |
fddaface | 4695 | |
04411806 PB |
4696 | flags = req->sr_msg.msg_flags; |
4697 | if (force_nonblock) | |
7a7cacba | 4698 | flags |= MSG_DONTWAIT; |
0031275d SM |
4699 | if (flags & MSG_WAITALL) |
4700 | min_ret = iov_iter_count(&msg.msg_iter); | |
4701 | ||
7a7cacba PB |
4702 | ret = sock_recvmsg(sock, &msg, flags); |
4703 | if (force_nonblock && ret == -EAGAIN) | |
4704 | return -EAGAIN; | |
4705 | if (ret == -ERESTARTSYS) | |
4706 | ret = -EINTR; | |
14c32eee | 4707 | out_free: |
7fbb1b54 PB |
4708 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4709 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4710 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4711 | req_set_fail(req); |
889fca73 | 4712 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4713 | return 0; |
fddaface JA |
4714 | } |
4715 | ||
3529d8c2 | 4716 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4717 | { |
8ed8d3c3 JA |
4718 | struct io_accept *accept = &req->accept; |
4719 | ||
14587a46 | 4720 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4721 | return -EINVAL; |
8042d6ce | 4722 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4723 | return -EINVAL; |
4724 | ||
d55e5f5b JA |
4725 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4726 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4727 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4728 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4729 | return 0; |
8ed8d3c3 | 4730 | } |
17f2fe35 | 4731 | |
889fca73 | 4732 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4733 | { |
4734 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4735 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4736 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4737 | int ret; |
4738 | ||
e697deed JX |
4739 | if (req->file->f_flags & O_NONBLOCK) |
4740 | req->flags |= REQ_F_NOWAIT; | |
4741 | ||
8ed8d3c3 | 4742 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4743 | accept->addr_len, accept->flags, |
4744 | accept->nofile); | |
8ed8d3c3 | 4745 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4746 | return -EAGAIN; |
ac45abc0 PB |
4747 | if (ret < 0) { |
4748 | if (ret == -ERESTARTSYS) | |
4749 | ret = -EINTR; | |
93d2bcd2 | 4750 | req_set_fail(req); |
ac45abc0 | 4751 | } |
889fca73 | 4752 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4753 | return 0; |
8ed8d3c3 JA |
4754 | } |
4755 | ||
93642ef8 PB |
4756 | static int io_connect_prep_async(struct io_kiocb *req) |
4757 | { | |
4758 | struct io_async_connect *io = req->async_data; | |
4759 | struct io_connect *conn = &req->connect; | |
4760 | ||
4761 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4762 | } | |
4763 | ||
3529d8c2 | 4764 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4765 | { |
3529d8c2 | 4766 | struct io_connect *conn = &req->connect; |
f499a021 | 4767 | |
14587a46 | 4768 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4769 | return -EINVAL; |
4770 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4771 | return -EINVAL; | |
4772 | ||
3529d8c2 JA |
4773 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4774 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4775 | return 0; |
f499a021 JA |
4776 | } |
4777 | ||
889fca73 | 4778 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4779 | { |
e8c2bc1f | 4780 | struct io_async_connect __io, *io; |
f8e85cf2 | 4781 | unsigned file_flags; |
3fbb51c1 | 4782 | int ret; |
45d189c6 | 4783 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4784 | |
e8c2bc1f JA |
4785 | if (req->async_data) { |
4786 | io = req->async_data; | |
f499a021 | 4787 | } else { |
3529d8c2 JA |
4788 | ret = move_addr_to_kernel(req->connect.addr, |
4789 | req->connect.addr_len, | |
e8c2bc1f | 4790 | &__io.address); |
f499a021 JA |
4791 | if (ret) |
4792 | goto out; | |
4793 | io = &__io; | |
4794 | } | |
4795 | ||
3fbb51c1 JA |
4796 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4797 | ||
e8c2bc1f | 4798 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4799 | req->connect.addr_len, file_flags); |
87f80d62 | 4800 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4801 | if (req->async_data) |
b7bb4f7d | 4802 | return -EAGAIN; |
e8c2bc1f | 4803 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4804 | ret = -ENOMEM; |
4805 | goto out; | |
4806 | } | |
e8c2bc1f | 4807 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4808 | return -EAGAIN; |
f499a021 | 4809 | } |
f8e85cf2 JA |
4810 | if (ret == -ERESTARTSYS) |
4811 | ret = -EINTR; | |
f499a021 | 4812 | out: |
4e88d6e7 | 4813 | if (ret < 0) |
93d2bcd2 | 4814 | req_set_fail(req); |
889fca73 | 4815 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4816 | return 0; |
469956e8 Y |
4817 | } |
4818 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4819 | #define IO_NETOP_FN(op) \ |
4820 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4821 | { \ | |
4822 | return -EOPNOTSUPP; \ | |
4823 | } | |
4824 | ||
4825 | #define IO_NETOP_PREP(op) \ | |
4826 | IO_NETOP_FN(op) \ | |
4827 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4828 | { \ | |
4829 | return -EOPNOTSUPP; \ | |
4830 | } \ | |
4831 | ||
4832 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4833 | IO_NETOP_PREP(op) \ | |
4834 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4835 | { \ | |
4836 | return -EOPNOTSUPP; \ | |
4837 | } | |
4838 | ||
4839 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4840 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4841 | IO_NETOP_PREP_ASYNC(connect); | |
4842 | IO_NETOP_PREP(accept); | |
4843 | IO_NETOP_FN(send); | |
4844 | IO_NETOP_FN(recv); | |
469956e8 | 4845 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4846 | |
d7718a9d JA |
4847 | struct io_poll_table { |
4848 | struct poll_table_struct pt; | |
4849 | struct io_kiocb *req; | |
68b11e8b | 4850 | int nr_entries; |
d7718a9d JA |
4851 | int error; |
4852 | }; | |
ce593a6c | 4853 | |
d7718a9d | 4854 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 4855 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 4856 | { |
d7718a9d JA |
4857 | /* for instances that support it check for an event match first: */ |
4858 | if (mask && !(mask & poll->events)) | |
4859 | return 0; | |
4860 | ||
4861 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4862 | ||
4863 | list_del_init(&poll->wait.entry); | |
4864 | ||
d7718a9d | 4865 | req->result = mask; |
5b0a6acc | 4866 | req->io_task_work.func = func; |
6d816e08 | 4867 | |
d7718a9d | 4868 | /* |
e3aabf95 JA |
4869 | * If this fails, then the task is exiting. When a task exits, the |
4870 | * work gets canceled, so just cancel this request as well instead | |
4871 | * of executing it. We can't safely execute it anyway, as we may not | |
4872 | * have the needed state needed for it anyway. | |
d7718a9d | 4873 | */ |
e09ee510 | 4874 | io_req_task_work_add(req); |
d7718a9d JA |
4875 | return 1; |
4876 | } | |
4877 | ||
74ce6ce4 JA |
4878 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4879 | __acquires(&req->ctx->completion_lock) | |
4880 | { | |
4881 | struct io_ring_ctx *ctx = req->ctx; | |
4882 | ||
e09ee510 PB |
4883 | if (unlikely(req->task->flags & PF_EXITING)) |
4884 | WRITE_ONCE(poll->canceled, true); | |
4885 | ||
74ce6ce4 JA |
4886 | if (!req->result && !READ_ONCE(poll->canceled)) { |
4887 | struct poll_table_struct pt = { ._key = poll->events }; | |
4888 | ||
4889 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4890 | } | |
4891 | ||
4892 | spin_lock_irq(&ctx->completion_lock); | |
4893 | if (!req->result && !READ_ONCE(poll->canceled)) { | |
4894 | add_wait_queue(poll->head, &poll->wait); | |
4895 | return true; | |
4896 | } | |
4897 | ||
4898 | return false; | |
4899 | } | |
4900 | ||
d4e7cd36 | 4901 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4902 | { |
e8c2bc1f | 4903 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4904 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4905 | return req->async_data; |
d4e7cd36 JA |
4906 | return req->apoll->double_poll; |
4907 | } | |
4908 | ||
4909 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4910 | { | |
4911 | if (req->opcode == IORING_OP_POLL_ADD) | |
4912 | return &req->poll; | |
4913 | return &req->apoll->poll; | |
4914 | } | |
4915 | ||
4916 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 4917 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
4918 | { |
4919 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4920 | |
4921 | lockdep_assert_held(&req->ctx->completion_lock); | |
4922 | ||
4923 | if (poll && poll->head) { | |
4924 | struct wait_queue_head *head = poll->head; | |
4925 | ||
4926 | spin_lock(&head->lock); | |
4927 | list_del_init(&poll->wait.entry); | |
4928 | if (poll->wait.private) | |
de9b4cca | 4929 | req_ref_put(req); |
18bceab1 JA |
4930 | poll->head = NULL; |
4931 | spin_unlock(&head->lock); | |
4932 | } | |
4933 | } | |
4934 | ||
e27414be | 4935 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 4936 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
4937 | { |
4938 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 4939 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 4940 | int error; |
18bceab1 | 4941 | |
e27414be | 4942 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 4943 | error = -ECANCELED; |
88e41cf9 | 4944 | req->poll.events |= EPOLLONESHOT; |
e27414be | 4945 | } else { |
5082620f | 4946 | error = mangle_poll(mask); |
e27414be | 4947 | } |
b69de288 JA |
4948 | if (req->poll.events & EPOLLONESHOT) |
4949 | flags = 0; | |
d4d19c19 | 4950 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
88e41cf9 JA |
4951 | req->poll.done = true; |
4952 | flags = 0; | |
4953 | } | |
7b289c38 HX |
4954 | if (flags & IORING_CQE_F_MORE) |
4955 | ctx->cq_extra++; | |
18bceab1 | 4956 | |
18bceab1 | 4957 | io_commit_cqring(ctx); |
88e41cf9 | 4958 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
4959 | } |
4960 | ||
5b0a6acc | 4961 | static void io_poll_task_func(struct io_kiocb *req) |
18bceab1 JA |
4962 | { |
4963 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 4964 | struct io_kiocb *nxt; |
18bceab1 JA |
4965 | |
4966 | if (io_poll_rewait(req, &req->poll)) { | |
4967 | spin_unlock_irq(&ctx->completion_lock); | |
dd221f46 | 4968 | } else { |
f40b964a | 4969 | bool done; |
18bceab1 | 4970 | |
e27414be | 4971 | done = io_poll_complete(req, req->result); |
88e41cf9 | 4972 | if (done) { |
a890d01e | 4973 | io_poll_remove_double(req); |
88e41cf9 | 4974 | hash_del(&req->hash_node); |
f40b964a | 4975 | } else { |
88e41cf9 JA |
4976 | req->result = 0; |
4977 | add_wait_queue(req->poll.head, &req->poll.wait); | |
4978 | } | |
dd221f46 | 4979 | spin_unlock_irq(&ctx->completion_lock); |
dd221f46 | 4980 | io_cqring_ev_posted(ctx); |
18bceab1 | 4981 | |
88e41cf9 JA |
4982 | if (done) { |
4983 | nxt = io_put_req_find_next(req); | |
4984 | if (nxt) | |
5b0a6acc | 4985 | io_req_task_submit(nxt); |
88e41cf9 | 4986 | } |
dd221f46 | 4987 | } |
18bceab1 JA |
4988 | } |
4989 | ||
4990 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
4991 | int sync, void *key) | |
4992 | { | |
4993 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 4994 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 JA |
4995 | __poll_t mask = key_to_poll(key); |
4996 | ||
4997 | /* for instances that support it check for an event match first: */ | |
4998 | if (mask && !(mask & poll->events)) | |
4999 | return 0; | |
88e41cf9 JA |
5000 | if (!(poll->events & EPOLLONESHOT)) |
5001 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5002 | |
8706e04e JA |
5003 | list_del_init(&wait->entry); |
5004 | ||
9ce85ef2 | 5005 | if (poll->head) { |
18bceab1 JA |
5006 | bool done; |
5007 | ||
807abcb0 JA |
5008 | spin_lock(&poll->head->lock); |
5009 | done = list_empty(&poll->wait.entry); | |
18bceab1 | 5010 | if (!done) |
807abcb0 | 5011 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5012 | /* make sure double remove sees this as being gone */ |
5013 | wait->private = NULL; | |
807abcb0 | 5014 | spin_unlock(&poll->head->lock); |
c8b5e260 JA |
5015 | if (!done) { |
5016 | /* use wait func handler, so it matches the rq type */ | |
5017 | poll->wait.func(&poll->wait, mode, sync, key); | |
5018 | } | |
18bceab1 | 5019 | } |
de9b4cca | 5020 | req_ref_put(req); |
18bceab1 JA |
5021 | return 1; |
5022 | } | |
5023 | ||
5024 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5025 | wait_queue_func_t wake_func) | |
5026 | { | |
5027 | poll->head = NULL; | |
5028 | poll->done = false; | |
5029 | poll->canceled = false; | |
464dca61 JA |
5030 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5031 | /* mask in events that we always want/need */ | |
5032 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5033 | INIT_LIST_HEAD(&poll->wait.entry); |
5034 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5035 | } | |
5036 | ||
5037 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5038 | struct wait_queue_head *head, |
5039 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5040 | { |
5041 | struct io_kiocb *req = pt->req; | |
5042 | ||
5043 | /* | |
68b11e8b PB |
5044 | * The file being polled uses multiple waitqueues for poll handling |
5045 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5046 | * if this happens. | |
18bceab1 | 5047 | */ |
68b11e8b | 5048 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5049 | struct io_poll_iocb *poll_one = poll; |
5050 | ||
18bceab1 | 5051 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5052 | if (*poll_ptr) { |
18bceab1 JA |
5053 | pt->error = -EINVAL; |
5054 | return; | |
5055 | } | |
ea6a693d JA |
5056 | /* |
5057 | * Can't handle multishot for double wait for now, turn it | |
5058 | * into one-shot mode. | |
5059 | */ | |
7a274727 PB |
5060 | if (!(poll_one->events & EPOLLONESHOT)) |
5061 | poll_one->events |= EPOLLONESHOT; | |
1c3b3e65 | 5062 | /* double add on the same waitqueue head, ignore */ |
7a274727 | 5063 | if (poll_one->head == head) |
1c3b3e65 | 5064 | return; |
18bceab1 JA |
5065 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5066 | if (!poll) { | |
5067 | pt->error = -ENOMEM; | |
5068 | return; | |
5069 | } | |
58852d4d | 5070 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5071 | req_ref_get(req); |
18bceab1 | 5072 | poll->wait.private = req; |
807abcb0 | 5073 | *poll_ptr = poll; |
18bceab1 JA |
5074 | } |
5075 | ||
68b11e8b | 5076 | pt->nr_entries++; |
18bceab1 | 5077 | poll->head = head; |
a31eb4a2 JX |
5078 | |
5079 | if (poll->events & EPOLLEXCLUSIVE) | |
5080 | add_wait_queue_exclusive(head, &poll->wait); | |
5081 | else | |
5082 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5083 | } |
5084 | ||
5085 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5086 | struct poll_table_struct *p) | |
5087 | { | |
5088 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5089 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5090 | |
807abcb0 | 5091 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5092 | } |
5093 | ||
5b0a6acc | 5094 | static void io_async_task_func(struct io_kiocb *req) |
d7718a9d | 5095 | { |
d7718a9d JA |
5096 | struct async_poll *apoll = req->apoll; |
5097 | struct io_ring_ctx *ctx = req->ctx; | |
5098 | ||
236daeae | 5099 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5100 | |
74ce6ce4 | 5101 | if (io_poll_rewait(req, &apoll->poll)) { |
d7718a9d | 5102 | spin_unlock_irq(&ctx->completion_lock); |
74ce6ce4 | 5103 | return; |
d7718a9d JA |
5104 | } |
5105 | ||
0ea13b44 | 5106 | hash_del(&req->hash_node); |
d4e7cd36 | 5107 | io_poll_remove_double(req); |
74ce6ce4 JA |
5108 | spin_unlock_irq(&ctx->completion_lock); |
5109 | ||
0be0b0e3 | 5110 | if (!READ_ONCE(apoll->poll.canceled)) |
5b0a6acc | 5111 | io_req_task_submit(req); |
0be0b0e3 | 5112 | else |
2593553a | 5113 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5114 | } |
5115 | ||
5116 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5117 | void *key) | |
5118 | { | |
5119 | struct io_kiocb *req = wait->private; | |
5120 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5121 | ||
5122 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5123 | key_to_poll(key)); | |
5124 | ||
5125 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5126 | } | |
5127 | ||
5128 | static void io_poll_req_insert(struct io_kiocb *req) | |
5129 | { | |
5130 | struct io_ring_ctx *ctx = req->ctx; | |
5131 | struct hlist_head *list; | |
5132 | ||
5133 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5134 | hlist_add_head(&req->hash_node, list); | |
5135 | } | |
5136 | ||
5137 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5138 | struct io_poll_iocb *poll, | |
5139 | struct io_poll_table *ipt, __poll_t mask, | |
5140 | wait_queue_func_t wake_func) | |
5141 | __acquires(&ctx->completion_lock) | |
5142 | { | |
5143 | struct io_ring_ctx *ctx = req->ctx; | |
5144 | bool cancel = false; | |
5145 | ||
4d52f338 | 5146 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5147 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5148 | poll->file = req->file; |
18bceab1 | 5149 | poll->wait.private = req; |
d7718a9d JA |
5150 | |
5151 | ipt->pt._key = mask; | |
5152 | ipt->req = req; | |
68b11e8b PB |
5153 | ipt->error = 0; |
5154 | ipt->nr_entries = 0; | |
d7718a9d | 5155 | |
d7718a9d | 5156 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5157 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5158 | ipt->error = -EINVAL; | |
d7718a9d JA |
5159 | |
5160 | spin_lock_irq(&ctx->completion_lock); | |
a890d01e | 5161 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5162 | io_poll_remove_double(req); |
d7718a9d JA |
5163 | if (likely(poll->head)) { |
5164 | spin_lock(&poll->head->lock); | |
5165 | if (unlikely(list_empty(&poll->wait.entry))) { | |
5166 | if (ipt->error) | |
5167 | cancel = true; | |
5168 | ipt->error = 0; | |
5169 | mask = 0; | |
5170 | } | |
88e41cf9 | 5171 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5172 | list_del_init(&poll->wait.entry); |
5173 | else if (cancel) | |
5174 | WRITE_ONCE(poll->canceled, true); | |
5175 | else if (!poll->done) /* actually waiting for an event */ | |
5176 | io_poll_req_insert(req); | |
5177 | spin_unlock(&poll->head->lock); | |
5178 | } | |
5179 | ||
5180 | return mask; | |
5181 | } | |
5182 | ||
59b735ae OL |
5183 | enum { |
5184 | IO_APOLL_OK, | |
5185 | IO_APOLL_ABORTED, | |
5186 | IO_APOLL_READY | |
5187 | }; | |
5188 | ||
5189 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5190 | { |
5191 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5192 | struct io_ring_ctx *ctx = req->ctx; | |
5193 | struct async_poll *apoll; | |
5194 | struct io_poll_table ipt; | |
b2d9c3da | 5195 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5196 | int rw; |
d7718a9d JA |
5197 | |
5198 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5199 | return IO_APOLL_ABORTED; |
24c74678 | 5200 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5201 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5202 | if (!def->pollin && !def->pollout) |
5203 | return IO_APOLL_ABORTED; | |
5204 | ||
5205 | if (def->pollin) { | |
9dab14b8 | 5206 | rw = READ; |
b2d9c3da PB |
5207 | mask |= POLLIN | POLLRDNORM; |
5208 | ||
5209 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5210 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5211 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5212 | mask &= ~POLLIN; | |
5213 | } else { | |
9dab14b8 | 5214 | rw = WRITE; |
b2d9c3da PB |
5215 | mask |= POLLOUT | POLLWRNORM; |
5216 | } | |
5217 | ||
9dab14b8 | 5218 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5219 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5220 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5221 | |
5222 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5223 | if (unlikely(!apoll)) | |
59b735ae | 5224 | return IO_APOLL_ABORTED; |
807abcb0 | 5225 | apoll->double_poll = NULL; |
d7718a9d | 5226 | req->apoll = apoll; |
b2d9c3da | 5227 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
5228 | ipt.pt._qproc = io_async_queue_proc; |
5229 | ||
5230 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5231 | io_async_wake); | |
a36da65c | 5232 | if (ret || ipt.error) { |
d7718a9d | 5233 | spin_unlock_irq(&ctx->completion_lock); |
59b735ae OL |
5234 | if (ret) |
5235 | return IO_APOLL_READY; | |
5236 | return IO_APOLL_ABORTED; | |
d7718a9d JA |
5237 | } |
5238 | spin_unlock_irq(&ctx->completion_lock); | |
236daeae OL |
5239 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5240 | mask, apoll->poll.events); | |
59b735ae | 5241 | return IO_APOLL_OK; |
d7718a9d JA |
5242 | } |
5243 | ||
5244 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5245 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5246 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5247 | { |
b41e9852 | 5248 | bool do_complete = false; |
221c5eb2 | 5249 | |
5082620f JA |
5250 | if (!poll->head) |
5251 | return false; | |
221c5eb2 | 5252 | spin_lock(&poll->head->lock); |
b2e720ac JA |
5253 | if (do_cancel) |
5254 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5255 | if (!list_empty(&poll->wait.entry)) { |
5256 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5257 | do_complete = true; |
221c5eb2 JA |
5258 | } |
5259 | spin_unlock(&poll->head->lock); | |
3bfa5bcb | 5260 | hash_del(&req->hash_node); |
d7718a9d JA |
5261 | return do_complete; |
5262 | } | |
5263 | ||
b2c3f7e1 | 5264 | static bool io_poll_remove_waitqs(struct io_kiocb *req) |
e07785b0 | 5265 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5266 | { |
5267 | bool do_complete; | |
5268 | ||
d4e7cd36 | 5269 | io_poll_remove_double(req); |
e31001a3 | 5270 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5271 | |
e31001a3 | 5272 | if (req->opcode != IORING_OP_POLL_ADD && do_complete) { |
d7718a9d | 5273 | /* non-poll requests have submit ref still */ |
e31001a3 | 5274 | req_ref_put(req); |
b1f573bd | 5275 | } |
b2c3f7e1 JA |
5276 | return do_complete; |
5277 | } | |
5278 | ||
5279 | static bool io_poll_remove_one(struct io_kiocb *req) | |
e07785b0 | 5280 | __must_hold(&req->ctx->completion_lock) |
b2c3f7e1 JA |
5281 | { |
5282 | bool do_complete; | |
b1f573bd | 5283 | |
b2c3f7e1 | 5284 | do_complete = io_poll_remove_waitqs(req); |
b41e9852 | 5285 | if (do_complete) { |
d4d19c19 | 5286 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5287 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5288 | req_set_fail(req); |
216578e5 | 5289 | io_put_req_deferred(req, 1); |
b41e9852 JA |
5290 | } |
5291 | ||
5292 | return do_complete; | |
221c5eb2 JA |
5293 | } |
5294 | ||
76e1b642 JA |
5295 | /* |
5296 | * Returns true if we found and killed one or more poll requests | |
5297 | */ | |
6b81928d | 5298 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5299 | bool cancel_all) |
221c5eb2 | 5300 | { |
78076bb6 | 5301 | struct hlist_node *tmp; |
221c5eb2 | 5302 | struct io_kiocb *req; |
8e2e1faf | 5303 | int posted = 0, i; |
221c5eb2 JA |
5304 | |
5305 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
5306 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5307 | struct hlist_head *list; | |
5308 | ||
5309 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5310 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5311 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5312 | posted += io_poll_remove_one(req); |
5313 | } | |
221c5eb2 JA |
5314 | } |
5315 | spin_unlock_irq(&ctx->completion_lock); | |
b41e9852 | 5316 | |
8e2e1faf JA |
5317 | if (posted) |
5318 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5319 | |
5320 | return posted != 0; | |
221c5eb2 JA |
5321 | } |
5322 | ||
9ba5fac8 PB |
5323 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5324 | bool poll_only) | |
e07785b0 | 5325 | __must_hold(&ctx->completion_lock) |
47f46768 | 5326 | { |
78076bb6 | 5327 | struct hlist_head *list; |
47f46768 JA |
5328 | struct io_kiocb *req; |
5329 | ||
78076bb6 JA |
5330 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5331 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5332 | if (sqe_addr != req->user_data) |
5333 | continue; | |
9ba5fac8 PB |
5334 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5335 | continue; | |
b2cb805f | 5336 | return req; |
47f46768 | 5337 | } |
b2cb805f JA |
5338 | return NULL; |
5339 | } | |
5340 | ||
9ba5fac8 PB |
5341 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5342 | bool poll_only) | |
e07785b0 | 5343 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5344 | { |
5345 | struct io_kiocb *req; | |
5346 | ||
9ba5fac8 | 5347 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5348 | if (!req) |
5349 | return -ENOENT; | |
5350 | if (io_poll_remove_one(req)) | |
5351 | return 0; | |
5352 | ||
5353 | return -EALREADY; | |
47f46768 JA |
5354 | } |
5355 | ||
9096af3e PB |
5356 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5357 | unsigned int flags) | |
5358 | { | |
5359 | u32 events; | |
47f46768 | 5360 | |
9096af3e PB |
5361 | events = READ_ONCE(sqe->poll32_events); |
5362 | #ifdef __BIG_ENDIAN | |
5363 | events = swahw32(events); | |
5364 | #endif | |
5365 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5366 | events |= EPOLLONESHOT; | |
5367 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5368 | } |
5369 | ||
c5de0036 | 5370 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5371 | const struct io_uring_sqe *sqe) |
0969e783 | 5372 | { |
c5de0036 PB |
5373 | struct io_poll_update *upd = &req->poll_update; |
5374 | u32 flags; | |
5375 | ||
0969e783 JA |
5376 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5377 | return -EINVAL; | |
c5de0036 PB |
5378 | if (sqe->ioprio || sqe->buf_index) |
5379 | return -EINVAL; | |
5380 | flags = READ_ONCE(sqe->len); | |
5381 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5382 | IORING_POLL_ADD_MULTI)) | |
5383 | return -EINVAL; | |
5384 | /* meaningless without update */ | |
5385 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5386 | return -EINVAL; |
5387 | ||
c5de0036 PB |
5388 | upd->old_user_data = READ_ONCE(sqe->addr); |
5389 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5390 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5391 | |
c5de0036 PB |
5392 | upd->new_user_data = READ_ONCE(sqe->off); |
5393 | if (!upd->update_user_data && upd->new_user_data) | |
5394 | return -EINVAL; | |
5395 | if (upd->update_events) | |
5396 | upd->events = io_poll_parse_events(sqe, flags); | |
5397 | else if (sqe->poll32_events) | |
5398 | return -EINVAL; | |
221c5eb2 | 5399 | |
221c5eb2 JA |
5400 | return 0; |
5401 | } | |
5402 | ||
221c5eb2 JA |
5403 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5404 | void *key) | |
5405 | { | |
c2f2eb7d JA |
5406 | struct io_kiocb *req = wait->private; |
5407 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5408 | |
d7718a9d | 5409 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5410 | } |
5411 | ||
221c5eb2 JA |
5412 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5413 | struct poll_table_struct *p) | |
5414 | { | |
5415 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5416 | ||
e8c2bc1f | 5417 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5418 | } |
5419 | ||
3529d8c2 | 5420 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5421 | { |
5422 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5423 | u32 flags; |
221c5eb2 JA |
5424 | |
5425 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5426 | return -EINVAL; | |
c5de0036 | 5427 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5428 | return -EINVAL; |
5429 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5430 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5431 | return -EINVAL; |
5432 | ||
c5de0036 | 5433 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5434 | return 0; |
5435 | } | |
5436 | ||
61e98203 | 5437 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5438 | { |
5439 | struct io_poll_iocb *poll = &req->poll; | |
5440 | struct io_ring_ctx *ctx = req->ctx; | |
5441 | struct io_poll_table ipt; | |
0969e783 | 5442 | __poll_t mask; |
0969e783 | 5443 | |
d7718a9d | 5444 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5445 | |
d7718a9d JA |
5446 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5447 | io_poll_wake); | |
221c5eb2 | 5448 | |
8c838788 | 5449 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5450 | ipt.error = 0; |
e27414be | 5451 | io_poll_complete(req, mask); |
221c5eb2 | 5452 | } |
221c5eb2 JA |
5453 | spin_unlock_irq(&ctx->completion_lock); |
5454 | ||
8c838788 JA |
5455 | if (mask) { |
5456 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5457 | if (poll->events & EPOLLONESHOT) |
5458 | io_put_req(req); | |
221c5eb2 | 5459 | } |
8c838788 | 5460 | return ipt.error; |
221c5eb2 JA |
5461 | } |
5462 | ||
c5de0036 | 5463 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5464 | { |
5465 | struct io_ring_ctx *ctx = req->ctx; | |
5466 | struct io_kiocb *preq; | |
cb3b200e | 5467 | bool completing; |
b69de288 JA |
5468 | int ret; |
5469 | ||
5470 | spin_lock_irq(&ctx->completion_lock); | |
9ba5fac8 | 5471 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5472 | if (!preq) { |
5473 | ret = -ENOENT; | |
5474 | goto err; | |
b69de288 | 5475 | } |
cb3b200e | 5476 | |
c5de0036 PB |
5477 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5478 | completing = true; | |
5479 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5480 | goto err; | |
5481 | } | |
5482 | ||
cb3b200e JA |
5483 | /* |
5484 | * Don't allow racy completion with singleshot, as we cannot safely | |
5485 | * update those. For multishot, if we're racing with completion, just | |
5486 | * let completion re-add it. | |
5487 | */ | |
5488 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5489 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5490 | ret = -EALREADY; | |
5491 | goto err; | |
b69de288 JA |
5492 | } |
5493 | /* we now have a detached poll request. reissue. */ | |
5494 | ret = 0; | |
5495 | err: | |
b69de288 | 5496 | if (ret < 0) { |
cb3b200e | 5497 | spin_unlock_irq(&ctx->completion_lock); |
93d2bcd2 | 5498 | req_set_fail(req); |
b69de288 JA |
5499 | io_req_complete(req, ret); |
5500 | return 0; | |
5501 | } | |
5502 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5503 | if (req->poll_update.update_events) { |
b69de288 | 5504 | preq->poll.events &= ~0xffff; |
9d805892 | 5505 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5506 | preq->poll.events |= IO_POLL_UNMASK; |
5507 | } | |
9d805892 PB |
5508 | if (req->poll_update.update_user_data) |
5509 | preq->user_data = req->poll_update.new_user_data; | |
cb3b200e JA |
5510 | spin_unlock_irq(&ctx->completion_lock); |
5511 | ||
b69de288 JA |
5512 | /* complete update request, we're done with it */ |
5513 | io_req_complete(req, ret); | |
5514 | ||
cb3b200e | 5515 | if (!completing) { |
c5de0036 | 5516 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5517 | if (ret < 0) { |
93d2bcd2 | 5518 | req_set_fail(preq); |
cb3b200e JA |
5519 | io_req_complete(preq, ret); |
5520 | } | |
b69de288 JA |
5521 | } |
5522 | return 0; | |
5523 | } | |
5524 | ||
5262f567 JA |
5525 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5526 | { | |
ad8a48ac JA |
5527 | struct io_timeout_data *data = container_of(timer, |
5528 | struct io_timeout_data, timer); | |
5529 | struct io_kiocb *req = data->req; | |
5530 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5531 | unsigned long flags; |
5532 | ||
5262f567 | 5533 | spin_lock_irqsave(&ctx->completion_lock, flags); |
a71976f3 | 5534 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5535 | atomic_set(&req->ctx->cq_timeouts, |
5536 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
5537 | ||
d4d19c19 | 5538 | io_cqring_fill_event(ctx, req->user_data, -ETIME, 0); |
5262f567 JA |
5539 | io_commit_cqring(ctx); |
5540 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5541 | ||
5542 | io_cqring_ev_posted(ctx); | |
93d2bcd2 | 5543 | req_set_fail(req); |
5262f567 JA |
5544 | io_put_req(req); |
5545 | return HRTIMER_NORESTART; | |
5546 | } | |
5547 | ||
fbd15848 PB |
5548 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5549 | __u64 user_data) | |
e07785b0 | 5550 | __must_hold(&ctx->completion_lock) |
f254ac04 | 5551 | { |
fbd15848 | 5552 | struct io_timeout_data *io; |
47f46768 | 5553 | struct io_kiocb *req; |
fd9c7bc5 | 5554 | bool found = false; |
f254ac04 | 5555 | |
135fcde8 | 5556 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5557 | found = user_data == req->user_data; |
5558 | if (found) | |
47f46768 | 5559 | break; |
47f46768 | 5560 | } |
fd9c7bc5 PB |
5561 | if (!found) |
5562 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5563 | |
5564 | io = req->async_data; | |
fd9c7bc5 | 5565 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5566 | return ERR_PTR(-EALREADY); |
a71976f3 | 5567 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5568 | return req; |
5569 | } | |
47f46768 | 5570 | |
fbd15848 | 5571 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
e07785b0 | 5572 | __must_hold(&ctx->completion_lock) |
fbd15848 PB |
5573 | { |
5574 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5575 | ||
5576 | if (IS_ERR(req)) | |
5577 | return PTR_ERR(req); | |
f254ac04 | 5578 | |
93d2bcd2 | 5579 | req_set_fail(req); |
d4d19c19 | 5580 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
216578e5 | 5581 | io_put_req_deferred(req, 1); |
f254ac04 JA |
5582 | return 0; |
5583 | } | |
5584 | ||
9c8e11b3 PB |
5585 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5586 | struct timespec64 *ts, enum hrtimer_mode mode) | |
e07785b0 | 5587 | __must_hold(&ctx->completion_lock) |
47f46768 | 5588 | { |
9c8e11b3 PB |
5589 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5590 | struct io_timeout_data *data; | |
47f46768 | 5591 | |
9c8e11b3 PB |
5592 | if (IS_ERR(req)) |
5593 | return PTR_ERR(req); | |
47f46768 | 5594 | |
9c8e11b3 PB |
5595 | req->timeout.off = 0; /* noseq */ |
5596 | data = req->async_data; | |
5597 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5598 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5599 | data->timer.function = io_timeout_fn; | |
5600 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5601 | return 0; | |
47f46768 JA |
5602 | } |
5603 | ||
3529d8c2 JA |
5604 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5605 | const struct io_uring_sqe *sqe) | |
b29472ee | 5606 | { |
9c8e11b3 PB |
5607 | struct io_timeout_rem *tr = &req->timeout_rem; |
5608 | ||
b29472ee JA |
5609 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5610 | return -EINVAL; | |
61710e43 DA |
5611 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5612 | return -EINVAL; | |
9c8e11b3 | 5613 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5614 | return -EINVAL; |
5615 | ||
9c8e11b3 PB |
5616 | tr->addr = READ_ONCE(sqe->addr); |
5617 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5618 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5619 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5620 | return -EINVAL; | |
5621 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5622 | return -EFAULT; | |
5623 | } else if (tr->flags) { | |
5624 | /* timeout removal doesn't support flags */ | |
b29472ee | 5625 | return -EINVAL; |
9c8e11b3 | 5626 | } |
b29472ee | 5627 | |
b29472ee JA |
5628 | return 0; |
5629 | } | |
5630 | ||
8662daec PB |
5631 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5632 | { | |
5633 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5634 | : HRTIMER_MODE_REL; | |
5635 | } | |
5636 | ||
11365043 JA |
5637 | /* |
5638 | * Remove or update an existing timeout command | |
5639 | */ | |
61e98203 | 5640 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5641 | { |
9c8e11b3 | 5642 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5643 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5644 | int ret; |
11365043 | 5645 | |
11365043 | 5646 | spin_lock_irq(&ctx->completion_lock); |
8662daec | 5647 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5648 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5649 | else |
5650 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5651 | io_translate_timeout_mode(tr->flags)); | |
11365043 | 5652 | |
d4d19c19 | 5653 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
11365043 JA |
5654 | io_commit_cqring(ctx); |
5655 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 5656 | io_cqring_ev_posted(ctx); |
4e88d6e7 | 5657 | if (ret < 0) |
93d2bcd2 | 5658 | req_set_fail(req); |
ec9c02ad | 5659 | io_put_req(req); |
11365043 | 5660 | return 0; |
5262f567 JA |
5661 | } |
5662 | ||
3529d8c2 | 5663 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5664 | bool is_timeout_link) |
5262f567 | 5665 | { |
ad8a48ac | 5666 | struct io_timeout_data *data; |
a41525ab | 5667 | unsigned flags; |
56080b02 | 5668 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5669 | |
ad8a48ac | 5670 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5671 | return -EINVAL; |
ad8a48ac | 5672 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5673 | return -EINVAL; |
56080b02 | 5674 | if (off && is_timeout_link) |
2d28390a | 5675 | return -EINVAL; |
a41525ab JA |
5676 | flags = READ_ONCE(sqe->timeout_flags); |
5677 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5678 | return -EINVAL; |
bdf20073 | 5679 | |
bfe68a22 | 5680 | req->timeout.off = off; |
f18ee4cf PB |
5681 | if (unlikely(off && !req->ctx->off_timeout_used)) |
5682 | req->ctx->off_timeout_used = true; | |
26a61679 | 5683 | |
e8c2bc1f | 5684 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5685 | return -ENOMEM; |
5686 | ||
e8c2bc1f | 5687 | data = req->async_data; |
ad8a48ac | 5688 | data->req = req; |
ad8a48ac JA |
5689 | |
5690 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5691 | return -EFAULT; |
5692 | ||
8662daec | 5693 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac | 5694 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
2482b58f PB |
5695 | if (is_timeout_link) |
5696 | io_req_track_inflight(req); | |
ad8a48ac JA |
5697 | return 0; |
5698 | } | |
5699 | ||
61e98203 | 5700 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5701 | { |
ad8a48ac | 5702 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5703 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5704 | struct list_head *entry; |
bfe68a22 | 5705 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5706 | |
733f5c95 | 5707 | spin_lock_irq(&ctx->completion_lock); |
93bd25bb | 5708 | |
5262f567 JA |
5709 | /* |
5710 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5711 | * timeout event to be satisfied. If it isn't set, then this is |
5712 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5713 | */ |
8eb7e2d0 | 5714 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5715 | entry = ctx->timeout_list.prev; |
5716 | goto add; | |
5717 | } | |
5262f567 | 5718 | |
bfe68a22 PB |
5719 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5720 | req->timeout.target_seq = tail + off; | |
5262f567 | 5721 | |
f010505b MDG |
5722 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5723 | * This is safe because ->completion_lock is held, and submissions | |
5724 | * and completions are never mixed in the same ->completion_lock section. | |
5725 | */ | |
5726 | ctx->cq_last_tm_flush = tail; | |
5727 | ||
5262f567 JA |
5728 | /* |
5729 | * Insertion sort, ensuring the first entry in the list is always | |
5730 | * the one we need first. | |
5731 | */ | |
5262f567 | 5732 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5733 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5734 | timeout.list); | |
5262f567 | 5735 | |
8eb7e2d0 | 5736 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5737 | continue; |
bfe68a22 PB |
5738 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5739 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5740 | break; |
5741 | } | |
93bd25bb | 5742 | add: |
135fcde8 | 5743 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5744 | data->timer.function = io_timeout_fn; |
5745 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 5746 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
5747 | return 0; |
5748 | } | |
5262f567 | 5749 | |
f458dd84 PB |
5750 | struct io_cancel_data { |
5751 | struct io_ring_ctx *ctx; | |
5752 | u64 user_data; | |
5753 | }; | |
5754 | ||
62755e35 JA |
5755 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5756 | { | |
5757 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5758 | struct io_cancel_data *cd = data; |
62755e35 | 5759 | |
f458dd84 | 5760 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5761 | } |
5762 | ||
f458dd84 PB |
5763 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5764 | struct io_ring_ctx *ctx) | |
62755e35 | 5765 | { |
f458dd84 | 5766 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5767 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5768 | int ret = 0; |
5769 | ||
f458dd84 | 5770 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5771 | return -ENOENT; |
5772 | ||
f458dd84 | 5773 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5774 | switch (cancel_ret) { |
5775 | case IO_WQ_CANCEL_OK: | |
5776 | ret = 0; | |
5777 | break; | |
5778 | case IO_WQ_CANCEL_RUNNING: | |
5779 | ret = -EALREADY; | |
5780 | break; | |
5781 | case IO_WQ_CANCEL_NOTFOUND: | |
5782 | ret = -ENOENT; | |
5783 | break; | |
5784 | } | |
5785 | ||
e977d6d3 JA |
5786 | return ret; |
5787 | } | |
5788 | ||
47f46768 JA |
5789 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5790 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5791 | int success_ret) |
47f46768 JA |
5792 | { |
5793 | unsigned long flags; | |
5794 | int ret; | |
5795 | ||
f458dd84 | 5796 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
47f46768 | 5797 | spin_lock_irqsave(&ctx->completion_lock, flags); |
df9727af PB |
5798 | if (ret != -ENOENT) |
5799 | goto done; | |
47f46768 JA |
5800 | ret = io_timeout_cancel(ctx, sqe_addr); |
5801 | if (ret != -ENOENT) | |
5802 | goto done; | |
9ba5fac8 | 5803 | ret = io_poll_cancel(ctx, sqe_addr, false); |
47f46768 | 5804 | done: |
b0dd8a41 JA |
5805 | if (!ret) |
5806 | ret = success_ret; | |
d4d19c19 | 5807 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
47f46768 JA |
5808 | io_commit_cqring(ctx); |
5809 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
5810 | io_cqring_ev_posted(ctx); | |
5811 | ||
4e88d6e7 | 5812 | if (ret < 0) |
93d2bcd2 | 5813 | req_set_fail(req); |
47f46768 JA |
5814 | } |
5815 | ||
3529d8c2 JA |
5816 | static int io_async_cancel_prep(struct io_kiocb *req, |
5817 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5818 | { |
fbf23849 | 5819 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5820 | return -EINVAL; |
61710e43 DA |
5821 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5822 | return -EINVAL; | |
5823 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5824 | return -EINVAL; |
5825 | ||
fbf23849 JA |
5826 | req->cancel.addr = READ_ONCE(sqe->addr); |
5827 | return 0; | |
5828 | } | |
5829 | ||
61e98203 | 5830 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5831 | { |
5832 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5833 | u64 sqe_addr = req->cancel.addr; |
5834 | struct io_tctx_node *node; | |
5835 | int ret; | |
5836 | ||
5837 | /* tasks should wait for their io-wq threads, so safe w/o sync */ | |
5838 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); | |
5839 | spin_lock_irq(&ctx->completion_lock); | |
5840 | if (ret != -ENOENT) | |
5841 | goto done; | |
5842 | ret = io_timeout_cancel(ctx, sqe_addr); | |
5843 | if (ret != -ENOENT) | |
5844 | goto done; | |
9ba5fac8 | 5845 | ret = io_poll_cancel(ctx, sqe_addr, false); |
58f99373 PB |
5846 | if (ret != -ENOENT) |
5847 | goto done; | |
5848 | spin_unlock_irq(&ctx->completion_lock); | |
5849 | ||
5850 | /* slow path, try all io-wq's */ | |
5851 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5852 | ret = -ENOENT; | |
5853 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
5854 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 5855 | |
58f99373 PB |
5856 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
5857 | if (ret != -ENOENT) | |
5858 | break; | |
5859 | } | |
5860 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5861 | ||
5862 | spin_lock_irq(&ctx->completion_lock); | |
5863 | done: | |
d4d19c19 | 5864 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
58f99373 PB |
5865 | io_commit_cqring(ctx); |
5866 | spin_unlock_irq(&ctx->completion_lock); | |
5867 | io_cqring_ev_posted(ctx); | |
5868 | ||
5869 | if (ret < 0) | |
93d2bcd2 | 5870 | req_set_fail(req); |
58f99373 | 5871 | io_put_req(req); |
5262f567 JA |
5872 | return 0; |
5873 | } | |
5874 | ||
269bbe5f | 5875 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5876 | const struct io_uring_sqe *sqe) |
5877 | { | |
61710e43 DA |
5878 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5879 | return -EINVAL; | |
5880 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5881 | return -EINVAL; |
5882 | ||
269bbe5f BM |
5883 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5884 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5885 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5886 | return -EINVAL; |
269bbe5f | 5887 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5888 | return 0; |
5889 | } | |
5890 | ||
889fca73 | 5891 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5892 | { |
5893 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 5894 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 5895 | int ret; |
fbf23849 | 5896 | |
45d189c6 | 5897 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5898 | return -EAGAIN; |
05f3fb3c | 5899 | |
269bbe5f BM |
5900 | up.offset = req->rsrc_update.offset; |
5901 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
5902 | up.nr = 0; |
5903 | up.tags = 0; | |
615cee49 | 5904 | up.resv = 0; |
05f3fb3c JA |
5905 | |
5906 | mutex_lock(&ctx->uring_lock); | |
fdecb662 | 5907 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 5908 | &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5909 | mutex_unlock(&ctx->uring_lock); |
5910 | ||
5911 | if (ret < 0) | |
93d2bcd2 | 5912 | req_set_fail(req); |
889fca73 | 5913 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5914 | return 0; |
5915 | } | |
5916 | ||
bfe76559 | 5917 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5918 | { |
d625c6ee | 5919 | switch (req->opcode) { |
e781573e | 5920 | case IORING_OP_NOP: |
bfe76559 | 5921 | return 0; |
f67676d1 JA |
5922 | case IORING_OP_READV: |
5923 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5924 | case IORING_OP_READ: |
bfe76559 | 5925 | return io_read_prep(req, sqe); |
f67676d1 JA |
5926 | case IORING_OP_WRITEV: |
5927 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5928 | case IORING_OP_WRITE: |
bfe76559 | 5929 | return io_write_prep(req, sqe); |
0969e783 | 5930 | case IORING_OP_POLL_ADD: |
bfe76559 | 5931 | return io_poll_add_prep(req, sqe); |
0969e783 | 5932 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 5933 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 5934 | case IORING_OP_FSYNC: |
1155c76a | 5935 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5936 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5937 | return io_sfr_prep(req, sqe); |
03b1230c | 5938 | case IORING_OP_SENDMSG: |
fddaface | 5939 | case IORING_OP_SEND: |
bfe76559 | 5940 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5941 | case IORING_OP_RECVMSG: |
fddaface | 5942 | case IORING_OP_RECV: |
bfe76559 | 5943 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5944 | case IORING_OP_CONNECT: |
bfe76559 | 5945 | return io_connect_prep(req, sqe); |
2d28390a | 5946 | case IORING_OP_TIMEOUT: |
bfe76559 | 5947 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5948 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5949 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5950 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5951 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5952 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5953 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5954 | case IORING_OP_ACCEPT: |
bfe76559 | 5955 | return io_accept_prep(req, sqe); |
d63d1b5e | 5956 | case IORING_OP_FALLOCATE: |
bfe76559 | 5957 | return io_fallocate_prep(req, sqe); |
15b71abe | 5958 | case IORING_OP_OPENAT: |
bfe76559 | 5959 | return io_openat_prep(req, sqe); |
b5dba59e | 5960 | case IORING_OP_CLOSE: |
bfe76559 | 5961 | return io_close_prep(req, sqe); |
05f3fb3c | 5962 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5963 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5964 | case IORING_OP_STATX: |
bfe76559 | 5965 | return io_statx_prep(req, sqe); |
4840e418 | 5966 | case IORING_OP_FADVISE: |
bfe76559 | 5967 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5968 | case IORING_OP_MADVISE: |
bfe76559 | 5969 | return io_madvise_prep(req, sqe); |
cebdb986 | 5970 | case IORING_OP_OPENAT2: |
bfe76559 | 5971 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5972 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5973 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5974 | case IORING_OP_SPLICE: |
bfe76559 | 5975 | return io_splice_prep(req, sqe); |
ddf0322d | 5976 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5977 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5978 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5979 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5980 | case IORING_OP_TEE: |
bfe76559 | 5981 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5982 | case IORING_OP_SHUTDOWN: |
5983 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5984 | case IORING_OP_RENAMEAT: |
5985 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5986 | case IORING_OP_UNLINKAT: |
5987 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5988 | } |
5989 | ||
bfe76559 PB |
5990 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
5991 | req->opcode); | |
bd54b6fe | 5992 | return -EINVAL; |
bfe76559 PB |
5993 | } |
5994 | ||
93642ef8 | 5995 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 5996 | { |
b7e298d2 PB |
5997 | if (!io_op_defs[req->opcode].needs_async_setup) |
5998 | return 0; | |
5999 | if (WARN_ON_ONCE(req->async_data)) | |
6000 | return -EFAULT; | |
6001 | if (io_alloc_async_data(req)) | |
6002 | return -EAGAIN; | |
6003 | ||
93642ef8 PB |
6004 | switch (req->opcode) { |
6005 | case IORING_OP_READV: | |
93642ef8 PB |
6006 | return io_rw_prep_async(req, READ); |
6007 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6008 | return io_rw_prep_async(req, WRITE); |
6009 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6010 | return io_sendmsg_prep_async(req); |
6011 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6012 | return io_recvmsg_prep_async(req); |
6013 | case IORING_OP_CONNECT: | |
6014 | return io_connect_prep_async(req); | |
6015 | } | |
b7e298d2 PB |
6016 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6017 | req->opcode); | |
6018 | return -EFAULT; | |
f67676d1 JA |
6019 | } |
6020 | ||
9cf7c104 PB |
6021 | static u32 io_get_sequence(struct io_kiocb *req) |
6022 | { | |
a3dbdf54 | 6023 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6024 | |
a3dbdf54 PB |
6025 | /* need original cached_sq_head, but it was increased for each req */ |
6026 | io_for_each_link(req, req) | |
6027 | seq--; | |
6028 | return seq; | |
9cf7c104 PB |
6029 | } |
6030 | ||
76cc33d7 | 6031 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6032 | { |
3c19966d | 6033 | struct io_kiocb *pos; |
a197f664 | 6034 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6035 | struct io_defer_entry *de; |
f67676d1 | 6036 | int ret; |
9cf7c104 | 6037 | u32 seq; |
de0617e4 | 6038 | |
3c19966d PB |
6039 | /* |
6040 | * If we need to drain a request in the middle of a link, drain the | |
6041 | * head request and the next request/link after the current link. | |
6042 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6043 | * maintained for every request of our link. | |
6044 | */ | |
6045 | if (ctx->drain_next) { | |
6046 | req->flags |= REQ_F_IO_DRAIN; | |
6047 | ctx->drain_next = false; | |
6048 | } | |
6049 | /* not interested in head, start from the first linked */ | |
6050 | io_for_each_link(pos, req->link) { | |
6051 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6052 | ctx->drain_next = true; | |
6053 | req->flags |= REQ_F_IO_DRAIN; | |
6054 | break; | |
6055 | } | |
6056 | } | |
6057 | ||
9d858b21 | 6058 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 | 6059 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 PB |
6060 | !(req->flags & REQ_F_IO_DRAIN))) { |
6061 | ctx->drain_active = false; | |
76cc33d7 | 6062 | return false; |
10c66904 | 6063 | } |
9cf7c104 PB |
6064 | |
6065 | seq = io_get_sequence(req); | |
6066 | /* Still a chance to pass the sequence check */ | |
6067 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6068 | return false; |
de0617e4 | 6069 | |
b7e298d2 | 6070 | ret = io_req_prep_async(req); |
be7053b7 | 6071 | if (ret) |
1b48773f | 6072 | goto fail; |
cbdcb435 | 6073 | io_prep_async_link(req); |
27dc8338 | 6074 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6075 | if (!de) { |
1b48773f PB |
6076 | ret = -ENOMEM; |
6077 | fail: | |
6078 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6079 | return true; |
6080 | } | |
2d28390a | 6081 | |
de0617e4 | 6082 | spin_lock_irq(&ctx->completion_lock); |
9cf7c104 | 6083 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
de0617e4 | 6084 | spin_unlock_irq(&ctx->completion_lock); |
27dc8338 | 6085 | kfree(de); |
ae34817b | 6086 | io_queue_async_work(req); |
76cc33d7 | 6087 | return true; |
de0617e4 JA |
6088 | } |
6089 | ||
915967f6 | 6090 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6091 | de->req = req; |
9cf7c104 | 6092 | de->seq = seq; |
27dc8338 | 6093 | list_add_tail(&de->list, &ctx->defer_list); |
de0617e4 | 6094 | spin_unlock_irq(&ctx->completion_lock); |
76cc33d7 | 6095 | return true; |
de0617e4 JA |
6096 | } |
6097 | ||
68fb8979 | 6098 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6099 | { |
0e1b6fe3 PB |
6100 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6101 | switch (req->opcode) { | |
6102 | case IORING_OP_READV: | |
6103 | case IORING_OP_READ_FIXED: | |
6104 | case IORING_OP_READ: | |
bcda7baa | 6105 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6106 | break; |
6107 | case IORING_OP_RECVMSG: | |
6108 | case IORING_OP_RECV: | |
bcda7baa | 6109 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6110 | break; |
6111 | } | |
99bc4c38 PB |
6112 | } |
6113 | ||
0e1b6fe3 PB |
6114 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6115 | switch (req->opcode) { | |
6116 | case IORING_OP_READV: | |
6117 | case IORING_OP_READ_FIXED: | |
6118 | case IORING_OP_READ: | |
6119 | case IORING_OP_WRITEV: | |
6120 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6121 | case IORING_OP_WRITE: { |
6122 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6123 | |
6124 | kfree(io->free_iovec); | |
0e1b6fe3 | 6125 | break; |
e8c2bc1f | 6126 | } |
0e1b6fe3 | 6127 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6128 | case IORING_OP_SENDMSG: { |
6129 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6130 | |
6131 | kfree(io->free_iov); | |
0e1b6fe3 | 6132 | break; |
e8c2bc1f | 6133 | } |
0e1b6fe3 PB |
6134 | case IORING_OP_SPLICE: |
6135 | case IORING_OP_TEE: | |
e1d767f0 PB |
6136 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6137 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6138 | break; |
f3cd4850 JA |
6139 | case IORING_OP_OPENAT: |
6140 | case IORING_OP_OPENAT2: | |
6141 | if (req->open.filename) | |
6142 | putname(req->open.filename); | |
6143 | break; | |
80a261fd JA |
6144 | case IORING_OP_RENAMEAT: |
6145 | putname(req->rename.oldpath); | |
6146 | putname(req->rename.newpath); | |
6147 | break; | |
14a1143b JA |
6148 | case IORING_OP_UNLINKAT: |
6149 | putname(req->unlink.filename); | |
6150 | break; | |
0e1b6fe3 | 6151 | } |
99bc4c38 | 6152 | } |
75652a30 JA |
6153 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6154 | kfree(req->apoll->double_poll); | |
6155 | kfree(req->apoll); | |
6156 | req->apoll = NULL; | |
6157 | } | |
3a0a6902 PB |
6158 | if (req->flags & REQ_F_INFLIGHT) { |
6159 | struct io_uring_task *tctx = req->task->io_uring; | |
6160 | ||
6161 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6162 | } |
c854357b | 6163 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6164 | put_cred(req->creds); |
c854357b PB |
6165 | |
6166 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6167 | } |
6168 | ||
889fca73 | 6169 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6170 | { |
a197f664 | 6171 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6172 | const struct cred *creds = NULL; |
d625c6ee | 6173 | int ret; |
2b188cc1 | 6174 | |
b8e64b53 | 6175 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6176 | creds = override_creds(req->creds); |
5730b27e | 6177 | |
d625c6ee | 6178 | switch (req->opcode) { |
2b188cc1 | 6179 | case IORING_OP_NOP: |
889fca73 | 6180 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6181 | break; |
6182 | case IORING_OP_READV: | |
edafccee | 6183 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6184 | case IORING_OP_READ: |
889fca73 | 6185 | ret = io_read(req, issue_flags); |
edafccee | 6186 | break; |
3529d8c2 | 6187 | case IORING_OP_WRITEV: |
edafccee | 6188 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6189 | case IORING_OP_WRITE: |
889fca73 | 6190 | ret = io_write(req, issue_flags); |
2b188cc1 | 6191 | break; |
c992fe29 | 6192 | case IORING_OP_FSYNC: |
45d189c6 | 6193 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6194 | break; |
221c5eb2 | 6195 | case IORING_OP_POLL_ADD: |
61e98203 | 6196 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6197 | break; |
6198 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6199 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6200 | break; |
5d17b4a4 | 6201 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6202 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6203 | break; |
0fa03c62 | 6204 | case IORING_OP_SENDMSG: |
889fca73 | 6205 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6206 | break; |
fddaface | 6207 | case IORING_OP_SEND: |
889fca73 | 6208 | ret = io_send(req, issue_flags); |
0fa03c62 | 6209 | break; |
aa1fa28f | 6210 | case IORING_OP_RECVMSG: |
889fca73 | 6211 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6212 | break; |
fddaface | 6213 | case IORING_OP_RECV: |
889fca73 | 6214 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6215 | break; |
5262f567 | 6216 | case IORING_OP_TIMEOUT: |
61e98203 | 6217 | ret = io_timeout(req, issue_flags); |
5262f567 | 6218 | break; |
11365043 | 6219 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6220 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6221 | break; |
17f2fe35 | 6222 | case IORING_OP_ACCEPT: |
889fca73 | 6223 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6224 | break; |
f8e85cf2 | 6225 | case IORING_OP_CONNECT: |
889fca73 | 6226 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6227 | break; |
62755e35 | 6228 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6229 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6230 | break; |
d63d1b5e | 6231 | case IORING_OP_FALLOCATE: |
45d189c6 | 6232 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6233 | break; |
15b71abe | 6234 | case IORING_OP_OPENAT: |
45d189c6 | 6235 | ret = io_openat(req, issue_flags); |
15b71abe | 6236 | break; |
b5dba59e | 6237 | case IORING_OP_CLOSE: |
889fca73 | 6238 | ret = io_close(req, issue_flags); |
b5dba59e | 6239 | break; |
05f3fb3c | 6240 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6241 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6242 | break; |
eddc7ef5 | 6243 | case IORING_OP_STATX: |
45d189c6 | 6244 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6245 | break; |
4840e418 | 6246 | case IORING_OP_FADVISE: |
45d189c6 | 6247 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6248 | break; |
c1ca757b | 6249 | case IORING_OP_MADVISE: |
45d189c6 | 6250 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6251 | break; |
cebdb986 | 6252 | case IORING_OP_OPENAT2: |
45d189c6 | 6253 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6254 | break; |
3e4827b0 | 6255 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6256 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6257 | break; |
7d67af2c | 6258 | case IORING_OP_SPLICE: |
45d189c6 | 6259 | ret = io_splice(req, issue_flags); |
7d67af2c | 6260 | break; |
ddf0322d | 6261 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6262 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6263 | break; |
067524e9 | 6264 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6265 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6266 | break; |
f2a8d5c7 | 6267 | case IORING_OP_TEE: |
45d189c6 | 6268 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6269 | break; |
36f4fa68 | 6270 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6271 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6272 | break; |
80a261fd | 6273 | case IORING_OP_RENAMEAT: |
45d189c6 | 6274 | ret = io_renameat(req, issue_flags); |
80a261fd | 6275 | break; |
14a1143b | 6276 | case IORING_OP_UNLINKAT: |
45d189c6 | 6277 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6278 | break; |
2b188cc1 JA |
6279 | default: |
6280 | ret = -EINVAL; | |
6281 | break; | |
6282 | } | |
6283 | ||
5730b27e JA |
6284 | if (creds) |
6285 | revert_creds(creds); | |
def596e9 JA |
6286 | if (ret) |
6287 | return ret; | |
b532576e | 6288 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6289 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6290 | io_iopoll_req_issued(req); | |
def596e9 JA |
6291 | |
6292 | return 0; | |
2b188cc1 JA |
6293 | } |
6294 | ||
ebc11b6c PB |
6295 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6296 | { | |
6297 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6298 | ||
6299 | req = io_put_req_find_next(req); | |
6300 | return req ? &req->work : NULL; | |
6301 | } | |
6302 | ||
5280f7e5 | 6303 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6304 | { |
6305 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6306 | struct io_kiocb *timeout; |
561fb04a | 6307 | int ret = 0; |
2b188cc1 | 6308 | |
6df1db6b PB |
6309 | timeout = io_prep_linked_timeout(req); |
6310 | if (timeout) | |
6311 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6312 | |
4014d943 | 6313 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6314 | ret = -ECANCELED; |
31b51510 | 6315 | |
561fb04a | 6316 | if (!ret) { |
561fb04a | 6317 | do { |
889fca73 | 6318 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6319 | /* |
6320 | * We can get EAGAIN for polled IO even though we're | |
6321 | * forcing a sync submission from here, since we can't | |
6322 | * wait for request slots on the block side. | |
6323 | */ | |
6324 | if (ret != -EAGAIN) | |
6325 | break; | |
6326 | cond_resched(); | |
6327 | } while (1); | |
6328 | } | |
31b51510 | 6329 | |
a3df7698 | 6330 | /* avoid locking problems by failing it from a clean context */ |
561fb04a | 6331 | if (ret) { |
a3df7698 | 6332 | /* io-wq is going to take one down */ |
de9b4cca | 6333 | req_ref_get(req); |
a3df7698 | 6334 | io_req_task_queue_fail(req, ret); |
edafccee | 6335 | } |
2b188cc1 JA |
6336 | } |
6337 | ||
aeca241b | 6338 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6339 | unsigned i) |
65e19f54 | 6340 | { |
042b0d85 | 6341 | return &table->files[i]; |
dafecf19 PB |
6342 | } |
6343 | ||
65e19f54 JA |
6344 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6345 | int index) | |
6346 | { | |
aeca241b | 6347 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6348 | |
a04b0ac0 | 6349 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6350 | } |
6351 | ||
a04b0ac0 | 6352 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6353 | { |
6354 | unsigned long file_ptr = (unsigned long) file; | |
6355 | ||
b191e2df | 6356 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6357 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6358 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6359 | file_ptr |= FFS_ASYNC_WRITE; |
6360 | if (S_ISREG(file_inode(file)->i_mode)) | |
6361 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6362 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6363 | } |
6364 | ||
ac177053 PB |
6365 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6366 | struct io_kiocb *req, int fd) | |
09bb8394 | 6367 | { |
8da11c19 | 6368 | struct file *file; |
ac177053 | 6369 | unsigned long file_ptr; |
09bb8394 | 6370 | |
ac177053 PB |
6371 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6372 | return NULL; | |
6373 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6374 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6375 | file = (struct file *) (file_ptr & FFS_MASK); | |
6376 | file_ptr &= ~FFS_MASK; | |
6377 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6378 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6379 | io_req_set_rsrc_node(req); |
6380 | return file; | |
6381 | } | |
d44f554e | 6382 | |
ac177053 PB |
6383 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
6384 | struct io_submit_state *state, | |
6385 | struct io_kiocb *req, int fd) | |
6386 | { | |
6387 | struct file *file = __io_file_get(state, fd); | |
6388 | ||
6389 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6390 | |
ac177053 PB |
6391 | /* we don't allow fixed io_uring files */ |
6392 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6393 | io_req_track_inflight(req); | |
8371adf5 | 6394 | return file; |
09bb8394 JA |
6395 | } |
6396 | ||
ac177053 PB |
6397 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
6398 | struct io_submit_state *state, | |
6399 | struct io_kiocb *req, int fd, bool fixed) | |
6400 | { | |
6401 | if (fixed) | |
6402 | return io_file_get_fixed(ctx, req, fd); | |
6403 | else | |
6404 | return io_file_get_normal(ctx, state, req, fd); | |
6405 | } | |
6406 | ||
2665abfd | 6407 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6408 | { |
ad8a48ac JA |
6409 | struct io_timeout_data *data = container_of(timer, |
6410 | struct io_timeout_data, timer); | |
90cd7e42 | 6411 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6412 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6413 | unsigned long flags; |
2665abfd JA |
6414 | |
6415 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
90cd7e42 PB |
6416 | prev = req->timeout.head; |
6417 | req->timeout.head = NULL; | |
2665abfd JA |
6418 | |
6419 | /* | |
6420 | * We don't expect the list to be empty, that will only happen if we | |
6421 | * race with the completion of the linked work. | |
6422 | */ | |
447c19f3 | 6423 | if (prev) { |
f2f87370 | 6424 | io_remove_next_linked(prev); |
447c19f3 PB |
6425 | if (!req_ref_inc_not_zero(prev)) |
6426 | prev = NULL; | |
6427 | } | |
2665abfd JA |
6428 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
6429 | ||
6430 | if (prev) { | |
014db007 | 6431 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
9ae1f8dd | 6432 | io_put_req_deferred(prev, 1); |
a298232e | 6433 | io_put_req_deferred(req, 1); |
47f46768 | 6434 | } else { |
9ae1f8dd | 6435 | io_req_complete_post(req, -ETIME, 0); |
2665abfd | 6436 | } |
2665abfd JA |
6437 | return HRTIMER_NORESTART; |
6438 | } | |
6439 | ||
de968c18 | 6440 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6441 | { |
de968c18 PB |
6442 | struct io_ring_ctx *ctx = req->ctx; |
6443 | ||
6444 | spin_lock_irq(&ctx->completion_lock); | |
76a46e06 | 6445 | /* |
f2f87370 PB |
6446 | * If the back reference is NULL, then our linked request finished |
6447 | * before we got a chance to setup the timer | |
76a46e06 | 6448 | */ |
90cd7e42 | 6449 | if (req->timeout.head) { |
e8c2bc1f | 6450 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6451 | |
ad8a48ac JA |
6452 | data->timer.function = io_link_timeout_fn; |
6453 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6454 | data->mode); | |
2665abfd | 6455 | } |
76a46e06 | 6456 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 6457 | /* drop submission reference */ |
76a46e06 JA |
6458 | io_put_req(req); |
6459 | } | |
2665abfd | 6460 | |
ad8a48ac | 6461 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd | 6462 | { |
f2f87370 | 6463 | struct io_kiocb *nxt = req->link; |
2665abfd | 6464 | |
f2f87370 PB |
6465 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
6466 | nxt->opcode != IORING_OP_LINK_TIMEOUT) | |
76a46e06 | 6467 | return NULL; |
2665abfd | 6468 | |
90cd7e42 | 6469 | nxt->timeout.head = req; |
900fad45 | 6470 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
76a46e06 | 6471 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 6472 | return nxt; |
2665abfd JA |
6473 | } |
6474 | ||
c5eef2b9 | 6475 | static void __io_queue_sqe(struct io_kiocb *req) |
2b188cc1 | 6476 | { |
d3d7298d | 6477 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
e0c5c576 | 6478 | int ret; |
2b188cc1 | 6479 | |
59b735ae | 6480 | issue_sqe: |
c5eef2b9 | 6481 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6482 | |
491381ce JA |
6483 | /* |
6484 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6485 | * doesn't support non-blocking read/write attempts | |
6486 | */ | |
1840038e | 6487 | if (likely(!ret)) { |
0d63c148 | 6488 | /* drop submission reference */ |
e342c807 | 6489 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 PB |
6490 | struct io_ring_ctx *ctx = req->ctx; |
6491 | struct io_comp_state *cs = &ctx->submit_state.comp; | |
e65ef56d | 6492 | |
6dd0be1e | 6493 | cs->reqs[cs->nr++] = req; |
d3d7298d | 6494 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
2a2758f2 | 6495 | io_submit_flush_completions(ctx); |
9affd664 | 6496 | } else { |
d3d7298d | 6497 | io_put_req(req); |
0d63c148 | 6498 | } |
1840038e | 6499 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
59b735ae OL |
6500 | switch (io_arm_poll_handler(req)) { |
6501 | case IO_APOLL_READY: | |
6502 | goto issue_sqe; | |
6503 | case IO_APOLL_ABORTED: | |
1840038e PB |
6504 | /* |
6505 | * Queued up for async execution, worker will release | |
6506 | * submit reference when the iocb is actually submitted. | |
6507 | */ | |
6508 | io_queue_async_work(req); | |
59b735ae | 6509 | break; |
1840038e | 6510 | } |
0d63c148 | 6511 | } else { |
f41db273 | 6512 | io_req_complete_failed(req, ret); |
9e645e11 | 6513 | } |
d3d7298d PB |
6514 | if (linked_timeout) |
6515 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6516 | } |
6517 | ||
441b8a78 | 6518 | static inline void io_queue_sqe(struct io_kiocb *req) |
4fe2c963 | 6519 | { |
10c66904 | 6520 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6521 | return; |
4fe2c963 | 6522 | |
76cc33d7 | 6523 | if (likely(!(req->flags & REQ_F_FORCE_ASYNC))) { |
c5eef2b9 | 6524 | __io_queue_sqe(req); |
76cc33d7 PB |
6525 | } else { |
6526 | int ret = io_req_prep_async(req); | |
6527 | ||
6528 | if (unlikely(ret)) | |
6529 | io_req_complete_failed(req, ret); | |
6530 | else | |
6531 | io_queue_async_work(req); | |
ce35a47a | 6532 | } |
4fe2c963 JL |
6533 | } |
6534 | ||
b16fed66 PB |
6535 | /* |
6536 | * Check SQE restrictions (opcode and flags). | |
6537 | * | |
6538 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6539 | */ | |
6540 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6541 | struct io_kiocb *req, | |
6542 | unsigned int sqe_flags) | |
4fe2c963 | 6543 | { |
4cfb25bf | 6544 | if (likely(!ctx->restricted)) |
b16fed66 PB |
6545 | return true; |
6546 | ||
6547 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6548 | return false; | |
6549 | ||
6550 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6551 | ctx->restrictions.sqe_flags_required) | |
6552 | return false; | |
6553 | ||
6554 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6555 | ctx->restrictions.sqe_flags_required)) | |
6556 | return false; | |
6557 | ||
6558 | return true; | |
4fe2c963 JL |
6559 | } |
6560 | ||
b16fed66 PB |
6561 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6562 | const struct io_uring_sqe *sqe) | |
6563 | { | |
6564 | struct io_submit_state *state; | |
6565 | unsigned int sqe_flags; | |
003e8dcc | 6566 | int personality, ret = 0; |
b16fed66 | 6567 | |
864ea921 | 6568 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6569 | req->opcode = READ_ONCE(sqe->opcode); |
6570 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6571 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6572 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6573 | req->file = NULL; |
b16fed66 PB |
6574 | req->fixed_rsrc_refs = NULL; |
6575 | /* one is dropped after submission, the other at completion */ | |
abc54d63 | 6576 | atomic_set(&req->refs, 2); |
b16fed66 | 6577 | req->task = current; |
b16fed66 PB |
6578 | |
6579 | /* enforce forwards compatibility on users */ | |
dddca226 | 6580 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 6581 | return -EINVAL; |
b16fed66 PB |
6582 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6583 | return -EINVAL; | |
4cfb25bf | 6584 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
6585 | return -EACCES; |
6586 | ||
6587 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6588 | !io_op_defs[req->opcode].buffer_select) | |
6589 | return -EOPNOTSUPP; | |
3c19966d PB |
6590 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
6591 | ctx->drain_active = true; | |
863e0560 | 6592 | |
003e8dcc JA |
6593 | personality = READ_ONCE(sqe->personality); |
6594 | if (personality) { | |
c10d1f98 PB |
6595 | req->creds = xa_load(&ctx->personalities, personality); |
6596 | if (!req->creds) | |
003e8dcc | 6597 | return -EINVAL; |
c10d1f98 | 6598 | get_cred(req->creds); |
b8e64b53 | 6599 | req->flags |= REQ_F_CREDS; |
003e8dcc | 6600 | } |
b16fed66 PB |
6601 | state = &ctx->submit_state; |
6602 | ||
6603 | /* | |
6604 | * Plug now if we have more than 1 IO left after this, and the target | |
6605 | * is potentially a read/write to block based storage. | |
6606 | */ | |
6607 | if (!state->plug_started && state->ios_left > 1 && | |
6608 | io_op_defs[req->opcode].plug) { | |
6609 | blk_start_plug(&state->plug); | |
6610 | state->plug_started = true; | |
6611 | } | |
6612 | ||
6613 | if (io_op_defs[req->opcode].needs_file) { | |
ac177053 PB |
6614 | req->file = io_file_get(ctx, state, req, READ_ONCE(sqe->fd), |
6615 | (sqe_flags & IOSQE_FIXED_FILE)); | |
b16fed66 PB |
6616 | if (unlikely(!req->file)) |
6617 | ret = -EBADF; | |
6618 | } | |
6619 | ||
6620 | state->ios_left--; | |
6621 | return ret; | |
6622 | } | |
6623 | ||
a6b8cadc | 6624 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6625 | const struct io_uring_sqe *sqe) |
9e645e11 | 6626 | { |
a1ab7b35 | 6627 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6628 | int ret; |
9e645e11 | 6629 | |
a6b8cadc PB |
6630 | ret = io_init_req(ctx, req, sqe); |
6631 | if (unlikely(ret)) { | |
6632 | fail_req: | |
de59bc10 PB |
6633 | if (link->head) { |
6634 | /* fail even hard links since we don't submit */ | |
93d2bcd2 | 6635 | req_set_fail(link->head); |
f41db273 | 6636 | io_req_complete_failed(link->head, -ECANCELED); |
de59bc10 PB |
6637 | link->head = NULL; |
6638 | } | |
f41db273 | 6639 | io_req_complete_failed(req, ret); |
a6b8cadc PB |
6640 | return ret; |
6641 | } | |
441b8a78 | 6642 | |
be7053b7 PB |
6643 | ret = io_req_prep(req, sqe); |
6644 | if (unlikely(ret)) | |
6645 | goto fail_req; | |
a6b8cadc | 6646 | |
be7053b7 | 6647 | /* don't need @sqe from now on */ |
236daeae OL |
6648 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
6649 | req->flags, true, | |
6650 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 6651 | |
9e645e11 JA |
6652 | /* |
6653 | * If we already have a head request, queue this one for async | |
6654 | * submittal once the head completes. If we don't have a head but | |
6655 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6656 | * submitted sync once the chain is complete. If none of those | |
6657 | * conditions are true (normal request), then just queue it. | |
6658 | */ | |
863e0560 PB |
6659 | if (link->head) { |
6660 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6661 | |
b7e298d2 | 6662 | ret = io_req_prep_async(req); |
cf109604 | 6663 | if (unlikely(ret)) |
a6b8cadc | 6664 | goto fail_req; |
9d76377f | 6665 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6666 | link->last->link = req; |
863e0560 | 6667 | link->last = req; |
32fe525b PB |
6668 | |
6669 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6670 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 6671 | link->head = NULL; |
5e159204 | 6672 | io_queue_sqe(head); |
32fe525b | 6673 | } |
9e645e11 | 6674 | } else { |
ef4ff581 | 6675 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6676 | link->head = req; |
6677 | link->last = req; | |
711be031 | 6678 | } else { |
be7053b7 | 6679 | io_queue_sqe(req); |
711be031 | 6680 | } |
9e645e11 | 6681 | } |
2e6e1fde | 6682 | |
1d4240cc | 6683 | return 0; |
9e645e11 JA |
6684 | } |
6685 | ||
9a56a232 JA |
6686 | /* |
6687 | * Batched submission is done, ensure local IO is flushed out. | |
6688 | */ | |
ba88ff11 PB |
6689 | static void io_submit_state_end(struct io_submit_state *state, |
6690 | struct io_ring_ctx *ctx) | |
9a56a232 | 6691 | { |
a1ab7b35 | 6692 | if (state->link.head) |
de59bc10 | 6693 | io_queue_sqe(state->link.head); |
6dd0be1e | 6694 | if (state->comp.nr) |
2a2758f2 | 6695 | io_submit_flush_completions(ctx); |
27926b68 JA |
6696 | if (state->plug_started) |
6697 | blk_finish_plug(&state->plug); | |
9f13c35b | 6698 | io_state_file_put(state); |
9a56a232 JA |
6699 | } |
6700 | ||
6701 | /* | |
6702 | * Start submission side cache. | |
6703 | */ | |
6704 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6705 | unsigned int max_ios) |
9a56a232 | 6706 | { |
27926b68 | 6707 | state->plug_started = false; |
9a56a232 | 6708 | state->ios_left = max_ios; |
a1ab7b35 PB |
6709 | /* set only head, no need to init link_last in advance */ |
6710 | state->link.head = NULL; | |
9a56a232 JA |
6711 | } |
6712 | ||
2b188cc1 JA |
6713 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6714 | { | |
75b28aff | 6715 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6716 | |
caf582c6 PB |
6717 | /* |
6718 | * Ensure any loads from the SQEs are done at this point, | |
6719 | * since once we write the new head, the application could | |
6720 | * write new data to them. | |
6721 | */ | |
6722 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6723 | } |
6724 | ||
2b188cc1 | 6725 | /* |
dd9ae8a0 | 6726 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
6727 | * that is mapped by userspace. This means that care needs to be taken to |
6728 | * ensure that reads are stable, as we cannot rely on userspace always | |
6729 | * being a good citizen. If members of the sqe are validated and then later | |
6730 | * used, it's important that those reads are done through READ_ONCE() to | |
6731 | * prevent a re-load down the line. | |
6732 | */ | |
709b302f | 6733 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6734 | { |
ea5ab3b5 | 6735 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 6736 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
6737 | |
6738 | /* | |
6739 | * The cached sq head (or cq tail) serves two purposes: | |
6740 | * | |
6741 | * 1) allows us to batch the cost of updating the user visible | |
6742 | * head updates. | |
6743 | * 2) allows the kernel side to track the head on its own, even | |
6744 | * though the application is the one updating it. | |
6745 | */ | |
17d3aeb3 | 6746 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
6747 | if (likely(head < ctx->sq_entries)) |
6748 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6749 | |
6750 | /* drop invalid entries */ | |
15641e42 PB |
6751 | ctx->cq_extra--; |
6752 | WRITE_ONCE(ctx->rings->sq_dropped, | |
6753 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
6754 | return NULL; |
6755 | } | |
6756 | ||
0f212204 | 6757 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
6c271ce2 | 6758 | { |
09899b19 | 6759 | struct io_uring_task *tctx; |
46c4e16a | 6760 | int submitted = 0; |
6c271ce2 | 6761 | |
ee7d46d9 PB |
6762 | /* make sure SQ entry isn't read before tail */ |
6763 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
6764 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6765 | return -EAGAIN; | |
6c271ce2 | 6766 | |
09899b19 PB |
6767 | tctx = current->io_uring; |
6768 | tctx->cached_refs -= nr; | |
6769 | if (unlikely(tctx->cached_refs < 0)) { | |
6770 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
6771 | ||
6772 | percpu_counter_add(&tctx->inflight, refill); | |
6773 | refcount_add(refill, ¤t->usage); | |
6774 | tctx->cached_refs += refill; | |
6775 | } | |
ba88ff11 | 6776 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6777 | |
46c4e16a | 6778 | while (submitted < nr) { |
3529d8c2 | 6779 | const struct io_uring_sqe *sqe; |
196be95c | 6780 | struct io_kiocb *req; |
fb5ccc98 | 6781 | |
258b29a9 | 6782 | req = io_alloc_req(ctx); |
196be95c PB |
6783 | if (unlikely(!req)) { |
6784 | if (!submitted) | |
6785 | submitted = -EAGAIN; | |
fb5ccc98 | 6786 | break; |
196be95c | 6787 | } |
4fccfcbb PB |
6788 | sqe = io_get_sqe(ctx); |
6789 | if (unlikely(!sqe)) { | |
6790 | kmem_cache_free(req_cachep, req); | |
6791 | break; | |
6792 | } | |
d3656344 JA |
6793 | /* will complete beyond this point, count as submitted */ |
6794 | submitted++; | |
a1ab7b35 | 6795 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6796 | break; |
6c271ce2 JA |
6797 | } |
6798 | ||
9466f437 PB |
6799 | if (unlikely(submitted != nr)) { |
6800 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 6801 | int unused = nr - ref_used; |
9466f437 | 6802 | |
09899b19 | 6803 | current->io_uring->cached_refs += unused; |
d8a6df10 | 6804 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 6805 | } |
6c271ce2 | 6806 | |
a1ab7b35 | 6807 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6808 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6809 | io_commit_sqring(ctx); | |
6810 | ||
6c271ce2 JA |
6811 | return submitted; |
6812 | } | |
6813 | ||
e4b6d902 PB |
6814 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
6815 | { | |
6816 | return READ_ONCE(sqd->state); | |
6817 | } | |
6818 | ||
23b3628e XW |
6819 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6820 | { | |
6821 | /* Tell userspace we may need a wakeup call */ | |
6822 | spin_lock_irq(&ctx->completion_lock); | |
20c0b380 NA |
6823 | WRITE_ONCE(ctx->rings->sq_flags, |
6824 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
23b3628e XW |
6825 | spin_unlock_irq(&ctx->completion_lock); |
6826 | } | |
6827 | ||
6828 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6829 | { | |
6830 | spin_lock_irq(&ctx->completion_lock); | |
20c0b380 NA |
6831 | WRITE_ONCE(ctx->rings->sq_flags, |
6832 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
23b3628e XW |
6833 | spin_unlock_irq(&ctx->completion_lock); |
6834 | } | |
6835 | ||
08369246 | 6836 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6837 | { |
c8d1ba58 | 6838 | unsigned int to_submit; |
bdcd3eab | 6839 | int ret = 0; |
6c271ce2 | 6840 | |
c8d1ba58 | 6841 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 6842 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
6843 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
6844 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 6845 | |
906a3c6f | 6846 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6847 | unsigned nr_events = 0; |
948e1947 PB |
6848 | const struct cred *creds = NULL; |
6849 | ||
6850 | if (ctx->sq_creds != current_cred()) | |
6851 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 6852 | |
c8d1ba58 | 6853 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6854 | if (!list_empty(&ctx->iopoll_list)) |
3c30ef0f | 6855 | io_do_iopoll(ctx, &nr_events, 0, true); |
906a3c6f | 6856 | |
3b763ba1 PB |
6857 | /* |
6858 | * Don't submit if refs are dying, good for io_uring_register(), | |
6859 | * but also it is relied upon by io_ring_exit_work() | |
6860 | */ | |
0298ef96 PB |
6861 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
6862 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 6863 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 6864 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 6865 | |
acfb381d PB |
6866 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6867 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
6868 | if (creds) |
6869 | revert_creds(creds); | |
acfb381d | 6870 | } |
6c271ce2 | 6871 | |
08369246 XW |
6872 | return ret; |
6873 | } | |
6c271ce2 | 6874 | |
08369246 XW |
6875 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6876 | { | |
6877 | struct io_ring_ctx *ctx; | |
6878 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6879 | |
c9dca27d PB |
6880 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6881 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 6882 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6883 | } |
6c271ce2 | 6884 | |
e4b6d902 PB |
6885 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
6886 | { | |
6887 | bool did_sig = false; | |
6888 | struct ksignal ksig; | |
6889 | ||
6890 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
6891 | signal_pending(current)) { | |
6892 | mutex_unlock(&sqd->lock); | |
6893 | if (signal_pending(current)) | |
6894 | did_sig = get_signal(&ksig); | |
6895 | cond_resched(); | |
6896 | mutex_lock(&sqd->lock); | |
6897 | } | |
e4b6d902 PB |
6898 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
6899 | } | |
6900 | ||
c8d1ba58 JA |
6901 | static int io_sq_thread(void *data) |
6902 | { | |
69fb2131 JA |
6903 | struct io_sq_data *sqd = data; |
6904 | struct io_ring_ctx *ctx; | |
a0d9205f | 6905 | unsigned long timeout = 0; |
37d1e2e3 | 6906 | char buf[TASK_COMM_LEN]; |
08369246 | 6907 | DEFINE_WAIT(wait); |
6c271ce2 | 6908 | |
696ee88a | 6909 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 6910 | set_task_comm(current, buf); |
37d1e2e3 JA |
6911 | |
6912 | if (sqd->sq_cpu != -1) | |
6913 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6914 | else | |
6915 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6916 | current->flags |= PF_NO_SETAFFINITY; | |
6917 | ||
09a6f4ef | 6918 | mutex_lock(&sqd->lock); |
e4b6d902 | 6919 | while (1) { |
1a924a80 | 6920 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 6921 | |
e4b6d902 PB |
6922 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
6923 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 6924 | break; |
08369246 XW |
6925 | timeout = jiffies + sqd->sq_thread_idle; |
6926 | } | |
e4b6d902 | 6927 | |
e95eee2d | 6928 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6929 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 6930 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 6931 | |
08369246 XW |
6932 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
6933 | sqt_spin = true; | |
69fb2131 | 6934 | } |
dd432ea5 PB |
6935 | if (io_run_task_work()) |
6936 | sqt_spin = true; | |
6c271ce2 | 6937 | |
08369246 | 6938 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 6939 | cond_resched(); |
08369246 XW |
6940 | if (sqt_spin) |
6941 | timeout = jiffies + sqd->sq_thread_idle; | |
6942 | continue; | |
6943 | } | |
6944 | ||
08369246 | 6945 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 6946 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
6947 | bool needs_sched = true; |
6948 | ||
724cb4f9 | 6949 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
6950 | io_ring_set_wakeup_flag(ctx); |
6951 | ||
724cb4f9 HX |
6952 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
6953 | !list_empty_careful(&ctx->iopoll_list)) { | |
6954 | needs_sched = false; | |
6955 | break; | |
6956 | } | |
6957 | if (io_sqring_entries(ctx)) { | |
6958 | needs_sched = false; | |
6959 | break; | |
6960 | } | |
6961 | } | |
6962 | ||
6963 | if (needs_sched) { | |
6964 | mutex_unlock(&sqd->lock); | |
6965 | schedule(); | |
6966 | mutex_lock(&sqd->lock); | |
6967 | } | |
69fb2131 JA |
6968 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6969 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6970 | } |
08369246 XW |
6971 | |
6972 | finish_wait(&sqd->wait, &wait); | |
6973 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 6974 | } |
28cea78a | 6975 | |
78cc687b | 6976 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 6977 | sqd->thread = NULL; |
05962f95 | 6978 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 6979 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 6980 | io_run_task_work(); |
734551df PB |
6981 | mutex_unlock(&sqd->lock); |
6982 | ||
37d1e2e3 JA |
6983 | complete(&sqd->exited); |
6984 | do_exit(0); | |
6c271ce2 JA |
6985 | } |
6986 | ||
bda52162 JA |
6987 | struct io_wait_queue { |
6988 | struct wait_queue_entry wq; | |
6989 | struct io_ring_ctx *ctx; | |
5fd46178 | 6990 | unsigned cq_tail; |
bda52162 JA |
6991 | unsigned nr_timeouts; |
6992 | }; | |
6993 | ||
6c503150 | 6994 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
6995 | { |
6996 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 6997 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
6998 | |
6999 | /* | |
d195a66e | 7000 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7001 | * started waiting. For timeouts, we always want to return to userspace, |
7002 | * regardless of event count. | |
7003 | */ | |
5fd46178 | 7004 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7005 | } |
7006 | ||
7007 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7008 | int wake_flags, void *key) | |
7009 | { | |
7010 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7011 | wq); | |
7012 | ||
6c503150 PB |
7013 | /* |
7014 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7015 | * the task, and the next invocation will do it. | |
7016 | */ | |
5ed7a37d | 7017 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7018 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7019 | return -1; | |
bda52162 JA |
7020 | } |
7021 | ||
af9c1a44 JA |
7022 | static int io_run_task_work_sig(void) |
7023 | { | |
7024 | if (io_run_task_work()) | |
7025 | return 1; | |
7026 | if (!signal_pending(current)) | |
7027 | return 0; | |
0b8cfa97 | 7028 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7029 | return -ERESTARTSYS; |
af9c1a44 JA |
7030 | return -EINTR; |
7031 | } | |
7032 | ||
eeb60b9a PB |
7033 | /* when returns >0, the caller should retry */ |
7034 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7035 | struct io_wait_queue *iowq, | |
7036 | signed long *timeout) | |
7037 | { | |
7038 | int ret; | |
7039 | ||
7040 | /* make sure we run task_work before checking for signals */ | |
7041 | ret = io_run_task_work_sig(); | |
7042 | if (ret || io_should_wake(iowq)) | |
7043 | return ret; | |
7044 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7045 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7046 | return 1; |
7047 | ||
7048 | *timeout = schedule_timeout(*timeout); | |
7049 | return !*timeout ? -ETIME : 1; | |
7050 | } | |
7051 | ||
2b188cc1 JA |
7052 | /* |
7053 | * Wait until events become available, if we don't already have some. The | |
7054 | * application must reap them itself, as they reside on the shared cq ring. | |
7055 | */ | |
7056 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7057 | const sigset_t __user *sig, size_t sigsz, |
7058 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7059 | { |
bda52162 JA |
7060 | struct io_wait_queue iowq = { |
7061 | .wq = { | |
7062 | .private = current, | |
7063 | .func = io_wake_function, | |
7064 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
7065 | }, | |
7066 | .ctx = ctx, | |
bda52162 | 7067 | }; |
75b28aff | 7068 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7069 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7070 | int ret; | |
2b188cc1 | 7071 | |
b41e9852 | 7072 | do { |
6c2450ae | 7073 | io_cqring_overflow_flush(ctx, false); |
6c503150 | 7074 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7075 | return 0; |
4c6e277c | 7076 | if (!io_run_task_work()) |
b41e9852 | 7077 | break; |
b41e9852 | 7078 | } while (1); |
2b188cc1 JA |
7079 | |
7080 | if (sig) { | |
9e75ad5d AB |
7081 | #ifdef CONFIG_COMPAT |
7082 | if (in_compat_syscall()) | |
7083 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7084 | sigsz); |
9e75ad5d AB |
7085 | else |
7086 | #endif | |
b772434b | 7087 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7088 | |
2b188cc1 JA |
7089 | if (ret) |
7090 | return ret; | |
7091 | } | |
7092 | ||
c73ebb68 | 7093 | if (uts) { |
c1d5a224 PB |
7094 | struct timespec64 ts; |
7095 | ||
c73ebb68 HX |
7096 | if (get_timespec64(&ts, uts)) |
7097 | return -EFAULT; | |
7098 | timeout = timespec64_to_jiffies(&ts); | |
7099 | } | |
7100 | ||
bda52162 | 7101 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7102 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
c826bd7a | 7103 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7104 | do { |
ca0a2651 | 7105 | /* if we can't even flush overflow, don't wait for more */ |
6c2450ae | 7106 | if (!io_cqring_overflow_flush(ctx, false)) { |
ca0a2651 JA |
7107 | ret = -EBUSY; |
7108 | break; | |
7109 | } | |
311997b3 | 7110 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7111 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7112 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7113 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7114 | cond_resched(); |
eeb60b9a | 7115 | } while (ret > 0); |
bda52162 | 7116 | |
b7db41c9 | 7117 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7118 | |
75b28aff | 7119 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7120 | } |
7121 | ||
9123c8ff | 7122 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7123 | { |
9123c8ff | 7124 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7125 | |
846a4ef2 | 7126 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7127 | kfree(table[i]); |
7128 | kfree(table); | |
7129 | } | |
7130 | ||
7131 | static void **io_alloc_page_table(size_t size) | |
7132 | { | |
7133 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7134 | size_t init_size = size; | |
7135 | void **table; | |
7136 | ||
7137 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL); | |
7138 | if (!table) | |
7139 | return NULL; | |
7140 | ||
7141 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7142 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff PB |
7143 | |
7144 | table[i] = kzalloc(this_size, GFP_KERNEL); | |
7145 | if (!table[i]) { | |
7146 | io_free_page_table(table, init_size); | |
7147 | return NULL; | |
7148 | } | |
7149 | size -= this_size; | |
7150 | } | |
7151 | return table; | |
05f3fb3c JA |
7152 | } |
7153 | ||
28a9fe25 | 7154 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7155 | { |
28a9fe25 PB |
7156 | percpu_ref_exit(&ref_node->refs); |
7157 | kfree(ref_node); | |
1642b445 PB |
7158 | } |
7159 | ||
a7f0ed5a PB |
7160 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7161 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7162 | { |
a7f0ed5a PB |
7163 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7164 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7165 | |
a7f0ed5a PB |
7166 | if (data_to_kill) { |
7167 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7168 | |
a7f0ed5a | 7169 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7170 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7171 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7172 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7173 | |
3e942498 | 7174 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7175 | percpu_ref_kill(&rsrc_node->refs); |
7176 | ctx->rsrc_node = NULL; | |
7177 | } | |
6b06314c | 7178 | |
a7f0ed5a PB |
7179 | if (!ctx->rsrc_node) { |
7180 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7181 | ctx->rsrc_backup_node = NULL; | |
7182 | } | |
8bad28d8 HX |
7183 | } |
7184 | ||
a7f0ed5a | 7185 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7186 | { |
7187 | if (ctx->rsrc_backup_node) | |
7188 | return 0; | |
b895c9a6 | 7189 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7190 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7191 | } |
7192 | ||
40ae0ff7 | 7193 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7194 | { |
7195 | int ret; | |
05589553 | 7196 | |
215c3902 | 7197 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7198 | if (data->quiesce) |
7199 | return -ENXIO; | |
05589553 | 7200 | |
8bad28d8 | 7201 | data->quiesce = true; |
1ffc5422 | 7202 | do { |
a7f0ed5a | 7203 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7204 | if (ret) |
f2303b1f | 7205 | break; |
a7f0ed5a | 7206 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7207 | |
3e942498 PB |
7208 | /* kill initial ref, already quiesced if zero */ |
7209 | if (atomic_dec_and_test(&data->refs)) | |
7210 | break; | |
c018db4a | 7211 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7212 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7213 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7214 | if (!ret) { |
7215 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7216 | break; |
c018db4a | 7217 | } |
8bad28d8 | 7218 | |
3e942498 PB |
7219 | atomic_inc(&data->refs); |
7220 | /* wait for all works potentially completing data->done */ | |
7221 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7222 | reinit_completion(&data->done); |
8dd03afe | 7223 | |
1ffc5422 | 7224 | ret = io_run_task_work_sig(); |
8bad28d8 | 7225 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7226 | } while (ret >= 0); |
8bad28d8 | 7227 | data->quiesce = false; |
05f3fb3c | 7228 | |
8bad28d8 | 7229 | return ret; |
d7954b2b BM |
7230 | } |
7231 | ||
2d091d62 PB |
7232 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7233 | { | |
7234 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7235 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7236 | ||
7237 | return &data->tags[table_idx][off]; | |
7238 | } | |
7239 | ||
44b31f2f | 7240 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7241 | { |
2d091d62 PB |
7242 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7243 | ||
7244 | if (data->tags) | |
7245 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7246 | kfree(data); |
7247 | } | |
7248 | ||
d878c816 PB |
7249 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7250 | u64 __user *utags, unsigned nr, | |
7251 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7252 | { |
b895c9a6 | 7253 | struct io_rsrc_data *data; |
2d091d62 | 7254 | int ret = -ENOMEM; |
d878c816 | 7255 | unsigned i; |
1ad555c6 BM |
7256 | |
7257 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7258 | if (!data) | |
d878c816 | 7259 | return -ENOMEM; |
2d091d62 | 7260 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7261 | if (!data->tags) { |
1ad555c6 | 7262 | kfree(data); |
d878c816 PB |
7263 | return -ENOMEM; |
7264 | } | |
2d091d62 PB |
7265 | |
7266 | data->nr = nr; | |
7267 | data->ctx = ctx; | |
7268 | data->do_put = do_put; | |
d878c816 | 7269 | if (utags) { |
2d091d62 | 7270 | ret = -EFAULT; |
d878c816 | 7271 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7272 | u64 *tag_slot = io_get_tag_slot(data, i); |
7273 | ||
7274 | if (copy_from_user(tag_slot, &utags[i], | |
7275 | sizeof(*tag_slot))) | |
2d091d62 | 7276 | goto fail; |
d878c816 | 7277 | } |
1ad555c6 | 7278 | } |
b60c8dce | 7279 | |
3e942498 | 7280 | atomic_set(&data->refs, 1); |
1ad555c6 | 7281 | init_completion(&data->done); |
d878c816 PB |
7282 | *pdata = data; |
7283 | return 0; | |
2d091d62 PB |
7284 | fail: |
7285 | io_rsrc_data_free(data); | |
7286 | return ret; | |
1ad555c6 BM |
7287 | } |
7288 | ||
9123c8ff PB |
7289 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7290 | { | |
042b0d85 | 7291 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), GFP_KERNEL); |
9123c8ff PB |
7292 | return !!table->files; |
7293 | } | |
7294 | ||
042b0d85 | 7295 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7296 | { |
042b0d85 | 7297 | kvfree(table->files); |
9123c8ff PB |
7298 | table->files = NULL; |
7299 | } | |
7300 | ||
fff4db76 | 7301 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7302 | { |
fff4db76 PB |
7303 | #if defined(CONFIG_UNIX) |
7304 | if (ctx->ring_sock) { | |
7305 | struct sock *sock = ctx->ring_sock->sk; | |
7306 | struct sk_buff *skb; | |
7307 | ||
7308 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7309 | kfree_skb(skb); | |
7310 | } | |
7311 | #else | |
7312 | int i; | |
7313 | ||
7314 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7315 | struct file *file; | |
7316 | ||
7317 | file = io_file_from_index(ctx, i); | |
7318 | if (file) | |
7319 | fput(file); | |
7320 | } | |
7321 | #endif | |
042b0d85 | 7322 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7323 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7324 | ctx->file_data = NULL; |
7325 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7326 | } |
7327 | ||
d7954b2b BM |
7328 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7329 | { | |
d7954b2b BM |
7330 | int ret; |
7331 | ||
08480400 | 7332 | if (!ctx->file_data) |
d7954b2b | 7333 | return -ENXIO; |
08480400 PB |
7334 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7335 | if (!ret) | |
7336 | __io_sqe_files_unregister(ctx); | |
7337 | return ret; | |
6b06314c JA |
7338 | } |
7339 | ||
37d1e2e3 | 7340 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7341 | __releases(&sqd->lock) |
37d1e2e3 | 7342 | { |
521d6a73 PB |
7343 | WARN_ON_ONCE(sqd->thread == current); |
7344 | ||
9e138a48 PB |
7345 | /* |
7346 | * Do the dance but not conditional clear_bit() because it'd race with | |
7347 | * other threads incrementing park_pending and setting the bit. | |
7348 | */ | |
37d1e2e3 | 7349 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7350 | if (atomic_dec_return(&sqd->park_pending)) |
7351 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7352 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7353 | } |
7354 | ||
86e0d676 | 7355 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7356 | __acquires(&sqd->lock) |
37d1e2e3 | 7357 | { |
521d6a73 PB |
7358 | WARN_ON_ONCE(sqd->thread == current); |
7359 | ||
9e138a48 | 7360 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7361 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7362 | mutex_lock(&sqd->lock); |
05962f95 | 7363 | if (sqd->thread) |
86e0d676 | 7364 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7365 | } |
7366 | ||
7367 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7368 | { | |
521d6a73 | 7369 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7370 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7371 | |
05962f95 | 7372 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7373 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7374 | if (sqd->thread) |
7375 | wake_up_process(sqd->thread); | |
09a6f4ef | 7376 | mutex_unlock(&sqd->lock); |
05962f95 | 7377 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7378 | } |
7379 | ||
534ca6d6 | 7380 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7381 | { |
534ca6d6 | 7382 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7383 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7384 | ||
37d1e2e3 JA |
7385 | io_sq_thread_stop(sqd); |
7386 | kfree(sqd); | |
7387 | } | |
7388 | } | |
7389 | ||
7390 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7391 | { | |
7392 | struct io_sq_data *sqd = ctx->sq_data; | |
7393 | ||
7394 | if (sqd) { | |
05962f95 | 7395 | io_sq_thread_park(sqd); |
521d6a73 | 7396 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7397 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7398 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7399 | |
7400 | io_put_sq_data(sqd); | |
7401 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7402 | } |
7403 | } | |
7404 | ||
aa06165d JA |
7405 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7406 | { | |
7407 | struct io_ring_ctx *ctx_attach; | |
7408 | struct io_sq_data *sqd; | |
7409 | struct fd f; | |
7410 | ||
7411 | f = fdget(p->wq_fd); | |
7412 | if (!f.file) | |
7413 | return ERR_PTR(-ENXIO); | |
7414 | if (f.file->f_op != &io_uring_fops) { | |
7415 | fdput(f); | |
7416 | return ERR_PTR(-EINVAL); | |
7417 | } | |
7418 | ||
7419 | ctx_attach = f.file->private_data; | |
7420 | sqd = ctx_attach->sq_data; | |
7421 | if (!sqd) { | |
7422 | fdput(f); | |
7423 | return ERR_PTR(-EINVAL); | |
7424 | } | |
5c2469e0 JA |
7425 | if (sqd->task_tgid != current->tgid) { |
7426 | fdput(f); | |
7427 | return ERR_PTR(-EPERM); | |
7428 | } | |
aa06165d JA |
7429 | |
7430 | refcount_inc(&sqd->refs); | |
7431 | fdput(f); | |
7432 | return sqd; | |
7433 | } | |
7434 | ||
26984fbf PB |
7435 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7436 | bool *attached) | |
534ca6d6 JA |
7437 | { |
7438 | struct io_sq_data *sqd; | |
7439 | ||
26984fbf | 7440 | *attached = false; |
5c2469e0 JA |
7441 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7442 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7443 | if (!IS_ERR(sqd)) { |
7444 | *attached = true; | |
5c2469e0 | 7445 | return sqd; |
26984fbf | 7446 | } |
5c2469e0 JA |
7447 | /* fall through for EPERM case, setup new sqd/task */ |
7448 | if (PTR_ERR(sqd) != -EPERM) | |
7449 | return sqd; | |
7450 | } | |
aa06165d | 7451 | |
534ca6d6 JA |
7452 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7453 | if (!sqd) | |
7454 | return ERR_PTR(-ENOMEM); | |
7455 | ||
9e138a48 | 7456 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7457 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7458 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7459 | mutex_init(&sqd->lock); |
534ca6d6 | 7460 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7461 | init_completion(&sqd->exited); |
534ca6d6 JA |
7462 | return sqd; |
7463 | } | |
7464 | ||
6b06314c | 7465 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7466 | /* |
7467 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7468 | * the io_uring can be safely unregistered on process exit, even if we have | |
7469 | * loops in the file referencing. | |
7470 | */ | |
7471 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7472 | { | |
7473 | struct sock *sk = ctx->ring_sock->sk; | |
7474 | struct scm_fp_list *fpl; | |
7475 | struct sk_buff *skb; | |
08a45173 | 7476 | int i, nr_files; |
6b06314c | 7477 | |
6b06314c JA |
7478 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7479 | if (!fpl) | |
7480 | return -ENOMEM; | |
7481 | ||
7482 | skb = alloc_skb(0, GFP_KERNEL); | |
7483 | if (!skb) { | |
7484 | kfree(fpl); | |
7485 | return -ENOMEM; | |
7486 | } | |
7487 | ||
7488 | skb->sk = sk; | |
6b06314c | 7489 | |
08a45173 | 7490 | nr_files = 0; |
62e398be | 7491 | fpl->user = get_uid(current_user()); |
6b06314c | 7492 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7493 | struct file *file = io_file_from_index(ctx, i + offset); |
7494 | ||
7495 | if (!file) | |
08a45173 | 7496 | continue; |
65e19f54 | 7497 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7498 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7499 | nr_files++; | |
6b06314c JA |
7500 | } |
7501 | ||
08a45173 JA |
7502 | if (nr_files) { |
7503 | fpl->max = SCM_MAX_FD; | |
7504 | fpl->count = nr_files; | |
7505 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7506 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7507 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7508 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7509 | |
08a45173 JA |
7510 | for (i = 0; i < nr_files; i++) |
7511 | fput(fpl->fp[i]); | |
7512 | } else { | |
7513 | kfree_skb(skb); | |
7514 | kfree(fpl); | |
7515 | } | |
6b06314c JA |
7516 | |
7517 | return 0; | |
7518 | } | |
7519 | ||
7520 | /* | |
7521 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7522 | * causes regular reference counting to break down. We rely on the UNIX | |
7523 | * garbage collection to take care of this problem for us. | |
7524 | */ | |
7525 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7526 | { | |
7527 | unsigned left, total; | |
7528 | int ret = 0; | |
7529 | ||
7530 | total = 0; | |
7531 | left = ctx->nr_user_files; | |
7532 | while (left) { | |
7533 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7534 | |
7535 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7536 | if (ret) | |
7537 | break; | |
7538 | left -= this_files; | |
7539 | total += this_files; | |
7540 | } | |
7541 | ||
7542 | if (!ret) | |
7543 | return 0; | |
7544 | ||
7545 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7546 | struct file *file = io_file_from_index(ctx, total); |
7547 | ||
7548 | if (file) | |
7549 | fput(file); | |
6b06314c JA |
7550 | total++; |
7551 | } | |
7552 | ||
7553 | return ret; | |
7554 | } | |
7555 | #else | |
7556 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7557 | { | |
7558 | return 0; | |
7559 | } | |
7560 | #endif | |
7561 | ||
47e90392 | 7562 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7563 | { |
50238531 | 7564 | struct file *file = prsrc->file; |
05f3fb3c JA |
7565 | #if defined(CONFIG_UNIX) |
7566 | struct sock *sock = ctx->ring_sock->sk; | |
7567 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7568 | struct sk_buff *skb; | |
7569 | int i; | |
7570 | ||
7571 | __skb_queue_head_init(&list); | |
7572 | ||
7573 | /* | |
7574 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7575 | * remove this entry and rearrange the file array. | |
7576 | */ | |
7577 | skb = skb_dequeue(head); | |
7578 | while (skb) { | |
7579 | struct scm_fp_list *fp; | |
7580 | ||
7581 | fp = UNIXCB(skb).fp; | |
7582 | for (i = 0; i < fp->count; i++) { | |
7583 | int left; | |
7584 | ||
7585 | if (fp->fp[i] != file) | |
7586 | continue; | |
7587 | ||
7588 | unix_notinflight(fp->user, fp->fp[i]); | |
7589 | left = fp->count - 1 - i; | |
7590 | if (left) { | |
7591 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7592 | left * sizeof(struct file *)); | |
7593 | } | |
7594 | fp->count--; | |
7595 | if (!fp->count) { | |
7596 | kfree_skb(skb); | |
7597 | skb = NULL; | |
7598 | } else { | |
7599 | __skb_queue_tail(&list, skb); | |
7600 | } | |
7601 | fput(file); | |
7602 | file = NULL; | |
7603 | break; | |
7604 | } | |
7605 | ||
7606 | if (!file) | |
7607 | break; | |
7608 | ||
7609 | __skb_queue_tail(&list, skb); | |
7610 | ||
7611 | skb = skb_dequeue(head); | |
7612 | } | |
7613 | ||
7614 | if (skb_peek(&list)) { | |
7615 | spin_lock_irq(&head->lock); | |
7616 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7617 | __skb_queue_tail(head, skb); | |
7618 | spin_unlock_irq(&head->lock); | |
7619 | } | |
7620 | #else | |
7621 | fput(file); | |
7622 | #endif | |
7623 | } | |
7624 | ||
b895c9a6 | 7625 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7626 | { |
b895c9a6 | 7627 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7628 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7629 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7630 | |
269bbe5f BM |
7631 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7632 | list_del(&prsrc->list); | |
b60c8dce PB |
7633 | |
7634 | if (prsrc->tag) { | |
7635 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
7636 | |
7637 | io_ring_submit_lock(ctx, lock_ring); | |
157d257f | 7638 | spin_lock_irq(&ctx->completion_lock); |
b60c8dce | 7639 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 7640 | ctx->cq_extra++; |
b60c8dce | 7641 | io_commit_cqring(ctx); |
157d257f | 7642 | spin_unlock_irq(&ctx->completion_lock); |
b60c8dce PB |
7643 | io_cqring_ev_posted(ctx); |
7644 | io_ring_submit_unlock(ctx, lock_ring); | |
7645 | } | |
7646 | ||
40ae0ff7 | 7647 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7648 | kfree(prsrc); |
65e19f54 | 7649 | } |
05589553 | 7650 | |
28a9fe25 | 7651 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
7652 | if (atomic_dec_and_test(&rsrc_data->refs)) |
7653 | complete(&rsrc_data->done); | |
2faf852d | 7654 | } |
65e19f54 | 7655 | |
269bbe5f | 7656 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7657 | { |
7658 | struct io_ring_ctx *ctx; | |
7659 | struct llist_node *node; | |
7660 | ||
269bbe5f BM |
7661 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7662 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7663 | |
7664 | while (node) { | |
b895c9a6 | 7665 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7666 | struct llist_node *next = node->next; |
7667 | ||
b895c9a6 | 7668 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7669 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7670 | node = next; |
7671 | } | |
7672 | } | |
7673 | ||
00835dce | 7674 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
2faf852d | 7675 | { |
b895c9a6 | 7676 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
3e942498 | 7677 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
4956b9ea | 7678 | unsigned long flags; |
e297822b | 7679 | bool first_add = false; |
e297822b | 7680 | |
4956b9ea | 7681 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
b895c9a6 | 7682 | node->done = true; |
e297822b | 7683 | |
d67d2263 | 7684 | while (!list_empty(&ctx->rsrc_ref_list)) { |
b895c9a6 PB |
7685 | node = list_first_entry(&ctx->rsrc_ref_list, |
7686 | struct io_rsrc_node, node); | |
e297822b | 7687 | /* recycle ref nodes in order */ |
b895c9a6 | 7688 | if (!node->done) |
e297822b | 7689 | break; |
b895c9a6 PB |
7690 | list_del(&node->node); |
7691 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
e297822b | 7692 | } |
4956b9ea | 7693 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
05589553 | 7694 | |
3e942498 PB |
7695 | if (first_add) |
7696 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
05f3fb3c | 7697 | } |
65e19f54 | 7698 | |
b895c9a6 | 7699 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
05f3fb3c | 7700 | { |
b895c9a6 | 7701 | struct io_rsrc_node *ref_node; |
05f3fb3c | 7702 | |
05589553 XW |
7703 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
7704 | if (!ref_node) | |
3e2224c5 | 7705 | return NULL; |
05f3fb3c | 7706 | |
00835dce | 7707 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
05589553 XW |
7708 | 0, GFP_KERNEL)) { |
7709 | kfree(ref_node); | |
3e2224c5 | 7710 | return NULL; |
05589553 XW |
7711 | } |
7712 | INIT_LIST_HEAD(&ref_node->node); | |
269bbe5f | 7713 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
e297822b | 7714 | ref_node->done = false; |
05589553 | 7715 | return ref_node; |
05589553 XW |
7716 | } |
7717 | ||
6b06314c | 7718 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 7719 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
7720 | { |
7721 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7722 | struct file *file; |
f3baed39 | 7723 | int fd, ret; |
846a4ef2 | 7724 | unsigned i; |
6b06314c | 7725 | |
05f3fb3c | 7726 | if (ctx->file_data) |
6b06314c JA |
7727 | return -EBUSY; |
7728 | if (!nr_args) | |
7729 | return -EINVAL; | |
7730 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7731 | return -EMFILE; | |
a7f0ed5a | 7732 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7733 | if (ret) |
7734 | return ret; | |
d878c816 PB |
7735 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
7736 | &ctx->file_data); | |
7737 | if (ret) | |
7738 | return ret; | |
6b06314c | 7739 | |
f3baed39 | 7740 | ret = -ENOMEM; |
aeca241b | 7741 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 7742 | goto out_free; |
65e19f54 | 7743 | |
08a45173 | 7744 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 7745 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
7746 | ret = -EFAULT; |
7747 | goto out_fput; | |
7748 | } | |
08a45173 | 7749 | /* allow sparse sets */ |
792e3582 PB |
7750 | if (fd == -1) { |
7751 | ret = -EINVAL; | |
2d091d62 | 7752 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 7753 | goto out_fput; |
08a45173 | 7754 | continue; |
792e3582 | 7755 | } |
6b06314c | 7756 | |
05f3fb3c | 7757 | file = fget(fd); |
6b06314c | 7758 | ret = -EBADF; |
792e3582 | 7759 | if (unlikely(!file)) |
600cf3f8 | 7760 | goto out_fput; |
05f3fb3c | 7761 | |
6b06314c JA |
7762 | /* |
7763 | * Don't allow io_uring instances to be registered. If UNIX | |
7764 | * isn't enabled, then this causes a reference cycle and this | |
7765 | * instance can never get freed. If UNIX is enabled we'll | |
7766 | * handle it just fine, but there's still no point in allowing | |
7767 | * a ring fd as it doesn't support regular read/write anyway. | |
7768 | */ | |
05f3fb3c JA |
7769 | if (file->f_op == &io_uring_fops) { |
7770 | fput(file); | |
600cf3f8 | 7771 | goto out_fput; |
6b06314c | 7772 | } |
aeca241b | 7773 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
7774 | } |
7775 | ||
6b06314c | 7776 | ret = io_sqe_files_scm(ctx); |
05589553 | 7777 | if (ret) { |
08480400 | 7778 | __io_sqe_files_unregister(ctx); |
05589553 XW |
7779 | return ret; |
7780 | } | |
6b06314c | 7781 | |
a7f0ed5a | 7782 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7783 | return ret; |
600cf3f8 PB |
7784 | out_fput: |
7785 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7786 | file = io_file_from_index(ctx, i); | |
7787 | if (file) | |
7788 | fput(file); | |
7789 | } | |
042b0d85 | 7790 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 7791 | ctx->nr_user_files = 0; |
600cf3f8 | 7792 | out_free: |
44b31f2f | 7793 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 7794 | ctx->file_data = NULL; |
6b06314c JA |
7795 | return ret; |
7796 | } | |
7797 | ||
c3a31e60 JA |
7798 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7799 | int index) | |
7800 | { | |
7801 | #if defined(CONFIG_UNIX) | |
7802 | struct sock *sock = ctx->ring_sock->sk; | |
7803 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7804 | struct sk_buff *skb; | |
7805 | ||
7806 | /* | |
7807 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7808 | * file set. If there's no room, fall back to allocating a new skb | |
7809 | * and filling it in. | |
7810 | */ | |
7811 | spin_lock_irq(&head->lock); | |
7812 | skb = skb_peek(head); | |
7813 | if (skb) { | |
7814 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7815 | ||
7816 | if (fpl->count < SCM_MAX_FD) { | |
7817 | __skb_unlink(skb, head); | |
7818 | spin_unlock_irq(&head->lock); | |
7819 | fpl->fp[fpl->count] = get_file(file); | |
7820 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7821 | fpl->count++; | |
7822 | spin_lock_irq(&head->lock); | |
7823 | __skb_queue_head(head, skb); | |
7824 | } else { | |
7825 | skb = NULL; | |
7826 | } | |
7827 | } | |
7828 | spin_unlock_irq(&head->lock); | |
7829 | ||
7830 | if (skb) { | |
7831 | fput(file); | |
7832 | return 0; | |
7833 | } | |
7834 | ||
7835 | return __io_sqe_files_scm(ctx, 1, index); | |
7836 | #else | |
7837 | return 0; | |
7838 | #endif | |
7839 | } | |
7840 | ||
b60c8dce | 7841 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
e7c78371 | 7842 | struct io_rsrc_node *node, void *rsrc) |
05f3fb3c | 7843 | { |
269bbe5f | 7844 | struct io_rsrc_put *prsrc; |
05f3fb3c | 7845 | |
269bbe5f BM |
7846 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7847 | if (!prsrc) | |
a5318d3c | 7848 | return -ENOMEM; |
05f3fb3c | 7849 | |
2d091d62 | 7850 | prsrc->tag = *io_get_tag_slot(data, idx); |
50238531 | 7851 | prsrc->rsrc = rsrc; |
e7c78371 | 7852 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 7853 | return 0; |
05f3fb3c JA |
7854 | } |
7855 | ||
7856 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
c3bdad02 | 7857 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
7858 | unsigned nr_args) |
7859 | { | |
c3bdad02 | 7860 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 7861 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 7862 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
7863 | struct io_fixed_file *file_slot; |
7864 | struct file *file; | |
98f0b3b4 PB |
7865 | int fd, i, err = 0; |
7866 | unsigned int done; | |
05589553 | 7867 | bool needs_switch = false; |
c3a31e60 | 7868 | |
98f0b3b4 PB |
7869 | if (!ctx->file_data) |
7870 | return -ENXIO; | |
7871 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
7872 | return -EINVAL; |
7873 | ||
67973b93 | 7874 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
7875 | u64 tag = 0; |
7876 | ||
7877 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
7878 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
7879 | err = -EFAULT; |
7880 | break; | |
7881 | } | |
c3bdad02 PB |
7882 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
7883 | err = -EINVAL; | |
7884 | break; | |
7885 | } | |
4e0377a1 | 7886 | if (fd == IORING_REGISTER_FILES_SKIP) |
7887 | continue; | |
7888 | ||
67973b93 | 7889 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 7890 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 7891 | |
a04b0ac0 PB |
7892 | if (file_slot->file_ptr) { |
7893 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
7894 | err = io_queue_rsrc_removal(data, up->offset + done, |
7895 | ctx->rsrc_node, file); | |
a5318d3c HD |
7896 | if (err) |
7897 | break; | |
a04b0ac0 | 7898 | file_slot->file_ptr = 0; |
05589553 | 7899 | needs_switch = true; |
c3a31e60 JA |
7900 | } |
7901 | if (fd != -1) { | |
c3a31e60 JA |
7902 | file = fget(fd); |
7903 | if (!file) { | |
7904 | err = -EBADF; | |
7905 | break; | |
7906 | } | |
7907 | /* | |
7908 | * Don't allow io_uring instances to be registered. If | |
7909 | * UNIX isn't enabled, then this causes a reference | |
7910 | * cycle and this instance can never get freed. If UNIX | |
7911 | * is enabled we'll handle it just fine, but there's | |
7912 | * still no point in allowing a ring fd as it doesn't | |
7913 | * support regular read/write anyway. | |
7914 | */ | |
7915 | if (file->f_op == &io_uring_fops) { | |
7916 | fput(file); | |
7917 | err = -EBADF; | |
7918 | break; | |
7919 | } | |
2d091d62 | 7920 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 7921 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 7922 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7923 | if (err) { |
a04b0ac0 | 7924 | file_slot->file_ptr = 0; |
f3bd9dae | 7925 | fput(file); |
c3a31e60 | 7926 | break; |
f3bd9dae | 7927 | } |
c3a31e60 | 7928 | } |
05f3fb3c JA |
7929 | } |
7930 | ||
a7f0ed5a PB |
7931 | if (needs_switch) |
7932 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
7933 | return done ? done : err; |
7934 | } | |
05589553 | 7935 | |
685fe7fe JA |
7936 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
7937 | struct task_struct *task) | |
24369c2e | 7938 | { |
e941894e | 7939 | struct io_wq_hash *hash; |
24369c2e | 7940 | struct io_wq_data data; |
24369c2e | 7941 | unsigned int concurrency; |
24369c2e | 7942 | |
362a9e65 | 7943 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
7944 | hash = ctx->hash_map; |
7945 | if (!hash) { | |
7946 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
7947 | if (!hash) { |
7948 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 7949 | return ERR_PTR(-ENOMEM); |
362a9e65 | 7950 | } |
e941894e JA |
7951 | refcount_set(&hash->refs, 1); |
7952 | init_waitqueue_head(&hash->wait); | |
7953 | ctx->hash_map = hash; | |
24369c2e | 7954 | } |
362a9e65 | 7955 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 7956 | |
e941894e | 7957 | data.hash = hash; |
685fe7fe | 7958 | data.task = task; |
ebc11b6c | 7959 | data.free_work = io_wq_free_work; |
f5fa38c5 | 7960 | data.do_work = io_wq_submit_work; |
24369c2e | 7961 | |
d25e3a3d JA |
7962 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7963 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7964 | |
5aa75ed5 | 7965 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7966 | } |
7967 | ||
5aa75ed5 JA |
7968 | static int io_uring_alloc_task_context(struct task_struct *task, |
7969 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7970 | { |
7971 | struct io_uring_task *tctx; | |
d8a6df10 | 7972 | int ret; |
0f212204 | 7973 | |
09899b19 | 7974 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
7975 | if (unlikely(!tctx)) |
7976 | return -ENOMEM; | |
7977 | ||
d8a6df10 JA |
7978 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7979 | if (unlikely(ret)) { | |
7980 | kfree(tctx); | |
7981 | return ret; | |
7982 | } | |
7983 | ||
685fe7fe | 7984 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
7985 | if (IS_ERR(tctx->io_wq)) { |
7986 | ret = PTR_ERR(tctx->io_wq); | |
7987 | percpu_counter_destroy(&tctx->inflight); | |
7988 | kfree(tctx); | |
7989 | return ret; | |
7990 | } | |
7991 | ||
0f212204 JA |
7992 | xa_init(&tctx->xa); |
7993 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 7994 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 7995 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 7996 | task->io_uring = tctx; |
7cbf1722 JA |
7997 | spin_lock_init(&tctx->task_lock); |
7998 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 7999 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8000 | return 0; |
8001 | } | |
8002 | ||
8003 | void __io_uring_free(struct task_struct *tsk) | |
8004 | { | |
8005 | struct io_uring_task *tctx = tsk->io_uring; | |
8006 | ||
8007 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8008 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8009 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8010 | |
d8a6df10 | 8011 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8012 | kfree(tctx); |
8013 | tsk->io_uring = NULL; | |
8014 | } | |
8015 | ||
7e84e1c7 SG |
8016 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8017 | struct io_uring_params *p) | |
2b188cc1 JA |
8018 | { |
8019 | int ret; | |
8020 | ||
d25e3a3d JA |
8021 | /* Retain compatibility with failing for an invalid attach attempt */ |
8022 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8023 | IORING_SETUP_ATTACH_WQ) { | |
8024 | struct fd f; | |
8025 | ||
8026 | f = fdget(p->wq_fd); | |
8027 | if (!f.file) | |
8028 | return -ENXIO; | |
0cc936f7 JA |
8029 | if (f.file->f_op != &io_uring_fops) { |
8030 | fdput(f); | |
f2a48dd0 | 8031 | return -EINVAL; |
0cc936f7 JA |
8032 | } |
8033 | fdput(f); | |
d25e3a3d | 8034 | } |
6c271ce2 | 8035 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8036 | struct task_struct *tsk; |
534ca6d6 | 8037 | struct io_sq_data *sqd; |
26984fbf | 8038 | bool attached; |
534ca6d6 | 8039 | |
26984fbf | 8040 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8041 | if (IS_ERR(sqd)) { |
8042 | ret = PTR_ERR(sqd); | |
8043 | goto err; | |
8044 | } | |
69fb2131 | 8045 | |
7c30f36a | 8046 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8047 | ctx->sq_data = sqd; |
917257da JA |
8048 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8049 | if (!ctx->sq_thread_idle) | |
8050 | ctx->sq_thread_idle = HZ; | |
8051 | ||
78d7f6ba | 8052 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8053 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8054 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8055 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8056 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8057 | io_sq_thread_unpark(sqd); |
8058 | ||
de75a3d3 PB |
8059 | if (ret < 0) |
8060 | goto err; | |
8061 | if (attached) | |
5aa75ed5 | 8062 | return 0; |
aa06165d | 8063 | |
6c271ce2 | 8064 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8065 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8066 | |
917257da | 8067 | ret = -EINVAL; |
f2a48dd0 | 8068 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8069 | goto err_sqpoll; |
37d1e2e3 | 8070 | sqd->sq_cpu = cpu; |
6c271ce2 | 8071 | } else { |
37d1e2e3 | 8072 | sqd->sq_cpu = -1; |
6c271ce2 | 8073 | } |
37d1e2e3 JA |
8074 | |
8075 | sqd->task_pid = current->pid; | |
5c2469e0 | 8076 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8077 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8078 | if (IS_ERR(tsk)) { | |
8079 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8080 | goto err_sqpoll; |
6c271ce2 | 8081 | } |
97a73a0f | 8082 | |
46fe18b1 | 8083 | sqd->thread = tsk; |
97a73a0f | 8084 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8085 | wake_up_new_task(tsk); |
0f212204 JA |
8086 | if (ret) |
8087 | goto err; | |
6c271ce2 JA |
8088 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8089 | /* Can't have SQ_AFF without SQPOLL */ | |
8090 | ret = -EINVAL; | |
8091 | goto err; | |
8092 | } | |
8093 | ||
2b188cc1 | 8094 | return 0; |
f2a48dd0 PB |
8095 | err_sqpoll: |
8096 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8097 | err: |
37d1e2e3 | 8098 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8099 | return ret; |
8100 | } | |
8101 | ||
a087e2b5 BM |
8102 | static inline void __io_unaccount_mem(struct user_struct *user, |
8103 | unsigned long nr_pages) | |
2b188cc1 JA |
8104 | { |
8105 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8106 | } | |
8107 | ||
a087e2b5 BM |
8108 | static inline int __io_account_mem(struct user_struct *user, |
8109 | unsigned long nr_pages) | |
2b188cc1 JA |
8110 | { |
8111 | unsigned long page_limit, cur_pages, new_pages; | |
8112 | ||
8113 | /* Don't allow more pages than we can safely lock */ | |
8114 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8115 | ||
8116 | do { | |
8117 | cur_pages = atomic_long_read(&user->locked_vm); | |
8118 | new_pages = cur_pages + nr_pages; | |
8119 | if (new_pages > page_limit) | |
8120 | return -ENOMEM; | |
8121 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8122 | new_pages) != cur_pages); | |
8123 | ||
8124 | return 0; | |
8125 | } | |
8126 | ||
26bfa89e | 8127 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8128 | { |
62e398be | 8129 | if (ctx->user) |
a087e2b5 | 8130 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8131 | |
26bfa89e JA |
8132 | if (ctx->mm_account) |
8133 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8134 | } |
8135 | ||
26bfa89e | 8136 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8137 | { |
30975825 BM |
8138 | int ret; |
8139 | ||
62e398be | 8140 | if (ctx->user) { |
30975825 BM |
8141 | ret = __io_account_mem(ctx->user, nr_pages); |
8142 | if (ret) | |
8143 | return ret; | |
8144 | } | |
8145 | ||
26bfa89e JA |
8146 | if (ctx->mm_account) |
8147 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8148 | |
8149 | return 0; | |
8150 | } | |
8151 | ||
2b188cc1 JA |
8152 | static void io_mem_free(void *ptr) |
8153 | { | |
52e04ef4 MR |
8154 | struct page *page; |
8155 | ||
8156 | if (!ptr) | |
8157 | return; | |
2b188cc1 | 8158 | |
52e04ef4 | 8159 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8160 | if (put_page_testzero(page)) |
8161 | free_compound_page(page); | |
8162 | } | |
8163 | ||
8164 | static void *io_mem_alloc(size_t size) | |
8165 | { | |
8166 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8167 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8168 | |
8169 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8170 | } | |
8171 | ||
75b28aff HV |
8172 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8173 | size_t *sq_offset) | |
8174 | { | |
8175 | struct io_rings *rings; | |
8176 | size_t off, sq_array_size; | |
8177 | ||
8178 | off = struct_size(rings, cqes, cq_entries); | |
8179 | if (off == SIZE_MAX) | |
8180 | return SIZE_MAX; | |
8181 | ||
8182 | #ifdef CONFIG_SMP | |
8183 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8184 | if (off == 0) | |
8185 | return SIZE_MAX; | |
8186 | #endif | |
8187 | ||
b36200f5 DV |
8188 | if (sq_offset) |
8189 | *sq_offset = off; | |
8190 | ||
75b28aff HV |
8191 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8192 | if (sq_array_size == SIZE_MAX) | |
8193 | return SIZE_MAX; | |
8194 | ||
8195 | if (check_add_overflow(off, sq_array_size, &off)) | |
8196 | return SIZE_MAX; | |
8197 | ||
75b28aff HV |
8198 | return off; |
8199 | } | |
8200 | ||
41edf1a5 | 8201 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8202 | { |
41edf1a5 | 8203 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8204 | unsigned int i; |
8205 | ||
6224843d PB |
8206 | if (imu != ctx->dummy_ubuf) { |
8207 | for (i = 0; i < imu->nr_bvecs; i++) | |
8208 | unpin_user_page(imu->bvec[i].bv_page); | |
8209 | if (imu->acct_pages) | |
8210 | io_unaccount_mem(ctx, imu->acct_pages); | |
8211 | kvfree(imu); | |
8212 | } | |
41edf1a5 | 8213 | *slot = NULL; |
7f61a1e9 PB |
8214 | } |
8215 | ||
bd54b6fe | 8216 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8217 | { |
634d00df PB |
8218 | io_buffer_unmap(ctx, &prsrc->buf); |
8219 | prsrc->buf = NULL; | |
bd54b6fe | 8220 | } |
edafccee | 8221 | |
bd54b6fe BM |
8222 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8223 | { | |
8224 | unsigned int i; | |
edafccee | 8225 | |
7f61a1e9 PB |
8226 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8227 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8228 | kfree(ctx->user_bufs); |
bb6659cc | 8229 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8230 | ctx->user_bufs = NULL; |
bd54b6fe | 8231 | ctx->buf_data = NULL; |
edafccee | 8232 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8233 | } |
8234 | ||
0a96bbe4 | 8235 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8236 | { |
bd54b6fe | 8237 | int ret; |
edafccee | 8238 | |
bd54b6fe | 8239 | if (!ctx->buf_data) |
edafccee JA |
8240 | return -ENXIO; |
8241 | ||
bd54b6fe BM |
8242 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8243 | if (!ret) | |
8244 | __io_sqe_buffers_unregister(ctx); | |
8245 | return ret; | |
edafccee JA |
8246 | } |
8247 | ||
8248 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8249 | void __user *arg, unsigned index) | |
8250 | { | |
8251 | struct iovec __user *src; | |
8252 | ||
8253 | #ifdef CONFIG_COMPAT | |
8254 | if (ctx->compat) { | |
8255 | struct compat_iovec __user *ciovs; | |
8256 | struct compat_iovec ciov; | |
8257 | ||
8258 | ciovs = (struct compat_iovec __user *) arg; | |
8259 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8260 | return -EFAULT; | |
8261 | ||
d55e5f5b | 8262 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8263 | dst->iov_len = ciov.iov_len; |
8264 | return 0; | |
8265 | } | |
8266 | #endif | |
8267 | src = (struct iovec __user *) arg; | |
8268 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8269 | return -EFAULT; | |
8270 | return 0; | |
8271 | } | |
8272 | ||
de293938 JA |
8273 | /* |
8274 | * Not super efficient, but this is just a registration time. And we do cache | |
8275 | * the last compound head, so generally we'll only do a full search if we don't | |
8276 | * match that one. | |
8277 | * | |
8278 | * We check if the given compound head page has already been accounted, to | |
8279 | * avoid double accounting it. This allows us to account the full size of the | |
8280 | * page, not just the constituent pages of a huge page. | |
8281 | */ | |
8282 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8283 | int nr_pages, struct page *hpage) | |
8284 | { | |
8285 | int i, j; | |
8286 | ||
8287 | /* check current page array */ | |
8288 | for (i = 0; i < nr_pages; i++) { | |
8289 | if (!PageCompound(pages[i])) | |
8290 | continue; | |
8291 | if (compound_head(pages[i]) == hpage) | |
8292 | return true; | |
8293 | } | |
8294 | ||
8295 | /* check previously registered pages */ | |
8296 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8297 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8298 | |
8299 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8300 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8301 | continue; | |
8302 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8303 | return true; | |
8304 | } | |
8305 | } | |
8306 | ||
8307 | return false; | |
8308 | } | |
8309 | ||
8310 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8311 | int nr_pages, struct io_mapped_ubuf *imu, | |
8312 | struct page **last_hpage) | |
8313 | { | |
8314 | int i, ret; | |
8315 | ||
216e5835 | 8316 | imu->acct_pages = 0; |
de293938 JA |
8317 | for (i = 0; i < nr_pages; i++) { |
8318 | if (!PageCompound(pages[i])) { | |
8319 | imu->acct_pages++; | |
8320 | } else { | |
8321 | struct page *hpage; | |
8322 | ||
8323 | hpage = compound_head(pages[i]); | |
8324 | if (hpage == *last_hpage) | |
8325 | continue; | |
8326 | *last_hpage = hpage; | |
8327 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8328 | continue; | |
8329 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8330 | } | |
8331 | } | |
8332 | ||
8333 | if (!imu->acct_pages) | |
8334 | return 0; | |
8335 | ||
26bfa89e | 8336 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8337 | if (ret) |
8338 | imu->acct_pages = 0; | |
8339 | return ret; | |
8340 | } | |
8341 | ||
0a96bbe4 | 8342 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8343 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8344 | struct page **last_hpage) |
edafccee | 8345 | { |
41edf1a5 | 8346 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8347 | struct vm_area_struct **vmas = NULL; |
8348 | struct page **pages = NULL; | |
0a96bbe4 BM |
8349 | unsigned long off, start, end, ubuf; |
8350 | size_t size; | |
8351 | int ret, pret, nr_pages, i; | |
8352 | ||
6224843d PB |
8353 | if (!iov->iov_base) { |
8354 | *pimu = ctx->dummy_ubuf; | |
8355 | return 0; | |
8356 | } | |
8357 | ||
0a96bbe4 BM |
8358 | ubuf = (unsigned long) iov->iov_base; |
8359 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8360 | start = ubuf >> PAGE_SHIFT; | |
8361 | nr_pages = end - start; | |
8362 | ||
41edf1a5 | 8363 | *pimu = NULL; |
0a96bbe4 BM |
8364 | ret = -ENOMEM; |
8365 | ||
8366 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8367 | if (!pages) | |
8368 | goto done; | |
8369 | ||
8370 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8371 | GFP_KERNEL); | |
8372 | if (!vmas) | |
8373 | goto done; | |
edafccee | 8374 | |
41edf1a5 | 8375 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8376 | if (!imu) |
0a96bbe4 BM |
8377 | goto done; |
8378 | ||
8379 | ret = 0; | |
8380 | mmap_read_lock(current->mm); | |
8381 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8382 | pages, vmas); | |
8383 | if (pret == nr_pages) { | |
8384 | /* don't support file backed memory */ | |
8385 | for (i = 0; i < nr_pages; i++) { | |
8386 | struct vm_area_struct *vma = vmas[i]; | |
8387 | ||
40dad765 PB |
8388 | if (vma_is_shmem(vma)) |
8389 | continue; | |
0a96bbe4 BM |
8390 | if (vma->vm_file && |
8391 | !is_file_hugepages(vma->vm_file)) { | |
8392 | ret = -EOPNOTSUPP; | |
8393 | break; | |
8394 | } | |
8395 | } | |
8396 | } else { | |
8397 | ret = pret < 0 ? pret : -EFAULT; | |
8398 | } | |
8399 | mmap_read_unlock(current->mm); | |
8400 | if (ret) { | |
8401 | /* | |
8402 | * if we did partial map, or found file backed vmas, | |
8403 | * release any pages we did get | |
8404 | */ | |
8405 | if (pret > 0) | |
8406 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8407 | goto done; |
8408 | } | |
8409 | ||
8410 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8411 | if (ret) { | |
8412 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8413 | goto done; |
8414 | } | |
8415 | ||
8416 | off = ubuf & ~PAGE_MASK; | |
8417 | size = iov->iov_len; | |
8418 | for (i = 0; i < nr_pages; i++) { | |
8419 | size_t vec_len; | |
8420 | ||
8421 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8422 | imu->bvec[i].bv_page = pages[i]; | |
8423 | imu->bvec[i].bv_len = vec_len; | |
8424 | imu->bvec[i].bv_offset = off; | |
8425 | off = 0; | |
8426 | size -= vec_len; | |
8427 | } | |
8428 | /* store original address for later verification */ | |
8429 | imu->ubuf = ubuf; | |
4751f53d | 8430 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8431 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8432 | *pimu = imu; |
0a96bbe4 BM |
8433 | ret = 0; |
8434 | done: | |
41edf1a5 PB |
8435 | if (ret) |
8436 | kvfree(imu); | |
0a96bbe4 BM |
8437 | kvfree(pages); |
8438 | kvfree(vmas); | |
8439 | return ret; | |
8440 | } | |
8441 | ||
2b358604 | 8442 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8443 | { |
87094465 PB |
8444 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8445 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8446 | } |
edafccee | 8447 | |
2b358604 BM |
8448 | static int io_buffer_validate(struct iovec *iov) |
8449 | { | |
50e96989 PB |
8450 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8451 | ||
2b358604 BM |
8452 | /* |
8453 | * Don't impose further limits on the size and buffer | |
8454 | * constraints here, we'll -EINVAL later when IO is | |
8455 | * submitted if they are wrong. | |
8456 | */ | |
6224843d PB |
8457 | if (!iov->iov_base) |
8458 | return iov->iov_len ? -EFAULT : 0; | |
8459 | if (!iov->iov_len) | |
2b358604 | 8460 | return -EFAULT; |
edafccee | 8461 | |
2b358604 BM |
8462 | /* arbitrary limit, but we need something */ |
8463 | if (iov->iov_len > SZ_1G) | |
8464 | return -EFAULT; | |
edafccee | 8465 | |
50e96989 PB |
8466 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8467 | return -EOVERFLOW; | |
8468 | ||
2b358604 BM |
8469 | return 0; |
8470 | } | |
edafccee | 8471 | |
2b358604 | 8472 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 8473 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 8474 | { |
bd54b6fe BM |
8475 | struct page *last_hpage = NULL; |
8476 | struct io_rsrc_data *data; | |
2b358604 BM |
8477 | int i, ret; |
8478 | struct iovec iov; | |
edafccee | 8479 | |
87094465 PB |
8480 | if (ctx->user_bufs) |
8481 | return -EBUSY; | |
489809e2 | 8482 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 8483 | return -EINVAL; |
bd54b6fe | 8484 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
8485 | if (ret) |
8486 | return ret; | |
d878c816 PB |
8487 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
8488 | if (ret) | |
8489 | return ret; | |
bd54b6fe BM |
8490 | ret = io_buffers_map_alloc(ctx, nr_args); |
8491 | if (ret) { | |
bb6659cc | 8492 | io_rsrc_data_free(data); |
bd54b6fe BM |
8493 | return ret; |
8494 | } | |
edafccee | 8495 | |
87094465 | 8496 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
8497 | ret = io_copy_iov(ctx, &iov, arg, i); |
8498 | if (ret) | |
0a96bbe4 | 8499 | break; |
2b358604 BM |
8500 | ret = io_buffer_validate(&iov); |
8501 | if (ret) | |
0a96bbe4 | 8502 | break; |
2d091d62 | 8503 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
8504 | ret = -EINVAL; |
8505 | break; | |
8506 | } | |
edafccee | 8507 | |
41edf1a5 PB |
8508 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
8509 | &last_hpage); | |
0a96bbe4 BM |
8510 | if (ret) |
8511 | break; | |
edafccee | 8512 | } |
0a96bbe4 | 8513 | |
bd54b6fe | 8514 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 8515 | |
bd54b6fe BM |
8516 | ctx->buf_data = data; |
8517 | if (ret) | |
8518 | __io_sqe_buffers_unregister(ctx); | |
8519 | else | |
8520 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
8521 | return ret; |
8522 | } | |
8523 | ||
634d00df PB |
8524 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
8525 | struct io_uring_rsrc_update2 *up, | |
8526 | unsigned int nr_args) | |
8527 | { | |
8528 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
8529 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
8530 | struct page *last_hpage = NULL; |
8531 | bool needs_switch = false; | |
8532 | __u32 done; | |
8533 | int i, err; | |
8534 | ||
8535 | if (!ctx->buf_data) | |
8536 | return -ENXIO; | |
8537 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
8538 | return -EINVAL; | |
8539 | ||
8540 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
8541 | struct io_mapped_ubuf *imu; |
8542 | int offset = up->offset + done; | |
634d00df PB |
8543 | u64 tag = 0; |
8544 | ||
8545 | err = io_copy_iov(ctx, &iov, iovs, done); | |
8546 | if (err) | |
8547 | break; | |
8548 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
8549 | err = -EFAULT; | |
8550 | break; | |
8551 | } | |
0b8c0e7c PB |
8552 | err = io_buffer_validate(&iov); |
8553 | if (err) | |
8554 | break; | |
cf3770e7 CIK |
8555 | if (!iov.iov_base && tag) { |
8556 | err = -EINVAL; | |
8557 | break; | |
8558 | } | |
0b8c0e7c PB |
8559 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
8560 | if (err) | |
8561 | break; | |
634d00df | 8562 | |
0b8c0e7c | 8563 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 8564 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
8565 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
8566 | ctx->rsrc_node, ctx->user_bufs[i]); | |
8567 | if (unlikely(err)) { | |
8568 | io_buffer_unmap(ctx, &imu); | |
634d00df | 8569 | break; |
0b8c0e7c | 8570 | } |
634d00df PB |
8571 | ctx->user_bufs[i] = NULL; |
8572 | needs_switch = true; | |
8573 | } | |
8574 | ||
0b8c0e7c | 8575 | ctx->user_bufs[i] = imu; |
2d091d62 | 8576 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
8577 | } |
8578 | ||
8579 | if (needs_switch) | |
8580 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
8581 | return done ? done : err; | |
8582 | } | |
8583 | ||
9b402849 JA |
8584 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8585 | { | |
8586 | __s32 __user *fds = arg; | |
8587 | int fd; | |
8588 | ||
8589 | if (ctx->cq_ev_fd) | |
8590 | return -EBUSY; | |
8591 | ||
8592 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8593 | return -EFAULT; | |
8594 | ||
8595 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8596 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8597 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 8598 | |
9b402849 JA |
8599 | ctx->cq_ev_fd = NULL; |
8600 | return ret; | |
8601 | } | |
8602 | ||
8603 | return 0; | |
8604 | } | |
8605 | ||
8606 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8607 | { | |
8608 | if (ctx->cq_ev_fd) { | |
8609 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8610 | ctx->cq_ev_fd = NULL; | |
8611 | return 0; | |
8612 | } | |
8613 | ||
8614 | return -ENXIO; | |
8615 | } | |
8616 | ||
5a2e745d JA |
8617 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8618 | { | |
9e15c3a0 JA |
8619 | struct io_buffer *buf; |
8620 | unsigned long index; | |
8621 | ||
8622 | xa_for_each(&ctx->io_buffers, index, buf) | |
8623 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8624 | } |
8625 | ||
68e68ee6 | 8626 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
1b4c351f | 8627 | { |
68e68ee6 | 8628 | struct io_kiocb *req, *nxt; |
1b4c351f | 8629 | |
68e68ee6 JA |
8630 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
8631 | if (tsk && req->task != tsk) | |
8632 | continue; | |
1b4c351f JA |
8633 | list_del(&req->compl.list); |
8634 | kmem_cache_free(req_cachep, req); | |
8635 | } | |
8636 | } | |
8637 | ||
4010fec4 | 8638 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8639 | { |
bf019da7 | 8640 | struct io_submit_state *submit_state = &ctx->submit_state; |
e5547d2c | 8641 | struct io_comp_state *cs = &ctx->submit_state.comp; |
bf019da7 | 8642 | |
9a4fdbd8 JA |
8643 | mutex_lock(&ctx->uring_lock); |
8644 | ||
8e5c66c4 | 8645 | if (submit_state->free_reqs) { |
9a4fdbd8 JA |
8646 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
8647 | submit_state->reqs); | |
8e5c66c4 PB |
8648 | submit_state->free_reqs = 0; |
8649 | } | |
9a4fdbd8 | 8650 | |
dac7a098 | 8651 | io_flush_cached_locked_reqs(ctx, cs); |
e5547d2c | 8652 | io_req_cache_free(&cs->free_list, NULL); |
9a4fdbd8 JA |
8653 | mutex_unlock(&ctx->uring_lock); |
8654 | } | |
8655 | ||
43597aac | 8656 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 8657 | { |
43597aac | 8658 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 8659 | wait_for_completion(&data->done); |
bd54b6fe | 8660 | } |
04fc6c80 | 8661 | |
2b188cc1 JA |
8662 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8663 | { | |
37d1e2e3 | 8664 | io_sq_thread_finish(ctx); |
2aede0e4 | 8665 | |
37d1e2e3 | 8666 | if (ctx->mm_account) { |
2aede0e4 JA |
8667 | mmdrop(ctx->mm_account); |
8668 | ctx->mm_account = NULL; | |
30975825 | 8669 | } |
def596e9 | 8670 | |
43597aac PB |
8671 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
8672 | io_wait_rsrc_data(ctx->buf_data); | |
8673 | io_wait_rsrc_data(ctx->file_data); | |
8674 | ||
8bad28d8 | 8675 | mutex_lock(&ctx->uring_lock); |
43597aac | 8676 | if (ctx->buf_data) |
bd54b6fe | 8677 | __io_sqe_buffers_unregister(ctx); |
43597aac | 8678 | if (ctx->file_data) |
08480400 | 8679 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
8680 | if (ctx->rings) |
8681 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8682 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8683 | io_eventfd_unregister(ctx); |
5a2e745d | 8684 | io_destroy_buffers(ctx); |
07db298a PB |
8685 | if (ctx->sq_creds) |
8686 | put_cred(ctx->sq_creds); | |
def596e9 | 8687 | |
a7f0ed5a PB |
8688 | /* there are no registered resources left, nobody uses it */ |
8689 | if (ctx->rsrc_node) | |
8690 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8691 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8692 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8693 | flush_delayed_work(&ctx->rsrc_put_work); |
8694 | ||
8695 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8696 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 8697 | |
2b188cc1 | 8698 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8699 | if (ctx->ring_sock) { |
8700 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8701 | sock_release(ctx->ring_sock); |
355e8d26 | 8702 | } |
2b188cc1 JA |
8703 | #endif |
8704 | ||
75b28aff | 8705 | io_mem_free(ctx->rings); |
2b188cc1 | 8706 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8707 | |
8708 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8709 | free_uid(ctx->user); |
4010fec4 | 8710 | io_req_caches_free(ctx); |
e941894e JA |
8711 | if (ctx->hash_map) |
8712 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8713 | kfree(ctx->cancel_hash); |
6224843d | 8714 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
8715 | kfree(ctx); |
8716 | } | |
8717 | ||
8718 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8719 | { | |
8720 | struct io_ring_ctx *ctx = file->private_data; | |
8721 | __poll_t mask = 0; | |
8722 | ||
311997b3 | 8723 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
8724 | /* |
8725 | * synchronizes with barrier from wq_has_sleeper call in | |
8726 | * io_commit_cqring | |
8727 | */ | |
2b188cc1 | 8728 | smp_rmb(); |
90554200 | 8729 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8730 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8731 | |
8732 | /* | |
8733 | * Don't flush cqring overflow list here, just do a simple check. | |
8734 | * Otherwise there could possible be ABBA deadlock: | |
8735 | * CPU0 CPU1 | |
8736 | * ---- ---- | |
8737 | * lock(&ctx->uring_lock); | |
8738 | * lock(&ep->mtx); | |
8739 | * lock(&ctx->uring_lock); | |
8740 | * lock(&ep->mtx); | |
8741 | * | |
8742 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8743 | * pushs them to do the flush. | |
8744 | */ | |
5ed7a37d | 8745 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
8746 | mask |= EPOLLIN | EPOLLRDNORM; |
8747 | ||
8748 | return mask; | |
8749 | } | |
8750 | ||
8751 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8752 | { | |
8753 | struct io_ring_ctx *ctx = file->private_data; | |
8754 | ||
8755 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8756 | } | |
8757 | ||
0bead8cd | 8758 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8759 | { |
4379bf8b | 8760 | const struct cred *creds; |
071698e1 | 8761 | |
61cf9370 | 8762 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8763 | if (creds) { |
8764 | put_cred(creds); | |
0bead8cd | 8765 | return 0; |
1e6fa521 | 8766 | } |
0bead8cd YD |
8767 | |
8768 | return -EINVAL; | |
8769 | } | |
8770 | ||
d56d938b PB |
8771 | struct io_tctx_exit { |
8772 | struct callback_head task_work; | |
8773 | struct completion completion; | |
baf186c4 | 8774 | struct io_ring_ctx *ctx; |
d56d938b PB |
8775 | }; |
8776 | ||
8777 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8778 | { | |
8779 | struct io_uring_task *tctx = current->io_uring; | |
8780 | struct io_tctx_exit *work; | |
8781 | ||
8782 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8783 | /* | |
8784 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8785 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8786 | */ | |
8787 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 8788 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
8789 | complete(&work->completion); |
8790 | } | |
8791 | ||
28090c13 PB |
8792 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8793 | { | |
8794 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8795 | ||
8796 | return req->ctx == data; | |
8797 | } | |
8798 | ||
85faa7b8 JA |
8799 | static void io_ring_exit_work(struct work_struct *work) |
8800 | { | |
d56d938b | 8801 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8802 | unsigned long timeout = jiffies + HZ * 60 * 5; |
d56d938b PB |
8803 | struct io_tctx_exit exit; |
8804 | struct io_tctx_node *node; | |
8805 | int ret; | |
85faa7b8 | 8806 | |
56952e91 JA |
8807 | /* |
8808 | * If we're doing polled IO and end up having requests being | |
8809 | * submitted async (out-of-line), then completions can come in while | |
8810 | * we're waiting for refs to drop. We need to reap these manually, | |
8811 | * as nobody else will be looking for them. | |
8812 | */ | |
b2edc0a7 | 8813 | do { |
3dd0c97a | 8814 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
8815 | if (ctx->sq_data) { |
8816 | struct io_sq_data *sqd = ctx->sq_data; | |
8817 | struct task_struct *tsk; | |
8818 | ||
8819 | io_sq_thread_park(sqd); | |
8820 | tsk = sqd->thread; | |
8821 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
8822 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
8823 | io_cancel_ctx_cb, ctx, true); | |
8824 | io_sq_thread_unpark(sqd); | |
8825 | } | |
b5bb3a24 PB |
8826 | |
8827 | WARN_ON_ONCE(time_after(jiffies, timeout)); | |
b2edc0a7 | 8828 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
d56d938b | 8829 | |
7f00651a PB |
8830 | init_completion(&exit.completion); |
8831 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
8832 | exit.ctx = ctx; | |
89b5066e PB |
8833 | /* |
8834 | * Some may use context even when all refs and requests have been put, | |
8835 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 8836 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
8837 | * this lock/unlock section also waits them to finish. |
8838 | */ | |
d56d938b PB |
8839 | mutex_lock(&ctx->uring_lock); |
8840 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
8841 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
8842 | ||
d56d938b PB |
8843 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
8844 | ctx_node); | |
7f00651a PB |
8845 | /* don't spin on a single task if cancellation failed */ |
8846 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
8847 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
8848 | if (WARN_ON_ONCE(ret)) | |
8849 | continue; | |
8850 | wake_up_process(node->task); | |
8851 | ||
8852 | mutex_unlock(&ctx->uring_lock); | |
8853 | wait_for_completion(&exit.completion); | |
d56d938b PB |
8854 | mutex_lock(&ctx->uring_lock); |
8855 | } | |
8856 | mutex_unlock(&ctx->uring_lock); | |
89b5066e PB |
8857 | spin_lock_irq(&ctx->completion_lock); |
8858 | spin_unlock_irq(&ctx->completion_lock); | |
d56d938b | 8859 | |
85faa7b8 JA |
8860 | io_ring_ctx_free(ctx); |
8861 | } | |
8862 | ||
80c4cbdb PB |
8863 | /* Returns true if we found and killed one or more timeouts */ |
8864 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 8865 | bool cancel_all) |
80c4cbdb PB |
8866 | { |
8867 | struct io_kiocb *req, *tmp; | |
8868 | int canceled = 0; | |
8869 | ||
8870 | spin_lock_irq(&ctx->completion_lock); | |
8871 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { | |
3dd0c97a | 8872 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
8873 | io_kill_timeout(req, -ECANCELED); |
8874 | canceled++; | |
8875 | } | |
8876 | } | |
51520426 PB |
8877 | if (canceled != 0) |
8878 | io_commit_cqring(ctx); | |
80c4cbdb | 8879 | spin_unlock_irq(&ctx->completion_lock); |
80c4cbdb PB |
8880 | if (canceled != 0) |
8881 | io_cqring_ev_posted(ctx); | |
8882 | return canceled != 0; | |
8883 | } | |
8884 | ||
2b188cc1 JA |
8885 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8886 | { | |
61cf9370 MWO |
8887 | unsigned long index; |
8888 | struct creds *creds; | |
8889 | ||
2b188cc1 JA |
8890 | mutex_lock(&ctx->uring_lock); |
8891 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 8892 | if (ctx->rings) |
6c2450ae | 8893 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
8894 | xa_for_each(&ctx->personalities, index, creds) |
8895 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
8896 | mutex_unlock(&ctx->uring_lock); |
8897 | ||
3dd0c97a PB |
8898 | io_kill_timeouts(ctx, NULL, true); |
8899 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 8900 | |
15dff286 | 8901 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8902 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8903 | |
85faa7b8 | 8904 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8905 | /* |
8906 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8907 | * if we're exiting a ton of rings at the same time. It just adds | |
8908 | * noise and overhead, there's no discernable change in runtime | |
8909 | * over using system_wq. | |
8910 | */ | |
8911 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8912 | } |
8913 | ||
8914 | static int io_uring_release(struct inode *inode, struct file *file) | |
8915 | { | |
8916 | struct io_ring_ctx *ctx = file->private_data; | |
8917 | ||
8918 | file->private_data = NULL; | |
8919 | io_ring_ctx_wait_and_kill(ctx); | |
8920 | return 0; | |
8921 | } | |
8922 | ||
f6edbabb PB |
8923 | struct io_task_cancel { |
8924 | struct task_struct *task; | |
3dd0c97a | 8925 | bool all; |
f6edbabb | 8926 | }; |
f254ac04 | 8927 | |
f6edbabb | 8928 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8929 | { |
9a472ef7 | 8930 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8931 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8932 | bool ret; |
8933 | ||
3dd0c97a | 8934 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8935 | unsigned long flags; |
8936 | struct io_ring_ctx *ctx = req->ctx; | |
8937 | ||
8938 | /* protect against races with linked timeouts */ | |
8939 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
3dd0c97a | 8940 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
8941 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
8942 | } else { | |
3dd0c97a | 8943 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
8944 | } |
8945 | return ret; | |
b711d4ea JA |
8946 | } |
8947 | ||
e1915f76 | 8948 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 8949 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 8950 | { |
e1915f76 | 8951 | struct io_defer_entry *de; |
b7ddce3c PB |
8952 | LIST_HEAD(list); |
8953 | ||
8954 | spin_lock_irq(&ctx->completion_lock); | |
8955 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { | |
3dd0c97a | 8956 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
8957 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8958 | break; | |
8959 | } | |
8960 | } | |
8961 | spin_unlock_irq(&ctx->completion_lock); | |
e1915f76 PB |
8962 | if (list_empty(&list)) |
8963 | return false; | |
b7ddce3c PB |
8964 | |
8965 | while (!list_empty(&list)) { | |
8966 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8967 | list_del_init(&de->list); | |
f41db273 | 8968 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
8969 | kfree(de); |
8970 | } | |
e1915f76 | 8971 | return true; |
b7ddce3c PB |
8972 | } |
8973 | ||
1b00764f PB |
8974 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
8975 | { | |
8976 | struct io_tctx_node *node; | |
8977 | enum io_wq_cancel cret; | |
8978 | bool ret = false; | |
8979 | ||
8980 | mutex_lock(&ctx->uring_lock); | |
8981 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
8982 | struct io_uring_task *tctx = node->task->io_uring; | |
8983 | ||
8984 | /* | |
8985 | * io_wq will stay alive while we hold uring_lock, because it's | |
8986 | * killed after ctx nodes, which requires to take the lock. | |
8987 | */ | |
8988 | if (!tctx || !tctx->io_wq) | |
8989 | continue; | |
8990 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
8991 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8992 | } | |
8993 | mutex_unlock(&ctx->uring_lock); | |
8994 | ||
8995 | return ret; | |
8996 | } | |
8997 | ||
9936c7c2 PB |
8998 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
8999 | struct task_struct *task, | |
3dd0c97a | 9000 | bool cancel_all) |
9936c7c2 | 9001 | { |
3dd0c97a | 9002 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9003 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9004 | |
9005 | while (1) { | |
9006 | enum io_wq_cancel cret; | |
9007 | bool ret = false; | |
9008 | ||
1b00764f PB |
9009 | if (!task) { |
9010 | ret |= io_uring_try_cancel_iowq(ctx); | |
9011 | } else if (tctx && tctx->io_wq) { | |
9012 | /* | |
9013 | * Cancels requests of all rings, not only @ctx, but | |
9014 | * it's fine as the task is in exit/exec. | |
9015 | */ | |
5aa75ed5 | 9016 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9017 | &cancel, true); |
9018 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9019 | } | |
9020 | ||
9021 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9022 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9023 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9024 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9025 | io_iopoll_try_reap_events(ctx); | |
9026 | ret = true; | |
9027 | } | |
9028 | } | |
9029 | ||
3dd0c97a PB |
9030 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9031 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9032 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9033 | if (task) |
9034 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9035 | if (!ret) |
9036 | break; | |
9037 | cond_resched(); | |
9038 | } | |
9039 | } | |
9040 | ||
eef51daa | 9041 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9042 | { |
236434c3 | 9043 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9044 | struct io_tctx_node *node; |
a528b04e | 9045 | int ret; |
236434c3 MWO |
9046 | |
9047 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9048 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9049 | if (unlikely(ret)) |
9050 | return ret; | |
236434c3 | 9051 | tctx = current->io_uring; |
0f212204 | 9052 | } |
cf27f3b1 PB |
9053 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9054 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9055 | if (!node) | |
9056 | return -ENOMEM; | |
9057 | node->ctx = ctx; | |
9058 | node->task = current; | |
13bf43f5 | 9059 | |
cf27f3b1 PB |
9060 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9061 | node, GFP_KERNEL)); | |
9062 | if (ret) { | |
9063 | kfree(node); | |
9064 | return ret; | |
0f212204 | 9065 | } |
cf27f3b1 PB |
9066 | |
9067 | mutex_lock(&ctx->uring_lock); | |
9068 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9069 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9070 | } |
cf27f3b1 | 9071 | tctx->last = ctx; |
0f212204 JA |
9072 | return 0; |
9073 | } | |
9074 | ||
cf27f3b1 PB |
9075 | /* |
9076 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9077 | */ | |
eef51daa | 9078 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9079 | { |
9080 | struct io_uring_task *tctx = current->io_uring; | |
9081 | ||
9082 | if (likely(tctx && tctx->last == ctx)) | |
9083 | return 0; | |
eef51daa | 9084 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9085 | } |
9086 | ||
0f212204 JA |
9087 | /* |
9088 | * Remove this io_uring_file -> task mapping. | |
9089 | */ | |
eef51daa | 9090 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9091 | { |
9092 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9093 | struct io_tctx_node *node; |
2941267b | 9094 | |
eebd2e37 PB |
9095 | if (!tctx) |
9096 | return; | |
13bf43f5 PB |
9097 | node = xa_erase(&tctx->xa, index); |
9098 | if (!node) | |
2941267b | 9099 | return; |
0f212204 | 9100 | |
13bf43f5 PB |
9101 | WARN_ON_ONCE(current != node->task); |
9102 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9103 | ||
9104 | mutex_lock(&node->ctx->uring_lock); | |
9105 | list_del(&node->ctx_node); | |
9106 | mutex_unlock(&node->ctx->uring_lock); | |
9107 | ||
baf186c4 | 9108 | if (tctx->last == node->ctx) |
0f212204 | 9109 | tctx->last = NULL; |
13bf43f5 | 9110 | kfree(node); |
0f212204 JA |
9111 | } |
9112 | ||
8452d4a6 | 9113 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9114 | { |
ba5ef6dc | 9115 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9116 | struct io_tctx_node *node; |
de7f1d9e PB |
9117 | unsigned long index; |
9118 | ||
13bf43f5 | 9119 | xa_for_each(&tctx->xa, index, node) |
eef51daa | 9120 | io_uring_del_tctx_node(index); |
b16ef427 ME |
9121 | if (wq) { |
9122 | /* | |
9123 | * Must be after io_uring_del_task_file() (removes nodes under | |
9124 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9125 | */ | |
9126 | tctx->io_wq = NULL; | |
ba5ef6dc | 9127 | io_wq_put_and_exit(wq); |
b16ef427 | 9128 | } |
de7f1d9e PB |
9129 | } |
9130 | ||
3f48cf18 | 9131 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9132 | { |
3f48cf18 PB |
9133 | if (tracked) |
9134 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9135 | return percpu_counter_sum(&tctx->inflight); |
9136 | } | |
9137 | ||
09899b19 PB |
9138 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9139 | { | |
9140 | struct io_uring_task *tctx = task->io_uring; | |
9141 | unsigned int refs = tctx->cached_refs; | |
9142 | ||
9143 | tctx->cached_refs = 0; | |
9144 | percpu_counter_sub(&tctx->inflight, refs); | |
9145 | put_task_struct_many(task, refs); | |
9146 | } | |
9147 | ||
78cc687b PB |
9148 | /* |
9149 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9150 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9151 | */ | |
9152 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9153 | { |
521d6a73 | 9154 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9155 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9156 | s64 inflight; |
9157 | DEFINE_WAIT(wait); | |
fdaf083c | 9158 | |
78cc687b PB |
9159 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9160 | ||
6d042ffb PO |
9161 | if (!current->io_uring) |
9162 | return; | |
17a91051 PB |
9163 | if (tctx->io_wq) |
9164 | io_wq_exit_start(tctx->io_wq); | |
9165 | ||
09899b19 | 9166 | io_uring_drop_tctx_refs(current); |
0e9ddb39 PB |
9167 | atomic_inc(&tctx->in_idle); |
9168 | do { | |
9169 | /* read completions before cancelations */ | |
78cc687b | 9170 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9171 | if (!inflight) |
9172 | break; | |
fdaf083c | 9173 | |
78cc687b PB |
9174 | if (!sqd) { |
9175 | struct io_tctx_node *node; | |
9176 | unsigned long index; | |
0f212204 | 9177 | |
78cc687b PB |
9178 | xa_for_each(&tctx->xa, index, node) { |
9179 | /* sqpoll task will cancel all its requests */ | |
9180 | if (node->ctx->sq_data) | |
9181 | continue; | |
9182 | io_uring_try_cancel_requests(node->ctx, current, | |
9183 | cancel_all); | |
9184 | } | |
9185 | } else { | |
9186 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9187 | io_uring_try_cancel_requests(ctx, current, | |
9188 | cancel_all); | |
9189 | } | |
17a91051 | 9190 | |
0f212204 | 9191 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
0f212204 | 9192 | /* |
a1bb3cd5 PB |
9193 | * If we've seen completions, retry without waiting. This |
9194 | * avoids a race where a completion comes in before we did | |
9195 | * prepare_to_wait(). | |
0f212204 | 9196 | */ |
3dd0c97a | 9197 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9198 | schedule(); |
f57555ed | 9199 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9200 | } while (1); |
fdaf083c | 9201 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9202 | |
8452d4a6 | 9203 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9204 | if (cancel_all) { |
3f48cf18 PB |
9205 | /* for exec all current's requests should be gone, kill tctx */ |
9206 | __io_uring_free(current); | |
9207 | } | |
44e728b8 PB |
9208 | } |
9209 | ||
78cc687b PB |
9210 | void __io_uring_cancel(struct files_struct *files) |
9211 | { | |
9212 | io_uring_cancel_generic(!files, NULL); | |
9213 | } | |
9214 | ||
6c5c240e RP |
9215 | static void *io_uring_validate_mmap_request(struct file *file, |
9216 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9217 | { |
2b188cc1 | 9218 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9219 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9220 | struct page *page; |
9221 | void *ptr; | |
9222 | ||
9223 | switch (offset) { | |
9224 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9225 | case IORING_OFF_CQ_RING: |
9226 | ptr = ctx->rings; | |
2b188cc1 JA |
9227 | break; |
9228 | case IORING_OFF_SQES: | |
9229 | ptr = ctx->sq_sqes; | |
9230 | break; | |
2b188cc1 | 9231 | default: |
6c5c240e | 9232 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9233 | } |
9234 | ||
9235 | page = virt_to_head_page(ptr); | |
a50b854e | 9236 | if (sz > page_size(page)) |
6c5c240e RP |
9237 | return ERR_PTR(-EINVAL); |
9238 | ||
9239 | return ptr; | |
9240 | } | |
9241 | ||
9242 | #ifdef CONFIG_MMU | |
9243 | ||
9244 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9245 | { | |
9246 | size_t sz = vma->vm_end - vma->vm_start; | |
9247 | unsigned long pfn; | |
9248 | void *ptr; | |
9249 | ||
9250 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9251 | if (IS_ERR(ptr)) | |
9252 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9253 | |
9254 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9255 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9256 | } | |
9257 | ||
6c5c240e RP |
9258 | #else /* !CONFIG_MMU */ |
9259 | ||
9260 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9261 | { | |
9262 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9263 | } | |
9264 | ||
9265 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9266 | { | |
9267 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9268 | } | |
9269 | ||
9270 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9271 | unsigned long addr, unsigned long len, | |
9272 | unsigned long pgoff, unsigned long flags) | |
9273 | { | |
9274 | void *ptr; | |
9275 | ||
9276 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9277 | if (IS_ERR(ptr)) | |
9278 | return PTR_ERR(ptr); | |
9279 | ||
9280 | return (unsigned long) ptr; | |
9281 | } | |
9282 | ||
9283 | #endif /* !CONFIG_MMU */ | |
9284 | ||
d9d05217 | 9285 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9286 | { |
9287 | DEFINE_WAIT(wait); | |
9288 | ||
9289 | do { | |
9290 | if (!io_sqring_full(ctx)) | |
9291 | break; | |
90554200 JA |
9292 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9293 | ||
9294 | if (!io_sqring_full(ctx)) | |
9295 | break; | |
90554200 JA |
9296 | schedule(); |
9297 | } while (!signal_pending(current)); | |
9298 | ||
9299 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9300 | return 0; |
90554200 JA |
9301 | } |
9302 | ||
c73ebb68 HX |
9303 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9304 | struct __kernel_timespec __user **ts, | |
9305 | const sigset_t __user **sig) | |
9306 | { | |
9307 | struct io_uring_getevents_arg arg; | |
9308 | ||
9309 | /* | |
9310 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9311 | * is just a pointer to the sigset_t. | |
9312 | */ | |
9313 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9314 | *sig = (const sigset_t __user *) argp; | |
9315 | *ts = NULL; | |
9316 | return 0; | |
9317 | } | |
9318 | ||
9319 | /* | |
9320 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9321 | * timespec and sigset_t pointers if good. | |
9322 | */ | |
9323 | if (*argsz != sizeof(arg)) | |
9324 | return -EINVAL; | |
9325 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9326 | return -EFAULT; | |
9327 | *sig = u64_to_user_ptr(arg.sigmask); | |
9328 | *argsz = arg.sigmask_sz; | |
9329 | *ts = u64_to_user_ptr(arg.ts); | |
9330 | return 0; | |
9331 | } | |
9332 | ||
2b188cc1 | 9333 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9334 | u32, min_complete, u32, flags, const void __user *, argp, |
9335 | size_t, argsz) | |
2b188cc1 JA |
9336 | { |
9337 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9338 | int submitted = 0; |
9339 | struct fd f; | |
33f993da | 9340 | long ret; |
2b188cc1 | 9341 | |
4c6e277c | 9342 | io_run_task_work(); |
b41e9852 | 9343 | |
33f993da PB |
9344 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9345 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9346 | return -EINVAL; |
9347 | ||
9348 | f = fdget(fd); | |
33f993da | 9349 | if (unlikely(!f.file)) |
2b188cc1 JA |
9350 | return -EBADF; |
9351 | ||
9352 | ret = -EOPNOTSUPP; | |
33f993da | 9353 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9354 | goto out_fput; |
9355 | ||
9356 | ret = -ENXIO; | |
9357 | ctx = f.file->private_data; | |
33f993da | 9358 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9359 | goto out_fput; |
9360 | ||
7e84e1c7 | 9361 | ret = -EBADFD; |
33f993da | 9362 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9363 | goto out; |
9364 | ||
6c271ce2 JA |
9365 | /* |
9366 | * For SQ polling, the thread will do all submissions and completions. | |
9367 | * Just return the requested submit count, and wake the thread if | |
9368 | * we were asked to. | |
9369 | */ | |
b2a9eada | 9370 | ret = 0; |
6c271ce2 | 9371 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
6c2450ae | 9372 | io_cqring_overflow_flush(ctx, false); |
89448c47 | 9373 | |
21f96522 JA |
9374 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9375 | ret = -EOWNERDEAD; | |
04147488 | 9376 | goto out; |
21f96522 | 9377 | } |
6c271ce2 | 9378 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9379 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9380 | if (flags & IORING_ENTER_SQ_WAIT) { |
9381 | ret = io_sqpoll_wait_sq(ctx); | |
9382 | if (ret) | |
9383 | goto out; | |
9384 | } | |
6c271ce2 | 9385 | submitted = to_submit; |
b2a9eada | 9386 | } else if (to_submit) { |
eef51daa | 9387 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9388 | if (unlikely(ret)) |
9389 | goto out; | |
2b188cc1 | 9390 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9391 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9392 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9393 | |
9394 | if (submitted != to_submit) | |
9395 | goto out; | |
2b188cc1 JA |
9396 | } |
9397 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9398 | const sigset_t __user *sig; |
9399 | struct __kernel_timespec __user *ts; | |
9400 | ||
9401 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9402 | if (unlikely(ret)) | |
9403 | goto out; | |
9404 | ||
2b188cc1 JA |
9405 | min_complete = min(min_complete, ctx->cq_entries); |
9406 | ||
32b2244a XW |
9407 | /* |
9408 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9409 | * space applications don't need to do io completion events | |
9410 | * polling again, they can rely on io_sq_thread to do polling | |
9411 | * work, which can reduce cpu usage and uring_lock contention. | |
9412 | */ | |
9413 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9414 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9415 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9416 | } else { |
c73ebb68 | 9417 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9418 | } |
2b188cc1 JA |
9419 | } |
9420 | ||
7c504e65 | 9421 | out: |
6805b32e | 9422 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9423 | out_fput: |
9424 | fdput(f); | |
9425 | return submitted ? submitted : ret; | |
9426 | } | |
9427 | ||
bebdb65e | 9428 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9429 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9430 | const struct cred *cred) | |
87ce955b | 9431 | { |
87ce955b JA |
9432 | struct user_namespace *uns = seq_user_ns(m); |
9433 | struct group_info *gi; | |
9434 | kernel_cap_t cap; | |
9435 | unsigned __capi; | |
9436 | int g; | |
9437 | ||
9438 | seq_printf(m, "%5d\n", id); | |
9439 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9440 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9441 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9442 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9443 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9444 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9445 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9446 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9447 | seq_puts(m, "\n\tGroups:\t"); | |
9448 | gi = cred->group_info; | |
9449 | for (g = 0; g < gi->ngroups; g++) { | |
9450 | seq_put_decimal_ull(m, g ? " " : "", | |
9451 | from_kgid_munged(uns, gi->gid[g])); | |
9452 | } | |
9453 | seq_puts(m, "\n\tCapEff:\t"); | |
9454 | cap = cred->cap_effective; | |
9455 | CAP_FOR_EACH_U32(__capi) | |
9456 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9457 | seq_putc(m, '\n'); | |
9458 | return 0; | |
9459 | } | |
9460 | ||
9461 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9462 | { | |
dbbe9c64 | 9463 | struct io_sq_data *sq = NULL; |
fad8e0de | 9464 | bool has_lock; |
87ce955b JA |
9465 | int i; |
9466 | ||
fad8e0de JA |
9467 | /* |
9468 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9469 | * since fdinfo case grabs it in the opposite direction of normal use | |
9470 | * cases. If we fail to get the lock, we just don't iterate any | |
9471 | * structures that could be going away outside the io_uring mutex. | |
9472 | */ | |
9473 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9474 | ||
5f3f26f9 | 9475 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9476 | sq = ctx->sq_data; |
5f3f26f9 JA |
9477 | if (!sq->thread) |
9478 | sq = NULL; | |
9479 | } | |
dbbe9c64 JQ |
9480 | |
9481 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9482 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9483 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9484 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9485 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9486 | |
87ce955b JA |
9487 | if (f) |
9488 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9489 | else | |
9490 | seq_printf(m, "%5u: <none>\n", i); | |
9491 | } | |
9492 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9493 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 9494 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 9495 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9496 | |
4751f53d | 9497 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9498 | } |
61cf9370 MWO |
9499 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9500 | unsigned long index; | |
9501 | const struct cred *cred; | |
9502 | ||
87ce955b | 9503 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9504 | xa_for_each(&ctx->personalities, index, cred) |
9505 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9506 | } |
d7718a9d JA |
9507 | seq_printf(m, "PollList:\n"); |
9508 | spin_lock_irq(&ctx->completion_lock); | |
9509 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { | |
9510 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9511 | struct io_kiocb *req; | |
9512 | ||
9513 | hlist_for_each_entry(req, list, hash_node) | |
9514 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9515 | req->task->task_works != NULL); | |
9516 | } | |
9517 | spin_unlock_irq(&ctx->completion_lock); | |
fad8e0de JA |
9518 | if (has_lock) |
9519 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9520 | } |
9521 | ||
9522 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9523 | { | |
9524 | struct io_ring_ctx *ctx = f->private_data; | |
9525 | ||
9526 | if (percpu_ref_tryget(&ctx->refs)) { | |
9527 | __io_uring_show_fdinfo(ctx, m); | |
9528 | percpu_ref_put(&ctx->refs); | |
9529 | } | |
9530 | } | |
bebdb65e | 9531 | #endif |
87ce955b | 9532 | |
2b188cc1 JA |
9533 | static const struct file_operations io_uring_fops = { |
9534 | .release = io_uring_release, | |
9535 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9536 | #ifndef CONFIG_MMU |
9537 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9538 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9539 | #endif | |
2b188cc1 JA |
9540 | .poll = io_uring_poll, |
9541 | .fasync = io_uring_fasync, | |
bebdb65e | 9542 | #ifdef CONFIG_PROC_FS |
87ce955b | 9543 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9544 | #endif |
2b188cc1 JA |
9545 | }; |
9546 | ||
9547 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9548 | struct io_uring_params *p) | |
9549 | { | |
75b28aff HV |
9550 | struct io_rings *rings; |
9551 | size_t size, sq_array_offset; | |
2b188cc1 | 9552 | |
bd740481 JA |
9553 | /* make sure these are sane, as we already accounted them */ |
9554 | ctx->sq_entries = p->sq_entries; | |
9555 | ctx->cq_entries = p->cq_entries; | |
9556 | ||
75b28aff HV |
9557 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9558 | if (size == SIZE_MAX) | |
9559 | return -EOVERFLOW; | |
9560 | ||
9561 | rings = io_mem_alloc(size); | |
9562 | if (!rings) | |
2b188cc1 JA |
9563 | return -ENOMEM; |
9564 | ||
75b28aff HV |
9565 | ctx->rings = rings; |
9566 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9567 | rings->sq_ring_mask = p->sq_entries - 1; | |
9568 | rings->cq_ring_mask = p->cq_entries - 1; | |
9569 | rings->sq_ring_entries = p->sq_entries; | |
9570 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
9571 | |
9572 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9573 | if (size == SIZE_MAX) { |
9574 | io_mem_free(ctx->rings); | |
9575 | ctx->rings = NULL; | |
2b188cc1 | 9576 | return -EOVERFLOW; |
eb065d30 | 9577 | } |
2b188cc1 JA |
9578 | |
9579 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9580 | if (!ctx->sq_sqes) { |
9581 | io_mem_free(ctx->rings); | |
9582 | ctx->rings = NULL; | |
2b188cc1 | 9583 | return -ENOMEM; |
eb065d30 | 9584 | } |
2b188cc1 | 9585 | |
2b188cc1 JA |
9586 | return 0; |
9587 | } | |
9588 | ||
9faadcc8 PB |
9589 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9590 | { | |
9591 | int ret, fd; | |
9592 | ||
9593 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9594 | if (fd < 0) | |
9595 | return fd; | |
9596 | ||
eef51daa | 9597 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
9598 | if (ret) { |
9599 | put_unused_fd(fd); | |
9600 | return ret; | |
9601 | } | |
9602 | fd_install(fd, file); | |
9603 | return fd; | |
9604 | } | |
9605 | ||
2b188cc1 JA |
9606 | /* |
9607 | * Allocate an anonymous fd, this is what constitutes the application | |
9608 | * visible backing of an io_uring instance. The application mmaps this | |
9609 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9610 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9611 | */ | |
9faadcc8 | 9612 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9613 | { |
9614 | struct file *file; | |
9faadcc8 | 9615 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9616 | int ret; |
9617 | ||
2b188cc1 JA |
9618 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9619 | &ctx->ring_sock); | |
9620 | if (ret) | |
9faadcc8 | 9621 | return ERR_PTR(ret); |
2b188cc1 JA |
9622 | #endif |
9623 | ||
2b188cc1 JA |
9624 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9625 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9626 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9627 | if (IS_ERR(file)) { |
9628 | sock_release(ctx->ring_sock); | |
9629 | ctx->ring_sock = NULL; | |
9630 | } else { | |
9631 | ctx->ring_sock->file = file; | |
0f212204 | 9632 | } |
2b188cc1 | 9633 | #endif |
9faadcc8 | 9634 | return file; |
2b188cc1 JA |
9635 | } |
9636 | ||
7f13657d XW |
9637 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9638 | struct io_uring_params __user *params) | |
2b188cc1 | 9639 | { |
2b188cc1 | 9640 | struct io_ring_ctx *ctx; |
9faadcc8 | 9641 | struct file *file; |
2b188cc1 JA |
9642 | int ret; |
9643 | ||
8110c1a6 | 9644 | if (!entries) |
2b188cc1 | 9645 | return -EINVAL; |
8110c1a6 JA |
9646 | if (entries > IORING_MAX_ENTRIES) { |
9647 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9648 | return -EINVAL; | |
9649 | entries = IORING_MAX_ENTRIES; | |
9650 | } | |
2b188cc1 JA |
9651 | |
9652 | /* | |
9653 | * Use twice as many entries for the CQ ring. It's possible for the | |
9654 | * application to drive a higher depth than the size of the SQ ring, | |
9655 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9656 | * some flexibility in overcommitting a bit. If the application has |
9657 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9658 | * of CQ ring entries manually. | |
2b188cc1 JA |
9659 | */ |
9660 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9661 | if (p->flags & IORING_SETUP_CQSIZE) { |
9662 | /* | |
9663 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9664 | * to a power-of-two, if it isn't already. We do NOT impose | |
9665 | * any cq vs sq ring sizing. | |
9666 | */ | |
eb2667b3 | 9667 | if (!p->cq_entries) |
33a107f0 | 9668 | return -EINVAL; |
8110c1a6 JA |
9669 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9670 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9671 | return -EINVAL; | |
9672 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9673 | } | |
eb2667b3 JQ |
9674 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9675 | if (p->cq_entries < p->sq_entries) | |
9676 | return -EINVAL; | |
33a107f0 JA |
9677 | } else { |
9678 | p->cq_entries = 2 * p->sq_entries; | |
9679 | } | |
2b188cc1 | 9680 | |
2b188cc1 | 9681 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9682 | if (!ctx) |
2b188cc1 | 9683 | return -ENOMEM; |
2b188cc1 | 9684 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9685 | if (!capable(CAP_IPC_LOCK)) |
9686 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9687 | |
9688 | /* | |
9689 | * This is just grabbed for accounting purposes. When a process exits, | |
9690 | * the mm is exited and dropped before the files, hence we need to hang | |
9691 | * on to this mm purely for the purposes of being able to unaccount | |
9692 | * memory (locked/pinned vm). It's not used for anything else. | |
9693 | */ | |
6b7898eb | 9694 | mmgrab(current->mm); |
2aede0e4 | 9695 | ctx->mm_account = current->mm; |
6b7898eb | 9696 | |
2b188cc1 JA |
9697 | ret = io_allocate_scq_urings(ctx, p); |
9698 | if (ret) | |
9699 | goto err; | |
9700 | ||
7e84e1c7 | 9701 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9702 | if (ret) |
9703 | goto err; | |
eae071c9 | 9704 | /* always set a rsrc node */ |
47b228ce PB |
9705 | ret = io_rsrc_node_switch_start(ctx); |
9706 | if (ret) | |
9707 | goto err; | |
eae071c9 | 9708 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 9709 | |
2b188cc1 | 9710 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9711 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9712 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9713 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9714 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9715 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9716 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9717 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9718 | |
9719 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9720 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9721 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9722 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9723 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9724 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9725 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9726 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9727 | |
7f13657d XW |
9728 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9729 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9730 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9731 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
9732 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
9733 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
9734 | |
9735 | if (copy_to_user(params, p, sizeof(*p))) { | |
9736 | ret = -EFAULT; | |
9737 | goto err; | |
9738 | } | |
d1719f70 | 9739 | |
9faadcc8 PB |
9740 | file = io_uring_get_file(ctx); |
9741 | if (IS_ERR(file)) { | |
9742 | ret = PTR_ERR(file); | |
9743 | goto err; | |
9744 | } | |
9745 | ||
044c1ab3 JA |
9746 | /* |
9747 | * Install ring fd as the very last thing, so we don't risk someone | |
9748 | * having closed it before we finish setup | |
9749 | */ | |
9faadcc8 PB |
9750 | ret = io_uring_install_fd(ctx, file); |
9751 | if (ret < 0) { | |
9752 | /* fput will clean it up */ | |
9753 | fput(file); | |
9754 | return ret; | |
9755 | } | |
044c1ab3 | 9756 | |
c826bd7a | 9757 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9758 | return ret; |
9759 | err: | |
9760 | io_ring_ctx_wait_and_kill(ctx); | |
9761 | return ret; | |
9762 | } | |
9763 | ||
9764 | /* | |
9765 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9766 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9767 | * params structure passed in. | |
9768 | */ | |
9769 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9770 | { | |
9771 | struct io_uring_params p; | |
2b188cc1 JA |
9772 | int i; |
9773 | ||
9774 | if (copy_from_user(&p, params, sizeof(p))) | |
9775 | return -EFAULT; | |
9776 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9777 | if (p.resv[i]) | |
9778 | return -EINVAL; | |
9779 | } | |
9780 | ||
6c271ce2 | 9781 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9782 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9783 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9784 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9785 | return -EINVAL; |
9786 | ||
7f13657d | 9787 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9788 | } |
9789 | ||
9790 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9791 | struct io_uring_params __user *, params) | |
9792 | { | |
9793 | return io_uring_setup(entries, params); | |
9794 | } | |
9795 | ||
66f4af93 JA |
9796 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9797 | { | |
9798 | struct io_uring_probe *p; | |
9799 | size_t size; | |
9800 | int i, ret; | |
9801 | ||
9802 | size = struct_size(p, ops, nr_args); | |
9803 | if (size == SIZE_MAX) | |
9804 | return -EOVERFLOW; | |
9805 | p = kzalloc(size, GFP_KERNEL); | |
9806 | if (!p) | |
9807 | return -ENOMEM; | |
9808 | ||
9809 | ret = -EFAULT; | |
9810 | if (copy_from_user(p, arg, size)) | |
9811 | goto out; | |
9812 | ret = -EINVAL; | |
9813 | if (memchr_inv(p, 0, size)) | |
9814 | goto out; | |
9815 | ||
9816 | p->last_op = IORING_OP_LAST - 1; | |
9817 | if (nr_args > IORING_OP_LAST) | |
9818 | nr_args = IORING_OP_LAST; | |
9819 | ||
9820 | for (i = 0; i < nr_args; i++) { | |
9821 | p->ops[i].op = i; | |
9822 | if (!io_op_defs[i].not_supported) | |
9823 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9824 | } | |
9825 | p->ops_len = i; | |
9826 | ||
9827 | ret = 0; | |
9828 | if (copy_to_user(arg, p, size)) | |
9829 | ret = -EFAULT; | |
9830 | out: | |
9831 | kfree(p); | |
9832 | return ret; | |
9833 | } | |
9834 | ||
071698e1 JA |
9835 | static int io_register_personality(struct io_ring_ctx *ctx) |
9836 | { | |
4379bf8b | 9837 | const struct cred *creds; |
61cf9370 | 9838 | u32 id; |
1e6fa521 | 9839 | int ret; |
071698e1 | 9840 | |
4379bf8b | 9841 | creds = get_current_cred(); |
1e6fa521 | 9842 | |
61cf9370 MWO |
9843 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
9844 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
9845 | if (ret < 0) { |
9846 | put_cred(creds); | |
9847 | return ret; | |
9848 | } | |
9849 | return id; | |
071698e1 JA |
9850 | } |
9851 | ||
21b55dbc SG |
9852 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9853 | unsigned int nr_args) | |
9854 | { | |
9855 | struct io_uring_restriction *res; | |
9856 | size_t size; | |
9857 | int i, ret; | |
9858 | ||
7e84e1c7 SG |
9859 | /* Restrictions allowed only if rings started disabled */ |
9860 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9861 | return -EBADFD; | |
9862 | ||
21b55dbc | 9863 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9864 | if (ctx->restrictions.registered) |
21b55dbc SG |
9865 | return -EBUSY; |
9866 | ||
9867 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9868 | return -EINVAL; | |
9869 | ||
9870 | size = array_size(nr_args, sizeof(*res)); | |
9871 | if (size == SIZE_MAX) | |
9872 | return -EOVERFLOW; | |
9873 | ||
9874 | res = memdup_user(arg, size); | |
9875 | if (IS_ERR(res)) | |
9876 | return PTR_ERR(res); | |
9877 | ||
9878 | ret = 0; | |
9879 | ||
9880 | for (i = 0; i < nr_args; i++) { | |
9881 | switch (res[i].opcode) { | |
9882 | case IORING_RESTRICTION_REGISTER_OP: | |
9883 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9884 | ret = -EINVAL; | |
9885 | goto out; | |
9886 | } | |
9887 | ||
9888 | __set_bit(res[i].register_op, | |
9889 | ctx->restrictions.register_op); | |
9890 | break; | |
9891 | case IORING_RESTRICTION_SQE_OP: | |
9892 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9893 | ret = -EINVAL; | |
9894 | goto out; | |
9895 | } | |
9896 | ||
9897 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9898 | break; | |
9899 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9900 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9901 | break; | |
9902 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9903 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9904 | break; | |
9905 | default: | |
9906 | ret = -EINVAL; | |
9907 | goto out; | |
9908 | } | |
9909 | } | |
9910 | ||
9911 | out: | |
9912 | /* Reset all restrictions if an error happened */ | |
9913 | if (ret != 0) | |
9914 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9915 | else | |
7e84e1c7 | 9916 | ctx->restrictions.registered = true; |
21b55dbc SG |
9917 | |
9918 | kfree(res); | |
9919 | return ret; | |
9920 | } | |
9921 | ||
7e84e1c7 SG |
9922 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9923 | { | |
9924 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9925 | return -EBADFD; | |
9926 | ||
9927 | if (ctx->restrictions.registered) | |
9928 | ctx->restricted = 1; | |
9929 | ||
0298ef96 PB |
9930 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
9931 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
9932 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
9933 | return 0; |
9934 | } | |
9935 | ||
fdecb662 | 9936 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 9937 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
9938 | unsigned nr_args) |
9939 | { | |
9940 | __u32 tmp; | |
9941 | int err; | |
9942 | ||
c3bdad02 PB |
9943 | if (up->resv) |
9944 | return -EINVAL; | |
98f0b3b4 PB |
9945 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
9946 | return -EOVERFLOW; | |
9947 | err = io_rsrc_node_switch_start(ctx); | |
9948 | if (err) | |
9949 | return err; | |
9950 | ||
fdecb662 PB |
9951 | switch (type) { |
9952 | case IORING_RSRC_FILE: | |
98f0b3b4 | 9953 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
9954 | case IORING_RSRC_BUFFER: |
9955 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
9956 | } |
9957 | return -EINVAL; | |
9958 | } | |
9959 | ||
c3bdad02 PB |
9960 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
9961 | unsigned nr_args) | |
98f0b3b4 | 9962 | { |
c3bdad02 | 9963 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
9964 | |
9965 | if (!nr_args) | |
9966 | return -EINVAL; | |
c3bdad02 PB |
9967 | memset(&up, 0, sizeof(up)); |
9968 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
9969 | return -EFAULT; | |
9970 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
9971 | } | |
9972 | ||
9973 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 9974 | unsigned size, unsigned type) |
c3bdad02 PB |
9975 | { |
9976 | struct io_uring_rsrc_update2 up; | |
9977 | ||
9978 | if (size != sizeof(up)) | |
9979 | return -EINVAL; | |
98f0b3b4 PB |
9980 | if (copy_from_user(&up, arg, sizeof(up))) |
9981 | return -EFAULT; | |
992da01a | 9982 | if (!up.nr || up.resv) |
98f0b3b4 | 9983 | return -EINVAL; |
992da01a | 9984 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
9985 | } |
9986 | ||
792e3582 | 9987 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 9988 | unsigned int size, unsigned int type) |
792e3582 PB |
9989 | { |
9990 | struct io_uring_rsrc_register rr; | |
9991 | ||
9992 | /* keep it extendible */ | |
9993 | if (size != sizeof(rr)) | |
9994 | return -EINVAL; | |
9995 | ||
9996 | memset(&rr, 0, sizeof(rr)); | |
9997 | if (copy_from_user(&rr, arg, size)) | |
9998 | return -EFAULT; | |
992da01a | 9999 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10000 | return -EINVAL; |
10001 | ||
992da01a | 10002 | switch (type) { |
792e3582 PB |
10003 | case IORING_RSRC_FILE: |
10004 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10005 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10006 | case IORING_RSRC_BUFFER: |
10007 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10008 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10009 | } |
10010 | return -EINVAL; | |
10011 | } | |
10012 | ||
fe76421d JA |
10013 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10014 | unsigned len) | |
10015 | { | |
10016 | struct io_uring_task *tctx = current->io_uring; | |
10017 | cpumask_var_t new_mask; | |
10018 | int ret; | |
10019 | ||
10020 | if (!tctx || !tctx->io_wq) | |
10021 | return -EINVAL; | |
10022 | ||
10023 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10024 | return -ENOMEM; | |
10025 | ||
10026 | cpumask_clear(new_mask); | |
10027 | if (len > cpumask_size()) | |
10028 | len = cpumask_size(); | |
10029 | ||
10030 | if (copy_from_user(new_mask, arg, len)) { | |
10031 | free_cpumask_var(new_mask); | |
10032 | return -EFAULT; | |
10033 | } | |
10034 | ||
10035 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10036 | free_cpumask_var(new_mask); | |
10037 | return ret; | |
10038 | } | |
10039 | ||
10040 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10041 | { | |
10042 | struct io_uring_task *tctx = current->io_uring; | |
10043 | ||
10044 | if (!tctx || !tctx->io_wq) | |
10045 | return -EINVAL; | |
10046 | ||
10047 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10048 | } | |
10049 | ||
071698e1 JA |
10050 | static bool io_register_op_must_quiesce(int op) |
10051 | { | |
10052 | switch (op) { | |
bd54b6fe BM |
10053 | case IORING_REGISTER_BUFFERS: |
10054 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10055 | case IORING_REGISTER_FILES: |
071698e1 JA |
10056 | case IORING_UNREGISTER_FILES: |
10057 | case IORING_REGISTER_FILES_UPDATE: | |
10058 | case IORING_REGISTER_PROBE: | |
10059 | case IORING_REGISTER_PERSONALITY: | |
10060 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10061 | case IORING_REGISTER_FILES2: |
10062 | case IORING_REGISTER_FILES_UPDATE2: | |
10063 | case IORING_REGISTER_BUFFERS2: | |
10064 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10065 | case IORING_REGISTER_IOWQ_AFF: |
10066 | case IORING_UNREGISTER_IOWQ_AFF: | |
071698e1 JA |
10067 | return false; |
10068 | default: | |
10069 | return true; | |
10070 | } | |
10071 | } | |
10072 | ||
edafccee JA |
10073 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10074 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10075 | __releases(ctx->uring_lock) |
10076 | __acquires(ctx->uring_lock) | |
edafccee JA |
10077 | { |
10078 | int ret; | |
10079 | ||
35fa71a0 JA |
10080 | /* |
10081 | * We're inside the ring mutex, if the ref is already dying, then | |
10082 | * someone else killed the ctx or is already going through | |
10083 | * io_uring_register(). | |
10084 | */ | |
10085 | if (percpu_ref_is_dying(&ctx->refs)) | |
10086 | return -ENXIO; | |
10087 | ||
75c4021a PB |
10088 | if (ctx->restricted) { |
10089 | if (opcode >= IORING_REGISTER_LAST) | |
10090 | return -EINVAL; | |
10091 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10092 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10093 | return -EACCES; | |
10094 | } | |
10095 | ||
071698e1 | 10096 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10097 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 10098 | |
05f3fb3c JA |
10099 | /* |
10100 | * Drop uring mutex before waiting for references to exit. If | |
10101 | * another thread is currently inside io_uring_enter() it might | |
10102 | * need to grab the uring_lock to make progress. If we hold it | |
10103 | * here across the drain wait, then we can deadlock. It's safe | |
10104 | * to drop the mutex here, since no new references will come in | |
10105 | * after we've killed the percpu ref. | |
10106 | */ | |
10107 | mutex_unlock(&ctx->uring_lock); | |
af9c1a44 JA |
10108 | do { |
10109 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10110 | if (!ret) | |
10111 | break; | |
ed6930c9 JA |
10112 | ret = io_run_task_work_sig(); |
10113 | if (ret < 0) | |
10114 | break; | |
af9c1a44 | 10115 | } while (1); |
05f3fb3c | 10116 | mutex_lock(&ctx->uring_lock); |
af9c1a44 | 10117 | |
c150368b | 10118 | if (ret) { |
f70865db PB |
10119 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
10120 | return ret; | |
c150368b | 10121 | } |
05f3fb3c | 10122 | } |
edafccee JA |
10123 | |
10124 | switch (opcode) { | |
10125 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10126 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10127 | break; |
10128 | case IORING_UNREGISTER_BUFFERS: | |
10129 | ret = -EINVAL; | |
10130 | if (arg || nr_args) | |
10131 | break; | |
0a96bbe4 | 10132 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10133 | break; |
6b06314c | 10134 | case IORING_REGISTER_FILES: |
792e3582 | 10135 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10136 | break; |
10137 | case IORING_UNREGISTER_FILES: | |
10138 | ret = -EINVAL; | |
10139 | if (arg || nr_args) | |
10140 | break; | |
10141 | ret = io_sqe_files_unregister(ctx); | |
10142 | break; | |
c3a31e60 | 10143 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10144 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10145 | break; |
9b402849 | 10146 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10147 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10148 | ret = -EINVAL; |
10149 | if (nr_args != 1) | |
10150 | break; | |
10151 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10152 | if (ret) |
10153 | break; | |
10154 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10155 | ctx->eventfd_async = 1; | |
10156 | else | |
10157 | ctx->eventfd_async = 0; | |
9b402849 JA |
10158 | break; |
10159 | case IORING_UNREGISTER_EVENTFD: | |
10160 | ret = -EINVAL; | |
10161 | if (arg || nr_args) | |
10162 | break; | |
10163 | ret = io_eventfd_unregister(ctx); | |
10164 | break; | |
66f4af93 JA |
10165 | case IORING_REGISTER_PROBE: |
10166 | ret = -EINVAL; | |
10167 | if (!arg || nr_args > 256) | |
10168 | break; | |
10169 | ret = io_probe(ctx, arg, nr_args); | |
10170 | break; | |
071698e1 JA |
10171 | case IORING_REGISTER_PERSONALITY: |
10172 | ret = -EINVAL; | |
10173 | if (arg || nr_args) | |
10174 | break; | |
10175 | ret = io_register_personality(ctx); | |
10176 | break; | |
10177 | case IORING_UNREGISTER_PERSONALITY: | |
10178 | ret = -EINVAL; | |
10179 | if (arg) | |
10180 | break; | |
10181 | ret = io_unregister_personality(ctx, nr_args); | |
10182 | break; | |
7e84e1c7 SG |
10183 | case IORING_REGISTER_ENABLE_RINGS: |
10184 | ret = -EINVAL; | |
10185 | if (arg || nr_args) | |
10186 | break; | |
10187 | ret = io_register_enable_rings(ctx); | |
10188 | break; | |
21b55dbc SG |
10189 | case IORING_REGISTER_RESTRICTIONS: |
10190 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10191 | break; | |
992da01a PB |
10192 | case IORING_REGISTER_FILES2: |
10193 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10194 | break; | |
10195 | case IORING_REGISTER_FILES_UPDATE2: | |
10196 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10197 | IORING_RSRC_FILE); | |
10198 | break; | |
10199 | case IORING_REGISTER_BUFFERS2: | |
10200 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10201 | break; |
992da01a PB |
10202 | case IORING_REGISTER_BUFFERS_UPDATE: |
10203 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10204 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10205 | break; |
fe76421d JA |
10206 | case IORING_REGISTER_IOWQ_AFF: |
10207 | ret = -EINVAL; | |
10208 | if (!arg || !nr_args) | |
10209 | break; | |
10210 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10211 | break; | |
10212 | case IORING_UNREGISTER_IOWQ_AFF: | |
10213 | ret = -EINVAL; | |
10214 | if (arg || nr_args) | |
10215 | break; | |
10216 | ret = io_unregister_iowq_aff(ctx); | |
10217 | break; | |
edafccee JA |
10218 | default: |
10219 | ret = -EINVAL; | |
10220 | break; | |
10221 | } | |
10222 | ||
071698e1 | 10223 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10224 | /* bring the ctx back to life */ |
05f3fb3c | 10225 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10226 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10227 | } |
edafccee JA |
10228 | return ret; |
10229 | } | |
10230 | ||
10231 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10232 | void __user *, arg, unsigned int, nr_args) | |
10233 | { | |
10234 | struct io_ring_ctx *ctx; | |
10235 | long ret = -EBADF; | |
10236 | struct fd f; | |
10237 | ||
10238 | f = fdget(fd); | |
10239 | if (!f.file) | |
10240 | return -EBADF; | |
10241 | ||
10242 | ret = -EOPNOTSUPP; | |
10243 | if (f.file->f_op != &io_uring_fops) | |
10244 | goto out_fput; | |
10245 | ||
10246 | ctx = f.file->private_data; | |
10247 | ||
b6c23dd5 PB |
10248 | io_run_task_work(); |
10249 | ||
edafccee JA |
10250 | mutex_lock(&ctx->uring_lock); |
10251 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10252 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10253 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10254 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10255 | out_fput: |
10256 | fdput(f); | |
10257 | return ret; | |
10258 | } | |
10259 | ||
2b188cc1 JA |
10260 | static int __init io_uring_init(void) |
10261 | { | |
d7f62e82 SM |
10262 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10263 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10264 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10265 | } while (0) | |
10266 | ||
10267 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10268 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10269 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10270 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10271 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10272 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10273 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10274 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10275 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10276 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10277 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10278 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10279 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10280 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10281 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10282 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10283 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10284 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10285 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10286 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10287 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10288 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10289 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10290 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10291 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10292 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10293 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10294 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10295 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10296 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10297 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10298 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10299 | |
b0d658ec PB |
10300 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10301 | sizeof(struct io_uring_rsrc_update)); | |
10302 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10303 | sizeof(struct io_uring_rsrc_update2)); | |
10304 | /* should fit into one byte */ | |
10305 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
10306 | ||
d3656344 | 10307 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10308 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
16340eab | 10309 | |
91f245d5 JA |
10310 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10311 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10312 | return 0; |
10313 | }; | |
10314 | __initcall(io_uring_init); |