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 | |
187f08c1 | 95 | /* only define max */ |
042b0d85 | 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 | |
187f08c1 | 100 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
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, |
e342c807 | 708 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 709 | REQ_F_REISSUE_BIT, |
8c130827 | 710 | REQ_F_DONT_REISSUE_BIT, |
b8e64b53 | 711 | REQ_F_CREDS_BIT, |
20e60a38 | 712 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 713 | REQ_F_ARM_LTIMEOUT_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), | |
e342c807 PB |
753 | /* completion is deferred through io_comp_state */ |
754 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
755 | /* caller should reissue async */ |
756 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
757 | /* don't attempt request reissue, see io_rw_reissue() */ |
758 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
7b29f92d | 759 | /* supports async reads */ |
b191e2df | 760 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 761 | /* supports async writes */ |
b191e2df | 762 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
763 | /* regular file */ |
764 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
765 | /* has creds assigned */ |
766 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
767 | /* skip refcounting if not set */ |
768 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
769 | /* there is a linked timeout that has to be armed */ |
770 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_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 | ||
f237c30a | 778 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 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 | ||
0756a869 PB |
1037 | /* requests with any of those set should undergo io_disarm_next() */ |
1038 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1039 | ||
7a612350 | 1040 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1041 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1042 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1043 | struct task_struct *task, | |
3dd0c97a | 1044 | bool cancel_all); |
78cc687b | 1045 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1046 | |
d4d19c19 PB |
1047 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1048 | long res, unsigned int cflags); | |
ec9c02ad | 1049 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1050 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1051 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1052 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1053 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1054 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1055 | unsigned nr_args); |
68fb8979 | 1056 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1057 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1058 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1059 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1060 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1061 | |
907d1df3 | 1062 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1063 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1064 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1065 | |
2b188cc1 JA |
1066 | static struct kmem_cache *req_cachep; |
1067 | ||
0918682b | 1068 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1069 | |
1070 | struct sock *io_uring_get_socket(struct file *file) | |
1071 | { | |
1072 | #if defined(CONFIG_UNIX) | |
1073 | if (file->f_op == &io_uring_fops) { | |
1074 | struct io_ring_ctx *ctx = file->private_data; | |
1075 | ||
1076 | return ctx->ring_sock->sk; | |
1077 | } | |
1078 | #endif | |
1079 | return NULL; | |
1080 | } | |
1081 | EXPORT_SYMBOL(io_uring_get_socket); | |
1082 | ||
f237c30a PB |
1083 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1084 | { | |
1085 | if (!*locked) { | |
1086 | mutex_lock(&ctx->uring_lock); | |
1087 | *locked = true; | |
1088 | } | |
1089 | } | |
1090 | ||
f2f87370 PB |
1091 | #define io_for_each_link(pos, head) \ |
1092 | for (pos = (head); pos; pos = pos->link) | |
1093 | ||
21c843d5 PB |
1094 | /* |
1095 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1096 | * see commit f958d7b528b1 for details. | |
1097 | */ | |
1098 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1099 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1100 | ||
1101 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1102 | { | |
20e60a38 | 1103 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1104 | return atomic_inc_not_zero(&req->refs); |
1105 | } | |
1106 | ||
21c843d5 PB |
1107 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1108 | { | |
20e60a38 PB |
1109 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1110 | return true; | |
1111 | ||
21c843d5 PB |
1112 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1113 | return atomic_dec_and_test(&req->refs); | |
1114 | } | |
1115 | ||
1116 | static inline void req_ref_put(struct io_kiocb *req) | |
1117 | { | |
20e60a38 | 1118 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1119 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1120 | } | |
1121 | ||
1122 | static inline void req_ref_get(struct io_kiocb *req) | |
1123 | { | |
20e60a38 | 1124 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1125 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1126 | atomic_inc(&req->refs); | |
1127 | } | |
1128 | ||
48dcd38d | 1129 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1130 | { |
1131 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1132 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1133 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1134 | } |
1135 | } | |
1136 | ||
48dcd38d PB |
1137 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1138 | { | |
1139 | __io_req_set_refcount(req, 1); | |
1140 | } | |
1141 | ||
b895c9a6 | 1142 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1143 | { |
1144 | struct io_ring_ctx *ctx = req->ctx; | |
1145 | ||
269bbe5f | 1146 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1147 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1148 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1149 | } |
1150 | } | |
1151 | ||
f70865db PB |
1152 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1153 | { | |
1154 | bool got = percpu_ref_tryget(ref); | |
1155 | ||
1156 | /* already at zero, wait for ->release() */ | |
1157 | if (!got) | |
1158 | wait_for_completion(compl); | |
1159 | percpu_ref_resurrect(ref); | |
1160 | if (got) | |
1161 | percpu_ref_put(ref); | |
1162 | } | |
1163 | ||
3dd0c97a PB |
1164 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1165 | bool cancel_all) | |
08d23634 PB |
1166 | { |
1167 | struct io_kiocb *req; | |
1168 | ||
68207680 | 1169 | if (task && head->task != task) |
08d23634 | 1170 | return false; |
3dd0c97a | 1171 | if (cancel_all) |
08d23634 PB |
1172 | return true; |
1173 | ||
1174 | io_for_each_link(req, head) { | |
b05a1bcd | 1175 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1176 | return true; |
08d23634 PB |
1177 | } |
1178 | return false; | |
1179 | } | |
1180 | ||
93d2bcd2 | 1181 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1182 | { |
93d2bcd2 | 1183 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1184 | } |
4a38aed2 | 1185 | |
2b188cc1 JA |
1186 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1187 | { | |
1188 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1189 | ||
0f158b4c | 1190 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1191 | } |
1192 | ||
8eb7e2d0 PB |
1193 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1194 | { | |
1195 | return !req->timeout.off; | |
1196 | } | |
1197 | ||
f56165e6 PB |
1198 | static void io_fallback_req_func(struct work_struct *work) |
1199 | { | |
1200 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1201 | fallback_work.work); | |
1202 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1203 | struct io_kiocb *req, *tmp; | |
f237c30a | 1204 | bool locked = false; |
f56165e6 PB |
1205 | |
1206 | percpu_ref_get(&ctx->refs); | |
1207 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1208 | req->io_task_work.func(req, &locked); |
5636c00d | 1209 | |
f237c30a PB |
1210 | if (locked) { |
1211 | if (ctx->submit_state.compl_nr) | |
1212 | io_submit_flush_completions(ctx); | |
1213 | mutex_unlock(&ctx->uring_lock); | |
1214 | } | |
f56165e6 | 1215 | percpu_ref_put(&ctx->refs); |
f237c30a | 1216 | |
f56165e6 PB |
1217 | } |
1218 | ||
2b188cc1 JA |
1219 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1220 | { | |
1221 | struct io_ring_ctx *ctx; | |
78076bb6 | 1222 | int hash_bits; |
2b188cc1 JA |
1223 | |
1224 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1225 | if (!ctx) | |
1226 | return NULL; | |
1227 | ||
78076bb6 JA |
1228 | /* |
1229 | * Use 5 bits less than the max cq entries, that should give us around | |
1230 | * 32 entries per hash list if totally full and uniformly spread. | |
1231 | */ | |
1232 | hash_bits = ilog2(p->cq_entries); | |
1233 | hash_bits -= 5; | |
1234 | if (hash_bits <= 0) | |
1235 | hash_bits = 1; | |
1236 | ctx->cancel_hash_bits = hash_bits; | |
1237 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1238 | GFP_KERNEL); | |
1239 | if (!ctx->cancel_hash) | |
1240 | goto err; | |
1241 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1242 | ||
6224843d PB |
1243 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1244 | if (!ctx->dummy_ubuf) | |
1245 | goto err; | |
1246 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1247 | ctx->dummy_ubuf->ubuf = -1UL; | |
1248 | ||
21482896 | 1249 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1250 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1251 | goto err; | |
2b188cc1 JA |
1252 | |
1253 | ctx->flags = p->flags; | |
90554200 | 1254 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1255 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1256 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1257 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1258 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1259 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1260 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1261 | mutex_init(&ctx->uring_lock); |
311997b3 | 1262 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1263 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1264 | spin_lock_init(&ctx->timeout_lock); |
540e32a0 | 1265 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1266 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1267 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1268 | spin_lock_init(&ctx->rsrc_ref_lock); |
1269 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1270 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1271 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1272 | INIT_LIST_HEAD(&ctx->tctx_list); |
cd0ca2e0 | 1273 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
d0acdee2 | 1274 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1275 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1276 | return ctx; |
206aefde | 1277 | err: |
6224843d | 1278 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1279 | kfree(ctx->cancel_hash); |
206aefde JA |
1280 | kfree(ctx); |
1281 | return NULL; | |
2b188cc1 JA |
1282 | } |
1283 | ||
8f6ed49a PB |
1284 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1285 | { | |
1286 | struct io_rings *r = ctx->rings; | |
1287 | ||
1288 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1289 | ctx->cq_extra--; | |
1290 | } | |
1291 | ||
9cf7c104 | 1292 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1293 | { |
2bc9930e JA |
1294 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1295 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1296 | |
8f6ed49a | 1297 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1298 | } |
de0617e4 | 1299 | |
9d858b21 | 1300 | return false; |
de0617e4 JA |
1301 | } |
1302 | ||
c97d8a0f PB |
1303 | #define FFS_ASYNC_READ 0x1UL |
1304 | #define FFS_ASYNC_WRITE 0x2UL | |
1305 | #ifdef CONFIG_64BIT | |
1306 | #define FFS_ISREG 0x4UL | |
1307 | #else | |
1308 | #define FFS_ISREG 0x0UL | |
1309 | #endif | |
1310 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1311 | ||
1312 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1313 | { | |
1314 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1315 | } | |
1316 | ||
ce3d5aae PB |
1317 | static void io_req_track_inflight(struct io_kiocb *req) |
1318 | { | |
ce3d5aae | 1319 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1320 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1321 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1322 | } |
1323 | } | |
1324 | ||
906c6caa PB |
1325 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1326 | { | |
1327 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1328 | } | |
1329 | ||
fd08e530 PB |
1330 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1331 | { | |
906c6caa PB |
1332 | if (WARN_ON_ONCE(!req->link)) |
1333 | return NULL; | |
1334 | ||
4d13d1a4 PB |
1335 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1336 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1337 | |
1338 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1339 | io_req_set_refcount(req); |
4d13d1a4 PB |
1340 | __io_req_set_refcount(req->link, 2); |
1341 | return req->link; | |
fd08e530 PB |
1342 | } |
1343 | ||
1344 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1345 | { | |
4d13d1a4 | 1346 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1347 | return NULL; |
1348 | return __io_prep_linked_timeout(req); | |
1349 | } | |
1350 | ||
1e6fa521 JA |
1351 | static void io_prep_async_work(struct io_kiocb *req) |
1352 | { | |
1353 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1354 | struct io_ring_ctx *ctx = req->ctx; |
1355 | ||
b8e64b53 PB |
1356 | if (!(req->flags & REQ_F_CREDS)) { |
1357 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1358 | req->creds = get_current_cred(); |
b8e64b53 | 1359 | } |
003e8dcc | 1360 | |
e1d675df PB |
1361 | req->work.list.next = NULL; |
1362 | req->work.flags = 0; | |
feaadc4f PB |
1363 | if (req->flags & REQ_F_FORCE_ASYNC) |
1364 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1365 | ||
1e6fa521 JA |
1366 | if (req->flags & REQ_F_ISREG) { |
1367 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1368 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1369 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1370 | if (def->unbound_nonreg_file) |
1371 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1372 | } | |
e1d675df PB |
1373 | |
1374 | switch (req->opcode) { | |
1375 | case IORING_OP_SPLICE: | |
1376 | case IORING_OP_TEE: | |
e1d675df PB |
1377 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1378 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1379 | break; | |
1380 | } | |
561fb04a | 1381 | } |
cccf0ee8 | 1382 | |
cbdcb435 | 1383 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1384 | { |
cbdcb435 | 1385 | struct io_kiocb *cur; |
54a91f3b | 1386 | |
44eff40a PB |
1387 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1388 | struct io_ring_ctx *ctx = req->ctx; | |
1389 | ||
79ebeaee | 1390 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1391 | io_for_each_link(cur, req) |
1392 | io_prep_async_work(cur); | |
79ebeaee | 1393 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1394 | } else { |
1395 | io_for_each_link(cur, req) | |
1396 | io_prep_async_work(cur); | |
1397 | } | |
561fb04a JA |
1398 | } |
1399 | ||
f237c30a | 1400 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1401 | { |
a197f664 | 1402 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1403 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1404 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1405 | |
f237c30a PB |
1406 | /* must not take the lock, NULL it as a precaution */ |
1407 | locked = NULL; | |
1408 | ||
3bfe6106 JA |
1409 | BUG_ON(!tctx); |
1410 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1411 | |
cbdcb435 PB |
1412 | /* init ->work of the whole link before punting */ |
1413 | io_prep_async_link(req); | |
991468dc JA |
1414 | |
1415 | /* | |
1416 | * Not expected to happen, but if we do have a bug where this _can_ | |
1417 | * happen, catch it here and ensure the request is marked as | |
1418 | * canceled. That will make io-wq go through the usual work cancel | |
1419 | * procedure rather than attempt to run this request (or create a new | |
1420 | * worker for it). | |
1421 | */ | |
1422 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1423 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1424 | ||
d07f1e8a PB |
1425 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1426 | &req->work, req->flags); | |
ebf93667 | 1427 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1428 | if (link) |
1429 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1430 | } |
1431 | ||
1ee4160c | 1432 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1433 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1434 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1435 | { |
e8c2bc1f | 1436 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1437 | |
fd9c7bc5 | 1438 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
01cec8c1 PB |
1439 | atomic_set(&req->ctx->cq_timeouts, |
1440 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1441 | list_del_init(&req->timeout.list); |
d4d19c19 | 1442 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1443 | io_put_req_deferred(req); |
5262f567 JA |
1444 | } |
1445 | } | |
1446 | ||
441b8a78 | 1447 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1448 | { |
441b8a78 | 1449 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1450 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1451 | struct io_defer_entry, list); | |
de0617e4 | 1452 | |
9cf7c104 | 1453 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1454 | break; |
27dc8338 | 1455 | list_del_init(&de->list); |
907d1df3 | 1456 | io_req_task_queue(de->req); |
27dc8338 | 1457 | kfree(de); |
441b8a78 | 1458 | } |
04518945 PB |
1459 | } |
1460 | ||
360428f8 | 1461 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1462 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1463 | { |
441b8a78 | 1464 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1465 | |
79ebeaee | 1466 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1467 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1468 | u32 events_needed, events_got; |
360428f8 | 1469 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1470 | struct io_kiocb, timeout.list); |
de0617e4 | 1471 | |
8eb7e2d0 | 1472 | if (io_is_timeout_noseq(req)) |
360428f8 | 1473 | break; |
f010505b MDG |
1474 | |
1475 | /* | |
1476 | * Since seq can easily wrap around over time, subtract | |
1477 | * the last seq at which timeouts were flushed before comparing. | |
1478 | * Assuming not more than 2^31-1 events have happened since, | |
1479 | * these subtractions won't have wrapped, so we can check if | |
1480 | * target is in [last_seq, current_seq] by comparing the two. | |
1481 | */ | |
1482 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1483 | events_got = seq - ctx->cq_last_tm_flush; | |
1484 | if (events_got < events_needed) | |
360428f8 | 1485 | break; |
bfe68a22 | 1486 | |
135fcde8 | 1487 | list_del_init(&req->timeout.list); |
1ee4160c | 1488 | io_kill_timeout(req, 0); |
f18ee4cf | 1489 | } |
f010505b | 1490 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1491 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1492 | } |
5262f567 | 1493 | |
2335f6f5 | 1494 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1495 | { |
2335f6f5 PB |
1496 | if (ctx->off_timeout_used) |
1497 | io_flush_timeouts(ctx); | |
1498 | if (ctx->drain_active) | |
1499 | io_queue_deferred(ctx); | |
1500 | } | |
1501 | ||
1502 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1503 | { | |
1504 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1505 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1506 | /* order cqe stores with ring update */ |
1507 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1508 | } |
1509 | ||
90554200 JA |
1510 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1511 | { | |
1512 | struct io_rings *r = ctx->rings; | |
1513 | ||
a566c556 | 1514 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1515 | } |
1516 | ||
888aae2e PB |
1517 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1518 | { | |
1519 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1520 | } | |
1521 | ||
d068b506 | 1522 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1523 | { |
75b28aff | 1524 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1525 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1526 | |
115e12e5 SB |
1527 | /* |
1528 | * writes to the cq entry need to come after reading head; the | |
1529 | * control dependency is enough as we're using WRITE_ONCE to | |
1530 | * fill the cq entry | |
1531 | */ | |
a566c556 | 1532 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1533 | return NULL; |
1534 | ||
888aae2e | 1535 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1536 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1537 | } |
1538 | ||
f2842ab5 JA |
1539 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1540 | { | |
44c769de | 1541 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1542 | return false; |
7e55a19c SG |
1543 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1544 | return false; | |
44c769de | 1545 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1546 | } |
1547 | ||
2c5d763c JA |
1548 | /* |
1549 | * This should only get called when at least one event has been posted. | |
1550 | * Some applications rely on the eventfd notification count only changing | |
1551 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1552 | * 1:1 relationship between how many times this function is called (and | |
1553 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1554 | */ | |
b41e9852 | 1555 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1556 | { |
5fd46178 JA |
1557 | /* |
1558 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1559 | * io_should_wake() handle the termination of the loop and only | |
1560 | * wake as many waiters as we need to. | |
1561 | */ | |
1562 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1563 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1564 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1565 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1566 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1567 | eventfd_signal(ctx->cq_ev_fd, 1); |
311997b3 PB |
1568 | if (waitqueue_active(&ctx->poll_wait)) { |
1569 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1570 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1571 | } | |
1d7bb1d5 JA |
1572 | } |
1573 | ||
80c18e4a PB |
1574 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1575 | { | |
1576 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
5fd46178 JA |
1577 | if (wq_has_sleeper(&ctx->cq_wait)) |
1578 | wake_up_all(&ctx->cq_wait); | |
80c18e4a PB |
1579 | } |
1580 | if (io_should_trigger_evfd(ctx)) | |
1581 | eventfd_signal(ctx->cq_ev_fd, 1); | |
311997b3 PB |
1582 | if (waitqueue_active(&ctx->poll_wait)) { |
1583 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1584 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1585 | } | |
80c18e4a PB |
1586 | } |
1587 | ||
c4a2ed72 | 1588 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1589 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1590 | { |
b18032bb | 1591 | bool all_flushed, posted; |
1d7bb1d5 | 1592 | |
a566c556 | 1593 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1594 | return false; |
1d7bb1d5 | 1595 | |
b18032bb | 1596 | posted = false; |
79ebeaee | 1597 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1598 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1599 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1600 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1601 | |
1d7bb1d5 JA |
1602 | if (!cqe && !force) |
1603 | break; | |
6c2450ae PB |
1604 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1605 | struct io_overflow_cqe, list); | |
1606 | if (cqe) | |
1607 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1608 | else | |
8f6ed49a PB |
1609 | io_account_cq_overflow(ctx); |
1610 | ||
b18032bb | 1611 | posted = true; |
6c2450ae PB |
1612 | list_del(&ocqe->list); |
1613 | kfree(ocqe); | |
1d7bb1d5 JA |
1614 | } |
1615 | ||
09e88404 PB |
1616 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1617 | if (all_flushed) { | |
5ed7a37d | 1618 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1619 | WRITE_ONCE(ctx->rings->sq_flags, |
1620 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1621 | } |
46930143 | 1622 | |
b18032bb JA |
1623 | if (posted) |
1624 | io_commit_cqring(ctx); | |
79ebeaee | 1625 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1626 | if (posted) |
1627 | io_cqring_ev_posted(ctx); | |
09e88404 | 1628 | return all_flushed; |
1d7bb1d5 JA |
1629 | } |
1630 | ||
90f67366 | 1631 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1632 | { |
ca0a2651 JA |
1633 | bool ret = true; |
1634 | ||
5ed7a37d | 1635 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1636 | /* iopoll syncs against uring_lock, not completion_lock */ |
1637 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1638 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1639 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1640 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1641 | mutex_unlock(&ctx->uring_lock); | |
1642 | } | |
ca0a2651 JA |
1643 | |
1644 | return ret; | |
6c503150 PB |
1645 | } |
1646 | ||
6a290a14 PB |
1647 | /* must to be called somewhat shortly after putting a request */ |
1648 | static inline void io_put_task(struct task_struct *task, int nr) | |
1649 | { | |
1650 | struct io_uring_task *tctx = task->io_uring; | |
1651 | ||
e98e49b2 PB |
1652 | if (likely(task == current)) { |
1653 | tctx->cached_refs += nr; | |
1654 | } else { | |
1655 | percpu_counter_sub(&tctx->inflight, nr); | |
1656 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1657 | wake_up(&tctx->wait); | |
1658 | put_task_struct_many(task, nr); | |
1659 | } | |
6a290a14 PB |
1660 | } |
1661 | ||
d4d19c19 PB |
1662 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1663 | long res, unsigned int cflags) | |
2b188cc1 | 1664 | { |
cce4b8b0 | 1665 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1666 | |
cce4b8b0 PB |
1667 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1668 | if (!ocqe) { | |
1669 | /* | |
1670 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1671 | * or cannot allocate an overflow entry, then we need to drop it | |
1672 | * on the floor. | |
1673 | */ | |
8f6ed49a | 1674 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1675 | return false; |
2b188cc1 | 1676 | } |
cce4b8b0 | 1677 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1678 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1679 | WRITE_ONCE(ctx->rings->sq_flags, |
1680 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1681 | ||
cce4b8b0 | 1682 | } |
d4d19c19 | 1683 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1684 | ocqe->cqe.res = res; |
1685 | ocqe->cqe.flags = cflags; | |
1686 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1687 | return true; | |
2b188cc1 JA |
1688 | } |
1689 | ||
d4d19c19 PB |
1690 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1691 | long res, unsigned int cflags) | |
2b188cc1 JA |
1692 | { |
1693 | struct io_uring_cqe *cqe; | |
1694 | ||
d4d19c19 | 1695 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1696 | |
2b188cc1 JA |
1697 | /* |
1698 | * If we can't get a cq entry, userspace overflowed the | |
1699 | * submission (by quite a lot). Increment the overflow count in | |
1700 | * the ring. | |
1701 | */ | |
d068b506 | 1702 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1703 | if (likely(cqe)) { |
d4d19c19 | 1704 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1705 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1706 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1707 | return true; |
2b188cc1 | 1708 | } |
d4d19c19 | 1709 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1710 | } |
1711 | ||
8d13326e | 1712 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1713 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1714 | long res, unsigned int cflags) | |
bcda7baa | 1715 | { |
d4d19c19 | 1716 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1717 | } |
1718 | ||
7a612350 PB |
1719 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1720 | unsigned int cflags) | |
2b188cc1 | 1721 | { |
78e19bbe | 1722 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1723 | |
79ebeaee | 1724 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1725 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1726 | /* |
1727 | * If we're the last reference to this request, add to our locked | |
1728 | * free_list cache. | |
1729 | */ | |
de9b4cca | 1730 | if (req_ref_put_and_test(req)) { |
7a612350 | 1731 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1732 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1733 | io_disarm_next(req); |
1734 | if (req->link) { | |
1735 | io_req_task_queue(req->link); | |
1736 | req->link = NULL; | |
1737 | } | |
1738 | } | |
c7dae4ba JA |
1739 | io_dismantle_req(req); |
1740 | io_put_task(req->task, 1); | |
bb943b82 | 1741 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
d0acdee2 | 1742 | ctx->locked_free_nr++; |
180f829f PB |
1743 | } else { |
1744 | if (!percpu_ref_tryget(&ctx->refs)) | |
1745 | req = NULL; | |
1746 | } | |
7a612350 | 1747 | io_commit_cqring(ctx); |
79ebeaee | 1748 | spin_unlock(&ctx->completion_lock); |
7a612350 | 1749 | |
180f829f PB |
1750 | if (req) { |
1751 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1752 | percpu_ref_put(&ctx->refs); |
180f829f | 1753 | } |
229a7b63 JA |
1754 | } |
1755 | ||
4e3d9ff9 JA |
1756 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1757 | { | |
c854357b | 1758 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1759 | } |
1760 | ||
a38d68db | 1761 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1762 | unsigned int cflags) |
229a7b63 | 1763 | { |
4e3d9ff9 | 1764 | if (io_req_needs_clean(req)) |
68fb8979 | 1765 | io_clean_op(req); |
a38d68db PB |
1766 | req->result = res; |
1767 | req->compl.cflags = cflags; | |
e342c807 | 1768 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1769 | } |
1770 | ||
889fca73 PB |
1771 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1772 | long res, unsigned cflags) | |
bcda7baa | 1773 | { |
889fca73 PB |
1774 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1775 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1776 | else |
c7dae4ba | 1777 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1778 | } |
1779 | ||
a38d68db | 1780 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1781 | { |
889fca73 | 1782 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1783 | } |
1784 | ||
f41db273 PB |
1785 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1786 | { | |
93d2bcd2 | 1787 | req_set_fail(req); |
f41db273 PB |
1788 | io_req_complete_post(req, res, 0); |
1789 | } | |
1790 | ||
864ea921 PB |
1791 | /* |
1792 | * Don't initialise the fields below on every allocation, but do that in | |
1793 | * advance and keep them valid across allocations. | |
1794 | */ | |
1795 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1796 | { | |
1797 | req->ctx = ctx; | |
1798 | req->link = NULL; | |
1799 | req->async_data = NULL; | |
1800 | /* not necessary, but safer to zero */ | |
1801 | req->result = 0; | |
1802 | } | |
1803 | ||
dac7a098 | 1804 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1805 | struct io_submit_state *state) |
dac7a098 | 1806 | { |
79ebeaee | 1807 | spin_lock(&ctx->completion_lock); |
cd0ca2e0 | 1808 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1809 | ctx->locked_free_nr = 0; |
79ebeaee | 1810 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1811 | } |
1812 | ||
dd78f492 | 1813 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1814 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1815 | { |
c7dae4ba | 1816 | struct io_submit_state *state = &ctx->submit_state; |
dd78f492 | 1817 | int nr; |
0ddf92e8 | 1818 | |
c7dae4ba JA |
1819 | /* |
1820 | * If we have more than a batch's worth of requests in our IRQ side | |
1821 | * locked cache, grab the lock and move them over to our submission | |
1822 | * side cache. | |
1823 | */ | |
d0acdee2 | 1824 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1825 | io_flush_cached_locked_reqs(ctx, state); |
0ddf92e8 | 1826 | |
dd78f492 | 1827 | nr = state->free_reqs; |
cd0ca2e0 PB |
1828 | while (!list_empty(&state->free_list)) { |
1829 | struct io_kiocb *req = list_first_entry(&state->free_list, | |
bb943b82 | 1830 | struct io_kiocb, inflight_entry); |
dd78f492 | 1831 | |
bb943b82 | 1832 | list_del(&req->inflight_entry); |
dd78f492 PB |
1833 | state->reqs[nr++] = req; |
1834 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1835 | break; |
1b4c351f JA |
1836 | } |
1837 | ||
dd78f492 PB |
1838 | state->free_reqs = nr; |
1839 | return nr != 0; | |
0ddf92e8 JA |
1840 | } |
1841 | ||
5d5901a3 PB |
1842 | /* |
1843 | * A request might get retired back into the request caches even before opcode | |
1844 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1845 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1846 | * and with extra caution to not get a request that is still worked on. | |
1847 | */ | |
e5d1bc0a | 1848 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1849 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1850 | { |
e5d1bc0a | 1851 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1852 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1853 | int ret, i; | |
e5d1bc0a | 1854 | |
fe7e3257 | 1855 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1856 | |
864ea921 PB |
1857 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1858 | goto got_req; | |
e5d1bc0a | 1859 | |
864ea921 PB |
1860 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1861 | state->reqs); | |
fd6fab2c | 1862 | |
864ea921 PB |
1863 | /* |
1864 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1865 | * retry single alloc to be on the safe side. | |
1866 | */ | |
1867 | if (unlikely(ret <= 0)) { | |
1868 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1869 | if (!state->reqs[0]) | |
1870 | return NULL; | |
1871 | ret = 1; | |
2b188cc1 | 1872 | } |
864ea921 PB |
1873 | |
1874 | for (i = 0; i < ret; i++) | |
1875 | io_preinit_req(state->reqs[i], ctx); | |
1876 | state->free_reqs = ret; | |
e5d1bc0a | 1877 | got_req: |
291b2821 PB |
1878 | state->free_reqs--; |
1879 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1880 | } |
1881 | ||
e1d767f0 | 1882 | static inline void io_put_file(struct file *file) |
8da11c19 | 1883 | { |
e1d767f0 | 1884 | if (file) |
8da11c19 PB |
1885 | fput(file); |
1886 | } | |
1887 | ||
4edf20f9 | 1888 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1889 | { |
094bae49 | 1890 | unsigned int flags = req->flags; |
929a3af9 | 1891 | |
3a0a6902 PB |
1892 | if (io_req_needs_clean(req)) |
1893 | io_clean_op(req); | |
e1d767f0 PB |
1894 | if (!(flags & REQ_F_FIXED_FILE)) |
1895 | io_put_file(req->file); | |
269bbe5f BM |
1896 | if (req->fixed_rsrc_refs) |
1897 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1898 | if (req->async_data) { |
094bae49 | 1899 | kfree(req->async_data); |
99ebe4ef PB |
1900 | req->async_data = NULL; |
1901 | } | |
e65ef56d JA |
1902 | } |
1903 | ||
216578e5 | 1904 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1905 | { |
51a4cc11 | 1906 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1907 | |
216578e5 | 1908 | io_dismantle_req(req); |
7c660731 | 1909 | io_put_task(req->task, 1); |
c6ca97b3 | 1910 | |
79ebeaee | 1911 | spin_lock(&ctx->completion_lock); |
bb943b82 | 1912 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
c34b025f | 1913 | ctx->locked_free_nr++; |
79ebeaee | 1914 | spin_unlock(&ctx->completion_lock); |
c34b025f | 1915 | |
ecfc5177 | 1916 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1917 | } |
1918 | ||
f2f87370 PB |
1919 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1920 | { | |
1921 | struct io_kiocb *nxt = req->link; | |
1922 | ||
1923 | req->link = nxt->link; | |
1924 | nxt->link = NULL; | |
1925 | } | |
1926 | ||
33cc89a9 PB |
1927 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1928 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 1929 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 1930 | { |
33cc89a9 | 1931 | struct io_kiocb *link = req->link; |
f2f87370 | 1932 | |
b97e736a | 1933 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 1934 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 1935 | |
f2f87370 | 1936 | io_remove_next_linked(req); |
90cd7e42 | 1937 | link->timeout.head = NULL; |
fd9c7bc5 | 1938 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
d4d19c19 PB |
1939 | io_cqring_fill_event(link->ctx, link->user_data, |
1940 | -ECANCELED, 0); | |
91c2f697 | 1941 | io_put_req_deferred(link); |
d4729fbd | 1942 | return true; |
c9abd7ad PB |
1943 | } |
1944 | } | |
d4729fbd | 1945 | return false; |
7c86ffee PB |
1946 | } |
1947 | ||
d148ca4b | 1948 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1949 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1950 | { |
33cc89a9 | 1951 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1952 | |
f2f87370 | 1953 | req->link = NULL; |
f2f87370 PB |
1954 | while (link) { |
1955 | nxt = link->link; | |
1956 | link->link = NULL; | |
2665abfd | 1957 | |
f2f87370 | 1958 | trace_io_uring_fail_link(req, link); |
d4d19c19 | 1959 | io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0); |
91c2f697 | 1960 | io_put_req_deferred(link); |
f2f87370 | 1961 | link = nxt; |
9e645e11 | 1962 | } |
33cc89a9 | 1963 | } |
9e645e11 | 1964 | |
33cc89a9 PB |
1965 | static bool io_disarm_next(struct io_kiocb *req) |
1966 | __must_hold(&req->ctx->completion_lock) | |
1967 | { | |
1968 | bool posted = false; | |
1969 | ||
0756a869 PB |
1970 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
1971 | struct io_kiocb *link = req->link; | |
1972 | ||
906c6caa | 1973 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
1974 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
1975 | io_remove_next_linked(req); | |
1976 | io_cqring_fill_event(link->ctx, link->user_data, | |
1977 | -ECANCELED, 0); | |
1978 | io_put_req_deferred(link); | |
1979 | posted = true; | |
1980 | } | |
1981 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
1982 | struct io_ring_ctx *ctx = req->ctx; |
1983 | ||
1984 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 1985 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
1986 | spin_unlock_irq(&ctx->timeout_lock); |
1987 | } | |
93d2bcd2 | 1988 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 1989 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
1990 | posted |= (req->link != NULL); |
1991 | io_fail_links(req); | |
1992 | } | |
1993 | return posted; | |
9e645e11 JA |
1994 | } |
1995 | ||
3fa5e0f3 | 1996 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 1997 | { |
33cc89a9 | 1998 | struct io_kiocb *nxt; |
944e58bf | 1999 | |
9e645e11 JA |
2000 | /* |
2001 | * If LINK is set, we have dependent requests in this chain. If we | |
2002 | * didn't fail this request, queue the first one up, moving any other | |
2003 | * dependencies to the next request. In case of failure, fail the rest | |
2004 | * of the chain. | |
2005 | */ | |
0756a869 | 2006 | if (req->flags & IO_DISARM_MASK) { |
33cc89a9 | 2007 | struct io_ring_ctx *ctx = req->ctx; |
33cc89a9 PB |
2008 | bool posted; |
2009 | ||
79ebeaee | 2010 | spin_lock(&ctx->completion_lock); |
33cc89a9 PB |
2011 | posted = io_disarm_next(req); |
2012 | if (posted) | |
2013 | io_commit_cqring(req->ctx); | |
79ebeaee | 2014 | spin_unlock(&ctx->completion_lock); |
33cc89a9 PB |
2015 | if (posted) |
2016 | io_cqring_ev_posted(ctx); | |
f2f87370 | 2017 | } |
33cc89a9 PB |
2018 | nxt = req->link; |
2019 | req->link = NULL; | |
2020 | return nxt; | |
4d7dd462 | 2021 | } |
9e645e11 | 2022 | |
f2f87370 | 2023 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2024 | { |
cdbff982 | 2025 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2026 | return NULL; |
2027 | return __io_req_find_next(req); | |
2028 | } | |
2029 | ||
f237c30a | 2030 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2031 | { |
2032 | if (!ctx) | |
2033 | return; | |
f237c30a | 2034 | if (*locked) { |
99c8bc52 HX |
2035 | if (ctx->submit_state.compl_nr) |
2036 | io_submit_flush_completions(ctx); | |
2c32395d | 2037 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2038 | *locked = false; |
2c32395d PB |
2039 | } |
2040 | percpu_ref_put(&ctx->refs); | |
2041 | } | |
2042 | ||
7cbf1722 | 2043 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2044 | { |
f237c30a | 2045 | bool locked = false; |
ebd0df2e | 2046 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2047 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2048 | task_work); | |
c40f6379 | 2049 | |
16f72070 | 2050 | while (1) { |
3f18407d PB |
2051 | struct io_wq_work_node *node; |
2052 | ||
2053 | spin_lock_irq(&tctx->task_lock); | |
c6538be9 | 2054 | node = tctx->task_list.first; |
3f18407d | 2055 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2056 | if (!node) |
2057 | tctx->task_running = false; | |
3f18407d | 2058 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2059 | if (!node) |
2060 | break; | |
3f18407d | 2061 | |
6294f368 | 2062 | do { |
3f18407d PB |
2063 | struct io_wq_work_node *next = node->next; |
2064 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2065 | io_task_work.node); | |
2066 | ||
2067 | if (req->ctx != ctx) { | |
f237c30a | 2068 | ctx_flush_and_put(ctx, &locked); |
3f18407d PB |
2069 | ctx = req->ctx; |
2070 | percpu_ref_get(&ctx->refs); | |
2071 | } | |
f237c30a | 2072 | req->io_task_work.func(req, &locked); |
3f18407d | 2073 | node = next; |
6294f368 PB |
2074 | } while (node); |
2075 | ||
7cbf1722 | 2076 | cond_resched(); |
3f18407d | 2077 | } |
ebd0df2e | 2078 | |
f237c30a | 2079 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2080 | } |
2081 | ||
e09ee510 | 2082 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2083 | { |
c15b79de | 2084 | struct task_struct *tsk = req->task; |
7cbf1722 | 2085 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2086 | enum task_work_notify_mode notify; |
e09ee510 | 2087 | struct io_wq_work_node *node; |
0b81e80c | 2088 | unsigned long flags; |
6294f368 | 2089 | bool running; |
7cbf1722 JA |
2090 | |
2091 | WARN_ON_ONCE(!tctx); | |
2092 | ||
0b81e80c | 2093 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2094 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2095 | running = tctx->task_running; |
2096 | if (!running) | |
2097 | tctx->task_running = true; | |
0b81e80c | 2098 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2099 | |
2100 | /* task_work already pending, we're done */ | |
6294f368 | 2101 | if (running) |
e09ee510 | 2102 | return; |
7cbf1722 | 2103 | |
c15b79de PB |
2104 | /* |
2105 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2106 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2107 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2108 | * will do the job. | |
2109 | */ | |
2110 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2111 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2112 | wake_up_process(tsk); | |
e09ee510 | 2113 | return; |
c15b79de | 2114 | } |
2215bed9 | 2115 | |
0b81e80c | 2116 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2117 | tctx->task_running = false; |
e09ee510 PB |
2118 | node = tctx->task_list.first; |
2119 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2120 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2121 | |
e09ee510 PB |
2122 | while (node) { |
2123 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2124 | node = node->next; | |
2125 | if (llist_add(&req->io_task_work.fallback_node, | |
2126 | &req->ctx->fallback_llist)) | |
2127 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2128 | } | |
eab30c4d PB |
2129 | } |
2130 | ||
f237c30a | 2131 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2132 | { |
87ceb6a6 | 2133 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2134 | |
e83acd7d | 2135 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
f237c30a | 2136 | io_tw_lock(ctx, locked); |
2593553a | 2137 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2138 | } |
2139 | ||
f237c30a | 2140 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2141 | { |
2142 | struct io_ring_ctx *ctx = req->ctx; | |
2143 | ||
04fc6c80 | 2144 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
f237c30a | 2145 | io_tw_lock(ctx, locked); |
316319e8 | 2146 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2147 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2148 | __io_queue_sqe(req); |
81b6d05c | 2149 | else |
2593553a | 2150 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2151 | } |
2152 | ||
2c4b8eb6 | 2153 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2154 | { |
2c4b8eb6 | 2155 | req->result = ret; |
5b0a6acc | 2156 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2157 | io_req_task_work_add(req); |
c40f6379 JA |
2158 | } |
2159 | ||
2c4b8eb6 | 2160 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2161 | { |
5b0a6acc | 2162 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2163 | io_req_task_work_add(req); |
a3df7698 PB |
2164 | } |
2165 | ||
773af691 JA |
2166 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2167 | { | |
2168 | req->io_task_work.func = io_queue_async_work; | |
2169 | io_req_task_work_add(req); | |
2170 | } | |
2171 | ||
f2f87370 | 2172 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2173 | { |
9b5f7bd9 | 2174 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2175 | |
2176 | if (nxt) | |
906a8c3f | 2177 | io_req_task_queue(nxt); |
c69f8dbe JL |
2178 | } |
2179 | ||
c3524383 | 2180 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2181 | { |
c3524383 PB |
2182 | io_queue_next(req); |
2183 | __io_free_req(req); | |
2184 | } | |
8766dd51 | 2185 | |
f237c30a PB |
2186 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2187 | { | |
2188 | io_free_req(req); | |
2189 | } | |
2190 | ||
2d6500d4 | 2191 | struct req_batch { |
5af1d13e PB |
2192 | struct task_struct *task; |
2193 | int task_refs; | |
1b4c351f | 2194 | int ctx_refs; |
2d6500d4 PB |
2195 | }; |
2196 | ||
5af1d13e PB |
2197 | static inline void io_init_req_batch(struct req_batch *rb) |
2198 | { | |
5af1d13e | 2199 | rb->task_refs = 0; |
9ae72463 | 2200 | rb->ctx_refs = 0; |
5af1d13e PB |
2201 | rb->task = NULL; |
2202 | } | |
2203 | ||
2d6500d4 PB |
2204 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2205 | struct req_batch *rb) | |
2206 | { | |
9ae72463 PB |
2207 | if (rb->ctx_refs) |
2208 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
e98e49b2 | 2209 | if (rb->task) |
e9dbe221 | 2210 | io_put_task(rb->task, rb->task_refs); |
2d6500d4 PB |
2211 | } |
2212 | ||
6ff119a6 PB |
2213 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2214 | struct io_submit_state *state) | |
2d6500d4 | 2215 | { |
f2f87370 | 2216 | io_queue_next(req); |
96670657 | 2217 | io_dismantle_req(req); |
2d6500d4 | 2218 | |
e3bc8e9d | 2219 | if (req->task != rb->task) { |
7c660731 PB |
2220 | if (rb->task) |
2221 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2222 | rb->task = req->task; |
2223 | rb->task_refs = 0; | |
5af1d13e | 2224 | } |
e3bc8e9d | 2225 | rb->task_refs++; |
9ae72463 | 2226 | rb->ctx_refs++; |
5af1d13e | 2227 | |
bd759045 | 2228 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2229 | state->reqs[state->free_reqs++] = req; |
bd759045 | 2230 | else |
cd0ca2e0 | 2231 | list_add(&req->inflight_entry, &state->free_list); |
7a743e22 PB |
2232 | } |
2233 | ||
2a2758f2 | 2234 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2235 | __must_hold(&ctx->uring_lock) |
905c172f | 2236 | { |
cd0ca2e0 PB |
2237 | struct io_submit_state *state = &ctx->submit_state; |
2238 | int i, nr = state->compl_nr; | |
905c172f PB |
2239 | struct req_batch rb; |
2240 | ||
79ebeaee | 2241 | spin_lock(&ctx->completion_lock); |
905c172f | 2242 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2243 | struct io_kiocb *req = state->compl_reqs[i]; |
5182ed2e | 2244 | |
d4d19c19 PB |
2245 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2246 | req->compl.cflags); | |
905c172f PB |
2247 | } |
2248 | io_commit_cqring(ctx); | |
79ebeaee | 2249 | spin_unlock(&ctx->completion_lock); |
905c172f | 2250 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2251 | |
2252 | io_init_req_batch(&rb); | |
905c172f | 2253 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2254 | struct io_kiocb *req = state->compl_reqs[i]; |
905c172f | 2255 | |
91c2f697 | 2256 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2257 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2258 | } |
2259 | ||
2260 | io_req_free_batch_finish(ctx, &rb); | |
cd0ca2e0 | 2261 | state->compl_nr = 0; |
7a743e22 PB |
2262 | } |
2263 | ||
ba816ad6 JA |
2264 | /* |
2265 | * Drop reference to request, return next in chain (if there is one) if this | |
2266 | * was the last reference to this request. | |
2267 | */ | |
0d85035a | 2268 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2269 | { |
9b5f7bd9 PB |
2270 | struct io_kiocb *nxt = NULL; |
2271 | ||
de9b4cca | 2272 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2273 | nxt = io_req_find_next(req); |
4d7dd462 | 2274 | __io_free_req(req); |
2a44f467 | 2275 | } |
9b5f7bd9 | 2276 | return nxt; |
2b188cc1 JA |
2277 | } |
2278 | ||
0d85035a | 2279 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2280 | { |
de9b4cca | 2281 | if (req_ref_put_and_test(req)) |
e65ef56d | 2282 | io_free_req(req); |
2b188cc1 JA |
2283 | } |
2284 | ||
91c2f697 | 2285 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2286 | { |
91c2f697 | 2287 | if (req_ref_put_and_test(req)) { |
f237c30a | 2288 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2289 | io_req_task_work_add(req); |
2290 | } | |
216578e5 PB |
2291 | } |
2292 | ||
6c503150 | 2293 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2294 | { |
2295 | /* See comment at the top of this file */ | |
2296 | smp_rmb(); | |
e23de15f | 2297 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2298 | } |
2299 | ||
fb5ccc98 PB |
2300 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2301 | { | |
2302 | struct io_rings *rings = ctx->rings; | |
2303 | ||
2304 | /* make sure SQ entry isn't read before tail */ | |
2305 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2306 | } | |
2307 | ||
8ff069bf | 2308 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2309 | { |
8ff069bf | 2310 | unsigned int cflags; |
e94f141b | 2311 | |
bcda7baa JA |
2312 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2313 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2314 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2315 | kfree(kbuf); |
2316 | return cflags; | |
e94f141b JA |
2317 | } |
2318 | ||
8ff069bf | 2319 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2320 | { |
4d954c25 | 2321 | struct io_buffer *kbuf; |
bcda7baa | 2322 | |
ae421d93 PB |
2323 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2324 | return 0; | |
4d954c25 | 2325 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2326 | return io_put_kbuf(req, kbuf); |
2327 | } | |
2328 | ||
4c6e277c JA |
2329 | static inline bool io_run_task_work(void) |
2330 | { | |
ef98eb04 | 2331 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2332 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2333 | tracehook_notify_signal(); |
4c6e277c JA |
2334 | return true; |
2335 | } | |
2336 | ||
2337 | return false; | |
bcda7baa JA |
2338 | } |
2339 | ||
def596e9 JA |
2340 | /* |
2341 | * Find and free completed poll iocbs | |
2342 | */ | |
2343 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
a8576af9 | 2344 | struct list_head *done) |
def596e9 | 2345 | { |
8237e045 | 2346 | struct req_batch rb; |
def596e9 | 2347 | struct io_kiocb *req; |
bbde017a XW |
2348 | |
2349 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2350 | smp_rmb(); | |
def596e9 | 2351 | |
5af1d13e | 2352 | io_init_req_batch(&rb); |
def596e9 | 2353 | while (!list_empty(done)) { |
d21ffe7e | 2354 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2355 | list_del(&req->inflight_entry); |
2356 | ||
a8576af9 | 2357 | if (READ_ONCE(req->result) == -EAGAIN && |
8c130827 | 2358 | !(req->flags & REQ_F_DONT_REISSUE)) { |
bbde017a | 2359 | req->iopoll_completed = 0; |
773af691 | 2360 | io_req_task_queue_reissue(req); |
8c130827 | 2361 | continue; |
bbde017a | 2362 | } |
def596e9 | 2363 | |
ae421d93 PB |
2364 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2365 | io_put_rw_kbuf(req)); | |
def596e9 JA |
2366 | (*nr_events)++; |
2367 | ||
de9b4cca | 2368 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2369 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2370 | } |
def596e9 | 2371 | |
09bb8394 | 2372 | io_commit_cqring(ctx); |
80c18e4a | 2373 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2374 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2375 | } |
2376 | ||
def596e9 | 2377 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
a8576af9 | 2378 | long min) |
def596e9 JA |
2379 | { |
2380 | struct io_kiocb *req, *tmp; | |
2381 | LIST_HEAD(done); | |
2382 | bool spin; | |
def596e9 JA |
2383 | |
2384 | /* | |
2385 | * Only spin for completions if we don't have multiple devices hanging | |
2386 | * off our complete list, and we're under the requested amount. | |
2387 | */ | |
915b3dde | 2388 | spin = !ctx->poll_multi_queue && *nr_events < min; |
def596e9 | 2389 | |
d21ffe7e | 2390 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2391 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2392 | int ret; |
def596e9 JA |
2393 | |
2394 | /* | |
581f9810 BM |
2395 | * Move completed and retryable entries to our local lists. |
2396 | * If we find a request that requires polling, break out | |
2397 | * and complete those lists first, if we have entries there. | |
def596e9 | 2398 | */ |
65a6543d | 2399 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2400 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2401 | continue; |
2402 | } | |
2403 | if (!list_empty(&done)) | |
2404 | break; | |
2405 | ||
2406 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
a2416e1e PB |
2407 | if (unlikely(ret < 0)) |
2408 | return ret; | |
2409 | else if (ret) | |
2410 | spin = false; | |
def596e9 | 2411 | |
3aadc23e PB |
2412 | /* iopoll may have completed current req */ |
2413 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2414 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2415 | } |
2416 | ||
2417 | if (!list_empty(&done)) | |
a8576af9 | 2418 | io_iopoll_complete(ctx, nr_events, &done); |
def596e9 | 2419 | |
a2416e1e | 2420 | return 0; |
def596e9 JA |
2421 | } |
2422 | ||
def596e9 JA |
2423 | /* |
2424 | * We can't just wait for polled events to come to us, we have to actively | |
2425 | * find and complete them. | |
2426 | */ | |
b2edc0a7 | 2427 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2428 | { |
2429 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2430 | return; | |
2431 | ||
2432 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2433 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2434 | unsigned int nr_events = 0; |
2435 | ||
a8576af9 | 2436 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2437 | |
b2edc0a7 PB |
2438 | /* let it sleep and repeat later if can't complete a request */ |
2439 | if (nr_events == 0) | |
2440 | break; | |
08f5439f JA |
2441 | /* |
2442 | * Ensure we allow local-to-the-cpu processing to take place, | |
2443 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2444 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2445 | */ |
3fcee5a6 PB |
2446 | if (need_resched()) { |
2447 | mutex_unlock(&ctx->uring_lock); | |
2448 | cond_resched(); | |
2449 | mutex_lock(&ctx->uring_lock); | |
2450 | } | |
def596e9 JA |
2451 | } |
2452 | mutex_unlock(&ctx->uring_lock); | |
2453 | } | |
2454 | ||
7668b92a | 2455 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2456 | { |
7668b92a | 2457 | unsigned int nr_events = 0; |
e9979b36 | 2458 | int ret = 0; |
500f9fba | 2459 | |
c7849be9 XW |
2460 | /* |
2461 | * We disallow the app entering submit/complete with polling, but we | |
2462 | * still need to lock the ring to prevent racing with polled issue | |
2463 | * that got punted to a workqueue. | |
2464 | */ | |
2465 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2466 | /* |
2467 | * Don't enter poll loop if we already have events pending. | |
2468 | * If we do, we can potentially be spinning for commands that | |
2469 | * already triggered a CQE (eg in error). | |
2470 | */ | |
5ed7a37d | 2471 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2472 | __io_cqring_overflow_flush(ctx, false); |
2473 | if (io_cqring_events(ctx)) | |
2474 | goto out; | |
def596e9 | 2475 | do { |
500f9fba JA |
2476 | /* |
2477 | * If a submit got punted to a workqueue, we can have the | |
2478 | * application entering polling for a command before it gets | |
2479 | * issued. That app will hold the uring_lock for the duration | |
2480 | * of the poll right here, so we need to take a breather every | |
2481 | * now and then to ensure that the issue has a chance to add | |
2482 | * the poll to the issued list. Otherwise we can spin here | |
2483 | * forever, while the workqueue is stuck trying to acquire the | |
2484 | * very same mutex. | |
2485 | */ | |
e9979b36 | 2486 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2487 | u32 tail = ctx->cached_cq_tail; |
2488 | ||
500f9fba | 2489 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2490 | io_run_task_work(); |
500f9fba | 2491 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2492 | |
8f487ef2 PB |
2493 | /* some requests don't go through iopoll_list */ |
2494 | if (tail != ctx->cached_cq_tail || | |
2495 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2496 | break; |
500f9fba | 2497 | } |
a8576af9 | 2498 | ret = io_do_iopoll(ctx, &nr_events, min); |
f39c8a5b PB |
2499 | } while (!ret && nr_events < min && !need_resched()); |
2500 | out: | |
500f9fba | 2501 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2502 | return ret; |
2503 | } | |
2504 | ||
491381ce | 2505 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2506 | { |
491381ce JA |
2507 | /* |
2508 | * Tell lockdep we inherited freeze protection from submission | |
2509 | * thread. | |
2510 | */ | |
2511 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2512 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2513 | |
1c98679d PB |
2514 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2515 | sb_end_write(sb); | |
2b188cc1 JA |
2516 | } |
2517 | } | |
2518 | ||
b63534c4 | 2519 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2520 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2521 | { |
ab454438 | 2522 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2523 | |
ab454438 PB |
2524 | if (!rw) |
2525 | return !io_req_prep_async(req); | |
2526 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2527 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2528 | return true; | |
b63534c4 | 2529 | } |
b63534c4 | 2530 | |
3e6a0d3c | 2531 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2532 | { |
355afaeb | 2533 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2534 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2535 | |
355afaeb JA |
2536 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2537 | return false; | |
3e6a0d3c JA |
2538 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2539 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2540 | return false; |
7c977a58 JA |
2541 | /* |
2542 | * If ref is dying, we might be running poll reap from the exit work. | |
2543 | * Don't attempt to reissue from that path, just let it fail with | |
2544 | * -EAGAIN. | |
2545 | */ | |
3e6a0d3c JA |
2546 | if (percpu_ref_is_dying(&ctx->refs)) |
2547 | return false; | |
ef046888 JA |
2548 | /* |
2549 | * Play it safe and assume not safe to re-import and reissue if we're | |
2550 | * not in the original thread group (or in task context). | |
2551 | */ | |
2552 | if (!same_thread_group(req->task, current) || !in_task()) | |
2553 | return false; | |
3e6a0d3c JA |
2554 | return true; |
2555 | } | |
e82ad485 | 2556 | #else |
a1ff1e3f | 2557 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2558 | { |
2559 | return false; | |
2560 | } | |
e82ad485 | 2561 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2562 | { |
b63534c4 JA |
2563 | return false; |
2564 | } | |
3e6a0d3c | 2565 | #endif |
b63534c4 | 2566 | |
8ef12efe | 2567 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2568 | { |
b65c128f PB |
2569 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2570 | kiocb_end_write(req); | |
9532b99b PB |
2571 | if (res != req->result) { |
2572 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2573 | io_rw_should_reissue(req)) { | |
2574 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2575 | return true; |
9532b99b | 2576 | } |
93d2bcd2 | 2577 | req_set_fail(req); |
8ef12efe | 2578 | req->result = res; |
9532b99b | 2579 | } |
8ef12efe JA |
2580 | return false; |
2581 | } | |
2582 | ||
f237c30a | 2583 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2584 | { |
ae421d93 | 2585 | __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req)); |
8ef12efe JA |
2586 | } |
2587 | ||
2588 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2589 | unsigned int issue_flags) | |
2590 | { | |
2591 | if (__io_complete_rw_common(req, res)) | |
2592 | return; | |
f237c30a | 2593 | __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2594 | } |
2595 | ||
2596 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2597 | { | |
9adbd45d | 2598 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2599 | |
8ef12efe JA |
2600 | if (__io_complete_rw_common(req, res)) |
2601 | return; | |
2602 | req->result = res; | |
2603 | req->io_task_work.func = io_req_task_complete; | |
2604 | io_req_task_work_add(req); | |
2b188cc1 JA |
2605 | } |
2606 | ||
def596e9 JA |
2607 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2608 | { | |
9adbd45d | 2609 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2610 | |
491381ce JA |
2611 | if (kiocb->ki_flags & IOCB_WRITE) |
2612 | kiocb_end_write(req); | |
9532b99b | 2613 | if (unlikely(res != req->result)) { |
a1ff1e3f JA |
2614 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
2615 | io_resubmit_prep(req))) { | |
93d2bcd2 | 2616 | req_set_fail(req); |
9532b99b PB |
2617 | req->flags |= REQ_F_DONT_REISSUE; |
2618 | } | |
8c130827 | 2619 | } |
bbde017a XW |
2620 | |
2621 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2622 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2623 | smp_wmb(); |
2624 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2625 | } |
2626 | ||
2627 | /* | |
2628 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2629 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2630 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2631 | * accessing the kiocb cookie. |
2632 | */ | |
cb3d8972 | 2633 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2634 | { |
2635 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2636 | const bool in_async = io_wq_current_is_worker(); |
2637 | ||
2638 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2639 | if (unlikely(in_async)) | |
2640 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2641 | |
2642 | /* | |
2643 | * Track whether we have multiple files in our lists. This will impact | |
2644 | * how we do polling eventually, not spinning if we're on potentially | |
2645 | * different devices. | |
2646 | */ | |
540e32a0 | 2647 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2648 | ctx->poll_multi_queue = false; |
2649 | } else if (!ctx->poll_multi_queue) { | |
def596e9 | 2650 | struct io_kiocb *list_req; |
915b3dde | 2651 | unsigned int queue_num0, queue_num1; |
def596e9 | 2652 | |
540e32a0 | 2653 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2654 | inflight_entry); |
915b3dde HX |
2655 | |
2656 | if (list_req->file != req->file) { | |
2657 | ctx->poll_multi_queue = true; | |
2658 | } else { | |
2659 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); | |
2660 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); | |
2661 | if (queue_num0 != queue_num1) | |
2662 | ctx->poll_multi_queue = true; | |
2663 | } | |
def596e9 JA |
2664 | } |
2665 | ||
2666 | /* | |
2667 | * For fast devices, IO may have already completed. If it has, add | |
2668 | * it to the front so we find it first. | |
2669 | */ | |
65a6543d | 2670 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2671 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2672 | else |
d21ffe7e | 2673 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2674 | |
cb3d8972 PB |
2675 | if (unlikely(in_async)) { |
2676 | /* | |
2677 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2678 | * in sq thread task context or in io worker task context. If | |
2679 | * current task context is sq thread, we don't need to check | |
2680 | * whether should wake up sq thread. | |
2681 | */ | |
2682 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2683 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2684 | wake_up(&ctx->sq_data->wait); | |
2685 | ||
2686 | mutex_unlock(&ctx->uring_lock); | |
2687 | } | |
def596e9 JA |
2688 | } |
2689 | ||
4503b767 JA |
2690 | static bool io_bdev_nowait(struct block_device *bdev) |
2691 | { | |
9ba0d0c8 | 2692 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2693 | } |
2694 | ||
2b188cc1 JA |
2695 | /* |
2696 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2697 | * any file. For now, just ensure that anything potentially problematic is done | |
2698 | * inline. | |
2699 | */ | |
b191e2df | 2700 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2701 | { |
2702 | umode_t mode = file_inode(file)->i_mode; | |
2703 | ||
4503b767 | 2704 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2705 | if (IS_ENABLED(CONFIG_BLOCK) && |
2706 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2707 | return true; |
2708 | return false; | |
2709 | } | |
976517f1 | 2710 | if (S_ISSOCK(mode)) |
2b188cc1 | 2711 | return true; |
4503b767 | 2712 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2713 | if (IS_ENABLED(CONFIG_BLOCK) && |
2714 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2715 | file->f_op != &io_uring_fops) |
2716 | return true; | |
2717 | return false; | |
2718 | } | |
2b188cc1 | 2719 | |
c5b85625 JA |
2720 | /* any ->read/write should understand O_NONBLOCK */ |
2721 | if (file->f_flags & O_NONBLOCK) | |
2722 | return true; | |
2723 | ||
af197f50 JA |
2724 | if (!(file->f_mode & FMODE_NOWAIT)) |
2725 | return false; | |
2726 | ||
2727 | if (rw == READ) | |
2728 | return file->f_op->read_iter != NULL; | |
2729 | ||
2730 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2731 | } |
2732 | ||
b191e2df | 2733 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2734 | { |
b191e2df | 2735 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2736 | return true; |
b191e2df | 2737 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2738 | return true; |
2739 | ||
b191e2df | 2740 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2741 | } |
2742 | ||
a88fc400 | 2743 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2744 | { |
def596e9 | 2745 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2746 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2747 | struct file *file = req->file; |
09bb8394 JA |
2748 | unsigned ioprio; |
2749 | int ret; | |
2b188cc1 | 2750 | |
c97d8a0f | 2751 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2752 | req->flags |= REQ_F_ISREG; |
2753 | ||
2b188cc1 | 2754 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2755 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2756 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2757 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2758 | } |
2b188cc1 | 2759 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2760 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2761 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2762 | if (unlikely(ret)) | |
2763 | return ret; | |
2b188cc1 | 2764 | |
75c668cd PB |
2765 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2766 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2767 | req->flags |= REQ_F_NOWAIT; | |
2768 | ||
2b188cc1 JA |
2769 | ioprio = READ_ONCE(sqe->ioprio); |
2770 | if (ioprio) { | |
2771 | ret = ioprio_check_cap(ioprio); | |
2772 | if (ret) | |
09bb8394 | 2773 | return ret; |
2b188cc1 JA |
2774 | |
2775 | kiocb->ki_ioprio = ioprio; | |
2776 | } else | |
2777 | kiocb->ki_ioprio = get_current_ioprio(); | |
2778 | ||
def596e9 | 2779 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2780 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2781 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2782 | return -EOPNOTSUPP; |
2b188cc1 | 2783 | |
def596e9 JA |
2784 | kiocb->ki_flags |= IOCB_HIPRI; |
2785 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2786 | req->iopoll_completed = 0; |
def596e9 | 2787 | } else { |
09bb8394 JA |
2788 | if (kiocb->ki_flags & IOCB_HIPRI) |
2789 | return -EINVAL; | |
def596e9 JA |
2790 | kiocb->ki_complete = io_complete_rw; |
2791 | } | |
9adbd45d | 2792 | |
eae071c9 PB |
2793 | if (req->opcode == IORING_OP_READ_FIXED || |
2794 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2795 | req->imu = NULL; | |
2796 | io_req_set_rsrc_node(req); | |
2797 | } | |
2798 | ||
3529d8c2 JA |
2799 | req->rw.addr = READ_ONCE(sqe->addr); |
2800 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2801 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2802 | return 0; |
2b188cc1 JA |
2803 | } |
2804 | ||
2805 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2806 | { | |
2807 | switch (ret) { | |
2808 | case -EIOCBQUEUED: | |
2809 | break; | |
2810 | case -ERESTARTSYS: | |
2811 | case -ERESTARTNOINTR: | |
2812 | case -ERESTARTNOHAND: | |
2813 | case -ERESTART_RESTARTBLOCK: | |
2814 | /* | |
2815 | * We can't just restart the syscall, since previously | |
2816 | * submitted sqes may already be in progress. Just fail this | |
2817 | * IO with EINTR. | |
2818 | */ | |
2819 | ret = -EINTR; | |
df561f66 | 2820 | fallthrough; |
2b188cc1 JA |
2821 | default: |
2822 | kiocb->ki_complete(kiocb, ret, 0); | |
2823 | } | |
2824 | } | |
2825 | ||
a1d7c393 | 2826 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2827 | unsigned int issue_flags) |
ba816ad6 | 2828 | { |
ba04291e | 2829 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2830 | struct io_async_rw *io = req->async_data; |
97284637 | 2831 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2832 | |
227c0c96 | 2833 | /* add previously done IO, if any */ |
e8c2bc1f | 2834 | if (io && io->bytes_done > 0) { |
227c0c96 | 2835 | if (ret < 0) |
e8c2bc1f | 2836 | ret = io->bytes_done; |
227c0c96 | 2837 | else |
e8c2bc1f | 2838 | ret += io->bytes_done; |
227c0c96 JA |
2839 | } |
2840 | ||
ba04291e JA |
2841 | if (req->flags & REQ_F_CUR_POS) |
2842 | req->file->f_pos = kiocb->ki_pos; | |
e149bd74 | 2843 | if (ret >= 0 && check_reissue) |
889fca73 | 2844 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2845 | else |
2846 | io_rw_done(kiocb, ret); | |
97284637 | 2847 | |
fe7e3257 | 2848 | if (check_reissue && (req->flags & REQ_F_REISSUE)) { |
97284637 | 2849 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2850 | if (io_resubmit_prep(req)) { |
773af691 | 2851 | io_req_task_queue_reissue(req); |
8c130827 | 2852 | } else { |
93d2bcd2 | 2853 | req_set_fail(req); |
ae421d93 PB |
2854 | __io_req_complete(req, issue_flags, ret, |
2855 | io_put_rw_kbuf(req)); | |
97284637 PB |
2856 | } |
2857 | } | |
ba816ad6 JA |
2858 | } |
2859 | ||
eae071c9 PB |
2860 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2861 | struct io_mapped_ubuf *imu) | |
edafccee | 2862 | { |
9adbd45d | 2863 | size_t len = req->rw.len; |
75769e3f | 2864 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2865 | size_t offset; |
edafccee | 2866 | |
75769e3f | 2867 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2868 | return -EFAULT; |
2869 | /* not inside the mapped region */ | |
4751f53d | 2870 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2871 | return -EFAULT; |
2872 | ||
2873 | /* | |
2874 | * May not be a start of buffer, set size appropriately | |
2875 | * and advance us to the beginning. | |
2876 | */ | |
2877 | offset = buf_addr - imu->ubuf; | |
2878 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2879 | |
2880 | if (offset) { | |
2881 | /* | |
2882 | * Don't use iov_iter_advance() here, as it's really slow for | |
2883 | * using the latter parts of a big fixed buffer - it iterates | |
2884 | * over each segment manually. We can cheat a bit here, because | |
2885 | * we know that: | |
2886 | * | |
2887 | * 1) it's a BVEC iter, we set it up | |
2888 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2889 | * first and last bvec | |
2890 | * | |
2891 | * So just find our index, and adjust the iterator afterwards. | |
2892 | * If the offset is within the first bvec (or the whole first | |
2893 | * bvec, just use iov_iter_advance(). This makes it easier | |
2894 | * since we can just skip the first segment, which may not | |
2895 | * be PAGE_SIZE aligned. | |
2896 | */ | |
2897 | const struct bio_vec *bvec = imu->bvec; | |
2898 | ||
2899 | if (offset <= bvec->bv_len) { | |
2900 | iov_iter_advance(iter, offset); | |
2901 | } else { | |
2902 | unsigned long seg_skip; | |
2903 | ||
2904 | /* skip first vec */ | |
2905 | offset -= bvec->bv_len; | |
2906 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2907 | ||
2908 | iter->bvec = bvec + seg_skip; | |
2909 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2910 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2911 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2912 | } |
2913 | } | |
2914 | ||
847595de | 2915 | return 0; |
edafccee JA |
2916 | } |
2917 | ||
eae071c9 PB |
2918 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2919 | { | |
2920 | struct io_ring_ctx *ctx = req->ctx; | |
2921 | struct io_mapped_ubuf *imu = req->imu; | |
2922 | u16 index, buf_index = req->buf_index; | |
2923 | ||
2924 | if (likely(!imu)) { | |
2925 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2926 | return -EFAULT; | |
2927 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2928 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2929 | req->imu = imu; | |
2930 | } | |
2931 | return __io_import_fixed(req, rw, iter, imu); | |
2932 | } | |
2933 | ||
bcda7baa JA |
2934 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2935 | { | |
2936 | if (needs_lock) | |
2937 | mutex_unlock(&ctx->uring_lock); | |
2938 | } | |
2939 | ||
2940 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2941 | { | |
2942 | /* | |
2943 | * "Normal" inline submissions always hold the uring_lock, since we | |
2944 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2945 | * The only exception is when we've detached the request and issue it | |
2946 | * from an async worker thread, grab the lock for that case. | |
2947 | */ | |
2948 | if (needs_lock) | |
2949 | mutex_lock(&ctx->uring_lock); | |
2950 | } | |
2951 | ||
2952 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
2953 | int bgid, struct io_buffer *kbuf, | |
2954 | bool needs_lock) | |
2955 | { | |
2956 | struct io_buffer *head; | |
2957 | ||
2958 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
2959 | return kbuf; | |
2960 | ||
2961 | io_ring_submit_lock(req->ctx, needs_lock); | |
2962 | ||
2963 | lockdep_assert_held(&req->ctx->uring_lock); | |
2964 | ||
9e15c3a0 | 2965 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2966 | if (head) { |
2967 | if (!list_empty(&head->list)) { | |
2968 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
2969 | list); | |
2970 | list_del(&kbuf->list); | |
2971 | } else { | |
2972 | kbuf = head; | |
9e15c3a0 | 2973 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
2974 | } |
2975 | if (*len > kbuf->len) | |
2976 | *len = kbuf->len; | |
2977 | } else { | |
2978 | kbuf = ERR_PTR(-ENOBUFS); | |
2979 | } | |
2980 | ||
2981 | io_ring_submit_unlock(req->ctx, needs_lock); | |
2982 | ||
2983 | return kbuf; | |
2984 | } | |
2985 | ||
4d954c25 JA |
2986 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
2987 | bool needs_lock) | |
2988 | { | |
2989 | struct io_buffer *kbuf; | |
4f4eeba8 | 2990 | u16 bgid; |
4d954c25 JA |
2991 | |
2992 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 2993 | bgid = req->buf_index; |
4d954c25 JA |
2994 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
2995 | if (IS_ERR(kbuf)) | |
2996 | return kbuf; | |
2997 | req->rw.addr = (u64) (unsigned long) kbuf; | |
2998 | req->flags |= REQ_F_BUFFER_SELECTED; | |
2999 | return u64_to_user_ptr(kbuf->addr); | |
3000 | } | |
3001 | ||
3002 | #ifdef CONFIG_COMPAT | |
3003 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3004 | bool needs_lock) | |
3005 | { | |
3006 | struct compat_iovec __user *uiov; | |
3007 | compat_ssize_t clen; | |
3008 | void __user *buf; | |
3009 | ssize_t len; | |
3010 | ||
3011 | uiov = u64_to_user_ptr(req->rw.addr); | |
3012 | if (!access_ok(uiov, sizeof(*uiov))) | |
3013 | return -EFAULT; | |
3014 | if (__get_user(clen, &uiov->iov_len)) | |
3015 | return -EFAULT; | |
3016 | if (clen < 0) | |
3017 | return -EINVAL; | |
3018 | ||
3019 | len = clen; | |
3020 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3021 | if (IS_ERR(buf)) | |
3022 | return PTR_ERR(buf); | |
3023 | iov[0].iov_base = buf; | |
3024 | iov[0].iov_len = (compat_size_t) len; | |
3025 | return 0; | |
3026 | } | |
3027 | #endif | |
3028 | ||
3029 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3030 | bool needs_lock) | |
3031 | { | |
3032 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3033 | void __user *buf; | |
3034 | ssize_t len; | |
3035 | ||
3036 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3037 | return -EFAULT; | |
3038 | ||
3039 | len = iov[0].iov_len; | |
3040 | if (len < 0) | |
3041 | return -EINVAL; | |
3042 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3043 | if (IS_ERR(buf)) | |
3044 | return PTR_ERR(buf); | |
3045 | iov[0].iov_base = buf; | |
3046 | iov[0].iov_len = len; | |
3047 | return 0; | |
3048 | } | |
3049 | ||
3050 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3051 | bool needs_lock) | |
3052 | { | |
dddb3e26 JA |
3053 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3054 | struct io_buffer *kbuf; | |
3055 | ||
3056 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3057 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3058 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3059 | return 0; |
dddb3e26 | 3060 | } |
dd201662 | 3061 | if (req->rw.len != 1) |
4d954c25 JA |
3062 | return -EINVAL; |
3063 | ||
3064 | #ifdef CONFIG_COMPAT | |
3065 | if (req->ctx->compat) | |
3066 | return io_compat_import(req, iov, needs_lock); | |
3067 | #endif | |
3068 | ||
3069 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3070 | } | |
3071 | ||
847595de PB |
3072 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3073 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3074 | { |
9adbd45d JA |
3075 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3076 | size_t sqe_len = req->rw.len; | |
847595de | 3077 | u8 opcode = req->opcode; |
4d954c25 | 3078 | ssize_t ret; |
edafccee | 3079 | |
7d009165 | 3080 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3081 | *iovec = NULL; |
9adbd45d | 3082 | return io_import_fixed(req, rw, iter); |
edafccee | 3083 | } |
2b188cc1 | 3084 | |
bcda7baa | 3085 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3086 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3087 | return -EINVAL; |
3088 | ||
3a6820f2 | 3089 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3090 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3091 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3092 | if (IS_ERR(buf)) |
4d954c25 | 3093 | return PTR_ERR(buf); |
3f9d6441 | 3094 | req->rw.len = sqe_len; |
bcda7baa JA |
3095 | } |
3096 | ||
3a6820f2 JA |
3097 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3098 | *iovec = NULL; | |
10fc72e4 | 3099 | return ret; |
3a6820f2 JA |
3100 | } |
3101 | ||
4d954c25 JA |
3102 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3103 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3104 | if (!ret) |
3105 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3106 | *iovec = NULL; |
3107 | return ret; | |
3108 | } | |
3109 | ||
89cd35c5 CH |
3110 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3111 | req->ctx->compat); | |
2b188cc1 JA |
3112 | } |
3113 | ||
0fef9483 JA |
3114 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3115 | { | |
5b09e37e | 3116 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3117 | } |
3118 | ||
31b51510 | 3119 | /* |
32960613 JA |
3120 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3121 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3122 | */ |
4017eb91 | 3123 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3124 | { |
4017eb91 JA |
3125 | struct kiocb *kiocb = &req->rw.kiocb; |
3126 | struct file *file = req->file; | |
32960613 JA |
3127 | ssize_t ret = 0; |
3128 | ||
3129 | /* | |
3130 | * Don't support polled IO through this interface, and we can't | |
3131 | * support non-blocking either. For the latter, this just causes | |
3132 | * the kiocb to be handled from an async context. | |
3133 | */ | |
3134 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3135 | return -EOPNOTSUPP; | |
3136 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3137 | return -EAGAIN; | |
3138 | ||
3139 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3140 | struct iovec iovec; |
32960613 JA |
3141 | ssize_t nr; |
3142 | ||
311ae9e1 PB |
3143 | if (!iov_iter_is_bvec(iter)) { |
3144 | iovec = iov_iter_iovec(iter); | |
3145 | } else { | |
4017eb91 JA |
3146 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3147 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3148 | } |
3149 | ||
32960613 JA |
3150 | if (rw == READ) { |
3151 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3152 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3153 | } else { |
3154 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3155 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3156 | } |
3157 | ||
3158 | if (nr < 0) { | |
3159 | if (!ret) | |
3160 | ret = nr; | |
3161 | break; | |
3162 | } | |
3163 | ret += nr; | |
3164 | if (nr != iovec.iov_len) | |
3165 | break; | |
4017eb91 JA |
3166 | req->rw.len -= nr; |
3167 | req->rw.addr += nr; | |
32960613 JA |
3168 | iov_iter_advance(iter, nr); |
3169 | } | |
3170 | ||
3171 | return ret; | |
3172 | } | |
3173 | ||
ff6165b2 JA |
3174 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3175 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3176 | { |
e8c2bc1f | 3177 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3178 | |
ff6165b2 | 3179 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3180 | rw->free_iovec = iovec; |
227c0c96 | 3181 | rw->bytes_done = 0; |
ff6165b2 | 3182 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3183 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3184 | return; |
b64e3444 | 3185 | if (!iovec) { |
ff6165b2 JA |
3186 | unsigned iov_off = 0; |
3187 | ||
3188 | rw->iter.iov = rw->fast_iov; | |
3189 | if (iter->iov != fast_iov) { | |
3190 | iov_off = iter->iov - fast_iov; | |
3191 | rw->iter.iov += iov_off; | |
3192 | } | |
3193 | if (rw->fast_iov != fast_iov) | |
3194 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3195 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3196 | } else { |
3197 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3198 | } |
3199 | } | |
3200 | ||
6cb78689 | 3201 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3202 | { |
e8c2bc1f JA |
3203 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3204 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3205 | return req->async_data == NULL; | |
3d9932a8 XW |
3206 | } |
3207 | ||
ff6165b2 JA |
3208 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3209 | const struct iovec *fast_iov, | |
227c0c96 | 3210 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3211 | { |
26f0505a | 3212 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3213 | return 0; |
e8c2bc1f | 3214 | if (!req->async_data) { |
6cb78689 | 3215 | if (io_alloc_async_data(req)) { |
6bf985dc | 3216 | kfree(iovec); |
5d204bcf | 3217 | return -ENOMEM; |
6bf985dc | 3218 | } |
b7bb4f7d | 3219 | |
ff6165b2 | 3220 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3221 | } |
b7bb4f7d | 3222 | return 0; |
f67676d1 JA |
3223 | } |
3224 | ||
73debe68 | 3225 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3226 | { |
e8c2bc1f | 3227 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3228 | struct iovec *iov = iorw->fast_iov; |
847595de | 3229 | int ret; |
c3e330a4 | 3230 | |
2846c481 | 3231 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3232 | if (unlikely(ret < 0)) |
3233 | return ret; | |
3234 | ||
ab0b196c PB |
3235 | iorw->bytes_done = 0; |
3236 | iorw->free_iovec = iov; | |
3237 | if (iov) | |
3238 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3239 | return 0; |
3240 | } | |
3241 | ||
73debe68 | 3242 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3243 | { |
3529d8c2 JA |
3244 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3245 | return -EBADF; | |
93642ef8 | 3246 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3247 | } |
3248 | ||
c1dd91d1 JA |
3249 | /* |
3250 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3251 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3252 | * This gets called when the page is unlocked, and we generally expect that to | |
3253 | * happen when the page IO is completed and the page is now uptodate. This will | |
3254 | * queue a task_work based retry of the operation, attempting to copy the data | |
3255 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3256 | * do a thread based blocking retry of the operation. That's the unexpected | |
3257 | * slow path. | |
3258 | */ | |
bcf5a063 JA |
3259 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3260 | int sync, void *arg) | |
3261 | { | |
3262 | struct wait_page_queue *wpq; | |
3263 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3264 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3265 | |
3266 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3267 | ||
cdc8fcb4 LT |
3268 | if (!wake_page_match(wpq, key)) |
3269 | return 0; | |
3270 | ||
c8d317aa | 3271 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3272 | list_del_init(&wait->entry); |
921b9054 | 3273 | io_req_task_queue(req); |
bcf5a063 JA |
3274 | return 1; |
3275 | } | |
3276 | ||
c1dd91d1 JA |
3277 | /* |
3278 | * This controls whether a given IO request should be armed for async page | |
3279 | * based retry. If we return false here, the request is handed to the async | |
3280 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3281 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3282 | * will either succeed because the page is now uptodate and unlocked, or it | |
3283 | * will register a callback when the page is unlocked at IO completion. Through | |
3284 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3285 | * That retry will attempt the buffered read again. The retry will generally | |
3286 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3287 | * async worker threads for a blocking retry. | |
3288 | */ | |
227c0c96 | 3289 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3290 | { |
e8c2bc1f JA |
3291 | struct io_async_rw *rw = req->async_data; |
3292 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3293 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3294 | |
bcf5a063 JA |
3295 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3296 | if (req->flags & REQ_F_NOWAIT) | |
3297 | return false; | |
f67676d1 | 3298 | |
227c0c96 | 3299 | /* Only for buffered IO */ |
3b2a4439 | 3300 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3301 | return false; |
3b2a4439 | 3302 | |
bcf5a063 JA |
3303 | /* |
3304 | * just use poll if we can, and don't attempt if the fs doesn't | |
3305 | * support callback based unlocks | |
3306 | */ | |
3307 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3308 | return false; | |
f67676d1 | 3309 | |
3b2a4439 JA |
3310 | wait->wait.func = io_async_buf_func; |
3311 | wait->wait.private = req; | |
3312 | wait->wait.flags = 0; | |
3313 | INIT_LIST_HEAD(&wait->wait.entry); | |
3314 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3315 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3316 | kiocb->ki_waitq = wait; |
3b2a4439 | 3317 | return true; |
bcf5a063 JA |
3318 | } |
3319 | ||
aeab9506 | 3320 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3321 | { |
3322 | if (req->file->f_op->read_iter) | |
3323 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3324 | else if (req->file->f_op->read) |
4017eb91 | 3325 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3326 | else |
3327 | return -EINVAL; | |
f67676d1 JA |
3328 | } |
3329 | ||
889fca73 | 3330 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3331 | { |
3332 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3333 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3334 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3335 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3336 | ssize_t io_size, ret, ret2; |
45d189c6 | 3337 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3338 | |
2846c481 | 3339 | if (rw) { |
e8c2bc1f | 3340 | iter = &rw->iter; |
2846c481 PB |
3341 | iovec = NULL; |
3342 | } else { | |
3343 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3344 | if (ret < 0) | |
3345 | return ret; | |
3346 | } | |
632546c4 | 3347 | io_size = iov_iter_count(iter); |
fa15bafb | 3348 | req->result = io_size; |
2b188cc1 | 3349 | |
fd6c2e4c JA |
3350 | /* Ensure we clear previously set non-block flag */ |
3351 | if (!force_nonblock) | |
29de5f6a | 3352 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3353 | else |
3354 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3355 | ||
24c74678 | 3356 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3357 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3358 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3359 | return ret ?: -EAGAIN; |
6713e7a6 | 3360 | } |
9e645e11 | 3361 | |
632546c4 | 3362 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3363 | if (unlikely(ret)) { |
3364 | kfree(iovec); | |
3365 | return ret; | |
3366 | } | |
2b188cc1 | 3367 | |
227c0c96 | 3368 | ret = io_iter_do_read(req, iter); |
32960613 | 3369 | |
230d50d4 | 3370 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3371 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3372 | /* IOPOLL retry should happen for io-wq threads */ |
3373 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3374 | goto done; |
75c668cd PB |
3375 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3376 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3377 | goto done; |
84216315 | 3378 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3379 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3380 | ret = 0; |
230d50d4 JA |
3381 | } else if (ret == -EIOCBQUEUED) { |
3382 | goto out_free; | |
7335e3bf | 3383 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3384 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3385 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3386 | goto done; |
227c0c96 JA |
3387 | } |
3388 | ||
227c0c96 | 3389 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3390 | if (ret2) |
3391 | return ret2; | |
3392 | ||
fe1cdd55 | 3393 | iovec = NULL; |
e8c2bc1f | 3394 | rw = req->async_data; |
227c0c96 | 3395 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3396 | iter = &rw->iter; |
227c0c96 | 3397 | |
b23df91b PB |
3398 | do { |
3399 | io_size -= ret; | |
3400 | rw->bytes_done += ret; | |
3401 | /* if we can retry, do so with the callbacks armed */ | |
3402 | if (!io_rw_should_retry(req)) { | |
3403 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3404 | return -EAGAIN; | |
3405 | } | |
3406 | ||
3407 | /* | |
3408 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3409 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3410 | * desired page gets unlocked. We can also get a partial read | |
3411 | * here, and if we do, then just retry at the new offset. | |
3412 | */ | |
3413 | ret = io_iter_do_read(req, iter); | |
3414 | if (ret == -EIOCBQUEUED) | |
3415 | return 0; | |
227c0c96 | 3416 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3417 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3418 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3419 | done: |
889fca73 | 3420 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3421 | out_free: |
3422 | /* it's faster to check here then delegate to kfree */ | |
3423 | if (iovec) | |
3424 | kfree(iovec); | |
5ea5dd45 | 3425 | return 0; |
2b188cc1 JA |
3426 | } |
3427 | ||
73debe68 | 3428 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3429 | { |
3529d8c2 JA |
3430 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3431 | return -EBADF; | |
93642ef8 | 3432 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3433 | } |
3434 | ||
889fca73 | 3435 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3436 | { |
3437 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3438 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3439 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3440 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3441 | ssize_t ret, ret2, io_size; |
45d189c6 | 3442 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3443 | |
2846c481 | 3444 | if (rw) { |
e8c2bc1f | 3445 | iter = &rw->iter; |
2846c481 PB |
3446 | iovec = NULL; |
3447 | } else { | |
3448 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3449 | if (ret < 0) | |
3450 | return ret; | |
3451 | } | |
632546c4 | 3452 | io_size = iov_iter_count(iter); |
fa15bafb | 3453 | req->result = io_size; |
2b188cc1 | 3454 | |
fd6c2e4c JA |
3455 | /* Ensure we clear previously set non-block flag */ |
3456 | if (!force_nonblock) | |
a88fc400 PB |
3457 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3458 | else | |
3459 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3460 | |
24c74678 | 3461 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3462 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3463 | goto copy_iov; |
31b51510 | 3464 | |
10d59345 JA |
3465 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3466 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3467 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3468 | goto copy_iov; |
31b51510 | 3469 | |
632546c4 | 3470 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3471 | if (unlikely(ret)) |
3472 | goto out_free; | |
4ed734b0 | 3473 | |
fa15bafb PB |
3474 | /* |
3475 | * Open-code file_start_write here to grab freeze protection, | |
3476 | * which will be released by another thread in | |
3477 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3478 | * released so that it doesn't complain about the held lock when | |
3479 | * we return to userspace. | |
3480 | */ | |
3481 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3482 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3483 | __sb_writers_release(file_inode(req->file)->i_sb, |
3484 | SB_FREEZE_WRITE); | |
3485 | } | |
3486 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3487 | |
fa15bafb | 3488 | if (req->file->f_op->write_iter) |
ff6165b2 | 3489 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3490 | else if (req->file->f_op->write) |
4017eb91 | 3491 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3492 | else |
3493 | ret2 = -EINVAL; | |
4ed734b0 | 3494 | |
6ad7f233 PB |
3495 | if (req->flags & REQ_F_REISSUE) { |
3496 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3497 | ret2 = -EAGAIN; |
6ad7f233 | 3498 | } |
230d50d4 | 3499 | |
fa15bafb PB |
3500 | /* |
3501 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3502 | * retry them without IOCB_NOWAIT. | |
3503 | */ | |
3504 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3505 | ret2 = -EAGAIN; | |
75c668cd PB |
3506 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3507 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3508 | goto done; |
fa15bafb | 3509 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3510 | /* IOPOLL retry should happen for io-wq threads */ |
3511 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3512 | goto copy_iov; | |
355afaeb | 3513 | done: |
889fca73 | 3514 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3515 | } else { |
f67676d1 | 3516 | copy_iov: |
84216315 | 3517 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3518 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3519 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3520 | return ret ?: -EAGAIN; |
2b188cc1 | 3521 | } |
31b51510 | 3522 | out_free: |
f261c168 | 3523 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3524 | if (iovec) |
6f2cc166 | 3525 | kfree(iovec); |
2b188cc1 JA |
3526 | return ret; |
3527 | } | |
3528 | ||
80a261fd JA |
3529 | static int io_renameat_prep(struct io_kiocb *req, |
3530 | const struct io_uring_sqe *sqe) | |
3531 | { | |
3532 | struct io_rename *ren = &req->rename; | |
3533 | const char __user *oldf, *newf; | |
3534 | ||
ed7eb259 JA |
3535 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3536 | return -EINVAL; | |
26578cda | 3537 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3538 | return -EINVAL; |
80a261fd JA |
3539 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3540 | return -EBADF; | |
3541 | ||
3542 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3543 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3544 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3545 | ren->new_dfd = READ_ONCE(sqe->len); | |
3546 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3547 | ||
3548 | ren->oldpath = getname(oldf); | |
3549 | if (IS_ERR(ren->oldpath)) | |
3550 | return PTR_ERR(ren->oldpath); | |
3551 | ||
3552 | ren->newpath = getname(newf); | |
3553 | if (IS_ERR(ren->newpath)) { | |
3554 | putname(ren->oldpath); | |
3555 | return PTR_ERR(ren->newpath); | |
3556 | } | |
3557 | ||
3558 | req->flags |= REQ_F_NEED_CLEANUP; | |
3559 | return 0; | |
3560 | } | |
3561 | ||
45d189c6 | 3562 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3563 | { |
3564 | struct io_rename *ren = &req->rename; | |
3565 | int ret; | |
3566 | ||
45d189c6 | 3567 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3568 | return -EAGAIN; |
3569 | ||
3570 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3571 | ren->newpath, ren->flags); | |
3572 | ||
3573 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3574 | if (ret < 0) | |
93d2bcd2 | 3575 | req_set_fail(req); |
80a261fd JA |
3576 | io_req_complete(req, ret); |
3577 | return 0; | |
3578 | } | |
3579 | ||
14a1143b JA |
3580 | static int io_unlinkat_prep(struct io_kiocb *req, |
3581 | const struct io_uring_sqe *sqe) | |
3582 | { | |
3583 | struct io_unlink *un = &req->unlink; | |
3584 | const char __user *fname; | |
3585 | ||
22634bc5 JA |
3586 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3587 | return -EINVAL; | |
26578cda PB |
3588 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3589 | sqe->splice_fd_in) | |
22634bc5 | 3590 | return -EINVAL; |
14a1143b JA |
3591 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3592 | return -EBADF; | |
3593 | ||
3594 | un->dfd = READ_ONCE(sqe->fd); | |
3595 | ||
3596 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3597 | if (un->flags & ~AT_REMOVEDIR) | |
3598 | return -EINVAL; | |
3599 | ||
3600 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3601 | un->filename = getname(fname); | |
3602 | if (IS_ERR(un->filename)) | |
3603 | return PTR_ERR(un->filename); | |
3604 | ||
3605 | req->flags |= REQ_F_NEED_CLEANUP; | |
3606 | return 0; | |
3607 | } | |
3608 | ||
45d189c6 | 3609 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3610 | { |
3611 | struct io_unlink *un = &req->unlink; | |
3612 | int ret; | |
3613 | ||
45d189c6 | 3614 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3615 | return -EAGAIN; |
3616 | ||
3617 | if (un->flags & AT_REMOVEDIR) | |
3618 | ret = do_rmdir(un->dfd, un->filename); | |
3619 | else | |
3620 | ret = do_unlinkat(un->dfd, un->filename); | |
3621 | ||
3622 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3623 | if (ret < 0) | |
93d2bcd2 | 3624 | req_set_fail(req); |
14a1143b JA |
3625 | io_req_complete(req, ret); |
3626 | return 0; | |
3627 | } | |
3628 | ||
36f4fa68 JA |
3629 | static int io_shutdown_prep(struct io_kiocb *req, |
3630 | const struct io_uring_sqe *sqe) | |
3631 | { | |
3632 | #if defined(CONFIG_NET) | |
3633 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3634 | return -EINVAL; | |
26578cda PB |
3635 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3636 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3637 | return -EINVAL; |
3638 | ||
3639 | req->shutdown.how = READ_ONCE(sqe->len); | |
3640 | return 0; | |
3641 | #else | |
3642 | return -EOPNOTSUPP; | |
3643 | #endif | |
3644 | } | |
3645 | ||
45d189c6 | 3646 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3647 | { |
3648 | #if defined(CONFIG_NET) | |
3649 | struct socket *sock; | |
3650 | int ret; | |
3651 | ||
45d189c6 | 3652 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3653 | return -EAGAIN; |
3654 | ||
48aba79b | 3655 | sock = sock_from_file(req->file); |
36f4fa68 | 3656 | if (unlikely(!sock)) |
48aba79b | 3657 | return -ENOTSOCK; |
36f4fa68 JA |
3658 | |
3659 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3660 | if (ret < 0) |
93d2bcd2 | 3661 | req_set_fail(req); |
36f4fa68 JA |
3662 | io_req_complete(req, ret); |
3663 | return 0; | |
3664 | #else | |
3665 | return -EOPNOTSUPP; | |
3666 | #endif | |
3667 | } | |
3668 | ||
f2a8d5c7 PB |
3669 | static int __io_splice_prep(struct io_kiocb *req, |
3670 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3671 | { |
fe7e3257 | 3672 | struct io_splice *sp = &req->splice; |
7d67af2c | 3673 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3674 | |
3232dd02 PB |
3675 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3676 | return -EINVAL; | |
7d67af2c PB |
3677 | |
3678 | sp->file_in = NULL; | |
7d67af2c PB |
3679 | sp->len = READ_ONCE(sqe->len); |
3680 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3681 | ||
3682 | if (unlikely(sp->flags & ~valid_flags)) | |
3683 | return -EINVAL; | |
3684 | ||
62906e89 | 3685 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3686 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3687 | if (!sp->file_in) | |
3688 | return -EBADF; | |
7d67af2c | 3689 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3690 | return 0; |
3691 | } | |
3692 | ||
f2a8d5c7 PB |
3693 | static int io_tee_prep(struct io_kiocb *req, |
3694 | const struct io_uring_sqe *sqe) | |
3695 | { | |
3696 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3697 | return -EINVAL; | |
3698 | return __io_splice_prep(req, sqe); | |
3699 | } | |
3700 | ||
45d189c6 | 3701 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3702 | { |
3703 | struct io_splice *sp = &req->splice; | |
3704 | struct file *in = sp->file_in; | |
3705 | struct file *out = sp->file_out; | |
3706 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3707 | long ret = 0; | |
3708 | ||
45d189c6 | 3709 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3710 | return -EAGAIN; |
3711 | if (sp->len) | |
3712 | ret = do_tee(in, out, sp->len, flags); | |
3713 | ||
e1d767f0 PB |
3714 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3715 | io_put_file(in); | |
f2a8d5c7 PB |
3716 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3717 | ||
f2a8d5c7 | 3718 | if (ret != sp->len) |
93d2bcd2 | 3719 | req_set_fail(req); |
e1e16097 | 3720 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3721 | return 0; |
3722 | } | |
3723 | ||
3724 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3725 | { | |
fe7e3257 | 3726 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3727 | |
3728 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3729 | sp->off_out = READ_ONCE(sqe->off); | |
3730 | return __io_splice_prep(req, sqe); | |
3731 | } | |
3732 | ||
45d189c6 | 3733 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3734 | { |
3735 | struct io_splice *sp = &req->splice; | |
3736 | struct file *in = sp->file_in; | |
3737 | struct file *out = sp->file_out; | |
3738 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3739 | loff_t *poff_in, *poff_out; | |
c9687426 | 3740 | long ret = 0; |
7d67af2c | 3741 | |
45d189c6 | 3742 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3743 | return -EAGAIN; |
7d67af2c PB |
3744 | |
3745 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3746 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3747 | |
948a7749 | 3748 | if (sp->len) |
c9687426 | 3749 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3750 | |
e1d767f0 PB |
3751 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3752 | io_put_file(in); | |
7d67af2c PB |
3753 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3754 | ||
7d67af2c | 3755 | if (ret != sp->len) |
93d2bcd2 | 3756 | req_set_fail(req); |
e1e16097 | 3757 | io_req_complete(req, ret); |
7d67af2c PB |
3758 | return 0; |
3759 | } | |
3760 | ||
2b188cc1 JA |
3761 | /* |
3762 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3763 | */ | |
889fca73 | 3764 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3765 | { |
3766 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3767 | |
def596e9 JA |
3768 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3769 | return -EINVAL; | |
3770 | ||
889fca73 | 3771 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3772 | return 0; |
3773 | } | |
3774 | ||
1155c76a | 3775 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3776 | { |
6b06314c | 3777 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3778 | |
09bb8394 JA |
3779 | if (!req->file) |
3780 | return -EBADF; | |
c992fe29 | 3781 | |
6b06314c | 3782 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3783 | return -EINVAL; |
26578cda PB |
3784 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
3785 | sqe->splice_fd_in)) | |
c992fe29 CH |
3786 | return -EINVAL; |
3787 | ||
8ed8d3c3 JA |
3788 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3789 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3790 | return -EINVAL; | |
3791 | ||
3792 | req->sync.off = READ_ONCE(sqe->off); | |
3793 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3794 | return 0; |
3795 | } | |
3796 | ||
45d189c6 | 3797 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3798 | { |
8ed8d3c3 | 3799 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3800 | int ret; |
3801 | ||
ac45abc0 | 3802 | /* fsync always requires a blocking context */ |
45d189c6 | 3803 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3804 | return -EAGAIN; |
3805 | ||
9adbd45d | 3806 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3807 | end > 0 ? end : LLONG_MAX, |
3808 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3809 | if (ret < 0) | |
93d2bcd2 | 3810 | req_set_fail(req); |
e1e16097 | 3811 | io_req_complete(req, ret); |
c992fe29 CH |
3812 | return 0; |
3813 | } | |
3814 | ||
d63d1b5e JA |
3815 | static int io_fallocate_prep(struct io_kiocb *req, |
3816 | const struct io_uring_sqe *sqe) | |
3817 | { | |
26578cda PB |
3818 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
3819 | sqe->splice_fd_in) | |
d63d1b5e | 3820 | return -EINVAL; |
3232dd02 PB |
3821 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3822 | return -EINVAL; | |
d63d1b5e JA |
3823 | |
3824 | req->sync.off = READ_ONCE(sqe->off); | |
3825 | req->sync.len = READ_ONCE(sqe->addr); | |
3826 | req->sync.mode = READ_ONCE(sqe->len); | |
3827 | return 0; | |
3828 | } | |
3829 | ||
45d189c6 | 3830 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3831 | { |
ac45abc0 PB |
3832 | int ret; |
3833 | ||
d63d1b5e | 3834 | /* fallocate always requiring blocking context */ |
45d189c6 | 3835 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3836 | return -EAGAIN; |
ac45abc0 PB |
3837 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3838 | req->sync.len); | |
ac45abc0 | 3839 | if (ret < 0) |
93d2bcd2 | 3840 | req_set_fail(req); |
e1e16097 | 3841 | io_req_complete(req, ret); |
5d17b4a4 JA |
3842 | return 0; |
3843 | } | |
3844 | ||
ec65fea5 | 3845 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3846 | { |
f8748881 | 3847 | const char __user *fname; |
15b71abe | 3848 | int ret; |
b7bb4f7d | 3849 | |
d3fddf6d PB |
3850 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3851 | return -EINVAL; | |
26578cda | 3852 | if (unlikely(sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)) |
15b71abe | 3853 | return -EINVAL; |
ec65fea5 | 3854 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3855 | return -EBADF; |
03b1230c | 3856 | |
ec65fea5 PB |
3857 | /* open.how should be already initialised */ |
3858 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3859 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3860 | |
25e72d10 PB |
3861 | req->open.dfd = READ_ONCE(sqe->fd); |
3862 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3863 | req->open.filename = getname(fname); |
15b71abe JA |
3864 | if (IS_ERR(req->open.filename)) { |
3865 | ret = PTR_ERR(req->open.filename); | |
3866 | req->open.filename = NULL; | |
3867 | return ret; | |
3868 | } | |
4022e7af | 3869 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3870 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3871 | return 0; |
03b1230c JA |
3872 | } |
3873 | ||
ec65fea5 PB |
3874 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3875 | { | |
d3fddf6d PB |
3876 | u64 mode = READ_ONCE(sqe->len); |
3877 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 3878 | |
ec65fea5 PB |
3879 | req->open.how = build_open_how(flags, mode); |
3880 | return __io_openat_prep(req, sqe); | |
3881 | } | |
3882 | ||
cebdb986 | 3883 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3884 | { |
cebdb986 | 3885 | struct open_how __user *how; |
cebdb986 | 3886 | size_t len; |
0fa03c62 JA |
3887 | int ret; |
3888 | ||
cebdb986 JA |
3889 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3890 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3891 | if (len < OPEN_HOW_SIZE_VER0) |
3892 | return -EINVAL; | |
3529d8c2 | 3893 | |
cebdb986 JA |
3894 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3895 | len); | |
3896 | if (ret) | |
3897 | return ret; | |
3529d8c2 | 3898 | |
ec65fea5 | 3899 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3900 | } |
3901 | ||
45d189c6 | 3902 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3903 | { |
3904 | struct open_flags op; | |
15b71abe | 3905 | struct file *file; |
3a81fd02 JA |
3906 | bool nonblock_set; |
3907 | bool resolve_nonblock; | |
15b71abe JA |
3908 | int ret; |
3909 | ||
cebdb986 | 3910 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3911 | if (ret) |
3912 | goto err; | |
3a81fd02 JA |
3913 | nonblock_set = op.open_flag & O_NONBLOCK; |
3914 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3915 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3916 | /* |
3917 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3918 | * it'll always -EAGAIN | |
3919 | */ | |
3920 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3921 | return -EAGAIN; | |
3922 | op.lookup_flags |= LOOKUP_CACHED; | |
3923 | op.open_flag |= O_NONBLOCK; | |
3924 | } | |
15b71abe | 3925 | |
4022e7af | 3926 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
15b71abe JA |
3927 | if (ret < 0) |
3928 | goto err; | |
3929 | ||
3930 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 3931 | if (IS_ERR(file)) { |
944d1444 | 3932 | /* |
12dcb58a PB |
3933 | * We could hang on to this 'fd' on retrying, but seems like |
3934 | * marginal gain for something that is now known to be a slower | |
3935 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 3936 | */ |
3a81fd02 | 3937 | put_unused_fd(ret); |
3a81fd02 | 3938 | |
15b71abe | 3939 | ret = PTR_ERR(file); |
12dcb58a PB |
3940 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
3941 | if (ret == -EAGAIN && | |
3942 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
3943 | return -EAGAIN; | |
3944 | goto err; | |
15b71abe | 3945 | } |
12dcb58a PB |
3946 | |
3947 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
3948 | file->f_flags &= ~O_NONBLOCK; | |
3949 | fsnotify_open(file); | |
3950 | fd_install(ret, file); | |
15b71abe JA |
3951 | err: |
3952 | putname(req->open.filename); | |
8fef80bf | 3953 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 3954 | if (ret < 0) |
93d2bcd2 | 3955 | req_set_fail(req); |
0bdf3398 | 3956 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
3957 | return 0; |
3958 | } | |
3959 | ||
45d189c6 | 3960 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 3961 | { |
e45cff58 | 3962 | return io_openat2(req, issue_flags); |
cebdb986 JA |
3963 | } |
3964 | ||
067524e9 JA |
3965 | static int io_remove_buffers_prep(struct io_kiocb *req, |
3966 | const struct io_uring_sqe *sqe) | |
3967 | { | |
3968 | struct io_provide_buf *p = &req->pbuf; | |
3969 | u64 tmp; | |
3970 | ||
26578cda PB |
3971 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
3972 | sqe->splice_fd_in) | |
067524e9 JA |
3973 | return -EINVAL; |
3974 | ||
3975 | tmp = READ_ONCE(sqe->fd); | |
3976 | if (!tmp || tmp > USHRT_MAX) | |
3977 | return -EINVAL; | |
3978 | ||
3979 | memset(p, 0, sizeof(*p)); | |
3980 | p->nbufs = tmp; | |
3981 | p->bgid = READ_ONCE(sqe->buf_group); | |
3982 | return 0; | |
3983 | } | |
3984 | ||
3985 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
3986 | int bgid, unsigned nbufs) | |
3987 | { | |
3988 | unsigned i = 0; | |
3989 | ||
3990 | /* shouldn't happen */ | |
3991 | if (!nbufs) | |
3992 | return 0; | |
3993 | ||
3994 | /* the head kbuf is the list itself */ | |
3995 | while (!list_empty(&buf->list)) { | |
3996 | struct io_buffer *nxt; | |
3997 | ||
3998 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
3999 | list_del(&nxt->list); | |
4000 | kfree(nxt); | |
4001 | if (++i == nbufs) | |
4002 | return i; | |
4003 | } | |
4004 | i++; | |
4005 | kfree(buf); | |
9e15c3a0 | 4006 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4007 | |
4008 | return i; | |
4009 | } | |
4010 | ||
889fca73 | 4011 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4012 | { |
4013 | struct io_provide_buf *p = &req->pbuf; | |
4014 | struct io_ring_ctx *ctx = req->ctx; | |
4015 | struct io_buffer *head; | |
4016 | int ret = 0; | |
45d189c6 | 4017 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4018 | |
4019 | io_ring_submit_lock(ctx, !force_nonblock); | |
4020 | ||
4021 | lockdep_assert_held(&ctx->uring_lock); | |
4022 | ||
4023 | ret = -ENOENT; | |
9e15c3a0 | 4024 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4025 | if (head) |
4026 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4027 | if (ret < 0) |
93d2bcd2 | 4028 | req_set_fail(req); |
067524e9 | 4029 | |
9fb8cb49 PB |
4030 | /* complete before unlock, IOPOLL may need the lock */ |
4031 | __io_req_complete(req, issue_flags, ret, 0); | |
4032 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4033 | return 0; |
4034 | } | |
4035 | ||
ddf0322d JA |
4036 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4037 | const struct io_uring_sqe *sqe) | |
4038 | { | |
38134ada | 4039 | unsigned long size, tmp_check; |
ddf0322d JA |
4040 | struct io_provide_buf *p = &req->pbuf; |
4041 | u64 tmp; | |
4042 | ||
26578cda | 4043 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4044 | return -EINVAL; |
4045 | ||
4046 | tmp = READ_ONCE(sqe->fd); | |
4047 | if (!tmp || tmp > USHRT_MAX) | |
4048 | return -E2BIG; | |
4049 | p->nbufs = tmp; | |
4050 | p->addr = READ_ONCE(sqe->addr); | |
4051 | p->len = READ_ONCE(sqe->len); | |
4052 | ||
38134ada PB |
4053 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4054 | &size)) | |
4055 | return -EOVERFLOW; | |
4056 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4057 | return -EOVERFLOW; | |
4058 | ||
d81269fe PB |
4059 | size = (unsigned long)p->len * p->nbufs; |
4060 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4061 | return -EFAULT; |
4062 | ||
4063 | p->bgid = READ_ONCE(sqe->buf_group); | |
4064 | tmp = READ_ONCE(sqe->off); | |
4065 | if (tmp > USHRT_MAX) | |
4066 | return -E2BIG; | |
4067 | p->bid = tmp; | |
4068 | return 0; | |
4069 | } | |
4070 | ||
4071 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4072 | { | |
4073 | struct io_buffer *buf; | |
4074 | u64 addr = pbuf->addr; | |
4075 | int i, bid = pbuf->bid; | |
4076 | ||
4077 | for (i = 0; i < pbuf->nbufs; i++) { | |
4078 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4079 | if (!buf) | |
4080 | break; | |
4081 | ||
4082 | buf->addr = addr; | |
d1f82808 | 4083 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4084 | buf->bid = bid; |
4085 | addr += pbuf->len; | |
4086 | bid++; | |
4087 | if (!*head) { | |
4088 | INIT_LIST_HEAD(&buf->list); | |
4089 | *head = buf; | |
4090 | } else { | |
4091 | list_add_tail(&buf->list, &(*head)->list); | |
4092 | } | |
4093 | } | |
4094 | ||
4095 | return i ? i : -ENOMEM; | |
4096 | } | |
4097 | ||
889fca73 | 4098 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4099 | { |
4100 | struct io_provide_buf *p = &req->pbuf; | |
4101 | struct io_ring_ctx *ctx = req->ctx; | |
4102 | struct io_buffer *head, *list; | |
4103 | int ret = 0; | |
45d189c6 | 4104 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4105 | |
4106 | io_ring_submit_lock(ctx, !force_nonblock); | |
4107 | ||
4108 | lockdep_assert_held(&ctx->uring_lock); | |
4109 | ||
9e15c3a0 | 4110 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4111 | |
4112 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4113 | if (ret >= 0 && !list) { |
4114 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4115 | if (ret < 0) | |
067524e9 | 4116 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4117 | } |
ddf0322d | 4118 | if (ret < 0) |
93d2bcd2 | 4119 | req_set_fail(req); |
9fb8cb49 PB |
4120 | /* complete before unlock, IOPOLL may need the lock */ |
4121 | __io_req_complete(req, issue_flags, ret, 0); | |
4122 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4123 | return 0; |
cebdb986 JA |
4124 | } |
4125 | ||
3e4827b0 JA |
4126 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4127 | const struct io_uring_sqe *sqe) | |
4128 | { | |
4129 | #if defined(CONFIG_EPOLL) | |
26578cda | 4130 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4131 | return -EINVAL; |
2d74d042 | 4132 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4133 | return -EINVAL; |
3e4827b0 JA |
4134 | |
4135 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4136 | req->epoll.op = READ_ONCE(sqe->len); | |
4137 | req->epoll.fd = READ_ONCE(sqe->off); | |
4138 | ||
4139 | if (ep_op_has_event(req->epoll.op)) { | |
4140 | struct epoll_event __user *ev; | |
4141 | ||
4142 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4143 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4144 | return -EFAULT; | |
4145 | } | |
4146 | ||
4147 | return 0; | |
4148 | #else | |
4149 | return -EOPNOTSUPP; | |
4150 | #endif | |
4151 | } | |
4152 | ||
889fca73 | 4153 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4154 | { |
4155 | #if defined(CONFIG_EPOLL) | |
4156 | struct io_epoll *ie = &req->epoll; | |
4157 | int ret; | |
45d189c6 | 4158 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4159 | |
4160 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4161 | if (force_nonblock && ret == -EAGAIN) | |
4162 | return -EAGAIN; | |
4163 | ||
4164 | if (ret < 0) | |
93d2bcd2 | 4165 | req_set_fail(req); |
889fca73 | 4166 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4167 | return 0; |
4168 | #else | |
4169 | return -EOPNOTSUPP; | |
4170 | #endif | |
4171 | } | |
4172 | ||
c1ca757b JA |
4173 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4174 | { | |
4175 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4176 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4177 | return -EINVAL; |
3232dd02 PB |
4178 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4179 | return -EINVAL; | |
c1ca757b JA |
4180 | |
4181 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4182 | req->madvise.len = READ_ONCE(sqe->len); | |
4183 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4184 | return 0; | |
4185 | #else | |
4186 | return -EOPNOTSUPP; | |
4187 | #endif | |
4188 | } | |
4189 | ||
45d189c6 | 4190 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4191 | { |
4192 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4193 | struct io_madvise *ma = &req->madvise; | |
4194 | int ret; | |
4195 | ||
45d189c6 | 4196 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4197 | return -EAGAIN; |
4198 | ||
0726b01e | 4199 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4200 | if (ret < 0) |
93d2bcd2 | 4201 | req_set_fail(req); |
e1e16097 | 4202 | io_req_complete(req, ret); |
c1ca757b JA |
4203 | return 0; |
4204 | #else | |
4205 | return -EOPNOTSUPP; | |
4206 | #endif | |
4207 | } | |
4208 | ||
4840e418 JA |
4209 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4210 | { | |
26578cda | 4211 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4212 | return -EINVAL; |
3232dd02 PB |
4213 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4214 | return -EINVAL; | |
4840e418 JA |
4215 | |
4216 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4217 | req->fadvise.len = READ_ONCE(sqe->len); | |
4218 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4219 | return 0; | |
4220 | } | |
4221 | ||
45d189c6 | 4222 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4223 | { |
4224 | struct io_fadvise *fa = &req->fadvise; | |
4225 | int ret; | |
4226 | ||
45d189c6 | 4227 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4228 | switch (fa->advice) { |
4229 | case POSIX_FADV_NORMAL: | |
4230 | case POSIX_FADV_RANDOM: | |
4231 | case POSIX_FADV_SEQUENTIAL: | |
4232 | break; | |
4233 | default: | |
4234 | return -EAGAIN; | |
4235 | } | |
4236 | } | |
4840e418 JA |
4237 | |
4238 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4239 | if (ret < 0) | |
93d2bcd2 | 4240 | req_set_fail(req); |
0bdf3398 | 4241 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4242 | return 0; |
4243 | } | |
4244 | ||
eddc7ef5 JA |
4245 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4246 | { | |
2d74d042 | 4247 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4248 | return -EINVAL; |
26578cda | 4249 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4250 | return -EINVAL; |
9c280f90 | 4251 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4252 | return -EBADF; |
eddc7ef5 | 4253 | |
1d9e1288 BM |
4254 | req->statx.dfd = READ_ONCE(sqe->fd); |
4255 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4256 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4257 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4258 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4259 | |
4260 | return 0; | |
4261 | } | |
4262 | ||
45d189c6 | 4263 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4264 | { |
1d9e1288 | 4265 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4266 | int ret; |
4267 | ||
59d70013 | 4268 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4269 | return -EAGAIN; |
4270 | ||
e62753e4 BM |
4271 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4272 | ctx->buffer); | |
eddc7ef5 | 4273 | |
eddc7ef5 | 4274 | if (ret < 0) |
93d2bcd2 | 4275 | req_set_fail(req); |
e1e16097 | 4276 | io_req_complete(req, ret); |
eddc7ef5 JA |
4277 | return 0; |
4278 | } | |
4279 | ||
b5dba59e JA |
4280 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4281 | { | |
14587a46 | 4282 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4283 | return -EINVAL; |
b5dba59e | 4284 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
26578cda | 4285 | sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
b5dba59e | 4286 | return -EINVAL; |
9c280f90 | 4287 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4288 | return -EBADF; |
b5dba59e JA |
4289 | |
4290 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4291 | return 0; |
b5dba59e JA |
4292 | } |
4293 | ||
889fca73 | 4294 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4295 | { |
9eac1904 | 4296 | struct files_struct *files = current->files; |
3af73b28 | 4297 | struct io_close *close = &req->close; |
9eac1904 | 4298 | struct fdtable *fdt; |
a1fde923 PB |
4299 | struct file *file = NULL; |
4300 | int ret = -EBADF; | |
b5dba59e | 4301 | |
9eac1904 JA |
4302 | spin_lock(&files->file_lock); |
4303 | fdt = files_fdtable(files); | |
4304 | if (close->fd >= fdt->max_fds) { | |
4305 | spin_unlock(&files->file_lock); | |
4306 | goto err; | |
4307 | } | |
4308 | file = fdt->fd[close->fd]; | |
a1fde923 | 4309 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4310 | spin_unlock(&files->file_lock); |
4311 | file = NULL; | |
4312 | goto err; | |
3af73b28 | 4313 | } |
b5dba59e JA |
4314 | |
4315 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4316 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4317 | spin_unlock(&files->file_lock); |
0bf0eefd | 4318 | return -EAGAIN; |
a2100672 | 4319 | } |
b5dba59e | 4320 | |
9eac1904 JA |
4321 | ret = __close_fd_get_file(close->fd, &file); |
4322 | spin_unlock(&files->file_lock); | |
4323 | if (ret < 0) { | |
4324 | if (ret == -ENOENT) | |
4325 | ret = -EBADF; | |
4326 | goto err; | |
4327 | } | |
4328 | ||
3af73b28 | 4329 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4330 | ret = filp_close(file, current->files); |
4331 | err: | |
3af73b28 | 4332 | if (ret < 0) |
93d2bcd2 | 4333 | req_set_fail(req); |
9eac1904 JA |
4334 | if (file) |
4335 | fput(file); | |
889fca73 | 4336 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4337 | return 0; |
b5dba59e JA |
4338 | } |
4339 | ||
1155c76a | 4340 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4341 | { |
4342 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4343 | |
5d17b4a4 JA |
4344 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4345 | return -EINVAL; | |
26578cda PB |
4346 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4347 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4348 | return -EINVAL; |
4349 | ||
8ed8d3c3 JA |
4350 | req->sync.off = READ_ONCE(sqe->off); |
4351 | req->sync.len = READ_ONCE(sqe->len); | |
4352 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4353 | return 0; |
4354 | } | |
4355 | ||
45d189c6 | 4356 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4357 | { |
8ed8d3c3 JA |
4358 | int ret; |
4359 | ||
ac45abc0 | 4360 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4361 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4362 | return -EAGAIN; |
4363 | ||
9adbd45d | 4364 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4365 | req->sync.flags); |
4366 | if (ret < 0) | |
93d2bcd2 | 4367 | req_set_fail(req); |
e1e16097 | 4368 | io_req_complete(req, ret); |
5d17b4a4 JA |
4369 | return 0; |
4370 | } | |
4371 | ||
469956e8 | 4372 | #if defined(CONFIG_NET) |
02d27d89 PB |
4373 | static int io_setup_async_msg(struct io_kiocb *req, |
4374 | struct io_async_msghdr *kmsg) | |
4375 | { | |
e8c2bc1f JA |
4376 | struct io_async_msghdr *async_msg = req->async_data; |
4377 | ||
4378 | if (async_msg) | |
02d27d89 | 4379 | return -EAGAIN; |
e8c2bc1f | 4380 | if (io_alloc_async_data(req)) { |
257e84a5 | 4381 | kfree(kmsg->free_iov); |
02d27d89 PB |
4382 | return -ENOMEM; |
4383 | } | |
e8c2bc1f | 4384 | async_msg = req->async_data; |
02d27d89 | 4385 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4386 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4387 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4388 | /* if were using fast_iov, set it to the new one */ |
4389 | if (!async_msg->free_iov) | |
4390 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4391 | ||
02d27d89 PB |
4392 | return -EAGAIN; |
4393 | } | |
4394 | ||
2ae523ed PB |
4395 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4396 | struct io_async_msghdr *iomsg) | |
4397 | { | |
2ae523ed | 4398 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4399 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4400 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4401 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4402 | } |
4403 | ||
93642ef8 PB |
4404 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4405 | { | |
4406 | int ret; | |
4407 | ||
93642ef8 PB |
4408 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4409 | if (!ret) | |
4410 | req->flags |= REQ_F_NEED_CLEANUP; | |
4411 | return ret; | |
4412 | } | |
4413 | ||
3529d8c2 | 4414 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4415 | { |
e47293fd | 4416 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4417 | |
d2b6f48b PB |
4418 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4419 | return -EINVAL; | |
4420 | ||
270a5940 | 4421 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4422 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4423 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4424 | if (sr->msg_flags & MSG_DONTWAIT) | |
4425 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4426 | |
d8768362 JA |
4427 | #ifdef CONFIG_COMPAT |
4428 | if (req->ctx->compat) | |
4429 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4430 | #endif | |
93642ef8 | 4431 | return 0; |
03b1230c JA |
4432 | } |
4433 | ||
889fca73 | 4434 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4435 | { |
6b754c8b | 4436 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4437 | struct socket *sock; |
7a7cacba | 4438 | unsigned flags; |
0031275d | 4439 | int min_ret = 0; |
0fa03c62 JA |
4440 | int ret; |
4441 | ||
dba4a925 | 4442 | sock = sock_from_file(req->file); |
7a7cacba | 4443 | if (unlikely(!sock)) |
dba4a925 | 4444 | return -ENOTSOCK; |
3529d8c2 | 4445 | |
257e84a5 PB |
4446 | kmsg = req->async_data; |
4447 | if (!kmsg) { | |
7a7cacba PB |
4448 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4449 | if (ret) | |
4450 | return ret; | |
4451 | kmsg = &iomsg; | |
0fa03c62 | 4452 | } |
0fa03c62 | 4453 | |
04411806 PB |
4454 | flags = req->sr_msg.msg_flags; |
4455 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4456 | flags |= MSG_DONTWAIT; |
0031275d SM |
4457 | if (flags & MSG_WAITALL) |
4458 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4459 | ||
7a7cacba | 4460 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4461 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4462 | return io_setup_async_msg(req, kmsg); |
4463 | if (ret == -ERESTARTSYS) | |
4464 | ret = -EINTR; | |
0fa03c62 | 4465 | |
257e84a5 PB |
4466 | /* fast path, check for non-NULL to avoid function call */ |
4467 | if (kmsg->free_iov) | |
4468 | kfree(kmsg->free_iov); | |
99bc4c38 | 4469 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4470 | if (ret < min_ret) |
93d2bcd2 | 4471 | req_set_fail(req); |
889fca73 | 4472 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4473 | return 0; |
03b1230c | 4474 | } |
aa1fa28f | 4475 | |
889fca73 | 4476 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4477 | { |
7a7cacba PB |
4478 | struct io_sr_msg *sr = &req->sr_msg; |
4479 | struct msghdr msg; | |
4480 | struct iovec iov; | |
fddaface | 4481 | struct socket *sock; |
7a7cacba | 4482 | unsigned flags; |
0031275d | 4483 | int min_ret = 0; |
fddaface JA |
4484 | int ret; |
4485 | ||
dba4a925 | 4486 | sock = sock_from_file(req->file); |
7a7cacba | 4487 | if (unlikely(!sock)) |
dba4a925 | 4488 | return -ENOTSOCK; |
fddaface | 4489 | |
7a7cacba PB |
4490 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4491 | if (unlikely(ret)) | |
14db8411 | 4492 | return ret; |
fddaface | 4493 | |
7a7cacba PB |
4494 | msg.msg_name = NULL; |
4495 | msg.msg_control = NULL; | |
4496 | msg.msg_controllen = 0; | |
4497 | msg.msg_namelen = 0; | |
fddaface | 4498 | |
04411806 PB |
4499 | flags = req->sr_msg.msg_flags; |
4500 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4501 | flags |= MSG_DONTWAIT; |
0031275d SM |
4502 | if (flags & MSG_WAITALL) |
4503 | min_ret = iov_iter_count(&msg.msg_iter); | |
4504 | ||
7a7cacba PB |
4505 | msg.msg_flags = flags; |
4506 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4507 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4508 | return -EAGAIN; |
4509 | if (ret == -ERESTARTSYS) | |
4510 | ret = -EINTR; | |
fddaface | 4511 | |
0031275d | 4512 | if (ret < min_ret) |
93d2bcd2 | 4513 | req_set_fail(req); |
889fca73 | 4514 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4515 | return 0; |
fddaface JA |
4516 | } |
4517 | ||
1400e697 PB |
4518 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4519 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4520 | { |
4521 | struct io_sr_msg *sr = &req->sr_msg; | |
4522 | struct iovec __user *uiov; | |
4523 | size_t iov_len; | |
4524 | int ret; | |
4525 | ||
1400e697 PB |
4526 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4527 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4528 | if (ret) |
4529 | return ret; | |
4530 | ||
4531 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4532 | if (iov_len > 1) | |
4533 | return -EINVAL; | |
5476dfed | 4534 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4535 | return -EFAULT; |
5476dfed | 4536 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4537 | iomsg->free_iov = NULL; |
52de1fe1 | 4538 | } else { |
257e84a5 | 4539 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4540 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4541 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4542 | false); |
52de1fe1 JA |
4543 | if (ret > 0) |
4544 | ret = 0; | |
4545 | } | |
4546 | ||
4547 | return ret; | |
4548 | } | |
4549 | ||
4550 | #ifdef CONFIG_COMPAT | |
4551 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4552 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4553 | { |
52de1fe1 JA |
4554 | struct io_sr_msg *sr = &req->sr_msg; |
4555 | struct compat_iovec __user *uiov; | |
4556 | compat_uptr_t ptr; | |
4557 | compat_size_t len; | |
4558 | int ret; | |
4559 | ||
4af3417a PB |
4560 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4561 | &ptr, &len); | |
52de1fe1 JA |
4562 | if (ret) |
4563 | return ret; | |
4564 | ||
4565 | uiov = compat_ptr(ptr); | |
4566 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4567 | compat_ssize_t clen; | |
4568 | ||
4569 | if (len > 1) | |
4570 | return -EINVAL; | |
4571 | if (!access_ok(uiov, sizeof(*uiov))) | |
4572 | return -EFAULT; | |
4573 | if (__get_user(clen, &uiov->iov_len)) | |
4574 | return -EFAULT; | |
4575 | if (clen < 0) | |
4576 | return -EINVAL; | |
2d280bc8 | 4577 | sr->len = clen; |
257e84a5 | 4578 | iomsg->free_iov = NULL; |
52de1fe1 | 4579 | } else { |
257e84a5 | 4580 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4581 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4582 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4583 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4584 | if (ret < 0) |
4585 | return ret; | |
4586 | } | |
4587 | ||
4588 | return 0; | |
4589 | } | |
4590 | #endif | |
4591 | ||
1400e697 PB |
4592 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4593 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4594 | { |
1400e697 | 4595 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4596 | |
4597 | #ifdef CONFIG_COMPAT | |
4598 | if (req->ctx->compat) | |
1400e697 | 4599 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4600 | #endif |
52de1fe1 | 4601 | |
1400e697 | 4602 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4603 | } |
4604 | ||
bcda7baa | 4605 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4606 | bool needs_lock) |
bcda7baa JA |
4607 | { |
4608 | struct io_sr_msg *sr = &req->sr_msg; | |
4609 | struct io_buffer *kbuf; | |
4610 | ||
bcda7baa JA |
4611 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4612 | if (IS_ERR(kbuf)) | |
4613 | return kbuf; | |
4614 | ||
4615 | sr->kbuf = kbuf; | |
4616 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4617 | return kbuf; |
fddaface JA |
4618 | } |
4619 | ||
7fbb1b54 PB |
4620 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4621 | { | |
4622 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4623 | } | |
4624 | ||
93642ef8 | 4625 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4626 | { |
99bc4c38 | 4627 | int ret; |
3529d8c2 | 4628 | |
93642ef8 PB |
4629 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4630 | if (!ret) | |
4631 | req->flags |= REQ_F_NEED_CLEANUP; | |
4632 | return ret; | |
4633 | } | |
4634 | ||
4635 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4636 | { | |
4637 | struct io_sr_msg *sr = &req->sr_msg; | |
4638 | ||
d2b6f48b PB |
4639 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4640 | return -EINVAL; | |
4641 | ||
270a5940 | 4642 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4643 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4644 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4645 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4646 | if (sr->msg_flags & MSG_DONTWAIT) | |
4647 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4648 | |
d8768362 JA |
4649 | #ifdef CONFIG_COMPAT |
4650 | if (req->ctx->compat) | |
4651 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4652 | #endif | |
93642ef8 | 4653 | return 0; |
aa1fa28f JA |
4654 | } |
4655 | ||
889fca73 | 4656 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4657 | { |
6b754c8b | 4658 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4659 | struct socket *sock; |
7fbb1b54 | 4660 | struct io_buffer *kbuf; |
7a7cacba | 4661 | unsigned flags; |
0031275d | 4662 | int min_ret = 0; |
52de1fe1 | 4663 | int ret, cflags = 0; |
45d189c6 | 4664 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4665 | |
dba4a925 | 4666 | sock = sock_from_file(req->file); |
7a7cacba | 4667 | if (unlikely(!sock)) |
dba4a925 | 4668 | return -ENOTSOCK; |
3529d8c2 | 4669 | |
257e84a5 PB |
4670 | kmsg = req->async_data; |
4671 | if (!kmsg) { | |
7a7cacba PB |
4672 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4673 | if (ret) | |
681fda8d | 4674 | return ret; |
7a7cacba PB |
4675 | kmsg = &iomsg; |
4676 | } | |
03b1230c | 4677 | |
bc02ef33 | 4678 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4679 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4680 | if (IS_ERR(kbuf)) |
52de1fe1 | 4681 | return PTR_ERR(kbuf); |
7a7cacba | 4682 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4683 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4684 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4685 | 1, req->sr_msg.len); |
4686 | } | |
52de1fe1 | 4687 | |
04411806 PB |
4688 | flags = req->sr_msg.msg_flags; |
4689 | if (force_nonblock) | |
7a7cacba | 4690 | flags |= MSG_DONTWAIT; |
0031275d SM |
4691 | if (flags & MSG_WAITALL) |
4692 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4693 | ||
7a7cacba PB |
4694 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4695 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4696 | if (force_nonblock && ret == -EAGAIN) |
4697 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4698 | if (ret == -ERESTARTSYS) |
4699 | ret = -EINTR; | |
03b1230c | 4700 | |
7fbb1b54 PB |
4701 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4702 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4703 | /* fast path, check for non-NULL to avoid function call */ |
4704 | if (kmsg->free_iov) | |
4705 | kfree(kmsg->free_iov); | |
99bc4c38 | 4706 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4707 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4708 | req_set_fail(req); |
889fca73 | 4709 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4710 | return 0; |
0fa03c62 | 4711 | } |
5d17b4a4 | 4712 | |
889fca73 | 4713 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4714 | { |
6b754c8b | 4715 | struct io_buffer *kbuf; |
7a7cacba PB |
4716 | struct io_sr_msg *sr = &req->sr_msg; |
4717 | struct msghdr msg; | |
4718 | void __user *buf = sr->buf; | |
fddaface | 4719 | struct socket *sock; |
7a7cacba PB |
4720 | struct iovec iov; |
4721 | unsigned flags; | |
0031275d | 4722 | int min_ret = 0; |
bcda7baa | 4723 | int ret, cflags = 0; |
45d189c6 | 4724 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4725 | |
dba4a925 | 4726 | sock = sock_from_file(req->file); |
7a7cacba | 4727 | if (unlikely(!sock)) |
dba4a925 | 4728 | return -ENOTSOCK; |
fddaface | 4729 | |
bc02ef33 | 4730 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4731 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4732 | if (IS_ERR(kbuf)) |
4733 | return PTR_ERR(kbuf); | |
7a7cacba | 4734 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4735 | } |
bcda7baa | 4736 | |
7a7cacba | 4737 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4738 | if (unlikely(ret)) |
4739 | goto out_free; | |
fddaface | 4740 | |
7a7cacba PB |
4741 | msg.msg_name = NULL; |
4742 | msg.msg_control = NULL; | |
4743 | msg.msg_controllen = 0; | |
4744 | msg.msg_namelen = 0; | |
4745 | msg.msg_iocb = NULL; | |
4746 | msg.msg_flags = 0; | |
fddaface | 4747 | |
04411806 PB |
4748 | flags = req->sr_msg.msg_flags; |
4749 | if (force_nonblock) | |
7a7cacba | 4750 | flags |= MSG_DONTWAIT; |
0031275d SM |
4751 | if (flags & MSG_WAITALL) |
4752 | min_ret = iov_iter_count(&msg.msg_iter); | |
4753 | ||
7a7cacba PB |
4754 | ret = sock_recvmsg(sock, &msg, flags); |
4755 | if (force_nonblock && ret == -EAGAIN) | |
4756 | return -EAGAIN; | |
4757 | if (ret == -ERESTARTSYS) | |
4758 | ret = -EINTR; | |
14c32eee | 4759 | out_free: |
7fbb1b54 PB |
4760 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4761 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4762 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4763 | req_set_fail(req); |
889fca73 | 4764 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4765 | return 0; |
fddaface JA |
4766 | } |
4767 | ||
3529d8c2 | 4768 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4769 | { |
8ed8d3c3 JA |
4770 | struct io_accept *accept = &req->accept; |
4771 | ||
14587a46 | 4772 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4773 | return -EINVAL; |
26578cda | 4774 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in) |
17f2fe35 JA |
4775 | return -EINVAL; |
4776 | ||
d55e5f5b JA |
4777 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4778 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4779 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4780 | accept->nofile = rlimit(RLIMIT_NOFILE); |
8ed8d3c3 | 4781 | return 0; |
8ed8d3c3 | 4782 | } |
17f2fe35 | 4783 | |
889fca73 | 4784 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4785 | { |
4786 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4787 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4788 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
8ed8d3c3 JA |
4789 | int ret; |
4790 | ||
e697deed JX |
4791 | if (req->file->f_flags & O_NONBLOCK) |
4792 | req->flags |= REQ_F_NOWAIT; | |
4793 | ||
8ed8d3c3 | 4794 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
09952e3e JA |
4795 | accept->addr_len, accept->flags, |
4796 | accept->nofile); | |
8ed8d3c3 | 4797 | if (ret == -EAGAIN && force_nonblock) |
17f2fe35 | 4798 | return -EAGAIN; |
ac45abc0 PB |
4799 | if (ret < 0) { |
4800 | if (ret == -ERESTARTSYS) | |
4801 | ret = -EINTR; | |
93d2bcd2 | 4802 | req_set_fail(req); |
ac45abc0 | 4803 | } |
889fca73 | 4804 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4805 | return 0; |
8ed8d3c3 JA |
4806 | } |
4807 | ||
93642ef8 PB |
4808 | static int io_connect_prep_async(struct io_kiocb *req) |
4809 | { | |
4810 | struct io_async_connect *io = req->async_data; | |
4811 | struct io_connect *conn = &req->connect; | |
4812 | ||
4813 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4814 | } | |
4815 | ||
3529d8c2 | 4816 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4817 | { |
3529d8c2 | 4818 | struct io_connect *conn = &req->connect; |
f499a021 | 4819 | |
14587a46 | 4820 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 4821 | return -EINVAL; |
26578cda PB |
4822 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
4823 | sqe->splice_fd_in) | |
3fbb51c1 JA |
4824 | return -EINVAL; |
4825 | ||
3529d8c2 JA |
4826 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4827 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4828 | return 0; |
f499a021 JA |
4829 | } |
4830 | ||
889fca73 | 4831 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4832 | { |
e8c2bc1f | 4833 | struct io_async_connect __io, *io; |
f8e85cf2 | 4834 | unsigned file_flags; |
3fbb51c1 | 4835 | int ret; |
45d189c6 | 4836 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4837 | |
e8c2bc1f JA |
4838 | if (req->async_data) { |
4839 | io = req->async_data; | |
f499a021 | 4840 | } else { |
3529d8c2 JA |
4841 | ret = move_addr_to_kernel(req->connect.addr, |
4842 | req->connect.addr_len, | |
e8c2bc1f | 4843 | &__io.address); |
f499a021 JA |
4844 | if (ret) |
4845 | goto out; | |
4846 | io = &__io; | |
4847 | } | |
4848 | ||
3fbb51c1 JA |
4849 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4850 | ||
e8c2bc1f | 4851 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4852 | req->connect.addr_len, file_flags); |
87f80d62 | 4853 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4854 | if (req->async_data) |
b7bb4f7d | 4855 | return -EAGAIN; |
e8c2bc1f | 4856 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4857 | ret = -ENOMEM; |
4858 | goto out; | |
4859 | } | |
e8c2bc1f | 4860 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4861 | return -EAGAIN; |
f499a021 | 4862 | } |
f8e85cf2 JA |
4863 | if (ret == -ERESTARTSYS) |
4864 | ret = -EINTR; | |
f499a021 | 4865 | out: |
4e88d6e7 | 4866 | if (ret < 0) |
93d2bcd2 | 4867 | req_set_fail(req); |
889fca73 | 4868 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4869 | return 0; |
469956e8 Y |
4870 | } |
4871 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4872 | #define IO_NETOP_FN(op) \ |
4873 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4874 | { \ | |
4875 | return -EOPNOTSUPP; \ | |
4876 | } | |
4877 | ||
4878 | #define IO_NETOP_PREP(op) \ | |
4879 | IO_NETOP_FN(op) \ | |
4880 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4881 | { \ | |
4882 | return -EOPNOTSUPP; \ | |
4883 | } \ | |
4884 | ||
4885 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4886 | IO_NETOP_PREP(op) \ | |
4887 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4888 | { \ | |
4889 | return -EOPNOTSUPP; \ | |
4890 | } | |
4891 | ||
4892 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4893 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4894 | IO_NETOP_PREP_ASYNC(connect); | |
4895 | IO_NETOP_PREP(accept); | |
4896 | IO_NETOP_FN(send); | |
4897 | IO_NETOP_FN(recv); | |
469956e8 | 4898 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4899 | |
d7718a9d JA |
4900 | struct io_poll_table { |
4901 | struct poll_table_struct pt; | |
4902 | struct io_kiocb *req; | |
68b11e8b | 4903 | int nr_entries; |
d7718a9d JA |
4904 | int error; |
4905 | }; | |
ce593a6c | 4906 | |
d7718a9d | 4907 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 4908 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 4909 | { |
d7718a9d JA |
4910 | /* for instances that support it check for an event match first: */ |
4911 | if (mask && !(mask & poll->events)) | |
4912 | return 0; | |
4913 | ||
4914 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
4915 | ||
4916 | list_del_init(&poll->wait.entry); | |
4917 | ||
d7718a9d | 4918 | req->result = mask; |
5b0a6acc | 4919 | req->io_task_work.func = func; |
6d816e08 | 4920 | |
d7718a9d | 4921 | /* |
e3aabf95 JA |
4922 | * If this fails, then the task is exiting. When a task exits, the |
4923 | * work gets canceled, so just cancel this request as well instead | |
4924 | * of executing it. We can't safely execute it anyway, as we may not | |
4925 | * have the needed state needed for it anyway. | |
d7718a9d | 4926 | */ |
e09ee510 | 4927 | io_req_task_work_add(req); |
d7718a9d JA |
4928 | return 1; |
4929 | } | |
4930 | ||
74ce6ce4 JA |
4931 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
4932 | __acquires(&req->ctx->completion_lock) | |
4933 | { | |
4934 | struct io_ring_ctx *ctx = req->ctx; | |
4935 | ||
316319e8 | 4936 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
4937 | if (unlikely(req->task->flags & PF_EXITING)) |
4938 | WRITE_ONCE(poll->canceled, true); | |
4939 | ||
74ce6ce4 JA |
4940 | if (!req->result && !READ_ONCE(poll->canceled)) { |
4941 | struct poll_table_struct pt = { ._key = poll->events }; | |
4942 | ||
4943 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
4944 | } | |
4945 | ||
79ebeaee | 4946 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
4947 | if (!req->result && !READ_ONCE(poll->canceled)) { |
4948 | add_wait_queue(poll->head, &poll->wait); | |
4949 | return true; | |
4950 | } | |
4951 | ||
4952 | return false; | |
4953 | } | |
4954 | ||
d4e7cd36 | 4955 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 4956 | { |
e8c2bc1f | 4957 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 4958 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 4959 | return req->async_data; |
d4e7cd36 JA |
4960 | return req->apoll->double_poll; |
4961 | } | |
4962 | ||
4963 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
4964 | { | |
4965 | if (req->opcode == IORING_OP_POLL_ADD) | |
4966 | return &req->poll; | |
4967 | return &req->apoll->poll; | |
4968 | } | |
4969 | ||
4970 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 4971 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
4972 | { |
4973 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
4974 | |
4975 | lockdep_assert_held(&req->ctx->completion_lock); | |
4976 | ||
4977 | if (poll && poll->head) { | |
4978 | struct wait_queue_head *head = poll->head; | |
4979 | ||
79ebeaee | 4980 | spin_lock_irq(&head->lock); |
18bceab1 JA |
4981 | list_del_init(&poll->wait.entry); |
4982 | if (poll->wait.private) | |
de9b4cca | 4983 | req_ref_put(req); |
18bceab1 | 4984 | poll->head = NULL; |
79ebeaee | 4985 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
4986 | } |
4987 | } | |
4988 | ||
e27414be | 4989 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 4990 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
4991 | { |
4992 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 4993 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 4994 | int error; |
18bceab1 | 4995 | |
e27414be | 4996 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 4997 | error = -ECANCELED; |
88e41cf9 | 4998 | req->poll.events |= EPOLLONESHOT; |
e27414be | 4999 | } else { |
5082620f | 5000 | error = mangle_poll(mask); |
e27414be | 5001 | } |
b69de288 JA |
5002 | if (req->poll.events & EPOLLONESHOT) |
5003 | flags = 0; | |
d4d19c19 | 5004 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
88e41cf9 JA |
5005 | req->poll.done = true; |
5006 | flags = 0; | |
5007 | } | |
7b289c38 HX |
5008 | if (flags & IORING_CQE_F_MORE) |
5009 | ctx->cq_extra++; | |
18bceab1 | 5010 | |
18bceab1 | 5011 | io_commit_cqring(ctx); |
88e41cf9 | 5012 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5013 | } |
5014 | ||
f237c30a | 5015 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5016 | { |
5017 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5018 | struct io_kiocb *nxt; |
18bceab1 JA |
5019 | |
5020 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5021 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5022 | } else { |
f40b964a | 5023 | bool done; |
18bceab1 | 5024 | |
e27414be | 5025 | done = io_poll_complete(req, req->result); |
88e41cf9 | 5026 | if (done) { |
a890d01e | 5027 | io_poll_remove_double(req); |
88e41cf9 | 5028 | hash_del(&req->hash_node); |
f40b964a | 5029 | } else { |
88e41cf9 JA |
5030 | req->result = 0; |
5031 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5032 | } | |
79ebeaee | 5033 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5034 | io_cqring_ev_posted(ctx); |
18bceab1 | 5035 | |
88e41cf9 JA |
5036 | if (done) { |
5037 | nxt = io_put_req_find_next(req); | |
5038 | if (nxt) | |
f237c30a | 5039 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5040 | } |
dd221f46 | 5041 | } |
18bceab1 JA |
5042 | } |
5043 | ||
5044 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5045 | int sync, void *key) | |
5046 | { | |
5047 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5048 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5049 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5050 | unsigned long flags; |
18bceab1 JA |
5051 | |
5052 | /* for instances that support it check for an event match first: */ | |
5053 | if (mask && !(mask & poll->events)) | |
5054 | return 0; | |
88e41cf9 JA |
5055 | if (!(poll->events & EPOLLONESHOT)) |
5056 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5057 | |
8706e04e JA |
5058 | list_del_init(&wait->entry); |
5059 | ||
9ce85ef2 | 5060 | if (poll->head) { |
18bceab1 JA |
5061 | bool done; |
5062 | ||
79ebeaee | 5063 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5064 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5065 | if (!done) |
807abcb0 | 5066 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5067 | /* make sure double remove sees this as being gone */ |
5068 | wait->private = NULL; | |
79ebeaee | 5069 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5070 | if (!done) { |
5071 | /* use wait func handler, so it matches the rq type */ | |
5072 | poll->wait.func(&poll->wait, mode, sync, key); | |
5073 | } | |
18bceab1 | 5074 | } |
de9b4cca | 5075 | req_ref_put(req); |
18bceab1 JA |
5076 | return 1; |
5077 | } | |
5078 | ||
5079 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5080 | wait_queue_func_t wake_func) | |
5081 | { | |
5082 | poll->head = NULL; | |
5083 | poll->done = false; | |
5084 | poll->canceled = false; | |
464dca61 JA |
5085 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5086 | /* mask in events that we always want/need */ | |
5087 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5088 | INIT_LIST_HEAD(&poll->wait.entry); |
5089 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5090 | } | |
5091 | ||
5092 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5093 | struct wait_queue_head *head, |
5094 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5095 | { |
5096 | struct io_kiocb *req = pt->req; | |
5097 | ||
5098 | /* | |
68b11e8b PB |
5099 | * The file being polled uses multiple waitqueues for poll handling |
5100 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5101 | * if this happens. | |
18bceab1 | 5102 | */ |
68b11e8b | 5103 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5104 | struct io_poll_iocb *poll_one = poll; |
5105 | ||
23a65db8 PB |
5106 | /* double add on the same waitqueue head, ignore */ |
5107 | if (poll_one->head == head) | |
5108 | return; | |
18bceab1 | 5109 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5110 | if (*poll_ptr) { |
23a65db8 PB |
5111 | if ((*poll_ptr)->head == head) |
5112 | return; | |
18bceab1 JA |
5113 | pt->error = -EINVAL; |
5114 | return; | |
5115 | } | |
ea6a693d JA |
5116 | /* |
5117 | * Can't handle multishot for double wait for now, turn it | |
5118 | * into one-shot mode. | |
5119 | */ | |
7a274727 PB |
5120 | if (!(poll_one->events & EPOLLONESHOT)) |
5121 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5122 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5123 | if (!poll) { | |
5124 | pt->error = -ENOMEM; | |
5125 | return; | |
5126 | } | |
58852d4d | 5127 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5128 | req_ref_get(req); |
18bceab1 | 5129 | poll->wait.private = req; |
807abcb0 | 5130 | *poll_ptr = poll; |
18bceab1 JA |
5131 | } |
5132 | ||
68b11e8b | 5133 | pt->nr_entries++; |
18bceab1 | 5134 | poll->head = head; |
a31eb4a2 JX |
5135 | |
5136 | if (poll->events & EPOLLEXCLUSIVE) | |
5137 | add_wait_queue_exclusive(head, &poll->wait); | |
5138 | else | |
5139 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5140 | } |
5141 | ||
5142 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5143 | struct poll_table_struct *p) | |
5144 | { | |
5145 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5146 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5147 | |
807abcb0 | 5148 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5149 | } |
5150 | ||
f237c30a | 5151 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5152 | { |
d7718a9d JA |
5153 | struct async_poll *apoll = req->apoll; |
5154 | struct io_ring_ctx *ctx = req->ctx; | |
5155 | ||
236daeae | 5156 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5157 | |
74ce6ce4 | 5158 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5159 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5160 | return; |
d7718a9d JA |
5161 | } |
5162 | ||
0ea13b44 | 5163 | hash_del(&req->hash_node); |
d4e7cd36 | 5164 | io_poll_remove_double(req); |
79ebeaee | 5165 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5166 | |
0be0b0e3 | 5167 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5168 | io_req_task_submit(req, locked); |
0be0b0e3 | 5169 | else |
2593553a | 5170 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5171 | } |
5172 | ||
5173 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5174 | void *key) | |
5175 | { | |
5176 | struct io_kiocb *req = wait->private; | |
5177 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5178 | ||
5179 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5180 | key_to_poll(key)); | |
5181 | ||
5182 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5183 | } | |
5184 | ||
5185 | static void io_poll_req_insert(struct io_kiocb *req) | |
5186 | { | |
5187 | struct io_ring_ctx *ctx = req->ctx; | |
5188 | struct hlist_head *list; | |
5189 | ||
5190 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5191 | hlist_add_head(&req->hash_node, list); | |
5192 | } | |
5193 | ||
5194 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5195 | struct io_poll_iocb *poll, | |
5196 | struct io_poll_table *ipt, __poll_t mask, | |
5197 | wait_queue_func_t wake_func) | |
5198 | __acquires(&ctx->completion_lock) | |
5199 | { | |
5200 | struct io_ring_ctx *ctx = req->ctx; | |
5201 | bool cancel = false; | |
5202 | ||
4d52f338 | 5203 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5204 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5205 | poll->file = req->file; |
18bceab1 | 5206 | poll->wait.private = req; |
d7718a9d JA |
5207 | |
5208 | ipt->pt._key = mask; | |
5209 | ipt->req = req; | |
68b11e8b PB |
5210 | ipt->error = 0; |
5211 | ipt->nr_entries = 0; | |
d7718a9d | 5212 | |
d7718a9d | 5213 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5214 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5215 | ipt->error = -EINVAL; | |
d7718a9d | 5216 | |
79ebeaee | 5217 | spin_lock(&ctx->completion_lock); |
a890d01e | 5218 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5219 | io_poll_remove_double(req); |
d7718a9d | 5220 | if (likely(poll->head)) { |
79ebeaee | 5221 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5222 | if (unlikely(list_empty(&poll->wait.entry))) { |
5223 | if (ipt->error) | |
5224 | cancel = true; | |
5225 | ipt->error = 0; | |
5226 | mask = 0; | |
5227 | } | |
88e41cf9 | 5228 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5229 | list_del_init(&poll->wait.entry); |
5230 | else if (cancel) | |
5231 | WRITE_ONCE(poll->canceled, true); | |
5232 | else if (!poll->done) /* actually waiting for an event */ | |
5233 | io_poll_req_insert(req); | |
79ebeaee | 5234 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5235 | } |
5236 | ||
5237 | return mask; | |
5238 | } | |
5239 | ||
59b735ae OL |
5240 | enum { |
5241 | IO_APOLL_OK, | |
5242 | IO_APOLL_ABORTED, | |
5243 | IO_APOLL_READY | |
5244 | }; | |
5245 | ||
5246 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5247 | { |
5248 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5249 | struct io_ring_ctx *ctx = req->ctx; | |
5250 | struct async_poll *apoll; | |
5251 | struct io_poll_table ipt; | |
b2d9c3da | 5252 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5253 | int rw; |
d7718a9d JA |
5254 | |
5255 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5256 | return IO_APOLL_ABORTED; |
24c74678 | 5257 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5258 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5259 | if (!def->pollin && !def->pollout) |
5260 | return IO_APOLL_ABORTED; | |
5261 | ||
5262 | if (def->pollin) { | |
9dab14b8 | 5263 | rw = READ; |
b2d9c3da PB |
5264 | mask |= POLLIN | POLLRDNORM; |
5265 | ||
5266 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5267 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5268 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5269 | mask &= ~POLLIN; | |
5270 | } else { | |
9dab14b8 | 5271 | rw = WRITE; |
b2d9c3da PB |
5272 | mask |= POLLOUT | POLLWRNORM; |
5273 | } | |
5274 | ||
9dab14b8 | 5275 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5276 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5277 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5278 | |
5279 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5280 | if (unlikely(!apoll)) | |
59b735ae | 5281 | return IO_APOLL_ABORTED; |
807abcb0 | 5282 | apoll->double_poll = NULL; |
d7718a9d | 5283 | req->apoll = apoll; |
b2d9c3da | 5284 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5285 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5286 | io_req_set_refcount(req); |
d7718a9d JA |
5287 | |
5288 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5289 | io_async_wake); | |
79ebeaee | 5290 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5291 | if (ret || ipt.error) |
5292 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5293 | ||
236daeae OL |
5294 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5295 | mask, apoll->poll.events); | |
59b735ae | 5296 | return IO_APOLL_OK; |
d7718a9d JA |
5297 | } |
5298 | ||
5299 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5300 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5301 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5302 | { |
b41e9852 | 5303 | bool do_complete = false; |
221c5eb2 | 5304 | |
5082620f JA |
5305 | if (!poll->head) |
5306 | return false; | |
79ebeaee | 5307 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5308 | if (do_cancel) |
5309 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5310 | if (!list_empty(&poll->wait.entry)) { |
5311 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5312 | do_complete = true; |
221c5eb2 | 5313 | } |
79ebeaee | 5314 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5315 | hash_del(&req->hash_node); |
d7718a9d JA |
5316 | return do_complete; |
5317 | } | |
5318 | ||
5d709043 | 5319 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5320 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5321 | { |
5322 | bool do_complete; | |
5323 | ||
d4e7cd36 | 5324 | io_poll_remove_double(req); |
e31001a3 | 5325 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5326 | |
b41e9852 | 5327 | if (do_complete) { |
d4d19c19 | 5328 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5329 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5330 | req_set_fail(req); |
91c2f697 | 5331 | io_put_req_deferred(req); |
5d709043 | 5332 | } |
b41e9852 | 5333 | return do_complete; |
221c5eb2 JA |
5334 | } |
5335 | ||
76e1b642 JA |
5336 | /* |
5337 | * Returns true if we found and killed one or more poll requests | |
5338 | */ | |
6b81928d | 5339 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5340 | bool cancel_all) |
221c5eb2 | 5341 | { |
78076bb6 | 5342 | struct hlist_node *tmp; |
221c5eb2 | 5343 | struct io_kiocb *req; |
8e2e1faf | 5344 | int posted = 0, i; |
221c5eb2 | 5345 | |
79ebeaee | 5346 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5347 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5348 | struct hlist_head *list; | |
5349 | ||
5350 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5351 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5352 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5353 | posted += io_poll_remove_one(req); |
5354 | } | |
221c5eb2 | 5355 | } |
79ebeaee | 5356 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5357 | |
8e2e1faf JA |
5358 | if (posted) |
5359 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5360 | |
5361 | return posted != 0; | |
221c5eb2 JA |
5362 | } |
5363 | ||
9ba5fac8 PB |
5364 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5365 | bool poll_only) | |
e07785b0 | 5366 | __must_hold(&ctx->completion_lock) |
47f46768 | 5367 | { |
78076bb6 | 5368 | struct hlist_head *list; |
47f46768 JA |
5369 | struct io_kiocb *req; |
5370 | ||
78076bb6 JA |
5371 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5372 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5373 | if (sqe_addr != req->user_data) |
5374 | continue; | |
9ba5fac8 PB |
5375 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5376 | continue; | |
b2cb805f | 5377 | return req; |
47f46768 | 5378 | } |
b2cb805f JA |
5379 | return NULL; |
5380 | } | |
5381 | ||
9ba5fac8 PB |
5382 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5383 | bool poll_only) | |
e07785b0 | 5384 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5385 | { |
5386 | struct io_kiocb *req; | |
5387 | ||
9ba5fac8 | 5388 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5389 | if (!req) |
5390 | return -ENOENT; | |
5391 | if (io_poll_remove_one(req)) | |
5392 | return 0; | |
5393 | ||
5394 | return -EALREADY; | |
47f46768 JA |
5395 | } |
5396 | ||
9096af3e PB |
5397 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5398 | unsigned int flags) | |
5399 | { | |
5400 | u32 events; | |
47f46768 | 5401 | |
9096af3e PB |
5402 | events = READ_ONCE(sqe->poll32_events); |
5403 | #ifdef __BIG_ENDIAN | |
5404 | events = swahw32(events); | |
5405 | #endif | |
5406 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5407 | events |= EPOLLONESHOT; | |
5408 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5409 | } |
5410 | ||
c5de0036 | 5411 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5412 | const struct io_uring_sqe *sqe) |
0969e783 | 5413 | { |
c5de0036 PB |
5414 | struct io_poll_update *upd = &req->poll_update; |
5415 | u32 flags; | |
5416 | ||
0969e783 JA |
5417 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5418 | return -EINVAL; | |
26578cda | 5419 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5420 | return -EINVAL; |
5421 | flags = READ_ONCE(sqe->len); | |
5422 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5423 | IORING_POLL_ADD_MULTI)) | |
5424 | return -EINVAL; | |
5425 | /* meaningless without update */ | |
5426 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5427 | return -EINVAL; |
5428 | ||
c5de0036 PB |
5429 | upd->old_user_data = READ_ONCE(sqe->addr); |
5430 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5431 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5432 | |
c5de0036 PB |
5433 | upd->new_user_data = READ_ONCE(sqe->off); |
5434 | if (!upd->update_user_data && upd->new_user_data) | |
5435 | return -EINVAL; | |
5436 | if (upd->update_events) | |
5437 | upd->events = io_poll_parse_events(sqe, flags); | |
5438 | else if (sqe->poll32_events) | |
5439 | return -EINVAL; | |
221c5eb2 | 5440 | |
221c5eb2 JA |
5441 | return 0; |
5442 | } | |
5443 | ||
221c5eb2 JA |
5444 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5445 | void *key) | |
5446 | { | |
c2f2eb7d JA |
5447 | struct io_kiocb *req = wait->private; |
5448 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5449 | |
d7718a9d | 5450 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5451 | } |
5452 | ||
221c5eb2 JA |
5453 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5454 | struct poll_table_struct *p) | |
5455 | { | |
5456 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5457 | ||
e8c2bc1f | 5458 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5459 | } |
5460 | ||
3529d8c2 | 5461 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5462 | { |
5463 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5464 | u32 flags; |
221c5eb2 JA |
5465 | |
5466 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5467 | return -EINVAL; | |
c5de0036 | 5468 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5469 | return -EINVAL; |
5470 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5471 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5472 | return -EINVAL; |
5473 | ||
48dcd38d | 5474 | io_req_set_refcount(req); |
c5de0036 | 5475 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5476 | return 0; |
5477 | } | |
5478 | ||
61e98203 | 5479 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5480 | { |
5481 | struct io_poll_iocb *poll = &req->poll; | |
5482 | struct io_ring_ctx *ctx = req->ctx; | |
5483 | struct io_poll_table ipt; | |
0969e783 | 5484 | __poll_t mask; |
0969e783 | 5485 | |
d7718a9d | 5486 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5487 | |
d7718a9d JA |
5488 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5489 | io_poll_wake); | |
221c5eb2 | 5490 | |
8c838788 | 5491 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5492 | ipt.error = 0; |
e27414be | 5493 | io_poll_complete(req, mask); |
221c5eb2 | 5494 | } |
79ebeaee | 5495 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5496 | |
8c838788 JA |
5497 | if (mask) { |
5498 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5499 | if (poll->events & EPOLLONESHOT) |
5500 | io_put_req(req); | |
221c5eb2 | 5501 | } |
8c838788 | 5502 | return ipt.error; |
221c5eb2 JA |
5503 | } |
5504 | ||
c5de0036 | 5505 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5506 | { |
5507 | struct io_ring_ctx *ctx = req->ctx; | |
5508 | struct io_kiocb *preq; | |
cb3b200e | 5509 | bool completing; |
b69de288 JA |
5510 | int ret; |
5511 | ||
79ebeaee | 5512 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5513 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5514 | if (!preq) { |
5515 | ret = -ENOENT; | |
5516 | goto err; | |
b69de288 | 5517 | } |
cb3b200e | 5518 | |
c5de0036 PB |
5519 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5520 | completing = true; | |
5521 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5522 | goto err; | |
5523 | } | |
5524 | ||
cb3b200e JA |
5525 | /* |
5526 | * Don't allow racy completion with singleshot, as we cannot safely | |
5527 | * update those. For multishot, if we're racing with completion, just | |
5528 | * let completion re-add it. | |
5529 | */ | |
5530 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5531 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5532 | ret = -EALREADY; | |
5533 | goto err; | |
b69de288 JA |
5534 | } |
5535 | /* we now have a detached poll request. reissue. */ | |
5536 | ret = 0; | |
5537 | err: | |
b69de288 | 5538 | if (ret < 0) { |
79ebeaee | 5539 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5540 | req_set_fail(req); |
b69de288 JA |
5541 | io_req_complete(req, ret); |
5542 | return 0; | |
5543 | } | |
5544 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5545 | if (req->poll_update.update_events) { |
b69de288 | 5546 | preq->poll.events &= ~0xffff; |
9d805892 | 5547 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5548 | preq->poll.events |= IO_POLL_UNMASK; |
5549 | } | |
9d805892 PB |
5550 | if (req->poll_update.update_user_data) |
5551 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5552 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5553 | |
b69de288 JA |
5554 | /* complete update request, we're done with it */ |
5555 | io_req_complete(req, ret); | |
5556 | ||
cb3b200e | 5557 | if (!completing) { |
c5de0036 | 5558 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5559 | if (ret < 0) { |
93d2bcd2 | 5560 | req_set_fail(preq); |
cb3b200e JA |
5561 | io_req_complete(preq, ret); |
5562 | } | |
b69de288 JA |
5563 | } |
5564 | return 0; | |
5565 | } | |
5566 | ||
f237c30a | 5567 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5568 | { |
89850fce | 5569 | req_set_fail(req); |
505657bc | 5570 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5571 | } |
5572 | ||
5262f567 JA |
5573 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5574 | { | |
ad8a48ac JA |
5575 | struct io_timeout_data *data = container_of(timer, |
5576 | struct io_timeout_data, timer); | |
5577 | struct io_kiocb *req = data->req; | |
5578 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5579 | unsigned long flags; |
5580 | ||
89850fce | 5581 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5582 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5583 | atomic_set(&req->ctx->cq_timeouts, |
5584 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5585 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5586 | |
89850fce JA |
5587 | req->io_task_work.func = io_req_task_timeout; |
5588 | io_req_task_work_add(req); | |
5262f567 JA |
5589 | return HRTIMER_NORESTART; |
5590 | } | |
5591 | ||
fbd15848 PB |
5592 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5593 | __u64 user_data) | |
89850fce | 5594 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5595 | { |
fbd15848 | 5596 | struct io_timeout_data *io; |
47f46768 | 5597 | struct io_kiocb *req; |
fd9c7bc5 | 5598 | bool found = false; |
f254ac04 | 5599 | |
135fcde8 | 5600 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5601 | found = user_data == req->user_data; |
5602 | if (found) | |
47f46768 | 5603 | break; |
47f46768 | 5604 | } |
fd9c7bc5 PB |
5605 | if (!found) |
5606 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5607 | |
5608 | io = req->async_data; | |
fd9c7bc5 | 5609 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5610 | return ERR_PTR(-EALREADY); |
a71976f3 | 5611 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5612 | return req; |
5613 | } | |
47f46768 | 5614 | |
fbd15848 | 5615 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5616 | __must_hold(&ctx->completion_lock) |
89850fce | 5617 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5618 | { |
5619 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5620 | ||
5621 | if (IS_ERR(req)) | |
5622 | return PTR_ERR(req); | |
f254ac04 | 5623 | |
93d2bcd2 | 5624 | req_set_fail(req); |
d4d19c19 | 5625 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5626 | io_put_req_deferred(req); |
f254ac04 JA |
5627 | return 0; |
5628 | } | |
5629 | ||
9c8e11b3 PB |
5630 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5631 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5632 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5633 | { |
9c8e11b3 PB |
5634 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5635 | struct io_timeout_data *data; | |
47f46768 | 5636 | |
9c8e11b3 PB |
5637 | if (IS_ERR(req)) |
5638 | return PTR_ERR(req); | |
47f46768 | 5639 | |
9c8e11b3 PB |
5640 | req->timeout.off = 0; /* noseq */ |
5641 | data = req->async_data; | |
5642 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
5643 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); | |
5644 | data->timer.function = io_timeout_fn; | |
5645 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5646 | return 0; | |
47f46768 JA |
5647 | } |
5648 | ||
3529d8c2 JA |
5649 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5650 | const struct io_uring_sqe *sqe) | |
b29472ee | 5651 | { |
9c8e11b3 PB |
5652 | struct io_timeout_rem *tr = &req->timeout_rem; |
5653 | ||
b29472ee JA |
5654 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5655 | return -EINVAL; | |
61710e43 DA |
5656 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5657 | return -EINVAL; | |
26578cda | 5658 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
5659 | return -EINVAL; |
5660 | ||
9c8e11b3 PB |
5661 | tr->addr = READ_ONCE(sqe->addr); |
5662 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5663 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5664 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5665 | return -EINVAL; | |
5666 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5667 | return -EFAULT; | |
5668 | } else if (tr->flags) { | |
5669 | /* timeout removal doesn't support flags */ | |
b29472ee | 5670 | return -EINVAL; |
9c8e11b3 | 5671 | } |
b29472ee | 5672 | |
b29472ee JA |
5673 | return 0; |
5674 | } | |
5675 | ||
8662daec PB |
5676 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5677 | { | |
5678 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5679 | : HRTIMER_MODE_REL; | |
5680 | } | |
5681 | ||
11365043 JA |
5682 | /* |
5683 | * Remove or update an existing timeout command | |
5684 | */ | |
61e98203 | 5685 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5686 | { |
9c8e11b3 | 5687 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5688 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5689 | int ret; |
11365043 | 5690 | |
ec3c3d0f PB |
5691 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
5692 | spin_lock(&ctx->completion_lock); | |
5693 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 5694 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
5695 | spin_unlock_irq(&ctx->timeout_lock); |
5696 | spin_unlock(&ctx->completion_lock); | |
5697 | } else { | |
5698 | spin_lock_irq(&ctx->timeout_lock); | |
8662daec PB |
5699 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
5700 | io_translate_timeout_mode(tr->flags)); | |
ec3c3d0f PB |
5701 | spin_unlock_irq(&ctx->timeout_lock); |
5702 | } | |
11365043 | 5703 | |
4e88d6e7 | 5704 | if (ret < 0) |
93d2bcd2 | 5705 | req_set_fail(req); |
505657bc | 5706 | io_req_complete_post(req, ret, 0); |
11365043 | 5707 | return 0; |
5262f567 JA |
5708 | } |
5709 | ||
3529d8c2 | 5710 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5711 | bool is_timeout_link) |
5262f567 | 5712 | { |
ad8a48ac | 5713 | struct io_timeout_data *data; |
a41525ab | 5714 | unsigned flags; |
56080b02 | 5715 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5716 | |
ad8a48ac | 5717 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5718 | return -EINVAL; |
26578cda PB |
5719 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
5720 | sqe->splice_fd_in) | |
a41525ab | 5721 | return -EINVAL; |
56080b02 | 5722 | if (off && is_timeout_link) |
2d28390a | 5723 | return -EINVAL; |
a41525ab JA |
5724 | flags = READ_ONCE(sqe->timeout_flags); |
5725 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 5726 | return -EINVAL; |
bdf20073 | 5727 | |
bfe68a22 | 5728 | req->timeout.off = off; |
f18ee4cf PB |
5729 | if (unlikely(off && !req->ctx->off_timeout_used)) |
5730 | req->ctx->off_timeout_used = true; | |
26a61679 | 5731 | |
e8c2bc1f | 5732 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5733 | return -ENOMEM; |
5734 | ||
e8c2bc1f | 5735 | data = req->async_data; |
ad8a48ac | 5736 | data->req = req; |
ad8a48ac JA |
5737 | |
5738 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5739 | return -EFAULT; |
5740 | ||
8662daec | 5741 | data->mode = io_translate_timeout_mode(flags); |
ad8a48ac | 5742 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
b97e736a PB |
5743 | |
5744 | if (is_timeout_link) { | |
5745 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
5746 | ||
5747 | if (!link->head) | |
5748 | return -EINVAL; | |
5749 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
5750 | return -EINVAL; | |
4d13d1a4 PB |
5751 | req->timeout.head = link->last; |
5752 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 5753 | } |
ad8a48ac JA |
5754 | return 0; |
5755 | } | |
5756 | ||
61e98203 | 5757 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5758 | { |
ad8a48ac | 5759 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5760 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5761 | struct list_head *entry; |
bfe68a22 | 5762 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5763 | |
89850fce | 5764 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 5765 | |
5262f567 JA |
5766 | /* |
5767 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5768 | * timeout event to be satisfied. If it isn't set, then this is |
5769 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5770 | */ |
8eb7e2d0 | 5771 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5772 | entry = ctx->timeout_list.prev; |
5773 | goto add; | |
5774 | } | |
5262f567 | 5775 | |
bfe68a22 PB |
5776 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5777 | req->timeout.target_seq = tail + off; | |
5262f567 | 5778 | |
f010505b MDG |
5779 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5780 | * This is safe because ->completion_lock is held, and submissions | |
5781 | * and completions are never mixed in the same ->completion_lock section. | |
5782 | */ | |
5783 | ctx->cq_last_tm_flush = tail; | |
5784 | ||
5262f567 JA |
5785 | /* |
5786 | * Insertion sort, ensuring the first entry in the list is always | |
5787 | * the one we need first. | |
5788 | */ | |
5262f567 | 5789 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5790 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5791 | timeout.list); | |
5262f567 | 5792 | |
8eb7e2d0 | 5793 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5794 | continue; |
bfe68a22 PB |
5795 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5796 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5797 | break; |
5798 | } | |
93bd25bb | 5799 | add: |
135fcde8 | 5800 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5801 | data->timer.function = io_timeout_fn; |
5802 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 5803 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
5804 | return 0; |
5805 | } | |
5262f567 | 5806 | |
f458dd84 PB |
5807 | struct io_cancel_data { |
5808 | struct io_ring_ctx *ctx; | |
5809 | u64 user_data; | |
5810 | }; | |
5811 | ||
62755e35 JA |
5812 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5813 | { | |
5814 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5815 | struct io_cancel_data *cd = data; |
62755e35 | 5816 | |
f458dd84 | 5817 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5818 | } |
5819 | ||
f458dd84 PB |
5820 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5821 | struct io_ring_ctx *ctx) | |
62755e35 | 5822 | { |
f458dd84 | 5823 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5824 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5825 | int ret = 0; |
5826 | ||
f458dd84 | 5827 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5828 | return -ENOENT; |
5829 | ||
f458dd84 | 5830 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5831 | switch (cancel_ret) { |
5832 | case IO_WQ_CANCEL_OK: | |
5833 | ret = 0; | |
5834 | break; | |
5835 | case IO_WQ_CANCEL_RUNNING: | |
5836 | ret = -EALREADY; | |
5837 | break; | |
5838 | case IO_WQ_CANCEL_NOTFOUND: | |
5839 | ret = -ENOENT; | |
5840 | break; | |
5841 | } | |
5842 | ||
e977d6d3 JA |
5843 | return ret; |
5844 | } | |
5845 | ||
8cb01fac | 5846 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 5847 | { |
8cb01fac | 5848 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
5849 | int ret; |
5850 | ||
8cb01fac PB |
5851 | WARN_ON_ONCE(req->task != current); |
5852 | ||
f458dd84 | 5853 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 5854 | if (ret != -ENOENT) |
8cb01fac | 5855 | return ret; |
505657bc PB |
5856 | |
5857 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 5858 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 5859 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 5860 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 5861 | if (ret != -ENOENT) |
505657bc PB |
5862 | goto out; |
5863 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
5864 | out: | |
5865 | spin_unlock(&ctx->completion_lock); | |
5866 | return ret; | |
47f46768 JA |
5867 | } |
5868 | ||
3529d8c2 JA |
5869 | static int io_async_cancel_prep(struct io_kiocb *req, |
5870 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5871 | { |
fbf23849 | 5872 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5873 | return -EINVAL; |
61710e43 DA |
5874 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5875 | return -EINVAL; | |
26578cda PB |
5876 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
5877 | sqe->splice_fd_in) | |
e977d6d3 JA |
5878 | return -EINVAL; |
5879 | ||
fbf23849 JA |
5880 | req->cancel.addr = READ_ONCE(sqe->addr); |
5881 | return 0; | |
5882 | } | |
5883 | ||
61e98203 | 5884 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5885 | { |
5886 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5887 | u64 sqe_addr = req->cancel.addr; |
5888 | struct io_tctx_node *node; | |
5889 | int ret; | |
5890 | ||
8cb01fac | 5891 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
5892 | if (ret != -ENOENT) |
5893 | goto done; | |
58f99373 PB |
5894 | |
5895 | /* slow path, try all io-wq's */ | |
5896 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
5897 | ret = -ENOENT; | |
5898 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
5899 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 5900 | |
58f99373 PB |
5901 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
5902 | if (ret != -ENOENT) | |
5903 | break; | |
5904 | } | |
5905 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 5906 | done: |
58f99373 | 5907 | if (ret < 0) |
93d2bcd2 | 5908 | req_set_fail(req); |
505657bc | 5909 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
5910 | return 0; |
5911 | } | |
5912 | ||
269bbe5f | 5913 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
5914 | const struct io_uring_sqe *sqe) |
5915 | { | |
61710e43 DA |
5916 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5917 | return -EINVAL; | |
26578cda | 5918 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
5919 | return -EINVAL; |
5920 | ||
269bbe5f BM |
5921 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
5922 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
5923 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 5924 | return -EINVAL; |
269bbe5f | 5925 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
5926 | return 0; |
5927 | } | |
5928 | ||
889fca73 | 5929 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5930 | { |
5931 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 5932 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 5933 | int ret; |
fbf23849 | 5934 | |
45d189c6 | 5935 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 5936 | return -EAGAIN; |
05f3fb3c | 5937 | |
269bbe5f BM |
5938 | up.offset = req->rsrc_update.offset; |
5939 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
5940 | up.nr = 0; |
5941 | up.tags = 0; | |
615cee49 | 5942 | up.resv = 0; |
05f3fb3c JA |
5943 | |
5944 | mutex_lock(&ctx->uring_lock); | |
fdecb662 | 5945 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 5946 | &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
5947 | mutex_unlock(&ctx->uring_lock); |
5948 | ||
5949 | if (ret < 0) | |
93d2bcd2 | 5950 | req_set_fail(req); |
889fca73 | 5951 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
5952 | return 0; |
5953 | } | |
5954 | ||
bfe76559 | 5955 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 5956 | { |
d625c6ee | 5957 | switch (req->opcode) { |
e781573e | 5958 | case IORING_OP_NOP: |
bfe76559 | 5959 | return 0; |
f67676d1 JA |
5960 | case IORING_OP_READV: |
5961 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 5962 | case IORING_OP_READ: |
bfe76559 | 5963 | return io_read_prep(req, sqe); |
f67676d1 JA |
5964 | case IORING_OP_WRITEV: |
5965 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 5966 | case IORING_OP_WRITE: |
bfe76559 | 5967 | return io_write_prep(req, sqe); |
0969e783 | 5968 | case IORING_OP_POLL_ADD: |
bfe76559 | 5969 | return io_poll_add_prep(req, sqe); |
0969e783 | 5970 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 5971 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 5972 | case IORING_OP_FSYNC: |
1155c76a | 5973 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 5974 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 5975 | return io_sfr_prep(req, sqe); |
03b1230c | 5976 | case IORING_OP_SENDMSG: |
fddaface | 5977 | case IORING_OP_SEND: |
bfe76559 | 5978 | return io_sendmsg_prep(req, sqe); |
03b1230c | 5979 | case IORING_OP_RECVMSG: |
fddaface | 5980 | case IORING_OP_RECV: |
bfe76559 | 5981 | return io_recvmsg_prep(req, sqe); |
f499a021 | 5982 | case IORING_OP_CONNECT: |
bfe76559 | 5983 | return io_connect_prep(req, sqe); |
2d28390a | 5984 | case IORING_OP_TIMEOUT: |
bfe76559 | 5985 | return io_timeout_prep(req, sqe, false); |
b29472ee | 5986 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 5987 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 5988 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 5989 | return io_async_cancel_prep(req, sqe); |
2d28390a | 5990 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 5991 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 5992 | case IORING_OP_ACCEPT: |
bfe76559 | 5993 | return io_accept_prep(req, sqe); |
d63d1b5e | 5994 | case IORING_OP_FALLOCATE: |
bfe76559 | 5995 | return io_fallocate_prep(req, sqe); |
15b71abe | 5996 | case IORING_OP_OPENAT: |
bfe76559 | 5997 | return io_openat_prep(req, sqe); |
b5dba59e | 5998 | case IORING_OP_CLOSE: |
bfe76559 | 5999 | return io_close_prep(req, sqe); |
05f3fb3c | 6000 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6001 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6002 | case IORING_OP_STATX: |
bfe76559 | 6003 | return io_statx_prep(req, sqe); |
4840e418 | 6004 | case IORING_OP_FADVISE: |
bfe76559 | 6005 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6006 | case IORING_OP_MADVISE: |
bfe76559 | 6007 | return io_madvise_prep(req, sqe); |
cebdb986 | 6008 | case IORING_OP_OPENAT2: |
bfe76559 | 6009 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6010 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6011 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6012 | case IORING_OP_SPLICE: |
bfe76559 | 6013 | return io_splice_prep(req, sqe); |
ddf0322d | 6014 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6015 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6016 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6017 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6018 | case IORING_OP_TEE: |
bfe76559 | 6019 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6020 | case IORING_OP_SHUTDOWN: |
6021 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6022 | case IORING_OP_RENAMEAT: |
6023 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6024 | case IORING_OP_UNLINKAT: |
6025 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6026 | } |
6027 | ||
bfe76559 PB |
6028 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6029 | req->opcode); | |
bd54b6fe | 6030 | return -EINVAL; |
bfe76559 PB |
6031 | } |
6032 | ||
93642ef8 | 6033 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6034 | { |
b7e298d2 PB |
6035 | if (!io_op_defs[req->opcode].needs_async_setup) |
6036 | return 0; | |
6037 | if (WARN_ON_ONCE(req->async_data)) | |
6038 | return -EFAULT; | |
6039 | if (io_alloc_async_data(req)) | |
6040 | return -EAGAIN; | |
6041 | ||
93642ef8 PB |
6042 | switch (req->opcode) { |
6043 | case IORING_OP_READV: | |
93642ef8 PB |
6044 | return io_rw_prep_async(req, READ); |
6045 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6046 | return io_rw_prep_async(req, WRITE); |
6047 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6048 | return io_sendmsg_prep_async(req); |
6049 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6050 | return io_recvmsg_prep_async(req); |
6051 | case IORING_OP_CONNECT: | |
6052 | return io_connect_prep_async(req); | |
6053 | } | |
b7e298d2 PB |
6054 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6055 | req->opcode); | |
6056 | return -EFAULT; | |
f67676d1 JA |
6057 | } |
6058 | ||
9cf7c104 PB |
6059 | static u32 io_get_sequence(struct io_kiocb *req) |
6060 | { | |
a3dbdf54 | 6061 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6062 | |
a3dbdf54 PB |
6063 | /* need original cached_sq_head, but it was increased for each req */ |
6064 | io_for_each_link(req, req) | |
6065 | seq--; | |
6066 | return seq; | |
9cf7c104 PB |
6067 | } |
6068 | ||
76cc33d7 | 6069 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6070 | { |
3c19966d | 6071 | struct io_kiocb *pos; |
a197f664 | 6072 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6073 | struct io_defer_entry *de; |
f67676d1 | 6074 | int ret; |
9cf7c104 | 6075 | u32 seq; |
de0617e4 | 6076 | |
3c19966d PB |
6077 | /* |
6078 | * If we need to drain a request in the middle of a link, drain the | |
6079 | * head request and the next request/link after the current link. | |
6080 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6081 | * maintained for every request of our link. | |
6082 | */ | |
6083 | if (ctx->drain_next) { | |
6084 | req->flags |= REQ_F_IO_DRAIN; | |
6085 | ctx->drain_next = false; | |
6086 | } | |
6087 | /* not interested in head, start from the first linked */ | |
6088 | io_for_each_link(pos, req->link) { | |
6089 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6090 | ctx->drain_next = true; | |
6091 | req->flags |= REQ_F_IO_DRAIN; | |
6092 | break; | |
6093 | } | |
6094 | } | |
6095 | ||
9d858b21 | 6096 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 | 6097 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 PB |
6098 | !(req->flags & REQ_F_IO_DRAIN))) { |
6099 | ctx->drain_active = false; | |
76cc33d7 | 6100 | return false; |
10c66904 | 6101 | } |
9cf7c104 PB |
6102 | |
6103 | seq = io_get_sequence(req); | |
6104 | /* Still a chance to pass the sequence check */ | |
6105 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6106 | return false; |
de0617e4 | 6107 | |
b7e298d2 | 6108 | ret = io_req_prep_async(req); |
be7053b7 | 6109 | if (ret) |
1b48773f | 6110 | goto fail; |
cbdcb435 | 6111 | io_prep_async_link(req); |
27dc8338 | 6112 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6113 | if (!de) { |
1b48773f PB |
6114 | ret = -ENOMEM; |
6115 | fail: | |
6116 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6117 | return true; |
6118 | } | |
2d28390a | 6119 | |
79ebeaee | 6120 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6121 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6122 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6123 | kfree(de); |
f237c30a | 6124 | io_queue_async_work(req, NULL); |
76cc33d7 | 6125 | return true; |
de0617e4 JA |
6126 | } |
6127 | ||
915967f6 | 6128 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6129 | de->req = req; |
9cf7c104 | 6130 | de->seq = seq; |
27dc8338 | 6131 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6132 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6133 | return true; |
de0617e4 JA |
6134 | } |
6135 | ||
68fb8979 | 6136 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6137 | { |
0e1b6fe3 PB |
6138 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6139 | switch (req->opcode) { | |
6140 | case IORING_OP_READV: | |
6141 | case IORING_OP_READ_FIXED: | |
6142 | case IORING_OP_READ: | |
bcda7baa | 6143 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6144 | break; |
6145 | case IORING_OP_RECVMSG: | |
6146 | case IORING_OP_RECV: | |
bcda7baa | 6147 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6148 | break; |
6149 | } | |
99bc4c38 PB |
6150 | } |
6151 | ||
0e1b6fe3 PB |
6152 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6153 | switch (req->opcode) { | |
6154 | case IORING_OP_READV: | |
6155 | case IORING_OP_READ_FIXED: | |
6156 | case IORING_OP_READ: | |
6157 | case IORING_OP_WRITEV: | |
6158 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6159 | case IORING_OP_WRITE: { |
6160 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6161 | |
6162 | kfree(io->free_iovec); | |
0e1b6fe3 | 6163 | break; |
e8c2bc1f | 6164 | } |
0e1b6fe3 | 6165 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6166 | case IORING_OP_SENDMSG: { |
6167 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6168 | |
6169 | kfree(io->free_iov); | |
0e1b6fe3 | 6170 | break; |
e8c2bc1f | 6171 | } |
0e1b6fe3 PB |
6172 | case IORING_OP_SPLICE: |
6173 | case IORING_OP_TEE: | |
e1d767f0 PB |
6174 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6175 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6176 | break; |
f3cd4850 JA |
6177 | case IORING_OP_OPENAT: |
6178 | case IORING_OP_OPENAT2: | |
6179 | if (req->open.filename) | |
6180 | putname(req->open.filename); | |
6181 | break; | |
80a261fd JA |
6182 | case IORING_OP_RENAMEAT: |
6183 | putname(req->rename.oldpath); | |
6184 | putname(req->rename.newpath); | |
6185 | break; | |
14a1143b JA |
6186 | case IORING_OP_UNLINKAT: |
6187 | putname(req->unlink.filename); | |
6188 | break; | |
0e1b6fe3 | 6189 | } |
99bc4c38 | 6190 | } |
75652a30 JA |
6191 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6192 | kfree(req->apoll->double_poll); | |
6193 | kfree(req->apoll); | |
6194 | req->apoll = NULL; | |
6195 | } | |
3a0a6902 PB |
6196 | if (req->flags & REQ_F_INFLIGHT) { |
6197 | struct io_uring_task *tctx = req->task->io_uring; | |
6198 | ||
6199 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6200 | } |
c854357b | 6201 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6202 | put_cred(req->creds); |
c854357b PB |
6203 | |
6204 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6205 | } |
6206 | ||
889fca73 | 6207 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6208 | { |
a197f664 | 6209 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6210 | const struct cred *creds = NULL; |
d625c6ee | 6211 | int ret; |
2b188cc1 | 6212 | |
b8e64b53 | 6213 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6214 | creds = override_creds(req->creds); |
5730b27e | 6215 | |
d625c6ee | 6216 | switch (req->opcode) { |
2b188cc1 | 6217 | case IORING_OP_NOP: |
889fca73 | 6218 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6219 | break; |
6220 | case IORING_OP_READV: | |
edafccee | 6221 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6222 | case IORING_OP_READ: |
889fca73 | 6223 | ret = io_read(req, issue_flags); |
edafccee | 6224 | break; |
3529d8c2 | 6225 | case IORING_OP_WRITEV: |
edafccee | 6226 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6227 | case IORING_OP_WRITE: |
889fca73 | 6228 | ret = io_write(req, issue_flags); |
2b188cc1 | 6229 | break; |
c992fe29 | 6230 | case IORING_OP_FSYNC: |
45d189c6 | 6231 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6232 | break; |
221c5eb2 | 6233 | case IORING_OP_POLL_ADD: |
61e98203 | 6234 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6235 | break; |
6236 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6237 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6238 | break; |
5d17b4a4 | 6239 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6240 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6241 | break; |
0fa03c62 | 6242 | case IORING_OP_SENDMSG: |
889fca73 | 6243 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6244 | break; |
fddaface | 6245 | case IORING_OP_SEND: |
889fca73 | 6246 | ret = io_send(req, issue_flags); |
0fa03c62 | 6247 | break; |
aa1fa28f | 6248 | case IORING_OP_RECVMSG: |
889fca73 | 6249 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6250 | break; |
fddaface | 6251 | case IORING_OP_RECV: |
889fca73 | 6252 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6253 | break; |
5262f567 | 6254 | case IORING_OP_TIMEOUT: |
61e98203 | 6255 | ret = io_timeout(req, issue_flags); |
5262f567 | 6256 | break; |
11365043 | 6257 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6258 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6259 | break; |
17f2fe35 | 6260 | case IORING_OP_ACCEPT: |
889fca73 | 6261 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6262 | break; |
f8e85cf2 | 6263 | case IORING_OP_CONNECT: |
889fca73 | 6264 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6265 | break; |
62755e35 | 6266 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6267 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6268 | break; |
d63d1b5e | 6269 | case IORING_OP_FALLOCATE: |
45d189c6 | 6270 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6271 | break; |
15b71abe | 6272 | case IORING_OP_OPENAT: |
45d189c6 | 6273 | ret = io_openat(req, issue_flags); |
15b71abe | 6274 | break; |
b5dba59e | 6275 | case IORING_OP_CLOSE: |
889fca73 | 6276 | ret = io_close(req, issue_flags); |
b5dba59e | 6277 | break; |
05f3fb3c | 6278 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6279 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6280 | break; |
eddc7ef5 | 6281 | case IORING_OP_STATX: |
45d189c6 | 6282 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6283 | break; |
4840e418 | 6284 | case IORING_OP_FADVISE: |
45d189c6 | 6285 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6286 | break; |
c1ca757b | 6287 | case IORING_OP_MADVISE: |
45d189c6 | 6288 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6289 | break; |
cebdb986 | 6290 | case IORING_OP_OPENAT2: |
45d189c6 | 6291 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6292 | break; |
3e4827b0 | 6293 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6294 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6295 | break; |
7d67af2c | 6296 | case IORING_OP_SPLICE: |
45d189c6 | 6297 | ret = io_splice(req, issue_flags); |
7d67af2c | 6298 | break; |
ddf0322d | 6299 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6300 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6301 | break; |
067524e9 | 6302 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6303 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6304 | break; |
f2a8d5c7 | 6305 | case IORING_OP_TEE: |
45d189c6 | 6306 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6307 | break; |
36f4fa68 | 6308 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6309 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6310 | break; |
80a261fd | 6311 | case IORING_OP_RENAMEAT: |
45d189c6 | 6312 | ret = io_renameat(req, issue_flags); |
80a261fd | 6313 | break; |
14a1143b | 6314 | case IORING_OP_UNLINKAT: |
45d189c6 | 6315 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6316 | break; |
2b188cc1 JA |
6317 | default: |
6318 | ret = -EINVAL; | |
6319 | break; | |
6320 | } | |
6321 | ||
5730b27e JA |
6322 | if (creds) |
6323 | revert_creds(creds); | |
def596e9 JA |
6324 | if (ret) |
6325 | return ret; | |
b532576e | 6326 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6327 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6328 | io_iopoll_req_issued(req); | |
def596e9 JA |
6329 | |
6330 | return 0; | |
2b188cc1 JA |
6331 | } |
6332 | ||
ebc11b6c PB |
6333 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6334 | { | |
6335 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6336 | ||
6337 | req = io_put_req_find_next(req); | |
6338 | return req ? &req->work : NULL; | |
6339 | } | |
6340 | ||
5280f7e5 | 6341 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6342 | { |
6343 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6344 | struct io_kiocb *timeout; |
561fb04a | 6345 | int ret = 0; |
2b188cc1 | 6346 | |
48dcd38d PB |
6347 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6348 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6349 | __io_req_set_refcount(req, 2); | |
6350 | else | |
6351 | req_ref_get(req); | |
5d5901a3 | 6352 | |
6df1db6b PB |
6353 | timeout = io_prep_linked_timeout(req); |
6354 | if (timeout) | |
6355 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6356 | |
4014d943 | 6357 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6358 | ret = -ECANCELED; |
31b51510 | 6359 | |
561fb04a | 6360 | if (!ret) { |
561fb04a | 6361 | do { |
889fca73 | 6362 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6363 | /* |
6364 | * We can get EAGAIN for polled IO even though we're | |
6365 | * forcing a sync submission from here, since we can't | |
6366 | * wait for request slots on the block side. | |
6367 | */ | |
6368 | if (ret != -EAGAIN) | |
6369 | break; | |
6370 | cond_resched(); | |
6371 | } while (1); | |
6372 | } | |
31b51510 | 6373 | |
a3df7698 | 6374 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6375 | if (ret) |
a3df7698 | 6376 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6377 | } |
6378 | ||
aeca241b | 6379 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6380 | unsigned i) |
65e19f54 | 6381 | { |
042b0d85 | 6382 | return &table->files[i]; |
dafecf19 PB |
6383 | } |
6384 | ||
65e19f54 JA |
6385 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6386 | int index) | |
6387 | { | |
aeca241b | 6388 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6389 | |
a04b0ac0 | 6390 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6391 | } |
6392 | ||
a04b0ac0 | 6393 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6394 | { |
6395 | unsigned long file_ptr = (unsigned long) file; | |
6396 | ||
b191e2df | 6397 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6398 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6399 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6400 | file_ptr |= FFS_ASYNC_WRITE; |
6401 | if (S_ISREG(file_inode(file)->i_mode)) | |
6402 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6403 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6404 | } |
6405 | ||
ac177053 PB |
6406 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6407 | struct io_kiocb *req, int fd) | |
09bb8394 | 6408 | { |
8da11c19 | 6409 | struct file *file; |
ac177053 | 6410 | unsigned long file_ptr; |
09bb8394 | 6411 | |
ac177053 PB |
6412 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6413 | return NULL; | |
6414 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6415 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6416 | file = (struct file *) (file_ptr & FFS_MASK); | |
6417 | file_ptr &= ~FFS_MASK; | |
6418 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6419 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6420 | io_req_set_rsrc_node(req); |
6421 | return file; | |
6422 | } | |
d44f554e | 6423 | |
ac177053 | 6424 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6425 | struct io_kiocb *req, int fd) |
6426 | { | |
62906e89 | 6427 | struct file *file = fget(fd); |
ac177053 PB |
6428 | |
6429 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6430 | |
ac177053 PB |
6431 | /* we don't allow fixed io_uring files */ |
6432 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6433 | io_req_track_inflight(req); | |
8371adf5 | 6434 | return file; |
09bb8394 JA |
6435 | } |
6436 | ||
ac177053 | 6437 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6438 | struct io_kiocb *req, int fd, bool fixed) |
6439 | { | |
6440 | if (fixed) | |
6441 | return io_file_get_fixed(ctx, req, fd); | |
6442 | else | |
62906e89 | 6443 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6444 | } |
6445 | ||
f237c30a | 6446 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6447 | { |
6448 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6449 | int ret; |
89b263f6 JA |
6450 | |
6451 | if (prev) { | |
8cb01fac | 6452 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6453 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6454 | io_put_req(prev); |
89b263f6 JA |
6455 | } else { |
6456 | io_req_complete_post(req, -ETIME, 0); | |
6457 | } | |
6458 | } | |
6459 | ||
2665abfd | 6460 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6461 | { |
ad8a48ac JA |
6462 | struct io_timeout_data *data = container_of(timer, |
6463 | struct io_timeout_data, timer); | |
90cd7e42 | 6464 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6465 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6466 | unsigned long flags; |
2665abfd | 6467 | |
89b263f6 | 6468 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6469 | prev = req->timeout.head; |
6470 | req->timeout.head = NULL; | |
2665abfd JA |
6471 | |
6472 | /* | |
6473 | * We don't expect the list to be empty, that will only happen if we | |
6474 | * race with the completion of the linked work. | |
6475 | */ | |
447c19f3 | 6476 | if (prev) { |
f2f87370 | 6477 | io_remove_next_linked(prev); |
447c19f3 PB |
6478 | if (!req_ref_inc_not_zero(prev)) |
6479 | prev = NULL; | |
6480 | } | |
89b263f6 JA |
6481 | req->timeout.prev = prev; |
6482 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6483 | |
89b263f6 JA |
6484 | req->io_task_work.func = io_req_task_link_timeout; |
6485 | io_req_task_work_add(req); | |
2665abfd JA |
6486 | return HRTIMER_NORESTART; |
6487 | } | |
6488 | ||
de968c18 | 6489 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6490 | { |
de968c18 PB |
6491 | struct io_ring_ctx *ctx = req->ctx; |
6492 | ||
89b263f6 | 6493 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6494 | /* |
f2f87370 PB |
6495 | * If the back reference is NULL, then our linked request finished |
6496 | * before we got a chance to setup the timer | |
76a46e06 | 6497 | */ |
90cd7e42 | 6498 | if (req->timeout.head) { |
e8c2bc1f | 6499 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6500 | |
ad8a48ac JA |
6501 | data->timer.function = io_link_timeout_fn; |
6502 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6503 | data->mode); | |
2665abfd | 6504 | } |
89b263f6 | 6505 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6506 | /* drop submission reference */ |
76a46e06 JA |
6507 | io_put_req(req); |
6508 | } | |
2665abfd | 6509 | |
c5eef2b9 | 6510 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6511 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6512 | { |
906c6caa | 6513 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6514 | int ret; |
2b188cc1 | 6515 | |
59b735ae | 6516 | issue_sqe: |
c5eef2b9 | 6517 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6518 | |
491381ce JA |
6519 | /* |
6520 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6521 | * doesn't support non-blocking read/write attempts | |
6522 | */ | |
1840038e | 6523 | if (likely(!ret)) { |
e342c807 | 6524 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6525 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6526 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6527 | |
cd0ca2e0 PB |
6528 | state->compl_reqs[state->compl_nr++] = req; |
6529 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6530 | io_submit_flush_completions(ctx); |
906c6caa | 6531 | return; |
0d63c148 | 6532 | } |
906c6caa PB |
6533 | |
6534 | linked_timeout = io_prep_linked_timeout(req); | |
6535 | if (linked_timeout) | |
6536 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6537 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
906c6caa PB |
6538 | linked_timeout = io_prep_linked_timeout(req); |
6539 | ||
59b735ae OL |
6540 | switch (io_arm_poll_handler(req)) { |
6541 | case IO_APOLL_READY: | |
906c6caa PB |
6542 | if (linked_timeout) |
6543 | io_unprep_linked_timeout(req); | |
59b735ae OL |
6544 | goto issue_sqe; |
6545 | case IO_APOLL_ABORTED: | |
1840038e PB |
6546 | /* |
6547 | * Queued up for async execution, worker will release | |
6548 | * submit reference when the iocb is actually submitted. | |
6549 | */ | |
f237c30a | 6550 | io_queue_async_work(req, NULL); |
59b735ae | 6551 | break; |
1840038e | 6552 | } |
906c6caa PB |
6553 | |
6554 | if (linked_timeout) | |
6555 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 | 6556 | } else { |
f41db273 | 6557 | io_req_complete_failed(req, ret); |
9e645e11 | 6558 | } |
2b188cc1 JA |
6559 | } |
6560 | ||
441b8a78 | 6561 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6562 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6563 | { |
10c66904 | 6564 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6565 | return; |
4fe2c963 | 6566 | |
76cc33d7 | 6567 | if (likely(!(req->flags & REQ_F_FORCE_ASYNC))) { |
c5eef2b9 | 6568 | __io_queue_sqe(req); |
76cc33d7 PB |
6569 | } else { |
6570 | int ret = io_req_prep_async(req); | |
6571 | ||
6572 | if (unlikely(ret)) | |
6573 | io_req_complete_failed(req, ret); | |
6574 | else | |
f237c30a | 6575 | io_queue_async_work(req, NULL); |
ce35a47a | 6576 | } |
4fe2c963 JL |
6577 | } |
6578 | ||
b16fed66 PB |
6579 | /* |
6580 | * Check SQE restrictions (opcode and flags). | |
6581 | * | |
6582 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6583 | */ | |
6584 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6585 | struct io_kiocb *req, | |
6586 | unsigned int sqe_flags) | |
4fe2c963 | 6587 | { |
4cfb25bf | 6588 | if (likely(!ctx->restricted)) |
b16fed66 PB |
6589 | return true; |
6590 | ||
6591 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6592 | return false; | |
6593 | ||
6594 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6595 | ctx->restrictions.sqe_flags_required) | |
6596 | return false; | |
6597 | ||
6598 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6599 | ctx->restrictions.sqe_flags_required)) | |
6600 | return false; | |
6601 | ||
6602 | return true; | |
4fe2c963 JL |
6603 | } |
6604 | ||
b16fed66 PB |
6605 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6606 | const struct io_uring_sqe *sqe) | |
282cdc86 | 6607 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
6608 | { |
6609 | struct io_submit_state *state; | |
6610 | unsigned int sqe_flags; | |
003e8dcc | 6611 | int personality, ret = 0; |
b16fed66 | 6612 | |
864ea921 | 6613 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6614 | req->opcode = READ_ONCE(sqe->opcode); |
6615 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6616 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6617 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6618 | req->file = NULL; |
b16fed66 | 6619 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 6620 | req->task = current; |
b16fed66 PB |
6621 | |
6622 | /* enforce forwards compatibility on users */ | |
dddca226 | 6623 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 6624 | return -EINVAL; |
b16fed66 PB |
6625 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6626 | return -EINVAL; | |
4cfb25bf | 6627 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
6628 | return -EACCES; |
6629 | ||
6630 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6631 | !io_op_defs[req->opcode].buffer_select) | |
6632 | return -EOPNOTSUPP; | |
3c19966d PB |
6633 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
6634 | ctx->drain_active = true; | |
863e0560 | 6635 | |
003e8dcc JA |
6636 | personality = READ_ONCE(sqe->personality); |
6637 | if (personality) { | |
c10d1f98 PB |
6638 | req->creds = xa_load(&ctx->personalities, personality); |
6639 | if (!req->creds) | |
003e8dcc | 6640 | return -EINVAL; |
c10d1f98 | 6641 | get_cred(req->creds); |
b8e64b53 | 6642 | req->flags |= REQ_F_CREDS; |
003e8dcc | 6643 | } |
b16fed66 PB |
6644 | state = &ctx->submit_state; |
6645 | ||
6646 | /* | |
6647 | * Plug now if we have more than 1 IO left after this, and the target | |
6648 | * is potentially a read/write to block based storage. | |
6649 | */ | |
6650 | if (!state->plug_started && state->ios_left > 1 && | |
6651 | io_op_defs[req->opcode].plug) { | |
6652 | blk_start_plug(&state->plug); | |
6653 | state->plug_started = true; | |
6654 | } | |
6655 | ||
6656 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 6657 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 6658 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
6659 | if (unlikely(!req->file)) |
6660 | ret = -EBADF; | |
6661 | } | |
6662 | ||
6663 | state->ios_left--; | |
6664 | return ret; | |
6665 | } | |
6666 | ||
a6b8cadc | 6667 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6668 | const struct io_uring_sqe *sqe) |
282cdc86 | 6669 | __must_hold(&ctx->uring_lock) |
9e645e11 | 6670 | { |
a1ab7b35 | 6671 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6672 | int ret; |
9e645e11 | 6673 | |
a6b8cadc PB |
6674 | ret = io_init_req(ctx, req, sqe); |
6675 | if (unlikely(ret)) { | |
6676 | fail_req: | |
de59bc10 PB |
6677 | if (link->head) { |
6678 | /* fail even hard links since we don't submit */ | |
93d2bcd2 | 6679 | req_set_fail(link->head); |
f41db273 | 6680 | io_req_complete_failed(link->head, -ECANCELED); |
de59bc10 PB |
6681 | link->head = NULL; |
6682 | } | |
f41db273 | 6683 | io_req_complete_failed(req, ret); |
a6b8cadc PB |
6684 | return ret; |
6685 | } | |
441b8a78 | 6686 | |
be7053b7 PB |
6687 | ret = io_req_prep(req, sqe); |
6688 | if (unlikely(ret)) | |
6689 | goto fail_req; | |
a6b8cadc | 6690 | |
be7053b7 | 6691 | /* don't need @sqe from now on */ |
236daeae OL |
6692 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
6693 | req->flags, true, | |
6694 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 6695 | |
9e645e11 JA |
6696 | /* |
6697 | * If we already have a head request, queue this one for async | |
6698 | * submittal once the head completes. If we don't have a head but | |
6699 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6700 | * submitted sync once the chain is complete. If none of those | |
6701 | * conditions are true (normal request), then just queue it. | |
6702 | */ | |
863e0560 PB |
6703 | if (link->head) { |
6704 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6705 | |
b7e298d2 | 6706 | ret = io_req_prep_async(req); |
cf109604 | 6707 | if (unlikely(ret)) |
a6b8cadc | 6708 | goto fail_req; |
9d76377f | 6709 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6710 | link->last->link = req; |
863e0560 | 6711 | link->last = req; |
32fe525b PB |
6712 | |
6713 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6714 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 6715 | link->head = NULL; |
5e159204 | 6716 | io_queue_sqe(head); |
32fe525b | 6717 | } |
9e645e11 | 6718 | } else { |
ef4ff581 | 6719 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6720 | link->head = req; |
6721 | link->last = req; | |
711be031 | 6722 | } else { |
be7053b7 | 6723 | io_queue_sqe(req); |
711be031 | 6724 | } |
9e645e11 | 6725 | } |
2e6e1fde | 6726 | |
1d4240cc | 6727 | return 0; |
9e645e11 JA |
6728 | } |
6729 | ||
9a56a232 JA |
6730 | /* |
6731 | * Batched submission is done, ensure local IO is flushed out. | |
6732 | */ | |
ba88ff11 PB |
6733 | static void io_submit_state_end(struct io_submit_state *state, |
6734 | struct io_ring_ctx *ctx) | |
9a56a232 | 6735 | { |
a1ab7b35 | 6736 | if (state->link.head) |
de59bc10 | 6737 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 6738 | if (state->compl_nr) |
2a2758f2 | 6739 | io_submit_flush_completions(ctx); |
27926b68 JA |
6740 | if (state->plug_started) |
6741 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
6742 | } |
6743 | ||
6744 | /* | |
6745 | * Start submission side cache. | |
6746 | */ | |
6747 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6748 | unsigned int max_ios) |
9a56a232 | 6749 | { |
27926b68 | 6750 | state->plug_started = false; |
9a56a232 | 6751 | state->ios_left = max_ios; |
a1ab7b35 PB |
6752 | /* set only head, no need to init link_last in advance */ |
6753 | state->link.head = NULL; | |
9a56a232 JA |
6754 | } |
6755 | ||
2b188cc1 JA |
6756 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6757 | { | |
75b28aff | 6758 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6759 | |
caf582c6 PB |
6760 | /* |
6761 | * Ensure any loads from the SQEs are done at this point, | |
6762 | * since once we write the new head, the application could | |
6763 | * write new data to them. | |
6764 | */ | |
6765 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6766 | } |
6767 | ||
2b188cc1 | 6768 | /* |
dd9ae8a0 | 6769 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
6770 | * that is mapped by userspace. This means that care needs to be taken to |
6771 | * ensure that reads are stable, as we cannot rely on userspace always | |
6772 | * being a good citizen. If members of the sqe are validated and then later | |
6773 | * used, it's important that those reads are done through READ_ONCE() to | |
6774 | * prevent a re-load down the line. | |
6775 | */ | |
709b302f | 6776 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6777 | { |
ea5ab3b5 | 6778 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 6779 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
6780 | |
6781 | /* | |
6782 | * The cached sq head (or cq tail) serves two purposes: | |
6783 | * | |
6784 | * 1) allows us to batch the cost of updating the user visible | |
6785 | * head updates. | |
6786 | * 2) allows the kernel side to track the head on its own, even | |
6787 | * though the application is the one updating it. | |
6788 | */ | |
17d3aeb3 | 6789 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
6790 | if (likely(head < ctx->sq_entries)) |
6791 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6792 | |
6793 | /* drop invalid entries */ | |
15641e42 PB |
6794 | ctx->cq_extra--; |
6795 | WRITE_ONCE(ctx->rings->sq_dropped, | |
6796 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
6797 | return NULL; |
6798 | } | |
6799 | ||
0f212204 | 6800 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 6801 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 6802 | { |
09899b19 | 6803 | struct io_uring_task *tctx; |
46c4e16a | 6804 | int submitted = 0; |
6c271ce2 | 6805 | |
ee7d46d9 PB |
6806 | /* make sure SQ entry isn't read before tail */ |
6807 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
6808 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6809 | return -EAGAIN; | |
6c271ce2 | 6810 | |
09899b19 PB |
6811 | tctx = current->io_uring; |
6812 | tctx->cached_refs -= nr; | |
6813 | if (unlikely(tctx->cached_refs < 0)) { | |
6814 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
6815 | ||
6816 | percpu_counter_add(&tctx->inflight, refill); | |
6817 | refcount_add(refill, ¤t->usage); | |
6818 | tctx->cached_refs += refill; | |
6819 | } | |
ba88ff11 | 6820 | io_submit_state_start(&ctx->submit_state, nr); |
b14cca0c | 6821 | |
46c4e16a | 6822 | while (submitted < nr) { |
3529d8c2 | 6823 | const struct io_uring_sqe *sqe; |
196be95c | 6824 | struct io_kiocb *req; |
fb5ccc98 | 6825 | |
258b29a9 | 6826 | req = io_alloc_req(ctx); |
196be95c PB |
6827 | if (unlikely(!req)) { |
6828 | if (!submitted) | |
6829 | submitted = -EAGAIN; | |
fb5ccc98 | 6830 | break; |
196be95c | 6831 | } |
4fccfcbb PB |
6832 | sqe = io_get_sqe(ctx); |
6833 | if (unlikely(!sqe)) { | |
6834 | kmem_cache_free(req_cachep, req); | |
6835 | break; | |
6836 | } | |
d3656344 JA |
6837 | /* will complete beyond this point, count as submitted */ |
6838 | submitted++; | |
a1ab7b35 | 6839 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6840 | break; |
6c271ce2 JA |
6841 | } |
6842 | ||
9466f437 PB |
6843 | if (unlikely(submitted != nr)) { |
6844 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 6845 | int unused = nr - ref_used; |
9466f437 | 6846 | |
09899b19 | 6847 | current->io_uring->cached_refs += unused; |
d8a6df10 | 6848 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 6849 | } |
6c271ce2 | 6850 | |
a1ab7b35 | 6851 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6852 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6853 | io_commit_sqring(ctx); | |
6854 | ||
6c271ce2 JA |
6855 | return submitted; |
6856 | } | |
6857 | ||
e4b6d902 PB |
6858 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
6859 | { | |
6860 | return READ_ONCE(sqd->state); | |
6861 | } | |
6862 | ||
23b3628e XW |
6863 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6864 | { | |
6865 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 6866 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6867 | WRITE_ONCE(ctx->rings->sq_flags, |
6868 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6869 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6870 | } |
6871 | ||
6872 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6873 | { | |
79ebeaee | 6874 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6875 | WRITE_ONCE(ctx->rings->sq_flags, |
6876 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6877 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6878 | } |
6879 | ||
08369246 | 6880 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6881 | { |
c8d1ba58 | 6882 | unsigned int to_submit; |
bdcd3eab | 6883 | int ret = 0; |
6c271ce2 | 6884 | |
c8d1ba58 | 6885 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 6886 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
6887 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
6888 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 6889 | |
906a3c6f | 6890 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 6891 | unsigned nr_events = 0; |
948e1947 PB |
6892 | const struct cred *creds = NULL; |
6893 | ||
6894 | if (ctx->sq_creds != current_cred()) | |
6895 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 6896 | |
c8d1ba58 | 6897 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 6898 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 6899 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 6900 | |
3b763ba1 PB |
6901 | /* |
6902 | * Don't submit if refs are dying, good for io_uring_register(), | |
6903 | * but also it is relied upon by io_ring_exit_work() | |
6904 | */ | |
0298ef96 PB |
6905 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
6906 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 6907 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 6908 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 6909 | |
acfb381d PB |
6910 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
6911 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
6912 | if (creds) |
6913 | revert_creds(creds); | |
acfb381d | 6914 | } |
6c271ce2 | 6915 | |
08369246 XW |
6916 | return ret; |
6917 | } | |
6c271ce2 | 6918 | |
08369246 XW |
6919 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
6920 | { | |
6921 | struct io_ring_ctx *ctx; | |
6922 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 6923 | |
c9dca27d PB |
6924 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
6925 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 6926 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 6927 | } |
6c271ce2 | 6928 | |
e4b6d902 PB |
6929 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
6930 | { | |
6931 | bool did_sig = false; | |
6932 | struct ksignal ksig; | |
6933 | ||
6934 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
6935 | signal_pending(current)) { | |
6936 | mutex_unlock(&sqd->lock); | |
6937 | if (signal_pending(current)) | |
6938 | did_sig = get_signal(&ksig); | |
6939 | cond_resched(); | |
6940 | mutex_lock(&sqd->lock); | |
6941 | } | |
e4b6d902 PB |
6942 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
6943 | } | |
6944 | ||
c8d1ba58 JA |
6945 | static int io_sq_thread(void *data) |
6946 | { | |
69fb2131 JA |
6947 | struct io_sq_data *sqd = data; |
6948 | struct io_ring_ctx *ctx; | |
a0d9205f | 6949 | unsigned long timeout = 0; |
37d1e2e3 | 6950 | char buf[TASK_COMM_LEN]; |
08369246 | 6951 | DEFINE_WAIT(wait); |
6c271ce2 | 6952 | |
696ee88a | 6953 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 6954 | set_task_comm(current, buf); |
37d1e2e3 JA |
6955 | |
6956 | if (sqd->sq_cpu != -1) | |
6957 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
6958 | else | |
6959 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
6960 | current->flags |= PF_NO_SETAFFINITY; | |
6961 | ||
09a6f4ef | 6962 | mutex_lock(&sqd->lock); |
e4b6d902 | 6963 | while (1) { |
1a924a80 | 6964 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 6965 | |
e4b6d902 PB |
6966 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
6967 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 6968 | break; |
08369246 XW |
6969 | timeout = jiffies + sqd->sq_thread_idle; |
6970 | } | |
e4b6d902 | 6971 | |
e95eee2d | 6972 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 6973 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 6974 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 6975 | |
08369246 XW |
6976 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
6977 | sqt_spin = true; | |
69fb2131 | 6978 | } |
dd432ea5 PB |
6979 | if (io_run_task_work()) |
6980 | sqt_spin = true; | |
6c271ce2 | 6981 | |
08369246 | 6982 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 6983 | cond_resched(); |
08369246 XW |
6984 | if (sqt_spin) |
6985 | timeout = jiffies + sqd->sq_thread_idle; | |
6986 | continue; | |
6987 | } | |
6988 | ||
08369246 | 6989 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 6990 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
6991 | bool needs_sched = true; |
6992 | ||
724cb4f9 | 6993 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
6994 | io_ring_set_wakeup_flag(ctx); |
6995 | ||
724cb4f9 HX |
6996 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
6997 | !list_empty_careful(&ctx->iopoll_list)) { | |
6998 | needs_sched = false; | |
6999 | break; | |
7000 | } | |
7001 | if (io_sqring_entries(ctx)) { | |
7002 | needs_sched = false; | |
7003 | break; | |
7004 | } | |
7005 | } | |
7006 | ||
7007 | if (needs_sched) { | |
7008 | mutex_unlock(&sqd->lock); | |
7009 | schedule(); | |
7010 | mutex_lock(&sqd->lock); | |
7011 | } | |
69fb2131 JA |
7012 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7013 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7014 | } |
08369246 XW |
7015 | |
7016 | finish_wait(&sqd->wait, &wait); | |
7017 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7018 | } |
28cea78a | 7019 | |
78cc687b | 7020 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7021 | sqd->thread = NULL; |
05962f95 | 7022 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7023 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7024 | io_run_task_work(); |
734551df PB |
7025 | mutex_unlock(&sqd->lock); |
7026 | ||
37d1e2e3 JA |
7027 | complete(&sqd->exited); |
7028 | do_exit(0); | |
6c271ce2 JA |
7029 | } |
7030 | ||
bda52162 JA |
7031 | struct io_wait_queue { |
7032 | struct wait_queue_entry wq; | |
7033 | struct io_ring_ctx *ctx; | |
5fd46178 | 7034 | unsigned cq_tail; |
bda52162 JA |
7035 | unsigned nr_timeouts; |
7036 | }; | |
7037 | ||
6c503150 | 7038 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7039 | { |
7040 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7041 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7042 | |
7043 | /* | |
d195a66e | 7044 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7045 | * started waiting. For timeouts, we always want to return to userspace, |
7046 | * regardless of event count. | |
7047 | */ | |
5fd46178 | 7048 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7049 | } |
7050 | ||
7051 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7052 | int wake_flags, void *key) | |
7053 | { | |
7054 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7055 | wq); | |
7056 | ||
6c503150 PB |
7057 | /* |
7058 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7059 | * the task, and the next invocation will do it. | |
7060 | */ | |
5ed7a37d | 7061 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7062 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7063 | return -1; | |
bda52162 JA |
7064 | } |
7065 | ||
af9c1a44 JA |
7066 | static int io_run_task_work_sig(void) |
7067 | { | |
7068 | if (io_run_task_work()) | |
7069 | return 1; | |
7070 | if (!signal_pending(current)) | |
7071 | return 0; | |
0b8cfa97 | 7072 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7073 | return -ERESTARTSYS; |
af9c1a44 JA |
7074 | return -EINTR; |
7075 | } | |
7076 | ||
eeb60b9a PB |
7077 | /* when returns >0, the caller should retry */ |
7078 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7079 | struct io_wait_queue *iowq, | |
7080 | signed long *timeout) | |
7081 | { | |
7082 | int ret; | |
7083 | ||
7084 | /* make sure we run task_work before checking for signals */ | |
7085 | ret = io_run_task_work_sig(); | |
7086 | if (ret || io_should_wake(iowq)) | |
7087 | return ret; | |
7088 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7089 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7090 | return 1; |
7091 | ||
7092 | *timeout = schedule_timeout(*timeout); | |
7093 | return !*timeout ? -ETIME : 1; | |
7094 | } | |
7095 | ||
2b188cc1 JA |
7096 | /* |
7097 | * Wait until events become available, if we don't already have some. The | |
7098 | * application must reap them itself, as they reside on the shared cq ring. | |
7099 | */ | |
7100 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7101 | const sigset_t __user *sig, size_t sigsz, |
7102 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7103 | { |
90291099 | 7104 | struct io_wait_queue iowq; |
75b28aff | 7105 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7106 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7107 | int ret; | |
2b188cc1 | 7108 | |
b41e9852 | 7109 | do { |
90f67366 | 7110 | io_cqring_overflow_flush(ctx); |
6c503150 | 7111 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7112 | return 0; |
4c6e277c | 7113 | if (!io_run_task_work()) |
b41e9852 | 7114 | break; |
b41e9852 | 7115 | } while (1); |
2b188cc1 JA |
7116 | |
7117 | if (sig) { | |
9e75ad5d AB |
7118 | #ifdef CONFIG_COMPAT |
7119 | if (in_compat_syscall()) | |
7120 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7121 | sigsz); |
9e75ad5d AB |
7122 | else |
7123 | #endif | |
b772434b | 7124 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7125 | |
2b188cc1 JA |
7126 | if (ret) |
7127 | return ret; | |
7128 | } | |
7129 | ||
c73ebb68 | 7130 | if (uts) { |
c1d5a224 PB |
7131 | struct timespec64 ts; |
7132 | ||
c73ebb68 HX |
7133 | if (get_timespec64(&ts, uts)) |
7134 | return -EFAULT; | |
7135 | timeout = timespec64_to_jiffies(&ts); | |
7136 | } | |
7137 | ||
90291099 PB |
7138 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7139 | iowq.wq.private = current; | |
7140 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7141 | iowq.ctx = ctx; | |
bda52162 | 7142 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7143 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7144 | |
c826bd7a | 7145 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7146 | do { |
ca0a2651 | 7147 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7148 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7149 | ret = -EBUSY; |
7150 | break; | |
7151 | } | |
311997b3 | 7152 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7153 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7154 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7155 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7156 | cond_resched(); |
eeb60b9a | 7157 | } while (ret > 0); |
bda52162 | 7158 | |
b7db41c9 | 7159 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7160 | |
75b28aff | 7161 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7162 | } |
7163 | ||
9123c8ff | 7164 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7165 | { |
9123c8ff | 7166 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7167 | |
846a4ef2 | 7168 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7169 | kfree(table[i]); |
7170 | kfree(table); | |
7171 | } | |
7172 | ||
7173 | static void **io_alloc_page_table(size_t size) | |
7174 | { | |
7175 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7176 | size_t init_size = size; | |
7177 | void **table; | |
7178 | ||
0bea96f5 | 7179 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7180 | if (!table) |
7181 | return NULL; | |
7182 | ||
7183 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7184 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7185 | |
0bea96f5 | 7186 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7187 | if (!table[i]) { |
7188 | io_free_page_table(table, init_size); | |
7189 | return NULL; | |
7190 | } | |
7191 | size -= this_size; | |
7192 | } | |
7193 | return table; | |
05f3fb3c JA |
7194 | } |
7195 | ||
28a9fe25 | 7196 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7197 | { |
28a9fe25 PB |
7198 | percpu_ref_exit(&ref_node->refs); |
7199 | kfree(ref_node); | |
1642b445 PB |
7200 | } |
7201 | ||
b9bd2bea PB |
7202 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7203 | { | |
7204 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7205 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7206 | unsigned long flags; | |
7207 | bool first_add = false; | |
7208 | ||
7209 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7210 | node->done = true; | |
7211 | ||
7212 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7213 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7214 | struct io_rsrc_node, node); | |
7215 | /* recycle ref nodes in order */ | |
7216 | if (!node->done) | |
7217 | break; | |
7218 | list_del(&node->node); | |
7219 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7220 | } | |
7221 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7222 | ||
7223 | if (first_add) | |
7224 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7225 | } | |
7226 | ||
7227 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7228 | { | |
7229 | struct io_rsrc_node *ref_node; | |
7230 | ||
7231 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7232 | if (!ref_node) | |
7233 | return NULL; | |
7234 | ||
7235 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7236 | 0, GFP_KERNEL)) { | |
7237 | kfree(ref_node); | |
7238 | return NULL; | |
7239 | } | |
7240 | INIT_LIST_HEAD(&ref_node->node); | |
7241 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7242 | ref_node->done = false; | |
7243 | return ref_node; | |
7244 | } | |
7245 | ||
a7f0ed5a PB |
7246 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7247 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7248 | { |
a7f0ed5a PB |
7249 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7250 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7251 | |
a7f0ed5a PB |
7252 | if (data_to_kill) { |
7253 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7254 | |
a7f0ed5a | 7255 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7256 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7257 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7258 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7259 | |
3e942498 | 7260 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7261 | percpu_ref_kill(&rsrc_node->refs); |
7262 | ctx->rsrc_node = NULL; | |
7263 | } | |
6b06314c | 7264 | |
a7f0ed5a PB |
7265 | if (!ctx->rsrc_node) { |
7266 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7267 | ctx->rsrc_backup_node = NULL; | |
7268 | } | |
8bad28d8 HX |
7269 | } |
7270 | ||
a7f0ed5a | 7271 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7272 | { |
7273 | if (ctx->rsrc_backup_node) | |
7274 | return 0; | |
b895c9a6 | 7275 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7276 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7277 | } |
7278 | ||
40ae0ff7 | 7279 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7280 | { |
7281 | int ret; | |
05589553 | 7282 | |
215c3902 | 7283 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7284 | if (data->quiesce) |
7285 | return -ENXIO; | |
05589553 | 7286 | |
8bad28d8 | 7287 | data->quiesce = true; |
1ffc5422 | 7288 | do { |
a7f0ed5a | 7289 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7290 | if (ret) |
f2303b1f | 7291 | break; |
a7f0ed5a | 7292 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7293 | |
3e942498 PB |
7294 | /* kill initial ref, already quiesced if zero */ |
7295 | if (atomic_dec_and_test(&data->refs)) | |
7296 | break; | |
c018db4a | 7297 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7298 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7299 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7300 | if (!ret) { |
7301 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7302 | break; |
c018db4a | 7303 | } |
8bad28d8 | 7304 | |
3e942498 PB |
7305 | atomic_inc(&data->refs); |
7306 | /* wait for all works potentially completing data->done */ | |
7307 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7308 | reinit_completion(&data->done); |
8dd03afe | 7309 | |
1ffc5422 | 7310 | ret = io_run_task_work_sig(); |
8bad28d8 | 7311 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7312 | } while (ret >= 0); |
8bad28d8 | 7313 | data->quiesce = false; |
05f3fb3c | 7314 | |
8bad28d8 | 7315 | return ret; |
d7954b2b BM |
7316 | } |
7317 | ||
2d091d62 PB |
7318 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7319 | { | |
7320 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7321 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7322 | ||
7323 | return &data->tags[table_idx][off]; | |
7324 | } | |
7325 | ||
44b31f2f | 7326 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7327 | { |
2d091d62 PB |
7328 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7329 | ||
7330 | if (data->tags) | |
7331 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7332 | kfree(data); |
7333 | } | |
7334 | ||
d878c816 PB |
7335 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7336 | u64 __user *utags, unsigned nr, | |
7337 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7338 | { |
b895c9a6 | 7339 | struct io_rsrc_data *data; |
2d091d62 | 7340 | int ret = -ENOMEM; |
d878c816 | 7341 | unsigned i; |
1ad555c6 BM |
7342 | |
7343 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7344 | if (!data) | |
d878c816 | 7345 | return -ENOMEM; |
2d091d62 | 7346 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7347 | if (!data->tags) { |
1ad555c6 | 7348 | kfree(data); |
d878c816 PB |
7349 | return -ENOMEM; |
7350 | } | |
2d091d62 PB |
7351 | |
7352 | data->nr = nr; | |
7353 | data->ctx = ctx; | |
7354 | data->do_put = do_put; | |
d878c816 | 7355 | if (utags) { |
2d091d62 | 7356 | ret = -EFAULT; |
d878c816 | 7357 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7358 | u64 *tag_slot = io_get_tag_slot(data, i); |
7359 | ||
7360 | if (copy_from_user(tag_slot, &utags[i], | |
7361 | sizeof(*tag_slot))) | |
2d091d62 | 7362 | goto fail; |
d878c816 | 7363 | } |
1ad555c6 | 7364 | } |
b60c8dce | 7365 | |
3e942498 | 7366 | atomic_set(&data->refs, 1); |
1ad555c6 | 7367 | init_completion(&data->done); |
d878c816 PB |
7368 | *pdata = data; |
7369 | return 0; | |
2d091d62 PB |
7370 | fail: |
7371 | io_rsrc_data_free(data); | |
7372 | return ret; | |
1ad555c6 BM |
7373 | } |
7374 | ||
9123c8ff PB |
7375 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7376 | { | |
0bea96f5 PB |
7377 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7378 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7379 | return !!table->files; |
7380 | } | |
7381 | ||
042b0d85 | 7382 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7383 | { |
042b0d85 | 7384 | kvfree(table->files); |
9123c8ff PB |
7385 | table->files = NULL; |
7386 | } | |
7387 | ||
fff4db76 | 7388 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7389 | { |
fff4db76 PB |
7390 | #if defined(CONFIG_UNIX) |
7391 | if (ctx->ring_sock) { | |
7392 | struct sock *sock = ctx->ring_sock->sk; | |
7393 | struct sk_buff *skb; | |
7394 | ||
7395 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7396 | kfree_skb(skb); | |
7397 | } | |
7398 | #else | |
7399 | int i; | |
7400 | ||
7401 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7402 | struct file *file; | |
7403 | ||
7404 | file = io_file_from_index(ctx, i); | |
7405 | if (file) | |
7406 | fput(file); | |
7407 | } | |
7408 | #endif | |
042b0d85 | 7409 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7410 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7411 | ctx->file_data = NULL; |
7412 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7413 | } |
7414 | ||
d7954b2b BM |
7415 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7416 | { | |
d7954b2b BM |
7417 | int ret; |
7418 | ||
08480400 | 7419 | if (!ctx->file_data) |
d7954b2b | 7420 | return -ENXIO; |
08480400 PB |
7421 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7422 | if (!ret) | |
7423 | __io_sqe_files_unregister(ctx); | |
7424 | return ret; | |
6b06314c JA |
7425 | } |
7426 | ||
37d1e2e3 | 7427 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7428 | __releases(&sqd->lock) |
37d1e2e3 | 7429 | { |
521d6a73 PB |
7430 | WARN_ON_ONCE(sqd->thread == current); |
7431 | ||
9e138a48 PB |
7432 | /* |
7433 | * Do the dance but not conditional clear_bit() because it'd race with | |
7434 | * other threads incrementing park_pending and setting the bit. | |
7435 | */ | |
37d1e2e3 | 7436 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7437 | if (atomic_dec_return(&sqd->park_pending)) |
7438 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7439 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7440 | } |
7441 | ||
86e0d676 | 7442 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7443 | __acquires(&sqd->lock) |
37d1e2e3 | 7444 | { |
521d6a73 PB |
7445 | WARN_ON_ONCE(sqd->thread == current); |
7446 | ||
9e138a48 | 7447 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7448 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7449 | mutex_lock(&sqd->lock); |
05962f95 | 7450 | if (sqd->thread) |
86e0d676 | 7451 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7452 | } |
7453 | ||
7454 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7455 | { | |
521d6a73 | 7456 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7457 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7458 | |
05962f95 | 7459 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7460 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7461 | if (sqd->thread) |
7462 | wake_up_process(sqd->thread); | |
09a6f4ef | 7463 | mutex_unlock(&sqd->lock); |
05962f95 | 7464 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7465 | } |
7466 | ||
534ca6d6 | 7467 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7468 | { |
534ca6d6 | 7469 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7470 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7471 | ||
37d1e2e3 JA |
7472 | io_sq_thread_stop(sqd); |
7473 | kfree(sqd); | |
7474 | } | |
7475 | } | |
7476 | ||
7477 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7478 | { | |
7479 | struct io_sq_data *sqd = ctx->sq_data; | |
7480 | ||
7481 | if (sqd) { | |
05962f95 | 7482 | io_sq_thread_park(sqd); |
521d6a73 | 7483 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7484 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7485 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7486 | |
7487 | io_put_sq_data(sqd); | |
7488 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7489 | } |
7490 | } | |
7491 | ||
aa06165d JA |
7492 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7493 | { | |
7494 | struct io_ring_ctx *ctx_attach; | |
7495 | struct io_sq_data *sqd; | |
7496 | struct fd f; | |
7497 | ||
7498 | f = fdget(p->wq_fd); | |
7499 | if (!f.file) | |
7500 | return ERR_PTR(-ENXIO); | |
7501 | if (f.file->f_op != &io_uring_fops) { | |
7502 | fdput(f); | |
7503 | return ERR_PTR(-EINVAL); | |
7504 | } | |
7505 | ||
7506 | ctx_attach = f.file->private_data; | |
7507 | sqd = ctx_attach->sq_data; | |
7508 | if (!sqd) { | |
7509 | fdput(f); | |
7510 | return ERR_PTR(-EINVAL); | |
7511 | } | |
5c2469e0 JA |
7512 | if (sqd->task_tgid != current->tgid) { |
7513 | fdput(f); | |
7514 | return ERR_PTR(-EPERM); | |
7515 | } | |
aa06165d JA |
7516 | |
7517 | refcount_inc(&sqd->refs); | |
7518 | fdput(f); | |
7519 | return sqd; | |
7520 | } | |
7521 | ||
26984fbf PB |
7522 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7523 | bool *attached) | |
534ca6d6 JA |
7524 | { |
7525 | struct io_sq_data *sqd; | |
7526 | ||
26984fbf | 7527 | *attached = false; |
5c2469e0 JA |
7528 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7529 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7530 | if (!IS_ERR(sqd)) { |
7531 | *attached = true; | |
5c2469e0 | 7532 | return sqd; |
26984fbf | 7533 | } |
5c2469e0 JA |
7534 | /* fall through for EPERM case, setup new sqd/task */ |
7535 | if (PTR_ERR(sqd) != -EPERM) | |
7536 | return sqd; | |
7537 | } | |
aa06165d | 7538 | |
534ca6d6 JA |
7539 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7540 | if (!sqd) | |
7541 | return ERR_PTR(-ENOMEM); | |
7542 | ||
9e138a48 | 7543 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7544 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7545 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7546 | mutex_init(&sqd->lock); |
534ca6d6 | 7547 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7548 | init_completion(&sqd->exited); |
534ca6d6 JA |
7549 | return sqd; |
7550 | } | |
7551 | ||
6b06314c | 7552 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7553 | /* |
7554 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7555 | * the io_uring can be safely unregistered on process exit, even if we have | |
7556 | * loops in the file referencing. | |
7557 | */ | |
7558 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7559 | { | |
7560 | struct sock *sk = ctx->ring_sock->sk; | |
7561 | struct scm_fp_list *fpl; | |
7562 | struct sk_buff *skb; | |
08a45173 | 7563 | int i, nr_files; |
6b06314c | 7564 | |
6b06314c JA |
7565 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7566 | if (!fpl) | |
7567 | return -ENOMEM; | |
7568 | ||
7569 | skb = alloc_skb(0, GFP_KERNEL); | |
7570 | if (!skb) { | |
7571 | kfree(fpl); | |
7572 | return -ENOMEM; | |
7573 | } | |
7574 | ||
7575 | skb->sk = sk; | |
6b06314c | 7576 | |
08a45173 | 7577 | nr_files = 0; |
62e398be | 7578 | fpl->user = get_uid(current_user()); |
6b06314c | 7579 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7580 | struct file *file = io_file_from_index(ctx, i + offset); |
7581 | ||
7582 | if (!file) | |
08a45173 | 7583 | continue; |
65e19f54 | 7584 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7585 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7586 | nr_files++; | |
6b06314c JA |
7587 | } |
7588 | ||
08a45173 JA |
7589 | if (nr_files) { |
7590 | fpl->max = SCM_MAX_FD; | |
7591 | fpl->count = nr_files; | |
7592 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7593 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7594 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7595 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7596 | |
08a45173 JA |
7597 | for (i = 0; i < nr_files; i++) |
7598 | fput(fpl->fp[i]); | |
7599 | } else { | |
7600 | kfree_skb(skb); | |
7601 | kfree(fpl); | |
7602 | } | |
6b06314c JA |
7603 | |
7604 | return 0; | |
7605 | } | |
7606 | ||
7607 | /* | |
7608 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7609 | * causes regular reference counting to break down. We rely on the UNIX | |
7610 | * garbage collection to take care of this problem for us. | |
7611 | */ | |
7612 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7613 | { | |
7614 | unsigned left, total; | |
7615 | int ret = 0; | |
7616 | ||
7617 | total = 0; | |
7618 | left = ctx->nr_user_files; | |
7619 | while (left) { | |
7620 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7621 | |
7622 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7623 | if (ret) | |
7624 | break; | |
7625 | left -= this_files; | |
7626 | total += this_files; | |
7627 | } | |
7628 | ||
7629 | if (!ret) | |
7630 | return 0; | |
7631 | ||
7632 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7633 | struct file *file = io_file_from_index(ctx, total); |
7634 | ||
7635 | if (file) | |
7636 | fput(file); | |
6b06314c JA |
7637 | total++; |
7638 | } | |
7639 | ||
7640 | return ret; | |
7641 | } | |
7642 | #else | |
7643 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7644 | { | |
7645 | return 0; | |
7646 | } | |
7647 | #endif | |
7648 | ||
47e90392 | 7649 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7650 | { |
50238531 | 7651 | struct file *file = prsrc->file; |
05f3fb3c JA |
7652 | #if defined(CONFIG_UNIX) |
7653 | struct sock *sock = ctx->ring_sock->sk; | |
7654 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7655 | struct sk_buff *skb; | |
7656 | int i; | |
7657 | ||
7658 | __skb_queue_head_init(&list); | |
7659 | ||
7660 | /* | |
7661 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7662 | * remove this entry and rearrange the file array. | |
7663 | */ | |
7664 | skb = skb_dequeue(head); | |
7665 | while (skb) { | |
7666 | struct scm_fp_list *fp; | |
7667 | ||
7668 | fp = UNIXCB(skb).fp; | |
7669 | for (i = 0; i < fp->count; i++) { | |
7670 | int left; | |
7671 | ||
7672 | if (fp->fp[i] != file) | |
7673 | continue; | |
7674 | ||
7675 | unix_notinflight(fp->user, fp->fp[i]); | |
7676 | left = fp->count - 1 - i; | |
7677 | if (left) { | |
7678 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7679 | left * sizeof(struct file *)); | |
7680 | } | |
7681 | fp->count--; | |
7682 | if (!fp->count) { | |
7683 | kfree_skb(skb); | |
7684 | skb = NULL; | |
7685 | } else { | |
7686 | __skb_queue_tail(&list, skb); | |
7687 | } | |
7688 | fput(file); | |
7689 | file = NULL; | |
7690 | break; | |
7691 | } | |
7692 | ||
7693 | if (!file) | |
7694 | break; | |
7695 | ||
7696 | __skb_queue_tail(&list, skb); | |
7697 | ||
7698 | skb = skb_dequeue(head); | |
7699 | } | |
7700 | ||
7701 | if (skb_peek(&list)) { | |
7702 | spin_lock_irq(&head->lock); | |
7703 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7704 | __skb_queue_tail(head, skb); | |
7705 | spin_unlock_irq(&head->lock); | |
7706 | } | |
7707 | #else | |
7708 | fput(file); | |
7709 | #endif | |
7710 | } | |
7711 | ||
b895c9a6 | 7712 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7713 | { |
b895c9a6 | 7714 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7715 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7716 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7717 | |
269bbe5f BM |
7718 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7719 | list_del(&prsrc->list); | |
b60c8dce PB |
7720 | |
7721 | if (prsrc->tag) { | |
7722 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
7723 | |
7724 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 7725 | spin_lock(&ctx->completion_lock); |
b60c8dce | 7726 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 7727 | ctx->cq_extra++; |
b60c8dce | 7728 | io_commit_cqring(ctx); |
79ebeaee | 7729 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
7730 | io_cqring_ev_posted(ctx); |
7731 | io_ring_submit_unlock(ctx, lock_ring); | |
7732 | } | |
7733 | ||
40ae0ff7 | 7734 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7735 | kfree(prsrc); |
65e19f54 | 7736 | } |
05589553 | 7737 | |
28a9fe25 | 7738 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
7739 | if (atomic_dec_and_test(&rsrc_data->refs)) |
7740 | complete(&rsrc_data->done); | |
2faf852d | 7741 | } |
65e19f54 | 7742 | |
269bbe5f | 7743 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7744 | { |
7745 | struct io_ring_ctx *ctx; | |
7746 | struct llist_node *node; | |
7747 | ||
269bbe5f BM |
7748 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7749 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7750 | |
7751 | while (node) { | |
b895c9a6 | 7752 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7753 | struct llist_node *next = node->next; |
7754 | ||
b895c9a6 | 7755 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7756 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7757 | node = next; |
7758 | } | |
7759 | } | |
7760 | ||
6b06314c | 7761 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 7762 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
7763 | { |
7764 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7765 | struct file *file; |
f3baed39 | 7766 | int fd, ret; |
846a4ef2 | 7767 | unsigned i; |
6b06314c | 7768 | |
05f3fb3c | 7769 | if (ctx->file_data) |
6b06314c JA |
7770 | return -EBUSY; |
7771 | if (!nr_args) | |
7772 | return -EINVAL; | |
7773 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7774 | return -EMFILE; | |
3a1b8a4e PB |
7775 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
7776 | return -EMFILE; | |
a7f0ed5a | 7777 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7778 | if (ret) |
7779 | return ret; | |
d878c816 PB |
7780 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
7781 | &ctx->file_data); | |
7782 | if (ret) | |
7783 | return ret; | |
6b06314c | 7784 | |
f3baed39 | 7785 | ret = -ENOMEM; |
aeca241b | 7786 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 7787 | goto out_free; |
65e19f54 | 7788 | |
08a45173 | 7789 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 7790 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
7791 | ret = -EFAULT; |
7792 | goto out_fput; | |
7793 | } | |
08a45173 | 7794 | /* allow sparse sets */ |
792e3582 PB |
7795 | if (fd == -1) { |
7796 | ret = -EINVAL; | |
2d091d62 | 7797 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 7798 | goto out_fput; |
08a45173 | 7799 | continue; |
792e3582 | 7800 | } |
6b06314c | 7801 | |
05f3fb3c | 7802 | file = fget(fd); |
6b06314c | 7803 | ret = -EBADF; |
792e3582 | 7804 | if (unlikely(!file)) |
600cf3f8 | 7805 | goto out_fput; |
05f3fb3c | 7806 | |
6b06314c JA |
7807 | /* |
7808 | * Don't allow io_uring instances to be registered. If UNIX | |
7809 | * isn't enabled, then this causes a reference cycle and this | |
7810 | * instance can never get freed. If UNIX is enabled we'll | |
7811 | * handle it just fine, but there's still no point in allowing | |
7812 | * a ring fd as it doesn't support regular read/write anyway. | |
7813 | */ | |
05f3fb3c JA |
7814 | if (file->f_op == &io_uring_fops) { |
7815 | fput(file); | |
600cf3f8 | 7816 | goto out_fput; |
6b06314c | 7817 | } |
aeca241b | 7818 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
7819 | } |
7820 | ||
6b06314c | 7821 | ret = io_sqe_files_scm(ctx); |
05589553 | 7822 | if (ret) { |
08480400 | 7823 | __io_sqe_files_unregister(ctx); |
05589553 XW |
7824 | return ret; |
7825 | } | |
6b06314c | 7826 | |
a7f0ed5a | 7827 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7828 | return ret; |
600cf3f8 PB |
7829 | out_fput: |
7830 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7831 | file = io_file_from_index(ctx, i); | |
7832 | if (file) | |
7833 | fput(file); | |
7834 | } | |
042b0d85 | 7835 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 7836 | ctx->nr_user_files = 0; |
600cf3f8 | 7837 | out_free: |
44b31f2f | 7838 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 7839 | ctx->file_data = NULL; |
6b06314c JA |
7840 | return ret; |
7841 | } | |
7842 | ||
c3a31e60 JA |
7843 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7844 | int index) | |
7845 | { | |
7846 | #if defined(CONFIG_UNIX) | |
7847 | struct sock *sock = ctx->ring_sock->sk; | |
7848 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7849 | struct sk_buff *skb; | |
7850 | ||
7851 | /* | |
7852 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7853 | * file set. If there's no room, fall back to allocating a new skb | |
7854 | * and filling it in. | |
7855 | */ | |
7856 | spin_lock_irq(&head->lock); | |
7857 | skb = skb_peek(head); | |
7858 | if (skb) { | |
7859 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7860 | ||
7861 | if (fpl->count < SCM_MAX_FD) { | |
7862 | __skb_unlink(skb, head); | |
7863 | spin_unlock_irq(&head->lock); | |
7864 | fpl->fp[fpl->count] = get_file(file); | |
7865 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7866 | fpl->count++; | |
7867 | spin_lock_irq(&head->lock); | |
7868 | __skb_queue_head(head, skb); | |
7869 | } else { | |
7870 | skb = NULL; | |
7871 | } | |
7872 | } | |
7873 | spin_unlock_irq(&head->lock); | |
7874 | ||
7875 | if (skb) { | |
7876 | fput(file); | |
7877 | return 0; | |
7878 | } | |
7879 | ||
7880 | return __io_sqe_files_scm(ctx, 1, index); | |
7881 | #else | |
7882 | return 0; | |
7883 | #endif | |
7884 | } | |
7885 | ||
b60c8dce | 7886 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
e7c78371 | 7887 | struct io_rsrc_node *node, void *rsrc) |
05f3fb3c | 7888 | { |
269bbe5f | 7889 | struct io_rsrc_put *prsrc; |
05f3fb3c | 7890 | |
269bbe5f BM |
7891 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
7892 | if (!prsrc) | |
a5318d3c | 7893 | return -ENOMEM; |
05f3fb3c | 7894 | |
2d091d62 | 7895 | prsrc->tag = *io_get_tag_slot(data, idx); |
50238531 | 7896 | prsrc->rsrc = rsrc; |
e7c78371 | 7897 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 7898 | return 0; |
05f3fb3c JA |
7899 | } |
7900 | ||
7901 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
c3bdad02 | 7902 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
7903 | unsigned nr_args) |
7904 | { | |
c3bdad02 | 7905 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 7906 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 7907 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
7908 | struct io_fixed_file *file_slot; |
7909 | struct file *file; | |
98f0b3b4 PB |
7910 | int fd, i, err = 0; |
7911 | unsigned int done; | |
05589553 | 7912 | bool needs_switch = false; |
c3a31e60 | 7913 | |
98f0b3b4 PB |
7914 | if (!ctx->file_data) |
7915 | return -ENXIO; | |
7916 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
7917 | return -EINVAL; |
7918 | ||
67973b93 | 7919 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
7920 | u64 tag = 0; |
7921 | ||
7922 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
7923 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
7924 | err = -EFAULT; |
7925 | break; | |
7926 | } | |
c3bdad02 PB |
7927 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
7928 | err = -EINVAL; | |
7929 | break; | |
7930 | } | |
4e0377a1 | 7931 | if (fd == IORING_REGISTER_FILES_SKIP) |
7932 | continue; | |
7933 | ||
67973b93 | 7934 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 7935 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 7936 | |
a04b0ac0 PB |
7937 | if (file_slot->file_ptr) { |
7938 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
7939 | err = io_queue_rsrc_removal(data, up->offset + done, |
7940 | ctx->rsrc_node, file); | |
a5318d3c HD |
7941 | if (err) |
7942 | break; | |
a04b0ac0 | 7943 | file_slot->file_ptr = 0; |
05589553 | 7944 | needs_switch = true; |
c3a31e60 JA |
7945 | } |
7946 | if (fd != -1) { | |
c3a31e60 JA |
7947 | file = fget(fd); |
7948 | if (!file) { | |
7949 | err = -EBADF; | |
7950 | break; | |
7951 | } | |
7952 | /* | |
7953 | * Don't allow io_uring instances to be registered. If | |
7954 | * UNIX isn't enabled, then this causes a reference | |
7955 | * cycle and this instance can never get freed. If UNIX | |
7956 | * is enabled we'll handle it just fine, but there's | |
7957 | * still no point in allowing a ring fd as it doesn't | |
7958 | * support regular read/write anyway. | |
7959 | */ | |
7960 | if (file->f_op == &io_uring_fops) { | |
7961 | fput(file); | |
7962 | err = -EBADF; | |
7963 | break; | |
7964 | } | |
2d091d62 | 7965 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 7966 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 7967 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 7968 | if (err) { |
a04b0ac0 | 7969 | file_slot->file_ptr = 0; |
f3bd9dae | 7970 | fput(file); |
c3a31e60 | 7971 | break; |
f3bd9dae | 7972 | } |
c3a31e60 | 7973 | } |
05f3fb3c JA |
7974 | } |
7975 | ||
a7f0ed5a PB |
7976 | if (needs_switch) |
7977 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
7978 | return done ? done : err; |
7979 | } | |
05589553 | 7980 | |
685fe7fe JA |
7981 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
7982 | struct task_struct *task) | |
24369c2e | 7983 | { |
e941894e | 7984 | struct io_wq_hash *hash; |
24369c2e | 7985 | struct io_wq_data data; |
24369c2e | 7986 | unsigned int concurrency; |
24369c2e | 7987 | |
362a9e65 | 7988 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
7989 | hash = ctx->hash_map; |
7990 | if (!hash) { | |
7991 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
7992 | if (!hash) { |
7993 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 7994 | return ERR_PTR(-ENOMEM); |
362a9e65 | 7995 | } |
e941894e JA |
7996 | refcount_set(&hash->refs, 1); |
7997 | init_waitqueue_head(&hash->wait); | |
7998 | ctx->hash_map = hash; | |
24369c2e | 7999 | } |
362a9e65 | 8000 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8001 | |
e941894e | 8002 | data.hash = hash; |
685fe7fe | 8003 | data.task = task; |
ebc11b6c | 8004 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8005 | data.do_work = io_wq_submit_work; |
24369c2e | 8006 | |
d25e3a3d JA |
8007 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8008 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8009 | |
5aa75ed5 | 8010 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8011 | } |
8012 | ||
5aa75ed5 JA |
8013 | static int io_uring_alloc_task_context(struct task_struct *task, |
8014 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8015 | { |
8016 | struct io_uring_task *tctx; | |
d8a6df10 | 8017 | int ret; |
0f212204 | 8018 | |
09899b19 | 8019 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8020 | if (unlikely(!tctx)) |
8021 | return -ENOMEM; | |
8022 | ||
d8a6df10 JA |
8023 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8024 | if (unlikely(ret)) { | |
8025 | kfree(tctx); | |
8026 | return ret; | |
8027 | } | |
8028 | ||
685fe7fe | 8029 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8030 | if (IS_ERR(tctx->io_wq)) { |
8031 | ret = PTR_ERR(tctx->io_wq); | |
8032 | percpu_counter_destroy(&tctx->inflight); | |
8033 | kfree(tctx); | |
8034 | return ret; | |
8035 | } | |
8036 | ||
0f212204 JA |
8037 | xa_init(&tctx->xa); |
8038 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8039 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8040 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8041 | task->io_uring = tctx; |
7cbf1722 JA |
8042 | spin_lock_init(&tctx->task_lock); |
8043 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8044 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8045 | return 0; |
8046 | } | |
8047 | ||
8048 | void __io_uring_free(struct task_struct *tsk) | |
8049 | { | |
8050 | struct io_uring_task *tctx = tsk->io_uring; | |
8051 | ||
8052 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8053 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8054 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8055 | |
d8a6df10 | 8056 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8057 | kfree(tctx); |
8058 | tsk->io_uring = NULL; | |
8059 | } | |
8060 | ||
7e84e1c7 SG |
8061 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8062 | struct io_uring_params *p) | |
2b188cc1 JA |
8063 | { |
8064 | int ret; | |
8065 | ||
d25e3a3d JA |
8066 | /* Retain compatibility with failing for an invalid attach attempt */ |
8067 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8068 | IORING_SETUP_ATTACH_WQ) { | |
8069 | struct fd f; | |
8070 | ||
8071 | f = fdget(p->wq_fd); | |
8072 | if (!f.file) | |
8073 | return -ENXIO; | |
0cc936f7 JA |
8074 | if (f.file->f_op != &io_uring_fops) { |
8075 | fdput(f); | |
f2a48dd0 | 8076 | return -EINVAL; |
0cc936f7 JA |
8077 | } |
8078 | fdput(f); | |
d25e3a3d | 8079 | } |
6c271ce2 | 8080 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8081 | struct task_struct *tsk; |
534ca6d6 | 8082 | struct io_sq_data *sqd; |
26984fbf | 8083 | bool attached; |
534ca6d6 | 8084 | |
26984fbf | 8085 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8086 | if (IS_ERR(sqd)) { |
8087 | ret = PTR_ERR(sqd); | |
8088 | goto err; | |
8089 | } | |
69fb2131 | 8090 | |
7c30f36a | 8091 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8092 | ctx->sq_data = sqd; |
917257da JA |
8093 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8094 | if (!ctx->sq_thread_idle) | |
8095 | ctx->sq_thread_idle = HZ; | |
8096 | ||
78d7f6ba | 8097 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8098 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8099 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8100 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8101 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8102 | io_sq_thread_unpark(sqd); |
8103 | ||
de75a3d3 PB |
8104 | if (ret < 0) |
8105 | goto err; | |
8106 | if (attached) | |
5aa75ed5 | 8107 | return 0; |
aa06165d | 8108 | |
6c271ce2 | 8109 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8110 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8111 | |
917257da | 8112 | ret = -EINVAL; |
f2a48dd0 | 8113 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8114 | goto err_sqpoll; |
37d1e2e3 | 8115 | sqd->sq_cpu = cpu; |
6c271ce2 | 8116 | } else { |
37d1e2e3 | 8117 | sqd->sq_cpu = -1; |
6c271ce2 | 8118 | } |
37d1e2e3 JA |
8119 | |
8120 | sqd->task_pid = current->pid; | |
5c2469e0 | 8121 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8122 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8123 | if (IS_ERR(tsk)) { | |
8124 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8125 | goto err_sqpoll; |
6c271ce2 | 8126 | } |
97a73a0f | 8127 | |
46fe18b1 | 8128 | sqd->thread = tsk; |
97a73a0f | 8129 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8130 | wake_up_new_task(tsk); |
0f212204 JA |
8131 | if (ret) |
8132 | goto err; | |
6c271ce2 JA |
8133 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8134 | /* Can't have SQ_AFF without SQPOLL */ | |
8135 | ret = -EINVAL; | |
8136 | goto err; | |
8137 | } | |
8138 | ||
2b188cc1 | 8139 | return 0; |
f2a48dd0 PB |
8140 | err_sqpoll: |
8141 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8142 | err: |
37d1e2e3 | 8143 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8144 | return ret; |
8145 | } | |
8146 | ||
a087e2b5 BM |
8147 | static inline void __io_unaccount_mem(struct user_struct *user, |
8148 | unsigned long nr_pages) | |
2b188cc1 JA |
8149 | { |
8150 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8151 | } | |
8152 | ||
a087e2b5 BM |
8153 | static inline int __io_account_mem(struct user_struct *user, |
8154 | unsigned long nr_pages) | |
2b188cc1 JA |
8155 | { |
8156 | unsigned long page_limit, cur_pages, new_pages; | |
8157 | ||
8158 | /* Don't allow more pages than we can safely lock */ | |
8159 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8160 | ||
8161 | do { | |
8162 | cur_pages = atomic_long_read(&user->locked_vm); | |
8163 | new_pages = cur_pages + nr_pages; | |
8164 | if (new_pages > page_limit) | |
8165 | return -ENOMEM; | |
8166 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8167 | new_pages) != cur_pages); | |
8168 | ||
8169 | return 0; | |
8170 | } | |
8171 | ||
26bfa89e | 8172 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8173 | { |
62e398be | 8174 | if (ctx->user) |
a087e2b5 | 8175 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8176 | |
26bfa89e JA |
8177 | if (ctx->mm_account) |
8178 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8179 | } |
8180 | ||
26bfa89e | 8181 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8182 | { |
30975825 BM |
8183 | int ret; |
8184 | ||
62e398be | 8185 | if (ctx->user) { |
30975825 BM |
8186 | ret = __io_account_mem(ctx->user, nr_pages); |
8187 | if (ret) | |
8188 | return ret; | |
8189 | } | |
8190 | ||
26bfa89e JA |
8191 | if (ctx->mm_account) |
8192 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8193 | |
8194 | return 0; | |
8195 | } | |
8196 | ||
2b188cc1 JA |
8197 | static void io_mem_free(void *ptr) |
8198 | { | |
52e04ef4 MR |
8199 | struct page *page; |
8200 | ||
8201 | if (!ptr) | |
8202 | return; | |
2b188cc1 | 8203 | |
52e04ef4 | 8204 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8205 | if (put_page_testzero(page)) |
8206 | free_compound_page(page); | |
8207 | } | |
8208 | ||
8209 | static void *io_mem_alloc(size_t size) | |
8210 | { | |
8211 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8212 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8213 | |
8214 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8215 | } | |
8216 | ||
75b28aff HV |
8217 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8218 | size_t *sq_offset) | |
8219 | { | |
8220 | struct io_rings *rings; | |
8221 | size_t off, sq_array_size; | |
8222 | ||
8223 | off = struct_size(rings, cqes, cq_entries); | |
8224 | if (off == SIZE_MAX) | |
8225 | return SIZE_MAX; | |
8226 | ||
8227 | #ifdef CONFIG_SMP | |
8228 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8229 | if (off == 0) | |
8230 | return SIZE_MAX; | |
8231 | #endif | |
8232 | ||
b36200f5 DV |
8233 | if (sq_offset) |
8234 | *sq_offset = off; | |
8235 | ||
75b28aff HV |
8236 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8237 | if (sq_array_size == SIZE_MAX) | |
8238 | return SIZE_MAX; | |
8239 | ||
8240 | if (check_add_overflow(off, sq_array_size, &off)) | |
8241 | return SIZE_MAX; | |
8242 | ||
75b28aff HV |
8243 | return off; |
8244 | } | |
8245 | ||
41edf1a5 | 8246 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8247 | { |
41edf1a5 | 8248 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8249 | unsigned int i; |
8250 | ||
6224843d PB |
8251 | if (imu != ctx->dummy_ubuf) { |
8252 | for (i = 0; i < imu->nr_bvecs; i++) | |
8253 | unpin_user_page(imu->bvec[i].bv_page); | |
8254 | if (imu->acct_pages) | |
8255 | io_unaccount_mem(ctx, imu->acct_pages); | |
8256 | kvfree(imu); | |
8257 | } | |
41edf1a5 | 8258 | *slot = NULL; |
7f61a1e9 PB |
8259 | } |
8260 | ||
bd54b6fe | 8261 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8262 | { |
634d00df PB |
8263 | io_buffer_unmap(ctx, &prsrc->buf); |
8264 | prsrc->buf = NULL; | |
bd54b6fe | 8265 | } |
edafccee | 8266 | |
bd54b6fe BM |
8267 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8268 | { | |
8269 | unsigned int i; | |
edafccee | 8270 | |
7f61a1e9 PB |
8271 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8272 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8273 | kfree(ctx->user_bufs); |
bb6659cc | 8274 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8275 | ctx->user_bufs = NULL; |
bd54b6fe | 8276 | ctx->buf_data = NULL; |
edafccee | 8277 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8278 | } |
8279 | ||
0a96bbe4 | 8280 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8281 | { |
bd54b6fe | 8282 | int ret; |
edafccee | 8283 | |
bd54b6fe | 8284 | if (!ctx->buf_data) |
edafccee JA |
8285 | return -ENXIO; |
8286 | ||
bd54b6fe BM |
8287 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8288 | if (!ret) | |
8289 | __io_sqe_buffers_unregister(ctx); | |
8290 | return ret; | |
edafccee JA |
8291 | } |
8292 | ||
8293 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8294 | void __user *arg, unsigned index) | |
8295 | { | |
8296 | struct iovec __user *src; | |
8297 | ||
8298 | #ifdef CONFIG_COMPAT | |
8299 | if (ctx->compat) { | |
8300 | struct compat_iovec __user *ciovs; | |
8301 | struct compat_iovec ciov; | |
8302 | ||
8303 | ciovs = (struct compat_iovec __user *) arg; | |
8304 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8305 | return -EFAULT; | |
8306 | ||
d55e5f5b | 8307 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8308 | dst->iov_len = ciov.iov_len; |
8309 | return 0; | |
8310 | } | |
8311 | #endif | |
8312 | src = (struct iovec __user *) arg; | |
8313 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8314 | return -EFAULT; | |
8315 | return 0; | |
8316 | } | |
8317 | ||
de293938 JA |
8318 | /* |
8319 | * Not super efficient, but this is just a registration time. And we do cache | |
8320 | * the last compound head, so generally we'll only do a full search if we don't | |
8321 | * match that one. | |
8322 | * | |
8323 | * We check if the given compound head page has already been accounted, to | |
8324 | * avoid double accounting it. This allows us to account the full size of the | |
8325 | * page, not just the constituent pages of a huge page. | |
8326 | */ | |
8327 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8328 | int nr_pages, struct page *hpage) | |
8329 | { | |
8330 | int i, j; | |
8331 | ||
8332 | /* check current page array */ | |
8333 | for (i = 0; i < nr_pages; i++) { | |
8334 | if (!PageCompound(pages[i])) | |
8335 | continue; | |
8336 | if (compound_head(pages[i]) == hpage) | |
8337 | return true; | |
8338 | } | |
8339 | ||
8340 | /* check previously registered pages */ | |
8341 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8342 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8343 | |
8344 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8345 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8346 | continue; | |
8347 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8348 | return true; | |
8349 | } | |
8350 | } | |
8351 | ||
8352 | return false; | |
8353 | } | |
8354 | ||
8355 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8356 | int nr_pages, struct io_mapped_ubuf *imu, | |
8357 | struct page **last_hpage) | |
8358 | { | |
8359 | int i, ret; | |
8360 | ||
216e5835 | 8361 | imu->acct_pages = 0; |
de293938 JA |
8362 | for (i = 0; i < nr_pages; i++) { |
8363 | if (!PageCompound(pages[i])) { | |
8364 | imu->acct_pages++; | |
8365 | } else { | |
8366 | struct page *hpage; | |
8367 | ||
8368 | hpage = compound_head(pages[i]); | |
8369 | if (hpage == *last_hpage) | |
8370 | continue; | |
8371 | *last_hpage = hpage; | |
8372 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8373 | continue; | |
8374 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8375 | } | |
8376 | } | |
8377 | ||
8378 | if (!imu->acct_pages) | |
8379 | return 0; | |
8380 | ||
26bfa89e | 8381 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8382 | if (ret) |
8383 | imu->acct_pages = 0; | |
8384 | return ret; | |
8385 | } | |
8386 | ||
0a96bbe4 | 8387 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8388 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8389 | struct page **last_hpage) |
edafccee | 8390 | { |
41edf1a5 | 8391 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8392 | struct vm_area_struct **vmas = NULL; |
8393 | struct page **pages = NULL; | |
0a96bbe4 BM |
8394 | unsigned long off, start, end, ubuf; |
8395 | size_t size; | |
8396 | int ret, pret, nr_pages, i; | |
8397 | ||
6224843d PB |
8398 | if (!iov->iov_base) { |
8399 | *pimu = ctx->dummy_ubuf; | |
8400 | return 0; | |
8401 | } | |
8402 | ||
0a96bbe4 BM |
8403 | ubuf = (unsigned long) iov->iov_base; |
8404 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8405 | start = ubuf >> PAGE_SHIFT; | |
8406 | nr_pages = end - start; | |
8407 | ||
41edf1a5 | 8408 | *pimu = NULL; |
0a96bbe4 BM |
8409 | ret = -ENOMEM; |
8410 | ||
8411 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8412 | if (!pages) | |
8413 | goto done; | |
8414 | ||
8415 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8416 | GFP_KERNEL); | |
8417 | if (!vmas) | |
8418 | goto done; | |
edafccee | 8419 | |
41edf1a5 | 8420 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8421 | if (!imu) |
0a96bbe4 BM |
8422 | goto done; |
8423 | ||
8424 | ret = 0; | |
8425 | mmap_read_lock(current->mm); | |
8426 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8427 | pages, vmas); | |
8428 | if (pret == nr_pages) { | |
8429 | /* don't support file backed memory */ | |
8430 | for (i = 0; i < nr_pages; i++) { | |
8431 | struct vm_area_struct *vma = vmas[i]; | |
8432 | ||
40dad765 PB |
8433 | if (vma_is_shmem(vma)) |
8434 | continue; | |
0a96bbe4 BM |
8435 | if (vma->vm_file && |
8436 | !is_file_hugepages(vma->vm_file)) { | |
8437 | ret = -EOPNOTSUPP; | |
8438 | break; | |
8439 | } | |
8440 | } | |
8441 | } else { | |
8442 | ret = pret < 0 ? pret : -EFAULT; | |
8443 | } | |
8444 | mmap_read_unlock(current->mm); | |
8445 | if (ret) { | |
8446 | /* | |
8447 | * if we did partial map, or found file backed vmas, | |
8448 | * release any pages we did get | |
8449 | */ | |
8450 | if (pret > 0) | |
8451 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8452 | goto done; |
8453 | } | |
8454 | ||
8455 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8456 | if (ret) { | |
8457 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8458 | goto done; |
8459 | } | |
8460 | ||
8461 | off = ubuf & ~PAGE_MASK; | |
8462 | size = iov->iov_len; | |
8463 | for (i = 0; i < nr_pages; i++) { | |
8464 | size_t vec_len; | |
8465 | ||
8466 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8467 | imu->bvec[i].bv_page = pages[i]; | |
8468 | imu->bvec[i].bv_len = vec_len; | |
8469 | imu->bvec[i].bv_offset = off; | |
8470 | off = 0; | |
8471 | size -= vec_len; | |
8472 | } | |
8473 | /* store original address for later verification */ | |
8474 | imu->ubuf = ubuf; | |
4751f53d | 8475 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8476 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8477 | *pimu = imu; |
0a96bbe4 BM |
8478 | ret = 0; |
8479 | done: | |
41edf1a5 PB |
8480 | if (ret) |
8481 | kvfree(imu); | |
0a96bbe4 BM |
8482 | kvfree(pages); |
8483 | kvfree(vmas); | |
8484 | return ret; | |
8485 | } | |
8486 | ||
2b358604 | 8487 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8488 | { |
87094465 PB |
8489 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8490 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8491 | } |
edafccee | 8492 | |
2b358604 BM |
8493 | static int io_buffer_validate(struct iovec *iov) |
8494 | { | |
50e96989 PB |
8495 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8496 | ||
2b358604 BM |
8497 | /* |
8498 | * Don't impose further limits on the size and buffer | |
8499 | * constraints here, we'll -EINVAL later when IO is | |
8500 | * submitted if they are wrong. | |
8501 | */ | |
6224843d PB |
8502 | if (!iov->iov_base) |
8503 | return iov->iov_len ? -EFAULT : 0; | |
8504 | if (!iov->iov_len) | |
2b358604 | 8505 | return -EFAULT; |
edafccee | 8506 | |
2b358604 BM |
8507 | /* arbitrary limit, but we need something */ |
8508 | if (iov->iov_len > SZ_1G) | |
8509 | return -EFAULT; | |
edafccee | 8510 | |
50e96989 PB |
8511 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8512 | return -EOVERFLOW; | |
8513 | ||
2b358604 BM |
8514 | return 0; |
8515 | } | |
edafccee | 8516 | |
2b358604 | 8517 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 8518 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 8519 | { |
bd54b6fe BM |
8520 | struct page *last_hpage = NULL; |
8521 | struct io_rsrc_data *data; | |
2b358604 BM |
8522 | int i, ret; |
8523 | struct iovec iov; | |
edafccee | 8524 | |
87094465 PB |
8525 | if (ctx->user_bufs) |
8526 | return -EBUSY; | |
489809e2 | 8527 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 8528 | return -EINVAL; |
bd54b6fe | 8529 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
8530 | if (ret) |
8531 | return ret; | |
d878c816 PB |
8532 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
8533 | if (ret) | |
8534 | return ret; | |
bd54b6fe BM |
8535 | ret = io_buffers_map_alloc(ctx, nr_args); |
8536 | if (ret) { | |
bb6659cc | 8537 | io_rsrc_data_free(data); |
bd54b6fe BM |
8538 | return ret; |
8539 | } | |
edafccee | 8540 | |
87094465 | 8541 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
8542 | ret = io_copy_iov(ctx, &iov, arg, i); |
8543 | if (ret) | |
0a96bbe4 | 8544 | break; |
2b358604 BM |
8545 | ret = io_buffer_validate(&iov); |
8546 | if (ret) | |
0a96bbe4 | 8547 | break; |
2d091d62 | 8548 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
8549 | ret = -EINVAL; |
8550 | break; | |
8551 | } | |
edafccee | 8552 | |
41edf1a5 PB |
8553 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
8554 | &last_hpage); | |
0a96bbe4 BM |
8555 | if (ret) |
8556 | break; | |
edafccee | 8557 | } |
0a96bbe4 | 8558 | |
bd54b6fe | 8559 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 8560 | |
bd54b6fe BM |
8561 | ctx->buf_data = data; |
8562 | if (ret) | |
8563 | __io_sqe_buffers_unregister(ctx); | |
8564 | else | |
8565 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
8566 | return ret; |
8567 | } | |
8568 | ||
634d00df PB |
8569 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
8570 | struct io_uring_rsrc_update2 *up, | |
8571 | unsigned int nr_args) | |
8572 | { | |
8573 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
8574 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
8575 | struct page *last_hpage = NULL; |
8576 | bool needs_switch = false; | |
8577 | __u32 done; | |
8578 | int i, err; | |
8579 | ||
8580 | if (!ctx->buf_data) | |
8581 | return -ENXIO; | |
8582 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
8583 | return -EINVAL; | |
8584 | ||
8585 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
8586 | struct io_mapped_ubuf *imu; |
8587 | int offset = up->offset + done; | |
634d00df PB |
8588 | u64 tag = 0; |
8589 | ||
8590 | err = io_copy_iov(ctx, &iov, iovs, done); | |
8591 | if (err) | |
8592 | break; | |
8593 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
8594 | err = -EFAULT; | |
8595 | break; | |
8596 | } | |
0b8c0e7c PB |
8597 | err = io_buffer_validate(&iov); |
8598 | if (err) | |
8599 | break; | |
cf3770e7 CIK |
8600 | if (!iov.iov_base && tag) { |
8601 | err = -EINVAL; | |
8602 | break; | |
8603 | } | |
0b8c0e7c PB |
8604 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
8605 | if (err) | |
8606 | break; | |
634d00df | 8607 | |
0b8c0e7c | 8608 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 8609 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
8610 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
8611 | ctx->rsrc_node, ctx->user_bufs[i]); | |
8612 | if (unlikely(err)) { | |
8613 | io_buffer_unmap(ctx, &imu); | |
634d00df | 8614 | break; |
0b8c0e7c | 8615 | } |
634d00df PB |
8616 | ctx->user_bufs[i] = NULL; |
8617 | needs_switch = true; | |
8618 | } | |
8619 | ||
0b8c0e7c | 8620 | ctx->user_bufs[i] = imu; |
2d091d62 | 8621 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
8622 | } |
8623 | ||
8624 | if (needs_switch) | |
8625 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
8626 | return done ? done : err; | |
8627 | } | |
8628 | ||
9b402849 JA |
8629 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8630 | { | |
8631 | __s32 __user *fds = arg; | |
8632 | int fd; | |
8633 | ||
8634 | if (ctx->cq_ev_fd) | |
8635 | return -EBUSY; | |
8636 | ||
8637 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8638 | return -EFAULT; | |
8639 | ||
8640 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8641 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8642 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 8643 | |
9b402849 JA |
8644 | ctx->cq_ev_fd = NULL; |
8645 | return ret; | |
8646 | } | |
8647 | ||
8648 | return 0; | |
8649 | } | |
8650 | ||
8651 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8652 | { | |
8653 | if (ctx->cq_ev_fd) { | |
8654 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8655 | ctx->cq_ev_fd = NULL; | |
8656 | return 0; | |
8657 | } | |
8658 | ||
8659 | return -ENXIO; | |
8660 | } | |
8661 | ||
5a2e745d JA |
8662 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8663 | { | |
9e15c3a0 JA |
8664 | struct io_buffer *buf; |
8665 | unsigned long index; | |
8666 | ||
8667 | xa_for_each(&ctx->io_buffers, index, buf) | |
8668 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8669 | } |
8670 | ||
7255834e | 8671 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 8672 | { |
68e68ee6 | 8673 | struct io_kiocb *req, *nxt; |
1b4c351f | 8674 | |
bb943b82 PB |
8675 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
8676 | list_del(&req->inflight_entry); | |
1b4c351f JA |
8677 | kmem_cache_free(req_cachep, req); |
8678 | } | |
8679 | } | |
8680 | ||
4010fec4 | 8681 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8682 | { |
cd0ca2e0 | 8683 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 8684 | |
9a4fdbd8 JA |
8685 | mutex_lock(&ctx->uring_lock); |
8686 | ||
cd0ca2e0 PB |
8687 | if (state->free_reqs) { |
8688 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
8689 | state->free_reqs = 0; | |
8e5c66c4 | 8690 | } |
9a4fdbd8 | 8691 | |
cd0ca2e0 PB |
8692 | io_flush_cached_locked_reqs(ctx, state); |
8693 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
8694 | mutex_unlock(&ctx->uring_lock); |
8695 | } | |
8696 | ||
43597aac | 8697 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 8698 | { |
43597aac | 8699 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 8700 | wait_for_completion(&data->done); |
bd54b6fe | 8701 | } |
04fc6c80 | 8702 | |
2b188cc1 JA |
8703 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8704 | { | |
37d1e2e3 | 8705 | io_sq_thread_finish(ctx); |
2aede0e4 | 8706 | |
37d1e2e3 | 8707 | if (ctx->mm_account) { |
2aede0e4 JA |
8708 | mmdrop(ctx->mm_account); |
8709 | ctx->mm_account = NULL; | |
30975825 | 8710 | } |
def596e9 | 8711 | |
43597aac PB |
8712 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
8713 | io_wait_rsrc_data(ctx->buf_data); | |
8714 | io_wait_rsrc_data(ctx->file_data); | |
8715 | ||
8bad28d8 | 8716 | mutex_lock(&ctx->uring_lock); |
43597aac | 8717 | if (ctx->buf_data) |
bd54b6fe | 8718 | __io_sqe_buffers_unregister(ctx); |
43597aac | 8719 | if (ctx->file_data) |
08480400 | 8720 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
8721 | if (ctx->rings) |
8722 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8723 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8724 | io_eventfd_unregister(ctx); |
5a2e745d | 8725 | io_destroy_buffers(ctx); |
07db298a PB |
8726 | if (ctx->sq_creds) |
8727 | put_cred(ctx->sq_creds); | |
def596e9 | 8728 | |
a7f0ed5a PB |
8729 | /* there are no registered resources left, nobody uses it */ |
8730 | if (ctx->rsrc_node) | |
8731 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8732 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8733 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8734 | flush_delayed_work(&ctx->rsrc_put_work); |
8735 | ||
8736 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8737 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 8738 | |
2b188cc1 | 8739 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8740 | if (ctx->ring_sock) { |
8741 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8742 | sock_release(ctx->ring_sock); |
355e8d26 | 8743 | } |
2b188cc1 JA |
8744 | #endif |
8745 | ||
75b28aff | 8746 | io_mem_free(ctx->rings); |
2b188cc1 | 8747 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8748 | |
8749 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8750 | free_uid(ctx->user); |
4010fec4 | 8751 | io_req_caches_free(ctx); |
e941894e JA |
8752 | if (ctx->hash_map) |
8753 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8754 | kfree(ctx->cancel_hash); |
6224843d | 8755 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
8756 | kfree(ctx); |
8757 | } | |
8758 | ||
8759 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8760 | { | |
8761 | struct io_ring_ctx *ctx = file->private_data; | |
8762 | __poll_t mask = 0; | |
8763 | ||
311997b3 | 8764 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
8765 | /* |
8766 | * synchronizes with barrier from wq_has_sleeper call in | |
8767 | * io_commit_cqring | |
8768 | */ | |
2b188cc1 | 8769 | smp_rmb(); |
90554200 | 8770 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8771 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8772 | |
8773 | /* | |
8774 | * Don't flush cqring overflow list here, just do a simple check. | |
8775 | * Otherwise there could possible be ABBA deadlock: | |
8776 | * CPU0 CPU1 | |
8777 | * ---- ---- | |
8778 | * lock(&ctx->uring_lock); | |
8779 | * lock(&ep->mtx); | |
8780 | * lock(&ctx->uring_lock); | |
8781 | * lock(&ep->mtx); | |
8782 | * | |
8783 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8784 | * pushs them to do the flush. | |
8785 | */ | |
5ed7a37d | 8786 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
8787 | mask |= EPOLLIN | EPOLLRDNORM; |
8788 | ||
8789 | return mask; | |
8790 | } | |
8791 | ||
8792 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8793 | { | |
8794 | struct io_ring_ctx *ctx = file->private_data; | |
8795 | ||
8796 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8797 | } | |
8798 | ||
0bead8cd | 8799 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8800 | { |
4379bf8b | 8801 | const struct cred *creds; |
071698e1 | 8802 | |
61cf9370 | 8803 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8804 | if (creds) { |
8805 | put_cred(creds); | |
0bead8cd | 8806 | return 0; |
1e6fa521 | 8807 | } |
0bead8cd YD |
8808 | |
8809 | return -EINVAL; | |
8810 | } | |
8811 | ||
d56d938b PB |
8812 | struct io_tctx_exit { |
8813 | struct callback_head task_work; | |
8814 | struct completion completion; | |
baf186c4 | 8815 | struct io_ring_ctx *ctx; |
d56d938b PB |
8816 | }; |
8817 | ||
8818 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8819 | { | |
8820 | struct io_uring_task *tctx = current->io_uring; | |
8821 | struct io_tctx_exit *work; | |
8822 | ||
8823 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8824 | /* | |
8825 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8826 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8827 | */ | |
8828 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 8829 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
8830 | complete(&work->completion); |
8831 | } | |
8832 | ||
28090c13 PB |
8833 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8834 | { | |
8835 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8836 | ||
8837 | return req->ctx == data; | |
8838 | } | |
8839 | ||
85faa7b8 JA |
8840 | static void io_ring_exit_work(struct work_struct *work) |
8841 | { | |
d56d938b | 8842 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 8843 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 8844 | unsigned long interval = HZ / 20; |
d56d938b PB |
8845 | struct io_tctx_exit exit; |
8846 | struct io_tctx_node *node; | |
8847 | int ret; | |
85faa7b8 | 8848 | |
56952e91 JA |
8849 | /* |
8850 | * If we're doing polled IO and end up having requests being | |
8851 | * submitted async (out-of-line), then completions can come in while | |
8852 | * we're waiting for refs to drop. We need to reap these manually, | |
8853 | * as nobody else will be looking for them. | |
8854 | */ | |
b2edc0a7 | 8855 | do { |
3dd0c97a | 8856 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
8857 | if (ctx->sq_data) { |
8858 | struct io_sq_data *sqd = ctx->sq_data; | |
8859 | struct task_struct *tsk; | |
8860 | ||
8861 | io_sq_thread_park(sqd); | |
8862 | tsk = sqd->thread; | |
8863 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
8864 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
8865 | io_cancel_ctx_cb, ctx, true); | |
8866 | io_sq_thread_unpark(sqd); | |
8867 | } | |
b5bb3a24 | 8868 | |
58d3be2c PB |
8869 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
8870 | /* there is little hope left, don't run it too often */ | |
8871 | interval = HZ * 60; | |
8872 | } | |
8873 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 8874 | |
7f00651a PB |
8875 | init_completion(&exit.completion); |
8876 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
8877 | exit.ctx = ctx; | |
89b5066e PB |
8878 | /* |
8879 | * Some may use context even when all refs and requests have been put, | |
8880 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 8881 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
8882 | * this lock/unlock section also waits them to finish. |
8883 | */ | |
d56d938b PB |
8884 | mutex_lock(&ctx->uring_lock); |
8885 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
8886 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
8887 | ||
d56d938b PB |
8888 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
8889 | ctx_node); | |
7f00651a PB |
8890 | /* don't spin on a single task if cancellation failed */ |
8891 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
8892 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
8893 | if (WARN_ON_ONCE(ret)) | |
8894 | continue; | |
8895 | wake_up_process(node->task); | |
8896 | ||
8897 | mutex_unlock(&ctx->uring_lock); | |
8898 | wait_for_completion(&exit.completion); | |
d56d938b PB |
8899 | mutex_lock(&ctx->uring_lock); |
8900 | } | |
8901 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
8902 | spin_lock(&ctx->completion_lock); |
8903 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 8904 | |
85faa7b8 JA |
8905 | io_ring_ctx_free(ctx); |
8906 | } | |
8907 | ||
80c4cbdb PB |
8908 | /* Returns true if we found and killed one or more timeouts */ |
8909 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 8910 | bool cancel_all) |
80c4cbdb PB |
8911 | { |
8912 | struct io_kiocb *req, *tmp; | |
8913 | int canceled = 0; | |
8914 | ||
79ebeaee JA |
8915 | spin_lock(&ctx->completion_lock); |
8916 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 8917 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 8918 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
8919 | io_kill_timeout(req, -ECANCELED); |
8920 | canceled++; | |
8921 | } | |
8922 | } | |
79ebeaee | 8923 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
8924 | if (canceled != 0) |
8925 | io_commit_cqring(ctx); | |
79ebeaee | 8926 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
8927 | if (canceled != 0) |
8928 | io_cqring_ev_posted(ctx); | |
8929 | return canceled != 0; | |
8930 | } | |
8931 | ||
2b188cc1 JA |
8932 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
8933 | { | |
61cf9370 MWO |
8934 | unsigned long index; |
8935 | struct creds *creds; | |
8936 | ||
2b188cc1 JA |
8937 | mutex_lock(&ctx->uring_lock); |
8938 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 8939 | if (ctx->rings) |
6c2450ae | 8940 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
8941 | xa_for_each(&ctx->personalities, index, creds) |
8942 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
8943 | mutex_unlock(&ctx->uring_lock); |
8944 | ||
3dd0c97a PB |
8945 | io_kill_timeouts(ctx, NULL, true); |
8946 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 8947 | |
15dff286 | 8948 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 8949 | io_iopoll_try_reap_events(ctx); |
309fc03a | 8950 | |
85faa7b8 | 8951 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
8952 | /* |
8953 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
8954 | * if we're exiting a ton of rings at the same time. It just adds | |
8955 | * noise and overhead, there's no discernable change in runtime | |
8956 | * over using system_wq. | |
8957 | */ | |
8958 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
8959 | } |
8960 | ||
8961 | static int io_uring_release(struct inode *inode, struct file *file) | |
8962 | { | |
8963 | struct io_ring_ctx *ctx = file->private_data; | |
8964 | ||
8965 | file->private_data = NULL; | |
8966 | io_ring_ctx_wait_and_kill(ctx); | |
8967 | return 0; | |
8968 | } | |
8969 | ||
f6edbabb PB |
8970 | struct io_task_cancel { |
8971 | struct task_struct *task; | |
3dd0c97a | 8972 | bool all; |
f6edbabb | 8973 | }; |
f254ac04 | 8974 | |
f6edbabb | 8975 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 8976 | { |
9a472ef7 | 8977 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 8978 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
8979 | bool ret; |
8980 | ||
3dd0c97a | 8981 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
8982 | struct io_ring_ctx *ctx = req->ctx; |
8983 | ||
8984 | /* protect against races with linked timeouts */ | |
79ebeaee | 8985 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 8986 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 8987 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 8988 | } else { |
3dd0c97a | 8989 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
8990 | } |
8991 | return ret; | |
b711d4ea JA |
8992 | } |
8993 | ||
e1915f76 | 8994 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 8995 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 8996 | { |
e1915f76 | 8997 | struct io_defer_entry *de; |
b7ddce3c PB |
8998 | LIST_HEAD(list); |
8999 | ||
79ebeaee | 9000 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9001 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9002 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9003 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9004 | break; | |
9005 | } | |
9006 | } | |
79ebeaee | 9007 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9008 | if (list_empty(&list)) |
9009 | return false; | |
b7ddce3c PB |
9010 | |
9011 | while (!list_empty(&list)) { | |
9012 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9013 | list_del_init(&de->list); | |
f41db273 | 9014 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9015 | kfree(de); |
9016 | } | |
e1915f76 | 9017 | return true; |
b7ddce3c PB |
9018 | } |
9019 | ||
1b00764f PB |
9020 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9021 | { | |
9022 | struct io_tctx_node *node; | |
9023 | enum io_wq_cancel cret; | |
9024 | bool ret = false; | |
9025 | ||
9026 | mutex_lock(&ctx->uring_lock); | |
9027 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9028 | struct io_uring_task *tctx = node->task->io_uring; | |
9029 | ||
9030 | /* | |
9031 | * io_wq will stay alive while we hold uring_lock, because it's | |
9032 | * killed after ctx nodes, which requires to take the lock. | |
9033 | */ | |
9034 | if (!tctx || !tctx->io_wq) | |
9035 | continue; | |
9036 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9037 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9038 | } | |
9039 | mutex_unlock(&ctx->uring_lock); | |
9040 | ||
9041 | return ret; | |
9042 | } | |
9043 | ||
9936c7c2 PB |
9044 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9045 | struct task_struct *task, | |
3dd0c97a | 9046 | bool cancel_all) |
9936c7c2 | 9047 | { |
3dd0c97a | 9048 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9049 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9050 | |
9051 | while (1) { | |
9052 | enum io_wq_cancel cret; | |
9053 | bool ret = false; | |
9054 | ||
1b00764f PB |
9055 | if (!task) { |
9056 | ret |= io_uring_try_cancel_iowq(ctx); | |
9057 | } else if (tctx && tctx->io_wq) { | |
9058 | /* | |
9059 | * Cancels requests of all rings, not only @ctx, but | |
9060 | * it's fine as the task is in exit/exec. | |
9061 | */ | |
5aa75ed5 | 9062 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9063 | &cancel, true); |
9064 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9065 | } | |
9066 | ||
9067 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9068 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9069 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9070 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9071 | io_iopoll_try_reap_events(ctx); | |
9072 | ret = true; | |
9073 | } | |
9074 | } | |
9075 | ||
3dd0c97a PB |
9076 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9077 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9078 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9079 | if (task) |
9080 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9081 | if (!ret) |
9082 | break; | |
9083 | cond_resched(); | |
9084 | } | |
9085 | } | |
9086 | ||
eef51daa | 9087 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9088 | { |
236434c3 | 9089 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9090 | struct io_tctx_node *node; |
a528b04e | 9091 | int ret; |
236434c3 MWO |
9092 | |
9093 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9094 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9095 | if (unlikely(ret)) |
9096 | return ret; | |
236434c3 | 9097 | tctx = current->io_uring; |
0f212204 | 9098 | } |
cf27f3b1 PB |
9099 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9100 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9101 | if (!node) | |
9102 | return -ENOMEM; | |
9103 | node->ctx = ctx; | |
9104 | node->task = current; | |
13bf43f5 | 9105 | |
cf27f3b1 PB |
9106 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9107 | node, GFP_KERNEL)); | |
9108 | if (ret) { | |
9109 | kfree(node); | |
9110 | return ret; | |
0f212204 | 9111 | } |
cf27f3b1 PB |
9112 | |
9113 | mutex_lock(&ctx->uring_lock); | |
9114 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9115 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9116 | } |
cf27f3b1 | 9117 | tctx->last = ctx; |
0f212204 JA |
9118 | return 0; |
9119 | } | |
9120 | ||
cf27f3b1 PB |
9121 | /* |
9122 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9123 | */ | |
eef51daa | 9124 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9125 | { |
9126 | struct io_uring_task *tctx = current->io_uring; | |
9127 | ||
9128 | if (likely(tctx && tctx->last == ctx)) | |
9129 | return 0; | |
eef51daa | 9130 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9131 | } |
9132 | ||
0f212204 JA |
9133 | /* |
9134 | * Remove this io_uring_file -> task mapping. | |
9135 | */ | |
eef51daa | 9136 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9137 | { |
9138 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9139 | struct io_tctx_node *node; |
2941267b | 9140 | |
eebd2e37 PB |
9141 | if (!tctx) |
9142 | return; | |
13bf43f5 PB |
9143 | node = xa_erase(&tctx->xa, index); |
9144 | if (!node) | |
2941267b | 9145 | return; |
0f212204 | 9146 | |
13bf43f5 PB |
9147 | WARN_ON_ONCE(current != node->task); |
9148 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9149 | ||
9150 | mutex_lock(&node->ctx->uring_lock); | |
9151 | list_del(&node->ctx_node); | |
9152 | mutex_unlock(&node->ctx->uring_lock); | |
9153 | ||
baf186c4 | 9154 | if (tctx->last == node->ctx) |
0f212204 | 9155 | tctx->last = NULL; |
13bf43f5 | 9156 | kfree(node); |
0f212204 JA |
9157 | } |
9158 | ||
8452d4a6 | 9159 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9160 | { |
ba5ef6dc | 9161 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9162 | struct io_tctx_node *node; |
de7f1d9e PB |
9163 | unsigned long index; |
9164 | ||
13bf43f5 | 9165 | xa_for_each(&tctx->xa, index, node) |
eef51daa | 9166 | io_uring_del_tctx_node(index); |
b16ef427 ME |
9167 | if (wq) { |
9168 | /* | |
9169 | * Must be after io_uring_del_task_file() (removes nodes under | |
9170 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9171 | */ | |
9172 | tctx->io_wq = NULL; | |
ba5ef6dc | 9173 | io_wq_put_and_exit(wq); |
b16ef427 | 9174 | } |
de7f1d9e PB |
9175 | } |
9176 | ||
3f48cf18 | 9177 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9178 | { |
3f48cf18 PB |
9179 | if (tracked) |
9180 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9181 | return percpu_counter_sum(&tctx->inflight); |
9182 | } | |
9183 | ||
09899b19 PB |
9184 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9185 | { | |
9186 | struct io_uring_task *tctx = task->io_uring; | |
9187 | unsigned int refs = tctx->cached_refs; | |
9188 | ||
e9dbe221 PB |
9189 | if (refs) { |
9190 | tctx->cached_refs = 0; | |
9191 | percpu_counter_sub(&tctx->inflight, refs); | |
9192 | put_task_struct_many(task, refs); | |
9193 | } | |
09899b19 PB |
9194 | } |
9195 | ||
78cc687b PB |
9196 | /* |
9197 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9198 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9199 | */ | |
9200 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9201 | { |
521d6a73 | 9202 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9203 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9204 | s64 inflight; |
9205 | DEFINE_WAIT(wait); | |
fdaf083c | 9206 | |
78cc687b PB |
9207 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9208 | ||
6d042ffb PO |
9209 | if (!current->io_uring) |
9210 | return; | |
17a91051 PB |
9211 | if (tctx->io_wq) |
9212 | io_wq_exit_start(tctx->io_wq); | |
9213 | ||
0e9ddb39 PB |
9214 | atomic_inc(&tctx->in_idle); |
9215 | do { | |
e9dbe221 | 9216 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9217 | /* read completions before cancelations */ |
78cc687b | 9218 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9219 | if (!inflight) |
9220 | break; | |
fdaf083c | 9221 | |
78cc687b PB |
9222 | if (!sqd) { |
9223 | struct io_tctx_node *node; | |
9224 | unsigned long index; | |
0f212204 | 9225 | |
78cc687b PB |
9226 | xa_for_each(&tctx->xa, index, node) { |
9227 | /* sqpoll task will cancel all its requests */ | |
9228 | if (node->ctx->sq_data) | |
9229 | continue; | |
9230 | io_uring_try_cancel_requests(node->ctx, current, | |
9231 | cancel_all); | |
9232 | } | |
9233 | } else { | |
9234 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9235 | io_uring_try_cancel_requests(ctx, current, | |
9236 | cancel_all); | |
9237 | } | |
17a91051 | 9238 | |
0f212204 | 9239 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9240 | io_uring_drop_tctx_refs(current); |
0f212204 | 9241 | /* |
a1bb3cd5 PB |
9242 | * If we've seen completions, retry without waiting. This |
9243 | * avoids a race where a completion comes in before we did | |
9244 | * prepare_to_wait(). | |
0f212204 | 9245 | */ |
3dd0c97a | 9246 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9247 | schedule(); |
f57555ed | 9248 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9249 | } while (1); |
fdaf083c | 9250 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9251 | |
8452d4a6 | 9252 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9253 | if (cancel_all) { |
3f48cf18 PB |
9254 | /* for exec all current's requests should be gone, kill tctx */ |
9255 | __io_uring_free(current); | |
9256 | } | |
44e728b8 PB |
9257 | } |
9258 | ||
f552a27a | 9259 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9260 | { |
f552a27a | 9261 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9262 | } |
9263 | ||
6c5c240e RP |
9264 | static void *io_uring_validate_mmap_request(struct file *file, |
9265 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9266 | { |
2b188cc1 | 9267 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9268 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9269 | struct page *page; |
9270 | void *ptr; | |
9271 | ||
9272 | switch (offset) { | |
9273 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9274 | case IORING_OFF_CQ_RING: |
9275 | ptr = ctx->rings; | |
2b188cc1 JA |
9276 | break; |
9277 | case IORING_OFF_SQES: | |
9278 | ptr = ctx->sq_sqes; | |
9279 | break; | |
2b188cc1 | 9280 | default: |
6c5c240e | 9281 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9282 | } |
9283 | ||
9284 | page = virt_to_head_page(ptr); | |
a50b854e | 9285 | if (sz > page_size(page)) |
6c5c240e RP |
9286 | return ERR_PTR(-EINVAL); |
9287 | ||
9288 | return ptr; | |
9289 | } | |
9290 | ||
9291 | #ifdef CONFIG_MMU | |
9292 | ||
9293 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9294 | { | |
9295 | size_t sz = vma->vm_end - vma->vm_start; | |
9296 | unsigned long pfn; | |
9297 | void *ptr; | |
9298 | ||
9299 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9300 | if (IS_ERR(ptr)) | |
9301 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9302 | |
9303 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9304 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9305 | } | |
9306 | ||
6c5c240e RP |
9307 | #else /* !CONFIG_MMU */ |
9308 | ||
9309 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9310 | { | |
9311 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9312 | } | |
9313 | ||
9314 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9315 | { | |
9316 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9317 | } | |
9318 | ||
9319 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9320 | unsigned long addr, unsigned long len, | |
9321 | unsigned long pgoff, unsigned long flags) | |
9322 | { | |
9323 | void *ptr; | |
9324 | ||
9325 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9326 | if (IS_ERR(ptr)) | |
9327 | return PTR_ERR(ptr); | |
9328 | ||
9329 | return (unsigned long) ptr; | |
9330 | } | |
9331 | ||
9332 | #endif /* !CONFIG_MMU */ | |
9333 | ||
d9d05217 | 9334 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9335 | { |
9336 | DEFINE_WAIT(wait); | |
9337 | ||
9338 | do { | |
9339 | if (!io_sqring_full(ctx)) | |
9340 | break; | |
90554200 JA |
9341 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9342 | ||
9343 | if (!io_sqring_full(ctx)) | |
9344 | break; | |
90554200 JA |
9345 | schedule(); |
9346 | } while (!signal_pending(current)); | |
9347 | ||
9348 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9349 | return 0; |
90554200 JA |
9350 | } |
9351 | ||
c73ebb68 HX |
9352 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9353 | struct __kernel_timespec __user **ts, | |
9354 | const sigset_t __user **sig) | |
9355 | { | |
9356 | struct io_uring_getevents_arg arg; | |
9357 | ||
9358 | /* | |
9359 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9360 | * is just a pointer to the sigset_t. | |
9361 | */ | |
9362 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9363 | *sig = (const sigset_t __user *) argp; | |
9364 | *ts = NULL; | |
9365 | return 0; | |
9366 | } | |
9367 | ||
9368 | /* | |
9369 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9370 | * timespec and sigset_t pointers if good. | |
9371 | */ | |
9372 | if (*argsz != sizeof(arg)) | |
9373 | return -EINVAL; | |
9374 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9375 | return -EFAULT; | |
9376 | *sig = u64_to_user_ptr(arg.sigmask); | |
9377 | *argsz = arg.sigmask_sz; | |
9378 | *ts = u64_to_user_ptr(arg.ts); | |
9379 | return 0; | |
9380 | } | |
9381 | ||
2b188cc1 | 9382 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9383 | u32, min_complete, u32, flags, const void __user *, argp, |
9384 | size_t, argsz) | |
2b188cc1 JA |
9385 | { |
9386 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9387 | int submitted = 0; |
9388 | struct fd f; | |
33f993da | 9389 | long ret; |
2b188cc1 | 9390 | |
4c6e277c | 9391 | io_run_task_work(); |
b41e9852 | 9392 | |
33f993da PB |
9393 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9394 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9395 | return -EINVAL; |
9396 | ||
9397 | f = fdget(fd); | |
33f993da | 9398 | if (unlikely(!f.file)) |
2b188cc1 JA |
9399 | return -EBADF; |
9400 | ||
9401 | ret = -EOPNOTSUPP; | |
33f993da | 9402 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9403 | goto out_fput; |
9404 | ||
9405 | ret = -ENXIO; | |
9406 | ctx = f.file->private_data; | |
33f993da | 9407 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9408 | goto out_fput; |
9409 | ||
7e84e1c7 | 9410 | ret = -EBADFD; |
33f993da | 9411 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9412 | goto out; |
9413 | ||
6c271ce2 JA |
9414 | /* |
9415 | * For SQ polling, the thread will do all submissions and completions. | |
9416 | * Just return the requested submit count, and wake the thread if | |
9417 | * we were asked to. | |
9418 | */ | |
b2a9eada | 9419 | ret = 0; |
6c271ce2 | 9420 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9421 | io_cqring_overflow_flush(ctx); |
89448c47 | 9422 | |
21f96522 JA |
9423 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9424 | ret = -EOWNERDEAD; | |
04147488 | 9425 | goto out; |
21f96522 | 9426 | } |
6c271ce2 | 9427 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9428 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9429 | if (flags & IORING_ENTER_SQ_WAIT) { |
9430 | ret = io_sqpoll_wait_sq(ctx); | |
9431 | if (ret) | |
9432 | goto out; | |
9433 | } | |
6c271ce2 | 9434 | submitted = to_submit; |
b2a9eada | 9435 | } else if (to_submit) { |
eef51daa | 9436 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9437 | if (unlikely(ret)) |
9438 | goto out; | |
2b188cc1 | 9439 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9440 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9441 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9442 | |
9443 | if (submitted != to_submit) | |
9444 | goto out; | |
2b188cc1 JA |
9445 | } |
9446 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9447 | const sigset_t __user *sig; |
9448 | struct __kernel_timespec __user *ts; | |
9449 | ||
9450 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9451 | if (unlikely(ret)) | |
9452 | goto out; | |
9453 | ||
2b188cc1 JA |
9454 | min_complete = min(min_complete, ctx->cq_entries); |
9455 | ||
32b2244a XW |
9456 | /* |
9457 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9458 | * space applications don't need to do io completion events | |
9459 | * polling again, they can rely on io_sq_thread to do polling | |
9460 | * work, which can reduce cpu usage and uring_lock contention. | |
9461 | */ | |
9462 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9463 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9464 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9465 | } else { |
c73ebb68 | 9466 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9467 | } |
2b188cc1 JA |
9468 | } |
9469 | ||
7c504e65 | 9470 | out: |
6805b32e | 9471 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9472 | out_fput: |
9473 | fdput(f); | |
9474 | return submitted ? submitted : ret; | |
9475 | } | |
9476 | ||
bebdb65e | 9477 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9478 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9479 | const struct cred *cred) | |
87ce955b | 9480 | { |
87ce955b JA |
9481 | struct user_namespace *uns = seq_user_ns(m); |
9482 | struct group_info *gi; | |
9483 | kernel_cap_t cap; | |
9484 | unsigned __capi; | |
9485 | int g; | |
9486 | ||
9487 | seq_printf(m, "%5d\n", id); | |
9488 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9489 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9490 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9491 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9492 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9493 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9494 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9495 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9496 | seq_puts(m, "\n\tGroups:\t"); | |
9497 | gi = cred->group_info; | |
9498 | for (g = 0; g < gi->ngroups; g++) { | |
9499 | seq_put_decimal_ull(m, g ? " " : "", | |
9500 | from_kgid_munged(uns, gi->gid[g])); | |
9501 | } | |
9502 | seq_puts(m, "\n\tCapEff:\t"); | |
9503 | cap = cred->cap_effective; | |
9504 | CAP_FOR_EACH_U32(__capi) | |
9505 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9506 | seq_putc(m, '\n'); | |
9507 | return 0; | |
9508 | } | |
9509 | ||
9510 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9511 | { | |
dbbe9c64 | 9512 | struct io_sq_data *sq = NULL; |
fad8e0de | 9513 | bool has_lock; |
87ce955b JA |
9514 | int i; |
9515 | ||
fad8e0de JA |
9516 | /* |
9517 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9518 | * since fdinfo case grabs it in the opposite direction of normal use | |
9519 | * cases. If we fail to get the lock, we just don't iterate any | |
9520 | * structures that could be going away outside the io_uring mutex. | |
9521 | */ | |
9522 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9523 | ||
5f3f26f9 | 9524 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9525 | sq = ctx->sq_data; |
5f3f26f9 JA |
9526 | if (!sq->thread) |
9527 | sq = NULL; | |
9528 | } | |
dbbe9c64 JQ |
9529 | |
9530 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9531 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9532 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9533 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9534 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9535 | |
87ce955b JA |
9536 | if (f) |
9537 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9538 | else | |
9539 | seq_printf(m, "%5u: <none>\n", i); | |
9540 | } | |
9541 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9542 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 9543 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 9544 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9545 | |
4751f53d | 9546 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9547 | } |
61cf9370 MWO |
9548 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9549 | unsigned long index; | |
9550 | const struct cred *cred; | |
9551 | ||
87ce955b | 9552 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9553 | xa_for_each(&ctx->personalities, index, cred) |
9554 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9555 | } |
d7718a9d | 9556 | seq_printf(m, "PollList:\n"); |
79ebeaee | 9557 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
9558 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
9559 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9560 | struct io_kiocb *req; | |
9561 | ||
9562 | hlist_for_each_entry(req, list, hash_node) | |
9563 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9564 | req->task->task_works != NULL); | |
9565 | } | |
79ebeaee | 9566 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
9567 | if (has_lock) |
9568 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9569 | } |
9570 | ||
9571 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9572 | { | |
9573 | struct io_ring_ctx *ctx = f->private_data; | |
9574 | ||
9575 | if (percpu_ref_tryget(&ctx->refs)) { | |
9576 | __io_uring_show_fdinfo(ctx, m); | |
9577 | percpu_ref_put(&ctx->refs); | |
9578 | } | |
9579 | } | |
bebdb65e | 9580 | #endif |
87ce955b | 9581 | |
2b188cc1 JA |
9582 | static const struct file_operations io_uring_fops = { |
9583 | .release = io_uring_release, | |
9584 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9585 | #ifndef CONFIG_MMU |
9586 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9587 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9588 | #endif | |
2b188cc1 JA |
9589 | .poll = io_uring_poll, |
9590 | .fasync = io_uring_fasync, | |
bebdb65e | 9591 | #ifdef CONFIG_PROC_FS |
87ce955b | 9592 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9593 | #endif |
2b188cc1 JA |
9594 | }; |
9595 | ||
9596 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9597 | struct io_uring_params *p) | |
9598 | { | |
75b28aff HV |
9599 | struct io_rings *rings; |
9600 | size_t size, sq_array_offset; | |
2b188cc1 | 9601 | |
bd740481 JA |
9602 | /* make sure these are sane, as we already accounted them */ |
9603 | ctx->sq_entries = p->sq_entries; | |
9604 | ctx->cq_entries = p->cq_entries; | |
9605 | ||
75b28aff HV |
9606 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9607 | if (size == SIZE_MAX) | |
9608 | return -EOVERFLOW; | |
9609 | ||
9610 | rings = io_mem_alloc(size); | |
9611 | if (!rings) | |
2b188cc1 JA |
9612 | return -ENOMEM; |
9613 | ||
75b28aff HV |
9614 | ctx->rings = rings; |
9615 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9616 | rings->sq_ring_mask = p->sq_entries - 1; | |
9617 | rings->cq_ring_mask = p->cq_entries - 1; | |
9618 | rings->sq_ring_entries = p->sq_entries; | |
9619 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
9620 | |
9621 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9622 | if (size == SIZE_MAX) { |
9623 | io_mem_free(ctx->rings); | |
9624 | ctx->rings = NULL; | |
2b188cc1 | 9625 | return -EOVERFLOW; |
eb065d30 | 9626 | } |
2b188cc1 JA |
9627 | |
9628 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9629 | if (!ctx->sq_sqes) { |
9630 | io_mem_free(ctx->rings); | |
9631 | ctx->rings = NULL; | |
2b188cc1 | 9632 | return -ENOMEM; |
eb065d30 | 9633 | } |
2b188cc1 | 9634 | |
2b188cc1 JA |
9635 | return 0; |
9636 | } | |
9637 | ||
9faadcc8 PB |
9638 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9639 | { | |
9640 | int ret, fd; | |
9641 | ||
9642 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9643 | if (fd < 0) | |
9644 | return fd; | |
9645 | ||
eef51daa | 9646 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
9647 | if (ret) { |
9648 | put_unused_fd(fd); | |
9649 | return ret; | |
9650 | } | |
9651 | fd_install(fd, file); | |
9652 | return fd; | |
9653 | } | |
9654 | ||
2b188cc1 JA |
9655 | /* |
9656 | * Allocate an anonymous fd, this is what constitutes the application | |
9657 | * visible backing of an io_uring instance. The application mmaps this | |
9658 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9659 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9660 | */ | |
9faadcc8 | 9661 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9662 | { |
9663 | struct file *file; | |
9faadcc8 | 9664 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9665 | int ret; |
9666 | ||
2b188cc1 JA |
9667 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9668 | &ctx->ring_sock); | |
9669 | if (ret) | |
9faadcc8 | 9670 | return ERR_PTR(ret); |
2b188cc1 JA |
9671 | #endif |
9672 | ||
2b188cc1 JA |
9673 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9674 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9675 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9676 | if (IS_ERR(file)) { |
9677 | sock_release(ctx->ring_sock); | |
9678 | ctx->ring_sock = NULL; | |
9679 | } else { | |
9680 | ctx->ring_sock->file = file; | |
0f212204 | 9681 | } |
2b188cc1 | 9682 | #endif |
9faadcc8 | 9683 | return file; |
2b188cc1 JA |
9684 | } |
9685 | ||
7f13657d XW |
9686 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9687 | struct io_uring_params __user *params) | |
2b188cc1 | 9688 | { |
2b188cc1 | 9689 | struct io_ring_ctx *ctx; |
9faadcc8 | 9690 | struct file *file; |
2b188cc1 JA |
9691 | int ret; |
9692 | ||
8110c1a6 | 9693 | if (!entries) |
2b188cc1 | 9694 | return -EINVAL; |
8110c1a6 JA |
9695 | if (entries > IORING_MAX_ENTRIES) { |
9696 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9697 | return -EINVAL; | |
9698 | entries = IORING_MAX_ENTRIES; | |
9699 | } | |
2b188cc1 JA |
9700 | |
9701 | /* | |
9702 | * Use twice as many entries for the CQ ring. It's possible for the | |
9703 | * application to drive a higher depth than the size of the SQ ring, | |
9704 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9705 | * some flexibility in overcommitting a bit. If the application has |
9706 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9707 | * of CQ ring entries manually. | |
2b188cc1 JA |
9708 | */ |
9709 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9710 | if (p->flags & IORING_SETUP_CQSIZE) { |
9711 | /* | |
9712 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9713 | * to a power-of-two, if it isn't already. We do NOT impose | |
9714 | * any cq vs sq ring sizing. | |
9715 | */ | |
eb2667b3 | 9716 | if (!p->cq_entries) |
33a107f0 | 9717 | return -EINVAL; |
8110c1a6 JA |
9718 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9719 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9720 | return -EINVAL; | |
9721 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9722 | } | |
eb2667b3 JQ |
9723 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9724 | if (p->cq_entries < p->sq_entries) | |
9725 | return -EINVAL; | |
33a107f0 JA |
9726 | } else { |
9727 | p->cq_entries = 2 * p->sq_entries; | |
9728 | } | |
2b188cc1 | 9729 | |
2b188cc1 | 9730 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9731 | if (!ctx) |
2b188cc1 | 9732 | return -ENOMEM; |
2b188cc1 | 9733 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9734 | if (!capable(CAP_IPC_LOCK)) |
9735 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9736 | |
9737 | /* | |
9738 | * This is just grabbed for accounting purposes. When a process exits, | |
9739 | * the mm is exited and dropped before the files, hence we need to hang | |
9740 | * on to this mm purely for the purposes of being able to unaccount | |
9741 | * memory (locked/pinned vm). It's not used for anything else. | |
9742 | */ | |
6b7898eb | 9743 | mmgrab(current->mm); |
2aede0e4 | 9744 | ctx->mm_account = current->mm; |
6b7898eb | 9745 | |
2b188cc1 JA |
9746 | ret = io_allocate_scq_urings(ctx, p); |
9747 | if (ret) | |
9748 | goto err; | |
9749 | ||
7e84e1c7 | 9750 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9751 | if (ret) |
9752 | goto err; | |
eae071c9 | 9753 | /* always set a rsrc node */ |
47b228ce PB |
9754 | ret = io_rsrc_node_switch_start(ctx); |
9755 | if (ret) | |
9756 | goto err; | |
eae071c9 | 9757 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 9758 | |
2b188cc1 | 9759 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9760 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9761 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9762 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9763 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9764 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9765 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9766 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9767 | |
9768 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9769 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9770 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9771 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9772 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9773 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9774 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9775 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9776 | |
7f13657d XW |
9777 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9778 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9779 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9780 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
9781 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
9782 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
9783 | |
9784 | if (copy_to_user(params, p, sizeof(*p))) { | |
9785 | ret = -EFAULT; | |
9786 | goto err; | |
9787 | } | |
d1719f70 | 9788 | |
9faadcc8 PB |
9789 | file = io_uring_get_file(ctx); |
9790 | if (IS_ERR(file)) { | |
9791 | ret = PTR_ERR(file); | |
9792 | goto err; | |
9793 | } | |
9794 | ||
044c1ab3 JA |
9795 | /* |
9796 | * Install ring fd as the very last thing, so we don't risk someone | |
9797 | * having closed it before we finish setup | |
9798 | */ | |
9faadcc8 PB |
9799 | ret = io_uring_install_fd(ctx, file); |
9800 | if (ret < 0) { | |
9801 | /* fput will clean it up */ | |
9802 | fput(file); | |
9803 | return ret; | |
9804 | } | |
044c1ab3 | 9805 | |
c826bd7a | 9806 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9807 | return ret; |
9808 | err: | |
9809 | io_ring_ctx_wait_and_kill(ctx); | |
9810 | return ret; | |
9811 | } | |
9812 | ||
9813 | /* | |
9814 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9815 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9816 | * params structure passed in. | |
9817 | */ | |
9818 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9819 | { | |
9820 | struct io_uring_params p; | |
2b188cc1 JA |
9821 | int i; |
9822 | ||
9823 | if (copy_from_user(&p, params, sizeof(p))) | |
9824 | return -EFAULT; | |
9825 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9826 | if (p.resv[i]) | |
9827 | return -EINVAL; | |
9828 | } | |
9829 | ||
6c271ce2 | 9830 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9831 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9832 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9833 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9834 | return -EINVAL; |
9835 | ||
7f13657d | 9836 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9837 | } |
9838 | ||
9839 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9840 | struct io_uring_params __user *, params) | |
9841 | { | |
9842 | return io_uring_setup(entries, params); | |
9843 | } | |
9844 | ||
66f4af93 JA |
9845 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
9846 | { | |
9847 | struct io_uring_probe *p; | |
9848 | size_t size; | |
9849 | int i, ret; | |
9850 | ||
9851 | size = struct_size(p, ops, nr_args); | |
9852 | if (size == SIZE_MAX) | |
9853 | return -EOVERFLOW; | |
9854 | p = kzalloc(size, GFP_KERNEL); | |
9855 | if (!p) | |
9856 | return -ENOMEM; | |
9857 | ||
9858 | ret = -EFAULT; | |
9859 | if (copy_from_user(p, arg, size)) | |
9860 | goto out; | |
9861 | ret = -EINVAL; | |
9862 | if (memchr_inv(p, 0, size)) | |
9863 | goto out; | |
9864 | ||
9865 | p->last_op = IORING_OP_LAST - 1; | |
9866 | if (nr_args > IORING_OP_LAST) | |
9867 | nr_args = IORING_OP_LAST; | |
9868 | ||
9869 | for (i = 0; i < nr_args; i++) { | |
9870 | p->ops[i].op = i; | |
9871 | if (!io_op_defs[i].not_supported) | |
9872 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
9873 | } | |
9874 | p->ops_len = i; | |
9875 | ||
9876 | ret = 0; | |
9877 | if (copy_to_user(arg, p, size)) | |
9878 | ret = -EFAULT; | |
9879 | out: | |
9880 | kfree(p); | |
9881 | return ret; | |
9882 | } | |
9883 | ||
071698e1 JA |
9884 | static int io_register_personality(struct io_ring_ctx *ctx) |
9885 | { | |
4379bf8b | 9886 | const struct cred *creds; |
61cf9370 | 9887 | u32 id; |
1e6fa521 | 9888 | int ret; |
071698e1 | 9889 | |
4379bf8b | 9890 | creds = get_current_cred(); |
1e6fa521 | 9891 | |
61cf9370 MWO |
9892 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
9893 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
9894 | if (ret < 0) { |
9895 | put_cred(creds); | |
9896 | return ret; | |
9897 | } | |
9898 | return id; | |
071698e1 JA |
9899 | } |
9900 | ||
21b55dbc SG |
9901 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
9902 | unsigned int nr_args) | |
9903 | { | |
9904 | struct io_uring_restriction *res; | |
9905 | size_t size; | |
9906 | int i, ret; | |
9907 | ||
7e84e1c7 SG |
9908 | /* Restrictions allowed only if rings started disabled */ |
9909 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9910 | return -EBADFD; | |
9911 | ||
21b55dbc | 9912 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 9913 | if (ctx->restrictions.registered) |
21b55dbc SG |
9914 | return -EBUSY; |
9915 | ||
9916 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
9917 | return -EINVAL; | |
9918 | ||
9919 | size = array_size(nr_args, sizeof(*res)); | |
9920 | if (size == SIZE_MAX) | |
9921 | return -EOVERFLOW; | |
9922 | ||
9923 | res = memdup_user(arg, size); | |
9924 | if (IS_ERR(res)) | |
9925 | return PTR_ERR(res); | |
9926 | ||
9927 | ret = 0; | |
9928 | ||
9929 | for (i = 0; i < nr_args; i++) { | |
9930 | switch (res[i].opcode) { | |
9931 | case IORING_RESTRICTION_REGISTER_OP: | |
9932 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
9933 | ret = -EINVAL; | |
9934 | goto out; | |
9935 | } | |
9936 | ||
9937 | __set_bit(res[i].register_op, | |
9938 | ctx->restrictions.register_op); | |
9939 | break; | |
9940 | case IORING_RESTRICTION_SQE_OP: | |
9941 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
9942 | ret = -EINVAL; | |
9943 | goto out; | |
9944 | } | |
9945 | ||
9946 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
9947 | break; | |
9948 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
9949 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
9950 | break; | |
9951 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
9952 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
9953 | break; | |
9954 | default: | |
9955 | ret = -EINVAL; | |
9956 | goto out; | |
9957 | } | |
9958 | } | |
9959 | ||
9960 | out: | |
9961 | /* Reset all restrictions if an error happened */ | |
9962 | if (ret != 0) | |
9963 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
9964 | else | |
7e84e1c7 | 9965 | ctx->restrictions.registered = true; |
21b55dbc SG |
9966 | |
9967 | kfree(res); | |
9968 | return ret; | |
9969 | } | |
9970 | ||
7e84e1c7 SG |
9971 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
9972 | { | |
9973 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
9974 | return -EBADFD; | |
9975 | ||
9976 | if (ctx->restrictions.registered) | |
9977 | ctx->restricted = 1; | |
9978 | ||
0298ef96 PB |
9979 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
9980 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
9981 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
9982 | return 0; |
9983 | } | |
9984 | ||
fdecb662 | 9985 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 9986 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
9987 | unsigned nr_args) |
9988 | { | |
9989 | __u32 tmp; | |
9990 | int err; | |
9991 | ||
c3bdad02 PB |
9992 | if (up->resv) |
9993 | return -EINVAL; | |
98f0b3b4 PB |
9994 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
9995 | return -EOVERFLOW; | |
9996 | err = io_rsrc_node_switch_start(ctx); | |
9997 | if (err) | |
9998 | return err; | |
9999 | ||
fdecb662 PB |
10000 | switch (type) { |
10001 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10002 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10003 | case IORING_RSRC_BUFFER: |
10004 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10005 | } |
10006 | return -EINVAL; | |
10007 | } | |
10008 | ||
c3bdad02 PB |
10009 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10010 | unsigned nr_args) | |
98f0b3b4 | 10011 | { |
c3bdad02 | 10012 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10013 | |
10014 | if (!nr_args) | |
10015 | return -EINVAL; | |
c3bdad02 PB |
10016 | memset(&up, 0, sizeof(up)); |
10017 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10018 | return -EFAULT; | |
10019 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10020 | } | |
10021 | ||
10022 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10023 | unsigned size, unsigned type) |
c3bdad02 PB |
10024 | { |
10025 | struct io_uring_rsrc_update2 up; | |
10026 | ||
10027 | if (size != sizeof(up)) | |
10028 | return -EINVAL; | |
98f0b3b4 PB |
10029 | if (copy_from_user(&up, arg, sizeof(up))) |
10030 | return -EFAULT; | |
992da01a | 10031 | if (!up.nr || up.resv) |
98f0b3b4 | 10032 | return -EINVAL; |
992da01a | 10033 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10034 | } |
10035 | ||
792e3582 | 10036 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10037 | unsigned int size, unsigned int type) |
792e3582 PB |
10038 | { |
10039 | struct io_uring_rsrc_register rr; | |
10040 | ||
10041 | /* keep it extendible */ | |
10042 | if (size != sizeof(rr)) | |
10043 | return -EINVAL; | |
10044 | ||
10045 | memset(&rr, 0, sizeof(rr)); | |
10046 | if (copy_from_user(&rr, arg, size)) | |
10047 | return -EFAULT; | |
992da01a | 10048 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10049 | return -EINVAL; |
10050 | ||
992da01a | 10051 | switch (type) { |
792e3582 PB |
10052 | case IORING_RSRC_FILE: |
10053 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10054 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10055 | case IORING_RSRC_BUFFER: |
10056 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10057 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10058 | } |
10059 | return -EINVAL; | |
10060 | } | |
10061 | ||
fe76421d JA |
10062 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10063 | unsigned len) | |
10064 | { | |
10065 | struct io_uring_task *tctx = current->io_uring; | |
10066 | cpumask_var_t new_mask; | |
10067 | int ret; | |
10068 | ||
10069 | if (!tctx || !tctx->io_wq) | |
10070 | return -EINVAL; | |
10071 | ||
10072 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10073 | return -ENOMEM; | |
10074 | ||
10075 | cpumask_clear(new_mask); | |
10076 | if (len > cpumask_size()) | |
10077 | len = cpumask_size(); | |
10078 | ||
10079 | if (copy_from_user(new_mask, arg, len)) { | |
10080 | free_cpumask_var(new_mask); | |
10081 | return -EFAULT; | |
10082 | } | |
10083 | ||
10084 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10085 | free_cpumask_var(new_mask); | |
10086 | return ret; | |
10087 | } | |
10088 | ||
10089 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10090 | { | |
10091 | struct io_uring_task *tctx = current->io_uring; | |
10092 | ||
10093 | if (!tctx || !tctx->io_wq) | |
10094 | return -EINVAL; | |
10095 | ||
10096 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10097 | } | |
10098 | ||
071698e1 JA |
10099 | static bool io_register_op_must_quiesce(int op) |
10100 | { | |
10101 | switch (op) { | |
bd54b6fe BM |
10102 | case IORING_REGISTER_BUFFERS: |
10103 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10104 | case IORING_REGISTER_FILES: |
071698e1 JA |
10105 | case IORING_UNREGISTER_FILES: |
10106 | case IORING_REGISTER_FILES_UPDATE: | |
10107 | case IORING_REGISTER_PROBE: | |
10108 | case IORING_REGISTER_PERSONALITY: | |
10109 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10110 | case IORING_REGISTER_FILES2: |
10111 | case IORING_REGISTER_FILES_UPDATE2: | |
10112 | case IORING_REGISTER_BUFFERS2: | |
10113 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10114 | case IORING_REGISTER_IOWQ_AFF: |
10115 | case IORING_UNREGISTER_IOWQ_AFF: | |
071698e1 JA |
10116 | return false; |
10117 | default: | |
10118 | return true; | |
10119 | } | |
10120 | } | |
10121 | ||
e73c5c7c PB |
10122 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10123 | { | |
10124 | long ret; | |
10125 | ||
10126 | percpu_ref_kill(&ctx->refs); | |
10127 | ||
10128 | /* | |
10129 | * Drop uring mutex before waiting for references to exit. If another | |
10130 | * thread is currently inside io_uring_enter() it might need to grab the | |
10131 | * uring_lock to make progress. If we hold it here across the drain | |
10132 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10133 | * no new references will come in after we've killed the percpu ref. | |
10134 | */ | |
10135 | mutex_unlock(&ctx->uring_lock); | |
10136 | do { | |
10137 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10138 | if (!ret) | |
10139 | break; | |
10140 | ret = io_run_task_work_sig(); | |
10141 | } while (ret >= 0); | |
10142 | mutex_lock(&ctx->uring_lock); | |
10143 | ||
10144 | if (ret) | |
10145 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10146 | return ret; | |
10147 | } | |
10148 | ||
edafccee JA |
10149 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10150 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10151 | __releases(ctx->uring_lock) |
10152 | __acquires(ctx->uring_lock) | |
edafccee JA |
10153 | { |
10154 | int ret; | |
10155 | ||
35fa71a0 JA |
10156 | /* |
10157 | * We're inside the ring mutex, if the ref is already dying, then | |
10158 | * someone else killed the ctx or is already going through | |
10159 | * io_uring_register(). | |
10160 | */ | |
10161 | if (percpu_ref_is_dying(&ctx->refs)) | |
10162 | return -ENXIO; | |
10163 | ||
75c4021a PB |
10164 | if (ctx->restricted) { |
10165 | if (opcode >= IORING_REGISTER_LAST) | |
10166 | return -EINVAL; | |
10167 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10168 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10169 | return -EACCES; | |
10170 | } | |
10171 | ||
071698e1 | 10172 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10173 | ret = io_ctx_quiesce(ctx); |
10174 | if (ret) | |
f70865db | 10175 | return ret; |
05f3fb3c | 10176 | } |
edafccee JA |
10177 | |
10178 | switch (opcode) { | |
10179 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10180 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10181 | break; |
10182 | case IORING_UNREGISTER_BUFFERS: | |
10183 | ret = -EINVAL; | |
10184 | if (arg || nr_args) | |
10185 | break; | |
0a96bbe4 | 10186 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10187 | break; |
6b06314c | 10188 | case IORING_REGISTER_FILES: |
792e3582 | 10189 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10190 | break; |
10191 | case IORING_UNREGISTER_FILES: | |
10192 | ret = -EINVAL; | |
10193 | if (arg || nr_args) | |
10194 | break; | |
10195 | ret = io_sqe_files_unregister(ctx); | |
10196 | break; | |
c3a31e60 | 10197 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10198 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10199 | break; |
9b402849 | 10200 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10201 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10202 | ret = -EINVAL; |
10203 | if (nr_args != 1) | |
10204 | break; | |
10205 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10206 | if (ret) |
10207 | break; | |
10208 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10209 | ctx->eventfd_async = 1; | |
10210 | else | |
10211 | ctx->eventfd_async = 0; | |
9b402849 JA |
10212 | break; |
10213 | case IORING_UNREGISTER_EVENTFD: | |
10214 | ret = -EINVAL; | |
10215 | if (arg || nr_args) | |
10216 | break; | |
10217 | ret = io_eventfd_unregister(ctx); | |
10218 | break; | |
66f4af93 JA |
10219 | case IORING_REGISTER_PROBE: |
10220 | ret = -EINVAL; | |
10221 | if (!arg || nr_args > 256) | |
10222 | break; | |
10223 | ret = io_probe(ctx, arg, nr_args); | |
10224 | break; | |
071698e1 JA |
10225 | case IORING_REGISTER_PERSONALITY: |
10226 | ret = -EINVAL; | |
10227 | if (arg || nr_args) | |
10228 | break; | |
10229 | ret = io_register_personality(ctx); | |
10230 | break; | |
10231 | case IORING_UNREGISTER_PERSONALITY: | |
10232 | ret = -EINVAL; | |
10233 | if (arg) | |
10234 | break; | |
10235 | ret = io_unregister_personality(ctx, nr_args); | |
10236 | break; | |
7e84e1c7 SG |
10237 | case IORING_REGISTER_ENABLE_RINGS: |
10238 | ret = -EINVAL; | |
10239 | if (arg || nr_args) | |
10240 | break; | |
10241 | ret = io_register_enable_rings(ctx); | |
10242 | break; | |
21b55dbc SG |
10243 | case IORING_REGISTER_RESTRICTIONS: |
10244 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10245 | break; | |
992da01a PB |
10246 | case IORING_REGISTER_FILES2: |
10247 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10248 | break; | |
10249 | case IORING_REGISTER_FILES_UPDATE2: | |
10250 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10251 | IORING_RSRC_FILE); | |
10252 | break; | |
10253 | case IORING_REGISTER_BUFFERS2: | |
10254 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10255 | break; |
992da01a PB |
10256 | case IORING_REGISTER_BUFFERS_UPDATE: |
10257 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10258 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10259 | break; |
fe76421d JA |
10260 | case IORING_REGISTER_IOWQ_AFF: |
10261 | ret = -EINVAL; | |
10262 | if (!arg || !nr_args) | |
10263 | break; | |
10264 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10265 | break; | |
10266 | case IORING_UNREGISTER_IOWQ_AFF: | |
10267 | ret = -EINVAL; | |
10268 | if (arg || nr_args) | |
10269 | break; | |
10270 | ret = io_unregister_iowq_aff(ctx); | |
10271 | break; | |
edafccee JA |
10272 | default: |
10273 | ret = -EINVAL; | |
10274 | break; | |
10275 | } | |
10276 | ||
071698e1 | 10277 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10278 | /* bring the ctx back to life */ |
05f3fb3c | 10279 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10280 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10281 | } |
edafccee JA |
10282 | return ret; |
10283 | } | |
10284 | ||
10285 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10286 | void __user *, arg, unsigned int, nr_args) | |
10287 | { | |
10288 | struct io_ring_ctx *ctx; | |
10289 | long ret = -EBADF; | |
10290 | struct fd f; | |
10291 | ||
10292 | f = fdget(fd); | |
10293 | if (!f.file) | |
10294 | return -EBADF; | |
10295 | ||
10296 | ret = -EOPNOTSUPP; | |
10297 | if (f.file->f_op != &io_uring_fops) | |
10298 | goto out_fput; | |
10299 | ||
10300 | ctx = f.file->private_data; | |
10301 | ||
b6c23dd5 PB |
10302 | io_run_task_work(); |
10303 | ||
edafccee JA |
10304 | mutex_lock(&ctx->uring_lock); |
10305 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10306 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10307 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10308 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10309 | out_fput: |
10310 | fdput(f); | |
10311 | return ret; | |
10312 | } | |
10313 | ||
2b188cc1 JA |
10314 | static int __init io_uring_init(void) |
10315 | { | |
d7f62e82 SM |
10316 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10317 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10318 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10319 | } while (0) | |
10320 | ||
10321 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10322 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10323 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10324 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10325 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10326 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10327 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10328 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10329 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10330 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10331 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10332 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10333 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10334 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10335 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10336 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10337 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10338 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10339 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10340 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10341 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10342 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10343 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10344 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10345 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10346 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10347 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10348 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10349 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10350 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10351 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10352 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
d7f62e82 | 10353 | |
b0d658ec PB |
10354 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10355 | sizeof(struct io_uring_rsrc_update)); | |
10356 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10357 | sizeof(struct io_uring_rsrc_update2)); | |
10358 | /* should fit into one byte */ | |
10359 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
10360 | ||
d3656344 | 10361 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10362 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
16340eab | 10363 | |
91f245d5 JA |
10364 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10365 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10366 | return 0; |
10367 | }; | |
10368 | __initcall(io_uring_init); |