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 | ||
6dd0be1e | 298 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 299 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 300 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 301 | |
a1ab7b35 PB |
302 | struct io_submit_link { |
303 | struct io_kiocb *head; | |
304 | struct io_kiocb *last; | |
305 | }; | |
306 | ||
258b29a9 PB |
307 | struct io_submit_state { |
308 | struct blk_plug plug; | |
a1ab7b35 | 309 | struct io_submit_link link; |
258b29a9 PB |
310 | |
311 | /* | |
312 | * io_kiocb alloc cache | |
313 | */ | |
bf019da7 | 314 | void *reqs[IO_REQ_CACHE_SIZE]; |
258b29a9 PB |
315 | unsigned int free_reqs; |
316 | ||
317 | bool plug_started; | |
318 | ||
319 | /* | |
320 | * Batch completion logic | |
321 | */ | |
cd0ca2e0 PB |
322 | struct io_kiocb *compl_reqs[IO_COMPL_BATCH]; |
323 | unsigned int compl_nr; | |
324 | /* inline/task_work completion list, under ->uring_lock */ | |
325 | struct list_head free_list; | |
258b29a9 | 326 | |
258b29a9 PB |
327 | unsigned int ios_left; |
328 | }; | |
329 | ||
2b188cc1 | 330 | struct io_ring_ctx { |
b52ecf8c | 331 | /* const or read-mostly hot data */ |
2b188cc1 JA |
332 | struct { |
333 | struct percpu_ref refs; | |
2b188cc1 | 334 | |
b52ecf8c | 335 | struct io_rings *rings; |
2b188cc1 | 336 | unsigned int flags; |
e1d85334 | 337 | unsigned int compat: 1; |
e1d85334 RD |
338 | unsigned int drain_next: 1; |
339 | unsigned int eventfd_async: 1; | |
21b55dbc | 340 | unsigned int restricted: 1; |
f18ee4cf | 341 | unsigned int off_timeout_used: 1; |
10c66904 | 342 | unsigned int drain_active: 1; |
b52ecf8c | 343 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 344 | |
7f1129d2 | 345 | /* submission data */ |
b52ecf8c | 346 | struct { |
0499e582 PB |
347 | struct mutex uring_lock; |
348 | ||
75b28aff HV |
349 | /* |
350 | * Ring buffer of indices into array of io_uring_sqe, which is | |
351 | * mmapped by the application using the IORING_OFF_SQES offset. | |
352 | * | |
353 | * This indirection could e.g. be used to assign fixed | |
354 | * io_uring_sqe entries to operations and only submit them to | |
355 | * the queue when needed. | |
356 | * | |
357 | * The kernel modifies neither the indices array nor the entries | |
358 | * array. | |
359 | */ | |
360 | u32 *sq_array; | |
c7af47cf | 361 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
362 | unsigned cached_sq_head; |
363 | unsigned sq_entries; | |
de0617e4 | 364 | struct list_head defer_list; |
7f1129d2 PB |
365 | |
366 | /* | |
367 | * Fixed resources fast path, should be accessed only under | |
368 | * uring_lock, and updated through io_uring_register(2) | |
369 | */ | |
370 | struct io_rsrc_node *rsrc_node; | |
371 | struct io_file_table file_table; | |
372 | unsigned nr_user_files; | |
373 | unsigned nr_user_bufs; | |
374 | struct io_mapped_ubuf **user_bufs; | |
375 | ||
376 | struct io_submit_state submit_state; | |
5262f567 | 377 | struct list_head timeout_list; |
1d7bb1d5 | 378 | struct list_head cq_overflow_list; |
7f1129d2 PB |
379 | struct xarray io_buffers; |
380 | struct xarray personalities; | |
381 | u32 pers_next; | |
382 | unsigned sq_thread_idle; | |
2b188cc1 JA |
383 | } ____cacheline_aligned_in_smp; |
384 | ||
d0acdee2 PB |
385 | /* IRQ completion list, under ->completion_lock */ |
386 | struct list_head locked_free_list; | |
387 | unsigned int locked_free_nr; | |
3c1a2ead | 388 | |
7c30f36a | 389 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
391 | ||
90554200 | 392 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 393 | struct list_head sqd_list; |
75b28aff | 394 | |
5ed7a37d PB |
395 | unsigned long check_cq_overflow; |
396 | ||
206aefde JA |
397 | struct { |
398 | unsigned cached_cq_tail; | |
399 | unsigned cq_entries; | |
0499e582 | 400 | struct eventfd_ctx *cq_ev_fd; |
311997b3 | 401 | struct wait_queue_head poll_wait; |
0499e582 PB |
402 | struct wait_queue_head cq_wait; |
403 | unsigned cq_extra; | |
404 | atomic_t cq_timeouts; | |
206aefde | 405 | struct fasync_struct *cq_fasync; |
0499e582 | 406 | unsigned cq_last_tm_flush; |
206aefde | 407 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 408 | |
2b188cc1 JA |
409 | struct { |
410 | spinlock_t completion_lock; | |
e94f141b | 411 | |
89850fce JA |
412 | spinlock_t timeout_lock; |
413 | ||
def596e9 | 414 | /* |
540e32a0 | 415 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
416 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
417 | * For SQPOLL, only the single threaded io_sq_thread() will | |
418 | * manipulate the list, hence no extra locking is needed there. | |
419 | */ | |
540e32a0 | 420 | struct list_head iopoll_list; |
78076bb6 JA |
421 | struct hlist_head *cancel_hash; |
422 | unsigned cancel_hash_bits; | |
915b3dde | 423 | bool poll_multi_queue; |
2b188cc1 | 424 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 425 | |
21b55dbc | 426 | struct io_restriction restrictions; |
3c1a2ead | 427 | |
b13a8918 PB |
428 | /* slow path rsrc auxilary data, used by update/register */ |
429 | struct { | |
430 | struct io_rsrc_node *rsrc_backup_node; | |
431 | struct io_mapped_ubuf *dummy_ubuf; | |
432 | struct io_rsrc_data *file_data; | |
433 | struct io_rsrc_data *buf_data; | |
434 | ||
435 | struct delayed_work rsrc_put_work; | |
436 | struct llist_head rsrc_put_llist; | |
437 | struct list_head rsrc_ref_list; | |
438 | spinlock_t rsrc_ref_lock; | |
439 | }; | |
440 | ||
3c1a2ead | 441 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
442 | struct { |
443 | #if defined(CONFIG_UNIX) | |
444 | struct socket *ring_sock; | |
445 | #endif | |
446 | /* hashed buffered write serialization */ | |
447 | struct io_wq_hash *hash_map; | |
448 | ||
449 | /* Only used for accounting purposes */ | |
450 | struct user_struct *user; | |
451 | struct mm_struct *mm_account; | |
452 | ||
453 | /* ctx exit and cancelation */ | |
9011bf9a PB |
454 | struct llist_head fallback_llist; |
455 | struct delayed_work fallback_work; | |
b986af7e PB |
456 | struct work_struct exit_work; |
457 | struct list_head tctx_list; | |
458 | struct completion ref_comp; | |
459 | }; | |
2b188cc1 JA |
460 | }; |
461 | ||
53e043b2 SM |
462 | struct io_uring_task { |
463 | /* submission side */ | |
09899b19 | 464 | int cached_refs; |
53e043b2 SM |
465 | struct xarray xa; |
466 | struct wait_queue_head wait; | |
ee53fb2b SM |
467 | const struct io_ring_ctx *last; |
468 | struct io_wq *io_wq; | |
53e043b2 | 469 | struct percpu_counter inflight; |
b303fe2e | 470 | atomic_t inflight_tracked; |
53e043b2 | 471 | atomic_t in_idle; |
53e043b2 SM |
472 | |
473 | spinlock_t task_lock; | |
474 | struct io_wq_work_list task_list; | |
53e043b2 | 475 | struct callback_head task_work; |
6294f368 | 476 | bool task_running; |
53e043b2 SM |
477 | }; |
478 | ||
09bb8394 JA |
479 | /* |
480 | * First field must be the file pointer in all the | |
481 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
482 | */ | |
221c5eb2 JA |
483 | struct io_poll_iocb { |
484 | struct file *file; | |
018043be | 485 | struct wait_queue_head *head; |
221c5eb2 | 486 | __poll_t events; |
8c838788 | 487 | bool done; |
221c5eb2 | 488 | bool canceled; |
392edb45 | 489 | struct wait_queue_entry wait; |
221c5eb2 JA |
490 | }; |
491 | ||
9d805892 | 492 | struct io_poll_update { |
018043be | 493 | struct file *file; |
9d805892 PB |
494 | u64 old_user_data; |
495 | u64 new_user_data; | |
496 | __poll_t events; | |
b69de288 JA |
497 | bool update_events; |
498 | bool update_user_data; | |
018043be PB |
499 | }; |
500 | ||
b5dba59e JA |
501 | struct io_close { |
502 | struct file *file; | |
b5dba59e JA |
503 | int fd; |
504 | }; | |
505 | ||
ad8a48ac JA |
506 | struct io_timeout_data { |
507 | struct io_kiocb *req; | |
508 | struct hrtimer timer; | |
509 | struct timespec64 ts; | |
510 | enum hrtimer_mode mode; | |
511 | }; | |
512 | ||
8ed8d3c3 JA |
513 | struct io_accept { |
514 | struct file *file; | |
515 | struct sockaddr __user *addr; | |
516 | int __user *addr_len; | |
517 | int flags; | |
09952e3e | 518 | unsigned long nofile; |
8ed8d3c3 JA |
519 | }; |
520 | ||
521 | struct io_sync { | |
522 | struct file *file; | |
523 | loff_t len; | |
524 | loff_t off; | |
525 | int flags; | |
d63d1b5e | 526 | int mode; |
8ed8d3c3 JA |
527 | }; |
528 | ||
fbf23849 JA |
529 | struct io_cancel { |
530 | struct file *file; | |
531 | u64 addr; | |
532 | }; | |
533 | ||
b29472ee JA |
534 | struct io_timeout { |
535 | struct file *file; | |
bfe68a22 PB |
536 | u32 off; |
537 | u32 target_seq; | |
135fcde8 | 538 | struct list_head list; |
90cd7e42 PB |
539 | /* head of the link, used by linked timeouts only */ |
540 | struct io_kiocb *head; | |
89b263f6 JA |
541 | /* for linked completions */ |
542 | struct io_kiocb *prev; | |
b29472ee JA |
543 | }; |
544 | ||
0bdf7a2d PB |
545 | struct io_timeout_rem { |
546 | struct file *file; | |
547 | u64 addr; | |
9c8e11b3 PB |
548 | |
549 | /* timeout update */ | |
550 | struct timespec64 ts; | |
551 | u32 flags; | |
0bdf7a2d PB |
552 | }; |
553 | ||
9adbd45d JA |
554 | struct io_rw { |
555 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
556 | struct kiocb kiocb; | |
557 | u64 addr; | |
558 | u64 len; | |
559 | }; | |
560 | ||
3fbb51c1 JA |
561 | struct io_connect { |
562 | struct file *file; | |
563 | struct sockaddr __user *addr; | |
564 | int addr_len; | |
565 | }; | |
566 | ||
e47293fd JA |
567 | struct io_sr_msg { |
568 | struct file *file; | |
fddaface | 569 | union { |
4af3417a PB |
570 | struct compat_msghdr __user *umsg_compat; |
571 | struct user_msghdr __user *umsg; | |
572 | void __user *buf; | |
fddaface | 573 | }; |
e47293fd | 574 | int msg_flags; |
bcda7baa | 575 | int bgid; |
fddaface | 576 | size_t len; |
bcda7baa | 577 | struct io_buffer *kbuf; |
e47293fd JA |
578 | }; |
579 | ||
15b71abe JA |
580 | struct io_open { |
581 | struct file *file; | |
582 | int dfd; | |
15b71abe | 583 | struct filename *filename; |
c12cedf2 | 584 | struct open_how how; |
4022e7af | 585 | unsigned long nofile; |
15b71abe JA |
586 | }; |
587 | ||
269bbe5f | 588 | struct io_rsrc_update { |
05f3fb3c JA |
589 | struct file *file; |
590 | u64 arg; | |
591 | u32 nr_args; | |
592 | u32 offset; | |
593 | }; | |
594 | ||
4840e418 JA |
595 | struct io_fadvise { |
596 | struct file *file; | |
597 | u64 offset; | |
598 | u32 len; | |
599 | u32 advice; | |
600 | }; | |
601 | ||
c1ca757b JA |
602 | struct io_madvise { |
603 | struct file *file; | |
604 | u64 addr; | |
605 | u32 len; | |
606 | u32 advice; | |
607 | }; | |
608 | ||
3e4827b0 JA |
609 | struct io_epoll { |
610 | struct file *file; | |
611 | int epfd; | |
612 | int op; | |
613 | int fd; | |
614 | struct epoll_event event; | |
e47293fd JA |
615 | }; |
616 | ||
7d67af2c PB |
617 | struct io_splice { |
618 | struct file *file_out; | |
619 | struct file *file_in; | |
620 | loff_t off_out; | |
621 | loff_t off_in; | |
622 | u64 len; | |
623 | unsigned int flags; | |
624 | }; | |
625 | ||
ddf0322d JA |
626 | struct io_provide_buf { |
627 | struct file *file; | |
628 | __u64 addr; | |
38134ada | 629 | __u32 len; |
ddf0322d JA |
630 | __u32 bgid; |
631 | __u16 nbufs; | |
632 | __u16 bid; | |
633 | }; | |
634 | ||
1d9e1288 BM |
635 | struct io_statx { |
636 | struct file *file; | |
637 | int dfd; | |
638 | unsigned int mask; | |
639 | unsigned int flags; | |
e62753e4 | 640 | const char __user *filename; |
1d9e1288 BM |
641 | struct statx __user *buffer; |
642 | }; | |
643 | ||
36f4fa68 JA |
644 | struct io_shutdown { |
645 | struct file *file; | |
646 | int how; | |
647 | }; | |
648 | ||
80a261fd JA |
649 | struct io_rename { |
650 | struct file *file; | |
651 | int old_dfd; | |
652 | int new_dfd; | |
653 | struct filename *oldpath; | |
654 | struct filename *newpath; | |
655 | int flags; | |
656 | }; | |
657 | ||
14a1143b JA |
658 | struct io_unlink { |
659 | struct file *file; | |
660 | int dfd; | |
661 | int flags; | |
662 | struct filename *filename; | |
663 | }; | |
664 | ||
3ca405eb PB |
665 | struct io_completion { |
666 | struct file *file; | |
8c3f9cd1 | 667 | u32 cflags; |
3ca405eb PB |
668 | }; |
669 | ||
f499a021 JA |
670 | struct io_async_connect { |
671 | struct sockaddr_storage address; | |
672 | }; | |
673 | ||
03b1230c JA |
674 | struct io_async_msghdr { |
675 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
676 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
677 | struct iovec *free_iov; | |
03b1230c JA |
678 | struct sockaddr __user *uaddr; |
679 | struct msghdr msg; | |
b537916c | 680 | struct sockaddr_storage addr; |
03b1230c JA |
681 | }; |
682 | ||
f67676d1 JA |
683 | struct io_async_rw { |
684 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
685 | const struct iovec *free_iovec; |
686 | struct iov_iter iter; | |
227c0c96 | 687 | size_t bytes_done; |
bcf5a063 | 688 | struct wait_page_queue wpq; |
f67676d1 JA |
689 | }; |
690 | ||
6b47ee6e PB |
691 | enum { |
692 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
693 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
694 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
695 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
696 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 697 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 698 | |
dddca226 | 699 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 700 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
701 | REQ_F_INFLIGHT_BIT, |
702 | REQ_F_CUR_POS_BIT, | |
703 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 704 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 705 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 706 | REQ_F_POLLED_BIT, |
bcda7baa | 707 | REQ_F_BUFFER_SELECTED_BIT, |
900fad45 | 708 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
e342c807 | 709 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 710 | REQ_F_REISSUE_BIT, |
8c130827 | 711 | REQ_F_DONT_REISSUE_BIT, |
b8e64b53 | 712 | REQ_F_CREDS_BIT, |
20e60a38 | 713 | REQ_F_REFCOUNT_BIT, |
7b29f92d | 714 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
715 | REQ_F_NOWAIT_READ_BIT, |
716 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 717 | REQ_F_ISREG_BIT, |
84557871 JA |
718 | |
719 | /* not a real bit, just to check we're not overflowing the space */ | |
720 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
721 | }; |
722 | ||
723 | enum { | |
724 | /* ctx owns file */ | |
725 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
726 | /* drain existing IO first */ | |
727 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
728 | /* linked sqes */ | |
729 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
730 | /* doesn't sever on completion < 0 */ | |
731 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
732 | /* IOSQE_ASYNC */ | |
733 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
734 | /* IOSQE_BUFFER_SELECT */ |
735 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 736 | |
6b47ee6e | 737 | /* fail rest of links */ |
93d2bcd2 | 738 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 739 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
740 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
741 | /* read/write uses file position */ | |
742 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
743 | /* must not punt to workers */ | |
744 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 745 | /* has or had linked timeout */ |
6b47ee6e | 746 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
747 | /* needs cleanup */ |
748 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
749 | /* already went through poll handler */ |
750 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
751 | /* buffer already selected */ |
752 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
900fad45 PB |
753 | /* linked timeout is active, i.e. prepared by link's head */ |
754 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), | |
e342c807 PB |
755 | /* completion is deferred through io_comp_state */ |
756 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
757 | /* caller should reissue async */ |
758 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
759 | /* don't attempt request reissue, see io_rw_reissue() */ |
760 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
7b29f92d | 761 | /* supports async reads */ |
b191e2df | 762 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 763 | /* supports async writes */ |
b191e2df | 764 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
765 | /* regular file */ |
766 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
767 | /* has creds assigned */ |
768 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
769 | /* skip refcounting if not set */ |
770 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
d7718a9d JA |
771 | }; |
772 | ||
773 | struct async_poll { | |
774 | struct io_poll_iocb poll; | |
807abcb0 | 775 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
776 | }; |
777 | ||
5b0a6acc PB |
778 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req); |
779 | ||
7cbf1722 | 780 | struct io_task_work { |
5b0a6acc PB |
781 | union { |
782 | struct io_wq_work_node node; | |
783 | struct llist_node fallback_node; | |
784 | }; | |
785 | io_req_tw_func_t func; | |
7cbf1722 JA |
786 | }; |
787 | ||
992da01a PB |
788 | enum { |
789 | IORING_RSRC_FILE = 0, | |
790 | IORING_RSRC_BUFFER = 1, | |
791 | }; | |
792 | ||
09bb8394 JA |
793 | /* |
794 | * NOTE! Each of the iocb union members has the file pointer | |
795 | * as the first entry in their struct definition. So you can | |
796 | * access the file pointer through any of the sub-structs, | |
797 | * or directly as just 'ki_filp' in this struct. | |
798 | */ | |
2b188cc1 | 799 | struct io_kiocb { |
221c5eb2 | 800 | union { |
09bb8394 | 801 | struct file *file; |
9adbd45d | 802 | struct io_rw rw; |
221c5eb2 | 803 | struct io_poll_iocb poll; |
9d805892 | 804 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
805 | struct io_accept accept; |
806 | struct io_sync sync; | |
fbf23849 | 807 | struct io_cancel cancel; |
b29472ee | 808 | struct io_timeout timeout; |
0bdf7a2d | 809 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 810 | struct io_connect connect; |
e47293fd | 811 | struct io_sr_msg sr_msg; |
15b71abe | 812 | struct io_open open; |
b5dba59e | 813 | struct io_close close; |
269bbe5f | 814 | struct io_rsrc_update rsrc_update; |
4840e418 | 815 | struct io_fadvise fadvise; |
c1ca757b | 816 | struct io_madvise madvise; |
3e4827b0 | 817 | struct io_epoll epoll; |
7d67af2c | 818 | struct io_splice splice; |
ddf0322d | 819 | struct io_provide_buf pbuf; |
1d9e1288 | 820 | struct io_statx statx; |
36f4fa68 | 821 | struct io_shutdown shutdown; |
80a261fd | 822 | struct io_rename rename; |
14a1143b | 823 | struct io_unlink unlink; |
3ca405eb PB |
824 | /* use only after cleaning per-op data, see io_clean_op() */ |
825 | struct io_completion compl; | |
221c5eb2 | 826 | }; |
2b188cc1 | 827 | |
e8c2bc1f JA |
828 | /* opcode allocated if it needs to store data for async defer */ |
829 | void *async_data; | |
d625c6ee | 830 | u8 opcode; |
65a6543d XW |
831 | /* polled IO has completed */ |
832 | u8 iopoll_completed; | |
2b188cc1 | 833 | |
4f4eeba8 | 834 | u16 buf_index; |
9cf7c104 | 835 | u32 result; |
4f4eeba8 | 836 | |
010e8e6b PB |
837 | struct io_ring_ctx *ctx; |
838 | unsigned int flags; | |
abc54d63 | 839 | atomic_t refs; |
010e8e6b PB |
840 | struct task_struct *task; |
841 | u64 user_data; | |
d7718a9d | 842 | |
f2f87370 | 843 | struct io_kiocb *link; |
269bbe5f | 844 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 845 | |
b303fe2e | 846 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 847 | struct list_head inflight_entry; |
5b0a6acc | 848 | struct io_task_work io_task_work; |
010e8e6b PB |
849 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
850 | struct hlist_node hash_node; | |
851 | struct async_poll *apoll; | |
852 | struct io_wq_work work; | |
fe7e3257 | 853 | const struct cred *creds; |
c10d1f98 | 854 | |
eae071c9 PB |
855 | /* store used ubuf, so we can prevent reloading */ |
856 | struct io_mapped_ubuf *imu; | |
2b188cc1 | 857 | }; |
05589553 | 858 | |
13bf43f5 PB |
859 | struct io_tctx_node { |
860 | struct list_head ctx_node; | |
861 | struct task_struct *task; | |
13bf43f5 PB |
862 | struct io_ring_ctx *ctx; |
863 | }; | |
864 | ||
27dc8338 PB |
865 | struct io_defer_entry { |
866 | struct list_head list; | |
867 | struct io_kiocb *req; | |
9cf7c104 | 868 | u32 seq; |
2b188cc1 JA |
869 | }; |
870 | ||
d3656344 | 871 | struct io_op_def { |
d3656344 JA |
872 | /* needs req->file assigned */ |
873 | unsigned needs_file : 1; | |
d3656344 JA |
874 | /* hash wq insertion if file is a regular file */ |
875 | unsigned hash_reg_file : 1; | |
876 | /* unbound wq insertion if file is a non-regular file */ | |
877 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
878 | /* opcode is not supported by this kernel */ |
879 | unsigned not_supported : 1; | |
8a72758c JA |
880 | /* set if opcode supports polled "wait" */ |
881 | unsigned pollin : 1; | |
882 | unsigned pollout : 1; | |
bcda7baa JA |
883 | /* op supports buffer selection */ |
884 | unsigned buffer_select : 1; | |
26f0505a PB |
885 | /* do prep async if is going to be punted */ |
886 | unsigned needs_async_setup : 1; | |
27926b68 JA |
887 | /* should block plug */ |
888 | unsigned plug : 1; | |
e8c2bc1f JA |
889 | /* size of async data needed, if any */ |
890 | unsigned short async_size; | |
d3656344 JA |
891 | }; |
892 | ||
0918682b | 893 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
894 | [IORING_OP_NOP] = {}, |
895 | [IORING_OP_READV] = { | |
d3656344 JA |
896 | .needs_file = 1, |
897 | .unbound_nonreg_file = 1, | |
8a72758c | 898 | .pollin = 1, |
4d954c25 | 899 | .buffer_select = 1, |
26f0505a | 900 | .needs_async_setup = 1, |
27926b68 | 901 | .plug = 1, |
e8c2bc1f | 902 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 903 | }, |
0463b6c5 | 904 | [IORING_OP_WRITEV] = { |
d3656344 JA |
905 | .needs_file = 1, |
906 | .hash_reg_file = 1, | |
907 | .unbound_nonreg_file = 1, | |
8a72758c | 908 | .pollout = 1, |
26f0505a | 909 | .needs_async_setup = 1, |
27926b68 | 910 | .plug = 1, |
e8c2bc1f | 911 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 912 | }, |
0463b6c5 | 913 | [IORING_OP_FSYNC] = { |
d3656344 JA |
914 | .needs_file = 1, |
915 | }, | |
0463b6c5 | 916 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
917 | .needs_file = 1, |
918 | .unbound_nonreg_file = 1, | |
8a72758c | 919 | .pollin = 1, |
27926b68 | 920 | .plug = 1, |
e8c2bc1f | 921 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 922 | }, |
0463b6c5 | 923 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
924 | .needs_file = 1, |
925 | .hash_reg_file = 1, | |
926 | .unbound_nonreg_file = 1, | |
8a72758c | 927 | .pollout = 1, |
27926b68 | 928 | .plug = 1, |
e8c2bc1f | 929 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 930 | }, |
0463b6c5 | 931 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
932 | .needs_file = 1, |
933 | .unbound_nonreg_file = 1, | |
934 | }, | |
0463b6c5 PB |
935 | [IORING_OP_POLL_REMOVE] = {}, |
936 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
937 | .needs_file = 1, |
938 | }, | |
0463b6c5 | 939 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
940 | .needs_file = 1, |
941 | .unbound_nonreg_file = 1, | |
8a72758c | 942 | .pollout = 1, |
26f0505a | 943 | .needs_async_setup = 1, |
e8c2bc1f | 944 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 945 | }, |
0463b6c5 | 946 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
947 | .needs_file = 1, |
948 | .unbound_nonreg_file = 1, | |
8a72758c | 949 | .pollin = 1, |
52de1fe1 | 950 | .buffer_select = 1, |
26f0505a | 951 | .needs_async_setup = 1, |
e8c2bc1f | 952 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 953 | }, |
0463b6c5 | 954 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 955 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 956 | }, |
9c8e11b3 PB |
957 | [IORING_OP_TIMEOUT_REMOVE] = { |
958 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 959 | }, |
0463b6c5 | 960 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
961 | .needs_file = 1, |
962 | .unbound_nonreg_file = 1, | |
8a72758c | 963 | .pollin = 1, |
d3656344 | 964 | }, |
0463b6c5 PB |
965 | [IORING_OP_ASYNC_CANCEL] = {}, |
966 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 967 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 968 | }, |
0463b6c5 | 969 | [IORING_OP_CONNECT] = { |
d3656344 JA |
970 | .needs_file = 1, |
971 | .unbound_nonreg_file = 1, | |
8a72758c | 972 | .pollout = 1, |
26f0505a | 973 | .needs_async_setup = 1, |
e8c2bc1f | 974 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 975 | }, |
0463b6c5 | 976 | [IORING_OP_FALLOCATE] = { |
d3656344 | 977 | .needs_file = 1, |
d3656344 | 978 | }, |
44526bed JA |
979 | [IORING_OP_OPENAT] = {}, |
980 | [IORING_OP_CLOSE] = {}, | |
981 | [IORING_OP_FILES_UPDATE] = {}, | |
982 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 983 | [IORING_OP_READ] = { |
3a6820f2 JA |
984 | .needs_file = 1, |
985 | .unbound_nonreg_file = 1, | |
8a72758c | 986 | .pollin = 1, |
bcda7baa | 987 | .buffer_select = 1, |
27926b68 | 988 | .plug = 1, |
e8c2bc1f | 989 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 990 | }, |
0463b6c5 | 991 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
992 | .needs_file = 1, |
993 | .unbound_nonreg_file = 1, | |
8a72758c | 994 | .pollout = 1, |
27926b68 | 995 | .plug = 1, |
e8c2bc1f | 996 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 997 | }, |
0463b6c5 | 998 | [IORING_OP_FADVISE] = { |
4840e418 | 999 | .needs_file = 1, |
c1ca757b | 1000 | }, |
44526bed | 1001 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1002 | [IORING_OP_SEND] = { |
fddaface JA |
1003 | .needs_file = 1, |
1004 | .unbound_nonreg_file = 1, | |
8a72758c | 1005 | .pollout = 1, |
fddaface | 1006 | }, |
0463b6c5 | 1007 | [IORING_OP_RECV] = { |
fddaface JA |
1008 | .needs_file = 1, |
1009 | .unbound_nonreg_file = 1, | |
8a72758c | 1010 | .pollin = 1, |
bcda7baa | 1011 | .buffer_select = 1, |
fddaface | 1012 | }, |
0463b6c5 | 1013 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1014 | }, |
3e4827b0 JA |
1015 | [IORING_OP_EPOLL_CTL] = { |
1016 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1017 | }, |
7d67af2c PB |
1018 | [IORING_OP_SPLICE] = { |
1019 | .needs_file = 1, | |
1020 | .hash_reg_file = 1, | |
1021 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1022 | }, |
1023 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1024 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1025 | [IORING_OP_TEE] = { |
1026 | .needs_file = 1, | |
1027 | .hash_reg_file = 1, | |
1028 | .unbound_nonreg_file = 1, | |
1029 | }, | |
36f4fa68 JA |
1030 | [IORING_OP_SHUTDOWN] = { |
1031 | .needs_file = 1, | |
1032 | }, | |
44526bed JA |
1033 | [IORING_OP_RENAMEAT] = {}, |
1034 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
1035 | }; |
1036 | ||
7a612350 | 1037 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1038 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1039 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1040 | struct task_struct *task, | |
3dd0c97a | 1041 | bool cancel_all); |
78cc687b | 1042 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1043 | |
d4d19c19 PB |
1044 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1045 | long res, unsigned int cflags); | |
ec9c02ad | 1046 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1047 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1048 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1049 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1050 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1051 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1052 | unsigned nr_args); |
68fb8979 | 1053 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1054 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1055 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1056 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1057 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1058 | |
907d1df3 | 1059 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1060 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1061 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1062 | |
2b188cc1 JA |
1063 | static struct kmem_cache *req_cachep; |
1064 | ||
0918682b | 1065 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1066 | |
1067 | struct sock *io_uring_get_socket(struct file *file) | |
1068 | { | |
1069 | #if defined(CONFIG_UNIX) | |
1070 | if (file->f_op == &io_uring_fops) { | |
1071 | struct io_ring_ctx *ctx = file->private_data; | |
1072 | ||
1073 | return ctx->ring_sock->sk; | |
1074 | } | |
1075 | #endif | |
1076 | return NULL; | |
1077 | } | |
1078 | EXPORT_SYMBOL(io_uring_get_socket); | |
1079 | ||
f2f87370 PB |
1080 | #define io_for_each_link(pos, head) \ |
1081 | for (pos = (head); pos; pos = pos->link) | |
1082 | ||
21c843d5 PB |
1083 | /* |
1084 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1085 | * see commit f958d7b528b1 for details. | |
1086 | */ | |
1087 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1088 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1089 | ||
1090 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1091 | { | |
20e60a38 | 1092 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1093 | return atomic_inc_not_zero(&req->refs); |
1094 | } | |
1095 | ||
21c843d5 PB |
1096 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1097 | { | |
20e60a38 PB |
1098 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1099 | return true; | |
1100 | ||
21c843d5 PB |
1101 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1102 | return atomic_dec_and_test(&req->refs); | |
1103 | } | |
1104 | ||
1105 | static inline void req_ref_put(struct io_kiocb *req) | |
1106 | { | |
20e60a38 | 1107 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1108 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1109 | } | |
1110 | ||
1111 | static inline void req_ref_get(struct io_kiocb *req) | |
1112 | { | |
20e60a38 | 1113 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1114 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1115 | atomic_inc(&req->refs); | |
1116 | } | |
1117 | ||
48dcd38d | 1118 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1119 | { |
1120 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1121 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1122 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1123 | } |
1124 | } | |
1125 | ||
48dcd38d PB |
1126 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1127 | { | |
1128 | __io_req_set_refcount(req, 1); | |
1129 | } | |
1130 | ||
b895c9a6 | 1131 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1132 | { |
1133 | struct io_ring_ctx *ctx = req->ctx; | |
1134 | ||
269bbe5f | 1135 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1136 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1137 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1138 | } |
1139 | } | |
1140 | ||
f70865db PB |
1141 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1142 | { | |
1143 | bool got = percpu_ref_tryget(ref); | |
1144 | ||
1145 | /* already at zero, wait for ->release() */ | |
1146 | if (!got) | |
1147 | wait_for_completion(compl); | |
1148 | percpu_ref_resurrect(ref); | |
1149 | if (got) | |
1150 | percpu_ref_put(ref); | |
1151 | } | |
1152 | ||
3dd0c97a PB |
1153 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1154 | bool cancel_all) | |
08d23634 PB |
1155 | { |
1156 | struct io_kiocb *req; | |
1157 | ||
68207680 | 1158 | if (task && head->task != task) |
08d23634 | 1159 | return false; |
3dd0c97a | 1160 | if (cancel_all) |
08d23634 PB |
1161 | return true; |
1162 | ||
1163 | io_for_each_link(req, head) { | |
b05a1bcd | 1164 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1165 | return true; |
08d23634 PB |
1166 | } |
1167 | return false; | |
1168 | } | |
1169 | ||
93d2bcd2 | 1170 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1171 | { |
93d2bcd2 | 1172 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1173 | } |
4a38aed2 | 1174 | |
2b188cc1 JA |
1175 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1176 | { | |
1177 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1178 | ||
0f158b4c | 1179 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1180 | } |
1181 | ||
8eb7e2d0 PB |
1182 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1183 | { | |
1184 | return !req->timeout.off; | |
1185 | } | |
1186 | ||
f56165e6 PB |
1187 | static void io_fallback_req_func(struct work_struct *work) |
1188 | { | |
1189 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1190 | fallback_work.work); | |
1191 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1192 | struct io_kiocb *req, *tmp; | |
1193 | ||
1194 | percpu_ref_get(&ctx->refs); | |
1195 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
1196 | req->io_task_work.func(req); | |
1197 | percpu_ref_put(&ctx->refs); | |
1198 | } | |
1199 | ||
2b188cc1 JA |
1200 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1201 | { | |
1202 | struct io_ring_ctx *ctx; | |
78076bb6 | 1203 | int hash_bits; |
2b188cc1 JA |
1204 | |
1205 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1206 | if (!ctx) | |
1207 | return NULL; | |
1208 | ||
78076bb6 JA |
1209 | /* |
1210 | * Use 5 bits less than the max cq entries, that should give us around | |
1211 | * 32 entries per hash list if totally full and uniformly spread. | |
1212 | */ | |
1213 | hash_bits = ilog2(p->cq_entries); | |
1214 | hash_bits -= 5; | |
1215 | if (hash_bits <= 0) | |
1216 | hash_bits = 1; | |
1217 | ctx->cancel_hash_bits = hash_bits; | |
1218 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1219 | GFP_KERNEL); | |
1220 | if (!ctx->cancel_hash) | |
1221 | goto err; | |
1222 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1223 | ||
6224843d PB |
1224 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1225 | if (!ctx->dummy_ubuf) | |
1226 | goto err; | |
1227 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1228 | ctx->dummy_ubuf->ubuf = -1UL; | |
1229 | ||
21482896 | 1230 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1231 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1232 | goto err; | |
2b188cc1 JA |
1233 | |
1234 | ctx->flags = p->flags; | |
90554200 | 1235 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1236 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1237 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1238 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1239 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1240 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1241 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1242 | mutex_init(&ctx->uring_lock); |
311997b3 | 1243 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1244 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1245 | spin_lock_init(&ctx->timeout_lock); |
540e32a0 | 1246 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1247 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1248 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1249 | spin_lock_init(&ctx->rsrc_ref_lock); |
1250 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1251 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1252 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1253 | INIT_LIST_HEAD(&ctx->tctx_list); |
cd0ca2e0 | 1254 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
d0acdee2 | 1255 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1256 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1257 | return ctx; |
206aefde | 1258 | err: |
6224843d | 1259 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1260 | kfree(ctx->cancel_hash); |
206aefde JA |
1261 | kfree(ctx); |
1262 | return NULL; | |
2b188cc1 JA |
1263 | } |
1264 | ||
8f6ed49a PB |
1265 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1266 | { | |
1267 | struct io_rings *r = ctx->rings; | |
1268 | ||
1269 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1270 | ctx->cq_extra--; | |
1271 | } | |
1272 | ||
9cf7c104 | 1273 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1274 | { |
2bc9930e JA |
1275 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1276 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1277 | |
8f6ed49a | 1278 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1279 | } |
de0617e4 | 1280 | |
9d858b21 | 1281 | return false; |
de0617e4 JA |
1282 | } |
1283 | ||
c97d8a0f PB |
1284 | #define FFS_ASYNC_READ 0x1UL |
1285 | #define FFS_ASYNC_WRITE 0x2UL | |
1286 | #ifdef CONFIG_64BIT | |
1287 | #define FFS_ISREG 0x4UL | |
1288 | #else | |
1289 | #define FFS_ISREG 0x0UL | |
1290 | #endif | |
1291 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1292 | ||
1293 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1294 | { | |
1295 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1296 | } | |
1297 | ||
ce3d5aae PB |
1298 | static void io_req_track_inflight(struct io_kiocb *req) |
1299 | { | |
ce3d5aae | 1300 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1301 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1302 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1303 | } |
1304 | } | |
1305 | ||
fd08e530 PB |
1306 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1307 | { | |
1308 | struct io_kiocb *nxt = req->link; | |
1309 | ||
1310 | if (req->flags & REQ_F_LINK_TIMEOUT) | |
1311 | return NULL; | |
1312 | ||
1313 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1314 | io_req_set_refcount(req); |
fb682099 | 1315 | __io_req_set_refcount(nxt, 2); |
fd08e530 PB |
1316 | |
1317 | nxt->timeout.head = req; | |
1318 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; | |
1319 | req->flags |= REQ_F_LINK_TIMEOUT; | |
1320 | return nxt; | |
1321 | } | |
1322 | ||
1323 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1324 | { | |
1325 | if (likely(!req->link || req->link->opcode != IORING_OP_LINK_TIMEOUT)) | |
1326 | return NULL; | |
1327 | return __io_prep_linked_timeout(req); | |
1328 | } | |
1329 | ||
1e6fa521 JA |
1330 | static void io_prep_async_work(struct io_kiocb *req) |
1331 | { | |
1332 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1333 | struct io_ring_ctx *ctx = req->ctx; |
1334 | ||
b8e64b53 PB |
1335 | if (!(req->flags & REQ_F_CREDS)) { |
1336 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1337 | req->creds = get_current_cred(); |
b8e64b53 | 1338 | } |
003e8dcc | 1339 | |
e1d675df PB |
1340 | req->work.list.next = NULL; |
1341 | req->work.flags = 0; | |
feaadc4f PB |
1342 | if (req->flags & REQ_F_FORCE_ASYNC) |
1343 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1344 | ||
1e6fa521 JA |
1345 | if (req->flags & REQ_F_ISREG) { |
1346 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1347 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1348 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1349 | if (def->unbound_nonreg_file) |
1350 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1351 | } | |
e1d675df PB |
1352 | |
1353 | switch (req->opcode) { | |
1354 | case IORING_OP_SPLICE: | |
1355 | case IORING_OP_TEE: | |
e1d675df PB |
1356 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1357 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1358 | break; | |
1359 | } | |
561fb04a | 1360 | } |
cccf0ee8 | 1361 | |
cbdcb435 | 1362 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1363 | { |
cbdcb435 | 1364 | struct io_kiocb *cur; |
54a91f3b | 1365 | |
44eff40a PB |
1366 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1367 | struct io_ring_ctx *ctx = req->ctx; | |
1368 | ||
79ebeaee | 1369 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1370 | io_for_each_link(cur, req) |
1371 | io_prep_async_work(cur); | |
79ebeaee | 1372 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1373 | } else { |
1374 | io_for_each_link(cur, req) | |
1375 | io_prep_async_work(cur); | |
1376 | } | |
561fb04a JA |
1377 | } |
1378 | ||
ebf93667 | 1379 | static void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 1380 | { |
a197f664 | 1381 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1382 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1383 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1384 | |
3bfe6106 JA |
1385 | BUG_ON(!tctx); |
1386 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1387 | |
cbdcb435 PB |
1388 | /* init ->work of the whole link before punting */ |
1389 | io_prep_async_link(req); | |
991468dc JA |
1390 | |
1391 | /* | |
1392 | * Not expected to happen, but if we do have a bug where this _can_ | |
1393 | * happen, catch it here and ensure the request is marked as | |
1394 | * canceled. That will make io-wq go through the usual work cancel | |
1395 | * procedure rather than attempt to run this request (or create a new | |
1396 | * worker for it). | |
1397 | */ | |
1398 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1399 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1400 | ||
d07f1e8a PB |
1401 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1402 | &req->work, req->flags); | |
ebf93667 | 1403 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1404 | if (link) |
1405 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1406 | } |
1407 | ||
1ee4160c | 1408 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1409 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1410 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1411 | { |
e8c2bc1f | 1412 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1413 | |
fd9c7bc5 | 1414 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
01cec8c1 PB |
1415 | atomic_set(&req->ctx->cq_timeouts, |
1416 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1417 | list_del_init(&req->timeout.list); |
d4d19c19 | 1418 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1419 | io_put_req_deferred(req); |
5262f567 JA |
1420 | } |
1421 | } | |
1422 | ||
441b8a78 | 1423 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1424 | { |
441b8a78 | 1425 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1426 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1427 | struct io_defer_entry, list); | |
de0617e4 | 1428 | |
9cf7c104 | 1429 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1430 | break; |
27dc8338 | 1431 | list_del_init(&de->list); |
907d1df3 | 1432 | io_req_task_queue(de->req); |
27dc8338 | 1433 | kfree(de); |
441b8a78 | 1434 | } |
04518945 PB |
1435 | } |
1436 | ||
360428f8 | 1437 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1438 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1439 | { |
441b8a78 | 1440 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1441 | |
79ebeaee | 1442 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1443 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1444 | u32 events_needed, events_got; |
360428f8 | 1445 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1446 | struct io_kiocb, timeout.list); |
de0617e4 | 1447 | |
8eb7e2d0 | 1448 | if (io_is_timeout_noseq(req)) |
360428f8 | 1449 | break; |
f010505b MDG |
1450 | |
1451 | /* | |
1452 | * Since seq can easily wrap around over time, subtract | |
1453 | * the last seq at which timeouts were flushed before comparing. | |
1454 | * Assuming not more than 2^31-1 events have happened since, | |
1455 | * these subtractions won't have wrapped, so we can check if | |
1456 | * target is in [last_seq, current_seq] by comparing the two. | |
1457 | */ | |
1458 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1459 | events_got = seq - ctx->cq_last_tm_flush; | |
1460 | if (events_got < events_needed) | |
360428f8 | 1461 | break; |
bfe68a22 | 1462 | |
135fcde8 | 1463 | list_del_init(&req->timeout.list); |
1ee4160c | 1464 | io_kill_timeout(req, 0); |
f18ee4cf | 1465 | } |
f010505b | 1466 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1467 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1468 | } |
5262f567 | 1469 | |
2335f6f5 | 1470 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1471 | { |
2335f6f5 PB |
1472 | if (ctx->off_timeout_used) |
1473 | io_flush_timeouts(ctx); | |
1474 | if (ctx->drain_active) | |
1475 | io_queue_deferred(ctx); | |
1476 | } | |
1477 | ||
1478 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1479 | { | |
1480 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1481 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1482 | /* order cqe stores with ring update */ |
1483 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1484 | } |
1485 | ||
90554200 JA |
1486 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1487 | { | |
1488 | struct io_rings *r = ctx->rings; | |
1489 | ||
a566c556 | 1490 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1491 | } |
1492 | ||
888aae2e PB |
1493 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1494 | { | |
1495 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1496 | } | |
1497 | ||
d068b506 | 1498 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1499 | { |
75b28aff | 1500 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1501 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1502 | |
115e12e5 SB |
1503 | /* |
1504 | * writes to the cq entry need to come after reading head; the | |
1505 | * control dependency is enough as we're using WRITE_ONCE to | |
1506 | * fill the cq entry | |
1507 | */ | |
a566c556 | 1508 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1509 | return NULL; |
1510 | ||
888aae2e | 1511 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1512 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1513 | } |
1514 | ||
f2842ab5 JA |
1515 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1516 | { | |
44c769de | 1517 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1518 | return false; |
7e55a19c SG |
1519 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1520 | return false; | |
44c769de | 1521 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1522 | } |
1523 | ||
b41e9852 | 1524 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1525 | { |
5fd46178 JA |
1526 | /* |
1527 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1528 | * io_should_wake() handle the termination of the loop and only | |
1529 | * wake as many waiters as we need to. | |
1530 | */ | |
1531 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1532 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1533 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1534 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1535 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1536 | eventfd_signal(ctx->cq_ev_fd, 1); |
311997b3 PB |
1537 | if (waitqueue_active(&ctx->poll_wait)) { |
1538 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1539 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1540 | } | |
1d7bb1d5 JA |
1541 | } |
1542 | ||
80c18e4a PB |
1543 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1544 | { | |
1545 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
5fd46178 JA |
1546 | if (wq_has_sleeper(&ctx->cq_wait)) |
1547 | wake_up_all(&ctx->cq_wait); | |
80c18e4a PB |
1548 | } |
1549 | if (io_should_trigger_evfd(ctx)) | |
1550 | eventfd_signal(ctx->cq_ev_fd, 1); | |
311997b3 PB |
1551 | if (waitqueue_active(&ctx->poll_wait)) { |
1552 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1553 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1554 | } | |
80c18e4a PB |
1555 | } |
1556 | ||
c4a2ed72 | 1557 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1558 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1559 | { |
b18032bb | 1560 | bool all_flushed, posted; |
1d7bb1d5 | 1561 | |
a566c556 | 1562 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1563 | return false; |
1d7bb1d5 | 1564 | |
b18032bb | 1565 | posted = false; |
79ebeaee | 1566 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1567 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1568 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1569 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1570 | |
1d7bb1d5 JA |
1571 | if (!cqe && !force) |
1572 | break; | |
6c2450ae PB |
1573 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1574 | struct io_overflow_cqe, list); | |
1575 | if (cqe) | |
1576 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1577 | else | |
8f6ed49a PB |
1578 | io_account_cq_overflow(ctx); |
1579 | ||
b18032bb | 1580 | posted = true; |
6c2450ae PB |
1581 | list_del(&ocqe->list); |
1582 | kfree(ocqe); | |
1d7bb1d5 JA |
1583 | } |
1584 | ||
09e88404 PB |
1585 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1586 | if (all_flushed) { | |
5ed7a37d | 1587 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1588 | WRITE_ONCE(ctx->rings->sq_flags, |
1589 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1590 | } |
46930143 | 1591 | |
b18032bb JA |
1592 | if (posted) |
1593 | io_commit_cqring(ctx); | |
79ebeaee | 1594 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1595 | if (posted) |
1596 | io_cqring_ev_posted(ctx); | |
09e88404 | 1597 | return all_flushed; |
1d7bb1d5 JA |
1598 | } |
1599 | ||
90f67366 | 1600 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1601 | { |
ca0a2651 JA |
1602 | bool ret = true; |
1603 | ||
5ed7a37d | 1604 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1605 | /* iopoll syncs against uring_lock, not completion_lock */ |
1606 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1607 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1608 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1609 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1610 | mutex_unlock(&ctx->uring_lock); | |
1611 | } | |
ca0a2651 JA |
1612 | |
1613 | return ret; | |
6c503150 PB |
1614 | } |
1615 | ||
6a290a14 PB |
1616 | /* must to be called somewhat shortly after putting a request */ |
1617 | static inline void io_put_task(struct task_struct *task, int nr) | |
1618 | { | |
1619 | struct io_uring_task *tctx = task->io_uring; | |
1620 | ||
1621 | percpu_counter_sub(&tctx->inflight, nr); | |
1622 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1623 | wake_up(&tctx->wait); | |
1624 | put_task_struct_many(task, nr); | |
1625 | } | |
1626 | ||
d4d19c19 PB |
1627 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1628 | long res, unsigned int cflags) | |
2b188cc1 | 1629 | { |
cce4b8b0 | 1630 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1631 | |
cce4b8b0 PB |
1632 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1633 | if (!ocqe) { | |
1634 | /* | |
1635 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1636 | * or cannot allocate an overflow entry, then we need to drop it | |
1637 | * on the floor. | |
1638 | */ | |
8f6ed49a | 1639 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1640 | return false; |
2b188cc1 | 1641 | } |
cce4b8b0 | 1642 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1643 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1644 | WRITE_ONCE(ctx->rings->sq_flags, |
1645 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1646 | ||
cce4b8b0 | 1647 | } |
d4d19c19 | 1648 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1649 | ocqe->cqe.res = res; |
1650 | ocqe->cqe.flags = cflags; | |
1651 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1652 | return true; | |
2b188cc1 JA |
1653 | } |
1654 | ||
d4d19c19 PB |
1655 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1656 | long res, unsigned int cflags) | |
2b188cc1 JA |
1657 | { |
1658 | struct io_uring_cqe *cqe; | |
1659 | ||
d4d19c19 | 1660 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1661 | |
2b188cc1 JA |
1662 | /* |
1663 | * If we can't get a cq entry, userspace overflowed the | |
1664 | * submission (by quite a lot). Increment the overflow count in | |
1665 | * the ring. | |
1666 | */ | |
d068b506 | 1667 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1668 | if (likely(cqe)) { |
d4d19c19 | 1669 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1670 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1671 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1672 | return true; |
2b188cc1 | 1673 | } |
d4d19c19 | 1674 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1675 | } |
1676 | ||
8d13326e | 1677 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1678 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1679 | long res, unsigned int cflags) | |
bcda7baa | 1680 | { |
d4d19c19 | 1681 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1682 | } |
1683 | ||
7a612350 PB |
1684 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1685 | unsigned int cflags) | |
2b188cc1 | 1686 | { |
78e19bbe | 1687 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1688 | |
79ebeaee | 1689 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1690 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1691 | /* |
1692 | * If we're the last reference to this request, add to our locked | |
1693 | * free_list cache. | |
1694 | */ | |
de9b4cca | 1695 | if (req_ref_put_and_test(req)) { |
7a612350 | 1696 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
93d2bcd2 | 1697 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) |
7a612350 PB |
1698 | io_disarm_next(req); |
1699 | if (req->link) { | |
1700 | io_req_task_queue(req->link); | |
1701 | req->link = NULL; | |
1702 | } | |
1703 | } | |
c7dae4ba JA |
1704 | io_dismantle_req(req); |
1705 | io_put_task(req->task, 1); | |
bb943b82 | 1706 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
d0acdee2 | 1707 | ctx->locked_free_nr++; |
180f829f PB |
1708 | } else { |
1709 | if (!percpu_ref_tryget(&ctx->refs)) | |
1710 | req = NULL; | |
1711 | } | |
7a612350 | 1712 | io_commit_cqring(ctx); |
79ebeaee | 1713 | spin_unlock(&ctx->completion_lock); |
7a612350 | 1714 | |
180f829f PB |
1715 | if (req) { |
1716 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1717 | percpu_ref_put(&ctx->refs); |
180f829f | 1718 | } |
229a7b63 JA |
1719 | } |
1720 | ||
4e3d9ff9 JA |
1721 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1722 | { | |
c854357b | 1723 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1724 | } |
1725 | ||
a38d68db | 1726 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1727 | unsigned int cflags) |
229a7b63 | 1728 | { |
4e3d9ff9 | 1729 | if (io_req_needs_clean(req)) |
68fb8979 | 1730 | io_clean_op(req); |
a38d68db PB |
1731 | req->result = res; |
1732 | req->compl.cflags = cflags; | |
e342c807 | 1733 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1734 | } |
1735 | ||
889fca73 PB |
1736 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1737 | long res, unsigned cflags) | |
bcda7baa | 1738 | { |
889fca73 PB |
1739 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1740 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1741 | else |
c7dae4ba | 1742 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1743 | } |
1744 | ||
a38d68db | 1745 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1746 | { |
889fca73 | 1747 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1748 | } |
1749 | ||
f41db273 PB |
1750 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1751 | { | |
93d2bcd2 | 1752 | req_set_fail(req); |
f41db273 PB |
1753 | io_req_complete_post(req, res, 0); |
1754 | } | |
1755 | ||
864ea921 PB |
1756 | /* |
1757 | * Don't initialise the fields below on every allocation, but do that in | |
1758 | * advance and keep them valid across allocations. | |
1759 | */ | |
1760 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1761 | { | |
1762 | req->ctx = ctx; | |
1763 | req->link = NULL; | |
1764 | req->async_data = NULL; | |
1765 | /* not necessary, but safer to zero */ | |
1766 | req->result = 0; | |
1767 | } | |
1768 | ||
dac7a098 | 1769 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1770 | struct io_submit_state *state) |
dac7a098 | 1771 | { |
79ebeaee | 1772 | spin_lock(&ctx->completion_lock); |
cd0ca2e0 | 1773 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1774 | ctx->locked_free_nr = 0; |
79ebeaee | 1775 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1776 | } |
1777 | ||
dd78f492 | 1778 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1779 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1780 | { |
c7dae4ba | 1781 | struct io_submit_state *state = &ctx->submit_state; |
dd78f492 | 1782 | int nr; |
0ddf92e8 | 1783 | |
c7dae4ba JA |
1784 | /* |
1785 | * If we have more than a batch's worth of requests in our IRQ side | |
1786 | * locked cache, grab the lock and move them over to our submission | |
1787 | * side cache. | |
1788 | */ | |
d0acdee2 | 1789 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1790 | io_flush_cached_locked_reqs(ctx, state); |
0ddf92e8 | 1791 | |
dd78f492 | 1792 | nr = state->free_reqs; |
cd0ca2e0 PB |
1793 | while (!list_empty(&state->free_list)) { |
1794 | struct io_kiocb *req = list_first_entry(&state->free_list, | |
bb943b82 | 1795 | struct io_kiocb, inflight_entry); |
dd78f492 | 1796 | |
bb943b82 | 1797 | list_del(&req->inflight_entry); |
dd78f492 PB |
1798 | state->reqs[nr++] = req; |
1799 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1800 | break; |
1b4c351f JA |
1801 | } |
1802 | ||
dd78f492 PB |
1803 | state->free_reqs = nr; |
1804 | return nr != 0; | |
0ddf92e8 JA |
1805 | } |
1806 | ||
5d5901a3 PB |
1807 | /* |
1808 | * A request might get retired back into the request caches even before opcode | |
1809 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1810 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1811 | * and with extra caution to not get a request that is still worked on. | |
1812 | */ | |
e5d1bc0a | 1813 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1814 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1815 | { |
e5d1bc0a | 1816 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1817 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1818 | int ret, i; | |
e5d1bc0a | 1819 | |
fe7e3257 | 1820 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1821 | |
864ea921 PB |
1822 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1823 | goto got_req; | |
e5d1bc0a | 1824 | |
864ea921 PB |
1825 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1826 | state->reqs); | |
fd6fab2c | 1827 | |
864ea921 PB |
1828 | /* |
1829 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1830 | * retry single alloc to be on the safe side. | |
1831 | */ | |
1832 | if (unlikely(ret <= 0)) { | |
1833 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1834 | if (!state->reqs[0]) | |
1835 | return NULL; | |
1836 | ret = 1; | |
2b188cc1 | 1837 | } |
864ea921 PB |
1838 | |
1839 | for (i = 0; i < ret; i++) | |
1840 | io_preinit_req(state->reqs[i], ctx); | |
1841 | state->free_reqs = ret; | |
e5d1bc0a | 1842 | got_req: |
291b2821 PB |
1843 | state->free_reqs--; |
1844 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1845 | } |
1846 | ||
e1d767f0 | 1847 | static inline void io_put_file(struct file *file) |
8da11c19 | 1848 | { |
e1d767f0 | 1849 | if (file) |
8da11c19 PB |
1850 | fput(file); |
1851 | } | |
1852 | ||
4edf20f9 | 1853 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1854 | { |
094bae49 | 1855 | unsigned int flags = req->flags; |
929a3af9 | 1856 | |
3a0a6902 PB |
1857 | if (io_req_needs_clean(req)) |
1858 | io_clean_op(req); | |
e1d767f0 PB |
1859 | if (!(flags & REQ_F_FIXED_FILE)) |
1860 | io_put_file(req->file); | |
269bbe5f BM |
1861 | if (req->fixed_rsrc_refs) |
1862 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1863 | if (req->async_data) { |
094bae49 | 1864 | kfree(req->async_data); |
99ebe4ef PB |
1865 | req->async_data = NULL; |
1866 | } | |
e65ef56d JA |
1867 | } |
1868 | ||
216578e5 | 1869 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1870 | { |
51a4cc11 | 1871 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1872 | |
216578e5 | 1873 | io_dismantle_req(req); |
7c660731 | 1874 | io_put_task(req->task, 1); |
c6ca97b3 | 1875 | |
79ebeaee | 1876 | spin_lock(&ctx->completion_lock); |
bb943b82 | 1877 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
c34b025f | 1878 | ctx->locked_free_nr++; |
79ebeaee | 1879 | spin_unlock(&ctx->completion_lock); |
c34b025f | 1880 | |
ecfc5177 | 1881 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1882 | } |
1883 | ||
f2f87370 PB |
1884 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1885 | { | |
1886 | struct io_kiocb *nxt = req->link; | |
1887 | ||
1888 | req->link = nxt->link; | |
1889 | nxt->link = NULL; | |
1890 | } | |
1891 | ||
33cc89a9 PB |
1892 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1893 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 1894 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 1895 | { |
33cc89a9 | 1896 | struct io_kiocb *link = req->link; |
f2f87370 | 1897 | |
900fad45 PB |
1898 | /* |
1899 | * Can happen if a linked timeout fired and link had been like | |
1900 | * req -> link t-out -> link t-out [-> ...] | |
1901 | */ | |
c9abd7ad PB |
1902 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
1903 | struct io_timeout_data *io = link->async_data; | |
7c86ffee | 1904 | |
f2f87370 | 1905 | io_remove_next_linked(req); |
90cd7e42 | 1906 | link->timeout.head = NULL; |
fd9c7bc5 | 1907 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
d4d19c19 PB |
1908 | io_cqring_fill_event(link->ctx, link->user_data, |
1909 | -ECANCELED, 0); | |
91c2f697 | 1910 | io_put_req_deferred(link); |
d4729fbd | 1911 | return true; |
c9abd7ad PB |
1912 | } |
1913 | } | |
d4729fbd | 1914 | return false; |
7c86ffee PB |
1915 | } |
1916 | ||
d148ca4b | 1917 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1918 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1919 | { |
33cc89a9 | 1920 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1921 | |
f2f87370 | 1922 | req->link = NULL; |
f2f87370 PB |
1923 | while (link) { |
1924 | nxt = link->link; | |
1925 | link->link = NULL; | |
2665abfd | 1926 | |
f2f87370 | 1927 | trace_io_uring_fail_link(req, link); |
d4d19c19 | 1928 | io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0); |
91c2f697 | 1929 | io_put_req_deferred(link); |
f2f87370 | 1930 | link = nxt; |
9e645e11 | 1931 | } |
33cc89a9 | 1932 | } |
9e645e11 | 1933 | |
33cc89a9 PB |
1934 | static bool io_disarm_next(struct io_kiocb *req) |
1935 | __must_hold(&req->ctx->completion_lock) | |
1936 | { | |
1937 | bool posted = false; | |
1938 | ||
89b263f6 JA |
1939 | if (likely(req->flags & REQ_F_LINK_TIMEOUT)) { |
1940 | struct io_ring_ctx *ctx = req->ctx; | |
1941 | ||
1942 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 1943 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
1944 | spin_unlock_irq(&ctx->timeout_lock); |
1945 | } | |
93d2bcd2 | 1946 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 1947 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
1948 | posted |= (req->link != NULL); |
1949 | io_fail_links(req); | |
1950 | } | |
1951 | return posted; | |
9e645e11 JA |
1952 | } |
1953 | ||
3fa5e0f3 | 1954 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1955 | { |
33cc89a9 | 1956 | struct io_kiocb *nxt; |
944e58bf | 1957 | |
9e645e11 JA |
1958 | /* |
1959 | * If LINK is set, we have dependent requests in this chain. If we | |
1960 | * didn't fail this request, queue the first one up, moving any other | |
1961 | * dependencies to the next request. In case of failure, fail the rest | |
1962 | * of the chain. | |
1963 | */ | |
93d2bcd2 | 1964 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) { |
33cc89a9 | 1965 | struct io_ring_ctx *ctx = req->ctx; |
33cc89a9 PB |
1966 | bool posted; |
1967 | ||
79ebeaee | 1968 | spin_lock(&ctx->completion_lock); |
33cc89a9 PB |
1969 | posted = io_disarm_next(req); |
1970 | if (posted) | |
1971 | io_commit_cqring(req->ctx); | |
79ebeaee | 1972 | spin_unlock(&ctx->completion_lock); |
33cc89a9 PB |
1973 | if (posted) |
1974 | io_cqring_ev_posted(ctx); | |
f2f87370 | 1975 | } |
33cc89a9 PB |
1976 | nxt = req->link; |
1977 | req->link = NULL; | |
1978 | return nxt; | |
4d7dd462 | 1979 | } |
9e645e11 | 1980 | |
f2f87370 | 1981 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 1982 | { |
cdbff982 | 1983 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
1984 | return NULL; |
1985 | return __io_req_find_next(req); | |
1986 | } | |
1987 | ||
2c32395d PB |
1988 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
1989 | { | |
1990 | if (!ctx) | |
1991 | return; | |
cd0ca2e0 | 1992 | if (ctx->submit_state.compl_nr) { |
2c32395d | 1993 | mutex_lock(&ctx->uring_lock); |
2a2758f2 | 1994 | io_submit_flush_completions(ctx); |
2c32395d PB |
1995 | mutex_unlock(&ctx->uring_lock); |
1996 | } | |
1997 | percpu_ref_put(&ctx->refs); | |
1998 | } | |
1999 | ||
7cbf1722 | 2000 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2001 | { |
ebd0df2e | 2002 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2003 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2004 | task_work); | |
c40f6379 | 2005 | |
16f72070 | 2006 | while (1) { |
3f18407d PB |
2007 | struct io_wq_work_node *node; |
2008 | ||
2009 | spin_lock_irq(&tctx->task_lock); | |
c6538be9 | 2010 | node = tctx->task_list.first; |
3f18407d | 2011 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2012 | if (!node) |
2013 | tctx->task_running = false; | |
3f18407d | 2014 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2015 | if (!node) |
2016 | break; | |
3f18407d | 2017 | |
6294f368 | 2018 | do { |
3f18407d PB |
2019 | struct io_wq_work_node *next = node->next; |
2020 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2021 | io_task_work.node); | |
2022 | ||
2023 | if (req->ctx != ctx) { | |
2024 | ctx_flush_and_put(ctx); | |
2025 | ctx = req->ctx; | |
2026 | percpu_ref_get(&ctx->refs); | |
2027 | } | |
5b0a6acc | 2028 | req->io_task_work.func(req); |
3f18407d | 2029 | node = next; |
6294f368 PB |
2030 | } while (node); |
2031 | ||
7cbf1722 | 2032 | cond_resched(); |
3f18407d | 2033 | } |
ebd0df2e PB |
2034 | |
2035 | ctx_flush_and_put(ctx); | |
7cbf1722 JA |
2036 | } |
2037 | ||
e09ee510 | 2038 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2039 | { |
c15b79de | 2040 | struct task_struct *tsk = req->task; |
7cbf1722 | 2041 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2042 | enum task_work_notify_mode notify; |
e09ee510 | 2043 | struct io_wq_work_node *node; |
0b81e80c | 2044 | unsigned long flags; |
6294f368 | 2045 | bool running; |
7cbf1722 JA |
2046 | |
2047 | WARN_ON_ONCE(!tctx); | |
2048 | ||
0b81e80c | 2049 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2050 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2051 | running = tctx->task_running; |
2052 | if (!running) | |
2053 | tctx->task_running = true; | |
0b81e80c | 2054 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2055 | |
2056 | /* task_work already pending, we're done */ | |
6294f368 | 2057 | if (running) |
e09ee510 | 2058 | return; |
7cbf1722 | 2059 | |
c15b79de PB |
2060 | /* |
2061 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2062 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2063 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2064 | * will do the job. | |
2065 | */ | |
2066 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2067 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2068 | wake_up_process(tsk); | |
e09ee510 | 2069 | return; |
c15b79de | 2070 | } |
2215bed9 | 2071 | |
0b81e80c | 2072 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2073 | tctx->task_running = false; |
e09ee510 PB |
2074 | node = tctx->task_list.first; |
2075 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2076 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2077 | |
e09ee510 PB |
2078 | while (node) { |
2079 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2080 | node = node->next; | |
2081 | if (llist_add(&req->io_task_work.fallback_node, | |
2082 | &req->ctx->fallback_llist)) | |
2083 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2084 | } | |
eab30c4d PB |
2085 | } |
2086 | ||
5b0a6acc | 2087 | static void io_req_task_cancel(struct io_kiocb *req) |
c40f6379 | 2088 | { |
87ceb6a6 | 2089 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2090 | |
e83acd7d | 2091 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
792bb6eb | 2092 | mutex_lock(&ctx->uring_lock); |
2593553a | 2093 | io_req_complete_failed(req, req->result); |
792bb6eb | 2094 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2095 | } |
2096 | ||
5b0a6acc | 2097 | static void io_req_task_submit(struct io_kiocb *req) |
c40f6379 JA |
2098 | { |
2099 | struct io_ring_ctx *ctx = req->ctx; | |
2100 | ||
04fc6c80 | 2101 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
81b6d05c | 2102 | mutex_lock(&ctx->uring_lock); |
af066f31 | 2103 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2104 | __io_queue_sqe(req); |
81b6d05c | 2105 | else |
2593553a | 2106 | io_req_complete_failed(req, -EFAULT); |
81b6d05c | 2107 | mutex_unlock(&ctx->uring_lock); |
c40f6379 JA |
2108 | } |
2109 | ||
2c4b8eb6 | 2110 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2111 | { |
2c4b8eb6 | 2112 | req->result = ret; |
5b0a6acc | 2113 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2114 | io_req_task_work_add(req); |
c40f6379 JA |
2115 | } |
2116 | ||
2c4b8eb6 | 2117 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2118 | { |
5b0a6acc | 2119 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2120 | io_req_task_work_add(req); |
a3df7698 PB |
2121 | } |
2122 | ||
773af691 JA |
2123 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2124 | { | |
2125 | req->io_task_work.func = io_queue_async_work; | |
2126 | io_req_task_work_add(req); | |
2127 | } | |
2128 | ||
f2f87370 | 2129 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2130 | { |
9b5f7bd9 | 2131 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2132 | |
2133 | if (nxt) | |
906a8c3f | 2134 | io_req_task_queue(nxt); |
c69f8dbe JL |
2135 | } |
2136 | ||
c3524383 | 2137 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2138 | { |
c3524383 PB |
2139 | io_queue_next(req); |
2140 | __io_free_req(req); | |
2141 | } | |
8766dd51 | 2142 | |
2d6500d4 | 2143 | struct req_batch { |
5af1d13e PB |
2144 | struct task_struct *task; |
2145 | int task_refs; | |
1b4c351f | 2146 | int ctx_refs; |
2d6500d4 PB |
2147 | }; |
2148 | ||
5af1d13e PB |
2149 | static inline void io_init_req_batch(struct req_batch *rb) |
2150 | { | |
5af1d13e | 2151 | rb->task_refs = 0; |
9ae72463 | 2152 | rb->ctx_refs = 0; |
5af1d13e PB |
2153 | rb->task = NULL; |
2154 | } | |
2155 | ||
2d6500d4 PB |
2156 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2157 | struct req_batch *rb) | |
2158 | { | |
9ae72463 PB |
2159 | if (rb->ctx_refs) |
2160 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
e9dbe221 PB |
2161 | if (rb->task == current) |
2162 | current->io_uring->cached_refs += rb->task_refs; | |
2163 | else if (rb->task) | |
2164 | io_put_task(rb->task, rb->task_refs); | |
2d6500d4 PB |
2165 | } |
2166 | ||
6ff119a6 PB |
2167 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2168 | struct io_submit_state *state) | |
2d6500d4 | 2169 | { |
f2f87370 | 2170 | io_queue_next(req); |
96670657 | 2171 | io_dismantle_req(req); |
2d6500d4 | 2172 | |
e3bc8e9d | 2173 | if (req->task != rb->task) { |
7c660731 PB |
2174 | if (rb->task) |
2175 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2176 | rb->task = req->task; |
2177 | rb->task_refs = 0; | |
5af1d13e | 2178 | } |
e3bc8e9d | 2179 | rb->task_refs++; |
9ae72463 | 2180 | rb->ctx_refs++; |
5af1d13e | 2181 | |
bd759045 | 2182 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2183 | state->reqs[state->free_reqs++] = req; |
bd759045 | 2184 | else |
cd0ca2e0 | 2185 | list_add(&req->inflight_entry, &state->free_list); |
7a743e22 PB |
2186 | } |
2187 | ||
2a2758f2 | 2188 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2189 | __must_hold(&ctx->uring_lock) |
905c172f | 2190 | { |
cd0ca2e0 PB |
2191 | struct io_submit_state *state = &ctx->submit_state; |
2192 | int i, nr = state->compl_nr; | |
905c172f PB |
2193 | struct req_batch rb; |
2194 | ||
79ebeaee | 2195 | spin_lock(&ctx->completion_lock); |
905c172f | 2196 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2197 | struct io_kiocb *req = state->compl_reqs[i]; |
5182ed2e | 2198 | |
d4d19c19 PB |
2199 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2200 | req->compl.cflags); | |
905c172f PB |
2201 | } |
2202 | io_commit_cqring(ctx); | |
79ebeaee | 2203 | spin_unlock(&ctx->completion_lock); |
905c172f | 2204 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2205 | |
2206 | io_init_req_batch(&rb); | |
905c172f | 2207 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2208 | struct io_kiocb *req = state->compl_reqs[i]; |
905c172f | 2209 | |
91c2f697 | 2210 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2211 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2212 | } |
2213 | ||
2214 | io_req_free_batch_finish(ctx, &rb); | |
cd0ca2e0 | 2215 | state->compl_nr = 0; |
7a743e22 PB |
2216 | } |
2217 | ||
ba816ad6 JA |
2218 | /* |
2219 | * Drop reference to request, return next in chain (if there is one) if this | |
2220 | * was the last reference to this request. | |
2221 | */ | |
0d85035a | 2222 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2223 | { |
9b5f7bd9 PB |
2224 | struct io_kiocb *nxt = NULL; |
2225 | ||
de9b4cca | 2226 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2227 | nxt = io_req_find_next(req); |
4d7dd462 | 2228 | __io_free_req(req); |
2a44f467 | 2229 | } |
9b5f7bd9 | 2230 | return nxt; |
2b188cc1 JA |
2231 | } |
2232 | ||
0d85035a | 2233 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2234 | { |
de9b4cca | 2235 | if (req_ref_put_and_test(req)) |
e65ef56d | 2236 | io_free_req(req); |
2b188cc1 JA |
2237 | } |
2238 | ||
91c2f697 | 2239 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2240 | { |
91c2f697 | 2241 | if (req_ref_put_and_test(req)) { |
543af3a1 PB |
2242 | req->io_task_work.func = io_free_req; |
2243 | io_req_task_work_add(req); | |
2244 | } | |
216578e5 PB |
2245 | } |
2246 | ||
6c503150 | 2247 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2248 | { |
2249 | /* See comment at the top of this file */ | |
2250 | smp_rmb(); | |
e23de15f | 2251 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2252 | } |
2253 | ||
fb5ccc98 PB |
2254 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2255 | { | |
2256 | struct io_rings *rings = ctx->rings; | |
2257 | ||
2258 | /* make sure SQ entry isn't read before tail */ | |
2259 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2260 | } | |
2261 | ||
8ff069bf | 2262 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2263 | { |
8ff069bf | 2264 | unsigned int cflags; |
e94f141b | 2265 | |
bcda7baa JA |
2266 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2267 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2268 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2269 | kfree(kbuf); |
2270 | return cflags; | |
e94f141b JA |
2271 | } |
2272 | ||
8ff069bf | 2273 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2274 | { |
4d954c25 | 2275 | struct io_buffer *kbuf; |
bcda7baa | 2276 | |
4d954c25 | 2277 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2278 | return io_put_kbuf(req, kbuf); |
2279 | } | |
2280 | ||
4c6e277c JA |
2281 | static inline bool io_run_task_work(void) |
2282 | { | |
ef98eb04 | 2283 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2284 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2285 | tracehook_notify_signal(); |
4c6e277c JA |
2286 | return true; |
2287 | } | |
2288 | ||
2289 | return false; | |
bcda7baa JA |
2290 | } |
2291 | ||
def596e9 JA |
2292 | /* |
2293 | * Find and free completed poll iocbs | |
2294 | */ | |
2295 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
a8576af9 | 2296 | struct list_head *done) |
def596e9 | 2297 | { |
8237e045 | 2298 | struct req_batch rb; |
def596e9 | 2299 | struct io_kiocb *req; |
bbde017a XW |
2300 | |
2301 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2302 | smp_rmb(); | |
def596e9 | 2303 | |
5af1d13e | 2304 | io_init_req_batch(&rb); |
def596e9 | 2305 | while (!list_empty(done)) { |
bcda7baa JA |
2306 | int cflags = 0; |
2307 | ||
d21ffe7e | 2308 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2309 | list_del(&req->inflight_entry); |
2310 | ||
a8576af9 | 2311 | if (READ_ONCE(req->result) == -EAGAIN && |
8c130827 | 2312 | !(req->flags & REQ_F_DONT_REISSUE)) { |
bbde017a | 2313 | req->iopoll_completed = 0; |
773af691 | 2314 | io_req_task_queue_reissue(req); |
8c130827 | 2315 | continue; |
bbde017a | 2316 | } |
def596e9 | 2317 | |
bcda7baa | 2318 | if (req->flags & REQ_F_BUFFER_SELECTED) |
8ff069bf | 2319 | cflags = io_put_rw_kbuf(req); |
bcda7baa | 2320 | |
d4d19c19 | 2321 | __io_cqring_fill_event(ctx, req->user_data, req->result, cflags); |
def596e9 JA |
2322 | (*nr_events)++; |
2323 | ||
de9b4cca | 2324 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2325 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2326 | } |
def596e9 | 2327 | |
09bb8394 | 2328 | io_commit_cqring(ctx); |
80c18e4a | 2329 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2330 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2331 | } |
2332 | ||
def596e9 | 2333 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
a8576af9 | 2334 | long min) |
def596e9 JA |
2335 | { |
2336 | struct io_kiocb *req, *tmp; | |
2337 | LIST_HEAD(done); | |
2338 | bool spin; | |
def596e9 JA |
2339 | |
2340 | /* | |
2341 | * Only spin for completions if we don't have multiple devices hanging | |
2342 | * off our complete list, and we're under the requested amount. | |
2343 | */ | |
915b3dde | 2344 | spin = !ctx->poll_multi_queue && *nr_events < min; |
def596e9 | 2345 | |
d21ffe7e | 2346 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2347 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2348 | int ret; |
def596e9 JA |
2349 | |
2350 | /* | |
581f9810 BM |
2351 | * Move completed and retryable entries to our local lists. |
2352 | * If we find a request that requires polling, break out | |
2353 | * and complete those lists first, if we have entries there. | |
def596e9 | 2354 | */ |
65a6543d | 2355 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2356 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2357 | continue; |
2358 | } | |
2359 | if (!list_empty(&done)) | |
2360 | break; | |
2361 | ||
2362 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
a2416e1e PB |
2363 | if (unlikely(ret < 0)) |
2364 | return ret; | |
2365 | else if (ret) | |
2366 | spin = false; | |
def596e9 | 2367 | |
3aadc23e PB |
2368 | /* iopoll may have completed current req */ |
2369 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2370 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2371 | } |
2372 | ||
2373 | if (!list_empty(&done)) | |
a8576af9 | 2374 | io_iopoll_complete(ctx, nr_events, &done); |
def596e9 | 2375 | |
a2416e1e | 2376 | return 0; |
def596e9 JA |
2377 | } |
2378 | ||
def596e9 JA |
2379 | /* |
2380 | * We can't just wait for polled events to come to us, we have to actively | |
2381 | * find and complete them. | |
2382 | */ | |
b2edc0a7 | 2383 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2384 | { |
2385 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2386 | return; | |
2387 | ||
2388 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2389 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2390 | unsigned int nr_events = 0; |
2391 | ||
a8576af9 | 2392 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2393 | |
b2edc0a7 PB |
2394 | /* let it sleep and repeat later if can't complete a request */ |
2395 | if (nr_events == 0) | |
2396 | break; | |
08f5439f JA |
2397 | /* |
2398 | * Ensure we allow local-to-the-cpu processing to take place, | |
2399 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2400 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2401 | */ |
3fcee5a6 PB |
2402 | if (need_resched()) { |
2403 | mutex_unlock(&ctx->uring_lock); | |
2404 | cond_resched(); | |
2405 | mutex_lock(&ctx->uring_lock); | |
2406 | } | |
def596e9 JA |
2407 | } |
2408 | mutex_unlock(&ctx->uring_lock); | |
2409 | } | |
2410 | ||
7668b92a | 2411 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2412 | { |
7668b92a | 2413 | unsigned int nr_events = 0; |
e9979b36 | 2414 | int ret = 0; |
500f9fba | 2415 | |
c7849be9 XW |
2416 | /* |
2417 | * We disallow the app entering submit/complete with polling, but we | |
2418 | * still need to lock the ring to prevent racing with polled issue | |
2419 | * that got punted to a workqueue. | |
2420 | */ | |
2421 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2422 | /* |
2423 | * Don't enter poll loop if we already have events pending. | |
2424 | * If we do, we can potentially be spinning for commands that | |
2425 | * already triggered a CQE (eg in error). | |
2426 | */ | |
5ed7a37d | 2427 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2428 | __io_cqring_overflow_flush(ctx, false); |
2429 | if (io_cqring_events(ctx)) | |
2430 | goto out; | |
def596e9 | 2431 | do { |
500f9fba JA |
2432 | /* |
2433 | * If a submit got punted to a workqueue, we can have the | |
2434 | * application entering polling for a command before it gets | |
2435 | * issued. That app will hold the uring_lock for the duration | |
2436 | * of the poll right here, so we need to take a breather every | |
2437 | * now and then to ensure that the issue has a chance to add | |
2438 | * the poll to the issued list. Otherwise we can spin here | |
2439 | * forever, while the workqueue is stuck trying to acquire the | |
2440 | * very same mutex. | |
2441 | */ | |
e9979b36 | 2442 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2443 | u32 tail = ctx->cached_cq_tail; |
2444 | ||
500f9fba | 2445 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2446 | io_run_task_work(); |
500f9fba | 2447 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2448 | |
8f487ef2 PB |
2449 | /* some requests don't go through iopoll_list */ |
2450 | if (tail != ctx->cached_cq_tail || | |
2451 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2452 | break; |
500f9fba | 2453 | } |
a8576af9 | 2454 | ret = io_do_iopoll(ctx, &nr_events, min); |
f39c8a5b PB |
2455 | } while (!ret && nr_events < min && !need_resched()); |
2456 | out: | |
500f9fba | 2457 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2458 | return ret; |
2459 | } | |
2460 | ||
491381ce | 2461 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2462 | { |
491381ce JA |
2463 | /* |
2464 | * Tell lockdep we inherited freeze protection from submission | |
2465 | * thread. | |
2466 | */ | |
2467 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2468 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2469 | |
1c98679d PB |
2470 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2471 | sb_end_write(sb); | |
2b188cc1 JA |
2472 | } |
2473 | } | |
2474 | ||
b63534c4 | 2475 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2476 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2477 | { |
ab454438 | 2478 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2479 | |
ab454438 PB |
2480 | if (!rw) |
2481 | return !io_req_prep_async(req); | |
2482 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2483 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2484 | return true; | |
b63534c4 | 2485 | } |
b63534c4 | 2486 | |
3e6a0d3c | 2487 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2488 | { |
355afaeb | 2489 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2490 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2491 | |
355afaeb JA |
2492 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2493 | return false; | |
3e6a0d3c JA |
2494 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2495 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2496 | return false; |
7c977a58 JA |
2497 | /* |
2498 | * If ref is dying, we might be running poll reap from the exit work. | |
2499 | * Don't attempt to reissue from that path, just let it fail with | |
2500 | * -EAGAIN. | |
2501 | */ | |
3e6a0d3c JA |
2502 | if (percpu_ref_is_dying(&ctx->refs)) |
2503 | return false; | |
ef046888 JA |
2504 | /* |
2505 | * Play it safe and assume not safe to re-import and reissue if we're | |
2506 | * not in the original thread group (or in task context). | |
2507 | */ | |
2508 | if (!same_thread_group(req->task, current) || !in_task()) | |
2509 | return false; | |
3e6a0d3c JA |
2510 | return true; |
2511 | } | |
e82ad485 | 2512 | #else |
a1ff1e3f | 2513 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2514 | { |
2515 | return false; | |
2516 | } | |
e82ad485 | 2517 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2518 | { |
b63534c4 JA |
2519 | return false; |
2520 | } | |
3e6a0d3c | 2521 | #endif |
b63534c4 | 2522 | |
8ef12efe | 2523 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2524 | { |
b65c128f PB |
2525 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2526 | kiocb_end_write(req); | |
9532b99b PB |
2527 | if (res != req->result) { |
2528 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2529 | io_rw_should_reissue(req)) { | |
2530 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2531 | return true; |
9532b99b | 2532 | } |
93d2bcd2 | 2533 | req_set_fail(req); |
8ef12efe | 2534 | req->result = res; |
9532b99b | 2535 | } |
8ef12efe JA |
2536 | return false; |
2537 | } | |
2538 | ||
2539 | static void io_req_task_complete(struct io_kiocb *req) | |
2540 | { | |
2541 | int cflags = 0; | |
2542 | ||
2f8e45f1 PB |
2543 | if (req->flags & REQ_F_BUFFER_SELECTED) |
2544 | cflags = io_put_rw_kbuf(req); | |
8ef12efe JA |
2545 | __io_req_complete(req, 0, req->result, cflags); |
2546 | } | |
2547 | ||
2548 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2549 | unsigned int issue_flags) | |
2550 | { | |
2551 | if (__io_complete_rw_common(req, res)) | |
2552 | return; | |
2553 | io_req_task_complete(req); | |
ba816ad6 JA |
2554 | } |
2555 | ||
2556 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2557 | { | |
9adbd45d | 2558 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2559 | |
8ef12efe JA |
2560 | if (__io_complete_rw_common(req, res)) |
2561 | return; | |
2562 | req->result = res; | |
2563 | req->io_task_work.func = io_req_task_complete; | |
2564 | io_req_task_work_add(req); | |
2b188cc1 JA |
2565 | } |
2566 | ||
def596e9 JA |
2567 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2568 | { | |
9adbd45d | 2569 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2570 | |
491381ce JA |
2571 | if (kiocb->ki_flags & IOCB_WRITE) |
2572 | kiocb_end_write(req); | |
9532b99b | 2573 | if (unlikely(res != req->result)) { |
a1ff1e3f JA |
2574 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
2575 | io_resubmit_prep(req))) { | |
93d2bcd2 | 2576 | req_set_fail(req); |
9532b99b PB |
2577 | req->flags |= REQ_F_DONT_REISSUE; |
2578 | } | |
8c130827 | 2579 | } |
bbde017a XW |
2580 | |
2581 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2582 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2583 | smp_wmb(); |
2584 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2585 | } |
2586 | ||
2587 | /* | |
2588 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2589 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2590 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2591 | * accessing the kiocb cookie. |
2592 | */ | |
cb3d8972 | 2593 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2594 | { |
2595 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2596 | const bool in_async = io_wq_current_is_worker(); |
2597 | ||
2598 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2599 | if (unlikely(in_async)) | |
2600 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2601 | |
2602 | /* | |
2603 | * Track whether we have multiple files in our lists. This will impact | |
2604 | * how we do polling eventually, not spinning if we're on potentially | |
2605 | * different devices. | |
2606 | */ | |
540e32a0 | 2607 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2608 | ctx->poll_multi_queue = false; |
2609 | } else if (!ctx->poll_multi_queue) { | |
def596e9 | 2610 | struct io_kiocb *list_req; |
915b3dde | 2611 | unsigned int queue_num0, queue_num1; |
def596e9 | 2612 | |
540e32a0 | 2613 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2614 | inflight_entry); |
915b3dde HX |
2615 | |
2616 | if (list_req->file != req->file) { | |
2617 | ctx->poll_multi_queue = true; | |
2618 | } else { | |
2619 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); | |
2620 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); | |
2621 | if (queue_num0 != queue_num1) | |
2622 | ctx->poll_multi_queue = true; | |
2623 | } | |
def596e9 JA |
2624 | } |
2625 | ||
2626 | /* | |
2627 | * For fast devices, IO may have already completed. If it has, add | |
2628 | * it to the front so we find it first. | |
2629 | */ | |
65a6543d | 2630 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2631 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2632 | else |
d21ffe7e | 2633 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2634 | |
cb3d8972 PB |
2635 | if (unlikely(in_async)) { |
2636 | /* | |
2637 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2638 | * in sq thread task context or in io worker task context. If | |
2639 | * current task context is sq thread, we don't need to check | |
2640 | * whether should wake up sq thread. | |
2641 | */ | |
2642 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2643 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2644 | wake_up(&ctx->sq_data->wait); | |
2645 | ||
2646 | mutex_unlock(&ctx->uring_lock); | |
2647 | } | |
def596e9 JA |
2648 | } |
2649 | ||
4503b767 JA |
2650 | static bool io_bdev_nowait(struct block_device *bdev) |
2651 | { | |
9ba0d0c8 | 2652 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2653 | } |
2654 | ||
2b188cc1 JA |
2655 | /* |
2656 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2657 | * any file. For now, just ensure that anything potentially problematic is done | |
2658 | * inline. | |
2659 | */ | |
b191e2df | 2660 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2661 | { |
2662 | umode_t mode = file_inode(file)->i_mode; | |
2663 | ||
4503b767 | 2664 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2665 | if (IS_ENABLED(CONFIG_BLOCK) && |
2666 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2667 | return true; |
2668 | return false; | |
2669 | } | |
976517f1 | 2670 | if (S_ISSOCK(mode)) |
2b188cc1 | 2671 | return true; |
4503b767 | 2672 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2673 | if (IS_ENABLED(CONFIG_BLOCK) && |
2674 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2675 | file->f_op != &io_uring_fops) |
2676 | return true; | |
2677 | return false; | |
2678 | } | |
2b188cc1 | 2679 | |
c5b85625 JA |
2680 | /* any ->read/write should understand O_NONBLOCK */ |
2681 | if (file->f_flags & O_NONBLOCK) | |
2682 | return true; | |
2683 | ||
af197f50 JA |
2684 | if (!(file->f_mode & FMODE_NOWAIT)) |
2685 | return false; | |
2686 | ||
2687 | if (rw == READ) | |
2688 | return file->f_op->read_iter != NULL; | |
2689 | ||
2690 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2691 | } |
2692 | ||
b191e2df | 2693 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2694 | { |
b191e2df | 2695 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2696 | return true; |
b191e2df | 2697 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2698 | return true; |
2699 | ||
b191e2df | 2700 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2701 | } |
2702 | ||
a88fc400 | 2703 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2704 | { |
def596e9 | 2705 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2706 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2707 | struct file *file = req->file; |
09bb8394 JA |
2708 | unsigned ioprio; |
2709 | int ret; | |
2b188cc1 | 2710 | |
c97d8a0f | 2711 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2712 | req->flags |= REQ_F_ISREG; |
2713 | ||
2b188cc1 | 2714 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2715 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2716 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2717 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2718 | } |
2b188cc1 | 2719 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2720 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2721 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2722 | if (unlikely(ret)) | |
2723 | return ret; | |
2b188cc1 | 2724 | |
75c668cd PB |
2725 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2726 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2727 | req->flags |= REQ_F_NOWAIT; | |
2728 | ||
2b188cc1 JA |
2729 | ioprio = READ_ONCE(sqe->ioprio); |
2730 | if (ioprio) { | |
2731 | ret = ioprio_check_cap(ioprio); | |
2732 | if (ret) | |
09bb8394 | 2733 | return ret; |
2b188cc1 JA |
2734 | |
2735 | kiocb->ki_ioprio = ioprio; | |
2736 | } else | |
2737 | kiocb->ki_ioprio = get_current_ioprio(); | |
2738 | ||
def596e9 | 2739 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2740 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2741 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2742 | return -EOPNOTSUPP; |
2b188cc1 | 2743 | |
def596e9 JA |
2744 | kiocb->ki_flags |= IOCB_HIPRI; |
2745 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2746 | req->iopoll_completed = 0; |
def596e9 | 2747 | } else { |
09bb8394 JA |
2748 | if (kiocb->ki_flags & IOCB_HIPRI) |
2749 | return -EINVAL; | |
def596e9 JA |
2750 | kiocb->ki_complete = io_complete_rw; |
2751 | } | |
9adbd45d | 2752 | |
eae071c9 PB |
2753 | if (req->opcode == IORING_OP_READ_FIXED || |
2754 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2755 | req->imu = NULL; | |
2756 | io_req_set_rsrc_node(req); | |
2757 | } | |
2758 | ||
3529d8c2 JA |
2759 | req->rw.addr = READ_ONCE(sqe->addr); |
2760 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2761 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2762 | return 0; |
2b188cc1 JA |
2763 | } |
2764 | ||
2765 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2766 | { | |
2767 | switch (ret) { | |
2768 | case -EIOCBQUEUED: | |
2769 | break; | |
2770 | case -ERESTARTSYS: | |
2771 | case -ERESTARTNOINTR: | |
2772 | case -ERESTARTNOHAND: | |
2773 | case -ERESTART_RESTARTBLOCK: | |
2774 | /* | |
2775 | * We can't just restart the syscall, since previously | |
2776 | * submitted sqes may already be in progress. Just fail this | |
2777 | * IO with EINTR. | |
2778 | */ | |
2779 | ret = -EINTR; | |
df561f66 | 2780 | fallthrough; |
2b188cc1 JA |
2781 | default: |
2782 | kiocb->ki_complete(kiocb, ret, 0); | |
2783 | } | |
2784 | } | |
2785 | ||
a1d7c393 | 2786 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2787 | unsigned int issue_flags) |
ba816ad6 | 2788 | { |
ba04291e | 2789 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2790 | struct io_async_rw *io = req->async_data; |
97284637 | 2791 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2792 | |
227c0c96 | 2793 | /* add previously done IO, if any */ |
e8c2bc1f | 2794 | if (io && io->bytes_done > 0) { |
227c0c96 | 2795 | if (ret < 0) |
e8c2bc1f | 2796 | ret = io->bytes_done; |
227c0c96 | 2797 | else |
e8c2bc1f | 2798 | ret += io->bytes_done; |
227c0c96 JA |
2799 | } |
2800 | ||
ba04291e JA |
2801 | if (req->flags & REQ_F_CUR_POS) |
2802 | req->file->f_pos = kiocb->ki_pos; | |
e149bd74 | 2803 | if (ret >= 0 && check_reissue) |
889fca73 | 2804 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2805 | else |
2806 | io_rw_done(kiocb, ret); | |
97284637 | 2807 | |
fe7e3257 | 2808 | if (check_reissue && (req->flags & REQ_F_REISSUE)) { |
97284637 | 2809 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2810 | if (io_resubmit_prep(req)) { |
773af691 | 2811 | io_req_task_queue_reissue(req); |
8c130827 | 2812 | } else { |
97284637 PB |
2813 | int cflags = 0; |
2814 | ||
93d2bcd2 | 2815 | req_set_fail(req); |
97284637 PB |
2816 | if (req->flags & REQ_F_BUFFER_SELECTED) |
2817 | cflags = io_put_rw_kbuf(req); | |
2818 | __io_req_complete(req, issue_flags, ret, cflags); | |
2819 | } | |
2820 | } | |
ba816ad6 JA |
2821 | } |
2822 | ||
eae071c9 PB |
2823 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2824 | struct io_mapped_ubuf *imu) | |
edafccee | 2825 | { |
9adbd45d | 2826 | size_t len = req->rw.len; |
75769e3f | 2827 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2828 | size_t offset; |
edafccee | 2829 | |
75769e3f | 2830 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2831 | return -EFAULT; |
2832 | /* not inside the mapped region */ | |
4751f53d | 2833 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2834 | return -EFAULT; |
2835 | ||
2836 | /* | |
2837 | * May not be a start of buffer, set size appropriately | |
2838 | * and advance us to the beginning. | |
2839 | */ | |
2840 | offset = buf_addr - imu->ubuf; | |
2841 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2842 | |
2843 | if (offset) { | |
2844 | /* | |
2845 | * Don't use iov_iter_advance() here, as it's really slow for | |
2846 | * using the latter parts of a big fixed buffer - it iterates | |
2847 | * over each segment manually. We can cheat a bit here, because | |
2848 | * we know that: | |
2849 | * | |
2850 | * 1) it's a BVEC iter, we set it up | |
2851 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2852 | * first and last bvec | |
2853 | * | |
2854 | * So just find our index, and adjust the iterator afterwards. | |
2855 | * If the offset is within the first bvec (or the whole first | |
2856 | * bvec, just use iov_iter_advance(). This makes it easier | |
2857 | * since we can just skip the first segment, which may not | |
2858 | * be PAGE_SIZE aligned. | |
2859 | */ | |
2860 | const struct bio_vec *bvec = imu->bvec; | |
2861 | ||
2862 | if (offset <= bvec->bv_len) { | |
2863 | iov_iter_advance(iter, offset); | |
2864 | } else { | |
2865 | unsigned long seg_skip; | |
2866 | ||
2867 | /* skip first vec */ | |
2868 | offset -= bvec->bv_len; | |
2869 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2870 | ||
2871 | iter->bvec = bvec + seg_skip; | |
2872 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2873 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2874 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2875 | } |
2876 | } | |
2877 | ||
847595de | 2878 | return 0; |
edafccee JA |
2879 | } |
2880 | ||
eae071c9 PB |
2881 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2882 | { | |
2883 | struct io_ring_ctx *ctx = req->ctx; | |
2884 | struct io_mapped_ubuf *imu = req->imu; | |
2885 | u16 index, buf_index = req->buf_index; | |
2886 | ||
2887 | if (likely(!imu)) { | |
2888 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2889 | return -EFAULT; | |
2890 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2891 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2892 | req->imu = imu; | |
2893 | } | |
2894 | return __io_import_fixed(req, rw, iter, imu); | |
2895 | } | |
2896 | ||
bcda7baa JA |
2897 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2898 | { | |
2899 | if (needs_lock) | |
2900 | mutex_unlock(&ctx->uring_lock); | |
2901 | } | |
2902 | ||
2903 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2904 | { | |
2905 | /* | |
2906 | * "Normal" inline submissions always hold the uring_lock, since we | |
2907 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2908 | * The only exception is when we've detached the request and issue it | |
2909 | * from an async worker thread, grab the lock for that case. | |
2910 | */ | |
2911 | if (needs_lock) | |
2912 | mutex_lock(&ctx->uring_lock); | |
2913 | } | |
2914 | ||
2915 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2916 | int bgid, struct io_buffer *kbuf, | |
2917 | bool needs_lock) | |
2918 | { | |
2919 | struct io_buffer *head; | |
2920 | ||
2921 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2922 | return kbuf; | |
2923 | ||
2924 | io_ring_submit_lock(req->ctx, needs_lock); | |
2925 | ||
2926 | lockdep_assert_held(&req->ctx->uring_lock); | |
2927 | ||
9e15c3a0 | 2928 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2929 | if (head) { |
2930 | if (!list_empty(&head->list)) { | |
2931 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2932 | list); | |
2933 | list_del(&kbuf->list); | |
2934 | } else { | |
2935 | kbuf = head; | |
9e15c3a0 | 2936 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2937 | } |
2938 | if (*len > kbuf->len) | |
2939 | *len = kbuf->len; | |
2940 | } else { | |
2941 | kbuf = ERR_PTR(-ENOBUFS); | |
2942 | } | |
2943 | ||
2944 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2945 | ||
2946 | return kbuf; | |
2947 | } | |
2948 | ||
4d954c25 JA |
2949 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2950 | bool needs_lock) | |
2951 | { | |
2952 | struct io_buffer *kbuf; | |
4f4eeba8 | 2953 | u16 bgid; |
4d954c25 JA |
2954 | |
2955 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2956 | bgid = req->buf_index; |
4d954c25 JA |
2957 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2958 | if (IS_ERR(kbuf)) | |
2959 | return kbuf; | |
2960 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2961 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2962 | return u64_to_user_ptr(kbuf->addr); | |
2963 | } | |
2964 | ||
2965 | #ifdef CONFIG_COMPAT | |
2966 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
2967 | bool needs_lock) | |
2968 | { | |
2969 | struct compat_iovec __user *uiov; | |
2970 | compat_ssize_t clen; | |
2971 | void __user *buf; | |
2972 | ssize_t len; | |
2973 | ||
2974 | uiov = u64_to_user_ptr(req->rw.addr); | |
2975 | if (!access_ok(uiov, sizeof(*uiov))) | |
2976 | return -EFAULT; | |
2977 | if (__get_user(clen, &uiov->iov_len)) | |
2978 | return -EFAULT; | |
2979 | if (clen < 0) | |
2980 | return -EINVAL; | |
2981 | ||
2982 | len = clen; | |
2983 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
2984 | if (IS_ERR(buf)) | |
2985 | return PTR_ERR(buf); | |
2986 | iov[0].iov_base = buf; | |
2987 | iov[0].iov_len = (compat_size_t) len; | |
2988 | return 0; | |
2989 | } | |
2990 | #endif | |
2991 | ||
2992 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
2993 | bool needs_lock) | |
2994 | { | |
2995 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
2996 | void __user *buf; | |
2997 | ssize_t len; | |
2998 | ||
2999 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3000 | return -EFAULT; | |
3001 | ||
3002 | len = iov[0].iov_len; | |
3003 | if (len < 0) | |
3004 | return -EINVAL; | |
3005 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3006 | if (IS_ERR(buf)) | |
3007 | return PTR_ERR(buf); | |
3008 | iov[0].iov_base = buf; | |
3009 | iov[0].iov_len = len; | |
3010 | return 0; | |
3011 | } | |
3012 | ||
3013 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3014 | bool needs_lock) | |
3015 | { | |
dddb3e26 JA |
3016 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3017 | struct io_buffer *kbuf; | |
3018 | ||
3019 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3020 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3021 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3022 | return 0; |
dddb3e26 | 3023 | } |
dd201662 | 3024 | if (req->rw.len != 1) |
4d954c25 JA |
3025 | return -EINVAL; |
3026 | ||
3027 | #ifdef CONFIG_COMPAT | |
3028 | if (req->ctx->compat) | |
3029 | return io_compat_import(req, iov, needs_lock); | |
3030 | #endif | |
3031 | ||
3032 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3033 | } | |
3034 | ||
847595de PB |
3035 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3036 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3037 | { |
9adbd45d JA |
3038 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3039 | size_t sqe_len = req->rw.len; | |
847595de | 3040 | u8 opcode = req->opcode; |
4d954c25 | 3041 | ssize_t ret; |
edafccee | 3042 | |
7d009165 | 3043 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3044 | *iovec = NULL; |
9adbd45d | 3045 | return io_import_fixed(req, rw, iter); |
edafccee | 3046 | } |
2b188cc1 | 3047 | |
bcda7baa | 3048 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3049 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3050 | return -EINVAL; |
3051 | ||
3a6820f2 | 3052 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3053 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3054 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3055 | if (IS_ERR(buf)) |
4d954c25 | 3056 | return PTR_ERR(buf); |
3f9d6441 | 3057 | req->rw.len = sqe_len; |
bcda7baa JA |
3058 | } |
3059 | ||
3a6820f2 JA |
3060 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3061 | *iovec = NULL; | |
10fc72e4 | 3062 | return ret; |
3a6820f2 JA |
3063 | } |
3064 | ||
4d954c25 JA |
3065 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3066 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3067 | if (!ret) |
3068 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3069 | *iovec = NULL; |
3070 | return ret; | |
3071 | } | |
3072 | ||
89cd35c5 CH |
3073 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3074 | req->ctx->compat); | |
2b188cc1 JA |
3075 | } |
3076 | ||
0fef9483 JA |
3077 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3078 | { | |
5b09e37e | 3079 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3080 | } |
3081 | ||
31b51510 | 3082 | /* |
32960613 JA |
3083 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3084 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3085 | */ |
4017eb91 | 3086 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3087 | { |
4017eb91 JA |
3088 | struct kiocb *kiocb = &req->rw.kiocb; |
3089 | struct file *file = req->file; | |
32960613 JA |
3090 | ssize_t ret = 0; |
3091 | ||
3092 | /* | |
3093 | * Don't support polled IO through this interface, and we can't | |
3094 | * support non-blocking either. For the latter, this just causes | |
3095 | * the kiocb to be handled from an async context. | |
3096 | */ | |
3097 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3098 | return -EOPNOTSUPP; | |
3099 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3100 | return -EAGAIN; | |
3101 | ||
3102 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3103 | struct iovec iovec; |
32960613 JA |
3104 | ssize_t nr; |
3105 | ||
311ae9e1 PB |
3106 | if (!iov_iter_is_bvec(iter)) { |
3107 | iovec = iov_iter_iovec(iter); | |
3108 | } else { | |
4017eb91 JA |
3109 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3110 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3111 | } |
3112 | ||
32960613 JA |
3113 | if (rw == READ) { |
3114 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3115 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3116 | } else { |
3117 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3118 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3119 | } |
3120 | ||
3121 | if (nr < 0) { | |
3122 | if (!ret) | |
3123 | ret = nr; | |
3124 | break; | |
3125 | } | |
3126 | ret += nr; | |
3127 | if (nr != iovec.iov_len) | |
3128 | break; | |
4017eb91 JA |
3129 | req->rw.len -= nr; |
3130 | req->rw.addr += nr; | |
32960613 JA |
3131 | iov_iter_advance(iter, nr); |
3132 | } | |
3133 | ||
3134 | return ret; | |
3135 | } | |
3136 | ||
ff6165b2 JA |
3137 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3138 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3139 | { |
e8c2bc1f | 3140 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3141 | |
ff6165b2 | 3142 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3143 | rw->free_iovec = iovec; |
227c0c96 | 3144 | rw->bytes_done = 0; |
ff6165b2 | 3145 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3146 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3147 | return; |
b64e3444 | 3148 | if (!iovec) { |
ff6165b2 JA |
3149 | unsigned iov_off = 0; |
3150 | ||
3151 | rw->iter.iov = rw->fast_iov; | |
3152 | if (iter->iov != fast_iov) { | |
3153 | iov_off = iter->iov - fast_iov; | |
3154 | rw->iter.iov += iov_off; | |
3155 | } | |
3156 | if (rw->fast_iov != fast_iov) | |
3157 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3158 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3159 | } else { |
3160 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3161 | } |
3162 | } | |
3163 | ||
6cb78689 | 3164 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3165 | { |
e8c2bc1f JA |
3166 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3167 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3168 | return req->async_data == NULL; | |
3d9932a8 XW |
3169 | } |
3170 | ||
ff6165b2 JA |
3171 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3172 | const struct iovec *fast_iov, | |
227c0c96 | 3173 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3174 | { |
26f0505a | 3175 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3176 | return 0; |
e8c2bc1f | 3177 | if (!req->async_data) { |
6cb78689 | 3178 | if (io_alloc_async_data(req)) { |
6bf985dc | 3179 | kfree(iovec); |
5d204bcf | 3180 | return -ENOMEM; |
6bf985dc | 3181 | } |
b7bb4f7d | 3182 | |
ff6165b2 | 3183 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3184 | } |
b7bb4f7d | 3185 | return 0; |
f67676d1 JA |
3186 | } |
3187 | ||
73debe68 | 3188 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3189 | { |
e8c2bc1f | 3190 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3191 | struct iovec *iov = iorw->fast_iov; |
847595de | 3192 | int ret; |
c3e330a4 | 3193 | |
2846c481 | 3194 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3195 | if (unlikely(ret < 0)) |
3196 | return ret; | |
3197 | ||
ab0b196c PB |
3198 | iorw->bytes_done = 0; |
3199 | iorw->free_iovec = iov; | |
3200 | if (iov) | |
3201 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3202 | return 0; |
3203 | } | |
3204 | ||
73debe68 | 3205 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3206 | { |
3529d8c2 JA |
3207 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3208 | return -EBADF; | |
93642ef8 | 3209 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3210 | } |
3211 | ||
c1dd91d1 JA |
3212 | /* |
3213 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3214 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3215 | * This gets called when the page is unlocked, and we generally expect that to | |
3216 | * happen when the page IO is completed and the page is now uptodate. This will | |
3217 | * queue a task_work based retry of the operation, attempting to copy the data | |
3218 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3219 | * do a thread based blocking retry of the operation. That's the unexpected | |
3220 | * slow path. | |
3221 | */ | |
bcf5a063 JA |
3222 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3223 | int sync, void *arg) | |
3224 | { | |
3225 | struct wait_page_queue *wpq; | |
3226 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3227 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3228 | |
3229 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3230 | ||
cdc8fcb4 LT |
3231 | if (!wake_page_match(wpq, key)) |
3232 | return 0; | |
3233 | ||
c8d317aa | 3234 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3235 | list_del_init(&wait->entry); |
921b9054 | 3236 | io_req_task_queue(req); |
bcf5a063 JA |
3237 | return 1; |
3238 | } | |
3239 | ||
c1dd91d1 JA |
3240 | /* |
3241 | * This controls whether a given IO request should be armed for async page | |
3242 | * based retry. If we return false here, the request is handed to the async | |
3243 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3244 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3245 | * will either succeed because the page is now uptodate and unlocked, or it | |
3246 | * will register a callback when the page is unlocked at IO completion. Through | |
3247 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3248 | * That retry will attempt the buffered read again. The retry will generally | |
3249 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3250 | * async worker threads for a blocking retry. | |
3251 | */ | |
227c0c96 | 3252 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3253 | { |
e8c2bc1f JA |
3254 | struct io_async_rw *rw = req->async_data; |
3255 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3256 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3257 | |
bcf5a063 JA |
3258 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3259 | if (req->flags & REQ_F_NOWAIT) | |
3260 | return false; | |
f67676d1 | 3261 | |
227c0c96 | 3262 | /* Only for buffered IO */ |
3b2a4439 | 3263 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3264 | return false; |
3b2a4439 | 3265 | |
bcf5a063 JA |
3266 | /* |
3267 | * just use poll if we can, and don't attempt if the fs doesn't | |
3268 | * support callback based unlocks | |
3269 | */ | |
3270 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3271 | return false; | |
f67676d1 | 3272 | |
3b2a4439 JA |
3273 | wait->wait.func = io_async_buf_func; |
3274 | wait->wait.private = req; | |
3275 | wait->wait.flags = 0; | |
3276 | INIT_LIST_HEAD(&wait->wait.entry); | |
3277 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3278 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3279 | kiocb->ki_waitq = wait; |
3b2a4439 | 3280 | return true; |
bcf5a063 JA |
3281 | } |
3282 | ||
aeab9506 | 3283 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3284 | { |
3285 | if (req->file->f_op->read_iter) | |
3286 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3287 | else if (req->file->f_op->read) |
4017eb91 | 3288 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3289 | else |
3290 | return -EINVAL; | |
f67676d1 JA |
3291 | } |
3292 | ||
889fca73 | 3293 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3294 | { |
3295 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3296 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3297 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3298 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3299 | ssize_t io_size, ret, ret2; |
45d189c6 | 3300 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3301 | |
2846c481 | 3302 | if (rw) { |
e8c2bc1f | 3303 | iter = &rw->iter; |
2846c481 PB |
3304 | iovec = NULL; |
3305 | } else { | |
3306 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3307 | if (ret < 0) | |
3308 | return ret; | |
3309 | } | |
632546c4 | 3310 | io_size = iov_iter_count(iter); |
fa15bafb | 3311 | req->result = io_size; |
2b188cc1 | 3312 | |
fd6c2e4c JA |
3313 | /* Ensure we clear previously set non-block flag */ |
3314 | if (!force_nonblock) | |
29de5f6a | 3315 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3316 | else |
3317 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3318 | ||
24c74678 | 3319 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3320 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3321 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3322 | return ret ?: -EAGAIN; |
6713e7a6 | 3323 | } |
9e645e11 | 3324 | |
632546c4 | 3325 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3326 | if (unlikely(ret)) { |
3327 | kfree(iovec); | |
3328 | return ret; | |
3329 | } | |
2b188cc1 | 3330 | |
227c0c96 | 3331 | ret = io_iter_do_read(req, iter); |
32960613 | 3332 | |
230d50d4 | 3333 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3334 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3335 | /* IOPOLL retry should happen for io-wq threads */ |
3336 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3337 | goto done; |
75c668cd PB |
3338 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3339 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3340 | goto done; |
84216315 | 3341 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3342 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3343 | ret = 0; |
230d50d4 JA |
3344 | } else if (ret == -EIOCBQUEUED) { |
3345 | goto out_free; | |
7335e3bf | 3346 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3347 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3348 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3349 | goto done; |
227c0c96 JA |
3350 | } |
3351 | ||
227c0c96 | 3352 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3353 | if (ret2) |
3354 | return ret2; | |
3355 | ||
fe1cdd55 | 3356 | iovec = NULL; |
e8c2bc1f | 3357 | rw = req->async_data; |
227c0c96 | 3358 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3359 | iter = &rw->iter; |
227c0c96 | 3360 | |
b23df91b PB |
3361 | do { |
3362 | io_size -= ret; | |
3363 | rw->bytes_done += ret; | |
3364 | /* if we can retry, do so with the callbacks armed */ | |
3365 | if (!io_rw_should_retry(req)) { | |
3366 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3367 | return -EAGAIN; | |
3368 | } | |
3369 | ||
3370 | /* | |
3371 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3372 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3373 | * desired page gets unlocked. We can also get a partial read | |
3374 | * here, and if we do, then just retry at the new offset. | |
3375 | */ | |
3376 | ret = io_iter_do_read(req, iter); | |
3377 | if (ret == -EIOCBQUEUED) | |
3378 | return 0; | |
227c0c96 | 3379 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3380 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3381 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3382 | done: |
889fca73 | 3383 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3384 | out_free: |
3385 | /* it's faster to check here then delegate to kfree */ | |
3386 | if (iovec) | |
3387 | kfree(iovec); | |
5ea5dd45 | 3388 | return 0; |
2b188cc1 JA |
3389 | } |
3390 | ||
73debe68 | 3391 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3392 | { |
3529d8c2 JA |
3393 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3394 | return -EBADF; | |
93642ef8 | 3395 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3396 | } |
3397 | ||
889fca73 | 3398 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3399 | { |
3400 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3401 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3402 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3403 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3404 | ssize_t ret, ret2, io_size; |
45d189c6 | 3405 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3406 | |
2846c481 | 3407 | if (rw) { |
e8c2bc1f | 3408 | iter = &rw->iter; |
2846c481 PB |
3409 | iovec = NULL; |
3410 | } else { | |
3411 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3412 | if (ret < 0) | |
3413 | return ret; | |
3414 | } | |
632546c4 | 3415 | io_size = iov_iter_count(iter); |
fa15bafb | 3416 | req->result = io_size; |
2b188cc1 | 3417 | |
fd6c2e4c JA |
3418 | /* Ensure we clear previously set non-block flag */ |
3419 | if (!force_nonblock) | |
a88fc400 PB |
3420 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3421 | else | |
3422 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3423 | |
24c74678 | 3424 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3425 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3426 | goto copy_iov; |
31b51510 | 3427 | |
10d59345 JA |
3428 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3429 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3430 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3431 | goto copy_iov; |
31b51510 | 3432 | |
632546c4 | 3433 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3434 | if (unlikely(ret)) |
3435 | goto out_free; | |
4ed734b0 | 3436 | |
fa15bafb PB |
3437 | /* |
3438 | * Open-code file_start_write here to grab freeze protection, | |
3439 | * which will be released by another thread in | |
3440 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3441 | * released so that it doesn't complain about the held lock when | |
3442 | * we return to userspace. | |
3443 | */ | |
3444 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3445 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3446 | __sb_writers_release(file_inode(req->file)->i_sb, |
3447 | SB_FREEZE_WRITE); | |
3448 | } | |
3449 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3450 | |
fa15bafb | 3451 | if (req->file->f_op->write_iter) |
ff6165b2 | 3452 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3453 | else if (req->file->f_op->write) |
4017eb91 | 3454 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3455 | else |
3456 | ret2 = -EINVAL; | |
4ed734b0 | 3457 | |
6ad7f233 PB |
3458 | if (req->flags & REQ_F_REISSUE) { |
3459 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3460 | ret2 = -EAGAIN; |
6ad7f233 | 3461 | } |
230d50d4 | 3462 | |
fa15bafb PB |
3463 | /* |
3464 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3465 | * retry them without IOCB_NOWAIT. | |
3466 | */ | |
3467 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3468 | ret2 = -EAGAIN; | |
75c668cd PB |
3469 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3470 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3471 | goto done; |
fa15bafb | 3472 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3473 | /* IOPOLL retry should happen for io-wq threads */ |
3474 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3475 | goto copy_iov; | |
355afaeb | 3476 | done: |
889fca73 | 3477 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3478 | } else { |
f67676d1 | 3479 | copy_iov: |
84216315 | 3480 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3481 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3482 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3483 | return ret ?: -EAGAIN; |
2b188cc1 | 3484 | } |
31b51510 | 3485 | out_free: |
f261c168 | 3486 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3487 | if (iovec) |
6f2cc166 | 3488 | kfree(iovec); |
2b188cc1 JA |
3489 | return ret; |
3490 | } | |
3491 | ||
80a261fd JA |
3492 | static int io_renameat_prep(struct io_kiocb *req, |
3493 | const struct io_uring_sqe *sqe) | |
3494 | { | |
3495 | struct io_rename *ren = &req->rename; | |
3496 | const char __user *oldf, *newf; | |
3497 | ||
ed7eb259 JA |
3498 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3499 | return -EINVAL; | |
3500 | if (sqe->ioprio || sqe->buf_index) | |
3501 | return -EINVAL; | |
80a261fd JA |
3502 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3503 | return -EBADF; | |
3504 | ||
3505 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3506 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3507 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3508 | ren->new_dfd = READ_ONCE(sqe->len); | |
3509 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3510 | ||
3511 | ren->oldpath = getname(oldf); | |
3512 | if (IS_ERR(ren->oldpath)) | |
3513 | return PTR_ERR(ren->oldpath); | |
3514 | ||
3515 | ren->newpath = getname(newf); | |
3516 | if (IS_ERR(ren->newpath)) { | |
3517 | putname(ren->oldpath); | |
3518 | return PTR_ERR(ren->newpath); | |
3519 | } | |
3520 | ||
3521 | req->flags |= REQ_F_NEED_CLEANUP; | |
3522 | return 0; | |
3523 | } | |
3524 | ||
45d189c6 | 3525 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3526 | { |
3527 | struct io_rename *ren = &req->rename; | |
3528 | int ret; | |
3529 | ||
45d189c6 | 3530 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3531 | return -EAGAIN; |
3532 | ||
3533 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3534 | ren->newpath, ren->flags); | |
3535 | ||
3536 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3537 | if (ret < 0) | |
93d2bcd2 | 3538 | req_set_fail(req); |
80a261fd JA |
3539 | io_req_complete(req, ret); |
3540 | return 0; | |
3541 | } | |
3542 | ||
14a1143b JA |
3543 | static int io_unlinkat_prep(struct io_kiocb *req, |
3544 | const struct io_uring_sqe *sqe) | |
3545 | { | |
3546 | struct io_unlink *un = &req->unlink; | |
3547 | const char __user *fname; | |
3548 | ||
22634bc5 JA |
3549 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3550 | return -EINVAL; | |
3551 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
3552 | return -EINVAL; | |
14a1143b JA |
3553 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3554 | return -EBADF; | |
3555 | ||
3556 | un->dfd = READ_ONCE(sqe->fd); | |
3557 | ||
3558 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3559 | if (un->flags & ~AT_REMOVEDIR) | |
3560 | return -EINVAL; | |
3561 | ||
3562 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3563 | un->filename = getname(fname); | |
3564 | if (IS_ERR(un->filename)) | |
3565 | return PTR_ERR(un->filename); | |
3566 | ||
3567 | req->flags |= REQ_F_NEED_CLEANUP; | |
3568 | return 0; | |
3569 | } | |
3570 | ||
45d189c6 | 3571 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3572 | { |
3573 | struct io_unlink *un = &req->unlink; | |
3574 | int ret; | |
3575 | ||
45d189c6 | 3576 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3577 | return -EAGAIN; |
3578 | ||
3579 | if (un->flags & AT_REMOVEDIR) | |
3580 | ret = do_rmdir(un->dfd, un->filename); | |
3581 | else | |
3582 | ret = do_unlinkat(un->dfd, un->filename); | |
3583 | ||
3584 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3585 | if (ret < 0) | |
93d2bcd2 | 3586 | req_set_fail(req); |
14a1143b JA |
3587 | io_req_complete(req, ret); |
3588 | return 0; | |
3589 | } | |
3590 | ||
36f4fa68 JA |
3591 | static int io_shutdown_prep(struct io_kiocb *req, |
3592 | const struct io_uring_sqe *sqe) | |
3593 | { | |
3594 | #if defined(CONFIG_NET) | |
3595 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3596 | return -EINVAL; | |
3597 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || | |
3598 | sqe->buf_index) | |
3599 | return -EINVAL; | |
3600 | ||
3601 | req->shutdown.how = READ_ONCE(sqe->len); | |
3602 | return 0; | |
3603 | #else | |
3604 | return -EOPNOTSUPP; | |
3605 | #endif | |
3606 | } | |
3607 | ||
45d189c6 | 3608 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3609 | { |
3610 | #if defined(CONFIG_NET) | |
3611 | struct socket *sock; | |
3612 | int ret; | |
3613 | ||
45d189c6 | 3614 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3615 | return -EAGAIN; |
3616 | ||
48aba79b | 3617 | sock = sock_from_file(req->file); |
36f4fa68 | 3618 | if (unlikely(!sock)) |
48aba79b | 3619 | return -ENOTSOCK; |
36f4fa68 JA |
3620 | |
3621 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3622 | if (ret < 0) |
93d2bcd2 | 3623 | req_set_fail(req); |
36f4fa68 JA |
3624 | io_req_complete(req, ret); |
3625 | return 0; | |
3626 | #else | |
3627 | return -EOPNOTSUPP; | |
3628 | #endif | |
3629 | } | |
3630 | ||
f2a8d5c7 PB |
3631 | static int __io_splice_prep(struct io_kiocb *req, |
3632 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3633 | { |
fe7e3257 | 3634 | struct io_splice *sp = &req->splice; |
7d67af2c | 3635 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3636 | |
3232dd02 PB |
3637 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3638 | return -EINVAL; | |
7d67af2c PB |
3639 | |
3640 | sp->file_in = NULL; | |
7d67af2c PB |
3641 | sp->len = READ_ONCE(sqe->len); |
3642 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3643 | ||
3644 | if (unlikely(sp->flags & ~valid_flags)) | |
3645 | return -EINVAL; | |
3646 | ||
62906e89 | 3647 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3648 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3649 | if (!sp->file_in) | |
3650 | return -EBADF; | |
7d67af2c | 3651 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3652 | return 0; |
3653 | } | |
3654 | ||
f2a8d5c7 PB |
3655 | static int io_tee_prep(struct io_kiocb *req, |
3656 | const struct io_uring_sqe *sqe) | |
3657 | { | |
3658 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3659 | return -EINVAL; | |
3660 | return __io_splice_prep(req, sqe); | |
3661 | } | |
3662 | ||
45d189c6 | 3663 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3664 | { |
3665 | struct io_splice *sp = &req->splice; | |
3666 | struct file *in = sp->file_in; | |
3667 | struct file *out = sp->file_out; | |
3668 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3669 | long ret = 0; | |
3670 | ||
45d189c6 | 3671 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3672 | return -EAGAIN; |
3673 | if (sp->len) | |
3674 | ret = do_tee(in, out, sp->len, flags); | |
3675 | ||
e1d767f0 PB |
3676 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3677 | io_put_file(in); | |
f2a8d5c7 PB |
3678 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3679 | ||
f2a8d5c7 | 3680 | if (ret != sp->len) |
93d2bcd2 | 3681 | req_set_fail(req); |
e1e16097 | 3682 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3683 | return 0; |
3684 | } | |
3685 | ||
3686 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3687 | { | |
fe7e3257 | 3688 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3689 | |
3690 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3691 | sp->off_out = READ_ONCE(sqe->off); | |
3692 | return __io_splice_prep(req, sqe); | |
3693 | } | |
3694 | ||
45d189c6 | 3695 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3696 | { |
3697 | struct io_splice *sp = &req->splice; | |
3698 | struct file *in = sp->file_in; | |
3699 | struct file *out = sp->file_out; | |
3700 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3701 | loff_t *poff_in, *poff_out; | |
c9687426 | 3702 | long ret = 0; |
7d67af2c | 3703 | |
45d189c6 | 3704 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3705 | return -EAGAIN; |
7d67af2c PB |
3706 | |
3707 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3708 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3709 | |
948a7749 | 3710 | if (sp->len) |
c9687426 | 3711 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3712 | |
e1d767f0 PB |
3713 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3714 | io_put_file(in); | |
7d67af2c PB |
3715 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3716 | ||
7d67af2c | 3717 | if (ret != sp->len) |
93d2bcd2 | 3718 | req_set_fail(req); |
e1e16097 | 3719 | io_req_complete(req, ret); |
7d67af2c PB |
3720 | return 0; |
3721 | } | |
3722 | ||
2b188cc1 JA |
3723 | /* |
3724 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3725 | */ | |
889fca73 | 3726 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3727 | { |
3728 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3729 | |
def596e9 JA |
3730 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3731 | return -EINVAL; | |
3732 | ||
889fca73 | 3733 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3734 | return 0; |
3735 | } | |
3736 | ||
1155c76a | 3737 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3738 | { |
6b06314c | 3739 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3740 | |
09bb8394 JA |
3741 | if (!req->file) |
3742 | return -EBADF; | |
c992fe29 | 3743 | |
6b06314c | 3744 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3745 | return -EINVAL; |
edafccee | 3746 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
3747 | return -EINVAL; |
3748 | ||
8ed8d3c3 JA |
3749 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3750 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3751 | return -EINVAL; | |
3752 | ||
3753 | req->sync.off = READ_ONCE(sqe->off); | |
3754 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3755 | return 0; |
3756 | } | |
3757 | ||
45d189c6 | 3758 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3759 | { |
8ed8d3c3 | 3760 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3761 | int ret; |
3762 | ||
ac45abc0 | 3763 | /* fsync always requires a blocking context */ |
45d189c6 | 3764 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3765 | return -EAGAIN; |
3766 | ||
9adbd45d | 3767 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3768 | end > 0 ? end : LLONG_MAX, |
3769 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3770 | if (ret < 0) | |
93d2bcd2 | 3771 | req_set_fail(req); |
e1e16097 | 3772 | io_req_complete(req, ret); |
c992fe29 CH |
3773 | return 0; |
3774 | } | |
3775 | ||
d63d1b5e JA |
3776 | static int io_fallocate_prep(struct io_kiocb *req, |
3777 | const struct io_uring_sqe *sqe) | |
3778 | { | |
3779 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
3780 | return -EINVAL; | |
3232dd02 PB |
3781 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3782 | return -EINVAL; | |
d63d1b5e JA |
3783 | |
3784 | req->sync.off = READ_ONCE(sqe->off); | |
3785 | req->sync.len = READ_ONCE(sqe->addr); | |
3786 | req->sync.mode = READ_ONCE(sqe->len); | |
3787 | return 0; | |
3788 | } | |
3789 | ||
45d189c6 | 3790 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3791 | { |
ac45abc0 PB |
3792 | int ret; |
3793 | ||
d63d1b5e | 3794 | /* fallocate always requiring blocking context */ |
45d189c6 | 3795 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3796 | return -EAGAIN; |
ac45abc0 PB |
3797 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3798 | req->sync.len); | |
ac45abc0 | 3799 | if (ret < 0) |
93d2bcd2 | 3800 | req_set_fail(req); |
e1e16097 | 3801 | io_req_complete(req, ret); |
5d17b4a4 JA |
3802 | return 0; |
3803 | } | |
3804 | ||
ec65fea5 | 3805 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3806 | { |
f8748881 | 3807 | const char __user *fname; |
15b71abe | 3808 | int ret; |
b7bb4f7d | 3809 | |
d3fddf6d PB |
3810 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3811 | return -EINVAL; | |
ec65fea5 | 3812 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3813 | return -EINVAL; |
ec65fea5 | 3814 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3815 | return -EBADF; |
03b1230c | 3816 | |
ec65fea5 PB |
3817 | /* open.how should be already initialised */ |
3818 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3819 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3820 | |
25e72d10 PB |
3821 | req->open.dfd = READ_ONCE(sqe->fd); |
3822 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3823 | req->open.filename = getname(fname); |
15b71abe JA |
3824 | if (IS_ERR(req->open.filename)) { |
3825 | ret = PTR_ERR(req->open.filename); | |
3826 | req->open.filename = NULL; | |
3827 | return ret; | |
3828 | } | |
4022e7af | 3829 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3830 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3831 | return 0; |
03b1230c JA |
3832 | } |
3833 | ||
ec65fea5 PB |
3834 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3835 | { | |
d3fddf6d PB |
3836 | u64 mode = READ_ONCE(sqe->len); |
3837 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 3838 | |
ec65fea5 PB |
3839 | req->open.how = build_open_how(flags, mode); |
3840 | return __io_openat_prep(req, sqe); | |
3841 | } | |
3842 | ||
cebdb986 | 3843 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3844 | { |
cebdb986 | 3845 | struct open_how __user *how; |
cebdb986 | 3846 | size_t len; |
0fa03c62 JA |
3847 | int ret; |
3848 | ||
cebdb986 JA |
3849 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3850 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3851 | if (len < OPEN_HOW_SIZE_VER0) |
3852 | return -EINVAL; | |
3529d8c2 | 3853 | |
cebdb986 JA |
3854 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3855 | len); | |
3856 | if (ret) | |
3857 | return ret; | |
3529d8c2 | 3858 | |
ec65fea5 | 3859 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3860 | } |
3861 | ||
45d189c6 | 3862 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3863 | { |
3864 | struct open_flags op; | |
15b71abe | 3865 | struct file *file; |
3a81fd02 JA |
3866 | bool nonblock_set; |
3867 | bool resolve_nonblock; | |
15b71abe JA |
3868 | int ret; |
3869 | ||
cebdb986 | 3870 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3871 | if (ret) |
3872 | goto err; | |
3a81fd02 JA |
3873 | nonblock_set = op.open_flag & O_NONBLOCK; |
3874 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3875 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3876 | /* |
3877 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3878 | * it'll always -EAGAIN | |
3879 | */ | |
3880 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3881 | return -EAGAIN; | |
3882 | op.lookup_flags |= LOOKUP_CACHED; | |
3883 | op.open_flag |= O_NONBLOCK; | |
3884 | } | |
15b71abe | 3885 | |
4022e7af | 3886 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3887 | if (ret < 0) |
3888 | goto err; | |
3889 | ||
3890 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 3891 | if (IS_ERR(file)) { |
944d1444 | 3892 | /* |
12dcb58a PB |
3893 | * We could hang on to this 'fd' on retrying, but seems like |
3894 | * marginal gain for something that is now known to be a slower | |
3895 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 3896 | */ |
3a81fd02 | 3897 | put_unused_fd(ret); |
3a81fd02 | 3898 | |
15b71abe | 3899 | ret = PTR_ERR(file); |
12dcb58a PB |
3900 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
3901 | if (ret == -EAGAIN && | |
3902 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
3903 | return -EAGAIN; | |
3904 | goto err; | |
15b71abe | 3905 | } |
12dcb58a PB |
3906 | |
3907 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
3908 | file->f_flags &= ~O_NONBLOCK; | |
3909 | fsnotify_open(file); | |
3910 | fd_install(ret, file); | |
15b71abe JA |
3911 | err: |
3912 | putname(req->open.filename); | |
8fef80bf | 3913 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 3914 | if (ret < 0) |
93d2bcd2 | 3915 | req_set_fail(req); |
0bdf3398 | 3916 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
3917 | return 0; |
3918 | } | |
3919 | ||
45d189c6 | 3920 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3921 | { |
e45cff58 | 3922 | return io_openat2(req, issue_flags); |
cebdb986 JA |
3923 | } |
3924 | ||
067524e9 JA |
3925 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3926 | const struct io_uring_sqe *sqe) | |
3927 | { | |
3928 | struct io_provide_buf *p = &req->pbuf; | |
3929 | u64 tmp; | |
3930 | ||
3931 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) | |
3932 | return -EINVAL; | |
3933 | ||
3934 | tmp = READ_ONCE(sqe->fd); | |
3935 | if (!tmp || tmp > USHRT_MAX) | |
3936 | return -EINVAL; | |
3937 | ||
3938 | memset(p, 0, sizeof(*p)); | |
3939 | p->nbufs = tmp; | |
3940 | p->bgid = READ_ONCE(sqe->buf_group); | |
3941 | return 0; | |
3942 | } | |
3943 | ||
3944 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3945 | int bgid, unsigned nbufs) | |
3946 | { | |
3947 | unsigned i = 0; | |
3948 | ||
3949 | /* shouldn't happen */ | |
3950 | if (!nbufs) | |
3951 | return 0; | |
3952 | ||
3953 | /* the head kbuf is the list itself */ | |
3954 | while (!list_empty(&buf->list)) { | |
3955 | struct io_buffer *nxt; | |
3956 | ||
3957 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3958 | list_del(&nxt->list); | |
3959 | kfree(nxt); | |
3960 | if (++i == nbufs) | |
3961 | return i; | |
3962 | } | |
3963 | i++; | |
3964 | kfree(buf); | |
9e15c3a0 | 3965 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
3966 | |
3967 | return i; | |
3968 | } | |
3969 | ||
889fca73 | 3970 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
3971 | { |
3972 | struct io_provide_buf *p = &req->pbuf; | |
3973 | struct io_ring_ctx *ctx = req->ctx; | |
3974 | struct io_buffer *head; | |
3975 | int ret = 0; | |
45d189c6 | 3976 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
3977 | |
3978 | io_ring_submit_lock(ctx, !force_nonblock); | |
3979 | ||
3980 | lockdep_assert_held(&ctx->uring_lock); | |
3981 | ||
3982 | ret = -ENOENT; | |
9e15c3a0 | 3983 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
3984 | if (head) |
3985 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 3986 | if (ret < 0) |
93d2bcd2 | 3987 | req_set_fail(req); |
067524e9 | 3988 | |
9fb8cb49 PB |
3989 | /* complete before unlock, IOPOLL may need the lock */ |
3990 | __io_req_complete(req, issue_flags, ret, 0); | |
3991 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
3992 | return 0; |
3993 | } | |
3994 | ||
ddf0322d JA |
3995 | static int io_provide_buffers_prep(struct io_kiocb *req, |
3996 | const struct io_uring_sqe *sqe) | |
3997 | { | |
38134ada | 3998 | unsigned long size, tmp_check; |
ddf0322d JA |
3999 | struct io_provide_buf *p = &req->pbuf; |
4000 | u64 tmp; | |
4001 | ||
4002 | if (sqe->ioprio || sqe->rw_flags) | |
4003 | return -EINVAL; | |
4004 | ||
4005 | tmp = READ_ONCE(sqe->fd); | |
4006 | if (!tmp || tmp > USHRT_MAX) | |
4007 | return -E2BIG; | |
4008 | p->nbufs = tmp; | |
4009 | p->addr = READ_ONCE(sqe->addr); | |
4010 | p->len = READ_ONCE(sqe->len); | |
4011 | ||
38134ada PB |
4012 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4013 | &size)) | |
4014 | return -EOVERFLOW; | |
4015 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4016 | return -EOVERFLOW; | |
4017 | ||
d81269fe PB |
4018 | size = (unsigned long)p->len * p->nbufs; |
4019 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4020 | return -EFAULT; |
4021 | ||
4022 | p->bgid = READ_ONCE(sqe->buf_group); | |
4023 | tmp = READ_ONCE(sqe->off); | |
4024 | if (tmp > USHRT_MAX) | |
4025 | return -E2BIG; | |
4026 | p->bid = tmp; | |
4027 | return 0; | |
4028 | } | |
4029 | ||
4030 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4031 | { | |
4032 | struct io_buffer *buf; | |
4033 | u64 addr = pbuf->addr; | |
4034 | int i, bid = pbuf->bid; | |
4035 | ||
4036 | for (i = 0; i < pbuf->nbufs; i++) { | |
4037 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4038 | if (!buf) | |
4039 | break; | |
4040 | ||
4041 | buf->addr = addr; | |
d1f82808 | 4042 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4043 | buf->bid = bid; |
4044 | addr += pbuf->len; | |
4045 | bid++; | |
4046 | if (!*head) { | |
4047 | INIT_LIST_HEAD(&buf->list); | |
4048 | *head = buf; | |
4049 | } else { | |
4050 | list_add_tail(&buf->list, &(*head)->list); | |
4051 | } | |
4052 | } | |
4053 | ||
4054 | return i ? i : -ENOMEM; | |
4055 | } | |
4056 | ||
889fca73 | 4057 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4058 | { |
4059 | struct io_provide_buf *p = &req->pbuf; | |
4060 | struct io_ring_ctx *ctx = req->ctx; | |
4061 | struct io_buffer *head, *list; | |
4062 | int ret = 0; | |
45d189c6 | 4063 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4064 | |
4065 | io_ring_submit_lock(ctx, !force_nonblock); | |
4066 | ||
4067 | lockdep_assert_held(&ctx->uring_lock); | |
4068 | ||
9e15c3a0 | 4069 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4070 | |
4071 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4072 | if (ret >= 0 && !list) { |
4073 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4074 | if (ret < 0) | |
067524e9 | 4075 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4076 | } |
ddf0322d | 4077 | if (ret < 0) |
93d2bcd2 | 4078 | req_set_fail(req); |
9fb8cb49 PB |
4079 | /* complete before unlock, IOPOLL may need the lock */ |
4080 | __io_req_complete(req, issue_flags, ret, 0); | |
4081 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4082 | return 0; |
cebdb986 JA |
4083 | } |
4084 | ||
3e4827b0 JA |
4085 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4086 | const struct io_uring_sqe *sqe) | |
4087 | { | |
4088 | #if defined(CONFIG_EPOLL) | |
4089 | if (sqe->ioprio || sqe->buf_index) | |
4090 | return -EINVAL; | |
2d74d042 | 4091 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4092 | return -EINVAL; |
3e4827b0 JA |
4093 | |
4094 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4095 | req->epoll.op = READ_ONCE(sqe->len); | |
4096 | req->epoll.fd = READ_ONCE(sqe->off); | |
4097 | ||
4098 | if (ep_op_has_event(req->epoll.op)) { | |
4099 | struct epoll_event __user *ev; | |
4100 | ||
4101 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4102 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4103 | return -EFAULT; | |
4104 | } | |
4105 | ||
4106 | return 0; | |
4107 | #else | |
4108 | return -EOPNOTSUPP; | |
4109 | #endif | |
4110 | } | |
4111 | ||
889fca73 | 4112 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4113 | { |
4114 | #if defined(CONFIG_EPOLL) | |
4115 | struct io_epoll *ie = &req->epoll; | |
4116 | int ret; | |
45d189c6 | 4117 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4118 | |
4119 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4120 | if (force_nonblock && ret == -EAGAIN) | |
4121 | return -EAGAIN; | |
4122 | ||
4123 | if (ret < 0) | |
93d2bcd2 | 4124 | req_set_fail(req); |
889fca73 | 4125 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4126 | return 0; |
4127 | #else | |
4128 | return -EOPNOTSUPP; | |
4129 | #endif | |
4130 | } | |
4131 | ||
c1ca757b JA |
4132 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4133 | { | |
4134 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4135 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
4136 | return -EINVAL; | |
3232dd02 PB |
4137 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4138 | return -EINVAL; | |
c1ca757b JA |
4139 | |
4140 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4141 | req->madvise.len = READ_ONCE(sqe->len); | |
4142 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4143 | return 0; | |
4144 | #else | |
4145 | return -EOPNOTSUPP; | |
4146 | #endif | |
4147 | } | |
4148 | ||
45d189c6 | 4149 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4150 | { |
4151 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4152 | struct io_madvise *ma = &req->madvise; | |
4153 | int ret; | |
4154 | ||
45d189c6 | 4155 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4156 | return -EAGAIN; |
4157 | ||
0726b01e | 4158 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4159 | if (ret < 0) |
93d2bcd2 | 4160 | req_set_fail(req); |
e1e16097 | 4161 | io_req_complete(req, ret); |
c1ca757b JA |
4162 | return 0; |
4163 | #else | |
4164 | return -EOPNOTSUPP; | |
4165 | #endif | |
4166 | } | |
4167 | ||
4840e418 JA |
4168 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4169 | { | |
4170 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
4171 | return -EINVAL; | |
3232dd02 PB |
4172 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4173 | return -EINVAL; | |
4840e418 JA |
4174 | |
4175 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4176 | req->fadvise.len = READ_ONCE(sqe->len); | |
4177 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4178 | return 0; | |
4179 | } | |
4180 | ||
45d189c6 | 4181 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4182 | { |
4183 | struct io_fadvise *fa = &req->fadvise; | |
4184 | int ret; | |
4185 | ||
45d189c6 | 4186 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4187 | switch (fa->advice) { |
4188 | case POSIX_FADV_NORMAL: | |
4189 | case POSIX_FADV_RANDOM: | |
4190 | case POSIX_FADV_SEQUENTIAL: | |
4191 | break; | |
4192 | default: | |
4193 | return -EAGAIN; | |
4194 | } | |
4195 | } | |
4840e418 JA |
4196 | |
4197 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4198 | if (ret < 0) | |
93d2bcd2 | 4199 | req_set_fail(req); |
0bdf3398 | 4200 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4201 | return 0; |
4202 | } | |
4203 | ||
eddc7ef5 JA |
4204 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4205 | { | |
2d74d042 | 4206 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4207 | return -EINVAL; |
eddc7ef5 JA |
4208 | if (sqe->ioprio || sqe->buf_index) |
4209 | return -EINVAL; | |
9c280f90 | 4210 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4211 | return -EBADF; |
eddc7ef5 | 4212 | |
1d9e1288 BM |
4213 | req->statx.dfd = READ_ONCE(sqe->fd); |
4214 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4215 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4216 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4217 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4218 | |
4219 | return 0; | |
4220 | } | |
4221 | ||
45d189c6 | 4222 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4223 | { |
1d9e1288 | 4224 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4225 | int ret; |
4226 | ||
59d70013 | 4227 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4228 | return -EAGAIN; |
4229 | ||
e62753e4 BM |
4230 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4231 | ctx->buffer); | |
eddc7ef5 | 4232 | |
eddc7ef5 | 4233 | if (ret < 0) |
93d2bcd2 | 4234 | req_set_fail(req); |
e1e16097 | 4235 | io_req_complete(req, ret); |
eddc7ef5 JA |
4236 | return 0; |
4237 | } | |
4238 | ||
b5dba59e JA |
4239 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4240 | { | |
14587a46 | 4241 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4242 | return -EINVAL; |
b5dba59e JA |
4243 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
4244 | sqe->rw_flags || sqe->buf_index) | |
4245 | return -EINVAL; | |
9c280f90 | 4246 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4247 | return -EBADF; |
b5dba59e JA |
4248 | |
4249 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4250 | return 0; |
b5dba59e JA |
4251 | } |
4252 | ||
889fca73 | 4253 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4254 | { |
9eac1904 | 4255 | struct files_struct *files = current->files; |
3af73b28 | 4256 | struct io_close *close = &req->close; |
9eac1904 | 4257 | struct fdtable *fdt; |
a1fde923 PB |
4258 | struct file *file = NULL; |
4259 | int ret = -EBADF; | |
b5dba59e | 4260 | |
9eac1904 JA |
4261 | spin_lock(&files->file_lock); |
4262 | fdt = files_fdtable(files); | |
4263 | if (close->fd >= fdt->max_fds) { | |
4264 | spin_unlock(&files->file_lock); | |
4265 | goto err; | |
4266 | } | |
4267 | file = fdt->fd[close->fd]; | |
a1fde923 | 4268 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4269 | spin_unlock(&files->file_lock); |
4270 | file = NULL; | |
4271 | goto err; | |
3af73b28 | 4272 | } |
b5dba59e JA |
4273 | |
4274 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4275 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4276 | spin_unlock(&files->file_lock); |
0bf0eefd | 4277 | return -EAGAIN; |
a2100672 | 4278 | } |
b5dba59e | 4279 | |
9eac1904 JA |
4280 | ret = __close_fd_get_file(close->fd, &file); |
4281 | spin_unlock(&files->file_lock); | |
4282 | if (ret < 0) { | |
4283 | if (ret == -ENOENT) | |
4284 | ret = -EBADF; | |
4285 | goto err; | |
4286 | } | |
4287 | ||
3af73b28 | 4288 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4289 | ret = filp_close(file, current->files); |
4290 | err: | |
3af73b28 | 4291 | if (ret < 0) |
93d2bcd2 | 4292 | req_set_fail(req); |
9eac1904 JA |
4293 | if (file) |
4294 | fput(file); | |
889fca73 | 4295 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4296 | return 0; |
b5dba59e JA |
4297 | } |
4298 | ||
1155c76a | 4299 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4300 | { |
4301 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4302 | |
5d17b4a4 JA |
4303 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4304 | return -EINVAL; | |
4305 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
4306 | return -EINVAL; | |
4307 | ||
8ed8d3c3 JA |
4308 | req->sync.off = READ_ONCE(sqe->off); |
4309 | req->sync.len = READ_ONCE(sqe->len); | |
4310 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4311 | return 0; |
4312 | } | |
4313 | ||
45d189c6 | 4314 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4315 | { |
8ed8d3c3 JA |
4316 | int ret; |
4317 | ||
ac45abc0 | 4318 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4319 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4320 | return -EAGAIN; |
4321 | ||
9adbd45d | 4322 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4323 | req->sync.flags); |
4324 | if (ret < 0) | |
93d2bcd2 | 4325 | req_set_fail(req); |
e1e16097 | 4326 | io_req_complete(req, ret); |
5d17b4a4 JA |
4327 | return 0; |
4328 | } | |
4329 | ||
469956e8 | 4330 | #if defined(CONFIG_NET) |
02d27d89 PB |
4331 | static int io_setup_async_msg(struct io_kiocb *req, |
4332 | struct io_async_msghdr *kmsg) | |
4333 | { | |
e8c2bc1f JA |
4334 | struct io_async_msghdr *async_msg = req->async_data; |
4335 | ||
4336 | if (async_msg) | |
02d27d89 | 4337 | return -EAGAIN; |
e8c2bc1f | 4338 | if (io_alloc_async_data(req)) { |
257e84a5 | 4339 | kfree(kmsg->free_iov); |
02d27d89 PB |
4340 | return -ENOMEM; |
4341 | } | |
e8c2bc1f | 4342 | async_msg = req->async_data; |
02d27d89 | 4343 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4344 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4345 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4346 | /* if were using fast_iov, set it to the new one */ |
4347 | if (!async_msg->free_iov) | |
4348 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4349 | ||
02d27d89 PB |
4350 | return -EAGAIN; |
4351 | } | |
4352 | ||
2ae523ed PB |
4353 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4354 | struct io_async_msghdr *iomsg) | |
4355 | { | |
2ae523ed | 4356 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4357 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4358 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4359 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4360 | } |
4361 | ||
93642ef8 PB |
4362 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4363 | { | |
4364 | int ret; | |
4365 | ||
93642ef8 PB |
4366 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4367 | if (!ret) | |
4368 | req->flags |= REQ_F_NEED_CLEANUP; | |
4369 | return ret; | |
4370 | } | |
4371 | ||
3529d8c2 | 4372 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4373 | { |
e47293fd | 4374 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4375 | |
d2b6f48b PB |
4376 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4377 | return -EINVAL; | |
4378 | ||
270a5940 | 4379 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4380 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4381 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4382 | if (sr->msg_flags & MSG_DONTWAIT) | |
4383 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4384 | |
d8768362 JA |
4385 | #ifdef CONFIG_COMPAT |
4386 | if (req->ctx->compat) | |
4387 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4388 | #endif | |
93642ef8 | 4389 | return 0; |
03b1230c JA |
4390 | } |
4391 | ||
889fca73 | 4392 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4393 | { |
6b754c8b | 4394 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4395 | struct socket *sock; |
7a7cacba | 4396 | unsigned flags; |
0031275d | 4397 | int min_ret = 0; |
0fa03c62 JA |
4398 | int ret; |
4399 | ||
dba4a925 | 4400 | sock = sock_from_file(req->file); |
7a7cacba | 4401 | if (unlikely(!sock)) |
dba4a925 | 4402 | return -ENOTSOCK; |
3529d8c2 | 4403 | |
257e84a5 PB |
4404 | kmsg = req->async_data; |
4405 | if (!kmsg) { | |
7a7cacba PB |
4406 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4407 | if (ret) | |
4408 | return ret; | |
4409 | kmsg = &iomsg; | |
0fa03c62 | 4410 | } |
0fa03c62 | 4411 | |
04411806 PB |
4412 | flags = req->sr_msg.msg_flags; |
4413 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4414 | flags |= MSG_DONTWAIT; |
0031275d SM |
4415 | if (flags & MSG_WAITALL) |
4416 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4417 | ||
7a7cacba | 4418 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4419 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4420 | return io_setup_async_msg(req, kmsg); |
4421 | if (ret == -ERESTARTSYS) | |
4422 | ret = -EINTR; | |
0fa03c62 | 4423 | |
257e84a5 PB |
4424 | /* fast path, check for non-NULL to avoid function call */ |
4425 | if (kmsg->free_iov) | |
4426 | kfree(kmsg->free_iov); | |
99bc4c38 | 4427 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4428 | if (ret < min_ret) |
93d2bcd2 | 4429 | req_set_fail(req); |
889fca73 | 4430 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4431 | return 0; |
03b1230c | 4432 | } |
aa1fa28f | 4433 | |
889fca73 | 4434 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4435 | { |
7a7cacba PB |
4436 | struct io_sr_msg *sr = &req->sr_msg; |
4437 | struct msghdr msg; | |
4438 | struct iovec iov; | |
fddaface | 4439 | struct socket *sock; |
7a7cacba | 4440 | unsigned flags; |
0031275d | 4441 | int min_ret = 0; |
fddaface JA |
4442 | int ret; |
4443 | ||
dba4a925 | 4444 | sock = sock_from_file(req->file); |
7a7cacba | 4445 | if (unlikely(!sock)) |
dba4a925 | 4446 | return -ENOTSOCK; |
fddaface | 4447 | |
7a7cacba PB |
4448 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4449 | if (unlikely(ret)) | |
14db8411 | 4450 | return ret; |
fddaface | 4451 | |
7a7cacba PB |
4452 | msg.msg_name = NULL; |
4453 | msg.msg_control = NULL; | |
4454 | msg.msg_controllen = 0; | |
4455 | msg.msg_namelen = 0; | |
fddaface | 4456 | |
04411806 PB |
4457 | flags = req->sr_msg.msg_flags; |
4458 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4459 | flags |= MSG_DONTWAIT; |
0031275d SM |
4460 | if (flags & MSG_WAITALL) |
4461 | min_ret = iov_iter_count(&msg.msg_iter); | |
4462 | ||
7a7cacba PB |
4463 | msg.msg_flags = flags; |
4464 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4465 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4466 | return -EAGAIN; |
4467 | if (ret == -ERESTARTSYS) | |
4468 | ret = -EINTR; | |
fddaface | 4469 | |
0031275d | 4470 | if (ret < min_ret) |
93d2bcd2 | 4471 | req_set_fail(req); |
889fca73 | 4472 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4473 | return 0; |
fddaface JA |
4474 | } |
4475 | ||
1400e697 PB |
4476 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4477 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4478 | { |
4479 | struct io_sr_msg *sr = &req->sr_msg; | |
4480 | struct iovec __user *uiov; | |
4481 | size_t iov_len; | |
4482 | int ret; | |
4483 | ||
1400e697 PB |
4484 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4485 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4486 | if (ret) |
4487 | return ret; | |
4488 | ||
4489 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4490 | if (iov_len > 1) | |
4491 | return -EINVAL; | |
5476dfed | 4492 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4493 | return -EFAULT; |
5476dfed | 4494 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4495 | iomsg->free_iov = NULL; |
52de1fe1 | 4496 | } else { |
257e84a5 | 4497 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4498 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4499 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4500 | false); |
52de1fe1 JA |
4501 | if (ret > 0) |
4502 | ret = 0; | |
4503 | } | |
4504 | ||
4505 | return ret; | |
4506 | } | |
4507 | ||
4508 | #ifdef CONFIG_COMPAT | |
4509 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4510 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4511 | { |
52de1fe1 JA |
4512 | struct io_sr_msg *sr = &req->sr_msg; |
4513 | struct compat_iovec __user *uiov; | |
4514 | compat_uptr_t ptr; | |
4515 | compat_size_t len; | |
4516 | int ret; | |
4517 | ||
4af3417a PB |
4518 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4519 | &ptr, &len); | |
52de1fe1 JA |
4520 | if (ret) |
4521 | return ret; | |
4522 | ||
4523 | uiov = compat_ptr(ptr); | |
4524 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4525 | compat_ssize_t clen; | |
4526 | ||
4527 | if (len > 1) | |
4528 | return -EINVAL; | |
4529 | if (!access_ok(uiov, sizeof(*uiov))) | |
4530 | return -EFAULT; | |
4531 | if (__get_user(clen, &uiov->iov_len)) | |
4532 | return -EFAULT; | |
4533 | if (clen < 0) | |
4534 | return -EINVAL; | |
2d280bc8 | 4535 | sr->len = clen; |
257e84a5 | 4536 | iomsg->free_iov = NULL; |
52de1fe1 | 4537 | } else { |
257e84a5 | 4538 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4539 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4540 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4541 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4542 | if (ret < 0) |
4543 | return ret; | |
4544 | } | |
4545 | ||
4546 | return 0; | |
4547 | } | |
4548 | #endif | |
4549 | ||
1400e697 PB |
4550 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4551 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4552 | { |
1400e697 | 4553 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4554 | |
4555 | #ifdef CONFIG_COMPAT | |
4556 | if (req->ctx->compat) | |
1400e697 | 4557 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4558 | #endif |
52de1fe1 | 4559 | |
1400e697 | 4560 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4561 | } |
4562 | ||
bcda7baa | 4563 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4564 | bool needs_lock) |
bcda7baa JA |
4565 | { |
4566 | struct io_sr_msg *sr = &req->sr_msg; | |
4567 | struct io_buffer *kbuf; | |
4568 | ||
bcda7baa JA |
4569 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4570 | if (IS_ERR(kbuf)) | |
4571 | return kbuf; | |
4572 | ||
4573 | sr->kbuf = kbuf; | |
4574 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4575 | return kbuf; |
fddaface JA |
4576 | } |
4577 | ||
7fbb1b54 PB |
4578 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4579 | { | |
4580 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4581 | } | |
4582 | ||
93642ef8 | 4583 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4584 | { |
99bc4c38 | 4585 | int ret; |
3529d8c2 | 4586 | |
93642ef8 PB |
4587 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4588 | if (!ret) | |
4589 | req->flags |= REQ_F_NEED_CLEANUP; | |
4590 | return ret; | |
4591 | } | |
4592 | ||
4593 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4594 | { | |
4595 | struct io_sr_msg *sr = &req->sr_msg; | |
4596 | ||
d2b6f48b PB |
4597 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4598 | return -EINVAL; | |
4599 | ||
270a5940 | 4600 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4601 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4602 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4603 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4604 | if (sr->msg_flags & MSG_DONTWAIT) | |
4605 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4606 | |
d8768362 JA |
4607 | #ifdef CONFIG_COMPAT |
4608 | if (req->ctx->compat) | |
4609 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4610 | #endif | |
93642ef8 | 4611 | return 0; |
aa1fa28f JA |
4612 | } |
4613 | ||
889fca73 | 4614 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4615 | { |
6b754c8b | 4616 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4617 | struct socket *sock; |
7fbb1b54 | 4618 | struct io_buffer *kbuf; |
7a7cacba | 4619 | unsigned flags; |
0031275d | 4620 | int min_ret = 0; |
52de1fe1 | 4621 | int ret, cflags = 0; |
45d189c6 | 4622 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4623 | |
dba4a925 | 4624 | sock = sock_from_file(req->file); |
7a7cacba | 4625 | if (unlikely(!sock)) |
dba4a925 | 4626 | return -ENOTSOCK; |
3529d8c2 | 4627 | |
257e84a5 PB |
4628 | kmsg = req->async_data; |
4629 | if (!kmsg) { | |
7a7cacba PB |
4630 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4631 | if (ret) | |
681fda8d | 4632 | return ret; |
7a7cacba PB |
4633 | kmsg = &iomsg; |
4634 | } | |
03b1230c | 4635 | |
bc02ef33 | 4636 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4637 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4638 | if (IS_ERR(kbuf)) |
52de1fe1 | 4639 | return PTR_ERR(kbuf); |
7a7cacba | 4640 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4641 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4642 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4643 | 1, req->sr_msg.len); |
4644 | } | |
52de1fe1 | 4645 | |
04411806 PB |
4646 | flags = req->sr_msg.msg_flags; |
4647 | if (force_nonblock) | |
7a7cacba | 4648 | flags |= MSG_DONTWAIT; |
0031275d SM |
4649 | if (flags & MSG_WAITALL) |
4650 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4651 | ||
7a7cacba PB |
4652 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4653 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4654 | if (force_nonblock && ret == -EAGAIN) |
4655 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4656 | if (ret == -ERESTARTSYS) |
4657 | ret = -EINTR; | |
03b1230c | 4658 | |
7fbb1b54 PB |
4659 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4660 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4661 | /* fast path, check for non-NULL to avoid function call */ |
4662 | if (kmsg->free_iov) | |
4663 | kfree(kmsg->free_iov); | |
99bc4c38 | 4664 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4665 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4666 | req_set_fail(req); |
889fca73 | 4667 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4668 | return 0; |
0fa03c62 | 4669 | } |
5d17b4a4 | 4670 | |
889fca73 | 4671 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4672 | { |
6b754c8b | 4673 | struct io_buffer *kbuf; |
7a7cacba PB |
4674 | struct io_sr_msg *sr = &req->sr_msg; |
4675 | struct msghdr msg; | |
4676 | void __user *buf = sr->buf; | |
fddaface | 4677 | struct socket *sock; |
7a7cacba PB |
4678 | struct iovec iov; |
4679 | unsigned flags; | |
0031275d | 4680 | int min_ret = 0; |
bcda7baa | 4681 | int ret, cflags = 0; |
45d189c6 | 4682 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4683 | |
dba4a925 | 4684 | sock = sock_from_file(req->file); |
7a7cacba | 4685 | if (unlikely(!sock)) |
dba4a925 | 4686 | return -ENOTSOCK; |
fddaface | 4687 | |
bc02ef33 | 4688 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4689 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4690 | if (IS_ERR(kbuf)) |
4691 | return PTR_ERR(kbuf); | |
7a7cacba | 4692 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4693 | } |
bcda7baa | 4694 | |
7a7cacba | 4695 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4696 | if (unlikely(ret)) |
4697 | goto out_free; | |
fddaface | 4698 | |
7a7cacba PB |
4699 | msg.msg_name = NULL; |
4700 | msg.msg_control = NULL; | |
4701 | msg.msg_controllen = 0; | |
4702 | msg.msg_namelen = 0; | |
4703 | msg.msg_iocb = NULL; | |
4704 | msg.msg_flags = 0; | |
fddaface | 4705 | |
04411806 PB |
4706 | flags = req->sr_msg.msg_flags; |
4707 | if (force_nonblock) | |
7a7cacba | 4708 | flags |= MSG_DONTWAIT; |
0031275d SM |
4709 | if (flags & MSG_WAITALL) |
4710 | min_ret = iov_iter_count(&msg.msg_iter); | |
4711 | ||
7a7cacba PB |
4712 | ret = sock_recvmsg(sock, &msg, flags); |
4713 | if (force_nonblock && ret == -EAGAIN) | |
4714 | return -EAGAIN; | |
4715 | if (ret == -ERESTARTSYS) | |
4716 | ret = -EINTR; | |
14c32eee | 4717 | out_free: |
7fbb1b54 PB |
4718 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4719 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4720 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4721 | req_set_fail(req); |
889fca73 | 4722 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4723 | return 0; |
fddaface JA |
4724 | } |
4725 | ||
3529d8c2 | 4726 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4727 | { |
8ed8d3c3 JA |
4728 | struct io_accept *accept = &req->accept; |
4729 | ||
14587a46 | 4730 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4731 | return -EINVAL; |
8042d6ce | 4732 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4733 | return -EINVAL; |
4734 | ||
d55e5f5b JA |
4735 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4736 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4737 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4738 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4739 | return 0; |
8ed8d3c3 | 4740 | } |
17f2fe35 | 4741 | |
889fca73 | 4742 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4743 | { |
4744 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4745 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4746 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4747 | int ret; |
4748 | ||
e697deed JX |
4749 | if (req->file->f_flags & O_NONBLOCK) |
4750 | req->flags |= REQ_F_NOWAIT; | |
4751 | ||
8ed8d3c3 | 4752 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4753 | accept->addr_len, accept->flags, |
4754 | accept->nofile); | |
8ed8d3c3 | 4755 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4756 | return -EAGAIN; |
ac45abc0 PB |
4757 | if (ret < 0) { |
4758 | if (ret == -ERESTARTSYS) | |
4759 | ret = -EINTR; | |
93d2bcd2 | 4760 | req_set_fail(req); |
ac45abc0 | 4761 | } |
889fca73 | 4762 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4763 | return 0; |
8ed8d3c3 JA |
4764 | } |
4765 | ||
93642ef8 PB |
4766 | static int io_connect_prep_async(struct io_kiocb *req) |
4767 | { | |
4768 | struct io_async_connect *io = req->async_data; | |
4769 | struct io_connect *conn = &req->connect; | |
4770 | ||
4771 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4772 | } | |
4773 | ||
3529d8c2 | 4774 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4775 | { |
3529d8c2 | 4776 | struct io_connect *conn = &req->connect; |
f499a021 | 4777 | |
14587a46 | 4778 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 JA |
4779 | return -EINVAL; |
4780 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
4781 | return -EINVAL; | |
4782 | ||
3529d8c2 JA |
4783 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4784 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4785 | return 0; |
f499a021 JA |
4786 | } |
4787 | ||
889fca73 | 4788 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4789 | { |
e8c2bc1f | 4790 | struct io_async_connect __io, *io; |
f8e85cf2 | 4791 | unsigned file_flags; |
3fbb51c1 | 4792 | int ret; |
45d189c6 | 4793 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4794 | |
e8c2bc1f JA |
4795 | if (req->async_data) { |
4796 | io = req->async_data; | |
f499a021 | 4797 | } else { |
3529d8c2 JA |
4798 | ret = move_addr_to_kernel(req->connect.addr, |
4799 | req->connect.addr_len, | |
e8c2bc1f | 4800 | &__io.address); |
f499a021 JA |
4801 | if (ret) |
4802 | goto out; | |
4803 | io = &__io; | |
4804 | } | |
4805 | ||
3fbb51c1 JA |
4806 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4807 | ||
e8c2bc1f | 4808 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4809 | req->connect.addr_len, file_flags); |
87f80d62 | 4810 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4811 | if (req->async_data) |
b7bb4f7d | 4812 | return -EAGAIN; |
e8c2bc1f | 4813 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4814 | ret = -ENOMEM; |
4815 | goto out; | |
4816 | } | |
e8c2bc1f | 4817 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4818 | return -EAGAIN; |
f499a021 | 4819 | } |
f8e85cf2 JA |
4820 | if (ret == -ERESTARTSYS) |
4821 | ret = -EINTR; | |
f499a021 | 4822 | out: |
4e88d6e7 | 4823 | if (ret < 0) |
93d2bcd2 | 4824 | req_set_fail(req); |
889fca73 | 4825 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4826 | return 0; |
469956e8 Y |
4827 | } |
4828 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4829 | #define IO_NETOP_FN(op) \ |
4830 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4831 | { \ | |
4832 | return -EOPNOTSUPP; \ | |
4833 | } | |
4834 | ||
4835 | #define IO_NETOP_PREP(op) \ | |
4836 | IO_NETOP_FN(op) \ | |
4837 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4838 | { \ | |
4839 | return -EOPNOTSUPP; \ | |
4840 | } \ | |
4841 | ||
4842 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4843 | IO_NETOP_PREP(op) \ | |
4844 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4845 | { \ | |
4846 | return -EOPNOTSUPP; \ | |
4847 | } | |
4848 | ||
4849 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4850 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4851 | IO_NETOP_PREP_ASYNC(connect); | |
4852 | IO_NETOP_PREP(accept); | |
4853 | IO_NETOP_FN(send); | |
4854 | IO_NETOP_FN(recv); | |
469956e8 | 4855 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4856 | |
d7718a9d JA |
4857 | struct io_poll_table { |
4858 | struct poll_table_struct pt; | |
4859 | struct io_kiocb *req; | |
68b11e8b | 4860 | int nr_entries; |
d7718a9d JA |
4861 | int error; |
4862 | }; | |
ce593a6c | 4863 | |
d7718a9d | 4864 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 4865 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 4866 | { |
d7718a9d JA |
4867 | /* for instances that support it check for an event match first: */ |
4868 | if (mask && !(mask & poll->events)) | |
4869 | return 0; | |
4870 | ||
4871 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4872 | ||
4873 | list_del_init(&poll->wait.entry); | |
4874 | ||
d7718a9d | 4875 | req->result = mask; |
5b0a6acc | 4876 | req->io_task_work.func = func; |
6d816e08 | 4877 | |
d7718a9d | 4878 | /* |
e3aabf95 JA |
4879 | * If this fails, then the task is exiting. When a task exits, the |
4880 | * work gets canceled, so just cancel this request as well instead | |
4881 | * of executing it. We can't safely execute it anyway, as we may not | |
4882 | * have the needed state needed for it anyway. | |
d7718a9d | 4883 | */ |
e09ee510 | 4884 | io_req_task_work_add(req); |
d7718a9d JA |
4885 | return 1; |
4886 | } | |
4887 | ||
74ce6ce4 JA |
4888 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4889 | __acquires(&req->ctx->completion_lock) | |
4890 | { | |
4891 | struct io_ring_ctx *ctx = req->ctx; | |
4892 | ||
e09ee510 PB |
4893 | if (unlikely(req->task->flags & PF_EXITING)) |
4894 | WRITE_ONCE(poll->canceled, true); | |
4895 | ||
74ce6ce4 JA |
4896 | if (!req->result && !READ_ONCE(poll->canceled)) { |
4897 | struct poll_table_struct pt = { ._key = poll->events }; | |
4898 | ||
4899 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4900 | } | |
4901 | ||
79ebeaee | 4902 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
4903 | if (!req->result && !READ_ONCE(poll->canceled)) { |
4904 | add_wait_queue(poll->head, &poll->wait); | |
4905 | return true; | |
4906 | } | |
4907 | ||
4908 | return false; | |
4909 | } | |
4910 | ||
d4e7cd36 | 4911 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4912 | { |
e8c2bc1f | 4913 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4914 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4915 | return req->async_data; |
d4e7cd36 JA |
4916 | return req->apoll->double_poll; |
4917 | } | |
4918 | ||
4919 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4920 | { | |
4921 | if (req->opcode == IORING_OP_POLL_ADD) | |
4922 | return &req->poll; | |
4923 | return &req->apoll->poll; | |
4924 | } | |
4925 | ||
4926 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 4927 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
4928 | { |
4929 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4930 | |
4931 | lockdep_assert_held(&req->ctx->completion_lock); | |
4932 | ||
4933 | if (poll && poll->head) { | |
4934 | struct wait_queue_head *head = poll->head; | |
4935 | ||
79ebeaee | 4936 | spin_lock_irq(&head->lock); |
18bceab1 JA |
4937 | list_del_init(&poll->wait.entry); |
4938 | if (poll->wait.private) | |
de9b4cca | 4939 | req_ref_put(req); |
18bceab1 | 4940 | poll->head = NULL; |
79ebeaee | 4941 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
4942 | } |
4943 | } | |
4944 | ||
e27414be | 4945 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 4946 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
4947 | { |
4948 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 4949 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 4950 | int error; |
18bceab1 | 4951 | |
e27414be | 4952 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 4953 | error = -ECANCELED; |
88e41cf9 | 4954 | req->poll.events |= EPOLLONESHOT; |
e27414be | 4955 | } else { |
5082620f | 4956 | error = mangle_poll(mask); |
e27414be | 4957 | } |
b69de288 JA |
4958 | if (req->poll.events & EPOLLONESHOT) |
4959 | flags = 0; | |
d4d19c19 | 4960 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
88e41cf9 JA |
4961 | req->poll.done = true; |
4962 | flags = 0; | |
4963 | } | |
7b289c38 HX |
4964 | if (flags & IORING_CQE_F_MORE) |
4965 | ctx->cq_extra++; | |
18bceab1 | 4966 | |
18bceab1 | 4967 | io_commit_cqring(ctx); |
88e41cf9 | 4968 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
4969 | } |
4970 | ||
5b0a6acc | 4971 | static void io_poll_task_func(struct io_kiocb *req) |
18bceab1 JA |
4972 | { |
4973 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 4974 | struct io_kiocb *nxt; |
18bceab1 JA |
4975 | |
4976 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 4977 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 4978 | } else { |
f40b964a | 4979 | bool done; |
18bceab1 | 4980 | |
e27414be | 4981 | done = io_poll_complete(req, req->result); |
88e41cf9 | 4982 | if (done) { |
a890d01e | 4983 | io_poll_remove_double(req); |
88e41cf9 | 4984 | hash_del(&req->hash_node); |
f40b964a | 4985 | } else { |
88e41cf9 JA |
4986 | req->result = 0; |
4987 | add_wait_queue(req->poll.head, &req->poll.wait); | |
4988 | } | |
79ebeaee | 4989 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 4990 | io_cqring_ev_posted(ctx); |
18bceab1 | 4991 | |
88e41cf9 JA |
4992 | if (done) { |
4993 | nxt = io_put_req_find_next(req); | |
4994 | if (nxt) | |
5b0a6acc | 4995 | io_req_task_submit(nxt); |
88e41cf9 | 4996 | } |
dd221f46 | 4997 | } |
18bceab1 JA |
4998 | } |
4999 | ||
5000 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5001 | int sync, void *key) | |
5002 | { | |
5003 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5004 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5005 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5006 | unsigned long flags; |
18bceab1 JA |
5007 | |
5008 | /* for instances that support it check for an event match first: */ | |
5009 | if (mask && !(mask & poll->events)) | |
5010 | return 0; | |
88e41cf9 JA |
5011 | if (!(poll->events & EPOLLONESHOT)) |
5012 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5013 | |
8706e04e JA |
5014 | list_del_init(&wait->entry); |
5015 | ||
9ce85ef2 | 5016 | if (poll->head) { |
18bceab1 JA |
5017 | bool done; |
5018 | ||
79ebeaee | 5019 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5020 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5021 | if (!done) |
807abcb0 | 5022 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5023 | /* make sure double remove sees this as being gone */ |
5024 | wait->private = NULL; | |
79ebeaee | 5025 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5026 | if (!done) { |
5027 | /* use wait func handler, so it matches the rq type */ | |
5028 | poll->wait.func(&poll->wait, mode, sync, key); | |
5029 | } | |
18bceab1 | 5030 | } |
de9b4cca | 5031 | req_ref_put(req); |
18bceab1 JA |
5032 | return 1; |
5033 | } | |
5034 | ||
5035 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5036 | wait_queue_func_t wake_func) | |
5037 | { | |
5038 | poll->head = NULL; | |
5039 | poll->done = false; | |
5040 | poll->canceled = false; | |
464dca61 JA |
5041 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5042 | /* mask in events that we always want/need */ | |
5043 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5044 | INIT_LIST_HEAD(&poll->wait.entry); |
5045 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5046 | } | |
5047 | ||
5048 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5049 | struct wait_queue_head *head, |
5050 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5051 | { |
5052 | struct io_kiocb *req = pt->req; | |
5053 | ||
5054 | /* | |
68b11e8b PB |
5055 | * The file being polled uses multiple waitqueues for poll handling |
5056 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5057 | * if this happens. | |
18bceab1 | 5058 | */ |
68b11e8b | 5059 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5060 | struct io_poll_iocb *poll_one = poll; |
5061 | ||
18bceab1 | 5062 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5063 | if (*poll_ptr) { |
18bceab1 JA |
5064 | pt->error = -EINVAL; |
5065 | return; | |
5066 | } | |
ea6a693d JA |
5067 | /* |
5068 | * Can't handle multishot for double wait for now, turn it | |
5069 | * into one-shot mode. | |
5070 | */ | |
7a274727 PB |
5071 | if (!(poll_one->events & EPOLLONESHOT)) |
5072 | poll_one->events |= EPOLLONESHOT; | |
1c3b3e65 | 5073 | /* double add on the same waitqueue head, ignore */ |
7a274727 | 5074 | if (poll_one->head == head) |
1c3b3e65 | 5075 | return; |
18bceab1 JA |
5076 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5077 | if (!poll) { | |
5078 | pt->error = -ENOMEM; | |
5079 | return; | |
5080 | } | |
58852d4d | 5081 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5082 | req_ref_get(req); |
18bceab1 | 5083 | poll->wait.private = req; |
807abcb0 | 5084 | *poll_ptr = poll; |
18bceab1 JA |
5085 | } |
5086 | ||
68b11e8b | 5087 | pt->nr_entries++; |
18bceab1 | 5088 | poll->head = head; |
a31eb4a2 JX |
5089 | |
5090 | if (poll->events & EPOLLEXCLUSIVE) | |
5091 | add_wait_queue_exclusive(head, &poll->wait); | |
5092 | else | |
5093 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5094 | } |
5095 | ||
5096 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5097 | struct poll_table_struct *p) | |
5098 | { | |
5099 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5100 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5101 | |
807abcb0 | 5102 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5103 | } |
5104 | ||
5b0a6acc | 5105 | static void io_async_task_func(struct io_kiocb *req) |
d7718a9d | 5106 | { |
d7718a9d JA |
5107 | struct async_poll *apoll = req->apoll; |
5108 | struct io_ring_ctx *ctx = req->ctx; | |
5109 | ||
236daeae | 5110 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5111 | |
74ce6ce4 | 5112 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5113 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5114 | return; |
d7718a9d JA |
5115 | } |
5116 | ||
0ea13b44 | 5117 | hash_del(&req->hash_node); |
d4e7cd36 | 5118 | io_poll_remove_double(req); |
79ebeaee | 5119 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5120 | |
0be0b0e3 | 5121 | if (!READ_ONCE(apoll->poll.canceled)) |
5b0a6acc | 5122 | io_req_task_submit(req); |
0be0b0e3 | 5123 | else |
2593553a | 5124 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5125 | } |
5126 | ||
5127 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5128 | void *key) | |
5129 | { | |
5130 | struct io_kiocb *req = wait->private; | |
5131 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5132 | ||
5133 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5134 | key_to_poll(key)); | |
5135 | ||
5136 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5137 | } | |
5138 | ||
5139 | static void io_poll_req_insert(struct io_kiocb *req) | |
5140 | { | |
5141 | struct io_ring_ctx *ctx = req->ctx; | |
5142 | struct hlist_head *list; | |
5143 | ||
5144 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5145 | hlist_add_head(&req->hash_node, list); | |
5146 | } | |
5147 | ||
5148 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5149 | struct io_poll_iocb *poll, | |
5150 | struct io_poll_table *ipt, __poll_t mask, | |
5151 | wait_queue_func_t wake_func) | |
5152 | __acquires(&ctx->completion_lock) | |
5153 | { | |
5154 | struct io_ring_ctx *ctx = req->ctx; | |
5155 | bool cancel = false; | |
5156 | ||
4d52f338 | 5157 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5158 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5159 | poll->file = req->file; |
18bceab1 | 5160 | poll->wait.private = req; |
d7718a9d JA |
5161 | |
5162 | ipt->pt._key = mask; | |
5163 | ipt->req = req; | |
68b11e8b PB |
5164 | ipt->error = 0; |
5165 | ipt->nr_entries = 0; | |
d7718a9d | 5166 | |
d7718a9d | 5167 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5168 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5169 | ipt->error = -EINVAL; | |
d7718a9d | 5170 | |
79ebeaee | 5171 | spin_lock(&ctx->completion_lock); |
a890d01e | 5172 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5173 | io_poll_remove_double(req); |
d7718a9d | 5174 | if (likely(poll->head)) { |
79ebeaee | 5175 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5176 | if (unlikely(list_empty(&poll->wait.entry))) { |
5177 | if (ipt->error) | |
5178 | cancel = true; | |
5179 | ipt->error = 0; | |
5180 | mask = 0; | |
5181 | } | |
88e41cf9 | 5182 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5183 | list_del_init(&poll->wait.entry); |
5184 | else if (cancel) | |
5185 | WRITE_ONCE(poll->canceled, true); | |
5186 | else if (!poll->done) /* actually waiting for an event */ | |
5187 | io_poll_req_insert(req); | |
79ebeaee | 5188 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5189 | } |
5190 | ||
5191 | return mask; | |
5192 | } | |
5193 | ||
59b735ae OL |
5194 | enum { |
5195 | IO_APOLL_OK, | |
5196 | IO_APOLL_ABORTED, | |
5197 | IO_APOLL_READY | |
5198 | }; | |
5199 | ||
5200 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5201 | { |
5202 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5203 | struct io_ring_ctx *ctx = req->ctx; | |
5204 | struct async_poll *apoll; | |
5205 | struct io_poll_table ipt; | |
b2d9c3da | 5206 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5207 | int rw; |
d7718a9d JA |
5208 | |
5209 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5210 | return IO_APOLL_ABORTED; |
24c74678 | 5211 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5212 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5213 | if (!def->pollin && !def->pollout) |
5214 | return IO_APOLL_ABORTED; | |
5215 | ||
5216 | if (def->pollin) { | |
9dab14b8 | 5217 | rw = READ; |
b2d9c3da PB |
5218 | mask |= POLLIN | POLLRDNORM; |
5219 | ||
5220 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5221 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5222 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5223 | mask &= ~POLLIN; | |
5224 | } else { | |
9dab14b8 | 5225 | rw = WRITE; |
b2d9c3da PB |
5226 | mask |= POLLOUT | POLLWRNORM; |
5227 | } | |
5228 | ||
9dab14b8 | 5229 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5230 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5231 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5232 | |
5233 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5234 | if (unlikely(!apoll)) | |
59b735ae | 5235 | return IO_APOLL_ABORTED; |
807abcb0 | 5236 | apoll->double_poll = NULL; |
d7718a9d | 5237 | req->apoll = apoll; |
b2d9c3da | 5238 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5239 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5240 | io_req_set_refcount(req); |
d7718a9d JA |
5241 | |
5242 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5243 | io_async_wake); | |
79ebeaee | 5244 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5245 | if (ret || ipt.error) |
5246 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5247 | ||
236daeae OL |
5248 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5249 | mask, apoll->poll.events); | |
59b735ae | 5250 | return IO_APOLL_OK; |
d7718a9d JA |
5251 | } |
5252 | ||
5253 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5254 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5255 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5256 | { |
b41e9852 | 5257 | bool do_complete = false; |
221c5eb2 | 5258 | |
5082620f JA |
5259 | if (!poll->head) |
5260 | return false; | |
79ebeaee | 5261 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5262 | if (do_cancel) |
5263 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5264 | if (!list_empty(&poll->wait.entry)) { |
5265 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5266 | do_complete = true; |
221c5eb2 | 5267 | } |
79ebeaee | 5268 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5269 | hash_del(&req->hash_node); |
d7718a9d JA |
5270 | return do_complete; |
5271 | } | |
5272 | ||
5d709043 | 5273 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5274 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5275 | { |
5276 | bool do_complete; | |
5277 | ||
d4e7cd36 | 5278 | io_poll_remove_double(req); |
e31001a3 | 5279 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5280 | |
b41e9852 | 5281 | if (do_complete) { |
d4d19c19 | 5282 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5283 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5284 | req_set_fail(req); |
91c2f697 | 5285 | io_put_req_deferred(req); |
5d709043 | 5286 | } |
b41e9852 | 5287 | return do_complete; |
221c5eb2 JA |
5288 | } |
5289 | ||
76e1b642 JA |
5290 | /* |
5291 | * Returns true if we found and killed one or more poll requests | |
5292 | */ | |
6b81928d | 5293 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5294 | bool cancel_all) |
221c5eb2 | 5295 | { |
78076bb6 | 5296 | struct hlist_node *tmp; |
221c5eb2 | 5297 | struct io_kiocb *req; |
8e2e1faf | 5298 | int posted = 0, i; |
221c5eb2 | 5299 | |
79ebeaee | 5300 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5301 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5302 | struct hlist_head *list; | |
5303 | ||
5304 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5305 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5306 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5307 | posted += io_poll_remove_one(req); |
5308 | } | |
221c5eb2 | 5309 | } |
79ebeaee | 5310 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5311 | |
8e2e1faf JA |
5312 | if (posted) |
5313 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5314 | |
5315 | return posted != 0; | |
221c5eb2 JA |
5316 | } |
5317 | ||
9ba5fac8 PB |
5318 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5319 | bool poll_only) | |
e07785b0 | 5320 | __must_hold(&ctx->completion_lock) |
47f46768 | 5321 | { |
78076bb6 | 5322 | struct hlist_head *list; |
47f46768 JA |
5323 | struct io_kiocb *req; |
5324 | ||
78076bb6 JA |
5325 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5326 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5327 | if (sqe_addr != req->user_data) |
5328 | continue; | |
9ba5fac8 PB |
5329 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5330 | continue; | |
b2cb805f | 5331 | return req; |
47f46768 | 5332 | } |
b2cb805f JA |
5333 | return NULL; |
5334 | } | |
5335 | ||
9ba5fac8 PB |
5336 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5337 | bool poll_only) | |
e07785b0 | 5338 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5339 | { |
5340 | struct io_kiocb *req; | |
5341 | ||
9ba5fac8 | 5342 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5343 | if (!req) |
5344 | return -ENOENT; | |
5345 | if (io_poll_remove_one(req)) | |
5346 | return 0; | |
5347 | ||
5348 | return -EALREADY; | |
47f46768 JA |
5349 | } |
5350 | ||
9096af3e PB |
5351 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5352 | unsigned int flags) | |
5353 | { | |
5354 | u32 events; | |
47f46768 | 5355 | |
9096af3e PB |
5356 | events = READ_ONCE(sqe->poll32_events); |
5357 | #ifdef __BIG_ENDIAN | |
5358 | events = swahw32(events); | |
5359 | #endif | |
5360 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5361 | events |= EPOLLONESHOT; | |
5362 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5363 | } |
5364 | ||
c5de0036 | 5365 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5366 | const struct io_uring_sqe *sqe) |
0969e783 | 5367 | { |
c5de0036 PB |
5368 | struct io_poll_update *upd = &req->poll_update; |
5369 | u32 flags; | |
5370 | ||
0969e783 JA |
5371 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5372 | return -EINVAL; | |
c5de0036 PB |
5373 | if (sqe->ioprio || sqe->buf_index) |
5374 | return -EINVAL; | |
5375 | flags = READ_ONCE(sqe->len); | |
5376 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5377 | IORING_POLL_ADD_MULTI)) | |
5378 | return -EINVAL; | |
5379 | /* meaningless without update */ | |
5380 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5381 | return -EINVAL; |
5382 | ||
c5de0036 PB |
5383 | upd->old_user_data = READ_ONCE(sqe->addr); |
5384 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5385 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5386 | |
c5de0036 PB |
5387 | upd->new_user_data = READ_ONCE(sqe->off); |
5388 | if (!upd->update_user_data && upd->new_user_data) | |
5389 | return -EINVAL; | |
5390 | if (upd->update_events) | |
5391 | upd->events = io_poll_parse_events(sqe, flags); | |
5392 | else if (sqe->poll32_events) | |
5393 | return -EINVAL; | |
221c5eb2 | 5394 | |
221c5eb2 JA |
5395 | return 0; |
5396 | } | |
5397 | ||
221c5eb2 JA |
5398 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5399 | void *key) | |
5400 | { | |
c2f2eb7d JA |
5401 | struct io_kiocb *req = wait->private; |
5402 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5403 | |
d7718a9d | 5404 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5405 | } |
5406 | ||
221c5eb2 JA |
5407 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5408 | struct poll_table_struct *p) | |
5409 | { | |
5410 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5411 | ||
e8c2bc1f | 5412 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5413 | } |
5414 | ||
3529d8c2 | 5415 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5416 | { |
5417 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5418 | u32 flags; |
221c5eb2 JA |
5419 | |
5420 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5421 | return -EINVAL; | |
c5de0036 | 5422 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5423 | return -EINVAL; |
5424 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5425 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5426 | return -EINVAL; |
5427 | ||
48dcd38d | 5428 | io_req_set_refcount(req); |
c5de0036 | 5429 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5430 | return 0; |
5431 | } | |
5432 | ||
61e98203 | 5433 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5434 | { |
5435 | struct io_poll_iocb *poll = &req->poll; | |
5436 | struct io_ring_ctx *ctx = req->ctx; | |
5437 | struct io_poll_table ipt; | |
0969e783 | 5438 | __poll_t mask; |
0969e783 | 5439 | |
d7718a9d | 5440 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5441 | |
d7718a9d JA |
5442 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5443 | io_poll_wake); | |
221c5eb2 | 5444 | |
8c838788 | 5445 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5446 | ipt.error = 0; |
e27414be | 5447 | io_poll_complete(req, mask); |
221c5eb2 | 5448 | } |
79ebeaee | 5449 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5450 | |
8c838788 JA |
5451 | if (mask) { |
5452 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5453 | if (poll->events & EPOLLONESHOT) |
5454 | io_put_req(req); | |
221c5eb2 | 5455 | } |
8c838788 | 5456 | return ipt.error; |
221c5eb2 JA |
5457 | } |
5458 | ||
c5de0036 | 5459 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5460 | { |
5461 | struct io_ring_ctx *ctx = req->ctx; | |
5462 | struct io_kiocb *preq; | |
cb3b200e | 5463 | bool completing; |
b69de288 JA |
5464 | int ret; |
5465 | ||
79ebeaee | 5466 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5467 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5468 | if (!preq) { |
5469 | ret = -ENOENT; | |
5470 | goto err; | |
b69de288 | 5471 | } |
cb3b200e | 5472 | |
c5de0036 PB |
5473 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5474 | completing = true; | |
5475 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5476 | goto err; | |
5477 | } | |
5478 | ||
cb3b200e JA |
5479 | /* |
5480 | * Don't allow racy completion with singleshot, as we cannot safely | |
5481 | * update those. For multishot, if we're racing with completion, just | |
5482 | * let completion re-add it. | |
5483 | */ | |
5484 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5485 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5486 | ret = -EALREADY; | |
5487 | goto err; | |
b69de288 JA |
5488 | } |
5489 | /* we now have a detached poll request. reissue. */ | |
5490 | ret = 0; | |
5491 | err: | |
b69de288 | 5492 | if (ret < 0) { |
79ebeaee | 5493 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5494 | req_set_fail(req); |
b69de288 JA |
5495 | io_req_complete(req, ret); |
5496 | return 0; | |
5497 | } | |
5498 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5499 | if (req->poll_update.update_events) { |
b69de288 | 5500 | preq->poll.events &= ~0xffff; |
9d805892 | 5501 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5502 | preq->poll.events |= IO_POLL_UNMASK; |
5503 | } | |
9d805892 PB |
5504 | if (req->poll_update.update_user_data) |
5505 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5506 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5507 | |
b69de288 JA |
5508 | /* complete update request, we're done with it */ |
5509 | io_req_complete(req, ret); | |
5510 | ||
cb3b200e | 5511 | if (!completing) { |
c5de0036 | 5512 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5513 | if (ret < 0) { |
93d2bcd2 | 5514 | req_set_fail(preq); |
cb3b200e JA |
5515 | io_req_complete(preq, ret); |
5516 | } | |
b69de288 JA |
5517 | } |
5518 | return 0; | |
5519 | } | |
5520 | ||
89850fce JA |
5521 | static void io_req_task_timeout(struct io_kiocb *req) |
5522 | { | |
5523 | struct io_ring_ctx *ctx = req->ctx; | |
5524 | ||
79ebeaee | 5525 | spin_lock(&ctx->completion_lock); |
89850fce JA |
5526 | io_cqring_fill_event(ctx, req->user_data, -ETIME, 0); |
5527 | io_commit_cqring(ctx); | |
79ebeaee | 5528 | spin_unlock(&ctx->completion_lock); |
89850fce JA |
5529 | |
5530 | io_cqring_ev_posted(ctx); | |
5531 | req_set_fail(req); | |
5532 | io_put_req(req); | |
5533 | } | |
5534 | ||
5262f567 JA |
5535 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5536 | { | |
ad8a48ac JA |
5537 | struct io_timeout_data *data = container_of(timer, |
5538 | struct io_timeout_data, timer); | |
5539 | struct io_kiocb *req = data->req; | |
5540 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5541 | unsigned long flags; |
5542 | ||
89850fce | 5543 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5544 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5545 | atomic_set(&req->ctx->cq_timeouts, |
5546 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5547 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5548 | |
89850fce JA |
5549 | req->io_task_work.func = io_req_task_timeout; |
5550 | io_req_task_work_add(req); | |
5262f567 JA |
5551 | return HRTIMER_NORESTART; |
5552 | } | |
5553 | ||
fbd15848 PB |
5554 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5555 | __u64 user_data) | |
89850fce | 5556 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5557 | { |
fbd15848 | 5558 | struct io_timeout_data *io; |
47f46768 | 5559 | struct io_kiocb *req; |
fd9c7bc5 | 5560 | bool found = false; |
f254ac04 | 5561 | |
135fcde8 | 5562 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5563 | found = user_data == req->user_data; |
5564 | if (found) | |
47f46768 | 5565 | break; |
47f46768 | 5566 | } |
fd9c7bc5 PB |
5567 | if (!found) |
5568 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5569 | |
5570 | io = req->async_data; | |
fd9c7bc5 | 5571 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5572 | return ERR_PTR(-EALREADY); |
a71976f3 | 5573 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5574 | return req; |
5575 | } | |
47f46768 | 5576 | |
fbd15848 | 5577 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
89850fce | 5578 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5579 | { |
5580 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5581 | ||
5582 | if (IS_ERR(req)) | |
5583 | return PTR_ERR(req); | |
f254ac04 | 5584 | |
93d2bcd2 | 5585 | req_set_fail(req); |
d4d19c19 | 5586 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5587 | io_put_req_deferred(req); |
f254ac04 JA |
5588 | return 0; |
5589 | } | |
5590 | ||
9c8e11b3 PB |
5591 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5592 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5593 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5594 | { |
9c8e11b3 PB |
5595 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5596 | struct io_timeout_data *data; | |
47f46768 | 5597 | |
9c8e11b3 PB |
5598 | if (IS_ERR(req)) |
5599 | return PTR_ERR(req); | |
47f46768 | 5600 | |
9c8e11b3 PB |
5601 | req->timeout.off = 0; /* noseq */ |
5602 | data = req->async_data; | |
5603 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5604 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5605 | data->timer.function = io_timeout_fn; | |
5606 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5607 | return 0; | |
47f46768 JA |
5608 | } |
5609 | ||
3529d8c2 JA |
5610 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5611 | const struct io_uring_sqe *sqe) | |
b29472ee | 5612 | { |
9c8e11b3 PB |
5613 | struct io_timeout_rem *tr = &req->timeout_rem; |
5614 | ||
b29472ee JA |
5615 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5616 | return -EINVAL; | |
61710e43 DA |
5617 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5618 | return -EINVAL; | |
9c8e11b3 | 5619 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
b29472ee JA |
5620 | return -EINVAL; |
5621 | ||
9c8e11b3 PB |
5622 | tr->addr = READ_ONCE(sqe->addr); |
5623 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5624 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5625 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5626 | return -EINVAL; | |
5627 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5628 | return -EFAULT; | |
5629 | } else if (tr->flags) { | |
5630 | /* timeout removal doesn't support flags */ | |
b29472ee | 5631 | return -EINVAL; |
9c8e11b3 | 5632 | } |
b29472ee | 5633 | |
b29472ee JA |
5634 | return 0; |
5635 | } | |
5636 | ||
8662daec PB |
5637 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5638 | { | |
5639 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5640 | : HRTIMER_MODE_REL; | |
5641 | } | |
5642 | ||
11365043 JA |
5643 | /* |
5644 | * Remove or update an existing timeout command | |
5645 | */ | |
61e98203 | 5646 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5647 | { |
9c8e11b3 | 5648 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5649 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5650 | int ret; |
11365043 | 5651 | |
89850fce | 5652 | spin_lock_irq(&ctx->timeout_lock); |
8662daec | 5653 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
9c8e11b3 | 5654 | ret = io_timeout_cancel(ctx, tr->addr); |
8662daec PB |
5655 | else |
5656 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, | |
5657 | io_translate_timeout_mode(tr->flags)); | |
89850fce | 5658 | spin_unlock_irq(&ctx->timeout_lock); |
11365043 | 5659 | |
79ebeaee | 5660 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 5661 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
11365043 | 5662 | io_commit_cqring(ctx); |
79ebeaee | 5663 | spin_unlock(&ctx->completion_lock); |
5262f567 | 5664 | io_cqring_ev_posted(ctx); |
4e88d6e7 | 5665 | if (ret < 0) |
93d2bcd2 | 5666 | req_set_fail(req); |
ec9c02ad | 5667 | io_put_req(req); |
11365043 | 5668 | return 0; |
5262f567 JA |
5669 | } |
5670 | ||
3529d8c2 | 5671 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5672 | bool is_timeout_link) |
5262f567 | 5673 | { |
ad8a48ac | 5674 | struct io_timeout_data *data; |
a41525ab | 5675 | unsigned flags; |
56080b02 | 5676 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5677 | |
ad8a48ac | 5678 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5679 | return -EINVAL; |
ad8a48ac | 5680 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 5681 | return -EINVAL; |
56080b02 | 5682 | if (off && is_timeout_link) |
2d28390a | 5683 | return -EINVAL; |
a41525ab JA |
5684 | flags = READ_ONCE(sqe->timeout_flags); |
5685 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5686 | return -EINVAL; |
bdf20073 | 5687 | |
bfe68a22 | 5688 | req->timeout.off = off; |
f18ee4cf PB |
5689 | if (unlikely(off && !req->ctx->off_timeout_used)) |
5690 | req->ctx->off_timeout_used = true; | |
26a61679 | 5691 | |
e8c2bc1f | 5692 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5693 | return -ENOMEM; |
5694 | ||
e8c2bc1f | 5695 | data = req->async_data; |
ad8a48ac | 5696 | data->req = req; |
ad8a48ac JA |
5697 | |
5698 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5699 | return -EFAULT; |
5700 | ||
8662daec | 5701 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac JA |
5702 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
5703 | return 0; | |
5704 | } | |
5705 | ||
61e98203 | 5706 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5707 | { |
ad8a48ac | 5708 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5709 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5710 | struct list_head *entry; |
bfe68a22 | 5711 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5712 | |
89850fce | 5713 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 5714 | |
5262f567 JA |
5715 | /* |
5716 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5717 | * timeout event to be satisfied. If it isn't set, then this is |
5718 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5719 | */ |
8eb7e2d0 | 5720 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5721 | entry = ctx->timeout_list.prev; |
5722 | goto add; | |
5723 | } | |
5262f567 | 5724 | |
bfe68a22 PB |
5725 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5726 | req->timeout.target_seq = tail + off; | |
5262f567 | 5727 | |
f010505b MDG |
5728 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5729 | * This is safe because ->completion_lock is held, and submissions | |
5730 | * and completions are never mixed in the same ->completion_lock section. | |
5731 | */ | |
5732 | ctx->cq_last_tm_flush = tail; | |
5733 | ||
5262f567 JA |
5734 | /* |
5735 | * Insertion sort, ensuring the first entry in the list is always | |
5736 | * the one we need first. | |
5737 | */ | |
5262f567 | 5738 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5739 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5740 | timeout.list); | |
5262f567 | 5741 | |
8eb7e2d0 | 5742 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5743 | continue; |
bfe68a22 PB |
5744 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5745 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5746 | break; |
5747 | } | |
93bd25bb | 5748 | add: |
135fcde8 | 5749 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5750 | data->timer.function = io_timeout_fn; |
5751 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 5752 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
5753 | return 0; |
5754 | } | |
5262f567 | 5755 | |
f458dd84 PB |
5756 | struct io_cancel_data { |
5757 | struct io_ring_ctx *ctx; | |
5758 | u64 user_data; | |
5759 | }; | |
5760 | ||
62755e35 JA |
5761 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5762 | { | |
5763 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5764 | struct io_cancel_data *cd = data; |
62755e35 | 5765 | |
f458dd84 | 5766 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5767 | } |
5768 | ||
f458dd84 PB |
5769 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5770 | struct io_ring_ctx *ctx) | |
62755e35 | 5771 | { |
f458dd84 | 5772 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5773 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5774 | int ret = 0; |
5775 | ||
f458dd84 | 5776 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5777 | return -ENOENT; |
5778 | ||
f458dd84 | 5779 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5780 | switch (cancel_ret) { |
5781 | case IO_WQ_CANCEL_OK: | |
5782 | ret = 0; | |
5783 | break; | |
5784 | case IO_WQ_CANCEL_RUNNING: | |
5785 | ret = -EALREADY; | |
5786 | break; | |
5787 | case IO_WQ_CANCEL_NOTFOUND: | |
5788 | ret = -ENOENT; | |
5789 | break; | |
5790 | } | |
5791 | ||
e977d6d3 JA |
5792 | return ret; |
5793 | } | |
5794 | ||
47f46768 JA |
5795 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
5796 | struct io_kiocb *req, __u64 sqe_addr, | |
014db007 | 5797 | int success_ret) |
47f46768 | 5798 | { |
47f46768 JA |
5799 | int ret; |
5800 | ||
f458dd84 | 5801 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
79ebeaee | 5802 | spin_lock(&ctx->completion_lock); |
df9727af PB |
5803 | if (ret != -ENOENT) |
5804 | goto done; | |
79ebeaee | 5805 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 5806 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 5807 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 JA |
5808 | if (ret != -ENOENT) |
5809 | goto done; | |
9ba5fac8 | 5810 | ret = io_poll_cancel(ctx, sqe_addr, false); |
47f46768 | 5811 | done: |
b0dd8a41 JA |
5812 | if (!ret) |
5813 | ret = success_ret; | |
d4d19c19 | 5814 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
47f46768 | 5815 | io_commit_cqring(ctx); |
79ebeaee | 5816 | spin_unlock(&ctx->completion_lock); |
47f46768 JA |
5817 | io_cqring_ev_posted(ctx); |
5818 | ||
4e88d6e7 | 5819 | if (ret < 0) |
93d2bcd2 | 5820 | req_set_fail(req); |
47f46768 JA |
5821 | } |
5822 | ||
3529d8c2 JA |
5823 | static int io_async_cancel_prep(struct io_kiocb *req, |
5824 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5825 | { |
fbf23849 | 5826 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5827 | return -EINVAL; |
61710e43 DA |
5828 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5829 | return -EINVAL; | |
5830 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) | |
e977d6d3 JA |
5831 | return -EINVAL; |
5832 | ||
fbf23849 JA |
5833 | req->cancel.addr = READ_ONCE(sqe->addr); |
5834 | return 0; | |
5835 | } | |
5836 | ||
61e98203 | 5837 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5838 | { |
5839 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5840 | u64 sqe_addr = req->cancel.addr; |
5841 | struct io_tctx_node *node; | |
5842 | int ret; | |
5843 | ||
5844 | /* tasks should wait for their io-wq threads, so safe w/o sync */ | |
5845 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); | |
79ebeaee | 5846 | spin_lock(&ctx->completion_lock); |
58f99373 PB |
5847 | if (ret != -ENOENT) |
5848 | goto done; | |
79ebeaee | 5849 | spin_lock_irq(&ctx->timeout_lock); |
58f99373 | 5850 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 5851 | spin_unlock_irq(&ctx->timeout_lock); |
58f99373 PB |
5852 | if (ret != -ENOENT) |
5853 | goto done; | |
9ba5fac8 | 5854 | ret = io_poll_cancel(ctx, sqe_addr, false); |
58f99373 PB |
5855 | if (ret != -ENOENT) |
5856 | goto done; | |
79ebeaee | 5857 | spin_unlock(&ctx->completion_lock); |
58f99373 PB |
5858 | |
5859 | /* slow path, try all io-wq's */ | |
5860 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5861 | ret = -ENOENT; | |
5862 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
5863 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 5864 | |
58f99373 PB |
5865 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
5866 | if (ret != -ENOENT) | |
5867 | break; | |
5868 | } | |
5869 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5870 | ||
79ebeaee | 5871 | spin_lock(&ctx->completion_lock); |
58f99373 | 5872 | done: |
d4d19c19 | 5873 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
58f99373 | 5874 | io_commit_cqring(ctx); |
79ebeaee | 5875 | spin_unlock(&ctx->completion_lock); |
58f99373 PB |
5876 | io_cqring_ev_posted(ctx); |
5877 | ||
5878 | if (ret < 0) | |
93d2bcd2 | 5879 | req_set_fail(req); |
58f99373 | 5880 | io_put_req(req); |
5262f567 JA |
5881 | return 0; |
5882 | } | |
5883 | ||
269bbe5f | 5884 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5885 | const struct io_uring_sqe *sqe) |
5886 | { | |
61710e43 DA |
5887 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5888 | return -EINVAL; | |
5889 | if (sqe->ioprio || sqe->rw_flags) | |
05f3fb3c JA |
5890 | return -EINVAL; |
5891 | ||
269bbe5f BM |
5892 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5893 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5894 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5895 | return -EINVAL; |
269bbe5f | 5896 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5897 | return 0; |
5898 | } | |
5899 | ||
889fca73 | 5900 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5901 | { |
5902 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 5903 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 5904 | int ret; |
fbf23849 | 5905 | |
45d189c6 | 5906 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5907 | return -EAGAIN; |
05f3fb3c | 5908 | |
269bbe5f BM |
5909 | up.offset = req->rsrc_update.offset; |
5910 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
5911 | up.nr = 0; |
5912 | up.tags = 0; | |
615cee49 | 5913 | up.resv = 0; |
05f3fb3c JA |
5914 | |
5915 | mutex_lock(&ctx->uring_lock); | |
fdecb662 | 5916 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 5917 | &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5918 | mutex_unlock(&ctx->uring_lock); |
5919 | ||
5920 | if (ret < 0) | |
93d2bcd2 | 5921 | req_set_fail(req); |
889fca73 | 5922 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5923 | return 0; |
5924 | } | |
5925 | ||
bfe76559 | 5926 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5927 | { |
d625c6ee | 5928 | switch (req->opcode) { |
e781573e | 5929 | case IORING_OP_NOP: |
bfe76559 | 5930 | return 0; |
f67676d1 JA |
5931 | case IORING_OP_READV: |
5932 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5933 | case IORING_OP_READ: |
bfe76559 | 5934 | return io_read_prep(req, sqe); |
f67676d1 JA |
5935 | case IORING_OP_WRITEV: |
5936 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5937 | case IORING_OP_WRITE: |
bfe76559 | 5938 | return io_write_prep(req, sqe); |
0969e783 | 5939 | case IORING_OP_POLL_ADD: |
bfe76559 | 5940 | return io_poll_add_prep(req, sqe); |
0969e783 | 5941 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 5942 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 5943 | case IORING_OP_FSYNC: |
1155c76a | 5944 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5945 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5946 | return io_sfr_prep(req, sqe); |
03b1230c | 5947 | case IORING_OP_SENDMSG: |
fddaface | 5948 | case IORING_OP_SEND: |
bfe76559 | 5949 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5950 | case IORING_OP_RECVMSG: |
fddaface | 5951 | case IORING_OP_RECV: |
bfe76559 | 5952 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5953 | case IORING_OP_CONNECT: |
bfe76559 | 5954 | return io_connect_prep(req, sqe); |
2d28390a | 5955 | case IORING_OP_TIMEOUT: |
bfe76559 | 5956 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5957 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5958 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5959 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5960 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5961 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5962 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5963 | case IORING_OP_ACCEPT: |
bfe76559 | 5964 | return io_accept_prep(req, sqe); |
d63d1b5e | 5965 | case IORING_OP_FALLOCATE: |
bfe76559 | 5966 | return io_fallocate_prep(req, sqe); |
15b71abe | 5967 | case IORING_OP_OPENAT: |
bfe76559 | 5968 | return io_openat_prep(req, sqe); |
b5dba59e | 5969 | case IORING_OP_CLOSE: |
bfe76559 | 5970 | return io_close_prep(req, sqe); |
05f3fb3c | 5971 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 5972 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 5973 | case IORING_OP_STATX: |
bfe76559 | 5974 | return io_statx_prep(req, sqe); |
4840e418 | 5975 | case IORING_OP_FADVISE: |
bfe76559 | 5976 | return io_fadvise_prep(req, sqe); |
c1ca757b | 5977 | case IORING_OP_MADVISE: |
bfe76559 | 5978 | return io_madvise_prep(req, sqe); |
cebdb986 | 5979 | case IORING_OP_OPENAT2: |
bfe76559 | 5980 | return io_openat2_prep(req, sqe); |
3e4827b0 | 5981 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 5982 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 5983 | case IORING_OP_SPLICE: |
bfe76559 | 5984 | return io_splice_prep(req, sqe); |
ddf0322d | 5985 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 5986 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 5987 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 5988 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 5989 | case IORING_OP_TEE: |
bfe76559 | 5990 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
5991 | case IORING_OP_SHUTDOWN: |
5992 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
5993 | case IORING_OP_RENAMEAT: |
5994 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
5995 | case IORING_OP_UNLINKAT: |
5996 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
5997 | } |
5998 | ||
bfe76559 PB |
5999 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6000 | req->opcode); | |
bd54b6fe | 6001 | return -EINVAL; |
bfe76559 PB |
6002 | } |
6003 | ||
93642ef8 | 6004 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6005 | { |
b7e298d2 PB |
6006 | if (!io_op_defs[req->opcode].needs_async_setup) |
6007 | return 0; | |
6008 | if (WARN_ON_ONCE(req->async_data)) | |
6009 | return -EFAULT; | |
6010 | if (io_alloc_async_data(req)) | |
6011 | return -EAGAIN; | |
6012 | ||
93642ef8 PB |
6013 | switch (req->opcode) { |
6014 | case IORING_OP_READV: | |
93642ef8 PB |
6015 | return io_rw_prep_async(req, READ); |
6016 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6017 | return io_rw_prep_async(req, WRITE); |
6018 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6019 | return io_sendmsg_prep_async(req); |
6020 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6021 | return io_recvmsg_prep_async(req); |
6022 | case IORING_OP_CONNECT: | |
6023 | return io_connect_prep_async(req); | |
6024 | } | |
b7e298d2 PB |
6025 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6026 | req->opcode); | |
6027 | return -EFAULT; | |
f67676d1 JA |
6028 | } |
6029 | ||
9cf7c104 PB |
6030 | static u32 io_get_sequence(struct io_kiocb *req) |
6031 | { | |
a3dbdf54 | 6032 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6033 | |
a3dbdf54 PB |
6034 | /* need original cached_sq_head, but it was increased for each req */ |
6035 | io_for_each_link(req, req) | |
6036 | seq--; | |
6037 | return seq; | |
9cf7c104 PB |
6038 | } |
6039 | ||
76cc33d7 | 6040 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6041 | { |
3c19966d | 6042 | struct io_kiocb *pos; |
a197f664 | 6043 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6044 | struct io_defer_entry *de; |
f67676d1 | 6045 | int ret; |
9cf7c104 | 6046 | u32 seq; |
de0617e4 | 6047 | |
3c19966d PB |
6048 | /* |
6049 | * If we need to drain a request in the middle of a link, drain the | |
6050 | * head request and the next request/link after the current link. | |
6051 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6052 | * maintained for every request of our link. | |
6053 | */ | |
6054 | if (ctx->drain_next) { | |
6055 | req->flags |= REQ_F_IO_DRAIN; | |
6056 | ctx->drain_next = false; | |
6057 | } | |
6058 | /* not interested in head, start from the first linked */ | |
6059 | io_for_each_link(pos, req->link) { | |
6060 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6061 | ctx->drain_next = true; | |
6062 | req->flags |= REQ_F_IO_DRAIN; | |
6063 | break; | |
6064 | } | |
6065 | } | |
6066 | ||
9d858b21 | 6067 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 | 6068 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 PB |
6069 | !(req->flags & REQ_F_IO_DRAIN))) { |
6070 | ctx->drain_active = false; | |
76cc33d7 | 6071 | return false; |
10c66904 | 6072 | } |
9cf7c104 PB |
6073 | |
6074 | seq = io_get_sequence(req); | |
6075 | /* Still a chance to pass the sequence check */ | |
6076 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6077 | return false; |
de0617e4 | 6078 | |
b7e298d2 | 6079 | ret = io_req_prep_async(req); |
be7053b7 | 6080 | if (ret) |
1b48773f | 6081 | goto fail; |
cbdcb435 | 6082 | io_prep_async_link(req); |
27dc8338 | 6083 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6084 | if (!de) { |
1b48773f PB |
6085 | ret = -ENOMEM; |
6086 | fail: | |
6087 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6088 | return true; |
6089 | } | |
2d28390a | 6090 | |
79ebeaee | 6091 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6092 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6093 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6094 | kfree(de); |
ae34817b | 6095 | io_queue_async_work(req); |
76cc33d7 | 6096 | return true; |
de0617e4 JA |
6097 | } |
6098 | ||
915967f6 | 6099 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6100 | de->req = req; |
9cf7c104 | 6101 | de->seq = seq; |
27dc8338 | 6102 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6103 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6104 | return true; |
de0617e4 JA |
6105 | } |
6106 | ||
68fb8979 | 6107 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6108 | { |
0e1b6fe3 PB |
6109 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6110 | switch (req->opcode) { | |
6111 | case IORING_OP_READV: | |
6112 | case IORING_OP_READ_FIXED: | |
6113 | case IORING_OP_READ: | |
bcda7baa | 6114 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6115 | break; |
6116 | case IORING_OP_RECVMSG: | |
6117 | case IORING_OP_RECV: | |
bcda7baa | 6118 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6119 | break; |
6120 | } | |
99bc4c38 PB |
6121 | } |
6122 | ||
0e1b6fe3 PB |
6123 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6124 | switch (req->opcode) { | |
6125 | case IORING_OP_READV: | |
6126 | case IORING_OP_READ_FIXED: | |
6127 | case IORING_OP_READ: | |
6128 | case IORING_OP_WRITEV: | |
6129 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6130 | case IORING_OP_WRITE: { |
6131 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6132 | |
6133 | kfree(io->free_iovec); | |
0e1b6fe3 | 6134 | break; |
e8c2bc1f | 6135 | } |
0e1b6fe3 | 6136 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6137 | case IORING_OP_SENDMSG: { |
6138 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6139 | |
6140 | kfree(io->free_iov); | |
0e1b6fe3 | 6141 | break; |
e8c2bc1f | 6142 | } |
0e1b6fe3 PB |
6143 | case IORING_OP_SPLICE: |
6144 | case IORING_OP_TEE: | |
e1d767f0 PB |
6145 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6146 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6147 | break; |
f3cd4850 JA |
6148 | case IORING_OP_OPENAT: |
6149 | case IORING_OP_OPENAT2: | |
6150 | if (req->open.filename) | |
6151 | putname(req->open.filename); | |
6152 | break; | |
80a261fd JA |
6153 | case IORING_OP_RENAMEAT: |
6154 | putname(req->rename.oldpath); | |
6155 | putname(req->rename.newpath); | |
6156 | break; | |
14a1143b JA |
6157 | case IORING_OP_UNLINKAT: |
6158 | putname(req->unlink.filename); | |
6159 | break; | |
0e1b6fe3 | 6160 | } |
99bc4c38 | 6161 | } |
75652a30 JA |
6162 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6163 | kfree(req->apoll->double_poll); | |
6164 | kfree(req->apoll); | |
6165 | req->apoll = NULL; | |
6166 | } | |
3a0a6902 PB |
6167 | if (req->flags & REQ_F_INFLIGHT) { |
6168 | struct io_uring_task *tctx = req->task->io_uring; | |
6169 | ||
6170 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6171 | } |
c854357b | 6172 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6173 | put_cred(req->creds); |
c854357b PB |
6174 | |
6175 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6176 | } |
6177 | ||
889fca73 | 6178 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6179 | { |
a197f664 | 6180 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6181 | const struct cred *creds = NULL; |
d625c6ee | 6182 | int ret; |
2b188cc1 | 6183 | |
b8e64b53 | 6184 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6185 | creds = override_creds(req->creds); |
5730b27e | 6186 | |
d625c6ee | 6187 | switch (req->opcode) { |
2b188cc1 | 6188 | case IORING_OP_NOP: |
889fca73 | 6189 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6190 | break; |
6191 | case IORING_OP_READV: | |
edafccee | 6192 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6193 | case IORING_OP_READ: |
889fca73 | 6194 | ret = io_read(req, issue_flags); |
edafccee | 6195 | break; |
3529d8c2 | 6196 | case IORING_OP_WRITEV: |
edafccee | 6197 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6198 | case IORING_OP_WRITE: |
889fca73 | 6199 | ret = io_write(req, issue_flags); |
2b188cc1 | 6200 | break; |
c992fe29 | 6201 | case IORING_OP_FSYNC: |
45d189c6 | 6202 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6203 | break; |
221c5eb2 | 6204 | case IORING_OP_POLL_ADD: |
61e98203 | 6205 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6206 | break; |
6207 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6208 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6209 | break; |
5d17b4a4 | 6210 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6211 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6212 | break; |
0fa03c62 | 6213 | case IORING_OP_SENDMSG: |
889fca73 | 6214 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6215 | break; |
fddaface | 6216 | case IORING_OP_SEND: |
889fca73 | 6217 | ret = io_send(req, issue_flags); |
0fa03c62 | 6218 | break; |
aa1fa28f | 6219 | case IORING_OP_RECVMSG: |
889fca73 | 6220 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6221 | break; |
fddaface | 6222 | case IORING_OP_RECV: |
889fca73 | 6223 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6224 | break; |
5262f567 | 6225 | case IORING_OP_TIMEOUT: |
61e98203 | 6226 | ret = io_timeout(req, issue_flags); |
5262f567 | 6227 | break; |
11365043 | 6228 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6229 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6230 | break; |
17f2fe35 | 6231 | case IORING_OP_ACCEPT: |
889fca73 | 6232 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6233 | break; |
f8e85cf2 | 6234 | case IORING_OP_CONNECT: |
889fca73 | 6235 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6236 | break; |
62755e35 | 6237 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6238 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6239 | break; |
d63d1b5e | 6240 | case IORING_OP_FALLOCATE: |
45d189c6 | 6241 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6242 | break; |
15b71abe | 6243 | case IORING_OP_OPENAT: |
45d189c6 | 6244 | ret = io_openat(req, issue_flags); |
15b71abe | 6245 | break; |
b5dba59e | 6246 | case IORING_OP_CLOSE: |
889fca73 | 6247 | ret = io_close(req, issue_flags); |
b5dba59e | 6248 | break; |
05f3fb3c | 6249 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6250 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6251 | break; |
eddc7ef5 | 6252 | case IORING_OP_STATX: |
45d189c6 | 6253 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6254 | break; |
4840e418 | 6255 | case IORING_OP_FADVISE: |
45d189c6 | 6256 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6257 | break; |
c1ca757b | 6258 | case IORING_OP_MADVISE: |
45d189c6 | 6259 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6260 | break; |
cebdb986 | 6261 | case IORING_OP_OPENAT2: |
45d189c6 | 6262 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6263 | break; |
3e4827b0 | 6264 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6265 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6266 | break; |
7d67af2c | 6267 | case IORING_OP_SPLICE: |
45d189c6 | 6268 | ret = io_splice(req, issue_flags); |
7d67af2c | 6269 | break; |
ddf0322d | 6270 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6271 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6272 | break; |
067524e9 | 6273 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6274 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6275 | break; |
f2a8d5c7 | 6276 | case IORING_OP_TEE: |
45d189c6 | 6277 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6278 | break; |
36f4fa68 | 6279 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6280 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6281 | break; |
80a261fd | 6282 | case IORING_OP_RENAMEAT: |
45d189c6 | 6283 | ret = io_renameat(req, issue_flags); |
80a261fd | 6284 | break; |
14a1143b | 6285 | case IORING_OP_UNLINKAT: |
45d189c6 | 6286 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6287 | break; |
2b188cc1 JA |
6288 | default: |
6289 | ret = -EINVAL; | |
6290 | break; | |
6291 | } | |
6292 | ||
5730b27e JA |
6293 | if (creds) |
6294 | revert_creds(creds); | |
def596e9 JA |
6295 | if (ret) |
6296 | return ret; | |
b532576e | 6297 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6298 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6299 | io_iopoll_req_issued(req); | |
def596e9 JA |
6300 | |
6301 | return 0; | |
2b188cc1 JA |
6302 | } |
6303 | ||
ebc11b6c PB |
6304 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6305 | { | |
6306 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6307 | ||
6308 | req = io_put_req_find_next(req); | |
6309 | return req ? &req->work : NULL; | |
6310 | } | |
6311 | ||
5280f7e5 | 6312 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6313 | { |
6314 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6315 | struct io_kiocb *timeout; |
561fb04a | 6316 | int ret = 0; |
2b188cc1 | 6317 | |
48dcd38d PB |
6318 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6319 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6320 | __io_req_set_refcount(req, 2); | |
6321 | else | |
6322 | req_ref_get(req); | |
5d5901a3 | 6323 | |
6df1db6b PB |
6324 | timeout = io_prep_linked_timeout(req); |
6325 | if (timeout) | |
6326 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6327 | |
4014d943 | 6328 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6329 | ret = -ECANCELED; |
31b51510 | 6330 | |
561fb04a | 6331 | if (!ret) { |
561fb04a | 6332 | do { |
889fca73 | 6333 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6334 | /* |
6335 | * We can get EAGAIN for polled IO even though we're | |
6336 | * forcing a sync submission from here, since we can't | |
6337 | * wait for request slots on the block side. | |
6338 | */ | |
6339 | if (ret != -EAGAIN) | |
6340 | break; | |
6341 | cond_resched(); | |
6342 | } while (1); | |
6343 | } | |
31b51510 | 6344 | |
a3df7698 | 6345 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6346 | if (ret) |
a3df7698 | 6347 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6348 | } |
6349 | ||
aeca241b | 6350 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6351 | unsigned i) |
65e19f54 | 6352 | { |
042b0d85 | 6353 | return &table->files[i]; |
dafecf19 PB |
6354 | } |
6355 | ||
65e19f54 JA |
6356 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6357 | int index) | |
6358 | { | |
aeca241b | 6359 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6360 | |
a04b0ac0 | 6361 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6362 | } |
6363 | ||
a04b0ac0 | 6364 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6365 | { |
6366 | unsigned long file_ptr = (unsigned long) file; | |
6367 | ||
b191e2df | 6368 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6369 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6370 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6371 | file_ptr |= FFS_ASYNC_WRITE; |
6372 | if (S_ISREG(file_inode(file)->i_mode)) | |
6373 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6374 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6375 | } |
6376 | ||
ac177053 PB |
6377 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6378 | struct io_kiocb *req, int fd) | |
09bb8394 | 6379 | { |
8da11c19 | 6380 | struct file *file; |
ac177053 | 6381 | unsigned long file_ptr; |
09bb8394 | 6382 | |
ac177053 PB |
6383 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6384 | return NULL; | |
6385 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6386 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6387 | file = (struct file *) (file_ptr & FFS_MASK); | |
6388 | file_ptr &= ~FFS_MASK; | |
6389 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6390 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6391 | io_req_set_rsrc_node(req); |
6392 | return file; | |
6393 | } | |
d44f554e | 6394 | |
ac177053 | 6395 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6396 | struct io_kiocb *req, int fd) |
6397 | { | |
62906e89 | 6398 | struct file *file = fget(fd); |
ac177053 PB |
6399 | |
6400 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6401 | |
ac177053 PB |
6402 | /* we don't allow fixed io_uring files */ |
6403 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6404 | io_req_track_inflight(req); | |
8371adf5 | 6405 | return file; |
09bb8394 JA |
6406 | } |
6407 | ||
ac177053 | 6408 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6409 | struct io_kiocb *req, int fd, bool fixed) |
6410 | { | |
6411 | if (fixed) | |
6412 | return io_file_get_fixed(ctx, req, fd); | |
6413 | else | |
62906e89 | 6414 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6415 | } |
6416 | ||
89b263f6 JA |
6417 | static void io_req_task_link_timeout(struct io_kiocb *req) |
6418 | { | |
6419 | struct io_kiocb *prev = req->timeout.prev; | |
6420 | struct io_ring_ctx *ctx = req->ctx; | |
6421 | ||
6422 | if (prev) { | |
6423 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); | |
6424 | io_put_req(prev); | |
6425 | io_put_req(req); | |
6426 | } else { | |
6427 | io_req_complete_post(req, -ETIME, 0); | |
6428 | } | |
6429 | } | |
6430 | ||
2665abfd | 6431 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6432 | { |
ad8a48ac JA |
6433 | struct io_timeout_data *data = container_of(timer, |
6434 | struct io_timeout_data, timer); | |
90cd7e42 | 6435 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6436 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6437 | unsigned long flags; |
2665abfd | 6438 | |
89b263f6 | 6439 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6440 | prev = req->timeout.head; |
6441 | req->timeout.head = NULL; | |
2665abfd JA |
6442 | |
6443 | /* | |
6444 | * We don't expect the list to be empty, that will only happen if we | |
6445 | * race with the completion of the linked work. | |
6446 | */ | |
447c19f3 | 6447 | if (prev) { |
f2f87370 | 6448 | io_remove_next_linked(prev); |
447c19f3 PB |
6449 | if (!req_ref_inc_not_zero(prev)) |
6450 | prev = NULL; | |
6451 | } | |
89b263f6 JA |
6452 | req->timeout.prev = prev; |
6453 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6454 | |
89b263f6 JA |
6455 | req->io_task_work.func = io_req_task_link_timeout; |
6456 | io_req_task_work_add(req); | |
2665abfd JA |
6457 | return HRTIMER_NORESTART; |
6458 | } | |
6459 | ||
de968c18 | 6460 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6461 | { |
de968c18 PB |
6462 | struct io_ring_ctx *ctx = req->ctx; |
6463 | ||
89b263f6 | 6464 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6465 | /* |
f2f87370 PB |
6466 | * If the back reference is NULL, then our linked request finished |
6467 | * before we got a chance to setup the timer | |
76a46e06 | 6468 | */ |
90cd7e42 | 6469 | if (req->timeout.head) { |
e8c2bc1f | 6470 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6471 | |
ad8a48ac JA |
6472 | data->timer.function = io_link_timeout_fn; |
6473 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6474 | data->mode); | |
2665abfd | 6475 | } |
89b263f6 | 6476 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6477 | /* drop submission reference */ |
76a46e06 JA |
6478 | io_put_req(req); |
6479 | } | |
2665abfd | 6480 | |
c5eef2b9 | 6481 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6482 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6483 | { |
d3d7298d | 6484 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
e0c5c576 | 6485 | int ret; |
2b188cc1 | 6486 | |
59b735ae | 6487 | issue_sqe: |
c5eef2b9 | 6488 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6489 | |
491381ce JA |
6490 | /* |
6491 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6492 | * doesn't support non-blocking read/write attempts | |
6493 | */ | |
1840038e | 6494 | if (likely(!ret)) { |
e342c807 | 6495 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6496 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6497 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6498 | |
cd0ca2e0 PB |
6499 | state->compl_reqs[state->compl_nr++] = req; |
6500 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6501 | io_submit_flush_completions(ctx); |
0d63c148 | 6502 | } |
1840038e | 6503 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
59b735ae OL |
6504 | switch (io_arm_poll_handler(req)) { |
6505 | case IO_APOLL_READY: | |
6506 | goto issue_sqe; | |
6507 | case IO_APOLL_ABORTED: | |
1840038e PB |
6508 | /* |
6509 | * Queued up for async execution, worker will release | |
6510 | * submit reference when the iocb is actually submitted. | |
6511 | */ | |
6512 | io_queue_async_work(req); | |
59b735ae | 6513 | break; |
1840038e | 6514 | } |
0d63c148 | 6515 | } else { |
f41db273 | 6516 | io_req_complete_failed(req, ret); |
9e645e11 | 6517 | } |
d3d7298d PB |
6518 | if (linked_timeout) |
6519 | io_queue_linked_timeout(linked_timeout); | |
2b188cc1 JA |
6520 | } |
6521 | ||
441b8a78 | 6522 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6523 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6524 | { |
10c66904 | 6525 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6526 | return; |
4fe2c963 | 6527 | |
76cc33d7 | 6528 | if (likely(!(req->flags & REQ_F_FORCE_ASYNC))) { |
c5eef2b9 | 6529 | __io_queue_sqe(req); |
76cc33d7 PB |
6530 | } else { |
6531 | int ret = io_req_prep_async(req); | |
6532 | ||
6533 | if (unlikely(ret)) | |
6534 | io_req_complete_failed(req, ret); | |
6535 | else | |
6536 | io_queue_async_work(req); | |
ce35a47a | 6537 | } |
4fe2c963 JL |
6538 | } |
6539 | ||
b16fed66 PB |
6540 | /* |
6541 | * Check SQE restrictions (opcode and flags). | |
6542 | * | |
6543 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6544 | */ | |
6545 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6546 | struct io_kiocb *req, | |
6547 | unsigned int sqe_flags) | |
4fe2c963 | 6548 | { |
4cfb25bf | 6549 | if (likely(!ctx->restricted)) |
b16fed66 PB |
6550 | return true; |
6551 | ||
6552 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6553 | return false; | |
6554 | ||
6555 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6556 | ctx->restrictions.sqe_flags_required) | |
6557 | return false; | |
6558 | ||
6559 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6560 | ctx->restrictions.sqe_flags_required)) | |
6561 | return false; | |
6562 | ||
6563 | return true; | |
4fe2c963 JL |
6564 | } |
6565 | ||
b16fed66 PB |
6566 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6567 | const struct io_uring_sqe *sqe) | |
282cdc86 | 6568 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
6569 | { |
6570 | struct io_submit_state *state; | |
6571 | unsigned int sqe_flags; | |
003e8dcc | 6572 | int personality, ret = 0; |
b16fed66 | 6573 | |
864ea921 | 6574 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6575 | req->opcode = READ_ONCE(sqe->opcode); |
6576 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6577 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6578 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6579 | req->file = NULL; |
b16fed66 | 6580 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 6581 | req->task = current; |
b16fed66 PB |
6582 | |
6583 | /* enforce forwards compatibility on users */ | |
dddca226 | 6584 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 6585 | return -EINVAL; |
b16fed66 PB |
6586 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6587 | return -EINVAL; | |
4cfb25bf | 6588 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
6589 | return -EACCES; |
6590 | ||
6591 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6592 | !io_op_defs[req->opcode].buffer_select) | |
6593 | return -EOPNOTSUPP; | |
3c19966d PB |
6594 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
6595 | ctx->drain_active = true; | |
863e0560 | 6596 | |
003e8dcc JA |
6597 | personality = READ_ONCE(sqe->personality); |
6598 | if (personality) { | |
c10d1f98 PB |
6599 | req->creds = xa_load(&ctx->personalities, personality); |
6600 | if (!req->creds) | |
003e8dcc | 6601 | return -EINVAL; |
c10d1f98 | 6602 | get_cred(req->creds); |
b8e64b53 | 6603 | req->flags |= REQ_F_CREDS; |
003e8dcc | 6604 | } |
b16fed66 PB |
6605 | state = &ctx->submit_state; |
6606 | ||
6607 | /* | |
6608 | * Plug now if we have more than 1 IO left after this, and the target | |
6609 | * is potentially a read/write to block based storage. | |
6610 | */ | |
6611 | if (!state->plug_started && state->ios_left > 1 && | |
6612 | io_op_defs[req->opcode].plug) { | |
6613 | blk_start_plug(&state->plug); | |
6614 | state->plug_started = true; | |
6615 | } | |
6616 | ||
6617 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 6618 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 6619 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
6620 | if (unlikely(!req->file)) |
6621 | ret = -EBADF; | |
6622 | } | |
6623 | ||
6624 | state->ios_left--; | |
6625 | return ret; | |
6626 | } | |
6627 | ||
a6b8cadc | 6628 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6629 | const struct io_uring_sqe *sqe) |
282cdc86 | 6630 | __must_hold(&ctx->uring_lock) |
9e645e11 | 6631 | { |
a1ab7b35 | 6632 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6633 | int ret; |
9e645e11 | 6634 | |
a6b8cadc PB |
6635 | ret = io_init_req(ctx, req, sqe); |
6636 | if (unlikely(ret)) { | |
6637 | fail_req: | |
de59bc10 PB |
6638 | if (link->head) { |
6639 | /* fail even hard links since we don't submit */ | |
93d2bcd2 | 6640 | req_set_fail(link->head); |
f41db273 | 6641 | io_req_complete_failed(link->head, -ECANCELED); |
de59bc10 PB |
6642 | link->head = NULL; |
6643 | } | |
f41db273 | 6644 | io_req_complete_failed(req, ret); |
a6b8cadc PB |
6645 | return ret; |
6646 | } | |
441b8a78 | 6647 | |
be7053b7 PB |
6648 | ret = io_req_prep(req, sqe); |
6649 | if (unlikely(ret)) | |
6650 | goto fail_req; | |
a6b8cadc | 6651 | |
be7053b7 | 6652 | /* don't need @sqe from now on */ |
236daeae OL |
6653 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
6654 | req->flags, true, | |
6655 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 6656 | |
9e645e11 JA |
6657 | /* |
6658 | * If we already have a head request, queue this one for async | |
6659 | * submittal once the head completes. If we don't have a head but | |
6660 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6661 | * submitted sync once the chain is complete. If none of those | |
6662 | * conditions are true (normal request), then just queue it. | |
6663 | */ | |
863e0560 PB |
6664 | if (link->head) { |
6665 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6666 | |
b7e298d2 | 6667 | ret = io_req_prep_async(req); |
cf109604 | 6668 | if (unlikely(ret)) |
a6b8cadc | 6669 | goto fail_req; |
9d76377f | 6670 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6671 | link->last->link = req; |
863e0560 | 6672 | link->last = req; |
32fe525b PB |
6673 | |
6674 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6675 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 6676 | link->head = NULL; |
5e159204 | 6677 | io_queue_sqe(head); |
32fe525b | 6678 | } |
9e645e11 | 6679 | } else { |
ef4ff581 | 6680 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6681 | link->head = req; |
6682 | link->last = req; | |
711be031 | 6683 | } else { |
be7053b7 | 6684 | io_queue_sqe(req); |
711be031 | 6685 | } |
9e645e11 | 6686 | } |
2e6e1fde | 6687 | |
1d4240cc | 6688 | return 0; |
9e645e11 JA |
6689 | } |
6690 | ||
9a56a232 JA |
6691 | /* |
6692 | * Batched submission is done, ensure local IO is flushed out. | |
6693 | */ | |
ba88ff11 PB |
6694 | static void io_submit_state_end(struct io_submit_state *state, |
6695 | struct io_ring_ctx *ctx) | |
9a56a232 | 6696 | { |
a1ab7b35 | 6697 | if (state->link.head) |
de59bc10 | 6698 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 6699 | if (state->compl_nr) |
2a2758f2 | 6700 | io_submit_flush_completions(ctx); |
27926b68 JA |
6701 | if (state->plug_started) |
6702 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
6703 | } |
6704 | ||
6705 | /* | |
6706 | * Start submission side cache. | |
6707 | */ | |
6708 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6709 | unsigned int max_ios) |
9a56a232 | 6710 | { |
27926b68 | 6711 | state->plug_started = false; |
9a56a232 | 6712 | state->ios_left = max_ios; |
a1ab7b35 PB |
6713 | /* set only head, no need to init link_last in advance */ |
6714 | state->link.head = NULL; | |
9a56a232 JA |
6715 | } |
6716 | ||
2b188cc1 JA |
6717 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6718 | { | |
75b28aff | 6719 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6720 | |
caf582c6 PB |
6721 | /* |
6722 | * Ensure any loads from the SQEs are done at this point, | |
6723 | * since once we write the new head, the application could | |
6724 | * write new data to them. | |
6725 | */ | |
6726 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6727 | } |
6728 | ||
2b188cc1 | 6729 | /* |
dd9ae8a0 | 6730 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
6731 | * that is mapped by userspace. This means that care needs to be taken to |
6732 | * ensure that reads are stable, as we cannot rely on userspace always | |
6733 | * being a good citizen. If members of the sqe are validated and then later | |
6734 | * used, it's important that those reads are done through READ_ONCE() to | |
6735 | * prevent a re-load down the line. | |
6736 | */ | |
709b302f | 6737 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6738 | { |
ea5ab3b5 | 6739 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 6740 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
6741 | |
6742 | /* | |
6743 | * The cached sq head (or cq tail) serves two purposes: | |
6744 | * | |
6745 | * 1) allows us to batch the cost of updating the user visible | |
6746 | * head updates. | |
6747 | * 2) allows the kernel side to track the head on its own, even | |
6748 | * though the application is the one updating it. | |
6749 | */ | |
17d3aeb3 | 6750 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
6751 | if (likely(head < ctx->sq_entries)) |
6752 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6753 | |
6754 | /* drop invalid entries */ | |
15641e42 PB |
6755 | ctx->cq_extra--; |
6756 | WRITE_ONCE(ctx->rings->sq_dropped, | |
6757 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
6758 | return NULL; |
6759 | } | |
6760 | ||
0f212204 | 6761 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 6762 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 6763 | { |
09899b19 | 6764 | struct io_uring_task *tctx; |
46c4e16a | 6765 | int submitted = 0; |
6c271ce2 | 6766 | |
ee7d46d9 PB |
6767 | /* make sure SQ entry isn't read before tail */ |
6768 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
6769 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6770 | return -EAGAIN; | |
6c271ce2 | 6771 | |
09899b19 PB |
6772 | tctx = current->io_uring; |
6773 | tctx->cached_refs -= nr; | |
6774 | if (unlikely(tctx->cached_refs < 0)) { | |
6775 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
6776 | ||
6777 | percpu_counter_add(&tctx->inflight, refill); | |
6778 | refcount_add(refill, ¤t->usage); | |
6779 | tctx->cached_refs += refill; | |
6780 | } | |
ba88ff11 | 6781 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6782 | |
46c4e16a | 6783 | while (submitted < nr) { |
3529d8c2 | 6784 | const struct io_uring_sqe *sqe; |
196be95c | 6785 | struct io_kiocb *req; |
fb5ccc98 | 6786 | |
258b29a9 | 6787 | req = io_alloc_req(ctx); |
196be95c PB |
6788 | if (unlikely(!req)) { |
6789 | if (!submitted) | |
6790 | submitted = -EAGAIN; | |
fb5ccc98 | 6791 | break; |
196be95c | 6792 | } |
4fccfcbb PB |
6793 | sqe = io_get_sqe(ctx); |
6794 | if (unlikely(!sqe)) { | |
6795 | kmem_cache_free(req_cachep, req); | |
6796 | break; | |
6797 | } | |
d3656344 JA |
6798 | /* will complete beyond this point, count as submitted */ |
6799 | submitted++; | |
a1ab7b35 | 6800 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6801 | break; |
6c271ce2 JA |
6802 | } |
6803 | ||
9466f437 PB |
6804 | if (unlikely(submitted != nr)) { |
6805 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 6806 | int unused = nr - ref_used; |
9466f437 | 6807 | |
09899b19 | 6808 | current->io_uring->cached_refs += unused; |
d8a6df10 | 6809 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 6810 | } |
6c271ce2 | 6811 | |
a1ab7b35 | 6812 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6813 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6814 | io_commit_sqring(ctx); | |
6815 | ||
6c271ce2 JA |
6816 | return submitted; |
6817 | } | |
6818 | ||
e4b6d902 PB |
6819 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
6820 | { | |
6821 | return READ_ONCE(sqd->state); | |
6822 | } | |
6823 | ||
23b3628e XW |
6824 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6825 | { | |
6826 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 6827 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6828 | WRITE_ONCE(ctx->rings->sq_flags, |
6829 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6830 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6831 | } |
6832 | ||
6833 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6834 | { | |
79ebeaee | 6835 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6836 | WRITE_ONCE(ctx->rings->sq_flags, |
6837 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6838 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6839 | } |
6840 | ||
08369246 | 6841 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6842 | { |
c8d1ba58 | 6843 | unsigned int to_submit; |
bdcd3eab | 6844 | int ret = 0; |
6c271ce2 | 6845 | |
c8d1ba58 | 6846 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 6847 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
6848 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
6849 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 6850 | |
906a3c6f | 6851 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6852 | unsigned nr_events = 0; |
948e1947 PB |
6853 | const struct cred *creds = NULL; |
6854 | ||
6855 | if (ctx->sq_creds != current_cred()) | |
6856 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 6857 | |
c8d1ba58 | 6858 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6859 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 6860 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6861 | |
3b763ba1 PB |
6862 | /* |
6863 | * Don't submit if refs are dying, good for io_uring_register(), | |
6864 | * but also it is relied upon by io_ring_exit_work() | |
6865 | */ | |
0298ef96 PB |
6866 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
6867 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 6868 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 6869 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 6870 | |
acfb381d PB |
6871 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6872 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
6873 | if (creds) |
6874 | revert_creds(creds); | |
acfb381d | 6875 | } |
6c271ce2 | 6876 | |
08369246 XW |
6877 | return ret; |
6878 | } | |
6c271ce2 | 6879 | |
08369246 XW |
6880 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6881 | { | |
6882 | struct io_ring_ctx *ctx; | |
6883 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6884 | |
c9dca27d PB |
6885 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6886 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 6887 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6888 | } |
6c271ce2 | 6889 | |
e4b6d902 PB |
6890 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
6891 | { | |
6892 | bool did_sig = false; | |
6893 | struct ksignal ksig; | |
6894 | ||
6895 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
6896 | signal_pending(current)) { | |
6897 | mutex_unlock(&sqd->lock); | |
6898 | if (signal_pending(current)) | |
6899 | did_sig = get_signal(&ksig); | |
6900 | cond_resched(); | |
6901 | mutex_lock(&sqd->lock); | |
6902 | } | |
e4b6d902 PB |
6903 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
6904 | } | |
6905 | ||
c8d1ba58 JA |
6906 | static int io_sq_thread(void *data) |
6907 | { | |
69fb2131 JA |
6908 | struct io_sq_data *sqd = data; |
6909 | struct io_ring_ctx *ctx; | |
a0d9205f | 6910 | unsigned long timeout = 0; |
37d1e2e3 | 6911 | char buf[TASK_COMM_LEN]; |
08369246 | 6912 | DEFINE_WAIT(wait); |
6c271ce2 | 6913 | |
696ee88a | 6914 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 6915 | set_task_comm(current, buf); |
37d1e2e3 JA |
6916 | |
6917 | if (sqd->sq_cpu != -1) | |
6918 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6919 | else | |
6920 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6921 | current->flags |= PF_NO_SETAFFINITY; | |
6922 | ||
09a6f4ef | 6923 | mutex_lock(&sqd->lock); |
e4b6d902 | 6924 | while (1) { |
1a924a80 | 6925 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 6926 | |
e4b6d902 PB |
6927 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
6928 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 6929 | break; |
08369246 XW |
6930 | timeout = jiffies + sqd->sq_thread_idle; |
6931 | } | |
e4b6d902 | 6932 | |
e95eee2d | 6933 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6934 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 6935 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 6936 | |
08369246 XW |
6937 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
6938 | sqt_spin = true; | |
69fb2131 | 6939 | } |
dd432ea5 PB |
6940 | if (io_run_task_work()) |
6941 | sqt_spin = true; | |
6c271ce2 | 6942 | |
08369246 | 6943 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 6944 | cond_resched(); |
08369246 XW |
6945 | if (sqt_spin) |
6946 | timeout = jiffies + sqd->sq_thread_idle; | |
6947 | continue; | |
6948 | } | |
6949 | ||
08369246 | 6950 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 6951 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
6952 | bool needs_sched = true; |
6953 | ||
724cb4f9 | 6954 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
6955 | io_ring_set_wakeup_flag(ctx); |
6956 | ||
724cb4f9 HX |
6957 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
6958 | !list_empty_careful(&ctx->iopoll_list)) { | |
6959 | needs_sched = false; | |
6960 | break; | |
6961 | } | |
6962 | if (io_sqring_entries(ctx)) { | |
6963 | needs_sched = false; | |
6964 | break; | |
6965 | } | |
6966 | } | |
6967 | ||
6968 | if (needs_sched) { | |
6969 | mutex_unlock(&sqd->lock); | |
6970 | schedule(); | |
6971 | mutex_lock(&sqd->lock); | |
6972 | } | |
69fb2131 JA |
6973 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6974 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 6975 | } |
08369246 XW |
6976 | |
6977 | finish_wait(&sqd->wait, &wait); | |
6978 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 6979 | } |
28cea78a | 6980 | |
78cc687b | 6981 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 6982 | sqd->thread = NULL; |
05962f95 | 6983 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 6984 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 6985 | io_run_task_work(); |
734551df PB |
6986 | mutex_unlock(&sqd->lock); |
6987 | ||
37d1e2e3 JA |
6988 | complete(&sqd->exited); |
6989 | do_exit(0); | |
6c271ce2 JA |
6990 | } |
6991 | ||
bda52162 JA |
6992 | struct io_wait_queue { |
6993 | struct wait_queue_entry wq; | |
6994 | struct io_ring_ctx *ctx; | |
5fd46178 | 6995 | unsigned cq_tail; |
bda52162 JA |
6996 | unsigned nr_timeouts; |
6997 | }; | |
6998 | ||
6c503150 | 6999 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7000 | { |
7001 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7002 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7003 | |
7004 | /* | |
d195a66e | 7005 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7006 | * started waiting. For timeouts, we always want to return to userspace, |
7007 | * regardless of event count. | |
7008 | */ | |
5fd46178 | 7009 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7010 | } |
7011 | ||
7012 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7013 | int wake_flags, void *key) | |
7014 | { | |
7015 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7016 | wq); | |
7017 | ||
6c503150 PB |
7018 | /* |
7019 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7020 | * the task, and the next invocation will do it. | |
7021 | */ | |
5ed7a37d | 7022 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7023 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7024 | return -1; | |
bda52162 JA |
7025 | } |
7026 | ||
af9c1a44 JA |
7027 | static int io_run_task_work_sig(void) |
7028 | { | |
7029 | if (io_run_task_work()) | |
7030 | return 1; | |
7031 | if (!signal_pending(current)) | |
7032 | return 0; | |
0b8cfa97 | 7033 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7034 | return -ERESTARTSYS; |
af9c1a44 JA |
7035 | return -EINTR; |
7036 | } | |
7037 | ||
eeb60b9a PB |
7038 | /* when returns >0, the caller should retry */ |
7039 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7040 | struct io_wait_queue *iowq, | |
7041 | signed long *timeout) | |
7042 | { | |
7043 | int ret; | |
7044 | ||
7045 | /* make sure we run task_work before checking for signals */ | |
7046 | ret = io_run_task_work_sig(); | |
7047 | if (ret || io_should_wake(iowq)) | |
7048 | return ret; | |
7049 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7050 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7051 | return 1; |
7052 | ||
7053 | *timeout = schedule_timeout(*timeout); | |
7054 | return !*timeout ? -ETIME : 1; | |
7055 | } | |
7056 | ||
2b188cc1 JA |
7057 | /* |
7058 | * Wait until events become available, if we don't already have some. The | |
7059 | * application must reap them itself, as they reside on the shared cq ring. | |
7060 | */ | |
7061 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7062 | const sigset_t __user *sig, size_t sigsz, |
7063 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7064 | { |
90291099 | 7065 | struct io_wait_queue iowq; |
75b28aff | 7066 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7067 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7068 | int ret; | |
2b188cc1 | 7069 | |
b41e9852 | 7070 | do { |
90f67366 | 7071 | io_cqring_overflow_flush(ctx); |
6c503150 | 7072 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7073 | return 0; |
4c6e277c | 7074 | if (!io_run_task_work()) |
b41e9852 | 7075 | break; |
b41e9852 | 7076 | } while (1); |
2b188cc1 JA |
7077 | |
7078 | if (sig) { | |
9e75ad5d AB |
7079 | #ifdef CONFIG_COMPAT |
7080 | if (in_compat_syscall()) | |
7081 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7082 | sigsz); |
9e75ad5d AB |
7083 | else |
7084 | #endif | |
b772434b | 7085 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7086 | |
2b188cc1 JA |
7087 | if (ret) |
7088 | return ret; | |
7089 | } | |
7090 | ||
c73ebb68 | 7091 | if (uts) { |
c1d5a224 PB |
7092 | struct timespec64 ts; |
7093 | ||
c73ebb68 HX |
7094 | if (get_timespec64(&ts, uts)) |
7095 | return -EFAULT; | |
7096 | timeout = timespec64_to_jiffies(&ts); | |
7097 | } | |
7098 | ||
90291099 PB |
7099 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7100 | iowq.wq.private = current; | |
7101 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7102 | iowq.ctx = ctx; | |
bda52162 | 7103 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7104 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7105 | |
c826bd7a | 7106 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7107 | do { |
ca0a2651 | 7108 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7109 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7110 | ret = -EBUSY; |
7111 | break; | |
7112 | } | |
311997b3 | 7113 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7114 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7115 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7116 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7117 | cond_resched(); |
eeb60b9a | 7118 | } while (ret > 0); |
bda52162 | 7119 | |
b7db41c9 | 7120 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7121 | |
75b28aff | 7122 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7123 | } |
7124 | ||
9123c8ff | 7125 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7126 | { |
9123c8ff | 7127 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7128 | |
846a4ef2 | 7129 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7130 | kfree(table[i]); |
7131 | kfree(table); | |
7132 | } | |
7133 | ||
7134 | static void **io_alloc_page_table(size_t size) | |
7135 | { | |
7136 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7137 | size_t init_size = size; | |
7138 | void **table; | |
7139 | ||
7140 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL); | |
7141 | if (!table) | |
7142 | return NULL; | |
7143 | ||
7144 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7145 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff PB |
7146 | |
7147 | table[i] = kzalloc(this_size, GFP_KERNEL); | |
7148 | if (!table[i]) { | |
7149 | io_free_page_table(table, init_size); | |
7150 | return NULL; | |
7151 | } | |
7152 | size -= this_size; | |
7153 | } | |
7154 | return table; | |
05f3fb3c JA |
7155 | } |
7156 | ||
28a9fe25 | 7157 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7158 | { |
28a9fe25 PB |
7159 | percpu_ref_exit(&ref_node->refs); |
7160 | kfree(ref_node); | |
1642b445 PB |
7161 | } |
7162 | ||
b9bd2bea PB |
7163 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7164 | { | |
7165 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7166 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7167 | unsigned long flags; | |
7168 | bool first_add = false; | |
7169 | ||
7170 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7171 | node->done = true; | |
7172 | ||
7173 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7174 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7175 | struct io_rsrc_node, node); | |
7176 | /* recycle ref nodes in order */ | |
7177 | if (!node->done) | |
7178 | break; | |
7179 | list_del(&node->node); | |
7180 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7181 | } | |
7182 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7183 | ||
7184 | if (first_add) | |
7185 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7186 | } | |
7187 | ||
7188 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7189 | { | |
7190 | struct io_rsrc_node *ref_node; | |
7191 | ||
7192 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7193 | if (!ref_node) | |
7194 | return NULL; | |
7195 | ||
7196 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7197 | 0, GFP_KERNEL)) { | |
7198 | kfree(ref_node); | |
7199 | return NULL; | |
7200 | } | |
7201 | INIT_LIST_HEAD(&ref_node->node); | |
7202 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7203 | ref_node->done = false; | |
7204 | return ref_node; | |
7205 | } | |
7206 | ||
a7f0ed5a PB |
7207 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7208 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7209 | { |
a7f0ed5a PB |
7210 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7211 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7212 | |
a7f0ed5a PB |
7213 | if (data_to_kill) { |
7214 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7215 | |
a7f0ed5a | 7216 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7217 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7218 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7219 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7220 | |
3e942498 | 7221 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7222 | percpu_ref_kill(&rsrc_node->refs); |
7223 | ctx->rsrc_node = NULL; | |
7224 | } | |
6b06314c | 7225 | |
a7f0ed5a PB |
7226 | if (!ctx->rsrc_node) { |
7227 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7228 | ctx->rsrc_backup_node = NULL; | |
7229 | } | |
8bad28d8 HX |
7230 | } |
7231 | ||
a7f0ed5a | 7232 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7233 | { |
7234 | if (ctx->rsrc_backup_node) | |
7235 | return 0; | |
b895c9a6 | 7236 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7237 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7238 | } |
7239 | ||
40ae0ff7 | 7240 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7241 | { |
7242 | int ret; | |
05589553 | 7243 | |
215c3902 | 7244 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7245 | if (data->quiesce) |
7246 | return -ENXIO; | |
05589553 | 7247 | |
8bad28d8 | 7248 | data->quiesce = true; |
1ffc5422 | 7249 | do { |
a7f0ed5a | 7250 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7251 | if (ret) |
f2303b1f | 7252 | break; |
a7f0ed5a | 7253 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7254 | |
3e942498 PB |
7255 | /* kill initial ref, already quiesced if zero */ |
7256 | if (atomic_dec_and_test(&data->refs)) | |
7257 | break; | |
c018db4a | 7258 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7259 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7260 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7261 | if (!ret) { |
7262 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7263 | break; |
c018db4a | 7264 | } |
8bad28d8 | 7265 | |
3e942498 PB |
7266 | atomic_inc(&data->refs); |
7267 | /* wait for all works potentially completing data->done */ | |
7268 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7269 | reinit_completion(&data->done); |
8dd03afe | 7270 | |
1ffc5422 | 7271 | ret = io_run_task_work_sig(); |
8bad28d8 | 7272 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7273 | } while (ret >= 0); |
8bad28d8 | 7274 | data->quiesce = false; |
05f3fb3c | 7275 | |
8bad28d8 | 7276 | return ret; |
d7954b2b BM |
7277 | } |
7278 | ||
2d091d62 PB |
7279 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7280 | { | |
7281 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7282 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7283 | ||
7284 | return &data->tags[table_idx][off]; | |
7285 | } | |
7286 | ||
44b31f2f | 7287 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7288 | { |
2d091d62 PB |
7289 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7290 | ||
7291 | if (data->tags) | |
7292 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7293 | kfree(data); |
7294 | } | |
7295 | ||
d878c816 PB |
7296 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7297 | u64 __user *utags, unsigned nr, | |
7298 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7299 | { |
b895c9a6 | 7300 | struct io_rsrc_data *data; |
2d091d62 | 7301 | int ret = -ENOMEM; |
d878c816 | 7302 | unsigned i; |
1ad555c6 BM |
7303 | |
7304 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7305 | if (!data) | |
d878c816 | 7306 | return -ENOMEM; |
2d091d62 | 7307 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7308 | if (!data->tags) { |
1ad555c6 | 7309 | kfree(data); |
d878c816 PB |
7310 | return -ENOMEM; |
7311 | } | |
2d091d62 PB |
7312 | |
7313 | data->nr = nr; | |
7314 | data->ctx = ctx; | |
7315 | data->do_put = do_put; | |
d878c816 | 7316 | if (utags) { |
2d091d62 | 7317 | ret = -EFAULT; |
d878c816 | 7318 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7319 | u64 *tag_slot = io_get_tag_slot(data, i); |
7320 | ||
7321 | if (copy_from_user(tag_slot, &utags[i], | |
7322 | sizeof(*tag_slot))) | |
2d091d62 | 7323 | goto fail; |
d878c816 | 7324 | } |
1ad555c6 | 7325 | } |
b60c8dce | 7326 | |
3e942498 | 7327 | atomic_set(&data->refs, 1); |
1ad555c6 | 7328 | init_completion(&data->done); |
d878c816 PB |
7329 | *pdata = data; |
7330 | return 0; | |
2d091d62 PB |
7331 | fail: |
7332 | io_rsrc_data_free(data); | |
7333 | return ret; | |
1ad555c6 BM |
7334 | } |
7335 | ||
9123c8ff PB |
7336 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7337 | { | |
042b0d85 | 7338 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), GFP_KERNEL); |
9123c8ff PB |
7339 | return !!table->files; |
7340 | } | |
7341 | ||
042b0d85 | 7342 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7343 | { |
042b0d85 | 7344 | kvfree(table->files); |
9123c8ff PB |
7345 | table->files = NULL; |
7346 | } | |
7347 | ||
fff4db76 | 7348 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7349 | { |
fff4db76 PB |
7350 | #if defined(CONFIG_UNIX) |
7351 | if (ctx->ring_sock) { | |
7352 | struct sock *sock = ctx->ring_sock->sk; | |
7353 | struct sk_buff *skb; | |
7354 | ||
7355 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7356 | kfree_skb(skb); | |
7357 | } | |
7358 | #else | |
7359 | int i; | |
7360 | ||
7361 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7362 | struct file *file; | |
7363 | ||
7364 | file = io_file_from_index(ctx, i); | |
7365 | if (file) | |
7366 | fput(file); | |
7367 | } | |
7368 | #endif | |
042b0d85 | 7369 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7370 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7371 | ctx->file_data = NULL; |
7372 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7373 | } |
7374 | ||
d7954b2b BM |
7375 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7376 | { | |
d7954b2b BM |
7377 | int ret; |
7378 | ||
08480400 | 7379 | if (!ctx->file_data) |
d7954b2b | 7380 | return -ENXIO; |
08480400 PB |
7381 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7382 | if (!ret) | |
7383 | __io_sqe_files_unregister(ctx); | |
7384 | return ret; | |
6b06314c JA |
7385 | } |
7386 | ||
37d1e2e3 | 7387 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7388 | __releases(&sqd->lock) |
37d1e2e3 | 7389 | { |
521d6a73 PB |
7390 | WARN_ON_ONCE(sqd->thread == current); |
7391 | ||
9e138a48 PB |
7392 | /* |
7393 | * Do the dance but not conditional clear_bit() because it'd race with | |
7394 | * other threads incrementing park_pending and setting the bit. | |
7395 | */ | |
37d1e2e3 | 7396 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7397 | if (atomic_dec_return(&sqd->park_pending)) |
7398 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7399 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7400 | } |
7401 | ||
86e0d676 | 7402 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7403 | __acquires(&sqd->lock) |
37d1e2e3 | 7404 | { |
521d6a73 PB |
7405 | WARN_ON_ONCE(sqd->thread == current); |
7406 | ||
9e138a48 | 7407 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7408 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7409 | mutex_lock(&sqd->lock); |
05962f95 | 7410 | if (sqd->thread) |
86e0d676 | 7411 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7412 | } |
7413 | ||
7414 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7415 | { | |
521d6a73 | 7416 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7417 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7418 | |
05962f95 | 7419 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7420 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7421 | if (sqd->thread) |
7422 | wake_up_process(sqd->thread); | |
09a6f4ef | 7423 | mutex_unlock(&sqd->lock); |
05962f95 | 7424 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7425 | } |
7426 | ||
534ca6d6 | 7427 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7428 | { |
534ca6d6 | 7429 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7430 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7431 | ||
37d1e2e3 JA |
7432 | io_sq_thread_stop(sqd); |
7433 | kfree(sqd); | |
7434 | } | |
7435 | } | |
7436 | ||
7437 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7438 | { | |
7439 | struct io_sq_data *sqd = ctx->sq_data; | |
7440 | ||
7441 | if (sqd) { | |
05962f95 | 7442 | io_sq_thread_park(sqd); |
521d6a73 | 7443 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7444 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7445 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7446 | |
7447 | io_put_sq_data(sqd); | |
7448 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7449 | } |
7450 | } | |
7451 | ||
aa06165d JA |
7452 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7453 | { | |
7454 | struct io_ring_ctx *ctx_attach; | |
7455 | struct io_sq_data *sqd; | |
7456 | struct fd f; | |
7457 | ||
7458 | f = fdget(p->wq_fd); | |
7459 | if (!f.file) | |
7460 | return ERR_PTR(-ENXIO); | |
7461 | if (f.file->f_op != &io_uring_fops) { | |
7462 | fdput(f); | |
7463 | return ERR_PTR(-EINVAL); | |
7464 | } | |
7465 | ||
7466 | ctx_attach = f.file->private_data; | |
7467 | sqd = ctx_attach->sq_data; | |
7468 | if (!sqd) { | |
7469 | fdput(f); | |
7470 | return ERR_PTR(-EINVAL); | |
7471 | } | |
5c2469e0 JA |
7472 | if (sqd->task_tgid != current->tgid) { |
7473 | fdput(f); | |
7474 | return ERR_PTR(-EPERM); | |
7475 | } | |
aa06165d JA |
7476 | |
7477 | refcount_inc(&sqd->refs); | |
7478 | fdput(f); | |
7479 | return sqd; | |
7480 | } | |
7481 | ||
26984fbf PB |
7482 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7483 | bool *attached) | |
534ca6d6 JA |
7484 | { |
7485 | struct io_sq_data *sqd; | |
7486 | ||
26984fbf | 7487 | *attached = false; |
5c2469e0 JA |
7488 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7489 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7490 | if (!IS_ERR(sqd)) { |
7491 | *attached = true; | |
5c2469e0 | 7492 | return sqd; |
26984fbf | 7493 | } |
5c2469e0 JA |
7494 | /* fall through for EPERM case, setup new sqd/task */ |
7495 | if (PTR_ERR(sqd) != -EPERM) | |
7496 | return sqd; | |
7497 | } | |
aa06165d | 7498 | |
534ca6d6 JA |
7499 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7500 | if (!sqd) | |
7501 | return ERR_PTR(-ENOMEM); | |
7502 | ||
9e138a48 | 7503 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7504 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7505 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7506 | mutex_init(&sqd->lock); |
534ca6d6 | 7507 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7508 | init_completion(&sqd->exited); |
534ca6d6 JA |
7509 | return sqd; |
7510 | } | |
7511 | ||
6b06314c | 7512 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7513 | /* |
7514 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7515 | * the io_uring can be safely unregistered on process exit, even if we have | |
7516 | * loops in the file referencing. | |
7517 | */ | |
7518 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7519 | { | |
7520 | struct sock *sk = ctx->ring_sock->sk; | |
7521 | struct scm_fp_list *fpl; | |
7522 | struct sk_buff *skb; | |
08a45173 | 7523 | int i, nr_files; |
6b06314c | 7524 | |
6b06314c JA |
7525 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7526 | if (!fpl) | |
7527 | return -ENOMEM; | |
7528 | ||
7529 | skb = alloc_skb(0, GFP_KERNEL); | |
7530 | if (!skb) { | |
7531 | kfree(fpl); | |
7532 | return -ENOMEM; | |
7533 | } | |
7534 | ||
7535 | skb->sk = sk; | |
6b06314c | 7536 | |
08a45173 | 7537 | nr_files = 0; |
62e398be | 7538 | fpl->user = get_uid(current_user()); |
6b06314c | 7539 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7540 | struct file *file = io_file_from_index(ctx, i + offset); |
7541 | ||
7542 | if (!file) | |
08a45173 | 7543 | continue; |
65e19f54 | 7544 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7545 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7546 | nr_files++; | |
6b06314c JA |
7547 | } |
7548 | ||
08a45173 JA |
7549 | if (nr_files) { |
7550 | fpl->max = SCM_MAX_FD; | |
7551 | fpl->count = nr_files; | |
7552 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7553 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7554 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7555 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7556 | |
08a45173 JA |
7557 | for (i = 0; i < nr_files; i++) |
7558 | fput(fpl->fp[i]); | |
7559 | } else { | |
7560 | kfree_skb(skb); | |
7561 | kfree(fpl); | |
7562 | } | |
6b06314c JA |
7563 | |
7564 | return 0; | |
7565 | } | |
7566 | ||
7567 | /* | |
7568 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7569 | * causes regular reference counting to break down. We rely on the UNIX | |
7570 | * garbage collection to take care of this problem for us. | |
7571 | */ | |
7572 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7573 | { | |
7574 | unsigned left, total; | |
7575 | int ret = 0; | |
7576 | ||
7577 | total = 0; | |
7578 | left = ctx->nr_user_files; | |
7579 | while (left) { | |
7580 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7581 | |
7582 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7583 | if (ret) | |
7584 | break; | |
7585 | left -= this_files; | |
7586 | total += this_files; | |
7587 | } | |
7588 | ||
7589 | if (!ret) | |
7590 | return 0; | |
7591 | ||
7592 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7593 | struct file *file = io_file_from_index(ctx, total); |
7594 | ||
7595 | if (file) | |
7596 | fput(file); | |
6b06314c JA |
7597 | total++; |
7598 | } | |
7599 | ||
7600 | return ret; | |
7601 | } | |
7602 | #else | |
7603 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7604 | { | |
7605 | return 0; | |
7606 | } | |
7607 | #endif | |
7608 | ||
47e90392 | 7609 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7610 | { |
50238531 | 7611 | struct file *file = prsrc->file; |
05f3fb3c JA |
7612 | #if defined(CONFIG_UNIX) |
7613 | struct sock *sock = ctx->ring_sock->sk; | |
7614 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7615 | struct sk_buff *skb; | |
7616 | int i; | |
7617 | ||
7618 | __skb_queue_head_init(&list); | |
7619 | ||
7620 | /* | |
7621 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7622 | * remove this entry and rearrange the file array. | |
7623 | */ | |
7624 | skb = skb_dequeue(head); | |
7625 | while (skb) { | |
7626 | struct scm_fp_list *fp; | |
7627 | ||
7628 | fp = UNIXCB(skb).fp; | |
7629 | for (i = 0; i < fp->count; i++) { | |
7630 | int left; | |
7631 | ||
7632 | if (fp->fp[i] != file) | |
7633 | continue; | |
7634 | ||
7635 | unix_notinflight(fp->user, fp->fp[i]); | |
7636 | left = fp->count - 1 - i; | |
7637 | if (left) { | |
7638 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7639 | left * sizeof(struct file *)); | |
7640 | } | |
7641 | fp->count--; | |
7642 | if (!fp->count) { | |
7643 | kfree_skb(skb); | |
7644 | skb = NULL; | |
7645 | } else { | |
7646 | __skb_queue_tail(&list, skb); | |
7647 | } | |
7648 | fput(file); | |
7649 | file = NULL; | |
7650 | break; | |
7651 | } | |
7652 | ||
7653 | if (!file) | |
7654 | break; | |
7655 | ||
7656 | __skb_queue_tail(&list, skb); | |
7657 | ||
7658 | skb = skb_dequeue(head); | |
7659 | } | |
7660 | ||
7661 | if (skb_peek(&list)) { | |
7662 | spin_lock_irq(&head->lock); | |
7663 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7664 | __skb_queue_tail(head, skb); | |
7665 | spin_unlock_irq(&head->lock); | |
7666 | } | |
7667 | #else | |
7668 | fput(file); | |
7669 | #endif | |
7670 | } | |
7671 | ||
b895c9a6 | 7672 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7673 | { |
b895c9a6 | 7674 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7675 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7676 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7677 | |
269bbe5f BM |
7678 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7679 | list_del(&prsrc->list); | |
b60c8dce PB |
7680 | |
7681 | if (prsrc->tag) { | |
7682 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
7683 | |
7684 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 7685 | spin_lock(&ctx->completion_lock); |
b60c8dce | 7686 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 7687 | ctx->cq_extra++; |
b60c8dce | 7688 | io_commit_cqring(ctx); |
79ebeaee | 7689 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
7690 | io_cqring_ev_posted(ctx); |
7691 | io_ring_submit_unlock(ctx, lock_ring); | |
7692 | } | |
7693 | ||
40ae0ff7 | 7694 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7695 | kfree(prsrc); |
65e19f54 | 7696 | } |
05589553 | 7697 | |
28a9fe25 | 7698 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
7699 | if (atomic_dec_and_test(&rsrc_data->refs)) |
7700 | complete(&rsrc_data->done); | |
2faf852d | 7701 | } |
65e19f54 | 7702 | |
269bbe5f | 7703 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7704 | { |
7705 | struct io_ring_ctx *ctx; | |
7706 | struct llist_node *node; | |
7707 | ||
269bbe5f BM |
7708 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7709 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7710 | |
7711 | while (node) { | |
b895c9a6 | 7712 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7713 | struct llist_node *next = node->next; |
7714 | ||
b895c9a6 | 7715 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7716 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7717 | node = next; |
7718 | } | |
7719 | } | |
7720 | ||
6b06314c | 7721 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 7722 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
7723 | { |
7724 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7725 | struct file *file; |
f3baed39 | 7726 | int fd, ret; |
846a4ef2 | 7727 | unsigned i; |
6b06314c | 7728 | |
05f3fb3c | 7729 | if (ctx->file_data) |
6b06314c JA |
7730 | return -EBUSY; |
7731 | if (!nr_args) | |
7732 | return -EINVAL; | |
7733 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7734 | return -EMFILE; | |
a7f0ed5a | 7735 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7736 | if (ret) |
7737 | return ret; | |
d878c816 PB |
7738 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
7739 | &ctx->file_data); | |
7740 | if (ret) | |
7741 | return ret; | |
6b06314c | 7742 | |
f3baed39 | 7743 | ret = -ENOMEM; |
aeca241b | 7744 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 7745 | goto out_free; |
65e19f54 | 7746 | |
08a45173 | 7747 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 7748 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
7749 | ret = -EFAULT; |
7750 | goto out_fput; | |
7751 | } | |
08a45173 | 7752 | /* allow sparse sets */ |
792e3582 PB |
7753 | if (fd == -1) { |
7754 | ret = -EINVAL; | |
2d091d62 | 7755 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 7756 | goto out_fput; |
08a45173 | 7757 | continue; |
792e3582 | 7758 | } |
6b06314c | 7759 | |
05f3fb3c | 7760 | file = fget(fd); |
6b06314c | 7761 | ret = -EBADF; |
792e3582 | 7762 | if (unlikely(!file)) |
600cf3f8 | 7763 | goto out_fput; |
05f3fb3c | 7764 | |
6b06314c JA |
7765 | /* |
7766 | * Don't allow io_uring instances to be registered. If UNIX | |
7767 | * isn't enabled, then this causes a reference cycle and this | |
7768 | * instance can never get freed. If UNIX is enabled we'll | |
7769 | * handle it just fine, but there's still no point in allowing | |
7770 | * a ring fd as it doesn't support regular read/write anyway. | |
7771 | */ | |
05f3fb3c JA |
7772 | if (file->f_op == &io_uring_fops) { |
7773 | fput(file); | |
600cf3f8 | 7774 | goto out_fput; |
6b06314c | 7775 | } |
aeca241b | 7776 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
7777 | } |
7778 | ||
6b06314c | 7779 | ret = io_sqe_files_scm(ctx); |
05589553 | 7780 | if (ret) { |
08480400 | 7781 | __io_sqe_files_unregister(ctx); |
05589553 XW |
7782 | return ret; |
7783 | } | |
6b06314c | 7784 | |
a7f0ed5a | 7785 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7786 | return ret; |
600cf3f8 PB |
7787 | out_fput: |
7788 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7789 | file = io_file_from_index(ctx, i); | |
7790 | if (file) | |
7791 | fput(file); | |
7792 | } | |
042b0d85 | 7793 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 7794 | ctx->nr_user_files = 0; |
600cf3f8 | 7795 | out_free: |
44b31f2f | 7796 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 7797 | ctx->file_data = NULL; |
6b06314c JA |
7798 | return ret; |
7799 | } | |
7800 | ||
c3a31e60 JA |
7801 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7802 | int index) | |
7803 | { | |
7804 | #if defined(CONFIG_UNIX) | |
7805 | struct sock *sock = ctx->ring_sock->sk; | |
7806 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7807 | struct sk_buff *skb; | |
7808 | ||
7809 | /* | |
7810 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7811 | * file set. If there's no room, fall back to allocating a new skb | |
7812 | * and filling it in. | |
7813 | */ | |
7814 | spin_lock_irq(&head->lock); | |
7815 | skb = skb_peek(head); | |
7816 | if (skb) { | |
7817 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7818 | ||
7819 | if (fpl->count < SCM_MAX_FD) { | |
7820 | __skb_unlink(skb, head); | |
7821 | spin_unlock_irq(&head->lock); | |
7822 | fpl->fp[fpl->count] = get_file(file); | |
7823 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7824 | fpl->count++; | |
7825 | spin_lock_irq(&head->lock); | |
7826 | __skb_queue_head(head, skb); | |
7827 | } else { | |
7828 | skb = NULL; | |
7829 | } | |
7830 | } | |
7831 | spin_unlock_irq(&head->lock); | |
7832 | ||
7833 | if (skb) { | |
7834 | fput(file); | |
7835 | return 0; | |
7836 | } | |
7837 | ||
7838 | return __io_sqe_files_scm(ctx, 1, index); | |
7839 | #else | |
7840 | return 0; | |
7841 | #endif | |
7842 | } | |
7843 | ||
b60c8dce | 7844 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
e7c78371 | 7845 | struct io_rsrc_node *node, void *rsrc) |
05f3fb3c | 7846 | { |
269bbe5f | 7847 | struct io_rsrc_put *prsrc; |
05f3fb3c | 7848 | |
269bbe5f BM |
7849 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7850 | if (!prsrc) | |
a5318d3c | 7851 | return -ENOMEM; |
05f3fb3c | 7852 | |
2d091d62 | 7853 | prsrc->tag = *io_get_tag_slot(data, idx); |
50238531 | 7854 | prsrc->rsrc = rsrc; |
e7c78371 | 7855 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 7856 | return 0; |
05f3fb3c JA |
7857 | } |
7858 | ||
7859 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
c3bdad02 | 7860 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
7861 | unsigned nr_args) |
7862 | { | |
c3bdad02 | 7863 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 7864 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 7865 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
7866 | struct io_fixed_file *file_slot; |
7867 | struct file *file; | |
98f0b3b4 PB |
7868 | int fd, i, err = 0; |
7869 | unsigned int done; | |
05589553 | 7870 | bool needs_switch = false; |
c3a31e60 | 7871 | |
98f0b3b4 PB |
7872 | if (!ctx->file_data) |
7873 | return -ENXIO; | |
7874 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
7875 | return -EINVAL; |
7876 | ||
67973b93 | 7877 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
7878 | u64 tag = 0; |
7879 | ||
7880 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
7881 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
7882 | err = -EFAULT; |
7883 | break; | |
7884 | } | |
c3bdad02 PB |
7885 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
7886 | err = -EINVAL; | |
7887 | break; | |
7888 | } | |
4e0377a1 | 7889 | if (fd == IORING_REGISTER_FILES_SKIP) |
7890 | continue; | |
7891 | ||
67973b93 | 7892 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 7893 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 7894 | |
a04b0ac0 PB |
7895 | if (file_slot->file_ptr) { |
7896 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
7897 | err = io_queue_rsrc_removal(data, up->offset + done, |
7898 | ctx->rsrc_node, file); | |
a5318d3c HD |
7899 | if (err) |
7900 | break; | |
a04b0ac0 | 7901 | file_slot->file_ptr = 0; |
05589553 | 7902 | needs_switch = true; |
c3a31e60 JA |
7903 | } |
7904 | if (fd != -1) { | |
c3a31e60 JA |
7905 | file = fget(fd); |
7906 | if (!file) { | |
7907 | err = -EBADF; | |
7908 | break; | |
7909 | } | |
7910 | /* | |
7911 | * Don't allow io_uring instances to be registered. If | |
7912 | * UNIX isn't enabled, then this causes a reference | |
7913 | * cycle and this instance can never get freed. If UNIX | |
7914 | * is enabled we'll handle it just fine, but there's | |
7915 | * still no point in allowing a ring fd as it doesn't | |
7916 | * support regular read/write anyway. | |
7917 | */ | |
7918 | if (file->f_op == &io_uring_fops) { | |
7919 | fput(file); | |
7920 | err = -EBADF; | |
7921 | break; | |
7922 | } | |
2d091d62 | 7923 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 7924 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 7925 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7926 | if (err) { |
a04b0ac0 | 7927 | file_slot->file_ptr = 0; |
f3bd9dae | 7928 | fput(file); |
c3a31e60 | 7929 | break; |
f3bd9dae | 7930 | } |
c3a31e60 | 7931 | } |
05f3fb3c JA |
7932 | } |
7933 | ||
a7f0ed5a PB |
7934 | if (needs_switch) |
7935 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
7936 | return done ? done : err; |
7937 | } | |
05589553 | 7938 | |
685fe7fe JA |
7939 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
7940 | struct task_struct *task) | |
24369c2e | 7941 | { |
e941894e | 7942 | struct io_wq_hash *hash; |
24369c2e | 7943 | struct io_wq_data data; |
24369c2e | 7944 | unsigned int concurrency; |
24369c2e | 7945 | |
362a9e65 | 7946 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
7947 | hash = ctx->hash_map; |
7948 | if (!hash) { | |
7949 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
7950 | if (!hash) { |
7951 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 7952 | return ERR_PTR(-ENOMEM); |
362a9e65 | 7953 | } |
e941894e JA |
7954 | refcount_set(&hash->refs, 1); |
7955 | init_waitqueue_head(&hash->wait); | |
7956 | ctx->hash_map = hash; | |
24369c2e | 7957 | } |
362a9e65 | 7958 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 7959 | |
e941894e | 7960 | data.hash = hash; |
685fe7fe | 7961 | data.task = task; |
ebc11b6c | 7962 | data.free_work = io_wq_free_work; |
f5fa38c5 | 7963 | data.do_work = io_wq_submit_work; |
24369c2e | 7964 | |
d25e3a3d JA |
7965 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
7966 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 7967 | |
5aa75ed5 | 7968 | return io_wq_create(concurrency, &data); |
24369c2e PB |
7969 | } |
7970 | ||
5aa75ed5 JA |
7971 | static int io_uring_alloc_task_context(struct task_struct *task, |
7972 | struct io_ring_ctx *ctx) | |
0f212204 JA |
7973 | { |
7974 | struct io_uring_task *tctx; | |
d8a6df10 | 7975 | int ret; |
0f212204 | 7976 | |
09899b19 | 7977 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
7978 | if (unlikely(!tctx)) |
7979 | return -ENOMEM; | |
7980 | ||
d8a6df10 JA |
7981 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
7982 | if (unlikely(ret)) { | |
7983 | kfree(tctx); | |
7984 | return ret; | |
7985 | } | |
7986 | ||
685fe7fe | 7987 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
7988 | if (IS_ERR(tctx->io_wq)) { |
7989 | ret = PTR_ERR(tctx->io_wq); | |
7990 | percpu_counter_destroy(&tctx->inflight); | |
7991 | kfree(tctx); | |
7992 | return ret; | |
7993 | } | |
7994 | ||
0f212204 JA |
7995 | xa_init(&tctx->xa); |
7996 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 7997 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 7998 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 7999 | task->io_uring = tctx; |
7cbf1722 JA |
8000 | spin_lock_init(&tctx->task_lock); |
8001 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8002 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8003 | return 0; |
8004 | } | |
8005 | ||
8006 | void __io_uring_free(struct task_struct *tsk) | |
8007 | { | |
8008 | struct io_uring_task *tctx = tsk->io_uring; | |
8009 | ||
8010 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8011 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8012 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8013 | |
d8a6df10 | 8014 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8015 | kfree(tctx); |
8016 | tsk->io_uring = NULL; | |
8017 | } | |
8018 | ||
7e84e1c7 SG |
8019 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8020 | struct io_uring_params *p) | |
2b188cc1 JA |
8021 | { |
8022 | int ret; | |
8023 | ||
d25e3a3d JA |
8024 | /* Retain compatibility with failing for an invalid attach attempt */ |
8025 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8026 | IORING_SETUP_ATTACH_WQ) { | |
8027 | struct fd f; | |
8028 | ||
8029 | f = fdget(p->wq_fd); | |
8030 | if (!f.file) | |
8031 | return -ENXIO; | |
0cc936f7 JA |
8032 | if (f.file->f_op != &io_uring_fops) { |
8033 | fdput(f); | |
f2a48dd0 | 8034 | return -EINVAL; |
0cc936f7 JA |
8035 | } |
8036 | fdput(f); | |
d25e3a3d | 8037 | } |
6c271ce2 | 8038 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8039 | struct task_struct *tsk; |
534ca6d6 | 8040 | struct io_sq_data *sqd; |
26984fbf | 8041 | bool attached; |
534ca6d6 | 8042 | |
26984fbf | 8043 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8044 | if (IS_ERR(sqd)) { |
8045 | ret = PTR_ERR(sqd); | |
8046 | goto err; | |
8047 | } | |
69fb2131 | 8048 | |
7c30f36a | 8049 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8050 | ctx->sq_data = sqd; |
917257da JA |
8051 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8052 | if (!ctx->sq_thread_idle) | |
8053 | ctx->sq_thread_idle = HZ; | |
8054 | ||
78d7f6ba | 8055 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8056 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8057 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8058 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8059 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8060 | io_sq_thread_unpark(sqd); |
8061 | ||
de75a3d3 PB |
8062 | if (ret < 0) |
8063 | goto err; | |
8064 | if (attached) | |
5aa75ed5 | 8065 | return 0; |
aa06165d | 8066 | |
6c271ce2 | 8067 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8068 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8069 | |
917257da | 8070 | ret = -EINVAL; |
f2a48dd0 | 8071 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8072 | goto err_sqpoll; |
37d1e2e3 | 8073 | sqd->sq_cpu = cpu; |
6c271ce2 | 8074 | } else { |
37d1e2e3 | 8075 | sqd->sq_cpu = -1; |
6c271ce2 | 8076 | } |
37d1e2e3 JA |
8077 | |
8078 | sqd->task_pid = current->pid; | |
5c2469e0 | 8079 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8080 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8081 | if (IS_ERR(tsk)) { | |
8082 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8083 | goto err_sqpoll; |
6c271ce2 | 8084 | } |
97a73a0f | 8085 | |
46fe18b1 | 8086 | sqd->thread = tsk; |
97a73a0f | 8087 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8088 | wake_up_new_task(tsk); |
0f212204 JA |
8089 | if (ret) |
8090 | goto err; | |
6c271ce2 JA |
8091 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8092 | /* Can't have SQ_AFF without SQPOLL */ | |
8093 | ret = -EINVAL; | |
8094 | goto err; | |
8095 | } | |
8096 | ||
2b188cc1 | 8097 | return 0; |
f2a48dd0 PB |
8098 | err_sqpoll: |
8099 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8100 | err: |
37d1e2e3 | 8101 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8102 | return ret; |
8103 | } | |
8104 | ||
a087e2b5 BM |
8105 | static inline void __io_unaccount_mem(struct user_struct *user, |
8106 | unsigned long nr_pages) | |
2b188cc1 JA |
8107 | { |
8108 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8109 | } | |
8110 | ||
a087e2b5 BM |
8111 | static inline int __io_account_mem(struct user_struct *user, |
8112 | unsigned long nr_pages) | |
2b188cc1 JA |
8113 | { |
8114 | unsigned long page_limit, cur_pages, new_pages; | |
8115 | ||
8116 | /* Don't allow more pages than we can safely lock */ | |
8117 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8118 | ||
8119 | do { | |
8120 | cur_pages = atomic_long_read(&user->locked_vm); | |
8121 | new_pages = cur_pages + nr_pages; | |
8122 | if (new_pages > page_limit) | |
8123 | return -ENOMEM; | |
8124 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8125 | new_pages) != cur_pages); | |
8126 | ||
8127 | return 0; | |
8128 | } | |
8129 | ||
26bfa89e | 8130 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8131 | { |
62e398be | 8132 | if (ctx->user) |
a087e2b5 | 8133 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8134 | |
26bfa89e JA |
8135 | if (ctx->mm_account) |
8136 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8137 | } |
8138 | ||
26bfa89e | 8139 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8140 | { |
30975825 BM |
8141 | int ret; |
8142 | ||
62e398be | 8143 | if (ctx->user) { |
30975825 BM |
8144 | ret = __io_account_mem(ctx->user, nr_pages); |
8145 | if (ret) | |
8146 | return ret; | |
8147 | } | |
8148 | ||
26bfa89e JA |
8149 | if (ctx->mm_account) |
8150 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8151 | |
8152 | return 0; | |
8153 | } | |
8154 | ||
2b188cc1 JA |
8155 | static void io_mem_free(void *ptr) |
8156 | { | |
52e04ef4 MR |
8157 | struct page *page; |
8158 | ||
8159 | if (!ptr) | |
8160 | return; | |
2b188cc1 | 8161 | |
52e04ef4 | 8162 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8163 | if (put_page_testzero(page)) |
8164 | free_compound_page(page); | |
8165 | } | |
8166 | ||
8167 | static void *io_mem_alloc(size_t size) | |
8168 | { | |
8169 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8170 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8171 | |
8172 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8173 | } | |
8174 | ||
75b28aff HV |
8175 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8176 | size_t *sq_offset) | |
8177 | { | |
8178 | struct io_rings *rings; | |
8179 | size_t off, sq_array_size; | |
8180 | ||
8181 | off = struct_size(rings, cqes, cq_entries); | |
8182 | if (off == SIZE_MAX) | |
8183 | return SIZE_MAX; | |
8184 | ||
8185 | #ifdef CONFIG_SMP | |
8186 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8187 | if (off == 0) | |
8188 | return SIZE_MAX; | |
8189 | #endif | |
8190 | ||
b36200f5 DV |
8191 | if (sq_offset) |
8192 | *sq_offset = off; | |
8193 | ||
75b28aff HV |
8194 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8195 | if (sq_array_size == SIZE_MAX) | |
8196 | return SIZE_MAX; | |
8197 | ||
8198 | if (check_add_overflow(off, sq_array_size, &off)) | |
8199 | return SIZE_MAX; | |
8200 | ||
75b28aff HV |
8201 | return off; |
8202 | } | |
8203 | ||
41edf1a5 | 8204 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8205 | { |
41edf1a5 | 8206 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8207 | unsigned int i; |
8208 | ||
6224843d PB |
8209 | if (imu != ctx->dummy_ubuf) { |
8210 | for (i = 0; i < imu->nr_bvecs; i++) | |
8211 | unpin_user_page(imu->bvec[i].bv_page); | |
8212 | if (imu->acct_pages) | |
8213 | io_unaccount_mem(ctx, imu->acct_pages); | |
8214 | kvfree(imu); | |
8215 | } | |
41edf1a5 | 8216 | *slot = NULL; |
7f61a1e9 PB |
8217 | } |
8218 | ||
bd54b6fe | 8219 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8220 | { |
634d00df PB |
8221 | io_buffer_unmap(ctx, &prsrc->buf); |
8222 | prsrc->buf = NULL; | |
bd54b6fe | 8223 | } |
edafccee | 8224 | |
bd54b6fe BM |
8225 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8226 | { | |
8227 | unsigned int i; | |
edafccee | 8228 | |
7f61a1e9 PB |
8229 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8230 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8231 | kfree(ctx->user_bufs); |
bb6659cc | 8232 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8233 | ctx->user_bufs = NULL; |
bd54b6fe | 8234 | ctx->buf_data = NULL; |
edafccee | 8235 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8236 | } |
8237 | ||
0a96bbe4 | 8238 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8239 | { |
bd54b6fe | 8240 | int ret; |
edafccee | 8241 | |
bd54b6fe | 8242 | if (!ctx->buf_data) |
edafccee JA |
8243 | return -ENXIO; |
8244 | ||
bd54b6fe BM |
8245 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8246 | if (!ret) | |
8247 | __io_sqe_buffers_unregister(ctx); | |
8248 | return ret; | |
edafccee JA |
8249 | } |
8250 | ||
8251 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8252 | void __user *arg, unsigned index) | |
8253 | { | |
8254 | struct iovec __user *src; | |
8255 | ||
8256 | #ifdef CONFIG_COMPAT | |
8257 | if (ctx->compat) { | |
8258 | struct compat_iovec __user *ciovs; | |
8259 | struct compat_iovec ciov; | |
8260 | ||
8261 | ciovs = (struct compat_iovec __user *) arg; | |
8262 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8263 | return -EFAULT; | |
8264 | ||
d55e5f5b | 8265 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8266 | dst->iov_len = ciov.iov_len; |
8267 | return 0; | |
8268 | } | |
8269 | #endif | |
8270 | src = (struct iovec __user *) arg; | |
8271 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8272 | return -EFAULT; | |
8273 | return 0; | |
8274 | } | |
8275 | ||
de293938 JA |
8276 | /* |
8277 | * Not super efficient, but this is just a registration time. And we do cache | |
8278 | * the last compound head, so generally we'll only do a full search if we don't | |
8279 | * match that one. | |
8280 | * | |
8281 | * We check if the given compound head page has already been accounted, to | |
8282 | * avoid double accounting it. This allows us to account the full size of the | |
8283 | * page, not just the constituent pages of a huge page. | |
8284 | */ | |
8285 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8286 | int nr_pages, struct page *hpage) | |
8287 | { | |
8288 | int i, j; | |
8289 | ||
8290 | /* check current page array */ | |
8291 | for (i = 0; i < nr_pages; i++) { | |
8292 | if (!PageCompound(pages[i])) | |
8293 | continue; | |
8294 | if (compound_head(pages[i]) == hpage) | |
8295 | return true; | |
8296 | } | |
8297 | ||
8298 | /* check previously registered pages */ | |
8299 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8300 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8301 | |
8302 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8303 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8304 | continue; | |
8305 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8306 | return true; | |
8307 | } | |
8308 | } | |
8309 | ||
8310 | return false; | |
8311 | } | |
8312 | ||
8313 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8314 | int nr_pages, struct io_mapped_ubuf *imu, | |
8315 | struct page **last_hpage) | |
8316 | { | |
8317 | int i, ret; | |
8318 | ||
216e5835 | 8319 | imu->acct_pages = 0; |
de293938 JA |
8320 | for (i = 0; i < nr_pages; i++) { |
8321 | if (!PageCompound(pages[i])) { | |
8322 | imu->acct_pages++; | |
8323 | } else { | |
8324 | struct page *hpage; | |
8325 | ||
8326 | hpage = compound_head(pages[i]); | |
8327 | if (hpage == *last_hpage) | |
8328 | continue; | |
8329 | *last_hpage = hpage; | |
8330 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8331 | continue; | |
8332 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8333 | } | |
8334 | } | |
8335 | ||
8336 | if (!imu->acct_pages) | |
8337 | return 0; | |
8338 | ||
26bfa89e | 8339 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8340 | if (ret) |
8341 | imu->acct_pages = 0; | |
8342 | return ret; | |
8343 | } | |
8344 | ||
0a96bbe4 | 8345 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8346 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8347 | struct page **last_hpage) |
edafccee | 8348 | { |
41edf1a5 | 8349 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8350 | struct vm_area_struct **vmas = NULL; |
8351 | struct page **pages = NULL; | |
0a96bbe4 BM |
8352 | unsigned long off, start, end, ubuf; |
8353 | size_t size; | |
8354 | int ret, pret, nr_pages, i; | |
8355 | ||
6224843d PB |
8356 | if (!iov->iov_base) { |
8357 | *pimu = ctx->dummy_ubuf; | |
8358 | return 0; | |
8359 | } | |
8360 | ||
0a96bbe4 BM |
8361 | ubuf = (unsigned long) iov->iov_base; |
8362 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8363 | start = ubuf >> PAGE_SHIFT; | |
8364 | nr_pages = end - start; | |
8365 | ||
41edf1a5 | 8366 | *pimu = NULL; |
0a96bbe4 BM |
8367 | ret = -ENOMEM; |
8368 | ||
8369 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8370 | if (!pages) | |
8371 | goto done; | |
8372 | ||
8373 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8374 | GFP_KERNEL); | |
8375 | if (!vmas) | |
8376 | goto done; | |
edafccee | 8377 | |
41edf1a5 | 8378 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8379 | if (!imu) |
0a96bbe4 BM |
8380 | goto done; |
8381 | ||
8382 | ret = 0; | |
8383 | mmap_read_lock(current->mm); | |
8384 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8385 | pages, vmas); | |
8386 | if (pret == nr_pages) { | |
8387 | /* don't support file backed memory */ | |
8388 | for (i = 0; i < nr_pages; i++) { | |
8389 | struct vm_area_struct *vma = vmas[i]; | |
8390 | ||
40dad765 PB |
8391 | if (vma_is_shmem(vma)) |
8392 | continue; | |
0a96bbe4 BM |
8393 | if (vma->vm_file && |
8394 | !is_file_hugepages(vma->vm_file)) { | |
8395 | ret = -EOPNOTSUPP; | |
8396 | break; | |
8397 | } | |
8398 | } | |
8399 | } else { | |
8400 | ret = pret < 0 ? pret : -EFAULT; | |
8401 | } | |
8402 | mmap_read_unlock(current->mm); | |
8403 | if (ret) { | |
8404 | /* | |
8405 | * if we did partial map, or found file backed vmas, | |
8406 | * release any pages we did get | |
8407 | */ | |
8408 | if (pret > 0) | |
8409 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8410 | goto done; |
8411 | } | |
8412 | ||
8413 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8414 | if (ret) { | |
8415 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8416 | goto done; |
8417 | } | |
8418 | ||
8419 | off = ubuf & ~PAGE_MASK; | |
8420 | size = iov->iov_len; | |
8421 | for (i = 0; i < nr_pages; i++) { | |
8422 | size_t vec_len; | |
8423 | ||
8424 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8425 | imu->bvec[i].bv_page = pages[i]; | |
8426 | imu->bvec[i].bv_len = vec_len; | |
8427 | imu->bvec[i].bv_offset = off; | |
8428 | off = 0; | |
8429 | size -= vec_len; | |
8430 | } | |
8431 | /* store original address for later verification */ | |
8432 | imu->ubuf = ubuf; | |
4751f53d | 8433 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8434 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8435 | *pimu = imu; |
0a96bbe4 BM |
8436 | ret = 0; |
8437 | done: | |
41edf1a5 PB |
8438 | if (ret) |
8439 | kvfree(imu); | |
0a96bbe4 BM |
8440 | kvfree(pages); |
8441 | kvfree(vmas); | |
8442 | return ret; | |
8443 | } | |
8444 | ||
2b358604 | 8445 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8446 | { |
87094465 PB |
8447 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8448 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8449 | } |
edafccee | 8450 | |
2b358604 BM |
8451 | static int io_buffer_validate(struct iovec *iov) |
8452 | { | |
50e96989 PB |
8453 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8454 | ||
2b358604 BM |
8455 | /* |
8456 | * Don't impose further limits on the size and buffer | |
8457 | * constraints here, we'll -EINVAL later when IO is | |
8458 | * submitted if they are wrong. | |
8459 | */ | |
6224843d PB |
8460 | if (!iov->iov_base) |
8461 | return iov->iov_len ? -EFAULT : 0; | |
8462 | if (!iov->iov_len) | |
2b358604 | 8463 | return -EFAULT; |
edafccee | 8464 | |
2b358604 BM |
8465 | /* arbitrary limit, but we need something */ |
8466 | if (iov->iov_len > SZ_1G) | |
8467 | return -EFAULT; | |
edafccee | 8468 | |
50e96989 PB |
8469 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8470 | return -EOVERFLOW; | |
8471 | ||
2b358604 BM |
8472 | return 0; |
8473 | } | |
edafccee | 8474 | |
2b358604 | 8475 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 8476 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 8477 | { |
bd54b6fe BM |
8478 | struct page *last_hpage = NULL; |
8479 | struct io_rsrc_data *data; | |
2b358604 BM |
8480 | int i, ret; |
8481 | struct iovec iov; | |
edafccee | 8482 | |
87094465 PB |
8483 | if (ctx->user_bufs) |
8484 | return -EBUSY; | |
489809e2 | 8485 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 8486 | return -EINVAL; |
bd54b6fe | 8487 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
8488 | if (ret) |
8489 | return ret; | |
d878c816 PB |
8490 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
8491 | if (ret) | |
8492 | return ret; | |
bd54b6fe BM |
8493 | ret = io_buffers_map_alloc(ctx, nr_args); |
8494 | if (ret) { | |
bb6659cc | 8495 | io_rsrc_data_free(data); |
bd54b6fe BM |
8496 | return ret; |
8497 | } | |
edafccee | 8498 | |
87094465 | 8499 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
8500 | ret = io_copy_iov(ctx, &iov, arg, i); |
8501 | if (ret) | |
0a96bbe4 | 8502 | break; |
2b358604 BM |
8503 | ret = io_buffer_validate(&iov); |
8504 | if (ret) | |
0a96bbe4 | 8505 | break; |
2d091d62 | 8506 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
8507 | ret = -EINVAL; |
8508 | break; | |
8509 | } | |
edafccee | 8510 | |
41edf1a5 PB |
8511 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
8512 | &last_hpage); | |
0a96bbe4 BM |
8513 | if (ret) |
8514 | break; | |
edafccee | 8515 | } |
0a96bbe4 | 8516 | |
bd54b6fe | 8517 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 8518 | |
bd54b6fe BM |
8519 | ctx->buf_data = data; |
8520 | if (ret) | |
8521 | __io_sqe_buffers_unregister(ctx); | |
8522 | else | |
8523 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
8524 | return ret; |
8525 | } | |
8526 | ||
634d00df PB |
8527 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
8528 | struct io_uring_rsrc_update2 *up, | |
8529 | unsigned int nr_args) | |
8530 | { | |
8531 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
8532 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
8533 | struct page *last_hpage = NULL; |
8534 | bool needs_switch = false; | |
8535 | __u32 done; | |
8536 | int i, err; | |
8537 | ||
8538 | if (!ctx->buf_data) | |
8539 | return -ENXIO; | |
8540 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
8541 | return -EINVAL; | |
8542 | ||
8543 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
8544 | struct io_mapped_ubuf *imu; |
8545 | int offset = up->offset + done; | |
634d00df PB |
8546 | u64 tag = 0; |
8547 | ||
8548 | err = io_copy_iov(ctx, &iov, iovs, done); | |
8549 | if (err) | |
8550 | break; | |
8551 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
8552 | err = -EFAULT; | |
8553 | break; | |
8554 | } | |
0b8c0e7c PB |
8555 | err = io_buffer_validate(&iov); |
8556 | if (err) | |
8557 | break; | |
cf3770e7 CIK |
8558 | if (!iov.iov_base && tag) { |
8559 | err = -EINVAL; | |
8560 | break; | |
8561 | } | |
0b8c0e7c PB |
8562 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
8563 | if (err) | |
8564 | break; | |
634d00df | 8565 | |
0b8c0e7c | 8566 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 8567 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
8568 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
8569 | ctx->rsrc_node, ctx->user_bufs[i]); | |
8570 | if (unlikely(err)) { | |
8571 | io_buffer_unmap(ctx, &imu); | |
634d00df | 8572 | break; |
0b8c0e7c | 8573 | } |
634d00df PB |
8574 | ctx->user_bufs[i] = NULL; |
8575 | needs_switch = true; | |
8576 | } | |
8577 | ||
0b8c0e7c | 8578 | ctx->user_bufs[i] = imu; |
2d091d62 | 8579 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
8580 | } |
8581 | ||
8582 | if (needs_switch) | |
8583 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
8584 | return done ? done : err; | |
8585 | } | |
8586 | ||
9b402849 JA |
8587 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8588 | { | |
8589 | __s32 __user *fds = arg; | |
8590 | int fd; | |
8591 | ||
8592 | if (ctx->cq_ev_fd) | |
8593 | return -EBUSY; | |
8594 | ||
8595 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8596 | return -EFAULT; | |
8597 | ||
8598 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8599 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8600 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 8601 | |
9b402849 JA |
8602 | ctx->cq_ev_fd = NULL; |
8603 | return ret; | |
8604 | } | |
8605 | ||
8606 | return 0; | |
8607 | } | |
8608 | ||
8609 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8610 | { | |
8611 | if (ctx->cq_ev_fd) { | |
8612 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8613 | ctx->cq_ev_fd = NULL; | |
8614 | return 0; | |
8615 | } | |
8616 | ||
8617 | return -ENXIO; | |
8618 | } | |
8619 | ||
5a2e745d JA |
8620 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8621 | { | |
9e15c3a0 JA |
8622 | struct io_buffer *buf; |
8623 | unsigned long index; | |
8624 | ||
8625 | xa_for_each(&ctx->io_buffers, index, buf) | |
8626 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8627 | } |
8628 | ||
7255834e | 8629 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 8630 | { |
68e68ee6 | 8631 | struct io_kiocb *req, *nxt; |
1b4c351f | 8632 | |
bb943b82 PB |
8633 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
8634 | list_del(&req->inflight_entry); | |
1b4c351f JA |
8635 | kmem_cache_free(req_cachep, req); |
8636 | } | |
8637 | } | |
8638 | ||
4010fec4 | 8639 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8640 | { |
cd0ca2e0 | 8641 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 8642 | |
9a4fdbd8 JA |
8643 | mutex_lock(&ctx->uring_lock); |
8644 | ||
cd0ca2e0 PB |
8645 | if (state->free_reqs) { |
8646 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
8647 | state->free_reqs = 0; | |
8e5c66c4 | 8648 | } |
9a4fdbd8 | 8649 | |
cd0ca2e0 PB |
8650 | io_flush_cached_locked_reqs(ctx, state); |
8651 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
8652 | mutex_unlock(&ctx->uring_lock); |
8653 | } | |
8654 | ||
43597aac | 8655 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 8656 | { |
43597aac | 8657 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 8658 | wait_for_completion(&data->done); |
bd54b6fe | 8659 | } |
04fc6c80 | 8660 | |
2b188cc1 JA |
8661 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8662 | { | |
37d1e2e3 | 8663 | io_sq_thread_finish(ctx); |
2aede0e4 | 8664 | |
37d1e2e3 | 8665 | if (ctx->mm_account) { |
2aede0e4 JA |
8666 | mmdrop(ctx->mm_account); |
8667 | ctx->mm_account = NULL; | |
30975825 | 8668 | } |
def596e9 | 8669 | |
43597aac PB |
8670 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
8671 | io_wait_rsrc_data(ctx->buf_data); | |
8672 | io_wait_rsrc_data(ctx->file_data); | |
8673 | ||
8bad28d8 | 8674 | mutex_lock(&ctx->uring_lock); |
43597aac | 8675 | if (ctx->buf_data) |
bd54b6fe | 8676 | __io_sqe_buffers_unregister(ctx); |
43597aac | 8677 | if (ctx->file_data) |
08480400 | 8678 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
8679 | if (ctx->rings) |
8680 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8681 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8682 | io_eventfd_unregister(ctx); |
5a2e745d | 8683 | io_destroy_buffers(ctx); |
07db298a PB |
8684 | if (ctx->sq_creds) |
8685 | put_cred(ctx->sq_creds); | |
def596e9 | 8686 | |
a7f0ed5a PB |
8687 | /* there are no registered resources left, nobody uses it */ |
8688 | if (ctx->rsrc_node) | |
8689 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8690 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8691 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8692 | flush_delayed_work(&ctx->rsrc_put_work); |
8693 | ||
8694 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8695 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 8696 | |
2b188cc1 | 8697 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8698 | if (ctx->ring_sock) { |
8699 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8700 | sock_release(ctx->ring_sock); |
355e8d26 | 8701 | } |
2b188cc1 JA |
8702 | #endif |
8703 | ||
75b28aff | 8704 | io_mem_free(ctx->rings); |
2b188cc1 | 8705 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8706 | |
8707 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8708 | free_uid(ctx->user); |
4010fec4 | 8709 | io_req_caches_free(ctx); |
e941894e JA |
8710 | if (ctx->hash_map) |
8711 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8712 | kfree(ctx->cancel_hash); |
6224843d | 8713 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
8714 | kfree(ctx); |
8715 | } | |
8716 | ||
8717 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8718 | { | |
8719 | struct io_ring_ctx *ctx = file->private_data; | |
8720 | __poll_t mask = 0; | |
8721 | ||
311997b3 | 8722 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
8723 | /* |
8724 | * synchronizes with barrier from wq_has_sleeper call in | |
8725 | * io_commit_cqring | |
8726 | */ | |
2b188cc1 | 8727 | smp_rmb(); |
90554200 | 8728 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8729 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8730 | |
8731 | /* | |
8732 | * Don't flush cqring overflow list here, just do a simple check. | |
8733 | * Otherwise there could possible be ABBA deadlock: | |
8734 | * CPU0 CPU1 | |
8735 | * ---- ---- | |
8736 | * lock(&ctx->uring_lock); | |
8737 | * lock(&ep->mtx); | |
8738 | * lock(&ctx->uring_lock); | |
8739 | * lock(&ep->mtx); | |
8740 | * | |
8741 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8742 | * pushs them to do the flush. | |
8743 | */ | |
5ed7a37d | 8744 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
8745 | mask |= EPOLLIN | EPOLLRDNORM; |
8746 | ||
8747 | return mask; | |
8748 | } | |
8749 | ||
8750 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8751 | { | |
8752 | struct io_ring_ctx *ctx = file->private_data; | |
8753 | ||
8754 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8755 | } | |
8756 | ||
0bead8cd | 8757 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8758 | { |
4379bf8b | 8759 | const struct cred *creds; |
071698e1 | 8760 | |
61cf9370 | 8761 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8762 | if (creds) { |
8763 | put_cred(creds); | |
0bead8cd | 8764 | return 0; |
1e6fa521 | 8765 | } |
0bead8cd YD |
8766 | |
8767 | return -EINVAL; | |
8768 | } | |
8769 | ||
d56d938b PB |
8770 | struct io_tctx_exit { |
8771 | struct callback_head task_work; | |
8772 | struct completion completion; | |
baf186c4 | 8773 | struct io_ring_ctx *ctx; |
d56d938b PB |
8774 | }; |
8775 | ||
8776 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8777 | { | |
8778 | struct io_uring_task *tctx = current->io_uring; | |
8779 | struct io_tctx_exit *work; | |
8780 | ||
8781 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8782 | /* | |
8783 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8784 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8785 | */ | |
8786 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 8787 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
8788 | complete(&work->completion); |
8789 | } | |
8790 | ||
28090c13 PB |
8791 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8792 | { | |
8793 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8794 | ||
8795 | return req->ctx == data; | |
8796 | } | |
8797 | ||
85faa7b8 JA |
8798 | static void io_ring_exit_work(struct work_struct *work) |
8799 | { | |
d56d938b | 8800 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8801 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 8802 | unsigned long interval = HZ / 20; |
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 | 8826 | |
58d3be2c PB |
8827 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
8828 | /* there is little hope left, don't run it too often */ | |
8829 | interval = HZ * 60; | |
8830 | } | |
8831 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 8832 | |
7f00651a PB |
8833 | init_completion(&exit.completion); |
8834 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
8835 | exit.ctx = ctx; | |
89b5066e PB |
8836 | /* |
8837 | * Some may use context even when all refs and requests have been put, | |
8838 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 8839 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
8840 | * this lock/unlock section also waits them to finish. |
8841 | */ | |
d56d938b PB |
8842 | mutex_lock(&ctx->uring_lock); |
8843 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
8844 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
8845 | ||
d56d938b PB |
8846 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
8847 | ctx_node); | |
7f00651a PB |
8848 | /* don't spin on a single task if cancellation failed */ |
8849 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
8850 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
8851 | if (WARN_ON_ONCE(ret)) | |
8852 | continue; | |
8853 | wake_up_process(node->task); | |
8854 | ||
8855 | mutex_unlock(&ctx->uring_lock); | |
8856 | wait_for_completion(&exit.completion); | |
d56d938b PB |
8857 | mutex_lock(&ctx->uring_lock); |
8858 | } | |
8859 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
8860 | spin_lock(&ctx->completion_lock); |
8861 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 8862 | |
85faa7b8 JA |
8863 | io_ring_ctx_free(ctx); |
8864 | } | |
8865 | ||
80c4cbdb PB |
8866 | /* Returns true if we found and killed one or more timeouts */ |
8867 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 8868 | bool cancel_all) |
80c4cbdb PB |
8869 | { |
8870 | struct io_kiocb *req, *tmp; | |
8871 | int canceled = 0; | |
8872 | ||
79ebeaee JA |
8873 | spin_lock(&ctx->completion_lock); |
8874 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 8875 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 8876 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
8877 | io_kill_timeout(req, -ECANCELED); |
8878 | canceled++; | |
8879 | } | |
8880 | } | |
79ebeaee | 8881 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
8882 | if (canceled != 0) |
8883 | io_commit_cqring(ctx); | |
79ebeaee | 8884 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
8885 | if (canceled != 0) |
8886 | io_cqring_ev_posted(ctx); | |
8887 | return canceled != 0; | |
8888 | } | |
8889 | ||
2b188cc1 JA |
8890 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8891 | { | |
61cf9370 MWO |
8892 | unsigned long index; |
8893 | struct creds *creds; | |
8894 | ||
2b188cc1 JA |
8895 | mutex_lock(&ctx->uring_lock); |
8896 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 8897 | if (ctx->rings) |
6c2450ae | 8898 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
8899 | xa_for_each(&ctx->personalities, index, creds) |
8900 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
8901 | mutex_unlock(&ctx->uring_lock); |
8902 | ||
3dd0c97a PB |
8903 | io_kill_timeouts(ctx, NULL, true); |
8904 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 8905 | |
15dff286 | 8906 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8907 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8908 | |
85faa7b8 | 8909 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8910 | /* |
8911 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8912 | * if we're exiting a ton of rings at the same time. It just adds | |
8913 | * noise and overhead, there's no discernable change in runtime | |
8914 | * over using system_wq. | |
8915 | */ | |
8916 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8917 | } |
8918 | ||
8919 | static int io_uring_release(struct inode *inode, struct file *file) | |
8920 | { | |
8921 | struct io_ring_ctx *ctx = file->private_data; | |
8922 | ||
8923 | file->private_data = NULL; | |
8924 | io_ring_ctx_wait_and_kill(ctx); | |
8925 | return 0; | |
8926 | } | |
8927 | ||
f6edbabb PB |
8928 | struct io_task_cancel { |
8929 | struct task_struct *task; | |
3dd0c97a | 8930 | bool all; |
f6edbabb | 8931 | }; |
f254ac04 | 8932 | |
f6edbabb | 8933 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8934 | { |
9a472ef7 | 8935 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8936 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8937 | bool ret; |
8938 | ||
3dd0c97a | 8939 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8940 | struct io_ring_ctx *ctx = req->ctx; |
8941 | ||
8942 | /* protect against races with linked timeouts */ | |
79ebeaee | 8943 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 8944 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 8945 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 8946 | } else { |
3dd0c97a | 8947 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
8948 | } |
8949 | return ret; | |
b711d4ea JA |
8950 | } |
8951 | ||
e1915f76 | 8952 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 8953 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 8954 | { |
e1915f76 | 8955 | struct io_defer_entry *de; |
b7ddce3c PB |
8956 | LIST_HEAD(list); |
8957 | ||
79ebeaee | 8958 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 8959 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 8960 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
8961 | list_cut_position(&list, &ctx->defer_list, &de->list); |
8962 | break; | |
8963 | } | |
8964 | } | |
79ebeaee | 8965 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
8966 | if (list_empty(&list)) |
8967 | return false; | |
b7ddce3c PB |
8968 | |
8969 | while (!list_empty(&list)) { | |
8970 | de = list_first_entry(&list, struct io_defer_entry, list); | |
8971 | list_del_init(&de->list); | |
f41db273 | 8972 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
8973 | kfree(de); |
8974 | } | |
e1915f76 | 8975 | return true; |
b7ddce3c PB |
8976 | } |
8977 | ||
1b00764f PB |
8978 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
8979 | { | |
8980 | struct io_tctx_node *node; | |
8981 | enum io_wq_cancel cret; | |
8982 | bool ret = false; | |
8983 | ||
8984 | mutex_lock(&ctx->uring_lock); | |
8985 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
8986 | struct io_uring_task *tctx = node->task->io_uring; | |
8987 | ||
8988 | /* | |
8989 | * io_wq will stay alive while we hold uring_lock, because it's | |
8990 | * killed after ctx nodes, which requires to take the lock. | |
8991 | */ | |
8992 | if (!tctx || !tctx->io_wq) | |
8993 | continue; | |
8994 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
8995 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
8996 | } | |
8997 | mutex_unlock(&ctx->uring_lock); | |
8998 | ||
8999 | return ret; | |
9000 | } | |
9001 | ||
9936c7c2 PB |
9002 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9003 | struct task_struct *task, | |
3dd0c97a | 9004 | bool cancel_all) |
9936c7c2 | 9005 | { |
3dd0c97a | 9006 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9007 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9008 | |
9009 | while (1) { | |
9010 | enum io_wq_cancel cret; | |
9011 | bool ret = false; | |
9012 | ||
1b00764f PB |
9013 | if (!task) { |
9014 | ret |= io_uring_try_cancel_iowq(ctx); | |
9015 | } else if (tctx && tctx->io_wq) { | |
9016 | /* | |
9017 | * Cancels requests of all rings, not only @ctx, but | |
9018 | * it's fine as the task is in exit/exec. | |
9019 | */ | |
5aa75ed5 | 9020 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9021 | &cancel, true); |
9022 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9023 | } | |
9024 | ||
9025 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9026 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9027 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9028 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9029 | io_iopoll_try_reap_events(ctx); | |
9030 | ret = true; | |
9031 | } | |
9032 | } | |
9033 | ||
3dd0c97a PB |
9034 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9035 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9036 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9037 | if (task) |
9038 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9039 | if (!ret) |
9040 | break; | |
9041 | cond_resched(); | |
9042 | } | |
9043 | } | |
9044 | ||
eef51daa | 9045 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9046 | { |
236434c3 | 9047 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9048 | struct io_tctx_node *node; |
a528b04e | 9049 | int ret; |
236434c3 MWO |
9050 | |
9051 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9052 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9053 | if (unlikely(ret)) |
9054 | return ret; | |
236434c3 | 9055 | tctx = current->io_uring; |
0f212204 | 9056 | } |
cf27f3b1 PB |
9057 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9058 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9059 | if (!node) | |
9060 | return -ENOMEM; | |
9061 | node->ctx = ctx; | |
9062 | node->task = current; | |
13bf43f5 | 9063 | |
cf27f3b1 PB |
9064 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9065 | node, GFP_KERNEL)); | |
9066 | if (ret) { | |
9067 | kfree(node); | |
9068 | return ret; | |
0f212204 | 9069 | } |
cf27f3b1 PB |
9070 | |
9071 | mutex_lock(&ctx->uring_lock); | |
9072 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9073 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9074 | } |
cf27f3b1 | 9075 | tctx->last = ctx; |
0f212204 JA |
9076 | return 0; |
9077 | } | |
9078 | ||
cf27f3b1 PB |
9079 | /* |
9080 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9081 | */ | |
eef51daa | 9082 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9083 | { |
9084 | struct io_uring_task *tctx = current->io_uring; | |
9085 | ||
9086 | if (likely(tctx && tctx->last == ctx)) | |
9087 | return 0; | |
eef51daa | 9088 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9089 | } |
9090 | ||
0f212204 JA |
9091 | /* |
9092 | * Remove this io_uring_file -> task mapping. | |
9093 | */ | |
eef51daa | 9094 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9095 | { |
9096 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9097 | struct io_tctx_node *node; |
2941267b | 9098 | |
eebd2e37 PB |
9099 | if (!tctx) |
9100 | return; | |
13bf43f5 PB |
9101 | node = xa_erase(&tctx->xa, index); |
9102 | if (!node) | |
2941267b | 9103 | return; |
0f212204 | 9104 | |
13bf43f5 PB |
9105 | WARN_ON_ONCE(current != node->task); |
9106 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9107 | ||
9108 | mutex_lock(&node->ctx->uring_lock); | |
9109 | list_del(&node->ctx_node); | |
9110 | mutex_unlock(&node->ctx->uring_lock); | |
9111 | ||
baf186c4 | 9112 | if (tctx->last == node->ctx) |
0f212204 | 9113 | tctx->last = NULL; |
13bf43f5 | 9114 | kfree(node); |
0f212204 JA |
9115 | } |
9116 | ||
8452d4a6 | 9117 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9118 | { |
ba5ef6dc | 9119 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9120 | struct io_tctx_node *node; |
de7f1d9e PB |
9121 | unsigned long index; |
9122 | ||
13bf43f5 | 9123 | xa_for_each(&tctx->xa, index, node) |
eef51daa | 9124 | io_uring_del_tctx_node(index); |
b16ef427 ME |
9125 | if (wq) { |
9126 | /* | |
9127 | * Must be after io_uring_del_task_file() (removes nodes under | |
9128 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9129 | */ | |
9130 | tctx->io_wq = NULL; | |
ba5ef6dc | 9131 | io_wq_put_and_exit(wq); |
b16ef427 | 9132 | } |
de7f1d9e PB |
9133 | } |
9134 | ||
3f48cf18 | 9135 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9136 | { |
3f48cf18 PB |
9137 | if (tracked) |
9138 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9139 | return percpu_counter_sum(&tctx->inflight); |
9140 | } | |
9141 | ||
09899b19 PB |
9142 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9143 | { | |
9144 | struct io_uring_task *tctx = task->io_uring; | |
9145 | unsigned int refs = tctx->cached_refs; | |
9146 | ||
e9dbe221 PB |
9147 | if (refs) { |
9148 | tctx->cached_refs = 0; | |
9149 | percpu_counter_sub(&tctx->inflight, refs); | |
9150 | put_task_struct_many(task, refs); | |
9151 | } | |
09899b19 PB |
9152 | } |
9153 | ||
78cc687b PB |
9154 | /* |
9155 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9156 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9157 | */ | |
9158 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9159 | { |
521d6a73 | 9160 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9161 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9162 | s64 inflight; |
9163 | DEFINE_WAIT(wait); | |
fdaf083c | 9164 | |
78cc687b PB |
9165 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9166 | ||
6d042ffb PO |
9167 | if (!current->io_uring) |
9168 | return; | |
17a91051 PB |
9169 | if (tctx->io_wq) |
9170 | io_wq_exit_start(tctx->io_wq); | |
9171 | ||
0e9ddb39 PB |
9172 | atomic_inc(&tctx->in_idle); |
9173 | do { | |
e9dbe221 | 9174 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9175 | /* read completions before cancelations */ |
78cc687b | 9176 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9177 | if (!inflight) |
9178 | break; | |
fdaf083c | 9179 | |
78cc687b PB |
9180 | if (!sqd) { |
9181 | struct io_tctx_node *node; | |
9182 | unsigned long index; | |
0f212204 | 9183 | |
78cc687b PB |
9184 | xa_for_each(&tctx->xa, index, node) { |
9185 | /* sqpoll task will cancel all its requests */ | |
9186 | if (node->ctx->sq_data) | |
9187 | continue; | |
9188 | io_uring_try_cancel_requests(node->ctx, current, | |
9189 | cancel_all); | |
9190 | } | |
9191 | } else { | |
9192 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9193 | io_uring_try_cancel_requests(ctx, current, | |
9194 | cancel_all); | |
9195 | } | |
17a91051 | 9196 | |
0f212204 | 9197 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9198 | io_uring_drop_tctx_refs(current); |
0f212204 | 9199 | /* |
a1bb3cd5 PB |
9200 | * If we've seen completions, retry without waiting. This |
9201 | * avoids a race where a completion comes in before we did | |
9202 | * prepare_to_wait(). | |
0f212204 | 9203 | */ |
3dd0c97a | 9204 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9205 | schedule(); |
f57555ed | 9206 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9207 | } while (1); |
fdaf083c | 9208 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9209 | |
8452d4a6 | 9210 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9211 | if (cancel_all) { |
3f48cf18 PB |
9212 | /* for exec all current's requests should be gone, kill tctx */ |
9213 | __io_uring_free(current); | |
9214 | } | |
44e728b8 PB |
9215 | } |
9216 | ||
f552a27a | 9217 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9218 | { |
f552a27a | 9219 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9220 | } |
9221 | ||
6c5c240e RP |
9222 | static void *io_uring_validate_mmap_request(struct file *file, |
9223 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9224 | { |
2b188cc1 | 9225 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9226 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9227 | struct page *page; |
9228 | void *ptr; | |
9229 | ||
9230 | switch (offset) { | |
9231 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9232 | case IORING_OFF_CQ_RING: |
9233 | ptr = ctx->rings; | |
2b188cc1 JA |
9234 | break; |
9235 | case IORING_OFF_SQES: | |
9236 | ptr = ctx->sq_sqes; | |
9237 | break; | |
2b188cc1 | 9238 | default: |
6c5c240e | 9239 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9240 | } |
9241 | ||
9242 | page = virt_to_head_page(ptr); | |
a50b854e | 9243 | if (sz > page_size(page)) |
6c5c240e RP |
9244 | return ERR_PTR(-EINVAL); |
9245 | ||
9246 | return ptr; | |
9247 | } | |
9248 | ||
9249 | #ifdef CONFIG_MMU | |
9250 | ||
9251 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9252 | { | |
9253 | size_t sz = vma->vm_end - vma->vm_start; | |
9254 | unsigned long pfn; | |
9255 | void *ptr; | |
9256 | ||
9257 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9258 | if (IS_ERR(ptr)) | |
9259 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9260 | |
9261 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9262 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9263 | } | |
9264 | ||
6c5c240e RP |
9265 | #else /* !CONFIG_MMU */ |
9266 | ||
9267 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9268 | { | |
9269 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9270 | } | |
9271 | ||
9272 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9273 | { | |
9274 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9275 | } | |
9276 | ||
9277 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9278 | unsigned long addr, unsigned long len, | |
9279 | unsigned long pgoff, unsigned long flags) | |
9280 | { | |
9281 | void *ptr; | |
9282 | ||
9283 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9284 | if (IS_ERR(ptr)) | |
9285 | return PTR_ERR(ptr); | |
9286 | ||
9287 | return (unsigned long) ptr; | |
9288 | } | |
9289 | ||
9290 | #endif /* !CONFIG_MMU */ | |
9291 | ||
d9d05217 | 9292 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9293 | { |
9294 | DEFINE_WAIT(wait); | |
9295 | ||
9296 | do { | |
9297 | if (!io_sqring_full(ctx)) | |
9298 | break; | |
90554200 JA |
9299 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9300 | ||
9301 | if (!io_sqring_full(ctx)) | |
9302 | break; | |
90554200 JA |
9303 | schedule(); |
9304 | } while (!signal_pending(current)); | |
9305 | ||
9306 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9307 | return 0; |
90554200 JA |
9308 | } |
9309 | ||
c73ebb68 HX |
9310 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9311 | struct __kernel_timespec __user **ts, | |
9312 | const sigset_t __user **sig) | |
9313 | { | |
9314 | struct io_uring_getevents_arg arg; | |
9315 | ||
9316 | /* | |
9317 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9318 | * is just a pointer to the sigset_t. | |
9319 | */ | |
9320 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9321 | *sig = (const sigset_t __user *) argp; | |
9322 | *ts = NULL; | |
9323 | return 0; | |
9324 | } | |
9325 | ||
9326 | /* | |
9327 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9328 | * timespec and sigset_t pointers if good. | |
9329 | */ | |
9330 | if (*argsz != sizeof(arg)) | |
9331 | return -EINVAL; | |
9332 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9333 | return -EFAULT; | |
9334 | *sig = u64_to_user_ptr(arg.sigmask); | |
9335 | *argsz = arg.sigmask_sz; | |
9336 | *ts = u64_to_user_ptr(arg.ts); | |
9337 | return 0; | |
9338 | } | |
9339 | ||
2b188cc1 | 9340 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9341 | u32, min_complete, u32, flags, const void __user *, argp, |
9342 | size_t, argsz) | |
2b188cc1 JA |
9343 | { |
9344 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9345 | int submitted = 0; |
9346 | struct fd f; | |
33f993da | 9347 | long ret; |
2b188cc1 | 9348 | |
4c6e277c | 9349 | io_run_task_work(); |
b41e9852 | 9350 | |
33f993da PB |
9351 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9352 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9353 | return -EINVAL; |
9354 | ||
9355 | f = fdget(fd); | |
33f993da | 9356 | if (unlikely(!f.file)) |
2b188cc1 JA |
9357 | return -EBADF; |
9358 | ||
9359 | ret = -EOPNOTSUPP; | |
33f993da | 9360 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9361 | goto out_fput; |
9362 | ||
9363 | ret = -ENXIO; | |
9364 | ctx = f.file->private_data; | |
33f993da | 9365 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9366 | goto out_fput; |
9367 | ||
7e84e1c7 | 9368 | ret = -EBADFD; |
33f993da | 9369 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9370 | goto out; |
9371 | ||
6c271ce2 JA |
9372 | /* |
9373 | * For SQ polling, the thread will do all submissions and completions. | |
9374 | * Just return the requested submit count, and wake the thread if | |
9375 | * we were asked to. | |
9376 | */ | |
b2a9eada | 9377 | ret = 0; |
6c271ce2 | 9378 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9379 | io_cqring_overflow_flush(ctx); |
89448c47 | 9380 | |
21f96522 JA |
9381 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9382 | ret = -EOWNERDEAD; | |
04147488 | 9383 | goto out; |
21f96522 | 9384 | } |
6c271ce2 | 9385 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9386 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9387 | if (flags & IORING_ENTER_SQ_WAIT) { |
9388 | ret = io_sqpoll_wait_sq(ctx); | |
9389 | if (ret) | |
9390 | goto out; | |
9391 | } | |
6c271ce2 | 9392 | submitted = to_submit; |
b2a9eada | 9393 | } else if (to_submit) { |
eef51daa | 9394 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9395 | if (unlikely(ret)) |
9396 | goto out; | |
2b188cc1 | 9397 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9398 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9399 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9400 | |
9401 | if (submitted != to_submit) | |
9402 | goto out; | |
2b188cc1 JA |
9403 | } |
9404 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9405 | const sigset_t __user *sig; |
9406 | struct __kernel_timespec __user *ts; | |
9407 | ||
9408 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9409 | if (unlikely(ret)) | |
9410 | goto out; | |
9411 | ||
2b188cc1 JA |
9412 | min_complete = min(min_complete, ctx->cq_entries); |
9413 | ||
32b2244a XW |
9414 | /* |
9415 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9416 | * space applications don't need to do io completion events | |
9417 | * polling again, they can rely on io_sq_thread to do polling | |
9418 | * work, which can reduce cpu usage and uring_lock contention. | |
9419 | */ | |
9420 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9421 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9422 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9423 | } else { |
c73ebb68 | 9424 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9425 | } |
2b188cc1 JA |
9426 | } |
9427 | ||
7c504e65 | 9428 | out: |
6805b32e | 9429 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9430 | out_fput: |
9431 | fdput(f); | |
9432 | return submitted ? submitted : ret; | |
9433 | } | |
9434 | ||
bebdb65e | 9435 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9436 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9437 | const struct cred *cred) | |
87ce955b | 9438 | { |
87ce955b JA |
9439 | struct user_namespace *uns = seq_user_ns(m); |
9440 | struct group_info *gi; | |
9441 | kernel_cap_t cap; | |
9442 | unsigned __capi; | |
9443 | int g; | |
9444 | ||
9445 | seq_printf(m, "%5d\n", id); | |
9446 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9447 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9448 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9449 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9450 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9451 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9452 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9453 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9454 | seq_puts(m, "\n\tGroups:\t"); | |
9455 | gi = cred->group_info; | |
9456 | for (g = 0; g < gi->ngroups; g++) { | |
9457 | seq_put_decimal_ull(m, g ? " " : "", | |
9458 | from_kgid_munged(uns, gi->gid[g])); | |
9459 | } | |
9460 | seq_puts(m, "\n\tCapEff:\t"); | |
9461 | cap = cred->cap_effective; | |
9462 | CAP_FOR_EACH_U32(__capi) | |
9463 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9464 | seq_putc(m, '\n'); | |
9465 | return 0; | |
9466 | } | |
9467 | ||
9468 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9469 | { | |
dbbe9c64 | 9470 | struct io_sq_data *sq = NULL; |
fad8e0de | 9471 | bool has_lock; |
87ce955b JA |
9472 | int i; |
9473 | ||
fad8e0de JA |
9474 | /* |
9475 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9476 | * since fdinfo case grabs it in the opposite direction of normal use | |
9477 | * cases. If we fail to get the lock, we just don't iterate any | |
9478 | * structures that could be going away outside the io_uring mutex. | |
9479 | */ | |
9480 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9481 | ||
5f3f26f9 | 9482 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9483 | sq = ctx->sq_data; |
5f3f26f9 JA |
9484 | if (!sq->thread) |
9485 | sq = NULL; | |
9486 | } | |
dbbe9c64 JQ |
9487 | |
9488 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9489 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9490 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9491 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9492 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9493 | |
87ce955b JA |
9494 | if (f) |
9495 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9496 | else | |
9497 | seq_printf(m, "%5u: <none>\n", i); | |
9498 | } | |
9499 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9500 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 9501 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 9502 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9503 | |
4751f53d | 9504 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9505 | } |
61cf9370 MWO |
9506 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9507 | unsigned long index; | |
9508 | const struct cred *cred; | |
9509 | ||
87ce955b | 9510 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9511 | xa_for_each(&ctx->personalities, index, cred) |
9512 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9513 | } |
d7718a9d | 9514 | seq_printf(m, "PollList:\n"); |
79ebeaee | 9515 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
9516 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
9517 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9518 | struct io_kiocb *req; | |
9519 | ||
9520 | hlist_for_each_entry(req, list, hash_node) | |
9521 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9522 | req->task->task_works != NULL); | |
9523 | } | |
79ebeaee | 9524 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
9525 | if (has_lock) |
9526 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9527 | } |
9528 | ||
9529 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9530 | { | |
9531 | struct io_ring_ctx *ctx = f->private_data; | |
9532 | ||
9533 | if (percpu_ref_tryget(&ctx->refs)) { | |
9534 | __io_uring_show_fdinfo(ctx, m); | |
9535 | percpu_ref_put(&ctx->refs); | |
9536 | } | |
9537 | } | |
bebdb65e | 9538 | #endif |
87ce955b | 9539 | |
2b188cc1 JA |
9540 | static const struct file_operations io_uring_fops = { |
9541 | .release = io_uring_release, | |
9542 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9543 | #ifndef CONFIG_MMU |
9544 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9545 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9546 | #endif | |
2b188cc1 JA |
9547 | .poll = io_uring_poll, |
9548 | .fasync = io_uring_fasync, | |
bebdb65e | 9549 | #ifdef CONFIG_PROC_FS |
87ce955b | 9550 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9551 | #endif |
2b188cc1 JA |
9552 | }; |
9553 | ||
9554 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9555 | struct io_uring_params *p) | |
9556 | { | |
75b28aff HV |
9557 | struct io_rings *rings; |
9558 | size_t size, sq_array_offset; | |
2b188cc1 | 9559 | |
bd740481 JA |
9560 | /* make sure these are sane, as we already accounted them */ |
9561 | ctx->sq_entries = p->sq_entries; | |
9562 | ctx->cq_entries = p->cq_entries; | |
9563 | ||
75b28aff HV |
9564 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9565 | if (size == SIZE_MAX) | |
9566 | return -EOVERFLOW; | |
9567 | ||
9568 | rings = io_mem_alloc(size); | |
9569 | if (!rings) | |
2b188cc1 JA |
9570 | return -ENOMEM; |
9571 | ||
75b28aff HV |
9572 | ctx->rings = rings; |
9573 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9574 | rings->sq_ring_mask = p->sq_entries - 1; | |
9575 | rings->cq_ring_mask = p->cq_entries - 1; | |
9576 | rings->sq_ring_entries = p->sq_entries; | |
9577 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
9578 | |
9579 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9580 | if (size == SIZE_MAX) { |
9581 | io_mem_free(ctx->rings); | |
9582 | ctx->rings = NULL; | |
2b188cc1 | 9583 | return -EOVERFLOW; |
eb065d30 | 9584 | } |
2b188cc1 JA |
9585 | |
9586 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9587 | if (!ctx->sq_sqes) { |
9588 | io_mem_free(ctx->rings); | |
9589 | ctx->rings = NULL; | |
2b188cc1 | 9590 | return -ENOMEM; |
eb065d30 | 9591 | } |
2b188cc1 | 9592 | |
2b188cc1 JA |
9593 | return 0; |
9594 | } | |
9595 | ||
9faadcc8 PB |
9596 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9597 | { | |
9598 | int ret, fd; | |
9599 | ||
9600 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9601 | if (fd < 0) | |
9602 | return fd; | |
9603 | ||
eef51daa | 9604 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
9605 | if (ret) { |
9606 | put_unused_fd(fd); | |
9607 | return ret; | |
9608 | } | |
9609 | fd_install(fd, file); | |
9610 | return fd; | |
9611 | } | |
9612 | ||
2b188cc1 JA |
9613 | /* |
9614 | * Allocate an anonymous fd, this is what constitutes the application | |
9615 | * visible backing of an io_uring instance. The application mmaps this | |
9616 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9617 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9618 | */ | |
9faadcc8 | 9619 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9620 | { |
9621 | struct file *file; | |
9faadcc8 | 9622 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9623 | int ret; |
9624 | ||
2b188cc1 JA |
9625 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9626 | &ctx->ring_sock); | |
9627 | if (ret) | |
9faadcc8 | 9628 | return ERR_PTR(ret); |
2b188cc1 JA |
9629 | #endif |
9630 | ||
2b188cc1 JA |
9631 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9632 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9633 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9634 | if (IS_ERR(file)) { |
9635 | sock_release(ctx->ring_sock); | |
9636 | ctx->ring_sock = NULL; | |
9637 | } else { | |
9638 | ctx->ring_sock->file = file; | |
0f212204 | 9639 | } |
2b188cc1 | 9640 | #endif |
9faadcc8 | 9641 | return file; |
2b188cc1 JA |
9642 | } |
9643 | ||
7f13657d XW |
9644 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9645 | struct io_uring_params __user *params) | |
2b188cc1 | 9646 | { |
2b188cc1 | 9647 | struct io_ring_ctx *ctx; |
9faadcc8 | 9648 | struct file *file; |
2b188cc1 JA |
9649 | int ret; |
9650 | ||
8110c1a6 | 9651 | if (!entries) |
2b188cc1 | 9652 | return -EINVAL; |
8110c1a6 JA |
9653 | if (entries > IORING_MAX_ENTRIES) { |
9654 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9655 | return -EINVAL; | |
9656 | entries = IORING_MAX_ENTRIES; | |
9657 | } | |
2b188cc1 JA |
9658 | |
9659 | /* | |
9660 | * Use twice as many entries for the CQ ring. It's possible for the | |
9661 | * application to drive a higher depth than the size of the SQ ring, | |
9662 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9663 | * some flexibility in overcommitting a bit. If the application has |
9664 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9665 | * of CQ ring entries manually. | |
2b188cc1 JA |
9666 | */ |
9667 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9668 | if (p->flags & IORING_SETUP_CQSIZE) { |
9669 | /* | |
9670 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9671 | * to a power-of-two, if it isn't already. We do NOT impose | |
9672 | * any cq vs sq ring sizing. | |
9673 | */ | |
eb2667b3 | 9674 | if (!p->cq_entries) |
33a107f0 | 9675 | return -EINVAL; |
8110c1a6 JA |
9676 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9677 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9678 | return -EINVAL; | |
9679 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9680 | } | |
eb2667b3 JQ |
9681 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9682 | if (p->cq_entries < p->sq_entries) | |
9683 | return -EINVAL; | |
33a107f0 JA |
9684 | } else { |
9685 | p->cq_entries = 2 * p->sq_entries; | |
9686 | } | |
2b188cc1 | 9687 | |
2b188cc1 | 9688 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9689 | if (!ctx) |
2b188cc1 | 9690 | return -ENOMEM; |
2b188cc1 | 9691 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9692 | if (!capable(CAP_IPC_LOCK)) |
9693 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9694 | |
9695 | /* | |
9696 | * This is just grabbed for accounting purposes. When a process exits, | |
9697 | * the mm is exited and dropped before the files, hence we need to hang | |
9698 | * on to this mm purely for the purposes of being able to unaccount | |
9699 | * memory (locked/pinned vm). It's not used for anything else. | |
9700 | */ | |
6b7898eb | 9701 | mmgrab(current->mm); |
2aede0e4 | 9702 | ctx->mm_account = current->mm; |
6b7898eb | 9703 | |
2b188cc1 JA |
9704 | ret = io_allocate_scq_urings(ctx, p); |
9705 | if (ret) | |
9706 | goto err; | |
9707 | ||
7e84e1c7 | 9708 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9709 | if (ret) |
9710 | goto err; | |
eae071c9 | 9711 | /* always set a rsrc node */ |
47b228ce PB |
9712 | ret = io_rsrc_node_switch_start(ctx); |
9713 | if (ret) | |
9714 | goto err; | |
eae071c9 | 9715 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 9716 | |
2b188cc1 | 9717 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9718 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9719 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9720 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9721 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9722 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9723 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9724 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9725 | |
9726 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9727 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9728 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9729 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9730 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9731 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9732 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9733 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9734 | |
7f13657d XW |
9735 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9736 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9737 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9738 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
9739 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
9740 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
9741 | |
9742 | if (copy_to_user(params, p, sizeof(*p))) { | |
9743 | ret = -EFAULT; | |
9744 | goto err; | |
9745 | } | |
d1719f70 | 9746 | |
9faadcc8 PB |
9747 | file = io_uring_get_file(ctx); |
9748 | if (IS_ERR(file)) { | |
9749 | ret = PTR_ERR(file); | |
9750 | goto err; | |
9751 | } | |
9752 | ||
044c1ab3 JA |
9753 | /* |
9754 | * Install ring fd as the very last thing, so we don't risk someone | |
9755 | * having closed it before we finish setup | |
9756 | */ | |
9faadcc8 PB |
9757 | ret = io_uring_install_fd(ctx, file); |
9758 | if (ret < 0) { | |
9759 | /* fput will clean it up */ | |
9760 | fput(file); | |
9761 | return ret; | |
9762 | } | |
044c1ab3 | 9763 | |
c826bd7a | 9764 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9765 | return ret; |
9766 | err: | |
9767 | io_ring_ctx_wait_and_kill(ctx); | |
9768 | return ret; | |
9769 | } | |
9770 | ||
9771 | /* | |
9772 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9773 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9774 | * params structure passed in. | |
9775 | */ | |
9776 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9777 | { | |
9778 | struct io_uring_params p; | |
2b188cc1 JA |
9779 | int i; |
9780 | ||
9781 | if (copy_from_user(&p, params, sizeof(p))) | |
9782 | return -EFAULT; | |
9783 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9784 | if (p.resv[i]) | |
9785 | return -EINVAL; | |
9786 | } | |
9787 | ||
6c271ce2 | 9788 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9789 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9790 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9791 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9792 | return -EINVAL; |
9793 | ||
7f13657d | 9794 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9795 | } |
9796 | ||
9797 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9798 | struct io_uring_params __user *, params) | |
9799 | { | |
9800 | return io_uring_setup(entries, params); | |
9801 | } | |
9802 | ||
66f4af93 JA |
9803 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9804 | { | |
9805 | struct io_uring_probe *p; | |
9806 | size_t size; | |
9807 | int i, ret; | |
9808 | ||
9809 | size = struct_size(p, ops, nr_args); | |
9810 | if (size == SIZE_MAX) | |
9811 | return -EOVERFLOW; | |
9812 | p = kzalloc(size, GFP_KERNEL); | |
9813 | if (!p) | |
9814 | return -ENOMEM; | |
9815 | ||
9816 | ret = -EFAULT; | |
9817 | if (copy_from_user(p, arg, size)) | |
9818 | goto out; | |
9819 | ret = -EINVAL; | |
9820 | if (memchr_inv(p, 0, size)) | |
9821 | goto out; | |
9822 | ||
9823 | p->last_op = IORING_OP_LAST - 1; | |
9824 | if (nr_args > IORING_OP_LAST) | |
9825 | nr_args = IORING_OP_LAST; | |
9826 | ||
9827 | for (i = 0; i < nr_args; i++) { | |
9828 | p->ops[i].op = i; | |
9829 | if (!io_op_defs[i].not_supported) | |
9830 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9831 | } | |
9832 | p->ops_len = i; | |
9833 | ||
9834 | ret = 0; | |
9835 | if (copy_to_user(arg, p, size)) | |
9836 | ret = -EFAULT; | |
9837 | out: | |
9838 | kfree(p); | |
9839 | return ret; | |
9840 | } | |
9841 | ||
071698e1 JA |
9842 | static int io_register_personality(struct io_ring_ctx *ctx) |
9843 | { | |
4379bf8b | 9844 | const struct cred *creds; |
61cf9370 | 9845 | u32 id; |
1e6fa521 | 9846 | int ret; |
071698e1 | 9847 | |
4379bf8b | 9848 | creds = get_current_cred(); |
1e6fa521 | 9849 | |
61cf9370 MWO |
9850 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
9851 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
9852 | if (ret < 0) { |
9853 | put_cred(creds); | |
9854 | return ret; | |
9855 | } | |
9856 | return id; | |
071698e1 JA |
9857 | } |
9858 | ||
21b55dbc SG |
9859 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9860 | unsigned int nr_args) | |
9861 | { | |
9862 | struct io_uring_restriction *res; | |
9863 | size_t size; | |
9864 | int i, ret; | |
9865 | ||
7e84e1c7 SG |
9866 | /* Restrictions allowed only if rings started disabled */ |
9867 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9868 | return -EBADFD; | |
9869 | ||
21b55dbc | 9870 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9871 | if (ctx->restrictions.registered) |
21b55dbc SG |
9872 | return -EBUSY; |
9873 | ||
9874 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9875 | return -EINVAL; | |
9876 | ||
9877 | size = array_size(nr_args, sizeof(*res)); | |
9878 | if (size == SIZE_MAX) | |
9879 | return -EOVERFLOW; | |
9880 | ||
9881 | res = memdup_user(arg, size); | |
9882 | if (IS_ERR(res)) | |
9883 | return PTR_ERR(res); | |
9884 | ||
9885 | ret = 0; | |
9886 | ||
9887 | for (i = 0; i < nr_args; i++) { | |
9888 | switch (res[i].opcode) { | |
9889 | case IORING_RESTRICTION_REGISTER_OP: | |
9890 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9891 | ret = -EINVAL; | |
9892 | goto out; | |
9893 | } | |
9894 | ||
9895 | __set_bit(res[i].register_op, | |
9896 | ctx->restrictions.register_op); | |
9897 | break; | |
9898 | case IORING_RESTRICTION_SQE_OP: | |
9899 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9900 | ret = -EINVAL; | |
9901 | goto out; | |
9902 | } | |
9903 | ||
9904 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9905 | break; | |
9906 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9907 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9908 | break; | |
9909 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9910 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9911 | break; | |
9912 | default: | |
9913 | ret = -EINVAL; | |
9914 | goto out; | |
9915 | } | |
9916 | } | |
9917 | ||
9918 | out: | |
9919 | /* Reset all restrictions if an error happened */ | |
9920 | if (ret != 0) | |
9921 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9922 | else | |
7e84e1c7 | 9923 | ctx->restrictions.registered = true; |
21b55dbc SG |
9924 | |
9925 | kfree(res); | |
9926 | return ret; | |
9927 | } | |
9928 | ||
7e84e1c7 SG |
9929 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9930 | { | |
9931 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9932 | return -EBADFD; | |
9933 | ||
9934 | if (ctx->restrictions.registered) | |
9935 | ctx->restricted = 1; | |
9936 | ||
0298ef96 PB |
9937 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
9938 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
9939 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
9940 | return 0; |
9941 | } | |
9942 | ||
fdecb662 | 9943 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 9944 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
9945 | unsigned nr_args) |
9946 | { | |
9947 | __u32 tmp; | |
9948 | int err; | |
9949 | ||
c3bdad02 PB |
9950 | if (up->resv) |
9951 | return -EINVAL; | |
98f0b3b4 PB |
9952 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
9953 | return -EOVERFLOW; | |
9954 | err = io_rsrc_node_switch_start(ctx); | |
9955 | if (err) | |
9956 | return err; | |
9957 | ||
fdecb662 PB |
9958 | switch (type) { |
9959 | case IORING_RSRC_FILE: | |
98f0b3b4 | 9960 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
9961 | case IORING_RSRC_BUFFER: |
9962 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
9963 | } |
9964 | return -EINVAL; | |
9965 | } | |
9966 | ||
c3bdad02 PB |
9967 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
9968 | unsigned nr_args) | |
98f0b3b4 | 9969 | { |
c3bdad02 | 9970 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
9971 | |
9972 | if (!nr_args) | |
9973 | return -EINVAL; | |
c3bdad02 PB |
9974 | memset(&up, 0, sizeof(up)); |
9975 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
9976 | return -EFAULT; | |
9977 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
9978 | } | |
9979 | ||
9980 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 9981 | unsigned size, unsigned type) |
c3bdad02 PB |
9982 | { |
9983 | struct io_uring_rsrc_update2 up; | |
9984 | ||
9985 | if (size != sizeof(up)) | |
9986 | return -EINVAL; | |
98f0b3b4 PB |
9987 | if (copy_from_user(&up, arg, sizeof(up))) |
9988 | return -EFAULT; | |
992da01a | 9989 | if (!up.nr || up.resv) |
98f0b3b4 | 9990 | return -EINVAL; |
992da01a | 9991 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
9992 | } |
9993 | ||
792e3582 | 9994 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 9995 | unsigned int size, unsigned int type) |
792e3582 PB |
9996 | { |
9997 | struct io_uring_rsrc_register rr; | |
9998 | ||
9999 | /* keep it extendible */ | |
10000 | if (size != sizeof(rr)) | |
10001 | return -EINVAL; | |
10002 | ||
10003 | memset(&rr, 0, sizeof(rr)); | |
10004 | if (copy_from_user(&rr, arg, size)) | |
10005 | return -EFAULT; | |
992da01a | 10006 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10007 | return -EINVAL; |
10008 | ||
992da01a | 10009 | switch (type) { |
792e3582 PB |
10010 | case IORING_RSRC_FILE: |
10011 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10012 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10013 | case IORING_RSRC_BUFFER: |
10014 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10015 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10016 | } |
10017 | return -EINVAL; | |
10018 | } | |
10019 | ||
fe76421d JA |
10020 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10021 | unsigned len) | |
10022 | { | |
10023 | struct io_uring_task *tctx = current->io_uring; | |
10024 | cpumask_var_t new_mask; | |
10025 | int ret; | |
10026 | ||
10027 | if (!tctx || !tctx->io_wq) | |
10028 | return -EINVAL; | |
10029 | ||
10030 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10031 | return -ENOMEM; | |
10032 | ||
10033 | cpumask_clear(new_mask); | |
10034 | if (len > cpumask_size()) | |
10035 | len = cpumask_size(); | |
10036 | ||
10037 | if (copy_from_user(new_mask, arg, len)) { | |
10038 | free_cpumask_var(new_mask); | |
10039 | return -EFAULT; | |
10040 | } | |
10041 | ||
10042 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10043 | free_cpumask_var(new_mask); | |
10044 | return ret; | |
10045 | } | |
10046 | ||
10047 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10048 | { | |
10049 | struct io_uring_task *tctx = current->io_uring; | |
10050 | ||
10051 | if (!tctx || !tctx->io_wq) | |
10052 | return -EINVAL; | |
10053 | ||
10054 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10055 | } | |
10056 | ||
071698e1 JA |
10057 | static bool io_register_op_must_quiesce(int op) |
10058 | { | |
10059 | switch (op) { | |
bd54b6fe BM |
10060 | case IORING_REGISTER_BUFFERS: |
10061 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10062 | case IORING_REGISTER_FILES: |
071698e1 JA |
10063 | case IORING_UNREGISTER_FILES: |
10064 | case IORING_REGISTER_FILES_UPDATE: | |
10065 | case IORING_REGISTER_PROBE: | |
10066 | case IORING_REGISTER_PERSONALITY: | |
10067 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10068 | case IORING_REGISTER_FILES2: |
10069 | case IORING_REGISTER_FILES_UPDATE2: | |
10070 | case IORING_REGISTER_BUFFERS2: | |
10071 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10072 | case IORING_REGISTER_IOWQ_AFF: |
10073 | case IORING_UNREGISTER_IOWQ_AFF: | |
071698e1 JA |
10074 | return false; |
10075 | default: | |
10076 | return true; | |
10077 | } | |
10078 | } | |
10079 | ||
e73c5c7c PB |
10080 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10081 | { | |
10082 | long ret; | |
10083 | ||
10084 | percpu_ref_kill(&ctx->refs); | |
10085 | ||
10086 | /* | |
10087 | * Drop uring mutex before waiting for references to exit. If another | |
10088 | * thread is currently inside io_uring_enter() it might need to grab the | |
10089 | * uring_lock to make progress. If we hold it here across the drain | |
10090 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10091 | * no new references will come in after we've killed the percpu ref. | |
10092 | */ | |
10093 | mutex_unlock(&ctx->uring_lock); | |
10094 | do { | |
10095 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10096 | if (!ret) | |
10097 | break; | |
10098 | ret = io_run_task_work_sig(); | |
10099 | } while (ret >= 0); | |
10100 | mutex_lock(&ctx->uring_lock); | |
10101 | ||
10102 | if (ret) | |
10103 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10104 | return ret; | |
10105 | } | |
10106 | ||
edafccee JA |
10107 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10108 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10109 | __releases(ctx->uring_lock) |
10110 | __acquires(ctx->uring_lock) | |
edafccee JA |
10111 | { |
10112 | int ret; | |
10113 | ||
35fa71a0 JA |
10114 | /* |
10115 | * We're inside the ring mutex, if the ref is already dying, then | |
10116 | * someone else killed the ctx or is already going through | |
10117 | * io_uring_register(). | |
10118 | */ | |
10119 | if (percpu_ref_is_dying(&ctx->refs)) | |
10120 | return -ENXIO; | |
10121 | ||
75c4021a PB |
10122 | if (ctx->restricted) { |
10123 | if (opcode >= IORING_REGISTER_LAST) | |
10124 | return -EINVAL; | |
10125 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10126 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10127 | return -EACCES; | |
10128 | } | |
10129 | ||
071698e1 | 10130 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10131 | ret = io_ctx_quiesce(ctx); |
10132 | if (ret) | |
f70865db | 10133 | return ret; |
05f3fb3c | 10134 | } |
edafccee JA |
10135 | |
10136 | switch (opcode) { | |
10137 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10138 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10139 | break; |
10140 | case IORING_UNREGISTER_BUFFERS: | |
10141 | ret = -EINVAL; | |
10142 | if (arg || nr_args) | |
10143 | break; | |
0a96bbe4 | 10144 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10145 | break; |
6b06314c | 10146 | case IORING_REGISTER_FILES: |
792e3582 | 10147 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10148 | break; |
10149 | case IORING_UNREGISTER_FILES: | |
10150 | ret = -EINVAL; | |
10151 | if (arg || nr_args) | |
10152 | break; | |
10153 | ret = io_sqe_files_unregister(ctx); | |
10154 | break; | |
c3a31e60 | 10155 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10156 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10157 | break; |
9b402849 | 10158 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10159 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10160 | ret = -EINVAL; |
10161 | if (nr_args != 1) | |
10162 | break; | |
10163 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10164 | if (ret) |
10165 | break; | |
10166 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10167 | ctx->eventfd_async = 1; | |
10168 | else | |
10169 | ctx->eventfd_async = 0; | |
9b402849 JA |
10170 | break; |
10171 | case IORING_UNREGISTER_EVENTFD: | |
10172 | ret = -EINVAL; | |
10173 | if (arg || nr_args) | |
10174 | break; | |
10175 | ret = io_eventfd_unregister(ctx); | |
10176 | break; | |
66f4af93 JA |
10177 | case IORING_REGISTER_PROBE: |
10178 | ret = -EINVAL; | |
10179 | if (!arg || nr_args > 256) | |
10180 | break; | |
10181 | ret = io_probe(ctx, arg, nr_args); | |
10182 | break; | |
071698e1 JA |
10183 | case IORING_REGISTER_PERSONALITY: |
10184 | ret = -EINVAL; | |
10185 | if (arg || nr_args) | |
10186 | break; | |
10187 | ret = io_register_personality(ctx); | |
10188 | break; | |
10189 | case IORING_UNREGISTER_PERSONALITY: | |
10190 | ret = -EINVAL; | |
10191 | if (arg) | |
10192 | break; | |
10193 | ret = io_unregister_personality(ctx, nr_args); | |
10194 | break; | |
7e84e1c7 SG |
10195 | case IORING_REGISTER_ENABLE_RINGS: |
10196 | ret = -EINVAL; | |
10197 | if (arg || nr_args) | |
10198 | break; | |
10199 | ret = io_register_enable_rings(ctx); | |
10200 | break; | |
21b55dbc SG |
10201 | case IORING_REGISTER_RESTRICTIONS: |
10202 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10203 | break; | |
992da01a PB |
10204 | case IORING_REGISTER_FILES2: |
10205 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10206 | break; | |
10207 | case IORING_REGISTER_FILES_UPDATE2: | |
10208 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10209 | IORING_RSRC_FILE); | |
10210 | break; | |
10211 | case IORING_REGISTER_BUFFERS2: | |
10212 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10213 | break; |
992da01a PB |
10214 | case IORING_REGISTER_BUFFERS_UPDATE: |
10215 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10216 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10217 | break; |
fe76421d JA |
10218 | case IORING_REGISTER_IOWQ_AFF: |
10219 | ret = -EINVAL; | |
10220 | if (!arg || !nr_args) | |
10221 | break; | |
10222 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10223 | break; | |
10224 | case IORING_UNREGISTER_IOWQ_AFF: | |
10225 | ret = -EINVAL; | |
10226 | if (arg || nr_args) | |
10227 | break; | |
10228 | ret = io_unregister_iowq_aff(ctx); | |
10229 | break; | |
edafccee JA |
10230 | default: |
10231 | ret = -EINVAL; | |
10232 | break; | |
10233 | } | |
10234 | ||
071698e1 | 10235 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10236 | /* bring the ctx back to life */ |
05f3fb3c | 10237 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10238 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10239 | } |
edafccee JA |
10240 | return ret; |
10241 | } | |
10242 | ||
10243 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10244 | void __user *, arg, unsigned int, nr_args) | |
10245 | { | |
10246 | struct io_ring_ctx *ctx; | |
10247 | long ret = -EBADF; | |
10248 | struct fd f; | |
10249 | ||
10250 | f = fdget(fd); | |
10251 | if (!f.file) | |
10252 | return -EBADF; | |
10253 | ||
10254 | ret = -EOPNOTSUPP; | |
10255 | if (f.file->f_op != &io_uring_fops) | |
10256 | goto out_fput; | |
10257 | ||
10258 | ctx = f.file->private_data; | |
10259 | ||
b6c23dd5 PB |
10260 | io_run_task_work(); |
10261 | ||
edafccee JA |
10262 | mutex_lock(&ctx->uring_lock); |
10263 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10264 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10265 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10266 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10267 | out_fput: |
10268 | fdput(f); | |
10269 | return ret; | |
10270 | } | |
10271 | ||
2b188cc1 JA |
10272 | static int __init io_uring_init(void) |
10273 | { | |
d7f62e82 SM |
10274 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10275 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10276 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10277 | } while (0) | |
10278 | ||
10279 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10280 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10281 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10282 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10283 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10284 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10285 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10286 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10287 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10288 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10289 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10290 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10291 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10292 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10293 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10294 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10295 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10296 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10297 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10298 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10299 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10300 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10301 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10302 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10303 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10304 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10305 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10306 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10307 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10308 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10309 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10310 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10311 | |
b0d658ec PB |
10312 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10313 | sizeof(struct io_uring_rsrc_update)); | |
10314 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10315 | sizeof(struct io_uring_rsrc_update2)); | |
10316 | /* should fit into one byte */ | |
10317 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
10318 | ||
d3656344 | 10319 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10320 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
16340eab | 10321 | |
91f245d5 JA |
10322 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10323 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10324 | return 0; |
10325 | }; | |
10326 | __initcall(io_uring_init); |