]>
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; | |
50c1df2b | 511 | u32 flags; |
ad8a48ac JA |
512 | }; |
513 | ||
8ed8d3c3 JA |
514 | struct io_accept { |
515 | struct file *file; | |
516 | struct sockaddr __user *addr; | |
517 | int __user *addr_len; | |
518 | int flags; | |
aaa4db12 | 519 | u32 file_slot; |
09952e3e | 520 | unsigned long nofile; |
8ed8d3c3 JA |
521 | }; |
522 | ||
523 | struct io_sync { | |
524 | struct file *file; | |
525 | loff_t len; | |
526 | loff_t off; | |
527 | int flags; | |
d63d1b5e | 528 | int mode; |
8ed8d3c3 JA |
529 | }; |
530 | ||
fbf23849 JA |
531 | struct io_cancel { |
532 | struct file *file; | |
533 | u64 addr; | |
534 | }; | |
535 | ||
b29472ee JA |
536 | struct io_timeout { |
537 | struct file *file; | |
bfe68a22 PB |
538 | u32 off; |
539 | u32 target_seq; | |
135fcde8 | 540 | struct list_head list; |
90cd7e42 PB |
541 | /* head of the link, used by linked timeouts only */ |
542 | struct io_kiocb *head; | |
89b263f6 JA |
543 | /* for linked completions */ |
544 | struct io_kiocb *prev; | |
b29472ee JA |
545 | }; |
546 | ||
0bdf7a2d PB |
547 | struct io_timeout_rem { |
548 | struct file *file; | |
549 | u64 addr; | |
9c8e11b3 PB |
550 | |
551 | /* timeout update */ | |
552 | struct timespec64 ts; | |
553 | u32 flags; | |
0bdf7a2d PB |
554 | }; |
555 | ||
9adbd45d JA |
556 | struct io_rw { |
557 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
558 | struct kiocb kiocb; | |
559 | u64 addr; | |
560 | u64 len; | |
561 | }; | |
562 | ||
3fbb51c1 JA |
563 | struct io_connect { |
564 | struct file *file; | |
565 | struct sockaddr __user *addr; | |
566 | int addr_len; | |
567 | }; | |
568 | ||
e47293fd JA |
569 | struct io_sr_msg { |
570 | struct file *file; | |
fddaface | 571 | union { |
4af3417a PB |
572 | struct compat_msghdr __user *umsg_compat; |
573 | struct user_msghdr __user *umsg; | |
574 | void __user *buf; | |
fddaface | 575 | }; |
e47293fd | 576 | int msg_flags; |
bcda7baa | 577 | int bgid; |
fddaface | 578 | size_t len; |
bcda7baa | 579 | struct io_buffer *kbuf; |
e47293fd JA |
580 | }; |
581 | ||
15b71abe JA |
582 | struct io_open { |
583 | struct file *file; | |
584 | int dfd; | |
b9445598 | 585 | u32 file_slot; |
15b71abe | 586 | struct filename *filename; |
c12cedf2 | 587 | struct open_how how; |
4022e7af | 588 | unsigned long nofile; |
15b71abe JA |
589 | }; |
590 | ||
269bbe5f | 591 | struct io_rsrc_update { |
05f3fb3c JA |
592 | struct file *file; |
593 | u64 arg; | |
594 | u32 nr_args; | |
595 | u32 offset; | |
596 | }; | |
597 | ||
4840e418 JA |
598 | struct io_fadvise { |
599 | struct file *file; | |
600 | u64 offset; | |
601 | u32 len; | |
602 | u32 advice; | |
603 | }; | |
604 | ||
c1ca757b JA |
605 | struct io_madvise { |
606 | struct file *file; | |
607 | u64 addr; | |
608 | u32 len; | |
609 | u32 advice; | |
610 | }; | |
611 | ||
3e4827b0 JA |
612 | struct io_epoll { |
613 | struct file *file; | |
614 | int epfd; | |
615 | int op; | |
616 | int fd; | |
617 | struct epoll_event event; | |
e47293fd JA |
618 | }; |
619 | ||
7d67af2c PB |
620 | struct io_splice { |
621 | struct file *file_out; | |
622 | struct file *file_in; | |
623 | loff_t off_out; | |
624 | loff_t off_in; | |
625 | u64 len; | |
626 | unsigned int flags; | |
627 | }; | |
628 | ||
ddf0322d JA |
629 | struct io_provide_buf { |
630 | struct file *file; | |
631 | __u64 addr; | |
38134ada | 632 | __u32 len; |
ddf0322d JA |
633 | __u32 bgid; |
634 | __u16 nbufs; | |
635 | __u16 bid; | |
636 | }; | |
637 | ||
1d9e1288 BM |
638 | struct io_statx { |
639 | struct file *file; | |
640 | int dfd; | |
641 | unsigned int mask; | |
642 | unsigned int flags; | |
e62753e4 | 643 | const char __user *filename; |
1d9e1288 BM |
644 | struct statx __user *buffer; |
645 | }; | |
646 | ||
36f4fa68 JA |
647 | struct io_shutdown { |
648 | struct file *file; | |
649 | int how; | |
650 | }; | |
651 | ||
80a261fd JA |
652 | struct io_rename { |
653 | struct file *file; | |
654 | int old_dfd; | |
655 | int new_dfd; | |
656 | struct filename *oldpath; | |
657 | struct filename *newpath; | |
658 | int flags; | |
659 | }; | |
660 | ||
14a1143b JA |
661 | struct io_unlink { |
662 | struct file *file; | |
663 | int dfd; | |
664 | int flags; | |
665 | struct filename *filename; | |
666 | }; | |
667 | ||
3ca405eb PB |
668 | struct io_completion { |
669 | struct file *file; | |
8c3f9cd1 | 670 | u32 cflags; |
3ca405eb PB |
671 | }; |
672 | ||
f499a021 JA |
673 | struct io_async_connect { |
674 | struct sockaddr_storage address; | |
675 | }; | |
676 | ||
03b1230c JA |
677 | struct io_async_msghdr { |
678 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
679 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
680 | struct iovec *free_iov; | |
03b1230c JA |
681 | struct sockaddr __user *uaddr; |
682 | struct msghdr msg; | |
b537916c | 683 | struct sockaddr_storage addr; |
03b1230c JA |
684 | }; |
685 | ||
f67676d1 JA |
686 | struct io_async_rw { |
687 | struct iovec fast_iov[UIO_FASTIOV]; | |
ff6165b2 JA |
688 | const struct iovec *free_iovec; |
689 | struct iov_iter iter; | |
227c0c96 | 690 | size_t bytes_done; |
bcf5a063 | 691 | struct wait_page_queue wpq; |
f67676d1 JA |
692 | }; |
693 | ||
6b47ee6e PB |
694 | enum { |
695 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
696 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
697 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
698 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
699 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 700 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
6b47ee6e | 701 | |
dddca226 | 702 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 703 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
704 | REQ_F_INFLIGHT_BIT, |
705 | REQ_F_CUR_POS_BIT, | |
706 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 707 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 708 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 709 | REQ_F_POLLED_BIT, |
bcda7baa | 710 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 711 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 712 | REQ_F_REISSUE_BIT, |
8c130827 | 713 | REQ_F_DONT_REISSUE_BIT, |
b8e64b53 | 714 | REQ_F_CREDS_BIT, |
20e60a38 | 715 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 716 | REQ_F_ARM_LTIMEOUT_BIT, |
7b29f92d | 717 | /* keep async read/write and isreg together and in order */ |
b191e2df PB |
718 | REQ_F_NOWAIT_READ_BIT, |
719 | REQ_F_NOWAIT_WRITE_BIT, | |
7b29f92d | 720 | REQ_F_ISREG_BIT, |
84557871 JA |
721 | |
722 | /* not a real bit, just to check we're not overflowing the space */ | |
723 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
724 | }; |
725 | ||
726 | enum { | |
727 | /* ctx owns file */ | |
728 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
729 | /* drain existing IO first */ | |
730 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
731 | /* linked sqes */ | |
732 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
733 | /* doesn't sever on completion < 0 */ | |
734 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
735 | /* IOSQE_ASYNC */ | |
736 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
737 | /* IOSQE_BUFFER_SELECT */ |
738 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
6b47ee6e | 739 | |
6b47ee6e | 740 | /* fail rest of links */ |
93d2bcd2 | 741 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 742 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
743 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
744 | /* read/write uses file position */ | |
745 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
746 | /* must not punt to workers */ | |
747 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 748 | /* has or had linked timeout */ |
6b47ee6e | 749 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
750 | /* needs cleanup */ |
751 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
752 | /* already went through poll handler */ |
753 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
754 | /* buffer already selected */ |
755 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
756 | /* completion is deferred through io_comp_state */ |
757 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
758 | /* caller should reissue async */ |
759 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
8c130827 PB |
760 | /* don't attempt request reissue, see io_rw_reissue() */ |
761 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), | |
7b29f92d | 762 | /* supports async reads */ |
b191e2df | 763 | REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT), |
7b29f92d | 764 | /* supports async writes */ |
b191e2df | 765 | REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT), |
7b29f92d JA |
766 | /* regular file */ |
767 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
768 | /* has creds assigned */ |
769 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
770 | /* skip refcounting if not set */ |
771 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
772 | /* there is a linked timeout that has to be armed */ |
773 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d7718a9d JA |
774 | }; |
775 | ||
776 | struct async_poll { | |
777 | struct io_poll_iocb poll; | |
807abcb0 | 778 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
779 | }; |
780 | ||
f237c30a | 781 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 782 | |
7cbf1722 | 783 | struct io_task_work { |
5b0a6acc PB |
784 | union { |
785 | struct io_wq_work_node node; | |
786 | struct llist_node fallback_node; | |
787 | }; | |
788 | io_req_tw_func_t func; | |
7cbf1722 JA |
789 | }; |
790 | ||
992da01a PB |
791 | enum { |
792 | IORING_RSRC_FILE = 0, | |
793 | IORING_RSRC_BUFFER = 1, | |
794 | }; | |
795 | ||
09bb8394 JA |
796 | /* |
797 | * NOTE! Each of the iocb union members has the file pointer | |
798 | * as the first entry in their struct definition. So you can | |
799 | * access the file pointer through any of the sub-structs, | |
800 | * or directly as just 'ki_filp' in this struct. | |
801 | */ | |
2b188cc1 | 802 | struct io_kiocb { |
221c5eb2 | 803 | union { |
09bb8394 | 804 | struct file *file; |
9adbd45d | 805 | struct io_rw rw; |
221c5eb2 | 806 | struct io_poll_iocb poll; |
9d805892 | 807 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
808 | struct io_accept accept; |
809 | struct io_sync sync; | |
fbf23849 | 810 | struct io_cancel cancel; |
b29472ee | 811 | struct io_timeout timeout; |
0bdf7a2d | 812 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 813 | struct io_connect connect; |
e47293fd | 814 | struct io_sr_msg sr_msg; |
15b71abe | 815 | struct io_open open; |
b5dba59e | 816 | struct io_close close; |
269bbe5f | 817 | struct io_rsrc_update rsrc_update; |
4840e418 | 818 | struct io_fadvise fadvise; |
c1ca757b | 819 | struct io_madvise madvise; |
3e4827b0 | 820 | struct io_epoll epoll; |
7d67af2c | 821 | struct io_splice splice; |
ddf0322d | 822 | struct io_provide_buf pbuf; |
1d9e1288 | 823 | struct io_statx statx; |
36f4fa68 | 824 | struct io_shutdown shutdown; |
80a261fd | 825 | struct io_rename rename; |
14a1143b | 826 | struct io_unlink unlink; |
3ca405eb PB |
827 | /* use only after cleaning per-op data, see io_clean_op() */ |
828 | struct io_completion compl; | |
221c5eb2 | 829 | }; |
2b188cc1 | 830 | |
e8c2bc1f JA |
831 | /* opcode allocated if it needs to store data for async defer */ |
832 | void *async_data; | |
d625c6ee | 833 | u8 opcode; |
65a6543d XW |
834 | /* polled IO has completed */ |
835 | u8 iopoll_completed; | |
2b188cc1 | 836 | |
4f4eeba8 | 837 | u16 buf_index; |
9cf7c104 | 838 | u32 result; |
4f4eeba8 | 839 | |
010e8e6b PB |
840 | struct io_ring_ctx *ctx; |
841 | unsigned int flags; | |
abc54d63 | 842 | atomic_t refs; |
010e8e6b PB |
843 | struct task_struct *task; |
844 | u64 user_data; | |
d7718a9d | 845 | |
f2f87370 | 846 | struct io_kiocb *link; |
269bbe5f | 847 | struct percpu_ref *fixed_rsrc_refs; |
fcb323cc | 848 | |
b303fe2e | 849 | /* used with ctx->iopoll_list with reads/writes */ |
010e8e6b | 850 | struct list_head inflight_entry; |
5b0a6acc | 851 | struct io_task_work io_task_work; |
010e8e6b PB |
852 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
853 | struct hlist_node hash_node; | |
854 | struct async_poll *apoll; | |
855 | struct io_wq_work work; | |
fe7e3257 | 856 | const struct cred *creds; |
c10d1f98 | 857 | |
eae071c9 PB |
858 | /* store used ubuf, so we can prevent reloading */ |
859 | struct io_mapped_ubuf *imu; | |
2b188cc1 | 860 | }; |
05589553 | 861 | |
13bf43f5 PB |
862 | struct io_tctx_node { |
863 | struct list_head ctx_node; | |
864 | struct task_struct *task; | |
13bf43f5 PB |
865 | struct io_ring_ctx *ctx; |
866 | }; | |
867 | ||
27dc8338 PB |
868 | struct io_defer_entry { |
869 | struct list_head list; | |
870 | struct io_kiocb *req; | |
9cf7c104 | 871 | u32 seq; |
2b188cc1 JA |
872 | }; |
873 | ||
d3656344 | 874 | struct io_op_def { |
d3656344 JA |
875 | /* needs req->file assigned */ |
876 | unsigned needs_file : 1; | |
d3656344 JA |
877 | /* hash wq insertion if file is a regular file */ |
878 | unsigned hash_reg_file : 1; | |
879 | /* unbound wq insertion if file is a non-regular file */ | |
880 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
881 | /* opcode is not supported by this kernel */ |
882 | unsigned not_supported : 1; | |
8a72758c JA |
883 | /* set if opcode supports polled "wait" */ |
884 | unsigned pollin : 1; | |
885 | unsigned pollout : 1; | |
bcda7baa JA |
886 | /* op supports buffer selection */ |
887 | unsigned buffer_select : 1; | |
26f0505a PB |
888 | /* do prep async if is going to be punted */ |
889 | unsigned needs_async_setup : 1; | |
27926b68 JA |
890 | /* should block plug */ |
891 | unsigned plug : 1; | |
e8c2bc1f JA |
892 | /* size of async data needed, if any */ |
893 | unsigned short async_size; | |
d3656344 JA |
894 | }; |
895 | ||
0918682b | 896 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
897 | [IORING_OP_NOP] = {}, |
898 | [IORING_OP_READV] = { | |
d3656344 JA |
899 | .needs_file = 1, |
900 | .unbound_nonreg_file = 1, | |
8a72758c | 901 | .pollin = 1, |
4d954c25 | 902 | .buffer_select = 1, |
26f0505a | 903 | .needs_async_setup = 1, |
27926b68 | 904 | .plug = 1, |
e8c2bc1f | 905 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 906 | }, |
0463b6c5 | 907 | [IORING_OP_WRITEV] = { |
d3656344 JA |
908 | .needs_file = 1, |
909 | .hash_reg_file = 1, | |
910 | .unbound_nonreg_file = 1, | |
8a72758c | 911 | .pollout = 1, |
26f0505a | 912 | .needs_async_setup = 1, |
27926b68 | 913 | .plug = 1, |
e8c2bc1f | 914 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 915 | }, |
0463b6c5 | 916 | [IORING_OP_FSYNC] = { |
d3656344 JA |
917 | .needs_file = 1, |
918 | }, | |
0463b6c5 | 919 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
920 | .needs_file = 1, |
921 | .unbound_nonreg_file = 1, | |
8a72758c | 922 | .pollin = 1, |
27926b68 | 923 | .plug = 1, |
e8c2bc1f | 924 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 925 | }, |
0463b6c5 | 926 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
927 | .needs_file = 1, |
928 | .hash_reg_file = 1, | |
929 | .unbound_nonreg_file = 1, | |
8a72758c | 930 | .pollout = 1, |
27926b68 | 931 | .plug = 1, |
e8c2bc1f | 932 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 933 | }, |
0463b6c5 | 934 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
935 | .needs_file = 1, |
936 | .unbound_nonreg_file = 1, | |
937 | }, | |
0463b6c5 PB |
938 | [IORING_OP_POLL_REMOVE] = {}, |
939 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
940 | .needs_file = 1, |
941 | }, | |
0463b6c5 | 942 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
943 | .needs_file = 1, |
944 | .unbound_nonreg_file = 1, | |
8a72758c | 945 | .pollout = 1, |
26f0505a | 946 | .needs_async_setup = 1, |
e8c2bc1f | 947 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 948 | }, |
0463b6c5 | 949 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
950 | .needs_file = 1, |
951 | .unbound_nonreg_file = 1, | |
8a72758c | 952 | .pollin = 1, |
52de1fe1 | 953 | .buffer_select = 1, |
26f0505a | 954 | .needs_async_setup = 1, |
e8c2bc1f | 955 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 956 | }, |
0463b6c5 | 957 | [IORING_OP_TIMEOUT] = { |
e8c2bc1f | 958 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 959 | }, |
9c8e11b3 PB |
960 | [IORING_OP_TIMEOUT_REMOVE] = { |
961 | /* used by timeout updates' prep() */ | |
9c8e11b3 | 962 | }, |
0463b6c5 | 963 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
964 | .needs_file = 1, |
965 | .unbound_nonreg_file = 1, | |
8a72758c | 966 | .pollin = 1, |
d3656344 | 967 | }, |
0463b6c5 PB |
968 | [IORING_OP_ASYNC_CANCEL] = {}, |
969 | [IORING_OP_LINK_TIMEOUT] = { | |
e8c2bc1f | 970 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 971 | }, |
0463b6c5 | 972 | [IORING_OP_CONNECT] = { |
d3656344 JA |
973 | .needs_file = 1, |
974 | .unbound_nonreg_file = 1, | |
8a72758c | 975 | .pollout = 1, |
26f0505a | 976 | .needs_async_setup = 1, |
e8c2bc1f | 977 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 978 | }, |
0463b6c5 | 979 | [IORING_OP_FALLOCATE] = { |
d3656344 | 980 | .needs_file = 1, |
d3656344 | 981 | }, |
44526bed JA |
982 | [IORING_OP_OPENAT] = {}, |
983 | [IORING_OP_CLOSE] = {}, | |
984 | [IORING_OP_FILES_UPDATE] = {}, | |
985 | [IORING_OP_STATX] = {}, | |
0463b6c5 | 986 | [IORING_OP_READ] = { |
3a6820f2 JA |
987 | .needs_file = 1, |
988 | .unbound_nonreg_file = 1, | |
8a72758c | 989 | .pollin = 1, |
bcda7baa | 990 | .buffer_select = 1, |
27926b68 | 991 | .plug = 1, |
e8c2bc1f | 992 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 993 | }, |
0463b6c5 | 994 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
995 | .needs_file = 1, |
996 | .unbound_nonreg_file = 1, | |
8a72758c | 997 | .pollout = 1, |
27926b68 | 998 | .plug = 1, |
e8c2bc1f | 999 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1000 | }, |
0463b6c5 | 1001 | [IORING_OP_FADVISE] = { |
4840e418 | 1002 | .needs_file = 1, |
c1ca757b | 1003 | }, |
44526bed | 1004 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1005 | [IORING_OP_SEND] = { |
fddaface JA |
1006 | .needs_file = 1, |
1007 | .unbound_nonreg_file = 1, | |
8a72758c | 1008 | .pollout = 1, |
fddaface | 1009 | }, |
0463b6c5 | 1010 | [IORING_OP_RECV] = { |
fddaface JA |
1011 | .needs_file = 1, |
1012 | .unbound_nonreg_file = 1, | |
8a72758c | 1013 | .pollin = 1, |
bcda7baa | 1014 | .buffer_select = 1, |
fddaface | 1015 | }, |
0463b6c5 | 1016 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1017 | }, |
3e4827b0 JA |
1018 | [IORING_OP_EPOLL_CTL] = { |
1019 | .unbound_nonreg_file = 1, | |
3e4827b0 | 1020 | }, |
7d67af2c PB |
1021 | [IORING_OP_SPLICE] = { |
1022 | .needs_file = 1, | |
1023 | .hash_reg_file = 1, | |
1024 | .unbound_nonreg_file = 1, | |
ddf0322d JA |
1025 | }, |
1026 | [IORING_OP_PROVIDE_BUFFERS] = {}, | |
067524e9 | 1027 | [IORING_OP_REMOVE_BUFFERS] = {}, |
f2a8d5c7 PB |
1028 | [IORING_OP_TEE] = { |
1029 | .needs_file = 1, | |
1030 | .hash_reg_file = 1, | |
1031 | .unbound_nonreg_file = 1, | |
1032 | }, | |
36f4fa68 JA |
1033 | [IORING_OP_SHUTDOWN] = { |
1034 | .needs_file = 1, | |
1035 | }, | |
44526bed JA |
1036 | [IORING_OP_RENAMEAT] = {}, |
1037 | [IORING_OP_UNLINKAT] = {}, | |
d3656344 JA |
1038 | }; |
1039 | ||
0756a869 PB |
1040 | /* requests with any of those set should undergo io_disarm_next() */ |
1041 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1042 | ||
7a612350 | 1043 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1044 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1045 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1046 | struct task_struct *task, | |
3dd0c97a | 1047 | bool cancel_all); |
78cc687b | 1048 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1049 | |
d4d19c19 PB |
1050 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1051 | long res, unsigned int cflags); | |
ec9c02ad | 1052 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1053 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1054 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1055 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1056 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1057 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1058 | unsigned nr_args); |
68fb8979 | 1059 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1060 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1061 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1062 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1063 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1064 | |
907d1df3 | 1065 | static void io_req_task_queue(struct io_kiocb *req); |
2a2758f2 | 1066 | static void io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1067 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1068 | |
b9445598 PB |
1069 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1070 | unsigned int issue_flags, u32 slot_index); | |
1071 | ||
2b188cc1 JA |
1072 | static struct kmem_cache *req_cachep; |
1073 | ||
0918682b | 1074 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1075 | |
1076 | struct sock *io_uring_get_socket(struct file *file) | |
1077 | { | |
1078 | #if defined(CONFIG_UNIX) | |
1079 | if (file->f_op == &io_uring_fops) { | |
1080 | struct io_ring_ctx *ctx = file->private_data; | |
1081 | ||
1082 | return ctx->ring_sock->sk; | |
1083 | } | |
1084 | #endif | |
1085 | return NULL; | |
1086 | } | |
1087 | EXPORT_SYMBOL(io_uring_get_socket); | |
1088 | ||
f237c30a PB |
1089 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1090 | { | |
1091 | if (!*locked) { | |
1092 | mutex_lock(&ctx->uring_lock); | |
1093 | *locked = true; | |
1094 | } | |
1095 | } | |
1096 | ||
f2f87370 PB |
1097 | #define io_for_each_link(pos, head) \ |
1098 | for (pos = (head); pos; pos = pos->link) | |
1099 | ||
21c843d5 PB |
1100 | /* |
1101 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1102 | * see commit f958d7b528b1 for details. | |
1103 | */ | |
1104 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1105 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1106 | ||
1107 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1108 | { | |
20e60a38 | 1109 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1110 | return atomic_inc_not_zero(&req->refs); |
1111 | } | |
1112 | ||
21c843d5 PB |
1113 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1114 | { | |
20e60a38 PB |
1115 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1116 | return true; | |
1117 | ||
21c843d5 PB |
1118 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1119 | return atomic_dec_and_test(&req->refs); | |
1120 | } | |
1121 | ||
1122 | static inline void req_ref_put(struct io_kiocb *req) | |
1123 | { | |
20e60a38 | 1124 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1125 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
1126 | } | |
1127 | ||
1128 | static inline void req_ref_get(struct io_kiocb *req) | |
1129 | { | |
20e60a38 | 1130 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1131 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1132 | atomic_inc(&req->refs); | |
1133 | } | |
1134 | ||
48dcd38d | 1135 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1136 | { |
1137 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1138 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1139 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1140 | } |
1141 | } | |
1142 | ||
48dcd38d PB |
1143 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1144 | { | |
1145 | __io_req_set_refcount(req, 1); | |
1146 | } | |
1147 | ||
b895c9a6 | 1148 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
36f72fe2 PB |
1149 | { |
1150 | struct io_ring_ctx *ctx = req->ctx; | |
1151 | ||
269bbe5f | 1152 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1153 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
269bbe5f | 1154 | percpu_ref_get(req->fixed_rsrc_refs); |
36f72fe2 PB |
1155 | } |
1156 | } | |
1157 | ||
f70865db PB |
1158 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
1159 | { | |
1160 | bool got = percpu_ref_tryget(ref); | |
1161 | ||
1162 | /* already at zero, wait for ->release() */ | |
1163 | if (!got) | |
1164 | wait_for_completion(compl); | |
1165 | percpu_ref_resurrect(ref); | |
1166 | if (got) | |
1167 | percpu_ref_put(ref); | |
1168 | } | |
1169 | ||
3dd0c97a PB |
1170 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1171 | bool cancel_all) | |
08d23634 PB |
1172 | { |
1173 | struct io_kiocb *req; | |
1174 | ||
68207680 | 1175 | if (task && head->task != task) |
08d23634 | 1176 | return false; |
3dd0c97a | 1177 | if (cancel_all) |
08d23634 PB |
1178 | return true; |
1179 | ||
1180 | io_for_each_link(req, head) { | |
b05a1bcd | 1181 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1182 | return true; |
08d23634 PB |
1183 | } |
1184 | return false; | |
1185 | } | |
1186 | ||
93d2bcd2 | 1187 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1188 | { |
93d2bcd2 | 1189 | req->flags |= REQ_F_FAIL; |
c40f6379 | 1190 | } |
4a38aed2 | 1191 | |
a8295b98 HX |
1192 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1193 | { | |
1194 | req_set_fail(req); | |
1195 | req->result = res; | |
1196 | } | |
1197 | ||
2b188cc1 JA |
1198 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
1199 | { | |
1200 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1201 | ||
0f158b4c | 1202 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1203 | } |
1204 | ||
8eb7e2d0 PB |
1205 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1206 | { | |
1207 | return !req->timeout.off; | |
1208 | } | |
1209 | ||
f56165e6 PB |
1210 | static void io_fallback_req_func(struct work_struct *work) |
1211 | { | |
1212 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1213 | fallback_work.work); | |
1214 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1215 | struct io_kiocb *req, *tmp; | |
f237c30a | 1216 | bool locked = false; |
f56165e6 PB |
1217 | |
1218 | percpu_ref_get(&ctx->refs); | |
1219 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1220 | req->io_task_work.func(req, &locked); |
5636c00d | 1221 | |
f237c30a PB |
1222 | if (locked) { |
1223 | if (ctx->submit_state.compl_nr) | |
1224 | io_submit_flush_completions(ctx); | |
1225 | mutex_unlock(&ctx->uring_lock); | |
1226 | } | |
f56165e6 | 1227 | percpu_ref_put(&ctx->refs); |
f237c30a | 1228 | |
f56165e6 PB |
1229 | } |
1230 | ||
2b188cc1 JA |
1231 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
1232 | { | |
1233 | struct io_ring_ctx *ctx; | |
78076bb6 | 1234 | int hash_bits; |
2b188cc1 JA |
1235 | |
1236 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1237 | if (!ctx) | |
1238 | return NULL; | |
1239 | ||
78076bb6 JA |
1240 | /* |
1241 | * Use 5 bits less than the max cq entries, that should give us around | |
1242 | * 32 entries per hash list if totally full and uniformly spread. | |
1243 | */ | |
1244 | hash_bits = ilog2(p->cq_entries); | |
1245 | hash_bits -= 5; | |
1246 | if (hash_bits <= 0) | |
1247 | hash_bits = 1; | |
1248 | ctx->cancel_hash_bits = hash_bits; | |
1249 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1250 | GFP_KERNEL); | |
1251 | if (!ctx->cancel_hash) | |
1252 | goto err; | |
1253 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1254 | ||
6224843d PB |
1255 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1256 | if (!ctx->dummy_ubuf) | |
1257 | goto err; | |
1258 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1259 | ctx->dummy_ubuf->ubuf = -1UL; | |
1260 | ||
21482896 | 1261 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1262 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1263 | goto err; | |
2b188cc1 JA |
1264 | |
1265 | ctx->flags = p->flags; | |
90554200 | 1266 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1267 | INIT_LIST_HEAD(&ctx->sqd_list); |
311997b3 | 1268 | init_waitqueue_head(&ctx->poll_wait); |
1d7bb1d5 | 1269 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
0f158b4c | 1270 | init_completion(&ctx->ref_comp); |
9e15c3a0 | 1271 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
61cf9370 | 1272 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1273 | mutex_init(&ctx->uring_lock); |
311997b3 | 1274 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1275 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1276 | spin_lock_init(&ctx->timeout_lock); |
540e32a0 | 1277 | INIT_LIST_HEAD(&ctx->iopoll_list); |
de0617e4 | 1278 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1279 | INIT_LIST_HEAD(&ctx->timeout_list); |
d67d2263 BM |
1280 | spin_lock_init(&ctx->rsrc_ref_lock); |
1281 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1282 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1283 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1284 | INIT_LIST_HEAD(&ctx->tctx_list); |
cd0ca2e0 | 1285 | INIT_LIST_HEAD(&ctx->submit_state.free_list); |
d0acdee2 | 1286 | INIT_LIST_HEAD(&ctx->locked_free_list); |
9011bf9a | 1287 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
2b188cc1 | 1288 | return ctx; |
206aefde | 1289 | err: |
6224843d | 1290 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1291 | kfree(ctx->cancel_hash); |
206aefde JA |
1292 | kfree(ctx); |
1293 | return NULL; | |
2b188cc1 JA |
1294 | } |
1295 | ||
8f6ed49a PB |
1296 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1297 | { | |
1298 | struct io_rings *r = ctx->rings; | |
1299 | ||
1300 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1301 | ctx->cq_extra--; | |
1302 | } | |
1303 | ||
9cf7c104 | 1304 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1305 | { |
2bc9930e JA |
1306 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1307 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1308 | |
8f6ed49a | 1309 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1310 | } |
de0617e4 | 1311 | |
9d858b21 | 1312 | return false; |
de0617e4 JA |
1313 | } |
1314 | ||
c97d8a0f PB |
1315 | #define FFS_ASYNC_READ 0x1UL |
1316 | #define FFS_ASYNC_WRITE 0x2UL | |
1317 | #ifdef CONFIG_64BIT | |
1318 | #define FFS_ISREG 0x4UL | |
1319 | #else | |
1320 | #define FFS_ISREG 0x0UL | |
1321 | #endif | |
1322 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) | |
1323 | ||
1324 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1325 | { | |
1326 | return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE); | |
1327 | } | |
1328 | ||
ce3d5aae PB |
1329 | static void io_req_track_inflight(struct io_kiocb *req) |
1330 | { | |
ce3d5aae | 1331 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1332 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1333 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1334 | } |
1335 | } | |
1336 | ||
906c6caa PB |
1337 | static inline void io_unprep_linked_timeout(struct io_kiocb *req) |
1338 | { | |
1339 | req->flags &= ~REQ_F_LINK_TIMEOUT; | |
1340 | } | |
1341 | ||
fd08e530 PB |
1342 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1343 | { | |
906c6caa PB |
1344 | if (WARN_ON_ONCE(!req->link)) |
1345 | return NULL; | |
1346 | ||
4d13d1a4 PB |
1347 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1348 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1349 | |
1350 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1351 | io_req_set_refcount(req); |
4d13d1a4 PB |
1352 | __io_req_set_refcount(req->link, 2); |
1353 | return req->link; | |
fd08e530 PB |
1354 | } |
1355 | ||
1356 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1357 | { | |
4d13d1a4 | 1358 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1359 | return NULL; |
1360 | return __io_prep_linked_timeout(req); | |
1361 | } | |
1362 | ||
1e6fa521 JA |
1363 | static void io_prep_async_work(struct io_kiocb *req) |
1364 | { | |
1365 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1366 | struct io_ring_ctx *ctx = req->ctx; |
1367 | ||
b8e64b53 PB |
1368 | if (!(req->flags & REQ_F_CREDS)) { |
1369 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1370 | req->creds = get_current_cred(); |
b8e64b53 | 1371 | } |
003e8dcc | 1372 | |
e1d675df PB |
1373 | req->work.list.next = NULL; |
1374 | req->work.flags = 0; | |
feaadc4f PB |
1375 | if (req->flags & REQ_F_FORCE_ASYNC) |
1376 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1377 | ||
1e6fa521 JA |
1378 | if (req->flags & REQ_F_ISREG) { |
1379 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1380 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1381 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1382 | if (def->unbound_nonreg_file) |
1383 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1384 | } | |
e1d675df PB |
1385 | |
1386 | switch (req->opcode) { | |
1387 | case IORING_OP_SPLICE: | |
1388 | case IORING_OP_TEE: | |
e1d675df PB |
1389 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1390 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1391 | break; | |
1392 | } | |
561fb04a | 1393 | } |
cccf0ee8 | 1394 | |
cbdcb435 | 1395 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1396 | { |
cbdcb435 | 1397 | struct io_kiocb *cur; |
54a91f3b | 1398 | |
44eff40a PB |
1399 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1400 | struct io_ring_ctx *ctx = req->ctx; | |
1401 | ||
79ebeaee | 1402 | spin_lock(&ctx->completion_lock); |
44eff40a PB |
1403 | io_for_each_link(cur, req) |
1404 | io_prep_async_work(cur); | |
79ebeaee | 1405 | spin_unlock(&ctx->completion_lock); |
44eff40a PB |
1406 | } else { |
1407 | io_for_each_link(cur, req) | |
1408 | io_prep_async_work(cur); | |
1409 | } | |
561fb04a JA |
1410 | } |
1411 | ||
f237c30a | 1412 | static void io_queue_async_work(struct io_kiocb *req, bool *locked) |
561fb04a | 1413 | { |
a197f664 | 1414 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1415 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1416 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1417 | |
f237c30a PB |
1418 | /* must not take the lock, NULL it as a precaution */ |
1419 | locked = NULL; | |
1420 | ||
3bfe6106 JA |
1421 | BUG_ON(!tctx); |
1422 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1423 | |
cbdcb435 PB |
1424 | /* init ->work of the whole link before punting */ |
1425 | io_prep_async_link(req); | |
991468dc JA |
1426 | |
1427 | /* | |
1428 | * Not expected to happen, but if we do have a bug where this _can_ | |
1429 | * happen, catch it here and ensure the request is marked as | |
1430 | * canceled. That will make io-wq go through the usual work cancel | |
1431 | * procedure rather than attempt to run this request (or create a new | |
1432 | * worker for it). | |
1433 | */ | |
1434 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1435 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1436 | ||
d07f1e8a PB |
1437 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
1438 | &req->work, req->flags); | |
ebf93667 | 1439 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1440 | if (link) |
1441 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1442 | } |
1443 | ||
1ee4160c | 1444 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1445 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1446 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1447 | { |
e8c2bc1f | 1448 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1449 | |
fd9c7bc5 | 1450 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
01cec8c1 PB |
1451 | atomic_set(&req->ctx->cq_timeouts, |
1452 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1453 | list_del_init(&req->timeout.list); |
d4d19c19 | 1454 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
91c2f697 | 1455 | io_put_req_deferred(req); |
5262f567 JA |
1456 | } |
1457 | } | |
1458 | ||
441b8a78 | 1459 | static void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1460 | { |
441b8a78 | 1461 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1462 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1463 | struct io_defer_entry, list); | |
de0617e4 | 1464 | |
9cf7c104 | 1465 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1466 | break; |
27dc8338 | 1467 | list_del_init(&de->list); |
907d1df3 | 1468 | io_req_task_queue(de->req); |
27dc8338 | 1469 | kfree(de); |
441b8a78 | 1470 | } |
04518945 PB |
1471 | } |
1472 | ||
360428f8 | 1473 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1474 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1475 | { |
441b8a78 | 1476 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1477 | |
79ebeaee | 1478 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1479 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1480 | u32 events_needed, events_got; |
360428f8 | 1481 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1482 | struct io_kiocb, timeout.list); |
de0617e4 | 1483 | |
8eb7e2d0 | 1484 | if (io_is_timeout_noseq(req)) |
360428f8 | 1485 | break; |
f010505b MDG |
1486 | |
1487 | /* | |
1488 | * Since seq can easily wrap around over time, subtract | |
1489 | * the last seq at which timeouts were flushed before comparing. | |
1490 | * Assuming not more than 2^31-1 events have happened since, | |
1491 | * these subtractions won't have wrapped, so we can check if | |
1492 | * target is in [last_seq, current_seq] by comparing the two. | |
1493 | */ | |
1494 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1495 | events_got = seq - ctx->cq_last_tm_flush; | |
1496 | if (events_got < events_needed) | |
360428f8 | 1497 | break; |
bfe68a22 | 1498 | |
135fcde8 | 1499 | list_del_init(&req->timeout.list); |
1ee4160c | 1500 | io_kill_timeout(req, 0); |
f18ee4cf | 1501 | } |
f010505b | 1502 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1503 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1504 | } |
5262f567 | 1505 | |
2335f6f5 | 1506 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1507 | { |
2335f6f5 PB |
1508 | if (ctx->off_timeout_used) |
1509 | io_flush_timeouts(ctx); | |
1510 | if (ctx->drain_active) | |
1511 | io_queue_deferred(ctx); | |
1512 | } | |
1513 | ||
1514 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) | |
1515 | { | |
1516 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) | |
1517 | __io_commit_cqring_flush(ctx); | |
ec30e04b PB |
1518 | /* order cqe stores with ring update */ |
1519 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
de0617e4 JA |
1520 | } |
1521 | ||
90554200 JA |
1522 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1523 | { | |
1524 | struct io_rings *r = ctx->rings; | |
1525 | ||
a566c556 | 1526 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1527 | } |
1528 | ||
888aae2e PB |
1529 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1530 | { | |
1531 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1532 | } | |
1533 | ||
d068b506 | 1534 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1535 | { |
75b28aff | 1536 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1537 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1538 | |
115e12e5 SB |
1539 | /* |
1540 | * writes to the cq entry need to come after reading head; the | |
1541 | * control dependency is enough as we're using WRITE_ONCE to | |
1542 | * fill the cq entry | |
1543 | */ | |
a566c556 | 1544 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1545 | return NULL; |
1546 | ||
888aae2e | 1547 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1548 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1549 | } |
1550 | ||
f2842ab5 JA |
1551 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1552 | { | |
44c769de | 1553 | if (likely(!ctx->cq_ev_fd)) |
f0b493e6 | 1554 | return false; |
7e55a19c SG |
1555 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
1556 | return false; | |
44c769de | 1557 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
f2842ab5 JA |
1558 | } |
1559 | ||
2c5d763c JA |
1560 | /* |
1561 | * This should only get called when at least one event has been posted. | |
1562 | * Some applications rely on the eventfd notification count only changing | |
1563 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1564 | * 1:1 relationship between how many times this function is called (and | |
1565 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1566 | */ | |
b41e9852 | 1567 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1568 | { |
5fd46178 JA |
1569 | /* |
1570 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1571 | * io_should_wake() handle the termination of the loop and only | |
1572 | * wake as many waiters as we need to. | |
1573 | */ | |
1574 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1575 | wake_up_all(&ctx->cq_wait); | |
534ca6d6 JA |
1576 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
1577 | wake_up(&ctx->sq_data->wait); | |
b41e9852 | 1578 | if (io_should_trigger_evfd(ctx)) |
1d7bb1d5 | 1579 | eventfd_signal(ctx->cq_ev_fd, 1); |
311997b3 PB |
1580 | if (waitqueue_active(&ctx->poll_wait)) { |
1581 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1582 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1583 | } | |
1d7bb1d5 JA |
1584 | } |
1585 | ||
80c18e4a PB |
1586 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1587 | { | |
1588 | if (ctx->flags & IORING_SETUP_SQPOLL) { | |
5fd46178 JA |
1589 | if (wq_has_sleeper(&ctx->cq_wait)) |
1590 | wake_up_all(&ctx->cq_wait); | |
80c18e4a PB |
1591 | } |
1592 | if (io_should_trigger_evfd(ctx)) | |
1593 | eventfd_signal(ctx->cq_ev_fd, 1); | |
311997b3 PB |
1594 | if (waitqueue_active(&ctx->poll_wait)) { |
1595 | wake_up_interruptible(&ctx->poll_wait); | |
4aa84f2f PB |
1596 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
1597 | } | |
80c18e4a PB |
1598 | } |
1599 | ||
c4a2ed72 | 1600 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1601 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1602 | { |
b18032bb | 1603 | bool all_flushed, posted; |
1d7bb1d5 | 1604 | |
a566c556 | 1605 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1606 | return false; |
1d7bb1d5 | 1607 | |
b18032bb | 1608 | posted = false; |
79ebeaee | 1609 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1610 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1611 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1612 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1613 | |
1d7bb1d5 JA |
1614 | if (!cqe && !force) |
1615 | break; | |
6c2450ae PB |
1616 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1617 | struct io_overflow_cqe, list); | |
1618 | if (cqe) | |
1619 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1620 | else | |
8f6ed49a PB |
1621 | io_account_cq_overflow(ctx); |
1622 | ||
b18032bb | 1623 | posted = true; |
6c2450ae PB |
1624 | list_del(&ocqe->list); |
1625 | kfree(ocqe); | |
1d7bb1d5 JA |
1626 | } |
1627 | ||
09e88404 PB |
1628 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1629 | if (all_flushed) { | |
5ed7a37d | 1630 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1631 | WRITE_ONCE(ctx->rings->sq_flags, |
1632 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1633 | } |
46930143 | 1634 | |
b18032bb JA |
1635 | if (posted) |
1636 | io_commit_cqring(ctx); | |
79ebeaee | 1637 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1638 | if (posted) |
1639 | io_cqring_ev_posted(ctx); | |
09e88404 | 1640 | return all_flushed; |
1d7bb1d5 JA |
1641 | } |
1642 | ||
90f67366 | 1643 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1644 | { |
ca0a2651 JA |
1645 | bool ret = true; |
1646 | ||
5ed7a37d | 1647 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1648 | /* iopoll syncs against uring_lock, not completion_lock */ |
1649 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1650 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1651 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1652 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1653 | mutex_unlock(&ctx->uring_lock); | |
1654 | } | |
ca0a2651 JA |
1655 | |
1656 | return ret; | |
6c503150 PB |
1657 | } |
1658 | ||
6a290a14 PB |
1659 | /* must to be called somewhat shortly after putting a request */ |
1660 | static inline void io_put_task(struct task_struct *task, int nr) | |
1661 | { | |
1662 | struct io_uring_task *tctx = task->io_uring; | |
1663 | ||
e98e49b2 PB |
1664 | if (likely(task == current)) { |
1665 | tctx->cached_refs += nr; | |
1666 | } else { | |
1667 | percpu_counter_sub(&tctx->inflight, nr); | |
1668 | if (unlikely(atomic_read(&tctx->in_idle))) | |
1669 | wake_up(&tctx->wait); | |
1670 | put_task_struct_many(task, nr); | |
1671 | } | |
6a290a14 PB |
1672 | } |
1673 | ||
9a10867a PB |
1674 | static void io_task_refs_refill(struct io_uring_task *tctx) |
1675 | { | |
1676 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
1677 | ||
1678 | percpu_counter_add(&tctx->inflight, refill); | |
1679 | refcount_add(refill, ¤t->usage); | |
1680 | tctx->cached_refs += refill; | |
1681 | } | |
1682 | ||
1683 | static inline void io_get_task_refs(int nr) | |
1684 | { | |
1685 | struct io_uring_task *tctx = current->io_uring; | |
1686 | ||
1687 | tctx->cached_refs -= nr; | |
1688 | if (unlikely(tctx->cached_refs < 0)) | |
1689 | io_task_refs_refill(tctx); | |
1690 | } | |
1691 | ||
d4d19c19 PB |
1692 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
1693 | long res, unsigned int cflags) | |
2b188cc1 | 1694 | { |
cce4b8b0 | 1695 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 1696 | |
cce4b8b0 PB |
1697 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
1698 | if (!ocqe) { | |
1699 | /* | |
1700 | * If we're in ring overflow flush mode, or in task cancel mode, | |
1701 | * or cannot allocate an overflow entry, then we need to drop it | |
1702 | * on the floor. | |
1703 | */ | |
8f6ed49a | 1704 | io_account_cq_overflow(ctx); |
cce4b8b0 | 1705 | return false; |
2b188cc1 | 1706 | } |
cce4b8b0 | 1707 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 1708 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1709 | WRITE_ONCE(ctx->rings->sq_flags, |
1710 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
1711 | ||
cce4b8b0 | 1712 | } |
d4d19c19 | 1713 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
1714 | ocqe->cqe.res = res; |
1715 | ocqe->cqe.flags = cflags; | |
1716 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
1717 | return true; | |
2b188cc1 JA |
1718 | } |
1719 | ||
d4d19c19 PB |
1720 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1721 | long res, unsigned int cflags) | |
2b188cc1 JA |
1722 | { |
1723 | struct io_uring_cqe *cqe; | |
1724 | ||
d4d19c19 | 1725 | trace_io_uring_complete(ctx, user_data, res, cflags); |
51c3ff62 | 1726 | |
2b188cc1 JA |
1727 | /* |
1728 | * If we can't get a cq entry, userspace overflowed the | |
1729 | * submission (by quite a lot). Increment the overflow count in | |
1730 | * the ring. | |
1731 | */ | |
d068b506 | 1732 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 1733 | if (likely(cqe)) { |
d4d19c19 | 1734 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 1735 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 1736 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 1737 | return true; |
2b188cc1 | 1738 | } |
d4d19c19 | 1739 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
1740 | } |
1741 | ||
8d13326e | 1742 | /* not as hot to bloat with inlining */ |
d4d19c19 PB |
1743 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
1744 | long res, unsigned int cflags) | |
bcda7baa | 1745 | { |
d4d19c19 | 1746 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
bcda7baa JA |
1747 | } |
1748 | ||
7a612350 PB |
1749 | static void io_req_complete_post(struct io_kiocb *req, long res, |
1750 | unsigned int cflags) | |
2b188cc1 | 1751 | { |
78e19bbe | 1752 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 1753 | |
79ebeaee | 1754 | spin_lock(&ctx->completion_lock); |
d4d19c19 | 1755 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
c7dae4ba JA |
1756 | /* |
1757 | * If we're the last reference to this request, add to our locked | |
1758 | * free_list cache. | |
1759 | */ | |
de9b4cca | 1760 | if (req_ref_put_and_test(req)) { |
7a612350 | 1761 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 1762 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
1763 | io_disarm_next(req); |
1764 | if (req->link) { | |
1765 | io_req_task_queue(req->link); | |
1766 | req->link = NULL; | |
1767 | } | |
1768 | } | |
c7dae4ba JA |
1769 | io_dismantle_req(req); |
1770 | io_put_task(req->task, 1); | |
bb943b82 | 1771 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
d0acdee2 | 1772 | ctx->locked_free_nr++; |
180f829f PB |
1773 | } else { |
1774 | if (!percpu_ref_tryget(&ctx->refs)) | |
1775 | req = NULL; | |
1776 | } | |
7a612350 | 1777 | io_commit_cqring(ctx); |
79ebeaee | 1778 | spin_unlock(&ctx->completion_lock); |
7a612350 | 1779 | |
180f829f PB |
1780 | if (req) { |
1781 | io_cqring_ev_posted(ctx); | |
c7dae4ba | 1782 | percpu_ref_put(&ctx->refs); |
180f829f | 1783 | } |
229a7b63 JA |
1784 | } |
1785 | ||
4e3d9ff9 JA |
1786 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
1787 | { | |
c854357b | 1788 | return req->flags & IO_REQ_CLEAN_FLAGS; |
4e3d9ff9 JA |
1789 | } |
1790 | ||
a38d68db | 1791 | static void io_req_complete_state(struct io_kiocb *req, long res, |
889fca73 | 1792 | unsigned int cflags) |
229a7b63 | 1793 | { |
4e3d9ff9 | 1794 | if (io_req_needs_clean(req)) |
68fb8979 | 1795 | io_clean_op(req); |
a38d68db PB |
1796 | req->result = res; |
1797 | req->compl.cflags = cflags; | |
e342c807 | 1798 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
1799 | } |
1800 | ||
889fca73 PB |
1801 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
1802 | long res, unsigned cflags) | |
bcda7baa | 1803 | { |
889fca73 PB |
1804 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
1805 | io_req_complete_state(req, res, cflags); | |
a38d68db | 1806 | else |
c7dae4ba | 1807 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
1808 | } |
1809 | ||
a38d68db | 1810 | static inline void io_req_complete(struct io_kiocb *req, long res) |
0ddf92e8 | 1811 | { |
889fca73 | 1812 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
1813 | } |
1814 | ||
f41db273 PB |
1815 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
1816 | { | |
93d2bcd2 | 1817 | req_set_fail(req); |
f41db273 PB |
1818 | io_req_complete_post(req, res, 0); |
1819 | } | |
1820 | ||
864ea921 PB |
1821 | /* |
1822 | * Don't initialise the fields below on every allocation, but do that in | |
1823 | * advance and keep them valid across allocations. | |
1824 | */ | |
1825 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1826 | { | |
1827 | req->ctx = ctx; | |
1828 | req->link = NULL; | |
1829 | req->async_data = NULL; | |
1830 | /* not necessary, but safer to zero */ | |
1831 | req->result = 0; | |
1832 | } | |
1833 | ||
dac7a098 | 1834 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 1835 | struct io_submit_state *state) |
dac7a098 | 1836 | { |
79ebeaee | 1837 | spin_lock(&ctx->completion_lock); |
cd0ca2e0 | 1838 | list_splice_init(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 1839 | ctx->locked_free_nr = 0; |
79ebeaee | 1840 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
1841 | } |
1842 | ||
dd78f492 | 1843 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 1844 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 1845 | { |
c7dae4ba | 1846 | struct io_submit_state *state = &ctx->submit_state; |
dd78f492 | 1847 | int nr; |
0ddf92e8 | 1848 | |
c7dae4ba JA |
1849 | /* |
1850 | * If we have more than a batch's worth of requests in our IRQ side | |
1851 | * locked cache, grab the lock and move them over to our submission | |
1852 | * side cache. | |
1853 | */ | |
d0acdee2 | 1854 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 1855 | io_flush_cached_locked_reqs(ctx, state); |
0ddf92e8 | 1856 | |
dd78f492 | 1857 | nr = state->free_reqs; |
cd0ca2e0 PB |
1858 | while (!list_empty(&state->free_list)) { |
1859 | struct io_kiocb *req = list_first_entry(&state->free_list, | |
bb943b82 | 1860 | struct io_kiocb, inflight_entry); |
dd78f492 | 1861 | |
bb943b82 | 1862 | list_del(&req->inflight_entry); |
dd78f492 PB |
1863 | state->reqs[nr++] = req; |
1864 | if (nr == ARRAY_SIZE(state->reqs)) | |
e5d1bc0a | 1865 | break; |
1b4c351f JA |
1866 | } |
1867 | ||
dd78f492 PB |
1868 | state->free_reqs = nr; |
1869 | return nr != 0; | |
0ddf92e8 JA |
1870 | } |
1871 | ||
5d5901a3 PB |
1872 | /* |
1873 | * A request might get retired back into the request caches even before opcode | |
1874 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
1875 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
1876 | * and with extra caution to not get a request that is still worked on. | |
1877 | */ | |
e5d1bc0a | 1878 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
5d5901a3 | 1879 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 1880 | { |
e5d1bc0a | 1881 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 PB |
1882 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
1883 | int ret, i; | |
e5d1bc0a | 1884 | |
fe7e3257 | 1885 | BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH); |
e5d1bc0a | 1886 | |
864ea921 PB |
1887 | if (likely(state->free_reqs || io_flush_cached_reqs(ctx))) |
1888 | goto got_req; | |
e5d1bc0a | 1889 | |
864ea921 PB |
1890 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
1891 | state->reqs); | |
fd6fab2c | 1892 | |
864ea921 PB |
1893 | /* |
1894 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1895 | * retry single alloc to be on the safe side. | |
1896 | */ | |
1897 | if (unlikely(ret <= 0)) { | |
1898 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1899 | if (!state->reqs[0]) | |
1900 | return NULL; | |
1901 | ret = 1; | |
2b188cc1 | 1902 | } |
864ea921 PB |
1903 | |
1904 | for (i = 0; i < ret; i++) | |
1905 | io_preinit_req(state->reqs[i], ctx); | |
1906 | state->free_reqs = ret; | |
e5d1bc0a | 1907 | got_req: |
291b2821 PB |
1908 | state->free_reqs--; |
1909 | return state->reqs[state->free_reqs]; | |
2b188cc1 JA |
1910 | } |
1911 | ||
e1d767f0 | 1912 | static inline void io_put_file(struct file *file) |
8da11c19 | 1913 | { |
e1d767f0 | 1914 | if (file) |
8da11c19 PB |
1915 | fput(file); |
1916 | } | |
1917 | ||
4edf20f9 | 1918 | static void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 1919 | { |
094bae49 | 1920 | unsigned int flags = req->flags; |
929a3af9 | 1921 | |
3a0a6902 PB |
1922 | if (io_req_needs_clean(req)) |
1923 | io_clean_op(req); | |
e1d767f0 PB |
1924 | if (!(flags & REQ_F_FIXED_FILE)) |
1925 | io_put_file(req->file); | |
269bbe5f BM |
1926 | if (req->fixed_rsrc_refs) |
1927 | percpu_ref_put(req->fixed_rsrc_refs); | |
99ebe4ef | 1928 | if (req->async_data) { |
094bae49 | 1929 | kfree(req->async_data); |
99ebe4ef PB |
1930 | req->async_data = NULL; |
1931 | } | |
e65ef56d JA |
1932 | } |
1933 | ||
216578e5 | 1934 | static void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 1935 | { |
51a4cc11 | 1936 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 1937 | |
216578e5 | 1938 | io_dismantle_req(req); |
7c660731 | 1939 | io_put_task(req->task, 1); |
c6ca97b3 | 1940 | |
79ebeaee | 1941 | spin_lock(&ctx->completion_lock); |
bb943b82 | 1942 | list_add(&req->inflight_entry, &ctx->locked_free_list); |
c34b025f | 1943 | ctx->locked_free_nr++; |
79ebeaee | 1944 | spin_unlock(&ctx->completion_lock); |
c34b025f | 1945 | |
ecfc5177 | 1946 | percpu_ref_put(&ctx->refs); |
e65ef56d JA |
1947 | } |
1948 | ||
f2f87370 PB |
1949 | static inline void io_remove_next_linked(struct io_kiocb *req) |
1950 | { | |
1951 | struct io_kiocb *nxt = req->link; | |
1952 | ||
1953 | req->link = nxt->link; | |
1954 | nxt->link = NULL; | |
1955 | } | |
1956 | ||
33cc89a9 PB |
1957 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
1958 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 1959 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 1960 | { |
33cc89a9 | 1961 | struct io_kiocb *link = req->link; |
f2f87370 | 1962 | |
b97e736a | 1963 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 1964 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 1965 | |
f2f87370 | 1966 | io_remove_next_linked(req); |
90cd7e42 | 1967 | link->timeout.head = NULL; |
fd9c7bc5 | 1968 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
d4d19c19 PB |
1969 | io_cqring_fill_event(link->ctx, link->user_data, |
1970 | -ECANCELED, 0); | |
91c2f697 | 1971 | io_put_req_deferred(link); |
d4729fbd | 1972 | return true; |
c9abd7ad PB |
1973 | } |
1974 | } | |
d4729fbd | 1975 | return false; |
7c86ffee PB |
1976 | } |
1977 | ||
d148ca4b | 1978 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 1979 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 1980 | { |
33cc89a9 | 1981 | struct io_kiocb *nxt, *link = req->link; |
9e645e11 | 1982 | |
f2f87370 | 1983 | req->link = NULL; |
f2f87370 | 1984 | while (link) { |
a8295b98 HX |
1985 | long res = -ECANCELED; |
1986 | ||
1987 | if (link->flags & REQ_F_FAIL) | |
1988 | res = link->result; | |
1989 | ||
f2f87370 PB |
1990 | nxt = link->link; |
1991 | link->link = NULL; | |
2665abfd | 1992 | |
f2f87370 | 1993 | trace_io_uring_fail_link(req, link); |
a8295b98 | 1994 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
91c2f697 | 1995 | io_put_req_deferred(link); |
f2f87370 | 1996 | link = nxt; |
9e645e11 | 1997 | } |
33cc89a9 | 1998 | } |
9e645e11 | 1999 | |
33cc89a9 PB |
2000 | static bool io_disarm_next(struct io_kiocb *req) |
2001 | __must_hold(&req->ctx->completion_lock) | |
2002 | { | |
2003 | bool posted = false; | |
2004 | ||
0756a869 PB |
2005 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2006 | struct io_kiocb *link = req->link; | |
2007 | ||
906c6caa | 2008 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2009 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2010 | io_remove_next_linked(req); | |
2011 | io_cqring_fill_event(link->ctx, link->user_data, | |
2012 | -ECANCELED, 0); | |
2013 | io_put_req_deferred(link); | |
2014 | posted = true; | |
2015 | } | |
2016 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2017 | struct io_ring_ctx *ctx = req->ctx; |
2018 | ||
2019 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2020 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2021 | spin_unlock_irq(&ctx->timeout_lock); |
2022 | } | |
93d2bcd2 | 2023 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2024 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2025 | posted |= (req->link != NULL); |
2026 | io_fail_links(req); | |
2027 | } | |
2028 | return posted; | |
9e645e11 JA |
2029 | } |
2030 | ||
3fa5e0f3 | 2031 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
c69f8dbe | 2032 | { |
33cc89a9 | 2033 | struct io_kiocb *nxt; |
944e58bf | 2034 | |
9e645e11 JA |
2035 | /* |
2036 | * If LINK is set, we have dependent requests in this chain. If we | |
2037 | * didn't fail this request, queue the first one up, moving any other | |
2038 | * dependencies to the next request. In case of failure, fail the rest | |
2039 | * of the chain. | |
2040 | */ | |
0756a869 | 2041 | if (req->flags & IO_DISARM_MASK) { |
33cc89a9 | 2042 | struct io_ring_ctx *ctx = req->ctx; |
33cc89a9 PB |
2043 | bool posted; |
2044 | ||
79ebeaee | 2045 | spin_lock(&ctx->completion_lock); |
33cc89a9 PB |
2046 | posted = io_disarm_next(req); |
2047 | if (posted) | |
2048 | io_commit_cqring(req->ctx); | |
79ebeaee | 2049 | spin_unlock(&ctx->completion_lock); |
33cc89a9 PB |
2050 | if (posted) |
2051 | io_cqring_ev_posted(ctx); | |
f2f87370 | 2052 | } |
33cc89a9 PB |
2053 | nxt = req->link; |
2054 | req->link = NULL; | |
2055 | return nxt; | |
4d7dd462 | 2056 | } |
9e645e11 | 2057 | |
f2f87370 | 2058 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
3fa5e0f3 | 2059 | { |
cdbff982 | 2060 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
3fa5e0f3 PB |
2061 | return NULL; |
2062 | return __io_req_find_next(req); | |
2063 | } | |
2064 | ||
f237c30a | 2065 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2066 | { |
2067 | if (!ctx) | |
2068 | return; | |
f237c30a | 2069 | if (*locked) { |
99c8bc52 HX |
2070 | if (ctx->submit_state.compl_nr) |
2071 | io_submit_flush_completions(ctx); | |
2c32395d | 2072 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2073 | *locked = false; |
2c32395d PB |
2074 | } |
2075 | percpu_ref_put(&ctx->refs); | |
2076 | } | |
2077 | ||
7cbf1722 | 2078 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2079 | { |
f237c30a | 2080 | bool locked = false; |
ebd0df2e | 2081 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2082 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2083 | task_work); | |
c40f6379 | 2084 | |
16f72070 | 2085 | while (1) { |
3f18407d PB |
2086 | struct io_wq_work_node *node; |
2087 | ||
2088 | spin_lock_irq(&tctx->task_lock); | |
c6538be9 | 2089 | node = tctx->task_list.first; |
3f18407d | 2090 | INIT_WQ_LIST(&tctx->task_list); |
6294f368 PB |
2091 | if (!node) |
2092 | tctx->task_running = false; | |
3f18407d | 2093 | spin_unlock_irq(&tctx->task_lock); |
6294f368 PB |
2094 | if (!node) |
2095 | break; | |
3f18407d | 2096 | |
6294f368 | 2097 | do { |
3f18407d PB |
2098 | struct io_wq_work_node *next = node->next; |
2099 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2100 | io_task_work.node); | |
2101 | ||
2102 | if (req->ctx != ctx) { | |
f237c30a | 2103 | ctx_flush_and_put(ctx, &locked); |
3f18407d | 2104 | ctx = req->ctx; |
126180b9 PB |
2105 | /* if not contended, grab and improve batching */ |
2106 | locked = mutex_trylock(&ctx->uring_lock); | |
3f18407d PB |
2107 | percpu_ref_get(&ctx->refs); |
2108 | } | |
f237c30a | 2109 | req->io_task_work.func(req, &locked); |
3f18407d | 2110 | node = next; |
6294f368 PB |
2111 | } while (node); |
2112 | ||
7cbf1722 | 2113 | cond_resched(); |
3f18407d | 2114 | } |
ebd0df2e | 2115 | |
f237c30a | 2116 | ctx_flush_and_put(ctx, &locked); |
7cbf1722 JA |
2117 | } |
2118 | ||
e09ee510 | 2119 | static void io_req_task_work_add(struct io_kiocb *req) |
7cbf1722 | 2120 | { |
c15b79de | 2121 | struct task_struct *tsk = req->task; |
7cbf1722 | 2122 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2123 | enum task_work_notify_mode notify; |
e09ee510 | 2124 | struct io_wq_work_node *node; |
0b81e80c | 2125 | unsigned long flags; |
6294f368 | 2126 | bool running; |
7cbf1722 JA |
2127 | |
2128 | WARN_ON_ONCE(!tctx); | |
2129 | ||
0b81e80c | 2130 | spin_lock_irqsave(&tctx->task_lock, flags); |
7cbf1722 | 2131 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
6294f368 PB |
2132 | running = tctx->task_running; |
2133 | if (!running) | |
2134 | tctx->task_running = true; | |
0b81e80c | 2135 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2136 | |
2137 | /* task_work already pending, we're done */ | |
6294f368 | 2138 | if (running) |
e09ee510 | 2139 | return; |
7cbf1722 | 2140 | |
c15b79de PB |
2141 | /* |
2142 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2143 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2144 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2145 | * will do the job. | |
2146 | */ | |
2147 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
c15b79de PB |
2148 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
2149 | wake_up_process(tsk); | |
e09ee510 | 2150 | return; |
c15b79de | 2151 | } |
2215bed9 | 2152 | |
0b81e80c | 2153 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2154 | tctx->task_running = false; |
e09ee510 PB |
2155 | node = tctx->task_list.first; |
2156 | INIT_WQ_LIST(&tctx->task_list); | |
0b81e80c | 2157 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2158 | |
e09ee510 PB |
2159 | while (node) { |
2160 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2161 | node = node->next; | |
2162 | if (llist_add(&req->io_task_work.fallback_node, | |
2163 | &req->ctx->fallback_llist)) | |
2164 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2165 | } | |
eab30c4d PB |
2166 | } |
2167 | ||
f237c30a | 2168 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2169 | { |
87ceb6a6 | 2170 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2171 | |
b18a1a45 | 2172 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2173 | io_tw_lock(ctx, locked); |
2593553a | 2174 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2175 | } |
2176 | ||
f237c30a | 2177 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2178 | { |
2179 | struct io_ring_ctx *ctx = req->ctx; | |
2180 | ||
f237c30a | 2181 | io_tw_lock(ctx, locked); |
316319e8 | 2182 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2183 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2184 | __io_queue_sqe(req); |
81b6d05c | 2185 | else |
2593553a | 2186 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2187 | } |
2188 | ||
2c4b8eb6 | 2189 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2190 | { |
2c4b8eb6 | 2191 | req->result = ret; |
5b0a6acc | 2192 | req->io_task_work.func = io_req_task_cancel; |
e09ee510 | 2193 | io_req_task_work_add(req); |
c40f6379 JA |
2194 | } |
2195 | ||
2c4b8eb6 | 2196 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2197 | { |
5b0a6acc | 2198 | req->io_task_work.func = io_req_task_submit; |
e09ee510 | 2199 | io_req_task_work_add(req); |
a3df7698 PB |
2200 | } |
2201 | ||
773af691 JA |
2202 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2203 | { | |
2204 | req->io_task_work.func = io_queue_async_work; | |
2205 | io_req_task_work_add(req); | |
2206 | } | |
2207 | ||
f2f87370 | 2208 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2209 | { |
9b5f7bd9 | 2210 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2211 | |
2212 | if (nxt) | |
906a8c3f | 2213 | io_req_task_queue(nxt); |
c69f8dbe JL |
2214 | } |
2215 | ||
c3524383 | 2216 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2217 | { |
c3524383 PB |
2218 | io_queue_next(req); |
2219 | __io_free_req(req); | |
2220 | } | |
8766dd51 | 2221 | |
f237c30a PB |
2222 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2223 | { | |
2224 | io_free_req(req); | |
2225 | } | |
2226 | ||
2d6500d4 | 2227 | struct req_batch { |
5af1d13e PB |
2228 | struct task_struct *task; |
2229 | int task_refs; | |
1b4c351f | 2230 | int ctx_refs; |
2d6500d4 PB |
2231 | }; |
2232 | ||
5af1d13e PB |
2233 | static inline void io_init_req_batch(struct req_batch *rb) |
2234 | { | |
5af1d13e | 2235 | rb->task_refs = 0; |
9ae72463 | 2236 | rb->ctx_refs = 0; |
5af1d13e PB |
2237 | rb->task = NULL; |
2238 | } | |
2239 | ||
2d6500d4 PB |
2240 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
2241 | struct req_batch *rb) | |
2242 | { | |
9ae72463 PB |
2243 | if (rb->ctx_refs) |
2244 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); | |
e98e49b2 | 2245 | if (rb->task) |
e9dbe221 | 2246 | io_put_task(rb->task, rb->task_refs); |
2d6500d4 PB |
2247 | } |
2248 | ||
6ff119a6 PB |
2249 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
2250 | struct io_submit_state *state) | |
2d6500d4 | 2251 | { |
f2f87370 | 2252 | io_queue_next(req); |
96670657 | 2253 | io_dismantle_req(req); |
2d6500d4 | 2254 | |
e3bc8e9d | 2255 | if (req->task != rb->task) { |
7c660731 PB |
2256 | if (rb->task) |
2257 | io_put_task(rb->task, rb->task_refs); | |
e3bc8e9d JA |
2258 | rb->task = req->task; |
2259 | rb->task_refs = 0; | |
5af1d13e | 2260 | } |
e3bc8e9d | 2261 | rb->task_refs++; |
9ae72463 | 2262 | rb->ctx_refs++; |
5af1d13e | 2263 | |
bd759045 | 2264 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
6ff119a6 | 2265 | state->reqs[state->free_reqs++] = req; |
bd759045 | 2266 | else |
cd0ca2e0 | 2267 | list_add(&req->inflight_entry, &state->free_list); |
7a743e22 PB |
2268 | } |
2269 | ||
2a2758f2 | 2270 | static void io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2271 | __must_hold(&ctx->uring_lock) |
905c172f | 2272 | { |
cd0ca2e0 PB |
2273 | struct io_submit_state *state = &ctx->submit_state; |
2274 | int i, nr = state->compl_nr; | |
905c172f PB |
2275 | struct req_batch rb; |
2276 | ||
79ebeaee | 2277 | spin_lock(&ctx->completion_lock); |
905c172f | 2278 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2279 | struct io_kiocb *req = state->compl_reqs[i]; |
5182ed2e | 2280 | |
d4d19c19 PB |
2281 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2282 | req->compl.cflags); | |
905c172f PB |
2283 | } |
2284 | io_commit_cqring(ctx); | |
79ebeaee | 2285 | spin_unlock(&ctx->completion_lock); |
905c172f | 2286 | io_cqring_ev_posted(ctx); |
5182ed2e PB |
2287 | |
2288 | io_init_req_batch(&rb); | |
905c172f | 2289 | for (i = 0; i < nr; i++) { |
cd0ca2e0 | 2290 | struct io_kiocb *req = state->compl_reqs[i]; |
905c172f | 2291 | |
91c2f697 | 2292 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2293 | io_req_free_batch(&rb, req, &ctx->submit_state); |
905c172f PB |
2294 | } |
2295 | ||
2296 | io_req_free_batch_finish(ctx, &rb); | |
cd0ca2e0 | 2297 | state->compl_nr = 0; |
7a743e22 PB |
2298 | } |
2299 | ||
ba816ad6 JA |
2300 | /* |
2301 | * Drop reference to request, return next in chain (if there is one) if this | |
2302 | * was the last reference to this request. | |
2303 | */ | |
0d85035a | 2304 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2305 | { |
9b5f7bd9 PB |
2306 | struct io_kiocb *nxt = NULL; |
2307 | ||
de9b4cca | 2308 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2309 | nxt = io_req_find_next(req); |
4d7dd462 | 2310 | __io_free_req(req); |
2a44f467 | 2311 | } |
9b5f7bd9 | 2312 | return nxt; |
2b188cc1 JA |
2313 | } |
2314 | ||
0d85035a | 2315 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2316 | { |
de9b4cca | 2317 | if (req_ref_put_and_test(req)) |
e65ef56d | 2318 | io_free_req(req); |
2b188cc1 JA |
2319 | } |
2320 | ||
91c2f697 | 2321 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2322 | { |
91c2f697 | 2323 | if (req_ref_put_and_test(req)) { |
f237c30a | 2324 | req->io_task_work.func = io_free_req_work; |
543af3a1 PB |
2325 | io_req_task_work_add(req); |
2326 | } | |
216578e5 PB |
2327 | } |
2328 | ||
6c503150 | 2329 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2330 | { |
2331 | /* See comment at the top of this file */ | |
2332 | smp_rmb(); | |
e23de15f | 2333 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2334 | } |
2335 | ||
fb5ccc98 PB |
2336 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2337 | { | |
2338 | struct io_rings *rings = ctx->rings; | |
2339 | ||
2340 | /* make sure SQ entry isn't read before tail */ | |
2341 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2342 | } | |
2343 | ||
8ff069bf | 2344 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
e94f141b | 2345 | { |
8ff069bf | 2346 | unsigned int cflags; |
e94f141b | 2347 | |
bcda7baa JA |
2348 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
2349 | cflags |= IORING_CQE_F_BUFFER; | |
0e1b6fe3 | 2350 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
bcda7baa JA |
2351 | kfree(kbuf); |
2352 | return cflags; | |
e94f141b JA |
2353 | } |
2354 | ||
8ff069bf | 2355 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
bcda7baa | 2356 | { |
4d954c25 | 2357 | struct io_buffer *kbuf; |
bcda7baa | 2358 | |
ae421d93 PB |
2359 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
2360 | return 0; | |
4d954c25 | 2361 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
8ff069bf PB |
2362 | return io_put_kbuf(req, kbuf); |
2363 | } | |
2364 | ||
4c6e277c JA |
2365 | static inline bool io_run_task_work(void) |
2366 | { | |
ef98eb04 | 2367 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
4c6e277c | 2368 | __set_current_state(TASK_RUNNING); |
ef98eb04 | 2369 | tracehook_notify_signal(); |
4c6e277c JA |
2370 | return true; |
2371 | } | |
2372 | ||
2373 | return false; | |
bcda7baa JA |
2374 | } |
2375 | ||
def596e9 JA |
2376 | /* |
2377 | * Find and free completed poll iocbs | |
2378 | */ | |
2379 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
a8576af9 | 2380 | struct list_head *done) |
def596e9 | 2381 | { |
8237e045 | 2382 | struct req_batch rb; |
def596e9 | 2383 | struct io_kiocb *req; |
bbde017a XW |
2384 | |
2385 | /* order with ->result store in io_complete_rw_iopoll() */ | |
2386 | smp_rmb(); | |
def596e9 | 2387 | |
5af1d13e | 2388 | io_init_req_batch(&rb); |
def596e9 | 2389 | while (!list_empty(done)) { |
d21ffe7e | 2390 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
f161340d PB |
2391 | list_del(&req->inflight_entry); |
2392 | ||
a8576af9 | 2393 | if (READ_ONCE(req->result) == -EAGAIN && |
8c130827 | 2394 | !(req->flags & REQ_F_DONT_REISSUE)) { |
bbde017a | 2395 | req->iopoll_completed = 0; |
773af691 | 2396 | io_req_task_queue_reissue(req); |
8c130827 | 2397 | continue; |
bbde017a | 2398 | } |
def596e9 | 2399 | |
ae421d93 PB |
2400 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
2401 | io_put_rw_kbuf(req)); | |
def596e9 JA |
2402 | (*nr_events)++; |
2403 | ||
de9b4cca | 2404 | if (req_ref_put_and_test(req)) |
6ff119a6 | 2405 | io_req_free_batch(&rb, req, &ctx->submit_state); |
def596e9 | 2406 | } |
def596e9 | 2407 | |
09bb8394 | 2408 | io_commit_cqring(ctx); |
80c18e4a | 2409 | io_cqring_ev_posted_iopoll(ctx); |
2d6500d4 | 2410 | io_req_free_batch_finish(ctx, &rb); |
581f9810 BM |
2411 | } |
2412 | ||
def596e9 | 2413 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
a8576af9 | 2414 | long min) |
def596e9 JA |
2415 | { |
2416 | struct io_kiocb *req, *tmp; | |
2417 | LIST_HEAD(done); | |
2418 | bool spin; | |
def596e9 JA |
2419 | |
2420 | /* | |
2421 | * Only spin for completions if we don't have multiple devices hanging | |
2422 | * off our complete list, and we're under the requested amount. | |
2423 | */ | |
915b3dde | 2424 | spin = !ctx->poll_multi_queue && *nr_events < min; |
def596e9 | 2425 | |
d21ffe7e | 2426 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
9adbd45d | 2427 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2428 | int ret; |
def596e9 JA |
2429 | |
2430 | /* | |
581f9810 BM |
2431 | * Move completed and retryable entries to our local lists. |
2432 | * If we find a request that requires polling, break out | |
2433 | * and complete those lists first, if we have entries there. | |
def596e9 | 2434 | */ |
65a6543d | 2435 | if (READ_ONCE(req->iopoll_completed)) { |
d21ffe7e | 2436 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2437 | continue; |
2438 | } | |
2439 | if (!list_empty(&done)) | |
2440 | break; | |
2441 | ||
2442 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
a2416e1e PB |
2443 | if (unlikely(ret < 0)) |
2444 | return ret; | |
2445 | else if (ret) | |
2446 | spin = false; | |
def596e9 | 2447 | |
3aadc23e PB |
2448 | /* iopoll may have completed current req */ |
2449 | if (READ_ONCE(req->iopoll_completed)) | |
d21ffe7e | 2450 | list_move_tail(&req->inflight_entry, &done); |
def596e9 JA |
2451 | } |
2452 | ||
2453 | if (!list_empty(&done)) | |
a8576af9 | 2454 | io_iopoll_complete(ctx, nr_events, &done); |
def596e9 | 2455 | |
a2416e1e | 2456 | return 0; |
def596e9 JA |
2457 | } |
2458 | ||
def596e9 JA |
2459 | /* |
2460 | * We can't just wait for polled events to come to us, we have to actively | |
2461 | * find and complete them. | |
2462 | */ | |
b2edc0a7 | 2463 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2464 | { |
2465 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2466 | return; | |
2467 | ||
2468 | mutex_lock(&ctx->uring_lock); | |
540e32a0 | 2469 | while (!list_empty(&ctx->iopoll_list)) { |
def596e9 JA |
2470 | unsigned int nr_events = 0; |
2471 | ||
a8576af9 | 2472 | io_do_iopoll(ctx, &nr_events, 0); |
08f5439f | 2473 | |
b2edc0a7 PB |
2474 | /* let it sleep and repeat later if can't complete a request */ |
2475 | if (nr_events == 0) | |
2476 | break; | |
08f5439f JA |
2477 | /* |
2478 | * Ensure we allow local-to-the-cpu processing to take place, | |
2479 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2480 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2481 | */ |
3fcee5a6 PB |
2482 | if (need_resched()) { |
2483 | mutex_unlock(&ctx->uring_lock); | |
2484 | cond_resched(); | |
2485 | mutex_lock(&ctx->uring_lock); | |
2486 | } | |
def596e9 JA |
2487 | } |
2488 | mutex_unlock(&ctx->uring_lock); | |
2489 | } | |
2490 | ||
7668b92a | 2491 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2492 | { |
7668b92a | 2493 | unsigned int nr_events = 0; |
e9979b36 | 2494 | int ret = 0; |
500f9fba | 2495 | |
c7849be9 XW |
2496 | /* |
2497 | * We disallow the app entering submit/complete with polling, but we | |
2498 | * still need to lock the ring to prevent racing with polled issue | |
2499 | * that got punted to a workqueue. | |
2500 | */ | |
2501 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2502 | /* |
2503 | * Don't enter poll loop if we already have events pending. | |
2504 | * If we do, we can potentially be spinning for commands that | |
2505 | * already triggered a CQE (eg in error). | |
2506 | */ | |
5ed7a37d | 2507 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2508 | __io_cqring_overflow_flush(ctx, false); |
2509 | if (io_cqring_events(ctx)) | |
2510 | goto out; | |
def596e9 | 2511 | do { |
500f9fba JA |
2512 | /* |
2513 | * If a submit got punted to a workqueue, we can have the | |
2514 | * application entering polling for a command before it gets | |
2515 | * issued. That app will hold the uring_lock for the duration | |
2516 | * of the poll right here, so we need to take a breather every | |
2517 | * now and then to ensure that the issue has a chance to add | |
2518 | * the poll to the issued list. Otherwise we can spin here | |
2519 | * forever, while the workqueue is stuck trying to acquire the | |
2520 | * very same mutex. | |
2521 | */ | |
e9979b36 | 2522 | if (list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2523 | u32 tail = ctx->cached_cq_tail; |
2524 | ||
500f9fba | 2525 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2526 | io_run_task_work(); |
500f9fba | 2527 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2528 | |
8f487ef2 PB |
2529 | /* some requests don't go through iopoll_list */ |
2530 | if (tail != ctx->cached_cq_tail || | |
2531 | list_empty(&ctx->iopoll_list)) | |
e9979b36 | 2532 | break; |
500f9fba | 2533 | } |
a8576af9 | 2534 | ret = io_do_iopoll(ctx, &nr_events, min); |
f39c8a5b PB |
2535 | } while (!ret && nr_events < min && !need_resched()); |
2536 | out: | |
500f9fba | 2537 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2538 | return ret; |
2539 | } | |
2540 | ||
491381ce | 2541 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2542 | { |
491381ce JA |
2543 | /* |
2544 | * Tell lockdep we inherited freeze protection from submission | |
2545 | * thread. | |
2546 | */ | |
2547 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2548 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2549 | |
1c98679d PB |
2550 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2551 | sb_end_write(sb); | |
2b188cc1 JA |
2552 | } |
2553 | } | |
2554 | ||
b63534c4 | 2555 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2556 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2557 | { |
ab454438 | 2558 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2559 | |
ab454438 PB |
2560 | if (!rw) |
2561 | return !io_req_prep_async(req); | |
2562 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ | |
2563 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); | |
2564 | return true; | |
b63534c4 | 2565 | } |
b63534c4 | 2566 | |
3e6a0d3c | 2567 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2568 | { |
355afaeb | 2569 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2570 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2571 | |
355afaeb JA |
2572 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2573 | return false; | |
3e6a0d3c JA |
2574 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2575 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2576 | return false; |
7c977a58 JA |
2577 | /* |
2578 | * If ref is dying, we might be running poll reap from the exit work. | |
2579 | * Don't attempt to reissue from that path, just let it fail with | |
2580 | * -EAGAIN. | |
2581 | */ | |
3e6a0d3c JA |
2582 | if (percpu_ref_is_dying(&ctx->refs)) |
2583 | return false; | |
ef046888 JA |
2584 | /* |
2585 | * Play it safe and assume not safe to re-import and reissue if we're | |
2586 | * not in the original thread group (or in task context). | |
2587 | */ | |
2588 | if (!same_thread_group(req->task, current) || !in_task()) | |
2589 | return false; | |
3e6a0d3c JA |
2590 | return true; |
2591 | } | |
e82ad485 | 2592 | #else |
a1ff1e3f | 2593 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2594 | { |
2595 | return false; | |
2596 | } | |
e82ad485 | 2597 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 2598 | { |
b63534c4 JA |
2599 | return false; |
2600 | } | |
3e6a0d3c | 2601 | #endif |
b63534c4 | 2602 | |
8ef12efe | 2603 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 2604 | { |
b65c128f PB |
2605 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
2606 | kiocb_end_write(req); | |
9532b99b PB |
2607 | if (res != req->result) { |
2608 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && | |
2609 | io_rw_should_reissue(req)) { | |
2610 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 2611 | return true; |
9532b99b | 2612 | } |
93d2bcd2 | 2613 | req_set_fail(req); |
8ef12efe | 2614 | req->result = res; |
9532b99b | 2615 | } |
8ef12efe JA |
2616 | return false; |
2617 | } | |
2618 | ||
f237c30a | 2619 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 2620 | { |
126180b9 PB |
2621 | unsigned int cflags = io_put_rw_kbuf(req); |
2622 | long res = req->result; | |
2623 | ||
2624 | if (*locked) { | |
2625 | struct io_ring_ctx *ctx = req->ctx; | |
2626 | struct io_submit_state *state = &ctx->submit_state; | |
2627 | ||
2628 | io_req_complete_state(req, res, cflags); | |
2629 | state->compl_reqs[state->compl_nr++] = req; | |
2630 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2631 | io_submit_flush_completions(ctx); | |
2632 | } else { | |
2633 | io_req_complete_post(req, res, cflags); | |
2634 | } | |
8ef12efe JA |
2635 | } |
2636 | ||
2637 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, | |
2638 | unsigned int issue_flags) | |
2639 | { | |
2640 | if (__io_complete_rw_common(req, res)) | |
2641 | return; | |
f237c30a | 2642 | __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req)); |
ba816ad6 JA |
2643 | } |
2644 | ||
2645 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
2646 | { | |
9adbd45d | 2647 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 2648 | |
8ef12efe JA |
2649 | if (__io_complete_rw_common(req, res)) |
2650 | return; | |
2651 | req->result = res; | |
2652 | req->io_task_work.func = io_req_task_complete; | |
2653 | io_req_task_work_add(req); | |
2b188cc1 JA |
2654 | } |
2655 | ||
def596e9 JA |
2656 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
2657 | { | |
9adbd45d | 2658 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 2659 | |
491381ce JA |
2660 | if (kiocb->ki_flags & IOCB_WRITE) |
2661 | kiocb_end_write(req); | |
9532b99b | 2662 | if (unlikely(res != req->result)) { |
a1ff1e3f JA |
2663 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
2664 | io_resubmit_prep(req))) { | |
93d2bcd2 | 2665 | req_set_fail(req); |
9532b99b PB |
2666 | req->flags |= REQ_F_DONT_REISSUE; |
2667 | } | |
8c130827 | 2668 | } |
bbde017a XW |
2669 | |
2670 | WRITE_ONCE(req->result, res); | |
b9b0e0d3 | 2671 | /* order with io_iopoll_complete() checking ->result */ |
cd664b0e PB |
2672 | smp_wmb(); |
2673 | WRITE_ONCE(req->iopoll_completed, 1); | |
def596e9 JA |
2674 | } |
2675 | ||
2676 | /* | |
2677 | * After the iocb has been issued, it's safe to be found on the poll list. | |
2678 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 2679 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
2680 | * accessing the kiocb cookie. |
2681 | */ | |
cb3d8972 | 2682 | static void io_iopoll_req_issued(struct io_kiocb *req) |
def596e9 JA |
2683 | { |
2684 | struct io_ring_ctx *ctx = req->ctx; | |
cb3d8972 PB |
2685 | const bool in_async = io_wq_current_is_worker(); |
2686 | ||
2687 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
2688 | if (unlikely(in_async)) | |
2689 | mutex_lock(&ctx->uring_lock); | |
def596e9 JA |
2690 | |
2691 | /* | |
2692 | * Track whether we have multiple files in our lists. This will impact | |
2693 | * how we do polling eventually, not spinning if we're on potentially | |
2694 | * different devices. | |
2695 | */ | |
540e32a0 | 2696 | if (list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
2697 | ctx->poll_multi_queue = false; |
2698 | } else if (!ctx->poll_multi_queue) { | |
def596e9 | 2699 | struct io_kiocb *list_req; |
915b3dde | 2700 | unsigned int queue_num0, queue_num1; |
def596e9 | 2701 | |
540e32a0 | 2702 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
d21ffe7e | 2703 | inflight_entry); |
915b3dde HX |
2704 | |
2705 | if (list_req->file != req->file) { | |
2706 | ctx->poll_multi_queue = true; | |
2707 | } else { | |
2708 | queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie); | |
2709 | queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie); | |
2710 | if (queue_num0 != queue_num1) | |
2711 | ctx->poll_multi_queue = true; | |
2712 | } | |
def596e9 JA |
2713 | } |
2714 | ||
2715 | /* | |
2716 | * For fast devices, IO may have already completed. If it has, add | |
2717 | * it to the front so we find it first. | |
2718 | */ | |
65a6543d | 2719 | if (READ_ONCE(req->iopoll_completed)) |
d21ffe7e | 2720 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
def596e9 | 2721 | else |
d21ffe7e | 2722 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
bdcd3eab | 2723 | |
cb3d8972 PB |
2724 | if (unlikely(in_async)) { |
2725 | /* | |
2726 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
2727 | * in sq thread task context or in io worker task context. If | |
2728 | * current task context is sq thread, we don't need to check | |
2729 | * whether should wake up sq thread. | |
2730 | */ | |
2731 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
2732 | wq_has_sleeper(&ctx->sq_data->wait)) | |
2733 | wake_up(&ctx->sq_data->wait); | |
2734 | ||
2735 | mutex_unlock(&ctx->uring_lock); | |
2736 | } | |
def596e9 JA |
2737 | } |
2738 | ||
4503b767 JA |
2739 | static bool io_bdev_nowait(struct block_device *bdev) |
2740 | { | |
9ba0d0c8 | 2741 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
2742 | } |
2743 | ||
2b188cc1 JA |
2744 | /* |
2745 | * If we tracked the file through the SCM inflight mechanism, we could support | |
2746 | * any file. For now, just ensure that anything potentially problematic is done | |
2747 | * inline. | |
2748 | */ | |
b191e2df | 2749 | static bool __io_file_supports_nowait(struct file *file, int rw) |
2b188cc1 JA |
2750 | { |
2751 | umode_t mode = file_inode(file)->i_mode; | |
2752 | ||
4503b767 | 2753 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
2754 | if (IS_ENABLED(CONFIG_BLOCK) && |
2755 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
2756 | return true; |
2757 | return false; | |
2758 | } | |
976517f1 | 2759 | if (S_ISSOCK(mode)) |
2b188cc1 | 2760 | return true; |
4503b767 | 2761 | if (S_ISREG(mode)) { |
4e7b5671 CH |
2762 | if (IS_ENABLED(CONFIG_BLOCK) && |
2763 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
2764 | file->f_op != &io_uring_fops) |
2765 | return true; | |
2766 | return false; | |
2767 | } | |
2b188cc1 | 2768 | |
c5b85625 JA |
2769 | /* any ->read/write should understand O_NONBLOCK */ |
2770 | if (file->f_flags & O_NONBLOCK) | |
2771 | return true; | |
2772 | ||
af197f50 JA |
2773 | if (!(file->f_mode & FMODE_NOWAIT)) |
2774 | return false; | |
2775 | ||
2776 | if (rw == READ) | |
2777 | return file->f_op->read_iter != NULL; | |
2778 | ||
2779 | return file->f_op->write_iter != NULL; | |
2b188cc1 JA |
2780 | } |
2781 | ||
b191e2df | 2782 | static bool io_file_supports_nowait(struct io_kiocb *req, int rw) |
7b29f92d | 2783 | { |
b191e2df | 2784 | if (rw == READ && (req->flags & REQ_F_NOWAIT_READ)) |
7b29f92d | 2785 | return true; |
b191e2df | 2786 | else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE)) |
7b29f92d JA |
2787 | return true; |
2788 | ||
b191e2df | 2789 | return __io_file_supports_nowait(req->file, rw); |
7b29f92d JA |
2790 | } |
2791 | ||
a88fc400 | 2792 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 2793 | { |
def596e9 | 2794 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 2795 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 2796 | struct file *file = req->file; |
09bb8394 JA |
2797 | unsigned ioprio; |
2798 | int ret; | |
2b188cc1 | 2799 | |
c97d8a0f | 2800 | if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode)) |
491381ce JA |
2801 | req->flags |= REQ_F_ISREG; |
2802 | ||
2b188cc1 | 2803 | kiocb->ki_pos = READ_ONCE(sqe->off); |
75c668cd | 2804 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
ba04291e | 2805 | req->flags |= REQ_F_CUR_POS; |
75c668cd | 2806 | kiocb->ki_pos = file->f_pos; |
ba04291e | 2807 | } |
2b188cc1 | 2808 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
2809 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
2810 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
2811 | if (unlikely(ret)) | |
2812 | return ret; | |
2b188cc1 | 2813 | |
75c668cd PB |
2814 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
2815 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) | |
2816 | req->flags |= REQ_F_NOWAIT; | |
2817 | ||
2b188cc1 JA |
2818 | ioprio = READ_ONCE(sqe->ioprio); |
2819 | if (ioprio) { | |
2820 | ret = ioprio_check_cap(ioprio); | |
2821 | if (ret) | |
09bb8394 | 2822 | return ret; |
2b188cc1 JA |
2823 | |
2824 | kiocb->ki_ioprio = ioprio; | |
2825 | } else | |
2826 | kiocb->ki_ioprio = get_current_ioprio(); | |
2827 | ||
def596e9 | 2828 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
2829 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
2830 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 2831 | return -EOPNOTSUPP; |
2b188cc1 | 2832 | |
def596e9 JA |
2833 | kiocb->ki_flags |= IOCB_HIPRI; |
2834 | kiocb->ki_complete = io_complete_rw_iopoll; | |
65a6543d | 2835 | req->iopoll_completed = 0; |
def596e9 | 2836 | } else { |
09bb8394 JA |
2837 | if (kiocb->ki_flags & IOCB_HIPRI) |
2838 | return -EINVAL; | |
def596e9 JA |
2839 | kiocb->ki_complete = io_complete_rw; |
2840 | } | |
9adbd45d | 2841 | |
eae071c9 PB |
2842 | if (req->opcode == IORING_OP_READ_FIXED || |
2843 | req->opcode == IORING_OP_WRITE_FIXED) { | |
2844 | req->imu = NULL; | |
2845 | io_req_set_rsrc_node(req); | |
2846 | } | |
2847 | ||
3529d8c2 JA |
2848 | req->rw.addr = READ_ONCE(sqe->addr); |
2849 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 2850 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 2851 | return 0; |
2b188cc1 JA |
2852 | } |
2853 | ||
2854 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
2855 | { | |
2856 | switch (ret) { | |
2857 | case -EIOCBQUEUED: | |
2858 | break; | |
2859 | case -ERESTARTSYS: | |
2860 | case -ERESTARTNOINTR: | |
2861 | case -ERESTARTNOHAND: | |
2862 | case -ERESTART_RESTARTBLOCK: | |
2863 | /* | |
2864 | * We can't just restart the syscall, since previously | |
2865 | * submitted sqes may already be in progress. Just fail this | |
2866 | * IO with EINTR. | |
2867 | */ | |
2868 | ret = -EINTR; | |
df561f66 | 2869 | fallthrough; |
2b188cc1 JA |
2870 | default: |
2871 | kiocb->ki_complete(kiocb, ret, 0); | |
2872 | } | |
2873 | } | |
2874 | ||
a1d7c393 | 2875 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
889fca73 | 2876 | unsigned int issue_flags) |
ba816ad6 | 2877 | { |
ba04291e | 2878 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
e8c2bc1f | 2879 | struct io_async_rw *io = req->async_data; |
97284637 | 2880 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
ba04291e | 2881 | |
227c0c96 | 2882 | /* add previously done IO, if any */ |
e8c2bc1f | 2883 | if (io && io->bytes_done > 0) { |
227c0c96 | 2884 | if (ret < 0) |
e8c2bc1f | 2885 | ret = io->bytes_done; |
227c0c96 | 2886 | else |
e8c2bc1f | 2887 | ret += io->bytes_done; |
227c0c96 JA |
2888 | } |
2889 | ||
ba04291e JA |
2890 | if (req->flags & REQ_F_CUR_POS) |
2891 | req->file->f_pos = kiocb->ki_pos; | |
e149bd74 | 2892 | if (ret >= 0 && check_reissue) |
889fca73 | 2893 | __io_complete_rw(req, ret, 0, issue_flags); |
ba816ad6 JA |
2894 | else |
2895 | io_rw_done(kiocb, ret); | |
97284637 | 2896 | |
fe7e3257 | 2897 | if (check_reissue && (req->flags & REQ_F_REISSUE)) { |
97284637 | 2898 | req->flags &= ~REQ_F_REISSUE; |
a7be7c23 | 2899 | if (io_resubmit_prep(req)) { |
773af691 | 2900 | io_req_task_queue_reissue(req); |
8c130827 | 2901 | } else { |
93d2bcd2 | 2902 | req_set_fail(req); |
ae421d93 PB |
2903 | __io_req_complete(req, issue_flags, ret, |
2904 | io_put_rw_kbuf(req)); | |
97284637 PB |
2905 | } |
2906 | } | |
ba816ad6 JA |
2907 | } |
2908 | ||
eae071c9 PB |
2909 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
2910 | struct io_mapped_ubuf *imu) | |
edafccee | 2911 | { |
9adbd45d | 2912 | size_t len = req->rw.len; |
75769e3f | 2913 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 2914 | size_t offset; |
edafccee | 2915 | |
75769e3f | 2916 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
2917 | return -EFAULT; |
2918 | /* not inside the mapped region */ | |
4751f53d | 2919 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
2920 | return -EFAULT; |
2921 | ||
2922 | /* | |
2923 | * May not be a start of buffer, set size appropriately | |
2924 | * and advance us to the beginning. | |
2925 | */ | |
2926 | offset = buf_addr - imu->ubuf; | |
2927 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
2928 | |
2929 | if (offset) { | |
2930 | /* | |
2931 | * Don't use iov_iter_advance() here, as it's really slow for | |
2932 | * using the latter parts of a big fixed buffer - it iterates | |
2933 | * over each segment manually. We can cheat a bit here, because | |
2934 | * we know that: | |
2935 | * | |
2936 | * 1) it's a BVEC iter, we set it up | |
2937 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
2938 | * first and last bvec | |
2939 | * | |
2940 | * So just find our index, and adjust the iterator afterwards. | |
2941 | * If the offset is within the first bvec (or the whole first | |
2942 | * bvec, just use iov_iter_advance(). This makes it easier | |
2943 | * since we can just skip the first segment, which may not | |
2944 | * be PAGE_SIZE aligned. | |
2945 | */ | |
2946 | const struct bio_vec *bvec = imu->bvec; | |
2947 | ||
2948 | if (offset <= bvec->bv_len) { | |
2949 | iov_iter_advance(iter, offset); | |
2950 | } else { | |
2951 | unsigned long seg_skip; | |
2952 | ||
2953 | /* skip first vec */ | |
2954 | offset -= bvec->bv_len; | |
2955 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2956 | ||
2957 | iter->bvec = bvec + seg_skip; | |
2958 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2959 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2960 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2961 | } |
2962 | } | |
2963 | ||
847595de | 2964 | return 0; |
edafccee JA |
2965 | } |
2966 | ||
eae071c9 PB |
2967 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
2968 | { | |
2969 | struct io_ring_ctx *ctx = req->ctx; | |
2970 | struct io_mapped_ubuf *imu = req->imu; | |
2971 | u16 index, buf_index = req->buf_index; | |
2972 | ||
2973 | if (likely(!imu)) { | |
2974 | if (unlikely(buf_index >= ctx->nr_user_bufs)) | |
2975 | return -EFAULT; | |
2976 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
2977 | imu = READ_ONCE(ctx->user_bufs[index]); | |
2978 | req->imu = imu; | |
2979 | } | |
2980 | return __io_import_fixed(req, rw, iter, imu); | |
2981 | } | |
2982 | ||
bcda7baa JA |
2983 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
2984 | { | |
2985 | if (needs_lock) | |
2986 | mutex_unlock(&ctx->uring_lock); | |
2987 | } | |
2988 | ||
2989 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
2990 | { | |
2991 | /* | |
2992 | * "Normal" inline submissions always hold the uring_lock, since we | |
2993 | * grab it from the system call. Same is true for the SQPOLL offload. | |
2994 | * The only exception is when we've detached the request and issue it | |
2995 | * from an async worker thread, grab the lock for that case. | |
2996 | */ | |
2997 | if (needs_lock) | |
2998 | mutex_lock(&ctx->uring_lock); | |
2999 | } | |
3000 | ||
3001 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, | |
3002 | int bgid, struct io_buffer *kbuf, | |
3003 | bool needs_lock) | |
3004 | { | |
3005 | struct io_buffer *head; | |
3006 | ||
3007 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3008 | return kbuf; | |
3009 | ||
3010 | io_ring_submit_lock(req->ctx, needs_lock); | |
3011 | ||
3012 | lockdep_assert_held(&req->ctx->uring_lock); | |
3013 | ||
9e15c3a0 | 3014 | head = xa_load(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3015 | if (head) { |
3016 | if (!list_empty(&head->list)) { | |
3017 | kbuf = list_last_entry(&head->list, struct io_buffer, | |
3018 | list); | |
3019 | list_del(&kbuf->list); | |
3020 | } else { | |
3021 | kbuf = head; | |
9e15c3a0 | 3022 | xa_erase(&req->ctx->io_buffers, bgid); |
bcda7baa JA |
3023 | } |
3024 | if (*len > kbuf->len) | |
3025 | *len = kbuf->len; | |
3026 | } else { | |
3027 | kbuf = ERR_PTR(-ENOBUFS); | |
3028 | } | |
3029 | ||
3030 | io_ring_submit_unlock(req->ctx, needs_lock); | |
3031 | ||
3032 | return kbuf; | |
3033 | } | |
3034 | ||
4d954c25 JA |
3035 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
3036 | bool needs_lock) | |
3037 | { | |
3038 | struct io_buffer *kbuf; | |
4f4eeba8 | 3039 | u16 bgid; |
4d954c25 JA |
3040 | |
3041 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
4f4eeba8 | 3042 | bgid = req->buf_index; |
4d954c25 JA |
3043 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
3044 | if (IS_ERR(kbuf)) | |
3045 | return kbuf; | |
3046 | req->rw.addr = (u64) (unsigned long) kbuf; | |
3047 | req->flags |= REQ_F_BUFFER_SELECTED; | |
3048 | return u64_to_user_ptr(kbuf->addr); | |
3049 | } | |
3050 | ||
3051 | #ifdef CONFIG_COMPAT | |
3052 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
3053 | bool needs_lock) | |
3054 | { | |
3055 | struct compat_iovec __user *uiov; | |
3056 | compat_ssize_t clen; | |
3057 | void __user *buf; | |
3058 | ssize_t len; | |
3059 | ||
3060 | uiov = u64_to_user_ptr(req->rw.addr); | |
3061 | if (!access_ok(uiov, sizeof(*uiov))) | |
3062 | return -EFAULT; | |
3063 | if (__get_user(clen, &uiov->iov_len)) | |
3064 | return -EFAULT; | |
3065 | if (clen < 0) | |
3066 | return -EINVAL; | |
3067 | ||
3068 | len = clen; | |
3069 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3070 | if (IS_ERR(buf)) | |
3071 | return PTR_ERR(buf); | |
3072 | iov[0].iov_base = buf; | |
3073 | iov[0].iov_len = (compat_size_t) len; | |
3074 | return 0; | |
3075 | } | |
3076 | #endif | |
3077 | ||
3078 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3079 | bool needs_lock) | |
3080 | { | |
3081 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3082 | void __user *buf; | |
3083 | ssize_t len; | |
3084 | ||
3085 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3086 | return -EFAULT; | |
3087 | ||
3088 | len = iov[0].iov_len; | |
3089 | if (len < 0) | |
3090 | return -EINVAL; | |
3091 | buf = io_rw_buffer_select(req, &len, needs_lock); | |
3092 | if (IS_ERR(buf)) | |
3093 | return PTR_ERR(buf); | |
3094 | iov[0].iov_base = buf; | |
3095 | iov[0].iov_len = len; | |
3096 | return 0; | |
3097 | } | |
3098 | ||
3099 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
3100 | bool needs_lock) | |
3101 | { | |
dddb3e26 JA |
3102 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
3103 | struct io_buffer *kbuf; | |
3104 | ||
3105 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; | |
3106 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); | |
3107 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3108 | return 0; |
dddb3e26 | 3109 | } |
dd201662 | 3110 | if (req->rw.len != 1) |
4d954c25 JA |
3111 | return -EINVAL; |
3112 | ||
3113 | #ifdef CONFIG_COMPAT | |
3114 | if (req->ctx->compat) | |
3115 | return io_compat_import(req, iov, needs_lock); | |
3116 | #endif | |
3117 | ||
3118 | return __io_iov_buffer_select(req, iov, needs_lock); | |
3119 | } | |
3120 | ||
847595de PB |
3121 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
3122 | struct iov_iter *iter, bool needs_lock) | |
2b188cc1 | 3123 | { |
9adbd45d JA |
3124 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
3125 | size_t sqe_len = req->rw.len; | |
847595de | 3126 | u8 opcode = req->opcode; |
4d954c25 | 3127 | ssize_t ret; |
edafccee | 3128 | |
7d009165 | 3129 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 3130 | *iovec = NULL; |
9adbd45d | 3131 | return io_import_fixed(req, rw, iter); |
edafccee | 3132 | } |
2b188cc1 | 3133 | |
bcda7baa | 3134 | /* buffer index only valid with fixed read/write, or buffer select */ |
4f4eeba8 | 3135 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
9adbd45d JA |
3136 | return -EINVAL; |
3137 | ||
3a6820f2 | 3138 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3139 | if (req->flags & REQ_F_BUFFER_SELECT) { |
4d954c25 | 3140 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
867a23ea | 3141 | if (IS_ERR(buf)) |
4d954c25 | 3142 | return PTR_ERR(buf); |
3f9d6441 | 3143 | req->rw.len = sqe_len; |
bcda7baa JA |
3144 | } |
3145 | ||
3a6820f2 JA |
3146 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
3147 | *iovec = NULL; | |
10fc72e4 | 3148 | return ret; |
3a6820f2 JA |
3149 | } |
3150 | ||
4d954c25 JA |
3151 | if (req->flags & REQ_F_BUFFER_SELECT) { |
3152 | ret = io_iov_buffer_select(req, *iovec, needs_lock); | |
847595de PB |
3153 | if (!ret) |
3154 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); | |
4d954c25 JA |
3155 | *iovec = NULL; |
3156 | return ret; | |
3157 | } | |
3158 | ||
89cd35c5 CH |
3159 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
3160 | req->ctx->compat); | |
2b188cc1 JA |
3161 | } |
3162 | ||
0fef9483 JA |
3163 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3164 | { | |
5b09e37e | 3165 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3166 | } |
3167 | ||
31b51510 | 3168 | /* |
32960613 JA |
3169 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3170 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3171 | */ |
4017eb91 | 3172 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3173 | { |
4017eb91 JA |
3174 | struct kiocb *kiocb = &req->rw.kiocb; |
3175 | struct file *file = req->file; | |
32960613 JA |
3176 | ssize_t ret = 0; |
3177 | ||
3178 | /* | |
3179 | * Don't support polled IO through this interface, and we can't | |
3180 | * support non-blocking either. For the latter, this just causes | |
3181 | * the kiocb to be handled from an async context. | |
3182 | */ | |
3183 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3184 | return -EOPNOTSUPP; | |
3185 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
3186 | return -EAGAIN; | |
3187 | ||
3188 | while (iov_iter_count(iter)) { | |
311ae9e1 | 3189 | struct iovec iovec; |
32960613 JA |
3190 | ssize_t nr; |
3191 | ||
311ae9e1 PB |
3192 | if (!iov_iter_is_bvec(iter)) { |
3193 | iovec = iov_iter_iovec(iter); | |
3194 | } else { | |
4017eb91 JA |
3195 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3196 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3197 | } |
3198 | ||
32960613 JA |
3199 | if (rw == READ) { |
3200 | nr = file->f_op->read(file, iovec.iov_base, | |
0fef9483 | 3201 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3202 | } else { |
3203 | nr = file->f_op->write(file, iovec.iov_base, | |
0fef9483 | 3204 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
32960613 JA |
3205 | } |
3206 | ||
3207 | if (nr < 0) { | |
3208 | if (!ret) | |
3209 | ret = nr; | |
3210 | break; | |
3211 | } | |
3212 | ret += nr; | |
3213 | if (nr != iovec.iov_len) | |
3214 | break; | |
4017eb91 JA |
3215 | req->rw.len -= nr; |
3216 | req->rw.addr += nr; | |
32960613 JA |
3217 | iov_iter_advance(iter, nr); |
3218 | } | |
3219 | ||
3220 | return ret; | |
3221 | } | |
3222 | ||
ff6165b2 JA |
3223 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3224 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3225 | { |
e8c2bc1f | 3226 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3227 | |
ff6165b2 | 3228 | memcpy(&rw->iter, iter, sizeof(*iter)); |
afb87658 | 3229 | rw->free_iovec = iovec; |
227c0c96 | 3230 | rw->bytes_done = 0; |
ff6165b2 | 3231 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3232 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3233 | return; |
b64e3444 | 3234 | if (!iovec) { |
ff6165b2 JA |
3235 | unsigned iov_off = 0; |
3236 | ||
3237 | rw->iter.iov = rw->fast_iov; | |
3238 | if (iter->iov != fast_iov) { | |
3239 | iov_off = iter->iov - fast_iov; | |
3240 | rw->iter.iov += iov_off; | |
3241 | } | |
3242 | if (rw->fast_iov != fast_iov) | |
3243 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3244 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3245 | } else { |
3246 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3247 | } |
3248 | } | |
3249 | ||
6cb78689 | 3250 | static inline int io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3251 | { |
e8c2bc1f JA |
3252 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3253 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
3254 | return req->async_data == NULL; | |
3d9932a8 XW |
3255 | } |
3256 | ||
ff6165b2 JA |
3257 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
3258 | const struct iovec *fast_iov, | |
227c0c96 | 3259 | struct iov_iter *iter, bool force) |
b7bb4f7d | 3260 | { |
26f0505a | 3261 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3262 | return 0; |
e8c2bc1f | 3263 | if (!req->async_data) { |
6cb78689 | 3264 | if (io_alloc_async_data(req)) { |
6bf985dc | 3265 | kfree(iovec); |
5d204bcf | 3266 | return -ENOMEM; |
6bf985dc | 3267 | } |
b7bb4f7d | 3268 | |
ff6165b2 | 3269 | io_req_map_rw(req, iovec, fast_iov, iter); |
5d204bcf | 3270 | } |
b7bb4f7d | 3271 | return 0; |
f67676d1 JA |
3272 | } |
3273 | ||
73debe68 | 3274 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3275 | { |
e8c2bc1f | 3276 | struct io_async_rw *iorw = req->async_data; |
f4bff104 | 3277 | struct iovec *iov = iorw->fast_iov; |
847595de | 3278 | int ret; |
c3e330a4 | 3279 | |
2846c481 | 3280 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
c3e330a4 PB |
3281 | if (unlikely(ret < 0)) |
3282 | return ret; | |
3283 | ||
ab0b196c PB |
3284 | iorw->bytes_done = 0; |
3285 | iorw->free_iovec = iov; | |
3286 | if (iov) | |
3287 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3288 | return 0; |
3289 | } | |
3290 | ||
73debe68 | 3291 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3292 | { |
3529d8c2 JA |
3293 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3294 | return -EBADF; | |
93642ef8 | 3295 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3296 | } |
3297 | ||
c1dd91d1 JA |
3298 | /* |
3299 | * This is our waitqueue callback handler, registered through lock_page_async() | |
3300 | * when we initially tried to do the IO with the iocb armed our waitqueue. | |
3301 | * This gets called when the page is unlocked, and we generally expect that to | |
3302 | * happen when the page IO is completed and the page is now uptodate. This will | |
3303 | * queue a task_work based retry of the operation, attempting to copy the data | |
3304 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3305 | * do a thread based blocking retry of the operation. That's the unexpected | |
3306 | * slow path. | |
3307 | */ | |
bcf5a063 JA |
3308 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3309 | int sync, void *arg) | |
3310 | { | |
3311 | struct wait_page_queue *wpq; | |
3312 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3313 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3314 | |
3315 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3316 | ||
cdc8fcb4 LT |
3317 | if (!wake_page_match(wpq, key)) |
3318 | return 0; | |
3319 | ||
c8d317aa | 3320 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3321 | list_del_init(&wait->entry); |
921b9054 | 3322 | io_req_task_queue(req); |
bcf5a063 JA |
3323 | return 1; |
3324 | } | |
3325 | ||
c1dd91d1 JA |
3326 | /* |
3327 | * This controls whether a given IO request should be armed for async page | |
3328 | * based retry. If we return false here, the request is handed to the async | |
3329 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3330 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3331 | * will either succeed because the page is now uptodate and unlocked, or it | |
3332 | * will register a callback when the page is unlocked at IO completion. Through | |
3333 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3334 | * That retry will attempt the buffered read again. The retry will generally | |
3335 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3336 | * async worker threads for a blocking retry. | |
3337 | */ | |
227c0c96 | 3338 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3339 | { |
e8c2bc1f JA |
3340 | struct io_async_rw *rw = req->async_data; |
3341 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3342 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3343 | |
bcf5a063 JA |
3344 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3345 | if (req->flags & REQ_F_NOWAIT) | |
3346 | return false; | |
f67676d1 | 3347 | |
227c0c96 | 3348 | /* Only for buffered IO */ |
3b2a4439 | 3349 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3350 | return false; |
3b2a4439 | 3351 | |
bcf5a063 JA |
3352 | /* |
3353 | * just use poll if we can, and don't attempt if the fs doesn't | |
3354 | * support callback based unlocks | |
3355 | */ | |
3356 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3357 | return false; | |
f67676d1 | 3358 | |
3b2a4439 JA |
3359 | wait->wait.func = io_async_buf_func; |
3360 | wait->wait.private = req; | |
3361 | wait->wait.flags = 0; | |
3362 | INIT_LIST_HEAD(&wait->wait.entry); | |
3363 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3364 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3365 | kiocb->ki_waitq = wait; |
3b2a4439 | 3366 | return true; |
bcf5a063 JA |
3367 | } |
3368 | ||
aeab9506 | 3369 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 JA |
3370 | { |
3371 | if (req->file->f_op->read_iter) | |
3372 | return call_read_iter(req->file, &req->rw.kiocb, iter); | |
2dd2111d | 3373 | else if (req->file->f_op->read) |
4017eb91 | 3374 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3375 | else |
3376 | return -EINVAL; | |
f67676d1 JA |
3377 | } |
3378 | ||
889fca73 | 3379 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3380 | { |
3381 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3382 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3383 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3384 | struct io_async_rw *rw = req->async_data; |
227c0c96 | 3385 | ssize_t io_size, ret, ret2; |
45d189c6 | 3386 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ff6165b2 | 3387 | |
2846c481 | 3388 | if (rw) { |
e8c2bc1f | 3389 | iter = &rw->iter; |
2846c481 PB |
3390 | iovec = NULL; |
3391 | } else { | |
3392 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); | |
3393 | if (ret < 0) | |
3394 | return ret; | |
3395 | } | |
632546c4 | 3396 | io_size = iov_iter_count(iter); |
fa15bafb | 3397 | req->result = io_size; |
2b188cc1 | 3398 | |
fd6c2e4c JA |
3399 | /* Ensure we clear previously set non-block flag */ |
3400 | if (!force_nonblock) | |
29de5f6a | 3401 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
a88fc400 PB |
3402 | else |
3403 | kiocb->ki_flags |= IOCB_NOWAIT; | |
3404 | ||
24c74678 | 3405 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3406 | if (force_nonblock && !io_file_supports_nowait(req, READ)) { |
6713e7a6 | 3407 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc | 3408 | return ret ?: -EAGAIN; |
6713e7a6 | 3409 | } |
9e645e11 | 3410 | |
632546c4 | 3411 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
5ea5dd45 PB |
3412 | if (unlikely(ret)) { |
3413 | kfree(iovec); | |
3414 | return ret; | |
3415 | } | |
2b188cc1 | 3416 | |
227c0c96 | 3417 | ret = io_iter_do_read(req, iter); |
32960613 | 3418 | |
230d50d4 | 3419 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3420 | req->flags &= ~REQ_F_REISSUE; |
eefdf30f JA |
3421 | /* IOPOLL retry should happen for io-wq threads */ |
3422 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3423 | goto done; |
75c668cd PB |
3424 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3425 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3426 | goto done; |
84216315 | 3427 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3428 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
f38c7e3a | 3429 | ret = 0; |
230d50d4 JA |
3430 | } else if (ret == -EIOCBQUEUED) { |
3431 | goto out_free; | |
7335e3bf | 3432 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
75c668cd | 3433 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
7335e3bf | 3434 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3435 | goto done; |
227c0c96 JA |
3436 | } |
3437 | ||
227c0c96 | 3438 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
6bf985dc PB |
3439 | if (ret2) |
3440 | return ret2; | |
3441 | ||
fe1cdd55 | 3442 | iovec = NULL; |
e8c2bc1f | 3443 | rw = req->async_data; |
227c0c96 | 3444 | /* now use our persistent iterator, if we aren't already */ |
e8c2bc1f | 3445 | iter = &rw->iter; |
227c0c96 | 3446 | |
b23df91b PB |
3447 | do { |
3448 | io_size -= ret; | |
3449 | rw->bytes_done += ret; | |
3450 | /* if we can retry, do so with the callbacks armed */ | |
3451 | if (!io_rw_should_retry(req)) { | |
3452 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3453 | return -EAGAIN; | |
3454 | } | |
3455 | ||
3456 | /* | |
3457 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3458 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3459 | * desired page gets unlocked. We can also get a partial read | |
3460 | * here, and if we do, then just retry at the new offset. | |
3461 | */ | |
3462 | ret = io_iter_do_read(req, iter); | |
3463 | if (ret == -EIOCBQUEUED) | |
3464 | return 0; | |
227c0c96 | 3465 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3466 | kiocb->ki_flags &= ~IOCB_WAITQ; |
b23df91b | 3467 | } while (ret > 0 && ret < io_size); |
227c0c96 | 3468 | done: |
889fca73 | 3469 | kiocb_done(kiocb, ret, issue_flags); |
fe1cdd55 PB |
3470 | out_free: |
3471 | /* it's faster to check here then delegate to kfree */ | |
3472 | if (iovec) | |
3473 | kfree(iovec); | |
5ea5dd45 | 3474 | return 0; |
2b188cc1 JA |
3475 | } |
3476 | ||
73debe68 | 3477 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3478 | { |
3529d8c2 JA |
3479 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3480 | return -EBADF; | |
93642ef8 | 3481 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3482 | } |
3483 | ||
889fca73 | 3484 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3485 | { |
3486 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 3487 | struct kiocb *kiocb = &req->rw.kiocb; |
ff6165b2 | 3488 | struct iov_iter __iter, *iter = &__iter; |
e8c2bc1f | 3489 | struct io_async_rw *rw = req->async_data; |
fa15bafb | 3490 | ssize_t ret, ret2, io_size; |
45d189c6 | 3491 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
2b188cc1 | 3492 | |
2846c481 | 3493 | if (rw) { |
e8c2bc1f | 3494 | iter = &rw->iter; |
2846c481 PB |
3495 | iovec = NULL; |
3496 | } else { | |
3497 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); | |
3498 | if (ret < 0) | |
3499 | return ret; | |
3500 | } | |
632546c4 | 3501 | io_size = iov_iter_count(iter); |
fa15bafb | 3502 | req->result = io_size; |
2b188cc1 | 3503 | |
fd6c2e4c JA |
3504 | /* Ensure we clear previously set non-block flag */ |
3505 | if (!force_nonblock) | |
a88fc400 PB |
3506 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3507 | else | |
3508 | kiocb->ki_flags |= IOCB_NOWAIT; | |
fd6c2e4c | 3509 | |
24c74678 | 3510 | /* If the file doesn't support async, just async punt */ |
b191e2df | 3511 | if (force_nonblock && !io_file_supports_nowait(req, WRITE)) |
f67676d1 | 3512 | goto copy_iov; |
31b51510 | 3513 | |
10d59345 JA |
3514 | /* file path doesn't support NOWAIT for non-direct_IO */ |
3515 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
3516 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 3517 | goto copy_iov; |
31b51510 | 3518 | |
632546c4 | 3519 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
fa15bafb PB |
3520 | if (unlikely(ret)) |
3521 | goto out_free; | |
4ed734b0 | 3522 | |
fa15bafb PB |
3523 | /* |
3524 | * Open-code file_start_write here to grab freeze protection, | |
3525 | * which will be released by another thread in | |
3526 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
3527 | * released so that it doesn't complain about the held lock when | |
3528 | * we return to userspace. | |
3529 | */ | |
3530 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 3531 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
3532 | __sb_writers_release(file_inode(req->file)->i_sb, |
3533 | SB_FREEZE_WRITE); | |
3534 | } | |
3535 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 3536 | |
fa15bafb | 3537 | if (req->file->f_op->write_iter) |
ff6165b2 | 3538 | ret2 = call_write_iter(req->file, kiocb, iter); |
2dd2111d | 3539 | else if (req->file->f_op->write) |
4017eb91 | 3540 | ret2 = loop_rw_iter(WRITE, req, iter); |
2dd2111d GH |
3541 | else |
3542 | ret2 = -EINVAL; | |
4ed734b0 | 3543 | |
6ad7f233 PB |
3544 | if (req->flags & REQ_F_REISSUE) { |
3545 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 3546 | ret2 = -EAGAIN; |
6ad7f233 | 3547 | } |
230d50d4 | 3548 | |
fa15bafb PB |
3549 | /* |
3550 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
3551 | * retry them without IOCB_NOWAIT. | |
3552 | */ | |
3553 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
3554 | ret2 = -EAGAIN; | |
75c668cd PB |
3555 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3556 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 3557 | goto done; |
fa15bafb | 3558 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f JA |
3559 | /* IOPOLL retry should happen for io-wq threads */ |
3560 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) | |
3561 | goto copy_iov; | |
355afaeb | 3562 | done: |
889fca73 | 3563 | kiocb_done(kiocb, ret2, issue_flags); |
fa15bafb | 3564 | } else { |
f67676d1 | 3565 | copy_iov: |
84216315 | 3566 | /* some cases will consume bytes even on error returns */ |
632546c4 | 3567 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
227c0c96 | 3568 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
6bf985dc | 3569 | return ret ?: -EAGAIN; |
2b188cc1 | 3570 | } |
31b51510 | 3571 | out_free: |
f261c168 | 3572 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 3573 | if (iovec) |
6f2cc166 | 3574 | kfree(iovec); |
2b188cc1 JA |
3575 | return ret; |
3576 | } | |
3577 | ||
80a261fd JA |
3578 | static int io_renameat_prep(struct io_kiocb *req, |
3579 | const struct io_uring_sqe *sqe) | |
3580 | { | |
3581 | struct io_rename *ren = &req->rename; | |
3582 | const char __user *oldf, *newf; | |
3583 | ||
ed7eb259 JA |
3584 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3585 | return -EINVAL; | |
26578cda | 3586 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 3587 | return -EINVAL; |
80a261fd JA |
3588 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3589 | return -EBADF; | |
3590 | ||
3591 | ren->old_dfd = READ_ONCE(sqe->fd); | |
3592 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3593 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
3594 | ren->new_dfd = READ_ONCE(sqe->len); | |
3595 | ren->flags = READ_ONCE(sqe->rename_flags); | |
3596 | ||
3597 | ren->oldpath = getname(oldf); | |
3598 | if (IS_ERR(ren->oldpath)) | |
3599 | return PTR_ERR(ren->oldpath); | |
3600 | ||
3601 | ren->newpath = getname(newf); | |
3602 | if (IS_ERR(ren->newpath)) { | |
3603 | putname(ren->oldpath); | |
3604 | return PTR_ERR(ren->newpath); | |
3605 | } | |
3606 | ||
3607 | req->flags |= REQ_F_NEED_CLEANUP; | |
3608 | return 0; | |
3609 | } | |
3610 | ||
45d189c6 | 3611 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
3612 | { |
3613 | struct io_rename *ren = &req->rename; | |
3614 | int ret; | |
3615 | ||
45d189c6 | 3616 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
3617 | return -EAGAIN; |
3618 | ||
3619 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
3620 | ren->newpath, ren->flags); | |
3621 | ||
3622 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3623 | if (ret < 0) | |
93d2bcd2 | 3624 | req_set_fail(req); |
80a261fd JA |
3625 | io_req_complete(req, ret); |
3626 | return 0; | |
3627 | } | |
3628 | ||
14a1143b JA |
3629 | static int io_unlinkat_prep(struct io_kiocb *req, |
3630 | const struct io_uring_sqe *sqe) | |
3631 | { | |
3632 | struct io_unlink *un = &req->unlink; | |
3633 | const char __user *fname; | |
3634 | ||
22634bc5 JA |
3635 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3636 | return -EINVAL; | |
26578cda PB |
3637 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
3638 | sqe->splice_fd_in) | |
22634bc5 | 3639 | return -EINVAL; |
14a1143b JA |
3640 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
3641 | return -EBADF; | |
3642 | ||
3643 | un->dfd = READ_ONCE(sqe->fd); | |
3644 | ||
3645 | un->flags = READ_ONCE(sqe->unlink_flags); | |
3646 | if (un->flags & ~AT_REMOVEDIR) | |
3647 | return -EINVAL; | |
3648 | ||
3649 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
3650 | un->filename = getname(fname); | |
3651 | if (IS_ERR(un->filename)) | |
3652 | return PTR_ERR(un->filename); | |
3653 | ||
3654 | req->flags |= REQ_F_NEED_CLEANUP; | |
3655 | return 0; | |
3656 | } | |
3657 | ||
45d189c6 | 3658 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
3659 | { |
3660 | struct io_unlink *un = &req->unlink; | |
3661 | int ret; | |
3662 | ||
45d189c6 | 3663 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
3664 | return -EAGAIN; |
3665 | ||
3666 | if (un->flags & AT_REMOVEDIR) | |
3667 | ret = do_rmdir(un->dfd, un->filename); | |
3668 | else | |
3669 | ret = do_unlinkat(un->dfd, un->filename); | |
3670 | ||
3671 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
3672 | if (ret < 0) | |
93d2bcd2 | 3673 | req_set_fail(req); |
14a1143b JA |
3674 | io_req_complete(req, ret); |
3675 | return 0; | |
3676 | } | |
3677 | ||
36f4fa68 JA |
3678 | static int io_shutdown_prep(struct io_kiocb *req, |
3679 | const struct io_uring_sqe *sqe) | |
3680 | { | |
3681 | #if defined(CONFIG_NET) | |
3682 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3683 | return -EINVAL; | |
26578cda PB |
3684 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
3685 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
3686 | return -EINVAL; |
3687 | ||
3688 | req->shutdown.how = READ_ONCE(sqe->len); | |
3689 | return 0; | |
3690 | #else | |
3691 | return -EOPNOTSUPP; | |
3692 | #endif | |
3693 | } | |
3694 | ||
45d189c6 | 3695 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
3696 | { |
3697 | #if defined(CONFIG_NET) | |
3698 | struct socket *sock; | |
3699 | int ret; | |
3700 | ||
45d189c6 | 3701 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
3702 | return -EAGAIN; |
3703 | ||
48aba79b | 3704 | sock = sock_from_file(req->file); |
36f4fa68 | 3705 | if (unlikely(!sock)) |
48aba79b | 3706 | return -ENOTSOCK; |
36f4fa68 JA |
3707 | |
3708 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 3709 | if (ret < 0) |
93d2bcd2 | 3710 | req_set_fail(req); |
36f4fa68 JA |
3711 | io_req_complete(req, ret); |
3712 | return 0; | |
3713 | #else | |
3714 | return -EOPNOTSUPP; | |
3715 | #endif | |
3716 | } | |
3717 | ||
f2a8d5c7 PB |
3718 | static int __io_splice_prep(struct io_kiocb *req, |
3719 | const struct io_uring_sqe *sqe) | |
7d67af2c | 3720 | { |
fe7e3257 | 3721 | struct io_splice *sp = &req->splice; |
7d67af2c | 3722 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 3723 | |
3232dd02 PB |
3724 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3725 | return -EINVAL; | |
7d67af2c PB |
3726 | |
3727 | sp->file_in = NULL; | |
7d67af2c PB |
3728 | sp->len = READ_ONCE(sqe->len); |
3729 | sp->flags = READ_ONCE(sqe->splice_flags); | |
3730 | ||
3731 | if (unlikely(sp->flags & ~valid_flags)) | |
3732 | return -EINVAL; | |
3733 | ||
62906e89 | 3734 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
3735 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
3736 | if (!sp->file_in) | |
3737 | return -EBADF; | |
7d67af2c | 3738 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
3739 | return 0; |
3740 | } | |
3741 | ||
f2a8d5c7 PB |
3742 | static int io_tee_prep(struct io_kiocb *req, |
3743 | const struct io_uring_sqe *sqe) | |
3744 | { | |
3745 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
3746 | return -EINVAL; | |
3747 | return __io_splice_prep(req, sqe); | |
3748 | } | |
3749 | ||
45d189c6 | 3750 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
3751 | { |
3752 | struct io_splice *sp = &req->splice; | |
3753 | struct file *in = sp->file_in; | |
3754 | struct file *out = sp->file_out; | |
3755 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3756 | long ret = 0; | |
3757 | ||
45d189c6 | 3758 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
3759 | return -EAGAIN; |
3760 | if (sp->len) | |
3761 | ret = do_tee(in, out, sp->len, flags); | |
3762 | ||
e1d767f0 PB |
3763 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3764 | io_put_file(in); | |
f2a8d5c7 PB |
3765 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3766 | ||
f2a8d5c7 | 3767 | if (ret != sp->len) |
93d2bcd2 | 3768 | req_set_fail(req); |
e1e16097 | 3769 | io_req_complete(req, ret); |
f2a8d5c7 PB |
3770 | return 0; |
3771 | } | |
3772 | ||
3773 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
3774 | { | |
fe7e3257 | 3775 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
3776 | |
3777 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
3778 | sp->off_out = READ_ONCE(sqe->off); | |
3779 | return __io_splice_prep(req, sqe); | |
3780 | } | |
3781 | ||
45d189c6 | 3782 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
3783 | { |
3784 | struct io_splice *sp = &req->splice; | |
3785 | struct file *in = sp->file_in; | |
3786 | struct file *out = sp->file_out; | |
3787 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
3788 | loff_t *poff_in, *poff_out; | |
c9687426 | 3789 | long ret = 0; |
7d67af2c | 3790 | |
45d189c6 | 3791 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 3792 | return -EAGAIN; |
7d67af2c PB |
3793 | |
3794 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
3795 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 3796 | |
948a7749 | 3797 | if (sp->len) |
c9687426 | 3798 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 3799 | |
e1d767f0 PB |
3800 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
3801 | io_put_file(in); | |
7d67af2c PB |
3802 | req->flags &= ~REQ_F_NEED_CLEANUP; |
3803 | ||
7d67af2c | 3804 | if (ret != sp->len) |
93d2bcd2 | 3805 | req_set_fail(req); |
e1e16097 | 3806 | io_req_complete(req, ret); |
7d67af2c PB |
3807 | return 0; |
3808 | } | |
3809 | ||
2b188cc1 JA |
3810 | /* |
3811 | * IORING_OP_NOP just posts a completion event, nothing else. | |
3812 | */ | |
889fca73 | 3813 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
3814 | { |
3815 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 3816 | |
def596e9 JA |
3817 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
3818 | return -EINVAL; | |
3819 | ||
889fca73 | 3820 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
3821 | return 0; |
3822 | } | |
3823 | ||
1155c76a | 3824 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 3825 | { |
6b06314c | 3826 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 3827 | |
09bb8394 JA |
3828 | if (!req->file) |
3829 | return -EBADF; | |
c992fe29 | 3830 | |
6b06314c | 3831 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 3832 | return -EINVAL; |
26578cda PB |
3833 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
3834 | sqe->splice_fd_in)) | |
c992fe29 CH |
3835 | return -EINVAL; |
3836 | ||
8ed8d3c3 JA |
3837 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
3838 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
3839 | return -EINVAL; | |
3840 | ||
3841 | req->sync.off = READ_ONCE(sqe->off); | |
3842 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
3843 | return 0; |
3844 | } | |
3845 | ||
45d189c6 | 3846 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 3847 | { |
8ed8d3c3 | 3848 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
3849 | int ret; |
3850 | ||
ac45abc0 | 3851 | /* fsync always requires a blocking context */ |
45d189c6 | 3852 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
3853 | return -EAGAIN; |
3854 | ||
9adbd45d | 3855 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
3856 | end > 0 ? end : LLONG_MAX, |
3857 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
3858 | if (ret < 0) | |
93d2bcd2 | 3859 | req_set_fail(req); |
e1e16097 | 3860 | io_req_complete(req, ret); |
c992fe29 CH |
3861 | return 0; |
3862 | } | |
3863 | ||
d63d1b5e JA |
3864 | static int io_fallocate_prep(struct io_kiocb *req, |
3865 | const struct io_uring_sqe *sqe) | |
3866 | { | |
26578cda PB |
3867 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
3868 | sqe->splice_fd_in) | |
d63d1b5e | 3869 | return -EINVAL; |
3232dd02 PB |
3870 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3871 | return -EINVAL; | |
d63d1b5e JA |
3872 | |
3873 | req->sync.off = READ_ONCE(sqe->off); | |
3874 | req->sync.len = READ_ONCE(sqe->addr); | |
3875 | req->sync.mode = READ_ONCE(sqe->len); | |
3876 | return 0; | |
3877 | } | |
3878 | ||
45d189c6 | 3879 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 3880 | { |
ac45abc0 PB |
3881 | int ret; |
3882 | ||
d63d1b5e | 3883 | /* fallocate always requiring blocking context */ |
45d189c6 | 3884 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 3885 | return -EAGAIN; |
ac45abc0 PB |
3886 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
3887 | req->sync.len); | |
ac45abc0 | 3888 | if (ret < 0) |
93d2bcd2 | 3889 | req_set_fail(req); |
e1e16097 | 3890 | io_req_complete(req, ret); |
5d17b4a4 JA |
3891 | return 0; |
3892 | } | |
3893 | ||
ec65fea5 | 3894 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 3895 | { |
f8748881 | 3896 | const char __user *fname; |
15b71abe | 3897 | int ret; |
b7bb4f7d | 3898 | |
d3fddf6d PB |
3899 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3900 | return -EINVAL; | |
b9445598 | 3901 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 3902 | return -EINVAL; |
ec65fea5 | 3903 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 3904 | return -EBADF; |
03b1230c | 3905 | |
ec65fea5 PB |
3906 | /* open.how should be already initialised */ |
3907 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 3908 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 3909 | |
25e72d10 PB |
3910 | req->open.dfd = READ_ONCE(sqe->fd); |
3911 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 3912 | req->open.filename = getname(fname); |
15b71abe JA |
3913 | if (IS_ERR(req->open.filename)) { |
3914 | ret = PTR_ERR(req->open.filename); | |
3915 | req->open.filename = NULL; | |
3916 | return ret; | |
3917 | } | |
b9445598 PB |
3918 | |
3919 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
3920 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
3921 | return -EINVAL; | |
3922 | ||
4022e7af | 3923 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 3924 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 3925 | return 0; |
03b1230c JA |
3926 | } |
3927 | ||
ec65fea5 PB |
3928 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
3929 | { | |
d3fddf6d PB |
3930 | u64 mode = READ_ONCE(sqe->len); |
3931 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 3932 | |
ec65fea5 PB |
3933 | req->open.how = build_open_how(flags, mode); |
3934 | return __io_openat_prep(req, sqe); | |
3935 | } | |
3936 | ||
cebdb986 | 3937 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 3938 | { |
cebdb986 | 3939 | struct open_how __user *how; |
cebdb986 | 3940 | size_t len; |
0fa03c62 JA |
3941 | int ret; |
3942 | ||
cebdb986 JA |
3943 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
3944 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
3945 | if (len < OPEN_HOW_SIZE_VER0) |
3946 | return -EINVAL; | |
3529d8c2 | 3947 | |
cebdb986 JA |
3948 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
3949 | len); | |
3950 | if (ret) | |
3951 | return ret; | |
3529d8c2 | 3952 | |
ec65fea5 | 3953 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
3954 | } |
3955 | ||
45d189c6 | 3956 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
3957 | { |
3958 | struct open_flags op; | |
15b71abe | 3959 | struct file *file; |
b9445598 PB |
3960 | bool resolve_nonblock, nonblock_set; |
3961 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
3962 | int ret; |
3963 | ||
cebdb986 | 3964 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
3965 | if (ret) |
3966 | goto err; | |
3a81fd02 JA |
3967 | nonblock_set = op.open_flag & O_NONBLOCK; |
3968 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 3969 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
3970 | /* |
3971 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
3972 | * it'll always -EAGAIN | |
3973 | */ | |
3974 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
3975 | return -EAGAIN; | |
3976 | op.lookup_flags |= LOOKUP_CACHED; | |
3977 | op.open_flag |= O_NONBLOCK; | |
3978 | } | |
15b71abe | 3979 | |
b9445598 PB |
3980 | if (!fixed) { |
3981 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
3982 | if (ret < 0) | |
3983 | goto err; | |
3984 | } | |
15b71abe JA |
3985 | |
3986 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 3987 | if (IS_ERR(file)) { |
944d1444 | 3988 | /* |
12dcb58a PB |
3989 | * We could hang on to this 'fd' on retrying, but seems like |
3990 | * marginal gain for something that is now known to be a slower | |
3991 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 3992 | */ |
b9445598 PB |
3993 | if (!fixed) |
3994 | put_unused_fd(ret); | |
3a81fd02 | 3995 | |
15b71abe | 3996 | ret = PTR_ERR(file); |
12dcb58a PB |
3997 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
3998 | if (ret == -EAGAIN && | |
3999 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4000 | return -EAGAIN; | |
4001 | goto err; | |
15b71abe | 4002 | } |
12dcb58a PB |
4003 | |
4004 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4005 | file->f_flags &= ~O_NONBLOCK; | |
4006 | fsnotify_open(file); | |
b9445598 PB |
4007 | |
4008 | if (!fixed) | |
4009 | fd_install(ret, file); | |
4010 | else | |
4011 | ret = io_install_fixed_file(req, file, issue_flags, | |
4012 | req->open.file_slot - 1); | |
15b71abe JA |
4013 | err: |
4014 | putname(req->open.filename); | |
8fef80bf | 4015 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4016 | if (ret < 0) |
93d2bcd2 | 4017 | req_set_fail(req); |
0bdf3398 | 4018 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4019 | return 0; |
4020 | } | |
4021 | ||
45d189c6 | 4022 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4023 | { |
e45cff58 | 4024 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4025 | } |
4026 | ||
067524e9 JA |
4027 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4028 | const struct io_uring_sqe *sqe) | |
4029 | { | |
4030 | struct io_provide_buf *p = &req->pbuf; | |
4031 | u64 tmp; | |
4032 | ||
26578cda PB |
4033 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4034 | sqe->splice_fd_in) | |
067524e9 JA |
4035 | return -EINVAL; |
4036 | ||
4037 | tmp = READ_ONCE(sqe->fd); | |
4038 | if (!tmp || tmp > USHRT_MAX) | |
4039 | return -EINVAL; | |
4040 | ||
4041 | memset(p, 0, sizeof(*p)); | |
4042 | p->nbufs = tmp; | |
4043 | p->bgid = READ_ONCE(sqe->buf_group); | |
4044 | return 0; | |
4045 | } | |
4046 | ||
4047 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, | |
4048 | int bgid, unsigned nbufs) | |
4049 | { | |
4050 | unsigned i = 0; | |
4051 | ||
4052 | /* shouldn't happen */ | |
4053 | if (!nbufs) | |
4054 | return 0; | |
4055 | ||
4056 | /* the head kbuf is the list itself */ | |
4057 | while (!list_empty(&buf->list)) { | |
4058 | struct io_buffer *nxt; | |
4059 | ||
4060 | nxt = list_first_entry(&buf->list, struct io_buffer, list); | |
4061 | list_del(&nxt->list); | |
4062 | kfree(nxt); | |
4063 | if (++i == nbufs) | |
4064 | return i; | |
4065 | } | |
4066 | i++; | |
4067 | kfree(buf); | |
9e15c3a0 | 4068 | xa_erase(&ctx->io_buffers, bgid); |
067524e9 JA |
4069 | |
4070 | return i; | |
4071 | } | |
4072 | ||
889fca73 | 4073 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4074 | { |
4075 | struct io_provide_buf *p = &req->pbuf; | |
4076 | struct io_ring_ctx *ctx = req->ctx; | |
4077 | struct io_buffer *head; | |
4078 | int ret = 0; | |
45d189c6 | 4079 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
067524e9 JA |
4080 | |
4081 | io_ring_submit_lock(ctx, !force_nonblock); | |
4082 | ||
4083 | lockdep_assert_held(&ctx->uring_lock); | |
4084 | ||
4085 | ret = -ENOENT; | |
9e15c3a0 | 4086 | head = xa_load(&ctx->io_buffers, p->bgid); |
067524e9 JA |
4087 | if (head) |
4088 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); | |
067524e9 | 4089 | if (ret < 0) |
93d2bcd2 | 4090 | req_set_fail(req); |
067524e9 | 4091 | |
9fb8cb49 PB |
4092 | /* complete before unlock, IOPOLL may need the lock */ |
4093 | __io_req_complete(req, issue_flags, ret, 0); | |
4094 | io_ring_submit_unlock(ctx, !force_nonblock); | |
067524e9 JA |
4095 | return 0; |
4096 | } | |
4097 | ||
ddf0322d JA |
4098 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4099 | const struct io_uring_sqe *sqe) | |
4100 | { | |
38134ada | 4101 | unsigned long size, tmp_check; |
ddf0322d JA |
4102 | struct io_provide_buf *p = &req->pbuf; |
4103 | u64 tmp; | |
4104 | ||
26578cda | 4105 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4106 | return -EINVAL; |
4107 | ||
4108 | tmp = READ_ONCE(sqe->fd); | |
4109 | if (!tmp || tmp > USHRT_MAX) | |
4110 | return -E2BIG; | |
4111 | p->nbufs = tmp; | |
4112 | p->addr = READ_ONCE(sqe->addr); | |
4113 | p->len = READ_ONCE(sqe->len); | |
4114 | ||
38134ada PB |
4115 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4116 | &size)) | |
4117 | return -EOVERFLOW; | |
4118 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4119 | return -EOVERFLOW; | |
4120 | ||
d81269fe PB |
4121 | size = (unsigned long)p->len * p->nbufs; |
4122 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4123 | return -EFAULT; |
4124 | ||
4125 | p->bgid = READ_ONCE(sqe->buf_group); | |
4126 | tmp = READ_ONCE(sqe->off); | |
4127 | if (tmp > USHRT_MAX) | |
4128 | return -E2BIG; | |
4129 | p->bid = tmp; | |
4130 | return 0; | |
4131 | } | |
4132 | ||
4133 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) | |
4134 | { | |
4135 | struct io_buffer *buf; | |
4136 | u64 addr = pbuf->addr; | |
4137 | int i, bid = pbuf->bid; | |
4138 | ||
4139 | for (i = 0; i < pbuf->nbufs; i++) { | |
4140 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
4141 | if (!buf) | |
4142 | break; | |
4143 | ||
4144 | buf->addr = addr; | |
d1f82808 | 4145 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d JA |
4146 | buf->bid = bid; |
4147 | addr += pbuf->len; | |
4148 | bid++; | |
4149 | if (!*head) { | |
4150 | INIT_LIST_HEAD(&buf->list); | |
4151 | *head = buf; | |
4152 | } else { | |
4153 | list_add_tail(&buf->list, &(*head)->list); | |
4154 | } | |
4155 | } | |
4156 | ||
4157 | return i ? i : -ENOMEM; | |
4158 | } | |
4159 | ||
889fca73 | 4160 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4161 | { |
4162 | struct io_provide_buf *p = &req->pbuf; | |
4163 | struct io_ring_ctx *ctx = req->ctx; | |
4164 | struct io_buffer *head, *list; | |
4165 | int ret = 0; | |
45d189c6 | 4166 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ddf0322d JA |
4167 | |
4168 | io_ring_submit_lock(ctx, !force_nonblock); | |
4169 | ||
4170 | lockdep_assert_held(&ctx->uring_lock); | |
4171 | ||
9e15c3a0 | 4172 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
ddf0322d JA |
4173 | |
4174 | ret = io_add_buffers(p, &head); | |
9e15c3a0 JA |
4175 | if (ret >= 0 && !list) { |
4176 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); | |
4177 | if (ret < 0) | |
067524e9 | 4178 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
ddf0322d | 4179 | } |
ddf0322d | 4180 | if (ret < 0) |
93d2bcd2 | 4181 | req_set_fail(req); |
9fb8cb49 PB |
4182 | /* complete before unlock, IOPOLL may need the lock */ |
4183 | __io_req_complete(req, issue_flags, ret, 0); | |
4184 | io_ring_submit_unlock(ctx, !force_nonblock); | |
ddf0322d | 4185 | return 0; |
cebdb986 JA |
4186 | } |
4187 | ||
3e4827b0 JA |
4188 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4189 | const struct io_uring_sqe *sqe) | |
4190 | { | |
4191 | #if defined(CONFIG_EPOLL) | |
26578cda | 4192 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4193 | return -EINVAL; |
2d74d042 | 4194 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4195 | return -EINVAL; |
3e4827b0 JA |
4196 | |
4197 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4198 | req->epoll.op = READ_ONCE(sqe->len); | |
4199 | req->epoll.fd = READ_ONCE(sqe->off); | |
4200 | ||
4201 | if (ep_op_has_event(req->epoll.op)) { | |
4202 | struct epoll_event __user *ev; | |
4203 | ||
4204 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4205 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4206 | return -EFAULT; | |
4207 | } | |
4208 | ||
4209 | return 0; | |
4210 | #else | |
4211 | return -EOPNOTSUPP; | |
4212 | #endif | |
4213 | } | |
4214 | ||
889fca73 | 4215 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4216 | { |
4217 | #if defined(CONFIG_EPOLL) | |
4218 | struct io_epoll *ie = &req->epoll; | |
4219 | int ret; | |
45d189c6 | 4220 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4221 | |
4222 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4223 | if (force_nonblock && ret == -EAGAIN) | |
4224 | return -EAGAIN; | |
4225 | ||
4226 | if (ret < 0) | |
93d2bcd2 | 4227 | req_set_fail(req); |
889fca73 | 4228 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4229 | return 0; |
4230 | #else | |
4231 | return -EOPNOTSUPP; | |
4232 | #endif | |
4233 | } | |
4234 | ||
c1ca757b JA |
4235 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4236 | { | |
4237 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4238 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4239 | return -EINVAL; |
3232dd02 PB |
4240 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4241 | return -EINVAL; | |
c1ca757b JA |
4242 | |
4243 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4244 | req->madvise.len = READ_ONCE(sqe->len); | |
4245 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4246 | return 0; | |
4247 | #else | |
4248 | return -EOPNOTSUPP; | |
4249 | #endif | |
4250 | } | |
4251 | ||
45d189c6 | 4252 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4253 | { |
4254 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4255 | struct io_madvise *ma = &req->madvise; | |
4256 | int ret; | |
4257 | ||
45d189c6 | 4258 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4259 | return -EAGAIN; |
4260 | ||
0726b01e | 4261 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4262 | if (ret < 0) |
93d2bcd2 | 4263 | req_set_fail(req); |
e1e16097 | 4264 | io_req_complete(req, ret); |
c1ca757b JA |
4265 | return 0; |
4266 | #else | |
4267 | return -EOPNOTSUPP; | |
4268 | #endif | |
4269 | } | |
4270 | ||
4840e418 JA |
4271 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4272 | { | |
26578cda | 4273 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 4274 | return -EINVAL; |
3232dd02 PB |
4275 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4276 | return -EINVAL; | |
4840e418 JA |
4277 | |
4278 | req->fadvise.offset = READ_ONCE(sqe->off); | |
4279 | req->fadvise.len = READ_ONCE(sqe->len); | |
4280 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4281 | return 0; | |
4282 | } | |
4283 | ||
45d189c6 | 4284 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
4285 | { |
4286 | struct io_fadvise *fa = &req->fadvise; | |
4287 | int ret; | |
4288 | ||
45d189c6 | 4289 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
4290 | switch (fa->advice) { |
4291 | case POSIX_FADV_NORMAL: | |
4292 | case POSIX_FADV_RANDOM: | |
4293 | case POSIX_FADV_SEQUENTIAL: | |
4294 | break; | |
4295 | default: | |
4296 | return -EAGAIN; | |
4297 | } | |
4298 | } | |
4840e418 JA |
4299 | |
4300 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
4301 | if (ret < 0) | |
93d2bcd2 | 4302 | req_set_fail(req); |
0bdf3398 | 4303 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
4304 | return 0; |
4305 | } | |
4306 | ||
eddc7ef5 JA |
4307 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4308 | { | |
2d74d042 | 4309 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4310 | return -EINVAL; |
26578cda | 4311 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 4312 | return -EINVAL; |
9c280f90 | 4313 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4314 | return -EBADF; |
eddc7ef5 | 4315 | |
1d9e1288 BM |
4316 | req->statx.dfd = READ_ONCE(sqe->fd); |
4317 | req->statx.mask = READ_ONCE(sqe->len); | |
e62753e4 | 4318 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
4319 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4320 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 JA |
4321 | |
4322 | return 0; | |
4323 | } | |
4324 | ||
45d189c6 | 4325 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 4326 | { |
1d9e1288 | 4327 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
4328 | int ret; |
4329 | ||
59d70013 | 4330 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
4331 | return -EAGAIN; |
4332 | ||
e62753e4 BM |
4333 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
4334 | ctx->buffer); | |
eddc7ef5 | 4335 | |
eddc7ef5 | 4336 | if (ret < 0) |
93d2bcd2 | 4337 | req_set_fail(req); |
e1e16097 | 4338 | io_req_complete(req, ret); |
eddc7ef5 JA |
4339 | return 0; |
4340 | } | |
4341 | ||
b5dba59e JA |
4342 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4343 | { | |
14587a46 | 4344 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4345 | return -EINVAL; |
b5dba59e | 4346 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
26578cda | 4347 | sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
b5dba59e | 4348 | return -EINVAL; |
9c280f90 | 4349 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 4350 | return -EBADF; |
b5dba59e JA |
4351 | |
4352 | req->close.fd = READ_ONCE(sqe->fd); | |
b5dba59e | 4353 | return 0; |
b5dba59e JA |
4354 | } |
4355 | ||
889fca73 | 4356 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 4357 | { |
9eac1904 | 4358 | struct files_struct *files = current->files; |
3af73b28 | 4359 | struct io_close *close = &req->close; |
9eac1904 | 4360 | struct fdtable *fdt; |
a1fde923 PB |
4361 | struct file *file = NULL; |
4362 | int ret = -EBADF; | |
b5dba59e | 4363 | |
9eac1904 JA |
4364 | spin_lock(&files->file_lock); |
4365 | fdt = files_fdtable(files); | |
4366 | if (close->fd >= fdt->max_fds) { | |
4367 | spin_unlock(&files->file_lock); | |
4368 | goto err; | |
4369 | } | |
4370 | file = fdt->fd[close->fd]; | |
a1fde923 | 4371 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
4372 | spin_unlock(&files->file_lock); |
4373 | file = NULL; | |
4374 | goto err; | |
3af73b28 | 4375 | } |
b5dba59e JA |
4376 | |
4377 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 4378 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 4379 | spin_unlock(&files->file_lock); |
0bf0eefd | 4380 | return -EAGAIN; |
a2100672 | 4381 | } |
b5dba59e | 4382 | |
9eac1904 JA |
4383 | ret = __close_fd_get_file(close->fd, &file); |
4384 | spin_unlock(&files->file_lock); | |
4385 | if (ret < 0) { | |
4386 | if (ret == -ENOENT) | |
4387 | ret = -EBADF; | |
4388 | goto err; | |
4389 | } | |
4390 | ||
3af73b28 | 4391 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
4392 | ret = filp_close(file, current->files); |
4393 | err: | |
3af73b28 | 4394 | if (ret < 0) |
93d2bcd2 | 4395 | req_set_fail(req); |
9eac1904 JA |
4396 | if (file) |
4397 | fput(file); | |
889fca73 | 4398 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 4399 | return 0; |
b5dba59e JA |
4400 | } |
4401 | ||
1155c76a | 4402 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
4403 | { |
4404 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 4405 | |
5d17b4a4 JA |
4406 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4407 | return -EINVAL; | |
26578cda PB |
4408 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4409 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
4410 | return -EINVAL; |
4411 | ||
8ed8d3c3 JA |
4412 | req->sync.off = READ_ONCE(sqe->off); |
4413 | req->sync.len = READ_ONCE(sqe->len); | |
4414 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
4415 | return 0; |
4416 | } | |
4417 | ||
45d189c6 | 4418 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4419 | { |
8ed8d3c3 JA |
4420 | int ret; |
4421 | ||
ac45abc0 | 4422 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 4423 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4424 | return -EAGAIN; |
4425 | ||
9adbd45d | 4426 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
4427 | req->sync.flags); |
4428 | if (ret < 0) | |
93d2bcd2 | 4429 | req_set_fail(req); |
e1e16097 | 4430 | io_req_complete(req, ret); |
5d17b4a4 JA |
4431 | return 0; |
4432 | } | |
4433 | ||
469956e8 | 4434 | #if defined(CONFIG_NET) |
02d27d89 PB |
4435 | static int io_setup_async_msg(struct io_kiocb *req, |
4436 | struct io_async_msghdr *kmsg) | |
4437 | { | |
e8c2bc1f JA |
4438 | struct io_async_msghdr *async_msg = req->async_data; |
4439 | ||
4440 | if (async_msg) | |
02d27d89 | 4441 | return -EAGAIN; |
e8c2bc1f | 4442 | if (io_alloc_async_data(req)) { |
257e84a5 | 4443 | kfree(kmsg->free_iov); |
02d27d89 PB |
4444 | return -ENOMEM; |
4445 | } | |
e8c2bc1f | 4446 | async_msg = req->async_data; |
02d27d89 | 4447 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 4448 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 4449 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
4450 | /* if were using fast_iov, set it to the new one */ |
4451 | if (!async_msg->free_iov) | |
4452 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
4453 | ||
02d27d89 PB |
4454 | return -EAGAIN; |
4455 | } | |
4456 | ||
2ae523ed PB |
4457 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
4458 | struct io_async_msghdr *iomsg) | |
4459 | { | |
2ae523ed | 4460 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 4461 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 4462 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 4463 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
4464 | } |
4465 | ||
93642ef8 PB |
4466 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
4467 | { | |
4468 | int ret; | |
4469 | ||
93642ef8 PB |
4470 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
4471 | if (!ret) | |
4472 | req->flags |= REQ_F_NEED_CLEANUP; | |
4473 | return ret; | |
4474 | } | |
4475 | ||
3529d8c2 | 4476 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 4477 | { |
e47293fd | 4478 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 4479 | |
d2b6f48b PB |
4480 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4481 | return -EINVAL; | |
4482 | ||
270a5940 | 4483 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 4484 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
4485 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4486 | if (sr->msg_flags & MSG_DONTWAIT) | |
4487 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 4488 | |
d8768362 JA |
4489 | #ifdef CONFIG_COMPAT |
4490 | if (req->ctx->compat) | |
4491 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4492 | #endif | |
93642ef8 | 4493 | return 0; |
03b1230c JA |
4494 | } |
4495 | ||
889fca73 | 4496 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4497 | { |
6b754c8b | 4498 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 4499 | struct socket *sock; |
7a7cacba | 4500 | unsigned flags; |
0031275d | 4501 | int min_ret = 0; |
0fa03c62 JA |
4502 | int ret; |
4503 | ||
dba4a925 | 4504 | sock = sock_from_file(req->file); |
7a7cacba | 4505 | if (unlikely(!sock)) |
dba4a925 | 4506 | return -ENOTSOCK; |
3529d8c2 | 4507 | |
257e84a5 PB |
4508 | kmsg = req->async_data; |
4509 | if (!kmsg) { | |
7a7cacba PB |
4510 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
4511 | if (ret) | |
4512 | return ret; | |
4513 | kmsg = &iomsg; | |
0fa03c62 | 4514 | } |
0fa03c62 | 4515 | |
04411806 PB |
4516 | flags = req->sr_msg.msg_flags; |
4517 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4518 | flags |= MSG_DONTWAIT; |
0031275d SM |
4519 | if (flags & MSG_WAITALL) |
4520 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4521 | ||
7a7cacba | 4522 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
45d189c6 | 4523 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4524 | return io_setup_async_msg(req, kmsg); |
4525 | if (ret == -ERESTARTSYS) | |
4526 | ret = -EINTR; | |
0fa03c62 | 4527 | |
257e84a5 PB |
4528 | /* fast path, check for non-NULL to avoid function call */ |
4529 | if (kmsg->free_iov) | |
4530 | kfree(kmsg->free_iov); | |
99bc4c38 | 4531 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4532 | if (ret < min_ret) |
93d2bcd2 | 4533 | req_set_fail(req); |
889fca73 | 4534 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 4535 | return 0; |
03b1230c | 4536 | } |
aa1fa28f | 4537 | |
889fca73 | 4538 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4539 | { |
7a7cacba PB |
4540 | struct io_sr_msg *sr = &req->sr_msg; |
4541 | struct msghdr msg; | |
4542 | struct iovec iov; | |
fddaface | 4543 | struct socket *sock; |
7a7cacba | 4544 | unsigned flags; |
0031275d | 4545 | int min_ret = 0; |
fddaface JA |
4546 | int ret; |
4547 | ||
dba4a925 | 4548 | sock = sock_from_file(req->file); |
7a7cacba | 4549 | if (unlikely(!sock)) |
dba4a925 | 4550 | return -ENOTSOCK; |
fddaface | 4551 | |
7a7cacba PB |
4552 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
4553 | if (unlikely(ret)) | |
14db8411 | 4554 | return ret; |
fddaface | 4555 | |
7a7cacba PB |
4556 | msg.msg_name = NULL; |
4557 | msg.msg_control = NULL; | |
4558 | msg.msg_controllen = 0; | |
4559 | msg.msg_namelen = 0; | |
fddaface | 4560 | |
04411806 PB |
4561 | flags = req->sr_msg.msg_flags; |
4562 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 4563 | flags |= MSG_DONTWAIT; |
0031275d SM |
4564 | if (flags & MSG_WAITALL) |
4565 | min_ret = iov_iter_count(&msg.msg_iter); | |
4566 | ||
7a7cacba PB |
4567 | msg.msg_flags = flags; |
4568 | ret = sock_sendmsg(sock, &msg); | |
45d189c6 | 4569 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
7a7cacba PB |
4570 | return -EAGAIN; |
4571 | if (ret == -ERESTARTSYS) | |
4572 | ret = -EINTR; | |
fddaface | 4573 | |
0031275d | 4574 | if (ret < min_ret) |
93d2bcd2 | 4575 | req_set_fail(req); |
889fca73 | 4576 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 4577 | return 0; |
fddaface JA |
4578 | } |
4579 | ||
1400e697 PB |
4580 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
4581 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
4582 | { |
4583 | struct io_sr_msg *sr = &req->sr_msg; | |
4584 | struct iovec __user *uiov; | |
4585 | size_t iov_len; | |
4586 | int ret; | |
4587 | ||
1400e697 PB |
4588 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
4589 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
4590 | if (ret) |
4591 | return ret; | |
4592 | ||
4593 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4594 | if (iov_len > 1) | |
4595 | return -EINVAL; | |
5476dfed | 4596 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 4597 | return -EFAULT; |
5476dfed | 4598 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 4599 | iomsg->free_iov = NULL; |
52de1fe1 | 4600 | } else { |
257e84a5 | 4601 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4602 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 4603 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 4604 | false); |
52de1fe1 JA |
4605 | if (ret > 0) |
4606 | ret = 0; | |
4607 | } | |
4608 | ||
4609 | return ret; | |
4610 | } | |
4611 | ||
4612 | #ifdef CONFIG_COMPAT | |
4613 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 4614 | struct io_async_msghdr *iomsg) |
52de1fe1 | 4615 | { |
52de1fe1 JA |
4616 | struct io_sr_msg *sr = &req->sr_msg; |
4617 | struct compat_iovec __user *uiov; | |
4618 | compat_uptr_t ptr; | |
4619 | compat_size_t len; | |
4620 | int ret; | |
4621 | ||
4af3417a PB |
4622 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
4623 | &ptr, &len); | |
52de1fe1 JA |
4624 | if (ret) |
4625 | return ret; | |
4626 | ||
4627 | uiov = compat_ptr(ptr); | |
4628 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
4629 | compat_ssize_t clen; | |
4630 | ||
4631 | if (len > 1) | |
4632 | return -EINVAL; | |
4633 | if (!access_ok(uiov, sizeof(*uiov))) | |
4634 | return -EFAULT; | |
4635 | if (__get_user(clen, &uiov->iov_len)) | |
4636 | return -EFAULT; | |
4637 | if (clen < 0) | |
4638 | return -EINVAL; | |
2d280bc8 | 4639 | sr->len = clen; |
257e84a5 | 4640 | iomsg->free_iov = NULL; |
52de1fe1 | 4641 | } else { |
257e84a5 | 4642 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 4643 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 4644 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 4645 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
4646 | if (ret < 0) |
4647 | return ret; | |
4648 | } | |
4649 | ||
4650 | return 0; | |
4651 | } | |
4652 | #endif | |
4653 | ||
1400e697 PB |
4654 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
4655 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 4656 | { |
1400e697 | 4657 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
4658 | |
4659 | #ifdef CONFIG_COMPAT | |
4660 | if (req->ctx->compat) | |
1400e697 | 4661 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 4662 | #endif |
52de1fe1 | 4663 | |
1400e697 | 4664 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
4665 | } |
4666 | ||
bcda7baa | 4667 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
7fbb1b54 | 4668 | bool needs_lock) |
bcda7baa JA |
4669 | { |
4670 | struct io_sr_msg *sr = &req->sr_msg; | |
4671 | struct io_buffer *kbuf; | |
4672 | ||
bcda7baa JA |
4673 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
4674 | if (IS_ERR(kbuf)) | |
4675 | return kbuf; | |
4676 | ||
4677 | sr->kbuf = kbuf; | |
4678 | req->flags |= REQ_F_BUFFER_SELECTED; | |
bcda7baa | 4679 | return kbuf; |
fddaface JA |
4680 | } |
4681 | ||
7fbb1b54 PB |
4682 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
4683 | { | |
4684 | return io_put_kbuf(req, req->sr_msg.kbuf); | |
4685 | } | |
4686 | ||
93642ef8 | 4687 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 4688 | { |
99bc4c38 | 4689 | int ret; |
3529d8c2 | 4690 | |
93642ef8 PB |
4691 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
4692 | if (!ret) | |
4693 | req->flags |= REQ_F_NEED_CLEANUP; | |
4694 | return ret; | |
4695 | } | |
4696 | ||
4697 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4698 | { | |
4699 | struct io_sr_msg *sr = &req->sr_msg; | |
4700 | ||
d2b6f48b PB |
4701 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4702 | return -EINVAL; | |
4703 | ||
270a5940 | 4704 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 4705 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 4706 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
4707 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
4708 | if (sr->msg_flags & MSG_DONTWAIT) | |
4709 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 4710 | |
d8768362 JA |
4711 | #ifdef CONFIG_COMPAT |
4712 | if (req->ctx->compat) | |
4713 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
4714 | #endif | |
93642ef8 | 4715 | return 0; |
aa1fa28f JA |
4716 | } |
4717 | ||
889fca73 | 4718 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 4719 | { |
6b754c8b | 4720 | struct io_async_msghdr iomsg, *kmsg; |
03b1230c | 4721 | struct socket *sock; |
7fbb1b54 | 4722 | struct io_buffer *kbuf; |
7a7cacba | 4723 | unsigned flags; |
0031275d | 4724 | int min_ret = 0; |
52de1fe1 | 4725 | int ret, cflags = 0; |
45d189c6 | 4726 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 4727 | |
dba4a925 | 4728 | sock = sock_from_file(req->file); |
7a7cacba | 4729 | if (unlikely(!sock)) |
dba4a925 | 4730 | return -ENOTSOCK; |
3529d8c2 | 4731 | |
257e84a5 PB |
4732 | kmsg = req->async_data; |
4733 | if (!kmsg) { | |
7a7cacba PB |
4734 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
4735 | if (ret) | |
681fda8d | 4736 | return ret; |
7a7cacba PB |
4737 | kmsg = &iomsg; |
4738 | } | |
03b1230c | 4739 | |
bc02ef33 | 4740 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4741 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bc02ef33 | 4742 | if (IS_ERR(kbuf)) |
52de1fe1 | 4743 | return PTR_ERR(kbuf); |
7a7cacba | 4744 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
4745 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
4746 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
4747 | 1, req->sr_msg.len); |
4748 | } | |
52de1fe1 | 4749 | |
04411806 PB |
4750 | flags = req->sr_msg.msg_flags; |
4751 | if (force_nonblock) | |
7a7cacba | 4752 | flags |= MSG_DONTWAIT; |
0031275d SM |
4753 | if (flags & MSG_WAITALL) |
4754 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
4755 | ||
7a7cacba PB |
4756 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
4757 | kmsg->uaddr, flags); | |
0e1b6fe3 PB |
4758 | if (force_nonblock && ret == -EAGAIN) |
4759 | return io_setup_async_msg(req, kmsg); | |
7a7cacba PB |
4760 | if (ret == -ERESTARTSYS) |
4761 | ret = -EINTR; | |
03b1230c | 4762 | |
7fbb1b54 PB |
4763 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4764 | cflags = io_put_recv_kbuf(req); | |
257e84a5 PB |
4765 | /* fast path, check for non-NULL to avoid function call */ |
4766 | if (kmsg->free_iov) | |
4767 | kfree(kmsg->free_iov); | |
99bc4c38 | 4768 | req->flags &= ~REQ_F_NEED_CLEANUP; |
0031275d | 4769 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4770 | req_set_fail(req); |
889fca73 | 4771 | __io_req_complete(req, issue_flags, ret, cflags); |
03b1230c | 4772 | return 0; |
0fa03c62 | 4773 | } |
5d17b4a4 | 4774 | |
889fca73 | 4775 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 4776 | { |
6b754c8b | 4777 | struct io_buffer *kbuf; |
7a7cacba PB |
4778 | struct io_sr_msg *sr = &req->sr_msg; |
4779 | struct msghdr msg; | |
4780 | void __user *buf = sr->buf; | |
fddaface | 4781 | struct socket *sock; |
7a7cacba PB |
4782 | struct iovec iov; |
4783 | unsigned flags; | |
0031275d | 4784 | int min_ret = 0; |
bcda7baa | 4785 | int ret, cflags = 0; |
45d189c6 | 4786 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 4787 | |
dba4a925 | 4788 | sock = sock_from_file(req->file); |
7a7cacba | 4789 | if (unlikely(!sock)) |
dba4a925 | 4790 | return -ENOTSOCK; |
fddaface | 4791 | |
bc02ef33 | 4792 | if (req->flags & REQ_F_BUFFER_SELECT) { |
7fbb1b54 | 4793 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
bcda7baa JA |
4794 | if (IS_ERR(kbuf)) |
4795 | return PTR_ERR(kbuf); | |
7a7cacba | 4796 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 4797 | } |
bcda7baa | 4798 | |
7a7cacba | 4799 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
4800 | if (unlikely(ret)) |
4801 | goto out_free; | |
fddaface | 4802 | |
7a7cacba PB |
4803 | msg.msg_name = NULL; |
4804 | msg.msg_control = NULL; | |
4805 | msg.msg_controllen = 0; | |
4806 | msg.msg_namelen = 0; | |
4807 | msg.msg_iocb = NULL; | |
4808 | msg.msg_flags = 0; | |
fddaface | 4809 | |
04411806 PB |
4810 | flags = req->sr_msg.msg_flags; |
4811 | if (force_nonblock) | |
7a7cacba | 4812 | flags |= MSG_DONTWAIT; |
0031275d SM |
4813 | if (flags & MSG_WAITALL) |
4814 | min_ret = iov_iter_count(&msg.msg_iter); | |
4815 | ||
7a7cacba PB |
4816 | ret = sock_recvmsg(sock, &msg, flags); |
4817 | if (force_nonblock && ret == -EAGAIN) | |
4818 | return -EAGAIN; | |
4819 | if (ret == -ERESTARTSYS) | |
4820 | ret = -EINTR; | |
14c32eee | 4821 | out_free: |
7fbb1b54 PB |
4822 | if (req->flags & REQ_F_BUFFER_SELECTED) |
4823 | cflags = io_put_recv_kbuf(req); | |
0031275d | 4824 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
93d2bcd2 | 4825 | req_set_fail(req); |
889fca73 | 4826 | __io_req_complete(req, issue_flags, ret, cflags); |
fddaface | 4827 | return 0; |
fddaface JA |
4828 | } |
4829 | ||
3529d8c2 | 4830 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 4831 | { |
8ed8d3c3 JA |
4832 | struct io_accept *accept = &req->accept; |
4833 | ||
14587a46 | 4834 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 4835 | return -EINVAL; |
aaa4db12 | 4836 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
4837 | return -EINVAL; |
4838 | ||
d55e5f5b JA |
4839 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4840 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 4841 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 4842 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 4843 | |
aaa4db12 PB |
4844 | accept->file_slot = READ_ONCE(sqe->file_index); |
4845 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || | |
4846 | (accept->flags & SOCK_CLOEXEC))) | |
4847 | return -EINVAL; | |
a7083ad5 PB |
4848 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
4849 | return -EINVAL; | |
4850 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
4851 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 4852 | return 0; |
8ed8d3c3 | 4853 | } |
17f2fe35 | 4854 | |
889fca73 | 4855 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
4856 | { |
4857 | struct io_accept *accept = &req->accept; | |
45d189c6 | 4858 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 4859 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 4860 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
4861 | struct file *file; |
4862 | int ret, fd; | |
8ed8d3c3 | 4863 | |
e697deed JX |
4864 | if (req->file->f_flags & O_NONBLOCK) |
4865 | req->flags |= REQ_F_NOWAIT; | |
4866 | ||
aaa4db12 PB |
4867 | if (!fixed) { |
4868 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
4869 | if (unlikely(fd < 0)) | |
4870 | return fd; | |
4871 | } | |
a7083ad5 PB |
4872 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
4873 | accept->flags); | |
4874 | if (IS_ERR(file)) { | |
aaa4db12 PB |
4875 | if (!fixed) |
4876 | put_unused_fd(fd); | |
a7083ad5 PB |
4877 | ret = PTR_ERR(file); |
4878 | if (ret == -EAGAIN && force_nonblock) | |
4879 | return -EAGAIN; | |
ac45abc0 PB |
4880 | if (ret == -ERESTARTSYS) |
4881 | ret = -EINTR; | |
93d2bcd2 | 4882 | req_set_fail(req); |
aaa4db12 | 4883 | } else if (!fixed) { |
a7083ad5 PB |
4884 | fd_install(fd, file); |
4885 | ret = fd; | |
aaa4db12 PB |
4886 | } else { |
4887 | ret = io_install_fixed_file(req, file, issue_flags, | |
4888 | accept->file_slot - 1); | |
ac45abc0 | 4889 | } |
889fca73 | 4890 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 4891 | return 0; |
8ed8d3c3 JA |
4892 | } |
4893 | ||
93642ef8 PB |
4894 | static int io_connect_prep_async(struct io_kiocb *req) |
4895 | { | |
4896 | struct io_async_connect *io = req->async_data; | |
4897 | struct io_connect *conn = &req->connect; | |
4898 | ||
4899 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
4900 | } | |
4901 | ||
3529d8c2 | 4902 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 4903 | { |
3529d8c2 | 4904 | struct io_connect *conn = &req->connect; |
f499a021 | 4905 | |
14587a46 | 4906 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 4907 | return -EINVAL; |
26578cda PB |
4908 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
4909 | sqe->splice_fd_in) | |
3fbb51c1 JA |
4910 | return -EINVAL; |
4911 | ||
3529d8c2 JA |
4912 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
4913 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 4914 | return 0; |
f499a021 JA |
4915 | } |
4916 | ||
889fca73 | 4917 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 4918 | { |
e8c2bc1f | 4919 | struct io_async_connect __io, *io; |
f8e85cf2 | 4920 | unsigned file_flags; |
3fbb51c1 | 4921 | int ret; |
45d189c6 | 4922 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 4923 | |
e8c2bc1f JA |
4924 | if (req->async_data) { |
4925 | io = req->async_data; | |
f499a021 | 4926 | } else { |
3529d8c2 JA |
4927 | ret = move_addr_to_kernel(req->connect.addr, |
4928 | req->connect.addr_len, | |
e8c2bc1f | 4929 | &__io.address); |
f499a021 JA |
4930 | if (ret) |
4931 | goto out; | |
4932 | io = &__io; | |
4933 | } | |
4934 | ||
3fbb51c1 JA |
4935 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
4936 | ||
e8c2bc1f | 4937 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 4938 | req->connect.addr_len, file_flags); |
87f80d62 | 4939 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
e8c2bc1f | 4940 | if (req->async_data) |
b7bb4f7d | 4941 | return -EAGAIN; |
e8c2bc1f | 4942 | if (io_alloc_async_data(req)) { |
f499a021 JA |
4943 | ret = -ENOMEM; |
4944 | goto out; | |
4945 | } | |
e8c2bc1f | 4946 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 4947 | return -EAGAIN; |
f499a021 | 4948 | } |
f8e85cf2 JA |
4949 | if (ret == -ERESTARTSYS) |
4950 | ret = -EINTR; | |
f499a021 | 4951 | out: |
4e88d6e7 | 4952 | if (ret < 0) |
93d2bcd2 | 4953 | req_set_fail(req); |
889fca73 | 4954 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 4955 | return 0; |
469956e8 Y |
4956 | } |
4957 | #else /* !CONFIG_NET */ | |
99a10081 JA |
4958 | #define IO_NETOP_FN(op) \ |
4959 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
4960 | { \ | |
4961 | return -EOPNOTSUPP; \ | |
4962 | } | |
4963 | ||
4964 | #define IO_NETOP_PREP(op) \ | |
4965 | IO_NETOP_FN(op) \ | |
4966 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
4967 | { \ | |
4968 | return -EOPNOTSUPP; \ | |
4969 | } \ | |
4970 | ||
4971 | #define IO_NETOP_PREP_ASYNC(op) \ | |
4972 | IO_NETOP_PREP(op) \ | |
4973 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
4974 | { \ | |
4975 | return -EOPNOTSUPP; \ | |
4976 | } | |
4977 | ||
4978 | IO_NETOP_PREP_ASYNC(sendmsg); | |
4979 | IO_NETOP_PREP_ASYNC(recvmsg); | |
4980 | IO_NETOP_PREP_ASYNC(connect); | |
4981 | IO_NETOP_PREP(accept); | |
4982 | IO_NETOP_FN(send); | |
4983 | IO_NETOP_FN(recv); | |
469956e8 | 4984 | #endif /* CONFIG_NET */ |
f8e85cf2 | 4985 | |
d7718a9d JA |
4986 | struct io_poll_table { |
4987 | struct poll_table_struct pt; | |
4988 | struct io_kiocb *req; | |
68b11e8b | 4989 | int nr_entries; |
d7718a9d JA |
4990 | int error; |
4991 | }; | |
ce593a6c | 4992 | |
d7718a9d | 4993 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
5b0a6acc | 4994 | __poll_t mask, io_req_tw_func_t func) |
d7718a9d | 4995 | { |
d7718a9d JA |
4996 | /* for instances that support it check for an event match first: */ |
4997 | if (mask && !(mask & poll->events)) | |
4998 | return 0; | |
4999 | ||
5000 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); | |
5001 | ||
5002 | list_del_init(&poll->wait.entry); | |
5003 | ||
d7718a9d | 5004 | req->result = mask; |
5b0a6acc | 5005 | req->io_task_work.func = func; |
6d816e08 | 5006 | |
d7718a9d | 5007 | /* |
e3aabf95 JA |
5008 | * If this fails, then the task is exiting. When a task exits, the |
5009 | * work gets canceled, so just cancel this request as well instead | |
5010 | * of executing it. We can't safely execute it anyway, as we may not | |
5011 | * have the needed state needed for it anyway. | |
d7718a9d | 5012 | */ |
e09ee510 | 5013 | io_req_task_work_add(req); |
d7718a9d JA |
5014 | return 1; |
5015 | } | |
5016 | ||
74ce6ce4 JA |
5017 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
5018 | __acquires(&req->ctx->completion_lock) | |
5019 | { | |
5020 | struct io_ring_ctx *ctx = req->ctx; | |
5021 | ||
316319e8 | 5022 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 PB |
5023 | if (unlikely(req->task->flags & PF_EXITING)) |
5024 | WRITE_ONCE(poll->canceled, true); | |
5025 | ||
74ce6ce4 JA |
5026 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5027 | struct poll_table_struct pt = { ._key = poll->events }; | |
5028 | ||
5029 | req->result = vfs_poll(req->file, &pt) & poll->events; | |
5030 | } | |
5031 | ||
79ebeaee | 5032 | spin_lock(&ctx->completion_lock); |
74ce6ce4 JA |
5033 | if (!req->result && !READ_ONCE(poll->canceled)) { |
5034 | add_wait_queue(poll->head, &poll->wait); | |
5035 | return true; | |
5036 | } | |
5037 | ||
5038 | return false; | |
5039 | } | |
5040 | ||
d4e7cd36 | 5041 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5042 | { |
e8c2bc1f | 5043 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5044 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5045 | return req->async_data; |
d4e7cd36 JA |
5046 | return req->apoll->double_poll; |
5047 | } | |
5048 | ||
5049 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5050 | { | |
5051 | if (req->opcode == IORING_OP_POLL_ADD) | |
5052 | return &req->poll; | |
5053 | return &req->apoll->poll; | |
5054 | } | |
5055 | ||
5056 | static void io_poll_remove_double(struct io_kiocb *req) | |
e07785b0 | 5057 | __must_hold(&req->ctx->completion_lock) |
d4e7cd36 JA |
5058 | { |
5059 | struct io_poll_iocb *poll = io_poll_get_double(req); | |
18bceab1 JA |
5060 | |
5061 | lockdep_assert_held(&req->ctx->completion_lock); | |
5062 | ||
5063 | if (poll && poll->head) { | |
5064 | struct wait_queue_head *head = poll->head; | |
5065 | ||
79ebeaee | 5066 | spin_lock_irq(&head->lock); |
18bceab1 JA |
5067 | list_del_init(&poll->wait.entry); |
5068 | if (poll->wait.private) | |
de9b4cca | 5069 | req_ref_put(req); |
18bceab1 | 5070 | poll->head = NULL; |
79ebeaee | 5071 | spin_unlock_irq(&head->lock); |
18bceab1 JA |
5072 | } |
5073 | } | |
5074 | ||
e27414be | 5075 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
e07785b0 | 5076 | __must_hold(&req->ctx->completion_lock) |
18bceab1 JA |
5077 | { |
5078 | struct io_ring_ctx *ctx = req->ctx; | |
88e41cf9 | 5079 | unsigned flags = IORING_CQE_F_MORE; |
e27414be | 5080 | int error; |
18bceab1 | 5081 | |
e27414be | 5082 | if (READ_ONCE(req->poll.canceled)) { |
45ab03b1 | 5083 | error = -ECANCELED; |
88e41cf9 | 5084 | req->poll.events |= EPOLLONESHOT; |
e27414be | 5085 | } else { |
5082620f | 5086 | error = mangle_poll(mask); |
e27414be | 5087 | } |
b69de288 JA |
5088 | if (req->poll.events & EPOLLONESHOT) |
5089 | flags = 0; | |
d4d19c19 | 5090 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
88e41cf9 JA |
5091 | req->poll.done = true; |
5092 | flags = 0; | |
5093 | } | |
7b289c38 HX |
5094 | if (flags & IORING_CQE_F_MORE) |
5095 | ctx->cq_extra++; | |
18bceab1 | 5096 | |
18bceab1 | 5097 | io_commit_cqring(ctx); |
88e41cf9 | 5098 | return !(flags & IORING_CQE_F_MORE); |
18bceab1 JA |
5099 | } |
5100 | ||
f237c30a | 5101 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
5102 | { |
5103 | struct io_ring_ctx *ctx = req->ctx; | |
dd221f46 | 5104 | struct io_kiocb *nxt; |
18bceab1 JA |
5105 | |
5106 | if (io_poll_rewait(req, &req->poll)) { | |
79ebeaee | 5107 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5108 | } else { |
f40b964a | 5109 | bool done; |
18bceab1 | 5110 | |
e27414be | 5111 | done = io_poll_complete(req, req->result); |
88e41cf9 | 5112 | if (done) { |
a890d01e | 5113 | io_poll_remove_double(req); |
88e41cf9 | 5114 | hash_del(&req->hash_node); |
f40b964a | 5115 | } else { |
88e41cf9 JA |
5116 | req->result = 0; |
5117 | add_wait_queue(req->poll.head, &req->poll.wait); | |
5118 | } | |
79ebeaee | 5119 | spin_unlock(&ctx->completion_lock); |
dd221f46 | 5120 | io_cqring_ev_posted(ctx); |
18bceab1 | 5121 | |
88e41cf9 JA |
5122 | if (done) { |
5123 | nxt = io_put_req_find_next(req); | |
5124 | if (nxt) | |
f237c30a | 5125 | io_req_task_submit(nxt, locked); |
88e41cf9 | 5126 | } |
dd221f46 | 5127 | } |
18bceab1 JA |
5128 | } |
5129 | ||
5130 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, | |
5131 | int sync, void *key) | |
5132 | { | |
5133 | struct io_kiocb *req = wait->private; | |
d4e7cd36 | 5134 | struct io_poll_iocb *poll = io_poll_get_single(req); |
18bceab1 | 5135 | __poll_t mask = key_to_poll(key); |
79ebeaee | 5136 | unsigned long flags; |
18bceab1 JA |
5137 | |
5138 | /* for instances that support it check for an event match first: */ | |
5139 | if (mask && !(mask & poll->events)) | |
5140 | return 0; | |
88e41cf9 JA |
5141 | if (!(poll->events & EPOLLONESHOT)) |
5142 | return poll->wait.func(&poll->wait, mode, sync, key); | |
18bceab1 | 5143 | |
8706e04e JA |
5144 | list_del_init(&wait->entry); |
5145 | ||
9ce85ef2 | 5146 | if (poll->head) { |
18bceab1 JA |
5147 | bool done; |
5148 | ||
79ebeaee | 5149 | spin_lock_irqsave(&poll->head->lock, flags); |
807abcb0 | 5150 | done = list_empty(&poll->wait.entry); |
18bceab1 | 5151 | if (!done) |
807abcb0 | 5152 | list_del_init(&poll->wait.entry); |
d4e7cd36 JA |
5153 | /* make sure double remove sees this as being gone */ |
5154 | wait->private = NULL; | |
79ebeaee | 5155 | spin_unlock_irqrestore(&poll->head->lock, flags); |
c8b5e260 JA |
5156 | if (!done) { |
5157 | /* use wait func handler, so it matches the rq type */ | |
5158 | poll->wait.func(&poll->wait, mode, sync, key); | |
5159 | } | |
18bceab1 | 5160 | } |
de9b4cca | 5161 | req_ref_put(req); |
18bceab1 JA |
5162 | return 1; |
5163 | } | |
5164 | ||
5165 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, | |
5166 | wait_queue_func_t wake_func) | |
5167 | { | |
5168 | poll->head = NULL; | |
5169 | poll->done = false; | |
5170 | poll->canceled = false; | |
464dca61 JA |
5171 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5172 | /* mask in events that we always want/need */ | |
5173 | poll->events = events | IO_POLL_UNMASK; | |
18bceab1 JA |
5174 | INIT_LIST_HEAD(&poll->wait.entry); |
5175 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
5176 | } | |
5177 | ||
5178 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
5179 | struct wait_queue_head *head, |
5180 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
5181 | { |
5182 | struct io_kiocb *req = pt->req; | |
5183 | ||
5184 | /* | |
68b11e8b PB |
5185 | * The file being polled uses multiple waitqueues for poll handling |
5186 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
5187 | * if this happens. | |
18bceab1 | 5188 | */ |
68b11e8b | 5189 | if (unlikely(pt->nr_entries)) { |
58852d4d PB |
5190 | struct io_poll_iocb *poll_one = poll; |
5191 | ||
23a65db8 PB |
5192 | /* double add on the same waitqueue head, ignore */ |
5193 | if (poll_one->head == head) | |
5194 | return; | |
18bceab1 | 5195 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 5196 | if (*poll_ptr) { |
23a65db8 PB |
5197 | if ((*poll_ptr)->head == head) |
5198 | return; | |
18bceab1 JA |
5199 | pt->error = -EINVAL; |
5200 | return; | |
5201 | } | |
ea6a693d JA |
5202 | /* |
5203 | * Can't handle multishot for double wait for now, turn it | |
5204 | * into one-shot mode. | |
5205 | */ | |
7a274727 PB |
5206 | if (!(poll_one->events & EPOLLONESHOT)) |
5207 | poll_one->events |= EPOLLONESHOT; | |
18bceab1 JA |
5208 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
5209 | if (!poll) { | |
5210 | pt->error = -ENOMEM; | |
5211 | return; | |
5212 | } | |
58852d4d | 5213 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
de9b4cca | 5214 | req_ref_get(req); |
18bceab1 | 5215 | poll->wait.private = req; |
807abcb0 | 5216 | *poll_ptr = poll; |
18bceab1 JA |
5217 | } |
5218 | ||
68b11e8b | 5219 | pt->nr_entries++; |
18bceab1 | 5220 | poll->head = head; |
a31eb4a2 JX |
5221 | |
5222 | if (poll->events & EPOLLEXCLUSIVE) | |
5223 | add_wait_queue_exclusive(head, &poll->wait); | |
5224 | else | |
5225 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
5226 | } |
5227 | ||
5228 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
5229 | struct poll_table_struct *p) | |
5230 | { | |
5231 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
807abcb0 | 5232 | struct async_poll *apoll = pt->req->apoll; |
18bceab1 | 5233 | |
807abcb0 | 5234 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
18bceab1 JA |
5235 | } |
5236 | ||
f237c30a | 5237 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
d7718a9d | 5238 | { |
d7718a9d JA |
5239 | struct async_poll *apoll = req->apoll; |
5240 | struct io_ring_ctx *ctx = req->ctx; | |
5241 | ||
236daeae | 5242 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
d7718a9d | 5243 | |
74ce6ce4 | 5244 | if (io_poll_rewait(req, &apoll->poll)) { |
79ebeaee | 5245 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5246 | return; |
d7718a9d JA |
5247 | } |
5248 | ||
0ea13b44 | 5249 | hash_del(&req->hash_node); |
d4e7cd36 | 5250 | io_poll_remove_double(req); |
79ebeaee | 5251 | spin_unlock(&ctx->completion_lock); |
74ce6ce4 | 5252 | |
0be0b0e3 | 5253 | if (!READ_ONCE(apoll->poll.canceled)) |
f237c30a | 5254 | io_req_task_submit(req, locked); |
0be0b0e3 | 5255 | else |
2593553a | 5256 | io_req_complete_failed(req, -ECANCELED); |
d7718a9d JA |
5257 | } |
5258 | ||
5259 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, | |
5260 | void *key) | |
5261 | { | |
5262 | struct io_kiocb *req = wait->private; | |
5263 | struct io_poll_iocb *poll = &req->apoll->poll; | |
5264 | ||
5265 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, | |
5266 | key_to_poll(key)); | |
5267 | ||
5268 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); | |
5269 | } | |
5270 | ||
5271 | static void io_poll_req_insert(struct io_kiocb *req) | |
5272 | { | |
5273 | struct io_ring_ctx *ctx = req->ctx; | |
5274 | struct hlist_head *list; | |
5275 | ||
5276 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
5277 | hlist_add_head(&req->hash_node, list); | |
5278 | } | |
5279 | ||
5280 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, | |
5281 | struct io_poll_iocb *poll, | |
5282 | struct io_poll_table *ipt, __poll_t mask, | |
5283 | wait_queue_func_t wake_func) | |
5284 | __acquires(&ctx->completion_lock) | |
5285 | { | |
5286 | struct io_ring_ctx *ctx = req->ctx; | |
5287 | bool cancel = false; | |
5288 | ||
4d52f338 | 5289 | INIT_HLIST_NODE(&req->hash_node); |
18bceab1 | 5290 | io_init_poll_iocb(poll, mask, wake_func); |
b90cd197 | 5291 | poll->file = req->file; |
18bceab1 | 5292 | poll->wait.private = req; |
d7718a9d JA |
5293 | |
5294 | ipt->pt._key = mask; | |
5295 | ipt->req = req; | |
68b11e8b PB |
5296 | ipt->error = 0; |
5297 | ipt->nr_entries = 0; | |
d7718a9d | 5298 | |
d7718a9d | 5299 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
68b11e8b PB |
5300 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
5301 | ipt->error = -EINVAL; | |
d7718a9d | 5302 | |
79ebeaee | 5303 | spin_lock(&ctx->completion_lock); |
a890d01e | 5304 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
46fee9ab | 5305 | io_poll_remove_double(req); |
d7718a9d | 5306 | if (likely(poll->head)) { |
79ebeaee | 5307 | spin_lock_irq(&poll->head->lock); |
d7718a9d JA |
5308 | if (unlikely(list_empty(&poll->wait.entry))) { |
5309 | if (ipt->error) | |
5310 | cancel = true; | |
5311 | ipt->error = 0; | |
5312 | mask = 0; | |
5313 | } | |
88e41cf9 | 5314 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
d7718a9d JA |
5315 | list_del_init(&poll->wait.entry); |
5316 | else if (cancel) | |
5317 | WRITE_ONCE(poll->canceled, true); | |
5318 | else if (!poll->done) /* actually waiting for an event */ | |
5319 | io_poll_req_insert(req); | |
79ebeaee | 5320 | spin_unlock_irq(&poll->head->lock); |
d7718a9d JA |
5321 | } |
5322 | ||
5323 | return mask; | |
5324 | } | |
5325 | ||
59b735ae OL |
5326 | enum { |
5327 | IO_APOLL_OK, | |
5328 | IO_APOLL_ABORTED, | |
5329 | IO_APOLL_READY | |
5330 | }; | |
5331 | ||
5332 | static int io_arm_poll_handler(struct io_kiocb *req) | |
d7718a9d JA |
5333 | { |
5334 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
5335 | struct io_ring_ctx *ctx = req->ctx; | |
5336 | struct async_poll *apoll; | |
5337 | struct io_poll_table ipt; | |
b2d9c3da | 5338 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
9dab14b8 | 5339 | int rw; |
d7718a9d JA |
5340 | |
5341 | if (!req->file || !file_can_poll(req->file)) | |
59b735ae | 5342 | return IO_APOLL_ABORTED; |
24c74678 | 5343 | if (req->flags & REQ_F_POLLED) |
59b735ae | 5344 | return IO_APOLL_ABORTED; |
b2d9c3da PB |
5345 | if (!def->pollin && !def->pollout) |
5346 | return IO_APOLL_ABORTED; | |
5347 | ||
5348 | if (def->pollin) { | |
9dab14b8 | 5349 | rw = READ; |
b2d9c3da PB |
5350 | mask |= POLLIN | POLLRDNORM; |
5351 | ||
5352 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
5353 | if ((req->opcode == IORING_OP_RECVMSG) && | |
5354 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
5355 | mask &= ~POLLIN; | |
5356 | } else { | |
9dab14b8 | 5357 | rw = WRITE; |
b2d9c3da PB |
5358 | mask |= POLLOUT | POLLWRNORM; |
5359 | } | |
5360 | ||
9dab14b8 | 5361 | /* if we can't nonblock try, then no point in arming a poll handler */ |
b191e2df | 5362 | if (!io_file_supports_nowait(req, rw)) |
59b735ae | 5363 | return IO_APOLL_ABORTED; |
d7718a9d JA |
5364 | |
5365 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
5366 | if (unlikely(!apoll)) | |
59b735ae | 5367 | return IO_APOLL_ABORTED; |
807abcb0 | 5368 | apoll->double_poll = NULL; |
d7718a9d | 5369 | req->apoll = apoll; |
b2d9c3da | 5370 | req->flags |= REQ_F_POLLED; |
d7718a9d | 5371 | ipt.pt._qproc = io_async_queue_proc; |
48dcd38d | 5372 | io_req_set_refcount(req); |
d7718a9d JA |
5373 | |
5374 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, | |
5375 | io_async_wake); | |
79ebeaee | 5376 | spin_unlock(&ctx->completion_lock); |
41a5169c HX |
5377 | if (ret || ipt.error) |
5378 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
5379 | ||
236daeae OL |
5380 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
5381 | mask, apoll->poll.events); | |
59b735ae | 5382 | return IO_APOLL_OK; |
d7718a9d JA |
5383 | } |
5384 | ||
5385 | static bool __io_poll_remove_one(struct io_kiocb *req, | |
b2e720ac | 5386 | struct io_poll_iocb *poll, bool do_cancel) |
e07785b0 | 5387 | __must_hold(&req->ctx->completion_lock) |
221c5eb2 | 5388 | { |
b41e9852 | 5389 | bool do_complete = false; |
221c5eb2 | 5390 | |
5082620f JA |
5391 | if (!poll->head) |
5392 | return false; | |
79ebeaee | 5393 | spin_lock_irq(&poll->head->lock); |
b2e720ac JA |
5394 | if (do_cancel) |
5395 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
5396 | if (!list_empty(&poll->wait.entry)) { |
5397 | list_del_init(&poll->wait.entry); | |
b41e9852 | 5398 | do_complete = true; |
221c5eb2 | 5399 | } |
79ebeaee | 5400 | spin_unlock_irq(&poll->head->lock); |
3bfa5bcb | 5401 | hash_del(&req->hash_node); |
d7718a9d JA |
5402 | return do_complete; |
5403 | } | |
5404 | ||
5d709043 | 5405 | static bool io_poll_remove_one(struct io_kiocb *req) |
e07785b0 | 5406 | __must_hold(&req->ctx->completion_lock) |
d7718a9d JA |
5407 | { |
5408 | bool do_complete; | |
5409 | ||
d4e7cd36 | 5410 | io_poll_remove_double(req); |
e31001a3 | 5411 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
d4e7cd36 | 5412 | |
b41e9852 | 5413 | if (do_complete) { |
d4d19c19 | 5414 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
b41e9852 | 5415 | io_commit_cqring(req->ctx); |
93d2bcd2 | 5416 | req_set_fail(req); |
91c2f697 | 5417 | io_put_req_deferred(req); |
5d709043 | 5418 | } |
b41e9852 | 5419 | return do_complete; |
221c5eb2 JA |
5420 | } |
5421 | ||
76e1b642 JA |
5422 | /* |
5423 | * Returns true if we found and killed one or more poll requests | |
5424 | */ | |
6b81928d | 5425 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
3dd0c97a | 5426 | bool cancel_all) |
221c5eb2 | 5427 | { |
78076bb6 | 5428 | struct hlist_node *tmp; |
221c5eb2 | 5429 | struct io_kiocb *req; |
8e2e1faf | 5430 | int posted = 0, i; |
221c5eb2 | 5431 | |
79ebeaee | 5432 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
5433 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
5434 | struct hlist_head *list; | |
5435 | ||
5436 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 5437 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
3dd0c97a | 5438 | if (io_match_task(req, tsk, cancel_all)) |
f3606e3a JA |
5439 | posted += io_poll_remove_one(req); |
5440 | } | |
221c5eb2 | 5441 | } |
79ebeaee | 5442 | spin_unlock(&ctx->completion_lock); |
b41e9852 | 5443 | |
8e2e1faf JA |
5444 | if (posted) |
5445 | io_cqring_ev_posted(ctx); | |
76e1b642 JA |
5446 | |
5447 | return posted != 0; | |
221c5eb2 JA |
5448 | } |
5449 | ||
9ba5fac8 PB |
5450 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5451 | bool poll_only) | |
e07785b0 | 5452 | __must_hold(&ctx->completion_lock) |
47f46768 | 5453 | { |
78076bb6 | 5454 | struct hlist_head *list; |
47f46768 JA |
5455 | struct io_kiocb *req; |
5456 | ||
78076bb6 JA |
5457 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
5458 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
5459 | if (sqe_addr != req->user_data) |
5460 | continue; | |
9ba5fac8 PB |
5461 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
5462 | continue; | |
b2cb805f | 5463 | return req; |
47f46768 | 5464 | } |
b2cb805f JA |
5465 | return NULL; |
5466 | } | |
5467 | ||
9ba5fac8 PB |
5468 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
5469 | bool poll_only) | |
e07785b0 | 5470 | __must_hold(&ctx->completion_lock) |
b2cb805f JA |
5471 | { |
5472 | struct io_kiocb *req; | |
5473 | ||
9ba5fac8 | 5474 | req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f JA |
5475 | if (!req) |
5476 | return -ENOENT; | |
5477 | if (io_poll_remove_one(req)) | |
5478 | return 0; | |
5479 | ||
5480 | return -EALREADY; | |
47f46768 JA |
5481 | } |
5482 | ||
9096af3e PB |
5483 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
5484 | unsigned int flags) | |
5485 | { | |
5486 | u32 events; | |
47f46768 | 5487 | |
9096af3e PB |
5488 | events = READ_ONCE(sqe->poll32_events); |
5489 | #ifdef __BIG_ENDIAN | |
5490 | events = swahw32(events); | |
5491 | #endif | |
5492 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
5493 | events |= EPOLLONESHOT; | |
5494 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
5495 | } |
5496 | ||
c5de0036 | 5497 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 5498 | const struct io_uring_sqe *sqe) |
0969e783 | 5499 | { |
c5de0036 PB |
5500 | struct io_poll_update *upd = &req->poll_update; |
5501 | u32 flags; | |
5502 | ||
0969e783 JA |
5503 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5504 | return -EINVAL; | |
26578cda | 5505 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
5506 | return -EINVAL; |
5507 | flags = READ_ONCE(sqe->len); | |
5508 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
5509 | IORING_POLL_ADD_MULTI)) | |
5510 | return -EINVAL; | |
5511 | /* meaningless without update */ | |
5512 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
5513 | return -EINVAL; |
5514 | ||
c5de0036 PB |
5515 | upd->old_user_data = READ_ONCE(sqe->addr); |
5516 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
5517 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 5518 | |
c5de0036 PB |
5519 | upd->new_user_data = READ_ONCE(sqe->off); |
5520 | if (!upd->update_user_data && upd->new_user_data) | |
5521 | return -EINVAL; | |
5522 | if (upd->update_events) | |
5523 | upd->events = io_poll_parse_events(sqe, flags); | |
5524 | else if (sqe->poll32_events) | |
5525 | return -EINVAL; | |
221c5eb2 | 5526 | |
221c5eb2 JA |
5527 | return 0; |
5528 | } | |
5529 | ||
221c5eb2 JA |
5530 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
5531 | void *key) | |
5532 | { | |
c2f2eb7d JA |
5533 | struct io_kiocb *req = wait->private; |
5534 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 5535 | |
d7718a9d | 5536 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
221c5eb2 JA |
5537 | } |
5538 | ||
221c5eb2 JA |
5539 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
5540 | struct poll_table_struct *p) | |
5541 | { | |
5542 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
5543 | ||
e8c2bc1f | 5544 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
eac406c6 JA |
5545 | } |
5546 | ||
3529d8c2 | 5547 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
5548 | { |
5549 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 5550 | u32 flags; |
221c5eb2 JA |
5551 | |
5552 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
5553 | return -EINVAL; | |
c5de0036 | 5554 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
5555 | return -EINVAL; |
5556 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 5557 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 JA |
5558 | return -EINVAL; |
5559 | ||
48dcd38d | 5560 | io_req_set_refcount(req); |
c5de0036 | 5561 | poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
5562 | return 0; |
5563 | } | |
5564 | ||
61e98203 | 5565 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
5566 | { |
5567 | struct io_poll_iocb *poll = &req->poll; | |
5568 | struct io_ring_ctx *ctx = req->ctx; | |
5569 | struct io_poll_table ipt; | |
0969e783 | 5570 | __poll_t mask; |
0969e783 | 5571 | |
d7718a9d | 5572 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 5573 | |
d7718a9d JA |
5574 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
5575 | io_poll_wake); | |
221c5eb2 | 5576 | |
8c838788 | 5577 | if (mask) { /* no async, we'd stolen it */ |
221c5eb2 | 5578 | ipt.error = 0; |
e27414be | 5579 | io_poll_complete(req, mask); |
221c5eb2 | 5580 | } |
79ebeaee | 5581 | spin_unlock(&ctx->completion_lock); |
221c5eb2 | 5582 | |
8c838788 JA |
5583 | if (mask) { |
5584 | io_cqring_ev_posted(ctx); | |
88e41cf9 JA |
5585 | if (poll->events & EPOLLONESHOT) |
5586 | io_put_req(req); | |
221c5eb2 | 5587 | } |
8c838788 | 5588 | return ipt.error; |
221c5eb2 JA |
5589 | } |
5590 | ||
c5de0036 | 5591 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
5592 | { |
5593 | struct io_ring_ctx *ctx = req->ctx; | |
5594 | struct io_kiocb *preq; | |
cb3b200e | 5595 | bool completing; |
b69de288 JA |
5596 | int ret; |
5597 | ||
79ebeaee | 5598 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 5599 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
b69de288 JA |
5600 | if (!preq) { |
5601 | ret = -ENOENT; | |
5602 | goto err; | |
b69de288 | 5603 | } |
cb3b200e | 5604 | |
c5de0036 PB |
5605 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
5606 | completing = true; | |
5607 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; | |
5608 | goto err; | |
5609 | } | |
5610 | ||
cb3b200e JA |
5611 | /* |
5612 | * Don't allow racy completion with singleshot, as we cannot safely | |
5613 | * update those. For multishot, if we're racing with completion, just | |
5614 | * let completion re-add it. | |
5615 | */ | |
5616 | completing = !__io_poll_remove_one(preq, &preq->poll, false); | |
5617 | if (completing && (preq->poll.events & EPOLLONESHOT)) { | |
5618 | ret = -EALREADY; | |
5619 | goto err; | |
b69de288 JA |
5620 | } |
5621 | /* we now have a detached poll request. reissue. */ | |
5622 | ret = 0; | |
5623 | err: | |
b69de288 | 5624 | if (ret < 0) { |
79ebeaee | 5625 | spin_unlock(&ctx->completion_lock); |
93d2bcd2 | 5626 | req_set_fail(req); |
b69de288 JA |
5627 | io_req_complete(req, ret); |
5628 | return 0; | |
5629 | } | |
5630 | /* only mask one event flags, keep behavior flags */ | |
9d805892 | 5631 | if (req->poll_update.update_events) { |
b69de288 | 5632 | preq->poll.events &= ~0xffff; |
9d805892 | 5633 | preq->poll.events |= req->poll_update.events & 0xffff; |
b69de288 JA |
5634 | preq->poll.events |= IO_POLL_UNMASK; |
5635 | } | |
9d805892 PB |
5636 | if (req->poll_update.update_user_data) |
5637 | preq->user_data = req->poll_update.new_user_data; | |
79ebeaee | 5638 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 5639 | |
b69de288 JA |
5640 | /* complete update request, we're done with it */ |
5641 | io_req_complete(req, ret); | |
5642 | ||
cb3b200e | 5643 | if (!completing) { |
c5de0036 | 5644 | ret = io_poll_add(preq, issue_flags); |
cb3b200e | 5645 | if (ret < 0) { |
93d2bcd2 | 5646 | req_set_fail(preq); |
cb3b200e JA |
5647 | io_req_complete(preq, ret); |
5648 | } | |
b69de288 JA |
5649 | } |
5650 | return 0; | |
5651 | } | |
5652 | ||
f237c30a | 5653 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
89850fce | 5654 | { |
89850fce | 5655 | req_set_fail(req); |
505657bc | 5656 | io_req_complete_post(req, -ETIME, 0); |
89850fce JA |
5657 | } |
5658 | ||
5262f567 JA |
5659 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
5660 | { | |
ad8a48ac JA |
5661 | struct io_timeout_data *data = container_of(timer, |
5662 | struct io_timeout_data, timer); | |
5663 | struct io_kiocb *req = data->req; | |
5664 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
5665 | unsigned long flags; |
5666 | ||
89850fce | 5667 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 5668 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
5669 | atomic_set(&req->ctx->cq_timeouts, |
5670 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 5671 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 5672 | |
89850fce JA |
5673 | req->io_task_work.func = io_req_task_timeout; |
5674 | io_req_task_work_add(req); | |
5262f567 JA |
5675 | return HRTIMER_NORESTART; |
5676 | } | |
5677 | ||
fbd15848 PB |
5678 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
5679 | __u64 user_data) | |
89850fce | 5680 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 5681 | { |
fbd15848 | 5682 | struct io_timeout_data *io; |
47f46768 | 5683 | struct io_kiocb *req; |
fd9c7bc5 | 5684 | bool found = false; |
f254ac04 | 5685 | |
135fcde8 | 5686 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
5687 | found = user_data == req->user_data; |
5688 | if (found) | |
47f46768 | 5689 | break; |
47f46768 | 5690 | } |
fd9c7bc5 PB |
5691 | if (!found) |
5692 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
5693 | |
5694 | io = req->async_data; | |
fd9c7bc5 | 5695 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 5696 | return ERR_PTR(-EALREADY); |
a71976f3 | 5697 | list_del_init(&req->timeout.list); |
fbd15848 PB |
5698 | return req; |
5699 | } | |
47f46768 | 5700 | |
fbd15848 | 5701 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 5702 | __must_hold(&ctx->completion_lock) |
89850fce | 5703 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
5704 | { |
5705 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
5706 | ||
5707 | if (IS_ERR(req)) | |
5708 | return PTR_ERR(req); | |
f254ac04 | 5709 | |
93d2bcd2 | 5710 | req_set_fail(req); |
d4d19c19 | 5711 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
91c2f697 | 5712 | io_put_req_deferred(req); |
f254ac04 JA |
5713 | return 0; |
5714 | } | |
5715 | ||
50c1df2b JA |
5716 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
5717 | { | |
5718 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
5719 | case IORING_TIMEOUT_BOOTTIME: | |
5720 | return CLOCK_BOOTTIME; | |
5721 | case IORING_TIMEOUT_REALTIME: | |
5722 | return CLOCK_REALTIME; | |
5723 | default: | |
5724 | /* can't happen, vetted at prep time */ | |
5725 | WARN_ON_ONCE(1); | |
5726 | fallthrough; | |
5727 | case 0: | |
5728 | return CLOCK_MONOTONIC; | |
5729 | } | |
5730 | } | |
5731 | ||
9c8e11b3 PB |
5732 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
5733 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 5734 | __must_hold(&ctx->timeout_lock) |
47f46768 | 5735 | { |
9c8e11b3 PB |
5736 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
5737 | struct io_timeout_data *data; | |
47f46768 | 5738 | |
9c8e11b3 PB |
5739 | if (IS_ERR(req)) |
5740 | return PTR_ERR(req); | |
47f46768 | 5741 | |
9c8e11b3 PB |
5742 | req->timeout.off = 0; /* noseq */ |
5743 | data = req->async_data; | |
5744 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 5745 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
5746 | data->timer.function = io_timeout_fn; |
5747 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
5748 | return 0; | |
47f46768 JA |
5749 | } |
5750 | ||
3529d8c2 JA |
5751 | static int io_timeout_remove_prep(struct io_kiocb *req, |
5752 | const struct io_uring_sqe *sqe) | |
b29472ee | 5753 | { |
9c8e11b3 PB |
5754 | struct io_timeout_rem *tr = &req->timeout_rem; |
5755 | ||
b29472ee JA |
5756 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5757 | return -EINVAL; | |
61710e43 DA |
5758 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5759 | return -EINVAL; | |
26578cda | 5760 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
5761 | return -EINVAL; |
5762 | ||
9c8e11b3 PB |
5763 | tr->addr = READ_ONCE(sqe->addr); |
5764 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
5765 | if (tr->flags & IORING_TIMEOUT_UPDATE) { | |
5766 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) | |
5767 | return -EINVAL; | |
5768 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
5769 | return -EFAULT; | |
5770 | } else if (tr->flags) { | |
5771 | /* timeout removal doesn't support flags */ | |
b29472ee | 5772 | return -EINVAL; |
9c8e11b3 | 5773 | } |
b29472ee | 5774 | |
b29472ee JA |
5775 | return 0; |
5776 | } | |
5777 | ||
8662daec PB |
5778 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
5779 | { | |
5780 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
5781 | : HRTIMER_MODE_REL; | |
5782 | } | |
5783 | ||
11365043 JA |
5784 | /* |
5785 | * Remove or update an existing timeout command | |
5786 | */ | |
61e98203 | 5787 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 5788 | { |
9c8e11b3 | 5789 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 5790 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 5791 | int ret; |
11365043 | 5792 | |
ec3c3d0f PB |
5793 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
5794 | spin_lock(&ctx->completion_lock); | |
5795 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 5796 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
5797 | spin_unlock_irq(&ctx->timeout_lock); |
5798 | spin_unlock(&ctx->completion_lock); | |
5799 | } else { | |
5800 | spin_lock_irq(&ctx->timeout_lock); | |
8662daec PB |
5801 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
5802 | io_translate_timeout_mode(tr->flags)); | |
ec3c3d0f PB |
5803 | spin_unlock_irq(&ctx->timeout_lock); |
5804 | } | |
11365043 | 5805 | |
4e88d6e7 | 5806 | if (ret < 0) |
93d2bcd2 | 5807 | req_set_fail(req); |
505657bc | 5808 | io_req_complete_post(req, ret, 0); |
11365043 | 5809 | return 0; |
5262f567 JA |
5810 | } |
5811 | ||
3529d8c2 | 5812 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 5813 | bool is_timeout_link) |
5262f567 | 5814 | { |
ad8a48ac | 5815 | struct io_timeout_data *data; |
a41525ab | 5816 | unsigned flags; |
56080b02 | 5817 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 5818 | |
ad8a48ac | 5819 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 5820 | return -EINVAL; |
26578cda PB |
5821 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
5822 | sqe->splice_fd_in) | |
a41525ab | 5823 | return -EINVAL; |
56080b02 | 5824 | if (off && is_timeout_link) |
2d28390a | 5825 | return -EINVAL; |
a41525ab | 5826 | flags = READ_ONCE(sqe->timeout_flags); |
50c1df2b JA |
5827 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK)) |
5828 | return -EINVAL; | |
5829 | /* more than one clock specified is invalid, obviously */ | |
5830 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 5831 | return -EINVAL; |
bdf20073 | 5832 | |
bfe68a22 | 5833 | req->timeout.off = off; |
f18ee4cf PB |
5834 | if (unlikely(off && !req->ctx->off_timeout_used)) |
5835 | req->ctx->off_timeout_used = true; | |
26a61679 | 5836 | |
e8c2bc1f | 5837 | if (!req->async_data && io_alloc_async_data(req)) |
26a61679 JA |
5838 | return -ENOMEM; |
5839 | ||
e8c2bc1f | 5840 | data = req->async_data; |
ad8a48ac | 5841 | data->req = req; |
50c1df2b | 5842 | data->flags = flags; |
ad8a48ac JA |
5843 | |
5844 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
5845 | return -EFAULT; |
5846 | ||
8662daec | 5847 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 5848 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
5849 | |
5850 | if (is_timeout_link) { | |
5851 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
5852 | ||
5853 | if (!link->head) | |
5854 | return -EINVAL; | |
5855 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
5856 | return -EINVAL; | |
4d13d1a4 PB |
5857 | req->timeout.head = link->last; |
5858 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 5859 | } |
ad8a48ac JA |
5860 | return 0; |
5861 | } | |
5862 | ||
61e98203 | 5863 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 5864 | { |
ad8a48ac | 5865 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 5866 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 5867 | struct list_head *entry; |
bfe68a22 | 5868 | u32 tail, off = req->timeout.off; |
ad8a48ac | 5869 | |
89850fce | 5870 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 5871 | |
5262f567 JA |
5872 | /* |
5873 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
5874 | * timeout event to be satisfied. If it isn't set, then this is |
5875 | * a pure timeout request, sequence isn't used. | |
5262f567 | 5876 | */ |
8eb7e2d0 | 5877 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
5878 | entry = ctx->timeout_list.prev; |
5879 | goto add; | |
5880 | } | |
5262f567 | 5881 | |
bfe68a22 PB |
5882 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
5883 | req->timeout.target_seq = tail + off; | |
5262f567 | 5884 | |
f010505b MDG |
5885 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
5886 | * This is safe because ->completion_lock is held, and submissions | |
5887 | * and completions are never mixed in the same ->completion_lock section. | |
5888 | */ | |
5889 | ctx->cq_last_tm_flush = tail; | |
5890 | ||
5262f567 JA |
5891 | /* |
5892 | * Insertion sort, ensuring the first entry in the list is always | |
5893 | * the one we need first. | |
5894 | */ | |
5262f567 | 5895 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
5896 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
5897 | timeout.list); | |
5262f567 | 5898 | |
8eb7e2d0 | 5899 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 5900 | continue; |
bfe68a22 PB |
5901 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
5902 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
5903 | break; |
5904 | } | |
93bd25bb | 5905 | add: |
135fcde8 | 5906 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
5907 | data->timer.function = io_timeout_fn; |
5908 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 5909 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
5910 | return 0; |
5911 | } | |
5262f567 | 5912 | |
f458dd84 PB |
5913 | struct io_cancel_data { |
5914 | struct io_ring_ctx *ctx; | |
5915 | u64 user_data; | |
5916 | }; | |
5917 | ||
62755e35 JA |
5918 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
5919 | { | |
5920 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 5921 | struct io_cancel_data *cd = data; |
62755e35 | 5922 | |
f458dd84 | 5923 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
5924 | } |
5925 | ||
f458dd84 PB |
5926 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
5927 | struct io_ring_ctx *ctx) | |
62755e35 | 5928 | { |
f458dd84 | 5929 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 5930 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
5931 | int ret = 0; |
5932 | ||
f458dd84 | 5933 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
5934 | return -ENOENT; |
5935 | ||
f458dd84 | 5936 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
5937 | switch (cancel_ret) { |
5938 | case IO_WQ_CANCEL_OK: | |
5939 | ret = 0; | |
5940 | break; | |
5941 | case IO_WQ_CANCEL_RUNNING: | |
5942 | ret = -EALREADY; | |
5943 | break; | |
5944 | case IO_WQ_CANCEL_NOTFOUND: | |
5945 | ret = -ENOENT; | |
5946 | break; | |
5947 | } | |
5948 | ||
e977d6d3 JA |
5949 | return ret; |
5950 | } | |
5951 | ||
8cb01fac | 5952 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 5953 | { |
8cb01fac | 5954 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
5955 | int ret; |
5956 | ||
dadebc35 | 5957 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 5958 | |
f458dd84 | 5959 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
df9727af | 5960 | if (ret != -ENOENT) |
8cb01fac | 5961 | return ret; |
505657bc PB |
5962 | |
5963 | spin_lock(&ctx->completion_lock); | |
79ebeaee | 5964 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 5965 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 5966 | spin_unlock_irq(&ctx->timeout_lock); |
47f46768 | 5967 | if (ret != -ENOENT) |
505657bc PB |
5968 | goto out; |
5969 | ret = io_poll_cancel(ctx, sqe_addr, false); | |
5970 | out: | |
5971 | spin_unlock(&ctx->completion_lock); | |
5972 | return ret; | |
47f46768 JA |
5973 | } |
5974 | ||
3529d8c2 JA |
5975 | static int io_async_cancel_prep(struct io_kiocb *req, |
5976 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 5977 | { |
fbf23849 | 5978 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 5979 | return -EINVAL; |
61710e43 DA |
5980 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
5981 | return -EINVAL; | |
26578cda PB |
5982 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
5983 | sqe->splice_fd_in) | |
e977d6d3 JA |
5984 | return -EINVAL; |
5985 | ||
fbf23849 JA |
5986 | req->cancel.addr = READ_ONCE(sqe->addr); |
5987 | return 0; | |
5988 | } | |
5989 | ||
61e98203 | 5990 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
5991 | { |
5992 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 PB |
5993 | u64 sqe_addr = req->cancel.addr; |
5994 | struct io_tctx_node *node; | |
5995 | int ret; | |
5996 | ||
8cb01fac | 5997 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
5998 | if (ret != -ENOENT) |
5999 | goto done; | |
58f99373 PB |
6000 | |
6001 | /* slow path, try all io-wq's */ | |
6002 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
6003 | ret = -ENOENT; | |
6004 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6005 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6006 | |
58f99373 PB |
6007 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6008 | if (ret != -ENOENT) | |
6009 | break; | |
6010 | } | |
6011 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); | |
58f99373 | 6012 | done: |
58f99373 | 6013 | if (ret < 0) |
93d2bcd2 | 6014 | req_set_fail(req); |
505657bc | 6015 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6016 | return 0; |
6017 | } | |
6018 | ||
269bbe5f | 6019 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6020 | const struct io_uring_sqe *sqe) |
6021 | { | |
61710e43 DA |
6022 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6023 | return -EINVAL; | |
26578cda | 6024 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6025 | return -EINVAL; |
6026 | ||
269bbe5f BM |
6027 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6028 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6029 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6030 | return -EINVAL; |
269bbe5f | 6031 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6032 | return 0; |
6033 | } | |
6034 | ||
889fca73 | 6035 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6036 | { |
6037 | struct io_ring_ctx *ctx = req->ctx; | |
c3bdad02 | 6038 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6039 | int ret; |
fbf23849 | 6040 | |
45d189c6 | 6041 | if (issue_flags & IO_URING_F_NONBLOCK) |
05f3fb3c | 6042 | return -EAGAIN; |
05f3fb3c | 6043 | |
269bbe5f BM |
6044 | up.offset = req->rsrc_update.offset; |
6045 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6046 | up.nr = 0; |
6047 | up.tags = 0; | |
615cee49 | 6048 | up.resv = 0; |
05f3fb3c JA |
6049 | |
6050 | mutex_lock(&ctx->uring_lock); | |
fdecb662 | 6051 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6052 | &up, req->rsrc_update.nr_args); |
05f3fb3c JA |
6053 | mutex_unlock(&ctx->uring_lock); |
6054 | ||
6055 | if (ret < 0) | |
93d2bcd2 | 6056 | req_set_fail(req); |
889fca73 | 6057 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6058 | return 0; |
6059 | } | |
6060 | ||
bfe76559 | 6061 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6062 | { |
d625c6ee | 6063 | switch (req->opcode) { |
e781573e | 6064 | case IORING_OP_NOP: |
bfe76559 | 6065 | return 0; |
f67676d1 JA |
6066 | case IORING_OP_READV: |
6067 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6068 | case IORING_OP_READ: |
bfe76559 | 6069 | return io_read_prep(req, sqe); |
f67676d1 JA |
6070 | case IORING_OP_WRITEV: |
6071 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6072 | case IORING_OP_WRITE: |
bfe76559 | 6073 | return io_write_prep(req, sqe); |
0969e783 | 6074 | case IORING_OP_POLL_ADD: |
bfe76559 | 6075 | return io_poll_add_prep(req, sqe); |
0969e783 | 6076 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 6077 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 6078 | case IORING_OP_FSYNC: |
1155c76a | 6079 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 6080 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 6081 | return io_sfr_prep(req, sqe); |
03b1230c | 6082 | case IORING_OP_SENDMSG: |
fddaface | 6083 | case IORING_OP_SEND: |
bfe76559 | 6084 | return io_sendmsg_prep(req, sqe); |
03b1230c | 6085 | case IORING_OP_RECVMSG: |
fddaface | 6086 | case IORING_OP_RECV: |
bfe76559 | 6087 | return io_recvmsg_prep(req, sqe); |
f499a021 | 6088 | case IORING_OP_CONNECT: |
bfe76559 | 6089 | return io_connect_prep(req, sqe); |
2d28390a | 6090 | case IORING_OP_TIMEOUT: |
bfe76559 | 6091 | return io_timeout_prep(req, sqe, false); |
b29472ee | 6092 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 6093 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 6094 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 6095 | return io_async_cancel_prep(req, sqe); |
2d28390a | 6096 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 6097 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 6098 | case IORING_OP_ACCEPT: |
bfe76559 | 6099 | return io_accept_prep(req, sqe); |
d63d1b5e | 6100 | case IORING_OP_FALLOCATE: |
bfe76559 | 6101 | return io_fallocate_prep(req, sqe); |
15b71abe | 6102 | case IORING_OP_OPENAT: |
bfe76559 | 6103 | return io_openat_prep(req, sqe); |
b5dba59e | 6104 | case IORING_OP_CLOSE: |
bfe76559 | 6105 | return io_close_prep(req, sqe); |
05f3fb3c | 6106 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 6107 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 6108 | case IORING_OP_STATX: |
bfe76559 | 6109 | return io_statx_prep(req, sqe); |
4840e418 | 6110 | case IORING_OP_FADVISE: |
bfe76559 | 6111 | return io_fadvise_prep(req, sqe); |
c1ca757b | 6112 | case IORING_OP_MADVISE: |
bfe76559 | 6113 | return io_madvise_prep(req, sqe); |
cebdb986 | 6114 | case IORING_OP_OPENAT2: |
bfe76559 | 6115 | return io_openat2_prep(req, sqe); |
3e4827b0 | 6116 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 6117 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 6118 | case IORING_OP_SPLICE: |
bfe76559 | 6119 | return io_splice_prep(req, sqe); |
ddf0322d | 6120 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 6121 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 6122 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 6123 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 6124 | case IORING_OP_TEE: |
bfe76559 | 6125 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
6126 | case IORING_OP_SHUTDOWN: |
6127 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
6128 | case IORING_OP_RENAMEAT: |
6129 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
6130 | case IORING_OP_UNLINKAT: |
6131 | return io_unlinkat_prep(req, sqe); | |
f67676d1 JA |
6132 | } |
6133 | ||
bfe76559 PB |
6134 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
6135 | req->opcode); | |
bd54b6fe | 6136 | return -EINVAL; |
bfe76559 PB |
6137 | } |
6138 | ||
93642ef8 | 6139 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 6140 | { |
b7e298d2 PB |
6141 | if (!io_op_defs[req->opcode].needs_async_setup) |
6142 | return 0; | |
6143 | if (WARN_ON_ONCE(req->async_data)) | |
6144 | return -EFAULT; | |
6145 | if (io_alloc_async_data(req)) | |
6146 | return -EAGAIN; | |
6147 | ||
93642ef8 PB |
6148 | switch (req->opcode) { |
6149 | case IORING_OP_READV: | |
93642ef8 PB |
6150 | return io_rw_prep_async(req, READ); |
6151 | case IORING_OP_WRITEV: | |
93642ef8 PB |
6152 | return io_rw_prep_async(req, WRITE); |
6153 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
6154 | return io_sendmsg_prep_async(req); |
6155 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
6156 | return io_recvmsg_prep_async(req); |
6157 | case IORING_OP_CONNECT: | |
6158 | return io_connect_prep_async(req); | |
6159 | } | |
b7e298d2 PB |
6160 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
6161 | req->opcode); | |
6162 | return -EFAULT; | |
f67676d1 JA |
6163 | } |
6164 | ||
9cf7c104 PB |
6165 | static u32 io_get_sequence(struct io_kiocb *req) |
6166 | { | |
a3dbdf54 | 6167 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 6168 | |
a3dbdf54 PB |
6169 | /* need original cached_sq_head, but it was increased for each req */ |
6170 | io_for_each_link(req, req) | |
6171 | seq--; | |
6172 | return seq; | |
9cf7c104 PB |
6173 | } |
6174 | ||
76cc33d7 | 6175 | static bool io_drain_req(struct io_kiocb *req) |
de0617e4 | 6176 | { |
3c19966d | 6177 | struct io_kiocb *pos; |
a197f664 | 6178 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 6179 | struct io_defer_entry *de; |
f67676d1 | 6180 | int ret; |
9cf7c104 | 6181 | u32 seq; |
de0617e4 | 6182 | |
3c19966d PB |
6183 | /* |
6184 | * If we need to drain a request in the middle of a link, drain the | |
6185 | * head request and the next request/link after the current link. | |
6186 | * Considering sequential execution of links, IOSQE_IO_DRAIN will be | |
6187 | * maintained for every request of our link. | |
6188 | */ | |
6189 | if (ctx->drain_next) { | |
6190 | req->flags |= REQ_F_IO_DRAIN; | |
6191 | ctx->drain_next = false; | |
6192 | } | |
6193 | /* not interested in head, start from the first linked */ | |
6194 | io_for_each_link(pos, req->link) { | |
6195 | if (pos->flags & REQ_F_IO_DRAIN) { | |
6196 | ctx->drain_next = true; | |
6197 | req->flags |= REQ_F_IO_DRAIN; | |
6198 | break; | |
6199 | } | |
6200 | } | |
6201 | ||
9d858b21 | 6202 | /* Still need defer if there is pending req in defer list. */ |
9cf7c104 | 6203 | if (likely(list_empty_careful(&ctx->defer_list) && |
10c66904 PB |
6204 | !(req->flags & REQ_F_IO_DRAIN))) { |
6205 | ctx->drain_active = false; | |
76cc33d7 | 6206 | return false; |
10c66904 | 6207 | } |
9cf7c104 PB |
6208 | |
6209 | seq = io_get_sequence(req); | |
6210 | /* Still a chance to pass the sequence check */ | |
6211 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) | |
76cc33d7 | 6212 | return false; |
de0617e4 | 6213 | |
b7e298d2 | 6214 | ret = io_req_prep_async(req); |
be7053b7 | 6215 | if (ret) |
1b48773f | 6216 | goto fail; |
cbdcb435 | 6217 | io_prep_async_link(req); |
27dc8338 | 6218 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 6219 | if (!de) { |
1b48773f PB |
6220 | ret = -ENOMEM; |
6221 | fail: | |
6222 | io_req_complete_failed(req, ret); | |
76cc33d7 PB |
6223 | return true; |
6224 | } | |
2d28390a | 6225 | |
79ebeaee | 6226 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 6227 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 6228 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 6229 | kfree(de); |
f237c30a | 6230 | io_queue_async_work(req, NULL); |
76cc33d7 | 6231 | return true; |
de0617e4 JA |
6232 | } |
6233 | ||
915967f6 | 6234 | trace_io_uring_defer(ctx, req, req->user_data); |
27dc8338 | 6235 | de->req = req; |
9cf7c104 | 6236 | de->seq = seq; |
27dc8338 | 6237 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 6238 | spin_unlock(&ctx->completion_lock); |
76cc33d7 | 6239 | return true; |
de0617e4 JA |
6240 | } |
6241 | ||
68fb8979 | 6242 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 6243 | { |
0e1b6fe3 PB |
6244 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
6245 | switch (req->opcode) { | |
6246 | case IORING_OP_READV: | |
6247 | case IORING_OP_READ_FIXED: | |
6248 | case IORING_OP_READ: | |
bcda7baa | 6249 | kfree((void *)(unsigned long)req->rw.addr); |
0e1b6fe3 PB |
6250 | break; |
6251 | case IORING_OP_RECVMSG: | |
6252 | case IORING_OP_RECV: | |
bcda7baa | 6253 | kfree(req->sr_msg.kbuf); |
0e1b6fe3 PB |
6254 | break; |
6255 | } | |
99bc4c38 PB |
6256 | } |
6257 | ||
0e1b6fe3 PB |
6258 | if (req->flags & REQ_F_NEED_CLEANUP) { |
6259 | switch (req->opcode) { | |
6260 | case IORING_OP_READV: | |
6261 | case IORING_OP_READ_FIXED: | |
6262 | case IORING_OP_READ: | |
6263 | case IORING_OP_WRITEV: | |
6264 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
6265 | case IORING_OP_WRITE: { |
6266 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
6267 | |
6268 | kfree(io->free_iovec); | |
0e1b6fe3 | 6269 | break; |
e8c2bc1f | 6270 | } |
0e1b6fe3 | 6271 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
6272 | case IORING_OP_SENDMSG: { |
6273 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
6274 | |
6275 | kfree(io->free_iov); | |
0e1b6fe3 | 6276 | break; |
e8c2bc1f | 6277 | } |
0e1b6fe3 PB |
6278 | case IORING_OP_SPLICE: |
6279 | case IORING_OP_TEE: | |
e1d767f0 PB |
6280 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
6281 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 6282 | break; |
f3cd4850 JA |
6283 | case IORING_OP_OPENAT: |
6284 | case IORING_OP_OPENAT2: | |
6285 | if (req->open.filename) | |
6286 | putname(req->open.filename); | |
6287 | break; | |
80a261fd JA |
6288 | case IORING_OP_RENAMEAT: |
6289 | putname(req->rename.oldpath); | |
6290 | putname(req->rename.newpath); | |
6291 | break; | |
14a1143b JA |
6292 | case IORING_OP_UNLINKAT: |
6293 | putname(req->unlink.filename); | |
6294 | break; | |
0e1b6fe3 | 6295 | } |
99bc4c38 | 6296 | } |
75652a30 JA |
6297 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
6298 | kfree(req->apoll->double_poll); | |
6299 | kfree(req->apoll); | |
6300 | req->apoll = NULL; | |
6301 | } | |
3a0a6902 PB |
6302 | if (req->flags & REQ_F_INFLIGHT) { |
6303 | struct io_uring_task *tctx = req->task->io_uring; | |
6304 | ||
6305 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 6306 | } |
c854357b | 6307 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 6308 | put_cred(req->creds); |
c854357b PB |
6309 | |
6310 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
99bc4c38 PB |
6311 | } |
6312 | ||
889fca73 | 6313 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 6314 | { |
a197f664 | 6315 | struct io_ring_ctx *ctx = req->ctx; |
5730b27e | 6316 | const struct cred *creds = NULL; |
d625c6ee | 6317 | int ret; |
2b188cc1 | 6318 | |
b8e64b53 | 6319 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) |
c10d1f98 | 6320 | creds = override_creds(req->creds); |
5730b27e | 6321 | |
d625c6ee | 6322 | switch (req->opcode) { |
2b188cc1 | 6323 | case IORING_OP_NOP: |
889fca73 | 6324 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
6325 | break; |
6326 | case IORING_OP_READV: | |
edafccee | 6327 | case IORING_OP_READ_FIXED: |
3a6820f2 | 6328 | case IORING_OP_READ: |
889fca73 | 6329 | ret = io_read(req, issue_flags); |
edafccee | 6330 | break; |
3529d8c2 | 6331 | case IORING_OP_WRITEV: |
edafccee | 6332 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 6333 | case IORING_OP_WRITE: |
889fca73 | 6334 | ret = io_write(req, issue_flags); |
2b188cc1 | 6335 | break; |
c992fe29 | 6336 | case IORING_OP_FSYNC: |
45d189c6 | 6337 | ret = io_fsync(req, issue_flags); |
c992fe29 | 6338 | break; |
221c5eb2 | 6339 | case IORING_OP_POLL_ADD: |
61e98203 | 6340 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
6341 | break; |
6342 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 6343 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 6344 | break; |
5d17b4a4 | 6345 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 6346 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 6347 | break; |
0fa03c62 | 6348 | case IORING_OP_SENDMSG: |
889fca73 | 6349 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 6350 | break; |
fddaface | 6351 | case IORING_OP_SEND: |
889fca73 | 6352 | ret = io_send(req, issue_flags); |
0fa03c62 | 6353 | break; |
aa1fa28f | 6354 | case IORING_OP_RECVMSG: |
889fca73 | 6355 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 6356 | break; |
fddaface | 6357 | case IORING_OP_RECV: |
889fca73 | 6358 | ret = io_recv(req, issue_flags); |
aa1fa28f | 6359 | break; |
5262f567 | 6360 | case IORING_OP_TIMEOUT: |
61e98203 | 6361 | ret = io_timeout(req, issue_flags); |
5262f567 | 6362 | break; |
11365043 | 6363 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 6364 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 6365 | break; |
17f2fe35 | 6366 | case IORING_OP_ACCEPT: |
889fca73 | 6367 | ret = io_accept(req, issue_flags); |
17f2fe35 | 6368 | break; |
f8e85cf2 | 6369 | case IORING_OP_CONNECT: |
889fca73 | 6370 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 6371 | break; |
62755e35 | 6372 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 6373 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 6374 | break; |
d63d1b5e | 6375 | case IORING_OP_FALLOCATE: |
45d189c6 | 6376 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 6377 | break; |
15b71abe | 6378 | case IORING_OP_OPENAT: |
45d189c6 | 6379 | ret = io_openat(req, issue_flags); |
15b71abe | 6380 | break; |
b5dba59e | 6381 | case IORING_OP_CLOSE: |
889fca73 | 6382 | ret = io_close(req, issue_flags); |
b5dba59e | 6383 | break; |
05f3fb3c | 6384 | case IORING_OP_FILES_UPDATE: |
889fca73 | 6385 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 6386 | break; |
eddc7ef5 | 6387 | case IORING_OP_STATX: |
45d189c6 | 6388 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 6389 | break; |
4840e418 | 6390 | case IORING_OP_FADVISE: |
45d189c6 | 6391 | ret = io_fadvise(req, issue_flags); |
4840e418 | 6392 | break; |
c1ca757b | 6393 | case IORING_OP_MADVISE: |
45d189c6 | 6394 | ret = io_madvise(req, issue_flags); |
c1ca757b | 6395 | break; |
cebdb986 | 6396 | case IORING_OP_OPENAT2: |
45d189c6 | 6397 | ret = io_openat2(req, issue_flags); |
cebdb986 | 6398 | break; |
3e4827b0 | 6399 | case IORING_OP_EPOLL_CTL: |
889fca73 | 6400 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 6401 | break; |
7d67af2c | 6402 | case IORING_OP_SPLICE: |
45d189c6 | 6403 | ret = io_splice(req, issue_flags); |
7d67af2c | 6404 | break; |
ddf0322d | 6405 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 6406 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 6407 | break; |
067524e9 | 6408 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 6409 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 6410 | break; |
f2a8d5c7 | 6411 | case IORING_OP_TEE: |
45d189c6 | 6412 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 6413 | break; |
36f4fa68 | 6414 | case IORING_OP_SHUTDOWN: |
45d189c6 | 6415 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 6416 | break; |
80a261fd | 6417 | case IORING_OP_RENAMEAT: |
45d189c6 | 6418 | ret = io_renameat(req, issue_flags); |
80a261fd | 6419 | break; |
14a1143b | 6420 | case IORING_OP_UNLINKAT: |
45d189c6 | 6421 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 6422 | break; |
2b188cc1 JA |
6423 | default: |
6424 | ret = -EINVAL; | |
6425 | break; | |
6426 | } | |
6427 | ||
5730b27e JA |
6428 | if (creds) |
6429 | revert_creds(creds); | |
def596e9 JA |
6430 | if (ret) |
6431 | return ret; | |
b532576e | 6432 | /* If the op doesn't have a file, we're not polling for it */ |
cb3d8972 PB |
6433 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
6434 | io_iopoll_req_issued(req); | |
def596e9 JA |
6435 | |
6436 | return 0; | |
2b188cc1 JA |
6437 | } |
6438 | ||
ebc11b6c PB |
6439 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
6440 | { | |
6441 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6442 | ||
6443 | req = io_put_req_find_next(req); | |
6444 | return req ? &req->work : NULL; | |
6445 | } | |
6446 | ||
5280f7e5 | 6447 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
6448 | { |
6449 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
6df1db6b | 6450 | struct io_kiocb *timeout; |
561fb04a | 6451 | int ret = 0; |
2b188cc1 | 6452 | |
48dcd38d PB |
6453 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
6454 | if (!(req->flags & REQ_F_REFCOUNT)) | |
6455 | __io_req_set_refcount(req, 2); | |
6456 | else | |
6457 | req_ref_get(req); | |
5d5901a3 | 6458 | |
6df1db6b PB |
6459 | timeout = io_prep_linked_timeout(req); |
6460 | if (timeout) | |
6461 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 6462 | |
dadebc35 | 6463 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
4014d943 | 6464 | if (work->flags & IO_WQ_WORK_CANCEL) |
561fb04a | 6465 | ret = -ECANCELED; |
31b51510 | 6466 | |
561fb04a | 6467 | if (!ret) { |
561fb04a | 6468 | do { |
889fca73 | 6469 | ret = io_issue_sqe(req, 0); |
561fb04a JA |
6470 | /* |
6471 | * We can get EAGAIN for polled IO even though we're | |
6472 | * forcing a sync submission from here, since we can't | |
6473 | * wait for request slots on the block side. | |
6474 | */ | |
6475 | if (ret != -EAGAIN) | |
6476 | break; | |
6477 | cond_resched(); | |
6478 | } while (1); | |
6479 | } | |
31b51510 | 6480 | |
a3df7698 | 6481 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 6482 | if (ret) |
a3df7698 | 6483 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
6484 | } |
6485 | ||
aeca241b | 6486 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 6487 | unsigned i) |
65e19f54 | 6488 | { |
042b0d85 | 6489 | return &table->files[i]; |
dafecf19 PB |
6490 | } |
6491 | ||
65e19f54 JA |
6492 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
6493 | int index) | |
6494 | { | |
aeca241b | 6495 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 6496 | |
a04b0ac0 | 6497 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
6498 | } |
6499 | ||
a04b0ac0 | 6500 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
6501 | { |
6502 | unsigned long file_ptr = (unsigned long) file; | |
6503 | ||
b191e2df | 6504 | if (__io_file_supports_nowait(file, READ)) |
9a321c98 | 6505 | file_ptr |= FFS_ASYNC_READ; |
b191e2df | 6506 | if (__io_file_supports_nowait(file, WRITE)) |
9a321c98 PB |
6507 | file_ptr |= FFS_ASYNC_WRITE; |
6508 | if (S_ISREG(file_inode(file)->i_mode)) | |
6509 | file_ptr |= FFS_ISREG; | |
a04b0ac0 | 6510 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
6511 | } |
6512 | ||
ac177053 PB |
6513 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
6514 | struct io_kiocb *req, int fd) | |
09bb8394 | 6515 | { |
8da11c19 | 6516 | struct file *file; |
ac177053 | 6517 | unsigned long file_ptr; |
09bb8394 | 6518 | |
ac177053 PB |
6519 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
6520 | return NULL; | |
6521 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
6522 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
6523 | file = (struct file *) (file_ptr & FFS_MASK); | |
6524 | file_ptr &= ~FFS_MASK; | |
6525 | /* mask in overlapping REQ_F and FFS bits */ | |
b191e2df | 6526 | req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT); |
ac177053 PB |
6527 | io_req_set_rsrc_node(req); |
6528 | return file; | |
6529 | } | |
d44f554e | 6530 | |
ac177053 | 6531 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
6532 | struct io_kiocb *req, int fd) |
6533 | { | |
62906e89 | 6534 | struct file *file = fget(fd); |
ac177053 PB |
6535 | |
6536 | trace_io_uring_file_get(ctx, fd); | |
09bb8394 | 6537 | |
ac177053 PB |
6538 | /* we don't allow fixed io_uring files */ |
6539 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
6540 | io_req_track_inflight(req); | |
8371adf5 | 6541 | return file; |
09bb8394 JA |
6542 | } |
6543 | ||
ac177053 | 6544 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
6545 | struct io_kiocb *req, int fd, bool fixed) |
6546 | { | |
6547 | if (fixed) | |
6548 | return io_file_get_fixed(ctx, req, fd); | |
6549 | else | |
62906e89 | 6550 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
6551 | } |
6552 | ||
f237c30a | 6553 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
6554 | { |
6555 | struct io_kiocb *prev = req->timeout.prev; | |
8cb01fac | 6556 | int ret; |
89b263f6 JA |
6557 | |
6558 | if (prev) { | |
8cb01fac | 6559 | ret = io_try_cancel_userdata(req, prev->user_data); |
505657bc | 6560 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 6561 | io_put_req(prev); |
89b263f6 JA |
6562 | } else { |
6563 | io_req_complete_post(req, -ETIME, 0); | |
6564 | } | |
6565 | } | |
6566 | ||
2665abfd | 6567 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 6568 | { |
ad8a48ac JA |
6569 | struct io_timeout_data *data = container_of(timer, |
6570 | struct io_timeout_data, timer); | |
90cd7e42 | 6571 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 6572 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 6573 | unsigned long flags; |
2665abfd | 6574 | |
89b263f6 | 6575 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
6576 | prev = req->timeout.head; |
6577 | req->timeout.head = NULL; | |
2665abfd JA |
6578 | |
6579 | /* | |
6580 | * We don't expect the list to be empty, that will only happen if we | |
6581 | * race with the completion of the linked work. | |
6582 | */ | |
447c19f3 | 6583 | if (prev) { |
f2f87370 | 6584 | io_remove_next_linked(prev); |
447c19f3 PB |
6585 | if (!req_ref_inc_not_zero(prev)) |
6586 | prev = NULL; | |
6587 | } | |
89b263f6 JA |
6588 | req->timeout.prev = prev; |
6589 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 6590 | |
89b263f6 JA |
6591 | req->io_task_work.func = io_req_task_link_timeout; |
6592 | io_req_task_work_add(req); | |
2665abfd JA |
6593 | return HRTIMER_NORESTART; |
6594 | } | |
6595 | ||
de968c18 | 6596 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 6597 | { |
de968c18 PB |
6598 | struct io_ring_ctx *ctx = req->ctx; |
6599 | ||
89b263f6 | 6600 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 6601 | /* |
f2f87370 PB |
6602 | * If the back reference is NULL, then our linked request finished |
6603 | * before we got a chance to setup the timer | |
76a46e06 | 6604 | */ |
90cd7e42 | 6605 | if (req->timeout.head) { |
e8c2bc1f | 6606 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 6607 | |
ad8a48ac JA |
6608 | data->timer.function = io_link_timeout_fn; |
6609 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
6610 | data->mode); | |
2665abfd | 6611 | } |
89b263f6 | 6612 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 6613 | /* drop submission reference */ |
76a46e06 JA |
6614 | io_put_req(req); |
6615 | } | |
2665abfd | 6616 | |
c5eef2b9 | 6617 | static void __io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6618 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 6619 | { |
906c6caa | 6620 | struct io_kiocb *linked_timeout; |
e0c5c576 | 6621 | int ret; |
2b188cc1 | 6622 | |
59b735ae | 6623 | issue_sqe: |
c5eef2b9 | 6624 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 6625 | |
491381ce JA |
6626 | /* |
6627 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
6628 | * doesn't support non-blocking read/write attempts | |
6629 | */ | |
1840038e | 6630 | if (likely(!ret)) { |
e342c807 | 6631 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
c5eef2b9 | 6632 | struct io_ring_ctx *ctx = req->ctx; |
cd0ca2e0 | 6633 | struct io_submit_state *state = &ctx->submit_state; |
e65ef56d | 6634 | |
cd0ca2e0 PB |
6635 | state->compl_reqs[state->compl_nr++] = req; |
6636 | if (state->compl_nr == ARRAY_SIZE(state->compl_reqs)) | |
2a2758f2 | 6637 | io_submit_flush_completions(ctx); |
906c6caa | 6638 | return; |
0d63c148 | 6639 | } |
906c6caa PB |
6640 | |
6641 | linked_timeout = io_prep_linked_timeout(req); | |
6642 | if (linked_timeout) | |
6643 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 6644 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
906c6caa PB |
6645 | linked_timeout = io_prep_linked_timeout(req); |
6646 | ||
59b735ae OL |
6647 | switch (io_arm_poll_handler(req)) { |
6648 | case IO_APOLL_READY: | |
906c6caa PB |
6649 | if (linked_timeout) |
6650 | io_unprep_linked_timeout(req); | |
59b735ae OL |
6651 | goto issue_sqe; |
6652 | case IO_APOLL_ABORTED: | |
1840038e PB |
6653 | /* |
6654 | * Queued up for async execution, worker will release | |
6655 | * submit reference when the iocb is actually submitted. | |
6656 | */ | |
f237c30a | 6657 | io_queue_async_work(req, NULL); |
59b735ae | 6658 | break; |
1840038e | 6659 | } |
906c6caa PB |
6660 | |
6661 | if (linked_timeout) | |
6662 | io_queue_linked_timeout(linked_timeout); | |
0d63c148 | 6663 | } else { |
f41db273 | 6664 | io_req_complete_failed(req, ret); |
9e645e11 | 6665 | } |
2b188cc1 JA |
6666 | } |
6667 | ||
441b8a78 | 6668 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 6669 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 6670 | { |
10c66904 | 6671 | if (unlikely(req->ctx->drain_active) && io_drain_req(req)) |
76cc33d7 | 6672 | return; |
4fe2c963 | 6673 | |
a8295b98 | 6674 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) { |
c5eef2b9 | 6675 | __io_queue_sqe(req); |
a8295b98 HX |
6676 | } else if (req->flags & REQ_F_FAIL) { |
6677 | io_req_complete_failed(req, req->result); | |
76cc33d7 PB |
6678 | } else { |
6679 | int ret = io_req_prep_async(req); | |
6680 | ||
6681 | if (unlikely(ret)) | |
6682 | io_req_complete_failed(req, ret); | |
6683 | else | |
f237c30a | 6684 | io_queue_async_work(req, NULL); |
ce35a47a | 6685 | } |
4fe2c963 JL |
6686 | } |
6687 | ||
b16fed66 PB |
6688 | /* |
6689 | * Check SQE restrictions (opcode and flags). | |
6690 | * | |
6691 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
6692 | */ | |
6693 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
6694 | struct io_kiocb *req, | |
6695 | unsigned int sqe_flags) | |
4fe2c963 | 6696 | { |
4cfb25bf | 6697 | if (likely(!ctx->restricted)) |
b16fed66 PB |
6698 | return true; |
6699 | ||
6700 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) | |
6701 | return false; | |
6702 | ||
6703 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
6704 | ctx->restrictions.sqe_flags_required) | |
6705 | return false; | |
6706 | ||
6707 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
6708 | ctx->restrictions.sqe_flags_required)) | |
6709 | return false; | |
6710 | ||
6711 | return true; | |
4fe2c963 JL |
6712 | } |
6713 | ||
b16fed66 PB |
6714 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
6715 | const struct io_uring_sqe *sqe) | |
282cdc86 | 6716 | __must_hold(&ctx->uring_lock) |
b16fed66 PB |
6717 | { |
6718 | struct io_submit_state *state; | |
6719 | unsigned int sqe_flags; | |
003e8dcc | 6720 | int personality, ret = 0; |
b16fed66 | 6721 | |
864ea921 | 6722 | /* req is partially pre-initialised, see io_preinit_req() */ |
b16fed66 PB |
6723 | req->opcode = READ_ONCE(sqe->opcode); |
6724 | /* same numerical values with corresponding REQ_F_*, safe to copy */ | |
6725 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
6726 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 6727 | req->file = NULL; |
b16fed66 | 6728 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 6729 | req->task = current; |
b16fed66 PB |
6730 | |
6731 | /* enforce forwards compatibility on users */ | |
dddca226 | 6732 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
b16fed66 | 6733 | return -EINVAL; |
b16fed66 PB |
6734 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
6735 | return -EINVAL; | |
4cfb25bf | 6736 | if (!io_check_restriction(ctx, req, sqe_flags)) |
b16fed66 PB |
6737 | return -EACCES; |
6738 | ||
6739 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
6740 | !io_op_defs[req->opcode].buffer_select) | |
6741 | return -EOPNOTSUPP; | |
3c19966d PB |
6742 | if (unlikely(sqe_flags & IOSQE_IO_DRAIN)) |
6743 | ctx->drain_active = true; | |
863e0560 | 6744 | |
003e8dcc JA |
6745 | personality = READ_ONCE(sqe->personality); |
6746 | if (personality) { | |
c10d1f98 PB |
6747 | req->creds = xa_load(&ctx->personalities, personality); |
6748 | if (!req->creds) | |
003e8dcc | 6749 | return -EINVAL; |
c10d1f98 | 6750 | get_cred(req->creds); |
b8e64b53 | 6751 | req->flags |= REQ_F_CREDS; |
003e8dcc | 6752 | } |
b16fed66 PB |
6753 | state = &ctx->submit_state; |
6754 | ||
6755 | /* | |
6756 | * Plug now if we have more than 1 IO left after this, and the target | |
6757 | * is potentially a read/write to block based storage. | |
6758 | */ | |
6759 | if (!state->plug_started && state->ios_left > 1 && | |
6760 | io_op_defs[req->opcode].plug) { | |
6761 | blk_start_plug(&state->plug); | |
6762 | state->plug_started = true; | |
6763 | } | |
6764 | ||
6765 | if (io_op_defs[req->opcode].needs_file) { | |
62906e89 | 6766 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 6767 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 PB |
6768 | if (unlikely(!req->file)) |
6769 | ret = -EBADF; | |
6770 | } | |
6771 | ||
6772 | state->ios_left--; | |
6773 | return ret; | |
6774 | } | |
6775 | ||
a6b8cadc | 6776 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 6777 | const struct io_uring_sqe *sqe) |
282cdc86 | 6778 | __must_hold(&ctx->uring_lock) |
9e645e11 | 6779 | { |
a1ab7b35 | 6780 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 6781 | int ret; |
9e645e11 | 6782 | |
a6b8cadc PB |
6783 | ret = io_init_req(ctx, req, sqe); |
6784 | if (unlikely(ret)) { | |
6785 | fail_req: | |
a8295b98 | 6786 | /* fail even hard links since we don't submit */ |
de59bc10 | 6787 | if (link->head) { |
a8295b98 HX |
6788 | /* |
6789 | * we can judge a link req is failed or cancelled by if | |
6790 | * REQ_F_FAIL is set, but the head is an exception since | |
6791 | * it may be set REQ_F_FAIL because of other req's failure | |
6792 | * so let's leverage req->result to distinguish if a head | |
6793 | * is set REQ_F_FAIL because of its failure or other req's | |
6794 | * failure so that we can set the correct ret code for it. | |
6795 | * init result here to avoid affecting the normal path. | |
6796 | */ | |
6797 | if (!(link->head->flags & REQ_F_FAIL)) | |
6798 | req_fail_link_node(link->head, -ECANCELED); | |
6799 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
6800 | /* | |
6801 | * the current req is a normal req, we should return | |
6802 | * error and thus break the submittion loop. | |
6803 | */ | |
6804 | io_req_complete_failed(req, ret); | |
6805 | return ret; | |
de59bc10 | 6806 | } |
a8295b98 HX |
6807 | req_fail_link_node(req, ret); |
6808 | } else { | |
6809 | ret = io_req_prep(req, sqe); | |
6810 | if (unlikely(ret)) | |
6811 | goto fail_req; | |
a6b8cadc | 6812 | } |
441b8a78 | 6813 | |
be7053b7 | 6814 | /* don't need @sqe from now on */ |
236daeae OL |
6815 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
6816 | req->flags, true, | |
6817 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 6818 | |
9e645e11 JA |
6819 | /* |
6820 | * If we already have a head request, queue this one for async | |
6821 | * submittal once the head completes. If we don't have a head but | |
6822 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
6823 | * submitted sync once the chain is complete. If none of those | |
6824 | * conditions are true (normal request), then just queue it. | |
6825 | */ | |
863e0560 PB |
6826 | if (link->head) { |
6827 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 6828 | |
a8295b98 HX |
6829 | if (!(req->flags & REQ_F_FAIL)) { |
6830 | ret = io_req_prep_async(req); | |
6831 | if (unlikely(ret)) { | |
6832 | req_fail_link_node(req, ret); | |
6833 | if (!(head->flags & REQ_F_FAIL)) | |
6834 | req_fail_link_node(head, -ECANCELED); | |
6835 | } | |
6836 | } | |
9d76377f | 6837 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 6838 | link->last->link = req; |
863e0560 | 6839 | link->last = req; |
32fe525b PB |
6840 | |
6841 | /* last request of a link, enqueue the link */ | |
ef4ff581 | 6842 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
863e0560 | 6843 | link->head = NULL; |
5e159204 | 6844 | io_queue_sqe(head); |
32fe525b | 6845 | } |
9e645e11 | 6846 | } else { |
ef4ff581 | 6847 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
863e0560 PB |
6848 | link->head = req; |
6849 | link->last = req; | |
711be031 | 6850 | } else { |
be7053b7 | 6851 | io_queue_sqe(req); |
711be031 | 6852 | } |
9e645e11 | 6853 | } |
2e6e1fde | 6854 | |
1d4240cc | 6855 | return 0; |
9e645e11 JA |
6856 | } |
6857 | ||
9a56a232 JA |
6858 | /* |
6859 | * Batched submission is done, ensure local IO is flushed out. | |
6860 | */ | |
ba88ff11 PB |
6861 | static void io_submit_state_end(struct io_submit_state *state, |
6862 | struct io_ring_ctx *ctx) | |
9a56a232 | 6863 | { |
a1ab7b35 | 6864 | if (state->link.head) |
de59bc10 | 6865 | io_queue_sqe(state->link.head); |
cd0ca2e0 | 6866 | if (state->compl_nr) |
2a2758f2 | 6867 | io_submit_flush_completions(ctx); |
27926b68 JA |
6868 | if (state->plug_started) |
6869 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
6870 | } |
6871 | ||
6872 | /* | |
6873 | * Start submission side cache. | |
6874 | */ | |
6875 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 6876 | unsigned int max_ios) |
9a56a232 | 6877 | { |
27926b68 | 6878 | state->plug_started = false; |
9a56a232 | 6879 | state->ios_left = max_ios; |
a1ab7b35 PB |
6880 | /* set only head, no need to init link_last in advance */ |
6881 | state->link.head = NULL; | |
9a56a232 JA |
6882 | } |
6883 | ||
2b188cc1 JA |
6884 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
6885 | { | |
75b28aff | 6886 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 6887 | |
caf582c6 PB |
6888 | /* |
6889 | * Ensure any loads from the SQEs are done at this point, | |
6890 | * since once we write the new head, the application could | |
6891 | * write new data to them. | |
6892 | */ | |
6893 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
6894 | } |
6895 | ||
2b188cc1 | 6896 | /* |
dd9ae8a0 | 6897 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
6898 | * that is mapped by userspace. This means that care needs to be taken to |
6899 | * ensure that reads are stable, as we cannot rely on userspace always | |
6900 | * being a good citizen. If members of the sqe are validated and then later | |
6901 | * used, it's important that those reads are done through READ_ONCE() to | |
6902 | * prevent a re-load down the line. | |
6903 | */ | |
709b302f | 6904 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 6905 | { |
ea5ab3b5 | 6906 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 6907 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
6908 | |
6909 | /* | |
6910 | * The cached sq head (or cq tail) serves two purposes: | |
6911 | * | |
6912 | * 1) allows us to batch the cost of updating the user visible | |
6913 | * head updates. | |
6914 | * 2) allows the kernel side to track the head on its own, even | |
6915 | * though the application is the one updating it. | |
6916 | */ | |
17d3aeb3 | 6917 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
6918 | if (likely(head < ctx->sq_entries)) |
6919 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
6920 | |
6921 | /* drop invalid entries */ | |
15641e42 PB |
6922 | ctx->cq_extra--; |
6923 | WRITE_ONCE(ctx->rings->sq_dropped, | |
6924 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
6925 | return NULL; |
6926 | } | |
6927 | ||
0f212204 | 6928 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 6929 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 6930 | { |
46c4e16a | 6931 | int submitted = 0; |
6c271ce2 | 6932 | |
ee7d46d9 PB |
6933 | /* make sure SQ entry isn't read before tail */ |
6934 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
2b85edfc PB |
6935 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
6936 | return -EAGAIN; | |
9a10867a | 6937 | io_get_task_refs(nr); |
6c271ce2 | 6938 | |
ba88ff11 | 6939 | io_submit_state_start(&ctx->submit_state, nr); |
46c4e16a | 6940 | while (submitted < nr) { |
3529d8c2 | 6941 | const struct io_uring_sqe *sqe; |
196be95c | 6942 | struct io_kiocb *req; |
fb5ccc98 | 6943 | |
258b29a9 | 6944 | req = io_alloc_req(ctx); |
196be95c PB |
6945 | if (unlikely(!req)) { |
6946 | if (!submitted) | |
6947 | submitted = -EAGAIN; | |
fb5ccc98 | 6948 | break; |
196be95c | 6949 | } |
4fccfcbb PB |
6950 | sqe = io_get_sqe(ctx); |
6951 | if (unlikely(!sqe)) { | |
0c6e1d7f | 6952 | list_add(&req->inflight_entry, &ctx->submit_state.free_list); |
4fccfcbb PB |
6953 | break; |
6954 | } | |
d3656344 JA |
6955 | /* will complete beyond this point, count as submitted */ |
6956 | submitted++; | |
a1ab7b35 | 6957 | if (io_submit_sqe(ctx, req, sqe)) |
196be95c | 6958 | break; |
6c271ce2 JA |
6959 | } |
6960 | ||
9466f437 PB |
6961 | if (unlikely(submitted != nr)) { |
6962 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 6963 | int unused = nr - ref_used; |
9466f437 | 6964 | |
09899b19 | 6965 | current->io_uring->cached_refs += unused; |
d8a6df10 | 6966 | percpu_ref_put_many(&ctx->refs, unused); |
9466f437 | 6967 | } |
6c271ce2 | 6968 | |
a1ab7b35 | 6969 | io_submit_state_end(&ctx->submit_state, ctx); |
ae9428ca PB |
6970 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
6971 | io_commit_sqring(ctx); | |
6972 | ||
6c271ce2 JA |
6973 | return submitted; |
6974 | } | |
6975 | ||
e4b6d902 PB |
6976 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
6977 | { | |
6978 | return READ_ONCE(sqd->state); | |
6979 | } | |
6980 | ||
23b3628e XW |
6981 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
6982 | { | |
6983 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 6984 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6985 | WRITE_ONCE(ctx->rings->sq_flags, |
6986 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6987 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6988 | } |
6989 | ||
6990 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
6991 | { | |
79ebeaee | 6992 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
6993 | WRITE_ONCE(ctx->rings->sq_flags, |
6994 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 6995 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
6996 | } |
6997 | ||
08369246 | 6998 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 6999 | { |
c8d1ba58 | 7000 | unsigned int to_submit; |
bdcd3eab | 7001 | int ret = 0; |
6c271ce2 | 7002 | |
c8d1ba58 | 7003 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 7004 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
7005 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
7006 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 7007 | |
906a3c6f | 7008 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
c8d1ba58 | 7009 | unsigned nr_events = 0; |
948e1947 PB |
7010 | const struct cred *creds = NULL; |
7011 | ||
7012 | if (ctx->sq_creds != current_cred()) | |
7013 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 7014 | |
c8d1ba58 | 7015 | mutex_lock(&ctx->uring_lock); |
906a3c6f | 7016 | if (!list_empty(&ctx->iopoll_list)) |
a8576af9 | 7017 | io_do_iopoll(ctx, &nr_events, 0); |
906a3c6f | 7018 | |
3b763ba1 PB |
7019 | /* |
7020 | * Don't submit if refs are dying, good for io_uring_register(), | |
7021 | * but also it is relied upon by io_ring_exit_work() | |
7022 | */ | |
0298ef96 PB |
7023 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
7024 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 7025 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 7026 | mutex_unlock(&ctx->uring_lock); |
6c271ce2 | 7027 | |
acfb381d PB |
7028 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
7029 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
7030 | if (creds) |
7031 | revert_creds(creds); | |
acfb381d | 7032 | } |
6c271ce2 | 7033 | |
08369246 XW |
7034 | return ret; |
7035 | } | |
6c271ce2 | 7036 | |
08369246 XW |
7037 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
7038 | { | |
7039 | struct io_ring_ctx *ctx; | |
7040 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 7041 | |
c9dca27d PB |
7042 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7043 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 7044 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 7045 | } |
6c271ce2 | 7046 | |
e4b6d902 PB |
7047 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
7048 | { | |
7049 | bool did_sig = false; | |
7050 | struct ksignal ksig; | |
7051 | ||
7052 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
7053 | signal_pending(current)) { | |
7054 | mutex_unlock(&sqd->lock); | |
7055 | if (signal_pending(current)) | |
7056 | did_sig = get_signal(&ksig); | |
7057 | cond_resched(); | |
7058 | mutex_lock(&sqd->lock); | |
7059 | } | |
e4b6d902 PB |
7060 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
7061 | } | |
7062 | ||
c8d1ba58 JA |
7063 | static int io_sq_thread(void *data) |
7064 | { | |
69fb2131 JA |
7065 | struct io_sq_data *sqd = data; |
7066 | struct io_ring_ctx *ctx; | |
a0d9205f | 7067 | unsigned long timeout = 0; |
37d1e2e3 | 7068 | char buf[TASK_COMM_LEN]; |
08369246 | 7069 | DEFINE_WAIT(wait); |
6c271ce2 | 7070 | |
696ee88a | 7071 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 7072 | set_task_comm(current, buf); |
37d1e2e3 JA |
7073 | |
7074 | if (sqd->sq_cpu != -1) | |
7075 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
7076 | else | |
7077 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
7078 | current->flags |= PF_NO_SETAFFINITY; | |
7079 | ||
09a6f4ef | 7080 | mutex_lock(&sqd->lock); |
e4b6d902 | 7081 | while (1) { |
1a924a80 | 7082 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 7083 | |
e4b6d902 PB |
7084 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
7085 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 7086 | break; |
08369246 XW |
7087 | timeout = jiffies + sqd->sq_thread_idle; |
7088 | } | |
e4b6d902 | 7089 | |
e95eee2d | 7090 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 7091 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 7092 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 7093 | |
08369246 XW |
7094 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
7095 | sqt_spin = true; | |
69fb2131 | 7096 | } |
dd432ea5 PB |
7097 | if (io_run_task_work()) |
7098 | sqt_spin = true; | |
6c271ce2 | 7099 | |
08369246 | 7100 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 7101 | cond_resched(); |
08369246 XW |
7102 | if (sqt_spin) |
7103 | timeout = jiffies + sqd->sq_thread_idle; | |
7104 | continue; | |
7105 | } | |
7106 | ||
08369246 | 7107 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
dd432ea5 | 7108 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
1a924a80 PB |
7109 | bool needs_sched = true; |
7110 | ||
724cb4f9 | 7111 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
7112 | io_ring_set_wakeup_flag(ctx); |
7113 | ||
724cb4f9 HX |
7114 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
7115 | !list_empty_careful(&ctx->iopoll_list)) { | |
7116 | needs_sched = false; | |
7117 | break; | |
7118 | } | |
7119 | if (io_sqring_entries(ctx)) { | |
7120 | needs_sched = false; | |
7121 | break; | |
7122 | } | |
7123 | } | |
7124 | ||
7125 | if (needs_sched) { | |
7126 | mutex_unlock(&sqd->lock); | |
7127 | schedule(); | |
7128 | mutex_lock(&sqd->lock); | |
7129 | } | |
69fb2131 JA |
7130 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
7131 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 7132 | } |
08369246 XW |
7133 | |
7134 | finish_wait(&sqd->wait, &wait); | |
7135 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 7136 | } |
28cea78a | 7137 | |
78cc687b | 7138 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 7139 | sqd->thread = NULL; |
05962f95 | 7140 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 7141 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 7142 | io_run_task_work(); |
734551df PB |
7143 | mutex_unlock(&sqd->lock); |
7144 | ||
37d1e2e3 JA |
7145 | complete(&sqd->exited); |
7146 | do_exit(0); | |
6c271ce2 JA |
7147 | } |
7148 | ||
bda52162 JA |
7149 | struct io_wait_queue { |
7150 | struct wait_queue_entry wq; | |
7151 | struct io_ring_ctx *ctx; | |
5fd46178 | 7152 | unsigned cq_tail; |
bda52162 JA |
7153 | unsigned nr_timeouts; |
7154 | }; | |
7155 | ||
6c503150 | 7156 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
7157 | { |
7158 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 7159 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
7160 | |
7161 | /* | |
d195a66e | 7162 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
7163 | * started waiting. For timeouts, we always want to return to userspace, |
7164 | * regardless of event count. | |
7165 | */ | |
5fd46178 | 7166 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
7167 | } |
7168 | ||
7169 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
7170 | int wake_flags, void *key) | |
7171 | { | |
7172 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
7173 | wq); | |
7174 | ||
6c503150 PB |
7175 | /* |
7176 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
7177 | * the task, and the next invocation will do it. | |
7178 | */ | |
5ed7a37d | 7179 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
7180 | return autoremove_wake_function(curr, mode, wake_flags, key); |
7181 | return -1; | |
bda52162 JA |
7182 | } |
7183 | ||
af9c1a44 JA |
7184 | static int io_run_task_work_sig(void) |
7185 | { | |
7186 | if (io_run_task_work()) | |
7187 | return 1; | |
7188 | if (!signal_pending(current)) | |
7189 | return 0; | |
0b8cfa97 | 7190 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 7191 | return -ERESTARTSYS; |
af9c1a44 JA |
7192 | return -EINTR; |
7193 | } | |
7194 | ||
eeb60b9a PB |
7195 | /* when returns >0, the caller should retry */ |
7196 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
7197 | struct io_wait_queue *iowq, | |
7198 | signed long *timeout) | |
7199 | { | |
7200 | int ret; | |
7201 | ||
7202 | /* make sure we run task_work before checking for signals */ | |
7203 | ret = io_run_task_work_sig(); | |
7204 | if (ret || io_should_wake(iowq)) | |
7205 | return ret; | |
7206 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 7207 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
7208 | return 1; |
7209 | ||
7210 | *timeout = schedule_timeout(*timeout); | |
7211 | return !*timeout ? -ETIME : 1; | |
7212 | } | |
7213 | ||
2b188cc1 JA |
7214 | /* |
7215 | * Wait until events become available, if we don't already have some. The | |
7216 | * application must reap them itself, as they reside on the shared cq ring. | |
7217 | */ | |
7218 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
7219 | const sigset_t __user *sig, size_t sigsz, |
7220 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 7221 | { |
90291099 | 7222 | struct io_wait_queue iowq; |
75b28aff | 7223 | struct io_rings *rings = ctx->rings; |
c1d5a224 PB |
7224 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
7225 | int ret; | |
2b188cc1 | 7226 | |
b41e9852 | 7227 | do { |
90f67366 | 7228 | io_cqring_overflow_flush(ctx); |
6c503150 | 7229 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 7230 | return 0; |
4c6e277c | 7231 | if (!io_run_task_work()) |
b41e9852 | 7232 | break; |
b41e9852 | 7233 | } while (1); |
2b188cc1 JA |
7234 | |
7235 | if (sig) { | |
9e75ad5d AB |
7236 | #ifdef CONFIG_COMPAT |
7237 | if (in_compat_syscall()) | |
7238 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 7239 | sigsz); |
9e75ad5d AB |
7240 | else |
7241 | #endif | |
b772434b | 7242 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 7243 | |
2b188cc1 JA |
7244 | if (ret) |
7245 | return ret; | |
7246 | } | |
7247 | ||
c73ebb68 | 7248 | if (uts) { |
c1d5a224 PB |
7249 | struct timespec64 ts; |
7250 | ||
c73ebb68 HX |
7251 | if (get_timespec64(&ts, uts)) |
7252 | return -EFAULT; | |
7253 | timeout = timespec64_to_jiffies(&ts); | |
7254 | } | |
7255 | ||
90291099 PB |
7256 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
7257 | iowq.wq.private = current; | |
7258 | INIT_LIST_HEAD(&iowq.wq.entry); | |
7259 | iowq.ctx = ctx; | |
bda52162 | 7260 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 7261 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 7262 | |
c826bd7a | 7263 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 7264 | do { |
ca0a2651 | 7265 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 7266 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
7267 | ret = -EBUSY; |
7268 | break; | |
7269 | } | |
311997b3 | 7270 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 7271 | TASK_INTERRUPTIBLE); |
eeb60b9a | 7272 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
311997b3 | 7273 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 7274 | cond_resched(); |
eeb60b9a | 7275 | } while (ret > 0); |
bda52162 | 7276 | |
b7db41c9 | 7277 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 7278 | |
75b28aff | 7279 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
7280 | } |
7281 | ||
9123c8ff | 7282 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 7283 | { |
9123c8ff | 7284 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 7285 | |
846a4ef2 | 7286 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
7287 | kfree(table[i]); |
7288 | kfree(table); | |
7289 | } | |
7290 | ||
7291 | static void **io_alloc_page_table(size_t size) | |
7292 | { | |
7293 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
7294 | size_t init_size = size; | |
7295 | void **table; | |
7296 | ||
0bea96f5 | 7297 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7298 | if (!table) |
7299 | return NULL; | |
7300 | ||
7301 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 7302 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 7303 | |
0bea96f5 | 7304 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
7305 | if (!table[i]) { |
7306 | io_free_page_table(table, init_size); | |
7307 | return NULL; | |
7308 | } | |
7309 | size -= this_size; | |
7310 | } | |
7311 | return table; | |
05f3fb3c JA |
7312 | } |
7313 | ||
28a9fe25 | 7314 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 7315 | { |
28a9fe25 PB |
7316 | percpu_ref_exit(&ref_node->refs); |
7317 | kfree(ref_node); | |
1642b445 PB |
7318 | } |
7319 | ||
b9bd2bea PB |
7320 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
7321 | { | |
7322 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
7323 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
7324 | unsigned long flags; | |
7325 | bool first_add = false; | |
7326 | ||
7327 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
7328 | node->done = true; | |
7329 | ||
7330 | while (!list_empty(&ctx->rsrc_ref_list)) { | |
7331 | node = list_first_entry(&ctx->rsrc_ref_list, | |
7332 | struct io_rsrc_node, node); | |
7333 | /* recycle ref nodes in order */ | |
7334 | if (!node->done) | |
7335 | break; | |
7336 | list_del(&node->node); | |
7337 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
7338 | } | |
7339 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
7340 | ||
7341 | if (first_add) | |
7342 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); | |
7343 | } | |
7344 | ||
7345 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) | |
7346 | { | |
7347 | struct io_rsrc_node *ref_node; | |
7348 | ||
7349 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
7350 | if (!ref_node) | |
7351 | return NULL; | |
7352 | ||
7353 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
7354 | 0, GFP_KERNEL)) { | |
7355 | kfree(ref_node); | |
7356 | return NULL; | |
7357 | } | |
7358 | INIT_LIST_HEAD(&ref_node->node); | |
7359 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
7360 | ref_node->done = false; | |
7361 | return ref_node; | |
7362 | } | |
7363 | ||
a7f0ed5a PB |
7364 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
7365 | struct io_rsrc_data *data_to_kill) | |
6b06314c | 7366 | { |
a7f0ed5a PB |
7367 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
7368 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 7369 | |
a7f0ed5a PB |
7370 | if (data_to_kill) { |
7371 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 7372 | |
a7f0ed5a | 7373 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 7374 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 7375 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 7376 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 7377 | |
3e942498 | 7378 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
7379 | percpu_ref_kill(&rsrc_node->refs); |
7380 | ctx->rsrc_node = NULL; | |
7381 | } | |
6b06314c | 7382 | |
a7f0ed5a PB |
7383 | if (!ctx->rsrc_node) { |
7384 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
7385 | ctx->rsrc_backup_node = NULL; | |
7386 | } | |
8bad28d8 HX |
7387 | } |
7388 | ||
a7f0ed5a | 7389 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
7390 | { |
7391 | if (ctx->rsrc_backup_node) | |
7392 | return 0; | |
b895c9a6 | 7393 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
8dd03afe | 7394 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
7395 | } |
7396 | ||
40ae0ff7 | 7397 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
8bad28d8 HX |
7398 | { |
7399 | int ret; | |
05589553 | 7400 | |
215c3902 | 7401 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
7402 | if (data->quiesce) |
7403 | return -ENXIO; | |
05589553 | 7404 | |
8bad28d8 | 7405 | data->quiesce = true; |
1ffc5422 | 7406 | do { |
a7f0ed5a | 7407 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 7408 | if (ret) |
f2303b1f | 7409 | break; |
a7f0ed5a | 7410 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 7411 | |
3e942498 PB |
7412 | /* kill initial ref, already quiesced if zero */ |
7413 | if (atomic_dec_and_test(&data->refs)) | |
7414 | break; | |
c018db4a | 7415 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 7416 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 7417 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
7418 | if (!ret) { |
7419 | mutex_lock(&ctx->uring_lock); | |
1ffc5422 | 7420 | break; |
c018db4a | 7421 | } |
8bad28d8 | 7422 | |
3e942498 PB |
7423 | atomic_inc(&data->refs); |
7424 | /* wait for all works potentially completing data->done */ | |
7425 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 7426 | reinit_completion(&data->done); |
8dd03afe | 7427 | |
1ffc5422 | 7428 | ret = io_run_task_work_sig(); |
8bad28d8 | 7429 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 7430 | } while (ret >= 0); |
8bad28d8 | 7431 | data->quiesce = false; |
05f3fb3c | 7432 | |
8bad28d8 | 7433 | return ret; |
d7954b2b BM |
7434 | } |
7435 | ||
2d091d62 PB |
7436 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
7437 | { | |
7438 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
7439 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
7440 | ||
7441 | return &data->tags[table_idx][off]; | |
7442 | } | |
7443 | ||
44b31f2f | 7444 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 7445 | { |
2d091d62 PB |
7446 | size_t size = data->nr * sizeof(data->tags[0][0]); |
7447 | ||
7448 | if (data->tags) | |
7449 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
7450 | kfree(data); |
7451 | } | |
7452 | ||
d878c816 PB |
7453 | static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
7454 | u64 __user *utags, unsigned nr, | |
7455 | struct io_rsrc_data **pdata) | |
1ad555c6 | 7456 | { |
b895c9a6 | 7457 | struct io_rsrc_data *data; |
2d091d62 | 7458 | int ret = -ENOMEM; |
d878c816 | 7459 | unsigned i; |
1ad555c6 BM |
7460 | |
7461 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
7462 | if (!data) | |
d878c816 | 7463 | return -ENOMEM; |
2d091d62 | 7464 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 7465 | if (!data->tags) { |
1ad555c6 | 7466 | kfree(data); |
d878c816 PB |
7467 | return -ENOMEM; |
7468 | } | |
2d091d62 PB |
7469 | |
7470 | data->nr = nr; | |
7471 | data->ctx = ctx; | |
7472 | data->do_put = do_put; | |
d878c816 | 7473 | if (utags) { |
2d091d62 | 7474 | ret = -EFAULT; |
d878c816 | 7475 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
7476 | u64 *tag_slot = io_get_tag_slot(data, i); |
7477 | ||
7478 | if (copy_from_user(tag_slot, &utags[i], | |
7479 | sizeof(*tag_slot))) | |
2d091d62 | 7480 | goto fail; |
d878c816 | 7481 | } |
1ad555c6 | 7482 | } |
b60c8dce | 7483 | |
3e942498 | 7484 | atomic_set(&data->refs, 1); |
1ad555c6 | 7485 | init_completion(&data->done); |
d878c816 PB |
7486 | *pdata = data; |
7487 | return 0; | |
2d091d62 PB |
7488 | fail: |
7489 | io_rsrc_data_free(data); | |
7490 | return ret; | |
1ad555c6 BM |
7491 | } |
7492 | ||
9123c8ff PB |
7493 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
7494 | { | |
0bea96f5 PB |
7495 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
7496 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
7497 | return !!table->files; |
7498 | } | |
7499 | ||
042b0d85 | 7500 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 7501 | { |
042b0d85 | 7502 | kvfree(table->files); |
9123c8ff PB |
7503 | table->files = NULL; |
7504 | } | |
7505 | ||
fff4db76 | 7506 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 7507 | { |
fff4db76 PB |
7508 | #if defined(CONFIG_UNIX) |
7509 | if (ctx->ring_sock) { | |
7510 | struct sock *sock = ctx->ring_sock->sk; | |
7511 | struct sk_buff *skb; | |
7512 | ||
7513 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
7514 | kfree_skb(skb); | |
7515 | } | |
7516 | #else | |
7517 | int i; | |
7518 | ||
7519 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7520 | struct file *file; | |
7521 | ||
7522 | file = io_file_from_index(ctx, i); | |
7523 | if (file) | |
7524 | fput(file); | |
7525 | } | |
7526 | #endif | |
042b0d85 | 7527 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 7528 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
7529 | ctx->file_data = NULL; |
7530 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
7531 | } |
7532 | ||
d7954b2b BM |
7533 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
7534 | { | |
d7954b2b BM |
7535 | int ret; |
7536 | ||
08480400 | 7537 | if (!ctx->file_data) |
d7954b2b | 7538 | return -ENXIO; |
08480400 PB |
7539 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
7540 | if (!ret) | |
7541 | __io_sqe_files_unregister(ctx); | |
7542 | return ret; | |
6b06314c JA |
7543 | } |
7544 | ||
37d1e2e3 | 7545 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 7546 | __releases(&sqd->lock) |
37d1e2e3 | 7547 | { |
521d6a73 PB |
7548 | WARN_ON_ONCE(sqd->thread == current); |
7549 | ||
9e138a48 PB |
7550 | /* |
7551 | * Do the dance but not conditional clear_bit() because it'd race with | |
7552 | * other threads incrementing park_pending and setting the bit. | |
7553 | */ | |
37d1e2e3 | 7554 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
7555 | if (atomic_dec_return(&sqd->park_pending)) |
7556 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 7557 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
7558 | } |
7559 | ||
86e0d676 | 7560 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 7561 | __acquires(&sqd->lock) |
37d1e2e3 | 7562 | { |
521d6a73 PB |
7563 | WARN_ON_ONCE(sqd->thread == current); |
7564 | ||
9e138a48 | 7565 | atomic_inc(&sqd->park_pending); |
86e0d676 | 7566 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 7567 | mutex_lock(&sqd->lock); |
05962f95 | 7568 | if (sqd->thread) |
86e0d676 | 7569 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
7570 | } |
7571 | ||
7572 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
7573 | { | |
521d6a73 | 7574 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 7575 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 7576 | |
05962f95 | 7577 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 7578 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
7579 | if (sqd->thread) |
7580 | wake_up_process(sqd->thread); | |
09a6f4ef | 7581 | mutex_unlock(&sqd->lock); |
05962f95 | 7582 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
7583 | } |
7584 | ||
534ca6d6 | 7585 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 7586 | { |
534ca6d6 | 7587 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
7588 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
7589 | ||
37d1e2e3 JA |
7590 | io_sq_thread_stop(sqd); |
7591 | kfree(sqd); | |
7592 | } | |
7593 | } | |
7594 | ||
7595 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
7596 | { | |
7597 | struct io_sq_data *sqd = ctx->sq_data; | |
7598 | ||
7599 | if (sqd) { | |
05962f95 | 7600 | io_sq_thread_park(sqd); |
521d6a73 | 7601 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 7602 | io_sqd_update_thread_idle(sqd); |
05962f95 | 7603 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
7604 | |
7605 | io_put_sq_data(sqd); | |
7606 | ctx->sq_data = NULL; | |
534ca6d6 JA |
7607 | } |
7608 | } | |
7609 | ||
aa06165d JA |
7610 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
7611 | { | |
7612 | struct io_ring_ctx *ctx_attach; | |
7613 | struct io_sq_data *sqd; | |
7614 | struct fd f; | |
7615 | ||
7616 | f = fdget(p->wq_fd); | |
7617 | if (!f.file) | |
7618 | return ERR_PTR(-ENXIO); | |
7619 | if (f.file->f_op != &io_uring_fops) { | |
7620 | fdput(f); | |
7621 | return ERR_PTR(-EINVAL); | |
7622 | } | |
7623 | ||
7624 | ctx_attach = f.file->private_data; | |
7625 | sqd = ctx_attach->sq_data; | |
7626 | if (!sqd) { | |
7627 | fdput(f); | |
7628 | return ERR_PTR(-EINVAL); | |
7629 | } | |
5c2469e0 JA |
7630 | if (sqd->task_tgid != current->tgid) { |
7631 | fdput(f); | |
7632 | return ERR_PTR(-EPERM); | |
7633 | } | |
aa06165d JA |
7634 | |
7635 | refcount_inc(&sqd->refs); | |
7636 | fdput(f); | |
7637 | return sqd; | |
7638 | } | |
7639 | ||
26984fbf PB |
7640 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
7641 | bool *attached) | |
534ca6d6 JA |
7642 | { |
7643 | struct io_sq_data *sqd; | |
7644 | ||
26984fbf | 7645 | *attached = false; |
5c2469e0 JA |
7646 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
7647 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
7648 | if (!IS_ERR(sqd)) { |
7649 | *attached = true; | |
5c2469e0 | 7650 | return sqd; |
26984fbf | 7651 | } |
5c2469e0 JA |
7652 | /* fall through for EPERM case, setup new sqd/task */ |
7653 | if (PTR_ERR(sqd) != -EPERM) | |
7654 | return sqd; | |
7655 | } | |
aa06165d | 7656 | |
534ca6d6 JA |
7657 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
7658 | if (!sqd) | |
7659 | return ERR_PTR(-ENOMEM); | |
7660 | ||
9e138a48 | 7661 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 7662 | refcount_set(&sqd->refs, 1); |
69fb2131 | 7663 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 7664 | mutex_init(&sqd->lock); |
534ca6d6 | 7665 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 7666 | init_completion(&sqd->exited); |
534ca6d6 JA |
7667 | return sqd; |
7668 | } | |
7669 | ||
6b06314c | 7670 | #if defined(CONFIG_UNIX) |
6b06314c JA |
7671 | /* |
7672 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
7673 | * the io_uring can be safely unregistered on process exit, even if we have | |
7674 | * loops in the file referencing. | |
7675 | */ | |
7676 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
7677 | { | |
7678 | struct sock *sk = ctx->ring_sock->sk; | |
7679 | struct scm_fp_list *fpl; | |
7680 | struct sk_buff *skb; | |
08a45173 | 7681 | int i, nr_files; |
6b06314c | 7682 | |
6b06314c JA |
7683 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
7684 | if (!fpl) | |
7685 | return -ENOMEM; | |
7686 | ||
7687 | skb = alloc_skb(0, GFP_KERNEL); | |
7688 | if (!skb) { | |
7689 | kfree(fpl); | |
7690 | return -ENOMEM; | |
7691 | } | |
7692 | ||
7693 | skb->sk = sk; | |
6b06314c | 7694 | |
08a45173 | 7695 | nr_files = 0; |
62e398be | 7696 | fpl->user = get_uid(current_user()); |
6b06314c | 7697 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
7698 | struct file *file = io_file_from_index(ctx, i + offset); |
7699 | ||
7700 | if (!file) | |
08a45173 | 7701 | continue; |
65e19f54 | 7702 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
7703 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
7704 | nr_files++; | |
6b06314c JA |
7705 | } |
7706 | ||
08a45173 JA |
7707 | if (nr_files) { |
7708 | fpl->max = SCM_MAX_FD; | |
7709 | fpl->count = nr_files; | |
7710 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 7711 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
7712 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
7713 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 7714 | |
08a45173 JA |
7715 | for (i = 0; i < nr_files; i++) |
7716 | fput(fpl->fp[i]); | |
7717 | } else { | |
7718 | kfree_skb(skb); | |
7719 | kfree(fpl); | |
7720 | } | |
6b06314c JA |
7721 | |
7722 | return 0; | |
7723 | } | |
7724 | ||
7725 | /* | |
7726 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
7727 | * causes regular reference counting to break down. We rely on the UNIX | |
7728 | * garbage collection to take care of this problem for us. | |
7729 | */ | |
7730 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7731 | { | |
7732 | unsigned left, total; | |
7733 | int ret = 0; | |
7734 | ||
7735 | total = 0; | |
7736 | left = ctx->nr_user_files; | |
7737 | while (left) { | |
7738 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
7739 | |
7740 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
7741 | if (ret) | |
7742 | break; | |
7743 | left -= this_files; | |
7744 | total += this_files; | |
7745 | } | |
7746 | ||
7747 | if (!ret) | |
7748 | return 0; | |
7749 | ||
7750 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
7751 | struct file *file = io_file_from_index(ctx, total); |
7752 | ||
7753 | if (file) | |
7754 | fput(file); | |
6b06314c JA |
7755 | total++; |
7756 | } | |
7757 | ||
7758 | return ret; | |
7759 | } | |
7760 | #else | |
7761 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
7762 | { | |
7763 | return 0; | |
7764 | } | |
7765 | #endif | |
7766 | ||
47e90392 | 7767 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 7768 | { |
50238531 | 7769 | struct file *file = prsrc->file; |
05f3fb3c JA |
7770 | #if defined(CONFIG_UNIX) |
7771 | struct sock *sock = ctx->ring_sock->sk; | |
7772 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
7773 | struct sk_buff *skb; | |
7774 | int i; | |
7775 | ||
7776 | __skb_queue_head_init(&list); | |
7777 | ||
7778 | /* | |
7779 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
7780 | * remove this entry and rearrange the file array. | |
7781 | */ | |
7782 | skb = skb_dequeue(head); | |
7783 | while (skb) { | |
7784 | struct scm_fp_list *fp; | |
7785 | ||
7786 | fp = UNIXCB(skb).fp; | |
7787 | for (i = 0; i < fp->count; i++) { | |
7788 | int left; | |
7789 | ||
7790 | if (fp->fp[i] != file) | |
7791 | continue; | |
7792 | ||
7793 | unix_notinflight(fp->user, fp->fp[i]); | |
7794 | left = fp->count - 1 - i; | |
7795 | if (left) { | |
7796 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
7797 | left * sizeof(struct file *)); | |
7798 | } | |
7799 | fp->count--; | |
7800 | if (!fp->count) { | |
7801 | kfree_skb(skb); | |
7802 | skb = NULL; | |
7803 | } else { | |
7804 | __skb_queue_tail(&list, skb); | |
7805 | } | |
7806 | fput(file); | |
7807 | file = NULL; | |
7808 | break; | |
7809 | } | |
7810 | ||
7811 | if (!file) | |
7812 | break; | |
7813 | ||
7814 | __skb_queue_tail(&list, skb); | |
7815 | ||
7816 | skb = skb_dequeue(head); | |
7817 | } | |
7818 | ||
7819 | if (skb_peek(&list)) { | |
7820 | spin_lock_irq(&head->lock); | |
7821 | while ((skb = __skb_dequeue(&list)) != NULL) | |
7822 | __skb_queue_tail(head, skb); | |
7823 | spin_unlock_irq(&head->lock); | |
7824 | } | |
7825 | #else | |
7826 | fput(file); | |
7827 | #endif | |
7828 | } | |
7829 | ||
b895c9a6 | 7830 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 7831 | { |
b895c9a6 | 7832 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
7833 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
7834 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 7835 | |
269bbe5f BM |
7836 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
7837 | list_del(&prsrc->list); | |
b60c8dce PB |
7838 | |
7839 | if (prsrc->tag) { | |
7840 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
7841 | |
7842 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 7843 | spin_lock(&ctx->completion_lock); |
b60c8dce | 7844 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
2840f710 | 7845 | ctx->cq_extra++; |
b60c8dce | 7846 | io_commit_cqring(ctx); |
79ebeaee | 7847 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
7848 | io_cqring_ev_posted(ctx); |
7849 | io_ring_submit_unlock(ctx, lock_ring); | |
7850 | } | |
7851 | ||
40ae0ff7 | 7852 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 7853 | kfree(prsrc); |
65e19f54 | 7854 | } |
05589553 | 7855 | |
28a9fe25 | 7856 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
7857 | if (atomic_dec_and_test(&rsrc_data->refs)) |
7858 | complete(&rsrc_data->done); | |
2faf852d | 7859 | } |
65e19f54 | 7860 | |
269bbe5f | 7861 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
7862 | { |
7863 | struct io_ring_ctx *ctx; | |
7864 | struct llist_node *node; | |
7865 | ||
269bbe5f BM |
7866 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
7867 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
7868 | |
7869 | while (node) { | |
b895c9a6 | 7870 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
7871 | struct llist_node *next = node->next; |
7872 | ||
b895c9a6 | 7873 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 7874 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
7875 | node = next; |
7876 | } | |
7877 | } | |
7878 | ||
6b06314c | 7879 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 7880 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
7881 | { |
7882 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 7883 | struct file *file; |
f3baed39 | 7884 | int fd, ret; |
846a4ef2 | 7885 | unsigned i; |
6b06314c | 7886 | |
05f3fb3c | 7887 | if (ctx->file_data) |
6b06314c JA |
7888 | return -EBUSY; |
7889 | if (!nr_args) | |
7890 | return -EINVAL; | |
7891 | if (nr_args > IORING_MAX_FIXED_FILES) | |
7892 | return -EMFILE; | |
3a1b8a4e PB |
7893 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
7894 | return -EMFILE; | |
a7f0ed5a | 7895 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
7896 | if (ret) |
7897 | return ret; | |
d878c816 PB |
7898 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
7899 | &ctx->file_data); | |
7900 | if (ret) | |
7901 | return ret; | |
6b06314c | 7902 | |
f3baed39 | 7903 | ret = -ENOMEM; |
aeca241b | 7904 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 7905 | goto out_free; |
65e19f54 | 7906 | |
08a45173 | 7907 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 7908 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
7909 | ret = -EFAULT; |
7910 | goto out_fput; | |
7911 | } | |
08a45173 | 7912 | /* allow sparse sets */ |
792e3582 PB |
7913 | if (fd == -1) { |
7914 | ret = -EINVAL; | |
2d091d62 | 7915 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 7916 | goto out_fput; |
08a45173 | 7917 | continue; |
792e3582 | 7918 | } |
6b06314c | 7919 | |
05f3fb3c | 7920 | file = fget(fd); |
6b06314c | 7921 | ret = -EBADF; |
792e3582 | 7922 | if (unlikely(!file)) |
600cf3f8 | 7923 | goto out_fput; |
05f3fb3c | 7924 | |
6b06314c JA |
7925 | /* |
7926 | * Don't allow io_uring instances to be registered. If UNIX | |
7927 | * isn't enabled, then this causes a reference cycle and this | |
7928 | * instance can never get freed. If UNIX is enabled we'll | |
7929 | * handle it just fine, but there's still no point in allowing | |
7930 | * a ring fd as it doesn't support regular read/write anyway. | |
7931 | */ | |
05f3fb3c JA |
7932 | if (file->f_op == &io_uring_fops) { |
7933 | fput(file); | |
600cf3f8 | 7934 | goto out_fput; |
6b06314c | 7935 | } |
aeca241b | 7936 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
7937 | } |
7938 | ||
6b06314c | 7939 | ret = io_sqe_files_scm(ctx); |
05589553 | 7940 | if (ret) { |
08480400 | 7941 | __io_sqe_files_unregister(ctx); |
05589553 XW |
7942 | return ret; |
7943 | } | |
6b06314c | 7944 | |
a7f0ed5a | 7945 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 7946 | return ret; |
600cf3f8 PB |
7947 | out_fput: |
7948 | for (i = 0; i < ctx->nr_user_files; i++) { | |
7949 | file = io_file_from_index(ctx, i); | |
7950 | if (file) | |
7951 | fput(file); | |
7952 | } | |
042b0d85 | 7953 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 7954 | ctx->nr_user_files = 0; |
600cf3f8 | 7955 | out_free: |
44b31f2f | 7956 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 7957 | ctx->file_data = NULL; |
6b06314c JA |
7958 | return ret; |
7959 | } | |
7960 | ||
c3a31e60 JA |
7961 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
7962 | int index) | |
7963 | { | |
7964 | #if defined(CONFIG_UNIX) | |
7965 | struct sock *sock = ctx->ring_sock->sk; | |
7966 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
7967 | struct sk_buff *skb; | |
7968 | ||
7969 | /* | |
7970 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
7971 | * file set. If there's no room, fall back to allocating a new skb | |
7972 | * and filling it in. | |
7973 | */ | |
7974 | spin_lock_irq(&head->lock); | |
7975 | skb = skb_peek(head); | |
7976 | if (skb) { | |
7977 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
7978 | ||
7979 | if (fpl->count < SCM_MAX_FD) { | |
7980 | __skb_unlink(skb, head); | |
7981 | spin_unlock_irq(&head->lock); | |
7982 | fpl->fp[fpl->count] = get_file(file); | |
7983 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
7984 | fpl->count++; | |
7985 | spin_lock_irq(&head->lock); | |
7986 | __skb_queue_head(head, skb); | |
7987 | } else { | |
7988 | skb = NULL; | |
7989 | } | |
7990 | } | |
7991 | spin_unlock_irq(&head->lock); | |
7992 | ||
7993 | if (skb) { | |
7994 | fput(file); | |
7995 | return 0; | |
7996 | } | |
7997 | ||
7998 | return __io_sqe_files_scm(ctx, 1, index); | |
7999 | #else | |
8000 | return 0; | |
8001 | #endif | |
8002 | } | |
8003 | ||
b9445598 PB |
8004 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
8005 | unsigned int issue_flags, u32 slot_index) | |
8006 | { | |
8007 | struct io_ring_ctx *ctx = req->ctx; | |
8008 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; | |
8009 | struct io_fixed_file *file_slot; | |
8010 | int ret = -EBADF; | |
8011 | ||
8012 | io_ring_submit_lock(ctx, !force_nonblock); | |
8013 | if (file->f_op == &io_uring_fops) | |
8014 | goto err; | |
8015 | ret = -ENXIO; | |
8016 | if (!ctx->file_data) | |
8017 | goto err; | |
8018 | ret = -EINVAL; | |
8019 | if (slot_index >= ctx->nr_user_files) | |
8020 | goto err; | |
8021 | ||
8022 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
8023 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
8024 | ret = -EBADF; | |
8025 | if (file_slot->file_ptr) | |
8026 | goto err; | |
8027 | ||
8028 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
8029 | io_fixed_file_set(file_slot, file); | |
8030 | ret = io_sqe_file_register(ctx, file, slot_index); | |
8031 | if (ret) { | |
8032 | file_slot->file_ptr = 0; | |
8033 | goto err; | |
8034 | } | |
8035 | ||
8036 | ret = 0; | |
8037 | err: | |
8038 | io_ring_submit_unlock(ctx, !force_nonblock); | |
8039 | if (ret) | |
8040 | fput(file); | |
8041 | return ret; | |
8042 | } | |
8043 | ||
b60c8dce | 8044 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
e7c78371 | 8045 | struct io_rsrc_node *node, void *rsrc) |
05f3fb3c | 8046 | { |
269bbe5f | 8047 | struct io_rsrc_put *prsrc; |
05f3fb3c | 8048 | |
269bbe5f BM |
8049 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
8050 | if (!prsrc) | |
a5318d3c | 8051 | return -ENOMEM; |
05f3fb3c | 8052 | |
2d091d62 | 8053 | prsrc->tag = *io_get_tag_slot(data, idx); |
50238531 | 8054 | prsrc->rsrc = rsrc; |
e7c78371 | 8055 | list_add(&prsrc->list, &node->rsrc_list); |
a5318d3c | 8056 | return 0; |
05f3fb3c JA |
8057 | } |
8058 | ||
8059 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
c3bdad02 | 8060 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
8061 | unsigned nr_args) |
8062 | { | |
c3bdad02 | 8063 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 8064 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 8065 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
8066 | struct io_fixed_file *file_slot; |
8067 | struct file *file; | |
98f0b3b4 PB |
8068 | int fd, i, err = 0; |
8069 | unsigned int done; | |
05589553 | 8070 | bool needs_switch = false; |
c3a31e60 | 8071 | |
98f0b3b4 PB |
8072 | if (!ctx->file_data) |
8073 | return -ENXIO; | |
8074 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
8075 | return -EINVAL; |
8076 | ||
67973b93 | 8077 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
8078 | u64 tag = 0; |
8079 | ||
8080 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
8081 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
8082 | err = -EFAULT; |
8083 | break; | |
8084 | } | |
c3bdad02 PB |
8085 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
8086 | err = -EINVAL; | |
8087 | break; | |
8088 | } | |
4e0377a1 | 8089 | if (fd == IORING_REGISTER_FILES_SKIP) |
8090 | continue; | |
8091 | ||
67973b93 | 8092 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 8093 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 8094 | |
a04b0ac0 PB |
8095 | if (file_slot->file_ptr) { |
8096 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
8097 | err = io_queue_rsrc_removal(data, up->offset + done, |
8098 | ctx->rsrc_node, file); | |
a5318d3c HD |
8099 | if (err) |
8100 | break; | |
a04b0ac0 | 8101 | file_slot->file_ptr = 0; |
05589553 | 8102 | needs_switch = true; |
c3a31e60 JA |
8103 | } |
8104 | if (fd != -1) { | |
c3a31e60 JA |
8105 | file = fget(fd); |
8106 | if (!file) { | |
8107 | err = -EBADF; | |
8108 | break; | |
8109 | } | |
8110 | /* | |
8111 | * Don't allow io_uring instances to be registered. If | |
8112 | * UNIX isn't enabled, then this causes a reference | |
8113 | * cycle and this instance can never get freed. If UNIX | |
8114 | * is enabled we'll handle it just fine, but there's | |
8115 | * still no point in allowing a ring fd as it doesn't | |
8116 | * support regular read/write anyway. | |
8117 | */ | |
8118 | if (file->f_op == &io_uring_fops) { | |
8119 | fput(file); | |
8120 | err = -EBADF; | |
8121 | break; | |
8122 | } | |
2d091d62 | 8123 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 8124 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 8125 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 8126 | if (err) { |
a04b0ac0 | 8127 | file_slot->file_ptr = 0; |
f3bd9dae | 8128 | fput(file); |
c3a31e60 | 8129 | break; |
f3bd9dae | 8130 | } |
c3a31e60 | 8131 | } |
05f3fb3c JA |
8132 | } |
8133 | ||
a7f0ed5a PB |
8134 | if (needs_switch) |
8135 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
8136 | return done ? done : err; |
8137 | } | |
05589553 | 8138 | |
685fe7fe JA |
8139 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
8140 | struct task_struct *task) | |
24369c2e | 8141 | { |
e941894e | 8142 | struct io_wq_hash *hash; |
24369c2e | 8143 | struct io_wq_data data; |
24369c2e | 8144 | unsigned int concurrency; |
24369c2e | 8145 | |
362a9e65 | 8146 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
8147 | hash = ctx->hash_map; |
8148 | if (!hash) { | |
8149 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
8150 | if (!hash) { |
8151 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 8152 | return ERR_PTR(-ENOMEM); |
362a9e65 | 8153 | } |
e941894e JA |
8154 | refcount_set(&hash->refs, 1); |
8155 | init_waitqueue_head(&hash->wait); | |
8156 | ctx->hash_map = hash; | |
24369c2e | 8157 | } |
362a9e65 | 8158 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 8159 | |
e941894e | 8160 | data.hash = hash; |
685fe7fe | 8161 | data.task = task; |
ebc11b6c | 8162 | data.free_work = io_wq_free_work; |
f5fa38c5 | 8163 | data.do_work = io_wq_submit_work; |
24369c2e | 8164 | |
d25e3a3d JA |
8165 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
8166 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 8167 | |
5aa75ed5 | 8168 | return io_wq_create(concurrency, &data); |
24369c2e PB |
8169 | } |
8170 | ||
5aa75ed5 JA |
8171 | static int io_uring_alloc_task_context(struct task_struct *task, |
8172 | struct io_ring_ctx *ctx) | |
0f212204 JA |
8173 | { |
8174 | struct io_uring_task *tctx; | |
d8a6df10 | 8175 | int ret; |
0f212204 | 8176 | |
09899b19 | 8177 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
8178 | if (unlikely(!tctx)) |
8179 | return -ENOMEM; | |
8180 | ||
d8a6df10 JA |
8181 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
8182 | if (unlikely(ret)) { | |
8183 | kfree(tctx); | |
8184 | return ret; | |
8185 | } | |
8186 | ||
685fe7fe | 8187 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
8188 | if (IS_ERR(tctx->io_wq)) { |
8189 | ret = PTR_ERR(tctx->io_wq); | |
8190 | percpu_counter_destroy(&tctx->inflight); | |
8191 | kfree(tctx); | |
8192 | return ret; | |
8193 | } | |
8194 | ||
0f212204 JA |
8195 | xa_init(&tctx->xa); |
8196 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 8197 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 8198 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 8199 | task->io_uring = tctx; |
7cbf1722 JA |
8200 | spin_lock_init(&tctx->task_lock); |
8201 | INIT_WQ_LIST(&tctx->task_list); | |
7cbf1722 | 8202 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
8203 | return 0; |
8204 | } | |
8205 | ||
8206 | void __io_uring_free(struct task_struct *tsk) | |
8207 | { | |
8208 | struct io_uring_task *tctx = tsk->io_uring; | |
8209 | ||
8210 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 8211 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 8212 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 8213 | |
d8a6df10 | 8214 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
8215 | kfree(tctx); |
8216 | tsk->io_uring = NULL; | |
8217 | } | |
8218 | ||
7e84e1c7 SG |
8219 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
8220 | struct io_uring_params *p) | |
2b188cc1 JA |
8221 | { |
8222 | int ret; | |
8223 | ||
d25e3a3d JA |
8224 | /* Retain compatibility with failing for an invalid attach attempt */ |
8225 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
8226 | IORING_SETUP_ATTACH_WQ) { | |
8227 | struct fd f; | |
8228 | ||
8229 | f = fdget(p->wq_fd); | |
8230 | if (!f.file) | |
8231 | return -ENXIO; | |
0cc936f7 JA |
8232 | if (f.file->f_op != &io_uring_fops) { |
8233 | fdput(f); | |
f2a48dd0 | 8234 | return -EINVAL; |
0cc936f7 JA |
8235 | } |
8236 | fdput(f); | |
d25e3a3d | 8237 | } |
6c271ce2 | 8238 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 8239 | struct task_struct *tsk; |
534ca6d6 | 8240 | struct io_sq_data *sqd; |
26984fbf | 8241 | bool attached; |
534ca6d6 | 8242 | |
26984fbf | 8243 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
8244 | if (IS_ERR(sqd)) { |
8245 | ret = PTR_ERR(sqd); | |
8246 | goto err; | |
8247 | } | |
69fb2131 | 8248 | |
7c30f36a | 8249 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 8250 | ctx->sq_data = sqd; |
917257da JA |
8251 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
8252 | if (!ctx->sq_thread_idle) | |
8253 | ctx->sq_thread_idle = HZ; | |
8254 | ||
78d7f6ba | 8255 | io_sq_thread_park(sqd); |
de75a3d3 PB |
8256 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
8257 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 8258 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 8259 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
8260 | io_sq_thread_unpark(sqd); |
8261 | ||
de75a3d3 PB |
8262 | if (ret < 0) |
8263 | goto err; | |
8264 | if (attached) | |
5aa75ed5 | 8265 | return 0; |
aa06165d | 8266 | |
6c271ce2 | 8267 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 8268 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 8269 | |
917257da | 8270 | ret = -EINVAL; |
f2a48dd0 | 8271 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 8272 | goto err_sqpoll; |
37d1e2e3 | 8273 | sqd->sq_cpu = cpu; |
6c271ce2 | 8274 | } else { |
37d1e2e3 | 8275 | sqd->sq_cpu = -1; |
6c271ce2 | 8276 | } |
37d1e2e3 JA |
8277 | |
8278 | sqd->task_pid = current->pid; | |
5c2469e0 | 8279 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
8280 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
8281 | if (IS_ERR(tsk)) { | |
8282 | ret = PTR_ERR(tsk); | |
e8f98f24 | 8283 | goto err_sqpoll; |
6c271ce2 | 8284 | } |
97a73a0f | 8285 | |
46fe18b1 | 8286 | sqd->thread = tsk; |
97a73a0f | 8287 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 8288 | wake_up_new_task(tsk); |
0f212204 JA |
8289 | if (ret) |
8290 | goto err; | |
6c271ce2 JA |
8291 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
8292 | /* Can't have SQ_AFF without SQPOLL */ | |
8293 | ret = -EINVAL; | |
8294 | goto err; | |
8295 | } | |
8296 | ||
2b188cc1 | 8297 | return 0; |
f2a48dd0 PB |
8298 | err_sqpoll: |
8299 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 8300 | err: |
37d1e2e3 | 8301 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
8302 | return ret; |
8303 | } | |
8304 | ||
a087e2b5 BM |
8305 | static inline void __io_unaccount_mem(struct user_struct *user, |
8306 | unsigned long nr_pages) | |
2b188cc1 JA |
8307 | { |
8308 | atomic_long_sub(nr_pages, &user->locked_vm); | |
8309 | } | |
8310 | ||
a087e2b5 BM |
8311 | static inline int __io_account_mem(struct user_struct *user, |
8312 | unsigned long nr_pages) | |
2b188cc1 JA |
8313 | { |
8314 | unsigned long page_limit, cur_pages, new_pages; | |
8315 | ||
8316 | /* Don't allow more pages than we can safely lock */ | |
8317 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
8318 | ||
8319 | do { | |
8320 | cur_pages = atomic_long_read(&user->locked_vm); | |
8321 | new_pages = cur_pages + nr_pages; | |
8322 | if (new_pages > page_limit) | |
8323 | return -ENOMEM; | |
8324 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
8325 | new_pages) != cur_pages); | |
8326 | ||
8327 | return 0; | |
8328 | } | |
8329 | ||
26bfa89e | 8330 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8331 | { |
62e398be | 8332 | if (ctx->user) |
a087e2b5 | 8333 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 8334 | |
26bfa89e JA |
8335 | if (ctx->mm_account) |
8336 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8337 | } |
8338 | ||
26bfa89e | 8339 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 8340 | { |
30975825 BM |
8341 | int ret; |
8342 | ||
62e398be | 8343 | if (ctx->user) { |
30975825 BM |
8344 | ret = __io_account_mem(ctx->user, nr_pages); |
8345 | if (ret) | |
8346 | return ret; | |
8347 | } | |
8348 | ||
26bfa89e JA |
8349 | if (ctx->mm_account) |
8350 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
8351 | |
8352 | return 0; | |
8353 | } | |
8354 | ||
2b188cc1 JA |
8355 | static void io_mem_free(void *ptr) |
8356 | { | |
52e04ef4 MR |
8357 | struct page *page; |
8358 | ||
8359 | if (!ptr) | |
8360 | return; | |
2b188cc1 | 8361 | |
52e04ef4 | 8362 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
8363 | if (put_page_testzero(page)) |
8364 | free_compound_page(page); | |
8365 | } | |
8366 | ||
8367 | static void *io_mem_alloc(size_t size) | |
8368 | { | |
8369 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
26bfa89e | 8370 | __GFP_NORETRY | __GFP_ACCOUNT; |
2b188cc1 JA |
8371 | |
8372 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
8373 | } | |
8374 | ||
75b28aff HV |
8375 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
8376 | size_t *sq_offset) | |
8377 | { | |
8378 | struct io_rings *rings; | |
8379 | size_t off, sq_array_size; | |
8380 | ||
8381 | off = struct_size(rings, cqes, cq_entries); | |
8382 | if (off == SIZE_MAX) | |
8383 | return SIZE_MAX; | |
8384 | ||
8385 | #ifdef CONFIG_SMP | |
8386 | off = ALIGN(off, SMP_CACHE_BYTES); | |
8387 | if (off == 0) | |
8388 | return SIZE_MAX; | |
8389 | #endif | |
8390 | ||
b36200f5 DV |
8391 | if (sq_offset) |
8392 | *sq_offset = off; | |
8393 | ||
75b28aff HV |
8394 | sq_array_size = array_size(sizeof(u32), sq_entries); |
8395 | if (sq_array_size == SIZE_MAX) | |
8396 | return SIZE_MAX; | |
8397 | ||
8398 | if (check_add_overflow(off, sq_array_size, &off)) | |
8399 | return SIZE_MAX; | |
8400 | ||
75b28aff HV |
8401 | return off; |
8402 | } | |
8403 | ||
41edf1a5 | 8404 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 8405 | { |
41edf1a5 | 8406 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
8407 | unsigned int i; |
8408 | ||
6224843d PB |
8409 | if (imu != ctx->dummy_ubuf) { |
8410 | for (i = 0; i < imu->nr_bvecs; i++) | |
8411 | unpin_user_page(imu->bvec[i].bv_page); | |
8412 | if (imu->acct_pages) | |
8413 | io_unaccount_mem(ctx, imu->acct_pages); | |
8414 | kvfree(imu); | |
8415 | } | |
41edf1a5 | 8416 | *slot = NULL; |
7f61a1e9 PB |
8417 | } |
8418 | ||
bd54b6fe | 8419 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 8420 | { |
634d00df PB |
8421 | io_buffer_unmap(ctx, &prsrc->buf); |
8422 | prsrc->buf = NULL; | |
bd54b6fe | 8423 | } |
edafccee | 8424 | |
bd54b6fe BM |
8425 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
8426 | { | |
8427 | unsigned int i; | |
edafccee | 8428 | |
7f61a1e9 PB |
8429 | for (i = 0; i < ctx->nr_user_bufs; i++) |
8430 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 8431 | kfree(ctx->user_bufs); |
bb6659cc | 8432 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 8433 | ctx->user_bufs = NULL; |
bd54b6fe | 8434 | ctx->buf_data = NULL; |
edafccee | 8435 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
8436 | } |
8437 | ||
0a96bbe4 | 8438 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 8439 | { |
bd54b6fe | 8440 | int ret; |
edafccee | 8441 | |
bd54b6fe | 8442 | if (!ctx->buf_data) |
edafccee JA |
8443 | return -ENXIO; |
8444 | ||
bd54b6fe BM |
8445 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
8446 | if (!ret) | |
8447 | __io_sqe_buffers_unregister(ctx); | |
8448 | return ret; | |
edafccee JA |
8449 | } |
8450 | ||
8451 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
8452 | void __user *arg, unsigned index) | |
8453 | { | |
8454 | struct iovec __user *src; | |
8455 | ||
8456 | #ifdef CONFIG_COMPAT | |
8457 | if (ctx->compat) { | |
8458 | struct compat_iovec __user *ciovs; | |
8459 | struct compat_iovec ciov; | |
8460 | ||
8461 | ciovs = (struct compat_iovec __user *) arg; | |
8462 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
8463 | return -EFAULT; | |
8464 | ||
d55e5f5b | 8465 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
8466 | dst->iov_len = ciov.iov_len; |
8467 | return 0; | |
8468 | } | |
8469 | #endif | |
8470 | src = (struct iovec __user *) arg; | |
8471 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
8472 | return -EFAULT; | |
8473 | return 0; | |
8474 | } | |
8475 | ||
de293938 JA |
8476 | /* |
8477 | * Not super efficient, but this is just a registration time. And we do cache | |
8478 | * the last compound head, so generally we'll only do a full search if we don't | |
8479 | * match that one. | |
8480 | * | |
8481 | * We check if the given compound head page has already been accounted, to | |
8482 | * avoid double accounting it. This allows us to account the full size of the | |
8483 | * page, not just the constituent pages of a huge page. | |
8484 | */ | |
8485 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
8486 | int nr_pages, struct page *hpage) | |
8487 | { | |
8488 | int i, j; | |
8489 | ||
8490 | /* check current page array */ | |
8491 | for (i = 0; i < nr_pages; i++) { | |
8492 | if (!PageCompound(pages[i])) | |
8493 | continue; | |
8494 | if (compound_head(pages[i]) == hpage) | |
8495 | return true; | |
8496 | } | |
8497 | ||
8498 | /* check previously registered pages */ | |
8499 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 8500 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
8501 | |
8502 | for (j = 0; j < imu->nr_bvecs; j++) { | |
8503 | if (!PageCompound(imu->bvec[j].bv_page)) | |
8504 | continue; | |
8505 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
8506 | return true; | |
8507 | } | |
8508 | } | |
8509 | ||
8510 | return false; | |
8511 | } | |
8512 | ||
8513 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
8514 | int nr_pages, struct io_mapped_ubuf *imu, | |
8515 | struct page **last_hpage) | |
8516 | { | |
8517 | int i, ret; | |
8518 | ||
216e5835 | 8519 | imu->acct_pages = 0; |
de293938 JA |
8520 | for (i = 0; i < nr_pages; i++) { |
8521 | if (!PageCompound(pages[i])) { | |
8522 | imu->acct_pages++; | |
8523 | } else { | |
8524 | struct page *hpage; | |
8525 | ||
8526 | hpage = compound_head(pages[i]); | |
8527 | if (hpage == *last_hpage) | |
8528 | continue; | |
8529 | *last_hpage = hpage; | |
8530 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
8531 | continue; | |
8532 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
8533 | } | |
8534 | } | |
8535 | ||
8536 | if (!imu->acct_pages) | |
8537 | return 0; | |
8538 | ||
26bfa89e | 8539 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
8540 | if (ret) |
8541 | imu->acct_pages = 0; | |
8542 | return ret; | |
8543 | } | |
8544 | ||
0a96bbe4 | 8545 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 8546 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 8547 | struct page **last_hpage) |
edafccee | 8548 | { |
41edf1a5 | 8549 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
8550 | struct vm_area_struct **vmas = NULL; |
8551 | struct page **pages = NULL; | |
0a96bbe4 BM |
8552 | unsigned long off, start, end, ubuf; |
8553 | size_t size; | |
8554 | int ret, pret, nr_pages, i; | |
8555 | ||
6224843d PB |
8556 | if (!iov->iov_base) { |
8557 | *pimu = ctx->dummy_ubuf; | |
8558 | return 0; | |
8559 | } | |
8560 | ||
0a96bbe4 BM |
8561 | ubuf = (unsigned long) iov->iov_base; |
8562 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
8563 | start = ubuf >> PAGE_SHIFT; | |
8564 | nr_pages = end - start; | |
8565 | ||
41edf1a5 | 8566 | *pimu = NULL; |
0a96bbe4 BM |
8567 | ret = -ENOMEM; |
8568 | ||
8569 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
8570 | if (!pages) | |
8571 | goto done; | |
8572 | ||
8573 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
8574 | GFP_KERNEL); | |
8575 | if (!vmas) | |
8576 | goto done; | |
edafccee | 8577 | |
41edf1a5 | 8578 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 8579 | if (!imu) |
0a96bbe4 BM |
8580 | goto done; |
8581 | ||
8582 | ret = 0; | |
8583 | mmap_read_lock(current->mm); | |
8584 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
8585 | pages, vmas); | |
8586 | if (pret == nr_pages) { | |
8587 | /* don't support file backed memory */ | |
8588 | for (i = 0; i < nr_pages; i++) { | |
8589 | struct vm_area_struct *vma = vmas[i]; | |
8590 | ||
40dad765 PB |
8591 | if (vma_is_shmem(vma)) |
8592 | continue; | |
0a96bbe4 BM |
8593 | if (vma->vm_file && |
8594 | !is_file_hugepages(vma->vm_file)) { | |
8595 | ret = -EOPNOTSUPP; | |
8596 | break; | |
8597 | } | |
8598 | } | |
8599 | } else { | |
8600 | ret = pret < 0 ? pret : -EFAULT; | |
8601 | } | |
8602 | mmap_read_unlock(current->mm); | |
8603 | if (ret) { | |
8604 | /* | |
8605 | * if we did partial map, or found file backed vmas, | |
8606 | * release any pages we did get | |
8607 | */ | |
8608 | if (pret > 0) | |
8609 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8610 | goto done; |
8611 | } | |
8612 | ||
8613 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
8614 | if (ret) { | |
8615 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
8616 | goto done; |
8617 | } | |
8618 | ||
8619 | off = ubuf & ~PAGE_MASK; | |
8620 | size = iov->iov_len; | |
8621 | for (i = 0; i < nr_pages; i++) { | |
8622 | size_t vec_len; | |
8623 | ||
8624 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
8625 | imu->bvec[i].bv_page = pages[i]; | |
8626 | imu->bvec[i].bv_len = vec_len; | |
8627 | imu->bvec[i].bv_offset = off; | |
8628 | off = 0; | |
8629 | size -= vec_len; | |
8630 | } | |
8631 | /* store original address for later verification */ | |
8632 | imu->ubuf = ubuf; | |
4751f53d | 8633 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 8634 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 8635 | *pimu = imu; |
0a96bbe4 BM |
8636 | ret = 0; |
8637 | done: | |
41edf1a5 PB |
8638 | if (ret) |
8639 | kvfree(imu); | |
0a96bbe4 BM |
8640 | kvfree(pages); |
8641 | kvfree(vmas); | |
8642 | return ret; | |
8643 | } | |
8644 | ||
2b358604 | 8645 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 8646 | { |
87094465 PB |
8647 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
8648 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 8649 | } |
edafccee | 8650 | |
2b358604 BM |
8651 | static int io_buffer_validate(struct iovec *iov) |
8652 | { | |
50e96989 PB |
8653 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
8654 | ||
2b358604 BM |
8655 | /* |
8656 | * Don't impose further limits on the size and buffer | |
8657 | * constraints here, we'll -EINVAL later when IO is | |
8658 | * submitted if they are wrong. | |
8659 | */ | |
6224843d PB |
8660 | if (!iov->iov_base) |
8661 | return iov->iov_len ? -EFAULT : 0; | |
8662 | if (!iov->iov_len) | |
2b358604 | 8663 | return -EFAULT; |
edafccee | 8664 | |
2b358604 BM |
8665 | /* arbitrary limit, but we need something */ |
8666 | if (iov->iov_len > SZ_1G) | |
8667 | return -EFAULT; | |
edafccee | 8668 | |
50e96989 PB |
8669 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
8670 | return -EOVERFLOW; | |
8671 | ||
2b358604 BM |
8672 | return 0; |
8673 | } | |
edafccee | 8674 | |
2b358604 | 8675 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 8676 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 8677 | { |
bd54b6fe BM |
8678 | struct page *last_hpage = NULL; |
8679 | struct io_rsrc_data *data; | |
2b358604 BM |
8680 | int i, ret; |
8681 | struct iovec iov; | |
edafccee | 8682 | |
87094465 PB |
8683 | if (ctx->user_bufs) |
8684 | return -EBUSY; | |
489809e2 | 8685 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 8686 | return -EINVAL; |
bd54b6fe | 8687 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
8688 | if (ret) |
8689 | return ret; | |
d878c816 PB |
8690 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
8691 | if (ret) | |
8692 | return ret; | |
bd54b6fe BM |
8693 | ret = io_buffers_map_alloc(ctx, nr_args); |
8694 | if (ret) { | |
bb6659cc | 8695 | io_rsrc_data_free(data); |
bd54b6fe BM |
8696 | return ret; |
8697 | } | |
edafccee | 8698 | |
87094465 | 8699 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
8700 | ret = io_copy_iov(ctx, &iov, arg, i); |
8701 | if (ret) | |
0a96bbe4 | 8702 | break; |
2b358604 BM |
8703 | ret = io_buffer_validate(&iov); |
8704 | if (ret) | |
0a96bbe4 | 8705 | break; |
2d091d62 | 8706 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
8707 | ret = -EINVAL; |
8708 | break; | |
8709 | } | |
edafccee | 8710 | |
41edf1a5 PB |
8711 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
8712 | &last_hpage); | |
0a96bbe4 BM |
8713 | if (ret) |
8714 | break; | |
edafccee | 8715 | } |
0a96bbe4 | 8716 | |
bd54b6fe | 8717 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 8718 | |
bd54b6fe BM |
8719 | ctx->buf_data = data; |
8720 | if (ret) | |
8721 | __io_sqe_buffers_unregister(ctx); | |
8722 | else | |
8723 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
8724 | return ret; |
8725 | } | |
8726 | ||
634d00df PB |
8727 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
8728 | struct io_uring_rsrc_update2 *up, | |
8729 | unsigned int nr_args) | |
8730 | { | |
8731 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
8732 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
8733 | struct page *last_hpage = NULL; |
8734 | bool needs_switch = false; | |
8735 | __u32 done; | |
8736 | int i, err; | |
8737 | ||
8738 | if (!ctx->buf_data) | |
8739 | return -ENXIO; | |
8740 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
8741 | return -EINVAL; | |
8742 | ||
8743 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
8744 | struct io_mapped_ubuf *imu; |
8745 | int offset = up->offset + done; | |
634d00df PB |
8746 | u64 tag = 0; |
8747 | ||
8748 | err = io_copy_iov(ctx, &iov, iovs, done); | |
8749 | if (err) | |
8750 | break; | |
8751 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
8752 | err = -EFAULT; | |
8753 | break; | |
8754 | } | |
0b8c0e7c PB |
8755 | err = io_buffer_validate(&iov); |
8756 | if (err) | |
8757 | break; | |
cf3770e7 CIK |
8758 | if (!iov.iov_base && tag) { |
8759 | err = -EINVAL; | |
8760 | break; | |
8761 | } | |
0b8c0e7c PB |
8762 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
8763 | if (err) | |
8764 | break; | |
634d00df | 8765 | |
0b8c0e7c | 8766 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 8767 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
8768 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
8769 | ctx->rsrc_node, ctx->user_bufs[i]); | |
8770 | if (unlikely(err)) { | |
8771 | io_buffer_unmap(ctx, &imu); | |
634d00df | 8772 | break; |
0b8c0e7c | 8773 | } |
634d00df PB |
8774 | ctx->user_bufs[i] = NULL; |
8775 | needs_switch = true; | |
8776 | } | |
8777 | ||
0b8c0e7c | 8778 | ctx->user_bufs[i] = imu; |
2d091d62 | 8779 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
8780 | } |
8781 | ||
8782 | if (needs_switch) | |
8783 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
8784 | return done ? done : err; | |
8785 | } | |
8786 | ||
9b402849 JA |
8787 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
8788 | { | |
8789 | __s32 __user *fds = arg; | |
8790 | int fd; | |
8791 | ||
8792 | if (ctx->cq_ev_fd) | |
8793 | return -EBUSY; | |
8794 | ||
8795 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
8796 | return -EFAULT; | |
8797 | ||
8798 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
8799 | if (IS_ERR(ctx->cq_ev_fd)) { | |
8800 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
fe7e3257 | 8801 | |
9b402849 JA |
8802 | ctx->cq_ev_fd = NULL; |
8803 | return ret; | |
8804 | } | |
8805 | ||
8806 | return 0; | |
8807 | } | |
8808 | ||
8809 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
8810 | { | |
8811 | if (ctx->cq_ev_fd) { | |
8812 | eventfd_ctx_put(ctx->cq_ev_fd); | |
8813 | ctx->cq_ev_fd = NULL; | |
8814 | return 0; | |
8815 | } | |
8816 | ||
8817 | return -ENXIO; | |
8818 | } | |
8819 | ||
5a2e745d JA |
8820 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
8821 | { | |
9e15c3a0 JA |
8822 | struct io_buffer *buf; |
8823 | unsigned long index; | |
8824 | ||
8825 | xa_for_each(&ctx->io_buffers, index, buf) | |
8826 | __io_remove_buffers(ctx, buf, index, -1U); | |
5a2e745d JA |
8827 | } |
8828 | ||
7255834e | 8829 | static void io_req_cache_free(struct list_head *list) |
1b4c351f | 8830 | { |
68e68ee6 | 8831 | struct io_kiocb *req, *nxt; |
1b4c351f | 8832 | |
bb943b82 PB |
8833 | list_for_each_entry_safe(req, nxt, list, inflight_entry) { |
8834 | list_del(&req->inflight_entry); | |
1b4c351f JA |
8835 | kmem_cache_free(req_cachep, req); |
8836 | } | |
8837 | } | |
8838 | ||
4010fec4 | 8839 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 8840 | { |
cd0ca2e0 | 8841 | struct io_submit_state *state = &ctx->submit_state; |
bf019da7 | 8842 | |
9a4fdbd8 JA |
8843 | mutex_lock(&ctx->uring_lock); |
8844 | ||
cd0ca2e0 PB |
8845 | if (state->free_reqs) { |
8846 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); | |
8847 | state->free_reqs = 0; | |
8e5c66c4 | 8848 | } |
9a4fdbd8 | 8849 | |
cd0ca2e0 PB |
8850 | io_flush_cached_locked_reqs(ctx, state); |
8851 | io_req_cache_free(&state->free_list); | |
9a4fdbd8 JA |
8852 | mutex_unlock(&ctx->uring_lock); |
8853 | } | |
8854 | ||
43597aac | 8855 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 8856 | { |
43597aac | 8857 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 8858 | wait_for_completion(&data->done); |
bd54b6fe | 8859 | } |
04fc6c80 | 8860 | |
2b188cc1 JA |
8861 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
8862 | { | |
37d1e2e3 | 8863 | io_sq_thread_finish(ctx); |
2aede0e4 | 8864 | |
37d1e2e3 | 8865 | if (ctx->mm_account) { |
2aede0e4 JA |
8866 | mmdrop(ctx->mm_account); |
8867 | ctx->mm_account = NULL; | |
30975825 | 8868 | } |
def596e9 | 8869 | |
43597aac PB |
8870 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
8871 | io_wait_rsrc_data(ctx->buf_data); | |
8872 | io_wait_rsrc_data(ctx->file_data); | |
8873 | ||
8bad28d8 | 8874 | mutex_lock(&ctx->uring_lock); |
43597aac | 8875 | if (ctx->buf_data) |
bd54b6fe | 8876 | __io_sqe_buffers_unregister(ctx); |
43597aac | 8877 | if (ctx->file_data) |
08480400 | 8878 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
8879 | if (ctx->rings) |
8880 | __io_cqring_overflow_flush(ctx, true); | |
8bad28d8 | 8881 | mutex_unlock(&ctx->uring_lock); |
9b402849 | 8882 | io_eventfd_unregister(ctx); |
5a2e745d | 8883 | io_destroy_buffers(ctx); |
07db298a PB |
8884 | if (ctx->sq_creds) |
8885 | put_cred(ctx->sq_creds); | |
def596e9 | 8886 | |
a7f0ed5a PB |
8887 | /* there are no registered resources left, nobody uses it */ |
8888 | if (ctx->rsrc_node) | |
8889 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 8890 | if (ctx->rsrc_backup_node) |
b895c9a6 | 8891 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a PB |
8892 | flush_delayed_work(&ctx->rsrc_put_work); |
8893 | ||
8894 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
8895 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 8896 | |
2b188cc1 | 8897 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
8898 | if (ctx->ring_sock) { |
8899 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 8900 | sock_release(ctx->ring_sock); |
355e8d26 | 8901 | } |
2b188cc1 JA |
8902 | #endif |
8903 | ||
75b28aff | 8904 | io_mem_free(ctx->rings); |
2b188cc1 | 8905 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
8906 | |
8907 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 8908 | free_uid(ctx->user); |
4010fec4 | 8909 | io_req_caches_free(ctx); |
e941894e JA |
8910 | if (ctx->hash_map) |
8911 | io_wq_put_hash(ctx->hash_map); | |
78076bb6 | 8912 | kfree(ctx->cancel_hash); |
6224843d | 8913 | kfree(ctx->dummy_ubuf); |
2b188cc1 JA |
8914 | kfree(ctx); |
8915 | } | |
8916 | ||
8917 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
8918 | { | |
8919 | struct io_ring_ctx *ctx = file->private_data; | |
8920 | __poll_t mask = 0; | |
8921 | ||
311997b3 | 8922 | poll_wait(file, &ctx->poll_wait, wait); |
4f7067c3 SB |
8923 | /* |
8924 | * synchronizes with barrier from wq_has_sleeper call in | |
8925 | * io_commit_cqring | |
8926 | */ | |
2b188cc1 | 8927 | smp_rmb(); |
90554200 | 8928 | if (!io_sqring_full(ctx)) |
2b188cc1 | 8929 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
8930 | |
8931 | /* | |
8932 | * Don't flush cqring overflow list here, just do a simple check. | |
8933 | * Otherwise there could possible be ABBA deadlock: | |
8934 | * CPU0 CPU1 | |
8935 | * ---- ---- | |
8936 | * lock(&ctx->uring_lock); | |
8937 | * lock(&ep->mtx); | |
8938 | * lock(&ctx->uring_lock); | |
8939 | * lock(&ep->mtx); | |
8940 | * | |
8941 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
8942 | * pushs them to do the flush. | |
8943 | */ | |
5ed7a37d | 8944 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
8945 | mask |= EPOLLIN | EPOLLRDNORM; |
8946 | ||
8947 | return mask; | |
8948 | } | |
8949 | ||
8950 | static int io_uring_fasync(int fd, struct file *file, int on) | |
8951 | { | |
8952 | struct io_ring_ctx *ctx = file->private_data; | |
8953 | ||
8954 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
8955 | } | |
8956 | ||
0bead8cd | 8957 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 8958 | { |
4379bf8b | 8959 | const struct cred *creds; |
071698e1 | 8960 | |
61cf9370 | 8961 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
8962 | if (creds) { |
8963 | put_cred(creds); | |
0bead8cd | 8964 | return 0; |
1e6fa521 | 8965 | } |
0bead8cd YD |
8966 | |
8967 | return -EINVAL; | |
8968 | } | |
8969 | ||
d56d938b PB |
8970 | struct io_tctx_exit { |
8971 | struct callback_head task_work; | |
8972 | struct completion completion; | |
baf186c4 | 8973 | struct io_ring_ctx *ctx; |
d56d938b PB |
8974 | }; |
8975 | ||
8976 | static void io_tctx_exit_cb(struct callback_head *cb) | |
8977 | { | |
8978 | struct io_uring_task *tctx = current->io_uring; | |
8979 | struct io_tctx_exit *work; | |
8980 | ||
8981 | work = container_of(cb, struct io_tctx_exit, task_work); | |
8982 | /* | |
8983 | * When @in_idle, we're in cancellation and it's racy to remove the | |
8984 | * node. It'll be removed by the end of cancellation, just ignore it. | |
8985 | */ | |
8986 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 8987 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
8988 | complete(&work->completion); |
8989 | } | |
8990 | ||
28090c13 PB |
8991 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
8992 | { | |
8993 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
8994 | ||
8995 | return req->ctx == data; | |
8996 | } | |
8997 | ||
85faa7b8 JA |
8998 | static void io_ring_exit_work(struct work_struct *work) |
8999 | { | |
d56d938b | 9000 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 9001 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 9002 | unsigned long interval = HZ / 20; |
d56d938b PB |
9003 | struct io_tctx_exit exit; |
9004 | struct io_tctx_node *node; | |
9005 | int ret; | |
85faa7b8 | 9006 | |
56952e91 JA |
9007 | /* |
9008 | * If we're doing polled IO and end up having requests being | |
9009 | * submitted async (out-of-line), then completions can come in while | |
9010 | * we're waiting for refs to drop. We need to reap these manually, | |
9011 | * as nobody else will be looking for them. | |
9012 | */ | |
b2edc0a7 | 9013 | do { |
3dd0c97a | 9014 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
9015 | if (ctx->sq_data) { |
9016 | struct io_sq_data *sqd = ctx->sq_data; | |
9017 | struct task_struct *tsk; | |
9018 | ||
9019 | io_sq_thread_park(sqd); | |
9020 | tsk = sqd->thread; | |
9021 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
9022 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
9023 | io_cancel_ctx_cb, ctx, true); | |
9024 | io_sq_thread_unpark(sqd); | |
9025 | } | |
b5bb3a24 | 9026 | |
58d3be2c PB |
9027 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
9028 | /* there is little hope left, don't run it too often */ | |
9029 | interval = HZ * 60; | |
9030 | } | |
9031 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 9032 | |
7f00651a PB |
9033 | init_completion(&exit.completion); |
9034 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
9035 | exit.ctx = ctx; | |
89b5066e PB |
9036 | /* |
9037 | * Some may use context even when all refs and requests have been put, | |
9038 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 9039 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
9040 | * this lock/unlock section also waits them to finish. |
9041 | */ | |
d56d938b PB |
9042 | mutex_lock(&ctx->uring_lock); |
9043 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
9044 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
9045 | ||
d56d938b PB |
9046 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
9047 | ctx_node); | |
7f00651a PB |
9048 | /* don't spin on a single task if cancellation failed */ |
9049 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
9050 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
9051 | if (WARN_ON_ONCE(ret)) | |
9052 | continue; | |
9053 | wake_up_process(node->task); | |
9054 | ||
9055 | mutex_unlock(&ctx->uring_lock); | |
9056 | wait_for_completion(&exit.completion); | |
d56d938b PB |
9057 | mutex_lock(&ctx->uring_lock); |
9058 | } | |
9059 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
9060 | spin_lock(&ctx->completion_lock); |
9061 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 9062 | |
85faa7b8 JA |
9063 | io_ring_ctx_free(ctx); |
9064 | } | |
9065 | ||
80c4cbdb PB |
9066 | /* Returns true if we found and killed one or more timeouts */ |
9067 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, | |
3dd0c97a | 9068 | bool cancel_all) |
80c4cbdb PB |
9069 | { |
9070 | struct io_kiocb *req, *tmp; | |
9071 | int canceled = 0; | |
9072 | ||
79ebeaee JA |
9073 | spin_lock(&ctx->completion_lock); |
9074 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 9075 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 9076 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
9077 | io_kill_timeout(req, -ECANCELED); |
9078 | canceled++; | |
9079 | } | |
9080 | } | |
79ebeaee | 9081 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
9082 | if (canceled != 0) |
9083 | io_commit_cqring(ctx); | |
79ebeaee | 9084 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
9085 | if (canceled != 0) |
9086 | io_cqring_ev_posted(ctx); | |
9087 | return canceled != 0; | |
9088 | } | |
9089 | ||
2b188cc1 JA |
9090 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
9091 | { | |
61cf9370 MWO |
9092 | unsigned long index; |
9093 | struct creds *creds; | |
9094 | ||
2b188cc1 JA |
9095 | mutex_lock(&ctx->uring_lock); |
9096 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 9097 | if (ctx->rings) |
6c2450ae | 9098 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
9099 | xa_for_each(&ctx->personalities, index, creds) |
9100 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
9101 | mutex_unlock(&ctx->uring_lock); |
9102 | ||
3dd0c97a PB |
9103 | io_kill_timeouts(ctx, NULL, true); |
9104 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 9105 | |
15dff286 | 9106 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 9107 | io_iopoll_try_reap_events(ctx); |
309fc03a | 9108 | |
85faa7b8 | 9109 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
9110 | /* |
9111 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
9112 | * if we're exiting a ton of rings at the same time. It just adds | |
9113 | * noise and overhead, there's no discernable change in runtime | |
9114 | * over using system_wq. | |
9115 | */ | |
9116 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
9117 | } |
9118 | ||
9119 | static int io_uring_release(struct inode *inode, struct file *file) | |
9120 | { | |
9121 | struct io_ring_ctx *ctx = file->private_data; | |
9122 | ||
9123 | file->private_data = NULL; | |
9124 | io_ring_ctx_wait_and_kill(ctx); | |
9125 | return 0; | |
9126 | } | |
9127 | ||
f6edbabb PB |
9128 | struct io_task_cancel { |
9129 | struct task_struct *task; | |
3dd0c97a | 9130 | bool all; |
f6edbabb | 9131 | }; |
f254ac04 | 9132 | |
f6edbabb | 9133 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 9134 | { |
9a472ef7 | 9135 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 9136 | struct io_task_cancel *cancel = data; |
9a472ef7 PB |
9137 | bool ret; |
9138 | ||
3dd0c97a | 9139 | if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) { |
9a472ef7 PB |
9140 | struct io_ring_ctx *ctx = req->ctx; |
9141 | ||
9142 | /* protect against races with linked timeouts */ | |
79ebeaee | 9143 | spin_lock(&ctx->completion_lock); |
3dd0c97a | 9144 | ret = io_match_task(req, cancel->task, cancel->all); |
79ebeaee | 9145 | spin_unlock(&ctx->completion_lock); |
9a472ef7 | 9146 | } else { |
3dd0c97a | 9147 | ret = io_match_task(req, cancel->task, cancel->all); |
9a472ef7 PB |
9148 | } |
9149 | return ret; | |
b711d4ea JA |
9150 | } |
9151 | ||
e1915f76 | 9152 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
3dd0c97a | 9153 | struct task_struct *task, bool cancel_all) |
b7ddce3c | 9154 | { |
e1915f76 | 9155 | struct io_defer_entry *de; |
b7ddce3c PB |
9156 | LIST_HEAD(list); |
9157 | ||
79ebeaee | 9158 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 9159 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
3dd0c97a | 9160 | if (io_match_task(de->req, task, cancel_all)) { |
b7ddce3c PB |
9161 | list_cut_position(&list, &ctx->defer_list, &de->list); |
9162 | break; | |
9163 | } | |
9164 | } | |
79ebeaee | 9165 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
9166 | if (list_empty(&list)) |
9167 | return false; | |
b7ddce3c PB |
9168 | |
9169 | while (!list_empty(&list)) { | |
9170 | de = list_first_entry(&list, struct io_defer_entry, list); | |
9171 | list_del_init(&de->list); | |
f41db273 | 9172 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
9173 | kfree(de); |
9174 | } | |
e1915f76 | 9175 | return true; |
b7ddce3c PB |
9176 | } |
9177 | ||
1b00764f PB |
9178 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
9179 | { | |
9180 | struct io_tctx_node *node; | |
9181 | enum io_wq_cancel cret; | |
9182 | bool ret = false; | |
9183 | ||
9184 | mutex_lock(&ctx->uring_lock); | |
9185 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
9186 | struct io_uring_task *tctx = node->task->io_uring; | |
9187 | ||
9188 | /* | |
9189 | * io_wq will stay alive while we hold uring_lock, because it's | |
9190 | * killed after ctx nodes, which requires to take the lock. | |
9191 | */ | |
9192 | if (!tctx || !tctx->io_wq) | |
9193 | continue; | |
9194 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
9195 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9196 | } | |
9197 | mutex_unlock(&ctx->uring_lock); | |
9198 | ||
9199 | return ret; | |
9200 | } | |
9201 | ||
9936c7c2 PB |
9202 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
9203 | struct task_struct *task, | |
3dd0c97a | 9204 | bool cancel_all) |
9936c7c2 | 9205 | { |
3dd0c97a | 9206 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 9207 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
9208 | |
9209 | while (1) { | |
9210 | enum io_wq_cancel cret; | |
9211 | bool ret = false; | |
9212 | ||
1b00764f PB |
9213 | if (!task) { |
9214 | ret |= io_uring_try_cancel_iowq(ctx); | |
9215 | } else if (tctx && tctx->io_wq) { | |
9216 | /* | |
9217 | * Cancels requests of all rings, not only @ctx, but | |
9218 | * it's fine as the task is in exit/exec. | |
9219 | */ | |
5aa75ed5 | 9220 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
9221 | &cancel, true); |
9222 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
9223 | } | |
9224 | ||
9225 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 9226 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 9227 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
9936c7c2 PB |
9228 | while (!list_empty_careful(&ctx->iopoll_list)) { |
9229 | io_iopoll_try_reap_events(ctx); | |
9230 | ret = true; | |
9231 | } | |
9232 | } | |
9233 | ||
3dd0c97a PB |
9234 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
9235 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
9236 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
9237 | if (task) |
9238 | ret |= io_run_task_work(); | |
9936c7c2 PB |
9239 | if (!ret) |
9240 | break; | |
9241 | cond_resched(); | |
9242 | } | |
9243 | } | |
9244 | ||
eef51daa | 9245 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 9246 | { |
236434c3 | 9247 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 9248 | struct io_tctx_node *node; |
a528b04e | 9249 | int ret; |
236434c3 MWO |
9250 | |
9251 | if (unlikely(!tctx)) { | |
5aa75ed5 | 9252 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
9253 | if (unlikely(ret)) |
9254 | return ret; | |
236434c3 | 9255 | tctx = current->io_uring; |
0f212204 | 9256 | } |
cf27f3b1 PB |
9257 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
9258 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
9259 | if (!node) | |
9260 | return -ENOMEM; | |
9261 | node->ctx = ctx; | |
9262 | node->task = current; | |
13bf43f5 | 9263 | |
cf27f3b1 PB |
9264 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
9265 | node, GFP_KERNEL)); | |
9266 | if (ret) { | |
9267 | kfree(node); | |
9268 | return ret; | |
0f212204 | 9269 | } |
cf27f3b1 PB |
9270 | |
9271 | mutex_lock(&ctx->uring_lock); | |
9272 | list_add(&node->ctx_node, &ctx->tctx_list); | |
9273 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 9274 | } |
cf27f3b1 | 9275 | tctx->last = ctx; |
0f212204 JA |
9276 | return 0; |
9277 | } | |
9278 | ||
cf27f3b1 PB |
9279 | /* |
9280 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
9281 | */ | |
eef51daa | 9282 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
9283 | { |
9284 | struct io_uring_task *tctx = current->io_uring; | |
9285 | ||
9286 | if (likely(tctx && tctx->last == ctx)) | |
9287 | return 0; | |
eef51daa | 9288 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
9289 | } |
9290 | ||
0f212204 JA |
9291 | /* |
9292 | * Remove this io_uring_file -> task mapping. | |
9293 | */ | |
eef51daa | 9294 | static void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
9295 | { |
9296 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 9297 | struct io_tctx_node *node; |
2941267b | 9298 | |
eebd2e37 PB |
9299 | if (!tctx) |
9300 | return; | |
13bf43f5 PB |
9301 | node = xa_erase(&tctx->xa, index); |
9302 | if (!node) | |
2941267b | 9303 | return; |
0f212204 | 9304 | |
13bf43f5 PB |
9305 | WARN_ON_ONCE(current != node->task); |
9306 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
9307 | ||
9308 | mutex_lock(&node->ctx->uring_lock); | |
9309 | list_del(&node->ctx_node); | |
9310 | mutex_unlock(&node->ctx->uring_lock); | |
9311 | ||
baf186c4 | 9312 | if (tctx->last == node->ctx) |
0f212204 | 9313 | tctx->last = NULL; |
13bf43f5 | 9314 | kfree(node); |
0f212204 JA |
9315 | } |
9316 | ||
8452d4a6 | 9317 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 9318 | { |
ba5ef6dc | 9319 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 9320 | struct io_tctx_node *node; |
de7f1d9e PB |
9321 | unsigned long index; |
9322 | ||
13bf43f5 | 9323 | xa_for_each(&tctx->xa, index, node) |
eef51daa | 9324 | io_uring_del_tctx_node(index); |
b16ef427 ME |
9325 | if (wq) { |
9326 | /* | |
9327 | * Must be after io_uring_del_task_file() (removes nodes under | |
9328 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). | |
9329 | */ | |
ba5ef6dc | 9330 | io_wq_put_and_exit(wq); |
dadebc35 | 9331 | tctx->io_wq = NULL; |
b16ef427 | 9332 | } |
de7f1d9e PB |
9333 | } |
9334 | ||
3f48cf18 | 9335 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 9336 | { |
3f48cf18 PB |
9337 | if (tracked) |
9338 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
9339 | return percpu_counter_sum(&tctx->inflight); |
9340 | } | |
9341 | ||
09899b19 PB |
9342 | static void io_uring_drop_tctx_refs(struct task_struct *task) |
9343 | { | |
9344 | struct io_uring_task *tctx = task->io_uring; | |
9345 | unsigned int refs = tctx->cached_refs; | |
9346 | ||
e9dbe221 PB |
9347 | if (refs) { |
9348 | tctx->cached_refs = 0; | |
9349 | percpu_counter_sub(&tctx->inflight, refs); | |
9350 | put_task_struct_many(task, refs); | |
9351 | } | |
09899b19 PB |
9352 | } |
9353 | ||
78cc687b PB |
9354 | /* |
9355 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
9356 | * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation. | |
9357 | */ | |
9358 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) | |
0e9ddb39 | 9359 | { |
521d6a73 | 9360 | struct io_uring_task *tctx = current->io_uring; |
734551df | 9361 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
9362 | s64 inflight; |
9363 | DEFINE_WAIT(wait); | |
fdaf083c | 9364 | |
78cc687b PB |
9365 | WARN_ON_ONCE(sqd && sqd->thread != current); |
9366 | ||
6d042ffb PO |
9367 | if (!current->io_uring) |
9368 | return; | |
17a91051 PB |
9369 | if (tctx->io_wq) |
9370 | io_wq_exit_start(tctx->io_wq); | |
9371 | ||
0e9ddb39 PB |
9372 | atomic_inc(&tctx->in_idle); |
9373 | do { | |
e9dbe221 | 9374 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 9375 | /* read completions before cancelations */ |
78cc687b | 9376 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
9377 | if (!inflight) |
9378 | break; | |
fdaf083c | 9379 | |
78cc687b PB |
9380 | if (!sqd) { |
9381 | struct io_tctx_node *node; | |
9382 | unsigned long index; | |
0f212204 | 9383 | |
78cc687b PB |
9384 | xa_for_each(&tctx->xa, index, node) { |
9385 | /* sqpoll task will cancel all its requests */ | |
9386 | if (node->ctx->sq_data) | |
9387 | continue; | |
9388 | io_uring_try_cancel_requests(node->ctx, current, | |
9389 | cancel_all); | |
9390 | } | |
9391 | } else { | |
9392 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
9393 | io_uring_try_cancel_requests(ctx, current, | |
9394 | cancel_all); | |
9395 | } | |
17a91051 | 9396 | |
0f212204 | 9397 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
e9dbe221 | 9398 | io_uring_drop_tctx_refs(current); |
0f212204 | 9399 | /* |
a1bb3cd5 PB |
9400 | * If we've seen completions, retry without waiting. This |
9401 | * avoids a race where a completion comes in before we did | |
9402 | * prepare_to_wait(). | |
0f212204 | 9403 | */ |
3dd0c97a | 9404 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 9405 | schedule(); |
f57555ed | 9406 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 9407 | } while (1); |
fdaf083c | 9408 | atomic_dec(&tctx->in_idle); |
de7f1d9e | 9409 | |
8452d4a6 | 9410 | io_uring_clean_tctx(tctx); |
3dd0c97a | 9411 | if (cancel_all) { |
3f48cf18 PB |
9412 | /* for exec all current's requests should be gone, kill tctx */ |
9413 | __io_uring_free(current); | |
9414 | } | |
44e728b8 PB |
9415 | } |
9416 | ||
f552a27a | 9417 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 9418 | { |
f552a27a | 9419 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
9420 | } |
9421 | ||
6c5c240e RP |
9422 | static void *io_uring_validate_mmap_request(struct file *file, |
9423 | loff_t pgoff, size_t sz) | |
2b188cc1 | 9424 | { |
2b188cc1 | 9425 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 9426 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
9427 | struct page *page; |
9428 | void *ptr; | |
9429 | ||
9430 | switch (offset) { | |
9431 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
9432 | case IORING_OFF_CQ_RING: |
9433 | ptr = ctx->rings; | |
2b188cc1 JA |
9434 | break; |
9435 | case IORING_OFF_SQES: | |
9436 | ptr = ctx->sq_sqes; | |
9437 | break; | |
2b188cc1 | 9438 | default: |
6c5c240e | 9439 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
9440 | } |
9441 | ||
9442 | page = virt_to_head_page(ptr); | |
a50b854e | 9443 | if (sz > page_size(page)) |
6c5c240e RP |
9444 | return ERR_PTR(-EINVAL); |
9445 | ||
9446 | return ptr; | |
9447 | } | |
9448 | ||
9449 | #ifdef CONFIG_MMU | |
9450 | ||
9451 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9452 | { | |
9453 | size_t sz = vma->vm_end - vma->vm_start; | |
9454 | unsigned long pfn; | |
9455 | void *ptr; | |
9456 | ||
9457 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
9458 | if (IS_ERR(ptr)) | |
9459 | return PTR_ERR(ptr); | |
2b188cc1 JA |
9460 | |
9461 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
9462 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
9463 | } | |
9464 | ||
6c5c240e RP |
9465 | #else /* !CONFIG_MMU */ |
9466 | ||
9467 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
9468 | { | |
9469 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
9470 | } | |
9471 | ||
9472 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
9473 | { | |
9474 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
9475 | } | |
9476 | ||
9477 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
9478 | unsigned long addr, unsigned long len, | |
9479 | unsigned long pgoff, unsigned long flags) | |
9480 | { | |
9481 | void *ptr; | |
9482 | ||
9483 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
9484 | if (IS_ERR(ptr)) | |
9485 | return PTR_ERR(ptr); | |
9486 | ||
9487 | return (unsigned long) ptr; | |
9488 | } | |
9489 | ||
9490 | #endif /* !CONFIG_MMU */ | |
9491 | ||
d9d05217 | 9492 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
9493 | { |
9494 | DEFINE_WAIT(wait); | |
9495 | ||
9496 | do { | |
9497 | if (!io_sqring_full(ctx)) | |
9498 | break; | |
90554200 JA |
9499 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
9500 | ||
9501 | if (!io_sqring_full(ctx)) | |
9502 | break; | |
90554200 JA |
9503 | schedule(); |
9504 | } while (!signal_pending(current)); | |
9505 | ||
9506 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 9507 | return 0; |
90554200 JA |
9508 | } |
9509 | ||
c73ebb68 HX |
9510 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
9511 | struct __kernel_timespec __user **ts, | |
9512 | const sigset_t __user **sig) | |
9513 | { | |
9514 | struct io_uring_getevents_arg arg; | |
9515 | ||
9516 | /* | |
9517 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
9518 | * is just a pointer to the sigset_t. | |
9519 | */ | |
9520 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
9521 | *sig = (const sigset_t __user *) argp; | |
9522 | *ts = NULL; | |
9523 | return 0; | |
9524 | } | |
9525 | ||
9526 | /* | |
9527 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
9528 | * timespec and sigset_t pointers if good. | |
9529 | */ | |
9530 | if (*argsz != sizeof(arg)) | |
9531 | return -EINVAL; | |
9532 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
9533 | return -EFAULT; | |
9534 | *sig = u64_to_user_ptr(arg.sigmask); | |
9535 | *argsz = arg.sigmask_sz; | |
9536 | *ts = u64_to_user_ptr(arg.ts); | |
9537 | return 0; | |
9538 | } | |
9539 | ||
2b188cc1 | 9540 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
9541 | u32, min_complete, u32, flags, const void __user *, argp, |
9542 | size_t, argsz) | |
2b188cc1 JA |
9543 | { |
9544 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
9545 | int submitted = 0; |
9546 | struct fd f; | |
33f993da | 9547 | long ret; |
2b188cc1 | 9548 | |
4c6e277c | 9549 | io_run_task_work(); |
b41e9852 | 9550 | |
33f993da PB |
9551 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
9552 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) | |
2b188cc1 JA |
9553 | return -EINVAL; |
9554 | ||
9555 | f = fdget(fd); | |
33f993da | 9556 | if (unlikely(!f.file)) |
2b188cc1 JA |
9557 | return -EBADF; |
9558 | ||
9559 | ret = -EOPNOTSUPP; | |
33f993da | 9560 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
9561 | goto out_fput; |
9562 | ||
9563 | ret = -ENXIO; | |
9564 | ctx = f.file->private_data; | |
33f993da | 9565 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
9566 | goto out_fput; |
9567 | ||
7e84e1c7 | 9568 | ret = -EBADFD; |
33f993da | 9569 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
9570 | goto out; |
9571 | ||
6c271ce2 JA |
9572 | /* |
9573 | * For SQ polling, the thread will do all submissions and completions. | |
9574 | * Just return the requested submit count, and wake the thread if | |
9575 | * we were asked to. | |
9576 | */ | |
b2a9eada | 9577 | ret = 0; |
6c271ce2 | 9578 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 9579 | io_cqring_overflow_flush(ctx); |
89448c47 | 9580 | |
21f96522 JA |
9581 | if (unlikely(ctx->sq_data->thread == NULL)) { |
9582 | ret = -EOWNERDEAD; | |
04147488 | 9583 | goto out; |
21f96522 | 9584 | } |
6c271ce2 | 9585 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 9586 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
9587 | if (flags & IORING_ENTER_SQ_WAIT) { |
9588 | ret = io_sqpoll_wait_sq(ctx); | |
9589 | if (ret) | |
9590 | goto out; | |
9591 | } | |
6c271ce2 | 9592 | submitted = to_submit; |
b2a9eada | 9593 | } else if (to_submit) { |
eef51daa | 9594 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
9595 | if (unlikely(ret)) |
9596 | goto out; | |
2b188cc1 | 9597 | mutex_lock(&ctx->uring_lock); |
0f212204 | 9598 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 9599 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
9600 | |
9601 | if (submitted != to_submit) | |
9602 | goto out; | |
2b188cc1 JA |
9603 | } |
9604 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
9605 | const sigset_t __user *sig; |
9606 | struct __kernel_timespec __user *ts; | |
9607 | ||
9608 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
9609 | if (unlikely(ret)) | |
9610 | goto out; | |
9611 | ||
2b188cc1 JA |
9612 | min_complete = min(min_complete, ctx->cq_entries); |
9613 | ||
32b2244a XW |
9614 | /* |
9615 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
9616 | * space applications don't need to do io completion events | |
9617 | * polling again, they can rely on io_sq_thread to do polling | |
9618 | * work, which can reduce cpu usage and uring_lock contention. | |
9619 | */ | |
9620 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
9621 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 9622 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 9623 | } else { |
c73ebb68 | 9624 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 9625 | } |
2b188cc1 JA |
9626 | } |
9627 | ||
7c504e65 | 9628 | out: |
6805b32e | 9629 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
9630 | out_fput: |
9631 | fdput(f); | |
9632 | return submitted ? submitted : ret; | |
9633 | } | |
9634 | ||
bebdb65e | 9635 | #ifdef CONFIG_PROC_FS |
61cf9370 MWO |
9636 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
9637 | const struct cred *cred) | |
87ce955b | 9638 | { |
87ce955b JA |
9639 | struct user_namespace *uns = seq_user_ns(m); |
9640 | struct group_info *gi; | |
9641 | kernel_cap_t cap; | |
9642 | unsigned __capi; | |
9643 | int g; | |
9644 | ||
9645 | seq_printf(m, "%5d\n", id); | |
9646 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
9647 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
9648 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
9649 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
9650 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
9651 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
9652 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
9653 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
9654 | seq_puts(m, "\n\tGroups:\t"); | |
9655 | gi = cred->group_info; | |
9656 | for (g = 0; g < gi->ngroups; g++) { | |
9657 | seq_put_decimal_ull(m, g ? " " : "", | |
9658 | from_kgid_munged(uns, gi->gid[g])); | |
9659 | } | |
9660 | seq_puts(m, "\n\tCapEff:\t"); | |
9661 | cap = cred->cap_effective; | |
9662 | CAP_FOR_EACH_U32(__capi) | |
9663 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
9664 | seq_putc(m, '\n'); | |
9665 | return 0; | |
9666 | } | |
9667 | ||
9668 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
9669 | { | |
dbbe9c64 | 9670 | struct io_sq_data *sq = NULL; |
fad8e0de | 9671 | bool has_lock; |
87ce955b JA |
9672 | int i; |
9673 | ||
fad8e0de JA |
9674 | /* |
9675 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
9676 | * since fdinfo case grabs it in the opposite direction of normal use | |
9677 | * cases. If we fail to get the lock, we just don't iterate any | |
9678 | * structures that could be going away outside the io_uring mutex. | |
9679 | */ | |
9680 | has_lock = mutex_trylock(&ctx->uring_lock); | |
9681 | ||
5f3f26f9 | 9682 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 9683 | sq = ctx->sq_data; |
5f3f26f9 JA |
9684 | if (!sq->thread) |
9685 | sq = NULL; | |
9686 | } | |
dbbe9c64 JQ |
9687 | |
9688 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
9689 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 9690 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 9691 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 9692 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 9693 | |
87ce955b JA |
9694 | if (f) |
9695 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
9696 | else | |
9697 | seq_printf(m, "%5u: <none>\n", i); | |
9698 | } | |
9699 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 9700 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 9701 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 9702 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 9703 | |
4751f53d | 9704 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 9705 | } |
61cf9370 MWO |
9706 | if (has_lock && !xa_empty(&ctx->personalities)) { |
9707 | unsigned long index; | |
9708 | const struct cred *cred; | |
9709 | ||
87ce955b | 9710 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
9711 | xa_for_each(&ctx->personalities, index, cred) |
9712 | io_uring_show_cred(m, index, cred); | |
87ce955b | 9713 | } |
d7718a9d | 9714 | seq_printf(m, "PollList:\n"); |
79ebeaee | 9715 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
9716 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
9717 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
9718 | struct io_kiocb *req; | |
9719 | ||
9720 | hlist_for_each_entry(req, list, hash_node) | |
9721 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
9722 | req->task->task_works != NULL); | |
9723 | } | |
79ebeaee | 9724 | spin_unlock(&ctx->completion_lock); |
fad8e0de JA |
9725 | if (has_lock) |
9726 | mutex_unlock(&ctx->uring_lock); | |
87ce955b JA |
9727 | } |
9728 | ||
9729 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
9730 | { | |
9731 | struct io_ring_ctx *ctx = f->private_data; | |
9732 | ||
9733 | if (percpu_ref_tryget(&ctx->refs)) { | |
9734 | __io_uring_show_fdinfo(ctx, m); | |
9735 | percpu_ref_put(&ctx->refs); | |
9736 | } | |
9737 | } | |
bebdb65e | 9738 | #endif |
87ce955b | 9739 | |
2b188cc1 JA |
9740 | static const struct file_operations io_uring_fops = { |
9741 | .release = io_uring_release, | |
9742 | .mmap = io_uring_mmap, | |
6c5c240e RP |
9743 | #ifndef CONFIG_MMU |
9744 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
9745 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
9746 | #endif | |
2b188cc1 JA |
9747 | .poll = io_uring_poll, |
9748 | .fasync = io_uring_fasync, | |
bebdb65e | 9749 | #ifdef CONFIG_PROC_FS |
87ce955b | 9750 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 9751 | #endif |
2b188cc1 JA |
9752 | }; |
9753 | ||
9754 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
9755 | struct io_uring_params *p) | |
9756 | { | |
75b28aff HV |
9757 | struct io_rings *rings; |
9758 | size_t size, sq_array_offset; | |
2b188cc1 | 9759 | |
bd740481 JA |
9760 | /* make sure these are sane, as we already accounted them */ |
9761 | ctx->sq_entries = p->sq_entries; | |
9762 | ctx->cq_entries = p->cq_entries; | |
9763 | ||
75b28aff HV |
9764 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
9765 | if (size == SIZE_MAX) | |
9766 | return -EOVERFLOW; | |
9767 | ||
9768 | rings = io_mem_alloc(size); | |
9769 | if (!rings) | |
2b188cc1 JA |
9770 | return -ENOMEM; |
9771 | ||
75b28aff HV |
9772 | ctx->rings = rings; |
9773 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
9774 | rings->sq_ring_mask = p->sq_entries - 1; | |
9775 | rings->cq_ring_mask = p->cq_entries - 1; | |
9776 | rings->sq_ring_entries = p->sq_entries; | |
9777 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
9778 | |
9779 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
9780 | if (size == SIZE_MAX) { |
9781 | io_mem_free(ctx->rings); | |
9782 | ctx->rings = NULL; | |
2b188cc1 | 9783 | return -EOVERFLOW; |
eb065d30 | 9784 | } |
2b188cc1 JA |
9785 | |
9786 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
9787 | if (!ctx->sq_sqes) { |
9788 | io_mem_free(ctx->rings); | |
9789 | ctx->rings = NULL; | |
2b188cc1 | 9790 | return -ENOMEM; |
eb065d30 | 9791 | } |
2b188cc1 | 9792 | |
2b188cc1 JA |
9793 | return 0; |
9794 | } | |
9795 | ||
9faadcc8 PB |
9796 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
9797 | { | |
9798 | int ret, fd; | |
9799 | ||
9800 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
9801 | if (fd < 0) | |
9802 | return fd; | |
9803 | ||
eef51daa | 9804 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
9805 | if (ret) { |
9806 | put_unused_fd(fd); | |
9807 | return ret; | |
9808 | } | |
9809 | fd_install(fd, file); | |
9810 | return fd; | |
9811 | } | |
9812 | ||
2b188cc1 JA |
9813 | /* |
9814 | * Allocate an anonymous fd, this is what constitutes the application | |
9815 | * visible backing of an io_uring instance. The application mmaps this | |
9816 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
9817 | * we have to tie this fd to a socket for file garbage collection purposes. | |
9818 | */ | |
9faadcc8 | 9819 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
9820 | { |
9821 | struct file *file; | |
9faadcc8 | 9822 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
9823 | int ret; |
9824 | ||
2b188cc1 JA |
9825 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
9826 | &ctx->ring_sock); | |
9827 | if (ret) | |
9faadcc8 | 9828 | return ERR_PTR(ret); |
2b188cc1 JA |
9829 | #endif |
9830 | ||
2b188cc1 JA |
9831 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
9832 | O_RDWR | O_CLOEXEC); | |
2b188cc1 | 9833 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
9834 | if (IS_ERR(file)) { |
9835 | sock_release(ctx->ring_sock); | |
9836 | ctx->ring_sock = NULL; | |
9837 | } else { | |
9838 | ctx->ring_sock->file = file; | |
0f212204 | 9839 | } |
2b188cc1 | 9840 | #endif |
9faadcc8 | 9841 | return file; |
2b188cc1 JA |
9842 | } |
9843 | ||
7f13657d XW |
9844 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
9845 | struct io_uring_params __user *params) | |
2b188cc1 | 9846 | { |
2b188cc1 | 9847 | struct io_ring_ctx *ctx; |
9faadcc8 | 9848 | struct file *file; |
2b188cc1 JA |
9849 | int ret; |
9850 | ||
8110c1a6 | 9851 | if (!entries) |
2b188cc1 | 9852 | return -EINVAL; |
8110c1a6 JA |
9853 | if (entries > IORING_MAX_ENTRIES) { |
9854 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9855 | return -EINVAL; | |
9856 | entries = IORING_MAX_ENTRIES; | |
9857 | } | |
2b188cc1 JA |
9858 | |
9859 | /* | |
9860 | * Use twice as many entries for the CQ ring. It's possible for the | |
9861 | * application to drive a higher depth than the size of the SQ ring, | |
9862 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
9863 | * some flexibility in overcommitting a bit. If the application has |
9864 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
9865 | * of CQ ring entries manually. | |
2b188cc1 JA |
9866 | */ |
9867 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
9868 | if (p->flags & IORING_SETUP_CQSIZE) { |
9869 | /* | |
9870 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
9871 | * to a power-of-two, if it isn't already. We do NOT impose | |
9872 | * any cq vs sq ring sizing. | |
9873 | */ | |
eb2667b3 | 9874 | if (!p->cq_entries) |
33a107f0 | 9875 | return -EINVAL; |
8110c1a6 JA |
9876 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
9877 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
9878 | return -EINVAL; | |
9879 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
9880 | } | |
eb2667b3 JQ |
9881 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
9882 | if (p->cq_entries < p->sq_entries) | |
9883 | return -EINVAL; | |
33a107f0 JA |
9884 | } else { |
9885 | p->cq_entries = 2 * p->sq_entries; | |
9886 | } | |
2b188cc1 | 9887 | |
2b188cc1 | 9888 | ctx = io_ring_ctx_alloc(p); |
62e398be | 9889 | if (!ctx) |
2b188cc1 | 9890 | return -ENOMEM; |
2b188cc1 | 9891 | ctx->compat = in_compat_syscall(); |
62e398be JA |
9892 | if (!capable(CAP_IPC_LOCK)) |
9893 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
9894 | |
9895 | /* | |
9896 | * This is just grabbed for accounting purposes. When a process exits, | |
9897 | * the mm is exited and dropped before the files, hence we need to hang | |
9898 | * on to this mm purely for the purposes of being able to unaccount | |
9899 | * memory (locked/pinned vm). It's not used for anything else. | |
9900 | */ | |
6b7898eb | 9901 | mmgrab(current->mm); |
2aede0e4 | 9902 | ctx->mm_account = current->mm; |
6b7898eb | 9903 | |
2b188cc1 JA |
9904 | ret = io_allocate_scq_urings(ctx, p); |
9905 | if (ret) | |
9906 | goto err; | |
9907 | ||
7e84e1c7 | 9908 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
9909 | if (ret) |
9910 | goto err; | |
eae071c9 | 9911 | /* always set a rsrc node */ |
47b228ce PB |
9912 | ret = io_rsrc_node_switch_start(ctx); |
9913 | if (ret) | |
9914 | goto err; | |
eae071c9 | 9915 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 9916 | |
2b188cc1 | 9917 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
9918 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
9919 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
9920 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
9921 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
9922 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
9923 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
9924 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
9925 | |
9926 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
9927 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
9928 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
9929 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
9930 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
9931 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
9932 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 9933 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 9934 | |
7f13657d XW |
9935 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
9936 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 9937 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 9938 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e PB |
9939 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
9940 | IORING_FEAT_RSRC_TAGS; | |
7f13657d XW |
9941 | |
9942 | if (copy_to_user(params, p, sizeof(*p))) { | |
9943 | ret = -EFAULT; | |
9944 | goto err; | |
9945 | } | |
d1719f70 | 9946 | |
9faadcc8 PB |
9947 | file = io_uring_get_file(ctx); |
9948 | if (IS_ERR(file)) { | |
9949 | ret = PTR_ERR(file); | |
9950 | goto err; | |
9951 | } | |
9952 | ||
044c1ab3 JA |
9953 | /* |
9954 | * Install ring fd as the very last thing, so we don't risk someone | |
9955 | * having closed it before we finish setup | |
9956 | */ | |
9faadcc8 PB |
9957 | ret = io_uring_install_fd(ctx, file); |
9958 | if (ret < 0) { | |
9959 | /* fput will clean it up */ | |
9960 | fput(file); | |
9961 | return ret; | |
9962 | } | |
044c1ab3 | 9963 | |
c826bd7a | 9964 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
9965 | return ret; |
9966 | err: | |
9967 | io_ring_ctx_wait_and_kill(ctx); | |
9968 | return ret; | |
9969 | } | |
9970 | ||
9971 | /* | |
9972 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
9973 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
9974 | * params structure passed in. | |
9975 | */ | |
9976 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
9977 | { | |
9978 | struct io_uring_params p; | |
2b188cc1 JA |
9979 | int i; |
9980 | ||
9981 | if (copy_from_user(&p, params, sizeof(p))) | |
9982 | return -EFAULT; | |
9983 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
9984 | if (p.resv[i]) | |
9985 | return -EINVAL; | |
9986 | } | |
9987 | ||
6c271ce2 | 9988 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 9989 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 SG |
9990 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
9991 | IORING_SETUP_R_DISABLED)) | |
2b188cc1 JA |
9992 | return -EINVAL; |
9993 | ||
7f13657d | 9994 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
9995 | } |
9996 | ||
9997 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
9998 | struct io_uring_params __user *, params) | |
9999 | { | |
10000 | return io_uring_setup(entries, params); | |
10001 | } | |
10002 | ||
66f4af93 JA |
10003 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
10004 | { | |
10005 | struct io_uring_probe *p; | |
10006 | size_t size; | |
10007 | int i, ret; | |
10008 | ||
10009 | size = struct_size(p, ops, nr_args); | |
10010 | if (size == SIZE_MAX) | |
10011 | return -EOVERFLOW; | |
10012 | p = kzalloc(size, GFP_KERNEL); | |
10013 | if (!p) | |
10014 | return -ENOMEM; | |
10015 | ||
10016 | ret = -EFAULT; | |
10017 | if (copy_from_user(p, arg, size)) | |
10018 | goto out; | |
10019 | ret = -EINVAL; | |
10020 | if (memchr_inv(p, 0, size)) | |
10021 | goto out; | |
10022 | ||
10023 | p->last_op = IORING_OP_LAST - 1; | |
10024 | if (nr_args > IORING_OP_LAST) | |
10025 | nr_args = IORING_OP_LAST; | |
10026 | ||
10027 | for (i = 0; i < nr_args; i++) { | |
10028 | p->ops[i].op = i; | |
10029 | if (!io_op_defs[i].not_supported) | |
10030 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
10031 | } | |
10032 | p->ops_len = i; | |
10033 | ||
10034 | ret = 0; | |
10035 | if (copy_to_user(arg, p, size)) | |
10036 | ret = -EFAULT; | |
10037 | out: | |
10038 | kfree(p); | |
10039 | return ret; | |
10040 | } | |
10041 | ||
071698e1 JA |
10042 | static int io_register_personality(struct io_ring_ctx *ctx) |
10043 | { | |
4379bf8b | 10044 | const struct cred *creds; |
61cf9370 | 10045 | u32 id; |
1e6fa521 | 10046 | int ret; |
071698e1 | 10047 | |
4379bf8b | 10048 | creds = get_current_cred(); |
1e6fa521 | 10049 | |
61cf9370 MWO |
10050 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
10051 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
10052 | if (ret < 0) { |
10053 | put_cred(creds); | |
10054 | return ret; | |
10055 | } | |
10056 | return id; | |
071698e1 JA |
10057 | } |
10058 | ||
21b55dbc SG |
10059 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
10060 | unsigned int nr_args) | |
10061 | { | |
10062 | struct io_uring_restriction *res; | |
10063 | size_t size; | |
10064 | int i, ret; | |
10065 | ||
7e84e1c7 SG |
10066 | /* Restrictions allowed only if rings started disabled */ |
10067 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10068 | return -EBADFD; | |
10069 | ||
21b55dbc | 10070 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 10071 | if (ctx->restrictions.registered) |
21b55dbc SG |
10072 | return -EBUSY; |
10073 | ||
10074 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
10075 | return -EINVAL; | |
10076 | ||
10077 | size = array_size(nr_args, sizeof(*res)); | |
10078 | if (size == SIZE_MAX) | |
10079 | return -EOVERFLOW; | |
10080 | ||
10081 | res = memdup_user(arg, size); | |
10082 | if (IS_ERR(res)) | |
10083 | return PTR_ERR(res); | |
10084 | ||
10085 | ret = 0; | |
10086 | ||
10087 | for (i = 0; i < nr_args; i++) { | |
10088 | switch (res[i].opcode) { | |
10089 | case IORING_RESTRICTION_REGISTER_OP: | |
10090 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
10091 | ret = -EINVAL; | |
10092 | goto out; | |
10093 | } | |
10094 | ||
10095 | __set_bit(res[i].register_op, | |
10096 | ctx->restrictions.register_op); | |
10097 | break; | |
10098 | case IORING_RESTRICTION_SQE_OP: | |
10099 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
10100 | ret = -EINVAL; | |
10101 | goto out; | |
10102 | } | |
10103 | ||
10104 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
10105 | break; | |
10106 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
10107 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
10108 | break; | |
10109 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
10110 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
10111 | break; | |
10112 | default: | |
10113 | ret = -EINVAL; | |
10114 | goto out; | |
10115 | } | |
10116 | } | |
10117 | ||
10118 | out: | |
10119 | /* Reset all restrictions if an error happened */ | |
10120 | if (ret != 0) | |
10121 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
10122 | else | |
7e84e1c7 | 10123 | ctx->restrictions.registered = true; |
21b55dbc SG |
10124 | |
10125 | kfree(res); | |
10126 | return ret; | |
10127 | } | |
10128 | ||
7e84e1c7 SG |
10129 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
10130 | { | |
10131 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
10132 | return -EBADFD; | |
10133 | ||
10134 | if (ctx->restrictions.registered) | |
10135 | ctx->restricted = 1; | |
10136 | ||
0298ef96 PB |
10137 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
10138 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
10139 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
10140 | return 0; |
10141 | } | |
10142 | ||
fdecb662 | 10143 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 10144 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
10145 | unsigned nr_args) |
10146 | { | |
10147 | __u32 tmp; | |
10148 | int err; | |
10149 | ||
c3bdad02 PB |
10150 | if (up->resv) |
10151 | return -EINVAL; | |
98f0b3b4 PB |
10152 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
10153 | return -EOVERFLOW; | |
10154 | err = io_rsrc_node_switch_start(ctx); | |
10155 | if (err) | |
10156 | return err; | |
10157 | ||
fdecb662 PB |
10158 | switch (type) { |
10159 | case IORING_RSRC_FILE: | |
98f0b3b4 | 10160 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
10161 | case IORING_RSRC_BUFFER: |
10162 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
10163 | } |
10164 | return -EINVAL; | |
10165 | } | |
10166 | ||
c3bdad02 PB |
10167 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
10168 | unsigned nr_args) | |
98f0b3b4 | 10169 | { |
c3bdad02 | 10170 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
10171 | |
10172 | if (!nr_args) | |
10173 | return -EINVAL; | |
c3bdad02 PB |
10174 | memset(&up, 0, sizeof(up)); |
10175 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
10176 | return -EFAULT; | |
10177 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
10178 | } | |
10179 | ||
10180 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 10181 | unsigned size, unsigned type) |
c3bdad02 PB |
10182 | { |
10183 | struct io_uring_rsrc_update2 up; | |
10184 | ||
10185 | if (size != sizeof(up)) | |
10186 | return -EINVAL; | |
98f0b3b4 PB |
10187 | if (copy_from_user(&up, arg, sizeof(up))) |
10188 | return -EFAULT; | |
992da01a | 10189 | if (!up.nr || up.resv) |
98f0b3b4 | 10190 | return -EINVAL; |
992da01a | 10191 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
10192 | } |
10193 | ||
792e3582 | 10194 | static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 10195 | unsigned int size, unsigned int type) |
792e3582 PB |
10196 | { |
10197 | struct io_uring_rsrc_register rr; | |
10198 | ||
10199 | /* keep it extendible */ | |
10200 | if (size != sizeof(rr)) | |
10201 | return -EINVAL; | |
10202 | ||
10203 | memset(&rr, 0, sizeof(rr)); | |
10204 | if (copy_from_user(&rr, arg, size)) | |
10205 | return -EFAULT; | |
992da01a | 10206 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
10207 | return -EINVAL; |
10208 | ||
992da01a | 10209 | switch (type) { |
792e3582 PB |
10210 | case IORING_RSRC_FILE: |
10211 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
10212 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
10213 | case IORING_RSRC_BUFFER: |
10214 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
10215 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
10216 | } |
10217 | return -EINVAL; | |
10218 | } | |
10219 | ||
fe76421d JA |
10220 | static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg, |
10221 | unsigned len) | |
10222 | { | |
10223 | struct io_uring_task *tctx = current->io_uring; | |
10224 | cpumask_var_t new_mask; | |
10225 | int ret; | |
10226 | ||
10227 | if (!tctx || !tctx->io_wq) | |
10228 | return -EINVAL; | |
10229 | ||
10230 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
10231 | return -ENOMEM; | |
10232 | ||
10233 | cpumask_clear(new_mask); | |
10234 | if (len > cpumask_size()) | |
10235 | len = cpumask_size(); | |
10236 | ||
10237 | if (copy_from_user(new_mask, arg, len)) { | |
10238 | free_cpumask_var(new_mask); | |
10239 | return -EFAULT; | |
10240 | } | |
10241 | ||
10242 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
10243 | free_cpumask_var(new_mask); | |
10244 | return ret; | |
10245 | } | |
10246 | ||
10247 | static int io_unregister_iowq_aff(struct io_ring_ctx *ctx) | |
10248 | { | |
10249 | struct io_uring_task *tctx = current->io_uring; | |
10250 | ||
10251 | if (!tctx || !tctx->io_wq) | |
10252 | return -EINVAL; | |
10253 | ||
10254 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
10255 | } | |
10256 | ||
2e480058 JA |
10257 | static int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
10258 | void __user *arg) | |
10259 | { | |
10260 | struct io_uring_task *tctx = current->io_uring; | |
10261 | __u32 new_count[2]; | |
10262 | int i, ret; | |
10263 | ||
10264 | if (!tctx || !tctx->io_wq) | |
10265 | return -EINVAL; | |
10266 | if (copy_from_user(new_count, arg, sizeof(new_count))) | |
10267 | return -EFAULT; | |
10268 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
10269 | if (new_count[i] > INT_MAX) | |
10270 | return -EINVAL; | |
10271 | ||
10272 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
10273 | if (ret) | |
10274 | return ret; | |
10275 | ||
10276 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
10277 | return -EFAULT; | |
10278 | ||
10279 | return 0; | |
10280 | } | |
10281 | ||
071698e1 JA |
10282 | static bool io_register_op_must_quiesce(int op) |
10283 | { | |
10284 | switch (op) { | |
bd54b6fe BM |
10285 | case IORING_REGISTER_BUFFERS: |
10286 | case IORING_UNREGISTER_BUFFERS: | |
f4f7d21c | 10287 | case IORING_REGISTER_FILES: |
071698e1 JA |
10288 | case IORING_UNREGISTER_FILES: |
10289 | case IORING_REGISTER_FILES_UPDATE: | |
10290 | case IORING_REGISTER_PROBE: | |
10291 | case IORING_REGISTER_PERSONALITY: | |
10292 | case IORING_UNREGISTER_PERSONALITY: | |
992da01a PB |
10293 | case IORING_REGISTER_FILES2: |
10294 | case IORING_REGISTER_FILES_UPDATE2: | |
10295 | case IORING_REGISTER_BUFFERS2: | |
10296 | case IORING_REGISTER_BUFFERS_UPDATE: | |
fe76421d JA |
10297 | case IORING_REGISTER_IOWQ_AFF: |
10298 | case IORING_UNREGISTER_IOWQ_AFF: | |
2e480058 | 10299 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
071698e1 JA |
10300 | return false; |
10301 | default: | |
10302 | return true; | |
10303 | } | |
10304 | } | |
10305 | ||
e73c5c7c PB |
10306 | static int io_ctx_quiesce(struct io_ring_ctx *ctx) |
10307 | { | |
10308 | long ret; | |
10309 | ||
10310 | percpu_ref_kill(&ctx->refs); | |
10311 | ||
10312 | /* | |
10313 | * Drop uring mutex before waiting for references to exit. If another | |
10314 | * thread is currently inside io_uring_enter() it might need to grab the | |
10315 | * uring_lock to make progress. If we hold it here across the drain | |
10316 | * wait, then we can deadlock. It's safe to drop the mutex here, since | |
10317 | * no new references will come in after we've killed the percpu ref. | |
10318 | */ | |
10319 | mutex_unlock(&ctx->uring_lock); | |
10320 | do { | |
10321 | ret = wait_for_completion_interruptible(&ctx->ref_comp); | |
10322 | if (!ret) | |
10323 | break; | |
10324 | ret = io_run_task_work_sig(); | |
10325 | } while (ret >= 0); | |
10326 | mutex_lock(&ctx->uring_lock); | |
10327 | ||
10328 | if (ret) | |
10329 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); | |
10330 | return ret; | |
10331 | } | |
10332 | ||
edafccee JA |
10333 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
10334 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
10335 | __releases(ctx->uring_lock) |
10336 | __acquires(ctx->uring_lock) | |
edafccee JA |
10337 | { |
10338 | int ret; | |
10339 | ||
35fa71a0 JA |
10340 | /* |
10341 | * We're inside the ring mutex, if the ref is already dying, then | |
10342 | * someone else killed the ctx or is already going through | |
10343 | * io_uring_register(). | |
10344 | */ | |
10345 | if (percpu_ref_is_dying(&ctx->refs)) | |
10346 | return -ENXIO; | |
10347 | ||
75c4021a PB |
10348 | if (ctx->restricted) { |
10349 | if (opcode >= IORING_REGISTER_LAST) | |
10350 | return -EINVAL; | |
10351 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
10352 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
10353 | return -EACCES; | |
10354 | } | |
10355 | ||
071698e1 | 10356 | if (io_register_op_must_quiesce(opcode)) { |
e73c5c7c PB |
10357 | ret = io_ctx_quiesce(ctx); |
10358 | if (ret) | |
f70865db | 10359 | return ret; |
05f3fb3c | 10360 | } |
edafccee JA |
10361 | |
10362 | switch (opcode) { | |
10363 | case IORING_REGISTER_BUFFERS: | |
634d00df | 10364 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
10365 | break; |
10366 | case IORING_UNREGISTER_BUFFERS: | |
10367 | ret = -EINVAL; | |
10368 | if (arg || nr_args) | |
10369 | break; | |
0a96bbe4 | 10370 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 10371 | break; |
6b06314c | 10372 | case IORING_REGISTER_FILES: |
792e3582 | 10373 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
10374 | break; |
10375 | case IORING_UNREGISTER_FILES: | |
10376 | ret = -EINVAL; | |
10377 | if (arg || nr_args) | |
10378 | break; | |
10379 | ret = io_sqe_files_unregister(ctx); | |
10380 | break; | |
c3a31e60 | 10381 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 10382 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 10383 | break; |
9b402849 | 10384 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 10385 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
10386 | ret = -EINVAL; |
10387 | if (nr_args != 1) | |
10388 | break; | |
10389 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
10390 | if (ret) |
10391 | break; | |
10392 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
10393 | ctx->eventfd_async = 1; | |
10394 | else | |
10395 | ctx->eventfd_async = 0; | |
9b402849 JA |
10396 | break; |
10397 | case IORING_UNREGISTER_EVENTFD: | |
10398 | ret = -EINVAL; | |
10399 | if (arg || nr_args) | |
10400 | break; | |
10401 | ret = io_eventfd_unregister(ctx); | |
10402 | break; | |
66f4af93 JA |
10403 | case IORING_REGISTER_PROBE: |
10404 | ret = -EINVAL; | |
10405 | if (!arg || nr_args > 256) | |
10406 | break; | |
10407 | ret = io_probe(ctx, arg, nr_args); | |
10408 | break; | |
071698e1 JA |
10409 | case IORING_REGISTER_PERSONALITY: |
10410 | ret = -EINVAL; | |
10411 | if (arg || nr_args) | |
10412 | break; | |
10413 | ret = io_register_personality(ctx); | |
10414 | break; | |
10415 | case IORING_UNREGISTER_PERSONALITY: | |
10416 | ret = -EINVAL; | |
10417 | if (arg) | |
10418 | break; | |
10419 | ret = io_unregister_personality(ctx, nr_args); | |
10420 | break; | |
7e84e1c7 SG |
10421 | case IORING_REGISTER_ENABLE_RINGS: |
10422 | ret = -EINVAL; | |
10423 | if (arg || nr_args) | |
10424 | break; | |
10425 | ret = io_register_enable_rings(ctx); | |
10426 | break; | |
21b55dbc SG |
10427 | case IORING_REGISTER_RESTRICTIONS: |
10428 | ret = io_register_restrictions(ctx, arg, nr_args); | |
10429 | break; | |
992da01a PB |
10430 | case IORING_REGISTER_FILES2: |
10431 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
10432 | break; | |
10433 | case IORING_REGISTER_FILES_UPDATE2: | |
10434 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10435 | IORING_RSRC_FILE); | |
10436 | break; | |
10437 | case IORING_REGISTER_BUFFERS2: | |
10438 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 10439 | break; |
992da01a PB |
10440 | case IORING_REGISTER_BUFFERS_UPDATE: |
10441 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
10442 | IORING_RSRC_BUFFER); | |
c3bdad02 | 10443 | break; |
fe76421d JA |
10444 | case IORING_REGISTER_IOWQ_AFF: |
10445 | ret = -EINVAL; | |
10446 | if (!arg || !nr_args) | |
10447 | break; | |
10448 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
10449 | break; | |
10450 | case IORING_UNREGISTER_IOWQ_AFF: | |
10451 | ret = -EINVAL; | |
10452 | if (arg || nr_args) | |
10453 | break; | |
10454 | ret = io_unregister_iowq_aff(ctx); | |
10455 | break; | |
2e480058 JA |
10456 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
10457 | ret = -EINVAL; | |
10458 | if (!arg || nr_args != 2) | |
10459 | break; | |
10460 | ret = io_register_iowq_max_workers(ctx, arg); | |
10461 | break; | |
edafccee JA |
10462 | default: |
10463 | ret = -EINVAL; | |
10464 | break; | |
10465 | } | |
10466 | ||
071698e1 | 10467 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 10468 | /* bring the ctx back to life */ |
05f3fb3c | 10469 | percpu_ref_reinit(&ctx->refs); |
0f158b4c | 10470 | reinit_completion(&ctx->ref_comp); |
05f3fb3c | 10471 | } |
edafccee JA |
10472 | return ret; |
10473 | } | |
10474 | ||
10475 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
10476 | void __user *, arg, unsigned int, nr_args) | |
10477 | { | |
10478 | struct io_ring_ctx *ctx; | |
10479 | long ret = -EBADF; | |
10480 | struct fd f; | |
10481 | ||
10482 | f = fdget(fd); | |
10483 | if (!f.file) | |
10484 | return -EBADF; | |
10485 | ||
10486 | ret = -EOPNOTSUPP; | |
10487 | if (f.file->f_op != &io_uring_fops) | |
10488 | goto out_fput; | |
10489 | ||
10490 | ctx = f.file->private_data; | |
10491 | ||
b6c23dd5 PB |
10492 | io_run_task_work(); |
10493 | ||
edafccee JA |
10494 | mutex_lock(&ctx->uring_lock); |
10495 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
10496 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
10497 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
10498 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
10499 | out_fput: |
10500 | fdput(f); | |
10501 | return ret; | |
10502 | } | |
10503 | ||
2b188cc1 JA |
10504 | static int __init io_uring_init(void) |
10505 | { | |
d7f62e82 SM |
10506 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
10507 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
10508 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
10509 | } while (0) | |
10510 | ||
10511 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
10512 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
10513 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
10514 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
10515 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
10516 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
10517 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
10518 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
10519 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
10520 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 10521 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
10522 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
10523 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
10524 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
10525 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
10526 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
10527 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
10528 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
10529 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
10530 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
10531 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
10532 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
10533 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
10534 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
10535 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
10536 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 10537 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
10538 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
10539 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 10540 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 10541 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 10542 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 10543 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 10544 | |
b0d658ec PB |
10545 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
10546 | sizeof(struct io_uring_rsrc_update)); | |
10547 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
10548 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
10549 | |
10550 | /* ->buf_index is u16 */ | |
10551 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
10552 | ||
b0d658ec PB |
10553 | /* should fit into one byte */ |
10554 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
10555 | ||
d3656344 | 10556 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
84557871 | 10557 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
16340eab | 10558 | |
91f245d5 JA |
10559 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
10560 | SLAB_ACCOUNT); | |
2b188cc1 JA |
10561 | return 0; |
10562 | }; | |
10563 | __initcall(io_uring_init); |